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_ct_event
*item
= (struct nf_ct_event
*)ptr
;
414 struct nf_conn
*ct
= item
->ct
;
418 unsigned int flags
= 0, group
;
420 /* ignore our fake conntrack entry */
421 if (ct
== &nf_conntrack_untracked
)
424 if (events
& IPCT_DESTROY
) {
425 type
= IPCTNL_MSG_CT_DELETE
;
426 group
= NFNLGRP_CONNTRACK_DESTROY
;
427 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
428 type
= IPCTNL_MSG_CT_NEW
;
429 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
430 group
= NFNLGRP_CONNTRACK_NEW
;
431 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
432 type
= IPCTNL_MSG_CT_NEW
;
433 group
= NFNLGRP_CONNTRACK_UPDATE
;
437 if (!item
->report
&& !nfnetlink_has_listeners(group
))
440 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
446 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
447 nlh
= NLMSG_PUT(skb
, item
->pid
, 0, type
, sizeof(struct nfgenmsg
));
448 nfmsg
= NLMSG_DATA(nlh
);
450 nlh
->nlmsg_flags
= flags
;
451 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
452 nfmsg
->version
= NFNETLINK_V0
;
456 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
458 goto nla_put_failure
;
459 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
460 goto nla_put_failure
;
461 nla_nest_end(skb
, nest_parms
);
463 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
465 goto nla_put_failure
;
466 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
467 goto nla_put_failure
;
468 nla_nest_end(skb
, nest_parms
);
470 if (ctnetlink_dump_id(skb
, ct
) < 0)
471 goto nla_put_failure
;
473 if (ctnetlink_dump_status(skb
, ct
) < 0)
474 goto nla_put_failure
;
476 if (events
& IPCT_DESTROY
) {
477 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
478 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
479 goto nla_put_failure
;
481 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
482 goto nla_put_failure
;
484 if (events
& IPCT_PROTOINFO
485 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
486 goto nla_put_failure
;
488 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
489 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
490 goto nla_put_failure
;
492 #ifdef CONFIG_NF_CONNTRACK_SECMARK
493 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
494 && ctnetlink_dump_secmark(skb
, ct
) < 0)
495 goto nla_put_failure
;
498 if (events
& IPCT_RELATED
&&
499 ctnetlink_dump_master(skb
, ct
) < 0)
500 goto nla_put_failure
;
502 if (events
& IPCT_NATSEQADJ
&&
503 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
504 goto nla_put_failure
;
507 #ifdef CONFIG_NF_CONNTRACK_MARK
508 if ((events
& IPCT_MARK
|| ct
->mark
)
509 && ctnetlink_dump_mark(skb
, ct
) < 0)
510 goto nla_put_failure
;
514 nlh
->nlmsg_len
= skb
->tail
- b
;
515 nfnetlink_send(skb
, item
->pid
, group
, item
->report
);
524 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
526 static int ctnetlink_done(struct netlink_callback
*cb
)
529 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
534 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
536 struct nf_conn
*ct
, *last
;
537 struct nf_conntrack_tuple_hash
*h
;
538 struct hlist_node
*n
;
539 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
540 u_int8_t l3proto
= nfmsg
->nfgen_family
;
543 last
= (struct nf_conn
*)cb
->args
[1];
544 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
546 hlist_for_each_entry_rcu(h
, n
, &init_net
.ct
.hash
[cb
->args
[0]],
548 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
550 ct
= nf_ct_tuplehash_to_ctrack(h
);
551 /* Dump entries of a given L3 protocol number.
552 * If it is not specified, ie. l3proto == 0,
553 * then dump everything. */
554 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
561 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
565 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
567 cb
->args
[1] = (unsigned long)ct
;
571 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
572 IPCTNL_MSG_CT_GET_CTRZERO
) {
573 struct nf_conn_counter
*acct
;
575 acct
= nf_conn_acct_find(ct
);
577 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
594 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
596 struct nlattr
*tb
[CTA_IP_MAX
+1];
597 struct nf_conntrack_l3proto
*l3proto
;
600 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
602 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
604 if (likely(l3proto
->nlattr_to_tuple
)) {
605 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
606 l3proto
->nla_policy
);
608 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
611 nf_ct_l3proto_put(l3proto
);
616 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
617 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
621 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
622 struct nf_conntrack_tuple
*tuple
)
624 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
625 struct nf_conntrack_l4proto
*l4proto
;
628 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
632 if (!tb
[CTA_PROTO_NUM
])
634 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
636 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
638 if (likely(l4proto
->nlattr_to_tuple
)) {
639 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
640 l4proto
->nla_policy
);
642 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
645 nf_ct_l4proto_put(l4proto
);
651 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
652 enum ctattr_tuple type
, u_int8_t l3num
)
654 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
657 memset(tuple
, 0, sizeof(*tuple
));
659 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
661 if (!tb
[CTA_TUPLE_IP
])
664 tuple
->src
.l3num
= l3num
;
666 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
670 if (!tb
[CTA_TUPLE_PROTO
])
673 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
677 /* orig and expect tuples get DIR_ORIGINAL */
678 if (type
== CTA_TUPLE_REPLY
)
679 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
681 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
687 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
689 struct nlattr
*tb
[CTA_HELP_MAX
+1];
691 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
693 if (!tb
[CTA_HELP_NAME
])
696 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
701 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
702 [CTA_STATUS
] = { .type
= NLA_U32
},
703 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
704 [CTA_MARK
] = { .type
= NLA_U32
},
705 [CTA_USE
] = { .type
= NLA_U32
},
706 [CTA_ID
] = { .type
= NLA_U32
},
710 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
711 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
713 struct nf_conntrack_tuple_hash
*h
;
714 struct nf_conntrack_tuple tuple
;
716 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
717 u_int8_t u3
= nfmsg
->nfgen_family
;
720 if (cda
[CTA_TUPLE_ORIG
])
721 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
722 else if (cda
[CTA_TUPLE_REPLY
])
723 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
725 /* Flush the whole table */
726 nf_conntrack_flush(&init_net
,
735 h
= nf_conntrack_find_get(&init_net
, &tuple
);
739 ct
= nf_ct_tuplehash_to_ctrack(h
);
742 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
743 if (id
!= (u32
)(unsigned long)ct
) {
749 nf_conntrack_event_report(IPCT_DESTROY
,
754 /* death_by_timeout would report the event again */
755 set_bit(IPS_DYING_BIT
, &ct
->status
);
764 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
765 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
767 struct nf_conntrack_tuple_hash
*h
;
768 struct nf_conntrack_tuple tuple
;
770 struct sk_buff
*skb2
= NULL
;
771 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
772 u_int8_t u3
= nfmsg
->nfgen_family
;
775 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
776 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
779 if (cda
[CTA_TUPLE_ORIG
])
780 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
781 else if (cda
[CTA_TUPLE_REPLY
])
782 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
789 h
= nf_conntrack_find_get(&init_net
, &tuple
);
793 ct
= nf_ct_tuplehash_to_ctrack(h
);
796 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
803 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
804 IPCTNL_MSG_CT_NEW
, 1, ct
);
810 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
822 #ifdef CONFIG_NF_NAT_NEEDED
824 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
825 enum nf_nat_manip_type manip
,
828 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
830 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
831 if (!parse_nat_setup
) {
832 #ifdef CONFIG_MODULES
834 spin_unlock_bh(&nf_conntrack_lock
);
836 if (request_module("nf-nat-ipv4") < 0) {
838 spin_lock_bh(&nf_conntrack_lock
);
843 spin_lock_bh(&nf_conntrack_lock
);
845 if (nfnetlink_parse_nat_setup_hook
)
851 return parse_nat_setup(ct
, manip
, attr
);
856 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
859 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
860 d
= ct
->status
^ status
;
862 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
866 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
867 /* SEEN_REPLY bit can only be set */
870 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
871 /* ASSURED bit can only be set */
874 /* Be careful here, modifying NAT bits can screw up things,
875 * so don't let users modify them directly if they don't pass
877 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
882 ctnetlink_change_nat(struct nf_conn
*ct
, struct nlattr
*cda
[])
884 #ifdef CONFIG_NF_NAT_NEEDED
887 if (cda
[CTA_NAT_DST
]) {
888 ret
= ctnetlink_parse_nat_setup(ct
,
894 if (cda
[CTA_NAT_SRC
]) {
895 ret
= ctnetlink_parse_nat_setup(ct
,
908 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
910 struct nf_conntrack_helper
*helper
;
911 struct nf_conn_help
*help
= nfct_help(ct
);
915 /* don't change helper of sibling connections */
919 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
923 if (!strcmp(helpname
, "")) {
924 if (help
&& help
->helper
) {
925 /* we had a helper before ... */
926 nf_ct_remove_expectations(ct
);
927 rcu_assign_pointer(help
->helper
, NULL
);
933 helper
= __nf_conntrack_helper_find_byname(helpname
);
934 if (helper
== NULL
) {
935 #ifdef CONFIG_MODULES
936 spin_unlock_bh(&nf_conntrack_lock
);
938 if (request_module("nfct-helper-%s", helpname
) < 0) {
939 spin_lock_bh(&nf_conntrack_lock
);
943 spin_lock_bh(&nf_conntrack_lock
);
944 helper
= __nf_conntrack_helper_find_byname(helpname
);
952 if (help
->helper
== helper
)
956 /* need to zero data of old helper */
957 memset(&help
->help
, 0, sizeof(help
->help
));
959 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
964 rcu_assign_pointer(help
->helper
, helper
);
970 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
972 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
974 if (!del_timer(&ct
->timeout
))
977 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
978 add_timer(&ct
->timeout
);
984 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
986 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
987 struct nf_conntrack_l4proto
*l4proto
;
990 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
992 l4proto
= nf_ct_l4proto_find_get(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
993 if (l4proto
->from_nlattr
)
994 err
= l4proto
->from_nlattr(tb
, ct
);
995 nf_ct_l4proto_put(l4proto
);
1000 #ifdef CONFIG_NF_NAT_NEEDED
1002 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
1004 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1006 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
1008 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1011 natseq
->correction_pos
=
1012 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1014 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1017 natseq
->offset_before
=
1018 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1020 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1023 natseq
->offset_after
=
1024 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1030 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1033 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1038 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1039 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1040 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1044 ct
->status
|= IPS_SEQ_ADJUST
;
1047 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1048 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1049 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1053 ct
->status
|= IPS_SEQ_ADJUST
;
1061 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1065 if (cda
[CTA_HELP
]) {
1066 err
= ctnetlink_change_helper(ct
, cda
);
1071 if (cda
[CTA_TIMEOUT
]) {
1072 err
= ctnetlink_change_timeout(ct
, cda
);
1077 if (cda
[CTA_STATUS
]) {
1078 err
= ctnetlink_change_status(ct
, cda
);
1083 if (cda
[CTA_PROTOINFO
]) {
1084 err
= ctnetlink_change_protoinfo(ct
, cda
);
1089 #if defined(CONFIG_NF_CONNTRACK_MARK)
1091 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1094 #ifdef CONFIG_NF_NAT_NEEDED
1095 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1096 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1106 ctnetlink_event_report(struct nf_conn
*ct
, u32 pid
, int report
)
1108 unsigned int events
= 0;
1110 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1111 events
|= IPCT_RELATED
;
1115 nf_conntrack_event_report(IPCT_STATUS
|
1128 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1129 struct nf_conntrack_tuple
*otuple
,
1130 struct nf_conntrack_tuple
*rtuple
,
1131 struct nf_conn
*master_ct
,
1137 struct nf_conntrack_helper
*helper
;
1139 ct
= nf_conntrack_alloc(&init_net
, otuple
, rtuple
, GFP_ATOMIC
);
1143 if (!cda
[CTA_TIMEOUT
])
1145 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1147 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1148 ct
->status
|= IPS_CONFIRMED
;
1151 if (cda
[CTA_HELP
]) {
1154 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1160 helper
= __nf_conntrack_helper_find_byname(helpname
);
1161 if (helper
== NULL
) {
1163 #ifdef CONFIG_MODULES
1164 if (request_module("nfct-helper-%s", helpname
) < 0) {
1170 helper
= __nf_conntrack_helper_find_byname(helpname
);
1181 struct nf_conn_help
*help
;
1183 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1190 /* not in hash table yet so not strictly necessary */
1191 rcu_assign_pointer(help
->helper
, helper
);
1194 /* try an implicit helper assignation */
1195 err
= __nf_ct_try_assign_helper(ct
, GFP_ATOMIC
);
1202 if (cda
[CTA_STATUS
]) {
1203 err
= ctnetlink_change_status(ct
, cda
);
1210 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1211 err
= ctnetlink_change_nat(ct
, cda
);
1218 #ifdef CONFIG_NF_NAT_NEEDED
1219 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1220 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1228 if (cda
[CTA_PROTOINFO
]) {
1229 err
= ctnetlink_change_protoinfo(ct
, cda
);
1236 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1238 #if defined(CONFIG_NF_CONNTRACK_MARK)
1240 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1243 /* setup master conntrack: this is a confirmed expectation */
1245 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1246 ct
->master
= master_ct
;
1249 nf_conntrack_get(&ct
->ct_general
);
1250 add_timer(&ct
->timeout
);
1251 nf_conntrack_hash_insert(ct
);
1253 ctnetlink_event_report(ct
, pid
, report
);
1259 nf_conntrack_free(ct
);
1264 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1265 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1267 struct nf_conntrack_tuple otuple
, rtuple
;
1268 struct nf_conntrack_tuple_hash
*h
= NULL
;
1269 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1270 u_int8_t u3
= nfmsg
->nfgen_family
;
1273 if (cda
[CTA_TUPLE_ORIG
]) {
1274 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1279 if (cda
[CTA_TUPLE_REPLY
]) {
1280 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1285 spin_lock_bh(&nf_conntrack_lock
);
1286 if (cda
[CTA_TUPLE_ORIG
])
1287 h
= __nf_conntrack_find(&init_net
, &otuple
);
1288 else if (cda
[CTA_TUPLE_REPLY
])
1289 h
= __nf_conntrack_find(&init_net
, &rtuple
);
1292 struct nf_conntrack_tuple master
;
1293 struct nf_conntrack_tuple_hash
*master_h
= NULL
;
1294 struct nf_conn
*master_ct
= NULL
;
1296 if (cda
[CTA_TUPLE_MASTER
]) {
1297 err
= ctnetlink_parse_tuple(cda
,
1304 master_h
= __nf_conntrack_find(&init_net
, &master
);
1305 if (master_h
== NULL
) {
1309 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1310 nf_conntrack_get(&master_ct
->ct_general
);
1314 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1315 err
= ctnetlink_create_conntrack(cda
,
1319 NETLINK_CB(skb
).pid
,
1321 spin_unlock_bh(&nf_conntrack_lock
);
1322 if (err
< 0 && master_ct
)
1323 nf_ct_put(master_ct
);
1327 /* implicit 'else' */
1329 /* We manipulate the conntrack inside the global conntrack table lock,
1330 * so there's no need to increase the refcount */
1332 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1333 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1335 /* we only allow nat config for new conntracks */
1336 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1340 /* can't link an existing conntrack to a master */
1341 if (cda
[CTA_TUPLE_MASTER
]) {
1346 err
= ctnetlink_change_conntrack(ct
, cda
);
1348 nf_conntrack_get(&ct
->ct_general
);
1349 spin_unlock_bh(&nf_conntrack_lock
);
1350 ctnetlink_event_report(ct
,
1351 NETLINK_CB(skb
).pid
,
1355 spin_unlock_bh(&nf_conntrack_lock
);
1361 spin_unlock_bh(&nf_conntrack_lock
);
1365 /***********************************************************************
1367 ***********************************************************************/
1370 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1371 const struct nf_conntrack_tuple
*tuple
,
1372 enum ctattr_expect type
)
1374 struct nlattr
*nest_parms
;
1376 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1378 goto nla_put_failure
;
1379 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1380 goto nla_put_failure
;
1381 nla_nest_end(skb
, nest_parms
);
1390 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1391 const struct nf_conntrack_tuple
*tuple
,
1392 const struct nf_conntrack_tuple_mask
*mask
)
1395 struct nf_conntrack_l3proto
*l3proto
;
1396 struct nf_conntrack_l4proto
*l4proto
;
1397 struct nf_conntrack_tuple m
;
1398 struct nlattr
*nest_parms
;
1400 memset(&m
, 0xFF, sizeof(m
));
1401 m
.src
.u
.all
= mask
->src
.u
.all
;
1402 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1404 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1406 goto nla_put_failure
;
1408 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1409 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1411 if (unlikely(ret
< 0))
1412 goto nla_put_failure
;
1414 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1415 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1416 if (unlikely(ret
< 0))
1417 goto nla_put_failure
;
1419 nla_nest_end(skb
, nest_parms
);
1428 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1429 const struct nf_conntrack_expect
*exp
)
1431 struct nf_conn
*master
= exp
->master
;
1432 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1437 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1438 goto nla_put_failure
;
1439 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1440 goto nla_put_failure
;
1441 if (ctnetlink_exp_dump_tuple(skb
,
1442 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1443 CTA_EXPECT_MASTER
) < 0)
1444 goto nla_put_failure
;
1446 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1447 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1456 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1459 const struct nf_conntrack_expect
*exp
)
1461 struct nlmsghdr
*nlh
;
1462 struct nfgenmsg
*nfmsg
;
1463 unsigned char *b
= skb_tail_pointer(skb
);
1465 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1466 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1467 nfmsg
= NLMSG_DATA(nlh
);
1469 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1470 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1471 nfmsg
->version
= NFNETLINK_V0
;
1474 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1475 goto nla_put_failure
;
1477 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1486 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1487 static int ctnetlink_expect_event(struct notifier_block
*this,
1488 unsigned long events
, void *ptr
)
1490 struct nlmsghdr
*nlh
;
1491 struct nfgenmsg
*nfmsg
;
1492 struct nf_exp_event
*item
= (struct nf_exp_event
*)ptr
;
1493 struct nf_conntrack_expect
*exp
= item
->exp
;
1494 struct sk_buff
*skb
;
1499 if (events
& IPEXP_NEW
) {
1500 type
= IPCTNL_MSG_EXP_NEW
;
1501 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1505 if (!item
->report
&&
1506 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1509 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1515 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1516 nlh
= NLMSG_PUT(skb
, item
->pid
, 0, type
, sizeof(struct nfgenmsg
));
1517 nfmsg
= NLMSG_DATA(nlh
);
1519 nlh
->nlmsg_flags
= flags
;
1520 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1521 nfmsg
->version
= NFNETLINK_V0
;
1525 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1526 goto nla_put_failure
;
1529 nlh
->nlmsg_len
= skb
->tail
- b
;
1530 nfnetlink_send(skb
, item
->pid
, NFNLGRP_CONNTRACK_EXP_NEW
, item
->report
);
1540 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1543 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1548 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1550 struct net
*net
= &init_net
;
1551 struct nf_conntrack_expect
*exp
, *last
;
1552 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1553 struct hlist_node
*n
;
1554 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1557 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1558 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1560 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1562 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1569 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1573 if (!atomic_inc_not_zero(&exp
->use
))
1575 cb
->args
[1] = (unsigned long)exp
;
1587 nf_ct_expect_put(last
);
1592 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1593 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1594 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1598 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1599 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1601 struct nf_conntrack_tuple tuple
;
1602 struct nf_conntrack_expect
*exp
;
1603 struct sk_buff
*skb2
;
1604 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1605 u_int8_t u3
= nfmsg
->nfgen_family
;
1608 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1609 return netlink_dump_start(ctnl
, skb
, nlh
,
1610 ctnetlink_exp_dump_table
,
1611 ctnetlink_exp_done
);
1614 if (cda
[CTA_EXPECT_MASTER
])
1615 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1622 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1626 if (cda
[CTA_EXPECT_ID
]) {
1627 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1628 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1629 nf_ct_expect_put(exp
);
1635 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1640 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1641 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1647 nf_ct_expect_put(exp
);
1649 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1654 nf_ct_expect_put(exp
);
1659 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1660 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1662 struct nf_conntrack_expect
*exp
;
1663 struct nf_conntrack_tuple tuple
;
1664 struct nf_conntrack_helper
*h
;
1665 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1666 struct hlist_node
*n
, *next
;
1667 u_int8_t u3
= nfmsg
->nfgen_family
;
1671 if (cda
[CTA_EXPECT_TUPLE
]) {
1672 /* delete a single expect by tuple */
1673 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1677 /* bump usage count to 2 */
1678 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1682 if (cda
[CTA_EXPECT_ID
]) {
1683 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1684 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1685 nf_ct_expect_put(exp
);
1690 /* after list removal, usage count == 1 */
1691 nf_ct_unexpect_related(exp
);
1692 /* have to put what we 'get' above.
1693 * after this line usage count == 0 */
1694 nf_ct_expect_put(exp
);
1695 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1696 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1697 struct nf_conn_help
*m_help
;
1699 /* delete all expectations for this helper */
1700 spin_lock_bh(&nf_conntrack_lock
);
1701 h
= __nf_conntrack_helper_find_byname(name
);
1703 spin_unlock_bh(&nf_conntrack_lock
);
1706 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1707 hlist_for_each_entry_safe(exp
, n
, next
,
1708 &init_net
.ct
.expect_hash
[i
],
1710 m_help
= nfct_help(exp
->master
);
1711 if (m_help
->helper
== h
1712 && del_timer(&exp
->timeout
)) {
1713 nf_ct_unlink_expect(exp
);
1714 nf_ct_expect_put(exp
);
1718 spin_unlock_bh(&nf_conntrack_lock
);
1720 /* This basically means we have to flush everything*/
1721 spin_lock_bh(&nf_conntrack_lock
);
1722 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1723 hlist_for_each_entry_safe(exp
, n
, next
,
1724 &init_net
.ct
.expect_hash
[i
],
1726 if (del_timer(&exp
->timeout
)) {
1727 nf_ct_unlink_expect(exp
);
1728 nf_ct_expect_put(exp
);
1732 spin_unlock_bh(&nf_conntrack_lock
);
1738 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1744 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
, u32 pid
, int report
)
1746 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1747 struct nf_conntrack_tuple_hash
*h
= NULL
;
1748 struct nf_conntrack_expect
*exp
;
1750 struct nf_conn_help
*help
;
1753 /* caller guarantees that those three CTA_EXPECT_* exist */
1754 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1757 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1760 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1764 /* Look for master conntrack of this expectation */
1765 h
= nf_conntrack_find_get(&init_net
, &master_tuple
);
1768 ct
= nf_ct_tuplehash_to_ctrack(h
);
1769 help
= nfct_help(ct
);
1771 if (!help
|| !help
->helper
) {
1772 /* such conntrack hasn't got any helper, abort */
1777 exp
= nf_ct_expect_alloc(ct
);
1784 exp
->expectfn
= NULL
;
1788 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1789 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1790 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1792 err
= nf_ct_expect_related_report(exp
, pid
, report
);
1793 nf_ct_expect_put(exp
);
1796 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1801 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1802 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1804 struct nf_conntrack_tuple tuple
;
1805 struct nf_conntrack_expect
*exp
;
1806 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1807 u_int8_t u3
= nfmsg
->nfgen_family
;
1810 if (!cda
[CTA_EXPECT_TUPLE
]
1811 || !cda
[CTA_EXPECT_MASK
]
1812 || !cda
[CTA_EXPECT_MASTER
])
1815 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1819 spin_lock_bh(&nf_conntrack_lock
);
1820 exp
= __nf_ct_expect_find(&init_net
, &tuple
);
1823 spin_unlock_bh(&nf_conntrack_lock
);
1825 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1826 err
= ctnetlink_create_expect(cda
,
1828 NETLINK_CB(skb
).pid
,
1835 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1836 err
= ctnetlink_change_expect(exp
, cda
);
1837 spin_unlock_bh(&nf_conntrack_lock
);
1842 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1843 static struct notifier_block ctnl_notifier
= {
1844 .notifier_call
= ctnetlink_conntrack_event
,
1847 static struct notifier_block ctnl_notifier_exp
= {
1848 .notifier_call
= ctnetlink_expect_event
,
1852 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1853 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1854 .attr_count
= CTA_MAX
,
1855 .policy
= ct_nla_policy
},
1856 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1857 .attr_count
= CTA_MAX
,
1858 .policy
= ct_nla_policy
},
1859 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1860 .attr_count
= CTA_MAX
,
1861 .policy
= ct_nla_policy
},
1862 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1863 .attr_count
= CTA_MAX
,
1864 .policy
= ct_nla_policy
},
1867 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1868 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1869 .attr_count
= CTA_EXPECT_MAX
,
1870 .policy
= exp_nla_policy
},
1871 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1872 .attr_count
= CTA_EXPECT_MAX
,
1873 .policy
= exp_nla_policy
},
1874 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1875 .attr_count
= CTA_EXPECT_MAX
,
1876 .policy
= exp_nla_policy
},
1879 static const struct nfnetlink_subsystem ctnl_subsys
= {
1880 .name
= "conntrack",
1881 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1882 .cb_count
= IPCTNL_MSG_MAX
,
1886 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1887 .name
= "conntrack_expect",
1888 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1889 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1893 MODULE_ALIAS("ip_conntrack_netlink");
1894 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1895 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1897 static int __init
ctnetlink_init(void)
1901 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1902 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1904 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1908 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1910 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1911 goto err_unreg_subsys
;
1914 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1915 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1917 printk("ctnetlink_init: cannot register notifier.\n");
1918 goto err_unreg_exp_subsys
;
1921 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1923 printk("ctnetlink_init: cannot expect register notifier.\n");
1924 goto err_unreg_notifier
;
1930 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1932 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1933 err_unreg_exp_subsys
:
1934 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1937 nfnetlink_subsys_unregister(&ctnl_subsys
);
1942 static void __exit
ctnetlink_exit(void)
1944 printk("ctnetlink: unregistering from nfnetlink.\n");
1946 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1947 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1948 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1951 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1952 nfnetlink_subsys_unregister(&ctnl_subsys
);
1956 module_init(ctnetlink_init
);
1957 module_exit(ctnetlink_exit
);