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 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/timer.h>
23 #include <linux/skbuff.h>
24 #include <linux/errno.h>
25 #include <linux/netlink.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/notifier.h>
30 #include <linux/netfilter.h>
31 #include <net/netlink.h>
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_expect.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_l3proto.h>
37 #include <net/netfilter/nf_conntrack_l4proto.h>
38 #include <net/netfilter/nf_conntrack_tuple.h>
39 #ifdef CONFIG_NF_NAT_NEEDED
40 #include <net/netfilter/nf_nat_core.h>
41 #include <net/netfilter/nf_nat_protocol.h>
44 #include <linux/netfilter/nfnetlink.h>
45 #include <linux/netfilter/nfnetlink_conntrack.h>
47 MODULE_LICENSE("GPL");
49 static char __initdata version
[] = "0.93";
52 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
53 const struct nf_conntrack_tuple
*tuple
,
54 struct nf_conntrack_l4proto
*l4proto
)
57 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
59 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
61 if (likely(l4proto
->tuple_to_nfattr
))
62 ret
= l4proto
->tuple_to_nfattr(skb
, tuple
);
64 NFA_NEST_END(skb
, nest_parms
);
73 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
74 const struct nf_conntrack_tuple
*tuple
,
75 struct nf_conntrack_l3proto
*l3proto
)
78 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
80 if (likely(l3proto
->tuple_to_nfattr
))
81 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
83 NFA_NEST_END(skb
, nest_parms
);
92 ctnetlink_dump_tuples(struct sk_buff
*skb
,
93 const struct nf_conntrack_tuple
*tuple
)
96 struct nf_conntrack_l3proto
*l3proto
;
97 struct nf_conntrack_l4proto
*l4proto
;
99 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
100 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
101 nf_ct_l3proto_put(l3proto
);
103 if (unlikely(ret
< 0))
106 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
107 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
108 nf_ct_l4proto_put(l4proto
);
114 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
116 __be32 status
= htonl((u_int32_t
) ct
->status
);
117 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
125 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
127 long timeout_l
= ct
->timeout
.expires
- jiffies
;
133 timeout
= htonl(timeout_l
/ HZ
);
135 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
143 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
145 struct nf_conntrack_l4proto
*l4proto
= nf_ct_l4proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
, ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
146 struct nfattr
*nest_proto
;
149 if (!l4proto
->to_nfattr
) {
150 nf_ct_l4proto_put(l4proto
);
154 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
156 ret
= l4proto
->to_nfattr(skb
, nest_proto
, ct
);
158 nf_ct_l4proto_put(l4proto
);
160 NFA_NEST_END(skb
, nest_proto
);
165 nf_ct_l4proto_put(l4proto
);
170 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
172 struct nfattr
*nest_helper
;
173 const struct nf_conn_help
*help
= nfct_help(ct
);
174 struct nf_conntrack_helper
*helper
;
180 helper
= rcu_dereference(help
->helper
);
184 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
185 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(helper
->name
), helper
->name
);
187 if (helper
->to_nfattr
)
188 helper
->to_nfattr(skb
, ct
);
190 NFA_NEST_END(skb
, nest_helper
);
200 #ifdef CONFIG_NF_CT_ACCT
202 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
203 enum ip_conntrack_dir dir
)
205 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
206 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
209 tmp
= htonl(ct
->counters
[dir
].packets
);
210 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
212 tmp
= htonl(ct
->counters
[dir
].bytes
);
213 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
215 NFA_NEST_END(skb
, nest_count
);
223 #define ctnetlink_dump_counters(a, b, c) (0)
226 #ifdef CONFIG_NF_CONNTRACK_MARK
228 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
230 __be32 mark
= htonl(ct
->mark
);
232 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
239 #define ctnetlink_dump_mark(a, b) (0)
243 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
245 __be32 id
= htonl(ct
->id
);
246 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
254 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
256 __be32 use
= htonl(atomic_read(&ct
->ct_general
.use
));
258 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
265 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
268 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
269 int event
, int nowait
,
270 const struct nf_conn
*ct
)
272 struct nlmsghdr
*nlh
;
273 struct nfgenmsg
*nfmsg
;
274 struct nfattr
*nest_parms
;
275 unsigned char *b
= skb_tail_pointer(skb
);
277 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
278 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
279 nfmsg
= NLMSG_DATA(nlh
);
281 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
282 nfmsg
->nfgen_family
=
283 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
284 nfmsg
->version
= NFNETLINK_V0
;
287 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
288 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
290 NFA_NEST_END(skb
, nest_parms
);
292 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
293 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
295 NFA_NEST_END(skb
, nest_parms
);
297 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
298 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
299 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
300 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
301 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
302 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
303 ctnetlink_dump_mark(skb
, ct
) < 0 ||
304 ctnetlink_dump_id(skb
, ct
) < 0 ||
305 ctnetlink_dump_use(skb
, ct
) < 0)
308 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
317 #ifdef CONFIG_NF_CONNTRACK_EVENTS
318 static int ctnetlink_conntrack_event(struct notifier_block
*this,
319 unsigned long events
, void *ptr
)
321 struct nlmsghdr
*nlh
;
322 struct nfgenmsg
*nfmsg
;
323 struct nfattr
*nest_parms
;
324 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
328 unsigned int flags
= 0, group
;
330 /* ignore our fake conntrack entry */
331 if (ct
== &nf_conntrack_untracked
)
334 if (events
& IPCT_DESTROY
) {
335 type
= IPCTNL_MSG_CT_DELETE
;
336 group
= NFNLGRP_CONNTRACK_DESTROY
;
337 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
338 type
= IPCTNL_MSG_CT_NEW
;
339 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
340 group
= NFNLGRP_CONNTRACK_NEW
;
341 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
342 type
= IPCTNL_MSG_CT_NEW
;
343 group
= NFNLGRP_CONNTRACK_UPDATE
;
347 if (!nfnetlink_has_listeners(group
))
350 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
356 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
357 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
358 nfmsg
= NLMSG_DATA(nlh
);
360 nlh
->nlmsg_flags
= flags
;
361 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
362 nfmsg
->version
= NFNETLINK_V0
;
365 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
366 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
368 NFA_NEST_END(skb
, nest_parms
);
370 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
371 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
373 NFA_NEST_END(skb
, nest_parms
);
375 if (events
& IPCT_DESTROY
) {
376 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
377 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
380 if (ctnetlink_dump_status(skb
, ct
) < 0)
383 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
386 if (events
& IPCT_PROTOINFO
387 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
390 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
391 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
394 #ifdef CONFIG_NF_CONNTRACK_MARK
395 if ((events
& IPCT_MARK
|| ct
->mark
)
396 && ctnetlink_dump_mark(skb
, ct
) < 0)
400 if (events
& IPCT_COUNTER_FILLING
&&
401 (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
402 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0))
406 nlh
->nlmsg_len
= skb
->tail
- b
;
407 nfnetlink_send(skb
, 0, group
, 0);
415 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
417 static int ctnetlink_done(struct netlink_callback
*cb
)
420 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
424 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
427 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
429 struct nf_conn
*ct
, *last
;
430 struct nf_conntrack_tuple_hash
*h
;
431 struct hlist_node
*n
;
432 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
433 u_int8_t l3proto
= nfmsg
->nfgen_family
;
435 read_lock_bh(&nf_conntrack_lock
);
436 last
= (struct nf_conn
*)cb
->args
[1];
437 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
439 hlist_for_each_entry(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
441 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
443 ct
= nf_ct_tuplehash_to_ctrack(h
);
444 /* Dump entries of a given L3 protocol number.
445 * If it is not specified, ie. l3proto == 0,
446 * then dump everything. */
447 if (l3proto
&& L3PROTO(ct
) != l3proto
)
454 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
458 nf_conntrack_get(&ct
->ct_general
);
459 cb
->args
[1] = (unsigned long)ct
;
462 #ifdef CONFIG_NF_CT_ACCT
463 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
464 IPCTNL_MSG_CT_GET_CTRZERO
)
465 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
474 read_unlock_bh(&nf_conntrack_lock
);
482 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
484 struct nfattr
*tb
[CTA_IP_MAX
];
485 struct nf_conntrack_l3proto
*l3proto
;
488 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
490 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
492 if (likely(l3proto
->nfattr_to_tuple
))
493 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
495 nf_ct_l3proto_put(l3proto
);
500 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
501 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
505 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
506 struct nf_conntrack_tuple
*tuple
)
508 struct nfattr
*tb
[CTA_PROTO_MAX
];
509 struct nf_conntrack_l4proto
*l4proto
;
512 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
514 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
517 if (!tb
[CTA_PROTO_NUM
-1])
519 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
521 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
523 if (likely(l4proto
->nfattr_to_tuple
))
524 ret
= l4proto
->nfattr_to_tuple(tb
, tuple
);
526 nf_ct_l4proto_put(l4proto
);
532 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
533 enum ctattr_tuple type
, u_int8_t l3num
)
535 struct nfattr
*tb
[CTA_TUPLE_MAX
];
538 memset(tuple
, 0, sizeof(*tuple
));
540 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
542 if (!tb
[CTA_TUPLE_IP
-1])
545 tuple
->src
.l3num
= l3num
;
547 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
551 if (!tb
[CTA_TUPLE_PROTO
-1])
554 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
558 /* orig and expect tuples get DIR_ORIGINAL */
559 if (type
== CTA_TUPLE_REPLY
)
560 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
562 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
567 #ifdef CONFIG_NF_NAT_NEEDED
568 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
569 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
570 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
573 static int nfnetlink_parse_nat_proto(struct nfattr
*attr
,
574 const struct nf_conn
*ct
,
575 struct nf_nat_range
*range
)
577 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
578 struct nf_nat_protocol
*npt
;
580 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
582 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
585 npt
= nf_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
587 if (!npt
->nfattr_to_range
) {
588 nf_nat_proto_put(npt
);
592 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
593 if (npt
->nfattr_to_range(tb
, range
) > 0)
594 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
596 nf_nat_proto_put(npt
);
601 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
602 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
603 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
607 nfnetlink_parse_nat(struct nfattr
*nat
,
608 const struct nf_conn
*ct
, struct nf_nat_range
*range
)
610 struct nfattr
*tb
[CTA_NAT_MAX
];
613 memset(range
, 0, sizeof(*range
));
615 nfattr_parse_nested(tb
, CTA_NAT_MAX
, nat
);
617 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
620 if (tb
[CTA_NAT_MINIP
-1])
621 range
->min_ip
= *(__be32
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
623 if (!tb
[CTA_NAT_MAXIP
-1])
624 range
->max_ip
= range
->min_ip
;
626 range
->max_ip
= *(__be32
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
629 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
631 if (!tb
[CTA_NAT_PROTO
-1])
634 err
= nfnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
643 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
645 struct nfattr
*tb
[CTA_HELP_MAX
];
647 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
649 if (!tb
[CTA_HELP_NAME
-1])
652 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
657 static const size_t cta_min
[CTA_MAX
] = {
658 [CTA_STATUS
-1] = sizeof(u_int32_t
),
659 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
660 [CTA_MARK
-1] = sizeof(u_int32_t
),
661 [CTA_USE
-1] = sizeof(u_int32_t
),
662 [CTA_ID
-1] = sizeof(u_int32_t
)
666 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
667 struct nlmsghdr
*nlh
, struct nfattr
*cda
[])
669 struct nf_conntrack_tuple_hash
*h
;
670 struct nf_conntrack_tuple tuple
;
672 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
673 u_int8_t u3
= nfmsg
->nfgen_family
;
676 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
679 if (cda
[CTA_TUPLE_ORIG
-1])
680 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
681 else if (cda
[CTA_TUPLE_REPLY
-1])
682 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
684 /* Flush the whole table */
685 nf_conntrack_flush();
692 h
= nf_conntrack_find_get(&tuple
);
696 ct
= nf_ct_tuplehash_to_ctrack(h
);
699 u_int32_t id
= ntohl(*(__be32
*)NFA_DATA(cda
[CTA_ID
-1]));
705 if (del_timer(&ct
->timeout
))
706 ct
->timeout
.function((unsigned long)ct
);
714 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
715 struct nlmsghdr
*nlh
, struct nfattr
*cda
[])
717 struct nf_conntrack_tuple_hash
*h
;
718 struct nf_conntrack_tuple tuple
;
720 struct sk_buff
*skb2
= NULL
;
721 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
722 u_int8_t u3
= nfmsg
->nfgen_family
;
725 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
726 #ifndef CONFIG_NF_CT_ACCT
727 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == IPCTNL_MSG_CT_GET_CTRZERO
)
730 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
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
);
747 h
= nf_conntrack_find_get(&tuple
);
751 ct
= nf_ct_tuplehash_to_ctrack(h
);
754 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
760 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
761 IPCTNL_MSG_CT_NEW
, 1, ct
);
766 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
779 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
782 unsigned int status
= ntohl(*(__be32
*)NFA_DATA(cda
[CTA_STATUS
-1]));
783 d
= ct
->status
^ status
;
785 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
789 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
790 /* SEEN_REPLY bit can only be set */
794 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
795 /* ASSURED bit can only be set */
798 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
799 #ifndef CONFIG_NF_NAT_NEEDED
802 struct nf_nat_range range
;
804 if (cda
[CTA_NAT_DST
-1]) {
805 if (nfnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
808 if (nf_nat_initialized(ct
,
809 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
811 nf_nat_setup_info(ct
, &range
, NF_IP_PRE_ROUTING
);
813 if (cda
[CTA_NAT_SRC
-1]) {
814 if (nfnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
817 if (nf_nat_initialized(ct
,
818 HOOK2MANIP(NF_IP_POST_ROUTING
)))
820 nf_nat_setup_info(ct
, &range
, NF_IP_POST_ROUTING
);
825 /* Be careful here, modifying NAT bits can screw up things,
826 * so don't let users modify them directly if they don't pass
828 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
834 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
836 struct nf_conntrack_helper
*helper
;
837 struct nf_conn_help
*help
= nfct_help(ct
);
841 /* don't change helper of sibling connections */
845 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
849 if (!strcmp(helpname
, "")) {
850 if (help
&& help
->helper
) {
851 /* we had a helper before ... */
852 nf_ct_remove_expectations(ct
);
853 rcu_assign_pointer(help
->helper
, NULL
);
859 helper
= __nf_conntrack_helper_find_byname(helpname
);
864 if (help
->helper
== helper
)
868 /* need to zero data of old helper */
869 memset(&help
->help
, 0, sizeof(help
->help
));
871 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
876 rcu_assign_pointer(help
->helper
, helper
);
882 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
884 u_int32_t timeout
= ntohl(*(__be32
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
886 if (!del_timer(&ct
->timeout
))
889 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
890 add_timer(&ct
->timeout
);
896 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
898 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
899 struct nf_conntrack_l4proto
*l4proto
;
900 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
901 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
904 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
906 l4proto
= nf_ct_l4proto_find_get(l3num
, npt
);
908 if (l4proto
->from_nfattr
)
909 err
= l4proto
->from_nfattr(tb
, ct
);
910 nf_ct_l4proto_put(l4proto
);
916 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
920 if (cda
[CTA_HELP
-1]) {
921 err
= ctnetlink_change_helper(ct
, cda
);
926 if (cda
[CTA_TIMEOUT
-1]) {
927 err
= ctnetlink_change_timeout(ct
, cda
);
932 if (cda
[CTA_STATUS
-1]) {
933 err
= ctnetlink_change_status(ct
, cda
);
938 if (cda
[CTA_PROTOINFO
-1]) {
939 err
= ctnetlink_change_protoinfo(ct
, cda
);
944 #if defined(CONFIG_NF_CONNTRACK_MARK)
946 ct
->mark
= ntohl(*(__be32
*)NFA_DATA(cda
[CTA_MARK
-1]));
953 ctnetlink_create_conntrack(struct nfattr
*cda
[],
954 struct nf_conntrack_tuple
*otuple
,
955 struct nf_conntrack_tuple
*rtuple
)
959 struct nf_conn_help
*help
;
960 struct nf_conntrack_helper
*helper
;
962 ct
= nf_conntrack_alloc(otuple
, rtuple
);
963 if (ct
== NULL
|| IS_ERR(ct
))
966 if (!cda
[CTA_TIMEOUT
-1])
968 ct
->timeout
.expires
= ntohl(*(__be32
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
970 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
971 ct
->status
|= IPS_CONFIRMED
;
973 if (cda
[CTA_STATUS
-1]) {
974 err
= ctnetlink_change_status(ct
, cda
);
979 if (cda
[CTA_PROTOINFO
-1]) {
980 err
= ctnetlink_change_protoinfo(ct
, cda
);
985 #if defined(CONFIG_NF_CONNTRACK_MARK)
987 ct
->mark
= ntohl(*(__be32
*)NFA_DATA(cda
[CTA_MARK
-1]));
990 helper
= nf_ct_helper_find_get(rtuple
);
992 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
994 nf_ct_helper_put(helper
);
998 /* not in hash table yet so not strictly necessary */
999 rcu_assign_pointer(help
->helper
, helper
);
1002 add_timer(&ct
->timeout
);
1003 nf_conntrack_hash_insert(ct
);
1006 nf_ct_helper_put(helper
);
1011 nf_conntrack_free(ct
);
1016 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1017 struct nlmsghdr
*nlh
, struct nfattr
*cda
[])
1019 struct nf_conntrack_tuple otuple
, rtuple
;
1020 struct nf_conntrack_tuple_hash
*h
= NULL
;
1021 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1022 u_int8_t u3
= nfmsg
->nfgen_family
;
1025 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1028 if (cda
[CTA_TUPLE_ORIG
-1]) {
1029 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1034 if (cda
[CTA_TUPLE_REPLY
-1]) {
1035 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1040 write_lock_bh(&nf_conntrack_lock
);
1041 if (cda
[CTA_TUPLE_ORIG
-1])
1042 h
= __nf_conntrack_find(&otuple
, NULL
);
1043 else if (cda
[CTA_TUPLE_REPLY
-1])
1044 h
= __nf_conntrack_find(&rtuple
, NULL
);
1047 write_unlock_bh(&nf_conntrack_lock
);
1049 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1050 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1053 /* implicit 'else' */
1055 /* We manipulate the conntrack inside the global conntrack table lock,
1056 * so there's no need to increase the refcount */
1058 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1059 /* we only allow nat config for new conntracks */
1060 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1064 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
),
1069 write_unlock_bh(&nf_conntrack_lock
);
1073 /***********************************************************************
1075 ***********************************************************************/
1078 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1079 const struct nf_conntrack_tuple
*tuple
,
1080 enum ctattr_expect type
)
1082 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1084 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1085 goto nfattr_failure
;
1087 NFA_NEST_END(skb
, nest_parms
);
1096 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1097 const struct nf_conntrack_tuple
*tuple
,
1098 const struct nf_conntrack_tuple_mask
*mask
)
1101 struct nf_conntrack_l3proto
*l3proto
;
1102 struct nf_conntrack_l4proto
*l4proto
;
1103 struct nf_conntrack_tuple m
;
1104 struct nfattr
*nest_parms
;
1106 memset(&m
, 0xFF, sizeof(m
));
1107 m
.src
.u
.all
= mask
->src
.u
.all
;
1108 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1110 nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1112 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1113 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1114 nf_ct_l3proto_put(l3proto
);
1116 if (unlikely(ret
< 0))
1117 goto nfattr_failure
;
1119 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1120 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1121 nf_ct_l4proto_put(l4proto
);
1122 if (unlikely(ret
< 0))
1123 goto nfattr_failure
;
1125 NFA_NEST_END(skb
, nest_parms
);
1134 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1135 const struct nf_conntrack_expect
*exp
)
1137 struct nf_conn
*master
= exp
->master
;
1138 __be32 timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1139 __be32 id
= htonl(exp
->id
);
1141 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1142 goto nfattr_failure
;
1143 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1144 goto nfattr_failure
;
1145 if (ctnetlink_exp_dump_tuple(skb
,
1146 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1147 CTA_EXPECT_MASTER
) < 0)
1148 goto nfattr_failure
;
1150 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1151 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1160 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1163 const struct nf_conntrack_expect
*exp
)
1165 struct nlmsghdr
*nlh
;
1166 struct nfgenmsg
*nfmsg
;
1167 unsigned char *b
= skb_tail_pointer(skb
);
1169 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1170 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1171 nfmsg
= NLMSG_DATA(nlh
);
1173 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1174 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1175 nfmsg
->version
= NFNETLINK_V0
;
1178 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1179 goto nfattr_failure
;
1181 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1190 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1191 static int ctnetlink_expect_event(struct notifier_block
*this,
1192 unsigned long events
, void *ptr
)
1194 struct nlmsghdr
*nlh
;
1195 struct nfgenmsg
*nfmsg
;
1196 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1197 struct sk_buff
*skb
;
1202 if (events
& IPEXP_NEW
) {
1203 type
= IPCTNL_MSG_EXP_NEW
;
1204 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1208 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1211 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1217 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1218 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1219 nfmsg
= NLMSG_DATA(nlh
);
1221 nlh
->nlmsg_flags
= flags
;
1222 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1223 nfmsg
->version
= NFNETLINK_V0
;
1226 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1227 goto nfattr_failure
;
1229 nlh
->nlmsg_len
= skb
->tail
- b
;
1230 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1239 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1242 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1247 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1249 struct nf_conntrack_expect
*exp
, *last
;
1250 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1251 struct hlist_node
*n
;
1252 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1254 read_lock_bh(&nf_conntrack_lock
);
1255 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1256 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1258 hlist_for_each_entry(exp
, n
, &nf_ct_expect_hash
[cb
->args
[0]],
1260 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1267 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1271 atomic_inc(&exp
->use
);
1272 cb
->args
[1] = (unsigned long)exp
;
1282 read_unlock_bh(&nf_conntrack_lock
);
1284 nf_ct_expect_put(last
);
1289 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1290 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1291 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1295 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1296 struct nlmsghdr
*nlh
, struct nfattr
*cda
[])
1298 struct nf_conntrack_tuple tuple
;
1299 struct nf_conntrack_expect
*exp
;
1300 struct sk_buff
*skb2
;
1301 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1302 u_int8_t u3
= nfmsg
->nfgen_family
;
1305 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1308 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1309 return netlink_dump_start(ctnl
, skb
, nlh
,
1310 ctnetlink_exp_dump_table
,
1311 ctnetlink_exp_done
);
1314 if (cda
[CTA_EXPECT_MASTER
-1])
1315 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1322 exp
= nf_ct_expect_find_get(&tuple
);
1326 if (cda
[CTA_EXPECT_ID
-1]) {
1327 __be32 id
= *(__be32
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1328 if (exp
->id
!= ntohl(id
)) {
1329 nf_ct_expect_put(exp
);
1335 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1339 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1340 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1345 nf_ct_expect_put(exp
);
1347 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1352 nf_ct_expect_put(exp
);
1357 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1358 struct nlmsghdr
*nlh
, struct nfattr
*cda
[])
1360 struct nf_conntrack_expect
*exp
;
1361 struct nf_conntrack_tuple tuple
;
1362 struct nf_conntrack_helper
*h
;
1363 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1364 struct hlist_node
*n
, *next
;
1365 u_int8_t u3
= nfmsg
->nfgen_family
;
1369 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1372 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1373 /* delete a single expect by tuple */
1374 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1378 /* bump usage count to 2 */
1379 exp
= nf_ct_expect_find_get(&tuple
);
1383 if (cda
[CTA_EXPECT_ID
-1]) {
1384 __be32 id
= *(__be32
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1385 if (exp
->id
!= ntohl(id
)) {
1386 nf_ct_expect_put(exp
);
1391 /* after list removal, usage count == 1 */
1392 nf_ct_unexpect_related(exp
);
1393 /* have to put what we 'get' above.
1394 * after this line usage count == 0 */
1395 nf_ct_expect_put(exp
);
1396 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1397 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1398 struct nf_conn_help
*m_help
;
1400 /* delete all expectations for this helper */
1401 write_lock_bh(&nf_conntrack_lock
);
1402 h
= __nf_conntrack_helper_find_byname(name
);
1404 write_unlock_bh(&nf_conntrack_lock
);
1407 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1408 hlist_for_each_entry_safe(exp
, n
, next
,
1409 &nf_ct_expect_hash
[i
],
1411 m_help
= nfct_help(exp
->master
);
1412 if (m_help
->helper
== h
1413 && del_timer(&exp
->timeout
)) {
1414 nf_ct_unlink_expect(exp
);
1415 nf_ct_expect_put(exp
);
1419 write_unlock_bh(&nf_conntrack_lock
);
1421 /* This basically means we have to flush everything*/
1422 write_lock_bh(&nf_conntrack_lock
);
1423 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1424 hlist_for_each_entry_safe(exp
, n
, next
,
1425 &nf_ct_expect_hash
[i
],
1427 if (del_timer(&exp
->timeout
)) {
1428 nf_ct_unlink_expect(exp
);
1429 nf_ct_expect_put(exp
);
1433 write_unlock_bh(&nf_conntrack_lock
);
1439 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1445 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1447 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1448 struct nf_conntrack_tuple_hash
*h
= NULL
;
1449 struct nf_conntrack_expect
*exp
;
1451 struct nf_conn_help
*help
;
1454 /* caller guarantees that those three CTA_EXPECT_* exist */
1455 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1458 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1461 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1465 /* Look for master conntrack of this expectation */
1466 h
= nf_conntrack_find_get(&master_tuple
);
1469 ct
= nf_ct_tuplehash_to_ctrack(h
);
1470 help
= nfct_help(ct
);
1472 if (!help
|| !help
->helper
) {
1473 /* such conntrack hasn't got any helper, abort */
1478 exp
= nf_ct_expect_alloc(ct
);
1484 exp
->expectfn
= NULL
;
1488 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1489 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1490 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1492 err
= nf_ct_expect_related(exp
);
1493 nf_ct_expect_put(exp
);
1496 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1501 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1502 struct nlmsghdr
*nlh
, struct nfattr
*cda
[])
1504 struct nf_conntrack_tuple tuple
;
1505 struct nf_conntrack_expect
*exp
;
1506 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1507 u_int8_t u3
= nfmsg
->nfgen_family
;
1510 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1513 if (!cda
[CTA_EXPECT_TUPLE
-1]
1514 || !cda
[CTA_EXPECT_MASK
-1]
1515 || !cda
[CTA_EXPECT_MASTER
-1])
1518 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1522 write_lock_bh(&nf_conntrack_lock
);
1523 exp
= __nf_ct_expect_find(&tuple
);
1526 write_unlock_bh(&nf_conntrack_lock
);
1528 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1529 err
= ctnetlink_create_expect(cda
, u3
);
1534 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1535 err
= ctnetlink_change_expect(exp
, cda
);
1536 write_unlock_bh(&nf_conntrack_lock
);
1541 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1542 static struct notifier_block ctnl_notifier
= {
1543 .notifier_call
= ctnetlink_conntrack_event
,
1546 static struct notifier_block ctnl_notifier_exp
= {
1547 .notifier_call
= ctnetlink_expect_event
,
1551 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1552 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1553 .attr_count
= CTA_MAX
, },
1554 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1555 .attr_count
= CTA_MAX
, },
1556 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1557 .attr_count
= CTA_MAX
, },
1558 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1559 .attr_count
= CTA_MAX
, },
1562 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1563 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1564 .attr_count
= CTA_EXPECT_MAX
, },
1565 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1566 .attr_count
= CTA_EXPECT_MAX
, },
1567 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1568 .attr_count
= CTA_EXPECT_MAX
, },
1571 static struct nfnetlink_subsystem ctnl_subsys
= {
1572 .name
= "conntrack",
1573 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1574 .cb_count
= IPCTNL_MSG_MAX
,
1578 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1579 .name
= "conntrack_expect",
1580 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1581 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1585 MODULE_ALIAS("ip_conntrack_netlink");
1586 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1587 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1589 static int __init
ctnetlink_init(void)
1593 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1594 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1596 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1600 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1602 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1603 goto err_unreg_subsys
;
1606 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1607 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1609 printk("ctnetlink_init: cannot register notifier.\n");
1610 goto err_unreg_exp_subsys
;
1613 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1615 printk("ctnetlink_init: cannot expect register notifier.\n");
1616 goto err_unreg_notifier
;
1622 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1624 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1625 err_unreg_exp_subsys
:
1626 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1629 nfnetlink_subsys_unregister(&ctnl_subsys
);
1634 static void __exit
ctnetlink_exit(void)
1636 printk("ctnetlink: unregistering from nfnetlink.\n");
1638 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1639 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1640 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1643 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1644 nfnetlink_subsys_unregister(&ctnl_subsys
);
1648 module_init(ctnetlink_init
);
1649 module_exit(ctnetlink_exit
);