aboutsummaryrefslogtreecommitdiffstats
path: root/net/bridge
diff options
context:
space:
mode:
Diffstat (limited to 'net/bridge')
-rw-r--r--net/bridge/br_forward.c1
-rw-r--r--net/bridge/br_multicast.c3
-rw-r--r--net/bridge/br_netfilter.c24
-rw-r--r--net/bridge/br_netlink.c1
-rw-r--r--net/bridge/netfilter/nf_tables_bridge.c6
-rw-r--r--net/bridge/netfilter/nft_reject_bridge.c297
6 files changed, 298 insertions, 34 deletions
diff --git a/net/bridge/br_forward.c b/net/bridge/br_forward.c
index 992ec49a96aa..44cb786b925a 100644
--- a/net/bridge/br_forward.c
+++ b/net/bridge/br_forward.c
@@ -112,6 +112,7 @@ void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
112 112
113 kfree_skb(skb); 113 kfree_skb(skb);
114} 114}
115EXPORT_SYMBOL_GPL(br_deliver);
115 116
116/* called with rcu_read_lock */ 117/* called with rcu_read_lock */
117void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0) 118void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index 648d79ccf462..c465876c7861 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -813,10 +813,9 @@ static void __br_multicast_send_query(struct net_bridge *br,
813 return; 813 return;
814 814
815 if (port) { 815 if (port) {
816 __skb_push(skb, sizeof(struct ethhdr));
817 skb->dev = port->dev; 816 skb->dev = port->dev;
818 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 817 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
819 dev_queue_xmit); 818 br_dev_queue_push_xmit);
820 } else { 819 } else {
821 br_multicast_select_own_querier(br, ip, skb); 820 br_multicast_select_own_querier(br, ip, skb);
822 netif_rx(skb); 821 netif_rx(skb);
diff --git a/net/bridge/br_netfilter.c b/net/bridge/br_netfilter.c
index 1bada53bb195..1a4f32c09ad5 100644
--- a/net/bridge/br_netfilter.c
+++ b/net/bridge/br_netfilter.c
@@ -192,7 +192,6 @@ static inline void nf_bridge_save_header(struct sk_buff *skb)
192 192
193static int br_parse_ip_options(struct sk_buff *skb) 193static int br_parse_ip_options(struct sk_buff *skb)
194{ 194{
195 struct ip_options *opt;
196 const struct iphdr *iph; 195 const struct iphdr *iph;
197 struct net_device *dev = skb->dev; 196 struct net_device *dev = skb->dev;
198 u32 len; 197 u32 len;
@@ -201,7 +200,6 @@ static int br_parse_ip_options(struct sk_buff *skb)
201 goto inhdr_error; 200 goto inhdr_error;
202 201
203 iph = ip_hdr(skb); 202 iph = ip_hdr(skb);
204 opt = &(IPCB(skb)->opt);
205 203
206 /* Basic sanity checks */ 204 /* Basic sanity checks */
207 if (iph->ihl < 5 || iph->version != 4) 205 if (iph->ihl < 5 || iph->version != 4)
@@ -227,23 +225,11 @@ static int br_parse_ip_options(struct sk_buff *skb)
227 } 225 }
228 226
229 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 227 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
230 if (iph->ihl == 5) 228 /* We should really parse IP options here but until
231 return 0; 229 * somebody who actually uses IP options complains to
232 230 * us we'll just silently ignore the options because
233 opt->optlen = iph->ihl*4 - sizeof(struct iphdr); 231 * we're lazy!
234 if (ip_options_compile(dev_net(dev), opt, skb)) 232 */
235 goto inhdr_error;
236
237 /* Check correct handling of SRR option */
238 if (unlikely(opt->srr)) {
239 struct in_device *in_dev = __in_dev_get_rcu(dev);
240 if (in_dev && !IN_DEV_SOURCE_ROUTE(in_dev))
241 goto drop;
242
243 if (ip_options_rcv_srr(skb))
244 goto drop;
245 }
246
247 return 0; 233 return 0;
248 234
249inhdr_error: 235inhdr_error:
diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c
index 2ff9706647f2..e5ec470b851f 100644
--- a/net/bridge/br_netlink.c
+++ b/net/bridge/br_netlink.c
@@ -280,6 +280,7 @@ static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
280 [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, 280 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
281 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, 281 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
282 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, 282 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
283 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
283 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, 284 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
284 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, 285 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
285}; 286};
diff --git a/net/bridge/netfilter/nf_tables_bridge.c b/net/bridge/netfilter/nf_tables_bridge.c
index da17a5eab8b4..074c557ab505 100644
--- a/net/bridge/netfilter/nf_tables_bridge.c
+++ b/net/bridge/netfilter/nf_tables_bridge.c
@@ -75,9 +75,11 @@ static const struct nf_chain_type filter_bridge = {
75 .type = NFT_CHAIN_T_DEFAULT, 75 .type = NFT_CHAIN_T_DEFAULT,
76 .family = NFPROTO_BRIDGE, 76 .family = NFPROTO_BRIDGE,
77 .owner = THIS_MODULE, 77 .owner = THIS_MODULE,
78 .hook_mask = (1 << NF_BR_LOCAL_IN) | 78 .hook_mask = (1 << NF_BR_PRE_ROUTING) |
79 (1 << NF_BR_LOCAL_IN) |
79 (1 << NF_BR_FORWARD) | 80 (1 << NF_BR_FORWARD) |
80 (1 << NF_BR_LOCAL_OUT), 81 (1 << NF_BR_LOCAL_OUT) |
82 (1 << NF_BR_POST_ROUTING),
81}; 83};
82 84
83static int __init nf_tables_bridge_init(void) 85static int __init nf_tables_bridge_init(void)
diff --git a/net/bridge/netfilter/nft_reject_bridge.c b/net/bridge/netfilter/nft_reject_bridge.c
index a76479535df2..48da2c54a69e 100644
--- a/net/bridge/netfilter/nft_reject_bridge.c
+++ b/net/bridge/netfilter/nft_reject_bridge.c
@@ -16,6 +16,239 @@
16#include <net/netfilter/nft_reject.h> 16#include <net/netfilter/nft_reject.h>
17#include <net/netfilter/ipv4/nf_reject.h> 17#include <net/netfilter/ipv4/nf_reject.h>
18#include <net/netfilter/ipv6/nf_reject.h> 18#include <net/netfilter/ipv6/nf_reject.h>
19#include <linux/ip.h>
20#include <net/ip.h>
21#include <net/ip6_checksum.h>
22#include <linux/netfilter_bridge.h>
23#include "../br_private.h"
24
25static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
26 struct sk_buff *nskb)
27{
28 struct ethhdr *eth;
29
30 eth = (struct ethhdr *)skb_push(nskb, ETH_HLEN);
31 skb_reset_mac_header(nskb);
32 ether_addr_copy(eth->h_source, eth_hdr(oldskb)->h_dest);
33 ether_addr_copy(eth->h_dest, eth_hdr(oldskb)->h_source);
34 eth->h_proto = eth_hdr(oldskb)->h_proto;
35 skb_pull(nskb, ETH_HLEN);
36}
37
38static int nft_reject_iphdr_validate(struct sk_buff *oldskb)
39{
40 struct iphdr *iph;
41 u32 len;
42
43 if (!pskb_may_pull(oldskb, sizeof(struct iphdr)))
44 return 0;
45
46 iph = ip_hdr(oldskb);
47 if (iph->ihl < 5 || iph->version != 4)
48 return 0;
49
50 len = ntohs(iph->tot_len);
51 if (oldskb->len < len)
52 return 0;
53 else if (len < (iph->ihl*4))
54 return 0;
55
56 if (!pskb_may_pull(oldskb, iph->ihl*4))
57 return 0;
58
59 return 1;
60}
61
62static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook)
63{
64 struct sk_buff *nskb;
65 struct iphdr *niph;
66 const struct tcphdr *oth;
67 struct tcphdr _oth;
68
69 if (!nft_reject_iphdr_validate(oldskb))
70 return;
71
72 oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
73 if (!oth)
74 return;
75
76 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
77 LL_MAX_HEADER, GFP_ATOMIC);
78 if (!nskb)
79 return;
80
81 skb_reserve(nskb, LL_MAX_HEADER);
82 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
83 sysctl_ip_default_ttl);
84 nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
85 niph->ttl = sysctl_ip_default_ttl;
86 niph->tot_len = htons(nskb->len);
87 ip_send_check(niph);
88
89 nft_reject_br_push_etherhdr(oldskb, nskb);
90
91 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
92}
93
94static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
95 u8 code)
96{
97 struct sk_buff *nskb;
98 struct iphdr *niph;
99 struct icmphdr *icmph;
100 unsigned int len;
101 void *payload;
102 __wsum csum;
103
104 if (!nft_reject_iphdr_validate(oldskb))
105 return;
106
107 /* IP header checks: fragment. */
108 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
109 return;
110
111 /* RFC says return as much as we can without exceeding 576 bytes. */
112 len = min_t(unsigned int, 536, oldskb->len);
113
114 if (!pskb_may_pull(oldskb, len))
115 return;
116
117 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), 0))
118 return;
119
120 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
121 LL_MAX_HEADER + len, GFP_ATOMIC);
122 if (!nskb)
123 return;
124
125 skb_reserve(nskb, LL_MAX_HEADER);
126 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_ICMP,
127 sysctl_ip_default_ttl);
128
129 skb_reset_transport_header(nskb);
130 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
131 memset(icmph, 0, sizeof(*icmph));
132 icmph->type = ICMP_DEST_UNREACH;
133 icmph->code = code;
134
135 payload = skb_put(nskb, len);
136 memcpy(payload, skb_network_header(oldskb), len);
137
138 csum = csum_partial((void *)icmph, len + sizeof(struct icmphdr), 0);
139 icmph->checksum = csum_fold(csum);
140
141 niph->tot_len = htons(nskb->len);
142 ip_send_check(niph);
143
144 nft_reject_br_push_etherhdr(oldskb, nskb);
145
146 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
147}
148
149static int nft_reject_ip6hdr_validate(struct sk_buff *oldskb)
150{
151 struct ipv6hdr *hdr;
152 u32 pkt_len;
153
154 if (!pskb_may_pull(oldskb, sizeof(struct ipv6hdr)))
155 return 0;
156
157 hdr = ipv6_hdr(oldskb);
158 if (hdr->version != 6)
159 return 0;
160
161 pkt_len = ntohs(hdr->payload_len);
162 if (pkt_len + sizeof(struct ipv6hdr) > oldskb->len)
163 return 0;
164
165 return 1;
166}
167
168static void nft_reject_br_send_v6_tcp_reset(struct net *net,
169 struct sk_buff *oldskb, int hook)
170{
171 struct sk_buff *nskb;
172 const struct tcphdr *oth;
173 struct tcphdr _oth;
174 unsigned int otcplen;
175 struct ipv6hdr *nip6h;
176
177 if (!nft_reject_ip6hdr_validate(oldskb))
178 return;
179
180 oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
181 if (!oth)
182 return;
183
184 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
185 LL_MAX_HEADER, GFP_ATOMIC);
186 if (!nskb)
187 return;
188
189 skb_reserve(nskb, LL_MAX_HEADER);
190 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
191 net->ipv6.devconf_all->hop_limit);
192 nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
193 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
194
195 nft_reject_br_push_etherhdr(oldskb, nskb);
196
197 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
198}
199
200static void nft_reject_br_send_v6_unreach(struct net *net,
201 struct sk_buff *oldskb, int hook,
202 u8 code)
203{
204 struct sk_buff *nskb;
205 struct ipv6hdr *nip6h;
206 struct icmp6hdr *icmp6h;
207 unsigned int len;
208 void *payload;
209
210 if (!nft_reject_ip6hdr_validate(oldskb))
211 return;
212
213 /* Include "As much of invoking packet as possible without the ICMPv6
214 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
215 */
216 len = min_t(unsigned int, 1220, oldskb->len);
217
218 if (!pskb_may_pull(oldskb, len))
219 return;
220
221 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) +
222 LL_MAX_HEADER + len, GFP_ATOMIC);
223 if (!nskb)
224 return;
225
226 skb_reserve(nskb, LL_MAX_HEADER);
227 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
228 net->ipv6.devconf_all->hop_limit);
229
230 skb_reset_transport_header(nskb);
231 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
232 memset(icmp6h, 0, sizeof(*icmp6h));
233 icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
234 icmp6h->icmp6_code = code;
235
236 payload = skb_put(nskb, len);
237 memcpy(payload, skb_network_header(oldskb), len);
238 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
239
240 icmp6h->icmp6_cksum =
241 csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
242 nskb->len - sizeof(struct ipv6hdr),
243 IPPROTO_ICMPV6,
244 csum_partial(icmp6h,
245 nskb->len - sizeof(struct ipv6hdr),
246 0));
247
248 nft_reject_br_push_etherhdr(oldskb, nskb);
249
250 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
251}
19 252
20static void nft_reject_bridge_eval(const struct nft_expr *expr, 253static void nft_reject_bridge_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1], 254 struct nft_data data[NFT_REG_MAX + 1],
@@ -23,35 +256,46 @@ static void nft_reject_bridge_eval(const struct nft_expr *expr,
23{ 256{
24 struct nft_reject *priv = nft_expr_priv(expr); 257 struct nft_reject *priv = nft_expr_priv(expr);
25 struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out); 258 struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out);
259 const unsigned char *dest = eth_hdr(pkt->skb)->h_dest;
260
261 if (is_broadcast_ether_addr(dest) ||
262 is_multicast_ether_addr(dest))
263 goto out;
26 264
27 switch (eth_hdr(pkt->skb)->h_proto) { 265 switch (eth_hdr(pkt->skb)->h_proto) {
28 case htons(ETH_P_IP): 266 case htons(ETH_P_IP):
29 switch (priv->type) { 267 switch (priv->type) {
30 case NFT_REJECT_ICMP_UNREACH: 268 case NFT_REJECT_ICMP_UNREACH:
31 nf_send_unreach(pkt->skb, priv->icmp_code); 269 nft_reject_br_send_v4_unreach(pkt->skb,
270 pkt->ops->hooknum,
271 priv->icmp_code);
32 break; 272 break;
33 case NFT_REJECT_TCP_RST: 273 case NFT_REJECT_TCP_RST:
34 nf_send_reset(pkt->skb, pkt->ops->hooknum); 274 nft_reject_br_send_v4_tcp_reset(pkt->skb,
275 pkt->ops->hooknum);
35 break; 276 break;
36 case NFT_REJECT_ICMPX_UNREACH: 277 case NFT_REJECT_ICMPX_UNREACH:
37 nf_send_unreach(pkt->skb, 278 nft_reject_br_send_v4_unreach(pkt->skb,
38 nft_reject_icmp_code(priv->icmp_code)); 279 pkt->ops->hooknum,
280 nft_reject_icmp_code(priv->icmp_code));
39 break; 281 break;
40 } 282 }
41 break; 283 break;
42 case htons(ETH_P_IPV6): 284 case htons(ETH_P_IPV6):
43 switch (priv->type) { 285 switch (priv->type) {
44 case NFT_REJECT_ICMP_UNREACH: 286 case NFT_REJECT_ICMP_UNREACH:
45 nf_send_unreach6(net, pkt->skb, priv->icmp_code, 287 nft_reject_br_send_v6_unreach(net, pkt->skb,
46 pkt->ops->hooknum); 288 pkt->ops->hooknum,
289 priv->icmp_code);
47 break; 290 break;
48 case NFT_REJECT_TCP_RST: 291 case NFT_REJECT_TCP_RST:
49 nf_send_reset6(net, pkt->skb, pkt->ops->hooknum); 292 nft_reject_br_send_v6_tcp_reset(net, pkt->skb,
293 pkt->ops->hooknum);
50 break; 294 break;
51 case NFT_REJECT_ICMPX_UNREACH: 295 case NFT_REJECT_ICMPX_UNREACH:
52 nf_send_unreach6(net, pkt->skb, 296 nft_reject_br_send_v6_unreach(net, pkt->skb,
53 nft_reject_icmpv6_code(priv->icmp_code), 297 pkt->ops->hooknum,
54 pkt->ops->hooknum); 298 nft_reject_icmpv6_code(priv->icmp_code));
55 break; 299 break;
56 } 300 }
57 break; 301 break;
@@ -59,15 +303,38 @@ static void nft_reject_bridge_eval(const struct nft_expr *expr,
59 /* No explicit way to reject this protocol, drop it. */ 303 /* No explicit way to reject this protocol, drop it. */
60 break; 304 break;
61 } 305 }
306out:
62 data[NFT_REG_VERDICT].verdict = NF_DROP; 307 data[NFT_REG_VERDICT].verdict = NF_DROP;
63} 308}
64 309
310static int nft_reject_bridge_validate_hooks(const struct nft_chain *chain)
311{
312 struct nft_base_chain *basechain;
313
314 if (chain->flags & NFT_BASE_CHAIN) {
315 basechain = nft_base_chain(chain);
316
317 switch (basechain->ops[0].hooknum) {
318 case NF_BR_PRE_ROUTING:
319 case NF_BR_LOCAL_IN:
320 break;
321 default:
322 return -EOPNOTSUPP;
323 }
324 }
325 return 0;
326}
327
65static int nft_reject_bridge_init(const struct nft_ctx *ctx, 328static int nft_reject_bridge_init(const struct nft_ctx *ctx,
66 const struct nft_expr *expr, 329 const struct nft_expr *expr,
67 const struct nlattr * const tb[]) 330 const struct nlattr * const tb[])
68{ 331{
69 struct nft_reject *priv = nft_expr_priv(expr); 332 struct nft_reject *priv = nft_expr_priv(expr);
70 int icmp_code; 333 int icmp_code, err;
334
335 err = nft_reject_bridge_validate_hooks(ctx->chain);
336 if (err < 0)
337 return err;
71 338
72 if (tb[NFTA_REJECT_TYPE] == NULL) 339 if (tb[NFTA_REJECT_TYPE] == NULL)
73 return -EINVAL; 340 return -EINVAL;
@@ -116,6 +383,13 @@ nla_put_failure:
116 return -1; 383 return -1;
117} 384}
118 385
386static int nft_reject_bridge_validate(const struct nft_ctx *ctx,
387 const struct nft_expr *expr,
388 const struct nft_data **data)
389{
390 return nft_reject_bridge_validate_hooks(ctx->chain);
391}
392
119static struct nft_expr_type nft_reject_bridge_type; 393static struct nft_expr_type nft_reject_bridge_type;
120static const struct nft_expr_ops nft_reject_bridge_ops = { 394static const struct nft_expr_ops nft_reject_bridge_ops = {
121 .type = &nft_reject_bridge_type, 395 .type = &nft_reject_bridge_type,
@@ -123,6 +397,7 @@ static const struct nft_expr_ops nft_reject_bridge_ops = {
123 .eval = nft_reject_bridge_eval, 397 .eval = nft_reject_bridge_eval,
124 .init = nft_reject_bridge_init, 398 .init = nft_reject_bridge_init,
125 .dump = nft_reject_bridge_dump, 399 .dump = nft_reject_bridge_dump,
400 .validate = nft_reject_bridge_validate,
126}; 401};
127 402
128static struct nft_expr_type nft_reject_bridge_type __read_mostly = { 403static struct nft_expr_type nft_reject_bridge_type __read_mostly = {