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