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