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
) == 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
);
978 #ifdef CONFIG_NF_NAT_NEEDED
980 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
981 enum nf_nat_manip_type manip
,
982 const struct nlattr
*attr
)
984 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
986 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
987 if (!parse_nat_setup
) {
988 #ifdef CONFIG_MODULES
990 spin_unlock_bh(&nf_conntrack_lock
);
992 if (request_module("nf-nat-ipv4") < 0) {
994 spin_lock_bh(&nf_conntrack_lock
);
999 spin_lock_bh(&nf_conntrack_lock
);
1001 if (nfnetlink_parse_nat_setup_hook
)
1007 return parse_nat_setup(ct
, manip
, attr
);
1012 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1015 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1016 d
= ct
->status
^ status
;
1018 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1022 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1023 /* SEEN_REPLY bit can only be set */
1026 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1027 /* ASSURED bit can only be set */
1030 /* Be careful here, modifying NAT bits can screw up things,
1031 * so don't let users modify them directly if they don't pass
1033 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1038 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1040 #ifdef CONFIG_NF_NAT_NEEDED
1043 if (cda
[CTA_NAT_DST
]) {
1044 ret
= ctnetlink_parse_nat_setup(ct
,
1050 if (cda
[CTA_NAT_SRC
]) {
1051 ret
= ctnetlink_parse_nat_setup(ct
,
1064 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1066 struct nf_conntrack_helper
*helper
;
1067 struct nf_conn_help
*help
= nfct_help(ct
);
1068 char *helpname
= NULL
;
1071 /* don't change helper of sibling connections */
1075 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1079 if (!strcmp(helpname
, "")) {
1080 if (help
&& help
->helper
) {
1081 /* we had a helper before ... */
1082 nf_ct_remove_expectations(ct
);
1083 rcu_assign_pointer(help
->helper
, NULL
);
1089 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1090 nf_ct_protonum(ct
));
1091 if (helper
== NULL
) {
1092 #ifdef CONFIG_MODULES
1093 spin_unlock_bh(&nf_conntrack_lock
);
1095 if (request_module("nfct-helper-%s", helpname
) < 0) {
1096 spin_lock_bh(&nf_conntrack_lock
);
1100 spin_lock_bh(&nf_conntrack_lock
);
1101 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1102 nf_ct_protonum(ct
));
1110 if (help
->helper
== helper
)
1114 /* need to zero data of old helper */
1115 memset(&help
->help
, 0, sizeof(help
->help
));
1117 /* we cannot set a helper for an existing conntrack */
1121 rcu_assign_pointer(help
->helper
, helper
);
1127 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1129 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1131 if (!del_timer(&ct
->timeout
))
1134 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1135 add_timer(&ct
->timeout
);
1140 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1141 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1142 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1143 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1147 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1149 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1150 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1151 struct nf_conntrack_l4proto
*l4proto
;
1154 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1157 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1158 if (l4proto
->from_nlattr
)
1159 err
= l4proto
->from_nlattr(tb
, ct
);
1165 #ifdef CONFIG_NF_NAT_NEEDED
1166 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1167 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1168 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1169 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1173 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1175 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1177 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1179 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1182 natseq
->correction_pos
=
1183 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1185 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1188 natseq
->offset_before
=
1189 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1191 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1194 natseq
->offset_after
=
1195 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1201 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1202 const struct nlattr
* const cda
[])
1205 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1210 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1211 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1212 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1216 ct
->status
|= IPS_SEQ_ADJUST
;
1219 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1220 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1221 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1225 ct
->status
|= IPS_SEQ_ADJUST
;
1233 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1234 const struct nlattr
* const cda
[])
1238 /* only allow NAT changes and master assignation for new conntracks */
1239 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1242 if (cda
[CTA_HELP
]) {
1243 err
= ctnetlink_change_helper(ct
, cda
);
1248 if (cda
[CTA_TIMEOUT
]) {
1249 err
= ctnetlink_change_timeout(ct
, cda
);
1254 if (cda
[CTA_STATUS
]) {
1255 err
= ctnetlink_change_status(ct
, cda
);
1260 if (cda
[CTA_PROTOINFO
]) {
1261 err
= ctnetlink_change_protoinfo(ct
, cda
);
1266 #if defined(CONFIG_NF_CONNTRACK_MARK)
1268 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1271 #ifdef CONFIG_NF_NAT_NEEDED
1272 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1273 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1282 static struct nf_conn
*
1283 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1284 const struct nlattr
* const cda
[],
1285 struct nf_conntrack_tuple
*otuple
,
1286 struct nf_conntrack_tuple
*rtuple
,
1291 struct nf_conntrack_helper
*helper
;
1293 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1295 return ERR_PTR(-ENOMEM
);
1297 if (!cda
[CTA_TIMEOUT
])
1299 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1301 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1304 if (cda
[CTA_HELP
]) {
1305 char *helpname
= NULL
;
1307 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1311 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1312 nf_ct_protonum(ct
));
1313 if (helper
== NULL
) {
1315 #ifdef CONFIG_MODULES
1316 if (request_module("nfct-helper-%s", helpname
) < 0) {
1322 helper
= __nf_conntrack_helper_find(helpname
,
1324 nf_ct_protonum(ct
));
1334 struct nf_conn_help
*help
;
1336 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1342 /* not in hash table yet so not strictly necessary */
1343 rcu_assign_pointer(help
->helper
, helper
);
1346 /* try an implicit helper assignation */
1347 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1352 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1353 err
= ctnetlink_change_nat(ct
, cda
);
1358 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1359 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1360 /* we must add conntrack extensions before confirmation. */
1361 ct
->status
|= IPS_CONFIRMED
;
1363 if (cda
[CTA_STATUS
]) {
1364 err
= ctnetlink_change_status(ct
, cda
);
1369 #ifdef CONFIG_NF_NAT_NEEDED
1370 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1371 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1377 if (cda
[CTA_PROTOINFO
]) {
1378 err
= ctnetlink_change_protoinfo(ct
, cda
);
1383 #if defined(CONFIG_NF_CONNTRACK_MARK)
1385 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1388 /* setup master conntrack: this is a confirmed expectation */
1389 if (cda
[CTA_TUPLE_MASTER
]) {
1390 struct nf_conntrack_tuple master
;
1391 struct nf_conntrack_tuple_hash
*master_h
;
1392 struct nf_conn
*master_ct
;
1394 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1398 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1399 if (master_h
== NULL
) {
1403 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1404 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1405 ct
->master
= master_ct
;
1408 add_timer(&ct
->timeout
);
1409 nf_conntrack_hash_insert(ct
);
1417 nf_conntrack_free(ct
);
1418 return ERR_PTR(err
);
1422 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1423 const struct nlmsghdr
*nlh
,
1424 const struct nlattr
* const cda
[])
1426 struct net
*net
= sock_net(ctnl
);
1427 struct nf_conntrack_tuple otuple
, rtuple
;
1428 struct nf_conntrack_tuple_hash
*h
= NULL
;
1429 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1430 u_int8_t u3
= nfmsg
->nfgen_family
;
1434 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1438 if (cda
[CTA_TUPLE_ORIG
]) {
1439 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1444 if (cda
[CTA_TUPLE_REPLY
]) {
1445 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1450 spin_lock_bh(&nf_conntrack_lock
);
1451 if (cda
[CTA_TUPLE_ORIG
])
1452 h
= __nf_conntrack_find(net
, zone
, &otuple
);
1453 else if (cda
[CTA_TUPLE_REPLY
])
1454 h
= __nf_conntrack_find(net
, zone
, &rtuple
);
1458 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1460 enum ip_conntrack_events events
;
1462 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1469 nf_conntrack_get(&ct
->ct_general
);
1470 spin_unlock_bh(&nf_conntrack_lock
);
1471 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1472 events
= IPCT_RELATED
;
1476 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1477 (1 << IPCT_ASSURED
) |
1478 (1 << IPCT_HELPER
) |
1479 (1 << IPCT_PROTOINFO
) |
1480 (1 << IPCT_NATSEQADJ
) |
1481 (1 << IPCT_MARK
) | events
,
1482 ct
, NETLINK_CB(skb
).pid
,
1486 spin_unlock_bh(&nf_conntrack_lock
);
1490 /* implicit 'else' */
1492 /* We manipulate the conntrack inside the global conntrack table lock,
1493 * so there's no need to increase the refcount */
1495 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1496 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1498 err
= ctnetlink_change_conntrack(ct
, cda
);
1500 nf_conntrack_get(&ct
->ct_general
);
1501 spin_unlock_bh(&nf_conntrack_lock
);
1502 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1503 (1 << IPCT_ASSURED
) |
1504 (1 << IPCT_HELPER
) |
1505 (1 << IPCT_PROTOINFO
) |
1506 (1 << IPCT_NATSEQADJ
) |
1508 ct
, NETLINK_CB(skb
).pid
,
1512 spin_unlock_bh(&nf_conntrack_lock
);
1518 spin_unlock_bh(&nf_conntrack_lock
);
1522 /***********************************************************************
1524 ***********************************************************************/
1527 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1528 const struct nf_conntrack_tuple
*tuple
,
1529 enum ctattr_expect type
)
1531 struct nlattr
*nest_parms
;
1533 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1535 goto nla_put_failure
;
1536 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1537 goto nla_put_failure
;
1538 nla_nest_end(skb
, nest_parms
);
1547 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1548 const struct nf_conntrack_tuple
*tuple
,
1549 const struct nf_conntrack_tuple_mask
*mask
)
1552 struct nf_conntrack_l3proto
*l3proto
;
1553 struct nf_conntrack_l4proto
*l4proto
;
1554 struct nf_conntrack_tuple m
;
1555 struct nlattr
*nest_parms
;
1557 memset(&m
, 0xFF, sizeof(m
));
1558 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1559 m
.src
.u
.all
= mask
->src
.u
.all
;
1560 m
.dst
.protonum
= tuple
->dst
.protonum
;
1562 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1564 goto nla_put_failure
;
1566 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1567 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1569 if (unlikely(ret
< 0))
1570 goto nla_put_failure
;
1572 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1573 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1574 if (unlikely(ret
< 0))
1575 goto nla_put_failure
;
1577 nla_nest_end(skb
, nest_parms
);
1586 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1587 const struct nf_conntrack_expect
*exp
)
1589 struct nf_conn
*master
= exp
->master
;
1590 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1591 struct nf_conn_help
*help
;
1596 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1597 goto nla_put_failure
;
1598 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1599 goto nla_put_failure
;
1600 if (ctnetlink_exp_dump_tuple(skb
,
1601 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1602 CTA_EXPECT_MASTER
) < 0)
1603 goto nla_put_failure
;
1605 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1606 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1607 NLA_PUT_BE32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
));
1608 help
= nfct_help(master
);
1610 struct nf_conntrack_helper
*helper
;
1612 helper
= rcu_dereference(help
->helper
);
1614 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1624 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1625 int event
, const struct nf_conntrack_expect
*exp
)
1627 struct nlmsghdr
*nlh
;
1628 struct nfgenmsg
*nfmsg
;
1629 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1631 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1632 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1636 nfmsg
= nlmsg_data(nlh
);
1637 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1638 nfmsg
->version
= NFNETLINK_V0
;
1641 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1642 goto nla_put_failure
;
1644 nlmsg_end(skb
, nlh
);
1649 nlmsg_cancel(skb
, nlh
);
1653 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1655 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1657 struct nf_conntrack_expect
*exp
= item
->exp
;
1658 struct net
*net
= nf_ct_exp_net(exp
);
1659 struct nlmsghdr
*nlh
;
1660 struct nfgenmsg
*nfmsg
;
1661 struct sk_buff
*skb
;
1662 unsigned int type
, group
;
1665 if (events
& (1 << IPEXP_DESTROY
)) {
1666 type
= IPCTNL_MSG_EXP_DELETE
;
1667 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
1668 } else if (events
& (1 << IPEXP_NEW
)) {
1669 type
= IPCTNL_MSG_EXP_NEW
;
1670 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1671 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
1675 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
1678 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1682 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1683 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1687 nfmsg
= nlmsg_data(nlh
);
1688 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1689 nfmsg
->version
= NFNETLINK_V0
;
1693 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1694 goto nla_put_failure
;
1697 nlmsg_end(skb
, nlh
);
1698 nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
, GFP_ATOMIC
);
1703 nlmsg_cancel(skb
, nlh
);
1707 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1711 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1714 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1719 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1721 struct net
*net
= sock_net(skb
->sk
);
1722 struct nf_conntrack_expect
*exp
, *last
;
1723 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1724 struct hlist_node
*n
;
1725 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1728 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1729 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1731 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1733 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1740 if (ctnetlink_exp_fill_info(skb
,
1741 NETLINK_CB(cb
->skb
).pid
,
1745 if (!atomic_inc_not_zero(&exp
->use
))
1747 cb
->args
[1] = (unsigned long)exp
;
1759 nf_ct_expect_put(last
);
1764 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1765 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1766 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1767 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1768 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1769 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1770 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1771 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
1772 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
1776 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1777 const struct nlmsghdr
*nlh
,
1778 const struct nlattr
* const cda
[])
1780 struct net
*net
= sock_net(ctnl
);
1781 struct nf_conntrack_tuple tuple
;
1782 struct nf_conntrack_expect
*exp
;
1783 struct sk_buff
*skb2
;
1784 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1785 u_int8_t u3
= nfmsg
->nfgen_family
;
1789 if ((nlh
->nlmsg_flags
& NLM_F_DUMP
) == NLM_F_DUMP
) {
1790 return netlink_dump_start(ctnl
, skb
, nlh
,
1791 ctnetlink_exp_dump_table
,
1792 ctnetlink_exp_done
);
1795 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1799 if (cda
[CTA_EXPECT_MASTER
])
1800 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1807 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1811 if (cda
[CTA_EXPECT_ID
]) {
1812 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1813 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1814 nf_ct_expect_put(exp
);
1820 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1825 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1826 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1831 nf_ct_expect_put(exp
);
1833 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1838 nf_ct_expect_put(exp
);
1843 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1844 const struct nlmsghdr
*nlh
,
1845 const struct nlattr
* const cda
[])
1847 struct net
*net
= sock_net(ctnl
);
1848 struct nf_conntrack_expect
*exp
;
1849 struct nf_conntrack_tuple tuple
;
1850 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1851 struct hlist_node
*n
, *next
;
1852 u_int8_t u3
= nfmsg
->nfgen_family
;
1857 if (cda
[CTA_EXPECT_TUPLE
]) {
1858 /* delete a single expect by tuple */
1859 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1863 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1867 /* bump usage count to 2 */
1868 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1872 if (cda
[CTA_EXPECT_ID
]) {
1873 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1874 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1875 nf_ct_expect_put(exp
);
1880 /* after list removal, usage count == 1 */
1881 spin_lock_bh(&nf_conntrack_lock
);
1882 if (del_timer(&exp
->timeout
)) {
1883 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).pid
,
1885 nf_ct_expect_put(exp
);
1887 spin_unlock_bh(&nf_conntrack_lock
);
1888 /* have to put what we 'get' above.
1889 * after this line usage count == 0 */
1890 nf_ct_expect_put(exp
);
1891 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1892 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1893 struct nf_conn_help
*m_help
;
1895 /* delete all expectations for this helper */
1896 spin_lock_bh(&nf_conntrack_lock
);
1897 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1898 hlist_for_each_entry_safe(exp
, n
, next
,
1899 &net
->ct
.expect_hash
[i
],
1901 m_help
= nfct_help(exp
->master
);
1902 if (!strcmp(m_help
->helper
->name
, name
) &&
1903 del_timer(&exp
->timeout
)) {
1904 nf_ct_unlink_expect_report(exp
,
1905 NETLINK_CB(skb
).pid
,
1907 nf_ct_expect_put(exp
);
1911 spin_unlock_bh(&nf_conntrack_lock
);
1913 /* This basically means we have to flush everything*/
1914 spin_lock_bh(&nf_conntrack_lock
);
1915 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1916 hlist_for_each_entry_safe(exp
, n
, next
,
1917 &net
->ct
.expect_hash
[i
],
1919 if (del_timer(&exp
->timeout
)) {
1920 nf_ct_unlink_expect_report(exp
,
1921 NETLINK_CB(skb
).pid
,
1923 nf_ct_expect_put(exp
);
1927 spin_unlock_bh(&nf_conntrack_lock
);
1933 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
1934 const struct nlattr
* const cda
[])
1940 ctnetlink_create_expect(struct net
*net
, u16 zone
,
1941 const struct nlattr
* const cda
[],
1943 u32 pid
, int report
)
1945 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1946 struct nf_conntrack_tuple_hash
*h
= NULL
;
1947 struct nf_conntrack_expect
*exp
;
1949 struct nf_conn_help
*help
;
1952 /* caller guarantees that those three CTA_EXPECT_* exist */
1953 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1956 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1959 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1963 /* Look for master conntrack of this expectation */
1964 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
1967 ct
= nf_ct_tuplehash_to_ctrack(h
);
1968 exp
= nf_ct_expect_alloc(ct
);
1973 help
= nfct_help(ct
);
1975 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
1979 exp
->timeout
.expires
=
1980 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
1982 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
1983 if (cda
[CTA_EXPECT_FLAGS
]) {
1985 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1988 if (cda
[CTA_EXPECT_FLAGS
]) {
1989 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1990 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
1996 exp
->expectfn
= NULL
;
1999 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2000 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2001 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2003 err
= nf_ct_expect_related_report(exp
, pid
, report
);
2004 nf_ct_expect_put(exp
);
2007 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2012 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2013 const struct nlmsghdr
*nlh
,
2014 const struct nlattr
* const cda
[])
2016 struct net
*net
= sock_net(ctnl
);
2017 struct nf_conntrack_tuple tuple
;
2018 struct nf_conntrack_expect
*exp
;
2019 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2020 u_int8_t u3
= nfmsg
->nfgen_family
;
2024 if (!cda
[CTA_EXPECT_TUPLE
]
2025 || !cda
[CTA_EXPECT_MASK
]
2026 || !cda
[CTA_EXPECT_MASTER
])
2029 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2033 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2037 spin_lock_bh(&nf_conntrack_lock
);
2038 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2041 spin_unlock_bh(&nf_conntrack_lock
);
2043 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2044 err
= ctnetlink_create_expect(net
, zone
, cda
,
2046 NETLINK_CB(skb
).pid
,
2053 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2054 err
= ctnetlink_change_expect(exp
, cda
);
2055 spin_unlock_bh(&nf_conntrack_lock
);
2060 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2061 static struct nf_ct_event_notifier ctnl_notifier
= {
2062 .fcn
= ctnetlink_conntrack_event
,
2065 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2066 .fcn
= ctnetlink_expect_event
,
2070 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2071 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2072 .attr_count
= CTA_MAX
,
2073 .policy
= ct_nla_policy
},
2074 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2075 .attr_count
= CTA_MAX
,
2076 .policy
= ct_nla_policy
},
2077 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2078 .attr_count
= CTA_MAX
,
2079 .policy
= ct_nla_policy
},
2080 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2081 .attr_count
= CTA_MAX
,
2082 .policy
= ct_nla_policy
},
2085 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2086 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2087 .attr_count
= CTA_EXPECT_MAX
,
2088 .policy
= exp_nla_policy
},
2089 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2090 .attr_count
= CTA_EXPECT_MAX
,
2091 .policy
= exp_nla_policy
},
2092 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2093 .attr_count
= CTA_EXPECT_MAX
,
2094 .policy
= exp_nla_policy
},
2097 static const struct nfnetlink_subsystem ctnl_subsys
= {
2098 .name
= "conntrack",
2099 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2100 .cb_count
= IPCTNL_MSG_MAX
,
2104 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2105 .name
= "conntrack_expect",
2106 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2107 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2111 MODULE_ALIAS("ip_conntrack_netlink");
2112 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2113 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2115 static int __init
ctnetlink_init(void)
2119 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2120 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2122 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2126 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2128 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2129 goto err_unreg_subsys
;
2132 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2133 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
2135 pr_err("ctnetlink_init: cannot register notifier.\n");
2136 goto err_unreg_exp_subsys
;
2139 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
2141 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2142 goto err_unreg_notifier
;
2148 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2150 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2151 err_unreg_exp_subsys
:
2152 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2155 nfnetlink_subsys_unregister(&ctnl_subsys
);
2160 static void __exit
ctnetlink_exit(void)
2162 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2164 nf_ct_remove_userspace_expectations();
2165 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2166 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2167 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2170 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2171 nfnetlink_subsys_unregister(&ctnl_subsys
);
2174 module_init(ctnetlink_init
);
2175 module_exit(ctnetlink_exit
);