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_CONNTRACK_SECMARK
259 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
261 __be32 mark = htonl(ct->secmark);
263 NLA_PUT(skb, CTA_SECMARK, sizeof(u_int32_t), &mark);
270 #define ctnetlink_dump_secmark(a, b) (0)
273 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
276 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
278 struct nlattr *nest_parms;
280 if (!(ct->status & IPS_EXPECTED))
283 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
285 goto nla_put_failure;
286 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
287 goto nla_put_failure;
288 nla_nest_end(skb, nest_parms);
296 #ifdef CONFIG_NF_NAT_NEEDED
298 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
301 struct nlattr *nest_parms;
303 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
305 goto nla_put_failure;
307 tmp = htonl(natseq->correction_pos);
308 NLA_PUT(skb, CTA_NAT_SEQ_CORRECTION_POS, sizeof(tmp), &tmp);
309 tmp = htonl(natseq->offset_before);
310 NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_BEFORE, sizeof(tmp), &tmp);
311 tmp = htonl(natseq->offset_after);
312 NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_AFTER, sizeof(tmp), &tmp);
314 nla_nest_end(skb, nest_parms);
323 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
325 struct nf_nat_seq *natseq;
326 struct nf_conn_nat *nat = nfct_nat(ct);
328 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
331 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
332 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
335 natseq = &nat->seq[IP_CT_DIR_REPLY];
336 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
342 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
346 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
348 __be32 id = htonl((unsigned long)ct);
349 NLA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
357 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
359 __be32 use = htonl(atomic_read(&ct->ct_general.use));
361 NLA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
368 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
371 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
372 int event, int nowait,
373 const struct nf_conn *ct)
375 struct nlmsghdr *nlh;
376 struct nfgenmsg *nfmsg;
377 struct nlattr *nest_parms;
378 unsigned char *b = skb_tail_pointer(skb);
380 event |= NFNL_SUBSYS_CTNETLINK << 8;
381 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
382 nfmsg = NLMSG_DATA(nlh);
384 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
385 nfmsg->nfgen_family =
386 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
387 nfmsg->version = NFNETLINK_V0;
390 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
392 goto nla_put_failure;
393 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
394 goto nla_put_failure;
395 nla_nest_end(skb, nest_parms);
397 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
399 goto nla_put_failure;
400 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
401 goto nla_put_failure;
402 nla_nest_end(skb, nest_parms);
404 if (ctnetlink_dump_status(skb, ct) < 0 ||
405 ctnetlink_dump_timeout(skb, ct) < 0 ||
406 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
407 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
408 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
409 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
410 ctnetlink_dump_mark(skb, ct) < 0 ||
411 ctnetlink_dump_secmark(skb, ct) < 0 ||
412 ctnetlink_dump_id(skb, ct) < 0 ||
413 ctnetlink_dump_use(skb, ct) < 0 ||
414 ctnetlink_dump_master(skb, ct) < 0 ||
415 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
416 goto nla_put_failure;
418 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
427 #ifdef CONFIG_NF_CONNTRACK_EVENTS
428 static int ctnetlink_conntrack_event(struct notifier_block *this,
429 unsigned long events, void *ptr)
431 struct nlmsghdr *nlh;
432 struct nfgenmsg *nfmsg;
433 struct nlattr *nest_parms;
434 struct nf_conn *ct = (struct nf_conn *)ptr;
438 unsigned int flags = 0, group;
440 /* ignore our fake conntrack entry */
441 if (ct == &nf_conntrack_untracked)
444 if (events & IPCT_DESTROY) {
445 type = IPCTNL_MSG_CT_DELETE;
446 group = NFNLGRP_CONNTRACK_DESTROY;
447 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
448 type = IPCTNL_MSG_CT_NEW;
449 flags = NLM_F_CREATE|NLM_F_EXCL;
450 group = NFNLGRP_CONNTRACK_NEW;
451 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
452 type = IPCTNL_MSG_CT_NEW;
453 group = NFNLGRP_CONNTRACK_UPDATE;
457 if (!nfnetlink_has_listeners(group))
460 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
466 type |= NFNL_SUBSYS_CTNETLINK << 8;
467 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
468 nfmsg = NLMSG_DATA(nlh);
470 nlh->nlmsg_flags = flags;
471 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
472 nfmsg->version = NFNETLINK_V0;
475 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
477 goto nla_put_failure;
478 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
479 goto nla_put_failure;
480 nla_nest_end(skb, nest_parms);
482 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
484 goto nla_put_failure;
485 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
486 goto nla_put_failure;
487 nla_nest_end(skb, nest_parms);
489 if (events & IPCT_DESTROY) {
490 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
491 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
492 goto nla_put_failure;
494 if (ctnetlink_dump_status(skb, ct) < 0)
495 goto nla_put_failure;
497 if (ctnetlink_dump_timeout(skb, ct) < 0)
498 goto nla_put_failure;
500 if (events & IPCT_PROTOINFO
501 && ctnetlink_dump_protoinfo(skb, ct) < 0)
502 goto nla_put_failure;
504 if ((events & IPCT_HELPER || nfct_help(ct))
505 && ctnetlink_dump_helpinfo(skb, ct) < 0)
506 goto nla_put_failure;
508 #ifdef CONFIG_NF_CONNTRACK_MARK
509 if ((events & IPCT_MARK || ct->mark)
510 && ctnetlink_dump_mark(skb, ct) < 0)
511 goto nla_put_failure;
513 #ifdef CONFIG_NF_CONNTRACK_SECMARK
514 if ((events & IPCT_SECMARK || ct->secmark)
515 && ctnetlink_dump_secmark(skb, ct) < 0)
516 goto nla_put_failure;
519 if (events & IPCT_COUNTER_FILLING &&
520 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
521 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
522 goto nla_put_failure;
524 if (events & IPCT_RELATED &&
525 ctnetlink_dump_master(skb, ct) < 0)
526 goto nla_put_failure;
528 if (events & IPCT_NATSEQADJ &&
529 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
530 goto nla_put_failure;
533 nlh->nlmsg_len = skb->tail - b;
534 nfnetlink_send(skb, 0, group, 0);
542 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
544 static int ctnetlink_done(struct netlink_callback *cb)
547 nf_ct_put((struct nf_conn *)cb->args[1]);
551 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
554 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
556 struct nf_conn *ct, *last;
557 struct nf_conntrack_tuple_hash *h;
558 struct hlist_node *n;
559 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
560 u_int8_t l3proto = nfmsg->nfgen_family;
562 read_lock_bh(&nf_conntrack_lock);
563 last = (struct nf_conn *)cb->args[1];
564 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
566 hlist_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
568 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
570 ct = nf_ct_tuplehash_to_ctrack(h);
571 /* Dump entries of a given L3 protocol number.
572 * If it is not specified, ie. l3proto == 0,
573 * then dump everything. */
574 if (l3proto && L3PROTO(ct) != l3proto)
581 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
585 nf_conntrack_get(&ct->ct_general);
586 cb->args[1] = (unsigned long)ct;
589 #ifdef CONFIG_NF_CT_ACCT
590 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
591 IPCTNL_MSG_CT_GET_CTRZERO)
592 memset(&ct->counters, 0, sizeof(ct->counters));
601 read_unlock_bh(&nf_conntrack_lock);
609 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
611 struct nlattr *tb[CTA_IP_MAX+1];
612 struct nf_conntrack_l3proto *l3proto;
615 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
617 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
619 if (likely(l3proto->nlattr_to_tuple)) {
620 ret = nla_validate_nested(attr, CTA_IP_MAX,
621 l3proto->nla_policy);
623 ret = l3proto->nlattr_to_tuple(tb, tuple);
626 nf_ct_l3proto_put(l3proto);
631 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
632 [CTA_PROTO_NUM] = { .type = NLA_U8 },
636 ctnetlink_parse_tuple_proto(struct nlattr *attr,
637 struct nf_conntrack_tuple *tuple)
639 struct nlattr *tb[CTA_PROTO_MAX+1];
640 struct nf_conntrack_l4proto *l4proto;
643 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
647 if (!tb[CTA_PROTO_NUM])
649 tuple->dst.protonum = *(u_int8_t *)nla_data(tb[CTA_PROTO_NUM]);
651 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
653 if (likely(l4proto->nlattr_to_tuple)) {
654 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
655 l4proto->nla_policy);
657 ret = l4proto->nlattr_to_tuple(tb, tuple);
660 nf_ct_l4proto_put(l4proto);
666 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
667 enum ctattr_tuple type, u_int8_t l3num)
669 struct nlattr *tb[CTA_TUPLE_MAX+1];
672 memset(tuple, 0, sizeof(*tuple));
674 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
676 if (!tb[CTA_TUPLE_IP])
679 tuple->src.l3num = l3num;
681 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
685 if (!tb[CTA_TUPLE_PROTO])
688 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
692 /* orig and expect tuples get DIR_ORIGINAL */
693 if (type == CTA_TUPLE_REPLY)
694 tuple->dst.dir = IP_CT_DIR_REPLY;
696 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
701 #ifdef CONFIG_NF_NAT_NEEDED
702 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
703 [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
704 [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
707 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
708 const struct nf_conn *ct,
709 struct nf_nat_range *range)
711 struct nlattr *tb[CTA_PROTONAT_MAX+1];
712 struct nf_nat_protocol *npt;
715 err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
719 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
721 if (!npt->nlattr_to_range) {
722 nf_nat_proto_put(npt);
726 /* nlattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
727 if (npt->nlattr_to_range(tb, range) > 0)
728 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
730 nf_nat_proto_put(npt);
735 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
736 [CTA_NAT_MINIP] = { .type = NLA_U32 },
737 [CTA_NAT_MAXIP] = { .type = NLA_U32 },
741 nfnetlink_parse_nat(struct nlattr *nat,
742 const struct nf_conn *ct, struct nf_nat_range *range)
744 struct nlattr *tb[CTA_NAT_MAX+1];
747 memset(range, 0, sizeof(*range));
749 err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
753 if (tb[CTA_NAT_MINIP])
754 range->min_ip = *(__be32 *)nla_data(tb[CTA_NAT_MINIP]);
756 if (!tb[CTA_NAT_MAXIP])
757 range->max_ip = range->min_ip;
759 range->max_ip = *(__be32 *)nla_data(tb[CTA_NAT_MAXIP]);
762 range->flags |= IP_NAT_RANGE_MAP_IPS;
764 if (!tb[CTA_NAT_PROTO])
767 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
776 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
778 struct nlattr *tb[CTA_HELP_MAX+1];
780 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
782 if (!tb[CTA_HELP_NAME])
785 *helper_name = nla_data(tb[CTA_HELP_NAME]);
790 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
791 [CTA_STATUS] = { .type = NLA_U32 },
792 [CTA_TIMEOUT] = { .type = NLA_U32 },
793 [CTA_MARK] = { .type = NLA_U32 },
794 [CTA_USE] = { .type = NLA_U32 },
795 [CTA_ID] = { .type = NLA_U32 },
799 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
800 struct nlmsghdr *nlh, struct nlattr *cda[])
802 struct nf_conntrack_tuple_hash *h;
803 struct nf_conntrack_tuple tuple;
805 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
806 u_int8_t u3 = nfmsg->nfgen_family;
809 if (cda[CTA_TUPLE_ORIG])
810 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
811 else if (cda[CTA_TUPLE_REPLY])
812 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
814 /* Flush the whole table */
815 nf_conntrack_flush();
822 h = nf_conntrack_find_get(&tuple);
826 ct = nf_ct_tuplehash_to_ctrack(h);
829 u_int32_t id = ntohl(*(__be32 *)nla_data(cda[CTA_ID]));
830 if (id != (u32)(unsigned long)ct) {
835 if (del_timer(&ct->timeout))
836 ct->timeout.function((unsigned long)ct);
844 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
845 struct nlmsghdr *nlh, struct nlattr *cda[])
847 struct nf_conntrack_tuple_hash *h;
848 struct nf_conntrack_tuple tuple;
850 struct sk_buff *skb2 = NULL;
851 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
852 u_int8_t u3 = nfmsg->nfgen_family;
855 if (nlh->nlmsg_flags & NLM_F_DUMP) {
856 #ifndef CONFIG_NF_CT_ACCT
857 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
860 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
864 if (cda[CTA_TUPLE_ORIG])
865 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
866 else if (cda[CTA_TUPLE_REPLY])
867 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
874 h = nf_conntrack_find_get(&tuple);
878 ct = nf_ct_tuplehash_to_ctrack(h);
881 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
887 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
888 IPCTNL_MSG_CT_NEW, 1, ct);
893 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
906 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
909 unsigned int status = ntohl(*(__be32 *)nla_data(cda[CTA_STATUS]));
910 d = ct->status ^ status;
912 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
916 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
917 /* SEEN_REPLY bit can only be set */
921 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
922 /* ASSURED bit can only be set */
925 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
926 #ifndef CONFIG_NF_NAT_NEEDED
929 struct nf_nat_range range;
931 if (cda[CTA_NAT_DST]) {
932 if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
935 if (nf_nat_initialized(ct,
936 HOOK2MANIP(NF_INET_PRE_ROUTING)))
938 nf_nat_setup_info(ct, &range, NF_INET_PRE_ROUTING);
940 if (cda[CTA_NAT_SRC]) {
941 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
944 if (nf_nat_initialized(ct,
945 HOOK2MANIP(NF_INET_POST_ROUTING)))
947 nf_nat_setup_info(ct, &range, NF_INET_POST_ROUTING);
952 /* Be careful here, modifying NAT bits can screw up things,
953 * so don't let users modify them directly if they don't pass
955 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
961 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
963 struct nf_conntrack_helper *helper;
964 struct nf_conn_help *help = nfct_help(ct);
968 /* don't change helper of sibling connections */
972 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
976 if (!strcmp(helpname, "")) {
977 if (help && help->helper) {
978 /* we had a helper before ... */
979 nf_ct_remove_expectations(ct);
980 rcu_assign_pointer(help->helper, NULL);
986 helper = __nf_conntrack_helper_find_byname(helpname);
991 if (help->helper == helper)
995 /* need to zero data of old helper */
996 memset(&help->help, 0, sizeof(help->help));
998 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1003 rcu_assign_pointer(help->helper, helper);
1009 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
1011 u_int32_t timeout = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT]));
1013 if (!del_timer(&ct->timeout))
1016 ct->timeout.expires = jiffies + timeout * HZ;
1017 add_timer(&ct->timeout);
1023 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1025 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1026 struct nf_conntrack_l4proto *l4proto;
1027 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
1028 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
1031 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1033 l4proto = nf_ct_l4proto_find_get(l3num, npt);
1035 if (l4proto->from_nlattr)
1036 err = l4proto->from_nlattr(tb, ct);
1037 nf_ct_l4proto_put(l4proto);
1042 #ifdef CONFIG_NF_NAT_NEEDED
1044 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1046 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1048 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1050 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1053 natseq->correction_pos =
1054 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1056 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1059 natseq->offset_before =
1060 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1062 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1065 natseq->offset_after =
1066 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1072 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1075 struct nf_conn_nat *nat = nfct_nat(ct);
1080 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1081 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1082 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1086 ct->status |= IPS_SEQ_ADJUST;
1089 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1090 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1091 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1095 ct->status |= IPS_SEQ_ADJUST;
1103 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1107 if (cda[CTA_HELP]) {
1108 err = ctnetlink_change_helper(ct, cda);
1113 if (cda[CTA_TIMEOUT]) {
1114 err = ctnetlink_change_timeout(ct, cda);
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 #ifdef CONFIG_NF_NAT_NEEDED
1137 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1138 err = ctnetlink_change_nat_seq_adj(ct, cda);
1148 ctnetlink_create_conntrack(struct nlattr *cda[],
1149 struct nf_conntrack_tuple *otuple,
1150 struct nf_conntrack_tuple *rtuple,
1151 struct nf_conn *master_ct)
1155 struct nf_conn_help *help;
1156 struct nf_conntrack_helper *helper;
1158 ct = nf_conntrack_alloc(otuple, rtuple);
1159 if (ct == NULL || IS_ERR(ct))
1162 if (!cda[CTA_TIMEOUT])
1164 ct->timeout.expires = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT]));
1166 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1167 ct->status |= IPS_CONFIRMED;
1169 if (cda[CTA_STATUS]) {
1170 err = ctnetlink_change_status(ct, cda);
1175 if (cda[CTA_PROTOINFO]) {
1176 err = ctnetlink_change_protoinfo(ct, cda);
1181 #if defined(CONFIG_NF_CONNTRACK_MARK)
1183 ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK]));
1186 helper = nf_ct_helper_find_get(rtuple);
1188 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1190 nf_ct_helper_put(helper);
1194 /* not in hash table yet so not strictly necessary */
1195 rcu_assign_pointer(help->helper, helper);
1198 /* setup master conntrack: this is a confirmed expectation */
1200 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1201 ct->master = master_ct;
1204 add_timer(&ct->timeout);
1205 nf_conntrack_hash_insert(ct);
1208 nf_ct_helper_put(helper);
1213 nf_conntrack_free(ct);
1218 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1219 struct nlmsghdr *nlh, struct nlattr *cda[])
1221 struct nf_conntrack_tuple otuple, rtuple;
1222 struct nf_conntrack_tuple_hash *h = NULL;
1223 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1224 u_int8_t u3 = nfmsg->nfgen_family;
1227 if (cda[CTA_TUPLE_ORIG]) {
1228 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1233 if (cda[CTA_TUPLE_REPLY]) {
1234 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1239 write_lock_bh(&nf_conntrack_lock);
1240 if (cda[CTA_TUPLE_ORIG])
1241 h = __nf_conntrack_find(&otuple, NULL);
1242 else if (cda[CTA_TUPLE_REPLY])
1243 h = __nf_conntrack_find(&rtuple, NULL);
1246 struct nf_conntrack_tuple master;
1247 struct nf_conntrack_tuple_hash *master_h = NULL;
1248 struct nf_conn *master_ct = NULL;
1250 if (cda[CTA_TUPLE_MASTER]) {
1251 err = ctnetlink_parse_tuple(cda,
1258 master_h = __nf_conntrack_find(&master, NULL);
1259 if (master_h == NULL) {
1263 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1264 atomic_inc(&master_ct->ct_general.use);
1267 write_unlock_bh(&nf_conntrack_lock);
1269 if (nlh->nlmsg_flags & NLM_F_CREATE)
1270 err = ctnetlink_create_conntrack(cda,
1274 if (err < 0 && master_ct)
1275 nf_ct_put(master_ct);
1279 /* implicit 'else' */
1281 /* We manipulate the conntrack inside the global conntrack table lock,
1282 * so there's no need to increase the refcount */
1284 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1285 /* we only allow nat config for new conntracks */
1286 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1290 /* can't link an existing conntrack to a master */
1291 if (cda[CTA_TUPLE_MASTER]) {
1295 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1300 write_unlock_bh(&nf_conntrack_lock);
1304 /***********************************************************************
1306 ***********************************************************************/
1309 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1310 const struct nf_conntrack_tuple *tuple,
1311 enum ctattr_expect type)
1313 struct nlattr *nest_parms;
1315 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1317 goto nla_put_failure;
1318 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1319 goto nla_put_failure;
1320 nla_nest_end(skb, nest_parms);
1329 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1330 const struct nf_conntrack_tuple *tuple,
1331 const struct nf_conntrack_tuple_mask *mask)
1334 struct nf_conntrack_l3proto *l3proto;
1335 struct nf_conntrack_l4proto *l4proto;
1336 struct nf_conntrack_tuple m;
1337 struct nlattr *nest_parms;
1339 memset(&m, 0xFF, sizeof(m));
1340 m.src.u.all = mask->src.u.all;
1341 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1343 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1345 goto nla_put_failure;
1347 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1348 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1349 nf_ct_l3proto_put(l3proto);
1351 if (unlikely(ret < 0))
1352 goto nla_put_failure;
1354 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1355 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1356 nf_ct_l4proto_put(l4proto);
1357 if (unlikely(ret < 0))
1358 goto nla_put_failure;
1360 nla_nest_end(skb, nest_parms);
1369 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1370 const struct nf_conntrack_expect *exp)
1372 struct nf_conn *master = exp->master;
1373 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1374 __be32 id = htonl((unsigned long)exp);
1376 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1377 goto nla_put_failure;
1378 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1379 goto nla_put_failure;
1380 if (ctnetlink_exp_dump_tuple(skb,
1381 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1382 CTA_EXPECT_MASTER) < 0)
1383 goto nla_put_failure;
1385 NLA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1386 NLA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1395 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1398 const struct nf_conntrack_expect *exp)
1400 struct nlmsghdr *nlh;
1401 struct nfgenmsg *nfmsg;
1402 unsigned char *b = skb_tail_pointer(skb);
1404 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1405 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1406 nfmsg = NLMSG_DATA(nlh);
1408 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1409 nfmsg->nfgen_family = exp->tuple.src.l3num;
1410 nfmsg->version = NFNETLINK_V0;
1413 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1414 goto nla_put_failure;
1416 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1425 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1426 static int ctnetlink_expect_event(struct notifier_block *this,
1427 unsigned long events, void *ptr)
1429 struct nlmsghdr *nlh;
1430 struct nfgenmsg *nfmsg;
1431 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1432 struct sk_buff *skb;
1437 if (events & IPEXP_NEW) {
1438 type = IPCTNL_MSG_EXP_NEW;
1439 flags = NLM_F_CREATE|NLM_F_EXCL;
1443 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1446 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1452 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1453 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1454 nfmsg = NLMSG_DATA(nlh);
1456 nlh->nlmsg_flags = flags;
1457 nfmsg->nfgen_family = exp->tuple.src.l3num;
1458 nfmsg->version = NFNETLINK_V0;
1461 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1462 goto nla_put_failure;
1464 nlh->nlmsg_len = skb->tail - b;
1465 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1474 static int ctnetlink_exp_done(struct netlink_callback *cb)
1477 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1482 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1484 struct nf_conntrack_expect *exp, *last;
1485 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1486 struct hlist_node *n;
1487 u_int8_t l3proto = nfmsg->nfgen_family;
1489 read_lock_bh(&nf_conntrack_lock);
1490 last = (struct nf_conntrack_expect *)cb->args[1];
1491 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1493 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1495 if (l3proto && exp->tuple.src.l3num != l3proto)
1502 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1506 atomic_inc(&exp->use);
1507 cb->args[1] = (unsigned long)exp;
1517 read_unlock_bh(&nf_conntrack_lock);
1519 nf_ct_expect_put(last);
1524 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1525 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1526 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1530 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1531 struct nlmsghdr *nlh, struct nlattr *cda[])
1533 struct nf_conntrack_tuple tuple;
1534 struct nf_conntrack_expect *exp;
1535 struct sk_buff *skb2;
1536 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1537 u_int8_t u3 = nfmsg->nfgen_family;
1540 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1541 return netlink_dump_start(ctnl, skb, nlh,
1542 ctnetlink_exp_dump_table,
1543 ctnetlink_exp_done);
1546 if (cda[CTA_EXPECT_MASTER])
1547 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1554 exp = nf_ct_expect_find_get(&tuple);
1558 if (cda[CTA_EXPECT_ID]) {
1559 __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]);
1560 if (ntohl(id) != (u32)(unsigned long)exp) {
1561 nf_ct_expect_put(exp);
1567 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1571 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1572 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1577 nf_ct_expect_put(exp);
1579 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1584 nf_ct_expect_put(exp);
1589 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1590 struct nlmsghdr *nlh, struct nlattr *cda[])
1592 struct nf_conntrack_expect *exp;
1593 struct nf_conntrack_tuple tuple;
1594 struct nf_conntrack_helper *h;
1595 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1596 struct hlist_node *n, *next;
1597 u_int8_t u3 = nfmsg->nfgen_family;
1601 if (cda[CTA_EXPECT_TUPLE]) {
1602 /* delete a single expect by tuple */
1603 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1607 /* bump usage count to 2 */
1608 exp = nf_ct_expect_find_get(&tuple);
1612 if (cda[CTA_EXPECT_ID]) {
1613 __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]);
1614 if (ntohl(id) != (u32)(unsigned long)exp) {
1615 nf_ct_expect_put(exp);
1620 /* after list removal, usage count == 1 */
1621 nf_ct_unexpect_related(exp);
1622 /* have to put what we 'get' above.
1623 * after this line usage count == 0 */
1624 nf_ct_expect_put(exp);
1625 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1626 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1627 struct nf_conn_help *m_help;
1629 /* delete all expectations for this helper */
1630 write_lock_bh(&nf_conntrack_lock);
1631 h = __nf_conntrack_helper_find_byname(name);
1633 write_unlock_bh(&nf_conntrack_lock);
1636 for (i = 0; i < nf_ct_expect_hsize; i++) {
1637 hlist_for_each_entry_safe(exp, n, next,
1638 &nf_ct_expect_hash[i],
1640 m_help = nfct_help(exp->master);
1641 if (m_help->helper == h
1642 && del_timer(&exp->timeout)) {
1643 nf_ct_unlink_expect(exp);
1644 nf_ct_expect_put(exp);
1648 write_unlock_bh(&nf_conntrack_lock);
1650 /* This basically means we have to flush everything*/
1651 write_lock_bh(&nf_conntrack_lock);
1652 for (i = 0; i < nf_ct_expect_hsize; i++) {
1653 hlist_for_each_entry_safe(exp, n, next,
1654 &nf_ct_expect_hash[i],
1656 if (del_timer(&exp->timeout)) {
1657 nf_ct_unlink_expect(exp);
1658 nf_ct_expect_put(exp);
1662 write_unlock_bh(&nf_conntrack_lock);
1668 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1674 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1676 struct nf_conntrack_tuple tuple, mask, master_tuple;
1677 struct nf_conntrack_tuple_hash *h = NULL;
1678 struct nf_conntrack_expect *exp;
1680 struct nf_conn_help *help;
1683 /* caller guarantees that those three CTA_EXPECT_* exist */
1684 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1687 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1690 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1694 /* Look for master conntrack of this expectation */
1695 h = nf_conntrack_find_get(&master_tuple);
1698 ct = nf_ct_tuplehash_to_ctrack(h);
1699 help = nfct_help(ct);
1701 if (!help || !help->helper) {
1702 /* such conntrack hasn't got any helper, abort */
1707 exp = nf_ct_expect_alloc(ct);
1713 exp->expectfn = NULL;
1717 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1718 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1719 exp->mask.src.u.all = mask.src.u.all;
1721 err = nf_ct_expect_related(exp);
1722 nf_ct_expect_put(exp);
1725 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1730 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1731 struct nlmsghdr *nlh, struct nlattr *cda[])
1733 struct nf_conntrack_tuple tuple;
1734 struct nf_conntrack_expect *exp;
1735 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1736 u_int8_t u3 = nfmsg->nfgen_family;
1739 if (!cda[CTA_EXPECT_TUPLE]
1740 || !cda[CTA_EXPECT_MASK]
1741 || !cda[CTA_EXPECT_MASTER])
1744 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1748 write_lock_bh(&nf_conntrack_lock);
1749 exp = __nf_ct_expect_find(&tuple);
1752 write_unlock_bh(&nf_conntrack_lock);
1754 if (nlh->nlmsg_flags & NLM_F_CREATE)
1755 err = ctnetlink_create_expect(cda, u3);
1760 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1761 err = ctnetlink_change_expect(exp, cda);
1762 write_unlock_bh(&nf_conntrack_lock);
1767 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1768 static struct notifier_block ctnl_notifier = {
1769 .notifier_call = ctnetlink_conntrack_event,
1772 static struct notifier_block ctnl_notifier_exp = {
1773 .notifier_call = ctnetlink_expect_event,
1777 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1778 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1779 .attr_count = CTA_MAX,
1780 .policy = ct_nla_policy },
1781 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1782 .attr_count = CTA_MAX,
1783 .policy = ct_nla_policy },
1784 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1785 .attr_count = CTA_MAX,
1786 .policy = ct_nla_policy },
1787 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1788 .attr_count = CTA_MAX,
1789 .policy = ct_nla_policy },
1792 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1793 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1794 .attr_count = CTA_EXPECT_MAX,
1795 .policy = exp_nla_policy },
1796 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1797 .attr_count = CTA_EXPECT_MAX,
1798 .policy = exp_nla_policy },
1799 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1800 .attr_count = CTA_EXPECT_MAX,
1801 .policy = exp_nla_policy },
1804 static const struct nfnetlink_subsystem ctnl_subsys = {
1805 .name = "conntrack",
1806 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1807 .cb_count = IPCTNL_MSG_MAX,
1811 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1812 .name = "conntrack_expect",
1813 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1814 .cb_count = IPCTNL_MSG_EXP_MAX,
1818 MODULE_ALIAS("ip_conntrack_netlink");
1819 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1820 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1822 static int __init ctnetlink_init(void)
1826 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1827 ret = nfnetlink_subsys_register(&ctnl_subsys);
1829 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1833 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1835 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1836 goto err_unreg_subsys;
1839 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1840 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1842 printk("ctnetlink_init: cannot register notifier.\n");
1843 goto err_unreg_exp_subsys;
1846 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1848 printk("ctnetlink_init: cannot expect register notifier.\n");
1849 goto err_unreg_notifier;
1855 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1857 nf_conntrack_unregister_notifier(&ctnl_notifier);
1858 err_unreg_exp_subsys:
1859 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1862 nfnetlink_subsys_unregister(&ctnl_subsys);
1867 static void __exit ctnetlink_exit(void)
1869 printk("ctnetlink: unregistering from nfnetlink.\n");
1871 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1872 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1873 nf_conntrack_unregister_notifier(&ctnl_notifier);
1876 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1877 nfnetlink_subsys_unregister(&ctnl_subsys);
1881 module_init(ctnetlink_init);
1882 module_exit(ctnetlink_exit);