1 /*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer <bdschuym@pandora.be>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Lennert dedicates this file to Kerstin Wurdinger.
15 */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/ip.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <linux/if_pppox.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/netfilter_bridge.h>
29 #include <linux/netfilter_ipv4.h>
30 #include <linux/netfilter_ipv6.h>
31 #include <linux/netfilter_arp.h>
32 #include <linux/in_route.h>
33 #include <linux/inetdevice.h>
34
35 #include <net/ip.h>
36 #include <net/ipv6.h>
37 #include <net/route.h>
38
39 #include <asm/uaccess.h>
40 #include "br_private.h"
41 #ifdef CONFIG_SYSCTL
42 #include <linux/sysctl.h>
43 #endif
44
45 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
46 (skb->nf_bridge->data))->daddr.ipv4)
47 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
48 #define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
49
50 #ifdef CONFIG_SYSCTL
51 static struct ctl_table_header *brnf_sysctl_header;
52 static int brnf_call_iptables __read_mostly = 1;
53 static int brnf_call_ip6tables __read_mostly = 1;
54 static int brnf_call_arptables __read_mostly = 1;
55 static int brnf_filter_vlan_tagged __read_mostly = 0;
56 static int brnf_filter_pppoe_tagged __read_mostly = 0;
57 #else
58 #define brnf_call_iptables 1
59 #define brnf_call_ip6tables 1
60 #define brnf_call_arptables 1
61 #define brnf_filter_vlan_tagged 0
62 #define brnf_filter_pppoe_tagged 0
63 #endif
64
65 #define IS_IP(skb) \
66 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
67
68 #define IS_IPV6(skb) \
69 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
70
71 #define IS_ARP(skb) \
72 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
73
vlan_proto(const struct sk_buff * skb)74 static inline __be16 vlan_proto(const struct sk_buff *skb)
75 {
76 if (vlan_tx_tag_present(skb))
77 return skb->protocol;
78 else if (skb->protocol == htons(ETH_P_8021Q))
79 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
80 else
81 return 0;
82 }
83
84 #define IS_VLAN_IP(skb) \
85 (vlan_proto(skb) == htons(ETH_P_IP) && \
86 brnf_filter_vlan_tagged)
87
88 #define IS_VLAN_IPV6(skb) \
89 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
90 brnf_filter_vlan_tagged)
91
92 #define IS_VLAN_ARP(skb) \
93 (vlan_proto(skb) == htons(ETH_P_ARP) && \
94 brnf_filter_vlan_tagged)
95
pppoe_proto(const struct sk_buff * skb)96 static inline __be16 pppoe_proto(const struct sk_buff *skb)
97 {
98 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
99 sizeof(struct pppoe_hdr)));
100 }
101
102 #define IS_PPPOE_IP(skb) \
103 (skb->protocol == htons(ETH_P_PPP_SES) && \
104 pppoe_proto(skb) == htons(PPP_IP) && \
105 brnf_filter_pppoe_tagged)
106
107 #define IS_PPPOE_IPV6(skb) \
108 (skb->protocol == htons(ETH_P_PPP_SES) && \
109 pppoe_proto(skb) == htons(PPP_IPV6) && \
110 brnf_filter_pppoe_tagged)
111
fake_update_pmtu(struct dst_entry * dst,u32 mtu)112 static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
113 {
114 }
115
fake_cow_metrics(struct dst_entry * dst,unsigned long old)116 static u32 *fake_cow_metrics(struct dst_entry *dst, unsigned long old)
117 {
118 return NULL;
119 }
120
fake_neigh_lookup(const struct dst_entry * dst,const void * daddr)121 static struct neighbour *fake_neigh_lookup(const struct dst_entry *dst, const void *daddr)
122 {
123 return NULL;
124 }
125
fake_mtu(const struct dst_entry * dst)126 static unsigned int fake_mtu(const struct dst_entry *dst)
127 {
128 return dst->dev->mtu;
129 }
130
131 static struct dst_ops fake_dst_ops = {
132 .family = AF_INET,
133 .protocol = cpu_to_be16(ETH_P_IP),
134 .update_pmtu = fake_update_pmtu,
135 .cow_metrics = fake_cow_metrics,
136 .neigh_lookup = fake_neigh_lookup,
137 .mtu = fake_mtu,
138 };
139
140 /*
141 * Initialize bogus route table used to keep netfilter happy.
142 * Currently, we fill in the PMTU entry because netfilter
143 * refragmentation needs it, and the rt_flags entry because
144 * ipt_REJECT needs it. Future netfilter modules might
145 * require us to fill additional fields.
146 */
147 static const u32 br_dst_default_metrics[RTAX_MAX] = {
148 [RTAX_MTU - 1] = 1500,
149 };
150
br_netfilter_rtable_init(struct net_bridge * br)151 void br_netfilter_rtable_init(struct net_bridge *br)
152 {
153 struct rtable *rt = &br->fake_rtable;
154
155 atomic_set(&rt->dst.__refcnt, 1);
156 rt->dst.dev = br->dev;
157 rt->dst.path = &rt->dst;
158 dst_init_metrics(&rt->dst, br_dst_default_metrics, true);
159 rt->dst.flags = DST_NOXFRM | DST_NOPEER | DST_FAKE_RTABLE;
160 rt->dst.ops = &fake_dst_ops;
161 }
162
bridge_parent_rtable(const struct net_device * dev)163 static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
164 {
165 struct net_bridge_port *port;
166
167 port = br_port_get_rcu(dev);
168 return port ? &port->br->fake_rtable : NULL;
169 }
170
bridge_parent(const struct net_device * dev)171 static inline struct net_device *bridge_parent(const struct net_device *dev)
172 {
173 struct net_bridge_port *port;
174
175 port = br_port_get_rcu(dev);
176 return port ? port->br->dev : NULL;
177 }
178
nf_bridge_alloc(struct sk_buff * skb)179 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
180 {
181 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
182 if (likely(skb->nf_bridge))
183 atomic_set(&(skb->nf_bridge->use), 1);
184
185 return skb->nf_bridge;
186 }
187
nf_bridge_unshare(struct sk_buff * skb)188 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
189 {
190 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
191
192 if (atomic_read(&nf_bridge->use) > 1) {
193 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
194
195 if (tmp) {
196 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
197 atomic_set(&tmp->use, 1);
198 }
199 nf_bridge_put(nf_bridge);
200 nf_bridge = tmp;
201 }
202 return nf_bridge;
203 }
204
nf_bridge_push_encap_header(struct sk_buff * skb)205 static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
206 {
207 unsigned int len = nf_bridge_encap_header_len(skb);
208
209 skb_push(skb, len);
210 skb->network_header -= len;
211 }
212
nf_bridge_pull_encap_header(struct sk_buff * skb)213 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
214 {
215 unsigned int len = nf_bridge_encap_header_len(skb);
216
217 skb_pull(skb, len);
218 skb->network_header += len;
219 }
220
nf_bridge_pull_encap_header_rcsum(struct sk_buff * skb)221 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
222 {
223 unsigned int len = nf_bridge_encap_header_len(skb);
224
225 skb_pull_rcsum(skb, len);
226 skb->network_header += len;
227 }
228
nf_bridge_save_header(struct sk_buff * skb)229 static inline void nf_bridge_save_header(struct sk_buff *skb)
230 {
231 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
232
233 skb_copy_from_linear_data_offset(skb, -header_size,
234 skb->nf_bridge->data, header_size);
235 }
236
nf_bridge_update_protocol(struct sk_buff * skb)237 static inline void nf_bridge_update_protocol(struct sk_buff *skb)
238 {
239 if (skb->nf_bridge->mask & BRNF_8021Q)
240 skb->protocol = htons(ETH_P_8021Q);
241 else if (skb->nf_bridge->mask & BRNF_PPPoE)
242 skb->protocol = htons(ETH_P_PPP_SES);
243 }
244
245 /* When handing a packet over to the IP layer
246 * check whether we have a skb that is in the
247 * expected format
248 */
249
br_parse_ip_options(struct sk_buff * skb)250 static int br_parse_ip_options(struct sk_buff *skb)
251 {
252 struct ip_options *opt;
253 const struct iphdr *iph;
254 struct net_device *dev = skb->dev;
255 u32 len;
256
257 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
258 goto inhdr_error;
259
260 iph = ip_hdr(skb);
261 opt = &(IPCB(skb)->opt);
262
263 /* Basic sanity checks */
264 if (iph->ihl < 5 || iph->version != 4)
265 goto inhdr_error;
266
267 if (!pskb_may_pull(skb, iph->ihl*4))
268 goto inhdr_error;
269
270 iph = ip_hdr(skb);
271 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
272 goto inhdr_error;
273
274 len = ntohs(iph->tot_len);
275 if (skb->len < len) {
276 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
277 goto drop;
278 } else if (len < (iph->ihl*4))
279 goto inhdr_error;
280
281 if (pskb_trim_rcsum(skb, len)) {
282 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
283 goto drop;
284 }
285
286 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
287 if (iph->ihl == 5)
288 return 0;
289
290 opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
291 if (ip_options_compile(dev_net(dev), opt, skb))
292 goto inhdr_error;
293
294 /* Check correct handling of SRR option */
295 if (unlikely(opt->srr)) {
296 struct in_device *in_dev = __in_dev_get_rcu(dev);
297 if (in_dev && !IN_DEV_SOURCE_ROUTE(in_dev))
298 goto drop;
299
300 if (ip_options_rcv_srr(skb))
301 goto drop;
302 }
303
304 return 0;
305
306 inhdr_error:
307 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
308 drop:
309 return -1;
310 }
311
312 /* Fill in the header for fragmented IP packets handled by
313 * the IPv4 connection tracking code.
314 */
nf_bridge_copy_header(struct sk_buff * skb)315 int nf_bridge_copy_header(struct sk_buff *skb)
316 {
317 int err;
318 unsigned int header_size;
319
320 nf_bridge_update_protocol(skb);
321 header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
322 err = skb_cow_head(skb, header_size);
323 if (err)
324 return err;
325
326 skb_copy_to_linear_data_offset(skb, -header_size,
327 skb->nf_bridge->data, header_size);
328 __skb_push(skb, nf_bridge_encap_header_len(skb));
329 return 0;
330 }
331
332 /* PF_BRIDGE/PRE_ROUTING *********************************************/
333 /* Undo the changes made for ip6tables PREROUTING and continue the
334 * bridge PRE_ROUTING hook. */
br_nf_pre_routing_finish_ipv6(struct sk_buff * skb)335 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
336 {
337 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
338 struct rtable *rt;
339
340 if (nf_bridge->mask & BRNF_PKT_TYPE) {
341 skb->pkt_type = PACKET_OTHERHOST;
342 nf_bridge->mask ^= BRNF_PKT_TYPE;
343 }
344 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
345
346 rt = bridge_parent_rtable(nf_bridge->physindev);
347 if (!rt) {
348 kfree_skb(skb);
349 return 0;
350 }
351 skb_dst_set_noref(skb, &rt->dst);
352
353 skb->dev = nf_bridge->physindev;
354 nf_bridge_update_protocol(skb);
355 nf_bridge_push_encap_header(skb);
356 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
357 br_handle_frame_finish, 1);
358
359 return 0;
360 }
361
362 /* Obtain the correct destination MAC address, while preserving the original
363 * source MAC address. If we already know this address, we just copy it. If we
364 * don't, we use the neighbour framework to find out. In both cases, we make
365 * sure that br_handle_frame_finish() is called afterwards.
366 */
br_nf_pre_routing_finish_bridge(struct sk_buff * skb)367 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
368 {
369 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
370 struct neighbour *neigh;
371 struct dst_entry *dst;
372
373 skb->dev = bridge_parent(skb->dev);
374 if (!skb->dev)
375 goto free_skb;
376 dst = skb_dst(skb);
377 neigh = dst_get_neighbour_noref(dst);
378 if (neigh->hh.hh_len) {
379 neigh_hh_bridge(&neigh->hh, skb);
380 skb->dev = nf_bridge->physindev;
381 return br_handle_frame_finish(skb);
382 } else {
383 /* the neighbour function below overwrites the complete
384 * MAC header, so we save the Ethernet source address and
385 * protocol number. */
386 skb_copy_from_linear_data_offset(skb, -(ETH_HLEN-ETH_ALEN), skb->nf_bridge->data, ETH_HLEN-ETH_ALEN);
387 /* tell br_dev_xmit to continue with forwarding */
388 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
389 return neigh->output(neigh, skb);
390 }
391 free_skb:
392 kfree_skb(skb);
393 return 0;
394 }
395
396 /* This requires some explaining. If DNAT has taken place,
397 * we will need to fix up the destination Ethernet address.
398 *
399 * There are two cases to consider:
400 * 1. The packet was DNAT'ed to a device in the same bridge
401 * port group as it was received on. We can still bridge
402 * the packet.
403 * 2. The packet was DNAT'ed to a different device, either
404 * a non-bridged device or another bridge port group.
405 * The packet will need to be routed.
406 *
407 * The correct way of distinguishing between these two cases is to
408 * call ip_route_input() and to look at skb->dst->dev, which is
409 * changed to the destination device if ip_route_input() succeeds.
410 *
411 * Let's first consider the case that ip_route_input() succeeds:
412 *
413 * If the output device equals the logical bridge device the packet
414 * came in on, we can consider this bridging. The corresponding MAC
415 * address will be obtained in br_nf_pre_routing_finish_bridge.
416 * Otherwise, the packet is considered to be routed and we just
417 * change the destination MAC address so that the packet will
418 * later be passed up to the IP stack to be routed. For a redirected
419 * packet, ip_route_input() will give back the localhost as output device,
420 * which differs from the bridge device.
421 *
422 * Let's now consider the case that ip_route_input() fails:
423 *
424 * This can be because the destination address is martian, in which case
425 * the packet will be dropped.
426 * If IP forwarding is disabled, ip_route_input() will fail, while
427 * ip_route_output_key() can return success. The source
428 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
429 * thinks we're handling a locally generated packet and won't care
430 * if IP forwarding is enabled. If the output device equals the logical bridge
431 * device, we proceed as if ip_route_input() succeeded. If it differs from the
432 * logical bridge port or if ip_route_output_key() fails we drop the packet.
433 */
br_nf_pre_routing_finish(struct sk_buff * skb)434 static int br_nf_pre_routing_finish(struct sk_buff *skb)
435 {
436 struct net_device *dev = skb->dev;
437 struct iphdr *iph = ip_hdr(skb);
438 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
439 struct rtable *rt;
440 int err;
441
442 if (nf_bridge->mask & BRNF_PKT_TYPE) {
443 skb->pkt_type = PACKET_OTHERHOST;
444 nf_bridge->mask ^= BRNF_PKT_TYPE;
445 }
446 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
447 if (dnat_took_place(skb)) {
448 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
449 struct in_device *in_dev = __in_dev_get_rcu(dev);
450
451 /* If err equals -EHOSTUNREACH the error is due to a
452 * martian destination or due to the fact that
453 * forwarding is disabled. For most martian packets,
454 * ip_route_output_key() will fail. It won't fail for 2 types of
455 * martian destinations: loopback destinations and destination
456 * 0.0.0.0. In both cases the packet will be dropped because the
457 * destination is the loopback device and not the bridge. */
458 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
459 goto free_skb;
460
461 rt = ip_route_output(dev_net(dev), iph->daddr, 0,
462 RT_TOS(iph->tos), 0);
463 if (!IS_ERR(rt)) {
464 /* - Bridged-and-DNAT'ed traffic doesn't
465 * require ip_forwarding. */
466 if (rt->dst.dev == dev) {
467 skb_dst_set(skb, &rt->dst);
468 goto bridged_dnat;
469 }
470 ip_rt_put(rt);
471 }
472 free_skb:
473 kfree_skb(skb);
474 return 0;
475 } else {
476 if (skb_dst(skb)->dev == dev) {
477 bridged_dnat:
478 skb->dev = nf_bridge->physindev;
479 nf_bridge_update_protocol(skb);
480 nf_bridge_push_encap_header(skb);
481 NF_HOOK_THRESH(NFPROTO_BRIDGE,
482 NF_BR_PRE_ROUTING,
483 skb, skb->dev, NULL,
484 br_nf_pre_routing_finish_bridge,
485 1);
486 return 0;
487 }
488 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
489 skb->pkt_type = PACKET_HOST;
490 }
491 } else {
492 rt = bridge_parent_rtable(nf_bridge->physindev);
493 if (!rt) {
494 kfree_skb(skb);
495 return 0;
496 }
497 skb_dst_set_noref(skb, &rt->dst);
498 }
499
500 skb->dev = nf_bridge->physindev;
501 nf_bridge_update_protocol(skb);
502 nf_bridge_push_encap_header(skb);
503 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
504 br_handle_frame_finish, 1);
505
506 return 0;
507 }
508
509 /* Some common code for IPv4/IPv6 */
setup_pre_routing(struct sk_buff * skb)510 static struct net_device *setup_pre_routing(struct sk_buff *skb)
511 {
512 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
513
514 if (skb->pkt_type == PACKET_OTHERHOST) {
515 skb->pkt_type = PACKET_HOST;
516 nf_bridge->mask |= BRNF_PKT_TYPE;
517 }
518
519 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
520 nf_bridge->physindev = skb->dev;
521 skb->dev = bridge_parent(skb->dev);
522 if (skb->protocol == htons(ETH_P_8021Q))
523 nf_bridge->mask |= BRNF_8021Q;
524 else if (skb->protocol == htons(ETH_P_PPP_SES))
525 nf_bridge->mask |= BRNF_PPPoE;
526
527 return skb->dev;
528 }
529
530 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
check_hbh_len(struct sk_buff * skb)531 static int check_hbh_len(struct sk_buff *skb)
532 {
533 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
534 u32 pkt_len;
535 const unsigned char *nh = skb_network_header(skb);
536 int off = raw - nh;
537 int len = (raw[1] + 1) << 3;
538
539 if ((raw + len) - skb->data > skb_headlen(skb))
540 goto bad;
541
542 off += 2;
543 len -= 2;
544
545 while (len > 0) {
546 int optlen = nh[off + 1] + 2;
547
548 switch (nh[off]) {
549 case IPV6_TLV_PAD0:
550 optlen = 1;
551 break;
552
553 case IPV6_TLV_PADN:
554 break;
555
556 case IPV6_TLV_JUMBO:
557 if (nh[off + 1] != 4 || (off & 3) != 2)
558 goto bad;
559 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
560 if (pkt_len <= IPV6_MAXPLEN ||
561 ipv6_hdr(skb)->payload_len)
562 goto bad;
563 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
564 goto bad;
565 if (pskb_trim_rcsum(skb,
566 pkt_len + sizeof(struct ipv6hdr)))
567 goto bad;
568 nh = skb_network_header(skb);
569 break;
570 default:
571 if (optlen > len)
572 goto bad;
573 break;
574 }
575 off += optlen;
576 len -= optlen;
577 }
578 if (len == 0)
579 return 0;
580 bad:
581 return -1;
582
583 }
584
585 /* Replicate the checks that IPv6 does on packet reception and pass the packet
586 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
br_nf_pre_routing_ipv6(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))587 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
588 struct sk_buff *skb,
589 const struct net_device *in,
590 const struct net_device *out,
591 int (*okfn)(struct sk_buff *))
592 {
593 const struct ipv6hdr *hdr;
594 u32 pkt_len;
595
596 if (skb->len < sizeof(struct ipv6hdr))
597 return NF_DROP;
598
599 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
600 return NF_DROP;
601
602 hdr = ipv6_hdr(skb);
603
604 if (hdr->version != 6)
605 return NF_DROP;
606
607 pkt_len = ntohs(hdr->payload_len);
608
609 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
610 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
611 return NF_DROP;
612 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
613 return NF_DROP;
614 }
615 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
616 return NF_DROP;
617
618 nf_bridge_put(skb->nf_bridge);
619 if (!nf_bridge_alloc(skb))
620 return NF_DROP;
621 if (!setup_pre_routing(skb))
622 return NF_DROP;
623
624 skb->protocol = htons(ETH_P_IPV6);
625 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
626 br_nf_pre_routing_finish_ipv6);
627
628 return NF_STOLEN;
629 }
630
631 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
632 * Replicate the checks that IPv4 does on packet reception.
633 * Set skb->dev to the bridge device (i.e. parent of the
634 * receiving device) to make netfilter happy, the REDIRECT
635 * target in particular. Save the original destination IP
636 * address to be able to detect DNAT afterwards. */
br_nf_pre_routing(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))637 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
638 const struct net_device *in,
639 const struct net_device *out,
640 int (*okfn)(struct sk_buff *))
641 {
642 struct net_bridge_port *p;
643 struct net_bridge *br;
644 __u32 len = nf_bridge_encap_header_len(skb);
645
646 if (unlikely(!pskb_may_pull(skb, len)))
647 return NF_DROP;
648
649 p = br_port_get_rcu(in);
650 if (p == NULL)
651 return NF_DROP;
652 br = p->br;
653
654 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
655 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
656 return NF_ACCEPT;
657
658 nf_bridge_pull_encap_header_rcsum(skb);
659 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
660 }
661
662 if (!brnf_call_iptables && !br->nf_call_iptables)
663 return NF_ACCEPT;
664
665 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
666 return NF_ACCEPT;
667
668 nf_bridge_pull_encap_header_rcsum(skb);
669
670 if (br_parse_ip_options(skb))
671 return NF_DROP;
672
673 nf_bridge_put(skb->nf_bridge);
674 if (!nf_bridge_alloc(skb))
675 return NF_DROP;
676 if (!setup_pre_routing(skb))
677 return NF_DROP;
678 store_orig_dstaddr(skb);
679 skb->protocol = htons(ETH_P_IP);
680
681 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
682 br_nf_pre_routing_finish);
683
684 return NF_STOLEN;
685 }
686
687
688 /* PF_BRIDGE/LOCAL_IN ************************************************/
689 /* The packet is locally destined, which requires a real
690 * dst_entry, so detach the fake one. On the way up, the
691 * packet would pass through PRE_ROUTING again (which already
692 * took place when the packet entered the bridge), but we
693 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
694 * prevent this from happening. */
br_nf_local_in(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))695 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
696 const struct net_device *in,
697 const struct net_device *out,
698 int (*okfn)(struct sk_buff *))
699 {
700 br_drop_fake_rtable(skb);
701 return NF_ACCEPT;
702 }
703
704 /* PF_BRIDGE/FORWARD *************************************************/
br_nf_forward_finish(struct sk_buff * skb)705 static int br_nf_forward_finish(struct sk_buff *skb)
706 {
707 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
708 struct net_device *in;
709
710 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
711 in = nf_bridge->physindev;
712 if (nf_bridge->mask & BRNF_PKT_TYPE) {
713 skb->pkt_type = PACKET_OTHERHOST;
714 nf_bridge->mask ^= BRNF_PKT_TYPE;
715 }
716 nf_bridge_update_protocol(skb);
717 } else {
718 in = *((struct net_device **)(skb->cb));
719 }
720 nf_bridge_push_encap_header(skb);
721
722 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
723 skb->dev, br_forward_finish, 1);
724 return 0;
725 }
726
727
728 /* This is the 'purely bridged' case. For IP, we pass the packet to
729 * netfilter with indev and outdev set to the bridge device,
730 * but we are still able to filter on the 'real' indev/outdev
731 * because of the physdev module. For ARP, indev and outdev are the
732 * bridge ports. */
br_nf_forward_ip(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))733 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
734 const struct net_device *in,
735 const struct net_device *out,
736 int (*okfn)(struct sk_buff *))
737 {
738 struct nf_bridge_info *nf_bridge;
739 struct net_device *parent;
740 u_int8_t pf;
741
742 if (!skb->nf_bridge)
743 return NF_ACCEPT;
744
745 /* Need exclusive nf_bridge_info since we might have multiple
746 * different physoutdevs. */
747 if (!nf_bridge_unshare(skb))
748 return NF_DROP;
749
750 parent = bridge_parent(out);
751 if (!parent)
752 return NF_DROP;
753
754 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
755 pf = PF_INET;
756 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
757 pf = PF_INET6;
758 else
759 return NF_ACCEPT;
760
761 nf_bridge_pull_encap_header(skb);
762
763 nf_bridge = skb->nf_bridge;
764 if (skb->pkt_type == PACKET_OTHERHOST) {
765 skb->pkt_type = PACKET_HOST;
766 nf_bridge->mask |= BRNF_PKT_TYPE;
767 }
768
769 if (pf == PF_INET && br_parse_ip_options(skb))
770 return NF_DROP;
771
772 /* The physdev module checks on this */
773 nf_bridge->mask |= BRNF_BRIDGED;
774 nf_bridge->physoutdev = skb->dev;
775 if (pf == PF_INET)
776 skb->protocol = htons(ETH_P_IP);
777 else
778 skb->protocol = htons(ETH_P_IPV6);
779
780 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
781 br_nf_forward_finish);
782
783 return NF_STOLEN;
784 }
785
br_nf_forward_arp(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))786 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
787 const struct net_device *in,
788 const struct net_device *out,
789 int (*okfn)(struct sk_buff *))
790 {
791 struct net_bridge_port *p;
792 struct net_bridge *br;
793 struct net_device **d = (struct net_device **)(skb->cb);
794
795 p = br_port_get_rcu(out);
796 if (p == NULL)
797 return NF_ACCEPT;
798 br = p->br;
799
800 if (!brnf_call_arptables && !br->nf_call_arptables)
801 return NF_ACCEPT;
802
803 if (!IS_ARP(skb)) {
804 if (!IS_VLAN_ARP(skb))
805 return NF_ACCEPT;
806 nf_bridge_pull_encap_header(skb);
807 }
808
809 if (arp_hdr(skb)->ar_pln != 4) {
810 if (IS_VLAN_ARP(skb))
811 nf_bridge_push_encap_header(skb);
812 return NF_ACCEPT;
813 }
814 *d = (struct net_device *)in;
815 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
816 (struct net_device *)out, br_nf_forward_finish);
817
818 return NF_STOLEN;
819 }
820
821 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV4)
br_nf_dev_queue_xmit(struct sk_buff * skb)822 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
823 {
824 int ret;
825
826 if (skb->nfct != NULL && skb->protocol == htons(ETH_P_IP) &&
827 skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
828 !skb_is_gso(skb)) {
829 if (br_parse_ip_options(skb))
830 /* Drop invalid packet */
831 return NF_DROP;
832 ret = ip_fragment(skb, br_dev_queue_push_xmit);
833 } else
834 ret = br_dev_queue_push_xmit(skb);
835
836 return ret;
837 }
838 #else
br_nf_dev_queue_xmit(struct sk_buff * skb)839 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
840 {
841 return br_dev_queue_push_xmit(skb);
842 }
843 #endif
844
845 /* PF_BRIDGE/POST_ROUTING ********************************************/
br_nf_post_routing(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))846 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
847 const struct net_device *in,
848 const struct net_device *out,
849 int (*okfn)(struct sk_buff *))
850 {
851 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
852 struct net_device *realoutdev = bridge_parent(skb->dev);
853 u_int8_t pf;
854
855 if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
856 return NF_ACCEPT;
857
858 if (!realoutdev)
859 return NF_DROP;
860
861 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
862 pf = PF_INET;
863 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
864 pf = PF_INET6;
865 else
866 return NF_ACCEPT;
867
868 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
869 * about the value of skb->pkt_type. */
870 if (skb->pkt_type == PACKET_OTHERHOST) {
871 skb->pkt_type = PACKET_HOST;
872 nf_bridge->mask |= BRNF_PKT_TYPE;
873 }
874
875 nf_bridge_pull_encap_header(skb);
876 nf_bridge_save_header(skb);
877 if (pf == PF_INET)
878 skb->protocol = htons(ETH_P_IP);
879 else
880 skb->protocol = htons(ETH_P_IPV6);
881
882 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
883 br_nf_dev_queue_xmit);
884
885 return NF_STOLEN;
886 }
887
888 /* IP/SABOTAGE *****************************************************/
889 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
890 * for the second time. */
ip_sabotage_in(unsigned int hook,struct sk_buff * skb,const struct net_device * in,const struct net_device * out,int (* okfn)(struct sk_buff *))891 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
892 const struct net_device *in,
893 const struct net_device *out,
894 int (*okfn)(struct sk_buff *))
895 {
896 if (skb->nf_bridge &&
897 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
898 return NF_STOP;
899 }
900
901 return NF_ACCEPT;
902 }
903
904 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
905 * br_dev_queue_push_xmit is called afterwards */
906 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
907 {
908 .hook = br_nf_pre_routing,
909 .owner = THIS_MODULE,
910 .pf = PF_BRIDGE,
911 .hooknum = NF_BR_PRE_ROUTING,
912 .priority = NF_BR_PRI_BRNF,
913 },
914 {
915 .hook = br_nf_local_in,
916 .owner = THIS_MODULE,
917 .pf = PF_BRIDGE,
918 .hooknum = NF_BR_LOCAL_IN,
919 .priority = NF_BR_PRI_BRNF,
920 },
921 {
922 .hook = br_nf_forward_ip,
923 .owner = THIS_MODULE,
924 .pf = PF_BRIDGE,
925 .hooknum = NF_BR_FORWARD,
926 .priority = NF_BR_PRI_BRNF - 1,
927 },
928 {
929 .hook = br_nf_forward_arp,
930 .owner = THIS_MODULE,
931 .pf = PF_BRIDGE,
932 .hooknum = NF_BR_FORWARD,
933 .priority = NF_BR_PRI_BRNF,
934 },
935 {
936 .hook = br_nf_post_routing,
937 .owner = THIS_MODULE,
938 .pf = PF_BRIDGE,
939 .hooknum = NF_BR_POST_ROUTING,
940 .priority = NF_BR_PRI_LAST,
941 },
942 {
943 .hook = ip_sabotage_in,
944 .owner = THIS_MODULE,
945 .pf = PF_INET,
946 .hooknum = NF_INET_PRE_ROUTING,
947 .priority = NF_IP_PRI_FIRST,
948 },
949 {
950 .hook = ip_sabotage_in,
951 .owner = THIS_MODULE,
952 .pf = PF_INET6,
953 .hooknum = NF_INET_PRE_ROUTING,
954 .priority = NF_IP6_PRI_FIRST,
955 },
956 };
957
958 #ifdef CONFIG_SYSCTL
959 static
brnf_sysctl_call_tables(ctl_table * ctl,int write,void __user * buffer,size_t * lenp,loff_t * ppos)960 int brnf_sysctl_call_tables(ctl_table * ctl, int write,
961 void __user * buffer, size_t * lenp, loff_t * ppos)
962 {
963 int ret;
964
965 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
966
967 if (write && *(int *)(ctl->data))
968 *(int *)(ctl->data) = 1;
969 return ret;
970 }
971
972 static ctl_table brnf_table[] = {
973 {
974 .procname = "bridge-nf-call-arptables",
975 .data = &brnf_call_arptables,
976 .maxlen = sizeof(int),
977 .mode = 0644,
978 .proc_handler = brnf_sysctl_call_tables,
979 },
980 {
981 .procname = "bridge-nf-call-iptables",
982 .data = &brnf_call_iptables,
983 .maxlen = sizeof(int),
984 .mode = 0644,
985 .proc_handler = brnf_sysctl_call_tables,
986 },
987 {
988 .procname = "bridge-nf-call-ip6tables",
989 .data = &brnf_call_ip6tables,
990 .maxlen = sizeof(int),
991 .mode = 0644,
992 .proc_handler = brnf_sysctl_call_tables,
993 },
994 {
995 .procname = "bridge-nf-filter-vlan-tagged",
996 .data = &brnf_filter_vlan_tagged,
997 .maxlen = sizeof(int),
998 .mode = 0644,
999 .proc_handler = brnf_sysctl_call_tables,
1000 },
1001 {
1002 .procname = "bridge-nf-filter-pppoe-tagged",
1003 .data = &brnf_filter_pppoe_tagged,
1004 .maxlen = sizeof(int),
1005 .mode = 0644,
1006 .proc_handler = brnf_sysctl_call_tables,
1007 },
1008 { }
1009 };
1010
1011 static struct ctl_path brnf_path[] = {
1012 { .procname = "net", },
1013 { .procname = "bridge", },
1014 { }
1015 };
1016 #endif
1017
br_netfilter_init(void)1018 int __init br_netfilter_init(void)
1019 {
1020 int ret;
1021
1022 ret = dst_entries_init(&fake_dst_ops);
1023 if (ret < 0)
1024 return ret;
1025
1026 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1027 if (ret < 0) {
1028 dst_entries_destroy(&fake_dst_ops);
1029 return ret;
1030 }
1031 #ifdef CONFIG_SYSCTL
1032 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
1033 if (brnf_sysctl_header == NULL) {
1034 printk(KERN_WARNING
1035 "br_netfilter: can't register to sysctl.\n");
1036 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1037 dst_entries_destroy(&fake_dst_ops);
1038 return -ENOMEM;
1039 }
1040 #endif
1041 printk(KERN_NOTICE "Bridge firewalling registered\n");
1042 return 0;
1043 }
1044
br_netfilter_fini(void)1045 void br_netfilter_fini(void)
1046 {
1047 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1048 #ifdef CONFIG_SYSCTL
1049 unregister_sysctl_table(brnf_sysctl_header);
1050 #endif
1051 dst_entries_destroy(&fake_dst_ops);
1052 }
1053