Merge tag 'nfc-next-4.1-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo...
[deliverable/linux.git] / net / netfilter / nft_ct.c
1 /*
2 * Copyright (c) 2008-2009 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 #include <net/netfilter/nf_conntrack.h>
19 #include <net/netfilter/nf_conntrack_tuple.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <net/netfilter/nf_conntrack_ecache.h>
22 #include <net/netfilter/nf_conntrack_labels.h>
23
24 struct nft_ct {
25 enum nft_ct_keys key:8;
26 enum ip_conntrack_dir dir:8;
27 union {
28 enum nft_registers dreg:8;
29 enum nft_registers sreg:8;
30 };
31 };
32
33 static void nft_ct_get_eval(const struct nft_expr *expr,
34 struct nft_data data[NFT_REG_MAX + 1],
35 const struct nft_pktinfo *pkt)
36 {
37 const struct nft_ct *priv = nft_expr_priv(expr);
38 struct nft_data *dest = &data[priv->dreg];
39 enum ip_conntrack_info ctinfo;
40 const struct nf_conn *ct;
41 const struct nf_conn_help *help;
42 const struct nf_conntrack_tuple *tuple;
43 const struct nf_conntrack_helper *helper;
44 long diff;
45 unsigned int state;
46
47 ct = nf_ct_get(pkt->skb, &ctinfo);
48
49 switch (priv->key) {
50 case NFT_CT_STATE:
51 if (ct == NULL)
52 state = NF_CT_STATE_INVALID_BIT;
53 else if (nf_ct_is_untracked(ct))
54 state = NF_CT_STATE_UNTRACKED_BIT;
55 else
56 state = NF_CT_STATE_BIT(ctinfo);
57 dest->data[0] = state;
58 return;
59 default:
60 break;
61 }
62
63 if (ct == NULL)
64 goto err;
65
66 switch (priv->key) {
67 case NFT_CT_DIRECTION:
68 dest->data[0] = CTINFO2DIR(ctinfo);
69 return;
70 case NFT_CT_STATUS:
71 dest->data[0] = ct->status;
72 return;
73 #ifdef CONFIG_NF_CONNTRACK_MARK
74 case NFT_CT_MARK:
75 dest->data[0] = ct->mark;
76 return;
77 #endif
78 #ifdef CONFIG_NF_CONNTRACK_SECMARK
79 case NFT_CT_SECMARK:
80 dest->data[0] = ct->secmark;
81 return;
82 #endif
83 case NFT_CT_EXPIRATION:
84 diff = (long)jiffies - (long)ct->timeout.expires;
85 if (diff < 0)
86 diff = 0;
87 dest->data[0] = jiffies_to_msecs(diff);
88 return;
89 case NFT_CT_HELPER:
90 if (ct->master == NULL)
91 goto err;
92 help = nfct_help(ct->master);
93 if (help == NULL)
94 goto err;
95 helper = rcu_dereference(help->helper);
96 if (helper == NULL)
97 goto err;
98 if (strlen(helper->name) >= sizeof(dest->data))
99 goto err;
100 strncpy((char *)dest->data, helper->name, sizeof(dest->data));
101 return;
102 #ifdef CONFIG_NF_CONNTRACK_LABELS
103 case NFT_CT_LABELS: {
104 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
105 unsigned int size;
106
107 if (!labels) {
108 memset(dest->data, 0, sizeof(dest->data));
109 return;
110 }
111
112 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > sizeof(dest->data));
113 size = labels->words * sizeof(long);
114
115 memcpy(dest->data, labels->bits, size);
116 if (size < sizeof(dest->data))
117 memset(((char *) dest->data) + size, 0,
118 sizeof(dest->data) - size);
119 return;
120 }
121 #endif
122 default:
123 break;
124 }
125
126 tuple = &ct->tuplehash[priv->dir].tuple;
127 switch (priv->key) {
128 case NFT_CT_L3PROTOCOL:
129 dest->data[0] = nf_ct_l3num(ct);
130 return;
131 case NFT_CT_SRC:
132 memcpy(dest->data, tuple->src.u3.all,
133 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
134 return;
135 case NFT_CT_DST:
136 memcpy(dest->data, tuple->dst.u3.all,
137 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
138 return;
139 case NFT_CT_PROTOCOL:
140 dest->data[0] = nf_ct_protonum(ct);
141 return;
142 case NFT_CT_PROTO_SRC:
143 dest->data[0] = (__force __u16)tuple->src.u.all;
144 return;
145 case NFT_CT_PROTO_DST:
146 dest->data[0] = (__force __u16)tuple->dst.u.all;
147 return;
148 default:
149 break;
150 }
151 return;
152 err:
153 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
154 }
155
156 static void nft_ct_set_eval(const struct nft_expr *expr,
157 struct nft_data data[NFT_REG_MAX + 1],
158 const struct nft_pktinfo *pkt)
159 {
160 const struct nft_ct *priv = nft_expr_priv(expr);
161 struct sk_buff *skb = pkt->skb;
162 #ifdef CONFIG_NF_CONNTRACK_MARK
163 u32 value = data[priv->sreg].data[0];
164 #endif
165 enum ip_conntrack_info ctinfo;
166 struct nf_conn *ct;
167
168 ct = nf_ct_get(skb, &ctinfo);
169 if (ct == NULL)
170 return;
171
172 switch (priv->key) {
173 #ifdef CONFIG_NF_CONNTRACK_MARK
174 case NFT_CT_MARK:
175 if (ct->mark != value) {
176 ct->mark = value;
177 nf_conntrack_event_cache(IPCT_MARK, ct);
178 }
179 break;
180 #endif
181 default:
182 break;
183 }
184 }
185
186 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
187 [NFTA_CT_DREG] = { .type = NLA_U32 },
188 [NFTA_CT_KEY] = { .type = NLA_U32 },
189 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
190 [NFTA_CT_SREG] = { .type = NLA_U32 },
191 };
192
193 static int nft_ct_l3proto_try_module_get(uint8_t family)
194 {
195 int err;
196
197 if (family == NFPROTO_INET) {
198 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4);
199 if (err < 0)
200 goto err1;
201 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6);
202 if (err < 0)
203 goto err2;
204 } else {
205 err = nf_ct_l3proto_try_module_get(family);
206 if (err < 0)
207 goto err1;
208 }
209 return 0;
210
211 err2:
212 nf_ct_l3proto_module_put(NFPROTO_IPV4);
213 err1:
214 return err;
215 }
216
217 static void nft_ct_l3proto_module_put(uint8_t family)
218 {
219 if (family == NFPROTO_INET) {
220 nf_ct_l3proto_module_put(NFPROTO_IPV4);
221 nf_ct_l3proto_module_put(NFPROTO_IPV6);
222 } else
223 nf_ct_l3proto_module_put(family);
224 }
225
226 static int nft_ct_get_init(const struct nft_ctx *ctx,
227 const struct nft_expr *expr,
228 const struct nlattr * const tb[])
229 {
230 struct nft_ct *priv = nft_expr_priv(expr);
231 int err;
232
233 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
234 switch (priv->key) {
235 case NFT_CT_STATE:
236 case NFT_CT_DIRECTION:
237 case NFT_CT_STATUS:
238 #ifdef CONFIG_NF_CONNTRACK_MARK
239 case NFT_CT_MARK:
240 #endif
241 #ifdef CONFIG_NF_CONNTRACK_SECMARK
242 case NFT_CT_SECMARK:
243 #endif
244 #ifdef CONFIG_NF_CONNTRACK_LABELS
245 case NFT_CT_LABELS:
246 #endif
247 case NFT_CT_EXPIRATION:
248 case NFT_CT_HELPER:
249 if (tb[NFTA_CT_DIRECTION] != NULL)
250 return -EINVAL;
251 break;
252 case NFT_CT_L3PROTOCOL:
253 case NFT_CT_PROTOCOL:
254 case NFT_CT_SRC:
255 case NFT_CT_DST:
256 case NFT_CT_PROTO_SRC:
257 case NFT_CT_PROTO_DST:
258 if (tb[NFTA_CT_DIRECTION] == NULL)
259 return -EINVAL;
260 break;
261 default:
262 return -EOPNOTSUPP;
263 }
264
265 if (tb[NFTA_CT_DIRECTION] != NULL) {
266 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
267 switch (priv->dir) {
268 case IP_CT_DIR_ORIGINAL:
269 case IP_CT_DIR_REPLY:
270 break;
271 default:
272 return -EINVAL;
273 }
274 }
275
276 priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
277 err = nft_validate_output_register(priv->dreg);
278 if (err < 0)
279 return err;
280
281 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
282 if (err < 0)
283 return err;
284
285 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
286 if (err < 0)
287 return err;
288
289 return 0;
290 }
291
292 static int nft_ct_set_init(const struct nft_ctx *ctx,
293 const struct nft_expr *expr,
294 const struct nlattr * const tb[])
295 {
296 struct nft_ct *priv = nft_expr_priv(expr);
297 int err;
298
299 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
300 switch (priv->key) {
301 #ifdef CONFIG_NF_CONNTRACK_MARK
302 case NFT_CT_MARK:
303 break;
304 #endif
305 default:
306 return -EOPNOTSUPP;
307 }
308
309 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
310 err = nft_validate_input_register(priv->sreg);
311 if (err < 0)
312 return err;
313
314 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
315 if (err < 0)
316 return err;
317
318 return 0;
319 }
320
321 static void nft_ct_destroy(const struct nft_ctx *ctx,
322 const struct nft_expr *expr)
323 {
324 nft_ct_l3proto_module_put(ctx->afi->family);
325 }
326
327 static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
328 {
329 const struct nft_ct *priv = nft_expr_priv(expr);
330
331 if (nla_put_be32(skb, NFTA_CT_DREG, htonl(priv->dreg)))
332 goto nla_put_failure;
333 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
334 goto nla_put_failure;
335
336 switch (priv->key) {
337 case NFT_CT_PROTOCOL:
338 case NFT_CT_SRC:
339 case NFT_CT_DST:
340 case NFT_CT_PROTO_SRC:
341 case NFT_CT_PROTO_DST:
342 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
343 goto nla_put_failure;
344 default:
345 break;
346 }
347
348 return 0;
349
350 nla_put_failure:
351 return -1;
352 }
353
354 static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
355 {
356 const struct nft_ct *priv = nft_expr_priv(expr);
357
358 if (nla_put_be32(skb, NFTA_CT_SREG, htonl(priv->sreg)))
359 goto nla_put_failure;
360 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
361 goto nla_put_failure;
362 return 0;
363
364 nla_put_failure:
365 return -1;
366 }
367
368 static struct nft_expr_type nft_ct_type;
369 static const struct nft_expr_ops nft_ct_get_ops = {
370 .type = &nft_ct_type,
371 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
372 .eval = nft_ct_get_eval,
373 .init = nft_ct_get_init,
374 .destroy = nft_ct_destroy,
375 .dump = nft_ct_get_dump,
376 };
377
378 static const struct nft_expr_ops nft_ct_set_ops = {
379 .type = &nft_ct_type,
380 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
381 .eval = nft_ct_set_eval,
382 .init = nft_ct_set_init,
383 .destroy = nft_ct_destroy,
384 .dump = nft_ct_set_dump,
385 };
386
387 static const struct nft_expr_ops *
388 nft_ct_select_ops(const struct nft_ctx *ctx,
389 const struct nlattr * const tb[])
390 {
391 if (tb[NFTA_CT_KEY] == NULL)
392 return ERR_PTR(-EINVAL);
393
394 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
395 return ERR_PTR(-EINVAL);
396
397 if (tb[NFTA_CT_DREG])
398 return &nft_ct_get_ops;
399
400 if (tb[NFTA_CT_SREG])
401 return &nft_ct_set_ops;
402
403 return ERR_PTR(-EINVAL);
404 }
405
406 static struct nft_expr_type nft_ct_type __read_mostly = {
407 .name = "ct",
408 .select_ops = &nft_ct_select_ops,
409 .policy = nft_ct_policy,
410 .maxattr = NFTA_CT_MAX,
411 .owner = THIS_MODULE,
412 };
413
414 static int __init nft_ct_module_init(void)
415 {
416 return nft_register_expr(&nft_ct_type);
417 }
418
419 static void __exit nft_ct_module_exit(void)
420 {
421 nft_unregister_expr(&nft_ct_type);
422 }
423
424 module_init(nft_ct_module_init);
425 module_exit(nft_ct_module_exit);
426
427 MODULE_LICENSE("GPL");
428 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
429 MODULE_ALIAS_NFT_EXPR("ct");
This page took 0.04085 seconds and 6 git commands to generate.