Skip to contentSkip to conversation

ovpn: support operations on interfaces in foreign netns

PATCH Superseded Marco Baffo 3 participants 8 messages Jul 29 – Sep 23

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 ovpn: support operations on interfaces in foreign netns

ovpn currently looks up the target interface in the network namespace associated with the netlink socket. This prevents a userspace process from controlling an ovpn interface located in another namespace.

Add the optional OVPN_A_TARGET_NETNSID attribute. When present, it identifies the namespace containing the target interface relative to the namespace of the requesting Netlink socket. Resolve the ID and perform the interface lookup there. When absent, preserve the existing lookup behavior.

Allow the attribute in all peer and key request policies, including peer dump requests.

---
Changes in v3:
- None.

Changes in v2:
- Improved Doc.

 Documentation/netlink/specs/ovpn.yaml | 27 ++++++++++++++++
 drivers/net/ovpn/netlink-gen.c        | 45 ++++++++++++++++-----------
 drivers/net/ovpn/netlink.c            | 17 ++++++++++
 include/uapi/linux/ovpn.h             |  1 +
 4 files changed, 72 insertions(+), 18 deletions(-)
Documentation/netlink/specs/ovpn.yaml +27 -0
index b0c782e59a32..1f0d4d6037c0 100644
@@ -337,12 +337,20 @@ attribute-sets:
         type: nest
         doc: Peer specific cipher configuration
         nested-attributes: keyconf
+      -
+        name: target-netnsid
+        type: s32
+        doc: >-
+          ID of the network namespace containing the ovpn interface, relative to
+          the network namespace of the requesting Netlink socket
   -
     name: ovpn-peer-new-input
     subset-of: ovpn
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: peer
         nested-attributes: peer-new-input
@@ -352,6 +360,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: peer
         nested-attributes: peer-set-input
@@ -361,6 +371,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: peer
         nested-attributes: peer-del-input
@@ -370,6 +382,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: keyconf
         nested-attributes: keyconf-get
@@ -379,6 +393,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: keyconf
         nested-attributes: keyconf-swap-input
@@ -388,6 +404,8 @@ attribute-sets:
     attributes:
       -
         name: ifindex
+      -
+        name: target-netnsid
       -
         name: keyconf
         nested-attributes: keyconf-del-input
@@ -405,6 +423,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-set
@@ -417,6 +436,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-get
@@ -429,6 +449,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
         reply:
           attributes:
@@ -437,6 +458,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
         reply:
           attributes:
             - peer
@@ -451,6 +473,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-del-ntf
@@ -469,6 +492,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
     -
       name: key-get
@@ -481,6 +505,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
         reply:
           attributes:
@@ -496,6 +521,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
     -
       name: key-swap-ntf
@@ -515,6 +541,7 @@ operations:
         request:
           attributes:
             - ifindex
+            - target-netnsid
             - keyconf
 
     -
drivers/net/ovpn/netlink-gen.c +27 -18

145 lines not on this page · raw message

drivers/net/ovpn/netlink.c +17 -0
index 4c66c1ec497e..b70ecfaf46c8 100644
@@ -38,6 +38,7 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
 {
 	struct ovpn_priv *ovpn;
 	struct net_device *dev;
+	struct net *target_net = NULL;
 	int ifindex;
 
 	if (GENL_REQ_ATTR_CHECK(info, OVPN_A_IFINDEX))
@@ -45,12 +46,26 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
 
 	ifindex = nla_get_u32(info->attrs[OVPN_A_IFINDEX]);
 
+	if (info->attrs[OVPN_A_TARGET_NETNSID]) {
+		/* Target netns IDs are relative to the requesting Netlink socket */
+		target_net = get_net_ns_by_id(net, nla_get_s32(info->attrs[OVPN_A_TARGET_NETNSID]));
+		if (!target_net) {
+			NL_SET_ERR_MSG_MOD(info->extack,
+					   "invalid target network namespace ID");
+			NL_SET_BAD_ATTR(info->extack,
+					info->attrs[OVPN_A_TARGET_NETNSID]);
+			return ERR_PTR(-EINVAL);
+		}
+		net = target_net;
+	}
+
 	rcu_read_lock();
 	dev = dev_get_by_index_rcu(net, ifindex);
 	if (!dev) {
 		rcu_read_unlock();
 		NL_SET_ERR_MSG_MOD(info->extack,
 				   "ifindex does not match any interface");
+		put_net(target_net);
 		return ERR_PTR(-ENODEV);
 	}
 
@@ -59,12 +74,14 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
 		NL_SET_ERR_MSG_MOD(info->extack,
 				   "specified interface is not ovpn");
 		NL_SET_BAD_ATTR(info->extack, info->attrs[OVPN_A_IFINDEX]);
+		put_net(target_net);
 		return ERR_PTR(-EINVAL);
 	}
 
 	ovpn = netdev_priv(dev);
 	netdev_hold(dev, tracker, GFP_ATOMIC);
 	rcu_read_unlock();
