]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bridge/br_netfilter_hooks.c
netfilter: remove hook owner refcounting
[karo-tx-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 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/LOCAL_IN ************************************************/
520 /* The packet is locally destined, which requires a real
521  * dst_entry, so detach the fake one.  On the way up, the
522  * packet would pass through PRE_ROUTING again (which already
523  * took place when the packet entered the bridge), but we
524  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
525  * prevent this from happening. */
526 static unsigned int br_nf_local_in(void *priv,
527                                    struct sk_buff *skb,
528                                    const struct nf_hook_state *state)
529 {
530         br_drop_fake_rtable(skb);
531         return NF_ACCEPT;
532 }
533
534 /* PF_BRIDGE/FORWARD *************************************************/
535 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
536 {
537         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
538         struct net_device *in;
539
540         if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
541
542                 if (skb->protocol == htons(ETH_P_IP))
543                         nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
544
545                 if (skb->protocol == htons(ETH_P_IPV6))
546                         nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
547
548                 in = nf_bridge->physindev;
549                 if (nf_bridge->pkt_otherhost) {
550                         skb->pkt_type = PACKET_OTHERHOST;
551                         nf_bridge->pkt_otherhost = false;
552                 }
553                 nf_bridge_update_protocol(skb);
554         } else {
555                 in = *((struct net_device **)(skb->cb));
556         }
557         nf_bridge_push_encap_header(skb);
558
559         NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, net, sk, skb,
560                        in, skb->dev, br_forward_finish, 1);
561         return 0;
562 }
563
564
565 /* This is the 'purely bridged' case.  For IP, we pass the packet to
566  * netfilter with indev and outdev set to the bridge device,
567  * but we are still able to filter on the 'real' indev/outdev
568  * because of the physdev module. For ARP, indev and outdev are the
569  * bridge ports. */
570 static unsigned int br_nf_forward_ip(void *priv,
571                                      struct sk_buff *skb,
572                                      const struct nf_hook_state *state)
573 {
574         struct nf_bridge_info *nf_bridge;
575         struct net_device *parent;
576         u_int8_t pf;
577
578         if (!skb->nf_bridge)
579                 return NF_ACCEPT;
580
581         /* Need exclusive nf_bridge_info since we might have multiple
582          * different physoutdevs. */
583         if (!nf_bridge_unshare(skb))
584                 return NF_DROP;
585
586         nf_bridge = nf_bridge_info_get(skb);
587         if (!nf_bridge)
588                 return NF_DROP;
589
590         parent = bridge_parent(state->out);
591         if (!parent)
592                 return NF_DROP;
593
594         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
595                 pf = NFPROTO_IPV4;
596         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
597                 pf = NFPROTO_IPV6;
598         else
599                 return NF_ACCEPT;
600
601         nf_bridge_pull_encap_header(skb);
602
603         if (skb->pkt_type == PACKET_OTHERHOST) {
604                 skb->pkt_type = PACKET_HOST;
605                 nf_bridge->pkt_otherhost = true;
606         }
607
608         if (pf == NFPROTO_IPV4) {
609                 if (br_validate_ipv4(state->net, skb))
610                         return NF_DROP;
611                 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
612         }
613
614         if (pf == NFPROTO_IPV6) {
615                 if (br_validate_ipv6(state->net, skb))
616                         return NF_DROP;
617                 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
618         }
619
620         nf_bridge->physoutdev = skb->dev;
621         if (pf == NFPROTO_IPV4)
622                 skb->protocol = htons(ETH_P_IP);
623         else
624                 skb->protocol = htons(ETH_P_IPV6);
625
626         NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
627                 brnf_get_logical_dev(skb, state->in),
628                 parent, br_nf_forward_finish);
629
630         return NF_STOLEN;
631 }
632
633 static unsigned int br_nf_forward_arp(void *priv,
634                                       struct sk_buff *skb,
635                                       const struct nf_hook_state *state)
636 {
637         struct net_bridge_port *p;
638         struct net_bridge *br;
639         struct net_device **d = (struct net_device **)(skb->cb);
640
641         p = br_port_get_rcu(state->out);
642         if (p == NULL)
643                 return NF_ACCEPT;
644         br = p->br;
645
646         if (!brnf_call_arptables && !br->nf_call_arptables)
647                 return NF_ACCEPT;
648
649         if (!IS_ARP(skb)) {
650                 if (!IS_VLAN_ARP(skb))
651                         return NF_ACCEPT;
652                 nf_bridge_pull_encap_header(skb);
653         }
654
655         if (arp_hdr(skb)->ar_pln != 4) {
656                 if (IS_VLAN_ARP(skb))
657                         nf_bridge_push_encap_header(skb);
658                 return NF_ACCEPT;
659         }
660         *d = state->in;
661         NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
662                 state->in, state->out, br_nf_forward_finish);
663
664         return NF_STOLEN;
665 }
666
667 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
668 {
669         struct brnf_frag_data *data;
670         int err;
671
672         data = this_cpu_ptr(&brnf_frag_data_storage);
673         err = skb_cow_head(skb, data->size);
674
675         if (err) {
676                 kfree_skb(skb);
677                 return 0;
678         }
679
680         if (data->vlan_tci) {
681                 skb->vlan_tci = data->vlan_tci;
682                 skb->vlan_proto = data->vlan_proto;
683         }
684
685         skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
686         __skb_push(skb, data->encap_size);
687
688         nf_bridge_info_free(skb);
689         return br_dev_queue_push_xmit(net, sk, skb);
690 }
691 static int br_nf_push_frag_xmit_sk(struct sock *sk, struct sk_buff *skb)
692 {
693         struct net *net = dev_net(skb_dst(skb)->dev);
694         return br_nf_push_frag_xmit(net, sk, skb);
695 }
696
697 static int
698 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
699                   int (*output)(struct sock *, struct sk_buff *))
700 {
701         unsigned int mtu = ip_skb_dst_mtu(skb);
702         struct iphdr *iph = ip_hdr(skb);
703
704         if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
705                      (IPCB(skb)->frag_max_size &&
706                       IPCB(skb)->frag_max_size > mtu))) {
707                 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
708                 kfree_skb(skb);
709                 return -EMSGSIZE;
710         }
711
712         return ip_do_fragment(sk, skb, output);
713 }
714
715 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
716 {
717         if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
718                 return PPPOE_SES_HLEN;
719         return 0;
720 }
721
722 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
723 {
724         struct nf_bridge_info *nf_bridge;
725         unsigned int mtu_reserved;
726
727         mtu_reserved = nf_bridge_mtu_reduction(skb);
728
729         if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
730                 nf_bridge_info_free(skb);
731                 return br_dev_queue_push_xmit(net, sk, skb);
732         }
733
734         nf_bridge = nf_bridge_info_get(skb);
735
736         /* This is wrong! We should preserve the original fragment
737          * boundaries by preserving frag_list rather than refragmenting.
738          */
739         if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
740             skb->protocol == htons(ETH_P_IP)) {
741                 struct brnf_frag_data *data;
742
743                 if (br_validate_ipv4(net, skb))
744                         goto drop;
745
746                 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
747
748                 nf_bridge_update_protocol(skb);
749
750                 data = this_cpu_ptr(&brnf_frag_data_storage);
751
752                 data->vlan_tci = skb->vlan_tci;
753                 data->vlan_proto = skb->vlan_proto;
754                 data->encap_size = nf_bridge_encap_header_len(skb);
755                 data->size = ETH_HLEN + data->encap_size;
756
757                 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
758                                                  data->size);
759
760                 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit_sk);
761         }
762         if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
763             skb->protocol == htons(ETH_P_IPV6)) {
764                 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
765                 struct brnf_frag_data *data;
766
767                 if (br_validate_ipv6(net, skb))
768                         goto drop;
769
770                 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
771
772                 nf_bridge_update_protocol(skb);
773
774                 data = this_cpu_ptr(&brnf_frag_data_storage);
775                 data->encap_size = nf_bridge_encap_header_len(skb);
776                 data->size = ETH_HLEN + data->encap_size;
777
778                 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
779                                                  data->size);
780
781                 if (v6ops)
782                         return v6ops->fragment(sk, skb, br_nf_push_frag_xmit_sk);
783
784                 kfree_skb(skb);
785                 return -EMSGSIZE;
786         }
787         nf_bridge_info_free(skb);
788         return br_dev_queue_push_xmit(net, sk, skb);
789  drop:
790         kfree_skb(skb);
791         return 0;
792 }
793
794 /* PF_BRIDGE/POST_ROUTING ********************************************/
795 static unsigned int br_nf_post_routing(void *priv,
796                                        struct sk_buff *skb,
797                                        const struct nf_hook_state *state)
798 {
799         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
800         struct net_device *realoutdev = bridge_parent(skb->dev);
801         u_int8_t pf;
802
803         /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
804          * on a bridge, but was delivered locally and is now being routed:
805          *
806          * POST_ROUTING was already invoked from the ip stack.
807          */
808         if (!nf_bridge || !nf_bridge->physoutdev)
809                 return NF_ACCEPT;
810
811         if (!realoutdev)
812                 return NF_DROP;
813
814         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
815                 pf = NFPROTO_IPV4;
816         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
817                 pf = NFPROTO_IPV6;
818         else
819                 return NF_ACCEPT;
820
821         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
822          * about the value of skb->pkt_type. */
823         if (skb->pkt_type == PACKET_OTHERHOST) {
824                 skb->pkt_type = PACKET_HOST;
825                 nf_bridge->pkt_otherhost = true;
826         }
827
828         nf_bridge_pull_encap_header(skb);
829         if (pf == NFPROTO_IPV4)
830                 skb->protocol = htons(ETH_P_IP);
831         else
832                 skb->protocol = htons(ETH_P_IPV6);
833
834         NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
835                 NULL, realoutdev,
836                 br_nf_dev_queue_xmit);
837
838         return NF_STOLEN;
839 }
840
841 /* IP/SABOTAGE *****************************************************/
842 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
843  * for the second time. */
844 static unsigned int ip_sabotage_in(void *priv,
845                                    struct sk_buff *skb,
846                                    const struct nf_hook_state *state)
847 {
848         if (skb->nf_bridge && !skb->nf_bridge->in_prerouting)
849                 return NF_STOP;
850
851         return NF_ACCEPT;
852 }
853
854 /* This is called when br_netfilter has called into iptables/netfilter,
855  * and DNAT has taken place on a bridge-forwarded packet.
856  *
857  * neigh->output has created a new MAC header, with local br0 MAC
858  * as saddr.
859  *
860  * This restores the original MAC saddr of the bridged packet
861  * before invoking bridge forward logic to transmit the packet.
862  */
863 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
864 {
865         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
866
867         skb_pull(skb, ETH_HLEN);
868         nf_bridge->bridged_dnat = 0;
869
870         BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
871
872         skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
873                                        nf_bridge->neigh_header,
874                                        ETH_HLEN - ETH_ALEN);
875         skb->dev = nf_bridge->physindev;
876
877         nf_bridge->physoutdev = NULL;
878         br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
879 }
880
881 static int br_nf_dev_xmit(struct sk_buff *skb)
882 {
883         if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
884                 br_nf_pre_routing_finish_bridge_slow(skb);
885                 return 1;
886         }
887         return 0;
888 }
889
890 static const struct nf_br_ops br_ops = {
891         .br_dev_xmit_hook =     br_nf_dev_xmit,
892 };
893
894 void br_netfilter_enable(void)
895 {
896 }
897 EXPORT_SYMBOL_GPL(br_netfilter_enable);
898
899 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
900  * br_dev_queue_push_xmit is called afterwards */
901 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
902         {
903                 .hook = br_nf_pre_routing,
904                 .pf = NFPROTO_BRIDGE,
905                 .hooknum = NF_BR_PRE_ROUTING,
906                 .priority = NF_BR_PRI_BRNF,
907         },
908         {
909                 .hook = br_nf_local_in,
910                 .pf = NFPROTO_BRIDGE,
911                 .hooknum = NF_BR_LOCAL_IN,
912                 .priority = NF_BR_PRI_BRNF,
913         },
914         {
915                 .hook = br_nf_forward_ip,
916                 .pf = NFPROTO_BRIDGE,
917                 .hooknum = NF_BR_FORWARD,
918                 .priority = NF_BR_PRI_BRNF - 1,
919         },
920         {
921                 .hook = br_nf_forward_arp,
922                 .pf = NFPROTO_BRIDGE,
923                 .hooknum = NF_BR_FORWARD,
924                 .priority = NF_BR_PRI_BRNF,
925         },
926         {
927                 .hook = br_nf_post_routing,
928                 .pf = NFPROTO_BRIDGE,
929                 .hooknum = NF_BR_POST_ROUTING,
930                 .priority = NF_BR_PRI_LAST,
931         },
932         {
933                 .hook = ip_sabotage_in,
934                 .pf = NFPROTO_IPV4,
935                 .hooknum = NF_INET_PRE_ROUTING,
936                 .priority = NF_IP_PRI_FIRST,
937         },
938         {
939                 .hook = ip_sabotage_in,
940                 .pf = NFPROTO_IPV6,
941                 .hooknum = NF_INET_PRE_ROUTING,
942                 .priority = NF_IP6_PRI_FIRST,
943         },
944 };
945
946 #ifdef CONFIG_SYSCTL
947 static
948 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
949                             void __user *buffer, size_t *lenp, loff_t *ppos)
950 {
951         int ret;
952
953         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
954
955         if (write && *(int *)(ctl->data))
956                 *(int *)(ctl->data) = 1;
957         return ret;
958 }
959
960 static struct ctl_table brnf_table[] = {
961         {
962                 .procname       = "bridge-nf-call-arptables",
963                 .data           = &brnf_call_arptables,
964                 .maxlen         = sizeof(int),
965                 .mode           = 0644,
966                 .proc_handler   = brnf_sysctl_call_tables,
967         },
968         {
969                 .procname       = "bridge-nf-call-iptables",
970                 .data           = &brnf_call_iptables,
971                 .maxlen         = sizeof(int),
972                 .mode           = 0644,
973                 .proc_handler   = brnf_sysctl_call_tables,
974         },
975         {
976                 .procname       = "bridge-nf-call-ip6tables",
977                 .data           = &brnf_call_ip6tables,
978                 .maxlen         = sizeof(int),
979                 .mode           = 0644,
980                 .proc_handler   = brnf_sysctl_call_tables,
981         },
982         {
983                 .procname       = "bridge-nf-filter-vlan-tagged",
984                 .data           = &brnf_filter_vlan_tagged,
985                 .maxlen         = sizeof(int),
986                 .mode           = 0644,
987                 .proc_handler   = brnf_sysctl_call_tables,
988         },
989         {
990                 .procname       = "bridge-nf-filter-pppoe-tagged",
991                 .data           = &brnf_filter_pppoe_tagged,
992                 .maxlen         = sizeof(int),
993                 .mode           = 0644,
994                 .proc_handler   = brnf_sysctl_call_tables,
995         },
996         {
997                 .procname       = "bridge-nf-pass-vlan-input-dev",
998                 .data           = &brnf_pass_vlan_indev,
999                 .maxlen         = sizeof(int),
1000                 .mode           = 0644,
1001                 .proc_handler   = brnf_sysctl_call_tables,
1002         },
1003         { }
1004 };
1005 #endif
1006
1007 static int __init br_netfilter_init(void)
1008 {
1009         int ret;
1010
1011         ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1012         if (ret < 0)
1013                 return ret;
1014
1015 #ifdef CONFIG_SYSCTL
1016         brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1017         if (brnf_sysctl_header == NULL) {
1018                 printk(KERN_WARNING
1019                        "br_netfilter: can't register to sysctl.\n");
1020                 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1021                 return -ENOMEM;
1022         }
1023 #endif
1024         RCU_INIT_POINTER(nf_br_ops, &br_ops);
1025         printk(KERN_NOTICE "Bridge firewalling registered\n");
1026         return 0;
1027 }
1028
1029 static void __exit br_netfilter_fini(void)
1030 {
1031         RCU_INIT_POINTER(nf_br_ops, NULL);
1032         nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1033 #ifdef CONFIG_SYSCTL
1034         unregister_net_sysctl_table(brnf_sysctl_header);
1035 #endif
1036 }
1037
1038 module_init(br_netfilter_init);
1039 module_exit(br_netfilter_fini);
1040
1041 MODULE_LICENSE("GPL");
1042 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1043 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1044 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");