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 nf_conntrack_get(&ct
->ct_general
);
671 cb
->args
[1] = (unsigned long)ct
;
675 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
676 IPCTNL_MSG_CT_GET_CTRZERO
) {
677 struct nf_conn_counter
*acct
;
679 acct
= nf_conn_acct_find(ct
);
681 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
690 spin_unlock_bh(&nf_conntrack_lock
);
698 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
700 struct nlattr
*tb
[CTA_IP_MAX
+1];
701 struct nf_conntrack_l3proto
*l3proto
;
704 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
707 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
709 if (likely(l3proto
->nlattr_to_tuple
)) {
710 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
711 l3proto
->nla_policy
);
713 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
721 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
722 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
726 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
727 struct nf_conntrack_tuple
*tuple
)
729 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
730 struct nf_conntrack_l4proto
*l4proto
;
733 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
737 if (!tb
[CTA_PROTO_NUM
])
739 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
742 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
744 if (likely(l4proto
->nlattr_to_tuple
)) {
745 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
746 l4proto
->nla_policy
);
748 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
756 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
757 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
758 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
762 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
763 struct nf_conntrack_tuple
*tuple
,
764 enum ctattr_tuple type
, u_int8_t l3num
)
766 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
769 memset(tuple
, 0, sizeof(*tuple
));
771 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
773 if (!tb
[CTA_TUPLE_IP
])
776 tuple
->src
.l3num
= l3num
;
778 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
782 if (!tb
[CTA_TUPLE_PROTO
])
785 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
789 /* orig and expect tuples get DIR_ORIGINAL */
790 if (type
== CTA_TUPLE_REPLY
)
791 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
793 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
799 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
802 #ifdef CONFIG_NF_CONNTRACK_ZONES
803 *zone
= ntohs(nla_get_be16(attr
));
813 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
814 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
818 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
820 struct nlattr
*tb
[CTA_HELP_MAX
+1];
822 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
824 if (!tb
[CTA_HELP_NAME
])
827 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
832 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
833 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
834 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
835 [CTA_STATUS
] = { .type
= NLA_U32
},
836 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
837 [CTA_HELP
] = { .type
= NLA_NESTED
},
838 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
839 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
840 [CTA_MARK
] = { .type
= NLA_U32
},
841 [CTA_ID
] = { .type
= NLA_U32
},
842 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
843 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
844 [CTA_ZONE
] = { .type
= NLA_U16
},
848 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
849 const struct nlmsghdr
*nlh
,
850 const struct nlattr
* const cda
[])
852 struct net
*net
= sock_net(ctnl
);
853 struct nf_conntrack_tuple_hash
*h
;
854 struct nf_conntrack_tuple tuple
;
856 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
857 u_int8_t u3
= nfmsg
->nfgen_family
;
861 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
865 if (cda
[CTA_TUPLE_ORIG
])
866 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
867 else if (cda
[CTA_TUPLE_REPLY
])
868 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
870 /* Flush the whole table */
871 nf_conntrack_flush_report(net
,
880 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
884 ct
= nf_ct_tuplehash_to_ctrack(h
);
887 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
888 if (id
!= (u32
)(unsigned long)ct
) {
894 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
896 nlmsg_report(nlh
)) < 0) {
897 nf_ct_delete_from_lists(ct
);
898 /* we failed to report the event, try later */
899 nf_ct_insert_dying_list(ct
);
904 /* death_by_timeout would report the event again */
905 set_bit(IPS_DYING_BIT
, &ct
->status
);
914 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
915 const struct nlmsghdr
*nlh
,
916 const struct nlattr
* const cda
[])
918 struct net
*net
= sock_net(ctnl
);
919 struct nf_conntrack_tuple_hash
*h
;
920 struct nf_conntrack_tuple tuple
;
922 struct sk_buff
*skb2
= NULL
;
923 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
924 u_int8_t u3
= nfmsg
->nfgen_family
;
928 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
929 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
932 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
936 if (cda
[CTA_TUPLE_ORIG
])
937 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
938 else if (cda
[CTA_TUPLE_REPLY
])
939 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
946 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
950 ct
= nf_ct_tuplehash_to_ctrack(h
);
953 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
960 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
961 IPCTNL_MSG_CT_NEW
, ct
);
967 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
976 /* this avoids a loop in nfnetlink. */
977 return err
== -EAGAIN
? -ENOBUFS
: err
;
980 #ifdef CONFIG_NF_NAT_NEEDED
982 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
983 enum nf_nat_manip_type manip
,
984 const struct nlattr
*attr
)
986 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
988 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
989 if (!parse_nat_setup
) {
990 #ifdef CONFIG_MODULES
992 spin_unlock_bh(&nf_conntrack_lock
);
994 if (request_module("nf-nat-ipv4") < 0) {
996 spin_lock_bh(&nf_conntrack_lock
);
1001 spin_lock_bh(&nf_conntrack_lock
);
1003 if (nfnetlink_parse_nat_setup_hook
)
1009 return parse_nat_setup(ct
, manip
, attr
);
1014 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1017 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1018 d
= ct
->status
^ status
;
1020 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1024 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1025 /* SEEN_REPLY bit can only be set */
1028 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1029 /* ASSURED bit can only be set */
1032 /* Be careful here, modifying NAT bits can screw up things,
1033 * so don't let users modify them directly if they don't pass
1035 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1040 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1042 #ifdef CONFIG_NF_NAT_NEEDED
1045 if (cda
[CTA_NAT_DST
]) {
1046 ret
= ctnetlink_parse_nat_setup(ct
,
1052 if (cda
[CTA_NAT_SRC
]) {
1053 ret
= ctnetlink_parse_nat_setup(ct
,
1066 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1068 struct nf_conntrack_helper
*helper
;
1069 struct nf_conn_help
*help
= nfct_help(ct
);
1070 char *helpname
= NULL
;
1073 /* don't change helper of sibling connections */
1077 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1081 if (!strcmp(helpname
, "")) {
1082 if (help
&& help
->helper
) {
1083 /* we had a helper before ... */
1084 nf_ct_remove_expectations(ct
);
1085 rcu_assign_pointer(help
->helper
, NULL
);
1091 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1092 nf_ct_protonum(ct
));
1093 if (helper
== NULL
) {
1094 #ifdef CONFIG_MODULES
1095 spin_unlock_bh(&nf_conntrack_lock
);
1097 if (request_module("nfct-helper-%s", helpname
) < 0) {
1098 spin_lock_bh(&nf_conntrack_lock
);
1102 spin_lock_bh(&nf_conntrack_lock
);
1103 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1104 nf_ct_protonum(ct
));
1112 if (help
->helper
== helper
)
1116 /* need to zero data of old helper */
1117 memset(&help
->help
, 0, sizeof(help
->help
));
1119 /* we cannot set a helper for an existing conntrack */
1123 rcu_assign_pointer(help
->helper
, helper
);
1129 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1131 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1133 if (!del_timer(&ct
->timeout
))
1136 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1137 add_timer(&ct
->timeout
);
1142 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1143 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1144 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1145 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1149 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1151 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1152 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1153 struct nf_conntrack_l4proto
*l4proto
;
1156 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1159 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1160 if (l4proto
->from_nlattr
)
1161 err
= l4proto
->from_nlattr(tb
, ct
);
1167 #ifdef CONFIG_NF_NAT_NEEDED
1168 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1169 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1170 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1171 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1175 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1177 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1179 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1181 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1184 natseq
->correction_pos
=
1185 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1187 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1190 natseq
->offset_before
=
1191 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1193 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1196 natseq
->offset_after
=
1197 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1203 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1204 const struct nlattr
* const cda
[])
1207 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1212 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1213 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1214 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1218 ct
->status
|= IPS_SEQ_ADJUST
;
1221 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1222 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1223 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1227 ct
->status
|= IPS_SEQ_ADJUST
;
1235 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1236 const struct nlattr
* const cda
[])
1240 /* only allow NAT changes and master assignation for new conntracks */
1241 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1244 if (cda
[CTA_HELP
]) {
1245 err
= ctnetlink_change_helper(ct
, cda
);
1250 if (cda
[CTA_TIMEOUT
]) {
1251 err
= ctnetlink_change_timeout(ct
, cda
);
1256 if (cda
[CTA_STATUS
]) {
1257 err
= ctnetlink_change_status(ct
, cda
);
1262 if (cda
[CTA_PROTOINFO
]) {
1263 err
= ctnetlink_change_protoinfo(ct
, cda
);
1268 #if defined(CONFIG_NF_CONNTRACK_MARK)
1270 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1273 #ifdef CONFIG_NF_NAT_NEEDED
1274 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1275 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1284 static struct nf_conn
*
1285 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1286 const struct nlattr
* const cda
[],
1287 struct nf_conntrack_tuple
*otuple
,
1288 struct nf_conntrack_tuple
*rtuple
,
1293 struct nf_conntrack_helper
*helper
;
1295 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1297 return ERR_PTR(-ENOMEM
);
1299 if (!cda
[CTA_TIMEOUT
])
1301 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1303 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1306 if (cda
[CTA_HELP
]) {
1307 char *helpname
= NULL
;
1309 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1313 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1314 nf_ct_protonum(ct
));
1315 if (helper
== NULL
) {
1317 #ifdef CONFIG_MODULES
1318 if (request_module("nfct-helper-%s", helpname
) < 0) {
1324 helper
= __nf_conntrack_helper_find(helpname
,
1326 nf_ct_protonum(ct
));
1336 struct nf_conn_help
*help
;
1338 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1344 /* not in hash table yet so not strictly necessary */
1345 rcu_assign_pointer(help
->helper
, helper
);
1348 /* try an implicit helper assignation */
1349 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1354 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1355 err
= ctnetlink_change_nat(ct
, cda
);
1360 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1361 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1362 /* we must add conntrack extensions before confirmation. */
1363 ct
->status
|= IPS_CONFIRMED
;
1365 if (cda
[CTA_STATUS
]) {
1366 err
= ctnetlink_change_status(ct
, cda
);
1371 #ifdef CONFIG_NF_NAT_NEEDED
1372 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1373 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1379 if (cda
[CTA_PROTOINFO
]) {
1380 err
= ctnetlink_change_protoinfo(ct
, cda
);
1385 #if defined(CONFIG_NF_CONNTRACK_MARK)
1387 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1390 /* setup master conntrack: this is a confirmed expectation */
1391 if (cda
[CTA_TUPLE_MASTER
]) {
1392 struct nf_conntrack_tuple master
;
1393 struct nf_conntrack_tuple_hash
*master_h
;
1394 struct nf_conn
*master_ct
;
1396 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1400 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1401 if (master_h
== NULL
) {
1405 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1406 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1407 ct
->master
= master_ct
;
1410 add_timer(&ct
->timeout
);
1411 nf_conntrack_hash_insert(ct
);
1419 nf_conntrack_free(ct
);
1420 return ERR_PTR(err
);
1424 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1425 const struct nlmsghdr
*nlh
,
1426 const struct nlattr
* const cda
[])
1428 struct net
*net
= sock_net(ctnl
);
1429 struct nf_conntrack_tuple otuple
, rtuple
;
1430 struct nf_conntrack_tuple_hash
*h
= NULL
;
1431 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1432 u_int8_t u3
= nfmsg
->nfgen_family
;
1436 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1440 if (cda
[CTA_TUPLE_ORIG
]) {
1441 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1446 if (cda
[CTA_TUPLE_REPLY
]) {
1447 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1452 spin_lock_bh(&nf_conntrack_lock
);
1453 if (cda
[CTA_TUPLE_ORIG
])
1454 h
= __nf_conntrack_find(net
, zone
, &otuple
);
1455 else if (cda
[CTA_TUPLE_REPLY
])
1456 h
= __nf_conntrack_find(net
, zone
, &rtuple
);
1460 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1462 enum ip_conntrack_events events
;
1464 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1471 nf_conntrack_get(&ct
->ct_general
);
1472 spin_unlock_bh(&nf_conntrack_lock
);
1473 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1474 events
= IPCT_RELATED
;
1478 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1479 (1 << IPCT_ASSURED
) |
1480 (1 << IPCT_HELPER
) |
1481 (1 << IPCT_PROTOINFO
) |
1482 (1 << IPCT_NATSEQADJ
) |
1483 (1 << IPCT_MARK
) | events
,
1484 ct
, NETLINK_CB(skb
).pid
,
1488 spin_unlock_bh(&nf_conntrack_lock
);
1492 /* implicit 'else' */
1494 /* We manipulate the conntrack inside the global conntrack table lock,
1495 * so there's no need to increase the refcount */
1497 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1498 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1500 err
= ctnetlink_change_conntrack(ct
, cda
);
1502 nf_conntrack_get(&ct
->ct_general
);
1503 spin_unlock_bh(&nf_conntrack_lock
);
1504 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1505 (1 << IPCT_ASSURED
) |
1506 (1 << IPCT_HELPER
) |
1507 (1 << IPCT_PROTOINFO
) |
1508 (1 << IPCT_NATSEQADJ
) |
1510 ct
, NETLINK_CB(skb
).pid
,
1514 spin_unlock_bh(&nf_conntrack_lock
);
1520 spin_unlock_bh(&nf_conntrack_lock
);
1524 /***********************************************************************
1526 ***********************************************************************/
1529 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1530 const struct nf_conntrack_tuple
*tuple
,
1531 enum ctattr_expect type
)
1533 struct nlattr
*nest_parms
;
1535 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1537 goto nla_put_failure
;
1538 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1539 goto nla_put_failure
;
1540 nla_nest_end(skb
, nest_parms
);
1549 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1550 const struct nf_conntrack_tuple
*tuple
,
1551 const struct nf_conntrack_tuple_mask
*mask
)
1554 struct nf_conntrack_l3proto
*l3proto
;
1555 struct nf_conntrack_l4proto
*l4proto
;
1556 struct nf_conntrack_tuple m
;
1557 struct nlattr
*nest_parms
;
1559 memset(&m
, 0xFF, sizeof(m
));
1560 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1561 m
.src
.u
.all
= mask
->src
.u
.all
;
1562 m
.dst
.protonum
= tuple
->dst
.protonum
;
1564 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1566 goto nla_put_failure
;
1568 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1569 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1571 if (unlikely(ret
< 0))
1572 goto nla_put_failure
;
1574 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1575 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1576 if (unlikely(ret
< 0))
1577 goto nla_put_failure
;
1579 nla_nest_end(skb
, nest_parms
);
1588 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1589 const struct nf_conntrack_expect
*exp
)
1591 struct nf_conn
*master
= exp
->master
;
1592 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1593 struct nf_conn_help
*help
;
1598 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1599 goto nla_put_failure
;
1600 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1601 goto nla_put_failure
;
1602 if (ctnetlink_exp_dump_tuple(skb
,
1603 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1604 CTA_EXPECT_MASTER
) < 0)
1605 goto nla_put_failure
;
1607 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1608 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1609 NLA_PUT_BE32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
));
1610 help
= nfct_help(master
);
1612 struct nf_conntrack_helper
*helper
;
1614 helper
= rcu_dereference(help
->helper
);
1616 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1626 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1627 int event
, const struct nf_conntrack_expect
*exp
)
1629 struct nlmsghdr
*nlh
;
1630 struct nfgenmsg
*nfmsg
;
1631 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1633 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1634 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1638 nfmsg
= nlmsg_data(nlh
);
1639 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1640 nfmsg
->version
= NFNETLINK_V0
;
1643 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1644 goto nla_put_failure
;
1646 nlmsg_end(skb
, nlh
);
1651 nlmsg_cancel(skb
, nlh
);
1655 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1657 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1659 struct nf_conntrack_expect
*exp
= item
->exp
;
1660 struct net
*net
= nf_ct_exp_net(exp
);
1661 struct nlmsghdr
*nlh
;
1662 struct nfgenmsg
*nfmsg
;
1663 struct sk_buff
*skb
;
1664 unsigned int type
, group
;
1667 if (events
& (1 << IPEXP_DESTROY
)) {
1668 type
= IPCTNL_MSG_EXP_DELETE
;
1669 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
1670 } else if (events
& (1 << IPEXP_NEW
)) {
1671 type
= IPCTNL_MSG_EXP_NEW
;
1672 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1673 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
1677 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
1680 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1684 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1685 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1689 nfmsg
= nlmsg_data(nlh
);
1690 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1691 nfmsg
->version
= NFNETLINK_V0
;
1695 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1696 goto nla_put_failure
;
1699 nlmsg_end(skb
, nlh
);
1700 nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
, GFP_ATOMIC
);
1705 nlmsg_cancel(skb
, nlh
);
1709 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1713 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1716 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1721 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1723 struct net
*net
= sock_net(skb
->sk
);
1724 struct nf_conntrack_expect
*exp
, *last
;
1725 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1726 struct hlist_node
*n
;
1727 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1730 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1731 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1733 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1735 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1742 if (ctnetlink_exp_fill_info(skb
,
1743 NETLINK_CB(cb
->skb
).pid
,
1747 if (!atomic_inc_not_zero(&exp
->use
))
1749 cb
->args
[1] = (unsigned long)exp
;
1761 nf_ct_expect_put(last
);
1766 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1767 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1768 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1769 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1770 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1771 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1772 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1773 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
1774 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
1778 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1779 const struct nlmsghdr
*nlh
,
1780 const struct nlattr
* const cda
[])
1782 struct net
*net
= sock_net(ctnl
);
1783 struct nf_conntrack_tuple tuple
;
1784 struct nf_conntrack_expect
*exp
;
1785 struct sk_buff
*skb2
;
1786 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1787 u_int8_t u3
= nfmsg
->nfgen_family
;
1791 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1792 return netlink_dump_start(ctnl
, skb
, nlh
,
1793 ctnetlink_exp_dump_table
,
1794 ctnetlink_exp_done
);
1797 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1801 if (cda
[CTA_EXPECT_MASTER
])
1802 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1809 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1813 if (cda
[CTA_EXPECT_ID
]) {
1814 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1815 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1816 nf_ct_expect_put(exp
);
1822 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1827 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1828 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1833 nf_ct_expect_put(exp
);
1835 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1840 nf_ct_expect_put(exp
);
1845 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1846 const struct nlmsghdr
*nlh
,
1847 const struct nlattr
* const cda
[])
1849 struct net
*net
= sock_net(ctnl
);
1850 struct nf_conntrack_expect
*exp
;
1851 struct nf_conntrack_tuple tuple
;
1852 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1853 struct hlist_node
*n
, *next
;
1854 u_int8_t u3
= nfmsg
->nfgen_family
;
1859 if (cda
[CTA_EXPECT_TUPLE
]) {
1860 /* delete a single expect by tuple */
1861 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1865 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1869 /* bump usage count to 2 */
1870 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1874 if (cda
[CTA_EXPECT_ID
]) {
1875 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1876 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1877 nf_ct_expect_put(exp
);
1882 /* after list removal, usage count == 1 */
1883 spin_lock_bh(&nf_conntrack_lock
);
1884 if (del_timer(&exp
->timeout
)) {
1885 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).pid
,
1887 nf_ct_expect_put(exp
);
1889 spin_unlock_bh(&nf_conntrack_lock
);
1890 /* have to put what we 'get' above.
1891 * after this line usage count == 0 */
1892 nf_ct_expect_put(exp
);
1893 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1894 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1895 struct nf_conn_help
*m_help
;
1897 /* delete all expectations for this helper */
1898 spin_lock_bh(&nf_conntrack_lock
);
1899 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1900 hlist_for_each_entry_safe(exp
, n
, next
,
1901 &net
->ct
.expect_hash
[i
],
1903 m_help
= nfct_help(exp
->master
);
1904 if (!strcmp(m_help
->helper
->name
, name
) &&
1905 del_timer(&exp
->timeout
)) {
1906 nf_ct_unlink_expect_report(exp
,
1907 NETLINK_CB(skb
).pid
,
1909 nf_ct_expect_put(exp
);
1913 spin_unlock_bh(&nf_conntrack_lock
);
1915 /* This basically means we have to flush everything*/
1916 spin_lock_bh(&nf_conntrack_lock
);
1917 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1918 hlist_for_each_entry_safe(exp
, n
, next
,
1919 &net
->ct
.expect_hash
[i
],
1921 if (del_timer(&exp
->timeout
)) {
1922 nf_ct_unlink_expect_report(exp
,
1923 NETLINK_CB(skb
).pid
,
1925 nf_ct_expect_put(exp
);
1929 spin_unlock_bh(&nf_conntrack_lock
);
1935 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
1936 const struct nlattr
* const cda
[])
1942 ctnetlink_create_expect(struct net
*net
, u16 zone
,
1943 const struct nlattr
* const cda
[],
1945 u32 pid
, int report
)
1947 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1948 struct nf_conntrack_tuple_hash
*h
= NULL
;
1949 struct nf_conntrack_expect
*exp
;
1951 struct nf_conn_help
*help
;
1954 /* caller guarantees that those three CTA_EXPECT_* exist */
1955 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1958 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1961 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1965 /* Look for master conntrack of this expectation */
1966 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
1969 ct
= nf_ct_tuplehash_to_ctrack(h
);
1970 exp
= nf_ct_expect_alloc(ct
);
1975 help
= nfct_help(ct
);
1977 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
1981 exp
->timeout
.expires
=
1982 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
1984 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
1985 if (cda
[CTA_EXPECT_FLAGS
]) {
1987 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1990 if (cda
[CTA_EXPECT_FLAGS
]) {
1991 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
1992 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
1998 exp
->expectfn
= NULL
;
2001 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2002 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2003 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2005 err
= nf_ct_expect_related_report(exp
, pid
, report
);
2006 nf_ct_expect_put(exp
);
2009 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2014 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2015 const struct nlmsghdr
*nlh
,
2016 const struct nlattr
* const cda
[])
2018 struct net
*net
= sock_net(ctnl
);
2019 struct nf_conntrack_tuple tuple
;
2020 struct nf_conntrack_expect
*exp
;
2021 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2022 u_int8_t u3
= nfmsg
->nfgen_family
;
2026 if (!cda
[CTA_EXPECT_TUPLE
]
2027 || !cda
[CTA_EXPECT_MASK
]
2028 || !cda
[CTA_EXPECT_MASTER
])
2031 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2035 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2039 spin_lock_bh(&nf_conntrack_lock
);
2040 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2043 spin_unlock_bh(&nf_conntrack_lock
);
2045 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2046 err
= ctnetlink_create_expect(net
, zone
, cda
,
2048 NETLINK_CB(skb
).pid
,
2055 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2056 err
= ctnetlink_change_expect(exp
, cda
);
2057 spin_unlock_bh(&nf_conntrack_lock
);
2062 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2063 static struct nf_ct_event_notifier ctnl_notifier
= {
2064 .fcn
= ctnetlink_conntrack_event
,
2067 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2068 .fcn
= ctnetlink_expect_event
,
2072 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2073 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2074 .attr_count
= CTA_MAX
,
2075 .policy
= ct_nla_policy
},
2076 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2077 .attr_count
= CTA_MAX
,
2078 .policy
= ct_nla_policy
},
2079 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2080 .attr_count
= CTA_MAX
,
2081 .policy
= ct_nla_policy
},
2082 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2083 .attr_count
= CTA_MAX
,
2084 .policy
= ct_nla_policy
},
2087 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2088 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2089 .attr_count
= CTA_EXPECT_MAX
,
2090 .policy
= exp_nla_policy
},
2091 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2092 .attr_count
= CTA_EXPECT_MAX
,
2093 .policy
= exp_nla_policy
},
2094 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2095 .attr_count
= CTA_EXPECT_MAX
,
2096 .policy
= exp_nla_policy
},
2099 static const struct nfnetlink_subsystem ctnl_subsys
= {
2100 .name
= "conntrack",
2101 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2102 .cb_count
= IPCTNL_MSG_MAX
,
2106 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2107 .name
= "conntrack_expect",
2108 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2109 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2113 MODULE_ALIAS("ip_conntrack_netlink");
2114 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2115 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2117 static int __init
ctnetlink_init(void)
2121 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2122 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2124 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2128 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2130 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2131 goto err_unreg_subsys
;
2134 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2135 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
2137 pr_err("ctnetlink_init: cannot register notifier.\n");
2138 goto err_unreg_exp_subsys
;
2141 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
2143 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2144 goto err_unreg_notifier
;
2150 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2152 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2153 err_unreg_exp_subsys
:
2154 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2157 nfnetlink_subsys_unregister(&ctnl_subsys
);
2162 static void __exit
ctnetlink_exit(void)
2164 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2166 nf_ct_remove_userspace_expectations();
2167 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2168 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2169 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2172 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2173 nfnetlink_subsys_unregister(&ctnl_subsys
);
2176 module_init(ctnetlink_init
);
2177 module_exit(ctnetlink_exit
);