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-2008 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/skbuff.h>
26 #include <linux/errno.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/interrupt.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
49 MODULE_LICENSE("GPL");
51 static char __initdata version[] = "0.93";
54 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
55 const struct nf_conntrack_tuple *tuple,
56 struct nf_conntrack_l4proto *l4proto)
59 struct nlattr *nest_parms;
61 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
64 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
66 if (likely(l4proto->tuple_to_nlattr))
67 ret = l4proto->tuple_to_nlattr(skb, tuple);
69 nla_nest_end(skb, nest_parms);
78 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79 const struct nf_conntrack_tuple *tuple,
80 struct nf_conntrack_l3proto *l3proto)
83 struct nlattr *nest_parms;
85 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
89 if (likely(l3proto->tuple_to_nlattr))
90 ret = l3proto->tuple_to_nlattr(skb, tuple);
92 nla_nest_end(skb, nest_parms);
101 ctnetlink_dump_tuples(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
105 struct nf_conntrack_l3proto *l3proto;
106 struct nf_conntrack_l4proto *l4proto;
108 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
109 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
111 if (unlikely(ret < 0))
114 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
115 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
121 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
123 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
131 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
133 long timeout = (ct->timeout.expires - jiffies) / HZ;
138 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
146 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
148 struct nf_conntrack_l4proto *l4proto;
149 struct nlattr *nest_proto;
152 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
153 if (!l4proto->to_nlattr)
156 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
158 goto nla_put_failure;
160 ret = l4proto->to_nlattr(skb, nest_proto, ct);
162 nla_nest_end(skb, nest_proto);
171 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
173 struct nlattr *nest_helper;
174 const struct nf_conn_help *help = nfct_help(ct);
175 struct nf_conntrack_helper *helper;
180 helper = rcu_dereference(help->helper);
184 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
186 goto nla_put_failure;
187 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
189 if (helper->to_nlattr)
190 helper->to_nlattr(skb, ct);
192 nla_nest_end(skb, nest_helper);
201 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
202 enum ip_conntrack_dir dir)
204 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
205 struct nlattr *nest_count;
206 const struct nf_conn_counter *acct;
208 acct = nf_conn_acct_find(ct);
212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
214 goto nla_put_failure;
216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
217 cpu_to_be64(acct[dir].packets));
218 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
219 cpu_to_be64(acct[dir].bytes));
221 nla_nest_end(skb, nest_count);
229 #ifdef CONFIG_NF_CONNTRACK_MARK
231 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
233 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
240 #define ctnetlink_dump_mark(a, b) (0)
243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
245 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
247 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
254 #define ctnetlink_dump_secmark(a, b) (0)
257 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
260 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
262 struct nlattr *nest_parms;
264 if (!(ct->status & IPS_EXPECTED))
267 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
269 goto nla_put_failure;
270 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
271 goto nla_put_failure;
272 nla_nest_end(skb, nest_parms);
280 #ifdef CONFIG_NF_NAT_NEEDED
282 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
284 struct nlattr *nest_parms;
286 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
288 goto nla_put_failure;
290 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
291 htonl(natseq->correction_pos));
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
293 htonl(natseq->offset_before));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
295 htonl(natseq->offset_after));
297 nla_nest_end(skb, nest_parms);
306 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
308 struct nf_nat_seq *natseq;
309 struct nf_conn_nat *nat = nfct_nat(ct);
311 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
314 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
315 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
318 natseq = &nat->seq[IP_CT_DIR_REPLY];
319 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
325 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
329 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
331 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
339 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
341 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
349 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
350 int event, struct nf_conn *ct)
352 struct nlmsghdr *nlh;
353 struct nfgenmsg *nfmsg;
354 struct nlattr *nest_parms;
355 unsigned int flags = pid ? NLM_F_MULTI : 0;
357 event |= NFNL_SUBSYS_CTNETLINK << 8;
358 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
362 nfmsg = nlmsg_data(nlh);
363 nfmsg->nfgen_family = nf_ct_l3num(ct);
364 nfmsg->version = NFNETLINK_V0;
367 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
369 goto nla_put_failure;
370 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
371 goto nla_put_failure;
372 nla_nest_end(skb, nest_parms);
374 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
376 goto nla_put_failure;
377 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
378 goto nla_put_failure;
379 nla_nest_end(skb, nest_parms);
381 if (ctnetlink_dump_status(skb, ct) < 0 ||
382 ctnetlink_dump_timeout(skb, ct) < 0 ||
383 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
384 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
385 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
386 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
387 ctnetlink_dump_mark(skb, ct) < 0 ||
388 ctnetlink_dump_secmark(skb, ct) < 0 ||
389 ctnetlink_dump_id(skb, ct) < 0 ||
390 ctnetlink_dump_use(skb, ct) < 0 ||
391 ctnetlink_dump_master(skb, ct) < 0 ||
392 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
393 goto nla_put_failure;
400 nlmsg_cancel(skb, nlh);
404 #ifdef CONFIG_NF_CONNTRACK_EVENTS
406 ctnetlink_proto_size(const struct nf_conn *ct)
408 struct nf_conntrack_l3proto *l3proto;
409 struct nf_conntrack_l4proto *l4proto;
413 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
414 len += l3proto->nla_size;
416 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
417 len += l4proto->nla_size;
424 ctnetlink_nlmsg_size(const struct nf_conn *ct)
426 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
427 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
428 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
429 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
430 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
431 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
432 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
433 #ifdef CONFIG_NF_CT_ACCT
434 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
435 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
436 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
438 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
439 + nla_total_size(0) /* CTA_PROTOINFO */
440 + nla_total_size(0) /* CTA_HELP */
441 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
442 #ifdef CONFIG_NF_CONNTRACK_SECMARK
443 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
445 #ifdef CONFIG_NF_NAT_NEEDED
446 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
447 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
449 #ifdef CONFIG_NF_CONNTRACK_MARK
450 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
452 + ctnetlink_proto_size(ct)
457 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
459 struct nlmsghdr *nlh;
460 struct nfgenmsg *nfmsg;
461 struct nlattr *nest_parms;
462 struct nf_conn *ct = item->ct;
465 unsigned int flags = 0, group;
468 /* ignore our fake conntrack entry */
469 if (ct == &nf_conntrack_untracked)
472 if (events & (1 << IPCT_DESTROY)) {
473 type = IPCTNL_MSG_CT_DELETE;
474 group = NFNLGRP_CONNTRACK_DESTROY;
475 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
476 type = IPCTNL_MSG_CT_NEW;
477 flags = NLM_F_CREATE|NLM_F_EXCL;
478 group = NFNLGRP_CONNTRACK_NEW;
480 type = IPCTNL_MSG_CT_NEW;
481 group = NFNLGRP_CONNTRACK_UPDATE;
485 if (!item->report && !nfnetlink_has_listeners(group))
488 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
492 type |= NFNL_SUBSYS_CTNETLINK << 8;
493 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
497 nfmsg = nlmsg_data(nlh);
498 nfmsg->nfgen_family = nf_ct_l3num(ct);
499 nfmsg->version = NFNETLINK_V0;
503 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
505 goto nla_put_failure;
506 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
507 goto nla_put_failure;
508 nla_nest_end(skb, nest_parms);
510 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
512 goto nla_put_failure;
513 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
514 goto nla_put_failure;
515 nla_nest_end(skb, nest_parms);
517 if (ctnetlink_dump_id(skb, ct) < 0)
518 goto nla_put_failure;
520 if (ctnetlink_dump_status(skb, ct) < 0)
521 goto nla_put_failure;
523 if (events & (1 << IPCT_DESTROY)) {
524 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
525 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
526 goto nla_put_failure;
528 if (ctnetlink_dump_timeout(skb, ct) < 0)
529 goto nla_put_failure;
531 if (events & (1 << IPCT_PROTOINFO)
532 && ctnetlink_dump_protoinfo(skb, ct) < 0)
533 goto nla_put_failure;
535 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
536 && ctnetlink_dump_helpinfo(skb, ct) < 0)
537 goto nla_put_failure;
539 #ifdef CONFIG_NF_CONNTRACK_SECMARK
540 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
541 && ctnetlink_dump_secmark(skb, ct) < 0)
542 goto nla_put_failure;
545 if (events & (1 << IPCT_RELATED) &&
546 ctnetlink_dump_master(skb, ct) < 0)
547 goto nla_put_failure;
549 if (events & (1 << IPCT_NATSEQADJ) &&
550 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
551 goto nla_put_failure;
554 #ifdef CONFIG_NF_CONNTRACK_MARK
555 if ((events & (1 << IPCT_MARK) || ct->mark)
556 && ctnetlink_dump_mark(skb, ct) < 0)
557 goto nla_put_failure;
562 err = nfnetlink_send(skb, item->pid, group, item->report, GFP_ATOMIC);
563 if (err == -ENOBUFS || err == -EAGAIN)
570 nlmsg_cancel(skb, nlh);
574 nfnetlink_set_err(0, group, -ENOBUFS);
577 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
579 static int ctnetlink_done(struct netlink_callback *cb)
582 nf_ct_put((struct nf_conn *)cb->args[1]);
587 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
589 struct nf_conn *ct, *last;
590 struct nf_conntrack_tuple_hash *h;
591 struct hlist_nulls_node *n;
592 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
593 u_int8_t l3proto = nfmsg->nfgen_family;
596 last = (struct nf_conn *)cb->args[1];
597 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
599 hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
601 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
603 ct = nf_ct_tuplehash_to_ctrack(h);
604 if (!atomic_inc_not_zero(&ct->ct_general.use))
606 /* Dump entries of a given L3 protocol number.
607 * If it is not specified, ie. l3proto == 0,
608 * then dump everything. */
609 if (l3proto && nf_ct_l3num(ct) != l3proto)
616 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
618 IPCTNL_MSG_CT_NEW, ct) < 0) {
619 cb->args[1] = (unsigned long)ct;
623 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
624 IPCTNL_MSG_CT_GET_CTRZERO) {
625 struct nf_conn_counter *acct;
627 acct = nf_conn_acct_find(ct);
629 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
648 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
650 struct nlattr *tb[CTA_IP_MAX+1];
651 struct nf_conntrack_l3proto *l3proto;
654 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
657 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
659 if (likely(l3proto->nlattr_to_tuple)) {
660 ret = nla_validate_nested(attr, CTA_IP_MAX,
661 l3proto->nla_policy);
663 ret = l3proto->nlattr_to_tuple(tb, tuple);
671 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
672 [CTA_PROTO_NUM] = { .type = NLA_U8 },
676 ctnetlink_parse_tuple_proto(struct nlattr *attr,
677 struct nf_conntrack_tuple *tuple)
679 struct nlattr *tb[CTA_PROTO_MAX+1];
680 struct nf_conntrack_l4proto *l4proto;
683 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
687 if (!tb[CTA_PROTO_NUM])
689 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
692 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
694 if (likely(l4proto->nlattr_to_tuple)) {
695 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
696 l4proto->nla_policy);
698 ret = l4proto->nlattr_to_tuple(tb, tuple);
707 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
708 enum ctattr_tuple type, u_int8_t l3num)
710 struct nlattr *tb[CTA_TUPLE_MAX+1];
713 memset(tuple, 0, sizeof(*tuple));
715 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
717 if (!tb[CTA_TUPLE_IP])
720 tuple->src.l3num = l3num;
722 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
726 if (!tb[CTA_TUPLE_PROTO])
729 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
733 /* orig and expect tuples get DIR_ORIGINAL */
734 if (type == CTA_TUPLE_REPLY)
735 tuple->dst.dir = IP_CT_DIR_REPLY;
737 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
743 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
745 struct nlattr *tb[CTA_HELP_MAX+1];
747 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
749 if (!tb[CTA_HELP_NAME])
752 *helper_name = nla_data(tb[CTA_HELP_NAME]);
757 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
758 [CTA_STATUS] = { .type = NLA_U32 },
759 [CTA_TIMEOUT] = { .type = NLA_U32 },
760 [CTA_MARK] = { .type = NLA_U32 },
761 [CTA_USE] = { .type = NLA_U32 },
762 [CTA_ID] = { .type = NLA_U32 },
766 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
767 struct nlmsghdr *nlh, struct nlattr *cda[])
769 struct nf_conntrack_tuple_hash *h;
770 struct nf_conntrack_tuple tuple;
772 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
773 u_int8_t u3 = nfmsg->nfgen_family;
776 if (cda[CTA_TUPLE_ORIG])
777 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
778 else if (cda[CTA_TUPLE_REPLY])
779 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
781 /* Flush the whole table */
782 nf_conntrack_flush_report(&init_net,
791 h = nf_conntrack_find_get(&init_net, &tuple);
795 ct = nf_ct_tuplehash_to_ctrack(h);
798 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
799 if (id != (u32)(unsigned long)ct) {
805 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
807 nlmsg_report(nlh)) < 0) {
808 nf_ct_delete_from_lists(ct);
809 /* we failed to report the event, try later */
810 nf_ct_insert_dying_list(ct);
815 /* death_by_timeout would report the event again */
816 set_bit(IPS_DYING_BIT, &ct->status);
825 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
826 struct nlmsghdr *nlh, struct nlattr *cda[])
828 struct nf_conntrack_tuple_hash *h;
829 struct nf_conntrack_tuple tuple;
831 struct sk_buff *skb2 = NULL;
832 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
833 u_int8_t u3 = nfmsg->nfgen_family;
836 if (nlh->nlmsg_flags & NLM_F_DUMP)
837 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
840 if (cda[CTA_TUPLE_ORIG])
841 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
842 else if (cda[CTA_TUPLE_REPLY])
843 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
850 h = nf_conntrack_find_get(&init_net, &tuple);
854 ct = nf_ct_tuplehash_to_ctrack(h);
857 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
864 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
865 IPCTNL_MSG_CT_NEW, ct);
871 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
883 #ifdef CONFIG_NF_NAT_NEEDED
885 ctnetlink_parse_nat_setup(struct nf_conn *ct,
886 enum nf_nat_manip_type manip,
889 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
891 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
892 if (!parse_nat_setup) {
893 #ifdef CONFIG_MODULES
895 spin_unlock_bh(&nf_conntrack_lock);
897 if (request_module("nf-nat-ipv4") < 0) {
899 spin_lock_bh(&nf_conntrack_lock);
904 spin_lock_bh(&nf_conntrack_lock);
906 if (nfnetlink_parse_nat_setup_hook)
912 return parse_nat_setup(ct, manip, attr);
917 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
920 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
921 d = ct->status ^ status;
923 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
927 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
928 /* SEEN_REPLY bit can only be set */
931 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
932 /* ASSURED bit can only be set */
935 /* Be careful here, modifying NAT bits can screw up things,
936 * so don't let users modify them directly if they don't pass
938 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
943 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
945 #ifdef CONFIG_NF_NAT_NEEDED
948 if (cda[CTA_NAT_DST]) {
949 ret = ctnetlink_parse_nat_setup(ct,
955 if (cda[CTA_NAT_SRC]) {
956 ret = ctnetlink_parse_nat_setup(ct,
969 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
971 struct nf_conntrack_helper *helper;
972 struct nf_conn_help *help = nfct_help(ct);
973 char *helpname = NULL;
976 /* don't change helper of sibling connections */
980 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
984 if (!strcmp(helpname, "")) {
985 if (help && help->helper) {
986 /* we had a helper before ... */
987 nf_ct_remove_expectations(ct);
988 rcu_assign_pointer(help->helper, NULL);
994 helper = __nf_conntrack_helper_find_byname(helpname);
995 if (helper == NULL) {
996 #ifdef CONFIG_MODULES
997 spin_unlock_bh(&nf_conntrack_lock);
999 if (request_module("nfct-helper-%s", helpname) < 0) {
1000 spin_lock_bh(&nf_conntrack_lock);
1004 spin_lock_bh(&nf_conntrack_lock);
1005 helper = __nf_conntrack_helper_find_byname(helpname);
1013 if (help->helper == helper)
1017 /* need to zero data of old helper */
1018 memset(&help->help, 0, sizeof(help->help));
1020 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1025 rcu_assign_pointer(help->helper, helper);
1031 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
1033 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1035 if (!del_timer(&ct->timeout))
1038 ct->timeout.expires = jiffies + timeout * HZ;
1039 add_timer(&ct->timeout);
1045 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1047 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1048 struct nf_conntrack_l4proto *l4proto;
1051 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1054 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1055 if (l4proto->from_nlattr)
1056 err = l4proto->from_nlattr(tb, ct);
1062 #ifdef CONFIG_NF_NAT_NEEDED
1064 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1066 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1068 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1070 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1073 natseq->correction_pos =
1074 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1076 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1079 natseq->offset_before =
1080 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1082 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1085 natseq->offset_after =
1086 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1092 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1095 struct nf_conn_nat *nat = nfct_nat(ct);
1100 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1101 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1102 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1106 ct->status |= IPS_SEQ_ADJUST;
1109 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1110 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1111 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1115 ct->status |= IPS_SEQ_ADJUST;
1123 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1127 /* only allow NAT changes and master assignation for new conntracks */
1128 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1131 if (cda[CTA_HELP]) {
1132 err = ctnetlink_change_helper(ct, cda);
1137 if (cda[CTA_TIMEOUT]) {
1138 err = ctnetlink_change_timeout(ct, cda);
1143 if (cda[CTA_STATUS]) {
1144 err = ctnetlink_change_status(ct, cda);
1149 if (cda[CTA_PROTOINFO]) {
1150 err = ctnetlink_change_protoinfo(ct, cda);
1155 #if defined(CONFIG_NF_CONNTRACK_MARK)
1157 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1160 #ifdef CONFIG_NF_NAT_NEEDED
1161 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1162 err = ctnetlink_change_nat_seq_adj(ct, cda);
1171 static struct nf_conn *
1172 ctnetlink_create_conntrack(struct nlattr *cda[],
1173 struct nf_conntrack_tuple *otuple,
1174 struct nf_conntrack_tuple *rtuple,
1179 struct nf_conntrack_helper *helper;
1181 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1183 return ERR_PTR(-ENOMEM);
1185 if (!cda[CTA_TIMEOUT])
1187 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1189 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1190 ct->status |= IPS_CONFIRMED;
1193 if (cda[CTA_HELP]) {
1194 char *helpname = NULL;
1196 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1200 helper = __nf_conntrack_helper_find_byname(helpname);
1201 if (helper == NULL) {
1203 #ifdef CONFIG_MODULES
1204 if (request_module("nfct-helper-%s", helpname) < 0) {
1210 helper = __nf_conntrack_helper_find_byname(helpname);
1220 struct nf_conn_help *help;
1222 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1228 /* not in hash table yet so not strictly necessary */
1229 rcu_assign_pointer(help->helper, helper);
1232 /* try an implicit helper assignation */
1233 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1238 if (cda[CTA_STATUS]) {
1239 err = ctnetlink_change_status(ct, cda);
1244 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1245 err = ctnetlink_change_nat(ct, cda);
1250 #ifdef CONFIG_NF_NAT_NEEDED
1251 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1252 err = ctnetlink_change_nat_seq_adj(ct, cda);
1258 if (cda[CTA_PROTOINFO]) {
1259 err = ctnetlink_change_protoinfo(ct, cda);
1264 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1265 nf_ct_ecache_ext_add(ct, GFP_ATOMIC);
1267 #if defined(CONFIG_NF_CONNTRACK_MARK)
1269 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1272 /* setup master conntrack: this is a confirmed expectation */
1273 if (cda[CTA_TUPLE_MASTER]) {
1274 struct nf_conntrack_tuple master;
1275 struct nf_conntrack_tuple_hash *master_h;
1276 struct nf_conn *master_ct;
1278 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1282 master_h = nf_conntrack_find_get(&init_net, &master);
1283 if (master_h == NULL) {
1287 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1288 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1289 ct->master = master_ct;
1292 add_timer(&ct->timeout);
1293 nf_conntrack_hash_insert(ct);
1301 nf_conntrack_free(ct);
1302 return ERR_PTR(err);
1306 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1307 struct nlmsghdr *nlh, struct nlattr *cda[])
1309 struct nf_conntrack_tuple otuple, rtuple;
1310 struct nf_conntrack_tuple_hash *h = NULL;
1311 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1312 u_int8_t u3 = nfmsg->nfgen_family;
1315 if (cda[CTA_TUPLE_ORIG]) {
1316 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1321 if (cda[CTA_TUPLE_REPLY]) {
1322 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1327 spin_lock_bh(&nf_conntrack_lock);
1328 if (cda[CTA_TUPLE_ORIG])
1329 h = __nf_conntrack_find(&init_net, &otuple);
1330 else if (cda[CTA_TUPLE_REPLY])
1331 h = __nf_conntrack_find(&init_net, &rtuple);
1335 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1337 enum ip_conntrack_events events;
1339 ct = ctnetlink_create_conntrack(cda, &otuple,
1346 nf_conntrack_get(&ct->ct_general);
1347 spin_unlock_bh(&nf_conntrack_lock);
1348 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1349 events = IPCT_RELATED;
1353 nf_conntrack_eventmask_report((1 << IPCT_STATUS) |
1354 (1 << IPCT_HELPER) |
1355 (1 << IPCT_PROTOINFO) |
1356 (1 << IPCT_NATSEQADJ) |
1357 (1 << IPCT_MARK) | events,
1358 ct, NETLINK_CB(skb).pid,
1362 spin_unlock_bh(&nf_conntrack_lock);
1366 /* implicit 'else' */
1368 /* We manipulate the conntrack inside the global conntrack table lock,
1369 * so there's no need to increase the refcount */
1371 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1372 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1374 err = ctnetlink_change_conntrack(ct, cda);
1376 nf_conntrack_get(&ct->ct_general);
1377 spin_unlock_bh(&nf_conntrack_lock);
1378 nf_conntrack_eventmask_report((1 << IPCT_STATUS) |
1379 (1 << IPCT_HELPER) |
1380 (1 << IPCT_PROTOINFO) |
1381 (1 << IPCT_NATSEQADJ) |
1383 ct, NETLINK_CB(skb).pid,
1387 spin_unlock_bh(&nf_conntrack_lock);
1393 spin_unlock_bh(&nf_conntrack_lock);
1397 /***********************************************************************
1399 ***********************************************************************/
1402 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1403 const struct nf_conntrack_tuple *tuple,
1404 enum ctattr_expect type)
1406 struct nlattr *nest_parms;
1408 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1410 goto nla_put_failure;
1411 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1412 goto nla_put_failure;
1413 nla_nest_end(skb, nest_parms);
1422 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1423 const struct nf_conntrack_tuple *tuple,
1424 const struct nf_conntrack_tuple_mask *mask)
1427 struct nf_conntrack_l3proto *l3proto;
1428 struct nf_conntrack_l4proto *l4proto;
1429 struct nf_conntrack_tuple m;
1430 struct nlattr *nest_parms;
1432 memset(&m, 0xFF, sizeof(m));
1433 m.src.u.all = mask->src.u.all;
1434 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1436 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1438 goto nla_put_failure;
1440 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1441 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1443 if (unlikely(ret < 0))
1444 goto nla_put_failure;
1446 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1447 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1448 if (unlikely(ret < 0))
1449 goto nla_put_failure;
1451 nla_nest_end(skb, nest_parms);
1460 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1461 const struct nf_conntrack_expect *exp)
1463 struct nf_conn *master = exp->master;
1464 long timeout = (exp->timeout.expires - jiffies) / HZ;
1469 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1470 goto nla_put_failure;
1471 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1472 goto nla_put_failure;
1473 if (ctnetlink_exp_dump_tuple(skb,
1474 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1475 CTA_EXPECT_MASTER) < 0)
1476 goto nla_put_failure;
1478 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1479 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1488 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1489 int event, const struct nf_conntrack_expect *exp)
1491 struct nlmsghdr *nlh;
1492 struct nfgenmsg *nfmsg;
1493 unsigned int flags = pid ? NLM_F_MULTI : 0;
1495 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1496 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1500 nfmsg = nlmsg_data(nlh);
1501 nfmsg->nfgen_family = exp->tuple.src.l3num;
1502 nfmsg->version = NFNETLINK_V0;
1505 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1506 goto nla_put_failure;
1508 nlmsg_end(skb, nlh);
1513 nlmsg_cancel(skb, nlh);
1517 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1519 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1521 struct nlmsghdr *nlh;
1522 struct nfgenmsg *nfmsg;
1523 struct nf_conntrack_expect *exp = item->exp;
1524 struct sk_buff *skb;
1528 if (events & (1 << IPEXP_NEW)) {
1529 type = IPCTNL_MSG_EXP_NEW;
1530 flags = NLM_F_CREATE|NLM_F_EXCL;
1534 if (!item->report &&
1535 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1538 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1542 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1543 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1547 nfmsg = nlmsg_data(nlh);
1548 nfmsg->nfgen_family = exp->tuple.src.l3num;
1549 nfmsg->version = NFNETLINK_V0;
1553 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1554 goto nla_put_failure;
1557 nlmsg_end(skb, nlh);
1558 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
1559 item->report, GFP_ATOMIC);
1564 nlmsg_cancel(skb, nlh);
1568 nfnetlink_set_err(0, 0, -ENOBUFS);
1572 static int ctnetlink_exp_done(struct netlink_callback *cb)
1575 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1580 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1582 struct net *net = &init_net;
1583 struct nf_conntrack_expect *exp, *last;
1584 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1585 struct hlist_node *n;
1586 u_int8_t l3proto = nfmsg->nfgen_family;
1589 last = (struct nf_conntrack_expect *)cb->args[1];
1590 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1592 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1594 if (l3proto && exp->tuple.src.l3num != l3proto)
1601 if (ctnetlink_exp_fill_info(skb,
1602 NETLINK_CB(cb->skb).pid,
1606 if (!atomic_inc_not_zero(&exp->use))
1608 cb->args[1] = (unsigned long)exp;
1620 nf_ct_expect_put(last);
1625 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1626 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1627 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1631 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1632 struct nlmsghdr *nlh, struct nlattr *cda[])
1634 struct nf_conntrack_tuple tuple;
1635 struct nf_conntrack_expect *exp;
1636 struct sk_buff *skb2;
1637 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1638 u_int8_t u3 = nfmsg->nfgen_family;
1641 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1642 return netlink_dump_start(ctnl, skb, nlh,
1643 ctnetlink_exp_dump_table,
1644 ctnetlink_exp_done);
1647 if (cda[CTA_EXPECT_MASTER])
1648 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1655 exp = nf_ct_expect_find_get(&init_net, &tuple);
1659 if (cda[CTA_EXPECT_ID]) {
1660 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1661 if (ntohl(id) != (u32)(unsigned long)exp) {
1662 nf_ct_expect_put(exp);
1668 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1673 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1674 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1679 nf_ct_expect_put(exp);
1681 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1686 nf_ct_expect_put(exp);
1691 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1692 struct nlmsghdr *nlh, struct nlattr *cda[])
1694 struct nf_conntrack_expect *exp;
1695 struct nf_conntrack_tuple tuple;
1696 struct nf_conntrack_helper *h;
1697 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1698 struct hlist_node *n, *next;
1699 u_int8_t u3 = nfmsg->nfgen_family;
1703 if (cda[CTA_EXPECT_TUPLE]) {
1704 /* delete a single expect by tuple */
1705 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1709 /* bump usage count to 2 */
1710 exp = nf_ct_expect_find_get(&init_net, &tuple);
1714 if (cda[CTA_EXPECT_ID]) {
1715 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1716 if (ntohl(id) != (u32)(unsigned long)exp) {
1717 nf_ct_expect_put(exp);
1722 /* after list removal, usage count == 1 */
1723 nf_ct_unexpect_related(exp);
1724 /* have to put what we 'get' above.
1725 * after this line usage count == 0 */
1726 nf_ct_expect_put(exp);
1727 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1728 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1729 struct nf_conn_help *m_help;
1731 /* delete all expectations for this helper */
1732 spin_lock_bh(&nf_conntrack_lock);
1733 h = __nf_conntrack_helper_find_byname(name);
1735 spin_unlock_bh(&nf_conntrack_lock);
1738 for (i = 0; i < nf_ct_expect_hsize; i++) {
1739 hlist_for_each_entry_safe(exp, n, next,
1740 &init_net.ct.expect_hash[i],
1742 m_help = nfct_help(exp->master);
1743 if (m_help->helper == h
1744 && del_timer(&exp->timeout)) {
1745 nf_ct_unlink_expect(exp);
1746 nf_ct_expect_put(exp);
1750 spin_unlock_bh(&nf_conntrack_lock);
1752 /* This basically means we have to flush everything*/
1753 spin_lock_bh(&nf_conntrack_lock);
1754 for (i = 0; i < nf_ct_expect_hsize; i++) {
1755 hlist_for_each_entry_safe(exp, n, next,
1756 &init_net.ct.expect_hash[i],
1758 if (del_timer(&exp->timeout)) {
1759 nf_ct_unlink_expect(exp);
1760 nf_ct_expect_put(exp);
1764 spin_unlock_bh(&nf_conntrack_lock);
1770 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1776 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
1778 struct nf_conntrack_tuple tuple, mask, master_tuple;
1779 struct nf_conntrack_tuple_hash *h = NULL;
1780 struct nf_conntrack_expect *exp;
1782 struct nf_conn_help *help;
1785 /* caller guarantees that those three CTA_EXPECT_* exist */
1786 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1789 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1792 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1796 /* Look for master conntrack of this expectation */
1797 h = nf_conntrack_find_get(&init_net, &master_tuple);
1800 ct = nf_ct_tuplehash_to_ctrack(h);
1801 help = nfct_help(ct);
1803 if (!help || !help->helper) {
1804 /* such conntrack hasn't got any helper, abort */
1809 exp = nf_ct_expect_alloc(ct);
1816 exp->expectfn = NULL;
1820 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1821 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1822 exp->mask.src.u.all = mask.src.u.all;
1824 err = nf_ct_expect_related_report(exp, pid, report);
1825 nf_ct_expect_put(exp);
1828 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1833 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1834 struct nlmsghdr *nlh, struct nlattr *cda[])
1836 struct nf_conntrack_tuple tuple;
1837 struct nf_conntrack_expect *exp;
1838 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1839 u_int8_t u3 = nfmsg->nfgen_family;
1842 if (!cda[CTA_EXPECT_TUPLE]
1843 || !cda[CTA_EXPECT_MASK]
1844 || !cda[CTA_EXPECT_MASTER])
1847 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1851 spin_lock_bh(&nf_conntrack_lock);
1852 exp = __nf_ct_expect_find(&init_net, &tuple);
1855 spin_unlock_bh(&nf_conntrack_lock);
1857 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1858 err = ctnetlink_create_expect(cda,
1860 NETLINK_CB(skb).pid,
1867 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1868 err = ctnetlink_change_expect(exp, cda);
1869 spin_unlock_bh(&nf_conntrack_lock);
1874 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1875 static struct nf_ct_event_notifier ctnl_notifier = {
1876 .fcn = ctnetlink_conntrack_event,
1879 static struct nf_exp_event_notifier ctnl_notifier_exp = {
1880 .fcn = ctnetlink_expect_event,
1884 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1885 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1886 .attr_count = CTA_MAX,
1887 .policy = ct_nla_policy },
1888 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1889 .attr_count = CTA_MAX,
1890 .policy = ct_nla_policy },
1891 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1892 .attr_count = CTA_MAX,
1893 .policy = ct_nla_policy },
1894 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1895 .attr_count = CTA_MAX,
1896 .policy = ct_nla_policy },
1899 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1900 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1901 .attr_count = CTA_EXPECT_MAX,
1902 .policy = exp_nla_policy },
1903 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1904 .attr_count = CTA_EXPECT_MAX,
1905 .policy = exp_nla_policy },
1906 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1907 .attr_count = CTA_EXPECT_MAX,
1908 .policy = exp_nla_policy },
1911 static const struct nfnetlink_subsystem ctnl_subsys = {
1912 .name = "conntrack",
1913 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1914 .cb_count = IPCTNL_MSG_MAX,
1918 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1919 .name = "conntrack_expect",
1920 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1921 .cb_count = IPCTNL_MSG_EXP_MAX,
1925 MODULE_ALIAS("ip_conntrack_netlink");
1926 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1927 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1929 static int __init ctnetlink_init(void)
1933 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1934 ret = nfnetlink_subsys_register(&ctnl_subsys);
1936 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1940 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1942 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1943 goto err_unreg_subsys;
1946 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1947 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1949 printk("ctnetlink_init: cannot register notifier.\n");
1950 goto err_unreg_exp_subsys;
1953 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1955 printk("ctnetlink_init: cannot expect register notifier.\n");
1956 goto err_unreg_notifier;
1962 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1964 nf_conntrack_unregister_notifier(&ctnl_notifier);
1965 err_unreg_exp_subsys:
1966 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1969 nfnetlink_subsys_unregister(&ctnl_subsys);
1974 static void __exit ctnetlink_exit(void)
1976 printk("ctnetlink: unregistering from nfnetlink.\n");
1978 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1979 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1980 nf_conntrack_unregister_notifier(&ctnl_notifier);
1983 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1984 nfnetlink_subsys_unregister(&ctnl_subsys);
1988 module_init(ctnetlink_init);
1989 module_exit(ctnetlink_exit);