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