1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
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>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #include <net/netfilter/nf_conntrack_timestamp.h>
46 #include <net/netfilter/nf_conntrack_labels.h>
47 #include <net/netfilter/nf_conntrack_synproxy.h>
48 #ifdef CONFIG_NF_NAT_NEEDED
49 #include <net/netfilter/nf_nat_core.h>
50 #include <net/netfilter/nf_nat_helper.h>
53 #include <linux/netfilter/nfnetlink.h>
54 #include <linux/netfilter/nfnetlink_conntrack.h>
56 MODULE_LICENSE("GPL");
58 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
59 const struct nf_conntrack_tuple *tuple,
60 const struct nf_conntrack_l4proto *l4proto)
63 struct nlattr *nest_parms;
65 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
68 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
71 if (likely(l4proto->tuple_to_nlattr))
72 ret = l4proto->tuple_to_nlattr(skb, tuple);
74 nla_nest_end(skb, nest_parms);
82 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
83 const struct nf_conntrack_tuple *tuple)
85 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
86 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
91 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
92 const struct nf_conntrack_tuple *tuple)
94 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
95 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
100 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
101 const struct nf_conntrack_tuple *tuple)
104 struct nlattr *nest_parms;
106 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
108 goto nla_put_failure;
110 switch (tuple->src.l3num) {
112 ret = ipv4_tuple_to_nlattr(skb, tuple);
115 ret = ipv6_tuple_to_nlattr(skb, tuple);
119 nla_nest_end(skb, nest_parms);
127 static int ctnetlink_dump_tuples(struct sk_buff *skb,
128 const struct nf_conntrack_tuple *tuple)
130 const struct nf_conntrack_l4proto *l4proto;
134 ret = ctnetlink_dump_tuples_ip(skb, tuple);
137 l4proto = __nf_ct_l4proto_find(tuple->dst.protonum);
138 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
144 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
145 const struct nf_conntrack_zone *zone, int dir)
147 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
149 if (nla_put_be16(skb, attrtype, htons(zone->id)))
150 goto nla_put_failure;
157 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
159 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
160 goto nla_put_failure;
167 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
169 long timeout = nf_ct_expires(ct) / HZ;
171 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
172 goto nla_put_failure;
179 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
181 const struct nf_conntrack_l4proto *l4proto;
182 struct nlattr *nest_proto;
185 l4proto = __nf_ct_l4proto_find(nf_ct_protonum(ct));
186 if (!l4proto->to_nlattr)
189 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
191 goto nla_put_failure;
193 ret = l4proto->to_nlattr(skb, nest_proto, ct);
195 nla_nest_end(skb, nest_proto);
203 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
204 const struct nf_conn *ct)
206 struct nlattr *nest_helper;
207 const struct nf_conn_help *help = nfct_help(ct);
208 struct nf_conntrack_helper *helper;
213 helper = rcu_dereference(help->helper);
217 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
219 goto nla_put_failure;
220 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
221 goto nla_put_failure;
223 if (helper->to_nlattr)
224 helper->to_nlattr(skb, ct);
226 nla_nest_end(skb, nest_helper);
235 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
236 enum ip_conntrack_dir dir, int type)
238 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
239 struct nf_conn_counter *counter = acct->counter;
240 struct nlattr *nest_count;
243 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
244 pkts = atomic64_xchg(&counter[dir].packets, 0);
245 bytes = atomic64_xchg(&counter[dir].bytes, 0);
247 pkts = atomic64_read(&counter[dir].packets);
248 bytes = atomic64_read(&counter[dir].bytes);
251 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
253 goto nla_put_failure;
255 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
257 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
259 goto nla_put_failure;
261 nla_nest_end(skb, nest_count);
270 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
272 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
277 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
279 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
286 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
288 struct nlattr *nest_count;
289 const struct nf_conn_tstamp *tstamp;
291 tstamp = nf_conn_tstamp_find(ct);
295 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
297 goto nla_put_failure;
299 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
300 CTA_TIMESTAMP_PAD) ||
301 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
302 cpu_to_be64(tstamp->stop),
304 goto nla_put_failure;
305 nla_nest_end(skb, nest_count);
313 #ifdef CONFIG_NF_CONNTRACK_MARK
314 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
316 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
317 goto nla_put_failure;
324 #define ctnetlink_dump_mark(a, b) (0)
327 #ifdef CONFIG_NF_CONNTRACK_SECMARK
328 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
330 struct nlattr *nest_secctx;
334 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
339 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
341 goto nla_put_failure;
343 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
344 goto nla_put_failure;
345 nla_nest_end(skb, nest_secctx);
349 security_release_secctx(secctx, len);
353 #define ctnetlink_dump_secctx(a, b) (0)
356 #ifdef CONFIG_NF_CONNTRACK_LABELS
357 static inline int ctnetlink_label_size(const struct nf_conn *ct)
359 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
363 return nla_total_size(sizeof(labels->bits));
367 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
369 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
377 if (labels->bits[i] != 0)
378 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
381 } while (i < ARRAY_SIZE(labels->bits));
386 #define ctnetlink_dump_labels(a, b) (0)
387 #define ctnetlink_label_size(a) (0)
390 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
392 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
394 struct nlattr *nest_parms;
396 if (!(ct->status & IPS_EXPECTED))
399 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
401 goto nla_put_failure;
402 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
403 goto nla_put_failure;
404 nla_nest_end(skb, nest_parms);
413 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
415 struct nlattr *nest_parms;
417 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
419 goto nla_put_failure;
421 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
422 htonl(seq->correction_pos)) ||
423 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
424 htonl(seq->offset_before)) ||
425 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
426 htonl(seq->offset_after)))
427 goto nla_put_failure;
429 nla_nest_end(skb, nest_parms);
437 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
439 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
440 struct nf_ct_seqadj *seq;
442 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
445 spin_lock_bh(&ct->lock);
446 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
447 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
450 seq = &seqadj->seq[IP_CT_DIR_REPLY];
451 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
454 spin_unlock_bh(&ct->lock);
457 spin_unlock_bh(&ct->lock);
461 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
463 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
464 struct nlattr *nest_parms;
469 nest_parms = nla_nest_start(skb, CTA_SYNPROXY | NLA_F_NESTED);
471 goto nla_put_failure;
473 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
474 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
475 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
476 goto nla_put_failure;
478 nla_nest_end(skb, nest_parms);
486 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
488 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
489 goto nla_put_failure;
496 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
498 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
499 goto nla_put_failure;
507 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
510 const struct nf_conntrack_zone *zone;
511 struct nlmsghdr *nlh;
512 struct nfgenmsg *nfmsg;
513 struct nlattr *nest_parms;
514 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
516 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
517 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
521 nfmsg = nlmsg_data(nlh);
522 nfmsg->nfgen_family = nf_ct_l3num(ct);
523 nfmsg->version = NFNETLINK_V0;
526 zone = nf_ct_zone(ct);
528 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
530 goto nla_put_failure;
531 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
532 goto nla_put_failure;
533 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
534 NF_CT_ZONE_DIR_ORIG) < 0)
535 goto nla_put_failure;
536 nla_nest_end(skb, nest_parms);
538 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
540 goto nla_put_failure;
541 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
542 goto nla_put_failure;
543 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
544 NF_CT_ZONE_DIR_REPL) < 0)
545 goto nla_put_failure;
546 nla_nest_end(skb, nest_parms);
548 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
549 NF_CT_DEFAULT_ZONE_DIR) < 0)
550 goto nla_put_failure;
552 if (ctnetlink_dump_status(skb, ct) < 0 ||
553 ctnetlink_dump_timeout(skb, ct) < 0 ||
554 ctnetlink_dump_acct(skb, ct, type) < 0 ||
555 ctnetlink_dump_timestamp(skb, ct) < 0 ||
556 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
557 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
558 ctnetlink_dump_mark(skb, ct) < 0 ||
559 ctnetlink_dump_secctx(skb, ct) < 0 ||
560 ctnetlink_dump_labels(skb, ct) < 0 ||
561 ctnetlink_dump_id(skb, ct) < 0 ||
562 ctnetlink_dump_use(skb, ct) < 0 ||
563 ctnetlink_dump_master(skb, ct) < 0 ||
564 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
565 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
566 goto nla_put_failure;
573 nlmsg_cancel(skb, nlh);
577 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
578 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
579 [CTA_IP_V4_DST] = { .type = NLA_U32 },
580 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 },
581 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 },
584 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
585 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
587 const struct nf_conntrack_l4proto *l4proto;
588 size_t len, len4 = 0;
590 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
591 len *= 3u; /* ORIG, REPLY, MASTER */
593 l4proto = __nf_ct_l4proto_find(nf_ct_protonum(ct));
594 len += l4proto->nlattr_size;
595 if (l4proto->nlattr_tuple_size) {
596 len4 = l4proto->nlattr_tuple_size();
597 len4 *= 3u; /* ORIG, REPLY, MASTER */
604 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
606 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
608 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
609 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
610 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
614 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
616 #ifdef CONFIG_NF_CONNTRACK_SECMARK
619 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
623 return nla_total_size(0) /* CTA_SECCTX */
624 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
630 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
632 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
633 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
635 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
641 #ifdef CONFIG_NF_CONNTRACK_EVENTS
642 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
644 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
645 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
646 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
647 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
648 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
649 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
650 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
651 + ctnetlink_acct_size(ct)
652 + ctnetlink_timestamp_size(ct)
653 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
654 + nla_total_size(0) /* CTA_PROTOINFO */
655 + nla_total_size(0) /* CTA_HELP */
656 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
657 + ctnetlink_secctx_size(ct)
658 #ifdef CONFIG_NF_NAT_NEEDED
659 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
660 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
662 #ifdef CONFIG_NF_CONNTRACK_MARK
663 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
665 #ifdef CONFIG_NF_CONNTRACK_ZONES
666 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
668 + ctnetlink_proto_size(ct)
669 + ctnetlink_label_size(ct)
674 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
676 const struct nf_conntrack_zone *zone;
678 struct nlmsghdr *nlh;
679 struct nfgenmsg *nfmsg;
680 struct nlattr *nest_parms;
681 struct nf_conn *ct = item->ct;
684 unsigned int flags = 0, group;
687 if (events & (1 << IPCT_DESTROY)) {
688 type = IPCTNL_MSG_CT_DELETE;
689 group = NFNLGRP_CONNTRACK_DESTROY;
690 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
691 type = IPCTNL_MSG_CT_NEW;
692 flags = NLM_F_CREATE|NLM_F_EXCL;
693 group = NFNLGRP_CONNTRACK_NEW;
695 type = IPCTNL_MSG_CT_NEW;
696 group = NFNLGRP_CONNTRACK_UPDATE;
701 if (!item->report && !nfnetlink_has_listeners(net, group))
704 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
708 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
709 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
713 nfmsg = nlmsg_data(nlh);
714 nfmsg->nfgen_family = nf_ct_l3num(ct);
715 nfmsg->version = NFNETLINK_V0;
718 zone = nf_ct_zone(ct);
720 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
722 goto nla_put_failure;
723 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
724 goto nla_put_failure;
725 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
726 NF_CT_ZONE_DIR_ORIG) < 0)
727 goto nla_put_failure;
728 nla_nest_end(skb, nest_parms);
730 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
732 goto nla_put_failure;
733 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
734 goto nla_put_failure;
735 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
736 NF_CT_ZONE_DIR_REPL) < 0)
737 goto nla_put_failure;
738 nla_nest_end(skb, nest_parms);
740 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
741 NF_CT_DEFAULT_ZONE_DIR) < 0)
742 goto nla_put_failure;
744 if (ctnetlink_dump_id(skb, ct) < 0)
745 goto nla_put_failure;
747 if (ctnetlink_dump_status(skb, ct) < 0)
748 goto nla_put_failure;
750 if (events & (1 << IPCT_DESTROY)) {
751 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
752 ctnetlink_dump_timestamp(skb, ct) < 0)
753 goto nla_put_failure;
755 if (ctnetlink_dump_timeout(skb, ct) < 0)
756 goto nla_put_failure;
758 if (events & (1 << IPCT_PROTOINFO)
759 && ctnetlink_dump_protoinfo(skb, ct) < 0)
760 goto nla_put_failure;
762 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
763 && ctnetlink_dump_helpinfo(skb, ct) < 0)
764 goto nla_put_failure;
766 #ifdef CONFIG_NF_CONNTRACK_SECMARK
767 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
768 && ctnetlink_dump_secctx(skb, ct) < 0)
769 goto nla_put_failure;
771 if (events & (1 << IPCT_LABEL) &&
772 ctnetlink_dump_labels(skb, ct) < 0)
773 goto nla_put_failure;
775 if (events & (1 << IPCT_RELATED) &&
776 ctnetlink_dump_master(skb, ct) < 0)
777 goto nla_put_failure;
779 if (events & (1 << IPCT_SEQADJ) &&
780 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
781 goto nla_put_failure;
783 if (events & (1 << IPCT_SYNPROXY) &&
784 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
785 goto nla_put_failure;
788 #ifdef CONFIG_NF_CONNTRACK_MARK
789 if ((events & (1 << IPCT_MARK) || ct->mark)
790 && ctnetlink_dump_mark(skb, ct) < 0)
791 goto nla_put_failure;
794 err = nfnetlink_send(skb, net, item->portid, group, item->report,
796 if (err == -ENOBUFS || err == -EAGAIN)
802 nlmsg_cancel(skb, nlh);
806 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
811 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
813 static int ctnetlink_done(struct netlink_callback *cb)
816 nf_ct_put((struct nf_conn *)cb->args[1]);
821 struct ctnetlink_filter {
829 static struct ctnetlink_filter *
830 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
832 struct ctnetlink_filter *filter;
834 #ifndef CONFIG_NF_CONNTRACK_MARK
835 if (cda[CTA_MARK] && cda[CTA_MARK_MASK])
836 return ERR_PTR(-EOPNOTSUPP);
839 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
841 return ERR_PTR(-ENOMEM);
843 filter->family = family;
845 #ifdef CONFIG_NF_CONNTRACK_MARK
846 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
847 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
848 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
854 static int ctnetlink_start(struct netlink_callback *cb)
856 const struct nlattr * const *cda = cb->data;
857 struct ctnetlink_filter *filter = NULL;
858 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
859 u8 family = nfmsg->nfgen_family;
861 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
862 filter = ctnetlink_alloc_filter(cda, family);
864 return PTR_ERR(filter);
871 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
873 struct ctnetlink_filter *filter = data;
878 /* Match entries of a given L3 protocol number.
879 * If it is not specified, ie. l3proto == 0,
880 * then match everything.
882 if (filter->family && nf_ct_l3num(ct) != filter->family)
885 #ifdef CONFIG_NF_CONNTRACK_MARK
886 if ((ct->mark & filter->mark.mask) != filter->mark.val)
898 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
900 struct net *net = sock_net(skb->sk);
901 struct nf_conn *ct, *last;
902 struct nf_conntrack_tuple_hash *h;
903 struct hlist_nulls_node *n;
904 struct nf_conn *nf_ct_evict[8];
908 last = (struct nf_conn *)cb->args[1];
912 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
916 if (nf_ct_should_gc(nf_ct_evict[i]))
917 nf_ct_kill(nf_ct_evict[i]);
918 nf_ct_put(nf_ct_evict[i]);
921 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
922 nf_conntrack_lock(lockp);
923 if (cb->args[0] >= nf_conntrack_htable_size) {
927 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
929 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
931 ct = nf_ct_tuplehash_to_ctrack(h);
932 if (nf_ct_is_expired(ct)) {
933 if (i < ARRAY_SIZE(nf_ct_evict) &&
934 atomic_inc_not_zero(&ct->ct_general.use))
935 nf_ct_evict[i++] = ct;
939 if (!net_eq(net, nf_ct_net(ct)))
947 if (!ctnetlink_filter_match(ct, cb->data))
952 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
954 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
958 nf_conntrack_get(&ct->ct_general);
959 cb->args[1] = (unsigned long)ct;
973 /* nf ct hash resize happened, now clear the leftover. */
974 if ((struct nf_conn *)cb->args[1] == last)
982 if (nf_ct_should_gc(nf_ct_evict[i]))
983 nf_ct_kill(nf_ct_evict[i]);
984 nf_ct_put(nf_ct_evict[i]);
990 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
991 struct nf_conntrack_tuple *t)
993 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
996 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
997 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1002 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1003 struct nf_conntrack_tuple *t)
1005 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
1008 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1009 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1014 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1015 struct nf_conntrack_tuple *tuple)
1017 struct nlattr *tb[CTA_IP_MAX+1];
1020 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
1024 ret = nla_validate_nested(attr, CTA_IP_MAX,
1025 cta_ip_nla_policy, NULL);
1029 switch (tuple->src.l3num) {
1031 ret = ipv4_nlattr_to_tuple(tb, tuple);
1034 ret = ipv6_nlattr_to_tuple(tb, tuple);
1041 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1042 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1045 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1046 struct nf_conntrack_tuple *tuple)
1048 const struct nf_conntrack_l4proto *l4proto;
1049 struct nlattr *tb[CTA_PROTO_MAX+1];
1052 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
1057 if (!tb[CTA_PROTO_NUM])
1059 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1062 l4proto = __nf_ct_l4proto_find(tuple->dst.protonum);
1064 if (likely(l4proto->nlattr_to_tuple)) {
1065 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
1066 l4proto->nla_policy, NULL);
1068 ret = l4proto->nlattr_to_tuple(tb, tuple);
1077 ctnetlink_parse_zone(const struct nlattr *attr,
1078 struct nf_conntrack_zone *zone)
1080 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1081 NF_CT_DEFAULT_ZONE_DIR, 0);
1082 #ifdef CONFIG_NF_CONNTRACK_ZONES
1084 zone->id = ntohs(nla_get_be16(attr));
1093 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1094 struct nf_conntrack_zone *zone)
1098 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1101 ret = ctnetlink_parse_zone(attr, zone);
1105 if (type == CTA_TUPLE_REPLY)
1106 zone->dir = NF_CT_ZONE_DIR_REPL;
1108 zone->dir = NF_CT_ZONE_DIR_ORIG;
1113 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1114 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1115 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1116 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1120 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1121 struct nf_conntrack_tuple *tuple, u32 type,
1122 u_int8_t l3num, struct nf_conntrack_zone *zone)
1124 struct nlattr *tb[CTA_TUPLE_MAX+1];
1127 memset(tuple, 0, sizeof(*tuple));
1129 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1134 if (!tb[CTA_TUPLE_IP])
1137 tuple->src.l3num = l3num;
1139 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1143 if (!tb[CTA_TUPLE_PROTO])
1146 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1150 if (tb[CTA_TUPLE_ZONE]) {
1154 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1160 /* orig and expect tuples get DIR_ORIGINAL */
1161 if (type == CTA_TUPLE_REPLY)
1162 tuple->dst.dir = IP_CT_DIR_REPLY;
1164 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1169 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1170 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1171 .len = NF_CT_HELPER_NAME_LEN - 1 },
1174 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1175 struct nlattr **helpinfo)
1178 struct nlattr *tb[CTA_HELP_MAX+1];
1180 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1184 if (!tb[CTA_HELP_NAME])
1187 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1189 if (tb[CTA_HELP_INFO])
1190 *helpinfo = tb[CTA_HELP_INFO];
1195 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1196 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1197 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1198 [CTA_STATUS] = { .type = NLA_U32 },
1199 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1200 [CTA_HELP] = { .type = NLA_NESTED },
1201 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1202 [CTA_TIMEOUT] = { .type = NLA_U32 },
1203 [CTA_MARK] = { .type = NLA_U32 },
1204 [CTA_ID] = { .type = NLA_U32 },
1205 [CTA_NAT_DST] = { .type = NLA_NESTED },
1206 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1207 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1208 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1209 [CTA_ZONE] = { .type = NLA_U16 },
1210 [CTA_MARK_MASK] = { .type = NLA_U32 },
1211 [CTA_LABELS] = { .type = NLA_BINARY,
1212 .len = NF_CT_LABELS_MAX_SIZE },
1213 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1214 .len = NF_CT_LABELS_MAX_SIZE },
1217 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1219 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1222 return ctnetlink_filter_match(ct, data);
1225 static int ctnetlink_flush_conntrack(struct net *net,
1226 const struct nlattr * const cda[],
1227 u32 portid, int report, u8 family)
1229 struct ctnetlink_filter *filter = NULL;
1231 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
1232 filter = ctnetlink_alloc_filter(cda, family);
1234 return PTR_ERR(filter);
1237 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1244 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1245 struct sk_buff *skb,
1246 const struct nlmsghdr *nlh,
1247 const struct nlattr * const cda[],
1248 struct netlink_ext_ack *extack)
1250 struct nf_conntrack_tuple_hash *h;
1251 struct nf_conntrack_tuple tuple;
1253 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1254 u_int8_t u3 = nfmsg->nfgen_family;
1255 struct nf_conntrack_zone zone;
1258 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1262 if (cda[CTA_TUPLE_ORIG])
1263 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1265 else if (cda[CTA_TUPLE_REPLY])
1266 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1269 return ctnetlink_flush_conntrack(net, cda,
1270 NETLINK_CB(skb).portid,
1271 nlmsg_report(nlh), u3);
1277 h = nf_conntrack_find_get(net, &zone, &tuple);
1281 ct = nf_ct_tuplehash_to_ctrack(h);
1283 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1289 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1290 if (id != (u32)(unsigned long)ct) {
1296 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1302 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1303 struct sk_buff *skb,
1304 const struct nlmsghdr *nlh,
1305 const struct nlattr * const cda[],
1306 struct netlink_ext_ack *extack)
1308 struct nf_conntrack_tuple_hash *h;
1309 struct nf_conntrack_tuple tuple;
1311 struct sk_buff *skb2 = NULL;
1312 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1313 u_int8_t u3 = nfmsg->nfgen_family;
1314 struct nf_conntrack_zone zone;
1317 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1318 struct netlink_dump_control c = {
1319 .start = ctnetlink_start,
1320 .dump = ctnetlink_dump_table,
1321 .done = ctnetlink_done,
1322 .data = (void *)cda,
1325 return netlink_dump_start(ctnl, skb, nlh, &c);
1328 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1332 if (cda[CTA_TUPLE_ORIG])
1333 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1335 else if (cda[CTA_TUPLE_REPLY])
1336 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1344 h = nf_conntrack_find_get(net, &zone, &tuple);
1348 ct = nf_ct_tuplehash_to_ctrack(h);
1351 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1358 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1359 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1365 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1374 /* this avoids a loop in nfnetlink. */
1375 return err == -EAGAIN ? -ENOBUFS : err;
1378 static int ctnetlink_done_list(struct netlink_callback *cb)
1381 nf_ct_put((struct nf_conn *)cb->args[1]);
1386 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1388 struct nf_conn *ct, *last;
1389 struct nf_conntrack_tuple_hash *h;
1390 struct hlist_nulls_node *n;
1391 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1392 u_int8_t l3proto = nfmsg->nfgen_family;
1395 struct hlist_nulls_head *list;
1396 struct net *net = sock_net(skb->sk);
1401 last = (struct nf_conn *)cb->args[1];
1403 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1404 struct ct_pcpu *pcpu;
1406 if (!cpu_possible(cpu))
1409 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1410 spin_lock_bh(&pcpu->lock);
1411 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1413 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1414 ct = nf_ct_tuplehash_to_ctrack(h);
1415 if (l3proto && nf_ct_l3num(ct) != l3proto)
1423 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1425 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1429 if (!atomic_inc_not_zero(&ct->ct_general.use))
1432 cb->args[1] = (unsigned long)ct;
1433 spin_unlock_bh(&pcpu->lock);
1441 spin_unlock_bh(&pcpu->lock);
1452 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1454 return ctnetlink_dump_list(skb, cb, true);
1457 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1458 struct sk_buff *skb,
1459 const struct nlmsghdr *nlh,
1460 const struct nlattr * const cda[],
1461 struct netlink_ext_ack *extack)
1463 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1464 struct netlink_dump_control c = {
1465 .dump = ctnetlink_dump_dying,
1466 .done = ctnetlink_done_list,
1468 return netlink_dump_start(ctnl, skb, nlh, &c);
1475 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1477 return ctnetlink_dump_list(skb, cb, false);
1480 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1481 struct sk_buff *skb,
1482 const struct nlmsghdr *nlh,
1483 const struct nlattr * const cda[],
1484 struct netlink_ext_ack *extack)
1486 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1487 struct netlink_dump_control c = {
1488 .dump = ctnetlink_dump_unconfirmed,
1489 .done = ctnetlink_done_list,
1491 return netlink_dump_start(ctnl, skb, nlh, &c);
1497 #ifdef CONFIG_NF_NAT_NEEDED
1499 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1500 enum nf_nat_manip_type manip,
1501 const struct nlattr *attr)
1503 struct nf_nat_hook *nat_hook;
1506 nat_hook = rcu_dereference(nf_nat_hook);
1508 #ifdef CONFIG_MODULES
1510 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1511 if (request_module("nf-nat") < 0) {
1512 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1516 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1518 nat_hook = rcu_dereference(nf_nat_hook);
1525 err = nat_hook->parse_nat_setup(ct, manip, attr);
1526 if (err == -EAGAIN) {
1527 #ifdef CONFIG_MODULES
1529 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1530 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1531 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1535 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1546 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1551 /* Ignore these unchangable bits */
1552 on &= ~IPS_UNCHANGEABLE_MASK;
1553 off &= ~IPS_UNCHANGEABLE_MASK;
1555 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1556 if (on & (1 << bit))
1557 set_bit(bit, &ct->status);
1558 else if (off & (1 << bit))
1559 clear_bit(bit, &ct->status);
1564 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1567 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1568 d = ct->status ^ status;
1570 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1574 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1575 /* SEEN_REPLY bit can only be set */
1578 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1579 /* ASSURED bit can only be set */
1582 __ctnetlink_change_status(ct, status, 0);
1587 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1589 #ifdef CONFIG_NF_NAT_NEEDED
1592 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1595 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1600 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1603 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1609 static int ctnetlink_change_helper(struct nf_conn *ct,
1610 const struct nlattr * const cda[])
1612 struct nf_conntrack_helper *helper;
1613 struct nf_conn_help *help = nfct_help(ct);
1614 char *helpname = NULL;
1615 struct nlattr *helpinfo = NULL;
1618 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1622 /* don't change helper of sibling connections */
1624 /* If we try to change the helper to the same thing twice,
1625 * treat the second attempt as a no-op instead of returning
1631 helper = rcu_dereference(help->helper);
1632 if (helper && !strcmp(helper->name, helpname))
1640 if (!strcmp(helpname, "")) {
1641 if (help && help->helper) {
1642 /* we had a helper before ... */
1643 nf_ct_remove_expectations(ct);
1644 RCU_INIT_POINTER(help->helper, NULL);
1651 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1652 nf_ct_protonum(ct));
1653 if (helper == NULL) {
1659 if (help->helper == helper) {
1660 /* update private helper data if allowed. */
1661 if (helper->from_nlattr)
1662 helper->from_nlattr(helpinfo, ct);
1667 /* we cannot set a helper for an existing conntrack */
1675 static int ctnetlink_change_timeout(struct nf_conn *ct,
1676 const struct nlattr * const cda[])
1678 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1680 if (timeout > INT_MAX)
1682 ct->timeout = nfct_time_stamp + (u32)timeout;
1684 if (test_bit(IPS_DYING_BIT, &ct->status))
1690 #if defined(CONFIG_NF_CONNTRACK_MARK)
1691 static void ctnetlink_change_mark(struct nf_conn *ct,
1692 const struct nlattr * const cda[])
1694 u32 mark, newmark, mask = 0;
1696 if (cda[CTA_MARK_MASK])
1697 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1699 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1700 newmark = (ct->mark & mask) ^ mark;
1701 if (newmark != ct->mark)
1706 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1707 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1708 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1709 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1712 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1713 const struct nlattr * const cda[])
1715 const struct nlattr *attr = cda[CTA_PROTOINFO];
1716 const struct nf_conntrack_l4proto *l4proto;
1717 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1720 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1726 l4proto = __nf_ct_l4proto_find(nf_ct_protonum(ct));
1727 if (l4proto->from_nlattr)
1728 err = l4proto->from_nlattr(tb, ct);
1734 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1735 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1736 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1737 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1740 static int change_seq_adj(struct nf_ct_seqadj *seq,
1741 const struct nlattr * const attr)
1744 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1746 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1750 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1753 seq->correction_pos =
1754 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1756 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1759 seq->offset_before =
1760 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1762 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1766 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1772 ctnetlink_change_seq_adj(struct nf_conn *ct,
1773 const struct nlattr * const cda[])
1775 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1781 spin_lock_bh(&ct->lock);
1782 if (cda[CTA_SEQ_ADJ_ORIG]) {
1783 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1784 cda[CTA_SEQ_ADJ_ORIG]);
1788 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1791 if (cda[CTA_SEQ_ADJ_REPLY]) {
1792 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1793 cda[CTA_SEQ_ADJ_REPLY]);
1797 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1800 spin_unlock_bh(&ct->lock);
1803 spin_unlock_bh(&ct->lock);
1807 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
1808 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
1809 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
1810 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
1813 static int ctnetlink_change_synproxy(struct nf_conn *ct,
1814 const struct nlattr * const cda[])
1816 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
1817 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
1823 err = nla_parse_nested(tb, CTA_SYNPROXY_MAX, cda[CTA_SYNPROXY],
1824 synproxy_policy, NULL);
1828 if (!tb[CTA_SYNPROXY_ISN] ||
1829 !tb[CTA_SYNPROXY_ITS] ||
1830 !tb[CTA_SYNPROXY_TSOFF])
1833 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
1834 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
1835 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
1841 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1843 #ifdef CONFIG_NF_CONNTRACK_LABELS
1844 size_t len = nla_len(cda[CTA_LABELS]);
1845 const void *mask = cda[CTA_LABELS_MASK];
1847 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1851 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1852 nla_len(cda[CTA_LABELS_MASK]) != len)
1854 mask = nla_data(cda[CTA_LABELS_MASK]);
1859 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1866 ctnetlink_change_conntrack(struct nf_conn *ct,
1867 const struct nlattr * const cda[])
1871 /* only allow NAT changes and master assignation for new conntracks */
1872 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1875 if (cda[CTA_HELP]) {
1876 err = ctnetlink_change_helper(ct, cda);
1881 if (cda[CTA_TIMEOUT]) {
1882 err = ctnetlink_change_timeout(ct, cda);
1887 if (cda[CTA_STATUS]) {
1888 err = ctnetlink_change_status(ct, cda);
1893 if (cda[CTA_PROTOINFO]) {
1894 err = ctnetlink_change_protoinfo(ct, cda);
1899 #if defined(CONFIG_NF_CONNTRACK_MARK)
1901 ctnetlink_change_mark(ct, cda);
1904 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1905 err = ctnetlink_change_seq_adj(ct, cda);
1910 if (cda[CTA_SYNPROXY]) {
1911 err = ctnetlink_change_synproxy(ct, cda);
1916 if (cda[CTA_LABELS]) {
1917 err = ctnetlink_attach_labels(ct, cda);
1925 static struct nf_conn *
1926 ctnetlink_create_conntrack(struct net *net,
1927 const struct nf_conntrack_zone *zone,
1928 const struct nlattr * const cda[],
1929 struct nf_conntrack_tuple *otuple,
1930 struct nf_conntrack_tuple *rtuple,
1935 struct nf_conntrack_helper *helper;
1936 struct nf_conn_tstamp *tstamp;
1939 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1941 return ERR_PTR(-ENOMEM);
1943 if (!cda[CTA_TIMEOUT])
1946 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1947 if (timeout > INT_MAX)
1949 ct->timeout = (u32)timeout + nfct_time_stamp;
1952 if (cda[CTA_HELP]) {
1953 char *helpname = NULL;
1954 struct nlattr *helpinfo = NULL;
1956 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1960 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1961 nf_ct_protonum(ct));
1962 if (helper == NULL) {
1964 #ifdef CONFIG_MODULES
1965 if (request_module("nfct-helper-%s", helpname) < 0) {
1971 helper = __nf_conntrack_helper_find(helpname,
1973 nf_ct_protonum(ct));
1983 struct nf_conn_help *help;
1985 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1990 /* set private helper data if allowed. */
1991 if (helper->from_nlattr)
1992 helper->from_nlattr(helpinfo, ct);
1994 /* not in hash table yet so not strictly necessary */
1995 RCU_INIT_POINTER(help->helper, helper);
1998 /* try an implicit helper assignation */
1999 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
2004 err = ctnetlink_setup_nat(ct, cda);
2008 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2009 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2010 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2011 nf_ct_labels_ext_add(ct);
2012 nfct_seqadj_ext_add(ct);
2013 nfct_synproxy_ext_add(ct);
2015 /* we must add conntrack extensions before confirmation. */
2016 ct->status |= IPS_CONFIRMED;
2018 if (cda[CTA_STATUS]) {
2019 err = ctnetlink_change_status(ct, cda);
2024 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2025 err = ctnetlink_change_seq_adj(ct, cda);
2030 memset(&ct->proto, 0, sizeof(ct->proto));
2031 if (cda[CTA_PROTOINFO]) {
2032 err = ctnetlink_change_protoinfo(ct, cda);
2037 if (cda[CTA_SYNPROXY]) {
2038 err = ctnetlink_change_synproxy(ct, cda);
2043 #if defined(CONFIG_NF_CONNTRACK_MARK)
2045 ctnetlink_change_mark(ct, cda);
2048 /* setup master conntrack: this is a confirmed expectation */
2049 if (cda[CTA_TUPLE_MASTER]) {
2050 struct nf_conntrack_tuple master;
2051 struct nf_conntrack_tuple_hash *master_h;
2052 struct nf_conn *master_ct;
2054 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2059 master_h = nf_conntrack_find_get(net, zone, &master);
2060 if (master_h == NULL) {
2064 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2065 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2066 ct->master = master_ct;
2068 tstamp = nf_conn_tstamp_find(ct);
2070 tstamp->start = ktime_get_real_ns();
2072 err = nf_conntrack_hash_check_insert(ct);
2083 nf_conntrack_free(ct);
2084 return ERR_PTR(err);
2087 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2088 struct sk_buff *skb,
2089 const struct nlmsghdr *nlh,
2090 const struct nlattr * const cda[],
2091 struct netlink_ext_ack *extack)
2093 struct nf_conntrack_tuple otuple, rtuple;
2094 struct nf_conntrack_tuple_hash *h = NULL;
2095 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2097 u_int8_t u3 = nfmsg->nfgen_family;
2098 struct nf_conntrack_zone zone;
2101 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2105 if (cda[CTA_TUPLE_ORIG]) {
2106 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2112 if (cda[CTA_TUPLE_REPLY]) {
2113 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2119 if (cda[CTA_TUPLE_ORIG])
2120 h = nf_conntrack_find_get(net, &zone, &otuple);
2121 else if (cda[CTA_TUPLE_REPLY])
2122 h = nf_conntrack_find_get(net, &zone, &rtuple);
2126 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2127 enum ip_conntrack_events events;
2129 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2131 if (otuple.dst.protonum != rtuple.dst.protonum)
2134 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2140 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2141 events = 1 << IPCT_RELATED;
2143 events = 1 << IPCT_NEW;
2145 if (cda[CTA_LABELS] &&
2146 ctnetlink_attach_labels(ct, cda) == 0)
2147 events |= (1 << IPCT_LABEL);
2149 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2150 (1 << IPCT_ASSURED) |
2151 (1 << IPCT_HELPER) |
2152 (1 << IPCT_PROTOINFO) |
2153 (1 << IPCT_SEQADJ) |
2155 (1 << IPCT_SYNPROXY) |
2157 ct, NETLINK_CB(skb).portid,
2164 /* implicit 'else' */
2167 ct = nf_ct_tuplehash_to_ctrack(h);
2168 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2169 err = ctnetlink_change_conntrack(ct, cda);
2171 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2172 (1 << IPCT_ASSURED) |
2173 (1 << IPCT_HELPER) |
2175 (1 << IPCT_PROTOINFO) |
2176 (1 << IPCT_SEQADJ) |
2178 (1 << IPCT_SYNPROXY),
2179 ct, NETLINK_CB(skb).portid,
2189 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2190 __u16 cpu, const struct ip_conntrack_stat *st)
2192 struct nlmsghdr *nlh;
2193 struct nfgenmsg *nfmsg;
2194 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2196 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2197 IPCTNL_MSG_CT_GET_STATS_CPU);
2198 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2202 nfmsg = nlmsg_data(nlh);
2203 nfmsg->nfgen_family = AF_UNSPEC;
2204 nfmsg->version = NFNETLINK_V0;
2205 nfmsg->res_id = htons(cpu);
2207 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2208 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2209 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2210 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2211 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2212 htonl(st->insert_failed)) ||
2213 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2214 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2215 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2216 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2217 htonl(st->search_restart)))
2218 goto nla_put_failure;
2220 nlmsg_end(skb, nlh);
2225 nlmsg_cancel(skb, nlh);
2230 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2233 struct net *net = sock_net(skb->sk);
2235 if (cb->args[0] == nr_cpu_ids)
2238 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2239 const struct ip_conntrack_stat *st;
2241 if (!cpu_possible(cpu))
2244 st = per_cpu_ptr(net->ct.stat, cpu);
2245 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2246 NETLINK_CB(cb->skb).portid,
2256 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2257 struct sk_buff *skb,
2258 const struct nlmsghdr *nlh,
2259 const struct nlattr * const cda[],
2260 struct netlink_ext_ack *extack)
2262 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2263 struct netlink_dump_control c = {
2264 .dump = ctnetlink_ct_stat_cpu_dump,
2266 return netlink_dump_start(ctnl, skb, nlh, &c);
2273 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2276 struct nlmsghdr *nlh;
2277 struct nfgenmsg *nfmsg;
2278 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2279 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2281 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2282 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2286 nfmsg = nlmsg_data(nlh);
2287 nfmsg->nfgen_family = AF_UNSPEC;
2288 nfmsg->version = NFNETLINK_V0;
2291 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2292 goto nla_put_failure;
2294 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2295 goto nla_put_failure;
2297 nlmsg_end(skb, nlh);
2302 nlmsg_cancel(skb, nlh);
2306 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2307 struct sk_buff *skb, const struct nlmsghdr *nlh,
2308 const struct nlattr * const cda[],
2309 struct netlink_ext_ack *extack)
2311 struct sk_buff *skb2;
2314 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2318 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2320 NFNL_MSG_TYPE(nlh->nlmsg_type),
2325 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2334 /* this avoids a loop in nfnetlink. */
2335 return err == -EAGAIN ? -ENOBUFS : err;
2338 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2339 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2340 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2341 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2342 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2343 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2344 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2345 .len = NF_CT_HELPER_NAME_LEN - 1 },
2346 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2347 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2348 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2349 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2350 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2353 static struct nf_conntrack_expect *
2354 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2355 struct nf_conntrack_helper *helper,
2356 struct nf_conntrack_tuple *tuple,
2357 struct nf_conntrack_tuple *mask);
2359 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2361 ctnetlink_glue_build_size(const struct nf_conn *ct)
2363 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2364 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2365 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2366 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2367 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2368 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2369 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2370 + nla_total_size(0) /* CTA_PROTOINFO */
2371 + nla_total_size(0) /* CTA_HELP */
2372 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2373 + ctnetlink_secctx_size(ct)
2374 #ifdef CONFIG_NF_NAT_NEEDED
2375 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2376 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2378 #ifdef CONFIG_NF_CONNTRACK_MARK
2379 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2381 #ifdef CONFIG_NF_CONNTRACK_ZONES
2382 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2384 + ctnetlink_proto_size(ct)
2388 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2389 enum ip_conntrack_info *ctinfo)
2391 return nf_ct_get(skb, ctinfo);
2394 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2396 const struct nf_conntrack_zone *zone;
2397 struct nlattr *nest_parms;
2399 zone = nf_ct_zone(ct);
2401 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2403 goto nla_put_failure;
2404 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2405 goto nla_put_failure;
2406 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2407 NF_CT_ZONE_DIR_ORIG) < 0)
2408 goto nla_put_failure;
2409 nla_nest_end(skb, nest_parms);
2411 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2413 goto nla_put_failure;
2414 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2415 goto nla_put_failure;
2416 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2417 NF_CT_ZONE_DIR_REPL) < 0)
2418 goto nla_put_failure;
2419 nla_nest_end(skb, nest_parms);
2421 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2422 NF_CT_DEFAULT_ZONE_DIR) < 0)
2423 goto nla_put_failure;
2425 if (ctnetlink_dump_id(skb, ct) < 0)
2426 goto nla_put_failure;
2428 if (ctnetlink_dump_status(skb, ct) < 0)
2429 goto nla_put_failure;
2431 if (ctnetlink_dump_timeout(skb, ct) < 0)
2432 goto nla_put_failure;
2434 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2435 goto nla_put_failure;
2437 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2438 goto nla_put_failure;
2440 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2441 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2442 goto nla_put_failure;
2444 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2445 goto nla_put_failure;
2447 if ((ct->status & IPS_SEQ_ADJUST) &&
2448 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2449 goto nla_put_failure;
2451 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2452 goto nla_put_failure;
2454 #ifdef CONFIG_NF_CONNTRACK_MARK
2455 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2456 goto nla_put_failure;
2458 if (ctnetlink_dump_labels(skb, ct) < 0)
2459 goto nla_put_failure;
2467 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2468 enum ip_conntrack_info ctinfo,
2469 u_int16_t ct_attr, u_int16_t ct_info_attr)
2471 struct nlattr *nest_parms;
2473 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2475 goto nla_put_failure;
2477 if (__ctnetlink_glue_build(skb, ct) < 0)
2478 goto nla_put_failure;
2480 nla_nest_end(skb, nest_parms);
2482 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2483 goto nla_put_failure;
2492 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2494 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2495 unsigned long d = ct->status ^ status;
2497 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2498 /* SEEN_REPLY bit can only be set */
2501 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2502 /* ASSURED bit can only be set */
2505 /* This check is less strict than ctnetlink_change_status()
2506 * because callers often flip IPS_EXPECTED bits when sending
2507 * an NFQA_CT attribute to the kernel. So ignore the
2508 * unchangeable bits but do not error out. Also user programs
2509 * are allowed to clear the bits that they are allowed to change.
2511 __ctnetlink_change_status(ct, status, ~status);
2516 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2520 if (cda[CTA_TIMEOUT]) {
2521 err = ctnetlink_change_timeout(ct, cda);
2525 if (cda[CTA_STATUS]) {
2526 err = ctnetlink_update_status(ct, cda);
2530 if (cda[CTA_HELP]) {
2531 err = ctnetlink_change_helper(ct, cda);
2535 if (cda[CTA_LABELS]) {
2536 err = ctnetlink_attach_labels(ct, cda);
2540 #if defined(CONFIG_NF_CONNTRACK_MARK)
2541 if (cda[CTA_MARK]) {
2542 ctnetlink_change_mark(ct, cda);
2549 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2551 struct nlattr *cda[CTA_MAX+1];
2554 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2558 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2561 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2562 const struct nf_conn *ct,
2563 struct nf_conntrack_tuple *tuple,
2564 struct nf_conntrack_tuple *mask)
2568 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2569 nf_ct_l3num(ct), NULL);
2573 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2574 nf_ct_l3num(ct), NULL);
2578 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2579 u32 portid, u32 report)
2581 struct nlattr *cda[CTA_EXPECT_MAX+1];
2582 struct nf_conntrack_tuple tuple, mask;
2583 struct nf_conntrack_helper *helper = NULL;
2584 struct nf_conntrack_expect *exp;
2587 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2592 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2597 if (cda[CTA_EXPECT_HELP_NAME]) {
2598 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2600 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2601 nf_ct_protonum(ct));
2606 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2607 helper, &tuple, &mask);
2609 return PTR_ERR(exp);
2611 err = nf_ct_expect_related_report(exp, portid, report);
2612 nf_ct_expect_put(exp);
2616 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2617 enum ip_conntrack_info ctinfo, int diff)
2619 if (!(ct->status & IPS_NAT_MASK))
2622 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2625 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2626 .get_ct = ctnetlink_glue_get_ct,
2627 .build_size = ctnetlink_glue_build_size,
2628 .build = ctnetlink_glue_build,
2629 .parse = ctnetlink_glue_parse,
2630 .attach_expect = ctnetlink_glue_attach_expect,
2631 .seq_adjust = ctnetlink_glue_seqadj,
2633 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2635 /***********************************************************************
2637 ***********************************************************************/
2639 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2640 const struct nf_conntrack_tuple *tuple,
2643 struct nlattr *nest_parms;
2645 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2647 goto nla_put_failure;
2648 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2649 goto nla_put_failure;
2650 nla_nest_end(skb, nest_parms);
2658 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2659 const struct nf_conntrack_tuple *tuple,
2660 const struct nf_conntrack_tuple_mask *mask)
2662 const struct nf_conntrack_l4proto *l4proto;
2663 struct nf_conntrack_tuple m;
2664 struct nlattr *nest_parms;
2667 memset(&m, 0xFF, sizeof(m));
2668 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2669 m.src.u.all = mask->src.u.all;
2670 m.dst.protonum = tuple->dst.protonum;
2672 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2674 goto nla_put_failure;
2677 ret = ctnetlink_dump_tuples_ip(skb, &m);
2679 l4proto = __nf_ct_l4proto_find(tuple->dst.protonum);
2680 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2684 if (unlikely(ret < 0))
2685 goto nla_put_failure;
2687 nla_nest_end(skb, nest_parms);
2695 static const union nf_inet_addr any_addr;
2698 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2699 const struct nf_conntrack_expect *exp)
2701 struct nf_conn *master = exp->master;
2702 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2703 struct nf_conn_help *help;
2704 #ifdef CONFIG_NF_NAT_NEEDED
2705 struct nlattr *nest_parms;
2706 struct nf_conntrack_tuple nat_tuple = {};
2708 struct nf_ct_helper_expectfn *expfn;
2713 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2714 goto nla_put_failure;
2715 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2716 goto nla_put_failure;
2717 if (ctnetlink_exp_dump_tuple(skb,
2718 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2719 CTA_EXPECT_MASTER) < 0)
2720 goto nla_put_failure;
2722 #ifdef CONFIG_NF_NAT_NEEDED
2723 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2724 exp->saved_proto.all) {
2725 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2727 goto nla_put_failure;
2729 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2730 goto nla_put_failure;
2732 nat_tuple.src.l3num = nf_ct_l3num(master);
2733 nat_tuple.src.u3 = exp->saved_addr;
2734 nat_tuple.dst.protonum = nf_ct_protonum(master);
2735 nat_tuple.src.u = exp->saved_proto;
2737 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2738 CTA_EXPECT_NAT_TUPLE) < 0)
2739 goto nla_put_failure;
2740 nla_nest_end(skb, nest_parms);
2743 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2744 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2745 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2746 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2747 goto nla_put_failure;
2748 help = nfct_help(master);
2750 struct nf_conntrack_helper *helper;
2752 helper = rcu_dereference(help->helper);
2754 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2755 goto nla_put_failure;
2757 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2758 if (expfn != NULL &&
2759 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2760 goto nla_put_failure;
2769 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2770 int event, const struct nf_conntrack_expect *exp)
2772 struct nlmsghdr *nlh;
2773 struct nfgenmsg *nfmsg;
2774 unsigned int flags = portid ? NLM_F_MULTI : 0;
2776 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2777 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2781 nfmsg = nlmsg_data(nlh);
2782 nfmsg->nfgen_family = exp->tuple.src.l3num;
2783 nfmsg->version = NFNETLINK_V0;
2786 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2787 goto nla_put_failure;
2789 nlmsg_end(skb, nlh);
2794 nlmsg_cancel(skb, nlh);
2798 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2800 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2802 struct nf_conntrack_expect *exp = item->exp;
2803 struct net *net = nf_ct_exp_net(exp);
2804 struct nlmsghdr *nlh;
2805 struct nfgenmsg *nfmsg;
2806 struct sk_buff *skb;
2807 unsigned int type, group;
2810 if (events & (1 << IPEXP_DESTROY)) {
2811 type = IPCTNL_MSG_EXP_DELETE;
2812 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2813 } else if (events & (1 << IPEXP_NEW)) {
2814 type = IPCTNL_MSG_EXP_NEW;
2815 flags = NLM_F_CREATE|NLM_F_EXCL;
2816 group = NFNLGRP_CONNTRACK_EXP_NEW;
2820 if (!item->report && !nfnetlink_has_listeners(net, group))
2823 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2827 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2828 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2832 nfmsg = nlmsg_data(nlh);
2833 nfmsg->nfgen_family = exp->tuple.src.l3num;
2834 nfmsg->version = NFNETLINK_V0;
2837 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2838 goto nla_put_failure;
2840 nlmsg_end(skb, nlh);
2841 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2845 nlmsg_cancel(skb, nlh);
2849 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2853 static int ctnetlink_exp_done(struct netlink_callback *cb)
2856 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2861 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2863 struct net *net = sock_net(skb->sk);
2864 struct nf_conntrack_expect *exp, *last;
2865 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2866 u_int8_t l3proto = nfmsg->nfgen_family;
2869 last = (struct nf_conntrack_expect *)cb->args[1];
2870 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2872 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2874 if (l3proto && exp->tuple.src.l3num != l3proto)
2877 if (!net_eq(nf_ct_net(exp->master), net))
2885 if (ctnetlink_exp_fill_info(skb,
2886 NETLINK_CB(cb->skb).portid,
2890 if (!refcount_inc_not_zero(&exp->use))
2892 cb->args[1] = (unsigned long)exp;
2904 nf_ct_expect_put(last);
2910 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2912 struct nf_conntrack_expect *exp, *last;
2913 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2914 struct nf_conn *ct = cb->data;
2915 struct nf_conn_help *help = nfct_help(ct);
2916 u_int8_t l3proto = nfmsg->nfgen_family;
2922 last = (struct nf_conntrack_expect *)cb->args[1];
2924 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2925 if (l3proto && exp->tuple.src.l3num != l3proto)
2932 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2936 if (!refcount_inc_not_zero(&exp->use))
2938 cb->args[1] = (unsigned long)exp;
2950 nf_ct_expect_put(last);
2955 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2956 struct sk_buff *skb,
2957 const struct nlmsghdr *nlh,
2958 const struct nlattr * const cda[],
2959 struct netlink_ext_ack *extack)
2962 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2963 u_int8_t u3 = nfmsg->nfgen_family;
2964 struct nf_conntrack_tuple tuple;
2965 struct nf_conntrack_tuple_hash *h;
2967 struct nf_conntrack_zone zone;
2968 struct netlink_dump_control c = {
2969 .dump = ctnetlink_exp_ct_dump_table,
2970 .done = ctnetlink_exp_done,
2973 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2978 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2982 h = nf_conntrack_find_get(net, &zone, &tuple);
2986 ct = nf_ct_tuplehash_to_ctrack(h);
2987 /* No expectation linked to this connection tracking. */
2988 if (!nfct_help(ct)) {
2995 err = netlink_dump_start(ctnl, skb, nlh, &c);
3001 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
3002 struct sk_buff *skb, const struct nlmsghdr *nlh,
3003 const struct nlattr * const cda[],
3004 struct netlink_ext_ack *extack)
3006 struct nf_conntrack_tuple tuple;
3007 struct nf_conntrack_expect *exp;
3008 struct sk_buff *skb2;
3009 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3010 u_int8_t u3 = nfmsg->nfgen_family;
3011 struct nf_conntrack_zone zone;
3014 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3015 if (cda[CTA_EXPECT_MASTER])
3016 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
3019 struct netlink_dump_control c = {
3020 .dump = ctnetlink_exp_dump_table,
3021 .done = ctnetlink_exp_done,
3023 return netlink_dump_start(ctnl, skb, nlh, &c);
3027 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3031 if (cda[CTA_EXPECT_TUPLE])
3032 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3034 else if (cda[CTA_EXPECT_MASTER])
3035 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3043 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3047 if (cda[CTA_EXPECT_ID]) {
3048 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3049 if (ntohl(id) != (u32)(unsigned long)exp) {
3050 nf_ct_expect_put(exp);
3056 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3058 nf_ct_expect_put(exp);
3063 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3064 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3066 nf_ct_expect_put(exp);
3070 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3079 /* this avoids a loop in nfnetlink. */
3080 return err == -EAGAIN ? -ENOBUFS : err;
3083 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3085 const struct nf_conn_help *m_help;
3086 const char *name = data;
3088 m_help = nfct_help(exp->master);
3090 return strcmp(m_help->helper->name, name) == 0;
3093 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3098 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3099 struct sk_buff *skb, const struct nlmsghdr *nlh,
3100 const struct nlattr * const cda[],
3101 struct netlink_ext_ack *extack)
3103 struct nf_conntrack_expect *exp;
3104 struct nf_conntrack_tuple tuple;
3105 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3106 u_int8_t u3 = nfmsg->nfgen_family;
3107 struct nf_conntrack_zone zone;
3110 if (cda[CTA_EXPECT_TUPLE]) {
3111 /* delete a single expect by tuple */
3112 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3116 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3121 /* bump usage count to 2 */
3122 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3126 if (cda[CTA_EXPECT_ID]) {
3127 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3128 if (ntohl(id) != (u32)(unsigned long)exp) {
3129 nf_ct_expect_put(exp);
3134 /* after list removal, usage count == 1 */
3135 spin_lock_bh(&nf_conntrack_expect_lock);
3136 if (del_timer(&exp->timeout)) {
3137 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3139 nf_ct_expect_put(exp);
3141 spin_unlock_bh(&nf_conntrack_expect_lock);
3142 /* have to put what we 'get' above.
3143 * after this line usage count == 0 */
3144 nf_ct_expect_put(exp);
3145 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3146 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3148 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3149 NETLINK_CB(skb).portid,
3152 /* This basically means we have to flush everything*/
3153 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3154 NETLINK_CB(skb).portid,
3161 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3162 const struct nlattr * const cda[])
3164 if (cda[CTA_EXPECT_TIMEOUT]) {
3165 if (!del_timer(&x->timeout))
3168 x->timeout.expires = jiffies +
3169 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3170 add_timer(&x->timeout);
3175 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3176 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3177 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3181 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3182 struct nf_conntrack_expect *exp,
3185 #ifdef CONFIG_NF_NAT_NEEDED
3186 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3187 struct nf_conntrack_tuple nat_tuple = {};
3190 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3191 exp_nat_nla_policy, NULL);
3195 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3198 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3199 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3204 exp->saved_addr = nat_tuple.src.u3;
3205 exp->saved_proto = nat_tuple.src.u;
3206 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3214 static struct nf_conntrack_expect *
3215 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3216 struct nf_conntrack_helper *helper,
3217 struct nf_conntrack_tuple *tuple,
3218 struct nf_conntrack_tuple *mask)
3220 u_int32_t class = 0;
3221 struct nf_conntrack_expect *exp;
3222 struct nf_conn_help *help;
3225 help = nfct_help(ct);
3227 return ERR_PTR(-EOPNOTSUPP);
3229 if (cda[CTA_EXPECT_CLASS] && helper) {
3230 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3231 if (class > helper->expect_class_max)
3232 return ERR_PTR(-EINVAL);
3234 exp = nf_ct_expect_alloc(ct);
3236 return ERR_PTR(-ENOMEM);
3238 if (cda[CTA_EXPECT_FLAGS]) {
3239 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3240 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3244 if (cda[CTA_EXPECT_FN]) {
3245 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3246 struct nf_ct_helper_expectfn *expfn;
3248 expfn = nf_ct_helper_expectfn_find_by_name(name);
3249 if (expfn == NULL) {
3253 exp->expectfn = expfn->expectfn;
3255 exp->expectfn = NULL;
3259 exp->helper = helper;
3260 exp->tuple = *tuple;
3261 exp->mask.src.u3 = mask->src.u3;
3262 exp->mask.src.u.all = mask->src.u.all;
3264 if (cda[CTA_EXPECT_NAT]) {
3265 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3266 exp, nf_ct_l3num(ct));
3272 nf_ct_expect_put(exp);
3273 return ERR_PTR(err);
3277 ctnetlink_create_expect(struct net *net,
3278 const struct nf_conntrack_zone *zone,
3279 const struct nlattr * const cda[],
3280 u_int8_t u3, u32 portid, int report)
3282 struct nf_conntrack_tuple tuple, mask, master_tuple;
3283 struct nf_conntrack_tuple_hash *h = NULL;
3284 struct nf_conntrack_helper *helper = NULL;
3285 struct nf_conntrack_expect *exp;
3289 /* caller guarantees that those three CTA_EXPECT_* exist */
3290 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3294 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3298 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3303 /* Look for master conntrack of this expectation */
3304 h = nf_conntrack_find_get(net, zone, &master_tuple);
3307 ct = nf_ct_tuplehash_to_ctrack(h);
3310 if (cda[CTA_EXPECT_HELP_NAME]) {
3311 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3313 helper = __nf_conntrack_helper_find(helpname, u3,
3314 nf_ct_protonum(ct));
3315 if (helper == NULL) {
3317 #ifdef CONFIG_MODULES
3318 if (request_module("nfct-helper-%s", helpname) < 0) {
3323 helper = __nf_conntrack_helper_find(helpname, u3,
3324 nf_ct_protonum(ct));
3336 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3342 err = nf_ct_expect_related_report(exp, portid, report);
3343 nf_ct_expect_put(exp);
3351 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3352 struct sk_buff *skb, const struct nlmsghdr *nlh,
3353 const struct nlattr * const cda[],
3354 struct netlink_ext_ack *extack)
3356 struct nf_conntrack_tuple tuple;
3357 struct nf_conntrack_expect *exp;
3358 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3359 u_int8_t u3 = nfmsg->nfgen_family;
3360 struct nf_conntrack_zone zone;
3363 if (!cda[CTA_EXPECT_TUPLE]
3364 || !cda[CTA_EXPECT_MASK]
3365 || !cda[CTA_EXPECT_MASTER])
3368 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3372 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3377 spin_lock_bh(&nf_conntrack_expect_lock);
3378 exp = __nf_ct_expect_find(net, &zone, &tuple);
3380 spin_unlock_bh(&nf_conntrack_expect_lock);
3382 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3383 err = ctnetlink_create_expect(net, &zone, cda, u3,
3384 NETLINK_CB(skb).portid,
3391 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3392 err = ctnetlink_change_expect(exp, cda);
3393 spin_unlock_bh(&nf_conntrack_expect_lock);
3399 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3400 const struct ip_conntrack_stat *st)
3402 struct nlmsghdr *nlh;
3403 struct nfgenmsg *nfmsg;
3404 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3406 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3407 IPCTNL_MSG_EXP_GET_STATS_CPU);
3408 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3412 nfmsg = nlmsg_data(nlh);
3413 nfmsg->nfgen_family = AF_UNSPEC;
3414 nfmsg->version = NFNETLINK_V0;
3415 nfmsg->res_id = htons(cpu);
3417 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3418 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3419 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3420 goto nla_put_failure;
3422 nlmsg_end(skb, nlh);
3427 nlmsg_cancel(skb, nlh);
3432 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3435 struct net *net = sock_net(skb->sk);
3437 if (cb->args[0] == nr_cpu_ids)
3440 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3441 const struct ip_conntrack_stat *st;
3443 if (!cpu_possible(cpu))
3446 st = per_cpu_ptr(net->ct.stat, cpu);
3447 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3457 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3458 struct sk_buff *skb,
3459 const struct nlmsghdr *nlh,
3460 const struct nlattr * const cda[],
3461 struct netlink_ext_ack *extack)
3463 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3464 struct netlink_dump_control c = {
3465 .dump = ctnetlink_exp_stat_cpu_dump,
3467 return netlink_dump_start(ctnl, skb, nlh, &c);
3473 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3474 static struct nf_ct_event_notifier ctnl_notifier = {
3475 .fcn = ctnetlink_conntrack_event,
3478 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3479 .fcn = ctnetlink_expect_event,
3483 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3484 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3485 .attr_count = CTA_MAX,
3486 .policy = ct_nla_policy },
3487 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3488 .attr_count = CTA_MAX,
3489 .policy = ct_nla_policy },
3490 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3491 .attr_count = CTA_MAX,
3492 .policy = ct_nla_policy },
3493 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3494 .attr_count = CTA_MAX,
3495 .policy = ct_nla_policy },
3496 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3497 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3498 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3499 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3502 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3503 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3504 .attr_count = CTA_EXPECT_MAX,
3505 .policy = exp_nla_policy },
3506 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3507 .attr_count = CTA_EXPECT_MAX,
3508 .policy = exp_nla_policy },
3509 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3510 .attr_count = CTA_EXPECT_MAX,
3511 .policy = exp_nla_policy },
3512 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3515 static const struct nfnetlink_subsystem ctnl_subsys = {
3516 .name = "conntrack",
3517 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3518 .cb_count = IPCTNL_MSG_MAX,
3522 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3523 .name = "conntrack_expect",
3524 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3525 .cb_count = IPCTNL_MSG_EXP_MAX,
3529 MODULE_ALIAS("ip_conntrack_netlink");
3530 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3531 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3533 static int __net_init ctnetlink_net_init(struct net *net)
3535 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3538 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3540 pr_err("ctnetlink_init: cannot register notifier.\n");
3544 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3546 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3547 goto err_unreg_notifier;
3552 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3554 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3560 static void ctnetlink_net_exit(struct net *net)
3562 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3563 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3564 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3568 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3572 list_for_each_entry(net, net_exit_list, exit_list)
3573 ctnetlink_net_exit(net);
3576 static struct pernet_operations ctnetlink_net_ops = {
3577 .init = ctnetlink_net_init,
3578 .exit_batch = ctnetlink_net_exit_batch,
3581 static int __init ctnetlink_init(void)
3585 ret = nfnetlink_subsys_register(&ctnl_subsys);
3587 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3591 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3593 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3594 goto err_unreg_subsys;
3597 ret = register_pernet_subsys(&ctnetlink_net_ops);
3599 pr_err("ctnetlink_init: cannot register pernet operations\n");
3600 goto err_unreg_exp_subsys;
3602 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3603 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3604 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3608 err_unreg_exp_subsys:
3609 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3611 nfnetlink_subsys_unregister(&ctnl_subsys);
3616 static void __exit ctnetlink_exit(void)
3618 unregister_pernet_subsys(&ctnetlink_net_ops);
3619 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3620 nfnetlink_subsys_unregister(&ctnl_subsys);
3621 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3622 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3627 module_init(ctnetlink_init);
3628 module_exit(ctnetlink_exit);