Skip to content

Commit ed842fa

Browse files
roopa-prabhudavem330
authored andcommitted
bridge: suppress nd pkts on BR_NEIGH_SUPPRESS ports
This patch avoids flooding and proxies ndisc packets for BR_NEIGH_SUPPRESS ports. Signed-off-by: Roopa Prabhu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 057658c commit ed842fa

File tree

4 files changed

+274
-0
lines changed

4 files changed

+274
-0
lines changed

net/bridge/br_arp_nd_proxy.c

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <linux/if_vlan.h>
2222
#include <linux/inetdevice.h>
2323
#include <net/addrconf.h>
24+
#if IS_ENABLED(CONFIG_IPV6)
25+
#include <net/ip6_checksum.h>
26+
#endif
2427

2528
#include "br_private.h"
2629

@@ -218,3 +221,249 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
218221
}
219222
}
220223
#endif
224+
225+
#if IS_ENABLED(CONFIG_IPV6)
226+
struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *msg)
227+
{
228+
struct nd_msg *m;
229+
230+
m = skb_header_pointer(skb, skb_network_offset(skb) +
231+
sizeof(struct ipv6hdr), sizeof(*msg), msg);
232+
if (!m)
233+
return NULL;
234+
235+
if (m->icmph.icmp6_code != 0 ||
236+
(m->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
237+
m->icmph.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
238+
return NULL;
239+
240+
return m;
241+
}
242+
243+
static void br_nd_send(struct net_bridge *br, struct net_bridge_port *p,
244+
struct sk_buff *request, struct neighbour *n,
245+
__be16 vlan_proto, u16 vlan_tci, struct nd_msg *ns)
246+
{
247+
struct net_device *dev = request->dev;
248+
struct net_bridge_vlan_group *vg;
249+
struct sk_buff *reply;
250+
struct nd_msg *na;
251+
struct ipv6hdr *pip6;
252+
int na_olen = 8; /* opt hdr + ETH_ALEN for target */
253+
int ns_olen;
254+
int i, len;
255+
u8 *daddr;
256+
u16 pvid;
257+
258+
if (!dev)
259+
return;
260+
261+
len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
262+
sizeof(*na) + na_olen + dev->needed_tailroom;
263+
264+
reply = alloc_skb(len, GFP_ATOMIC);
265+
if (!reply)
266+
return;
267+
268+
reply->protocol = htons(ETH_P_IPV6);
269+
reply->dev = dev;
270+
skb_reserve(reply, LL_RESERVED_SPACE(dev));
271+
skb_push(reply, sizeof(struct ethhdr));
272+
skb_set_mac_header(reply, 0);
273+
274+
daddr = eth_hdr(request)->h_source;
275+
276+
/* Do we need option processing ? */
277+
ns_olen = request->len - (skb_network_offset(request) +
278+
sizeof(struct ipv6hdr)) - sizeof(*ns);
279+
for (i = 0; i < ns_olen - 1; i += (ns->opt[i + 1] << 3)) {
280+
if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
281+
daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
282+
break;
283+
}
284+
}
285+
286+
/* Ethernet header */
287+
ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
288+
ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
289+
eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
290+
reply->protocol = htons(ETH_P_IPV6);
291+
292+
skb_pull(reply, sizeof(struct ethhdr));
293+
skb_set_network_header(reply, 0);
294+
skb_put(reply, sizeof(struct ipv6hdr));
295+
296+
/* IPv6 header */
297+
pip6 = ipv6_hdr(reply);
298+
memset(pip6, 0, sizeof(struct ipv6hdr));
299+
pip6->version = 6;
300+
pip6->priority = ipv6_hdr(request)->priority;
301+
pip6->nexthdr = IPPROTO_ICMPV6;
302+
pip6->hop_limit = 255;
303+
pip6->daddr = ipv6_hdr(request)->saddr;
304+
pip6->saddr = *(struct in6_addr *)n->primary_key;
305+
306+
skb_pull(reply, sizeof(struct ipv6hdr));
307+
skb_set_transport_header(reply, 0);
308+
309+
na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
310+
311+
/* Neighbor Advertisement */
312+
memset(na, 0, sizeof(*na) + na_olen);
313+
na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
314+
na->icmph.icmp6_router = 0; /* XXX: should be 1 ? */
315+
na->icmph.icmp6_override = 1;
316+
na->icmph.icmp6_solicited = 1;
317+
na->target = ns->target;
318+
ether_addr_copy(&na->opt[2], n->ha);
319+
na->opt[0] = ND_OPT_TARGET_LL_ADDR;
320+
na->opt[1] = na_olen >> 3;
321+
322+
na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
323+
&pip6->daddr,
324+
sizeof(*na) + na_olen,
325+
IPPROTO_ICMPV6,
326+
csum_partial(na, sizeof(*na) + na_olen, 0));
327+
328+
pip6->payload_len = htons(sizeof(*na) + na_olen);
329+
330+
skb_push(reply, sizeof(struct ipv6hdr));
331+
skb_push(reply, sizeof(struct ethhdr));
332+
333+
reply->ip_summed = CHECKSUM_UNNECESSARY;
334+
335+
if (p)
336+
vg = nbp_vlan_group_rcu(p);
337+
else
338+
vg = br_vlan_group_rcu(br);
339+
pvid = br_get_pvid(vg);
340+
if (pvid == (vlan_tci & VLAN_VID_MASK))
341+
vlan_tci = 0;
342+
343+
if (vlan_tci)
344+
__vlan_hwaccel_put_tag(reply, vlan_proto, vlan_tci);
345+
346+
netdev_dbg(dev, "nd send dev %s dst %pI6 dst_hw %pM src %pI6 src_hw %pM\n",
347+
dev->name, &pip6->daddr, daddr, &pip6->saddr, n->ha);
348+
349+
if (p) {
350+
dev_queue_xmit(reply);
351+
} else {
352+
skb_reset_mac_header(reply);
353+
__skb_pull(reply, skb_network_offset(reply));
354+
reply->ip_summed = CHECKSUM_UNNECESSARY;
355+
reply->pkt_type = PACKET_HOST;
356+
357+
netif_rx_ni(reply);
358+
}
359+
}
360+
361+
static int br_chk_addr_ip6(struct net_device *dev, void *data)
362+
{
363+
struct in6_addr *addr = (struct in6_addr *)data;
364+
365+
if (ipv6_chk_addr(dev_net(dev), addr, dev, 0))
366+
return 1;
367+
368+
return 0;
369+
}
370+
371+
static bool br_is_local_ip6(struct net_device *dev, struct in6_addr *addr)
372+
373+
{
374+
if (br_chk_addr_ip6(dev, addr))
375+
return true;
376+
377+
/* check if ip is configured on upper dev */
378+
if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip6, addr))
379+
return true;
380+
381+
return false;
382+
}
383+
384+
void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
385+
u16 vid, struct net_bridge_port *p, struct nd_msg *msg)
386+
{
387+
struct net_device *dev = br->dev;
388+
struct net_device *vlandev = NULL;
389+
struct in6_addr *saddr, *daddr;
390+
struct ipv6hdr *iphdr;
391+
struct neighbour *n;
392+
393+
BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
394+
395+
if (p && (p->flags & BR_NEIGH_SUPPRESS))
396+
return;
397+
398+
if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
399+
!msg->icmph.icmp6_solicited) {
400+
/* prevent flooding to neigh suppress ports */
401+
BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
402+
return;
403+
}
404+
405+
if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
406+
return;
407+
408+
iphdr = ipv6_hdr(skb);
409+
saddr = &iphdr->saddr;
410+
daddr = &iphdr->daddr;
411+
412+
if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) {
413+
/* prevent flooding to neigh suppress ports */
414+
BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
415+
return;
416+
}
417+
418+
if (vid != 0) {
419+
/* build neigh table lookup on the vlan device */
420+
vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
421+
vid);
422+
if (!vlandev)
423+
return;
424+
} else {
425+
vlandev = dev;
426+
}
427+
428+
if (br_is_local_ip6(vlandev, &msg->target)) {
429+
/* its our own ip, so don't proxy reply
430+
* and don't forward to arp suppress ports
431+
*/
432+
BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
433+
return;
434+
}
435+
436+
n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, vlandev);
437+
if (n) {
438+
struct net_bridge_fdb_entry *f;
439+
440+
if (!(n->nud_state & NUD_VALID)) {
441+
neigh_release(n);
442+
return;
443+
}
444+
445+
f = br_fdb_find_rcu(br, n->ha, vid);
446+
if (f) {
447+
bool replied = false;
448+
449+
if (f->dst && (f->dst->flags & BR_NEIGH_SUPPRESS)) {
450+
if (vid != 0)
451+
br_nd_send(br, p, skb, n,
452+
skb->vlan_proto,
453+
skb_vlan_tag_get(skb), msg);
454+
else
455+
br_nd_send(br, p, skb, n, 0, 0, msg);
456+
replied = true;
457+
}
458+
459+
/* If we have replied or as long as we know the
460+
* mac, indicate to NEIGH_SUPPRESS ports that we
461+
* have replied
462+
*/
463+
if (replied || br->neigh_suppress_enabled)
464+
BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
465+
}
466+
neigh_release(n);
467+
}
468+
}
469+
#endif

