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";
51 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
52 const struct nf_conntrack_tuple
*tuple
,
53 struct nf_conntrack_protocol
*proto
)
56 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_PROTO
);
58 NFA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
60 if (likely(proto
->tuple_to_nfattr
))
61 ret
= proto
->tuple_to_nfattr(skb
, tuple
);
63 NFA_NEST_END(skb
, nest_parms
);
72 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
73 const struct nf_conntrack_tuple
*tuple
,
74 struct nf_conntrack_l3proto
*l3proto
)
77 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_TUPLE_IP
);
79 if (likely(l3proto
->tuple_to_nfattr
))
80 ret
= l3proto
->tuple_to_nfattr(skb
, tuple
);
82 NFA_NEST_END(skb
, nest_parms
);
91 ctnetlink_dump_tuples(struct sk_buff
*skb
,
92 const struct nf_conntrack_tuple
*tuple
)
95 struct nf_conntrack_l3proto
*l3proto
;
96 struct nf_conntrack_protocol
*proto
;
98 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
99 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
100 nf_ct_l3proto_put(l3proto
);
102 if (unlikely(ret
< 0))
105 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
106 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, proto
);
107 nf_ct_proto_put(proto
);
113 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
115 u_int32_t status
= htonl((u_int32_t
) ct
->status
);
116 NFA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
124 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
126 long timeout_l
= ct
->timeout
.expires
- jiffies
;
132 timeout
= htonl(timeout_l
/ HZ
);
134 NFA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
142 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
144 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
);
145 struct nfattr
*nest_proto
;
148 if (!proto
->to_nfattr
) {
149 nf_ct_proto_put(proto
);
153 nest_proto
= NFA_NEST(skb
, CTA_PROTOINFO
);
155 ret
= proto
->to_nfattr(skb
, nest_proto
, ct
);
157 nf_ct_proto_put(proto
);
159 NFA_NEST_END(skb
, nest_proto
);
164 nf_ct_proto_put(proto
);
169 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
171 struct nfattr
*nest_helper
;
172 const struct nf_conn_help
*help
= nfct_help(ct
);
174 if (!help
|| !help
->helper
)
177 nest_helper
= NFA_NEST(skb
, CTA_HELP
);
178 NFA_PUT(skb
, CTA_HELP_NAME
, strlen(help
->helper
->name
), help
->helper
->name
);
180 if (help
->helper
->to_nfattr
)
181 help
->helper
->to_nfattr(skb
, ct
);
183 NFA_NEST_END(skb
, nest_helper
);
191 #ifdef CONFIG_NF_CT_ACCT
193 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
194 enum ip_conntrack_dir dir
)
196 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
197 struct nfattr
*nest_count
= NFA_NEST(skb
, type
);
200 tmp
= htonl(ct
->counters
[dir
].packets
);
201 NFA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
203 tmp
= htonl(ct
->counters
[dir
].bytes
);
204 NFA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
206 NFA_NEST_END(skb
, nest_count
);
214 #define ctnetlink_dump_counters(a, b, c) (0)
217 #ifdef CONFIG_NF_CONNTRACK_MARK
219 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
221 u_int32_t mark
= htonl(ct
->mark
);
223 NFA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
230 #define ctnetlink_dump_mark(a, b) (0)
234 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
236 u_int32_t id
= htonl(ct
->id
);
237 NFA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
245 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
247 u_int32_t use
= htonl(atomic_read(&ct
->ct_general
.use
));
249 NFA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
256 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
259 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
260 int event
, int nowait
,
261 const struct nf_conn
*ct
)
263 struct nlmsghdr
*nlh
;
264 struct nfgenmsg
*nfmsg
;
265 struct nfattr
*nest_parms
;
270 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
271 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
272 nfmsg
= NLMSG_DATA(nlh
);
274 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
275 nfmsg
->nfgen_family
=
276 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
277 nfmsg
->version
= NFNETLINK_V0
;
280 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
281 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
283 NFA_NEST_END(skb
, nest_parms
);
285 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
286 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
288 NFA_NEST_END(skb
, nest_parms
);
290 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
291 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
292 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
293 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
294 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
295 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
296 ctnetlink_dump_mark(skb
, ct
) < 0 ||
297 ctnetlink_dump_id(skb
, ct
) < 0 ||
298 ctnetlink_dump_use(skb
, ct
) < 0)
301 nlh
->nlmsg_len
= skb
->tail
- b
;
306 skb_trim(skb
, b
- skb
->data
);
310 #ifdef CONFIG_NF_CONNTRACK_EVENTS
311 static int ctnetlink_conntrack_event(struct notifier_block
*this,
312 unsigned long events
, void *ptr
)
314 struct nlmsghdr
*nlh
;
315 struct nfgenmsg
*nfmsg
;
316 struct nfattr
*nest_parms
;
317 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
321 unsigned int flags
= 0, group
;
323 /* ignore our fake conntrack entry */
324 if (ct
== &nf_conntrack_untracked
)
327 if (events
& IPCT_DESTROY
) {
328 type
= IPCTNL_MSG_CT_DELETE
;
329 group
= NFNLGRP_CONNTRACK_DESTROY
;
330 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
331 type
= IPCTNL_MSG_CT_NEW
;
332 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
333 /* dump everything */
335 group
= NFNLGRP_CONNTRACK_NEW
;
336 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
337 type
= IPCTNL_MSG_CT_NEW
;
338 group
= NFNLGRP_CONNTRACK_UPDATE
;
342 if (!nfnetlink_has_listeners(group
))
345 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
351 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
352 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
353 nfmsg
= NLMSG_DATA(nlh
);
355 nlh
->nlmsg_flags
= flags
;
356 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
357 nfmsg
->version
= NFNETLINK_V0
;
360 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_ORIG
);
361 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
363 NFA_NEST_END(skb
, nest_parms
);
365 nest_parms
= NFA_NEST(skb
, CTA_TUPLE_REPLY
);
366 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
368 NFA_NEST_END(skb
, nest_parms
);
370 /* NAT stuff is now a status flag */
371 if ((events
& IPCT_STATUS
|| events
& IPCT_NATINFO
)
372 && ctnetlink_dump_status(skb
, ct
) < 0)
374 if (events
& IPCT_REFRESH
375 && ctnetlink_dump_timeout(skb
, ct
) < 0)
377 if (events
& IPCT_PROTOINFO
378 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
380 if (events
& IPCT_HELPINFO
381 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
384 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
385 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
388 if (events
& IPCT_MARK
389 && ctnetlink_dump_mark(skb
, ct
) < 0)
392 nlh
->nlmsg_len
= skb
->tail
- b
;
393 nfnetlink_send(skb
, 0, group
, 0);
401 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
403 static int ctnetlink_done(struct netlink_callback
*cb
)
406 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
410 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
413 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
415 struct nf_conn
*ct
, *last
;
416 struct nf_conntrack_tuple_hash
*h
;
418 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
419 u_int8_t l3proto
= nfmsg
->nfgen_family
;
421 read_lock_bh(&nf_conntrack_lock
);
422 last
= (struct nf_conn
*)cb
->args
[1];
423 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
425 list_for_each_prev(i
, &nf_conntrack_hash
[cb
->args
[0]]) {
426 h
= (struct nf_conntrack_tuple_hash
*) i
;
427 if (DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
429 ct
= nf_ct_tuplehash_to_ctrack(h
);
430 /* Dump entries of a given L3 protocol number.
431 * If it is not specified, ie. l3proto == 0,
432 * then dump everything. */
433 if (l3proto
&& L3PROTO(ct
) != l3proto
)
440 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
444 nf_conntrack_get(&ct
->ct_general
);
445 cb
->args
[1] = (unsigned long)ct
;
448 #ifdef CONFIG_NF_CT_ACCT
449 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
450 IPCTNL_MSG_CT_GET_CTRZERO
)
451 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
460 read_unlock_bh(&nf_conntrack_lock
);
468 ctnetlink_parse_tuple_ip(struct nfattr
*attr
, struct nf_conntrack_tuple
*tuple
)
470 struct nfattr
*tb
[CTA_IP_MAX
];
471 struct nf_conntrack_l3proto
*l3proto
;
474 nfattr_parse_nested(tb
, CTA_IP_MAX
, attr
);
476 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
478 if (likely(l3proto
->nfattr_to_tuple
))
479 ret
= l3proto
->nfattr_to_tuple(tb
, tuple
);
481 nf_ct_l3proto_put(l3proto
);
486 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
487 [CTA_PROTO_NUM
-1] = sizeof(u_int8_t
),
491 ctnetlink_parse_tuple_proto(struct nfattr
*attr
,
492 struct nf_conntrack_tuple
*tuple
)
494 struct nfattr
*tb
[CTA_PROTO_MAX
];
495 struct nf_conntrack_protocol
*proto
;
498 nfattr_parse_nested(tb
, CTA_PROTO_MAX
, attr
);
500 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
503 if (!tb
[CTA_PROTO_NUM
-1])
505 tuple
->dst
.protonum
= *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_NUM
-1]);
507 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
509 if (likely(proto
->nfattr_to_tuple
))
510 ret
= proto
->nfattr_to_tuple(tb
, tuple
);
512 nf_ct_proto_put(proto
);
518 ctnetlink_parse_tuple(struct nfattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
519 enum ctattr_tuple type
, u_int8_t l3num
)
521 struct nfattr
*tb
[CTA_TUPLE_MAX
];
524 memset(tuple
, 0, sizeof(*tuple
));
526 nfattr_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
-1]);
528 if (!tb
[CTA_TUPLE_IP
-1])
531 tuple
->src
.l3num
= l3num
;
533 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
-1], tuple
);
537 if (!tb
[CTA_TUPLE_PROTO
-1])
540 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
-1], tuple
);
544 /* orig and expect tuples get DIR_ORIGINAL */
545 if (type
== CTA_TUPLE_REPLY
)
546 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
548 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
553 #ifdef CONFIG_IP_NF_NAT_NEEDED
554 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
] = {
555 [CTA_PROTONAT_PORT_MIN
-1] = sizeof(u_int16_t
),
556 [CTA_PROTONAT_PORT_MAX
-1] = sizeof(u_int16_t
),
559 static int ctnetlink_parse_nat_proto(struct nfattr
*attr
,
560 const struct nf_conn
*ct
,
561 struct ip_nat_range
*range
)
563 struct nfattr
*tb
[CTA_PROTONAT_MAX
];
564 struct ip_nat_protocol
*npt
;
566 nfattr_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
);
568 if (nfattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
571 npt
= ip_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
573 if (!npt
->nfattr_to_range
) {
574 ip_nat_proto_put(npt
);
578 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
579 if (npt
->nfattr_to_range(tb
, range
) > 0)
580 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
582 ip_nat_proto_put(npt
);
587 static const size_t cta_min_nat
[CTA_NAT_MAX
] = {
588 [CTA_NAT_MINIP
-1] = sizeof(u_int32_t
),
589 [CTA_NAT_MAXIP
-1] = sizeof(u_int32_t
),
593 ctnetlink_parse_nat(struct nfattr
*nat
,
594 const struct nf_conn
*ct
, struct ip_nat_range
*range
)
596 struct nfattr
*tb
[CTA_NAT_MAX
];
599 memset(range
, 0, sizeof(*range
));
601 nfattr_parse_nested(tb
, CTA_NAT_MAX
, nat
);
603 if (nfattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
606 if (tb
[CTA_NAT_MINIP
-1])
607 range
->min_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MINIP
-1]);
609 if (!tb
[CTA_NAT_MAXIP
-1])
610 range
->max_ip
= range
->min_ip
;
612 range
->max_ip
= *(u_int32_t
*)NFA_DATA(tb
[CTA_NAT_MAXIP
-1]);
615 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
617 if (!tb
[CTA_NAT_PROTO
-1])
620 err
= ctnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
-1], ct
, range
);
629 ctnetlink_parse_help(struct nfattr
*attr
, char **helper_name
)
631 struct nfattr
*tb
[CTA_HELP_MAX
];
633 nfattr_parse_nested(tb
, CTA_HELP_MAX
, attr
);
635 if (!tb
[CTA_HELP_NAME
-1])
638 *helper_name
= NFA_DATA(tb
[CTA_HELP_NAME
-1]);
643 static const size_t cta_min
[CTA_MAX
] = {
644 [CTA_STATUS
-1] = sizeof(u_int32_t
),
645 [CTA_TIMEOUT
-1] = sizeof(u_int32_t
),
646 [CTA_MARK
-1] = sizeof(u_int32_t
),
647 [CTA_USE
-1] = sizeof(u_int32_t
),
648 [CTA_ID
-1] = sizeof(u_int32_t
)
652 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
653 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
655 struct nf_conntrack_tuple_hash
*h
;
656 struct nf_conntrack_tuple tuple
;
658 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
659 u_int8_t u3
= nfmsg
->nfgen_family
;
662 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
665 if (cda
[CTA_TUPLE_ORIG
-1])
666 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
667 else if (cda
[CTA_TUPLE_REPLY
-1])
668 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
670 /* Flush the whole table */
671 nf_conntrack_flush();
678 h
= nf_conntrack_find_get(&tuple
, NULL
);
682 ct
= nf_ct_tuplehash_to_ctrack(h
);
685 u_int32_t id
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_ID
-1]));
691 if (del_timer(&ct
->timeout
))
692 ct
->timeout
.function((unsigned long)ct
);
700 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
701 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
703 struct nf_conntrack_tuple_hash
*h
;
704 struct nf_conntrack_tuple tuple
;
706 struct sk_buff
*skb2
= NULL
;
707 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
708 u_int8_t u3
= nfmsg
->nfgen_family
;
711 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
714 #ifndef CONFIG_NF_CT_ACCT
715 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == IPCTNL_MSG_CT_GET_CTRZERO
)
718 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
719 ctnetlink_dump_table
,
720 ctnetlink_done
)) != 0)
723 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
730 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
733 if (cda
[CTA_TUPLE_ORIG
-1])
734 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
735 else if (cda
[CTA_TUPLE_REPLY
-1])
736 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
743 h
= nf_conntrack_find_get(&tuple
, NULL
);
747 ct
= nf_ct_tuplehash_to_ctrack(h
);
750 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
756 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
757 IPCTNL_MSG_CT_NEW
, 1, ct
);
762 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
775 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
778 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
779 d
= ct
->status
^ status
;
781 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
785 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
786 /* SEEN_REPLY bit can only be set */
790 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
791 /* ASSURED bit can only be set */
794 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
795 #ifndef CONFIG_IP_NF_NAT_NEEDED
798 struct ip_nat_range range
;
800 if (cda
[CTA_NAT_DST
-1]) {
801 if (ctnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
804 if (ip_nat_initialized(ct
,
805 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
807 ip_nat_setup_info(ct
, &range
, hooknum
);
809 if (cda
[CTA_NAT_SRC
-1]) {
810 if (ctnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
813 if (ip_nat_initialized(ct
,
814 HOOK2MANIP(NF_IP_POST_ROUTING
)))
816 ip_nat_setup_info(ct
, &range
, hooknum
);
821 /* Be careful here, modifying NAT bits can screw up things,
822 * so don't let users modify them directly if they don't pass
824 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
830 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
832 struct nf_conntrack_helper
*helper
;
833 struct nf_conn_help
*help
= nfct_help(ct
);
838 /* FIXME: we need to reallocate and rehash */
842 /* don't change helper of sibling connections */
846 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
850 helper
= __nf_conntrack_helper_find_byname(helpname
);
852 if (!strcmp(helpname
, ""))
860 /* we had a helper before ... */
861 nf_ct_remove_expectations(ct
);
864 /* need to zero data of old helper */
865 memset(&help
->help
, 0, sizeof(help
->help
));
869 help
->helper
= helper
;
875 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
877 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
879 if (!del_timer(&ct
->timeout
))
882 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
883 add_timer(&ct
->timeout
);
889 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
891 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
892 struct nf_conntrack_protocol
*proto
;
893 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
894 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
897 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
899 proto
= nf_ct_proto_find_get(l3num
, npt
);
901 if (proto
->from_nfattr
)
902 err
= proto
->from_nfattr(tb
, ct
);
903 nf_ct_proto_put(proto
);
909 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
913 if (cda
[CTA_HELP
-1]) {
914 err
= ctnetlink_change_helper(ct
, cda
);
919 if (cda
[CTA_TIMEOUT
-1]) {
920 err
= ctnetlink_change_timeout(ct
, cda
);
925 if (cda
[CTA_STATUS
-1]) {
926 err
= ctnetlink_change_status(ct
, cda
);
931 if (cda
[CTA_PROTOINFO
-1]) {
932 err
= ctnetlink_change_protoinfo(ct
, cda
);
937 #if defined(CONFIG_NF_CONNTRACK_MARK)
939 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
946 ctnetlink_create_conntrack(struct nfattr
*cda
[],
947 struct nf_conntrack_tuple
*otuple
,
948 struct nf_conntrack_tuple
*rtuple
)
952 struct nf_conn_help
*help
;
954 ct
= nf_conntrack_alloc(otuple
, rtuple
);
955 if (ct
== NULL
|| IS_ERR(ct
))
958 if (!cda
[CTA_TIMEOUT
-1])
960 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
962 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
963 ct
->status
|= IPS_CONFIRMED
;
965 err
= ctnetlink_change_status(ct
, cda
);
969 if (cda
[CTA_PROTOINFO
-1]) {
970 err
= ctnetlink_change_protoinfo(ct
, cda
);
975 #if defined(CONFIG_NF_CONNTRACK_MARK)
977 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
980 help
= nfct_help(ct
);
982 help
->helper
= nf_ct_helper_find_get(rtuple
);
984 add_timer(&ct
->timeout
);
985 nf_conntrack_hash_insert(ct
);
987 if (help
&& help
->helper
)
988 nf_ct_helper_put(help
->helper
);
993 nf_conntrack_free(ct
);
998 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
999 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1001 struct nf_conntrack_tuple otuple
, rtuple
;
1002 struct nf_conntrack_tuple_hash
*h
= NULL
;
1003 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1004 u_int8_t u3
= nfmsg
->nfgen_family
;
1007 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1010 if (cda
[CTA_TUPLE_ORIG
-1]) {
1011 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1016 if (cda
[CTA_TUPLE_REPLY
-1]) {
1017 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1022 write_lock_bh(&nf_conntrack_lock
);
1023 if (cda
[CTA_TUPLE_ORIG
-1])
1024 h
= __nf_conntrack_find(&otuple
, NULL
);
1025 else if (cda
[CTA_TUPLE_REPLY
-1])
1026 h
= __nf_conntrack_find(&rtuple
, NULL
);
1029 write_unlock_bh(&nf_conntrack_lock
);
1031 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1032 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1035 /* implicit 'else' */
1037 /* we only allow nat config for new conntracks */
1038 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1043 /* We manipulate the conntrack inside the global conntrack table lock,
1044 * so there's no need to increase the refcount */
1046 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1047 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1050 write_unlock_bh(&nf_conntrack_lock
);
1054 /***********************************************************************
1056 ***********************************************************************/
1059 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1060 const struct nf_conntrack_tuple
*tuple
,
1061 enum ctattr_expect type
)
1063 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1065 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1066 goto nfattr_failure
;
1068 NFA_NEST_END(skb
, nest_parms
);
1077 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1078 const struct nf_conntrack_tuple
*tuple
,
1079 const struct nf_conntrack_tuple
*mask
)
1082 struct nf_conntrack_l3proto
*l3proto
;
1083 struct nf_conntrack_protocol
*proto
;
1084 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1086 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1087 ret
= ctnetlink_dump_tuples_ip(skb
, mask
, l3proto
);
1088 nf_ct_l3proto_put(l3proto
);
1090 if (unlikely(ret
< 0))
1091 goto nfattr_failure
;
1093 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1094 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1095 nf_ct_proto_put(proto
);
1096 if (unlikely(ret
< 0))
1097 goto nfattr_failure
;
1099 NFA_NEST_END(skb
, nest_parms
);
1108 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1109 const struct nf_conntrack_expect
*exp
)
1111 struct nf_conn
*master
= exp
->master
;
1112 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1113 u_int32_t id
= htonl(exp
->id
);
1115 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1116 goto nfattr_failure
;
1117 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1118 goto nfattr_failure
;
1119 if (ctnetlink_exp_dump_tuple(skb
,
1120 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1121 CTA_EXPECT_MASTER
) < 0)
1122 goto nfattr_failure
;
1124 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1125 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1134 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1137 const struct nf_conntrack_expect
*exp
)
1139 struct nlmsghdr
*nlh
;
1140 struct nfgenmsg
*nfmsg
;
1145 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1146 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1147 nfmsg
= NLMSG_DATA(nlh
);
1149 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1150 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1151 nfmsg
->version
= NFNETLINK_V0
;
1154 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1155 goto nfattr_failure
;
1157 nlh
->nlmsg_len
= skb
->tail
- b
;
1162 skb_trim(skb
, b
- skb
->data
);
1166 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1167 static int ctnetlink_expect_event(struct notifier_block
*this,
1168 unsigned long events
, void *ptr
)
1170 struct nlmsghdr
*nlh
;
1171 struct nfgenmsg
*nfmsg
;
1172 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1173 struct sk_buff
*skb
;
1178 if (events
& IPEXP_NEW
) {
1179 type
= IPCTNL_MSG_EXP_NEW
;
1180 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1184 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1187 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1193 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1194 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1195 nfmsg
= NLMSG_DATA(nlh
);
1197 nlh
->nlmsg_flags
= flags
;
1198 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1199 nfmsg
->version
= NFNETLINK_V0
;
1202 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1203 goto nfattr_failure
;
1205 nlh
->nlmsg_len
= skb
->tail
- b
;
1206 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1217 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1219 struct nf_conntrack_expect
*exp
= NULL
;
1220 struct list_head
*i
;
1221 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1222 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1223 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1225 read_lock_bh(&nf_conntrack_lock
);
1226 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1227 exp
= (struct nf_conntrack_expect
*) i
;
1228 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1232 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1240 read_unlock_bh(&nf_conntrack_lock
);
1245 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1246 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1247 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1251 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1252 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1254 struct nf_conntrack_tuple tuple
;
1255 struct nf_conntrack_expect
*exp
;
1256 struct sk_buff
*skb2
;
1257 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1258 u_int8_t u3
= nfmsg
->nfgen_family
;
1261 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1264 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1267 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1268 ctnetlink_exp_dump_table
,
1269 ctnetlink_done
)) != 0)
1271 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1272 if (rlen
> skb
->len
)
1274 skb_pull(skb
, rlen
);
1278 if (cda
[CTA_EXPECT_MASTER
-1])
1279 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1286 exp
= nf_conntrack_expect_find(&tuple
);
1290 if (cda
[CTA_EXPECT_ID
-1]) {
1291 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1292 if (exp
->id
!= ntohl(id
)) {
1293 nf_conntrack_expect_put(exp
);
1299 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1303 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1304 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1309 nf_conntrack_expect_put(exp
);
1311 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1316 nf_conntrack_expect_put(exp
);
1321 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1322 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1324 struct nf_conntrack_expect
*exp
, *tmp
;
1325 struct nf_conntrack_tuple tuple
;
1326 struct nf_conntrack_helper
*h
;
1327 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1328 u_int8_t u3
= nfmsg
->nfgen_family
;
1331 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1334 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1335 /* delete a single expect by tuple */
1336 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1340 /* bump usage count to 2 */
1341 exp
= nf_conntrack_expect_find(&tuple
);
1345 if (cda
[CTA_EXPECT_ID
-1]) {
1347 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1348 if (exp
->id
!= ntohl(id
)) {
1349 nf_conntrack_expect_put(exp
);
1354 /* after list removal, usage count == 1 */
1355 nf_conntrack_unexpect_related(exp
);
1356 /* have to put what we 'get' above.
1357 * after this line usage count == 0 */
1358 nf_conntrack_expect_put(exp
);
1359 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1360 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1362 /* delete all expectations for this helper */
1363 write_lock_bh(&nf_conntrack_lock
);
1364 h
= __nf_conntrack_helper_find_byname(name
);
1366 write_unlock_bh(&nf_conntrack_lock
);
1369 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1371 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1372 if (m_help
->helper
== h
1373 && del_timer(&exp
->timeout
)) {
1374 nf_ct_unlink_expect(exp
);
1375 nf_conntrack_expect_put(exp
);
1378 write_unlock_bh(&nf_conntrack_lock
);
1380 /* This basically means we have to flush everything*/
1381 write_lock_bh(&nf_conntrack_lock
);
1382 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1384 if (del_timer(&exp
->timeout
)) {
1385 nf_ct_unlink_expect(exp
);
1386 nf_conntrack_expect_put(exp
);
1389 write_unlock_bh(&nf_conntrack_lock
);
1395 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1401 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1403 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1404 struct nf_conntrack_tuple_hash
*h
= NULL
;
1405 struct nf_conntrack_expect
*exp
;
1407 struct nf_conn_help
*help
;
1410 /* caller guarantees that those three CTA_EXPECT_* exist */
1411 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1414 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1417 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1421 /* Look for master conntrack of this expectation */
1422 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1425 ct
= nf_ct_tuplehash_to_ctrack(h
);
1426 help
= nfct_help(ct
);
1428 if (!help
|| !help
->helper
) {
1429 /* such conntrack hasn't got any helper, abort */
1434 exp
= nf_conntrack_expect_alloc(ct
);
1440 exp
->expectfn
= NULL
;
1443 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1444 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1446 err
= nf_conntrack_expect_related(exp
);
1447 nf_conntrack_expect_put(exp
);
1450 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1455 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1456 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1458 struct nf_conntrack_tuple tuple
;
1459 struct nf_conntrack_expect
*exp
;
1460 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1461 u_int8_t u3
= nfmsg
->nfgen_family
;
1464 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1467 if (!cda
[CTA_EXPECT_TUPLE
-1]
1468 || !cda
[CTA_EXPECT_MASK
-1]
1469 || !cda
[CTA_EXPECT_MASTER
-1])
1472 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1476 write_lock_bh(&nf_conntrack_lock
);
1477 exp
= __nf_conntrack_expect_find(&tuple
);
1480 write_unlock_bh(&nf_conntrack_lock
);
1482 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1483 err
= ctnetlink_create_expect(cda
, u3
);
1488 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1489 err
= ctnetlink_change_expect(exp
, cda
);
1490 write_unlock_bh(&nf_conntrack_lock
);
1495 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1496 static struct notifier_block ctnl_notifier
= {
1497 .notifier_call
= ctnetlink_conntrack_event
,
1500 static struct notifier_block ctnl_notifier_exp
= {
1501 .notifier_call
= ctnetlink_expect_event
,
1505 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1506 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1507 .attr_count
= CTA_MAX
, },
1508 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1509 .attr_count
= CTA_MAX
, },
1510 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1511 .attr_count
= CTA_MAX
, },
1512 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1513 .attr_count
= CTA_MAX
, },
1516 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1517 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1518 .attr_count
= CTA_EXPECT_MAX
, },
1519 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1520 .attr_count
= CTA_EXPECT_MAX
, },
1521 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1522 .attr_count
= CTA_EXPECT_MAX
, },
1525 static struct nfnetlink_subsystem ctnl_subsys
= {
1526 .name
= "conntrack",
1527 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1528 .cb_count
= IPCTNL_MSG_MAX
,
1532 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1533 .name
= "conntrack_expect",
1534 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1535 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1539 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1540 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1542 static int __init
ctnetlink_init(void)
1546 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1547 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1549 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1553 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1555 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1556 goto err_unreg_subsys
;
1559 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1560 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1562 printk("ctnetlink_init: cannot register notifier.\n");
1563 goto err_unreg_exp_subsys
;
1566 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1568 printk("ctnetlink_init: cannot expect register notifier.\n");
1569 goto err_unreg_notifier
;
1575 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1577 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1578 err_unreg_exp_subsys
:
1579 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1582 nfnetlink_subsys_unregister(&ctnl_subsys
);
1587 static void __exit
ctnetlink_exit(void)
1589 printk("ctnetlink: unregistering from nfnetlink.\n");
1591 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1592 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1593 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1596 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1597 nfnetlink_subsys_unregister(&ctnl_subsys
);
1601 module_init(ctnetlink_init
);
1602 module_exit(ctnetlink_exit
);