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>
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 #include <net/netfilter/nf_conntrack_zones.h>
43 #ifdef CONFIG_NF_NAT_NEEDED
44 #include <net/netfilter/nf_nat_core.h>
45 #include <net/netfilter/nf_nat_protocol.h>
48 #include <linux/netfilter/nfnetlink.h>
49 #include <linux/netfilter/nfnetlink_conntrack.h>
51 MODULE_LICENSE("GPL");
53 static char __initdata version[] = "0.93";
56 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
57 const struct nf_conntrack_tuple *tuple,
58 struct nf_conntrack_l4proto *l4proto)
61 struct nlattr *nest_parms;
63 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
66 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
68 if (likely(l4proto->tuple_to_nlattr))
69 ret = l4proto->tuple_to_nlattr(skb, tuple);
71 nla_nest_end(skb, nest_parms);
80 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
81 const struct nf_conntrack_tuple *tuple,
82 struct nf_conntrack_l3proto *l3proto)
85 struct nlattr *nest_parms;
87 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
91 if (likely(l3proto->tuple_to_nlattr))
92 ret = l3proto->tuple_to_nlattr(skb, tuple);
94 nla_nest_end(skb, nest_parms);
103 ctnetlink_dump_tuples(struct sk_buff *skb,
104 const struct nf_conntrack_tuple *tuple)
107 struct nf_conntrack_l3proto *l3proto;
108 struct nf_conntrack_l4proto *l4proto;
110 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
111 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
113 if (unlikely(ret < 0))
116 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
117 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
123 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
125 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
133 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
135 long timeout = (ct->timeout.expires - jiffies) / HZ;
140 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
148 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
150 struct nf_conntrack_l4proto *l4proto;
151 struct nlattr *nest_proto;
154 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
155 if (!l4proto->to_nlattr)
158 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
160 goto nla_put_failure;
162 ret = l4proto->to_nlattr(skb, nest_proto, ct);
164 nla_nest_end(skb, nest_proto);
173 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
175 struct nlattr *nest_helper;
176 const struct nf_conn_help *help = nfct_help(ct);
177 struct nf_conntrack_helper *helper;
182 helper = rcu_dereference(help->helper);
186 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
188 goto nla_put_failure;
189 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
191 if (helper->to_nlattr)
192 helper->to_nlattr(skb, ct);
194 nla_nest_end(skb, nest_helper);
203 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
204 enum ip_conntrack_dir dir)
206 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
207 struct nlattr *nest_count;
208 const struct nf_conn_counter *acct;
210 acct = nf_conn_acct_find(ct);
214 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
216 goto nla_put_failure;
218 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
219 cpu_to_be64(acct[dir].packets));
220 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
221 cpu_to_be64(acct[dir].bytes));
223 nla_nest_end(skb, nest_count);
231 #ifdef CONFIG_NF_CONNTRACK_MARK
233 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
235 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
242 #define ctnetlink_dump_mark(a, b) (0)
245 #ifdef CONFIG_NF_CONNTRACK_SECMARK
247 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
249 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
256 #define ctnetlink_dump_secmark(a, b) (0)
259 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
262 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
264 struct nlattr *nest_parms;
266 if (!(ct->status & IPS_EXPECTED))
269 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
271 goto nla_put_failure;
272 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
273 goto nla_put_failure;
274 nla_nest_end(skb, nest_parms);
282 #ifdef CONFIG_NF_NAT_NEEDED
284 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
286 struct nlattr *nest_parms;
288 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
290 goto nla_put_failure;
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
293 htonl(natseq->correction_pos));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
295 htonl(natseq->offset_before));
296 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
297 htonl(natseq->offset_after));
299 nla_nest_end(skb, nest_parms);
308 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
310 struct nf_nat_seq *natseq;
311 struct nf_conn_nat *nat = nfct_nat(ct);
313 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
316 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
317 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
320 natseq = &nat->seq[IP_CT_DIR_REPLY];
321 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
327 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
331 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
333 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
341 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
343 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
351 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
352 int event, struct nf_conn *ct)
354 struct nlmsghdr *nlh;
355 struct nfgenmsg *nfmsg;
356 struct nlattr *nest_parms;
357 unsigned int flags = pid ? NLM_F_MULTI : 0;
359 event |= NFNL_SUBSYS_CTNETLINK << 8;
360 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
364 nfmsg = nlmsg_data(nlh);
365 nfmsg->nfgen_family = nf_ct_l3num(ct);
366 nfmsg->version = NFNETLINK_V0;
369 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
371 goto nla_put_failure;
372 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
373 goto nla_put_failure;
374 nla_nest_end(skb, nest_parms);
376 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
378 goto nla_put_failure;
379 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
380 goto nla_put_failure;
381 nla_nest_end(skb, nest_parms);
384 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
386 if (ctnetlink_dump_status(skb, ct) < 0 ||
387 ctnetlink_dump_timeout(skb, ct) < 0 ||
388 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
389 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
390 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
391 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
392 ctnetlink_dump_mark(skb, ct) < 0 ||
393 ctnetlink_dump_secmark(skb, ct) < 0 ||
394 ctnetlink_dump_id(skb, ct) < 0 ||
395 ctnetlink_dump_use(skb, ct) < 0 ||
396 ctnetlink_dump_master(skb, ct) < 0 ||
397 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
398 goto nla_put_failure;
405 nlmsg_cancel(skb, nlh);
409 #ifdef CONFIG_NF_CONNTRACK_EVENTS
411 ctnetlink_proto_size(const struct nf_conn *ct)
413 struct nf_conntrack_l3proto *l3proto;
414 struct nf_conntrack_l4proto *l4proto;
418 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
419 len += l3proto->nla_size;
421 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
422 len += l4proto->nla_size;
429 ctnetlink_nlmsg_size(const struct nf_conn *ct)
431 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
432 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
433 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
434 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
435 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
436 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
437 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
438 #ifdef CONFIG_NF_CT_ACCT
439 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
440 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
441 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
443 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
444 + nla_total_size(0) /* CTA_PROTOINFO */
445 + nla_total_size(0) /* CTA_HELP */
446 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
447 #ifdef CONFIG_NF_CONNTRACK_SECMARK
448 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
450 #ifdef CONFIG_NF_NAT_NEEDED
451 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
452 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
454 #ifdef CONFIG_NF_CONNTRACK_MARK
455 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
457 + ctnetlink_proto_size(ct)
462 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
465 struct nlmsghdr *nlh;
466 struct nfgenmsg *nfmsg;
467 struct nlattr *nest_parms;
468 struct nf_conn *ct = item->ct;
471 unsigned int flags = 0, group;
474 /* ignore our fake conntrack entry */
475 if (ct == &nf_conntrack_untracked)
478 if (events & (1 << IPCT_DESTROY)) {
479 type = IPCTNL_MSG_CT_DELETE;
480 group = NFNLGRP_CONNTRACK_DESTROY;
481 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
482 type = IPCTNL_MSG_CT_NEW;
483 flags = NLM_F_CREATE|NLM_F_EXCL;
484 group = NFNLGRP_CONNTRACK_NEW;
486 type = IPCTNL_MSG_CT_NEW;
487 group = NFNLGRP_CONNTRACK_UPDATE;
492 if (!item->report && !nfnetlink_has_listeners(net, group))
495 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
499 type |= NFNL_SUBSYS_CTNETLINK << 8;
500 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
504 nfmsg = nlmsg_data(nlh);
505 nfmsg->nfgen_family = nf_ct_l3num(ct);
506 nfmsg->version = NFNETLINK_V0;
510 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
512 goto nla_put_failure;
513 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
514 goto nla_put_failure;
515 nla_nest_end(skb, nest_parms);
517 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
519 goto nla_put_failure;
520 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
521 goto nla_put_failure;
522 nla_nest_end(skb, nest_parms);
525 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
527 if (ctnetlink_dump_id(skb, ct) < 0)
528 goto nla_put_failure;
530 if (ctnetlink_dump_status(skb, ct) < 0)
531 goto nla_put_failure;
533 if (events & (1 << IPCT_DESTROY)) {
534 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
535 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
536 goto nla_put_failure;
538 if (ctnetlink_dump_timeout(skb, ct) < 0)
539 goto nla_put_failure;
541 if (events & (1 << IPCT_PROTOINFO)
542 && ctnetlink_dump_protoinfo(skb, ct) < 0)
543 goto nla_put_failure;
545 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
546 && ctnetlink_dump_helpinfo(skb, ct) < 0)
547 goto nla_put_failure;
549 #ifdef CONFIG_NF_CONNTRACK_SECMARK
550 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
551 && ctnetlink_dump_secmark(skb, ct) < 0)
552 goto nla_put_failure;
555 if (events & (1 << IPCT_RELATED) &&
556 ctnetlink_dump_master(skb, ct) < 0)
557 goto nla_put_failure;
559 if (events & (1 << IPCT_NATSEQADJ) &&
560 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
561 goto nla_put_failure;
564 #ifdef CONFIG_NF_CONNTRACK_MARK
565 if ((events & (1 << IPCT_MARK) || ct->mark)
566 && ctnetlink_dump_mark(skb, ct) < 0)
567 goto nla_put_failure;
572 err = nfnetlink_send(skb, net, item->pid, group, item->report,
574 if (err == -ENOBUFS || err == -EAGAIN)
581 nlmsg_cancel(skb, nlh);
585 nfnetlink_set_err(net, 0, group, -ENOBUFS);
588 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
590 static int ctnetlink_done(struct netlink_callback *cb)
593 nf_ct_put((struct nf_conn *)cb->args[1]);
598 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
600 struct net *net = sock_net(skb->sk);
601 struct nf_conn *ct, *last;
602 struct nf_conntrack_tuple_hash *h;
603 struct hlist_nulls_node *n;
604 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
605 u_int8_t l3proto = nfmsg->nfgen_family;
608 last = (struct nf_conn *)cb->args[1];
609 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
611 hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[cb->args[0]],
613 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
615 ct = nf_ct_tuplehash_to_ctrack(h);
616 if (!atomic_inc_not_zero(&ct->ct_general.use))
618 /* Dump entries of a given L3 protocol number.
619 * If it is not specified, ie. l3proto == 0,
620 * then dump everything. */
621 if (l3proto && nf_ct_l3num(ct) != l3proto)
628 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
630 IPCTNL_MSG_CT_NEW, ct) < 0) {
631 cb->args[1] = (unsigned long)ct;
635 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
636 IPCTNL_MSG_CT_GET_CTRZERO) {
637 struct nf_conn_counter *acct;
639 acct = nf_conn_acct_find(ct);
641 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
660 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
662 struct nlattr *tb[CTA_IP_MAX+1];
663 struct nf_conntrack_l3proto *l3proto;
666 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
669 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
671 if (likely(l3proto->nlattr_to_tuple)) {
672 ret = nla_validate_nested(attr, CTA_IP_MAX,
673 l3proto->nla_policy);
675 ret = l3proto->nlattr_to_tuple(tb, tuple);
683 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
684 [CTA_PROTO_NUM] = { .type = NLA_U8 },
688 ctnetlink_parse_tuple_proto(struct nlattr *attr,
689 struct nf_conntrack_tuple *tuple)
691 struct nlattr *tb[CTA_PROTO_MAX+1];
692 struct nf_conntrack_l4proto *l4proto;
695 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
699 if (!tb[CTA_PROTO_NUM])
701 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
704 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
706 if (likely(l4proto->nlattr_to_tuple)) {
707 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
708 l4proto->nla_policy);
710 ret = l4proto->nlattr_to_tuple(tb, tuple);
718 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
719 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
720 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
724 ctnetlink_parse_tuple(const struct nlattr * const cda[],
725 struct nf_conntrack_tuple *tuple,
726 enum ctattr_tuple type, u_int8_t l3num)
728 struct nlattr *tb[CTA_TUPLE_MAX+1];
731 memset(tuple, 0, sizeof(*tuple));
733 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
735 if (!tb[CTA_TUPLE_IP])
738 tuple->src.l3num = l3num;
740 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
744 if (!tb[CTA_TUPLE_PROTO])
747 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
751 /* orig and expect tuples get DIR_ORIGINAL */
752 if (type == CTA_TUPLE_REPLY)
753 tuple->dst.dir = IP_CT_DIR_REPLY;
755 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
761 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
764 #ifdef CONFIG_NF_CONNTRACK_ZONES
765 *zone = ntohs(nla_get_be16(attr));
775 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
776 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
780 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
782 struct nlattr *tb[CTA_HELP_MAX+1];
784 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
786 if (!tb[CTA_HELP_NAME])
789 *helper_name = nla_data(tb[CTA_HELP_NAME]);
794 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
795 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
796 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
797 [CTA_STATUS] = { .type = NLA_U32 },
798 [CTA_PROTOINFO] = { .type = NLA_NESTED },
799 [CTA_HELP] = { .type = NLA_NESTED },
800 [CTA_NAT_SRC] = { .type = NLA_NESTED },
801 [CTA_TIMEOUT] = { .type = NLA_U32 },
802 [CTA_MARK] = { .type = NLA_U32 },
803 [CTA_ID] = { .type = NLA_U32 },
804 [CTA_NAT_DST] = { .type = NLA_NESTED },
805 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
806 [CTA_ZONE] = { .type = NLA_U16 },
810 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
811 const struct nlmsghdr *nlh,
812 const struct nlattr * const cda[])
814 struct net *net = sock_net(ctnl);
815 struct nf_conntrack_tuple_hash *h;
816 struct nf_conntrack_tuple tuple;
818 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
819 u_int8_t u3 = nfmsg->nfgen_family;
823 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
827 if (cda[CTA_TUPLE_ORIG])
828 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
829 else if (cda[CTA_TUPLE_REPLY])
830 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
832 /* Flush the whole table */
833 nf_conntrack_flush_report(net,
842 h = nf_conntrack_find_get(net, zone, &tuple);
846 ct = nf_ct_tuplehash_to_ctrack(h);
849 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
850 if (id != (u32)(unsigned long)ct) {
856 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
858 nlmsg_report(nlh)) < 0) {
859 nf_ct_delete_from_lists(ct);
860 /* we failed to report the event, try later */
861 nf_ct_insert_dying_list(ct);
866 /* death_by_timeout would report the event again */
867 set_bit(IPS_DYING_BIT, &ct->status);
876 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
877 const struct nlmsghdr *nlh,
878 const struct nlattr * const cda[])
880 struct net *net = sock_net(ctnl);
881 struct nf_conntrack_tuple_hash *h;
882 struct nf_conntrack_tuple tuple;
884 struct sk_buff *skb2 = NULL;
885 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
886 u_int8_t u3 = nfmsg->nfgen_family;
890 if (nlh->nlmsg_flags & NLM_F_DUMP)
891 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
894 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
898 if (cda[CTA_TUPLE_ORIG])
899 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
900 else if (cda[CTA_TUPLE_REPLY])
901 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
908 h = nf_conntrack_find_get(net, zone, &tuple);
912 ct = nf_ct_tuplehash_to_ctrack(h);
915 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
922 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
923 IPCTNL_MSG_CT_NEW, ct);
929 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
941 #ifdef CONFIG_NF_NAT_NEEDED
943 ctnetlink_parse_nat_setup(struct nf_conn *ct,
944 enum nf_nat_manip_type manip,
945 const struct nlattr *attr)
947 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
949 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
950 if (!parse_nat_setup) {
951 #ifdef CONFIG_MODULES
953 spin_unlock_bh(&nf_conntrack_lock);
955 if (request_module("nf-nat-ipv4") < 0) {
957 spin_lock_bh(&nf_conntrack_lock);
962 spin_lock_bh(&nf_conntrack_lock);
964 if (nfnetlink_parse_nat_setup_hook)
970 return parse_nat_setup(ct, manip, attr);
975 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
978 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
979 d = ct->status ^ status;
981 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
985 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
986 /* SEEN_REPLY bit can only be set */
989 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
990 /* ASSURED bit can only be set */
993 /* Be careful here, modifying NAT bits can screw up things,
994 * so don't let users modify them directly if they don't pass
996 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1001 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1003 #ifdef CONFIG_NF_NAT_NEEDED
1006 if (cda[CTA_NAT_DST]) {
1007 ret = ctnetlink_parse_nat_setup(ct,
1013 if (cda[CTA_NAT_SRC]) {
1014 ret = ctnetlink_parse_nat_setup(ct,
1027 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1029 struct nf_conntrack_helper *helper;
1030 struct nf_conn_help *help = nfct_help(ct);
1031 char *helpname = NULL;
1034 /* don't change helper of sibling connections */
1038 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1042 if (!strcmp(helpname, "")) {
1043 if (help && help->helper) {
1044 /* we had a helper before ... */
1045 nf_ct_remove_expectations(ct);
1046 rcu_assign_pointer(help->helper, NULL);
1052 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1053 nf_ct_protonum(ct));
1054 if (helper == NULL) {
1055 #ifdef CONFIG_MODULES
1056 spin_unlock_bh(&nf_conntrack_lock);
1058 if (request_module("nfct-helper-%s", helpname) < 0) {
1059 spin_lock_bh(&nf_conntrack_lock);
1063 spin_lock_bh(&nf_conntrack_lock);
1064 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1065 nf_ct_protonum(ct));
1073 if (help->helper == helper)
1077 /* need to zero data of old helper */
1078 memset(&help->help, 0, sizeof(help->help));
1080 /* we cannot set a helper for an existing conntrack */
1084 rcu_assign_pointer(help->helper, helper);
1090 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1092 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1094 if (!del_timer(&ct->timeout))
1097 ct->timeout.expires = jiffies + timeout * HZ;
1098 add_timer(&ct->timeout);
1103 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1104 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1105 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1106 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1110 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1112 const struct nlattr *attr = cda[CTA_PROTOINFO];
1113 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1114 struct nf_conntrack_l4proto *l4proto;
1117 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1120 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1121 if (l4proto->from_nlattr)
1122 err = l4proto->from_nlattr(tb, ct);
1128 #ifdef CONFIG_NF_NAT_NEEDED
1129 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1130 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1131 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1132 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1136 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1138 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1140 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1142 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1145 natseq->correction_pos =
1146 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1148 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1151 natseq->offset_before =
1152 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1154 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1157 natseq->offset_after =
1158 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1164 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1165 const struct nlattr * const cda[])
1168 struct nf_conn_nat *nat = nfct_nat(ct);
1173 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1174 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1175 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1179 ct->status |= IPS_SEQ_ADJUST;
1182 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1183 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1184 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1188 ct->status |= IPS_SEQ_ADJUST;
1196 ctnetlink_change_conntrack(struct nf_conn *ct,
1197 const struct nlattr * const cda[])
1201 /* only allow NAT changes and master assignation for new conntracks */
1202 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1205 if (cda[CTA_HELP]) {
1206 err = ctnetlink_change_helper(ct, cda);
1211 if (cda[CTA_TIMEOUT]) {
1212 err = ctnetlink_change_timeout(ct, cda);
1217 if (cda[CTA_STATUS]) {
1218 err = ctnetlink_change_status(ct, cda);
1223 if (cda[CTA_PROTOINFO]) {
1224 err = ctnetlink_change_protoinfo(ct, cda);
1229 #if defined(CONFIG_NF_CONNTRACK_MARK)
1231 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1234 #ifdef CONFIG_NF_NAT_NEEDED
1235 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1236 err = ctnetlink_change_nat_seq_adj(ct, cda);
1245 static struct nf_conn *
1246 ctnetlink_create_conntrack(struct net *net, u16 zone,
1247 const struct nlattr * const cda[],
1248 struct nf_conntrack_tuple *otuple,
1249 struct nf_conntrack_tuple *rtuple,
1254 struct nf_conntrack_helper *helper;
1256 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1258 return ERR_PTR(-ENOMEM);
1260 if (!cda[CTA_TIMEOUT])
1262 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1264 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1267 if (cda[CTA_HELP]) {
1268 char *helpname = NULL;
1270 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1274 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1275 nf_ct_protonum(ct));
1276 if (helper == NULL) {
1278 #ifdef CONFIG_MODULES
1279 if (request_module("nfct-helper-%s", helpname) < 0) {
1285 helper = __nf_conntrack_helper_find(helpname,
1287 nf_ct_protonum(ct));
1297 struct nf_conn_help *help;
1299 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1305 /* not in hash table yet so not strictly necessary */
1306 rcu_assign_pointer(help->helper, helper);
1309 /* try an implicit helper assignation */
1310 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1315 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1316 err = ctnetlink_change_nat(ct, cda);
1321 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1322 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1323 /* we must add conntrack extensions before confirmation. */
1324 ct->status |= IPS_CONFIRMED;
1326 if (cda[CTA_STATUS]) {
1327 err = ctnetlink_change_status(ct, cda);
1332 #ifdef CONFIG_NF_NAT_NEEDED
1333 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1334 err = ctnetlink_change_nat_seq_adj(ct, cda);
1340 if (cda[CTA_PROTOINFO]) {
1341 err = ctnetlink_change_protoinfo(ct, cda);
1346 #if defined(CONFIG_NF_CONNTRACK_MARK)
1348 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1351 /* setup master conntrack: this is a confirmed expectation */
1352 if (cda[CTA_TUPLE_MASTER]) {
1353 struct nf_conntrack_tuple master;
1354 struct nf_conntrack_tuple_hash *master_h;
1355 struct nf_conn *master_ct;
1357 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1361 master_h = nf_conntrack_find_get(net, zone, &master);
1362 if (master_h == NULL) {
1366 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1367 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1368 ct->master = master_ct;
1371 add_timer(&ct->timeout);
1372 nf_conntrack_hash_insert(ct);
1380 nf_conntrack_free(ct);
1381 return ERR_PTR(err);
1385 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1386 const struct nlmsghdr *nlh,
1387 const struct nlattr * const cda[])
1389 struct net *net = sock_net(ctnl);
1390 struct nf_conntrack_tuple otuple, rtuple;
1391 struct nf_conntrack_tuple_hash *h = NULL;
1392 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1393 u_int8_t u3 = nfmsg->nfgen_family;
1397 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1401 if (cda[CTA_TUPLE_ORIG]) {
1402 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1407 if (cda[CTA_TUPLE_REPLY]) {
1408 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1413 spin_lock_bh(&nf_conntrack_lock);
1414 if (cda[CTA_TUPLE_ORIG])
1415 h = __nf_conntrack_find(net, zone, &otuple);
1416 else if (cda[CTA_TUPLE_REPLY])
1417 h = __nf_conntrack_find(net, zone, &rtuple);
1421 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1423 enum ip_conntrack_events events;
1425 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1432 nf_conntrack_get(&ct->ct_general);
1433 spin_unlock_bh(&nf_conntrack_lock);
1434 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1435 events = IPCT_RELATED;
1439 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1440 (1 << IPCT_ASSURED) |
1441 (1 << IPCT_HELPER) |
1442 (1 << IPCT_PROTOINFO) |
1443 (1 << IPCT_NATSEQADJ) |
1444 (1 << IPCT_MARK) | events,
1445 ct, NETLINK_CB(skb).pid,
1449 spin_unlock_bh(&nf_conntrack_lock);
1453 /* implicit 'else' */
1455 /* We manipulate the conntrack inside the global conntrack table lock,
1456 * so there's no need to increase the refcount */
1458 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1459 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1461 err = ctnetlink_change_conntrack(ct, cda);
1463 nf_conntrack_get(&ct->ct_general);
1464 spin_unlock_bh(&nf_conntrack_lock);
1465 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1466 (1 << IPCT_ASSURED) |
1467 (1 << IPCT_HELPER) |
1468 (1 << IPCT_PROTOINFO) |
1469 (1 << IPCT_NATSEQADJ) |
1471 ct, NETLINK_CB(skb).pid,
1475 spin_unlock_bh(&nf_conntrack_lock);
1481 spin_unlock_bh(&nf_conntrack_lock);
1485 /***********************************************************************
1487 ***********************************************************************/
1490 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1491 const struct nf_conntrack_tuple *tuple,
1492 enum ctattr_expect type)
1494 struct nlattr *nest_parms;
1496 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1498 goto nla_put_failure;
1499 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1500 goto nla_put_failure;
1501 nla_nest_end(skb, nest_parms);
1510 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1511 const struct nf_conntrack_tuple *tuple,
1512 const struct nf_conntrack_tuple_mask *mask)
1515 struct nf_conntrack_l3proto *l3proto;
1516 struct nf_conntrack_l4proto *l4proto;
1517 struct nf_conntrack_tuple m;
1518 struct nlattr *nest_parms;
1520 memset(&m, 0xFF, sizeof(m));
1521 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1522 m.src.u.all = mask->src.u.all;
1523 m.dst.protonum = tuple->dst.protonum;
1525 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1527 goto nla_put_failure;
1529 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1530 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1532 if (unlikely(ret < 0))
1533 goto nla_put_failure;
1535 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1536 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1537 if (unlikely(ret < 0))
1538 goto nla_put_failure;
1540 nla_nest_end(skb, nest_parms);
1549 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1550 const struct nf_conntrack_expect *exp)
1552 struct nf_conn *master = exp->master;
1553 struct nf_conntrack_helper *helper;
1554 long timeout = (exp->timeout.expires - jiffies) / HZ;
1559 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1560 goto nla_put_failure;
1561 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1562 goto nla_put_failure;
1563 if (ctnetlink_exp_dump_tuple(skb,
1564 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1565 CTA_EXPECT_MASTER) < 0)
1566 goto nla_put_failure;
1568 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1569 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1570 helper = rcu_dereference(nfct_help(master)->helper);
1572 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1581 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1582 int event, const struct nf_conntrack_expect *exp)
1584 struct nlmsghdr *nlh;
1585 struct nfgenmsg *nfmsg;
1586 unsigned int flags = pid ? NLM_F_MULTI : 0;
1588 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1589 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1593 nfmsg = nlmsg_data(nlh);
1594 nfmsg->nfgen_family = exp->tuple.src.l3num;
1595 nfmsg->version = NFNETLINK_V0;
1598 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1599 goto nla_put_failure;
1601 nlmsg_end(skb, nlh);
1606 nlmsg_cancel(skb, nlh);
1610 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1612 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1614 struct nf_conntrack_expect *exp = item->exp;
1615 struct net *net = nf_ct_exp_net(exp);
1616 struct nlmsghdr *nlh;
1617 struct nfgenmsg *nfmsg;
1618 struct sk_buff *skb;
1622 if (events & (1 << IPEXP_NEW)) {
1623 type = IPCTNL_MSG_EXP_NEW;
1624 flags = NLM_F_CREATE|NLM_F_EXCL;
1628 if (!item->report &&
1629 !nfnetlink_has_listeners(net, NFNLGRP_CONNTRACK_EXP_NEW))
1632 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1636 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1637 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1641 nfmsg = nlmsg_data(nlh);
1642 nfmsg->nfgen_family = exp->tuple.src.l3num;
1643 nfmsg->version = NFNETLINK_V0;
1647 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1648 goto nla_put_failure;
1651 nlmsg_end(skb, nlh);
1652 nfnetlink_send(skb, net, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
1653 item->report, GFP_ATOMIC);
1658 nlmsg_cancel(skb, nlh);
1662 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1666 static int ctnetlink_exp_done(struct netlink_callback *cb)
1669 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1674 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1676 struct net *net = sock_net(skb->sk);
1677 struct nf_conntrack_expect *exp, *last;
1678 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1679 struct hlist_node *n;
1680 u_int8_t l3proto = nfmsg->nfgen_family;
1683 last = (struct nf_conntrack_expect *)cb->args[1];
1684 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1686 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1688 if (l3proto && exp->tuple.src.l3num != l3proto)
1695 if (ctnetlink_exp_fill_info(skb,
1696 NETLINK_CB(cb->skb).pid,
1700 if (!atomic_inc_not_zero(&exp->use))
1702 cb->args[1] = (unsigned long)exp;
1714 nf_ct_expect_put(last);
1719 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1720 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1721 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1722 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
1723 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1724 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1725 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
1729 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1730 const struct nlmsghdr *nlh,
1731 const struct nlattr * const cda[])
1733 struct net *net = sock_net(ctnl);
1734 struct nf_conntrack_tuple tuple;
1735 struct nf_conntrack_expect *exp;
1736 struct sk_buff *skb2;
1737 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1738 u_int8_t u3 = nfmsg->nfgen_family;
1742 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1743 return netlink_dump_start(ctnl, skb, nlh,
1744 ctnetlink_exp_dump_table,
1745 ctnetlink_exp_done);
1748 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1752 if (cda[CTA_EXPECT_MASTER])
1753 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1760 exp = nf_ct_expect_find_get(net, zone, &tuple);
1764 if (cda[CTA_EXPECT_ID]) {
1765 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1766 if (ntohl(id) != (u32)(unsigned long)exp) {
1767 nf_ct_expect_put(exp);
1773 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1778 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1779 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1784 nf_ct_expect_put(exp);
1786 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1791 nf_ct_expect_put(exp);
1796 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1797 const struct nlmsghdr *nlh,
1798 const struct nlattr * const cda[])
1800 struct net *net = sock_net(ctnl);
1801 struct nf_conntrack_expect *exp;
1802 struct nf_conntrack_tuple tuple;
1803 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1804 struct hlist_node *n, *next;
1805 u_int8_t u3 = nfmsg->nfgen_family;
1810 if (cda[CTA_EXPECT_TUPLE]) {
1811 /* delete a single expect by tuple */
1812 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1816 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1820 /* bump usage count to 2 */
1821 exp = nf_ct_expect_find_get(net, zone, &tuple);
1825 if (cda[CTA_EXPECT_ID]) {
1826 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1827 if (ntohl(id) != (u32)(unsigned long)exp) {
1828 nf_ct_expect_put(exp);
1833 /* after list removal, usage count == 1 */
1834 nf_ct_unexpect_related(exp);
1835 /* have to put what we 'get' above.
1836 * after this line usage count == 0 */
1837 nf_ct_expect_put(exp);
1838 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1839 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1840 struct nf_conn_help *m_help;
1842 /* delete all expectations for this helper */
1843 spin_lock_bh(&nf_conntrack_lock);
1844 for (i = 0; i < nf_ct_expect_hsize; i++) {
1845 hlist_for_each_entry_safe(exp, n, next,
1846 &net->ct.expect_hash[i],
1848 m_help = nfct_help(exp->master);
1849 if (!strcmp(m_help->helper->name, name) &&
1850 del_timer(&exp->timeout)) {
1851 nf_ct_unlink_expect(exp);
1852 nf_ct_expect_put(exp);
1856 spin_unlock_bh(&nf_conntrack_lock);
1858 /* This basically means we have to flush everything*/
1859 spin_lock_bh(&nf_conntrack_lock);
1860 for (i = 0; i < nf_ct_expect_hsize; i++) {
1861 hlist_for_each_entry_safe(exp, n, next,
1862 &net->ct.expect_hash[i],
1864 if (del_timer(&exp->timeout)) {
1865 nf_ct_unlink_expect(exp);
1866 nf_ct_expect_put(exp);
1870 spin_unlock_bh(&nf_conntrack_lock);
1876 ctnetlink_change_expect(struct nf_conntrack_expect *x,
1877 const struct nlattr * const cda[])
1883 ctnetlink_create_expect(struct net *net, u16 zone,
1884 const struct nlattr * const cda[],
1886 u32 pid, int report)
1888 struct nf_conntrack_tuple tuple, mask, master_tuple;
1889 struct nf_conntrack_tuple_hash *h = NULL;
1890 struct nf_conntrack_expect *exp;
1892 struct nf_conn_help *help;
1895 /* caller guarantees that those three CTA_EXPECT_* exist */
1896 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1899 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1902 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1906 /* Look for master conntrack of this expectation */
1907 h = nf_conntrack_find_get(net, zone, &master_tuple);
1910 ct = nf_ct_tuplehash_to_ctrack(h);
1911 help = nfct_help(ct);
1913 if (!help || !help->helper) {
1914 /* such conntrack hasn't got any helper, abort */
1919 exp = nf_ct_expect_alloc(ct);
1926 exp->expectfn = NULL;
1930 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1931 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1932 exp->mask.src.u.all = mask.src.u.all;
1934 err = nf_ct_expect_related_report(exp, pid, report);
1935 nf_ct_expect_put(exp);
1938 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1943 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1944 const struct nlmsghdr *nlh,
1945 const struct nlattr * const cda[])
1947 struct net *net = sock_net(ctnl);
1948 struct nf_conntrack_tuple tuple;
1949 struct nf_conntrack_expect *exp;
1950 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1951 u_int8_t u3 = nfmsg->nfgen_family;
1955 if (!cda[CTA_EXPECT_TUPLE]
1956 || !cda[CTA_EXPECT_MASK]
1957 || !cda[CTA_EXPECT_MASTER])
1960 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1964 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1968 spin_lock_bh(&nf_conntrack_lock);
1969 exp = __nf_ct_expect_find(net, zone, &tuple);
1972 spin_unlock_bh(&nf_conntrack_lock);
1974 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1975 err = ctnetlink_create_expect(net, zone, cda,
1977 NETLINK_CB(skb).pid,
1984 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1985 err = ctnetlink_change_expect(exp, cda);
1986 spin_unlock_bh(&nf_conntrack_lock);
1991 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1992 static struct nf_ct_event_notifier ctnl_notifier = {
1993 .fcn = ctnetlink_conntrack_event,
1996 static struct nf_exp_event_notifier ctnl_notifier_exp = {
1997 .fcn = ctnetlink_expect_event,
2001 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2002 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2003 .attr_count = CTA_MAX,
2004 .policy = ct_nla_policy },
2005 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2006 .attr_count = CTA_MAX,
2007 .policy = ct_nla_policy },
2008 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2009 .attr_count = CTA_MAX,
2010 .policy = ct_nla_policy },
2011 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2012 .attr_count = CTA_MAX,
2013 .policy = ct_nla_policy },
2016 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2017 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2018 .attr_count = CTA_EXPECT_MAX,
2019 .policy = exp_nla_policy },
2020 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2021 .attr_count = CTA_EXPECT_MAX,
2022 .policy = exp_nla_policy },
2023 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2024 .attr_count = CTA_EXPECT_MAX,
2025 .policy = exp_nla_policy },
2028 static const struct nfnetlink_subsystem ctnl_subsys = {
2029 .name = "conntrack",
2030 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2031 .cb_count = IPCTNL_MSG_MAX,
2035 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2036 .name = "conntrack_expect",
2037 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2038 .cb_count = IPCTNL_MSG_EXP_MAX,
2042 MODULE_ALIAS("ip_conntrack_netlink");
2043 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2044 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2046 static int __init ctnetlink_init(void)
2050 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
2051 ret = nfnetlink_subsys_register(&ctnl_subsys);
2053 printk("ctnetlink_init: cannot register with nfnetlink.\n");
2057 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2059 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
2060 goto err_unreg_subsys;
2063 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2064 ret = nf_conntrack_register_notifier(&ctnl_notifier);
2066 printk("ctnetlink_init: cannot register notifier.\n");
2067 goto err_unreg_exp_subsys;
2070 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
2072 printk("ctnetlink_init: cannot expect register notifier.\n");
2073 goto err_unreg_notifier;
2079 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2081 nf_conntrack_unregister_notifier(&ctnl_notifier);
2082 err_unreg_exp_subsys:
2083 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2086 nfnetlink_subsys_unregister(&ctnl_subsys);
2091 static void __exit ctnetlink_exit(void)
2093 printk("ctnetlink: unregistering from nfnetlink.\n");
2095 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2096 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
2097 nf_conntrack_unregister_notifier(&ctnl_notifier);
2100 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2101 nfnetlink_subsys_unregister(&ctnl_subsys);
2105 module_init(ctnetlink_init);
2106 module_exit(ctnetlink_exit);