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/notifier.h>
34 #include <linux/netfilter.h>
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_core.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
42 #include <linux/netfilter/nfnetlink.h>
43 #include <linux/netfilter/nfnetlink_conntrack.h>
45 MODULE_LICENSE("GPL");
47 static char __initdata version
[] = "0.93";
52 #define DEBUGP(format, args...)
57 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
58 const struct nf_conntrack_tuple
*tuple
,
59 struct nf_conntrack_protocol
*proto
)
62 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
64 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
66 if (likely(proto
->tuple_to_nfattr
))
67 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
69 NFA_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 nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
85 if (likely(l3proto
->tuple_to_nfattr
))
86 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
88 NFA_NEST_END(skb
, nest_parms
);
97 ctnetlink_dump_tuples(struct sk_buff
*skb
,
98 const struct nf_conntrack_tuple
*tuple
)
101 struct nf_conntrack_l3proto
*l3proto
;
102 struct nf_conntrack_protocol
*proto
;
104 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
105 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
106 nf_ct_l3proto_put(l3proto
);
108 if (unlikely(ret
< 0))
111 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
112 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, proto
);
113 nf_ct_proto_put(proto
);
119 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
121 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
122 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
130 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
132 long timeout_l
= ct
->timeout
.expires
- jiffies
;
138 timeout
= htonl(timeout_l
/ HZ
);
140 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
148 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
150 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
);
151 struct nfattr
*nest_proto
;
154 if (!proto
->to_nfattr
) {
155 nf_ct_proto_put(proto
);
159 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
161 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
163 nf_ct_proto_put(proto
);
165 NFA_NEST_END(skb
, nest_proto
);
174 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
176 struct nfattr
*nest_helper
;
177 const struct nf_conn_help
*help
= nfct_help(ct
);
179 if (!help
|| !help
->helper
)
182 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
183 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(help
->helper
->name
), help
->helper
->name
);
185 if (help
->helper
->to_nfattr
)
186 help
->helper
->to_nfattr(skb
, ct
);
188 NFA_NEST_END(skb
, nest_helper
);
196 #ifdef CONFIG_NF_CT_ACCT
198 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
199 enum ip_conntrack_dir dir
)
201 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
202 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
205 tmp
= htonl(ct
->counters
[dir
].packets
);
206 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
208 tmp
= htonl(ct
->counters
[dir
].bytes
);
209 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
211 NFA_NEST_END(skb
, nest_count
);
219 #define ctnetlink_dump_counters(a, b, c) (0)
222 #ifdef CONFIG_NF_CONNTRACK_MARK
224 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
226 u_int32_t mark
= htonl(ct
->mark
);
228 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
235 #define ctnetlink_dump_mark(a, b) (0)
239 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
241 u_int32_t id
= htonl(ct
->id
);
242 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
250 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
252 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
254 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
261 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
264 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
265 int event
, int nowait
,
266 const struct nf_conn
*ct
)
268 struct nlmsghdr
*nlh
;
269 struct nfgenmsg
*nfmsg
;
270 struct nfattr
*nest_parms
;
275 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
276 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
277 nfmsg
= NLMSG_DATA(nlh
);
279 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
280 nfmsg
->nfgen_family
=
281 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
282 nfmsg
->version
= NFNETLINK_V0
;
285 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
286 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
288 NFA_NEST_END(skb
, nest_parms
);
290 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
291 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
293 NFA_NEST_END(skb
, nest_parms
);
295 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
296 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
297 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
298 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
299 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
300 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
301 ctnetlink_dump_mark(skb
, ct
) < 0 ||
302 ctnetlink_dump_id(skb
, ct
) < 0 ||
303 ctnetlink_dump_use(skb
, ct
) < 0)
306 nlh
->nlmsg_len
= skb
->tail
- b
;
311 skb_trim(skb
, b
- skb
->data
);
315 #ifdef CONFIG_NF_CONNTRACK_EVENTS
316 static int ctnetlink_conntrack_event(struct notifier_block
*this,
317 unsigned long events
, void *ptr
)
319 struct nlmsghdr
*nlh
;
320 struct nfgenmsg
*nfmsg
;
321 struct nfattr
*nest_parms
;
322 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
326 unsigned int flags
= 0, group
;
328 /* ignore our fake conntrack entry */
329 if (ct
== &nf_conntrack_untracked
)
332 if (events
& IPCT_DESTROY
) {
333 type
= IPCTNL_MSG_CT_DELETE
;
334 group
= NFNLGRP_CONNTRACK_DESTROY
;
335 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
336 type
= IPCTNL_MSG_CT_NEW
;
337 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
338 /* dump everything */
340 group
= NFNLGRP_CONNTRACK_NEW
;
341 } else if (events
& (IPCT_STATUS
|
346 type
= IPCTNL_MSG_CT_NEW
;
347 group
= NFNLGRP_CONNTRACK_UPDATE
;
351 if (!nfnetlink_has_listeners(group
))
354 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
360 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
361 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
362 nfmsg
= NLMSG_DATA(nlh
);
364 nlh
->nlmsg_flags
= flags
;
365 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
366 nfmsg
->version
= NFNETLINK_V0
;
369 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
370 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
372 NFA_NEST_END(skb
, nest_parms
);
374 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
375 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
377 NFA_NEST_END(skb
, nest_parms
);
379 /* NAT stuff is now a status flag */
380 if ((events
& IPCT_STATUS
|| events
& IPCT_NATINFO
)
381 && ctnetlink_dump_status(skb
, ct
) < 0)
383 if (events
& IPCT_REFRESH
384 && ctnetlink_dump_timeout(skb
, ct
) < 0)
386 if (events
& IPCT_PROTOINFO
387 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
389 if (events
& IPCT_HELPINFO
390 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
393 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
394 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
397 nlh
->nlmsg_len
= skb
->tail
- b
;
398 nfnetlink_send(skb
, 0, group
, 0);
406 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
408 static int ctnetlink_done(struct netlink_callback
*cb
)
411 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
412 DEBUGP("entered %s\n", __FUNCTION__
);
416 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
419 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
421 struct nf_conn
*ct
, *last
;
422 struct nf_conntrack_tuple_hash
*h
;
424 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
425 u_int8_t l3proto
= nfmsg
->nfgen_family
;
427 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
430 read_lock_bh(&nf_conntrack_lock
);
431 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
433 last
= (struct nf_conn
*)cb
->args
[1];
434 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
435 h
= (struct nf_conntrack_tuple_hash
*) i
;
436 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
438 ct
= nf_ct_tuplehash_to_ctrack(h
);
439 /* Dump entries of a given L3 protocol number.
440 * If it is not specified, ie. l3proto == 0,
441 * then dump everything. */
442 if (l3proto
&& L3PROTO(ct
) != l3proto
)
452 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
456 nf_conntrack_get(&ct
->ct_general
);
457 cb
->args
[1] = (unsigned long)ct
;
468 read_unlock_bh(&nf_conntrack_lock
);
470 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
475 #ifdef CONFIG_NF_CT_ACCT
477 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
479 struct nf_conn
*ct
= NULL
;
480 struct nf_conntrack_tuple_hash
*h
;
482 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
483 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
484 u_int8_t l3proto
= nfmsg
->nfgen_family
;
486 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
489 write_lock_bh(&nf_conntrack_lock
);
490 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
491 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
492 h
= (struct nf_conntrack_tuple_hash
*) i
;
493 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
495 ct
= nf_ct_tuplehash_to_ctrack(h
);
496 if (l3proto
&& L3PROTO(ct
) != l3proto
)
500 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
507 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
511 write_unlock_bh(&nf_conntrack_lock
);
513 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
520 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
522 struct nfattr
*tb
[CTA_IP_MAX
];
523 struct nf_conntrack_l3proto
*l3proto
;
526 DEBUGP("entered %s\n", __FUNCTION__
);
528 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
530 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
532 if (likely(l3proto
->nfattr_to_tuple
))
533 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
535 nf_ct_l3proto_put(l3proto
);
542 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
543 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
547 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
548 struct nf_conntrack_tuple
*tuple
)
550 struct nfattr
*tb
[CTA_PROTO_MAX
];
551 struct nf_conntrack_protocol
*proto
;
554 DEBUGP("entered %s\n", __FUNCTION__
);
556 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
558 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
561 if (!tb
[CTA_PROTO_NUM
-1])
563 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
565 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
567 if (likely(proto
->nfattr_to_tuple
))
568 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
570 nf_ct_proto_put(proto
);
576 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
577 enum ctattr_tuple type
, u_int8_t l3num
)
579 struct nfattr
*tb
[CTA_TUPLE_MAX
];
582 DEBUGP("entered %s\n", __FUNCTION__
);
584 memset(tuple
, 0, sizeof(*tuple
));
586 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
588 if (!tb
[CTA_TUPLE_IP
-1])
591 tuple
->src
.l3num
= l3num
;
593 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
597 if (!tb
[CTA_TUPLE_PROTO
-1])
600 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
604 /* orig and expect tuples get DIR_ORIGINAL */
605 if (type
== CTA_TUPLE_REPLY
)
606 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
608 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
610 NF_CT_DUMP_TUPLE(tuple
);
617 #ifdef CONFIG_IP_NF_NAT_NEEDED
618 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
619 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
620 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
623 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
624 const struct nf_conn
*ct
,
625 struct ip_nat_range
*range
)
627 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
628 struct ip_nat_protocol
*npt
;
630 DEBUGP("entered %s\n", __FUNCTION__
);
632 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
634 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
637 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
639 if (!npt
->nfattr_to_range
) {
640 ip_nat_proto_put(npt
);
644 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
645 if (npt
->nfattr_to_range(tb
, range
) > 0)
646 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
648 ip_nat_proto_put(npt
);
654 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
655 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
656 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
660 ctnetlink_parse_nat(struct nfattr
*nat
,
661 const struct nf_conn
*ct
, struct ip_nat_range
*range
)
663 struct nfattr
*tb
[CTA_NAT_MAX
];
666 DEBUGP("entered %s\n", __FUNCTION__
);
668 memset(range
, 0, sizeof(*range
));
670 nfattr_parse_nested(tb
, CTA_NAT_MAX
, nat
);
672 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
675 if (tb
[CTA_NAT_MINIP
-1])
676 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
678 if (!tb
[CTA_NAT_MAXIP
-1])
679 range
->max_ip
= range
->min_ip
;
681 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
684 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
686 if (!tb
[CTA_NAT_PROTO
-1])
689 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
699 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
701 struct nfattr
*tb
[CTA_HELP_MAX
];
703 DEBUGP("entered %s\n", __FUNCTION__
);
705 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
707 if (!tb
[CTA_HELP_NAME
-1])
710 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
715 static const size_t cta_min
[CTA_MAX
] = {
716 [CTA_STATUS
-1] = sizeof(u_int32_t
),
717 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
718 [CTA_MARK
-1] = sizeof(u_int32_t
),
719 [CTA_USE
-1] = sizeof(u_int32_t
),
720 [CTA_ID
-1] = sizeof(u_int32_t
)
724 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
725 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
727 struct nf_conntrack_tuple_hash
*h
;
728 struct nf_conntrack_tuple tuple
;
730 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
731 u_int8_t u3
= nfmsg
->nfgen_family
;
734 DEBUGP("entered %s\n", __FUNCTION__
);
736 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
739 if (cda
[CTA_TUPLE_ORIG
-1])
740 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
741 else if (cda
[CTA_TUPLE_REPLY
-1])
742 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
744 /* Flush the whole table */
745 nf_conntrack_flush();
752 h
= nf_conntrack_find_get(&tuple
, NULL
);
754 DEBUGP("tuple not found in conntrack hash\n");
758 ct
= nf_ct_tuplehash_to_ctrack(h
);
761 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
767 if (del_timer(&ct
->timeout
))
768 ct
->timeout
.function((unsigned long)ct
);
777 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
778 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
780 struct nf_conntrack_tuple_hash
*h
;
781 struct nf_conntrack_tuple tuple
;
783 struct sk_buff
*skb2
= NULL
;
784 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
785 u_int8_t u3
= nfmsg
->nfgen_family
;
788 DEBUGP("entered %s\n", __FUNCTION__
);
790 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
793 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
794 IPCTNL_MSG_CT_GET_CTRZERO
) {
795 #ifdef CONFIG_NF_CT_ACCT
796 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
797 ctnetlink_dump_table_w
,
798 ctnetlink_done
)) != 0)
804 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
805 ctnetlink_dump_table
,
806 ctnetlink_done
)) != 0)
810 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
817 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
820 if (cda
[CTA_TUPLE_ORIG
-1])
821 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
822 else if (cda
[CTA_TUPLE_REPLY
-1])
823 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
830 h
= nf_conntrack_find_get(&tuple
, NULL
);
832 DEBUGP("tuple not found in conntrack hash");
835 DEBUGP("tuple found\n");
836 ct
= nf_ct_tuplehash_to_ctrack(h
);
839 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
844 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
846 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
847 IPCTNL_MSG_CT_NEW
, 1, ct
);
852 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
866 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
869 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
870 d
= ct
->status
^ status
;
872 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
876 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
877 /* SEEN_REPLY bit can only be set */
881 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
882 /* ASSURED bit can only be set */
885 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
886 #ifndef CONFIG_IP_NF_NAT_NEEDED
889 struct ip_nat_range range
;
891 if (cda
[CTA_NAT_DST
-1]) {
892 if (ctnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
895 if (ip_nat_initialized(ct
,
896 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
898 ip_nat_setup_info(ct
, &range
, hooknum
);
900 if (cda
[CTA_NAT_SRC
-1]) {
901 if (ctnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
904 if (ip_nat_initialized(ct
,
905 HOOK2MANIP(NF_IP_POST_ROUTING
)))
907 ip_nat_setup_info(ct
, &range
, hooknum
);
912 /* Be careful here, modifying NAT bits can screw up things,
913 * so don't let users modify them directly if they don't pass
915 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
921 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
923 struct nf_conntrack_helper
*helper
;
924 struct nf_conn_help
*help
= nfct_help(ct
);
928 DEBUGP("entered %s\n", __FUNCTION__
);
931 /* FIXME: we need to reallocate and rehash */
935 /* don't change helper of sibling connections */
939 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
943 helper
= __nf_conntrack_helper_find_byname(helpname
);
945 if (!strcmp(helpname
, ""))
953 /* we had a helper before ... */
954 nf_ct_remove_expectations(ct
);
957 /* need to zero data of old helper */
958 memset(&help
->help
, 0, sizeof(help
->help
));
962 help
->helper
= helper
;
968 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
970 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
972 if (!del_timer(&ct
->timeout
))
975 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
976 add_timer(&ct
->timeout
);
982 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
984 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
985 struct nf_conntrack_protocol
*proto
;
986 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
987 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
990 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
992 proto
= nf_ct_proto_find_get(l3num
, npt
);
994 if (proto
->from_nfattr
)
995 err
= proto
->from_nfattr(tb
, ct
);
996 nf_ct_proto_put(proto
);
1002 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
1006 DEBUGP("entered %s\n", __FUNCTION__
);
1008 if (cda
[CTA_HELP
-1]) {
1009 err
= ctnetlink_change_helper(ct
, cda
);
1014 if (cda
[CTA_TIMEOUT
-1]) {
1015 err
= ctnetlink_change_timeout(ct
, cda
);
1020 if (cda
[CTA_STATUS
-1]) {
1021 err
= ctnetlink_change_status(ct
, cda
);
1026 if (cda
[CTA_PROTOINFO
-1]) {
1027 err
= ctnetlink_change_protoinfo(ct
, cda
);
1032 #if defined(CONFIG_NF_CONNTRACK_MARK)
1033 if (cda
[CTA_MARK
-1])
1034 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1037 DEBUGP("all done\n");
1042 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1043 struct nf_conntrack_tuple
*otuple
,
1044 struct nf_conntrack_tuple
*rtuple
)
1049 DEBUGP("entered %s\n", __FUNCTION__
);
1051 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1052 if (ct
== NULL
|| IS_ERR(ct
))
1055 if (!cda
[CTA_TIMEOUT
-1])
1057 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1059 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1060 ct
->status
|= IPS_CONFIRMED
;
1062 err
= ctnetlink_change_status(ct
, cda
);
1066 if (cda
[CTA_PROTOINFO
-1]) {
1067 err
= ctnetlink_change_protoinfo(ct
, cda
);
1072 #if defined(CONFIG_NF_CONNTRACK_MARK)
1073 if (cda
[CTA_MARK
-1])
1074 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1077 add_timer(&ct
->timeout
);
1078 nf_conntrack_hash_insert(ct
);
1080 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1084 nf_conntrack_free(ct
);
1089 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1090 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1092 struct nf_conntrack_tuple otuple
, rtuple
;
1093 struct nf_conntrack_tuple_hash
*h
= NULL
;
1094 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1095 u_int8_t u3
= nfmsg
->nfgen_family
;
1098 DEBUGP("entered %s\n", __FUNCTION__
);
1100 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1103 if (cda
[CTA_TUPLE_ORIG
-1]) {
1104 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1109 if (cda
[CTA_TUPLE_REPLY
-1]) {
1110 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1115 write_lock_bh(&nf_conntrack_lock
);
1116 if (cda
[CTA_TUPLE_ORIG
-1])
1117 h
= __nf_conntrack_find(&otuple
, NULL
);
1118 else if (cda
[CTA_TUPLE_REPLY
-1])
1119 h
= __nf_conntrack_find(&rtuple
, NULL
);
1122 write_unlock_bh(&nf_conntrack_lock
);
1123 DEBUGP("no such conntrack, create new\n");
1125 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1126 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1129 /* implicit 'else' */
1131 /* we only allow nat config for new conntracks */
1132 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1137 /* We manipulate the conntrack inside the global conntrack table lock,
1138 * so there's no need to increase the refcount */
1139 DEBUGP("conntrack found\n");
1141 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1142 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1145 write_unlock_bh(&nf_conntrack_lock
);
1149 /***********************************************************************
1151 ***********************************************************************/
1154 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1155 const struct nf_conntrack_tuple
*tuple
,
1156 enum ctattr_expect type
)
1158 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1160 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1161 goto nfattr_failure
;
1163 NFA_NEST_END(skb
, nest_parms
);
1172 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1173 const struct nf_conntrack_tuple
*tuple
,
1174 const struct nf_conntrack_tuple
*mask
)
1177 struct nf_conntrack_l3proto
*l3proto
;
1178 struct nf_conntrack_protocol
*proto
;
1179 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1181 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1182 ret
= ctnetlink_dump_tuples_ip(skb
, mask
, l3proto
);
1183 nf_ct_l3proto_put(l3proto
);
1185 if (unlikely(ret
< 0))
1186 goto nfattr_failure
;
1188 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1189 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1190 nf_ct_proto_put(proto
);
1191 if (unlikely(ret
< 0))
1192 goto nfattr_failure
;
1194 NFA_NEST_END(skb
, nest_parms
);
1203 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1204 const struct nf_conntrack_expect
*exp
)
1206 struct nf_conn
*master
= exp
->master
;
1207 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1208 u_int32_t id
= htonl(exp
->id
);
1210 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1211 goto nfattr_failure
;
1212 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1213 goto nfattr_failure
;
1214 if (ctnetlink_exp_dump_tuple(skb
,
1215 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1216 CTA_EXPECT_MASTER
) < 0)
1217 goto nfattr_failure
;
1219 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1220 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1229 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1232 const struct nf_conntrack_expect
*exp
)
1234 struct nlmsghdr
*nlh
;
1235 struct nfgenmsg
*nfmsg
;
1240 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1241 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1242 nfmsg
= NLMSG_DATA(nlh
);
1244 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1245 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1246 nfmsg
->version
= NFNETLINK_V0
;
1249 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1250 goto nfattr_failure
;
1252 nlh
->nlmsg_len
= skb
->tail
- b
;
1257 skb_trim(skb
, b
- skb
->data
);
1261 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1262 static int ctnetlink_expect_event(struct notifier_block
*this,
1263 unsigned long events
, void *ptr
)
1265 struct nlmsghdr
*nlh
;
1266 struct nfgenmsg
*nfmsg
;
1267 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1268 struct sk_buff
*skb
;
1273 if (events
& IPEXP_NEW
) {
1274 type
= IPCTNL_MSG_EXP_NEW
;
1275 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1279 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1285 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1286 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1287 nfmsg
= NLMSG_DATA(nlh
);
1289 nlh
->nlmsg_flags
= flags
;
1290 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1291 nfmsg
->version
= NFNETLINK_V0
;
1294 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1295 goto nfattr_failure
;
1297 nlh
->nlmsg_len
= skb
->tail
- b
;
1298 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1309 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1311 struct nf_conntrack_expect
*exp
= NULL
;
1312 struct list_head
*i
;
1313 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1314 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1315 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1317 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1319 read_lock_bh(&nf_conntrack_lock
);
1320 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1321 exp
= (struct nf_conntrack_expect
*) i
;
1322 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1326 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1334 read_unlock_bh(&nf_conntrack_lock
);
1336 DEBUGP("leaving, last id=%llu\n", *id
);
1341 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1342 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1343 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1347 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1348 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1350 struct nf_conntrack_tuple tuple
;
1351 struct nf_conntrack_expect
*exp
;
1352 struct sk_buff
*skb2
;
1353 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1354 u_int8_t u3
= nfmsg
->nfgen_family
;
1357 DEBUGP("entered %s\n", __FUNCTION__
);
1359 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1362 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1365 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1366 ctnetlink_exp_dump_table
,
1367 ctnetlink_done
)) != 0)
1369 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1370 if (rlen
> skb
->len
)
1372 skb_pull(skb
, rlen
);
1376 if (cda
[CTA_EXPECT_MASTER
-1])
1377 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1384 exp
= nf_conntrack_expect_find(&tuple
);
1388 if (cda
[CTA_EXPECT_ID
-1]) {
1389 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1390 if (exp
->id
!= ntohl(id
)) {
1391 nf_conntrack_expect_put(exp
);
1397 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1400 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1402 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1403 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1408 nf_conntrack_expect_put(exp
);
1410 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1415 nf_conntrack_expect_put(exp
);
1420 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1421 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1423 struct nf_conntrack_expect
*exp
, *tmp
;
1424 struct nf_conntrack_tuple tuple
;
1425 struct nf_conntrack_helper
*h
;
1426 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1427 u_int8_t u3
= nfmsg
->nfgen_family
;
1430 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1433 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1434 /* delete a single expect by tuple */
1435 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1439 /* bump usage count to 2 */
1440 exp
= nf_conntrack_expect_find(&tuple
);
1444 if (cda
[CTA_EXPECT_ID
-1]) {
1446 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1447 if (exp
->id
!= ntohl(id
)) {
1448 nf_conntrack_expect_put(exp
);
1453 /* after list removal, usage count == 1 */
1454 nf_conntrack_unexpect_related(exp
);
1455 /* have to put what we 'get' above.
1456 * after this line usage count == 0 */
1457 nf_conntrack_expect_put(exp
);
1458 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1459 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1461 /* delete all expectations for this helper */
1462 write_lock_bh(&nf_conntrack_lock
);
1463 h
= __nf_conntrack_helper_find_byname(name
);
1465 write_unlock_bh(&nf_conntrack_lock
);
1468 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1470 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1471 if (m_help
->helper
== h
1472 && del_timer(&exp
->timeout
)) {
1473 nf_ct_unlink_expect(exp
);
1474 nf_conntrack_expect_put(exp
);
1477 write_unlock_bh(&nf_conntrack_lock
);
1479 /* This basically means we have to flush everything*/
1480 write_lock_bh(&nf_conntrack_lock
);
1481 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1483 if (del_timer(&exp
->timeout
)) {
1484 nf_ct_unlink_expect(exp
);
1485 nf_conntrack_expect_put(exp
);
1488 write_unlock_bh(&nf_conntrack_lock
);
1494 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1500 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1502 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1503 struct nf_conntrack_tuple_hash
*h
= NULL
;
1504 struct nf_conntrack_expect
*exp
;
1506 struct nf_conn_help
*help
;
1509 DEBUGP("entered %s\n", __FUNCTION__
);
1511 /* caller guarantees that those three CTA_EXPECT_* exist */
1512 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1515 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1518 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1522 /* Look for master conntrack of this expectation */
1523 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1526 ct
= nf_ct_tuplehash_to_ctrack(h
);
1527 help
= nfct_help(ct
);
1529 if (!help
|| !help
->helper
) {
1530 /* such conntrack hasn't got any helper, abort */
1535 exp
= nf_conntrack_expect_alloc(ct
);
1541 exp
->expectfn
= NULL
;
1544 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1545 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1547 err
= nf_conntrack_expect_related(exp
);
1548 nf_conntrack_expect_put(exp
);
1551 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1556 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1557 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1559 struct nf_conntrack_tuple tuple
;
1560 struct nf_conntrack_expect
*exp
;
1561 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1562 u_int8_t u3
= nfmsg
->nfgen_family
;
1565 DEBUGP("entered %s\n", __FUNCTION__
);
1567 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1570 if (!cda
[CTA_EXPECT_TUPLE
-1]
1571 || !cda
[CTA_EXPECT_MASK
-1]
1572 || !cda
[CTA_EXPECT_MASTER
-1])
1575 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1579 write_lock_bh(&nf_conntrack_lock
);
1580 exp
= __nf_conntrack_expect_find(&tuple
);
1583 write_unlock_bh(&nf_conntrack_lock
);
1585 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1586 err
= ctnetlink_create_expect(cda
, u3
);
1591 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1592 err
= ctnetlink_change_expect(exp
, cda
);
1593 write_unlock_bh(&nf_conntrack_lock
);
1595 DEBUGP("leaving\n");
1600 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1601 static struct notifier_block ctnl_notifier
= {
1602 .notifier_call
= ctnetlink_conntrack_event
,
1605 static struct notifier_block ctnl_notifier_exp
= {
1606 .notifier_call
= ctnetlink_expect_event
,
1610 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1611 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1612 .attr_count
= CTA_MAX
, },
1613 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1614 .attr_count
= CTA_MAX
, },
1615 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1616 .attr_count
= CTA_MAX
, },
1617 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1618 .attr_count
= CTA_MAX
, },
1621 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1622 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1623 .attr_count
= CTA_EXPECT_MAX
, },
1624 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1625 .attr_count
= CTA_EXPECT_MAX
, },
1626 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1627 .attr_count
= CTA_EXPECT_MAX
, },
1630 static struct nfnetlink_subsystem ctnl_subsys
= {
1631 .name
= "conntrack",
1632 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1633 .cb_count
= IPCTNL_MSG_MAX
,
1637 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1638 .name
= "conntrack_expect",
1639 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1640 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1644 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1645 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1647 static int __init
ctnetlink_init(void)
1651 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1652 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1654 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1658 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1660 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1661 goto err_unreg_subsys
;
1664 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1665 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1667 printk("ctnetlink_init: cannot register notifier.\n");
1668 goto err_unreg_exp_subsys
;
1671 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1673 printk("ctnetlink_init: cannot expect register notifier.\n");
1674 goto err_unreg_notifier
;
1680 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1682 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1683 err_unreg_exp_subsys
:
1684 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1687 nfnetlink_subsys_unregister(&ctnl_subsys
);
1692 static void __exit
ctnetlink_exit(void)
1694 printk("ctnetlink: unregistering from nfnetlink.\n");
1696 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1697 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1698 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1701 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1702 nfnetlink_subsys_unregister(&ctnl_subsys
);
1706 module_init(ctnetlink_init
);
1707 module_exit(ctnetlink_exit
);