blob: 47beb3abcc9daf46e084c0f189eaf7091d11241e [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
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 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18// FIXME:
19#include <net/ipv6.h>
20
21struct nft_exthdr {
22 u8 type;
23 u8 offset;
24 u8 len;
25 enum nft_registers dreg:8;
26};
27
28static void nft_exthdr_eval(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010029 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +020030 const struct nft_pktinfo *pkt)
31{
32 struct nft_exthdr *priv = nft_expr_priv(expr);
Patrick McHardy49499c32015-04-11 02:27:37 +010033 u32 *dest = &regs->data[priv->dreg];
Daniel Borkmann540436c2013-12-20 11:23:15 +010034 unsigned int offset = 0;
Patrick McHardy96518512013-10-14 11:00:02 +020035 int err;
36
37 err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
38 if (err < 0)
39 goto err;
40 offset += priv->offset;
41
Patrick McHardy49499c32015-04-11 02:27:37 +010042 dest[priv->len / NFT_REG32_SIZE] = 0;
Patrick McHardyfad136e2015-04-11 02:27:33 +010043 if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
Patrick McHardy96518512013-10-14 11:00:02 +020044 goto err;
45 return;
46err:
Patrick McHardya55e22e2015-04-11 02:27:31 +010047 regs->verdict.code = NFT_BREAK;
Patrick McHardy96518512013-10-14 11:00:02 +020048}
49
50static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
51 [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
52 [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
53 [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
54 [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
55};
56
57static int nft_exthdr_init(const struct nft_ctx *ctx,
58 const struct nft_expr *expr,
59 const struct nlattr * const tb[])
60{
61 struct nft_exthdr *priv = nft_expr_priv(expr);
Dan Carpenter21a9e0f2016-10-12 09:09:12 +030062 u32 offset, len;
63 int err;
Patrick McHardy96518512013-10-14 11:00:02 +020064
65 if (tb[NFTA_EXTHDR_DREG] == NULL ||
66 tb[NFTA_EXTHDR_TYPE] == NULL ||
67 tb[NFTA_EXTHDR_OFFSET] == NULL ||
68 tb[NFTA_EXTHDR_LEN] == NULL)
69 return -EINVAL;
70
Laura Garcia Liebana36b701f2016-09-14 15:00:02 +020071 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
72 if (err < 0)
73 return err;
Laura Garcia Liebana4da449a2016-08-09 20:46:16 +020074
Laura Garcia Liebana36b701f2016-09-14 15:00:02 +020075 err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
76 if (err < 0)
77 return err;
Laura Garcia Liebana4da449a2016-08-09 20:46:16 +020078
Patrick McHardy96518512013-10-14 11:00:02 +020079 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
Laura Garcia Liebana4da449a2016-08-09 20:46:16 +020080 priv->offset = offset;
81 priv->len = len;
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010082 priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
Patrick McHardy96518512013-10-14 11:00:02 +020083
Patrick McHardy1ec10212015-04-11 02:27:27 +010084 return nft_validate_register_store(ctx, priv->dreg, NULL,
85 NFT_DATA_VALUE, priv->len);
Patrick McHardy96518512013-10-14 11:00:02 +020086}
87
88static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
89{
90 const struct nft_exthdr *priv = nft_expr_priv(expr);
91
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010092 if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
Patrick McHardy96518512013-10-14 11:00:02 +020093 goto nla_put_failure;
94 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
95 goto nla_put_failure;
96 if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
97 goto nla_put_failure;
98 if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
99 goto nla_put_failure;
100 return 0;
101
102nla_put_failure:
103 return -1;
104}
105
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200106static struct nft_expr_type nft_exthdr_type;
107static const struct nft_expr_ops nft_exthdr_ops = {
108 .type = &nft_exthdr_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200109 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
Patrick McHardy96518512013-10-14 11:00:02 +0200110 .eval = nft_exthdr_eval,
111 .init = nft_exthdr_init,
112 .dump = nft_exthdr_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200113};
114
115static struct nft_expr_type nft_exthdr_type __read_mostly = {
116 .name = "exthdr",
117 .ops = &nft_exthdr_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200118 .policy = nft_exthdr_policy,
119 .maxattr = NFTA_EXTHDR_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200120 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200121};
122
123static int __init nft_exthdr_module_init(void)
124{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200125 return nft_register_expr(&nft_exthdr_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200126}
127
128static void __exit nft_exthdr_module_exit(void)
129{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200130 nft_unregister_expr(&nft_exthdr_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200131}
132
133module_init(nft_exthdr_module_init);
134module_exit(nft_exthdr_module_exit);
135
136MODULE_LICENSE("GPL");
137MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
138MODULE_ALIAS_NFT_EXPR("exthdr");