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