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/)
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>
25 enum nft_registers dreg:8;
28 static void nft_exthdr_eval(const struct nft_expr *expr,
29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt)
32 struct nft_exthdr *priv = nft_expr_priv(expr);
33 u32 *dest = ®s->data[priv->dreg];
34 unsigned int offset = 0;
37 if (pkt->skb->protocol != htons(ETH_P_IPV6))
40 err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
43 offset += priv->offset;
45 dest[priv->len / NFT_REG32_SIZE] = 0;
46 if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
50 regs->verdict.code = NFT_BREAK;
53 static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
54 [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
55 [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
56 [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
57 [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
60 static int nft_exthdr_init(const struct nft_ctx *ctx,
61 const struct nft_expr *expr,
62 const struct nlattr * const tb[])
64 struct nft_exthdr *priv = nft_expr_priv(expr);
68 if (tb[NFTA_EXTHDR_DREG] == NULL ||
69 tb[NFTA_EXTHDR_TYPE] == NULL ||
70 tb[NFTA_EXTHDR_OFFSET] == NULL ||
71 tb[NFTA_EXTHDR_LEN] == NULL)
74 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
78 err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
82 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
83 priv->offset = offset;
85 priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
87 return nft_validate_register_store(ctx, priv->dreg, NULL,
88 NFT_DATA_VALUE, priv->len);
91 static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
93 const struct nft_exthdr *priv = nft_expr_priv(expr);
95 if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
97 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
99 if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
100 goto nla_put_failure;
101 if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
102 goto nla_put_failure;
109 static struct nft_expr_type nft_exthdr_type;
110 static const struct nft_expr_ops nft_exthdr_ops = {
111 .type = &nft_exthdr_type,
112 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
113 .eval = nft_exthdr_eval,
114 .init = nft_exthdr_init,
115 .dump = nft_exthdr_dump,
118 static struct nft_expr_type nft_exthdr_type __read_mostly = {
120 .ops = &nft_exthdr_ops,
121 .policy = nft_exthdr_policy,
122 .maxattr = NFTA_EXTHDR_MAX,
123 .owner = THIS_MODULE,
126 static int __init nft_exthdr_module_init(void)
128 return nft_register_expr(&nft_exthdr_type);
131 static void __exit nft_exthdr_module_exit(void)
133 nft_unregister_expr(&nft_exthdr_type);
136 module_init(nft_exthdr_module_init);
137 module_exit(nft_exthdr_module_exit);
139 MODULE_LICENSE("GPL");
140 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
141 MODULE_ALIAS_NFT_EXPR("exthdr");