1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
9 * I've reworked this stuff to use attributes instead of conntrack
10 * structures. 5.44 am. I need more tea. --pablo 05/07/11.
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
20 * Derived from ip_conntrack_netlink.c: Port by Pablo Neira Ayuso (05/11/14)
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/timer.h>
28 #include <linux/skbuff.h>
29 #include <linux/errno.h>
30 #include <linux/netlink.h>
31 #include <linux/spinlock.h>
32 #include <linux/notifier.h>
34 #include <linux/netfilter.h>
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_core.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
42 #include <linux/netfilter/nfnetlink.h>
43 #include <linux/netfilter/nfnetlink_conntrack.h>
45 MODULE_LICENSE("GPL");
47 static char __initdata version
[] = "0.93";
52 #define DEBUGP(format, args...)
57 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
58 const struct nf_conntrack_tuple
*tuple
,
59 struct nf_conntrack_protocol
*proto
)
62 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
64 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
66 if (likely(proto
->tuple_to_nfattr
))
67 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
69 NFA_NEST_END(skb
, nest_parms
);
78 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
79 const struct nf_conntrack_tuple
*tuple
,
80 struct nf_conntrack_l3proto
*l3proto
)
83 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
85 if (likely(l3proto
->tuple_to_nfattr
))
86 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
88 NFA_NEST_END(skb
, nest_parms
);
97 ctnetlink_dump_tuples(struct sk_buff
*skb
,
98 const struct nf_conntrack_tuple
*tuple
)
101 struct nf_conntrack_l3proto
*l3proto
;
102 struct nf_conntrack_protocol
*proto
;
104 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
105 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
106 nf_ct_l3proto_put(l3proto
);
108 if (unlikely(ret
< 0))
111 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
112 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, proto
);
113 nf_ct_proto_put(proto
);
119 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
121 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
122 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
130 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
132 long timeout_l
= ct
->timeout
.expires
- jiffies
;
138 timeout
= htonl(timeout_l
/ HZ
);
140 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
148 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
150 struct nf_conntrack_protocol
*proto
= nf_ct_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
, ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
151 struct nfattr
*nest_proto
;
154 if (!proto
->to_nfattr
) {
155 nf_ct_proto_put(proto
);
159 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
161 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
163 nf_ct_proto_put(proto
);
165 NFA_NEST_END(skb
, nest_proto
);
174 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
176 struct nfattr
*nest_helper
;
177 const struct nf_conn_help
*help
= nfct_help(ct
);
179 if (!help
|| !help
->helper
)
182 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
183 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(help
->helper
->name
), help
->helper
->name
);
185 if (help
->helper
->to_nfattr
)
186 help
->helper
->to_nfattr(skb
, ct
);
188 NFA_NEST_END(skb
, nest_helper
);
196 #ifdef CONFIG_NF_CT_ACCT
198 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
199 enum ip_conntrack_dir dir
)
201 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
202 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
205 tmp
= htonl(ct
->counters
[dir
].packets
);
206 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
208 tmp
= htonl(ct
->counters
[dir
].bytes
);
209 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
211 NFA_NEST_END(skb
, nest_count
);
219 #define ctnetlink_dump_counters(a, b, c) (0)
222 #ifdef CONFIG_NF_CONNTRACK_MARK
224 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
226 u_int32_t mark
= htonl(ct
->mark
);
228 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
235 #define ctnetlink_dump_mark(a, b) (0)
239 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
241 u_int32_t id
= htonl(ct
->id
);
242 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
250 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
252 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
254 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
261 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
264 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
265 int event
, int nowait
,
266 const struct nf_conn
*ct
)
268 struct nlmsghdr
*nlh
;
269 struct nfgenmsg
*nfmsg
;
270 struct nfattr
*nest_parms
;
275 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
276 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
277 nfmsg
= NLMSG_DATA(nlh
);
279 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
280 nfmsg
->nfgen_family
=
281 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
282 nfmsg
->version
= NFNETLINK_V0
;
285 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
286 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
288 NFA_NEST_END(skb
, nest_parms
);
290 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
291 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
293 NFA_NEST_END(skb
, nest_parms
);
295 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
296 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
297 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
298 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
299 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
300 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
301 ctnetlink_dump_mark(skb
, ct
) < 0 ||
302 ctnetlink_dump_id(skb
, ct
) < 0 ||
303 ctnetlink_dump_use(skb
, ct
) < 0)
306 nlh
->nlmsg_len
= skb
->tail
- b
;
311 skb_trim(skb
, b
- skb
->data
);
315 #ifdef CONFIG_NF_CONNTRACK_EVENTS
316 static int ctnetlink_conntrack_event(struct notifier_block
*this,
317 unsigned long events
, void *ptr
)
319 struct nlmsghdr
*nlh
;
320 struct nfgenmsg
*nfmsg
;
321 struct nfattr
*nest_parms
;
322 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
326 unsigned int flags
= 0, group
;
328 /* ignore our fake conntrack entry */
329 if (ct
== &nf_conntrack_untracked
)
332 if (events
& IPCT_DESTROY
) {
333 type
= IPCTNL_MSG_CT_DELETE
;
334 group
= NFNLGRP_CONNTRACK_DESTROY
;
335 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
336 type
= IPCTNL_MSG_CT_NEW
;
337 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
338 /* dump everything */
340 group
= NFNLGRP_CONNTRACK_NEW
;
341 } else if (events
& (IPCT_STATUS
|
346 type
= IPCTNL_MSG_CT_NEW
;
347 group
= NFNLGRP_CONNTRACK_UPDATE
;
351 if (!nfnetlink_has_listeners(group
))
354 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
360 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
361 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
362 nfmsg
= NLMSG_DATA(nlh
);
364 nlh
->nlmsg_flags
= flags
;
365 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
366 nfmsg
->version
= NFNETLINK_V0
;
369 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
370 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
372 NFA_NEST_END(skb
, nest_parms
);
374 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
375 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
377 NFA_NEST_END(skb
, nest_parms
);
379 /* NAT stuff is now a status flag */
380 if ((events
& IPCT_STATUS
|| events
& IPCT_NATINFO
)
381 && ctnetlink_dump_status(skb
, ct
) < 0)
383 if (events
& IPCT_REFRESH
384 && ctnetlink_dump_timeout(skb
, ct
) < 0)
386 if (events
& IPCT_PROTOINFO
387 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
389 if (events
& IPCT_HELPINFO
390 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
393 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
394 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
397 nlh
->nlmsg_len
= skb
->tail
- b
;
398 nfnetlink_send(skb
, 0, group
, 0);
406 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
408 static int ctnetlink_done(struct netlink_callback
*cb
)
410 DEBUGP("entered %s\n", __FUNCTION__
);
414 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
417 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
419 struct nf_conn
*ct
= NULL
;
420 struct nf_conntrack_tuple_hash
*h
;
422 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
423 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
424 u_int8_t l3proto
= nfmsg
->nfgen_family
;
426 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
429 read_lock_bh(&nf_conntrack_lock
);
430 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
431 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
432 h
= (struct nf_conntrack_tuple_hash
*) i
;
433 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
435 ct
= nf_ct_tuplehash_to_ctrack(h
);
436 /* Dump entries of a given L3 protocol number.
437 * If it is not specified, ie. l3proto == 0,
438 * then dump everything. */
439 if (l3proto
&& L3PROTO(ct
) != l3proto
)
443 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
452 read_unlock_bh(&nf_conntrack_lock
);
454 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
459 #ifdef CONFIG_NF_CT_ACCT
461 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
463 struct nf_conn
*ct
= NULL
;
464 struct nf_conntrack_tuple_hash
*h
;
466 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
467 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
468 u_int8_t l3proto
= nfmsg
->nfgen_family
;
470 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
473 write_lock_bh(&nf_conntrack_lock
);
474 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
475 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
476 h
= (struct nf_conntrack_tuple_hash
*) i
;
477 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
479 ct
= nf_ct_tuplehash_to_ctrack(h
);
480 if (l3proto
&& L3PROTO(ct
) != l3proto
)
484 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
491 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
495 write_unlock_bh(&nf_conntrack_lock
);
497 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
504 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
506 struct nfattr
*tb
[CTA_IP_MAX
];
507 struct nf_conntrack_l3proto
*l3proto
;
510 DEBUGP("entered %s\n", __FUNCTION__
);
512 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
514 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
516 if (likely(l3proto
->nfattr_to_tuple
))
517 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
519 nf_ct_l3proto_put(l3proto
);
526 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
527 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
531 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
532 struct nf_conntrack_tuple
*tuple
)
534 struct nfattr
*tb
[CTA_PROTO_MAX
];
535 struct nf_conntrack_protocol
*proto
;
538 DEBUGP("entered %s\n", __FUNCTION__
);
540 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
542 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
545 if (!tb
[CTA_PROTO_NUM
-1])
547 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
549 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
551 if (likely(proto
->nfattr_to_tuple
))
552 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
554 nf_ct_proto_put(proto
);
560 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
561 enum ctattr_tuple type
, u_int8_t l3num
)
563 struct nfattr
*tb
[CTA_TUPLE_MAX
];
566 DEBUGP("entered %s\n", __FUNCTION__
);
568 memset(tuple
, 0, sizeof(*tuple
));
570 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
572 if (!tb
[CTA_TUPLE_IP
-1])
575 tuple
->src
.l3num
= l3num
;
577 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
581 if (!tb
[CTA_TUPLE_PROTO
-1])
584 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
588 /* orig and expect tuples get DIR_ORIGINAL */
589 if (type
== CTA_TUPLE_REPLY
)
590 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
592 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
594 NF_CT_DUMP_TUPLE(tuple
);
601 #ifdef CONFIG_IP_NF_NAT_NEEDED
602 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
603 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
604 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
607 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
608 const struct nf_conn
*ct
,
609 struct ip_nat_range
*range
)
611 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
612 struct ip_nat_protocol
*npt
;
614 DEBUGP("entered %s\n", __FUNCTION__
);
616 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
618 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
621 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
623 if (!npt
->nfattr_to_range
) {
624 ip_nat_proto_put(npt
);
628 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
629 if (npt
->nfattr_to_range(tb
, range
) > 0)
630 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
632 ip_nat_proto_put(npt
);
638 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
639 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
640 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
644 ctnetlink_parse_nat(struct nfattr
*cda
[],
645 const struct nf_conn
*ct
, struct ip_nat_range
*range
)
647 struct nfattr
*tb
[CTA_NAT_MAX
];
650 DEBUGP("entered %s\n", __FUNCTION__
);
652 memset(range
, 0, sizeof(*range
));
654 nfattr_parse_nested(tb
, CTA_NAT_MAX
, cda
[CTA_NAT
-1]);
656 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
659 if (tb
[CTA_NAT_MINIP
-1])
660 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
662 if (!tb
[CTA_NAT_MAXIP
-1])
663 range
->max_ip
= range
->min_ip
;
665 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
668 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
670 if (!tb
[CTA_NAT_PROTO
-1])
673 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
683 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
685 struct nfattr
*tb
[CTA_HELP_MAX
];
687 DEBUGP("entered %s\n", __FUNCTION__
);
689 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
691 if (!tb
[CTA_HELP_NAME
-1])
694 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
699 static const size_t cta_min
[CTA_MAX
] = {
700 [CTA_STATUS
-1] = sizeof(u_int32_t
),
701 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
702 [CTA_MARK
-1] = sizeof(u_int32_t
),
703 [CTA_USE
-1] = sizeof(u_int32_t
),
704 [CTA_ID
-1] = sizeof(u_int32_t
)
708 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
709 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
711 struct nf_conntrack_tuple_hash
*h
;
712 struct nf_conntrack_tuple tuple
;
714 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
715 u_int8_t u3
= nfmsg
->nfgen_family
;
718 DEBUGP("entered %s\n", __FUNCTION__
);
720 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
723 if (cda
[CTA_TUPLE_ORIG
-1])
724 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
725 else if (cda
[CTA_TUPLE_REPLY
-1])
726 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
728 /* Flush the whole table */
729 nf_conntrack_flush();
736 h
= nf_conntrack_find_get(&tuple
, NULL
);
738 DEBUGP("tuple not found in conntrack hash\n");
742 ct
= nf_ct_tuplehash_to_ctrack(h
);
745 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
751 if (del_timer(&ct
->timeout
))
752 ct
->timeout
.function((unsigned long)ct
);
761 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
762 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
764 struct nf_conntrack_tuple_hash
*h
;
765 struct nf_conntrack_tuple tuple
;
767 struct sk_buff
*skb2
= NULL
;
768 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
769 u_int8_t u3
= nfmsg
->nfgen_family
;
772 DEBUGP("entered %s\n", __FUNCTION__
);
774 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
777 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
778 IPCTNL_MSG_CT_GET_CTRZERO
) {
779 #ifdef CONFIG_NF_CT_ACCT
780 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
781 ctnetlink_dump_table_w
,
782 ctnetlink_done
)) != 0)
788 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
789 ctnetlink_dump_table
,
790 ctnetlink_done
)) != 0)
794 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
801 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
804 if (cda
[CTA_TUPLE_ORIG
-1])
805 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
806 else if (cda
[CTA_TUPLE_REPLY
-1])
807 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
814 h
= nf_conntrack_find_get(&tuple
, NULL
);
816 DEBUGP("tuple not found in conntrack hash");
819 DEBUGP("tuple found\n");
820 ct
= nf_ct_tuplehash_to_ctrack(h
);
823 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
828 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
830 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
831 IPCTNL_MSG_CT_NEW
, 1, ct
);
836 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
850 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
853 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
854 d
= ct
->status
^ status
;
856 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
860 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
861 /* SEEN_REPLY bit can only be set */
865 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
866 /* ASSURED bit can only be set */
869 if (cda
[CTA_NAT
-1]) {
870 #ifndef CONFIG_IP_NF_NAT_NEEDED
873 unsigned int hooknum
;
874 struct ip_nat_range range
;
876 if (ctnetlink_parse_nat(cda
, ct
, &range
) < 0)
879 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
880 NIPQUAD(range
.min_ip
), NIPQUAD(range
.max_ip
),
881 htons(range
.min
.all
), htons(range
.max
.all
));
883 /* This is tricky but it works. ip_nat_setup_info needs the
884 * hook number as parameter, so let's do the correct
885 * conversion and run away */
886 if (status
& IPS_SRC_NAT_DONE
)
887 hooknum
= NF_IP_POST_ROUTING
; /* IP_NAT_MANIP_SRC */
888 else if (status
& IPS_DST_NAT_DONE
)
889 hooknum
= NF_IP_PRE_ROUTING
; /* IP_NAT_MANIP_DST */
891 return -EINVAL
; /* Missing NAT flags */
893 DEBUGP("NAT status: %lu\n",
894 status
& (IPS_NAT_MASK
| IPS_NAT_DONE_MASK
));
896 if (ip_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
898 ip_nat_setup_info(ct
, &range
, hooknum
);
900 DEBUGP("NAT status after setup_info: %lu\n",
901 ct
->status
& (IPS_NAT_MASK
| IPS_NAT_DONE_MASK
));
905 /* Be careful here, modifying NAT bits can screw up things,
906 * so don't let users modify them directly if they don't pass
908 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
914 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
916 struct nf_conntrack_helper
*helper
;
917 struct nf_conn_help
*help
= nfct_help(ct
);
921 DEBUGP("entered %s\n", __FUNCTION__
);
924 /* FIXME: we need to reallocate and rehash */
928 /* don't change helper of sibling connections */
932 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
936 helper
= __nf_conntrack_helper_find_byname(helpname
);
938 if (!strcmp(helpname
, ""))
946 /* we had a helper before ... */
947 nf_ct_remove_expectations(ct
);
950 /* need to zero data of old helper */
951 memset(&help
->help
, 0, sizeof(help
->help
));
955 help
->helper
= helper
;
961 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
963 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
965 if (!del_timer(&ct
->timeout
))
968 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
969 add_timer(&ct
->timeout
);
975 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
977 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
978 struct nf_conntrack_protocol
*proto
;
979 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
980 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
983 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
985 proto
= nf_ct_proto_find_get(l3num
, npt
);
987 if (proto
->from_nfattr
)
988 err
= proto
->from_nfattr(tb
, ct
);
989 nf_ct_proto_put(proto
);
995 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
999 DEBUGP("entered %s\n", __FUNCTION__
);
1001 if (cda
[CTA_HELP
-1]) {
1002 err
= ctnetlink_change_helper(ct
, cda
);
1007 if (cda
[CTA_TIMEOUT
-1]) {
1008 err
= ctnetlink_change_timeout(ct
, cda
);
1013 if (cda
[CTA_STATUS
-1]) {
1014 err
= ctnetlink_change_status(ct
, cda
);
1019 if (cda
[CTA_PROTOINFO
-1]) {
1020 err
= ctnetlink_change_protoinfo(ct
, cda
);
1025 #if defined(CONFIG_NF_CONNTRACK_MARK)
1026 if (cda
[CTA_MARK
-1])
1027 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1030 DEBUGP("all done\n");
1035 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1036 struct nf_conntrack_tuple
*otuple
,
1037 struct nf_conntrack_tuple
*rtuple
)
1042 DEBUGP("entered %s\n", __FUNCTION__
);
1044 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1045 if (ct
== NULL
|| IS_ERR(ct
))
1048 if (!cda
[CTA_TIMEOUT
-1])
1050 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1052 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1053 ct
->status
|= IPS_CONFIRMED
;
1055 err
= ctnetlink_change_status(ct
, cda
);
1059 if (cda
[CTA_PROTOINFO
-1]) {
1060 err
= ctnetlink_change_protoinfo(ct
, cda
);
1065 #if defined(CONFIG_NF_CONNTRACK_MARK)
1066 if (cda
[CTA_MARK
-1])
1067 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1070 add_timer(&ct
->timeout
);
1071 nf_conntrack_hash_insert(ct
);
1073 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1077 nf_conntrack_free(ct
);
1082 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1083 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1085 struct nf_conntrack_tuple otuple
, rtuple
;
1086 struct nf_conntrack_tuple_hash
*h
= NULL
;
1087 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1088 u_int8_t u3
= nfmsg
->nfgen_family
;
1091 DEBUGP("entered %s\n", __FUNCTION__
);
1093 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1096 if (cda
[CTA_TUPLE_ORIG
-1]) {
1097 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1102 if (cda
[CTA_TUPLE_REPLY
-1]) {
1103 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1108 write_lock_bh(&nf_conntrack_lock
);
1109 if (cda
[CTA_TUPLE_ORIG
-1])
1110 h
= __nf_conntrack_find(&otuple
, NULL
);
1111 else if (cda
[CTA_TUPLE_REPLY
-1])
1112 h
= __nf_conntrack_find(&rtuple
, NULL
);
1115 write_unlock_bh(&nf_conntrack_lock
);
1116 DEBUGP("no such conntrack, create new\n");
1118 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1119 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1122 /* implicit 'else' */
1124 /* we only allow nat config for new conntracks */
1125 if (cda
[CTA_NAT
-1]) {
1130 /* We manipulate the conntrack inside the global conntrack table lock,
1131 * so there's no need to increase the refcount */
1132 DEBUGP("conntrack found\n");
1134 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1135 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1138 write_unlock_bh(&nf_conntrack_lock
);
1142 /***********************************************************************
1144 ***********************************************************************/
1147 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1148 const struct nf_conntrack_tuple
*tuple
,
1149 enum ctattr_expect type
)
1151 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1153 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1154 goto nfattr_failure
;
1156 NFA_NEST_END(skb
, nest_parms
);
1165 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1166 const struct nf_conntrack_tuple
*tuple
,
1167 const struct nf_conntrack_tuple
*mask
)
1170 struct nf_conntrack_l3proto
*l3proto
;
1171 struct nf_conntrack_protocol
*proto
;
1172 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1174 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1175 ret
= ctnetlink_dump_tuples_ip(skb
, mask
, l3proto
);
1176 nf_ct_l3proto_put(l3proto
);
1178 if (unlikely(ret
< 0))
1179 goto nfattr_failure
;
1181 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1182 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1183 nf_ct_proto_put(proto
);
1184 if (unlikely(ret
< 0))
1185 goto nfattr_failure
;
1187 NFA_NEST_END(skb
, nest_parms
);
1196 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1197 const struct nf_conntrack_expect
*exp
)
1199 struct nf_conn
*master
= exp
->master
;
1200 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1201 u_int32_t id
= htonl(exp
->id
);
1203 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1204 goto nfattr_failure
;
1205 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1206 goto nfattr_failure
;
1207 if (ctnetlink_exp_dump_tuple(skb
,
1208 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1209 CTA_EXPECT_MASTER
) < 0)
1210 goto nfattr_failure
;
1212 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1213 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1222 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1225 const struct nf_conntrack_expect
*exp
)
1227 struct nlmsghdr
*nlh
;
1228 struct nfgenmsg
*nfmsg
;
1233 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1234 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1235 nfmsg
= NLMSG_DATA(nlh
);
1237 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1238 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1239 nfmsg
->version
= NFNETLINK_V0
;
1242 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1243 goto nfattr_failure
;
1245 nlh
->nlmsg_len
= skb
->tail
- b
;
1250 skb_trim(skb
, b
- skb
->data
);
1254 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1255 static int ctnetlink_expect_event(struct notifier_block
*this,
1256 unsigned long events
, void *ptr
)
1258 struct nlmsghdr
*nlh
;
1259 struct nfgenmsg
*nfmsg
;
1260 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1261 struct sk_buff
*skb
;
1266 if (events
& IPEXP_NEW
) {
1267 type
= IPCTNL_MSG_EXP_NEW
;
1268 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1272 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1278 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1279 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1280 nfmsg
= NLMSG_DATA(nlh
);
1282 nlh
->nlmsg_flags
= flags
;
1283 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1284 nfmsg
->version
= NFNETLINK_V0
;
1287 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1288 goto nfattr_failure
;
1290 nlh
->nlmsg_len
= skb
->tail
- b
;
1291 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1302 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1304 struct nf_conntrack_expect
*exp
= NULL
;
1305 struct list_head
*i
;
1306 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1307 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1308 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1310 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1312 read_lock_bh(&nf_conntrack_lock
);
1313 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1314 exp
= (struct nf_conntrack_expect
*) i
;
1315 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1319 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1327 read_unlock_bh(&nf_conntrack_lock
);
1329 DEBUGP("leaving, last id=%llu\n", *id
);
1334 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1335 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1336 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1340 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1341 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1343 struct nf_conntrack_tuple tuple
;
1344 struct nf_conntrack_expect
*exp
;
1345 struct sk_buff
*skb2
;
1346 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1347 u_int8_t u3
= nfmsg
->nfgen_family
;
1350 DEBUGP("entered %s\n", __FUNCTION__
);
1352 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1355 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1358 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1359 ctnetlink_exp_dump_table
,
1360 ctnetlink_done
)) != 0)
1362 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1363 if (rlen
> skb
->len
)
1365 skb_pull(skb
, rlen
);
1369 if (cda
[CTA_EXPECT_MASTER
-1])
1370 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1377 exp
= nf_conntrack_expect_find(&tuple
);
1381 if (cda
[CTA_EXPECT_ID
-1]) {
1382 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1383 if (exp
->id
!= ntohl(id
)) {
1384 nf_conntrack_expect_put(exp
);
1390 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1393 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1395 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1396 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1401 nf_conntrack_expect_put(exp
);
1403 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1408 nf_conntrack_expect_put(exp
);
1413 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1414 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1416 struct nf_conntrack_expect
*exp
, *tmp
;
1417 struct nf_conntrack_tuple tuple
;
1418 struct nf_conntrack_helper
*h
;
1419 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1420 u_int8_t u3
= nfmsg
->nfgen_family
;
1423 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1426 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1427 /* delete a single expect by tuple */
1428 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1432 /* bump usage count to 2 */
1433 exp
= nf_conntrack_expect_find(&tuple
);
1437 if (cda
[CTA_EXPECT_ID
-1]) {
1439 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1440 if (exp
->id
!= ntohl(id
)) {
1441 nf_conntrack_expect_put(exp
);
1446 /* after list removal, usage count == 1 */
1447 nf_conntrack_unexpect_related(exp
);
1448 /* have to put what we 'get' above.
1449 * after this line usage count == 0 */
1450 nf_conntrack_expect_put(exp
);
1451 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1452 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1454 /* delete all expectations for this helper */
1455 write_lock_bh(&nf_conntrack_lock
);
1456 h
= __nf_conntrack_helper_find_byname(name
);
1458 write_unlock_bh(&nf_conntrack_lock
);
1461 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1463 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1464 if (m_help
->helper
== h
1465 && del_timer(&exp
->timeout
)) {
1466 nf_ct_unlink_expect(exp
);
1467 nf_conntrack_expect_put(exp
);
1470 write_unlock_bh(&nf_conntrack_lock
);
1472 /* This basically means we have to flush everything*/
1473 write_lock_bh(&nf_conntrack_lock
);
1474 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1476 if (del_timer(&exp
->timeout
)) {
1477 nf_ct_unlink_expect(exp
);
1478 nf_conntrack_expect_put(exp
);
1481 write_unlock_bh(&nf_conntrack_lock
);
1487 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1493 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1495 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1496 struct nf_conntrack_tuple_hash
*h
= NULL
;
1497 struct nf_conntrack_expect
*exp
;
1499 struct nf_conn_help
*help
;
1502 DEBUGP("entered %s\n", __FUNCTION__
);
1504 /* caller guarantees that those three CTA_EXPECT_* exist */
1505 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1508 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1511 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1515 /* Look for master conntrack of this expectation */
1516 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1519 ct
= nf_ct_tuplehash_to_ctrack(h
);
1520 help
= nfct_help(ct
);
1522 if (!help
|| !help
->helper
) {
1523 /* such conntrack hasn't got any helper, abort */
1528 exp
= nf_conntrack_expect_alloc(ct
);
1534 exp
->expectfn
= NULL
;
1537 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1538 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1540 err
= nf_conntrack_expect_related(exp
);
1541 nf_conntrack_expect_put(exp
);
1544 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1549 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1550 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1552 struct nf_conntrack_tuple tuple
;
1553 struct nf_conntrack_expect
*exp
;
1554 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1555 u_int8_t u3
= nfmsg
->nfgen_family
;
1558 DEBUGP("entered %s\n", __FUNCTION__
);
1560 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1563 if (!cda
[CTA_EXPECT_TUPLE
-1]
1564 || !cda
[CTA_EXPECT_MASK
-1]
1565 || !cda
[CTA_EXPECT_MASTER
-1])
1568 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1572 write_lock_bh(&nf_conntrack_lock
);
1573 exp
= __nf_conntrack_expect_find(&tuple
);
1576 write_unlock_bh(&nf_conntrack_lock
);
1578 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1579 err
= ctnetlink_create_expect(cda
, u3
);
1584 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1585 err
= ctnetlink_change_expect(exp
, cda
);
1586 write_unlock_bh(&nf_conntrack_lock
);
1588 DEBUGP("leaving\n");
1593 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1594 static struct notifier_block ctnl_notifier
= {
1595 .notifier_call
= ctnetlink_conntrack_event
,
1598 static struct notifier_block ctnl_notifier_exp
= {
1599 .notifier_call
= ctnetlink_expect_event
,
1603 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1604 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1605 .attr_count
= CTA_MAX
, },
1606 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1607 .attr_count
= CTA_MAX
, },
1608 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1609 .attr_count
= CTA_MAX
, },
1610 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1611 .attr_count
= CTA_MAX
, },
1614 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1615 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1616 .attr_count
= CTA_EXPECT_MAX
, },
1617 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1618 .attr_count
= CTA_EXPECT_MAX
, },
1619 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1620 .attr_count
= CTA_EXPECT_MAX
, },
1623 static struct nfnetlink_subsystem ctnl_subsys
= {
1624 .name
= "conntrack",
1625 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1626 .cb_count
= IPCTNL_MSG_MAX
,
1630 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1631 .name
= "conntrack_expect",
1632 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1633 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1637 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1638 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1640 static int __init
ctnetlink_init(void)
1644 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1645 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1647 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1651 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1653 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1654 goto err_unreg_subsys
;
1657 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1658 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1660 printk("ctnetlink_init: cannot register notifier.\n");
1661 goto err_unreg_exp_subsys
;
1664 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1666 printk("ctnetlink_init: cannot expect register notifier.\n");
1667 goto err_unreg_notifier
;
1673 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1675 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1676 err_unreg_exp_subsys
:
1677 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1680 nfnetlink_subsys_unregister(&ctnl_subsys
);
1685 static void __exit
ctnetlink_exit(void)
1687 printk("ctnetlink: unregistering from nfnetlink.\n");
1689 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1690 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1691 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1694 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1695 nfnetlink_subsys_unregister(&ctnl_subsys
);
1699 module_init(ctnetlink_init
);
1700 module_exit(ctnetlink_exit
);