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/interrupt.h>
33 #include <linux/notifier.h>
35 #include <linux/netfilter.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_helper.h>
39 #include <net/netfilter/nf_conntrack_l3proto.h>
40 #include <net/netfilter/nf_conntrack_protocol.h>
41 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
43 #include <linux/netfilter/nfnetlink.h>
44 #include <linux/netfilter/nfnetlink_conntrack.h>
46 MODULE_LICENSE("GPL");
48 static char __initdata version
[] = "0.93";
53 #define DEBUGP(format, args...)
58 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
59 const struct nf_conntrack_tuple
*tuple
,
60 struct nf_conntrack_protocol
*proto
)
63 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
65 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
67 if (likely(proto
->tuple_to_nfattr
))
68 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
70 NFA_NEST_END(skb
, nest_parms
);
79 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
80 const struct nf_conntrack_tuple
*tuple
,
81 struct nf_conntrack_l3proto
*l3proto
)
84 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
86 if (likely(l3proto
->tuple_to_nfattr
))
87 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
89 NFA_NEST_END(skb
, nest_parms
);
98 ctnetlink_dump_tuples(struct sk_buff
*skb
,
99 const struct nf_conntrack_tuple
*tuple
)
102 struct nf_conntrack_l3proto
*l3proto
;
103 struct nf_conntrack_protocol
*proto
;
105 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
106 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
107 nf_ct_l3proto_put(l3proto
);
109 if (unlikely(ret
< 0))
112 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
113 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, proto
);
114 nf_ct_proto_put(proto
);
120 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
122 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
123 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
131 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
133 long timeout_l
= ct
->timeout
.expires
- jiffies
;
139 timeout
= htonl(timeout_l
/ HZ
);
141 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
149 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
151 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
);
152 struct nfattr
*nest_proto
;
155 if (!proto
->to_nfattr
) {
156 nf_ct_proto_put(proto
);
160 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
162 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
164 nf_ct_proto_put(proto
);
166 NFA_NEST_END(skb
, nest_proto
);
175 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
177 struct nfattr
*nest_helper
;
178 const struct nf_conn_help
*help
= nfct_help(ct
);
180 if (!help
|| !help
->helper
)
183 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
184 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(help
->helper
->name
), help
->helper
->name
);
186 if (help
->helper
->to_nfattr
)
187 help
->helper
->to_nfattr(skb
, ct
);
189 NFA_NEST_END(skb
, nest_helper
);
197 #ifdef CONFIG_NF_CT_ACCT
199 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
200 enum ip_conntrack_dir dir
)
202 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
203 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
206 tmp
= htonl(ct
->counters
[dir
].packets
);
207 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
209 tmp
= htonl(ct
->counters
[dir
].bytes
);
210 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
212 NFA_NEST_END(skb
, nest_count
);
220 #define ctnetlink_dump_counters(a, b, c) (0)
223 #ifdef CONFIG_NF_CONNTRACK_MARK
225 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
227 u_int32_t mark
= htonl(ct
->mark
);
229 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
236 #define ctnetlink_dump_mark(a, b) (0)
240 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
242 u_int32_t id
= htonl(ct
->id
);
243 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
251 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
253 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
255 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
262 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
265 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
266 int event
, int nowait
,
267 const struct nf_conn
*ct
)
269 struct nlmsghdr
*nlh
;
270 struct nfgenmsg
*nfmsg
;
271 struct nfattr
*nest_parms
;
276 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
277 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
278 nfmsg
= NLMSG_DATA(nlh
);
280 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
281 nfmsg
->nfgen_family
=
282 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
283 nfmsg
->version
= NFNETLINK_V0
;
286 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
287 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
289 NFA_NEST_END(skb
, nest_parms
);
291 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
292 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
294 NFA_NEST_END(skb
, nest_parms
);
296 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
297 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
298 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
299 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
300 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
301 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
302 ctnetlink_dump_mark(skb
, ct
) < 0 ||
303 ctnetlink_dump_id(skb
, ct
) < 0 ||
304 ctnetlink_dump_use(skb
, ct
) < 0)
307 nlh
->nlmsg_len
= skb
->tail
- b
;
312 skb_trim(skb
, b
- skb
->data
);
316 #ifdef CONFIG_NF_CONNTRACK_EVENTS
317 static int ctnetlink_conntrack_event(struct notifier_block
*this,
318 unsigned long events
, void *ptr
)
320 struct nlmsghdr
*nlh
;
321 struct nfgenmsg
*nfmsg
;
322 struct nfattr
*nest_parms
;
323 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
327 unsigned int flags
= 0, group
;
329 /* ignore our fake conntrack entry */
330 if (ct
== &nf_conntrack_untracked
)
333 if (events
& IPCT_DESTROY
) {
334 type
= IPCTNL_MSG_CT_DELETE
;
335 group
= NFNLGRP_CONNTRACK_DESTROY
;
336 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
337 type
= IPCTNL_MSG_CT_NEW
;
338 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
339 /* dump everything */
341 group
= NFNLGRP_CONNTRACK_NEW
;
342 } else if (events
& (IPCT_STATUS
|
347 type
= IPCTNL_MSG_CT_NEW
;
348 group
= NFNLGRP_CONNTRACK_UPDATE
;
352 if (!nfnetlink_has_listeners(group
))
355 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
361 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
362 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
363 nfmsg
= NLMSG_DATA(nlh
);
365 nlh
->nlmsg_flags
= flags
;
366 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
367 nfmsg
->version
= NFNETLINK_V0
;
370 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
371 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
373 NFA_NEST_END(skb
, nest_parms
);
375 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
376 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
378 NFA_NEST_END(skb
, nest_parms
);
380 /* NAT stuff is now a status flag */
381 if ((events
& IPCT_STATUS
|| events
& IPCT_NATINFO
)
382 && ctnetlink_dump_status(skb
, ct
) < 0)
384 if (events
& IPCT_REFRESH
385 && ctnetlink_dump_timeout(skb
, ct
) < 0)
387 if (events
& IPCT_PROTOINFO
388 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
390 if (events
& IPCT_HELPINFO
391 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
394 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
395 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
398 nlh
->nlmsg_len
= skb
->tail
- b
;
399 nfnetlink_send(skb
, 0, group
, 0);
407 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
409 static int ctnetlink_done(struct netlink_callback
*cb
)
412 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
413 DEBUGP("entered %s\n", __FUNCTION__
);
417 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
420 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
422 struct nf_conn
*ct
, *last
;
423 struct nf_conntrack_tuple_hash
*h
;
425 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
426 u_int8_t l3proto
= nfmsg
->nfgen_family
;
428 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__
,
431 read_lock_bh(&nf_conntrack_lock
);
432 last
= (struct nf_conn
*)cb
->args
[1];
433 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
435 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
436 h
= (struct nf_conntrack_tuple_hash
*) i
;
437 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
439 ct
= nf_ct_tuplehash_to_ctrack(h
);
440 /* Dump entries of a given L3 protocol number.
441 * If it is not specified, ie. l3proto == 0,
442 * then dump everything. */
443 if (l3proto
&& L3PROTO(ct
) != l3proto
)
450 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
454 nf_conntrack_get(&ct
->ct_general
);
455 cb
->args
[1] = (unsigned long)ct
;
465 read_unlock_bh(&nf_conntrack_lock
);
469 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
473 #ifdef CONFIG_NF_CT_ACCT
475 ctnetlink_dump_table_w(struct sk_buff
*skb
, struct netlink_callback
*cb
)
477 struct nf_conn
*ct
= NULL
;
478 struct nf_conntrack_tuple_hash
*h
;
480 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[1];
481 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
482 u_int8_t l3proto
= nfmsg
->nfgen_family
;
484 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__
,
487 write_lock_bh(&nf_conntrack_lock
);
488 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++, *id
= 0) {
489 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
490 h
= (struct nf_conntrack_tuple_hash
*) i
;
491 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
493 ct
= nf_ct_tuplehash_to_ctrack(h
);
494 if (l3proto
&& L3PROTO(ct
) != l3proto
)
498 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
505 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
509 write_unlock_bh(&nf_conntrack_lock
);
511 DEBUGP("leaving, last bucket=%lu id=%u\n", cb
->args
[0], *id
);
518 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
520 struct nfattr
*tb
[CTA_IP_MAX
];
521 struct nf_conntrack_l3proto
*l3proto
;
524 DEBUGP("entered %s\n", __FUNCTION__
);
526 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
528 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
530 if (likely(l3proto
->nfattr_to_tuple
))
531 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
533 nf_ct_l3proto_put(l3proto
);
540 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
541 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
545 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
546 struct nf_conntrack_tuple
*tuple
)
548 struct nfattr
*tb
[CTA_PROTO_MAX
];
549 struct nf_conntrack_protocol
*proto
;
552 DEBUGP("entered %s\n", __FUNCTION__
);
554 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
556 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
559 if (!tb
[CTA_PROTO_NUM
-1])
561 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
563 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
565 if (likely(proto
->nfattr_to_tuple
))
566 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
568 nf_ct_proto_put(proto
);
574 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
575 enum ctattr_tuple type
, u_int8_t l3num
)
577 struct nfattr
*tb
[CTA_TUPLE_MAX
];
580 DEBUGP("entered %s\n", __FUNCTION__
);
582 memset(tuple
, 0, sizeof(*tuple
));
584 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
586 if (!tb
[CTA_TUPLE_IP
-1])
589 tuple
->src
.l3num
= l3num
;
591 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
595 if (!tb
[CTA_TUPLE_PROTO
-1])
598 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
602 /* orig and expect tuples get DIR_ORIGINAL */
603 if (type
== CTA_TUPLE_REPLY
)
604 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
606 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
608 NF_CT_DUMP_TUPLE(tuple
);
615 #ifdef CONFIG_IP_NF_NAT_NEEDED
616 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
617 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
618 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
621 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
622 const struct nf_conn
*ct
,
623 struct ip_nat_range
*range
)
625 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
626 struct ip_nat_protocol
*npt
;
628 DEBUGP("entered %s\n", __FUNCTION__
);
630 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
632 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
635 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
637 if (!npt
->nfattr_to_range
) {
638 ip_nat_proto_put(npt
);
642 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
643 if (npt
->nfattr_to_range(tb
, range
) > 0)
644 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
646 ip_nat_proto_put(npt
);
652 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
653 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
654 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
658 ctnetlink_parse_nat(struct nfattr
*nat
,
659 const struct nf_conn
*ct
, struct ip_nat_range
*range
)
661 struct nfattr
*tb
[CTA_NAT_MAX
];
664 DEBUGP("entered %s\n", __FUNCTION__
);
666 memset(range
, 0, sizeof(*range
));
668 nfattr_parse_nested(tb
, CTA_NAT_MAX
, nat
);
670 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
673 if (tb
[CTA_NAT_MINIP
-1])
674 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
676 if (!tb
[CTA_NAT_MAXIP
-1])
677 range
->max_ip
= range
->min_ip
;
679 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
682 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
684 if (!tb
[CTA_NAT_PROTO
-1])
687 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
697 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
699 struct nfattr
*tb
[CTA_HELP_MAX
];
701 DEBUGP("entered %s\n", __FUNCTION__
);
703 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
705 if (!tb
[CTA_HELP_NAME
-1])
708 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
713 static const size_t cta_min
[CTA_MAX
] = {
714 [CTA_STATUS
-1] = sizeof(u_int32_t
),
715 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
716 [CTA_MARK
-1] = sizeof(u_int32_t
),
717 [CTA_USE
-1] = sizeof(u_int32_t
),
718 [CTA_ID
-1] = sizeof(u_int32_t
)
722 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
723 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
725 struct nf_conntrack_tuple_hash
*h
;
726 struct nf_conntrack_tuple tuple
;
728 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
729 u_int8_t u3
= nfmsg
->nfgen_family
;
732 DEBUGP("entered %s\n", __FUNCTION__
);
734 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
737 if (cda
[CTA_TUPLE_ORIG
-1])
738 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
739 else if (cda
[CTA_TUPLE_REPLY
-1])
740 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
742 /* Flush the whole table */
743 nf_conntrack_flush();
750 h
= nf_conntrack_find_get(&tuple
, NULL
);
752 DEBUGP("tuple not found in conntrack hash\n");
756 ct
= nf_ct_tuplehash_to_ctrack(h
);
759 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
765 if (del_timer(&ct
->timeout
))
766 ct
->timeout
.function((unsigned long)ct
);
775 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
776 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
778 struct nf_conntrack_tuple_hash
*h
;
779 struct nf_conntrack_tuple tuple
;
781 struct sk_buff
*skb2
= NULL
;
782 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
783 u_int8_t u3
= nfmsg
->nfgen_family
;
786 DEBUGP("entered %s\n", __FUNCTION__
);
788 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
791 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) ==
792 IPCTNL_MSG_CT_GET_CTRZERO
) {
793 #ifdef CONFIG_NF_CT_ACCT
794 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
795 ctnetlink_dump_table_w
,
796 ctnetlink_done
)) != 0)
802 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
803 ctnetlink_dump_table
,
804 ctnetlink_done
)) != 0)
808 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
815 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
818 if (cda
[CTA_TUPLE_ORIG
-1])
819 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
820 else if (cda
[CTA_TUPLE_REPLY
-1])
821 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
828 h
= nf_conntrack_find_get(&tuple
, NULL
);
830 DEBUGP("tuple not found in conntrack hash");
833 DEBUGP("tuple found\n");
834 ct
= nf_ct_tuplehash_to_ctrack(h
);
837 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
842 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
844 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
845 IPCTNL_MSG_CT_NEW
, 1, ct
);
850 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
864 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
867 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
868 d
= ct
->status
^ status
;
870 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
874 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
875 /* SEEN_REPLY bit can only be set */
879 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
880 /* ASSURED bit can only be set */
883 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
884 #ifndef CONFIG_IP_NF_NAT_NEEDED
887 struct ip_nat_range range
;
889 if (cda
[CTA_NAT_DST
-1]) {
890 if (ctnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
893 if (ip_nat_initialized(ct
,
894 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
896 ip_nat_setup_info(ct
, &range
, hooknum
);
898 if (cda
[CTA_NAT_SRC
-1]) {
899 if (ctnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
902 if (ip_nat_initialized(ct
,
903 HOOK2MANIP(NF_IP_POST_ROUTING
)))
905 ip_nat_setup_info(ct
, &range
, hooknum
);
910 /* Be careful here, modifying NAT bits can screw up things,
911 * so don't let users modify them directly if they don't pass
913 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
919 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
921 struct nf_conntrack_helper
*helper
;
922 struct nf_conn_help
*help
= nfct_help(ct
);
926 DEBUGP("entered %s\n", __FUNCTION__
);
929 /* FIXME: we need to reallocate and rehash */
933 /* don't change helper of sibling connections */
937 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
941 helper
= __nf_conntrack_helper_find_byname(helpname
);
943 if (!strcmp(helpname
, ""))
951 /* we had a helper before ... */
952 nf_ct_remove_expectations(ct
);
955 /* need to zero data of old helper */
956 memset(&help
->help
, 0, sizeof(help
->help
));
960 help
->helper
= helper
;
966 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
968 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
970 if (!del_timer(&ct
->timeout
))
973 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
974 add_timer(&ct
->timeout
);
980 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
982 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
983 struct nf_conntrack_protocol
*proto
;
984 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
985 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
988 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
990 proto
= nf_ct_proto_find_get(l3num
, npt
);
992 if (proto
->from_nfattr
)
993 err
= proto
->from_nfattr(tb
, ct
);
994 nf_ct_proto_put(proto
);
1000 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
1004 DEBUGP("entered %s\n", __FUNCTION__
);
1006 if (cda
[CTA_HELP
-1]) {
1007 err
= ctnetlink_change_helper(ct
, cda
);
1012 if (cda
[CTA_TIMEOUT
-1]) {
1013 err
= ctnetlink_change_timeout(ct
, cda
);
1018 if (cda
[CTA_STATUS
-1]) {
1019 err
= ctnetlink_change_status(ct
, cda
);
1024 if (cda
[CTA_PROTOINFO
-1]) {
1025 err
= ctnetlink_change_protoinfo(ct
, cda
);
1030 #if defined(CONFIG_NF_CONNTRACK_MARK)
1031 if (cda
[CTA_MARK
-1])
1032 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1035 DEBUGP("all done\n");
1040 ctnetlink_create_conntrack(struct nfattr
*cda
[],
1041 struct nf_conntrack_tuple
*otuple
,
1042 struct nf_conntrack_tuple
*rtuple
)
1047 DEBUGP("entered %s\n", __FUNCTION__
);
1049 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1050 if (ct
== NULL
|| IS_ERR(ct
))
1053 if (!cda
[CTA_TIMEOUT
-1])
1055 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
1057 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1058 ct
->status
|= IPS_CONFIRMED
;
1060 err
= ctnetlink_change_status(ct
, cda
);
1064 if (cda
[CTA_PROTOINFO
-1]) {
1065 err
= ctnetlink_change_protoinfo(ct
, cda
);
1070 #if defined(CONFIG_NF_CONNTRACK_MARK)
1071 if (cda
[CTA_MARK
-1])
1072 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
1075 add_timer(&ct
->timeout
);
1076 nf_conntrack_hash_insert(ct
);
1078 DEBUGP("conntrack with id %u inserted\n", ct
->id
);
1082 nf_conntrack_free(ct
);
1087 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1088 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1090 struct nf_conntrack_tuple otuple
, rtuple
;
1091 struct nf_conntrack_tuple_hash
*h
= NULL
;
1092 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1093 u_int8_t u3
= nfmsg
->nfgen_family
;
1096 DEBUGP("entered %s\n", __FUNCTION__
);
1098 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1101 if (cda
[CTA_TUPLE_ORIG
-1]) {
1102 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1107 if (cda
[CTA_TUPLE_REPLY
-1]) {
1108 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1113 write_lock_bh(&nf_conntrack_lock
);
1114 if (cda
[CTA_TUPLE_ORIG
-1])
1115 h
= __nf_conntrack_find(&otuple
, NULL
);
1116 else if (cda
[CTA_TUPLE_REPLY
-1])
1117 h
= __nf_conntrack_find(&rtuple
, NULL
);
1120 write_unlock_bh(&nf_conntrack_lock
);
1121 DEBUGP("no such conntrack, create new\n");
1123 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1124 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1127 /* implicit 'else' */
1129 /* we only allow nat config for new conntracks */
1130 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1135 /* We manipulate the conntrack inside the global conntrack table lock,
1136 * so there's no need to increase the refcount */
1137 DEBUGP("conntrack found\n");
1139 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1140 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1143 write_unlock_bh(&nf_conntrack_lock
);
1147 /***********************************************************************
1149 ***********************************************************************/
1152 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1153 const struct nf_conntrack_tuple
*tuple
,
1154 enum ctattr_expect type
)
1156 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1158 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1159 goto nfattr_failure
;
1161 NFA_NEST_END(skb
, nest_parms
);
1170 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1171 const struct nf_conntrack_tuple
*tuple
,
1172 const struct nf_conntrack_tuple
*mask
)
1175 struct nf_conntrack_l3proto
*l3proto
;
1176 struct nf_conntrack_protocol
*proto
;
1177 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1179 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1180 ret
= ctnetlink_dump_tuples_ip(skb
, mask
, l3proto
);
1181 nf_ct_l3proto_put(l3proto
);
1183 if (unlikely(ret
< 0))
1184 goto nfattr_failure
;
1186 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1187 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1188 nf_ct_proto_put(proto
);
1189 if (unlikely(ret
< 0))
1190 goto nfattr_failure
;
1192 NFA_NEST_END(skb
, nest_parms
);
1201 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1202 const struct nf_conntrack_expect
*exp
)
1204 struct nf_conn
*master
= exp
->master
;
1205 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1206 u_int32_t id
= htonl(exp
->id
);
1208 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1209 goto nfattr_failure
;
1210 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1211 goto nfattr_failure
;
1212 if (ctnetlink_exp_dump_tuple(skb
,
1213 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1214 CTA_EXPECT_MASTER
) < 0)
1215 goto nfattr_failure
;
1217 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1218 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1227 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1230 const struct nf_conntrack_expect
*exp
)
1232 struct nlmsghdr
*nlh
;
1233 struct nfgenmsg
*nfmsg
;
1238 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1239 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1240 nfmsg
= NLMSG_DATA(nlh
);
1242 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
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
;
1255 skb_trim(skb
, b
- skb
->data
);
1259 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1260 static int ctnetlink_expect_event(struct notifier_block
*this,
1261 unsigned long events
, void *ptr
)
1263 struct nlmsghdr
*nlh
;
1264 struct nfgenmsg
*nfmsg
;
1265 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1266 struct sk_buff
*skb
;
1271 if (events
& IPEXP_NEW
) {
1272 type
= IPCTNL_MSG_EXP_NEW
;
1273 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1277 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1283 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1284 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1285 nfmsg
= NLMSG_DATA(nlh
);
1287 nlh
->nlmsg_flags
= flags
;
1288 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1289 nfmsg
->version
= NFNETLINK_V0
;
1292 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1293 goto nfattr_failure
;
1295 nlh
->nlmsg_len
= skb
->tail
- b
;
1296 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1307 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1309 struct nf_conntrack_expect
*exp
= NULL
;
1310 struct list_head
*i
;
1311 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1312 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1313 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1315 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__
, *id
);
1317 read_lock_bh(&nf_conntrack_lock
);
1318 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1319 exp
= (struct nf_conntrack_expect
*) i
;
1320 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1324 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1332 read_unlock_bh(&nf_conntrack_lock
);
1334 DEBUGP("leaving, last id=%llu\n", *id
);
1339 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1340 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1341 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1345 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1346 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1348 struct nf_conntrack_tuple tuple
;
1349 struct nf_conntrack_expect
*exp
;
1350 struct sk_buff
*skb2
;
1351 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1352 u_int8_t u3
= nfmsg
->nfgen_family
;
1355 DEBUGP("entered %s\n", __FUNCTION__
);
1357 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1360 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1363 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1364 ctnetlink_exp_dump_table
,
1365 ctnetlink_done
)) != 0)
1367 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1368 if (rlen
> skb
->len
)
1370 skb_pull(skb
, rlen
);
1374 if (cda
[CTA_EXPECT_MASTER
-1])
1375 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1382 exp
= nf_conntrack_expect_find(&tuple
);
1386 if (cda
[CTA_EXPECT_ID
-1]) {
1387 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1388 if (exp
->id
!= ntohl(id
)) {
1389 nf_conntrack_expect_put(exp
);
1395 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1398 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1400 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1401 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1406 nf_conntrack_expect_put(exp
);
1408 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1413 nf_conntrack_expect_put(exp
);
1418 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1419 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1421 struct nf_conntrack_expect
*exp
, *tmp
;
1422 struct nf_conntrack_tuple tuple
;
1423 struct nf_conntrack_helper
*h
;
1424 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1425 u_int8_t u3
= nfmsg
->nfgen_family
;
1428 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1431 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1432 /* delete a single expect by tuple */
1433 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1437 /* bump usage count to 2 */
1438 exp
= nf_conntrack_expect_find(&tuple
);
1442 if (cda
[CTA_EXPECT_ID
-1]) {
1444 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1445 if (exp
->id
!= ntohl(id
)) {
1446 nf_conntrack_expect_put(exp
);
1451 /* after list removal, usage count == 1 */
1452 nf_conntrack_unexpect_related(exp
);
1453 /* have to put what we 'get' above.
1454 * after this line usage count == 0 */
1455 nf_conntrack_expect_put(exp
);
1456 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1457 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1459 /* delete all expectations for this helper */
1460 write_lock_bh(&nf_conntrack_lock
);
1461 h
= __nf_conntrack_helper_find_byname(name
);
1463 write_unlock_bh(&nf_conntrack_lock
);
1466 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1468 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1469 if (m_help
->helper
== h
1470 && del_timer(&exp
->timeout
)) {
1471 nf_ct_unlink_expect(exp
);
1472 nf_conntrack_expect_put(exp
);
1475 write_unlock_bh(&nf_conntrack_lock
);
1477 /* This basically means we have to flush everything*/
1478 write_lock_bh(&nf_conntrack_lock
);
1479 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1481 if (del_timer(&exp
->timeout
)) {
1482 nf_ct_unlink_expect(exp
);
1483 nf_conntrack_expect_put(exp
);
1486 write_unlock_bh(&nf_conntrack_lock
);
1492 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1498 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1500 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1501 struct nf_conntrack_tuple_hash
*h
= NULL
;
1502 struct nf_conntrack_expect
*exp
;
1504 struct nf_conn_help
*help
;
1507 DEBUGP("entered %s\n", __FUNCTION__
);
1509 /* caller guarantees that those three CTA_EXPECT_* exist */
1510 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1513 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1516 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1520 /* Look for master conntrack of this expectation */
1521 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1524 ct
= nf_ct_tuplehash_to_ctrack(h
);
1525 help
= nfct_help(ct
);
1527 if (!help
|| !help
->helper
) {
1528 /* such conntrack hasn't got any helper, abort */
1533 exp
= nf_conntrack_expect_alloc(ct
);
1539 exp
->expectfn
= NULL
;
1542 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1543 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1545 err
= nf_conntrack_expect_related(exp
);
1546 nf_conntrack_expect_put(exp
);
1549 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1554 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1555 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1557 struct nf_conntrack_tuple tuple
;
1558 struct nf_conntrack_expect
*exp
;
1559 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1560 u_int8_t u3
= nfmsg
->nfgen_family
;
1563 DEBUGP("entered %s\n", __FUNCTION__
);
1565 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1568 if (!cda
[CTA_EXPECT_TUPLE
-1]
1569 || !cda
[CTA_EXPECT_MASK
-1]
1570 || !cda
[CTA_EXPECT_MASTER
-1])
1573 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1577 write_lock_bh(&nf_conntrack_lock
);
1578 exp
= __nf_conntrack_expect_find(&tuple
);
1581 write_unlock_bh(&nf_conntrack_lock
);
1583 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1584 err
= ctnetlink_create_expect(cda
, u3
);
1589 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1590 err
= ctnetlink_change_expect(exp
, cda
);
1591 write_unlock_bh(&nf_conntrack_lock
);
1593 DEBUGP("leaving\n");
1598 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1599 static struct notifier_block ctnl_notifier
= {
1600 .notifier_call
= ctnetlink_conntrack_event
,
1603 static struct notifier_block ctnl_notifier_exp
= {
1604 .notifier_call
= ctnetlink_expect_event
,
1608 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1609 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1610 .attr_count
= CTA_MAX
, },
1611 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1612 .attr_count
= CTA_MAX
, },
1613 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1614 .attr_count
= CTA_MAX
, },
1615 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1616 .attr_count
= CTA_MAX
, },
1619 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1620 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1621 .attr_count
= CTA_EXPECT_MAX
, },
1622 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1623 .attr_count
= CTA_EXPECT_MAX
, },
1624 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1625 .attr_count
= CTA_EXPECT_MAX
, },
1628 static struct nfnetlink_subsystem ctnl_subsys
= {
1629 .name
= "conntrack",
1630 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1631 .cb_count
= IPCTNL_MSG_MAX
,
1635 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1636 .name
= "conntrack_expect",
1637 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1638 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1642 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1643 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1645 static int __init
ctnetlink_init(void)
1649 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1650 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1652 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1656 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1658 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1659 goto err_unreg_subsys
;
1662 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1663 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1665 printk("ctnetlink_init: cannot register notifier.\n");
1666 goto err_unreg_exp_subsys
;
1669 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1671 printk("ctnetlink_init: cannot expect register notifier.\n");
1672 goto err_unreg_notifier
;
1678 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1680 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1681 err_unreg_exp_subsys
:
1682 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1685 nfnetlink_subsys_unregister(&ctnl_subsys
);
1690 static void __exit
ctnetlink_exit(void)
1692 printk("ctnetlink: unregistering from nfnetlink.\n");
1694 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1695 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1696 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1699 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1700 nfnetlink_subsys_unregister(&ctnl_subsys
);
1704 module_init(ctnetlink_init
);
1705 module_exit(ctnetlink_exit
);