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-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
9 * I've reworked this stuff to use attributes instead of conntrack
10 * structures. 5.44 am. I need more tea. --pablo 05/07/11.
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
20 * Derived from ip_conntrack_netlink.c: Port by Pablo Neira Ayuso (05/11/14)
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/timer.h>
28 #include <linux/skbuff.h>
29 #include <linux/errno.h>
30 #include <linux/netlink.h>
31 #include <linux/spinlock.h>
32 #include <linux/interrupt.h>
33 #include <linux/notifier.h>
35 #include <linux/netfilter.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_helper.h>
39 #include <net/netfilter/nf_conntrack_l3proto.h>
40 #include <net/netfilter/nf_conntrack_protocol.h>
41 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
43 #include <linux/netfilter/nfnetlink.h>
44 #include <linux/netfilter/nfnetlink_conntrack.h>
46 MODULE_LICENSE("GPL");
48 static char __initdata version
[] = "0.93";
53 #define DEBUGP(format, args...)
58 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
59 const struct nf_conntrack_tuple
*tuple
,
60 struct nf_conntrack_protocol
*proto
)
63 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
65 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
67 if (likely(proto
->tuple_to_nfattr
))
68 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
70 NFA_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 nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
86 if (likely(l3proto
->tuple_to_nfattr
))
87 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
89 NFA_NEST_END(skb
, nest_parms
);
98 ctnetlink_dump_tuples(struct sk_buff
*skb
,
99 const struct nf_conntrack_tuple
*tuple
)
102 struct nf_conntrack_l3proto
*l3proto
;
103 struct nf_conntrack_protocol
*proto
;
105 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
106 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
107 nf_ct_l3proto_put(l3proto
);
109 if (unlikely(ret
< 0))
112 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
113 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, proto
);
114 nf_ct_proto_put(proto
);
120 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
122 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
123 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
131 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
133 long timeout_l
= ct
->timeout
.expires
- jiffies
;
139 timeout
= htonl(timeout_l
/ HZ
);
141 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
149 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
151 struct nf_conntrack_protocol
*proto
= nf_ct_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
, ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
152 struct nfattr
*nest_proto
;
155 if (!proto
->to_nfattr
) {
156 nf_ct_proto_put(proto
);
160 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
162 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
164 nf_ct_proto_put(proto
);
166 NFA_NEST_END(skb
, nest_proto
);
175 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
177 struct nfattr
*nest_helper
;
178 const struct nf_conn_help
*help
= nfct_help(ct
);
180 if (!help
|| !help
->helper
)
183 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
184 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(help
->helper
->name
), help
->helper
->name
);
186 if (help
->helper
->to_nfattr
)
187 help
->helper
->to_nfattr(skb
, ct
);
189 NFA_NEST_END(skb
, nest_helper
);
197 #ifdef CONFIG_NF_CT_ACCT
199 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
200 enum ip_conntrack_dir dir
)
202 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
203 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
206 tmp
= htonl(ct
->counters
[dir
].packets
);
207 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
209 tmp
= htonl(ct
->counters
[dir
].bytes
);
210 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
212 NFA_NEST_END(skb
, nest_count
);
220 #define ctnetlink_dump_counters(a, b, c) (0)
223 #ifdef CONFIG_NF_CONNTRACK_MARK
225 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
227 u_int32_t mark
= htonl(ct
->mark
);
229 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
236 #define ctnetlink_dump_mark(a, b) (0)
240 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
242 u_int32_t id
= htonl(ct
->id
);
243 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
251 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
253 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
255 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
262 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
265 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
266 int event
, int nowait
,
267 const struct nf_conn
*ct
)
269 struct nlmsghdr
*nlh
;
270 struct nfgenmsg
*nfmsg
;
271 struct nfattr
*nest_parms
;
276 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
277 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
278 nfmsg
= NLMSG_DATA(nlh
);
280 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
281 nfmsg
->nfgen_family
=
282 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
283 nfmsg
->version
= NFNETLINK_V0
;
286 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
287 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
289 NFA_NEST_END(skb
, nest_parms
);
291 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
292 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
294 NFA_NEST_END(skb
, nest_parms
);
296 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
297 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
298 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
299 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
300 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
301 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
302 ctnetlink_dump_mark(skb
, ct
) < 0 ||
303 ctnetlink_dump_id(skb
, ct
) < 0 ||
304 ctnetlink_dump_use(skb
, ct
) < 0)
307 nlh
->nlmsg_len
= skb
->tail
- b
;
312 skb_trim(skb
, b
- skb
->data
);
316 #ifdef CONFIG_NF_CONNTRACK_EVENTS
317 static int ctnetlink_conntrack_event(struct notifier_block
*this,
318 unsigned long events
, void *ptr
)
320 struct nlmsghdr
*nlh
;
321 struct nfgenmsg
*nfmsg
;
322 struct nfattr
*nest_parms
;
323 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
327 unsigned int flags
= 0, group
;
329 /* ignore our fake conntrack entry */
330 if (ct
== &nf_conntrack_untracked
)
333 if (events
& IPCT_DESTROY
) {
334 type
= IPCTNL_MSG_CT_DELETE
;
335 group
= NFNLGRP_CONNTRACK_DESTROY
;
336 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
337 type
= IPCTNL_MSG_CT_NEW
;
338 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
339 /* dump everything */
341 group
= NFNLGRP_CONNTRACK_NEW
;
342 } else if (events
& (IPCT_STATUS
|
347 type
= IPCTNL_MSG_CT_NEW
;
348 group
= NFNLGRP_CONNTRACK_UPDATE
;
352 if (!nfnetlink_has_listeners(group
))
355 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
361 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
362 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
363 nfmsg
= NLMSG_DATA(nlh
);
365 nlh
->nlmsg_flags
= flags
;
366 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
367 nfmsg
->version
= NFNETLINK_V0
;
370 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
371 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
373 NFA_NEST_END(skb
, nest_parms
);
375 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
376 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
378 NFA_NEST_END(skb
, nest_parms
);
380 /* NAT stuff is now a status flag */
381 if ((events
& IPCT_STATUS
|| events
& IPCT_NATINFO
)
382 && ctnetlink_dump_status(skb
, ct
) < 0)
384 if (events
& IPCT_REFRESH
385 && ctnetlink_dump_timeout(skb
, ct
) < 0)
387 if (events
& IPCT_PROTOINFO
388 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
390 if (events
& IPCT_HELPINFO
391 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
394 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
395 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
398 nlh
->nlmsg_len
= skb
->tail
- b
;
399 nfnetlink_send(skb
, 0, group
, 0);
407 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
409 static int ctnetlink_done(struct netlink_callback
*cb
)
412 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
413 DEBUGP("entered %s\n", __FUNCTION__
);
417 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
420 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
422 struct nf_conn
*ct
, *last
;
423 struct nf_conntrack_tuple_hash
*h
;
425 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
426 u_int8_t l3proto
= nfmsg
->nfgen_family
;
428 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
431 read_lock_bh(&nf_conntrack_lock
);
432 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
434 last
= (struct nf_conn
*)cb
->args
[1];
435 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
436 h
= (struct nf_conntrack_tuple_hash
*) i
;
437 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
439 ct
= nf_ct_tuplehash_to_ctrack(h
);
440 /* Dump entries of a given L3 protocol number.
441 * If it is not specified, ie. l3proto == 0,
442 * then dump everything. */
443 if (l3proto
&& L3PROTO(ct
) != l3proto
)
453 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
457 nf_conntrack_get(&ct
->ct_general
);
458 cb
->args
[1] = (unsigned long)ct
;
469 read_unlock_bh(&nf_conntrack_lock
);
471 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
476 #ifdef CONFIG_NF_CT_ACCT
478 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
480 struct nf_conn
*ct
= NULL
;
481 struct nf_conntrack_tuple_hash
*h
;
483 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
484 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
485 u_int8_t l3proto
= nfmsg
->nfgen_family
;
487 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
490 write_lock_bh(&nf_conntrack_lock
);
491 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
492 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
493 h
= (struct nf_conntrack_tuple_hash
*) i
;
494 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
496 ct
= nf_ct_tuplehash_to_ctrack(h
);
497 if (l3proto
&& L3PROTO(ct
) != l3proto
)
501 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
508 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
512 write_unlock_bh(&nf_conntrack_lock
);
514 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
521 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
523 struct nfattr
*tb
[CTA_IP_MAX
];
524 struct nf_conntrack_l3proto
*l3proto
;
527 DEBUGP("entered %s\n", __FUNCTION__
);
529 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
531 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
533 if (likely(l3proto
->nfattr_to_tuple
))
534 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
536 nf_ct_l3proto_put(l3proto
);
543 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
544 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
548 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
549 struct nf_conntrack_tuple
*tuple
)
551 struct nfattr
*tb
[CTA_PROTO_MAX
];
552 struct nf_conntrack_protocol
*proto
;
555 DEBUGP("entered %s\n", __FUNCTION__
);
557 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
559 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
562 if (!tb
[CTA_PROTO_NUM
-1])
564 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
566 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
568 if (likely(proto
->nfattr_to_tuple
))
569 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
571 nf_ct_proto_put(proto
);
577 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
578 enum ctattr_tuple type
, u_int8_t l3num
)
580 struct nfattr
*tb
[CTA_TUPLE_MAX
];
583 DEBUGP("entered %s\n", __FUNCTION__
);
585 memset(tuple
, 0, sizeof(*tuple
));
587 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
589 if (!tb
[CTA_TUPLE_IP
-1])
592 tuple
->src
.l3num
= l3num
;
594 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
598 if (!tb
[CTA_TUPLE_PROTO
-1])
601 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
605 /* orig and expect tuples get DIR_ORIGINAL */
606 if (type
== CTA_TUPLE_REPLY
)
607 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
609 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
611 NF_CT_DUMP_TUPLE(tuple
);
618 #ifdef CONFIG_IP_NF_NAT_NEEDED
619 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
620 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
621 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
624 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
625 const struct nf_conn
*ct
,
626 struct ip_nat_range
*range
)
628 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
629 struct ip_nat_protocol
*npt
;
631 DEBUGP("entered %s\n", __FUNCTION__
);
633 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
635 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
638 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
640 if (!npt
->nfattr_to_range
) {
641 ip_nat_proto_put(npt
);
645 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
646 if (npt
->nfattr_to_range(tb
, range
) > 0)
647 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
649 ip_nat_proto_put(npt
);
655 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
656 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
657 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
661 ctnetlink_parse_nat(struct nfattr
*nat
,
662 const struct nf_conn
*ct
, struct ip_nat_range
*range
)
664 struct nfattr
*tb
[CTA_NAT_MAX
];
667 DEBUGP("entered %s\n", __FUNCTION__
);
669 memset(range
, 0, sizeof(*range
));
671 nfattr_parse_nested(tb
, CTA_NAT_MAX
, nat
);
673 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
676 if (tb
[CTA_NAT_MINIP
-1])
677 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
679 if (!tb
[CTA_NAT_MAXIP
-1])
680 range
->max_ip
= range
->min_ip
;
682 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
685 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
687 if (!tb
[CTA_NAT_PROTO
-1])
690 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
700 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
702 struct nfattr
*tb
[CTA_HELP_MAX
];
704 DEBUGP("entered %s\n", __FUNCTION__
);
706 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
708 if (!tb
[CTA_HELP_NAME
-1])
711 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
716 static const size_t cta_min
[CTA_MAX
] = {
717 [CTA_STATUS
-1] = sizeof(u_int32_t
),
718 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
719 [CTA_MARK
-1] = sizeof(u_int32_t
),
720 [CTA_USE
-1] = sizeof(u_int32_t
),
721 [CTA_ID
-1] = sizeof(u_int32_t
)
725 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
726 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
728 struct nf_conntrack_tuple_hash
*h
;
729 struct nf_conntrack_tuple tuple
;
731 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
732 u_int8_t u3
= nfmsg
->nfgen_family
;
735 DEBUGP("entered %s\n", __FUNCTION__
);
737 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
740 if (cda
[CTA_TUPLE_ORIG
-1])
741 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
742 else if (cda
[CTA_TUPLE_REPLY
-1])
743 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
745 /* Flush the whole table */
746 nf_conntrack_flush();
753 h
= nf_conntrack_find_get(&tuple
, NULL
);
755 DEBUGP("tuple not found in conntrack hash\n");
759 ct
= nf_ct_tuplehash_to_ctrack(h
);
762 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
768 if (del_timer(&ct
->timeout
))
769 ct
->timeout
.function((unsigned long)ct
);
778 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
779 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
781 struct nf_conntrack_tuple_hash
*h
;
782 struct nf_conntrack_tuple tuple
;
784 struct sk_buff
*skb2
= NULL
;
785 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
786 u_int8_t u3
= nfmsg
->nfgen_family
;
789 DEBUGP("entered %s\n", __FUNCTION__
);
791 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
794 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
795 IPCTNL_MSG_CT_GET_CTRZERO
) {
796 #ifdef CONFIG_NF_CT_ACCT
797 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
798 ctnetlink_dump_table_w
,
799 ctnetlink_done
)) != 0)
805 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
806 ctnetlink_dump_table
,
807 ctnetlink_done
)) != 0)
811 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
818 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
821 if (cda
[CTA_TUPLE_ORIG
-1])
822 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
823 else if (cda
[CTA_TUPLE_REPLY
-1])
824 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
831 h
= nf_conntrack_find_get(&tuple
, NULL
);
833 DEBUGP("tuple not found in conntrack hash");
836 DEBUGP("tuple found\n");
837 ct
= nf_ct_tuplehash_to_ctrack(h
);
840 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
845 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
847 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
848 IPCTNL_MSG_CT_NEW
, 1, ct
);
853 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
867 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
870 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
871 d
= ct
->status
^ status
;
873 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
877 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
878 /* SEEN_REPLY bit can only be set */
882 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
883 /* ASSURED bit can only be set */
886 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
887 #ifndef CONFIG_IP_NF_NAT_NEEDED
890 struct ip_nat_range range
;
892 if (cda
[CTA_NAT_DST
-1]) {
893 if (ctnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
896 if (ip_nat_initialized(ct
,
897 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
899 ip_nat_setup_info(ct
, &range
, hooknum
);
901 if (cda
[CTA_NAT_SRC
-1]) {
902 if (ctnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
905 if (ip_nat_initialized(ct
,
906 HOOK2MANIP(NF_IP_POST_ROUTING
)))
908 ip_nat_setup_info(ct
, &range
, hooknum
);
913 /* Be careful here, modifying NAT bits can screw up things,
914 * so don't let users modify them directly if they don't pass
916 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
922 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
924 struct nf_conntrack_helper
*helper
;
925 struct nf_conn_help
*help
= nfct_help(ct
);
929 DEBUGP("entered %s\n", __FUNCTION__
);
932 /* FIXME: we need to reallocate and rehash */
936 /* don't change helper of sibling connections */
940 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
944 helper
= __nf_conntrack_helper_find_byname(helpname
);
946 if (!strcmp(helpname
, ""))
954 /* we had a helper before ... */
955 nf_ct_remove_expectations(ct
);
958 /* need to zero data of old helper */
959 memset(&help
->help
, 0, sizeof(help
->help
));
963 help
->helper
= helper
;
969 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
971 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
973 if (!del_timer(&ct
->timeout
))
976 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
977 add_timer(&ct
->timeout
);
983 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
985 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
986 struct nf_conntrack_protocol
*proto
;
987 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
988 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
991 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
993 proto
= nf_ct_proto_find_get(l3num
, npt
);
995 if (proto
->from_nfattr
)
996 err
= proto
->from_nfattr(tb
, ct
);
997 nf_ct_proto_put(proto
);
1003 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
1007 DEBUGP("entered %s\n", __FUNCTION__
);
1009 if (cda
[CTA_HELP
-1]) {
1010 err
= ctnetlink_change_helper(ct
, cda
);
1015 if (cda
[CTA_TIMEOUT
-1]) {
1016 err
= ctnetlink_change_timeout(ct
, cda
);
1021 if (cda
[CTA_STATUS
-1]) {
1022 err
= ctnetlink_change_status(ct
, cda
);
1027 if (cda
[CTA_PROTOINFO
-1]) {
1028 err
= ctnetlink_change_protoinfo(ct
, cda
);
1033 #if defined(CONFIG_NF_CONNTRACK_MARK)
1034 if (cda
[CTA_MARK
-1])
1035 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1038 DEBUGP("all done\n");
1043 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1044 struct nf_conntrack_tuple
*otuple
,
1045 struct nf_conntrack_tuple
*rtuple
)
1050 DEBUGP("entered %s\n", __FUNCTION__
);
1052 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1053 if (ct
== NULL
|| IS_ERR(ct
))
1056 if (!cda
[CTA_TIMEOUT
-1])
1058 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1060 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1061 ct
->status
|= IPS_CONFIRMED
;
1063 err
= ctnetlink_change_status(ct
, cda
);
1067 if (cda
[CTA_PROTOINFO
-1]) {
1068 err
= ctnetlink_change_protoinfo(ct
, cda
);
1073 #if defined(CONFIG_NF_CONNTRACK_MARK)
1074 if (cda
[CTA_MARK
-1])
1075 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1078 add_timer(&ct
->timeout
);
1079 nf_conntrack_hash_insert(ct
);
1081 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1085 nf_conntrack_free(ct
);
1090 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1091 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1093 struct nf_conntrack_tuple otuple
, rtuple
;
1094 struct nf_conntrack_tuple_hash
*h
= NULL
;
1095 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1096 u_int8_t u3
= nfmsg
->nfgen_family
;
1099 DEBUGP("entered %s\n", __FUNCTION__
);
1101 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1104 if (cda
[CTA_TUPLE_ORIG
-1]) {
1105 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1110 if (cda
[CTA_TUPLE_REPLY
-1]) {
1111 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1116 write_lock_bh(&nf_conntrack_lock
);
1117 if (cda
[CTA_TUPLE_ORIG
-1])
1118 h
= __nf_conntrack_find(&otuple
, NULL
);
1119 else if (cda
[CTA_TUPLE_REPLY
-1])
1120 h
= __nf_conntrack_find(&rtuple
, NULL
);
1123 write_unlock_bh(&nf_conntrack_lock
);
1124 DEBUGP("no such conntrack, create new\n");
1126 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1127 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1130 /* implicit 'else' */
1132 /* we only allow nat config for new conntracks */
1133 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1138 /* We manipulate the conntrack inside the global conntrack table lock,
1139 * so there's no need to increase the refcount */
1140 DEBUGP("conntrack found\n");
1142 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1143 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1146 write_unlock_bh(&nf_conntrack_lock
);
1150 /***********************************************************************
1152 ***********************************************************************/
1155 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1156 const struct nf_conntrack_tuple
*tuple
,
1157 enum ctattr_expect type
)
1159 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1161 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1162 goto nfattr_failure
;
1164 NFA_NEST_END(skb
, nest_parms
);
1173 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1174 const struct nf_conntrack_tuple
*tuple
,
1175 const struct nf_conntrack_tuple
*mask
)
1178 struct nf_conntrack_l3proto
*l3proto
;
1179 struct nf_conntrack_protocol
*proto
;
1180 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1182 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1183 ret
= ctnetlink_dump_tuples_ip(skb
, mask
, l3proto
);
1184 nf_ct_l3proto_put(l3proto
);
1186 if (unlikely(ret
< 0))
1187 goto nfattr_failure
;
1189 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1190 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1191 nf_ct_proto_put(proto
);
1192 if (unlikely(ret
< 0))
1193 goto nfattr_failure
;
1195 NFA_NEST_END(skb
, nest_parms
);
1204 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1205 const struct nf_conntrack_expect
*exp
)
1207 struct nf_conn
*master
= exp
->master
;
1208 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1209 u_int32_t id
= htonl(exp
->id
);
1211 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1212 goto nfattr_failure
;
1213 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1214 goto nfattr_failure
;
1215 if (ctnetlink_exp_dump_tuple(skb
,
1216 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1217 CTA_EXPECT_MASTER
) < 0)
1218 goto nfattr_failure
;
1220 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1221 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1230 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1233 const struct nf_conntrack_expect
*exp
)
1235 struct nlmsghdr
*nlh
;
1236 struct nfgenmsg
*nfmsg
;
1241 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1242 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1243 nfmsg
= NLMSG_DATA(nlh
);
1245 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1246 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1247 nfmsg
->version
= NFNETLINK_V0
;
1250 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1251 goto nfattr_failure
;
1253 nlh
->nlmsg_len
= skb
->tail
- b
;
1258 skb_trim(skb
, b
- skb
->data
);
1262 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1263 static int ctnetlink_expect_event(struct notifier_block
*this,
1264 unsigned long events
, void *ptr
)
1266 struct nlmsghdr
*nlh
;
1267 struct nfgenmsg
*nfmsg
;
1268 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1269 struct sk_buff
*skb
;
1274 if (events
& IPEXP_NEW
) {
1275 type
= IPCTNL_MSG_EXP_NEW
;
1276 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1280 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1286 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1287 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1288 nfmsg
= NLMSG_DATA(nlh
);
1290 nlh
->nlmsg_flags
= flags
;
1291 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1292 nfmsg
->version
= NFNETLINK_V0
;
1295 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1296 goto nfattr_failure
;
1298 nlh
->nlmsg_len
= skb
->tail
- b
;
1299 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1310 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1312 struct nf_conntrack_expect
*exp
= NULL
;
1313 struct list_head
*i
;
1314 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1315 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1316 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1318 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1320 read_lock_bh(&nf_conntrack_lock
);
1321 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1322 exp
= (struct nf_conntrack_expect
*) i
;
1323 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1327 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1335 read_unlock_bh(&nf_conntrack_lock
);
1337 DEBUGP("leaving, last id=%llu\n", *id
);
1342 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1343 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1344 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1348 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1349 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1351 struct nf_conntrack_tuple tuple
;
1352 struct nf_conntrack_expect
*exp
;
1353 struct sk_buff
*skb2
;
1354 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1355 u_int8_t u3
= nfmsg
->nfgen_family
;
1358 DEBUGP("entered %s\n", __FUNCTION__
);
1360 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1363 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1366 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1367 ctnetlink_exp_dump_table
,
1368 ctnetlink_done
)) != 0)
1370 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1371 if (rlen
> skb
->len
)
1373 skb_pull(skb
, rlen
);
1377 if (cda
[CTA_EXPECT_MASTER
-1])
1378 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1385 exp
= nf_conntrack_expect_find(&tuple
);
1389 if (cda
[CTA_EXPECT_ID
-1]) {
1390 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1391 if (exp
->id
!= ntohl(id
)) {
1392 nf_conntrack_expect_put(exp
);
1398 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1401 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1403 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1404 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1409 nf_conntrack_expect_put(exp
);
1411 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1416 nf_conntrack_expect_put(exp
);
1421 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1422 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1424 struct nf_conntrack_expect
*exp
, *tmp
;
1425 struct nf_conntrack_tuple tuple
;
1426 struct nf_conntrack_helper
*h
;
1427 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1428 u_int8_t u3
= nfmsg
->nfgen_family
;
1431 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1434 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1435 /* delete a single expect by tuple */
1436 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1440 /* bump usage count to 2 */
1441 exp
= nf_conntrack_expect_find(&tuple
);
1445 if (cda
[CTA_EXPECT_ID
-1]) {
1447 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1448 if (exp
->id
!= ntohl(id
)) {
1449 nf_conntrack_expect_put(exp
);
1454 /* after list removal, usage count == 1 */
1455 nf_conntrack_unexpect_related(exp
);
1456 /* have to put what we 'get' above.
1457 * after this line usage count == 0 */
1458 nf_conntrack_expect_put(exp
);
1459 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1460 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1462 /* delete all expectations for this helper */
1463 write_lock_bh(&nf_conntrack_lock
);
1464 h
= __nf_conntrack_helper_find_byname(name
);
1466 write_unlock_bh(&nf_conntrack_lock
);
1469 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1471 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1472 if (m_help
->helper
== h
1473 && del_timer(&exp
->timeout
)) {
1474 nf_ct_unlink_expect(exp
);
1475 nf_conntrack_expect_put(exp
);
1478 write_unlock_bh(&nf_conntrack_lock
);
1480 /* This basically means we have to flush everything*/
1481 write_lock_bh(&nf_conntrack_lock
);
1482 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1484 if (del_timer(&exp
->timeout
)) {
1485 nf_ct_unlink_expect(exp
);
1486 nf_conntrack_expect_put(exp
);
1489 write_unlock_bh(&nf_conntrack_lock
);
1495 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1501 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1503 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1504 struct nf_conntrack_tuple_hash
*h
= NULL
;
1505 struct nf_conntrack_expect
*exp
;
1507 struct nf_conn_help
*help
;
1510 DEBUGP("entered %s\n", __FUNCTION__
);
1512 /* caller guarantees that those three CTA_EXPECT_* exist */
1513 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1516 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1519 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1523 /* Look for master conntrack of this expectation */
1524 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1527 ct
= nf_ct_tuplehash_to_ctrack(h
);
1528 help
= nfct_help(ct
);
1530 if (!help
|| !help
->helper
) {
1531 /* such conntrack hasn't got any helper, abort */
1536 exp
= nf_conntrack_expect_alloc(ct
);
1542 exp
->expectfn
= NULL
;
1545 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1546 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1548 err
= nf_conntrack_expect_related(exp
);
1549 nf_conntrack_expect_put(exp
);
1552 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1557 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1558 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1560 struct nf_conntrack_tuple tuple
;
1561 struct nf_conntrack_expect
*exp
;
1562 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1563 u_int8_t u3
= nfmsg
->nfgen_family
;
1566 DEBUGP("entered %s\n", __FUNCTION__
);
1568 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1571 if (!cda
[CTA_EXPECT_TUPLE
-1]
1572 || !cda
[CTA_EXPECT_MASK
-1]
1573 || !cda
[CTA_EXPECT_MASTER
-1])
1576 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1580 write_lock_bh(&nf_conntrack_lock
);
1581 exp
= __nf_conntrack_expect_find(&tuple
);
1584 write_unlock_bh(&nf_conntrack_lock
);
1586 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1587 err
= ctnetlink_create_expect(cda
, u3
);
1592 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1593 err
= ctnetlink_change_expect(exp
, cda
);
1594 write_unlock_bh(&nf_conntrack_lock
);
1596 DEBUGP("leaving\n");
1601 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1602 static struct notifier_block ctnl_notifier
= {
1603 .notifier_call
= ctnetlink_conntrack_event
,
1606 static struct notifier_block ctnl_notifier_exp
= {
1607 .notifier_call
= ctnetlink_expect_event
,
1611 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1612 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1613 .attr_count
= CTA_MAX
, },
1614 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1615 .attr_count
= CTA_MAX
, },
1616 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1617 .attr_count
= CTA_MAX
, },
1618 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1619 .attr_count
= CTA_MAX
, },
1622 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1623 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1624 .attr_count
= CTA_EXPECT_MAX
, },
1625 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1626 .attr_count
= CTA_EXPECT_MAX
, },
1627 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1628 .attr_count
= CTA_EXPECT_MAX
, },
1631 static struct nfnetlink_subsystem ctnl_subsys
= {
1632 .name
= "conntrack",
1633 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1634 .cb_count
= IPCTNL_MSG_MAX
,
1638 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1639 .name
= "conntrack_expect",
1640 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1641 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1645 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1646 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1648 static int __init
ctnetlink_init(void)
1652 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1653 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1655 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1659 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1661 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1662 goto err_unreg_subsys
;
1665 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1666 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1668 printk("ctnetlink_init: cannot register notifier.\n");
1669 goto err_unreg_exp_subsys
;
1672 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1674 printk("ctnetlink_init: cannot expect register notifier.\n");
1675 goto err_unreg_notifier
;
1681 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1683 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1684 err_unreg_exp_subsys
:
1685 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1688 nfnetlink_subsys_unregister(&ctnl_subsys
);
1693 static void __exit
ctnetlink_exit(void)
1695 printk("ctnetlink: unregistering from nfnetlink.\n");
1697 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1698 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1699 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1702 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1703 nfnetlink_subsys_unregister(&ctnl_subsys
);
1707 module_init(ctnetlink_init
);
1708 module_exit(ctnetlink_exit
);