Skip to contentSkip to conversation

net: Support STP on bridge in non-root netns.

PATCH Applied Kuniyuki Iwashima 2 participants 6 messages Jul 18, 2023 – Jul 20, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover net: Support STP on bridge in non-root netns.

Currently, STP does not work in non-root netns as llc_rcv() drops packets from non-root netns.

This series fixes it by making some protocol handlers netns-aware, which are called from llc_rcv() as follows:

llc_rcv()

  |
  |- sap->rcv_func : registered by llc_sap_open()
  |
  |  * functions : regsitered by register_8022_client()
  |    -> No in-kernel user call register_8022_client()
  |
  |  * snap_rcv()
  |    |
  |    `- proto->rcvfunc() : registered by register_snap_client()
  |
  |       * aarp_rcv()  : drop packets from non-root netns
  |       * atalk_rcv() : drop packets from non-root netns
  |
  |  * stp_pdu_rcv()
  |    |
  |    `- garp_protos[]->rcv() : registered by stp_proto_register()
  |
  |       * garp_pdu_rcv() : netns-aware
  |       * br_stp_rcv()   : netns-aware
  |
  |- llc_type_handlers[llc_pdu_type(skb) - 1]
  |
  |  * llc_sap_handler()  : NOT netns-aware (Patch 1)
  |  * llc_conn_handler() : NOT netns-aware (Patch 2)
  |

