blob: d89f4fac0bc507cb3faea7782d9791d48a552fea [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 Westphala9fcc6a2015-05-03 22:06:07 +0200126static void nf_bridge_info_free(struct sk_buff *skb)
127{
128 if (skb->nf_bridge) {
129 nf_bridge_put(skb->nf_bridge);
130 skb->nf_bridge = NULL;
131 }
132}
133
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800134static inline struct net_device *bridge_parent(const struct net_device *dev)
135{
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000136 struct net_bridge_port *port;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800137
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000138 port = br_port_get_rcu(dev);
139 return port ? port->br->dev : NULL;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800142static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
143{
144 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
145
146 if (atomic_read(&nf_bridge->use) > 1) {
147 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
148
149 if (tmp) {
150 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
151 atomic_set(&tmp->use, 1);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800152 }
Changli Gao4c3a76a2010-08-22 19:03:26 +0000153 nf_bridge_put(nf_bridge);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800154 nf_bridge = tmp;
155 }
156 return nf_bridge;
157}
158
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200159unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
Florian Westphal8d045162015-03-18 20:55:31 +0100160{
161 switch (skb->protocol) {
162 case __cpu_to_be16(ETH_P_8021Q):
163 return VLAN_HLEN;
164 case __cpu_to_be16(ETH_P_PPP_SES):
165 return PPPOE_SES_HLEN;
166 default:
167 return 0;
168 }
169}
170
Patrick McHardyfc385822007-05-03 03:36:16 -0700171static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
172{
173 unsigned int len = nf_bridge_encap_header_len(skb);
174
175 skb_pull(skb, len);
176 skb->network_header += len;
177}
178
179static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
180{
181 unsigned int len = nf_bridge_encap_header_len(skb);
182
183 skb_pull_rcsum(skb, len);
184 skb->network_header += len;
185}
186
Bandan Das462fb2a2010-09-19 09:34:33 +0000187/* When handing a packet over to the IP layer
188 * check whether we have a skb that is in the
189 * expected format
190 */
191
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200192static int br_validate_ipv4(struct sk_buff *skb)
Bandan Das462fb2a2010-09-19 09:34:33 +0000193{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000194 const struct iphdr *iph;
Bandan Das462fb2a2010-09-19 09:34:33 +0000195 struct net_device *dev = skb->dev;
196 u32 len;
197
Sarveshwar Bandi6caab7b2012-10-10 01:15:01 +0000198 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
199 goto inhdr_error;
200
Bandan Das462fb2a2010-09-19 09:34:33 +0000201 iph = ip_hdr(skb);
Bandan Das462fb2a2010-09-19 09:34:33 +0000202
203 /* Basic sanity checks */
204 if (iph->ihl < 5 || iph->version != 4)
205 goto inhdr_error;
206
207 if (!pskb_may_pull(skb, iph->ihl*4))
208 goto inhdr_error;
209
210 iph = ip_hdr(skb);
211 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
212 goto inhdr_error;
213
214 len = ntohs(iph->tot_len);
215 if (skb->len < len) {
216 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
217 goto drop;
218 } else if (len < (iph->ihl*4))
219 goto inhdr_error;
220
221 if (pskb_trim_rcsum(skb, len)) {
222 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
223 goto drop;
224 }
225
Eric Dumazetf8e98812011-04-12 13:39:14 -0700226 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Herbert Xu7677e862014-10-04 22:18:02 +0800227 /* We should really parse IP options here but until
228 * somebody who actually uses IP options complains to
229 * us we'll just silently ignore the options because
230 * we're lazy!
231 */
Bandan Das462fb2a2010-09-19 09:34:33 +0000232 return 0;
233
234inhdr_error:
235 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
236drop:
237 return -1;
238}
239
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200240void nf_bridge_update_protocol(struct sk_buff *skb)
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100241{
Florian Westphal3eaf4022015-04-02 14:31:44 +0200242 switch (skb->nf_bridge->orig_proto) {
243 case BRNF_PROTO_8021Q:
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100244 skb->protocol = htons(ETH_P_8021Q);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200245 break;
246 case BRNF_PROTO_PPPOE:
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100247 skb->protocol = htons(ETH_P_PPP_SES);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200248 break;
249 case BRNF_PROTO_UNCHANGED:
250 break;
251 }
Florian Westphal4a9d2f22015-03-05 00:52:34 +0100252}
253
Bart De Schuymere179e632010-04-15 12:26:39 +0200254/* Obtain the correct destination MAC address, while preserving the original
255 * source MAC address. If we already know this address, we just copy it. If we
256 * don't, we use the neighbour framework to find out. In both cases, we make
257 * sure that br_handle_frame_finish() is called afterwards.
258 */
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200259int br_nf_pre_routing_finish_bridge(struct sock *sk, struct sk_buff *skb)
Bart De Schuymere179e632010-04-15 12:26:39 +0200260{
David S. Millerf6b72b62011-07-14 07:53:20 -0700261 struct neighbour *neigh;
Bart De Schuymere179e632010-04-15 12:26:39 +0200262 struct dst_entry *dst;
263
264 skb->dev = bridge_parent(skb->dev);
265 if (!skb->dev)
266 goto free_skb;
267 dst = skb_dst(skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700268 neigh = dst_neigh_lookup_skb(dst, skb);
269 if (neigh) {
Florian Westphal38330782015-04-02 14:31:43 +0200270 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700271 int ret;
272
273 if (neigh->hh.hh_len) {
274 neigh_hh_bridge(&neigh->hh, skb);
275 skb->dev = nf_bridge->physindev;
David Miller7026b1d2015-04-05 22:19:04 -0400276 ret = br_handle_frame_finish(sk, skb);
David S. Millerf9d75162012-07-02 22:12:59 -0700277 } else {
278 /* the neighbour function below overwrites the complete
279 * MAC header, so we save the Ethernet source address and
280 * protocol number.
281 */
282 skb_copy_from_linear_data_offset(skb,
283 -(ETH_HLEN-ETH_ALEN),
Florian Westphale70deec2015-04-02 14:31:40 +0200284 nf_bridge->neigh_header,
David S. Millerf9d75162012-07-02 22:12:59 -0700285 ETH_HLEN-ETH_ALEN);
286 /* tell br_dev_xmit to continue with forwarding */
287 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
Herbert Xu93fdd472014-10-05 12:00:22 +0800288 /* FIXME Need to refragment */
David S. Millerf9d75162012-07-02 22:12:59 -0700289 ret = neigh->output(neigh, skb);
290 }
291 neigh_release(neigh);
292 return ret;
Bart De Schuymere179e632010-04-15 12:26:39 +0200293 }
294free_skb:
295 kfree_skb(skb);
296 return 0;
297}
298
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200299static inline bool
300br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
301 const struct nf_bridge_info *nf_bridge)
Florian Westphalc055d5b2015-03-10 05:08:19 +0100302{
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200303 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
Bernhard Thaler8cae3082015-05-30 15:26:57 +0200304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/* This requires some explaining. If DNAT has taken place,
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200307 * we will need to fix up the destination Ethernet address.
Florian Westphalfaecbb42015-05-20 13:42:25 +0200308 * This is also true when SNAT takes place (for the reply direction).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
310 * There are two cases to consider:
311 * 1. The packet was DNAT'ed to a device in the same bridge
312 * port group as it was received on. We can still bridge
313 * the packet.
314 * 2. The packet was DNAT'ed to a different device, either
315 * a non-bridged device or another bridge port group.
316 * The packet will need to be routed.
317 *
318 * The correct way of distinguishing between these two cases is to
319 * call ip_route_input() and to look at skb->dst->dev, which is
320 * changed to the destination device if ip_route_input() succeeds.
321 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200322 * Let's first consider the case that ip_route_input() succeeds:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200324 * If the output device equals the logical bridge device the packet
325 * came in on, we can consider this bridging. The corresponding MAC
326 * address will be obtained in br_nf_pre_routing_finish_bridge.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Otherwise, the packet is considered to be routed and we just
328 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800329 * later be passed up to the IP stack to be routed. For a redirected
330 * packet, ip_route_input() will give back the localhost as output device,
331 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200333 * Let's now consider the case that ip_route_input() fails:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800335 * This can be because the destination address is martian, in which case
336 * the packet will be dropped.
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200337 * If IP forwarding is disabled, ip_route_input() will fail, while
338 * ip_route_output_key() can return success. The source
339 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * thinks we're handling a locally generated packet and won't care
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200341 * if IP forwarding is enabled. If the output device equals the logical bridge
342 * device, we proceed as if ip_route_input() succeeded. If it differs from the
343 * logical bridge port or if ip_route_output_key() fails we drop the packet.
344 */
David Miller7026b1d2015-04-05 22:19:04 -0400345static int br_nf_pre_routing_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700348 struct iphdr *iph = ip_hdr(skb);
Florian Westphal38330782015-04-02 14:31:43 +0200349 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000350 struct rtable *rt;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800351 int err;
Herbert Xu93fdd472014-10-05 12:00:22 +0800352
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200353 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Florian Westphala1e67952015-04-02 14:31:45 +0200355 if (nf_bridge->pkt_otherhost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 skb->pkt_type = PACKET_OTHERHOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200357 nf_bridge->pkt_otherhost = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Bernhard Thalerd39a33e2015-05-30 15:26:13 +0200359 nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING;
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200360 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800361 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Eric Dumazetf3abc9b2009-08-24 19:35:38 +0200362 struct in_device *in_dev = __in_dev_get_rcu(dev);
Bart De Schuymerf216f082006-12-05 13:45:21 -0800363
364 /* If err equals -EHOSTUNREACH the error is due to a
365 * martian destination or due to the fact that
366 * forwarding is disabled. For most martian packets,
367 * ip_route_output_key() will fail. It won't fail for 2 types of
368 * martian destinations: loopback destinations and destination
369 * 0.0.0.0. In both cases the packet will be dropped because the
370 * destination is the loopback device and not the bridge. */
371 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
372 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David S. Miller78fbfd82011-03-12 00:00:52 -0500374 rt = ip_route_output(dev_net(dev), iph->daddr, 0,
375 RT_TOS(iph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800376 if (!IS_ERR(rt)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700377 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800378 * require ip_forwarding. */
David S. Millerb23dd4f2011-03-02 14:31:35 -0800379 if (rt->dst.dev == dev) {
380 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto bridged_dnat;
382 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800383 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800385free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 kfree_skb(skb);
387 return 0;
388 } else {
Eric Dumazetadf30902009-06-02 05:19:30 +0000389 if (skb_dst(skb)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390bridged_dnat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200392 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700393 nf_bridge_push_encap_header(skb);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100394 NF_HOOK_THRESH(NFPROTO_BRIDGE,
395 NF_BR_PRE_ROUTING,
David Miller7026b1d2015-04-05 22:19:04 -0400396 sk, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 br_nf_pre_routing_finish_bridge,
398 1);
399 return 0;
400 }
Joe Perches04091142014-02-23 00:05:26 -0800401 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 skb->pkt_type = PACKET_HOST;
403 }
404 } else {
Eric Dumazet511c3f92009-06-02 05:14:27 +0000405 rt = bridge_parent_rtable(nf_bridge->physindev);
406 if (!rt) {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700407 kfree_skb(skb);
408 return 0;
409 }
Patrick McHardyf9181f42010-06-15 17:31:06 +0200410 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200414 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700415 nf_bridge_push_encap_header(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400416 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
417 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 br_handle_frame_finish, 1);
419
420 return 0;
421}
422
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200423static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
424{
425 struct net_device *vlan, *br;
426
427 br = bridge_parent(dev);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100428 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200429 return br;
430
dingtianhongf06c7f92014-05-09 14:58:05 +0800431 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100432 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200433
434 return vlan ? vlan : br;
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/* Some common code for IPv4/IPv6 */
Pablo Neira Ayuso230ac492015-06-16 14:07:03 +0200438struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Florian Westphal38330782015-04-02 14:31:43 +0200440 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (skb->pkt_type == PACKET_OTHERHOST) {
443 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200444 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
448 nf_bridge->physindev = skb->dev;
Pablo Neira Ayuso49816822012-05-08 19:36:44 +0200449 skb->dev = brnf_get_logical_dev(skb, skb->dev);
Florian Westphal3eaf4022015-04-02 14:31:44 +0200450
Bart De Schuymere179e632010-04-15 12:26:39 +0200451 if (skb->protocol == htons(ETH_P_8021Q))
Florian Westphal3eaf4022015-04-02 14:31:44 +0200452 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
Bart De Schuymere179e632010-04-15 12:26:39 +0200453 else if (skb->protocol == htons(ETH_P_PPP_SES))
Florian Westphal3eaf4022015-04-02 14:31:44 +0200454 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800455
Florian Westphal6b8dbcf2013-10-24 21:32:42 +0200456 /* Must drop socket now because of tproxy. */
457 skb_orphan(skb);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800458 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
462 * Replicate the checks that IPv4 does on packet reception.
463 * Set skb->dev to the bridge device (i.e. parent of the
464 * receiving device) to make netfilter happy, the REDIRECT
465 * target in particular. Save the original destination IP
466 * address to be able to detect DNAT afterwards. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200467static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
468 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400469 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Florian Westphalfaecbb42015-05-20 13:42:25 +0200471 struct nf_bridge_info *nf_bridge;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200472 struct net_bridge_port *p;
473 struct net_bridge *br;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700474 __u32 len = nf_bridge_encap_header_len(skb);
475
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700476 if (unlikely(!pskb_may_pull(skb, len)))
Herbert Xudeef4b52010-12-09 17:38:11 +0000477 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David S. Miller238e54c2015-04-03 20:32:56 -0400479 p = br_port_get_rcu(state->in);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200480 if (p == NULL)
Herbert Xudeef4b52010-12-09 17:38:11 +0000481 return NF_DROP;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200482 br = p->br;
483
Florian Westphal739e4502012-03-06 01:22:54 +0000484 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
Patrick McHardy4df53d82010-07-02 09:32:57 +0200485 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200487
Patrick McHardyfc385822007-05-03 03:36:16 -0700488 nf_bridge_pull_encap_header_rcsum(skb);
David S. Miller238e54c2015-04-03 20:32:56 -0400489 return br_nf_pre_routing_ipv6(ops, skb, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Patrick McHardy4df53d82010-07-02 09:32:57 +0200491
492 if (!brnf_call_iptables && !br->nf_call_iptables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Florian Westphal739e4502012-03-06 01:22:54 +0000495 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return NF_ACCEPT;
497
Patrick McHardyfc385822007-05-03 03:36:16 -0700498 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200500 if (br_validate_ipv4(skb))
Herbert Xudeef4b52010-12-09 17:38:11 +0000501 return NF_DROP;
Herbert Xu17762062010-07-05 21:29:28 +0000502
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800503 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800504 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800506 if (!setup_pre_routing(skb))
507 return NF_DROP;
Florian Westphalc055d5b2015-03-10 05:08:19 +0100508
Florian Westphalfaecbb42015-05-20 13:42:25 +0200509 nf_bridge = nf_bridge_info_get(skb);
510 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
511
Bart De Schuymere179e632010-04-15 12:26:39 +0200512 skb->protocol = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
David Miller7026b1d2015-04-05 22:19:04 -0400514 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->sk, skb,
515 skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 br_nf_pre_routing_finish);
517
518 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521
522/* PF_BRIDGE/LOCAL_IN ************************************************/
523/* The packet is locally destined, which requires a real
524 * dst_entry, so detach the fake one. On the way up, the
525 * packet would pass through PRE_ROUTING again (which already
526 * took place when the packet entered the bridge), but we
527 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
528 * prevent this from happening. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200529static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
530 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400531 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Peter Huang (Peng)a881e962012-04-19 20:12:51 +0000533 br_drop_fake_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return NF_ACCEPT;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/* PF_BRIDGE/FORWARD *************************************************/
David Miller7026b1d2015-04-05 22:19:04 -0400538static int br_nf_forward_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Florian Westphal38330782015-04-02 14:31:43 +0200540 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Florian Westphal739e4502012-03-06 01:22:54 +0000543 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
Florian Westphal0b67c432015-04-01 22:36:27 +0200544
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200545 if (skb->protocol == htons(ETH_P_IP))
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200546 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200547
548 if (skb->protocol == htons(ETH_P_IPV6))
549 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
Florian Westphal0b67c432015-04-01 22:36:27 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 in = nf_bridge->physindev;
Florian Westphala1e67952015-04-02 14:31:45 +0200552 if (nf_bridge->pkt_otherhost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 skb->pkt_type = PACKET_OTHERHOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200554 nf_bridge->pkt_otherhost = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Bart De Schuymere94c6742010-05-13 14:55:34 +0200556 nf_bridge_update_protocol(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
558 in = *((struct net_device **)(skb->cb));
559 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700560 nf_bridge_push_encap_header(skb);
Bart De Schuymere179e632010-04-15 12:26:39 +0200561
David Miller7026b1d2015-04-05 22:19:04 -0400562 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, sk, skb,
563 in, skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 0;
565}
566
Florian Westphal739e4502012-03-06 01:22:54 +0000567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/* This is the 'purely bridged' case. For IP, we pass the packet to
569 * netfilter with indev and outdev set to the bridge device,
570 * but we are still able to filter on the 'real' indev/outdev
571 * because of the physdev module. For ARP, indev and outdev are the
572 * bridge ports. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200573static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
574 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400575 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800578 struct net_device *parent;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200579 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (!skb->nf_bridge)
582 return NF_ACCEPT;
583
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800584 /* Need exclusive nf_bridge_info since we might have multiple
585 * different physoutdevs. */
586 if (!nf_bridge_unshare(skb))
587 return NF_DROP;
588
Florian Westphal38330782015-04-02 14:31:43 +0200589 nf_bridge = nf_bridge_info_get(skb);
590 if (!nf_bridge)
591 return NF_DROP;
592
David S. Miller238e54c2015-04-03 20:32:56 -0400593 parent = bridge_parent(state->out);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800594 if (!parent)
595 return NF_DROP;
596
Florian Westphal739e4502012-03-06 01:22:54 +0000597 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000598 pf = NFPROTO_IPV4;
Florian Westphal739e4502012-03-06 01:22:54 +0000599 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000600 pf = NFPROTO_IPV6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000601 else
602 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Herbert Xu3db05fe2007-10-15 00:53:15 -0700604 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (skb->pkt_type == PACKET_OTHERHOST) {
607 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200608 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Florian Westphal0b67c432015-04-01 22:36:27 +0200611 if (pf == NFPROTO_IPV4) {
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200612 if (br_validate_ipv4(skb))
Florian Westphal0b67c432015-04-01 22:36:27 +0200613 return NF_DROP;
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200614 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
Florian Westphal0b67c432015-04-01 22:36:27 +0200615 }
Herbert Xu6b1e9602011-03-18 05:27:28 +0000616
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200617 if (pf == NFPROTO_IPV6) {
618 if (br_validate_ipv6(skb))
619 return NF_DROP;
620 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
621 }
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 nf_bridge->physoutdev = skb->dev;
Alban Crequyaa740f42012-05-14 03:56:36 +0000624 if (pf == NFPROTO_IPV4)
Bart De Schuymere179e632010-04-15 12:26:39 +0200625 skb->protocol = htons(ETH_P_IP);
626 else
627 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
David Miller7026b1d2015-04-05 22:19:04 -0400629 NF_HOOK(pf, NF_INET_FORWARD, NULL, skb,
630 brnf_get_logical_dev(skb, state->in),
David S. Miller238e54c2015-04-03 20:32:56 -0400631 parent, br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return NF_STOLEN;
634}
635
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200636static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
637 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400638 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Patrick McHardy4df53d82010-07-02 09:32:57 +0200640 struct net_bridge_port *p;
641 struct net_bridge *br;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct net_device **d = (struct net_device **)(skb->cb);
643
David S. Miller238e54c2015-04-03 20:32:56 -0400644 p = br_port_get_rcu(state->out);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200645 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200647 br = p->br;
648
649 if (!brnf_call_arptables && !br->nf_call_arptables)
650 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Florian Westphal739e4502012-03-06 01:22:54 +0000652 if (!IS_ARP(skb)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800653 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700655 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300658 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700659 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700660 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return NF_ACCEPT;
662 }
David S. Miller238e54c2015-04-03 20:32:56 -0400663 *d = state->in;
David Miller7026b1d2015-04-05 22:19:04 -0400664 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->sk, skb,
665 state->in, state->out, br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return NF_STOLEN;
668}
669
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200670#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
David Miller7026b1d2015-04-05 22:19:04 -0400671static int br_nf_push_frag_xmit(struct sock *sk, struct sk_buff *skb)
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100672{
Florian Westphale70deec2015-04-02 14:31:40 +0200673 struct brnf_frag_data *data;
674 int err;
675
676 data = this_cpu_ptr(&brnf_frag_data_storage);
677 err = skb_cow_head(skb, data->size);
678
679 if (err) {
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100680 kfree_skb(skb);
681 return 0;
682 }
683
Florian Westphald7b59742015-06-05 13:27:13 +0200684 if (data->vlan_tci) {
685 skb->vlan_tci = data->vlan_tci;
686 skb->vlan_proto = data->vlan_proto;
687 }
688
Florian Westphale70deec2015-04-02 14:31:40 +0200689 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
690 __skb_push(skb, data->encap_size);
691
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200692 nf_bridge_info_free(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400693 return br_dev_queue_push_xmit(sk, skb);
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100694}
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200695#endif
Florian Westphal8bd63cf2015-03-05 00:52:33 +0100696
Andy Zhou49d16b22015-05-15 14:15:37 -0700697static int br_nf_ip_fragment(struct sock *sk, struct sk_buff *skb,
698 int (*output)(struct sock *, struct sk_buff *))
699{
700 unsigned int mtu = ip_skb_dst_mtu(skb);
701 struct iphdr *iph = ip_hdr(skb);
702 struct rtable *rt = skb_rtable(skb);
703 struct net_device *dev = rt->dst.dev;
704
705 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
706 (IPCB(skb)->frag_max_size &&
707 IPCB(skb)->frag_max_size > mtu))) {
708 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
709 kfree_skb(skb);
710 return -EMSGSIZE;
711 }
712
713 return ip_do_fragment(sk, skb, output);
714}
715
Florian Westphal33b1f312015-06-05 13:28:38 +0200716static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
717{
718 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
719 return PPPOE_SES_HLEN;
720 return 0;
721}
722
David Miller7026b1d2015-04-05 22:19:04 -0400723static int br_nf_dev_queue_xmit(struct sock *sk, struct sk_buff *skb)
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700724{
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200725 struct nf_bridge_info *nf_bridge;
Florian Westphal7a8d8312015-03-05 00:52:36 +0100726 unsigned int mtu_reserved;
Bandan Das462fb2a2010-09-19 09:34:33 +0000727
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200728 mtu_reserved = nf_bridge_mtu_reduction(skb);
729
730 if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200731 nf_bridge_info_free(skb);
David Miller7026b1d2015-04-05 22:19:04 -0400732 return br_dev_queue_push_xmit(sk, skb);
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200733 }
Florian Westphal7a8d8312015-03-05 00:52:36 +0100734
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200735 nf_bridge = nf_bridge_info_get(skb);
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200736
737#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
Herbert Xu93fdd472014-10-05 12:00:22 +0800738 /* This is wrong! We should preserve the original fragment
739 * boundaries by preserving frag_list rather than refragmenting.
740 */
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200741 if (skb->protocol == htons(ETH_P_IP)) {
Florian Westphale70deec2015-04-02 14:31:40 +0200742 struct brnf_frag_data *data;
743
Bernhard Thaler77d574e2015-05-30 15:29:02 +0200744 if (br_validate_ipv4(skb))
Bandan Das462fb2a2010-09-19 09:34:33 +0000745 return NF_DROP;
Bernhard Thaler411ffb42015-05-30 15:28:28 +0200746
747 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
Florian Westphale70deec2015-04-02 14:31:40 +0200748
749 nf_bridge_update_protocol(skb);
750
751 data = this_cpu_ptr(&brnf_frag_data_storage);
Florian Westphald7b59742015-06-05 13:27:13 +0200752
753 data->vlan_tci = skb->vlan_tci;
754 data->vlan_proto = skb->vlan_proto;
Florian Westphale70deec2015-04-02 14:31:40 +0200755 data->encap_size = nf_bridge_encap_header_len(skb);
756 data->size = ETH_HLEN + data->encap_size;
757
758 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
759 data->size);
760
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200761 return br_nf_ip_fragment(sk, skb, br_nf_push_frag_xmit);
Florian Westphale70deec2015-04-02 14:31:40 +0200762 }
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200763#endif
764#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
765 if (skb->protocol == htons(ETH_P_IPV6)) {
766 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
767 struct brnf_frag_data *data;
Bandan Das462fb2a2010-09-19 09:34:33 +0000768
Bernhard Thalerefb6de92015-05-30 15:30:16 +0200769 if (br_validate_ipv6(skb))
770 return NF_DROP;
771
772 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
773
774 nf_bridge_update_protocol(skb);
775
776 data = this_cpu_ptr(&brnf_frag_data_storage);
777 data->encap_size = nf_bridge_encap_header_len(skb);
778 data->size = ETH_HLEN + data->encap_size;
779
780 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
781 data->size);
782
783 if (v6ops)
784 return v6ops->fragment(sk, skb, br_nf_push_frag_xmit);
785 else
786 return -EMSGSIZE;
787 }
788#endif
Florian Westphala9fcc6a2015-05-03 22:06:07 +0200789 nf_bridge_info_free(skb);
790 return br_dev_queue_push_xmit(sk, skb);
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +0200791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793/* PF_BRIDGE/POST_ROUTING ********************************************/
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200794static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
795 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400796 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Florian Westphal38330782015-04-02 14:31:43 +0200798 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct net_device *realoutdev = bridge_parent(skb->dev);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200800 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Florian Westphale4bb9bc2015-03-10 10:36:48 +0100802 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
803 * on a bridge, but was delivered locally and is now being routed:
804 *
805 * POST_ROUTING was already invoked from the ip stack.
806 */
807 if (!nf_bridge || !nf_bridge->physoutdev)
Patrick McHardy81d9dda2007-11-13 02:58:44 -0800808 return NF_ACCEPT;
809
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800810 if (!realoutdev)
811 return NF_DROP;
812
Florian Westphal739e4502012-03-06 01:22:54 +0000813 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000814 pf = NFPROTO_IPV4;
Florian Westphal739e4502012-03-06 01:22:54 +0000815 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
Alban Crequyaa740f42012-05-14 03:56:36 +0000816 pf = NFPROTO_IPV6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000817 else
818 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
821 * about the value of skb->pkt_type. */
822 if (skb->pkt_type == PACKET_OTHERHOST) {
823 skb->pkt_type = PACKET_HOST;
Florian Westphala1e67952015-04-02 14:31:45 +0200824 nf_bridge->pkt_otherhost = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Patrick McHardyfc385822007-05-03 03:36:16 -0700827 nf_bridge_pull_encap_header(skb);
Alban Crequyaa740f42012-05-14 03:56:36 +0000828 if (pf == NFPROTO_IPV4)
Bart De Schuymere179e632010-04-15 12:26:39 +0200829 skb->protocol = htons(ETH_P_IP);
830 else
831 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David Miller7026b1d2015-04-05 22:19:04 -0400833 NF_HOOK(pf, NF_INET_POST_ROUTING, state->sk, skb,
834 NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700835 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/* IP/SABOTAGE *****************************************************/
841/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
842 * for the second time. */
Patrick McHardy795aa6e2013-10-10 09:21:55 +0200843static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
844 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400845 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700847 if (skb->nf_bridge &&
848 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return NF_STOP;
850 }
851
852 return NF_ACCEPT;
853}
854
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +0100855/* This is called when br_netfilter has called into iptables/netfilter,
856 * and DNAT has taken place on a bridge-forwarded packet.
857 *
858 * neigh->output has created a new MAC header, with local br0 MAC
859 * as saddr.
860 *
861 * This restores the original MAC saddr of the bridged packet
862 * before invoking bridge forward logic to transmit the packet.
863 */
864static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
865{
Florian Westphal38330782015-04-02 14:31:43 +0200866 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +0100867
868 skb_pull(skb, ETH_HLEN);
869 nf_bridge->mask &= ~BRNF_BRIDGED_DNAT;
870
Florian Westphale70deec2015-04-02 14:31:40 +0200871 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
872
873 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
874 nf_bridge->neigh_header,
875 ETH_HLEN - ETH_ALEN);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +0100876 skb->dev = nf_bridge->physindev;
Florian Westphal7fb48c52015-05-03 22:05:28 +0200877
878 nf_bridge->physoutdev = NULL;
David Miller7026b1d2015-04-05 22:19:04 -0400879 br_handle_frame_finish(NULL, skb);
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +0100880}
881
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +0100882static int br_nf_dev_xmit(struct sk_buff *skb)
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +0100883{
884 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
885 br_nf_pre_routing_finish_bridge_slow(skb);
886 return 1;
887 }
888 return 0;
889}
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +0100890
891static const struct nf_br_ops br_ops = {
892 .br_dev_xmit_hook = br_nf_dev_xmit,
893};
Pablo Neira Ayusoe5de75b2015-03-09 12:30:12 +0100894
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +0200895void br_netfilter_enable(void)
896{
897}
898EXPORT_SYMBOL_GPL(br_netfilter_enable);
899
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200900/* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
901 * br_dev_queue_push_xmit is called afterwards */
Patrick McHardy19994142007-12-05 01:23:00 -0800902static struct nf_hook_ops br_nf_ops[] __read_mostly = {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000903 {
904 .hook = br_nf_pre_routing,
905 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000906 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000907 .hooknum = NF_BR_PRE_ROUTING,
908 .priority = NF_BR_PRI_BRNF,
909 },
910 {
911 .hook = br_nf_local_in,
912 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000913 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000914 .hooknum = NF_BR_LOCAL_IN,
915 .priority = NF_BR_PRI_BRNF,
916 },
917 {
918 .hook = br_nf_forward_ip,
919 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000920 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000921 .hooknum = NF_BR_FORWARD,
922 .priority = NF_BR_PRI_BRNF - 1,
923 },
924 {
925 .hook = br_nf_forward_arp,
926 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000927 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000928 .hooknum = NF_BR_FORWARD,
929 .priority = NF_BR_PRI_BRNF,
930 },
931 {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000932 .hook = br_nf_post_routing,
933 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000934 .pf = NFPROTO_BRIDGE,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000935 .hooknum = NF_BR_POST_ROUTING,
936 .priority = NF_BR_PRI_LAST,
937 },
938 {
939 .hook = ip_sabotage_in,
940 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000941 .pf = NFPROTO_IPV4,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000942 .hooknum = NF_INET_PRE_ROUTING,
943 .priority = NF_IP_PRI_FIRST,
944 },
945 {
946 .hook = ip_sabotage_in,
947 .owner = THIS_MODULE,
Alban Crequyaa740f42012-05-14 03:56:36 +0000948 .pf = NFPROTO_IPV6,
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000949 .hooknum = NF_INET_PRE_ROUTING,
950 .priority = NF_IP6_PRI_FIRST,
951 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952};
953
954#ifdef CONFIG_SYSCTL
955static
Joe Perchesfe2c6332013-06-11 23:04:25 -0700956int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
tanxiaojun56b148e2013-12-19 13:28:13 +0800957 void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 int ret;
960
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700961 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (write && *(int *)(ctl->data))
964 *(int *)(ctl->data) = 1;
965 return ret;
966}
967
Joe Perchesfe2c6332013-06-11 23:04:25 -0700968static struct ctl_table brnf_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .procname = "bridge-nf-call-arptables",
971 .data = &brnf_call_arptables,
972 .maxlen = sizeof(int),
973 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800974 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 },
976 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 .procname = "bridge-nf-call-iptables",
978 .data = &brnf_call_iptables,
979 .maxlen = sizeof(int),
980 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800981 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 },
983 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 .procname = "bridge-nf-call-ip6tables",
985 .data = &brnf_call_ip6tables,
986 .maxlen = sizeof(int),
987 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800988 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 },
990 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 .procname = "bridge-nf-filter-vlan-tagged",
992 .data = &brnf_filter_vlan_tagged,
993 .maxlen = sizeof(int),
994 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800995 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 },
Michael Milner516299d2007-04-12 22:14:23 -0700997 {
Michael Milner516299d2007-04-12 22:14:23 -0700998 .procname = "bridge-nf-filter-pppoe-tagged",
999 .data = &brnf_filter_pppoe_tagged,
1000 .maxlen = sizeof(int),
1001 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001002 .proc_handler = brnf_sysctl_call_tables,
Michael Milner516299d2007-04-12 22:14:23 -07001003 },
Pablo Neira Ayuso49816822012-05-08 19:36:44 +02001004 {
1005 .procname = "bridge-nf-pass-vlan-input-dev",
1006 .data = &brnf_pass_vlan_indev,
1007 .maxlen = sizeof(int),
1008 .mode = 0644,
1009 .proc_handler = brnf_sysctl_call_tables,
1010 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001011 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#endif
1014
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001015static int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001017 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001019 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001020 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return ret;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023#ifdef CONFIG_SYSCTL
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +00001024 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -08001026 printk(KERN_WARNING
1027 "br_netfilter: can't register to sysctl.\n");
Geert Uytterhoeven96727232015-02-12 15:17:27 +01001028 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1029 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031#endif
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001032 RCU_INIT_POINTER(nf_br_ops, &br_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
1035}
1036
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001037static void __exit br_netfilter_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Pablo Neira Ayuso1a4ba642015-03-10 10:27:18 +01001039 RCU_INIT_POINTER(nf_br_ops, NULL);
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001040 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041#ifdef CONFIG_SYSCTL
Eric W. Biederman5dd3df12012-04-19 13:24:33 +00001042 unregister_net_sysctl_table(brnf_sysctl_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001045
1046module_init(br_netfilter_init);
1047module_exit(br_netfilter_fini);
1048
1049MODULE_LICENSE("GPL");
1050MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1051MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1052MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");