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-2007 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/types.h>
22 #include <linux/timer.h>
23 #include <linux/skbuff.h>
24 #include <linux/errno.h>
25 #include <linux/netlink.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/notifier.h>
30 #include <linux/netfilter.h>
31 #include <net/netlink.h>
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_expect.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_l3proto.h>
37 #include <net/netfilter/nf_conntrack_l4proto.h>
38 #include <net/netfilter/nf_conntrack_tuple.h>
39 #ifdef CONFIG_NF_NAT_NEEDED
40 #include <net/netfilter/nf_nat_core.h>
41 #include <net/netfilter/nf_nat_protocol.h>
44 #include <linux/netfilter/nfnetlink.h>
45 #include <linux/netfilter/nfnetlink_conntrack.h>
47 MODULE_LICENSE("GPL");
49 static char __initdata version[] = "0.93";
52 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
53 const struct nf_conntrack_tuple *tuple,
54 struct nf_conntrack_l4proto *l4proto)
57 struct nlattr *nest_parms;
59 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
62 NLA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
64 if (likely(l4proto->tuple_to_nlattr))
65 ret = l4proto->tuple_to_nlattr(skb, tuple);
67 nla_nest_end(skb, nest_parms);
76 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
77 const struct nf_conntrack_tuple *tuple,
78 struct nf_conntrack_l3proto *l3proto)
81 struct nlattr *nest_parms;
83 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
87 if (likely(l3proto->tuple_to_nlattr))
88 ret = l3proto->tuple_to_nlattr(skb, tuple);
90 nla_nest_end(skb, nest_parms);
99 ctnetlink_dump_tuples(struct sk_buff *skb,
100 const struct nf_conntrack_tuple *tuple)
103 struct nf_conntrack_l3proto *l3proto;
104 struct nf_conntrack_l4proto *l4proto;
106 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
107 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
108 nf_ct_l3proto_put(l3proto);
110 if (unlikely(ret < 0))
113 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
114 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
115 nf_ct_l4proto_put(l4proto);
121 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
123 __be32 status = htonl((u_int32_t) ct->status);
124 NLA_PUT(skb, CTA_STATUS, sizeof(status), &status);
132 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
134 long timeout_l = ct->timeout.expires - jiffies;
140 timeout = htonl(timeout_l / HZ);
142 NLA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
150 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
152 struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
153 struct nlattr *nest_proto;
156 if (!l4proto->to_nlattr) {
157 nf_ct_l4proto_put(l4proto);
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 nf_ct_l4proto_put(l4proto);
169 nla_nest_end(skb, nest_proto);
174 nf_ct_l4proto_put(l4proto);
179 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
181 struct nlattr *nest_helper;
182 const struct nf_conn_help *help = nfct_help(ct);
183 struct nf_conntrack_helper *helper;
189 helper = rcu_dereference(help->helper);
193 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
195 goto nla_put_failure;
196 NLA_PUT(skb, CTA_HELP_NAME, strlen(helper->name), helper->name);
198 if (helper->to_nlattr)
199 helper->to_nlattr(skb, ct);
201 nla_nest_end(skb, nest_helper);
211 #ifdef CONFIG_NF_CT_ACCT
213 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
214 enum ip_conntrack_dir dir)
216 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
217 struct nlattr *nest_count;
220 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
222 goto nla_put_failure;
224 tmp = htonl(ct->counters[dir].packets);
225 NLA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
227 tmp = htonl(ct->counters[dir].bytes);
228 NLA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
230 nla_nest_end(skb, nest_count);
238 #define ctnetlink_dump_counters(a, b, c) (0)
241 #ifdef CONFIG_NF_CONNTRACK_MARK
243 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
245 __be32 mark = htonl(ct->mark);
247 NLA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
254 #define ctnetlink_dump_mark(a, b) (0)
257 #ifdef CONFIG_NF_NAT_NEEDED
259 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
262 struct nlattr *nest_parms;
264 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
266 goto nla_put_failure;
268 tmp = htonl(natseq->correction_pos);
269 NLA_PUT(skb, CTA_NAT_SEQ_CORRECTION_POS, sizeof(tmp), &tmp);
270 tmp = htonl(natseq->offset_before);
271 NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_BEFORE, sizeof(tmp), &tmp);
272 tmp = htonl(natseq->offset_after);
273 NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_AFTER, sizeof(tmp), &tmp);
275 nla_nest_end(skb, nest_parms);
284 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
286 struct nf_nat_seq *natseq;
287 struct nf_conn_nat *nat = nfct_nat(ct);
289 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
292 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
293 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
296 natseq = &nat->seq[IP_CT_DIR_REPLY];
297 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
303 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
307 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
309 __be32 id = htonl((unsigned long)ct);
310 NLA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
318 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
320 __be32 use = htonl(atomic_read(&ct->ct_general.use));
322 NLA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
329 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
332 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
333 int event, int nowait,
334 const struct nf_conn *ct)
336 struct nlmsghdr *nlh;
337 struct nfgenmsg *nfmsg;
338 struct nlattr *nest_parms;
339 unsigned char *b = skb_tail_pointer(skb);
341 event |= NFNL_SUBSYS_CTNETLINK << 8;
342 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
343 nfmsg = NLMSG_DATA(nlh);
345 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
346 nfmsg->nfgen_family =
347 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
348 nfmsg->version = NFNETLINK_V0;
351 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
353 goto nla_put_failure;
354 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
355 goto nla_put_failure;
356 nla_nest_end(skb, nest_parms);
358 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
360 goto nla_put_failure;
361 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
362 goto nla_put_failure;
363 nla_nest_end(skb, nest_parms);
365 if (ctnetlink_dump_status(skb, ct) < 0 ||
366 ctnetlink_dump_timeout(skb, ct) < 0 ||
367 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
368 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
369 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
370 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
371 ctnetlink_dump_mark(skb, ct) < 0 ||
372 ctnetlink_dump_id(skb, ct) < 0 ||
373 ctnetlink_dump_use(skb, ct) < 0 ||
374 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
375 goto nla_put_failure;
377 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
386 #ifdef CONFIG_NF_CONNTRACK_EVENTS
387 static int ctnetlink_conntrack_event(struct notifier_block *this,
388 unsigned long events, void *ptr)
390 struct nlmsghdr *nlh;
391 struct nfgenmsg *nfmsg;
392 struct nlattr *nest_parms;
393 struct nf_conn *ct = (struct nf_conn *)ptr;
397 unsigned int flags = 0, group;
399 /* ignore our fake conntrack entry */
400 if (ct == &nf_conntrack_untracked)
403 if (events & IPCT_DESTROY) {
404 type = IPCTNL_MSG_CT_DELETE;
405 group = NFNLGRP_CONNTRACK_DESTROY;
406 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
407 type = IPCTNL_MSG_CT_NEW;
408 flags = NLM_F_CREATE|NLM_F_EXCL;
409 group = NFNLGRP_CONNTRACK_NEW;
410 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
411 type = IPCTNL_MSG_CT_NEW;
412 group = NFNLGRP_CONNTRACK_UPDATE;
416 if (!nfnetlink_has_listeners(group))
419 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
425 type |= NFNL_SUBSYS_CTNETLINK << 8;
426 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
427 nfmsg = NLMSG_DATA(nlh);
429 nlh->nlmsg_flags = flags;
430 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
431 nfmsg->version = NFNETLINK_V0;
434 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
436 goto nla_put_failure;
437 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
438 goto nla_put_failure;
439 nla_nest_end(skb, nest_parms);
441 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
443 goto nla_put_failure;
444 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
445 goto nla_put_failure;
446 nla_nest_end(skb, nest_parms);
448 if (events & IPCT_DESTROY) {
449 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
450 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
451 goto nla_put_failure;
453 if (ctnetlink_dump_status(skb, ct) < 0)
454 goto nla_put_failure;
456 if (ctnetlink_dump_timeout(skb, ct) < 0)
457 goto nla_put_failure;
459 if (events & IPCT_PROTOINFO
460 && ctnetlink_dump_protoinfo(skb, ct) < 0)
461 goto nla_put_failure;
463 if ((events & IPCT_HELPER || nfct_help(ct))
464 && ctnetlink_dump_helpinfo(skb, ct) < 0)
465 goto nla_put_failure;
467 #ifdef CONFIG_NF_CONNTRACK_MARK
468 if ((events & IPCT_MARK || ct->mark)
469 && ctnetlink_dump_mark(skb, ct) < 0)
470 goto nla_put_failure;
473 if (events & IPCT_COUNTER_FILLING &&
474 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
475 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
476 goto nla_put_failure;
478 if (events & IPCT_NATSEQADJ &&
479 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
480 goto nla_put_failure;
483 nlh->nlmsg_len = skb->tail - b;
484 nfnetlink_send(skb, 0, group, 0);
492 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
494 static int ctnetlink_done(struct netlink_callback *cb)
497 nf_ct_put((struct nf_conn *)cb->args[1]);
501 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
504 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
506 struct nf_conn *ct, *last;
507 struct nf_conntrack_tuple_hash *h;
508 struct hlist_node *n;
509 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
510 u_int8_t l3proto = nfmsg->nfgen_family;
512 read_lock_bh(&nf_conntrack_lock);
513 last = (struct nf_conn *)cb->args[1];
514 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
516 hlist_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
518 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
520 ct = nf_ct_tuplehash_to_ctrack(h);
521 /* Dump entries of a given L3 protocol number.
522 * If it is not specified, ie. l3proto == 0,
523 * then dump everything. */
524 if (l3proto && L3PROTO(ct) != l3proto)
531 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
535 nf_conntrack_get(&ct->ct_general);
536 cb->args[1] = (unsigned long)ct;
539 #ifdef CONFIG_NF_CT_ACCT
540 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
541 IPCTNL_MSG_CT_GET_CTRZERO)
542 memset(&ct->counters, 0, sizeof(ct->counters));
551 read_unlock_bh(&nf_conntrack_lock);
559 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
561 struct nlattr *tb[CTA_IP_MAX+1];
562 struct nf_conntrack_l3proto *l3proto;
565 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
567 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
569 if (likely(l3proto->nlattr_to_tuple)) {
570 ret = nla_validate_nested(attr, CTA_IP_MAX,
571 l3proto->nla_policy);
573 ret = l3proto->nlattr_to_tuple(tb, tuple);
576 nf_ct_l3proto_put(l3proto);
581 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
582 [CTA_PROTO_NUM] = { .type = NLA_U8 },
586 ctnetlink_parse_tuple_proto(struct nlattr *attr,
587 struct nf_conntrack_tuple *tuple)
589 struct nlattr *tb[CTA_PROTO_MAX+1];
590 struct nf_conntrack_l4proto *l4proto;
593 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
597 if (!tb[CTA_PROTO_NUM])
599 tuple->dst.protonum = *(u_int8_t *)nla_data(tb[CTA_PROTO_NUM]);
601 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
603 if (likely(l4proto->nlattr_to_tuple)) {
604 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
605 l4proto->nla_policy);
607 ret = l4proto->nlattr_to_tuple(tb, tuple);
610 nf_ct_l4proto_put(l4proto);
616 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
617 enum ctattr_tuple type, u_int8_t l3num)
619 struct nlattr *tb[CTA_TUPLE_MAX+1];
622 memset(tuple, 0, sizeof(*tuple));
624 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
626 if (!tb[CTA_TUPLE_IP])
629 tuple->src.l3num = l3num;
631 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
635 if (!tb[CTA_TUPLE_PROTO])
638 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
642 /* orig and expect tuples get DIR_ORIGINAL */
643 if (type == CTA_TUPLE_REPLY)
644 tuple->dst.dir = IP_CT_DIR_REPLY;
646 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
651 #ifdef CONFIG_NF_NAT_NEEDED
652 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
653 [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
654 [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
657 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
658 const struct nf_conn *ct,
659 struct nf_nat_range *range)
661 struct nlattr *tb[CTA_PROTONAT_MAX+1];
662 struct nf_nat_protocol *npt;
665 err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
669 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
671 if (!npt->nlattr_to_range) {
672 nf_nat_proto_put(npt);
676 /* nlattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
677 if (npt->nlattr_to_range(tb, range) > 0)
678 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
680 nf_nat_proto_put(npt);
685 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
686 [CTA_NAT_MINIP] = { .type = NLA_U32 },
687 [CTA_NAT_MAXIP] = { .type = NLA_U32 },
691 nfnetlink_parse_nat(struct nlattr *nat,
692 const struct nf_conn *ct, struct nf_nat_range *range)
694 struct nlattr *tb[CTA_NAT_MAX+1];
697 memset(range, 0, sizeof(*range));
699 err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
703 if (tb[CTA_NAT_MINIP])
704 range->min_ip = *(__be32 *)nla_data(tb[CTA_NAT_MINIP]);
706 if (!tb[CTA_NAT_MAXIP])
707 range->max_ip = range->min_ip;
709 range->max_ip = *(__be32 *)nla_data(tb[CTA_NAT_MAXIP]);
712 range->flags |= IP_NAT_RANGE_MAP_IPS;
714 if (!tb[CTA_NAT_PROTO])
717 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
726 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
728 struct nlattr *tb[CTA_HELP_MAX+1];
730 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
732 if (!tb[CTA_HELP_NAME])
735 *helper_name = nla_data(tb[CTA_HELP_NAME]);
740 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
741 [CTA_STATUS] = { .type = NLA_U32 },
742 [CTA_TIMEOUT] = { .type = NLA_U32 },
743 [CTA_MARK] = { .type = NLA_U32 },
744 [CTA_USE] = { .type = NLA_U32 },
745 [CTA_ID] = { .type = NLA_U32 },
749 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
750 struct nlmsghdr *nlh, struct nlattr *cda[])
752 struct nf_conntrack_tuple_hash *h;
753 struct nf_conntrack_tuple tuple;
755 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
756 u_int8_t u3 = nfmsg->nfgen_family;
759 if (cda[CTA_TUPLE_ORIG])
760 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
761 else if (cda[CTA_TUPLE_REPLY])
762 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
764 /* Flush the whole table */
765 nf_conntrack_flush();
772 h = nf_conntrack_find_get(&tuple);
776 ct = nf_ct_tuplehash_to_ctrack(h);
779 u_int32_t id = ntohl(*(__be32 *)nla_data(cda[CTA_ID]));
780 if (id != (u32)(unsigned long)ct) {
785 if (del_timer(&ct->timeout))
786 ct->timeout.function((unsigned long)ct);
794 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
795 struct nlmsghdr *nlh, struct nlattr *cda[])
797 struct nf_conntrack_tuple_hash *h;
798 struct nf_conntrack_tuple tuple;
800 struct sk_buff *skb2 = NULL;
801 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
802 u_int8_t u3 = nfmsg->nfgen_family;
805 if (nlh->nlmsg_flags & NLM_F_DUMP) {
806 #ifndef CONFIG_NF_CT_ACCT
807 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
810 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
814 if (cda[CTA_TUPLE_ORIG])
815 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
816 else if (cda[CTA_TUPLE_REPLY])
817 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
824 h = nf_conntrack_find_get(&tuple);
828 ct = nf_ct_tuplehash_to_ctrack(h);
831 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
837 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
838 IPCTNL_MSG_CT_NEW, 1, ct);
843 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
856 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
859 unsigned int status = ntohl(*(__be32 *)nla_data(cda[CTA_STATUS]));
860 d = ct->status ^ status;
862 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
866 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
867 /* SEEN_REPLY bit can only be set */
871 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
872 /* ASSURED bit can only be set */
875 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
876 #ifndef CONFIG_NF_NAT_NEEDED
879 struct nf_nat_range range;
881 if (cda[CTA_NAT_DST]) {
882 if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
885 if (nf_nat_initialized(ct,
886 HOOK2MANIP(NF_INET_PRE_ROUTING)))
888 nf_nat_setup_info(ct, &range, NF_INET_PRE_ROUTING);
890 if (cda[CTA_NAT_SRC]) {
891 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
894 if (nf_nat_initialized(ct,
895 HOOK2MANIP(NF_INET_POST_ROUTING)))
897 nf_nat_setup_info(ct, &range, NF_INET_POST_ROUTING);
902 /* Be careful here, modifying NAT bits can screw up things,
903 * so don't let users modify them directly if they don't pass
905 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
911 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
913 struct nf_conntrack_helper *helper;
914 struct nf_conn_help *help = nfct_help(ct);
918 /* don't change helper of sibling connections */
922 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
926 if (!strcmp(helpname, "")) {
927 if (help && help->helper) {
928 /* we had a helper before ... */
929 nf_ct_remove_expectations(ct);
930 rcu_assign_pointer(help->helper, NULL);
936 helper = __nf_conntrack_helper_find_byname(helpname);
941 if (help->helper == helper)
945 /* need to zero data of old helper */
946 memset(&help->help, 0, sizeof(help->help));
948 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
953 rcu_assign_pointer(help->helper, helper);
959 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
961 u_int32_t timeout = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT]));
963 if (!del_timer(&ct->timeout))
966 ct->timeout.expires = jiffies + timeout * HZ;
967 add_timer(&ct->timeout);
973 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
975 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
976 struct nf_conntrack_l4proto *l4proto;
977 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
978 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
981 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
983 l4proto = nf_ct_l4proto_find_get(l3num, npt);
985 if (l4proto->from_nlattr)
986 err = l4proto->from_nlattr(tb, ct);
987 nf_ct_l4proto_put(l4proto);
992 #ifdef CONFIG_NF_NAT_NEEDED
994 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
996 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
998 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1000 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1003 natseq->correction_pos =
1004 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1006 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1009 natseq->offset_before =
1010 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1012 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1015 natseq->offset_after =
1016 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1022 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1025 struct nf_conn_nat *nat = nfct_nat(ct);
1030 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1031 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1032 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1036 ct->status |= IPS_SEQ_ADJUST;
1039 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1040 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1041 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1045 ct->status |= IPS_SEQ_ADJUST;
1053 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1057 if (cda[CTA_HELP]) {
1058 err = ctnetlink_change_helper(ct, cda);
1063 if (cda[CTA_TIMEOUT]) {
1064 err = ctnetlink_change_timeout(ct, cda);
1069 if (cda[CTA_STATUS]) {
1070 err = ctnetlink_change_status(ct, cda);
1075 if (cda[CTA_PROTOINFO]) {
1076 err = ctnetlink_change_protoinfo(ct, cda);
1081 #if defined(CONFIG_NF_CONNTRACK_MARK)
1083 ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK]));
1086 #ifdef CONFIG_NF_NAT_NEEDED
1087 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1088 err = ctnetlink_change_nat_seq_adj(ct, cda);
1098 ctnetlink_create_conntrack(struct nlattr *cda[],
1099 struct nf_conntrack_tuple *otuple,
1100 struct nf_conntrack_tuple *rtuple,
1101 struct nf_conn *master_ct)
1105 struct nf_conn_help *help;
1106 struct nf_conntrack_helper *helper;
1108 ct = nf_conntrack_alloc(otuple, rtuple);
1109 if (ct == NULL || IS_ERR(ct))
1112 if (!cda[CTA_TIMEOUT])
1114 ct->timeout.expires = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT]));
1116 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1117 ct->status |= IPS_CONFIRMED;
1119 if (cda[CTA_STATUS]) {
1120 err = ctnetlink_change_status(ct, cda);
1125 if (cda[CTA_PROTOINFO]) {
1126 err = ctnetlink_change_protoinfo(ct, cda);
1131 #if defined(CONFIG_NF_CONNTRACK_MARK)
1133 ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK]));
1136 helper = nf_ct_helper_find_get(rtuple);
1138 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1140 nf_ct_helper_put(helper);
1144 /* not in hash table yet so not strictly necessary */
1145 rcu_assign_pointer(help->helper, helper);
1148 /* setup master conntrack: this is a confirmed expectation */
1150 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1151 ct->master = master_ct;
1154 add_timer(&ct->timeout);
1155 nf_conntrack_hash_insert(ct);
1158 nf_ct_helper_put(helper);
1163 nf_conntrack_free(ct);
1168 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1169 struct nlmsghdr *nlh, struct nlattr *cda[])
1171 struct nf_conntrack_tuple otuple, rtuple;
1172 struct nf_conntrack_tuple_hash *h = NULL;
1173 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1174 u_int8_t u3 = nfmsg->nfgen_family;
1177 if (cda[CTA_TUPLE_ORIG]) {
1178 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1183 if (cda[CTA_TUPLE_REPLY]) {
1184 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1189 write_lock_bh(&nf_conntrack_lock);
1190 if (cda[CTA_TUPLE_ORIG])
1191 h = __nf_conntrack_find(&otuple, NULL);
1192 else if (cda[CTA_TUPLE_REPLY])
1193 h = __nf_conntrack_find(&rtuple, NULL);
1196 struct nf_conntrack_tuple master;
1197 struct nf_conntrack_tuple_hash *master_h = NULL;
1198 struct nf_conn *master_ct = NULL;
1200 if (cda[CTA_TUPLE_MASTER]) {
1201 err = ctnetlink_parse_tuple(cda,
1208 master_h = __nf_conntrack_find(&master, NULL);
1209 if (master_h == NULL) {
1213 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1214 atomic_inc(&master_ct->ct_general.use);
1217 write_unlock_bh(&nf_conntrack_lock);
1219 if (nlh->nlmsg_flags & NLM_F_CREATE)
1220 err = ctnetlink_create_conntrack(cda,
1224 if (err < 0 && master_ct)
1225 nf_ct_put(master_ct);
1229 /* implicit 'else' */
1231 /* We manipulate the conntrack inside the global conntrack table lock,
1232 * so there's no need to increase the refcount */
1234 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1235 /* we only allow nat config for new conntracks */
1236 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1240 /* can't link an existing conntrack to a master */
1241 if (cda[CTA_TUPLE_MASTER]) {
1245 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1250 write_unlock_bh(&nf_conntrack_lock);
1254 /***********************************************************************
1256 ***********************************************************************/
1259 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1260 const struct nf_conntrack_tuple *tuple,
1261 enum ctattr_expect type)
1263 struct nlattr *nest_parms;
1265 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1267 goto nla_put_failure;
1268 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1269 goto nla_put_failure;
1270 nla_nest_end(skb, nest_parms);
1279 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1280 const struct nf_conntrack_tuple *tuple,
1281 const struct nf_conntrack_tuple_mask *mask)
1284 struct nf_conntrack_l3proto *l3proto;
1285 struct nf_conntrack_l4proto *l4proto;
1286 struct nf_conntrack_tuple m;
1287 struct nlattr *nest_parms;
1289 memset(&m, 0xFF, sizeof(m));
1290 m.src.u.all = mask->src.u.all;
1291 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1293 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1295 goto nla_put_failure;
1297 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1298 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1299 nf_ct_l3proto_put(l3proto);
1301 if (unlikely(ret < 0))
1302 goto nla_put_failure;
1304 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1305 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1306 nf_ct_l4proto_put(l4proto);
1307 if (unlikely(ret < 0))
1308 goto nla_put_failure;
1310 nla_nest_end(skb, nest_parms);
1319 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1320 const struct nf_conntrack_expect *exp)
1322 struct nf_conn *master = exp->master;
1323 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1324 __be32 id = htonl((unsigned long)exp);
1326 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1327 goto nla_put_failure;
1328 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1329 goto nla_put_failure;
1330 if (ctnetlink_exp_dump_tuple(skb,
1331 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1332 CTA_EXPECT_MASTER) < 0)
1333 goto nla_put_failure;
1335 NLA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1336 NLA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1345 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1348 const struct nf_conntrack_expect *exp)
1350 struct nlmsghdr *nlh;
1351 struct nfgenmsg *nfmsg;
1352 unsigned char *b = skb_tail_pointer(skb);
1354 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1355 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1356 nfmsg = NLMSG_DATA(nlh);
1358 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1359 nfmsg->nfgen_family = exp->tuple.src.l3num;
1360 nfmsg->version = NFNETLINK_V0;
1363 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1364 goto nla_put_failure;
1366 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1375 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1376 static int ctnetlink_expect_event(struct notifier_block *this,
1377 unsigned long events, void *ptr)
1379 struct nlmsghdr *nlh;
1380 struct nfgenmsg *nfmsg;
1381 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1382 struct sk_buff *skb;
1387 if (events & IPEXP_NEW) {
1388 type = IPCTNL_MSG_EXP_NEW;
1389 flags = NLM_F_CREATE|NLM_F_EXCL;
1393 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1396 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1402 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1403 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1404 nfmsg = NLMSG_DATA(nlh);
1406 nlh->nlmsg_flags = flags;
1407 nfmsg->nfgen_family = exp->tuple.src.l3num;
1408 nfmsg->version = NFNETLINK_V0;
1411 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1412 goto nla_put_failure;
1414 nlh->nlmsg_len = skb->tail - b;
1415 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1424 static int ctnetlink_exp_done(struct netlink_callback *cb)
1427 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1432 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1434 struct nf_conntrack_expect *exp, *last;
1435 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1436 struct hlist_node *n;
1437 u_int8_t l3proto = nfmsg->nfgen_family;
1439 read_lock_bh(&nf_conntrack_lock);
1440 last = (struct nf_conntrack_expect *)cb->args[1];
1441 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1443 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1445 if (l3proto && exp->tuple.src.l3num != l3proto)
1452 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1456 atomic_inc(&exp->use);
1457 cb->args[1] = (unsigned long)exp;
1467 read_unlock_bh(&nf_conntrack_lock);
1469 nf_ct_expect_put(last);
1474 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1475 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1476 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1480 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1481 struct nlmsghdr *nlh, struct nlattr *cda[])
1483 struct nf_conntrack_tuple tuple;
1484 struct nf_conntrack_expect *exp;
1485 struct sk_buff *skb2;
1486 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1487 u_int8_t u3 = nfmsg->nfgen_family;
1490 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1491 return netlink_dump_start(ctnl, skb, nlh,
1492 ctnetlink_exp_dump_table,
1493 ctnetlink_exp_done);
1496 if (cda[CTA_EXPECT_MASTER])
1497 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1504 exp = nf_ct_expect_find_get(&tuple);
1508 if (cda[CTA_EXPECT_ID]) {
1509 __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]);
1510 if (ntohl(id) != (u32)(unsigned long)exp) {
1511 nf_ct_expect_put(exp);
1517 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1521 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1522 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1527 nf_ct_expect_put(exp);
1529 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1534 nf_ct_expect_put(exp);
1539 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1540 struct nlmsghdr *nlh, struct nlattr *cda[])
1542 struct nf_conntrack_expect *exp;
1543 struct nf_conntrack_tuple tuple;
1544 struct nf_conntrack_helper *h;
1545 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1546 struct hlist_node *n, *next;
1547 u_int8_t u3 = nfmsg->nfgen_family;
1551 if (cda[CTA_EXPECT_TUPLE]) {
1552 /* delete a single expect by tuple */
1553 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1557 /* bump usage count to 2 */
1558 exp = nf_ct_expect_find_get(&tuple);
1562 if (cda[CTA_EXPECT_ID]) {
1563 __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]);
1564 if (ntohl(id) != (u32)(unsigned long)exp) {
1565 nf_ct_expect_put(exp);
1570 /* after list removal, usage count == 1 */
1571 nf_ct_unexpect_related(exp);
1572 /* have to put what we 'get' above.
1573 * after this line usage count == 0 */
1574 nf_ct_expect_put(exp);
1575 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1576 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1577 struct nf_conn_help *m_help;
1579 /* delete all expectations for this helper */
1580 write_lock_bh(&nf_conntrack_lock);
1581 h = __nf_conntrack_helper_find_byname(name);
1583 write_unlock_bh(&nf_conntrack_lock);
1586 for (i = 0; i < nf_ct_expect_hsize; i++) {
1587 hlist_for_each_entry_safe(exp, n, next,
1588 &nf_ct_expect_hash[i],
1590 m_help = nfct_help(exp->master);
1591 if (m_help->helper == h
1592 && del_timer(&exp->timeout)) {
1593 nf_ct_unlink_expect(exp);
1594 nf_ct_expect_put(exp);
1598 write_unlock_bh(&nf_conntrack_lock);
1600 /* This basically means we have to flush everything*/
1601 write_lock_bh(&nf_conntrack_lock);
1602 for (i = 0; i < nf_ct_expect_hsize; i++) {
1603 hlist_for_each_entry_safe(exp, n, next,
1604 &nf_ct_expect_hash[i],
1606 if (del_timer(&exp->timeout)) {
1607 nf_ct_unlink_expect(exp);
1608 nf_ct_expect_put(exp);
1612 write_unlock_bh(&nf_conntrack_lock);
1618 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1624 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1626 struct nf_conntrack_tuple tuple, mask, master_tuple;
1627 struct nf_conntrack_tuple_hash *h = NULL;
1628 struct nf_conntrack_expect *exp;
1630 struct nf_conn_help *help;
1633 /* caller guarantees that those three CTA_EXPECT_* exist */
1634 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1637 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1640 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1644 /* Look for master conntrack of this expectation */
1645 h = nf_conntrack_find_get(&master_tuple);
1648 ct = nf_ct_tuplehash_to_ctrack(h);
1649 help = nfct_help(ct);
1651 if (!help || !help->helper) {
1652 /* such conntrack hasn't got any helper, abort */
1657 exp = nf_ct_expect_alloc(ct);
1663 exp->expectfn = NULL;
1667 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1668 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1669 exp->mask.src.u.all = mask.src.u.all;
1671 err = nf_ct_expect_related(exp);
1672 nf_ct_expect_put(exp);
1675 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1680 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1681 struct nlmsghdr *nlh, struct nlattr *cda[])
1683 struct nf_conntrack_tuple tuple;
1684 struct nf_conntrack_expect *exp;
1685 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1686 u_int8_t u3 = nfmsg->nfgen_family;
1689 if (!cda[CTA_EXPECT_TUPLE]
1690 || !cda[CTA_EXPECT_MASK]
1691 || !cda[CTA_EXPECT_MASTER])
1694 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1698 write_lock_bh(&nf_conntrack_lock);
1699 exp = __nf_ct_expect_find(&tuple);
1702 write_unlock_bh(&nf_conntrack_lock);
1704 if (nlh->nlmsg_flags & NLM_F_CREATE)
1705 err = ctnetlink_create_expect(cda, u3);
1710 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1711 err = ctnetlink_change_expect(exp, cda);
1712 write_unlock_bh(&nf_conntrack_lock);
1717 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1718 static struct notifier_block ctnl_notifier = {
1719 .notifier_call = ctnetlink_conntrack_event,
1722 static struct notifier_block ctnl_notifier_exp = {
1723 .notifier_call = ctnetlink_expect_event,
1727 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1728 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1729 .attr_count = CTA_MAX,
1730 .policy = ct_nla_policy },
1731 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1732 .attr_count = CTA_MAX,
1733 .policy = ct_nla_policy },
1734 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1735 .attr_count = CTA_MAX,
1736 .policy = ct_nla_policy },
1737 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1738 .attr_count = CTA_MAX,
1739 .policy = ct_nla_policy },
1742 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1743 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1744 .attr_count = CTA_EXPECT_MAX,
1745 .policy = exp_nla_policy },
1746 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1747 .attr_count = CTA_EXPECT_MAX,
1748 .policy = exp_nla_policy },
1749 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1750 .attr_count = CTA_EXPECT_MAX,
1751 .policy = exp_nla_policy },
1754 static const struct nfnetlink_subsystem ctnl_subsys = {
1755 .name = "conntrack",
1756 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1757 .cb_count = IPCTNL_MSG_MAX,
1761 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1762 .name = "conntrack_expect",
1763 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1764 .cb_count = IPCTNL_MSG_EXP_MAX,
1768 MODULE_ALIAS("ip_conntrack_netlink");
1769 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1770 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1772 static int __init ctnetlink_init(void)
1776 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1777 ret = nfnetlink_subsys_register(&ctnl_subsys);
1779 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1783 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1785 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1786 goto err_unreg_subsys;
1789 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1790 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1792 printk("ctnetlink_init: cannot register notifier.\n");
1793 goto err_unreg_exp_subsys;
1796 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1798 printk("ctnetlink_init: cannot expect register notifier.\n");
1799 goto err_unreg_notifier;
1805 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1807 nf_conntrack_unregister_notifier(&ctnl_notifier);
1808 err_unreg_exp_subsys:
1809 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1812 nfnetlink_subsys_unregister(&ctnl_subsys);
1817 static void __exit ctnetlink_exit(void)
1819 printk("ctnetlink: unregistering from nfnetlink.\n");
1821 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1822 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1823 nf_conntrack_unregister_notifier(&ctnl_notifier);
1826 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1827 nfnetlink_subsys_unregister(&ctnl_subsys);
1831 module_init(ctnetlink_init);
1832 module_exit(ctnetlink_exit);