]>
Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
ef1f7df9 | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
25443261 | 3 | * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org> |
96518518 PM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/netlink.h> | |
16 | #include <linux/netfilter.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
18 | #include <net/netfilter/nf_tables.h> | |
19 | #include <net/netfilter/nf_conntrack.h> | |
48f66c90 | 20 | #include <net/netfilter/nf_conntrack_acct.h> |
96518518 PM |
21 | #include <net/netfilter/nf_conntrack_tuple.h> |
22 | #include <net/netfilter/nf_conntrack_helper.h> | |
c4ede3d3 | 23 | #include <net/netfilter/nf_conntrack_ecache.h> |
d2bf2f34 | 24 | #include <net/netfilter/nf_conntrack_labels.h> |
96518518 PM |
25 | |
26 | struct nft_ct { | |
27 | enum nft_ct_keys key:8; | |
28 | enum ip_conntrack_dir dir:8; | |
d46f2cd2 | 29 | union { |
c4ede3d3 KE |
30 | enum nft_registers dreg:8; |
31 | enum nft_registers sreg:8; | |
32 | }; | |
96518518 PM |
33 | }; |
34 | ||
1a64edf5 FW |
35 | struct nft_ct_helper_obj { |
36 | struct nf_conntrack_helper *helper4; | |
37 | struct nf_conntrack_helper *helper6; | |
38 | u8 l4proto; | |
39 | }; | |
40 | ||
edee4f1e FW |
41 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
42 | static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template); | |
43 | static unsigned int nft_ct_pcpu_template_refcnt __read_mostly; | |
44 | #endif | |
45 | ||
48f66c90 FW |
46 | static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c, |
47 | enum nft_ct_keys k, | |
48 | enum ip_conntrack_dir d) | |
49 | { | |
50 | if (d < IP_CT_DIR_MAX) | |
51 | return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) : | |
52 | atomic64_read(&c[d].packets); | |
53 | ||
54 | return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) + | |
55 | nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY); | |
56 | } | |
57 | ||
c4ede3d3 | 58 | static void nft_ct_get_eval(const struct nft_expr *expr, |
a55e22e9 | 59 | struct nft_regs *regs, |
c4ede3d3 | 60 | const struct nft_pktinfo *pkt) |
96518518 PM |
61 | { |
62 | const struct nft_ct *priv = nft_expr_priv(expr); | |
49499c3e | 63 | u32 *dest = ®s->data[priv->dreg]; |
96518518 PM |
64 | enum ip_conntrack_info ctinfo; |
65 | const struct nf_conn *ct; | |
66 | const struct nf_conn_help *help; | |
67 | const struct nf_conntrack_tuple *tuple; | |
68 | const struct nf_conntrack_helper *helper; | |
96518518 PM |
69 | unsigned int state; |
70 | ||
71 | ct = nf_ct_get(pkt->skb, &ctinfo); | |
72 | ||
73 | switch (priv->key) { | |
74 | case NFT_CT_STATE: | |
cc41c84b FW |
75 | if (ct) |
76 | state = NF_CT_STATE_BIT(ctinfo); | |
77 | else if (ctinfo == IP_CT_UNTRACKED) | |
96518518 PM |
78 | state = NF_CT_STATE_UNTRACKED_BIT; |
79 | else | |
cc41c84b | 80 | state = NF_CT_STATE_INVALID_BIT; |
fad136ea | 81 | *dest = state; |
96518518 | 82 | return; |
c1f86676 DM |
83 | default: |
84 | break; | |
96518518 PM |
85 | } |
86 | ||
87 | if (ct == NULL) | |
88 | goto err; | |
89 | ||
90 | switch (priv->key) { | |
91 | case NFT_CT_DIRECTION: | |
10596608 | 92 | nft_reg_store8(dest, CTINFO2DIR(ctinfo)); |
96518518 PM |
93 | return; |
94 | case NFT_CT_STATUS: | |
fad136ea | 95 | *dest = ct->status; |
96518518 PM |
96 | return; |
97 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
98 | case NFT_CT_MARK: | |
fad136ea | 99 | *dest = ct->mark; |
96518518 PM |
100 | return; |
101 | #endif | |
102 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
103 | case NFT_CT_SECMARK: | |
fad136ea | 104 | *dest = ct->secmark; |
96518518 PM |
105 | return; |
106 | #endif | |
107 | case NFT_CT_EXPIRATION: | |
c8607e02 | 108 | *dest = jiffies_to_msecs(nf_ct_expires(ct)); |
96518518 PM |
109 | return; |
110 | case NFT_CT_HELPER: | |
111 | if (ct->master == NULL) | |
112 | goto err; | |
113 | help = nfct_help(ct->master); | |
114 | if (help == NULL) | |
115 | goto err; | |
116 | helper = rcu_dereference(help->helper); | |
117 | if (helper == NULL) | |
118 | goto err; | |
fad136ea | 119 | strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN); |
96518518 | 120 | return; |
d2bf2f34 FW |
121 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
122 | case NFT_CT_LABELS: { | |
123 | struct nf_conn_labels *labels = nf_ct_labels_find(ct); | |
d2bf2f34 | 124 | |
23014011 FW |
125 | if (labels) |
126 | memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE); | |
127 | else | |
fad136ea | 128 | memset(dest, 0, NF_CT_LABELS_MAX_SIZE); |
d2bf2f34 FW |
129 | return; |
130 | } | |
efaea94a | 131 | #endif |
48f66c90 FW |
132 | case NFT_CT_BYTES: /* fallthrough */ |
133 | case NFT_CT_PKTS: { | |
134 | const struct nf_conn_acct *acct = nf_conn_acct_find(ct); | |
135 | u64 count = 0; | |
136 | ||
137 | if (acct) | |
138 | count = nft_ct_get_eval_counter(acct->counter, | |
139 | priv->key, priv->dir); | |
140 | memcpy(dest, &count, sizeof(count)); | |
141 | return; | |
142 | } | |
949a3584 LZ |
143 | case NFT_CT_AVGPKT: { |
144 | const struct nf_conn_acct *acct = nf_conn_acct_find(ct); | |
145 | u64 avgcnt = 0, bcnt = 0, pcnt = 0; | |
146 | ||
147 | if (acct) { | |
148 | pcnt = nft_ct_get_eval_counter(acct->counter, | |
149 | NFT_CT_PKTS, priv->dir); | |
150 | bcnt = nft_ct_get_eval_counter(acct->counter, | |
151 | NFT_CT_BYTES, priv->dir); | |
152 | if (pcnt != 0) | |
153 | avgcnt = div64_u64(bcnt, pcnt); | |
154 | } | |
155 | ||
156 | memcpy(dest, &avgcnt, sizeof(avgcnt)); | |
157 | return; | |
158 | } | |
d767ff2c | 159 | case NFT_CT_L3PROTOCOL: |
10596608 | 160 | nft_reg_store8(dest, nf_ct_l3num(ct)); |
d767ff2c LZ |
161 | return; |
162 | case NFT_CT_PROTOCOL: | |
10596608 | 163 | nft_reg_store8(dest, nf_ct_protonum(ct)); |
d767ff2c | 164 | return; |
ab23821f FW |
165 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
166 | case NFT_CT_ZONE: { | |
167 | const struct nf_conntrack_zone *zone = nf_ct_zone(ct); | |
10596608 | 168 | u16 zoneid; |
ab23821f FW |
169 | |
170 | if (priv->dir < IP_CT_DIR_MAX) | |
10596608 | 171 | zoneid = nf_ct_zone_id(zone, priv->dir); |
ab23821f | 172 | else |
10596608 | 173 | zoneid = zone->id; |
ab23821f | 174 | |
10596608 | 175 | nft_reg_store16(dest, zoneid); |
ab23821f FW |
176 | return; |
177 | } | |
178 | #endif | |
c1f86676 DM |
179 | default: |
180 | break; | |
96518518 PM |
181 | } |
182 | ||
183 | tuple = &ct->tuplehash[priv->dir].tuple; | |
184 | switch (priv->key) { | |
96518518 | 185 | case NFT_CT_SRC: |
fad136ea | 186 | memcpy(dest, tuple->src.u3.all, |
96518518 PM |
187 | nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16); |
188 | return; | |
189 | case NFT_CT_DST: | |
fad136ea | 190 | memcpy(dest, tuple->dst.u3.all, |
96518518 PM |
191 | nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16); |
192 | return; | |
96518518 | 193 | case NFT_CT_PROTO_SRC: |
10596608 | 194 | nft_reg_store16(dest, (__force u16)tuple->src.u.all); |
96518518 PM |
195 | return; |
196 | case NFT_CT_PROTO_DST: | |
10596608 | 197 | nft_reg_store16(dest, (__force u16)tuple->dst.u.all); |
96518518 | 198 | return; |
c1f86676 DM |
199 | default: |
200 | break; | |
96518518 PM |
201 | } |
202 | return; | |
203 | err: | |
a55e22e9 | 204 | regs->verdict.code = NFT_BREAK; |
96518518 PM |
205 | } |
206 | ||
edee4f1e FW |
207 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
208 | static void nft_ct_set_zone_eval(const struct nft_expr *expr, | |
209 | struct nft_regs *regs, | |
210 | const struct nft_pktinfo *pkt) | |
211 | { | |
212 | struct nf_conntrack_zone zone = { .dir = NF_CT_DEFAULT_ZONE_DIR }; | |
213 | const struct nft_ct *priv = nft_expr_priv(expr); | |
214 | struct sk_buff *skb = pkt->skb; | |
215 | enum ip_conntrack_info ctinfo; | |
10596608 | 216 | u16 value = nft_reg_load16(®s->data[priv->sreg]); |
edee4f1e FW |
217 | struct nf_conn *ct; |
218 | ||
219 | ct = nf_ct_get(skb, &ctinfo); | |
220 | if (ct) /* already tracked */ | |
221 | return; | |
222 | ||
223 | zone.id = value; | |
224 | ||
225 | switch (priv->dir) { | |
226 | case IP_CT_DIR_ORIGINAL: | |
227 | zone.dir = NF_CT_ZONE_DIR_ORIG; | |
228 | break; | |
229 | case IP_CT_DIR_REPLY: | |
230 | zone.dir = NF_CT_ZONE_DIR_REPL; | |
231 | break; | |
232 | default: | |
233 | break; | |
234 | } | |
235 | ||
236 | ct = this_cpu_read(nft_ct_pcpu_template); | |
237 | ||
238 | if (likely(atomic_read(&ct->ct_general.use) == 1)) { | |
239 | nf_ct_zone_add(ct, &zone); | |
240 | } else { | |
241 | /* previous skb got queued to userspace */ | |
242 | ct = nf_ct_tmpl_alloc(nft_net(pkt), &zone, GFP_ATOMIC); | |
243 | if (!ct) { | |
244 | regs->verdict.code = NF_DROP; | |
245 | return; | |
246 | } | |
247 | } | |
248 | ||
249 | atomic_inc(&ct->ct_general.use); | |
250 | nf_ct_set(skb, ct, IP_CT_NEW); | |
251 | } | |
252 | #endif | |
253 | ||
c4ede3d3 | 254 | static void nft_ct_set_eval(const struct nft_expr *expr, |
a55e22e9 | 255 | struct nft_regs *regs, |
c4ede3d3 KE |
256 | const struct nft_pktinfo *pkt) |
257 | { | |
258 | const struct nft_ct *priv = nft_expr_priv(expr); | |
259 | struct sk_buff *skb = pkt->skb; | |
847c8e29 | 260 | #ifdef CONFIG_NF_CONNTRACK_MARK |
49499c3e | 261 | u32 value = regs->data[priv->sreg]; |
847c8e29 | 262 | #endif |
c4ede3d3 KE |
263 | enum ip_conntrack_info ctinfo; |
264 | struct nf_conn *ct; | |
265 | ||
266 | ct = nf_ct_get(skb, &ctinfo); | |
694a0055 | 267 | if (ct == NULL || nf_ct_is_template(ct)) |
c4ede3d3 KE |
268 | return; |
269 | ||
270 | switch (priv->key) { | |
271 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
272 | case NFT_CT_MARK: | |
273 | if (ct->mark != value) { | |
274 | ct->mark = value; | |
275 | nf_conntrack_event_cache(IPCT_MARK, ct); | |
276 | } | |
277 | break; | |
1ad8f48d FW |
278 | #endif |
279 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
280 | case NFT_CT_LABELS: | |
281 | nf_connlabels_replace(ct, | |
282 | ®s->data[priv->sreg], | |
283 | ®s->data[priv->sreg], | |
284 | NF_CT_LABELS_MAX_SIZE / sizeof(u32)); | |
285 | break; | |
694a0055 FW |
286 | #endif |
287 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
288 | case NFT_CT_EVENTMASK: { | |
289 | struct nf_conntrack_ecache *e = nf_ct_ecache_find(ct); | |
290 | u32 ctmask = regs->data[priv->sreg]; | |
291 | ||
292 | if (e) { | |
293 | if (e->ctmask != ctmask) | |
294 | e->ctmask = ctmask; | |
295 | break; | |
296 | } | |
297 | ||
298 | if (ctmask && !nf_ct_is_confirmed(ct)) | |
299 | nf_ct_ecache_ext_add(ct, ctmask, 0, GFP_ATOMIC); | |
300 | break; | |
301 | } | |
c4ede3d3 | 302 | #endif |
c1f86676 DM |
303 | default: |
304 | break; | |
c4ede3d3 KE |
305 | } |
306 | } | |
307 | ||
96518518 PM |
308 | static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = { |
309 | [NFTA_CT_DREG] = { .type = NLA_U32 }, | |
310 | [NFTA_CT_KEY] = { .type = NLA_U32 }, | |
311 | [NFTA_CT_DIRECTION] = { .type = NLA_U8 }, | |
c4ede3d3 | 312 | [NFTA_CT_SREG] = { .type = NLA_U32 }, |
96518518 PM |
313 | }; |
314 | ||
edee4f1e FW |
315 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
316 | static void nft_ct_tmpl_put_pcpu(void) | |
317 | { | |
318 | struct nf_conn *ct; | |
319 | int cpu; | |
320 | ||
321 | for_each_possible_cpu(cpu) { | |
322 | ct = per_cpu(nft_ct_pcpu_template, cpu); | |
323 | if (!ct) | |
324 | break; | |
325 | nf_ct_put(ct); | |
326 | per_cpu(nft_ct_pcpu_template, cpu) = NULL; | |
327 | } | |
328 | } | |
329 | ||
330 | static bool nft_ct_tmpl_alloc_pcpu(void) | |
331 | { | |
332 | struct nf_conntrack_zone zone = { .id = 0 }; | |
333 | struct nf_conn *tmp; | |
334 | int cpu; | |
335 | ||
336 | if (nft_ct_pcpu_template_refcnt) | |
337 | return true; | |
338 | ||
339 | for_each_possible_cpu(cpu) { | |
340 | tmp = nf_ct_tmpl_alloc(&init_net, &zone, GFP_KERNEL); | |
341 | if (!tmp) { | |
342 | nft_ct_tmpl_put_pcpu(); | |
343 | return false; | |
344 | } | |
345 | ||
346 | atomic_set(&tmp->ct_general.use, 1); | |
347 | per_cpu(nft_ct_pcpu_template, cpu) = tmp; | |
348 | } | |
349 | ||
350 | return true; | |
351 | } | |
352 | #endif | |
353 | ||
fe92ca45 PM |
354 | static int nft_ct_get_init(const struct nft_ctx *ctx, |
355 | const struct nft_expr *expr, | |
356 | const struct nlattr * const tb[]) | |
96518518 PM |
357 | { |
358 | struct nft_ct *priv = nft_expr_priv(expr); | |
45d9bcda | 359 | unsigned int len; |
fe92ca45 | 360 | int err; |
96518518 | 361 | |
fe92ca45 | 362 | priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY])); |
ab23821f | 363 | priv->dir = IP_CT_DIR_MAX; |
96518518 | 364 | switch (priv->key) { |
96518518 | 365 | case NFT_CT_DIRECTION: |
45d9bcda PM |
366 | if (tb[NFTA_CT_DIRECTION] != NULL) |
367 | return -EINVAL; | |
368 | len = sizeof(u8); | |
369 | break; | |
370 | case NFT_CT_STATE: | |
96518518 PM |
371 | case NFT_CT_STATUS: |
372 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
373 | case NFT_CT_MARK: | |
374 | #endif | |
375 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
376 | case NFT_CT_SECMARK: | |
d2bf2f34 | 377 | #endif |
45d9bcda PM |
378 | case NFT_CT_EXPIRATION: |
379 | if (tb[NFTA_CT_DIRECTION] != NULL) | |
380 | return -EINVAL; | |
381 | len = sizeof(u32); | |
382 | break; | |
d2bf2f34 FW |
383 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
384 | case NFT_CT_LABELS: | |
45d9bcda PM |
385 | if (tb[NFTA_CT_DIRECTION] != NULL) |
386 | return -EINVAL; | |
387 | len = NF_CT_LABELS_MAX_SIZE; | |
388 | break; | |
96518518 | 389 | #endif |
96518518 PM |
390 | case NFT_CT_HELPER: |
391 | if (tb[NFTA_CT_DIRECTION] != NULL) | |
392 | return -EINVAL; | |
45d9bcda | 393 | len = NF_CT_HELPER_NAME_LEN; |
96518518 | 394 | break; |
45d9bcda | 395 | |
51292c07 | 396 | case NFT_CT_L3PROTOCOL: |
96518518 | 397 | case NFT_CT_PROTOCOL: |
d767ff2c LZ |
398 | /* For compatibility, do not report error if NFTA_CT_DIRECTION |
399 | * attribute is specified. | |
400 | */ | |
45d9bcda PM |
401 | len = sizeof(u8); |
402 | break; | |
96518518 PM |
403 | case NFT_CT_SRC: |
404 | case NFT_CT_DST: | |
45d9bcda PM |
405 | if (tb[NFTA_CT_DIRECTION] == NULL) |
406 | return -EINVAL; | |
407 | ||
408 | switch (ctx->afi->family) { | |
409 | case NFPROTO_IPV4: | |
410 | len = FIELD_SIZEOF(struct nf_conntrack_tuple, | |
411 | src.u3.ip); | |
412 | break; | |
413 | case NFPROTO_IPV6: | |
414 | case NFPROTO_INET: | |
415 | len = FIELD_SIZEOF(struct nf_conntrack_tuple, | |
416 | src.u3.ip6); | |
417 | break; | |
418 | default: | |
419 | return -EAFNOSUPPORT; | |
420 | } | |
421 | break; | |
96518518 PM |
422 | case NFT_CT_PROTO_SRC: |
423 | case NFT_CT_PROTO_DST: | |
424 | if (tb[NFTA_CT_DIRECTION] == NULL) | |
425 | return -EINVAL; | |
45d9bcda | 426 | len = FIELD_SIZEOF(struct nf_conntrack_tuple, src.u.all); |
96518518 | 427 | break; |
48f66c90 FW |
428 | case NFT_CT_BYTES: |
429 | case NFT_CT_PKTS: | |
949a3584 | 430 | case NFT_CT_AVGPKT: |
48f66c90 FW |
431 | len = sizeof(u64); |
432 | break; | |
ab23821f FW |
433 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
434 | case NFT_CT_ZONE: | |
435 | len = sizeof(u16); | |
436 | break; | |
437 | #endif | |
96518518 PM |
438 | default: |
439 | return -EOPNOTSUPP; | |
440 | } | |
441 | ||
fe92ca45 PM |
442 | if (tb[NFTA_CT_DIRECTION] != NULL) { |
443 | priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]); | |
444 | switch (priv->dir) { | |
445 | case IP_CT_DIR_ORIGINAL: | |
446 | case IP_CT_DIR_REPLY: | |
447 | break; | |
448 | default: | |
449 | return -EINVAL; | |
450 | } | |
451 | } | |
452 | ||
b1c96ed3 | 453 | priv->dreg = nft_parse_register(tb[NFTA_CT_DREG]); |
1ec10212 PM |
454 | err = nft_validate_register_store(ctx, priv->dreg, NULL, |
455 | NFT_DATA_VALUE, len); | |
fe92ca45 PM |
456 | if (err < 0) |
457 | return err; | |
458 | ||
7e35ec0e | 459 | err = nf_ct_netns_get(ctx->net, ctx->afi->family); |
fe92ca45 PM |
460 | if (err < 0) |
461 | return err; | |
462 | ||
949a3584 LZ |
463 | if (priv->key == NFT_CT_BYTES || |
464 | priv->key == NFT_CT_PKTS || | |
465 | priv->key == NFT_CT_AVGPKT) | |
3f8b61b7 LZ |
466 | nf_ct_set_acct(ctx->net, true); |
467 | ||
c4ede3d3 KE |
468 | return 0; |
469 | } | |
470 | ||
5c178d81 FW |
471 | static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv) |
472 | { | |
473 | switch (priv->key) { | |
474 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
475 | case NFT_CT_LABELS: | |
476 | nf_connlabels_put(ctx->net); | |
477 | break; | |
edee4f1e FW |
478 | #endif |
479 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
480 | case NFT_CT_ZONE: | |
481 | if (--nft_ct_pcpu_template_refcnt == 0) | |
482 | nft_ct_tmpl_put_pcpu(); | |
5c178d81 FW |
483 | #endif |
484 | default: | |
485 | break; | |
486 | } | |
487 | } | |
488 | ||
fe92ca45 PM |
489 | static int nft_ct_set_init(const struct nft_ctx *ctx, |
490 | const struct nft_expr *expr, | |
491 | const struct nlattr * const tb[]) | |
c4ede3d3 | 492 | { |
fe92ca45 | 493 | struct nft_ct *priv = nft_expr_priv(expr); |
d07db988 | 494 | unsigned int len; |
fe92ca45 PM |
495 | int err; |
496 | ||
edee4f1e | 497 | priv->dir = IP_CT_DIR_MAX; |
fe92ca45 PM |
498 | priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY])); |
499 | switch (priv->key) { | |
e88e514e | 500 | #ifdef CONFIG_NF_CONNTRACK_MARK |
c4ede3d3 | 501 | case NFT_CT_MARK: |
7bfdde70 LZ |
502 | if (tb[NFTA_CT_DIRECTION]) |
503 | return -EINVAL; | |
d07db988 | 504 | len = FIELD_SIZEOF(struct nf_conn, mark); |
c4ede3d3 | 505 | break; |
1ad8f48d FW |
506 | #endif |
507 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
508 | case NFT_CT_LABELS: | |
509 | if (tb[NFTA_CT_DIRECTION]) | |
510 | return -EINVAL; | |
511 | len = NF_CT_LABELS_MAX_SIZE; | |
512 | err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1); | |
513 | if (err) | |
514 | return err; | |
515 | break; | |
edee4f1e FW |
516 | #endif |
517 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
518 | case NFT_CT_ZONE: | |
519 | if (!nft_ct_tmpl_alloc_pcpu()) | |
520 | return -ENOMEM; | |
521 | nft_ct_pcpu_template_refcnt++; | |
427345d6 | 522 | len = sizeof(u16); |
edee4f1e | 523 | break; |
694a0055 FW |
524 | #endif |
525 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
526 | case NFT_CT_EVENTMASK: | |
527 | if (tb[NFTA_CT_DIRECTION]) | |
528 | return -EINVAL; | |
529 | len = sizeof(u32); | |
530 | break; | |
e88e514e | 531 | #endif |
c4ede3d3 KE |
532 | default: |
533 | return -EOPNOTSUPP; | |
534 | } | |
535 | ||
edee4f1e FW |
536 | if (tb[NFTA_CT_DIRECTION]) { |
537 | priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]); | |
538 | switch (priv->dir) { | |
539 | case IP_CT_DIR_ORIGINAL: | |
540 | case IP_CT_DIR_REPLY: | |
541 | break; | |
542 | default: | |
4494dbc6 LZ |
543 | err = -EINVAL; |
544 | goto err1; | |
edee4f1e FW |
545 | } |
546 | } | |
547 | ||
b1c96ed3 | 548 | priv->sreg = nft_parse_register(tb[NFTA_CT_SREG]); |
d07db988 | 549 | err = nft_validate_register_load(priv->sreg, len); |
fe92ca45 | 550 | if (err < 0) |
590025a2 | 551 | goto err1; |
c4ede3d3 | 552 | |
7e35ec0e | 553 | err = nf_ct_netns_get(ctx->net, ctx->afi->family); |
96518518 | 554 | if (err < 0) |
590025a2 | 555 | goto err1; |
96518518 | 556 | |
96518518 | 557 | return 0; |
590025a2 LZ |
558 | |
559 | err1: | |
5c178d81 | 560 | __nft_ct_set_destroy(ctx, priv); |
590025a2 LZ |
561 | return err; |
562 | } | |
563 | ||
564 | static void nft_ct_get_destroy(const struct nft_ctx *ctx, | |
565 | const struct nft_expr *expr) | |
566 | { | |
ecb2421b | 567 | nf_ct_netns_put(ctx->net, ctx->afi->family); |
96518518 PM |
568 | } |
569 | ||
590025a2 LZ |
570 | static void nft_ct_set_destroy(const struct nft_ctx *ctx, |
571 | const struct nft_expr *expr) | |
96518518 | 572 | { |
1ad8f48d FW |
573 | struct nft_ct *priv = nft_expr_priv(expr); |
574 | ||
5c178d81 | 575 | __nft_ct_set_destroy(ctx, priv); |
7e35ec0e | 576 | nf_ct_netns_put(ctx->net, ctx->afi->family); |
96518518 PM |
577 | } |
578 | ||
c4ede3d3 | 579 | static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr) |
96518518 PM |
580 | { |
581 | const struct nft_ct *priv = nft_expr_priv(expr); | |
582 | ||
b1c96ed3 | 583 | if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg)) |
96518518 PM |
584 | goto nla_put_failure; |
585 | if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key))) | |
586 | goto nla_put_failure; | |
2a53bfb3 AB |
587 | |
588 | switch (priv->key) { | |
2a53bfb3 AB |
589 | case NFT_CT_SRC: |
590 | case NFT_CT_DST: | |
591 | case NFT_CT_PROTO_SRC: | |
592 | case NFT_CT_PROTO_DST: | |
593 | if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir)) | |
594 | goto nla_put_failure; | |
48f66c90 FW |
595 | break; |
596 | case NFT_CT_BYTES: | |
597 | case NFT_CT_PKTS: | |
949a3584 | 598 | case NFT_CT_AVGPKT: |
ab23821f | 599 | case NFT_CT_ZONE: |
48f66c90 FW |
600 | if (priv->dir < IP_CT_DIR_MAX && |
601 | nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir)) | |
602 | goto nla_put_failure; | |
603 | break; | |
2a53bfb3 AB |
604 | default: |
605 | break; | |
606 | } | |
607 | ||
96518518 PM |
608 | return 0; |
609 | ||
610 | nla_put_failure: | |
611 | return -1; | |
612 | } | |
613 | ||
c4ede3d3 KE |
614 | static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr) |
615 | { | |
616 | const struct nft_ct *priv = nft_expr_priv(expr); | |
617 | ||
b1c96ed3 | 618 | if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg)) |
c4ede3d3 KE |
619 | goto nla_put_failure; |
620 | if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key))) | |
621 | goto nla_put_failure; | |
edee4f1e FW |
622 | |
623 | switch (priv->key) { | |
624 | case NFT_CT_ZONE: | |
625 | if (priv->dir < IP_CT_DIR_MAX && | |
626 | nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir)) | |
627 | goto nla_put_failure; | |
628 | break; | |
629 | default: | |
630 | break; | |
631 | } | |
632 | ||
c4ede3d3 KE |
633 | return 0; |
634 | ||
635 | nla_put_failure: | |
636 | return -1; | |
637 | } | |
638 | ||
ef1f7df9 | 639 | static struct nft_expr_type nft_ct_type; |
c4ede3d3 | 640 | static const struct nft_expr_ops nft_ct_get_ops = { |
ef1f7df9 | 641 | .type = &nft_ct_type, |
96518518 | 642 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)), |
c4ede3d3 | 643 | .eval = nft_ct_get_eval, |
fe92ca45 | 644 | .init = nft_ct_get_init, |
590025a2 | 645 | .destroy = nft_ct_get_destroy, |
c4ede3d3 | 646 | .dump = nft_ct_get_dump, |
ef1f7df9 PM |
647 | }; |
648 | ||
c4ede3d3 KE |
649 | static const struct nft_expr_ops nft_ct_set_ops = { |
650 | .type = &nft_ct_type, | |
651 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)), | |
652 | .eval = nft_ct_set_eval, | |
fe92ca45 | 653 | .init = nft_ct_set_init, |
590025a2 | 654 | .destroy = nft_ct_set_destroy, |
c4ede3d3 KE |
655 | .dump = nft_ct_set_dump, |
656 | }; | |
657 | ||
edee4f1e FW |
658 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
659 | static const struct nft_expr_ops nft_ct_set_zone_ops = { | |
660 | .type = &nft_ct_type, | |
661 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)), | |
662 | .eval = nft_ct_set_zone_eval, | |
663 | .init = nft_ct_set_init, | |
664 | .destroy = nft_ct_set_destroy, | |
665 | .dump = nft_ct_set_dump, | |
666 | }; | |
667 | #endif | |
668 | ||
c4ede3d3 KE |
669 | static const struct nft_expr_ops * |
670 | nft_ct_select_ops(const struct nft_ctx *ctx, | |
671 | const struct nlattr * const tb[]) | |
672 | { | |
673 | if (tb[NFTA_CT_KEY] == NULL) | |
674 | return ERR_PTR(-EINVAL); | |
675 | ||
676 | if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG]) | |
677 | return ERR_PTR(-EINVAL); | |
678 | ||
679 | if (tb[NFTA_CT_DREG]) | |
680 | return &nft_ct_get_ops; | |
681 | ||
edee4f1e FW |
682 | if (tb[NFTA_CT_SREG]) { |
683 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
684 | if (nla_get_be32(tb[NFTA_CT_KEY]) == htonl(NFT_CT_ZONE)) | |
685 | return &nft_ct_set_zone_ops; | |
686 | #endif | |
c4ede3d3 | 687 | return &nft_ct_set_ops; |
edee4f1e | 688 | } |
c4ede3d3 KE |
689 | |
690 | return ERR_PTR(-EINVAL); | |
691 | } | |
692 | ||
ef1f7df9 PM |
693 | static struct nft_expr_type nft_ct_type __read_mostly = { |
694 | .name = "ct", | |
d4ef3835 | 695 | .select_ops = nft_ct_select_ops, |
96518518 PM |
696 | .policy = nft_ct_policy, |
697 | .maxattr = NFTA_CT_MAX, | |
ef1f7df9 | 698 | .owner = THIS_MODULE, |
96518518 PM |
699 | }; |
700 | ||
25443261 PNA |
701 | static void nft_notrack_eval(const struct nft_expr *expr, |
702 | struct nft_regs *regs, | |
703 | const struct nft_pktinfo *pkt) | |
704 | { | |
705 | struct sk_buff *skb = pkt->skb; | |
706 | enum ip_conntrack_info ctinfo; | |
707 | struct nf_conn *ct; | |
708 | ||
709 | ct = nf_ct_get(pkt->skb, &ctinfo); | |
710 | /* Previously seen (loopback or untracked)? Ignore. */ | |
cc41c84b | 711 | if (ct || ctinfo == IP_CT_UNTRACKED) |
25443261 PNA |
712 | return; |
713 | ||
cc41c84b | 714 | nf_ct_set(skb, ct, IP_CT_UNTRACKED); |
25443261 PNA |
715 | } |
716 | ||
717 | static struct nft_expr_type nft_notrack_type; | |
718 | static const struct nft_expr_ops nft_notrack_ops = { | |
719 | .type = &nft_notrack_type, | |
720 | .size = NFT_EXPR_SIZE(0), | |
721 | .eval = nft_notrack_eval, | |
722 | }; | |
723 | ||
724 | static struct nft_expr_type nft_notrack_type __read_mostly = { | |
725 | .name = "notrack", | |
726 | .ops = &nft_notrack_ops, | |
727 | .owner = THIS_MODULE, | |
728 | }; | |
729 | ||
1a64edf5 FW |
730 | static int nft_ct_helper_obj_init(const struct nft_ctx *ctx, |
731 | const struct nlattr * const tb[], | |
732 | struct nft_object *obj) | |
733 | { | |
734 | struct nft_ct_helper_obj *priv = nft_obj_data(obj); | |
735 | struct nf_conntrack_helper *help4, *help6; | |
736 | char name[NF_CT_HELPER_NAME_LEN]; | |
737 | int family = ctx->afi->family; | |
738 | ||
739 | if (!tb[NFTA_CT_HELPER_NAME] || !tb[NFTA_CT_HELPER_L4PROTO]) | |
740 | return -EINVAL; | |
741 | ||
742 | priv->l4proto = nla_get_u8(tb[NFTA_CT_HELPER_L4PROTO]); | |
743 | if (!priv->l4proto) | |
744 | return -ENOENT; | |
745 | ||
746 | nla_strlcpy(name, tb[NFTA_CT_HELPER_NAME], sizeof(name)); | |
747 | ||
748 | if (tb[NFTA_CT_HELPER_L3PROTO]) | |
749 | family = ntohs(nla_get_be16(tb[NFTA_CT_HELPER_L3PROTO])); | |
750 | ||
751 | help4 = NULL; | |
752 | help6 = NULL; | |
753 | ||
754 | switch (family) { | |
755 | case NFPROTO_IPV4: | |
756 | if (ctx->afi->family == NFPROTO_IPV6) | |
757 | return -EINVAL; | |
758 | ||
759 | help4 = nf_conntrack_helper_try_module_get(name, family, | |
760 | priv->l4proto); | |
761 | break; | |
762 | case NFPROTO_IPV6: | |
763 | if (ctx->afi->family == NFPROTO_IPV4) | |
764 | return -EINVAL; | |
765 | ||
766 | help6 = nf_conntrack_helper_try_module_get(name, family, | |
767 | priv->l4proto); | |
768 | break; | |
769 | case NFPROTO_NETDEV: /* fallthrough */ | |
770 | case NFPROTO_BRIDGE: /* same */ | |
771 | case NFPROTO_INET: | |
772 | help4 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV4, | |
773 | priv->l4proto); | |
774 | help6 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV6, | |
775 | priv->l4proto); | |
776 | break; | |
777 | default: | |
778 | return -EAFNOSUPPORT; | |
779 | } | |
780 | ||
781 | /* && is intentional; only error if INET found neither ipv4 or ipv6 */ | |
782 | if (!help4 && !help6) | |
783 | return -ENOENT; | |
784 | ||
785 | priv->helper4 = help4; | |
786 | priv->helper6 = help6; | |
787 | ||
788 | return 0; | |
789 | } | |
790 | ||
791 | static void nft_ct_helper_obj_destroy(struct nft_object *obj) | |
792 | { | |
793 | struct nft_ct_helper_obj *priv = nft_obj_data(obj); | |
794 | ||
795 | if (priv->helper4) | |
d91fc59c | 796 | nf_conntrack_helper_put(priv->helper4); |
1a64edf5 | 797 | if (priv->helper6) |
d91fc59c | 798 | nf_conntrack_helper_put(priv->helper6); |
1a64edf5 FW |
799 | } |
800 | ||
801 | static void nft_ct_helper_obj_eval(struct nft_object *obj, | |
802 | struct nft_regs *regs, | |
803 | const struct nft_pktinfo *pkt) | |
804 | { | |
805 | const struct nft_ct_helper_obj *priv = nft_obj_data(obj); | |
806 | struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb); | |
807 | struct nf_conntrack_helper *to_assign = NULL; | |
808 | struct nf_conn_help *help; | |
809 | ||
810 | if (!ct || | |
811 | nf_ct_is_confirmed(ct) || | |
812 | nf_ct_is_template(ct) || | |
813 | priv->l4proto != nf_ct_protonum(ct)) | |
814 | return; | |
815 | ||
816 | switch (nf_ct_l3num(ct)) { | |
817 | case NFPROTO_IPV4: | |
818 | to_assign = priv->helper4; | |
819 | break; | |
820 | case NFPROTO_IPV6: | |
821 | to_assign = priv->helper6; | |
822 | break; | |
823 | default: | |
824 | WARN_ON_ONCE(1); | |
825 | return; | |
826 | } | |
827 | ||
828 | if (!to_assign) | |
829 | return; | |
830 | ||
831 | if (test_bit(IPS_HELPER_BIT, &ct->status)) | |
832 | return; | |
833 | ||
834 | help = nf_ct_helper_ext_add(ct, to_assign, GFP_ATOMIC); | |
835 | if (help) { | |
836 | rcu_assign_pointer(help->helper, to_assign); | |
837 | set_bit(IPS_HELPER_BIT, &ct->status); | |
838 | } | |
839 | } | |
840 | ||
841 | static int nft_ct_helper_obj_dump(struct sk_buff *skb, | |
842 | struct nft_object *obj, bool reset) | |
843 | { | |
844 | const struct nft_ct_helper_obj *priv = nft_obj_data(obj); | |
e414b939 | 845 | const struct nf_conntrack_helper *helper; |
1a64edf5 FW |
846 | u16 family; |
847 | ||
e414b939 TY |
848 | if (priv->helper4 && priv->helper6) { |
849 | family = NFPROTO_INET; | |
850 | helper = priv->helper4; | |
851 | } else if (priv->helper6) { | |
852 | family = NFPROTO_IPV6; | |
853 | helper = priv->helper6; | |
854 | } else { | |
855 | family = NFPROTO_IPV4; | |
856 | helper = priv->helper4; | |
857 | } | |
858 | ||
1a64edf5 FW |
859 | if (nla_put_string(skb, NFTA_CT_HELPER_NAME, helper->name)) |
860 | return -1; | |
861 | ||
862 | if (nla_put_u8(skb, NFTA_CT_HELPER_L4PROTO, priv->l4proto)) | |
863 | return -1; | |
864 | ||
1a64edf5 FW |
865 | if (nla_put_be16(skb, NFTA_CT_HELPER_L3PROTO, htons(family))) |
866 | return -1; | |
867 | ||
868 | return 0; | |
869 | } | |
870 | ||
871 | static const struct nla_policy nft_ct_helper_policy[NFTA_CT_HELPER_MAX + 1] = { | |
872 | [NFTA_CT_HELPER_NAME] = { .type = NLA_STRING, | |
873 | .len = NF_CT_HELPER_NAME_LEN - 1 }, | |
874 | [NFTA_CT_HELPER_L3PROTO] = { .type = NLA_U16 }, | |
875 | [NFTA_CT_HELPER_L4PROTO] = { .type = NLA_U8 }, | |
876 | }; | |
877 | ||
dfc46034 PBG |
878 | static struct nft_object_type nft_ct_helper_obj_type; |
879 | static const struct nft_object_ops nft_ct_helper_obj_ops = { | |
880 | .type = &nft_ct_helper_obj_type, | |
1a64edf5 | 881 | .size = sizeof(struct nft_ct_helper_obj), |
1a64edf5 FW |
882 | .eval = nft_ct_helper_obj_eval, |
883 | .init = nft_ct_helper_obj_init, | |
884 | .destroy = nft_ct_helper_obj_destroy, | |
885 | .dump = nft_ct_helper_obj_dump, | |
dfc46034 PBG |
886 | }; |
887 | ||
888 | static struct nft_object_type nft_ct_helper_obj_type __read_mostly = { | |
889 | .type = NFT_OBJECT_CT_HELPER, | |
890 | .ops = &nft_ct_helper_obj_ops, | |
891 | .maxattr = NFTA_CT_HELPER_MAX, | |
892 | .policy = nft_ct_helper_policy, | |
1a64edf5 FW |
893 | .owner = THIS_MODULE, |
894 | }; | |
895 | ||
96518518 PM |
896 | static int __init nft_ct_module_init(void) |
897 | { | |
25443261 PNA |
898 | int err; |
899 | ||
adff6c65 FW |
900 | BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE); |
901 | ||
25443261 PNA |
902 | err = nft_register_expr(&nft_ct_type); |
903 | if (err < 0) | |
904 | return err; | |
905 | ||
906 | err = nft_register_expr(&nft_notrack_type); | |
907 | if (err < 0) | |
908 | goto err1; | |
909 | ||
dfc46034 | 910 | err = nft_register_obj(&nft_ct_helper_obj_type); |
1a64edf5 FW |
911 | if (err < 0) |
912 | goto err2; | |
913 | ||
25443261 | 914 | return 0; |
1a64edf5 FW |
915 | |
916 | err2: | |
917 | nft_unregister_expr(&nft_notrack_type); | |
25443261 PNA |
918 | err1: |
919 | nft_unregister_expr(&nft_ct_type); | |
920 | return err; | |
96518518 PM |
921 | } |
922 | ||
923 | static void __exit nft_ct_module_exit(void) | |
924 | { | |
dfc46034 | 925 | nft_unregister_obj(&nft_ct_helper_obj_type); |
25443261 | 926 | nft_unregister_expr(&nft_notrack_type); |
ef1f7df9 | 927 | nft_unregister_expr(&nft_ct_type); |
96518518 PM |
928 | } |
929 | ||
930 | module_init(nft_ct_module_init); | |
931 | module_exit(nft_ct_module_exit); | |
932 | ||
933 | MODULE_LICENSE("GPL"); | |
934 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
935 | MODULE_ALIAS_NFT_EXPR("ct"); | |
25443261 | 936 | MODULE_ALIAS_NFT_EXPR("notrack"); |
1a64edf5 | 937 | MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_HELPER); |