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/slab.h>
32 #include <linux/netfilter.h>
33 #include <net/netlink.h>
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_core.h>
37 #include <net/netfilter/nf_conntrack_expect.h>
38 #include <net/netfilter/nf_conntrack_helper.h>
39 #include <net/netfilter/nf_conntrack_l3proto.h>
40 #include <net/netfilter/nf_conntrack_l4proto.h>
41 #include <net/netfilter/nf_conntrack_tuple.h>
42 #include <net/netfilter/nf_conntrack_acct.h>
43 #include <net/netfilter/nf_conntrack_zones.h>
44 #ifdef CONFIG_NF_NAT_NEEDED
45 #include <net/netfilter/nf_nat_core.h>
46 #include <net/netfilter/nf_nat_protocol.h>
49 #include <linux/netfilter/nfnetlink.h>
50 #include <linux/netfilter/nfnetlink_conntrack.h>
52 MODULE_LICENSE("GPL");
54 static char __initdata version
[] = "0.93";
57 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
58 const struct nf_conntrack_tuple
*tuple
,
59 struct nf_conntrack_l4proto
*l4proto
)
62 struct nlattr
*nest_parms
;
64 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
67 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
69 if (likely(l4proto
->tuple_to_nlattr
))
70 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
72 nla_nest_end(skb
, nest_parms
);
81 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
82 const struct nf_conntrack_tuple
*tuple
,
83 struct nf_conntrack_l3proto
*l3proto
)
86 struct nlattr
*nest_parms
;
88 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
92 if (likely(l3proto
->tuple_to_nlattr
))
93 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
95 nla_nest_end(skb
, nest_parms
);
104 ctnetlink_dump_tuples(struct sk_buff
*skb
,
105 const struct nf_conntrack_tuple
*tuple
)
108 struct nf_conntrack_l3proto
*l3proto
;
109 struct nf_conntrack_l4proto
*l4proto
;
111 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
112 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
114 if (unlikely(ret
< 0))
117 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
118 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
124 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
126 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
134 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
136 long timeout
= (ct
->timeout
.expires
- jiffies
) / HZ
;
141 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
149 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
151 struct nf_conntrack_l4proto
*l4proto
;
152 struct nlattr
*nest_proto
;
155 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
156 if (!l4proto
->to_nlattr
)
159 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
161 goto nla_put_failure
;
163 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
165 nla_nest_end(skb
, nest_proto
);
174 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
176 struct nlattr
*nest_helper
;
177 const struct nf_conn_help
*help
= nfct_help(ct
);
178 struct nf_conntrack_helper
*helper
;
183 helper
= rcu_dereference(help
->helper
);
187 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
189 goto nla_put_failure
;
190 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
192 if (helper
->to_nlattr
)
193 helper
->to_nlattr(skb
, ct
);
195 nla_nest_end(skb
, nest_helper
);
204 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
205 enum ip_conntrack_dir dir
)
207 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
208 struct nlattr
*nest_count
;
209 const struct nf_conn_counter
*acct
;
211 acct
= nf_conn_acct_find(ct
);
215 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
217 goto nla_put_failure
;
219 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
,
220 cpu_to_be64(acct
[dir
].packets
));
221 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
,
222 cpu_to_be64(acct
[dir
].bytes
));
224 nla_nest_end(skb
, nest_count
);
232 #ifdef CONFIG_NF_CONNTRACK_MARK
234 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
236 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
243 #define ctnetlink_dump_mark(a, b) (0)
246 #ifdef CONFIG_NF_CONNTRACK_SECMARK
248 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
250 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
257 #define ctnetlink_dump_secmark(a, b) (0)
260 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
263 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
265 struct nlattr
*nest_parms
;
267 if (!(ct
->status
& IPS_EXPECTED
))
270 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
272 goto nla_put_failure
;
273 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
274 goto nla_put_failure
;
275 nla_nest_end(skb
, nest_parms
);
283 #ifdef CONFIG_NF_NAT_NEEDED
285 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
287 struct nlattr
*nest_parms
;
289 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
291 goto nla_put_failure
;
293 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
294 htonl(natseq
->correction_pos
));
295 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
296 htonl(natseq
->offset_before
));
297 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
298 htonl(natseq
->offset_after
));
300 nla_nest_end(skb
, nest_parms
);
309 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
311 struct nf_nat_seq
*natseq
;
312 struct nf_conn_nat
*nat
= nfct_nat(ct
);
314 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
317 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
318 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
321 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
322 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
328 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
332 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
334 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
342 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
344 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
352 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
353 int event
, struct nf_conn
*ct
)
355 struct nlmsghdr
*nlh
;
356 struct nfgenmsg
*nfmsg
;
357 struct nlattr
*nest_parms
;
358 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
360 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
361 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
365 nfmsg
= nlmsg_data(nlh
);
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
, nf_ct_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
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
381 goto nla_put_failure
;
382 nla_nest_end(skb
, nest_parms
);
385 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
387 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
388 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
389 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
390 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
391 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
392 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
393 ctnetlink_dump_mark(skb
, ct
) < 0 ||
394 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
395 ctnetlink_dump_id(skb
, ct
) < 0 ||
396 ctnetlink_dump_use(skb
, ct
) < 0 ||
397 ctnetlink_dump_master(skb
, ct
) < 0 ||
398 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
399 goto nla_put_failure
;
406 nlmsg_cancel(skb
, nlh
);
410 #ifdef CONFIG_NF_CONNTRACK_EVENTS
412 ctnetlink_proto_size(const struct nf_conn
*ct
)
414 struct nf_conntrack_l3proto
*l3proto
;
415 struct nf_conntrack_l4proto
*l4proto
;
419 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
420 len
+= l3proto
->nla_size
;
422 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
423 len
+= l4proto
->nla_size
;
430 ctnetlink_counters_size(const struct nf_conn
*ct
)
432 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
434 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
435 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
436 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
441 ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
443 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
444 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
445 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
446 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
447 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
448 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
449 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
450 + ctnetlink_counters_size(ct
)
451 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
452 + nla_total_size(0) /* CTA_PROTOINFO */
453 + nla_total_size(0) /* CTA_HELP */
454 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
455 #ifdef CONFIG_NF_CONNTRACK_SECMARK
456 + nla_total_size(sizeof(u_int32_t
)) /* CTA_SECMARK */
458 #ifdef CONFIG_NF_NAT_NEEDED
459 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
460 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
462 #ifdef CONFIG_NF_CONNTRACK_MARK
463 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
465 + ctnetlink_proto_size(ct
)
470 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
473 struct nlmsghdr
*nlh
;
474 struct nfgenmsg
*nfmsg
;
475 struct nlattr
*nest_parms
;
476 struct nf_conn
*ct
= item
->ct
;
479 unsigned int flags
= 0, group
;
482 /* ignore our fake conntrack entry */
483 if (ct
== &nf_conntrack_untracked
)
486 if (events
& (1 << IPCT_DESTROY
)) {
487 type
= IPCTNL_MSG_CT_DELETE
;
488 group
= NFNLGRP_CONNTRACK_DESTROY
;
489 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
490 type
= IPCTNL_MSG_CT_NEW
;
491 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
492 group
= NFNLGRP_CONNTRACK_NEW
;
494 type
= IPCTNL_MSG_CT_NEW
;
495 group
= NFNLGRP_CONNTRACK_UPDATE
;
500 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
503 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
507 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
508 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
512 nfmsg
= nlmsg_data(nlh
);
513 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
514 nfmsg
->version
= NFNETLINK_V0
;
518 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
520 goto nla_put_failure
;
521 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
522 goto nla_put_failure
;
523 nla_nest_end(skb
, nest_parms
);
525 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
527 goto nla_put_failure
;
528 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
529 goto nla_put_failure
;
530 nla_nest_end(skb
, nest_parms
);
533 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
535 if (ctnetlink_dump_id(skb
, ct
) < 0)
536 goto nla_put_failure
;
538 if (ctnetlink_dump_status(skb
, ct
) < 0)
539 goto nla_put_failure
;
541 if (events
& (1 << IPCT_DESTROY
)) {
542 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
543 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
544 goto nla_put_failure
;
546 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
547 goto nla_put_failure
;
549 if (events
& (1 << IPCT_PROTOINFO
)
550 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
551 goto nla_put_failure
;
553 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
554 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
555 goto nla_put_failure
;
557 #ifdef CONFIG_NF_CONNTRACK_SECMARK
558 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
559 && ctnetlink_dump_secmark(skb
, ct
) < 0)
560 goto nla_put_failure
;
563 if (events
& (1 << IPCT_RELATED
) &&
564 ctnetlink_dump_master(skb
, ct
) < 0)
565 goto nla_put_failure
;
567 if (events
& (1 << IPCT_NATSEQADJ
) &&
568 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
569 goto nla_put_failure
;
572 #ifdef CONFIG_NF_CONNTRACK_MARK
573 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
574 && ctnetlink_dump_mark(skb
, ct
) < 0)
575 goto nla_put_failure
;
580 err
= nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
,
582 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
589 nlmsg_cancel(skb
, nlh
);
593 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
598 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
600 static int ctnetlink_done(struct netlink_callback
*cb
)
603 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
608 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
610 struct net
*net
= sock_net(skb
->sk
);
611 struct nf_conn
*ct
, *last
;
612 struct nf_conntrack_tuple_hash
*h
;
613 struct hlist_nulls_node
*n
;
614 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
615 u_int8_t l3proto
= nfmsg
->nfgen_family
;
618 last
= (struct nf_conn
*)cb
->args
[1];
619 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
621 hlist_nulls_for_each_entry_rcu(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
623 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
625 ct
= nf_ct_tuplehash_to_ctrack(h
);
626 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
628 /* Dump entries of a given L3 protocol number.
629 * If it is not specified, ie. l3proto == 0,
630 * then dump everything. */
631 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
638 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
640 IPCTNL_MSG_CT_NEW
, ct
) < 0) {
641 cb
->args
[1] = (unsigned long)ct
;
645 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
646 IPCTNL_MSG_CT_GET_CTRZERO
) {
647 struct nf_conn_counter
*acct
;
649 acct
= nf_conn_acct_find(ct
);
651 memset(acct
, 0, sizeof(struct nf_conn_counter
[IP_CT_DIR_MAX
]));
670 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
672 struct nlattr
*tb
[CTA_IP_MAX
+1];
673 struct nf_conntrack_l3proto
*l3proto
;
676 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
679 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
681 if (likely(l3proto
->nlattr_to_tuple
)) {
682 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
683 l3proto
->nla_policy
);
685 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
693 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
694 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
698 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
699 struct nf_conntrack_tuple
*tuple
)
701 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
702 struct nf_conntrack_l4proto
*l4proto
;
705 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
709 if (!tb
[CTA_PROTO_NUM
])
711 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
714 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
716 if (likely(l4proto
->nlattr_to_tuple
)) {
717 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
718 l4proto
->nla_policy
);
720 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
728 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
729 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
730 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
734 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
735 struct nf_conntrack_tuple
*tuple
,
736 enum ctattr_tuple type
, u_int8_t l3num
)
738 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
741 memset(tuple
, 0, sizeof(*tuple
));
743 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
745 if (!tb
[CTA_TUPLE_IP
])
748 tuple
->src
.l3num
= l3num
;
750 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
754 if (!tb
[CTA_TUPLE_PROTO
])
757 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
761 /* orig and expect tuples get DIR_ORIGINAL */
762 if (type
== CTA_TUPLE_REPLY
)
763 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
765 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
771 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
774 #ifdef CONFIG_NF_CONNTRACK_ZONES
775 *zone
= ntohs(nla_get_be16(attr
));
785 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
786 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
790 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
792 struct nlattr
*tb
[CTA_HELP_MAX
+1];
794 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
796 if (!tb
[CTA_HELP_NAME
])
799 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
804 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
805 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
806 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
807 [CTA_STATUS
] = { .type
= NLA_U32
},
808 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
809 [CTA_HELP
] = { .type
= NLA_NESTED
},
810 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
811 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
812 [CTA_MARK
] = { .type
= NLA_U32
},
813 [CTA_ID
] = { .type
= NLA_U32
},
814 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
815 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
816 [CTA_ZONE
] = { .type
= NLA_U16
},
820 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
821 const struct nlmsghdr
*nlh
,
822 const struct nlattr
* const cda
[])
824 struct net
*net
= sock_net(ctnl
);
825 struct nf_conntrack_tuple_hash
*h
;
826 struct nf_conntrack_tuple tuple
;
828 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
829 u_int8_t u3
= nfmsg
->nfgen_family
;
833 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
837 if (cda
[CTA_TUPLE_ORIG
])
838 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
839 else if (cda
[CTA_TUPLE_REPLY
])
840 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
842 /* Flush the whole table */
843 nf_conntrack_flush_report(net
,
852 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
856 ct
= nf_ct_tuplehash_to_ctrack(h
);
859 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
860 if (id
!= (u32
)(unsigned long)ct
) {
866 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
868 nlmsg_report(nlh
)) < 0) {
869 nf_ct_delete_from_lists(ct
);
870 /* we failed to report the event, try later */
871 nf_ct_insert_dying_list(ct
);
876 /* death_by_timeout would report the event again */
877 set_bit(IPS_DYING_BIT
, &ct
->status
);
886 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
887 const struct nlmsghdr
*nlh
,
888 const struct nlattr
* const cda
[])
890 struct net
*net
= sock_net(ctnl
);
891 struct nf_conntrack_tuple_hash
*h
;
892 struct nf_conntrack_tuple tuple
;
894 struct sk_buff
*skb2
= NULL
;
895 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
896 u_int8_t u3
= nfmsg
->nfgen_family
;
900 if (nlh
->nlmsg_flags
& NLM_F_DUMP
)
901 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
904 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
908 if (cda
[CTA_TUPLE_ORIG
])
909 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
910 else if (cda
[CTA_TUPLE_REPLY
])
911 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
918 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
922 ct
= nf_ct_tuplehash_to_ctrack(h
);
925 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
932 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
933 IPCTNL_MSG_CT_NEW
, ct
);
939 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
951 #ifdef CONFIG_NF_NAT_NEEDED
953 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
954 enum nf_nat_manip_type manip
,
955 const struct nlattr
*attr
)
957 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
959 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
960 if (!parse_nat_setup
) {
961 #ifdef CONFIG_MODULES
963 spin_unlock_bh(&nf_conntrack_lock
);
965 if (request_module("nf-nat-ipv4") < 0) {
967 spin_lock_bh(&nf_conntrack_lock
);
972 spin_lock_bh(&nf_conntrack_lock
);
974 if (nfnetlink_parse_nat_setup_hook
)
980 return parse_nat_setup(ct
, manip
, attr
);
985 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
988 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
989 d
= ct
->status
^ status
;
991 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
995 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
996 /* SEEN_REPLY bit can only be set */
999 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1000 /* ASSURED bit can only be set */
1003 /* Be careful here, modifying NAT bits can screw up things,
1004 * so don't let users modify them directly if they don't pass
1006 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1011 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1013 #ifdef CONFIG_NF_NAT_NEEDED
1016 if (cda
[CTA_NAT_DST
]) {
1017 ret
= ctnetlink_parse_nat_setup(ct
,
1023 if (cda
[CTA_NAT_SRC
]) {
1024 ret
= ctnetlink_parse_nat_setup(ct
,
1037 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1039 struct nf_conntrack_helper
*helper
;
1040 struct nf_conn_help
*help
= nfct_help(ct
);
1041 char *helpname
= NULL
;
1044 /* don't change helper of sibling connections */
1048 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1052 if (!strcmp(helpname
, "")) {
1053 if (help
&& help
->helper
) {
1054 /* we had a helper before ... */
1055 nf_ct_remove_expectations(ct
);
1056 rcu_assign_pointer(help
->helper
, NULL
);
1062 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1063 nf_ct_protonum(ct
));
1064 if (helper
== NULL
) {
1065 #ifdef CONFIG_MODULES
1066 spin_unlock_bh(&nf_conntrack_lock
);
1068 if (request_module("nfct-helper-%s", helpname
) < 0) {
1069 spin_lock_bh(&nf_conntrack_lock
);
1073 spin_lock_bh(&nf_conntrack_lock
);
1074 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1075 nf_ct_protonum(ct
));
1083 if (help
->helper
== helper
)
1087 /* need to zero data of old helper */
1088 memset(&help
->help
, 0, sizeof(help
->help
));
1090 /* we cannot set a helper for an existing conntrack */
1094 rcu_assign_pointer(help
->helper
, helper
);
1100 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1102 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1104 if (!del_timer(&ct
->timeout
))
1107 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1108 add_timer(&ct
->timeout
);
1113 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1114 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1115 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1116 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1120 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1122 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1123 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1124 struct nf_conntrack_l4proto
*l4proto
;
1127 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1130 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1131 if (l4proto
->from_nlattr
)
1132 err
= l4proto
->from_nlattr(tb
, ct
);
1138 #ifdef CONFIG_NF_NAT_NEEDED
1139 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1140 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1141 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1142 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1146 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1148 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1150 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1152 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1155 natseq
->correction_pos
=
1156 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1158 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1161 natseq
->offset_before
=
1162 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1164 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1167 natseq
->offset_after
=
1168 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1174 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1175 const struct nlattr
* const cda
[])
1178 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1183 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1184 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1185 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1189 ct
->status
|= IPS_SEQ_ADJUST
;
1192 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1193 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1194 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1198 ct
->status
|= IPS_SEQ_ADJUST
;
1206 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1207 const struct nlattr
* const cda
[])
1211 /* only allow NAT changes and master assignation for new conntracks */
1212 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1215 if (cda
[CTA_HELP
]) {
1216 err
= ctnetlink_change_helper(ct
, cda
);
1221 if (cda
[CTA_TIMEOUT
]) {
1222 err
= ctnetlink_change_timeout(ct
, cda
);
1227 if (cda
[CTA_STATUS
]) {
1228 err
= ctnetlink_change_status(ct
, cda
);
1233 if (cda
[CTA_PROTOINFO
]) {
1234 err
= ctnetlink_change_protoinfo(ct
, cda
);
1239 #if defined(CONFIG_NF_CONNTRACK_MARK)
1241 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1244 #ifdef CONFIG_NF_NAT_NEEDED
1245 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1246 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1255 static struct nf_conn
*
1256 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1257 const struct nlattr
* const cda
[],
1258 struct nf_conntrack_tuple
*otuple
,
1259 struct nf_conntrack_tuple
*rtuple
,
1264 struct nf_conntrack_helper
*helper
;
1266 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1268 return ERR_PTR(-ENOMEM
);
1270 if (!cda
[CTA_TIMEOUT
])
1272 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1274 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1277 if (cda
[CTA_HELP
]) {
1278 char *helpname
= NULL
;
1280 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1284 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1285 nf_ct_protonum(ct
));
1286 if (helper
== NULL
) {
1288 #ifdef CONFIG_MODULES
1289 if (request_module("nfct-helper-%s", helpname
) < 0) {
1295 helper
= __nf_conntrack_helper_find(helpname
,
1297 nf_ct_protonum(ct
));
1307 struct nf_conn_help
*help
;
1309 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1315 /* not in hash table yet so not strictly necessary */
1316 rcu_assign_pointer(help
->helper
, helper
);
1319 /* try an implicit helper assignation */
1320 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1325 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1326 err
= ctnetlink_change_nat(ct
, cda
);
1331 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1332 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1333 /* we must add conntrack extensions before confirmation. */
1334 ct
->status
|= IPS_CONFIRMED
;
1336 if (cda
[CTA_STATUS
]) {
1337 err
= ctnetlink_change_status(ct
, cda
);
1342 #ifdef CONFIG_NF_NAT_NEEDED
1343 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1344 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1350 if (cda
[CTA_PROTOINFO
]) {
1351 err
= ctnetlink_change_protoinfo(ct
, cda
);
1356 #if defined(CONFIG_NF_CONNTRACK_MARK)
1358 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1361 /* setup master conntrack: this is a confirmed expectation */
1362 if (cda
[CTA_TUPLE_MASTER
]) {
1363 struct nf_conntrack_tuple master
;
1364 struct nf_conntrack_tuple_hash
*master_h
;
1365 struct nf_conn
*master_ct
;
1367 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1371 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1372 if (master_h
== NULL
) {
1376 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1377 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1378 ct
->master
= master_ct
;
1381 add_timer(&ct
->timeout
);
1382 nf_conntrack_hash_insert(ct
);
1390 nf_conntrack_free(ct
);
1391 return ERR_PTR(err
);
1395 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1396 const struct nlmsghdr
*nlh
,
1397 const struct nlattr
* const cda
[])
1399 struct net
*net
= sock_net(ctnl
);
1400 struct nf_conntrack_tuple otuple
, rtuple
;
1401 struct nf_conntrack_tuple_hash
*h
= NULL
;
1402 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1403 u_int8_t u3
= nfmsg
->nfgen_family
;
1407 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1411 if (cda
[CTA_TUPLE_ORIG
]) {
1412 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1417 if (cda
[CTA_TUPLE_REPLY
]) {
1418 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1423 spin_lock_bh(&nf_conntrack_lock
);
1424 if (cda
[CTA_TUPLE_ORIG
])
1425 h
= __nf_conntrack_find(net
, zone
, &otuple
);
1426 else if (cda
[CTA_TUPLE_REPLY
])
1427 h
= __nf_conntrack_find(net
, zone
, &rtuple
);
1431 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1433 enum ip_conntrack_events events
;
1435 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1442 nf_conntrack_get(&ct
->ct_general
);
1443 spin_unlock_bh(&nf_conntrack_lock
);
1444 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1445 events
= IPCT_RELATED
;
1449 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1450 (1 << IPCT_ASSURED
) |
1451 (1 << IPCT_HELPER
) |
1452 (1 << IPCT_PROTOINFO
) |
1453 (1 << IPCT_NATSEQADJ
) |
1454 (1 << IPCT_MARK
) | events
,
1455 ct
, NETLINK_CB(skb
).pid
,
1459 spin_unlock_bh(&nf_conntrack_lock
);
1463 /* implicit 'else' */
1465 /* We manipulate the conntrack inside the global conntrack table lock,
1466 * so there's no need to increase the refcount */
1468 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1469 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
1471 err
= ctnetlink_change_conntrack(ct
, cda
);
1473 nf_conntrack_get(&ct
->ct_general
);
1474 spin_unlock_bh(&nf_conntrack_lock
);
1475 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1476 (1 << IPCT_ASSURED
) |
1477 (1 << IPCT_HELPER
) |
1478 (1 << IPCT_PROTOINFO
) |
1479 (1 << IPCT_NATSEQADJ
) |
1481 ct
, NETLINK_CB(skb
).pid
,
1485 spin_unlock_bh(&nf_conntrack_lock
);
1491 spin_unlock_bh(&nf_conntrack_lock
);
1495 /***********************************************************************
1497 ***********************************************************************/
1500 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1501 const struct nf_conntrack_tuple
*tuple
,
1502 enum ctattr_expect type
)
1504 struct nlattr
*nest_parms
;
1506 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1508 goto nla_put_failure
;
1509 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1510 goto nla_put_failure
;
1511 nla_nest_end(skb
, nest_parms
);
1520 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1521 const struct nf_conntrack_tuple
*tuple
,
1522 const struct nf_conntrack_tuple_mask
*mask
)
1525 struct nf_conntrack_l3proto
*l3proto
;
1526 struct nf_conntrack_l4proto
*l4proto
;
1527 struct nf_conntrack_tuple m
;
1528 struct nlattr
*nest_parms
;
1530 memset(&m
, 0xFF, sizeof(m
));
1531 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1532 m
.src
.u
.all
= mask
->src
.u
.all
;
1533 m
.dst
.protonum
= tuple
->dst
.protonum
;
1535 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1537 goto nla_put_failure
;
1539 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1540 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1542 if (unlikely(ret
< 0))
1543 goto nla_put_failure
;
1545 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1546 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1547 if (unlikely(ret
< 0))
1548 goto nla_put_failure
;
1550 nla_nest_end(skb
, nest_parms
);
1559 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1560 const struct nf_conntrack_expect
*exp
)
1562 struct nf_conn
*master
= exp
->master
;
1563 struct nf_conntrack_helper
*helper
;
1564 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1569 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1570 goto nla_put_failure
;
1571 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1572 goto nla_put_failure
;
1573 if (ctnetlink_exp_dump_tuple(skb
,
1574 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1575 CTA_EXPECT_MASTER
) < 0)
1576 goto nla_put_failure
;
1578 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1579 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1580 helper
= rcu_dereference(nfct_help(master
)->helper
);
1582 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1591 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1592 int event
, const struct nf_conntrack_expect
*exp
)
1594 struct nlmsghdr
*nlh
;
1595 struct nfgenmsg
*nfmsg
;
1596 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1598 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1599 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1603 nfmsg
= nlmsg_data(nlh
);
1604 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1605 nfmsg
->version
= NFNETLINK_V0
;
1608 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1609 goto nla_put_failure
;
1611 nlmsg_end(skb
, nlh
);
1616 nlmsg_cancel(skb
, nlh
);
1620 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1622 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1624 struct nf_conntrack_expect
*exp
= item
->exp
;
1625 struct net
*net
= nf_ct_exp_net(exp
);
1626 struct nlmsghdr
*nlh
;
1627 struct nfgenmsg
*nfmsg
;
1628 struct sk_buff
*skb
;
1632 if (events
& (1 << IPEXP_NEW
)) {
1633 type
= IPCTNL_MSG_EXP_NEW
;
1634 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1638 if (!item
->report
&&
1639 !nfnetlink_has_listeners(net
, NFNLGRP_CONNTRACK_EXP_NEW
))
1642 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1646 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1647 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1651 nfmsg
= nlmsg_data(nlh
);
1652 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1653 nfmsg
->version
= NFNETLINK_V0
;
1657 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1658 goto nla_put_failure
;
1661 nlmsg_end(skb
, nlh
);
1662 nfnetlink_send(skb
, net
, item
->pid
, NFNLGRP_CONNTRACK_EXP_NEW
,
1663 item
->report
, GFP_ATOMIC
);
1668 nlmsg_cancel(skb
, nlh
);
1672 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1676 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1679 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1684 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1686 struct net
*net
= sock_net(skb
->sk
);
1687 struct nf_conntrack_expect
*exp
, *last
;
1688 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1689 struct hlist_node
*n
;
1690 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1693 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1694 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1696 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1698 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1705 if (ctnetlink_exp_fill_info(skb
,
1706 NETLINK_CB(cb
->skb
).pid
,
1710 if (!atomic_inc_not_zero(&exp
->use
))
1712 cb
->args
[1] = (unsigned long)exp
;
1724 nf_ct_expect_put(last
);
1729 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1730 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1731 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1732 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1733 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1734 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1735 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1739 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1740 const struct nlmsghdr
*nlh
,
1741 const struct nlattr
* const cda
[])
1743 struct net
*net
= sock_net(ctnl
);
1744 struct nf_conntrack_tuple tuple
;
1745 struct nf_conntrack_expect
*exp
;
1746 struct sk_buff
*skb2
;
1747 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1748 u_int8_t u3
= nfmsg
->nfgen_family
;
1752 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1753 return netlink_dump_start(ctnl
, skb
, nlh
,
1754 ctnetlink_exp_dump_table
,
1755 ctnetlink_exp_done
);
1758 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1762 if (cda
[CTA_EXPECT_MASTER
])
1763 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1770 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1774 if (cda
[CTA_EXPECT_ID
]) {
1775 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1776 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1777 nf_ct_expect_put(exp
);
1783 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1788 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1789 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1794 nf_ct_expect_put(exp
);
1796 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1801 nf_ct_expect_put(exp
);
1806 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1807 const struct nlmsghdr
*nlh
,
1808 const struct nlattr
* const cda
[])
1810 struct net
*net
= sock_net(ctnl
);
1811 struct nf_conntrack_expect
*exp
;
1812 struct nf_conntrack_tuple tuple
;
1813 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1814 struct hlist_node
*n
, *next
;
1815 u_int8_t u3
= nfmsg
->nfgen_family
;
1820 if (cda
[CTA_EXPECT_TUPLE
]) {
1821 /* delete a single expect by tuple */
1822 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1826 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1830 /* bump usage count to 2 */
1831 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1835 if (cda
[CTA_EXPECT_ID
]) {
1836 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1837 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1838 nf_ct_expect_put(exp
);
1843 /* after list removal, usage count == 1 */
1844 nf_ct_unexpect_related(exp
);
1845 /* have to put what we 'get' above.
1846 * after this line usage count == 0 */
1847 nf_ct_expect_put(exp
);
1848 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1849 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1850 struct nf_conn_help
*m_help
;
1852 /* delete all expectations for this helper */
1853 spin_lock_bh(&nf_conntrack_lock
);
1854 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1855 hlist_for_each_entry_safe(exp
, n
, next
,
1856 &net
->ct
.expect_hash
[i
],
1858 m_help
= nfct_help(exp
->master
);
1859 if (!strcmp(m_help
->helper
->name
, name
) &&
1860 del_timer(&exp
->timeout
)) {
1861 nf_ct_unlink_expect(exp
);
1862 nf_ct_expect_put(exp
);
1866 spin_unlock_bh(&nf_conntrack_lock
);
1868 /* This basically means we have to flush everything*/
1869 spin_lock_bh(&nf_conntrack_lock
);
1870 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1871 hlist_for_each_entry_safe(exp
, n
, next
,
1872 &net
->ct
.expect_hash
[i
],
1874 if (del_timer(&exp
->timeout
)) {
1875 nf_ct_unlink_expect(exp
);
1876 nf_ct_expect_put(exp
);
1880 spin_unlock_bh(&nf_conntrack_lock
);
1886 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
1887 const struct nlattr
* const cda
[])
1893 ctnetlink_create_expect(struct net
*net
, u16 zone
,
1894 const struct nlattr
* const cda
[],
1896 u32 pid
, int report
)
1898 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1899 struct nf_conntrack_tuple_hash
*h
= NULL
;
1900 struct nf_conntrack_expect
*exp
;
1902 struct nf_conn_help
*help
;
1905 /* caller guarantees that those three CTA_EXPECT_* exist */
1906 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1909 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1912 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1916 /* Look for master conntrack of this expectation */
1917 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
1920 ct
= nf_ct_tuplehash_to_ctrack(h
);
1921 help
= nfct_help(ct
);
1923 if (!help
|| !help
->helper
) {
1924 /* such conntrack hasn't got any helper, abort */
1929 exp
= nf_ct_expect_alloc(ct
);
1936 exp
->expectfn
= NULL
;
1940 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1941 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1942 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1944 err
= nf_ct_expect_related_report(exp
, pid
, report
);
1945 nf_ct_expect_put(exp
);
1948 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1953 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1954 const struct nlmsghdr
*nlh
,
1955 const struct nlattr
* const cda
[])
1957 struct net
*net
= sock_net(ctnl
);
1958 struct nf_conntrack_tuple tuple
;
1959 struct nf_conntrack_expect
*exp
;
1960 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1961 u_int8_t u3
= nfmsg
->nfgen_family
;
1965 if (!cda
[CTA_EXPECT_TUPLE
]
1966 || !cda
[CTA_EXPECT_MASK
]
1967 || !cda
[CTA_EXPECT_MASTER
])
1970 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1974 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1978 spin_lock_bh(&nf_conntrack_lock
);
1979 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
1982 spin_unlock_bh(&nf_conntrack_lock
);
1984 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1985 err
= ctnetlink_create_expect(net
, zone
, cda
,
1987 NETLINK_CB(skb
).pid
,
1994 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1995 err
= ctnetlink_change_expect(exp
, cda
);
1996 spin_unlock_bh(&nf_conntrack_lock
);
2001 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2002 static struct nf_ct_event_notifier ctnl_notifier
= {
2003 .fcn
= ctnetlink_conntrack_event
,
2006 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2007 .fcn
= ctnetlink_expect_event
,
2011 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2012 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2013 .attr_count
= CTA_MAX
,
2014 .policy
= ct_nla_policy
},
2015 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2016 .attr_count
= CTA_MAX
,
2017 .policy
= ct_nla_policy
},
2018 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2019 .attr_count
= CTA_MAX
,
2020 .policy
= ct_nla_policy
},
2021 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2022 .attr_count
= CTA_MAX
,
2023 .policy
= ct_nla_policy
},
2026 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2027 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2028 .attr_count
= CTA_EXPECT_MAX
,
2029 .policy
= exp_nla_policy
},
2030 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2031 .attr_count
= CTA_EXPECT_MAX
,
2032 .policy
= exp_nla_policy
},
2033 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2034 .attr_count
= CTA_EXPECT_MAX
,
2035 .policy
= exp_nla_policy
},
2038 static const struct nfnetlink_subsystem ctnl_subsys
= {
2039 .name
= "conntrack",
2040 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2041 .cb_count
= IPCTNL_MSG_MAX
,
2045 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2046 .name
= "conntrack_expect",
2047 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2048 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2052 MODULE_ALIAS("ip_conntrack_netlink");
2053 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2054 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2056 static int __init
ctnetlink_init(void)
2060 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2061 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2063 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2067 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2069 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2070 goto err_unreg_subsys
;
2073 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2074 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
2076 pr_err("ctnetlink_init: cannot register notifier.\n");
2077 goto err_unreg_exp_subsys
;
2080 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
2082 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2083 goto err_unreg_notifier
;
2089 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2091 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2092 err_unreg_exp_subsys
:
2093 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2096 nfnetlink_subsys_unregister(&ctnl_subsys
);
2101 static void __exit
ctnetlink_exit(void)
2103 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2105 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2106 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
2107 nf_conntrack_unregister_notifier(&ctnl_notifier
);
2110 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2111 nfnetlink_subsys_unregister(&ctnl_subsys
);
2114 module_init(ctnetlink_init
);
2115 module_exit(ctnetlink_exit
);