]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/bridge/br_netfilter_hooks.c
netfilter: bridge: prevent UAF in brnf_exit_net()
[mirror_ubuntu-bionic-kernel.git] / net / bridge / br_netfilter_hooks.c
1 /*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer <bdschuym@pandora.be>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Lennert dedicates this file to Kerstin Wurdinger.
15 */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/ip.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <linux/if_pppox.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/netfilter_bridge.h>
29 #include <linux/netfilter_ipv4.h>
30 #include <linux/netfilter_ipv6.h>
31 #include <linux/netfilter_arp.h>
32 #include <linux/in_route.h>
33 #include <linux/rculist.h>
34 #include <linux/inetdevice.h>
35
36 #include <net/ip.h>
37 #include <net/ipv6.h>
38 #include <net/addrconf.h>
39 #include <net/route.h>
40 #include <net/netfilter/br_netfilter.h>
41 #include <net/netns/generic.h>
42
43 #include <linux/uaccess.h>
44 #include "br_private.h"
45 #ifdef CONFIG_SYSCTL
46 #include <linux/sysctl.h>
47 #endif
48
49 static unsigned int brnf_net_id __read_mostly;
50
51 struct brnf_net {
52 bool enabled;
53
54 #ifdef CONFIG_SYSCTL
55 struct ctl_table_header *ctl_hdr;
56 #endif
57
58 /* default value is 1 */
59 int call_iptables;
60 int call_ip6tables;
61 int call_arptables;
62
63 /* default value is 0 */
64 int filter_vlan_tagged;
65 int filter_pppoe_tagged;
66 int pass_vlan_indev;
67 };
68
69 #define IS_IP(skb) \
70 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
71
72 #define IS_IPV6(skb) \
73 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
74
75 #define IS_ARP(skb) \
76 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
77
78 static inline __be16 vlan_proto(const struct sk_buff *skb)
79 {
80 if (skb_vlan_tag_present(skb))
81 return skb->protocol;
82 else if (skb->protocol == htons(ETH_P_8021Q))
83 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
84 else
85 return 0;
86 }
87
88 static inline bool is_vlan_ip(const struct sk_buff *skb, const struct net *net)
89 {
90 struct brnf_net *brnet = net_generic(net, brnf_net_id);
91
92 return vlan_proto(skb) == htons(ETH_P_IP) && brnet->filter_vlan_tagged;
93 }
94
95 static inline bool is_vlan_ipv6(const struct sk_buff *skb,
96 const struct net *net)
97 {
98 struct brnf_net *brnet = net_generic(net, brnf_net_id);
99
100 return vlan_proto(skb) == htons(ETH_P_IPV6) &&
101 brnet->filter_vlan_tagged;
102 }
103
104 static inline bool is_vlan_arp(const struct sk_buff *skb, const struct net *net)
105 {
106 struct brnf_net *brnet = net_generic(net, brnf_net_id);
107
108 return vlan_proto(skb) == htons(ETH_P_ARP) && brnet->filter_vlan_tagged;
109 }
110
111 static inline __be16 pppoe_proto(const struct sk_buff *skb)
112 {
113 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
114 sizeof(struct pppoe_hdr)));
115 }
116
117 static inline bool is_pppoe_ip(const struct sk_buff *skb, const struct net *net)
118 {
119 struct brnf_net *brnet = net_generic(net, brnf_net_id);
120
121 return skb->protocol == htons(ETH_P_PPP_SES) &&
122 pppoe_proto(skb) == htons(PPP_IP) && brnet->filter_pppoe_tagged;
123 }
124
125 static inline bool is_pppoe_ipv6(const struct sk_buff *skb,
126 const struct net *net)
127 {
128 struct brnf_net *brnet = net_generic(net, brnf_net_id);
129
130 return skb->protocol == htons(ETH_P_PPP_SES) &&
131 pppoe_proto(skb) == htons(PPP_IPV6) &&
132 brnet->filter_pppoe_tagged;
133 }
134
135 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
136 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
137
138 struct brnf_frag_data {
139 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
140 u8 encap_size;
141 u8 size;
142 u16 vlan_tci;
143 __be16 vlan_proto;
144 };
145
146 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
147
148 static void nf_bridge_info_free(struct sk_buff *skb)
149 {
150 if (skb->nf_bridge) {
151 nf_bridge_put(skb->nf_bridge);
152 skb->nf_bridge = NULL;
153 }
154 }
155
156 static inline struct net_device *bridge_parent(const struct net_device *dev)
157 {
158 struct net_bridge_port *port;
159
160 port = br_port_get_rcu(dev);
161 return port ? port->br->dev : NULL;
162 }
163
164 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
165 {
166 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
167
168 if (refcount_read(&nf_bridge->use) > 1) {
169 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
170
171 if (tmp) {
172 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
173 refcount_set(&tmp->use, 1);
174 }
175 nf_bridge_put(nf_bridge);
176 nf_bridge = tmp;
177 }
178 return nf_bridge;
179 }
180
181 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
182 {
183 switch (skb->protocol) {
184 case __cpu_to_be16(ETH_P_8021Q):
185 return VLAN_HLEN;
186 case __cpu_to_be16(ETH_P_PPP_SES):
187 return PPPOE_SES_HLEN;
188 default:
189 return 0;
190 }
191 }
192
193 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
194 {
195 unsigned int len = nf_bridge_encap_header_len(skb);
196
197 skb_pull(skb, len);
198 skb->network_header += len;
199 }
200
201 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
202 {
203 unsigned int len = nf_bridge_encap_header_len(skb);
204
205 skb_pull_rcsum(skb, len);
206 skb->network_header += len;
207 }
208
209 /* When handing a packet over to the IP layer
210 * check whether we have a skb that is in the
211 * expected format
212 */
213
214 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
215 {
216 const struct iphdr *iph;
217 u32 len;
218
219 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
220 goto inhdr_error;
221
222 iph = ip_hdr(skb);
223
224 /* Basic sanity checks */
225 if (iph->ihl < 5 || iph->version != 4)
226 goto inhdr_error;
227
228 if (!pskb_may_pull(skb, iph->ihl*4))
229 goto inhdr_error;
230
231 iph = ip_hdr(skb);
232 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
233 goto inhdr_error;
234
235 len = ntohs(iph->tot_len);
236 if (skb->len < len) {
237 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
238 goto drop;
239 } else if (len < (iph->ihl*4))
240 goto inhdr_error;
241
242 if (pskb_trim_rcsum(skb, len)) {
243 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
244 goto drop;
245 }
246
247 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
248 /* We should really parse IP options here but until
249 * somebody who actually uses IP options complains to
250 * us we'll just silently ignore the options because
251 * we're lazy!
252 */
253 return 0;
254
255 inhdr_error:
256 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
257 drop:
258 return -1;
259 }
260
261 void nf_bridge_update_protocol(struct sk_buff *skb)
262 {
263 switch (skb->nf_bridge->orig_proto) {
264 case BRNF_PROTO_8021Q:
265 skb->protocol = htons(ETH_P_8021Q);
266 break;
267 case BRNF_PROTO_PPPOE:
268 skb->protocol = htons(ETH_P_PPP_SES);
269 break;
270 case BRNF_PROTO_UNCHANGED:
271 break;
272 }
273 }
274
275 /* Obtain the correct destination MAC address, while preserving the original
276 * source MAC address. If we already know this address, we just copy it. If we
277 * don't, we use the neighbour framework to find out. In both cases, we make
278 * sure that br_handle_frame_finish() is called afterwards.
279 */
280 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
281 {
282 struct neighbour *neigh;
283 struct dst_entry *dst;
284
285 skb->dev = bridge_parent(skb->dev);
286 if (!skb->dev)
287 goto free_skb;
288 dst = skb_dst(skb);
289 neigh = dst_neigh_lookup_skb(dst, skb);
290 if (neigh) {
291 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
292 int ret;
293
294 if ((neigh->nud_state & NUD_CONNECTED) && neigh->hh.hh_len) {
295 neigh_hh_bridge(&neigh->hh, skb);
296 skb->dev = nf_bridge->physindev;
297 ret = br_handle_frame_finish(net, sk, skb);
298 } else {
299 /* the neighbour function below overwrites the complete
300 * MAC header, so we save the Ethernet source address and
301 * protocol number.
302 */
303 skb_copy_from_linear_data_offset(skb,
304 -(ETH_HLEN-ETH_ALEN),
305 nf_bridge->neigh_header,
306 ETH_HLEN-ETH_ALEN);
307 /* tell br_dev_xmit to continue with forwarding */
308 nf_bridge->bridged_dnat = 1;
309 /* FIXME Need to refragment */
310 ret = neigh->output(neigh, skb);
311 }
312 neigh_release(neigh);
313 return ret;
314 }
315 free_skb:
316 kfree_skb(skb);
317 return 0;
318 }
319
320 static inline bool
321 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
322 const struct nf_bridge_info *nf_bridge)
323 {
324 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
325 }
326
327 /* This requires some explaining. If DNAT has taken place,
328 * we will need to fix up the destination Ethernet address.
329 * This is also true when SNAT takes place (for the reply direction).
330 *
331 * There are two cases to consider:
332 * 1. The packet was DNAT'ed to a device in the same bridge
333 * port group as it was received on. We can still bridge
334 * the packet.
335 * 2. The packet was DNAT'ed to a different device, either
336 * a non-bridged device or another bridge port group.
337 * The packet will need to be routed.
338 *
339 * The correct way of distinguishing between these two cases is to
340 * call ip_route_input() and to look at skb->dst->dev, which is
341 * changed to the destination device if ip_route_input() succeeds.
342 *
343 * Let's first consider the case that ip_route_input() succeeds:
344 *
345 * If the output device equals the logical bridge device the packet
346 * came in on, we can consider this bridging. The corresponding MAC
347 * address will be obtained in br_nf_pre_routing_finish_bridge.
348 * Otherwise, the packet is considered to be routed and we just
349 * change the destination MAC address so that the packet will
350 * later be passed up to the IP stack to be routed. For a redirected
351 * packet, ip_route_input() will give back the localhost as output device,
352 * which differs from the bridge device.
353 *
354 * Let's now consider the case that ip_route_input() fails:
355 *
356 * This can be because the destination address is martian, in which case
357 * the packet will be dropped.
358 * If IP forwarding is disabled, ip_route_input() will fail, while
359 * ip_route_output_key() can return success. The source
360 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
361 * thinks we're handling a locally generated packet and won't care
362 * if IP forwarding is enabled. If the output device equals the logical bridge
363 * device, we proceed as if ip_route_input() succeeded. If it differs from the
364 * logical bridge port or if ip_route_output_key() fails we drop the packet.
365 */
366 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
367 {
368 struct net_device *dev = skb->dev;
369 struct iphdr *iph = ip_hdr(skb);
370 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
371 struct rtable *rt;
372 int err;
373
374 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
375
376 if (nf_bridge->pkt_otherhost) {
377 skb->pkt_type = PACKET_OTHERHOST;
378 nf_bridge->pkt_otherhost = false;
379 }
380 nf_bridge->in_prerouting = 0;
381 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
382 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
383 struct in_device *in_dev = __in_dev_get_rcu(dev);
384
385 /* If err equals -EHOSTUNREACH the error is due to a
386 * martian destination or due to the fact that
387 * forwarding is disabled. For most martian packets,
388 * ip_route_output_key() will fail. It won't fail for 2 types of
389 * martian destinations: loopback destinations and destination
390 * 0.0.0.0. In both cases the packet will be dropped because the
391 * destination is the loopback device and not the bridge. */
392 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
393 goto free_skb;
394
395 rt = ip_route_output(net, iph->daddr, 0,
396 RT_TOS(iph->tos), 0);
397 if (!IS_ERR(rt)) {
398 /* - Bridged-and-DNAT'ed traffic doesn't
399 * require ip_forwarding. */
400 if (rt->dst.dev == dev) {
401 skb_dst_set(skb, &rt->dst);
402 goto bridged_dnat;
403 }
404 ip_rt_put(rt);
405 }
406 free_skb:
407 kfree_skb(skb);
408 return 0;
409 } else {
410 if (skb_dst(skb)->dev == dev) {
411 bridged_dnat:
412 skb->dev = nf_bridge->physindev;
413 nf_bridge_update_protocol(skb);
414 nf_bridge_push_encap_header(skb);
415 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
416 net, sk, skb, skb->dev,
417 NULL,
418 br_nf_pre_routing_finish_bridge);
419 return 0;
420 }
421 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
422 skb->pkt_type = PACKET_HOST;
423 }
424 } else {
425 rt = bridge_parent_rtable(nf_bridge->physindev);
426 if (!rt) {
427 kfree_skb(skb);
428 return 0;
429 }
430 skb_dst_set_noref(skb, &rt->dst);
431 }
432
433 skb->dev = nf_bridge->physindev;
434 nf_bridge_update_protocol(skb);
435 nf_bridge_push_encap_header(skb);
436 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
437 br_handle_frame_finish);
438 return 0;
439 }
440
441 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb,
442 const struct net_device *dev,
443 const struct net *net)
444 {
445 struct net_device *vlan, *br;
446 struct brnf_net *brnet = net_generic(net, brnf_net_id);
447
448 br = bridge_parent(dev);
449
450 if (brnet->pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
451 return br;
452
453 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
454 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
455
456 return vlan ? vlan : br;
457 }
458
459 /* Some common code for IPv4/IPv6 */
460 struct net_device *setup_pre_routing(struct sk_buff *skb, const struct net *net)
461 {
462 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
463
464 if (skb->pkt_type == PACKET_OTHERHOST) {
465 skb->pkt_type = PACKET_HOST;
466 nf_bridge->pkt_otherhost = true;
467 }
468
469 nf_bridge->in_prerouting = 1;
470 nf_bridge->physindev = skb->dev;
471 skb->dev = brnf_get_logical_dev(skb, skb->dev, net);
472
473 if (skb->protocol == htons(ETH_P_8021Q))
474 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
475 else if (skb->protocol == htons(ETH_P_PPP_SES))
476 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
477
478 /* Must drop socket now because of tproxy. */
479 skb_orphan(skb);
480 return skb->dev;
481 }
482
483 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
484 * Replicate the checks that IPv4 does on packet reception.
485 * Set skb->dev to the bridge device (i.e. parent of the
486 * receiving device) to make netfilter happy, the REDIRECT
487 * target in particular. Save the original destination IP
488 * address to be able to detect DNAT afterwards. */
489 static unsigned int br_nf_pre_routing(void *priv,
490 struct sk_buff *skb,
491 const struct nf_hook_state *state)
492 {
493 struct nf_bridge_info *nf_bridge;
494 struct net_bridge_port *p;
495 struct net_bridge *br;
496 __u32 len = nf_bridge_encap_header_len(skb);
497 struct brnf_net *brnet;
498
499 if (unlikely(!pskb_may_pull(skb, len)))
500 return NF_DROP;
501
502 p = br_port_get_rcu(state->in);
503 if (p == NULL)
504 return NF_DROP;
505 br = p->br;
506
507 brnet = net_generic(state->net, brnf_net_id);
508 if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
509 is_pppoe_ipv6(skb, state->net)) {
510 if (!brnet->call_ip6tables &&
511 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
512 return NF_ACCEPT;
513
514 nf_bridge_pull_encap_header_rcsum(skb);
515 return br_nf_pre_routing_ipv6(priv, skb, state);
516 }
517
518 if (!brnet->call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
519 return NF_ACCEPT;
520
521 if (!IS_IP(skb) && !is_vlan_ip(skb, state->net) &&
522 !is_pppoe_ip(skb, state->net))
523 return NF_ACCEPT;
524
525 nf_bridge_pull_encap_header_rcsum(skb);
526
527 if (br_validate_ipv4(state->net, skb))
528 return NF_DROP;
529
530 nf_bridge_put(skb->nf_bridge);
531 if (!nf_bridge_alloc(skb))
532 return NF_DROP;
533 if (!setup_pre_routing(skb, state->net))
534 return NF_DROP;
535
536 nf_bridge = nf_bridge_info_get(skb);
537 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
538
539 skb->protocol = htons(ETH_P_IP);
540 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
541
542 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
543 skb->dev, NULL,
544 br_nf_pre_routing_finish);
545
546 return NF_STOLEN;
547 }
548
549
550 /* PF_BRIDGE/FORWARD *************************************************/
551 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
552 {
553 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
554 struct net_device *in;
555
556 if (!IS_ARP(skb) && !is_vlan_arp(skb, net)) {
557
558 if (skb->protocol == htons(ETH_P_IP))
559 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
560
561 if (skb->protocol == htons(ETH_P_IPV6))
562 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
563
564 in = nf_bridge->physindev;
565 if (nf_bridge->pkt_otherhost) {
566 skb->pkt_type = PACKET_OTHERHOST;
567 nf_bridge->pkt_otherhost = false;
568 }
569 nf_bridge_update_protocol(skb);
570 } else {
571 in = *((struct net_device **)(skb->cb));
572 }
573 nf_bridge_push_encap_header(skb);
574
575 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
576 br_forward_finish);
577 return 0;
578 }
579
580
581 /* This is the 'purely bridged' case. For IP, we pass the packet to
582 * netfilter with indev and outdev set to the bridge device,
583 * but we are still able to filter on the 'real' indev/outdev
584 * because of the physdev module. For ARP, indev and outdev are the
585 * bridge ports. */
586 static unsigned int br_nf_forward_ip(void *priv,
587 struct sk_buff *skb,
588 const struct nf_hook_state *state)
589 {
590 struct nf_bridge_info *nf_bridge;
591 struct net_device *parent;
592 u_int8_t pf;
593
594 if (!skb->nf_bridge)
595 return NF_ACCEPT;
596
597 /* Need exclusive nf_bridge_info since we might have multiple
598 * different physoutdevs. */
599 if (!nf_bridge_unshare(skb))
600 return NF_DROP;
601
602 nf_bridge = nf_bridge_info_get(skb);
603 if (!nf_bridge)
604 return NF_DROP;
605
606 parent = bridge_parent(state->out);
607 if (!parent)
608 return NF_DROP;
609
610 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
611 is_pppoe_ip(skb, state->net))
612 pf = NFPROTO_IPV4;
613 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
614 is_pppoe_ipv6(skb, state->net))
615 pf = NFPROTO_IPV6;
616 else
617 return NF_ACCEPT;
618
619 nf_bridge_pull_encap_header(skb);
620
621 if (skb->pkt_type == PACKET_OTHERHOST) {
622 skb->pkt_type = PACKET_HOST;
623 nf_bridge->pkt_otherhost = true;
624 }
625
626 if (pf == NFPROTO_IPV4) {
627 if (br_validate_ipv4(state->net, skb))
628 return NF_DROP;
629 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
630 }
631
632 if (pf == NFPROTO_IPV6) {
633 if (br_validate_ipv6(state->net, skb))
634 return NF_DROP;
635 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
636 }
637
638 nf_bridge->physoutdev = skb->dev;
639 if (pf == NFPROTO_IPV4)
640 skb->protocol = htons(ETH_P_IP);
641 else
642 skb->protocol = htons(ETH_P_IPV6);
643
644 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
645 brnf_get_logical_dev(skb, state->in, state->net),
646 parent, br_nf_forward_finish);
647
648 return NF_STOLEN;
649 }
650
651 static unsigned int br_nf_forward_arp(void *priv,
652 struct sk_buff *skb,
653 const struct nf_hook_state *state)
654 {
655 struct net_bridge_port *p;
656 struct net_bridge *br;
657 struct net_device **d = (struct net_device **)(skb->cb);
658 struct brnf_net *brnet;
659
660 p = br_port_get_rcu(state->out);
661 if (p == NULL)
662 return NF_ACCEPT;
663 br = p->br;
664
665 brnet = net_generic(state->net, brnf_net_id);
666 if (!brnet->call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
667 return NF_ACCEPT;
668
669 if (!IS_ARP(skb)) {
670 if (!is_vlan_arp(skb, state->net))
671 return NF_ACCEPT;
672 nf_bridge_pull_encap_header(skb);
673 }
674
675 if (arp_hdr(skb)->ar_pln != 4) {
676 if (is_vlan_arp(skb, state->net))
677 nf_bridge_push_encap_header(skb);
678 return NF_ACCEPT;
679 }
680 *d = state->in;
681 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
682 state->in, state->out, br_nf_forward_finish);
683
684 return NF_STOLEN;
685 }
686
687 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
688 {
689 struct brnf_frag_data *data;
690 int err;
691
692 data = this_cpu_ptr(&brnf_frag_data_storage);
693 err = skb_cow_head(skb, data->size);
694
695 if (err) {
696 kfree_skb(skb);
697 return 0;
698 }
699
700 if (data->vlan_tci) {
701 skb->vlan_tci = data->vlan_tci;
702 skb->vlan_proto = data->vlan_proto;
703 }
704
705 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
706 __skb_push(skb, data->encap_size);
707
708 nf_bridge_info_free(skb);
709 return br_dev_queue_push_xmit(net, sk, skb);
710 }
711
712 static int
713 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
714 int (*output)(struct net *, struct sock *, struct sk_buff *))
715 {
716 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
717 struct iphdr *iph = ip_hdr(skb);
718
719 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
720 (IPCB(skb)->frag_max_size &&
721 IPCB(skb)->frag_max_size > mtu))) {
722 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
723 kfree_skb(skb);
724 return -EMSGSIZE;
725 }
726
727 return ip_do_fragment(net, sk, skb, output);
728 }
729
730 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
731 {
732 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
733 return PPPOE_SES_HLEN;
734 return 0;
735 }
736
737 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
738 {
739 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
740 unsigned int mtu, mtu_reserved;
741
742 mtu_reserved = nf_bridge_mtu_reduction(skb);
743 mtu = skb->dev->mtu;
744
745 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
746 mtu = nf_bridge->frag_max_size;
747
748 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
749 nf_bridge_info_free(skb);
750 return br_dev_queue_push_xmit(net, sk, skb);
751 }
752
753 /* This is wrong! We should preserve the original fragment
754 * boundaries by preserving frag_list rather than refragmenting.
755 */
756 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
757 skb->protocol == htons(ETH_P_IP)) {
758 struct brnf_frag_data *data;
759
760 if (br_validate_ipv4(net, skb))
761 goto drop;
762
763 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
764
765 nf_bridge_update_protocol(skb);
766
767 data = this_cpu_ptr(&brnf_frag_data_storage);
768
769 data->vlan_tci = skb->vlan_tci;
770 data->vlan_proto = skb->vlan_proto;
771 data->encap_size = nf_bridge_encap_header_len(skb);
772 data->size = ETH_HLEN + data->encap_size;
773
774 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
775 data->size);
776
777 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
778 }
779 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
780 skb->protocol == htons(ETH_P_IPV6)) {
781 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
782 struct brnf_frag_data *data;
783
784 if (br_validate_ipv6(net, skb))
785 goto drop;
786
787 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
788
789 nf_bridge_update_protocol(skb);
790
791 data = this_cpu_ptr(&brnf_frag_data_storage);
792 data->encap_size = nf_bridge_encap_header_len(skb);
793 data->size = ETH_HLEN + data->encap_size;
794
795 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
796 data->size);
797
798 if (v6ops)
799 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
800
801 kfree_skb(skb);
802 return -EMSGSIZE;
803 }
804 nf_bridge_info_free(skb);
805 return br_dev_queue_push_xmit(net, sk, skb);
806 drop:
807 kfree_skb(skb);
808 return 0;
809 }
810
811 /* PF_BRIDGE/POST_ROUTING ********************************************/
812 static unsigned int br_nf_post_routing(void *priv,
813 struct sk_buff *skb,
814 const struct nf_hook_state *state)
815 {
816 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
817 struct net_device *realoutdev = bridge_parent(skb->dev);
818 u_int8_t pf;
819
820 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
821 * on a bridge, but was delivered locally and is now being routed:
822 *
823 * POST_ROUTING was already invoked from the ip stack.
824 */
825 if (!nf_bridge || !nf_bridge->physoutdev)
826 return NF_ACCEPT;
827
828 if (!realoutdev)
829 return NF_DROP;
830
831 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
832 is_pppoe_ip(skb, state->net))
833 pf = NFPROTO_IPV4;
834 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
835 is_pppoe_ipv6(skb, state->net))
836 pf = NFPROTO_IPV6;
837 else
838 return NF_ACCEPT;
839
840 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
841 * about the value of skb->pkt_type. */
842 if (skb->pkt_type == PACKET_OTHERHOST) {
843 skb->pkt_type = PACKET_HOST;
844 nf_bridge->pkt_otherhost = true;
845 }
846
847 nf_bridge_pull_encap_header(skb);
848 if (pf == NFPROTO_IPV4)
849 skb->protocol = htons(ETH_P_IP);
850 else
851 skb->protocol = htons(ETH_P_IPV6);
852
853 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
854 NULL, realoutdev,
855 br_nf_dev_queue_xmit);
856
857 return NF_STOLEN;
858 }
859
860 /* IP/SABOTAGE *****************************************************/
861 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
862 * for the second time. */
863 static unsigned int ip_sabotage_in(void *priv,
864 struct sk_buff *skb,
865 const struct nf_hook_state *state)
866 {
867 if (skb->nf_bridge && !skb->nf_bridge->in_prerouting &&
868 !netif_is_l3_master(skb->dev)) {
869 state->okfn(state->net, state->sk, skb);
870 return NF_STOLEN;
871 }
872
873 return NF_ACCEPT;
874 }
875
876 /* This is called when br_netfilter has called into iptables/netfilter,
877 * and DNAT has taken place on a bridge-forwarded packet.
878 *
879 * neigh->output has created a new MAC header, with local br0 MAC
880 * as saddr.
881 *
882 * This restores the original MAC saddr of the bridged packet
883 * before invoking bridge forward logic to transmit the packet.
884 */
885 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
886 {
887 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
888
889 skb_pull(skb, ETH_HLEN);
890 nf_bridge->bridged_dnat = 0;
891
892 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
893
894 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
895 nf_bridge->neigh_header,
896 ETH_HLEN - ETH_ALEN);
897 skb->dev = nf_bridge->physindev;
898
899 nf_bridge->physoutdev = NULL;
900 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
901 }
902
903 static int br_nf_dev_xmit(struct sk_buff *skb)
904 {
905 if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
906 br_nf_pre_routing_finish_bridge_slow(skb);
907 return 1;
908 }
909 return 0;
910 }
911
912 static const struct nf_br_ops br_ops = {
913 .br_dev_xmit_hook = br_nf_dev_xmit,
914 };
915
916 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
917 * br_dev_queue_push_xmit is called afterwards */
918 static const struct nf_hook_ops br_nf_ops[] = {
919 {
920 .hook = br_nf_pre_routing,
921 .pf = NFPROTO_BRIDGE,
922 .hooknum = NF_BR_PRE_ROUTING,
923 .priority = NF_BR_PRI_BRNF,
924 },
925 {
926 .hook = br_nf_forward_ip,
927 .pf = NFPROTO_BRIDGE,
928 .hooknum = NF_BR_FORWARD,
929 .priority = NF_BR_PRI_BRNF - 1,
930 },
931 {
932 .hook = br_nf_forward_arp,
933 .pf = NFPROTO_BRIDGE,
934 .hooknum = NF_BR_FORWARD,
935 .priority = NF_BR_PRI_BRNF,
936 },
937 {
938 .hook = br_nf_post_routing,
939 .pf = NFPROTO_BRIDGE,
940 .hooknum = NF_BR_POST_ROUTING,
941 .priority = NF_BR_PRI_LAST,
942 },
943 {
944 .hook = ip_sabotage_in,
945 .pf = NFPROTO_IPV4,
946 .hooknum = NF_INET_PRE_ROUTING,
947 .priority = NF_IP_PRI_FIRST,
948 },
949 {
950 .hook = ip_sabotage_in,
951 .pf = NFPROTO_IPV6,
952 .hooknum = NF_INET_PRE_ROUTING,
953 .priority = NF_IP6_PRI_FIRST,
954 },
955 };
956
957 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
958 void *ptr)
959 {
960 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
961 struct brnf_net *brnet;
962 struct net *net;
963 int ret;
964
965 if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
966 return NOTIFY_DONE;
967
968 ASSERT_RTNL();
969
970 net = dev_net(dev);
971 brnet = net_generic(net, brnf_net_id);
972 if (brnet->enabled)
973 return NOTIFY_OK;
974
975 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
976 if (ret)
977 return NOTIFY_BAD;
978
979 brnet->enabled = true;
980 return NOTIFY_OK;
981 }
982
983 static struct notifier_block brnf_notifier __read_mostly = {
984 .notifier_call = brnf_device_event,
985 };
986
987 /* recursively invokes nf_hook_slow (again), skipping already-called
988 * hooks (< NF_BR_PRI_BRNF).
989 *
990 * Called with rcu read lock held.
991 */
992 int br_nf_hook_thresh(unsigned int hook, struct net *net,
993 struct sock *sk, struct sk_buff *skb,
994 struct net_device *indev,
995 struct net_device *outdev,
996 int (*okfn)(struct net *, struct sock *,
997 struct sk_buff *))
998 {
999 const struct nf_hook_entries *e;
1000 struct nf_hook_state state;
1001 struct nf_hook_ops **ops;
1002 unsigned int i;
1003 int ret;
1004
1005 e = rcu_dereference(net->nf.hooks[NFPROTO_BRIDGE][hook]);
1006 if (!e)
1007 return okfn(net, sk, skb);
1008
1009 ops = nf_hook_entries_get_hook_ops(e);
1010 for (i = 0; i < e->num_hook_entries &&
1011 ops[i]->priority <= NF_BR_PRI_BRNF; i++)
1012 ;
1013
1014 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1015 sk, net, okfn);
1016
1017 ret = nf_hook_slow(skb, &state, e, i);
1018 if (ret == 1)
1019 ret = okfn(net, sk, skb);
1020
1021 return ret;
1022 }
1023
1024 #ifdef CONFIG_SYSCTL
1025 static
1026 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1027 void __user *buffer, size_t *lenp, loff_t *ppos)
1028 {
1029 int ret;
1030
1031 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1032
1033 if (write && *(int *)(ctl->data))
1034 *(int *)(ctl->data) = 1;
1035 return ret;
1036 }
1037
1038 static struct ctl_table brnf_table[] = {
1039 {
1040 .procname = "bridge-nf-call-arptables",
1041 .maxlen = sizeof(int),
1042 .mode = 0644,
1043 .proc_handler = brnf_sysctl_call_tables,
1044 },
1045 {
1046 .procname = "bridge-nf-call-iptables",
1047 .maxlen = sizeof(int),
1048 .mode = 0644,
1049 .proc_handler = brnf_sysctl_call_tables,
1050 },
1051 {
1052 .procname = "bridge-nf-call-ip6tables",
1053 .maxlen = sizeof(int),
1054 .mode = 0644,
1055 .proc_handler = brnf_sysctl_call_tables,
1056 },
1057 {
1058 .procname = "bridge-nf-filter-vlan-tagged",
1059 .maxlen = sizeof(int),
1060 .mode = 0644,
1061 .proc_handler = brnf_sysctl_call_tables,
1062 },
1063 {
1064 .procname = "bridge-nf-filter-pppoe-tagged",
1065 .maxlen = sizeof(int),
1066 .mode = 0644,
1067 .proc_handler = brnf_sysctl_call_tables,
1068 },
1069 {
1070 .procname = "bridge-nf-pass-vlan-input-dev",
1071 .maxlen = sizeof(int),
1072 .mode = 0644,
1073 .proc_handler = brnf_sysctl_call_tables,
1074 },
1075 { }
1076 };
1077
1078 static inline void br_netfilter_sysctl_default(struct brnf_net *brnf)
1079 {
1080 brnf->call_iptables = 1;
1081 brnf->call_ip6tables = 1;
1082 brnf->call_arptables = 1;
1083 brnf->filter_vlan_tagged = 0;
1084 brnf->filter_pppoe_tagged = 0;
1085 brnf->pass_vlan_indev = 0;
1086 }
1087
1088 static int br_netfilter_sysctl_init_net(struct net *net)
1089 {
1090 struct ctl_table *table = brnf_table;
1091 struct brnf_net *brnet;
1092
1093 if (!net_eq(net, &init_net)) {
1094 table = kmemdup(table, sizeof(brnf_table), GFP_KERNEL);
1095 if (!table)
1096 return -ENOMEM;
1097 }
1098
1099 brnet = net_generic(net, brnf_net_id);
1100 table[0].data = &brnet->call_arptables;
1101 table[1].data = &brnet->call_iptables;
1102 table[2].data = &brnet->call_ip6tables;
1103 table[3].data = &brnet->filter_vlan_tagged;
1104 table[4].data = &brnet->filter_pppoe_tagged;
1105 table[5].data = &brnet->pass_vlan_indev;
1106
1107 br_netfilter_sysctl_default(brnet);
1108
1109 brnet->ctl_hdr = register_net_sysctl(net, "net/bridge", table);
1110 if (!brnet->ctl_hdr) {
1111 if (!net_eq(net, &init_net))
1112 kfree(table);
1113
1114 return -ENOMEM;
1115 }
1116
1117 return 0;
1118 }
1119
1120 static void br_netfilter_sysctl_exit_net(struct net *net,
1121 struct brnf_net *brnet)
1122 {
1123 struct ctl_table *table = brnet->ctl_hdr->ctl_table_arg;
1124
1125 unregister_net_sysctl_table(brnet->ctl_hdr);
1126 if (!net_eq(net, &init_net))
1127 kfree(table);
1128 }
1129
1130 static int __net_init brnf_init_net(struct net *net)
1131 {
1132 return br_netfilter_sysctl_init_net(net);
1133 }
1134 #endif
1135
1136 static void __net_exit brnf_exit_net(struct net *net)
1137 {
1138 struct brnf_net *brnet;
1139
1140 brnet = net_generic(net, brnf_net_id);
1141 if (brnet->enabled) {
1142 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1143 brnet->enabled = false;
1144 }
1145
1146 #ifdef CONFIG_SYSCTL
1147 br_netfilter_sysctl_exit_net(net, brnet);
1148 #endif
1149 }
1150
1151 static struct pernet_operations brnf_net_ops __read_mostly = {
1152 #ifdef CONFIG_SYSCTL
1153 .init = brnf_init_net,
1154 #endif
1155 .exit = brnf_exit_net,
1156 .id = &brnf_net_id,
1157 .size = sizeof(struct brnf_net),
1158 };
1159
1160 static int __init br_netfilter_init(void)
1161 {
1162 int ret;
1163
1164 ret = register_pernet_subsys(&brnf_net_ops);
1165 if (ret < 0)
1166 return ret;
1167
1168 ret = register_netdevice_notifier(&brnf_notifier);
1169 if (ret < 0) {
1170 unregister_pernet_subsys(&brnf_net_ops);
1171 return ret;
1172 }
1173
1174 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1175 printk(KERN_NOTICE "Bridge firewalling registered\n");
1176 return 0;
1177 }
1178
1179 static void __exit br_netfilter_fini(void)
1180 {
1181 RCU_INIT_POINTER(nf_br_ops, NULL);
1182 unregister_netdevice_notifier(&brnf_notifier);
1183 unregister_pernet_subsys(&brnf_net_ops);
1184 }
1185
1186 module_init(br_netfilter_init);
1187 module_exit(br_netfilter_fini);
1188
1189 MODULE_LICENSE("GPL");
1190 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1191 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1192 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");