net: Add optional SKB arg to dst_ops->neigh_lookup().
[firefly-linux-kernel-4.4.55.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_get_neighbour_noref(dst);
379         if (neigh->hh.hh_len) {
380                 neigh_hh_bridge(&neigh->hh, skb);
381                 skb->dev = nf_bridge->physindev;
382                 return br_handle_frame_finish(skb);
383         } else {
384                 /* the neighbour function below overwrites the complete
385                  * MAC header, so we save the Ethernet source address and
386                  * protocol number. */
387                 skb_copy_from_linear_data_offset(skb, -(ETH_HLEN-ETH_ALEN), skb->nf_bridge->data, ETH_HLEN-ETH_ALEN);
388                 /* tell br_dev_xmit to continue with forwarding */
389                 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
390                 return neigh->output(neigh, skb);
391         }
392 free_skb:
393         kfree_skb(skb);
394         return 0;
395 }
396
397 /* This requires some explaining. If DNAT has taken place,
398  * we will need to fix up the destination Ethernet address.
399  *
400  * There are two cases to consider:
401  * 1. The packet was DNAT'ed to a device in the same bridge
402  *    port group as it was received on. We can still bridge
403  *    the packet.
404  * 2. The packet was DNAT'ed to a different device, either
405  *    a non-bridged device or another bridge port group.
406  *    The packet will need to be routed.
407  *
408  * The correct way of distinguishing between these two cases is to
409  * call ip_route_input() and to look at skb->dst->dev, which is
410  * changed to the destination device if ip_route_input() succeeds.
411  *
412  * Let's first consider the case that ip_route_input() succeeds:
413  *
414  * If the output device equals the logical bridge device the packet
415  * came in on, we can consider this bridging. The corresponding MAC
416  * address will be obtained in br_nf_pre_routing_finish_bridge.
417  * Otherwise, the packet is considered to be routed and we just
418  * change the destination MAC address so that the packet will
419  * later be passed up to the IP stack to be routed. For a redirected
420  * packet, ip_route_input() will give back the localhost as output device,
421  * which differs from the bridge device.
422  *
423  * Let's now consider the case that ip_route_input() fails:
424  *
425  * This can be because the destination address is martian, in which case
426  * the packet will be dropped.
427  * If IP forwarding is disabled, ip_route_input() will fail, while
428  * ip_route_output_key() can return success. The source
429  * address for ip_route_output_key() is set to zero, so ip_route_output_key()
430  * thinks we're handling a locally generated packet and won't care
431  * if IP forwarding is enabled. If the output device equals the logical bridge
432  * device, we proceed as if ip_route_input() succeeded. If it differs from the
433  * logical bridge port or if ip_route_output_key() fails we drop the packet.
434  */
435 static int br_nf_pre_routing_finish(struct sk_buff *skb)
436 {
437         struct net_device *dev = skb->dev;
438         struct iphdr *iph = ip_hdr(skb);
439         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
440         struct rtable *rt;
441         int err;
442
443         if (nf_bridge->mask & BRNF_PKT_TYPE) {
444                 skb->pkt_type = PACKET_OTHERHOST;
445                 nf_bridge->mask ^= BRNF_PKT_TYPE;
446         }
447         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
448         if (dnat_took_place(skb)) {
449                 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
450                         struct in_device *in_dev = __in_dev_get_rcu(dev);
451
452                         /* If err equals -EHOSTUNREACH the error is due to a
453                          * martian destination or due to the fact that
454                          * forwarding is disabled. For most martian packets,
455                          * ip_route_output_key() will fail. It won't fail for 2 types of
456                          * martian destinations: loopback destinations and destination
457                          * 0.0.0.0. In both cases the packet will be dropped because the
458                          * destination is the loopback device and not the bridge. */
459                         if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
460                                 goto free_skb;
461
462                         rt = ip_route_output(dev_net(dev), iph->daddr, 0,
463                                              RT_TOS(iph->tos), 0);
464                         if (!IS_ERR(rt)) {
465                                 /* - Bridged-and-DNAT'ed traffic doesn't
466                                  *   require ip_forwarding. */
467                                 if (rt->dst.dev == dev) {
468                                         skb_dst_set(skb, &rt->dst);
469                                         goto bridged_dnat;
470                                 }
471                                 ip_rt_put(rt);
472                         }
473 free_skb:
474                         kfree_skb(skb);
475                         return 0;
476                 } else {
477                         if (skb_dst(skb)->dev == dev) {
478 bridged_dnat:
479                                 skb->dev = nf_bridge->physindev;
480                                 nf_bridge_update_protocol(skb);
481                                 nf_bridge_push_encap_header(skb);
482                                 NF_HOOK_THRESH(NFPROTO_BRIDGE,
483                                                NF_BR_PRE_ROUTING,
484                                                skb, skb->dev, NULL,
485                                                br_nf_pre_routing_finish_bridge,
486                                                1);
487                                 return 0;
488                         }
489                         memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
490                         skb->pkt_type = PACKET_HOST;
491                 }
492         } else {
493                 rt = bridge_parent_rtable(nf_bridge->physindev);
494                 if (!rt) {
495                         kfree_skb(skb);
496                         return 0;
497                 }
498                 skb_dst_set_noref(skb, &rt->dst);
499         }
500
501         skb->dev = nf_bridge->physindev;
502         nf_bridge_update_protocol(skb);
503         nf_bridge_push_encap_header(skb);
504         NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
505                        br_handle_frame_finish, 1);
506
507         return 0;
508 }
509
510 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
511 {
512         struct net_device *vlan, *br;
513
514         br = bridge_parent(dev);
515         if (brnf_pass_vlan_indev == 0 || !vlan_tx_tag_present(skb))
516                 return br;
517
518         vlan = __vlan_find_dev_deep(br, vlan_tx_tag_get(skb) & VLAN_VID_MASK);
519
520         return vlan ? vlan : br;
521 }
522
523 /* Some common code for IPv4/IPv6 */
524 static struct net_device *setup_pre_routing(struct sk_buff *skb)
525 {
526         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
527
528         if (skb->pkt_type == PACKET_OTHERHOST) {
529                 skb->pkt_type = PACKET_HOST;
530                 nf_bridge->mask |= BRNF_PKT_TYPE;
531         }
532
533         nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
534         nf_bridge->physindev = skb->dev;
535         skb->dev = brnf_get_logical_dev(skb, skb->dev);
536         if (skb->protocol == htons(ETH_P_8021Q))
537                 nf_bridge->mask |= BRNF_8021Q;
538         else if (skb->protocol == htons(ETH_P_PPP_SES))
539                 nf_bridge->mask |= BRNF_PPPoE;
540
541         return skb->dev;
542 }
543
544 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
545 static int check_hbh_len(struct sk_buff *skb)
546 {
547         unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
548         u32 pkt_len;
549         const unsigned char *nh = skb_network_header(skb);
550         int off = raw - nh;
551         int len = (raw[1] + 1) << 3;
552
553         if ((raw + len) - skb->data > skb_headlen(skb))
554                 goto bad;
555
556         off += 2;
557         len -= 2;
558
559         while (len > 0) {
560                 int optlen = nh[off + 1] + 2;
561
562                 switch (nh[off]) {
563                 case IPV6_TLV_PAD1:
564                         optlen = 1;
565                         break;
566
567                 case IPV6_TLV_PADN:
568                         break;
569
570                 case IPV6_TLV_JUMBO:
571                         if (nh[off + 1] != 4 || (off & 3) != 2)
572                                 goto bad;
573                         pkt_len = ntohl(*(__be32 *) (nh + off + 2));
574                         if (pkt_len <= IPV6_MAXPLEN ||
575                             ipv6_hdr(skb)->payload_len)
576                                 goto bad;
577                         if (pkt_len > skb->len - sizeof(struct ipv6hdr))
578                                 goto bad;
579                         if (pskb_trim_rcsum(skb,
580                                             pkt_len + sizeof(struct ipv6hdr)))
581                                 goto bad;
582                         nh = skb_network_header(skb);
583                         break;
584                 default:
585                         if (optlen > len)
586                                 goto bad;
587                         break;
588                 }
589                 off += optlen;
590                 len -= optlen;
591         }
592         if (len == 0)
593                 return 0;
594 bad:
595         return -1;
596
597 }
598
599 /* Replicate the checks that IPv6 does on packet reception and pass the packet
600  * to ip6tables, which doesn't support NAT, so things are fairly simple. */
601 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
602                                            struct sk_buff *skb,
603                                            const struct net_device *in,
604                                            const struct net_device *out,
605                                            int (*okfn)(struct sk_buff *))
606 {
607         const struct ipv6hdr *hdr;
608         u32 pkt_len;
609
610         if (skb->len < sizeof(struct ipv6hdr))
611                 return NF_DROP;
612
613         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
614                 return NF_DROP;
615
616         hdr = ipv6_hdr(skb);
617
618         if (hdr->version != 6)
619                 return NF_DROP;
620
621         pkt_len = ntohs(hdr->payload_len);
622
623         if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
624                 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
625                         return NF_DROP;
626                 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
627                         return NF_DROP;
628         }
629         if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
630                 return NF_DROP;
631
632         nf_bridge_put(skb->nf_bridge);
633         if (!nf_bridge_alloc(skb))
634                 return NF_DROP;
635         if (!setup_pre_routing(skb))
636                 return NF_DROP;
637
638         skb->protocol = htons(ETH_P_IPV6);
639         NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
640                 br_nf_pre_routing_finish_ipv6);
641
642         return NF_STOLEN;
643 }
644
645 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
646  * Replicate the checks that IPv4 does on packet reception.
647  * Set skb->dev to the bridge device (i.e. parent of the
648  * receiving device) to make netfilter happy, the REDIRECT
649  * target in particular.  Save the original destination IP
650  * address to be able to detect DNAT afterwards. */
651 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
652                                       const struct net_device *in,
653                                       const struct net_device *out,
654                                       int (*okfn)(struct sk_buff *))
655 {
656         struct net_bridge_port *p;
657         struct net_bridge *br;
658         __u32 len = nf_bridge_encap_header_len(skb);
659
660         if (unlikely(!pskb_may_pull(skb, len)))
661                 return NF_DROP;
662
663         p = br_port_get_rcu(in);
664         if (p == NULL)
665                 return NF_DROP;
666         br = p->br;
667
668         if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
669                 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
670                         return NF_ACCEPT;
671
672                 nf_bridge_pull_encap_header_rcsum(skb);
673                 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
674         }
675
676         if (!brnf_call_iptables && !br->nf_call_iptables)
677                 return NF_ACCEPT;
678
679         if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
680                 return NF_ACCEPT;
681
682         nf_bridge_pull_encap_header_rcsum(skb);
683
684         if (br_parse_ip_options(skb))
685                 return NF_DROP;
686
687         nf_bridge_put(skb->nf_bridge);
688         if (!nf_bridge_alloc(skb))
689                 return NF_DROP;
690         if (!setup_pre_routing(skb))
691                 return NF_DROP;
692         store_orig_dstaddr(skb);
693         skb->protocol = htons(ETH_P_IP);
694
695         NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
696                 br_nf_pre_routing_finish);
697
698         return NF_STOLEN;
699 }
700
701
702 /* PF_BRIDGE/LOCAL_IN ************************************************/
703 /* The packet is locally destined, which requires a real
704  * dst_entry, so detach the fake one.  On the way up, the
705  * packet would pass through PRE_ROUTING again (which already
706  * took place when the packet entered the bridge), but we
707  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
708  * prevent this from happening. */
709 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
710                                    const struct net_device *in,
711                                    const struct net_device *out,
712                                    int (*okfn)(struct sk_buff *))
713 {
714         br_drop_fake_rtable(skb);
715         return NF_ACCEPT;
716 }
717
718 /* PF_BRIDGE/FORWARD *************************************************/
719 static int br_nf_forward_finish(struct sk_buff *skb)
720 {
721         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
722         struct net_device *in;
723
724         if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
725                 in = nf_bridge->physindev;
726                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
727                         skb->pkt_type = PACKET_OTHERHOST;
728                         nf_bridge->mask ^= BRNF_PKT_TYPE;
729                 }
730                 nf_bridge_update_protocol(skb);
731         } else {
732                 in = *((struct net_device **)(skb->cb));
733         }
734         nf_bridge_push_encap_header(skb);
735
736         NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
737                        skb->dev, br_forward_finish, 1);
738         return 0;
739 }
740
741
742 /* This is the 'purely bridged' case.  For IP, we pass the packet to
743  * netfilter with indev and outdev set to the bridge device,
744  * but we are still able to filter on the 'real' indev/outdev
745  * because of the physdev module. For ARP, indev and outdev are the
746  * bridge ports. */
747 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
748                                      const struct net_device *in,
749                                      const struct net_device *out,
750                                      int (*okfn)(struct sk_buff *))
751 {
752         struct nf_bridge_info *nf_bridge;
753         struct net_device *parent;
754         u_int8_t pf;
755
756         if (!skb->nf_bridge)
757                 return NF_ACCEPT;
758
759         /* Need exclusive nf_bridge_info since we might have multiple
760          * different physoutdevs. */
761         if (!nf_bridge_unshare(skb))
762                 return NF_DROP;
763
764         parent = bridge_parent(out);
765         if (!parent)
766                 return NF_DROP;
767
768         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
769                 pf = NFPROTO_IPV4;
770         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
771                 pf = NFPROTO_IPV6;
772         else
773                 return NF_ACCEPT;
774
775         nf_bridge_pull_encap_header(skb);
776
777         nf_bridge = skb->nf_bridge;
778         if (skb->pkt_type == PACKET_OTHERHOST) {
779                 skb->pkt_type = PACKET_HOST;
780                 nf_bridge->mask |= BRNF_PKT_TYPE;
781         }
782
783         if (pf == NFPROTO_IPV4 && br_parse_ip_options(skb))
784                 return NF_DROP;
785
786         /* The physdev module checks on this */
787         nf_bridge->mask |= BRNF_BRIDGED;
788         nf_bridge->physoutdev = skb->dev;
789         if (pf == NFPROTO_IPV4)
790                 skb->protocol = htons(ETH_P_IP);
791         else
792                 skb->protocol = htons(ETH_P_IPV6);
793
794         NF_HOOK(pf, NF_INET_FORWARD, skb, brnf_get_logical_dev(skb, in), parent,
795                 br_nf_forward_finish);
796
797         return NF_STOLEN;
798 }
799
800 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
801                                       const struct net_device *in,
802                                       const struct net_device *out,
803                                       int (*okfn)(struct sk_buff *))
804 {
805         struct net_bridge_port *p;
806         struct net_bridge *br;
807         struct net_device **d = (struct net_device **)(skb->cb);
808
809         p = br_port_get_rcu(out);
810         if (p == NULL)
811                 return NF_ACCEPT;
812         br = p->br;
813
814         if (!brnf_call_arptables && !br->nf_call_arptables)
815                 return NF_ACCEPT;
816
817         if (!IS_ARP(skb)) {
818                 if (!IS_VLAN_ARP(skb))
819                         return NF_ACCEPT;
820                 nf_bridge_pull_encap_header(skb);
821         }
822
823         if (arp_hdr(skb)->ar_pln != 4) {
824                 if (IS_VLAN_ARP(skb))
825                         nf_bridge_push_encap_header(skb);
826                 return NF_ACCEPT;
827         }
828         *d = (struct net_device *)in;
829         NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
830                 (struct net_device *)out, br_nf_forward_finish);
831
832         return NF_STOLEN;
833 }
834
835 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV4)
836 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
837 {
838         int ret;
839
840         if (skb->nfct != NULL && skb->protocol == htons(ETH_P_IP) &&
841             skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
842             !skb_is_gso(skb)) {
843                 if (br_parse_ip_options(skb))
844                         /* Drop invalid packet */
845                         return NF_DROP;
846                 ret = ip_fragment(skb, br_dev_queue_push_xmit);
847         } else
848                 ret = br_dev_queue_push_xmit(skb);
849
850         return ret;
851 }
852 #else
853 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
854 {
855         return br_dev_queue_push_xmit(skb);
856 }
857 #endif
858
859 /* PF_BRIDGE/POST_ROUTING ********************************************/
860 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
861                                        const struct net_device *in,
862                                        const struct net_device *out,
863                                        int (*okfn)(struct sk_buff *))
864 {
865         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
866         struct net_device *realoutdev = bridge_parent(skb->dev);
867         u_int8_t pf;
868
869         if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
870                 return NF_ACCEPT;
871
872         if (!realoutdev)
873                 return NF_DROP;
874
875         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
876                 pf = NFPROTO_IPV4;
877         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
878                 pf = NFPROTO_IPV6;
879         else
880                 return NF_ACCEPT;
881
882         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
883          * about the value of skb->pkt_type. */
884         if (skb->pkt_type == PACKET_OTHERHOST) {
885                 skb->pkt_type = PACKET_HOST;
886                 nf_bridge->mask |= BRNF_PKT_TYPE;
887         }
888
889         nf_bridge_pull_encap_header(skb);
890         nf_bridge_save_header(skb);
891         if (pf == NFPROTO_IPV4)
892                 skb->protocol = htons(ETH_P_IP);
893         else
894                 skb->protocol = htons(ETH_P_IPV6);
895
896         NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
897                 br_nf_dev_queue_xmit);
898
899         return NF_STOLEN;
900 }
901
902 /* IP/SABOTAGE *****************************************************/
903 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
904  * for the second time. */
905 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
906                                    const struct net_device *in,
907                                    const struct net_device *out,
908                                    int (*okfn)(struct sk_buff *))
909 {
910         if (skb->nf_bridge &&
911             !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
912                 return NF_STOP;
913         }
914
915         return NF_ACCEPT;
916 }
917
918 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
919  * br_dev_queue_push_xmit is called afterwards */
920 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
921         {
922                 .hook = br_nf_pre_routing,
923                 .owner = THIS_MODULE,
924                 .pf = NFPROTO_BRIDGE,
925                 .hooknum = NF_BR_PRE_ROUTING,
926                 .priority = NF_BR_PRI_BRNF,
927         },
928         {
929                 .hook = br_nf_local_in,
930                 .owner = THIS_MODULE,
931                 .pf = NFPROTO_BRIDGE,
932                 .hooknum = NF_BR_LOCAL_IN,
933                 .priority = NF_BR_PRI_BRNF,
934         },
935         {
936                 .hook = br_nf_forward_ip,
937                 .owner = THIS_MODULE,
938                 .pf = NFPROTO_BRIDGE,
939                 .hooknum = NF_BR_FORWARD,
940                 .priority = NF_BR_PRI_BRNF - 1,
941         },
942         {
943                 .hook = br_nf_forward_arp,
944                 .owner = THIS_MODULE,
945                 .pf = NFPROTO_BRIDGE,
946                 .hooknum = NF_BR_FORWARD,
947                 .priority = NF_BR_PRI_BRNF,
948         },
949         {
950                 .hook = br_nf_post_routing,
951                 .owner = THIS_MODULE,
952                 .pf = NFPROTO_BRIDGE,
953                 .hooknum = NF_BR_POST_ROUTING,
954                 .priority = NF_BR_PRI_LAST,
955         },
956         {
957                 .hook = ip_sabotage_in,
958                 .owner = THIS_MODULE,
959                 .pf = NFPROTO_IPV4,
960                 .hooknum = NF_INET_PRE_ROUTING,
961                 .priority = NF_IP_PRI_FIRST,
962         },
963         {
964                 .hook = ip_sabotage_in,
965                 .owner = THIS_MODULE,
966                 .pf = NFPROTO_IPV6,
967                 .hooknum = NF_INET_PRE_ROUTING,
968                 .priority = NF_IP6_PRI_FIRST,
969         },
970 };
971
972 #ifdef CONFIG_SYSCTL
973 static
974 int brnf_sysctl_call_tables(ctl_table * ctl, int write,
975                             void __user * buffer, size_t * lenp, loff_t * ppos)
976 {
977         int ret;
978
979         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
980
981         if (write && *(int *)(ctl->data))
982                 *(int *)(ctl->data) = 1;
983         return ret;
984 }
985
986 static ctl_table brnf_table[] = {
987         {
988                 .procname       = "bridge-nf-call-arptables",
989                 .data           = &brnf_call_arptables,
990                 .maxlen         = sizeof(int),
991                 .mode           = 0644,
992                 .proc_handler   = brnf_sysctl_call_tables,
993         },
994         {
995                 .procname       = "bridge-nf-call-iptables",
996                 .data           = &brnf_call_iptables,
997                 .maxlen         = sizeof(int),
998                 .mode           = 0644,
999                 .proc_handler   = brnf_sysctl_call_tables,
1000         },
1001         {
1002                 .procname       = "bridge-nf-call-ip6tables",
1003                 .data           = &brnf_call_ip6tables,
1004                 .maxlen         = sizeof(int),
1005                 .mode           = 0644,
1006                 .proc_handler   = brnf_sysctl_call_tables,
1007         },
1008         {
1009                 .procname       = "bridge-nf-filter-vlan-tagged",
1010                 .data           = &brnf_filter_vlan_tagged,
1011                 .maxlen         = sizeof(int),
1012                 .mode           = 0644,
1013                 .proc_handler   = brnf_sysctl_call_tables,
1014         },
1015         {
1016                 .procname       = "bridge-nf-filter-pppoe-tagged",
1017                 .data           = &brnf_filter_pppoe_tagged,
1018                 .maxlen         = sizeof(int),
1019                 .mode           = 0644,
1020                 .proc_handler   = brnf_sysctl_call_tables,
1021         },
1022         {
1023                 .procname       = "bridge-nf-pass-vlan-input-dev",
1024                 .data           = &brnf_pass_vlan_indev,
1025                 .maxlen         = sizeof(int),
1026                 .mode           = 0644,
1027                 .proc_handler   = brnf_sysctl_call_tables,
1028         },
1029         { }
1030 };
1031 #endif
1032
1033 int __init br_netfilter_init(void)
1034 {
1035         int ret;
1036
1037         ret = dst_entries_init(&fake_dst_ops);
1038         if (ret < 0)
1039                 return ret;
1040
1041         ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1042         if (ret < 0) {
1043                 dst_entries_destroy(&fake_dst_ops);
1044                 return ret;
1045         }
1046 #ifdef CONFIG_SYSCTL
1047         brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1048         if (brnf_sysctl_header == NULL) {
1049                 printk(KERN_WARNING
1050                        "br_netfilter: can't register to sysctl.\n");
1051                 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1052                 dst_entries_destroy(&fake_dst_ops);
1053                 return -ENOMEM;
1054         }
1055 #endif
1056         printk(KERN_NOTICE "Bridge firewalling registered\n");
1057         return 0;
1058 }
1059
1060 void br_netfilter_fini(void)
1061 {
1062         nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1063 #ifdef CONFIG_SYSCTL
1064         unregister_net_sysctl_table(brnf_sysctl_header);
1065 #endif
1066         dst_entries_destroy(&fake_dst_ops);
1067 }