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