blob: 535f9dae743ec879d81636959f8c199739678e4d [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;
119};
120
121static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
122#endif
123
Florian Westphal38330782015-04-02 14:31:43 +0200124static struct nf_bridge_info *nf_bridge_info_get(const struct sk_buff *skb)
125{
126 return skb->nf_bridge;
127}
128
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200129static 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
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700137static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
138{
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000139 struct net_bridge_port *port;
140
141 port = br_port_get_rcu(dev);
142 return port ? &port->br->fake_rtable : NULL;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800145static inline struct net_device *bridge_parent(const struct net_device *dev)
146{
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000147 struct net_bridge_port *port;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800148
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000149 port = br_port_get_rcu(dev);
150 return port ? port->br->dev : NULL;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800153static 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
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800162static 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);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800172 }
Changli Gao4c3a76a2010-08-22 19:03:26 +0000173 nf_bridge_put(nf_bridge);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800174 nf_bridge = tmp;
175 }
176 return nf_bridge;
177}
178
Florian Westphal8d045162015-03-18 20:55:31 +0100179static 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
Patrick McHardyfc385822007-05-03 03:36:16 -0700191static 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)
200{
201 unsigned int len = nf_bridge_encap_header_len(skb);
202
203 skb_pull(skb, len);
204 skb->network_header += len;
205}
206
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
Bandan Das462fb2a2010-09-19 09:34:33 +0000215/* 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
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200220static int br_validate_ipv4(struct sk_buff *skb)
Bandan Das462fb2a2010-09-19 09:34:33 +0000221{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000222 const struct iphdr *iph;
Bandan Das462fb2a2010-09-19 09:34:33 +0000223 struct net_device *dev = skb->dev;
224 u32 len;
225
Sarveshwar Bandi6caab7b2012-10-10 01:15:01 +0000226 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
227 goto inhdr_error;
228
Bandan Das462fb2a2010-09-19 09:34:33 +0000229 iph = ip_hdr(skb);
Bandan Das462fb2a2010-09-19 09:34:33 +0000230
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
Eric Dumazetf8e98812011-04-12 13:39:14 -0700254 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Herbert Xu7677e862014-10-04 22:18:02 +0800255 /* 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 */
Bandan Das462fb2a2010-09-19 09:34:33 +0000260 return 0;
261
262inhdr_error:
263 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
264drop:
265 return -1;
266}
267
Bernhard Thalera4611d32015-05-30 15:29:38 +0200268/* 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
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200324/* 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
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100373static void nf_bridge_update_protocol(struct sk_buff *skb)
374{
Florian Westphal3eaf4022015-04-02 14:31:44 +0200375 switch (skb->nf_bridge->orig_proto) {
376 case BRNF_PROTO_8021Q:
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100377 skb->protocol = htons(ETH_P_8021Q);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200378 break;
379 case BRNF_PROTO_PPPOE:
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100380 skb->protocol = htons(ETH_P_PPP_SES);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200381 break;
382 case BRNF_PROTO_UNCHANGED:
383 break;
384 }
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100385}
386
Bart De Schuymere179e632010-04-15 12:26:39 +0200387/* 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 */
David Miller7026b1d2015-04-05 22:19:04 -0400392static int br_nf_pre_routing_finish_bridge(struct sock *sk, struct sk_buff *skb)
Bart De Schuymere179e632010-04-15 12:26:39 +0200393{
David S. Millerf6b72b62011-07-14 07:53:20 -0700394 struct neighbour *neigh;
Bart De Schuymere179e632010-04-15 12:26:39 +0200395 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);
David S. Millerf9d75162012-07-02 22:12:59 -0700401 neigh = dst_neigh_lookup_skb(dst, skb);
402 if (neigh) {
Florian Westphal38330782015-04-02 14:31:43 +0200403 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700404 int ret;
405
406 if (neigh->hh.hh_len) {
407 neigh_hh_bridge(&neigh->hh, skb);
408 skb->dev = nf_bridge->physindev;
David Miller7026b1d2015-04-05 22:19:04 -0400409 ret = br_handle_frame_finish(sk, skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700410 } 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),
Florian Westphale70deec2015-04-02 14:31:40 +0200417 nf_bridge->neigh_header,
David S. Millerf9d75162012-07-02 22:12:59 -0700418 ETH_HLEN-ETH_ALEN);
419 /* tell br_dev_xmit to continue with forwarding */
420 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
Herbert Xu93fdd472014-10-05 12:00:22 +0800421 /* FIXME Need to refragment */
David S. Millerf9d75162012-07-02 22:12:59 -0700422 ret = neigh->output(neigh, skb);
423 }
424 neigh_release(neigh);
425 return ret;
Bart De Schuymere179e632010-04-15 12:26:39 +0200426 }
427free_skb:
428 kfree_skb(skb);
429 return 0;
430}
431
Florian Westphalfaecbb42015-05-20 13:42:25 +0200432static bool daddr_was_changed(const struct sk_buff *skb,
433 const struct nf_bridge_info *nf_bridge)
Florian Westphalc055d5b2015-03-10 05:08:19 +0100434{
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200435 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 }
Florian Westphalc055d5b2015-03-10 05:08:19 +0100444}
445
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200446/* 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.
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200450 */
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;
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200455 struct net_device *dev = skb->dev;
456 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200457
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200458 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
459
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200460 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;
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200465 if (daddr_was_changed(skb, nf_bridge)) {
466 skb_dst_drop(skb);
467 v6ops->route_input(skb);
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200468
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200469 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);
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200493 }
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200494
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/* This requires some explaining. If DNAT has taken place,
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200506 * we will need to fix up the destination Ethernet address.
Florian Westphalfaecbb42015-05-20 13:42:25 +0200507 * This is also true when SNAT takes place (for the reply direction).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *
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 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200521 * Let's first consider the case that ip_route_input() succeeds:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200523 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Otherwise, the packet is considered to be routed and we just
527 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800528 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200532 * Let's now consider the case that ip_route_input() fails:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800534 * This can be because the destination address is martian, in which case
535 * the packet will be dropped.
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200536 * 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()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * thinks we're handling a locally generated packet and won't care
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200540 * 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 */
David Miller7026b1d2015-04-05 22:19:04 -0400544static int br_nf_pre_routing_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700547 struct iphdr *iph = ip_hdr(skb);
Florian Westphal38330782015-04-02 14:31:43 +0200548 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000549 struct rtable *rt;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800550 int err;
Herbert Xu93fdd472014-10-05 12:00:22 +0800551
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200552 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Florian Westphala1e67952015-04-02 14:31:45 +0200554 if (nf_bridge->pkt_otherhost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 skb->pkt_type = PACKET_OTHERHOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200556 nf_bridge->pkt_otherhost = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Bernhard Thalerd39a33e2015-05-30 15:26:13 +0200558 nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING;
Florian Westphalfaecbb42015-05-20 13:42:25 +0200559 if (daddr_was_changed(skb, nf_bridge)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800560 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Eric Dumazetf3abc9b2009-08-24 19:35:38 +0200561 struct in_device *in_dev = __in_dev_get_rcu(dev);
Bart De Schuymerf216f082006-12-05 13:45:21 -0800562
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
David S. Miller78fbfd82011-03-12 00:00:52 -0500573 rt = ip_route_output(dev_net(dev), iph->daddr, 0,
574 RT_TOS(iph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800575 if (!IS_ERR(rt)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700576 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800577 * require ip_forwarding. */
David S. Millerb23dd4f2011-03-02 14:31:35 -0800578 if (rt->dst.dev == dev) {
579 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto bridged_dnat;
581 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800582 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800584free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 kfree_skb(skb);
586 return 0;
587 } else {
Eric Dumazetadf30902009-06-02 05:19:30 +0000588 if (skb_dst(skb)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589bridged_dnat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200591 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700592 nf_bridge_push_encap_header(skb);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100593 NF_HOOK_THRESH(NFPROTO_BRIDGE,
594 NF_BR_PRE_ROUTING,
David Miller7026b1d2015-04-05 22:19:04 -0400595 sk, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 br_nf_pre_routing_finish_bridge,
597 1);
598 return 0;
599 }
Joe Perches04091142014-02-23 00:05:26 -0800600 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 skb->pkt_type = PACKET_HOST;
602 }
603 } else {
Eric Dumazet511c3f92009-06-02 05:14:27 +0000604 rt = bridge_parent_rtable(nf_bridge->physindev);
605 if (!rt) {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700606 kfree_skb(skb);
607 return 0;
608 }
Patrick McHardyf9181f42010-06-15 17:31:06 +0200609 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200613 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700614 nf_bridge_push_encap_header(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400615 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
616 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 br_handle_frame_finish, 1);
618
619 return 0;
620}
621
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200622static 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);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100627 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200628 return br;
629
dingtianhongf06c7f92014-05-09 14:58:05 +0800630 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100631 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200632
633 return vlan ? vlan : br;
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800637static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Florian Westphal38330782015-04-02 14:31:43 +0200639 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (skb->pkt_type == PACKET_OTHERHOST) {
642 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200643 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
647 nf_bridge->physindev = skb->dev;
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200648 skb->dev = brnf_get_logical_dev(skb, skb->dev);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200649
Bart De Schuymere179e632010-04-15 12:26:39 +0200650 if (skb->protocol == htons(ETH_P_8021Q))
Florian Westphal3eaf4022015-04-02 14:31:44 +0200651 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
Bart De Schuymere179e632010-04-15 12:26:39 +0200652 else if (skb->protocol == htons(ETH_P_PPP_SES))
Florian Westphal3eaf4022015-04-02 14:31:44 +0200653 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800654
Florian Westphal6b8dbcf2013-10-24 21:32:42 +0200655 /* Must drop socket now because of tproxy. */
656 skb_orphan(skb);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800657 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/* Replicate the checks that IPv6 does on packet reception and pass the packet
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200661 * to ip6tables.
662 */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200663static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800664 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400665 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200667 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200669 if (br_validate_ipv6(skb))
Herbert Xudeef4b52010-12-09 17:38:11 +0000670 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800672 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800673 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800675 if (!setup_pre_routing(skb))
676 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Bernhard Thaler72b31f72015-05-30 15:27:40 +0200678 nf_bridge = nf_bridge_info_get(skb);
679 nf_bridge->ipv6_daddr = ipv6_hdr(skb)->daddr;
680
Bart De Schuymere179e632010-04-15 12:26:39 +0200681 skb->protocol = htons(ETH_P_IPV6);
David Miller7026b1d2015-04-05 22:19:04 -0400682 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, state->sk, skb,
683 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 br_nf_pre_routing_finish_ipv6);
685
686 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
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. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200695static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
696 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400697 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Florian Westphalfaecbb42015-05-20 13:42:25 +0200699 struct nf_bridge_info *nf_bridge;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200700 struct net_bridge_port *p;
701 struct net_bridge *br;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700702 __u32 len = nf_bridge_encap_header_len(skb);
703
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700704 if (unlikely(!pskb_may_pull(skb, len)))
Herbert Xudeef4b52010-12-09 17:38:11 +0000705 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller238e54c2015-04-03 20:32:56 -0400707 p = br_port_get_rcu(state->in);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200708 if (p == NULL)
Herbert Xudeef4b52010-12-09 17:38:11 +0000709 return NF_DROP;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200710 br = p->br;
711
Florian Westphal739e4502012-03-06 01:22:54 +0000712 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
Patrick McHardy4df53d82010-07-02 09:32:57 +0200713 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200715
Patrick McHardyfc385822007-05-03 03:36:16 -0700716 nf_bridge_pull_encap_header_rcsum(skb);
David S. Miller238e54c2015-04-03 20:32:56 -0400717 return br_nf_pre_routing_ipv6(ops, skb, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Patrick McHardy4df53d82010-07-02 09:32:57 +0200719
720 if (!brnf_call_iptables && !br->nf_call_iptables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Florian Westphal739e4502012-03-06 01:22:54 +0000723 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return NF_ACCEPT;
725
Patrick McHardyfc385822007-05-03 03:36:16 -0700726 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200728 if (br_validate_ipv4(skb))
Herbert Xudeef4b52010-12-09 17:38:11 +0000729 return NF_DROP;
Herbert Xu17762062010-07-05 21:29:28 +0000730
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800731 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800732 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800734 if (!setup_pre_routing(skb))
735 return NF_DROP;
Florian Westphalc055d5b2015-03-10 05:08:19 +0100736
Florian Westphalfaecbb42015-05-20 13:42:25 +0200737 nf_bridge = nf_bridge_info_get(skb);
738 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
739
Bart De Schuymere179e632010-04-15 12:26:39 +0200740 skb->protocol = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
David Miller7026b1d2015-04-05 22:19:04 -0400742 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->sk, skb,
743 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 br_nf_pre_routing_finish);
745
746 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
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. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200757static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
758 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400759 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Peter Huang (Peng)a881e962012-04-19 20:12:51 +0000761 br_drop_fake_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return NF_ACCEPT;
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/* PF_BRIDGE/FORWARD *************************************************/
David Miller7026b1d2015-04-05 22:19:04 -0400766static int br_nf_forward_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Florian Westphal38330782015-04-02 14:31:43 +0200768 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Florian Westphal739e4502012-03-06 01:22:54 +0000771 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
Florian Westphal0b67c432015-04-01 22:36:27 +0200772
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200773 if (skb->protocol == htons(ETH_P_IP))
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200774 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200775
776 if (skb->protocol == htons(ETH_P_IPV6))
777 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
Florian Westphal0b67c432015-04-01 22:36:27 +0200778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 in = nf_bridge->physindev;
Florian Westphala1e67952015-04-02 14:31:45 +0200780 if (nf_bridge->pkt_otherhost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 skb->pkt_type = PACKET_OTHERHOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200782 nf_bridge->pkt_otherhost = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Bart De Schuymere94c6742010-05-13 14:55:34 +0200784 nf_bridge_update_protocol(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 } else {
786 in = *((struct net_device **)(skb->cb));
787 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700788 nf_bridge_push_encap_header(skb);
Bart De Schuymere179e632010-04-15 12:26:39 +0200789
David Miller7026b1d2015-04-05 22:19:04 -0400790 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, sk, skb,
791 in, skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
Florian Westphal739e4502012-03-06 01:22:54 +0000795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/* 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. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200801static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
802 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400803 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800806 struct net_device *parent;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200807 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (!skb->nf_bridge)
810 return NF_ACCEPT;
811
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800812 /* Need exclusive nf_bridge_info since we might have multiple
813 * different physoutdevs. */
814 if (!nf_bridge_unshare(skb))
815 return NF_DROP;
816
Florian Westphal38330782015-04-02 14:31:43 +0200817 nf_bridge = nf_bridge_info_get(skb);
818 if (!nf_bridge)
819 return NF_DROP;
820
David S. Miller238e54c2015-04-03 20:32:56 -0400821 parent = bridge_parent(state->out);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800822 if (!parent)
823 return NF_DROP;
824
Florian Westphal739e4502012-03-06 01:22:54 +0000825 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000826 pf = NFPROTO_IPV4;
Florian Westphal739e4502012-03-06 01:22:54 +0000827 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000828 pf = NFPROTO_IPV6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000829 else
830 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Herbert Xu3db05fe2007-10-15 00:53:15 -0700832 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (skb->pkt_type == PACKET_OTHERHOST) {
835 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200836 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Florian Westphal0b67c432015-04-01 22:36:27 +0200839 if (pf == NFPROTO_IPV4) {
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200840 if (br_validate_ipv4(skb))
Florian Westphal0b67c432015-04-01 22:36:27 +0200841 return NF_DROP;
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200842 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
Florian Westphal0b67c432015-04-01 22:36:27 +0200843 }
Herbert Xu6b1e9602011-03-18 05:27:28 +0000844
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200845 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 nf_bridge->physoutdev = skb->dev;
Alban Crequyaa740f42012-05-14 03:56:36 +0000852 if (pf == NFPROTO_IPV4)
Bart De Schuymere179e632010-04-15 12:26:39 +0200853 skb->protocol = htons(ETH_P_IP);
854 else
855 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
David Miller7026b1d2015-04-05 22:19:04 -0400857 NF_HOOK(pf, NF_INET_FORWARD, NULL, skb,
858 brnf_get_logical_dev(skb, state->in),
David S. Miller238e54c2015-04-03 20:32:56 -0400859 parent, br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 return NF_STOLEN;
862}
863
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200864static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
865 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400866 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Patrick McHardy4df53d82010-07-02 09:32:57 +0200868 struct net_bridge_port *p;
869 struct net_bridge *br;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct net_device **d = (struct net_device **)(skb->cb);
871
David S. Miller238e54c2015-04-03 20:32:56 -0400872 p = br_port_get_rcu(state->out);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200873 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200875 br = p->br;
876
877 if (!brnf_call_arptables && !br->nf_call_arptables)
878 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Florian Westphal739e4502012-03-06 01:22:54 +0000880 if (!IS_ARP(skb)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800881 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700883 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300886 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700887 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700888 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return NF_ACCEPT;
890 }
David S. Miller238e54c2015-04-03 20:32:56 -0400891 *d = state->in;
David Miller7026b1d2015-04-05 22:19:04 -0400892 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->sk, skb,
893 state->in, state->out, br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 return NF_STOLEN;
896}
897
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200898#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
David Miller7026b1d2015-04-05 22:19:04 -0400899static int br_nf_push_frag_xmit(struct sock *sk, struct sk_buff *skb)
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100900{
Florian Westphale70deec2015-04-02 14:31:40 +0200901 struct brnf_frag_data *data;
902 int err;
903
904 data = this_cpu_ptr(&brnf_frag_data_storage);
905 err = skb_cow_head(skb, data->size);
906
907 if (err) {
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100908 kfree_skb(skb);
909 return 0;
910 }
911
Florian Westphale70deec2015-04-02 14:31:40 +0200912 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
913 __skb_push(skb, data->encap_size);
914
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200915 nf_bridge_info_free(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400916 return br_dev_queue_push_xmit(sk, skb);
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100917}
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200918#endif
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100919
Andy Zhou49d16b22015-05-15 14:15:37 -0700920static 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
David Miller7026b1d2015-04-05 22:19:04 -0400939static int br_nf_dev_queue_xmit(struct sock *sk, struct sk_buff *skb)
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700940{
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200941 struct nf_bridge_info *nf_bridge;
Florian Westphal7a8d8312015-03-05 00:52:36 +0100942 unsigned int mtu_reserved;
Bandan Das462fb2a2010-09-19 09:34:33 +0000943
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200944 mtu_reserved = nf_bridge_mtu_reduction(skb);
945
946 if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200947 nf_bridge_info_free(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400948 return br_dev_queue_push_xmit(sk, skb);
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200949 }
Florian Westphal7a8d8312015-03-05 00:52:36 +0100950
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200951 nf_bridge = nf_bridge_info_get(skb);
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200952
953#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
Herbert Xu93fdd472014-10-05 12:00:22 +0800954 /* This is wrong! We should preserve the original fragment
955 * boundaries by preserving frag_list rather than refragmenting.
956 */
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200957 if (skb->protocol == htons(ETH_P_IP)) {
Florian Westphale70deec2015-04-02 14:31:40 +0200958 struct brnf_frag_data *data;
959
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200960 if (br_validate_ipv4(skb))
Bandan Das462fb2a2010-09-19 09:34:33 +0000961 return NF_DROP;
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200962
963 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
Florian Westphale70deec2015-04-02 14:31:40 +0200964
965 nf_bridge_update_protocol(skb);
966
967 data = this_cpu_ptr(&brnf_frag_data_storage);
968 data->encap_size = nf_bridge_encap_header_len(skb);
969 data->size = ETH_HLEN + data->encap_size;
970
971 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
972 data->size);
973
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200974 return br_nf_ip_fragment(sk, skb, br_nf_push_frag_xmit);
Florian Westphale70deec2015-04-02 14:31:40 +0200975 }
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200976#endif
977#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
978 if (skb->protocol == htons(ETH_P_IPV6)) {
979 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
980 struct brnf_frag_data *data;
Bandan Das462fb2a2010-09-19 09:34:33 +0000981
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200982 if (br_validate_ipv6(skb))
983 return NF_DROP;
984
985 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
986
987 nf_bridge_update_protocol(skb);
988
989 data = this_cpu_ptr(&brnf_frag_data_storage);
990 data->encap_size = nf_bridge_encap_header_len(skb);
991 data->size = ETH_HLEN + data->encap_size;
992
993 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
994 data->size);
995
996 if (v6ops)
997 return v6ops->fragment(sk, skb, br_nf_push_frag_xmit);
998 else
999 return -EMSGSIZE;
1000 }
1001#endif
Florian Westphala9fcc6a2015-05-03 22:06:07 +02001002 nf_bridge_info_free(skb);
1003 return br_dev_queue_push_xmit(sk, skb);
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +02001004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006/* PF_BRIDGE/POST_ROUTING ********************************************/
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001007static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
1008 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001009 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Florian Westphal38330782015-04-02 14:31:43 +02001011 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct net_device *realoutdev = bridge_parent(skb->dev);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001013 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Florian Westphale4bb9bc2015-03-10 10:36:48 +01001015 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
1016 * on a bridge, but was delivered locally and is now being routed:
1017 *
1018 * POST_ROUTING was already invoked from the ip stack.
1019 */
1020 if (!nf_bridge || !nf_bridge->physoutdev)
Patrick McHardy81d9dda2007-11-13 02:58:44 -08001021 return NF_ACCEPT;
1022
Stephen Hemminger5dce9712006-02-09 17:09:38 -08001023 if (!realoutdev)
1024 return NF_DROP;
1025
Florian Westphal739e4502012-03-06 01:22:54 +00001026 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +00001027 pf = NFPROTO_IPV4;
Florian Westphal739e4502012-03-06 01:22:54 +00001028 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +00001029 pf = NFPROTO_IPV6;
Herbert Xua2bd40a2009-01-12 00:06:02 +00001030 else
1031 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
1034 * about the value of skb->pkt_type. */
1035 if (skb->pkt_type == PACKET_OTHERHOST) {
1036 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +02001037 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Patrick McHardyfc385822007-05-03 03:36:16 -07001040 nf_bridge_pull_encap_header(skb);
Alban Crequyaa740f42012-05-14 03:56:36 +00001041 if (pf == NFPROTO_IPV4)
Bart De Schuymere179e632010-04-15 12:26:39 +02001042 skb->protocol = htons(ETH_P_IP);
1043 else
1044 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
David Miller7026b1d2015-04-05 22:19:04 -04001046 NF_HOOK(pf, NF_INET_POST_ROUTING, state->sk, skb,
1047 NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -07001048 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/* IP/SABOTAGE *****************************************************/
1054/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
1055 * for the second time. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001056static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
1057 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001058 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Herbert Xu3db05fe2007-10-15 00:53:15 -07001060 if (skb->nf_bridge &&
1061 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return NF_STOP;
1063 }
1064
1065 return NF_ACCEPT;
1066}
1067
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001068/* This is called when br_netfilter has called into iptables/netfilter,
1069 * and DNAT has taken place on a bridge-forwarded packet.
1070 *
1071 * neigh->output has created a new MAC header, with local br0 MAC
1072 * as saddr.
1073 *
1074 * This restores the original MAC saddr of the bridged packet
1075 * before invoking bridge forward logic to transmit the packet.
1076 */
1077static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
1078{
Florian Westphal38330782015-04-02 14:31:43 +02001079 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001080
1081 skb_pull(skb, ETH_HLEN);
1082 nf_bridge->mask &= ~BRNF_BRIDGED_DNAT;
1083
Florian Westphale70deec2015-04-02 14:31:40 +02001084 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
1085
1086 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
1087 nf_bridge->neigh_header,
1088 ETH_HLEN - ETH_ALEN);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001089 skb->dev = nf_bridge->physindev;
Florian Westphal7fb48c52015-05-03 22:05:28 +02001090
1091 nf_bridge->physoutdev = NULL;
David Miller7026b1d2015-04-05 22:19:04 -04001092 br_handle_frame_finish(NULL, skb);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001093}
1094
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001095static int br_nf_dev_xmit(struct sk_buff *skb)
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001096{
1097 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
1098 br_nf_pre_routing_finish_bridge_slow(skb);
1099 return 1;
1100 }
1101 return 0;
1102}
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001103
1104static const struct nf_br_ops br_ops = {
1105 .br_dev_xmit_hook = br_nf_dev_xmit,
1106};
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +01001107
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +02001108void br_netfilter_enable(void)
1109{
1110}
1111EXPORT_SYMBOL_GPL(br_netfilter_enable);
1112
Bart De Schuymerea2d9b42010-04-15 12:14:51 +02001113/* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
1114 * br_dev_queue_push_xmit is called afterwards */
Patrick McHardy19994142007-12-05 01:23:00 -08001115static struct nf_hook_ops br_nf_ops[] __read_mostly = {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001116 {
1117 .hook = br_nf_pre_routing,
1118 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001119 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001120 .hooknum = NF_BR_PRE_ROUTING,
1121 .priority = NF_BR_PRI_BRNF,
1122 },
1123 {
1124 .hook = br_nf_local_in,
1125 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001126 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001127 .hooknum = NF_BR_LOCAL_IN,
1128 .priority = NF_BR_PRI_BRNF,
1129 },
1130 {
1131 .hook = br_nf_forward_ip,
1132 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001133 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001134 .hooknum = NF_BR_FORWARD,
1135 .priority = NF_BR_PRI_BRNF - 1,
1136 },
1137 {
1138 .hook = br_nf_forward_arp,
1139 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001140 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001141 .hooknum = NF_BR_FORWARD,
1142 .priority = NF_BR_PRI_BRNF,
1143 },
1144 {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001145 .hook = br_nf_post_routing,
1146 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001147 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001148 .hooknum = NF_BR_POST_ROUTING,
1149 .priority = NF_BR_PRI_LAST,
1150 },
1151 {
1152 .hook = ip_sabotage_in,
1153 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001154 .pf = NFPROTO_IPV4,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001155 .hooknum = NF_INET_PRE_ROUTING,
1156 .priority = NF_IP_PRI_FIRST,
1157 },
1158 {
1159 .hook = ip_sabotage_in,
1160 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +00001161 .pf = NFPROTO_IPV6,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +00001162 .hooknum = NF_INET_PRE_ROUTING,
1163 .priority = NF_IP6_PRI_FIRST,
1164 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165};
1166
1167#ifdef CONFIG_SYSCTL
1168static
Joe Perchesfe2c6332013-06-11 23:04:25 -07001169int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
tanxiaojun56b148e2013-12-19 13:28:13 +08001170 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int ret;
1173
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001174 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if (write && *(int *)(ctl->data))
1177 *(int *)(ctl->data) = 1;
1178 return ret;
1179}
1180
Joe Perchesfe2c6332013-06-11 23:04:25 -07001181static struct ctl_table brnf_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 .procname = "bridge-nf-call-arptables",
1184 .data = &brnf_call_arptables,
1185 .maxlen = sizeof(int),
1186 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001187 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 },
1189 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 .procname = "bridge-nf-call-iptables",
1191 .data = &brnf_call_iptables,
1192 .maxlen = sizeof(int),
1193 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001194 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 },
1196 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 .procname = "bridge-nf-call-ip6tables",
1198 .data = &brnf_call_ip6tables,
1199 .maxlen = sizeof(int),
1200 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001201 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 },
1203 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 .procname = "bridge-nf-filter-vlan-tagged",
1205 .data = &brnf_filter_vlan_tagged,
1206 .maxlen = sizeof(int),
1207 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001208 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 },
Michael Milner516299d2007-04-12 22:14:23 -07001210 {
Michael Milner516299d2007-04-12 22:14:23 -07001211 .procname = "bridge-nf-filter-pppoe-tagged",
1212 .data = &brnf_filter_pppoe_tagged,
1213 .maxlen = sizeof(int),
1214 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001215 .proc_handler = brnf_sysctl_call_tables,
Michael Milner516299d2007-04-12 22:14:23 -07001216 },
Pablo Neira Ayuso49816822012-05-08 19:36:44 +02001217 {
1218 .procname = "bridge-nf-pass-vlan-input-dev",
1219 .data = &brnf_pass_vlan_indev,
1220 .maxlen = sizeof(int),
1221 .mode = 0644,
1222 .proc_handler = brnf_sysctl_call_tables,
1223 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001224 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226#endif
1227
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001228static int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001232 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001233 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return ret;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236#ifdef CONFIG_SYSCTL
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +00001237 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -08001239 printk(KERN_WARNING
1240 "br_netfilter: can't register to sysctl.\n");
Geert Uytterhoeven96727232015-02-12 15:17:27 +01001241 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1242 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244#endif
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001245 RCU_INIT_POINTER(nf_br_ops, &br_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return 0;
1248}
1249
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001250static void __exit br_netfilter_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001252 RCU_INIT_POINTER(nf_br_ops, NULL);
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001253 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254#ifdef CONFIG_SYSCTL
Eric W. Biederman5dd3df12012-04-19 13:24:33 +00001255 unregister_net_sysctl_table(brnf_sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001258
1259module_init(br_netfilter_init);
1260module_exit(br_netfilter_fini);
1261
1262MODULE_LICENSE("GPL");
1263MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1264MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1265MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");