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
;
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_rcu(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 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
659 /* Dump entries of a given L3 protocol number.
660 * If it is not specified, ie. l3proto == 0,
661 * then dump everything. */
662 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
669 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
671 IPCTNL_MSG_CT_NEW
, ct
) < 0) {
672 cb
->args
[1] = (unsigned long)ct
;
676 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
677 IPCTNL_MSG_CT_GET_CTRZERO
) {
678 struct nf_conn_counter
*acct
;
680 acct
= nf_conn_acct_find(ct
);
682 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
701 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
703 struct nlattr
*tb
[CTA_IP_MAX
+1];
704 struct nf_conntrack_l3proto
*l3proto
;
707 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
710 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
712 if (likely(l3proto
->nlattr_to_tuple
)) {
713 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
714 l3proto
->nla_policy
);
716 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
724 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
725 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
729 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
730 struct nf_conntrack_tuple
*tuple
)
732 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
733 struct nf_conntrack_l4proto
*l4proto
;
736 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
740 if (!tb
[CTA_PROTO_NUM
])
742 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
745 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
747 if (likely(l4proto
->nlattr_to_tuple
)) {
748 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
749 l4proto
->nla_policy
);
751 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
759 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
760 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
761 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
765 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
766 struct nf_conntrack_tuple
*tuple
,
767 enum ctattr_tuple type
, u_int8_t l3num
)
769 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
772 memset(tuple
, 0, sizeof(*tuple
));
774 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
776 if (!tb
[CTA_TUPLE_IP
])
779 tuple
->src
.l3num
= l3num
;
781 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
785 if (!tb
[CTA_TUPLE_PROTO
])
788 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
792 /* orig and expect tuples get DIR_ORIGINAL */
793 if (type
== CTA_TUPLE_REPLY
)
794 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
796 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
802 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
805 #ifdef CONFIG_NF_CONNTRACK_ZONES
806 *zone
= ntohs(nla_get_be16(attr
));
816 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
817 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
821 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
823 struct nlattr
*tb
[CTA_HELP_MAX
+1];
825 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
827 if (!tb
[CTA_HELP_NAME
])
830 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
835 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
836 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
837 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
838 [CTA_STATUS
] = { .type
= NLA_U32
},
839 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
840 [CTA_HELP
] = { .type
= NLA_NESTED
},
841 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
842 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
843 [CTA_MARK
] = { .type
= NLA_U32
},
844 [CTA_ID
] = { .type
= NLA_U32
},
845 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
846 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
847 [CTA_ZONE
] = { .type
= NLA_U16
},
851 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
852 const struct nlmsghdr
*nlh
,
853 const struct nlattr
* const cda
[])
855 struct net
*net
= sock_net(ctnl
);
856 struct nf_conntrack_tuple_hash
*h
;
857 struct nf_conntrack_tuple tuple
;
859 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
860 u_int8_t u3
= nfmsg
->nfgen_family
;
864 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
868 if (cda
[CTA_TUPLE_ORIG
])
869 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
870 else if (cda
[CTA_TUPLE_REPLY
])
871 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
873 /* Flush the whole table */
874 nf_conntrack_flush_report(net
,
883 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
887 ct
= nf_ct_tuplehash_to_ctrack(h
);
890 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
891 if (id
!= (u32
)(unsigned long)ct
) {
897 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
899 nlmsg_report(nlh
)) < 0) {
900 nf_ct_delete_from_lists(ct
);
901 /* we failed to report the event, try later */
902 nf_ct_insert_dying_list(ct
);
907 /* death_by_timeout would report the event again */
908 set_bit(IPS_DYING_BIT
, &ct
->status
);
917 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
918 const struct nlmsghdr
*nlh
,
919 const struct nlattr
* const cda
[])
921 struct net
*net
= sock_net(ctnl
);
922 struct nf_conntrack_tuple_hash
*h
;
923 struct nf_conntrack_tuple tuple
;
925 struct sk_buff
*skb2
= NULL
;
926 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
927 u_int8_t u3
= nfmsg
->nfgen_family
;
931 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
932 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
935 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
939 if (cda
[CTA_TUPLE_ORIG
])
940 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
941 else if (cda
[CTA_TUPLE_REPLY
])
942 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
949 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
953 ct
= nf_ct_tuplehash_to_ctrack(h
);
956 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
963 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
964 IPCTNL_MSG_CT_NEW
, ct
);
970 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
982 #ifdef CONFIG_NF_NAT_NEEDED
984 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
985 enum nf_nat_manip_type manip
,
986 const struct nlattr
*attr
)
988 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
990 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
991 if (!parse_nat_setup
) {
992 #ifdef CONFIG_MODULES
994 spin_unlock_bh(&nf_conntrack_lock
);
996 if (request_module("nf-nat-ipv4") < 0) {
998 spin_lock_bh(&nf_conntrack_lock
);
1003 spin_lock_bh(&nf_conntrack_lock
);
1005 if (nfnetlink_parse_nat_setup_hook
)
1011 return parse_nat_setup(ct
, manip
, attr
);
1016 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1019 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1020 d
= ct
->status
^ status
;
1022 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1026 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1027 /* SEEN_REPLY bit can only be set */
1030 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1031 /* ASSURED bit can only be set */
1034 /* Be careful here, modifying NAT bits can screw up things,
1035 * so don't let users modify them directly if they don't pass
1037 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1042 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1044 #ifdef CONFIG_NF_NAT_NEEDED
1047 if (cda
[CTA_NAT_DST
]) {
1048 ret
= ctnetlink_parse_nat_setup(ct
,
1054 if (cda
[CTA_NAT_SRC
]) {
1055 ret
= ctnetlink_parse_nat_setup(ct
,
1068 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1070 struct nf_conntrack_helper
*helper
;
1071 struct nf_conn_help
*help
= nfct_help(ct
);
1072 char *helpname
= NULL
;
1075 /* don't change helper of sibling connections */
1079 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1083 if (!strcmp(helpname
, "")) {
1084 if (help
&& help
->helper
) {
1085 /* we had a helper before ... */
1086 nf_ct_remove_expectations(ct
);
1087 rcu_assign_pointer(help
->helper
, NULL
);
1093 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1094 nf_ct_protonum(ct
));
1095 if (helper
== NULL
) {
1096 #ifdef CONFIG_MODULES
1097 spin_unlock_bh(&nf_conntrack_lock
);
1099 if (request_module("nfct-helper-%s", helpname
) < 0) {
1100 spin_lock_bh(&nf_conntrack_lock
);
1104 spin_lock_bh(&nf_conntrack_lock
);
1105 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1106 nf_ct_protonum(ct
));
1114 if (help
->helper
== helper
)
1118 /* need to zero data of old helper */
1119 memset(&help
->help
, 0, sizeof(help
->help
));
1121 /* we cannot set a helper for an existing conntrack */
1125 rcu_assign_pointer(help
->helper
, helper
);
1131 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1133 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1135 if (!del_timer(&ct
->timeout
))
1138 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1139 add_timer(&ct
->timeout
);
1144 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1145 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1146 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1147 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1151 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1153 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1154 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1155 struct nf_conntrack_l4proto
*l4proto
;
1158 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1161 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1162 if (l4proto
->from_nlattr
)
1163 err
= l4proto
->from_nlattr(tb
, ct
);
1169 #ifdef CONFIG_NF_NAT_NEEDED
1170 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1171 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1172 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1173 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1177 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1179 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1181 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1183 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1186 natseq
->correction_pos
=
1187 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1189 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1192 natseq
->offset_before
=
1193 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1195 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1198 natseq
->offset_after
=
1199 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1205 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1206 const struct nlattr
* const cda
[])
1209 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1214 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1215 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1216 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1220 ct
->status
|= IPS_SEQ_ADJUST
;
1223 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1224 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1225 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1229 ct
->status
|= IPS_SEQ_ADJUST
;
1237 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1238 const struct nlattr
* const cda
[])
1242 /* only allow NAT changes and master assignation for new conntracks */
1243 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1246 if (cda
[CTA_HELP
]) {
1247 err
= ctnetlink_change_helper(ct
, cda
);
1252 if (cda
[CTA_TIMEOUT
]) {
1253 err
= ctnetlink_change_timeout(ct
, cda
);
1258 if (cda
[CTA_STATUS
]) {
1259 err
= ctnetlink_change_status(ct
, cda
);
1264 if (cda
[CTA_PROTOINFO
]) {
1265 err
= ctnetlink_change_protoinfo(ct
, cda
);
1270 #if defined(CONFIG_NF_CONNTRACK_MARK)
1272 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1275 #ifdef CONFIG_NF_NAT_NEEDED
1276 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1277 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1286 static struct nf_conn
*
1287 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1288 const struct nlattr
* const cda
[],
1289 struct nf_conntrack_tuple
*otuple
,
1290 struct nf_conntrack_tuple
*rtuple
,
1295 struct nf_conntrack_helper
*helper
;
1297 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1299 return ERR_PTR(-ENOMEM
);
1301 if (!cda
[CTA_TIMEOUT
])
1303 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1305 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1308 if (cda
[CTA_HELP
]) {
1309 char *helpname
= NULL
;
1311 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1315 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1316 nf_ct_protonum(ct
));
1317 if (helper
== NULL
) {
1319 #ifdef CONFIG_MODULES
1320 if (request_module("nfct-helper-%s", helpname
) < 0) {
1326 helper
= __nf_conntrack_helper_find(helpname
,
1328 nf_ct_protonum(ct
));
1338 struct nf_conn_help
*help
;
1340 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1346 /* not in hash table yet so not strictly necessary */
1347 rcu_assign_pointer(help
->helper
, helper
);
1350 /* try an implicit helper assignation */
1351 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1356 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1357 err
= ctnetlink_change_nat(ct
, cda
);
1362 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1363 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1364 /* we must add conntrack extensions before confirmation. */
1365 ct
->status
|= IPS_CONFIRMED
;
1367 if (cda
[CTA_STATUS
]) {
1368 err
= ctnetlink_change_status(ct
, cda
);
1373 #ifdef CONFIG_NF_NAT_NEEDED
1374 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1375 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1381 if (cda
[CTA_PROTOINFO
]) {
1382 err
= ctnetlink_change_protoinfo(ct
, cda
);
1387 #if defined(CONFIG_NF_CONNTRACK_MARK)
1389 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1392 /* setup master conntrack: this is a confirmed expectation */
1393 if (cda
[CTA_TUPLE_MASTER
]) {
1394 struct nf_conntrack_tuple master
;
1395 struct nf_conntrack_tuple_hash
*master_h
;
1396 struct nf_conn
*master_ct
;
1398 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1402 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1403 if (master_h
== NULL
) {
1407 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1408 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1409 ct
->master
= master_ct
;
1412 add_timer(&ct
->timeout
);
1413 nf_conntrack_hash_insert(ct
);
1421 nf_conntrack_free(ct
);
1422 return ERR_PTR(err
);
1426 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1427 const struct nlmsghdr
*nlh
,
1428 const struct nlattr
* const cda
[])
1430 struct net
*net
= sock_net(ctnl
);
1431 struct nf_conntrack_tuple otuple
, rtuple
;
1432 struct nf_conntrack_tuple_hash
*h
= NULL
;
1433 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1434 u_int8_t u3
= nfmsg
->nfgen_family
;
1438 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1442 if (cda
[CTA_TUPLE_ORIG
]) {
1443 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1448 if (cda
[CTA_TUPLE_REPLY
]) {
1449 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1454 spin_lock_bh(&nf_conntrack_lock
);
1455 if (cda
[CTA_TUPLE_ORIG
])
1456 h
= __nf_conntrack_find(net
, zone
, &otuple
);
1457 else if (cda
[CTA_TUPLE_REPLY
])
1458 h
= __nf_conntrack_find(net
, zone
, &rtuple
);
1462 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1464 enum ip_conntrack_events events
;
1466 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1473 nf_conntrack_get(&ct
->ct_general
);
1474 spin_unlock_bh(&nf_conntrack_lock
);
1475 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1476 events
= IPCT_RELATED
;
1480 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1481 (1 << IPCT_ASSURED
) |
1482 (1 << IPCT_HELPER
) |
1483 (1 << IPCT_PROTOINFO
) |
1484 (1 << IPCT_NATSEQADJ
) |
1485 (1 << IPCT_MARK
) | events
,
1486 ct
, NETLINK_CB(skb
).pid
,
1490 spin_unlock_bh(&nf_conntrack_lock
);
1494 /* implicit 'else' */
1496 /* We manipulate the conntrack inside the global conntrack table lock,
1497 * so there's no need to increase the refcount */
1499 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1500 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1502 err
= ctnetlink_change_conntrack(ct
, cda
);
1504 nf_conntrack_get(&ct
->ct_general
);
1505 spin_unlock_bh(&nf_conntrack_lock
);
1506 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1507 (1 << IPCT_ASSURED
) |
1508 (1 << IPCT_HELPER
) |
1509 (1 << IPCT_PROTOINFO
) |
1510 (1 << IPCT_NATSEQADJ
) |
1512 ct
, NETLINK_CB(skb
).pid
,
1516 spin_unlock_bh(&nf_conntrack_lock
);
1522 spin_unlock_bh(&nf_conntrack_lock
);
1526 /***********************************************************************
1528 ***********************************************************************/
1531 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1532 const struct nf_conntrack_tuple
*tuple
,
1533 enum ctattr_expect type
)
1535 struct nlattr
*nest_parms
;
1537 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1539 goto nla_put_failure
;
1540 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1541 goto nla_put_failure
;
1542 nla_nest_end(skb
, nest_parms
);
1551 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1552 const struct nf_conntrack_tuple
*tuple
,
1553 const struct nf_conntrack_tuple_mask
*mask
)
1556 struct nf_conntrack_l3proto
*l3proto
;
1557 struct nf_conntrack_l4proto
*l4proto
;
1558 struct nf_conntrack_tuple m
;
1559 struct nlattr
*nest_parms
;
1561 memset(&m
, 0xFF, sizeof(m
));
1562 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1563 m
.src
.u
.all
= mask
->src
.u
.all
;
1564 m
.dst
.protonum
= tuple
->dst
.protonum
;
1566 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1568 goto nla_put_failure
;
1570 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1571 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1573 if (unlikely(ret
< 0))
1574 goto nla_put_failure
;
1576 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1577 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1578 if (unlikely(ret
< 0))
1579 goto nla_put_failure
;
1581 nla_nest_end(skb
, nest_parms
);
1590 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1591 const struct nf_conntrack_expect
*exp
)
1593 struct nf_conn
*master
= exp
->master
;
1594 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1595 struct nf_conn_help
*help
;
1600 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1601 goto nla_put_failure
;
1602 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1603 goto nla_put_failure
;
1604 if (ctnetlink_exp_dump_tuple(skb
,
1605 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1606 CTA_EXPECT_MASTER
) < 0)
1607 goto nla_put_failure
;
1609 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1610 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1611 NLA_PUT_BE32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
));
1612 help
= nfct_help(master
);
1614 struct nf_conntrack_helper
*helper
;
1616 helper
= rcu_dereference(help
->helper
);
1618 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1628 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1629 int event
, const struct nf_conntrack_expect
*exp
)
1631 struct nlmsghdr
*nlh
;
1632 struct nfgenmsg
*nfmsg
;
1633 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1635 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1636 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1640 nfmsg
= nlmsg_data(nlh
);
1641 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1642 nfmsg
->version
= NFNETLINK_V0
;
1645 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1646 goto nla_put_failure
;
1648 nlmsg_end(skb
, nlh
);
1653 nlmsg_cancel(skb
, nlh
);
1657 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1659 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1661 struct nf_conntrack_expect
*exp
= item
->exp
;
1662 struct net
*net
= nf_ct_exp_net(exp
);
1663 struct nlmsghdr
*nlh
;
1664 struct nfgenmsg
*nfmsg
;
1665 struct sk_buff
*skb
;
1666 unsigned int type
, group
;
1669 if (events
& (1 << IPEXP_DESTROY
)) {
1670 type
= IPCTNL_MSG_EXP_DELETE
;
1671 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
1672 } else if (events
& (1 << IPEXP_NEW
)) {
1673 type
= IPCTNL_MSG_EXP_NEW
;
1674 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1675 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
1679 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
1682 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1686 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1687 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1691 nfmsg
= nlmsg_data(nlh
);
1692 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1693 nfmsg
->version
= NFNETLINK_V0
;
1697 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1698 goto nla_put_failure
;
1701 nlmsg_end(skb
, nlh
);
1702 nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
, GFP_ATOMIC
);
1707 nlmsg_cancel(skb
, nlh
);
1711 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1715 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1718 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1723 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1725 struct net
*net
= sock_net(skb
->sk
);
1726 struct nf_conntrack_expect
*exp
, *last
;
1727 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1728 struct hlist_node
*n
;
1729 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1732 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1733 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1735 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1737 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1744 if (ctnetlink_exp_fill_info(skb
,
1745 NETLINK_CB(cb
->skb
).pid
,
1749 if (!atomic_inc_not_zero(&exp
->use
))
1751 cb
->args
[1] = (unsigned long)exp
;
1763 nf_ct_expect_put(last
);
1768 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1769 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1770 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1771 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1772 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1773 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1774 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1775 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
1776 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
1780 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1781 const struct nlmsghdr
*nlh
,
1782 const struct nlattr
* const cda
[])
1784 struct net
*net
= sock_net(ctnl
);
1785 struct nf_conntrack_tuple tuple
;
1786 struct nf_conntrack_expect
*exp
;
1787 struct sk_buff
*skb2
;
1788 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1789 u_int8_t u3
= nfmsg
->nfgen_family
;
1793 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1794 return netlink_dump_start(ctnl
, skb
, nlh
,
1795 ctnetlink_exp_dump_table
,
1796 ctnetlink_exp_done
);
1799 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1803 if (cda
[CTA_EXPECT_MASTER
])
1804 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1811 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1815 if (cda
[CTA_EXPECT_ID
]) {
1816 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1817 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1818 nf_ct_expect_put(exp
);
1824 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1829 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1830 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1835 nf_ct_expect_put(exp
);
1837 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1842 nf_ct_expect_put(exp
);
1847 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1848 const struct nlmsghdr
*nlh
,
1849 const struct nlattr
* const cda
[])
1851 struct net
*net
= sock_net(ctnl
);
1852 struct nf_conntrack_expect
*exp
;
1853 struct nf_conntrack_tuple tuple
;
1854 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1855 struct hlist_node
*n
, *next
;
1856 u_int8_t u3
= nfmsg
->nfgen_family
;
1861 if (cda
[CTA_EXPECT_TUPLE
]) {
1862 /* delete a single expect by tuple */
1863 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1867 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1871 /* bump usage count to 2 */
1872 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1876 if (cda
[CTA_EXPECT_ID
]) {
1877 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1878 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1879 nf_ct_expect_put(exp
);
1884 /* after list removal, usage count == 1 */
1885 spin_lock_bh(&nf_conntrack_lock
);
1886 if (del_timer(&exp
->timeout
)) {
1887 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).pid
,
1889 nf_ct_expect_put(exp
);
1891 spin_unlock_bh(&nf_conntrack_lock
);
1892 /* have to put what we 'get' above.
1893 * after this line usage count == 0 */
1894 nf_ct_expect_put(exp
);
1895 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1896 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1897 struct nf_conn_help
*m_help
;
1899 /* delete all expectations for this helper */
1900 spin_lock_bh(&nf_conntrack_lock
);
1901 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1902 hlist_for_each_entry_safe(exp
, n
, next
,
1903 &net
->ct
.expect_hash
[i
],
1905 m_help
= nfct_help(exp
->master
);
1906 if (!strcmp(m_help
->helper
->name
, name
) &&
1907 del_timer(&exp
->timeout
)) {
1908 nf_ct_unlink_expect_report(exp
,
1909 NETLINK_CB(skb
).pid
,
1911 nf_ct_expect_put(exp
);
1915 spin_unlock_bh(&nf_conntrack_lock
);
1917 /* This basically means we have to flush everything*/
1918 spin_lock_bh(&nf_conntrack_lock
);
1919 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1920 hlist_for_each_entry_safe(exp
, n
, next
,
1921 &net
->ct
.expect_hash
[i
],
1923 if (del_timer(&exp
->timeout
)) {
1924 nf_ct_unlink_expect_report(exp
,
1925 NETLINK_CB(skb
).pid
,
1927 nf_ct_expect_put(exp
);
1931 spin_unlock_bh(&nf_conntrack_lock
);
1937 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
1938 const struct nlattr
* const cda
[])
1944 ctnetlink_create_expect(struct net
*net
, u16 zone
,
1945 const struct nlattr
* const cda
[],
1947 u32 pid
, int report
)
1949 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1950 struct nf_conntrack_tuple_hash
*h
= NULL
;
1951 struct nf_conntrack_expect
*exp
;
1953 struct nf_conn_help
*help
;
1956 /* caller guarantees that those three CTA_EXPECT_* exist */
1957 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1960 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1963 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1967 /* Look for master conntrack of this expectation */
1968 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
1971 ct
= nf_ct_tuplehash_to_ctrack(h
);
1972 exp
= nf_ct_expect_alloc(ct
);
1977 help
= nfct_help(ct
);
1979 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
1983 exp
->timeout
.expires
=
1984 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
1986 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
1987 if (cda
[CTA_EXPECT_FLAGS
]) {
1989 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1992 if (cda
[CTA_EXPECT_FLAGS
]) {
1993 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1994 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
2000 exp
->expectfn
= NULL
;
2003 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2004 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2005 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2007 err
= nf_ct_expect_related_report(exp
, pid
, report
);
2008 nf_ct_expect_put(exp
);
2011 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2016 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2017 const struct nlmsghdr
*nlh
,
2018 const struct nlattr
* const cda
[])
2020 struct net
*net
= sock_net(ctnl
);
2021 struct nf_conntrack_tuple tuple
;
2022 struct nf_conntrack_expect
*exp
;
2023 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2024 u_int8_t u3
= nfmsg
->nfgen_family
;
2028 if (!cda
[CTA_EXPECT_TUPLE
]
2029 || !cda
[CTA_EXPECT_MASK
]
2030 || !cda
[CTA_EXPECT_MASTER
])
2033 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2037 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2041 spin_lock_bh(&nf_conntrack_lock
);
2042 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2045 spin_unlock_bh(&nf_conntrack_lock
);
2047 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2048 err
= ctnetlink_create_expect(net
, zone
, cda
,
2050 NETLINK_CB(skb
).pid
,
2057 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2058 err
= ctnetlink_change_expect(exp
, cda
);
2059 spin_unlock_bh(&nf_conntrack_lock
);
2064 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2065 static struct nf_ct_event_notifier ctnl_notifier
= {
2066 .fcn
= ctnetlink_conntrack_event
,
2069 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2070 .fcn
= ctnetlink_expect_event
,
2074 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2075 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2076 .attr_count
= CTA_MAX
,
2077 .policy
= ct_nla_policy
},
2078 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2079 .attr_count
= CTA_MAX
,
2080 .policy
= ct_nla_policy
},
2081 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2082 .attr_count
= CTA_MAX
,
2083 .policy
= ct_nla_policy
},
2084 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2085 .attr_count
= CTA_MAX
,
2086 .policy
= ct_nla_policy
},
2089 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2090 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2091 .attr_count
= CTA_EXPECT_MAX
,
2092 .policy
= exp_nla_policy
},
2093 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2094 .attr_count
= CTA_EXPECT_MAX
,
2095 .policy
= exp_nla_policy
},
2096 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2097 .attr_count
= CTA_EXPECT_MAX
,
2098 .policy
= exp_nla_policy
},
2101 static const struct nfnetlink_subsystem ctnl_subsys
= {
2102 .name
= "conntrack",
2103 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2104 .cb_count
= IPCTNL_MSG_MAX
,
2108 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2109 .name
= "conntrack_expect",
2110 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2111 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2115 MODULE_ALIAS("ip_conntrack_netlink");
2116 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2117 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2119 static int __init
ctnetlink_init(void)
2123 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2124 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2126 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2130 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2132 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2133 goto err_unreg_subsys
;
2136 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2137 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
2139 pr_err("ctnetlink_init: cannot register notifier.\n");
2140 goto err_unreg_exp_subsys
;
2143 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
2145 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2146 goto err_unreg_notifier
;
2152 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2154 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2155 err_unreg_exp_subsys
:
2156 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2159 nfnetlink_subsys_unregister(&ctnl_subsys
);
2164 static void __exit
ctnetlink_exit(void)
2166 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2168 nf_ct_remove_userspace_expectations();
2169 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2170 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2171 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2174 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2175 nfnetlink_subsys_unregister(&ctnl_subsys
);
2178 module_init(ctnetlink_init
);
2179 module_exit(ctnetlink_exit
);