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-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
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/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #include <net/netfilter/nf_conntrack_timestamp.h>
46 #ifdef CONFIG_NF_NAT_NEEDED
47 #include <net/netfilter/nf_nat_core.h>
48 #include <net/netfilter/nf_nat_protocol.h>
51 #include <linux/netfilter/nfnetlink.h>
52 #include <linux/netfilter/nfnetlink_conntrack.h>
54 MODULE_LICENSE("GPL");
56 static char __initdata version
[] = "0.93";
59 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
60 const struct nf_conntrack_tuple
*tuple
,
61 struct nf_conntrack_l4proto
*l4proto
)
64 struct nlattr
*nest_parms
;
66 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
69 if (nla_put_u8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
))
72 if (likely(l4proto
->tuple_to_nlattr
))
73 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
75 nla_nest_end(skb
, nest_parms
);
84 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
85 const struct nf_conntrack_tuple
*tuple
,
86 struct nf_conntrack_l3proto
*l3proto
)
89 struct nlattr
*nest_parms
;
91 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
95 if (likely(l3proto
->tuple_to_nlattr
))
96 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
98 nla_nest_end(skb
, nest_parms
);
107 ctnetlink_dump_tuples(struct sk_buff
*skb
,
108 const struct nf_conntrack_tuple
*tuple
)
111 struct nf_conntrack_l3proto
*l3proto
;
112 struct nf_conntrack_l4proto
*l4proto
;
115 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
116 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
119 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
120 tuple
->dst
.protonum
);
121 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
128 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
130 if (nla_put_be32(skb
, CTA_STATUS
, htonl(ct
->status
)))
131 goto nla_put_failure
;
139 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
141 long timeout
= ((long)ct
->timeout
.expires
- (long)jiffies
) / HZ
;
146 if (nla_put_be32(skb
, CTA_TIMEOUT
, htonl(timeout
)))
147 goto nla_put_failure
;
155 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
157 struct nf_conntrack_l4proto
*l4proto
;
158 struct nlattr
*nest_proto
;
161 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
162 if (!l4proto
->to_nlattr
)
165 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
167 goto nla_put_failure
;
169 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
171 nla_nest_end(skb
, nest_proto
);
180 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
182 struct nlattr
*nest_helper
;
183 const struct nf_conn_help
*help
= nfct_help(ct
);
184 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 if (nla_put_string(skb
, CTA_HELP_NAME
, helper
->name
))
197 goto nla_put_failure
;
199 if (helper
->to_nlattr
)
200 helper
->to_nlattr(skb
, ct
);
202 nla_nest_end(skb
, nest_helper
);
211 dump_counters(struct sk_buff
*skb
, u64 pkts
, u64 bytes
,
212 enum ip_conntrack_dir dir
)
214 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
215 struct nlattr
*nest_count
;
217 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
219 goto nla_put_failure
;
221 if (nla_put_be64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
)) ||
222 nla_put_be64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
)))
223 goto nla_put_failure
;
225 nla_nest_end(skb
, nest_count
);
234 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
235 enum ip_conntrack_dir dir
, int type
)
237 struct nf_conn_counter
*acct
;
240 acct
= nf_conn_acct_find(ct
);
244 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
245 pkts
= atomic64_xchg(&acct
[dir
].packets
, 0);
246 bytes
= atomic64_xchg(&acct
[dir
].bytes
, 0);
248 pkts
= atomic64_read(&acct
[dir
].packets
);
249 bytes
= atomic64_read(&acct
[dir
].bytes
);
251 return dump_counters(skb
, pkts
, bytes
, dir
);
255 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
257 struct nlattr
*nest_count
;
258 const struct nf_conn_tstamp
*tstamp
;
260 tstamp
= nf_conn_tstamp_find(ct
);
264 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
| NLA_F_NESTED
);
266 goto nla_put_failure
;
268 if (nla_put_be64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
)) ||
269 (tstamp
->stop
!= 0 && nla_put_be64(skb
, CTA_TIMESTAMP_STOP
,
270 cpu_to_be64(tstamp
->stop
))))
271 goto nla_put_failure
;
272 nla_nest_end(skb
, nest_count
);
280 #ifdef CONFIG_NF_CONNTRACK_MARK
282 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
284 if (nla_put_be32(skb
, CTA_MARK
, htonl(ct
->mark
)))
285 goto nla_put_failure
;
292 #define ctnetlink_dump_mark(a, b) (0)
295 #ifdef CONFIG_NF_CONNTRACK_SECMARK
297 ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
299 struct nlattr
*nest_secctx
;
303 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
308 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
310 goto nla_put_failure
;
312 if (nla_put_string(skb
, CTA_SECCTX_NAME
, secctx
))
313 goto nla_put_failure
;
314 nla_nest_end(skb
, nest_secctx
);
318 security_release_secctx(secctx
, len
);
322 #define ctnetlink_dump_secctx(a, b) (0)
325 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
328 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
330 struct nlattr
*nest_parms
;
332 if (!(ct
->status
& IPS_EXPECTED
))
335 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
337 goto nla_put_failure
;
338 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
339 goto nla_put_failure
;
340 nla_nest_end(skb
, nest_parms
);
348 #ifdef CONFIG_NF_NAT_NEEDED
350 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
352 struct nlattr
*nest_parms
;
354 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
356 goto nla_put_failure
;
358 if (nla_put_be32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
359 htonl(natseq
->correction_pos
)) ||
360 nla_put_be32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
361 htonl(natseq
->offset_before
)) ||
362 nla_put_be32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
363 htonl(natseq
->offset_after
)))
364 goto nla_put_failure
;
366 nla_nest_end(skb
, nest_parms
);
375 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
377 struct nf_nat_seq
*natseq
;
378 struct nf_conn_nat
*nat
= nfct_nat(ct
);
380 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
383 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
384 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
387 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
388 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
394 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
398 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
400 if (nla_put_be32(skb
, CTA_ID
, htonl((unsigned long)ct
)))
401 goto nla_put_failure
;
409 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
411 if (nla_put_be32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
))))
412 goto nla_put_failure
;
420 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 type
,
423 struct nlmsghdr
*nlh
;
424 struct nfgenmsg
*nfmsg
;
425 struct nlattr
*nest_parms
;
426 unsigned int flags
= pid
? NLM_F_MULTI
: 0, event
;
428 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_NEW
);
429 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
433 nfmsg
= nlmsg_data(nlh
);
434 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
435 nfmsg
->version
= NFNETLINK_V0
;
438 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
440 goto nla_put_failure
;
441 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
442 goto nla_put_failure
;
443 nla_nest_end(skb
, nest_parms
);
445 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
447 goto nla_put_failure
;
448 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
449 goto nla_put_failure
;
450 nla_nest_end(skb
, nest_parms
);
452 if (nf_ct_zone(ct
) &&
453 nla_put_be16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
))))
454 goto nla_put_failure
;
456 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
457 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
458 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
, type
) < 0 ||
459 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
, type
) < 0 ||
460 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
461 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
462 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
463 ctnetlink_dump_mark(skb
, ct
) < 0 ||
464 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
465 ctnetlink_dump_id(skb
, ct
) < 0 ||
466 ctnetlink_dump_use(skb
, ct
) < 0 ||
467 ctnetlink_dump_master(skb
, ct
) < 0 ||
468 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
469 goto nla_put_failure
;
476 nlmsg_cancel(skb
, nlh
);
480 #ifdef CONFIG_NF_CONNTRACK_EVENTS
482 ctnetlink_proto_size(const struct nf_conn
*ct
)
484 struct nf_conntrack_l3proto
*l3proto
;
485 struct nf_conntrack_l4proto
*l4proto
;
489 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
490 len
+= l3proto
->nla_size
;
492 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
493 len
+= l4proto
->nla_size
;
500 ctnetlink_counters_size(const struct nf_conn
*ct
)
502 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
504 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
505 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
506 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
511 ctnetlink_secctx_size(const struct nf_conn
*ct
)
513 #ifdef CONFIG_NF_CONNTRACK_SECMARK
516 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
520 return nla_total_size(0) /* CTA_SECCTX */
521 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
528 ctnetlink_timestamp_size(const struct nf_conn
*ct
)
530 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
531 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
533 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
540 ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
542 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
543 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
544 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
545 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
546 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
547 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
548 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
549 + ctnetlink_counters_size(ct
)
550 + ctnetlink_timestamp_size(ct
)
551 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
552 + nla_total_size(0) /* CTA_PROTOINFO */
553 + nla_total_size(0) /* CTA_HELP */
554 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
555 + ctnetlink_secctx_size(ct
)
556 #ifdef CONFIG_NF_NAT_NEEDED
557 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
558 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
560 #ifdef CONFIG_NF_CONNTRACK_MARK
561 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
563 + ctnetlink_proto_size(ct
)
568 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
571 struct nlmsghdr
*nlh
;
572 struct nfgenmsg
*nfmsg
;
573 struct nlattr
*nest_parms
;
574 struct nf_conn
*ct
= item
->ct
;
577 unsigned int flags
= 0, group
;
580 /* ignore our fake conntrack entry */
581 if (nf_ct_is_untracked(ct
))
584 if (events
& (1 << IPCT_DESTROY
)) {
585 type
= IPCTNL_MSG_CT_DELETE
;
586 group
= NFNLGRP_CONNTRACK_DESTROY
;
587 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
588 type
= IPCTNL_MSG_CT_NEW
;
589 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
590 group
= NFNLGRP_CONNTRACK_NEW
;
592 type
= IPCTNL_MSG_CT_NEW
;
593 group
= NFNLGRP_CONNTRACK_UPDATE
;
598 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
601 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
605 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
606 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
610 nfmsg
= nlmsg_data(nlh
);
611 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
612 nfmsg
->version
= NFNETLINK_V0
;
616 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
618 goto nla_put_failure
;
619 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
620 goto nla_put_failure
;
621 nla_nest_end(skb
, nest_parms
);
623 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
625 goto nla_put_failure
;
626 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
627 goto nla_put_failure
;
628 nla_nest_end(skb
, nest_parms
);
630 if (nf_ct_zone(ct
) &&
631 nla_put_be16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
))))
632 goto nla_put_failure
;
634 if (ctnetlink_dump_id(skb
, ct
) < 0)
635 goto nla_put_failure
;
637 if (ctnetlink_dump_status(skb
, ct
) < 0)
638 goto nla_put_failure
;
640 if (events
& (1 << IPCT_DESTROY
)) {
641 if (ctnetlink_dump_counters(skb
, ct
,
642 IP_CT_DIR_ORIGINAL
, type
) < 0 ||
643 ctnetlink_dump_counters(skb
, ct
,
644 IP_CT_DIR_REPLY
, type
) < 0 ||
645 ctnetlink_dump_timestamp(skb
, ct
) < 0)
646 goto nla_put_failure
;
648 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
649 goto nla_put_failure
;
651 if (events
& (1 << IPCT_PROTOINFO
)
652 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
653 goto nla_put_failure
;
655 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
656 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
657 goto nla_put_failure
;
659 #ifdef CONFIG_NF_CONNTRACK_SECMARK
660 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
661 && ctnetlink_dump_secctx(skb
, ct
) < 0)
662 goto nla_put_failure
;
665 if (events
& (1 << IPCT_RELATED
) &&
666 ctnetlink_dump_master(skb
, ct
) < 0)
667 goto nla_put_failure
;
669 if (events
& (1 << IPCT_NATSEQADJ
) &&
670 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
671 goto nla_put_failure
;
674 #ifdef CONFIG_NF_CONNTRACK_MARK
675 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
676 && ctnetlink_dump_mark(skb
, ct
) < 0)
677 goto nla_put_failure
;
682 err
= nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
,
684 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
691 nlmsg_cancel(skb
, nlh
);
695 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
700 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
702 static int ctnetlink_done(struct netlink_callback
*cb
)
705 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
711 struct ctnetlink_dump_filter
{
719 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
721 struct net
*net
= sock_net(skb
->sk
);
722 struct nf_conn
*ct
, *last
;
723 struct nf_conntrack_tuple_hash
*h
;
724 struct hlist_nulls_node
*n
;
725 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
726 u_int8_t l3proto
= nfmsg
->nfgen_family
;
728 #ifdef CONFIG_NF_CONNTRACK_MARK
729 const struct ctnetlink_dump_filter
*filter
= cb
->data
;
732 spin_lock_bh(&nf_conntrack_lock
);
733 last
= (struct nf_conn
*)cb
->args
[1];
734 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
736 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
738 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
740 ct
= nf_ct_tuplehash_to_ctrack(h
);
741 /* Dump entries of a given L3 protocol number.
742 * If it is not specified, ie. l3proto == 0,
743 * then dump everything. */
744 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
751 #ifdef CONFIG_NF_CONNTRACK_MARK
752 if (filter
&& !((ct
->mark
& filter
->mark
.mask
) ==
759 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
761 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
765 nf_conntrack_get(&ct
->ct_general
);
766 cb
->args
[1] = (unsigned long)ct
;
776 spin_unlock_bh(&nf_conntrack_lock
);
784 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
786 struct nlattr
*tb
[CTA_IP_MAX
+1];
787 struct nf_conntrack_l3proto
*l3proto
;
790 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
793 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
795 if (likely(l3proto
->nlattr_to_tuple
)) {
796 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
797 l3proto
->nla_policy
);
799 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
807 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
808 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
812 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
813 struct nf_conntrack_tuple
*tuple
)
815 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
816 struct nf_conntrack_l4proto
*l4proto
;
819 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
823 if (!tb
[CTA_PROTO_NUM
])
825 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
828 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
830 if (likely(l4proto
->nlattr_to_tuple
)) {
831 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
832 l4proto
->nla_policy
);
834 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
842 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
843 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
844 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
848 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
849 struct nf_conntrack_tuple
*tuple
,
850 enum ctattr_type type
, u_int8_t l3num
)
852 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
855 memset(tuple
, 0, sizeof(*tuple
));
857 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
859 if (!tb
[CTA_TUPLE_IP
])
862 tuple
->src
.l3num
= l3num
;
864 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
868 if (!tb
[CTA_TUPLE_PROTO
])
871 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
875 /* orig and expect tuples get DIR_ORIGINAL */
876 if (type
== CTA_TUPLE_REPLY
)
877 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
879 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
885 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
888 #ifdef CONFIG_NF_CONNTRACK_ZONES
889 *zone
= ntohs(nla_get_be16(attr
));
899 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
900 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
904 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
906 struct nlattr
*tb
[CTA_HELP_MAX
+1];
908 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
910 if (!tb
[CTA_HELP_NAME
])
913 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
918 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
919 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
920 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
921 [CTA_STATUS
] = { .type
= NLA_U32
},
922 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
923 [CTA_HELP
] = { .type
= NLA_NESTED
},
924 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
925 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
926 [CTA_MARK
] = { .type
= NLA_U32
},
927 [CTA_ID
] = { .type
= NLA_U32
},
928 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
929 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
930 [CTA_ZONE
] = { .type
= NLA_U16
},
931 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
935 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
936 const struct nlmsghdr
*nlh
,
937 const struct nlattr
* const cda
[])
939 struct net
*net
= sock_net(ctnl
);
940 struct nf_conntrack_tuple_hash
*h
;
941 struct nf_conntrack_tuple tuple
;
943 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
944 u_int8_t u3
= nfmsg
->nfgen_family
;
948 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
952 if (cda
[CTA_TUPLE_ORIG
])
953 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
954 else if (cda
[CTA_TUPLE_REPLY
])
955 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
957 /* Flush the whole table */
958 nf_conntrack_flush_report(net
,
967 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
971 ct
= nf_ct_tuplehash_to_ctrack(h
);
974 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
975 if (id
!= (u32
)(unsigned long)ct
) {
981 if (del_timer(&ct
->timeout
)) {
982 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
984 nlmsg_report(nlh
)) < 0) {
985 nf_ct_delete_from_lists(ct
);
986 /* we failed to report the event, try later */
987 nf_ct_insert_dying_list(ct
);
991 /* death_by_timeout would report the event again */
992 set_bit(IPS_DYING_BIT
, &ct
->status
);
993 nf_ct_delete_from_lists(ct
);
1002 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1003 const struct nlmsghdr
*nlh
,
1004 const struct nlattr
* const cda
[])
1006 struct net
*net
= sock_net(ctnl
);
1007 struct nf_conntrack_tuple_hash
*h
;
1008 struct nf_conntrack_tuple tuple
;
1010 struct sk_buff
*skb2
= NULL
;
1011 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1012 u_int8_t u3
= nfmsg
->nfgen_family
;
1016 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1017 struct netlink_dump_control c
= {
1018 .dump
= ctnetlink_dump_table
,
1019 .done
= ctnetlink_done
,
1021 #ifdef CONFIG_NF_CONNTRACK_MARK
1022 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1023 struct ctnetlink_dump_filter
*filter
;
1025 filter
= kzalloc(sizeof(struct ctnetlink_dump_filter
),
1030 filter
->mark
.val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1032 ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
1036 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1039 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1043 if (cda
[CTA_TUPLE_ORIG
])
1044 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
1045 else if (cda
[CTA_TUPLE_REPLY
])
1046 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
1053 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
1057 ct
= nf_ct_tuplehash_to_ctrack(h
);
1060 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1067 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
1068 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1074 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1083 /* this avoids a loop in nfnetlink. */
1084 return err
== -EAGAIN
? -ENOBUFS
: err
;
1087 #ifdef CONFIG_NF_NAT_NEEDED
1089 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1090 enum nf_nat_manip_type manip
,
1091 const struct nlattr
*attr
)
1093 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1095 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1096 if (!parse_nat_setup
) {
1097 #ifdef CONFIG_MODULES
1100 if (request_module("nf-nat-ipv4") < 0) {
1107 if (nfnetlink_parse_nat_setup_hook
)
1113 return parse_nat_setup(ct
, manip
, attr
);
1118 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1121 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1122 d
= ct
->status
^ status
;
1124 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1128 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1129 /* SEEN_REPLY bit can only be set */
1132 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1133 /* ASSURED bit can only be set */
1136 /* Be careful here, modifying NAT bits can screw up things,
1137 * so don't let users modify them directly if they don't pass
1139 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1144 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1146 #ifdef CONFIG_NF_NAT_NEEDED
1149 if (cda
[CTA_NAT_DST
]) {
1150 ret
= ctnetlink_parse_nat_setup(ct
,
1156 if (cda
[CTA_NAT_SRC
]) {
1157 ret
= ctnetlink_parse_nat_setup(ct
,
1170 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1172 struct nf_conntrack_helper
*helper
;
1173 struct nf_conn_help
*help
= nfct_help(ct
);
1174 char *helpname
= NULL
;
1177 /* don't change helper of sibling connections */
1181 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1185 if (!strcmp(helpname
, "")) {
1186 if (help
&& help
->helper
) {
1187 /* we had a helper before ... */
1188 nf_ct_remove_expectations(ct
);
1189 RCU_INIT_POINTER(help
->helper
, NULL
);
1195 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1196 nf_ct_protonum(ct
));
1197 if (helper
== NULL
) {
1198 #ifdef CONFIG_MODULES
1199 spin_unlock_bh(&nf_conntrack_lock
);
1201 if (request_module("nfct-helper-%s", helpname
) < 0) {
1202 spin_lock_bh(&nf_conntrack_lock
);
1206 spin_lock_bh(&nf_conntrack_lock
);
1207 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1208 nf_ct_protonum(ct
));
1216 if (help
->helper
== helper
)
1220 /* need to zero data of old helper */
1221 memset(&help
->help
, 0, sizeof(help
->help
));
1223 /* we cannot set a helper for an existing conntrack */
1227 rcu_assign_pointer(help
->helper
, helper
);
1233 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1235 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1237 if (!del_timer(&ct
->timeout
))
1240 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1241 add_timer(&ct
->timeout
);
1246 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1247 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1248 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1249 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1253 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1255 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1256 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1257 struct nf_conntrack_l4proto
*l4proto
;
1260 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1263 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1264 if (l4proto
->from_nlattr
)
1265 err
= l4proto
->from_nlattr(tb
, ct
);
1271 #ifdef CONFIG_NF_NAT_NEEDED
1272 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1273 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1274 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1275 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1279 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1281 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1283 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1285 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1288 natseq
->correction_pos
=
1289 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1291 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1294 natseq
->offset_before
=
1295 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1297 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1300 natseq
->offset_after
=
1301 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1307 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1308 const struct nlattr
* const cda
[])
1311 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1316 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1317 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1318 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1322 ct
->status
|= IPS_SEQ_ADJUST
;
1325 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1326 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1327 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1331 ct
->status
|= IPS_SEQ_ADJUST
;
1339 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1340 const struct nlattr
* const cda
[])
1344 /* only allow NAT changes and master assignation for new conntracks */
1345 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1348 if (cda
[CTA_HELP
]) {
1349 err
= ctnetlink_change_helper(ct
, cda
);
1354 if (cda
[CTA_TIMEOUT
]) {
1355 err
= ctnetlink_change_timeout(ct
, cda
);
1360 if (cda
[CTA_STATUS
]) {
1361 err
= ctnetlink_change_status(ct
, cda
);
1366 if (cda
[CTA_PROTOINFO
]) {
1367 err
= ctnetlink_change_protoinfo(ct
, cda
);
1372 #if defined(CONFIG_NF_CONNTRACK_MARK)
1374 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1377 #ifdef CONFIG_NF_NAT_NEEDED
1378 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1379 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1388 static struct nf_conn
*
1389 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1390 const struct nlattr
* const cda
[],
1391 struct nf_conntrack_tuple
*otuple
,
1392 struct nf_conntrack_tuple
*rtuple
,
1397 struct nf_conntrack_helper
*helper
;
1398 struct nf_conn_tstamp
*tstamp
;
1400 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1402 return ERR_PTR(-ENOMEM
);
1404 if (!cda
[CTA_TIMEOUT
])
1406 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1408 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1411 if (cda
[CTA_HELP
]) {
1412 char *helpname
= NULL
;
1414 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1418 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1419 nf_ct_protonum(ct
));
1420 if (helper
== NULL
) {
1422 #ifdef CONFIG_MODULES
1423 if (request_module("nfct-helper-%s", helpname
) < 0) {
1429 helper
= __nf_conntrack_helper_find(helpname
,
1431 nf_ct_protonum(ct
));
1441 struct nf_conn_help
*help
;
1443 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1449 /* not in hash table yet so not strictly necessary */
1450 RCU_INIT_POINTER(help
->helper
, helper
);
1453 /* try an implicit helper assignation */
1454 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1459 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1460 err
= ctnetlink_change_nat(ct
, cda
);
1465 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1466 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1467 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1468 /* we must add conntrack extensions before confirmation. */
1469 ct
->status
|= IPS_CONFIRMED
;
1471 if (cda
[CTA_STATUS
]) {
1472 err
= ctnetlink_change_status(ct
, cda
);
1477 #ifdef CONFIG_NF_NAT_NEEDED
1478 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1479 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1485 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1486 if (cda
[CTA_PROTOINFO
]) {
1487 err
= ctnetlink_change_protoinfo(ct
, cda
);
1492 #if defined(CONFIG_NF_CONNTRACK_MARK)
1494 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1497 /* setup master conntrack: this is a confirmed expectation */
1498 if (cda
[CTA_TUPLE_MASTER
]) {
1499 struct nf_conntrack_tuple master
;
1500 struct nf_conntrack_tuple_hash
*master_h
;
1501 struct nf_conn
*master_ct
;
1503 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1507 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1508 if (master_h
== NULL
) {
1512 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1513 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1514 ct
->master
= master_ct
;
1516 tstamp
= nf_conn_tstamp_find(ct
);
1518 tstamp
->start
= ktime_to_ns(ktime_get_real());
1520 err
= nf_conntrack_hash_check_insert(ct
);
1531 nf_conntrack_free(ct
);
1532 return ERR_PTR(err
);
1536 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1537 const struct nlmsghdr
*nlh
,
1538 const struct nlattr
* const cda
[])
1540 struct net
*net
= sock_net(ctnl
);
1541 struct nf_conntrack_tuple otuple
, rtuple
;
1542 struct nf_conntrack_tuple_hash
*h
= NULL
;
1543 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1545 u_int8_t u3
= nfmsg
->nfgen_family
;
1549 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1553 if (cda
[CTA_TUPLE_ORIG
]) {
1554 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1559 if (cda
[CTA_TUPLE_REPLY
]) {
1560 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1565 if (cda
[CTA_TUPLE_ORIG
])
1566 h
= nf_conntrack_find_get(net
, zone
, &otuple
);
1567 else if (cda
[CTA_TUPLE_REPLY
])
1568 h
= nf_conntrack_find_get(net
, zone
, &rtuple
);
1572 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1573 enum ip_conntrack_events events
;
1575 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1581 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1582 events
= IPCT_RELATED
;
1586 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1587 (1 << IPCT_ASSURED
) |
1588 (1 << IPCT_HELPER
) |
1589 (1 << IPCT_PROTOINFO
) |
1590 (1 << IPCT_NATSEQADJ
) |
1591 (1 << IPCT_MARK
) | events
,
1592 ct
, NETLINK_CB(skb
).pid
,
1599 /* implicit 'else' */
1602 ct
= nf_ct_tuplehash_to_ctrack(h
);
1603 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1604 spin_lock_bh(&nf_conntrack_lock
);
1605 err
= ctnetlink_change_conntrack(ct
, cda
);
1606 spin_unlock_bh(&nf_conntrack_lock
);
1608 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1609 (1 << IPCT_ASSURED
) |
1610 (1 << IPCT_HELPER
) |
1611 (1 << IPCT_PROTOINFO
) |
1612 (1 << IPCT_NATSEQADJ
) |
1614 ct
, NETLINK_CB(skb
).pid
,
1623 /***********************************************************************
1625 ***********************************************************************/
1628 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1629 const struct nf_conntrack_tuple
*tuple
,
1630 enum ctattr_expect type
)
1632 struct nlattr
*nest_parms
;
1634 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1636 goto nla_put_failure
;
1637 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1638 goto nla_put_failure
;
1639 nla_nest_end(skb
, nest_parms
);
1648 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1649 const struct nf_conntrack_tuple
*tuple
,
1650 const struct nf_conntrack_tuple_mask
*mask
)
1653 struct nf_conntrack_l3proto
*l3proto
;
1654 struct nf_conntrack_l4proto
*l4proto
;
1655 struct nf_conntrack_tuple m
;
1656 struct nlattr
*nest_parms
;
1658 memset(&m
, 0xFF, sizeof(m
));
1659 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1660 m
.src
.u
.all
= mask
->src
.u
.all
;
1661 m
.dst
.protonum
= tuple
->dst
.protonum
;
1663 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1665 goto nla_put_failure
;
1668 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1669 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1671 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
1672 tuple
->dst
.protonum
);
1673 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1677 if (unlikely(ret
< 0))
1678 goto nla_put_failure
;
1680 nla_nest_end(skb
, nest_parms
);
1689 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1690 const struct nf_conntrack_expect
*exp
)
1692 struct nf_conn
*master
= exp
->master
;
1693 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
1694 struct nf_conn_help
*help
;
1695 #ifdef CONFIG_NF_NAT_NEEDED
1696 struct nlattr
*nest_parms
;
1697 struct nf_conntrack_tuple nat_tuple
= {};
1699 struct nf_ct_helper_expectfn
*expfn
;
1704 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1705 goto nla_put_failure
;
1706 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1707 goto nla_put_failure
;
1708 if (ctnetlink_exp_dump_tuple(skb
,
1709 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1710 CTA_EXPECT_MASTER
) < 0)
1711 goto nla_put_failure
;
1713 #ifdef CONFIG_NF_NAT_NEEDED
1714 if (exp
->saved_ip
|| exp
->saved_proto
.all
) {
1715 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
1717 goto nla_put_failure
;
1719 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
1720 goto nla_put_failure
;
1722 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
1723 nat_tuple
.src
.u3
.ip
= exp
->saved_ip
;
1724 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
1725 nat_tuple
.src
.u
= exp
->saved_proto
;
1727 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
1728 CTA_EXPECT_NAT_TUPLE
) < 0)
1729 goto nla_put_failure
;
1730 nla_nest_end(skb
, nest_parms
);
1733 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
1734 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
1735 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
1736 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
1737 goto nla_put_failure
;
1738 help
= nfct_help(master
);
1740 struct nf_conntrack_helper
*helper
;
1742 helper
= rcu_dereference(help
->helper
);
1744 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
1745 goto nla_put_failure
;
1747 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
1748 if (expfn
!= NULL
&&
1749 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
1750 goto nla_put_failure
;
1759 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1760 int event
, const struct nf_conntrack_expect
*exp
)
1762 struct nlmsghdr
*nlh
;
1763 struct nfgenmsg
*nfmsg
;
1764 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1766 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1767 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1771 nfmsg
= nlmsg_data(nlh
);
1772 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1773 nfmsg
->version
= NFNETLINK_V0
;
1776 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1777 goto nla_put_failure
;
1779 nlmsg_end(skb
, nlh
);
1784 nlmsg_cancel(skb
, nlh
);
1788 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1790 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1792 struct nf_conntrack_expect
*exp
= item
->exp
;
1793 struct net
*net
= nf_ct_exp_net(exp
);
1794 struct nlmsghdr
*nlh
;
1795 struct nfgenmsg
*nfmsg
;
1796 struct sk_buff
*skb
;
1797 unsigned int type
, group
;
1800 if (events
& (1 << IPEXP_DESTROY
)) {
1801 type
= IPCTNL_MSG_EXP_DELETE
;
1802 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
1803 } else if (events
& (1 << IPEXP_NEW
)) {
1804 type
= IPCTNL_MSG_EXP_NEW
;
1805 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1806 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
1810 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
1813 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1817 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1818 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1822 nfmsg
= nlmsg_data(nlh
);
1823 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1824 nfmsg
->version
= NFNETLINK_V0
;
1828 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1829 goto nla_put_failure
;
1832 nlmsg_end(skb
, nlh
);
1833 nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
, GFP_ATOMIC
);
1838 nlmsg_cancel(skb
, nlh
);
1842 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1846 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1849 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1854 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1856 struct net
*net
= sock_net(skb
->sk
);
1857 struct nf_conntrack_expect
*exp
, *last
;
1858 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1859 struct hlist_node
*n
;
1860 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1863 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1864 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1866 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1868 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1875 if (ctnetlink_exp_fill_info(skb
,
1876 NETLINK_CB(cb
->skb
).pid
,
1880 if (!atomic_inc_not_zero(&exp
->use
))
1882 cb
->args
[1] = (unsigned long)exp
;
1894 nf_ct_expect_put(last
);
1899 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1900 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1901 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1902 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1903 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1904 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1905 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1906 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
1907 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
1908 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
1909 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
1910 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
1914 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1915 const struct nlmsghdr
*nlh
,
1916 const struct nlattr
* const cda
[])
1918 struct net
*net
= sock_net(ctnl
);
1919 struct nf_conntrack_tuple tuple
;
1920 struct nf_conntrack_expect
*exp
;
1921 struct sk_buff
*skb2
;
1922 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1923 u_int8_t u3
= nfmsg
->nfgen_family
;
1927 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1928 struct netlink_dump_control c
= {
1929 .dump
= ctnetlink_exp_dump_table
,
1930 .done
= ctnetlink_exp_done
,
1932 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1935 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1939 if (cda
[CTA_EXPECT_TUPLE
])
1940 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1941 else if (cda
[CTA_EXPECT_MASTER
])
1942 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1949 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1953 if (cda
[CTA_EXPECT_ID
]) {
1954 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1955 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1956 nf_ct_expect_put(exp
);
1962 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1964 nf_ct_expect_put(exp
);
1969 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1970 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1972 nf_ct_expect_put(exp
);
1976 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1985 /* this avoids a loop in nfnetlink. */
1986 return err
== -EAGAIN
? -ENOBUFS
: err
;
1990 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1991 const struct nlmsghdr
*nlh
,
1992 const struct nlattr
* const cda
[])
1994 struct net
*net
= sock_net(ctnl
);
1995 struct nf_conntrack_expect
*exp
;
1996 struct nf_conntrack_tuple tuple
;
1997 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1998 struct hlist_node
*n
, *next
;
1999 u_int8_t u3
= nfmsg
->nfgen_family
;
2004 if (cda
[CTA_EXPECT_TUPLE
]) {
2005 /* delete a single expect by tuple */
2006 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2010 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2014 /* bump usage count to 2 */
2015 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
2019 if (cda
[CTA_EXPECT_ID
]) {
2020 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2021 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2022 nf_ct_expect_put(exp
);
2027 /* after list removal, usage count == 1 */
2028 spin_lock_bh(&nf_conntrack_lock
);
2029 if (del_timer(&exp
->timeout
)) {
2030 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).pid
,
2032 nf_ct_expect_put(exp
);
2034 spin_unlock_bh(&nf_conntrack_lock
);
2035 /* have to put what we 'get' above.
2036 * after this line usage count == 0 */
2037 nf_ct_expect_put(exp
);
2038 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2039 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2040 struct nf_conn_help
*m_help
;
2042 /* delete all expectations for this helper */
2043 spin_lock_bh(&nf_conntrack_lock
);
2044 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2045 hlist_for_each_entry_safe(exp
, n
, next
,
2046 &net
->ct
.expect_hash
[i
],
2048 m_help
= nfct_help(exp
->master
);
2049 if (!strcmp(m_help
->helper
->name
, name
) &&
2050 del_timer(&exp
->timeout
)) {
2051 nf_ct_unlink_expect_report(exp
,
2052 NETLINK_CB(skb
).pid
,
2054 nf_ct_expect_put(exp
);
2058 spin_unlock_bh(&nf_conntrack_lock
);
2060 /* This basically means we have to flush everything*/
2061 spin_lock_bh(&nf_conntrack_lock
);
2062 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2063 hlist_for_each_entry_safe(exp
, n
, next
,
2064 &net
->ct
.expect_hash
[i
],
2066 if (del_timer(&exp
->timeout
)) {
2067 nf_ct_unlink_expect_report(exp
,
2068 NETLINK_CB(skb
).pid
,
2070 nf_ct_expect_put(exp
);
2074 spin_unlock_bh(&nf_conntrack_lock
);
2080 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2081 const struct nlattr
* const cda
[])
2083 if (cda
[CTA_EXPECT_TIMEOUT
]) {
2084 if (!del_timer(&x
->timeout
))
2087 x
->timeout
.expires
= jiffies
+
2088 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2089 add_timer(&x
->timeout
);
2094 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2095 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2096 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2100 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2101 struct nf_conntrack_expect
*exp
,
2104 #ifdef CONFIG_NF_NAT_NEEDED
2105 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2106 struct nf_conntrack_tuple nat_tuple
= {};
2109 nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2111 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2114 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2115 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
, u3
);
2119 exp
->saved_ip
= nat_tuple
.src
.u3
.ip
;
2120 exp
->saved_proto
= nat_tuple
.src
.u
;
2121 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2130 ctnetlink_create_expect(struct net
*net
, u16 zone
,
2131 const struct nlattr
* const cda
[],
2133 u32 pid
, int report
)
2135 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
2136 struct nf_conntrack_tuple_hash
*h
= NULL
;
2137 struct nf_conntrack_expect
*exp
;
2139 struct nf_conn_help
*help
;
2140 struct nf_conntrack_helper
*helper
= NULL
;
2141 u_int32_t
class = 0;
2144 /* caller guarantees that those three CTA_EXPECT_* exist */
2145 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2148 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
2151 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
2155 /* Look for master conntrack of this expectation */
2156 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
2159 ct
= nf_ct_tuplehash_to_ctrack(h
);
2161 /* Look for helper of this expectation */
2162 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2163 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2165 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2166 nf_ct_protonum(ct
));
2167 if (helper
== NULL
) {
2168 #ifdef CONFIG_MODULES
2169 if (request_module("nfct-helper-%s", helpname
) < 0) {
2174 helper
= __nf_conntrack_helper_find(helpname
,
2176 nf_ct_protonum(ct
));
2187 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2188 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
2189 if (class > helper
->expect_class_max
) {
2194 exp
= nf_ct_expect_alloc(ct
);
2199 help
= nfct_help(ct
);
2201 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
2205 exp
->timeout
.expires
=
2206 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2208 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
2209 if (cda
[CTA_EXPECT_FLAGS
]) {
2211 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2214 if (cda
[CTA_EXPECT_FLAGS
]) {
2215 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2216 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
2220 if (cda
[CTA_EXPECT_FN
]) {
2221 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
2222 struct nf_ct_helper_expectfn
*expfn
;
2224 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
2225 if (expfn
== NULL
) {
2229 exp
->expectfn
= expfn
->expectfn
;
2231 exp
->expectfn
= NULL
;
2235 exp
->helper
= helper
;
2236 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2237 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2238 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2240 if (cda
[CTA_EXPECT_NAT
]) {
2241 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
2246 err
= nf_ct_expect_related_report(exp
, pid
, report
);
2248 nf_ct_expect_put(exp
);
2250 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2255 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2256 const struct nlmsghdr
*nlh
,
2257 const struct nlattr
* const cda
[])
2259 struct net
*net
= sock_net(ctnl
);
2260 struct nf_conntrack_tuple tuple
;
2261 struct nf_conntrack_expect
*exp
;
2262 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2263 u_int8_t u3
= nfmsg
->nfgen_family
;
2267 if (!cda
[CTA_EXPECT_TUPLE
]
2268 || !cda
[CTA_EXPECT_MASK
]
2269 || !cda
[CTA_EXPECT_MASTER
])
2272 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2276 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2280 spin_lock_bh(&nf_conntrack_lock
);
2281 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2284 spin_unlock_bh(&nf_conntrack_lock
);
2286 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2287 err
= ctnetlink_create_expect(net
, zone
, cda
,
2289 NETLINK_CB(skb
).pid
,
2296 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2297 err
= ctnetlink_change_expect(exp
, cda
);
2298 spin_unlock_bh(&nf_conntrack_lock
);
2303 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2304 static struct nf_ct_event_notifier ctnl_notifier
= {
2305 .fcn
= ctnetlink_conntrack_event
,
2308 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2309 .fcn
= ctnetlink_expect_event
,
2313 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2314 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2315 .attr_count
= CTA_MAX
,
2316 .policy
= ct_nla_policy
},
2317 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2318 .attr_count
= CTA_MAX
,
2319 .policy
= ct_nla_policy
},
2320 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2321 .attr_count
= CTA_MAX
,
2322 .policy
= ct_nla_policy
},
2323 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2324 .attr_count
= CTA_MAX
,
2325 .policy
= ct_nla_policy
},
2328 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2329 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2330 .attr_count
= CTA_EXPECT_MAX
,
2331 .policy
= exp_nla_policy
},
2332 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2333 .attr_count
= CTA_EXPECT_MAX
,
2334 .policy
= exp_nla_policy
},
2335 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2336 .attr_count
= CTA_EXPECT_MAX
,
2337 .policy
= exp_nla_policy
},
2340 static const struct nfnetlink_subsystem ctnl_subsys
= {
2341 .name
= "conntrack",
2342 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2343 .cb_count
= IPCTNL_MSG_MAX
,
2347 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2348 .name
= "conntrack_expect",
2349 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2350 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2354 MODULE_ALIAS("ip_conntrack_netlink");
2355 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2356 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2358 static int __net_init
ctnetlink_net_init(struct net
*net
)
2360 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2363 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
2365 pr_err("ctnetlink_init: cannot register notifier.\n");
2369 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
2371 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2372 goto err_unreg_notifier
;
2377 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2379 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
2385 static void ctnetlink_net_exit(struct net
*net
)
2387 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2388 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
2389 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
2393 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
2397 list_for_each_entry(net
, net_exit_list
, exit_list
)
2398 ctnetlink_net_exit(net
);
2401 static struct pernet_operations ctnetlink_net_ops
= {
2402 .init
= ctnetlink_net_init
,
2403 .exit_batch
= ctnetlink_net_exit_batch
,
2406 static int __init
ctnetlink_init(void)
2410 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2411 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2413 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2417 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2419 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2420 goto err_unreg_subsys
;
2423 if (register_pernet_subsys(&ctnetlink_net_ops
)) {
2424 pr_err("ctnetlink_init: cannot register pernet operations\n");
2425 goto err_unreg_exp_subsys
;
2430 err_unreg_exp_subsys
:
2431 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2433 nfnetlink_subsys_unregister(&ctnl_subsys
);
2438 static void __exit
ctnetlink_exit(void)
2440 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2442 unregister_pernet_subsys(&ctnetlink_net_ops
);
2443 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2444 nfnetlink_subsys_unregister(&ctnl_subsys
);
2447 module_init(ctnetlink_init
);
2448 module_exit(ctnetlink_exit
);