1 /* Connection tracking via netlink socket. Allows for user space
2  * protocol helpers and general trouble making from userspace.
3  *
4  * (C) 2001 by Jay Schulist <jschlst@samba.org>
5  * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6  * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7  * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
8  *
9  * Initial connection tracking via netlink development funded and
10  * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11  *
12  * Further development of this code funded by Astaro AG (http://www.astaro.com)
13  *
14  * This software may be used and distributed according to the terms
15  * of the GNU General Public License, incorporated herein by reference.
16  */
17 
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32 #include <linux/siphash.h>
33 
34 #include <linux/netfilter.h>
35 #include <net/netlink.h>
36 #include <net/sock.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_seqadj.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_synproxy.h>
49 #if IS_ENABLED(CONFIG_NF_NAT)
50 #include <net/netfilter/nf_nat.h>
51 #include <net/netfilter/nf_nat_helper.h>
52 #endif
53 
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
56 
57 #include "nf_internals.h"
58 
59 MODULE_LICENSE("GPL");
60 
61 struct ctnetlink_list_dump_ctx {
62 	struct nf_conn *last;
63 	unsigned int cpu;
64 	bool done;
65 };
66 
ctnetlink_dump_tuples_proto(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple,const struct nf_conntrack_l4proto * l4proto)67 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
68 				const struct nf_conntrack_tuple *tuple,
69 				const struct nf_conntrack_l4proto *l4proto)
70 {
71 	int ret = 0;
72 	struct nlattr *nest_parms;
73 
74 	nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO);
75 	if (!nest_parms)
76 		goto nla_put_failure;
77 	if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
78 		goto nla_put_failure;
79 
80 	if (likely(l4proto->tuple_to_nlattr))
81 		ret = l4proto->tuple_to_nlattr(skb, tuple);
82 
83 	nla_nest_end(skb, nest_parms);
84 
85 	return ret;
86 
87 nla_put_failure:
88 	return -1;
89 }
90 
ipv4_tuple_to_nlattr(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple)91 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
92 				const struct nf_conntrack_tuple *tuple)
93 {
94 	if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
95 	    nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
96 		return -EMSGSIZE;
97 	return 0;
98 }
99 
ipv6_tuple_to_nlattr(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple)100 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
101 				const struct nf_conntrack_tuple *tuple)
102 {
103 	if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
104 	    nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
105 		return -EMSGSIZE;
106 	return 0;
107 }
108 
ctnetlink_dump_tuples_ip(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple)109 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
110 				    const struct nf_conntrack_tuple *tuple)
111 {
112 	int ret = 0;
113 	struct nlattr *nest_parms;
114 
115 	nest_parms = nla_nest_start(skb, CTA_TUPLE_IP);
116 	if (!nest_parms)
117 		goto nla_put_failure;
118 
119 	switch (tuple->src.l3num) {
120 	case NFPROTO_IPV4:
121 		ret = ipv4_tuple_to_nlattr(skb, tuple);
122 		break;
123 	case NFPROTO_IPV6:
124 		ret = ipv6_tuple_to_nlattr(skb, tuple);
125 		break;
126 	}
127 
128 	nla_nest_end(skb, nest_parms);
129 
130 	return ret;
131 
132 nla_put_failure:
133 	return -1;
134 }
135 
ctnetlink_dump_tuples(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple)136 static int ctnetlink_dump_tuples(struct sk_buff *skb,
137 				 const struct nf_conntrack_tuple *tuple)
138 {
139 	const struct nf_conntrack_l4proto *l4proto;
140 	int ret;
141 
142 	rcu_read_lock();
143 	ret = ctnetlink_dump_tuples_ip(skb, tuple);
144 
145 	if (ret >= 0) {
146 		l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
147 		ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
148 	}
149 	rcu_read_unlock();
150 	return ret;
151 }
152 
ctnetlink_dump_zone_id(struct sk_buff * skb,int attrtype,const struct nf_conntrack_zone * zone,int dir)153 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
154 				  const struct nf_conntrack_zone *zone, int dir)
155 {
156 	if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
157 		return 0;
158 	if (nla_put_be16(skb, attrtype, htons(zone->id)))
159 		goto nla_put_failure;
160 	return 0;
161 
162 nla_put_failure:
163 	return -1;
164 }
165 
ctnetlink_dump_status(struct sk_buff * skb,const struct nf_conn * ct)166 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
167 {
168 	if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
169 		goto nla_put_failure;
170 	return 0;
171 
172 nla_put_failure:
173 	return -1;
174 }
175 
ctnetlink_dump_timeout(struct sk_buff * skb,const struct nf_conn * ct,bool skip_zero)176 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct,
177 				  bool skip_zero)
178 {
179 	long timeout;
180 
181 	if (nf_ct_is_confirmed(ct))
182 		timeout = nf_ct_expires(ct) / HZ;
183 	else
184 		timeout = ct->timeout / HZ;
185 
186 	if (skip_zero && timeout == 0)
187 		return 0;
188 
189 	if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
190 		goto nla_put_failure;
191 	return 0;
192 
193 nla_put_failure:
194 	return -1;
195 }
196 
ctnetlink_dump_protoinfo(struct sk_buff * skb,struct nf_conn * ct,bool destroy)197 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct,
198 				    bool destroy)
199 {
200 	const struct nf_conntrack_l4proto *l4proto;
201 	struct nlattr *nest_proto;
202 	int ret;
203 
204 	l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
205 	if (!l4proto->to_nlattr)
206 		return 0;
207 
208 	nest_proto = nla_nest_start(skb, CTA_PROTOINFO);
209 	if (!nest_proto)
210 		goto nla_put_failure;
211 
212 	ret = l4proto->to_nlattr(skb, nest_proto, ct, destroy);
213 
214 	nla_nest_end(skb, nest_proto);
215 
216 	return ret;
217 
218 nla_put_failure:
219 	return -1;
220 }
221 
ctnetlink_dump_helpinfo(struct sk_buff * skb,const struct nf_conn * ct)222 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
223 				   const struct nf_conn *ct)
224 {
225 	struct nlattr *nest_helper;
226 	const struct nf_conn_help *help = nfct_help(ct);
227 	struct nf_conntrack_helper *helper;
228 
229 	if (!help)
230 		return 0;
231 
232 	rcu_read_lock();
233 	helper = rcu_dereference(help->helper);
234 	if (!helper)
235 		goto out;
236 
237 	nest_helper = nla_nest_start(skb, CTA_HELP);
238 	if (!nest_helper)
239 		goto nla_put_failure;
240 	if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
241 		goto nla_put_failure;
242 
243 	if (helper->to_nlattr)
244 		helper->to_nlattr(skb, ct);
245 
246 	nla_nest_end(skb, nest_helper);
247 out:
248 	rcu_read_unlock();
249 	return 0;
250 
251 nla_put_failure:
252 	rcu_read_unlock();
253 	return -1;
254 }
255 
256 static int
dump_counters(struct sk_buff * skb,struct nf_conn_acct * acct,enum ip_conntrack_dir dir,int type)257 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
258 	      enum ip_conntrack_dir dir, int type)
259 {
260 	enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
261 	struct nf_conn_counter *counter = acct->counter;
262 	struct nlattr *nest_count;
263 	u64 pkts, bytes;
264 
265 	if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
266 		pkts = atomic64_xchg(&counter[dir].packets, 0);
267 		bytes = atomic64_xchg(&counter[dir].bytes, 0);
268 	} else {
269 		pkts = atomic64_read(&counter[dir].packets);
270 		bytes = atomic64_read(&counter[dir].bytes);
271 	}
272 
273 	nest_count = nla_nest_start(skb, attr);
274 	if (!nest_count)
275 		goto nla_put_failure;
276 
277 	if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
278 			 CTA_COUNTERS_PAD) ||
279 	    nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
280 			 CTA_COUNTERS_PAD))
281 		goto nla_put_failure;
282 
283 	nla_nest_end(skb, nest_count);
284 
285 	return 0;
286 
287 nla_put_failure:
288 	return -1;
289 }
290 
291 static int
ctnetlink_dump_acct(struct sk_buff * skb,const struct nf_conn * ct,int type)292 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
293 {
294 	struct nf_conn_acct *acct = nf_conn_acct_find(ct);
295 
296 	if (!acct)
297 		return 0;
298 
299 	if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
300 		return -1;
301 	if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
302 		return -1;
303 
304 	return 0;
305 }
306 
307 static int
ctnetlink_dump_timestamp(struct sk_buff * skb,const struct nf_conn * ct)308 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
309 {
310 	struct nlattr *nest_count;
311 	const struct nf_conn_tstamp *tstamp;
312 
313 	tstamp = nf_conn_tstamp_find(ct);
314 	if (!tstamp)
315 		return 0;
316 
317 	nest_count = nla_nest_start(skb, CTA_TIMESTAMP);
318 	if (!nest_count)
319 		goto nla_put_failure;
320 
321 	if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
322 			 CTA_TIMESTAMP_PAD) ||
323 	    (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
324 					       cpu_to_be64(tstamp->stop),
325 					       CTA_TIMESTAMP_PAD)))
326 		goto nla_put_failure;
327 	nla_nest_end(skb, nest_count);
328 
329 	return 0;
330 
331 nla_put_failure:
332 	return -1;
333 }
334 
335 #ifdef CONFIG_NF_CONNTRACK_MARK
ctnetlink_dump_mark(struct sk_buff * skb,const struct nf_conn * ct,bool dump)336 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct,
337 			       bool dump)
338 {
339 	u32 mark = READ_ONCE(ct->mark);
340 
341 	if (!mark && !dump)
342 		return 0;
343 
344 	if (nla_put_be32(skb, CTA_MARK, htonl(mark)))
345 		goto nla_put_failure;
346 	return 0;
347 
348 nla_put_failure:
349 	return -1;
350 }
351 #else
352 #define ctnetlink_dump_mark(a, b, c) (0)
353 #endif
354 
355 #ifdef CONFIG_NF_CONNTRACK_SECMARK
ctnetlink_dump_secctx(struct sk_buff * skb,const struct nf_conn * ct)356 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
357 {
358 	struct nlattr *nest_secctx;
359 	int len, ret;
360 	char *secctx;
361 
362 	ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
363 	if (ret)
364 		return 0;
365 
366 	ret = -1;
367 	nest_secctx = nla_nest_start(skb, CTA_SECCTX);
368 	if (!nest_secctx)
369 		goto nla_put_failure;
370 
371 	if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
372 		goto nla_put_failure;
373 	nla_nest_end(skb, nest_secctx);
374 
375 	ret = 0;
376 nla_put_failure:
377 	security_release_secctx(secctx, len);
378 	return ret;
379 }
380 #else
381 #define ctnetlink_dump_secctx(a, b) (0)
382 #endif
383 
384 #ifdef CONFIG_NF_CONNTRACK_LABELS
ctnetlink_label_size(const struct nf_conn * ct)385 static inline int ctnetlink_label_size(const struct nf_conn *ct)
386 {
387 	struct nf_conn_labels *labels = nf_ct_labels_find(ct);
388 
389 	if (!labels)
390 		return 0;
391 	return nla_total_size(sizeof(labels->bits));
392 }
393 
394 static int
ctnetlink_dump_labels(struct sk_buff * skb,const struct nf_conn * ct)395 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
396 {
397 	struct nf_conn_labels *labels = nf_ct_labels_find(ct);
398 	unsigned int i;
399 
400 	if (!labels)
401 		return 0;
402 
403 	i = 0;
404 	do {
405 		if (labels->bits[i] != 0)
406 			return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
407 				       labels->bits);
408 		i++;
409 	} while (i < ARRAY_SIZE(labels->bits));
410 
411 	return 0;
412 }
413 #else
414 #define ctnetlink_dump_labels(a, b) (0)
415 #define ctnetlink_label_size(a)	(0)
416 #endif
417 
418 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
419 
ctnetlink_dump_master(struct sk_buff * skb,const struct nf_conn * ct)420 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
421 {
422 	struct nlattr *nest_parms;
423 
424 	if (!(ct->status & IPS_EXPECTED))
425 		return 0;
426 
427 	nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER);
428 	if (!nest_parms)
429 		goto nla_put_failure;
430 	if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
431 		goto nla_put_failure;
432 	nla_nest_end(skb, nest_parms);
433 
434 	return 0;
435 
436 nla_put_failure:
437 	return -1;
438 }
439 
440 static int
dump_ct_seq_adj(struct sk_buff * skb,const struct nf_ct_seqadj * seq,int type)441 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
442 {
443 	struct nlattr *nest_parms;
444 
445 	nest_parms = nla_nest_start(skb, type);
446 	if (!nest_parms)
447 		goto nla_put_failure;
448 
449 	if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
450 			 htonl(seq->correction_pos)) ||
451 	    nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
452 			 htonl(seq->offset_before)) ||
453 	    nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
454 			 htonl(seq->offset_after)))
455 		goto nla_put_failure;
456 
457 	nla_nest_end(skb, nest_parms);
458 
459 	return 0;
460 
461 nla_put_failure:
462 	return -1;
463 }
464 
ctnetlink_dump_ct_seq_adj(struct sk_buff * skb,struct nf_conn * ct)465 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
466 {
467 	struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
468 	struct nf_ct_seqadj *seq;
469 
470 	if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
471 		return 0;
472 
473 	spin_lock_bh(&ct->lock);
474 	seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
475 	if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
476 		goto err;
477 
478 	seq = &seqadj->seq[IP_CT_DIR_REPLY];
479 	if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
480 		goto err;
481 
482 	spin_unlock_bh(&ct->lock);
483 	return 0;
484 err:
485 	spin_unlock_bh(&ct->lock);
486 	return -1;
487 }
488 
ctnetlink_dump_ct_synproxy(struct sk_buff * skb,struct nf_conn * ct)489 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
490 {
491 	struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
492 	struct nlattr *nest_parms;
493 
494 	if (!synproxy)
495 		return 0;
496 
497 	nest_parms = nla_nest_start(skb, CTA_SYNPROXY);
498 	if (!nest_parms)
499 		goto nla_put_failure;
500 
501 	if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
502 	    nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
503 	    nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
504 		goto nla_put_failure;
505 
506 	nla_nest_end(skb, nest_parms);
507 
508 	return 0;
509 
510 nla_put_failure:
511 	return -1;
512 }
513 
ctnetlink_dump_id(struct sk_buff * skb,const struct nf_conn * ct)514 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
515 {
516 	__be32 id = (__force __be32)nf_ct_get_id(ct);
517 
518 	if (nla_put_be32(skb, CTA_ID, id))
519 		goto nla_put_failure;
520 	return 0;
521 
522 nla_put_failure:
523 	return -1;
524 }
525 
ctnetlink_dump_use(struct sk_buff * skb,const struct nf_conn * ct)526 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
527 {
528 	if (nla_put_be32(skb, CTA_USE, htonl(refcount_read(&ct->ct_general.use))))
529 		goto nla_put_failure;
530 	return 0;
531 
532 nla_put_failure:
533 	return -1;
534 }
535 
536 /* all these functions access ct->ext. Caller must either hold a reference
537  * on ct or prevent its deletion by holding either the bucket spinlock or
538  * pcpu dying list lock.
539  */
ctnetlink_dump_extinfo(struct sk_buff * skb,struct nf_conn * ct,u32 type)540 static int ctnetlink_dump_extinfo(struct sk_buff *skb,
541 				  struct nf_conn *ct, u32 type)
542 {
543 	if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
544 	    ctnetlink_dump_timestamp(skb, ct) < 0 ||
545 	    ctnetlink_dump_helpinfo(skb, ct) < 0 ||
546 	    ctnetlink_dump_labels(skb, ct) < 0 ||
547 	    ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
548 	    ctnetlink_dump_ct_synproxy(skb, ct) < 0)
549 		return -1;
550 
551 	return 0;
552 }
553 
ctnetlink_dump_info(struct sk_buff * skb,struct nf_conn * ct)554 static int ctnetlink_dump_info(struct sk_buff *skb, struct nf_conn *ct)
555 {
556 	if (ctnetlink_dump_status(skb, ct) < 0 ||
557 	    ctnetlink_dump_mark(skb, ct, true) < 0 ||
558 	    ctnetlink_dump_secctx(skb, ct) < 0 ||
559 	    ctnetlink_dump_id(skb, ct) < 0 ||
560 	    ctnetlink_dump_use(skb, ct) < 0 ||
561 	    ctnetlink_dump_master(skb, ct) < 0)
562 		return -1;
563 
564 	if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) &&
565 	    (ctnetlink_dump_timeout(skb, ct, false) < 0 ||
566 	     ctnetlink_dump_protoinfo(skb, ct, false) < 0))
567 		return -1;
568 
569 	return 0;
570 }
571 
572 static int
ctnetlink_fill_info(struct sk_buff * skb,u32 portid,u32 seq,u32 type,struct nf_conn * ct,bool extinfo,unsigned int flags)573 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
574 		    struct nf_conn *ct, bool extinfo, unsigned int flags)
575 {
576 	const struct nf_conntrack_zone *zone;
577 	struct nlmsghdr *nlh;
578 	struct nlattr *nest_parms;
579 	unsigned int event;
580 
581 	if (portid)
582 		flags |= NLM_F_MULTI;
583 	event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
584 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, nf_ct_l3num(ct),
585 			   NFNETLINK_V0, 0);
586 	if (!nlh)
587 		goto nlmsg_failure;
588 
589 	zone = nf_ct_zone(ct);
590 
591 	nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
592 	if (!nest_parms)
593 		goto nla_put_failure;
594 	if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
595 		goto nla_put_failure;
596 	if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
597 				   NF_CT_ZONE_DIR_ORIG) < 0)
598 		goto nla_put_failure;
599 	nla_nest_end(skb, nest_parms);
600 
601 	nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
602 	if (!nest_parms)
603 		goto nla_put_failure;
604 	if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
605 		goto nla_put_failure;
606 	if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
607 				   NF_CT_ZONE_DIR_REPL) < 0)
608 		goto nla_put_failure;
609 	nla_nest_end(skb, nest_parms);
610 
611 	if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
612 				   NF_CT_DEFAULT_ZONE_DIR) < 0)
613 		goto nla_put_failure;
614 
615 	if (ctnetlink_dump_info(skb, ct) < 0)
616 		goto nla_put_failure;
617 	if (extinfo && ctnetlink_dump_extinfo(skb, ct, type) < 0)
618 		goto nla_put_failure;
619 
620 	nlmsg_end(skb, nlh);
621 	return skb->len;
622 
623 nlmsg_failure:
624 nla_put_failure:
625 	nlmsg_cancel(skb, nlh);
626 	return -1;
627 }
628 
629 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
630 	[CTA_IP_V4_SRC]	= { .type = NLA_U32 },
631 	[CTA_IP_V4_DST]	= { .type = NLA_U32 },
632 	[CTA_IP_V6_SRC]	= { .len = sizeof(__be32) * 4 },
633 	[CTA_IP_V6_DST]	= { .len = sizeof(__be32) * 4 },
634 };
635 
636 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
ctnetlink_proto_size(const struct nf_conn * ct)637 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
638 {
639 	const struct nf_conntrack_l4proto *l4proto;
640 	size_t len, len4 = 0;
641 
642 	len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
643 	len *= 3u; /* ORIG, REPLY, MASTER */
644 
645 	l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
646 	len += l4proto->nlattr_size;
647 	if (l4proto->nlattr_tuple_size) {
648 		len4 = l4proto->nlattr_tuple_size();
649 		len4 *= 3u; /* ORIG, REPLY, MASTER */
650 	}
651 
652 	return len + len4;
653 }
654 #endif
655 
ctnetlink_acct_size(const struct nf_conn * ct)656 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
657 {
658 	if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
659 		return 0;
660 	return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
661 	       + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
662 	       + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
663 	       ;
664 }
665 
ctnetlink_secctx_size(const struct nf_conn * ct)666 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
667 {
668 #ifdef CONFIG_NF_CONNTRACK_SECMARK
669 	int len, ret;
670 
671 	ret = security_secid_to_secctx(ct->secmark, NULL, &len);
672 	if (ret)
673 		return 0;
674 
675 	return nla_total_size(0) /* CTA_SECCTX */
676 	       + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
677 #else
678 	return 0;
679 #endif
680 }
681 
ctnetlink_timestamp_size(const struct nf_conn * ct)682 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
683 {
684 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
685 	if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
686 		return 0;
687 	return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
688 #else
689 	return 0;
690 #endif
691 }
692 
693 #ifdef CONFIG_NF_CONNTRACK_EVENTS
ctnetlink_nlmsg_size(const struct nf_conn * ct)694 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
695 {
696 	return NLMSG_ALIGN(sizeof(struct nfgenmsg))
697 	       + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
698 	       + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
699 	       + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
700 	       + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
701 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
702 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
703 	       + ctnetlink_acct_size(ct)
704 	       + ctnetlink_timestamp_size(ct)
705 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
706 	       + nla_total_size(0) /* CTA_PROTOINFO */
707 	       + nla_total_size(0) /* CTA_HELP */
708 	       + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
709 	       + ctnetlink_secctx_size(ct)
710 #if IS_ENABLED(CONFIG_NF_NAT)
711 	       + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
712 	       + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
713 #endif
714 #ifdef CONFIG_NF_CONNTRACK_MARK
715 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
716 #endif
717 #ifdef CONFIG_NF_CONNTRACK_ZONES
718 	       + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
719 #endif
720 	       + ctnetlink_proto_size(ct)
721 	       + ctnetlink_label_size(ct)
722 	       ;
723 }
724 
725 static int
ctnetlink_conntrack_event(unsigned int events,const struct nf_ct_event * item)726 ctnetlink_conntrack_event(unsigned int events, const struct nf_ct_event *item)
727 {
728 	const struct nf_conntrack_zone *zone;
729 	struct net *net;
730 	struct nlmsghdr *nlh;
731 	struct nlattr *nest_parms;
732 	struct nf_conn *ct = item->ct;
733 	struct sk_buff *skb;
734 	unsigned int type;
735 	unsigned int flags = 0, group;
736 	int err;
737 
738 	if (events & (1 << IPCT_DESTROY)) {
739 		type = IPCTNL_MSG_CT_DELETE;
740 		group = NFNLGRP_CONNTRACK_DESTROY;
741 	} else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
742 		type = IPCTNL_MSG_CT_NEW;
743 		flags = NLM_F_CREATE|NLM_F_EXCL;
744 		group = NFNLGRP_CONNTRACK_NEW;
745 	} else if (events) {
746 		type = IPCTNL_MSG_CT_NEW;
747 		group = NFNLGRP_CONNTRACK_UPDATE;
748 	} else
749 		return 0;
750 
751 	net = nf_ct_net(ct);
752 	if (!item->report && !nfnetlink_has_listeners(net, group))
753 		return 0;
754 
755 	skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
756 	if (skb == NULL)
757 		goto errout;
758 
759 	type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
760 	nlh = nfnl_msg_put(skb, item->portid, 0, type, flags, nf_ct_l3num(ct),
761 			   NFNETLINK_V0, 0);
762 	if (!nlh)
763 		goto nlmsg_failure;
764 
765 	zone = nf_ct_zone(ct);
766 
767 	nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
768 	if (!nest_parms)
769 		goto nla_put_failure;
770 	if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
771 		goto nla_put_failure;
772 	if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
773 				   NF_CT_ZONE_DIR_ORIG) < 0)
774 		goto nla_put_failure;
775 	nla_nest_end(skb, nest_parms);
776 
777 	nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
778 	if (!nest_parms)
779 		goto nla_put_failure;
780 	if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
781 		goto nla_put_failure;
782 	if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
783 				   NF_CT_ZONE_DIR_REPL) < 0)
784 		goto nla_put_failure;
785 	nla_nest_end(skb, nest_parms);
786 
787 	if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
788 				   NF_CT_DEFAULT_ZONE_DIR) < 0)
789 		goto nla_put_failure;
790 
791 	if (ctnetlink_dump_id(skb, ct) < 0)
792 		goto nla_put_failure;
793 
794 	if (ctnetlink_dump_status(skb, ct) < 0)
795 		goto nla_put_failure;
796 
797 	if (events & (1 << IPCT_DESTROY)) {
798 		if (ctnetlink_dump_timeout(skb, ct, true) < 0)
799 			goto nla_put_failure;
800 
801 		if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
802 		    ctnetlink_dump_timestamp(skb, ct) < 0 ||
803 		    ctnetlink_dump_protoinfo(skb, ct, true) < 0)
804 			goto nla_put_failure;
805 	} else {
806 		if (ctnetlink_dump_timeout(skb, ct, false) < 0)
807 			goto nla_put_failure;
808 
809 		if (events & (1 << IPCT_PROTOINFO) &&
810 		    ctnetlink_dump_protoinfo(skb, ct, false) < 0)
811 			goto nla_put_failure;
812 
813 		if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
814 		    && ctnetlink_dump_helpinfo(skb, ct) < 0)
815 			goto nla_put_failure;
816 
817 #ifdef CONFIG_NF_CONNTRACK_SECMARK
818 		if ((events & (1 << IPCT_SECMARK) || ct->secmark)
819 		    && ctnetlink_dump_secctx(skb, ct) < 0)
820 			goto nla_put_failure;
821 #endif
822 		if (events & (1 << IPCT_LABEL) &&
823 		     ctnetlink_dump_labels(skb, ct) < 0)
824 			goto nla_put_failure;
825 
826 		if (events & (1 << IPCT_RELATED) &&
827 		    ctnetlink_dump_master(skb, ct) < 0)
828 			goto nla_put_failure;
829 
830 		if (events & (1 << IPCT_SEQADJ) &&
831 		    ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
832 			goto nla_put_failure;
833 
834 		if (events & (1 << IPCT_SYNPROXY) &&
835 		    ctnetlink_dump_ct_synproxy(skb, ct) < 0)
836 			goto nla_put_failure;
837 	}
838 
839 #ifdef CONFIG_NF_CONNTRACK_MARK
840 	if (ctnetlink_dump_mark(skb, ct, events & (1 << IPCT_MARK)))
841 		goto nla_put_failure;
842 #endif
843 	nlmsg_end(skb, nlh);
844 	err = nfnetlink_send(skb, net, item->portid, group, item->report,
845 			     GFP_ATOMIC);
846 	if (err == -ENOBUFS || err == -EAGAIN)
847 		return -ENOBUFS;
848 
849 	return 0;
850 
851 nla_put_failure:
852 	nlmsg_cancel(skb, nlh);
853 nlmsg_failure:
854 	kfree_skb(skb);
855 errout:
856 	if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
857 		return -ENOBUFS;
858 
859 	return 0;
860 }
861 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
862 
ctnetlink_done(struct netlink_callback * cb)863 static int ctnetlink_done(struct netlink_callback *cb)
864 {
865 	if (cb->args[1])
866 		nf_ct_put((struct nf_conn *)cb->args[1]);
867 	kfree(cb->data);
868 	return 0;
869 }
870 
871 struct ctnetlink_filter_u32 {
872 	u32 val;
873 	u32 mask;
874 };
875 
876 struct ctnetlink_filter {
877 	u8 family;
878 
879 	u_int32_t orig_flags;
880 	u_int32_t reply_flags;
881 
882 	struct nf_conntrack_tuple orig;
883 	struct nf_conntrack_tuple reply;
884 	struct nf_conntrack_zone zone;
885 
886 	struct ctnetlink_filter_u32 mark;
887 	struct ctnetlink_filter_u32 status;
888 };
889 
890 static const struct nla_policy cta_filter_nla_policy[CTA_FILTER_MAX + 1] = {
891 	[CTA_FILTER_ORIG_FLAGS]		= { .type = NLA_U32 },
892 	[CTA_FILTER_REPLY_FLAGS]	= { .type = NLA_U32 },
893 };
894 
ctnetlink_parse_filter(const struct nlattr * attr,struct ctnetlink_filter * filter)895 static int ctnetlink_parse_filter(const struct nlattr *attr,
896 				  struct ctnetlink_filter *filter)
897 {
898 	struct nlattr *tb[CTA_FILTER_MAX + 1];
899 	int ret = 0;
900 
901 	ret = nla_parse_nested(tb, CTA_FILTER_MAX, attr, cta_filter_nla_policy,
902 			       NULL);
903 	if (ret)
904 		return ret;
905 
906 	if (tb[CTA_FILTER_ORIG_FLAGS]) {
907 		filter->orig_flags = nla_get_u32(tb[CTA_FILTER_ORIG_FLAGS]);
908 		if (filter->orig_flags & ~CTA_FILTER_F_ALL)
909 			return -EOPNOTSUPP;
910 	}
911 
912 	if (tb[CTA_FILTER_REPLY_FLAGS]) {
913 		filter->reply_flags = nla_get_u32(tb[CTA_FILTER_REPLY_FLAGS]);
914 		if (filter->reply_flags & ~CTA_FILTER_F_ALL)
915 			return -EOPNOTSUPP;
916 	}
917 
918 	return 0;
919 }
920 
921 static int ctnetlink_parse_zone(const struct nlattr *attr,
922 				struct nf_conntrack_zone *zone);
923 static int ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],
924 					 struct nf_conntrack_tuple *tuple,
925 					 u32 type, u_int8_t l3num,
926 					 struct nf_conntrack_zone *zone,
927 					 u_int32_t flags);
928 
ctnetlink_filter_parse_mark(struct ctnetlink_filter_u32 * mark,const struct nlattr * const cda[])929 static int ctnetlink_filter_parse_mark(struct ctnetlink_filter_u32 *mark,
930 				       const struct nlattr * const cda[])
931 {
932 #ifdef CONFIG_NF_CONNTRACK_MARK
933 	if (cda[CTA_MARK]) {
934 		mark->val = ntohl(nla_get_be32(cda[CTA_MARK]));
935 
936 		if (cda[CTA_MARK_MASK])
937 			mark->mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
938 		else
939 			mark->mask = 0xffffffff;
940 	} else if (cda[CTA_MARK_MASK]) {
941 		return -EINVAL;
942 	}
943 #endif
944 	return 0;
945 }
946 
ctnetlink_filter_parse_status(struct ctnetlink_filter_u32 * status,const struct nlattr * const cda[])947 static int ctnetlink_filter_parse_status(struct ctnetlink_filter_u32 *status,
948 					 const struct nlattr * const cda[])
949 {
950 	if (cda[CTA_STATUS]) {
951 		status->val = ntohl(nla_get_be32(cda[CTA_STATUS]));
952 		if (cda[CTA_STATUS_MASK])
953 			status->mask = ntohl(nla_get_be32(cda[CTA_STATUS_MASK]));
954 		else
955 			status->mask = status->val;
956 
957 		/* status->val == 0? always true, else always false. */
958 		if (status->mask == 0)
959 			return -EINVAL;
960 	} else if (cda[CTA_STATUS_MASK]) {
961 		return -EINVAL;
962 	}
963 
964 	/* CTA_STATUS is NLA_U32, if this fires UAPI needs to be extended */
965 	BUILD_BUG_ON(__IPS_MAX_BIT >= 32);
966 	return 0;
967 }
968 
969 static struct ctnetlink_filter *
ctnetlink_alloc_filter(const struct nlattr * const cda[],u8 family)970 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
971 {
972 	struct ctnetlink_filter *filter;
973 	int err;
974 
975 #ifndef CONFIG_NF_CONNTRACK_MARK
976 	if (cda[CTA_MARK] || cda[CTA_MARK_MASK])
977 		return ERR_PTR(-EOPNOTSUPP);
978 #endif
979 
980 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
981 	if (filter == NULL)
982 		return ERR_PTR(-ENOMEM);
983 
984 	filter->family = family;
985 
986 	err = ctnetlink_filter_parse_mark(&filter->mark, cda);
987 	if (err)
988 		goto err_filter;
989 
990 	err = ctnetlink_filter_parse_status(&filter->status, cda);
991 	if (err)
992 		goto err_filter;
993 
994 	if (!cda[CTA_FILTER])
995 		return filter;
996 
997 	err = ctnetlink_parse_zone(cda[CTA_ZONE], &filter->zone);
998 	if (err < 0)
999 		goto err_filter;
1000 
1001 	err = ctnetlink_parse_filter(cda[CTA_FILTER], filter);
1002 	if (err < 0)
1003 		goto err_filter;
1004 
1005 	if (filter->orig_flags) {
1006 		if (!cda[CTA_TUPLE_ORIG]) {
1007 			err = -EINVAL;
1008 			goto err_filter;
1009 		}
1010 
1011 		err = ctnetlink_parse_tuple_filter(cda, &filter->orig,
1012 						   CTA_TUPLE_ORIG,
1013 						   filter->family,
1014 						   &filter->zone,
1015 						   filter->orig_flags);
1016 		if (err < 0)
1017 			goto err_filter;
1018 	}
1019 
1020 	if (filter->reply_flags) {
1021 		if (!cda[CTA_TUPLE_REPLY]) {
1022 			err = -EINVAL;
1023 			goto err_filter;
1024 		}
1025 
1026 		err = ctnetlink_parse_tuple_filter(cda, &filter->reply,
1027 						   CTA_TUPLE_REPLY,
1028 						   filter->family,
1029 						   &filter->zone,
1030 						   filter->reply_flags);
1031 		if (err < 0)
1032 			goto err_filter;
1033 	}
1034 
1035 	return filter;
1036 
1037 err_filter:
1038 	kfree(filter);
1039 
1040 	return ERR_PTR(err);
1041 }
1042 
ctnetlink_needs_filter(u8 family,const struct nlattr * const * cda)1043 static bool ctnetlink_needs_filter(u8 family, const struct nlattr * const *cda)
1044 {
1045 	return family || cda[CTA_MARK] || cda[CTA_FILTER] || cda[CTA_STATUS];
1046 }
1047 
ctnetlink_start(struct netlink_callback * cb)1048 static int ctnetlink_start(struct netlink_callback *cb)
1049 {
1050 	const struct nlattr * const *cda = cb->data;
1051 	struct ctnetlink_filter *filter = NULL;
1052 	struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1053 	u8 family = nfmsg->nfgen_family;
1054 
1055 	if (ctnetlink_needs_filter(family, cda)) {
1056 		filter = ctnetlink_alloc_filter(cda, family);
1057 		if (IS_ERR(filter))
1058 			return PTR_ERR(filter);
1059 	}
1060 
1061 	cb->data = filter;
1062 	return 0;
1063 }
1064 
ctnetlink_filter_match_tuple(struct nf_conntrack_tuple * filter_tuple,struct nf_conntrack_tuple * ct_tuple,u_int32_t flags,int family)1065 static int ctnetlink_filter_match_tuple(struct nf_conntrack_tuple *filter_tuple,
1066 					struct nf_conntrack_tuple *ct_tuple,
1067 					u_int32_t flags, int family)
1068 {
1069 	switch (family) {
1070 	case NFPROTO_IPV4:
1071 		if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) &&
1072 		    filter_tuple->src.u3.ip != ct_tuple->src.u3.ip)
1073 			return  0;
1074 
1075 		if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) &&
1076 		    filter_tuple->dst.u3.ip != ct_tuple->dst.u3.ip)
1077 			return  0;
1078 		break;
1079 	case NFPROTO_IPV6:
1080 		if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) &&
1081 		    !ipv6_addr_cmp(&filter_tuple->src.u3.in6,
1082 				   &ct_tuple->src.u3.in6))
1083 			return 0;
1084 
1085 		if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) &&
1086 		    !ipv6_addr_cmp(&filter_tuple->dst.u3.in6,
1087 				   &ct_tuple->dst.u3.in6))
1088 			return 0;
1089 		break;
1090 	}
1091 
1092 	if ((flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) &&
1093 	    filter_tuple->dst.protonum != ct_tuple->dst.protonum)
1094 		return 0;
1095 
1096 	switch (ct_tuple->dst.protonum) {
1097 	case IPPROTO_TCP:
1098 	case IPPROTO_UDP:
1099 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT)) &&
1100 		    filter_tuple->src.u.tcp.port != ct_tuple->src.u.tcp.port)
1101 			return 0;
1102 
1103 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_DST_PORT)) &&
1104 		    filter_tuple->dst.u.tcp.port != ct_tuple->dst.u.tcp.port)
1105 			return 0;
1106 		break;
1107 	case IPPROTO_ICMP:
1108 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_TYPE)) &&
1109 		    filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type)
1110 			return 0;
1111 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_CODE)) &&
1112 		    filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code)
1113 			return 0;
1114 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_ID)) &&
1115 		    filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id)
1116 			return 0;
1117 		break;
1118 	case IPPROTO_ICMPV6:
1119 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_TYPE)) &&
1120 		    filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type)
1121 			return 0;
1122 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_CODE)) &&
1123 		    filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code)
1124 			return 0;
1125 		if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_ID)) &&
1126 		    filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id)
1127 			return 0;
1128 		break;
1129 	}
1130 
1131 	return 1;
1132 }
1133 
ctnetlink_filter_match(struct nf_conn * ct,void * data)1134 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
1135 {
1136 	struct ctnetlink_filter *filter = data;
1137 	struct nf_conntrack_tuple *tuple;
1138 	u32 status;
1139 
1140 	if (filter == NULL)
1141 		goto out;
1142 
1143 	/* Match entries of a given L3 protocol number.
1144 	 * If it is not specified, ie. l3proto == 0,
1145 	 * then match everything.
1146 	 */
1147 	if (filter->family && nf_ct_l3num(ct) != filter->family)
1148 		goto ignore_entry;
1149 
1150 	if (filter->orig_flags) {
1151 		tuple = nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL);
1152 		if (!ctnetlink_filter_match_tuple(&filter->orig, tuple,
1153 						  filter->orig_flags,
1154 						  filter->family))
1155 			goto ignore_entry;
1156 	}
1157 
1158 	if (filter->reply_flags) {
1159 		tuple = nf_ct_tuple(ct, IP_CT_DIR_REPLY);
1160 		if (!ctnetlink_filter_match_tuple(&filter->reply, tuple,
1161 						  filter->reply_flags,
1162 						  filter->family))
1163 			goto ignore_entry;
1164 	}
1165 
1166 #ifdef CONFIG_NF_CONNTRACK_MARK
1167 	if ((READ_ONCE(ct->mark) & filter->mark.mask) != filter->mark.val)
1168 		goto ignore_entry;
1169 #endif
1170 	status = (u32)READ_ONCE(ct->status);
1171 	if ((status & filter->status.mask) != filter->status.val)
1172 		goto ignore_entry;
1173 
1174 out:
1175 	return 1;
1176 
1177 ignore_entry:
1178 	return 0;
1179 }
1180 
1181 static int
ctnetlink_dump_table(struct sk_buff * skb,struct netlink_callback * cb)1182 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1183 {
1184 	unsigned int flags = cb->data ? NLM_F_DUMP_FILTERED : 0;
1185 	struct net *net = sock_net(skb->sk);
1186 	struct nf_conn *ct, *last;
1187 	struct nf_conntrack_tuple_hash *h;
1188 	struct hlist_nulls_node *n;
1189 	struct nf_conn *nf_ct_evict[8];
1190 	int res, i;
1191 	spinlock_t *lockp;
1192 
1193 	last = (struct nf_conn *)cb->args[1];
1194 	i = 0;
1195 
1196 	local_bh_disable();
1197 	for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
1198 restart:
1199 		while (i) {
1200 			i--;
1201 			if (nf_ct_should_gc(nf_ct_evict[i]))
1202 				nf_ct_kill(nf_ct_evict[i]);
1203 			nf_ct_put(nf_ct_evict[i]);
1204 		}
1205 
1206 		lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
1207 		nf_conntrack_lock(lockp);
1208 		if (cb->args[0] >= nf_conntrack_htable_size) {
1209 			spin_unlock(lockp);
1210 			goto out;
1211 		}
1212 		hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
1213 					   hnnode) {
1214 			ct = nf_ct_tuplehash_to_ctrack(h);
1215 			if (nf_ct_is_expired(ct)) {
1216 				/* need to defer nf_ct_kill() until lock is released */
1217 				if (i < ARRAY_SIZE(nf_ct_evict) &&
1218 				    refcount_inc_not_zero(&ct->ct_general.use))
1219 					nf_ct_evict[i++] = ct;
1220 				continue;
1221 			}
1222 
1223 			if (!net_eq(net, nf_ct_net(ct)))
1224 				continue;
1225 
1226 			if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1227 				continue;
1228 
1229 			if (cb->args[1]) {
1230 				if (ct != last)
1231 					continue;
1232 				cb->args[1] = 0;
1233 			}
1234 			if (!ctnetlink_filter_match(ct, cb->data))
1235 				continue;
1236 
1237 			res =
1238 			ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1239 					    cb->nlh->nlmsg_seq,
1240 					    NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1241 					    ct, true, flags);
1242 			if (res < 0) {
1243 				nf_conntrack_get(&ct->ct_general);
1244 				cb->args[1] = (unsigned long)ct;
1245 				spin_unlock(lockp);
1246 				goto out;
1247 			}
1248 		}
1249 		spin_unlock(lockp);
1250 		if (cb->args[1]) {
1251 			cb->args[1] = 0;
1252 			goto restart;
1253 		}
1254 	}
1255 out:
1256 	local_bh_enable();
1257 	if (last) {
1258 		/* nf ct hash resize happened, now clear the leftover. */
1259 		if ((struct nf_conn *)cb->args[1] == last)
1260 			cb->args[1] = 0;
1261 
1262 		nf_ct_put(last);
1263 	}
1264 
1265 	while (i) {
1266 		i--;
1267 		if (nf_ct_should_gc(nf_ct_evict[i]))
1268 			nf_ct_kill(nf_ct_evict[i]);
1269 		nf_ct_put(nf_ct_evict[i]);
1270 	}
1271 
1272 	return skb->len;
1273 }
1274 
ipv4_nlattr_to_tuple(struct nlattr * tb[],struct nf_conntrack_tuple * t,u_int32_t flags)1275 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
1276 				struct nf_conntrack_tuple *t,
1277 				u_int32_t flags)
1278 {
1279 	if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1280 		if (!tb[CTA_IP_V4_SRC])
1281 			return -EINVAL;
1282 
1283 		t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
1284 	}
1285 
1286 	if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) {
1287 		if (!tb[CTA_IP_V4_DST])
1288 			return -EINVAL;
1289 
1290 		t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1291 	}
1292 
1293 	return 0;
1294 }
1295 
ipv6_nlattr_to_tuple(struct nlattr * tb[],struct nf_conntrack_tuple * t,u_int32_t flags)1296 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1297 				struct nf_conntrack_tuple *t,
1298 				u_int32_t flags)
1299 {
1300 	if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1301 		if (!tb[CTA_IP_V6_SRC])
1302 			return -EINVAL;
1303 
1304 		t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1305 	}
1306 
1307 	if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) {
1308 		if (!tb[CTA_IP_V6_DST])
1309 			return -EINVAL;
1310 
1311 		t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1312 	}
1313 
1314 	return 0;
1315 }
1316 
ctnetlink_parse_tuple_ip(struct nlattr * attr,struct nf_conntrack_tuple * tuple,u_int32_t flags)1317 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1318 				    struct nf_conntrack_tuple *tuple,
1319 				    u_int32_t flags)
1320 {
1321 	struct nlattr *tb[CTA_IP_MAX+1];
1322 	int ret = 0;
1323 
1324 	ret = nla_parse_nested_deprecated(tb, CTA_IP_MAX, attr,
1325 					  cta_ip_nla_policy, NULL);
1326 	if (ret < 0)
1327 		return ret;
1328 
1329 	switch (tuple->src.l3num) {
1330 	case NFPROTO_IPV4:
1331 		ret = ipv4_nlattr_to_tuple(tb, tuple, flags);
1332 		break;
1333 	case NFPROTO_IPV6:
1334 		ret = ipv6_nlattr_to_tuple(tb, tuple, flags);
1335 		break;
1336 	}
1337 
1338 	return ret;
1339 }
1340 
1341 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1342 	[CTA_PROTO_NUM]	= { .type = NLA_U8 },
1343 };
1344 
ctnetlink_parse_tuple_proto(struct nlattr * attr,struct nf_conntrack_tuple * tuple,u_int32_t flags)1345 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1346 				       struct nf_conntrack_tuple *tuple,
1347 				       u_int32_t flags)
1348 {
1349 	const struct nf_conntrack_l4proto *l4proto;
1350 	struct nlattr *tb[CTA_PROTO_MAX+1];
1351 	int ret = 0;
1352 
1353 	ret = nla_parse_nested_deprecated(tb, CTA_PROTO_MAX, attr,
1354 					  proto_nla_policy, NULL);
1355 	if (ret < 0)
1356 		return ret;
1357 
1358 	if (!(flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)))
1359 		return 0;
1360 
1361 	if (!tb[CTA_PROTO_NUM])
1362 		return -EINVAL;
1363 
1364 	tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1365 
1366 	rcu_read_lock();
1367 	l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
1368 
1369 	if (likely(l4proto->nlattr_to_tuple)) {
1370 		ret = nla_validate_nested_deprecated(attr, CTA_PROTO_MAX,
1371 						     l4proto->nla_policy,
1372 						     NULL);
1373 		if (ret == 0)
1374 			ret = l4proto->nlattr_to_tuple(tb, tuple, flags);
1375 	}
1376 
1377 	rcu_read_unlock();
1378 
1379 	return ret;
1380 }
1381 
1382 static int
ctnetlink_parse_zone(const struct nlattr * attr,struct nf_conntrack_zone * zone)1383 ctnetlink_parse_zone(const struct nlattr *attr,
1384 		     struct nf_conntrack_zone *zone)
1385 {
1386 	nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1387 			NF_CT_DEFAULT_ZONE_DIR, 0);
1388 #ifdef CONFIG_NF_CONNTRACK_ZONES
1389 	if (attr)
1390 		zone->id = ntohs(nla_get_be16(attr));
1391 #else
1392 	if (attr)
1393 		return -EOPNOTSUPP;
1394 #endif
1395 	return 0;
1396 }
1397 
1398 static int
ctnetlink_parse_tuple_zone(struct nlattr * attr,enum ctattr_type type,struct nf_conntrack_zone * zone)1399 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1400 			   struct nf_conntrack_zone *zone)
1401 {
1402 	int ret;
1403 
1404 	if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1405 		return -EINVAL;
1406 
1407 	ret = ctnetlink_parse_zone(attr, zone);
1408 	if (ret < 0)
1409 		return ret;
1410 
1411 	if (type == CTA_TUPLE_REPLY)
1412 		zone->dir = NF_CT_ZONE_DIR_REPL;
1413 	else
1414 		zone->dir = NF_CT_ZONE_DIR_ORIG;
1415 
1416 	return 0;
1417 }
1418 
1419 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1420 	[CTA_TUPLE_IP]		= { .type = NLA_NESTED },
1421 	[CTA_TUPLE_PROTO]	= { .type = NLA_NESTED },
1422 	[CTA_TUPLE_ZONE]	= { .type = NLA_U16 },
1423 };
1424 
1425 #define CTA_FILTER_F_ALL_CTA_PROTO \
1426   (CTA_FILTER_F_CTA_PROTO_SRC_PORT | \
1427    CTA_FILTER_F_CTA_PROTO_DST_PORT | \
1428    CTA_FILTER_F_CTA_PROTO_ICMP_TYPE | \
1429    CTA_FILTER_F_CTA_PROTO_ICMP_CODE | \
1430    CTA_FILTER_F_CTA_PROTO_ICMP_ID | \
1431    CTA_FILTER_F_CTA_PROTO_ICMPV6_TYPE | \
1432    CTA_FILTER_F_CTA_PROTO_ICMPV6_CODE | \
1433    CTA_FILTER_F_CTA_PROTO_ICMPV6_ID)
1434 
1435 static int
ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],struct nf_conntrack_tuple * tuple,u32 type,u_int8_t l3num,struct nf_conntrack_zone * zone,u_int32_t flags)1436 ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],
1437 			      struct nf_conntrack_tuple *tuple, u32 type,
1438 			      u_int8_t l3num, struct nf_conntrack_zone *zone,
1439 			      u_int32_t flags)
1440 {
1441 	struct nlattr *tb[CTA_TUPLE_MAX+1];
1442 	int err;
1443 
1444 	memset(tuple, 0, sizeof(*tuple));
1445 
1446 	err = nla_parse_nested_deprecated(tb, CTA_TUPLE_MAX, cda[type],
1447 					  tuple_nla_policy, NULL);
1448 	if (err < 0)
1449 		return err;
1450 
1451 	if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1452 		return -EOPNOTSUPP;
1453 	tuple->src.l3num = l3num;
1454 
1455 	if (flags & CTA_FILTER_FLAG(CTA_IP_DST) ||
1456 	    flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1457 		if (!tb[CTA_TUPLE_IP])
1458 			return -EINVAL;
1459 
1460 		err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple, flags);
1461 		if (err < 0)
1462 			return err;
1463 	}
1464 
1465 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) {
1466 		if (!tb[CTA_TUPLE_PROTO])
1467 			return -EINVAL;
1468 
1469 		err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple, flags);
1470 		if (err < 0)
1471 			return err;
1472 	} else if (flags & CTA_FILTER_FLAG(ALL_CTA_PROTO)) {
1473 		/* Can't manage proto flags without a protonum  */
1474 		return -EINVAL;
1475 	}
1476 
1477 	if ((flags & CTA_FILTER_FLAG(CTA_TUPLE_ZONE)) && tb[CTA_TUPLE_ZONE]) {
1478 		if (!zone)
1479 			return -EINVAL;
1480 
1481 		err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1482 						 type, zone);
1483 		if (err < 0)
1484 			return err;
1485 	}
1486 
1487 	/* orig and expect tuples get DIR_ORIGINAL */
1488 	if (type == CTA_TUPLE_REPLY)
1489 		tuple->dst.dir = IP_CT_DIR_REPLY;
1490 	else
1491 		tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1492 
1493 	return 0;
1494 }
1495 
1496 static int
ctnetlink_parse_tuple(const struct nlattr * const cda[],struct nf_conntrack_tuple * tuple,u32 type,u_int8_t l3num,struct nf_conntrack_zone * zone)1497 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1498 		      struct nf_conntrack_tuple *tuple, u32 type,
1499 		      u_int8_t l3num, struct nf_conntrack_zone *zone)
1500 {
1501 	return ctnetlink_parse_tuple_filter(cda, tuple, type, l3num, zone,
1502 					    CTA_FILTER_FLAG(ALL));
1503 }
1504 
1505 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1506 	[CTA_HELP_NAME]		= { .type = NLA_NUL_STRING,
1507 				    .len = NF_CT_HELPER_NAME_LEN - 1 },
1508 };
1509 
ctnetlink_parse_help(const struct nlattr * attr,char ** helper_name,struct nlattr ** helpinfo)1510 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1511 				struct nlattr **helpinfo)
1512 {
1513 	int err;
1514 	struct nlattr *tb[CTA_HELP_MAX+1];
1515 
1516 	err = nla_parse_nested_deprecated(tb, CTA_HELP_MAX, attr,
1517 					  help_nla_policy, NULL);
1518 	if (err < 0)
1519 		return err;
1520 
1521 	if (!tb[CTA_HELP_NAME])
1522 		return -EINVAL;
1523 
1524 	*helper_name = nla_data(tb[CTA_HELP_NAME]);
1525 
1526 	if (tb[CTA_HELP_INFO])
1527 		*helpinfo = tb[CTA_HELP_INFO];
1528 
1529 	return 0;
1530 }
1531 
1532 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1533 	[CTA_TUPLE_ORIG]	= { .type = NLA_NESTED },
1534 	[CTA_TUPLE_REPLY]	= { .type = NLA_NESTED },
1535 	[CTA_STATUS] 		= { .type = NLA_U32 },
1536 	[CTA_PROTOINFO]		= { .type = NLA_NESTED },
1537 	[CTA_HELP]		= { .type = NLA_NESTED },
1538 	[CTA_NAT_SRC]		= { .type = NLA_NESTED },
1539 	[CTA_TIMEOUT] 		= { .type = NLA_U32 },
1540 	[CTA_MARK]		= { .type = NLA_U32 },
1541 	[CTA_ID]		= { .type = NLA_U32 },
1542 	[CTA_NAT_DST]		= { .type = NLA_NESTED },
1543 	[CTA_TUPLE_MASTER]	= { .type = NLA_NESTED },
1544 	[CTA_NAT_SEQ_ADJ_ORIG]  = { .type = NLA_NESTED },
1545 	[CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1546 	[CTA_ZONE]		= { .type = NLA_U16 },
1547 	[CTA_MARK_MASK]		= { .type = NLA_U32 },
1548 	[CTA_LABELS]		= { .type = NLA_BINARY,
1549 				    .len = NF_CT_LABELS_MAX_SIZE },
1550 	[CTA_LABELS_MASK]	= { .type = NLA_BINARY,
1551 				    .len = NF_CT_LABELS_MAX_SIZE },
1552 	[CTA_FILTER]		= { .type = NLA_NESTED },
1553 	[CTA_STATUS_MASK]	= { .type = NLA_U32 },
1554 };
1555 
ctnetlink_flush_iterate(struct nf_conn * ct,void * data)1556 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1557 {
1558 	return ctnetlink_filter_match(ct, data);
1559 }
1560 
ctnetlink_flush_conntrack(struct net * net,const struct nlattr * const cda[],u32 portid,int report,u8 family)1561 static int ctnetlink_flush_conntrack(struct net *net,
1562 				     const struct nlattr * const cda[],
1563 				     u32 portid, int report, u8 family)
1564 {
1565 	struct ctnetlink_filter *filter = NULL;
1566 	struct nf_ct_iter_data iter = {
1567 		.net		= net,
1568 		.portid		= portid,
1569 		.report		= report,
1570 	};
1571 
1572 	if (ctnetlink_needs_filter(family, cda)) {
1573 		if (cda[CTA_FILTER])
1574 			return -EOPNOTSUPP;
1575 
1576 		filter = ctnetlink_alloc_filter(cda, family);
1577 		if (IS_ERR(filter))
1578 			return PTR_ERR(filter);
1579 
1580 		iter.data = filter;
1581 	}
1582 
1583 	nf_ct_iterate_cleanup_net(ctnetlink_flush_iterate, &iter);
1584 	kfree(filter);
1585 
1586 	return 0;
1587 }
1588 
ctnetlink_del_conntrack(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])1589 static int ctnetlink_del_conntrack(struct sk_buff *skb,
1590 				   const struct nfnl_info *info,
1591 				   const struct nlattr * const cda[])
1592 {
1593 	u8 family = info->nfmsg->nfgen_family;
1594 	struct nf_conntrack_tuple_hash *h;
1595 	struct nf_conntrack_tuple tuple;
1596 	struct nf_conntrack_zone zone;
1597 	struct nf_conn *ct;
1598 	int err;
1599 
1600 	err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1601 	if (err < 0)
1602 		return err;
1603 
1604 	if (cda[CTA_TUPLE_ORIG])
1605 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1606 					    family, &zone);
1607 	else if (cda[CTA_TUPLE_REPLY])
1608 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1609 					    family, &zone);
1610 	else {
1611 		u_int8_t u3 = info->nfmsg->version ? family : AF_UNSPEC;
1612 
1613 		return ctnetlink_flush_conntrack(info->net, cda,
1614 						 NETLINK_CB(skb).portid,
1615 						 nlmsg_report(info->nlh), u3);
1616 	}
1617 
1618 	if (err < 0)
1619 		return err;
1620 
1621 	h = nf_conntrack_find_get(info->net, &zone, &tuple);
1622 	if (!h)
1623 		return -ENOENT;
1624 
1625 	ct = nf_ct_tuplehash_to_ctrack(h);
1626 
1627 	if (cda[CTA_ID]) {
1628 		__be32 id = nla_get_be32(cda[CTA_ID]);
1629 
1630 		if (id != (__force __be32)nf_ct_get_id(ct)) {
1631 			nf_ct_put(ct);
1632 			return -ENOENT;
1633 		}
1634 	}
1635 
1636 	nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(info->nlh));
1637 	nf_ct_put(ct);
1638 
1639 	return 0;
1640 }
1641 
ctnetlink_get_conntrack(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])1642 static int ctnetlink_get_conntrack(struct sk_buff *skb,
1643 				   const struct nfnl_info *info,
1644 				   const struct nlattr * const cda[])
1645 {
1646 	u_int8_t u3 = info->nfmsg->nfgen_family;
1647 	struct nf_conntrack_tuple_hash *h;
1648 	struct nf_conntrack_tuple tuple;
1649 	struct nf_conntrack_zone zone;
1650 	struct sk_buff *skb2;
1651 	struct nf_conn *ct;
1652 	int err;
1653 
1654 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1655 		struct netlink_dump_control c = {
1656 			.start = ctnetlink_start,
1657 			.dump = ctnetlink_dump_table,
1658 			.done = ctnetlink_done,
1659 			.data = (void *)cda,
1660 		};
1661 
1662 		return netlink_dump_start(info->sk, skb, info->nlh, &c);
1663 	}
1664 
1665 	err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1666 	if (err < 0)
1667 		return err;
1668 
1669 	if (cda[CTA_TUPLE_ORIG])
1670 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1671 					    u3, &zone);
1672 	else if (cda[CTA_TUPLE_REPLY])
1673 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1674 					    u3, &zone);
1675 	else
1676 		return -EINVAL;
1677 
1678 	if (err < 0)
1679 		return err;
1680 
1681 	h = nf_conntrack_find_get(info->net, &zone, &tuple);
1682 	if (!h)
1683 		return -ENOENT;
1684 
1685 	ct = nf_ct_tuplehash_to_ctrack(h);
1686 
1687 	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1688 	if (!skb2) {
1689 		nf_ct_put(ct);
1690 		return -ENOMEM;
1691 	}
1692 
1693 	err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid,
1694 				  info->nlh->nlmsg_seq,
1695 				  NFNL_MSG_TYPE(info->nlh->nlmsg_type), ct,
1696 				  true, 0);
1697 	nf_ct_put(ct);
1698 	if (err <= 0) {
1699 		kfree_skb(skb2);
1700 		return -ENOMEM;
1701 	}
1702 
1703 	return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1704 }
1705 
ctnetlink_done_list(struct netlink_callback * cb)1706 static int ctnetlink_done_list(struct netlink_callback *cb)
1707 {
1708 	struct ctnetlink_list_dump_ctx *ctx = (void *)cb->ctx;
1709 
1710 	if (ctx->last)
1711 		nf_ct_put(ctx->last);
1712 
1713 	return 0;
1714 }
1715 
1716 #ifdef CONFIG_NF_CONNTRACK_EVENTS
ctnetlink_dump_one_entry(struct sk_buff * skb,struct netlink_callback * cb,struct nf_conn * ct,bool dying)1717 static int ctnetlink_dump_one_entry(struct sk_buff *skb,
1718 				    struct netlink_callback *cb,
1719 				    struct nf_conn *ct,
1720 				    bool dying)
1721 {
1722 	struct ctnetlink_list_dump_ctx *ctx = (void *)cb->ctx;
1723 	struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1724 	u8 l3proto = nfmsg->nfgen_family;
1725 	int res;
1726 
1727 	if (l3proto && nf_ct_l3num(ct) != l3proto)
1728 		return 0;
1729 
1730 	if (ctx->last) {
1731 		if (ct != ctx->last)
1732 			return 0;
1733 
1734 		ctx->last = NULL;
1735 	}
1736 
1737 	/* We can't dump extension info for the unconfirmed
1738 	 * list because unconfirmed conntracks can have
1739 	 * ct->ext reallocated (and thus freed).
1740 	 *
1741 	 * In the dying list case ct->ext can't be free'd
1742 	 * until after we drop pcpu->lock.
1743 	 */
1744 	res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1745 				  cb->nlh->nlmsg_seq,
1746 				  NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1747 				  ct, dying, 0);
1748 	if (res < 0) {
1749 		if (!refcount_inc_not_zero(&ct->ct_general.use))
1750 			return 0;
1751 
1752 		ctx->last = ct;
1753 	}
1754 
1755 	return res;
1756 }
1757 #endif
1758 
1759 static int
ctnetlink_dump_unconfirmed(struct sk_buff * skb,struct netlink_callback * cb)1760 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1761 {
1762 	return 0;
1763 }
1764 
1765 static int
ctnetlink_dump_dying(struct sk_buff * skb,struct netlink_callback * cb)1766 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1767 {
1768 	struct ctnetlink_list_dump_ctx *ctx = (void *)cb->ctx;
1769 	struct nf_conn *last = ctx->last;
1770 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1771 	const struct net *net = sock_net(skb->sk);
1772 	struct nf_conntrack_net_ecache *ecache_net;
1773 	struct nf_conntrack_tuple_hash *h;
1774 	struct hlist_nulls_node *n;
1775 #endif
1776 
1777 	if (ctx->done)
1778 		return 0;
1779 
1780 	ctx->last = NULL;
1781 
1782 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1783 	ecache_net = nf_conn_pernet_ecache(net);
1784 	spin_lock_bh(&ecache_net->dying_lock);
1785 
1786 	hlist_nulls_for_each_entry(h, n, &ecache_net->dying_list, hnnode) {
1787 		struct nf_conn *ct;
1788 		int res;
1789 
1790 		ct = nf_ct_tuplehash_to_ctrack(h);
1791 		if (last && last != ct)
1792 			continue;
1793 
1794 		res = ctnetlink_dump_one_entry(skb, cb, ct, true);
1795 		if (res < 0) {
1796 			spin_unlock_bh(&ecache_net->dying_lock);
1797 			nf_ct_put(last);
1798 			return skb->len;
1799 		}
1800 
1801 		nf_ct_put(last);
1802 		last = NULL;
1803 	}
1804 
1805 	spin_unlock_bh(&ecache_net->dying_lock);
1806 #endif
1807 	ctx->done = true;
1808 	nf_ct_put(last);
1809 
1810 	return skb->len;
1811 }
1812 
ctnetlink_get_ct_dying(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])1813 static int ctnetlink_get_ct_dying(struct sk_buff *skb,
1814 				  const struct nfnl_info *info,
1815 				  const struct nlattr * const cda[])
1816 {
1817 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1818 		struct netlink_dump_control c = {
1819 			.dump = ctnetlink_dump_dying,
1820 			.done = ctnetlink_done_list,
1821 		};
1822 		return netlink_dump_start(info->sk, skb, info->nlh, &c);
1823 	}
1824 
1825 	return -EOPNOTSUPP;
1826 }
1827 
ctnetlink_get_ct_unconfirmed(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])1828 static int ctnetlink_get_ct_unconfirmed(struct sk_buff *skb,
1829 					const struct nfnl_info *info,
1830 					const struct nlattr * const cda[])
1831 {
1832 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1833 		struct netlink_dump_control c = {
1834 			.dump = ctnetlink_dump_unconfirmed,
1835 			.done = ctnetlink_done_list,
1836 		};
1837 		return netlink_dump_start(info->sk, skb, info->nlh, &c);
1838 	}
1839 
1840 	return -EOPNOTSUPP;
1841 }
1842 
1843 #if IS_ENABLED(CONFIG_NF_NAT)
1844 static int
ctnetlink_parse_nat_setup(struct nf_conn * ct,enum nf_nat_manip_type manip,const struct nlattr * attr)1845 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1846 			  enum nf_nat_manip_type manip,
1847 			  const struct nlattr *attr)
1848 	__must_hold(RCU)
1849 {
1850 	const struct nf_nat_hook *nat_hook;
1851 	int err;
1852 
1853 	nat_hook = rcu_dereference(nf_nat_hook);
1854 	if (!nat_hook) {
1855 #ifdef CONFIG_MODULES
1856 		rcu_read_unlock();
1857 		nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1858 		if (request_module("nf-nat") < 0) {
1859 			nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1860 			rcu_read_lock();
1861 			return -EOPNOTSUPP;
1862 		}
1863 		nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1864 		rcu_read_lock();
1865 		nat_hook = rcu_dereference(nf_nat_hook);
1866 		if (nat_hook)
1867 			return -EAGAIN;
1868 #endif
1869 		return -EOPNOTSUPP;
1870 	}
1871 
1872 	err = nat_hook->parse_nat_setup(ct, manip, attr);
1873 	if (err == -EAGAIN) {
1874 #ifdef CONFIG_MODULES
1875 		rcu_read_unlock();
1876 		nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1877 		if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1878 			nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1879 			rcu_read_lock();
1880 			return -EOPNOTSUPP;
1881 		}
1882 		nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1883 		rcu_read_lock();
1884 #else
1885 		err = -EOPNOTSUPP;
1886 #endif
1887 	}
1888 	return err;
1889 }
1890 #endif
1891 
1892 static int
ctnetlink_change_status(struct nf_conn * ct,const struct nlattr * const cda[])1893 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1894 {
1895 	return nf_ct_change_status_common(ct, ntohl(nla_get_be32(cda[CTA_STATUS])));
1896 }
1897 
1898 static int
ctnetlink_setup_nat(struct nf_conn * ct,const struct nlattr * const cda[])1899 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1900 {
1901 #if IS_ENABLED(CONFIG_NF_NAT)
1902 	int ret;
1903 
1904 	if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1905 		return 0;
1906 
1907 	ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1908 					cda[CTA_NAT_DST]);
1909 	if (ret < 0)
1910 		return ret;
1911 
1912 	return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1913 					 cda[CTA_NAT_SRC]);
1914 #else
1915 	if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1916 		return 0;
1917 	return -EOPNOTSUPP;
1918 #endif
1919 }
1920 
ctnetlink_change_helper(struct nf_conn * ct,const struct nlattr * const cda[])1921 static int ctnetlink_change_helper(struct nf_conn *ct,
1922 				   const struct nlattr * const cda[])
1923 {
1924 	struct nf_conntrack_helper *helper;
1925 	struct nf_conn_help *help = nfct_help(ct);
1926 	char *helpname = NULL;
1927 	struct nlattr *helpinfo = NULL;
1928 	int err;
1929 
1930 	err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1931 	if (err < 0)
1932 		return err;
1933 
1934 	/* don't change helper of sibling connections */
1935 	if (ct->master) {
1936 		/* If we try to change the helper to the same thing twice,
1937 		 * treat the second attempt as a no-op instead of returning
1938 		 * an error.
1939 		 */
1940 		err = -EBUSY;
1941 		if (help) {
1942 			rcu_read_lock();
1943 			helper = rcu_dereference(help->helper);
1944 			if (helper && !strcmp(helper->name, helpname))
1945 				err = 0;
1946 			rcu_read_unlock();
1947 		}
1948 
1949 		return err;
1950 	}
1951 
1952 	if (!strcmp(helpname, "")) {
1953 		if (help && help->helper) {
1954 			/* we had a helper before ... */
1955 			nf_ct_remove_expectations(ct);
1956 			RCU_INIT_POINTER(help->helper, NULL);
1957 		}
1958 
1959 		return 0;
1960 	}
1961 
1962 	rcu_read_lock();
1963 	helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1964 					    nf_ct_protonum(ct));
1965 	if (helper == NULL) {
1966 		rcu_read_unlock();
1967 		return -EOPNOTSUPP;
1968 	}
1969 
1970 	if (help) {
1971 		if (rcu_access_pointer(help->helper) == helper) {
1972 			/* update private helper data if allowed. */
1973 			if (helper->from_nlattr)
1974 				helper->from_nlattr(helpinfo, ct);
1975 			err = 0;
1976 		} else
1977 			err = -EBUSY;
1978 	} else {
1979 		/* we cannot set a helper for an existing conntrack */
1980 		err = -EOPNOTSUPP;
1981 	}
1982 
1983 	rcu_read_unlock();
1984 	return err;
1985 }
1986 
ctnetlink_change_timeout(struct nf_conn * ct,const struct nlattr * const cda[])1987 static int ctnetlink_change_timeout(struct nf_conn *ct,
1988 				    const struct nlattr * const cda[])
1989 {
1990 	return __nf_ct_change_timeout(ct, (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ);
1991 }
1992 
1993 #if defined(CONFIG_NF_CONNTRACK_MARK)
ctnetlink_change_mark(struct nf_conn * ct,const struct nlattr * const cda[])1994 static void ctnetlink_change_mark(struct nf_conn *ct,
1995 				    const struct nlattr * const cda[])
1996 {
1997 	u32 mark, newmark, mask = 0;
1998 
1999 	if (cda[CTA_MARK_MASK])
2000 		mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2001 
2002 	mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2003 	newmark = (READ_ONCE(ct->mark) & mask) ^ mark;
2004 	if (newmark != READ_ONCE(ct->mark))
2005 		WRITE_ONCE(ct->mark, newmark);
2006 }
2007 #endif
2008 
2009 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
2010 	[CTA_PROTOINFO_TCP]	= { .type = NLA_NESTED },
2011 	[CTA_PROTOINFO_DCCP]	= { .type = NLA_NESTED },
2012 	[CTA_PROTOINFO_SCTP]	= { .type = NLA_NESTED },
2013 };
2014 
ctnetlink_change_protoinfo(struct nf_conn * ct,const struct nlattr * const cda[])2015 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
2016 				      const struct nlattr * const cda[])
2017 {
2018 	const struct nlattr *attr = cda[CTA_PROTOINFO];
2019 	const struct nf_conntrack_l4proto *l4proto;
2020 	struct nlattr *tb[CTA_PROTOINFO_MAX+1];
2021 	int err = 0;
2022 
2023 	err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_MAX, attr,
2024 					  protoinfo_policy, NULL);
2025 	if (err < 0)
2026 		return err;
2027 
2028 	l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
2029 	if (l4proto->from_nlattr)
2030 		err = l4proto->from_nlattr(tb, ct);
2031 
2032 	return err;
2033 }
2034 
2035 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
2036 	[CTA_SEQADJ_CORRECTION_POS]	= { .type = NLA_U32 },
2037 	[CTA_SEQADJ_OFFSET_BEFORE]	= { .type = NLA_U32 },
2038 	[CTA_SEQADJ_OFFSET_AFTER]	= { .type = NLA_U32 },
2039 };
2040 
change_seq_adj(struct nf_ct_seqadj * seq,const struct nlattr * const attr)2041 static int change_seq_adj(struct nf_ct_seqadj *seq,
2042 			  const struct nlattr * const attr)
2043 {
2044 	int err;
2045 	struct nlattr *cda[CTA_SEQADJ_MAX+1];
2046 
2047 	err = nla_parse_nested_deprecated(cda, CTA_SEQADJ_MAX, attr,
2048 					  seqadj_policy, NULL);
2049 	if (err < 0)
2050 		return err;
2051 
2052 	if (!cda[CTA_SEQADJ_CORRECTION_POS])
2053 		return -EINVAL;
2054 
2055 	seq->correction_pos =
2056 		ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
2057 
2058 	if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
2059 		return -EINVAL;
2060 
2061 	seq->offset_before =
2062 		ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
2063 
2064 	if (!cda[CTA_SEQADJ_OFFSET_AFTER])
2065 		return -EINVAL;
2066 
2067 	seq->offset_after =
2068 		ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
2069 
2070 	return 0;
2071 }
2072 
2073 static int
ctnetlink_change_seq_adj(struct nf_conn * ct,const struct nlattr * const cda[])2074 ctnetlink_change_seq_adj(struct nf_conn *ct,
2075 			 const struct nlattr * const cda[])
2076 {
2077 	struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
2078 	int ret = 0;
2079 
2080 	if (!seqadj)
2081 		return 0;
2082 
2083 	spin_lock_bh(&ct->lock);
2084 	if (cda[CTA_SEQ_ADJ_ORIG]) {
2085 		ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
2086 				     cda[CTA_SEQ_ADJ_ORIG]);
2087 		if (ret < 0)
2088 			goto err;
2089 
2090 		set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
2091 	}
2092 
2093 	if (cda[CTA_SEQ_ADJ_REPLY]) {
2094 		ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
2095 				     cda[CTA_SEQ_ADJ_REPLY]);
2096 		if (ret < 0)
2097 			goto err;
2098 
2099 		set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
2100 	}
2101 
2102 	spin_unlock_bh(&ct->lock);
2103 	return 0;
2104 err:
2105 	spin_unlock_bh(&ct->lock);
2106 	return ret;
2107 }
2108 
2109 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
2110 	[CTA_SYNPROXY_ISN]	= { .type = NLA_U32 },
2111 	[CTA_SYNPROXY_ITS]	= { .type = NLA_U32 },
2112 	[CTA_SYNPROXY_TSOFF]	= { .type = NLA_U32 },
2113 };
2114 
ctnetlink_change_synproxy(struct nf_conn * ct,const struct nlattr * const cda[])2115 static int ctnetlink_change_synproxy(struct nf_conn *ct,
2116 				     const struct nlattr * const cda[])
2117 {
2118 	struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
2119 	struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
2120 	int err;
2121 
2122 	if (!synproxy)
2123 		return 0;
2124 
2125 	err = nla_parse_nested_deprecated(tb, CTA_SYNPROXY_MAX,
2126 					  cda[CTA_SYNPROXY], synproxy_policy,
2127 					  NULL);
2128 	if (err < 0)
2129 		return err;
2130 
2131 	if (!tb[CTA_SYNPROXY_ISN] ||
2132 	    !tb[CTA_SYNPROXY_ITS] ||
2133 	    !tb[CTA_SYNPROXY_TSOFF])
2134 		return -EINVAL;
2135 
2136 	synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
2137 	synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
2138 	synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
2139 
2140 	return 0;
2141 }
2142 
2143 static int
ctnetlink_attach_labels(struct nf_conn * ct,const struct nlattr * const cda[])2144 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
2145 {
2146 #ifdef CONFIG_NF_CONNTRACK_LABELS
2147 	size_t len = nla_len(cda[CTA_LABELS]);
2148 	const void *mask = cda[CTA_LABELS_MASK];
2149 
2150 	if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
2151 		return -EINVAL;
2152 
2153 	if (mask) {
2154 		if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
2155 		    nla_len(cda[CTA_LABELS_MASK]) != len)
2156 			return -EINVAL;
2157 		mask = nla_data(cda[CTA_LABELS_MASK]);
2158 	}
2159 
2160 	len /= sizeof(u32);
2161 
2162 	return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
2163 #else
2164 	return -EOPNOTSUPP;
2165 #endif
2166 }
2167 
2168 static int
ctnetlink_change_conntrack(struct nf_conn * ct,const struct nlattr * const cda[])2169 ctnetlink_change_conntrack(struct nf_conn *ct,
2170 			   const struct nlattr * const cda[])
2171 {
2172 	int err;
2173 
2174 	/* only allow NAT changes and master assignation for new conntracks */
2175 	if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
2176 		return -EOPNOTSUPP;
2177 
2178 	if (cda[CTA_HELP]) {
2179 		err = ctnetlink_change_helper(ct, cda);
2180 		if (err < 0)
2181 			return err;
2182 	}
2183 
2184 	if (cda[CTA_TIMEOUT]) {
2185 		err = ctnetlink_change_timeout(ct, cda);
2186 		if (err < 0)
2187 			return err;
2188 	}
2189 
2190 	if (cda[CTA_STATUS]) {
2191 		err = ctnetlink_change_status(ct, cda);
2192 		if (err < 0)
2193 			return err;
2194 	}
2195 
2196 	if (cda[CTA_PROTOINFO]) {
2197 		err = ctnetlink_change_protoinfo(ct, cda);
2198 		if (err < 0)
2199 			return err;
2200 	}
2201 
2202 #if defined(CONFIG_NF_CONNTRACK_MARK)
2203 	if (cda[CTA_MARK])
2204 		ctnetlink_change_mark(ct, cda);
2205 #endif
2206 
2207 	if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2208 		err = ctnetlink_change_seq_adj(ct, cda);
2209 		if (err < 0)
2210 			return err;
2211 	}
2212 
2213 	if (cda[CTA_SYNPROXY]) {
2214 		err = ctnetlink_change_synproxy(ct, cda);
2215 		if (err < 0)
2216 			return err;
2217 	}
2218 
2219 	if (cda[CTA_LABELS]) {
2220 		err = ctnetlink_attach_labels(ct, cda);
2221 		if (err < 0)
2222 			return err;
2223 	}
2224 
2225 	return 0;
2226 }
2227 
2228 static struct nf_conn *
ctnetlink_create_conntrack(struct net * net,const struct nf_conntrack_zone * zone,const struct nlattr * const cda[],struct nf_conntrack_tuple * otuple,struct nf_conntrack_tuple * rtuple,u8 u3)2229 ctnetlink_create_conntrack(struct net *net,
2230 			   const struct nf_conntrack_zone *zone,
2231 			   const struct nlattr * const cda[],
2232 			   struct nf_conntrack_tuple *otuple,
2233 			   struct nf_conntrack_tuple *rtuple,
2234 			   u8 u3)
2235 {
2236 	struct nf_conn *ct;
2237 	int err = -EINVAL;
2238 	struct nf_conntrack_helper *helper;
2239 	struct nf_conn_tstamp *tstamp;
2240 	u64 timeout;
2241 
2242 	ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
2243 	if (IS_ERR(ct))
2244 		return ERR_PTR(-ENOMEM);
2245 
2246 	if (!cda[CTA_TIMEOUT])
2247 		goto err1;
2248 
2249 	rcu_read_lock();
2250  	if (cda[CTA_HELP]) {
2251 		char *helpname = NULL;
2252 		struct nlattr *helpinfo = NULL;
2253 
2254 		err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
2255  		if (err < 0)
2256 			goto err2;
2257 
2258 		helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2259 						    nf_ct_protonum(ct));
2260 		if (helper == NULL) {
2261 			rcu_read_unlock();
2262 #ifdef CONFIG_MODULES
2263 			if (request_module("nfct-helper-%s", helpname) < 0) {
2264 				err = -EOPNOTSUPP;
2265 				goto err1;
2266 			}
2267 
2268 			rcu_read_lock();
2269 			helper = __nf_conntrack_helper_find(helpname,
2270 							    nf_ct_l3num(ct),
2271 							    nf_ct_protonum(ct));
2272 			if (helper) {
2273 				err = -EAGAIN;
2274 				goto err2;
2275 			}
2276 			rcu_read_unlock();
2277 #endif
2278 			err = -EOPNOTSUPP;
2279 			goto err1;
2280 		} else {
2281 			struct nf_conn_help *help;
2282 
2283 			help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
2284 			if (help == NULL) {
2285 				err = -ENOMEM;
2286 				goto err2;
2287 			}
2288 			/* set private helper data if allowed. */
2289 			if (helper->from_nlattr)
2290 				helper->from_nlattr(helpinfo, ct);
2291 
2292 			/* disable helper auto-assignment for this entry */
2293 			ct->status |= IPS_HELPER;
2294 			RCU_INIT_POINTER(help->helper, helper);
2295 		}
2296 	}
2297 
2298 	err = ctnetlink_setup_nat(ct, cda);
2299 	if (err < 0)
2300 		goto err2;
2301 
2302 	nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2303 	nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2304 	nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2305 	nf_ct_labels_ext_add(ct);
2306 	nfct_seqadj_ext_add(ct);
2307 	nfct_synproxy_ext_add(ct);
2308 
2309 	/* we must add conntrack extensions before confirmation. */
2310 	ct->status |= IPS_CONFIRMED;
2311 
2312 	timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
2313 	__nf_ct_set_timeout(ct, timeout);
2314 
2315 	if (cda[CTA_STATUS]) {
2316 		err = ctnetlink_change_status(ct, cda);
2317 		if (err < 0)
2318 			goto err2;
2319 	}
2320 
2321 	if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2322 		err = ctnetlink_change_seq_adj(ct, cda);
2323 		if (err < 0)
2324 			goto err2;
2325 	}
2326 
2327 	memset(&ct->proto, 0, sizeof(ct->proto));
2328 	if (cda[CTA_PROTOINFO]) {
2329 		err = ctnetlink_change_protoinfo(ct, cda);
2330 		if (err < 0)
2331 			goto err2;
2332 	}
2333 
2334 	if (cda[CTA_SYNPROXY]) {
2335 		err = ctnetlink_change_synproxy(ct, cda);
2336 		if (err < 0)
2337 			goto err2;
2338 	}
2339 
2340 #if defined(CONFIG_NF_CONNTRACK_MARK)
2341 	if (cda[CTA_MARK])
2342 		ctnetlink_change_mark(ct, cda);
2343 #endif
2344 
2345 	/* setup master conntrack: this is a confirmed expectation */
2346 	if (cda[CTA_TUPLE_MASTER]) {
2347 		struct nf_conntrack_tuple master;
2348 		struct nf_conntrack_tuple_hash *master_h;
2349 		struct nf_conn *master_ct;
2350 
2351 		err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2352 					    u3, NULL);
2353 		if (err < 0)
2354 			goto err2;
2355 
2356 		master_h = nf_conntrack_find_get(net, zone, &master);
2357 		if (master_h == NULL) {
2358 			err = -ENOENT;
2359 			goto err2;
2360 		}
2361 		master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2362 		__set_bit(IPS_EXPECTED_BIT, &ct->status);
2363 		ct->master = master_ct;
2364 	}
2365 	tstamp = nf_conn_tstamp_find(ct);
2366 	if (tstamp)
2367 		tstamp->start = ktime_get_real_ns();
2368 
2369 	err = nf_conntrack_hash_check_insert(ct);
2370 	if (err < 0)
2371 		goto err3;
2372 
2373 	rcu_read_unlock();
2374 
2375 	return ct;
2376 
2377 err3:
2378 	if (ct->master)
2379 		nf_ct_put(ct->master);
2380 err2:
2381 	rcu_read_unlock();
2382 err1:
2383 	nf_conntrack_free(ct);
2384 	return ERR_PTR(err);
2385 }
2386 
ctnetlink_new_conntrack(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])2387 static int ctnetlink_new_conntrack(struct sk_buff *skb,
2388 				   const struct nfnl_info *info,
2389 				   const struct nlattr * const cda[])
2390 {
2391 	struct nf_conntrack_tuple otuple, rtuple;
2392 	struct nf_conntrack_tuple_hash *h = NULL;
2393 	u_int8_t u3 = info->nfmsg->nfgen_family;
2394 	struct nf_conntrack_zone zone;
2395 	struct nf_conn *ct;
2396 	int err;
2397 
2398 	err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2399 	if (err < 0)
2400 		return err;
2401 
2402 	if (cda[CTA_TUPLE_ORIG]) {
2403 		err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2404 					    u3, &zone);
2405 		if (err < 0)
2406 			return err;
2407 	}
2408 
2409 	if (cda[CTA_TUPLE_REPLY]) {
2410 		err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2411 					    u3, &zone);
2412 		if (err < 0)
2413 			return err;
2414 	}
2415 
2416 	if (cda[CTA_TUPLE_ORIG])
2417 		h = nf_conntrack_find_get(info->net, &zone, &otuple);
2418 	else if (cda[CTA_TUPLE_REPLY])
2419 		h = nf_conntrack_find_get(info->net, &zone, &rtuple);
2420 
2421 	if (h == NULL) {
2422 		err = -ENOENT;
2423 		if (info->nlh->nlmsg_flags & NLM_F_CREATE) {
2424 			enum ip_conntrack_events events;
2425 
2426 			if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2427 				return -EINVAL;
2428 			if (otuple.dst.protonum != rtuple.dst.protonum)
2429 				return -EINVAL;
2430 
2431 			ct = ctnetlink_create_conntrack(info->net, &zone, cda,
2432 							&otuple, &rtuple, u3);
2433 			if (IS_ERR(ct))
2434 				return PTR_ERR(ct);
2435 
2436 			err = 0;
2437 			if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2438 				events = 1 << IPCT_RELATED;
2439 			else
2440 				events = 1 << IPCT_NEW;
2441 
2442 			if (cda[CTA_LABELS] &&
2443 			    ctnetlink_attach_labels(ct, cda) == 0)
2444 				events |= (1 << IPCT_LABEL);
2445 
2446 			nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2447 						      (1 << IPCT_ASSURED) |
2448 						      (1 << IPCT_HELPER) |
2449 						      (1 << IPCT_PROTOINFO) |
2450 						      (1 << IPCT_SEQADJ) |
2451 						      (1 << IPCT_MARK) |
2452 						      (1 << IPCT_SYNPROXY) |
2453 						      events,
2454 						      ct, NETLINK_CB(skb).portid,
2455 						      nlmsg_report(info->nlh));
2456 			nf_ct_put(ct);
2457 		}
2458 
2459 		return err;
2460 	}
2461 	/* implicit 'else' */
2462 
2463 	err = -EEXIST;
2464 	ct = nf_ct_tuplehash_to_ctrack(h);
2465 	if (!(info->nlh->nlmsg_flags & NLM_F_EXCL)) {
2466 		err = ctnetlink_change_conntrack(ct, cda);
2467 		if (err == 0) {
2468 			nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2469 						      (1 << IPCT_ASSURED) |
2470 						      (1 << IPCT_HELPER) |
2471 						      (1 << IPCT_LABEL) |
2472 						      (1 << IPCT_PROTOINFO) |
2473 						      (1 << IPCT_SEQADJ) |
2474 						      (1 << IPCT_MARK) |
2475 						      (1 << IPCT_SYNPROXY),
2476 						      ct, NETLINK_CB(skb).portid,
2477 						      nlmsg_report(info->nlh));
2478 		}
2479 	}
2480 
2481 	nf_ct_put(ct);
2482 	return err;
2483 }
2484 
2485 static int
ctnetlink_ct_stat_cpu_fill_info(struct sk_buff * skb,u32 portid,u32 seq,__u16 cpu,const struct ip_conntrack_stat * st)2486 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2487 				__u16 cpu, const struct ip_conntrack_stat *st)
2488 {
2489 	struct nlmsghdr *nlh;
2490 	unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2491 
2492 	event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2493 			      IPCTNL_MSG_CT_GET_STATS_CPU);
2494 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2495 			   NFNETLINK_V0, htons(cpu));
2496 	if (!nlh)
2497 		goto nlmsg_failure;
2498 
2499 	if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2500 	    nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2501 	    nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2502 	    nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2503 				htonl(st->insert_failed)) ||
2504 	    nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2505 	    nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2506 	    nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2507 	    nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2508 				htonl(st->search_restart)) ||
2509 	    nla_put_be32(skb, CTA_STATS_CLASH_RESOLVE,
2510 				htonl(st->clash_resolve)) ||
2511 	    nla_put_be32(skb, CTA_STATS_CHAIN_TOOLONG,
2512 			 htonl(st->chaintoolong)))
2513 		goto nla_put_failure;
2514 
2515 	nlmsg_end(skb, nlh);
2516 	return skb->len;
2517 
2518 nla_put_failure:
2519 nlmsg_failure:
2520 	nlmsg_cancel(skb, nlh);
2521 	return -1;
2522 }
2523 
2524 static int
ctnetlink_ct_stat_cpu_dump(struct sk_buff * skb,struct netlink_callback * cb)2525 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2526 {
2527 	int cpu;
2528 	struct net *net = sock_net(skb->sk);
2529 
2530 	if (cb->args[0] == nr_cpu_ids)
2531 		return 0;
2532 
2533 	for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2534 		const struct ip_conntrack_stat *st;
2535 
2536 		if (!cpu_possible(cpu))
2537 			continue;
2538 
2539 		st = per_cpu_ptr(net->ct.stat, cpu);
2540 		if (ctnetlink_ct_stat_cpu_fill_info(skb,
2541 						    NETLINK_CB(cb->skb).portid,
2542 						    cb->nlh->nlmsg_seq,
2543 						    cpu, st) < 0)
2544 				break;
2545 	}
2546 	cb->args[0] = cpu;
2547 
2548 	return skb->len;
2549 }
2550 
ctnetlink_stat_ct_cpu(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])2551 static int ctnetlink_stat_ct_cpu(struct sk_buff *skb,
2552 				 const struct nfnl_info *info,
2553 				 const struct nlattr * const cda[])
2554 {
2555 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
2556 		struct netlink_dump_control c = {
2557 			.dump = ctnetlink_ct_stat_cpu_dump,
2558 		};
2559 		return netlink_dump_start(info->sk, skb, info->nlh, &c);
2560 	}
2561 
2562 	return 0;
2563 }
2564 
2565 static int
ctnetlink_stat_ct_fill_info(struct sk_buff * skb,u32 portid,u32 seq,u32 type,struct net * net)2566 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2567 			    struct net *net)
2568 {
2569 	unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2570 	unsigned int nr_conntracks;
2571 	struct nlmsghdr *nlh;
2572 
2573 	event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2574 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2575 			   NFNETLINK_V0, 0);
2576 	if (!nlh)
2577 		goto nlmsg_failure;
2578 
2579 	nr_conntracks = nf_conntrack_count(net);
2580 	if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2581 		goto nla_put_failure;
2582 
2583 	if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2584 		goto nla_put_failure;
2585 
2586 	nlmsg_end(skb, nlh);
2587 	return skb->len;
2588 
2589 nla_put_failure:
2590 nlmsg_failure:
2591 	nlmsg_cancel(skb, nlh);
2592 	return -1;
2593 }
2594 
ctnetlink_stat_ct(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])2595 static int ctnetlink_stat_ct(struct sk_buff *skb, const struct nfnl_info *info,
2596 			     const struct nlattr * const cda[])
2597 {
2598 	struct sk_buff *skb2;
2599 	int err;
2600 
2601 	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2602 	if (skb2 == NULL)
2603 		return -ENOMEM;
2604 
2605 	err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2606 					  info->nlh->nlmsg_seq,
2607 					  NFNL_MSG_TYPE(info->nlh->nlmsg_type),
2608 					  sock_net(skb->sk));
2609 	if (err <= 0) {
2610 		kfree_skb(skb2);
2611 		return -ENOMEM;
2612 	}
2613 
2614 	return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2615 }
2616 
2617 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2618 	[CTA_EXPECT_MASTER]	= { .type = NLA_NESTED },
2619 	[CTA_EXPECT_TUPLE]	= { .type = NLA_NESTED },
2620 	[CTA_EXPECT_MASK]	= { .type = NLA_NESTED },
2621 	[CTA_EXPECT_TIMEOUT]	= { .type = NLA_U32 },
2622 	[CTA_EXPECT_ID]		= { .type = NLA_U32 },
2623 	[CTA_EXPECT_HELP_NAME]	= { .type = NLA_NUL_STRING,
2624 				    .len = NF_CT_HELPER_NAME_LEN - 1 },
2625 	[CTA_EXPECT_ZONE]	= { .type = NLA_U16 },
2626 	[CTA_EXPECT_FLAGS]	= { .type = NLA_U32 },
2627 	[CTA_EXPECT_CLASS]	= { .type = NLA_U32 },
2628 	[CTA_EXPECT_NAT]	= { .type = NLA_NESTED },
2629 	[CTA_EXPECT_FN]		= { .type = NLA_NUL_STRING },
2630 };
2631 
2632 static struct nf_conntrack_expect *
2633 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2634 		       struct nf_conntrack_helper *helper,
2635 		       struct nf_conntrack_tuple *tuple,
2636 		       struct nf_conntrack_tuple *mask);
2637 
2638 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2639 static size_t
ctnetlink_glue_build_size(const struct nf_conn * ct)2640 ctnetlink_glue_build_size(const struct nf_conn *ct)
2641 {
2642 	return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2643 	       + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2644 	       + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2645 	       + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2646 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2647 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2648 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2649 	       + nla_total_size(0) /* CTA_PROTOINFO */
2650 	       + nla_total_size(0) /* CTA_HELP */
2651 	       + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2652 	       + ctnetlink_secctx_size(ct)
2653 	       + ctnetlink_acct_size(ct)
2654 	       + ctnetlink_timestamp_size(ct)
2655 #if IS_ENABLED(CONFIG_NF_NAT)
2656 	       + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2657 	       + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2658 #endif
2659 #ifdef CONFIG_NF_CONNTRACK_MARK
2660 	       + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2661 #endif
2662 #ifdef CONFIG_NF_CONNTRACK_ZONES
2663 	       + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2664 #endif
2665 	       + ctnetlink_proto_size(ct)
2666 	       ;
2667 }
2668 
__ctnetlink_glue_build(struct sk_buff * skb,struct nf_conn * ct)2669 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2670 {
2671 	const struct nf_conntrack_zone *zone;
2672 	struct nlattr *nest_parms;
2673 
2674 	zone = nf_ct_zone(ct);
2675 
2676 	nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
2677 	if (!nest_parms)
2678 		goto nla_put_failure;
2679 	if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2680 		goto nla_put_failure;
2681 	if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2682 				   NF_CT_ZONE_DIR_ORIG) < 0)
2683 		goto nla_put_failure;
2684 	nla_nest_end(skb, nest_parms);
2685 
2686 	nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
2687 	if (!nest_parms)
2688 		goto nla_put_failure;
2689 	if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2690 		goto nla_put_failure;
2691 	if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2692 				   NF_CT_ZONE_DIR_REPL) < 0)
2693 		goto nla_put_failure;
2694 	nla_nest_end(skb, nest_parms);
2695 
2696 	if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2697 				   NF_CT_DEFAULT_ZONE_DIR) < 0)
2698 		goto nla_put_failure;
2699 
2700 	if (ctnetlink_dump_id(skb, ct) < 0)
2701 		goto nla_put_failure;
2702 
2703 	if (ctnetlink_dump_status(skb, ct) < 0)
2704 		goto nla_put_failure;
2705 
2706 	if (ctnetlink_dump_timeout(skb, ct, false) < 0)
2707 		goto nla_put_failure;
2708 
2709 	if (ctnetlink_dump_protoinfo(skb, ct, false) < 0)
2710 		goto nla_put_failure;
2711 
2712 	if (ctnetlink_dump_acct(skb, ct, IPCTNL_MSG_CT_GET) < 0 ||
2713 	    ctnetlink_dump_timestamp(skb, ct) < 0)
2714 		goto nla_put_failure;
2715 
2716 	if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2717 		goto nla_put_failure;
2718 
2719 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2720 	if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2721 		goto nla_put_failure;
2722 #endif
2723 	if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2724 		goto nla_put_failure;
2725 
2726 	if ((ct->status & IPS_SEQ_ADJUST) &&
2727 	    ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2728 		goto nla_put_failure;
2729 
2730 	if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2731 		goto nla_put_failure;
2732 
2733 #ifdef CONFIG_NF_CONNTRACK_MARK
2734 	if (ctnetlink_dump_mark(skb, ct, true) < 0)
2735 		goto nla_put_failure;
2736 #endif
2737 	if (ctnetlink_dump_labels(skb, ct) < 0)
2738 		goto nla_put_failure;
2739 	return 0;
2740 
2741 nla_put_failure:
2742 	return -ENOSPC;
2743 }
2744 
2745 static int
ctnetlink_glue_build(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,u_int16_t ct_attr,u_int16_t ct_info_attr)2746 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2747 		     enum ip_conntrack_info ctinfo,
2748 		     u_int16_t ct_attr, u_int16_t ct_info_attr)
2749 {
2750 	struct nlattr *nest_parms;
2751 
2752 	nest_parms = nla_nest_start(skb, ct_attr);
2753 	if (!nest_parms)
2754 		goto nla_put_failure;
2755 
2756 	if (__ctnetlink_glue_build(skb, ct) < 0)
2757 		goto nla_put_failure;
2758 
2759 	nla_nest_end(skb, nest_parms);
2760 
2761 	if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2762 		goto nla_put_failure;
2763 
2764 	return 0;
2765 
2766 nla_put_failure:
2767 	return -ENOSPC;
2768 }
2769 
2770 static int
ctnetlink_update_status(struct nf_conn * ct,const struct nlattr * const cda[])2771 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2772 {
2773 	unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2774 	unsigned long d = ct->status ^ status;
2775 
2776 	if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2777 		/* SEEN_REPLY bit can only be set */
2778 		return -EBUSY;
2779 
2780 	if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2781 		/* ASSURED bit can only be set */
2782 		return -EBUSY;
2783 
2784 	/* This check is less strict than ctnetlink_change_status()
2785 	 * because callers often flip IPS_EXPECTED bits when sending
2786 	 * an NFQA_CT attribute to the kernel.  So ignore the
2787 	 * unchangeable bits but do not error out. Also user programs
2788 	 * are allowed to clear the bits that they are allowed to change.
2789 	 */
2790 	__nf_ct_change_status(ct, status, ~status);
2791 	return 0;
2792 }
2793 
2794 static int
ctnetlink_glue_parse_ct(const struct nlattr * cda[],struct nf_conn * ct)2795 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2796 {
2797 	int err;
2798 
2799 	if (cda[CTA_TIMEOUT]) {
2800 		err = ctnetlink_change_timeout(ct, cda);
2801 		if (err < 0)
2802 			return err;
2803 	}
2804 	if (cda[CTA_STATUS]) {
2805 		err = ctnetlink_update_status(ct, cda);
2806 		if (err < 0)
2807 			return err;
2808 	}
2809 	if (cda[CTA_HELP]) {
2810 		err = ctnetlink_change_helper(ct, cda);
2811 		if (err < 0)
2812 			return err;
2813 	}
2814 	if (cda[CTA_LABELS]) {
2815 		err = ctnetlink_attach_labels(ct, cda);
2816 		if (err < 0)
2817 			return err;
2818 	}
2819 #if defined(CONFIG_NF_CONNTRACK_MARK)
2820 	if (cda[CTA_MARK]) {
2821 		ctnetlink_change_mark(ct, cda);
2822 	}
2823 #endif
2824 	return 0;
2825 }
2826 
2827 static int
ctnetlink_glue_parse(const struct nlattr * attr,struct nf_conn * ct)2828 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2829 {
2830 	struct nlattr *cda[CTA_MAX+1];
2831 	int ret;
2832 
2833 	ret = nla_parse_nested_deprecated(cda, CTA_MAX, attr, ct_nla_policy,
2834 					  NULL);
2835 	if (ret < 0)
2836 		return ret;
2837 
2838 	return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2839 }
2840 
ctnetlink_glue_exp_parse(const struct nlattr * const * cda,const struct nf_conn * ct,struct nf_conntrack_tuple * tuple,struct nf_conntrack_tuple * mask)2841 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2842 				    const struct nf_conn *ct,
2843 				    struct nf_conntrack_tuple *tuple,
2844 				    struct nf_conntrack_tuple *mask)
2845 {
2846 	int err;
2847 
2848 	err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2849 				    nf_ct_l3num(ct), NULL);
2850 	if (err < 0)
2851 		return err;
2852 
2853 	return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2854 				     nf_ct_l3num(ct), NULL);
2855 }
2856 
2857 static int
ctnetlink_glue_attach_expect(const struct nlattr * attr,struct nf_conn * ct,u32 portid,u32 report)2858 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2859 			     u32 portid, u32 report)
2860 {
2861 	struct nlattr *cda[CTA_EXPECT_MAX+1];
2862 	struct nf_conntrack_tuple tuple, mask;
2863 	struct nf_conntrack_helper *helper = NULL;
2864 	struct nf_conntrack_expect *exp;
2865 	int err;
2866 
2867 	err = nla_parse_nested_deprecated(cda, CTA_EXPECT_MAX, attr,
2868 					  exp_nla_policy, NULL);
2869 	if (err < 0)
2870 		return err;
2871 
2872 	err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2873 				       ct, &tuple, &mask);
2874 	if (err < 0)
2875 		return err;
2876 
2877 	if (cda[CTA_EXPECT_HELP_NAME]) {
2878 		const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2879 
2880 		helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2881 						    nf_ct_protonum(ct));
2882 		if (helper == NULL)
2883 			return -EOPNOTSUPP;
2884 	}
2885 
2886 	exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2887 				     helper, &tuple, &mask);
2888 	if (IS_ERR(exp))
2889 		return PTR_ERR(exp);
2890 
2891 	err = nf_ct_expect_related_report(exp, portid, report, 0);
2892 	nf_ct_expect_put(exp);
2893 	return err;
2894 }
2895 
ctnetlink_glue_seqadj(struct sk_buff * skb,struct nf_conn * ct,enum ip_conntrack_info ctinfo,int diff)2896 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2897 				  enum ip_conntrack_info ctinfo, int diff)
2898 {
2899 	if (!(ct->status & IPS_NAT_MASK))
2900 		return;
2901 
2902 	nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2903 }
2904 
2905 static const struct nfnl_ct_hook ctnetlink_glue_hook = {
2906 	.build_size	= ctnetlink_glue_build_size,
2907 	.build		= ctnetlink_glue_build,
2908 	.parse		= ctnetlink_glue_parse,
2909 	.attach_expect	= ctnetlink_glue_attach_expect,
2910 	.seq_adjust	= ctnetlink_glue_seqadj,
2911 };
2912 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2913 
2914 /***********************************************************************
2915  * EXPECT
2916  ***********************************************************************/
2917 
ctnetlink_exp_dump_tuple(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple,u32 type)2918 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2919 				    const struct nf_conntrack_tuple *tuple,
2920 				    u32 type)
2921 {
2922 	struct nlattr *nest_parms;
2923 
2924 	nest_parms = nla_nest_start(skb, type);
2925 	if (!nest_parms)
2926 		goto nla_put_failure;
2927 	if (ctnetlink_dump_tuples(skb, tuple) < 0)
2928 		goto nla_put_failure;
2929 	nla_nest_end(skb, nest_parms);
2930 
2931 	return 0;
2932 
2933 nla_put_failure:
2934 	return -1;
2935 }
2936 
ctnetlink_exp_dump_mask(struct sk_buff * skb,const struct nf_conntrack_tuple * tuple,const struct nf_conntrack_tuple_mask * mask)2937 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2938 				   const struct nf_conntrack_tuple *tuple,
2939 				   const struct nf_conntrack_tuple_mask *mask)
2940 {
2941 	const struct nf_conntrack_l4proto *l4proto;
2942 	struct nf_conntrack_tuple m;
2943 	struct nlattr *nest_parms;
2944 	int ret;
2945 
2946 	memset(&m, 0xFF, sizeof(m));
2947 	memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2948 	m.src.u.all = mask->src.u.all;
2949 	m.src.l3num = tuple->src.l3num;
2950 	m.dst.protonum = tuple->dst.protonum;
2951 
2952 	nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK);
2953 	if (!nest_parms)
2954 		goto nla_put_failure;
2955 
2956 	rcu_read_lock();
2957 	ret = ctnetlink_dump_tuples_ip(skb, &m);
2958 	if (ret >= 0) {
2959 		l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
2960 		ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2961 	}
2962 	rcu_read_unlock();
2963 
2964 	if (unlikely(ret < 0))
2965 		goto nla_put_failure;
2966 
2967 	nla_nest_end(skb, nest_parms);
2968 
2969 	return 0;
2970 
2971 nla_put_failure:
2972 	return -1;
2973 }
2974 
2975 #if IS_ENABLED(CONFIG_NF_NAT)
2976 static const union nf_inet_addr any_addr;
2977 #endif
2978 
nf_expect_get_id(const struct nf_conntrack_expect * exp)2979 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2980 {
2981 	static siphash_aligned_key_t exp_id_seed;
2982 	unsigned long a, b, c, d;
2983 
2984 	net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2985 
2986 	a = (unsigned long)exp;
2987 	b = (unsigned long)exp->helper;
2988 	c = (unsigned long)exp->master;
2989 	d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2990 
2991 #ifdef CONFIG_64BIT
2992 	return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2993 #else
2994 	return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2995 #endif
2996 }
2997 
2998 static int
ctnetlink_exp_dump_expect(struct sk_buff * skb,const struct nf_conntrack_expect * exp)2999 ctnetlink_exp_dump_expect(struct sk_buff *skb,
3000 			  const struct nf_conntrack_expect *exp)
3001 {
3002 	struct nf_conn *master = exp->master;
3003 	long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
3004 	struct nf_conn_help *help;
3005 #if IS_ENABLED(CONFIG_NF_NAT)
3006 	struct nlattr *nest_parms;
3007 	struct nf_conntrack_tuple nat_tuple = {};
3008 #endif
3009 	struct nf_ct_helper_expectfn *expfn;
3010 
3011 	if (timeout < 0)
3012 		timeout = 0;
3013 
3014 	if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
3015 		goto nla_put_failure;
3016 	if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
3017 		goto nla_put_failure;
3018 	if (ctnetlink_exp_dump_tuple(skb,
3019 				 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
3020 				 CTA_EXPECT_MASTER) < 0)
3021 		goto nla_put_failure;
3022 
3023 #if IS_ENABLED(CONFIG_NF_NAT)
3024 	if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
3025 	    exp->saved_proto.all) {
3026 		nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT);
3027 		if (!nest_parms)
3028 			goto nla_put_failure;
3029 
3030 		if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
3031 			goto nla_put_failure;
3032 
3033 		nat_tuple.src.l3num = nf_ct_l3num(master);
3034 		nat_tuple.src.u3 = exp->saved_addr;
3035 		nat_tuple.dst.protonum = nf_ct_protonum(master);
3036 		nat_tuple.src.u = exp->saved_proto;
3037 
3038 		if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
3039 						CTA_EXPECT_NAT_TUPLE) < 0)
3040 	                goto nla_put_failure;
3041 	        nla_nest_end(skb, nest_parms);
3042 	}
3043 #endif
3044 	if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
3045 	    nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
3046 	    nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
3047 	    nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
3048 		goto nla_put_failure;
3049 	help = nfct_help(master);
3050 	if (help) {
3051 		struct nf_conntrack_helper *helper;
3052 
3053 		helper = rcu_dereference(help->helper);
3054 		if (helper &&
3055 		    nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
3056 			goto nla_put_failure;
3057 	}
3058 	expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
3059 	if (expfn != NULL &&
3060 	    nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
3061 		goto nla_put_failure;
3062 
3063 	return 0;
3064 
3065 nla_put_failure:
3066 	return -1;
3067 }
3068 
3069 static int
ctnetlink_exp_fill_info(struct sk_buff * skb,u32 portid,u32 seq,int event,const struct nf_conntrack_expect * exp)3070 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
3071 			int event, const struct nf_conntrack_expect *exp)
3072 {
3073 	struct nlmsghdr *nlh;
3074 	unsigned int flags = portid ? NLM_F_MULTI : 0;
3075 
3076 	event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
3077 	nlh = nfnl_msg_put(skb, portid, seq, event, flags,
3078 			   exp->tuple.src.l3num, NFNETLINK_V0, 0);
3079 	if (!nlh)
3080 		goto nlmsg_failure;
3081 
3082 	if (ctnetlink_exp_dump_expect(skb, exp) < 0)
3083 		goto nla_put_failure;
3084 
3085 	nlmsg_end(skb, nlh);
3086 	return skb->len;
3087 
3088 nlmsg_failure:
3089 nla_put_failure:
3090 	nlmsg_cancel(skb, nlh);
3091 	return -1;
3092 }
3093 
3094 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3095 static int
ctnetlink_expect_event(unsigned int events,const struct nf_exp_event * item)3096 ctnetlink_expect_event(unsigned int events, const struct nf_exp_event *item)
3097 {
3098 	struct nf_conntrack_expect *exp = item->exp;
3099 	struct net *net = nf_ct_exp_net(exp);
3100 	struct nlmsghdr *nlh;
3101 	struct sk_buff *skb;
3102 	unsigned int type, group;
3103 	int flags = 0;
3104 
3105 	if (events & (1 << IPEXP_DESTROY)) {
3106 		type = IPCTNL_MSG_EXP_DELETE;
3107 		group = NFNLGRP_CONNTRACK_EXP_DESTROY;
3108 	} else if (events & (1 << IPEXP_NEW)) {
3109 		type = IPCTNL_MSG_EXP_NEW;
3110 		flags = NLM_F_CREATE|NLM_F_EXCL;
3111 		group = NFNLGRP_CONNTRACK_EXP_NEW;
3112 	} else
3113 		return 0;
3114 
3115 	if (!item->report && !nfnetlink_has_listeners(net, group))
3116 		return 0;
3117 
3118 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3119 	if (skb == NULL)
3120 		goto errout;
3121 
3122 	type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
3123 	nlh = nfnl_msg_put(skb, item->portid, 0, type, flags,
3124 			   exp->tuple.src.l3num, NFNETLINK_V0, 0);
3125 	if (!nlh)
3126 		goto nlmsg_failure;
3127 
3128 	if (ctnetlink_exp_dump_expect(skb, exp) < 0)
3129 		goto nla_put_failure;
3130 
3131 	nlmsg_end(skb, nlh);
3132 	nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
3133 	return 0;
3134 
3135 nla_put_failure:
3136 	nlmsg_cancel(skb, nlh);
3137 nlmsg_failure:
3138 	kfree_skb(skb);
3139 errout:
3140 	nfnetlink_set_err(net, 0, 0, -ENOBUFS);
3141 	return 0;
3142 }
3143 #endif
ctnetlink_exp_done(struct netlink_callback * cb)3144 static int ctnetlink_exp_done(struct netlink_callback *cb)
3145 {
3146 	if (cb->args[1])
3147 		nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
3148 	return 0;
3149 }
3150 
3151 static int
ctnetlink_exp_dump_table(struct sk_buff * skb,struct netlink_callback * cb)3152 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
3153 {
3154 	struct net *net = sock_net(skb->sk);
3155 	struct nf_conntrack_expect *exp, *last;
3156 	struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3157 	u_int8_t l3proto = nfmsg->nfgen_family;
3158 
3159 	rcu_read_lock();
3160 	last = (struct nf_conntrack_expect *)cb->args[1];
3161 	for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
3162 restart:
3163 		hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
3164 					 hnode) {
3165 			if (l3proto && exp->tuple.src.l3num != l3proto)
3166 				continue;
3167 
3168 			if (!net_eq(nf_ct_net(exp->master), net))
3169 				continue;
3170 
3171 			if (cb->args[1]) {
3172 				if (exp != last)
3173 					continue;
3174 				cb->args[1] = 0;
3175 			}
3176 			if (ctnetlink_exp_fill_info(skb,
3177 						    NETLINK_CB(cb->skb).portid,
3178 						    cb->nlh->nlmsg_seq,
3179 						    IPCTNL_MSG_EXP_NEW,
3180 						    exp) < 0) {
3181 				if (!refcount_inc_not_zero(&exp->use))
3182 					continue;
3183 				cb->args[1] = (unsigned long)exp;
3184 				goto out;
3185 			}
3186 		}
3187 		if (cb->args[1]) {
3188 			cb->args[1] = 0;
3189 			goto restart;
3190 		}
3191 	}
3192 out:
3193 	rcu_read_unlock();
3194 	if (last)
3195 		nf_ct_expect_put(last);
3196 
3197 	return skb->len;
3198 }
3199 
3200 static int
ctnetlink_exp_ct_dump_table(struct sk_buff * skb,struct netlink_callback * cb)3201 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
3202 {
3203 	struct nf_conntrack_expect *exp, *last;
3204 	struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3205 	struct nf_conn *ct = cb->data;
3206 	struct nf_conn_help *help = nfct_help(ct);
3207 	u_int8_t l3proto = nfmsg->nfgen_family;
3208 
3209 	if (cb->args[0])
3210 		return 0;
3211 
3212 	rcu_read_lock();
3213 	last = (struct nf_conntrack_expect *)cb->args[1];
3214 restart:
3215 	hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
3216 		if (l3proto && exp->tuple.src.l3num != l3proto)
3217 			continue;
3218 		if (cb->args[1]) {
3219 			if (exp != last)
3220 				continue;
3221 			cb->args[1] = 0;
3222 		}
3223 		if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
3224 					    cb->nlh->nlmsg_seq,
3225 					    IPCTNL_MSG_EXP_NEW,
3226 					    exp) < 0) {
3227 			if (!refcount_inc_not_zero(&exp->use))
3228 				continue;
3229 			cb->args[1] = (unsigned long)exp;
3230 			goto out;
3231 		}
3232 	}
3233 	if (cb->args[1]) {
3234 		cb->args[1] = 0;
3235 		goto restart;
3236 	}
3237 	cb->args[0] = 1;
3238 out:
3239 	rcu_read_unlock();
3240 	if (last)
3241 		nf_ct_expect_put(last);
3242 
3243 	return skb->len;
3244 }
3245 
ctnetlink_dump_exp_ct(struct net * net,struct sock * ctnl,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const cda[],struct netlink_ext_ack * extack)3246 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
3247 				 struct sk_buff *skb,
3248 				 const struct nlmsghdr *nlh,
3249 				 const struct nlattr * const cda[],
3250 				 struct netlink_ext_ack *extack)
3251 {
3252 	int err;
3253 	struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3254 	u_int8_t u3 = nfmsg->nfgen_family;
3255 	struct nf_conntrack_tuple tuple;
3256 	struct nf_conntrack_tuple_hash *h;
3257 	struct nf_conn *ct;
3258 	struct nf_conntrack_zone zone;
3259 	struct netlink_dump_control c = {
3260 		.dump = ctnetlink_exp_ct_dump_table,
3261 		.done = ctnetlink_exp_done,
3262 	};
3263 
3264 	err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3265 				    u3, NULL);
3266 	if (err < 0)
3267 		return err;
3268 
3269 	err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3270 	if (err < 0)
3271 		return err;
3272 
3273 	h = nf_conntrack_find_get(net, &zone, &tuple);
3274 	if (!h)
3275 		return -ENOENT;
3276 
3277 	ct = nf_ct_tuplehash_to_ctrack(h);
3278 	/* No expectation linked to this connection tracking. */
3279 	if (!nfct_help(ct)) {
3280 		nf_ct_put(ct);
3281 		return 0;
3282 	}
3283 
3284 	c.data = ct;
3285 
3286 	err = netlink_dump_start(ctnl, skb, nlh, &c);
3287 	nf_ct_put(ct);
3288 
3289 	return err;
3290 }
3291 
ctnetlink_get_expect(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])3292 static int ctnetlink_get_expect(struct sk_buff *skb,
3293 				const struct nfnl_info *info,
3294 				const struct nlattr * const cda[])
3295 {
3296 	u_int8_t u3 = info->nfmsg->nfgen_family;
3297 	struct nf_conntrack_tuple tuple;
3298 	struct nf_conntrack_expect *exp;
3299 	struct nf_conntrack_zone zone;
3300 	struct sk_buff *skb2;
3301 	int err;
3302 
3303 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3304 		if (cda[CTA_EXPECT_MASTER])
3305 			return ctnetlink_dump_exp_ct(info->net, info->sk, skb,
3306 						     info->nlh, cda,
3307 						     info->extack);
3308 		else {
3309 			struct netlink_dump_control c = {
3310 				.dump = ctnetlink_exp_dump_table,
3311 				.done = ctnetlink_exp_done,
3312 			};
3313 			return netlink_dump_start(info->sk, skb, info->nlh, &c);
3314 		}
3315 	}
3316 
3317 	err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3318 	if (err < 0)
3319 		return err;
3320 
3321 	if (cda[CTA_EXPECT_TUPLE])
3322 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3323 					    u3, NULL);
3324 	else if (cda[CTA_EXPECT_MASTER])
3325 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3326 					    u3, NULL);
3327 	else
3328 		return -EINVAL;
3329 
3330 	if (err < 0)
3331 		return err;
3332 
3333 	exp = nf_ct_expect_find_get(info->net, &zone, &tuple);
3334 	if (!exp)
3335 		return -ENOENT;
3336 
3337 	if (cda[CTA_EXPECT_ID]) {
3338 		__be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3339 
3340 		if (id != nf_expect_get_id(exp)) {
3341 			nf_ct_expect_put(exp);
3342 			return -ENOENT;
3343 		}
3344 	}
3345 
3346 	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3347 	if (!skb2) {
3348 		nf_ct_expect_put(exp);
3349 		return -ENOMEM;
3350 	}
3351 
3352 	rcu_read_lock();
3353 	err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3354 				      info->nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
3355 				      exp);
3356 	rcu_read_unlock();
3357 	nf_ct_expect_put(exp);
3358 	if (err <= 0) {
3359 		kfree_skb(skb2);
3360 		return -ENOMEM;
3361 	}
3362 
3363 	return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
3364 }
3365 
expect_iter_name(struct nf_conntrack_expect * exp,void * data)3366 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3367 {
3368 	struct nf_conntrack_helper *helper;
3369 	const struct nf_conn_help *m_help;
3370 	const char *name = data;
3371 
3372 	m_help = nfct_help(exp->master);
3373 
3374 	helper = rcu_dereference(m_help->helper);
3375 	if (!helper)
3376 		return false;
3377 
3378 	return strcmp(helper->name, name) == 0;
3379 }
3380 
expect_iter_all(struct nf_conntrack_expect * exp,void * data)3381 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3382 {
3383 	return true;
3384 }
3385 
ctnetlink_del_expect(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])3386 static int ctnetlink_del_expect(struct sk_buff *skb,
3387 				const struct nfnl_info *info,
3388 				const struct nlattr * const cda[])
3389 {
3390 	u_int8_t u3 = info->nfmsg->nfgen_family;
3391 	struct nf_conntrack_expect *exp;
3392 	struct nf_conntrack_tuple tuple;
3393 	struct nf_conntrack_zone zone;
3394 	int err;
3395 
3396 	if (cda[CTA_EXPECT_TUPLE]) {
3397 		/* delete a single expect by tuple */
3398 		err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3399 		if (err < 0)
3400 			return err;
3401 
3402 		err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3403 					    u3, NULL);
3404 		if (err < 0)
3405 			return err;
3406 
3407 		/* bump usage count to 2 */
3408 		exp = nf_ct_expect_find_get(info->net, &zone, &tuple);
3409 		if (!exp)
3410 			return -ENOENT;
3411 
3412 		if (cda[CTA_EXPECT_ID]) {
3413 			__be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3414 			if (ntohl(id) != (u32)(unsigned long)exp) {
3415 				nf_ct_expect_put(exp);
3416 				return -ENOENT;
3417 			}
3418 		}
3419 
3420 		/* after list removal, usage count == 1 */
3421 		spin_lock_bh(&nf_conntrack_expect_lock);
3422 		if (del_timer(&exp->timeout)) {
3423 			nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3424 						   nlmsg_report(info->nlh));
3425 			nf_ct_expect_put(exp);
3426 		}
3427 		spin_unlock_bh(&nf_conntrack_expect_lock);
3428 		/* have to put what we 'get' above.
3429 		 * after this line usage count == 0 */
3430 		nf_ct_expect_put(exp);
3431 	} else if (cda[CTA_EXPECT_HELP_NAME]) {
3432 		char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3433 
3434 		nf_ct_expect_iterate_net(info->net, expect_iter_name, name,
3435 					 NETLINK_CB(skb).portid,
3436 					 nlmsg_report(info->nlh));
3437 	} else {
3438 		/* This basically means we have to flush everything*/
3439 		nf_ct_expect_iterate_net(info->net, expect_iter_all, NULL,
3440 					 NETLINK_CB(skb).portid,
3441 					 nlmsg_report(info->nlh));
3442 	}
3443 
3444 	return 0;
3445 }
3446 static int
ctnetlink_change_expect(struct nf_conntrack_expect * x,const struct nlattr * const cda[])3447 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3448 			const struct nlattr * const cda[])
3449 {
3450 	if (cda[CTA_EXPECT_TIMEOUT]) {
3451 		if (!del_timer(&x->timeout))
3452 			return -ETIME;
3453 
3454 		x->timeout.expires = jiffies +
3455 			ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3456 		add_timer(&x->timeout);
3457 	}
3458 	return 0;
3459 }
3460 
3461 #if IS_ENABLED(CONFIG_NF_NAT)
3462 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3463 	[CTA_EXPECT_NAT_DIR]	= { .type = NLA_U32 },
3464 	[CTA_EXPECT_NAT_TUPLE]	= { .type = NLA_NESTED },
3465 };
3466 #endif
3467 
3468 static int
ctnetlink_parse_expect_nat(const struct nlattr * attr,struct nf_conntrack_expect * exp,u_int8_t u3)3469 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3470 			   struct nf_conntrack_expect *exp,
3471 			   u_int8_t u3)
3472 {
3473 #if IS_ENABLED(CONFIG_NF_NAT)
3474 	struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3475 	struct nf_conntrack_tuple nat_tuple = {};
3476 	int err;
3477 
3478 	err = nla_parse_nested_deprecated(tb, CTA_EXPECT_NAT_MAX, attr,
3479 					  exp_nat_nla_policy, NULL);
3480 	if (err < 0)
3481 		return err;
3482 
3483 	if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3484 		return -EINVAL;
3485 
3486 	err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3487 				    &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3488 				    u3, NULL);
3489 	if (err < 0)
3490 		return err;
3491 
3492 	exp->saved_addr = nat_tuple.src.u3;
3493 	exp->saved_proto = nat_tuple.src.u;
3494 	exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3495 
3496 	return 0;
3497 #else
3498 	return -EOPNOTSUPP;
3499 #endif
3500 }
3501 
3502 static struct nf_conntrack_expect *
ctnetlink_alloc_expect(const struct nlattr * const cda[],struct nf_conn * ct,struct nf_conntrack_helper * helper,struct nf_conntrack_tuple * tuple,struct nf_conntrack_tuple * mask)3503 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3504 		       struct nf_conntrack_helper *helper,
3505 		       struct nf_conntrack_tuple *tuple,
3506 		       struct nf_conntrack_tuple *mask)
3507 {
3508 	u_int32_t class = 0;
3509 	struct nf_conntrack_expect *exp;
3510 	struct nf_conn_help *help;
3511 	int err;
3512 
3513 	help = nfct_help(ct);
3514 	if (!help)
3515 		return ERR_PTR(-EOPNOTSUPP);
3516 
3517 	if (cda[CTA_EXPECT_CLASS] && helper) {
3518 		class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3519 		if (class > helper->expect_class_max)
3520 			return ERR_PTR(-EINVAL);
3521 	}
3522 	exp = nf_ct_expect_alloc(ct);
3523 	if (!exp)
3524 		return ERR_PTR(-ENOMEM);
3525 
3526 	if (cda[CTA_EXPECT_FLAGS]) {
3527 		exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3528 		exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3529 	} else {
3530 		exp->flags = 0;
3531 	}
3532 	if (cda[CTA_EXPECT_FN]) {
3533 		const char *name = nla_data(cda[CTA_EXPECT_FN]);
3534 		struct nf_ct_helper_expectfn *expfn;
3535 
3536 		expfn = nf_ct_helper_expectfn_find_by_name(name);
3537 		if (expfn == NULL) {
3538 			err = -EINVAL;
3539 			goto err_out;
3540 		}
3541 		exp->expectfn = expfn->expectfn;
3542 	} else
3543 		exp->expectfn = NULL;
3544 
3545 	exp->class = class;
3546 	exp->master = ct;
3547 	exp->helper = helper;
3548 	exp->tuple = *tuple;
3549 	exp->mask.src.u3 = mask->src.u3;
3550 	exp->mask.src.u.all = mask->src.u.all;
3551 
3552 	if (cda[CTA_EXPECT_NAT]) {
3553 		err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3554 						 exp, nf_ct_l3num(ct));
3555 		if (err < 0)
3556 			goto err_out;
3557 	}
3558 	return exp;
3559 err_out:
3560 	nf_ct_expect_put(exp);
3561 	return ERR_PTR(err);
3562 }
3563 
3564 static int
ctnetlink_create_expect(struct net * net,const struct nf_conntrack_zone * zone,const struct nlattr * const cda[],u_int8_t u3,u32 portid,int report)3565 ctnetlink_create_expect(struct net *net,
3566 			const struct nf_conntrack_zone *zone,
3567 			const struct nlattr * const cda[],
3568 			u_int8_t u3, u32 portid, int report)
3569 {
3570 	struct nf_conntrack_tuple tuple, mask, master_tuple;
3571 	struct nf_conntrack_tuple_hash *h = NULL;
3572 	struct nf_conntrack_helper *helper = NULL;
3573 	struct nf_conntrack_expect *exp;
3574 	struct nf_conn *ct;
3575 	int err;
3576 
3577 	/* caller guarantees that those three CTA_EXPECT_* exist */
3578 	err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3579 				    u3, NULL);
3580 	if (err < 0)
3581 		return err;
3582 	err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3583 				    u3, NULL);
3584 	if (err < 0)
3585 		return err;
3586 	err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3587 				    u3, NULL);
3588 	if (err < 0)
3589 		return err;
3590 
3591 	/* Look for master conntrack of this expectation */
3592 	h = nf_conntrack_find_get(net, zone, &master_tuple);
3593 	if (!h)
3594 		return -ENOENT;
3595 	ct = nf_ct_tuplehash_to_ctrack(h);
3596 
3597 	rcu_read_lock();
3598 	if (cda[CTA_EXPECT_HELP_NAME]) {
3599 		const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3600 
3601 		helper = __nf_conntrack_helper_find(helpname, u3,
3602 						    nf_ct_protonum(ct));
3603 		if (helper == NULL) {
3604 			rcu_read_unlock();
3605 #ifdef CONFIG_MODULES
3606 			if (request_module("nfct-helper-%s", helpname) < 0) {
3607 				err = -EOPNOTSUPP;
3608 				goto err_ct;
3609 			}
3610 			rcu_read_lock();
3611 			helper = __nf_conntrack_helper_find(helpname, u3,
3612 							    nf_ct_protonum(ct));
3613 			if (helper) {
3614 				err = -EAGAIN;
3615 				goto err_rcu;
3616 			}
3617 			rcu_read_unlock();
3618 #endif
3619 			err = -EOPNOTSUPP;
3620 			goto err_ct;
3621 		}
3622 	}
3623 
3624 	exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3625 	if (IS_ERR(exp)) {
3626 		err = PTR_ERR(exp);
3627 		goto err_rcu;
3628 	}
3629 
3630 	err = nf_ct_expect_related_report(exp, portid, report, 0);
3631 	nf_ct_expect_put(exp);
3632 err_rcu:
3633 	rcu_read_unlock();
3634 err_ct:
3635 	nf_ct_put(ct);
3636 	return err;
3637 }
3638 
ctnetlink_new_expect(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])3639 static int ctnetlink_new_expect(struct sk_buff *skb,
3640 				const struct nfnl_info *info,
3641 				const struct nlattr * const cda[])
3642 {
3643 	u_int8_t u3 = info->nfmsg->nfgen_family;
3644 	struct nf_conntrack_tuple tuple;
3645 	struct nf_conntrack_expect *exp;
3646 	struct nf_conntrack_zone zone;
3647 	int err;
3648 
3649 	if (!cda[CTA_EXPECT_TUPLE]
3650 	    || !cda[CTA_EXPECT_MASK]
3651 	    || !cda[CTA_EXPECT_MASTER])
3652 		return -EINVAL;
3653 
3654 	err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3655 	if (err < 0)
3656 		return err;
3657 
3658 	err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3659 				    u3, NULL);
3660 	if (err < 0)
3661 		return err;
3662 
3663 	spin_lock_bh(&nf_conntrack_expect_lock);
3664 	exp = __nf_ct_expect_find(info->net, &zone, &tuple);
3665 	if (!exp) {
3666 		spin_unlock_bh(&nf_conntrack_expect_lock);
3667 		err = -ENOENT;
3668 		if (info->nlh->nlmsg_flags & NLM_F_CREATE) {
3669 			err = ctnetlink_create_expect(info->net, &zone, cda, u3,
3670 						      NETLINK_CB(skb).portid,
3671 						      nlmsg_report(info->nlh));
3672 		}
3673 		return err;
3674 	}
3675 
3676 	err = -EEXIST;
3677 	if (!(info->nlh->nlmsg_flags & NLM_F_EXCL))
3678 		err = ctnetlink_change_expect(exp, cda);
3679 	spin_unlock_bh(&nf_conntrack_expect_lock);
3680 
3681 	return err;
3682 }
3683 
3684 static int
ctnetlink_exp_stat_fill_info(struct sk_buff * skb,u32 portid,u32 seq,int cpu,const struct ip_conntrack_stat * st)3685 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3686 			     const struct ip_conntrack_stat *st)
3687 {
3688 	struct nlmsghdr *nlh;
3689 	unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3690 
3691 	event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3692 			      IPCTNL_MSG_EXP_GET_STATS_CPU);
3693 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
3694 			   NFNETLINK_V0, htons(cpu));
3695 	if (!nlh)
3696 		goto nlmsg_failure;
3697 
3698 	if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3699 	    nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3700 	    nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3701 		goto nla_put_failure;
3702 
3703 	nlmsg_end(skb, nlh);
3704 	return skb->len;
3705 
3706 nla_put_failure:
3707 nlmsg_failure:
3708 	nlmsg_cancel(skb, nlh);
3709 	return -1;
3710 }
3711 
3712 static int
ctnetlink_exp_stat_cpu_dump(struct sk_buff * skb,struct netlink_callback * cb)3713 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3714 {
3715 	int cpu;
3716 	struct net *net = sock_net(skb->sk);
3717 
3718 	if (cb->args[0] == nr_cpu_ids)
3719 		return 0;
3720 
3721 	for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3722 		const struct ip_conntrack_stat *st;
3723 
3724 		if (!cpu_possible(cpu))
3725 			continue;
3726 
3727 		st = per_cpu_ptr(net->ct.stat, cpu);
3728 		if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3729 						 cb->nlh->nlmsg_seq,
3730 						 cpu, st) < 0)
3731 			break;
3732 	}
3733 	cb->args[0] = cpu;
3734 
3735 	return skb->len;
3736 }
3737 
ctnetlink_stat_exp_cpu(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const cda[])3738 static int ctnetlink_stat_exp_cpu(struct sk_buff *skb,
3739 				  const struct nfnl_info *info,
3740 				  const struct nlattr * const cda[])
3741 {
3742 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3743 		struct netlink_dump_control c = {
3744 			.dump = ctnetlink_exp_stat_cpu_dump,
3745 		};
3746 		return netlink_dump_start(info->sk, skb, info->nlh, &c);
3747 	}
3748 
3749 	return 0;
3750 }
3751 
3752 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3753 static struct nf_ct_event_notifier ctnl_notifier = {
3754 	.ct_event = ctnetlink_conntrack_event,
3755 	.exp_event = ctnetlink_expect_event,
3756 };
3757 #endif
3758 
3759 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3760 	[IPCTNL_MSG_CT_NEW]	= {
3761 		.call		= ctnetlink_new_conntrack,
3762 		.type		= NFNL_CB_MUTEX,
3763 		.attr_count	= CTA_MAX,
3764 		.policy		= ct_nla_policy
3765 	},
3766 	[IPCTNL_MSG_CT_GET]	= {
3767 		.call		= ctnetlink_get_conntrack,
3768 		.type		= NFNL_CB_MUTEX,
3769 		.attr_count	= CTA_MAX,
3770 		.policy		= ct_nla_policy
3771 	},
3772 	[IPCTNL_MSG_CT_DELETE]	= {
3773 		.call		= ctnetlink_del_conntrack,
3774 		.type		= NFNL_CB_MUTEX,
3775 		.attr_count	= CTA_MAX,
3776 		.policy		= ct_nla_policy
3777 	},
3778 	[IPCTNL_MSG_CT_GET_CTRZERO] = {
3779 		.call		= ctnetlink_get_conntrack,
3780 		.type		= NFNL_CB_MUTEX,
3781 		.attr_count	= CTA_MAX,
3782 		.policy		= ct_nla_policy
3783 	},
3784 	[IPCTNL_MSG_CT_GET_STATS_CPU] = {
3785 		.call		= ctnetlink_stat_ct_cpu,
3786 		.type		= NFNL_CB_MUTEX,
3787 	},
3788 	[IPCTNL_MSG_CT_GET_STATS] = {
3789 		.call		= ctnetlink_stat_ct,
3790 		.type		= NFNL_CB_MUTEX,
3791 	},
3792 	[IPCTNL_MSG_CT_GET_DYING] = {
3793 		.call		= ctnetlink_get_ct_dying,
3794 		.type		= NFNL_CB_MUTEX,
3795 	},
3796 	[IPCTNL_MSG_CT_GET_UNCONFIRMED]	= {
3797 		.call		= ctnetlink_get_ct_unconfirmed,
3798 		.type		= NFNL_CB_MUTEX,
3799 	},
3800 };
3801 
3802 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3803 	[IPCTNL_MSG_EXP_GET] = {
3804 		.call		= ctnetlink_get_expect,
3805 		.type		= NFNL_CB_MUTEX,
3806 		.attr_count	= CTA_EXPECT_MAX,
3807 		.policy		= exp_nla_policy
3808 	},
3809 	[IPCTNL_MSG_EXP_NEW] = {
3810 		.call		= ctnetlink_new_expect,
3811 		.type		= NFNL_CB_MUTEX,
3812 		.attr_count	= CTA_EXPECT_MAX,
3813 		.policy		= exp_nla_policy
3814 	},
3815 	[IPCTNL_MSG_EXP_DELETE] = {
3816 		.call		= ctnetlink_del_expect,
3817 		.type		= NFNL_CB_MUTEX,
3818 		.attr_count	= CTA_EXPECT_MAX,
3819 		.policy		= exp_nla_policy
3820 	},
3821 	[IPCTNL_MSG_EXP_GET_STATS_CPU] = {
3822 		.call		= ctnetlink_stat_exp_cpu,
3823 		.type		= NFNL_CB_MUTEX,
3824 	},
3825 };
3826 
3827 static const struct nfnetlink_subsystem ctnl_subsys = {
3828 	.name				= "conntrack",
3829 	.subsys_id			= NFNL_SUBSYS_CTNETLINK,
3830 	.cb_count			= IPCTNL_MSG_MAX,
3831 	.cb				= ctnl_cb,
3832 };
3833 
3834 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3835 	.name				= "conntrack_expect",
3836 	.subsys_id			= NFNL_SUBSYS_CTNETLINK_EXP,
3837 	.cb_count			= IPCTNL_MSG_EXP_MAX,
3838 	.cb				= ctnl_exp_cb,
3839 };
3840 
3841 MODULE_ALIAS("ip_conntrack_netlink");
3842 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3843 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3844 
ctnetlink_net_init(struct net * net)3845 static int __net_init ctnetlink_net_init(struct net *net)
3846 {
3847 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3848 	nf_conntrack_register_notifier(net, &ctnl_notifier);
3849 #endif
3850 	return 0;
3851 }
3852 
ctnetlink_net_pre_exit(struct net * net)3853 static void ctnetlink_net_pre_exit(struct net *net)
3854 {
3855 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3856 	nf_conntrack_unregister_notifier(net);
3857 #endif
3858 }
3859 
3860 static struct pernet_operations ctnetlink_net_ops = {
3861 	.init		= ctnetlink_net_init,
3862 	.pre_exit	= ctnetlink_net_pre_exit,
3863 };
3864 
ctnetlink_init(void)3865 static int __init ctnetlink_init(void)
3866 {
3867 	int ret;
3868 
3869 	NL_ASSERT_DUMP_CTX_FITS(struct ctnetlink_list_dump_ctx);
3870 
3871 	ret = nfnetlink_subsys_register(&ctnl_subsys);
3872 	if (ret < 0) {
3873 		pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3874 		goto err_out;
3875 	}
3876 
3877 	ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3878 	if (ret < 0) {
3879 		pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3880 		goto err_unreg_subsys;
3881 	}
3882 
3883 	ret = register_pernet_subsys(&ctnetlink_net_ops);
3884 	if (ret < 0) {
3885 		pr_err("ctnetlink_init: cannot register pernet operations\n");
3886 		goto err_unreg_exp_subsys;
3887 	}
3888 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3889 	/* setup interaction between nf_queue and nf_conntrack_netlink. */
3890 	RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3891 #endif
3892 	return 0;
3893 
3894 err_unreg_exp_subsys:
3895 	nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3896 err_unreg_subsys:
3897 	nfnetlink_subsys_unregister(&ctnl_subsys);
3898 err_out:
3899 	return ret;
3900 }
3901 
ctnetlink_exit(void)3902 static void __exit ctnetlink_exit(void)
3903 {
3904 	unregister_pernet_subsys(&ctnetlink_net_ops);
3905 	nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3906 	nfnetlink_subsys_unregister(&ctnl_subsys);
3907 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3908 	RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3909 #endif
3910 	synchronize_rcu();
3911 }
3912 
3913 module_init(ctnetlink_init);
3914 module_exit(ctnetlink_exit);
3915