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