1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
5 *
6 * Development of this code funded by Astaro AG (http://www.astaro.com/)
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables_core.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_acct.h>
18 #include <net/netfilter/nf_conntrack_tuple.h>
19 #include <net/netfilter/nf_conntrack_helper.h>
20 #include <net/netfilter/nf_conntrack_ecache.h>
21 #include <net/netfilter/nf_conntrack_labels.h>
22 #include <net/netfilter/nf_conntrack_timeout.h>
23 #include <net/netfilter/nf_conntrack_l4proto.h>
24 #include <net/netfilter/nf_conntrack_expect.h>
25 #include <net/netfilter/nf_conntrack_seqadj.h>
26
27 struct nft_ct_helper_obj {
28 struct nf_conntrack_helper *helper4;
29 struct nf_conntrack_helper *helper6;
30 u8 l4proto;
31 };
32
33 #ifdef CONFIG_NF_CONNTRACK_ZONES
34 static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template);
35 static unsigned int nft_ct_pcpu_template_refcnt __read_mostly;
36 static DEFINE_MUTEX(nft_ct_pcpu_mutex);
37 #endif
38
nft_ct_get_eval_counter(const struct nf_conn_counter * c,enum nft_ct_keys k,enum ip_conntrack_dir d)39 static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
40 enum nft_ct_keys k,
41 enum ip_conntrack_dir d)
42 {
43 if (d < IP_CT_DIR_MAX)
44 return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) :
45 atomic64_read(&c[d].packets);
46
47 return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) +
48 nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY);
49 }
50
nft_ct_get_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)51 static void nft_ct_get_eval(const struct nft_expr *expr,
52 struct nft_regs *regs,
53 const struct nft_pktinfo *pkt)
54 {
55 const struct nft_ct *priv = nft_expr_priv(expr);
56 u32 *dest = ®s->data[priv->dreg];
57 enum ip_conntrack_info ctinfo;
58 const struct nf_conn *ct;
59 const struct nf_conn_help *help;
60 const struct nf_conntrack_tuple *tuple;
61 const struct nf_conntrack_helper *helper;
62 unsigned int state;
63
64 ct = nf_ct_get(pkt->skb, &ctinfo);
65
66 switch (priv->key) {
67 case NFT_CT_STATE:
68 if (ct)
69 state = NF_CT_STATE_BIT(ctinfo);
70 else if (ctinfo == IP_CT_UNTRACKED)
71 state = NF_CT_STATE_UNTRACKED_BIT;
72 else
73 state = NF_CT_STATE_INVALID_BIT;
74 *dest = state;
75 return;
76 default:
77 break;
78 }
79
80 if (ct == NULL)
81 goto err;
82
83 switch (priv->key) {
84 case NFT_CT_DIRECTION:
85 nft_reg_store8(dest, CTINFO2DIR(ctinfo));
86 return;
87 case NFT_CT_STATUS:
88 *dest = ct->status;
89 return;
90 #ifdef CONFIG_NF_CONNTRACK_MARK
91 case NFT_CT_MARK:
92 *dest = READ_ONCE(ct->mark);
93 return;
94 #endif
95 #ifdef CONFIG_NF_CONNTRACK_SECMARK
96 case NFT_CT_SECMARK:
97 *dest = ct->secmark;
98 return;
99 #endif
100 case NFT_CT_EXPIRATION:
101 *dest = jiffies_to_msecs(nf_ct_expires(ct));
102 return;
103 case NFT_CT_HELPER:
104 if (ct->master == NULL)
105 goto err;
106 help = nfct_help(ct->master);
107 if (help == NULL)
108 goto err;
109 helper = rcu_dereference(help->helper);
110 if (helper == NULL)
111 goto err;
112 strscpy_pad((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
113 return;
114 #ifdef CONFIG_NF_CONNTRACK_LABELS
115 case NFT_CT_LABELS: {
116 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
117
118 if (labels)
119 memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE);
120 else
121 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
122 return;
123 }
124 #endif
125 case NFT_CT_BYTES:
126 case NFT_CT_PKTS: {
127 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
128 u64 count = 0;
129
130 if (acct)
131 count = nft_ct_get_eval_counter(acct->counter,
132 priv->key, priv->dir);
133 memcpy(dest, &count, sizeof(count));
134 return;
135 }
136 case NFT_CT_AVGPKT: {
137 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
138 u64 avgcnt = 0, bcnt = 0, pcnt = 0;
139
140 if (acct) {
141 pcnt = nft_ct_get_eval_counter(acct->counter,
142 NFT_CT_PKTS, priv->dir);
143 bcnt = nft_ct_get_eval_counter(acct->counter,
144 NFT_CT_BYTES, priv->dir);
145 if (pcnt != 0)
146 avgcnt = div64_u64(bcnt, pcnt);
147 }
148
149 memcpy(dest, &avgcnt, sizeof(avgcnt));
150 return;
151 }
152 case NFT_CT_L3PROTOCOL:
153 nft_reg_store8(dest, nf_ct_l3num(ct));
154 return;
155 case NFT_CT_PROTOCOL:
156 nft_reg_store8(dest, nf_ct_protonum(ct));
157 return;
158 #ifdef CONFIG_NF_CONNTRACK_ZONES
159 case NFT_CT_ZONE: {
160 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
161 u16 zoneid;
162
163 if (priv->dir < IP_CT_DIR_MAX)
164 zoneid = nf_ct_zone_id(zone, priv->dir);
165 else
166 zoneid = zone->id;
167
168 nft_reg_store16(dest, zoneid);
169 return;
170 }
171 #endif
172 case NFT_CT_ID:
173 *dest = nf_ct_get_id(ct);
174 return;
175 default:
176 break;
177 }
178
179 tuple = &ct->tuplehash[priv->dir].tuple;
180 switch (priv->key) {
181 case NFT_CT_SRC:
182 memcpy(dest, tuple->src.u3.all,
183 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
184 return;
185 case NFT_CT_DST:
186 memcpy(dest, tuple->dst.u3.all,
187 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
188 return;
189 case NFT_CT_PROTO_SRC:
190 nft_reg_store16(dest, (__force u16)tuple->src.u.all);
191 return;
192 case NFT_CT_PROTO_DST:
193 nft_reg_store16(dest, (__force u16)tuple->dst.u.all);
194 return;
195 case NFT_CT_SRC_IP:
196 if (nf_ct_l3num(ct) != NFPROTO_IPV4)
197 goto err;
198 *dest = (__force __u32)tuple->src.u3.ip;
199 return;
200 case NFT_CT_DST_IP:
201 if (nf_ct_l3num(ct) != NFPROTO_IPV4)
202 goto err;
203 *dest = (__force __u32)tuple->dst.u3.ip;
204 return;
205 case NFT_CT_SRC_IP6:
206 if (nf_ct_l3num(ct) != NFPROTO_IPV6)
207 goto err;
208 memcpy(dest, tuple->src.u3.ip6, sizeof(struct in6_addr));
209 return;
210 case NFT_CT_DST_IP6:
211 if (nf_ct_l3num(ct) != NFPROTO_IPV6)
212 goto err;
213 memcpy(dest, tuple->dst.u3.ip6, sizeof(struct in6_addr));
214 return;
215 default:
216 break;
217 }
218 return;
219 err:
220 regs->verdict.code = NFT_BREAK;
221 }
222
223 #ifdef CONFIG_NF_CONNTRACK_ZONES
nft_ct_set_zone_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)224 static void nft_ct_set_zone_eval(const struct nft_expr *expr,
225 struct nft_regs *regs,
226 const struct nft_pktinfo *pkt)
227 {
228 struct nf_conntrack_zone zone = { .dir = NF_CT_DEFAULT_ZONE_DIR };
229 const struct nft_ct *priv = nft_expr_priv(expr);
230 struct sk_buff *skb = pkt->skb;
231 enum ip_conntrack_info ctinfo;
232 u16 value = nft_reg_load16(®s->data[priv->sreg]);
233 struct nf_conn *ct;
234 int oldcnt;
235
236 ct = nf_ct_get(skb, &ctinfo);
237 if (ct) /* already tracked */
238 return;
239
240 zone.id = value;
241
242 switch (priv->dir) {
243 case IP_CT_DIR_ORIGINAL:
244 zone.dir = NF_CT_ZONE_DIR_ORIG;
245 break;
246 case IP_CT_DIR_REPLY:
247 zone.dir = NF_CT_ZONE_DIR_REPL;
248 break;
249 default:
250 break;
251 }
252
253 ct = this_cpu_read(nft_ct_pcpu_template);
254
255 __refcount_inc(&ct->ct_general.use, &oldcnt);
256 if (likely(oldcnt == 1)) {
257 nf_ct_zone_add(ct, &zone);
258 } else {
259 refcount_dec(&ct->ct_general.use);
260 /* previous skb got queued to userspace, allocate temporary
261 * one until percpu template can be reused.
262 */
263 ct = nf_ct_tmpl_alloc(nft_net(pkt), &zone, GFP_ATOMIC);
264 if (!ct) {
265 regs->verdict.code = NF_DROP;
266 return;
267 }
268 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
269 }
270
271 nf_ct_set(skb, ct, IP_CT_NEW);
272 }
273 #endif
274
nft_ct_set_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)275 static void nft_ct_set_eval(const struct nft_expr *expr,
276 struct nft_regs *regs,
277 const struct nft_pktinfo *pkt)
278 {
279 const struct nft_ct *priv = nft_expr_priv(expr);
280 struct sk_buff *skb = pkt->skb;
281 #if defined(CONFIG_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_SECMARK)
282 u32 value = regs->data[priv->sreg];
283 #endif
284 enum ip_conntrack_info ctinfo;
285 struct nf_conn *ct;
286
287 ct = nf_ct_get(skb, &ctinfo);
288 if (ct == NULL || nf_ct_is_template(ct))
289 return;
290
291 switch (priv->key) {
292 #ifdef CONFIG_NF_CONNTRACK_MARK
293 case NFT_CT_MARK:
294 if (READ_ONCE(ct->mark) != value) {
295 WRITE_ONCE(ct->mark, value);
296 nf_conntrack_event_cache(IPCT_MARK, ct);
297 }
298 break;
299 #endif
300 #ifdef CONFIG_NF_CONNTRACK_SECMARK
301 case NFT_CT_SECMARK:
302 if (ct->secmark != value) {
303 ct->secmark = value;
304 nf_conntrack_event_cache(IPCT_SECMARK, ct);
305 }
306 break;
307 #endif
308 #ifdef CONFIG_NF_CONNTRACK_LABELS
309 case NFT_CT_LABELS:
310 nf_connlabels_replace(ct,
311 ®s->data[priv->sreg],
312 ®s->data[priv->sreg],
313 NF_CT_LABELS_MAX_SIZE / sizeof(u32));
314 break;
315 #endif
316 #ifdef CONFIG_NF_CONNTRACK_EVENTS
317 case NFT_CT_EVENTMASK: {
318 struct nf_conntrack_ecache *e = nf_ct_ecache_find(ct);
319 u32 ctmask = regs->data[priv->sreg];
320
321 if (e) {
322 if (e->ctmask != ctmask)
323 e->ctmask = ctmask;
324 break;
325 }
326
327 if (ctmask && !nf_ct_is_confirmed(ct))
328 nf_ct_ecache_ext_add(ct, ctmask, 0, GFP_ATOMIC);
329 break;
330 }
331 #endif
332 default:
333 break;
334 }
335 }
336
337 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
338 [NFTA_CT_DREG] = { .type = NLA_U32 },
339 [NFTA_CT_KEY] = NLA_POLICY_MAX(NLA_BE32, 255),
340 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
341 [NFTA_CT_SREG] = { .type = NLA_U32 },
342 };
343
344 #ifdef CONFIG_NF_CONNTRACK_ZONES
nft_ct_tmpl_put_pcpu(void)345 static void nft_ct_tmpl_put_pcpu(void)
346 {
347 struct nf_conn *ct;
348 int cpu;
349
350 for_each_possible_cpu(cpu) {
351 ct = per_cpu(nft_ct_pcpu_template, cpu);
352 if (!ct)
353 break;
354 nf_ct_put(ct);
355 per_cpu(nft_ct_pcpu_template, cpu) = NULL;
356 }
357 }
358
nft_ct_tmpl_alloc_pcpu(void)359 static bool nft_ct_tmpl_alloc_pcpu(void)
360 {
361 struct nf_conntrack_zone zone = { .id = 0 };
362 struct nf_conn *tmp;
363 int cpu;
364
365 if (nft_ct_pcpu_template_refcnt)
366 return true;
367
368 for_each_possible_cpu(cpu) {
369 tmp = nf_ct_tmpl_alloc(&init_net, &zone, GFP_KERNEL);
370 if (!tmp) {
371 nft_ct_tmpl_put_pcpu();
372 return false;
373 }
374
375 __set_bit(IPS_CONFIRMED_BIT, &tmp->status);
376 per_cpu(nft_ct_pcpu_template, cpu) = tmp;
377 }
378
379 return true;
380 }
381 #endif
382
__nft_ct_get_destroy(const struct nft_ctx * ctx,struct nft_ct * priv)383 static void __nft_ct_get_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)
384 {
385 #ifdef CONFIG_NF_CONNTRACK_LABELS
386 if (priv->key == NFT_CT_LABELS)
387 nf_connlabels_put(ctx->net);
388 #endif
389 }
390
nft_ct_get_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])391 static int nft_ct_get_init(const struct nft_ctx *ctx,
392 const struct nft_expr *expr,
393 const struct nlattr * const tb[])
394 {
395 struct nft_ct *priv = nft_expr_priv(expr);
396 unsigned int len;
397 int err;
398
399 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
400 priv->dir = IP_CT_DIR_MAX;
401 switch (priv->key) {
402 case NFT_CT_DIRECTION:
403 if (tb[NFTA_CT_DIRECTION] != NULL)
404 return -EINVAL;
405 len = sizeof(u8);
406 break;
407 case NFT_CT_STATE:
408 case NFT_CT_STATUS:
409 #ifdef CONFIG_NF_CONNTRACK_MARK
410 case NFT_CT_MARK:
411 #endif
412 #ifdef CONFIG_NF_CONNTRACK_SECMARK
413 case NFT_CT_SECMARK:
414 #endif
415 case NFT_CT_EXPIRATION:
416 if (tb[NFTA_CT_DIRECTION] != NULL)
417 return -EINVAL;
418 len = sizeof(u32);
419 break;
420 #ifdef CONFIG_NF_CONNTRACK_LABELS
421 case NFT_CT_LABELS:
422 if (tb[NFTA_CT_DIRECTION] != NULL)
423 return -EINVAL;
424 len = NF_CT_LABELS_MAX_SIZE;
425
426 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
427 if (err)
428 return err;
429 break;
430 #endif
431 case NFT_CT_HELPER:
432 if (tb[NFTA_CT_DIRECTION] != NULL)
433 return -EINVAL;
434 len = NF_CT_HELPER_NAME_LEN;
435 break;
436
437 case NFT_CT_L3PROTOCOL:
438 case NFT_CT_PROTOCOL:
439 /* For compatibility, do not report error if NFTA_CT_DIRECTION
440 * attribute is specified.
441 */
442 len = sizeof(u8);
443 break;
444 case NFT_CT_SRC:
445 case NFT_CT_DST:
446 if (tb[NFTA_CT_DIRECTION] == NULL)
447 return -EINVAL;
448
449 switch (ctx->family) {
450 case NFPROTO_IPV4:
451 len = sizeof_field(struct nf_conntrack_tuple,
452 src.u3.ip);
453 break;
454 case NFPROTO_IPV6:
455 case NFPROTO_INET:
456 len = sizeof_field(struct nf_conntrack_tuple,
457 src.u3.ip6);
458 break;
459 default:
460 return -EAFNOSUPPORT;
461 }
462 break;
463 case NFT_CT_SRC_IP:
464 case NFT_CT_DST_IP:
465 if (tb[NFTA_CT_DIRECTION] == NULL)
466 return -EINVAL;
467
468 len = sizeof_field(struct nf_conntrack_tuple, src.u3.ip);
469 break;
470 case NFT_CT_SRC_IP6:
471 case NFT_CT_DST_IP6:
472 if (tb[NFTA_CT_DIRECTION] == NULL)
473 return -EINVAL;
474
475 len = sizeof_field(struct nf_conntrack_tuple, src.u3.ip6);
476 break;
477 case NFT_CT_PROTO_SRC:
478 case NFT_CT_PROTO_DST:
479 if (tb[NFTA_CT_DIRECTION] == NULL)
480 return -EINVAL;
481 len = sizeof_field(struct nf_conntrack_tuple, src.u.all);
482 break;
483 case NFT_CT_BYTES:
484 case NFT_CT_PKTS:
485 case NFT_CT_AVGPKT:
486 len = sizeof(u64);
487 break;
488 #ifdef CONFIG_NF_CONNTRACK_ZONES
489 case NFT_CT_ZONE:
490 len = sizeof(u16);
491 break;
492 #endif
493 case NFT_CT_ID:
494 if (tb[NFTA_CT_DIRECTION])
495 return -EINVAL;
496
497 len = sizeof(u32);
498 break;
499 default:
500 return -EOPNOTSUPP;
501 }
502
503 if (tb[NFTA_CT_DIRECTION] != NULL) {
504 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
505 switch (priv->dir) {
506 case IP_CT_DIR_ORIGINAL:
507 case IP_CT_DIR_REPLY:
508 break;
509 default:
510 err = -EINVAL;
511 goto err;
512 }
513 }
514
515 priv->len = len;
516 err = nft_parse_register_store(ctx, tb[NFTA_CT_DREG], &priv->dreg, NULL,
517 NFT_DATA_VALUE, len);
518 if (err < 0)
519 goto err;
520
521 err = nf_ct_netns_get(ctx->net, ctx->family);
522 if (err < 0)
523 goto err;
524
525 if (priv->key == NFT_CT_BYTES ||
526 priv->key == NFT_CT_PKTS ||
527 priv->key == NFT_CT_AVGPKT)
528 nf_ct_set_acct(ctx->net, true);
529
530 return 0;
531 err:
532 __nft_ct_get_destroy(ctx, priv);
533 return err;
534 }
535
__nft_ct_set_destroy(const struct nft_ctx * ctx,struct nft_ct * priv)536 static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)
537 {
538 switch (priv->key) {
539 #ifdef CONFIG_NF_CONNTRACK_LABELS
540 case NFT_CT_LABELS:
541 nf_connlabels_put(ctx->net);
542 break;
543 #endif
544 #ifdef CONFIG_NF_CONNTRACK_ZONES
545 case NFT_CT_ZONE:
546 mutex_lock(&nft_ct_pcpu_mutex);
547 if (--nft_ct_pcpu_template_refcnt == 0)
548 nft_ct_tmpl_put_pcpu();
549 mutex_unlock(&nft_ct_pcpu_mutex);
550 break;
551 #endif
552 default:
553 break;
554 }
555 }
556
nft_ct_set_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])557 static int nft_ct_set_init(const struct nft_ctx *ctx,
558 const struct nft_expr *expr,
559 const struct nlattr * const tb[])
560 {
561 struct nft_ct *priv = nft_expr_priv(expr);
562 unsigned int len;
563 int err;
564
565 priv->dir = IP_CT_DIR_MAX;
566 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
567 switch (priv->key) {
568 #ifdef CONFIG_NF_CONNTRACK_MARK
569 case NFT_CT_MARK:
570 if (tb[NFTA_CT_DIRECTION])
571 return -EINVAL;
572 len = sizeof_field(struct nf_conn, mark);
573 break;
574 #endif
575 #ifdef CONFIG_NF_CONNTRACK_LABELS
576 case NFT_CT_LABELS:
577 if (tb[NFTA_CT_DIRECTION])
578 return -EINVAL;
579 len = NF_CT_LABELS_MAX_SIZE;
580 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
581 if (err)
582 return err;
583 break;
584 #endif
585 #ifdef CONFIG_NF_CONNTRACK_ZONES
586 case NFT_CT_ZONE:
587 mutex_lock(&nft_ct_pcpu_mutex);
588 if (!nft_ct_tmpl_alloc_pcpu()) {
589 mutex_unlock(&nft_ct_pcpu_mutex);
590 return -ENOMEM;
591 }
592 nft_ct_pcpu_template_refcnt++;
593 mutex_unlock(&nft_ct_pcpu_mutex);
594 len = sizeof(u16);
595 break;
596 #endif
597 #ifdef CONFIG_NF_CONNTRACK_EVENTS
598 case NFT_CT_EVENTMASK:
599 if (tb[NFTA_CT_DIRECTION])
600 return -EINVAL;
601 len = sizeof(u32);
602 break;
603 #endif
604 #ifdef CONFIG_NF_CONNTRACK_SECMARK
605 case NFT_CT_SECMARK:
606 if (tb[NFTA_CT_DIRECTION])
607 return -EINVAL;
608 len = sizeof(u32);
609 break;
610 #endif
611 default:
612 return -EOPNOTSUPP;
613 }
614
615 if (tb[NFTA_CT_DIRECTION]) {
616 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
617 switch (priv->dir) {
618 case IP_CT_DIR_ORIGINAL:
619 case IP_CT_DIR_REPLY:
620 break;
621 default:
622 err = -EINVAL;
623 goto err1;
624 }
625 }
626
627 priv->len = len;
628 err = nft_parse_register_load(ctx, tb[NFTA_CT_SREG], &priv->sreg, len);
629 if (err < 0)
630 goto err1;
631
632 err = nf_ct_netns_get(ctx->net, ctx->family);
633 if (err < 0)
634 goto err1;
635
636 return 0;
637
638 err1:
639 __nft_ct_set_destroy(ctx, priv);
640 return err;
641 }
642
nft_ct_get_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)643 static void nft_ct_get_destroy(const struct nft_ctx *ctx,
644 const struct nft_expr *expr)
645 {
646 struct nft_ct *priv = nft_expr_priv(expr);
647
648 __nft_ct_get_destroy(ctx, priv);
649 nf_ct_netns_put(ctx->net, ctx->family);
650 }
651
nft_ct_set_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)652 static void nft_ct_set_destroy(const struct nft_ctx *ctx,
653 const struct nft_expr *expr)
654 {
655 struct nft_ct *priv = nft_expr_priv(expr);
656
657 __nft_ct_set_destroy(ctx, priv);
658 nf_ct_netns_put(ctx->net, ctx->family);
659 }
660
nft_ct_get_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)661 static int nft_ct_get_dump(struct sk_buff *skb,
662 const struct nft_expr *expr, bool reset)
663 {
664 const struct nft_ct *priv = nft_expr_priv(expr);
665
666 if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg))
667 goto nla_put_failure;
668 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
669 goto nla_put_failure;
670
671 switch (priv->key) {
672 case NFT_CT_SRC:
673 case NFT_CT_DST:
674 case NFT_CT_SRC_IP:
675 case NFT_CT_DST_IP:
676 case NFT_CT_SRC_IP6:
677 case NFT_CT_DST_IP6:
678 case NFT_CT_PROTO_SRC:
679 case NFT_CT_PROTO_DST:
680 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
681 goto nla_put_failure;
682 break;
683 case NFT_CT_BYTES:
684 case NFT_CT_PKTS:
685 case NFT_CT_AVGPKT:
686 case NFT_CT_ZONE:
687 if (priv->dir < IP_CT_DIR_MAX &&
688 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
689 goto nla_put_failure;
690 break;
691 default:
692 break;
693 }
694
695 return 0;
696
697 nla_put_failure:
698 return -1;
699 }
700
nft_ct_get_reduce(struct nft_regs_track * track,const struct nft_expr * expr)701 static bool nft_ct_get_reduce(struct nft_regs_track *track,
702 const struct nft_expr *expr)
703 {
704 const struct nft_ct *priv = nft_expr_priv(expr);
705 const struct nft_ct *ct;
706
707 if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
708 nft_reg_track_update(track, expr, priv->dreg, priv->len);
709 return false;
710 }
711
712 ct = nft_expr_priv(track->regs[priv->dreg].selector);
713 if (priv->key != ct->key) {
714 nft_reg_track_update(track, expr, priv->dreg, priv->len);
715 return false;
716 }
717
718 if (!track->regs[priv->dreg].bitwise)
719 return true;
720
721 return nft_expr_reduce_bitwise(track, expr);
722 }
723
nft_ct_set_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)724 static int nft_ct_set_dump(struct sk_buff *skb,
725 const struct nft_expr *expr, bool reset)
726 {
727 const struct nft_ct *priv = nft_expr_priv(expr);
728
729 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
730 goto nla_put_failure;
731 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
732 goto nla_put_failure;
733
734 switch (priv->key) {
735 case NFT_CT_ZONE:
736 if (priv->dir < IP_CT_DIR_MAX &&
737 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
738 goto nla_put_failure;
739 break;
740 default:
741 break;
742 }
743
744 return 0;
745
746 nla_put_failure:
747 return -1;
748 }
749
750 static struct nft_expr_type nft_ct_type;
751 static const struct nft_expr_ops nft_ct_get_ops = {
752 .type = &nft_ct_type,
753 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
754 .eval = nft_ct_get_eval,
755 .init = nft_ct_get_init,
756 .destroy = nft_ct_get_destroy,
757 .dump = nft_ct_get_dump,
758 .reduce = nft_ct_get_reduce,
759 };
760
nft_ct_set_reduce(struct nft_regs_track * track,const struct nft_expr * expr)761 static bool nft_ct_set_reduce(struct nft_regs_track *track,
762 const struct nft_expr *expr)
763 {
764 int i;
765
766 for (i = 0; i < NFT_REG32_NUM; i++) {
767 if (!track->regs[i].selector)
768 continue;
769
770 if (track->regs[i].selector->ops != &nft_ct_get_ops)
771 continue;
772
773 __nft_reg_track_cancel(track, i);
774 }
775
776 return false;
777 }
778
779 #ifdef CONFIG_MITIGATION_RETPOLINE
780 static const struct nft_expr_ops nft_ct_get_fast_ops = {
781 .type = &nft_ct_type,
782 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
783 .eval = nft_ct_get_fast_eval,
784 .init = nft_ct_get_init,
785 .destroy = nft_ct_get_destroy,
786 .dump = nft_ct_get_dump,
787 .reduce = nft_ct_set_reduce,
788 };
789 #endif
790
791 static const struct nft_expr_ops nft_ct_set_ops = {
792 .type = &nft_ct_type,
793 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
794 .eval = nft_ct_set_eval,
795 .init = nft_ct_set_init,
796 .destroy = nft_ct_set_destroy,
797 .dump = nft_ct_set_dump,
798 .reduce = nft_ct_set_reduce,
799 };
800
801 #ifdef CONFIG_NF_CONNTRACK_ZONES
802 static const struct nft_expr_ops nft_ct_set_zone_ops = {
803 .type = &nft_ct_type,
804 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
805 .eval = nft_ct_set_zone_eval,
806 .init = nft_ct_set_init,
807 .destroy = nft_ct_set_destroy,
808 .dump = nft_ct_set_dump,
809 .reduce = nft_ct_set_reduce,
810 };
811 #endif
812
813 static const struct nft_expr_ops *
nft_ct_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])814 nft_ct_select_ops(const struct nft_ctx *ctx,
815 const struct nlattr * const tb[])
816 {
817 if (tb[NFTA_CT_KEY] == NULL)
818 return ERR_PTR(-EINVAL);
819
820 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
821 return ERR_PTR(-EINVAL);
822
823 if (tb[NFTA_CT_DREG]) {
824 #ifdef CONFIG_MITIGATION_RETPOLINE
825 u32 k = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
826
827 switch (k) {
828 case NFT_CT_STATE:
829 case NFT_CT_DIRECTION:
830 case NFT_CT_STATUS:
831 case NFT_CT_MARK:
832 case NFT_CT_SECMARK:
833 return &nft_ct_get_fast_ops;
834 }
835 #endif
836 return &nft_ct_get_ops;
837 }
838
839 if (tb[NFTA_CT_SREG]) {
840 #ifdef CONFIG_NF_CONNTRACK_ZONES
841 if (nla_get_be32(tb[NFTA_CT_KEY]) == htonl(NFT_CT_ZONE))
842 return &nft_ct_set_zone_ops;
843 #endif
844 return &nft_ct_set_ops;
845 }
846
847 return ERR_PTR(-EINVAL);
848 }
849
850 static struct nft_expr_type nft_ct_type __read_mostly = {
851 .name = "ct",
852 .select_ops = nft_ct_select_ops,
853 .policy = nft_ct_policy,
854 .maxattr = NFTA_CT_MAX,
855 .owner = THIS_MODULE,
856 };
857
nft_notrack_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)858 static void nft_notrack_eval(const struct nft_expr *expr,
859 struct nft_regs *regs,
860 const struct nft_pktinfo *pkt)
861 {
862 struct sk_buff *skb = pkt->skb;
863 enum ip_conntrack_info ctinfo;
864 struct nf_conn *ct;
865
866 ct = nf_ct_get(pkt->skb, &ctinfo);
867 /* Previously seen (loopback or untracked)? Ignore. */
868 if (ct || ctinfo == IP_CT_UNTRACKED)
869 return;
870
871 nf_ct_set(skb, ct, IP_CT_UNTRACKED);
872 }
873
874 static struct nft_expr_type nft_notrack_type;
875 static const struct nft_expr_ops nft_notrack_ops = {
876 .type = &nft_notrack_type,
877 .size = NFT_EXPR_SIZE(0),
878 .eval = nft_notrack_eval,
879 .reduce = NFT_REDUCE_READONLY,
880 };
881
882 static struct nft_expr_type nft_notrack_type __read_mostly = {
883 .name = "notrack",
884 .ops = &nft_notrack_ops,
885 .owner = THIS_MODULE,
886 };
887
888 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
889 static int
nft_ct_timeout_parse_policy(void * timeouts,const struct nf_conntrack_l4proto * l4proto,struct net * net,const struct nlattr * attr)890 nft_ct_timeout_parse_policy(void *timeouts,
891 const struct nf_conntrack_l4proto *l4proto,
892 struct net *net, const struct nlattr *attr)
893 {
894 struct nlattr **tb;
895 int ret = 0;
896
897 tb = kcalloc(l4proto->ctnl_timeout.nlattr_max + 1, sizeof(*tb),
898 GFP_KERNEL);
899
900 if (!tb)
901 return -ENOMEM;
902
903 ret = nla_parse_nested_deprecated(tb,
904 l4proto->ctnl_timeout.nlattr_max,
905 attr,
906 l4proto->ctnl_timeout.nla_policy,
907 NULL);
908 if (ret < 0)
909 goto err;
910
911 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, timeouts);
912
913 err:
914 kfree(tb);
915 return ret;
916 }
917
918 struct nft_ct_timeout_obj {
919 struct nf_ct_timeout *timeout;
920 u8 l4proto;
921 };
922
nft_ct_timeout_obj_eval(struct nft_object * obj,struct nft_regs * regs,const struct nft_pktinfo * pkt)923 static void nft_ct_timeout_obj_eval(struct nft_object *obj,
924 struct nft_regs *regs,
925 const struct nft_pktinfo *pkt)
926 {
927 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
928 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
929 struct nf_conn_timeout *timeout;
930 const unsigned int *values;
931
932 if (priv->l4proto != pkt->tprot)
933 return;
934
935 if (!ct || nf_ct_is_template(ct) || nf_ct_is_confirmed(ct))
936 return;
937
938 timeout = nf_ct_timeout_find(ct);
939 if (!timeout) {
940 timeout = nf_ct_timeout_ext_add(ct, priv->timeout, GFP_ATOMIC);
941 if (!timeout) {
942 regs->verdict.code = NF_DROP;
943 return;
944 }
945 }
946
947 rcu_assign_pointer(timeout->timeout, priv->timeout);
948
949 /* adjust the timeout as per 'new' state. ct is unconfirmed,
950 * so the current timestamp must not be added.
951 */
952 values = nf_ct_timeout_data(timeout);
953 if (values)
954 nf_ct_refresh(ct, values[0]);
955 }
956
nft_ct_timeout_obj_init(const struct nft_ctx * ctx,const struct nlattr * const tb[],struct nft_object * obj)957 static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
958 const struct nlattr * const tb[],
959 struct nft_object *obj)
960 {
961 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
962 const struct nf_conntrack_l4proto *l4proto;
963 struct nf_ct_timeout *timeout;
964 int l3num = ctx->family;
965 __u8 l4num;
966 int ret;
967
968 if (!tb[NFTA_CT_TIMEOUT_L4PROTO] ||
969 !tb[NFTA_CT_TIMEOUT_DATA])
970 return -EINVAL;
971
972 if (tb[NFTA_CT_TIMEOUT_L3PROTO])
973 l3num = ntohs(nla_get_be16(tb[NFTA_CT_TIMEOUT_L3PROTO]));
974
975 l4num = nla_get_u8(tb[NFTA_CT_TIMEOUT_L4PROTO]);
976 priv->l4proto = l4num;
977
978 l4proto = nf_ct_l4proto_find(l4num);
979
980 if (l4proto->l4proto != l4num) {
981 ret = -EOPNOTSUPP;
982 goto err_proto_put;
983 }
984
985 timeout = kzalloc(sizeof(struct nf_ct_timeout) +
986 l4proto->ctnl_timeout.obj_size, GFP_KERNEL);
987 if (timeout == NULL) {
988 ret = -ENOMEM;
989 goto err_proto_put;
990 }
991
992 ret = nft_ct_timeout_parse_policy(&timeout->data, l4proto, ctx->net,
993 tb[NFTA_CT_TIMEOUT_DATA]);
994 if (ret < 0)
995 goto err_free_timeout;
996
997 timeout->l3num = l3num;
998 timeout->l4proto = l4proto;
999
1000 ret = nf_ct_netns_get(ctx->net, ctx->family);
1001 if (ret < 0)
1002 goto err_free_timeout;
1003
1004 priv->timeout = timeout;
1005 return 0;
1006
1007 err_free_timeout:
1008 kfree(timeout);
1009 err_proto_put:
1010 return ret;
1011 }
1012
nft_ct_timeout_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)1013 static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,
1014 struct nft_object *obj)
1015 {
1016 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
1017 struct nf_ct_timeout *timeout = priv->timeout;
1018
1019 nf_ct_untimeout(ctx->net, timeout);
1020 nf_ct_netns_put(ctx->net, ctx->family);
1021 kfree(priv->timeout);
1022 }
1023
nft_ct_timeout_obj_dump(struct sk_buff * skb,struct nft_object * obj,bool reset)1024 static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
1025 struct nft_object *obj, bool reset)
1026 {
1027 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
1028 const struct nf_ct_timeout *timeout = priv->timeout;
1029 struct nlattr *nest_params;
1030 int ret;
1031
1032 if (nla_put_u8(skb, NFTA_CT_TIMEOUT_L4PROTO, timeout->l4proto->l4proto) ||
1033 nla_put_be16(skb, NFTA_CT_TIMEOUT_L3PROTO, htons(timeout->l3num)))
1034 return -1;
1035
1036 nest_params = nla_nest_start(skb, NFTA_CT_TIMEOUT_DATA);
1037 if (!nest_params)
1038 return -1;
1039
1040 ret = timeout->l4proto->ctnl_timeout.obj_to_nlattr(skb, &timeout->data);
1041 if (ret < 0)
1042 return -1;
1043 nla_nest_end(skb, nest_params);
1044 return 0;
1045 }
1046
1047 static const struct nla_policy nft_ct_timeout_policy[NFTA_CT_TIMEOUT_MAX + 1] = {
1048 [NFTA_CT_TIMEOUT_L3PROTO] = {.type = NLA_U16 },
1049 [NFTA_CT_TIMEOUT_L4PROTO] = {.type = NLA_U8 },
1050 [NFTA_CT_TIMEOUT_DATA] = {.type = NLA_NESTED },
1051 };
1052
1053 static struct nft_object_type nft_ct_timeout_obj_type;
1054
1055 static const struct nft_object_ops nft_ct_timeout_obj_ops = {
1056 .type = &nft_ct_timeout_obj_type,
1057 .size = sizeof(struct nft_ct_timeout_obj),
1058 .eval = nft_ct_timeout_obj_eval,
1059 .init = nft_ct_timeout_obj_init,
1060 .destroy = nft_ct_timeout_obj_destroy,
1061 .dump = nft_ct_timeout_obj_dump,
1062 };
1063
1064 static struct nft_object_type nft_ct_timeout_obj_type __read_mostly = {
1065 .type = NFT_OBJECT_CT_TIMEOUT,
1066 .ops = &nft_ct_timeout_obj_ops,
1067 .maxattr = NFTA_CT_TIMEOUT_MAX,
1068 .policy = nft_ct_timeout_policy,
1069 .owner = THIS_MODULE,
1070 };
1071 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
1072
nft_ct_helper_obj_init(const struct nft_ctx * ctx,const struct nlattr * const tb[],struct nft_object * obj)1073 static int nft_ct_helper_obj_init(const struct nft_ctx *ctx,
1074 const struct nlattr * const tb[],
1075 struct nft_object *obj)
1076 {
1077 struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1078 struct nf_conntrack_helper *help4, *help6;
1079 char name[NF_CT_HELPER_NAME_LEN];
1080 int family = ctx->family;
1081 int err;
1082
1083 if (!tb[NFTA_CT_HELPER_NAME] || !tb[NFTA_CT_HELPER_L4PROTO])
1084 return -EINVAL;
1085
1086 priv->l4proto = nla_get_u8(tb[NFTA_CT_HELPER_L4PROTO]);
1087 if (!priv->l4proto)
1088 return -ENOENT;
1089
1090 nla_strscpy(name, tb[NFTA_CT_HELPER_NAME], sizeof(name));
1091
1092 if (tb[NFTA_CT_HELPER_L3PROTO])
1093 family = ntohs(nla_get_be16(tb[NFTA_CT_HELPER_L3PROTO]));
1094
1095 help4 = NULL;
1096 help6 = NULL;
1097
1098 switch (family) {
1099 case NFPROTO_IPV4:
1100 if (ctx->family == NFPROTO_IPV6)
1101 return -EINVAL;
1102
1103 help4 = nf_conntrack_helper_try_module_get(name, family,
1104 priv->l4proto);
1105 break;
1106 case NFPROTO_IPV6:
1107 if (ctx->family == NFPROTO_IPV4)
1108 return -EINVAL;
1109
1110 help6 = nf_conntrack_helper_try_module_get(name, family,
1111 priv->l4proto);
1112 break;
1113 case NFPROTO_NETDEV:
1114 case NFPROTO_BRIDGE:
1115 case NFPROTO_INET:
1116 help4 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV4,
1117 priv->l4proto);
1118 help6 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV6,
1119 priv->l4proto);
1120 break;
1121 default:
1122 return -EAFNOSUPPORT;
1123 }
1124
1125 /* && is intentional; only error if INET found neither ipv4 or ipv6 */
1126 if (!help4 && !help6)
1127 return -ENOENT;
1128
1129 priv->helper4 = help4;
1130 priv->helper6 = help6;
1131
1132 err = nf_ct_netns_get(ctx->net, ctx->family);
1133 if (err < 0)
1134 goto err_put_helper;
1135
1136 return 0;
1137
1138 err_put_helper:
1139 if (priv->helper4)
1140 nf_conntrack_helper_put(priv->helper4);
1141 if (priv->helper6)
1142 nf_conntrack_helper_put(priv->helper6);
1143 return err;
1144 }
1145
nft_ct_helper_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)1146 static void nft_ct_helper_obj_destroy(const struct nft_ctx *ctx,
1147 struct nft_object *obj)
1148 {
1149 struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1150
1151 if (priv->helper4)
1152 nf_conntrack_helper_put(priv->helper4);
1153 if (priv->helper6)
1154 nf_conntrack_helper_put(priv->helper6);
1155
1156 nf_ct_netns_put(ctx->net, ctx->family);
1157 }
1158
nft_ct_helper_obj_eval(struct nft_object * obj,struct nft_regs * regs,const struct nft_pktinfo * pkt)1159 static void nft_ct_helper_obj_eval(struct nft_object *obj,
1160 struct nft_regs *regs,
1161 const struct nft_pktinfo *pkt)
1162 {
1163 const struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1164 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
1165 struct nf_conntrack_helper *to_assign = NULL;
1166 struct nf_conn_help *help;
1167
1168 if (!ct ||
1169 nf_ct_is_confirmed(ct) ||
1170 nf_ct_is_template(ct) ||
1171 priv->l4proto != nf_ct_protonum(ct))
1172 return;
1173
1174 switch (nf_ct_l3num(ct)) {
1175 case NFPROTO_IPV4:
1176 to_assign = priv->helper4;
1177 break;
1178 case NFPROTO_IPV6:
1179 to_assign = priv->helper6;
1180 break;
1181 default:
1182 WARN_ON_ONCE(1);
1183 return;
1184 }
1185
1186 if (!to_assign)
1187 return;
1188
1189 if (test_bit(IPS_HELPER_BIT, &ct->status))
1190 return;
1191
1192 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1193 if (help) {
1194 rcu_assign_pointer(help->helper, to_assign);
1195 set_bit(IPS_HELPER_BIT, &ct->status);
1196
1197 if ((ct->status & IPS_NAT_MASK) && !nfct_seqadj(ct))
1198 if (!nfct_seqadj_ext_add(ct))
1199 regs->verdict.code = NF_DROP;
1200 }
1201 }
1202
nft_ct_helper_obj_dump(struct sk_buff * skb,struct nft_object * obj,bool reset)1203 static int nft_ct_helper_obj_dump(struct sk_buff *skb,
1204 struct nft_object *obj, bool reset)
1205 {
1206 const struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1207 const struct nf_conntrack_helper *helper;
1208 u16 family;
1209
1210 if (priv->helper4 && priv->helper6) {
1211 family = NFPROTO_INET;
1212 helper = priv->helper4;
1213 } else if (priv->helper6) {
1214 family = NFPROTO_IPV6;
1215 helper = priv->helper6;
1216 } else {
1217 family = NFPROTO_IPV4;
1218 helper = priv->helper4;
1219 }
1220
1221 if (nla_put_string(skb, NFTA_CT_HELPER_NAME, helper->name))
1222 return -1;
1223
1224 if (nla_put_u8(skb, NFTA_CT_HELPER_L4PROTO, priv->l4proto))
1225 return -1;
1226
1227 if (nla_put_be16(skb, NFTA_CT_HELPER_L3PROTO, htons(family)))
1228 return -1;
1229
1230 return 0;
1231 }
1232
1233 static const struct nla_policy nft_ct_helper_policy[NFTA_CT_HELPER_MAX + 1] = {
1234 [NFTA_CT_HELPER_NAME] = { .type = NLA_STRING,
1235 .len = NF_CT_HELPER_NAME_LEN - 1 },
1236 [NFTA_CT_HELPER_L3PROTO] = { .type = NLA_U16 },
1237 [NFTA_CT_HELPER_L4PROTO] = { .type = NLA_U8 },
1238 };
1239
1240 static struct nft_object_type nft_ct_helper_obj_type;
1241 static const struct nft_object_ops nft_ct_helper_obj_ops = {
1242 .type = &nft_ct_helper_obj_type,
1243 .size = sizeof(struct nft_ct_helper_obj),
1244 .eval = nft_ct_helper_obj_eval,
1245 .init = nft_ct_helper_obj_init,
1246 .destroy = nft_ct_helper_obj_destroy,
1247 .dump = nft_ct_helper_obj_dump,
1248 };
1249
1250 static struct nft_object_type nft_ct_helper_obj_type __read_mostly = {
1251 .type = NFT_OBJECT_CT_HELPER,
1252 .ops = &nft_ct_helper_obj_ops,
1253 .maxattr = NFTA_CT_HELPER_MAX,
1254 .policy = nft_ct_helper_policy,
1255 .owner = THIS_MODULE,
1256 };
1257
1258 struct nft_ct_expect_obj {
1259 u16 l3num;
1260 __be16 dport;
1261 u8 l4proto;
1262 u8 size;
1263 u32 timeout;
1264 };
1265
nft_ct_expect_obj_init(const struct nft_ctx * ctx,const struct nlattr * const tb[],struct nft_object * obj)1266 static int nft_ct_expect_obj_init(const struct nft_ctx *ctx,
1267 const struct nlattr * const tb[],
1268 struct nft_object *obj)
1269 {
1270 struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1271
1272 if (!tb[NFTA_CT_EXPECT_L4PROTO] ||
1273 !tb[NFTA_CT_EXPECT_DPORT] ||
1274 !tb[NFTA_CT_EXPECT_TIMEOUT] ||
1275 !tb[NFTA_CT_EXPECT_SIZE])
1276 return -EINVAL;
1277
1278 priv->l3num = ctx->family;
1279 if (tb[NFTA_CT_EXPECT_L3PROTO])
1280 priv->l3num = ntohs(nla_get_be16(tb[NFTA_CT_EXPECT_L3PROTO]));
1281
1282 switch (priv->l3num) {
1283 case NFPROTO_IPV4:
1284 case NFPROTO_IPV6:
1285 if (priv->l3num == ctx->family || ctx->family == NFPROTO_INET)
1286 break;
1287
1288 return -EINVAL;
1289 case NFPROTO_INET: /* tuple.src.l3num supports NFPROTO_IPV4/6 only */
1290 default:
1291 return -EAFNOSUPPORT;
1292 }
1293
1294 priv->l4proto = nla_get_u8(tb[NFTA_CT_EXPECT_L4PROTO]);
1295 switch (priv->l4proto) {
1296 case IPPROTO_TCP:
1297 case IPPROTO_UDP:
1298 case IPPROTO_UDPLITE:
1299 case IPPROTO_DCCP:
1300 case IPPROTO_SCTP:
1301 break;
1302 default:
1303 return -EOPNOTSUPP;
1304 }
1305
1306 priv->dport = nla_get_be16(tb[NFTA_CT_EXPECT_DPORT]);
1307 priv->timeout = nla_get_u32(tb[NFTA_CT_EXPECT_TIMEOUT]);
1308 priv->size = nla_get_u8(tb[NFTA_CT_EXPECT_SIZE]);
1309
1310 return nf_ct_netns_get(ctx->net, ctx->family);
1311 }
1312
nft_ct_expect_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)1313 static void nft_ct_expect_obj_destroy(const struct nft_ctx *ctx,
1314 struct nft_object *obj)
1315 {
1316 nf_ct_netns_put(ctx->net, ctx->family);
1317 }
1318
nft_ct_expect_obj_dump(struct sk_buff * skb,struct nft_object * obj,bool reset)1319 static int nft_ct_expect_obj_dump(struct sk_buff *skb,
1320 struct nft_object *obj, bool reset)
1321 {
1322 const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1323
1324 if (nla_put_be16(skb, NFTA_CT_EXPECT_L3PROTO, htons(priv->l3num)) ||
1325 nla_put_u8(skb, NFTA_CT_EXPECT_L4PROTO, priv->l4proto) ||
1326 nla_put_be16(skb, NFTA_CT_EXPECT_DPORT, priv->dport) ||
1327 nla_put_u32(skb, NFTA_CT_EXPECT_TIMEOUT, priv->timeout) ||
1328 nla_put_u8(skb, NFTA_CT_EXPECT_SIZE, priv->size))
1329 return -1;
1330
1331 return 0;
1332 }
1333
nft_ct_expect_obj_eval(struct nft_object * obj,struct nft_regs * regs,const struct nft_pktinfo * pkt)1334 static void nft_ct_expect_obj_eval(struct nft_object *obj,
1335 struct nft_regs *regs,
1336 const struct nft_pktinfo *pkt)
1337 {
1338 const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1339 struct nf_conntrack_expect *exp;
1340 enum ip_conntrack_info ctinfo;
1341 struct nf_conn_help *help;
1342 enum ip_conntrack_dir dir;
1343 u16 l3num = priv->l3num;
1344 struct nf_conn *ct;
1345
1346 ct = nf_ct_get(pkt->skb, &ctinfo);
1347 if (!ct || nf_ct_is_confirmed(ct) || nf_ct_is_template(ct)) {
1348 regs->verdict.code = NFT_BREAK;
1349 return;
1350 }
1351 dir = CTINFO2DIR(ctinfo);
1352
1353 help = nfct_help(ct);
1354 if (!help)
1355 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1356 if (!help) {
1357 regs->verdict.code = NF_DROP;
1358 return;
1359 }
1360
1361 if (help->expecting[NF_CT_EXPECT_CLASS_DEFAULT] >= priv->size) {
1362 regs->verdict.code = NFT_BREAK;
1363 return;
1364 }
1365 if (l3num == NFPROTO_INET)
1366 l3num = nf_ct_l3num(ct);
1367
1368 exp = nf_ct_expect_alloc(ct);
1369 if (exp == NULL) {
1370 regs->verdict.code = NF_DROP;
1371 return;
1372 }
1373 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, l3num,
1374 &ct->tuplehash[!dir].tuple.src.u3,
1375 &ct->tuplehash[!dir].tuple.dst.u3,
1376 priv->l4proto, NULL, &priv->dport);
1377 exp->timeout.expires = jiffies + priv->timeout * HZ;
1378
1379 if (nf_ct_expect_related(exp, 0) != 0)
1380 regs->verdict.code = NF_DROP;
1381 }
1382
1383 static const struct nla_policy nft_ct_expect_policy[NFTA_CT_EXPECT_MAX + 1] = {
1384 [NFTA_CT_EXPECT_L3PROTO] = { .type = NLA_U16 },
1385 [NFTA_CT_EXPECT_L4PROTO] = { .type = NLA_U8 },
1386 [NFTA_CT_EXPECT_DPORT] = { .type = NLA_U16 },
1387 [NFTA_CT_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1388 [NFTA_CT_EXPECT_SIZE] = { .type = NLA_U8 },
1389 };
1390
1391 static struct nft_object_type nft_ct_expect_obj_type;
1392
1393 static const struct nft_object_ops nft_ct_expect_obj_ops = {
1394 .type = &nft_ct_expect_obj_type,
1395 .size = sizeof(struct nft_ct_expect_obj),
1396 .eval = nft_ct_expect_obj_eval,
1397 .init = nft_ct_expect_obj_init,
1398 .destroy = nft_ct_expect_obj_destroy,
1399 .dump = nft_ct_expect_obj_dump,
1400 };
1401
1402 static struct nft_object_type nft_ct_expect_obj_type __read_mostly = {
1403 .type = NFT_OBJECT_CT_EXPECT,
1404 .ops = &nft_ct_expect_obj_ops,
1405 .maxattr = NFTA_CT_EXPECT_MAX,
1406 .policy = nft_ct_expect_policy,
1407 .owner = THIS_MODULE,
1408 };
1409
nft_ct_module_init(void)1410 static int __init nft_ct_module_init(void)
1411 {
1412 int err;
1413
1414 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE);
1415
1416 err = nft_register_expr(&nft_ct_type);
1417 if (err < 0)
1418 return err;
1419
1420 err = nft_register_expr(&nft_notrack_type);
1421 if (err < 0)
1422 goto err1;
1423
1424 err = nft_register_obj(&nft_ct_helper_obj_type);
1425 if (err < 0)
1426 goto err2;
1427
1428 err = nft_register_obj(&nft_ct_expect_obj_type);
1429 if (err < 0)
1430 goto err3;
1431 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1432 err = nft_register_obj(&nft_ct_timeout_obj_type);
1433 if (err < 0)
1434 goto err4;
1435 #endif
1436 return 0;
1437
1438 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1439 err4:
1440 nft_unregister_obj(&nft_ct_expect_obj_type);
1441 #endif
1442 err3:
1443 nft_unregister_obj(&nft_ct_helper_obj_type);
1444 err2:
1445 nft_unregister_expr(&nft_notrack_type);
1446 err1:
1447 nft_unregister_expr(&nft_ct_type);
1448 return err;
1449 }
1450
nft_ct_module_exit(void)1451 static void __exit nft_ct_module_exit(void)
1452 {
1453 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1454 nft_unregister_obj(&nft_ct_timeout_obj_type);
1455 #endif
1456 nft_unregister_obj(&nft_ct_expect_obj_type);
1457 nft_unregister_obj(&nft_ct_helper_obj_type);
1458 nft_unregister_expr(&nft_notrack_type);
1459 nft_unregister_expr(&nft_ct_type);
1460 }
1461
1462 module_init(nft_ct_module_init);
1463 module_exit(nft_ct_module_exit);
1464
1465 MODULE_LICENSE("GPL");
1466 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1467 MODULE_ALIAS_NFT_EXPR("ct");
1468 MODULE_ALIAS_NFT_EXPR("notrack");
1469 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_HELPER);
1470 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_TIMEOUT);
1471 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_EXPECT);
1472 MODULE_DESCRIPTION("Netfilter nf_tables conntrack module");
1473