`- llc_station_handler

* llc_station_rcv() : netns-aware

Patch 1 & 2 convert not-netns-aware functions and Patch 3 remove the netns restriction in llc_rcv().

Note this series does not namespacify AF_LLC so that these patches can be backported to stable without conflicts (at least to 4.14.y).

Another series that adds netns support for AF_LLC will be targeted to net-next later.

Changes:
v2:
* Patch 1 : Update changelog, s/in the following patch/soon/
* Patch 1 & 2 : Fix kdoc warning of make W=1

v1: https://lore.kernel.org/netdev/[email protected]/

Kuniyuki Iwashima (4):
llc: Check netns in llc_dgram_match().
llc: Check netns in llc_estab_match() and llc_listener_match().
llc: Don't drop packet from non-root netns.
Revert "bridge: Add extack warning when enabling STP in netns."

 include/net/llc_conn.h |  2 +-
 net/bridge/br_stp_if.c |  3 ---
 net/llc/af_llc.c       |  2 +-
 net/llc/llc_conn.c     | 49 ++++++++++++++++++++++++++----------------
 net/llc/llc_if.c       |  2 +-
 net/llc/llc_input.c    |  3 ---
 net/llc/llc_sap.c      | 18 ++++++++++------
 7 files changed, 44 insertions(+), 35 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/4 llc: Check netns in llc_dgram_match().

We will remove this restriction in llc_rcv() soon, which means that the protocol handler must be aware of netns.

	if (!net_eq(dev_net(dev), &init_net))
		goto drop;

llc_rcv() fetches llc_type_handlers[llc_pdu_type(skb) - 1] and calls it if not NULL.

If the PDU type is LLC_DEST_SAP, llc_sap_handler() is called to pass skb to corresponding sockets. Then, we must look up a proper socket in the same netns with skb->dev.

If the destination is a multicast address, llc_sap_handler() calls llc_sap_mcast(). It calculates a hash based on DSAP and skb->dev->ifindex, iterates on a socket list, and calls llc_mcast_match() to check if the socket is the correct destination. Then, llc_mcast_match() checks if skb->dev matches with llc_sk(sk)->dev. So, we need not check netns here.

OTOH, if the destination is a unicast address, llc_sap_handler() calls llc_lookup_dgram() to look up a socket, but it does not check the netns.

Therefore, we need to add netns check in llc_lookup_dgram().

---
 net/llc/llc_sap.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
net/llc/llc_sap.c +11 -7
index 6805ce43a055..116c0e479183 100644
@@ -294,25 +294,29 @@ static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
 
 static inline bool llc_dgram_match(const struct llc_sap *sap,
 				   const struct llc_addr *laddr,
-				   const struct sock *sk)
+				   const struct sock *sk,
+				   const struct net *net)
 {
      struct llc_sock *llc = llc_sk(sk);
 
      return sk->sk_type == SOCK_DGRAM &&
-	  llc->laddr.lsap == laddr->lsap &&
-	  ether_addr_equal(llc->laddr.mac, laddr->mac);
+	     net_eq(sock_net(sk), net) &&
+	     llc->laddr.lsap == laddr->lsap &&
+	     ether_addr_equal(llc->laddr.mac, laddr->mac);
 }
 
 /**
  *	llc_lookup_dgram - Finds dgram socket for the local sap/mac
  *	@sap: SAP
  *	@laddr: address of local LLC (MAC + SAP)
+ *	@net: netns to look up a socket in
  *
  *	Search socket list of the SAP and finds connection using the local
  *	mac, and local sap. Returns pointer for socket found, %NULL otherwise.
  */
 static struct sock *llc_lookup_dgram(struct llc_sap *sap,
-				     const struct llc_addr *laddr)
+				     const struct llc_addr *laddr,
+				     const struct net *net)
 {
 	struct sock *rc;
 	struct hlist_nulls_node *node;
@@ -322,12 +326,12 @@ static struct sock *llc_lookup_dgram(struct llc_sap *sap,
 	rcu_read_lock_bh();
 again:
 	sk_nulls_for_each_rcu(rc, node, laddr_hb) {
-		if (llc_dgram_match(sap, laddr, rc)) {
+		if (llc_dgram_match(sap, laddr, rc, net)) {
 			/* Extra checks required by SLAB_TYPESAFE_BY_RCU */
 			if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
 				goto again;
 			if (unlikely(llc_sk(rc)->sap != sap ||
-				     !llc_dgram_match(sap, laddr, rc))) {
+				     !llc_dgram_match(sap, laddr, rc, net))) {
 				sock_put(rc);
 				continue;
 			}
@@ -429,7 +433,7 @@ void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
 		llc_sap_mcast(sap, &laddr, skb);
 		kfree_skb(skb);
 	} else {
-		struct sock *sk = llc_lookup_dgram(sap, &laddr);
+		struct sock *sk = llc_lookup_dgram(sap, &laddr, dev_net(skb->dev));
 		if (sk) {
 			llc_sap_rcv(sap, skb, sk);
 			sock_put(sk);
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/4 llc: Check netns in llc_estab_match() and llc_listener_match().

In reply to Kuniyuki Iwashima

We will remove this restriction in llc_rcv() in the following patch, which means that the protocol handler must be aware of netns.

        if (!net_eq(dev_net(dev), &init_net))
                goto drop;

llc_rcv() fetches llc_type_handlers[llc_pdu_type(skb) - 1] and calls it if not NULL.

If the PDU type is LLC_DEST_CONN, llc_conn_handler() is called to pass skb to corresponding sockets. Then, we must look up a proper socket in the same netns with skb->dev.

llc_conn_handler() calls __llc_lookup() to look up a established or litening socket by __llc_lookup_established() and llc_lookup_listener().

Both functions iterate on a list and call llc_estab_match() or llc_listener_match() to check if the socket is the correct destination. However, these functions do not check netns.

Also, bind() and connect() call llc_establish_connection(), which finally calls __llc_lookup_established(), to check if there is a conflicting socket.

Let's test netns in llc_estab_match() and llc_listener_match().

---
 include/net/llc_conn.h |  2 +-
 net/llc/af_llc.c       |  2 +-
 net/llc/llc_conn.c     | 49 ++++++++++++++++++++++++++----------------
 net/llc/llc_if.c       |  2 +-
 4 files changed, 33 insertions(+), 22 deletions(-)
include/net/llc_conn.h +1 -1
index 2c1ea3414640..374411b3066c 100644
@@ -111,7 +111,7 @@ void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit);
 void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit);
 int llc_conn_remove_acked_pdus(struct sock *conn, u8 nr, u16 *how_many_unacked);
 struct sock *llc_lookup_established(struct llc_sap *sap, struct llc_addr *daddr,
-				    struct llc_addr *laddr);
+				    struct llc_addr *laddr, const struct net *net);
 void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk);
 void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk);
 
net/llc/af_llc.c +1 -1
index 57c35c960b2c..9b06c380866b 100644
@@ -402,7 +402,7 @@ static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
 		memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
 		laddr.lsap = addr->sllc_sap;
 		rc = -EADDRINUSE; /* mac + sap clash. */
-		ask = llc_lookup_established(sap, &daddr, &laddr);
+		ask = llc_lookup_established(sap, &daddr, &laddr, &init_net);
 		if (ask) {
 			sock_put(ask);
 			goto out_put;
net/llc/llc_conn.c +30 -19

144 lines not on this page · raw message

net/llc/llc_if.c +1 -1
index dde9bf08a593..58a5f419adc6 100644
@@ -92,7 +92,7 @@ int llc_establish_connection(struct sock *sk, const u8 *lmac, u8 *dmac, u8 dsap)
 	daddr.lsap = dsap;
 	memcpy(daddr.mac, dmac, sizeof(daddr.mac));
 	memcpy(laddr.mac, lmac, sizeof(laddr.mac));
-	existing = llc_lookup_established(llc->sap, &daddr, &laddr);
+	existing = llc_lookup_established(llc->sap, &daddr, &laddr, sock_net(sk));
 	if (existing) {
 		if (existing->sk_state == TCP_ESTABLISHED) {
 			sk = existing;
Signature · 1 line
2.30.2
#3 lore raw

PATCH v2 3/4 llc: Don't drop packet from non-root netns.

In reply to Kuniyuki Iwashima

Now these upper layer protocol handlers can be called from llc_rcv() as sap->rcv_func(), which is registered by llc_sap_open().

* function which is passed to register_8022_client()

    -> no in-kernel user calls register_8022_client().

* snap_rcv()

    `- proto->rcvfunc() : registered by register_snap_client()
       -> aarp_rcv() and atalk_rcv() drop packets from non-root netns

