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/types.h>
23 #include <linux/timer.h>
24 #include <linux/skbuff.h>
25 #include <linux/errno.h>
26 #include <linux/netlink.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/notifier.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
49 MODULE_LICENSE("GPL");
51 static char __initdata version
[] = "0.93";
54 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
55 const struct nf_conntrack_tuple
*tuple
,
56 struct nf_conntrack_l4proto
*l4proto
)
59 struct nlattr
*nest_parms
;
61 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
64 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
66 if (likely(l4proto
->tuple_to_nlattr
))
67 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
69 nla_nest_end(skb
, nest_parms
);
78 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
79 const struct nf_conntrack_tuple
*tuple
,
80 struct nf_conntrack_l3proto
*l3proto
)
83 struct nlattr
*nest_parms
;
85 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
89 if (likely(l3proto
->tuple_to_nlattr
))
90 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
92 nla_nest_end(skb
, nest_parms
);
101 ctnetlink_dump_tuples(struct sk_buff
*skb
,
102 const struct nf_conntrack_tuple
*tuple
)
105 struct nf_conntrack_l3proto
*l3proto
;
106 struct nf_conntrack_l4proto
*l4proto
;
108 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
109 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
111 if (unlikely(ret
< 0))
114 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
115 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
121 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
123 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
131 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
133 long timeout
= (ct
->timeout
.expires
- jiffies
) / HZ
;
138 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
146 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
148 struct nf_conntrack_l4proto
*l4proto
;
149 struct nlattr
*nest_proto
;
152 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
153 if (!l4proto
->to_nlattr
)
156 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
158 goto nla_put_failure
;
160 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
162 nla_nest_end(skb
, nest_proto
);
171 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
173 struct nlattr
*nest_helper
;
174 const struct nf_conn_help
*help
= nfct_help(ct
);
175 struct nf_conntrack_helper
*helper
;
180 helper
= rcu_dereference(help
->helper
);
184 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
186 goto nla_put_failure
;
187 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
189 if (helper
->to_nlattr
)
190 helper
->to_nlattr(skb
, ct
);
192 nla_nest_end(skb
, nest_helper
);
201 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
202 enum ip_conntrack_dir dir
)
204 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
205 struct nlattr
*nest_count
;
206 const struct nf_conn_counter
*acct
;
208 acct
= nf_conn_acct_find(ct
);
212 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
214 goto nla_put_failure
;
216 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
,
217 cpu_to_be64(acct
[dir
].packets
));
218 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
,
219 cpu_to_be64(acct
[dir
].bytes
));
221 nla_nest_end(skb
, nest_count
);
229 #ifdef CONFIG_NF_CONNTRACK_MARK
231 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
233 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
240 #define ctnetlink_dump_mark(a, b) (0)
243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
245 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
247 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
254 #define ctnetlink_dump_secmark(a, b) (0)
257 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
260 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
262 struct nlattr
*nest_parms
;
264 if (!(ct
->status
& IPS_EXPECTED
))
267 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
269 goto nla_put_failure
;
270 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
271 goto nla_put_failure
;
272 nla_nest_end(skb
, nest_parms
);
280 #ifdef CONFIG_NF_NAT_NEEDED
282 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
284 struct nlattr
*nest_parms
;
286 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
288 goto nla_put_failure
;
290 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
291 htonl(natseq
->correction_pos
));
292 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
293 htonl(natseq
->offset_before
));
294 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
295 htonl(natseq
->offset_after
));
297 nla_nest_end(skb
, nest_parms
);
306 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
308 struct nf_nat_seq
*natseq
;
309 struct nf_conn_nat
*nat
= nfct_nat(ct
);
311 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
314 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
315 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
318 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
319 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
325 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
329 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
331 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
339 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
341 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
348 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
351 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
352 int event
, int nowait
,
353 const struct nf_conn
*ct
)
355 struct nlmsghdr
*nlh
;
356 struct nfgenmsg
*nfmsg
;
357 struct nlattr
*nest_parms
;
358 unsigned char *b
= skb_tail_pointer(skb
);
360 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
361 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
362 nfmsg
= NLMSG_DATA(nlh
);
364 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
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
, 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
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
380 goto nla_put_failure
;
381 nla_nest_end(skb
, nest_parms
);
383 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
384 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
385 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
386 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
387 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
388 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
389 ctnetlink_dump_mark(skb
, ct
) < 0 ||
390 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
391 ctnetlink_dump_id(skb
, ct
) < 0 ||
392 ctnetlink_dump_use(skb
, ct
) < 0 ||
393 ctnetlink_dump_master(skb
, ct
) < 0 ||
394 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
395 goto nla_put_failure
;
397 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
406 #ifdef CONFIG_NF_CONNTRACK_EVENTS
407 static int ctnetlink_conntrack_event(struct notifier_block
*this,
408 unsigned long events
, void *ptr
)
410 struct nlmsghdr
*nlh
;
411 struct nfgenmsg
*nfmsg
;
412 struct nlattr
*nest_parms
;
413 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
417 unsigned int flags
= 0, group
;
419 /* ignore our fake conntrack entry */
420 if (ct
== &nf_conntrack_untracked
)
423 if (events
& IPCT_DESTROY
) {
424 type
= IPCTNL_MSG_CT_DELETE
;
425 group
= NFNLGRP_CONNTRACK_DESTROY
;
426 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
427 type
= IPCTNL_MSG_CT_NEW
;
428 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
429 group
= NFNLGRP_CONNTRACK_NEW
;
430 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
431 type
= IPCTNL_MSG_CT_NEW
;
432 group
= NFNLGRP_CONNTRACK_UPDATE
;
436 if (!nfnetlink_has_listeners(group
))
439 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
445 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
446 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
447 nfmsg
= NLMSG_DATA(nlh
);
449 nlh
->nlmsg_flags
= flags
;
450 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
451 nfmsg
->version
= NFNETLINK_V0
;
455 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
457 goto nla_put_failure
;
458 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
459 goto nla_put_failure
;
460 nla_nest_end(skb
, nest_parms
);
462 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
464 goto nla_put_failure
;
465 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
466 goto nla_put_failure
;
467 nla_nest_end(skb
, nest_parms
);
469 if (ctnetlink_dump_id(skb
, ct
) < 0)
470 goto nla_put_failure
;
472 if (ctnetlink_dump_status(skb
, ct
) < 0)
473 goto nla_put_failure
;
475 if (events
& IPCT_DESTROY
) {
476 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
477 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
478 goto nla_put_failure
;
480 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
481 goto nla_put_failure
;
483 if (events
& IPCT_PROTOINFO
484 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
485 goto nla_put_failure
;
487 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
488 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
489 goto nla_put_failure
;
491 #ifdef CONFIG_NF_CONNTRACK_SECMARK
492 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
493 && ctnetlink_dump_secmark(skb
, ct
) < 0)
494 goto nla_put_failure
;
497 if (events
& IPCT_RELATED
&&
498 ctnetlink_dump_master(skb
, ct
) < 0)
499 goto nla_put_failure
;
501 if (events
& IPCT_NATSEQADJ
&&
502 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
503 goto nla_put_failure
;
506 #ifdef CONFIG_NF_CONNTRACK_MARK
507 if ((events
& IPCT_MARK
|| ct
->mark
)
508 && ctnetlink_dump_mark(skb
, ct
) < 0)
509 goto nla_put_failure
;
513 nlh
->nlmsg_len
= skb
->tail
- b
;
514 nfnetlink_send(skb
, 0, group
, 0);
523 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
525 static int ctnetlink_done(struct netlink_callback
*cb
)
528 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
533 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
535 struct nf_conn
*ct
, *last
;
536 struct nf_conntrack_tuple_hash
*h
;
537 struct hlist_node
*n
;
538 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
539 u_int8_t l3proto
= nfmsg
->nfgen_family
;
542 last
= (struct nf_conn
*)cb
->args
[1];
543 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
545 hlist_for_each_entry_rcu(h
, n
, &init_net
.ct
.hash
[cb
->args
[0]],
547 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
549 ct
= nf_ct_tuplehash_to_ctrack(h
);
550 /* Dump entries of a given L3 protocol number.
551 * If it is not specified, ie. l3proto == 0,
552 * then dump everything. */
553 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
560 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
564 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
566 cb
->args
[1] = (unsigned long)ct
;
570 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
571 IPCTNL_MSG_CT_GET_CTRZERO
) {
572 struct nf_conn_counter
*acct
;
574 acct
= nf_conn_acct_find(ct
);
576 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
593 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
595 struct nlattr
*tb
[CTA_IP_MAX
+1];
596 struct nf_conntrack_l3proto
*l3proto
;
599 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
601 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
603 if (likely(l3proto
->nlattr_to_tuple
)) {
604 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
605 l3proto
->nla_policy
);
607 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
610 nf_ct_l3proto_put(l3proto
);
615 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
616 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
620 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
621 struct nf_conntrack_tuple
*tuple
)
623 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
624 struct nf_conntrack_l4proto
*l4proto
;
627 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
631 if (!tb
[CTA_PROTO_NUM
])
633 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
635 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
637 if (likely(l4proto
->nlattr_to_tuple
)) {
638 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
639 l4proto
->nla_policy
);
641 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
644 nf_ct_l4proto_put(l4proto
);
650 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
651 enum ctattr_tuple type
, u_int8_t l3num
)
653 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
656 memset(tuple
, 0, sizeof(*tuple
));
658 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
660 if (!tb
[CTA_TUPLE_IP
])
663 tuple
->src
.l3num
= l3num
;
665 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
669 if (!tb
[CTA_TUPLE_PROTO
])
672 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
676 /* orig and expect tuples get DIR_ORIGINAL */
677 if (type
== CTA_TUPLE_REPLY
)
678 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
680 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
686 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
688 struct nlattr
*tb
[CTA_HELP_MAX
+1];
690 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
692 if (!tb
[CTA_HELP_NAME
])
695 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
700 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
701 [CTA_STATUS
] = { .type
= NLA_U32
},
702 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
703 [CTA_MARK
] = { .type
= NLA_U32
},
704 [CTA_USE
] = { .type
= NLA_U32
},
705 [CTA_ID
] = { .type
= NLA_U32
},
709 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
710 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
712 struct nf_conntrack_tuple_hash
*h
;
713 struct nf_conntrack_tuple tuple
;
715 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
716 u_int8_t u3
= nfmsg
->nfgen_family
;
719 if (cda
[CTA_TUPLE_ORIG
])
720 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
721 else if (cda
[CTA_TUPLE_REPLY
])
722 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
724 /* Flush the whole table */
725 nf_conntrack_flush(&init_net
);
732 h
= nf_conntrack_find_get(&init_net
, &tuple
);
736 ct
= nf_ct_tuplehash_to_ctrack(h
);
739 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
740 if (id
!= (u32
)(unsigned long)ct
) {
753 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
754 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
756 struct nf_conntrack_tuple_hash
*h
;
757 struct nf_conntrack_tuple tuple
;
759 struct sk_buff
*skb2
= NULL
;
760 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
761 u_int8_t u3
= nfmsg
->nfgen_family
;
764 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
765 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
768 if (cda
[CTA_TUPLE_ORIG
])
769 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
770 else if (cda
[CTA_TUPLE_REPLY
])
771 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
778 h
= nf_conntrack_find_get(&init_net
, &tuple
);
782 ct
= nf_ct_tuplehash_to_ctrack(h
);
785 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
792 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
793 IPCTNL_MSG_CT_NEW
, 1, ct
);
799 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
811 #ifdef CONFIG_NF_NAT_NEEDED
813 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
814 enum nf_nat_manip_type manip
,
817 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
819 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
820 if (!parse_nat_setup
) {
821 #ifdef CONFIG_MODULES
824 if (request_module("nf-nat-ipv4") < 0) {
831 if (nfnetlink_parse_nat_setup_hook
)
837 return parse_nat_setup(ct
, manip
, attr
);
842 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
845 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
846 d
= ct
->status
^ status
;
848 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
852 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
853 /* SEEN_REPLY bit can only be set */
856 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
857 /* ASSURED bit can only be set */
860 /* Be careful here, modifying NAT bits can screw up things,
861 * so don't let users modify them directly if they don't pass
863 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
868 ctnetlink_change_nat(struct nf_conn
*ct
, struct nlattr
*cda
[])
870 #ifdef CONFIG_NF_NAT_NEEDED
873 if (cda
[CTA_NAT_DST
]) {
874 ret
= ctnetlink_parse_nat_setup(ct
,
880 if (cda
[CTA_NAT_SRC
]) {
881 ret
= ctnetlink_parse_nat_setup(ct
,
894 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
896 struct nf_conntrack_helper
*helper
;
897 struct nf_conn_help
*help
= nfct_help(ct
);
901 /* don't change helper of sibling connections */
905 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
909 if (!strcmp(helpname
, "")) {
910 if (help
&& help
->helper
) {
911 /* we had a helper before ... */
912 nf_ct_remove_expectations(ct
);
913 rcu_assign_pointer(help
->helper
, NULL
);
919 helper
= __nf_conntrack_helper_find_byname(helpname
);
924 if (help
->helper
== helper
)
928 /* need to zero data of old helper */
929 memset(&help
->help
, 0, sizeof(help
->help
));
931 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
936 rcu_assign_pointer(help
->helper
, helper
);
942 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
944 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
946 if (!del_timer(&ct
->timeout
))
949 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
950 add_timer(&ct
->timeout
);
956 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
958 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
959 struct nf_conntrack_l4proto
*l4proto
;
962 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
964 l4proto
= nf_ct_l4proto_find_get(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
965 if (l4proto
->from_nlattr
)
966 err
= l4proto
->from_nlattr(tb
, ct
);
967 nf_ct_l4proto_put(l4proto
);
972 #ifdef CONFIG_NF_NAT_NEEDED
974 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
976 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
978 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
980 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
983 natseq
->correction_pos
=
984 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
986 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
989 natseq
->offset_before
=
990 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
992 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
995 natseq
->offset_after
=
996 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1002 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1005 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1010 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1011 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1012 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1016 ct
->status
|= IPS_SEQ_ADJUST
;
1019 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1020 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1021 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1025 ct
->status
|= IPS_SEQ_ADJUST
;
1033 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1037 if (cda
[CTA_HELP
]) {
1038 err
= ctnetlink_change_helper(ct
, cda
);
1043 if (cda
[CTA_TIMEOUT
]) {
1044 err
= ctnetlink_change_timeout(ct
, cda
);
1049 if (cda
[CTA_STATUS
]) {
1050 err
= ctnetlink_change_status(ct
, cda
);
1055 if (cda
[CTA_PROTOINFO
]) {
1056 err
= ctnetlink_change_protoinfo(ct
, cda
);
1061 #if defined(CONFIG_NF_CONNTRACK_MARK)
1063 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1066 #ifdef CONFIG_NF_NAT_NEEDED
1067 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1068 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1078 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1079 struct nf_conntrack_tuple
*otuple
,
1080 struct nf_conntrack_tuple
*rtuple
,
1081 struct nf_conn
*master_ct
)
1085 struct nf_conn_help
*help
;
1086 struct nf_conntrack_helper
*helper
;
1088 ct
= nf_conntrack_alloc(&init_net
, otuple
, rtuple
, GFP_KERNEL
);
1089 if (ct
== NULL
|| IS_ERR(ct
))
1092 if (!cda
[CTA_TIMEOUT
])
1094 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1096 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1097 ct
->status
|= IPS_CONFIRMED
;
1100 helper
= __nf_ct_helper_find(rtuple
);
1102 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1108 /* not in hash table yet so not strictly necessary */
1109 rcu_assign_pointer(help
->helper
, helper
);
1112 if (cda
[CTA_STATUS
]) {
1113 err
= ctnetlink_change_status(ct
, cda
);
1120 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1121 err
= ctnetlink_change_nat(ct
, cda
);
1128 if (cda
[CTA_PROTOINFO
]) {
1129 err
= ctnetlink_change_protoinfo(ct
, cda
);
1136 nf_ct_acct_ext_add(ct
, GFP_KERNEL
);
1138 #if defined(CONFIG_NF_CONNTRACK_MARK)
1140 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1143 /* setup master conntrack: this is a confirmed expectation */
1145 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1146 ct
->master
= master_ct
;
1149 add_timer(&ct
->timeout
);
1150 nf_conntrack_hash_insert(ct
);
1156 nf_conntrack_free(ct
);
1161 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1162 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1164 struct nf_conntrack_tuple otuple
, rtuple
;
1165 struct nf_conntrack_tuple_hash
*h
= NULL
;
1166 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1167 u_int8_t u3
= nfmsg
->nfgen_family
;
1170 if (cda
[CTA_TUPLE_ORIG
]) {
1171 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1176 if (cda
[CTA_TUPLE_REPLY
]) {
1177 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1182 spin_lock_bh(&nf_conntrack_lock
);
1183 if (cda
[CTA_TUPLE_ORIG
])
1184 h
= __nf_conntrack_find(&init_net
, &otuple
);
1185 else if (cda
[CTA_TUPLE_REPLY
])
1186 h
= __nf_conntrack_find(&init_net
, &rtuple
);
1189 struct nf_conntrack_tuple master
;
1190 struct nf_conntrack_tuple_hash
*master_h
= NULL
;
1191 struct nf_conn
*master_ct
= NULL
;
1193 if (cda
[CTA_TUPLE_MASTER
]) {
1194 err
= ctnetlink_parse_tuple(cda
,
1201 master_h
= __nf_conntrack_find(&init_net
, &master
);
1202 if (master_h
== NULL
) {
1206 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1207 nf_conntrack_get(&master_ct
->ct_general
);
1210 spin_unlock_bh(&nf_conntrack_lock
);
1212 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1213 err
= ctnetlink_create_conntrack(cda
,
1217 if (err
< 0 && master_ct
)
1218 nf_ct_put(master_ct
);
1222 /* implicit 'else' */
1224 /* We manipulate the conntrack inside the global conntrack table lock,
1225 * so there's no need to increase the refcount */
1227 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1228 /* we only allow nat config for new conntracks */
1229 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1233 /* can't link an existing conntrack to a master */
1234 if (cda
[CTA_TUPLE_MASTER
]) {
1238 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
),
1243 spin_unlock_bh(&nf_conntrack_lock
);
1247 /***********************************************************************
1249 ***********************************************************************/
1252 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1253 const struct nf_conntrack_tuple
*tuple
,
1254 enum ctattr_expect type
)
1256 struct nlattr
*nest_parms
;
1258 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1260 goto nla_put_failure
;
1261 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1262 goto nla_put_failure
;
1263 nla_nest_end(skb
, nest_parms
);
1272 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1273 const struct nf_conntrack_tuple
*tuple
,
1274 const struct nf_conntrack_tuple_mask
*mask
)
1277 struct nf_conntrack_l3proto
*l3proto
;
1278 struct nf_conntrack_l4proto
*l4proto
;
1279 struct nf_conntrack_tuple m
;
1280 struct nlattr
*nest_parms
;
1282 memset(&m
, 0xFF, sizeof(m
));
1283 m
.src
.u
.all
= mask
->src
.u
.all
;
1284 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1286 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1288 goto nla_put_failure
;
1290 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1291 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1293 if (unlikely(ret
< 0))
1294 goto nla_put_failure
;
1296 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1297 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1298 if (unlikely(ret
< 0))
1299 goto nla_put_failure
;
1301 nla_nest_end(skb
, nest_parms
);
1310 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1311 const struct nf_conntrack_expect
*exp
)
1313 struct nf_conn
*master
= exp
->master
;
1314 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1319 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1320 goto nla_put_failure
;
1321 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1322 goto nla_put_failure
;
1323 if (ctnetlink_exp_dump_tuple(skb
,
1324 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1325 CTA_EXPECT_MASTER
) < 0)
1326 goto nla_put_failure
;
1328 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1329 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1338 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1341 const struct nf_conntrack_expect
*exp
)
1343 struct nlmsghdr
*nlh
;
1344 struct nfgenmsg
*nfmsg
;
1345 unsigned char *b
= skb_tail_pointer(skb
);
1347 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1348 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1349 nfmsg
= NLMSG_DATA(nlh
);
1351 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1352 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1353 nfmsg
->version
= NFNETLINK_V0
;
1356 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1357 goto nla_put_failure
;
1359 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1368 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1369 static int ctnetlink_expect_event(struct notifier_block
*this,
1370 unsigned long events
, void *ptr
)
1372 struct nlmsghdr
*nlh
;
1373 struct nfgenmsg
*nfmsg
;
1374 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1375 struct sk_buff
*skb
;
1380 if (events
& IPEXP_NEW
) {
1381 type
= IPCTNL_MSG_EXP_NEW
;
1382 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1386 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1389 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1395 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1396 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1397 nfmsg
= NLMSG_DATA(nlh
);
1399 nlh
->nlmsg_flags
= flags
;
1400 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1401 nfmsg
->version
= NFNETLINK_V0
;
1405 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1406 goto nla_put_failure
;
1409 nlh
->nlmsg_len
= skb
->tail
- b
;
1410 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1420 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1423 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1428 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1430 struct net
*net
= &init_net
;
1431 struct nf_conntrack_expect
*exp
, *last
;
1432 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1433 struct hlist_node
*n
;
1434 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1437 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1438 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1440 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1442 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1449 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1453 if (!atomic_inc_not_zero(&exp
->use
))
1455 cb
->args
[1] = (unsigned long)exp
;
1467 nf_ct_expect_put(last
);
1472 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1473 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1474 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1478 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1479 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1481 struct nf_conntrack_tuple tuple
;
1482 struct nf_conntrack_expect
*exp
;
1483 struct sk_buff
*skb2
;
1484 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1485 u_int8_t u3
= nfmsg
->nfgen_family
;
1488 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1489 return netlink_dump_start(ctnl
, skb
, nlh
,
1490 ctnetlink_exp_dump_table
,
1491 ctnetlink_exp_done
);
1494 if (cda
[CTA_EXPECT_MASTER
])
1495 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1502 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1506 if (cda
[CTA_EXPECT_ID
]) {
1507 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1508 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1509 nf_ct_expect_put(exp
);
1515 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1520 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1521 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1527 nf_ct_expect_put(exp
);
1529 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1534 nf_ct_expect_put(exp
);
1539 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1540 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1542 struct nf_conntrack_expect
*exp
;
1543 struct nf_conntrack_tuple tuple
;
1544 struct nf_conntrack_helper
*h
;
1545 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1546 struct hlist_node
*n
, *next
;
1547 u_int8_t u3
= nfmsg
->nfgen_family
;
1551 if (cda
[CTA_EXPECT_TUPLE
]) {
1552 /* delete a single expect by tuple */
1553 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1557 /* bump usage count to 2 */
1558 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1562 if (cda
[CTA_EXPECT_ID
]) {
1563 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1564 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1565 nf_ct_expect_put(exp
);
1570 /* after list removal, usage count == 1 */
1571 nf_ct_unexpect_related(exp
);
1572 /* have to put what we 'get' above.
1573 * after this line usage count == 0 */
1574 nf_ct_expect_put(exp
);
1575 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1576 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1577 struct nf_conn_help
*m_help
;
1579 /* delete all expectations for this helper */
1580 spin_lock_bh(&nf_conntrack_lock
);
1581 h
= __nf_conntrack_helper_find_byname(name
);
1583 spin_unlock_bh(&nf_conntrack_lock
);
1586 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1587 hlist_for_each_entry_safe(exp
, n
, next
,
1588 &init_net
.ct
.expect_hash
[i
],
1590 m_help
= nfct_help(exp
->master
);
1591 if (m_help
->helper
== h
1592 && del_timer(&exp
->timeout
)) {
1593 nf_ct_unlink_expect(exp
);
1594 nf_ct_expect_put(exp
);
1598 spin_unlock_bh(&nf_conntrack_lock
);
1600 /* This basically means we have to flush everything*/
1601 spin_lock_bh(&nf_conntrack_lock
);
1602 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1603 hlist_for_each_entry_safe(exp
, n
, next
,
1604 &init_net
.ct
.expect_hash
[i
],
1606 if (del_timer(&exp
->timeout
)) {
1607 nf_ct_unlink_expect(exp
);
1608 nf_ct_expect_put(exp
);
1612 spin_unlock_bh(&nf_conntrack_lock
);
1618 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1624 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
)
1626 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1627 struct nf_conntrack_tuple_hash
*h
= NULL
;
1628 struct nf_conntrack_expect
*exp
;
1630 struct nf_conn_help
*help
;
1633 /* caller guarantees that those three CTA_EXPECT_* exist */
1634 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1637 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1640 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1644 /* Look for master conntrack of this expectation */
1645 h
= nf_conntrack_find_get(&init_net
, &master_tuple
);
1648 ct
= nf_ct_tuplehash_to_ctrack(h
);
1649 help
= nfct_help(ct
);
1651 if (!help
|| !help
->helper
) {
1652 /* such conntrack hasn't got any helper, abort */
1657 exp
= nf_ct_expect_alloc(ct
);
1663 exp
->expectfn
= NULL
;
1667 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1668 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1669 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1671 err
= nf_ct_expect_related(exp
);
1672 nf_ct_expect_put(exp
);
1675 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1680 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1681 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1683 struct nf_conntrack_tuple tuple
;
1684 struct nf_conntrack_expect
*exp
;
1685 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1686 u_int8_t u3
= nfmsg
->nfgen_family
;
1689 if (!cda
[CTA_EXPECT_TUPLE
]
1690 || !cda
[CTA_EXPECT_MASK
]
1691 || !cda
[CTA_EXPECT_MASTER
])
1694 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1698 spin_lock_bh(&nf_conntrack_lock
);
1699 exp
= __nf_ct_expect_find(&init_net
, &tuple
);
1702 spin_unlock_bh(&nf_conntrack_lock
);
1704 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1705 err
= ctnetlink_create_expect(cda
, u3
);
1710 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1711 err
= ctnetlink_change_expect(exp
, cda
);
1712 spin_unlock_bh(&nf_conntrack_lock
);
1717 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1718 static struct notifier_block ctnl_notifier
= {
1719 .notifier_call
= ctnetlink_conntrack_event
,
1722 static struct notifier_block ctnl_notifier_exp
= {
1723 .notifier_call
= ctnetlink_expect_event
,
1727 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1728 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1729 .attr_count
= CTA_MAX
,
1730 .policy
= ct_nla_policy
},
1731 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1732 .attr_count
= CTA_MAX
,
1733 .policy
= ct_nla_policy
},
1734 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1735 .attr_count
= CTA_MAX
,
1736 .policy
= ct_nla_policy
},
1737 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1738 .attr_count
= CTA_MAX
,
1739 .policy
= ct_nla_policy
},
1742 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1743 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1744 .attr_count
= CTA_EXPECT_MAX
,
1745 .policy
= exp_nla_policy
},
1746 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1747 .attr_count
= CTA_EXPECT_MAX
,
1748 .policy
= exp_nla_policy
},
1749 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1750 .attr_count
= CTA_EXPECT_MAX
,
1751 .policy
= exp_nla_policy
},
1754 static const struct nfnetlink_subsystem ctnl_subsys
= {
1755 .name
= "conntrack",
1756 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1757 .cb_count
= IPCTNL_MSG_MAX
,
1761 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1762 .name
= "conntrack_expect",
1763 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1764 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1768 MODULE_ALIAS("ip_conntrack_netlink");
1769 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1770 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1772 static int __init
ctnetlink_init(void)
1776 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1777 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1779 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1783 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1785 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1786 goto err_unreg_subsys
;
1789 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1790 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1792 printk("ctnetlink_init: cannot register notifier.\n");
1793 goto err_unreg_exp_subsys
;
1796 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1798 printk("ctnetlink_init: cannot expect register notifier.\n");
1799 goto err_unreg_notifier
;
1805 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1807 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1808 err_unreg_exp_subsys
:
1809 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1812 nfnetlink_subsys_unregister(&ctnl_subsys
);
1817 static void __exit
ctnetlink_exit(void)
1819 printk("ctnetlink: unregistering from nfnetlink.\n");
1821 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1822 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1823 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1826 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1827 nfnetlink_subsys_unregister(&ctnl_subsys
);
1831 module_init(ctnetlink_init
);
1832 module_exit(ctnetlink_exit
);