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