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