These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / 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
692 static int
693 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
694                   int (*output)(struct net *, struct sock *, struct sk_buff *))
695 {
696         unsigned int mtu = ip_skb_dst_mtu(skb);
697         struct iphdr *iph = ip_hdr(skb);
698
699         if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
700                      (IPCB(skb)->frag_max_size &&
701                       IPCB(skb)->frag_max_size > mtu))) {
702                 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
703                 kfree_skb(skb);
704                 return -EMSGSIZE;
705         }
706
707         return ip_do_fragment(net, sk, skb, output);
708 }
709
710 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
711 {
712         if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
713                 return PPPOE_SES_HLEN;
714         return 0;
715 }
716
717 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
718 {
719         struct nf_bridge_info *nf_bridge;
720         unsigned int mtu_reserved;
721
722         mtu_reserved = nf_bridge_mtu_reduction(skb);
723
724         if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
725                 nf_bridge_info_free(skb);
726                 return br_dev_queue_push_xmit(net, sk, skb);
727         }
728
729         nf_bridge = nf_bridge_info_get(skb);
730
731         /* This is wrong! We should preserve the original fragment
732          * boundaries by preserving frag_list rather than refragmenting.
733          */
734         if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
735             skb->protocol == htons(ETH_P_IP)) {
736                 struct brnf_frag_data *data;
737
738                 if (br_validate_ipv4(net, skb))
739                         goto drop;
740
741                 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
742
743                 nf_bridge_update_protocol(skb);
744
745                 data = this_cpu_ptr(&brnf_frag_data_storage);
746
747                 data->vlan_tci = skb->vlan_tci;
748                 data->vlan_proto = skb->vlan_proto;
749                 data->encap_size = nf_bridge_encap_header_len(skb);
750                 data->size = ETH_HLEN + data->encap_size;
751
752                 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
753                                                  data->size);
754
755                 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
756         }
757         if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
758             skb->protocol == htons(ETH_P_IPV6)) {
759                 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
760                 struct brnf_frag_data *data;
761
762                 if (br_validate_ipv6(net, skb))
763                         goto drop;
764
765                 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
766
767                 nf_bridge_update_protocol(skb);
768
769                 data = this_cpu_ptr(&brnf_frag_data_storage);
770                 data->encap_size = nf_bridge_encap_header_len(skb);
771                 data->size = ETH_HLEN + data->encap_size;
772
773                 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
774                                                  data->size);
775
776                 if (v6ops)
777                         return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
778
779                 kfree_skb(skb);
780                 return -EMSGSIZE;
781         }
782         nf_bridge_info_free(skb);
783         return br_dev_queue_push_xmit(net, sk, skb);
784  drop:
785         kfree_skb(skb);
786         return 0;
787 }
788
789 /* PF_BRIDGE/POST_ROUTING ********************************************/
790 static unsigned int br_nf_post_routing(void *priv,
791                                        struct sk_buff *skb,
792                                        const struct nf_hook_state *state)
793 {
794         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
795         struct net_device *realoutdev = bridge_parent(skb->dev);
796         u_int8_t pf;
797
798         /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
799          * on a bridge, but was delivered locally and is now being routed:
800          *
801          * POST_ROUTING was already invoked from the ip stack.
802          */
803         if (!nf_bridge || !nf_bridge->physoutdev)
804                 return NF_ACCEPT;
805
806         if (!realoutdev)
807                 return NF_DROP;
808
809         if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
810                 pf = NFPROTO_IPV4;
811         else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
812                 pf = NFPROTO_IPV6;
813         else
814                 return NF_ACCEPT;
815
816         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
817          * about the value of skb->pkt_type. */
818         if (skb->pkt_type == PACKET_OTHERHOST) {
819                 skb->pkt_type = PACKET_HOST;
820                 nf_bridge->pkt_otherhost = true;
821         }
822
823         nf_bridge_pull_encap_header(skb);
824         if (pf == NFPROTO_IPV4)
825                 skb->protocol = htons(ETH_P_IP);
826         else
827                 skb->protocol = htons(ETH_P_IPV6);
828
829         NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
830                 NULL, realoutdev,
831                 br_nf_dev_queue_xmit);
832
833         return NF_STOLEN;
834 }
835
836 /* IP/SABOTAGE *****************************************************/
837 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
838  * for the second time. */
839 static unsigned int ip_sabotage_in(void *priv,
840                                    struct sk_buff *skb,
841                                    const struct nf_hook_state *state)
842 {
843         if (skb->nf_bridge && !skb->nf_bridge->in_prerouting)
844                 return NF_STOP;
845
846         return NF_ACCEPT;
847 }
848
849 /* This is called when br_netfilter has called into iptables/netfilter,
850  * and DNAT has taken place on a bridge-forwarded packet.
851  *
852  * neigh->output has created a new MAC header, with local br0 MAC
853  * as saddr.
854  *
855  * This restores the original MAC saddr of the bridged packet
856  * before invoking bridge forward logic to transmit the packet.
857  */
858 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
859 {
860         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
861
862         skb_pull(skb, ETH_HLEN);
863         nf_bridge->bridged_dnat = 0;
864
865         BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
866
867         skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
868                                        nf_bridge->neigh_header,
869                                        ETH_HLEN - ETH_ALEN);
870         skb->dev = nf_bridge->physindev;
871
872         nf_bridge->physoutdev = NULL;
873         br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
874 }
875
876 static int br_nf_dev_xmit(struct sk_buff *skb)
877 {
878         if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
879                 br_nf_pre_routing_finish_bridge_slow(skb);
880                 return 1;
881         }
882         return 0;
883 }
884
885 static const struct nf_br_ops br_ops = {
886         .br_dev_xmit_hook =     br_nf_dev_xmit,
887 };
888
889 void br_netfilter_enable(void)
890 {
891 }
892 EXPORT_SYMBOL_GPL(br_netfilter_enable);
893
894 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
895  * br_dev_queue_push_xmit is called afterwards */
896 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
897         {
898                 .hook = br_nf_pre_routing,
899                 .pf = NFPROTO_BRIDGE,
900                 .hooknum = NF_BR_PRE_ROUTING,
901                 .priority = NF_BR_PRI_BRNF,
902         },
903         {
904                 .hook = br_nf_local_in,
905                 .pf = NFPROTO_BRIDGE,
906                 .hooknum = NF_BR_LOCAL_IN,
907                 .priority = NF_BR_PRI_BRNF,
908         },
909         {
910                 .hook = br_nf_forward_ip,
911                 .pf = NFPROTO_BRIDGE,
912                 .hooknum = NF_BR_FORWARD,
913                 .priority = NF_BR_PRI_BRNF - 1,
914         },
915         {
916                 .hook = br_nf_forward_arp,
917                 .pf = NFPROTO_BRIDGE,
918                 .hooknum = NF_BR_FORWARD,
919                 .priority = NF_BR_PRI_BRNF,
920         },
921         {
922                 .hook = br_nf_post_routing,
923                 .pf = NFPROTO_BRIDGE,
924                 .hooknum = NF_BR_POST_ROUTING,
925                 .priority = NF_BR_PRI_LAST,
926         },
927         {
928                 .hook = ip_sabotage_in,
929                 .pf = NFPROTO_IPV4,
930                 .hooknum = NF_INET_PRE_ROUTING,
931                 .priority = NF_IP_PRI_FIRST,
932         },
933         {
934                 .hook = ip_sabotage_in,
935                 .pf = NFPROTO_IPV6,
936                 .hooknum = NF_INET_PRE_ROUTING,
937                 .priority = NF_IP6_PRI_FIRST,
938         },
939 };
940
941 #ifdef CONFIG_SYSCTL
942 static
943 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
944                             void __user *buffer, size_t *lenp, loff_t *ppos)
945 {
946         int ret;
947
948         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
949
950         if (write && *(int *)(ctl->data))
951                 *(int *)(ctl->data) = 1;
952         return ret;
953 }
954
955 static struct ctl_table brnf_table[] = {
956         {
957                 .procname       = "bridge-nf-call-arptables",
958                 .data           = &brnf_call_arptables,
959                 .maxlen         = sizeof(int),
960                 .mode           = 0644,
961                 .proc_handler   = brnf_sysctl_call_tables,
962         },
963         {
964                 .procname       = "bridge-nf-call-iptables",
965                 .data           = &brnf_call_iptables,
966                 .maxlen         = sizeof(int),
967                 .mode           = 0644,
968                 .proc_handler   = brnf_sysctl_call_tables,
969         },
970         {
971                 .procname       = "bridge-nf-call-ip6tables",
972                 .data           = &brnf_call_ip6tables,
973                 .maxlen         = sizeof(int),
974                 .mode           = 0644,
975                 .proc_handler   = brnf_sysctl_call_tables,
976         },
977         {
978                 .procname       = "bridge-nf-filter-vlan-tagged",
979                 .data           = &brnf_filter_vlan_tagged,
980                 .maxlen         = sizeof(int),
981                 .mode           = 0644,
982                 .proc_handler   = brnf_sysctl_call_tables,
983         },
984         {
985                 .procname       = "bridge-nf-filter-pppoe-tagged",
986                 .data           = &brnf_filter_pppoe_tagged,
987                 .maxlen         = sizeof(int),
988                 .mode           = 0644,
989                 .proc_handler   = brnf_sysctl_call_tables,
990         },
991         {
992                 .procname       = "bridge-nf-pass-vlan-input-dev",
993                 .data           = &brnf_pass_vlan_indev,
994                 .maxlen         = sizeof(int),
995                 .mode           = 0644,
996                 .proc_handler   = brnf_sysctl_call_tables,
997         },
998         { }
999 };
1000 #endif
1001
1002 static int __init br_netfilter_init(void)
1003 {
1004         int ret;
1005
1006         ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1007         if (ret < 0)
1008                 return ret;
1009
1010 #ifdef CONFIG_SYSCTL
1011         brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1012         if (brnf_sysctl_header == NULL) {
1013                 printk(KERN_WARNING
1014                        "br_netfilter: can't register to sysctl.\n");
1015                 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1016                 return -ENOMEM;
1017         }
1018 #endif
1019         RCU_INIT_POINTER(nf_br_ops, &br_ops);
1020         printk(KERN_NOTICE "Bridge firewalling registered\n");
1021         return 0;
1022 }
1023
1024 static void __exit br_netfilter_fini(void)
1025 {
1026         RCU_INIT_POINTER(nf_br_ops, NULL);
1027         nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1028 #ifdef CONFIG_SYSCTL
1029         unregister_net_sysctl_table(brnf_sysctl_header);
1030 #endif
1031 }
1032
1033 module_init(br_netfilter_init);
1034 module_exit(br_netfilter_fini);
1035
1036 MODULE_LICENSE("GPL");
1037 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1038 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1039 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");