1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/types.h>
23 #include <linux/timer.h>
24 #include <linux/skbuff.h>
25 #include <linux/errno.h>
26 #include <linux/netlink.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/notifier.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
49 MODULE_LICENSE("GPL");
51 static char __initdata version
[] = "0.93";
54 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
55 const struct nf_conntrack_tuple
*tuple
,
56 struct nf_conntrack_l4proto
*l4proto
)
59 struct nlattr
*nest_parms
;
61 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
64 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
66 if (likely(l4proto
->tuple_to_nlattr
))
67 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
69 nla_nest_end(skb
, nest_parms
);
78 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
79 const struct nf_conntrack_tuple
*tuple
,
80 struct nf_conntrack_l3proto
*l3proto
)
83 struct nlattr
*nest_parms
;
85 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
89 if (likely(l3proto
->tuple_to_nlattr
))
90 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
92 nla_nest_end(skb
, nest_parms
);
101 ctnetlink_dump_tuples(struct sk_buff
*skb
,
102 const struct nf_conntrack_tuple
*tuple
)
105 struct nf_conntrack_l3proto
*l3proto
;
106 struct nf_conntrack_l4proto
*l4proto
;
108 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
109 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
110 nf_ct_l3proto_put(l3proto
);
112 if (unlikely(ret
< 0))
115 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
116 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
117 nf_ct_l4proto_put(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
, const struct nf_conn
*ct
)
150 struct nf_conntrack_l4proto
*l4proto
;
151 struct nlattr
*nest_proto
;
154 l4proto
= nf_ct_l4proto_find_get(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
155 if (!l4proto
->to_nlattr
) {
156 nf_ct_l4proto_put(l4proto
);
160 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
162 goto nla_put_failure
;
164 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
166 nf_ct_l4proto_put(l4proto
);
168 nla_nest_end(skb
, nest_proto
);
173 nf_ct_l4proto_put(l4proto
);
178 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
180 struct nlattr
*nest_helper
;
181 const struct nf_conn_help
*help
= nfct_help(ct
);
182 struct nf_conntrack_helper
*helper
;
188 helper
= rcu_dereference(help
->helper
);
192 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
194 goto nla_put_failure
;
195 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
197 if (helper
->to_nlattr
)
198 helper
->to_nlattr(skb
, ct
);
200 nla_nest_end(skb
, nest_helper
);
211 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
212 enum ip_conntrack_dir dir
)
214 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
215 struct nlattr
*nest_count
;
216 const struct nf_conn_counter
*acct
;
218 acct
= nf_conn_acct_find(ct
);
222 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
224 goto nla_put_failure
;
226 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
,
227 cpu_to_be64(acct
[dir
].packets
));
228 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
,
229 cpu_to_be64(acct
[dir
].bytes
));
231 nla_nest_end(skb
, nest_count
);
239 #ifdef CONFIG_NF_CONNTRACK_MARK
241 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
243 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
250 #define ctnetlink_dump_mark(a, b) (0)
253 #ifdef CONFIG_NF_CONNTRACK_SECMARK
255 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
257 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
264 #define ctnetlink_dump_secmark(a, b) (0)
267 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
270 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
272 struct nlattr
*nest_parms
;
274 if (!(ct
->status
& IPS_EXPECTED
))
277 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
279 goto nla_put_failure
;
280 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
281 goto nla_put_failure
;
282 nla_nest_end(skb
, nest_parms
);
290 #ifdef CONFIG_NF_NAT_NEEDED
292 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
294 struct nlattr
*nest_parms
;
296 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
298 goto nla_put_failure
;
300 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
301 htonl(natseq
->correction_pos
));
302 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
303 htonl(natseq
->offset_before
));
304 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
305 htonl(natseq
->offset_after
));
307 nla_nest_end(skb
, nest_parms
);
316 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
318 struct nf_nat_seq
*natseq
;
319 struct nf_conn_nat
*nat
= nfct_nat(ct
);
321 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
324 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
325 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
328 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
329 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
335 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
339 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
341 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
349 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
351 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
358 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
361 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
362 int event
, int nowait
,
363 const struct nf_conn
*ct
)
365 struct nlmsghdr
*nlh
;
366 struct nfgenmsg
*nfmsg
;
367 struct nlattr
*nest_parms
;
368 unsigned char *b
= skb_tail_pointer(skb
);
370 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
371 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
372 nfmsg
= NLMSG_DATA(nlh
);
374 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
375 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
376 nfmsg
->version
= NFNETLINK_V0
;
379 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
381 goto nla_put_failure
;
382 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
383 goto nla_put_failure
;
384 nla_nest_end(skb
, nest_parms
);
386 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
388 goto nla_put_failure
;
389 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
390 goto nla_put_failure
;
391 nla_nest_end(skb
, nest_parms
);
393 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
394 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
395 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
396 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
397 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
398 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
399 ctnetlink_dump_mark(skb
, ct
) < 0 ||
400 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
401 ctnetlink_dump_id(skb
, ct
) < 0 ||
402 ctnetlink_dump_use(skb
, ct
) < 0 ||
403 ctnetlink_dump_master(skb
, ct
) < 0 ||
404 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
405 goto nla_put_failure
;
407 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
416 #ifdef CONFIG_NF_CONNTRACK_EVENTS
417 static int ctnetlink_conntrack_event(struct notifier_block
*this,
418 unsigned long events
, void *ptr
)
420 struct nlmsghdr
*nlh
;
421 struct nfgenmsg
*nfmsg
;
422 struct nlattr
*nest_parms
;
423 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
427 unsigned int flags
= 0, group
;
429 /* ignore our fake conntrack entry */
430 if (ct
== &nf_conntrack_untracked
)
433 if (events
& IPCT_DESTROY
) {
434 type
= IPCTNL_MSG_CT_DELETE
;
435 group
= NFNLGRP_CONNTRACK_DESTROY
;
436 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
437 type
= IPCTNL_MSG_CT_NEW
;
438 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
439 group
= NFNLGRP_CONNTRACK_NEW
;
440 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
441 type
= IPCTNL_MSG_CT_NEW
;
442 group
= NFNLGRP_CONNTRACK_UPDATE
;
446 if (!nfnetlink_has_listeners(group
))
449 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
455 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
456 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
457 nfmsg
= NLMSG_DATA(nlh
);
459 nlh
->nlmsg_flags
= flags
;
460 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
461 nfmsg
->version
= NFNETLINK_V0
;
464 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
466 goto nla_put_failure
;
467 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
468 goto nla_put_failure
;
469 nla_nest_end(skb
, nest_parms
);
471 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
473 goto nla_put_failure
;
474 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
475 goto nla_put_failure
;
476 nla_nest_end(skb
, nest_parms
);
478 if (ctnetlink_dump_id(skb
, ct
) < 0)
479 goto nla_put_failure
;
481 if (ctnetlink_dump_status(skb
, ct
) < 0)
482 goto nla_put_failure
;
484 if (events
& IPCT_DESTROY
) {
485 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
486 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
487 goto nla_put_failure
;
489 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
490 goto nla_put_failure
;
492 if (events
& IPCT_PROTOINFO
493 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
494 goto nla_put_failure
;
496 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
497 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
498 goto nla_put_failure
;
500 #ifdef CONFIG_NF_CONNTRACK_SECMARK
501 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
502 && ctnetlink_dump_secmark(skb
, ct
) < 0)
503 goto nla_put_failure
;
506 if (events
& IPCT_RELATED
&&
507 ctnetlink_dump_master(skb
, ct
) < 0)
508 goto nla_put_failure
;
510 if (events
& IPCT_NATSEQADJ
&&
511 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
512 goto nla_put_failure
;
515 #ifdef CONFIG_NF_CONNTRACK_MARK
516 if ((events
& IPCT_MARK
|| ct
->mark
)
517 && ctnetlink_dump_mark(skb
, ct
) < 0)
518 goto nla_put_failure
;
521 nlh
->nlmsg_len
= skb
->tail
- b
;
522 nfnetlink_send(skb
, 0, group
, 0);
530 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
532 static int ctnetlink_done(struct netlink_callback
*cb
)
535 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
540 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
542 struct nf_conn
*ct
, *last
;
543 struct nf_conntrack_tuple_hash
*h
;
544 struct hlist_node
*n
;
545 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
546 u_int8_t l3proto
= nfmsg
->nfgen_family
;
549 last
= (struct nf_conn
*)cb
->args
[1];
550 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
552 hlist_for_each_entry_rcu(h
, n
, &init_net
.ct
.hash
[cb
->args
[0]],
554 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
556 ct
= nf_ct_tuplehash_to_ctrack(h
);
557 /* Dump entries of a given L3 protocol number.
558 * If it is not specified, ie. l3proto == 0,
559 * then dump everything. */
560 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
567 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
571 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
573 cb
->args
[1] = (unsigned long)ct
;
577 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
578 IPCTNL_MSG_CT_GET_CTRZERO
) {
579 struct nf_conn_counter
*acct
;
581 acct
= nf_conn_acct_find(ct
);
583 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
600 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
602 struct nlattr
*tb
[CTA_IP_MAX
+1];
603 struct nf_conntrack_l3proto
*l3proto
;
606 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
608 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
610 if (likely(l3proto
->nlattr_to_tuple
)) {
611 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
612 l3proto
->nla_policy
);
614 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
617 nf_ct_l3proto_put(l3proto
);
622 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
623 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
627 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
628 struct nf_conntrack_tuple
*tuple
)
630 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
631 struct nf_conntrack_l4proto
*l4proto
;
634 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
638 if (!tb
[CTA_PROTO_NUM
])
640 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
642 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
644 if (likely(l4proto
->nlattr_to_tuple
)) {
645 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
646 l4proto
->nla_policy
);
648 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
651 nf_ct_l4proto_put(l4proto
);
657 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
658 enum ctattr_tuple type
, u_int8_t l3num
)
660 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
663 memset(tuple
, 0, sizeof(*tuple
));
665 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
667 if (!tb
[CTA_TUPLE_IP
])
670 tuple
->src
.l3num
= l3num
;
672 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
676 if (!tb
[CTA_TUPLE_PROTO
])
679 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
683 /* orig and expect tuples get DIR_ORIGINAL */
684 if (type
== CTA_TUPLE_REPLY
)
685 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
687 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
693 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
695 struct nlattr
*tb
[CTA_HELP_MAX
+1];
697 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
699 if (!tb
[CTA_HELP_NAME
])
702 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
707 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
708 [CTA_STATUS
] = { .type
= NLA_U32
},
709 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
710 [CTA_MARK
] = { .type
= NLA_U32
},
711 [CTA_USE
] = { .type
= NLA_U32
},
712 [CTA_ID
] = { .type
= NLA_U32
},
716 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
717 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
719 struct nf_conntrack_tuple_hash
*h
;
720 struct nf_conntrack_tuple tuple
;
722 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
723 u_int8_t u3
= nfmsg
->nfgen_family
;
726 if (cda
[CTA_TUPLE_ORIG
])
727 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
728 else if (cda
[CTA_TUPLE_REPLY
])
729 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
731 /* Flush the whole table */
732 nf_conntrack_flush(&init_net
);
739 h
= nf_conntrack_find_get(&init_net
, &tuple
);
743 ct
= nf_ct_tuplehash_to_ctrack(h
);
746 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
747 if (id
!= (u32
)(unsigned long)ct
) {
760 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
761 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
763 struct nf_conntrack_tuple_hash
*h
;
764 struct nf_conntrack_tuple tuple
;
766 struct sk_buff
*skb2
= NULL
;
767 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
768 u_int8_t u3
= nfmsg
->nfgen_family
;
771 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
772 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
775 if (cda
[CTA_TUPLE_ORIG
])
776 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
777 else if (cda
[CTA_TUPLE_REPLY
])
778 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
785 h
= nf_conntrack_find_get(&init_net
, &tuple
);
789 ct
= nf_ct_tuplehash_to_ctrack(h
);
792 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
798 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
799 IPCTNL_MSG_CT_NEW
, 1, ct
);
804 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
816 #ifdef CONFIG_NF_NAT_NEEDED
818 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
819 enum nf_nat_manip_type manip
,
822 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
824 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
825 if (!parse_nat_setup
) {
826 #ifdef CONFIG_MODULES
829 if (request_module("nf-nat-ipv4") < 0) {
836 if (nfnetlink_parse_nat_setup_hook
)
842 return parse_nat_setup(ct
, manip
, attr
);
847 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
850 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
851 d
= ct
->status
^ status
;
853 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
857 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
858 /* SEEN_REPLY bit can only be set */
861 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
862 /* ASSURED bit can only be set */
865 /* Be careful here, modifying NAT bits can screw up things,
866 * so don't let users modify them directly if they don't pass
868 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
873 ctnetlink_change_nat(struct nf_conn
*ct
, struct nlattr
*cda
[])
875 #ifdef CONFIG_NF_NAT_NEEDED
878 if (cda
[CTA_NAT_DST
]) {
879 ret
= ctnetlink_parse_nat_setup(ct
,
885 if (cda
[CTA_NAT_SRC
]) {
886 ret
= ctnetlink_parse_nat_setup(ct
,
899 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
901 struct nf_conntrack_helper
*helper
;
902 struct nf_conn_help
*help
= nfct_help(ct
);
906 /* don't change helper of sibling connections */
910 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
914 if (!strcmp(helpname
, "")) {
915 if (help
&& help
->helper
) {
916 /* we had a helper before ... */
917 nf_ct_remove_expectations(ct
);
918 rcu_assign_pointer(help
->helper
, NULL
);
924 helper
= __nf_conntrack_helper_find_byname(helpname
);
929 if (help
->helper
== helper
)
933 /* need to zero data of old helper */
934 memset(&help
->help
, 0, sizeof(help
->help
));
936 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
941 rcu_assign_pointer(help
->helper
, helper
);
947 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
949 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
951 if (!del_timer(&ct
->timeout
))
954 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
955 add_timer(&ct
->timeout
);
961 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
963 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
964 struct nf_conntrack_l4proto
*l4proto
;
967 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
969 l4proto
= nf_ct_l4proto_find_get(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
970 if (l4proto
->from_nlattr
)
971 err
= l4proto
->from_nlattr(tb
, ct
);
972 nf_ct_l4proto_put(l4proto
);
977 #ifdef CONFIG_NF_NAT_NEEDED
979 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
981 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
983 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
985 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
988 natseq
->correction_pos
=
989 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
991 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
994 natseq
->offset_before
=
995 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
997 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1000 natseq
->offset_after
=
1001 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1007 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1010 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1015 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1016 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1017 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1021 ct
->status
|= IPS_SEQ_ADJUST
;
1024 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1025 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1026 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1030 ct
->status
|= IPS_SEQ_ADJUST
;
1038 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1042 if (cda
[CTA_HELP
]) {
1043 err
= ctnetlink_change_helper(ct
, cda
);
1048 if (cda
[CTA_TIMEOUT
]) {
1049 err
= ctnetlink_change_timeout(ct
, cda
);
1054 if (cda
[CTA_STATUS
]) {
1055 err
= ctnetlink_change_status(ct
, cda
);
1060 if (cda
[CTA_PROTOINFO
]) {
1061 err
= ctnetlink_change_protoinfo(ct
, cda
);
1066 #if defined(CONFIG_NF_CONNTRACK_MARK)
1068 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1071 #ifdef CONFIG_NF_NAT_NEEDED
1072 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1073 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1083 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1084 struct nf_conntrack_tuple
*otuple
,
1085 struct nf_conntrack_tuple
*rtuple
,
1086 struct nf_conn
*master_ct
)
1090 struct nf_conn_help
*help
;
1091 struct nf_conntrack_helper
*helper
;
1093 ct
= nf_conntrack_alloc(&init_net
, otuple
, rtuple
, GFP_ATOMIC
);
1094 if (ct
== NULL
|| IS_ERR(ct
))
1097 if (!cda
[CTA_TIMEOUT
])
1099 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1101 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1102 ct
->status
|= IPS_CONFIRMED
;
1105 helper
= __nf_ct_helper_find(rtuple
);
1107 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1113 /* not in hash table yet so not strictly necessary */
1114 rcu_assign_pointer(help
->helper
, helper
);
1117 if (cda
[CTA_STATUS
]) {
1118 err
= ctnetlink_change_status(ct
, cda
);
1125 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1126 err
= ctnetlink_change_nat(ct
, cda
);
1133 if (cda
[CTA_PROTOINFO
]) {
1134 err
= ctnetlink_change_protoinfo(ct
, cda
);
1141 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1143 #if defined(CONFIG_NF_CONNTRACK_MARK)
1145 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1148 /* setup master conntrack: this is a confirmed expectation */
1150 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1151 ct
->master
= master_ct
;
1154 add_timer(&ct
->timeout
);
1155 nf_conntrack_hash_insert(ct
);
1161 nf_conntrack_free(ct
);
1166 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1167 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1169 struct nf_conntrack_tuple otuple
, rtuple
;
1170 struct nf_conntrack_tuple_hash
*h
= NULL
;
1171 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1172 u_int8_t u3
= nfmsg
->nfgen_family
;
1175 if (cda
[CTA_TUPLE_ORIG
]) {
1176 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1181 if (cda
[CTA_TUPLE_REPLY
]) {
1182 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1187 spin_lock_bh(&nf_conntrack_lock
);
1188 if (cda
[CTA_TUPLE_ORIG
])
1189 h
= __nf_conntrack_find(&init_net
, &otuple
);
1190 else if (cda
[CTA_TUPLE_REPLY
])
1191 h
= __nf_conntrack_find(&init_net
, &rtuple
);
1194 struct nf_conntrack_tuple master
;
1195 struct nf_conntrack_tuple_hash
*master_h
= NULL
;
1196 struct nf_conn
*master_ct
= NULL
;
1198 if (cda
[CTA_TUPLE_MASTER
]) {
1199 err
= ctnetlink_parse_tuple(cda
,
1206 master_h
= __nf_conntrack_find(&init_net
, &master
);
1207 if (master_h
== NULL
) {
1211 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1212 atomic_inc(&master_ct
->ct_general
.use
);
1216 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1217 err
= ctnetlink_create_conntrack(cda
,
1221 spin_unlock_bh(&nf_conntrack_lock
);
1223 if (err
< 0 && master_ct
)
1224 nf_ct_put(master_ct
);
1228 /* implicit 'else' */
1230 /* We manipulate the conntrack inside the global conntrack table lock,
1231 * so there's no need to increase the refcount */
1233 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1234 /* we only allow nat config for new conntracks */
1235 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1239 /* can't link an existing conntrack to a master */
1240 if (cda
[CTA_TUPLE_MASTER
]) {
1244 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
),
1249 spin_unlock_bh(&nf_conntrack_lock
);
1253 /***********************************************************************
1255 ***********************************************************************/
1258 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1259 const struct nf_conntrack_tuple
*tuple
,
1260 enum ctattr_expect type
)
1262 struct nlattr
*nest_parms
;
1264 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1266 goto nla_put_failure
;
1267 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1268 goto nla_put_failure
;
1269 nla_nest_end(skb
, nest_parms
);
1278 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1279 const struct nf_conntrack_tuple
*tuple
,
1280 const struct nf_conntrack_tuple_mask
*mask
)
1283 struct nf_conntrack_l3proto
*l3proto
;
1284 struct nf_conntrack_l4proto
*l4proto
;
1285 struct nf_conntrack_tuple m
;
1286 struct nlattr
*nest_parms
;
1288 memset(&m
, 0xFF, sizeof(m
));
1289 m
.src
.u
.all
= mask
->src
.u
.all
;
1290 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1292 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1294 goto nla_put_failure
;
1296 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1297 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1298 nf_ct_l3proto_put(l3proto
);
1300 if (unlikely(ret
< 0))
1301 goto nla_put_failure
;
1303 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1304 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1305 nf_ct_l4proto_put(l4proto
);
1306 if (unlikely(ret
< 0))
1307 goto nla_put_failure
;
1309 nla_nest_end(skb
, nest_parms
);
1318 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1319 const struct nf_conntrack_expect
*exp
)
1321 struct nf_conn
*master
= exp
->master
;
1322 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1327 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1328 goto nla_put_failure
;
1329 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1330 goto nla_put_failure
;
1331 if (ctnetlink_exp_dump_tuple(skb
,
1332 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1333 CTA_EXPECT_MASTER
) < 0)
1334 goto nla_put_failure
;
1336 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1337 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1346 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1349 const struct nf_conntrack_expect
*exp
)
1351 struct nlmsghdr
*nlh
;
1352 struct nfgenmsg
*nfmsg
;
1353 unsigned char *b
= skb_tail_pointer(skb
);
1355 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1356 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1357 nfmsg
= NLMSG_DATA(nlh
);
1359 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1360 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1361 nfmsg
->version
= NFNETLINK_V0
;
1364 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1365 goto nla_put_failure
;
1367 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1376 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1377 static int ctnetlink_expect_event(struct notifier_block
*this,
1378 unsigned long events
, void *ptr
)
1380 struct nlmsghdr
*nlh
;
1381 struct nfgenmsg
*nfmsg
;
1382 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1383 struct sk_buff
*skb
;
1388 if (events
& IPEXP_NEW
) {
1389 type
= IPCTNL_MSG_EXP_NEW
;
1390 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1394 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1397 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1403 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1404 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1405 nfmsg
= NLMSG_DATA(nlh
);
1407 nlh
->nlmsg_flags
= flags
;
1408 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1409 nfmsg
->version
= NFNETLINK_V0
;
1412 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1413 goto nla_put_failure
;
1415 nlh
->nlmsg_len
= skb
->tail
- b
;
1416 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1425 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1428 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1433 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1435 struct net
*net
= &init_net
;
1436 struct nf_conntrack_expect
*exp
, *last
;
1437 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1438 struct hlist_node
*n
;
1439 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1442 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1443 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1445 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1447 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1454 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1458 if (!atomic_inc_not_zero(&exp
->use
))
1460 cb
->args
[1] = (unsigned long)exp
;
1472 nf_ct_expect_put(last
);
1477 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1478 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1479 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1483 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1484 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1486 struct nf_conntrack_tuple tuple
;
1487 struct nf_conntrack_expect
*exp
;
1488 struct sk_buff
*skb2
;
1489 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1490 u_int8_t u3
= nfmsg
->nfgen_family
;
1493 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1494 return netlink_dump_start(ctnl
, skb
, nlh
,
1495 ctnetlink_exp_dump_table
,
1496 ctnetlink_exp_done
);
1499 if (cda
[CTA_EXPECT_MASTER
])
1500 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1507 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1511 if (cda
[CTA_EXPECT_ID
]) {
1512 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1513 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1514 nf_ct_expect_put(exp
);
1520 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1524 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1525 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1530 nf_ct_expect_put(exp
);
1532 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1537 nf_ct_expect_put(exp
);
1542 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1543 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1545 struct nf_conntrack_expect
*exp
;
1546 struct nf_conntrack_tuple tuple
;
1547 struct nf_conntrack_helper
*h
;
1548 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1549 struct hlist_node
*n
, *next
;
1550 u_int8_t u3
= nfmsg
->nfgen_family
;
1554 if (cda
[CTA_EXPECT_TUPLE
]) {
1555 /* delete a single expect by tuple */
1556 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1560 /* bump usage count to 2 */
1561 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1565 if (cda
[CTA_EXPECT_ID
]) {
1566 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1567 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1568 nf_ct_expect_put(exp
);
1573 /* after list removal, usage count == 1 */
1574 nf_ct_unexpect_related(exp
);
1575 /* have to put what we 'get' above.
1576 * after this line usage count == 0 */
1577 nf_ct_expect_put(exp
);
1578 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1579 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1580 struct nf_conn_help
*m_help
;
1582 /* delete all expectations for this helper */
1583 spin_lock_bh(&nf_conntrack_lock
);
1584 h
= __nf_conntrack_helper_find_byname(name
);
1586 spin_unlock_bh(&nf_conntrack_lock
);
1589 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1590 hlist_for_each_entry_safe(exp
, n
, next
,
1591 &init_net
.ct
.expect_hash
[i
],
1593 m_help
= nfct_help(exp
->master
);
1594 if (m_help
->helper
== h
1595 && del_timer(&exp
->timeout
)) {
1596 nf_ct_unlink_expect(exp
);
1597 nf_ct_expect_put(exp
);
1601 spin_unlock_bh(&nf_conntrack_lock
);
1603 /* This basically means we have to flush everything*/
1604 spin_lock_bh(&nf_conntrack_lock
);
1605 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1606 hlist_for_each_entry_safe(exp
, n
, next
,
1607 &init_net
.ct
.expect_hash
[i
],
1609 if (del_timer(&exp
->timeout
)) {
1610 nf_ct_unlink_expect(exp
);
1611 nf_ct_expect_put(exp
);
1615 spin_unlock_bh(&nf_conntrack_lock
);
1621 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1627 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
)
1629 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1630 struct nf_conntrack_tuple_hash
*h
= NULL
;
1631 struct nf_conntrack_expect
*exp
;
1633 struct nf_conn_help
*help
;
1636 /* caller guarantees that those three CTA_EXPECT_* exist */
1637 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1640 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1643 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1647 /* Look for master conntrack of this expectation */
1648 h
= nf_conntrack_find_get(&init_net
, &master_tuple
);
1651 ct
= nf_ct_tuplehash_to_ctrack(h
);
1652 help
= nfct_help(ct
);
1654 if (!help
|| !help
->helper
) {
1655 /* such conntrack hasn't got any helper, abort */
1660 exp
= nf_ct_expect_alloc(ct
);
1666 exp
->expectfn
= NULL
;
1670 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1671 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1672 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1674 err
= nf_ct_expect_related(exp
);
1675 nf_ct_expect_put(exp
);
1678 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1683 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1684 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1686 struct nf_conntrack_tuple tuple
;
1687 struct nf_conntrack_expect
*exp
;
1688 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1689 u_int8_t u3
= nfmsg
->nfgen_family
;
1692 if (!cda
[CTA_EXPECT_TUPLE
]
1693 || !cda
[CTA_EXPECT_MASK
]
1694 || !cda
[CTA_EXPECT_MASTER
])
1697 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1701 spin_lock_bh(&nf_conntrack_lock
);
1702 exp
= __nf_ct_expect_find(&init_net
, &tuple
);
1705 spin_unlock_bh(&nf_conntrack_lock
);
1707 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1708 err
= ctnetlink_create_expect(cda
, u3
);
1713 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1714 err
= ctnetlink_change_expect(exp
, cda
);
1715 spin_unlock_bh(&nf_conntrack_lock
);
1720 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1721 static struct notifier_block ctnl_notifier
= {
1722 .notifier_call
= ctnetlink_conntrack_event
,
1725 static struct notifier_block ctnl_notifier_exp
= {
1726 .notifier_call
= ctnetlink_expect_event
,
1730 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1731 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1732 .attr_count
= CTA_MAX
,
1733 .policy
= ct_nla_policy
},
1734 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1735 .attr_count
= CTA_MAX
,
1736 .policy
= ct_nla_policy
},
1737 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1738 .attr_count
= CTA_MAX
,
1739 .policy
= ct_nla_policy
},
1740 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1741 .attr_count
= CTA_MAX
,
1742 .policy
= ct_nla_policy
},
1745 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1746 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1747 .attr_count
= CTA_EXPECT_MAX
,
1748 .policy
= exp_nla_policy
},
1749 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1750 .attr_count
= CTA_EXPECT_MAX
,
1751 .policy
= exp_nla_policy
},
1752 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1753 .attr_count
= CTA_EXPECT_MAX
,
1754 .policy
= exp_nla_policy
},
1757 static const struct nfnetlink_subsystem ctnl_subsys
= {
1758 .name
= "conntrack",
1759 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1760 .cb_count
= IPCTNL_MSG_MAX
,
1764 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1765 .name
= "conntrack_expect",
1766 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1767 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1771 MODULE_ALIAS("ip_conntrack_netlink");
1772 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1773 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1775 static int __init
ctnetlink_init(void)
1779 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1780 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1782 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1786 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1788 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1789 goto err_unreg_subsys
;
1792 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1793 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1795 printk("ctnetlink_init: cannot register notifier.\n");
1796 goto err_unreg_exp_subsys
;
1799 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1801 printk("ctnetlink_init: cannot expect register notifier.\n");
1802 goto err_unreg_notifier
;
1808 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1810 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1811 err_unreg_exp_subsys
:
1812 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1815 nfnetlink_subsys_unregister(&ctnl_subsys
);
1820 static void __exit
ctnetlink_exit(void)
1822 printk("ctnetlink: unregistering from nfnetlink.\n");
1824 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1825 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1826 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1829 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1830 nfnetlink_subsys_unregister(&ctnl_subsys
);
1834 module_init(ctnetlink_init
);
1835 module_exit(ctnetlink_exit
);