+	put_net(target_net);
 
 	return ovpn;
 }
include/uapi/linux/ovpn.h +1 -0
index 06690090a1a9..3fdf24182df4 100644
@@ -85,6 +85,7 @@ enum {
 	OVPN_A_IFINDEX = 1,
 	OVPN_A_PEER,
 	OVPN_A_KEYCONF,
+	OVPN_A_TARGET_NETNSID,
 
 	__OVPN_A_MAX,
 	OVPN_A_MAX = (__OVPN_A_MAX - 1)
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#1 lore raw

PATCH v3 2/4 ovpn: include target netns ID in notifications

ovpn notifications are multicast in the network namespace of the peer transport socket, but carry an ifindex from the ovpn device namespace. If these namespaces differ, the ifindex alone is ambiguous to listeners.

Include the device namespace ID relative to the receiving namespace when the two namespaces differ.

Notifications may be generated from softirq context, so use peernet2id() without allocating a namespace ID. When the Netlink control and transport sockets share a namespace, the mapping normally already exists because it was needed to address the foreign interface. Otherwise, report NETNSA_NSID_NOT_ASSIGNED unless userspace established the mapping beforehand.

Fixes 89d3c0e4612a ("ovpn: kill key and notify userspace in case of IV exhaustion")
Fixes a215d253c17a ("ovpn: notify userspace when a peer is deleted")
Fixes c841b676da98 ("ovpn: notify userspace on client float event")

---
Changes in v3:
- Removed changes in the ovpn.yaml .
- Changed peernet2id_alloc() to peernet2id() to avoid potential deadlock when the notication is send from softirq context (peer-float, key-swap).

Changes in v2:
- This is a new patch.

 drivers/net/ovpn/netlink.c | 90 +++++++++++++++++++++-----------------
 1 file changed, 50 insertions(+), 40 deletions(-)
drivers/net/ovpn/netlink.c +50 -40
index b70ecfaf46c8..81953467e6fb 100644
@@ -1167,6 +1167,47 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info)
 	return 0;
 }
 
