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-2012 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_l4proto.h>
49 #include <net/netfilter/nf_nat_helper.h>
52 #include <linux/netfilter/nfnetlink.h>
53 #include <linux/netfilter/nfnetlink_conntrack.h>
55 MODULE_LICENSE("GPL");
57 static char __initdata version
[] = "0.93";
60 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
61 const struct nf_conntrack_tuple
*tuple
,
62 struct nf_conntrack_l4proto
*l4proto
)
65 struct nlattr
*nest_parms
;
67 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
70 if (nla_put_u8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
))
73 if (likely(l4proto
->tuple_to_nlattr
))
74 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
76 nla_nest_end(skb
, nest_parms
);
85 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
86 const struct nf_conntrack_tuple
*tuple
,
87 struct nf_conntrack_l3proto
*l3proto
)
90 struct nlattr
*nest_parms
;
92 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
96 if (likely(l3proto
->tuple_to_nlattr
))
97 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
99 nla_nest_end(skb
, nest_parms
);
108 ctnetlink_dump_tuples(struct sk_buff
*skb
,
109 const struct nf_conntrack_tuple
*tuple
)
112 struct nf_conntrack_l3proto
*l3proto
;
113 struct nf_conntrack_l4proto
*l4proto
;
116 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
117 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
120 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
121 tuple
->dst
.protonum
);
122 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
129 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
131 if (nla_put_be32(skb
, CTA_STATUS
, htonl(ct
->status
)))
132 goto nla_put_failure
;
140 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
142 long timeout
= ((long)ct
->timeout
.expires
- (long)jiffies
) / HZ
;
147 if (nla_put_be32(skb
, CTA_TIMEOUT
, htonl(timeout
)))
148 goto nla_put_failure
;
156 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
158 struct nf_conntrack_l4proto
*l4proto
;
159 struct nlattr
*nest_proto
;
162 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
163 if (!l4proto
->to_nlattr
)
166 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
168 goto nla_put_failure
;
170 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
172 nla_nest_end(skb
, nest_proto
);
181 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
183 struct nlattr
*nest_helper
;
184 const struct nf_conn_help
*help
= nfct_help(ct
);
185 struct nf_conntrack_helper
*helper
;
190 helper
= rcu_dereference(help
->helper
);
194 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
196 goto nla_put_failure
;
197 if (nla_put_string(skb
, CTA_HELP_NAME
, helper
->name
))
198 goto nla_put_failure
;
200 if (helper
->to_nlattr
)
201 helper
->to_nlattr(skb
, ct
);
203 nla_nest_end(skb
, nest_helper
);
212 dump_counters(struct sk_buff
*skb
, u64 pkts
, u64 bytes
,
213 enum ip_conntrack_dir dir
)
215 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
216 struct nlattr
*nest_count
;
218 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
220 goto nla_put_failure
;
222 if (nla_put_be64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
)) ||
223 nla_put_be64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
)))
224 goto nla_put_failure
;
226 nla_nest_end(skb
, nest_count
);
235 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
236 enum ip_conntrack_dir dir
, int type
)
238 struct nf_conn_counter
*acct
;
241 acct
= nf_conn_acct_find(ct
);
245 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
246 pkts
= atomic64_xchg(&acct
[dir
].packets
, 0);
247 bytes
= atomic64_xchg(&acct
[dir
].bytes
, 0);
249 pkts
= atomic64_read(&acct
[dir
].packets
);
250 bytes
= atomic64_read(&acct
[dir
].bytes
);
252 return dump_counters(skb
, pkts
, bytes
, dir
);
256 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
258 struct nlattr
*nest_count
;
259 const struct nf_conn_tstamp
*tstamp
;
261 tstamp
= nf_conn_tstamp_find(ct
);
265 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
| NLA_F_NESTED
);
267 goto nla_put_failure
;
269 if (nla_put_be64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
)) ||
270 (tstamp
->stop
!= 0 && nla_put_be64(skb
, CTA_TIMESTAMP_STOP
,
271 cpu_to_be64(tstamp
->stop
))))
272 goto nla_put_failure
;
273 nla_nest_end(skb
, nest_count
);
281 #ifdef CONFIG_NF_CONNTRACK_MARK
283 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
285 if (nla_put_be32(skb
, CTA_MARK
, htonl(ct
->mark
)))
286 goto nla_put_failure
;
293 #define ctnetlink_dump_mark(a, b) (0)
296 #ifdef CONFIG_NF_CONNTRACK_SECMARK
298 ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
300 struct nlattr
*nest_secctx
;
304 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
309 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
311 goto nla_put_failure
;
313 if (nla_put_string(skb
, CTA_SECCTX_NAME
, secctx
))
314 goto nla_put_failure
;
315 nla_nest_end(skb
, nest_secctx
);
319 security_release_secctx(secctx
, len
);
323 #define ctnetlink_dump_secctx(a, b) (0)
326 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
329 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
331 struct nlattr
*nest_parms
;
333 if (!(ct
->status
& IPS_EXPECTED
))
336 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
338 goto nla_put_failure
;
339 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
340 goto nla_put_failure
;
341 nla_nest_end(skb
, nest_parms
);
349 #ifdef CONFIG_NF_NAT_NEEDED
351 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
353 struct nlattr
*nest_parms
;
355 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
357 goto nla_put_failure
;
359 if (nla_put_be32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
360 htonl(natseq
->correction_pos
)) ||
361 nla_put_be32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
362 htonl(natseq
->offset_before
)) ||
363 nla_put_be32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
364 htonl(natseq
->offset_after
)))
365 goto nla_put_failure
;
367 nla_nest_end(skb
, nest_parms
);
376 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
378 struct nf_nat_seq
*natseq
;
379 struct nf_conn_nat
*nat
= nfct_nat(ct
);
381 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
384 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
385 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
388 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
389 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
395 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
399 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
401 if (nla_put_be32(skb
, CTA_ID
, htonl((unsigned long)ct
)))
402 goto nla_put_failure
;
410 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
412 if (nla_put_be32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
))))
413 goto nla_put_failure
;
421 ctnetlink_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
424 struct nlmsghdr
*nlh
;
425 struct nfgenmsg
*nfmsg
;
426 struct nlattr
*nest_parms
;
427 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
429 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_NEW
);
430 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
434 nfmsg
= nlmsg_data(nlh
);
435 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
436 nfmsg
->version
= NFNETLINK_V0
;
439 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
441 goto nla_put_failure
;
442 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
443 goto nla_put_failure
;
444 nla_nest_end(skb
, nest_parms
);
446 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
448 goto nla_put_failure
;
449 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
450 goto nla_put_failure
;
451 nla_nest_end(skb
, nest_parms
);
453 if (nf_ct_zone(ct
) &&
454 nla_put_be16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
))))
455 goto nla_put_failure
;
457 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
458 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
459 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
, type
) < 0 ||
460 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
, type
) < 0 ||
461 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
462 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
463 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
464 ctnetlink_dump_mark(skb
, ct
) < 0 ||
465 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
466 ctnetlink_dump_id(skb
, ct
) < 0 ||
467 ctnetlink_dump_use(skb
, ct
) < 0 ||
468 ctnetlink_dump_master(skb
, ct
) < 0 ||
469 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
470 goto nla_put_failure
;
477 nlmsg_cancel(skb
, nlh
);
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
)
567 #ifdef CONFIG_NF_CONNTRACK_EVENTS
569 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
572 struct nlmsghdr
*nlh
;
573 struct nfgenmsg
*nfmsg
;
574 struct nlattr
*nest_parms
;
575 struct nf_conn
*ct
= item
->ct
;
578 unsigned int flags
= 0, group
;
581 /* ignore our fake conntrack entry */
582 if (nf_ct_is_untracked(ct
))
585 if (events
& (1 << IPCT_DESTROY
)) {
586 type
= IPCTNL_MSG_CT_DELETE
;
587 group
= NFNLGRP_CONNTRACK_DESTROY
;
588 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
589 type
= IPCTNL_MSG_CT_NEW
;
590 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
591 group
= NFNLGRP_CONNTRACK_NEW
;
593 type
= IPCTNL_MSG_CT_NEW
;
594 group
= NFNLGRP_CONNTRACK_UPDATE
;
599 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
602 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
606 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
607 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
611 nfmsg
= nlmsg_data(nlh
);
612 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
613 nfmsg
->version
= NFNETLINK_V0
;
617 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
619 goto nla_put_failure
;
620 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
621 goto nla_put_failure
;
622 nla_nest_end(skb
, nest_parms
);
624 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
626 goto nla_put_failure
;
627 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
628 goto nla_put_failure
;
629 nla_nest_end(skb
, nest_parms
);
631 if (nf_ct_zone(ct
) &&
632 nla_put_be16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
))))
633 goto nla_put_failure
;
635 if (ctnetlink_dump_id(skb
, ct
) < 0)
636 goto nla_put_failure
;
638 if (ctnetlink_dump_status(skb
, ct
) < 0)
639 goto nla_put_failure
;
641 if (events
& (1 << IPCT_DESTROY
)) {
642 if (ctnetlink_dump_counters(skb
, ct
,
643 IP_CT_DIR_ORIGINAL
, type
) < 0 ||
644 ctnetlink_dump_counters(skb
, ct
,
645 IP_CT_DIR_REPLY
, type
) < 0 ||
646 ctnetlink_dump_timestamp(skb
, ct
) < 0)
647 goto nla_put_failure
;
649 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
650 goto nla_put_failure
;
652 if (events
& (1 << IPCT_PROTOINFO
)
653 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
654 goto nla_put_failure
;
656 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
657 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
658 goto nla_put_failure
;
660 #ifdef CONFIG_NF_CONNTRACK_SECMARK
661 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
662 && ctnetlink_dump_secctx(skb
, ct
) < 0)
663 goto nla_put_failure
;
666 if (events
& (1 << IPCT_RELATED
) &&
667 ctnetlink_dump_master(skb
, ct
) < 0)
668 goto nla_put_failure
;
670 if (events
& (1 << IPCT_NATSEQADJ
) &&
671 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
672 goto nla_put_failure
;
675 #ifdef CONFIG_NF_CONNTRACK_MARK
676 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
677 && ctnetlink_dump_mark(skb
, ct
) < 0)
678 goto nla_put_failure
;
683 err
= nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
,
685 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
692 nlmsg_cancel(skb
, nlh
);
696 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
701 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
703 static int ctnetlink_done(struct netlink_callback
*cb
)
706 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
712 struct ctnetlink_dump_filter
{
720 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
722 struct net
*net
= sock_net(skb
->sk
);
723 struct nf_conn
*ct
, *last
;
724 struct nf_conntrack_tuple_hash
*h
;
725 struct hlist_nulls_node
*n
;
726 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
727 u_int8_t l3proto
= nfmsg
->nfgen_family
;
729 #ifdef CONFIG_NF_CONNTRACK_MARK
730 const struct ctnetlink_dump_filter
*filter
= cb
->data
;
733 spin_lock_bh(&nf_conntrack_lock
);
734 last
= (struct nf_conn
*)cb
->args
[1];
735 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
737 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
739 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
741 ct
= nf_ct_tuplehash_to_ctrack(h
);
742 /* Dump entries of a given L3 protocol number.
743 * If it is not specified, ie. l3proto == 0,
744 * then dump everything. */
745 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
752 #ifdef CONFIG_NF_CONNTRACK_MARK
753 if (filter
&& !((ct
->mark
& filter
->mark
.mask
) ==
760 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
762 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
766 nf_conntrack_get(&ct
->ct_general
);
767 cb
->args
[1] = (unsigned long)ct
;
777 spin_unlock_bh(&nf_conntrack_lock
);
785 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
787 struct nlattr
*tb
[CTA_IP_MAX
+1];
788 struct nf_conntrack_l3proto
*l3proto
;
791 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
794 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
796 if (likely(l3proto
->nlattr_to_tuple
)) {
797 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
798 l3proto
->nla_policy
);
800 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
808 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
809 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
813 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
814 struct nf_conntrack_tuple
*tuple
)
816 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
817 struct nf_conntrack_l4proto
*l4proto
;
820 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
824 if (!tb
[CTA_PROTO_NUM
])
826 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
829 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
831 if (likely(l4proto
->nlattr_to_tuple
)) {
832 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
833 l4proto
->nla_policy
);
835 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
843 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
844 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
845 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
849 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
850 struct nf_conntrack_tuple
*tuple
,
851 enum ctattr_type type
, u_int8_t l3num
)
853 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
856 memset(tuple
, 0, sizeof(*tuple
));
858 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
860 if (!tb
[CTA_TUPLE_IP
])
863 tuple
->src
.l3num
= l3num
;
865 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
869 if (!tb
[CTA_TUPLE_PROTO
])
872 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
876 /* orig and expect tuples get DIR_ORIGINAL */
877 if (type
== CTA_TUPLE_REPLY
)
878 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
880 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
886 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
889 #ifdef CONFIG_NF_CONNTRACK_ZONES
890 *zone
= ntohs(nla_get_be16(attr
));
900 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
901 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
905 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
,
906 struct nlattr
**helpinfo
)
908 struct nlattr
*tb
[CTA_HELP_MAX
+1];
910 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
912 if (!tb
[CTA_HELP_NAME
])
915 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
917 if (tb
[CTA_HELP_INFO
])
918 *helpinfo
= tb
[CTA_HELP_INFO
];
923 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
924 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
925 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
926 [CTA_STATUS
] = { .type
= NLA_U32
},
927 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
928 [CTA_HELP
] = { .type
= NLA_NESTED
},
929 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
930 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
931 [CTA_MARK
] = { .type
= NLA_U32
},
932 [CTA_ID
] = { .type
= NLA_U32
},
933 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
934 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
935 [CTA_ZONE
] = { .type
= NLA_U16
},
936 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
940 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
941 const struct nlmsghdr
*nlh
,
942 const struct nlattr
* const cda
[])
944 struct net
*net
= sock_net(ctnl
);
945 struct nf_conntrack_tuple_hash
*h
;
946 struct nf_conntrack_tuple tuple
;
948 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
949 u_int8_t u3
= nfmsg
->nfgen_family
;
953 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
957 if (cda
[CTA_TUPLE_ORIG
])
958 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
959 else if (cda
[CTA_TUPLE_REPLY
])
960 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
962 /* Flush the whole table */
963 nf_conntrack_flush_report(net
,
964 NETLINK_CB(skb
).portid
,
972 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
976 ct
= nf_ct_tuplehash_to_ctrack(h
);
979 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
980 if (id
!= (u32
)(unsigned long)ct
) {
986 if (del_timer(&ct
->timeout
)) {
987 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
988 NETLINK_CB(skb
).portid
,
989 nlmsg_report(nlh
)) < 0) {
990 nf_ct_delete_from_lists(ct
);
991 /* we failed to report the event, try later */
992 nf_ct_insert_dying_list(ct
);
996 /* death_by_timeout would report the event again */
997 set_bit(IPS_DYING_BIT
, &ct
->status
);
998 nf_ct_delete_from_lists(ct
);
1007 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1008 const struct nlmsghdr
*nlh
,
1009 const struct nlattr
* const cda
[])
1011 struct net
*net
= sock_net(ctnl
);
1012 struct nf_conntrack_tuple_hash
*h
;
1013 struct nf_conntrack_tuple tuple
;
1015 struct sk_buff
*skb2
= NULL
;
1016 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1017 u_int8_t u3
= nfmsg
->nfgen_family
;
1021 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1022 struct netlink_dump_control c
= {
1023 .dump
= ctnetlink_dump_table
,
1024 .done
= ctnetlink_done
,
1026 #ifdef CONFIG_NF_CONNTRACK_MARK
1027 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1028 struct ctnetlink_dump_filter
*filter
;
1030 filter
= kzalloc(sizeof(struct ctnetlink_dump_filter
),
1035 filter
->mark
.val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1037 ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
1041 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1044 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1048 if (cda
[CTA_TUPLE_ORIG
])
1049 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
1050 else if (cda
[CTA_TUPLE_REPLY
])
1051 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
1058 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
1062 ct
= nf_ct_tuplehash_to_ctrack(h
);
1065 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1072 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
1073 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1079 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
1088 /* this avoids a loop in nfnetlink. */
1089 return err
== -EAGAIN
? -ENOBUFS
: err
;
1092 #ifdef CONFIG_NF_NAT_NEEDED
1094 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1095 enum nf_nat_manip_type manip
,
1096 const struct nlattr
*attr
)
1098 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1101 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1102 if (!parse_nat_setup
) {
1103 #ifdef CONFIG_MODULES
1106 if (request_module("nf-nat") < 0) {
1113 if (nfnetlink_parse_nat_setup_hook
)
1119 err
= parse_nat_setup(ct
, manip
, attr
);
1120 if (err
== -EAGAIN
) {
1121 #ifdef CONFIG_MODULES
1123 spin_unlock_bh(&nf_conntrack_lock
);
1125 if (request_module("nf-nat-%u", nf_ct_l3num(ct
)) < 0) {
1127 spin_lock_bh(&nf_conntrack_lock
);
1132 spin_lock_bh(&nf_conntrack_lock
);
1143 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1146 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1147 d
= ct
->status
^ status
;
1149 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1153 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1154 /* SEEN_REPLY bit can only be set */
1157 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1158 /* ASSURED bit can only be set */
1161 /* Be careful here, modifying NAT bits can screw up things,
1162 * so don't let users modify them directly if they don't pass
1164 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1169 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1171 #ifdef CONFIG_NF_NAT_NEEDED
1174 if (cda
[CTA_NAT_DST
]) {
1175 ret
= ctnetlink_parse_nat_setup(ct
,
1181 if (cda
[CTA_NAT_SRC
]) {
1182 ret
= ctnetlink_parse_nat_setup(ct
,
1195 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1197 struct nf_conntrack_helper
*helper
;
1198 struct nf_conn_help
*help
= nfct_help(ct
);
1199 char *helpname
= NULL
;
1200 struct nlattr
*helpinfo
= NULL
;
1203 /* don't change helper of sibling connections */
1207 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1211 if (!strcmp(helpname
, "")) {
1212 if (help
&& help
->helper
) {
1213 /* we had a helper before ... */
1214 nf_ct_remove_expectations(ct
);
1215 RCU_INIT_POINTER(help
->helper
, NULL
);
1221 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1222 nf_ct_protonum(ct
));
1223 if (helper
== NULL
) {
1224 #ifdef CONFIG_MODULES
1225 spin_unlock_bh(&nf_conntrack_lock
);
1227 if (request_module("nfct-helper-%s", helpname
) < 0) {
1228 spin_lock_bh(&nf_conntrack_lock
);
1232 spin_lock_bh(&nf_conntrack_lock
);
1233 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1234 nf_ct_protonum(ct
));
1242 if (help
->helper
== helper
) {
1243 /* update private helper data if allowed. */
1244 if (helper
->from_nlattr
&& helpinfo
)
1245 helper
->from_nlattr(helpinfo
, ct
);
1251 /* we cannot set a helper for an existing conntrack */
1256 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1258 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1260 if (!del_timer(&ct
->timeout
))
1263 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1264 add_timer(&ct
->timeout
);
1269 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1270 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1271 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1272 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1276 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1278 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1279 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1280 struct nf_conntrack_l4proto
*l4proto
;
1283 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1286 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1287 if (l4proto
->from_nlattr
)
1288 err
= l4proto
->from_nlattr(tb
, ct
);
1294 #ifdef CONFIG_NF_NAT_NEEDED
1295 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1296 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1297 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1298 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1302 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1304 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1306 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1308 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1311 natseq
->correction_pos
=
1312 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1314 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1317 natseq
->offset_before
=
1318 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1320 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1323 natseq
->offset_after
=
1324 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1330 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1331 const struct nlattr
* const cda
[])
1334 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1339 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1340 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1341 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1345 ct
->status
|= IPS_SEQ_ADJUST
;
1348 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1349 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1350 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1354 ct
->status
|= IPS_SEQ_ADJUST
;
1362 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1363 const struct nlattr
* const cda
[])
1367 /* only allow NAT changes and master assignation for new conntracks */
1368 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1371 if (cda
[CTA_HELP
]) {
1372 err
= ctnetlink_change_helper(ct
, cda
);
1377 if (cda
[CTA_TIMEOUT
]) {
1378 err
= ctnetlink_change_timeout(ct
, cda
);
1383 if (cda
[CTA_STATUS
]) {
1384 err
= ctnetlink_change_status(ct
, cda
);
1389 if (cda
[CTA_PROTOINFO
]) {
1390 err
= ctnetlink_change_protoinfo(ct
, cda
);
1395 #if defined(CONFIG_NF_CONNTRACK_MARK)
1397 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1400 #ifdef CONFIG_NF_NAT_NEEDED
1401 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1402 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1411 static struct nf_conn
*
1412 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1413 const struct nlattr
* const cda
[],
1414 struct nf_conntrack_tuple
*otuple
,
1415 struct nf_conntrack_tuple
*rtuple
,
1420 struct nf_conntrack_helper
*helper
;
1421 struct nf_conn_tstamp
*tstamp
;
1423 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1425 return ERR_PTR(-ENOMEM
);
1427 if (!cda
[CTA_TIMEOUT
])
1429 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1431 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1434 if (cda
[CTA_HELP
]) {
1435 char *helpname
= NULL
;
1436 struct nlattr
*helpinfo
= NULL
;
1438 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1442 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1443 nf_ct_protonum(ct
));
1444 if (helper
== NULL
) {
1446 #ifdef CONFIG_MODULES
1447 if (request_module("nfct-helper-%s", helpname
) < 0) {
1453 helper
= __nf_conntrack_helper_find(helpname
,
1455 nf_ct_protonum(ct
));
1465 struct nf_conn_help
*help
;
1467 help
= nf_ct_helper_ext_add(ct
, helper
, GFP_ATOMIC
);
1472 /* set private helper data if allowed. */
1473 if (helper
->from_nlattr
&& helpinfo
)
1474 helper
->from_nlattr(helpinfo
, ct
);
1476 /* not in hash table yet so not strictly necessary */
1477 RCU_INIT_POINTER(help
->helper
, helper
);
1480 /* try an implicit helper assignation */
1481 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1486 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1487 err
= ctnetlink_change_nat(ct
, cda
);
1492 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1493 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1494 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1495 /* we must add conntrack extensions before confirmation. */
1496 ct
->status
|= IPS_CONFIRMED
;
1498 if (cda
[CTA_STATUS
]) {
1499 err
= ctnetlink_change_status(ct
, cda
);
1504 #ifdef CONFIG_NF_NAT_NEEDED
1505 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1506 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1512 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1513 if (cda
[CTA_PROTOINFO
]) {
1514 err
= ctnetlink_change_protoinfo(ct
, cda
);
1519 #if defined(CONFIG_NF_CONNTRACK_MARK)
1521 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1524 /* setup master conntrack: this is a confirmed expectation */
1525 if (cda
[CTA_TUPLE_MASTER
]) {
1526 struct nf_conntrack_tuple master
;
1527 struct nf_conntrack_tuple_hash
*master_h
;
1528 struct nf_conn
*master_ct
;
1530 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1534 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1535 if (master_h
== NULL
) {
1539 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1540 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1541 ct
->master
= master_ct
;
1543 tstamp
= nf_conn_tstamp_find(ct
);
1545 tstamp
->start
= ktime_to_ns(ktime_get_real());
1547 err
= nf_conntrack_hash_check_insert(ct
);
1558 nf_conntrack_free(ct
);
1559 return ERR_PTR(err
);
1563 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1564 const struct nlmsghdr
*nlh
,
1565 const struct nlattr
* const cda
[])
1567 struct net
*net
= sock_net(ctnl
);
1568 struct nf_conntrack_tuple otuple
, rtuple
;
1569 struct nf_conntrack_tuple_hash
*h
= NULL
;
1570 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1572 u_int8_t u3
= nfmsg
->nfgen_family
;
1576 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1580 if (cda
[CTA_TUPLE_ORIG
]) {
1581 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1586 if (cda
[CTA_TUPLE_REPLY
]) {
1587 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1592 if (cda
[CTA_TUPLE_ORIG
])
1593 h
= nf_conntrack_find_get(net
, zone
, &otuple
);
1594 else if (cda
[CTA_TUPLE_REPLY
])
1595 h
= nf_conntrack_find_get(net
, zone
, &rtuple
);
1599 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1600 enum ip_conntrack_events events
;
1602 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1608 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1609 events
= IPCT_RELATED
;
1613 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1614 (1 << IPCT_ASSURED
) |
1615 (1 << IPCT_HELPER
) |
1616 (1 << IPCT_PROTOINFO
) |
1617 (1 << IPCT_NATSEQADJ
) |
1618 (1 << IPCT_MARK
) | events
,
1619 ct
, NETLINK_CB(skb
).portid
,
1626 /* implicit 'else' */
1629 ct
= nf_ct_tuplehash_to_ctrack(h
);
1630 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1631 spin_lock_bh(&nf_conntrack_lock
);
1632 err
= ctnetlink_change_conntrack(ct
, cda
);
1633 spin_unlock_bh(&nf_conntrack_lock
);
1635 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1636 (1 << IPCT_ASSURED
) |
1637 (1 << IPCT_HELPER
) |
1638 (1 << IPCT_PROTOINFO
) |
1639 (1 << IPCT_NATSEQADJ
) |
1641 ct
, NETLINK_CB(skb
).portid
,
1651 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1652 __u16 cpu
, const struct ip_conntrack_stat
*st
)
1654 struct nlmsghdr
*nlh
;
1655 struct nfgenmsg
*nfmsg
;
1656 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
1658 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS_CPU
);
1659 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
1663 nfmsg
= nlmsg_data(nlh
);
1664 nfmsg
->nfgen_family
= AF_UNSPEC
;
1665 nfmsg
->version
= NFNETLINK_V0
;
1666 nfmsg
->res_id
= htons(cpu
);
1668 if (nla_put_be32(skb
, CTA_STATS_SEARCHED
, htonl(st
->searched
)) ||
1669 nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
1670 nla_put_be32(skb
, CTA_STATS_NEW
, htonl(st
->new)) ||
1671 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
1672 nla_put_be32(skb
, CTA_STATS_IGNORE
, htonl(st
->ignore
)) ||
1673 nla_put_be32(skb
, CTA_STATS_DELETE
, htonl(st
->delete)) ||
1674 nla_put_be32(skb
, CTA_STATS_DELETE_LIST
, htonl(st
->delete_list
)) ||
1675 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
1676 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
1677 htonl(st
->insert_failed
)) ||
1678 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
1679 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
1680 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
1681 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
1682 htonl(st
->search_restart
)))
1683 goto nla_put_failure
;
1685 nlmsg_end(skb
, nlh
);
1690 nlmsg_cancel(skb
, nlh
);
1695 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1698 struct net
*net
= sock_net(skb
->sk
);
1700 if (cb
->args
[0] == nr_cpu_ids
)
1703 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
1704 const struct ip_conntrack_stat
*st
;
1706 if (!cpu_possible(cpu
))
1709 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
1710 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
1711 NETLINK_CB(cb
->skb
).portid
,
1722 ctnetlink_stat_ct_cpu(struct sock
*ctnl
, struct sk_buff
*skb
,
1723 const struct nlmsghdr
*nlh
,
1724 const struct nlattr
* const cda
[])
1726 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1727 struct netlink_dump_control c
= {
1728 .dump
= ctnetlink_ct_stat_cpu_dump
,
1730 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1737 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
1740 struct nlmsghdr
*nlh
;
1741 struct nfgenmsg
*nfmsg
;
1742 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
1743 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
1745 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS
);
1746 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
1750 nfmsg
= nlmsg_data(nlh
);
1751 nfmsg
->nfgen_family
= AF_UNSPEC
;
1752 nfmsg
->version
= NFNETLINK_V0
;
1755 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
1756 goto nla_put_failure
;
1758 nlmsg_end(skb
, nlh
);
1763 nlmsg_cancel(skb
, nlh
);
1768 ctnetlink_stat_ct(struct sock
*ctnl
, struct sk_buff
*skb
,
1769 const struct nlmsghdr
*nlh
,
1770 const struct nlattr
* const cda
[])
1772 struct sk_buff
*skb2
;
1775 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1779 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
1781 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
1786 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
1795 /* this avoids a loop in nfnetlink. */
1796 return err
== -EAGAIN
? -ENOBUFS
: err
;
1799 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
1801 ctnetlink_nfqueue_build_size(const struct nf_conn
*ct
)
1803 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1804 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1805 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1806 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
1807 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
1808 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
1809 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
1810 + nla_total_size(0) /* CTA_PROTOINFO */
1811 + nla_total_size(0) /* CTA_HELP */
1812 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
1813 + ctnetlink_secctx_size(ct
)
1814 #ifdef CONFIG_NF_NAT_NEEDED
1815 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
1816 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
1818 #ifdef CONFIG_NF_CONNTRACK_MARK
1819 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
1821 + ctnetlink_proto_size(ct
)
1826 ctnetlink_nfqueue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
1828 struct nlattr
*nest_parms
;
1831 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
1833 goto nla_put_failure
;
1834 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
1835 goto nla_put_failure
;
1836 nla_nest_end(skb
, nest_parms
);
1838 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
1840 goto nla_put_failure
;
1841 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
1842 goto nla_put_failure
;
1843 nla_nest_end(skb
, nest_parms
);
1845 if (nf_ct_zone(ct
)) {
1846 if (nla_put_be16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
))))
1847 goto nla_put_failure
;
1850 if (ctnetlink_dump_id(skb
, ct
) < 0)
1851 goto nla_put_failure
;
1853 if (ctnetlink_dump_status(skb
, ct
) < 0)
1854 goto nla_put_failure
;
1856 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
1857 goto nla_put_failure
;
1859 if (ctnetlink_dump_protoinfo(skb
, ct
) < 0)
1860 goto nla_put_failure
;
1862 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
1863 goto nla_put_failure
;
1865 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1866 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
1867 goto nla_put_failure
;
1869 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
1870 goto nla_put_failure
;
1872 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
1873 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
1874 goto nla_put_failure
;
1876 #ifdef CONFIG_NF_CONNTRACK_MARK
1877 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
1878 goto nla_put_failure
;
1889 ctnetlink_nfqueue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
1893 if (cda
[CTA_TIMEOUT
]) {
1894 err
= ctnetlink_change_timeout(ct
, cda
);
1898 if (cda
[CTA_STATUS
]) {
1899 err
= ctnetlink_change_status(ct
, cda
);
1903 if (cda
[CTA_HELP
]) {
1904 err
= ctnetlink_change_helper(ct
, cda
);
1908 #if defined(CONFIG_NF_CONNTRACK_MARK)
1910 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1916 ctnetlink_nfqueue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
1918 struct nlattr
*cda
[CTA_MAX
+1];
1921 nla_parse_nested(cda
, CTA_MAX
, attr
, ct_nla_policy
);
1923 spin_lock_bh(&nf_conntrack_lock
);
1924 ret
= ctnetlink_nfqueue_parse_ct((const struct nlattr
**)cda
, ct
);
1925 spin_unlock_bh(&nf_conntrack_lock
);
1930 static struct nfq_ct_hook ctnetlink_nfqueue_hook
= {
1931 .build_size
= ctnetlink_nfqueue_build_size
,
1932 .build
= ctnetlink_nfqueue_build
,
1933 .parse
= ctnetlink_nfqueue_parse
,
1935 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
1937 /***********************************************************************
1939 ***********************************************************************/
1942 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1943 const struct nf_conntrack_tuple
*tuple
,
1944 enum ctattr_expect type
)
1946 struct nlattr
*nest_parms
;
1948 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1950 goto nla_put_failure
;
1951 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1952 goto nla_put_failure
;
1953 nla_nest_end(skb
, nest_parms
);
1962 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1963 const struct nf_conntrack_tuple
*tuple
,
1964 const struct nf_conntrack_tuple_mask
*mask
)
1967 struct nf_conntrack_l3proto
*l3proto
;
1968 struct nf_conntrack_l4proto
*l4proto
;
1969 struct nf_conntrack_tuple m
;
1970 struct nlattr
*nest_parms
;
1972 memset(&m
, 0xFF, sizeof(m
));
1973 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1974 m
.src
.u
.all
= mask
->src
.u
.all
;
1975 m
.dst
.protonum
= tuple
->dst
.protonum
;
1977 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1979 goto nla_put_failure
;
1982 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1983 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1985 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
1986 tuple
->dst
.protonum
);
1987 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1991 if (unlikely(ret
< 0))
1992 goto nla_put_failure
;
1994 nla_nest_end(skb
, nest_parms
);
2002 static const union nf_inet_addr any_addr
;
2005 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
2006 const struct nf_conntrack_expect
*exp
)
2008 struct nf_conn
*master
= exp
->master
;
2009 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
2010 struct nf_conn_help
*help
;
2011 #ifdef CONFIG_NF_NAT_NEEDED
2012 struct nlattr
*nest_parms
;
2013 struct nf_conntrack_tuple nat_tuple
= {};
2015 struct nf_ct_helper_expectfn
*expfn
;
2020 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
2021 goto nla_put_failure
;
2022 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
2023 goto nla_put_failure
;
2024 if (ctnetlink_exp_dump_tuple(skb
,
2025 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
2026 CTA_EXPECT_MASTER
) < 0)
2027 goto nla_put_failure
;
2029 #ifdef CONFIG_NF_NAT_NEEDED
2030 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
2031 exp
->saved_proto
.all
) {
2032 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
2034 goto nla_put_failure
;
2036 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
2037 goto nla_put_failure
;
2039 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
2040 nat_tuple
.src
.u3
= exp
->saved_addr
;
2041 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
2042 nat_tuple
.src
.u
= exp
->saved_proto
;
2044 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
2045 CTA_EXPECT_NAT_TUPLE
) < 0)
2046 goto nla_put_failure
;
2047 nla_nest_end(skb
, nest_parms
);
2050 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
2051 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
2052 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
2053 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
2054 goto nla_put_failure
;
2055 help
= nfct_help(master
);
2057 struct nf_conntrack_helper
*helper
;
2059 helper
= rcu_dereference(help
->helper
);
2061 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
2062 goto nla_put_failure
;
2064 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
2065 if (expfn
!= NULL
&&
2066 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
2067 goto nla_put_failure
;
2076 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2077 int event
, const struct nf_conntrack_expect
*exp
)
2079 struct nlmsghdr
*nlh
;
2080 struct nfgenmsg
*nfmsg
;
2081 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
2083 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2084 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2088 nfmsg
= nlmsg_data(nlh
);
2089 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2090 nfmsg
->version
= NFNETLINK_V0
;
2093 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2094 goto nla_put_failure
;
2096 nlmsg_end(skb
, nlh
);
2101 nlmsg_cancel(skb
, nlh
);
2105 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2107 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
2109 struct nf_conntrack_expect
*exp
= item
->exp
;
2110 struct net
*net
= nf_ct_exp_net(exp
);
2111 struct nlmsghdr
*nlh
;
2112 struct nfgenmsg
*nfmsg
;
2113 struct sk_buff
*skb
;
2114 unsigned int type
, group
;
2117 if (events
& (1 << IPEXP_DESTROY
)) {
2118 type
= IPCTNL_MSG_EXP_DELETE
;
2119 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
2120 } else if (events
& (1 << IPEXP_NEW
)) {
2121 type
= IPCTNL_MSG_EXP_NEW
;
2122 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
2123 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
2127 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
2130 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
2134 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2135 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
2139 nfmsg
= nlmsg_data(nlh
);
2140 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2141 nfmsg
->version
= NFNETLINK_V0
;
2145 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2146 goto nla_put_failure
;
2149 nlmsg_end(skb
, nlh
);
2150 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
2155 nlmsg_cancel(skb
, nlh
);
2159 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
2163 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
2166 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
2171 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2173 struct net
*net
= sock_net(skb
->sk
);
2174 struct nf_conntrack_expect
*exp
, *last
;
2175 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2176 struct hlist_node
*n
;
2177 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2180 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2181 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
2183 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
2185 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2192 if (ctnetlink_exp_fill_info(skb
,
2193 NETLINK_CB(cb
->skb
).portid
,
2197 if (!atomic_inc_not_zero(&exp
->use
))
2199 cb
->args
[1] = (unsigned long)exp
;
2211 nf_ct_expect_put(last
);
2216 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2217 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2218 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2219 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2220 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2221 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2222 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
2223 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2224 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2225 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2226 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2227 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2231 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2232 const struct nlmsghdr
*nlh
,
2233 const struct nlattr
* const cda
[])
2235 struct net
*net
= sock_net(ctnl
);
2236 struct nf_conntrack_tuple tuple
;
2237 struct nf_conntrack_expect
*exp
;
2238 struct sk_buff
*skb2
;
2239 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2240 u_int8_t u3
= nfmsg
->nfgen_family
;
2244 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2245 struct netlink_dump_control c
= {
2246 .dump
= ctnetlink_exp_dump_table
,
2247 .done
= ctnetlink_exp_done
,
2249 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2252 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2256 if (cda
[CTA_EXPECT_TUPLE
])
2257 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2258 else if (cda
[CTA_EXPECT_MASTER
])
2259 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
2266 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
2270 if (cda
[CTA_EXPECT_ID
]) {
2271 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2272 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2273 nf_ct_expect_put(exp
);
2279 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2281 nf_ct_expect_put(exp
);
2286 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2287 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
2289 nf_ct_expect_put(exp
);
2293 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2302 /* this avoids a loop in nfnetlink. */
2303 return err
== -EAGAIN
? -ENOBUFS
: err
;
2307 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2308 const struct nlmsghdr
*nlh
,
2309 const struct nlattr
* const cda
[])
2311 struct net
*net
= sock_net(ctnl
);
2312 struct nf_conntrack_expect
*exp
;
2313 struct nf_conntrack_tuple tuple
;
2314 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2315 struct hlist_node
*n
, *next
;
2316 u_int8_t u3
= nfmsg
->nfgen_family
;
2321 if (cda
[CTA_EXPECT_TUPLE
]) {
2322 /* delete a single expect by tuple */
2323 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2327 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2331 /* bump usage count to 2 */
2332 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
2336 if (cda
[CTA_EXPECT_ID
]) {
2337 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2338 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2339 nf_ct_expect_put(exp
);
2344 /* after list removal, usage count == 1 */
2345 spin_lock_bh(&nf_conntrack_lock
);
2346 if (del_timer(&exp
->timeout
)) {
2347 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
2349 nf_ct_expect_put(exp
);
2351 spin_unlock_bh(&nf_conntrack_lock
);
2352 /* have to put what we 'get' above.
2353 * after this line usage count == 0 */
2354 nf_ct_expect_put(exp
);
2355 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2356 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2357 struct nf_conn_help
*m_help
;
2359 /* delete all expectations for this helper */
2360 spin_lock_bh(&nf_conntrack_lock
);
2361 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2362 hlist_for_each_entry_safe(exp
, n
, next
,
2363 &net
->ct
.expect_hash
[i
],
2365 m_help
= nfct_help(exp
->master
);
2366 if (!strcmp(m_help
->helper
->name
, name
) &&
2367 del_timer(&exp
->timeout
)) {
2368 nf_ct_unlink_expect_report(exp
,
2369 NETLINK_CB(skb
).portid
,
2371 nf_ct_expect_put(exp
);
2375 spin_unlock_bh(&nf_conntrack_lock
);
2377 /* This basically means we have to flush everything*/
2378 spin_lock_bh(&nf_conntrack_lock
);
2379 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2380 hlist_for_each_entry_safe(exp
, n
, next
,
2381 &net
->ct
.expect_hash
[i
],
2383 if (del_timer(&exp
->timeout
)) {
2384 nf_ct_unlink_expect_report(exp
,
2385 NETLINK_CB(skb
).portid
,
2387 nf_ct_expect_put(exp
);
2391 spin_unlock_bh(&nf_conntrack_lock
);
2397 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2398 const struct nlattr
* const cda
[])
2400 if (cda
[CTA_EXPECT_TIMEOUT
]) {
2401 if (!del_timer(&x
->timeout
))
2404 x
->timeout
.expires
= jiffies
+
2405 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2406 add_timer(&x
->timeout
);
2411 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2412 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2413 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2417 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2418 struct nf_conntrack_expect
*exp
,
2421 #ifdef CONFIG_NF_NAT_NEEDED
2422 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2423 struct nf_conntrack_tuple nat_tuple
= {};
2426 nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2428 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2431 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2432 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
, u3
);
2436 exp
->saved_addr
= nat_tuple
.src
.u3
;
2437 exp
->saved_proto
= nat_tuple
.src
.u
;
2438 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2447 ctnetlink_create_expect(struct net
*net
, u16 zone
,
2448 const struct nlattr
* const cda
[],
2450 u32 portid
, int report
)
2452 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
2453 struct nf_conntrack_tuple_hash
*h
= NULL
;
2454 struct nf_conntrack_expect
*exp
;
2456 struct nf_conn_help
*help
;
2457 struct nf_conntrack_helper
*helper
= NULL
;
2458 u_int32_t
class = 0;
2461 /* caller guarantees that those three CTA_EXPECT_* exist */
2462 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2465 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
2468 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
2472 /* Look for master conntrack of this expectation */
2473 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
2476 ct
= nf_ct_tuplehash_to_ctrack(h
);
2478 /* Look for helper of this expectation */
2479 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2480 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2482 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2483 nf_ct_protonum(ct
));
2484 if (helper
== NULL
) {
2485 #ifdef CONFIG_MODULES
2486 if (request_module("nfct-helper-%s", helpname
) < 0) {
2491 helper
= __nf_conntrack_helper_find(helpname
,
2493 nf_ct_protonum(ct
));
2504 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2505 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
2506 if (class > helper
->expect_class_max
) {
2511 exp
= nf_ct_expect_alloc(ct
);
2516 help
= nfct_help(ct
);
2518 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
2522 exp
->timeout
.expires
=
2523 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2525 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
2526 if (cda
[CTA_EXPECT_FLAGS
]) {
2528 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2531 if (cda
[CTA_EXPECT_FLAGS
]) {
2532 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2533 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
2537 if (cda
[CTA_EXPECT_FN
]) {
2538 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
2539 struct nf_ct_helper_expectfn
*expfn
;
2541 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
2542 if (expfn
== NULL
) {
2546 exp
->expectfn
= expfn
->expectfn
;
2548 exp
->expectfn
= NULL
;
2552 exp
->helper
= helper
;
2553 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2554 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2555 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2557 if (cda
[CTA_EXPECT_NAT
]) {
2558 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
2563 err
= nf_ct_expect_related_report(exp
, portid
, report
);
2565 nf_ct_expect_put(exp
);
2567 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2572 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2573 const struct nlmsghdr
*nlh
,
2574 const struct nlattr
* const cda
[])
2576 struct net
*net
= sock_net(ctnl
);
2577 struct nf_conntrack_tuple tuple
;
2578 struct nf_conntrack_expect
*exp
;
2579 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2580 u_int8_t u3
= nfmsg
->nfgen_family
;
2584 if (!cda
[CTA_EXPECT_TUPLE
]
2585 || !cda
[CTA_EXPECT_MASK
]
2586 || !cda
[CTA_EXPECT_MASTER
])
2589 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2593 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2597 spin_lock_bh(&nf_conntrack_lock
);
2598 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2601 spin_unlock_bh(&nf_conntrack_lock
);
2603 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2604 err
= ctnetlink_create_expect(net
, zone
, cda
,
2606 NETLINK_CB(skb
).portid
,
2613 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2614 err
= ctnetlink_change_expect(exp
, cda
);
2615 spin_unlock_bh(&nf_conntrack_lock
);
2621 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
2622 const struct ip_conntrack_stat
*st
)
2624 struct nlmsghdr
*nlh
;
2625 struct nfgenmsg
*nfmsg
;
2626 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2628 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_EXP_GET_STATS_CPU
);
2629 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2633 nfmsg
= nlmsg_data(nlh
);
2634 nfmsg
->nfgen_family
= AF_UNSPEC
;
2635 nfmsg
->version
= NFNETLINK_V0
;
2636 nfmsg
->res_id
= htons(cpu
);
2638 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
2639 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
2640 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
2641 goto nla_put_failure
;
2643 nlmsg_end(skb
, nlh
);
2648 nlmsg_cancel(skb
, nlh
);
2653 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2656 struct net
*net
= sock_net(skb
->sk
);
2658 if (cb
->args
[0] == nr_cpu_ids
)
2661 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2662 const struct ip_conntrack_stat
*st
;
2664 if (!cpu_possible(cpu
))
2667 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2668 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
2679 ctnetlink_stat_exp_cpu(struct sock
*ctnl
, struct sk_buff
*skb
,
2680 const struct nlmsghdr
*nlh
,
2681 const struct nlattr
* const cda
[])
2683 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2684 struct netlink_dump_control c
= {
2685 .dump
= ctnetlink_exp_stat_cpu_dump
,
2687 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2693 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2694 static struct nf_ct_event_notifier ctnl_notifier
= {
2695 .fcn
= ctnetlink_conntrack_event
,
2698 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2699 .fcn
= ctnetlink_expect_event
,
2703 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2704 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2705 .attr_count
= CTA_MAX
,
2706 .policy
= ct_nla_policy
},
2707 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2708 .attr_count
= CTA_MAX
,
2709 .policy
= ct_nla_policy
},
2710 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2711 .attr_count
= CTA_MAX
,
2712 .policy
= ct_nla_policy
},
2713 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2714 .attr_count
= CTA_MAX
,
2715 .policy
= ct_nla_policy
},
2716 [IPCTNL_MSG_CT_GET_STATS_CPU
] = { .call
= ctnetlink_stat_ct_cpu
},
2717 [IPCTNL_MSG_CT_GET_STATS
] = { .call
= ctnetlink_stat_ct
},
2720 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2721 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2722 .attr_count
= CTA_EXPECT_MAX
,
2723 .policy
= exp_nla_policy
},
2724 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2725 .attr_count
= CTA_EXPECT_MAX
,
2726 .policy
= exp_nla_policy
},
2727 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2728 .attr_count
= CTA_EXPECT_MAX
,
2729 .policy
= exp_nla_policy
},
2730 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = { .call
= ctnetlink_stat_exp_cpu
},
2733 static const struct nfnetlink_subsystem ctnl_subsys
= {
2734 .name
= "conntrack",
2735 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2736 .cb_count
= IPCTNL_MSG_MAX
,
2740 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2741 .name
= "conntrack_expect",
2742 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2743 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2747 MODULE_ALIAS("ip_conntrack_netlink");
2748 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2749 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2751 static int __net_init
ctnetlink_net_init(struct net
*net
)
2753 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2756 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
2758 pr_err("ctnetlink_init: cannot register notifier.\n");
2762 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
2764 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2765 goto err_unreg_notifier
;
2770 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2772 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
2778 static void ctnetlink_net_exit(struct net
*net
)
2780 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2781 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
2782 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
2786 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
2790 list_for_each_entry(net
, net_exit_list
, exit_list
)
2791 ctnetlink_net_exit(net
);
2794 static struct pernet_operations ctnetlink_net_ops
= {
2795 .init
= ctnetlink_net_init
,
2796 .exit_batch
= ctnetlink_net_exit_batch
,
2799 static int __init
ctnetlink_init(void)
2803 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2804 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2806 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2810 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2812 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2813 goto err_unreg_subsys
;
2816 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
2818 pr_err("ctnetlink_init: cannot register pernet operations\n");
2819 goto err_unreg_exp_subsys
;
2821 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2822 /* setup interaction between nf_queue and nf_conntrack_netlink. */
2823 RCU_INIT_POINTER(nfq_ct_hook
, &ctnetlink_nfqueue_hook
);
2827 err_unreg_exp_subsys
:
2828 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2830 nfnetlink_subsys_unregister(&ctnl_subsys
);
2835 static void __exit
ctnetlink_exit(void)
2837 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2839 unregister_pernet_subsys(&ctnetlink_net_ops
);
2840 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2841 nfnetlink_subsys_unregister(&ctnl_subsys
);
2842 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2843 RCU_INIT_POINTER(nfq_ct_hook
, NULL
);
2847 module_init(ctnetlink_init
);
2848 module_exit(ctnetlink_exit
);