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