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