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
);
755 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
757 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
758 IPCTNL_MSG_CT_NEW
, 1, ct
);
763 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
776 ctnetlink_change_status(struct nf_conn
*ct
, struct nfattr
*cda
[])
779 unsigned status
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_STATUS
-1]));
780 d
= ct
->status
^ status
;
782 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
786 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
787 /* SEEN_REPLY bit can only be set */
791 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
792 /* ASSURED bit can only be set */
795 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
796 #ifndef CONFIG_IP_NF_NAT_NEEDED
799 struct ip_nat_range range
;
801 if (cda
[CTA_NAT_DST
-1]) {
802 if (ctnetlink_parse_nat(cda
[CTA_NAT_DST
-1], ct
,
805 if (ip_nat_initialized(ct
,
806 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
808 ip_nat_setup_info(ct
, &range
, hooknum
);
810 if (cda
[CTA_NAT_SRC
-1]) {
811 if (ctnetlink_parse_nat(cda
[CTA_NAT_SRC
-1], ct
,
814 if (ip_nat_initialized(ct
,
815 HOOK2MANIP(NF_IP_POST_ROUTING
)))
817 ip_nat_setup_info(ct
, &range
, hooknum
);
822 /* Be careful here, modifying NAT bits can screw up things,
823 * so don't let users modify them directly if they don't pass
825 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
831 ctnetlink_change_helper(struct nf_conn
*ct
, struct nfattr
*cda
[])
833 struct nf_conntrack_helper
*helper
;
834 struct nf_conn_help
*help
= nfct_help(ct
);
839 /* FIXME: we need to reallocate and rehash */
843 /* don't change helper of sibling connections */
847 err
= ctnetlink_parse_help(cda
[CTA_HELP
-1], &helpname
);
851 helper
= __nf_conntrack_helper_find_byname(helpname
);
853 if (!strcmp(helpname
, ""))
861 /* we had a helper before ... */
862 nf_ct_remove_expectations(ct
);
865 /* need to zero data of old helper */
866 memset(&help
->help
, 0, sizeof(help
->help
));
870 help
->helper
= helper
;
876 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nfattr
*cda
[])
878 u_int32_t timeout
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
880 if (!del_timer(&ct
->timeout
))
883 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
884 add_timer(&ct
->timeout
);
890 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nfattr
*cda
[])
892 struct nfattr
*tb
[CTA_PROTOINFO_MAX
], *attr
= cda
[CTA_PROTOINFO
-1];
893 struct nf_conntrack_protocol
*proto
;
894 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
895 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
898 nfattr_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
);
900 proto
= nf_ct_proto_find_get(l3num
, npt
);
902 if (proto
->from_nfattr
)
903 err
= proto
->from_nfattr(tb
, ct
);
904 nf_ct_proto_put(proto
);
910 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nfattr
*cda
[])
914 if (cda
[CTA_HELP
-1]) {
915 err
= ctnetlink_change_helper(ct
, cda
);
920 if (cda
[CTA_TIMEOUT
-1]) {
921 err
= ctnetlink_change_timeout(ct
, cda
);
926 if (cda
[CTA_STATUS
-1]) {
927 err
= ctnetlink_change_status(ct
, cda
);
932 if (cda
[CTA_PROTOINFO
-1]) {
933 err
= ctnetlink_change_protoinfo(ct
, cda
);
938 #if defined(CONFIG_NF_CONNTRACK_MARK)
940 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
947 ctnetlink_create_conntrack(struct nfattr
*cda
[],
948 struct nf_conntrack_tuple
*otuple
,
949 struct nf_conntrack_tuple
*rtuple
)
953 struct nf_conn_help
*help
;
955 ct
= nf_conntrack_alloc(otuple
, rtuple
);
956 if (ct
== NULL
|| IS_ERR(ct
))
959 if (!cda
[CTA_TIMEOUT
-1])
961 ct
->timeout
.expires
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_TIMEOUT
-1]));
963 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
964 ct
->status
|= IPS_CONFIRMED
;
966 err
= ctnetlink_change_status(ct
, cda
);
970 if (cda
[CTA_PROTOINFO
-1]) {
971 err
= ctnetlink_change_protoinfo(ct
, cda
);
976 #if defined(CONFIG_NF_CONNTRACK_MARK)
978 ct
->mark
= ntohl(*(u_int32_t
*)NFA_DATA(cda
[CTA_MARK
-1]));
981 help
= nfct_help(ct
);
983 help
->helper
= nf_ct_helper_find_get(rtuple
);
985 add_timer(&ct
->timeout
);
986 nf_conntrack_hash_insert(ct
);
988 if (help
&& help
->helper
)
989 nf_ct_helper_put(help
->helper
);
994 nf_conntrack_free(ct
);
999 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1000 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1002 struct nf_conntrack_tuple otuple
, rtuple
;
1003 struct nf_conntrack_tuple_hash
*h
= NULL
;
1004 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1005 u_int8_t u3
= nfmsg
->nfgen_family
;
1008 if (nfattr_bad_size(cda
, CTA_MAX
, cta_min
))
1011 if (cda
[CTA_TUPLE_ORIG
-1]) {
1012 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1017 if (cda
[CTA_TUPLE_REPLY
-1]) {
1018 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1023 write_lock_bh(&nf_conntrack_lock
);
1024 if (cda
[CTA_TUPLE_ORIG
-1])
1025 h
= __nf_conntrack_find(&otuple
, NULL
);
1026 else if (cda
[CTA_TUPLE_REPLY
-1])
1027 h
= __nf_conntrack_find(&rtuple
, NULL
);
1030 write_unlock_bh(&nf_conntrack_lock
);
1032 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1033 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1036 /* implicit 'else' */
1038 /* we only allow nat config for new conntracks */
1039 if (cda
[CTA_NAT_SRC
-1] || cda
[CTA_NAT_DST
-1]) {
1044 /* We manipulate the conntrack inside the global conntrack table lock,
1045 * so there's no need to increase the refcount */
1047 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1048 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
), cda
);
1051 write_unlock_bh(&nf_conntrack_lock
);
1055 /***********************************************************************
1057 ***********************************************************************/
1060 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1061 const struct nf_conntrack_tuple
*tuple
,
1062 enum ctattr_expect type
)
1064 struct nfattr
*nest_parms
= NFA_NEST(skb
, type
);
1066 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1067 goto nfattr_failure
;
1069 NFA_NEST_END(skb
, nest_parms
);
1078 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1079 const struct nf_conntrack_tuple
*tuple
,
1080 const struct nf_conntrack_tuple
*mask
)
1083 struct nf_conntrack_l3proto
*l3proto
;
1084 struct nf_conntrack_protocol
*proto
;
1085 struct nfattr
*nest_parms
= NFA_NEST(skb
, CTA_EXPECT_MASK
);
1087 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1088 ret
= ctnetlink_dump_tuples_ip(skb
, mask
, l3proto
);
1089 nf_ct_l3proto_put(l3proto
);
1091 if (unlikely(ret
< 0))
1092 goto nfattr_failure
;
1094 proto
= nf_ct_proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1095 ret
= ctnetlink_dump_tuples_proto(skb
, mask
, proto
);
1096 nf_ct_proto_put(proto
);
1097 if (unlikely(ret
< 0))
1098 goto nfattr_failure
;
1100 NFA_NEST_END(skb
, nest_parms
);
1109 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1110 const struct nf_conntrack_expect
*exp
)
1112 struct nf_conn
*master
= exp
->master
;
1113 u_int32_t timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1114 u_int32_t id
= htonl(exp
->id
);
1116 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1117 goto nfattr_failure
;
1118 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1119 goto nfattr_failure
;
1120 if (ctnetlink_exp_dump_tuple(skb
,
1121 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1122 CTA_EXPECT_MASTER
) < 0)
1123 goto nfattr_failure
;
1125 NFA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1126 NFA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1135 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1138 const struct nf_conntrack_expect
*exp
)
1140 struct nlmsghdr
*nlh
;
1141 struct nfgenmsg
*nfmsg
;
1146 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1147 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1148 nfmsg
= NLMSG_DATA(nlh
);
1150 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1151 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1152 nfmsg
->version
= NFNETLINK_V0
;
1155 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1156 goto nfattr_failure
;
1158 nlh
->nlmsg_len
= skb
->tail
- b
;
1163 skb_trim(skb
, b
- skb
->data
);
1167 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1168 static int ctnetlink_expect_event(struct notifier_block
*this,
1169 unsigned long events
, void *ptr
)
1171 struct nlmsghdr
*nlh
;
1172 struct nfgenmsg
*nfmsg
;
1173 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1174 struct sk_buff
*skb
;
1179 if (events
& IPEXP_NEW
) {
1180 type
= IPCTNL_MSG_EXP_NEW
;
1181 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1185 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1188 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1194 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1195 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1196 nfmsg
= NLMSG_DATA(nlh
);
1198 nlh
->nlmsg_flags
= flags
;
1199 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1200 nfmsg
->version
= NFNETLINK_V0
;
1203 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1204 goto nfattr_failure
;
1206 nlh
->nlmsg_len
= skb
->tail
- b
;
1207 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1218 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1220 struct nf_conntrack_expect
*exp
= NULL
;
1221 struct list_head
*i
;
1222 u_int32_t
*id
= (u_int32_t
*) &cb
->args
[0];
1223 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1224 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1226 read_lock_bh(&nf_conntrack_lock
);
1227 list_for_each_prev(i
, &nf_conntrack_expect_list
) {
1228 exp
= (struct nf_conntrack_expect
*) i
;
1229 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1233 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1241 read_unlock_bh(&nf_conntrack_lock
);
1246 static const size_t cta_min_exp
[CTA_EXPECT_MAX
] = {
1247 [CTA_EXPECT_TIMEOUT
-1] = sizeof(u_int32_t
),
1248 [CTA_EXPECT_ID
-1] = sizeof(u_int32_t
)
1252 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1253 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1255 struct nf_conntrack_tuple tuple
;
1256 struct nf_conntrack_expect
*exp
;
1257 struct sk_buff
*skb2
;
1258 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1259 u_int8_t u3
= nfmsg
->nfgen_family
;
1262 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1265 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1268 if ((*errp
= netlink_dump_start(ctnl
, skb
, nlh
,
1269 ctnetlink_exp_dump_table
,
1270 ctnetlink_done
)) != 0)
1272 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1273 if (rlen
> skb
->len
)
1275 skb_pull(skb
, rlen
);
1279 if (cda
[CTA_EXPECT_MASTER
-1])
1280 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1287 exp
= nf_conntrack_expect_find(&tuple
);
1291 if (cda
[CTA_EXPECT_ID
-1]) {
1292 u_int32_t id
= *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1293 if (exp
->id
!= ntohl(id
)) {
1294 nf_conntrack_expect_put(exp
);
1300 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1303 NETLINK_CB(skb2
).dst_pid
= NETLINK_CB(skb
).pid
;
1305 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1306 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1311 nf_conntrack_expect_put(exp
);
1313 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1318 nf_conntrack_expect_put(exp
);
1323 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1324 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1326 struct nf_conntrack_expect
*exp
, *tmp
;
1327 struct nf_conntrack_tuple tuple
;
1328 struct nf_conntrack_helper
*h
;
1329 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1330 u_int8_t u3
= nfmsg
->nfgen_family
;
1333 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1336 if (cda
[CTA_EXPECT_TUPLE
-1]) {
1337 /* delete a single expect by tuple */
1338 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1342 /* bump usage count to 2 */
1343 exp
= nf_conntrack_expect_find(&tuple
);
1347 if (cda
[CTA_EXPECT_ID
-1]) {
1349 *(u_int32_t
*)NFA_DATA(cda
[CTA_EXPECT_ID
-1]);
1350 if (exp
->id
!= ntohl(id
)) {
1351 nf_conntrack_expect_put(exp
);
1356 /* after list removal, usage count == 1 */
1357 nf_conntrack_unexpect_related(exp
);
1358 /* have to put what we 'get' above.
1359 * after this line usage count == 0 */
1360 nf_conntrack_expect_put(exp
);
1361 } else if (cda
[CTA_EXPECT_HELP_NAME
-1]) {
1362 char *name
= NFA_DATA(cda
[CTA_EXPECT_HELP_NAME
-1]);
1364 /* delete all expectations for this helper */
1365 write_lock_bh(&nf_conntrack_lock
);
1366 h
= __nf_conntrack_helper_find_byname(name
);
1368 write_unlock_bh(&nf_conntrack_lock
);
1371 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1373 struct nf_conn_help
*m_help
= nfct_help(exp
->master
);
1374 if (m_help
->helper
== h
1375 && del_timer(&exp
->timeout
)) {
1376 nf_ct_unlink_expect(exp
);
1377 nf_conntrack_expect_put(exp
);
1380 write_unlock_bh(&nf_conntrack_lock
);
1382 /* This basically means we have to flush everything*/
1383 write_lock_bh(&nf_conntrack_lock
);
1384 list_for_each_entry_safe(exp
, tmp
, &nf_conntrack_expect_list
,
1386 if (del_timer(&exp
->timeout
)) {
1387 nf_ct_unlink_expect(exp
);
1388 nf_conntrack_expect_put(exp
);
1391 write_unlock_bh(&nf_conntrack_lock
);
1397 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nfattr
*cda
[])
1403 ctnetlink_create_expect(struct nfattr
*cda
[], u_int8_t u3
)
1405 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1406 struct nf_conntrack_tuple_hash
*h
= NULL
;
1407 struct nf_conntrack_expect
*exp
;
1409 struct nf_conn_help
*help
;
1412 /* caller guarantees that those three CTA_EXPECT_* exist */
1413 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1416 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1419 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1423 /* Look for master conntrack of this expectation */
1424 h
= nf_conntrack_find_get(&master_tuple
, NULL
);
1427 ct
= nf_ct_tuplehash_to_ctrack(h
);
1428 help
= nfct_help(ct
);
1430 if (!help
|| !help
->helper
) {
1431 /* such conntrack hasn't got any helper, abort */
1436 exp
= nf_conntrack_expect_alloc(ct
);
1442 exp
->expectfn
= NULL
;
1445 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1446 memcpy(&exp
->mask
, &mask
, sizeof(struct nf_conntrack_tuple
));
1448 err
= nf_conntrack_expect_related(exp
);
1449 nf_conntrack_expect_put(exp
);
1452 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1457 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1458 struct nlmsghdr
*nlh
, struct nfattr
*cda
[], int *errp
)
1460 struct nf_conntrack_tuple tuple
;
1461 struct nf_conntrack_expect
*exp
;
1462 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1463 u_int8_t u3
= nfmsg
->nfgen_family
;
1466 if (nfattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1469 if (!cda
[CTA_EXPECT_TUPLE
-1]
1470 || !cda
[CTA_EXPECT_MASK
-1]
1471 || !cda
[CTA_EXPECT_MASTER
-1])
1474 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1478 write_lock_bh(&nf_conntrack_lock
);
1479 exp
= __nf_conntrack_expect_find(&tuple
);
1482 write_unlock_bh(&nf_conntrack_lock
);
1484 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1485 err
= ctnetlink_create_expect(cda
, u3
);
1490 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1491 err
= ctnetlink_change_expect(exp
, cda
);
1492 write_unlock_bh(&nf_conntrack_lock
);
1497 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1498 static struct notifier_block ctnl_notifier
= {
1499 .notifier_call
= ctnetlink_conntrack_event
,
1502 static struct notifier_block ctnl_notifier_exp
= {
1503 .notifier_call
= ctnetlink_expect_event
,
1507 static struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1508 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1509 .attr_count
= CTA_MAX
, },
1510 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1511 .attr_count
= CTA_MAX
, },
1512 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1513 .attr_count
= CTA_MAX
, },
1514 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1515 .attr_count
= CTA_MAX
, },
1518 static struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1519 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1520 .attr_count
= CTA_EXPECT_MAX
, },
1521 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1522 .attr_count
= CTA_EXPECT_MAX
, },
1523 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1524 .attr_count
= CTA_EXPECT_MAX
, },
1527 static struct nfnetlink_subsystem ctnl_subsys
= {
1528 .name
= "conntrack",
1529 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1530 .cb_count
= IPCTNL_MSG_MAX
,
1534 static struct nfnetlink_subsystem ctnl_exp_subsys
= {
1535 .name
= "conntrack_expect",
1536 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1537 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1541 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1542 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1544 static int __init
ctnetlink_init(void)
1548 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1549 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1551 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1555 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1557 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1558 goto err_unreg_subsys
;
1561 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1562 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1564 printk("ctnetlink_init: cannot register notifier.\n");
1565 goto err_unreg_exp_subsys
;
1568 ret
= nf_conntrack_expect_register_notifier(&ctnl_notifier_exp
);
1570 printk("ctnetlink_init: cannot expect register notifier.\n");
1571 goto err_unreg_notifier
;
1577 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1579 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1580 err_unreg_exp_subsys
:
1581 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1584 nfnetlink_subsys_unregister(&ctnl_subsys
);
1589 static void __exit
ctnetlink_exit(void)
1591 printk("ctnetlink: unregistering from nfnetlink.\n");
1593 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1594 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp
);
1595 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1598 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1599 nfnetlink_subsys_unregister(&ctnl_subsys
);
1603 module_init(ctnetlink_init
);
1604 module_exit(ctnetlink_exit
);