net/bridge/br_device.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
6969
eth->h_proto == htons(ETH_P_RARP)) &&
7070
br->neigh_suppress_enabled) {
7171
br_do_proxy_suppress_arp(skb, br, vid, NULL);
72+
} else if (IS_ENABLED(CONFIG_IPV6) &&
73+
skb->protocol == htons(ETH_P_IPV6) &&
74+
br->neigh_suppress_enabled &&
75+
pskb_may_pull(skb, sizeof(struct ipv6hdr) +
76+
sizeof(struct nd_msg)) &&
77+
ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
78+
struct nd_msg *msg, _msg;
79+
80+
msg = br_is_nd_neigh_msg(skb, &_msg);
81+
if (msg)
82+
br_do_suppress_nd(skb, br, vid, NULL, msg);
7283
}
7384

7485
dest = eth_hdr(skb)->h_dest;

net/bridge/br_input.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb
119119
(skb->protocol == htons(ETH_P_ARP) ||
120120
skb->protocol == htons(ETH_P_RARP))) {
121121
br_do_proxy_suppress_arp(skb, br, vid, p);
122+
} else if (IS_ENABLED(CONFIG_IPV6) &&
123+
skb->protocol == htons(ETH_P_IPV6) &&
124+
br->neigh_suppress_enabled &&
125+
pskb_may_pull(skb, sizeof(struct ipv6hdr) +
126+
sizeof(struct nd_msg)) &&
127+
ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
128+
struct nd_msg *msg, _msg;
129+
130+
msg = br_is_nd_neigh_msg(skb, &_msg);
131+
if (msg)
132+
br_do_suppress_nd(skb, br, vid, p, msg);
122133
}
123134

124135
switch (pkt_type) {

net/bridge/br_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,4 +1144,7 @@ static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
11441144
void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
11451145
void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
11461146
u16 vid, struct net_bridge_port *p);
1147+
void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
1148+
u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
1149+
struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);
11471150
#endif

0 commit comments

Comments
 (0)