Skip to contentSkip to conversation

net: ipv4/ipv6: new option to accept garp/untracked na only if in-network

PATCH Applied Jaehee Park 3 participants 7 messages Jul 13, 2022 – Jul 18, 2022

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

v3 cover net: ipv4/ipv6: new option to accept garp/untracked na only if in-network

The first patch adds an option to learn a neighbor from garp only if the source ip is in the same subnet as an address configured on the interface that received the garp message. The option has been added to arp_accept in ipv4.

The same feature has been added to ndisc (patch 2). For ipv6, the subnet filtering knob is an extension of the accept_untracked_na option introduced in these patches:
https://lore.kernel.org/all/[email protected]/t/
https://lore.kernel.org/netdev/[email protected]/T/

The third patch contains selftests for testing the different options for accepting arp and neighbor advertisements.

v3
- fixed tabs in the selftest

v2
- reworded documentation and commit messages
- cleanup selftest

Jaehee Park (3):
net: ipv4: new arp_accept option to accept garp only if in-network
net: ipv6: new accept_untracked_na option to accept na only if
in-network
selftests: net: arp_ndisc_untracked_subnets: test for arp_accept and
accept_untracked_na

 Documentation/networking/ip-sysctl.rst        |  52 +--
 include/linux/inetdevice.h                    |   2 +-
 net/ipv4/arp.c                                |  24 +-
 net/ipv6/addrconf.c                           |   2 +-
 net/ipv6/ndisc.c                              |  29 +-
 tools/testing/selftests/net/Makefile          |   1 +
 .../net/arp_ndisc_untracked_subnets.sh        | 308 ++++++++++++++++++
 7 files changed, 389 insertions(+), 29 deletions(-)
 create mode 100755 tools/testing/selftests/net/arp_ndisc_untracked_subnets.sh
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/3 net: ipv4: new arp_accept option to accept garp only if in-network

In many deployments, we want the option to not learn a neighbor from garp if the src ip is not in the same subnet as an address configured on the interface that received the garp message. net.ipv4.arp_accept sysctl is currently used to control creation of a neigh from a received garp packet. This patch adds a new option '2' to net.ipv4.arp_accept which extends option '1' by including the subnet check.

---
 Documentation/networking/ip-sysctl.rst |  9 ++++++---
 include/linux/inetdevice.h             |  2 +-
 net/ipv4/arp.c                         | 24 ++++++++++++++++++++++--
 3 files changed, 29 insertions(+), 6 deletions(-)
Documentation/networking/ip-sysctl.rst +6 -3
index 4c8bbf5acfd1..5c017fc1e24d 100644
@@ -1633,12 +1633,15 @@ arp_notify - BOOLEAN
 	     or hardware address changes.
 	 ==  ==========================================================
 
-arp_accept - BOOLEAN
-	Define behavior for gratuitous ARP frames who's IP is not
-	already present in the ARP table:
+arp_accept - INTEGER
+	Define behavior for accepting gratuitous ARP (garp) frames from devices
+	that are not already present in the ARP table:
 
 	- 0 - don't create new entries in the ARP table
 	- 1 - create new entries in the ARP table
+	- 2 - create new entries only if the source IP address is in the same
+	  subnet as an address configured on the interface that received the
+	  garp message.
 
 	Both replies and requests type gratuitous arp will trigger the
 	ARP table to be updated, if this setting is on.
include/linux/inetdevice.h +1 -1
index ead323243e7b..ddb27fc0ee8c 100644
@@ -131,7 +131,7 @@ static inline void ipv4_devconf_setall(struct in_device *in_dev)
 	IN_DEV_ORCONF((in_dev), IGNORE_ROUTES_WITH_LINKDOWN)
 
 #define IN_DEV_ARPFILTER(in_dev)	IN_DEV_ORCONF((in_dev), ARPFILTER)
