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-2011 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/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #include <net/netfilter/nf_conntrack_timestamp.h>
46 #ifdef CONFIG_NF_NAT_NEEDED
47 #include <net/netfilter/nf_nat_core.h>
48 #include <net/netfilter/nf_nat_protocol.h>
51 #include <linux/netfilter/nfnetlink.h>
52 #include <linux/netfilter/nfnetlink_conntrack.h>
54 MODULE_LICENSE("GPL");
56 static char __initdata version
[] = "0.93";
59 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
60 const struct nf_conntrack_tuple
*tuple
,
61 struct nf_conntrack_l4proto
*l4proto
)
64 struct nlattr
*nest_parms
;
66 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
69 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
71 if (likely(l4proto
->tuple_to_nlattr
))
72 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
74 nla_nest_end(skb
, nest_parms
);
83 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
84 const struct nf_conntrack_tuple
*tuple
,
85 struct nf_conntrack_l3proto
*l3proto
)
88 struct nlattr
*nest_parms
;
90 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
94 if (likely(l3proto
->tuple_to_nlattr
))
95 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
97 nla_nest_end(skb
, nest_parms
);
106 ctnetlink_dump_tuples(struct sk_buff
*skb
,
107 const struct nf_conntrack_tuple
*tuple
)
110 struct nf_conntrack_l3proto
*l3proto
;
111 struct nf_conntrack_l4proto
*l4proto
;
114 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
115 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
118 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
119 tuple
->dst
.protonum
);
120 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
127 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
129 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
137 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
139 long timeout
= ((long)ct
->timeout
.expires
- (long)jiffies
) / HZ
;
144 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
152 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
154 struct nf_conntrack_l4proto
*l4proto
;
155 struct nlattr
*nest_proto
;
158 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
159 if (!l4proto
->to_nlattr
)
162 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
164 goto nla_put_failure
;
166 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
168 nla_nest_end(skb
, nest_proto
);
177 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
179 struct nlattr
*nest_helper
;
180 const struct nf_conn_help
*help
= nfct_help(ct
);
181 struct nf_conntrack_helper
*helper
;
186 helper
= rcu_dereference(help
->helper
);
190 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
192 goto nla_put_failure
;
193 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
195 if (helper
->to_nlattr
)
196 helper
->to_nlattr(skb
, ct
);
198 nla_nest_end(skb
, nest_helper
);
207 dump_counters(struct sk_buff
*skb
, u64 pkts
, u64 bytes
,
208 enum ip_conntrack_dir dir
)
210 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
211 struct nlattr
*nest_count
;
213 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
215 goto nla_put_failure
;
217 NLA_PUT_BE64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
));
218 NLA_PUT_BE64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
));
220 nla_nest_end(skb
, nest_count
);
229 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
230 enum ip_conntrack_dir dir
, int type
)
232 struct nf_conn_counter
*acct
;
235 acct
= nf_conn_acct_find(ct
);
239 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
240 pkts
= atomic64_xchg(&acct
[dir
].packets
, 0);
241 bytes
= atomic64_xchg(&acct
[dir
].bytes
, 0);
243 pkts
= atomic64_read(&acct
[dir
].packets
);
244 bytes
= atomic64_read(&acct
[dir
].bytes
);
246 return dump_counters(skb
, pkts
, bytes
, dir
);
250 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
252 struct nlattr
*nest_count
;
253 const struct nf_conn_tstamp
*tstamp
;
255 tstamp
= nf_conn_tstamp_find(ct
);
259 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
| NLA_F_NESTED
);
261 goto nla_put_failure
;
263 NLA_PUT_BE64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
));
264 if (tstamp
->stop
!= 0) {
265 NLA_PUT_BE64(skb
, CTA_TIMESTAMP_STOP
,
266 cpu_to_be64(tstamp
->stop
));
268 nla_nest_end(skb
, nest_count
);
276 #ifdef CONFIG_NF_CONNTRACK_MARK
278 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
280 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
287 #define ctnetlink_dump_mark(a, b) (0)
290 #ifdef CONFIG_NF_CONNTRACK_SECMARK
292 ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
294 struct nlattr
*nest_secctx
;
298 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
303 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
305 goto nla_put_failure
;
307 NLA_PUT_STRING(skb
, CTA_SECCTX_NAME
, secctx
);
308 nla_nest_end(skb
, nest_secctx
);
312 security_release_secctx(secctx
, len
);
316 #define ctnetlink_dump_secctx(a, b) (0)
319 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
322 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
324 struct nlattr
*nest_parms
;
326 if (!(ct
->status
& IPS_EXPECTED
))
329 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
331 goto nla_put_failure
;
332 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
333 goto nla_put_failure
;
334 nla_nest_end(skb
, nest_parms
);
342 #ifdef CONFIG_NF_NAT_NEEDED
344 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
346 struct nlattr
*nest_parms
;
348 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
350 goto nla_put_failure
;
352 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
353 htonl(natseq
->correction_pos
));
354 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
355 htonl(natseq
->offset_before
));
356 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
357 htonl(natseq
->offset_after
));
359 nla_nest_end(skb
, nest_parms
);
368 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
370 struct nf_nat_seq
*natseq
;
371 struct nf_conn_nat
*nat
= nfct_nat(ct
);
373 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
376 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
377 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
380 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
381 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
387 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
391 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
393 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
401 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
403 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
411 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 type
,
414 struct nlmsghdr
*nlh
;
415 struct nfgenmsg
*nfmsg
;
416 struct nlattr
*nest_parms
;
417 unsigned int flags
= pid
? NLM_F_MULTI
: 0, event
;
419 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_NEW
);
420 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
424 nfmsg
= nlmsg_data(nlh
);
425 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
426 nfmsg
->version
= NFNETLINK_V0
;
429 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
431 goto nla_put_failure
;
432 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
433 goto nla_put_failure
;
434 nla_nest_end(skb
, nest_parms
);
436 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
438 goto nla_put_failure
;
439 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
440 goto nla_put_failure
;
441 nla_nest_end(skb
, nest_parms
);
444 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
446 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
447 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
448 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
, type
) < 0 ||
449 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
, type
) < 0 ||
450 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
451 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
452 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
453 ctnetlink_dump_mark(skb
, ct
) < 0 ||
454 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
455 ctnetlink_dump_id(skb
, ct
) < 0 ||
456 ctnetlink_dump_use(skb
, ct
) < 0 ||
457 ctnetlink_dump_master(skb
, ct
) < 0 ||
458 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
459 goto nla_put_failure
;
466 nlmsg_cancel(skb
, nlh
);
470 #ifdef CONFIG_NF_CONNTRACK_EVENTS
472 ctnetlink_proto_size(const struct nf_conn
*ct
)
474 struct nf_conntrack_l3proto
*l3proto
;
475 struct nf_conntrack_l4proto
*l4proto
;
479 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
480 len
+= l3proto
->nla_size
;
482 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
483 len
+= l4proto
->nla_size
;
490 ctnetlink_counters_size(const struct nf_conn
*ct
)
492 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
494 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
495 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
496 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
501 ctnetlink_secctx_size(const struct nf_conn
*ct
)
503 #ifdef CONFIG_NF_CONNTRACK_SECMARK
506 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
510 return nla_total_size(0) /* CTA_SECCTX */
511 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
518 ctnetlink_timestamp_size(const struct nf_conn
*ct
)
520 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
521 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
523 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
530 ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
532 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
533 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
534 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
535 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
536 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
537 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
538 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
539 + ctnetlink_counters_size(ct
)
540 + ctnetlink_timestamp_size(ct
)
541 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
542 + nla_total_size(0) /* CTA_PROTOINFO */
543 + nla_total_size(0) /* CTA_HELP */
544 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
545 + ctnetlink_secctx_size(ct
)
546 #ifdef CONFIG_NF_NAT_NEEDED
547 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
548 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
550 #ifdef CONFIG_NF_CONNTRACK_MARK
551 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
553 + ctnetlink_proto_size(ct
)
558 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
561 struct nlmsghdr
*nlh
;
562 struct nfgenmsg
*nfmsg
;
563 struct nlattr
*nest_parms
;
564 struct nf_conn
*ct
= item
->ct
;
567 unsigned int flags
= 0, group
;
570 /* ignore our fake conntrack entry */
571 if (nf_ct_is_untracked(ct
))
574 if (events
& (1 << IPCT_DESTROY
)) {
575 type
= IPCTNL_MSG_CT_DELETE
;
576 group
= NFNLGRP_CONNTRACK_DESTROY
;
577 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
578 type
= IPCTNL_MSG_CT_NEW
;
579 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
580 group
= NFNLGRP_CONNTRACK_NEW
;
582 type
= IPCTNL_MSG_CT_NEW
;
583 group
= NFNLGRP_CONNTRACK_UPDATE
;
588 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
591 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
595 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
596 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
600 nfmsg
= nlmsg_data(nlh
);
601 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
602 nfmsg
->version
= NFNETLINK_V0
;
606 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
608 goto nla_put_failure
;
609 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
610 goto nla_put_failure
;
611 nla_nest_end(skb
, nest_parms
);
613 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
615 goto nla_put_failure
;
616 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
617 goto nla_put_failure
;
618 nla_nest_end(skb
, nest_parms
);
621 NLA_PUT_BE16(skb
, CTA_ZONE
, htons(nf_ct_zone(ct
)));
623 if (ctnetlink_dump_id(skb
, ct
) < 0)
624 goto nla_put_failure
;
626 if (ctnetlink_dump_status(skb
, ct
) < 0)
627 goto nla_put_failure
;
629 if (events
& (1 << IPCT_DESTROY
)) {
630 if (ctnetlink_dump_counters(skb
, ct
,
631 IP_CT_DIR_ORIGINAL
, type
) < 0 ||
632 ctnetlink_dump_counters(skb
, ct
,
633 IP_CT_DIR_REPLY
, type
) < 0 ||
634 ctnetlink_dump_timestamp(skb
, ct
) < 0)
635 goto nla_put_failure
;
637 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
638 goto nla_put_failure
;
640 if (events
& (1 << IPCT_PROTOINFO
)
641 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
642 goto nla_put_failure
;
644 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
645 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
646 goto nla_put_failure
;
648 #ifdef CONFIG_NF_CONNTRACK_SECMARK
649 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
650 && ctnetlink_dump_secctx(skb
, ct
) < 0)
651 goto nla_put_failure
;
654 if (events
& (1 << IPCT_RELATED
) &&
655 ctnetlink_dump_master(skb
, ct
) < 0)
656 goto nla_put_failure
;
658 if (events
& (1 << IPCT_NATSEQADJ
) &&
659 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
660 goto nla_put_failure
;
663 #ifdef CONFIG_NF_CONNTRACK_MARK
664 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
665 && ctnetlink_dump_mark(skb
, ct
) < 0)
666 goto nla_put_failure
;
671 err
= nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
,
673 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
680 nlmsg_cancel(skb
, nlh
);
684 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
689 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
691 static int ctnetlink_done(struct netlink_callback
*cb
)
694 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
700 struct ctnetlink_dump_filter
{
708 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
710 struct net
*net
= sock_net(skb
->sk
);
711 struct nf_conn
*ct
, *last
;
712 struct nf_conntrack_tuple_hash
*h
;
713 struct hlist_nulls_node
*n
;
714 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
715 u_int8_t l3proto
= nfmsg
->nfgen_family
;
717 #ifdef CONFIG_NF_CONNTRACK_MARK
718 const struct ctnetlink_dump_filter
*filter
= cb
->data
;
721 spin_lock_bh(&nf_conntrack_lock
);
722 last
= (struct nf_conn
*)cb
->args
[1];
723 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
725 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
727 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
729 ct
= nf_ct_tuplehash_to_ctrack(h
);
730 /* Dump entries of a given L3 protocol number.
731 * If it is not specified, ie. l3proto == 0,
732 * then dump everything. */
733 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
740 #ifdef CONFIG_NF_CONNTRACK_MARK
741 if (filter
&& !((ct
->mark
& filter
->mark
.mask
) ==
748 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
750 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
754 nf_conntrack_get(&ct
->ct_general
);
755 cb
->args
[1] = (unsigned long)ct
;
765 spin_unlock_bh(&nf_conntrack_lock
);
773 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
775 struct nlattr
*tb
[CTA_IP_MAX
+1];
776 struct nf_conntrack_l3proto
*l3proto
;
779 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
782 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
784 if (likely(l3proto
->nlattr_to_tuple
)) {
785 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
786 l3proto
->nla_policy
);
788 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
796 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
797 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
801 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
802 struct nf_conntrack_tuple
*tuple
)
804 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
805 struct nf_conntrack_l4proto
*l4proto
;
808 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
812 if (!tb
[CTA_PROTO_NUM
])
814 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
817 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
819 if (likely(l4proto
->nlattr_to_tuple
)) {
820 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
821 l4proto
->nla_policy
);
823 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
831 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
832 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
833 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
837 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
838 struct nf_conntrack_tuple
*tuple
,
839 enum ctattr_type type
, u_int8_t l3num
)
841 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
844 memset(tuple
, 0, sizeof(*tuple
));
846 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
848 if (!tb
[CTA_TUPLE_IP
])
851 tuple
->src
.l3num
= l3num
;
853 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
857 if (!tb
[CTA_TUPLE_PROTO
])
860 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
864 /* orig and expect tuples get DIR_ORIGINAL */
865 if (type
== CTA_TUPLE_REPLY
)
866 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
868 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
874 ctnetlink_parse_zone(const struct nlattr
*attr
, u16
*zone
)
877 #ifdef CONFIG_NF_CONNTRACK_ZONES
878 *zone
= ntohs(nla_get_be16(attr
));
888 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
889 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
893 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
)
895 struct nlattr
*tb
[CTA_HELP_MAX
+1];
897 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
899 if (!tb
[CTA_HELP_NAME
])
902 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
907 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
908 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
909 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
910 [CTA_STATUS
] = { .type
= NLA_U32
},
911 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
912 [CTA_HELP
] = { .type
= NLA_NESTED
},
913 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
914 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
915 [CTA_MARK
] = { .type
= NLA_U32
},
916 [CTA_ID
] = { .type
= NLA_U32
},
917 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
918 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
919 [CTA_ZONE
] = { .type
= NLA_U16
},
920 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
924 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
925 const struct nlmsghdr
*nlh
,
926 const struct nlattr
* const cda
[])
928 struct net
*net
= sock_net(ctnl
);
929 struct nf_conntrack_tuple_hash
*h
;
930 struct nf_conntrack_tuple tuple
;
932 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
933 u_int8_t u3
= nfmsg
->nfgen_family
;
937 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
941 if (cda
[CTA_TUPLE_ORIG
])
942 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
943 else if (cda
[CTA_TUPLE_REPLY
])
944 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
946 /* Flush the whole table */
947 nf_conntrack_flush_report(net
,
956 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
960 ct
= nf_ct_tuplehash_to_ctrack(h
);
963 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
964 if (id
!= (u32
)(unsigned long)ct
) {
970 if (del_timer(&ct
->timeout
)) {
971 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
973 nlmsg_report(nlh
)) < 0) {
974 nf_ct_delete_from_lists(ct
);
975 /* we failed to report the event, try later */
976 nf_ct_insert_dying_list(ct
);
980 /* death_by_timeout would report the event again */
981 set_bit(IPS_DYING_BIT
, &ct
->status
);
982 nf_ct_delete_from_lists(ct
);
991 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
992 const struct nlmsghdr
*nlh
,
993 const struct nlattr
* const cda
[])
995 struct net
*net
= sock_net(ctnl
);
996 struct nf_conntrack_tuple_hash
*h
;
997 struct nf_conntrack_tuple tuple
;
999 struct sk_buff
*skb2
= NULL
;
1000 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1001 u_int8_t u3
= nfmsg
->nfgen_family
;
1005 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1006 struct netlink_dump_control c
= {
1007 .dump
= ctnetlink_dump_table
,
1008 .done
= ctnetlink_done
,
1010 #ifdef CONFIG_NF_CONNTRACK_MARK
1011 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1012 struct ctnetlink_dump_filter
*filter
;
1014 filter
= kzalloc(sizeof(struct ctnetlink_dump_filter
),
1019 filter
->mark
.val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1021 ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
1025 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1028 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1032 if (cda
[CTA_TUPLE_ORIG
])
1033 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
1034 else if (cda
[CTA_TUPLE_REPLY
])
1035 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
1042 h
= nf_conntrack_find_get(net
, zone
, &tuple
);
1046 ct
= nf_ct_tuplehash_to_ctrack(h
);
1049 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1056 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
1057 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1063 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1072 /* this avoids a loop in nfnetlink. */
1073 return err
== -EAGAIN
? -ENOBUFS
: err
;
1076 #ifdef CONFIG_NF_NAT_NEEDED
1078 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1079 enum nf_nat_manip_type manip
,
1080 const struct nlattr
*attr
)
1082 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1084 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1085 if (!parse_nat_setup
) {
1086 #ifdef CONFIG_MODULES
1089 if (request_module("nf-nat-ipv4") < 0) {
1096 if (nfnetlink_parse_nat_setup_hook
)
1102 return parse_nat_setup(ct
, manip
, attr
);
1107 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1110 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1111 d
= ct
->status
^ status
;
1113 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1117 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1118 /* SEEN_REPLY bit can only be set */
1121 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1122 /* ASSURED bit can only be set */
1125 /* Be careful here, modifying NAT bits can screw up things,
1126 * so don't let users modify them directly if they don't pass
1128 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1133 ctnetlink_change_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1135 #ifdef CONFIG_NF_NAT_NEEDED
1138 if (cda
[CTA_NAT_DST
]) {
1139 ret
= ctnetlink_parse_nat_setup(ct
,
1145 if (cda
[CTA_NAT_SRC
]) {
1146 ret
= ctnetlink_parse_nat_setup(ct
,
1159 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1161 struct nf_conntrack_helper
*helper
;
1162 struct nf_conn_help
*help
= nfct_help(ct
);
1163 char *helpname
= NULL
;
1166 /* don't change helper of sibling connections */
1170 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1174 if (!strcmp(helpname
, "")) {
1175 if (help
&& help
->helper
) {
1176 /* we had a helper before ... */
1177 nf_ct_remove_expectations(ct
);
1178 RCU_INIT_POINTER(help
->helper
, NULL
);
1184 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1185 nf_ct_protonum(ct
));
1186 if (helper
== NULL
) {
1187 #ifdef CONFIG_MODULES
1188 spin_unlock_bh(&nf_conntrack_lock
);
1190 if (request_module("nfct-helper-%s", helpname
) < 0) {
1191 spin_lock_bh(&nf_conntrack_lock
);
1195 spin_lock_bh(&nf_conntrack_lock
);
1196 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1197 nf_ct_protonum(ct
));
1205 if (help
->helper
== helper
)
1209 /* need to zero data of old helper */
1210 memset(&help
->help
, 0, sizeof(help
->help
));
1212 /* we cannot set a helper for an existing conntrack */
1216 rcu_assign_pointer(help
->helper
, helper
);
1222 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1224 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1226 if (!del_timer(&ct
->timeout
))
1229 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1230 add_timer(&ct
->timeout
);
1235 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1236 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1237 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1238 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1242 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1244 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1245 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1246 struct nf_conntrack_l4proto
*l4proto
;
1249 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1252 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1253 if (l4proto
->from_nlattr
)
1254 err
= l4proto
->from_nlattr(tb
, ct
);
1260 #ifdef CONFIG_NF_NAT_NEEDED
1261 static const struct nla_policy nat_seq_policy
[CTA_NAT_SEQ_MAX
+1] = {
1262 [CTA_NAT_SEQ_CORRECTION_POS
] = { .type
= NLA_U32
},
1263 [CTA_NAT_SEQ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1264 [CTA_NAT_SEQ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1268 change_nat_seq_adj(struct nf_nat_seq
*natseq
, const struct nlattr
* const attr
)
1270 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1272 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, nat_seq_policy
);
1274 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1277 natseq
->correction_pos
=
1278 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1280 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1283 natseq
->offset_before
=
1284 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1286 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1289 natseq
->offset_after
=
1290 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1296 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
,
1297 const struct nlattr
* const cda
[])
1300 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1305 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1306 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1307 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1311 ct
->status
|= IPS_SEQ_ADJUST
;
1314 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1315 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1316 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1320 ct
->status
|= IPS_SEQ_ADJUST
;
1328 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1329 const struct nlattr
* const cda
[])
1333 /* only allow NAT changes and master assignation for new conntracks */
1334 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1337 if (cda
[CTA_HELP
]) {
1338 err
= ctnetlink_change_helper(ct
, cda
);
1343 if (cda
[CTA_TIMEOUT
]) {
1344 err
= ctnetlink_change_timeout(ct
, cda
);
1349 if (cda
[CTA_STATUS
]) {
1350 err
= ctnetlink_change_status(ct
, cda
);
1355 if (cda
[CTA_PROTOINFO
]) {
1356 err
= ctnetlink_change_protoinfo(ct
, cda
);
1361 #if defined(CONFIG_NF_CONNTRACK_MARK)
1363 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1366 #ifdef CONFIG_NF_NAT_NEEDED
1367 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1368 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1377 static struct nf_conn
*
1378 ctnetlink_create_conntrack(struct net
*net
, u16 zone
,
1379 const struct nlattr
* const cda
[],
1380 struct nf_conntrack_tuple
*otuple
,
1381 struct nf_conntrack_tuple
*rtuple
,
1386 struct nf_conntrack_helper
*helper
;
1387 struct nf_conn_tstamp
*tstamp
;
1389 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1391 return ERR_PTR(-ENOMEM
);
1393 if (!cda
[CTA_TIMEOUT
])
1395 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1397 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1400 if (cda
[CTA_HELP
]) {
1401 char *helpname
= NULL
;
1403 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
1407 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1408 nf_ct_protonum(ct
));
1409 if (helper
== NULL
) {
1411 #ifdef CONFIG_MODULES
1412 if (request_module("nfct-helper-%s", helpname
) < 0) {
1418 helper
= __nf_conntrack_helper_find(helpname
,
1420 nf_ct_protonum(ct
));
1430 struct nf_conn_help
*help
;
1432 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1438 /* not in hash table yet so not strictly necessary */
1439 RCU_INIT_POINTER(help
->helper
, helper
);
1442 /* try an implicit helper assignation */
1443 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1448 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1449 err
= ctnetlink_change_nat(ct
, cda
);
1454 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1455 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1456 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1457 /* we must add conntrack extensions before confirmation. */
1458 ct
->status
|= IPS_CONFIRMED
;
1460 if (cda
[CTA_STATUS
]) {
1461 err
= ctnetlink_change_status(ct
, cda
);
1466 #ifdef CONFIG_NF_NAT_NEEDED
1467 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1468 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1474 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1475 if (cda
[CTA_PROTOINFO
]) {
1476 err
= ctnetlink_change_protoinfo(ct
, cda
);
1481 #if defined(CONFIG_NF_CONNTRACK_MARK)
1483 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1486 /* setup master conntrack: this is a confirmed expectation */
1487 if (cda
[CTA_TUPLE_MASTER
]) {
1488 struct nf_conntrack_tuple master
;
1489 struct nf_conntrack_tuple_hash
*master_h
;
1490 struct nf_conn
*master_ct
;
1492 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
, u3
);
1496 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1497 if (master_h
== NULL
) {
1501 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1502 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1503 ct
->master
= master_ct
;
1505 tstamp
= nf_conn_tstamp_find(ct
);
1507 tstamp
->start
= ktime_to_ns(ktime_get_real());
1509 err
= nf_conntrack_hash_check_insert(ct
);
1520 nf_conntrack_free(ct
);
1521 return ERR_PTR(err
);
1525 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1526 const struct nlmsghdr
*nlh
,
1527 const struct nlattr
* const cda
[])
1529 struct net
*net
= sock_net(ctnl
);
1530 struct nf_conntrack_tuple otuple
, rtuple
;
1531 struct nf_conntrack_tuple_hash
*h
= NULL
;
1532 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1534 u_int8_t u3
= nfmsg
->nfgen_family
;
1538 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1542 if (cda
[CTA_TUPLE_ORIG
]) {
1543 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1548 if (cda
[CTA_TUPLE_REPLY
]) {
1549 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1554 if (cda
[CTA_TUPLE_ORIG
])
1555 h
= nf_conntrack_find_get(net
, zone
, &otuple
);
1556 else if (cda
[CTA_TUPLE_REPLY
])
1557 h
= nf_conntrack_find_get(net
, zone
, &rtuple
);
1561 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1562 enum ip_conntrack_events events
;
1564 ct
= ctnetlink_create_conntrack(net
, zone
, cda
, &otuple
,
1570 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1571 events
= IPCT_RELATED
;
1575 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1576 (1 << IPCT_ASSURED
) |
1577 (1 << IPCT_HELPER
) |
1578 (1 << IPCT_PROTOINFO
) |
1579 (1 << IPCT_NATSEQADJ
) |
1580 (1 << IPCT_MARK
) | events
,
1581 ct
, NETLINK_CB(skb
).pid
,
1588 /* implicit 'else' */
1591 ct
= nf_ct_tuplehash_to_ctrack(h
);
1592 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1593 spin_lock_bh(&nf_conntrack_lock
);
1594 err
= ctnetlink_change_conntrack(ct
, cda
);
1595 spin_unlock_bh(&nf_conntrack_lock
);
1597 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1598 (1 << IPCT_ASSURED
) |
1599 (1 << IPCT_HELPER
) |
1600 (1 << IPCT_PROTOINFO
) |
1601 (1 << IPCT_NATSEQADJ
) |
1603 ct
, NETLINK_CB(skb
).pid
,
1612 /***********************************************************************
1614 ***********************************************************************/
1617 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1618 const struct nf_conntrack_tuple
*tuple
,
1619 enum ctattr_expect type
)
1621 struct nlattr
*nest_parms
;
1623 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1625 goto nla_put_failure
;
1626 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1627 goto nla_put_failure
;
1628 nla_nest_end(skb
, nest_parms
);
1637 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1638 const struct nf_conntrack_tuple
*tuple
,
1639 const struct nf_conntrack_tuple_mask
*mask
)
1642 struct nf_conntrack_l3proto
*l3proto
;
1643 struct nf_conntrack_l4proto
*l4proto
;
1644 struct nf_conntrack_tuple m
;
1645 struct nlattr
*nest_parms
;
1647 memset(&m
, 0xFF, sizeof(m
));
1648 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1649 m
.src
.u
.all
= mask
->src
.u
.all
;
1650 m
.dst
.protonum
= tuple
->dst
.protonum
;
1652 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1654 goto nla_put_failure
;
1657 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
1658 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1660 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
1661 tuple
->dst
.protonum
);
1662 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1666 if (unlikely(ret
< 0))
1667 goto nla_put_failure
;
1669 nla_nest_end(skb
, nest_parms
);
1678 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1679 const struct nf_conntrack_expect
*exp
)
1681 struct nf_conn
*master
= exp
->master
;
1682 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
1683 struct nf_conn_help
*help
;
1684 #ifdef CONFIG_NF_NAT_NEEDED
1685 struct nlattr
*nest_parms
;
1686 struct nf_conntrack_tuple nat_tuple
= {};
1688 struct nf_ct_helper_expectfn
*expfn
;
1693 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1694 goto nla_put_failure
;
1695 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1696 goto nla_put_failure
;
1697 if (ctnetlink_exp_dump_tuple(skb
,
1698 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1699 CTA_EXPECT_MASTER
) < 0)
1700 goto nla_put_failure
;
1702 #ifdef CONFIG_NF_NAT_NEEDED
1703 if (exp
->saved_ip
|| exp
->saved_proto
.all
) {
1704 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
1706 goto nla_put_failure
;
1708 NLA_PUT_BE32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
));
1710 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
1711 nat_tuple
.src
.u3
.ip
= exp
->saved_ip
;
1712 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
1713 nat_tuple
.src
.u
= exp
->saved_proto
;
1715 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
1716 CTA_EXPECT_NAT_TUPLE
) < 0)
1717 goto nla_put_failure
;
1718 nla_nest_end(skb
, nest_parms
);
1721 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1722 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1723 NLA_PUT_BE32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
));
1724 NLA_PUT_BE32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class));
1725 help
= nfct_help(master
);
1727 struct nf_conntrack_helper
*helper
;
1729 helper
= rcu_dereference(help
->helper
);
1731 NLA_PUT_STRING(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
);
1733 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
1735 NLA_PUT_STRING(skb
, CTA_EXPECT_FN
, expfn
->name
);
1744 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1745 int event
, const struct nf_conntrack_expect
*exp
)
1747 struct nlmsghdr
*nlh
;
1748 struct nfgenmsg
*nfmsg
;
1749 unsigned int flags
= pid
? NLM_F_MULTI
: 0;
1751 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1752 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*nfmsg
), flags
);
1756 nfmsg
= nlmsg_data(nlh
);
1757 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1758 nfmsg
->version
= NFNETLINK_V0
;
1761 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1762 goto nla_put_failure
;
1764 nlmsg_end(skb
, nlh
);
1769 nlmsg_cancel(skb
, nlh
);
1773 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1775 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
1777 struct nf_conntrack_expect
*exp
= item
->exp
;
1778 struct net
*net
= nf_ct_exp_net(exp
);
1779 struct nlmsghdr
*nlh
;
1780 struct nfgenmsg
*nfmsg
;
1781 struct sk_buff
*skb
;
1782 unsigned int type
, group
;
1785 if (events
& (1 << IPEXP_DESTROY
)) {
1786 type
= IPCTNL_MSG_EXP_DELETE
;
1787 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
1788 } else if (events
& (1 << IPEXP_NEW
)) {
1789 type
= IPCTNL_MSG_EXP_NEW
;
1790 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1791 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
1795 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
1798 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1802 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1803 nlh
= nlmsg_put(skb
, item
->pid
, 0, type
, sizeof(*nfmsg
), flags
);
1807 nfmsg
= nlmsg_data(nlh
);
1808 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1809 nfmsg
->version
= NFNETLINK_V0
;
1813 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1814 goto nla_put_failure
;
1817 nlmsg_end(skb
, nlh
);
1818 nfnetlink_send(skb
, net
, item
->pid
, group
, item
->report
, GFP_ATOMIC
);
1823 nlmsg_cancel(skb
, nlh
);
1827 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
1831 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1834 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1839 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1841 struct net
*net
= sock_net(skb
->sk
);
1842 struct nf_conntrack_expect
*exp
, *last
;
1843 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1844 struct hlist_node
*n
;
1845 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1848 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1849 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1851 hlist_for_each_entry(exp
, n
, &net
->ct
.expect_hash
[cb
->args
[0]],
1853 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1860 if (ctnetlink_exp_fill_info(skb
,
1861 NETLINK_CB(cb
->skb
).pid
,
1865 if (!atomic_inc_not_zero(&exp
->use
))
1867 cb
->args
[1] = (unsigned long)exp
;
1879 nf_ct_expect_put(last
);
1884 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1885 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
1886 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
1887 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
1888 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1889 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1890 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
},
1891 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
1892 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
1893 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
1894 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
1895 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
1899 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1900 const struct nlmsghdr
*nlh
,
1901 const struct nlattr
* const cda
[])
1903 struct net
*net
= sock_net(ctnl
);
1904 struct nf_conntrack_tuple tuple
;
1905 struct nf_conntrack_expect
*exp
;
1906 struct sk_buff
*skb2
;
1907 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1908 u_int8_t u3
= nfmsg
->nfgen_family
;
1912 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1913 struct netlink_dump_control c
= {
1914 .dump
= ctnetlink_exp_dump_table
,
1915 .done
= ctnetlink_exp_done
,
1917 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1920 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1924 if (cda
[CTA_EXPECT_TUPLE
])
1925 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1926 else if (cda
[CTA_EXPECT_MASTER
])
1927 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1934 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
1938 if (cda
[CTA_EXPECT_ID
]) {
1939 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1940 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1941 nf_ct_expect_put(exp
);
1947 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1949 nf_ct_expect_put(exp
);
1954 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1955 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
1957 nf_ct_expect_put(exp
);
1961 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1970 /* this avoids a loop in nfnetlink. */
1971 return err
== -EAGAIN
? -ENOBUFS
: err
;
1975 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1976 const struct nlmsghdr
*nlh
,
1977 const struct nlattr
* const cda
[])
1979 struct net
*net
= sock_net(ctnl
);
1980 struct nf_conntrack_expect
*exp
;
1981 struct nf_conntrack_tuple tuple
;
1982 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1983 struct hlist_node
*n
, *next
;
1984 u_int8_t u3
= nfmsg
->nfgen_family
;
1989 if (cda
[CTA_EXPECT_TUPLE
]) {
1990 /* delete a single expect by tuple */
1991 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
1995 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1999 /* bump usage count to 2 */
2000 exp
= nf_ct_expect_find_get(net
, zone
, &tuple
);
2004 if (cda
[CTA_EXPECT_ID
]) {
2005 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2006 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2007 nf_ct_expect_put(exp
);
2012 /* after list removal, usage count == 1 */
2013 spin_lock_bh(&nf_conntrack_lock
);
2014 if (del_timer(&exp
->timeout
)) {
2015 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).pid
,
2017 nf_ct_expect_put(exp
);
2019 spin_unlock_bh(&nf_conntrack_lock
);
2020 /* have to put what we 'get' above.
2021 * after this line usage count == 0 */
2022 nf_ct_expect_put(exp
);
2023 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2024 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2025 struct nf_conn_help
*m_help
;
2027 /* delete all expectations for this helper */
2028 spin_lock_bh(&nf_conntrack_lock
);
2029 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2030 hlist_for_each_entry_safe(exp
, n
, next
,
2031 &net
->ct
.expect_hash
[i
],
2033 m_help
= nfct_help(exp
->master
);
2034 if (!strcmp(m_help
->helper
->name
, name
) &&
2035 del_timer(&exp
->timeout
)) {
2036 nf_ct_unlink_expect_report(exp
,
2037 NETLINK_CB(skb
).pid
,
2039 nf_ct_expect_put(exp
);
2043 spin_unlock_bh(&nf_conntrack_lock
);
2045 /* This basically means we have to flush everything*/
2046 spin_lock_bh(&nf_conntrack_lock
);
2047 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2048 hlist_for_each_entry_safe(exp
, n
, next
,
2049 &net
->ct
.expect_hash
[i
],
2051 if (del_timer(&exp
->timeout
)) {
2052 nf_ct_unlink_expect_report(exp
,
2053 NETLINK_CB(skb
).pid
,
2055 nf_ct_expect_put(exp
);
2059 spin_unlock_bh(&nf_conntrack_lock
);
2065 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2066 const struct nlattr
* const cda
[])
2071 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2072 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2073 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2077 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2078 struct nf_conntrack_expect
*exp
,
2081 #ifdef CONFIG_NF_NAT_NEEDED
2082 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2083 struct nf_conntrack_tuple nat_tuple
= {};
2086 nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2088 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2091 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2092 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
, u3
);
2096 exp
->saved_ip
= nat_tuple
.src
.u3
.ip
;
2097 exp
->saved_proto
= nat_tuple
.src
.u
;
2098 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2107 ctnetlink_create_expect(struct net
*net
, u16 zone
,
2108 const struct nlattr
* const cda
[],
2110 u32 pid
, int report
)
2112 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
2113 struct nf_conntrack_tuple_hash
*h
= NULL
;
2114 struct nf_conntrack_expect
*exp
;
2116 struct nf_conn_help
*help
;
2117 struct nf_conntrack_helper
*helper
= NULL
;
2118 u_int32_t
class = 0;
2121 /* caller guarantees that those three CTA_EXPECT_* exist */
2122 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2125 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
2128 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
2132 /* Look for master conntrack of this expectation */
2133 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
2136 ct
= nf_ct_tuplehash_to_ctrack(h
);
2138 /* Look for helper of this expectation */
2139 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2140 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2142 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2143 nf_ct_protonum(ct
));
2144 if (helper
== NULL
) {
2145 #ifdef CONFIG_MODULES
2146 if (request_module("nfct-helper-%s", helpname
) < 0) {
2151 helper
= __nf_conntrack_helper_find(helpname
,
2153 nf_ct_protonum(ct
));
2164 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2165 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
2166 if (class > helper
->expect_class_max
) {
2171 exp
= nf_ct_expect_alloc(ct
);
2176 help
= nfct_help(ct
);
2178 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
2182 exp
->timeout
.expires
=
2183 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2185 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
2186 if (cda
[CTA_EXPECT_FLAGS
]) {
2188 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2191 if (cda
[CTA_EXPECT_FLAGS
]) {
2192 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2193 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
2197 if (cda
[CTA_EXPECT_FN
]) {
2198 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
2199 struct nf_ct_helper_expectfn
*expfn
;
2201 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
2202 if (expfn
== NULL
) {
2206 exp
->expectfn
= expfn
->expectfn
;
2208 exp
->expectfn
= NULL
;
2212 exp
->helper
= helper
;
2213 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
2214 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
2215 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
2217 if (cda
[CTA_EXPECT_NAT
]) {
2218 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
2223 err
= nf_ct_expect_related_report(exp
, pid
, report
);
2225 nf_ct_expect_put(exp
);
2227 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
2232 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2233 const struct nlmsghdr
*nlh
,
2234 const struct nlattr
* const cda
[])
2236 struct net
*net
= sock_net(ctnl
);
2237 struct nf_conntrack_tuple tuple
;
2238 struct nf_conntrack_expect
*exp
;
2239 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2240 u_int8_t u3
= nfmsg
->nfgen_family
;
2244 if (!cda
[CTA_EXPECT_TUPLE
]
2245 || !cda
[CTA_EXPECT_MASK
]
2246 || !cda
[CTA_EXPECT_MASTER
])
2249 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2253 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
2257 spin_lock_bh(&nf_conntrack_lock
);
2258 exp
= __nf_ct_expect_find(net
, zone
, &tuple
);
2261 spin_unlock_bh(&nf_conntrack_lock
);
2263 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2264 err
= ctnetlink_create_expect(net
, zone
, cda
,
2266 NETLINK_CB(skb
).pid
,
2273 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
2274 err
= ctnetlink_change_expect(exp
, cda
);
2275 spin_unlock_bh(&nf_conntrack_lock
);
2280 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2281 static struct nf_ct_event_notifier ctnl_notifier
= {
2282 .fcn
= ctnetlink_conntrack_event
,
2285 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
2286 .fcn
= ctnetlink_expect_event
,
2290 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
2291 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
2292 .attr_count
= CTA_MAX
,
2293 .policy
= ct_nla_policy
},
2294 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
2295 .attr_count
= CTA_MAX
,
2296 .policy
= ct_nla_policy
},
2297 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
2298 .attr_count
= CTA_MAX
,
2299 .policy
= ct_nla_policy
},
2300 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
2301 .attr_count
= CTA_MAX
,
2302 .policy
= ct_nla_policy
},
2305 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
2306 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
2307 .attr_count
= CTA_EXPECT_MAX
,
2308 .policy
= exp_nla_policy
},
2309 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
2310 .attr_count
= CTA_EXPECT_MAX
,
2311 .policy
= exp_nla_policy
},
2312 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
2313 .attr_count
= CTA_EXPECT_MAX
,
2314 .policy
= exp_nla_policy
},
2317 static const struct nfnetlink_subsystem ctnl_subsys
= {
2318 .name
= "conntrack",
2319 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
2320 .cb_count
= IPCTNL_MSG_MAX
,
2324 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
2325 .name
= "conntrack_expect",
2326 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
2327 .cb_count
= IPCTNL_MSG_EXP_MAX
,
2331 MODULE_ALIAS("ip_conntrack_netlink");
2332 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
2333 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
2335 static int __net_init
ctnetlink_net_init(struct net
*net
)
2337 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2340 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
2342 pr_err("ctnetlink_init: cannot register notifier.\n");
2346 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
2348 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2349 goto err_unreg_notifier
;
2354 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2356 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
2362 static void ctnetlink_net_exit(struct net
*net
)
2364 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2365 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
2366 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
2370 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
2374 list_for_each_entry(net
, net_exit_list
, exit_list
)
2375 ctnetlink_net_exit(net
);
2378 static struct pernet_operations ctnetlink_net_ops
= {
2379 .init
= ctnetlink_net_init
,
2380 .exit_batch
= ctnetlink_net_exit_batch
,
2383 static int __init
ctnetlink_init(void)
2387 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
2388 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
2390 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2394 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
2396 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2397 goto err_unreg_subsys
;
2400 if (register_pernet_subsys(&ctnetlink_net_ops
)) {
2401 pr_err("ctnetlink_init: cannot register pernet operations\n");
2402 goto err_unreg_exp_subsys
;
2407 err_unreg_exp_subsys
:
2408 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2410 nfnetlink_subsys_unregister(&ctnl_subsys
);
2415 static void __exit
ctnetlink_exit(void)
2417 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2419 unregister_pernet_subsys(&ctnetlink_net_ops
);
2420 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
2421 nfnetlink_subsys_unregister(&ctnl_subsys
);
2424 module_init(ctnetlink_init
);
2425 module_exit(ctnetlink_exit
);