]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bridge/br_netfilter.c
Merge branch 'master' of git://1984.lsi.us.es/nf-next
[karo-tx-linux.git] / net / bridge / br_netfilter.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/inetdevice.h>
34
35 #include <net/ip.h>
36 #include <net/ipv6.h>
37 #include <net/route.h>
38
39 #include <asm/uaccess.h>
40 #include "br_private.h"
41 #ifdef CONFIG_SYSCTL
42 #include <linux/sysctl.h>
43 #endif
44
45 #define skb_origaddr(skb)        (((struct bridge_skb_cb *) \
46                                  (skb->nf_bridge->data))->daddr.ipv4)
47 #define store_orig_dstaddr(skb)  (skb_origaddr(skb) = ip_hdr(skb)->daddr)
48 #define dnat_took_place(skb)     (skb_origaddr(skb) != ip_hdr(skb)->daddr)
49
50 #ifdef CONFIG_SYSCTL
51 static struct ctl_table_header *brnf_sysctl_header;
52 static int brnf_call_iptables __read_mostly = 1;
53 static int brnf_call_ip6tables __read_mostly = 1;
54 static int brnf_call_arptables __read_mostly = 1;
55 static int brnf_filter_vlan_tagged __read_mostly = 0;
56 static int brnf_filter_pppoe_tagged __read_mostly = 0;
57 static int brnf_pass_vlan_indev __read_mostly = 0;
58 #else
59 #define brnf_call_iptables 1
60 #define brnf_call_ip6tables 1
61 #define brnf_call_arptables 1
62 #define brnf_filter_vlan_tagged 0
63 #define brnf_filter_pppoe_tagged 0
64 #define brnf_pass_vlan_indev 0
65 #endif
66
67 #define IS_IP(skb) \
68         (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
69
70 #define IS_IPV6(skb) \
71         (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
72
73 #define IS_ARP(skb) \
74         (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
75
76 static inline __be16 vlan_proto(const struct sk_buff *skb)
77 {
78         if (vlan_tx_tag_present(skb))
79                 return skb->protocol;
80         else if (skb->protocol == htons(ETH_P_8021Q))
81                 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
82         else
83                 return 0;
84 }
85
86 #define IS_VLAN_IP(skb) \
87         (vlan_proto(skb) == htons(ETH_P_IP) && \
88          brnf_filter_vlan_tagged)
89
90 #define IS_VLAN_IPV6(skb) \
91         (vlan_proto(skb) == htons(ETH_P_IPV6) && \
92          brnf_filter_vlan_tagged)
93
94 #define IS_VLAN_ARP(skb) \
95         (vlan_proto(skb) == htons(ETH_P_ARP) && \
96          brnf_filter_vlan_tagged)
97
98 static inline __be16 pppoe_proto(const struct sk_buff *skb)
99 {
100         return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
101                             sizeof(struct pppoe_hdr)));
102 }
103
104 #define IS_PPPOE_IP(skb) \
105         (skb->protocol == htons(ETH_P_PPP_SES) && \
106          pppoe_proto(skb) == htons(PPP_IP) && \
107          brnf_filter_pppoe_tagged)
108
109 #define IS_PPPOE_IPV6(skb) \
110         (skb->protocol == htons(ETH_P_PPP_SES) && \
111          pppoe_proto(skb) == htons(PPP_IPV6) && \
112          brnf_filter_pppoe_tagged)
113
114 static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
115 {
116 }
117
118 static u32 *fake_cow_metrics(struct dst_entry *dst, unsigned long old)
119 {
120         return NULL;
121 }
122
123 static struct neighbour *fake_neigh_lookup(const struct dst_entry *dst,
124                                            struct sk_buff *skb,
125                                            const void *daddr)
126 {
127         return NULL;
128 }
129
130 static unsigned int fake_mtu(const struct dst_entry *dst)
131 {
132         return dst->dev->mtu;
133 }
134
135 static struct dst_ops fake_dst_ops = {
136         .family =               AF_INET,
137         .protocol =             cpu_to_be16(ETH_P_IP),
138         .update_pmtu =          fake_update_pmtu,
139         .cow_metrics =          fake_cow_metrics,
140         .neigh_lookup =         fake_neigh_lookup,
141         .mtu =                  fake_mtu,
142 };
143
144 /*
145  * Initialize bogus route table used to keep netfilter happy.
146  * Currently, we fill in the PMTU entry because netfilter
147  * refragmentation needs it, and the rt_flags entry because
148  * ipt_REJECT needs it.  Future netfilter modules might
149  * require us to fill additional fields.
150  */
151 static const u32 br_dst_default_metrics[RTAX_MAX] = {
152         [RTAX_MTU - 1] = 1500,
153 };
154
155 void br_netfilter_rtable_init(struct net_bridge *br)
156 {
157         struct rtable *rt = &br->fake_rtable;
158
159         atomic_set(&rt->dst.__refcnt, 1);
160         rt->dst.dev = br->dev;
161         rt->dst.path = &rt->dst;
162         dst_init_metrics(&rt->dst, br_dst_default_metrics, true);
163         rt->dst.flags   = DST_NOXFRM | DST_NOPEER | DST_FAKE_RTABLE;
164         rt->dst.ops = &fake_dst_ops;
165 }
166
167 static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
168 {
169         struct net_bridge_port *port;
170
171         port = br_port_get_rcu(dev);
172         return port ? &port->br->fake_rtable : NULL;
173 }
174
175 static inline struct net_device *bridge_parent(const struct net_device *dev)
176 {
177         struct net_bridge_port *port;
178
179         port = br_port_get_rcu(dev);
180         return port ? port->br->dev : NULL;
181 }
182
183 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
184 {
185         skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
186         if (likely(skb->nf_bridge))
187                 atomic_set(&(skb->nf_bridge->use), 1);
188
189         return skb->nf_bridge;
190 }
191
192 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
193 {
194         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
195
196         if (atomic_read(&nf_bridge->use) > 1) {
197                 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
198
199                 if (tmp) {
200                         memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
201                         atomic_set(&tmp->use, 1);
202                 }
203                 nf_bridge_put(nf_bridge);
204                 nf_bridge = tmp;
205         }
206         return nf_bridge;
207 }
208
209 static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
210 {
211         unsigned int len = nf_bridge_encap_header_len(skb);
212
213         skb_push(skb, len);
214         skb->network_header -= len;
215 }
216
217 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
218 {
219         unsigned int len = nf_bridge_encap_header_len(skb);
220
221         skb_pull(skb, len);
222         skb->network_header += len;
223 }
224
225 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
226 {
227         unsigned int len = nf_bridge_encap_header_len(skb);
228
229         skb_pull_rcsum(skb, len);
230         skb->network_header += len;
231 }
232
233 static inline void nf_bridge_save_header(struct sk_buff *skb)
234 {
235         int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
236
237         skb_copy_from_linear_data_offset(skb, -header_size,
238                                          skb->nf_bridge->data, header_size);
239 }
240
241 static inline void nf_bridge_update_protocol(struct sk_buff *skb)
242 {
243         if (skb->nf_bridge->mask & BRNF_8021Q)
244                 skb->protocol = htons(ETH_P_8021Q);
245         else if (skb->nf_bridge->mask & BRNF_PPPoE)
246                 skb->protocol = htons(ETH_P_PPP_SES);
247 }
248
249 /* When handing a packet over to the IP layer
250  * check whether we have a skb that is in the
251  * expected format
252  */
253
254 static int br_parse_ip_options(struct sk_buff *skb)
255 {
256         struct ip_options *opt;
257         const struct iphdr *iph;
258         struct net_device *dev = skb->dev;
259         u32 len;
260
261         iph = ip_hdr(skb);
262         opt = &(IPCB(skb)->opt);
263
264         /* Basic sanity checks */
265         if (iph->ihl < 5 || iph->version != 4)
266                 goto inhdr_error;
267
268         if (!pskb_may_pull(skb, iph->ihl*4))
269                 goto inhdr_error;
270
271         iph = ip_hdr(skb);
272         if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
273                 goto inhdr_error;
274
275         len = ntohs(iph->tot_len);
276         if (skb->len < len) {
277                 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
278                 goto drop;
279         } else if (len < (iph->ihl*4))
280                 goto inhdr_error;
281
282         if (pskb_trim_rcsum(skb, len)) {
283                 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
284                 goto drop;
285         }
286
287         memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
288         if (iph->ihl == 5)
289                 return 0;
290
291         opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
292         if (ip_options_compile(dev_net(dev), opt, skb))
293                 goto inhdr_error;
294
295         /* Check correct handling of SRR option */
296         if (unlikely(opt->srr)) {
297                 struct in_device *in_dev = __in_dev_get_rcu(dev);
298                 if (in_dev && !IN_DEV_SOURCE_ROUTE(in_dev))
299                         goto drop;
300
301                 if (ip_options_rcv_srr(skb))
302                         goto drop;
303         }
304
305         return 0;
306
307 inhdr_error:
308         IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
309 drop:
310         return -1;
311 }
312
313 /* Fill in the header for fragmented IP packets handled by
314  * the IPv4 connection tracking code.
315  */
316 int nf_bridge_copy_header(struct sk_buff *skb)
317 {
318         int err;
319         unsigned int header_size;
320
321         nf_bridge_update_protocol(skb);
322         header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
323         err = skb_cow_head(skb, header_size);
324         if (err)
325                 return err;
326
327         skb_copy_to_linear_data_offset(skb, -header_size,
328                                        skb->nf_bridge->data, header_size);
329         __skb_push(skb, nf_bridge_encap_header_len(skb));
330         return 0;
331 }
332
333 /* PF_BRIDGE/PRE_ROUTING *********************************************/
334 /* Undo the changes made for ip6tables PREROUTING and continue the
335  * bridge PRE_ROUTING hook. */
336 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
337 {
338         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
339         struct rtable *rt;
340
341         if (nf_bridge->mask & BRNF_PKT_TYPE) {
342                 skb->pkt_type = PACKET_OTHERHOST;
343                 nf_bridge->mask ^= BRNF_PKT_TYPE;
344         }
345         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
346
347         rt = bridge_parent_rtable(nf_bridge->physindev);
348         if (!rt) {
349                 kfree_skb(skb);
350                 return 0;
351         }
352         skb_dst_set_noref(skb, &rt->dst);
353
354         skb->dev = nf_bridge->physindev;
355         nf_bridge_update_protocol(skb);
356         nf_bridge_push_encap_header(skb);
357         NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
358                        br_handle_frame_finish, 1);
359
360         return 0;
361 }
362
363 /* Obtain the correct destination MAC address, while preserving the original
364  * source MAC address. If we already know this address, we just copy it. If we
365  * don't, we use the neighbour framework to find out. In both cases, we make
366  * sure that br_handle_frame_finish() is called afterwards.
367  */
368 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
369 {
370         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
371         struct neighbour *neigh;
372         struct dst_entry *dst;
373
374         skb->dev = bridge_parent(skb->dev);
375         if (!skb->dev)
376                 goto free_skb;
377         dst = skb_dst(skb);
378         neigh = dst_neigh_lookup_skb(dst, skb);
379         if (neigh) {
380                 int ret;
381
382                 if (neigh->hh.hh_len) {
383                         neigh_hh_bridge(&neigh->hh, skb);
384                         skb->dev = nf_bridge->physindev;
385                         ret = br_handle_frame_finish(skb);
386                 } else {
387                         /* the neighbour function below overwrites the complete
388                          * MAC header, so we save the Ethernet source address and
389                          * protocol number.
390                          */
391                         skb_copy_from_linear_data_offset(skb,
392                                                          -(ETH_HLEN-ETH_ALEN),
393                                                          skb->nf_bridge->data,
394                                                          ETH_HLEN-ETH_ALEN);
395                         /* tell br_dev_xmit to continue with forwarding */
396                         nf_bridge->mask |= BRNF_BRIDGED_DNAT;
397                         ret = neigh->output(neigh, skb);
398                 }
399                 neigh_release(neigh);
400                 return ret;
401         }
402 free_skb:
403         kfree_skb(skb);
404         return 0;
405 }
406
407 /* This requires some explaining. If DNAT has taken place,
408  * we will need to fix up the destination Ethernet address.
409  *
410  * There are two cases to consider:
411  * 1. The packet was DNAT'ed to a device in the same bridge
412  *    port group as it was received on. We can still bridge
413  *    the packet.
414  * 2. The packet was DNAT'ed to a different device, either
415  *    a non-bridged device or another bridge port group.
416  *    The packet will need to be routed.
417  *
418  * The correct way of distinguishing between these two cases is to
419  * call ip_route_input() and to look at skb->dst->dev, which is
420  * changed to the destination device if ip_route_input() succeeds.
421  *
422  * Let's first consider the case that ip_route_input() succeeds:
423  *
424  * If the output device equals the logical bridge device the packet
425  * came in on, we can consider this bridging. The corresponding MAC
426  * address will be obtained in br_nf_pre_routing_finish_bridge.
427  * Otherwise, the packet is considered to be routed and we just
428  * change the destination MAC address so that the packet will
429  * later be passed up to the IP stack to be routed. For a redirected
430  * packet, ip_route_input() will give back the localhost as output device,
431  * which differs from the bridge device.
432  *
433  * Let's now consider the case that ip_route_input() fails:
434  *
435  * This can be because the destination address is martian, in which case
436  * the packet will be dropped.
437  * If IP forwarding is disabled, ip_route_input() will fail, while
438  * ip_route_output_key() can return success. The source
439  * address for ip_route_output_key() is set to zero, so ip_route_output_key()
440  * thinks we're handling a locally generated packet and won't care
441  * if IP forwarding is enabled. If the output device equals the logical bridge
442  * device, we proceed as if ip_route_input() succeeded. If it differs from the
443  * logical bridge port or if ip_route_output_key() fails we drop the packet.
444  */
445 static int br_nf_pre_routing_finish(struct sk_buff *skb)
446 {
447         struct net_device *dev = skb->dev;
448         struct iphdr *iph = ip_hdr(skb);
449         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
450         struct rtable *rt;
451         int err;
452
453         if (nf_bridge->mask & BRNF_PKT_TYPE) {
454                 skb->pkt_type = PACKET_OTHERHOST;
455                 nf_bridge->mask ^= BRNF_PKT_TYPE;
456         }
457         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
458         if (dnat_took_place(skb)) {
459                 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
460                         struct in_device *in_dev = __in_dev_get_rcu(dev);
461
462                         /* If err equals -EHOSTUNREACH the error is due to a
463                          * martian destination or due to the fact that
464                          * forwarding is disabled. For most martian packets,
465                          * ip_route_output_key() will fail. It won't fail for 2 types of
466                          * martian destinations: loopback destinations and destination
467                          * 0.0.0.0. In both cases the packet will be dropped because the
468                          * destination is the loopback device and not the bridge. */
469                         if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
470                                 goto free_skb;
471
472                         rt = ip_route_output(dev_net(dev), iph->daddr, 0,
473                                              RT_TOS(iph->tos), 0);
474                         if (!IS_ERR(rt)) {
475                                 /* - Bridged-and-DNAT'ed traffic doesn't
476                                  *   require ip_forwarding. */
477                                 if (rt->dst.dev == dev) {
478                                         skb_dst_set(skb, &rt->dst);
479                                         goto bridged_dnat;
480                                 }
481                                 ip_rt_put(rt);
482                         }
483 free_skb:
484                         kfree_skb(skb);
485                         return 0;
486                 } else {
487                         if (skb_dst(skb)->dev == dev) {
488 bridged_dnat:
489                                 skb->dev = nf_bridge->physindev;
490                                 nf_bridge_update_protocol(skb);
491                                 nf_bridge_push_encap_header(skb);
492                                 NF_HOOK_THRESH(NFPROTO_BRIDGE,
493                                                NF_BR_PRE_ROUTING,
494                                                skb, skb->dev, NULL,
495                                                br_nf_pre_routing_finish_bridge,
496                                                1);
497                                 return 0;
498                         }
499                         memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
500                         skb->pkt_type = PACKET_HOST;
501                 }
502         } else {
503                 rt = bridge_parent_rtable(nf_bridge->physindev);
504                 if (!rt) {
505                         kfree_skb(skb);
506                         return 0;
507                 }
508                 skb_dst_set_noref(skb, &rt->dst);
509         }
510
511         skb->dev = nf_bridge->physindev;
512         nf_bridge_update_protocol(skb);
513         nf_bridge_push_encap_header(skb);
514         NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
515                        br_handle_frame_finish, 1);
516
517         return 0;
518 }
519
520 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
521 {
522         struct net_device *vlan, *br;
523
524         br = bridge_parent(dev);
525         if (brnf_pass_vlan_indev == 0 || !vlan_tx_tag_present(skb))
526                 return br;
527
528         vlan = __vlan_find_dev_deep(br, vlan_tx_tag_get(skb) & VLAN_VID_MASK);
529
530         return vlan ? vlan : br;
531 }
532
533 /* Some common code for IPv4/IPv6 */
534 static struct net_device *setup_pre_routing(struct sk_buff *skb)
535 {
536         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
537
538         if (skb->pkt_type == PACKET_OTHERHOST) {
539                 skb->pkt_type = PACKET_HOST;
540                 nf_bridge->mask |= BRNF_PKT_TYPE;
541         }
542
543         nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
544         nf_bridge->physindev = skb->dev;
545         skb->dev = brnf_get_logical_dev(skb, skb->dev);
546         if (skb->protocol == htons(ETH_P_8021Q))
547                 nf_bridge->mask |= BRNF_8021Q;
548         else if (skb->protocol == htons(ETH_P_PPP_SES))
549                 nf_bridge->mask |= BRNF_PPPoE;
550
551         return skb->dev;
552 }
553
554 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
555 static int check_hbh_len(struct sk_buff *skb)
556 {
557         unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
558         u32 pkt_len;
559         const unsigned char *nh = skb_network_header(skb);
560         int off = raw - nh;
561         int len = (raw[1] + 1) << 3;
562
563         if ((raw + len) - skb->data > skb_headlen(skb))
564                 goto bad;
565
566         off += 2;
567         len -= 2;
568
569         while (len > 0) {
570                 int optlen = nh[off + 1] + 2;
571
572                 switch (nh[off]) {
573                 case IPV6_TLV_PAD1:
574                         optlen = 1;
575                         break;
576
577                 case IPV6_TLV_PADN:
578                         break;
579
580                 case IPV6_TLV_JUMBO:
581                         if (nh[off + 1] != 4 || (off & 3) != 2)
582                                 goto bad;
583                         pkt_len = ntohl(*(__be32 *) (nh + off + 2));
584                         if (pkt_len <= IPV6_MAXPLEN ||
585                             ipv6_hdr(skb)->payload_len)
586                                 goto bad;
587                         if (pkt_len > skb->len - sizeof(struct ipv6hdr))
588                                 goto bad;
589                         if (pskb_trim_rcsum(skb,
590                                             pkt_len + sizeof(struct ipv6hdr)))
591                                 goto bad;
592                         nh = skb_network_header(skb);
593                         break;
594                 default:
595                         if (optlen > len)
596                                 goto bad;
597                         break;
598                 }
599                 off += optlen;
600                 len -= optlen;
601         }
602         if (len == 0)
603                 return 0;
604 bad:
605         return -1;
606
607 }
608
609 /* Replicate the checks that IPv6 does on packet reception and pass the packet
610  * to ip6tables, which doesn't support NAT, so things are fairly simple. */
611 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
612                                            struct sk_buff *skb,
613                                            const struct net_device *in,
614                                            const struct net_device *out,
615                                            int (*okfn)(struct sk_buff *))
616 {
617         const struct ipv6hdr *hdr;
618         u32 pkt_len;
619
620         if (skb->len < sizeof(struct ipv6hdr))
621                 return NF_DROP;
622
623         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
624                 return NF_DROP;
625
626         hdr = ipv6_hdr(skb);
627
628         if (hdr->version != 6)
629                 return NF_DROP;
630
631         pkt_len = ntohs(hdr->payload_len);
632
633         if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
634                 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
635                         return NF_DROP;
636                 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
637                         return NF_DROP;
638         }
639         if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
640                 return NF_DROP;
641
642         nf_bridge_put(skb->nf_bridge);
643         if (!nf_bridge_alloc(skb))
644                 return NF_DROP;
645         if (!setup_pre_routing(skb))
646                 return NF_DROP;
647
648         skb->protocol = htons(ETH_P_IPV6);
649         NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
650                 br_nf_pre_routing_finish_ipv6);
651
652         return NF_STOLEN;
653 }
654
655 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
656  * Replicate the checks that IPv4 does on packet reception.
657  * Set skb->dev to the bridge device (i.e. parent of the
658  * receiving device) to make netfilter happy, the REDIRECT
659  * target in particular.  Save the original destination IP
660  * address to be able to detect DNAT afterwards. */
661 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
662                                       const struct net_device *in,
663                                       const struct net_device *out,
664                                       int (*okfn)(struct sk_buff *))
665 {
666         struct net_bridge_port *p;
667         struct net_bridge *br;
668         __u32 len = nf_bridge_encap_header_len(skb);
669
670         if (unlikely(!pskb_may_pull(skb, len)))
671                 return NF_DROP;
672
673         p = br_port_get_rcu(in);
674         if (p == NULL)
675                 return NF_DROP;
676         br = p->br;
677
678         if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
679                 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
680                         return NF_ACCEPT;
681
682                 nf_bridge_pull_encap_header_rcsum(skb);
683                 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
684         }
685
686         if (!brnf_call_iptables && !br->nf_call_iptables)
687                 return NF_ACCEPT;
688
689         if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
690                 return NF_ACCEPT;
691
692         nf_bridge_pull_encap_header_rcsum(skb);
693
694         if (br_parse_ip_options(skb))
695                 return NF_DROP;
696
697         nf_bridge_put(skb->nf_bridge);
698         if (!nf_bridge_alloc(skb))
699                 return NF_DROP;
700         if (!setup_pre_routing(skb))
701                 return NF_DROP;
702         store_orig_dstaddr(skb);
703         skb->protocol = htons(ETH_P_IP);
704
705         NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
706                 br_nf_pre_routing_finish);
707
708         return NF_STOLEN;
709 }
710
711
712 /* PF_BRIDGE/LOCAL_IN ************************************************/
713 /* The packet is locally destined, which requires a real
714  * dst_entry, so detach the fake one.  On the way up, the
715  * packet would pass through PRE_ROUTING again (which already
716  * took place when the packet entered the bridge), but we
717  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
718  * prevent this from happening. */
719 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
720                                    const struct net_device *in,
721                                    const struct net_device *out,
722                                    int (*okfn)(struct sk_buff *))
723 {
724         br_drop_fake_rtable(skb);
725         return NF_ACCEPT;
726 }
727
728 /* PF_BRIDGE/FORWARD *************************************************/
729 static int br_nf_forward_finish(struct sk_buff *skb)
730 {
731         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
732         struct net_device *in;
733
734         if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
735                 in = nf_bridge->physindev;
736                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
737                         skb->pkt_type = PACKET_OTHERHOST;
738                         nf_bridge->mask ^= BRNF_PKT_TYPE;
739                 }
740                 nf_bridge_update_protocol(skb);
741         } else {
742                 in = *((struct net_device **)(skb->cb));
743         }
744         nf_bridge_push_encap_header(skb);
745
746         NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
747                        skb->dev, br_forward_finish, 1);
748         return 0;
749 }
750
751
752 /* This is the 'purely bridged' case.  For IP, we pass the packet to
753  * netfilter with indev and outdev set to the bridge device,
754  * but we are still able to filter on the 'real' indev/outdev
755  * because of the physdev module. For ARP, indev and outdev are the
756  * bridge ports. */
757 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
758                                      const struct net_device *in,
759                                      const struct net_device *out,
760                                      int (*okfn)(struct sk_buff *))
761 {
762         struct nf_bridge_info *nf_bridge;
763         struct net_device *parent;
764         u_int8_t pf;
765
766         if (!skb->nf_bridge)
767                 return NF_ACCEPT;
768
769         /* Need exclusive nf_bridge_info since we might have multiple
770          * different physoutdevs. */
771         if (!nf_bridge_unshare(skb))
772                 return NF_DROP;
773
774         parent = bridge_parent(out);
775         if (!parent)
776                 return NF_DROP;
777
778         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
779                 pf = NFPROTO_IPV4;
780         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
781                 pf = NFPROTO_IPV6;
782         else
783                 return NF_ACCEPT;
784
785         nf_bridge_pull_encap_header(skb);
786
787         nf_bridge = skb->nf_bridge;
788         if (skb->pkt_type == PACKET_OTHERHOST) {
789                 skb->pkt_type = PACKET_HOST;
790                 nf_bridge->mask |= BRNF_PKT_TYPE;
791         }
792
793         if (pf == NFPROTO_IPV4 && br_parse_ip_options(skb))
794                 return NF_DROP;
795
796         /* The physdev module checks on this */
797         nf_bridge->mask |= BRNF_BRIDGED;
798         nf_bridge->physoutdev = skb->dev;
799         if (pf == NFPROTO_IPV4)
800                 skb->protocol = htons(ETH_P_IP);
801         else
802                 skb->protocol = htons(ETH_P_IPV6);
803
804         NF_HOOK(pf, NF_INET_FORWARD, skb, brnf_get_logical_dev(skb, in), parent,
805                 br_nf_forward_finish);
806
807         return NF_STOLEN;
808 }
809
810 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
811                                       const struct net_device *in,
812                                       const struct net_device *out,
813                                       int (*okfn)(struct sk_buff *))
814 {
815         struct net_bridge_port *p;
816         struct net_bridge *br;
817         struct net_device **d = (struct net_device **)(skb->cb);
818
819         p = br_port_get_rcu(out);
820         if (p == NULL)
821                 return NF_ACCEPT;
822         br = p->br;
823
824         if (!brnf_call_arptables && !br->nf_call_arptables)
825                 return NF_ACCEPT;
826
827         if (!IS_ARP(skb)) {
828                 if (!IS_VLAN_ARP(skb))
829                         return NF_ACCEPT;
830                 nf_bridge_pull_encap_header(skb);
831         }
832
833         if (arp_hdr(skb)->ar_pln != 4) {
834                 if (IS_VLAN_ARP(skb))
835                         nf_bridge_push_encap_header(skb);
836                 return NF_ACCEPT;
837         }
838         *d = (struct net_device *)in;
839         NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
840                 (struct net_device *)out, br_nf_forward_finish);
841
842         return NF_STOLEN;
843 }
844
845 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV4)
846 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
847 {
848         int ret;
849
850         if (skb->nfct != NULL && skb->protocol == htons(ETH_P_IP) &&
851             skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
852             !skb_is_gso(skb)) {
853                 if (br_parse_ip_options(skb))
854                         /* Drop invalid packet */
855                         return NF_DROP;
856                 ret = ip_fragment(skb, br_dev_queue_push_xmit);
857         } else
858                 ret = br_dev_queue_push_xmit(skb);
859
860         return ret;
861 }
862 #else
863 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
864 {
865         return br_dev_queue_push_xmit(skb);
866 }
867 #endif
868
869 /* PF_BRIDGE/POST_ROUTING ********************************************/
870 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
871                                        const struct net_device *in,
872                                        const struct net_device *out,
873                                        int (*okfn)(struct sk_buff *))
874 {
875         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
876         struct net_device *realoutdev = bridge_parent(skb->dev);
877         u_int8_t pf;
878
879         if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
880                 return NF_ACCEPT;
881
882         if (!realoutdev)
883                 return NF_DROP;
884
885         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
886                 pf = NFPROTO_IPV4;
887         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
888                 pf = NFPROTO_IPV6;
889         else
890                 return NF_ACCEPT;
891
892         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
893          * about the value of skb->pkt_type. */
894         if (skb->pkt_type == PACKET_OTHERHOST) {
895                 skb->pkt_type = PACKET_HOST;
896                 nf_bridge->mask |= BRNF_PKT_TYPE;
897         }
898
899         nf_bridge_pull_encap_header(skb);
900         nf_bridge_save_header(skb);
901         if (pf == NFPROTO_IPV4)
902                 skb->protocol = htons(ETH_P_IP);
903         else
904                 skb->protocol = htons(ETH_P_IPV6);
905
906         NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
907                 br_nf_dev_queue_xmit);
908
909         return NF_STOLEN;
910 }
911
912 /* IP/SABOTAGE *****************************************************/
913 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
914  * for the second time. */
915 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
916                                    const struct net_device *in,
917                                    const struct net_device *out,
918                                    int (*okfn)(struct sk_buff *))
919 {
920         if (skb->nf_bridge &&
921             !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
922                 return NF_STOP;
923         }
924
925         return NF_ACCEPT;
926 }
927
928 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
929  * br_dev_queue_push_xmit is called afterwards */
930 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
931         {
932                 .hook = br_nf_pre_routing,
933                 .owner = THIS_MODULE,
934                 .pf = NFPROTO_BRIDGE,
935                 .hooknum = NF_BR_PRE_ROUTING,
936                 .priority = NF_BR_PRI_BRNF,
937         },
938         {
939                 .hook = br_nf_local_in,
940                 .owner = THIS_MODULE,
941                 .pf = NFPROTO_BRIDGE,
942                 .hooknum = NF_BR_LOCAL_IN,
943                 .priority = NF_BR_PRI_BRNF,
944         },
945         {
946                 .hook = br_nf_forward_ip,
947                 .owner = THIS_MODULE,
948                 .pf = NFPROTO_BRIDGE,
949                 .hooknum = NF_BR_FORWARD,
950                 .priority = NF_BR_PRI_BRNF - 1,
951         },
952         {
953                 .hook = br_nf_forward_arp,
954                 .owner = THIS_MODULE,
955                 .pf = NFPROTO_BRIDGE,
956                 .hooknum = NF_BR_FORWARD,
957                 .priority = NF_BR_PRI_BRNF,
958         },
959         {
960                 .hook = br_nf_post_routing,
961                 .owner = THIS_MODULE,
962                 .pf = NFPROTO_BRIDGE,
963                 .hooknum = NF_BR_POST_ROUTING,
964                 .priority = NF_BR_PRI_LAST,
965         },
966         {
967                 .hook = ip_sabotage_in,
968                 .owner = THIS_MODULE,
969                 .pf = NFPROTO_IPV4,
970                 .hooknum = NF_INET_PRE_ROUTING,
971                 .priority = NF_IP_PRI_FIRST,
972         },
973         {
974                 .hook = ip_sabotage_in,
975                 .owner = THIS_MODULE,
976                 .pf = NFPROTO_IPV6,
977                 .hooknum = NF_INET_PRE_ROUTING,
978                 .priority = NF_IP6_PRI_FIRST,
979         },
980 };
981
982 #ifdef CONFIG_SYSCTL
983 static
984 int brnf_sysctl_call_tables(ctl_table * ctl, int write,
985                             void __user * buffer, size_t * lenp, loff_t * ppos)
986 {
987         int ret;
988
989         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
990
991         if (write && *(int *)(ctl->data))
992                 *(int *)(ctl->data) = 1;
993         return ret;
994 }
995
996 static ctl_table brnf_table[] = {
997         {
998                 .procname       = "bridge-nf-call-arptables",
999                 .data           = &brnf_call_arptables,
1000                 .maxlen         = sizeof(int),
1001                 .mode           = 0644,
1002                 .proc_handler   = brnf_sysctl_call_tables,
1003         },
1004         {
1005                 .procname       = "bridge-nf-call-iptables",
1006                 .data           = &brnf_call_iptables,
1007                 .maxlen         = sizeof(int),
1008                 .mode           = 0644,
1009                 .proc_handler   = brnf_sysctl_call_tables,
1010         },
1011         {
1012                 .procname       = "bridge-nf-call-ip6tables",
1013                 .data           = &brnf_call_ip6tables,
1014                 .maxlen         = sizeof(int),
1015                 .mode           = 0644,
1016                 .proc_handler   = brnf_sysctl_call_tables,
1017         },
1018         {
1019                 .procname       = "bridge-nf-filter-vlan-tagged",
1020                 .data           = &brnf_filter_vlan_tagged,
1021                 .maxlen         = sizeof(int),
1022                 .mode           = 0644,
1023                 .proc_handler   = brnf_sysctl_call_tables,
1024         },
1025         {
1026                 .procname       = "bridge-nf-filter-pppoe-tagged",
1027                 .data           = &brnf_filter_pppoe_tagged,
1028                 .maxlen         = sizeof(int),
1029                 .mode           = 0644,
1030                 .proc_handler   = brnf_sysctl_call_tables,
1031         },
1032         {
1033                 .procname       = "bridge-nf-pass-vlan-input-dev",
1034                 .data           = &brnf_pass_vlan_indev,
1035                 .maxlen         = sizeof(int),
1036                 .mode           = 0644,
1037                 .proc_handler   = brnf_sysctl_call_tables,
1038         },
1039         { }
1040 };
1041 #endif
1042
1043 int __init br_netfilter_init(void)
1044 {
1045         int ret;
1046
1047         ret = dst_entries_init(&fake_dst_ops);
1048         if (ret < 0)
1049                 return ret;
1050
1051         ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1052         if (ret < 0) {
1053                 dst_entries_destroy(&fake_dst_ops);
1054                 return ret;
1055         }
1056 #ifdef CONFIG_SYSCTL
1057         brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1058         if (brnf_sysctl_header == NULL) {
1059                 printk(KERN_WARNING
1060                        "br_netfilter: can't register to sysctl.\n");
1061                 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1062                 dst_entries_destroy(&fake_dst_ops);
1063                 return -ENOMEM;
1064         }
1065 #endif
1066         printk(KERN_NOTICE "Bridge firewalling registered\n");
1067         return 0;
1068 }
1069
1070 void br_netfilter_fini(void)
1071 {
1072         nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1073 #ifdef CONFIG_SYSCTL
1074         unregister_net_sysctl_table(brnf_sysctl_header);
1075 #endif
1076         dst_entries_destroy(&fake_dst_ops);
1077 }