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 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
)
60 struct nf_conntrack_protocol
*proto
;
63 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
65 /* If no protocol helper is found, this function will return the
66 * generic protocol helper, so proto won't *ever* be NULL */
67 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
68 if (likely(proto
->tuple_to_nfattr
))
69 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
71 nf_ct_proto_put(proto
);
80 ctnetlink_dump_tuples(struct sk_buff
*skb
,
81 const struct nf_conntrack_tuple
*tuple
)
83 struct nfattr
*nest_parms
;
84 struct nf_conntrack_l3proto
*l3proto
;
87 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
89 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
90 if (likely(l3proto
->tuple_to_nfattr
))
91 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
92 NFA_NEST_END(skb
, nest_parms
);
94 nf_ct_l3proto_put(l3proto
);
96 if (unlikely(ret
< 0))
99 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
100 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
);
101 NFA_NEST_END(skb
, nest_parms
);
110 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
112 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
113 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
121 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
123 long timeout_l
= ct
->timeout
.expires
- jiffies
;
129 timeout
= htonl(timeout_l
/ HZ
);
131 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
139 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
141 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
);
142 struct nfattr
*nest_proto
;
145 if (!proto
->to_nfattr
) {
146 nf_ct_proto_put(proto
);
150 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
152 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
154 nf_ct_proto_put(proto
);
156 NFA_NEST_END(skb
, nest_proto
);
165 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
167 struct nfattr
*nest_helper
;
168 const struct nf_conn_help
*help
= nfct_help(ct
);
170 if (!help
|| !help
->helper
)
173 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
174 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(help
->helper
->name
), help
->helper
->name
);
176 if (help
->helper
->to_nfattr
)
177 help
->helper
->to_nfattr(skb
, ct
);
179 NFA_NEST_END(skb
, nest_helper
);
187 #ifdef CONFIG_NF_CT_ACCT
189 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
190 enum ip_conntrack_dir dir
)
192 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
193 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
196 tmp
= htonl(ct
->counters
[dir
].packets
);
197 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
199 tmp
= htonl(ct
->counters
[dir
].bytes
);
200 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
202 NFA_NEST_END(skb
, nest_count
);
210 #define ctnetlink_dump_counters(a, b, c) (0)
213 #ifdef CONFIG_NF_CONNTRACK_MARK
215 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
217 u_int32_t mark
= htonl(ct
->mark
);
219 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
226 #define ctnetlink_dump_mark(a, b) (0)
230 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
232 u_int32_t id
= htonl(ct
->id
);
233 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
241 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
243 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
245 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
252 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
255 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
256 int event
, int nowait
,
257 const struct nf_conn
*ct
)
259 struct nlmsghdr
*nlh
;
260 struct nfgenmsg
*nfmsg
;
261 struct nfattr
*nest_parms
;
266 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
267 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
268 nfmsg
= NLMSG_DATA(nlh
);
270 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
271 nfmsg
->nfgen_family
=
272 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
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_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 nf_conn
*ct
= (struct nf_conn
*)ptr
;
317 unsigned int flags
= 0, group
;
319 /* ignore our fake conntrack entry */
320 if (ct
== &nf_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
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
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_NF_CONNTRACK_EVENTS */
399 static int ctnetlink_done(struct netlink_callback
*cb
)
401 DEBUGP("entered %s\n", __FUNCTION__
);
405 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
408 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
410 struct nf_conn
*ct
= NULL
;
411 struct nf_conntrack_tuple_hash
*h
;
413 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
414 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
415 u_int8_t l3proto
= nfmsg
->nfgen_family
;
417 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
420 read_lock_bh(&nf_conntrack_lock
);
421 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
422 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
423 h
= (struct nf_conntrack_tuple_hash
*) i
;
424 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
426 ct
= nf_ct_tuplehash_to_ctrack(h
);
427 /* Dump entries of a given L3 protocol number.
428 * If it is not specified, ie. l3proto == 0,
429 * then dump everything. */
430 if (l3proto
&& L3PROTO(ct
) != l3proto
)
434 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
443 read_unlock_bh(&nf_conntrack_lock
);
445 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
450 #ifdef CONFIG_NF_CT_ACCT
452 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
454 struct nf_conn
*ct
= NULL
;
455 struct nf_conntrack_tuple_hash
*h
;
457 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
458 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
459 u_int8_t l3proto
= nfmsg
->nfgen_family
;
461 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
464 write_lock_bh(&nf_conntrack_lock
);
465 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
466 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
467 h
= (struct nf_conntrack_tuple_hash
*) i
;
468 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
470 ct
= nf_ct_tuplehash_to_ctrack(h
);
471 if (l3proto
&& L3PROTO(ct
) != l3proto
)
475 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
482 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
486 write_unlock_bh(&nf_conntrack_lock
);
488 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
495 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
497 struct nfattr
*tb
[CTA_IP_MAX
];
498 struct nf_conntrack_l3proto
*l3proto
;
501 DEBUGP("entered %s\n", __FUNCTION__
);
503 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
505 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
507 if (likely(l3proto
->nfattr_to_tuple
))
508 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
510 nf_ct_l3proto_put(l3proto
);
517 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
518 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
522 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
523 struct nf_conntrack_tuple
*tuple
)
525 struct nfattr
*tb
[CTA_PROTO_MAX
];
526 struct nf_conntrack_protocol
*proto
;
529 DEBUGP("entered %s\n", __FUNCTION__
);
531 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
533 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
536 if (!tb
[CTA_PROTO_NUM
-1])
538 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
540 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
542 if (likely(proto
->nfattr_to_tuple
))
543 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
545 nf_ct_proto_put(proto
);
551 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
552 enum ctattr_tuple type
, u_int8_t l3num
)
554 struct nfattr
*tb
[CTA_TUPLE_MAX
];
557 DEBUGP("entered %s\n", __FUNCTION__
);
559 memset(tuple
, 0, sizeof(*tuple
));
561 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
563 if (!tb
[CTA_TUPLE_IP
-1])
566 tuple
->src
.l3num
= l3num
;
568 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
572 if (!tb
[CTA_TUPLE_PROTO
-1])
575 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
579 /* orig and expect tuples get DIR_ORIGINAL */
580 if (type
== CTA_TUPLE_REPLY
)
581 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
583 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
585 NF_CT_DUMP_TUPLE(tuple
);
592 #ifdef CONFIG_IP_NF_NAT_NEEDED
593 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
594 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
595 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
598 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
599 const struct nf_conn
*ct
,
600 struct ip_nat_range
*range
)
602 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
603 struct ip_nat_protocol
*npt
;
605 DEBUGP("entered %s\n", __FUNCTION__
);
607 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
609 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
612 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
614 if (!npt
->nfattr_to_range
) {
615 ip_nat_proto_put(npt
);
619 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
620 if (npt
->nfattr_to_range(tb
, range
) > 0)
621 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
623 ip_nat_proto_put(npt
);
629 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
630 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
631 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
635 ctnetlink_parse_nat(struct nfattr
*cda
[],
636 const struct nf_conn
*ct
, struct ip_nat_range
*range
)
638 struct nfattr
*tb
[CTA_NAT_MAX
];
641 DEBUGP("entered %s\n", __FUNCTION__
);
643 memset(range
, 0, sizeof(*range
));
645 nfattr_parse_nested(tb
, CTA_NAT_MAX
, cda
[CTA_NAT
-1]);
647 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
650 if (tb
[CTA_NAT_MINIP
-1])
651 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
653 if (!tb
[CTA_NAT_MAXIP
-1])
654 range
->max_ip
= range
->min_ip
;
656 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
659 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
661 if (!tb
[CTA_NAT_PROTO
-1])
664 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
674 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
676 struct nfattr
*tb
[CTA_HELP_MAX
];
678 DEBUGP("entered %s\n", __FUNCTION__
);
680 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
682 if (!tb
[CTA_HELP_NAME
-1])
685 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
690 static const size_t cta_min
[CTA_MAX
] = {
691 [CTA_STATUS
-1] = sizeof(u_int32_t
),
692 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
693 [CTA_MARK
-1] = sizeof(u_int32_t
),
694 [CTA_USE
-1] = sizeof(u_int32_t
),
695 [CTA_ID
-1] = sizeof(u_int32_t
)
699 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
700 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
702 struct nf_conntrack_tuple_hash
*h
;
703 struct nf_conntrack_tuple tuple
;
705 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
706 u_int8_t u3
= nfmsg
->nfgen_family
;
709 DEBUGP("entered %s\n", __FUNCTION__
);
711 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
714 if (cda
[CTA_TUPLE_ORIG
-1])
715 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
716 else if (cda
[CTA_TUPLE_REPLY
-1])
717 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
719 /* Flush the whole table */
720 nf_conntrack_flush();
727 h
= nf_conntrack_find_get(&tuple
, NULL
);
729 DEBUGP("tuple not found in conntrack hash\n");
733 ct
= nf_ct_tuplehash_to_ctrack(h
);
736 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
742 if (del_timer(&ct
->timeout
))
743 ct
->timeout
.function((unsigned long)ct
);
752 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
753 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
755 struct nf_conntrack_tuple_hash
*h
;
756 struct nf_conntrack_tuple tuple
;
758 struct sk_buff
*skb2
= NULL
;
759 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
760 u_int8_t u3
= nfmsg
->nfgen_family
;
763 DEBUGP("entered %s\n", __FUNCTION__
);
765 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
768 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
769 IPCTNL_MSG_CT_GET_CTRZERO
) {
770 #ifdef CONFIG_NF_CT_ACCT
771 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
772 ctnetlink_dump_table_w
,
773 ctnetlink_done
)) != 0)
779 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
780 ctnetlink_dump_table
,
781 ctnetlink_done
)) != 0)
785 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
792 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
795 if (cda
[CTA_TUPLE_ORIG
-1])
796 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
797 else if (cda
[CTA_TUPLE_REPLY
-1])
798 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
805 h
= nf_conntrack_find_get(&tuple
, NULL
);
807 DEBUGP("tuple not found in conntrack hash");
810 DEBUGP("tuple found\n");
811 ct
= nf_ct_tuplehash_to_ctrack(h
);
814 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
819 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
821 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
822 IPCTNL_MSG_CT_NEW
, 1, ct
);
827 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
841 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
844 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
845 d
= ct
->status
^ status
;
847 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
851 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
852 /* SEEN_REPLY bit can only be set */
856 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
857 /* ASSURED bit can only be set */
860 if (cda
[CTA_NAT
-1]) {
861 #ifndef CONFIG_IP_NF_NAT_NEEDED
864 unsigned int hooknum
;
865 struct ip_nat_range range
;
867 if (ctnetlink_parse_nat(cda
, ct
, &range
) < 0)
870 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
871 NIPQUAD(range
.min_ip
), NIPQUAD(range
.max_ip
),
872 htons(range
.min
.all
), htons(range
.max
.all
));
874 /* This is tricky but it works. ip_nat_setup_info needs the
875 * hook number as parameter, so let's do the correct
876 * conversion and run away */
877 if (status
& IPS_SRC_NAT_DONE
)
878 hooknum
= NF_IP_POST_ROUTING
; /* IP_NAT_MANIP_SRC */
879 else if (status
& IPS_DST_NAT_DONE
)
880 hooknum
= NF_IP_PRE_ROUTING
; /* IP_NAT_MANIP_DST */
882 return -EINVAL
; /* Missing NAT flags */
884 DEBUGP("NAT status: %lu\n",
885 status
& (IPS_NAT_MASK
| IPS_NAT_DONE_MASK
));
887 if (ip_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
889 ip_nat_setup_info(ct
, &range
, hooknum
);
891 DEBUGP("NAT status after setup_info: %lu\n",
892 ct
->status
& (IPS_NAT_MASK
| IPS_NAT_DONE_MASK
));
896 /* Be careful here, modifying NAT bits can screw up things,
897 * so don't let users modify them directly if they don't pass
899 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
905 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
907 struct nf_conntrack_helper
*helper
;
908 struct nf_conn_help
*help
= nfct_help(ct
);
912 DEBUGP("entered %s\n", __FUNCTION__
);
915 /* FIXME: we need to reallocate and rehash */
919 /* don't change helper of sibling connections */
923 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
927 helper
= __nf_conntrack_helper_find_byname(helpname
);
929 if (!strcmp(helpname
, ""))
937 /* we had a helper before ... */
938 nf_ct_remove_expectations(ct
);
941 /* need to zero data of old helper */
942 memset(&help
->help
, 0, sizeof(help
->help
));
946 help
->helper
= helper
;
952 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
954 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
956 if (!del_timer(&ct
->timeout
))
959 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
960 add_timer(&ct
->timeout
);
966 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
968 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
969 struct nf_conntrack_protocol
*proto
;
970 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
971 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
974 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
976 proto
= nf_ct_proto_find_get(l3num
, npt
);
978 if (proto
->from_nfattr
)
979 err
= proto
->from_nfattr(tb
, ct
);
980 nf_ct_proto_put(proto
);
986 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
990 DEBUGP("entered %s\n", __FUNCTION__
);
992 if (cda
[CTA_HELP
-1]) {
993 err
= ctnetlink_change_helper(ct
, cda
);
998 if (cda
[CTA_TIMEOUT
-1]) {
999 err
= ctnetlink_change_timeout(ct
, cda
);
1004 if (cda
[CTA_STATUS
-1]) {
1005 err
= ctnetlink_change_status(ct
, cda
);
1010 if (cda
[CTA_PROTOINFO
-1]) {
1011 err
= ctnetlink_change_protoinfo(ct
, cda
);
1016 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1017 if (cda
[CTA_MARK
-1])
1018 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1021 DEBUGP("all done\n");
1026 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1027 struct nf_conntrack_tuple
*otuple
,
1028 struct nf_conntrack_tuple
*rtuple
)
1033 DEBUGP("entered %s\n", __FUNCTION__
);
1035 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1036 if (ct
== NULL
|| IS_ERR(ct
))
1039 if (!cda
[CTA_TIMEOUT
-1])
1041 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1043 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1044 ct
->status
|= IPS_CONFIRMED
;
1046 err
= ctnetlink_change_status(ct
, cda
);
1050 if (cda
[CTA_PROTOINFO
-1]) {
1051 err
= ctnetlink_change_protoinfo(ct
, cda
);
1056 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1057 if (cda
[CTA_MARK
-1])
1058 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1061 add_timer(&ct
->timeout
);
1062 nf_conntrack_hash_insert(ct
);
1064 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1068 nf_conntrack_free(ct
);
1073 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1074 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1076 struct nf_conntrack_tuple otuple
, rtuple
;
1077 struct nf_conntrack_tuple_hash
*h
= NULL
;
1078 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1079 u_int8_t u3
= nfmsg
->nfgen_family
;
1082 DEBUGP("entered %s\n", __FUNCTION__
);
1084 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1087 if (cda
[CTA_TUPLE_ORIG
-1]) {
1088 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1093 if (cda
[CTA_TUPLE_REPLY
-1]) {
1094 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1099 write_lock_bh(&nf_conntrack_lock
);
1100 if (cda
[CTA_TUPLE_ORIG
-1])
1101 h
= __nf_conntrack_find(&otuple
, NULL
);
1102 else if (cda
[CTA_TUPLE_REPLY
-1])
1103 h
= __nf_conntrack_find(&rtuple
, NULL
);
1106 write_unlock_bh(&nf_conntrack_lock
);
1107 DEBUGP("no such conntrack, create new\n");
1109 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1110 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1113 /* implicit 'else' */
1115 /* we only allow nat config for new conntracks */
1116 if (cda
[CTA_NAT
-1]) {
1121 /* We manipulate the conntrack inside the global conntrack table lock,
1122 * so there's no need to increase the refcount */
1123 DEBUGP("conntrack found\n");
1125 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1126 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1129 write_unlock_bh(&nf_conntrack_lock
);
1133 /***********************************************************************
1135 ***********************************************************************/
1138 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1139 const struct nf_conntrack_tuple
*tuple
,
1140 enum ctattr_expect type
)
1142 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1144 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1145 goto nfattr_failure
;
1147 NFA_NEST_END(skb
, nest_parms
);
1156 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1157 const struct nf_conntrack_expect
*exp
)
1159 struct nf_conn
*master
= exp
->master
;
1160 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1161 u_int32_t id
= htonl(exp
->id
);
1163 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1164 goto nfattr_failure
;
1165 if (ctnetlink_exp_dump_tuple(skb
, &exp
->mask
, CTA_EXPECT_MASK
) < 0)
1166 goto nfattr_failure
;
1167 if (ctnetlink_exp_dump_tuple(skb
,
1168 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1169 CTA_EXPECT_MASTER
) < 0)
1170 goto nfattr_failure
;
1172 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1173 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1182 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1185 const struct nf_conntrack_expect
*exp
)
1187 struct nlmsghdr
*nlh
;
1188 struct nfgenmsg
*nfmsg
;
1193 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1194 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1195 nfmsg
= NLMSG_DATA(nlh
);
1197 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1198 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1199 nfmsg
->version
= NFNETLINK_V0
;
1202 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1203 goto nfattr_failure
;
1205 nlh
->nlmsg_len
= skb
->tail
- b
;
1210 skb_trim(skb
, b
- skb
->data
);
1214 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1215 static int ctnetlink_expect_event(struct notifier_block
*this,
1216 unsigned long events
, void *ptr
)
1218 struct nlmsghdr
*nlh
;
1219 struct nfgenmsg
*nfmsg
;
1220 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1221 struct sk_buff
*skb
;
1226 if (events
& IPEXP_NEW
) {
1227 type
= IPCTNL_MSG_EXP_NEW
;
1228 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1232 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1238 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1239 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1240 nfmsg
= NLMSG_DATA(nlh
);
1242 nlh
->nlmsg_flags
= flags
;
1243 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1244 nfmsg
->version
= NFNETLINK_V0
;
1247 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1248 goto nfattr_failure
;
1250 nlh
->nlmsg_len
= skb
->tail
- b
;
1251 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1262 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1264 struct nf_conntrack_expect
*exp
= NULL
;
1265 struct list_head
*i
;
1266 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1267 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1268 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1270 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1272 read_lock_bh(&nf_conntrack_lock
);
1273 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1274 exp
= (struct nf_conntrack_expect
*) i
;
1275 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1279 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1287 read_unlock_bh(&nf_conntrack_lock
);
1289 DEBUGP("leaving, last id=%llu\n", *id
);
1294 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1295 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1296 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1300 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1301 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1303 struct nf_conntrack_tuple tuple
;
1304 struct nf_conntrack_expect
*exp
;
1305 struct sk_buff
*skb2
;
1306 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1307 u_int8_t u3
= nfmsg
->nfgen_family
;
1310 DEBUGP("entered %s\n", __FUNCTION__
);
1312 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1315 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1318 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1319 ctnetlink_exp_dump_table
,
1320 ctnetlink_done
)) != 0)
1322 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1323 if (rlen
> skb
->len
)
1325 skb_pull(skb
, rlen
);
1329 if (cda
[CTA_EXPECT_MASTER
-1])
1330 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1337 exp
= nf_conntrack_expect_find(&tuple
);
1341 if (cda
[CTA_EXPECT_ID
-1]) {
1342 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1343 if (exp
->id
!= ntohl(id
)) {
1344 nf_conntrack_expect_put(exp
);
1350 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1353 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1355 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1356 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1361 nf_conntrack_expect_put(exp
);
1363 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1368 nf_conntrack_expect_put(exp
);
1373 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1374 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1376 struct nf_conntrack_expect
*exp
, *tmp
;
1377 struct nf_conntrack_tuple tuple
;
1378 struct nf_conntrack_helper
*h
;
1379 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1380 u_int8_t u3
= nfmsg
->nfgen_family
;
1383 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1386 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1387 /* delete a single expect by tuple */
1388 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1392 /* bump usage count to 2 */
1393 exp
= nf_conntrack_expect_find(&tuple
);
1397 if (cda
[CTA_EXPECT_ID
-1]) {
1399 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1400 if (exp
->id
!= ntohl(id
)) {
1401 nf_conntrack_expect_put(exp
);
1406 /* after list removal, usage count == 1 */
1407 nf_conntrack_unexpect_related(exp
);
1408 /* have to put what we 'get' above.
1409 * after this line usage count == 0 */
1410 nf_conntrack_expect_put(exp
);
1411 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1412 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1414 /* delete all expectations for this helper */
1415 write_lock_bh(&nf_conntrack_lock
);
1416 h
= __nf_conntrack_helper_find_byname(name
);
1418 write_unlock_bh(&nf_conntrack_lock
);
1421 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1423 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1424 if (m_help
->helper
== h
1425 && del_timer(&exp
->timeout
)) {
1426 nf_ct_unlink_expect(exp
);
1427 nf_conntrack_expect_put(exp
);
1430 write_unlock_bh(&nf_conntrack_lock
);
1432 /* This basically means we have to flush everything*/
1433 write_lock_bh(&nf_conntrack_lock
);
1434 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1436 if (del_timer(&exp
->timeout
)) {
1437 nf_ct_unlink_expect(exp
);
1438 nf_conntrack_expect_put(exp
);
1441 write_unlock_bh(&nf_conntrack_lock
);
1447 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1453 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1455 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1456 struct nf_conntrack_tuple_hash
*h
= NULL
;
1457 struct nf_conntrack_expect
*exp
;
1459 struct nf_conn_help
*help
;
1462 DEBUGP("entered %s\n", __FUNCTION__
);
1464 /* caller guarantees that those three CTA_EXPECT_* exist */
1465 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1468 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1471 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1475 /* Look for master conntrack of this expectation */
1476 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1479 ct
= nf_ct_tuplehash_to_ctrack(h
);
1480 help
= nfct_help(ct
);
1482 if (!help
|| !help
->helper
) {
1483 /* such conntrack hasn't got any helper, abort */
1488 exp
= nf_conntrack_expect_alloc(ct
);
1494 exp
->expectfn
= NULL
;
1497 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1498 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1500 err
= nf_conntrack_expect_related(exp
);
1501 nf_conntrack_expect_put(exp
);
1504 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1509 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1510 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1512 struct nf_conntrack_tuple tuple
;
1513 struct nf_conntrack_expect
*exp
;
1514 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1515 u_int8_t u3
= nfmsg
->nfgen_family
;
1518 DEBUGP("entered %s\n", __FUNCTION__
);
1520 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1523 if (!cda
[CTA_EXPECT_TUPLE
-1]
1524 || !cda
[CTA_EXPECT_MASK
-1]
1525 || !cda
[CTA_EXPECT_MASTER
-1])
1528 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1532 write_lock_bh(&nf_conntrack_lock
);
1533 exp
= __nf_conntrack_expect_find(&tuple
);
1536 write_unlock_bh(&nf_conntrack_lock
);
1538 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1539 err
= ctnetlink_create_expect(cda
, u3
);
1544 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1545 err
= ctnetlink_change_expect(exp
, cda
);
1546 write_unlock_bh(&nf_conntrack_lock
);
1548 DEBUGP("leaving\n");
1553 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1554 static struct notifier_block ctnl_notifier
= {
1555 .notifier_call
= ctnetlink_conntrack_event
,
1558 static struct notifier_block ctnl_notifier_exp
= {
1559 .notifier_call
= ctnetlink_expect_event
,
1563 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1564 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1565 .attr_count
= CTA_MAX
, },
1566 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1567 .attr_count
= CTA_MAX
, },
1568 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1569 .attr_count
= CTA_MAX
, },
1570 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1571 .attr_count
= CTA_MAX
, },
1574 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1575 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1576 .attr_count
= CTA_EXPECT_MAX
, },
1577 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1578 .attr_count
= CTA_EXPECT_MAX
, },
1579 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1580 .attr_count
= CTA_EXPECT_MAX
, },
1583 static struct nfnetlink_subsystem ctnl_subsys
= {
1584 .name
= "conntrack",
1585 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1586 .cb_count
= IPCTNL_MSG_MAX
,
1590 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1591 .name
= "conntrack_expect",
1592 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1593 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1597 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1598 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1600 static int __init
ctnetlink_init(void)
1604 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1605 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1607 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1611 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1613 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1614 goto err_unreg_subsys
;
1617 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1618 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1620 printk("ctnetlink_init: cannot register notifier.\n");
1621 goto err_unreg_exp_subsys
;
1624 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1626 printk("ctnetlink_init: cannot expect register notifier.\n");
1627 goto err_unreg_notifier
;
1633 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1635 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1636 err_unreg_exp_subsys
:
1637 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1640 nfnetlink_subsys_unregister(&ctnl_subsys
);
1645 static void __exit
ctnetlink_exit(void)
1647 printk("ctnetlink: unregistering from nfnetlink.\n");
1649 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1650 nf_conntrack_unregister_notifier(&ctnl_notifier_exp
);
1651 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1654 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1655 nfnetlink_subsys_unregister(&ctnl_subsys
);
1659 module_init(ctnetlink_init
);
1660 module_exit(ctnetlink_exit
);