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__
);
406 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
408 struct ip_conntrack
*ct
= NULL
;
409 struct ip_conntrack_tuple_hash
*h
;
411 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
413 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
416 read_lock_bh(&ip_conntrack_lock
);
417 for (; cb
->args
[0] < ip_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
418 list_for_each_prev(i
, &ip_conntrack_hash
[cb
->args
[0]]) {
419 h
= (struct ip_conntrack_tuple_hash
*) i
;
420 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
422 ct
= tuplehash_to_ctrack(h
);
425 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
434 read_unlock_bh(&ip_conntrack_lock
);
436 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
441 #ifdef CONFIG_IP_NF_CT_ACCT
443 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
445 struct ip_conntrack
*ct
= NULL
;
446 struct ip_conntrack_tuple_hash
*h
;
448 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
450 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
453 write_lock_bh(&ip_conntrack_lock
);
454 for (; cb
->args
[0] < ip_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
455 list_for_each_prev(i
, &ip_conntrack_hash
[cb
->args
[0]]) {
456 h
= (struct ip_conntrack_tuple_hash
*) i
;
457 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
459 ct
= tuplehash_to_ctrack(h
);
462 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
469 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
473 write_unlock_bh(&ip_conntrack_lock
);
475 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
481 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
482 [CTA_IP_V4_SRC
-1] = sizeof(u_int32_t
),
483 [CTA_IP_V4_DST
-1] = sizeof(u_int32_t
),
487 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct ip_conntrack_tuple
*tuple
)
489 struct nfattr
*tb
[CTA_IP_MAX
];
491 DEBUGP("entered %s\n", __FUNCTION__
);
493 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
495 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
498 if (!tb
[CTA_IP_V4_SRC
-1])
500 tuple
->src
.ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_IP_V4_SRC
-1]);
502 if (!tb
[CTA_IP_V4_DST
-1])
504 tuple
->dst
.ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_IP_V4_DST
-1]);
511 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
512 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
513 [CTA_PROTO_SRC_PORT
-1] = sizeof(u_int16_t
),
514 [CTA_PROTO_DST_PORT
-1] = sizeof(u_int16_t
),
515 [CTA_PROTO_ICMP_TYPE
-1] = sizeof(u_int8_t
),
516 [CTA_PROTO_ICMP_CODE
-1] = sizeof(u_int8_t
),
517 [CTA_PROTO_ICMP_ID
-1] = sizeof(u_int16_t
),
521 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
522 struct ip_conntrack_tuple
*tuple
)
524 struct nfattr
*tb
[CTA_PROTO_MAX
];
525 struct ip_conntrack_protocol
*proto
;
528 DEBUGP("entered %s\n", __FUNCTION__
);
530 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
532 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
535 if (!tb
[CTA_PROTO_NUM
-1])
537 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
539 proto
= ip_conntrack_proto_find_get(tuple
->dst
.protonum
);
541 if (likely(proto
->nfattr_to_tuple
))
542 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
544 ip_conntrack_proto_put(proto
);
550 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct ip_conntrack_tuple
*tuple
,
551 enum ctattr_tuple type
)
553 struct nfattr
*tb
[CTA_TUPLE_MAX
];
556 DEBUGP("entered %s\n", __FUNCTION__
);
558 memset(tuple
, 0, sizeof(*tuple
));
560 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
562 if (!tb
[CTA_TUPLE_IP
-1])
565 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
569 if (!tb
[CTA_TUPLE_PROTO
-1])
572 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
576 /* orig and expect tuples get DIR_ORIGINAL */
577 if (type
== CTA_TUPLE_REPLY
)
578 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
580 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
589 #ifdef CONFIG_IP_NF_NAT_NEEDED
590 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
591 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
592 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
595 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
596 const struct ip_conntrack
*ct
,
597 struct ip_nat_range
*range
)
599 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
600 struct ip_nat_protocol
*npt
;
602 DEBUGP("entered %s\n", __FUNCTION__
);
604 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
606 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
609 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
611 if (!npt
->nfattr_to_range
) {
612 ip_nat_proto_put(npt
);
616 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
617 if (npt
->nfattr_to_range(tb
, range
) > 0)
618 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
620 ip_nat_proto_put(npt
);
626 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
627 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
628 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
632 ctnetlink_parse_nat(struct nfattr
*cda
[],
633 const struct ip_conntrack
*ct
, struct ip_nat_range
*range
)
635 struct nfattr
*tb
[CTA_NAT_MAX
];
638 DEBUGP("entered %s\n", __FUNCTION__
);
640 memset(range
, 0, sizeof(*range
));
642 nfattr_parse_nested(tb
, CTA_NAT_MAX
, cda
[CTA_NAT
-1]);
644 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
647 if (tb
[CTA_NAT_MINIP
-1])
648 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
650 if (!tb
[CTA_NAT_MAXIP
-1])
651 range
->max_ip
= range
->min_ip
;
653 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
656 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
658 if (!tb
[CTA_NAT_PROTO
-1])
661 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
671 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
673 struct nfattr
*tb
[CTA_HELP_MAX
];
675 DEBUGP("entered %s\n", __FUNCTION__
);
677 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
679 if (!tb
[CTA_HELP_NAME
-1])
682 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
687 static const size_t cta_min
[CTA_MAX
] = {
688 [CTA_STATUS
-1] = sizeof(u_int32_t
),
689 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
690 [CTA_MARK
-1] = sizeof(u_int32_t
),
691 [CTA_USE
-1] = sizeof(u_int32_t
),
692 [CTA_ID
-1] = sizeof(u_int32_t
)
696 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
697 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
699 struct ip_conntrack_tuple_hash
*h
;
700 struct ip_conntrack_tuple tuple
;
701 struct ip_conntrack
*ct
;
704 DEBUGP("entered %s\n", __FUNCTION__
);
706 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
709 if (cda
[CTA_TUPLE_ORIG
-1])
710 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
);
711 else if (cda
[CTA_TUPLE_REPLY
-1])
712 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
);
714 /* Flush the whole table */
715 ip_conntrack_flush();
722 h
= ip_conntrack_find_get(&tuple
, NULL
);
724 DEBUGP("tuple not found in conntrack hash\n");
728 ct
= tuplehash_to_ctrack(h
);
731 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
733 ip_conntrack_put(ct
);
737 if (del_timer(&ct
->timeout
))
738 ct
->timeout
.function((unsigned long)ct
);
740 ip_conntrack_put(ct
);
747 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
748 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
750 struct ip_conntrack_tuple_hash
*h
;
751 struct ip_conntrack_tuple tuple
;
752 struct ip_conntrack
*ct
;
753 struct sk_buff
*skb2
= NULL
;
756 DEBUGP("entered %s\n", __FUNCTION__
);
758 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
759 struct nfgenmsg
*msg
= NLMSG_DATA(nlh
);
762 if (msg
->nfgen_family
!= AF_INET
)
763 return -EAFNOSUPPORT
;
765 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
766 IPCTNL_MSG_CT_GET_CTRZERO
) {
767 #ifdef CONFIG_IP_NF_CT_ACCT
768 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
769 ctnetlink_dump_table_w
,
770 ctnetlink_done
)) != 0)
776 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
777 ctnetlink_dump_table
,
778 ctnetlink_done
)) != 0)
782 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
789 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
792 if (cda
[CTA_TUPLE_ORIG
-1])
793 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
);
794 else if (cda
[CTA_TUPLE_REPLY
-1])
795 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
);
802 h
= ip_conntrack_find_get(&tuple
, NULL
);
804 DEBUGP("tuple not found in conntrack hash");
807 DEBUGP("tuple found\n");
808 ct
= tuplehash_to_ctrack(h
);
811 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
813 ip_conntrack_put(ct
);
816 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
818 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
819 IPCTNL_MSG_CT_NEW
, 1, ct
);
820 ip_conntrack_put(ct
);
824 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
838 ctnetlink_change_status(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
841 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
842 d
= ct
->status
^ status
;
844 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
848 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
849 /* SEEN_REPLY bit can only be set */
853 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
854 /* ASSURED bit can only be set */
857 if (cda
[CTA_NAT
-1]) {
858 #ifndef CONFIG_IP_NF_NAT_NEEDED
861 unsigned int hooknum
;
862 struct ip_nat_range range
;
864 if (ctnetlink_parse_nat(cda
, ct
, &range
) < 0)
867 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
868 NIPQUAD(range
.min_ip
), NIPQUAD(range
.max_ip
),
869 htons(range
.min
.all
), htons(range
.max
.all
));
871 /* This is tricky but it works. ip_nat_setup_info needs the
872 * hook number as parameter, so let's do the correct
873 * conversion and run away */
874 if (status
& IPS_SRC_NAT_DONE
)
875 hooknum
= NF_IP_POST_ROUTING
; /* IP_NAT_MANIP_SRC */
876 else if (status
& IPS_DST_NAT_DONE
)
877 hooknum
= NF_IP_PRE_ROUTING
; /* IP_NAT_MANIP_DST */
879 return -EINVAL
; /* Missing NAT flags */
881 DEBUGP("NAT status: %lu\n",
882 status
& (IPS_NAT_MASK
| IPS_NAT_DONE_MASK
));
884 if (ip_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
886 ip_nat_setup_info(ct
, &range
, hooknum
);
888 DEBUGP("NAT status after setup_info: %lu\n",
889 ct
->status
& (IPS_NAT_MASK
| IPS_NAT_DONE_MASK
));
893 /* Be careful here, modifying NAT bits can screw up things,
894 * so don't let users modify them directly if they don't pass
896 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
902 ctnetlink_change_helper(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
904 struct ip_conntrack_helper
*helper
;
908 DEBUGP("entered %s\n", __FUNCTION__
);
910 /* don't change helper of sibling connections */
914 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
918 helper
= __ip_conntrack_helper_find_byname(helpname
);
920 if (!strcmp(helpname
, ""))
928 /* we had a helper before ... */
929 ip_ct_remove_expectations(ct
);
932 /* need to zero data of old helper */
933 memset(&ct
->help
, 0, sizeof(ct
->help
));
943 ctnetlink_change_timeout(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
945 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
947 if (!del_timer(&ct
->timeout
))
950 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
951 add_timer(&ct
->timeout
);
957 ctnetlink_change_protoinfo(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
959 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
960 struct ip_conntrack_protocol
*proto
;
961 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
964 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
966 proto
= ip_conntrack_proto_find_get(npt
);
968 if (proto
->from_nfattr
)
969 err
= proto
->from_nfattr(tb
, ct
);
970 ip_conntrack_proto_put(proto
);
976 ctnetlink_change_conntrack(struct ip_conntrack
*ct
, struct nfattr
*cda
[])
980 DEBUGP("entered %s\n", __FUNCTION__
);
982 if (cda
[CTA_HELP
-1]) {
983 err
= ctnetlink_change_helper(ct
, cda
);
988 if (cda
[CTA_TIMEOUT
-1]) {
989 err
= ctnetlink_change_timeout(ct
, cda
);
994 if (cda
[CTA_STATUS
-1]) {
995 err
= ctnetlink_change_status(ct
, cda
);
1000 if (cda
[CTA_PROTOINFO
-1]) {
1001 err
= ctnetlink_change_protoinfo(ct
, cda
);
1006 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1007 if (cda
[CTA_MARK
-1])
1008 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1011 DEBUGP("all done\n");
1016 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1017 struct ip_conntrack_tuple
*otuple
,
1018 struct ip_conntrack_tuple
*rtuple
)
1020 struct ip_conntrack
*ct
;
1023 DEBUGP("entered %s\n", __FUNCTION__
);
1025 ct
= ip_conntrack_alloc(otuple
, rtuple
);
1026 if (ct
== NULL
|| IS_ERR(ct
))
1029 if (!cda
[CTA_TIMEOUT
-1])
1031 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1033 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1034 ct
->status
|= IPS_CONFIRMED
;
1036 err
= ctnetlink_change_status(ct
, cda
);
1040 if (cda
[CTA_PROTOINFO
-1]) {
1041 err
= ctnetlink_change_protoinfo(ct
, cda
);
1046 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1047 if (cda
[CTA_MARK
-1])
1048 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1051 ct
->helper
= ip_conntrack_helper_find_get(rtuple
);
1053 add_timer(&ct
->timeout
);
1054 ip_conntrack_hash_insert(ct
);
1057 ip_conntrack_helper_put(ct
->helper
);
1059 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1063 ip_conntrack_free(ct
);
1068 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1069 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1071 struct ip_conntrack_tuple otuple
, rtuple
;
1072 struct ip_conntrack_tuple_hash
*h
= NULL
;
1075 DEBUGP("entered %s\n", __FUNCTION__
);
1077 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1080 if (cda
[CTA_TUPLE_ORIG
-1]) {
1081 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
);
1086 if (cda
[CTA_TUPLE_REPLY
-1]) {
1087 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
);
1092 write_lock_bh(&ip_conntrack_lock
);
1093 if (cda
[CTA_TUPLE_ORIG
-1])
1094 h
= __ip_conntrack_find(&otuple
, NULL
);
1095 else if (cda
[CTA_TUPLE_REPLY
-1])
1096 h
= __ip_conntrack_find(&rtuple
, NULL
);
1099 write_unlock_bh(&ip_conntrack_lock
);
1100 DEBUGP("no such conntrack, create new\n");
1102 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1103 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1106 /* implicit 'else' */
1108 /* we only allow nat config for new conntracks */
1109 if (cda
[CTA_NAT
-1]) {
1114 /* We manipulate the conntrack inside the global conntrack table lock,
1115 * so there's no need to increase the refcount */
1116 DEBUGP("conntrack found\n");
1118 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1119 err
= ctnetlink_change_conntrack(tuplehash_to_ctrack(h
), cda
);
1122 write_unlock_bh(&ip_conntrack_lock
);
1126 /***********************************************************************
1128 ***********************************************************************/
1131 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1132 const struct ip_conntrack_tuple
*tuple
,
1133 enum ctattr_expect type
)
1135 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1137 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1138 goto nfattr_failure
;
1140 NFA_NEST_END(skb
, nest_parms
);
1149 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1150 const struct ip_conntrack_tuple
*tuple
,
1151 const struct ip_conntrack_tuple
*mask
)
1154 struct ip_conntrack_protocol
*proto
;
1155 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1157 ret
= ctnetlink_dump_tuples_ip(skb
, mask
);
1158 if (unlikely(ret
< 0))
1159 goto nfattr_failure
;
1161 proto
= ip_conntrack_proto_find_get(tuple
->dst
.protonum
);
1162 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1163 ip_conntrack_proto_put(proto
);
1164 if (unlikely(ret
< 0))
1165 goto nfattr_failure
;
1167 NFA_NEST_END(skb
, nest_parms
);
1176 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1177 const struct ip_conntrack_expect
*exp
)
1179 struct ip_conntrack
*master
= exp
->master
;
1180 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1181 u_int32_t id
= htonl(exp
->id
);
1183 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1184 goto nfattr_failure
;
1185 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1186 goto nfattr_failure
;
1187 if (ctnetlink_exp_dump_tuple(skb
,
1188 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1189 CTA_EXPECT_MASTER
) < 0)
1190 goto nfattr_failure
;
1192 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1193 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1202 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1205 const struct ip_conntrack_expect
*exp
)
1207 struct nlmsghdr
*nlh
;
1208 struct nfgenmsg
*nfmsg
;
1213 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1214 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1215 nfmsg
= NLMSG_DATA(nlh
);
1217 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1218 nfmsg
->nfgen_family
= AF_INET
;
1219 nfmsg
->version
= NFNETLINK_V0
;
1222 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1223 goto nfattr_failure
;
1225 nlh
->nlmsg_len
= skb
->tail
- b
;
1230 skb_trim(skb
, b
- skb
->data
);
1234 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1235 static int ctnetlink_expect_event(struct notifier_block
*this,
1236 unsigned long events
, void *ptr
)
1238 struct nlmsghdr
*nlh
;
1239 struct nfgenmsg
*nfmsg
;
1240 struct ip_conntrack_expect
*exp
= (struct ip_conntrack_expect
*)ptr
;
1241 struct sk_buff
*skb
;
1246 if (events
& IPEXP_NEW
) {
1247 type
= IPCTNL_MSG_EXP_NEW
;
1248 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1252 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1258 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1259 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1260 nfmsg
= NLMSG_DATA(nlh
);
1262 nlh
->nlmsg_flags
= flags
;
1263 nfmsg
->nfgen_family
= AF_INET
;
1264 nfmsg
->version
= NFNETLINK_V0
;
1267 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1268 goto nfattr_failure
;
1270 nlh
->nlmsg_len
= skb
->tail
- b
;
1271 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1282 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1284 struct ip_conntrack_expect
*exp
= NULL
;
1285 struct list_head
*i
;
1286 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1288 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1290 read_lock_bh(&ip_conntrack_lock
);
1291 list_for_each_prev(i
, &ip_conntrack_expect_list
) {
1292 exp
= (struct ip_conntrack_expect
*) i
;
1295 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1303 read_unlock_bh(&ip_conntrack_lock
);
1305 DEBUGP("leaving, last id=%llu\n", *id
);
1310 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1311 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1312 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1316 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1317 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1319 struct ip_conntrack_tuple tuple
;
1320 struct ip_conntrack_expect
*exp
;
1321 struct sk_buff
*skb2
;
1324 DEBUGP("entered %s\n", __FUNCTION__
);
1326 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1329 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1330 struct nfgenmsg
*msg
= NLMSG_DATA(nlh
);
1333 if (msg
->nfgen_family
!= AF_INET
)
1334 return -EAFNOSUPPORT
;
1336 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1337 ctnetlink_exp_dump_table
,
1338 ctnetlink_done
)) != 0)
1340 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1341 if (rlen
> skb
->len
)
1343 skb_pull(skb
, rlen
);
1347 if (cda
[CTA_EXPECT_MASTER
-1])
1348 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
);
1355 exp
= ip_conntrack_expect_find(&tuple
);
1359 if (cda
[CTA_EXPECT_ID
-1]) {
1360 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1361 if (exp
->id
!= ntohl(id
)) {
1362 ip_conntrack_expect_put(exp
);
1368 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1371 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1373 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1374 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1379 ip_conntrack_expect_put(exp
);
1381 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1386 ip_conntrack_expect_put(exp
);
1391 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1392 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1394 struct ip_conntrack_expect
*exp
, *tmp
;
1395 struct ip_conntrack_tuple tuple
;
1396 struct ip_conntrack_helper
*h
;
1399 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1402 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1403 /* delete a single expect by tuple */
1404 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
);
1408 /* bump usage count to 2 */
1409 exp
= ip_conntrack_expect_find(&tuple
);
1413 if (cda
[CTA_EXPECT_ID
-1]) {
1415 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1416 if (exp
->id
!= ntohl(id
)) {
1417 ip_conntrack_expect_put(exp
);
1422 /* after list removal, usage count == 1 */
1423 ip_conntrack_unexpect_related(exp
);
1424 /* have to put what we 'get' above.
1425 * after this line usage count == 0 */
1426 ip_conntrack_expect_put(exp
);
1427 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1428 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1430 /* delete all expectations for this helper */
1431 write_lock_bh(&ip_conntrack_lock
);
1432 h
= __ip_conntrack_helper_find_byname(name
);
1434 write_unlock_bh(&ip_conntrack_lock
);
1437 list_for_each_entry_safe(exp
, tmp
, &ip_conntrack_expect_list
,
1439 if (exp
->master
->helper
== h
1440 && del_timer(&exp
->timeout
)) {
1441 ip_ct_unlink_expect(exp
);
1442 ip_conntrack_expect_put(exp
);
1445 write_unlock_bh(&ip_conntrack_lock
);
1447 /* This basically means we have to flush everything*/
1448 write_lock_bh(&ip_conntrack_lock
);
1449 list_for_each_entry_safe(exp
, tmp
, &ip_conntrack_expect_list
,
1451 if (del_timer(&exp
->timeout
)) {
1452 ip_ct_unlink_expect(exp
);
1453 ip_conntrack_expect_put(exp
);
1456 write_unlock_bh(&ip_conntrack_lock
);
1462 ctnetlink_change_expect(struct ip_conntrack_expect
*x
, struct nfattr
*cda
[])
1468 ctnetlink_create_expect(struct nfattr
*cda
[])
1470 struct ip_conntrack_tuple tuple
, mask
, master_tuple
;
1471 struct ip_conntrack_tuple_hash
*h
= NULL
;
1472 struct ip_conntrack_expect
*exp
;
1473 struct ip_conntrack
*ct
;
1476 DEBUGP("entered %s\n", __FUNCTION__
);
1478 /* caller guarantees that those three CTA_EXPECT_* exist */
1479 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
);
1482 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
);
1485 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
);
1489 /* Look for master conntrack of this expectation */
1490 h
= ip_conntrack_find_get(&master_tuple
, NULL
);
1493 ct
= tuplehash_to_ctrack(h
);
1496 /* such conntrack hasn't got any helper, abort */
1501 exp
= ip_conntrack_expect_alloc(ct
);
1507 exp
->expectfn
= NULL
;
1510 memcpy(&exp
->tuple
, &tuple
, sizeof(struct ip_conntrack_tuple
));
1511 memcpy(&exp
->mask
, &mask
, sizeof(struct ip_conntrack_tuple
));
1513 err
= ip_conntrack_expect_related(exp
);
1514 ip_conntrack_expect_put(exp
);
1517 ip_conntrack_put(tuplehash_to_ctrack(h
));
1522 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1523 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1525 struct ip_conntrack_tuple tuple
;
1526 struct ip_conntrack_expect
*exp
;
1529 DEBUGP("entered %s\n", __FUNCTION__
);
1531 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1534 if (!cda
[CTA_EXPECT_TUPLE
-1]
1535 || !cda
[CTA_EXPECT_MASK
-1]
1536 || !cda
[CTA_EXPECT_MASTER
-1])
1539 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
);
1543 write_lock_bh(&ip_conntrack_lock
);
1544 exp
= __ip_conntrack_expect_find(&tuple
);
1547 write_unlock_bh(&ip_conntrack_lock
);
1549 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1550 err
= ctnetlink_create_expect(cda
);
1555 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1556 err
= ctnetlink_change_expect(exp
, cda
);
1557 write_unlock_bh(&ip_conntrack_lock
);
1559 DEBUGP("leaving\n");
1564 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1565 static struct notifier_block ctnl_notifier
= {
1566 .notifier_call
= ctnetlink_conntrack_event
,
1569 static struct notifier_block ctnl_notifier_exp
= {
1570 .notifier_call
= ctnetlink_expect_event
,
1574 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1575 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1576 .attr_count
= CTA_MAX
, },
1577 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1578 .attr_count
= CTA_MAX
, },
1579 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1580 .attr_count
= CTA_MAX
, },
1581 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1582 .attr_count
= CTA_MAX
, },
1585 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1586 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1587 .attr_count
= CTA_EXPECT_MAX
, },
1588 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1589 .attr_count
= CTA_EXPECT_MAX
, },
1590 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1591 .attr_count
= CTA_EXPECT_MAX
, },
1594 static struct nfnetlink_subsystem ctnl_subsys
= {
1595 .name
= "conntrack",
1596 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1597 .cb_count
= IPCTNL_MSG_MAX
,
1601 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1602 .name
= "conntrack_expect",
1603 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1604 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1608 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1609 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1611 static int __init
ctnetlink_init(void)
1615 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1616 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1618 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1622 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1624 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1625 goto err_unreg_subsys
;
1628 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1629 ret
= ip_conntrack_register_notifier(&ctnl_notifier
);
1631 printk("ctnetlink_init: cannot register notifier.\n");
1632 goto err_unreg_exp_subsys
;
1635 ret
= ip_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1637 printk("ctnetlink_init: cannot expect register notifier.\n");
1638 goto err_unreg_notifier
;
1644 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1646 ip_conntrack_unregister_notifier(&ctnl_notifier
);
1647 err_unreg_exp_subsys
:
1648 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1651 nfnetlink_subsys_unregister(&ctnl_subsys
);
1656 static void __exit
ctnetlink_exit(void)
1658 printk("ctnetlink: unregistering from nfnetlink.\n");
1660 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1661 ip_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1662 ip_conntrack_unregister_notifier(&ctnl_notifier
);
1665 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1666 nfnetlink_subsys_unregister(&ctnl_subsys
);
1670 module_init(ctnetlink_init
);
1671 module_exit(ctnetlink_exit
);