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