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
408 static int ctnetlink_conntrack_event(struct notifier_block
*this,
409 unsigned long events
, void *ptr
)
411 struct nlmsghdr
*nlh
;
412 struct nfgenmsg
*nfmsg
;
413 struct nlattr
*nest_parms
;
414 struct nf_ct_event
*item
= (struct nf_ct_event
*)ptr
;
415 struct nf_conn
*ct
= item
->ct
;
419 unsigned int flags
= 0, group
;
421 /* ignore our fake conntrack entry */
422 if (ct
== &nf_conntrack_untracked
)
425 if (events
& IPCT_DESTROY
) {
426 type
= IPCTNL_MSG_CT_DELETE
;
427 group
= NFNLGRP_CONNTRACK_DESTROY
;
428 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
429 type
= IPCTNL_MSG_CT_NEW
;
430 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
431 group
= NFNLGRP_CONNTRACK_NEW
;
432 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
433 type
= IPCTNL_MSG_CT_NEW
;
434 group
= NFNLGRP_CONNTRACK_UPDATE
;
438 if (!item
->report
&& !nfnetlink_has_listeners(group
))
441 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
447 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
448 nlh
= NLMSG_PUT(skb
, item
->pid
, 0, type
, sizeof(struct nfgenmsg
));
449 nfmsg
= NLMSG_DATA(nlh
);
451 nlh
->nlmsg_flags
= flags
;
452 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
453 nfmsg
->version
= NFNETLINK_V0
;
457 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
459 goto nla_put_failure
;
460 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
461 goto nla_put_failure
;
462 nla_nest_end(skb
, nest_parms
);
464 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
466 goto nla_put_failure
;
467 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
468 goto nla_put_failure
;
469 nla_nest_end(skb
, nest_parms
);
471 if (ctnetlink_dump_id(skb
, ct
) < 0)
472 goto nla_put_failure
;
474 if (ctnetlink_dump_status(skb
, ct
) < 0)
475 goto nla_put_failure
;
477 if (events
& IPCT_DESTROY
) {
478 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
479 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
480 goto nla_put_failure
;
482 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
483 goto nla_put_failure
;
485 if (events
& IPCT_PROTOINFO
486 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
487 goto nla_put_failure
;
489 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
490 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
491 goto nla_put_failure
;
493 #ifdef CONFIG_NF_CONNTRACK_SECMARK
494 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
495 && ctnetlink_dump_secmark(skb
, ct
) < 0)
496 goto nla_put_failure
;
499 if (events
& IPCT_RELATED
&&
500 ctnetlink_dump_master(skb
, ct
) < 0)
501 goto nla_put_failure
;
503 if (events
& IPCT_NATSEQADJ
&&
504 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
505 goto nla_put_failure
;
508 #ifdef CONFIG_NF_CONNTRACK_MARK
509 if ((events
& IPCT_MARK
|| ct
->mark
)
510 && ctnetlink_dump_mark(skb
, ct
) < 0)
511 goto nla_put_failure
;
515 nlh
->nlmsg_len
= skb
->tail
- b
;
516 nfnetlink_send(skb
, item
->pid
, group
, item
->report
);
522 nfnetlink_set_err(0, group
, -ENOBUFS
);
526 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
528 static int ctnetlink_done(struct netlink_callback
*cb
)
531 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
536 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
538 struct nf_conn
*ct
, *last
;
539 struct nf_conntrack_tuple_hash
*h
;
540 struct hlist_nulls_node
*n
;
541 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
542 u_int8_t l3proto
= nfmsg
->nfgen_family
;
545 last
= (struct nf_conn
*)cb
->args
[1];
546 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
548 hlist_nulls_for_each_entry_rcu(h
, n
, &init_net
.ct
.hash
[cb
->args
[0]],
550 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
552 ct
= nf_ct_tuplehash_to_ctrack(h
);
553 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
555 /* Dump entries of a given L3 protocol number.
556 * If it is not specified, ie. l3proto == 0,
557 * then dump everything. */
558 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
565 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
569 cb
->args
[1] = (unsigned long)ct
;
573 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
574 IPCTNL_MSG_CT_GET_CTRZERO
) {
575 struct nf_conn_counter
*acct
;
577 acct
= nf_conn_acct_find(ct
);
579 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
598 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
600 struct nlattr
*tb
[CTA_IP_MAX
+1];
601 struct nf_conntrack_l3proto
*l3proto
;
604 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
607 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
609 if (likely(l3proto
->nlattr_to_tuple
)) {
610 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
611 l3proto
->nla_policy
);
613 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
621 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
622 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
626 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
627 struct nf_conntrack_tuple
*tuple
)
629 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
630 struct nf_conntrack_l4proto
*l4proto
;
633 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
637 if (!tb
[CTA_PROTO_NUM
])
639 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
642 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
644 if (likely(l4proto
->nlattr_to_tuple
)) {
645 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
646 l4proto
->nla_policy
);
648 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
657 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
658 enum ctattr_tuple type
, u_int8_t l3num
)
660 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
663 memset(tuple
, 0, sizeof(*tuple
));
665 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
667 if (!tb
[CTA_TUPLE_IP
])
670 tuple
->src
.l3num
= l3num
;
672 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
676 if (!tb
[CTA_TUPLE_PROTO
])
679 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
683 /* orig and expect tuples get DIR_ORIGINAL */
684 if (type
== CTA_TUPLE_REPLY
)
685 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
687 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
693 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
695 struct nlattr
*tb
[CTA_HELP_MAX
+1];
697 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
699 if (!tb
[CTA_HELP_NAME
])
702 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
707 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
708 [CTA_STATUS
] = { .type
= NLA_U32
},
709 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
710 [CTA_MARK
] = { .type
= NLA_U32
},
711 [CTA_USE
] = { .type
= NLA_U32
},
712 [CTA_ID
] = { .type
= NLA_U32
},
716 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
717 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
719 struct nf_conntrack_tuple_hash
*h
;
720 struct nf_conntrack_tuple tuple
;
722 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
723 u_int8_t u3
= nfmsg
->nfgen_family
;
726 if (cda
[CTA_TUPLE_ORIG
])
727 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
728 else if (cda
[CTA_TUPLE_REPLY
])
729 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
731 /* Flush the whole table */
732 nf_conntrack_flush(&init_net
,
741 h
= nf_conntrack_find_get(&init_net
, &tuple
);
745 ct
= nf_ct_tuplehash_to_ctrack(h
);
748 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
749 if (id
!= (u32
)(unsigned long)ct
) {
755 nf_conntrack_event_report(IPCT_DESTROY
,
760 /* death_by_timeout would report the event again */
761 set_bit(IPS_DYING_BIT
, &ct
->status
);
770 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
771 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
773 struct nf_conntrack_tuple_hash
*h
;
774 struct nf_conntrack_tuple tuple
;
776 struct sk_buff
*skb2
= NULL
;
777 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
778 u_int8_t u3
= nfmsg
->nfgen_family
;
781 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
782 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
785 if (cda
[CTA_TUPLE_ORIG
])
786 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
787 else if (cda
[CTA_TUPLE_REPLY
])
788 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
795 h
= nf_conntrack_find_get(&init_net
, &tuple
);
799 ct
= nf_ct_tuplehash_to_ctrack(h
);
802 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
809 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
810 IPCTNL_MSG_CT_NEW
, 1, ct
);
816 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
828 #ifdef CONFIG_NF_NAT_NEEDED
830 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
831 enum nf_nat_manip_type manip
,
834 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
836 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
837 if (!parse_nat_setup
) {
838 #ifdef CONFIG_MODULES
840 spin_unlock_bh(&nf_conntrack_lock
);
842 if (request_module("nf-nat-ipv4") < 0) {
844 spin_lock_bh(&nf_conntrack_lock
);
849 spin_lock_bh(&nf_conntrack_lock
);
851 if (nfnetlink_parse_nat_setup_hook
)
857 return parse_nat_setup(ct
, manip
, attr
);
862 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
865 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
866 d
= ct
->status
^ status
;
868 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
872 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
873 /* SEEN_REPLY bit can only be set */
876 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
877 /* ASSURED bit can only be set */
880 /* Be careful here, modifying NAT bits can screw up things,
881 * so don't let users modify them directly if they don't pass
883 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
888 ctnetlink_change_nat(struct nf_conn
*ct
, struct nlattr
*cda
[])
890 #ifdef CONFIG_NF_NAT_NEEDED
893 if (cda
[CTA_NAT_DST
]) {
894 ret
= ctnetlink_parse_nat_setup(ct
,
900 if (cda
[CTA_NAT_SRC
]) {
901 ret
= ctnetlink_parse_nat_setup(ct
,
914 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
916 struct nf_conntrack_helper
*helper
;
917 struct nf_conn_help
*help
= nfct_help(ct
);
921 /* don't change helper of sibling connections */
925 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
929 if (!strcmp(helpname
, "")) {
930 if (help
&& help
->helper
) {
931 /* we had a helper before ... */
932 nf_ct_remove_expectations(ct
);
933 rcu_assign_pointer(help
->helper
, NULL
);
939 helper
= __nf_conntrack_helper_find_byname(helpname
);
940 if (helper
== NULL
) {
941 #ifdef CONFIG_MODULES
942 spin_unlock_bh(&nf_conntrack_lock
);
944 if (request_module("nfct-helper-%s", helpname
) < 0) {
945 spin_lock_bh(&nf_conntrack_lock
);
949 spin_lock_bh(&nf_conntrack_lock
);
950 helper
= __nf_conntrack_helper_find_byname(helpname
);
958 if (help
->helper
== helper
)
962 /* need to zero data of old helper */
963 memset(&help
->help
, 0, sizeof(help
->help
));
965 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
970 rcu_assign_pointer(help
->helper
, helper
);
976 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
978 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
980 if (!del_timer(&ct
->timeout
))
983 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
984 add_timer(&ct
->timeout
);
990 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
992 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
993 struct nf_conntrack_l4proto
*l4proto
;
996 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
999 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1000 if (l4proto
->from_nlattr
)
1001 err
= l4proto
->from_nlattr(tb
, ct
);
1007 #ifdef CONFIG_NF_NAT_NEEDED
1009 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
1011 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1013 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
1015 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1018 natseq
->correction_pos
=
1019 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1021 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1024 natseq
->offset_before
=
1025 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1027 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1030 natseq
->offset_after
=
1031 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1037 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1040 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1045 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1046 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1047 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1051 ct
->status
|= IPS_SEQ_ADJUST
;
1054 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1055 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1056 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1060 ct
->status
|= IPS_SEQ_ADJUST
;
1068 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1072 /* only allow NAT changes and master assignation for new conntracks */
1073 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1076 if (cda
[CTA_HELP
]) {
1077 err
= ctnetlink_change_helper(ct
, cda
);
1082 if (cda
[CTA_TIMEOUT
]) {
1083 err
= ctnetlink_change_timeout(ct
, cda
);
1088 if (cda
[CTA_STATUS
]) {
1089 err
= ctnetlink_change_status(ct
, cda
);
1094 if (cda
[CTA_PROTOINFO
]) {
1095 err
= ctnetlink_change_protoinfo(ct
, cda
);
1100 #if defined(CONFIG_NF_CONNTRACK_MARK)
1102 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1105 #ifdef CONFIG_NF_NAT_NEEDED
1106 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1107 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1117 ctnetlink_event_report(struct nf_conn
*ct
, u32 pid
, int report
)
1119 unsigned int events
= 0;
1121 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1122 events
|= IPCT_RELATED
;
1126 nf_conntrack_event_report(IPCT_STATUS
|
1138 static struct nf_conn
*
1139 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1140 struct nf_conntrack_tuple
*otuple
,
1141 struct nf_conntrack_tuple
*rtuple
,
1146 struct nf_conntrack_helper
*helper
;
1148 ct
= nf_conntrack_alloc(&init_net
, otuple
, rtuple
, GFP_ATOMIC
);
1150 return ERR_PTR(-ENOMEM
);
1152 if (!cda
[CTA_TIMEOUT
])
1154 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1156 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1157 ct
->status
|= IPS_CONFIRMED
;
1160 if (cda
[CTA_HELP
]) {
1163 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1167 helper
= __nf_conntrack_helper_find_byname(helpname
);
1168 if (helper
== NULL
) {
1170 #ifdef CONFIG_MODULES
1171 if (request_module("nfct-helper-%s", helpname
) < 0) {
1177 helper
= __nf_conntrack_helper_find_byname(helpname
);
1187 struct nf_conn_help
*help
;
1189 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1195 /* not in hash table yet so not strictly necessary */
1196 rcu_assign_pointer(help
->helper
, helper
);
1199 /* try an implicit helper assignation */
1200 err
= __nf_ct_try_assign_helper(ct
, GFP_ATOMIC
);
1205 if (cda
[CTA_STATUS
]) {
1206 err
= ctnetlink_change_status(ct
, cda
);
1211 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1212 err
= ctnetlink_change_nat(ct
, cda
);
1217 #ifdef CONFIG_NF_NAT_NEEDED
1218 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1219 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1225 if (cda
[CTA_PROTOINFO
]) {
1226 err
= ctnetlink_change_protoinfo(ct
, cda
);
1231 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1233 #if defined(CONFIG_NF_CONNTRACK_MARK)
1235 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1238 /* setup master conntrack: this is a confirmed expectation */
1239 if (cda
[CTA_TUPLE_MASTER
]) {
1240 struct nf_conntrack_tuple master
;
1241 struct nf_conntrack_tuple_hash
*master_h
;
1242 struct nf_conn
*master_ct
;
1244 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1248 master_h
= nf_conntrack_find_get(&init_net
, &master
);
1249 if (master_h
== NULL
) {
1253 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1254 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1255 ct
->master
= master_ct
;
1258 add_timer(&ct
->timeout
);
1259 nf_conntrack_hash_insert(ct
);
1267 nf_conntrack_free(ct
);
1268 return ERR_PTR(err
);
1272 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1273 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1275 struct nf_conntrack_tuple otuple
, rtuple
;
1276 struct nf_conntrack_tuple_hash
*h
= NULL
;
1277 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1278 u_int8_t u3
= nfmsg
->nfgen_family
;
1281 if (cda
[CTA_TUPLE_ORIG
]) {
1282 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1287 if (cda
[CTA_TUPLE_REPLY
]) {
1288 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1293 spin_lock_bh(&nf_conntrack_lock
);
1294 if (cda
[CTA_TUPLE_ORIG
])
1295 h
= __nf_conntrack_find(&init_net
, &otuple
);
1296 else if (cda
[CTA_TUPLE_REPLY
])
1297 h
= __nf_conntrack_find(&init_net
, &rtuple
);
1301 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1304 ct
= ctnetlink_create_conntrack(cda
, &otuple
,
1311 nf_conntrack_get(&ct
->ct_general
);
1312 spin_unlock_bh(&nf_conntrack_lock
);
1313 ctnetlink_event_report(ct
,
1314 NETLINK_CB(skb
).pid
,
1318 spin_unlock_bh(&nf_conntrack_lock
);
1322 /* implicit 'else' */
1324 /* We manipulate the conntrack inside the global conntrack table lock,
1325 * so there's no need to increase the refcount */
1327 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1328 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1330 err
= ctnetlink_change_conntrack(ct
, cda
);
1332 nf_conntrack_get(&ct
->ct_general
);
1333 spin_unlock_bh(&nf_conntrack_lock
);
1334 ctnetlink_event_report(ct
,
1335 NETLINK_CB(skb
).pid
,
1339 spin_unlock_bh(&nf_conntrack_lock
);
1345 spin_unlock_bh(&nf_conntrack_lock
);
1349 /***********************************************************************
1351 ***********************************************************************/
1354 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1355 const struct nf_conntrack_tuple
*tuple
,
1356 enum ctattr_expect type
)
1358 struct nlattr
*nest_parms
;
1360 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1362 goto nla_put_failure
;
1363 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1364 goto nla_put_failure
;
1365 nla_nest_end(skb
, nest_parms
);
1374 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1375 const struct nf_conntrack_tuple
*tuple
,
1376 const struct nf_conntrack_tuple_mask
*mask
)
1379 struct nf_conntrack_l3proto
*l3proto
;
1380 struct nf_conntrack_l4proto
*l4proto
;
1381 struct nf_conntrack_tuple m
;
1382 struct nlattr
*nest_parms
;
1384 memset(&m
, 0xFF, sizeof(m
));
1385 m
.src
.u
.all
= mask
->src
.u
.all
;
1386 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1388 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1390 goto nla_put_failure
;
1392 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1393 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1395 if (unlikely(ret
< 0))
1396 goto nla_put_failure
;
1398 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1399 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1400 if (unlikely(ret
< 0))
1401 goto nla_put_failure
;
1403 nla_nest_end(skb
, nest_parms
);
1412 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1413 const struct nf_conntrack_expect
*exp
)
1415 struct nf_conn
*master
= exp
->master
;
1416 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1421 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1422 goto nla_put_failure
;
1423 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1424 goto nla_put_failure
;
1425 if (ctnetlink_exp_dump_tuple(skb
,
1426 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1427 CTA_EXPECT_MASTER
) < 0)
1428 goto nla_put_failure
;
1430 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1431 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1440 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1443 const struct nf_conntrack_expect
*exp
)
1445 struct nlmsghdr
*nlh
;
1446 struct nfgenmsg
*nfmsg
;
1447 unsigned char *b
= skb_tail_pointer(skb
);
1449 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1450 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1451 nfmsg
= NLMSG_DATA(nlh
);
1453 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1454 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1455 nfmsg
->version
= NFNETLINK_V0
;
1458 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1459 goto nla_put_failure
;
1461 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1470 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1471 static int ctnetlink_expect_event(struct notifier_block
*this,
1472 unsigned long events
, void *ptr
)
1474 struct nlmsghdr
*nlh
;
1475 struct nfgenmsg
*nfmsg
;
1476 struct nf_exp_event
*item
= (struct nf_exp_event
*)ptr
;
1477 struct nf_conntrack_expect
*exp
= item
->exp
;
1478 struct sk_buff
*skb
;
1483 if (events
& IPEXP_NEW
) {
1484 type
= IPCTNL_MSG_EXP_NEW
;
1485 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1489 if (!item
->report
&&
1490 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1493 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1499 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1500 nlh
= NLMSG_PUT(skb
, item
->pid
, 0, type
, sizeof(struct nfgenmsg
));
1501 nfmsg
= NLMSG_DATA(nlh
);
1503 nlh
->nlmsg_flags
= flags
;
1504 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1505 nfmsg
->version
= NFNETLINK_V0
;
1509 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1510 goto nla_put_failure
;
1513 nlh
->nlmsg_len
= skb
->tail
- b
;
1514 nfnetlink_send(skb
, item
->pid
, NFNLGRP_CONNTRACK_EXP_NEW
, item
->report
);
1520 nfnetlink_set_err(0, 0, -ENOBUFS
);
1525 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1528 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1533 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1535 struct net
*net
= &init_net
;
1536 struct nf_conntrack_expect
*exp
, *last
;
1537 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1538 struct hlist_node
*n
;
1539 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1542 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1543 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1545 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1547 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1554 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1558 if (!atomic_inc_not_zero(&exp
->use
))
1560 cb
->args
[1] = (unsigned long)exp
;
1572 nf_ct_expect_put(last
);
1577 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1578 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1579 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1583 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1584 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1586 struct nf_conntrack_tuple tuple
;
1587 struct nf_conntrack_expect
*exp
;
1588 struct sk_buff
*skb2
;
1589 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1590 u_int8_t u3
= nfmsg
->nfgen_family
;
1593 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1594 return netlink_dump_start(ctnl
, skb
, nlh
,
1595 ctnetlink_exp_dump_table
,
1596 ctnetlink_exp_done
);
1599 if (cda
[CTA_EXPECT_MASTER
])
1600 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1607 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1611 if (cda
[CTA_EXPECT_ID
]) {
1612 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1613 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1614 nf_ct_expect_put(exp
);
1620 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1625 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1626 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1632 nf_ct_expect_put(exp
);
1634 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1639 nf_ct_expect_put(exp
);
1644 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1645 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1647 struct nf_conntrack_expect
*exp
;
1648 struct nf_conntrack_tuple tuple
;
1649 struct nf_conntrack_helper
*h
;
1650 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1651 struct hlist_node
*n
, *next
;
1652 u_int8_t u3
= nfmsg
->nfgen_family
;
1656 if (cda
[CTA_EXPECT_TUPLE
]) {
1657 /* delete a single expect by tuple */
1658 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1662 /* bump usage count to 2 */
1663 exp
= nf_ct_expect_find_get(&init_net
, &tuple
);
1667 if (cda
[CTA_EXPECT_ID
]) {
1668 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1669 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1670 nf_ct_expect_put(exp
);
1675 /* after list removal, usage count == 1 */
1676 nf_ct_unexpect_related(exp
);
1677 /* have to put what we 'get' above.
1678 * after this line usage count == 0 */
1679 nf_ct_expect_put(exp
);
1680 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1681 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1682 struct nf_conn_help
*m_help
;
1684 /* delete all expectations for this helper */
1685 spin_lock_bh(&nf_conntrack_lock
);
1686 h
= __nf_conntrack_helper_find_byname(name
);
1688 spin_unlock_bh(&nf_conntrack_lock
);
1691 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1692 hlist_for_each_entry_safe(exp
, n
, next
,
1693 &init_net
.ct
.expect_hash
[i
],
1695 m_help
= nfct_help(exp
->master
);
1696 if (m_help
->helper
== h
1697 && del_timer(&exp
->timeout
)) {
1698 nf_ct_unlink_expect(exp
);
1699 nf_ct_expect_put(exp
);
1703 spin_unlock_bh(&nf_conntrack_lock
);
1705 /* This basically means we have to flush everything*/
1706 spin_lock_bh(&nf_conntrack_lock
);
1707 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1708 hlist_for_each_entry_safe(exp
, n
, next
,
1709 &init_net
.ct
.expect_hash
[i
],
1711 if (del_timer(&exp
->timeout
)) {
1712 nf_ct_unlink_expect(exp
);
1713 nf_ct_expect_put(exp
);
1717 spin_unlock_bh(&nf_conntrack_lock
);
1723 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1729 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
, u32 pid
, int report
)
1731 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1732 struct nf_conntrack_tuple_hash
*h
= NULL
;
1733 struct nf_conntrack_expect
*exp
;
1735 struct nf_conn_help
*help
;
1738 /* caller guarantees that those three CTA_EXPECT_* exist */
1739 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1742 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1745 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1749 /* Look for master conntrack of this expectation */
1750 h
= nf_conntrack_find_get(&init_net
, &master_tuple
);
1753 ct
= nf_ct_tuplehash_to_ctrack(h
);
1754 help
= nfct_help(ct
);
1756 if (!help
|| !help
->helper
) {
1757 /* such conntrack hasn't got any helper, abort */
1762 exp
= nf_ct_expect_alloc(ct
);
1768 exp
->expectfn
= NULL
;
1772 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1773 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1774 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1776 err
= nf_ct_expect_related_report(exp
, pid
, report
);
1777 nf_ct_expect_put(exp
);
1780 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1785 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1786 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1788 struct nf_conntrack_tuple tuple
;
1789 struct nf_conntrack_expect
*exp
;
1790 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1791 u_int8_t u3
= nfmsg
->nfgen_family
;
1794 if (!cda
[CTA_EXPECT_TUPLE
]
1795 || !cda
[CTA_EXPECT_MASK
]
1796 || !cda
[CTA_EXPECT_MASTER
])
1799 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1803 spin_lock_bh(&nf_conntrack_lock
);
1804 exp
= __nf_ct_expect_find(&init_net
, &tuple
);
1807 spin_unlock_bh(&nf_conntrack_lock
);
1809 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1810 err
= ctnetlink_create_expect(cda
,
1812 NETLINK_CB(skb
).pid
,
1819 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1820 err
= ctnetlink_change_expect(exp
, cda
);
1821 spin_unlock_bh(&nf_conntrack_lock
);
1826 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1827 static struct notifier_block ctnl_notifier
= {
1828 .notifier_call
= ctnetlink_conntrack_event
,
1831 static struct notifier_block ctnl_notifier_exp
= {
1832 .notifier_call
= ctnetlink_expect_event
,
1836 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1837 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1838 .attr_count
= CTA_MAX
,
1839 .policy
= ct_nla_policy
},
1840 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1841 .attr_count
= CTA_MAX
,
1842 .policy
= ct_nla_policy
},
1843 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1844 .attr_count
= CTA_MAX
,
1845 .policy
= ct_nla_policy
},
1846 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1847 .attr_count
= CTA_MAX
,
1848 .policy
= ct_nla_policy
},
1851 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1852 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1853 .attr_count
= CTA_EXPECT_MAX
,
1854 .policy
= exp_nla_policy
},
1855 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1856 .attr_count
= CTA_EXPECT_MAX
,
1857 .policy
= exp_nla_policy
},
1858 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1859 .attr_count
= CTA_EXPECT_MAX
,
1860 .policy
= exp_nla_policy
},
1863 static const struct nfnetlink_subsystem ctnl_subsys
= {
1864 .name
= "conntrack",
1865 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1866 .cb_count
= IPCTNL_MSG_MAX
,
1870 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1871 .name
= "conntrack_expect",
1872 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1873 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1877 MODULE_ALIAS("ip_conntrack_netlink");
1878 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1879 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1881 static int __init
ctnetlink_init(void)
1885 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1886 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1888 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1892 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1894 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1895 goto err_unreg_subsys
;
1898 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1899 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1901 printk("ctnetlink_init: cannot register notifier.\n");
1902 goto err_unreg_exp_subsys
;
1905 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1907 printk("ctnetlink_init: cannot expect register notifier.\n");
1908 goto err_unreg_notifier
;
1914 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1916 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1917 err_unreg_exp_subsys
:
1918 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1921 nfnetlink_subsys_unregister(&ctnl_subsys
);
1926 static void __exit
ctnetlink_exit(void)
1928 printk("ctnetlink: unregistering from nfnetlink.\n");
1930 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1931 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1932 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1935 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1936 nfnetlink_subsys_unregister(&ctnl_subsys
);
1940 module_init(ctnetlink_init
);
1941 module_exit(ctnetlink_exit
);