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