netfilter: Fix switch statement warnings with recent gcc.
[deliverable/linux.git] / net / netfilter / nft_ct.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
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>
c4ede3d3 21#include <net/netfilter/nf_conntrack_ecache.h>
d2bf2f34 22#include <net/netfilter/nf_conntrack_labels.h>
96518518
PM
23
24struct nft_ct {
25 enum nft_ct_keys key:8;
26 enum ip_conntrack_dir dir:8;
d46f2cd2 27 union {
c4ede3d3
KE
28 enum nft_registers dreg:8;
29 enum nft_registers sreg:8;
30 };
96518518
PM
31};
32
c4ede3d3
KE
33static 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)
96518518
PM
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;
c1f86676
DM
59 default:
60 break;
96518518
PM
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;
d2bf2f34
FW
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
c1f86676
DM
122 default:
123 break;
96518518
PM
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;
c1f86676
DM
148 default:
149 break;
96518518
PM
150 }
151 return;
152err:
153 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
154}
155
c4ede3d3
KE
156static 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;
847c8e29 162#ifdef CONFIG_NF_CONNTRACK_MARK
c4ede3d3 163 u32 value = data[priv->sreg].data[0];
847c8e29 164#endif
c4ede3d3
KE
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
c1f86676
DM
181 default:
182 break;
c4ede3d3
KE
183 }
184}
185
96518518
PM
186static 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 },
c4ede3d3 190 [NFTA_CT_SREG] = { .type = NLA_U32 },
96518518
PM
191};
192
9638f33e
PM
193static 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
211err2:
212 nf_ct_l3proto_module_put(NFPROTO_IPV4);
213err1:
214 return err;
215}
216
217static 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
fe92ca45
PM
226static int nft_ct_get_init(const struct nft_ctx *ctx,
227 const struct nft_expr *expr,
228 const struct nlattr * const tb[])
96518518
PM
229{
230 struct nft_ct *priv = nft_expr_priv(expr);
fe92ca45 231 int err;
96518518 232
fe92ca45 233 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
96518518
PM
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:
d2bf2f34
FW
243#endif
244#ifdef CONFIG_NF_CONNTRACK_LABELS
245 case NFT_CT_LABELS:
96518518
PM
246#endif
247 case NFT_CT_EXPIRATION:
248 case NFT_CT_HELPER:
249 if (tb[NFTA_CT_DIRECTION] != NULL)
250 return -EINVAL;
251 break;
51292c07 252 case NFT_CT_L3PROTOCOL:
96518518
PM
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
fe92ca45
PM
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
c4ede3d3
KE
289 return 0;
290}
291
fe92ca45
PM
292static int nft_ct_set_init(const struct nft_ctx *ctx,
293 const struct nft_expr *expr,
294 const struct nlattr * const tb[])
c4ede3d3 295{
fe92ca45
PM
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) {
e88e514e 301#ifdef CONFIG_NF_CONNTRACK_MARK
c4ede3d3
KE
302 case NFT_CT_MARK:
303 break;
e88e514e 304#endif
c4ede3d3
KE
305 default:
306 return -EOPNOTSUPP;
307 }
308
fe92ca45
PM
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;
c4ede3d3 313
9638f33e 314 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
96518518
PM
315 if (err < 0)
316 return err;
96518518 317
96518518 318 return 0;
96518518
PM
319}
320
62472bce
PM
321static void nft_ct_destroy(const struct nft_ctx *ctx,
322 const struct nft_expr *expr)
96518518 323{
d46f2cd2 324 nft_ct_l3proto_module_put(ctx->afi->family);
96518518
PM
325}
326
c4ede3d3 327static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
96518518
PM
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;
2a53bfb3
AB
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
96518518
PM
348 return 0;
349
350nla_put_failure:
351 return -1;
352}
353
c4ede3d3
KE
354static 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
364nla_put_failure:
365 return -1;
366}
367
ef1f7df9 368static struct nft_expr_type nft_ct_type;
c4ede3d3 369static const struct nft_expr_ops nft_ct_get_ops = {
ef1f7df9 370 .type = &nft_ct_type,
96518518 371 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
c4ede3d3 372 .eval = nft_ct_get_eval,
fe92ca45 373 .init = nft_ct_get_init,
96518518 374 .destroy = nft_ct_destroy,
c4ede3d3 375 .dump = nft_ct_get_dump,
ef1f7df9
PM
376};
377
c4ede3d3
KE
378static 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,
fe92ca45 382 .init = nft_ct_set_init,
c4ede3d3
KE
383 .destroy = nft_ct_destroy,
384 .dump = nft_ct_set_dump,
385};
386
387static const struct nft_expr_ops *
388nft_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
ef1f7df9
PM
406static struct nft_expr_type nft_ct_type __read_mostly = {
407 .name = "ct",
c4ede3d3 408 .select_ops = &nft_ct_select_ops,
96518518
PM
409 .policy = nft_ct_policy,
410 .maxattr = NFTA_CT_MAX,
ef1f7df9 411 .owner = THIS_MODULE,
96518518
PM
412};
413
414static int __init nft_ct_module_init(void)
415{
ef1f7df9 416 return nft_register_expr(&nft_ct_type);
96518518
PM
417}
418
419static void __exit nft_ct_module_exit(void)
420{
ef1f7df9 421 nft_unregister_expr(&nft_ct_type);
96518518
PM
422}
423
424module_init(nft_ct_module_init);
425module_exit(nft_ct_module_exit);
426
427MODULE_LICENSE("GPL");
428MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
429MODULE_ALIAS_NFT_EXPR("ct");
This page took 0.103707 seconds and 5 git commands to generate.