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