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