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/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/skbuff.h>
26 #include <linux/errno.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/interrupt.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 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
49 MODULE_LICENSE("GPL");
51 static char __initdata version[] = "0.93";
54 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
55 const struct nf_conntrack_tuple *tuple,
56 struct nf_conntrack_l4proto *l4proto)
59 struct nlattr *nest_parms;
61 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
64 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
66 if (likely(l4proto->tuple_to_nlattr))
67 ret = l4proto->tuple_to_nlattr(skb, tuple);
69 nla_nest_end(skb, nest_parms);
78 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79 const struct nf_conntrack_tuple *tuple,
80 struct nf_conntrack_l3proto *l3proto)
83 struct nlattr *nest_parms;
85 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
89 if (likely(l3proto->tuple_to_nlattr))
90 ret = l3proto->tuple_to_nlattr(skb, tuple);
92 nla_nest_end(skb, nest_parms);
101 ctnetlink_dump_tuples(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
105 struct nf_conntrack_l3proto *l3proto;
106 struct nf_conntrack_l4proto *l4proto;
108 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
109 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
111 if (unlikely(ret < 0))
114 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
115 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
121 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
123 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
131 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
133 long timeout = (ct->timeout.expires - jiffies) / HZ;
138 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
146 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
148 struct nf_conntrack_l4proto *l4proto;
149 struct nlattr *nest_proto;
152 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
153 if (!l4proto->to_nlattr)
156 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
158 goto nla_put_failure;
160 ret = l4proto->to_nlattr(skb, nest_proto, ct);
162 nla_nest_end(skb, nest_proto);
171 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
173 struct nlattr *nest_helper;
174 const struct nf_conn_help *help = nfct_help(ct);
175 struct nf_conntrack_helper *helper;
180 helper = rcu_dereference(help->helper);
184 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
186 goto nla_put_failure;
187 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
189 if (helper->to_nlattr)
190 helper->to_nlattr(skb, ct);
192 nla_nest_end(skb, nest_helper);
201 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
202 enum ip_conntrack_dir dir)
204 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
205 struct nlattr *nest_count;
206 const struct nf_conn_counter *acct;
208 acct = nf_conn_acct_find(ct);
212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
214 goto nla_put_failure;
216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
217 cpu_to_be64(acct[dir].packets));
218 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
219 cpu_to_be64(acct[dir].bytes));
221 nla_nest_end(skb, nest_count);
229 #ifdef CONFIG_NF_CONNTRACK_MARK
231 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
233 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
240 #define ctnetlink_dump_mark(a, b) (0)
243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
245 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
247 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
254 #define ctnetlink_dump_secmark(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)
284 struct nlattr *nest_parms;
286 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
288 goto nla_put_failure;
290 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
291 htonl(natseq->correction_pos));
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
293 htonl(natseq->offset_before));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
295 htonl(natseq->offset_after));
297 nla_nest_end(skb, nest_parms);
306 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
308 struct nf_nat_seq *natseq;
309 struct nf_conn_nat *nat = nfct_nat(ct);
311 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
314 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
315 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
318 natseq = &nat->seq[IP_CT_DIR_REPLY];
319 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
325 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
329 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
331 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
339 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
341 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
349 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
350 int event, const struct nf_conn *ct)
352 struct nlmsghdr *nlh;
353 struct nfgenmsg *nfmsg;
354 struct nlattr *nest_parms;
355 unsigned int flags = pid ? NLM_F_MULTI : 0;
357 event |= NFNL_SUBSYS_CTNETLINK << 8;
358 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
362 nfmsg = nlmsg_data(nlh);
363 nfmsg->nfgen_family = nf_ct_l3num(ct);
364 nfmsg->version = NFNETLINK_V0;
367 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
369 goto nla_put_failure;
370 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
371 goto nla_put_failure;
372 nla_nest_end(skb, nest_parms);
374 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
376 goto nla_put_failure;
377 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
378 goto nla_put_failure;
379 nla_nest_end(skb, nest_parms);
381 if (ctnetlink_dump_status(skb, ct) < 0 ||
382 ctnetlink_dump_timeout(skb, ct) < 0 ||
383 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
384 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
385 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
386 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
387 ctnetlink_dump_mark(skb, ct) < 0 ||
388 ctnetlink_dump_secmark(skb, ct) < 0 ||
389 ctnetlink_dump_id(skb, ct) < 0 ||
390 ctnetlink_dump_use(skb, ct) < 0 ||
391 ctnetlink_dump_master(skb, ct) < 0 ||
392 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
393 goto nla_put_failure;
400 nlmsg_cancel(skb, nlh);
404 #ifdef CONFIG_NF_CONNTRACK_EVENTS
406 ctnetlink_proto_size(const struct nf_conn *ct)
408 struct nf_conntrack_l3proto *l3proto;
409 struct nf_conntrack_l4proto *l4proto;
413 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
414 len += l3proto->nla_size;
416 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
417 len += l4proto->nla_size;
424 ctnetlink_nlmsg_size(const struct nf_conn *ct)
426 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
427 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
428 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
429 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
430 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
431 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
432 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
433 #ifdef CONFIG_NF_CT_ACCT
434 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
435 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
436 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
438 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
439 + nla_total_size(0) /* CTA_PROTOINFO */
440 + nla_total_size(0) /* CTA_HELP */
441 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
442 #ifdef CONFIG_NF_CONNTRACK_SECMARK
443 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
445 #ifdef CONFIG_NF_NAT_NEEDED
446 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
447 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
449 #ifdef CONFIG_NF_CONNTRACK_MARK
450 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
452 + ctnetlink_proto_size(ct)
457 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
459 struct nlmsghdr *nlh;
460 struct nfgenmsg *nfmsg;
461 struct nlattr *nest_parms;
462 struct nf_conn *ct = item->ct;
465 unsigned int flags = 0, group;
467 /* ignore our fake conntrack entry */
468 if (ct == &nf_conntrack_untracked)
471 if (events & IPCT_DESTROY) {
472 type = IPCTNL_MSG_CT_DELETE;
473 group = NFNLGRP_CONNTRACK_DESTROY;
474 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
475 type = IPCTNL_MSG_CT_NEW;
476 flags = NLM_F_CREATE|NLM_F_EXCL;
477 group = NFNLGRP_CONNTRACK_NEW;
479 type = IPCTNL_MSG_CT_NEW;
480 group = NFNLGRP_CONNTRACK_UPDATE;
484 if (!item->report && !nfnetlink_has_listeners(group))
487 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
491 type |= NFNL_SUBSYS_CTNETLINK << 8;
492 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
496 nfmsg = nlmsg_data(nlh);
497 nfmsg->nfgen_family = nf_ct_l3num(ct);
498 nfmsg->version = NFNETLINK_V0;
502 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
504 goto nla_put_failure;
505 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
506 goto nla_put_failure;
507 nla_nest_end(skb, nest_parms);
509 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
511 goto nla_put_failure;
512 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
513 goto nla_put_failure;
514 nla_nest_end(skb, nest_parms);
516 if (ctnetlink_dump_id(skb, ct) < 0)
517 goto nla_put_failure;
519 if (ctnetlink_dump_status(skb, ct) < 0)
520 goto nla_put_failure;
522 if (events & IPCT_DESTROY) {
523 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
524 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
525 goto nla_put_failure;
527 if (ctnetlink_dump_timeout(skb, ct) < 0)
528 goto nla_put_failure;
530 if (events & IPCT_PROTOINFO
531 && ctnetlink_dump_protoinfo(skb, ct) < 0)
532 goto nla_put_failure;
534 if ((events & IPCT_HELPER || nfct_help(ct))
535 && ctnetlink_dump_helpinfo(skb, ct) < 0)
536 goto nla_put_failure;
538 #ifdef CONFIG_NF_CONNTRACK_SECMARK
539 if ((events & IPCT_SECMARK || ct->secmark)
540 && ctnetlink_dump_secmark(skb, ct) < 0)
541 goto nla_put_failure;
544 if (events & IPCT_RELATED &&
545 ctnetlink_dump_master(skb, ct) < 0)
546 goto nla_put_failure;
548 if (events & IPCT_NATSEQADJ &&
549 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
550 goto nla_put_failure;
553 #ifdef CONFIG_NF_CONNTRACK_MARK
554 if ((events & IPCT_MARK || ct->mark)
555 && ctnetlink_dump_mark(skb, ct) < 0)
556 goto nla_put_failure;
561 nfnetlink_send(skb, item->pid, group, item->report, GFP_ATOMIC);
566 nlmsg_cancel(skb, nlh);
570 nfnetlink_set_err(0, group, -ENOBUFS);
573 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
575 static int ctnetlink_done(struct netlink_callback *cb)
578 nf_ct_put((struct nf_conn *)cb->args[1]);
583 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
585 struct nf_conn *ct, *last;
586 struct nf_conntrack_tuple_hash *h;
587 struct hlist_nulls_node *n;
588 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
589 u_int8_t l3proto = nfmsg->nfgen_family;
592 last = (struct nf_conn *)cb->args[1];
593 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
595 hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
597 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
599 ct = nf_ct_tuplehash_to_ctrack(h);
600 if (!atomic_inc_not_zero(&ct->ct_general.use))
602 /* Dump entries of a given L3 protocol number.
603 * If it is not specified, ie. l3proto == 0,
604 * then dump everything. */
605 if (l3proto && nf_ct_l3num(ct) != l3proto)
612 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
614 IPCTNL_MSG_CT_NEW, ct) < 0) {
615 cb->args[1] = (unsigned long)ct;
619 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
620 IPCTNL_MSG_CT_GET_CTRZERO) {
621 struct nf_conn_counter *acct;
623 acct = nf_conn_acct_find(ct);
625 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
644 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
646 struct nlattr *tb[CTA_IP_MAX+1];
647 struct nf_conntrack_l3proto *l3proto;
650 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
653 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
655 if (likely(l3proto->nlattr_to_tuple)) {
656 ret = nla_validate_nested(attr, CTA_IP_MAX,
657 l3proto->nla_policy);
659 ret = l3proto->nlattr_to_tuple(tb, tuple);
667 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
668 [CTA_PROTO_NUM] = { .type = NLA_U8 },
672 ctnetlink_parse_tuple_proto(struct nlattr *attr,
673 struct nf_conntrack_tuple *tuple)
675 struct nlattr *tb[CTA_PROTO_MAX+1];
676 struct nf_conntrack_l4proto *l4proto;
679 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
683 if (!tb[CTA_PROTO_NUM])
685 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
688 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
690 if (likely(l4proto->nlattr_to_tuple)) {
691 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
692 l4proto->nla_policy);
694 ret = l4proto->nlattr_to_tuple(tb, tuple);
703 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
704 enum ctattr_tuple type, u_int8_t l3num)
706 struct nlattr *tb[CTA_TUPLE_MAX+1];
709 memset(tuple, 0, sizeof(*tuple));
711 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
713 if (!tb[CTA_TUPLE_IP])
716 tuple->src.l3num = l3num;
718 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
722 if (!tb[CTA_TUPLE_PROTO])
725 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
729 /* orig and expect tuples get DIR_ORIGINAL */
730 if (type == CTA_TUPLE_REPLY)
731 tuple->dst.dir = IP_CT_DIR_REPLY;
733 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
739 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
741 struct nlattr *tb[CTA_HELP_MAX+1];
743 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
745 if (!tb[CTA_HELP_NAME])
748 *helper_name = nla_data(tb[CTA_HELP_NAME]);
753 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
754 [CTA_STATUS] = { .type = NLA_U32 },
755 [CTA_TIMEOUT] = { .type = NLA_U32 },
756 [CTA_MARK] = { .type = NLA_U32 },
757 [CTA_USE] = { .type = NLA_U32 },
758 [CTA_ID] = { .type = NLA_U32 },
762 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
763 struct nlmsghdr *nlh, struct nlattr *cda[])
765 struct nf_conntrack_tuple_hash *h;
766 struct nf_conntrack_tuple tuple;
768 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
769 u_int8_t u3 = nfmsg->nfgen_family;
772 if (cda[CTA_TUPLE_ORIG])
773 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
774 else if (cda[CTA_TUPLE_REPLY])
775 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
777 /* Flush the whole table */
778 nf_conntrack_flush_report(&init_net,
787 h = nf_conntrack_find_get(&init_net, &tuple);
791 ct = nf_ct_tuplehash_to_ctrack(h);
794 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
795 if (id != (u32)(unsigned long)ct) {
801 nf_conntrack_event_report(IPCT_DESTROY,
806 /* death_by_timeout would report the event again */
807 set_bit(IPS_DYING_BIT, &ct->status);
816 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
817 struct nlmsghdr *nlh, struct nlattr *cda[])
819 struct nf_conntrack_tuple_hash *h;
820 struct nf_conntrack_tuple tuple;
822 struct sk_buff *skb2 = NULL;
823 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
824 u_int8_t u3 = nfmsg->nfgen_family;
827 if (nlh->nlmsg_flags & NLM_F_DUMP)
828 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
831 if (cda[CTA_TUPLE_ORIG])
832 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
833 else if (cda[CTA_TUPLE_REPLY])
834 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
841 h = nf_conntrack_find_get(&init_net, &tuple);
845 ct = nf_ct_tuplehash_to_ctrack(h);
848 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
855 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
856 IPCTNL_MSG_CT_NEW, ct);
862 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
874 #ifdef CONFIG_NF_NAT_NEEDED
876 ctnetlink_parse_nat_setup(struct nf_conn *ct,
877 enum nf_nat_manip_type manip,
880 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
882 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
883 if (!parse_nat_setup) {
884 #ifdef CONFIG_MODULES
886 spin_unlock_bh(&nf_conntrack_lock);
888 if (request_module("nf-nat-ipv4") < 0) {
890 spin_lock_bh(&nf_conntrack_lock);
895 spin_lock_bh(&nf_conntrack_lock);
897 if (nfnetlink_parse_nat_setup_hook)
903 return parse_nat_setup(ct, manip, attr);
908 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
911 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
912 d = ct->status ^ status;
914 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
918 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
919 /* SEEN_REPLY bit can only be set */
922 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
923 /* ASSURED bit can only be set */
926 /* Be careful here, modifying NAT bits can screw up things,
927 * so don't let users modify them directly if they don't pass
929 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
934 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
936 #ifdef CONFIG_NF_NAT_NEEDED
939 if (cda[CTA_NAT_DST]) {
940 ret = ctnetlink_parse_nat_setup(ct,
946 if (cda[CTA_NAT_SRC]) {
947 ret = ctnetlink_parse_nat_setup(ct,
960 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
962 struct nf_conntrack_helper *helper;
963 struct nf_conn_help *help = nfct_help(ct);
964 char *helpname = NULL;
967 /* don't change helper of sibling connections */
971 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
975 if (!strcmp(helpname, "")) {
976 if (help && help->helper) {
977 /* we had a helper before ... */
978 nf_ct_remove_expectations(ct);
979 rcu_assign_pointer(help->helper, NULL);
985 helper = __nf_conntrack_helper_find_byname(helpname);
986 if (helper == NULL) {
987 #ifdef CONFIG_MODULES
988 spin_unlock_bh(&nf_conntrack_lock);
990 if (request_module("nfct-helper-%s", helpname) < 0) {
991 spin_lock_bh(&nf_conntrack_lock);
995 spin_lock_bh(&nf_conntrack_lock);
996 helper = __nf_conntrack_helper_find_byname(helpname);
1004 if (help->helper == helper)
1008 /* need to zero data of old helper */
1009 memset(&help->help, 0, sizeof(help->help));
1011 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1016 rcu_assign_pointer(help->helper, helper);
1022 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
1024 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1026 if (!del_timer(&ct->timeout))
1029 ct->timeout.expires = jiffies + timeout * HZ;
1030 add_timer(&ct->timeout);
1036 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1038 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1039 struct nf_conntrack_l4proto *l4proto;
1042 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1045 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1046 if (l4proto->from_nlattr)
1047 err = l4proto->from_nlattr(tb, ct);
1053 #ifdef CONFIG_NF_NAT_NEEDED
1055 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1057 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1059 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1061 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1064 natseq->correction_pos =
1065 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1067 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1070 natseq->offset_before =
1071 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1073 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1076 natseq->offset_after =
1077 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1083 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1086 struct nf_conn_nat *nat = nfct_nat(ct);
1091 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1092 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1093 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1097 ct->status |= IPS_SEQ_ADJUST;
1100 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1101 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1102 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1106 ct->status |= IPS_SEQ_ADJUST;
1114 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1118 /* only allow NAT changes and master assignation for new conntracks */
1119 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1122 if (cda[CTA_HELP]) {
1123 err = ctnetlink_change_helper(ct, cda);
1128 if (cda[CTA_TIMEOUT]) {
1129 err = ctnetlink_change_timeout(ct, cda);
1134 if (cda[CTA_STATUS]) {
1135 err = ctnetlink_change_status(ct, cda);
1140 if (cda[CTA_PROTOINFO]) {
1141 err = ctnetlink_change_protoinfo(ct, cda);
1146 #if defined(CONFIG_NF_CONNTRACK_MARK)
1148 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1151 #ifdef CONFIG_NF_NAT_NEEDED
1152 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1153 err = ctnetlink_change_nat_seq_adj(ct, cda);
1162 static struct nf_conn *
1163 ctnetlink_create_conntrack(struct nlattr *cda[],
1164 struct nf_conntrack_tuple *otuple,
1165 struct nf_conntrack_tuple *rtuple,
1170 struct nf_conntrack_helper *helper;
1172 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1174 return ERR_PTR(-ENOMEM);
1176 if (!cda[CTA_TIMEOUT])
1178 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1180 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1181 ct->status |= IPS_CONFIRMED;
1184 if (cda[CTA_HELP]) {
1185 char *helpname = NULL;
1187 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1191 helper = __nf_conntrack_helper_find_byname(helpname);
1192 if (helper == NULL) {
1194 #ifdef CONFIG_MODULES
1195 if (request_module("nfct-helper-%s", helpname) < 0) {
1201 helper = __nf_conntrack_helper_find_byname(helpname);
1211 struct nf_conn_help *help;
1213 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1219 /* not in hash table yet so not strictly necessary */
1220 rcu_assign_pointer(help->helper, helper);
1223 /* try an implicit helper assignation */
1224 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1229 if (cda[CTA_STATUS]) {
1230 err = ctnetlink_change_status(ct, cda);
1235 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1236 err = ctnetlink_change_nat(ct, cda);
1241 #ifdef CONFIG_NF_NAT_NEEDED
1242 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1243 err = ctnetlink_change_nat_seq_adj(ct, cda);
1249 if (cda[CTA_PROTOINFO]) {
1250 err = ctnetlink_change_protoinfo(ct, cda);
1255 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1257 #if defined(CONFIG_NF_CONNTRACK_MARK)
1259 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1262 /* setup master conntrack: this is a confirmed expectation */
1263 if (cda[CTA_TUPLE_MASTER]) {
1264 struct nf_conntrack_tuple master;
1265 struct nf_conntrack_tuple_hash *master_h;
1266 struct nf_conn *master_ct;
1268 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1272 master_h = nf_conntrack_find_get(&init_net, &master);
1273 if (master_h == NULL) {
1277 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1278 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1279 ct->master = master_ct;
1282 add_timer(&ct->timeout);
1283 nf_conntrack_hash_insert(ct);
1291 nf_conntrack_free(ct);
1292 return ERR_PTR(err);
1296 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1297 struct nlmsghdr *nlh, struct nlattr *cda[])
1299 struct nf_conntrack_tuple otuple, rtuple;
1300 struct nf_conntrack_tuple_hash *h = NULL;
1301 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1302 u_int8_t u3 = nfmsg->nfgen_family;
1305 if (cda[CTA_TUPLE_ORIG]) {
1306 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1311 if (cda[CTA_TUPLE_REPLY]) {
1312 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1317 spin_lock_bh(&nf_conntrack_lock);
1318 if (cda[CTA_TUPLE_ORIG])
1319 h = __nf_conntrack_find(&init_net, &otuple);
1320 else if (cda[CTA_TUPLE_REPLY])
1321 h = __nf_conntrack_find(&init_net, &rtuple);
1325 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1327 enum ip_conntrack_events events;
1329 ct = ctnetlink_create_conntrack(cda, &otuple,
1336 nf_conntrack_get(&ct->ct_general);
1337 spin_unlock_bh(&nf_conntrack_lock);
1338 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1339 events = IPCT_RELATED;
1343 nf_conntrack_event_report(IPCT_STATUS |
1348 ct, NETLINK_CB(skb).pid,
1352 spin_unlock_bh(&nf_conntrack_lock);
1356 /* implicit 'else' */
1358 /* We manipulate the conntrack inside the global conntrack table lock,
1359 * so there's no need to increase the refcount */
1361 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1362 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1364 err = ctnetlink_change_conntrack(ct, cda);
1366 nf_conntrack_get(&ct->ct_general);
1367 spin_unlock_bh(&nf_conntrack_lock);
1368 nf_conntrack_event_report(IPCT_STATUS |
1373 ct, NETLINK_CB(skb).pid,
1377 spin_unlock_bh(&nf_conntrack_lock);
1383 spin_unlock_bh(&nf_conntrack_lock);
1387 /***********************************************************************
1389 ***********************************************************************/
1392 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1393 const struct nf_conntrack_tuple *tuple,
1394 enum ctattr_expect type)
1396 struct nlattr *nest_parms;
1398 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1400 goto nla_put_failure;
1401 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1402 goto nla_put_failure;
1403 nla_nest_end(skb, nest_parms);
1412 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1413 const struct nf_conntrack_tuple *tuple,
1414 const struct nf_conntrack_tuple_mask *mask)
1417 struct nf_conntrack_l3proto *l3proto;
1418 struct nf_conntrack_l4proto *l4proto;
1419 struct nf_conntrack_tuple m;
1420 struct nlattr *nest_parms;
1422 memset(&m, 0xFF, sizeof(m));
1423 m.src.u.all = mask->src.u.all;
1424 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1426 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1428 goto nla_put_failure;
1430 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1431 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1433 if (unlikely(ret < 0))
1434 goto nla_put_failure;
1436 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1437 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1438 if (unlikely(ret < 0))
1439 goto nla_put_failure;
1441 nla_nest_end(skb, nest_parms);
1450 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1451 const struct nf_conntrack_expect *exp)
1453 struct nf_conn *master = exp->master;
1454 long timeout = (exp->timeout.expires - jiffies) / HZ;
1459 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1460 goto nla_put_failure;
1461 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1462 goto nla_put_failure;
1463 if (ctnetlink_exp_dump_tuple(skb,
1464 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1465 CTA_EXPECT_MASTER) < 0)
1466 goto nla_put_failure;
1468 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1469 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1478 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1479 int event, const struct nf_conntrack_expect *exp)
1481 struct nlmsghdr *nlh;
1482 struct nfgenmsg *nfmsg;
1483 unsigned int flags = pid ? NLM_F_MULTI : 0;
1485 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1486 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1490 nfmsg = nlmsg_data(nlh);
1491 nfmsg->nfgen_family = exp->tuple.src.l3num;
1492 nfmsg->version = NFNETLINK_V0;
1495 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1496 goto nla_put_failure;
1498 nlmsg_end(skb, nlh);
1503 nlmsg_cancel(skb, nlh);
1507 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1509 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1511 struct nlmsghdr *nlh;
1512 struct nfgenmsg *nfmsg;
1513 struct nf_conntrack_expect *exp = item->exp;
1514 struct sk_buff *skb;
1518 if (events & IPEXP_NEW) {
1519 type = IPCTNL_MSG_EXP_NEW;
1520 flags = NLM_F_CREATE|NLM_F_EXCL;
1524 if (!item->report &&
1525 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1528 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1532 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1533 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1537 nfmsg = nlmsg_data(nlh);
1538 nfmsg->nfgen_family = exp->tuple.src.l3num;
1539 nfmsg->version = NFNETLINK_V0;
1543 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1544 goto nla_put_failure;
1547 nlmsg_end(skb, nlh);
1548 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
1549 item->report, GFP_ATOMIC);
1554 nlmsg_cancel(skb, nlh);
1558 nfnetlink_set_err(0, 0, -ENOBUFS);
1562 static int ctnetlink_exp_done(struct netlink_callback *cb)
1565 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1570 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1572 struct net *net = &init_net;
1573 struct nf_conntrack_expect *exp, *last;
1574 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1575 struct hlist_node *n;
1576 u_int8_t l3proto = nfmsg->nfgen_family;
1579 last = (struct nf_conntrack_expect *)cb->args[1];
1580 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1582 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1584 if (l3proto && exp->tuple.src.l3num != l3proto)
1591 if (ctnetlink_exp_fill_info(skb,
1592 NETLINK_CB(cb->skb).pid,
1596 if (!atomic_inc_not_zero(&exp->use))
1598 cb->args[1] = (unsigned long)exp;
1610 nf_ct_expect_put(last);
1615 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1616 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1617 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1621 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1622 struct nlmsghdr *nlh, struct nlattr *cda[])
1624 struct nf_conntrack_tuple tuple;
1625 struct nf_conntrack_expect *exp;
1626 struct sk_buff *skb2;
1627 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1628 u_int8_t u3 = nfmsg->nfgen_family;
1631 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1632 return netlink_dump_start(ctnl, skb, nlh,
1633 ctnetlink_exp_dump_table,
1634 ctnetlink_exp_done);
1637 if (cda[CTA_EXPECT_MASTER])
1638 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1645 exp = nf_ct_expect_find_get(&init_net, &tuple);
1649 if (cda[CTA_EXPECT_ID]) {
1650 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1651 if (ntohl(id) != (u32)(unsigned long)exp) {
1652 nf_ct_expect_put(exp);
1658 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1663 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1664 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1669 nf_ct_expect_put(exp);
1671 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1676 nf_ct_expect_put(exp);
1681 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1682 struct nlmsghdr *nlh, struct nlattr *cda[])
1684 struct nf_conntrack_expect *exp;
1685 struct nf_conntrack_tuple tuple;
1686 struct nf_conntrack_helper *h;
1687 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1688 struct hlist_node *n, *next;
1689 u_int8_t u3 = nfmsg->nfgen_family;
1693 if (cda[CTA_EXPECT_TUPLE]) {
1694 /* delete a single expect by tuple */
1695 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1699 /* bump usage count to 2 */
1700 exp = nf_ct_expect_find_get(&init_net, &tuple);
1704 if (cda[CTA_EXPECT_ID]) {
1705 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1706 if (ntohl(id) != (u32)(unsigned long)exp) {
1707 nf_ct_expect_put(exp);
1712 /* after list removal, usage count == 1 */
1713 nf_ct_unexpect_related(exp);
1714 /* have to put what we 'get' above.
1715 * after this line usage count == 0 */
1716 nf_ct_expect_put(exp);
1717 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1718 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1719 struct nf_conn_help *m_help;
1721 /* delete all expectations for this helper */
1722 spin_lock_bh(&nf_conntrack_lock);
1723 h = __nf_conntrack_helper_find_byname(name);
1725 spin_unlock_bh(&nf_conntrack_lock);
1728 for (i = 0; i < nf_ct_expect_hsize; i++) {
1729 hlist_for_each_entry_safe(exp, n, next,
1730 &init_net.ct.expect_hash[i],
1732 m_help = nfct_help(exp->master);
1733 if (m_help->helper == h
1734 && del_timer(&exp->timeout)) {
1735 nf_ct_unlink_expect(exp);
1736 nf_ct_expect_put(exp);
1740 spin_unlock_bh(&nf_conntrack_lock);
1742 /* This basically means we have to flush everything*/
1743 spin_lock_bh(&nf_conntrack_lock);
1744 for (i = 0; i < nf_ct_expect_hsize; i++) {
1745 hlist_for_each_entry_safe(exp, n, next,
1746 &init_net.ct.expect_hash[i],
1748 if (del_timer(&exp->timeout)) {
1749 nf_ct_unlink_expect(exp);
1750 nf_ct_expect_put(exp);
1754 spin_unlock_bh(&nf_conntrack_lock);
1760 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1766 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
1768 struct nf_conntrack_tuple tuple, mask, master_tuple;
1769 struct nf_conntrack_tuple_hash *h = NULL;
1770 struct nf_conntrack_expect *exp;
1772 struct nf_conn_help *help;
1775 /* caller guarantees that those three CTA_EXPECT_* exist */
1776 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1779 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1782 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1786 /* Look for master conntrack of this expectation */
1787 h = nf_conntrack_find_get(&init_net, &master_tuple);
1790 ct = nf_ct_tuplehash_to_ctrack(h);
1791 help = nfct_help(ct);
1793 if (!help || !help->helper) {
1794 /* such conntrack hasn't got any helper, abort */
1799 exp = nf_ct_expect_alloc(ct);
1806 exp->expectfn = NULL;
1810 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1811 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1812 exp->mask.src.u.all = mask.src.u.all;
1814 err = nf_ct_expect_related_report(exp, pid, report);
1815 nf_ct_expect_put(exp);
1818 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1823 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1824 struct nlmsghdr *nlh, struct nlattr *cda[])
1826 struct nf_conntrack_tuple tuple;
1827 struct nf_conntrack_expect *exp;
1828 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1829 u_int8_t u3 = nfmsg->nfgen_family;
1832 if (!cda[CTA_EXPECT_TUPLE]
1833 || !cda[CTA_EXPECT_MASK]
1834 || !cda[CTA_EXPECT_MASTER])
1837 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1841 spin_lock_bh(&nf_conntrack_lock);
1842 exp = __nf_ct_expect_find(&init_net, &tuple);
1845 spin_unlock_bh(&nf_conntrack_lock);
1847 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1848 err = ctnetlink_create_expect(cda,
1850 NETLINK_CB(skb).pid,
1857 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1858 err = ctnetlink_change_expect(exp, cda);
1859 spin_unlock_bh(&nf_conntrack_lock);
1864 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1865 static struct nf_ct_event_notifier ctnl_notifier = {
1866 .fcn = ctnetlink_conntrack_event,
1869 static struct nf_exp_event_notifier ctnl_notifier_exp = {
1870 .fcn = ctnetlink_expect_event,
1874 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1875 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1876 .attr_count = CTA_MAX,
1877 .policy = ct_nla_policy },
1878 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1879 .attr_count = CTA_MAX,
1880 .policy = ct_nla_policy },
1881 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1882 .attr_count = CTA_MAX,
1883 .policy = ct_nla_policy },
1884 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1885 .attr_count = CTA_MAX,
1886 .policy = ct_nla_policy },
1889 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1890 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1891 .attr_count = CTA_EXPECT_MAX,
1892 .policy = exp_nla_policy },
1893 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1894 .attr_count = CTA_EXPECT_MAX,
1895 .policy = exp_nla_policy },
1896 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1897 .attr_count = CTA_EXPECT_MAX,
1898 .policy = exp_nla_policy },
1901 static const struct nfnetlink_subsystem ctnl_subsys = {
1902 .name = "conntrack",
1903 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1904 .cb_count = IPCTNL_MSG_MAX,
1908 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1909 .name = "conntrack_expect",
1910 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1911 .cb_count = IPCTNL_MSG_EXP_MAX,
1915 MODULE_ALIAS("ip_conntrack_netlink");
1916 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1917 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1919 static int __init ctnetlink_init(void)
1923 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1924 ret = nfnetlink_subsys_register(&ctnl_subsys);
1926 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1930 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1932 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1933 goto err_unreg_subsys;
1936 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1937 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1939 printk("ctnetlink_init: cannot register notifier.\n");
1940 goto err_unreg_exp_subsys;
1943 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1945 printk("ctnetlink_init: cannot expect register notifier.\n");
1946 goto err_unreg_notifier;
1952 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1954 nf_conntrack_unregister_notifier(&ctnl_notifier);
1955 err_unreg_exp_subsys:
1956 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1959 nfnetlink_subsys_unregister(&ctnl_subsys);
1964 static void __exit ctnetlink_exit(void)
1966 printk("ctnetlink: unregistering from nfnetlink.\n");
1968 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1969 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1970 nf_conntrack_unregister_notifier(&ctnl_notifier);
1973 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1974 nfnetlink_subsys_unregister(&ctnl_subsys);
1978 module_init(ctnetlink_init);
1979 module_exit(ctnetlink_exit);