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 nlattr
*nest_parms
;
59 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
62 NLA_PUT(skb
, CTA_PROTO_NUM
, sizeof(u_int8_t
), &tuple
->dst
.protonum
);
64 if (likely(l4proto
->tuple_to_nlattr
))
65 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
67 nla_nest_end(skb
, nest_parms
);
76 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
77 const struct nf_conntrack_tuple
*tuple
,
78 struct nf_conntrack_l3proto
*l3proto
)
81 struct nlattr
*nest_parms
;
83 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
87 if (likely(l3proto
->tuple_to_nlattr
))
88 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
90 nla_nest_end(skb
, nest_parms
);
99 ctnetlink_dump_tuples(struct sk_buff
*skb
,
100 const struct nf_conntrack_tuple
*tuple
)
103 struct nf_conntrack_l3proto
*l3proto
;
104 struct nf_conntrack_l4proto
*l4proto
;
106 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
107 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
108 nf_ct_l3proto_put(l3proto
);
110 if (unlikely(ret
< 0))
113 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
114 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
115 nf_ct_l4proto_put(l4proto
);
121 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
123 __be32 status
= htonl((u_int32_t
) ct
->status
);
124 NLA_PUT(skb
, CTA_STATUS
, sizeof(status
), &status
);
132 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
134 long timeout_l
= ct
->timeout
.expires
- jiffies
;
140 timeout
= htonl(timeout_l
/ HZ
);
142 NLA_PUT(skb
, CTA_TIMEOUT
, sizeof(timeout
), &timeout
);
150 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
152 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
);
153 struct nlattr
*nest_proto
;
156 if (!l4proto
->to_nlattr
) {
157 nf_ct_l4proto_put(l4proto
);
161 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
163 goto nla_put_failure
;
165 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
167 nf_ct_l4proto_put(l4proto
);
169 nla_nest_end(skb
, nest_proto
);
174 nf_ct_l4proto_put(l4proto
);
179 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
181 struct nlattr
*nest_helper
;
182 const struct nf_conn_help
*help
= nfct_help(ct
);
183 struct nf_conntrack_helper
*helper
;
189 helper
= rcu_dereference(help
->helper
);
193 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
195 goto nla_put_failure
;
196 NLA_PUT(skb
, CTA_HELP_NAME
, strlen(helper
->name
), helper
->name
);
198 if (helper
->to_nlattr
)
199 helper
->to_nlattr(skb
, ct
);
201 nla_nest_end(skb
, nest_helper
);
211 #ifdef CONFIG_NF_CT_ACCT
213 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
214 enum ip_conntrack_dir dir
)
216 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
217 struct nlattr
*nest_count
;
220 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
222 goto nla_put_failure
;
224 tmp
= htonl(ct
->counters
[dir
].packets
);
225 NLA_PUT(skb
, CTA_COUNTERS32_PACKETS
, sizeof(u_int32_t
), &tmp
);
227 tmp
= htonl(ct
->counters
[dir
].bytes
);
228 NLA_PUT(skb
, CTA_COUNTERS32_BYTES
, sizeof(u_int32_t
), &tmp
);
230 nla_nest_end(skb
, nest_count
);
238 #define ctnetlink_dump_counters(a, b, c) (0)
241 #ifdef CONFIG_NF_CONNTRACK_MARK
243 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
245 __be32 mark
= htonl(ct
->mark
);
247 NLA_PUT(skb
, CTA_MARK
, sizeof(u_int32_t
), &mark
);
254 #define ctnetlink_dump_mark(a, b) (0)
258 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
260 __be32 id
= htonl(ct
->id
);
261 NLA_PUT(skb
, CTA_ID
, sizeof(u_int32_t
), &id
);
269 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
271 __be32 use
= htonl(atomic_read(&ct
->ct_general
.use
));
273 NLA_PUT(skb
, CTA_USE
, sizeof(u_int32_t
), &use
);
280 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
283 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
284 int event
, int nowait
,
285 const struct nf_conn
*ct
)
287 struct nlmsghdr
*nlh
;
288 struct nfgenmsg
*nfmsg
;
289 struct nlattr
*nest_parms
;
290 unsigned char *b
= skb_tail_pointer(skb
);
292 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
293 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
294 nfmsg
= NLMSG_DATA(nlh
);
296 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
297 nfmsg
->nfgen_family
=
298 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
299 nfmsg
->version
= NFNETLINK_V0
;
302 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
304 goto nla_put_failure
;
305 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
306 goto nla_put_failure
;
307 nla_nest_end(skb
, nest_parms
);
309 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
311 goto nla_put_failure
;
312 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
313 goto nla_put_failure
;
314 nla_nest_end(skb
, nest_parms
);
316 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
317 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
318 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
319 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
320 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
321 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
322 ctnetlink_dump_mark(skb
, ct
) < 0 ||
323 ctnetlink_dump_id(skb
, ct
) < 0 ||
324 ctnetlink_dump_use(skb
, ct
) < 0)
325 goto nla_put_failure
;
327 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
336 #ifdef CONFIG_NF_CONNTRACK_EVENTS
337 static int ctnetlink_conntrack_event(struct notifier_block
*this,
338 unsigned long events
, void *ptr
)
340 struct nlmsghdr
*nlh
;
341 struct nfgenmsg
*nfmsg
;
342 struct nlattr
*nest_parms
;
343 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
347 unsigned int flags
= 0, group
;
349 /* ignore our fake conntrack entry */
350 if (ct
== &nf_conntrack_untracked
)
353 if (events
& IPCT_DESTROY
) {
354 type
= IPCTNL_MSG_CT_DELETE
;
355 group
= NFNLGRP_CONNTRACK_DESTROY
;
356 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
357 type
= IPCTNL_MSG_CT_NEW
;
358 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
359 group
= NFNLGRP_CONNTRACK_NEW
;
360 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
361 type
= IPCTNL_MSG_CT_NEW
;
362 group
= NFNLGRP_CONNTRACK_UPDATE
;
366 if (!nfnetlink_has_listeners(group
))
369 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
375 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
376 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
377 nfmsg
= NLMSG_DATA(nlh
);
379 nlh
->nlmsg_flags
= flags
;
380 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
381 nfmsg
->version
= NFNETLINK_V0
;
384 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
386 goto nla_put_failure
;
387 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
388 goto nla_put_failure
;
389 nla_nest_end(skb
, nest_parms
);
391 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
393 goto nla_put_failure
;
394 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
395 goto nla_put_failure
;
396 nla_nest_end(skb
, nest_parms
);
398 if (events
& IPCT_DESTROY
) {
399 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
400 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
401 goto nla_put_failure
;
403 if (ctnetlink_dump_status(skb
, ct
) < 0)
404 goto nla_put_failure
;
406 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
407 goto nla_put_failure
;
409 if (events
& IPCT_PROTOINFO
410 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
411 goto nla_put_failure
;
413 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
414 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
415 goto nla_put_failure
;
417 #ifdef CONFIG_NF_CONNTRACK_MARK
418 if ((events
& IPCT_MARK
|| ct
->mark
)
419 && ctnetlink_dump_mark(skb
, ct
) < 0)
420 goto nla_put_failure
;
423 if (events
& IPCT_COUNTER_FILLING
&&
424 (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
425 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0))
426 goto nla_put_failure
;
429 nlh
->nlmsg_len
= skb
->tail
- b
;
430 nfnetlink_send(skb
, 0, group
, 0);
438 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
440 static int ctnetlink_done(struct netlink_callback
*cb
)
443 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
447 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
450 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
452 struct nf_conn
*ct
, *last
;
453 struct nf_conntrack_tuple_hash
*h
;
454 struct hlist_node
*n
;
455 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
456 u_int8_t l3proto
= nfmsg
->nfgen_family
;
458 read_lock_bh(&nf_conntrack_lock
);
459 last
= (struct nf_conn
*)cb
->args
[1];
460 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
462 hlist_for_each_entry(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
464 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
466 ct
= nf_ct_tuplehash_to_ctrack(h
);
467 /* Dump entries of a given L3 protocol number.
468 * If it is not specified, ie. l3proto == 0,
469 * then dump everything. */
470 if (l3proto
&& L3PROTO(ct
) != l3proto
)
477 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
481 nf_conntrack_get(&ct
->ct_general
);
482 cb
->args
[1] = (unsigned long)ct
;
485 #ifdef CONFIG_NF_CT_ACCT
486 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
487 IPCTNL_MSG_CT_GET_CTRZERO
)
488 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
497 read_unlock_bh(&nf_conntrack_lock
);
505 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
507 struct nlattr
*tb
[CTA_IP_MAX
+1];
508 struct nf_conntrack_l3proto
*l3proto
;
511 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
513 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
515 if (likely(l3proto
->nlattr_to_tuple
))
516 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
518 nf_ct_l3proto_put(l3proto
);
523 static const size_t cta_min_proto
[CTA_PROTO_MAX
+1] = {
524 [CTA_PROTO_NUM
] = sizeof(u_int8_t
),
528 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
529 struct nf_conntrack_tuple
*tuple
)
531 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
532 struct nf_conntrack_l4proto
*l4proto
;
535 nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, NULL
);
537 if (nlattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
540 if (!tb
[CTA_PROTO_NUM
])
542 tuple
->dst
.protonum
= *(u_int8_t
*)nla_data(tb
[CTA_PROTO_NUM
]);
544 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
546 if (likely(l4proto
->nlattr_to_tuple
))
547 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
549 nf_ct_l4proto_put(l4proto
);
555 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
556 enum ctattr_tuple type
, u_int8_t l3num
)
558 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
561 memset(tuple
, 0, sizeof(*tuple
));
563 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
565 if (!tb
[CTA_TUPLE_IP
])
568 tuple
->src
.l3num
= l3num
;
570 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
574 if (!tb
[CTA_TUPLE_PROTO
])
577 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
581 /* orig and expect tuples get DIR_ORIGINAL */
582 if (type
== CTA_TUPLE_REPLY
)
583 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
585 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
590 #ifdef CONFIG_NF_NAT_NEEDED
591 static const size_t cta_min_protonat
[CTA_PROTONAT_MAX
+1] = {
592 [CTA_PROTONAT_PORT_MIN
] = sizeof(u_int16_t
),
593 [CTA_PROTONAT_PORT_MAX
] = sizeof(u_int16_t
),
596 static int nfnetlink_parse_nat_proto(struct nlattr
*attr
,
597 const struct nf_conn
*ct
,
598 struct nf_nat_range
*range
)
600 struct nlattr
*tb
[CTA_PROTONAT_MAX
+1];
601 struct nf_nat_protocol
*npt
;
603 nla_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
, NULL
);
605 if (nlattr_bad_size(tb
, CTA_PROTONAT_MAX
, cta_min_protonat
))
608 npt
= nf_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
610 if (!npt
->nlattr_to_range
) {
611 nf_nat_proto_put(npt
);
615 /* nlattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
616 if (npt
->nlattr_to_range(tb
, range
) > 0)
617 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
619 nf_nat_proto_put(npt
);
624 static const size_t cta_min_nat
[CTA_NAT_MAX
+1] = {
625 [CTA_NAT_MINIP
] = sizeof(u_int32_t
),
626 [CTA_NAT_MAXIP
] = sizeof(u_int32_t
),
630 nfnetlink_parse_nat(struct nlattr
*nat
,
631 const struct nf_conn
*ct
, struct nf_nat_range
*range
)
633 struct nlattr
*tb
[CTA_NAT_MAX
+1];
636 memset(range
, 0, sizeof(*range
));
638 nla_parse_nested(tb
, CTA_NAT_MAX
, nat
, NULL
);
640 if (nlattr_bad_size(tb
, CTA_NAT_MAX
, cta_min_nat
))
643 if (tb
[CTA_NAT_MINIP
])
644 range
->min_ip
= *(__be32
*)nla_data(tb
[CTA_NAT_MINIP
]);
646 if (!tb
[CTA_NAT_MAXIP
])
647 range
->max_ip
= range
->min_ip
;
649 range
->max_ip
= *(__be32
*)nla_data(tb
[CTA_NAT_MAXIP
]);
652 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
654 if (!tb
[CTA_NAT_PROTO
])
657 err
= nfnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
], ct
, range
);
666 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
668 struct nlattr
*tb
[CTA_HELP_MAX
+1];
670 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
672 if (!tb
[CTA_HELP_NAME
])
675 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
680 static const size_t cta_min
[CTA_MAX
+1] = {
681 [CTA_STATUS
] = sizeof(u_int32_t
),
682 [CTA_TIMEOUT
] = sizeof(u_int32_t
),
683 [CTA_MARK
] = sizeof(u_int32_t
),
684 [CTA_USE
] = sizeof(u_int32_t
),
685 [CTA_ID
] = sizeof(u_int32_t
)
689 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
690 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
692 struct nf_conntrack_tuple_hash
*h
;
693 struct nf_conntrack_tuple tuple
;
695 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
696 u_int8_t u3
= nfmsg
->nfgen_family
;
699 if (nlattr_bad_size(cda
, CTA_MAX
, cta_min
))
702 if (cda
[CTA_TUPLE_ORIG
])
703 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
704 else if (cda
[CTA_TUPLE_REPLY
])
705 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
707 /* Flush the whole table */
708 nf_conntrack_flush();
715 h
= nf_conntrack_find_get(&tuple
);
719 ct
= nf_ct_tuplehash_to_ctrack(h
);
722 u_int32_t id
= ntohl(*(__be32
*)nla_data(cda
[CTA_ID
]));
728 if (del_timer(&ct
->timeout
))
729 ct
->timeout
.function((unsigned long)ct
);
737 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
738 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
740 struct nf_conntrack_tuple_hash
*h
;
741 struct nf_conntrack_tuple tuple
;
743 struct sk_buff
*skb2
= NULL
;
744 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
745 u_int8_t u3
= nfmsg
->nfgen_family
;
748 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
749 #ifndef CONFIG_NF_CT_ACCT
750 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == IPCTNL_MSG_CT_GET_CTRZERO
)
753 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
757 if (nlattr_bad_size(cda
, CTA_MAX
, cta_min
))
760 if (cda
[CTA_TUPLE_ORIG
])
761 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
762 else if (cda
[CTA_TUPLE_REPLY
])
763 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
770 h
= nf_conntrack_find_get(&tuple
);
774 ct
= nf_ct_tuplehash_to_ctrack(h
);
777 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
783 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
784 IPCTNL_MSG_CT_NEW
, 1, ct
);
789 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
802 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
805 unsigned int status
= ntohl(*(__be32
*)nla_data(cda
[CTA_STATUS
]));
806 d
= ct
->status
^ status
;
808 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
812 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
813 /* SEEN_REPLY bit can only be set */
817 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
818 /* ASSURED bit can only be set */
821 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
822 #ifndef CONFIG_NF_NAT_NEEDED
825 struct nf_nat_range range
;
827 if (cda
[CTA_NAT_DST
]) {
828 if (nfnetlink_parse_nat(cda
[CTA_NAT_DST
], ct
,
831 if (nf_nat_initialized(ct
,
832 HOOK2MANIP(NF_IP_PRE_ROUTING
)))
834 nf_nat_setup_info(ct
, &range
, NF_IP_PRE_ROUTING
);
836 if (cda
[CTA_NAT_SRC
]) {
837 if (nfnetlink_parse_nat(cda
[CTA_NAT_SRC
], ct
,
840 if (nf_nat_initialized(ct
,
841 HOOK2MANIP(NF_IP_POST_ROUTING
)))
843 nf_nat_setup_info(ct
, &range
, NF_IP_POST_ROUTING
);
848 /* Be careful here, modifying NAT bits can screw up things,
849 * so don't let users modify them directly if they don't pass
851 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
857 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
859 struct nf_conntrack_helper
*helper
;
860 struct nf_conn_help
*help
= nfct_help(ct
);
864 /* don't change helper of sibling connections */
868 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
872 if (!strcmp(helpname
, "")) {
873 if (help
&& help
->helper
) {
874 /* we had a helper before ... */
875 nf_ct_remove_expectations(ct
);
876 rcu_assign_pointer(help
->helper
, NULL
);
882 helper
= __nf_conntrack_helper_find_byname(helpname
);
887 if (help
->helper
== helper
)
891 /* need to zero data of old helper */
892 memset(&help
->help
, 0, sizeof(help
->help
));
894 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
899 rcu_assign_pointer(help
->helper
, helper
);
905 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
907 u_int32_t timeout
= ntohl(*(__be32
*)nla_data(cda
[CTA_TIMEOUT
]));
909 if (!del_timer(&ct
->timeout
))
912 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
913 add_timer(&ct
->timeout
);
919 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
921 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
922 struct nf_conntrack_l4proto
*l4proto
;
923 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
924 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
927 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
929 l4proto
= nf_ct_l4proto_find_get(l3num
, npt
);
931 if (l4proto
->from_nlattr
)
932 err
= l4proto
->from_nlattr(tb
, ct
);
933 nf_ct_l4proto_put(l4proto
);
939 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
944 err
= ctnetlink_change_helper(ct
, cda
);
949 if (cda
[CTA_TIMEOUT
]) {
950 err
= ctnetlink_change_timeout(ct
, cda
);
955 if (cda
[CTA_STATUS
]) {
956 err
= ctnetlink_change_status(ct
, cda
);
961 if (cda
[CTA_PROTOINFO
]) {
962 err
= ctnetlink_change_protoinfo(ct
, cda
);
967 #if defined(CONFIG_NF_CONNTRACK_MARK)
969 ct
->mark
= ntohl(*(__be32
*)nla_data(cda
[CTA_MARK
]));
976 ctnetlink_create_conntrack(struct nlattr
*cda
[],
977 struct nf_conntrack_tuple
*otuple
,
978 struct nf_conntrack_tuple
*rtuple
)
982 struct nf_conn_help
*help
;
983 struct nf_conntrack_helper
*helper
;
985 ct
= nf_conntrack_alloc(otuple
, rtuple
);
986 if (ct
== NULL
|| IS_ERR(ct
))
989 if (!cda
[CTA_TIMEOUT
])
991 ct
->timeout
.expires
= ntohl(*(__be32
*)nla_data(cda
[CTA_TIMEOUT
]));
993 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
994 ct
->status
|= IPS_CONFIRMED
;
996 if (cda
[CTA_STATUS
]) {
997 err
= ctnetlink_change_status(ct
, cda
);
1002 if (cda
[CTA_PROTOINFO
]) {
1003 err
= ctnetlink_change_protoinfo(ct
, cda
);
1008 #if defined(CONFIG_NF_CONNTRACK_MARK)
1010 ct
->mark
= ntohl(*(__be32
*)nla_data(cda
[CTA_MARK
]));
1013 helper
= nf_ct_helper_find_get(rtuple
);
1015 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
1017 nf_ct_helper_put(helper
);
1021 /* not in hash table yet so not strictly necessary */
1022 rcu_assign_pointer(help
->helper
, helper
);
1025 add_timer(&ct
->timeout
);
1026 nf_conntrack_hash_insert(ct
);
1029 nf_ct_helper_put(helper
);
1034 nf_conntrack_free(ct
);
1039 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1040 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1042 struct nf_conntrack_tuple otuple
, rtuple
;
1043 struct nf_conntrack_tuple_hash
*h
= NULL
;
1044 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1045 u_int8_t u3
= nfmsg
->nfgen_family
;
1048 if (nlattr_bad_size(cda
, CTA_MAX
, cta_min
))
1051 if (cda
[CTA_TUPLE_ORIG
]) {
1052 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1057 if (cda
[CTA_TUPLE_REPLY
]) {
1058 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1063 write_lock_bh(&nf_conntrack_lock
);
1064 if (cda
[CTA_TUPLE_ORIG
])
1065 h
= __nf_conntrack_find(&otuple
, NULL
);
1066 else if (cda
[CTA_TUPLE_REPLY
])
1067 h
= __nf_conntrack_find(&rtuple
, NULL
);
1070 write_unlock_bh(&nf_conntrack_lock
);
1072 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1073 err
= ctnetlink_create_conntrack(cda
, &otuple
, &rtuple
);
1076 /* implicit 'else' */
1078 /* We manipulate the conntrack inside the global conntrack table lock,
1079 * so there's no need to increase the refcount */
1081 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1082 /* we only allow nat config for new conntracks */
1083 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1087 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
),
1092 write_unlock_bh(&nf_conntrack_lock
);
1096 /***********************************************************************
1098 ***********************************************************************/
1101 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1102 const struct nf_conntrack_tuple
*tuple
,
1103 enum ctattr_expect type
)
1105 struct nlattr
*nest_parms
;
1107 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1109 goto nla_put_failure
;
1110 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1111 goto nla_put_failure
;
1112 nla_nest_end(skb
, nest_parms
);
1121 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1122 const struct nf_conntrack_tuple
*tuple
,
1123 const struct nf_conntrack_tuple_mask
*mask
)
1126 struct nf_conntrack_l3proto
*l3proto
;
1127 struct nf_conntrack_l4proto
*l4proto
;
1128 struct nf_conntrack_tuple m
;
1129 struct nlattr
*nest_parms
;
1131 memset(&m
, 0xFF, sizeof(m
));
1132 m
.src
.u
.all
= mask
->src
.u
.all
;
1133 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1135 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1137 goto nla_put_failure
;
1139 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1140 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1141 nf_ct_l3proto_put(l3proto
);
1143 if (unlikely(ret
< 0))
1144 goto nla_put_failure
;
1146 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1147 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1148 nf_ct_l4proto_put(l4proto
);
1149 if (unlikely(ret
< 0))
1150 goto nla_put_failure
;
1152 nla_nest_end(skb
, nest_parms
);
1161 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1162 const struct nf_conntrack_expect
*exp
)
1164 struct nf_conn
*master
= exp
->master
;
1165 __be32 timeout
= htonl((exp
->timeout
.expires
- jiffies
) / HZ
);
1166 __be32 id
= htonl(exp
->id
);
1168 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1169 goto nla_put_failure
;
1170 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1171 goto nla_put_failure
;
1172 if (ctnetlink_exp_dump_tuple(skb
,
1173 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1174 CTA_EXPECT_MASTER
) < 0)
1175 goto nla_put_failure
;
1177 NLA_PUT(skb
, CTA_EXPECT_TIMEOUT
, sizeof(timeout
), &timeout
);
1178 NLA_PUT(skb
, CTA_EXPECT_ID
, sizeof(u_int32_t
), &id
);
1187 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1190 const struct nf_conntrack_expect
*exp
)
1192 struct nlmsghdr
*nlh
;
1193 struct nfgenmsg
*nfmsg
;
1194 unsigned char *b
= skb_tail_pointer(skb
);
1196 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1197 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1198 nfmsg
= NLMSG_DATA(nlh
);
1200 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1201 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1202 nfmsg
->version
= NFNETLINK_V0
;
1205 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1206 goto nla_put_failure
;
1208 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1217 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1218 static int ctnetlink_expect_event(struct notifier_block
*this,
1219 unsigned long events
, void *ptr
)
1221 struct nlmsghdr
*nlh
;
1222 struct nfgenmsg
*nfmsg
;
1223 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1224 struct sk_buff
*skb
;
1229 if (events
& IPEXP_NEW
) {
1230 type
= IPCTNL_MSG_EXP_NEW
;
1231 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1235 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1238 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1244 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1245 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1246 nfmsg
= NLMSG_DATA(nlh
);
1248 nlh
->nlmsg_flags
= flags
;
1249 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1250 nfmsg
->version
= NFNETLINK_V0
;
1253 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1254 goto nla_put_failure
;
1256 nlh
->nlmsg_len
= skb
->tail
- b
;
1257 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1266 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1269 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1274 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1276 struct nf_conntrack_expect
*exp
, *last
;
1277 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1278 struct hlist_node
*n
;
1279 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1281 read_lock_bh(&nf_conntrack_lock
);
1282 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1283 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1285 hlist_for_each_entry(exp
, n
, &nf_ct_expect_hash
[cb
->args
[0]],
1287 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1294 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1298 atomic_inc(&exp
->use
);
1299 cb
->args
[1] = (unsigned long)exp
;
1309 read_unlock_bh(&nf_conntrack_lock
);
1311 nf_ct_expect_put(last
);
1316 static const size_t cta_min_exp
[CTA_EXPECT_MAX
+1] = {
1317 [CTA_EXPECT_TIMEOUT
] = sizeof(u_int32_t
),
1318 [CTA_EXPECT_ID
] = sizeof(u_int32_t
)
1322 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1323 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1325 struct nf_conntrack_tuple tuple
;
1326 struct nf_conntrack_expect
*exp
;
1327 struct sk_buff
*skb2
;
1328 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1329 u_int8_t u3
= nfmsg
->nfgen_family
;
1332 if (nlattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1335 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1336 return netlink_dump_start(ctnl
, skb
, nlh
,
1337 ctnetlink_exp_dump_table
,
1338 ctnetlink_exp_done
);
1341 if (cda
[CTA_EXPECT_MASTER
])
1342 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1349 exp
= nf_ct_expect_find_get(&tuple
);
1353 if (cda
[CTA_EXPECT_ID
]) {
1354 __be32 id
= *(__be32
*)nla_data(cda
[CTA_EXPECT_ID
]);
1355 if (exp
->id
!= ntohl(id
)) {
1356 nf_ct_expect_put(exp
);
1362 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1366 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1367 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1372 nf_ct_expect_put(exp
);
1374 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1379 nf_ct_expect_put(exp
);
1384 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1385 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1387 struct nf_conntrack_expect
*exp
;
1388 struct nf_conntrack_tuple tuple
;
1389 struct nf_conntrack_helper
*h
;
1390 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1391 struct hlist_node
*n
, *next
;
1392 u_int8_t u3
= nfmsg
->nfgen_family
;
1396 if (nlattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1399 if (cda
[CTA_EXPECT_TUPLE
]) {
1400 /* delete a single expect by tuple */
1401 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1405 /* bump usage count to 2 */
1406 exp
= nf_ct_expect_find_get(&tuple
);
1410 if (cda
[CTA_EXPECT_ID
]) {
1411 __be32 id
= *(__be32
*)nla_data(cda
[CTA_EXPECT_ID
]);
1412 if (exp
->id
!= ntohl(id
)) {
1413 nf_ct_expect_put(exp
);
1418 /* after list removal, usage count == 1 */
1419 nf_ct_unexpect_related(exp
);
1420 /* have to put what we 'get' above.
1421 * after this line usage count == 0 */
1422 nf_ct_expect_put(exp
);
1423 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1424 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1425 struct nf_conn_help
*m_help
;
1427 /* delete all expectations for this helper */
1428 write_lock_bh(&nf_conntrack_lock
);
1429 h
= __nf_conntrack_helper_find_byname(name
);
1431 write_unlock_bh(&nf_conntrack_lock
);
1434 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1435 hlist_for_each_entry_safe(exp
, n
, next
,
1436 &nf_ct_expect_hash
[i
],
1438 m_help
= nfct_help(exp
->master
);
1439 if (m_help
->helper
== h
1440 && del_timer(&exp
->timeout
)) {
1441 nf_ct_unlink_expect(exp
);
1442 nf_ct_expect_put(exp
);
1446 write_unlock_bh(&nf_conntrack_lock
);
1448 /* This basically means we have to flush everything*/
1449 write_lock_bh(&nf_conntrack_lock
);
1450 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1451 hlist_for_each_entry_safe(exp
, n
, next
,
1452 &nf_ct_expect_hash
[i
],
1454 if (del_timer(&exp
->timeout
)) {
1455 nf_ct_unlink_expect(exp
);
1456 nf_ct_expect_put(exp
);
1460 write_unlock_bh(&nf_conntrack_lock
);
1466 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1472 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
)
1474 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1475 struct nf_conntrack_tuple_hash
*h
= NULL
;
1476 struct nf_conntrack_expect
*exp
;
1478 struct nf_conn_help
*help
;
1481 /* caller guarantees that those three CTA_EXPECT_* exist */
1482 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1485 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1488 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1492 /* Look for master conntrack of this expectation */
1493 h
= nf_conntrack_find_get(&master_tuple
);
1496 ct
= nf_ct_tuplehash_to_ctrack(h
);
1497 help
= nfct_help(ct
);
1499 if (!help
|| !help
->helper
) {
1500 /* such conntrack hasn't got any helper, abort */
1505 exp
= nf_ct_expect_alloc(ct
);
1511 exp
->expectfn
= NULL
;
1515 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1516 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1517 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1519 err
= nf_ct_expect_related(exp
);
1520 nf_ct_expect_put(exp
);
1523 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1528 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1529 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1531 struct nf_conntrack_tuple tuple
;
1532 struct nf_conntrack_expect
*exp
;
1533 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1534 u_int8_t u3
= nfmsg
->nfgen_family
;
1537 if (nlattr_bad_size(cda
, CTA_EXPECT_MAX
, cta_min_exp
))
1540 if (!cda
[CTA_EXPECT_TUPLE
]
1541 || !cda
[CTA_EXPECT_MASK
]
1542 || !cda
[CTA_EXPECT_MASTER
])
1545 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1549 write_lock_bh(&nf_conntrack_lock
);
1550 exp
= __nf_ct_expect_find(&tuple
);
1553 write_unlock_bh(&nf_conntrack_lock
);
1555 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1556 err
= ctnetlink_create_expect(cda
, u3
);
1561 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1562 err
= ctnetlink_change_expect(exp
, cda
);
1563 write_unlock_bh(&nf_conntrack_lock
);
1568 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1569 static struct notifier_block ctnl_notifier
= {
1570 .notifier_call
= ctnetlink_conntrack_event
,
1573 static struct notifier_block ctnl_notifier_exp
= {
1574 .notifier_call
= ctnetlink_expect_event
,
1578 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1579 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1580 .attr_count
= CTA_MAX
, },
1581 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1582 .attr_count
= CTA_MAX
, },
1583 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1584 .attr_count
= CTA_MAX
, },
1585 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1586 .attr_count
= CTA_MAX
, },
1589 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1590 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1591 .attr_count
= CTA_EXPECT_MAX
, },
1592 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1593 .attr_count
= CTA_EXPECT_MAX
, },
1594 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1595 .attr_count
= CTA_EXPECT_MAX
, },
1598 static const struct nfnetlink_subsystem ctnl_subsys
= {
1599 .name
= "conntrack",
1600 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1601 .cb_count
= IPCTNL_MSG_MAX
,
1605 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1606 .name
= "conntrack_expect",
1607 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1608 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1612 MODULE_ALIAS("ip_conntrack_netlink");
1613 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1614 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1616 static int __init
ctnetlink_init(void)
1620 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1621 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1623 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1627 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1629 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1630 goto err_unreg_subsys
;
1633 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1634 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1636 printk("ctnetlink_init: cannot register notifier.\n");
1637 goto err_unreg_exp_subsys
;
1640 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1642 printk("ctnetlink_init: cannot expect register notifier.\n");
1643 goto err_unreg_notifier
;
1649 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1651 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1652 err_unreg_exp_subsys
:
1653 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1656 nfnetlink_subsys_unregister(&ctnl_subsys
);
1661 static void __exit
ctnetlink_exit(void)
1663 printk("ctnetlink: unregistering from nfnetlink.\n");
1665 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1666 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1667 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1670 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1671 nfnetlink_subsys_unregister(&ctnl_subsys
);
1675 module_init(ctnetlink_init
);
1676 module_exit(ctnetlink_exit
);