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-2007 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/types.h>
22 #include <linux/timer.h>
23 #include <linux/skbuff.h>
24 #include <linux/errno.h>
25 #include <linux/netlink.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/notifier.h>
30 #include <linux/netfilter.h>
31 #include <net/netlink.h>
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_expect.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_l3proto.h>
37 #include <net/netfilter/nf_conntrack_l4proto.h>
38 #include <net/netfilter/nf_conntrack_tuple.h>
39 #ifdef CONFIG_NF_NAT_NEEDED
40 #include <net/netfilter/nf_nat_core.h>
41 #include <net/netfilter/nf_nat_protocol.h>
44 #include <linux/netfilter/nfnetlink.h>
45 #include <linux/netfilter/nfnetlink_conntrack.h>
47 MODULE_LICENSE("GPL");
49 static char __initdata version
[] = "0.93";
52 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
53 const struct nf_conntrack_tuple
*tuple
,
54 struct nf_conntrack_l4proto
*l4proto
)
57 struct nlattr
*nest_parms
;
59 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
62 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
64 if (likely(l4proto
->tuple_to_nlattr
))
65 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
67 nla_nest_end(skb
, nest_parms
);
76 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
77 const struct nf_conntrack_tuple
*tuple
,
78 struct nf_conntrack_l3proto
*l3proto
)
81 struct nlattr
*nest_parms
;
83 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
87 if (likely(l3proto
->tuple_to_nlattr
))
88 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
90 nla_nest_end(skb
, nest_parms
);
99 ctnetlink_dump_tuples(struct sk_buff
*skb
,
100 const struct nf_conntrack_tuple
*tuple
)
103 struct nf_conntrack_l3proto
*l3proto
;
104 struct nf_conntrack_l4proto
*l4proto
;
106 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
107 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
108 nf_ct_l3proto_put(l3proto
);
110 if (unlikely(ret
< 0))
113 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
114 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
115 nf_ct_l4proto_put(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
= nf_ct_l4proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
, ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
149 struct nlattr
*nest_proto
;
152 if (!l4proto
->to_nlattr
) {
153 nf_ct_l4proto_put(l4proto
);
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 nf_ct_l4proto_put(l4proto
);
165 nla_nest_end(skb
, nest_proto
);
170 nf_ct_l4proto_put(l4proto
);
175 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
177 struct nlattr
*nest_helper
;
178 const struct nf_conn_help
*help
= nfct_help(ct
);
179 struct nf_conntrack_helper
*helper
;
185 helper
= rcu_dereference(help
->helper
);
189 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
191 goto nla_put_failure
;
192 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
194 if (helper
->to_nlattr
)
195 helper
->to_nlattr(skb
, ct
);
197 nla_nest_end(skb
, nest_helper
);
207 #ifdef CONFIG_NF_CT_ACCT
209 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
210 enum ip_conntrack_dir dir
)
212 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
213 struct nlattr
*nest_count
;
215 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
217 goto nla_put_failure
;
219 NLA_PUT_BE32(skb
, CTA_COUNTERS32_PACKETS
,
220 htonl(ct
->counters
[dir
].packets
));
221 NLA_PUT_BE32(skb
, CTA_COUNTERS32_BYTES
,
222 htonl(ct
->counters
[dir
].bytes
));
224 nla_nest_end(skb
, nest_count
);
232 #define ctnetlink_dump_counters(a, b, c) (0)
235 #ifdef CONFIG_NF_CONNTRACK_MARK
237 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
239 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
246 #define ctnetlink_dump_mark(a, b) (0)
249 #ifdef CONFIG_NF_CONNTRACK_SECMARK
251 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
253 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
260 #define ctnetlink_dump_secmark(a, b) (0)
263 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
266 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
268 struct nlattr
*nest_parms
;
270 if (!(ct
->status
& IPS_EXPECTED
))
273 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
275 goto nla_put_failure
;
276 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
277 goto nla_put_failure
;
278 nla_nest_end(skb
, nest_parms
);
286 #ifdef CONFIG_NF_NAT_NEEDED
288 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
290 struct nlattr
*nest_parms
;
292 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
294 goto nla_put_failure
;
296 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
297 htonl(natseq
->correction_pos
));
298 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
299 htonl(natseq
->offset_before
));
300 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
301 htonl(natseq
->offset_after
));
303 nla_nest_end(skb
, nest_parms
);
312 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
314 struct nf_nat_seq
*natseq
;
315 struct nf_conn_nat
*nat
= nfct_nat(ct
);
317 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
320 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
321 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
324 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
325 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
331 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
335 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
337 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
345 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
347 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
354 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
357 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
358 int event
, int nowait
,
359 const struct nf_conn
*ct
)
361 struct nlmsghdr
*nlh
;
362 struct nfgenmsg
*nfmsg
;
363 struct nlattr
*nest_parms
;
364 unsigned char *b
= skb_tail_pointer(skb
);
366 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
367 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
368 nfmsg
= NLMSG_DATA(nlh
);
370 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
371 nfmsg
->nfgen_family
=
372 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
373 nfmsg
->version
= NFNETLINK_V0
;
376 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
378 goto nla_put_failure
;
379 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
380 goto nla_put_failure
;
381 nla_nest_end(skb
, nest_parms
);
383 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
385 goto nla_put_failure
;
386 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
387 goto nla_put_failure
;
388 nla_nest_end(skb
, nest_parms
);
390 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
391 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
392 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
393 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
394 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
395 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
396 ctnetlink_dump_mark(skb
, ct
) < 0 ||
397 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
398 ctnetlink_dump_id(skb
, ct
) < 0 ||
399 ctnetlink_dump_use(skb
, ct
) < 0 ||
400 ctnetlink_dump_master(skb
, ct
) < 0 ||
401 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
402 goto nla_put_failure
;
404 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
413 #ifdef CONFIG_NF_CONNTRACK_EVENTS
414 static int ctnetlink_conntrack_event(struct notifier_block
*this,
415 unsigned long events
, void *ptr
)
417 struct nlmsghdr
*nlh
;
418 struct nfgenmsg
*nfmsg
;
419 struct nlattr
*nest_parms
;
420 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
424 unsigned int flags
= 0, group
;
426 /* ignore our fake conntrack entry */
427 if (ct
== &nf_conntrack_untracked
)
430 if (events
& IPCT_DESTROY
) {
431 type
= IPCTNL_MSG_CT_DELETE
;
432 group
= NFNLGRP_CONNTRACK_DESTROY
;
433 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
434 type
= IPCTNL_MSG_CT_NEW
;
435 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
436 group
= NFNLGRP_CONNTRACK_NEW
;
437 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
438 type
= IPCTNL_MSG_CT_NEW
;
439 group
= NFNLGRP_CONNTRACK_UPDATE
;
443 if (!nfnetlink_has_listeners(group
))
446 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
452 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
453 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
454 nfmsg
= NLMSG_DATA(nlh
);
456 nlh
->nlmsg_flags
= flags
;
457 nfmsg
->nfgen_family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
458 nfmsg
->version
= NFNETLINK_V0
;
461 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
463 goto nla_put_failure
;
464 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
465 goto nla_put_failure
;
466 nla_nest_end(skb
, nest_parms
);
468 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
470 goto nla_put_failure
;
471 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
472 goto nla_put_failure
;
473 nla_nest_end(skb
, nest_parms
);
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_status(skb
, ct
) < 0)
481 goto nla_put_failure
;
483 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
484 goto nla_put_failure
;
486 if (events
& IPCT_PROTOINFO
487 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
488 goto nla_put_failure
;
490 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
491 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
492 goto nla_put_failure
;
494 #ifdef CONFIG_NF_CONNTRACK_MARK
495 if ((events
& IPCT_MARK
|| ct
->mark
)
496 && ctnetlink_dump_mark(skb
, ct
) < 0)
497 goto nla_put_failure
;
499 #ifdef CONFIG_NF_CONNTRACK_SECMARK
500 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
501 && ctnetlink_dump_secmark(skb
, ct
) < 0)
502 goto nla_put_failure
;
505 if (events
& IPCT_COUNTER_FILLING
&&
506 (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
507 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0))
508 goto nla_put_failure
;
510 if (events
& IPCT_RELATED
&&
511 ctnetlink_dump_master(skb
, ct
) < 0)
512 goto nla_put_failure
;
514 if (events
& IPCT_NATSEQADJ
&&
515 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
516 goto nla_put_failure
;
519 nlh
->nlmsg_len
= skb
->tail
- b
;
520 nfnetlink_send(skb
, 0, group
, 0);
528 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
530 static int ctnetlink_done(struct netlink_callback
*cb
)
533 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
537 #define L3PROTO(ct) (ct)->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
540 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
542 struct nf_conn
*ct
, *last
;
543 struct nf_conntrack_tuple_hash
*h
;
544 struct hlist_node
*n
;
545 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
546 u_int8_t l3proto
= nfmsg
->nfgen_family
;
549 last
= (struct nf_conn
*)cb
->args
[1];
550 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
552 hlist_for_each_entry_rcu(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
554 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
556 ct
= nf_ct_tuplehash_to_ctrack(h
);
557 /* Dump entries of a given L3 protocol number.
558 * If it is not specified, ie. l3proto == 0,
559 * then dump everything. */
560 if (l3proto
&& L3PROTO(ct
) != l3proto
)
567 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
571 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
573 cb
->args
[1] = (unsigned long)ct
;
576 #ifdef CONFIG_NF_CT_ACCT
577 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
578 IPCTNL_MSG_CT_GET_CTRZERO
)
579 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
596 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
598 struct nlattr
*tb
[CTA_IP_MAX
+1];
599 struct nf_conntrack_l3proto
*l3proto
;
602 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
604 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
606 if (likely(l3proto
->nlattr_to_tuple
)) {
607 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
608 l3proto
->nla_policy
);
610 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
613 nf_ct_l3proto_put(l3proto
);
618 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
619 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
623 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
624 struct nf_conntrack_tuple
*tuple
)
626 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
627 struct nf_conntrack_l4proto
*l4proto
;
630 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
634 if (!tb
[CTA_PROTO_NUM
])
636 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
638 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
640 if (likely(l4proto
->nlattr_to_tuple
)) {
641 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
642 l4proto
->nla_policy
);
644 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
647 nf_ct_l4proto_put(l4proto
);
653 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
654 enum ctattr_tuple type
, u_int8_t l3num
)
656 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
659 memset(tuple
, 0, sizeof(*tuple
));
661 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
663 if (!tb
[CTA_TUPLE_IP
])
666 tuple
->src
.l3num
= l3num
;
668 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
672 if (!tb
[CTA_TUPLE_PROTO
])
675 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
679 /* orig and expect tuples get DIR_ORIGINAL */
680 if (type
== CTA_TUPLE_REPLY
)
681 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
683 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
688 #ifdef CONFIG_NF_NAT_NEEDED
689 static const struct nla_policy protonat_nla_policy
[CTA_PROTONAT_MAX
+1] = {
690 [CTA_PROTONAT_PORT_MIN
] = { .type
= NLA_U16
},
691 [CTA_PROTONAT_PORT_MAX
] = { .type
= NLA_U16
},
694 static int nfnetlink_parse_nat_proto(struct nlattr
*attr
,
695 const struct nf_conn
*ct
,
696 struct nf_nat_range
*range
)
698 struct nlattr
*tb
[CTA_PROTONAT_MAX
+1];
699 const struct nf_nat_protocol
*npt
;
702 err
= nla_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
, protonat_nla_policy
);
706 npt
= nf_nat_proto_find_get(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
);
708 if (!npt
->nlattr_to_range
) {
709 nf_nat_proto_put(npt
);
713 /* nlattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
714 if (npt
->nlattr_to_range(tb
, range
) > 0)
715 range
->flags
|= IP_NAT_RANGE_PROTO_SPECIFIED
;
717 nf_nat_proto_put(npt
);
722 static const struct nla_policy nat_nla_policy
[CTA_NAT_MAX
+1] = {
723 [CTA_NAT_MINIP
] = { .type
= NLA_U32
},
724 [CTA_NAT_MAXIP
] = { .type
= NLA_U32
},
728 nfnetlink_parse_nat(struct nlattr
*nat
,
729 const struct nf_conn
*ct
, struct nf_nat_range
*range
)
731 struct nlattr
*tb
[CTA_NAT_MAX
+1];
734 memset(range
, 0, sizeof(*range
));
736 err
= nla_parse_nested(tb
, CTA_NAT_MAX
, nat
, nat_nla_policy
);
740 if (tb
[CTA_NAT_MINIP
])
741 range
->min_ip
= nla_get_be32(tb
[CTA_NAT_MINIP
]);
743 if (!tb
[CTA_NAT_MAXIP
])
744 range
->max_ip
= range
->min_ip
;
746 range
->max_ip
= nla_get_be32(tb
[CTA_NAT_MAXIP
]);
749 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
751 if (!tb
[CTA_NAT_PROTO
])
754 err
= nfnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
], ct
, range
);
763 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
765 struct nlattr
*tb
[CTA_HELP_MAX
+1];
767 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
769 if (!tb
[CTA_HELP_NAME
])
772 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
777 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
778 [CTA_STATUS
] = { .type
= NLA_U32
},
779 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
780 [CTA_MARK
] = { .type
= NLA_U32
},
781 [CTA_USE
] = { .type
= NLA_U32
},
782 [CTA_ID
] = { .type
= NLA_U32
},
786 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
787 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
789 struct nf_conntrack_tuple_hash
*h
;
790 struct nf_conntrack_tuple tuple
;
792 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
793 u_int8_t u3
= nfmsg
->nfgen_family
;
796 if (cda
[CTA_TUPLE_ORIG
])
797 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
798 else if (cda
[CTA_TUPLE_REPLY
])
799 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
801 /* Flush the whole table */
802 nf_conntrack_flush();
809 h
= nf_conntrack_find_get(&tuple
);
813 ct
= nf_ct_tuplehash_to_ctrack(h
);
816 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
817 if (id
!= (u32
)(unsigned long)ct
) {
822 if (del_timer(&ct
->timeout
))
823 ct
->timeout
.function((unsigned long)ct
);
831 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
832 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
834 struct nf_conntrack_tuple_hash
*h
;
835 struct nf_conntrack_tuple tuple
;
837 struct sk_buff
*skb2
= NULL
;
838 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
839 u_int8_t u3
= nfmsg
->nfgen_family
;
842 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
843 #ifndef CONFIG_NF_CT_ACCT
844 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == IPCTNL_MSG_CT_GET_CTRZERO
)
847 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
851 if (cda
[CTA_TUPLE_ORIG
])
852 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
853 else if (cda
[CTA_TUPLE_REPLY
])
854 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
861 h
= nf_conntrack_find_get(&tuple
);
865 ct
= nf_ct_tuplehash_to_ctrack(h
);
868 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
874 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
875 IPCTNL_MSG_CT_NEW
, 1, ct
);
880 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
893 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
896 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
897 d
= ct
->status
^ status
;
899 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
903 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
904 /* SEEN_REPLY bit can only be set */
908 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
909 /* ASSURED bit can only be set */
912 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
913 #ifndef CONFIG_NF_NAT_NEEDED
916 struct nf_nat_range range
;
918 if (cda
[CTA_NAT_DST
]) {
919 if (nfnetlink_parse_nat(cda
[CTA_NAT_DST
], ct
,
922 if (nf_nat_initialized(ct
, IP_NAT_MANIP_DST
))
924 nf_nat_setup_info(ct
, &range
, IP_NAT_MANIP_DST
);
926 if (cda
[CTA_NAT_SRC
]) {
927 if (nfnetlink_parse_nat(cda
[CTA_NAT_SRC
], ct
,
930 if (nf_nat_initialized(ct
, IP_NAT_MANIP_SRC
))
932 nf_nat_setup_info(ct
, &range
, IP_NAT_MANIP_SRC
);
937 /* Be careful here, modifying NAT bits can screw up things,
938 * so don't let users modify them directly if they don't pass
940 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
946 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
948 struct nf_conntrack_helper
*helper
;
949 struct nf_conn_help
*help
= nfct_help(ct
);
953 /* don't change helper of sibling connections */
957 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
961 if (!strcmp(helpname
, "")) {
962 if (help
&& help
->helper
) {
963 /* we had a helper before ... */
964 nf_ct_remove_expectations(ct
);
965 rcu_assign_pointer(help
->helper
, NULL
);
971 helper
= __nf_conntrack_helper_find_byname(helpname
);
976 if (help
->helper
== helper
)
980 /* need to zero data of old helper */
981 memset(&help
->help
, 0, sizeof(help
->help
));
983 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
988 rcu_assign_pointer(help
->helper
, helper
);
994 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
996 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
998 if (!del_timer(&ct
->timeout
))
1001 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1002 add_timer(&ct
->timeout
);
1008 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
1010 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
1011 struct nf_conntrack_l4proto
*l4proto
;
1012 u_int16_t npt
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
;
1013 u_int16_t l3num
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
1016 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
1018 l4proto
= nf_ct_l4proto_find_get(l3num
, npt
);
1020 if (l4proto
->from_nlattr
)
1021 err
= l4proto
->from_nlattr(tb
, ct
);
1022 nf_ct_l4proto_put(l4proto
);
1027 #ifdef CONFIG_NF_NAT_NEEDED
1029 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
1031 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1033 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
1035 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1038 natseq
->correction_pos
=
1039 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1041 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1044 natseq
->offset_before
=
1045 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1047 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1050 natseq
->offset_after
=
1051 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1057 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1060 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1065 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1066 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1067 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1071 ct
->status
|= IPS_SEQ_ADJUST
;
1074 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1075 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1076 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1080 ct
->status
|= IPS_SEQ_ADJUST
;
1088 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1092 if (cda
[CTA_HELP
]) {
1093 err
= ctnetlink_change_helper(ct
, cda
);
1098 if (cda
[CTA_TIMEOUT
]) {
1099 err
= ctnetlink_change_timeout(ct
, cda
);
1104 if (cda
[CTA_STATUS
]) {
1105 err
= ctnetlink_change_status(ct
, cda
);
1110 if (cda
[CTA_PROTOINFO
]) {
1111 err
= ctnetlink_change_protoinfo(ct
, cda
);
1116 #if defined(CONFIG_NF_CONNTRACK_MARK)
1118 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1121 #ifdef CONFIG_NF_NAT_NEEDED
1122 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1123 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1133 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1134 struct nf_conntrack_tuple
*otuple
,
1135 struct nf_conntrack_tuple
*rtuple
,
1136 struct nf_conn
*master_ct
)
1140 struct nf_conn_help
*help
;
1141 struct nf_conntrack_helper
*helper
;
1143 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1144 if (ct
== NULL
|| IS_ERR(ct
))
1147 if (!cda
[CTA_TIMEOUT
])
1149 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1151 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1152 ct
->status
|= IPS_CONFIRMED
;
1154 if (cda
[CTA_STATUS
]) {
1155 err
= ctnetlink_change_status(ct
, cda
);
1160 if (cda
[CTA_PROTOINFO
]) {
1161 err
= ctnetlink_change_protoinfo(ct
, cda
);
1166 #if defined(CONFIG_NF_CONNTRACK_MARK)
1168 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1172 helper
= __nf_ct_helper_find(rtuple
);
1174 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
1180 /* not in hash table yet so not strictly necessary */
1181 rcu_assign_pointer(help
->helper
, helper
);
1184 /* setup master conntrack: this is a confirmed expectation */
1186 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1187 ct
->master
= master_ct
;
1190 add_timer(&ct
->timeout
);
1191 nf_conntrack_hash_insert(ct
);
1197 nf_conntrack_free(ct
);
1202 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1203 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1205 struct nf_conntrack_tuple otuple
, rtuple
;
1206 struct nf_conntrack_tuple_hash
*h
= NULL
;
1207 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1208 u_int8_t u3
= nfmsg
->nfgen_family
;
1211 if (cda
[CTA_TUPLE_ORIG
]) {
1212 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1217 if (cda
[CTA_TUPLE_REPLY
]) {
1218 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1223 spin_lock_bh(&nf_conntrack_lock
);
1224 if (cda
[CTA_TUPLE_ORIG
])
1225 h
= __nf_conntrack_find(&otuple
);
1226 else if (cda
[CTA_TUPLE_REPLY
])
1227 h
= __nf_conntrack_find(&rtuple
);
1230 struct nf_conntrack_tuple master
;
1231 struct nf_conntrack_tuple_hash
*master_h
= NULL
;
1232 struct nf_conn
*master_ct
= NULL
;
1234 if (cda
[CTA_TUPLE_MASTER
]) {
1235 err
= ctnetlink_parse_tuple(cda
,
1242 master_h
= __nf_conntrack_find(&master
);
1243 if (master_h
== NULL
) {
1247 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1248 atomic_inc(&master_ct
->ct_general
.use
);
1251 spin_unlock_bh(&nf_conntrack_lock
);
1253 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1254 err
= ctnetlink_create_conntrack(cda
,
1258 if (err
< 0 && master_ct
)
1259 nf_ct_put(master_ct
);
1263 /* implicit 'else' */
1265 /* We manipulate the conntrack inside the global conntrack table lock,
1266 * so there's no need to increase the refcount */
1268 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1269 /* we only allow nat config for new conntracks */
1270 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1274 /* can't link an existing conntrack to a master */
1275 if (cda
[CTA_TUPLE_MASTER
]) {
1279 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
),
1284 spin_unlock_bh(&nf_conntrack_lock
);
1288 /***********************************************************************
1290 ***********************************************************************/
1293 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1294 const struct nf_conntrack_tuple
*tuple
,
1295 enum ctattr_expect type
)
1297 struct nlattr
*nest_parms
;
1299 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1301 goto nla_put_failure
;
1302 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1303 goto nla_put_failure
;
1304 nla_nest_end(skb
, nest_parms
);
1313 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1314 const struct nf_conntrack_tuple
*tuple
,
1315 const struct nf_conntrack_tuple_mask
*mask
)
1318 struct nf_conntrack_l3proto
*l3proto
;
1319 struct nf_conntrack_l4proto
*l4proto
;
1320 struct nf_conntrack_tuple m
;
1321 struct nlattr
*nest_parms
;
1323 memset(&m
, 0xFF, sizeof(m
));
1324 m
.src
.u
.all
= mask
->src
.u
.all
;
1325 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1327 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1329 goto nla_put_failure
;
1331 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1332 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1333 nf_ct_l3proto_put(l3proto
);
1335 if (unlikely(ret
< 0))
1336 goto nla_put_failure
;
1338 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1339 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1340 nf_ct_l4proto_put(l4proto
);
1341 if (unlikely(ret
< 0))
1342 goto nla_put_failure
;
1344 nla_nest_end(skb
, nest_parms
);
1353 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1354 const struct nf_conntrack_expect
*exp
)
1356 struct nf_conn
*master
= exp
->master
;
1357 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1362 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1363 goto nla_put_failure
;
1364 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1365 goto nla_put_failure
;
1366 if (ctnetlink_exp_dump_tuple(skb
,
1367 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1368 CTA_EXPECT_MASTER
) < 0)
1369 goto nla_put_failure
;
1371 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1372 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1381 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1384 const struct nf_conntrack_expect
*exp
)
1386 struct nlmsghdr
*nlh
;
1387 struct nfgenmsg
*nfmsg
;
1388 unsigned char *b
= skb_tail_pointer(skb
);
1390 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1391 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1392 nfmsg
= NLMSG_DATA(nlh
);
1394 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1395 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1396 nfmsg
->version
= NFNETLINK_V0
;
1399 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1400 goto nla_put_failure
;
1402 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1411 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1412 static int ctnetlink_expect_event(struct notifier_block
*this,
1413 unsigned long events
, void *ptr
)
1415 struct nlmsghdr
*nlh
;
1416 struct nfgenmsg
*nfmsg
;
1417 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1418 struct sk_buff
*skb
;
1423 if (events
& IPEXP_NEW
) {
1424 type
= IPCTNL_MSG_EXP_NEW
;
1425 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1429 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1432 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1438 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1439 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1440 nfmsg
= NLMSG_DATA(nlh
);
1442 nlh
->nlmsg_flags
= flags
;
1443 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1444 nfmsg
->version
= NFNETLINK_V0
;
1447 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1448 goto nla_put_failure
;
1450 nlh
->nlmsg_len
= skb
->tail
- b
;
1451 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1460 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1463 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1468 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1470 struct nf_conntrack_expect
*exp
, *last
;
1471 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1472 struct hlist_node
*n
;
1473 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1476 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1477 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1479 hlist_for_each_entry(exp
, n
, &nf_ct_expect_hash
[cb
->args
[0]],
1481 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1488 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1492 if (!atomic_inc_not_zero(&exp
->use
))
1494 cb
->args
[1] = (unsigned long)exp
;
1506 nf_ct_expect_put(last
);
1511 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1512 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1513 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1517 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1518 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1520 struct nf_conntrack_tuple tuple
;
1521 struct nf_conntrack_expect
*exp
;
1522 struct sk_buff
*skb2
;
1523 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1524 u_int8_t u3
= nfmsg
->nfgen_family
;
1527 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1528 return netlink_dump_start(ctnl
, skb
, nlh
,
1529 ctnetlink_exp_dump_table
,
1530 ctnetlink_exp_done
);
1533 if (cda
[CTA_EXPECT_MASTER
])
1534 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1541 exp
= nf_ct_expect_find_get(&tuple
);
1545 if (cda
[CTA_EXPECT_ID
]) {
1546 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1547 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1548 nf_ct_expect_put(exp
);
1554 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1558 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1559 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1564 nf_ct_expect_put(exp
);
1566 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1571 nf_ct_expect_put(exp
);
1576 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1577 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1579 struct nf_conntrack_expect
*exp
;
1580 struct nf_conntrack_tuple tuple
;
1581 struct nf_conntrack_helper
*h
;
1582 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1583 struct hlist_node
*n
, *next
;
1584 u_int8_t u3
= nfmsg
->nfgen_family
;
1588 if (cda
[CTA_EXPECT_TUPLE
]) {
1589 /* delete a single expect by tuple */
1590 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1594 /* bump usage count to 2 */
1595 exp
= nf_ct_expect_find_get(&tuple
);
1599 if (cda
[CTA_EXPECT_ID
]) {
1600 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1601 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1602 nf_ct_expect_put(exp
);
1607 /* after list removal, usage count == 1 */
1608 nf_ct_unexpect_related(exp
);
1609 /* have to put what we 'get' above.
1610 * after this line usage count == 0 */
1611 nf_ct_expect_put(exp
);
1612 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1613 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1614 struct nf_conn_help
*m_help
;
1616 /* delete all expectations for this helper */
1617 spin_lock_bh(&nf_conntrack_lock
);
1618 h
= __nf_conntrack_helper_find_byname(name
);
1620 spin_unlock_bh(&nf_conntrack_lock
);
1623 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1624 hlist_for_each_entry_safe(exp
, n
, next
,
1625 &nf_ct_expect_hash
[i
],
1627 m_help
= nfct_help(exp
->master
);
1628 if (m_help
->helper
== h
1629 && del_timer(&exp
->timeout
)) {
1630 nf_ct_unlink_expect(exp
);
1631 nf_ct_expect_put(exp
);
1635 spin_unlock_bh(&nf_conntrack_lock
);
1637 /* This basically means we have to flush everything*/
1638 spin_lock_bh(&nf_conntrack_lock
);
1639 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1640 hlist_for_each_entry_safe(exp
, n
, next
,
1641 &nf_ct_expect_hash
[i
],
1643 if (del_timer(&exp
->timeout
)) {
1644 nf_ct_unlink_expect(exp
);
1645 nf_ct_expect_put(exp
);
1649 spin_unlock_bh(&nf_conntrack_lock
);
1655 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1661 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
)
1663 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1664 struct nf_conntrack_tuple_hash
*h
= NULL
;
1665 struct nf_conntrack_expect
*exp
;
1667 struct nf_conn_help
*help
;
1670 /* caller guarantees that those three CTA_EXPECT_* exist */
1671 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1674 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1677 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1681 /* Look for master conntrack of this expectation */
1682 h
= nf_conntrack_find_get(&master_tuple
);
1685 ct
= nf_ct_tuplehash_to_ctrack(h
);
1686 help
= nfct_help(ct
);
1688 if (!help
|| !help
->helper
) {
1689 /* such conntrack hasn't got any helper, abort */
1694 exp
= nf_ct_expect_alloc(ct
);
1700 exp
->expectfn
= NULL
;
1704 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1705 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1706 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1708 err
= nf_ct_expect_related(exp
);
1709 nf_ct_expect_put(exp
);
1712 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1717 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1718 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1720 struct nf_conntrack_tuple tuple
;
1721 struct nf_conntrack_expect
*exp
;
1722 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1723 u_int8_t u3
= nfmsg
->nfgen_family
;
1726 if (!cda
[CTA_EXPECT_TUPLE
]
1727 || !cda
[CTA_EXPECT_MASK
]
1728 || !cda
[CTA_EXPECT_MASTER
])
1731 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1735 spin_lock_bh(&nf_conntrack_lock
);
1736 exp
= __nf_ct_expect_find(&tuple
);
1739 spin_unlock_bh(&nf_conntrack_lock
);
1741 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1742 err
= ctnetlink_create_expect(cda
, u3
);
1747 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1748 err
= ctnetlink_change_expect(exp
, cda
);
1749 spin_unlock_bh(&nf_conntrack_lock
);
1754 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1755 static struct notifier_block ctnl_notifier
= {
1756 .notifier_call
= ctnetlink_conntrack_event
,
1759 static struct notifier_block ctnl_notifier_exp
= {
1760 .notifier_call
= ctnetlink_expect_event
,
1764 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1765 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1766 .attr_count
= CTA_MAX
,
1767 .policy
= ct_nla_policy
},
1768 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1769 .attr_count
= CTA_MAX
,
1770 .policy
= ct_nla_policy
},
1771 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1772 .attr_count
= CTA_MAX
,
1773 .policy
= ct_nla_policy
},
1774 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1775 .attr_count
= CTA_MAX
,
1776 .policy
= ct_nla_policy
},
1779 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1780 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1781 .attr_count
= CTA_EXPECT_MAX
,
1782 .policy
= exp_nla_policy
},
1783 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1784 .attr_count
= CTA_EXPECT_MAX
,
1785 .policy
= exp_nla_policy
},
1786 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1787 .attr_count
= CTA_EXPECT_MAX
,
1788 .policy
= exp_nla_policy
},
1791 static const struct nfnetlink_subsystem ctnl_subsys
= {
1792 .name
= "conntrack",
1793 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1794 .cb_count
= IPCTNL_MSG_MAX
,
1798 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1799 .name
= "conntrack_expect",
1800 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1801 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1805 MODULE_ALIAS("ip_conntrack_netlink");
1806 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1807 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1809 static int __init
ctnetlink_init(void)
1813 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1814 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1816 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1820 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1822 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1823 goto err_unreg_subsys
;
1826 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1827 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1829 printk("ctnetlink_init: cannot register notifier.\n");
1830 goto err_unreg_exp_subsys
;
1833 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1835 printk("ctnetlink_init: cannot expect register notifier.\n");
1836 goto err_unreg_notifier
;
1842 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1844 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1845 err_unreg_exp_subsys
:
1846 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1849 nfnetlink_subsys_unregister(&ctnl_subsys
);
1854 static void __exit
ctnetlink_exit(void)
1856 printk("ctnetlink: unregistering from nfnetlink.\n");
1858 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1859 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1860 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1863 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1864 nfnetlink_subsys_unregister(&ctnl_subsys
);
1868 module_init(ctnetlink_init
);
1869 module_exit(ctnetlink_exit
);