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