Skip to contentSkip to conversation

batman-adv: Snoop DHCPACKs for DAT

PATCH Superseded Linus Lüssing 2 participants 6 messages May 15, 2018 – May 21, 2018 Batman advanced

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 batman-adv: Snoop DHCPACKs for DAT

In a 1000 nodes mesh network (Freifunk Hamburg) we can still see 30KBit/s of ARP traffic (equalling about 25% of all layer two specific overhead, remaining after some filtering) flooded through the mesh. These 30KBit/s are mainly ARP Requests from the gateways / DHCP servers.

By snooping DHCPACKs we can learn about MAC/IP address pairs in the DHCP range without relying on ARP. This patch is in preparation to eliminate the need for mesh wide message flooding for IPv4 address resolution.

Also this allows to quickly update a MAC/IP pair at least in the DHT when DHCP reassigns an IP address to a new host.

---

Changes in v4:
* Removed @proto from kerneldoc
* Less enum values in batadv_packet.h
* Moved skb_set_network_header() call
* Updated commit message
* Removed noflood mark patch as a result of the discussions from the Battlemesh: An idea which should work transparently to reduce the ARP overhead (together with DHCP snooping) with no need for packet marking via ebtables or tc was suggested.

Changes in v3:
* Rebase to master
* Shortened commit message

Changes in v2:
* Rebase to master
* Fix compilation with CONFIG_BATMAN_ADV=n (added stubs)

Changes in RFC -> non-RFC

* Added kerneldoc
* Added Signed-off-by
* More IP Header checks
(iph->hlen considered, ip version checked, ...)
* Parsing & checking DHCP Message Type Option,
only snooping DHCPACKs now
* Moved ethernet protocol check from batadv_dat_check_dhcp to batadv_dat_check_dhcp_ipudp
* Removed buffer-length parameter from batadv_dat_dhcp_get_{yiaddr,chaddr}()
* Renamed batadv_dat_put() to batadv_dat_put_pairs()

fixup DAT DHCP snooping

---
 include/uapi/linux/batadv_packet.h     |  40 +++++
 net/batman-adv/distributed-arp-table.c | 295 +++++++++++++++++++++++++++++++++
 net/batman-adv/distributed-arp-table.h |  11 ++
 net/batman-adv/soft-interface.c        |  11 +-
 4 files changed, 355 insertions(+), 2 deletions(-)
include/uapi/linux/batadv_packet.h +40 -0
index 894d8d2f..6a48f36c 100644
@@ -630,6 +630,46 @@ struct batadv_tvlv_mcast_data {
 	__u8 reserved[3];
 };
 
+enum batadv_bootpop {
+	BATADV_BOOTREPLY	= 2,
+};
+
+enum batadv_boothtype {
+	BATADV_HTYPE_ETHERNET	= 1,
+};
+
+enum batadv_dhcpoptioncode {
+	BATADV_DHCP_OPT_PAD		= 0,
+	BATADV_DHCP_OPT_MSG_TYPE	= 53,
+	BATADV_DHCP_OPT_END		= 255,
+};
+
+enum batadv_dhcptype {
+	BATADV_DHCPACK		= 5,
+};
+
+/* { 99, 130, 83, 99 } */
+#define BATADV_DHCP_MAGIC 1669485411
+
+struct batadv_dhcp_packet {
+	__u8 op;
+	__u8 htype;
+	__u8 hlen;
+	__u8 hops;
+	__be32 xid;
+	__be16 secs;
+	__be16 flags;
+	__be32 ciaddr;
+	__be32 yiaddr;
+	__be32 siaddr;
+	__be32 giaddr;
+	__u8 chaddr[16];
+	__u8 sname[64];
+	__u8 file[128];
+	__be32 magic;
+	__u8 options[0];
+};
+
 #pragma pack()
 
 #endif /* _UAPI_LINUX_BATADV_PACKET_H_ */
net/batman-adv/distributed-arp-table.c +295 -0

316 lines not on this page · raw message

