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 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
260 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
262 struct nlattr *nest_parms;
264 if (!(ct->status & IPS_EXPECTED))
267 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
269 goto nla_put_failure;
270 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
271 goto nla_put_failure;
272 nla_nest_end(skb, nest_parms);
280 #ifdef CONFIG_NF_NAT_NEEDED
282 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
285 struct nlattr *nest_parms;
287 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
289 goto nla_put_failure;
291 tmp = htonl(natseq->correction_pos);
292 NLA_PUT(skb, CTA_NAT_SEQ_CORRECTION_POS, sizeof(tmp), &tmp);
293 tmp = htonl(natseq->offset_before);
294 NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_BEFORE, sizeof(tmp), &tmp);
295 tmp = htonl(natseq->offset_after);
296 NLA_PUT(skb, CTA_NAT_SEQ_OFFSET_AFTER, sizeof(tmp), &tmp);
298 nla_nest_end(skb, nest_parms);
307 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
309 struct nf_nat_seq *natseq;
310 struct nf_conn_nat *nat = nfct_nat(ct);
312 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
315 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
316 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
319 natseq = &nat->seq[IP_CT_DIR_REPLY];
320 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
326 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
330 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
332 __be32 id = htonl((unsigned long)ct);
333 NLA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
341 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
343 __be32 use = htonl(atomic_read(&ct->ct_general.use));
345 NLA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
352 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
355 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
356 int event, int nowait,
357 const struct nf_conn *ct)
359 struct nlmsghdr *nlh;
360 struct nfgenmsg *nfmsg;
361 struct nlattr *nest_parms;
362 unsigned char *b = skb_tail_pointer(skb);
364 event |= NFNL_SUBSYS_CTNETLINK << 8;
365 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
366 nfmsg = NLMSG_DATA(nlh);
368 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
369 nfmsg->nfgen_family =
370 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
371 nfmsg->version = NFNETLINK_V0;
374 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
376 goto nla_put_failure;
377 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
378 goto nla_put_failure;
379 nla_nest_end(skb, nest_parms);
381 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
383 goto nla_put_failure;
384 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
385 goto nla_put_failure;
386 nla_nest_end(skb, nest_parms);
388 if (ctnetlink_dump_status(skb, ct) < 0 ||
389 ctnetlink_dump_timeout(skb, ct) < 0 ||
390 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
391 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
392 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
393 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
394 ctnetlink_dump_mark(skb, ct) < 0 ||
395 ctnetlink_dump_id(skb, ct) < 0 ||
396 ctnetlink_dump_use(skb, ct) < 0 ||
397 ctnetlink_dump_master(skb, ct) < 0 ||
398 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
399 goto nla_put_failure;
401 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
410 #ifdef CONFIG_NF_CONNTRACK_EVENTS
411 static int ctnetlink_conntrack_event(struct notifier_block *this,
412 unsigned long events, void *ptr)
414 struct nlmsghdr *nlh;
415 struct nfgenmsg *nfmsg;
416 struct nlattr *nest_parms;
417 struct nf_conn *ct = (struct nf_conn *)ptr;
421 unsigned int flags = 0, group;
423 /* ignore our fake conntrack entry */
424 if (ct == &nf_conntrack_untracked)
427 if (events & IPCT_DESTROY) {
428 type = IPCTNL_MSG_CT_DELETE;
429 group = NFNLGRP_CONNTRACK_DESTROY;
430 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
431 type = IPCTNL_MSG_CT_NEW;
432 flags = NLM_F_CREATE|NLM_F_EXCL;
433 group = NFNLGRP_CONNTRACK_NEW;
434 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
435 type = IPCTNL_MSG_CT_NEW;
436 group = NFNLGRP_CONNTRACK_UPDATE;
440 if (!nfnetlink_has_listeners(group))
443 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
449 type |= NFNL_SUBSYS_CTNETLINK << 8;
450 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
451 nfmsg = NLMSG_DATA(nlh);
453 nlh->nlmsg_flags = flags;
454 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
455 nfmsg->version = NFNETLINK_V0;
458 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
460 goto nla_put_failure;
461 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
462 goto nla_put_failure;
463 nla_nest_end(skb, nest_parms);
465 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
467 goto nla_put_failure;
468 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
469 goto nla_put_failure;
470 nla_nest_end(skb, nest_parms);
472 if (events & IPCT_DESTROY) {
473 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
474 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
475 goto nla_put_failure;
477 if (ctnetlink_dump_status(skb, ct) < 0)
478 goto nla_put_failure;
480 if (ctnetlink_dump_timeout(skb, ct) < 0)
481 goto nla_put_failure;
483 if (events & IPCT_PROTOINFO
484 && ctnetlink_dump_protoinfo(skb, ct) < 0)
485 goto nla_put_failure;
487 if ((events & IPCT_HELPER || nfct_help(ct))
488 && ctnetlink_dump_helpinfo(skb, ct) < 0)
489 goto nla_put_failure;
491 #ifdef CONFIG_NF_CONNTRACK_MARK
492 if ((events & IPCT_MARK || ct->mark)
493 && ctnetlink_dump_mark(skb, ct) < 0)
494 goto nla_put_failure;
497 if (events & IPCT_COUNTER_FILLING &&
498 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
499 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
500 goto nla_put_failure;
502 if (events & IPCT_RELATED &&
503 ctnetlink_dump_master(skb, ct) < 0)
504 goto nla_put_failure;
506 if (events & IPCT_NATSEQADJ &&
507 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
508 goto nla_put_failure;
511 nlh->nlmsg_len = skb->tail - b;
512 nfnetlink_send(skb, 0, group, 0);
520 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
522 static int ctnetlink_done(struct netlink_callback *cb)
525 nf_ct_put((struct nf_conn *)cb->args[1]);
529 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
532 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
534 struct nf_conn *ct, *last;
535 struct nf_conntrack_tuple_hash *h;
536 struct hlist_node *n;
537 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
538 u_int8_t l3proto = nfmsg->nfgen_family;
540 read_lock_bh(&nf_conntrack_lock);
541 last = (struct nf_conn *)cb->args[1];
542 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
544 hlist_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
546 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
548 ct = nf_ct_tuplehash_to_ctrack(h);
549 /* Dump entries of a given L3 protocol number.
550 * If it is not specified, ie. l3proto == 0,
551 * then dump everything. */
552 if (l3proto && L3PROTO(ct) != l3proto)
559 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
563 nf_conntrack_get(&ct->ct_general);
564 cb->args[1] = (unsigned long)ct;
567 #ifdef CONFIG_NF_CT_ACCT
568 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
569 IPCTNL_MSG_CT_GET_CTRZERO)
570 memset(&ct->counters, 0, sizeof(ct->counters));
579 read_unlock_bh(&nf_conntrack_lock);
587 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
589 struct nlattr *tb[CTA_IP_MAX+1];
590 struct nf_conntrack_l3proto *l3proto;
593 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
595 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
597 if (likely(l3proto->nlattr_to_tuple)) {
598 ret = nla_validate_nested(attr, CTA_IP_MAX,
599 l3proto->nla_policy);
601 ret = l3proto->nlattr_to_tuple(tb, tuple);
604 nf_ct_l3proto_put(l3proto);
609 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
610 [CTA_PROTO_NUM] = { .type = NLA_U8 },
614 ctnetlink_parse_tuple_proto(struct nlattr *attr,
615 struct nf_conntrack_tuple *tuple)
617 struct nlattr *tb[CTA_PROTO_MAX+1];
618 struct nf_conntrack_l4proto *l4proto;
621 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
625 if (!tb[CTA_PROTO_NUM])
627 tuple->dst.protonum = *(u_int8_t *)nla_data(tb[CTA_PROTO_NUM]);
629 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
631 if (likely(l4proto->nlattr_to_tuple)) {
632 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
633 l4proto->nla_policy);
635 ret = l4proto->nlattr_to_tuple(tb, tuple);
638 nf_ct_l4proto_put(l4proto);
644 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
645 enum ctattr_tuple type, u_int8_t l3num)
647 struct nlattr *tb[CTA_TUPLE_MAX+1];
650 memset(tuple, 0, sizeof(*tuple));
652 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
654 if (!tb[CTA_TUPLE_IP])
657 tuple->src.l3num = l3num;
659 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
663 if (!tb[CTA_TUPLE_PROTO])
666 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
670 /* orig and expect tuples get DIR_ORIGINAL */
671 if (type == CTA_TUPLE_REPLY)
672 tuple->dst.dir = IP_CT_DIR_REPLY;
674 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
679 #ifdef CONFIG_NF_NAT_NEEDED
680 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
681 [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
682 [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
685 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
686 const struct nf_conn *ct,
687 struct nf_nat_range *range)
689 struct nlattr *tb[CTA_PROTONAT_MAX+1];
690 struct nf_nat_protocol *npt;
693 err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
697 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
699 if (!npt->nlattr_to_range) {
700 nf_nat_proto_put(npt);
704 /* nlattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
705 if (npt->nlattr_to_range(tb, range) > 0)
706 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
708 nf_nat_proto_put(npt);
713 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
714 [CTA_NAT_MINIP] = { .type = NLA_U32 },
715 [CTA_NAT_MAXIP] = { .type = NLA_U32 },
719 nfnetlink_parse_nat(struct nlattr *nat,
720 const struct nf_conn *ct, struct nf_nat_range *range)
722 struct nlattr *tb[CTA_NAT_MAX+1];
725 memset(range, 0, sizeof(*range));
727 err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
731 if (tb[CTA_NAT_MINIP])
732 range->min_ip = *(__be32 *)nla_data(tb[CTA_NAT_MINIP]);
734 if (!tb[CTA_NAT_MAXIP])
735 range->max_ip = range->min_ip;
737 range->max_ip = *(__be32 *)nla_data(tb[CTA_NAT_MAXIP]);
740 range->flags |= IP_NAT_RANGE_MAP_IPS;
742 if (!tb[CTA_NAT_PROTO])
745 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
754 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
756 struct nlattr *tb[CTA_HELP_MAX+1];
758 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
760 if (!tb[CTA_HELP_NAME])
763 *helper_name = nla_data(tb[CTA_HELP_NAME]);
768 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
769 [CTA_STATUS] = { .type = NLA_U32 },
770 [CTA_TIMEOUT] = { .type = NLA_U32 },
771 [CTA_MARK] = { .type = NLA_U32 },
772 [CTA_USE] = { .type = NLA_U32 },
773 [CTA_ID] = { .type = NLA_U32 },
777 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
778 struct nlmsghdr *nlh, struct nlattr *cda[])
780 struct nf_conntrack_tuple_hash *h;
781 struct nf_conntrack_tuple tuple;
783 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
784 u_int8_t u3 = nfmsg->nfgen_family;
787 if (cda[CTA_TUPLE_ORIG])
788 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
789 else if (cda[CTA_TUPLE_REPLY])
790 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
792 /* Flush the whole table */
793 nf_conntrack_flush();
800 h = nf_conntrack_find_get(&tuple);
804 ct = nf_ct_tuplehash_to_ctrack(h);
807 u_int32_t id = ntohl(*(__be32 *)nla_data(cda[CTA_ID]));
808 if (id != (u32)(unsigned long)ct) {
813 if (del_timer(&ct->timeout))
814 ct->timeout.function((unsigned long)ct);
822 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
823 struct nlmsghdr *nlh, struct nlattr *cda[])
825 struct nf_conntrack_tuple_hash *h;
826 struct nf_conntrack_tuple tuple;
828 struct sk_buff *skb2 = NULL;
829 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
830 u_int8_t u3 = nfmsg->nfgen_family;
833 if (nlh->nlmsg_flags & NLM_F_DUMP) {
834 #ifndef CONFIG_NF_CT_ACCT
835 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
838 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
842 if (cda[CTA_TUPLE_ORIG])
843 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
844 else if (cda[CTA_TUPLE_REPLY])
845 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
852 h = nf_conntrack_find_get(&tuple);
856 ct = nf_ct_tuplehash_to_ctrack(h);
859 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
865 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
866 IPCTNL_MSG_CT_NEW, 1, ct);
871 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
884 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
887 unsigned int status = ntohl(*(__be32 *)nla_data(cda[CTA_STATUS]));
888 d = ct->status ^ status;
890 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
894 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
895 /* SEEN_REPLY bit can only be set */
899 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
900 /* ASSURED bit can only be set */
903 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
904 #ifndef CONFIG_NF_NAT_NEEDED
907 struct nf_nat_range range;
909 if (cda[CTA_NAT_DST]) {
910 if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
913 if (nf_nat_initialized(ct,
914 HOOK2MANIP(NF_INET_PRE_ROUTING)))
916 nf_nat_setup_info(ct, &range, NF_INET_PRE_ROUTING);
918 if (cda[CTA_NAT_SRC]) {
919 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
922 if (nf_nat_initialized(ct,
923 HOOK2MANIP(NF_INET_POST_ROUTING)))
925 nf_nat_setup_info(ct, &range, NF_INET_POST_ROUTING);
930 /* Be careful here, modifying NAT bits can screw up things,
931 * so don't let users modify them directly if they don't pass
933 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
939 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
941 struct nf_conntrack_helper *helper;
942 struct nf_conn_help *help = nfct_help(ct);
946 /* don't change helper of sibling connections */
950 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
954 if (!strcmp(helpname, "")) {
955 if (help && help->helper) {
956 /* we had a helper before ... */
957 nf_ct_remove_expectations(ct);
958 rcu_assign_pointer(help->helper, NULL);
964 helper = __nf_conntrack_helper_find_byname(helpname);
969 if (help->helper == helper)
973 /* need to zero data of old helper */
974 memset(&help->help, 0, sizeof(help->help));
976 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
981 rcu_assign_pointer(help->helper, helper);
987 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
989 u_int32_t timeout = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT]));
991 if (!del_timer(&ct->timeout))
994 ct->timeout.expires = jiffies + timeout * HZ;
995 add_timer(&ct->timeout);
1001 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1003 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1004 struct nf_conntrack_l4proto *l4proto;
1005 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
1006 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
1009 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1011 l4proto = nf_ct_l4proto_find_get(l3num, npt);
1013 if (l4proto->from_nlattr)
1014 err = l4proto->from_nlattr(tb, ct);
1015 nf_ct_l4proto_put(l4proto);
1020 #ifdef CONFIG_NF_NAT_NEEDED
1022 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1024 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1026 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1028 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1031 natseq->correction_pos =
1032 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1034 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1037 natseq->offset_before =
1038 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1040 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1043 natseq->offset_after =
1044 ntohl(*(__be32 *)nla_data(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1050 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1053 struct nf_conn_nat *nat = nfct_nat(ct);
1058 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1059 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1060 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1064 ct->status |= IPS_SEQ_ADJUST;
1067 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1068 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1069 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1073 ct->status |= IPS_SEQ_ADJUST;
1081 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1085 if (cda[CTA_HELP]) {
1086 err = ctnetlink_change_helper(ct, cda);
1091 if (cda[CTA_TIMEOUT]) {
1092 err = ctnetlink_change_timeout(ct, cda);
1097 if (cda[CTA_STATUS]) {
1098 err = ctnetlink_change_status(ct, cda);
1103 if (cda[CTA_PROTOINFO]) {
1104 err = ctnetlink_change_protoinfo(ct, cda);
1109 #if defined(CONFIG_NF_CONNTRACK_MARK)
1111 ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK]));
1114 #ifdef CONFIG_NF_NAT_NEEDED
1115 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1116 err = ctnetlink_change_nat_seq_adj(ct, cda);
1126 ctnetlink_create_conntrack(struct nlattr *cda[],
1127 struct nf_conntrack_tuple *otuple,
1128 struct nf_conntrack_tuple *rtuple,
1129 struct nf_conn *master_ct)
1133 struct nf_conn_help *help;
1134 struct nf_conntrack_helper *helper;
1136 ct = nf_conntrack_alloc(otuple, rtuple);
1137 if (ct == NULL || IS_ERR(ct))
1140 if (!cda[CTA_TIMEOUT])
1142 ct->timeout.expires = ntohl(*(__be32 *)nla_data(cda[CTA_TIMEOUT]));
1144 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1145 ct->status |= IPS_CONFIRMED;
1147 if (cda[CTA_STATUS]) {
1148 err = ctnetlink_change_status(ct, cda);
1153 if (cda[CTA_PROTOINFO]) {
1154 err = ctnetlink_change_protoinfo(ct, cda);
1159 #if defined(CONFIG_NF_CONNTRACK_MARK)
1161 ct->mark = ntohl(*(__be32 *)nla_data(cda[CTA_MARK]));
1164 helper = nf_ct_helper_find_get(rtuple);
1166 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1168 nf_ct_helper_put(helper);
1172 /* not in hash table yet so not strictly necessary */
1173 rcu_assign_pointer(help->helper, helper);
1176 /* setup master conntrack: this is a confirmed expectation */
1178 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1179 ct->master = master_ct;
1182 add_timer(&ct->timeout);
1183 nf_conntrack_hash_insert(ct);
1186 nf_ct_helper_put(helper);
1191 nf_conntrack_free(ct);
1196 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1197 struct nlmsghdr *nlh, struct nlattr *cda[])
1199 struct nf_conntrack_tuple otuple, rtuple;
1200 struct nf_conntrack_tuple_hash *h = NULL;
1201 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1202 u_int8_t u3 = nfmsg->nfgen_family;
1205 if (cda[CTA_TUPLE_ORIG]) {
1206 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1211 if (cda[CTA_TUPLE_REPLY]) {
1212 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1217 write_lock_bh(&nf_conntrack_lock);
1218 if (cda[CTA_TUPLE_ORIG])
1219 h = __nf_conntrack_find(&otuple, NULL);
1220 else if (cda[CTA_TUPLE_REPLY])
1221 h = __nf_conntrack_find(&rtuple, NULL);
1224 struct nf_conntrack_tuple master;
1225 struct nf_conntrack_tuple_hash *master_h = NULL;
1226 struct nf_conn *master_ct = NULL;
1228 if (cda[CTA_TUPLE_MASTER]) {
1229 err = ctnetlink_parse_tuple(cda,
1236 master_h = __nf_conntrack_find(&master, NULL);
1237 if (master_h == NULL) {
1241 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1242 atomic_inc(&master_ct->ct_general.use);
1245 write_unlock_bh(&nf_conntrack_lock);
1247 if (nlh->nlmsg_flags & NLM_F_CREATE)
1248 err = ctnetlink_create_conntrack(cda,
1252 if (err < 0 && master_ct)
1253 nf_ct_put(master_ct);
1257 /* implicit 'else' */
1259 /* We manipulate the conntrack inside the global conntrack table lock,
1260 * so there's no need to increase the refcount */
1262 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1263 /* we only allow nat config for new conntracks */
1264 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1268 /* can't link an existing conntrack to a master */
1269 if (cda[CTA_TUPLE_MASTER]) {
1273 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1278 write_unlock_bh(&nf_conntrack_lock);
1282 /***********************************************************************
1284 ***********************************************************************/
1287 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1288 const struct nf_conntrack_tuple *tuple,
1289 enum ctattr_expect type)
1291 struct nlattr *nest_parms;
1293 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1295 goto nla_put_failure;
1296 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1297 goto nla_put_failure;
1298 nla_nest_end(skb, nest_parms);
1307 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1308 const struct nf_conntrack_tuple *tuple,
1309 const struct nf_conntrack_tuple_mask *mask)
1312 struct nf_conntrack_l3proto *l3proto;
1313 struct nf_conntrack_l4proto *l4proto;
1314 struct nf_conntrack_tuple m;
1315 struct nlattr *nest_parms;
1317 memset(&m, 0xFF, sizeof(m));
1318 m.src.u.all = mask->src.u.all;
1319 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1321 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1323 goto nla_put_failure;
1325 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1326 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1327 nf_ct_l3proto_put(l3proto);
1329 if (unlikely(ret < 0))
1330 goto nla_put_failure;
1332 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1333 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1334 nf_ct_l4proto_put(l4proto);
1335 if (unlikely(ret < 0))
1336 goto nla_put_failure;
1338 nla_nest_end(skb, nest_parms);
1347 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1348 const struct nf_conntrack_expect *exp)
1350 struct nf_conn *master = exp->master;
1351 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1352 __be32 id = htonl((unsigned long)exp);
1354 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1355 goto nla_put_failure;
1356 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1357 goto nla_put_failure;
1358 if (ctnetlink_exp_dump_tuple(skb,
1359 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1360 CTA_EXPECT_MASTER) < 0)
1361 goto nla_put_failure;
1363 NLA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1364 NLA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1373 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1376 const struct nf_conntrack_expect *exp)
1378 struct nlmsghdr *nlh;
1379 struct nfgenmsg *nfmsg;
1380 unsigned char *b = skb_tail_pointer(skb);
1382 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1383 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1384 nfmsg = NLMSG_DATA(nlh);
1386 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1387 nfmsg->nfgen_family = exp->tuple.src.l3num;
1388 nfmsg->version = NFNETLINK_V0;
1391 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1392 goto nla_put_failure;
1394 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1403 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1404 static int ctnetlink_expect_event(struct notifier_block *this,
1405 unsigned long events, void *ptr)
1407 struct nlmsghdr *nlh;
1408 struct nfgenmsg *nfmsg;
1409 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1410 struct sk_buff *skb;
1415 if (events & IPEXP_NEW) {
1416 type = IPCTNL_MSG_EXP_NEW;
1417 flags = NLM_F_CREATE|NLM_F_EXCL;
1421 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1424 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1430 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1431 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1432 nfmsg = NLMSG_DATA(nlh);
1434 nlh->nlmsg_flags = flags;
1435 nfmsg->nfgen_family = exp->tuple.src.l3num;
1436 nfmsg->version = NFNETLINK_V0;
1439 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1440 goto nla_put_failure;
1442 nlh->nlmsg_len = skb->tail - b;
1443 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1452 static int ctnetlink_exp_done(struct netlink_callback *cb)
1455 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1460 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1462 struct nf_conntrack_expect *exp, *last;
1463 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1464 struct hlist_node *n;
1465 u_int8_t l3proto = nfmsg->nfgen_family;
1467 read_lock_bh(&nf_conntrack_lock);
1468 last = (struct nf_conntrack_expect *)cb->args[1];
1469 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1471 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1473 if (l3proto && exp->tuple.src.l3num != l3proto)
1480 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1484 atomic_inc(&exp->use);
1485 cb->args[1] = (unsigned long)exp;
1495 read_unlock_bh(&nf_conntrack_lock);
1497 nf_ct_expect_put(last);
1502 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1503 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1504 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1508 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1509 struct nlmsghdr *nlh, struct nlattr *cda[])
1511 struct nf_conntrack_tuple tuple;
1512 struct nf_conntrack_expect *exp;
1513 struct sk_buff *skb2;
1514 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1515 u_int8_t u3 = nfmsg->nfgen_family;
1518 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1519 return netlink_dump_start(ctnl, skb, nlh,
1520 ctnetlink_exp_dump_table,
1521 ctnetlink_exp_done);
1524 if (cda[CTA_EXPECT_MASTER])
1525 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1532 exp = nf_ct_expect_find_get(&tuple);
1536 if (cda[CTA_EXPECT_ID]) {
1537 __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]);
1538 if (ntohl(id) != (u32)(unsigned long)exp) {
1539 nf_ct_expect_put(exp);
1545 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1549 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1550 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1555 nf_ct_expect_put(exp);
1557 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1562 nf_ct_expect_put(exp);
1567 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1568 struct nlmsghdr *nlh, struct nlattr *cda[])
1570 struct nf_conntrack_expect *exp;
1571 struct nf_conntrack_tuple tuple;
1572 struct nf_conntrack_helper *h;
1573 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1574 struct hlist_node *n, *next;
1575 u_int8_t u3 = nfmsg->nfgen_family;
1579 if (cda[CTA_EXPECT_TUPLE]) {
1580 /* delete a single expect by tuple */
1581 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1585 /* bump usage count to 2 */
1586 exp = nf_ct_expect_find_get(&tuple);
1590 if (cda[CTA_EXPECT_ID]) {
1591 __be32 id = *(__be32 *)nla_data(cda[CTA_EXPECT_ID]);
1592 if (ntohl(id) != (u32)(unsigned long)exp) {
1593 nf_ct_expect_put(exp);
1598 /* after list removal, usage count == 1 */
1599 nf_ct_unexpect_related(exp);
1600 /* have to put what we 'get' above.
1601 * after this line usage count == 0 */
1602 nf_ct_expect_put(exp);
1603 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1604 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1605 struct nf_conn_help *m_help;
1607 /* delete all expectations for this helper */
1608 write_lock_bh(&nf_conntrack_lock);
1609 h = __nf_conntrack_helper_find_byname(name);
1611 write_unlock_bh(&nf_conntrack_lock);
1614 for (i = 0; i < nf_ct_expect_hsize; i++) {
1615 hlist_for_each_entry_safe(exp, n, next,
1616 &nf_ct_expect_hash[i],
1618 m_help = nfct_help(exp->master);
1619 if (m_help->helper == h
1620 && del_timer(&exp->timeout)) {
1621 nf_ct_unlink_expect(exp);
1622 nf_ct_expect_put(exp);
1626 write_unlock_bh(&nf_conntrack_lock);
1628 /* This basically means we have to flush everything*/
1629 write_lock_bh(&nf_conntrack_lock);
1630 for (i = 0; i < nf_ct_expect_hsize; i++) {
1631 hlist_for_each_entry_safe(exp, n, next,
1632 &nf_ct_expect_hash[i],
1634 if (del_timer(&exp->timeout)) {
1635 nf_ct_unlink_expect(exp);
1636 nf_ct_expect_put(exp);
1640 write_unlock_bh(&nf_conntrack_lock);
1646 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1652 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1654 struct nf_conntrack_tuple tuple, mask, master_tuple;
1655 struct nf_conntrack_tuple_hash *h = NULL;
1656 struct nf_conntrack_expect *exp;
1658 struct nf_conn_help *help;
1661 /* caller guarantees that those three CTA_EXPECT_* exist */
1662 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1665 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1668 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1672 /* Look for master conntrack of this expectation */
1673 h = nf_conntrack_find_get(&master_tuple);
1676 ct = nf_ct_tuplehash_to_ctrack(h);
1677 help = nfct_help(ct);
1679 if (!help || !help->helper) {
1680 /* such conntrack hasn't got any helper, abort */
1685 exp = nf_ct_expect_alloc(ct);
1691 exp->expectfn = NULL;
1695 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1696 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1697 exp->mask.src.u.all = mask.src.u.all;
1699 err = nf_ct_expect_related(exp);
1700 nf_ct_expect_put(exp);
1703 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1708 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1709 struct nlmsghdr *nlh, struct nlattr *cda[])
1711 struct nf_conntrack_tuple tuple;
1712 struct nf_conntrack_expect *exp;
1713 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1714 u_int8_t u3 = nfmsg->nfgen_family;
1717 if (!cda[CTA_EXPECT_TUPLE]
1718 || !cda[CTA_EXPECT_MASK]
1719 || !cda[CTA_EXPECT_MASTER])
1722 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1726 write_lock_bh(&nf_conntrack_lock);
1727 exp = __nf_ct_expect_find(&tuple);
1730 write_unlock_bh(&nf_conntrack_lock);
1732 if (nlh->nlmsg_flags & NLM_F_CREATE)
1733 err = ctnetlink_create_expect(cda, u3);
1738 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1739 err = ctnetlink_change_expect(exp, cda);
1740 write_unlock_bh(&nf_conntrack_lock);
1745 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1746 static struct notifier_block ctnl_notifier = {
1747 .notifier_call = ctnetlink_conntrack_event,
1750 static struct notifier_block ctnl_notifier_exp = {
1751 .notifier_call = ctnetlink_expect_event,
1755 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1756 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1757 .attr_count = CTA_MAX,
1758 .policy = ct_nla_policy },
1759 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1760 .attr_count = CTA_MAX,
1761 .policy = ct_nla_policy },
1762 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1763 .attr_count = CTA_MAX,
1764 .policy = ct_nla_policy },
1765 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1766 .attr_count = CTA_MAX,
1767 .policy = ct_nla_policy },
1770 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1771 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1772 .attr_count = CTA_EXPECT_MAX,
1773 .policy = exp_nla_policy },
1774 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1775 .attr_count = CTA_EXPECT_MAX,
1776 .policy = exp_nla_policy },
1777 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1778 .attr_count = CTA_EXPECT_MAX,
1779 .policy = exp_nla_policy },
1782 static const struct nfnetlink_subsystem ctnl_subsys = {
1783 .name = "conntrack",
1784 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1785 .cb_count = IPCTNL_MSG_MAX,
1789 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1790 .name = "conntrack_expect",
1791 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1792 .cb_count = IPCTNL_MSG_EXP_MAX,
1796 MODULE_ALIAS("ip_conntrack_netlink");
1797 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1798 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1800 static int __init ctnetlink_init(void)
1804 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1805 ret = nfnetlink_subsys_register(&ctnl_subsys);
1807 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1811 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1813 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1814 goto err_unreg_subsys;
1817 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1818 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1820 printk("ctnetlink_init: cannot register notifier.\n");
1821 goto err_unreg_exp_subsys;
1824 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1826 printk("ctnetlink_init: cannot expect register notifier.\n");
1827 goto err_unreg_notifier;
1833 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1835 nf_conntrack_unregister_notifier(&ctnl_notifier);
1836 err_unreg_exp_subsys:
1837 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1840 nfnetlink_subsys_unregister(&ctnl_subsys);
1845 static void __exit ctnetlink_exit(void)
1847 printk("ctnetlink: unregistering from nfnetlink.\n");
1849 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1850 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1851 nf_conntrack_unregister_notifier(&ctnl_notifier);
1854 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1855 nfnetlink_subsys_unregister(&ctnl_subsys);
1859 module_init(ctnetlink_init);
1860 module_exit(ctnetlink_exit);