1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49static unsigned int brnf_net_id __read_mostly;
50
51struct brnf_net {
52 bool enabled;
53};
54
55#ifdef CONFIG_SYSCTL
56static struct ctl_table_header *brnf_sysctl_header;
57static int brnf_call_iptables __read_mostly = 1;
58static int brnf_call_ip6tables __read_mostly = 1;
59static int brnf_call_arptables __read_mostly = 1;
60static int brnf_filter_vlan_tagged __read_mostly;
61static int brnf_filter_pppoe_tagged __read_mostly;
62static int brnf_pass_vlan_indev __read_mostly;
63#else
64#define brnf_call_iptables 1
65#define brnf_call_ip6tables 1
66#define brnf_call_arptables 1
67#define brnf_filter_vlan_tagged 0
68#define brnf_filter_pppoe_tagged 0
69#define brnf_pass_vlan_indev 0
70#endif
71
72#define IS_IP(skb) \
73 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
74
75#define IS_IPV6(skb) \
76 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
77
78#define IS_ARP(skb) \
79 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
80
81static inline __be16 vlan_proto(const struct sk_buff *skb)
82{
83 if (skb_vlan_tag_present(skb))
84 return skb->protocol;
85 else if (skb->protocol == htons(ETH_P_8021Q))
86 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
87 else
88 return 0;
89}
90
91#define IS_VLAN_IP(skb) \
92 (vlan_proto(skb) == htons(ETH_P_IP) && \
93 brnf_filter_vlan_tagged)
94
95#define IS_VLAN_IPV6(skb) \
96 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
97 brnf_filter_vlan_tagged)
98
99#define IS_VLAN_ARP(skb) \
100 (vlan_proto(skb) == htons(ETH_P_ARP) && \
101 brnf_filter_vlan_tagged)
102
103static inline __be16 pppoe_proto(const struct sk_buff *skb)
104{
105 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
106 sizeof(struct pppoe_hdr)));
107}
108
109#define IS_PPPOE_IP(skb) \
110 (skb->protocol == htons(ETH_P_PPP_SES) && \
111 pppoe_proto(skb) == htons(PPP_IP) && \
112 brnf_filter_pppoe_tagged)
113
114#define IS_PPPOE_IPV6(skb) \
115 (skb->protocol == htons(ETH_P_PPP_SES) && \
116 pppoe_proto(skb) == htons(PPP_IPV6) && \
117 brnf_filter_pppoe_tagged)
118
119
120#define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
121
122struct brnf_frag_data {
123 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
124 u8 encap_size;
125 u8 size;
126 u16 vlan_tci;
127 __be16 vlan_proto;
128};
129
130static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
131
132static void nf_bridge_info_free(struct sk_buff *skb)
133{
134 if (skb->nf_bridge) {
135 nf_bridge_put(skb->nf_bridge);
136 skb->nf_bridge = NULL;
137 }
138}
139
140static inline struct net_device *bridge_parent(const struct net_device *dev)
141{
142 struct net_bridge_port *port;
143
144 port = br_port_get_rcu(dev);
145 return port ? port->br->dev : NULL;
146}
147
148static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
149{
150 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
151
152 if (refcount_read(&nf_bridge->use) > 1) {
153 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
154
155 if (tmp) {
156 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
157 refcount_set(&tmp->use, 1);
158 }
159 nf_bridge_put(nf_bridge);
160 nf_bridge = tmp;
161 }
162 return nf_bridge;
163}
164
165unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
166{
167 switch (skb->protocol) {
168 case __cpu_to_be16(ETH_P_8021Q):
169 return VLAN_HLEN;
170 case __cpu_to_be16(ETH_P_PPP_SES):
171 return PPPOE_SES_HLEN;
172 default:
173 return 0;
174 }
175}
176
177static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
178{
179 unsigned int len = nf_bridge_encap_header_len(skb);
180
181 skb_pull(skb, len);
182 skb->network_header += len;
183}
184
185static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
186{
187 unsigned int len = nf_bridge_encap_header_len(skb);
188
189 skb_pull_rcsum(skb, len);
190 skb->network_header += len;
191}
192
193
194
195
196
197
198static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
199{
200 const struct iphdr *iph;
201 u32 len;
202
203 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
204 goto inhdr_error;
205
206 iph = ip_hdr(skb);
207
208
209 if (iph->ihl < 5 || iph->version != 4)
210 goto inhdr_error;
211
212 if (!pskb_may_pull(skb, iph->ihl*4))
213 goto inhdr_error;
214
215 iph = ip_hdr(skb);
216 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
217 goto csum_error;
218
219 len = ntohs(iph->tot_len);
220 if (skb->len < len) {
221 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
222 goto drop;
223 } else if (len < (iph->ihl*4))
224 goto inhdr_error;
225
226 if (pskb_trim_rcsum(skb, len)) {
227 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
228 goto drop;
229 }
230
231 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
232
233
234
235
236
237 return 0;
238
239csum_error:
240 __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
241inhdr_error:
242 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
243drop:
244 return -1;
245}
246
247void nf_bridge_update_protocol(struct sk_buff *skb)
248{
249 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
250
251 switch (nf_bridge->orig_proto) {
252 case BRNF_PROTO_8021Q:
253 skb->protocol = htons(ETH_P_8021Q);
254 break;
255 case BRNF_PROTO_PPPOE:
256 skb->protocol = htons(ETH_P_PPP_SES);
257 break;
258 case BRNF_PROTO_UNCHANGED:
259 break;
260 }
261}
262
263
264
265
266
267
268int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
269{
270 struct neighbour *neigh;
271 struct dst_entry *dst;
272
273 skb->dev = bridge_parent(skb->dev);
274 if (!skb->dev)
275 goto free_skb;
276 dst = skb_dst(skb);
277 neigh = dst_neigh_lookup_skb(dst, skb);
278 if (neigh) {
279 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
280 int ret;
281
282 if (neigh->hh.hh_len) {
283 neigh_hh_bridge(&neigh->hh, skb);
284 skb->dev = nf_bridge->physindev;
285 ret = br_handle_frame_finish(net, sk, skb);
286 } else {
287
288
289
290
291 skb_copy_from_linear_data_offset(skb,
292 -(ETH_HLEN-ETH_ALEN),
293 nf_bridge->neigh_header,
294 ETH_HLEN-ETH_ALEN);
295
296 nf_bridge->bridged_dnat = 1;
297
298 ret = neigh->output(neigh, skb);
299 }
300 neigh_release(neigh);
301 return ret;
302 }
303free_skb:
304 kfree_skb(skb);
305 return 0;
306}
307
308static inline bool
309br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
310 const struct nf_bridge_info *nf_bridge)
311{
312 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
313}
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
355{
356 struct net_device *dev = skb->dev;
357 struct iphdr *iph = ip_hdr(skb);
358 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
359 struct rtable *rt;
360 int err;
361
362 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
363
364 if (nf_bridge->pkt_otherhost) {
365 skb->pkt_type = PACKET_OTHERHOST;
366 nf_bridge->pkt_otherhost = false;
367 }
368 nf_bridge->in_prerouting = 0;
369 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
370 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
371 struct in_device *in_dev = __in_dev_get_rcu(dev);
372
373
374
375
376
377
378
379
380 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
381 goto free_skb;
382
383 rt = ip_route_output(net, iph->daddr, 0,
384 RT_TOS(iph->tos), 0);
385 if (!IS_ERR(rt)) {
386
387
388 if (rt->dst.dev == dev) {
389 skb_dst_set(skb, &rt->dst);
390 goto bridged_dnat;
391 }
392 ip_rt_put(rt);
393 }
394free_skb:
395 kfree_skb(skb);
396 return 0;
397 } else {
398 if (skb_dst(skb)->dev == dev) {
399bridged_dnat:
400 skb->dev = nf_bridge->physindev;
401 nf_bridge_update_protocol(skb);
402 nf_bridge_push_encap_header(skb);
403 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
404 net, sk, skb, skb->dev,
405 NULL,
406 br_nf_pre_routing_finish_bridge);
407 return 0;
408 }
409 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
410 skb->pkt_type = PACKET_HOST;
411 }
412 } else {
413 rt = bridge_parent_rtable(nf_bridge->physindev);
414 if (!rt) {
415 kfree_skb(skb);
416 return 0;
417 }
418 skb_dst_set_noref(skb, &rt->dst);
419 }
420
421 skb->dev = nf_bridge->physindev;
422 nf_bridge_update_protocol(skb);
423 nf_bridge_push_encap_header(skb);
424 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
425 br_handle_frame_finish);
426 return 0;
427}
428
429static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
430{
431 struct net_device *vlan, *br;
432
433 br = bridge_parent(dev);
434 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
435 return br;
436
437 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
438 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
439
440 return vlan ? vlan : br;
441}
442
443
444struct net_device *setup_pre_routing(struct sk_buff *skb)
445{
446 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
447
448 if (skb->pkt_type == PACKET_OTHERHOST) {
449 skb->pkt_type = PACKET_HOST;
450 nf_bridge->pkt_otherhost = true;
451 }
452
453 nf_bridge->in_prerouting = 1;
454 nf_bridge->physindev = skb->dev;
455 skb->dev = brnf_get_logical_dev(skb, skb->dev);
456
457 if (skb->protocol == htons(ETH_P_8021Q))
458 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
459 else if (skb->protocol == htons(ETH_P_PPP_SES))
460 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
461
462
463 skb_orphan(skb);
464 return skb->dev;
465}
466
467
468
469
470
471
472
473static unsigned int br_nf_pre_routing(void *priv,
474 struct sk_buff *skb,
475 const struct nf_hook_state *state)
476{
477 struct nf_bridge_info *nf_bridge;
478 struct net_bridge_port *p;
479 struct net_bridge *br;
480 __u32 len = nf_bridge_encap_header_len(skb);
481
482 if (unlikely(!pskb_may_pull(skb, len)))
483 return NF_DROP;
484
485 p = br_port_get_rcu(state->in);
486 if (p == NULL)
487 return NF_DROP;
488 br = p->br;
489
490 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
491 if (!brnf_call_ip6tables &&
492 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
493 return NF_ACCEPT;
494 if (!ipv6_mod_enabled()) {
495 pr_warn_once("Module ipv6 is disabled, so call_ip6tables is not supported.");
496 return NF_DROP;
497 }
498
499 nf_bridge_pull_encap_header_rcsum(skb);
500 return br_nf_pre_routing_ipv6(priv, skb, state);
501 }
502
503 if (!brnf_call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
504 return NF_ACCEPT;
505
506 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
507 return NF_ACCEPT;
508
509 nf_bridge_pull_encap_header_rcsum(skb);
510
511 if (br_validate_ipv4(state->net, skb))
512 return NF_DROP;
513
514 nf_bridge_put(skb->nf_bridge);
515 if (!nf_bridge_alloc(skb))
516 return NF_DROP;
517 if (!setup_pre_routing(skb))
518 return NF_DROP;
519
520 nf_bridge = nf_bridge_info_get(skb);
521 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
522
523 skb->protocol = htons(ETH_P_IP);
524 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
525
526 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
527 skb->dev, NULL,
528 br_nf_pre_routing_finish);
529
530 return NF_STOLEN;
531}
532
533
534
535static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
536{
537 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
538 struct net_device *in;
539
540 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
541
542 if (skb->protocol == htons(ETH_P_IP))
543 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
544
545 if (skb->protocol == htons(ETH_P_IPV6))
546 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
547
548 in = nf_bridge->physindev;
549 if (nf_bridge->pkt_otherhost) {
550 skb->pkt_type = PACKET_OTHERHOST;
551 nf_bridge->pkt_otherhost = false;
552 }
553 nf_bridge_update_protocol(skb);
554 } else {
555 in = *((struct net_device **)(skb->cb));
556 }
557 nf_bridge_push_encap_header(skb);
558
559 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
560 br_forward_finish);
561 return 0;
562}
563
564
565
566
567
568
569
570static unsigned int br_nf_forward_ip(void *priv,
571 struct sk_buff *skb,
572 const struct nf_hook_state *state)
573{
574 struct nf_bridge_info *nf_bridge;
575 struct net_device *parent;
576 u_int8_t pf;
577
578 nf_bridge = nf_bridge_info_get(skb);
579 if (!nf_bridge)
580 return NF_ACCEPT;
581
582
583
584 if (!nf_bridge_unshare(skb))
585 return NF_DROP;
586
587 nf_bridge = nf_bridge_info_get(skb);
588 if (!nf_bridge)
589 return NF_DROP;
590
591 parent = bridge_parent(state->out);
592 if (!parent)
593 return NF_DROP;
594
595 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
596 pf = NFPROTO_IPV4;
597 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
598 pf = NFPROTO_IPV6;
599 else
600 return NF_ACCEPT;
601
602 nf_bridge_pull_encap_header(skb);
603
604 if (skb->pkt_type == PACKET_OTHERHOST) {
605 skb->pkt_type = PACKET_HOST;
606 nf_bridge->pkt_otherhost = true;
607 }
608
609 if (pf == NFPROTO_IPV4) {
610 if (br_validate_ipv4(state->net, skb))
611 return NF_DROP;
612 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
613 }
614
615 if (pf == NFPROTO_IPV6) {
616 if (br_validate_ipv6(state->net, skb))
617 return NF_DROP;
618 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
619 }
620
621 nf_bridge->physoutdev = skb->dev;
622 if (pf == NFPROTO_IPV4)
623 skb->protocol = htons(ETH_P_IP);
624 else
625 skb->protocol = htons(ETH_P_IPV6);
626
627 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
628 brnf_get_logical_dev(skb, state->in),
629 parent, br_nf_forward_finish);
630
631 return NF_STOLEN;
632}
633
634static unsigned int br_nf_forward_arp(void *priv,
635 struct sk_buff *skb,
636 const struct nf_hook_state *state)
637{
638 struct net_bridge_port *p;
639 struct net_bridge *br;
640 struct net_device **d = (struct net_device **)(skb->cb);
641
642 p = br_port_get_rcu(state->out);
643 if (p == NULL)
644 return NF_ACCEPT;
645 br = p->br;
646
647 if (!brnf_call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
648 return NF_ACCEPT;
649
650 if (!IS_ARP(skb)) {
651 if (!IS_VLAN_ARP(skb))
652 return NF_ACCEPT;
653 nf_bridge_pull_encap_header(skb);
654 }
655
656 if (arp_hdr(skb)->ar_pln != 4) {
657 if (IS_VLAN_ARP(skb))
658 nf_bridge_push_encap_header(skb);
659 return NF_ACCEPT;
660 }
661 *d = state->in;
662 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
663 state->in, state->out, br_nf_forward_finish);
664
665 return NF_STOLEN;
666}
667
668static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
669{
670 struct brnf_frag_data *data;
671 int err;
672
673 data = this_cpu_ptr(&brnf_frag_data_storage);
674 err = skb_cow_head(skb, data->size);
675
676 if (err) {
677 kfree_skb(skb);
678 return 0;
679 }
680
681 if (data->vlan_tci) {
682 skb->vlan_tci = data->vlan_tci;
683 skb->vlan_proto = data->vlan_proto;
684 }
685
686 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
687 __skb_push(skb, data->encap_size);
688
689 nf_bridge_info_free(skb);
690 return br_dev_queue_push_xmit(net, sk, skb);
691}
692
693static int
694br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
695 int (*output)(struct net *, struct sock *, struct sk_buff *))
696{
697 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
698 struct iphdr *iph = ip_hdr(skb);
699
700 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
701 (IPCB(skb)->frag_max_size &&
702 IPCB(skb)->frag_max_size > mtu))) {
703 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
704 kfree_skb(skb);
705 return -EMSGSIZE;
706 }
707
708 return ip_do_fragment(net, sk, skb, output);
709}
710
711static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
712{
713 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
714
715 if (nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
716 return PPPOE_SES_HLEN;
717 return 0;
718}
719
720static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
721{
722 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
723 unsigned int mtu, mtu_reserved;
724
725 mtu_reserved = nf_bridge_mtu_reduction(skb);
726 mtu = skb->dev->mtu;
727
728 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
729 mtu = nf_bridge->frag_max_size;
730
731 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
732 nf_bridge_info_free(skb);
733 return br_dev_queue_push_xmit(net, sk, skb);
734 }
735
736
737
738
739 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
740 skb->protocol == htons(ETH_P_IP)) {
741 struct brnf_frag_data *data;
742
743 if (br_validate_ipv4(net, skb))
744 goto drop;
745
746 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
747
748 nf_bridge_update_protocol(skb);
749
750 data = this_cpu_ptr(&brnf_frag_data_storage);
751
752 data->vlan_tci = skb->vlan_tci;
753 data->vlan_proto = skb->vlan_proto;
754 data->encap_size = nf_bridge_encap_header_len(skb);
755 data->size = ETH_HLEN + data->encap_size;
756
757 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
758 data->size);
759
760 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
761 }
762 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
763 skb->protocol == htons(ETH_P_IPV6)) {
764 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
765 struct brnf_frag_data *data;
766
767 if (br_validate_ipv6(net, skb))
768 goto drop;
769
770 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
771
772 nf_bridge_update_protocol(skb);
773
774 data = this_cpu_ptr(&brnf_frag_data_storage);
775 data->encap_size = nf_bridge_encap_header_len(skb);
776 data->size = ETH_HLEN + data->encap_size;
777
778 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
779 data->size);
780
781 if (v6ops)
782 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
783
784 kfree_skb(skb);
785 return -EMSGSIZE;
786 }
787 nf_bridge_info_free(skb);
788 return br_dev_queue_push_xmit(net, sk, skb);
789 drop:
790 kfree_skb(skb);
791 return 0;
792}
793
794
795static unsigned int br_nf_post_routing(void *priv,
796 struct sk_buff *skb,
797 const struct nf_hook_state *state)
798{
799 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
800 struct net_device *realoutdev = bridge_parent(skb->dev);
801 u_int8_t pf;
802
803
804
805
806
807
808 if (!nf_bridge || !nf_bridge->physoutdev)
809 return NF_ACCEPT;
810
811 if (!realoutdev)
812 return NF_DROP;
813
814 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
815 pf = NFPROTO_IPV4;
816 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
817 pf = NFPROTO_IPV6;
818 else
819 return NF_ACCEPT;
820
821
822
823 if (skb->pkt_type == PACKET_OTHERHOST) {
824 skb->pkt_type = PACKET_HOST;
825 nf_bridge->pkt_otherhost = true;
826 }
827
828 nf_bridge_pull_encap_header(skb);
829 if (pf == NFPROTO_IPV4)
830 skb->protocol = htons(ETH_P_IP);
831 else
832 skb->protocol = htons(ETH_P_IPV6);
833
834 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
835 NULL, realoutdev,
836 br_nf_dev_queue_xmit);
837
838 return NF_STOLEN;
839}
840
841
842
843
844static unsigned int ip_sabotage_in(void *priv,
845 struct sk_buff *skb,
846 const struct nf_hook_state *state)
847{
848 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
849
850 if (nf_bridge && !nf_bridge->in_prerouting &&
851 !netif_is_l3_master(skb->dev) &&
852 !netif_is_l3_slave(skb->dev)) {
853 state->okfn(state->net, state->sk, skb);
854 return NF_STOLEN;
855 }
856
857 return NF_ACCEPT;
858}
859
860
861
862
863
864
865
866
867
868
869static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
870{
871 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
872
873 skb_pull(skb, ETH_HLEN);
874 nf_bridge->bridged_dnat = 0;
875
876 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
877
878 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
879 nf_bridge->neigh_header,
880 ETH_HLEN - ETH_ALEN);
881 skb->dev = nf_bridge->physindev;
882
883 nf_bridge->physoutdev = NULL;
884 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
885}
886
887static int br_nf_dev_xmit(struct sk_buff *skb)
888{
889 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
890
891 if (nf_bridge && nf_bridge->bridged_dnat) {
892 br_nf_pre_routing_finish_bridge_slow(skb);
893 return 1;
894 }
895 return 0;
896}
897
898static const struct nf_br_ops br_ops = {
899 .br_dev_xmit_hook = br_nf_dev_xmit,
900};
901
902
903
904static const struct nf_hook_ops br_nf_ops[] = {
905 {
906 .hook = br_nf_pre_routing,
907 .pf = NFPROTO_BRIDGE,
908 .hooknum = NF_BR_PRE_ROUTING,
909 .priority = NF_BR_PRI_BRNF,
910 },
911 {
912 .hook = br_nf_forward_ip,
913 .pf = NFPROTO_BRIDGE,
914 .hooknum = NF_BR_FORWARD,
915 .priority = NF_BR_PRI_BRNF - 1,
916 },
917 {
918 .hook = br_nf_forward_arp,
919 .pf = NFPROTO_BRIDGE,
920 .hooknum = NF_BR_FORWARD,
921 .priority = NF_BR_PRI_BRNF,
922 },
923 {
924 .hook = br_nf_post_routing,
925 .pf = NFPROTO_BRIDGE,
926 .hooknum = NF_BR_POST_ROUTING,
927 .priority = NF_BR_PRI_LAST,
928 },
929 {
930 .hook = ip_sabotage_in,
931 .pf = NFPROTO_IPV4,
932 .hooknum = NF_INET_PRE_ROUTING,
933 .priority = NF_IP_PRI_FIRST,
934 },
935 {
936 .hook = ip_sabotage_in,
937 .pf = NFPROTO_IPV6,
938 .hooknum = NF_INET_PRE_ROUTING,
939 .priority = NF_IP6_PRI_FIRST,
940 },
941};
942
943static int brnf_device_event(struct notifier_block *unused, unsigned long event,
944 void *ptr)
945{
946 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
947 struct brnf_net *brnet;
948 struct net *net;
949 int ret;
950
951 if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
952 return NOTIFY_DONE;
953
954 ASSERT_RTNL();
955
956 net = dev_net(dev);
957 brnet = net_generic(net, brnf_net_id);
958 if (brnet->enabled)
959 return NOTIFY_OK;
960
961 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
962 if (ret)
963 return NOTIFY_BAD;
964
965 brnet->enabled = true;
966 return NOTIFY_OK;
967}
968
969static void __net_exit brnf_exit_net(struct net *net)
970{
971 struct brnf_net *brnet = net_generic(net, brnf_net_id);
972
973 if (!brnet->enabled)
974 return;
975
976 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
977 brnet->enabled = false;
978}
979
980static struct pernet_operations brnf_net_ops __read_mostly = {
981 .exit = brnf_exit_net,
982 .id = &brnf_net_id,
983 .size = sizeof(struct brnf_net),
984};
985
986static struct notifier_block brnf_notifier __read_mostly = {
987 .notifier_call = brnf_device_event,
988};
989
990
991
992
993
994
995int br_nf_hook_thresh(unsigned int hook, struct net *net,
996 struct sock *sk, struct sk_buff *skb,
997 struct net_device *indev,
998 struct net_device *outdev,
999 int (*okfn)(struct net *, struct sock *,
1000 struct sk_buff *))
1001{
1002 const struct nf_hook_entries *e;
1003 struct nf_hook_state state;
1004 struct nf_hook_ops **ops;
1005 unsigned int i;
1006 int ret;
1007
1008 e = rcu_dereference(net->nf.hooks_bridge[hook]);
1009 if (!e)
1010 return okfn(net, sk, skb);
1011
1012 ops = nf_hook_entries_get_hook_ops(e);
1013 for (i = 0; i < e->num_hook_entries &&
1014 ops[i]->priority <= NF_BR_PRI_BRNF; i++)
1015 ;
1016
1017 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1018 sk, net, okfn);
1019
1020 ret = nf_hook_slow(skb, &state, e, i);
1021 if (ret == 1)
1022 ret = okfn(net, sk, skb);
1023
1024 return ret;
1025}
1026
1027#ifdef CONFIG_SYSCTL
1028static
1029int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1030 void __user *buffer, size_t *lenp, loff_t *ppos)
1031{
1032 int ret;
1033
1034 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1035
1036 if (write && *(int *)(ctl->data))
1037 *(int *)(ctl->data) = 1;
1038 return ret;
1039}
1040
1041static struct ctl_table brnf_table[] = {
1042 {
1043 .procname = "bridge-nf-call-arptables",
1044 .data = &brnf_call_arptables,
1045 .maxlen = sizeof(int),
1046 .mode = 0644,
1047 .proc_handler = brnf_sysctl_call_tables,
1048 },
1049 {
1050 .procname = "bridge-nf-call-iptables",
1051 .data = &brnf_call_iptables,
1052 .maxlen = sizeof(int),
1053 .mode = 0644,
1054 .proc_handler = brnf_sysctl_call_tables,
1055 },
1056 {
1057 .procname = "bridge-nf-call-ip6tables",
1058 .data = &brnf_call_ip6tables,
1059 .maxlen = sizeof(int),
1060 .mode = 0644,
1061 .proc_handler = brnf_sysctl_call_tables,
1062 },
1063 {
1064 .procname = "bridge-nf-filter-vlan-tagged",
1065 .data = &brnf_filter_vlan_tagged,
1066 .maxlen = sizeof(int),
1067 .mode = 0644,
1068 .proc_handler = brnf_sysctl_call_tables,
1069 },
1070 {
1071 .procname = "bridge-nf-filter-pppoe-tagged",
1072 .data = &brnf_filter_pppoe_tagged,
1073 .maxlen = sizeof(int),
1074 .mode = 0644,
1075 .proc_handler = brnf_sysctl_call_tables,
1076 },
1077 {
1078 .procname = "bridge-nf-pass-vlan-input-dev",
1079 .data = &brnf_pass_vlan_indev,
1080 .maxlen = sizeof(int),
1081 .mode = 0644,
1082 .proc_handler = brnf_sysctl_call_tables,
1083 },
1084 { }
1085};
1086#endif
1087
1088static int __init br_netfilter_init(void)
1089{
1090 int ret;
1091
1092 ret = register_pernet_subsys(&brnf_net_ops);
1093 if (ret < 0)
1094 return ret;
1095
1096 ret = register_netdevice_notifier(&brnf_notifier);
1097 if (ret < 0) {
1098 unregister_pernet_subsys(&brnf_net_ops);
1099 return ret;
1100 }
1101
1102#ifdef CONFIG_SYSCTL
1103 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1104 if (brnf_sysctl_header == NULL) {
1105 printk(KERN_WARNING
1106 "br_netfilter: can't register to sysctl.\n");
1107 unregister_netdevice_notifier(&brnf_notifier);
1108 unregister_pernet_subsys(&brnf_net_ops);
1109 return -ENOMEM;
1110 }
1111#endif
1112 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1113 printk(KERN_NOTICE "Bridge firewalling registered\n");
1114 return 0;
1115}
1116
1117static void __exit br_netfilter_fini(void)
1118{
1119 RCU_INIT_POINTER(nf_br_ops, NULL);
1120 unregister_netdevice_notifier(&brnf_notifier);
1121 unregister_pernet_subsys(&brnf_net_ops);
1122#ifdef CONFIG_SYSCTL
1123 unregister_net_sysctl_table(brnf_sysctl_header);
1124#endif
1125}
1126
1127module_init(br_netfilter_init);
1128module_exit(br_netfilter_fini);
1129
1130MODULE_LICENSE("GPL");
1131MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1132MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1133MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");
1134