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;
114 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
115 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
118 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
119 tuple->dst.protonum);
120 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
127 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
129 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
137 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
139 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
144 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
152 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
154 struct nf_conntrack_l4proto *l4proto;
155 struct nlattr *nest_proto;
158 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
159 if (!l4proto->to_nlattr)
162 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
164 goto nla_put_failure;
166 ret = l4proto->to_nlattr(skb, nest_proto, ct);
168 nla_nest_end(skb, nest_proto);
177 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
179 struct nlattr *nest_helper;
180 const struct nf_conn_help *help = nfct_help(ct);
181 struct nf_conntrack_helper *helper;
186 helper = rcu_dereference(help->helper);
190 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
192 goto nla_put_failure;
193 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
195 if (helper->to_nlattr)
196 helper->to_nlattr(skb, ct);
198 nla_nest_end(skb, nest_helper);
207 dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
208 enum ip_conntrack_dir dir)
210 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
211 struct nlattr *nest_count;
213 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
215 goto nla_put_failure;
217 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts));
218 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes));
220 nla_nest_end(skb, nest_count);
229 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
230 enum ip_conntrack_dir dir, int type)
232 struct nf_conn_counter *acct;
235 acct = nf_conn_acct_find(ct);
239 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
240 pkts = atomic64_xchg(&acct[dir].packets, 0);
241 bytes = atomic64_xchg(&acct[dir].bytes, 0);
243 pkts = atomic64_read(&acct[dir].packets);
244 bytes = atomic64_read(&acct[dir].bytes);
246 return dump_counters(skb, pkts, bytes, dir);
250 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
252 struct nlattr *nest_count;
253 const struct nf_conn_tstamp *tstamp;
255 tstamp = nf_conn_tstamp_find(ct);
259 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
261 goto nla_put_failure;
263 NLA_PUT_BE64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start));
264 if (tstamp->stop != 0) {
265 NLA_PUT_BE64(skb, CTA_TIMESTAMP_STOP,
266 cpu_to_be64(tstamp->stop));
268 nla_nest_end(skb, nest_count);
276 #ifdef CONFIG_NF_CONNTRACK_MARK
278 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
280 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
287 #define ctnetlink_dump_mark(a, b) (0)
290 #ifdef CONFIG_NF_CONNTRACK_SECMARK
292 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
294 struct nlattr *nest_secctx;
298 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
303 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
305 goto nla_put_failure;
307 NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
308 nla_nest_end(skb, nest_secctx);
312 security_release_secctx(secctx, len);
316 #define ctnetlink_dump_secctx(a, b) (0)
319 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
322 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
324 struct nlattr *nest_parms;
326 if (!(ct->status & IPS_EXPECTED))
329 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
331 goto nla_put_failure;
332 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
333 goto nla_put_failure;
334 nla_nest_end(skb, nest_parms);
342 #ifdef CONFIG_NF_NAT_NEEDED
344 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
346 struct nlattr *nest_parms;
348 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
350 goto nla_put_failure;
352 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
353 htonl(natseq->correction_pos));
354 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
355 htonl(natseq->offset_before));
356 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
357 htonl(natseq->offset_after));
359 nla_nest_end(skb, nest_parms);
368 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
370 struct nf_nat_seq *natseq;
371 struct nf_conn_nat *nat = nfct_nat(ct);
373 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
376 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
377 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
380 natseq = &nat->seq[IP_CT_DIR_REPLY];
381 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
387 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
391 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
393 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
401 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
403 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
411 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, u32 type,
414 struct nlmsghdr *nlh;
415 struct nfgenmsg *nfmsg;
416 struct nlattr *nest_parms;
417 unsigned int flags = pid ? NLM_F_MULTI : 0, event;
419 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
420 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
424 nfmsg = nlmsg_data(nlh);
425 nfmsg->nfgen_family = nf_ct_l3num(ct);
426 nfmsg->version = NFNETLINK_V0;
429 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
431 goto nla_put_failure;
432 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
433 goto nla_put_failure;
434 nla_nest_end(skb, nest_parms);
436 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
438 goto nla_put_failure;
439 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
440 goto nla_put_failure;
441 nla_nest_end(skb, nest_parms);
444 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
446 if (ctnetlink_dump_status(skb, ct) < 0 ||
447 ctnetlink_dump_timeout(skb, ct) < 0 ||
448 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
449 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
450 ctnetlink_dump_timestamp(skb, ct) < 0 ||
451 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
452 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
453 ctnetlink_dump_mark(skb, ct) < 0 ||
454 ctnetlink_dump_secctx(skb, ct) < 0 ||
455 ctnetlink_dump_id(skb, ct) < 0 ||
456 ctnetlink_dump_use(skb, ct) < 0 ||
457 ctnetlink_dump_master(skb, ct) < 0 ||
458 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
459 goto nla_put_failure;
466 nlmsg_cancel(skb, nlh);
470 #ifdef CONFIG_NF_CONNTRACK_EVENTS
472 ctnetlink_proto_size(const struct nf_conn *ct)
474 struct nf_conntrack_l3proto *l3proto;
475 struct nf_conntrack_l4proto *l4proto;
479 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
480 len += l3proto->nla_size;
482 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
483 len += l4proto->nla_size;
490 ctnetlink_counters_size(const struct nf_conn *ct)
492 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
494 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
495 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
496 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
501 ctnetlink_secctx_size(const struct nf_conn *ct)
503 #ifdef CONFIG_NF_CONNTRACK_SECMARK
506 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
510 return nla_total_size(0) /* CTA_SECCTX */
511 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
518 ctnetlink_timestamp_size(const struct nf_conn *ct)
520 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
521 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
523 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
530 ctnetlink_nlmsg_size(const struct nf_conn *ct)
532 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
533 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
534 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
535 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
536 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
537 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
538 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
539 + ctnetlink_counters_size(ct)
540 + ctnetlink_timestamp_size(ct)
541 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
542 + nla_total_size(0) /* CTA_PROTOINFO */
543 + nla_total_size(0) /* CTA_HELP */
544 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
545 + ctnetlink_secctx_size(ct)
546 #ifdef CONFIG_NF_NAT_NEEDED
547 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
548 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
550 #ifdef CONFIG_NF_CONNTRACK_MARK
551 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
553 + ctnetlink_proto_size(ct)
558 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
561 struct nlmsghdr *nlh;
562 struct nfgenmsg *nfmsg;
563 struct nlattr *nest_parms;
564 struct nf_conn *ct = item->ct;
567 unsigned int flags = 0, group;
570 /* ignore our fake conntrack entry */
571 if (nf_ct_is_untracked(ct))
574 if (events & (1 << IPCT_DESTROY)) {
575 type = IPCTNL_MSG_CT_DELETE;
576 group = NFNLGRP_CONNTRACK_DESTROY;
577 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
578 type = IPCTNL_MSG_CT_NEW;
579 flags = NLM_F_CREATE|NLM_F_EXCL;
580 group = NFNLGRP_CONNTRACK_NEW;
582 type = IPCTNL_MSG_CT_NEW;
583 group = NFNLGRP_CONNTRACK_UPDATE;
588 if (!item->report && !nfnetlink_has_listeners(net, group))
591 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
595 type |= NFNL_SUBSYS_CTNETLINK << 8;
596 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
600 nfmsg = nlmsg_data(nlh);
601 nfmsg->nfgen_family = nf_ct_l3num(ct);
602 nfmsg->version = NFNETLINK_V0;
606 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
608 goto nla_put_failure;
609 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
610 goto nla_put_failure;
611 nla_nest_end(skb, nest_parms);
613 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
615 goto nla_put_failure;
616 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
617 goto nla_put_failure;
618 nla_nest_end(skb, nest_parms);
621 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
623 if (ctnetlink_dump_id(skb, ct) < 0)
624 goto nla_put_failure;
626 if (ctnetlink_dump_status(skb, ct) < 0)
627 goto nla_put_failure;
629 if (events & (1 << IPCT_DESTROY)) {
630 if (ctnetlink_dump_counters(skb, ct,
631 IP_CT_DIR_ORIGINAL, type) < 0 ||
632 ctnetlink_dump_counters(skb, ct,
633 IP_CT_DIR_REPLY, type) < 0 ||
634 ctnetlink_dump_timestamp(skb, ct) < 0)
635 goto nla_put_failure;
637 if (ctnetlink_dump_timeout(skb, ct) < 0)
638 goto nla_put_failure;
640 if (events & (1 << IPCT_PROTOINFO)
641 && ctnetlink_dump_protoinfo(skb, ct) < 0)
642 goto nla_put_failure;
644 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
645 && ctnetlink_dump_helpinfo(skb, ct) < 0)
646 goto nla_put_failure;
648 #ifdef CONFIG_NF_CONNTRACK_SECMARK
649 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
650 && ctnetlink_dump_secctx(skb, ct) < 0)
651 goto nla_put_failure;
654 if (events & (1 << IPCT_RELATED) &&
655 ctnetlink_dump_master(skb, ct) < 0)
656 goto nla_put_failure;
658 if (events & (1 << IPCT_NATSEQADJ) &&
659 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
660 goto nla_put_failure;
663 #ifdef CONFIG_NF_CONNTRACK_MARK
664 if ((events & (1 << IPCT_MARK) || ct->mark)
665 && ctnetlink_dump_mark(skb, ct) < 0)
666 goto nla_put_failure;
671 err = nfnetlink_send(skb, net, item->pid, group, item->report,
673 if (err == -ENOBUFS || err == -EAGAIN)
680 nlmsg_cancel(skb, nlh);
684 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
689 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
691 static int ctnetlink_done(struct netlink_callback *cb)
694 nf_ct_put((struct nf_conn *)cb->args[1]);
700 struct ctnetlink_dump_filter {
708 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
710 struct net *net = sock_net(skb->sk);
711 struct nf_conn *ct, *last;
712 struct nf_conntrack_tuple_hash *h;
713 struct hlist_nulls_node *n;
714 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
715 u_int8_t l3proto = nfmsg->nfgen_family;
717 #ifdef CONFIG_NF_CONNTRACK_MARK
718 const struct ctnetlink_dump_filter *filter = cb->data;
721 spin_lock_bh(&nf_conntrack_lock);
722 last = (struct nf_conn *)cb->args[1];
723 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
725 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
727 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
729 ct = nf_ct_tuplehash_to_ctrack(h);
730 /* Dump entries of a given L3 protocol number.
731 * If it is not specified, ie. l3proto == 0,
732 * then dump everything. */
733 if (l3proto && nf_ct_l3num(ct) != l3proto)
740 #ifdef CONFIG_NF_CONNTRACK_MARK
741 if (filter && !((ct->mark & filter->mark.mask) ==
748 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
750 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
754 nf_conntrack_get(&ct->ct_general);
755 cb->args[1] = (unsigned long)ct;
765 spin_unlock_bh(&nf_conntrack_lock);
773 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
775 struct nlattr *tb[CTA_IP_MAX+1];
776 struct nf_conntrack_l3proto *l3proto;
779 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
782 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
784 if (likely(l3proto->nlattr_to_tuple)) {
785 ret = nla_validate_nested(attr, CTA_IP_MAX,
786 l3proto->nla_policy);
788 ret = l3proto->nlattr_to_tuple(tb, tuple);
796 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
797 [CTA_PROTO_NUM] = { .type = NLA_U8 },
801 ctnetlink_parse_tuple_proto(struct nlattr *attr,
802 struct nf_conntrack_tuple *tuple)
804 struct nlattr *tb[CTA_PROTO_MAX+1];
805 struct nf_conntrack_l4proto *l4proto;
808 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
812 if (!tb[CTA_PROTO_NUM])
814 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
817 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
819 if (likely(l4proto->nlattr_to_tuple)) {
820 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
821 l4proto->nla_policy);
823 ret = l4proto->nlattr_to_tuple(tb, tuple);
831 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
832 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
833 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
837 ctnetlink_parse_tuple(const struct nlattr * const cda[],
838 struct nf_conntrack_tuple *tuple,
839 enum ctattr_type type, u_int8_t l3num)
841 struct nlattr *tb[CTA_TUPLE_MAX+1];
844 memset(tuple, 0, sizeof(*tuple));
846 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
848 if (!tb[CTA_TUPLE_IP])
851 tuple->src.l3num = l3num;
853 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
857 if (!tb[CTA_TUPLE_PROTO])
860 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
864 /* orig and expect tuples get DIR_ORIGINAL */
865 if (type == CTA_TUPLE_REPLY)
866 tuple->dst.dir = IP_CT_DIR_REPLY;
868 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
874 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
877 #ifdef CONFIG_NF_CONNTRACK_ZONES
878 *zone = ntohs(nla_get_be16(attr));
888 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
889 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
893 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
895 struct nlattr *tb[CTA_HELP_MAX+1];
897 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
899 if (!tb[CTA_HELP_NAME])
902 *helper_name = nla_data(tb[CTA_HELP_NAME]);
907 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
908 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
909 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
910 [CTA_STATUS] = { .type = NLA_U32 },
911 [CTA_PROTOINFO] = { .type = NLA_NESTED },
912 [CTA_HELP] = { .type = NLA_NESTED },
913 [CTA_NAT_SRC] = { .type = NLA_NESTED },
914 [CTA_TIMEOUT] = { .type = NLA_U32 },
915 [CTA_MARK] = { .type = NLA_U32 },
916 [CTA_ID] = { .type = NLA_U32 },
917 [CTA_NAT_DST] = { .type = NLA_NESTED },
918 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
919 [CTA_ZONE] = { .type = NLA_U16 },
920 [CTA_MARK_MASK] = { .type = NLA_U32 },
924 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
925 const struct nlmsghdr *nlh,
926 const struct nlattr * const cda[])
928 struct net *net = sock_net(ctnl);
929 struct nf_conntrack_tuple_hash *h;
930 struct nf_conntrack_tuple tuple;
932 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
933 u_int8_t u3 = nfmsg->nfgen_family;
937 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
941 if (cda[CTA_TUPLE_ORIG])
942 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
943 else if (cda[CTA_TUPLE_REPLY])
944 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
946 /* Flush the whole table */
947 nf_conntrack_flush_report(net,
956 h = nf_conntrack_find_get(net, zone, &tuple);
960 ct = nf_ct_tuplehash_to_ctrack(h);
963 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
964 if (id != (u32)(unsigned long)ct) {
970 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
972 nlmsg_report(nlh)) < 0) {
973 nf_ct_delete_from_lists(ct);
974 /* we failed to report the event, try later */
975 nf_ct_insert_dying_list(ct);
980 /* death_by_timeout would report the event again */
981 set_bit(IPS_DYING_BIT, &ct->status);
990 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
991 const struct nlmsghdr *nlh,
992 const struct nlattr * const cda[])
994 struct net *net = sock_net(ctnl);
995 struct nf_conntrack_tuple_hash *h;
996 struct nf_conntrack_tuple tuple;
998 struct sk_buff *skb2 = NULL;
999 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1000 u_int8_t u3 = nfmsg->nfgen_family;
1004 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1005 struct netlink_dump_control c = {
1006 .dump = ctnetlink_dump_table,
1007 .done = ctnetlink_done,
1009 #ifdef CONFIG_NF_CONNTRACK_MARK
1010 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1011 struct ctnetlink_dump_filter *filter;
1013 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1018 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1020 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1024 return netlink_dump_start(ctnl, skb, nlh, &c);
1027 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1031 if (cda[CTA_TUPLE_ORIG])
1032 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1033 else if (cda[CTA_TUPLE_REPLY])
1034 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1041 h = nf_conntrack_find_get(net, zone, &tuple);
1045 ct = nf_ct_tuplehash_to_ctrack(h);
1048 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1055 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
1056 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1062 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1071 /* this avoids a loop in nfnetlink. */
1072 return err == -EAGAIN ? -ENOBUFS : err;
1075 #ifdef CONFIG_NF_NAT_NEEDED
1077 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1078 enum nf_nat_manip_type manip,
1079 const struct nlattr *attr)
1081 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1083 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1084 if (!parse_nat_setup) {
1085 #ifdef CONFIG_MODULES
1088 if (request_module("nf-nat-ipv4") < 0) {
1095 if (nfnetlink_parse_nat_setup_hook)
1101 return parse_nat_setup(ct, manip, attr);
1106 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1109 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1110 d = ct->status ^ status;
1112 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1116 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1117 /* SEEN_REPLY bit can only be set */
1120 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1121 /* ASSURED bit can only be set */
1124 /* Be careful here, modifying NAT bits can screw up things,
1125 * so don't let users modify them directly if they don't pass
1127 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1132 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1134 #ifdef CONFIG_NF_NAT_NEEDED
1137 if (cda[CTA_NAT_DST]) {
1138 ret = ctnetlink_parse_nat_setup(ct,
1144 if (cda[CTA_NAT_SRC]) {
1145 ret = ctnetlink_parse_nat_setup(ct,
1158 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1160 struct nf_conntrack_helper *helper;
1161 struct nf_conn_help *help = nfct_help(ct);
1162 char *helpname = NULL;
1165 /* don't change helper of sibling connections */
1169 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1173 if (!strcmp(helpname, "")) {
1174 if (help && help->helper) {
1175 /* we had a helper before ... */
1176 nf_ct_remove_expectations(ct);
1177 RCU_INIT_POINTER(help->helper, NULL);
1183 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1184 nf_ct_protonum(ct));
1185 if (helper == NULL) {
1186 #ifdef CONFIG_MODULES
1187 spin_unlock_bh(&nf_conntrack_lock);
1189 if (request_module("nfct-helper-%s", helpname) < 0) {
1190 spin_lock_bh(&nf_conntrack_lock);
1194 spin_lock_bh(&nf_conntrack_lock);
1195 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1196 nf_ct_protonum(ct));
1204 if (help->helper == helper)
1208 /* need to zero data of old helper */
1209 memset(&help->help, 0, sizeof(help->help));
1211 /* we cannot set a helper for an existing conntrack */
1215 rcu_assign_pointer(help->helper, helper);
1221 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1223 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1225 if (!del_timer(&ct->timeout))
1228 ct->timeout.expires = jiffies + timeout * HZ;
1229 add_timer(&ct->timeout);
1234 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1235 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1236 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1237 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1241 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1243 const struct nlattr *attr = cda[CTA_PROTOINFO];
1244 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1245 struct nf_conntrack_l4proto *l4proto;
1248 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1251 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1252 if (l4proto->from_nlattr)
1253 err = l4proto->from_nlattr(tb, ct);
1259 #ifdef CONFIG_NF_NAT_NEEDED
1260 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1261 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1262 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1263 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1267 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1269 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1271 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1273 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1276 natseq->correction_pos =
1277 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1279 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1282 natseq->offset_before =
1283 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1285 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1288 natseq->offset_after =
1289 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1295 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1296 const struct nlattr * const cda[])
1299 struct nf_conn_nat *nat = nfct_nat(ct);
1304 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1305 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1306 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1310 ct->status |= IPS_SEQ_ADJUST;
1313 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1314 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1315 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1319 ct->status |= IPS_SEQ_ADJUST;
1327 ctnetlink_change_conntrack(struct nf_conn *ct,
1328 const struct nlattr * const cda[])
1332 /* only allow NAT changes and master assignation for new conntracks */
1333 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1336 if (cda[CTA_HELP]) {
1337 err = ctnetlink_change_helper(ct, cda);
1342 if (cda[CTA_TIMEOUT]) {
1343 err = ctnetlink_change_timeout(ct, cda);
1348 if (cda[CTA_STATUS]) {
1349 err = ctnetlink_change_status(ct, cda);
1354 if (cda[CTA_PROTOINFO]) {
1355 err = ctnetlink_change_protoinfo(ct, cda);
1360 #if defined(CONFIG_NF_CONNTRACK_MARK)
1362 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1365 #ifdef CONFIG_NF_NAT_NEEDED
1366 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1367 err = ctnetlink_change_nat_seq_adj(ct, cda);
1376 static struct nf_conn *
1377 ctnetlink_create_conntrack(struct net *net, u16 zone,
1378 const struct nlattr * const cda[],
1379 struct nf_conntrack_tuple *otuple,
1380 struct nf_conntrack_tuple *rtuple,
1385 struct nf_conntrack_helper *helper;
1386 struct nf_conn_tstamp *tstamp;
1388 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1390 return ERR_PTR(-ENOMEM);
1392 if (!cda[CTA_TIMEOUT])
1394 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1396 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1399 if (cda[CTA_HELP]) {
1400 char *helpname = NULL;
1402 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1406 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1407 nf_ct_protonum(ct));
1408 if (helper == NULL) {
1410 #ifdef CONFIG_MODULES
1411 if (request_module("nfct-helper-%s", helpname) < 0) {
1417 helper = __nf_conntrack_helper_find(helpname,
1419 nf_ct_protonum(ct));
1429 struct nf_conn_help *help;
1431 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1437 /* not in hash table yet so not strictly necessary */
1438 RCU_INIT_POINTER(help->helper, helper);
1441 /* try an implicit helper assignation */
1442 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1447 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1448 err = ctnetlink_change_nat(ct, cda);
1453 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1454 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1455 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1456 /* we must add conntrack extensions before confirmation. */
1457 ct->status |= IPS_CONFIRMED;
1459 if (cda[CTA_STATUS]) {
1460 err = ctnetlink_change_status(ct, cda);
1465 #ifdef CONFIG_NF_NAT_NEEDED
1466 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1467 err = ctnetlink_change_nat_seq_adj(ct, cda);
1473 memset(&ct->proto, 0, sizeof(ct->proto));
1474 if (cda[CTA_PROTOINFO]) {
1475 err = ctnetlink_change_protoinfo(ct, cda);
1480 #if defined(CONFIG_NF_CONNTRACK_MARK)
1482 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1485 /* setup master conntrack: this is a confirmed expectation */
1486 if (cda[CTA_TUPLE_MASTER]) {
1487 struct nf_conntrack_tuple master;
1488 struct nf_conntrack_tuple_hash *master_h;
1489 struct nf_conn *master_ct;
1491 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1495 master_h = nf_conntrack_find_get(net, zone, &master);
1496 if (master_h == NULL) {
1500 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1501 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1502 ct->master = master_ct;
1504 tstamp = nf_conn_tstamp_find(ct);
1506 tstamp->start = ktime_to_ns(ktime_get_real());
1508 err = nf_conntrack_hash_check_insert(ct);
1519 nf_conntrack_free(ct);
1520 return ERR_PTR(err);
1524 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1525 const struct nlmsghdr *nlh,
1526 const struct nlattr * const cda[])
1528 struct net *net = sock_net(ctnl);
1529 struct nf_conntrack_tuple otuple, rtuple;
1530 struct nf_conntrack_tuple_hash *h = NULL;
1531 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1533 u_int8_t u3 = nfmsg->nfgen_family;
1537 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1541 if (cda[CTA_TUPLE_ORIG]) {
1542 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1547 if (cda[CTA_TUPLE_REPLY]) {
1548 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1553 if (cda[CTA_TUPLE_ORIG])
1554 h = nf_conntrack_find_get(net, zone, &otuple);
1555 else if (cda[CTA_TUPLE_REPLY])
1556 h = nf_conntrack_find_get(net, zone, &rtuple);
1560 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1561 enum ip_conntrack_events events;
1563 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1569 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1570 events = IPCT_RELATED;
1574 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1575 (1 << IPCT_ASSURED) |
1576 (1 << IPCT_HELPER) |
1577 (1 << IPCT_PROTOINFO) |
1578 (1 << IPCT_NATSEQADJ) |
1579 (1 << IPCT_MARK) | events,
1580 ct, NETLINK_CB(skb).pid,
1587 /* implicit 'else' */
1590 ct = nf_ct_tuplehash_to_ctrack(h);
1591 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1592 spin_lock_bh(&nf_conntrack_lock);
1593 err = ctnetlink_change_conntrack(ct, cda);
1594 spin_unlock_bh(&nf_conntrack_lock);
1596 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1597 (1 << IPCT_ASSURED) |
1598 (1 << IPCT_HELPER) |
1599 (1 << IPCT_PROTOINFO) |
1600 (1 << IPCT_NATSEQADJ) |
1602 ct, NETLINK_CB(skb).pid,
1611 /***********************************************************************
1613 ***********************************************************************/
1616 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1617 const struct nf_conntrack_tuple *tuple,
1618 enum ctattr_expect type)
1620 struct nlattr *nest_parms;
1622 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1624 goto nla_put_failure;
1625 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1626 goto nla_put_failure;
1627 nla_nest_end(skb, nest_parms);
1636 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1637 const struct nf_conntrack_tuple *tuple,
1638 const struct nf_conntrack_tuple_mask *mask)
1641 struct nf_conntrack_l3proto *l3proto;
1642 struct nf_conntrack_l4proto *l4proto;
1643 struct nf_conntrack_tuple m;
1644 struct nlattr *nest_parms;
1646 memset(&m, 0xFF, sizeof(m));
1647 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1648 m.src.u.all = mask->src.u.all;
1649 m.dst.protonum = tuple->dst.protonum;
1651 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1653 goto nla_put_failure;
1656 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1657 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1659 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
1660 tuple->dst.protonum);
1661 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1665 if (unlikely(ret < 0))
1666 goto nla_put_failure;
1668 nla_nest_end(skb, nest_parms);
1677 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1678 const struct nf_conntrack_expect *exp)
1680 struct nf_conn *master = exp->master;
1681 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
1682 struct nf_conn_help *help;
1683 #ifdef CONFIG_NF_NAT_NEEDED
1684 struct nlattr *nest_parms;
1685 struct nf_conntrack_tuple nat_tuple = {};
1687 struct nf_ct_helper_expectfn *expfn;
1692 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1693 goto nla_put_failure;
1694 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1695 goto nla_put_failure;
1696 if (ctnetlink_exp_dump_tuple(skb,
1697 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1698 CTA_EXPECT_MASTER) < 0)
1699 goto nla_put_failure;
1701 #ifdef CONFIG_NF_NAT_NEEDED
1702 if (exp->saved_ip || exp->saved_proto.all) {
1703 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
1705 goto nla_put_failure;
1707 NLA_PUT_BE32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir));
1709 nat_tuple.src.l3num = nf_ct_l3num(master);
1710 nat_tuple.src.u3.ip = exp->saved_ip;
1711 nat_tuple.dst.protonum = nf_ct_protonum(master);
1712 nat_tuple.src.u = exp->saved_proto;
1714 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
1715 CTA_EXPECT_NAT_TUPLE) < 0)
1716 goto nla_put_failure;
1717 nla_nest_end(skb, nest_parms);
1720 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1721 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1722 NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1723 NLA_PUT_BE32(skb, CTA_EXPECT_CLASS, htonl(exp->class));
1724 help = nfct_help(master);
1726 struct nf_conntrack_helper *helper;
1728 helper = rcu_dereference(help->helper);
1730 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1732 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
1734 NLA_PUT_STRING(skb, CTA_EXPECT_FN, expfn->name);
1743 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1744 int event, const struct nf_conntrack_expect *exp)
1746 struct nlmsghdr *nlh;
1747 struct nfgenmsg *nfmsg;
1748 unsigned int flags = pid ? NLM_F_MULTI : 0;
1750 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1751 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1755 nfmsg = nlmsg_data(nlh);
1756 nfmsg->nfgen_family = exp->tuple.src.l3num;
1757 nfmsg->version = NFNETLINK_V0;
1760 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1761 goto nla_put_failure;
1763 nlmsg_end(skb, nlh);
1768 nlmsg_cancel(skb, nlh);
1772 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1774 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1776 struct nf_conntrack_expect *exp = item->exp;
1777 struct net *net = nf_ct_exp_net(exp);
1778 struct nlmsghdr *nlh;
1779 struct nfgenmsg *nfmsg;
1780 struct sk_buff *skb;
1781 unsigned int type, group;
1784 if (events & (1 << IPEXP_DESTROY)) {
1785 type = IPCTNL_MSG_EXP_DELETE;
1786 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1787 } else if (events & (1 << IPEXP_NEW)) {
1788 type = IPCTNL_MSG_EXP_NEW;
1789 flags = NLM_F_CREATE|NLM_F_EXCL;
1790 group = NFNLGRP_CONNTRACK_EXP_NEW;
1794 if (!item->report && !nfnetlink_has_listeners(net, group))
1797 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1801 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1802 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1806 nfmsg = nlmsg_data(nlh);
1807 nfmsg->nfgen_family = exp->tuple.src.l3num;
1808 nfmsg->version = NFNETLINK_V0;
1812 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1813 goto nla_put_failure;
1816 nlmsg_end(skb, nlh);
1817 nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
1822 nlmsg_cancel(skb, nlh);
1826 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1830 static int ctnetlink_exp_done(struct netlink_callback *cb)
1833 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1838 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1840 struct net *net = sock_net(skb->sk);
1841 struct nf_conntrack_expect *exp, *last;
1842 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1843 struct hlist_node *n;
1844 u_int8_t l3proto = nfmsg->nfgen_family;
1847 last = (struct nf_conntrack_expect *)cb->args[1];
1848 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1850 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1852 if (l3proto && exp->tuple.src.l3num != l3proto)
1859 if (ctnetlink_exp_fill_info(skb,
1860 NETLINK_CB(cb->skb).pid,
1864 if (!atomic_inc_not_zero(&exp->use))
1866 cb->args[1] = (unsigned long)exp;
1878 nf_ct_expect_put(last);
1883 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1884 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1885 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1886 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
1887 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1888 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1889 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
1890 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
1891 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
1892 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
1893 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
1894 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
1898 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1899 const struct nlmsghdr *nlh,
1900 const struct nlattr * const cda[])
1902 struct net *net = sock_net(ctnl);
1903 struct nf_conntrack_tuple tuple;
1904 struct nf_conntrack_expect *exp;
1905 struct sk_buff *skb2;
1906 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1907 u_int8_t u3 = nfmsg->nfgen_family;
1911 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1912 struct netlink_dump_control c = {
1913 .dump = ctnetlink_exp_dump_table,
1914 .done = ctnetlink_exp_done,
1916 return netlink_dump_start(ctnl, skb, nlh, &c);
1919 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1923 if (cda[CTA_EXPECT_TUPLE])
1924 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1925 else if (cda[CTA_EXPECT_MASTER])
1926 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1933 exp = nf_ct_expect_find_get(net, zone, &tuple);
1937 if (cda[CTA_EXPECT_ID]) {
1938 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1939 if (ntohl(id) != (u32)(unsigned long)exp) {
1940 nf_ct_expect_put(exp);
1946 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1948 nf_ct_expect_put(exp);
1953 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1954 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1956 nf_ct_expect_put(exp);
1960 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1969 /* this avoids a loop in nfnetlink. */
1970 return err == -EAGAIN ? -ENOBUFS : err;
1974 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1975 const struct nlmsghdr *nlh,
1976 const struct nlattr * const cda[])
1978 struct net *net = sock_net(ctnl);
1979 struct nf_conntrack_expect *exp;
1980 struct nf_conntrack_tuple tuple;
1981 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1982 struct hlist_node *n, *next;
1983 u_int8_t u3 = nfmsg->nfgen_family;
1988 if (cda[CTA_EXPECT_TUPLE]) {
1989 /* delete a single expect by tuple */
1990 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1994 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1998 /* bump usage count to 2 */
1999 exp = nf_ct_expect_find_get(net, zone, &tuple);
2003 if (cda[CTA_EXPECT_ID]) {
2004 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2005 if (ntohl(id) != (u32)(unsigned long)exp) {
2006 nf_ct_expect_put(exp);
2011 /* after list removal, usage count == 1 */
2012 spin_lock_bh(&nf_conntrack_lock);
2013 if (del_timer(&exp->timeout)) {
2014 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
2016 nf_ct_expect_put(exp);
2018 spin_unlock_bh(&nf_conntrack_lock);
2019 /* have to put what we 'get' above.
2020 * after this line usage count == 0 */
2021 nf_ct_expect_put(exp);
2022 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2023 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2024 struct nf_conn_help *m_help;
2026 /* delete all expectations for this helper */
2027 spin_lock_bh(&nf_conntrack_lock);
2028 for (i = 0; i < nf_ct_expect_hsize; i++) {
2029 hlist_for_each_entry_safe(exp, n, next,
2030 &net->ct.expect_hash[i],
2032 m_help = nfct_help(exp->master);
2033 if (!strcmp(m_help->helper->name, name) &&
2034 del_timer(&exp->timeout)) {
2035 nf_ct_unlink_expect_report(exp,
2036 NETLINK_CB(skb).pid,
2038 nf_ct_expect_put(exp);
2042 spin_unlock_bh(&nf_conntrack_lock);
2044 /* This basically means we have to flush everything*/
2045 spin_lock_bh(&nf_conntrack_lock);
2046 for (i = 0; i < nf_ct_expect_hsize; i++) {
2047 hlist_for_each_entry_safe(exp, n, next,
2048 &net->ct.expect_hash[i],
2050 if (del_timer(&exp->timeout)) {
2051 nf_ct_unlink_expect_report(exp,
2052 NETLINK_CB(skb).pid,
2054 nf_ct_expect_put(exp);
2058 spin_unlock_bh(&nf_conntrack_lock);
2064 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2065 const struct nlattr * const cda[])
2070 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2071 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2072 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2076 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2077 struct nf_conntrack_expect *exp,
2080 #ifdef CONFIG_NF_NAT_NEEDED
2081 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2082 struct nf_conntrack_tuple nat_tuple = {};
2085 nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2087 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2090 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2091 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2095 exp->saved_ip = nat_tuple.src.u3.ip;
2096 exp->saved_proto = nat_tuple.src.u;
2097 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2106 ctnetlink_create_expect(struct net *net, u16 zone,
2107 const struct nlattr * const cda[],
2109 u32 pid, int report)
2111 struct nf_conntrack_tuple tuple, mask, master_tuple;
2112 struct nf_conntrack_tuple_hash *h = NULL;
2113 struct nf_conntrack_expect *exp;
2115 struct nf_conn_help *help;
2116 struct nf_conntrack_helper *helper = NULL;
2117 u_int32_t class = 0;
2120 /* caller guarantees that those three CTA_EXPECT_* exist */
2121 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2124 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2127 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2131 /* Look for master conntrack of this expectation */
2132 h = nf_conntrack_find_get(net, zone, &master_tuple);
2135 ct = nf_ct_tuplehash_to_ctrack(h);
2137 /* Look for helper of this expectation */
2138 if (cda[CTA_EXPECT_HELP_NAME]) {
2139 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2141 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2142 nf_ct_protonum(ct));
2143 if (helper == NULL) {
2144 #ifdef CONFIG_MODULES
2145 if (request_module("nfct-helper-%s", helpname) < 0) {
2150 helper = __nf_conntrack_helper_find(helpname,
2152 nf_ct_protonum(ct));
2163 if (cda[CTA_EXPECT_CLASS] && helper) {
2164 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2165 if (class > helper->expect_class_max) {
2170 exp = nf_ct_expect_alloc(ct);
2175 help = nfct_help(ct);
2177 if (!cda[CTA_EXPECT_TIMEOUT]) {
2181 exp->timeout.expires =
2182 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2184 exp->flags = NF_CT_EXPECT_USERSPACE;
2185 if (cda[CTA_EXPECT_FLAGS]) {
2187 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2190 if (cda[CTA_EXPECT_FLAGS]) {
2191 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2192 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2196 if (cda[CTA_EXPECT_FN]) {
2197 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2198 struct nf_ct_helper_expectfn *expfn;
2200 expfn = nf_ct_helper_expectfn_find_by_name(name);
2201 if (expfn == NULL) {
2205 exp->expectfn = expfn->expectfn;
2207 exp->expectfn = NULL;
2211 exp->helper = helper;
2212 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2213 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2214 exp->mask.src.u.all = mask.src.u.all;
2216 if (cda[CTA_EXPECT_NAT]) {
2217 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2222 err = nf_ct_expect_related_report(exp, pid, report);
2224 nf_ct_expect_put(exp);
2226 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2231 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2232 const struct nlmsghdr *nlh,
2233 const struct nlattr * const cda[])
2235 struct net *net = sock_net(ctnl);
2236 struct nf_conntrack_tuple tuple;
2237 struct nf_conntrack_expect *exp;
2238 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2239 u_int8_t u3 = nfmsg->nfgen_family;
2243 if (!cda[CTA_EXPECT_TUPLE]
2244 || !cda[CTA_EXPECT_MASK]
2245 || !cda[CTA_EXPECT_MASTER])
2248 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2252 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2256 spin_lock_bh(&nf_conntrack_lock);
2257 exp = __nf_ct_expect_find(net, zone, &tuple);
2260 spin_unlock_bh(&nf_conntrack_lock);
2262 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2263 err = ctnetlink_create_expect(net, zone, cda,
2265 NETLINK_CB(skb).pid,
2272 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2273 err = ctnetlink_change_expect(exp, cda);
2274 spin_unlock_bh(&nf_conntrack_lock);
2279 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2280 static struct nf_ct_event_notifier ctnl_notifier = {
2281 .fcn = ctnetlink_conntrack_event,
2284 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2285 .fcn = ctnetlink_expect_event,
2289 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2290 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2291 .attr_count = CTA_MAX,
2292 .policy = ct_nla_policy },
2293 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2294 .attr_count = CTA_MAX,
2295 .policy = ct_nla_policy },
2296 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2297 .attr_count = CTA_MAX,
2298 .policy = ct_nla_policy },
2299 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2300 .attr_count = CTA_MAX,
2301 .policy = ct_nla_policy },
2304 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2305 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2306 .attr_count = CTA_EXPECT_MAX,
2307 .policy = exp_nla_policy },
2308 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2309 .attr_count = CTA_EXPECT_MAX,
2310 .policy = exp_nla_policy },
2311 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2312 .attr_count = CTA_EXPECT_MAX,
2313 .policy = exp_nla_policy },
2316 static const struct nfnetlink_subsystem ctnl_subsys = {
2317 .name = "conntrack",
2318 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2319 .cb_count = IPCTNL_MSG_MAX,
2323 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2324 .name = "conntrack_expect",
2325 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2326 .cb_count = IPCTNL_MSG_EXP_MAX,
2330 MODULE_ALIAS("ip_conntrack_netlink");
2331 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2332 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2334 static int __net_init ctnetlink_net_init(struct net *net)
2336 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2339 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2341 pr_err("ctnetlink_init: cannot register notifier.\n");
2345 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2347 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2348 goto err_unreg_notifier;
2353 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2355 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2361 static void ctnetlink_net_exit(struct net *net)
2363 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2364 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2365 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2369 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2373 list_for_each_entry(net, net_exit_list, exit_list)
2374 ctnetlink_net_exit(net);
2377 static struct pernet_operations ctnetlink_net_ops = {
2378 .init = ctnetlink_net_init,
2379 .exit_batch = ctnetlink_net_exit_batch,
2382 static int __init ctnetlink_init(void)
2386 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2387 ret = nfnetlink_subsys_register(&ctnl_subsys);
2389 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2393 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2395 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2396 goto err_unreg_subsys;
2399 if (register_pernet_subsys(&ctnetlink_net_ops)) {
2400 pr_err("ctnetlink_init: cannot register pernet operations\n");
2401 goto err_unreg_exp_subsys;
2406 err_unreg_exp_subsys:
2407 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2409 nfnetlink_subsys_unregister(&ctnl_subsys);
2414 static void __exit ctnetlink_exit(void)
2416 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2418 unregister_pernet_subsys(&ctnetlink_net_ops);
2419 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2420 nfnetlink_subsys_unregister(&ctnl_subsys);
2423 module_init(ctnetlink_init);
2424 module_exit(ctnetlink_exit);