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-2011 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_l3proto.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 #ifdef CONFIG_NF_NAT_NEEDED
47 #include <net/netfilter/nf_nat_core.h>
48 #include <net/netfilter/nf_nat_protocol.h>
51 #include <linux/netfilter/nfnetlink.h>
52 #include <linux/netfilter/nfnetlink_conntrack.h>
54 MODULE_LICENSE("GPL");
56 static char __initdata version[] = "0.93";
59 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
60 const struct nf_conntrack_tuple *tuple,
61 struct nf_conntrack_l4proto *l4proto)
64 struct nlattr *nest_parms;
66 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
69 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);
83 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
84 const struct nf_conntrack_tuple *tuple,
85 struct nf_conntrack_l3proto *l3proto)
88 struct nlattr *nest_parms;
90 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
94 if (likely(l3proto->tuple_to_nlattr))
95 ret = l3proto->tuple_to_nlattr(skb, tuple);
97 nla_nest_end(skb, nest_parms);
106 ctnetlink_dump_tuples(struct sk_buff *skb,
107 const struct nf_conntrack_tuple *tuple)
110 struct nf_conntrack_l3proto *l3proto;
111 struct nf_conntrack_l4proto *l4proto;
113 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
114 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
116 if (unlikely(ret < 0))
119 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
120 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
126 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
128 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
136 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
138 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
143 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
151 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
153 struct nf_conntrack_l4proto *l4proto;
154 struct nlattr *nest_proto;
157 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
158 if (!l4proto->to_nlattr)
161 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
163 goto nla_put_failure;
165 ret = l4proto->to_nlattr(skb, nest_proto, ct);
167 nla_nest_end(skb, nest_proto);
176 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
178 struct nlattr *nest_helper;
179 const struct nf_conn_help *help = nfct_help(ct);
180 struct nf_conntrack_helper *helper;
185 helper = rcu_dereference(help->helper);
189 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
191 goto nla_put_failure;
192 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
194 if (helper->to_nlattr)
195 helper->to_nlattr(skb, ct);
197 nla_nest_end(skb, nest_helper);
206 dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
207 enum ip_conntrack_dir dir)
209 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
210 struct nlattr *nest_count;
212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
214 goto nla_put_failure;
216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts));
217 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes));
219 nla_nest_end(skb, nest_count);
228 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
229 enum ip_conntrack_dir dir, int type)
231 struct nf_conn_counter *acct;
234 acct = nf_conn_acct_find(ct);
238 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
239 pkts = atomic64_xchg(&acct[dir].packets, 0);
240 bytes = atomic64_xchg(&acct[dir].bytes, 0);
242 pkts = atomic64_read(&acct[dir].packets);
243 bytes = atomic64_read(&acct[dir].bytes);
245 return dump_counters(skb, pkts, bytes, dir);
249 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
251 struct nlattr *nest_count;
252 const struct nf_conn_tstamp *tstamp;
254 tstamp = nf_conn_tstamp_find(ct);
258 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
260 goto nla_put_failure;
262 NLA_PUT_BE64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start));
263 if (tstamp->stop != 0) {
264 NLA_PUT_BE64(skb, CTA_TIMESTAMP_STOP,
265 cpu_to_be64(tstamp->stop));
267 nla_nest_end(skb, nest_count);
275 #ifdef CONFIG_NF_CONNTRACK_MARK
277 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
279 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
286 #define ctnetlink_dump_mark(a, b) (0)
289 #ifdef CONFIG_NF_CONNTRACK_SECMARK
291 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
293 struct nlattr *nest_secctx;
297 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
302 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
304 goto nla_put_failure;
306 NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
307 nla_nest_end(skb, nest_secctx);
311 security_release_secctx(secctx, len);
315 #define ctnetlink_dump_secctx(a, b) (0)
318 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
321 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
323 struct nlattr *nest_parms;
325 if (!(ct->status & IPS_EXPECTED))
328 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
330 goto nla_put_failure;
331 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
332 goto nla_put_failure;
333 nla_nest_end(skb, nest_parms);
341 #ifdef CONFIG_NF_NAT_NEEDED
343 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
345 struct nlattr *nest_parms;
347 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
349 goto nla_put_failure;
351 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
352 htonl(natseq->correction_pos));
353 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
354 htonl(natseq->offset_before));
355 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
356 htonl(natseq->offset_after));
358 nla_nest_end(skb, nest_parms);
367 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
369 struct nf_nat_seq *natseq;
370 struct nf_conn_nat *nat = nfct_nat(ct);
372 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
375 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
376 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
379 natseq = &nat->seq[IP_CT_DIR_REPLY];
380 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
386 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
390 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
392 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
400 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
402 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
410 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, u32 type,
413 struct nlmsghdr *nlh;
414 struct nfgenmsg *nfmsg;
415 struct nlattr *nest_parms;
416 unsigned int flags = pid ? NLM_F_MULTI : 0, event;
418 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
419 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
423 nfmsg = nlmsg_data(nlh);
424 nfmsg->nfgen_family = nf_ct_l3num(ct);
425 nfmsg->version = NFNETLINK_V0;
428 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
430 goto nla_put_failure;
431 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
432 goto nla_put_failure;
433 nla_nest_end(skb, nest_parms);
435 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
437 goto nla_put_failure;
438 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
439 goto nla_put_failure;
440 nla_nest_end(skb, nest_parms);
443 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
445 if (ctnetlink_dump_status(skb, ct) < 0 ||
446 ctnetlink_dump_timeout(skb, ct) < 0 ||
447 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
448 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
449 ctnetlink_dump_timestamp(skb, ct) < 0 ||
450 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
451 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
452 ctnetlink_dump_mark(skb, ct) < 0 ||
453 ctnetlink_dump_secctx(skb, ct) < 0 ||
454 ctnetlink_dump_id(skb, ct) < 0 ||
455 ctnetlink_dump_use(skb, ct) < 0 ||
456 ctnetlink_dump_master(skb, ct) < 0 ||
457 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
458 goto nla_put_failure;
465 nlmsg_cancel(skb, nlh);
469 #ifdef CONFIG_NF_CONNTRACK_EVENTS
471 ctnetlink_proto_size(const struct nf_conn *ct)
473 struct nf_conntrack_l3proto *l3proto;
474 struct nf_conntrack_l4proto *l4proto;
478 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
479 len += l3proto->nla_size;
481 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
482 len += l4proto->nla_size;
489 ctnetlink_counters_size(const struct nf_conn *ct)
491 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
493 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
494 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
495 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
500 ctnetlink_secctx_size(const struct nf_conn *ct)
502 #ifdef CONFIG_NF_CONNTRACK_SECMARK
505 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
509 return nla_total_size(0) /* CTA_SECCTX */
510 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
517 ctnetlink_timestamp_size(const struct nf_conn *ct)
519 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
520 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
522 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
529 ctnetlink_nlmsg_size(const struct nf_conn *ct)
531 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
532 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
533 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
534 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
535 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
536 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
537 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
538 + ctnetlink_counters_size(ct)
539 + ctnetlink_timestamp_size(ct)
540 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
541 + nla_total_size(0) /* CTA_PROTOINFO */
542 + nla_total_size(0) /* CTA_HELP */
543 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
544 + ctnetlink_secctx_size(ct)
545 #ifdef CONFIG_NF_NAT_NEEDED
546 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
547 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
549 #ifdef CONFIG_NF_CONNTRACK_MARK
550 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
552 + ctnetlink_proto_size(ct)
557 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
560 struct nlmsghdr *nlh;
561 struct nfgenmsg *nfmsg;
562 struct nlattr *nest_parms;
563 struct nf_conn *ct = item->ct;
566 unsigned int flags = 0, group;
569 /* ignore our fake conntrack entry */
570 if (nf_ct_is_untracked(ct))
573 if (events & (1 << IPCT_DESTROY)) {
574 type = IPCTNL_MSG_CT_DELETE;
575 group = NFNLGRP_CONNTRACK_DESTROY;
576 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
577 type = IPCTNL_MSG_CT_NEW;
578 flags = NLM_F_CREATE|NLM_F_EXCL;
579 group = NFNLGRP_CONNTRACK_NEW;
581 type = IPCTNL_MSG_CT_NEW;
582 group = NFNLGRP_CONNTRACK_UPDATE;
587 if (!item->report && !nfnetlink_has_listeners(net, group))
590 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
594 type |= NFNL_SUBSYS_CTNETLINK << 8;
595 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
599 nfmsg = nlmsg_data(nlh);
600 nfmsg->nfgen_family = nf_ct_l3num(ct);
601 nfmsg->version = NFNETLINK_V0;
605 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
607 goto nla_put_failure;
608 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
609 goto nla_put_failure;
610 nla_nest_end(skb, nest_parms);
612 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
614 goto nla_put_failure;
615 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
616 goto nla_put_failure;
617 nla_nest_end(skb, nest_parms);
620 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
622 if (ctnetlink_dump_id(skb, ct) < 0)
623 goto nla_put_failure;
625 if (ctnetlink_dump_status(skb, ct) < 0)
626 goto nla_put_failure;
628 if (events & (1 << IPCT_DESTROY)) {
629 if (ctnetlink_dump_counters(skb, ct,
630 IP_CT_DIR_ORIGINAL, type) < 0 ||
631 ctnetlink_dump_counters(skb, ct,
632 IP_CT_DIR_REPLY, type) < 0 ||
633 ctnetlink_dump_timestamp(skb, ct) < 0)
634 goto nla_put_failure;
636 if (ctnetlink_dump_timeout(skb, ct) < 0)
637 goto nla_put_failure;
639 if (events & (1 << IPCT_PROTOINFO)
640 && ctnetlink_dump_protoinfo(skb, ct) < 0)
641 goto nla_put_failure;
643 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
644 && ctnetlink_dump_helpinfo(skb, ct) < 0)
645 goto nla_put_failure;
647 #ifdef CONFIG_NF_CONNTRACK_SECMARK
648 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
649 && ctnetlink_dump_secctx(skb, ct) < 0)
650 goto nla_put_failure;
653 if (events & (1 << IPCT_RELATED) &&
654 ctnetlink_dump_master(skb, ct) < 0)
655 goto nla_put_failure;
657 if (events & (1 << IPCT_NATSEQADJ) &&
658 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
659 goto nla_put_failure;
662 #ifdef CONFIG_NF_CONNTRACK_MARK
663 if ((events & (1 << IPCT_MARK) || ct->mark)
664 && ctnetlink_dump_mark(skb, ct) < 0)
665 goto nla_put_failure;
670 err = nfnetlink_send(skb, net, item->pid, group, item->report,
672 if (err == -ENOBUFS || err == -EAGAIN)
679 nlmsg_cancel(skb, nlh);
683 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
688 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
690 static int ctnetlink_done(struct netlink_callback *cb)
693 nf_ct_put((struct nf_conn *)cb->args[1]);
698 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
700 struct net *net = sock_net(skb->sk);
701 struct nf_conn *ct, *last;
702 struct nf_conntrack_tuple_hash *h;
703 struct hlist_nulls_node *n;
704 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
705 u_int8_t l3proto = nfmsg->nfgen_family;
707 spin_lock_bh(&nf_conntrack_lock);
708 last = (struct nf_conn *)cb->args[1];
709 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
711 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
713 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
715 ct = nf_ct_tuplehash_to_ctrack(h);
716 /* Dump entries of a given L3 protocol number.
717 * If it is not specified, ie. l3proto == 0,
718 * then dump everything. */
719 if (l3proto && nf_ct_l3num(ct) != l3proto)
726 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
729 cb->nlh->nlmsg_type),
731 nf_conntrack_get(&ct->ct_general);
732 cb->args[1] = (unsigned long)ct;
742 spin_unlock_bh(&nf_conntrack_lock);
750 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
752 struct nlattr *tb[CTA_IP_MAX+1];
753 struct nf_conntrack_l3proto *l3proto;
756 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
759 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
761 if (likely(l3proto->nlattr_to_tuple)) {
762 ret = nla_validate_nested(attr, CTA_IP_MAX,
763 l3proto->nla_policy);
765 ret = l3proto->nlattr_to_tuple(tb, tuple);
773 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
774 [CTA_PROTO_NUM] = { .type = NLA_U8 },
778 ctnetlink_parse_tuple_proto(struct nlattr *attr,
779 struct nf_conntrack_tuple *tuple)
781 struct nlattr *tb[CTA_PROTO_MAX+1];
782 struct nf_conntrack_l4proto *l4proto;
785 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
789 if (!tb[CTA_PROTO_NUM])
791 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
794 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
796 if (likely(l4proto->nlattr_to_tuple)) {
797 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
798 l4proto->nla_policy);
800 ret = l4proto->nlattr_to_tuple(tb, tuple);
808 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
809 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
810 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
814 ctnetlink_parse_tuple(const struct nlattr * const cda[],
815 struct nf_conntrack_tuple *tuple,
816 enum ctattr_type type, u_int8_t l3num)
818 struct nlattr *tb[CTA_TUPLE_MAX+1];
821 memset(tuple, 0, sizeof(*tuple));
823 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
825 if (!tb[CTA_TUPLE_IP])
828 tuple->src.l3num = l3num;
830 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
834 if (!tb[CTA_TUPLE_PROTO])
837 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
841 /* orig and expect tuples get DIR_ORIGINAL */
842 if (type == CTA_TUPLE_REPLY)
843 tuple->dst.dir = IP_CT_DIR_REPLY;
845 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
851 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
854 #ifdef CONFIG_NF_CONNTRACK_ZONES
855 *zone = ntohs(nla_get_be16(attr));
865 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
866 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
870 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
872 struct nlattr *tb[CTA_HELP_MAX+1];
874 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
876 if (!tb[CTA_HELP_NAME])
879 *helper_name = nla_data(tb[CTA_HELP_NAME]);
884 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
885 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
886 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
887 [CTA_STATUS] = { .type = NLA_U32 },
888 [CTA_PROTOINFO] = { .type = NLA_NESTED },
889 [CTA_HELP] = { .type = NLA_NESTED },
890 [CTA_NAT_SRC] = { .type = NLA_NESTED },
891 [CTA_TIMEOUT] = { .type = NLA_U32 },
892 [CTA_MARK] = { .type = NLA_U32 },
893 [CTA_ID] = { .type = NLA_U32 },
894 [CTA_NAT_DST] = { .type = NLA_NESTED },
895 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
896 [CTA_ZONE] = { .type = NLA_U16 },
900 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
901 const struct nlmsghdr *nlh,
902 const struct nlattr * const cda[])
904 struct net *net = sock_net(ctnl);
905 struct nf_conntrack_tuple_hash *h;
906 struct nf_conntrack_tuple tuple;
908 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
909 u_int8_t u3 = nfmsg->nfgen_family;
913 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
917 if (cda[CTA_TUPLE_ORIG])
918 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
919 else if (cda[CTA_TUPLE_REPLY])
920 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
922 /* Flush the whole table */
923 nf_conntrack_flush_report(net,
932 h = nf_conntrack_find_get(net, zone, &tuple);
936 ct = nf_ct_tuplehash_to_ctrack(h);
939 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
940 if (id != (u32)(unsigned long)ct) {
946 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
948 nlmsg_report(nlh)) < 0) {
949 nf_ct_delete_from_lists(ct);
950 /* we failed to report the event, try later */
951 nf_ct_insert_dying_list(ct);
956 /* death_by_timeout would report the event again */
957 set_bit(IPS_DYING_BIT, &ct->status);
966 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
967 const struct nlmsghdr *nlh,
968 const struct nlattr * const cda[])
970 struct net *net = sock_net(ctnl);
971 struct nf_conntrack_tuple_hash *h;
972 struct nf_conntrack_tuple tuple;
974 struct sk_buff *skb2 = NULL;
975 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
976 u_int8_t u3 = nfmsg->nfgen_family;
980 if (nlh->nlmsg_flags & NLM_F_DUMP) {
981 struct netlink_dump_control c = {
982 .dump = ctnetlink_dump_table,
983 .done = ctnetlink_done,
985 return netlink_dump_start(ctnl, skb, nlh, &c);
988 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
992 if (cda[CTA_TUPLE_ORIG])
993 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
994 else if (cda[CTA_TUPLE_REPLY])
995 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1002 h = nf_conntrack_find_get(net, zone, &tuple);
1006 ct = nf_ct_tuplehash_to_ctrack(h);
1009 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1016 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
1017 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1023 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1032 /* this avoids a loop in nfnetlink. */
1033 return err == -EAGAIN ? -ENOBUFS : err;
1036 #ifdef CONFIG_NF_NAT_NEEDED
1038 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1039 enum nf_nat_manip_type manip,
1040 const struct nlattr *attr)
1042 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1044 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1045 if (!parse_nat_setup) {
1046 #ifdef CONFIG_MODULES
1048 spin_unlock_bh(&nf_conntrack_lock);
1050 if (request_module("nf-nat-ipv4") < 0) {
1052 spin_lock_bh(&nf_conntrack_lock);
1057 spin_lock_bh(&nf_conntrack_lock);
1059 if (nfnetlink_parse_nat_setup_hook)
1065 return parse_nat_setup(ct, manip, attr);
1070 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1073 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1074 d = ct->status ^ status;
1076 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1080 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1081 /* SEEN_REPLY bit can only be set */
1084 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1085 /* ASSURED bit can only be set */
1088 /* Be careful here, modifying NAT bits can screw up things,
1089 * so don't let users modify them directly if they don't pass
1091 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1096 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1098 #ifdef CONFIG_NF_NAT_NEEDED
1101 if (cda[CTA_NAT_DST]) {
1102 ret = ctnetlink_parse_nat_setup(ct,
1108 if (cda[CTA_NAT_SRC]) {
1109 ret = ctnetlink_parse_nat_setup(ct,
1122 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1124 struct nf_conntrack_helper *helper;
1125 struct nf_conn_help *help = nfct_help(ct);
1126 char *helpname = NULL;
1129 /* don't change helper of sibling connections */
1133 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1137 if (!strcmp(helpname, "")) {
1138 if (help && help->helper) {
1139 /* we had a helper before ... */
1140 nf_ct_remove_expectations(ct);
1141 RCU_INIT_POINTER(help->helper, NULL);
1147 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1148 nf_ct_protonum(ct));
1149 if (helper == NULL) {
1150 #ifdef CONFIG_MODULES
1151 spin_unlock_bh(&nf_conntrack_lock);
1153 if (request_module("nfct-helper-%s", helpname) < 0) {
1154 spin_lock_bh(&nf_conntrack_lock);
1158 spin_lock_bh(&nf_conntrack_lock);
1159 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1160 nf_ct_protonum(ct));
1168 if (help->helper == helper)
1172 /* need to zero data of old helper */
1173 memset(&help->help, 0, sizeof(help->help));
1175 /* we cannot set a helper for an existing conntrack */
1179 rcu_assign_pointer(help->helper, helper);
1185 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1187 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1189 if (!del_timer(&ct->timeout))
1192 ct->timeout.expires = jiffies + timeout * HZ;
1193 add_timer(&ct->timeout);
1198 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1199 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1200 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1201 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1205 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1207 const struct nlattr *attr = cda[CTA_PROTOINFO];
1208 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1209 struct nf_conntrack_l4proto *l4proto;
1212 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1215 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1216 if (l4proto->from_nlattr)
1217 err = l4proto->from_nlattr(tb, ct);
1223 #ifdef CONFIG_NF_NAT_NEEDED
1224 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1225 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1226 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1227 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1231 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1233 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1235 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1237 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1240 natseq->correction_pos =
1241 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1243 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1246 natseq->offset_before =
1247 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1249 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1252 natseq->offset_after =
1253 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1259 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1260 const struct nlattr * const cda[])
1263 struct nf_conn_nat *nat = nfct_nat(ct);
1268 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1269 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1270 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1274 ct->status |= IPS_SEQ_ADJUST;
1277 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1278 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1279 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1283 ct->status |= IPS_SEQ_ADJUST;
1291 ctnetlink_change_conntrack(struct nf_conn *ct,
1292 const struct nlattr * const cda[])
1296 /* only allow NAT changes and master assignation for new conntracks */
1297 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1300 if (cda[CTA_HELP]) {
1301 err = ctnetlink_change_helper(ct, cda);
1306 if (cda[CTA_TIMEOUT]) {
1307 err = ctnetlink_change_timeout(ct, cda);
1312 if (cda[CTA_STATUS]) {
1313 err = ctnetlink_change_status(ct, cda);
1318 if (cda[CTA_PROTOINFO]) {
1319 err = ctnetlink_change_protoinfo(ct, cda);
1324 #if defined(CONFIG_NF_CONNTRACK_MARK)
1326 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1329 #ifdef CONFIG_NF_NAT_NEEDED
1330 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1331 err = ctnetlink_change_nat_seq_adj(ct, cda);
1340 static struct nf_conn *
1341 ctnetlink_create_conntrack(struct net *net, u16 zone,
1342 const struct nlattr * const cda[],
1343 struct nf_conntrack_tuple *otuple,
1344 struct nf_conntrack_tuple *rtuple,
1349 struct nf_conntrack_helper *helper;
1350 struct nf_conn_tstamp *tstamp;
1352 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1354 return ERR_PTR(-ENOMEM);
1356 if (!cda[CTA_TIMEOUT])
1358 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1360 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1363 if (cda[CTA_HELP]) {
1364 char *helpname = NULL;
1366 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1370 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1371 nf_ct_protonum(ct));
1372 if (helper == NULL) {
1374 spin_unlock_bh(&nf_conntrack_lock);
1375 #ifdef CONFIG_MODULES
1376 if (request_module("nfct-helper-%s", helpname) < 0) {
1377 spin_lock_bh(&nf_conntrack_lock);
1382 spin_lock_bh(&nf_conntrack_lock);
1384 helper = __nf_conntrack_helper_find(helpname,
1386 nf_ct_protonum(ct));
1396 struct nf_conn_help *help;
1398 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1404 /* not in hash table yet so not strictly necessary */
1405 RCU_INIT_POINTER(help->helper, helper);
1408 /* try an implicit helper assignation */
1409 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1414 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1415 err = ctnetlink_change_nat(ct, cda);
1420 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1421 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1422 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1423 /* we must add conntrack extensions before confirmation. */
1424 ct->status |= IPS_CONFIRMED;
1426 if (cda[CTA_STATUS]) {
1427 err = ctnetlink_change_status(ct, cda);
1432 #ifdef CONFIG_NF_NAT_NEEDED
1433 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1434 err = ctnetlink_change_nat_seq_adj(ct, cda);
1440 memset(&ct->proto, 0, sizeof(ct->proto));
1441 if (cda[CTA_PROTOINFO]) {
1442 err = ctnetlink_change_protoinfo(ct, cda);
1447 #if defined(CONFIG_NF_CONNTRACK_MARK)
1449 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1452 /* setup master conntrack: this is a confirmed expectation */
1453 if (cda[CTA_TUPLE_MASTER]) {
1454 struct nf_conntrack_tuple master;
1455 struct nf_conntrack_tuple_hash *master_h;
1456 struct nf_conn *master_ct;
1458 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1462 master_h = nf_conntrack_find_get(net, zone, &master);
1463 if (master_h == NULL) {
1467 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1468 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1469 ct->master = master_ct;
1471 tstamp = nf_conn_tstamp_find(ct);
1473 tstamp->start = ktime_to_ns(ktime_get_real());
1475 add_timer(&ct->timeout);
1476 nf_conntrack_hash_insert(ct);
1484 nf_conntrack_free(ct);
1485 return ERR_PTR(err);
1489 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1490 const struct nlmsghdr *nlh,
1491 const struct nlattr * const cda[])
1493 struct net *net = sock_net(ctnl);
1494 struct nf_conntrack_tuple otuple, rtuple;
1495 struct nf_conntrack_tuple_hash *h = NULL;
1496 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1497 u_int8_t u3 = nfmsg->nfgen_family;
1501 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1505 if (cda[CTA_TUPLE_ORIG]) {
1506 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1511 if (cda[CTA_TUPLE_REPLY]) {
1512 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1517 spin_lock_bh(&nf_conntrack_lock);
1518 if (cda[CTA_TUPLE_ORIG])
1519 h = __nf_conntrack_find(net, zone, &otuple);
1520 else if (cda[CTA_TUPLE_REPLY])
1521 h = __nf_conntrack_find(net, zone, &rtuple);
1525 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1527 enum ip_conntrack_events events;
1529 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1536 nf_conntrack_get(&ct->ct_general);
1537 spin_unlock_bh(&nf_conntrack_lock);
1538 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1539 events = IPCT_RELATED;
1543 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1544 (1 << IPCT_ASSURED) |
1545 (1 << IPCT_HELPER) |
1546 (1 << IPCT_PROTOINFO) |
1547 (1 << IPCT_NATSEQADJ) |
1548 (1 << IPCT_MARK) | events,
1549 ct, NETLINK_CB(skb).pid,
1553 spin_unlock_bh(&nf_conntrack_lock);
1557 /* implicit 'else' */
1559 /* We manipulate the conntrack inside the global conntrack table lock,
1560 * so there's no need to increase the refcount */
1562 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1563 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1565 err = ctnetlink_change_conntrack(ct, cda);
1567 nf_conntrack_get(&ct->ct_general);
1568 spin_unlock_bh(&nf_conntrack_lock);
1569 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1570 (1 << IPCT_ASSURED) |
1571 (1 << IPCT_HELPER) |
1572 (1 << IPCT_PROTOINFO) |
1573 (1 << IPCT_NATSEQADJ) |
1575 ct, NETLINK_CB(skb).pid,
1579 spin_unlock_bh(&nf_conntrack_lock);
1585 spin_unlock_bh(&nf_conntrack_lock);
1589 /***********************************************************************
1591 ***********************************************************************/
1594 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1595 const struct nf_conntrack_tuple *tuple,
1596 enum ctattr_expect type)
1598 struct nlattr *nest_parms;
1600 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1602 goto nla_put_failure;
1603 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1604 goto nla_put_failure;
1605 nla_nest_end(skb, nest_parms);
1614 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1615 const struct nf_conntrack_tuple *tuple,
1616 const struct nf_conntrack_tuple_mask *mask)
1619 struct nf_conntrack_l3proto *l3proto;
1620 struct nf_conntrack_l4proto *l4proto;
1621 struct nf_conntrack_tuple m;
1622 struct nlattr *nest_parms;
1624 memset(&m, 0xFF, sizeof(m));
1625 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1626 m.src.u.all = mask->src.u.all;
1627 m.dst.protonum = tuple->dst.protonum;
1629 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1631 goto nla_put_failure;
1633 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1634 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1636 if (unlikely(ret < 0))
1637 goto nla_put_failure;
1639 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1640 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1641 if (unlikely(ret < 0))
1642 goto nla_put_failure;
1644 nla_nest_end(skb, nest_parms);
1653 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1654 const struct nf_conntrack_expect *exp)
1656 struct nf_conn *master = exp->master;
1657 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
1658 struct nf_conn_help *help;
1663 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1664 goto nla_put_failure;
1665 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1666 goto nla_put_failure;
1667 if (ctnetlink_exp_dump_tuple(skb,
1668 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1669 CTA_EXPECT_MASTER) < 0)
1670 goto nla_put_failure;
1672 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1673 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1674 NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1675 help = nfct_help(master);
1677 struct nf_conntrack_helper *helper;
1679 helper = rcu_dereference(help->helper);
1681 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1691 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1692 int event, const struct nf_conntrack_expect *exp)
1694 struct nlmsghdr *nlh;
1695 struct nfgenmsg *nfmsg;
1696 unsigned int flags = pid ? NLM_F_MULTI : 0;
1698 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1699 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1703 nfmsg = nlmsg_data(nlh);
1704 nfmsg->nfgen_family = exp->tuple.src.l3num;
1705 nfmsg->version = NFNETLINK_V0;
1708 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1709 goto nla_put_failure;
1711 nlmsg_end(skb, nlh);
1716 nlmsg_cancel(skb, nlh);
1720 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1722 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1724 struct nf_conntrack_expect *exp = item->exp;
1725 struct net *net = nf_ct_exp_net(exp);
1726 struct nlmsghdr *nlh;
1727 struct nfgenmsg *nfmsg;
1728 struct sk_buff *skb;
1729 unsigned int type, group;
1732 if (events & (1 << IPEXP_DESTROY)) {
1733 type = IPCTNL_MSG_EXP_DELETE;
1734 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1735 } else if (events & (1 << IPEXP_NEW)) {
1736 type = IPCTNL_MSG_EXP_NEW;
1737 flags = NLM_F_CREATE|NLM_F_EXCL;
1738 group = NFNLGRP_CONNTRACK_EXP_NEW;
1742 if (!item->report && !nfnetlink_has_listeners(net, group))
1745 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1749 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1750 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1754 nfmsg = nlmsg_data(nlh);
1755 nfmsg->nfgen_family = exp->tuple.src.l3num;
1756 nfmsg->version = NFNETLINK_V0;
1760 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1761 goto nla_put_failure;
1764 nlmsg_end(skb, nlh);
1765 nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
1770 nlmsg_cancel(skb, nlh);
1774 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1778 static int ctnetlink_exp_done(struct netlink_callback *cb)
1781 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1786 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1788 struct net *net = sock_net(skb->sk);
1789 struct nf_conntrack_expect *exp, *last;
1790 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1791 struct hlist_node *n;
1792 u_int8_t l3proto = nfmsg->nfgen_family;
1795 last = (struct nf_conntrack_expect *)cb->args[1];
1796 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1798 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1800 if (l3proto && exp->tuple.src.l3num != l3proto)
1807 if (ctnetlink_exp_fill_info(skb,
1808 NETLINK_CB(cb->skb).pid,
1812 if (!atomic_inc_not_zero(&exp->use))
1814 cb->args[1] = (unsigned long)exp;
1826 nf_ct_expect_put(last);
1831 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1832 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1833 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1834 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
1835 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1836 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1837 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
1838 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
1839 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
1843 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1844 const struct nlmsghdr *nlh,
1845 const struct nlattr * const cda[])
1847 struct net *net = sock_net(ctnl);
1848 struct nf_conntrack_tuple tuple;
1849 struct nf_conntrack_expect *exp;
1850 struct sk_buff *skb2;
1851 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1852 u_int8_t u3 = nfmsg->nfgen_family;
1856 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1857 struct netlink_dump_control c = {
1858 .dump = ctnetlink_exp_dump_table,
1859 .done = ctnetlink_exp_done,
1861 return netlink_dump_start(ctnl, skb, nlh, &c);
1864 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1868 if (cda[CTA_EXPECT_TUPLE])
1869 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1870 else if (cda[CTA_EXPECT_MASTER])
1871 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1878 exp = nf_ct_expect_find_get(net, zone, &tuple);
1882 if (cda[CTA_EXPECT_ID]) {
1883 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1884 if (ntohl(id) != (u32)(unsigned long)exp) {
1885 nf_ct_expect_put(exp);
1891 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1893 nf_ct_expect_put(exp);
1898 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1899 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1901 nf_ct_expect_put(exp);
1905 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1914 /* this avoids a loop in nfnetlink. */
1915 return err == -EAGAIN ? -ENOBUFS : err;
1919 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1920 const struct nlmsghdr *nlh,
1921 const struct nlattr * const cda[])
1923 struct net *net = sock_net(ctnl);
1924 struct nf_conntrack_expect *exp;
1925 struct nf_conntrack_tuple tuple;
1926 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1927 struct hlist_node *n, *next;
1928 u_int8_t u3 = nfmsg->nfgen_family;
1933 if (cda[CTA_EXPECT_TUPLE]) {
1934 /* delete a single expect by tuple */
1935 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1939 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1943 /* bump usage count to 2 */
1944 exp = nf_ct_expect_find_get(net, zone, &tuple);
1948 if (cda[CTA_EXPECT_ID]) {
1949 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1950 if (ntohl(id) != (u32)(unsigned long)exp) {
1951 nf_ct_expect_put(exp);
1956 /* after list removal, usage count == 1 */
1957 spin_lock_bh(&nf_conntrack_lock);
1958 if (del_timer(&exp->timeout)) {
1959 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
1961 nf_ct_expect_put(exp);
1963 spin_unlock_bh(&nf_conntrack_lock);
1964 /* have to put what we 'get' above.
1965 * after this line usage count == 0 */
1966 nf_ct_expect_put(exp);
1967 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1968 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1969 struct nf_conn_help *m_help;
1971 /* delete all expectations for this helper */
1972 spin_lock_bh(&nf_conntrack_lock);
1973 for (i = 0; i < nf_ct_expect_hsize; i++) {
1974 hlist_for_each_entry_safe(exp, n, next,
1975 &net->ct.expect_hash[i],
1977 m_help = nfct_help(exp->master);
1978 if (!strcmp(m_help->helper->name, name) &&
1979 del_timer(&exp->timeout)) {
1980 nf_ct_unlink_expect_report(exp,
1981 NETLINK_CB(skb).pid,
1983 nf_ct_expect_put(exp);
1987 spin_unlock_bh(&nf_conntrack_lock);
1989 /* This basically means we have to flush everything*/
1990 spin_lock_bh(&nf_conntrack_lock);
1991 for (i = 0; i < nf_ct_expect_hsize; i++) {
1992 hlist_for_each_entry_safe(exp, n, next,
1993 &net->ct.expect_hash[i],
1995 if (del_timer(&exp->timeout)) {
1996 nf_ct_unlink_expect_report(exp,
1997 NETLINK_CB(skb).pid,
1999 nf_ct_expect_put(exp);
2003 spin_unlock_bh(&nf_conntrack_lock);
2009 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2010 const struct nlattr * const cda[])
2016 ctnetlink_create_expect(struct net *net, u16 zone,
2017 const struct nlattr * const cda[],
2019 u32 pid, int report)
2021 struct nf_conntrack_tuple tuple, mask, master_tuple;
2022 struct nf_conntrack_tuple_hash *h = NULL;
2023 struct nf_conntrack_expect *exp;
2025 struct nf_conn_help *help;
2028 /* caller guarantees that those three CTA_EXPECT_* exist */
2029 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2032 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2035 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2039 /* Look for master conntrack of this expectation */
2040 h = nf_conntrack_find_get(net, zone, &master_tuple);
2043 ct = nf_ct_tuplehash_to_ctrack(h);
2044 exp = nf_ct_expect_alloc(ct);
2049 help = nfct_help(ct);
2051 if (!cda[CTA_EXPECT_TIMEOUT]) {
2055 exp->timeout.expires =
2056 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2058 exp->flags = NF_CT_EXPECT_USERSPACE;
2059 if (cda[CTA_EXPECT_FLAGS]) {
2061 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2064 if (cda[CTA_EXPECT_FLAGS]) {
2065 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2066 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2072 exp->expectfn = NULL;
2075 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2076 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2077 exp->mask.src.u.all = mask.src.u.all;
2079 err = nf_ct_expect_related_report(exp, pid, report);
2080 nf_ct_expect_put(exp);
2083 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2088 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2089 const struct nlmsghdr *nlh,
2090 const struct nlattr * const cda[])
2092 struct net *net = sock_net(ctnl);
2093 struct nf_conntrack_tuple tuple;
2094 struct nf_conntrack_expect *exp;
2095 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2096 u_int8_t u3 = nfmsg->nfgen_family;
2100 if (!cda[CTA_EXPECT_TUPLE]
2101 || !cda[CTA_EXPECT_MASK]
2102 || !cda[CTA_EXPECT_MASTER])
2105 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2109 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2113 spin_lock_bh(&nf_conntrack_lock);
2114 exp = __nf_ct_expect_find(net, zone, &tuple);
2117 spin_unlock_bh(&nf_conntrack_lock);
2119 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2120 err = ctnetlink_create_expect(net, zone, cda,
2122 NETLINK_CB(skb).pid,
2129 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2130 err = ctnetlink_change_expect(exp, cda);
2131 spin_unlock_bh(&nf_conntrack_lock);
2136 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2137 static struct nf_ct_event_notifier ctnl_notifier = {
2138 .fcn = ctnetlink_conntrack_event,
2141 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2142 .fcn = ctnetlink_expect_event,
2146 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2147 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2148 .attr_count = CTA_MAX,
2149 .policy = ct_nla_policy },
2150 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2151 .attr_count = CTA_MAX,
2152 .policy = ct_nla_policy },
2153 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2154 .attr_count = CTA_MAX,
2155 .policy = ct_nla_policy },
2156 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2157 .attr_count = CTA_MAX,
2158 .policy = ct_nla_policy },
2161 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2162 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2163 .attr_count = CTA_EXPECT_MAX,
2164 .policy = exp_nla_policy },
2165 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2166 .attr_count = CTA_EXPECT_MAX,
2167 .policy = exp_nla_policy },
2168 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2169 .attr_count = CTA_EXPECT_MAX,
2170 .policy = exp_nla_policy },
2173 static const struct nfnetlink_subsystem ctnl_subsys = {
2174 .name = "conntrack",
2175 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2176 .cb_count = IPCTNL_MSG_MAX,
2180 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2181 .name = "conntrack_expect",
2182 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2183 .cb_count = IPCTNL_MSG_EXP_MAX,
2187 MODULE_ALIAS("ip_conntrack_netlink");
2188 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2189 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2191 static int __net_init ctnetlink_net_init(struct net *net)
2193 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2196 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2198 pr_err("ctnetlink_init: cannot register notifier.\n");
2202 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2204 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2205 goto err_unreg_notifier;
2210 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2212 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2218 static void ctnetlink_net_exit(struct net *net)
2220 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2221 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2222 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2226 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2230 list_for_each_entry(net, net_exit_list, exit_list)
2231 ctnetlink_net_exit(net);
2234 static struct pernet_operations ctnetlink_net_ops = {
2235 .init = ctnetlink_net_init,
2236 .exit_batch = ctnetlink_net_exit_batch,
2239 static int __init ctnetlink_init(void)
2243 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2244 ret = nfnetlink_subsys_register(&ctnl_subsys);
2246 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2250 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2252 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2253 goto err_unreg_subsys;
2256 if (register_pernet_subsys(&ctnetlink_net_ops)) {
2257 pr_err("ctnetlink_init: cannot register pernet operations\n");
2258 goto err_unreg_exp_subsys;
2263 err_unreg_exp_subsys:
2264 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2266 nfnetlink_subsys_unregister(&ctnl_subsys);
2271 static void __exit ctnetlink_exit(void)
2273 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2275 unregister_pernet_subsys(&ctnetlink_net_ops);
2276 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2277 nfnetlink_subsys_unregister(&ctnl_subsys);
2280 module_init(ctnetlink_init);
2281 module_exit(ctnetlink_exit);