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