netfilter: nft_reject_bridge: don't use IP stack to reject traffic
[deliverable/linux.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>
21#include "../br_private.h"
22
23static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
24 struct sk_buff *nskb)
25{
26 struct ethhdr *eth;
27
28 eth = (struct ethhdr *)skb_push(nskb, ETH_HLEN);
29 skb_reset_mac_header(nskb);
30 ether_addr_copy(eth->h_source, eth_hdr(oldskb)->h_dest);
31 ether_addr_copy(eth->h_dest, eth_hdr(oldskb)->h_source);
32 eth->h_proto = eth_hdr(oldskb)->h_proto;
33 skb_pull(nskb, ETH_HLEN);
34}
35
36static int nft_reject_iphdr_validate(struct sk_buff *oldskb)
37{
38 struct iphdr *iph;
39 u32 len;
40
41 if (!pskb_may_pull(oldskb, sizeof(struct iphdr)))
42 return 0;
43
44 iph = ip_hdr(oldskb);
45 if (iph->ihl < 5 || iph->version != 4)
46 return 0;
47
48 len = ntohs(iph->tot_len);
49 if (oldskb->len < len)
50 return 0;
51 else if (len < (iph->ihl*4))
52 return 0;
53
54 if (!pskb_may_pull(oldskb, iph->ihl*4))
55 return 0;
56
57 return 1;
58}
59
60static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook)
61{
62 struct sk_buff *nskb;
63 struct iphdr *niph;
64 const struct tcphdr *oth;
65 struct tcphdr _oth;
66
67 if (!nft_reject_iphdr_validate(oldskb))
68 return;
69
70 oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
71 if (!oth)
72 return;
73
74 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
75 LL_MAX_HEADER, GFP_ATOMIC);
76 if (!nskb)
77 return;
78
79 skb_reserve(nskb, LL_MAX_HEADER);
80 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
81 sysctl_ip_default_ttl);
82 nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
83 niph->ttl = sysctl_ip_default_ttl;
84 niph->tot_len = htons(nskb->len);
85 ip_send_check(niph);
86
87 nft_reject_br_push_etherhdr(oldskb, nskb);
88
89 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
90}
91
92static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
93 u8 code)
94{
95 struct sk_buff *nskb;
96 struct iphdr *niph;
97 struct icmphdr *icmph;
98 unsigned int len;
99 void *payload;
100 __wsum csum;
101
102 if (!nft_reject_iphdr_validate(oldskb))
103 return;
104
105 /* IP header checks: fragment. */
106 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
107 return;
108
109 /* RFC says return as much as we can without exceeding 576 bytes. */
110 len = min_t(unsigned int, 536, oldskb->len);
111
112 if (!pskb_may_pull(oldskb, len))
113 return;
114
115 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), 0))
116 return;
117
118 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
119 LL_MAX_HEADER + len, GFP_ATOMIC);
120 if (!nskb)
121 return;
122
123 skb_reserve(nskb, LL_MAX_HEADER);
124 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_ICMP,
125 sysctl_ip_default_ttl);
126
127 skb_reset_transport_header(nskb);
128 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
129 memset(icmph, 0, sizeof(*icmph));
130 icmph->type = ICMP_DEST_UNREACH;
131 icmph->code = code;
132
133 payload = skb_put(nskb, len);
134 memcpy(payload, skb_network_header(oldskb), len);
135
136 csum = csum_partial((void *)icmph, len + sizeof(struct icmphdr), 0);
137 icmph->checksum = csum_fold(csum);
138
139 niph->tot_len = htons(nskb->len);
140 ip_send_check(niph);
141
142 nft_reject_br_push_etherhdr(oldskb, nskb);
143
144 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
145}
146
147static int nft_reject_ip6hdr_validate(struct sk_buff *oldskb)
148{
149 struct ipv6hdr *hdr;
150 u32 pkt_len;
151
152 if (!pskb_may_pull(oldskb, sizeof(struct ipv6hdr)))
153 return 0;
154
155 hdr = ipv6_hdr(oldskb);
156 if (hdr->version != 6)
157 return 0;
158
159 pkt_len = ntohs(hdr->payload_len);
160 if (pkt_len + sizeof(struct ipv6hdr) > oldskb->len)
161 return 0;
162
163 return 1;
164}
165
166static void nft_reject_br_send_v6_tcp_reset(struct net *net,
167 struct sk_buff *oldskb, int hook)
168{
169 struct sk_buff *nskb;
170 const struct tcphdr *oth;
171 struct tcphdr _oth;
172 unsigned int otcplen;
173 struct ipv6hdr *nip6h;
174
175 if (!nft_reject_ip6hdr_validate(oldskb))
176 return;
177
178 oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
179 if (!oth)
180 return;
181
182 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
183 LL_MAX_HEADER, GFP_ATOMIC);
184 if (!nskb)
185 return;
186
187 skb_reserve(nskb, LL_MAX_HEADER);
188 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
189 net->ipv6.devconf_all->hop_limit);
190 nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
191 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
192
193 nft_reject_br_push_etherhdr(oldskb, nskb);
194
195 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
196}
197
198static void nft_reject_br_send_v6_unreach(struct net *net,
199 struct sk_buff *oldskb, int hook,
200 u8 code)
201{
202 struct sk_buff *nskb;
203 struct ipv6hdr *nip6h;
204 struct icmp6hdr *icmp6h;
205 unsigned int len;
206 void *payload;
207
208 if (!nft_reject_ip6hdr_validate(oldskb))
209 return;
210
211 /* Include "As much of invoking packet as possible without the ICMPv6
212 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
213 */
214 len = min_t(unsigned int, 1220, oldskb->len);
215
216 if (!pskb_may_pull(oldskb, len))
217 return;
218
219 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) +
220 LL_MAX_HEADER + len, GFP_ATOMIC);
221 if (!nskb)
222 return;
223
224 skb_reserve(nskb, LL_MAX_HEADER);
225 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
226 net->ipv6.devconf_all->hop_limit);
227
228 skb_reset_transport_header(nskb);
229 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
230 memset(icmp6h, 0, sizeof(*icmp6h));
231 icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
232 icmp6h->icmp6_code = code;
233
234 payload = skb_put(nskb, len);
235 memcpy(payload, skb_network_header(oldskb), len);
236 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
237
238 icmp6h->icmp6_cksum =
239 csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
240 nskb->len - sizeof(struct ipv6hdr),
241 IPPROTO_ICMPV6,
242 csum_partial(icmp6h,
243 nskb->len - sizeof(struct ipv6hdr),
244 0));
245
246 nft_reject_br_push_etherhdr(oldskb, nskb);
247
248 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
249}
85f5b308
PNA
250
251static void nft_reject_bridge_eval(const struct nft_expr *expr,
252 struct nft_data data[NFT_REG_MAX + 1],
253 const struct nft_pktinfo *pkt)
254{
51b0a5d8
PNA
255 struct nft_reject *priv = nft_expr_priv(expr);
256 struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out);
523b929d
PNA
257 const unsigned char *dest = eth_hdr(pkt->skb)->h_dest;
258
259 if (is_broadcast_ether_addr(dest) ||
260 is_multicast_ether_addr(dest))
261 goto out;
51b0a5d8 262
85f5b308
PNA
263 switch (eth_hdr(pkt->skb)->h_proto) {
264 case htons(ETH_P_IP):
51b0a5d8
PNA
265 switch (priv->type) {
266 case NFT_REJECT_ICMP_UNREACH:
523b929d
PNA
267 nft_reject_br_send_v4_unreach(pkt->skb,
268 pkt->ops->hooknum,
269 priv->icmp_code);
51b0a5d8
PNA
270 break;
271 case NFT_REJECT_TCP_RST:
523b929d
PNA
272 nft_reject_br_send_v4_tcp_reset(pkt->skb,
273 pkt->ops->hooknum);
51b0a5d8
PNA
274 break;
275 case NFT_REJECT_ICMPX_UNREACH:
523b929d
PNA
276 nft_reject_br_send_v4_unreach(pkt->skb,
277 pkt->ops->hooknum,
278 nft_reject_icmp_code(priv->icmp_code));
51b0a5d8
PNA
279 break;
280 }
281 break;
85f5b308 282 case htons(ETH_P_IPV6):
51b0a5d8
PNA
283 switch (priv->type) {
284 case NFT_REJECT_ICMP_UNREACH:
523b929d
PNA
285 nft_reject_br_send_v6_unreach(net, pkt->skb,
286 pkt->ops->hooknum,
287 priv->icmp_code);
51b0a5d8
PNA
288 break;
289 case NFT_REJECT_TCP_RST:
523b929d
PNA
290 nft_reject_br_send_v6_tcp_reset(net, pkt->skb,
291 pkt->ops->hooknum);
51b0a5d8
PNA
292 break;
293 case NFT_REJECT_ICMPX_UNREACH:
523b929d
PNA
294 nft_reject_br_send_v6_unreach(net, pkt->skb,
295 pkt->ops->hooknum,
296 nft_reject_icmpv6_code(priv->icmp_code));
51b0a5d8
PNA
297 break;
298 }
299 break;
85f5b308
PNA
300 default:
301 /* No explicit way to reject this protocol, drop it. */
85f5b308
PNA
302 break;
303 }
523b929d 304out:
51b0a5d8
PNA
305 data[NFT_REG_VERDICT].verdict = NF_DROP;
306}
307
308static int nft_reject_bridge_init(const struct nft_ctx *ctx,
309 const struct nft_expr *expr,
310 const struct nlattr * const tb[])
311{
312 struct nft_reject *priv = nft_expr_priv(expr);
313 int icmp_code;
314
315 if (tb[NFTA_REJECT_TYPE] == NULL)
316 return -EINVAL;
317
318 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
319 switch (priv->type) {
320 case NFT_REJECT_ICMP_UNREACH:
321 case NFT_REJECT_ICMPX_UNREACH:
322 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
323 return -EINVAL;
324
325 icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
326 if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
327 icmp_code > NFT_REJECT_ICMPX_MAX)
328 return -EINVAL;
329
330 priv->icmp_code = icmp_code;
331 break;
332 case NFT_REJECT_TCP_RST:
333 break;
334 default:
335 return -EINVAL;
336 }
337 return 0;
338}
339
340static int nft_reject_bridge_dump(struct sk_buff *skb,
341 const struct nft_expr *expr)
342{
343 const struct nft_reject *priv = nft_expr_priv(expr);
344
345 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
346 goto nla_put_failure;
347
348 switch (priv->type) {
349 case NFT_REJECT_ICMP_UNREACH:
350 case NFT_REJECT_ICMPX_UNREACH:
351 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
352 goto nla_put_failure;
353 break;
354 }
355
356 return 0;
357
358nla_put_failure:
359 return -1;
85f5b308
PNA
360}
361
362static struct nft_expr_type nft_reject_bridge_type;
363static const struct nft_expr_ops nft_reject_bridge_ops = {
364 .type = &nft_reject_bridge_type,
365 .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
366 .eval = nft_reject_bridge_eval,
51b0a5d8
PNA
367 .init = nft_reject_bridge_init,
368 .dump = nft_reject_bridge_dump,
85f5b308
PNA
369};
370
371static struct nft_expr_type nft_reject_bridge_type __read_mostly = {
372 .family = NFPROTO_BRIDGE,
373 .name = "reject",
374 .ops = &nft_reject_bridge_ops,
375 .policy = nft_reject_policy,
376 .maxattr = NFTA_REJECT_MAX,
377 .owner = THIS_MODULE,
378};
379
380static int __init nft_reject_bridge_module_init(void)
381{
382 return nft_register_expr(&nft_reject_bridge_type);
383}
384
385static void __exit nft_reject_bridge_module_exit(void)
386{
387 nft_unregister_expr(&nft_reject_bridge_type);
388}
389
390module_init(nft_reject_bridge_module_init);
391module_exit(nft_reject_bridge_module_exit);
392
393MODULE_LICENSE("GPL");
394MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
395MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "reject");
This page took 0.05962 seconds and 5 git commands to generate.