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-2008 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 #ifdef CONFIG_NF_NAT_NEEDED
46 #include <net/netfilter/nf_nat_core.h>
47 #include <net/netfilter/nf_nat_protocol.h>
50 #include <linux/netfilter/nfnetlink.h>
51 #include <linux/netfilter/nfnetlink_conntrack.h>
53 MODULE_LICENSE("GPL");
55 static char __initdata version
[] = "0.93";
58 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
59 const struct nf_conntrack_tuple
*tuple
,
60 struct nf_conntrack_l4proto
*l4proto
)
63 struct nlattr
*nest_parms
;
65 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
68 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
70 if (likely(l4proto
->tuple_to_nlattr
))
71 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
73 nla_nest_end(skb
, nest_parms
);
82 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
83 const struct nf_conntrack_tuple
*tuple
,
84 struct nf_conntrack_l3proto
*l3proto
)
87 struct nlattr
*nest_parms
;
89 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
93 if (likely(l3proto
->tuple_to_nlattr
))
94 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
96 nla_nest_end(skb
, nest_parms
);
105 ctnetlink_dump_tuples(struct sk_buff
*skb
,
106 const struct nf_conntrack_tuple
*tuple
)
109 struct nf_conntrack_l3proto
*l3proto
;
110 struct nf_conntrack_l4proto
*l4proto
;
112 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
113 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
115 if (unlikely(ret
< 0))
118 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
119 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
125 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
127 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
135 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
137 long timeout
= (ct
->timeout
.expires
- jiffies
) / HZ
;
142 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
150 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
152 struct nf_conntrack_l4proto
*l4proto
;
153 struct nlattr
*nest_proto
;
156 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
157 if (!l4proto
->to_nlattr
)
160 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
162 goto nla_put_failure
;
164 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
166 nla_nest_end(skb
, nest_proto
);
175 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
177 struct nlattr
*nest_helper
;
178 const struct nf_conn_help
*help
= nfct_help(ct
);
179 struct nf_conntrack_helper
*helper
;
184 helper
= rcu_dereference(help
->helper
);
188 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
190 goto nla_put_failure
;
191 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
193 if (helper
->to_nlattr
)
194 helper
->to_nlattr(skb
, ct
);
196 nla_nest_end(skb
, nest_helper
);
205 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
206 enum ip_conntrack_dir dir
)
208 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
209 struct nlattr
*nest_count
;
210 const struct nf_conn_counter
*acct
;
212 acct
= nf_conn_acct_find(ct
);
216 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
218 goto nla_put_failure
;
220 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
,
221 cpu_to_be64(acct
[dir
].packets
));
222 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
,
223 cpu_to_be64(acct
[dir
].bytes
));
225 nla_nest_end(skb
, nest_count
);
233 #ifdef CONFIG_NF_CONNTRACK_MARK
235 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
237 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
244 #define ctnetlink_dump_mark(a, b) (0)
247 #ifdef CONFIG_NF_CONNTRACK_SECMARK
249 ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
251 struct nlattr
*nest_secctx
;
255 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
260 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
262 goto nla_put_failure
;
264 NLA_PUT_STRING(skb
, CTA_SECCTX_NAME
, secctx
);
265 nla_nest_end(skb
, nest_secctx
);
269 security_release_secctx(secctx
, len
);
273 #define ctnetlink_dump_secctx(a, b) (0)
276 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
279 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
281 struct nlattr
*nest_parms
;
283 if (!(ct
->status
& IPS_EXPECTED
))
286 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
288 goto nla_put_failure
;
289 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
290 goto nla_put_failure
;
291 nla_nest_end(skb
, nest_parms
);
299 #ifdef CONFIG_NF_NAT_NEEDED
301 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
303 struct nlattr
*nest_parms
;
305 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
307 goto nla_put_failure
;
309 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
310 htonl(natseq
->correction_pos
));
311 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
312 htonl(natseq
->offset_before
));
313 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
314 htonl(natseq
->offset_after
));
316 nla_nest_end(skb
, nest_parms
);
325 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
327 struct nf_nat_seq
*natseq
;
328 struct nf_conn_nat
*nat
= nfct_nat(ct
);
330 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
333 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
334 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
337 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
338 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
344 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
348 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
350 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
358 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
360 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
368 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
369 int event
, struct nf_conn
*ct
)
371 struct nlmsghdr
*nlh
;
372 struct nfgenmsg
*nfmsg
;
373 struct nlattr
*nest_parms
;
374 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
376 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
377 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
381 nfmsg
= nlmsg_data(nlh
);
382 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
383 nfmsg
->version
= NFNETLINK_V0
;
386 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
388 goto nla_put_failure
;
389 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
390 goto nla_put_failure
;
391 nla_nest_end(skb
, nest_parms
);
393 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
395 goto nla_put_failure
;
396 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
397 goto nla_put_failure
;
398 nla_nest_end(skb
, nest_parms
);
401 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
403 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
404 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
405 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
406 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
407 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
408 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
409 ctnetlink_dump_mark(skb
, ct
) < 0 ||
410 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
411 ctnetlink_dump_id(skb
, ct
) < 0 ||
412 ctnetlink_dump_use(skb
, ct
) < 0 ||
413 ctnetlink_dump_master(skb
, ct
) < 0 ||
414 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
415 goto nla_put_failure
;
422 nlmsg_cancel(skb
, nlh
);
426 #ifdef CONFIG_NF_CONNTRACK_EVENTS
428 ctnetlink_proto_size(const struct nf_conn
*ct
)
430 struct nf_conntrack_l3proto
*l3proto
;
431 struct nf_conntrack_l4proto
*l4proto
;
435 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
436 len
+= l3proto
->nla_size
;
438 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
439 len
+= l4proto
->nla_size
;
446 ctnetlink_counters_size(const struct nf_conn
*ct
)
448 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
450 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
451 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
452 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
457 ctnetlink_secctx_size(const struct nf_conn
*ct
)
459 #ifdef CONFIG_NF_CONNTRACK_SECMARK
462 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
466 return nla_total_size(0) /* CTA_SECCTX */
467 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
474 ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
476 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
477 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
478 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
479 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
480 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
481 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
482 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
483 + ctnetlink_counters_size(ct
)
484 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
485 + nla_total_size(0) /* CTA_PROTOINFO */
486 + nla_total_size(0) /* CTA_HELP */
487 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
488 + ctnetlink_secctx_size(ct
)
489 #ifdef CONFIG_NF_NAT_NEEDED
490 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
491 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
493 #ifdef CONFIG_NF_CONNTRACK_MARK
494 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
496 + ctnetlink_proto_size(ct
)
501 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
504 struct nlmsghdr
*nlh
;
505 struct nfgenmsg
*nfmsg
;
506 struct nlattr
*nest_parms
;
507 struct nf_conn
*ct
= item
->ct
;
510 unsigned int flags
= 0, group
;
513 /* ignore our fake conntrack entry */
514 if (nf_ct_is_untracked(ct
))
517 if (events
& (1 << IPCT_DESTROY
)) {
518 type
= IPCTNL_MSG_CT_DELETE
;
519 group
= NFNLGRP_CONNTRACK_DESTROY
;
520 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
521 type
= IPCTNL_MSG_CT_NEW
;
522 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
523 group
= NFNLGRP_CONNTRACK_NEW
;
525 type
= IPCTNL_MSG_CT_NEW
;
526 group
= NFNLGRP_CONNTRACK_UPDATE
;
531 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
534 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
538 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
539 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
543 nfmsg
= nlmsg_data(nlh
);
544 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
545 nfmsg
->version
= NFNETLINK_V0
;
549 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
551 goto nla_put_failure
;
552 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
553 goto nla_put_failure
;
554 nla_nest_end(skb
, nest_parms
);
556 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
558 goto nla_put_failure
;
559 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
560 goto nla_put_failure
;
561 nla_nest_end(skb
, nest_parms
);
564 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
566 if (ctnetlink_dump_id(skb
, ct
) < 0)
567 goto nla_put_failure
;
569 if (ctnetlink_dump_status(skb
, ct
) < 0)
570 goto nla_put_failure
;
572 if (events
& (1 << IPCT_DESTROY
)) {
573 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
574 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
575 goto nla_put_failure
;
577 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
578 goto nla_put_failure
;
580 if (events
& (1 << IPCT_PROTOINFO
)
581 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
582 goto nla_put_failure
;
584 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
585 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
586 goto nla_put_failure
;
588 #ifdef CONFIG_NF_CONNTRACK_SECMARK
589 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
590 && ctnetlink_dump_secctx(skb
, ct
) < 0)
591 goto nla_put_failure
;
594 if (events
& (1 << IPCT_RELATED
) &&
595 ctnetlink_dump_master(skb
, ct
) < 0)
596 goto nla_put_failure
;
598 if (events
& (1 << IPCT_NATSEQADJ
) &&
599 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
600 goto nla_put_failure
;
603 #ifdef CONFIG_NF_CONNTRACK_MARK
604 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
605 && ctnetlink_dump_mark(skb
, ct
) < 0)
606 goto nla_put_failure
;
611 err
= nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
,
613 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
620 nlmsg_cancel(skb
, nlh
);
624 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
629 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
631 static int ctnetlink_done(struct netlink_callback
*cb
)
634 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
639 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
641 struct net
*net
= sock_net(skb
->sk
);
642 struct nf_conn
*ct
, *last
;
643 struct nf_conntrack_tuple_hash
*h
;
644 struct hlist_nulls_node
*n
;
645 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
646 u_int8_t l3proto
= nfmsg
->nfgen_family
;
648 spin_lock_bh(&nf_conntrack_lock
);
649 last
= (struct nf_conn
*)cb
->args
[1];
650 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
652 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
654 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
656 ct
= nf_ct_tuplehash_to_ctrack(h
);
657 /* Dump entries of a given L3 protocol number.
658 * If it is not specified, ie. l3proto == 0,
659 * then dump everything. */
660 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
667 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
669 IPCTNL_MSG_CT_NEW
, ct
) < 0) {
670 cb
->args
[1] = (unsigned long)ct
;
674 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
675 IPCTNL_MSG_CT_GET_CTRZERO
) {
676 struct nf_conn_counter
*acct
;
678 acct
= nf_conn_acct_find(ct
);
680 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
689 spin_unlock_bh(&nf_conntrack_lock
);
697 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
699 struct nlattr
*tb
[CTA_IP_MAX
+1];
700 struct nf_conntrack_l3proto
*l3proto
;
703 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
706 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
708 if (likely(l3proto
->nlattr_to_tuple
)) {
709 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
710 l3proto
->nla_policy
);
712 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
720 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
721 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
725 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
726 struct nf_conntrack_tuple
*tuple
)
728 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
729 struct nf_conntrack_l4proto
*l4proto
;
732 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
736 if (!tb
[CTA_PROTO_NUM
])
738 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
741 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
743 if (likely(l4proto
->nlattr_to_tuple
)) {
744 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
745 l4proto
->nla_policy
);
747 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
755 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
756 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
757 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
761 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
762 struct nf_conntrack_tuple
*tuple
,
763 enum ctattr_tuple type
, u_int8_t l3num
)
765 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
768 memset(tuple
, 0, sizeof(*tuple
));
770 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
772 if (!tb
[CTA_TUPLE_IP
])
775 tuple
->src
.l3num
= l3num
;
777 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
781 if (!tb
[CTA_TUPLE_PROTO
])
784 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
788 /* orig and expect tuples get DIR_ORIGINAL */
789 if (type
== CTA_TUPLE_REPLY
)
790 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
792 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
798 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
801 #ifdef CONFIG_NF_CONNTRACK_ZONES
802 *zone
= ntohs(nla_get_be16(attr
));
812 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
813 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
817 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
819 struct nlattr
*tb
[CTA_HELP_MAX
+1];
821 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
823 if (!tb
[CTA_HELP_NAME
])
826 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
831 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
832 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
833 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
834 [CTA_STATUS
] = { .type
= NLA_U32
},
835 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
836 [CTA_HELP
] = { .type
= NLA_NESTED
},
837 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
838 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
839 [CTA_MARK
] = { .type
= NLA_U32
},
840 [CTA_ID
] = { .type
= NLA_U32
},
841 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
842 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
843 [CTA_ZONE
] = { .type
= NLA_U16
},
847 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
848 const struct nlmsghdr
*nlh
,
849 const struct nlattr
* const cda
[])
851 struct net
*net
= sock_net(ctnl
);
852 struct nf_conntrack_tuple_hash
*h
;
853 struct nf_conntrack_tuple tuple
;
855 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
856 u_int8_t u3
= nfmsg
->nfgen_family
;
860 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
864 if (cda
[CTA_TUPLE_ORIG
])
865 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
866 else if (cda
[CTA_TUPLE_REPLY
])
867 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
869 /* Flush the whole table */
870 nf_conntrack_flush_report(net
,
879 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
883 ct
= nf_ct_tuplehash_to_ctrack(h
);
886 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
887 if (id
!= (u32
)(unsigned long)ct
) {
893 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
895 nlmsg_report(nlh
)) < 0) {
896 nf_ct_delete_from_lists(ct
);
897 /* we failed to report the event, try later */
898 nf_ct_insert_dying_list(ct
);
903 /* death_by_timeout would report the event again */
904 set_bit(IPS_DYING_BIT
, &ct
->status
);
913 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
914 const struct nlmsghdr
*nlh
,
915 const struct nlattr
* const cda
[])
917 struct net
*net
= sock_net(ctnl
);
918 struct nf_conntrack_tuple_hash
*h
;
919 struct nf_conntrack_tuple tuple
;
921 struct sk_buff
*skb2
= NULL
;
922 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
923 u_int8_t u3
= nfmsg
->nfgen_family
;
927 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
928 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
931 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
935 if (cda
[CTA_TUPLE_ORIG
])
936 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
937 else if (cda
[CTA_TUPLE_REPLY
])
938 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
945 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
949 ct
= nf_ct_tuplehash_to_ctrack(h
);
952 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
959 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
960 IPCTNL_MSG_CT_NEW
, ct
);
966 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
975 /* this avoids a loop in nfnetlink. */
976 return err
== -EAGAIN
? -ENOBUFS
: err
;
979 #ifdef CONFIG_NF_NAT_NEEDED
981 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
982 enum nf_nat_manip_type manip
,
983 const struct nlattr
*attr
)
985 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
987 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
988 if (!parse_nat_setup
) {
989 #ifdef CONFIG_MODULES
991 spin_unlock_bh(&nf_conntrack_lock
);
993 if (request_module("nf-nat-ipv4") < 0) {
995 spin_lock_bh(&nf_conntrack_lock
);
1000 spin_lock_bh(&nf_conntrack_lock
);
1002 if (nfnetlink_parse_nat_setup_hook
)
1008 return parse_nat_setup(ct
, manip
, attr
);
1013 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1016 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1017 d
= ct
->status
^ status
;
1019 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1023 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1024 /* SEEN_REPLY bit can only be set */
1027 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1028 /* ASSURED bit can only be set */
1031 /* Be careful here, modifying NAT bits can screw up things,
1032 * so don't let users modify them directly if they don't pass
1034 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1039 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1041 #ifdef CONFIG_NF_NAT_NEEDED
1044 if (cda
[CTA_NAT_DST
]) {
1045 ret
= ctnetlink_parse_nat_setup(ct
,
1051 if (cda
[CTA_NAT_SRC
]) {
1052 ret
= ctnetlink_parse_nat_setup(ct
,
1065 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1067 struct nf_conntrack_helper
*helper
;
1068 struct nf_conn_help
*help
= nfct_help(ct
);
1069 char *helpname
= NULL
;
1072 /* don't change helper of sibling connections */
1076 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1080 if (!strcmp(helpname
, "")) {
1081 if (help
&& help
->helper
) {
1082 /* we had a helper before ... */
1083 nf_ct_remove_expectations(ct
);
1084 rcu_assign_pointer(help
->helper
, NULL
);
1090 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1091 nf_ct_protonum(ct
));
1092 if (helper
== NULL
) {
1093 #ifdef CONFIG_MODULES
1094 spin_unlock_bh(&nf_conntrack_lock
);
1096 if (request_module("nfct-helper-%s", helpname
) < 0) {
1097 spin_lock_bh(&nf_conntrack_lock
);
1101 spin_lock_bh(&nf_conntrack_lock
);
1102 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1103 nf_ct_protonum(ct
));
1111 if (help
->helper
== helper
)
1115 /* need to zero data of old helper */
1116 memset(&help
->help
, 0, sizeof(help
->help
));
1118 /* we cannot set a helper for an existing conntrack */
1122 rcu_assign_pointer(help
->helper
, helper
);
1128 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1130 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1132 if (!del_timer(&ct
->timeout
))
1135 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1136 add_timer(&ct
->timeout
);
1141 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1142 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1143 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1144 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1148 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1150 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1151 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1152 struct nf_conntrack_l4proto
*l4proto
;
1155 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1158 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1159 if (l4proto
->from_nlattr
)
1160 err
= l4proto
->from_nlattr(tb
, ct
);
1166 #ifdef CONFIG_NF_NAT_NEEDED
1167 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1168 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1169 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1170 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1174 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1176 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1178 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1180 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1183 natseq
->correction_pos
=
1184 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1186 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1189 natseq
->offset_before
=
1190 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1192 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1195 natseq
->offset_after
=
1196 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1202 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1203 const struct nlattr
* const cda
[])
1206 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1211 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1212 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1213 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1217 ct
->status
|= IPS_SEQ_ADJUST
;
1220 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1221 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1222 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1226 ct
->status
|= IPS_SEQ_ADJUST
;
1234 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1235 const struct nlattr
* const cda
[])
1239 /* only allow NAT changes and master assignation for new conntracks */
1240 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1243 if (cda
[CTA_HELP
]) {
1244 err
= ctnetlink_change_helper(ct
, cda
);
1249 if (cda
[CTA_TIMEOUT
]) {
1250 err
= ctnetlink_change_timeout(ct
, cda
);
1255 if (cda
[CTA_STATUS
]) {
1256 err
= ctnetlink_change_status(ct
, cda
);
1261 if (cda
[CTA_PROTOINFO
]) {
1262 err
= ctnetlink_change_protoinfo(ct
, cda
);
1267 #if defined(CONFIG_NF_CONNTRACK_MARK)
1269 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1272 #ifdef CONFIG_NF_NAT_NEEDED
1273 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1274 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1283 static struct nf_conn
*
1284 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1285 const struct nlattr
* const cda
[],
1286 struct nf_conntrack_tuple
*otuple
,
1287 struct nf_conntrack_tuple
*rtuple
,
1292 struct nf_conntrack_helper
*helper
;
1294 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1296 return ERR_PTR(-ENOMEM
);
1298 if (!cda
[CTA_TIMEOUT
])
1300 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1302 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1305 if (cda
[CTA_HELP
]) {
1306 char *helpname
= NULL
;
1308 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1312 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1313 nf_ct_protonum(ct
));
1314 if (helper
== NULL
) {
1316 #ifdef CONFIG_MODULES
1317 if (request_module("nfct-helper-%s", helpname
) < 0) {
1323 helper
= __nf_conntrack_helper_find(helpname
,
1325 nf_ct_protonum(ct
));
1335 struct nf_conn_help
*help
;
1337 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1343 /* not in hash table yet so not strictly necessary */
1344 rcu_assign_pointer(help
->helper
, helper
);
1347 /* try an implicit helper assignation */
1348 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1353 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1354 err
= ctnetlink_change_nat(ct
, cda
);
1359 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1360 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1361 /* we must add conntrack extensions before confirmation. */
1362 ct
->status
|= IPS_CONFIRMED
;
1364 if (cda
[CTA_STATUS
]) {
1365 err
= ctnetlink_change_status(ct
, cda
);
1370 #ifdef CONFIG_NF_NAT_NEEDED
1371 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1372 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1378 if (cda
[CTA_PROTOINFO
]) {
1379 err
= ctnetlink_change_protoinfo(ct
, cda
);
1384 #if defined(CONFIG_NF_CONNTRACK_MARK)
1386 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1389 /* setup master conntrack: this is a confirmed expectation */
1390 if (cda
[CTA_TUPLE_MASTER
]) {
1391 struct nf_conntrack_tuple master
;
1392 struct nf_conntrack_tuple_hash
*master_h
;
1393 struct nf_conn
*master_ct
;
1395 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1399 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1400 if (master_h
== NULL
) {
1404 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1405 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1406 ct
->master
= master_ct
;
1409 add_timer(&ct
->timeout
);
1410 nf_conntrack_hash_insert(ct
);
1418 nf_conntrack_free(ct
);
1419 return ERR_PTR(err
);
1423 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1424 const struct nlmsghdr
*nlh
,
1425 const struct nlattr
* const cda
[])
1427 struct net
*net
= sock_net(ctnl
);
1428 struct nf_conntrack_tuple otuple
, rtuple
;
1429 struct nf_conntrack_tuple_hash
*h
= NULL
;
1430 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1431 u_int8_t u3
= nfmsg
->nfgen_family
;
1435 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1439 if (cda
[CTA_TUPLE_ORIG
]) {
1440 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1445 if (cda
[CTA_TUPLE_REPLY
]) {
1446 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1451 spin_lock_bh(&nf_conntrack_lock
);
1452 if (cda
[CTA_TUPLE_ORIG
])
1453 h
= __nf_conntrack_find(net
, zone
, &otuple
);
1454 else if (cda
[CTA_TUPLE_REPLY
])
1455 h
= __nf_conntrack_find(net
, zone
, &rtuple
);
1459 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1461 enum ip_conntrack_events events
;
1463 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1470 nf_conntrack_get(&ct
->ct_general
);
1471 spin_unlock_bh(&nf_conntrack_lock
);
1472 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1473 events
= IPCT_RELATED
;
1477 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1478 (1 << IPCT_ASSURED
) |
1479 (1 << IPCT_HELPER
) |
1480 (1 << IPCT_PROTOINFO
) |
1481 (1 << IPCT_NATSEQADJ
) |
1482 (1 << IPCT_MARK
) | events
,
1483 ct
, NETLINK_CB(skb
).pid
,
1487 spin_unlock_bh(&nf_conntrack_lock
);
1491 /* implicit 'else' */
1493 /* We manipulate the conntrack inside the global conntrack table lock,
1494 * so there's no need to increase the refcount */
1496 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1497 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1499 err
= ctnetlink_change_conntrack(ct
, cda
);
1501 nf_conntrack_get(&ct
->ct_general
);
1502 spin_unlock_bh(&nf_conntrack_lock
);
1503 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1504 (1 << IPCT_ASSURED
) |
1505 (1 << IPCT_HELPER
) |
1506 (1 << IPCT_PROTOINFO
) |
1507 (1 << IPCT_NATSEQADJ
) |
1509 ct
, NETLINK_CB(skb
).pid
,
1513 spin_unlock_bh(&nf_conntrack_lock
);
1519 spin_unlock_bh(&nf_conntrack_lock
);
1523 /***********************************************************************
1525 ***********************************************************************/
1528 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1529 const struct nf_conntrack_tuple
*tuple
,
1530 enum ctattr_expect type
)
1532 struct nlattr
*nest_parms
;
1534 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1536 goto nla_put_failure
;
1537 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1538 goto nla_put_failure
;
1539 nla_nest_end(skb
, nest_parms
);
1548 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1549 const struct nf_conntrack_tuple
*tuple
,
1550 const struct nf_conntrack_tuple_mask
*mask
)
1553 struct nf_conntrack_l3proto
*l3proto
;
1554 struct nf_conntrack_l4proto
*l4proto
;
1555 struct nf_conntrack_tuple m
;
1556 struct nlattr
*nest_parms
;
1558 memset(&m
, 0xFF, sizeof(m
));
1559 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1560 m
.src
.u
.all
= mask
->src
.u
.all
;
1561 m
.dst
.protonum
= tuple
->dst
.protonum
;
1563 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1565 goto nla_put_failure
;
1567 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1568 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1570 if (unlikely(ret
< 0))
1571 goto nla_put_failure
;
1573 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1574 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1575 if (unlikely(ret
< 0))
1576 goto nla_put_failure
;
1578 nla_nest_end(skb
, nest_parms
);
1587 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1588 const struct nf_conntrack_expect
*exp
)
1590 struct nf_conn
*master
= exp
->master
;
1591 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1592 struct nf_conn_help
*help
;
1597 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1598 goto nla_put_failure
;
1599 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1600 goto nla_put_failure
;
1601 if (ctnetlink_exp_dump_tuple(skb
,
1602 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1603 CTA_EXPECT_MASTER
) < 0)
1604 goto nla_put_failure
;
1606 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1607 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1608 NLA_PUT_BE32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
));
1609 help
= nfct_help(master
);
1611 struct nf_conntrack_helper
*helper
;
1613 helper
= rcu_dereference(help
->helper
);
1615 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1625 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1626 int event
, const struct nf_conntrack_expect
*exp
)
1628 struct nlmsghdr
*nlh
;
1629 struct nfgenmsg
*nfmsg
;
1630 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1632 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1633 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1637 nfmsg
= nlmsg_data(nlh
);
1638 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1639 nfmsg
->version
= NFNETLINK_V0
;
1642 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1643 goto nla_put_failure
;
1645 nlmsg_end(skb
, nlh
);
1650 nlmsg_cancel(skb
, nlh
);
1654 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1656 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1658 struct nf_conntrack_expect
*exp
= item
->exp
;
1659 struct net
*net
= nf_ct_exp_net(exp
);
1660 struct nlmsghdr
*nlh
;
1661 struct nfgenmsg
*nfmsg
;
1662 struct sk_buff
*skb
;
1663 unsigned int type
, group
;
1666 if (events
& (1 << IPEXP_DESTROY
)) {
1667 type
= IPCTNL_MSG_EXP_DELETE
;
1668 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
1669 } else if (events
& (1 << IPEXP_NEW
)) {
1670 type
= IPCTNL_MSG_EXP_NEW
;
1671 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1672 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
1676 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
1679 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1683 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1684 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1688 nfmsg
= nlmsg_data(nlh
);
1689 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1690 nfmsg
->version
= NFNETLINK_V0
;
1694 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1695 goto nla_put_failure
;
1698 nlmsg_end(skb
, nlh
);
1699 nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
, GFP_ATOMIC
);
1704 nlmsg_cancel(skb
, nlh
);
1708 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1712 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1715 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1720 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1722 struct net
*net
= sock_net(skb
->sk
);
1723 struct nf_conntrack_expect
*exp
, *last
;
1724 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1725 struct hlist_node
*n
;
1726 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1729 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1730 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1732 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1734 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1741 if (ctnetlink_exp_fill_info(skb
,
1742 NETLINK_CB(cb
->skb
).pid
,
1746 if (!atomic_inc_not_zero(&exp
->use
))
1748 cb
->args
[1] = (unsigned long)exp
;
1760 nf_ct_expect_put(last
);
1765 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1766 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1767 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1768 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1769 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1770 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1771 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1772 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
1773 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
1777 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1778 const struct nlmsghdr
*nlh
,
1779 const struct nlattr
* const cda
[])
1781 struct net
*net
= sock_net(ctnl
);
1782 struct nf_conntrack_tuple tuple
;
1783 struct nf_conntrack_expect
*exp
;
1784 struct sk_buff
*skb2
;
1785 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1786 u_int8_t u3
= nfmsg
->nfgen_family
;
1790 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1791 return netlink_dump_start(ctnl
, skb
, nlh
,
1792 ctnetlink_exp_dump_table
,
1793 ctnetlink_exp_done
);
1796 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1800 if (cda
[CTA_EXPECT_MASTER
])
1801 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1808 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1812 if (cda
[CTA_EXPECT_ID
]) {
1813 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1814 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1815 nf_ct_expect_put(exp
);
1821 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1826 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1827 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1832 nf_ct_expect_put(exp
);
1834 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1839 nf_ct_expect_put(exp
);
1844 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1845 const struct nlmsghdr
*nlh
,
1846 const struct nlattr
* const cda
[])
1848 struct net
*net
= sock_net(ctnl
);
1849 struct nf_conntrack_expect
*exp
;
1850 struct nf_conntrack_tuple tuple
;
1851 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1852 struct hlist_node
*n
, *next
;
1853 u_int8_t u3
= nfmsg
->nfgen_family
;
1858 if (cda
[CTA_EXPECT_TUPLE
]) {
1859 /* delete a single expect by tuple */
1860 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1864 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1868 /* bump usage count to 2 */
1869 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1873 if (cda
[CTA_EXPECT_ID
]) {
1874 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1875 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1876 nf_ct_expect_put(exp
);
1881 /* after list removal, usage count == 1 */
1882 spin_lock_bh(&nf_conntrack_lock
);
1883 if (del_timer(&exp
->timeout
)) {
1884 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).pid
,
1886 nf_ct_expect_put(exp
);
1888 spin_unlock_bh(&nf_conntrack_lock
);
1889 /* have to put what we 'get' above.
1890 * after this line usage count == 0 */
1891 nf_ct_expect_put(exp
);
1892 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1893 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1894 struct nf_conn_help
*m_help
;
1896 /* delete all expectations for this helper */
1897 spin_lock_bh(&nf_conntrack_lock
);
1898 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1899 hlist_for_each_entry_safe(exp
, n
, next
,
1900 &net
->ct
.expect_hash
[i
],
1902 m_help
= nfct_help(exp
->master
);
1903 if (!strcmp(m_help
->helper
->name
, name
) &&
1904 del_timer(&exp
->timeout
)) {
1905 nf_ct_unlink_expect_report(exp
,
1906 NETLINK_CB(skb
).pid
,
1908 nf_ct_expect_put(exp
);
1912 spin_unlock_bh(&nf_conntrack_lock
);
1914 /* This basically means we have to flush everything*/
1915 spin_lock_bh(&nf_conntrack_lock
);
1916 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1917 hlist_for_each_entry_safe(exp
, n
, next
,
1918 &net
->ct
.expect_hash
[i
],
1920 if (del_timer(&exp
->timeout
)) {
1921 nf_ct_unlink_expect_report(exp
,
1922 NETLINK_CB(skb
).pid
,
1924 nf_ct_expect_put(exp
);
1928 spin_unlock_bh(&nf_conntrack_lock
);
1934 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
1935 const struct nlattr
* const cda
[])
1941 ctnetlink_create_expect(struct net
*net
, u16 zone
,
1942 const struct nlattr
* const cda
[],
1944 u32 pid
, int report
)
1946 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1947 struct nf_conntrack_tuple_hash
*h
= NULL
;
1948 struct nf_conntrack_expect
*exp
;
1950 struct nf_conn_help
*help
;
1953 /* caller guarantees that those three CTA_EXPECT_* exist */
1954 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1957 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1960 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1964 /* Look for master conntrack of this expectation */
1965 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
1968 ct
= nf_ct_tuplehash_to_ctrack(h
);
1969 exp
= nf_ct_expect_alloc(ct
);
1974 help
= nfct_help(ct
);
1976 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
1980 exp
->timeout
.expires
=
1981 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
1983 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
1984 if (cda
[CTA_EXPECT_FLAGS
]) {
1986 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1989 if (cda
[CTA_EXPECT_FLAGS
]) {
1990 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1991 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
1997 exp
->expectfn
= NULL
;
2000 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2001 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2002 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2004 err
= nf_ct_expect_related_report(exp
, pid
, report
);
2005 nf_ct_expect_put(exp
);
2008 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2013 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2014 const struct nlmsghdr
*nlh
,
2015 const struct nlattr
* const cda
[])
2017 struct net
*net
= sock_net(ctnl
);
2018 struct nf_conntrack_tuple tuple
;
2019 struct nf_conntrack_expect
*exp
;
2020 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2021 u_int8_t u3
= nfmsg
->nfgen_family
;
2025 if (!cda
[CTA_EXPECT_TUPLE
]
2026 || !cda
[CTA_EXPECT_MASK
]
2027 || !cda
[CTA_EXPECT_MASTER
])
2030 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2034 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2038 spin_lock_bh(&nf_conntrack_lock
);
2039 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2042 spin_unlock_bh(&nf_conntrack_lock
);
2044 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2045 err
= ctnetlink_create_expect(net
, zone
, cda
,
2047 NETLINK_CB(skb
).pid
,
2054 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2055 err
= ctnetlink_change_expect(exp
, cda
);
2056 spin_unlock_bh(&nf_conntrack_lock
);
2061 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2062 static struct nf_ct_event_notifier ctnl_notifier
= {
2063 .fcn
= ctnetlink_conntrack_event
,
2066 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2067 .fcn
= ctnetlink_expect_event
,
2071 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2072 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2073 .attr_count
= CTA_MAX
,
2074 .policy
= ct_nla_policy
},
2075 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2076 .attr_count
= CTA_MAX
,
2077 .policy
= ct_nla_policy
},
2078 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2079 .attr_count
= CTA_MAX
,
2080 .policy
= ct_nla_policy
},
2081 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2082 .attr_count
= CTA_MAX
,
2083 .policy
= ct_nla_policy
},
2086 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2087 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2088 .attr_count
= CTA_EXPECT_MAX
,
2089 .policy
= exp_nla_policy
},
2090 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2091 .attr_count
= CTA_EXPECT_MAX
,
2092 .policy
= exp_nla_policy
},
2093 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2094 .attr_count
= CTA_EXPECT_MAX
,
2095 .policy
= exp_nla_policy
},
2098 static const struct nfnetlink_subsystem ctnl_subsys
= {
2099 .name
= "conntrack",
2100 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2101 .cb_count
= IPCTNL_MSG_MAX
,
2105 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2106 .name
= "conntrack_expect",
2107 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2108 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2112 MODULE_ALIAS("ip_conntrack_netlink");
2113 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2114 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2116 static int __init
ctnetlink_init(void)
2120 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2121 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2123 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2127 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2129 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2130 goto err_unreg_subsys
;
2133 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2134 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
2136 pr_err("ctnetlink_init: cannot register notifier.\n");
2137 goto err_unreg_exp_subsys
;
2140 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
2142 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2143 goto err_unreg_notifier
;
2149 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2151 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2152 err_unreg_exp_subsys
:
2153 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2156 nfnetlink_subsys_unregister(&ctnl_subsys
);
2161 static void __exit
ctnetlink_exit(void)
2163 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2165 nf_ct_remove_userspace_expectations();
2166 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2167 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2168 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2171 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2172 nfnetlink_subsys_unregister(&ctnl_subsys
);
2175 module_init(ctnetlink_init
);
2176 module_exit(ctnetlink_exit
);