* stp_pdu_rcv()

    `- garp_protos[]->rcv() : registered by stp_proto_register()
       -> garp_pdu_rcv() and br_stp_rcv() are netns-aware

So, we can safely remove the netns restriction in llc_rcv().

Fixes e730c15519d0 ("[NET]: Make packet reception network namespace safe")
---
 net/llc/llc_input.c | 3 ---
 1 file changed, 3 deletions(-)
net/llc/llc_input.c +0 -3
index c309b72a5877..7cac441862e2 100644
@@ -163,9 +163,6 @@ int llc_rcv(struct sk_buff *skb, struct net_device *dev,
 	void (*sta_handler)(struct sk_buff *skb);
 	void (*sap_handler)(struct llc_sap *sap, struct sk_buff *skb);
 
-	if (!net_eq(dev_net(dev), &init_net))
-		goto drop;
-
 	/*
 	 * When the interface is in promisc. mode, drop all the crap that it
 	 * receives, do not try to analyse it.
Signature · 1 line
2.30.2
#4 lore raw

PATCH v2 4/4 Revert "bridge: Add extack warning when enabling STP in netns."

In reply to Kuniyuki Iwashima

This reverts commit 56a16035bb6effb37177867cea94c13a8382f745.

Since the previous commit, STP works on bridge in netns.

  # unshare -n
  # ip link add br0 type bridge
  # ip link add veth0 type veth peer name veth1
  # ip link set veth0 master br0 up
  [   50.558135] br0: port 1(veth0) entered blocking state
  [   50.558366] br0: port 1(veth0) entered disabled state
  [   50.558798] veth0: entered allmulticast mode
  [   50.564401] veth0: entered promiscuous mode
  # ip link set veth1 master br0 up
  [   54.215487] br0: port 2(veth1) entered blocking state
  [   54.215657] br0: port 2(veth1) entered disabled state
  [   54.215848] veth1: entered allmulticast mode
  [   54.219577] veth1: entered promiscuous mode
  # ip link set br0 type bridge stp_state 1
  # ip link set br0 up
  [   61.960726] br0: port 2(veth1) entered blocking state
  [   61.961097] br0: port 2(veth1) entered listening state
  [   61.961495] br0: port 1(veth0) entered blocking state
  [   61.961653] br0: port 1(veth0) entered listening state
  [   63.998835] br0: port 2(veth1) entered blocking state
  [   77.437113] br0: port 1(veth0) entered learning state
  [   86.653501] br0: received packet on veth0 with own address as source address (addr:6e:0f:e7:6f:5f:5f, vlan:0)
  [   92.797095] br0: port 1(veth0) entered forwarding state
  [   92.797398] br0: topology change detected, propagating

Let's remove the warning.

---
 net/bridge/br_stp_if.c | 3 ---
 1 file changed, 3 deletions(-)
net/bridge/br_stp_if.c +0 -3
index b65962682771..75204d36d7f9 100644
@@ -201,9 +201,6 @@ int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
 {
 	ASSERT_RTNL();
 
-	if (!net_eq(dev_net(br->dev), &init_net))
-		NL_SET_ERR_MSG_MOD(extack, "STP does not work in non-root netns");
-
 	if (br_mrp_enabled(br)) {
 		NL_SET_ERR_MSG_MOD(extack,
 				   "STP can't be enabled if MRP is already enabled");
Signature · 1 line
2.30.2
#5 lore raw
In reply to Kuniyuki Iwashima
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 32 lines · On Tue, 18 Jul 2023 10:41:48 -0700 you wrote:

Currently, STP does not work in non-root netns as llc_rcv() drops packets from non-root netns.

This series fixes it by making some protocol handlers netns-aware, which are called from llc_rcv() as follows:

llc_rcv()

  |
  |- sap->rcv_func : registered by llc_sap_open()
  |
  |  * functions : regsitered by register_8022_client()
  |    -> No in-kernel user call register_8022_client()
  |
  |  * snap_rcv()
  |    |
  |    `- proto->rcvfunc() : registered by register_snap_client()
  |
  |       * aarp_rcv()  : drop packets from non-root netns
  |       * atalk_rcv() : drop packets from non-root netns
  |
  |  * stp_pdu_rcv()
  |    |
  |    `- garp_protos[]->rcv() : registered by stp_proto_register()
  |
  |       * garp_pdu_rcv() : netns-aware
  |       * br_stp_rcv()   : netns-aware
  |
  |- llc_type_handlers[llc_pdu_type(skb) - 1]
  |
  |  * llc_sap_handler()  : NOT netns-aware (Patch 1)
  |  * llc_conn_handler() : NOT netns-aware (Patch 2)
  |
Quoted 3 lines

`- llc_station_handler

[...]

Here is the summary with links:
- [v2,net,1/4] llc: Check netns in llc_dgram_match().

    https://git.kernel.org/netdev/net/c/9b64e93e83c2
  - [v2,net,2/4] llc: Check netns in llc_estab_match() and llc_listener_match().
    https://git.kernel.org/netdev/net/c/97b1d320f48c
  - [v2,net,3/4] llc: Don't drop packet from non-root netns.
    https://git.kernel.org/netdev/net/c/6631463b6e66
  - [v2,net,4/4] Revert "bridge: Add extack warning when enabling STP in netns."
    https://git.kernel.org/netdev/net/c/7ebd00a5a20c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html