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.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,
1725 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
1726 if (l4proto->from_nlattr)
1727 err = l4proto->from_nlattr(tb, ct);
1732 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1733 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1734 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1735 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1738 static int change_seq_adj(struct nf_ct_seqadj *seq,
1739 const struct nlattr * const attr)
1742 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1744 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1748 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1751 seq->correction_pos =
1752 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1754 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1757 seq->offset_before =
1758 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1760 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1764 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1770 ctnetlink_change_seq_adj(struct nf_conn *ct,
1771 const struct nlattr * const cda[])
1773 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1779 spin_lock_bh(&ct->lock);
1780 if (cda[CTA_SEQ_ADJ_ORIG]) {
1781 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1782 cda[CTA_SEQ_ADJ_ORIG]);
1786 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1789 if (cda[CTA_SEQ_ADJ_REPLY]) {
1790 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1791 cda[CTA_SEQ_ADJ_REPLY]);
1795 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1798 spin_unlock_bh(&ct->lock);
1801 spin_unlock_bh(&ct->lock);
1805 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
1806 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
1807 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
1808 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
1811 static int ctnetlink_change_synproxy(struct nf_conn *ct,
1812 const struct nlattr * const cda[])
1814 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
1815 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
1821 err = nla_parse_nested(tb, CTA_SYNPROXY_MAX, cda[CTA_SYNPROXY],
1822 synproxy_policy, NULL);
1826 if (!tb[CTA_SYNPROXY_ISN] ||
1827 !tb[CTA_SYNPROXY_ITS] ||
1828 !tb[CTA_SYNPROXY_TSOFF])
1831 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
1832 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
1833 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
1839 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1841 #ifdef CONFIG_NF_CONNTRACK_LABELS
1842 size_t len = nla_len(cda[CTA_LABELS]);
1843 const void *mask = cda[CTA_LABELS_MASK];
1845 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1849 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1850 nla_len(cda[CTA_LABELS_MASK]) != len)
1852 mask = nla_data(cda[CTA_LABELS_MASK]);
1857 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1864 ctnetlink_change_conntrack(struct nf_conn *ct,
1865 const struct nlattr * const cda[])
1869 /* only allow NAT changes and master assignation for new conntracks */
1870 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1873 if (cda[CTA_HELP]) {
1874 err = ctnetlink_change_helper(ct, cda);
1879 if (cda[CTA_TIMEOUT]) {
1880 err = ctnetlink_change_timeout(ct, cda);
1885 if (cda[CTA_STATUS]) {
1886 err = ctnetlink_change_status(ct, cda);
1891 if (cda[CTA_PROTOINFO]) {
1892 err = ctnetlink_change_protoinfo(ct, cda);
1897 #if defined(CONFIG_NF_CONNTRACK_MARK)
1899 ctnetlink_change_mark(ct, cda);
1902 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1903 err = ctnetlink_change_seq_adj(ct, cda);
1908 if (cda[CTA_SYNPROXY]) {
1909 err = ctnetlink_change_synproxy(ct, cda);
1914 if (cda[CTA_LABELS]) {
1915 err = ctnetlink_attach_labels(ct, cda);
1923 static struct nf_conn *
1924 ctnetlink_create_conntrack(struct net *net,
1925 const struct nf_conntrack_zone *zone,
1926 const struct nlattr * const cda[],
1927 struct nf_conntrack_tuple *otuple,
1928 struct nf_conntrack_tuple *rtuple,
1933 struct nf_conntrack_helper *helper;
1934 struct nf_conn_tstamp *tstamp;
1937 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1939 return ERR_PTR(-ENOMEM);
1941 if (!cda[CTA_TIMEOUT])
1944 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1945 if (timeout > INT_MAX)
1947 ct->timeout = (u32)timeout + nfct_time_stamp;
1950 if (cda[CTA_HELP]) {
1951 char *helpname = NULL;
1952 struct nlattr *helpinfo = NULL;
1954 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1958 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1959 nf_ct_protonum(ct));
1960 if (helper == NULL) {
1962 #ifdef CONFIG_MODULES
1963 if (request_module("nfct-helper-%s", helpname) < 0) {
1969 helper = __nf_conntrack_helper_find(helpname,
1971 nf_ct_protonum(ct));
1981 struct nf_conn_help *help;
1983 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1988 /* set private helper data if allowed. */
1989 if (helper->from_nlattr)
1990 helper->from_nlattr(helpinfo, ct);
1992 /* not in hash table yet so not strictly necessary */
1993 RCU_INIT_POINTER(help->helper, helper);
1996 /* try an implicit helper assignation */
1997 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
2002 err = ctnetlink_setup_nat(ct, cda);
2006 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2007 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2008 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2009 nf_ct_labels_ext_add(ct);
2010 nfct_seqadj_ext_add(ct);
2011 nfct_synproxy_ext_add(ct);
2013 /* we must add conntrack extensions before confirmation. */
2014 ct->status |= IPS_CONFIRMED;
2016 if (cda[CTA_STATUS]) {
2017 err = ctnetlink_change_status(ct, cda);
2022 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2023 err = ctnetlink_change_seq_adj(ct, cda);
2028 memset(&ct->proto, 0, sizeof(ct->proto));
2029 if (cda[CTA_PROTOINFO]) {
2030 err = ctnetlink_change_protoinfo(ct, cda);
2035 if (cda[CTA_SYNPROXY]) {
2036 err = ctnetlink_change_synproxy(ct, cda);
2041 #if defined(CONFIG_NF_CONNTRACK_MARK)
2043 ctnetlink_change_mark(ct, cda);
2046 /* setup master conntrack: this is a confirmed expectation */
2047 if (cda[CTA_TUPLE_MASTER]) {
2048 struct nf_conntrack_tuple master;
2049 struct nf_conntrack_tuple_hash *master_h;
2050 struct nf_conn *master_ct;
2052 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2057 master_h = nf_conntrack_find_get(net, zone, &master);
2058 if (master_h == NULL) {
2062 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2063 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2064 ct->master = master_ct;
2066 tstamp = nf_conn_tstamp_find(ct);
2068 tstamp->start = ktime_get_real_ns();
2070 err = nf_conntrack_hash_check_insert(ct);
2081 nf_conntrack_free(ct);
2082 return ERR_PTR(err);
2085 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2086 struct sk_buff *skb,
2087 const struct nlmsghdr *nlh,
2088 const struct nlattr * const cda[],
2089 struct netlink_ext_ack *extack)
2091 struct nf_conntrack_tuple otuple, rtuple;
2092 struct nf_conntrack_tuple_hash *h = NULL;
2093 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2095 u_int8_t u3 = nfmsg->nfgen_family;
2096 struct nf_conntrack_zone zone;
2099 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2103 if (cda[CTA_TUPLE_ORIG]) {
2104 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2110 if (cda[CTA_TUPLE_REPLY]) {
2111 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2117 if (cda[CTA_TUPLE_ORIG])
2118 h = nf_conntrack_find_get(net, &zone, &otuple);
2119 else if (cda[CTA_TUPLE_REPLY])
2120 h = nf_conntrack_find_get(net, &zone, &rtuple);
2124 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2125 enum ip_conntrack_events events;
2127 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2129 if (otuple.dst.protonum != rtuple.dst.protonum)
2132 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2138 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2139 events = 1 << IPCT_RELATED;
2141 events = 1 << IPCT_NEW;
2143 if (cda[CTA_LABELS] &&
2144 ctnetlink_attach_labels(ct, cda) == 0)
2145 events |= (1 << IPCT_LABEL);
2147 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2148 (1 << IPCT_ASSURED) |
2149 (1 << IPCT_HELPER) |
2150 (1 << IPCT_PROTOINFO) |
2151 (1 << IPCT_SEQADJ) |
2153 (1 << IPCT_SYNPROXY) |
2155 ct, NETLINK_CB(skb).portid,
2162 /* implicit 'else' */
2165 ct = nf_ct_tuplehash_to_ctrack(h);
2166 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2167 err = ctnetlink_change_conntrack(ct, cda);
2169 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2170 (1 << IPCT_ASSURED) |
2171 (1 << IPCT_HELPER) |
2173 (1 << IPCT_PROTOINFO) |
2174 (1 << IPCT_SEQADJ) |
2176 (1 << IPCT_SYNPROXY),
2177 ct, NETLINK_CB(skb).portid,
2187 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2188 __u16 cpu, const struct ip_conntrack_stat *st)
2190 struct nlmsghdr *nlh;
2191 struct nfgenmsg *nfmsg;
2192 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2194 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2195 IPCTNL_MSG_CT_GET_STATS_CPU);
2196 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2200 nfmsg = nlmsg_data(nlh);
2201 nfmsg->nfgen_family = AF_UNSPEC;
2202 nfmsg->version = NFNETLINK_V0;
2203 nfmsg->res_id = htons(cpu);
2205 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2206 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2207 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2208 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2209 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2210 htonl(st->insert_failed)) ||
2211 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2212 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2213 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2214 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2215 htonl(st->search_restart)))
2216 goto nla_put_failure;
2218 nlmsg_end(skb, nlh);
2223 nlmsg_cancel(skb, nlh);
2228 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2231 struct net *net = sock_net(skb->sk);
2233 if (cb->args[0] == nr_cpu_ids)
2236 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2237 const struct ip_conntrack_stat *st;
2239 if (!cpu_possible(cpu))
2242 st = per_cpu_ptr(net->ct.stat, cpu);
2243 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2244 NETLINK_CB(cb->skb).portid,
2254 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2255 struct sk_buff *skb,
2256 const struct nlmsghdr *nlh,
2257 const struct nlattr * const cda[],
2258 struct netlink_ext_ack *extack)
2260 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2261 struct netlink_dump_control c = {
2262 .dump = ctnetlink_ct_stat_cpu_dump,
2264 return netlink_dump_start(ctnl, skb, nlh, &c);
2271 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2274 struct nlmsghdr *nlh;
2275 struct nfgenmsg *nfmsg;
2276 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2277 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2279 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2280 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2284 nfmsg = nlmsg_data(nlh);
2285 nfmsg->nfgen_family = AF_UNSPEC;
2286 nfmsg->version = NFNETLINK_V0;
2289 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2290 goto nla_put_failure;
2292 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2293 goto nla_put_failure;
2295 nlmsg_end(skb, nlh);
2300 nlmsg_cancel(skb, nlh);
2304 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2305 struct sk_buff *skb, const struct nlmsghdr *nlh,
2306 const struct nlattr * const cda[],
2307 struct netlink_ext_ack *extack)
2309 struct sk_buff *skb2;
2312 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2316 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2318 NFNL_MSG_TYPE(nlh->nlmsg_type),
2323 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2332 /* this avoids a loop in nfnetlink. */
2333 return err == -EAGAIN ? -ENOBUFS : err;
2336 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2337 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2338 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2339 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2340 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2341 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2342 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2343 .len = NF_CT_HELPER_NAME_LEN - 1 },
2344 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2345 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2346 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2347 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2348 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2351 static struct nf_conntrack_expect *
2352 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2353 struct nf_conntrack_helper *helper,
2354 struct nf_conntrack_tuple *tuple,
2355 struct nf_conntrack_tuple *mask);
2357 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2359 ctnetlink_glue_build_size(const struct nf_conn *ct)
2361 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2362 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2363 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2364 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2365 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2366 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2367 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2368 + nla_total_size(0) /* CTA_PROTOINFO */
2369 + nla_total_size(0) /* CTA_HELP */
2370 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2371 + ctnetlink_secctx_size(ct)
2372 #ifdef CONFIG_NF_NAT_NEEDED
2373 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2374 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2376 #ifdef CONFIG_NF_CONNTRACK_MARK
2377 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2379 #ifdef CONFIG_NF_CONNTRACK_ZONES
2380 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2382 + ctnetlink_proto_size(ct)
2386 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2387 enum ip_conntrack_info *ctinfo)
2389 return nf_ct_get(skb, ctinfo);
2392 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2394 const struct nf_conntrack_zone *zone;
2395 struct nlattr *nest_parms;
2397 zone = nf_ct_zone(ct);
2399 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2401 goto nla_put_failure;
2402 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2403 goto nla_put_failure;
2404 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2405 NF_CT_ZONE_DIR_ORIG) < 0)
2406 goto nla_put_failure;
2407 nla_nest_end(skb, nest_parms);
2409 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2411 goto nla_put_failure;
2412 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2413 goto nla_put_failure;
2414 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2415 NF_CT_ZONE_DIR_REPL) < 0)
2416 goto nla_put_failure;
2417 nla_nest_end(skb, nest_parms);
2419 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2420 NF_CT_DEFAULT_ZONE_DIR) < 0)
2421 goto nla_put_failure;
2423 if (ctnetlink_dump_id(skb, ct) < 0)
2424 goto nla_put_failure;
2426 if (ctnetlink_dump_status(skb, ct) < 0)
2427 goto nla_put_failure;
2429 if (ctnetlink_dump_timeout(skb, ct) < 0)
2430 goto nla_put_failure;
2432 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2433 goto nla_put_failure;
2435 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2436 goto nla_put_failure;
2438 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2439 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2440 goto nla_put_failure;
2442 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2443 goto nla_put_failure;
2445 if ((ct->status & IPS_SEQ_ADJUST) &&
2446 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2447 goto nla_put_failure;
2449 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2450 goto nla_put_failure;
2452 #ifdef CONFIG_NF_CONNTRACK_MARK
2453 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2454 goto nla_put_failure;
2456 if (ctnetlink_dump_labels(skb, ct) < 0)
2457 goto nla_put_failure;
2465 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2466 enum ip_conntrack_info ctinfo,
2467 u_int16_t ct_attr, u_int16_t ct_info_attr)
2469 struct nlattr *nest_parms;
2471 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2473 goto nla_put_failure;
2475 if (__ctnetlink_glue_build(skb, ct) < 0)
2476 goto nla_put_failure;
2478 nla_nest_end(skb, nest_parms);
2480 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2481 goto nla_put_failure;
2490 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2492 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2493 unsigned long d = ct->status ^ status;
2495 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2496 /* SEEN_REPLY bit can only be set */
2499 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2500 /* ASSURED bit can only be set */
2503 /* This check is less strict than ctnetlink_change_status()
2504 * because callers often flip IPS_EXPECTED bits when sending
2505 * an NFQA_CT attribute to the kernel. So ignore the
2506 * unchangeable bits but do not error out. Also user programs
2507 * are allowed to clear the bits that they are allowed to change.
2509 __ctnetlink_change_status(ct, status, ~status);
2514 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2518 if (cda[CTA_TIMEOUT]) {
2519 err = ctnetlink_change_timeout(ct, cda);
2523 if (cda[CTA_STATUS]) {
2524 err = ctnetlink_update_status(ct, cda);
2528 if (cda[CTA_HELP]) {
2529 err = ctnetlink_change_helper(ct, cda);
2533 if (cda[CTA_LABELS]) {
2534 err = ctnetlink_attach_labels(ct, cda);
2538 #if defined(CONFIG_NF_CONNTRACK_MARK)
2539 if (cda[CTA_MARK]) {
2540 ctnetlink_change_mark(ct, cda);
2547 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2549 struct nlattr *cda[CTA_MAX+1];
2552 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2556 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2559 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2560 const struct nf_conn *ct,
2561 struct nf_conntrack_tuple *tuple,
2562 struct nf_conntrack_tuple *mask)
2566 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2567 nf_ct_l3num(ct), NULL);
2571 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2572 nf_ct_l3num(ct), NULL);
2576 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2577 u32 portid, u32 report)
2579 struct nlattr *cda[CTA_EXPECT_MAX+1];
2580 struct nf_conntrack_tuple tuple, mask;
2581 struct nf_conntrack_helper *helper = NULL;
2582 struct nf_conntrack_expect *exp;
2585 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2590 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2595 if (cda[CTA_EXPECT_HELP_NAME]) {
2596 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2598 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2599 nf_ct_protonum(ct));
2604 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2605 helper, &tuple, &mask);
2607 return PTR_ERR(exp);
2609 err = nf_ct_expect_related_report(exp, portid, report);
2610 nf_ct_expect_put(exp);
2614 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2615 enum ip_conntrack_info ctinfo, int diff)
2617 if (!(ct->status & IPS_NAT_MASK))
2620 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2623 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2624 .get_ct = ctnetlink_glue_get_ct,
2625 .build_size = ctnetlink_glue_build_size,
2626 .build = ctnetlink_glue_build,
2627 .parse = ctnetlink_glue_parse,
2628 .attach_expect = ctnetlink_glue_attach_expect,
2629 .seq_adjust = ctnetlink_glue_seqadj,
2631 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2633 /***********************************************************************
2635 ***********************************************************************/
2637 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2638 const struct nf_conntrack_tuple *tuple,
2641 struct nlattr *nest_parms;
2643 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2645 goto nla_put_failure;
2646 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2647 goto nla_put_failure;
2648 nla_nest_end(skb, nest_parms);
2656 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2657 const struct nf_conntrack_tuple *tuple,
2658 const struct nf_conntrack_tuple_mask *mask)
2660 const struct nf_conntrack_l4proto *l4proto;
2661 struct nf_conntrack_tuple m;
2662 struct nlattr *nest_parms;
2665 memset(&m, 0xFF, sizeof(m));
2666 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2667 m.src.u.all = mask->src.u.all;
2668 m.dst.protonum = tuple->dst.protonum;
2670 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2672 goto nla_put_failure;
2675 ret = ctnetlink_dump_tuples_ip(skb, &m);
2677 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
2678 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2682 if (unlikely(ret < 0))
2683 goto nla_put_failure;
2685 nla_nest_end(skb, nest_parms);
2693 static const union nf_inet_addr any_addr;
2696 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2697 const struct nf_conntrack_expect *exp)
2699 struct nf_conn *master = exp->master;
2700 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2701 struct nf_conn_help *help;
2702 #ifdef CONFIG_NF_NAT_NEEDED
2703 struct nlattr *nest_parms;
2704 struct nf_conntrack_tuple nat_tuple = {};
2706 struct nf_ct_helper_expectfn *expfn;
2711 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2712 goto nla_put_failure;
2713 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2714 goto nla_put_failure;
2715 if (ctnetlink_exp_dump_tuple(skb,
2716 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2717 CTA_EXPECT_MASTER) < 0)
2718 goto nla_put_failure;
2720 #ifdef CONFIG_NF_NAT_NEEDED
2721 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2722 exp->saved_proto.all) {
2723 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2725 goto nla_put_failure;
2727 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2728 goto nla_put_failure;
2730 nat_tuple.src.l3num = nf_ct_l3num(master);
2731 nat_tuple.src.u3 = exp->saved_addr;
2732 nat_tuple.dst.protonum = nf_ct_protonum(master);
2733 nat_tuple.src.u = exp->saved_proto;
2735 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2736 CTA_EXPECT_NAT_TUPLE) < 0)
2737 goto nla_put_failure;
2738 nla_nest_end(skb, nest_parms);
2741 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2742 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2743 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2744 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2745 goto nla_put_failure;
2746 help = nfct_help(master);
2748 struct nf_conntrack_helper *helper;
2750 helper = rcu_dereference(help->helper);
2752 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2753 goto nla_put_failure;
2755 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2756 if (expfn != NULL &&
2757 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2758 goto nla_put_failure;
2767 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2768 int event, const struct nf_conntrack_expect *exp)
2770 struct nlmsghdr *nlh;
2771 struct nfgenmsg *nfmsg;
2772 unsigned int flags = portid ? NLM_F_MULTI : 0;
2774 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2775 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2779 nfmsg = nlmsg_data(nlh);
2780 nfmsg->nfgen_family = exp->tuple.src.l3num;
2781 nfmsg->version = NFNETLINK_V0;
2784 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2785 goto nla_put_failure;
2787 nlmsg_end(skb, nlh);
2792 nlmsg_cancel(skb, nlh);
2796 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2798 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2800 struct nf_conntrack_expect *exp = item->exp;
2801 struct net *net = nf_ct_exp_net(exp);
2802 struct nlmsghdr *nlh;
2803 struct nfgenmsg *nfmsg;
2804 struct sk_buff *skb;
2805 unsigned int type, group;
2808 if (events & (1 << IPEXP_DESTROY)) {
2809 type = IPCTNL_MSG_EXP_DELETE;
2810 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2811 } else if (events & (1 << IPEXP_NEW)) {
2812 type = IPCTNL_MSG_EXP_NEW;
2813 flags = NLM_F_CREATE|NLM_F_EXCL;
2814 group = NFNLGRP_CONNTRACK_EXP_NEW;
2818 if (!item->report && !nfnetlink_has_listeners(net, group))
2821 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2825 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2826 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2830 nfmsg = nlmsg_data(nlh);
2831 nfmsg->nfgen_family = exp->tuple.src.l3num;
2832 nfmsg->version = NFNETLINK_V0;
2835 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2836 goto nla_put_failure;
2838 nlmsg_end(skb, nlh);
2839 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2843 nlmsg_cancel(skb, nlh);
2847 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2851 static int ctnetlink_exp_done(struct netlink_callback *cb)
2854 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2859 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2861 struct net *net = sock_net(skb->sk);
2862 struct nf_conntrack_expect *exp, *last;
2863 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2864 u_int8_t l3proto = nfmsg->nfgen_family;
2867 last = (struct nf_conntrack_expect *)cb->args[1];
2868 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2870 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2872 if (l3proto && exp->tuple.src.l3num != l3proto)
2875 if (!net_eq(nf_ct_net(exp->master), net))
2883 if (ctnetlink_exp_fill_info(skb,
2884 NETLINK_CB(cb->skb).portid,
2888 if (!refcount_inc_not_zero(&exp->use))
2890 cb->args[1] = (unsigned long)exp;
2902 nf_ct_expect_put(last);
2908 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2910 struct nf_conntrack_expect *exp, *last;
2911 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2912 struct nf_conn *ct = cb->data;
2913 struct nf_conn_help *help = nfct_help(ct);
2914 u_int8_t l3proto = nfmsg->nfgen_family;
2920 last = (struct nf_conntrack_expect *)cb->args[1];
2922 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2923 if (l3proto && exp->tuple.src.l3num != l3proto)
2930 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2934 if (!refcount_inc_not_zero(&exp->use))
2936 cb->args[1] = (unsigned long)exp;
2948 nf_ct_expect_put(last);
2953 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2954 struct sk_buff *skb,
2955 const struct nlmsghdr *nlh,
2956 const struct nlattr * const cda[],
2957 struct netlink_ext_ack *extack)
2960 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2961 u_int8_t u3 = nfmsg->nfgen_family;
2962 struct nf_conntrack_tuple tuple;
2963 struct nf_conntrack_tuple_hash *h;
2965 struct nf_conntrack_zone zone;
2966 struct netlink_dump_control c = {
2967 .dump = ctnetlink_exp_ct_dump_table,
2968 .done = ctnetlink_exp_done,
2971 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2976 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2980 h = nf_conntrack_find_get(net, &zone, &tuple);
2984 ct = nf_ct_tuplehash_to_ctrack(h);
2985 /* No expectation linked to this connection tracking. */
2986 if (!nfct_help(ct)) {
2993 err = netlink_dump_start(ctnl, skb, nlh, &c);
2999 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
3000 struct sk_buff *skb, const struct nlmsghdr *nlh,
3001 const struct nlattr * const cda[],
3002 struct netlink_ext_ack *extack)
3004 struct nf_conntrack_tuple tuple;
3005 struct nf_conntrack_expect *exp;
3006 struct sk_buff *skb2;
3007 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3008 u_int8_t u3 = nfmsg->nfgen_family;
3009 struct nf_conntrack_zone zone;
3012 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3013 if (cda[CTA_EXPECT_MASTER])
3014 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
3017 struct netlink_dump_control c = {
3018 .dump = ctnetlink_exp_dump_table,
3019 .done = ctnetlink_exp_done,
3021 return netlink_dump_start(ctnl, skb, nlh, &c);
3025 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3029 if (cda[CTA_EXPECT_TUPLE])
3030 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3032 else if (cda[CTA_EXPECT_MASTER])
3033 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3041 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3045 if (cda[CTA_EXPECT_ID]) {
3046 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3047 if (ntohl(id) != (u32)(unsigned long)exp) {
3048 nf_ct_expect_put(exp);
3054 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3056 nf_ct_expect_put(exp);
3061 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3062 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3064 nf_ct_expect_put(exp);
3068 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3077 /* this avoids a loop in nfnetlink. */
3078 return err == -EAGAIN ? -ENOBUFS : err;
3081 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3083 const struct nf_conn_help *m_help;
3084 const char *name = data;
3086 m_help = nfct_help(exp->master);
3088 return strcmp(m_help->helper->name, name) == 0;
3091 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3096 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3097 struct sk_buff *skb, const struct nlmsghdr *nlh,
3098 const struct nlattr * const cda[],
3099 struct netlink_ext_ack *extack)
3101 struct nf_conntrack_expect *exp;
3102 struct nf_conntrack_tuple tuple;
3103 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3104 u_int8_t u3 = nfmsg->nfgen_family;
3105 struct nf_conntrack_zone zone;
3108 if (cda[CTA_EXPECT_TUPLE]) {
3109 /* delete a single expect by tuple */
3110 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3114 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3119 /* bump usage count to 2 */
3120 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3124 if (cda[CTA_EXPECT_ID]) {
3125 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3126 if (ntohl(id) != (u32)(unsigned long)exp) {
3127 nf_ct_expect_put(exp);
3132 /* after list removal, usage count == 1 */
3133 spin_lock_bh(&nf_conntrack_expect_lock);
3134 if (del_timer(&exp->timeout)) {
3135 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3137 nf_ct_expect_put(exp);
3139 spin_unlock_bh(&nf_conntrack_expect_lock);
3140 /* have to put what we 'get' above.
3141 * after this line usage count == 0 */
3142 nf_ct_expect_put(exp);
3143 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3144 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3146 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3147 NETLINK_CB(skb).portid,
3150 /* This basically means we have to flush everything*/
3151 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3152 NETLINK_CB(skb).portid,
3159 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3160 const struct nlattr * const cda[])
3162 if (cda[CTA_EXPECT_TIMEOUT]) {
3163 if (!del_timer(&x->timeout))
3166 x->timeout.expires = jiffies +
3167 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3168 add_timer(&x->timeout);
3173 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3174 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3175 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3179 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3180 struct nf_conntrack_expect *exp,
3183 #ifdef CONFIG_NF_NAT_NEEDED
3184 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3185 struct nf_conntrack_tuple nat_tuple = {};
3188 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3189 exp_nat_nla_policy, NULL);
3193 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3196 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3197 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3202 exp->saved_addr = nat_tuple.src.u3;
3203 exp->saved_proto = nat_tuple.src.u;
3204 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3212 static struct nf_conntrack_expect *
3213 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3214 struct nf_conntrack_helper *helper,
3215 struct nf_conntrack_tuple *tuple,
3216 struct nf_conntrack_tuple *mask)
3218 u_int32_t class = 0;
3219 struct nf_conntrack_expect *exp;
3220 struct nf_conn_help *help;
3223 help = nfct_help(ct);
3225 return ERR_PTR(-EOPNOTSUPP);
3227 if (cda[CTA_EXPECT_CLASS] && helper) {
3228 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3229 if (class > helper->expect_class_max)
3230 return ERR_PTR(-EINVAL);
3232 exp = nf_ct_expect_alloc(ct);
3234 return ERR_PTR(-ENOMEM);
3236 if (cda[CTA_EXPECT_FLAGS]) {
3237 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3238 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3242 if (cda[CTA_EXPECT_FN]) {
3243 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3244 struct nf_ct_helper_expectfn *expfn;
3246 expfn = nf_ct_helper_expectfn_find_by_name(name);
3247 if (expfn == NULL) {
3251 exp->expectfn = expfn->expectfn;
3253 exp->expectfn = NULL;
3257 exp->helper = helper;
3258 exp->tuple = *tuple;
3259 exp->mask.src.u3 = mask->src.u3;
3260 exp->mask.src.u.all = mask->src.u.all;
3262 if (cda[CTA_EXPECT_NAT]) {
3263 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3264 exp, nf_ct_l3num(ct));
3270 nf_ct_expect_put(exp);
3271 return ERR_PTR(err);
3275 ctnetlink_create_expect(struct net *net,
3276 const struct nf_conntrack_zone *zone,
3277 const struct nlattr * const cda[],
3278 u_int8_t u3, u32 portid, int report)
3280 struct nf_conntrack_tuple tuple, mask, master_tuple;
3281 struct nf_conntrack_tuple_hash *h = NULL;
3282 struct nf_conntrack_helper *helper = NULL;
3283 struct nf_conntrack_expect *exp;
3287 /* caller guarantees that those three CTA_EXPECT_* exist */
3288 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3292 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3296 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3301 /* Look for master conntrack of this expectation */
3302 h = nf_conntrack_find_get(net, zone, &master_tuple);
3305 ct = nf_ct_tuplehash_to_ctrack(h);
3308 if (cda[CTA_EXPECT_HELP_NAME]) {
3309 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3311 helper = __nf_conntrack_helper_find(helpname, u3,
3312 nf_ct_protonum(ct));
3313 if (helper == NULL) {
3315 #ifdef CONFIG_MODULES
3316 if (request_module("nfct-helper-%s", helpname) < 0) {
3321 helper = __nf_conntrack_helper_find(helpname, u3,
3322 nf_ct_protonum(ct));
3334 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3340 err = nf_ct_expect_related_report(exp, portid, report);
3341 nf_ct_expect_put(exp);
3349 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3350 struct sk_buff *skb, const struct nlmsghdr *nlh,
3351 const struct nlattr * const cda[],
3352 struct netlink_ext_ack *extack)
3354 struct nf_conntrack_tuple tuple;
3355 struct nf_conntrack_expect *exp;
3356 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3357 u_int8_t u3 = nfmsg->nfgen_family;
3358 struct nf_conntrack_zone zone;
3361 if (!cda[CTA_EXPECT_TUPLE]
3362 || !cda[CTA_EXPECT_MASK]
3363 || !cda[CTA_EXPECT_MASTER])
3366 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3370 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3375 spin_lock_bh(&nf_conntrack_expect_lock);
3376 exp = __nf_ct_expect_find(net, &zone, &tuple);
3378 spin_unlock_bh(&nf_conntrack_expect_lock);
3380 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3381 err = ctnetlink_create_expect(net, &zone, cda, u3,
3382 NETLINK_CB(skb).portid,
3389 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3390 err = ctnetlink_change_expect(exp, cda);
3391 spin_unlock_bh(&nf_conntrack_expect_lock);
3397 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3398 const struct ip_conntrack_stat *st)
3400 struct nlmsghdr *nlh;
3401 struct nfgenmsg *nfmsg;
3402 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3404 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3405 IPCTNL_MSG_EXP_GET_STATS_CPU);
3406 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3410 nfmsg = nlmsg_data(nlh);
3411 nfmsg->nfgen_family = AF_UNSPEC;
3412 nfmsg->version = NFNETLINK_V0;
3413 nfmsg->res_id = htons(cpu);
3415 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3416 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3417 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3418 goto nla_put_failure;
3420 nlmsg_end(skb, nlh);
3425 nlmsg_cancel(skb, nlh);
3430 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3433 struct net *net = sock_net(skb->sk);
3435 if (cb->args[0] == nr_cpu_ids)
3438 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3439 const struct ip_conntrack_stat *st;
3441 if (!cpu_possible(cpu))
3444 st = per_cpu_ptr(net->ct.stat, cpu);
3445 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3455 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3456 struct sk_buff *skb,
3457 const struct nlmsghdr *nlh,
3458 const struct nlattr * const cda[],
3459 struct netlink_ext_ack *extack)
3461 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3462 struct netlink_dump_control c = {
3463 .dump = ctnetlink_exp_stat_cpu_dump,
3465 return netlink_dump_start(ctnl, skb, nlh, &c);
3471 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3472 static struct nf_ct_event_notifier ctnl_notifier = {
3473 .fcn = ctnetlink_conntrack_event,
3476 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3477 .fcn = ctnetlink_expect_event,
3481 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3482 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3483 .attr_count = CTA_MAX,
3484 .policy = ct_nla_policy },
3485 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3486 .attr_count = CTA_MAX,
3487 .policy = ct_nla_policy },
3488 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3489 .attr_count = CTA_MAX,
3490 .policy = ct_nla_policy },
3491 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3492 .attr_count = CTA_MAX,
3493 .policy = ct_nla_policy },
3494 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3495 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3496 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3497 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3500 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3501 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3502 .attr_count = CTA_EXPECT_MAX,
3503 .policy = exp_nla_policy },
3504 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3505 .attr_count = CTA_EXPECT_MAX,
3506 .policy = exp_nla_policy },
3507 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3508 .attr_count = CTA_EXPECT_MAX,
3509 .policy = exp_nla_policy },
3510 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3513 static const struct nfnetlink_subsystem ctnl_subsys = {
3514 .name = "conntrack",
3515 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3516 .cb_count = IPCTNL_MSG_MAX,
3520 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3521 .name = "conntrack_expect",
3522 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3523 .cb_count = IPCTNL_MSG_EXP_MAX,
3527 MODULE_ALIAS("ip_conntrack_netlink");
3528 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3529 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3531 static int __net_init ctnetlink_net_init(struct net *net)
3533 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3536 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3538 pr_err("ctnetlink_init: cannot register notifier.\n");
3542 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3544 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3545 goto err_unreg_notifier;
3550 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3552 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3558 static void ctnetlink_net_exit(struct net *net)
3560 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3561 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3562 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3566 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3570 list_for_each_entry(net, net_exit_list, exit_list)
3571 ctnetlink_net_exit(net);
3574 static struct pernet_operations ctnetlink_net_ops = {
3575 .init = ctnetlink_net_init,
3576 .exit_batch = ctnetlink_net_exit_batch,
3579 static int __init ctnetlink_init(void)
3583 ret = nfnetlink_subsys_register(&ctnl_subsys);
3585 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3589 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3591 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3592 goto err_unreg_subsys;
3595 ret = register_pernet_subsys(&ctnetlink_net_ops);
3597 pr_err("ctnetlink_init: cannot register pernet operations\n");
3598 goto err_unreg_exp_subsys;
3600 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3601 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3602 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3606 err_unreg_exp_subsys:
3607 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3609 nfnetlink_subsys_unregister(&ctnl_subsys);
3614 static void __exit ctnetlink_exit(void)
3616 unregister_pernet_subsys(&ctnetlink_net_ops);
3617 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3618 nfnetlink_subsys_unregister(&ctnl_subsys);
3619 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3620 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3625 module_init(ctnetlink_init);
3626 module_exit(ctnetlink_exit);