]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/bridge/netfilter/nft_reject_bridge.c
ipv4: Fix table id reference in fib_sync_down_addr
[mirror_ubuntu-bionic-kernel.git] / net / bridge / netfilter / nft_reject_bridge.c
CommitLineData
85f5b308
PNA
1/*
2 * Copyright (c) 2014 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/netlink.h>
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15#include <net/netfilter/nf_tables.h>
16#include <net/netfilter/nft_reject.h>
51b0a5d8
PNA
17#include <net/netfilter/ipv4/nf_reject.h>
18#include <net/netfilter/ipv6/nf_reject.h>
523b929d
PNA
19#include <linux/ip.h>
20#include <net/ip.h>
c1207c04 21#include <net/ip6_checksum.h>
127917c2 22#include <linux/netfilter_bridge.h>
72500bc1 23#include <linux/netfilter_ipv6.h>
523b929d
PNA
24#include "../br_private.h"
25
26static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
27 struct sk_buff *nskb)
28{
29 struct ethhdr *eth;
30
d58ff351 31 eth = skb_push(nskb, ETH_HLEN);
523b929d
PNA
32 skb_reset_mac_header(nskb);
33 ether_addr_copy(eth->h_source, eth_hdr(oldskb)->h_dest);
34 ether_addr_copy(eth->h_dest, eth_hdr(oldskb)->h_source);
35 eth->h_proto = eth_hdr(oldskb)->h_proto;
36 skb_pull(nskb, ETH_HLEN);
37}
38
10151d7b
PNA
39static int nft_bridge_iphdr_validate(struct sk_buff *skb)
40{
41 struct iphdr *iph;
42 u32 len;
43
44 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
45 return 0;
46
47 iph = ip_hdr(skb);
48 if (iph->ihl < 5 || iph->version != 4)
49 return 0;
50
51 len = ntohs(iph->tot_len);
52 if (skb->len < len)
53 return 0;
54 else if (len < (iph->ihl*4))
55 return 0;
56
57 if (!pskb_may_pull(skb, iph->ihl*4))
58 return 0;
59
60 return 1;
61}
62
72500bc1
FW
63/* We cannot use oldskb->dev, it can be either bridge device (NF_BRIDGE INPUT)
64 * or the bridge port (NF_BRIDGE PREROUTING).
65 */
29421198
LZ
66static void nft_reject_br_send_v4_tcp_reset(struct net *net,
67 struct sk_buff *oldskb,
72500bc1
FW
68 const struct net_device *dev,
69 int hook)
523b929d
PNA
70{
71 struct sk_buff *nskb;
72 struct iphdr *niph;
73 const struct tcphdr *oth;
74 struct tcphdr _oth;
75
68b0faa8 76 if (!nft_bridge_iphdr_validate(oldskb))
523b929d
PNA
77 return;
78
79 oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
80 if (!oth)
81 return;
82
83 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
84 LL_MAX_HEADER, GFP_ATOMIC);
85 if (!nskb)
86 return;
87
88 skb_reserve(nskb, LL_MAX_HEADER);
89 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
fa50d974 90 net->ipv4.sysctl_ip_default_ttl);
523b929d 91 nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
fa50d974 92 niph->ttl = net->ipv4.sysctl_ip_default_ttl;
523b929d
PNA
93 niph->tot_len = htons(nskb->len);
94 ip_send_check(niph);
95
96 nft_reject_br_push_etherhdr(oldskb, nskb);
97
37b090e6 98 br_forward(br_port_get_rcu(dev), nskb, false, true);
523b929d
PNA
99}
100
29421198
LZ
101static void nft_reject_br_send_v4_unreach(struct net *net,
102 struct sk_buff *oldskb,
72500bc1
FW
103 const struct net_device *dev,
104 int hook, u8 code)
523b929d
PNA
105{
106 struct sk_buff *nskb;
107 struct iphdr *niph;
108 struct icmphdr *icmph;
109 unsigned int len;
523b929d 110 __wsum csum;
72500bc1 111 u8 proto;
523b929d 112
219f1d79 113 if (!nft_bridge_iphdr_validate(oldskb))
523b929d
PNA
114 return;
115
116 /* IP header checks: fragment. */
117 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
118 return;
119
120 /* RFC says return as much as we can without exceeding 576 bytes. */
121 len = min_t(unsigned int, 536, oldskb->len);
122
123 if (!pskb_may_pull(oldskb, len))
124 return;
125
a03a8dbe 126 if (pskb_trim_rcsum(oldskb, ntohs(ip_hdr(oldskb)->tot_len)))
72500bc1
FW
127 return;
128
129 if (ip_hdr(oldskb)->protocol == IPPROTO_TCP ||
130 ip_hdr(oldskb)->protocol == IPPROTO_UDP)
131 proto = ip_hdr(oldskb)->protocol;
132 else
133 proto = 0;
134
135 if (!skb_csum_unnecessary(oldskb) &&
136 nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), proto))
523b929d
PNA
137 return;
138
139 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
140 LL_MAX_HEADER + len, GFP_ATOMIC);
141 if (!nskb)
142 return;
143
144 skb_reserve(nskb, LL_MAX_HEADER);
145 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_ICMP,
fa50d974 146 net->ipv4.sysctl_ip_default_ttl);
523b929d
PNA
147
148 skb_reset_transport_header(nskb);
b080db58 149 icmph = skb_put_zero(nskb, sizeof(struct icmphdr));
523b929d
PNA
150 icmph->type = ICMP_DEST_UNREACH;
151 icmph->code = code;
152
b952f4df 153 skb_put_data(nskb, skb_network_header(oldskb), len);
523b929d
PNA
154
155 csum = csum_partial((void *)icmph, len + sizeof(struct icmphdr), 0);
156 icmph->checksum = csum_fold(csum);
157
158 niph->tot_len = htons(nskb->len);
159 ip_send_check(niph);
160
161 nft_reject_br_push_etherhdr(oldskb, nskb);
162
37b090e6 163 br_forward(br_port_get_rcu(dev), nskb, false, true);
523b929d
PNA
164}
165
10151d7b
PNA
166static int nft_bridge_ip6hdr_validate(struct sk_buff *skb)
167{
168 struct ipv6hdr *hdr;
169 u32 pkt_len;
170
171 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
172 return 0;
173
174 hdr = ipv6_hdr(skb);
175 if (hdr->version != 6)
176 return 0;
177
178 pkt_len = ntohs(hdr->payload_len);
179 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
180 return 0;
181
182 return 1;
183}
184
523b929d 185static void nft_reject_br_send_v6_tcp_reset(struct net *net,
72500bc1
FW
186 struct sk_buff *oldskb,
187 const struct net_device *dev,
188 int hook)
523b929d
PNA
189{
190 struct sk_buff *nskb;
191 const struct tcphdr *oth;
192 struct tcphdr _oth;
193 unsigned int otcplen;
194 struct ipv6hdr *nip6h;
195
68b0faa8 196 if (!nft_bridge_ip6hdr_validate(oldskb))
523b929d
PNA
197 return;
198
199 oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
200 if (!oth)
201 return;
202
203 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
204 LL_MAX_HEADER, GFP_ATOMIC);
205 if (!nskb)
206 return;
207
208 skb_reserve(nskb, LL_MAX_HEADER);
209 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
210 net->ipv6.devconf_all->hop_limit);
211 nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
212 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
213
214 nft_reject_br_push_etherhdr(oldskb, nskb);
215
37b090e6 216 br_forward(br_port_get_rcu(dev), nskb, false, true);
72500bc1
FW
217}
218
219static bool reject6_br_csum_ok(struct sk_buff *skb, int hook)
220{
221 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
222 int thoff;
223 __be16 fo;
224 u8 proto = ip6h->nexthdr;
225
72500bc1
FW
226 if (skb_csum_unnecessary(skb))
227 return true;
228
229 if (ip6h->payload_len &&
230 pskb_trim_rcsum(skb, ntohs(ip6h->payload_len) + sizeof(*ip6h)))
231 return false;
232
fc4dee53 233 ip6h = ipv6_hdr(skb);
72500bc1
FW
234 thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
235 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
236 return false;
237
238 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
523b929d
PNA
239}
240
241static void nft_reject_br_send_v6_unreach(struct net *net,
72500bc1
FW
242 struct sk_buff *oldskb,
243 const struct net_device *dev,
244 int hook, u8 code)
523b929d
PNA
245{
246 struct sk_buff *nskb;
247 struct ipv6hdr *nip6h;
248 struct icmp6hdr *icmp6h;
249 unsigned int len;
523b929d 250
68b0faa8 251 if (!nft_bridge_ip6hdr_validate(oldskb))
523b929d
PNA
252 return;
253
254 /* Include "As much of invoking packet as possible without the ICMPv6
255 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
256 */
257 len = min_t(unsigned int, 1220, oldskb->len);
258
259 if (!pskb_may_pull(oldskb, len))
260 return;
261
72500bc1
FW
262 if (!reject6_br_csum_ok(oldskb, hook))
263 return;
264
523b929d
PNA
265 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) +
266 LL_MAX_HEADER + len, GFP_ATOMIC);
267 if (!nskb)
268 return;
269
270 skb_reserve(nskb, LL_MAX_HEADER);
271 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
272 net->ipv6.devconf_all->hop_limit);
273
274 skb_reset_transport_header(nskb);
b080db58 275 icmp6h = skb_put_zero(nskb, sizeof(struct icmp6hdr));
523b929d
PNA
276 icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
277 icmp6h->icmp6_code = code;
278
b952f4df 279 skb_put_data(nskb, skb_network_header(oldskb), len);
523b929d
PNA
280 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
281
282 icmp6h->icmp6_cksum =
283 csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
284 nskb->len - sizeof(struct ipv6hdr),
285 IPPROTO_ICMPV6,
286 csum_partial(icmp6h,
287 nskb->len - sizeof(struct ipv6hdr),
288 0));
289
290 nft_reject_br_push_etherhdr(oldskb, nskb);
291
37b090e6 292 br_forward(br_port_get_rcu(dev), nskb, false, true);
523b929d 293}
85f5b308
PNA
294
295static void nft_reject_bridge_eval(const struct nft_expr *expr,
a55e22e9
PM
296 struct nft_regs *regs,
297 const struct nft_pktinfo *pkt)
85f5b308 298{
51b0a5d8 299 struct nft_reject *priv = nft_expr_priv(expr);
523b929d
PNA
300 const unsigned char *dest = eth_hdr(pkt->skb)->h_dest;
301
302 if (is_broadcast_ether_addr(dest) ||
303 is_multicast_ether_addr(dest))
304 goto out;
51b0a5d8 305
85f5b308
PNA
306 switch (eth_hdr(pkt->skb)->h_proto) {
307 case htons(ETH_P_IP):
51b0a5d8
PNA
308 switch (priv->type) {
309 case NFT_REJECT_ICMP_UNREACH:
0e5a1c7e
PNA
310 nft_reject_br_send_v4_unreach(nft_net(pkt), pkt->skb,
311 nft_in(pkt),
312 nft_hook(pkt),
523b929d 313 priv->icmp_code);
51b0a5d8
PNA
314 break;
315 case NFT_REJECT_TCP_RST:
0e5a1c7e
PNA
316 nft_reject_br_send_v4_tcp_reset(nft_net(pkt), pkt->skb,
317 nft_in(pkt),
318 nft_hook(pkt));
51b0a5d8
PNA
319 break;
320 case NFT_REJECT_ICMPX_UNREACH:
0e5a1c7e
PNA
321 nft_reject_br_send_v4_unreach(nft_net(pkt), pkt->skb,
322 nft_in(pkt),
323 nft_hook(pkt),
523b929d 324 nft_reject_icmp_code(priv->icmp_code));
51b0a5d8
PNA
325 break;
326 }
327 break;
85f5b308 328 case htons(ETH_P_IPV6):
51b0a5d8
PNA
329 switch (priv->type) {
330 case NFT_REJECT_ICMP_UNREACH:
0e5a1c7e
PNA
331 nft_reject_br_send_v6_unreach(nft_net(pkt), pkt->skb,
332 nft_in(pkt),
333 nft_hook(pkt),
523b929d 334 priv->icmp_code);
51b0a5d8
PNA
335 break;
336 case NFT_REJECT_TCP_RST:
0e5a1c7e
PNA
337 nft_reject_br_send_v6_tcp_reset(nft_net(pkt), pkt->skb,
338 nft_in(pkt),
339 nft_hook(pkt));
51b0a5d8
PNA
340 break;
341 case NFT_REJECT_ICMPX_UNREACH:
0e5a1c7e
PNA
342 nft_reject_br_send_v6_unreach(nft_net(pkt), pkt->skb,
343 nft_in(pkt),
344 nft_hook(pkt),
523b929d 345 nft_reject_icmpv6_code(priv->icmp_code));
51b0a5d8
PNA
346 break;
347 }
348 break;
85f5b308
PNA
349 default:
350 /* No explicit way to reject this protocol, drop it. */
85f5b308
PNA
351 break;
352 }
523b929d 353out:
a55e22e9 354 regs->verdict.code = NF_DROP;
51b0a5d8
PNA
355}
356
75e8d06d
PNA
357static int nft_reject_bridge_validate(const struct nft_ctx *ctx,
358 const struct nft_expr *expr,
359 const struct nft_data **data)
127917c2 360{
75e8d06d
PNA
361 return nft_chain_validate_hooks(ctx->chain, (1 << NF_BR_PRE_ROUTING) |
362 (1 << NF_BR_LOCAL_IN));
127917c2
PNA
363}
364
51b0a5d8
PNA
365static int nft_reject_bridge_init(const struct nft_ctx *ctx,
366 const struct nft_expr *expr,
367 const struct nlattr * const tb[])
368{
369 struct nft_reject *priv = nft_expr_priv(expr);
c56e3956 370 int icmp_code;
51b0a5d8
PNA
371
372 if (tb[NFTA_REJECT_TYPE] == NULL)
373 return -EINVAL;
374
375 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
376 switch (priv->type) {
377 case NFT_REJECT_ICMP_UNREACH:
378 case NFT_REJECT_ICMPX_UNREACH:
379 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
380 return -EINVAL;
381
382 icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
383 if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
384 icmp_code > NFT_REJECT_ICMPX_MAX)
385 return -EINVAL;
386
387 priv->icmp_code = icmp_code;
388 break;
389 case NFT_REJECT_TCP_RST:
390 break;
391 default:
392 return -EINVAL;
393 }
394 return 0;
395}
396
397static int nft_reject_bridge_dump(struct sk_buff *skb,
398 const struct nft_expr *expr)
399{
400 const struct nft_reject *priv = nft_expr_priv(expr);
401
402 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
403 goto nla_put_failure;
404
405 switch (priv->type) {
406 case NFT_REJECT_ICMP_UNREACH:
407 case NFT_REJECT_ICMPX_UNREACH:
408 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
409 goto nla_put_failure;
410 break;
c1f86676
DM
411 default:
412 break;
51b0a5d8
PNA
413 }
414
415 return 0;
416
417nla_put_failure:
418 return -1;
85f5b308
PNA
419}
420
421static struct nft_expr_type nft_reject_bridge_type;
422static const struct nft_expr_ops nft_reject_bridge_ops = {
423 .type = &nft_reject_bridge_type,
424 .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
425 .eval = nft_reject_bridge_eval,
51b0a5d8
PNA
426 .init = nft_reject_bridge_init,
427 .dump = nft_reject_bridge_dump,
127917c2 428 .validate = nft_reject_bridge_validate,
85f5b308
PNA
429};
430
431static struct nft_expr_type nft_reject_bridge_type __read_mostly = {
432 .family = NFPROTO_BRIDGE,
433 .name = "reject",
434 .ops = &nft_reject_bridge_ops,
435 .policy = nft_reject_policy,
436 .maxattr = NFTA_REJECT_MAX,
437 .owner = THIS_MODULE,
438};
439
440static int __init nft_reject_bridge_module_init(void)
441{
442 return nft_register_expr(&nft_reject_bridge_type);
443}
444
445static void __exit nft_reject_bridge_module_exit(void)
446{
447 nft_unregister_expr(&nft_reject_bridge_type);
448}
449
450module_init(nft_reject_bridge_module_init);
451module_exit(nft_reject_bridge_module_exit);
452
453MODULE_LICENSE("GPL");
454MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
455MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "reject");