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_U8(skb, CTA_PROTO_NUM, 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 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, const struct nf_conn *ct)
148 struct nf_conntrack_l4proto *l4proto;
149 struct nlattr *nest_proto;
152 l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
153 if (!l4proto->to_nlattr) {
154 nf_ct_l4proto_put(l4proto);
158 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
160 goto nla_put_failure;
162 ret = l4proto->to_nlattr(skb, nest_proto, ct);
164 nf_ct_l4proto_put(l4proto);
166 nla_nest_end(skb, nest_proto);
171 nf_ct_l4proto_put(l4proto);
176 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
178 struct nlattr *nest_helper;
179 const struct nf_conn_help *help = nfct_help(ct);
180 struct nf_conntrack_helper *helper;
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);
208 #ifdef CONFIG_NF_CT_ACCT
210 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
211 enum ip_conntrack_dir dir)
213 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
214 struct nlattr *nest_count;
216 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
218 goto nla_put_failure;
220 NLA_PUT_BE32(skb, CTA_COUNTERS32_PACKETS,
221 htonl(ct->counters[dir].packets));
222 NLA_PUT_BE32(skb, CTA_COUNTERS32_BYTES,
223 htonl(ct->counters[dir].bytes));
225 nla_nest_end(skb, nest_count);
233 #define ctnetlink_dump_counters(a, b, c) (0)
236 #ifdef CONFIG_NF_CONNTRACK_MARK
238 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
240 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
247 #define ctnetlink_dump_mark(a, b) (0)
250 #ifdef CONFIG_NF_CONNTRACK_SECMARK
252 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
254 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
261 #define ctnetlink_dump_secmark(a, b) (0)
264 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
267 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
269 struct nlattr *nest_parms;
271 if (!(ct->status & IPS_EXPECTED))
274 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
276 goto nla_put_failure;
277 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
278 goto nla_put_failure;
279 nla_nest_end(skb, nest_parms);
287 #ifdef CONFIG_NF_NAT_NEEDED
289 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
291 struct nlattr *nest_parms;
293 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
295 goto nla_put_failure;
297 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
298 htonl(natseq->correction_pos));
299 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
300 htonl(natseq->offset_before));
301 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
302 htonl(natseq->offset_after));
304 nla_nest_end(skb, nest_parms);
313 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
315 struct nf_nat_seq *natseq;
316 struct nf_conn_nat *nat = nfct_nat(ct);
318 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
321 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
322 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
325 natseq = &nat->seq[IP_CT_DIR_REPLY];
326 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
332 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
336 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
338 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
346 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
348 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
355 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
358 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
359 int event, int nowait,
360 const struct nf_conn *ct)
362 struct nlmsghdr *nlh;
363 struct nfgenmsg *nfmsg;
364 struct nlattr *nest_parms;
365 unsigned char *b = skb_tail_pointer(skb);
367 event |= NFNL_SUBSYS_CTNETLINK << 8;
368 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
369 nfmsg = NLMSG_DATA(nlh);
371 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
372 nfmsg->nfgen_family = nf_ct_l3num(ct);
373 nfmsg->version = NFNETLINK_V0;
376 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
378 goto nla_put_failure;
379 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
380 goto nla_put_failure;
381 nla_nest_end(skb, nest_parms);
383 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
385 goto nla_put_failure;
386 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
387 goto nla_put_failure;
388 nla_nest_end(skb, nest_parms);
390 if (ctnetlink_dump_status(skb, ct) < 0 ||
391 ctnetlink_dump_timeout(skb, ct) < 0 ||
392 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
393 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
394 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
395 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
396 ctnetlink_dump_mark(skb, ct) < 0 ||
397 ctnetlink_dump_secmark(skb, ct) < 0 ||
398 ctnetlink_dump_id(skb, ct) < 0 ||
399 ctnetlink_dump_use(skb, ct) < 0 ||
400 ctnetlink_dump_master(skb, ct) < 0 ||
401 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
402 goto nla_put_failure;
404 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
413 #ifdef CONFIG_NF_CONNTRACK_EVENTS
414 static int ctnetlink_conntrack_event(struct notifier_block *this,
415 unsigned long events, void *ptr)
417 struct nlmsghdr *nlh;
418 struct nfgenmsg *nfmsg;
419 struct nlattr *nest_parms;
420 struct nf_conn *ct = (struct nf_conn *)ptr;
424 unsigned int flags = 0, group;
426 /* ignore our fake conntrack entry */
427 if (ct == &nf_conntrack_untracked)
430 if (events & IPCT_DESTROY) {
431 type = IPCTNL_MSG_CT_DELETE;
432 group = NFNLGRP_CONNTRACK_DESTROY;
433 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
434 type = IPCTNL_MSG_CT_NEW;
435 flags = NLM_F_CREATE|NLM_F_EXCL;
436 group = NFNLGRP_CONNTRACK_NEW;
437 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
438 type = IPCTNL_MSG_CT_NEW;
439 group = NFNLGRP_CONNTRACK_UPDATE;
443 if (!nfnetlink_has_listeners(group))
446 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
452 type |= NFNL_SUBSYS_CTNETLINK << 8;
453 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
454 nfmsg = NLMSG_DATA(nlh);
456 nlh->nlmsg_flags = flags;
457 nfmsg->nfgen_family = nf_ct_l3num(ct);
458 nfmsg->version = NFNETLINK_V0;
461 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
463 goto nla_put_failure;
464 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
465 goto nla_put_failure;
466 nla_nest_end(skb, nest_parms);
468 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
470 goto nla_put_failure;
471 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
472 goto nla_put_failure;
473 nla_nest_end(skb, nest_parms);
475 if (events & IPCT_DESTROY) {
476 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
477 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
478 goto nla_put_failure;
480 if (ctnetlink_dump_status(skb, ct) < 0)
481 goto nla_put_failure;
483 if (ctnetlink_dump_timeout(skb, ct) < 0)
484 goto nla_put_failure;
486 if (events & IPCT_PROTOINFO
487 && ctnetlink_dump_protoinfo(skb, ct) < 0)
488 goto nla_put_failure;
490 if ((events & IPCT_HELPER || nfct_help(ct))
491 && ctnetlink_dump_helpinfo(skb, ct) < 0)
492 goto nla_put_failure;
494 #ifdef CONFIG_NF_CONNTRACK_SECMARK
495 if ((events & IPCT_SECMARK || ct->secmark)
496 && ctnetlink_dump_secmark(skb, ct) < 0)
497 goto nla_put_failure;
500 if (events & IPCT_COUNTER_FILLING &&
501 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
502 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
503 goto nla_put_failure;
505 if (events & IPCT_RELATED &&
506 ctnetlink_dump_master(skb, ct) < 0)
507 goto nla_put_failure;
509 if (events & IPCT_NATSEQADJ &&
510 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
511 goto nla_put_failure;
514 #ifdef CONFIG_NF_CONNTRACK_MARK
515 if ((events & IPCT_MARK || ct->mark)
516 && ctnetlink_dump_mark(skb, ct) < 0)
517 goto nla_put_failure;
520 nlh->nlmsg_len = skb->tail - b;
521 nfnetlink_send(skb, 0, group, 0);
529 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
531 static int ctnetlink_done(struct netlink_callback *cb)
534 nf_ct_put((struct nf_conn *)cb->args[1]);
539 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
541 struct nf_conn *ct, *last;
542 struct nf_conntrack_tuple_hash *h;
543 struct hlist_node *n;
544 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
545 u_int8_t l3proto = nfmsg->nfgen_family;
548 last = (struct nf_conn *)cb->args[1];
549 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
551 hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[cb->args[0]],
553 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
555 ct = nf_ct_tuplehash_to_ctrack(h);
556 /* Dump entries of a given L3 protocol number.
557 * If it is not specified, ie. l3proto == 0,
558 * then dump everything. */
559 if (l3proto && nf_ct_l3num(ct) != l3proto)
566 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
570 if (!atomic_inc_not_zero(&ct->ct_general.use))
572 cb->args[1] = (unsigned long)ct;
575 #ifdef CONFIG_NF_CT_ACCT
576 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
577 IPCTNL_MSG_CT_GET_CTRZERO)
578 memset(&ct->counters, 0, sizeof(ct->counters));
595 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
597 struct nlattr *tb[CTA_IP_MAX+1];
598 struct nf_conntrack_l3proto *l3proto;
601 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
603 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
605 if (likely(l3proto->nlattr_to_tuple)) {
606 ret = nla_validate_nested(attr, CTA_IP_MAX,
607 l3proto->nla_policy);
609 ret = l3proto->nlattr_to_tuple(tb, tuple);
612 nf_ct_l3proto_put(l3proto);
617 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
618 [CTA_PROTO_NUM] = { .type = NLA_U8 },
622 ctnetlink_parse_tuple_proto(struct nlattr *attr,
623 struct nf_conntrack_tuple *tuple)
625 struct nlattr *tb[CTA_PROTO_MAX+1];
626 struct nf_conntrack_l4proto *l4proto;
629 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
633 if (!tb[CTA_PROTO_NUM])
635 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
637 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
639 if (likely(l4proto->nlattr_to_tuple)) {
640 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
641 l4proto->nla_policy);
643 ret = l4proto->nlattr_to_tuple(tb, tuple);
646 nf_ct_l4proto_put(l4proto);
652 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
653 enum ctattr_tuple type, u_int8_t l3num)
655 struct nlattr *tb[CTA_TUPLE_MAX+1];
658 memset(tuple, 0, sizeof(*tuple));
660 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
662 if (!tb[CTA_TUPLE_IP])
665 tuple->src.l3num = l3num;
667 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
671 if (!tb[CTA_TUPLE_PROTO])
674 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
678 /* orig and expect tuples get DIR_ORIGINAL */
679 if (type == CTA_TUPLE_REPLY)
680 tuple->dst.dir = IP_CT_DIR_REPLY;
682 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
687 #ifdef CONFIG_NF_NAT_NEEDED
688 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
689 [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
690 [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
693 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
694 const struct nf_conn *ct,
695 struct nf_nat_range *range)
697 struct nlattr *tb[CTA_PROTONAT_MAX+1];
698 const struct nf_nat_protocol *npt;
701 err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
705 npt = nf_nat_proto_find_get(nf_ct_protonum(ct));
706 if (npt->nlattr_to_range)
707 err = npt->nlattr_to_range(tb, range);
708 nf_nat_proto_put(npt);
712 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
713 [CTA_NAT_MINIP] = { .type = NLA_U32 },
714 [CTA_NAT_MAXIP] = { .type = NLA_U32 },
718 nfnetlink_parse_nat(struct nlattr *nat,
719 const struct nf_conn *ct, struct nf_nat_range *range)
721 struct nlattr *tb[CTA_NAT_MAX+1];
724 memset(range, 0, sizeof(*range));
726 err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
730 if (tb[CTA_NAT_MINIP])
731 range->min_ip = nla_get_be32(tb[CTA_NAT_MINIP]);
733 if (!tb[CTA_NAT_MAXIP])
734 range->max_ip = range->min_ip;
736 range->max_ip = nla_get_be32(tb[CTA_NAT_MAXIP]);
739 range->flags |= IP_NAT_RANGE_MAP_IPS;
741 if (!tb[CTA_NAT_PROTO])
744 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
753 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
755 struct nlattr *tb[CTA_HELP_MAX+1];
757 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
759 if (!tb[CTA_HELP_NAME])
762 *helper_name = nla_data(tb[CTA_HELP_NAME]);
767 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
768 [CTA_STATUS] = { .type = NLA_U32 },
769 [CTA_TIMEOUT] = { .type = NLA_U32 },
770 [CTA_MARK] = { .type = NLA_U32 },
771 [CTA_USE] = { .type = NLA_U32 },
772 [CTA_ID] = { .type = NLA_U32 },
776 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
777 struct nlmsghdr *nlh, struct nlattr *cda[])
779 struct nf_conntrack_tuple_hash *h;
780 struct nf_conntrack_tuple tuple;
782 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
783 u_int8_t u3 = nfmsg->nfgen_family;
786 if (cda[CTA_TUPLE_ORIG])
787 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
788 else if (cda[CTA_TUPLE_REPLY])
789 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
791 /* Flush the whole table */
792 nf_conntrack_flush();
799 h = nf_conntrack_find_get(&tuple);
803 ct = nf_ct_tuplehash_to_ctrack(h);
806 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
807 if (id != (u32)(unsigned long)ct) {
812 if (del_timer(&ct->timeout))
813 ct->timeout.function((unsigned long)ct);
821 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
822 struct nlmsghdr *nlh, struct nlattr *cda[])
824 struct nf_conntrack_tuple_hash *h;
825 struct nf_conntrack_tuple tuple;
827 struct sk_buff *skb2 = NULL;
828 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
829 u_int8_t u3 = nfmsg->nfgen_family;
832 if (nlh->nlmsg_flags & NLM_F_DUMP) {
833 #ifndef CONFIG_NF_CT_ACCT
834 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
837 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
841 if (cda[CTA_TUPLE_ORIG])
842 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
843 else if (cda[CTA_TUPLE_REPLY])
844 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
851 h = nf_conntrack_find_get(&tuple);
855 ct = nf_ct_tuplehash_to_ctrack(h);
858 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
864 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
865 IPCTNL_MSG_CT_NEW, 1, ct);
870 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
883 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
886 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
887 d = ct->status ^ status;
889 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
893 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
894 /* SEEN_REPLY bit can only be set */
898 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
899 /* ASSURED bit can only be set */
902 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
903 #ifndef CONFIG_NF_NAT_NEEDED
906 struct nf_nat_range range;
908 if (cda[CTA_NAT_DST]) {
909 if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
912 if (nf_nat_initialized(ct, IP_NAT_MANIP_DST))
914 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
916 if (cda[CTA_NAT_SRC]) {
917 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
920 if (nf_nat_initialized(ct, IP_NAT_MANIP_SRC))
922 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
927 /* Be careful here, modifying NAT bits can screw up things,
928 * so don't let users modify them directly if they don't pass
930 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
936 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
938 struct nf_conntrack_helper *helper;
939 struct nf_conn_help *help = nfct_help(ct);
943 /* don't change helper of sibling connections */
947 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
951 if (!strcmp(helpname, "")) {
952 if (help && help->helper) {
953 /* we had a helper before ... */
954 nf_ct_remove_expectations(ct);
955 rcu_assign_pointer(help->helper, NULL);
961 helper = __nf_conntrack_helper_find_byname(helpname);
966 if (help->helper == helper)
970 /* need to zero data of old helper */
971 memset(&help->help, 0, sizeof(help->help));
973 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
978 rcu_assign_pointer(help->helper, helper);
984 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
986 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
988 if (!del_timer(&ct->timeout))
991 ct->timeout.expires = jiffies + timeout * HZ;
992 add_timer(&ct->timeout);
998 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1000 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1001 struct nf_conntrack_l4proto *l4proto;
1004 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1006 l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
1007 if (l4proto->from_nlattr)
1008 err = l4proto->from_nlattr(tb, ct);
1009 nf_ct_l4proto_put(l4proto);
1014 #ifdef CONFIG_NF_NAT_NEEDED
1016 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1018 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1020 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1022 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1025 natseq->correction_pos =
1026 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1028 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1031 natseq->offset_before =
1032 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1034 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1037 natseq->offset_after =
1038 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1044 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1047 struct nf_conn_nat *nat = nfct_nat(ct);
1052 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1053 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1054 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1058 ct->status |= IPS_SEQ_ADJUST;
1061 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1062 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1063 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1067 ct->status |= IPS_SEQ_ADJUST;
1075 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1079 if (cda[CTA_HELP]) {
1080 err = ctnetlink_change_helper(ct, cda);
1085 if (cda[CTA_TIMEOUT]) {
1086 err = ctnetlink_change_timeout(ct, cda);
1091 if (cda[CTA_STATUS]) {
1092 err = ctnetlink_change_status(ct, cda);
1097 if (cda[CTA_PROTOINFO]) {
1098 err = ctnetlink_change_protoinfo(ct, cda);
1103 #if defined(CONFIG_NF_CONNTRACK_MARK)
1105 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1108 #ifdef CONFIG_NF_NAT_NEEDED
1109 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1110 err = ctnetlink_change_nat_seq_adj(ct, cda);
1120 ctnetlink_create_conntrack(struct nlattr *cda[],
1121 struct nf_conntrack_tuple *otuple,
1122 struct nf_conntrack_tuple *rtuple,
1123 struct nf_conn *master_ct)
1127 struct nf_conn_help *help;
1128 struct nf_conntrack_helper *helper;
1130 ct = nf_conntrack_alloc(otuple, rtuple);
1131 if (ct == NULL || IS_ERR(ct))
1134 if (!cda[CTA_TIMEOUT])
1136 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1138 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1139 ct->status |= IPS_CONFIRMED;
1141 if (cda[CTA_STATUS]) {
1142 err = ctnetlink_change_status(ct, cda);
1147 if (cda[CTA_PROTOINFO]) {
1148 err = ctnetlink_change_protoinfo(ct, cda);
1153 #if defined(CONFIG_NF_CONNTRACK_MARK)
1155 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1159 helper = __nf_ct_helper_find(rtuple);
1161 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1167 /* not in hash table yet so not strictly necessary */
1168 rcu_assign_pointer(help->helper, helper);
1171 /* setup master conntrack: this is a confirmed expectation */
1173 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1174 ct->master = master_ct;
1177 add_timer(&ct->timeout);
1178 nf_conntrack_hash_insert(ct);
1184 nf_conntrack_free(ct);
1189 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1190 struct nlmsghdr *nlh, struct nlattr *cda[])
1192 struct nf_conntrack_tuple otuple, rtuple;
1193 struct nf_conntrack_tuple_hash *h = NULL;
1194 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1195 u_int8_t u3 = nfmsg->nfgen_family;
1198 if (cda[CTA_TUPLE_ORIG]) {
1199 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1204 if (cda[CTA_TUPLE_REPLY]) {
1205 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1210 spin_lock_bh(&nf_conntrack_lock);
1211 if (cda[CTA_TUPLE_ORIG])
1212 h = __nf_conntrack_find(&otuple);
1213 else if (cda[CTA_TUPLE_REPLY])
1214 h = __nf_conntrack_find(&rtuple);
1217 struct nf_conntrack_tuple master;
1218 struct nf_conntrack_tuple_hash *master_h = NULL;
1219 struct nf_conn *master_ct = NULL;
1221 if (cda[CTA_TUPLE_MASTER]) {
1222 err = ctnetlink_parse_tuple(cda,
1229 master_h = __nf_conntrack_find(&master);
1230 if (master_h == NULL) {
1234 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1235 atomic_inc(&master_ct->ct_general.use);
1238 spin_unlock_bh(&nf_conntrack_lock);
1240 if (nlh->nlmsg_flags & NLM_F_CREATE)
1241 err = ctnetlink_create_conntrack(cda,
1245 if (err < 0 && master_ct)
1246 nf_ct_put(master_ct);
1250 /* implicit 'else' */
1252 /* We manipulate the conntrack inside the global conntrack table lock,
1253 * so there's no need to increase the refcount */
1255 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1256 /* we only allow nat config for new conntracks */
1257 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1261 /* can't link an existing conntrack to a master */
1262 if (cda[CTA_TUPLE_MASTER]) {
1266 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1271 spin_unlock_bh(&nf_conntrack_lock);
1275 /***********************************************************************
1277 ***********************************************************************/
1280 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1281 const struct nf_conntrack_tuple *tuple,
1282 enum ctattr_expect type)
1284 struct nlattr *nest_parms;
1286 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1288 goto nla_put_failure;
1289 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1290 goto nla_put_failure;
1291 nla_nest_end(skb, nest_parms);
1300 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1301 const struct nf_conntrack_tuple *tuple,
1302 const struct nf_conntrack_tuple_mask *mask)
1305 struct nf_conntrack_l3proto *l3proto;
1306 struct nf_conntrack_l4proto *l4proto;
1307 struct nf_conntrack_tuple m;
1308 struct nlattr *nest_parms;
1310 memset(&m, 0xFF, sizeof(m));
1311 m.src.u.all = mask->src.u.all;
1312 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1314 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1316 goto nla_put_failure;
1318 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1319 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1320 nf_ct_l3proto_put(l3proto);
1322 if (unlikely(ret < 0))
1323 goto nla_put_failure;
1325 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1326 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1327 nf_ct_l4proto_put(l4proto);
1328 if (unlikely(ret < 0))
1329 goto nla_put_failure;
1331 nla_nest_end(skb, nest_parms);
1340 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1341 const struct nf_conntrack_expect *exp)
1343 struct nf_conn *master = exp->master;
1344 long timeout = (exp->timeout.expires - jiffies) / HZ;
1349 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1350 goto nla_put_failure;
1351 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1352 goto nla_put_failure;
1353 if (ctnetlink_exp_dump_tuple(skb,
1354 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1355 CTA_EXPECT_MASTER) < 0)
1356 goto nla_put_failure;
1358 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1359 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1368 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1371 const struct nf_conntrack_expect *exp)
1373 struct nlmsghdr *nlh;
1374 struct nfgenmsg *nfmsg;
1375 unsigned char *b = skb_tail_pointer(skb);
1377 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1378 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1379 nfmsg = NLMSG_DATA(nlh);
1381 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1382 nfmsg->nfgen_family = exp->tuple.src.l3num;
1383 nfmsg->version = NFNETLINK_V0;
1386 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1387 goto nla_put_failure;
1389 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1398 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1399 static int ctnetlink_expect_event(struct notifier_block *this,
1400 unsigned long events, void *ptr)
1402 struct nlmsghdr *nlh;
1403 struct nfgenmsg *nfmsg;
1404 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1405 struct sk_buff *skb;
1410 if (events & IPEXP_NEW) {
1411 type = IPCTNL_MSG_EXP_NEW;
1412 flags = NLM_F_CREATE|NLM_F_EXCL;
1416 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1419 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1425 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1426 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1427 nfmsg = NLMSG_DATA(nlh);
1429 nlh->nlmsg_flags = flags;
1430 nfmsg->nfgen_family = exp->tuple.src.l3num;
1431 nfmsg->version = NFNETLINK_V0;
1434 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1435 goto nla_put_failure;
1437 nlh->nlmsg_len = skb->tail - b;
1438 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1447 static int ctnetlink_exp_done(struct netlink_callback *cb)
1450 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1455 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1457 struct nf_conntrack_expect *exp, *last;
1458 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1459 struct hlist_node *n;
1460 u_int8_t l3proto = nfmsg->nfgen_family;
1463 last = (struct nf_conntrack_expect *)cb->args[1];
1464 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1466 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1468 if (l3proto && exp->tuple.src.l3num != l3proto)
1475 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1479 if (!atomic_inc_not_zero(&exp->use))
1481 cb->args[1] = (unsigned long)exp;
1493 nf_ct_expect_put(last);
1498 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1499 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1500 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1504 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1505 struct nlmsghdr *nlh, struct nlattr *cda[])
1507 struct nf_conntrack_tuple tuple;
1508 struct nf_conntrack_expect *exp;
1509 struct sk_buff *skb2;
1510 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1511 u_int8_t u3 = nfmsg->nfgen_family;
1514 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1515 return netlink_dump_start(ctnl, skb, nlh,
1516 ctnetlink_exp_dump_table,
1517 ctnetlink_exp_done);
1520 if (cda[CTA_EXPECT_MASTER])
1521 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1528 exp = nf_ct_expect_find_get(&tuple);
1532 if (cda[CTA_EXPECT_ID]) {
1533 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1534 if (ntohl(id) != (u32)(unsigned long)exp) {
1535 nf_ct_expect_put(exp);
1541 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1545 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1546 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1551 nf_ct_expect_put(exp);
1553 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1558 nf_ct_expect_put(exp);
1563 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1564 struct nlmsghdr *nlh, struct nlattr *cda[])
1566 struct nf_conntrack_expect *exp;
1567 struct nf_conntrack_tuple tuple;
1568 struct nf_conntrack_helper *h;
1569 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1570 struct hlist_node *n, *next;
1571 u_int8_t u3 = nfmsg->nfgen_family;
1575 if (cda[CTA_EXPECT_TUPLE]) {
1576 /* delete a single expect by tuple */
1577 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1581 /* bump usage count to 2 */
1582 exp = nf_ct_expect_find_get(&tuple);
1586 if (cda[CTA_EXPECT_ID]) {
1587 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1588 if (ntohl(id) != (u32)(unsigned long)exp) {
1589 nf_ct_expect_put(exp);
1594 /* after list removal, usage count == 1 */
1595 nf_ct_unexpect_related(exp);
1596 /* have to put what we 'get' above.
1597 * after this line usage count == 0 */
1598 nf_ct_expect_put(exp);
1599 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1600 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1601 struct nf_conn_help *m_help;
1603 /* delete all expectations for this helper */
1604 spin_lock_bh(&nf_conntrack_lock);
1605 h = __nf_conntrack_helper_find_byname(name);
1607 spin_unlock_bh(&nf_conntrack_lock);
1610 for (i = 0; i < nf_ct_expect_hsize; i++) {
1611 hlist_for_each_entry_safe(exp, n, next,
1612 &nf_ct_expect_hash[i],
1614 m_help = nfct_help(exp->master);
1615 if (m_help->helper == h
1616 && del_timer(&exp->timeout)) {
1617 nf_ct_unlink_expect(exp);
1618 nf_ct_expect_put(exp);
1622 spin_unlock_bh(&nf_conntrack_lock);
1624 /* This basically means we have to flush everything*/
1625 spin_lock_bh(&nf_conntrack_lock);
1626 for (i = 0; i < nf_ct_expect_hsize; i++) {
1627 hlist_for_each_entry_safe(exp, n, next,
1628 &nf_ct_expect_hash[i],
1630 if (del_timer(&exp->timeout)) {
1631 nf_ct_unlink_expect(exp);
1632 nf_ct_expect_put(exp);
1636 spin_unlock_bh(&nf_conntrack_lock);
1642 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1648 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1650 struct nf_conntrack_tuple tuple, mask, master_tuple;
1651 struct nf_conntrack_tuple_hash *h = NULL;
1652 struct nf_conntrack_expect *exp;
1654 struct nf_conn_help *help;
1657 /* caller guarantees that those three CTA_EXPECT_* exist */
1658 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1661 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1664 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1668 /* Look for master conntrack of this expectation */
1669 h = nf_conntrack_find_get(&master_tuple);
1672 ct = nf_ct_tuplehash_to_ctrack(h);
1673 help = nfct_help(ct);
1675 if (!help || !help->helper) {
1676 /* such conntrack hasn't got any helper, abort */
1681 exp = nf_ct_expect_alloc(ct);
1687 exp->expectfn = NULL;
1691 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1692 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1693 exp->mask.src.u.all = mask.src.u.all;
1695 err = nf_ct_expect_related(exp);
1696 nf_ct_expect_put(exp);
1699 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1704 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1705 struct nlmsghdr *nlh, struct nlattr *cda[])
1707 struct nf_conntrack_tuple tuple;
1708 struct nf_conntrack_expect *exp;
1709 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1710 u_int8_t u3 = nfmsg->nfgen_family;
1713 if (!cda[CTA_EXPECT_TUPLE]
1714 || !cda[CTA_EXPECT_MASK]
1715 || !cda[CTA_EXPECT_MASTER])
1718 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1722 spin_lock_bh(&nf_conntrack_lock);
1723 exp = __nf_ct_expect_find(&tuple);
1726 spin_unlock_bh(&nf_conntrack_lock);
1728 if (nlh->nlmsg_flags & NLM_F_CREATE)
1729 err = ctnetlink_create_expect(cda, u3);
1734 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1735 err = ctnetlink_change_expect(exp, cda);
1736 spin_unlock_bh(&nf_conntrack_lock);
1741 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1742 static struct notifier_block ctnl_notifier = {
1743 .notifier_call = ctnetlink_conntrack_event,
1746 static struct notifier_block ctnl_notifier_exp = {
1747 .notifier_call = ctnetlink_expect_event,
1751 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1752 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1753 .attr_count = CTA_MAX,
1754 .policy = ct_nla_policy },
1755 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1756 .attr_count = CTA_MAX,
1757 .policy = ct_nla_policy },
1758 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1759 .attr_count = CTA_MAX,
1760 .policy = ct_nla_policy },
1761 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1762 .attr_count = CTA_MAX,
1763 .policy = ct_nla_policy },
1766 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1767 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1768 .attr_count = CTA_EXPECT_MAX,
1769 .policy = exp_nla_policy },
1770 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1771 .attr_count = CTA_EXPECT_MAX,
1772 .policy = exp_nla_policy },
1773 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1774 .attr_count = CTA_EXPECT_MAX,
1775 .policy = exp_nla_policy },
1778 static const struct nfnetlink_subsystem ctnl_subsys = {
1779 .name = "conntrack",
1780 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1781 .cb_count = IPCTNL_MSG_MAX,
1785 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1786 .name = "conntrack_expect",
1787 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1788 .cb_count = IPCTNL_MSG_EXP_MAX,
1792 MODULE_ALIAS("ip_conntrack_netlink");
1793 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1794 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1796 static int __init ctnetlink_init(void)
1800 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1801 ret = nfnetlink_subsys_register(&ctnl_subsys);
1803 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1807 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1809 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1810 goto err_unreg_subsys;
1813 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1814 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1816 printk("ctnetlink_init: cannot register notifier.\n");
1817 goto err_unreg_exp_subsys;
1820 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1822 printk("ctnetlink_init: cannot expect register notifier.\n");
1823 goto err_unreg_notifier;
1829 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1831 nf_conntrack_unregister_notifier(&ctnl_notifier);
1832 err_unreg_exp_subsys:
1833 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1836 nfnetlink_subsys_unregister(&ctnl_subsys);
1841 static void __exit ctnetlink_exit(void)
1843 printk("ctnetlink: unregistering from nfnetlink.\n");
1845 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1846 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1847 nf_conntrack_unregister_notifier(&ctnl_notifier);
1850 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1851 nfnetlink_subsys_unregister(&ctnl_subsys);
1855 module_init(ctnetlink_init);
1856 module_exit(ctnetlink_exit);