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