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)));
349 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
352 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
353 int event, const struct nf_conn *ct)
355 struct nlmsghdr *nlh;
356 struct nfgenmsg *nfmsg;
357 struct nlattr *nest_parms;
358 unsigned char *b = skb_tail_pointer(skb);
360 event |= NFNL_SUBSYS_CTNETLINK << 8;
361 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
362 nfmsg = NLMSG_DATA(nlh);
364 nlh->nlmsg_flags = pid ? NLM_F_MULTI : 0;
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, 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, tuple(ct, IP_CT_DIR_REPLY)) < 0)
380 goto nla_put_failure;
381 nla_nest_end(skb, nest_parms);
383 if (ctnetlink_dump_status(skb, ct) < 0 ||
384 ctnetlink_dump_timeout(skb, ct) < 0 ||
385 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
386 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
387 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
388 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
389 ctnetlink_dump_mark(skb, ct) < 0 ||
390 ctnetlink_dump_secmark(skb, ct) < 0 ||
391 ctnetlink_dump_id(skb, ct) < 0 ||
392 ctnetlink_dump_use(skb, ct) < 0 ||
393 ctnetlink_dump_master(skb, ct) < 0 ||
394 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
395 goto nla_put_failure;
397 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
406 #ifdef CONFIG_NF_CONNTRACK_EVENTS
408 * The general structure of a ctnetlink event is
411 * <l3/l4-proto-attributes>
413 * <l3/l4-proto-attributes>
417 * <l4-proto-attributes>
419 * <l3/l4-proto-attributes>
421 * Therefore the formular is
423 * size = sizeof(headers) + sizeof(generic_nlas) + 3 * sizeof(tuple_nlas)
424 * + sizeof(protoinfo_nlas)
426 static struct sk_buff *
427 ctnetlink_alloc_skb(const struct nf_conntrack_tuple *tuple, gfp_t gfp)
429 struct nf_conntrack_l3proto *l3proto;
430 struct nf_conntrack_l4proto *l4proto;
433 #define NLA_TYPE_SIZE(type) nla_total_size(sizeof(type))
435 /* proto independant part */
436 len = NLMSG_SPACE(sizeof(struct nfgenmsg))
437 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
438 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
439 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
440 + 3 * NLA_TYPE_SIZE(u_int8_t) /* CTA_PROTO_NUM */
441 + NLA_TYPE_SIZE(u_int32_t) /* CTA_ID */
442 + NLA_TYPE_SIZE(u_int32_t) /* CTA_STATUS */
443 #ifdef CONFIG_NF_CT_ACCT
444 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
445 + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_PACKETS */
446 + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_BYTES */
448 + NLA_TYPE_SIZE(u_int32_t) /* CTA_TIMEOUT */
449 + nla_total_size(0) /* CTA_PROTOINFO */
450 + nla_total_size(0) /* CTA_HELP */
451 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
452 #ifdef CONFIG_NF_CONNTRACK_SECMARK
453 + NLA_TYPE_SIZE(u_int32_t) /* CTA_SECMARK */
455 #ifdef CONFIG_NF_NAT_NEEDED
456 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
457 + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_POS */
458 + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_BEFORE */
459 + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_AFTER */
461 #ifdef CONFIG_NF_CONNTRACK_MARK
462 + NLA_TYPE_SIZE(u_int32_t) /* CTA_MARK */
469 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
470 len += l3proto->nla_size;
472 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
473 len += l4proto->nla_size;
476 return alloc_skb(len, gfp);
479 static int ctnetlink_conntrack_event(struct notifier_block *this,
480 unsigned long events, void *ptr)
482 struct nlmsghdr *nlh;
483 struct nfgenmsg *nfmsg;
484 struct nlattr *nest_parms;
485 struct nf_ct_event *item = (struct nf_ct_event *)ptr;
486 struct nf_conn *ct = item->ct;
490 unsigned int flags = 0, group;
492 /* ignore our fake conntrack entry */
493 if (ct == &nf_conntrack_untracked)
496 if (events & IPCT_DESTROY) {
497 type = IPCTNL_MSG_CT_DELETE;
498 group = NFNLGRP_CONNTRACK_DESTROY;
499 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
500 type = IPCTNL_MSG_CT_NEW;
501 flags = NLM_F_CREATE|NLM_F_EXCL;
502 group = NFNLGRP_CONNTRACK_NEW;
503 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
504 type = IPCTNL_MSG_CT_NEW;
505 group = NFNLGRP_CONNTRACK_UPDATE;
509 if (!item->report && !nfnetlink_has_listeners(group))
512 skb = ctnetlink_alloc_skb(tuple(ct, IP_CT_DIR_ORIGINAL), GFP_ATOMIC);
518 type |= NFNL_SUBSYS_CTNETLINK << 8;
519 nlh = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg));
520 nfmsg = NLMSG_DATA(nlh);
522 nlh->nlmsg_flags = flags;
523 nfmsg->nfgen_family = nf_ct_l3num(ct);
524 nfmsg->version = NFNETLINK_V0;
528 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
530 goto nla_put_failure;
531 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
532 goto nla_put_failure;
533 nla_nest_end(skb, nest_parms);
535 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
537 goto nla_put_failure;
538 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
539 goto nla_put_failure;
540 nla_nest_end(skb, nest_parms);
542 if (ctnetlink_dump_id(skb, ct) < 0)
543 goto nla_put_failure;
545 if (ctnetlink_dump_status(skb, ct) < 0)
546 goto nla_put_failure;
548 if (events & IPCT_DESTROY) {
549 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
550 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
551 goto nla_put_failure;
553 if (ctnetlink_dump_timeout(skb, ct) < 0)
554 goto nla_put_failure;
556 if (events & IPCT_PROTOINFO
557 && ctnetlink_dump_protoinfo(skb, ct) < 0)
558 goto nla_put_failure;
560 if ((events & IPCT_HELPER || nfct_help(ct))
561 && ctnetlink_dump_helpinfo(skb, ct) < 0)
562 goto nla_put_failure;
564 #ifdef CONFIG_NF_CONNTRACK_SECMARK
565 if ((events & IPCT_SECMARK || ct->secmark)
566 && ctnetlink_dump_secmark(skb, ct) < 0)
567 goto nla_put_failure;
570 if (events & IPCT_RELATED &&
571 ctnetlink_dump_master(skb, ct) < 0)
572 goto nla_put_failure;
574 if (events & IPCT_NATSEQADJ &&
575 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
576 goto nla_put_failure;
579 #ifdef CONFIG_NF_CONNTRACK_MARK
580 if ((events & IPCT_MARK || ct->mark)
581 && ctnetlink_dump_mark(skb, ct) < 0)
582 goto nla_put_failure;
586 nlh->nlmsg_len = skb->tail - b;
587 nfnetlink_send(skb, item->pid, group, item->report);
595 nfnetlink_set_err(0, group, -ENOBUFS);
598 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
600 static int ctnetlink_done(struct netlink_callback *cb)
603 nf_ct_put((struct nf_conn *)cb->args[1]);
608 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
610 struct nf_conn *ct, *last;
611 struct nf_conntrack_tuple_hash *h;
612 struct hlist_nulls_node *n;
613 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
614 u_int8_t l3proto = nfmsg->nfgen_family;
617 last = (struct nf_conn *)cb->args[1];
618 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
620 hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
622 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
624 ct = nf_ct_tuplehash_to_ctrack(h);
625 if (!atomic_inc_not_zero(&ct->ct_general.use))
627 /* Dump entries of a given L3 protocol number.
628 * If it is not specified, ie. l3proto == 0,
629 * then dump everything. */
630 if (l3proto && nf_ct_l3num(ct) != l3proto)
637 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
639 IPCTNL_MSG_CT_NEW, ct) < 0) {
640 cb->args[1] = (unsigned long)ct;
644 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
645 IPCTNL_MSG_CT_GET_CTRZERO) {
646 struct nf_conn_counter *acct;
648 acct = nf_conn_acct_find(ct);
650 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
669 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
671 struct nlattr *tb[CTA_IP_MAX+1];
672 struct nf_conntrack_l3proto *l3proto;
675 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
678 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
680 if (likely(l3proto->nlattr_to_tuple)) {
681 ret = nla_validate_nested(attr, CTA_IP_MAX,
682 l3proto->nla_policy);
684 ret = l3proto->nlattr_to_tuple(tb, tuple);
692 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
693 [CTA_PROTO_NUM] = { .type = NLA_U8 },
697 ctnetlink_parse_tuple_proto(struct nlattr *attr,
698 struct nf_conntrack_tuple *tuple)
700 struct nlattr *tb[CTA_PROTO_MAX+1];
701 struct nf_conntrack_l4proto *l4proto;
704 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
708 if (!tb[CTA_PROTO_NUM])
710 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
713 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
715 if (likely(l4proto->nlattr_to_tuple)) {
716 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
717 l4proto->nla_policy);
719 ret = l4proto->nlattr_to_tuple(tb, tuple);
728 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
729 enum ctattr_tuple type, u_int8_t l3num)
731 struct nlattr *tb[CTA_TUPLE_MAX+1];
734 memset(tuple, 0, sizeof(*tuple));
736 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
738 if (!tb[CTA_TUPLE_IP])
741 tuple->src.l3num = l3num;
743 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
747 if (!tb[CTA_TUPLE_PROTO])
750 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
754 /* orig and expect tuples get DIR_ORIGINAL */
755 if (type == CTA_TUPLE_REPLY)
756 tuple->dst.dir = IP_CT_DIR_REPLY;
758 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
764 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
766 struct nlattr *tb[CTA_HELP_MAX+1];
768 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
770 if (!tb[CTA_HELP_NAME])
773 *helper_name = nla_data(tb[CTA_HELP_NAME]);
778 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
779 [CTA_STATUS] = { .type = NLA_U32 },
780 [CTA_TIMEOUT] = { .type = NLA_U32 },
781 [CTA_MARK] = { .type = NLA_U32 },
782 [CTA_USE] = { .type = NLA_U32 },
783 [CTA_ID] = { .type = NLA_U32 },
787 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
788 struct nlmsghdr *nlh, struct nlattr *cda[])
790 struct nf_conntrack_tuple_hash *h;
791 struct nf_conntrack_tuple tuple;
793 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
794 u_int8_t u3 = nfmsg->nfgen_family;
797 if (cda[CTA_TUPLE_ORIG])
798 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
799 else if (cda[CTA_TUPLE_REPLY])
800 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
802 /* Flush the whole table */
803 nf_conntrack_flush(&init_net,
812 h = nf_conntrack_find_get(&init_net, &tuple);
816 ct = nf_ct_tuplehash_to_ctrack(h);
819 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
820 if (id != (u32)(unsigned long)ct) {
826 nf_conntrack_event_report(IPCT_DESTROY,
831 /* death_by_timeout would report the event again */
832 set_bit(IPS_DYING_BIT, &ct->status);
841 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
842 struct nlmsghdr *nlh, struct nlattr *cda[])
844 struct nf_conntrack_tuple_hash *h;
845 struct nf_conntrack_tuple tuple;
847 struct sk_buff *skb2 = NULL;
848 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
849 u_int8_t u3 = nfmsg->nfgen_family;
852 if (nlh->nlmsg_flags & NLM_F_DUMP)
853 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
856 if (cda[CTA_TUPLE_ORIG])
857 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
858 else if (cda[CTA_TUPLE_REPLY])
859 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
866 h = nf_conntrack_find_get(&init_net, &tuple);
870 ct = nf_ct_tuplehash_to_ctrack(h);
873 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
880 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
881 IPCTNL_MSG_CT_NEW, ct);
887 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
899 #ifdef CONFIG_NF_NAT_NEEDED
901 ctnetlink_parse_nat_setup(struct nf_conn *ct,
902 enum nf_nat_manip_type manip,
905 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
907 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
908 if (!parse_nat_setup) {
909 #ifdef CONFIG_MODULES
911 spin_unlock_bh(&nf_conntrack_lock);
913 if (request_module("nf-nat-ipv4") < 0) {
915 spin_lock_bh(&nf_conntrack_lock);
920 spin_lock_bh(&nf_conntrack_lock);
922 if (nfnetlink_parse_nat_setup_hook)
928 return parse_nat_setup(ct, manip, attr);
933 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
936 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
937 d = ct->status ^ status;
939 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
943 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
944 /* SEEN_REPLY bit can only be set */
947 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
948 /* ASSURED bit can only be set */
951 /* Be careful here, modifying NAT bits can screw up things,
952 * so don't let users modify them directly if they don't pass
954 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
959 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
961 #ifdef CONFIG_NF_NAT_NEEDED
964 if (cda[CTA_NAT_DST]) {
965 ret = ctnetlink_parse_nat_setup(ct,
971 if (cda[CTA_NAT_SRC]) {
972 ret = ctnetlink_parse_nat_setup(ct,
985 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
987 struct nf_conntrack_helper *helper;
988 struct nf_conn_help *help = nfct_help(ct);
989 char *helpname = NULL;
992 /* don't change helper of sibling connections */
996 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1000 if (!strcmp(helpname, "")) {
1001 if (help && help->helper) {
1002 /* we had a helper before ... */
1003 nf_ct_remove_expectations(ct);
1004 rcu_assign_pointer(help->helper, NULL);
1010 helper = __nf_conntrack_helper_find_byname(helpname);
1011 if (helper == NULL) {
1012 #ifdef CONFIG_MODULES
1013 spin_unlock_bh(&nf_conntrack_lock);
1015 if (request_module("nfct-helper-%s", helpname) < 0) {
1016 spin_lock_bh(&nf_conntrack_lock);
1020 spin_lock_bh(&nf_conntrack_lock);
1021 helper = __nf_conntrack_helper_find_byname(helpname);
1029 if (help->helper == helper)
1033 /* need to zero data of old helper */
1034 memset(&help->help, 0, sizeof(help->help));
1036 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1041 rcu_assign_pointer(help->helper, helper);
1047 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
1049 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1051 if (!del_timer(&ct->timeout))
1054 ct->timeout.expires = jiffies + timeout * HZ;
1055 add_timer(&ct->timeout);
1061 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1063 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1064 struct nf_conntrack_l4proto *l4proto;
1067 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1070 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1071 if (l4proto->from_nlattr)
1072 err = l4proto->from_nlattr(tb, ct);
1078 #ifdef CONFIG_NF_NAT_NEEDED
1080 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1082 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1084 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1086 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1089 natseq->correction_pos =
1090 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1092 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1095 natseq->offset_before =
1096 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1098 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1101 natseq->offset_after =
1102 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1108 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1111 struct nf_conn_nat *nat = nfct_nat(ct);
1116 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1117 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1118 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1122 ct->status |= IPS_SEQ_ADJUST;
1125 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1126 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1127 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1131 ct->status |= IPS_SEQ_ADJUST;
1139 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1143 /* only allow NAT changes and master assignation for new conntracks */
1144 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1147 if (cda[CTA_HELP]) {
1148 err = ctnetlink_change_helper(ct, cda);
1153 if (cda[CTA_TIMEOUT]) {
1154 err = ctnetlink_change_timeout(ct, cda);
1159 if (cda[CTA_STATUS]) {
1160 err = ctnetlink_change_status(ct, cda);
1165 if (cda[CTA_PROTOINFO]) {
1166 err = ctnetlink_change_protoinfo(ct, cda);
1171 #if defined(CONFIG_NF_CONNTRACK_MARK)
1173 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1176 #ifdef CONFIG_NF_NAT_NEEDED
1177 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1178 err = ctnetlink_change_nat_seq_adj(ct, cda);
1187 static struct nf_conn *
1188 ctnetlink_create_conntrack(struct nlattr *cda[],
1189 struct nf_conntrack_tuple *otuple,
1190 struct nf_conntrack_tuple *rtuple,
1195 struct nf_conntrack_helper *helper;
1197 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1199 return ERR_PTR(-ENOMEM);
1201 if (!cda[CTA_TIMEOUT])
1203 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1205 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1206 ct->status |= IPS_CONFIRMED;
1209 if (cda[CTA_HELP]) {
1210 char *helpname = NULL;
1212 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1216 helper = __nf_conntrack_helper_find_byname(helpname);
1217 if (helper == NULL) {
1219 #ifdef CONFIG_MODULES
1220 if (request_module("nfct-helper-%s", helpname) < 0) {
1226 helper = __nf_conntrack_helper_find_byname(helpname);
1236 struct nf_conn_help *help;
1238 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1244 /* not in hash table yet so not strictly necessary */
1245 rcu_assign_pointer(help->helper, helper);
1248 /* try an implicit helper assignation */
1249 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1254 if (cda[CTA_STATUS]) {
1255 err = ctnetlink_change_status(ct, cda);
1260 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1261 err = ctnetlink_change_nat(ct, cda);
1266 #ifdef CONFIG_NF_NAT_NEEDED
1267 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1268 err = ctnetlink_change_nat_seq_adj(ct, cda);
1274 if (cda[CTA_PROTOINFO]) {
1275 err = ctnetlink_change_protoinfo(ct, cda);
1280 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1282 #if defined(CONFIG_NF_CONNTRACK_MARK)
1284 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1287 /* setup master conntrack: this is a confirmed expectation */
1288 if (cda[CTA_TUPLE_MASTER]) {
1289 struct nf_conntrack_tuple master;
1290 struct nf_conntrack_tuple_hash *master_h;
1291 struct nf_conn *master_ct;
1293 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1297 master_h = nf_conntrack_find_get(&init_net, &master);
1298 if (master_h == NULL) {
1302 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1303 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1304 ct->master = master_ct;
1307 add_timer(&ct->timeout);
1308 nf_conntrack_hash_insert(ct);
1316 nf_conntrack_free(ct);
1317 return ERR_PTR(err);
1321 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1322 struct nlmsghdr *nlh, struct nlattr *cda[])
1324 struct nf_conntrack_tuple otuple, rtuple;
1325 struct nf_conntrack_tuple_hash *h = NULL;
1326 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1327 u_int8_t u3 = nfmsg->nfgen_family;
1330 if (cda[CTA_TUPLE_ORIG]) {
1331 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1336 if (cda[CTA_TUPLE_REPLY]) {
1337 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1342 spin_lock_bh(&nf_conntrack_lock);
1343 if (cda[CTA_TUPLE_ORIG])
1344 h = __nf_conntrack_find(&init_net, &otuple);
1345 else if (cda[CTA_TUPLE_REPLY])
1346 h = __nf_conntrack_find(&init_net, &rtuple);
1350 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1352 enum ip_conntrack_events events;
1354 ct = ctnetlink_create_conntrack(cda, &otuple,
1361 nf_conntrack_get(&ct->ct_general);
1362 spin_unlock_bh(&nf_conntrack_lock);
1363 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1364 events = IPCT_RELATED;
1368 nf_conntrack_event_report(IPCT_STATUS |
1373 ct, NETLINK_CB(skb).pid,
1377 spin_unlock_bh(&nf_conntrack_lock);
1381 /* implicit 'else' */
1383 /* We manipulate the conntrack inside the global conntrack table lock,
1384 * so there's no need to increase the refcount */
1386 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1387 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1389 err = ctnetlink_change_conntrack(ct, cda);
1391 nf_conntrack_get(&ct->ct_general);
1392 spin_unlock_bh(&nf_conntrack_lock);
1393 nf_conntrack_event_report(IPCT_STATUS |
1398 ct, NETLINK_CB(skb).pid,
1402 spin_unlock_bh(&nf_conntrack_lock);
1408 spin_unlock_bh(&nf_conntrack_lock);
1412 /***********************************************************************
1414 ***********************************************************************/
1417 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1418 const struct nf_conntrack_tuple *tuple,
1419 enum ctattr_expect type)
1421 struct nlattr *nest_parms;
1423 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1425 goto nla_put_failure;
1426 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1427 goto nla_put_failure;
1428 nla_nest_end(skb, nest_parms);
1437 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1438 const struct nf_conntrack_tuple *tuple,
1439 const struct nf_conntrack_tuple_mask *mask)
1442 struct nf_conntrack_l3proto *l3proto;
1443 struct nf_conntrack_l4proto *l4proto;
1444 struct nf_conntrack_tuple m;
1445 struct nlattr *nest_parms;
1447 memset(&m, 0xFF, sizeof(m));
1448 m.src.u.all = mask->src.u.all;
1449 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1451 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1453 goto nla_put_failure;
1455 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1456 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1458 if (unlikely(ret < 0))
1459 goto nla_put_failure;
1461 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1462 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1463 if (unlikely(ret < 0))
1464 goto nla_put_failure;
1466 nla_nest_end(skb, nest_parms);
1475 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1476 const struct nf_conntrack_expect *exp)
1478 struct nf_conn *master = exp->master;
1479 long timeout = (exp->timeout.expires - jiffies) / HZ;
1484 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1485 goto nla_put_failure;
1486 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1487 goto nla_put_failure;
1488 if (ctnetlink_exp_dump_tuple(skb,
1489 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1490 CTA_EXPECT_MASTER) < 0)
1491 goto nla_put_failure;
1493 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1494 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1503 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1504 int event, const struct nf_conntrack_expect *exp)
1506 struct nlmsghdr *nlh;
1507 struct nfgenmsg *nfmsg;
1508 unsigned char *b = skb_tail_pointer(skb);
1510 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1511 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1512 nfmsg = NLMSG_DATA(nlh);
1514 nlh->nlmsg_flags = pid ? NLM_F_MULTI : 0;
1515 nfmsg->nfgen_family = exp->tuple.src.l3num;
1516 nfmsg->version = NFNETLINK_V0;
1519 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1520 goto nla_put_failure;
1522 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1531 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1532 static int ctnetlink_expect_event(struct notifier_block *this,
1533 unsigned long events, void *ptr)
1535 struct nlmsghdr *nlh;
1536 struct nfgenmsg *nfmsg;
1537 struct nf_exp_event *item = (struct nf_exp_event *)ptr;
1538 struct nf_conntrack_expect *exp = item->exp;
1539 struct sk_buff *skb;
1544 if (events & IPEXP_NEW) {
1545 type = IPCTNL_MSG_EXP_NEW;
1546 flags = NLM_F_CREATE|NLM_F_EXCL;
1550 if (!item->report &&
1551 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1554 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1560 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1561 nlh = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg));
1562 nfmsg = NLMSG_DATA(nlh);
1564 nlh->nlmsg_flags = flags;
1565 nfmsg->nfgen_family = exp->tuple.src.l3num;
1566 nfmsg->version = NFNETLINK_V0;
1570 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1571 goto nla_put_failure;
1574 nlh->nlmsg_len = skb->tail - b;
1575 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report);
1583 nfnetlink_set_err(0, 0, -ENOBUFS);
1587 static int ctnetlink_exp_done(struct netlink_callback *cb)
1590 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1595 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1597 struct net *net = &init_net;
1598 struct nf_conntrack_expect *exp, *last;
1599 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1600 struct hlist_node *n;
1601 u_int8_t l3proto = nfmsg->nfgen_family;
1604 last = (struct nf_conntrack_expect *)cb->args[1];
1605 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1607 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1609 if (l3proto && exp->tuple.src.l3num != l3proto)
1616 if (ctnetlink_exp_fill_info(skb,
1617 NETLINK_CB(cb->skb).pid,
1621 if (!atomic_inc_not_zero(&exp->use))
1623 cb->args[1] = (unsigned long)exp;
1635 nf_ct_expect_put(last);
1640 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1641 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1642 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1646 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1647 struct nlmsghdr *nlh, struct nlattr *cda[])
1649 struct nf_conntrack_tuple tuple;
1650 struct nf_conntrack_expect *exp;
1651 struct sk_buff *skb2;
1652 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1653 u_int8_t u3 = nfmsg->nfgen_family;
1656 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1657 return netlink_dump_start(ctnl, skb, nlh,
1658 ctnetlink_exp_dump_table,
1659 ctnetlink_exp_done);
1662 if (cda[CTA_EXPECT_MASTER])
1663 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1670 exp = nf_ct_expect_find_get(&init_net, &tuple);
1674 if (cda[CTA_EXPECT_ID]) {
1675 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1676 if (ntohl(id) != (u32)(unsigned long)exp) {
1677 nf_ct_expect_put(exp);
1683 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1688 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1689 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1694 nf_ct_expect_put(exp);
1696 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1701 nf_ct_expect_put(exp);
1706 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1707 struct nlmsghdr *nlh, struct nlattr *cda[])
1709 struct nf_conntrack_expect *exp;
1710 struct nf_conntrack_tuple tuple;
1711 struct nf_conntrack_helper *h;
1712 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1713 struct hlist_node *n, *next;
1714 u_int8_t u3 = nfmsg->nfgen_family;
1718 if (cda[CTA_EXPECT_TUPLE]) {
1719 /* delete a single expect by tuple */
1720 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1724 /* bump usage count to 2 */
1725 exp = nf_ct_expect_find_get(&init_net, &tuple);
1729 if (cda[CTA_EXPECT_ID]) {
1730 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1731 if (ntohl(id) != (u32)(unsigned long)exp) {
1732 nf_ct_expect_put(exp);
1737 /* after list removal, usage count == 1 */
1738 nf_ct_unexpect_related(exp);
1739 /* have to put what we 'get' above.
1740 * after this line usage count == 0 */
1741 nf_ct_expect_put(exp);
1742 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1743 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1744 struct nf_conn_help *m_help;
1746 /* delete all expectations for this helper */
1747 spin_lock_bh(&nf_conntrack_lock);
1748 h = __nf_conntrack_helper_find_byname(name);
1750 spin_unlock_bh(&nf_conntrack_lock);
1753 for (i = 0; i < nf_ct_expect_hsize; i++) {
1754 hlist_for_each_entry_safe(exp, n, next,
1755 &init_net.ct.expect_hash[i],
1757 m_help = nfct_help(exp->master);
1758 if (m_help->helper == h
1759 && del_timer(&exp->timeout)) {
1760 nf_ct_unlink_expect(exp);
1761 nf_ct_expect_put(exp);
1765 spin_unlock_bh(&nf_conntrack_lock);
1767 /* This basically means we have to flush everything*/
1768 spin_lock_bh(&nf_conntrack_lock);
1769 for (i = 0; i < nf_ct_expect_hsize; i++) {
1770 hlist_for_each_entry_safe(exp, n, next,
1771 &init_net.ct.expect_hash[i],
1773 if (del_timer(&exp->timeout)) {
1774 nf_ct_unlink_expect(exp);
1775 nf_ct_expect_put(exp);
1779 spin_unlock_bh(&nf_conntrack_lock);
1785 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1791 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
1793 struct nf_conntrack_tuple tuple, mask, master_tuple;
1794 struct nf_conntrack_tuple_hash *h = NULL;
1795 struct nf_conntrack_expect *exp;
1797 struct nf_conn_help *help;
1800 /* caller guarantees that those three CTA_EXPECT_* exist */
1801 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1804 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1807 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1811 /* Look for master conntrack of this expectation */
1812 h = nf_conntrack_find_get(&init_net, &master_tuple);
1815 ct = nf_ct_tuplehash_to_ctrack(h);
1816 help = nfct_help(ct);
1818 if (!help || !help->helper) {
1819 /* such conntrack hasn't got any helper, abort */
1824 exp = nf_ct_expect_alloc(ct);
1831 exp->expectfn = NULL;
1835 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1836 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1837 exp->mask.src.u.all = mask.src.u.all;
1839 err = nf_ct_expect_related_report(exp, pid, report);
1840 nf_ct_expect_put(exp);
1843 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1848 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1849 struct nlmsghdr *nlh, struct nlattr *cda[])
1851 struct nf_conntrack_tuple tuple;
1852 struct nf_conntrack_expect *exp;
1853 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1854 u_int8_t u3 = nfmsg->nfgen_family;
1857 if (!cda[CTA_EXPECT_TUPLE]
1858 || !cda[CTA_EXPECT_MASK]
1859 || !cda[CTA_EXPECT_MASTER])
1862 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1866 spin_lock_bh(&nf_conntrack_lock);
1867 exp = __nf_ct_expect_find(&init_net, &tuple);
1870 spin_unlock_bh(&nf_conntrack_lock);
1872 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1873 err = ctnetlink_create_expect(cda,
1875 NETLINK_CB(skb).pid,
1882 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1883 err = ctnetlink_change_expect(exp, cda);
1884 spin_unlock_bh(&nf_conntrack_lock);
1889 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1890 static struct notifier_block ctnl_notifier = {
1891 .notifier_call = ctnetlink_conntrack_event,
1894 static struct notifier_block ctnl_notifier_exp = {
1895 .notifier_call = ctnetlink_expect_event,
1899 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1900 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1901 .attr_count = CTA_MAX,
1902 .policy = ct_nla_policy },
1903 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1904 .attr_count = CTA_MAX,
1905 .policy = ct_nla_policy },
1906 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1907 .attr_count = CTA_MAX,
1908 .policy = ct_nla_policy },
1909 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1910 .attr_count = CTA_MAX,
1911 .policy = ct_nla_policy },
1914 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1915 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1916 .attr_count = CTA_EXPECT_MAX,
1917 .policy = exp_nla_policy },
1918 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1919 .attr_count = CTA_EXPECT_MAX,
1920 .policy = exp_nla_policy },
1921 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1922 .attr_count = CTA_EXPECT_MAX,
1923 .policy = exp_nla_policy },
1926 static const struct nfnetlink_subsystem ctnl_subsys = {
1927 .name = "conntrack",
1928 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1929 .cb_count = IPCTNL_MSG_MAX,
1933 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1934 .name = "conntrack_expect",
1935 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1936 .cb_count = IPCTNL_MSG_EXP_MAX,
1940 MODULE_ALIAS("ip_conntrack_netlink");
1941 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1942 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1944 static int __init ctnetlink_init(void)
1948 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1949 ret = nfnetlink_subsys_register(&ctnl_subsys);
1951 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1955 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1957 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1958 goto err_unreg_subsys;
1961 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1962 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1964 printk("ctnetlink_init: cannot register notifier.\n");
1965 goto err_unreg_exp_subsys;
1968 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1970 printk("ctnetlink_init: cannot expect register notifier.\n");
1971 goto err_unreg_notifier;
1977 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1979 nf_conntrack_unregister_notifier(&ctnl_notifier);
1980 err_unreg_exp_subsys:
1981 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1984 nfnetlink_subsys_unregister(&ctnl_subsys);
1989 static void __exit ctnetlink_exit(void)
1991 printk("ctnetlink: unregistering from nfnetlink.\n");
1993 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1994 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1995 nf_conntrack_unregister_notifier(&ctnl_notifier);
1998 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1999 nfnetlink_subsys_unregister(&ctnl_subsys);
2003 module_init(ctnetlink_init);
2004 module_exit(ctnetlink_exit);