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