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>
30 #include <linux/notifier.h>
32 #include <linux/netfilter.h>
33 #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 #ifdef CONFIG_NF_NAT_NEEDED
43 #include <net/netfilter/nf_nat_core.h>
44 #include <net/netfilter/nf_nat_protocol.h>
47 #include <linux/netfilter/nfnetlink.h>
48 #include <linux/netfilter/nfnetlink_conntrack.h>
50 MODULE_LICENSE("GPL");
52 static char __initdata version
[] = "0.93";
55 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
56 const struct nf_conntrack_tuple
*tuple
,
57 struct nf_conntrack_l4proto
*l4proto
)
60 struct nlattr
*nest_parms
;
62 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
65 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
67 if (likely(l4proto
->tuple_to_nlattr
))
68 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
70 nla_nest_end(skb
, nest_parms
);
79 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
80 const struct nf_conntrack_tuple
*tuple
,
81 struct nf_conntrack_l3proto
*l3proto
)
84 struct nlattr
*nest_parms
;
86 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
90 if (likely(l3proto
->tuple_to_nlattr
))
91 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
93 nla_nest_end(skb
, nest_parms
);
102 ctnetlink_dump_tuples(struct sk_buff
*skb
,
103 const struct nf_conntrack_tuple
*tuple
)
106 struct nf_conntrack_l3proto
*l3proto
;
107 struct nf_conntrack_l4proto
*l4proto
;
109 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
110 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
112 if (unlikely(ret
< 0))
115 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
116 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
122 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
124 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
132 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
134 long timeout
= (ct
->timeout
.expires
- jiffies
) / HZ
;
139 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
147 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
149 struct nf_conntrack_l4proto
*l4proto
;
150 struct nlattr
*nest_proto
;
153 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
154 if (!l4proto
->to_nlattr
)
157 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
159 goto nla_put_failure
;
161 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
163 nla_nest_end(skb
, nest_proto
);
172 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
174 struct nlattr
*nest_helper
;
175 const struct nf_conn_help
*help
= nfct_help(ct
);
176 struct nf_conntrack_helper
*helper
;
181 helper
= rcu_dereference(help
->helper
);
185 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
187 goto nla_put_failure
;
188 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
190 if (helper
->to_nlattr
)
191 helper
->to_nlattr(skb
, ct
);
193 nla_nest_end(skb
, nest_helper
);
202 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
203 enum ip_conntrack_dir dir
)
205 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
206 struct nlattr
*nest_count
;
207 const struct nf_conn_counter
*acct
;
209 acct
= nf_conn_acct_find(ct
);
213 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
215 goto nla_put_failure
;
217 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
,
218 cpu_to_be64(acct
[dir
].packets
));
219 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
,
220 cpu_to_be64(acct
[dir
].bytes
));
222 nla_nest_end(skb
, nest_count
);
230 #ifdef CONFIG_NF_CONNTRACK_MARK
232 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
234 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
241 #define ctnetlink_dump_mark(a, b) (0)
244 #ifdef CONFIG_NF_CONNTRACK_SECMARK
246 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
248 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
255 #define ctnetlink_dump_secmark(a, b) (0)
258 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
261 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
263 struct nlattr
*nest_parms
;
265 if (!(ct
->status
& IPS_EXPECTED
))
268 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
270 goto nla_put_failure
;
271 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
272 goto nla_put_failure
;
273 nla_nest_end(skb
, nest_parms
);
281 #ifdef CONFIG_NF_NAT_NEEDED
283 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
285 struct nlattr
*nest_parms
;
287 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
289 goto nla_put_failure
;
291 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
292 htonl(natseq
->correction_pos
));
293 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
294 htonl(natseq
->offset_before
));
295 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
296 htonl(natseq
->offset_after
));
298 nla_nest_end(skb
, nest_parms
);
307 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
309 struct nf_nat_seq
*natseq
;
310 struct nf_conn_nat
*nat
= nfct_nat(ct
);
312 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
315 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
316 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
319 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
320 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
326 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
330 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
332 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
340 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
342 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
349 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
352 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
353 int event
, int nowait
,
354 const struct nf_conn
*ct
)
356 struct nlmsghdr
*nlh
;
357 struct nfgenmsg
*nfmsg
;
358 struct nlattr
*nest_parms
;
359 unsigned char *b
= skb_tail_pointer(skb
);
361 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
362 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
363 nfmsg
= NLMSG_DATA(nlh
);
365 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
366 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
367 nfmsg
->version
= NFNETLINK_V0
;
370 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
372 goto nla_put_failure
;
373 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
374 goto nla_put_failure
;
375 nla_nest_end(skb
, nest_parms
);
377 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
379 goto nla_put_failure
;
380 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
381 goto nla_put_failure
;
382 nla_nest_end(skb
, nest_parms
);
384 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
385 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
386 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
387 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
388 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
389 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
390 ctnetlink_dump_mark(skb
, ct
) < 0 ||
391 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
392 ctnetlink_dump_id(skb
, ct
) < 0 ||
393 ctnetlink_dump_use(skb
, ct
) < 0 ||
394 ctnetlink_dump_master(skb
, ct
) < 0 ||
395 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
396 goto nla_put_failure
;
398 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
407 #ifdef CONFIG_NF_CONNTRACK_EVENTS
409 * The general structure of a ctnetlink event is
412 * <l3/l4-proto-attributes>
414 * <l3/l4-proto-attributes>
418 * <l4-proto-attributes>
420 * <l3/l4-proto-attributes>
422 * Therefore the formular is
424 * size = sizeof(headers) + sizeof(generic_nlas) + 3 * sizeof(tuple_nlas)
425 * + sizeof(protoinfo_nlas)
427 static struct sk_buff
*
428 ctnetlink_alloc_skb(const struct nf_conntrack_tuple
*tuple
, gfp_t gfp
)
430 struct nf_conntrack_l3proto
*l3proto
;
431 struct nf_conntrack_l4proto
*l4proto
;
434 #define NLA_TYPE_SIZE(type) nla_total_size(sizeof(type))
436 /* proto independant part */
437 len
= NLMSG_SPACE(sizeof(struct nfgenmsg
))
438 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
439 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
440 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
441 + 3 * NLA_TYPE_SIZE(u_int8_t
) /* CTA_PROTO_NUM */
442 + NLA_TYPE_SIZE(u_int32_t
) /* CTA_ID */
443 + NLA_TYPE_SIZE(u_int32_t
) /* CTA_STATUS */
444 #ifdef CONFIG_NF_CT_ACCT
445 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
446 + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_PACKETS */
447 + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_BYTES */
449 + NLA_TYPE_SIZE(u_int32_t
) /* CTA_TIMEOUT */
450 + nla_total_size(0) /* CTA_PROTOINFO */
451 + nla_total_size(0) /* CTA_HELP */
452 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
453 #ifdef CONFIG_NF_CONNTRACK_SECMARK
454 + NLA_TYPE_SIZE(u_int32_t
) /* CTA_SECMARK */
456 #ifdef CONFIG_NF_NAT_NEEDED
457 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
458 + 2 * NLA_TYPE_SIZE(u_int32_t
) /* CTA_NAT_SEQ_CORRECTION_POS */
459 + 2 * NLA_TYPE_SIZE(u_int32_t
) /* CTA_NAT_SEQ_CORRECTION_BEFORE */
460 + 2 * NLA_TYPE_SIZE(u_int32_t
) /* CTA_NAT_SEQ_CORRECTION_AFTER */
462 #ifdef CONFIG_NF_CONNTRACK_MARK
463 + NLA_TYPE_SIZE(u_int32_t
) /* CTA_MARK */
470 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
471 len
+= l3proto
->nla_size
;
473 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
474 len
+= l4proto
->nla_size
;
477 return alloc_skb(len
, gfp
);
480 static int ctnetlink_conntrack_event(struct notifier_block
*this,
481 unsigned long events
, void *ptr
)
483 struct nlmsghdr
*nlh
;
484 struct nfgenmsg
*nfmsg
;
485 struct nlattr
*nest_parms
;
486 struct nf_ct_event
*item
= (struct nf_ct_event
*)ptr
;
487 struct nf_conn
*ct
= item
->ct
;
491 unsigned int flags
= 0, group
;
493 /* ignore our fake conntrack entry */
494 if (ct
== &nf_conntrack_untracked
)
497 if (events
& IPCT_DESTROY
) {
498 type
= IPCTNL_MSG_CT_DELETE
;
499 group
= NFNLGRP_CONNTRACK_DESTROY
;
500 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
501 type
= IPCTNL_MSG_CT_NEW
;
502 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
503 group
= NFNLGRP_CONNTRACK_NEW
;
504 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
505 type
= IPCTNL_MSG_CT_NEW
;
506 group
= NFNLGRP_CONNTRACK_UPDATE
;
510 if (!item
->report
&& !nfnetlink_has_listeners(group
))
513 skb
= ctnetlink_alloc_skb(tuple(ct
, IP_CT_DIR_ORIGINAL
), GFP_ATOMIC
);
519 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
520 nlh
= NLMSG_PUT(skb
, item
->pid
, 0, type
, sizeof(struct nfgenmsg
));
521 nfmsg
= NLMSG_DATA(nlh
);
523 nlh
->nlmsg_flags
= flags
;
524 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
525 nfmsg
->version
= NFNETLINK_V0
;
529 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
531 goto nla_put_failure
;
532 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
533 goto nla_put_failure
;
534 nla_nest_end(skb
, nest_parms
);
536 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
538 goto nla_put_failure
;
539 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
540 goto nla_put_failure
;
541 nla_nest_end(skb
, nest_parms
);
543 if (ctnetlink_dump_id(skb
, ct
) < 0)
544 goto nla_put_failure
;
546 if (ctnetlink_dump_status(skb
, ct
) < 0)
547 goto nla_put_failure
;
549 if (events
& IPCT_DESTROY
) {
550 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
551 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
552 goto nla_put_failure
;
554 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
555 goto nla_put_failure
;
557 if (events
& IPCT_PROTOINFO
558 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
559 goto nla_put_failure
;
561 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
562 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
563 goto nla_put_failure
;
565 #ifdef CONFIG_NF_CONNTRACK_SECMARK
566 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
567 && ctnetlink_dump_secmark(skb
, ct
) < 0)
568 goto nla_put_failure
;
571 if (events
& IPCT_RELATED
&&
572 ctnetlink_dump_master(skb
, ct
) < 0)
573 goto nla_put_failure
;
575 if (events
& IPCT_NATSEQADJ
&&
576 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
577 goto nla_put_failure
;
580 #ifdef CONFIG_NF_CONNTRACK_MARK
581 if ((events
& IPCT_MARK
|| ct
->mark
)
582 && ctnetlink_dump_mark(skb
, ct
) < 0)
583 goto nla_put_failure
;
587 nlh
->nlmsg_len
= skb
->tail
- b
;
588 nfnetlink_send(skb
, item
->pid
, group
, item
->report
);
596 nfnetlink_set_err(0, group
, -ENOBUFS
);
599 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
601 static int ctnetlink_done(struct netlink_callback
*cb
)
604 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
609 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
611 struct nf_conn
*ct
, *last
;
612 struct nf_conntrack_tuple_hash
*h
;
613 struct hlist_nulls_node
*n
;
614 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
615 u_int8_t l3proto
= nfmsg
->nfgen_family
;
618 last
= (struct nf_conn
*)cb
->args
[1];
619 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
621 hlist_nulls_for_each_entry_rcu(h
, n
, &init_net
.ct
.hash
[cb
->args
[0]],
623 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
625 ct
= nf_ct_tuplehash_to_ctrack(h
);
626 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
628 /* Dump entries of a given L3 protocol number.
629 * If it is not specified, ie. l3proto == 0,
630 * then dump everything. */
631 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
638 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
642 cb
->args
[1] = (unsigned long)ct
;
646 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
647 IPCTNL_MSG_CT_GET_CTRZERO
) {
648 struct nf_conn_counter
*acct
;
650 acct
= nf_conn_acct_find(ct
);
652 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
671 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
673 struct nlattr
*tb
[CTA_IP_MAX
+1];
674 struct nf_conntrack_l3proto
*l3proto
;
677 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
680 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
682 if (likely(l3proto
->nlattr_to_tuple
)) {
683 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
684 l3proto
->nla_policy
);
686 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
694 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
695 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
699 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
700 struct nf_conntrack_tuple
*tuple
)
702 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
703 struct nf_conntrack_l4proto
*l4proto
;
706 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
710 if (!tb
[CTA_PROTO_NUM
])
712 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
715 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
717 if (likely(l4proto
->nlattr_to_tuple
)) {
718 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
719 l4proto
->nla_policy
);
721 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
730 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
731 enum ctattr_tuple type
, u_int8_t l3num
)
733 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
736 memset(tuple
, 0, sizeof(*tuple
));
738 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
740 if (!tb
[CTA_TUPLE_IP
])
743 tuple
->src
.l3num
= l3num
;
745 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
749 if (!tb
[CTA_TUPLE_PROTO
])
752 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
756 /* orig and expect tuples get DIR_ORIGINAL */
757 if (type
== CTA_TUPLE_REPLY
)
758 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
760 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
766 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
768 struct nlattr
*tb
[CTA_HELP_MAX
+1];
770 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
772 if (!tb
[CTA_HELP_NAME
])
775 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
780 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
781 [CTA_STATUS
] = { .type
= NLA_U32
},
782 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
783 [CTA_MARK
] = { .type
= NLA_U32
},
784 [CTA_USE
] = { .type
= NLA_U32
},
785 [CTA_ID
] = { .type
= NLA_U32
},
789 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
790 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
792 struct nf_conntrack_tuple_hash
*h
;
793 struct nf_conntrack_tuple tuple
;
795 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
796 u_int8_t u3
= nfmsg
->nfgen_family
;
799 if (cda
[CTA_TUPLE_ORIG
])
800 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
801 else if (cda
[CTA_TUPLE_REPLY
])
802 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
804 /* Flush the whole table */
805 nf_conntrack_flush(&init_net
,
814 h
= nf_conntrack_find_get(&init_net
, &tuple
);
818 ct
= nf_ct_tuplehash_to_ctrack(h
);
821 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
822 if (id
!= (u32
)(unsigned long)ct
) {
828 nf_conntrack_event_report(IPCT_DESTROY
,
833 /* death_by_timeout would report the event again */
834 set_bit(IPS_DYING_BIT
, &ct
->status
);
843 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
844 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
846 struct nf_conntrack_tuple_hash
*h
;
847 struct nf_conntrack_tuple tuple
;
849 struct sk_buff
*skb2
= NULL
;
850 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
851 u_int8_t u3
= nfmsg
->nfgen_family
;
854 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
855 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
858 if (cda
[CTA_TUPLE_ORIG
])
859 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
860 else if (cda
[CTA_TUPLE_REPLY
])
861 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
868 h
= nf_conntrack_find_get(&init_net
, &tuple
);
872 ct
= nf_ct_tuplehash_to_ctrack(h
);
875 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
882 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
883 IPCTNL_MSG_CT_NEW
, 1, ct
);
889 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
901 #ifdef CONFIG_NF_NAT_NEEDED
903 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
904 enum nf_nat_manip_type manip
,
907 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
909 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
910 if (!parse_nat_setup
) {
911 #ifdef CONFIG_MODULES
913 spin_unlock_bh(&nf_conntrack_lock
);
915 if (request_module("nf-nat-ipv4") < 0) {
917 spin_lock_bh(&nf_conntrack_lock
);
922 spin_lock_bh(&nf_conntrack_lock
);
924 if (nfnetlink_parse_nat_setup_hook
)
930 return parse_nat_setup(ct
, manip
, attr
);
935 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
938 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
939 d
= ct
->status
^ status
;
941 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
945 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
946 /* SEEN_REPLY bit can only be set */
949 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
950 /* ASSURED bit can only be set */
953 /* Be careful here, modifying NAT bits can screw up things,
954 * so don't let users modify them directly if they don't pass
956 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
961 ctnetlink_change_nat(struct nf_conn
*ct
, struct nlattr
*cda
[])
963 #ifdef CONFIG_NF_NAT_NEEDED
966 if (cda
[CTA_NAT_DST
]) {
967 ret
= ctnetlink_parse_nat_setup(ct
,
973 if (cda
[CTA_NAT_SRC
]) {
974 ret
= ctnetlink_parse_nat_setup(ct
,
987 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
989 struct nf_conntrack_helper
*helper
;
990 struct nf_conn_help
*help
= nfct_help(ct
);
991 char *helpname
= NULL
;
994 /* don't change helper of sibling connections */
998 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1002 if (!strcmp(helpname
, "")) {
1003 if (help
&& help
->helper
) {
1004 /* we had a helper before ... */
1005 nf_ct_remove_expectations(ct
);
1006 rcu_assign_pointer(help
->helper
, NULL
);
1012 helper
= __nf_conntrack_helper_find_byname(helpname
);
1013 if (helper
== NULL
) {
1014 #ifdef CONFIG_MODULES
1015 spin_unlock_bh(&nf_conntrack_lock
);
1017 if (request_module("nfct-helper-%s", helpname
) < 0) {
1018 spin_lock_bh(&nf_conntrack_lock
);
1022 spin_lock_bh(&nf_conntrack_lock
);
1023 helper
= __nf_conntrack_helper_find_byname(helpname
);
1031 if (help
->helper
== helper
)
1035 /* need to zero data of old helper */
1036 memset(&help
->help
, 0, sizeof(help
->help
));
1038 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1043 rcu_assign_pointer(help
->helper
, helper
);
1049 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
1051 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1053 if (!del_timer(&ct
->timeout
))
1056 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1057 add_timer(&ct
->timeout
);
1063 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
1065 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
1066 struct nf_conntrack_l4proto
*l4proto
;
1069 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
1072 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1073 if (l4proto
->from_nlattr
)
1074 err
= l4proto
->from_nlattr(tb
, ct
);
1080 #ifdef CONFIG_NF_NAT_NEEDED
1082 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
1084 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1086 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
1088 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1091 natseq
->correction_pos
=
1092 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1094 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1097 natseq
->offset_before
=
1098 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1100 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1103 natseq
->offset_after
=
1104 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1110 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1113 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1118 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1119 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1120 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1124 ct
->status
|= IPS_SEQ_ADJUST
;
1127 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1128 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1129 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1133 ct
->status
|= IPS_SEQ_ADJUST
;
1141 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1145 /* only allow NAT changes and master assignation for new conntracks */
1146 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1149 if (cda
[CTA_HELP
]) {
1150 err
= ctnetlink_change_helper(ct
, cda
);
1155 if (cda
[CTA_TIMEOUT
]) {
1156 err
= ctnetlink_change_timeout(ct
, cda
);
1161 if (cda
[CTA_STATUS
]) {
1162 err
= ctnetlink_change_status(ct
, cda
);
1167 if (cda
[CTA_PROTOINFO
]) {
1168 err
= ctnetlink_change_protoinfo(ct
, cda
);
1173 #if defined(CONFIG_NF_CONNTRACK_MARK)
1175 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1178 #ifdef CONFIG_NF_NAT_NEEDED
1179 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1180 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1190 ctnetlink_event_report(struct nf_conn
*ct
, u32 pid
, int report
)
1192 unsigned int events
= 0;
1194 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1195 events
|= IPCT_RELATED
;
1199 nf_conntrack_event_report(IPCT_STATUS
|
1211 static struct nf_conn
*
1212 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1213 struct nf_conntrack_tuple
*otuple
,
1214 struct nf_conntrack_tuple
*rtuple
,
1219 struct nf_conntrack_helper
*helper
;
1221 ct
= nf_conntrack_alloc(&init_net
, otuple
, rtuple
, GFP_ATOMIC
);
1223 return ERR_PTR(-ENOMEM
);
1225 if (!cda
[CTA_TIMEOUT
])
1227 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1229 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1230 ct
->status
|= IPS_CONFIRMED
;
1233 if (cda
[CTA_HELP
]) {
1234 char *helpname
= NULL
;
1236 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1240 helper
= __nf_conntrack_helper_find_byname(helpname
);
1241 if (helper
== NULL
) {
1243 #ifdef CONFIG_MODULES
1244 if (request_module("nfct-helper-%s", helpname
) < 0) {
1250 helper
= __nf_conntrack_helper_find_byname(helpname
);
1260 struct nf_conn_help
*help
;
1262 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1268 /* not in hash table yet so not strictly necessary */
1269 rcu_assign_pointer(help
->helper
, helper
);
1272 /* try an implicit helper assignation */
1273 err
= __nf_ct_try_assign_helper(ct
, GFP_ATOMIC
);
1278 if (cda
[CTA_STATUS
]) {
1279 err
= ctnetlink_change_status(ct
, cda
);
1284 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1285 err
= ctnetlink_change_nat(ct
, cda
);
1290 #ifdef CONFIG_NF_NAT_NEEDED
1291 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1292 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1298 if (cda
[CTA_PROTOINFO
]) {
1299 err
= ctnetlink_change_protoinfo(ct
, cda
);
1304 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1306 #if defined(CONFIG_NF_CONNTRACK_MARK)
1308 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1311 /* setup master conntrack: this is a confirmed expectation */
1312 if (cda
[CTA_TUPLE_MASTER
]) {
1313 struct nf_conntrack_tuple master
;
1314 struct nf_conntrack_tuple_hash
*master_h
;
1315 struct nf_conn
*master_ct
;
1317 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1321 master_h
= nf_conntrack_find_get(&init_net
, &master
);
1322 if (master_h
== NULL
) {
1326 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1327 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1328 ct
->master
= master_ct
;
1331 add_timer(&ct
->timeout
);
1332 nf_conntrack_hash_insert(ct
);
1340 nf_conntrack_free(ct
);
1341 return ERR_PTR(err
);
1345 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1346 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1348 struct nf_conntrack_tuple otuple
, rtuple
;
1349 struct nf_conntrack_tuple_hash
*h
= NULL
;
1350 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1351 u_int8_t u3
= nfmsg
->nfgen_family
;
1354 if (cda
[CTA_TUPLE_ORIG
]) {
1355 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1360 if (cda
[CTA_TUPLE_REPLY
]) {
1361 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1366 spin_lock_bh(&nf_conntrack_lock
);
1367 if (cda
[CTA_TUPLE_ORIG
])
1368 h
= __nf_conntrack_find(&init_net
, &otuple
);
1369 else if (cda
[CTA_TUPLE_REPLY
])
1370 h
= __nf_conntrack_find(&init_net
, &rtuple
);
1374 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1377 ct
= ctnetlink_create_conntrack(cda
, &otuple
,
1384 nf_conntrack_get(&ct
->ct_general
);
1385 spin_unlock_bh(&nf_conntrack_lock
);
1386 ctnetlink_event_report(ct
,
1387 NETLINK_CB(skb
).pid
,
1391 spin_unlock_bh(&nf_conntrack_lock
);
1395 /* implicit 'else' */
1397 /* We manipulate the conntrack inside the global conntrack table lock,
1398 * so there's no need to increase the refcount */
1400 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1401 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1403 err
= ctnetlink_change_conntrack(ct
, cda
);
1405 nf_conntrack_get(&ct
->ct_general
);
1406 spin_unlock_bh(&nf_conntrack_lock
);
1407 ctnetlink_event_report(ct
,
1408 NETLINK_CB(skb
).pid
,
1412 spin_unlock_bh(&nf_conntrack_lock
);
1418 spin_unlock_bh(&nf_conntrack_lock
);
1422 /***********************************************************************
1424 ***********************************************************************/
1427 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1428 const struct nf_conntrack_tuple
*tuple
,
1429 enum ctattr_expect type
)
1431 struct nlattr
*nest_parms
;
1433 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1435 goto nla_put_failure
;
1436 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1437 goto nla_put_failure
;
1438 nla_nest_end(skb
, nest_parms
);
1447 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1448 const struct nf_conntrack_tuple
*tuple
,
1449 const struct nf_conntrack_tuple_mask
*mask
)
1452 struct nf_conntrack_l3proto
*l3proto
;
1453 struct nf_conntrack_l4proto
*l4proto
;
1454 struct nf_conntrack_tuple m
;
1455 struct nlattr
*nest_parms
;
1457 memset(&m
, 0xFF, sizeof(m
));
1458 m
.src
.u
.all
= mask
->src
.u
.all
;
1459 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1461 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1463 goto nla_put_failure
;
1465 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1466 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1468 if (unlikely(ret
< 0))
1469 goto nla_put_failure
;
1471 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1472 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1473 if (unlikely(ret
< 0))
1474 goto nla_put_failure
;
1476 nla_nest_end(skb
, nest_parms
);
1485 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1486 const struct nf_conntrack_expect
*exp
)
1488 struct nf_conn
*master
= exp
->master
;
1489 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1494 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1495 goto nla_put_failure
;
1496 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1497 goto nla_put_failure
;
1498 if (ctnetlink_exp_dump_tuple(skb
,
1499 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1500 CTA_EXPECT_MASTER
) < 0)
1501 goto nla_put_failure
;
1503 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1504 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1513 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1516 const struct nf_conntrack_expect
*exp
)
1518 struct nlmsghdr
*nlh
;
1519 struct nfgenmsg
*nfmsg
;
1520 unsigned char *b
= skb_tail_pointer(skb
);
1522 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1523 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1524 nfmsg
= NLMSG_DATA(nlh
);
1526 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1527 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1528 nfmsg
->version
= NFNETLINK_V0
;
1531 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1532 goto nla_put_failure
;
1534 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1543 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1544 static int ctnetlink_expect_event(struct notifier_block
*this,
1545 unsigned long events
, void *ptr
)
1547 struct nlmsghdr
*nlh
;
1548 struct nfgenmsg
*nfmsg
;
1549 struct nf_exp_event
*item
= (struct nf_exp_event
*)ptr
;
1550 struct nf_conntrack_expect
*exp
= item
->exp
;
1551 struct sk_buff
*skb
;
1556 if (events
& IPEXP_NEW
) {
1557 type
= IPCTNL_MSG_EXP_NEW
;
1558 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1562 if (!item
->report
&&
1563 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1566 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1572 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1573 nlh
= NLMSG_PUT(skb
, item
->pid
, 0, type
, sizeof(struct nfgenmsg
));
1574 nfmsg
= NLMSG_DATA(nlh
);
1576 nlh
->nlmsg_flags
= flags
;
1577 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1578 nfmsg
->version
= NFNETLINK_V0
;
1582 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1583 goto nla_put_failure
;
1586 nlh
->nlmsg_len
= skb
->tail
- b
;
1587 nfnetlink_send(skb
, item
->pid
, NFNLGRP_CONNTRACK_EXP_NEW
, item
->report
);
1595 nfnetlink_set_err(0, 0, -ENOBUFS
);
1599 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1602 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1607 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1609 struct net
*net
= &init_net
;
1610 struct nf_conntrack_expect
*exp
, *last
;
1611 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1612 struct hlist_node
*n
;
1613 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1616 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1617 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1619 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1621 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1628 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1632 if (!atomic_inc_not_zero(&exp
->use
))
1634 cb
->args
[1] = (unsigned long)exp
;
1646 nf_ct_expect_put(last
);
1651 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1652 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1653 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1657 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1658 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1660 struct nf_conntrack_tuple tuple
;
1661 struct nf_conntrack_expect
*exp
;
1662 struct sk_buff
*skb2
;
1663 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1664 u_int8_t u3
= nfmsg
->nfgen_family
;
1667 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1668 return netlink_dump_start(ctnl
, skb
, nlh
,
1669 ctnetlink_exp_dump_table
,
1670 ctnetlink_exp_done
);
1673 if (cda
[CTA_EXPECT_MASTER
])
1674 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1681 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1685 if (cda
[CTA_EXPECT_ID
]) {
1686 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1687 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1688 nf_ct_expect_put(exp
);
1694 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1699 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1700 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1706 nf_ct_expect_put(exp
);
1708 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1713 nf_ct_expect_put(exp
);
1718 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1719 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1721 struct nf_conntrack_expect
*exp
;
1722 struct nf_conntrack_tuple tuple
;
1723 struct nf_conntrack_helper
*h
;
1724 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1725 struct hlist_node
*n
, *next
;
1726 u_int8_t u3
= nfmsg
->nfgen_family
;
1730 if (cda
[CTA_EXPECT_TUPLE
]) {
1731 /* delete a single expect by tuple */
1732 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1736 /* bump usage count to 2 */
1737 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1741 if (cda
[CTA_EXPECT_ID
]) {
1742 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1743 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1744 nf_ct_expect_put(exp
);
1749 /* after list removal, usage count == 1 */
1750 nf_ct_unexpect_related(exp
);
1751 /* have to put what we 'get' above.
1752 * after this line usage count == 0 */
1753 nf_ct_expect_put(exp
);
1754 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1755 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1756 struct nf_conn_help
*m_help
;
1758 /* delete all expectations for this helper */
1759 spin_lock_bh(&nf_conntrack_lock
);
1760 h
= __nf_conntrack_helper_find_byname(name
);
1762 spin_unlock_bh(&nf_conntrack_lock
);
1765 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1766 hlist_for_each_entry_safe(exp
, n
, next
,
1767 &init_net
.ct
.expect_hash
[i
],
1769 m_help
= nfct_help(exp
->master
);
1770 if (m_help
->helper
== h
1771 && del_timer(&exp
->timeout
)) {
1772 nf_ct_unlink_expect(exp
);
1773 nf_ct_expect_put(exp
);
1777 spin_unlock_bh(&nf_conntrack_lock
);
1779 /* This basically means we have to flush everything*/
1780 spin_lock_bh(&nf_conntrack_lock
);
1781 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1782 hlist_for_each_entry_safe(exp
, n
, next
,
1783 &init_net
.ct
.expect_hash
[i
],
1785 if (del_timer(&exp
->timeout
)) {
1786 nf_ct_unlink_expect(exp
);
1787 nf_ct_expect_put(exp
);
1791 spin_unlock_bh(&nf_conntrack_lock
);
1797 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1803 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
, u32 pid
, int report
)
1805 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1806 struct nf_conntrack_tuple_hash
*h
= NULL
;
1807 struct nf_conntrack_expect
*exp
;
1809 struct nf_conn_help
*help
;
1812 /* caller guarantees that those three CTA_EXPECT_* exist */
1813 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1816 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1819 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1823 /* Look for master conntrack of this expectation */
1824 h
= nf_conntrack_find_get(&init_net
, &master_tuple
);
1827 ct
= nf_ct_tuplehash_to_ctrack(h
);
1828 help
= nfct_help(ct
);
1830 if (!help
|| !help
->helper
) {
1831 /* such conntrack hasn't got any helper, abort */
1836 exp
= nf_ct_expect_alloc(ct
);
1843 exp
->expectfn
= NULL
;
1847 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1848 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1849 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1851 err
= nf_ct_expect_related_report(exp
, pid
, report
);
1852 nf_ct_expect_put(exp
);
1855 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1860 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1861 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1863 struct nf_conntrack_tuple tuple
;
1864 struct nf_conntrack_expect
*exp
;
1865 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1866 u_int8_t u3
= nfmsg
->nfgen_family
;
1869 if (!cda
[CTA_EXPECT_TUPLE
]
1870 || !cda
[CTA_EXPECT_MASK
]
1871 || !cda
[CTA_EXPECT_MASTER
])
1874 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1878 spin_lock_bh(&nf_conntrack_lock
);
1879 exp
= __nf_ct_expect_find(&init_net
, &tuple
);
1882 spin_unlock_bh(&nf_conntrack_lock
);
1884 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1885 err
= ctnetlink_create_expect(cda
,
1887 NETLINK_CB(skb
).pid
,
1894 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1895 err
= ctnetlink_change_expect(exp
, cda
);
1896 spin_unlock_bh(&nf_conntrack_lock
);
1901 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1902 static struct notifier_block ctnl_notifier
= {
1903 .notifier_call
= ctnetlink_conntrack_event
,
1906 static struct notifier_block ctnl_notifier_exp
= {
1907 .notifier_call
= ctnetlink_expect_event
,
1911 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1912 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1913 .attr_count
= CTA_MAX
,
1914 .policy
= ct_nla_policy
},
1915 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1916 .attr_count
= CTA_MAX
,
1917 .policy
= ct_nla_policy
},
1918 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1919 .attr_count
= CTA_MAX
,
1920 .policy
= ct_nla_policy
},
1921 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1922 .attr_count
= CTA_MAX
,
1923 .policy
= ct_nla_policy
},
1926 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1927 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1928 .attr_count
= CTA_EXPECT_MAX
,
1929 .policy
= exp_nla_policy
},
1930 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1931 .attr_count
= CTA_EXPECT_MAX
,
1932 .policy
= exp_nla_policy
},
1933 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1934 .attr_count
= CTA_EXPECT_MAX
,
1935 .policy
= exp_nla_policy
},
1938 static const struct nfnetlink_subsystem ctnl_subsys
= {
1939 .name
= "conntrack",
1940 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1941 .cb_count
= IPCTNL_MSG_MAX
,
1945 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1946 .name
= "conntrack_expect",
1947 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1948 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1952 MODULE_ALIAS("ip_conntrack_netlink");
1953 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1954 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1956 static int __init
ctnetlink_init(void)
1960 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1961 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1963 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1967 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1969 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1970 goto err_unreg_subsys
;
1973 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1974 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1976 printk("ctnetlink_init: cannot register notifier.\n");
1977 goto err_unreg_exp_subsys
;
1980 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1982 printk("ctnetlink_init: cannot expect register notifier.\n");
1983 goto err_unreg_notifier
;
1989 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1991 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1992 err_unreg_exp_subsys
:
1993 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1996 nfnetlink_subsys_unregister(&ctnl_subsys
);
2001 static void __exit
ctnetlink_exit(void)
2003 printk("ctnetlink: unregistering from nfnetlink.\n");
2005 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2006 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2007 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2010 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2011 nfnetlink_subsys_unregister(&ctnl_subsys
);
2015 module_init(ctnetlink_init
);
2016 module_exit(ctnetlink_exit
);