+static int ovpn_nl_send_notify(struct ovpn_peer *peer, struct sk_buff *msg,
+			       void *hdr)
+{
+	struct ovpn_socket *sock;
+	struct net *net_sock, *net_dev;
+	int netnsid, ret = 0;
+
+	rcu_read_lock();
+	sock = rcu_dereference(peer->sock);
+	if (!sock) {
+		ret = -EINVAL;
+		goto unlock;
+	}
+
+	net_sock = sock_net(sock->sk);
+	net_dev = dev_net(peer->ovpn->dev);
+
+	/* The notification is delivered in the transport socket's netns. If the
+	 * ovpn device is elsewhere, report its netns ID relative to that netns.
+	 */
+	if (!net_eq(net_sock, net_dev)) {
+		/* Peer-float and key-swap notifications may be generated from
+		 * softirq context, so do not allocate an NSID here. If none
+		 * exists, peernet2id() returns NETNSA_NSID_NOT_ASSIGNED.
+		 */
+		netnsid = peernet2id(net_sock, net_dev);
+		if (nla_put_s32(msg, OVPN_A_TARGET_NETNSID, netnsid)) {
+			ret = -EMSGSIZE;
+			goto unlock;
+		}
+	}
+
+	genlmsg_end(msg, hdr);
+	genlmsg_multicast_netns(&ovpn_nl_family, net_sock, msg, 0,
+				OVPN_NLGRP_PEERS, GFP_ATOMIC);
+unlock:
+	rcu_read_unlock();
+
+	return ret;
+}
+
 /**
  * ovpn_nl_peer_del_notify - notify userspace about peer being deleted
  * @peer: the peer being deleted
@@ -1175,7 +1216,6 @@ int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info)
  */
 int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 {
-	struct ovpn_socket *sock;
 	struct sk_buff *msg;
 	struct nlattr *attr;
 	int ret = -EMSGSIZE;
@@ -1209,22 +1249,12 @@ int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 
 	nla_nest_end(msg, attr);
 
-	genlmsg_end(msg, hdr);
-
-	rcu_read_lock();
-	sock = rcu_dereference(peer->sock);
-	if (!sock) {
-		ret = -EINVAL;
-		goto err_unlock;
-	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
-				OVPN_NLGRP_PEERS, GFP_ATOMIC);
-	rcu_read_unlock();
+	ret = ovpn_nl_send_notify(peer, msg, hdr);
+	if (ret < 0)
+		goto err_cancel_msg;
 
 	return 0;
 
-err_unlock:
-	rcu_read_unlock();
 err_cancel_msg:
 	genlmsg_cancel(msg, hdr);
 err_free_msg:
@@ -1242,7 +1272,6 @@ int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
 int ovpn_nl_peer_float_notify(struct ovpn_peer *peer,
 			      const struct sockaddr_storage *ss)
 {
-	struct ovpn_socket *sock;
 	struct sockaddr_in6 *sa6;
 	struct sockaddr_in *sa;
 	struct sk_buff *msg;
@@ -1291,22 +1320,13 @@ int ovpn_nl_peer_float_notify(struct ovpn_peer *peer,
 	}
 
 	nla_nest_end(msg, attr);
-	genlmsg_end(msg, hdr);
 
-	rcu_read_lock();
-	sock = rcu_dereference(peer->sock);
-	if (!sock) {
-		ret = -EINVAL;
-		goto err_unlock;
-	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg,
-				0, OVPN_NLGRP_PEERS, GFP_ATOMIC);
-	rcu_read_unlock();
+	ret = ovpn_nl_send_notify(peer, msg, hdr);
+	if (ret < 0)
+		goto err_cancel_msg;
 
 	return 0;
 
-err_unlock:
-	rcu_read_unlock();
 err_cancel_msg:
 	genlmsg_cancel(msg, hdr);
 err_free_msg:
@@ -1323,7 +1343,6 @@ int ovpn_nl_peer_float_notify(struct ovpn_peer *peer,
  */
 int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
 {
-	struct ovpn_socket *sock;
 	struct nlattr *k_attr;
 	struct sk_buff *msg;
 	int ret = -EMSGSIZE;
@@ -1356,21 +1375,12 @@ int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
 		goto err_cancel_msg;
 
 	nla_nest_end(msg, k_attr);
-	genlmsg_end(msg, hdr);
 
-	rcu_read_lock();
-	sock = rcu_dereference(peer->sock);
-	if (!sock) {
-		ret = -EINVAL;
-		goto err_unlock;
-	}
-	genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
-				OVPN_NLGRP_PEERS, GFP_ATOMIC);
-	rcu_read_unlock();
+	ret = ovpn_nl_send_notify(peer, msg, hdr);
+	if (ret < 0)
+		goto err_cancel_msg;
 
 	return 0;
-err_unlock:
-	rcu_read_unlock();
 err_cancel_msg:
 	genlmsg_cancel(msg, hdr);
 err_free_msg:
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#2 lore raw

PATCH v3 3/4 selftests: ovpn: test operations in a foreign netns

In reply to Marco Baffo

Extend ovpn-cli with -n to resolve an interface in a named network namespace and include OVPN_A_TARGET_NETNSID in peer and key requests.

Use -n for existing peer and key operations that do not create transport sockets, exercising foreign-netns requests throughout the test suite.

---
Changes in v3:
- None.

Changes in v2:
- Used "ovpn-cli -n" instead of "ip netns exec ovpn-cli" where possible.
- Removed additional tests since we modified the existing ones to exercise the new code path.

 tools/testing/selftests/net/ovpn/common.sh    |   8 +-
 tools/testing/selftests/net/ovpn/ovpn-cli.c   | 266 +++++++++++++++++-
 .../selftests/net/ovpn/test-close-socket.sh   |   4 +-
 tools/testing/selftests/net/ovpn/test-mark.sh |   6 +-
 tools/testing/selftests/net/ovpn/test.sh      |  50 ++--
 5 files changed, 289 insertions(+), 45 deletions(-)
tools/testing/selftests/net/ovpn/common.sh +4 -4
index 2d844eb3aa6e..4f2510fb6d7f 100644
@@ -221,7 +221,7 @@ ovpn_add_peer() {
 				${OVPN_UDP_PEERS_FILE}
 
 			for p in $(seq 1 ${OVPN_NUM_PEERS}); do
-				ip netns exec "${server_ns}" ${OVPN_CLI} \
+				"${OVPN_CLI}" -n "${server_ns}" \
 					new_key tun0 ${p} 1 0 ${OVPN_ALG} 0 \
 					data64.key
 			done
@@ -244,7 +244,7 @@ ovpn_add_peer() {
 			ip netns exec "${peer_ns}" ${OVPN_CLI} new_peer \
 				tun${1} ${PEER_ID} ${TX_ID} ${LPORT} ${RADDR} \
 				${RPORT}
-			ip netns exec "${peer_ns}" ${OVPN_CLI} new_key tun${1} \
+			"${OVPN_CLI}" -n "${peer_ns}" new_key tun${1} \
 				${PEER_ID} 1 0 ${OVPN_ALG} 1 data64.key
 		fi
 	else
@@ -252,8 +252,8 @@ ovpn_add_peer() {
 			(ip netns exec "${server_ns}" ${OVPN_CLI} listen tun0 \
 				1 ${M_ID} ${OVPN_TCP_PEERS_FILE} && {
 				for p in $(seq 1 ${OVPN_NUM_PEERS}); do
-					ip netns exec "${server_ns}" \
-						${OVPN_CLI} new_key tun0 ${p} \
+					"${OVPN_CLI}" -n "${server_ns}" \
+						new_key tun0 ${p} \
 						1 0 ${OVPN_ALG} 0 data64.key
 				done
 			}) &
tools/testing/selftests/net/ovpn/ovpn-cli.c +255 -11

359 lines not on this page · raw message

tools/testing/selftests/net/ovpn/test-close-socket.sh +2 -2
index ec9a51bbf3c9..0e4e33b2973f 100755
@@ -40,10 +40,10 @@ ovpn_prepare_network() {
 	for p in $(seq 1 ${OVPN_NUM_PEERS}); do
 		peer_ns="ovpn_peer${p}"
 		ovpn_cmd_ok "set peer0 timeout for peer ${p}" \
-			ip netns exec ovpn_peer0 ${OVPN_CLI} set_peer tun0 \
+			"${OVPN_CLI}" -n ovpn_peer0 set_peer tun0 \
 				${p} 60 120
 		ovpn_cmd_ok "set peer${p} timeout for peer ${p}" \
-			ip netns exec "${peer_ns}" ${OVPN_CLI} set_peer \
+			"${OVPN_CLI}" -n "${peer_ns}" set_peer \
 				tun${p} $((p + OVPN_ID_OFFSET)) 60 120
 	done
 }
tools/testing/selftests/net/ovpn/test-mark.sh +3 -3
index 7c1d56e9c525..8a9ca52f86be 100755
@@ -42,7 +42,7 @@ ovpn_mark_prepare_network() {
 			ASYMM "${OVPN_UDP_PEERS_FILE}" "${MARK}"
 	for p in $(seq 1 3); do
 		ovpn_cmd_ok "install server key for peer ${p}" \
-			ip netns exec ovpn_peer0 "${OVPN_CLI}" new_key tun0 \
+			"${OVPN_CLI}" -n ovpn_peer0 new_key tun0 \
 				"${p}" 1 0 "${OVPN_ALG}" 0 data64.key
 	done
 
@@ -53,10 +53,10 @@ ovpn_mark_prepare_network() {
 	for p in $(seq 1 3); do
 		peer_ns="ovpn_peer${p}"
 		ovpn_cmd_ok "set peer0 timeout for peer ${p}" \
-			ip netns exec ovpn_peer0 "${OVPN_CLI}" set_peer tun0 \
+			"${OVPN_CLI}" -n ovpn_peer0 set_peer tun0 \
 				"${p}" 60 120
 		ovpn_cmd_ok "set peer${p} timeout for peer ${p}" \
-			ip netns exec "${peer_ns}" "${OVPN_CLI}" set_peer \
+			"${OVPN_CLI}" -n "${peer_ns}" set_peer \
 				tun"${p}" $((p + OVPN_ID_OFFSET)) 60 120
 	done
 }
tools/testing/selftests/net/ovpn/test.sh +25 -25

134 lines not on this page · raw message

Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#3 lore raw

PATCH v3 4/4 selftests: ovpn: validate target netns ID in notifications

In reply to Marco Baffo

Move one peer interface to a foreign namespace while keeping its transport socket in the original namespace. Verify that its deletion notification contains the expected target namespace ID.

---
Changes in v3:
- Ignore unrelated events when validating
notification target netns IDs.

Changes in v2:
- This is a new patch.

 tools/testing/selftests/net/ovpn/common.sh |  3 ++-
 tools/testing/selftests/net/ovpn/test.sh   | 25 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
tools/testing/selftests/net/ovpn/common.sh +2 -1
index 4f2510fb6d7f..2b90b7bf8700 100644
@@ -21,7 +21,8 @@ export OVPN_ID_OFFSET=$(( 9 * (OVPN_SYMMETRIC_ID == 0) ))
 
 OVPN_JQ_FILTER='map(if type == "array" then .[] else . end) |
 	map(select(.msg.peer | has("remote-ipv6") | not)) |
-	map(del(.msg.ifindex)) | sort_by(.msg.peer.id)[]'
+	map(del(.msg.ifindex, .msg["target-netnsid"])) |
+	sort_by(.msg.peer.id)[]'
 OVPN_LAN_IP="11.11.11.11"
 
 declare -A OVPN_TMP_JSONS=()
tools/testing/selftests/net/ovpn/test.sh +25 -0
index 468dd7985c8f..52e482641016 100755
@@ -270,8 +270,13 @@ ovpn_run_timeouts() {
 
 	ovpn_log "Setting timeout to 3s P2P:"
 
+	# The transport socket stays in peer3 when its interface is moved.
+	ovpn_cmd_ok "move peer3 interface to peer0" \
+		ip -n ovpn_peer3 link set tun3 netns ovpn_peer0
+
 	for p in $(seq 3 ${OVPN_NUM_PEERS}); do
 		peer_ns="ovpn_peer${p}"
+		[ "${p}" -eq 3 ] && peer_ns="ovpn_peer0"
 		ovpn_cmd_ok "set peer${p} P2P timeout" \
 			"${OVPN_CLI}" -n "${peer_ns}" set_peer \
 				tun${p} $((p + OVPN_ID_OFFSET)) 3 3
@@ -279,9 +284,29 @@ ovpn_run_timeouts() {
 	sleep 5
 }
 
+ovpn_validate_ntf_netnsid() {
+	local peer="$1"
+	local target="$2"
+	local expected_netnsid
+	local received_netnsid
+
+	expected_netnsid=$(ip -n "ovpn_peer${peer}" -j netns list-id |
+		jq -er --arg target "${target}" \
+			'.[] | select(.name == $target) | .nsid')
+	received_netnsid=$(jq -er \
+		'select(.name == "peer-del-ntf" and
+			(.msg | has("target-netnsid"))) |
+		 .msg["target-netnsid"]' \
+		"${OVPN_TMP_JSONS[$peer]}")
+
+	[ "${received_netnsid}" -eq "${expected_netnsid}" ]
+}
+
 ovpn_run_notifications() {
 	local p
 
+	ovpn_cmd_ok "validate peer3 target netns ID" \
+		ovpn_validate_ntf_netnsid 3 ovpn_peer0
 	for p in $(seq 0 ${OVPN_NUM_PEERS}); do
 		ovpn_cmd_ok "validate listener output for peer ${p}" \
 			ovpn_compare_ntfs "${p}"
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#4 lore raw
In reply to Marco Baffo

Hi Sabrina,

I hope you're doing well!

Do you have any strong opinion on this patchset?
I was eventually hoping to get it to net-next, because it would simplify userspace a bit, when dealing with network namespaces.

Please, let me know what you think.

Thanks a lot!

Regards,

Quoted 393 lines · Marco Baffo

ovpn currently looks up the target interface in the network namespace associated with the netlink socket. This prevents a userspace process from controlling an ovpn interface located in another namespace.

Add the optional OVPN_A_TARGET_NETNSID attribute. When present, it identifies the namespace containing the target interface relative to the namespace of the requesting Netlink socket. Resolve the ID and perform the interface lookup there. When absent, preserve the existing lookup behavior.

Allow the attribute in all peer and key request policies, including peer dump requests.

---
Changes in v3:
- None.

Changes in v2:
- Improved Doc.

  Documentation/netlink/specs/ovpn.yaml | 27 ++++++++++++++++
  drivers/net/ovpn/netlink-gen.c        | 45 ++++++++++++++++-----------
  drivers/net/ovpn/netlink.c            | 17 ++++++++++
  include/uapi/linux/ovpn.h             |  1 +
  4 files changed, 72 insertions(+), 18 deletions(-)
Documentation/netlink/specs/ovpn.yaml +27 -0
index b0c782e59a32..1f0d4d6037c0 100644
@@ -337,12 +337,20 @@ attribute-sets:
          type: nest
          doc: Peer specific cipher configuration
          nested-attributes: keyconf
+      -
+        name: target-netnsid
+        type: s32
+        doc: >-
+          ID of the network namespace containing the ovpn interface, relative to
+          the network namespace of the requesting Netlink socket
    -
      name: ovpn-peer-new-input
      subset-of: ovpn
      attributes:
        -
          name: ifindex
+      -
+        name: target-netnsid
        -
          name: peer
          nested-attributes: peer-new-input
@@ -352,6 +360,8 @@ attribute-sets:
      attributes:
        -
          name: ifindex
+      -
+        name: target-netnsid
        -
          name: peer
          nested-attributes: peer-set-input
@@ -361,6 +371,8 @@ attribute-sets:
      attributes:
        -
          name: ifindex
+      -
+        name: target-netnsid
        -
          name: peer
          nested-attributes: peer-del-input
@@ -370,6 +382,8 @@ attribute-sets:
      attributes:
        -
          name: ifindex
+      -
+        name: target-netnsid
        -
          name: keyconf
          nested-attributes: keyconf-get
@@ -379,6 +393,8 @@ attribute-sets:
      attributes:
        -
          name: ifindex
+      -
+        name: target-netnsid
        -
          name: keyconf
          nested-attributes: keyconf-swap-input
@@ -388,6 +404,8 @@ attribute-sets:
      attributes:
        -
          name: ifindex
+      -
+        name: target-netnsid
        -
          name: keyconf
          nested-attributes: keyconf-del-input
@@ -405,6 +423,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - peer
      -
        name: peer-set
@@ -417,6 +436,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - peer
      -
        name: peer-get
@@ -429,6 +449,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - peer
          reply:
            attributes:
@@ -437,6 +458,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
          reply:
            attributes:
              - peer
@@ -451,6 +473,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - peer
      -
        name: peer-del-ntf
@@ -469,6 +492,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - keyconf
      -
        name: key-get
@@ -481,6 +505,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - keyconf
          reply:
            attributes:
@@ -496,6 +521,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - keyconf
      -
        name: key-swap-ntf
@@ -515,6 +541,7 @@ operations:
          request:
            attributes:
              - ifindex
+            - target-netnsid
              - keyconf
 
      -
drivers/net/ovpn/netlink-gen.c +27 -18
index 2147cec7c2c5..3f513e3675a3 100644
@@ -118,55 +118,64 @@ const struct nla_policy ovpn_peer_set_input_nl_policy[OVPN_A_PEER_TX_ID + 1] = {
  };
 
  /* OVPN_CMD_PEER_NEW - do */
-static const struct nla_policy ovpn_peer_new_nl_policy[OVPN_A_PEER + 1] = {
+static const struct nla_policy ovpn_peer_new_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_peer_new_input_nl_policy),
  };
 
  /* OVPN_CMD_PEER_SET - do */
-static const struct nla_policy ovpn_peer_set_nl_policy[OVPN_A_PEER + 1] = {
+static const struct nla_policy ovpn_peer_set_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_peer_set_input_nl_policy),
  };
 
  /* OVPN_CMD_PEER_GET - do */
-static const struct nla_policy ovpn_peer_get_do_nl_policy[OVPN_A_PEER + 1] = {
+static const struct nla_policy ovpn_peer_get_do_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_peer_nl_policy),
  };
 
  /* OVPN_CMD_PEER_GET - dump */
-static const struct nla_policy ovpn_peer_get_dump_nl_policy[OVPN_A_IFINDEX + 1] = {
+static const struct nla_policy ovpn_peer_get_dump_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  };
 
  /* OVPN_CMD_PEER_DEL - do */
-static const struct nla_policy ovpn_peer_del_nl_policy[OVPN_A_PEER + 1] = {
+static const struct nla_policy ovpn_peer_del_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_PEER] = NLA_POLICY_NESTED(ovpn_peer_del_input_nl_policy),
  };
 
  /* OVPN_CMD_KEY_NEW - do */
-static const struct nla_policy ovpn_key_new_nl_policy[OVPN_A_KEYCONF + 1] = {
+static const struct nla_policy ovpn_key_new_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_KEYCONF] = NLA_POLICY_NESTED(ovpn_keyconf_nl_policy),
  };
 
  /* OVPN_CMD_KEY_GET - do */
-static const struct nla_policy ovpn_key_get_nl_policy[OVPN_A_KEYCONF + 1] = {
+static const struct nla_policy ovpn_key_get_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_KEYCONF] = NLA_POLICY_NESTED(ovpn_keyconf_get_nl_policy),
  };
 
  /* OVPN_CMD_KEY_SWAP - do */
-static const struct nla_policy ovpn_key_swap_nl_policy[OVPN_A_KEYCONF + 1] = {
+static const struct nla_policy ovpn_key_swap_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_KEYCONF] = NLA_POLICY_NESTED(ovpn_keyconf_swap_input_nl_policy),
  };
 
  /* OVPN_CMD_KEY_DEL - do */
-static const struct nla_policy ovpn_key_del_nl_policy[OVPN_A_KEYCONF + 1] = {
+static const struct nla_policy ovpn_key_del_nl_policy[OVPN_A_TARGET_NETNSID + 1] = {
  	[OVPN_A_IFINDEX] = { .type = NLA_U32, },
+	[OVPN_A_TARGET_NETNSID] = { .type = NLA_S32, },
  	[OVPN_A_KEYCONF] = NLA_POLICY_NESTED(ovpn_keyconf_del_input_nl_policy),
  };
 
@@ -178,7 +187,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_peer_new_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_peer_new_nl_policy,
-		.maxattr	= OVPN_A_PEER,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
@@ -187,7 +196,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_peer_set_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_peer_set_nl_policy,
-		.maxattr	= OVPN_A_PEER,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
@@ -196,14 +205,14 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_peer_get_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_peer_get_do_nl_policy,
-		.maxattr	= OVPN_A_PEER,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
  		.cmd		= OVPN_CMD_PEER_GET,
  		.dumpit		= ovpn_nl_peer_get_dumpit,
  		.policy		= ovpn_peer_get_dump_nl_policy,
-		.maxattr	= OVPN_A_IFINDEX,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DUMP,
  	},
  	{
@@ -212,7 +221,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_peer_del_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_peer_del_nl_policy,
-		.maxattr	= OVPN_A_PEER,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
@@ -221,7 +230,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_key_new_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_key_new_nl_policy,
-		.maxattr	= OVPN_A_KEYCONF,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
@@ -230,7 +239,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_key_get_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_key_get_nl_policy,
-		.maxattr	= OVPN_A_KEYCONF,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
@@ -239,7 +248,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_key_swap_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_key_swap_nl_policy,
-		.maxattr	= OVPN_A_KEYCONF,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  	{
@@ -248,7 +257,7 @@ static const struct genl_split_ops ovpn_nl_ops[] = {
  		.doit		= ovpn_nl_key_del_doit,
  		.post_doit	= ovpn_nl_post_doit,
  		.policy		= ovpn_key_del_nl_policy,
-		.maxattr	= OVPN_A_KEYCONF,
+		.maxattr	= OVPN_A_TARGET_NETNSID,
  		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
  	},
  };
drivers/net/ovpn/netlink.c +17 -0
index 4c66c1ec497e..b70ecfaf46c8 100644
@@ -38,6 +38,7 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
  {
  	struct ovpn_priv *ovpn;
  	struct net_device *dev;
+	struct net *target_net = NULL;
  	int ifindex;
 
  	if (GENL_REQ_ATTR_CHECK(info, OVPN_A_IFINDEX))
@@ -45,12 +46,26 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
 
  	ifindex = nla_get_u32(info->attrs[OVPN_A_IFINDEX]);
 
+	if (info->attrs[OVPN_A_TARGET_NETNSID]) {
+		/* Target netns IDs are relative to the requesting Netlink socket */
+		target_net = get_net_ns_by_id(net, nla_get_s32(info->attrs[OVPN_A_TARGET_NETNSID]));
+		if (!target_net) {
+			NL_SET_ERR_MSG_MOD(info->extack,
+					   "invalid target network namespace ID");
+			NL_SET_BAD_ATTR(info->extack,
+					info->attrs[OVPN_A_TARGET_NETNSID]);
+			return ERR_PTR(-EINVAL);
+		}
+		net = target_net;
+	}
+
  	rcu_read_lock();
  	dev = dev_get_by_index_rcu(net, ifindex);
  	if (!dev) {
  		rcu_read_unlock();
  		NL_SET_ERR_MSG_MOD(info->extack,
  				   "ifindex does not match any interface");
+		put_net(target_net);
  		return ERR_PTR(-ENODEV);
  	}
 
@@ -59,12 +74,14 @@ ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
  		NL_SET_ERR_MSG_MOD(info->extack,
  				   "specified interface is not ovpn");
  		NL_SET_BAD_ATTR(info->extack, info->attrs[OVPN_A_IFINDEX]);
+		put_net(target_net);
  		return ERR_PTR(-EINVAL);
  	}
 
  	ovpn = netdev_priv(dev);
  	netdev_hold(dev, tracker, GFP_ATOMIC);
  	rcu_read_unlock();
+	put_net(target_net);
 
  	return ovpn;
  }
include/uapi/linux/ovpn.h +1 -0
index 06690090a1a9..3fdf24182df4 100644
@@ -85,6 +85,7 @@ enum {
  	OVPN_A_IFINDEX = 1,
  	OVPN_A_PEER,
  	OVPN_A_KEYCONF,
+	OVPN_A_TARGET_NETNSID,
 
  	__OVPN_A_MAX,
  	OVPN_A_MAX = (__OVPN_A_MAX - 1)
Signature · 8 lines
Antonio Quartulli



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#5 lore raw
Quoted 5 lines · Antonio Quartulli

Hi Sabrina,

I hope you're doing well!

Do you have any strong opinion on this patchset?

FTR - there is a v4 of this patchset.

This is not the last version on the list.

Regards,

Signature · 8 lines
Antonio Quartulli



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#6 lore raw
In reply to Antonio Quartulli

Hi Antonio (and the rest of the ovpn crew),

Quoted 3 lines · Antonio Quartulli

Hi Sabrina,

I hope you're doing well!

Yeah. Sorry, I've been struggling to keep up with upstream activity for a while, and especially with ovpn now that there's so many people working on it.

Quoted 5 lines

Do you have any strong opinion on this patchset?
I was eventually hoping to get it to net-next, because it would simplify userspace a bit, when dealing with network namespaces.

Please, let me know what you think.

I've mentioned the permissions issue before, the extra CAP_NET_ADMIN check that Marco mentioned probably does the trick.

Since there's only one intended user of ovpn (the "official" openvpn client, though of course you never what happens in the future), make sure to pick the "right" way of identifying the netns. I've been a bit traumatized by rtnl_link_get_net_by_nlattr()...

Signature · 7 lines
Sabrina


_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel