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/skbuff.h>
26 #include <linux/errno.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/interrupt.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
34 #include <net/netfilter/nf_conntrack.h>
35 #include <net/netfilter/nf_conntrack_core.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_l4proto.h>
40 #include <net/netfilter/nf_conntrack_tuple.h>
41 #include <net/netfilter/nf_conntrack_acct.h>
42 #include <net/netfilter/nf_conntrack_zones.h>
43 #ifdef CONFIG_NF_NAT_NEEDED
44 #include <net/netfilter/nf_nat_core.h>
45 #include <net/netfilter/nf_nat_protocol.h>
48 #include <linux/netfilter/nfnetlink.h>
49 #include <linux/netfilter/nfnetlink_conntrack.h>
51 MODULE_LICENSE("GPL");
53 static char __initdata version
[] = "0.93";
56 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
57 const struct nf_conntrack_tuple
*tuple
,
58 struct nf_conntrack_l4proto
*l4proto
)
61 struct nlattr
*nest_parms
;
63 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
66 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
68 if (likely(l4proto
->tuple_to_nlattr
))
69 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
71 nla_nest_end(skb
, nest_parms
);
80 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
81 const struct nf_conntrack_tuple
*tuple
,
82 struct nf_conntrack_l3proto
*l3proto
)
85 struct nlattr
*nest_parms
;
87 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
91 if (likely(l3proto
->tuple_to_nlattr
))
92 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
94 nla_nest_end(skb
, nest_parms
);
103 ctnetlink_dump_tuples(struct sk_buff
*skb
,
104 const struct nf_conntrack_tuple
*tuple
)
107 struct nf_conntrack_l3proto
*l3proto
;
108 struct nf_conntrack_l4proto
*l4proto
;
110 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
111 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
113 if (unlikely(ret
< 0))
116 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
117 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
123 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
125 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
133 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
135 long timeout
= (ct
->timeout
.expires
- jiffies
) / HZ
;
140 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
148 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
150 struct nf_conntrack_l4proto
*l4proto
;
151 struct nlattr
*nest_proto
;
154 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
155 if (!l4proto
->to_nlattr
)
158 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
160 goto nla_put_failure
;
162 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
164 nla_nest_end(skb
, nest_proto
);
173 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
175 struct nlattr
*nest_helper
;
176 const struct nf_conn_help
*help
= nfct_help(ct
);
177 struct nf_conntrack_helper
*helper
;
182 helper
= rcu_dereference(help
->helper
);
186 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
188 goto nla_put_failure
;
189 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
191 if (helper
->to_nlattr
)
192 helper
->to_nlattr(skb
, ct
);
194 nla_nest_end(skb
, nest_helper
);
203 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
204 enum ip_conntrack_dir dir
)
206 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
207 struct nlattr
*nest_count
;
208 const struct nf_conn_counter
*acct
;
210 acct
= nf_conn_acct_find(ct
);
214 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
216 goto nla_put_failure
;
218 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
,
219 cpu_to_be64(acct
[dir
].packets
));
220 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
,
221 cpu_to_be64(acct
[dir
].bytes
));
223 nla_nest_end(skb
, nest_count
);
231 #ifdef CONFIG_NF_CONNTRACK_MARK
233 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
235 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
242 #define ctnetlink_dump_mark(a, b) (0)
245 #ifdef CONFIG_NF_CONNTRACK_SECMARK
247 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
249 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
256 #define ctnetlink_dump_secmark(a, b) (0)
259 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
262 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
264 struct nlattr
*nest_parms
;
266 if (!(ct
->status
& IPS_EXPECTED
))
269 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
271 goto nla_put_failure
;
272 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
273 goto nla_put_failure
;
274 nla_nest_end(skb
, nest_parms
);
282 #ifdef CONFIG_NF_NAT_NEEDED
284 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
286 struct nlattr
*nest_parms
;
288 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
290 goto nla_put_failure
;
292 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
293 htonl(natseq
->correction_pos
));
294 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
295 htonl(natseq
->offset_before
));
296 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
297 htonl(natseq
->offset_after
));
299 nla_nest_end(skb
, nest_parms
);
308 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
310 struct nf_nat_seq
*natseq
;
311 struct nf_conn_nat
*nat
= nfct_nat(ct
);
313 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
316 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
317 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
320 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
321 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
327 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
331 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
333 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
341 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
343 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
351 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
352 int event
, struct nf_conn
*ct
)
354 struct nlmsghdr
*nlh
;
355 struct nfgenmsg
*nfmsg
;
356 struct nlattr
*nest_parms
;
357 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
359 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
360 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
364 nfmsg
= nlmsg_data(nlh
);
365 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
366 nfmsg
->version
= NFNETLINK_V0
;
369 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
371 goto nla_put_failure
;
372 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
373 goto nla_put_failure
;
374 nla_nest_end(skb
, nest_parms
);
376 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
378 goto nla_put_failure
;
379 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
380 goto nla_put_failure
;
381 nla_nest_end(skb
, nest_parms
);
384 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
386 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
387 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
388 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
389 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
390 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
391 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
392 ctnetlink_dump_mark(skb
, ct
) < 0 ||
393 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
394 ctnetlink_dump_id(skb
, ct
) < 0 ||
395 ctnetlink_dump_use(skb
, ct
) < 0 ||
396 ctnetlink_dump_master(skb
, ct
) < 0 ||
397 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
398 goto nla_put_failure
;
405 nlmsg_cancel(skb
, nlh
);
409 #ifdef CONFIG_NF_CONNTRACK_EVENTS
411 ctnetlink_proto_size(const struct nf_conn
*ct
)
413 struct nf_conntrack_l3proto
*l3proto
;
414 struct nf_conntrack_l4proto
*l4proto
;
418 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
419 len
+= l3proto
->nla_size
;
421 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
422 len
+= l4proto
->nla_size
;
429 ctnetlink_counters_size(const struct nf_conn
*ct
)
431 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
433 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
434 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
435 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
440 ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
442 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
443 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
444 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
445 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
446 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
447 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
448 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
449 + ctnetlink_counters_size(ct
)
450 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
451 + nla_total_size(0) /* CTA_PROTOINFO */
452 + nla_total_size(0) /* CTA_HELP */
453 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
454 #ifdef CONFIG_NF_CONNTRACK_SECMARK
455 + nla_total_size(sizeof(u_int32_t
)) /* CTA_SECMARK */
457 #ifdef CONFIG_NF_NAT_NEEDED
458 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
459 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
461 #ifdef CONFIG_NF_CONNTRACK_MARK
462 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
464 + ctnetlink_proto_size(ct
)
469 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
472 struct nlmsghdr
*nlh
;
473 struct nfgenmsg
*nfmsg
;
474 struct nlattr
*nest_parms
;
475 struct nf_conn
*ct
= item
->ct
;
478 unsigned int flags
= 0, group
;
481 /* ignore our fake conntrack entry */
482 if (ct
== &nf_conntrack_untracked
)
485 if (events
& (1 << IPCT_DESTROY
)) {
486 type
= IPCTNL_MSG_CT_DELETE
;
487 group
= NFNLGRP_CONNTRACK_DESTROY
;
488 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
489 type
= IPCTNL_MSG_CT_NEW
;
490 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
491 group
= NFNLGRP_CONNTRACK_NEW
;
493 type
= IPCTNL_MSG_CT_NEW
;
494 group
= NFNLGRP_CONNTRACK_UPDATE
;
499 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
502 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
506 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
507 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
511 nfmsg
= nlmsg_data(nlh
);
512 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
513 nfmsg
->version
= NFNETLINK_V0
;
517 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
519 goto nla_put_failure
;
520 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
521 goto nla_put_failure
;
522 nla_nest_end(skb
, nest_parms
);
524 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
526 goto nla_put_failure
;
527 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
528 goto nla_put_failure
;
529 nla_nest_end(skb
, nest_parms
);
532 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
534 if (ctnetlink_dump_id(skb
, ct
) < 0)
535 goto nla_put_failure
;
537 if (ctnetlink_dump_status(skb
, ct
) < 0)
538 goto nla_put_failure
;
540 if (events
& (1 << IPCT_DESTROY
)) {
541 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
542 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
543 goto nla_put_failure
;
545 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
546 goto nla_put_failure
;
548 if (events
& (1 << IPCT_PROTOINFO
)
549 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
550 goto nla_put_failure
;
552 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
553 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
554 goto nla_put_failure
;
556 #ifdef CONFIG_NF_CONNTRACK_SECMARK
557 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
558 && ctnetlink_dump_secmark(skb
, ct
) < 0)
559 goto nla_put_failure
;
562 if (events
& (1 << IPCT_RELATED
) &&
563 ctnetlink_dump_master(skb
, ct
) < 0)
564 goto nla_put_failure
;
566 if (events
& (1 << IPCT_NATSEQADJ
) &&
567 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
568 goto nla_put_failure
;
571 #ifdef CONFIG_NF_CONNTRACK_MARK
572 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
573 && ctnetlink_dump_mark(skb
, ct
) < 0)
574 goto nla_put_failure
;
579 err
= nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
,
581 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
588 nlmsg_cancel(skb
, nlh
);
592 nfnetlink_set_err(net
, 0, group
, -ENOBUFS
);
595 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
597 static int ctnetlink_done(struct netlink_callback
*cb
)
600 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
605 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
607 struct net
*net
= sock_net(skb
->sk
);
608 struct nf_conn
*ct
, *last
;
609 struct nf_conntrack_tuple_hash
*h
;
610 struct hlist_nulls_node
*n
;
611 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
612 u_int8_t l3proto
= nfmsg
->nfgen_family
;
615 last
= (struct nf_conn
*)cb
->args
[1];
616 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
618 hlist_nulls_for_each_entry_rcu(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
620 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
622 ct
= nf_ct_tuplehash_to_ctrack(h
);
623 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
625 /* Dump entries of a given L3 protocol number.
626 * If it is not specified, ie. l3proto == 0,
627 * then dump everything. */
628 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
635 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
637 IPCTNL_MSG_CT_NEW
, ct
) < 0) {
638 cb
->args
[1] = (unsigned long)ct
;
642 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
643 IPCTNL_MSG_CT_GET_CTRZERO
) {
644 struct nf_conn_counter
*acct
;
646 acct
= nf_conn_acct_find(ct
);
648 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
667 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
669 struct nlattr
*tb
[CTA_IP_MAX
+1];
670 struct nf_conntrack_l3proto
*l3proto
;
673 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
676 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
678 if (likely(l3proto
->nlattr_to_tuple
)) {
679 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
680 l3proto
->nla_policy
);
682 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
690 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
691 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
695 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
696 struct nf_conntrack_tuple
*tuple
)
698 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
699 struct nf_conntrack_l4proto
*l4proto
;
702 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
706 if (!tb
[CTA_PROTO_NUM
])
708 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
711 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
713 if (likely(l4proto
->nlattr_to_tuple
)) {
714 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
715 l4proto
->nla_policy
);
717 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
725 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
726 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
727 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
731 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
732 struct nf_conntrack_tuple
*tuple
,
733 enum ctattr_tuple type
, u_int8_t l3num
)
735 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
738 memset(tuple
, 0, sizeof(*tuple
));
740 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
742 if (!tb
[CTA_TUPLE_IP
])
745 tuple
->src
.l3num
= l3num
;
747 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
751 if (!tb
[CTA_TUPLE_PROTO
])
754 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
758 /* orig and expect tuples get DIR_ORIGINAL */
759 if (type
== CTA_TUPLE_REPLY
)
760 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
762 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
768 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
771 #ifdef CONFIG_NF_CONNTRACK_ZONES
772 *zone
= ntohs(nla_get_be16(attr
));
782 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
783 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
787 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
789 struct nlattr
*tb
[CTA_HELP_MAX
+1];
791 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
793 if (!tb
[CTA_HELP_NAME
])
796 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
801 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
802 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
803 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
804 [CTA_STATUS
] = { .type
= NLA_U32
},
805 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
806 [CTA_HELP
] = { .type
= NLA_NESTED
},
807 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
808 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
809 [CTA_MARK
] = { .type
= NLA_U32
},
810 [CTA_ID
] = { .type
= NLA_U32
},
811 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
812 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
813 [CTA_ZONE
] = { .type
= NLA_U16
},
817 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
818 const struct nlmsghdr
*nlh
,
819 const struct nlattr
* const cda
[])
821 struct net
*net
= sock_net(ctnl
);
822 struct nf_conntrack_tuple_hash
*h
;
823 struct nf_conntrack_tuple tuple
;
825 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
826 u_int8_t u3
= nfmsg
->nfgen_family
;
830 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
834 if (cda
[CTA_TUPLE_ORIG
])
835 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
836 else if (cda
[CTA_TUPLE_REPLY
])
837 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
839 /* Flush the whole table */
840 nf_conntrack_flush_report(net
,
849 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
853 ct
= nf_ct_tuplehash_to_ctrack(h
);
856 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
857 if (id
!= (u32
)(unsigned long)ct
) {
863 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
865 nlmsg_report(nlh
)) < 0) {
866 nf_ct_delete_from_lists(ct
);
867 /* we failed to report the event, try later */
868 nf_ct_insert_dying_list(ct
);
873 /* death_by_timeout would report the event again */
874 set_bit(IPS_DYING_BIT
, &ct
->status
);
883 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
884 const struct nlmsghdr
*nlh
,
885 const struct nlattr
* const cda
[])
887 struct net
*net
= sock_net(ctnl
);
888 struct nf_conntrack_tuple_hash
*h
;
889 struct nf_conntrack_tuple tuple
;
891 struct sk_buff
*skb2
= NULL
;
892 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
893 u_int8_t u3
= nfmsg
->nfgen_family
;
897 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
898 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
901 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
905 if (cda
[CTA_TUPLE_ORIG
])
906 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
907 else if (cda
[CTA_TUPLE_REPLY
])
908 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
915 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
919 ct
= nf_ct_tuplehash_to_ctrack(h
);
922 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
929 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
930 IPCTNL_MSG_CT_NEW
, ct
);
936 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
948 #ifdef CONFIG_NF_NAT_NEEDED
950 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
951 enum nf_nat_manip_type manip
,
952 const struct nlattr
*attr
)
954 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
956 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
957 if (!parse_nat_setup
) {
958 #ifdef CONFIG_MODULES
960 spin_unlock_bh(&nf_conntrack_lock
);
962 if (request_module("nf-nat-ipv4") < 0) {
964 spin_lock_bh(&nf_conntrack_lock
);
969 spin_lock_bh(&nf_conntrack_lock
);
971 if (nfnetlink_parse_nat_setup_hook
)
977 return parse_nat_setup(ct
, manip
, attr
);
982 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
985 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
986 d
= ct
->status
^ status
;
988 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
992 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
993 /* SEEN_REPLY bit can only be set */
996 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
997 /* ASSURED bit can only be set */
1000 /* Be careful here, modifying NAT bits can screw up things,
1001 * so don't let users modify them directly if they don't pass
1003 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1008 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1010 #ifdef CONFIG_NF_NAT_NEEDED
1013 if (cda
[CTA_NAT_DST
]) {
1014 ret
= ctnetlink_parse_nat_setup(ct
,
1020 if (cda
[CTA_NAT_SRC
]) {
1021 ret
= ctnetlink_parse_nat_setup(ct
,
1034 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1036 struct nf_conntrack_helper
*helper
;
1037 struct nf_conn_help
*help
= nfct_help(ct
);
1038 char *helpname
= NULL
;
1041 /* don't change helper of sibling connections */
1045 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1049 if (!strcmp(helpname
, "")) {
1050 if (help
&& help
->helper
) {
1051 /* we had a helper before ... */
1052 nf_ct_remove_expectations(ct
);
1053 rcu_assign_pointer(help
->helper
, NULL
);
1059 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1060 nf_ct_protonum(ct
));
1061 if (helper
== NULL
) {
1062 #ifdef CONFIG_MODULES
1063 spin_unlock_bh(&nf_conntrack_lock
);
1065 if (request_module("nfct-helper-%s", helpname
) < 0) {
1066 spin_lock_bh(&nf_conntrack_lock
);
1070 spin_lock_bh(&nf_conntrack_lock
);
1071 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1072 nf_ct_protonum(ct
));
1080 if (help
->helper
== helper
)
1084 /* need to zero data of old helper */
1085 memset(&help
->help
, 0, sizeof(help
->help
));
1087 /* we cannot set a helper for an existing conntrack */
1091 rcu_assign_pointer(help
->helper
, helper
);
1097 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1099 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1101 if (!del_timer(&ct
->timeout
))
1104 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1105 add_timer(&ct
->timeout
);
1110 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1111 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1112 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1113 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1117 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1119 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1120 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1121 struct nf_conntrack_l4proto
*l4proto
;
1124 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1127 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1128 if (l4proto
->from_nlattr
)
1129 err
= l4proto
->from_nlattr(tb
, ct
);
1135 #ifdef CONFIG_NF_NAT_NEEDED
1136 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1137 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1138 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1139 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1143 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1145 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1147 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1149 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1152 natseq
->correction_pos
=
1153 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1155 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1158 natseq
->offset_before
=
1159 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1161 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1164 natseq
->offset_after
=
1165 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1171 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1172 const struct nlattr
* const cda
[])
1175 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1180 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1181 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1182 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1186 ct
->status
|= IPS_SEQ_ADJUST
;
1189 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1190 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1191 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1195 ct
->status
|= IPS_SEQ_ADJUST
;
1203 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1204 const struct nlattr
* const cda
[])
1208 /* only allow NAT changes and master assignation for new conntracks */
1209 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1212 if (cda
[CTA_HELP
]) {
1213 err
= ctnetlink_change_helper(ct
, cda
);
1218 if (cda
[CTA_TIMEOUT
]) {
1219 err
= ctnetlink_change_timeout(ct
, cda
);
1224 if (cda
[CTA_STATUS
]) {
1225 err
= ctnetlink_change_status(ct
, cda
);
1230 if (cda
[CTA_PROTOINFO
]) {
1231 err
= ctnetlink_change_protoinfo(ct
, cda
);
1236 #if defined(CONFIG_NF_CONNTRACK_MARK)
1238 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1241 #ifdef CONFIG_NF_NAT_NEEDED
1242 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1243 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1252 static struct nf_conn
*
1253 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1254 const struct nlattr
* const cda
[],
1255 struct nf_conntrack_tuple
*otuple
,
1256 struct nf_conntrack_tuple
*rtuple
,
1261 struct nf_conntrack_helper
*helper
;
1263 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1265 return ERR_PTR(-ENOMEM
);
1267 if (!cda
[CTA_TIMEOUT
])
1269 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1271 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1274 if (cda
[CTA_HELP
]) {
1275 char *helpname
= NULL
;
1277 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1281 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1282 nf_ct_protonum(ct
));
1283 if (helper
== NULL
) {
1285 #ifdef CONFIG_MODULES
1286 if (request_module("nfct-helper-%s", helpname
) < 0) {
1292 helper
= __nf_conntrack_helper_find(helpname
,
1294 nf_ct_protonum(ct
));
1304 struct nf_conn_help
*help
;
1306 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1312 /* not in hash table yet so not strictly necessary */
1313 rcu_assign_pointer(help
->helper
, helper
);
1316 /* try an implicit helper assignation */
1317 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1322 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1323 err
= ctnetlink_change_nat(ct
, cda
);
1328 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1329 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1330 /* we must add conntrack extensions before confirmation. */
1331 ct
->status
|= IPS_CONFIRMED
;
1333 if (cda
[CTA_STATUS
]) {
1334 err
= ctnetlink_change_status(ct
, cda
);
1339 #ifdef CONFIG_NF_NAT_NEEDED
1340 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1341 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1347 if (cda
[CTA_PROTOINFO
]) {
1348 err
= ctnetlink_change_protoinfo(ct
, cda
);
1353 #if defined(CONFIG_NF_CONNTRACK_MARK)
1355 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1358 /* setup master conntrack: this is a confirmed expectation */
1359 if (cda
[CTA_TUPLE_MASTER
]) {
1360 struct nf_conntrack_tuple master
;
1361 struct nf_conntrack_tuple_hash
*master_h
;
1362 struct nf_conn
*master_ct
;
1364 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1368 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1369 if (master_h
== NULL
) {
1373 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1374 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1375 ct
->master
= master_ct
;
1378 add_timer(&ct
->timeout
);
1379 nf_conntrack_hash_insert(ct
);
1387 nf_conntrack_free(ct
);
1388 return ERR_PTR(err
);
1392 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1393 const struct nlmsghdr
*nlh
,
1394 const struct nlattr
* const cda
[])
1396 struct net
*net
= sock_net(ctnl
);
1397 struct nf_conntrack_tuple otuple
, rtuple
;
1398 struct nf_conntrack_tuple_hash
*h
= NULL
;
1399 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1400 u_int8_t u3
= nfmsg
->nfgen_family
;
1404 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1408 if (cda
[CTA_TUPLE_ORIG
]) {
1409 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1414 if (cda
[CTA_TUPLE_REPLY
]) {
1415 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1420 spin_lock_bh(&nf_conntrack_lock
);
1421 if (cda
[CTA_TUPLE_ORIG
])
1422 h
= __nf_conntrack_find(net
, zone
, &otuple
);
1423 else if (cda
[CTA_TUPLE_REPLY
])
1424 h
= __nf_conntrack_find(net
, zone
, &rtuple
);
1428 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1430 enum ip_conntrack_events events
;
1432 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1439 nf_conntrack_get(&ct
->ct_general
);
1440 spin_unlock_bh(&nf_conntrack_lock
);
1441 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1442 events
= IPCT_RELATED
;
1446 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1447 (1 << IPCT_ASSURED
) |
1448 (1 << IPCT_HELPER
) |
1449 (1 << IPCT_PROTOINFO
) |
1450 (1 << IPCT_NATSEQADJ
) |
1451 (1 << IPCT_MARK
) | events
,
1452 ct
, NETLINK_CB(skb
).pid
,
1456 spin_unlock_bh(&nf_conntrack_lock
);
1460 /* implicit 'else' */
1462 /* We manipulate the conntrack inside the global conntrack table lock,
1463 * so there's no need to increase the refcount */
1465 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1466 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1468 err
= ctnetlink_change_conntrack(ct
, cda
);
1470 nf_conntrack_get(&ct
->ct_general
);
1471 spin_unlock_bh(&nf_conntrack_lock
);
1472 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1473 (1 << IPCT_ASSURED
) |
1474 (1 << IPCT_HELPER
) |
1475 (1 << IPCT_PROTOINFO
) |
1476 (1 << IPCT_NATSEQADJ
) |
1478 ct
, NETLINK_CB(skb
).pid
,
1482 spin_unlock_bh(&nf_conntrack_lock
);
1488 spin_unlock_bh(&nf_conntrack_lock
);
1492 /***********************************************************************
1494 ***********************************************************************/
1497 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1498 const struct nf_conntrack_tuple
*tuple
,
1499 enum ctattr_expect type
)
1501 struct nlattr
*nest_parms
;
1503 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1505 goto nla_put_failure
;
1506 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1507 goto nla_put_failure
;
1508 nla_nest_end(skb
, nest_parms
);
1517 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1518 const struct nf_conntrack_tuple
*tuple
,
1519 const struct nf_conntrack_tuple_mask
*mask
)
1522 struct nf_conntrack_l3proto
*l3proto
;
1523 struct nf_conntrack_l4proto
*l4proto
;
1524 struct nf_conntrack_tuple m
;
1525 struct nlattr
*nest_parms
;
1527 memset(&m
, 0xFF, sizeof(m
));
1528 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1529 m
.src
.u
.all
= mask
->src
.u
.all
;
1530 m
.dst
.protonum
= tuple
->dst
.protonum
;
1532 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1534 goto nla_put_failure
;
1536 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1537 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1539 if (unlikely(ret
< 0))
1540 goto nla_put_failure
;
1542 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1543 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1544 if (unlikely(ret
< 0))
1545 goto nla_put_failure
;
1547 nla_nest_end(skb
, nest_parms
);
1556 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1557 const struct nf_conntrack_expect
*exp
)
1559 struct nf_conn
*master
= exp
->master
;
1560 struct nf_conntrack_helper
*helper
;
1561 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1566 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1567 goto nla_put_failure
;
1568 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1569 goto nla_put_failure
;
1570 if (ctnetlink_exp_dump_tuple(skb
,
1571 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1572 CTA_EXPECT_MASTER
) < 0)
1573 goto nla_put_failure
;
1575 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1576 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1577 helper
= rcu_dereference(nfct_help(master
)->helper
);
1579 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1588 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1589 int event
, const struct nf_conntrack_expect
*exp
)
1591 struct nlmsghdr
*nlh
;
1592 struct nfgenmsg
*nfmsg
;
1593 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1595 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1596 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1600 nfmsg
= nlmsg_data(nlh
);
1601 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1602 nfmsg
->version
= NFNETLINK_V0
;
1605 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1606 goto nla_put_failure
;
1608 nlmsg_end(skb
, nlh
);
1613 nlmsg_cancel(skb
, nlh
);
1617 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1619 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1621 struct nf_conntrack_expect
*exp
= item
->exp
;
1622 struct net
*net
= nf_ct_exp_net(exp
);
1623 struct nlmsghdr
*nlh
;
1624 struct nfgenmsg
*nfmsg
;
1625 struct sk_buff
*skb
;
1629 if (events
& (1 << IPEXP_NEW
)) {
1630 type
= IPCTNL_MSG_EXP_NEW
;
1631 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1635 if (!item
->report
&&
1636 !nfnetlink_has_listeners(net
, NFNLGRP_CONNTRACK_EXP_NEW
))
1639 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1643 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1644 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1648 nfmsg
= nlmsg_data(nlh
);
1649 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1650 nfmsg
->version
= NFNETLINK_V0
;
1654 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1655 goto nla_put_failure
;
1658 nlmsg_end(skb
, nlh
);
1659 nfnetlink_send(skb
, net
, item
->pid
, NFNLGRP_CONNTRACK_EXP_NEW
,
1660 item
->report
, GFP_ATOMIC
);
1665 nlmsg_cancel(skb
, nlh
);
1669 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1673 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1676 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1681 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1683 struct net
*net
= sock_net(skb
->sk
);
1684 struct nf_conntrack_expect
*exp
, *last
;
1685 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1686 struct hlist_node
*n
;
1687 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1690 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1691 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1693 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1695 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1702 if (ctnetlink_exp_fill_info(skb
,
1703 NETLINK_CB(cb
->skb
).pid
,
1707 if (!atomic_inc_not_zero(&exp
->use
))
1709 cb
->args
[1] = (unsigned long)exp
;
1721 nf_ct_expect_put(last
);
1726 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1727 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1728 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1729 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1730 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1731 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1732 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1736 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1737 const struct nlmsghdr
*nlh
,
1738 const struct nlattr
* const cda
[])
1740 struct net
*net
= sock_net(ctnl
);
1741 struct nf_conntrack_tuple tuple
;
1742 struct nf_conntrack_expect
*exp
;
1743 struct sk_buff
*skb2
;
1744 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1745 u_int8_t u3
= nfmsg
->nfgen_family
;
1749 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1750 return netlink_dump_start(ctnl
, skb
, nlh
,
1751 ctnetlink_exp_dump_table
,
1752 ctnetlink_exp_done
);
1755 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1759 if (cda
[CTA_EXPECT_MASTER
])
1760 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1767 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1771 if (cda
[CTA_EXPECT_ID
]) {
1772 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1773 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1774 nf_ct_expect_put(exp
);
1780 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1785 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1786 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1791 nf_ct_expect_put(exp
);
1793 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1798 nf_ct_expect_put(exp
);
1803 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1804 const struct nlmsghdr
*nlh
,
1805 const struct nlattr
* const cda
[])
1807 struct net
*net
= sock_net(ctnl
);
1808 struct nf_conntrack_expect
*exp
;
1809 struct nf_conntrack_tuple tuple
;
1810 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1811 struct hlist_node
*n
, *next
;
1812 u_int8_t u3
= nfmsg
->nfgen_family
;
1817 if (cda
[CTA_EXPECT_TUPLE
]) {
1818 /* delete a single expect by tuple */
1819 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1823 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1827 /* bump usage count to 2 */
1828 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1832 if (cda
[CTA_EXPECT_ID
]) {
1833 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1834 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1835 nf_ct_expect_put(exp
);
1840 /* after list removal, usage count == 1 */
1841 nf_ct_unexpect_related(exp
);
1842 /* have to put what we 'get' above.
1843 * after this line usage count == 0 */
1844 nf_ct_expect_put(exp
);
1845 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1846 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1847 struct nf_conn_help
*m_help
;
1849 /* delete all expectations for this helper */
1850 spin_lock_bh(&nf_conntrack_lock
);
1851 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1852 hlist_for_each_entry_safe(exp
, n
, next
,
1853 &net
->ct
.expect_hash
[i
],
1855 m_help
= nfct_help(exp
->master
);
1856 if (!strcmp(m_help
->helper
->name
, name
) &&
1857 del_timer(&exp
->timeout
)) {
1858 nf_ct_unlink_expect(exp
);
1859 nf_ct_expect_put(exp
);
1863 spin_unlock_bh(&nf_conntrack_lock
);
1865 /* This basically means we have to flush everything*/
1866 spin_lock_bh(&nf_conntrack_lock
);
1867 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1868 hlist_for_each_entry_safe(exp
, n
, next
,
1869 &net
->ct
.expect_hash
[i
],
1871 if (del_timer(&exp
->timeout
)) {
1872 nf_ct_unlink_expect(exp
);
1873 nf_ct_expect_put(exp
);
1877 spin_unlock_bh(&nf_conntrack_lock
);
1883 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
1884 const struct nlattr
* const cda
[])
1890 ctnetlink_create_expect(struct net
*net
, u16 zone
,
1891 const struct nlattr
* const cda
[],
1893 u32 pid
, int report
)
1895 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1896 struct nf_conntrack_tuple_hash
*h
= NULL
;
1897 struct nf_conntrack_expect
*exp
;
1899 struct nf_conn_help
*help
;
1902 /* caller guarantees that those three CTA_EXPECT_* exist */
1903 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1906 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1909 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1913 /* Look for master conntrack of this expectation */
1914 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
1917 ct
= nf_ct_tuplehash_to_ctrack(h
);
1918 help
= nfct_help(ct
);
1920 if (!help
|| !help
->helper
) {
1921 /* such conntrack hasn't got any helper, abort */
1926 exp
= nf_ct_expect_alloc(ct
);
1933 exp
->expectfn
= NULL
;
1937 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1938 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1939 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1941 err
= nf_ct_expect_related_report(exp
, pid
, report
);
1942 nf_ct_expect_put(exp
);
1945 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1950 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1951 const struct nlmsghdr
*nlh
,
1952 const struct nlattr
* const cda
[])
1954 struct net
*net
= sock_net(ctnl
);
1955 struct nf_conntrack_tuple tuple
;
1956 struct nf_conntrack_expect
*exp
;
1957 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1958 u_int8_t u3
= nfmsg
->nfgen_family
;
1962 if (!cda
[CTA_EXPECT_TUPLE
]
1963 || !cda
[CTA_EXPECT_MASK
]
1964 || !cda
[CTA_EXPECT_MASTER
])
1967 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1971 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1975 spin_lock_bh(&nf_conntrack_lock
);
1976 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
1979 spin_unlock_bh(&nf_conntrack_lock
);
1981 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1982 err
= ctnetlink_create_expect(net
, zone
, cda
,
1984 NETLINK_CB(skb
).pid
,
1991 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1992 err
= ctnetlink_change_expect(exp
, cda
);
1993 spin_unlock_bh(&nf_conntrack_lock
);
1998 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1999 static struct nf_ct_event_notifier ctnl_notifier
= {
2000 .fcn
= ctnetlink_conntrack_event
,
2003 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2004 .fcn
= ctnetlink_expect_event
,
2008 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2009 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2010 .attr_count
= CTA_MAX
,
2011 .policy
= ct_nla_policy
},
2012 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2013 .attr_count
= CTA_MAX
,
2014 .policy
= ct_nla_policy
},
2015 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2016 .attr_count
= CTA_MAX
,
2017 .policy
= ct_nla_policy
},
2018 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2019 .attr_count
= CTA_MAX
,
2020 .policy
= ct_nla_policy
},
2023 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2024 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2025 .attr_count
= CTA_EXPECT_MAX
,
2026 .policy
= exp_nla_policy
},
2027 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2028 .attr_count
= CTA_EXPECT_MAX
,
2029 .policy
= exp_nla_policy
},
2030 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2031 .attr_count
= CTA_EXPECT_MAX
,
2032 .policy
= exp_nla_policy
},
2035 static const struct nfnetlink_subsystem ctnl_subsys
= {
2036 .name
= "conntrack",
2037 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2038 .cb_count
= IPCTNL_MSG_MAX
,
2042 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2043 .name
= "conntrack_expect",
2044 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2045 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2049 MODULE_ALIAS("ip_conntrack_netlink");
2050 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2051 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2053 static int __init
ctnetlink_init(void)
2057 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
2058 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2060 printk("ctnetlink_init: cannot register with nfnetlink.\n");
2064 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2066 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
2067 goto err_unreg_subsys
;
2070 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2071 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
2073 printk("ctnetlink_init: cannot register notifier.\n");
2074 goto err_unreg_exp_subsys
;
2077 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
2079 printk("ctnetlink_init: cannot expect register notifier.\n");
2080 goto err_unreg_notifier
;
2086 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2088 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2089 err_unreg_exp_subsys
:
2090 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2093 nfnetlink_subsys_unregister(&ctnl_subsys
);
2098 static void __exit
ctnetlink_exit(void)
2100 printk("ctnetlink: unregistering from nfnetlink.\n");
2102 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2103 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2104 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2107 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2108 nfnetlink_subsys_unregister(&ctnl_subsys
);
2112 module_init(ctnetlink_init
);
2113 module_exit(ctnetlink_exit
);