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-2005 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.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/timer.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/notifier.h>
33 #include <linux/netfilter.h>
34 #include <linux/netfilter_ipv4/ip_conntrack.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
38 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
40 #include <linux/netfilter/nfnetlink.h>
41 #include <linux/netfilter/nfnetlink_conntrack.h>
43 MODULE_LICENSE("GPL");
45 static char __initdata version
[] = "0.90";
50 #define DEBUGP(format, args...)
55 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
56 const struct ip_conntrack_tuple
*tuple
,
57 struct ip_conntrack_protocol
*proto
)
60 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
62 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
64 if (likely(proto
->tuple_to_nfattr
))
65 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
67 NFA_NEST_END(skb
, nest_parms
);
76 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
77 const struct ip_conntrack_tuple
*tuple
)
79 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
81 NFA_PUT(skb
, CTA_IP_V4_SRC
, sizeof(u_int32_t
), &tuple
->src
.ip
);
82 NFA_PUT(skb
, CTA_IP_V4_DST
, sizeof(u_int32_t
), &tuple
->dst
.ip
);
84 NFA_NEST_END(skb
, nest_parms
);
93 ctnetlink_dump_tuples(struct sk_buff
*skb
,
94 const struct ip_conntrack_tuple
*tuple
)
97 struct ip_conntrack_protocol
*proto
;
99 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
);
100 if (unlikely(ret
< 0))
103 proto
= ip_conntrack_proto_find_get(tuple
->dst
.protonum
);
104 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, proto
);
105 ip_conntrack_proto_put(proto
);
111 ctnetlink_dump_status(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
113 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
114 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
122 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
124 long timeout_l
= ct
->timeout
.expires
- jiffies
;
130 timeout
= htonl(timeout_l
/ HZ
);
132 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
140 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
142 struct ip_conntrack_protocol
*proto
= ip_conntrack_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
144 struct nfattr
*nest_proto
;
147 if (!proto
->to_nfattr
) {
148 ip_conntrack_proto_put(proto
);
152 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
154 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
156 ip_conntrack_proto_put(proto
);
158 NFA_NEST_END(skb
, nest_proto
);
167 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
169 struct nfattr
*nest_helper
;
174 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
175 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(ct
->helper
->name
), ct
->helper
->name
);
177 if (ct
->helper
->to_nfattr
)
178 ct
->helper
->to_nfattr(skb
, ct
);
180 NFA_NEST_END(skb
, nest_helper
);
188 #ifdef CONFIG_IP_NF_CT_ACCT
190 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct ip_conntrack
*ct
,
191 enum ip_conntrack_dir dir
)
193 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
194 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
197 tmp
= htonl(ct
->counters
[dir
].packets
);
198 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
200 tmp
= htonl(ct
->counters
[dir
].bytes
);
201 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
203 NFA_NEST_END(skb
, nest_count
);
211 #define ctnetlink_dump_counters(a, b, c) (0)
214 #ifdef CONFIG_IP_NF_CONNTRACK_MARK
216 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
218 u_int32_t mark
= htonl(ct
->mark
);
220 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
227 #define ctnetlink_dump_mark(a, b) (0)
231 ctnetlink_dump_id(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
233 u_int32_t id
= htonl(ct
->id
);
234 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
242 ctnetlink_dump_use(struct sk_buff
*skb
, const struct ip_conntrack
*ct
)
244 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
246 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
253 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
256 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
257 int event
, int nowait
,
258 const struct ip_conntrack
*ct
)
260 struct nlmsghdr
*nlh
;
261 struct nfgenmsg
*nfmsg
;
262 struct nfattr
*nest_parms
;
267 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
268 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
269 nfmsg
= NLMSG_DATA(nlh
);
271 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
272 nfmsg
->nfgen_family
= AF_INET
;
273 nfmsg
->version
= NFNETLINK_V0
;
276 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
277 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
279 NFA_NEST_END(skb
, nest_parms
);
281 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
282 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
284 NFA_NEST_END(skb
, nest_parms
);
286 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
287 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
288 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
289 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
290 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
291 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
292 ctnetlink_dump_mark(skb
, ct
) < 0 ||
293 ctnetlink_dump_id(skb
, ct
) < 0 ||
294 ctnetlink_dump_use(skb
, ct
) < 0)
297 nlh
->nlmsg_len
= skb
->tail
- b
;
302 skb_trim(skb
, b
- skb
->data
);
306 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
307 static int ctnetlink_conntrack_event(struct notifier_block
*this,
308 unsigned long events
, void *ptr
)
310 struct nlmsghdr
*nlh
;
311 struct nfgenmsg
*nfmsg
;
312 struct nfattr
*nest_parms
;
313 struct ip_conntrack
*ct
= (struct ip_conntrack
*)ptr
;
317 unsigned int flags
= 0, group
;
319 /* ignore our fake conntrack entry */
320 if (ct
== &ip_conntrack_untracked
)
323 if (events
& IPCT_DESTROY
) {
324 type
= IPCTNL_MSG_CT_DELETE
;
325 group
= NFNLGRP_CONNTRACK_DESTROY
;
326 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
327 type
= IPCTNL_MSG_CT_NEW
;
328 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
329 /* dump everything */
331 group
= NFNLGRP_CONNTRACK_NEW
;
332 } else if (events
& (IPCT_STATUS
|
337 type
= IPCTNL_MSG_CT_NEW
;
338 group
= NFNLGRP_CONNTRACK_UPDATE
;
342 if (!nfnetlink_has_listeners(group
))
345 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
351 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
352 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
353 nfmsg
= NLMSG_DATA(nlh
);
355 nlh
->nlmsg_flags
= flags
;
356 nfmsg
->nfgen_family
= AF_INET
;
357 nfmsg
->version
= NFNETLINK_V0
;
360 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
361 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
363 NFA_NEST_END(skb
, nest_parms
);
365 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
366 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
368 NFA_NEST_END(skb
, nest_parms
);
370 /* NAT stuff is now a status flag */
371 if ((events
& IPCT_STATUS
|| events
& IPCT_NATINFO
)
372 && ctnetlink_dump_status(skb
, ct
) < 0)
374 if (events
& IPCT_REFRESH
375 && ctnetlink_dump_timeout(skb
, ct
) < 0)
377 if (events
& IPCT_PROTOINFO
378 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
380 if (events
& IPCT_HELPINFO
381 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
384 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
385 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
388 nlh
->nlmsg_len
= skb
->tail
- b
;
389 nfnetlink_send(skb
, 0, group
, 0);
397 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
399 static int ctnetlink_done(struct netlink_callback
*cb
)
401 DEBUGP("entered %s\n", __FUNCTION__
);
403 ip_conntrack_put((struct ip_conntrack
*)cb
->args
[1]);
408 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
410 struct ip_conntrack
*ct
, *last
;
411 struct ip_conntrack_tuple_hash
*h
;
414 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
417 read_lock_bh(&ip_conntrack_lock
);
418 last
= (struct ip_conntrack
*)cb
->args
[1];
419 for (; cb
->args
[0] < ip_conntrack_htable_size
; cb
->args
[0]++) {
421 list_for_each_prev(i
, &ip_conntrack_hash
[cb
->args
[0]]) {
422 h
= (struct ip_conntrack_tuple_hash
*) i
;
423 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
425 ct
= tuplehash_to_ctrack(h
);
431 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
435 nf_conntrack_get(&ct
->ct_general
);
436 cb
->args
[1] = (unsigned long)ct
;
446 read_unlock_bh(&ip_conntrack_lock
);
448 ip_conntrack_put(last
);
450 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
454 #ifdef CONFIG_IP_NF_CT_ACCT
456 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
458 struct ip_conntrack
*ct
= NULL
;
459 struct ip_conntrack_tuple_hash
*h
;
461 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
463 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
466 write_lock_bh(&ip_conntrack_lock
);
467 for (; cb
->args
[0] < ip_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
468 list_for_each_prev(i
, &ip_conntrack_hash
[cb
->args
[0]]) {
469 h
= (struct ip_conntrack_tuple_hash
*) i
;
470 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
472 ct
= tuplehash_to_ctrack(h
);
475 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
482 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
486 write_unlock_bh(&ip_conntrack_lock
);
488 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
494 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
495 [CTA_IP_V4_SRC
-1] = sizeof(u_int32_t
),
496 [CTA_IP_V4_DST
-1] = sizeof(u_int32_t
),
500 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct ip_conntrack_tuple
*tuple
)
502 struct nfattr
*tb
[CTA_IP_MAX
];
504 DEBUGP("entered %s\n", __FUNCTION__
);
506 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
508 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
511 if (!tb
[CTA_IP_V4_SRC
-1])
513 tuple
->src
.ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_IP_V4_SRC
-1]);
515 if (!tb
[CTA_IP_V4_DST
-1])
517 tuple
->dst
.ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_IP_V4_DST
-1]);
524 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
525 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
526 [CTA_PROTO_SRC_PORT
-1] = sizeof(u_int16_t
),
527 [CTA_PROTO_DST_PORT
-1] = sizeof(u_int16_t
),
528 [CTA_PROTO_ICMP_TYPE
-1] = sizeof(u_int8_t
),
529 [CTA_PROTO_ICMP_CODE
-1] = sizeof(u_int8_t
),
530 [CTA_PROTO_ICMP_ID
-1] = sizeof(u_int16_t
),
534 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
535 struct ip_conntrack_tuple
*tuple
)
537 struct nfattr
*tb
[CTA_PROTO_MAX
];
538 struct ip_conntrack_protocol
*proto
;
541 DEBUGP("entered %s\n", __FUNCTION__
);
543 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
545 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
548 if (!tb
[CTA_PROTO_NUM
-1])
550 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
552 proto
= ip_conntrack_proto_find_get(tuple
->dst
.protonum
);
554 if (likely(proto
->nfattr_to_tuple
))
555 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
557 ip_conntrack_proto_put(proto
);
563 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct ip_conntrack_tuple
*tuple
,
564 enum ctattr_tuple type
)
566 struct nfattr
*tb
[CTA_TUPLE_MAX
];
569 DEBUGP("entered %s\n", __FUNCTION__
);
571 memset(tuple
, 0, sizeof(*tuple
));
573 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
575 if (!tb
[CTA_TUPLE_IP
-1])
578 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
582 if (!tb
[CTA_TUPLE_PROTO
-1])
585 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
589 /* orig and expect tuples get DIR_ORIGINAL */
590 if (type
== CTA_TUPLE_REPLY
)
591 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
593 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
602 #ifdef CONFIG_IP_NF_NAT_NEEDED
603 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
604 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
605 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
608 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
609 const struct ip_conntrack
*ct
,
610 struct ip_nat_range
*range
)
612 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
613 struct ip_nat_protocol
*npt
;
615 DEBUGP("entered %s\n", __FUNCTION__
);
617 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
619 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
622 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
624 if (!npt
->nfattr_to_range
) {
625 ip_nat_proto_put(npt
);
629 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
630 if (npt
->nfattr_to_range(tb
, range
) > 0)
631 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
633 ip_nat_proto_put(npt
);
639 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
640 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
641 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
645 ctnetlink_parse_nat(struct nfattr
*nat
,
646 const struct ip_conntrack
*ct
, struct ip_nat_range
*range
)
648 struct nfattr
*tb
[CTA_NAT_MAX
];
651 DEBUGP("entered %s\n", __FUNCTION__
);
653 memset(range
, 0, sizeof(*range
));
655 nfattr_parse_nested(tb
, CTA_NAT_MAX
, nat
);
657 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
660 if (tb
[CTA_NAT_MINIP
-1])
661 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
663 if (!tb
[CTA_NAT_MAXIP
-1])
664 range
->max_ip
= range
->min_ip
;
666 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
669 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
671 if (!tb
[CTA_NAT_PROTO
-1])
674 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
684 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
686 struct nfattr
*tb
[CTA_HELP_MAX
];
688 DEBUGP("entered %s\n", __FUNCTION__
);
690 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
692 if (!tb
[CTA_HELP_NAME
-1])
695 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
700 static const size_t cta_min
[CTA_MAX
] = {
701 [CTA_STATUS
-1] = sizeof(u_int32_t
),
702 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
703 [CTA_MARK
-1] = sizeof(u_int32_t
),
704 [CTA_USE
-1] = sizeof(u_int32_t
),
705 [CTA_ID
-1] = sizeof(u_int32_t
)
709 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
710 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
712 struct ip_conntrack_tuple_hash
*h
;
713 struct ip_conntrack_tuple tuple
;
714 struct ip_conntrack
*ct
;
717 DEBUGP("entered %s\n", __FUNCTION__
);
719 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
722 if (cda
[CTA_TUPLE_ORIG
-1])
723 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
);
724 else if (cda
[CTA_TUPLE_REPLY
-1])
725 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
);
727 /* Flush the whole table */
728 ip_conntrack_flush();
735 h
= ip_conntrack_find_get(&tuple
, NULL
);
737 DEBUGP("tuple not found in conntrack hash\n");
741 ct
= tuplehash_to_ctrack(h
);
744 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
746 ip_conntrack_put(ct
);
750 if (del_timer(&ct
->timeout
))
751 ct
->timeout
.function((unsigned long)ct
);
753 ip_conntrack_put(ct
);
760 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
761 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
763 struct ip_conntrack_tuple_hash
*h
;
764 struct ip_conntrack_tuple tuple
;
765 struct ip_conntrack
*ct
;
766 struct sk_buff
*skb2
= NULL
;
769 DEBUGP("entered %s\n", __FUNCTION__
);
771 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
772 struct nfgenmsg
*msg
= NLMSG_DATA(nlh
);
775 if (msg
->nfgen_family
!= AF_INET
)
776 return -EAFNOSUPPORT
;
778 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
779 IPCTNL_MSG_CT_GET_CTRZERO
) {
780 #ifdef CONFIG_IP_NF_CT_ACCT
781 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
782 ctnetlink_dump_table_w
,
783 ctnetlink_done
)) != 0)
789 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
790 ctnetlink_dump_table
,
791 ctnetlink_done
)) != 0)
795 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
802 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
805 if (cda
[CTA_TUPLE_ORIG
-1])
806 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
);
807 else if (cda
[CTA_TUPLE_REPLY
-1])
808 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
);
815 h
= ip_conntrack_find_get(&tuple
, NULL
);
817 DEBUGP("tuple not found in conntrack hash");
820 DEBUGP("tuple found\n");
821 ct
= tuplehash_to_ctrack(h
);
824 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
826 ip_conntrack_put(ct
);
829 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
831 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
832 IPCTNL_MSG_CT_NEW
, 1, ct
);
833 ip_conntrack_put(ct
);
837 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
851 ctnetlink_change_status(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
854 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
855 d
= ct
->status
^ status
;
857 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
861 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
862 /* SEEN_REPLY bit can only be set */
866 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
867 /* ASSURED bit can only be set */
870 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
871 #ifndef CONFIG_IP_NF_NAT_NEEDED
874 struct ip_nat_range range
;
876 if (cda
[CTA_NAT_DST
-1]) {
877 if (ctnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
880 if (ip_nat_initialized(ct
,
881 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
883 ip_nat_setup_info(ct
, &range
, NF_IP_PRE_ROUTING
);
885 if (cda
[CTA_NAT_SRC
-1]) {
886 if (ctnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
889 if (ip_nat_initialized(ct
,
890 HOOK2MANIP(NF_IP_POST_ROUTING
)))
892 ip_nat_setup_info(ct
, &range
, NF_IP_POST_ROUTING
);
897 /* Be careful here, modifying NAT bits can screw up things,
898 * so don't let users modify them directly if they don't pass
900 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
906 ctnetlink_change_helper(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
908 struct ip_conntrack_helper
*helper
;
912 DEBUGP("entered %s\n", __FUNCTION__
);
914 /* don't change helper of sibling connections */
918 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
922 helper
= __ip_conntrack_helper_find_byname(helpname
);
924 if (!strcmp(helpname
, ""))
932 /* we had a helper before ... */
933 ip_ct_remove_expectations(ct
);
936 /* need to zero data of old helper */
937 memset(&ct
->help
, 0, sizeof(ct
->help
));
947 ctnetlink_change_timeout(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
949 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
951 if (!del_timer(&ct
->timeout
))
954 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
955 add_timer(&ct
->timeout
);
961 ctnetlink_change_protoinfo(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
963 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
964 struct ip_conntrack_protocol
*proto
;
965 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
968 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
970 proto
= ip_conntrack_proto_find_get(npt
);
972 if (proto
->from_nfattr
)
973 err
= proto
->from_nfattr(tb
, ct
);
974 ip_conntrack_proto_put(proto
);
980 ctnetlink_change_conntrack(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
984 DEBUGP("entered %s\n", __FUNCTION__
);
986 if (cda
[CTA_HELP
-1]) {
987 err
= ctnetlink_change_helper(ct
, cda
);
992 if (cda
[CTA_TIMEOUT
-1]) {
993 err
= ctnetlink_change_timeout(ct
, cda
);
998 if (cda
[CTA_STATUS
-1]) {
999 err
= ctnetlink_change_status(ct
, cda
);
1004 if (cda
[CTA_PROTOINFO
-1]) {
1005 err
= ctnetlink_change_protoinfo(ct
, cda
);
1010 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1011 if (cda
[CTA_MARK
-1])
1012 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1015 DEBUGP("all done\n");
1020 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1021 struct ip_conntrack_tuple
*otuple
,
1022 struct ip_conntrack_tuple
*rtuple
)
1024 struct ip_conntrack
*ct
;
1027 DEBUGP("entered %s\n", __FUNCTION__
);
1029 ct
= ip_conntrack_alloc(otuple
, rtuple
);
1030 if (ct
== NULL
|| IS_ERR(ct
))
1033 if (!cda
[CTA_TIMEOUT
-1])
1035 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1037 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1038 ct
->status
|= IPS_CONFIRMED
;
1040 err
= ctnetlink_change_status(ct
, cda
);
1044 if (cda
[CTA_PROTOINFO
-1]) {
1045 err
= ctnetlink_change_protoinfo(ct
, cda
);
1050 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1051 if (cda
[CTA_MARK
-1])
1052 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1055 ct
->helper
= ip_conntrack_helper_find_get(rtuple
);
1057 add_timer(&ct
->timeout
);
1058 ip_conntrack_hash_insert(ct
);
1061 ip_conntrack_helper_put(ct
->helper
);
1063 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1067 ip_conntrack_free(ct
);
1072 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1073 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1075 struct ip_conntrack_tuple otuple
, rtuple
;
1076 struct ip_conntrack_tuple_hash
*h
= NULL
;
1079 DEBUGP("entered %s\n", __FUNCTION__
);
1081 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1084 if (cda
[CTA_TUPLE_ORIG
-1]) {
1085 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
);
1090 if (cda
[CTA_TUPLE_REPLY
-1]) {
1091 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
);
1096 write_lock_bh(&ip_conntrack_lock
);
1097 if (cda
[CTA_TUPLE_ORIG
-1])
1098 h
= __ip_conntrack_find(&otuple
, NULL
);
1099 else if (cda
[CTA_TUPLE_REPLY
-1])
1100 h
= __ip_conntrack_find(&rtuple
, NULL
);
1103 write_unlock_bh(&ip_conntrack_lock
);
1104 DEBUGP("no such conntrack, create new\n");
1106 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1107 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1110 /* implicit 'else' */
1112 /* we only allow nat config for new conntracks */
1113 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1118 /* We manipulate the conntrack inside the global conntrack table lock,
1119 * so there's no need to increase the refcount */
1120 DEBUGP("conntrack found\n");
1122 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1123 err
= ctnetlink_change_conntrack(tuplehash_to_ctrack(h
), cda
);
1126 write_unlock_bh(&ip_conntrack_lock
);
1130 /***********************************************************************
1132 ***********************************************************************/
1135 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1136 const struct ip_conntrack_tuple
*tuple
,
1137 enum ctattr_expect type
)
1139 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1141 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1142 goto nfattr_failure
;
1144 NFA_NEST_END(skb
, nest_parms
);
1153 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1154 const struct ip_conntrack_tuple
*tuple
,
1155 const struct ip_conntrack_tuple
*mask
)
1158 struct ip_conntrack_protocol
*proto
;
1159 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1161 ret
= ctnetlink_dump_tuples_ip(skb
, mask
);
1162 if (unlikely(ret
< 0))
1163 goto nfattr_failure
;
1165 proto
= ip_conntrack_proto_find_get(tuple
->dst
.protonum
);
1166 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1167 ip_conntrack_proto_put(proto
);
1168 if (unlikely(ret
< 0))
1169 goto nfattr_failure
;
1171 NFA_NEST_END(skb
, nest_parms
);
1180 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1181 const struct ip_conntrack_expect
*exp
)
1183 struct ip_conntrack
*master
= exp
->master
;
1184 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1185 u_int32_t id
= htonl(exp
->id
);
1187 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1188 goto nfattr_failure
;
1189 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1190 goto nfattr_failure
;
1191 if (ctnetlink_exp_dump_tuple(skb
,
1192 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1193 CTA_EXPECT_MASTER
) < 0)
1194 goto nfattr_failure
;
1196 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1197 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1206 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1209 const struct ip_conntrack_expect
*exp
)
1211 struct nlmsghdr
*nlh
;
1212 struct nfgenmsg
*nfmsg
;
1217 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1218 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1219 nfmsg
= NLMSG_DATA(nlh
);
1221 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1222 nfmsg
->nfgen_family
= AF_INET
;
1223 nfmsg
->version
= NFNETLINK_V0
;
1226 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1227 goto nfattr_failure
;
1229 nlh
->nlmsg_len
= skb
->tail
- b
;
1234 skb_trim(skb
, b
- skb
->data
);
1238 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1239 static int ctnetlink_expect_event(struct notifier_block
*this,
1240 unsigned long events
, void *ptr
)
1242 struct nlmsghdr
*nlh
;
1243 struct nfgenmsg
*nfmsg
;
1244 struct ip_conntrack_expect
*exp
= (struct ip_conntrack_expect
*)ptr
;
1245 struct sk_buff
*skb
;
1250 if (events
& IPEXP_NEW
) {
1251 type
= IPCTNL_MSG_EXP_NEW
;
1252 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1256 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1262 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1263 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1264 nfmsg
= NLMSG_DATA(nlh
);
1266 nlh
->nlmsg_flags
= flags
;
1267 nfmsg
->nfgen_family
= AF_INET
;
1268 nfmsg
->version
= NFNETLINK_V0
;
1271 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1272 goto nfattr_failure
;
1274 nlh
->nlmsg_len
= skb
->tail
- b
;
1275 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1286 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1288 struct ip_conntrack_expect
*exp
= NULL
;
1289 struct list_head
*i
;
1290 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1292 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1294 read_lock_bh(&ip_conntrack_lock
);
1295 list_for_each_prev(i
, &ip_conntrack_expect_list
) {
1296 exp
= (struct ip_conntrack_expect
*) i
;
1299 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1307 read_unlock_bh(&ip_conntrack_lock
);
1309 DEBUGP("leaving, last id=%llu\n", *id
);
1314 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1315 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1316 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1320 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1321 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1323 struct ip_conntrack_tuple tuple
;
1324 struct ip_conntrack_expect
*exp
;
1325 struct sk_buff
*skb2
;
1328 DEBUGP("entered %s\n", __FUNCTION__
);
1330 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1333 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1334 struct nfgenmsg
*msg
= NLMSG_DATA(nlh
);
1337 if (msg
->nfgen_family
!= AF_INET
)
1338 return -EAFNOSUPPORT
;
1340 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1341 ctnetlink_exp_dump_table
,
1342 ctnetlink_done
)) != 0)
1344 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1345 if (rlen
> skb
->len
)
1347 skb_pull(skb
, rlen
);
1351 if (cda
[CTA_EXPECT_MASTER
-1])
1352 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
);
1359 exp
= ip_conntrack_expect_find(&tuple
);
1363 if (cda
[CTA_EXPECT_ID
-1]) {
1364 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1365 if (exp
->id
!= ntohl(id
)) {
1366 ip_conntrack_expect_put(exp
);
1372 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1375 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1377 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1378 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1383 ip_conntrack_expect_put(exp
);
1385 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1390 ip_conntrack_expect_put(exp
);
1395 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1396 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1398 struct ip_conntrack_expect
*exp
, *tmp
;
1399 struct ip_conntrack_tuple tuple
;
1400 struct ip_conntrack_helper
*h
;
1403 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1406 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1407 /* delete a single expect by tuple */
1408 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
);
1412 /* bump usage count to 2 */
1413 exp
= ip_conntrack_expect_find(&tuple
);
1417 if (cda
[CTA_EXPECT_ID
-1]) {
1419 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1420 if (exp
->id
!= ntohl(id
)) {
1421 ip_conntrack_expect_put(exp
);
1426 /* after list removal, usage count == 1 */
1427 ip_conntrack_unexpect_related(exp
);
1428 /* have to put what we 'get' above.
1429 * after this line usage count == 0 */
1430 ip_conntrack_expect_put(exp
);
1431 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1432 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1434 /* delete all expectations for this helper */
1435 write_lock_bh(&ip_conntrack_lock
);
1436 h
= __ip_conntrack_helper_find_byname(name
);
1438 write_unlock_bh(&ip_conntrack_lock
);
1441 list_for_each_entry_safe(exp
, tmp
, &ip_conntrack_expect_list
,
1443 if (exp
->master
->helper
== h
1444 && del_timer(&exp
->timeout
)) {
1445 ip_ct_unlink_expect(exp
);
1446 ip_conntrack_expect_put(exp
);
1449 write_unlock_bh(&ip_conntrack_lock
);
1451 /* This basically means we have to flush everything*/
1452 write_lock_bh(&ip_conntrack_lock
);
1453 list_for_each_entry_safe(exp
, tmp
, &ip_conntrack_expect_list
,
1455 if (del_timer(&exp
->timeout
)) {
1456 ip_ct_unlink_expect(exp
);
1457 ip_conntrack_expect_put(exp
);
1460 write_unlock_bh(&ip_conntrack_lock
);
1466 ctnetlink_change_expect(struct ip_conntrack_expect
*x
, struct nfattr
*cda
[])
1472 ctnetlink_create_expect(struct nfattr
*cda
[])
1474 struct ip_conntrack_tuple tuple
, mask
, master_tuple
;
1475 struct ip_conntrack_tuple_hash
*h
= NULL
;
1476 struct ip_conntrack_expect
*exp
;
1477 struct ip_conntrack
*ct
;
1480 DEBUGP("entered %s\n", __FUNCTION__
);
1482 /* caller guarantees that those three CTA_EXPECT_* exist */
1483 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
);
1486 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
);
1489 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
);
1493 /* Look for master conntrack of this expectation */
1494 h
= ip_conntrack_find_get(&master_tuple
, NULL
);
1497 ct
= tuplehash_to_ctrack(h
);
1500 /* such conntrack hasn't got any helper, abort */
1505 exp
= ip_conntrack_expect_alloc(ct
);
1511 exp
->expectfn
= NULL
;
1514 memcpy(&exp
->tuple
, &tuple
, sizeof(struct ip_conntrack_tuple
));
1515 memcpy(&exp
->mask
, &mask
, sizeof(struct ip_conntrack_tuple
));
1517 err
= ip_conntrack_expect_related(exp
);
1518 ip_conntrack_expect_put(exp
);
1521 ip_conntrack_put(tuplehash_to_ctrack(h
));
1526 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1527 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1529 struct ip_conntrack_tuple tuple
;
1530 struct ip_conntrack_expect
*exp
;
1533 DEBUGP("entered %s\n", __FUNCTION__
);
1535 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1538 if (!cda
[CTA_EXPECT_TUPLE
-1]
1539 || !cda
[CTA_EXPECT_MASK
-1]
1540 || !cda
[CTA_EXPECT_MASTER
-1])
1543 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
);
1547 write_lock_bh(&ip_conntrack_lock
);
1548 exp
= __ip_conntrack_expect_find(&tuple
);
1551 write_unlock_bh(&ip_conntrack_lock
);
1553 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1554 err
= ctnetlink_create_expect(cda
);
1559 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1560 err
= ctnetlink_change_expect(exp
, cda
);
1561 write_unlock_bh(&ip_conntrack_lock
);
1563 DEBUGP("leaving\n");
1568 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1569 static struct notifier_block ctnl_notifier
= {
1570 .notifier_call
= ctnetlink_conntrack_event
,
1573 static struct notifier_block ctnl_notifier_exp
= {
1574 .notifier_call
= ctnetlink_expect_event
,
1578 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1579 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1580 .attr_count
= CTA_MAX
, },
1581 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1582 .attr_count
= CTA_MAX
, },
1583 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1584 .attr_count
= CTA_MAX
, },
1585 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1586 .attr_count
= CTA_MAX
, },
1589 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1590 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1591 .attr_count
= CTA_EXPECT_MAX
, },
1592 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1593 .attr_count
= CTA_EXPECT_MAX
, },
1594 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1595 .attr_count
= CTA_EXPECT_MAX
, },
1598 static struct nfnetlink_subsystem ctnl_subsys
= {
1599 .name
= "conntrack",
1600 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1601 .cb_count
= IPCTNL_MSG_MAX
,
1605 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1606 .name
= "conntrack_expect",
1607 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1608 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1612 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1613 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1615 static int __init
ctnetlink_init(void)
1619 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1620 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1622 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1626 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1628 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1629 goto err_unreg_subsys
;
1632 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1633 ret
= ip_conntrack_register_notifier(&ctnl_notifier
);
1635 printk("ctnetlink_init: cannot register notifier.\n");
1636 goto err_unreg_exp_subsys
;
1639 ret
= ip_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1641 printk("ctnetlink_init: cannot expect register notifier.\n");
1642 goto err_unreg_notifier
;
1648 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1650 ip_conntrack_unregister_notifier(&ctnl_notifier
);
1651 err_unreg_exp_subsys
:
1652 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1655 nfnetlink_subsys_unregister(&ctnl_subsys
);
1660 static void __exit
ctnetlink_exit(void)
1662 printk("ctnetlink: unregistering from nfnetlink.\n");
1664 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1665 ip_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1666 ip_conntrack_unregister_notifier(&ctnl_notifier
);
1669 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1670 nfnetlink_subsys_unregister(&ctnl_subsys
);
1674 module_init(ctnetlink_init
);
1675 module_exit(ctnetlink_exit
);