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>
30 #include <linux/notifier.h>
32 #include <linux/netfilter.h>
33 #include <net/netlink.h>
34 #include <net/netfilter/nf_conntrack.h>
35 #include <net/netfilter/nf_conntrack_core.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_l4proto.h>
40 #include <net/netfilter/nf_conntrack_tuple.h>
41 #include <net/netfilter/nf_conntrack_acct.h>
42 #ifdef CONFIG_NF_NAT_NEEDED
43 #include <net/netfilter/nf_nat_core.h>
44 #include <net/netfilter/nf_nat_protocol.h>
47 #include <linux/netfilter/nfnetlink.h>
48 #include <linux/netfilter/nfnetlink_conntrack.h>
50 MODULE_LICENSE("GPL");
52 static char __initdata version[] = "0.93";
55 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
56 const struct nf_conntrack_tuple *tuple,
57 struct nf_conntrack_l4proto *l4proto)
60 struct nlattr *nest_parms;
62 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
65 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
67 if (likely(l4proto->tuple_to_nlattr))
68 ret = l4proto->tuple_to_nlattr(skb, tuple);
70 nla_nest_end(skb, nest_parms);
79 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
80 const struct nf_conntrack_tuple *tuple,
81 struct nf_conntrack_l3proto *l3proto)
84 struct nlattr *nest_parms;
86 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
90 if (likely(l3proto->tuple_to_nlattr))
91 ret = l3proto->tuple_to_nlattr(skb, tuple);
93 nla_nest_end(skb, nest_parms);
102 ctnetlink_dump_tuples(struct sk_buff *skb,
103 const struct nf_conntrack_tuple *tuple)
106 struct nf_conntrack_l3proto *l3proto;
107 struct nf_conntrack_l4proto *l4proto;
109 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
110 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
112 if (unlikely(ret < 0))
115 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
116 ret = ctnetlink_dump_tuples_proto(skb, tuple, 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(nf_ct_l3num(ct), nf_ct_protonum(ct));
154 if (!l4proto->to_nlattr)
157 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
159 goto nla_put_failure;
161 ret = l4proto->to_nlattr(skb, nest_proto, ct);
163 nla_nest_end(skb, nest_proto);
172 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
174 struct nlattr *nest_helper;
175 const struct nf_conn_help *help = nfct_help(ct);
176 struct nf_conntrack_helper *helper;
181 helper = rcu_dereference(help->helper);
185 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
187 goto nla_put_failure;
188 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
190 if (helper->to_nlattr)
191 helper->to_nlattr(skb, ct);
193 nla_nest_end(skb, nest_helper);
202 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
203 enum ip_conntrack_dir dir)
205 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
206 struct nlattr *nest_count;
207 const struct nf_conn_counter *acct;
209 acct = nf_conn_acct_find(ct);
213 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
215 goto nla_put_failure;
217 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
218 cpu_to_be64(acct[dir].packets));
219 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
220 cpu_to_be64(acct[dir].bytes));
222 nla_nest_end(skb, nest_count);
230 #ifdef CONFIG_NF_CONNTRACK_MARK
232 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
234 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
241 #define ctnetlink_dump_mark(a, b) (0)
244 #ifdef CONFIG_NF_CONNTRACK_SECMARK
246 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
248 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
255 #define ctnetlink_dump_secmark(a, b) (0)
258 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
261 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
263 struct nlattr *nest_parms;
265 if (!(ct->status & IPS_EXPECTED))
268 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
270 goto nla_put_failure;
271 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
272 goto nla_put_failure;
273 nla_nest_end(skb, nest_parms);
281 #ifdef CONFIG_NF_NAT_NEEDED
283 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 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
292 htonl(natseq->correction_pos));
293 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
294 htonl(natseq->offset_before));
295 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
296 htonl(natseq->offset_after));
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 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
340 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
342 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
350 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
351 int event, const struct nf_conn *ct)
353 struct nlmsghdr *nlh;
354 struct nfgenmsg *nfmsg;
355 struct nlattr *nest_parms;
356 unsigned int flags = pid ? NLM_F_MULTI : 0;
358 event |= NFNL_SUBSYS_CTNETLINK << 8;
359 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
363 nfmsg = nlmsg_data(nlh);
364 nfmsg->nfgen_family = nf_ct_l3num(ct);
365 nfmsg->version = NFNETLINK_V0;
368 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
370 goto nla_put_failure;
371 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
372 goto nla_put_failure;
373 nla_nest_end(skb, nest_parms);
375 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
377 goto nla_put_failure;
378 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
379 goto nla_put_failure;
380 nla_nest_end(skb, nest_parms);
382 if (ctnetlink_dump_status(skb, ct) < 0 ||
383 ctnetlink_dump_timeout(skb, ct) < 0 ||
384 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
385 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
386 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
387 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
388 ctnetlink_dump_mark(skb, ct) < 0 ||
389 ctnetlink_dump_secmark(skb, ct) < 0 ||
390 ctnetlink_dump_id(skb, ct) < 0 ||
391 ctnetlink_dump_use(skb, ct) < 0 ||
392 ctnetlink_dump_master(skb, ct) < 0 ||
393 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
394 goto nla_put_failure;
401 nlmsg_cancel(skb, nlh);
405 #ifdef CONFIG_NF_CONNTRACK_EVENTS
407 ctnetlink_proto_size(const struct nf_conn *ct)
409 struct nf_conntrack_l3proto *l3proto;
410 struct nf_conntrack_l4proto *l4proto;
414 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
415 len += l3proto->nla_size;
417 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
418 len += l4proto->nla_size;
425 ctnetlink_nlmsg_size(const struct nf_conn *ct)
427 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
428 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
429 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
430 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
431 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
432 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
433 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
434 #ifdef CONFIG_NF_CT_ACCT
435 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
436 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
437 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
439 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
440 + nla_total_size(0) /* CTA_PROTOINFO */
441 + nla_total_size(0) /* CTA_HELP */
442 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
443 #ifdef CONFIG_NF_CONNTRACK_SECMARK
444 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
446 #ifdef CONFIG_NF_NAT_NEEDED
447 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
448 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
450 #ifdef CONFIG_NF_CONNTRACK_MARK
451 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
453 + ctnetlink_proto_size(ct)
457 static int ctnetlink_conntrack_event(struct notifier_block *this,
458 unsigned long events, void *ptr)
460 struct nlmsghdr *nlh;
461 struct nfgenmsg *nfmsg;
462 struct nlattr *nest_parms;
463 struct nf_ct_event *item = (struct nf_ct_event *)ptr;
464 struct nf_conn *ct = item->ct;
467 unsigned int flags = 0, group;
469 /* ignore our fake conntrack entry */
470 if (ct == &nf_conntrack_untracked)
473 if (events & IPCT_DESTROY) {
474 type = IPCTNL_MSG_CT_DELETE;
475 group = NFNLGRP_CONNTRACK_DESTROY;
476 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
477 type = IPCTNL_MSG_CT_NEW;
478 flags = NLM_F_CREATE|NLM_F_EXCL;
479 group = NFNLGRP_CONNTRACK_NEW;
480 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
481 type = IPCTNL_MSG_CT_NEW;
482 group = NFNLGRP_CONNTRACK_UPDATE;
486 if (!item->report && !nfnetlink_has_listeners(group))
489 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
493 type |= NFNL_SUBSYS_CTNETLINK << 8;
494 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
498 nfmsg = nlmsg_data(nlh);
499 nfmsg->nfgen_family = nf_ct_l3num(ct);
500 nfmsg->version = NFNETLINK_V0;
504 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
506 goto nla_put_failure;
507 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
508 goto nla_put_failure;
509 nla_nest_end(skb, nest_parms);
511 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
513 goto nla_put_failure;
514 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
515 goto nla_put_failure;
516 nla_nest_end(skb, nest_parms);
518 if (ctnetlink_dump_id(skb, ct) < 0)
519 goto nla_put_failure;
521 if (ctnetlink_dump_status(skb, ct) < 0)
522 goto nla_put_failure;
524 if (events & IPCT_DESTROY) {
525 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
526 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
527 goto nla_put_failure;
529 if (ctnetlink_dump_timeout(skb, ct) < 0)
530 goto nla_put_failure;
532 if (events & IPCT_PROTOINFO
533 && ctnetlink_dump_protoinfo(skb, ct) < 0)
534 goto nla_put_failure;
536 if ((events & IPCT_HELPER || nfct_help(ct))
537 && ctnetlink_dump_helpinfo(skb, ct) < 0)
538 goto nla_put_failure;
540 #ifdef CONFIG_NF_CONNTRACK_SECMARK
541 if ((events & IPCT_SECMARK || ct->secmark)
542 && ctnetlink_dump_secmark(skb, ct) < 0)
543 goto nla_put_failure;
546 if (events & IPCT_RELATED &&
547 ctnetlink_dump_master(skb, ct) < 0)
548 goto nla_put_failure;
550 if (events & IPCT_NATSEQADJ &&
551 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
552 goto nla_put_failure;
555 #ifdef CONFIG_NF_CONNTRACK_MARK
556 if ((events & IPCT_MARK || ct->mark)
557 && ctnetlink_dump_mark(skb, ct) < 0)
558 goto nla_put_failure;
563 nfnetlink_send(skb, item->pid, group, item->report);
568 nlmsg_cancel(skb, nlh);
572 nfnetlink_set_err(0, group, -ENOBUFS);
575 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
577 static int ctnetlink_done(struct netlink_callback *cb)
580 nf_ct_put((struct nf_conn *)cb->args[1]);
585 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
587 struct nf_conn *ct, *last;
588 struct nf_conntrack_tuple_hash *h;
589 struct hlist_nulls_node *n;
590 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
591 u_int8_t l3proto = nfmsg->nfgen_family;
594 last = (struct nf_conn *)cb->args[1];
595 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
597 hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
599 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
601 ct = nf_ct_tuplehash_to_ctrack(h);
602 if (!atomic_inc_not_zero(&ct->ct_general.use))
604 /* Dump entries of a given L3 protocol number.
605 * If it is not specified, ie. l3proto == 0,
606 * then dump everything. */
607 if (l3proto && nf_ct_l3num(ct) != l3proto)
614 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
616 IPCTNL_MSG_CT_NEW, ct) < 0) {
617 cb->args[1] = (unsigned long)ct;
621 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
622 IPCTNL_MSG_CT_GET_CTRZERO) {
623 struct nf_conn_counter *acct;
625 acct = nf_conn_acct_find(ct);
627 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
646 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
648 struct nlattr *tb[CTA_IP_MAX+1];
649 struct nf_conntrack_l3proto *l3proto;
652 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
655 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
657 if (likely(l3proto->nlattr_to_tuple)) {
658 ret = nla_validate_nested(attr, CTA_IP_MAX,
659 l3proto->nla_policy);
661 ret = l3proto->nlattr_to_tuple(tb, tuple);
669 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
670 [CTA_PROTO_NUM] = { .type = NLA_U8 },
674 ctnetlink_parse_tuple_proto(struct nlattr *attr,
675 struct nf_conntrack_tuple *tuple)
677 struct nlattr *tb[CTA_PROTO_MAX+1];
678 struct nf_conntrack_l4proto *l4proto;
681 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
685 if (!tb[CTA_PROTO_NUM])
687 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
690 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
692 if (likely(l4proto->nlattr_to_tuple)) {
693 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
694 l4proto->nla_policy);
696 ret = l4proto->nlattr_to_tuple(tb, tuple);
705 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
706 enum ctattr_tuple type, u_int8_t l3num)
708 struct nlattr *tb[CTA_TUPLE_MAX+1];
711 memset(tuple, 0, sizeof(*tuple));
713 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
715 if (!tb[CTA_TUPLE_IP])
718 tuple->src.l3num = l3num;
720 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
724 if (!tb[CTA_TUPLE_PROTO])
727 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
731 /* orig and expect tuples get DIR_ORIGINAL */
732 if (type == CTA_TUPLE_REPLY)
733 tuple->dst.dir = IP_CT_DIR_REPLY;
735 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
741 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
743 struct nlattr *tb[CTA_HELP_MAX+1];
745 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
747 if (!tb[CTA_HELP_NAME])
750 *helper_name = nla_data(tb[CTA_HELP_NAME]);
755 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
756 [CTA_STATUS] = { .type = NLA_U32 },
757 [CTA_TIMEOUT] = { .type = NLA_U32 },
758 [CTA_MARK] = { .type = NLA_U32 },
759 [CTA_USE] = { .type = NLA_U32 },
760 [CTA_ID] = { .type = NLA_U32 },
764 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
765 struct nlmsghdr *nlh, struct nlattr *cda[])
767 struct nf_conntrack_tuple_hash *h;
768 struct nf_conntrack_tuple tuple;
770 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
771 u_int8_t u3 = nfmsg->nfgen_family;
774 if (cda[CTA_TUPLE_ORIG])
775 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
776 else if (cda[CTA_TUPLE_REPLY])
777 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
779 /* Flush the whole table */
780 nf_conntrack_flush_report(&init_net,
789 h = nf_conntrack_find_get(&init_net, &tuple);
793 ct = nf_ct_tuplehash_to_ctrack(h);
796 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
797 if (id != (u32)(unsigned long)ct) {
803 nf_conntrack_event_report(IPCT_DESTROY,
808 /* death_by_timeout would report the event again */
809 set_bit(IPS_DYING_BIT, &ct->status);
818 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
819 struct nlmsghdr *nlh, struct nlattr *cda[])
821 struct nf_conntrack_tuple_hash *h;
822 struct nf_conntrack_tuple tuple;
824 struct sk_buff *skb2 = NULL;
825 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
826 u_int8_t u3 = nfmsg->nfgen_family;
829 if (nlh->nlmsg_flags & NLM_F_DUMP)
830 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
833 if (cda[CTA_TUPLE_ORIG])
834 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
835 else if (cda[CTA_TUPLE_REPLY])
836 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
843 h = nf_conntrack_find_get(&init_net, &tuple);
847 ct = nf_ct_tuplehash_to_ctrack(h);
850 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
857 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
858 IPCTNL_MSG_CT_NEW, ct);
864 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
876 #ifdef CONFIG_NF_NAT_NEEDED
878 ctnetlink_parse_nat_setup(struct nf_conn *ct,
879 enum nf_nat_manip_type manip,
882 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
884 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
885 if (!parse_nat_setup) {
886 #ifdef CONFIG_MODULES
888 spin_unlock_bh(&nf_conntrack_lock);
890 if (request_module("nf-nat-ipv4") < 0) {
892 spin_lock_bh(&nf_conntrack_lock);
897 spin_lock_bh(&nf_conntrack_lock);
899 if (nfnetlink_parse_nat_setup_hook)
905 return parse_nat_setup(ct, manip, attr);
910 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
913 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
914 d = ct->status ^ status;
916 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
920 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
921 /* SEEN_REPLY bit can only be set */
924 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
925 /* ASSURED bit can only be set */
928 /* Be careful here, modifying NAT bits can screw up things,
929 * so don't let users modify them directly if they don't pass
931 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
936 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
938 #ifdef CONFIG_NF_NAT_NEEDED
941 if (cda[CTA_NAT_DST]) {
942 ret = ctnetlink_parse_nat_setup(ct,
948 if (cda[CTA_NAT_SRC]) {
949 ret = ctnetlink_parse_nat_setup(ct,
962 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
964 struct nf_conntrack_helper *helper;
965 struct nf_conn_help *help = nfct_help(ct);
966 char *helpname = NULL;
969 /* don't change helper of sibling connections */
973 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
977 if (!strcmp(helpname, "")) {
978 if (help && help->helper) {
979 /* we had a helper before ... */
980 nf_ct_remove_expectations(ct);
981 rcu_assign_pointer(help->helper, NULL);
987 helper = __nf_conntrack_helper_find_byname(helpname);
988 if (helper == NULL) {
989 #ifdef CONFIG_MODULES
990 spin_unlock_bh(&nf_conntrack_lock);
992 if (request_module("nfct-helper-%s", helpname) < 0) {
993 spin_lock_bh(&nf_conntrack_lock);
997 spin_lock_bh(&nf_conntrack_lock);
998 helper = __nf_conntrack_helper_find_byname(helpname);
1006 if (help->helper == helper)
1010 /* need to zero data of old helper */
1011 memset(&help->help, 0, sizeof(help->help));
1013 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1018 rcu_assign_pointer(help->helper, helper);
1024 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
1026 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1028 if (!del_timer(&ct->timeout))
1031 ct->timeout.expires = jiffies + timeout * HZ;
1032 add_timer(&ct->timeout);
1038 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1040 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1041 struct nf_conntrack_l4proto *l4proto;
1044 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1047 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1048 if (l4proto->from_nlattr)
1049 err = l4proto->from_nlattr(tb, ct);
1055 #ifdef CONFIG_NF_NAT_NEEDED
1057 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1059 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1061 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1063 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1066 natseq->correction_pos =
1067 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1069 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1072 natseq->offset_before =
1073 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1075 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1078 natseq->offset_after =
1079 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1085 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1088 struct nf_conn_nat *nat = nfct_nat(ct);
1093 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1094 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1095 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1099 ct->status |= IPS_SEQ_ADJUST;
1102 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1103 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1104 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1108 ct->status |= IPS_SEQ_ADJUST;
1116 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1120 /* only allow NAT changes and master assignation for new conntracks */
1121 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1124 if (cda[CTA_HELP]) {
1125 err = ctnetlink_change_helper(ct, cda);
1130 if (cda[CTA_TIMEOUT]) {
1131 err = ctnetlink_change_timeout(ct, cda);
1136 if (cda[CTA_STATUS]) {
1137 err = ctnetlink_change_status(ct, cda);
1142 if (cda[CTA_PROTOINFO]) {
1143 err = ctnetlink_change_protoinfo(ct, cda);
1148 #if defined(CONFIG_NF_CONNTRACK_MARK)
1150 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1153 #ifdef CONFIG_NF_NAT_NEEDED
1154 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1155 err = ctnetlink_change_nat_seq_adj(ct, cda);
1164 static struct nf_conn *
1165 ctnetlink_create_conntrack(struct nlattr *cda[],
1166 struct nf_conntrack_tuple *otuple,
1167 struct nf_conntrack_tuple *rtuple,
1172 struct nf_conntrack_helper *helper;
1174 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1176 return ERR_PTR(-ENOMEM);
1178 if (!cda[CTA_TIMEOUT])
1180 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1182 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1183 ct->status |= IPS_CONFIRMED;
1186 if (cda[CTA_HELP]) {
1187 char *helpname = NULL;
1189 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1193 helper = __nf_conntrack_helper_find_byname(helpname);
1194 if (helper == NULL) {
1196 #ifdef CONFIG_MODULES
1197 if (request_module("nfct-helper-%s", helpname) < 0) {
1203 helper = __nf_conntrack_helper_find_byname(helpname);
1213 struct nf_conn_help *help;
1215 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1221 /* not in hash table yet so not strictly necessary */
1222 rcu_assign_pointer(help->helper, helper);
1225 /* try an implicit helper assignation */
1226 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1231 if (cda[CTA_STATUS]) {
1232 err = ctnetlink_change_status(ct, cda);
1237 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1238 err = ctnetlink_change_nat(ct, cda);
1243 #ifdef CONFIG_NF_NAT_NEEDED
1244 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1245 err = ctnetlink_change_nat_seq_adj(ct, cda);
1251 if (cda[CTA_PROTOINFO]) {
1252 err = ctnetlink_change_protoinfo(ct, cda);
1257 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1259 #if defined(CONFIG_NF_CONNTRACK_MARK)
1261 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1264 /* setup master conntrack: this is a confirmed expectation */
1265 if (cda[CTA_TUPLE_MASTER]) {
1266 struct nf_conntrack_tuple master;
1267 struct nf_conntrack_tuple_hash *master_h;
1268 struct nf_conn *master_ct;
1270 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1274 master_h = nf_conntrack_find_get(&init_net, &master);
1275 if (master_h == NULL) {
1279 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1280 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1281 ct->master = master_ct;
1284 add_timer(&ct->timeout);
1285 nf_conntrack_hash_insert(ct);
1293 nf_conntrack_free(ct);
1294 return ERR_PTR(err);
1298 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1299 struct nlmsghdr *nlh, struct nlattr *cda[])
1301 struct nf_conntrack_tuple otuple, rtuple;
1302 struct nf_conntrack_tuple_hash *h = NULL;
1303 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1304 u_int8_t u3 = nfmsg->nfgen_family;
1307 if (cda[CTA_TUPLE_ORIG]) {
1308 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1313 if (cda[CTA_TUPLE_REPLY]) {
1314 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1319 spin_lock_bh(&nf_conntrack_lock);
1320 if (cda[CTA_TUPLE_ORIG])
1321 h = __nf_conntrack_find(&init_net, &otuple);
1322 else if (cda[CTA_TUPLE_REPLY])
1323 h = __nf_conntrack_find(&init_net, &rtuple);
1327 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1329 enum ip_conntrack_events events;
1331 ct = ctnetlink_create_conntrack(cda, &otuple,
1338 nf_conntrack_get(&ct->ct_general);
1339 spin_unlock_bh(&nf_conntrack_lock);
1340 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1341 events = IPCT_RELATED;
1345 nf_conntrack_event_report(IPCT_STATUS |
1350 ct, NETLINK_CB(skb).pid,
1354 spin_unlock_bh(&nf_conntrack_lock);
1358 /* implicit 'else' */
1360 /* We manipulate the conntrack inside the global conntrack table lock,
1361 * so there's no need to increase the refcount */
1363 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1364 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1366 err = ctnetlink_change_conntrack(ct, cda);
1368 nf_conntrack_get(&ct->ct_general);
1369 spin_unlock_bh(&nf_conntrack_lock);
1370 nf_conntrack_event_report(IPCT_STATUS |
1375 ct, NETLINK_CB(skb).pid,
1379 spin_unlock_bh(&nf_conntrack_lock);
1385 spin_unlock_bh(&nf_conntrack_lock);
1389 /***********************************************************************
1391 ***********************************************************************/
1394 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1395 const struct nf_conntrack_tuple *tuple,
1396 enum ctattr_expect type)
1398 struct nlattr *nest_parms;
1400 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1402 goto nla_put_failure;
1403 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1404 goto nla_put_failure;
1405 nla_nest_end(skb, nest_parms);
1414 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1415 const struct nf_conntrack_tuple *tuple,
1416 const struct nf_conntrack_tuple_mask *mask)
1419 struct nf_conntrack_l3proto *l3proto;
1420 struct nf_conntrack_l4proto *l4proto;
1421 struct nf_conntrack_tuple m;
1422 struct nlattr *nest_parms;
1424 memset(&m, 0xFF, sizeof(m));
1425 m.src.u.all = mask->src.u.all;
1426 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1428 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1430 goto nla_put_failure;
1432 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1433 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1435 if (unlikely(ret < 0))
1436 goto nla_put_failure;
1438 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1439 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1440 if (unlikely(ret < 0))
1441 goto nla_put_failure;
1443 nla_nest_end(skb, nest_parms);
1452 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1453 const struct nf_conntrack_expect *exp)
1455 struct nf_conn *master = exp->master;
1456 long timeout = (exp->timeout.expires - jiffies) / HZ;
1461 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1462 goto nla_put_failure;
1463 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1464 goto nla_put_failure;
1465 if (ctnetlink_exp_dump_tuple(skb,
1466 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1467 CTA_EXPECT_MASTER) < 0)
1468 goto nla_put_failure;
1470 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1471 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1480 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1481 int event, const struct nf_conntrack_expect *exp)
1483 struct nlmsghdr *nlh;
1484 struct nfgenmsg *nfmsg;
1485 unsigned int flags = pid ? NLM_F_MULTI : 0;
1487 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1488 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1492 nfmsg = nlmsg_data(nlh);
1493 nfmsg->nfgen_family = exp->tuple.src.l3num;
1494 nfmsg->version = NFNETLINK_V0;
1497 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1498 goto nla_put_failure;
1500 nlmsg_end(skb, nlh);
1505 nlmsg_cancel(skb, nlh);
1509 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1510 static int ctnetlink_expect_event(struct notifier_block *this,
1511 unsigned long events, void *ptr)
1513 struct nlmsghdr *nlh;
1514 struct nfgenmsg *nfmsg;
1515 struct nf_exp_event *item = (struct nf_exp_event *)ptr;
1516 struct nf_conntrack_expect *exp = item->exp;
1517 struct sk_buff *skb;
1521 if (events & IPEXP_NEW) {
1522 type = IPCTNL_MSG_EXP_NEW;
1523 flags = NLM_F_CREATE|NLM_F_EXCL;
1527 if (!item->report &&
1528 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1531 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1535 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1536 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1540 nfmsg = nlmsg_data(nlh);
1541 nfmsg->nfgen_family = exp->tuple.src.l3num;
1542 nfmsg->version = NFNETLINK_V0;
1546 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1547 goto nla_put_failure;
1550 nlmsg_end(skb, nlh);
1551 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report);
1556 nlmsg_cancel(skb, nlh);
1560 nfnetlink_set_err(0, 0, -ENOBUFS);
1564 static int ctnetlink_exp_done(struct netlink_callback *cb)
1567 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1572 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1574 struct net *net = &init_net;
1575 struct nf_conntrack_expect *exp, *last;
1576 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1577 struct hlist_node *n;
1578 u_int8_t l3proto = nfmsg->nfgen_family;
1581 last = (struct nf_conntrack_expect *)cb->args[1];
1582 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1584 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1586 if (l3proto && exp->tuple.src.l3num != l3proto)
1593 if (ctnetlink_exp_fill_info(skb,
1594 NETLINK_CB(cb->skb).pid,
1598 if (!atomic_inc_not_zero(&exp->use))
1600 cb->args[1] = (unsigned long)exp;
1612 nf_ct_expect_put(last);
1617 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1618 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1619 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1623 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1624 struct nlmsghdr *nlh, struct nlattr *cda[])
1626 struct nf_conntrack_tuple tuple;
1627 struct nf_conntrack_expect *exp;
1628 struct sk_buff *skb2;
1629 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1630 u_int8_t u3 = nfmsg->nfgen_family;
1633 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1634 return netlink_dump_start(ctnl, skb, nlh,
1635 ctnetlink_exp_dump_table,
1636 ctnetlink_exp_done);
1639 if (cda[CTA_EXPECT_MASTER])
1640 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1647 exp = nf_ct_expect_find_get(&init_net, &tuple);
1651 if (cda[CTA_EXPECT_ID]) {
1652 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1653 if (ntohl(id) != (u32)(unsigned long)exp) {
1654 nf_ct_expect_put(exp);
1660 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1665 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1666 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1671 nf_ct_expect_put(exp);
1673 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1678 nf_ct_expect_put(exp);
1683 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1684 struct nlmsghdr *nlh, struct nlattr *cda[])
1686 struct nf_conntrack_expect *exp;
1687 struct nf_conntrack_tuple tuple;
1688 struct nf_conntrack_helper *h;
1689 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1690 struct hlist_node *n, *next;
1691 u_int8_t u3 = nfmsg->nfgen_family;
1695 if (cda[CTA_EXPECT_TUPLE]) {
1696 /* delete a single expect by tuple */
1697 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1701 /* bump usage count to 2 */
1702 exp = nf_ct_expect_find_get(&init_net, &tuple);
1706 if (cda[CTA_EXPECT_ID]) {
1707 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1708 if (ntohl(id) != (u32)(unsigned long)exp) {
1709 nf_ct_expect_put(exp);
1714 /* after list removal, usage count == 1 */
1715 nf_ct_unexpect_related(exp);
1716 /* have to put what we 'get' above.
1717 * after this line usage count == 0 */
1718 nf_ct_expect_put(exp);
1719 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1720 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1721 struct nf_conn_help *m_help;
1723 /* delete all expectations for this helper */
1724 spin_lock_bh(&nf_conntrack_lock);
1725 h = __nf_conntrack_helper_find_byname(name);
1727 spin_unlock_bh(&nf_conntrack_lock);
1730 for (i = 0; i < nf_ct_expect_hsize; i++) {
1731 hlist_for_each_entry_safe(exp, n, next,
1732 &init_net.ct.expect_hash[i],
1734 m_help = nfct_help(exp->master);
1735 if (m_help->helper == h
1736 && del_timer(&exp->timeout)) {
1737 nf_ct_unlink_expect(exp);
1738 nf_ct_expect_put(exp);
1742 spin_unlock_bh(&nf_conntrack_lock);
1744 /* This basically means we have to flush everything*/
1745 spin_lock_bh(&nf_conntrack_lock);
1746 for (i = 0; i < nf_ct_expect_hsize; i++) {
1747 hlist_for_each_entry_safe(exp, n, next,
1748 &init_net.ct.expect_hash[i],
1750 if (del_timer(&exp->timeout)) {
1751 nf_ct_unlink_expect(exp);
1752 nf_ct_expect_put(exp);
1756 spin_unlock_bh(&nf_conntrack_lock);
1762 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1768 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
1770 struct nf_conntrack_tuple tuple, mask, master_tuple;
1771 struct nf_conntrack_tuple_hash *h = NULL;
1772 struct nf_conntrack_expect *exp;
1774 struct nf_conn_help *help;
1777 /* caller guarantees that those three CTA_EXPECT_* exist */
1778 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1781 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1784 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1788 /* Look for master conntrack of this expectation */
1789 h = nf_conntrack_find_get(&init_net, &master_tuple);
1792 ct = nf_ct_tuplehash_to_ctrack(h);
1793 help = nfct_help(ct);
1795 if (!help || !help->helper) {
1796 /* such conntrack hasn't got any helper, abort */
1801 exp = nf_ct_expect_alloc(ct);
1808 exp->expectfn = NULL;
1812 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1813 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1814 exp->mask.src.u.all = mask.src.u.all;
1816 err = nf_ct_expect_related_report(exp, pid, report);
1817 nf_ct_expect_put(exp);
1820 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1825 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1826 struct nlmsghdr *nlh, struct nlattr *cda[])
1828 struct nf_conntrack_tuple tuple;
1829 struct nf_conntrack_expect *exp;
1830 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1831 u_int8_t u3 = nfmsg->nfgen_family;
1834 if (!cda[CTA_EXPECT_TUPLE]
1835 || !cda[CTA_EXPECT_MASK]
1836 || !cda[CTA_EXPECT_MASTER])
1839 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1843 spin_lock_bh(&nf_conntrack_lock);
1844 exp = __nf_ct_expect_find(&init_net, &tuple);
1847 spin_unlock_bh(&nf_conntrack_lock);
1849 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1850 err = ctnetlink_create_expect(cda,
1852 NETLINK_CB(skb).pid,
1859 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1860 err = ctnetlink_change_expect(exp, cda);
1861 spin_unlock_bh(&nf_conntrack_lock);
1866 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1867 static struct notifier_block ctnl_notifier = {
1868 .notifier_call = ctnetlink_conntrack_event,
1871 static struct notifier_block ctnl_notifier_exp = {
1872 .notifier_call = ctnetlink_expect_event,
1876 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1877 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1878 .attr_count = CTA_MAX,
1879 .policy = ct_nla_policy },
1880 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1881 .attr_count = CTA_MAX,
1882 .policy = ct_nla_policy },
1883 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1884 .attr_count = CTA_MAX,
1885 .policy = ct_nla_policy },
1886 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1887 .attr_count = CTA_MAX,
1888 .policy = ct_nla_policy },
1891 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1892 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1893 .attr_count = CTA_EXPECT_MAX,
1894 .policy = exp_nla_policy },
1895 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1896 .attr_count = CTA_EXPECT_MAX,
1897 .policy = exp_nla_policy },
1898 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1899 .attr_count = CTA_EXPECT_MAX,
1900 .policy = exp_nla_policy },
1903 static const struct nfnetlink_subsystem ctnl_subsys = {
1904 .name = "conntrack",
1905 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1906 .cb_count = IPCTNL_MSG_MAX,
1910 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1911 .name = "conntrack_expect",
1912 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1913 .cb_count = IPCTNL_MSG_EXP_MAX,
1917 MODULE_ALIAS("ip_conntrack_netlink");
1918 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1919 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1921 static int __init ctnetlink_init(void)
1925 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1926 ret = nfnetlink_subsys_register(&ctnl_subsys);
1928 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1932 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1934 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1935 goto err_unreg_subsys;
1938 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1939 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1941 printk("ctnetlink_init: cannot register notifier.\n");
1942 goto err_unreg_exp_subsys;
1945 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1947 printk("ctnetlink_init: cannot expect register notifier.\n");
1948 goto err_unreg_notifier;
1954 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1956 nf_conntrack_unregister_notifier(&ctnl_notifier);
1957 err_unreg_exp_subsys:
1958 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1961 nfnetlink_subsys_unregister(&ctnl_subsys);
1966 static void __exit ctnetlink_exit(void)
1968 printk("ctnetlink: unregistering from nfnetlink.\n");
1970 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1971 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1972 nf_conntrack_unregister_notifier(&ctnl_notifier);
1975 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1976 nfnetlink_subsys_unregister(&ctnl_subsys);
1980 module_init(ctnetlink_init);
1981 module_exit(ctnetlink_exit);