net/batman-adv/distributed-arp-table.h +11 -0
index a0459602..cf58d230 100644
@@ -46,6 +46,10 @@ void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
 					 struct sk_buff *skb);
 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
 					 struct sk_buff *skb, int hdr_size);
+void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
+					struct sk_buff *skb,
+					__be16 proto,
+					unsigned short vid);
 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
 				      struct batadv_forw_packet *forw_packet);
 
@@ -140,6 +144,13 @@ batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
 	return false;
 }
 
+static inline void
+batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
+				   struct sk_buff *skb, __be16 proto,
+				   unsigned short vid)
+{
+}
+
 static inline bool
 batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
 				 struct batadv_forw_packet *forw_packet)
net/batman-adv/soft-interface.c +9 -2
index 1485263a..a8559629 100644
@@ -212,6 +212,7 @@ static netdev_tx_t batadv_interface_tx(struct sk_buff *skb,
 	enum batadv_forw_mode forw_mode;
 	struct batadv_orig_node *mcast_single_orig = NULL;
 	int network_offset = ETH_HLEN;
+	__be16 proto;
 
 	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
 		goto dropped;
@@ -223,12 +224,15 @@ static netdev_tx_t batadv_interface_tx(struct sk_buff *skb,
 	vid = batadv_get_vid(skb, 0);
 	ethhdr = eth_hdr(skb);
 
-	switch (ntohs(ethhdr->h_proto)) {
+	proto = ethhdr->h_proto;
+
+	switch (ntohs(proto)) {
 	case ETH_P_8021Q:
 		vhdr = vlan_eth_hdr(skb);
+		proto = vhdr->h_vlan_encapsulated_proto;
 
 		/* drop batman-in-batman packets to prevent loops */
-		if (vhdr->h_vlan_encapsulated_proto != htons(ETH_P_BATMAN)) {
+		if (proto != htons(ETH_P_BATMAN)) {
 			network_offset += VLAN_HLEN;
 			break;
 		}
@@ -256,6 +260,9 @@ static netdev_tx_t batadv_interface_tx(struct sk_buff *skb,
 			goto dropped;
 	}
 
+	/* Snoop address candidates from DHCPACKs for early DAT filling */
+	batadv_dat_snoop_outgoing_dhcp_ack(bat_priv, skb, proto, vid);
+
 	/* don't accept stp packets. STP does not help in meshes.
 	 * better use the bridge loop avoidance ...
 	 *
Signature · 1 line
2.11.0
maintainer
#1 lore raw

On Dienstag, 15. Mai 2018 17:59:08 CEST Linus Lüssing wrote:
[...]

include/uapi/linux/

Quoted 1 line
include/uapi/linux/ renamed from include/uapi/linux/batadv_packet.h +0 -0

batadv_packet.h

Quoted 40 lines

index 894d8d2f..6a48f36c 100644

include/uapi/linux/batadv_packet.h +33 -0
@@ -630,6 +630,46 @@ struct batadv_tvlv_mcast_data {
 	__u8 reserved[3];
 };
 
+enum batadv_bootpop {
+	BATADV_BOOTREPLY	= 2,
+};
+
+enum batadv_boothtype {
+	BATADV_HTYPE_ETHERNET	= 1,
+};
+
+enum batadv_dhcpoptioncode {
+	BATADV_DHCP_OPT_PAD		= 0,
+	BATADV_DHCP_OPT_MSG_TYPE	= 53,
+	BATADV_DHCP_OPT_END		= 255,
+};
+
+enum batadv_dhcptype {
+	BATADV_DHCPACK		= 5,
+};
+
+/* { 99, 130, 83, 99 } */
+#define BATADV_DHCP_MAGIC 1669485411
+
+struct batadv_dhcp_packet {
+	__u8 op;
+	__u8 htype;
+	__u8 hlen;
+	__u8 hops;
+	__be32 xid;
+	__be16 secs;
+	__be16 flags;
+	__be32 ciaddr;
+	__be32 yiaddr;
+	__be32 siaddr;
+	__be32 giaddr;

include/uapi/linux/batadv_packet.h struct batadv_tvlv_mcast_data { · line 675

Quoted 10 lines
include/uapi/linux/batadv_packet.h +7 -0
+	__u8 chaddr[16];
+	__u8 sname[64];
+	__u8 file[128];
+	__be32 magic;
+	__u8 options[0];
+};
+
 #pragma pack()
 
 #endif /* _UAPI_LINUX_BATADV_PACKET_H_ */

Please correct me but it doesn't look like these things have anything to do with the batman-adv packet format. And userspace should also not use the bootp_pkt/batadv_dhcp_packet definition from batman-adv.

And what about the basically duplicated bootp_pkt definition in net/ipv4/ ipconfig.c?

Kind regards,
Sven

#2 lore raw
Quoted 3 lines · Sven Eckelmann

Please correct me but it doesn't look like these things have anything to do with the batman-adv packet format. And userspace should also not use the bootp_pkt/batadv_dhcp_packet definition from batman-adv.

Good point. I could move those to distributed-arp-table.c to avoid userspace from using it.

Quoted 2 lines

And what about the basically duplicated bootp_pkt definition in net/ipv4/ ipconfig.c?

Would it make sense to first add it to distributed-arp-table.c and then later I'd make a second patch directly for netdev@ to unify those?

Alternatively, if I'd get a partial ACK for the DHCP snooping approach in general here, I could do it the other way round, of course.

Regards, Linus

maintainer
#3 lore raw

On Montag, 21. Mai 2018 12:49:23 CEST Linus Lüssing wrote:
[...]

Quoted 5 lines
Quoted 2 lines

And what about the basically duplicated bootp_pkt definition in net/ipv4/ ipconfig.c?

Would it make sense to first add it to distributed-arp-table.c and then later I'd make a second patch directly for netdev@ to unify those?

I think this could work.

Quoted 4 lines · On Montag, 21. Mai 2018 13:04:40 CEST Linus Lüssing wrote:

Hm, I guess there's nothing I could do about that unaligned access other than suppressing any potential warnings? I guess since DHCP ACKs are relatively rare compared to other traffic in fast path I think the performance impact should be negligible.

No, it triggers the unaligned acccess exception and some architectures will not even be able to work around them [1].

Quoted 2 lines

What was the magic command to check unaligned accesses during runtime again?

I think the debugfs infrastructure can be used to debug this [2]

echo 2 >/sys/kernel/debug/mips/unaligned_action

neoraider used vxlan between outer ethernet header and batman-adv to "misalign" such fields by two bytes.

Quoted 2 lines

With the preceding "#pragma pack(2)" any warnings should be suppressed already, shouldn't they?

No, you cast these fields to __be32 * and then access them. The pointer will therefore be accessed with the assumption that it is aligned correctly (4 bytes boundary for MIPS). This will cause an exception on those machines.

Let me show you some examples. Let us first start with an direct access of a struct member:

    $MIPS_GCC/mips-openwrt-linux-musl-gcc -O1 -x c -S - -o - << EOF
    #include <stdint.h>
    struct test {
            uint32_t test1;
            uint32_t test2;
    };
    
    uint32_t test(struct test *t)
    {
            return t->test2;
    }
    EOF

You will get a simple function with a single (32 bit) load which also requires the 4 byte alignment:

    test:
            .frame  $sp,0,$31
            .mask   0x00000000,0
            .fmask  0x00000000,0
            .set    noreorder
            .set    nomacro
            lw      $2,4($4)
            j       $31
            nop

Now let us add the pragma which to reduce it to a 2 byte alignment requirement:

    $MIPS_GCC/mips-openwrt-linux-musl-gcc -O1 -x c -S - -o - << EOF
    #include <stdint.h>
    #pragma pack(2)
    struct test {
            uint32_t test1;
            uint32_t test2;
    };
    #pragma pack()
    
    uint32_t test(struct test *t)
    {
            return t->test2;
    }
    EOF

This will create two different loads to get the complete 32 bit word when only 2 byte alignment is guaranteed

    test:
            .frame  $sp,0,$31
            .mask   0x00000000,0
            .fmask  0x00000000,0
            .set    noreorder
            .set    nomacro
            lwl     $2,4($4)
            nop
            lwr     $2,7($4)
            j       $31
            nop

And now we convert all this to your method of indirect access (uint32_t pointer):

    $MIPS_GCC/mips-openwrt-linux-musl-gcc -O1 -x c -S - -o - << EOF
    #include <stdint.h>
    #pragma pack(2)
    struct test {
            uint32_t test1;
            uint32_t test2;
    };
    #pragma pack()
    
    uint32_t test(struct test *t)
    {
            uint32_t *t2 = &t->test2;
            return *t2;
    }
    EOF

You will see that this will again only be a single 32 bit load which requires 4 byte alignment (and thus will cause an CPU exception when it is only 2 byte aligned):

    test:
            .frame  $sp,0,$31
            .mask   0x00000000,0
            .fmask  0x00000000,0
            .set    noreorder
            .set    nomacro
            lw      $2,4($4)
            j       $31
            nop

Kind regards,
Sven

[1] https://www.kernel.org/doc/Documentation/unaligned-memory-access.txt
[2] https://www.linux-mips.org/wiki/Alignment#Transparent_fixing_by_the_kernel

maintainer
#4 lore raw
In reply to Linus Lüssing

net/batman-adv/distributed-arp-table.c bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv, · line 1526

Quoted 3 lines · On Dienstag, 15. Mai 2018 17:59:08 CEST Linus Lüssing wrote:
net/batman-adv/distributed-arp-table.c +3 -0
+       magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
+       if (!magic || *magic != htonl(BATADV_DHCP_MAGIC))
+               return -EINVAL;

I just hear a neoraider cry [1] in the background because you've added a new potentially unaligned 4 byte access in the network packet parsing code.

[...]

net/batman-adv/distributed-arp-table.c bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv, · line 1600

Quoted 9 lines
net/batman-adv/distributed-arp-table.c +9 -0
+static __be32 *batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buffer)
+{
+       unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
+       unsigned int len = sizeof(((struct batadv_dhcp_packet *)0)->yiaddr);
+
+       offset += offsetof(struct batadv_dhcp_packet, yiaddr);
+
+       return skb_header_pointer(skb, offset, len, buffer);
+}

Same here

Kind regards,
Sven

[1] https://patchwork.open-mesh.org/patch/17253/

#5 lore raw
Quoted 10 lines · Sven Eckelmann

net/batman-adv/distributed-arp-table.c bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv, · line 1526

Quoted 3 lines · On Dienstag, 15. Mai 2018 17:59:08 CEST Linus Lüssing wrote:
net/batman-adv/distributed-arp-table.c +3 -0
+       magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
+       if (!magic || *magic != htonl(BATADV_DHCP_MAGIC))
+               return -EINVAL;

I just hear a neoraider cry [1] in the background because you've added a new potentially unaligned 4 byte access in the network packet parsing code.

[...]

Quoted 11 lines

net/batman-adv/distributed-arp-table.c bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv, · line 1600

Quoted 9 lines
net/batman-adv/distributed-arp-table.c +9 -0
+static __be32 *batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buffer)
+{
+       unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
+       unsigned int len = sizeof(((struct batadv_dhcp_packet *)0)->yiaddr);
+
+       offset += offsetof(struct batadv_dhcp_packet, yiaddr);
+
+       return skb_header_pointer(skb, offset, len, buffer);
+}

Same here

Hm, I guess there's nothing I could do about that unaligned access other than suppressing any potential warnings? I guess since DHCP ACKs are relatively rare compared to other traffic in fast path I think the performance impact should be negligible.

What was the magic command to check unaligned accesses during runtime again?

With the preceding "#pragma pack(2)" any warnings should be suppressed already, shouldn't they?

Regards, Linus