1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/types.h>
23 #include <linux/timer.h>
24 #include <linux/skbuff.h>
25 #include <linux/errno.h>
26 #include <linux/netlink.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/notifier.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #ifdef CONFIG_NF_NAT_NEEDED
41 #include <net/netfilter/nf_nat_core.h>
42 #include <net/netfilter/nf_nat_protocol.h>
45 #include <linux/netfilter/nfnetlink.h>
46 #include <linux/netfilter/nfnetlink_conntrack.h>
48 MODULE_LICENSE("GPL");
50 static char __initdata version[] = "0.93";
53 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
54 const struct nf_conntrack_tuple *tuple,
55 struct nf_conntrack_l4proto *l4proto)
58 struct nlattr *nest_parms;
60 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
63 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
65 if (likely(l4proto->tuple_to_nlattr))
66 ret = l4proto->tuple_to_nlattr(skb, tuple);
68 nla_nest_end(skb, nest_parms);
77 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
78 const struct nf_conntrack_tuple *tuple,
79 struct nf_conntrack_l3proto *l3proto)
82 struct nlattr *nest_parms;
84 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
88 if (likely(l3proto->tuple_to_nlattr))
89 ret = l3proto->tuple_to_nlattr(skb, tuple);
91 nla_nest_end(skb, nest_parms);
100 ctnetlink_dump_tuples(struct sk_buff *skb,
101 const struct nf_conntrack_tuple *tuple)
104 struct nf_conntrack_l3proto *l3proto;
105 struct nf_conntrack_l4proto *l4proto;
107 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
108 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
109 nf_ct_l3proto_put(l3proto);
111 if (unlikely(ret < 0))
114 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
115 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
116 nf_ct_l4proto_put(l4proto);
122 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
124 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
132 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
134 long timeout = (ct->timeout.expires - jiffies) / HZ;
139 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
147 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
149 struct nf_conntrack_l4proto *l4proto;
150 struct nlattr *nest_proto;
153 l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
154 if (!l4proto->to_nlattr) {
155 nf_ct_l4proto_put(l4proto);
159 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
161 goto nla_put_failure;
163 ret = l4proto->to_nlattr(skb, nest_proto, ct);
165 nf_ct_l4proto_put(l4proto);
167 nla_nest_end(skb, nest_proto);
172 nf_ct_l4proto_put(l4proto);
177 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
179 struct nlattr *nest_helper;
180 const struct nf_conn_help *help = nfct_help(ct);
181 struct nf_conntrack_helper *helper;
187 helper = rcu_dereference(help->helper);
191 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
193 goto nla_put_failure;
194 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
196 if (helper->to_nlattr)
197 helper->to_nlattr(skb, ct);
199 nla_nest_end(skb, nest_helper);
209 #ifdef CONFIG_NF_CT_ACCT
211 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
212 enum ip_conntrack_dir dir)
214 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
215 struct nlattr *nest_count;
217 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
219 goto nla_put_failure;
221 NLA_PUT_BE32(skb, CTA_COUNTERS32_PACKETS,
222 htonl(ct->counters[dir].packets));
223 NLA_PUT_BE32(skb, CTA_COUNTERS32_BYTES,
224 htonl(ct->counters[dir].bytes));
226 nla_nest_end(skb, nest_count);
234 #define ctnetlink_dump_counters(a, b, c) (0)
237 #ifdef CONFIG_NF_CONNTRACK_MARK
239 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
241 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
248 #define ctnetlink_dump_mark(a, b) (0)
251 #ifdef CONFIG_NF_CONNTRACK_SECMARK
253 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
255 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
262 #define ctnetlink_dump_secmark(a, b) (0)
265 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
268 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
270 struct nlattr *nest_parms;
272 if (!(ct->status & IPS_EXPECTED))
275 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
277 goto nla_put_failure;
278 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
279 goto nla_put_failure;
280 nla_nest_end(skb, nest_parms);
288 #ifdef CONFIG_NF_NAT_NEEDED
290 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
292 struct nlattr *nest_parms;
294 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
296 goto nla_put_failure;
298 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
299 htonl(natseq->correction_pos));
300 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
301 htonl(natseq->offset_before));
302 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
303 htonl(natseq->offset_after));
305 nla_nest_end(skb, nest_parms);
314 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
316 struct nf_nat_seq *natseq;
317 struct nf_conn_nat *nat = nfct_nat(ct);
319 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
322 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
323 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
326 natseq = &nat->seq[IP_CT_DIR_REPLY];
327 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
333 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
337 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
339 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
347 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
349 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
356 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
359 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
360 int event, int nowait,
361 const struct nf_conn *ct)
363 struct nlmsghdr *nlh;
364 struct nfgenmsg *nfmsg;
365 struct nlattr *nest_parms;
366 unsigned char *b = skb_tail_pointer(skb);
368 event |= NFNL_SUBSYS_CTNETLINK << 8;
369 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
370 nfmsg = NLMSG_DATA(nlh);
372 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
373 nfmsg->nfgen_family = nf_ct_l3num(ct);
374 nfmsg->version = NFNETLINK_V0;
377 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
379 goto nla_put_failure;
380 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
381 goto nla_put_failure;
382 nla_nest_end(skb, nest_parms);
384 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
386 goto nla_put_failure;
387 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
388 goto nla_put_failure;
389 nla_nest_end(skb, nest_parms);
391 if (ctnetlink_dump_status(skb, ct) < 0 ||
392 ctnetlink_dump_timeout(skb, ct) < 0 ||
393 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
394 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
395 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
396 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
397 ctnetlink_dump_mark(skb, ct) < 0 ||
398 ctnetlink_dump_secmark(skb, ct) < 0 ||
399 ctnetlink_dump_id(skb, ct) < 0 ||
400 ctnetlink_dump_use(skb, ct) < 0 ||
401 ctnetlink_dump_master(skb, ct) < 0 ||
402 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
403 goto nla_put_failure;
405 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
414 #ifdef CONFIG_NF_CONNTRACK_EVENTS
415 static int ctnetlink_conntrack_event(struct notifier_block *this,
416 unsigned long events, void *ptr)
418 struct nlmsghdr *nlh;
419 struct nfgenmsg *nfmsg;
420 struct nlattr *nest_parms;
421 struct nf_conn *ct = (struct nf_conn *)ptr;
425 unsigned int flags = 0, group;
427 /* ignore our fake conntrack entry */
428 if (ct == &nf_conntrack_untracked)
431 if (events & IPCT_DESTROY) {
432 type = IPCTNL_MSG_CT_DELETE;
433 group = NFNLGRP_CONNTRACK_DESTROY;
434 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
435 type = IPCTNL_MSG_CT_NEW;
436 flags = NLM_F_CREATE|NLM_F_EXCL;
437 group = NFNLGRP_CONNTRACK_NEW;
438 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
439 type = IPCTNL_MSG_CT_NEW;
440 group = NFNLGRP_CONNTRACK_UPDATE;
444 if (!nfnetlink_has_listeners(group))
447 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
453 type |= NFNL_SUBSYS_CTNETLINK << 8;
454 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
455 nfmsg = NLMSG_DATA(nlh);
457 nlh->nlmsg_flags = flags;
458 nfmsg->nfgen_family = nf_ct_l3num(ct);
459 nfmsg->version = NFNETLINK_V0;
462 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
464 goto nla_put_failure;
465 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
466 goto nla_put_failure;
467 nla_nest_end(skb, nest_parms);
469 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
471 goto nla_put_failure;
472 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
473 goto nla_put_failure;
474 nla_nest_end(skb, nest_parms);
476 if (ctnetlink_dump_id(skb, ct) < 0)
477 goto nla_put_failure;
479 if (ctnetlink_dump_status(skb, ct) < 0)
480 goto nla_put_failure;
482 if (events & IPCT_DESTROY) {
483 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
484 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
485 goto nla_put_failure;
487 if (ctnetlink_dump_timeout(skb, ct) < 0)
488 goto nla_put_failure;
490 if (events & IPCT_PROTOINFO
491 && ctnetlink_dump_protoinfo(skb, ct) < 0)
492 goto nla_put_failure;
494 if ((events & IPCT_HELPER || nfct_help(ct))
495 && ctnetlink_dump_helpinfo(skb, ct) < 0)
496 goto nla_put_failure;
498 #ifdef CONFIG_NF_CONNTRACK_SECMARK
499 if ((events & IPCT_SECMARK || ct->secmark)
500 && ctnetlink_dump_secmark(skb, ct) < 0)
501 goto nla_put_failure;
504 if (events & IPCT_COUNTER_FILLING &&
505 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
506 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
507 goto nla_put_failure;
509 if (events & IPCT_RELATED &&
510 ctnetlink_dump_master(skb, ct) < 0)
511 goto nla_put_failure;
513 if (events & IPCT_NATSEQADJ &&
514 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
515 goto nla_put_failure;
518 #ifdef CONFIG_NF_CONNTRACK_MARK
519 if ((events & IPCT_MARK || ct->mark)
520 && ctnetlink_dump_mark(skb, ct) < 0)
521 goto nla_put_failure;
524 nlh->nlmsg_len = skb->tail - b;
525 nfnetlink_send(skb, 0, group, 0);
533 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
535 static int ctnetlink_done(struct netlink_callback *cb)
538 nf_ct_put((struct nf_conn *)cb->args[1]);
543 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
545 struct nf_conn *ct, *last;
546 struct nf_conntrack_tuple_hash *h;
547 struct hlist_node *n;
548 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
549 u_int8_t l3proto = nfmsg->nfgen_family;
552 last = (struct nf_conn *)cb->args[1];
553 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
555 hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[cb->args[0]],
557 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
559 ct = nf_ct_tuplehash_to_ctrack(h);
560 /* Dump entries of a given L3 protocol number.
561 * If it is not specified, ie. l3proto == 0,
562 * then dump everything. */
563 if (l3proto && nf_ct_l3num(ct) != l3proto)
570 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
574 if (!atomic_inc_not_zero(&ct->ct_general.use))
576 cb->args[1] = (unsigned long)ct;
579 #ifdef CONFIG_NF_CT_ACCT
580 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
581 IPCTNL_MSG_CT_GET_CTRZERO)
582 memset(&ct->counters, 0, sizeof(ct->counters));
599 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
601 struct nlattr *tb[CTA_IP_MAX+1];
602 struct nf_conntrack_l3proto *l3proto;
605 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
607 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
609 if (likely(l3proto->nlattr_to_tuple)) {
610 ret = nla_validate_nested(attr, CTA_IP_MAX,
611 l3proto->nla_policy);
613 ret = l3proto->nlattr_to_tuple(tb, tuple);
616 nf_ct_l3proto_put(l3proto);
621 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
622 [CTA_PROTO_NUM] = { .type = NLA_U8 },
626 ctnetlink_parse_tuple_proto(struct nlattr *attr,
627 struct nf_conntrack_tuple *tuple)
629 struct nlattr *tb[CTA_PROTO_MAX+1];
630 struct nf_conntrack_l4proto *l4proto;
633 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
637 if (!tb[CTA_PROTO_NUM])
639 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
641 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
643 if (likely(l4proto->nlattr_to_tuple)) {
644 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
645 l4proto->nla_policy);
647 ret = l4proto->nlattr_to_tuple(tb, tuple);
650 nf_ct_l4proto_put(l4proto);
656 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
657 enum ctattr_tuple type, u_int8_t l3num)
659 struct nlattr *tb[CTA_TUPLE_MAX+1];
662 memset(tuple, 0, sizeof(*tuple));
664 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
666 if (!tb[CTA_TUPLE_IP])
669 tuple->src.l3num = l3num;
671 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
675 if (!tb[CTA_TUPLE_PROTO])
678 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
682 /* orig and expect tuples get DIR_ORIGINAL */
683 if (type == CTA_TUPLE_REPLY)
684 tuple->dst.dir = IP_CT_DIR_REPLY;
686 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
691 #ifdef CONFIG_NF_NAT_NEEDED
692 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
693 [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
694 [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
697 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
698 const struct nf_conn *ct,
699 struct nf_nat_range *range)
701 struct nlattr *tb[CTA_PROTONAT_MAX+1];
702 const struct nf_nat_protocol *npt;
705 err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr, protonat_nla_policy);
709 npt = nf_nat_proto_find_get(nf_ct_protonum(ct));
710 if (npt->nlattr_to_range)
711 err = npt->nlattr_to_range(tb, range);
712 nf_nat_proto_put(npt);
716 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
717 [CTA_NAT_MINIP] = { .type = NLA_U32 },
718 [CTA_NAT_MAXIP] = { .type = NLA_U32 },
722 nfnetlink_parse_nat(struct nlattr *nat,
723 const struct nf_conn *ct, struct nf_nat_range *range)
725 struct nlattr *tb[CTA_NAT_MAX+1];
728 memset(range, 0, sizeof(*range));
730 err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy);
734 if (tb[CTA_NAT_MINIP])
735 range->min_ip = nla_get_be32(tb[CTA_NAT_MINIP]);
737 if (!tb[CTA_NAT_MAXIP])
738 range->max_ip = range->min_ip;
740 range->max_ip = nla_get_be32(tb[CTA_NAT_MAXIP]);
743 range->flags |= IP_NAT_RANGE_MAP_IPS;
745 if (!tb[CTA_NAT_PROTO])
748 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
757 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
759 struct nlattr *tb[CTA_HELP_MAX+1];
761 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
763 if (!tb[CTA_HELP_NAME])
766 *helper_name = nla_data(tb[CTA_HELP_NAME]);
771 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
772 [CTA_STATUS] = { .type = NLA_U32 },
773 [CTA_TIMEOUT] = { .type = NLA_U32 },
774 [CTA_MARK] = { .type = NLA_U32 },
775 [CTA_USE] = { .type = NLA_U32 },
776 [CTA_ID] = { .type = NLA_U32 },
780 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
781 struct nlmsghdr *nlh, struct nlattr *cda[])
783 struct nf_conntrack_tuple_hash *h;
784 struct nf_conntrack_tuple tuple;
786 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
787 u_int8_t u3 = nfmsg->nfgen_family;
790 if (cda[CTA_TUPLE_ORIG])
791 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
792 else if (cda[CTA_TUPLE_REPLY])
793 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
795 /* Flush the whole table */
796 nf_conntrack_flush();
803 h = nf_conntrack_find_get(&tuple);
807 ct = nf_ct_tuplehash_to_ctrack(h);
810 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
811 if (id != (u32)(unsigned long)ct) {
824 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
825 struct nlmsghdr *nlh, struct nlattr *cda[])
827 struct nf_conntrack_tuple_hash *h;
828 struct nf_conntrack_tuple tuple;
830 struct sk_buff *skb2 = NULL;
831 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
832 u_int8_t u3 = nfmsg->nfgen_family;
835 if (nlh->nlmsg_flags & NLM_F_DUMP) {
836 #ifndef CONFIG_NF_CT_ACCT
837 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
840 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
844 if (cda[CTA_TUPLE_ORIG])
845 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
846 else if (cda[CTA_TUPLE_REPLY])
847 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
854 h = nf_conntrack_find_get(&tuple);
858 ct = nf_ct_tuplehash_to_ctrack(h);
861 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
867 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
868 IPCTNL_MSG_CT_NEW, 1, ct);
873 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
886 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
889 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
890 d = ct->status ^ status;
892 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
896 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
897 /* SEEN_REPLY bit can only be set */
900 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
901 /* ASSURED bit can only be set */
904 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
905 #ifndef CONFIG_NF_NAT_NEEDED
908 struct nf_nat_range range;
910 if (cda[CTA_NAT_DST]) {
911 if (nfnetlink_parse_nat(cda[CTA_NAT_DST], ct,
914 if (nf_nat_initialized(ct, IP_NAT_MANIP_DST))
916 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
918 if (cda[CTA_NAT_SRC]) {
919 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC], ct,
922 if (nf_nat_initialized(ct, IP_NAT_MANIP_SRC))
924 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
929 /* Be careful here, modifying NAT bits can screw up things,
930 * so don't let users modify them directly if they don't pass
932 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
938 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
940 struct nf_conntrack_helper *helper;
941 struct nf_conn_help *help = nfct_help(ct);
945 /* don't change helper of sibling connections */
949 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
953 if (!strcmp(helpname, "")) {
954 if (help && help->helper) {
955 /* we had a helper before ... */
956 nf_ct_remove_expectations(ct);
957 rcu_assign_pointer(help->helper, NULL);
963 helper = __nf_conntrack_helper_find_byname(helpname);
968 if (help->helper == helper)
972 /* need to zero data of old helper */
973 memset(&help->help, 0, sizeof(help->help));
975 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
980 rcu_assign_pointer(help->helper, helper);
986 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
988 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
990 if (!del_timer(&ct->timeout))
993 ct->timeout.expires = jiffies + timeout * HZ;
994 add_timer(&ct->timeout);
1000 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1002 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1003 struct nf_conntrack_l4proto *l4proto;
1006 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1008 l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
1009 if (l4proto->from_nlattr)
1010 err = l4proto->from_nlattr(tb, ct);
1011 nf_ct_l4proto_put(l4proto);
1016 #ifdef CONFIG_NF_NAT_NEEDED
1018 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1020 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1022 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1024 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1027 natseq->correction_pos =
1028 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1030 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1033 natseq->offset_before =
1034 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1036 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1039 natseq->offset_after =
1040 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1046 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1049 struct nf_conn_nat *nat = nfct_nat(ct);
1054 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1055 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1056 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1060 ct->status |= IPS_SEQ_ADJUST;
1063 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1064 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1065 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1069 ct->status |= IPS_SEQ_ADJUST;
1077 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1081 if (cda[CTA_HELP]) {
1082 err = ctnetlink_change_helper(ct, cda);
1087 if (cda[CTA_TIMEOUT]) {
1088 err = ctnetlink_change_timeout(ct, cda);
1093 if (cda[CTA_STATUS]) {
1094 err = ctnetlink_change_status(ct, cda);
1099 if (cda[CTA_PROTOINFO]) {
1100 err = ctnetlink_change_protoinfo(ct, cda);
1105 #if defined(CONFIG_NF_CONNTRACK_MARK)
1107 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1110 #ifdef CONFIG_NF_NAT_NEEDED
1111 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1112 err = ctnetlink_change_nat_seq_adj(ct, cda);
1122 ctnetlink_create_conntrack(struct nlattr *cda[],
1123 struct nf_conntrack_tuple *otuple,
1124 struct nf_conntrack_tuple *rtuple,
1125 struct nf_conn *master_ct)
1129 struct nf_conn_help *help;
1130 struct nf_conntrack_helper *helper;
1132 ct = nf_conntrack_alloc(otuple, rtuple, GFP_KERNEL);
1133 if (ct == NULL || IS_ERR(ct))
1136 if (!cda[CTA_TIMEOUT])
1138 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1140 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1141 ct->status |= IPS_CONFIRMED;
1143 if (cda[CTA_STATUS]) {
1144 err = ctnetlink_change_status(ct, cda);
1149 if (cda[CTA_PROTOINFO]) {
1150 err = ctnetlink_change_protoinfo(ct, cda);
1155 #if defined(CONFIG_NF_CONNTRACK_MARK)
1157 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1161 helper = __nf_ct_helper_find(rtuple);
1163 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
1169 /* not in hash table yet so not strictly necessary */
1170 rcu_assign_pointer(help->helper, helper);
1173 /* setup master conntrack: this is a confirmed expectation */
1175 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1176 ct->master = master_ct;
1179 add_timer(&ct->timeout);
1180 nf_conntrack_hash_insert(ct);
1186 nf_conntrack_free(ct);
1191 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1192 struct nlmsghdr *nlh, struct nlattr *cda[])
1194 struct nf_conntrack_tuple otuple, rtuple;
1195 struct nf_conntrack_tuple_hash *h = NULL;
1196 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1197 u_int8_t u3 = nfmsg->nfgen_family;
1200 if (cda[CTA_TUPLE_ORIG]) {
1201 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1206 if (cda[CTA_TUPLE_REPLY]) {
1207 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1212 spin_lock_bh(&nf_conntrack_lock);
1213 if (cda[CTA_TUPLE_ORIG])
1214 h = __nf_conntrack_find(&otuple);
1215 else if (cda[CTA_TUPLE_REPLY])
1216 h = __nf_conntrack_find(&rtuple);
1219 struct nf_conntrack_tuple master;
1220 struct nf_conntrack_tuple_hash *master_h = NULL;
1221 struct nf_conn *master_ct = NULL;
1223 if (cda[CTA_TUPLE_MASTER]) {
1224 err = ctnetlink_parse_tuple(cda,
1231 master_h = __nf_conntrack_find(&master);
1232 if (master_h == NULL) {
1236 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1237 atomic_inc(&master_ct->ct_general.use);
1240 spin_unlock_bh(&nf_conntrack_lock);
1242 if (nlh->nlmsg_flags & NLM_F_CREATE)
1243 err = ctnetlink_create_conntrack(cda,
1247 if (err < 0 && master_ct)
1248 nf_ct_put(master_ct);
1252 /* implicit 'else' */
1254 /* We manipulate the conntrack inside the global conntrack table lock,
1255 * so there's no need to increase the refcount */
1257 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1258 /* we only allow nat config for new conntracks */
1259 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1263 /* can't link an existing conntrack to a master */
1264 if (cda[CTA_TUPLE_MASTER]) {
1268 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1273 spin_unlock_bh(&nf_conntrack_lock);
1277 /***********************************************************************
1279 ***********************************************************************/
1282 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1283 const struct nf_conntrack_tuple *tuple,
1284 enum ctattr_expect type)
1286 struct nlattr *nest_parms;
1288 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1290 goto nla_put_failure;
1291 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1292 goto nla_put_failure;
1293 nla_nest_end(skb, nest_parms);
1302 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1303 const struct nf_conntrack_tuple *tuple,
1304 const struct nf_conntrack_tuple_mask *mask)
1307 struct nf_conntrack_l3proto *l3proto;
1308 struct nf_conntrack_l4proto *l4proto;
1309 struct nf_conntrack_tuple m;
1310 struct nlattr *nest_parms;
1312 memset(&m, 0xFF, sizeof(m));
1313 m.src.u.all = mask->src.u.all;
1314 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1316 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1318 goto nla_put_failure;
1320 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1321 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1322 nf_ct_l3proto_put(l3proto);
1324 if (unlikely(ret < 0))
1325 goto nla_put_failure;
1327 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1328 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1329 nf_ct_l4proto_put(l4proto);
1330 if (unlikely(ret < 0))
1331 goto nla_put_failure;
1333 nla_nest_end(skb, nest_parms);
1342 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1343 const struct nf_conntrack_expect *exp)
1345 struct nf_conn *master = exp->master;
1346 long timeout = (exp->timeout.expires - jiffies) / HZ;
1351 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1352 goto nla_put_failure;
1353 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1354 goto nla_put_failure;
1355 if (ctnetlink_exp_dump_tuple(skb,
1356 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1357 CTA_EXPECT_MASTER) < 0)
1358 goto nla_put_failure;
1360 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1361 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1370 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1373 const struct nf_conntrack_expect *exp)
1375 struct nlmsghdr *nlh;
1376 struct nfgenmsg *nfmsg;
1377 unsigned char *b = skb_tail_pointer(skb);
1379 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1380 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1381 nfmsg = NLMSG_DATA(nlh);
1383 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1384 nfmsg->nfgen_family = exp->tuple.src.l3num;
1385 nfmsg->version = NFNETLINK_V0;
1388 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1389 goto nla_put_failure;
1391 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1400 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1401 static int ctnetlink_expect_event(struct notifier_block *this,
1402 unsigned long events, void *ptr)
1404 struct nlmsghdr *nlh;
1405 struct nfgenmsg *nfmsg;
1406 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1407 struct sk_buff *skb;
1412 if (events & IPEXP_NEW) {
1413 type = IPCTNL_MSG_EXP_NEW;
1414 flags = NLM_F_CREATE|NLM_F_EXCL;
1418 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1421 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1427 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1428 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1429 nfmsg = NLMSG_DATA(nlh);
1431 nlh->nlmsg_flags = flags;
1432 nfmsg->nfgen_family = exp->tuple.src.l3num;
1433 nfmsg->version = NFNETLINK_V0;
1436 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1437 goto nla_put_failure;
1439 nlh->nlmsg_len = skb->tail - b;
1440 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1449 static int ctnetlink_exp_done(struct netlink_callback *cb)
1452 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1457 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1459 struct nf_conntrack_expect *exp, *last;
1460 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1461 struct hlist_node *n;
1462 u_int8_t l3proto = nfmsg->nfgen_family;
1465 last = (struct nf_conntrack_expect *)cb->args[1];
1466 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1468 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1470 if (l3proto && exp->tuple.src.l3num != l3proto)
1477 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1481 if (!atomic_inc_not_zero(&exp->use))
1483 cb->args[1] = (unsigned long)exp;
1495 nf_ct_expect_put(last);
1500 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1501 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1502 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1506 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1507 struct nlmsghdr *nlh, struct nlattr *cda[])
1509 struct nf_conntrack_tuple tuple;
1510 struct nf_conntrack_expect *exp;
1511 struct sk_buff *skb2;
1512 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1513 u_int8_t u3 = nfmsg->nfgen_family;
1516 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1517 return netlink_dump_start(ctnl, skb, nlh,
1518 ctnetlink_exp_dump_table,
1519 ctnetlink_exp_done);
1522 if (cda[CTA_EXPECT_MASTER])
1523 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1530 exp = nf_ct_expect_find_get(&tuple);
1534 if (cda[CTA_EXPECT_ID]) {
1535 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1536 if (ntohl(id) != (u32)(unsigned long)exp) {
1537 nf_ct_expect_put(exp);
1543 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1547 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1548 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1553 nf_ct_expect_put(exp);
1555 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1560 nf_ct_expect_put(exp);
1565 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1566 struct nlmsghdr *nlh, struct nlattr *cda[])
1568 struct nf_conntrack_expect *exp;
1569 struct nf_conntrack_tuple tuple;
1570 struct nf_conntrack_helper *h;
1571 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1572 struct hlist_node *n, *next;
1573 u_int8_t u3 = nfmsg->nfgen_family;
1577 if (cda[CTA_EXPECT_TUPLE]) {
1578 /* delete a single expect by tuple */
1579 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1583 /* bump usage count to 2 */
1584 exp = nf_ct_expect_find_get(&tuple);
1588 if (cda[CTA_EXPECT_ID]) {
1589 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1590 if (ntohl(id) != (u32)(unsigned long)exp) {
1591 nf_ct_expect_put(exp);
1596 /* after list removal, usage count == 1 */
1597 nf_ct_unexpect_related(exp);
1598 /* have to put what we 'get' above.
1599 * after this line usage count == 0 */
1600 nf_ct_expect_put(exp);
1601 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1602 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1603 struct nf_conn_help *m_help;
1605 /* delete all expectations for this helper */
1606 spin_lock_bh(&nf_conntrack_lock);
1607 h = __nf_conntrack_helper_find_byname(name);
1609 spin_unlock_bh(&nf_conntrack_lock);
1612 for (i = 0; i < nf_ct_expect_hsize; i++) {
1613 hlist_for_each_entry_safe(exp, n, next,
1614 &nf_ct_expect_hash[i],
1616 m_help = nfct_help(exp->master);
1617 if (m_help->helper == h
1618 && del_timer(&exp->timeout)) {
1619 nf_ct_unlink_expect(exp);
1620 nf_ct_expect_put(exp);
1624 spin_unlock_bh(&nf_conntrack_lock);
1626 /* This basically means we have to flush everything*/
1627 spin_lock_bh(&nf_conntrack_lock);
1628 for (i = 0; i < nf_ct_expect_hsize; i++) {
1629 hlist_for_each_entry_safe(exp, n, next,
1630 &nf_ct_expect_hash[i],
1632 if (del_timer(&exp->timeout)) {
1633 nf_ct_unlink_expect(exp);
1634 nf_ct_expect_put(exp);
1638 spin_unlock_bh(&nf_conntrack_lock);
1644 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1650 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1652 struct nf_conntrack_tuple tuple, mask, master_tuple;
1653 struct nf_conntrack_tuple_hash *h = NULL;
1654 struct nf_conntrack_expect *exp;
1656 struct nf_conn_help *help;
1659 /* caller guarantees that those three CTA_EXPECT_* exist */
1660 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1663 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1666 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1670 /* Look for master conntrack of this expectation */
1671 h = nf_conntrack_find_get(&master_tuple);
1674 ct = nf_ct_tuplehash_to_ctrack(h);
1675 help = nfct_help(ct);
1677 if (!help || !help->helper) {
1678 /* such conntrack hasn't got any helper, abort */
1683 exp = nf_ct_expect_alloc(ct);
1689 exp->expectfn = NULL;
1693 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1694 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1695 exp->mask.src.u.all = mask.src.u.all;
1697 err = nf_ct_expect_related(exp);
1698 nf_ct_expect_put(exp);
1701 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1706 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1707 struct nlmsghdr *nlh, struct nlattr *cda[])
1709 struct nf_conntrack_tuple tuple;
1710 struct nf_conntrack_expect *exp;
1711 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1712 u_int8_t u3 = nfmsg->nfgen_family;
1715 if (!cda[CTA_EXPECT_TUPLE]
1716 || !cda[CTA_EXPECT_MASK]
1717 || !cda[CTA_EXPECT_MASTER])
1720 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1724 spin_lock_bh(&nf_conntrack_lock);
1725 exp = __nf_ct_expect_find(&tuple);
1728 spin_unlock_bh(&nf_conntrack_lock);
1730 if (nlh->nlmsg_flags & NLM_F_CREATE)
1731 err = ctnetlink_create_expect(cda, u3);
1736 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1737 err = ctnetlink_change_expect(exp, cda);
1738 spin_unlock_bh(&nf_conntrack_lock);
1743 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1744 static struct notifier_block ctnl_notifier = {
1745 .notifier_call = ctnetlink_conntrack_event,
1748 static struct notifier_block ctnl_notifier_exp = {
1749 .notifier_call = ctnetlink_expect_event,
1753 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1754 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1755 .attr_count = CTA_MAX,
1756 .policy = ct_nla_policy },
1757 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1758 .attr_count = CTA_MAX,
1759 .policy = ct_nla_policy },
1760 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1761 .attr_count = CTA_MAX,
1762 .policy = ct_nla_policy },
1763 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1764 .attr_count = CTA_MAX,
1765 .policy = ct_nla_policy },
1768 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1769 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1770 .attr_count = CTA_EXPECT_MAX,
1771 .policy = exp_nla_policy },
1772 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1773 .attr_count = CTA_EXPECT_MAX,
1774 .policy = exp_nla_policy },
1775 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1776 .attr_count = CTA_EXPECT_MAX,
1777 .policy = exp_nla_policy },
1780 static const struct nfnetlink_subsystem ctnl_subsys = {
1781 .name = "conntrack",
1782 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1783 .cb_count = IPCTNL_MSG_MAX,
1787 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1788 .name = "conntrack_expect",
1789 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1790 .cb_count = IPCTNL_MSG_EXP_MAX,
1794 MODULE_ALIAS("ip_conntrack_netlink");
1795 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1796 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1798 static int __init ctnetlink_init(void)
1802 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1803 ret = nfnetlink_subsys_register(&ctnl_subsys);
1805 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1809 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1811 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1812 goto err_unreg_subsys;
1815 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1816 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1818 printk("ctnetlink_init: cannot register notifier.\n");
1819 goto err_unreg_exp_subsys;
1822 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1824 printk("ctnetlink_init: cannot expect register notifier.\n");
1825 goto err_unreg_notifier;
1831 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1833 nf_conntrack_unregister_notifier(&ctnl_notifier);
1834 err_unreg_exp_subsys:
1835 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1838 nfnetlink_subsys_unregister(&ctnl_subsys);
1843 static void __exit ctnetlink_exit(void)
1845 printk("ctnetlink: unregistering from nfnetlink.\n");
1847 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1848 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1849 nf_conntrack_unregister_notifier(&ctnl_notifier);
1852 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1853 nfnetlink_subsys_unregister(&ctnl_subsys);
1857 module_init(ctnetlink_init);
1858 module_exit(ctnetlink_exit);