]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_exthdr.c
netfilter: nft_exthdr: Add support for existence check
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nft_exthdr.c
CommitLineData
96518518
PM
1/*
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;
c078ca3b 26 u8 flags;
96518518
PM
27};
28
29static void nft_exthdr_eval(const struct nft_expr *expr,
a55e22e9 30 struct nft_regs *regs,
96518518
PM
31 const struct nft_pktinfo *pkt)
32{
33 struct nft_exthdr *priv = nft_expr_priv(expr);
49499c3e 34 u32 *dest = &regs->data[priv->dreg];
540436c8 35 unsigned int offset = 0;
96518518
PM
36 int err;
37
38 err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
c078ca3b
PS
39 if (priv->flags & NFT_EXTHDR_F_PRESENT) {
40 *dest = (err >= 0);
41 return;
42 } else if (err < 0) {
96518518 43 goto err;
c078ca3b 44 }
96518518
PM
45 offset += priv->offset;
46
49499c3e 47 dest[priv->len / NFT_REG32_SIZE] = 0;
fad136ea 48 if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
96518518
PM
49 goto err;
50 return;
51err:
a55e22e9 52 regs->verdict.code = NFT_BREAK;
96518518
PM
53}
54
55static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
56 [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
57 [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
58 [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
59 [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
c078ca3b 60 [NFTA_EXTHDR_FLAGS] = { .type = NLA_U32 },
96518518
PM
61};
62
63static int nft_exthdr_init(const struct nft_ctx *ctx,
64 const struct nft_expr *expr,
65 const struct nlattr * const tb[])
66{
67 struct nft_exthdr *priv = nft_expr_priv(expr);
c078ca3b 68 u32 offset, len, flags = 0;
21a9e0f1 69 int err;
96518518
PM
70
71 if (tb[NFTA_EXTHDR_DREG] == NULL ||
72 tb[NFTA_EXTHDR_TYPE] == NULL ||
73 tb[NFTA_EXTHDR_OFFSET] == NULL ||
74 tb[NFTA_EXTHDR_LEN] == NULL)
75 return -EINVAL;
76
36b701fa
LGL
77 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
78 if (err < 0)
79 return err;
4da449ae 80
36b701fa
LGL
81 err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
82 if (err < 0)
83 return err;
4da449ae 84
c078ca3b
PS
85 if (tb[NFTA_EXTHDR_FLAGS]) {
86 err = nft_parse_u32_check(tb[NFTA_EXTHDR_FLAGS], U8_MAX, &flags);
87 if (err < 0)
88 return err;
89
90 if (flags & ~NFT_EXTHDR_F_PRESENT)
91 return -EINVAL;
92 }
93
96518518 94 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
4da449ae
LGL
95 priv->offset = offset;
96 priv->len = len;
b1c96ed3 97 priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
c078ca3b 98 priv->flags = flags;
96518518 99
1ec10212
PM
100 return nft_validate_register_store(ctx, priv->dreg, NULL,
101 NFT_DATA_VALUE, priv->len);
96518518
PM
102}
103
104static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
105{
106 const struct nft_exthdr *priv = nft_expr_priv(expr);
107
b1c96ed3 108 if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
96518518
PM
109 goto nla_put_failure;
110 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
111 goto nla_put_failure;
112 if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
113 goto nla_put_failure;
114 if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
115 goto nla_put_failure;
c078ca3b
PS
116 if (nla_put_be32(skb, NFTA_EXTHDR_FLAGS, htonl(priv->flags)))
117 goto nla_put_failure;
96518518
PM
118 return 0;
119
120nla_put_failure:
121 return -1;
122}
123
ef1f7df9
PM
124static struct nft_expr_type nft_exthdr_type;
125static const struct nft_expr_ops nft_exthdr_ops = {
126 .type = &nft_exthdr_type,
96518518 127 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
96518518
PM
128 .eval = nft_exthdr_eval,
129 .init = nft_exthdr_init,
130 .dump = nft_exthdr_dump,
ef1f7df9
PM
131};
132
133static struct nft_expr_type nft_exthdr_type __read_mostly = {
134 .name = "exthdr",
135 .ops = &nft_exthdr_ops,
96518518
PM
136 .policy = nft_exthdr_policy,
137 .maxattr = NFTA_EXTHDR_MAX,
ef1f7df9 138 .owner = THIS_MODULE,
96518518
PM
139};
140
141static int __init nft_exthdr_module_init(void)
142{
ef1f7df9 143 return nft_register_expr(&nft_exthdr_type);
96518518
PM
144}
145
146static void __exit nft_exthdr_module_exit(void)
147{
ef1f7df9 148 nft_unregister_expr(&nft_exthdr_type);
96518518
PM
149}
150
151module_init(nft_exthdr_module_init);
152module_exit(nft_exthdr_module_exit);
153
154MODULE_LICENSE("GPL");
155MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
156MODULE_ALIAS_NFT_EXPR("exthdr");