Commit | Line | Data |
---|---|---|
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> | |
935b7f64 | 18 | #include <net/tcp.h> |
96518518 PM |
19 | |
20 | struct nft_exthdr { | |
21 | u8 type; | |
22 | u8 offset; | |
23 | u8 len; | |
935b7f64 | 24 | u8 op; |
96518518 | 25 | enum nft_registers dreg:8; |
c078ca3b | 26 | u8 flags; |
96518518 PM |
27 | }; |
28 | ||
935b7f64 MM |
29 | static unsigned int optlen(const u8 *opt, unsigned int offset) |
30 | { | |
31 | /* Beware zero-length options: make finite progress */ | |
32 | if (opt[offset] <= TCPOPT_NOP || opt[offset + 1] == 0) | |
33 | return 1; | |
34 | else | |
35 | return opt[offset + 1]; | |
36 | } | |
37 | ||
38 | static void nft_exthdr_ipv6_eval(const struct nft_expr *expr, | |
39 | struct nft_regs *regs, | |
40 | const struct nft_pktinfo *pkt) | |
96518518 PM |
41 | { |
42 | struct nft_exthdr *priv = nft_expr_priv(expr); | |
49499c3e | 43 | u32 *dest = ®s->data[priv->dreg]; |
540436c8 | 44 | unsigned int offset = 0; |
96518518 PM |
45 | int err; |
46 | ||
47 | err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL); | |
c078ca3b PS |
48 | if (priv->flags & NFT_EXTHDR_F_PRESENT) { |
49 | *dest = (err >= 0); | |
50 | return; | |
51 | } else if (err < 0) { | |
96518518 | 52 | goto err; |
c078ca3b | 53 | } |
96518518 PM |
54 | offset += priv->offset; |
55 | ||
49499c3e | 56 | dest[priv->len / NFT_REG32_SIZE] = 0; |
fad136ea | 57 | if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0) |
96518518 PM |
58 | goto err; |
59 | return; | |
60 | err: | |
a55e22e9 | 61 | regs->verdict.code = NFT_BREAK; |
96518518 PM |
62 | } |
63 | ||
935b7f64 MM |
64 | static void nft_exthdr_tcp_eval(const struct nft_expr *expr, |
65 | struct nft_regs *regs, | |
66 | const struct nft_pktinfo *pkt) | |
67 | { | |
68 | u8 buff[sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE]; | |
69 | struct nft_exthdr *priv = nft_expr_priv(expr); | |
70 | unsigned int i, optl, tcphdr_len, offset; | |
71 | u32 *dest = ®s->data[priv->dreg]; | |
72 | struct tcphdr *tcph; | |
73 | u8 *opt; | |
74 | ||
75 | if (!pkt->tprot_set || pkt->tprot != IPPROTO_TCP) | |
76 | goto err; | |
77 | ||
78 | tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, sizeof(*tcph), buff); | |
79 | if (!tcph) | |
80 | goto err; | |
81 | ||
82 | tcphdr_len = __tcp_hdrlen(tcph); | |
83 | if (tcphdr_len < sizeof(*tcph)) | |
84 | goto err; | |
85 | ||
86 | tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, tcphdr_len, buff); | |
87 | if (!tcph) | |
88 | goto err; | |
89 | ||
90 | opt = (u8 *)tcph; | |
91 | for (i = sizeof(*tcph); i < tcphdr_len - 1; i += optl) { | |
92 | optl = optlen(opt, i); | |
93 | ||
94 | if (priv->type != opt[i]) | |
95 | continue; | |
96 | ||
97 | if (i + optl > tcphdr_len || priv->len + priv->offset > optl) | |
98 | goto err; | |
99 | ||
100 | offset = i + priv->offset; | |
101 | dest[priv->len / NFT_REG32_SIZE] = 0; | |
102 | memcpy(dest, opt + offset, priv->len); | |
103 | ||
104 | return; | |
105 | } | |
106 | ||
107 | err: | |
108 | regs->verdict.code = NFT_BREAK; | |
109 | } | |
110 | ||
96518518 PM |
111 | static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = { |
112 | [NFTA_EXTHDR_DREG] = { .type = NLA_U32 }, | |
113 | [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 }, | |
114 | [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 }, | |
115 | [NFTA_EXTHDR_LEN] = { .type = NLA_U32 }, | |
c078ca3b | 116 | [NFTA_EXTHDR_FLAGS] = { .type = NLA_U32 }, |
96518518 PM |
117 | }; |
118 | ||
119 | static int nft_exthdr_init(const struct nft_ctx *ctx, | |
120 | const struct nft_expr *expr, | |
121 | const struct nlattr * const tb[]) | |
122 | { | |
123 | struct nft_exthdr *priv = nft_expr_priv(expr); | |
935b7f64 | 124 | u32 offset, len, flags = 0, op = NFT_EXTHDR_OP_IPV6; |
21a9e0f1 | 125 | int err; |
96518518 | 126 | |
935b7f64 MM |
127 | if (!tb[NFTA_EXTHDR_DREG] || |
128 | !tb[NFTA_EXTHDR_TYPE] || | |
129 | !tb[NFTA_EXTHDR_OFFSET] || | |
130 | !tb[NFTA_EXTHDR_LEN]) | |
96518518 PM |
131 | return -EINVAL; |
132 | ||
36b701fa LGL |
133 | err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset); |
134 | if (err < 0) | |
135 | return err; | |
4da449ae | 136 | |
36b701fa LGL |
137 | err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len); |
138 | if (err < 0) | |
139 | return err; | |
4da449ae | 140 | |
c078ca3b PS |
141 | if (tb[NFTA_EXTHDR_FLAGS]) { |
142 | err = nft_parse_u32_check(tb[NFTA_EXTHDR_FLAGS], U8_MAX, &flags); | |
143 | if (err < 0) | |
144 | return err; | |
145 | ||
146 | if (flags & ~NFT_EXTHDR_F_PRESENT) | |
147 | return -EINVAL; | |
148 | } | |
149 | ||
935b7f64 MM |
150 | if (tb[NFTA_EXTHDR_OP]) { |
151 | err = nft_parse_u32_check(tb[NFTA_EXTHDR_OP], U8_MAX, &op); | |
152 | if (err < 0) | |
153 | return err; | |
154 | } | |
155 | ||
96518518 | 156 | priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]); |
4da449ae LGL |
157 | priv->offset = offset; |
158 | priv->len = len; | |
b1c96ed3 | 159 | priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]); |
c078ca3b | 160 | priv->flags = flags; |
935b7f64 | 161 | priv->op = op; |
96518518 | 162 | |
1ec10212 PM |
163 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
164 | NFT_DATA_VALUE, priv->len); | |
96518518 PM |
165 | } |
166 | ||
167 | static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
168 | { | |
169 | const struct nft_exthdr *priv = nft_expr_priv(expr); | |
170 | ||
b1c96ed3 | 171 | if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg)) |
96518518 PM |
172 | goto nla_put_failure; |
173 | if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type)) | |
174 | goto nla_put_failure; | |
175 | if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset))) | |
176 | goto nla_put_failure; | |
177 | if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len))) | |
178 | goto nla_put_failure; | |
c078ca3b PS |
179 | if (nla_put_be32(skb, NFTA_EXTHDR_FLAGS, htonl(priv->flags))) |
180 | goto nla_put_failure; | |
935b7f64 MM |
181 | if (nla_put_be32(skb, NFTA_EXTHDR_OP, htonl(priv->op))) |
182 | goto nla_put_failure; | |
96518518 PM |
183 | return 0; |
184 | ||
185 | nla_put_failure: | |
186 | return -1; | |
187 | } | |
188 | ||
ef1f7df9 | 189 | static struct nft_expr_type nft_exthdr_type; |
935b7f64 MM |
190 | static const struct nft_expr_ops nft_exthdr_ipv6_ops = { |
191 | .type = &nft_exthdr_type, | |
192 | .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)), | |
193 | .eval = nft_exthdr_ipv6_eval, | |
194 | .init = nft_exthdr_init, | |
195 | .dump = nft_exthdr_dump, | |
196 | }; | |
197 | ||
198 | static const struct nft_expr_ops nft_exthdr_tcp_ops = { | |
ef1f7df9 | 199 | .type = &nft_exthdr_type, |
96518518 | 200 | .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)), |
935b7f64 | 201 | .eval = nft_exthdr_tcp_eval, |
96518518 PM |
202 | .init = nft_exthdr_init, |
203 | .dump = nft_exthdr_dump, | |
ef1f7df9 PM |
204 | }; |
205 | ||
935b7f64 MM |
206 | static const struct nft_expr_ops * |
207 | nft_exthdr_select_ops(const struct nft_ctx *ctx, | |
208 | const struct nlattr * const tb[]) | |
209 | { | |
210 | u32 op; | |
211 | ||
212 | if (!tb[NFTA_EXTHDR_OP]) | |
213 | return &nft_exthdr_ipv6_ops; | |
214 | ||
215 | op = ntohl(nla_get_u32(tb[NFTA_EXTHDR_OP])); | |
216 | switch (op) { | |
217 | case NFT_EXTHDR_OP_TCPOPT: | |
218 | return &nft_exthdr_tcp_ops; | |
219 | case NFT_EXTHDR_OP_IPV6: | |
220 | return &nft_exthdr_ipv6_ops; | |
221 | } | |
222 | ||
223 | return ERR_PTR(-EOPNOTSUPP); | |
224 | } | |
225 | ||
ef1f7df9 PM |
226 | static struct nft_expr_type nft_exthdr_type __read_mostly = { |
227 | .name = "exthdr", | |
935b7f64 | 228 | .select_ops = &nft_exthdr_select_ops, |
96518518 PM |
229 | .policy = nft_exthdr_policy, |
230 | .maxattr = NFTA_EXTHDR_MAX, | |
ef1f7df9 | 231 | .owner = THIS_MODULE, |
96518518 PM |
232 | }; |
233 | ||
234 | static int __init nft_exthdr_module_init(void) | |
235 | { | |
ef1f7df9 | 236 | return nft_register_expr(&nft_exthdr_type); |
96518518 PM |
237 | } |
238 | ||
239 | static void __exit nft_exthdr_module_exit(void) | |
240 | { | |
ef1f7df9 | 241 | nft_unregister_expr(&nft_exthdr_type); |
96518518 PM |
242 | } |
243 | ||
244 | module_init(nft_exthdr_module_init); | |
245 | module_exit(nft_exthdr_module_exit); | |
246 | ||
247 | MODULE_LICENSE("GPL"); | |
248 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
249 | MODULE_ALIAS_NFT_EXPR("exthdr"); |