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