Skip to contentSkip to conversation

ovpn: support operations on interfaces in foreign netns

PATCH Superseded Marco Baffo 1 participant 4 messages Jul 29

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 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 v2 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. Update the Netlink specification accordingly.

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 v2:
- This is a new patch.

 Documentation/netlink/specs/ovpn.yaml |  6 ++
 drivers/net/ovpn/netlink.c            | 87 +++++++++++++++------------
 2 files changed, 53 insertions(+), 40 deletions(-)
Documentation/netlink/specs/ovpn.yaml +6 -0
index 1f0d4d6037c0..5dc998cce714 100644
@@ -453,6 +453,8 @@ operations:
             - peer
         reply:
           attributes:
+            - ifindex
+            - target-netnsid
             - peer
       dump:
         request:
@@ -461,6 +463,8 @@ operations:
             - target-netnsid
         reply:
           attributes:
+            - ifindex
+            - target-netnsid
             - peer
     -
       name: peer-del
@@ -509,6 +513,8 @@ operations:
             - keyconf
         reply:
           attributes:
+            - ifindex
+            - target-netnsid
             - keyconf
     -
       name: key-swap
drivers/net/ovpn/netlink.c +47 -40

139 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
#2 lore raw

PATCH v2 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 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 v2 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 v2:
- This is a new patch.

 tools/testing/selftests/net/ovpn/common.sh |  3 ++-
 tools/testing/selftests/net/ovpn/test.sh   | 22 ++++++++++++++++++++++
 2 files changed, 24 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 +22 -0
index 468dd7985c8f..d30cf250d59a 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,26 @@ 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 '.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