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