KVM: PPC: Book3S HV: Don't rely on host's page size information
[linux/fpc-iii.git] / net / netfilter / nft_exthdr.c
bloba0a93d987a3bd440dc19bafe9e65b023c6baf217
1 /*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
11 #include <asm/unaligned.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables.h>
19 #include <net/tcp.h>
21 struct nft_exthdr {
22 u8 type;
23 u8 offset;
24 u8 len;
25 u8 op;
26 enum nft_registers dreg:8;
27 enum nft_registers sreg:8;
28 u8 flags;
31 static unsigned int optlen(const u8 *opt, unsigned int offset)
33 /* Beware zero-length options: make finite progress */
34 if (opt[offset] <= TCPOPT_NOP || opt[offset + 1] == 0)
35 return 1;
36 else
37 return opt[offset + 1];
40 static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
41 struct nft_regs *regs,
42 const struct nft_pktinfo *pkt)
44 struct nft_exthdr *priv = nft_expr_priv(expr);
45 u32 *dest = &regs->data[priv->dreg];
46 unsigned int offset = 0;
47 int err;
49 err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
50 if (priv->flags & NFT_EXTHDR_F_PRESENT) {
51 *dest = (err >= 0);
52 return;
53 } else if (err < 0) {
54 goto err;
56 offset += priv->offset;
58 dest[priv->len / NFT_REG32_SIZE] = 0;
59 if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
60 goto err;
61 return;
62 err:
63 regs->verdict.code = NFT_BREAK;
66 static void *
67 nft_tcp_header_pointer(const struct nft_pktinfo *pkt,
68 unsigned int len, void *buffer, unsigned int *tcphdr_len)
70 struct tcphdr *tcph;
72 if (!pkt->tprot_set || pkt->tprot != IPPROTO_TCP)
73 return NULL;
75 tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, sizeof(*tcph), buffer);
76 if (!tcph)
77 return NULL;
79 *tcphdr_len = __tcp_hdrlen(tcph);
80 if (*tcphdr_len < sizeof(*tcph) || *tcphdr_len > len)
81 return NULL;
83 return skb_header_pointer(pkt->skb, pkt->xt.thoff, *tcphdr_len, buffer);
86 static void nft_exthdr_tcp_eval(const struct nft_expr *expr,
87 struct nft_regs *regs,
88 const struct nft_pktinfo *pkt)
90 u8 buff[sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE];
91 struct nft_exthdr *priv = nft_expr_priv(expr);
92 unsigned int i, optl, tcphdr_len, offset;
93 u32 *dest = &regs->data[priv->dreg];
94 struct tcphdr *tcph;
95 u8 *opt;
97 tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff, &tcphdr_len);
98 if (!tcph)
99 goto err;
101 opt = (u8 *)tcph;
102 for (i = sizeof(*tcph); i < tcphdr_len - 1; i += optl) {
103 optl = optlen(opt, i);
105 if (priv->type != opt[i])
106 continue;
108 if (i + optl > tcphdr_len || priv->len + priv->offset > optl)
109 goto err;
111 offset = i + priv->offset;
112 if (priv->flags & NFT_EXTHDR_F_PRESENT) {
113 *dest = 1;
114 } else {
115 dest[priv->len / NFT_REG32_SIZE] = 0;
116 memcpy(dest, opt + offset, priv->len);
119 return;
122 err:
123 if (priv->flags & NFT_EXTHDR_F_PRESENT)
124 *dest = 0;
125 else
126 regs->verdict.code = NFT_BREAK;
129 static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,
130 struct nft_regs *regs,
131 const struct nft_pktinfo *pkt)
133 u8 buff[sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE];
134 struct nft_exthdr *priv = nft_expr_priv(expr);
135 unsigned int i, optl, tcphdr_len, offset;
136 struct tcphdr *tcph;
137 u8 *opt;
138 u32 src;
140 tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff, &tcphdr_len);
141 if (!tcph)
142 return;
144 opt = (u8 *)tcph;
145 for (i = sizeof(*tcph); i < tcphdr_len - 1; i += optl) {
146 union {
147 u8 octet;
148 __be16 v16;
149 __be32 v32;
150 } old, new;
152 optl = optlen(opt, i);
154 if (priv->type != opt[i])
155 continue;
157 if (i + optl > tcphdr_len || priv->len + priv->offset > optl)
158 return;
160 if (!skb_make_writable(pkt->skb, pkt->xt.thoff + i + priv->len))
161 return;
163 tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff,
164 &tcphdr_len);
165 if (!tcph)
166 return;
168 src = regs->data[priv->sreg];
169 offset = i + priv->offset;
171 switch (priv->len) {
172 case 2:
173 old.v16 = get_unaligned((u16 *)(opt + offset));
174 new.v16 = src;
176 switch (priv->type) {
177 case TCPOPT_MSS:
178 /* increase can cause connection to stall */
179 if (ntohs(old.v16) <= ntohs(new.v16))
180 return;
181 break;
184 if (old.v16 == new.v16)
185 return;
187 put_unaligned(new.v16, (u16*)(opt + offset));
188 inet_proto_csum_replace2(&tcph->check, pkt->skb,
189 old.v16, new.v16, false);
190 break;
191 case 4:
192 new.v32 = src;
193 old.v32 = get_unaligned((u32 *)(opt + offset));
195 if (old.v32 == new.v32)
196 return;
198 put_unaligned(new.v32, (u32*)(opt + offset));
199 inet_proto_csum_replace4(&tcph->check, pkt->skb,
200 old.v32, new.v32, false);
201 break;
202 default:
203 WARN_ON_ONCE(1);
204 break;
207 return;
211 static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
212 [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
213 [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
214 [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
215 [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
216 [NFTA_EXTHDR_FLAGS] = { .type = NLA_U32 },
219 static int nft_exthdr_init(const struct nft_ctx *ctx,
220 const struct nft_expr *expr,
221 const struct nlattr * const tb[])
223 struct nft_exthdr *priv = nft_expr_priv(expr);
224 u32 offset, len, flags = 0, op = NFT_EXTHDR_OP_IPV6;
225 int err;
227 if (!tb[NFTA_EXTHDR_DREG] ||
228 !tb[NFTA_EXTHDR_TYPE] ||
229 !tb[NFTA_EXTHDR_OFFSET] ||
230 !tb[NFTA_EXTHDR_LEN])
231 return -EINVAL;
233 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
234 if (err < 0)
235 return err;
237 err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
238 if (err < 0)
239 return err;
241 if (tb[NFTA_EXTHDR_FLAGS]) {
242 err = nft_parse_u32_check(tb[NFTA_EXTHDR_FLAGS], U8_MAX, &flags);
243 if (err < 0)
244 return err;
246 if (flags & ~NFT_EXTHDR_F_PRESENT)
247 return -EINVAL;
250 if (tb[NFTA_EXTHDR_OP]) {
251 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OP], U8_MAX, &op);
252 if (err < 0)
253 return err;
256 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
257 priv->offset = offset;
258 priv->len = len;
259 priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
260 priv->flags = flags;
261 priv->op = op;
263 return nft_validate_register_store(ctx, priv->dreg, NULL,
264 NFT_DATA_VALUE, priv->len);
267 static int nft_exthdr_tcp_set_init(const struct nft_ctx *ctx,
268 const struct nft_expr *expr,
269 const struct nlattr * const tb[])
271 struct nft_exthdr *priv = nft_expr_priv(expr);
272 u32 offset, len, flags = 0, op = NFT_EXTHDR_OP_IPV6;
273 int err;
275 if (!tb[NFTA_EXTHDR_SREG] ||
276 !tb[NFTA_EXTHDR_TYPE] ||
277 !tb[NFTA_EXTHDR_OFFSET] ||
278 !tb[NFTA_EXTHDR_LEN])
279 return -EINVAL;
281 if (tb[NFTA_EXTHDR_DREG] || tb[NFTA_EXTHDR_FLAGS])
282 return -EINVAL;
284 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
285 if (err < 0)
286 return err;
288 err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
289 if (err < 0)
290 return err;
292 if (offset < 2)
293 return -EOPNOTSUPP;
295 switch (len) {
296 case 2: break;
297 case 4: break;
298 default:
299 return -EOPNOTSUPP;
302 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OP], U8_MAX, &op);
303 if (err < 0)
304 return err;
306 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
307 priv->offset = offset;
308 priv->len = len;
309 priv->sreg = nft_parse_register(tb[NFTA_EXTHDR_SREG]);
310 priv->flags = flags;
311 priv->op = op;
313 return nft_validate_register_load(priv->sreg, priv->len);
316 static int nft_exthdr_dump_common(struct sk_buff *skb, const struct nft_exthdr *priv)
318 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
319 goto nla_put_failure;
320 if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
321 goto nla_put_failure;
322 if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
323 goto nla_put_failure;
324 if (nla_put_be32(skb, NFTA_EXTHDR_FLAGS, htonl(priv->flags)))
325 goto nla_put_failure;
326 if (nla_put_be32(skb, NFTA_EXTHDR_OP, htonl(priv->op)))
327 goto nla_put_failure;
328 return 0;
330 nla_put_failure:
331 return -1;
334 static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
336 const struct nft_exthdr *priv = nft_expr_priv(expr);
338 if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
339 return -1;
341 return nft_exthdr_dump_common(skb, priv);
344 static int nft_exthdr_dump_set(struct sk_buff *skb, const struct nft_expr *expr)
346 const struct nft_exthdr *priv = nft_expr_priv(expr);
348 if (nft_dump_register(skb, NFTA_EXTHDR_SREG, priv->sreg))
349 return -1;
351 return nft_exthdr_dump_common(skb, priv);
354 static struct nft_expr_type nft_exthdr_type;
355 static const struct nft_expr_ops nft_exthdr_ipv6_ops = {
356 .type = &nft_exthdr_type,
357 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
358 .eval = nft_exthdr_ipv6_eval,
359 .init = nft_exthdr_init,
360 .dump = nft_exthdr_dump,
363 static const struct nft_expr_ops nft_exthdr_tcp_ops = {
364 .type = &nft_exthdr_type,
365 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
366 .eval = nft_exthdr_tcp_eval,
367 .init = nft_exthdr_init,
368 .dump = nft_exthdr_dump,
371 static const struct nft_expr_ops nft_exthdr_tcp_set_ops = {
372 .type = &nft_exthdr_type,
373 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
374 .eval = nft_exthdr_tcp_set_eval,
375 .init = nft_exthdr_tcp_set_init,
376 .dump = nft_exthdr_dump_set,
379 static const struct nft_expr_ops *
380 nft_exthdr_select_ops(const struct nft_ctx *ctx,
381 const struct nlattr * const tb[])
383 u32 op;
385 if (!tb[NFTA_EXTHDR_OP])
386 return &nft_exthdr_ipv6_ops;
388 if (tb[NFTA_EXTHDR_SREG] && tb[NFTA_EXTHDR_DREG])
389 return ERR_PTR(-EOPNOTSUPP);
391 op = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OP]));
392 switch (op) {
393 case NFT_EXTHDR_OP_TCPOPT:
394 if (tb[NFTA_EXTHDR_SREG])
395 return &nft_exthdr_tcp_set_ops;
396 if (tb[NFTA_EXTHDR_DREG])
397 return &nft_exthdr_tcp_ops;
398 break;
399 case NFT_EXTHDR_OP_IPV6:
400 if (tb[NFTA_EXTHDR_DREG])
401 return &nft_exthdr_ipv6_ops;
402 break;
405 return ERR_PTR(-EOPNOTSUPP);
408 static struct nft_expr_type nft_exthdr_type __read_mostly = {
409 .name = "exthdr",
410 .select_ops = nft_exthdr_select_ops,
411 .policy = nft_exthdr_policy,
412 .maxattr = NFTA_EXTHDR_MAX,
413 .owner = THIS_MODULE,
416 static int __init nft_exthdr_module_init(void)
418 return nft_register_expr(&nft_exthdr_type);
421 static void __exit nft_exthdr_module_exit(void)
423 nft_unregister_expr(&nft_exthdr_type);
426 module_init(nft_exthdr_module_init);
427 module_exit(nft_exthdr_module_exit);
429 MODULE_LICENSE("GPL");
430 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
431 MODULE_ALIAS_NFT_EXPR("exthdr");