Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/net/bridge/br_arp_nd_proxy.c
26278 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* Handle bridge arp/nd proxy/suppress
4
*
5
* Copyright (C) 2017 Cumulus Networks
6
* Copyright (c) 2017 Roopa Prabhu <[email protected]>
7
*
8
* Authors:
9
* Roopa Prabhu <[email protected]>
10
*/
11
12
#include <linux/kernel.h>
13
#include <linux/netdevice.h>
14
#include <linux/etherdevice.h>
15
#include <linux/neighbour.h>
16
#include <net/arp.h>
17
#include <linux/if_vlan.h>
18
#include <linux/inetdevice.h>
19
#include <net/addrconf.h>
20
#include <net/ipv6_stubs.h>
21
#if IS_ENABLED(CONFIG_IPV6)
22
#include <net/ip6_checksum.h>
23
#endif
24
25
#include "br_private.h"
26
27
void br_recalculate_neigh_suppress_enabled(struct net_bridge *br)
28
{
29
struct net_bridge_port *p;
30
bool neigh_suppress = false;
31
32
list_for_each_entry(p, &br->port_list, list) {
33
if (p->flags & (BR_NEIGH_SUPPRESS | BR_NEIGH_VLAN_SUPPRESS)) {
34
neigh_suppress = true;
35
break;
36
}
37
}
38
39
br_opt_toggle(br, BROPT_NEIGH_SUPPRESS_ENABLED, neigh_suppress);
40
}
41
42
#if IS_ENABLED(CONFIG_INET)
43
static void br_arp_send(struct net_bridge *br, struct net_bridge_port *p,
44
struct net_device *dev, __be32 dest_ip, __be32 src_ip,
45
const unsigned char *dest_hw,
46
const unsigned char *src_hw,
47
const unsigned char *target_hw,
48
__be16 vlan_proto, u16 vlan_tci)
49
{
50
struct net_bridge_vlan_group *vg;
51
struct sk_buff *skb;
52
u16 pvid;
53
54
netdev_dbg(dev, "arp send dev %s dst %pI4 dst_hw %pM src %pI4 src_hw %pM\n",
55
dev->name, &dest_ip, dest_hw, &src_ip, src_hw);
56
57
if (!vlan_tci) {
58
arp_send(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
59
dest_hw, src_hw, target_hw);
60
return;
61
}
62
63
skb = arp_create(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
64
dest_hw, src_hw, target_hw);
65
if (!skb)
66
return;
67
68
if (p)
69
vg = nbp_vlan_group_rcu(p);
70
else
71
vg = br_vlan_group_rcu(br);
72
pvid = br_get_pvid(vg);
73
if (pvid == (vlan_tci & VLAN_VID_MASK))
74
vlan_tci = 0;
75
76
if (vlan_tci)
77
__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
78
79
if (p) {
80
arp_xmit(skb);
81
} else {
82
skb_reset_mac_header(skb);
83
__skb_pull(skb, skb_network_offset(skb));
84
skb->ip_summed = CHECKSUM_UNNECESSARY;
85
skb->pkt_type = PACKET_HOST;
86
87
netif_rx(skb);
88
}
89
}
90
91
static int br_chk_addr_ip(struct net_device *dev,
92
struct netdev_nested_priv *priv)
93
{
94
__be32 ip = *(__be32 *)priv->data;
95
struct in_device *in_dev;
96
__be32 addr = 0;
97
98
in_dev = __in_dev_get_rcu(dev);
99
if (in_dev)
100
addr = inet_confirm_addr(dev_net(dev), in_dev, 0, ip,
101
RT_SCOPE_HOST);
102
103
if (addr == ip)
104
return 1;
105
106
return 0;
107
}
108
109
static bool br_is_local_ip(struct net_device *dev, __be32 ip)
110
{
111
struct netdev_nested_priv priv = {
112
.data = (void *)&ip,
113
};
114
115
if (br_chk_addr_ip(dev, &priv))
116
return true;
117
118
/* check if ip is configured on upper dev */
119
if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip, &priv))
120
return true;
121
122
return false;
123
}
124
125
void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
126
u16 vid, struct net_bridge_port *p)
127
{
128
struct net_device *dev = br->dev;
129
struct net_device *vlandev = dev;
130
struct neighbour *n;
131
struct arphdr *parp;
132
u8 *arpptr, *sha;
133
__be32 sip, tip;
134
135
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
136
137
if ((dev->flags & IFF_NOARP) ||
138
!pskb_may_pull(skb, arp_hdr_len(dev)))
139
return;
140
141
parp = arp_hdr(skb);
142
143
if (parp->ar_pro != htons(ETH_P_IP) ||
144
parp->ar_hln != dev->addr_len ||
145
parp->ar_pln != 4)
146
return;
147
148
arpptr = (u8 *)parp + sizeof(struct arphdr);
149
sha = arpptr;
150
arpptr += dev->addr_len; /* sha */
151
memcpy(&sip, arpptr, sizeof(sip));
152
arpptr += sizeof(sip);
153
arpptr += dev->addr_len; /* tha */
154
memcpy(&tip, arpptr, sizeof(tip));
155
156
if (ipv4_is_loopback(tip) ||
157
ipv4_is_multicast(tip))
158
return;
159
160
if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
161
if (br_is_neigh_suppress_enabled(p, vid))
162
return;
163
if (is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
164
parp->ar_op == htons(ARPOP_REQUEST))
165
return;
166
if (parp->ar_op != htons(ARPOP_RREQUEST) &&
167
parp->ar_op != htons(ARPOP_RREPLY) &&
168
(ipv4_is_zeronet(sip) || sip == tip)) {
169
/* prevent flooding to neigh suppress ports */
170
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
171
return;
172
}
173
}
174
175
if (parp->ar_op != htons(ARPOP_REQUEST))
176
return;
177
178
if (vid != 0) {
179
vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
180
vid);
181
if (!vlandev)
182
return;
183
}
184
185
if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
186
br_is_local_ip(vlandev, tip)) {
187
/* its our local ip, so don't proxy reply
188
* and don't forward to neigh suppress ports
189
*/
190
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
191
return;
192
}
193
194
n = neigh_lookup(&arp_tbl, &tip, vlandev);
195
if (n) {
196
struct net_bridge_fdb_entry *f;
197
198
if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
199
neigh_release(n);
200
return;
201
}
202
203
f = br_fdb_find_rcu(br, n->ha, vid);
204
if (f) {
205
bool replied = false;
206
207
if ((p && (p->flags & BR_PROXYARP)) ||
208
(f->dst && (f->dst->flags & BR_PROXYARP_WIFI)) ||
209
br_is_neigh_suppress_enabled(f->dst, vid)) {
210
if (!vid)
211
br_arp_send(br, p, skb->dev, sip, tip,
212
sha, n->ha, sha, 0, 0);
213
else
214
br_arp_send(br, p, skb->dev, sip, tip,
215
sha, n->ha, sha,
216
skb->vlan_proto,
217
skb_vlan_tag_get(skb));
218
replied = true;
219
}
220
221
/* If we have replied or as long as we know the
222
* mac, indicate to arp replied
223
*/
224
if (replied ||
225
br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
226
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
227
}
228
229
neigh_release(n);
230
}
231
}
232
#endif
233
234
#if IS_ENABLED(CONFIG_IPV6)
235
struct nd_msg *br_is_nd_neigh_msg(const struct sk_buff *skb, struct nd_msg *msg)
236
{
237
struct nd_msg *m;
238
239
m = skb_header_pointer(skb, skb_network_offset(skb) +
240
sizeof(struct ipv6hdr), sizeof(*msg), msg);
241
if (!m)
242
return NULL;
243
244
if (m->icmph.icmp6_code != 0 ||
245
(m->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
246
m->icmph.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
247
return NULL;
248
249
return m;
250
}
251
252
static void br_nd_send(struct net_bridge *br, struct net_bridge_port *p,
253
struct sk_buff *request, struct neighbour *n,
254
__be16 vlan_proto, u16 vlan_tci, struct nd_msg *ns)
255
{
256
struct net_device *dev = request->dev;
257
struct net_bridge_vlan_group *vg;
258
struct sk_buff *reply;
259
struct nd_msg *na;
260
struct ipv6hdr *pip6;
261
int na_olen = 8; /* opt hdr + ETH_ALEN for target */
262
int ns_olen;
263
int i, len;
264
u8 *daddr;
265
u16 pvid;
266
267
if (!dev)
268
return;
269
270
len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
271
sizeof(*na) + na_olen + dev->needed_tailroom;
272
273
reply = alloc_skb(len, GFP_ATOMIC);
274
if (!reply)
275
return;
276
277
reply->protocol = htons(ETH_P_IPV6);
278
reply->dev = dev;
279
skb_reserve(reply, LL_RESERVED_SPACE(dev));
280
skb_push(reply, sizeof(struct ethhdr));
281
skb_set_mac_header(reply, 0);
282
283
daddr = eth_hdr(request)->h_source;
284
285
/* Do we need option processing ? */
286
ns_olen = request->len - (skb_network_offset(request) +
287
sizeof(struct ipv6hdr)) - sizeof(*ns);
288
for (i = 0; i < ns_olen - 1; i += (ns->opt[i + 1] << 3)) {
289
if (!ns->opt[i + 1]) {
290
kfree_skb(reply);
291
return;
292
}
293
if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
294
daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
295
break;
296
}
297
}
298
299
/* Ethernet header */
300
ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
301
ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
302
eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
303
reply->protocol = htons(ETH_P_IPV6);
304
305
skb_pull(reply, sizeof(struct ethhdr));
306
skb_set_network_header(reply, 0);
307
skb_put(reply, sizeof(struct ipv6hdr));
308
309
/* IPv6 header */
310
pip6 = ipv6_hdr(reply);
311
memset(pip6, 0, sizeof(struct ipv6hdr));
312
pip6->version = 6;
313
pip6->priority = ipv6_hdr(request)->priority;
314
pip6->nexthdr = IPPROTO_ICMPV6;
315
pip6->hop_limit = 255;
316
pip6->daddr = ipv6_hdr(request)->saddr;
317
pip6->saddr = *(struct in6_addr *)n->primary_key;
318
319
skb_pull(reply, sizeof(struct ipv6hdr));
320
skb_set_transport_header(reply, 0);
321
322
na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
323
324
/* Neighbor Advertisement */
325
memset(na, 0, sizeof(*na) + na_olen);
326
na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
327
na->icmph.icmp6_router = (n->flags & NTF_ROUTER) ? 1 : 0;
328
na->icmph.icmp6_override = 1;
329
na->icmph.icmp6_solicited = 1;
330
na->target = ns->target;
331
ether_addr_copy(&na->opt[2], n->ha);
332
na->opt[0] = ND_OPT_TARGET_LL_ADDR;
333
na->opt[1] = na_olen >> 3;
334
335
na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
336
&pip6->daddr,
337
sizeof(*na) + na_olen,
338
IPPROTO_ICMPV6,
339
csum_partial(na, sizeof(*na) + na_olen, 0));
340
341
pip6->payload_len = htons(sizeof(*na) + na_olen);
342
343
skb_push(reply, sizeof(struct ipv6hdr));
344
skb_push(reply, sizeof(struct ethhdr));
345
346
reply->ip_summed = CHECKSUM_UNNECESSARY;
347
348
if (p)
349
vg = nbp_vlan_group_rcu(p);
350
else
351
vg = br_vlan_group_rcu(br);
352
pvid = br_get_pvid(vg);
353
if (pvid == (vlan_tci & VLAN_VID_MASK))
354
vlan_tci = 0;
355
356
if (vlan_tci)
357
__vlan_hwaccel_put_tag(reply, vlan_proto, vlan_tci);
358
359
netdev_dbg(dev, "nd send dev %s dst %pI6 dst_hw %pM src %pI6 src_hw %pM\n",
360
dev->name, &pip6->daddr, daddr, &pip6->saddr, n->ha);
361
362
if (p) {
363
dev_queue_xmit(reply);
364
} else {
365
skb_reset_mac_header(reply);
366
__skb_pull(reply, skb_network_offset(reply));
367
reply->ip_summed = CHECKSUM_UNNECESSARY;
368
reply->pkt_type = PACKET_HOST;
369
370
netif_rx(reply);
371
}
372
}
373
374
static int br_chk_addr_ip6(struct net_device *dev,
375
struct netdev_nested_priv *priv)
376
{
377
struct in6_addr *addr = (struct in6_addr *)priv->data;
378
379
if (ipv6_chk_addr(dev_net(dev), addr, dev, 0))
380
return 1;
381
382
return 0;
383
}
384
385
static bool br_is_local_ip6(struct net_device *dev, struct in6_addr *addr)
386
387
{
388
struct netdev_nested_priv priv = {
389
.data = (void *)addr,
390
};
391
392
if (br_chk_addr_ip6(dev, &priv))
393
return true;
394
395
/* check if ip is configured on upper dev */
396
if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip6, &priv))
397
return true;
398
399
return false;
400
}
401
402
void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
403
u16 vid, struct net_bridge_port *p, struct nd_msg *msg)
404
{
405
struct net_device *dev = br->dev;
406
struct net_device *vlandev = NULL;
407
struct in6_addr *saddr, *daddr;
408
struct ipv6hdr *iphdr;
409
struct neighbour *n;
410
411
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
412
413
if (br_is_neigh_suppress_enabled(p, vid))
414
return;
415
416
if (is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
417
msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
418
return;
419
420
if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
421
!msg->icmph.icmp6_solicited) {
422
/* prevent flooding to neigh suppress ports */
423
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
424
return;
425
}
426
427
if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
428
return;
429
430
iphdr = ipv6_hdr(skb);
431
saddr = &iphdr->saddr;
432
daddr = &iphdr->daddr;
433
434
if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) {
435
/* prevent flooding to neigh suppress ports */
436
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
437
return;
438
}
439
440
if (vid != 0) {
441
/* build neigh table lookup on the vlan device */
442
vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
443
vid);
444
if (!vlandev)
445
return;
446
} else {
447
vlandev = dev;
448
}
449
450
if (br_is_local_ip6(vlandev, &msg->target)) {
451
/* its our own ip, so don't proxy reply
452
* and don't forward to arp suppress ports
453
*/
454
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
455
return;
456
}
457
458
n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, vlandev);
459
if (n) {
460
struct net_bridge_fdb_entry *f;
461
462
if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
463
neigh_release(n);
464
return;
465
}
466
467
f = br_fdb_find_rcu(br, n->ha, vid);
468
if (f) {
469
bool replied = false;
470
471
if (br_is_neigh_suppress_enabled(f->dst, vid)) {
472
if (vid != 0)
473
br_nd_send(br, p, skb, n,
474
skb->vlan_proto,
475
skb_vlan_tag_get(skb), msg);
476
else
477
br_nd_send(br, p, skb, n, 0, 0, msg);
478
replied = true;
479
}
480
481
/* If we have replied or as long as we know the
482
* mac, indicate to NEIGH_SUPPRESS ports that we
483
* have replied
484
*/
485
if (replied ||
486
br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
487
BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
488
}
489
neigh_release(n);
490
}
491
}
492
#endif
493
494
bool br_is_neigh_suppress_enabled(const struct net_bridge_port *p, u16 vid)
495
{
496
if (!p)
497
return false;
498
499
if (!vid)
500
return !!(p->flags & BR_NEIGH_SUPPRESS);
501
502
if (p->flags & BR_NEIGH_VLAN_SUPPRESS) {
503
struct net_bridge_vlan_group *vg = nbp_vlan_group_rcu(p);
504
struct net_bridge_vlan *v;
505
506
v = br_vlan_find(vg, vid);
507
if (!v)
508
return false;
509
return !!(v->priv_flags & BR_VLFLAG_NEIGH_SUPPRESS_ENABLED);
510
} else {
511
return !!(p->flags & BR_NEIGH_SUPPRESS);
512
}
513
}
514
515