-#define IN_DEV_ARP_ACCEPT(in_dev)	IN_DEV_ORCONF((in_dev), ARP_ACCEPT)
+#define IN_DEV_ARP_ACCEPT(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_ACCEPT)
 #define IN_DEV_ARP_ANNOUNCE(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_ANNOUNCE)
 #define IN_DEV_ARP_IGNORE(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_IGNORE)
 #define IN_DEV_ARP_NOTIFY(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_NOTIFY)
net/ipv4/arp.c +22 -2
index af2f12ffc9ca..87c7e3fc5197 100644
@@ -429,6 +429,26 @@ static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
 	return !inet_confirm_addr(net, in_dev, sip, tip, scope);
 }
 
+static int arp_accept(struct in_device *in_dev, __be32 sip)
+{
+	struct net *net = dev_net(in_dev->dev);
+	int scope = RT_SCOPE_LINK;
+
+	switch (IN_DEV_ARP_ACCEPT(in_dev)) {
+	case 0: /* Don't create new entries from garp */
+		return 0;
+	case 1: /* Create new entries from garp */
+		return 1;
+	case 2: /* Create a neighbor in the arp table only if sip
+		 * is in the same subnet as an address configured
+		 * on the interface that received the garp message
+		 */
+		return !!inet_confirm_addr(net, in_dev, sip, 0, scope);
+	default:
+		return 0;
+	}
+}
+
 static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
 {
 	struct rtable *rt;
@@ -868,12 +888,12 @@ static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
 	n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
 
 	addr_type = -1;
-	if (n || IN_DEV_ARP_ACCEPT(in_dev)) {
+	if (n || arp_accept(in_dev, sip)) {
 		is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
 				      sip, tip, sha, tha);
 	}
 
-	if (IN_DEV_ARP_ACCEPT(in_dev)) {
+	if (arp_accept(in_dev, sip)) {
 		/* Unsolicited ARP is not accepted by default.
 		   It is possible, that this option should be enabled for some
 		   devices (strip is candidate)
Signature · 1 line
2.30.2
#2 lore raw

PATCH v3 2/3 net: ipv6: new accept_untracked_na option to accept na only if in-network

In reply to Jaehee Park

This patch adds a third knob, '2', which extends the accept_untracked_na option to learn a neighbor only if the src ip is in the same subnet as an address configured on the interface that received the neighbor advertisement. This is similar to the arp_accept configuration for ipv4.

---
 Documentation/networking/ip-sysctl.rst | 51 +++++++++++++++-----------
 net/ipv6/addrconf.c                    |  2 +-
 net/ipv6/ndisc.c                       | 29 ++++++++++++---
 3 files changed, 55 insertions(+), 27 deletions(-)
Documentation/networking/ip-sysctl.rst +30 -21
index 5c017fc1e24d..722ec4f491db 100644
@@ -2483,27 +2483,36 @@ drop_unsolicited_na - BOOLEAN
 
 	By default this is turned off.
 
-accept_untracked_na - BOOLEAN
-	Add a new neighbour cache entry in STALE state for routers on receiving a
-	neighbour advertisement (either solicited or unsolicited) with target
-	link-layer address option specified if no neighbour entry is already
-	present for the advertised IPv6 address. Without this knob, NAs received
-	for untracked addresses (absent in neighbour cache) are silently ignored.
-
-	This is as per router-side behaviour documented in RFC9131.
-
-	This has lower precedence than drop_unsolicited_na.
-
-	This will optimize the return path for the initial off-link communication
-	that is initiated by a directly connected host, by ensuring that
-	the first-hop router which turns on this setting doesn't have to
-	buffer the initial return packets to do neighbour-solicitation.
-	The prerequisite is that the host is configured to send
-	unsolicited neighbour advertisements on interface bringup.
-	This setting should be used in conjunction with the ndisc_notify setting
-	on the host to satisfy this prerequisite.
-
-	By default this is turned off.
+accept_untracked_na - INTEGER
+	Define behavior for accepting neighbor advertisements from devices that
+	are absent in the neighbor cache:
+
+	- 0 - (default) Do not accept unsolicited and untracked neighbor
+	  advertisements.
+
+	- 1 - Add a new neighbor cache entry in STALE state for routers on
+	  receiving a neighbor advertisement (either solicited or unsolicited)
+	  with target link-layer address option specified if no neighbor entry
+	  is already present for the advertised IPv6 address. Without this knob,
+	  NAs received for untracked addresses (absent in neighbor cache) are
+	  silently ignored.
+
+	  This is as per router-side behavior documented in RFC9131.
+
+	  This has lower precedence than drop_unsolicited_na.
+
+	  This will optimize the return path for the initial off-link
+	  communication that is initiated by a directly connected host, by
+	  ensuring that the first-hop router which turns on this setting doesn't
+	  have to buffer the initial return packets to do neighbor-solicitation.
+	  The prerequisite is that the host is configured to send unsolicited
+	  neighbor advertisements on interface bringup. This setting should be
+	  used in conjunction with the ndisc_notify setting on the host to
+	  satisfy this prerequisite.
+
+	- 2 - Extend option (1) to add a new neighbor cache entry only if the
+	  source IP address is in the same subnet as an address configured on
+	  the interface that received the neighbor advertisement.
 
 enhanced_dad - BOOLEAN
 	Include a nonce option in the IPv6 neighbor solicitation messages used for
net/ipv6/addrconf.c +1 -1
index 88becb037eb6..6ed807b6c647 100644
@@ -7042,7 +7042,7 @@ static const struct ctl_table addrconf_sysctl[] = {
 		.data		= &ipv6_devconf.accept_untracked_na,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= proc_dointvec,
 		.extra1		= (void *)SYSCTL_ZERO,
 		.extra2		= (void *)SYSCTL_ONE,
 	},
net/ipv6/ndisc.c +24 -5
index cd84cbdac0a2..98453693e400 100644
@@ -967,6 +967,25 @@ static void ndisc_recv_ns(struct sk_buff *skb)
 		in6_dev_put(idev);
 }
 
+static int accept_untracked_na(struct net_device *dev, struct in6_addr *saddr)
+{
+	struct inet6_dev *idev = __in6_dev_get(dev);
+
+	switch (idev->cnf.accept_untracked_na) {
+	case 0: /* Don't accept untracked na (absent in neighbor cache) */
+		return 0;
+	case 1: /* Create new entries from na if currently untracked */
+		return 1;
+	case 2: /* Create new entries from untracked na only if saddr is in the
+		 * same subnet as an address configured on the interface that
+		 * received the na
+		 */
+		return !!ipv6_chk_prefix(saddr, dev);
+	default:
+		return 0;
+	}
+}
+
 static void ndisc_recv_na(struct sk_buff *skb)
 {
 	struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
@@ -1061,11 +1080,11 @@ static void ndisc_recv_na(struct sk_buff *skb)
 	 * Note that we don't do a (daddr == all-routers-mcast) check.
 	 */
 	new_state = msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE;
-	if (!neigh && lladdr &&
-	    idev && idev->cnf.forwarding &&
-	    idev->cnf.accept_untracked_na) {
-		neigh = neigh_create(&nd_tbl, &msg->target, dev);
-		new_state = NUD_STALE;
+	if (!neigh && lladdr && idev && idev->cnf.forwarding) {
+		if (accept_untracked_na(dev, saddr)) {
+			neigh = neigh_create(&nd_tbl, &msg->target, dev);
+			new_state = NUD_STALE;
+		}
 	}
 
 	if (neigh && !IS_ERR(neigh)) {
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH v3 net-next 2/3] net: ipv6: new accept_untracked_na option to accept na only if in-network

Le 14/07/2022 à 01:40, Jaehee Park a écrit :

Quoted 81 lines

This patch adds a third knob, '2', which extends the accept_untracked_na option to learn a neighbor only if the src ip is in the same subnet as an address configured on the interface that received the neighbor advertisement. This is similar to the arp_accept configuration for ipv4.

---
 Documentation/networking/ip-sysctl.rst | 51 +++++++++++++++-----------
 net/ipv6/addrconf.c                    |  2 +-
 net/ipv6/ndisc.c                       | 29 ++++++++++++---
 3 files changed, 55 insertions(+), 27 deletions(-)
Documentation/networking/ip-sysctl.rst +30 -21
index 5c017fc1e24d..722ec4f491db 100644
@@ -2483,27 +2483,36 @@ drop_unsolicited_na - BOOLEAN
 
 	By default this is turned off.
 
-accept_untracked_na - BOOLEAN
-	Add a new neighbour cache entry in STALE state for routers on receiving a
-	neighbour advertisement (either solicited or unsolicited) with target
-	link-layer address option specified if no neighbour entry is already
-	present for the advertised IPv6 address. Without this knob, NAs received
-	for untracked addresses (absent in neighbour cache) are silently ignored.
-
-	This is as per router-side behaviour documented in RFC9131.
-
-	This has lower precedence than drop_unsolicited_na.
-
-	This will optimize the return path for the initial off-link communication
-	that is initiated by a directly connected host, by ensuring that
-	the first-hop router which turns on this setting doesn't have to
-	buffer the initial return packets to do neighbour-solicitation.
-	The prerequisite is that the host is configured to send
-	unsolicited neighbour advertisements on interface bringup.
-	This setting should be used in conjunction with the ndisc_notify setting
-	on the host to satisfy this prerequisite.
-
-	By default this is turned off.
+accept_untracked_na - INTEGER
+	Define behavior for accepting neighbor advertisements from devices that
+	are absent in the neighbor cache:
+
+	- 0 - (default) Do not accept unsolicited and untracked neighbor
+	  advertisements.
+
+	- 1 - Add a new neighbor cache entry in STALE state for routers on
+	  receiving a neighbor advertisement (either solicited or unsolicited)
+	  with target link-layer address option specified if no neighbor entry
+	  is already present for the advertised IPv6 address. Without this knob,
+	  NAs received for untracked addresses (absent in neighbor cache) are
+	  silently ignored.
+
+	  This is as per router-side behavior documented in RFC9131.
+
+	  This has lower precedence than drop_unsolicited_na.
+
+	  This will optimize the return path for the initial off-link
+	  communication that is initiated by a directly connected host, by
+	  ensuring that the first-hop router which turns on this setting doesn't
+	  have to buffer the initial return packets to do neighbor-solicitation.
+	  The prerequisite is that the host is configured to send unsolicited
+	  neighbor advertisements on interface bringup. This setting should be
+	  used in conjunction with the ndisc_notify setting on the host to
+	  satisfy this prerequisite.
+
+	- 2 - Extend option (1) to add a new neighbor cache entry only if the
+	  source IP address is in the same subnet as an address configured on
+	  the interface that received the neighbor advertisement.
 
 enhanced_dad - BOOLEAN
 	Include a nonce option in the IPv6 neighbor solicitation messages used for
net/ipv6/addrconf.c +0 -0
index 88becb037eb6..6ed807b6c647 100644

net/ipv6/addrconf.c static const struct ctl_table addrconf_sysctl[] = { · line 7047

Quoted 7 lines
net/ipv6/addrconf.c +1 -1
@@ -7042,7 +7042,7 @@ static const struct ctl_table addrconf_sysctl[] = {
 		.data		= &ipv6_devconf.accept_untracked_na,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= proc_dointvec,
 		.extra1		= (void *)SYSCTL_ZERO,
 		.extra2		= (void *)SYSCTL_ONE,

Maybe keeping proc_dointvec_minmax with SYSCTL_TWO for extra2 is better to avoid accepting all values. It enables to add another value later.

Regards,
Nicolas

author
#4 lore raw

Re: [PATCH v3 net-next 2/3] net: ipv6: new accept_untracked_na option to accept na only if in-network

Quoted 77 lines · Nicolas Dichtel
Quoted 74 lines · Jaehee Park

This patch adds a third knob, '2', which extends the accept_untracked_na option to learn a neighbor only if the src ip is in the same subnet as an address configured on the interface that received the neighbor advertisement. This is similar to the arp_accept configuration for ipv4.

---
 Documentation/networking/ip-sysctl.rst | 51 +++++++++++++++-----------
 net/ipv6/addrconf.c                    |  2 +-
 net/ipv6/ndisc.c                       | 29 ++++++++++++---
 3 files changed, 55 insertions(+), 27 deletions(-)
Documentation/networking/ip-sysctl.rst +30 -21
index 5c017fc1e24d..722ec4f491db 100644
@@ -2483,27 +2483,36 @@ drop_unsolicited_na - BOOLEAN
 
      By default this is turned off.
 
-accept_untracked_na - BOOLEAN
-     Add a new neighbour cache entry in STALE state for routers on receiving a
-     neighbour advertisement (either solicited or unsolicited) with target
-     link-layer address option specified if no neighbour entry is already
-     present for the advertised IPv6 address. Without this knob, NAs received
-     for untracked addresses (absent in neighbour cache) are silently ignored.
-
-     This is as per router-side behaviour documented in RFC9131.
-
-     This has lower precedence than drop_unsolicited_na.
-
-     This will optimize the return path for the initial off-link communication
-     that is initiated by a directly connected host, by ensuring that
-     the first-hop router which turns on this setting doesn't have to
-     buffer the initial return packets to do neighbour-solicitation.
-     The prerequisite is that the host is configured to send
-     unsolicited neighbour advertisements on interface bringup.
-     This setting should be used in conjunction with the ndisc_notify setting
-     on the host to satisfy this prerequisite.
-
-     By default this is turned off.
+accept_untracked_na - INTEGER
+     Define behavior for accepting neighbor advertisements from devices that
+     are absent in the neighbor cache:
+
+     - 0 - (default) Do not accept unsolicited and untracked neighbor
+       advertisements.
+
+     - 1 - Add a new neighbor cache entry in STALE state for routers on
+       receiving a neighbor advertisement (either solicited or unsolicited)
+       with target link-layer address option specified if no neighbor entry
+       is already present for the advertised IPv6 address. Without this knob,
+       NAs received for untracked addresses (absent in neighbor cache) are
+       silently ignored.
+
+       This is as per router-side behavior documented in RFC9131.
+
+       This has lower precedence than drop_unsolicited_na.
+
+       This will optimize the return path for the initial off-link
+       communication that is initiated by a directly connected host, by
+       ensuring that the first-hop router which turns on this setting doesn't
+       have to buffer the initial return packets to do neighbor-solicitation.
+       The prerequisite is that the host is configured to send unsolicited
+       neighbor advertisements on interface bringup. This setting should be
+       used in conjunction with the ndisc_notify setting on the host to
+       satisfy this prerequisite.
+
+     - 2 - Extend option (1) to add a new neighbor cache entry only if the
+       source IP address is in the same subnet as an address configured on
+       the interface that received the neighbor advertisement.
 
Quoted 17 lines
Quoted 7 lines
Documentation/networking/ip-sysctl.rst +0 -0
 enhanced_dad - BOOLEAN
      Include a nonce option in the IPv6 neighbor solicitation messages used for
net/ipv6/addrconf.c +0 -0
index 88becb037eb6..6ed807b6c647 100644

net/ipv6/addrconf.c static const struct ctl_table addrconf_sysctl[] = { · line 7047

Quoted 7 lines
net/ipv6/addrconf.c +1 -1
@@ -7042,7 +7042,7 @@ static const struct ctl_table addrconf_sysctl[] = {
              .data           = &ipv6_devconf.accept_untracked_na,
              .maxlen         = sizeof(int),
              .mode           = 0644,
-             .proc_handler   = proc_dointvec_minmax,
+             .proc_handler   = proc_dointvec,
              .extra1         = (void *)SYSCTL_ZERO,
              .extra2         = (void *)SYSCTL_ONE,

Maybe keeping proc_dointvec_minmax with SYSCTL_TWO for extra2 is better to avoid accepting all values. It enables to add another value later.

Hi Nicolas, your suggested fix seems to work well in helping us avoid accepting values >2. When I applied this change, and try to set accept_untracked_na=3, it gives me a warning: "invalid argument" which is the desired behavior, thanks! There might be a similar way for arp; i'm looking for this now.

Quoted 3 lines

Regards,
Nicolas

#5 lore raw

PATCH v3 3/3 selftests: net: arp_ndisc_untracked_subnets: test for arp_accept and accept_untracked_na

In reply to Jaehee Park

ipv4 arp_accept has a new option '2' to create new neighbor entries only if the src ip is in the same subnet as an address configured on the interface that received the garp message. This selftest tests all options in arp_accept.

ipv6 has a sysctl endpoint, accept_untracked_na, that defines the behavior for accepting untracked neighbor advertisements. A new option similar to that of arp_accept for learning only from the same subnet is added to accept_untracked_na. This selftest tests this new feature.

---
 tools/testing/selftests/net/Makefile          |   1 +
 .../net/arp_ndisc_untracked_subnets.sh        | 308 ++++++++++++++++++
 2 files changed, 309 insertions(+)
 create mode 100755 tools/testing/selftests/net/arp_ndisc_untracked_subnets.sh
tools/testing/selftests/net/Makefile +1 -0
index ddad703ace34..9c2e9e303c37 100644
@@ -38,6 +38,7 @@ TEST_PROGS += srv6_end_dt6_l3vpn_test.sh
 TEST_PROGS += vrf_strict_mode_test.sh
 TEST_PROGS += arp_ndisc_evict_nocarrier.sh
 TEST_PROGS += ndisc_unsolicited_na_test.sh
+TEST_PROGS += arp_ndisc_untracked_subnets.sh
 TEST_PROGS += stress_reuseport_listen.sh
 TEST_PROGS_EXTENDED := in_netns.sh setup_loopback.sh setup_veth.sh
 TEST_PROGS_EXTENDED += toeplitz_client.sh toeplitz.sh
tools/testing/selftests/net/arp_ndisc_untracked_subnets.sh new file +308 -0

308 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw
In reply to Jaehee Park
This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 5 lines · On Wed, 13 Jul 2022 16:40:46 -0700 you wrote:

The first patch adds an option to learn a neighbor from garp only if the source ip is in the same subnet as an address configured on the interface that received the garp message. The option has been added to arp_accept in ipv4.

Quoted 7 lines

The same feature has been added to ndisc (patch 2). For ipv6, the subnet filtering knob is an extension of the accept_untracked_na option introduced in these patches:
https://lore.kernel.org/all/[email protected]/t/
https://lore.kernel.org/netdev/[email protected]/T/

[...]

Here is the summary with links:
- [v3,net-next,1/3] net: ipv4: new arp_accept option to accept garp only if in-network

    https://git.kernel.org/netdev/net-next/c/e68c5dcf0aac
  - [v3,net-next,2/3] net: ipv6: new accept_untracked_na option to accept na only if in-network
    https://git.kernel.org/netdev/net-next/c/aaa5f515b16b
  - [v3,net-next,3/3] selftests: net: arp_ndisc_untracked_subnets: test for arp_accept and accept_untracked_na
    https://git.kernel.org/netdev/net-next/c/0ea7b0a454ca

You are awesome, thank you!

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