blob: e4e5f2f29173f0fa6a50b815b9fad26febc9c5a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
Bart De Schuymer82379082010-04-13 11:40:41 +02006 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer <bdschuym@pandora.be>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/ip.h>
21#include <linux/netdevice.h>
22#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020023#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/if_ether.h>
25#include <linux/if_vlan.h>
Michael Milner516299d2007-04-12 22:14:23 -070026#include <linux/if_pppox.h>
27#include <linux/ppp_defs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Bart De Schuymerf216f082006-12-05 13:45:21 -080033#include <linux/inetdevice.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Bernhard Thalerefb6de92015-05-30 15:30:16 +020037#include <net/addrconf.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020038#include <net/route.h>
Florian Westphal56768642014-11-13 10:04:16 +010039#include <net/netfilter/br_netfilter.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "br_private.h"
43#ifdef CONFIG_SYSCTL
44#include <linux/sysctl.h>
45#endif
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef CONFIG_SYSCTL
48static struct ctl_table_header *brnf_sysctl_header;
Brian Haley9c1ea142006-09-18 00:03:41 -070049static int brnf_call_iptables __read_mostly = 1;
50static int brnf_call_ip6tables __read_mostly = 1;
51static int brnf_call_arptables __read_mostly = 1;
Herbert Xu47e0e1c2009-01-12 00:06:03 +000052static int brnf_filter_vlan_tagged __read_mostly = 0;
53static int brnf_filter_pppoe_tagged __read_mostly = 0;
Pablo Neira Ayuso49816822012-05-08 19:36:44 +020054static int brnf_pass_vlan_indev __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
Patrick McHardy4df53d82010-07-02 09:32:57 +020056#define brnf_call_iptables 1
57#define brnf_call_ip6tables 1
58#define brnf_call_arptables 1
Herbert Xu47e0e1c2009-01-12 00:06:03 +000059#define brnf_filter_vlan_tagged 0
60#define brnf_filter_pppoe_tagged 0
Pablo Neira Ayuso49816822012-05-08 19:36:44 +020061#define brnf_pass_vlan_indev 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
Florian Westphal739e4502012-03-06 01:22:54 +000064#define IS_IP(skb) \
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010065 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
Florian Westphal739e4502012-03-06 01:22:54 +000066
67#define IS_IPV6(skb) \
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010068 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
Florian Westphal739e4502012-03-06 01:22:54 +000069
70#define IS_ARP(skb) \
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010071 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
Florian Westphal739e4502012-03-06 01:22:54 +000072
Dave Jonesb6f99a22007-03-22 12:27:49 -070073static inline __be16 vlan_proto(const struct sk_buff *skb)
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080074{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010075 if (skb_vlan_tag_present(skb))
Jesse Gross13937912010-10-20 13:56:01 +000076 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;
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080081}
82
83#define IS_VLAN_IP(skb) \
Jesse Gross13937912010-10-20 13:56:01 +000084 (vlan_proto(skb) == htons(ETH_P_IP) && \
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080085 brnf_filter_vlan_tagged)
86
87#define IS_VLAN_IPV6(skb) \
Jesse Gross13937912010-10-20 13:56:01 +000088 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080089 brnf_filter_vlan_tagged)
90
91#define IS_VLAN_ARP(skb) \
Jesse Gross13937912010-10-20 13:56:01 +000092 (vlan_proto(skb) == htons(ETH_P_ARP) && \
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080093 brnf_filter_vlan_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Michael Milner516299d2007-04-12 22:14:23 -070095static 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
Florian Westphale70deec2015-04-02 14:31:40 +0200111/* 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;
Florian Westphald7b59742015-06-05 13:27:13 +0200119 u16 vlan_tci;
120 __be16 vlan_proto;
Florian Westphale70deec2015-04-02 14:31:40 +0200121};
122
123static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
124#endif
125
Florian Westphal38330782015-04-02 14:31:43 +0200126static struct nf_bridge_info *nf_bridge_info_get(const struct sk_buff *skb)
127{
128 return skb->nf_bridge;
129}
130
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200131static void nf_bridge_info_free(struct sk_buff *skb)
132{
133 if (skb->nf_bridge) {
134 nf_bridge_put(skb->nf_bridge);
135 skb->nf_bridge = NULL;
136 }
137}
138
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700139static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
140{
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000141 struct net_bridge_port *port;
142
143 port = br_port_get_rcu(dev);
144 return port ? &port->br->fake_rtable : NULL;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800147static inline struct net_device *bridge_parent(const struct net_device *dev)
148{
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000149 struct net_bridge_port *port;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800150
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000151 port = br_port_get_rcu(dev);
152 return port ? port->br->dev : NULL;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800155static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
156{
157 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
158 if (likely(skb->nf_bridge))
159 atomic_set(&(skb->nf_bridge->use), 1);
160
161 return skb->nf_bridge;
162}
163
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800164static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
165{
166 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
167
168 if (atomic_read(&nf_bridge->use) > 1) {
169 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
170
171 if (tmp) {
172 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
173 atomic_set(&tmp->use, 1);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800174 }
Changli Gao4c3a76a2010-08-22 19:03:26 +0000175 nf_bridge_put(nf_bridge);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800176 nf_bridge = tmp;
177 }
178 return nf_bridge;
179}
180
Florian Westphal8d045162015-03-18 20:55:31 +0100181static unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
182{
183 switch (skb->protocol) {
184 case __cpu_to_be16(ETH_P_8021Q):
185 return VLAN_HLEN;
186 case __cpu_to_be16(ETH_P_PPP_SES):
187 return PPPOE_SES_HLEN;
188 default:
189 return 0;
190 }
191}
192
Patrick McHardyfc385822007-05-03 03:36:16 -0700193static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
194{
195 unsigned int len = nf_bridge_encap_header_len(skb);
196
197 skb_push(skb, len);
198 skb->network_header -= len;
199}
200
201static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
202{
203 unsigned int len = nf_bridge_encap_header_len(skb);
204
205 skb_pull(skb, len);
206 skb->network_header += len;
207}
208
209static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
210{
211 unsigned int len = nf_bridge_encap_header_len(skb);
212
213 skb_pull_rcsum(skb, len);
214 skb->network_header += len;
215}
216
Bandan Das462fb2a2010-09-19 09:34:33 +0000217/* When handing a packet over to the IP layer
218 * check whether we have a skb that is in the
219 * expected format
220 */
221
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200222static int br_validate_ipv4(struct sk_buff *skb)
Bandan Das462fb2a2010-09-19 09:34:33 +0000223{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000224 const struct iphdr *iph;
Bandan Das462fb2a2010-09-19 09:34:33 +0000225 struct net_device *dev = skb->dev;
226 u32 len;
227
Sarveshwar Bandi6caab7b2012-10-10 01:15:01 +0000228 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
229 goto inhdr_error;
230
Bandan Das462fb2a2010-09-19 09:34:33 +0000231 iph = ip_hdr(skb);
Bandan Das462fb2a2010-09-19 09:34:33 +0000232
233 /* Basic sanity checks */
234 if (iph->ihl < 5 || iph->version != 4)
235 goto inhdr_error;
236
237 if (!pskb_may_pull(skb, iph->ihl*4))
238 goto inhdr_error;
239
240 iph = ip_hdr(skb);
241 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
242 goto inhdr_error;
243
244 len = ntohs(iph->tot_len);
245 if (skb->len < len) {
246 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
247 goto drop;
248 } else if (len < (iph->ihl*4))
249 goto inhdr_error;
250
251 if (pskb_trim_rcsum(skb, len)) {
252 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
253 goto drop;
254 }
255
Eric Dumazetf8e98812011-04-12 13:39:14 -0700256 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Herbert Xu7677e862014-10-04 22:18:02 +0800257 /* We should really parse IP options here but until
258 * somebody who actually uses IP options complains to
259 * us we'll just silently ignore the options because
260 * we're lazy!
261 */
Bandan Das462fb2a2010-09-19 09:34:33 +0000262 return 0;
263
264inhdr_error:
265 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
266drop:
267 return -1;
268}
269
Bernhard Thalera4611d32015-05-30 15:29:38 +0200270/* We only check the length. A bridge shouldn't do any hop-by-hop stuff
271 * anyway
272 */
273static int check_hbh_len(struct sk_buff *skb)
274{
275 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
276 u32 pkt_len;
277 const unsigned char *nh = skb_network_header(skb);
278 int off = raw - nh;
279 int len = (raw[1] + 1) << 3;
280
281 if ((raw + len) - skb->data > skb_headlen(skb))
282 goto bad;
283
284 off += 2;
285 len -= 2;
286
287 while (len > 0) {
288 int optlen = nh[off + 1] + 2;
289
290 switch (nh[off]) {
291 case IPV6_TLV_PAD1:
292 optlen = 1;
293 break;
294
295 case IPV6_TLV_PADN:
296 break;
297
298 case IPV6_TLV_JUMBO:
299 if (nh[off + 1] != 4 || (off & 3) != 2)
300 goto bad;
301 pkt_len = ntohl(*(__be32 *)(nh + off + 2));
302 if (pkt_len <= IPV6_MAXPLEN ||
303 ipv6_hdr(skb)->payload_len)
304 goto bad;
305 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
306 goto bad;
307 if (pskb_trim_rcsum(skb,
308 pkt_len + sizeof(struct ipv6hdr)))
309 goto bad;
310 nh = skb_network_header(skb);
311 break;
312 default:
313 if (optlen > len)
314 goto bad;
315 break;
316 }
317 off += optlen;
318 len -= optlen;
319 }
320 if (len == 0)
321 return 0;
322bad:
323 return -1;
324}
325
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200326/* Equivalent to br_validate_ipv4 for IPv6 */
327static int br_validate_ipv6(struct sk_buff *skb)
328{
329 const struct ipv6hdr *hdr;
330 struct net_device *dev = skb->dev;
331 struct inet6_dev *idev = in6_dev_get(skb->dev);
332 u32 pkt_len;
333 u8 ip6h_len = sizeof(struct ipv6hdr);
334
335 if (!pskb_may_pull(skb, ip6h_len))
336 goto inhdr_error;
337
338 if (skb->len < ip6h_len)
339 goto drop;
340
341 hdr = ipv6_hdr(skb);
342
343 if (hdr->version != 6)
344 goto inhdr_error;
345
346 pkt_len = ntohs(hdr->payload_len);
347
348 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
349 if (pkt_len + ip6h_len > skb->len) {
350 IP6_INC_STATS_BH(dev_net(dev), idev,
351 IPSTATS_MIB_INTRUNCATEDPKTS);
352 goto drop;
353 }
354 if (pskb_trim_rcsum(skb, pkt_len + ip6h_len)) {
355 IP6_INC_STATS_BH(dev_net(dev), idev,
356 IPSTATS_MIB_INDISCARDS);
357 goto drop;
358 }
359 }
360 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
361 goto drop;
362
363 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
364 /* No IP options in IPv6 header; however it should be
365 * checked if some next headers need special treatment
366 */
367 return 0;
368
369inhdr_error:
370 IP6_INC_STATS_BH(dev_net(dev), idev, IPSTATS_MIB_INHDRERRORS);
371drop:
372 return -1;
373}
374
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100375static void nf_bridge_update_protocol(struct sk_buff *skb)
376{
Florian Westphal3eaf4022015-04-02 14:31:44 +0200377 switch (skb->nf_bridge->orig_proto) {
378 case BRNF_PROTO_8021Q:
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100379 skb->protocol = htons(ETH_P_8021Q);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200380 break;
381 case BRNF_PROTO_PPPOE:
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100382 skb->protocol = htons(ETH_P_PPP_SES);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200383 break;
384 case BRNF_PROTO_UNCHANGED:
385 break;
386 }
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100387}
388
Bart De Schuymere179e632010-04-15 12:26:39 +0200389/* Obtain the correct destination MAC address, while preserving the original
390 * source MAC address. If we already know this address, we just copy it. If we
391 * don't, we use the neighbour framework to find out. In both cases, we make
392 * sure that br_handle_frame_finish() is called afterwards.
393 */
David Miller7026b1d2015-04-05 22:19:04 -0400394static int br_nf_pre_routing_finish_bridge(struct sock *sk, struct sk_buff *skb)
Bart De Schuymere179e632010-04-15 12:26:39 +0200395{
David S. Millerf6b72b62011-07-14 07:53:20 -0700396 struct neighbour *neigh;
Bart De Schuymere179e632010-04-15 12:26:39 +0200397 struct dst_entry *dst;
398
399 skb->dev = bridge_parent(skb->dev);
400 if (!skb->dev)
401 goto free_skb;
402 dst = skb_dst(skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700403 neigh = dst_neigh_lookup_skb(dst, skb);
404 if (neigh) {
Florian Westphal38330782015-04-02 14:31:43 +0200405 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700406 int ret;
407
408 if (neigh->hh.hh_len) {
409 neigh_hh_bridge(&neigh->hh, skb);
410 skb->dev = nf_bridge->physindev;
David Miller7026b1d2015-04-05 22:19:04 -0400411 ret = br_handle_frame_finish(sk, skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700412 } else {
413 /* the neighbour function below overwrites the complete
414 * MAC header, so we save the Ethernet source address and
415 * protocol number.
416 */
417 skb_copy_from_linear_data_offset(skb,
418 -(ETH_HLEN-ETH_ALEN),
Florian Westphale70deec2015-04-02 14:31:40 +0200419 nf_bridge->neigh_header,
David S. Millerf9d75162012-07-02 22:12:59 -0700420 ETH_HLEN-ETH_ALEN);
421 /* tell br_dev_xmit to continue with forwarding */
422 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
Herbert Xu93fdd472014-10-05 12:00:22 +0800423 /* FIXME Need to refragment */
David S. Millerf9d75162012-07-02 22:12:59 -0700424 ret = neigh->output(neigh, skb);
425 }
426 neigh_release(neigh);
427 return ret;
Bart De Schuymere179e632010-04-15 12:26:39 +0200428 }
429free_skb:
430 kfree_skb(skb);
431 return 0;
432}
433
Florian Westphalfaecbb42015-05-20 13:42:25 +0200434static bool daddr_was_changed(const struct sk_buff *skb,
435 const struct nf_bridge_info *nf_bridge)
Florian Westphalc055d5b2015-03-10 05:08:19 +0100436{
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200437 switch (skb->protocol) {
438 case htons(ETH_P_IP):
439 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
440 case htons(ETH_P_IPV6):
441 return memcmp(&nf_bridge->ipv6_daddr, &ipv6_hdr(skb)->daddr,
442 sizeof(ipv6_hdr(skb)->daddr)) != 0;
443 default:
444 return false;
445 }
Florian Westphalc055d5b2015-03-10 05:08:19 +0100446}
447
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200448/* PF_BRIDGE/PRE_ROUTING: Undo the changes made for ip6tables
449 * PREROUTING and continue the bridge PRE_ROUTING hook. See comment
450 * for br_nf_pre_routing_finish(), same logic is used here but
451 * equivalent IPv6 function ip6_route_input() called indirectly.
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200452 */
453static int br_nf_pre_routing_finish_ipv6(struct sock *sk, struct sk_buff *skb)
454{
455 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
456 struct rtable *rt;
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200457 struct net_device *dev = skb->dev;
458 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200459
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200460 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
461
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200462 if (nf_bridge->pkt_otherhost) {
463 skb->pkt_type = PACKET_OTHERHOST;
464 nf_bridge->pkt_otherhost = false;
465 }
466 nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING;
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200467 if (daddr_was_changed(skb, nf_bridge)) {
468 skb_dst_drop(skb);
469 v6ops->route_input(skb);
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200470
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200471 if (skb_dst(skb)->error) {
472 kfree_skb(skb);
473 return 0;
474 }
475
476 if (skb_dst(skb)->dev == dev) {
477 skb->dev = nf_bridge->physindev;
478 nf_bridge_update_protocol(skb);
479 nf_bridge_push_encap_header(skb);
480 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
481 sk, skb, skb->dev, NULL,
482 br_nf_pre_routing_finish_bridge,
483 1);
484 return 0;
485 }
486 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
487 skb->pkt_type = PACKET_HOST;
488 } else {
489 rt = bridge_parent_rtable(nf_bridge->physindev);
490 if (!rt) {
491 kfree_skb(skb);
492 return 0;
493 }
494 skb_dst_set_noref(skb, &rt->dst);
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200495 }
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200496
497 skb->dev = nf_bridge->physindev;
498 nf_bridge_update_protocol(skb);
499 nf_bridge_push_encap_header(skb);
500 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
501 skb->dev, NULL,
502 br_handle_frame_finish, 1);
503
504 return 0;
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/* This requires some explaining. If DNAT has taken place,
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200508 * we will need to fix up the destination Ethernet address.
Florian Westphalfaecbb42015-05-20 13:42:25 +0200509 * This is also true when SNAT takes place (for the reply direction).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
511 * There are two cases to consider:
512 * 1. The packet was DNAT'ed to a device in the same bridge
513 * port group as it was received on. We can still bridge
514 * the packet.
515 * 2. The packet was DNAT'ed to a different device, either
516 * a non-bridged device or another bridge port group.
517 * The packet will need to be routed.
518 *
519 * The correct way of distinguishing between these two cases is to
520 * call ip_route_input() and to look at skb->dst->dev, which is
521 * changed to the destination device if ip_route_input() succeeds.
522 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200523 * Let's first consider the case that ip_route_input() succeeds:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200525 * If the output device equals the logical bridge device the packet
526 * came in on, we can consider this bridging. The corresponding MAC
527 * address will be obtained in br_nf_pre_routing_finish_bridge.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Otherwise, the packet is considered to be routed and we just
529 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800530 * later be passed up to the IP stack to be routed. For a redirected
531 * packet, ip_route_input() will give back the localhost as output device,
532 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200534 * Let's now consider the case that ip_route_input() fails:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800536 * This can be because the destination address is martian, in which case
537 * the packet will be dropped.
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200538 * If IP forwarding is disabled, ip_route_input() will fail, while
539 * ip_route_output_key() can return success. The source
540 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * thinks we're handling a locally generated packet and won't care
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200542 * if IP forwarding is enabled. If the output device equals the logical bridge
543 * device, we proceed as if ip_route_input() succeeded. If it differs from the
544 * logical bridge port or if ip_route_output_key() fails we drop the packet.
545 */
David Miller7026b1d2015-04-05 22:19:04 -0400546static int br_nf_pre_routing_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700549 struct iphdr *iph = ip_hdr(skb);
Florian Westphal38330782015-04-02 14:31:43 +0200550 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000551 struct rtable *rt;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800552 int err;
Herbert Xu93fdd472014-10-05 12:00:22 +0800553
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200554 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Florian Westphala1e67952015-04-02 14:31:45 +0200556 if (nf_bridge->pkt_otherhost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 skb->pkt_type = PACKET_OTHERHOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200558 nf_bridge->pkt_otherhost = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Bernhard Thalerd39a33e2015-05-30 15:26:13 +0200560 nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING;
Florian Westphalfaecbb42015-05-20 13:42:25 +0200561 if (daddr_was_changed(skb, nf_bridge)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800562 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Eric Dumazetf3abc9b2009-08-24 19:35:38 +0200563 struct in_device *in_dev = __in_dev_get_rcu(dev);
Bart De Schuymerf216f082006-12-05 13:45:21 -0800564
565 /* If err equals -EHOSTUNREACH the error is due to a
566 * martian destination or due to the fact that
567 * forwarding is disabled. For most martian packets,
568 * ip_route_output_key() will fail. It won't fail for 2 types of
569 * martian destinations: loopback destinations and destination
570 * 0.0.0.0. In both cases the packet will be dropped because the
571 * destination is the loopback device and not the bridge. */
572 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
573 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
David S. Miller78fbfd82011-03-12 00:00:52 -0500575 rt = ip_route_output(dev_net(dev), iph->daddr, 0,
576 RT_TOS(iph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800577 if (!IS_ERR(rt)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700578 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800579 * require ip_forwarding. */
David S. Millerb23dd4f2011-03-02 14:31:35 -0800580 if (rt->dst.dev == dev) {
581 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto bridged_dnat;
583 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800584 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800586free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 kfree_skb(skb);
588 return 0;
589 } else {
Eric Dumazetadf30902009-06-02 05:19:30 +0000590 if (skb_dst(skb)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591bridged_dnat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200593 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700594 nf_bridge_push_encap_header(skb);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100595 NF_HOOK_THRESH(NFPROTO_BRIDGE,
596 NF_BR_PRE_ROUTING,
David Miller7026b1d2015-04-05 22:19:04 -0400597 sk, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 br_nf_pre_routing_finish_bridge,
599 1);
600 return 0;
601 }
Joe Perches04091142014-02-23 00:05:26 -0800602 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 skb->pkt_type = PACKET_HOST;
604 }
605 } else {
Eric Dumazet511c3f92009-06-02 05:14:27 +0000606 rt = bridge_parent_rtable(nf_bridge->physindev);
607 if (!rt) {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700608 kfree_skb(skb);
609 return 0;
610 }
Patrick McHardyf9181f42010-06-15 17:31:06 +0200611 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
614 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200615 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700616 nf_bridge_push_encap_header(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400617 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
618 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 br_handle_frame_finish, 1);
620
621 return 0;
622}
623
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200624static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
625{
626 struct net_device *vlan, *br;
627
628 br = bridge_parent(dev);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100629 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200630 return br;
631
dingtianhongf06c7f92014-05-09 14:58:05 +0800632 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100633 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200634
635 return vlan ? vlan : br;
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800639static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Florian Westphal38330782015-04-02 14:31:43 +0200641 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (skb->pkt_type == PACKET_OTHERHOST) {
644 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200645 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
648 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
649 nf_bridge->physindev = skb->dev;
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200650 skb->dev = brnf_get_logical_dev(skb, skb->dev);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200651
Bart De Schuymere179e632010-04-15 12:26:39 +0200652 if (skb->protocol == htons(ETH_P_8021Q))
Florian Westphal3eaf4022015-04-02 14:31:44 +0200653 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
Bart De Schuymere179e632010-04-15 12:26:39 +0200654 else if (skb->protocol == htons(ETH_P_PPP_SES))
Florian Westphal3eaf4022015-04-02 14:31:44 +0200655 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800656
Florian Westphal6b8dbcf2013-10-24 21:32:42 +0200657 /* Must drop socket now because of tproxy. */
658 skb_orphan(skb);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800659 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* Replicate the checks that IPv6 does on packet reception and pass the packet
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200663 * to ip6tables.
664 */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200665static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800666 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400667 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200669 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200671 if (br_validate_ipv6(skb))
Herbert Xudeef4b52010-12-09 17:38:11 +0000672 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800674 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800675 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800677 if (!setup_pre_routing(skb))
678 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200680 nf_bridge = nf_bridge_info_get(skb);
681 nf_bridge->ipv6_daddr = ipv6_hdr(skb)->daddr;
682
Bart De Schuymere179e632010-04-15 12:26:39 +0200683 skb->protocol = htons(ETH_P_IPV6);
David Miller7026b1d2015-04-05 22:19:04 -0400684 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, state->sk, skb,
685 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 br_nf_pre_routing_finish_ipv6);
687
688 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
692 * Replicate the checks that IPv4 does on packet reception.
693 * Set skb->dev to the bridge device (i.e. parent of the
694 * receiving device) to make netfilter happy, the REDIRECT
695 * target in particular. Save the original destination IP
696 * address to be able to detect DNAT afterwards. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200697static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
698 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400699 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Florian Westphalfaecbb42015-05-20 13:42:25 +0200701 struct nf_bridge_info *nf_bridge;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200702 struct net_bridge_port *p;
703 struct net_bridge *br;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700704 __u32 len = nf_bridge_encap_header_len(skb);
705
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700706 if (unlikely(!pskb_may_pull(skb, len)))
Herbert Xudeef4b52010-12-09 17:38:11 +0000707 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
David S. Miller238e54c2015-04-03 20:32:56 -0400709 p = br_port_get_rcu(state->in);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200710 if (p == NULL)
Herbert Xudeef4b52010-12-09 17:38:11 +0000711 return NF_DROP;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200712 br = p->br;
713
Florian Westphal739e4502012-03-06 01:22:54 +0000714 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
Patrick McHardy4df53d82010-07-02 09:32:57 +0200715 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200717
Patrick McHardyfc385822007-05-03 03:36:16 -0700718 nf_bridge_pull_encap_header_rcsum(skb);
David S. Miller238e54c2015-04-03 20:32:56 -0400719 return br_nf_pre_routing_ipv6(ops, skb, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Patrick McHardy4df53d82010-07-02 09:32:57 +0200721
722 if (!brnf_call_iptables && !br->nf_call_iptables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Florian Westphal739e4502012-03-06 01:22:54 +0000725 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return NF_ACCEPT;
727
Patrick McHardyfc385822007-05-03 03:36:16 -0700728 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200730 if (br_validate_ipv4(skb))
Herbert Xudeef4b52010-12-09 17:38:11 +0000731 return NF_DROP;
Herbert Xu17762062010-07-05 21:29:28 +0000732
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800733 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800734 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800736 if (!setup_pre_routing(skb))
737 return NF_DROP;
Florian Westphalc055d5b2015-03-10 05:08:19 +0100738
Florian Westphalfaecbb42015-05-20 13:42:25 +0200739 nf_bridge = nf_bridge_info_get(skb);
740 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
741
Bart De Schuymere179e632010-04-15 12:26:39 +0200742 skb->protocol = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David Miller7026b1d2015-04-05 22:19:04 -0400744 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->sk, skb,
745 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 br_nf_pre_routing_finish);
747
748 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751
752/* PF_BRIDGE/LOCAL_IN ************************************************/
753/* The packet is locally destined, which requires a real
754 * dst_entry, so detach the fake one. On the way up, the
755 * packet would pass through PRE_ROUTING again (which already
756 * took place when the packet entered the bridge), but we
757 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
758 * prevent this from happening. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200759static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
760 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400761 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Peter Huang (Peng)a881e962012-04-19 20:12:51 +0000763 br_drop_fake_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return NF_ACCEPT;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/* PF_BRIDGE/FORWARD *************************************************/
David Miller7026b1d2015-04-05 22:19:04 -0400768static int br_nf_forward_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Florian Westphal38330782015-04-02 14:31:43 +0200770 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Florian Westphal739e4502012-03-06 01:22:54 +0000773 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
Florian Westphal0b67c432015-04-01 22:36:27 +0200774
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200775 if (skb->protocol == htons(ETH_P_IP))
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200776 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200777
778 if (skb->protocol == htons(ETH_P_IPV6))
779 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
Florian Westphal0b67c432015-04-01 22:36:27 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 in = nf_bridge->physindev;
Florian Westphala1e67952015-04-02 14:31:45 +0200782 if (nf_bridge->pkt_otherhost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 skb->pkt_type = PACKET_OTHERHOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200784 nf_bridge->pkt_otherhost = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Bart De Schuymere94c6742010-05-13 14:55:34 +0200786 nf_bridge_update_protocol(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 } else {
788 in = *((struct net_device **)(skb->cb));
789 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700790 nf_bridge_push_encap_header(skb);
Bart De Schuymere179e632010-04-15 12:26:39 +0200791
David Miller7026b1d2015-04-05 22:19:04 -0400792 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, sk, skb,
793 in, skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
795}
796
Florian Westphal739e4502012-03-06 01:22:54 +0000797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/* This is the 'purely bridged' case. For IP, we pass the packet to
799 * netfilter with indev and outdev set to the bridge device,
800 * but we are still able to filter on the 'real' indev/outdev
801 * because of the physdev module. For ARP, indev and outdev are the
802 * bridge ports. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200803static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
804 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400805 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800808 struct net_device *parent;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200809 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (!skb->nf_bridge)
812 return NF_ACCEPT;
813
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800814 /* Need exclusive nf_bridge_info since we might have multiple
815 * different physoutdevs. */
816 if (!nf_bridge_unshare(skb))
817 return NF_DROP;
818
Florian Westphal38330782015-04-02 14:31:43 +0200819 nf_bridge = nf_bridge_info_get(skb);
820 if (!nf_bridge)
821 return NF_DROP;
822
David S. Miller238e54c2015-04-03 20:32:56 -0400823 parent = bridge_parent(state->out);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800824 if (!parent)
825 return NF_DROP;
826
Florian Westphal739e4502012-03-06 01:22:54 +0000827 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000828 pf = NFPROTO_IPV4;
Florian Westphal739e4502012-03-06 01:22:54 +0000829 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000830 pf = NFPROTO_IPV6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000831 else
832 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Herbert Xu3db05fe2007-10-15 00:53:15 -0700834 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (skb->pkt_type == PACKET_OTHERHOST) {
837 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200838 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Florian Westphal0b67c432015-04-01 22:36:27 +0200841 if (pf == NFPROTO_IPV4) {
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200842 if (br_validate_ipv4(skb))
Florian Westphal0b67c432015-04-01 22:36:27 +0200843 return NF_DROP;
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200844 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
Florian Westphal0b67c432015-04-01 22:36:27 +0200845 }
Herbert Xu6b1e9602011-03-18 05:27:28 +0000846
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200847 if (pf == NFPROTO_IPV6) {
848 if (br_validate_ipv6(skb))
849 return NF_DROP;
850 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 nf_bridge->physoutdev = skb->dev;
Alban Crequyaa740f42012-05-14 03:56:36 +0000854 if (pf == NFPROTO_IPV4)
Bart De Schuymere179e632010-04-15 12:26:39 +0200855 skb->protocol = htons(ETH_P_IP);
856 else
857 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
David Miller7026b1d2015-04-05 22:19:04 -0400859 NF_HOOK(pf, NF_INET_FORWARD, NULL, skb,
860 brnf_get_logical_dev(skb, state->in),
David S. Miller238e54c2015-04-03 20:32:56 -0400861 parent, br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 return NF_STOLEN;
864}
865
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200866static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
867 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400868 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Patrick McHardy4df53d82010-07-02 09:32:57 +0200870 struct net_bridge_port *p;
871 struct net_bridge *br;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct net_device **d = (struct net_device **)(skb->cb);
873
David S. Miller238e54c2015-04-03 20:32:56 -0400874 p = br_port_get_rcu(state->out);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200875 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200877 br = p->br;
878
879 if (!brnf_call_arptables && !br->nf_call_arptables)
880 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Florian Westphal739e4502012-03-06 01:22:54 +0000882 if (!IS_ARP(skb)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800883 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700885 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300888 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700889 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700890 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return NF_ACCEPT;
892 }
David S. Miller238e54c2015-04-03 20:32:56 -0400893 *d = state->in;
David Miller7026b1d2015-04-05 22:19:04 -0400894 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->sk, skb,
895 state->in, state->out, br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 return NF_STOLEN;
898}
899
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200900#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
David Miller7026b1d2015-04-05 22:19:04 -0400901static int br_nf_push_frag_xmit(struct sock *sk, struct sk_buff *skb)
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100902{
Florian Westphale70deec2015-04-02 14:31:40 +0200903 struct brnf_frag_data *data;
904 int err;
905
906 data = this_cpu_ptr(&brnf_frag_data_storage);
907 err = skb_cow_head(skb, data->size);
908
909 if (err) {
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100910 kfree_skb(skb);
911 return 0;
912 }
913
Florian Westphald7b59742015-06-05 13:27:13 +0200914 if (data->vlan_tci) {
915 skb->vlan_tci = data->vlan_tci;
916 skb->vlan_proto = data->vlan_proto;
917 }
918
Florian Westphale70deec2015-04-02 14:31:40 +0200919 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
920 __skb_push(skb, data->encap_size);
921
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200922 nf_bridge_info_free(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400923 return br_dev_queue_push_xmit(sk, skb);
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100924}
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200925#endif
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100926
Andy Zhou49d16b22015-05-15 14:15:37 -0700927static int br_nf_ip_fragment(struct sock *sk, struct sk_buff *skb,
928 int (*output)(struct sock *, struct sk_buff *))
929{
930 unsigned int mtu = ip_skb_dst_mtu(skb);
931 struct iphdr *iph = ip_hdr(skb);
932 struct rtable *rt = skb_rtable(skb);
933 struct net_device *dev = rt->dst.dev;
934
935 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
936 (IPCB(skb)->frag_max_size &&
937 IPCB(skb)->frag_max_size > mtu))) {
938 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
939 kfree_skb(skb);
940 return -EMSGSIZE;
941 }
942
943 return ip_do_fragment(sk, skb, output);
944}
945
Florian Westphal33b1f312015-06-05 13:28:38 +0200946static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
947{
948 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
949 return PPPOE_SES_HLEN;
950 return 0;
951}
952
David Miller7026b1d2015-04-05 22:19:04 -0400953static int br_nf_dev_queue_xmit(struct sock *sk, struct sk_buff *skb)
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700954{
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200955 struct nf_bridge_info *nf_bridge;
Florian Westphal7a8d8312015-03-05 00:52:36 +0100956 unsigned int mtu_reserved;
Bandan Das462fb2a2010-09-19 09:34:33 +0000957
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200958 mtu_reserved = nf_bridge_mtu_reduction(skb);
959
960 if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200961 nf_bridge_info_free(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400962 return br_dev_queue_push_xmit(sk, skb);
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200963 }
Florian Westphal7a8d8312015-03-05 00:52:36 +0100964
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200965 nf_bridge = nf_bridge_info_get(skb);
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200966
967#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
Herbert Xu93fdd472014-10-05 12:00:22 +0800968 /* This is wrong! We should preserve the original fragment
969 * boundaries by preserving frag_list rather than refragmenting.
970 */
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200971 if (skb->protocol == htons(ETH_P_IP)) {
Florian Westphale70deec2015-04-02 14:31:40 +0200972 struct brnf_frag_data *data;
973
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200974 if (br_validate_ipv4(skb))
Bandan Das462fb2a2010-09-19 09:34:33 +0000975 return NF_DROP;
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200976
977 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
Florian Westphale70deec2015-04-02 14:31:40 +0200978
979 nf_bridge_update_protocol(skb);
980
981 data = this_cpu_ptr(&brnf_frag_data_storage);
Florian Westphald7b59742015-06-05 13:27:13 +0200982
983 data->vlan_tci = skb->vlan_tci;
984 data->vlan_proto = skb->vlan_proto;
Florian Westphale70deec2015-04-02 14:31:40 +0200985 data->encap_size = nf_bridge_encap_header_len(skb);
986 data->size = ETH_HLEN + data->encap_size;
987
988 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
989 data->size);
990
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200991 return br_nf_ip_fragment(sk, skb, br_nf_push_frag_xmit);
Florian Westphale70deec2015-04-02 14:31:40 +0200992 }
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200993#endif
994#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
995 if (skb->protocol == htons(ETH_P_IPV6)) {
996 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
997 struct brnf_frag_data *data;
Bandan Das462fb2a2010-09-19 09:34:33 +0000998
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200999 if (br_validate_ipv6(skb))
1000 return NF_DROP;
1001
1002 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
1003
1004 nf_bridge_update_protocol(skb);
1005
1006 data = this_cpu_ptr(&brnf_frag_data_storage);
1007 data->encap_size = nf_bridge_encap_header_len(skb);
1008 data->size = ETH_HLEN + data->encap_size;
1009
1010 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
1011 data->size);
1012
1013 if (v6ops)
1014 return v6ops->fragment(sk, skb, br_nf_push_frag_xmit);
1015 else
1016 return -EMSGSIZE;
1017 }
1018#endif
Florian Westphala9fcc6a2015-05-03 22:06:07 +02001019 nf_bridge_info_free(skb);
1020 return br_dev_queue_push_xmit(sk, skb);
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +02001021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023/* PF_BRIDGE/POST_ROUTING ********************************************/
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001024static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
1025 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001026 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Florian Westphal38330782015-04-02 14:31:43 +02001028 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct net_device *realoutdev = bridge_parent(skb->dev);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001030 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Florian Westphale4bb9bc2015-03-10 10:36:48 +01001032 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
1033 * on a bridge, but was delivered locally and is now being routed:
1034 *
1035 * POST_ROUTING was already invoked from the ip stack.
1036 */
1037 if (!nf_bridge || !nf_bridge->physoutdev)
Patrick McHardy81d9dda2007-11-13 02:58:44 -08001038 return NF_ACCEPT;
1039
Stephen Hemminger5dce9712006-02-09 17:09:38 -08001040 if (!realoutdev)
1041 return NF_DROP;
1042
Florian Westphal739e4502012-03-06 01:22:54 +00001043 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +00001044 pf = NFPROTO_IPV4;
Florian Westphal739e4502012-03-06 01:22:54 +00001045 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +00001046 pf = NFPROTO_IPV6;
Herbert Xua2bd40a2009-01-12 00:06:02 +00001047 else
1048 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
1051 * about the value of skb->pkt_type. */
1052 if (skb->pkt_type == PACKET_OTHERHOST) {
1053 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +02001054 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Patrick McHardyfc385822007-05-03 03:36:16 -07001057 nf_bridge_pull_encap_header(skb);
Alban Crequyaa740f42012-05-14 03:56:36 +00001058 if (pf == NFPROTO_IPV4)
Bart De Schuymere179e632010-04-15 12:26:39 +02001059 skb->protocol = htons(ETH_P_IP);
1060 else
1061 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
David Miller7026b1d2015-04-05 22:19:04 -04001063 NF_HOOK(pf, NF_INET_POST_ROUTING, state->sk, skb,
1064 NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -07001065 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/* IP/SABOTAGE *****************************************************/
1071/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
1072 * for the second time. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001073static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
1074 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001075 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Herbert Xu3db05fe2007-10-15 00:53:15 -07001077 if (skb->nf_bridge &&
1078 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return NF_STOP;
1080 }
1081
1082 return NF_ACCEPT;
1083}
1084
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001085/* This is called when br_netfilter has called into iptables/netfilter,
1086 * and DNAT has taken place on a bridge-forwarded packet.
1087 *
1088 * neigh->output has created a new MAC header, with local br0 MAC
1089 * as saddr.
1090 *
1091 * This restores the original MAC saddr of the bridged packet
1092 * before invoking bridge forward logic to transmit the packet.
1093 */
1094static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
1095{
Florian Westphal38330782015-04-02 14:31:43 +02001096 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001097
1098 skb_pull(skb, ETH_HLEN);
1099 nf_bridge->mask &= ~BRNF_BRIDGED_DNAT;
1100
Florian Westphale70deec2015-04-02 14:31:40 +02001101 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
1102
1103 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
1104 nf_bridge->neigh_header,
1105 ETH_HLEN - ETH_ALEN);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001106 skb->dev = nf_bridge->physindev;
Florian Westphal7fb48c52015-05-03 22:05:28 +02001107
1108 nf_bridge->physoutdev = NULL;
David Miller7026b1d2015-04-05 22:19:04 -04001109 br_handle_frame_finish(NULL, skb);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001110}
1111
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001112static int br_nf_dev_xmit(struct sk_buff *skb)
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001113{
1114 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
1115 br_nf_pre_routing_finish_bridge_slow(skb);
1116 return 1;
1117 }
1118 return 0;
1119}
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001120
1121static const struct nf_br_ops br_ops = {
1122 .br_dev_xmit_hook = br_nf_dev_xmit,
1123};
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001124
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +02001125void br_netfilter_enable(void)
1126{
1127}
1128EXPORT_SYMBOL_GPL(br_netfilter_enable);
1129
Bart De Schuymerea2d9b42010-04-15 12:14:51 +02001130/* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
1131 * br_dev_queue_push_xmit is called afterwards */
Patrick McHardy19994142007-12-05 01:23:00 -08001132static struct nf_hook_ops br_nf_ops[] __read_mostly = {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001133 {
1134 .hook = br_nf_pre_routing,
1135 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001136 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001137 .hooknum = NF_BR_PRE_ROUTING,
1138 .priority = NF_BR_PRI_BRNF,
1139 },
1140 {
1141 .hook = br_nf_local_in,
1142 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001143 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001144 .hooknum = NF_BR_LOCAL_IN,
1145 .priority = NF_BR_PRI_BRNF,
1146 },
1147 {
1148 .hook = br_nf_forward_ip,
1149 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001150 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001151 .hooknum = NF_BR_FORWARD,
1152 .priority = NF_BR_PRI_BRNF - 1,
1153 },
1154 {
1155 .hook = br_nf_forward_arp,
1156 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001157 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001158 .hooknum = NF_BR_FORWARD,
1159 .priority = NF_BR_PRI_BRNF,
1160 },
1161 {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001162 .hook = br_nf_post_routing,
1163 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001164 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001165 .hooknum = NF_BR_POST_ROUTING,
1166 .priority = NF_BR_PRI_LAST,
1167 },
1168 {
1169 .hook = ip_sabotage_in,
1170 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001171 .pf = NFPROTO_IPV4,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001172 .hooknum = NF_INET_PRE_ROUTING,
1173 .priority = NF_IP_PRI_FIRST,
1174 },
1175 {
1176 .hook = ip_sabotage_in,
1177 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001178 .pf = NFPROTO_IPV6,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001179 .hooknum = NF_INET_PRE_ROUTING,
1180 .priority = NF_IP6_PRI_FIRST,
1181 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182};
1183
1184#ifdef CONFIG_SYSCTL
1185static
Joe Perchesfe2c6332013-06-11 23:04:25 -07001186int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
tanxiaojun56b148e2013-12-19 13:28:13 +08001187 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 int ret;
1190
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001191 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (write && *(int *)(ctl->data))
1194 *(int *)(ctl->data) = 1;
1195 return ret;
1196}
1197
Joe Perchesfe2c6332013-06-11 23:04:25 -07001198static struct ctl_table brnf_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .procname = "bridge-nf-call-arptables",
1201 .data = &brnf_call_arptables,
1202 .maxlen = sizeof(int),
1203 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001204 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 },
1206 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 .procname = "bridge-nf-call-iptables",
1208 .data = &brnf_call_iptables,
1209 .maxlen = sizeof(int),
1210 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001211 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 },
1213 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 .procname = "bridge-nf-call-ip6tables",
1215 .data = &brnf_call_ip6tables,
1216 .maxlen = sizeof(int),
1217 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001218 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 },
1220 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 .procname = "bridge-nf-filter-vlan-tagged",
1222 .data = &brnf_filter_vlan_tagged,
1223 .maxlen = sizeof(int),
1224 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001225 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 },
Michael Milner516299d2007-04-12 22:14:23 -07001227 {
Michael Milner516299d2007-04-12 22:14:23 -07001228 .procname = "bridge-nf-filter-pppoe-tagged",
1229 .data = &brnf_filter_pppoe_tagged,
1230 .maxlen = sizeof(int),
1231 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001232 .proc_handler = brnf_sysctl_call_tables,
Michael Milner516299d2007-04-12 22:14:23 -07001233 },
Pablo Neira Ayuso49816822012-05-08 19:36:44 +02001234 {
1235 .procname = "bridge-nf-pass-vlan-input-dev",
1236 .data = &brnf_pass_vlan_indev,
1237 .maxlen = sizeof(int),
1238 .mode = 0644,
1239 .proc_handler = brnf_sysctl_call_tables,
1240 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001241 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#endif
1244
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001245static int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001247 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001249 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001250 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return ret;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#ifdef CONFIG_SYSCTL
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +00001254 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -08001256 printk(KERN_WARNING
1257 "br_netfilter: can't register to sysctl.\n");
Geert Uytterhoeven96727232015-02-12 15:17:27 +01001258 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1259 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261#endif
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001262 RCU_INIT_POINTER(nf_br_ops, &br_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return 0;
1265}
1266
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001267static void __exit br_netfilter_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001269 RCU_INIT_POINTER(nf_br_ops, NULL);
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001270 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271#ifdef CONFIG_SYSCTL
Eric W. Biederman5dd3df12012-04-19 13:24:33 +00001272 unregister_net_sysctl_table(brnf_sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001275
1276module_init(br_netfilter_init);
1277module_exit(br_netfilter_fini);
1278
1279MODULE_LICENSE("GPL");
1280MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1281MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1282MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");