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-2012 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_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_seqadj.h>
49 #include <net/netfilter/nf_conntrack_synproxy.h>
50 #ifdef CONFIG_NF_NAT_NEEDED
51 #include <net/netfilter/nf_nat_core.h>
52 #include <net/netfilter/nf_nat_l4proto.h>
53 #include <net/netfilter/nf_nat_helper.h>
56 #include <linux/netfilter/nfnetlink.h>
57 #include <linux/netfilter/nfnetlink_conntrack.h>
59 MODULE_LICENSE("GPL");
61 static char __initdata version
[] = "0.93";
63 static int ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
64 const struct nf_conntrack_tuple
*tuple
,
65 struct nf_conntrack_l4proto
*l4proto
)
68 struct nlattr
*nest_parms
;
70 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
73 if (nla_put_u8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
))
76 if (likely(l4proto
->tuple_to_nlattr
))
77 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
79 nla_nest_end(skb
, nest_parms
);
87 static int ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
88 const struct nf_conntrack_tuple
*tuple
,
89 struct nf_conntrack_l3proto
*l3proto
)
92 struct nlattr
*nest_parms
;
94 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
98 if (likely(l3proto
->tuple_to_nlattr
))
99 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
101 nla_nest_end(skb
, nest_parms
);
109 static int ctnetlink_dump_tuples(struct sk_buff
*skb
,
110 const struct nf_conntrack_tuple
*tuple
)
113 struct nf_conntrack_l3proto
*l3proto
;
114 struct nf_conntrack_l4proto
*l4proto
;
117 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
118 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
121 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
122 tuple
->dst
.protonum
);
123 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
129 static int ctnetlink_dump_zone_id(struct sk_buff
*skb
, int attrtype
,
130 const struct nf_conntrack_zone
*zone
, int dir
)
132 if (zone
->id
== NF_CT_DEFAULT_ZONE_ID
|| zone
->dir
!= dir
)
134 if (nla_put_be16(skb
, attrtype
, htons(zone
->id
)))
135 goto nla_put_failure
;
142 static int ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
144 if (nla_put_be32(skb
, CTA_STATUS
, htonl(ct
->status
)))
145 goto nla_put_failure
;
152 static int ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
154 long timeout
= nf_ct_expires(ct
) / HZ
;
156 if (nla_put_be32(skb
, CTA_TIMEOUT
, htonl(timeout
)))
157 goto nla_put_failure
;
164 static int ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
166 struct nf_conntrack_l4proto
*l4proto
;
167 struct nlattr
*nest_proto
;
170 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
171 if (!l4proto
->to_nlattr
)
174 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
176 goto nla_put_failure
;
178 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
180 nla_nest_end(skb
, nest_proto
);
188 static int ctnetlink_dump_helpinfo(struct sk_buff
*skb
,
189 const struct nf_conn
*ct
)
191 struct nlattr
*nest_helper
;
192 const struct nf_conn_help
*help
= nfct_help(ct
);
193 struct nf_conntrack_helper
*helper
;
198 helper
= rcu_dereference(help
->helper
);
202 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
204 goto nla_put_failure
;
205 if (nla_put_string(skb
, CTA_HELP_NAME
, helper
->name
))
206 goto nla_put_failure
;
208 if (helper
->to_nlattr
)
209 helper
->to_nlattr(skb
, ct
);
211 nla_nest_end(skb
, nest_helper
);
220 dump_counters(struct sk_buff
*skb
, struct nf_conn_acct
*acct
,
221 enum ip_conntrack_dir dir
, int type
)
223 enum ctattr_type attr
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
224 struct nf_conn_counter
*counter
= acct
->counter
;
225 struct nlattr
*nest_count
;
228 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
229 pkts
= atomic64_xchg(&counter
[dir
].packets
, 0);
230 bytes
= atomic64_xchg(&counter
[dir
].bytes
, 0);
232 pkts
= atomic64_read(&counter
[dir
].packets
);
233 bytes
= atomic64_read(&counter
[dir
].bytes
);
236 nest_count
= nla_nest_start(skb
, attr
| NLA_F_NESTED
);
238 goto nla_put_failure
;
240 if (nla_put_be64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
),
242 nla_put_be64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
),
244 goto nla_put_failure
;
246 nla_nest_end(skb
, nest_count
);
255 ctnetlink_dump_acct(struct sk_buff
*skb
, const struct nf_conn
*ct
, int type
)
257 struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
262 if (dump_counters(skb
, acct
, IP_CT_DIR_ORIGINAL
, type
) < 0)
264 if (dump_counters(skb
, acct
, IP_CT_DIR_REPLY
, type
) < 0)
271 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
273 struct nlattr
*nest_count
;
274 const struct nf_conn_tstamp
*tstamp
;
276 tstamp
= nf_conn_tstamp_find(ct
);
280 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
| NLA_F_NESTED
);
282 goto nla_put_failure
;
284 if (nla_put_be64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
),
285 CTA_TIMESTAMP_PAD
) ||
286 (tstamp
->stop
!= 0 && nla_put_be64(skb
, CTA_TIMESTAMP_STOP
,
287 cpu_to_be64(tstamp
->stop
),
289 goto nla_put_failure
;
290 nla_nest_end(skb
, nest_count
);
298 #ifdef CONFIG_NF_CONNTRACK_MARK
299 static int ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
301 if (nla_put_be32(skb
, CTA_MARK
, htonl(ct
->mark
)))
302 goto nla_put_failure
;
309 #define ctnetlink_dump_mark(a, b) (0)
312 #ifdef CONFIG_NF_CONNTRACK_SECMARK
313 static int ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
315 struct nlattr
*nest_secctx
;
319 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
324 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
326 goto nla_put_failure
;
328 if (nla_put_string(skb
, CTA_SECCTX_NAME
, secctx
))
329 goto nla_put_failure
;
330 nla_nest_end(skb
, nest_secctx
);
334 security_release_secctx(secctx
, len
);
338 #define ctnetlink_dump_secctx(a, b) (0)
341 #ifdef CONFIG_NF_CONNTRACK_LABELS
342 static inline int ctnetlink_label_size(const struct nf_conn
*ct
)
344 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
348 return nla_total_size(sizeof(labels
->bits
));
352 ctnetlink_dump_labels(struct sk_buff
*skb
, const struct nf_conn
*ct
)
354 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
362 if (labels
->bits
[i
] != 0)
363 return nla_put(skb
, CTA_LABELS
, sizeof(labels
->bits
),
366 } while (i
< ARRAY_SIZE(labels
->bits
));
371 #define ctnetlink_dump_labels(a, b) (0)
372 #define ctnetlink_label_size(a) (0)
375 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
377 static int ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
379 struct nlattr
*nest_parms
;
381 if (!(ct
->status
& IPS_EXPECTED
))
384 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
386 goto nla_put_failure
;
387 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
388 goto nla_put_failure
;
389 nla_nest_end(skb
, nest_parms
);
398 dump_ct_seq_adj(struct sk_buff
*skb
, const struct nf_ct_seqadj
*seq
, int type
)
400 struct nlattr
*nest_parms
;
402 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
404 goto nla_put_failure
;
406 if (nla_put_be32(skb
, CTA_SEQADJ_CORRECTION_POS
,
407 htonl(seq
->correction_pos
)) ||
408 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_BEFORE
,
409 htonl(seq
->offset_before
)) ||
410 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_AFTER
,
411 htonl(seq
->offset_after
)))
412 goto nla_put_failure
;
414 nla_nest_end(skb
, nest_parms
);
422 static int ctnetlink_dump_ct_seq_adj(struct sk_buff
*skb
, struct nf_conn
*ct
)
424 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
425 struct nf_ct_seqadj
*seq
;
427 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !seqadj
)
430 spin_lock_bh(&ct
->lock
);
431 seq
= &seqadj
->seq
[IP_CT_DIR_ORIGINAL
];
432 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_ORIG
) == -1)
435 seq
= &seqadj
->seq
[IP_CT_DIR_REPLY
];
436 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_REPLY
) == -1)
439 spin_unlock_bh(&ct
->lock
);
442 spin_unlock_bh(&ct
->lock
);
446 static int ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
448 if (nla_put_be32(skb
, CTA_ID
, htonl((unsigned long)ct
)))
449 goto nla_put_failure
;
456 static int ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
458 if (nla_put_be32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
))))
459 goto nla_put_failure
;
467 ctnetlink_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
470 const struct nf_conntrack_zone
*zone
;
471 struct nlmsghdr
*nlh
;
472 struct nfgenmsg
*nfmsg
;
473 struct nlattr
*nest_parms
;
474 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
476 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
, IPCTNL_MSG_CT_NEW
);
477 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
481 nfmsg
= nlmsg_data(nlh
);
482 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
483 nfmsg
->version
= NFNETLINK_V0
;
486 zone
= nf_ct_zone(ct
);
488 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
490 goto nla_put_failure
;
491 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
492 goto nla_put_failure
;
493 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
494 NF_CT_ZONE_DIR_ORIG
) < 0)
495 goto nla_put_failure
;
496 nla_nest_end(skb
, nest_parms
);
498 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
500 goto nla_put_failure
;
501 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
502 goto nla_put_failure
;
503 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
504 NF_CT_ZONE_DIR_REPL
) < 0)
505 goto nla_put_failure
;
506 nla_nest_end(skb
, nest_parms
);
508 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
509 NF_CT_DEFAULT_ZONE_DIR
) < 0)
510 goto nla_put_failure
;
512 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
513 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
514 ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
515 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
516 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
517 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
518 ctnetlink_dump_mark(skb
, ct
) < 0 ||
519 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
520 ctnetlink_dump_labels(skb
, ct
) < 0 ||
521 ctnetlink_dump_id(skb
, ct
) < 0 ||
522 ctnetlink_dump_use(skb
, ct
) < 0 ||
523 ctnetlink_dump_master(skb
, ct
) < 0 ||
524 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
525 goto nla_put_failure
;
532 nlmsg_cancel(skb
, nlh
);
536 static inline size_t ctnetlink_proto_size(const struct nf_conn
*ct
)
538 struct nf_conntrack_l3proto
*l3proto
;
539 struct nf_conntrack_l4proto
*l4proto
;
543 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
544 len
+= l3proto
->nla_size
;
546 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
547 len
+= l4proto
->nla_size
;
553 static inline size_t ctnetlink_acct_size(const struct nf_conn
*ct
)
555 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
557 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
558 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
559 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
563 static inline int ctnetlink_secctx_size(const struct nf_conn
*ct
)
565 #ifdef CONFIG_NF_CONNTRACK_SECMARK
568 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
572 return nla_total_size(0) /* CTA_SECCTX */
573 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
579 static inline size_t ctnetlink_timestamp_size(const struct nf_conn
*ct
)
581 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
582 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
584 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
590 #ifdef CONFIG_NF_CONNTRACK_EVENTS
591 static size_t ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
593 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
594 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
595 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
596 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
597 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
598 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
599 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
600 + ctnetlink_acct_size(ct
)
601 + ctnetlink_timestamp_size(ct
)
602 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
603 + nla_total_size(0) /* CTA_PROTOINFO */
604 + nla_total_size(0) /* CTA_HELP */
605 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
606 + ctnetlink_secctx_size(ct
)
607 #ifdef CONFIG_NF_NAT_NEEDED
608 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
609 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
611 #ifdef CONFIG_NF_CONNTRACK_MARK
612 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
614 #ifdef CONFIG_NF_CONNTRACK_ZONES
615 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
617 + ctnetlink_proto_size(ct
)
618 + ctnetlink_label_size(ct
)
623 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
625 const struct nf_conntrack_zone
*zone
;
627 struct nlmsghdr
*nlh
;
628 struct nfgenmsg
*nfmsg
;
629 struct nlattr
*nest_parms
;
630 struct nf_conn
*ct
= item
->ct
;
633 unsigned int flags
= 0, group
;
636 if (events
& (1 << IPCT_DESTROY
)) {
637 type
= IPCTNL_MSG_CT_DELETE
;
638 group
= NFNLGRP_CONNTRACK_DESTROY
;
639 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
640 type
= IPCTNL_MSG_CT_NEW
;
641 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
642 group
= NFNLGRP_CONNTRACK_NEW
;
644 type
= IPCTNL_MSG_CT_NEW
;
645 group
= NFNLGRP_CONNTRACK_UPDATE
;
650 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
653 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
657 type
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
, type
);
658 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
662 nfmsg
= nlmsg_data(nlh
);
663 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
664 nfmsg
->version
= NFNETLINK_V0
;
668 zone
= nf_ct_zone(ct
);
670 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
672 goto nla_put_failure
;
673 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
674 goto nla_put_failure
;
675 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
676 NF_CT_ZONE_DIR_ORIG
) < 0)
677 goto nla_put_failure
;
678 nla_nest_end(skb
, nest_parms
);
680 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
682 goto nla_put_failure
;
683 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
684 goto nla_put_failure
;
685 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
686 NF_CT_ZONE_DIR_REPL
) < 0)
687 goto nla_put_failure
;
688 nla_nest_end(skb
, nest_parms
);
690 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
691 NF_CT_DEFAULT_ZONE_DIR
) < 0)
692 goto nla_put_failure
;
694 if (ctnetlink_dump_id(skb
, ct
) < 0)
695 goto nla_put_failure
;
697 if (ctnetlink_dump_status(skb
, ct
) < 0)
698 goto nla_put_failure
;
700 if (events
& (1 << IPCT_DESTROY
)) {
701 if (ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
702 ctnetlink_dump_timestamp(skb
, ct
) < 0)
703 goto nla_put_failure
;
705 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
706 goto nla_put_failure
;
708 if (events
& (1 << IPCT_PROTOINFO
)
709 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
710 goto nla_put_failure
;
712 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
713 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
714 goto nla_put_failure
;
716 #ifdef CONFIG_NF_CONNTRACK_SECMARK
717 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
718 && ctnetlink_dump_secctx(skb
, ct
) < 0)
719 goto nla_put_failure
;
721 if (events
& (1 << IPCT_LABEL
) &&
722 ctnetlink_dump_labels(skb
, ct
) < 0)
723 goto nla_put_failure
;
725 if (events
& (1 << IPCT_RELATED
) &&
726 ctnetlink_dump_master(skb
, ct
) < 0)
727 goto nla_put_failure
;
729 if (events
& (1 << IPCT_SEQADJ
) &&
730 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
731 goto nla_put_failure
;
734 #ifdef CONFIG_NF_CONNTRACK_MARK
735 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
736 && ctnetlink_dump_mark(skb
, ct
) < 0)
737 goto nla_put_failure
;
742 err
= nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
,
744 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
751 nlmsg_cancel(skb
, nlh
);
755 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
760 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
762 static int ctnetlink_done(struct netlink_callback
*cb
)
765 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
770 struct ctnetlink_filter
{
777 static struct ctnetlink_filter
*
778 ctnetlink_alloc_filter(const struct nlattr
* const cda
[])
780 #ifdef CONFIG_NF_CONNTRACK_MARK
781 struct ctnetlink_filter
*filter
;
783 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
785 return ERR_PTR(-ENOMEM
);
787 filter
->mark
.val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
788 filter
->mark
.mask
= ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
792 return ERR_PTR(-EOPNOTSUPP
);
796 static int ctnetlink_filter_match(struct nf_conn
*ct
, void *data
)
798 struct ctnetlink_filter
*filter
= data
;
803 #ifdef CONFIG_NF_CONNTRACK_MARK
804 if ((ct
->mark
& filter
->mark
.mask
) == filter
->mark
.val
)
812 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
814 struct net
*net
= sock_net(skb
->sk
);
815 struct nf_conn
*ct
, *last
;
816 struct nf_conntrack_tuple_hash
*h
;
817 struct hlist_nulls_node
*n
;
818 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
819 u_int8_t l3proto
= nfmsg
->nfgen_family
;
820 struct nf_conn
*nf_ct_evict
[8];
824 last
= (struct nf_conn
*)cb
->args
[1];
828 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
832 if (nf_ct_should_gc(nf_ct_evict
[i
]))
833 nf_ct_kill(nf_ct_evict
[i
]);
834 nf_ct_put(nf_ct_evict
[i
]);
837 lockp
= &nf_conntrack_locks
[cb
->args
[0] % CONNTRACK_LOCKS
];
838 nf_conntrack_lock(lockp
);
839 if (cb
->args
[0] >= nf_conntrack_htable_size
) {
843 hlist_nulls_for_each_entry(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
845 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
847 ct
= nf_ct_tuplehash_to_ctrack(h
);
848 if (nf_ct_is_expired(ct
)) {
849 if (i
< ARRAY_SIZE(nf_ct_evict
) &&
850 atomic_inc_not_zero(&ct
->ct_general
.use
))
851 nf_ct_evict
[i
++] = ct
;
855 if (!net_eq(net
, nf_ct_net(ct
)))
858 /* Dump entries of a given L3 protocol number.
859 * If it is not specified, ie. l3proto == 0,
860 * then dump everything. */
861 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
868 if (!ctnetlink_filter_match(ct
, cb
->data
))
873 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
875 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
879 nf_conntrack_get(&ct
->ct_general
);
880 cb
->args
[1] = (unsigned long)ct
;
894 /* nf ct hash resize happened, now clear the leftover. */
895 if ((struct nf_conn
*)cb
->args
[1] == last
)
903 if (nf_ct_should_gc(nf_ct_evict
[i
]))
904 nf_ct_kill(nf_ct_evict
[i
]);
905 nf_ct_put(nf_ct_evict
[i
]);
911 static int ctnetlink_parse_tuple_ip(struct nlattr
*attr
,
912 struct nf_conntrack_tuple
*tuple
)
914 struct nlattr
*tb
[CTA_IP_MAX
+1];
915 struct nf_conntrack_l3proto
*l3proto
;
918 ret
= nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
, NULL
);
923 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
925 if (likely(l3proto
->nlattr_to_tuple
)) {
926 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
927 l3proto
->nla_policy
, NULL
);
929 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
937 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
938 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
941 static int ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
942 struct nf_conntrack_tuple
*tuple
)
944 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
945 struct nf_conntrack_l4proto
*l4proto
;
948 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
,
953 if (!tb
[CTA_PROTO_NUM
])
955 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
958 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
960 if (likely(l4proto
->nlattr_to_tuple
)) {
961 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
962 l4proto
->nla_policy
, NULL
);
964 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
973 ctnetlink_parse_zone(const struct nlattr
*attr
,
974 struct nf_conntrack_zone
*zone
)
976 nf_ct_zone_init(zone
, NF_CT_DEFAULT_ZONE_ID
,
977 NF_CT_DEFAULT_ZONE_DIR
, 0);
978 #ifdef CONFIG_NF_CONNTRACK_ZONES
980 zone
->id
= ntohs(nla_get_be16(attr
));
989 ctnetlink_parse_tuple_zone(struct nlattr
*attr
, enum ctattr_type type
,
990 struct nf_conntrack_zone
*zone
)
994 if (zone
->id
!= NF_CT_DEFAULT_ZONE_ID
)
997 ret
= ctnetlink_parse_zone(attr
, zone
);
1001 if (type
== CTA_TUPLE_REPLY
)
1002 zone
->dir
= NF_CT_ZONE_DIR_REPL
;
1004 zone
->dir
= NF_CT_ZONE_DIR_ORIG
;
1009 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
1010 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
1011 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
1012 [CTA_TUPLE_ZONE
] = { .type
= NLA_U16
},
1016 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
1017 struct nf_conntrack_tuple
*tuple
, u32 type
,
1018 u_int8_t l3num
, struct nf_conntrack_zone
*zone
)
1020 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
1023 memset(tuple
, 0, sizeof(*tuple
));
1025 err
= nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
,
1030 if (!tb
[CTA_TUPLE_IP
])
1033 tuple
->src
.l3num
= l3num
;
1035 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
1039 if (!tb
[CTA_TUPLE_PROTO
])
1042 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
1046 if (tb
[CTA_TUPLE_ZONE
]) {
1050 err
= ctnetlink_parse_tuple_zone(tb
[CTA_TUPLE_ZONE
],
1056 /* orig and expect tuples get DIR_ORIGINAL */
1057 if (type
== CTA_TUPLE_REPLY
)
1058 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
1060 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
1065 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
1066 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
1067 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1070 static int ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
,
1071 struct nlattr
**helpinfo
)
1074 struct nlattr
*tb
[CTA_HELP_MAX
+1];
1076 err
= nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
, NULL
);
1080 if (!tb
[CTA_HELP_NAME
])
1083 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
1085 if (tb
[CTA_HELP_INFO
])
1086 *helpinfo
= tb
[CTA_HELP_INFO
];
1091 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
1092 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
1093 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
1094 [CTA_STATUS
] = { .type
= NLA_U32
},
1095 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
1096 [CTA_HELP
] = { .type
= NLA_NESTED
},
1097 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
1098 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
1099 [CTA_MARK
] = { .type
= NLA_U32
},
1100 [CTA_ID
] = { .type
= NLA_U32
},
1101 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
1102 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
1103 [CTA_NAT_SEQ_ADJ_ORIG
] = { .type
= NLA_NESTED
},
1104 [CTA_NAT_SEQ_ADJ_REPLY
] = { .type
= NLA_NESTED
},
1105 [CTA_ZONE
] = { .type
= NLA_U16
},
1106 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
1107 [CTA_LABELS
] = { .type
= NLA_BINARY
,
1108 .len
= NF_CT_LABELS_MAX_SIZE
},
1109 [CTA_LABELS_MASK
] = { .type
= NLA_BINARY
,
1110 .len
= NF_CT_LABELS_MAX_SIZE
},
1113 static int ctnetlink_flush_conntrack(struct net
*net
,
1114 const struct nlattr
* const cda
[],
1115 u32 portid
, int report
)
1117 struct ctnetlink_filter
*filter
= NULL
;
1119 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1120 filter
= ctnetlink_alloc_filter(cda
);
1122 return PTR_ERR(filter
);
1125 nf_ct_iterate_cleanup(net
, ctnetlink_filter_match
, filter
,
1132 static int ctnetlink_del_conntrack(struct net
*net
, struct sock
*ctnl
,
1133 struct sk_buff
*skb
,
1134 const struct nlmsghdr
*nlh
,
1135 const struct nlattr
* const cda
[])
1137 struct nf_conntrack_tuple_hash
*h
;
1138 struct nf_conntrack_tuple tuple
;
1140 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1141 u_int8_t u3
= nfmsg
->nfgen_family
;
1142 struct nf_conntrack_zone zone
;
1145 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1149 if (cda
[CTA_TUPLE_ORIG
])
1150 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1152 else if (cda
[CTA_TUPLE_REPLY
])
1153 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1156 return ctnetlink_flush_conntrack(net
, cda
,
1157 NETLINK_CB(skb
).portid
,
1164 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1168 ct
= nf_ct_tuplehash_to_ctrack(h
);
1171 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
1172 if (id
!= (u32
)(unsigned long)ct
) {
1178 nf_ct_delete(ct
, NETLINK_CB(skb
).portid
, nlmsg_report(nlh
));
1184 static int ctnetlink_get_conntrack(struct net
*net
, struct sock
*ctnl
,
1185 struct sk_buff
*skb
,
1186 const struct nlmsghdr
*nlh
,
1187 const struct nlattr
* const cda
[])
1189 struct nf_conntrack_tuple_hash
*h
;
1190 struct nf_conntrack_tuple tuple
;
1192 struct sk_buff
*skb2
= NULL
;
1193 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1194 u_int8_t u3
= nfmsg
->nfgen_family
;
1195 struct nf_conntrack_zone zone
;
1198 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1199 struct netlink_dump_control c
= {
1200 .dump
= ctnetlink_dump_table
,
1201 .done
= ctnetlink_done
,
1204 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1205 struct ctnetlink_filter
*filter
;
1207 filter
= ctnetlink_alloc_filter(cda
);
1209 return PTR_ERR(filter
);
1213 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1216 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1220 if (cda
[CTA_TUPLE_ORIG
])
1221 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1223 else if (cda
[CTA_TUPLE_REPLY
])
1224 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1232 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1236 ct
= nf_ct_tuplehash_to_ctrack(h
);
1239 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1246 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
1247 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1253 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
1262 /* this avoids a loop in nfnetlink. */
1263 return err
== -EAGAIN
? -ENOBUFS
: err
;
1266 static int ctnetlink_done_list(struct netlink_callback
*cb
)
1269 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
1274 ctnetlink_dump_list(struct sk_buff
*skb
, struct netlink_callback
*cb
, bool dying
)
1276 struct nf_conn
*ct
, *last
;
1277 struct nf_conntrack_tuple_hash
*h
;
1278 struct hlist_nulls_node
*n
;
1279 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1280 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1283 struct hlist_nulls_head
*list
;
1284 struct net
*net
= sock_net(skb
->sk
);
1289 last
= (struct nf_conn
*)cb
->args
[1];
1291 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
1292 struct ct_pcpu
*pcpu
;
1294 if (!cpu_possible(cpu
))
1297 pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1298 spin_lock_bh(&pcpu
->lock
);
1299 list
= dying
? &pcpu
->dying
: &pcpu
->unconfirmed
;
1301 hlist_nulls_for_each_entry(h
, n
, list
, hnnode
) {
1302 ct
= nf_ct_tuplehash_to_ctrack(h
);
1303 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
1311 res
= ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1313 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
1317 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
1320 cb
->args
[1] = (unsigned long)ct
;
1321 spin_unlock_bh(&pcpu
->lock
);
1329 spin_unlock_bh(&pcpu
->lock
);
1340 ctnetlink_dump_dying(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1342 return ctnetlink_dump_list(skb
, cb
, true);
1345 static int ctnetlink_get_ct_dying(struct net
*net
, struct sock
*ctnl
,
1346 struct sk_buff
*skb
,
1347 const struct nlmsghdr
*nlh
,
1348 const struct nlattr
* const cda
[])
1350 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1351 struct netlink_dump_control c
= {
1352 .dump
= ctnetlink_dump_dying
,
1353 .done
= ctnetlink_done_list
,
1355 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1362 ctnetlink_dump_unconfirmed(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1364 return ctnetlink_dump_list(skb
, cb
, false);
1367 static int ctnetlink_get_ct_unconfirmed(struct net
*net
, struct sock
*ctnl
,
1368 struct sk_buff
*skb
,
1369 const struct nlmsghdr
*nlh
,
1370 const struct nlattr
* const cda
[])
1372 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1373 struct netlink_dump_control c
= {
1374 .dump
= ctnetlink_dump_unconfirmed
,
1375 .done
= ctnetlink_done_list
,
1377 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1383 #ifdef CONFIG_NF_NAT_NEEDED
1385 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1386 enum nf_nat_manip_type manip
,
1387 const struct nlattr
*attr
)
1389 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1392 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1393 if (!parse_nat_setup
) {
1394 #ifdef CONFIG_MODULES
1396 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1397 if (request_module("nf-nat") < 0) {
1398 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1404 if (nfnetlink_parse_nat_setup_hook
)
1410 err
= parse_nat_setup(ct
, manip
, attr
);
1411 if (err
== -EAGAIN
) {
1412 #ifdef CONFIG_MODULES
1414 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1415 if (request_module("nf-nat-%u", nf_ct_l3num(ct
)) < 0) {
1416 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1420 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1431 __ctnetlink_change_status(struct nf_conn
*ct
, unsigned long on
,
1436 /* Ignore these unchangable bits */
1437 on
&= ~IPS_UNCHANGEABLE_MASK
;
1438 off
&= ~IPS_UNCHANGEABLE_MASK
;
1440 for (bit
= 0; bit
< __IPS_MAX_BIT
; bit
++) {
1441 if (on
& (1 << bit
))
1442 set_bit(bit
, &ct
->status
);
1443 else if (off
& (1 << bit
))
1444 clear_bit(bit
, &ct
->status
);
1449 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1452 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1453 d
= ct
->status
^ status
;
1455 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1459 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1460 /* SEEN_REPLY bit can only be set */
1463 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1464 /* ASSURED bit can only be set */
1467 __ctnetlink_change_status(ct
, status
, 0);
1472 ctnetlink_setup_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1474 #ifdef CONFIG_NF_NAT_NEEDED
1477 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1480 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_DST
,
1485 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_SRC
,
1489 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1495 static int ctnetlink_change_helper(struct nf_conn
*ct
,
1496 const struct nlattr
* const cda
[])
1498 struct nf_conntrack_helper
*helper
;
1499 struct nf_conn_help
*help
= nfct_help(ct
);
1500 char *helpname
= NULL
;
1501 struct nlattr
*helpinfo
= NULL
;
1504 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1508 /* don't change helper of sibling connections */
1510 /* If we try to change the helper to the same thing twice,
1511 * treat the second attempt as a no-op instead of returning
1517 helper
= rcu_dereference(help
->helper
);
1518 if (helper
&& !strcmp(helper
->name
, helpname
))
1526 if (!strcmp(helpname
, "")) {
1527 if (help
&& help
->helper
) {
1528 /* we had a helper before ... */
1529 nf_ct_remove_expectations(ct
);
1530 RCU_INIT_POINTER(help
->helper
, NULL
);
1537 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1538 nf_ct_protonum(ct
));
1539 if (helper
== NULL
) {
1545 if (help
->helper
== helper
) {
1546 /* update private helper data if allowed. */
1547 if (helper
->from_nlattr
)
1548 helper
->from_nlattr(helpinfo
, ct
);
1553 /* we cannot set a helper for an existing conntrack */
1561 static int ctnetlink_change_timeout(struct nf_conn
*ct
,
1562 const struct nlattr
* const cda
[])
1564 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1566 ct
->timeout
= nfct_time_stamp
+ timeout
* HZ
;
1568 if (test_bit(IPS_DYING_BIT
, &ct
->status
))
1574 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1575 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1576 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1577 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1580 static int ctnetlink_change_protoinfo(struct nf_conn
*ct
,
1581 const struct nlattr
* const cda
[])
1583 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1584 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1585 struct nf_conntrack_l4proto
*l4proto
;
1588 err
= nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
,
1594 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1595 if (l4proto
->from_nlattr
)
1596 err
= l4proto
->from_nlattr(tb
, ct
);
1602 static const struct nla_policy seqadj_policy
[CTA_SEQADJ_MAX
+1] = {
1603 [CTA_SEQADJ_CORRECTION_POS
] = { .type
= NLA_U32
},
1604 [CTA_SEQADJ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1605 [CTA_SEQADJ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1608 static int change_seq_adj(struct nf_ct_seqadj
*seq
,
1609 const struct nlattr
* const attr
)
1612 struct nlattr
*cda
[CTA_SEQADJ_MAX
+1];
1614 err
= nla_parse_nested(cda
, CTA_SEQADJ_MAX
, attr
, seqadj_policy
, NULL
);
1618 if (!cda
[CTA_SEQADJ_CORRECTION_POS
])
1621 seq
->correction_pos
=
1622 ntohl(nla_get_be32(cda
[CTA_SEQADJ_CORRECTION_POS
]));
1624 if (!cda
[CTA_SEQADJ_OFFSET_BEFORE
])
1627 seq
->offset_before
=
1628 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_BEFORE
]));
1630 if (!cda
[CTA_SEQADJ_OFFSET_AFTER
])
1634 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_AFTER
]));
1640 ctnetlink_change_seq_adj(struct nf_conn
*ct
,
1641 const struct nlattr
* const cda
[])
1643 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
1649 spin_lock_bh(&ct
->lock
);
1650 if (cda
[CTA_SEQ_ADJ_ORIG
]) {
1651 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_ORIGINAL
],
1652 cda
[CTA_SEQ_ADJ_ORIG
]);
1656 set_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
);
1659 if (cda
[CTA_SEQ_ADJ_REPLY
]) {
1660 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_REPLY
],
1661 cda
[CTA_SEQ_ADJ_REPLY
]);
1665 set_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
);
1668 spin_unlock_bh(&ct
->lock
);
1671 spin_unlock_bh(&ct
->lock
);
1676 ctnetlink_attach_labels(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1678 #ifdef CONFIG_NF_CONNTRACK_LABELS
1679 size_t len
= nla_len(cda
[CTA_LABELS
]);
1680 const void *mask
= cda
[CTA_LABELS_MASK
];
1682 if (len
& (sizeof(u32
)-1)) /* must be multiple of u32 */
1686 if (nla_len(cda
[CTA_LABELS_MASK
]) == 0 ||
1687 nla_len(cda
[CTA_LABELS_MASK
]) != len
)
1689 mask
= nla_data(cda
[CTA_LABELS_MASK
]);
1694 return nf_connlabels_replace(ct
, nla_data(cda
[CTA_LABELS
]), mask
, len
);
1701 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1702 const struct nlattr
* const cda
[])
1706 /* only allow NAT changes and master assignation for new conntracks */
1707 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1710 if (cda
[CTA_HELP
]) {
1711 err
= ctnetlink_change_helper(ct
, cda
);
1716 if (cda
[CTA_TIMEOUT
]) {
1717 err
= ctnetlink_change_timeout(ct
, cda
);
1722 if (cda
[CTA_STATUS
]) {
1723 err
= ctnetlink_change_status(ct
, cda
);
1728 if (cda
[CTA_PROTOINFO
]) {
1729 err
= ctnetlink_change_protoinfo(ct
, cda
);
1734 #if defined(CONFIG_NF_CONNTRACK_MARK)
1736 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1739 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1740 err
= ctnetlink_change_seq_adj(ct
, cda
);
1745 if (cda
[CTA_LABELS
]) {
1746 err
= ctnetlink_attach_labels(ct
, cda
);
1754 static struct nf_conn
*
1755 ctnetlink_create_conntrack(struct net
*net
,
1756 const struct nf_conntrack_zone
*zone
,
1757 const struct nlattr
* const cda
[],
1758 struct nf_conntrack_tuple
*otuple
,
1759 struct nf_conntrack_tuple
*rtuple
,
1764 struct nf_conntrack_helper
*helper
;
1765 struct nf_conn_tstamp
*tstamp
;
1767 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1769 return ERR_PTR(-ENOMEM
);
1771 if (!cda
[CTA_TIMEOUT
])
1774 ct
->timeout
= nfct_time_stamp
+ ntohl(nla_get_be32(cda
[CTA_TIMEOUT
])) * HZ
;
1777 if (cda
[CTA_HELP
]) {
1778 char *helpname
= NULL
;
1779 struct nlattr
*helpinfo
= NULL
;
1781 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1785 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1786 nf_ct_protonum(ct
));
1787 if (helper
== NULL
) {
1789 #ifdef CONFIG_MODULES
1790 if (request_module("nfct-helper-%s", helpname
) < 0) {
1796 helper
= __nf_conntrack_helper_find(helpname
,
1798 nf_ct_protonum(ct
));
1808 struct nf_conn_help
*help
;
1810 help
= nf_ct_helper_ext_add(ct
, helper
, GFP_ATOMIC
);
1815 /* set private helper data if allowed. */
1816 if (helper
->from_nlattr
)
1817 helper
->from_nlattr(helpinfo
, ct
);
1819 /* not in hash table yet so not strictly necessary */
1820 RCU_INIT_POINTER(help
->helper
, helper
);
1823 /* try an implicit helper assignation */
1824 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1829 err
= ctnetlink_setup_nat(ct
, cda
);
1833 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1834 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1835 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1836 nf_ct_labels_ext_add(ct
);
1837 nfct_seqadj_ext_add(ct
);
1838 nfct_synproxy_ext_add(ct
);
1840 /* we must add conntrack extensions before confirmation. */
1841 ct
->status
|= IPS_CONFIRMED
;
1843 if (cda
[CTA_STATUS
]) {
1844 err
= ctnetlink_change_status(ct
, cda
);
1849 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1850 err
= ctnetlink_change_seq_adj(ct
, cda
);
1855 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1856 if (cda
[CTA_PROTOINFO
]) {
1857 err
= ctnetlink_change_protoinfo(ct
, cda
);
1862 #if defined(CONFIG_NF_CONNTRACK_MARK)
1864 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1867 /* setup master conntrack: this is a confirmed expectation */
1868 if (cda
[CTA_TUPLE_MASTER
]) {
1869 struct nf_conntrack_tuple master
;
1870 struct nf_conntrack_tuple_hash
*master_h
;
1871 struct nf_conn
*master_ct
;
1873 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
,
1878 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1879 if (master_h
== NULL
) {
1883 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1884 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1885 ct
->master
= master_ct
;
1887 tstamp
= nf_conn_tstamp_find(ct
);
1889 tstamp
->start
= ktime_get_real_ns();
1891 err
= nf_conntrack_hash_check_insert(ct
);
1902 nf_conntrack_free(ct
);
1903 return ERR_PTR(err
);
1906 static int ctnetlink_new_conntrack(struct net
*net
, struct sock
*ctnl
,
1907 struct sk_buff
*skb
,
1908 const struct nlmsghdr
*nlh
,
1909 const struct nlattr
* const cda
[])
1911 struct nf_conntrack_tuple otuple
, rtuple
;
1912 struct nf_conntrack_tuple_hash
*h
= NULL
;
1913 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1915 u_int8_t u3
= nfmsg
->nfgen_family
;
1916 struct nf_conntrack_zone zone
;
1919 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1923 if (cda
[CTA_TUPLE_ORIG
]) {
1924 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
,
1930 if (cda
[CTA_TUPLE_REPLY
]) {
1931 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
,
1937 if (cda
[CTA_TUPLE_ORIG
])
1938 h
= nf_conntrack_find_get(net
, &zone
, &otuple
);
1939 else if (cda
[CTA_TUPLE_REPLY
])
1940 h
= nf_conntrack_find_get(net
, &zone
, &rtuple
);
1944 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1945 enum ip_conntrack_events events
;
1947 if (!cda
[CTA_TUPLE_ORIG
] || !cda
[CTA_TUPLE_REPLY
])
1949 if (otuple
.dst
.protonum
!= rtuple
.dst
.protonum
)
1952 ct
= ctnetlink_create_conntrack(net
, &zone
, cda
, &otuple
,
1958 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1959 events
= 1 << IPCT_RELATED
;
1961 events
= 1 << IPCT_NEW
;
1963 if (cda
[CTA_LABELS
] &&
1964 ctnetlink_attach_labels(ct
, cda
) == 0)
1965 events
|= (1 << IPCT_LABEL
);
1967 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1968 (1 << IPCT_ASSURED
) |
1969 (1 << IPCT_HELPER
) |
1970 (1 << IPCT_PROTOINFO
) |
1971 (1 << IPCT_SEQADJ
) |
1972 (1 << IPCT_MARK
) | events
,
1973 ct
, NETLINK_CB(skb
).portid
,
1980 /* implicit 'else' */
1983 ct
= nf_ct_tuplehash_to_ctrack(h
);
1984 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1985 err
= ctnetlink_change_conntrack(ct
, cda
);
1987 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1988 (1 << IPCT_ASSURED
) |
1989 (1 << IPCT_HELPER
) |
1991 (1 << IPCT_PROTOINFO
) |
1992 (1 << IPCT_SEQADJ
) |
1994 ct
, NETLINK_CB(skb
).portid
,
2004 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2005 __u16 cpu
, const struct ip_conntrack_stat
*st
)
2007 struct nlmsghdr
*nlh
;
2008 struct nfgenmsg
*nfmsg
;
2009 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2011 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
,
2012 IPCTNL_MSG_CT_GET_STATS_CPU
);
2013 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2017 nfmsg
= nlmsg_data(nlh
);
2018 nfmsg
->nfgen_family
= AF_UNSPEC
;
2019 nfmsg
->version
= NFNETLINK_V0
;
2020 nfmsg
->res_id
= htons(cpu
);
2022 if (nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
2023 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
2024 nla_put_be32(skb
, CTA_STATS_IGNORE
, htonl(st
->ignore
)) ||
2025 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
2026 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
2027 htonl(st
->insert_failed
)) ||
2028 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
2029 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
2030 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
2031 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
2032 htonl(st
->search_restart
)))
2033 goto nla_put_failure
;
2035 nlmsg_end(skb
, nlh
);
2040 nlmsg_cancel(skb
, nlh
);
2045 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2048 struct net
*net
= sock_net(skb
->sk
);
2050 if (cb
->args
[0] == nr_cpu_ids
)
2053 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2054 const struct ip_conntrack_stat
*st
;
2056 if (!cpu_possible(cpu
))
2059 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2060 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
2061 NETLINK_CB(cb
->skb
).portid
,
2071 static int ctnetlink_stat_ct_cpu(struct net
*net
, struct sock
*ctnl
,
2072 struct sk_buff
*skb
,
2073 const struct nlmsghdr
*nlh
,
2074 const struct nlattr
* const cda
[])
2076 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2077 struct netlink_dump_control c
= {
2078 .dump
= ctnetlink_ct_stat_cpu_dump
,
2080 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2087 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
2090 struct nlmsghdr
*nlh
;
2091 struct nfgenmsg
*nfmsg
;
2092 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2093 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
2095 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
, IPCTNL_MSG_CT_GET_STATS
);
2096 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2100 nfmsg
= nlmsg_data(nlh
);
2101 nfmsg
->nfgen_family
= AF_UNSPEC
;
2102 nfmsg
->version
= NFNETLINK_V0
;
2105 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
2106 goto nla_put_failure
;
2108 nlmsg_end(skb
, nlh
);
2113 nlmsg_cancel(skb
, nlh
);
2117 static int ctnetlink_stat_ct(struct net
*net
, struct sock
*ctnl
,
2118 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2119 const struct nlattr
* const cda
[])
2121 struct sk_buff
*skb2
;
2124 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2128 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2130 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
2135 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2144 /* this avoids a loop in nfnetlink. */
2145 return err
== -EAGAIN
? -ENOBUFS
: err
;
2148 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2149 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2150 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2151 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2152 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2153 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2154 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
2155 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
2156 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2157 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2158 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2159 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2160 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2163 static struct nf_conntrack_expect
*
2164 ctnetlink_alloc_expect(const struct nlattr
*const cda
[], struct nf_conn
*ct
,
2165 struct nf_conntrack_helper
*helper
,
2166 struct nf_conntrack_tuple
*tuple
,
2167 struct nf_conntrack_tuple
*mask
);
2169 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2171 ctnetlink_glue_build_size(const struct nf_conn
*ct
)
2173 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2174 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2175 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2176 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
2177 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
2178 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
2179 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
2180 + nla_total_size(0) /* CTA_PROTOINFO */
2181 + nla_total_size(0) /* CTA_HELP */
2182 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
2183 + ctnetlink_secctx_size(ct
)
2184 #ifdef CONFIG_NF_NAT_NEEDED
2185 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2186 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
2188 #ifdef CONFIG_NF_CONNTRACK_MARK
2189 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
2191 #ifdef CONFIG_NF_CONNTRACK_ZONES
2192 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2194 + ctnetlink_proto_size(ct
)
2198 static struct nf_conn
*ctnetlink_glue_get_ct(const struct sk_buff
*skb
,
2199 enum ip_conntrack_info
*ctinfo
)
2201 return nf_ct_get(skb
, ctinfo
);
2204 static int __ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
2206 const struct nf_conntrack_zone
*zone
;
2207 struct nlattr
*nest_parms
;
2210 zone
= nf_ct_zone(ct
);
2212 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
2214 goto nla_put_failure
;
2215 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
2216 goto nla_put_failure
;
2217 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2218 NF_CT_ZONE_DIR_ORIG
) < 0)
2219 goto nla_put_failure
;
2220 nla_nest_end(skb
, nest_parms
);
2222 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
2224 goto nla_put_failure
;
2225 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
2226 goto nla_put_failure
;
2227 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2228 NF_CT_ZONE_DIR_REPL
) < 0)
2229 goto nla_put_failure
;
2230 nla_nest_end(skb
, nest_parms
);
2232 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
2233 NF_CT_DEFAULT_ZONE_DIR
) < 0)
2234 goto nla_put_failure
;
2236 if (ctnetlink_dump_id(skb
, ct
) < 0)
2237 goto nla_put_failure
;
2239 if (ctnetlink_dump_status(skb
, ct
) < 0)
2240 goto nla_put_failure
;
2242 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
2243 goto nla_put_failure
;
2245 if (ctnetlink_dump_protoinfo(skb
, ct
) < 0)
2246 goto nla_put_failure
;
2248 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
2249 goto nla_put_failure
;
2251 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2252 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
2253 goto nla_put_failure
;
2255 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
2256 goto nla_put_failure
;
2258 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
2259 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
2260 goto nla_put_failure
;
2262 #ifdef CONFIG_NF_CONNTRACK_MARK
2263 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
2264 goto nla_put_failure
;
2266 if (ctnetlink_dump_labels(skb
, ct
) < 0)
2267 goto nla_put_failure
;
2277 ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
,
2278 enum ip_conntrack_info ctinfo
,
2279 u_int16_t ct_attr
, u_int16_t ct_info_attr
)
2281 struct nlattr
*nest_parms
;
2283 nest_parms
= nla_nest_start(skb
, ct_attr
| NLA_F_NESTED
);
2285 goto nla_put_failure
;
2287 if (__ctnetlink_glue_build(skb
, ct
) < 0)
2288 goto nla_put_failure
;
2290 nla_nest_end(skb
, nest_parms
);
2292 if (nla_put_be32(skb
, ct_info_attr
, htonl(ctinfo
)))
2293 goto nla_put_failure
;
2302 ctnetlink_update_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
2304 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
2305 unsigned long d
= ct
->status
^ status
;
2307 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
2308 /* SEEN_REPLY bit can only be set */
2311 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
2312 /* ASSURED bit can only be set */
2315 /* This check is less strict than ctnetlink_change_status()
2316 * because callers often flip IPS_EXPECTED bits when sending
2317 * an NFQA_CT attribute to the kernel. So ignore the
2318 * unchangeable bits but do not error out. Also user programs
2319 * are allowed to clear the bits that they are allowed to change.
2321 __ctnetlink_change_status(ct
, status
, ~status
);
2326 ctnetlink_glue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
2330 if (cda
[CTA_TIMEOUT
]) {
2331 err
= ctnetlink_change_timeout(ct
, cda
);
2335 if (cda
[CTA_STATUS
]) {
2336 err
= ctnetlink_update_status(ct
, cda
);
2340 if (cda
[CTA_HELP
]) {
2341 err
= ctnetlink_change_helper(ct
, cda
);
2345 if (cda
[CTA_LABELS
]) {
2346 err
= ctnetlink_attach_labels(ct
, cda
);
2350 #if defined(CONFIG_NF_CONNTRACK_MARK)
2351 if (cda
[CTA_MARK
]) {
2352 u32 mask
= 0, mark
, newmark
;
2353 if (cda
[CTA_MARK_MASK
])
2354 mask
= ~ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
2356 mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
2357 newmark
= (ct
->mark
& mask
) ^ mark
;
2358 if (newmark
!= ct
->mark
)
2366 ctnetlink_glue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
2368 struct nlattr
*cda
[CTA_MAX
+1];
2371 ret
= nla_parse_nested(cda
, CTA_MAX
, attr
, ct_nla_policy
, NULL
);
2375 return ctnetlink_glue_parse_ct((const struct nlattr
**)cda
, ct
);
2378 static int ctnetlink_glue_exp_parse(const struct nlattr
* const *cda
,
2379 const struct nf_conn
*ct
,
2380 struct nf_conntrack_tuple
*tuple
,
2381 struct nf_conntrack_tuple
*mask
)
2385 err
= ctnetlink_parse_tuple(cda
, tuple
, CTA_EXPECT_TUPLE
,
2386 nf_ct_l3num(ct
), NULL
);
2390 return ctnetlink_parse_tuple(cda
, mask
, CTA_EXPECT_MASK
,
2391 nf_ct_l3num(ct
), NULL
);
2395 ctnetlink_glue_attach_expect(const struct nlattr
*attr
, struct nf_conn
*ct
,
2396 u32 portid
, u32 report
)
2398 struct nlattr
*cda
[CTA_EXPECT_MAX
+1];
2399 struct nf_conntrack_tuple tuple
, mask
;
2400 struct nf_conntrack_helper
*helper
= NULL
;
2401 struct nf_conntrack_expect
*exp
;
2404 err
= nla_parse_nested(cda
, CTA_EXPECT_MAX
, attr
, exp_nla_policy
,
2409 err
= ctnetlink_glue_exp_parse((const struct nlattr
* const *)cda
,
2414 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2415 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2417 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2418 nf_ct_protonum(ct
));
2423 exp
= ctnetlink_alloc_expect((const struct nlattr
* const *)cda
, ct
,
2424 helper
, &tuple
, &mask
);
2426 return PTR_ERR(exp
);
2428 err
= nf_ct_expect_related_report(exp
, portid
, report
);
2429 nf_ct_expect_put(exp
);
2433 static void ctnetlink_glue_seqadj(struct sk_buff
*skb
, struct nf_conn
*ct
,
2434 enum ip_conntrack_info ctinfo
, int diff
)
2436 if (!(ct
->status
& IPS_NAT_MASK
))
2439 nf_ct_tcp_seqadj_set(skb
, ct
, ctinfo
, diff
);
2442 static struct nfnl_ct_hook ctnetlink_glue_hook
= {
2443 .get_ct
= ctnetlink_glue_get_ct
,
2444 .build_size
= ctnetlink_glue_build_size
,
2445 .build
= ctnetlink_glue_build
,
2446 .parse
= ctnetlink_glue_parse
,
2447 .attach_expect
= ctnetlink_glue_attach_expect
,
2448 .seq_adjust
= ctnetlink_glue_seqadj
,
2450 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2452 /***********************************************************************
2454 ***********************************************************************/
2456 static int ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
2457 const struct nf_conntrack_tuple
*tuple
,
2460 struct nlattr
*nest_parms
;
2462 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
2464 goto nla_put_failure
;
2465 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
2466 goto nla_put_failure
;
2467 nla_nest_end(skb
, nest_parms
);
2475 static int ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
2476 const struct nf_conntrack_tuple
*tuple
,
2477 const struct nf_conntrack_tuple_mask
*mask
)
2480 struct nf_conntrack_l3proto
*l3proto
;
2481 struct nf_conntrack_l4proto
*l4proto
;
2482 struct nf_conntrack_tuple m
;
2483 struct nlattr
*nest_parms
;
2485 memset(&m
, 0xFF, sizeof(m
));
2486 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
2487 m
.src
.u
.all
= mask
->src
.u
.all
;
2488 m
.dst
.protonum
= tuple
->dst
.protonum
;
2490 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
2492 goto nla_put_failure
;
2495 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
2496 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
2498 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
2499 tuple
->dst
.protonum
);
2500 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
2504 if (unlikely(ret
< 0))
2505 goto nla_put_failure
;
2507 nla_nest_end(skb
, nest_parms
);
2515 static const union nf_inet_addr any_addr
;
2518 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
2519 const struct nf_conntrack_expect
*exp
)
2521 struct nf_conn
*master
= exp
->master
;
2522 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
2523 struct nf_conn_help
*help
;
2524 #ifdef CONFIG_NF_NAT_NEEDED
2525 struct nlattr
*nest_parms
;
2526 struct nf_conntrack_tuple nat_tuple
= {};
2528 struct nf_ct_helper_expectfn
*expfn
;
2533 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
2534 goto nla_put_failure
;
2535 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
2536 goto nla_put_failure
;
2537 if (ctnetlink_exp_dump_tuple(skb
,
2538 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
2539 CTA_EXPECT_MASTER
) < 0)
2540 goto nla_put_failure
;
2542 #ifdef CONFIG_NF_NAT_NEEDED
2543 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
2544 exp
->saved_proto
.all
) {
2545 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
2547 goto nla_put_failure
;
2549 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
2550 goto nla_put_failure
;
2552 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
2553 nat_tuple
.src
.u3
= exp
->saved_addr
;
2554 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
2555 nat_tuple
.src
.u
= exp
->saved_proto
;
2557 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
2558 CTA_EXPECT_NAT_TUPLE
) < 0)
2559 goto nla_put_failure
;
2560 nla_nest_end(skb
, nest_parms
);
2563 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
2564 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
2565 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
2566 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
2567 goto nla_put_failure
;
2568 help
= nfct_help(master
);
2570 struct nf_conntrack_helper
*helper
;
2572 helper
= rcu_dereference(help
->helper
);
2574 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
2575 goto nla_put_failure
;
2577 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
2578 if (expfn
!= NULL
&&
2579 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
2580 goto nla_put_failure
;
2589 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2590 int event
, const struct nf_conntrack_expect
*exp
)
2592 struct nlmsghdr
*nlh
;
2593 struct nfgenmsg
*nfmsg
;
2594 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
2596 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP
, event
);
2597 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2601 nfmsg
= nlmsg_data(nlh
);
2602 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2603 nfmsg
->version
= NFNETLINK_V0
;
2606 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2607 goto nla_put_failure
;
2609 nlmsg_end(skb
, nlh
);
2614 nlmsg_cancel(skb
, nlh
);
2618 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2620 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
2622 struct nf_conntrack_expect
*exp
= item
->exp
;
2623 struct net
*net
= nf_ct_exp_net(exp
);
2624 struct nlmsghdr
*nlh
;
2625 struct nfgenmsg
*nfmsg
;
2626 struct sk_buff
*skb
;
2627 unsigned int type
, group
;
2630 if (events
& (1 << IPEXP_DESTROY
)) {
2631 type
= IPCTNL_MSG_EXP_DELETE
;
2632 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
2633 } else if (events
& (1 << IPEXP_NEW
)) {
2634 type
= IPCTNL_MSG_EXP_NEW
;
2635 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
2636 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
2640 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
2643 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
2647 type
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP
, type
);
2648 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
2652 nfmsg
= nlmsg_data(nlh
);
2653 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2654 nfmsg
->version
= NFNETLINK_V0
;
2658 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2659 goto nla_put_failure
;
2662 nlmsg_end(skb
, nlh
);
2663 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
2668 nlmsg_cancel(skb
, nlh
);
2672 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
2676 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
2679 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
2684 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2686 struct net
*net
= sock_net(skb
->sk
);
2687 struct nf_conntrack_expect
*exp
, *last
;
2688 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2689 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2692 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2693 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
2695 hlist_for_each_entry_rcu(exp
, &nf_ct_expect_hash
[cb
->args
[0]],
2697 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2700 if (!net_eq(nf_ct_net(exp
->master
), net
))
2708 if (ctnetlink_exp_fill_info(skb
,
2709 NETLINK_CB(cb
->skb
).portid
,
2713 if (!refcount_inc_not_zero(&exp
->use
))
2715 cb
->args
[1] = (unsigned long)exp
;
2727 nf_ct_expect_put(last
);
2733 ctnetlink_exp_ct_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2735 struct nf_conntrack_expect
*exp
, *last
;
2736 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2737 struct nf_conn
*ct
= cb
->data
;
2738 struct nf_conn_help
*help
= nfct_help(ct
);
2739 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2745 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2747 hlist_for_each_entry_rcu(exp
, &help
->expectations
, lnode
) {
2748 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2755 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
2759 if (!refcount_inc_not_zero(&exp
->use
))
2761 cb
->args
[1] = (unsigned long)exp
;
2773 nf_ct_expect_put(last
);
2778 static int ctnetlink_dump_exp_ct(struct net
*net
, struct sock
*ctnl
,
2779 struct sk_buff
*skb
,
2780 const struct nlmsghdr
*nlh
,
2781 const struct nlattr
* const cda
[])
2784 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2785 u_int8_t u3
= nfmsg
->nfgen_family
;
2786 struct nf_conntrack_tuple tuple
;
2787 struct nf_conntrack_tuple_hash
*h
;
2789 struct nf_conntrack_zone zone
;
2790 struct netlink_dump_control c
= {
2791 .dump
= ctnetlink_exp_ct_dump_table
,
2792 .done
= ctnetlink_exp_done
,
2795 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2800 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2804 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
2808 ct
= nf_ct_tuplehash_to_ctrack(h
);
2809 /* No expectation linked to this connection tracking. */
2810 if (!nfct_help(ct
)) {
2817 err
= netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2823 static int ctnetlink_get_expect(struct net
*net
, struct sock
*ctnl
,
2824 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2825 const struct nlattr
* const cda
[])
2827 struct nf_conntrack_tuple tuple
;
2828 struct nf_conntrack_expect
*exp
;
2829 struct sk_buff
*skb2
;
2830 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2831 u_int8_t u3
= nfmsg
->nfgen_family
;
2832 struct nf_conntrack_zone zone
;
2835 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2836 if (cda
[CTA_EXPECT_MASTER
])
2837 return ctnetlink_dump_exp_ct(net
, ctnl
, skb
, nlh
, cda
);
2839 struct netlink_dump_control c
= {
2840 .dump
= ctnetlink_exp_dump_table
,
2841 .done
= ctnetlink_exp_done
,
2843 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2847 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2851 if (cda
[CTA_EXPECT_TUPLE
])
2852 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2854 else if (cda
[CTA_EXPECT_MASTER
])
2855 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2863 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2867 if (cda
[CTA_EXPECT_ID
]) {
2868 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2869 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2870 nf_ct_expect_put(exp
);
2876 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2878 nf_ct_expect_put(exp
);
2883 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2884 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
2886 nf_ct_expect_put(exp
);
2890 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2899 /* this avoids a loop in nfnetlink. */
2900 return err
== -EAGAIN
? -ENOBUFS
: err
;
2903 static int ctnetlink_del_expect(struct net
*net
, struct sock
*ctnl
,
2904 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2905 const struct nlattr
* const cda
[])
2907 struct nf_conntrack_expect
*exp
;
2908 struct nf_conntrack_tuple tuple
;
2909 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2910 struct hlist_node
*next
;
2911 u_int8_t u3
= nfmsg
->nfgen_family
;
2912 struct nf_conntrack_zone zone
;
2916 if (cda
[CTA_EXPECT_TUPLE
]) {
2917 /* delete a single expect by tuple */
2918 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2922 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2927 /* bump usage count to 2 */
2928 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2932 if (cda
[CTA_EXPECT_ID
]) {
2933 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2934 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2935 nf_ct_expect_put(exp
);
2940 /* after list removal, usage count == 1 */
2941 spin_lock_bh(&nf_conntrack_expect_lock
);
2942 if (del_timer(&exp
->timeout
)) {
2943 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
2945 nf_ct_expect_put(exp
);
2947 spin_unlock_bh(&nf_conntrack_expect_lock
);
2948 /* have to put what we 'get' above.
2949 * after this line usage count == 0 */
2950 nf_ct_expect_put(exp
);
2951 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2952 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2953 struct nf_conn_help
*m_help
;
2955 /* delete all expectations for this helper */
2956 spin_lock_bh(&nf_conntrack_expect_lock
);
2957 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2958 hlist_for_each_entry_safe(exp
, next
,
2959 &nf_ct_expect_hash
[i
],
2962 if (!net_eq(nf_ct_exp_net(exp
), net
))
2965 m_help
= nfct_help(exp
->master
);
2966 if (!strcmp(m_help
->helper
->name
, name
) &&
2967 del_timer(&exp
->timeout
)) {
2968 nf_ct_unlink_expect_report(exp
,
2969 NETLINK_CB(skb
).portid
,
2971 nf_ct_expect_put(exp
);
2975 spin_unlock_bh(&nf_conntrack_expect_lock
);
2977 /* This basically means we have to flush everything*/
2978 spin_lock_bh(&nf_conntrack_expect_lock
);
2979 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2980 hlist_for_each_entry_safe(exp
, next
,
2981 &nf_ct_expect_hash
[i
],
2984 if (!net_eq(nf_ct_exp_net(exp
), net
))
2987 if (del_timer(&exp
->timeout
)) {
2988 nf_ct_unlink_expect_report(exp
,
2989 NETLINK_CB(skb
).portid
,
2991 nf_ct_expect_put(exp
);
2995 spin_unlock_bh(&nf_conntrack_expect_lock
);
3001 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
3002 const struct nlattr
* const cda
[])
3004 if (cda
[CTA_EXPECT_TIMEOUT
]) {
3005 if (!del_timer(&x
->timeout
))
3008 x
->timeout
.expires
= jiffies
+
3009 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
3010 add_timer(&x
->timeout
);
3015 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
3016 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
3017 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
3021 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
3022 struct nf_conntrack_expect
*exp
,
3025 #ifdef CONFIG_NF_NAT_NEEDED
3026 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
3027 struct nf_conntrack_tuple nat_tuple
= {};
3030 err
= nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
,
3031 exp_nat_nla_policy
, NULL
);
3035 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
3038 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
3039 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
,
3044 exp
->saved_addr
= nat_tuple
.src
.u3
;
3045 exp
->saved_proto
= nat_tuple
.src
.u
;
3046 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
3054 static struct nf_conntrack_expect
*
3055 ctnetlink_alloc_expect(const struct nlattr
* const cda
[], struct nf_conn
*ct
,
3056 struct nf_conntrack_helper
*helper
,
3057 struct nf_conntrack_tuple
*tuple
,
3058 struct nf_conntrack_tuple
*mask
)
3060 u_int32_t
class = 0;
3061 struct nf_conntrack_expect
*exp
;
3062 struct nf_conn_help
*help
;
3065 help
= nfct_help(ct
);
3067 return ERR_PTR(-EOPNOTSUPP
);
3069 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
3070 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
3071 if (class > helper
->expect_class_max
)
3072 return ERR_PTR(-EINVAL
);
3074 exp
= nf_ct_expect_alloc(ct
);
3076 return ERR_PTR(-ENOMEM
);
3078 if (cda
[CTA_EXPECT_FLAGS
]) {
3079 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
3080 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
3084 if (cda
[CTA_EXPECT_FN
]) {
3085 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
3086 struct nf_ct_helper_expectfn
*expfn
;
3088 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
3089 if (expfn
== NULL
) {
3093 exp
->expectfn
= expfn
->expectfn
;
3095 exp
->expectfn
= NULL
;
3099 exp
->helper
= helper
;
3100 exp
->tuple
= *tuple
;
3101 exp
->mask
.src
.u3
= mask
->src
.u3
;
3102 exp
->mask
.src
.u
.all
= mask
->src
.u
.all
;
3104 if (cda
[CTA_EXPECT_NAT
]) {
3105 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
3106 exp
, nf_ct_l3num(ct
));
3112 nf_ct_expect_put(exp
);
3113 return ERR_PTR(err
);
3117 ctnetlink_create_expect(struct net
*net
,
3118 const struct nf_conntrack_zone
*zone
,
3119 const struct nlattr
* const cda
[],
3120 u_int8_t u3
, u32 portid
, int report
)
3122 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
3123 struct nf_conntrack_tuple_hash
*h
= NULL
;
3124 struct nf_conntrack_helper
*helper
= NULL
;
3125 struct nf_conntrack_expect
*exp
;
3129 /* caller guarantees that those three CTA_EXPECT_* exist */
3130 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3134 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
,
3138 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
,
3143 /* Look for master conntrack of this expectation */
3144 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
3147 ct
= nf_ct_tuplehash_to_ctrack(h
);
3150 if (cda
[CTA_EXPECT_HELP_NAME
]) {
3151 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3153 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3154 nf_ct_protonum(ct
));
3155 if (helper
== NULL
) {
3157 #ifdef CONFIG_MODULES
3158 if (request_module("nfct-helper-%s", helpname
) < 0) {
3163 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3164 nf_ct_protonum(ct
));
3176 exp
= ctnetlink_alloc_expect(cda
, ct
, helper
, &tuple
, &mask
);
3182 err
= nf_ct_expect_related_report(exp
, portid
, report
);
3183 nf_ct_expect_put(exp
);
3191 static int ctnetlink_new_expect(struct net
*net
, struct sock
*ctnl
,
3192 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3193 const struct nlattr
* const cda
[])
3195 struct nf_conntrack_tuple tuple
;
3196 struct nf_conntrack_expect
*exp
;
3197 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3198 u_int8_t u3
= nfmsg
->nfgen_family
;
3199 struct nf_conntrack_zone zone
;
3202 if (!cda
[CTA_EXPECT_TUPLE
]
3203 || !cda
[CTA_EXPECT_MASK
]
3204 || !cda
[CTA_EXPECT_MASTER
])
3207 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3211 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3216 spin_lock_bh(&nf_conntrack_expect_lock
);
3217 exp
= __nf_ct_expect_find(net
, &zone
, &tuple
);
3219 spin_unlock_bh(&nf_conntrack_expect_lock
);
3221 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
3222 err
= ctnetlink_create_expect(net
, &zone
, cda
, u3
,
3223 NETLINK_CB(skb
).portid
,
3230 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
3231 err
= ctnetlink_change_expect(exp
, cda
);
3232 spin_unlock_bh(&nf_conntrack_expect_lock
);
3238 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
3239 const struct ip_conntrack_stat
*st
)
3241 struct nlmsghdr
*nlh
;
3242 struct nfgenmsg
*nfmsg
;
3243 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
3245 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
,
3246 IPCTNL_MSG_EXP_GET_STATS_CPU
);
3247 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
3251 nfmsg
= nlmsg_data(nlh
);
3252 nfmsg
->nfgen_family
= AF_UNSPEC
;
3253 nfmsg
->version
= NFNETLINK_V0
;
3254 nfmsg
->res_id
= htons(cpu
);
3256 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
3257 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
3258 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
3259 goto nla_put_failure
;
3261 nlmsg_end(skb
, nlh
);
3266 nlmsg_cancel(skb
, nlh
);
3271 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3274 struct net
*net
= sock_net(skb
->sk
);
3276 if (cb
->args
[0] == nr_cpu_ids
)
3279 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
3280 const struct ip_conntrack_stat
*st
;
3282 if (!cpu_possible(cpu
))
3285 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
3286 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3296 static int ctnetlink_stat_exp_cpu(struct net
*net
, struct sock
*ctnl
,
3297 struct sk_buff
*skb
,
3298 const struct nlmsghdr
*nlh
,
3299 const struct nlattr
* const cda
[])
3301 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3302 struct netlink_dump_control c
= {
3303 .dump
= ctnetlink_exp_stat_cpu_dump
,
3305 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
3311 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3312 static struct nf_ct_event_notifier ctnl_notifier
= {
3313 .fcn
= ctnetlink_conntrack_event
,
3316 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
3317 .fcn
= ctnetlink_expect_event
,
3321 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
3322 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
3323 .attr_count
= CTA_MAX
,
3324 .policy
= ct_nla_policy
},
3325 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
3326 .attr_count
= CTA_MAX
,
3327 .policy
= ct_nla_policy
},
3328 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
3329 .attr_count
= CTA_MAX
,
3330 .policy
= ct_nla_policy
},
3331 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
3332 .attr_count
= CTA_MAX
,
3333 .policy
= ct_nla_policy
},
3334 [IPCTNL_MSG_CT_GET_STATS_CPU
] = { .call
= ctnetlink_stat_ct_cpu
},
3335 [IPCTNL_MSG_CT_GET_STATS
] = { .call
= ctnetlink_stat_ct
},
3336 [IPCTNL_MSG_CT_GET_DYING
] = { .call
= ctnetlink_get_ct_dying
},
3337 [IPCTNL_MSG_CT_GET_UNCONFIRMED
] = { .call
= ctnetlink_get_ct_unconfirmed
},
3340 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
3341 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
3342 .attr_count
= CTA_EXPECT_MAX
,
3343 .policy
= exp_nla_policy
},
3344 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
3345 .attr_count
= CTA_EXPECT_MAX
,
3346 .policy
= exp_nla_policy
},
3347 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
3348 .attr_count
= CTA_EXPECT_MAX
,
3349 .policy
= exp_nla_policy
},
3350 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = { .call
= ctnetlink_stat_exp_cpu
},
3353 static const struct nfnetlink_subsystem ctnl_subsys
= {
3354 .name
= "conntrack",
3355 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
3356 .cb_count
= IPCTNL_MSG_MAX
,
3360 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
3361 .name
= "conntrack_expect",
3362 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
3363 .cb_count
= IPCTNL_MSG_EXP_MAX
,
3367 MODULE_ALIAS("ip_conntrack_netlink");
3368 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
3369 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
3371 static int __net_init
ctnetlink_net_init(struct net
*net
)
3373 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3376 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
3378 pr_err("ctnetlink_init: cannot register notifier.\n");
3382 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
3384 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3385 goto err_unreg_notifier
;
3390 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3392 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3398 static void ctnetlink_net_exit(struct net
*net
)
3400 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3401 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
3402 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3406 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
3410 list_for_each_entry(net
, net_exit_list
, exit_list
)
3411 ctnetlink_net_exit(net
);
3414 static struct pernet_operations ctnetlink_net_ops
= {
3415 .init
= ctnetlink_net_init
,
3416 .exit_batch
= ctnetlink_net_exit_batch
,
3419 static int __init
ctnetlink_init(void)
3423 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
3424 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
3426 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3430 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
3432 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3433 goto err_unreg_subsys
;
3436 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
3438 pr_err("ctnetlink_init: cannot register pernet operations\n");
3439 goto err_unreg_exp_subsys
;
3441 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3442 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3443 RCU_INIT_POINTER(nfnl_ct_hook
, &ctnetlink_glue_hook
);
3447 err_unreg_exp_subsys
:
3448 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3450 nfnetlink_subsys_unregister(&ctnl_subsys
);
3455 static void __exit
ctnetlink_exit(void)
3457 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3459 unregister_pernet_subsys(&ctnetlink_net_ops
);
3460 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3461 nfnetlink_subsys_unregister(&ctnl_subsys
);
3462 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3463 RCU_INIT_POINTER(nfnl_ct_hook
, NULL
);
3468 module_init(ctnetlink_init
);
3469 module_exit(ctnetlink_exit
);