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 #ifdef CONFIG_NF_NAT_NEEDED
49 #include <net/netfilter/nf_nat_core.h>
50 #include <net/netfilter/nf_nat_l4proto.h>
51 #include <net/netfilter/nf_nat_helper.h>
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
57 MODULE_LICENSE("GPL");
59 static char __initdata version
[] = "0.93";
61 static int ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
62 const struct nf_conntrack_tuple
*tuple
,
63 struct nf_conntrack_l4proto
*l4proto
)
66 struct nlattr
*nest_parms
;
68 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
71 if (nla_put_u8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
))
74 if (likely(l4proto
->tuple_to_nlattr
))
75 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
77 nla_nest_end(skb
, nest_parms
);
85 static int ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
86 const struct nf_conntrack_tuple
*tuple
,
87 struct nf_conntrack_l3proto
*l3proto
)
90 struct nlattr
*nest_parms
;
92 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
96 if (likely(l3proto
->tuple_to_nlattr
))
97 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
99 nla_nest_end(skb
, nest_parms
);
107 static int ctnetlink_dump_tuples(struct sk_buff
*skb
,
108 const struct nf_conntrack_tuple
*tuple
)
111 struct nf_conntrack_l3proto
*l3proto
;
112 struct nf_conntrack_l4proto
*l4proto
;
115 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
116 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
119 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
120 tuple
->dst
.protonum
);
121 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
127 static int ctnetlink_dump_zone_id(struct sk_buff
*skb
, int attrtype
,
128 const struct nf_conntrack_zone
*zone
, int dir
)
130 if (zone
->id
== NF_CT_DEFAULT_ZONE_ID
|| zone
->dir
!= dir
)
132 if (nla_put_be16(skb
, attrtype
, htons(zone
->id
)))
133 goto nla_put_failure
;
140 static int ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
142 if (nla_put_be32(skb
, CTA_STATUS
, htonl(ct
->status
)))
143 goto nla_put_failure
;
150 static int ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
152 long timeout
= ((long)ct
->timeout
.expires
- (long)jiffies
) / HZ
;
157 if (nla_put_be32(skb
, CTA_TIMEOUT
, htonl(timeout
)))
158 goto nla_put_failure
;
165 static int ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
167 struct nf_conntrack_l4proto
*l4proto
;
168 struct nlattr
*nest_proto
;
171 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
172 if (!l4proto
->to_nlattr
)
175 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
177 goto nla_put_failure
;
179 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
181 nla_nest_end(skb
, nest_proto
);
189 static int ctnetlink_dump_helpinfo(struct sk_buff
*skb
,
190 const struct nf_conn
*ct
)
192 struct nlattr
*nest_helper
;
193 const struct nf_conn_help
*help
= nfct_help(ct
);
194 struct nf_conntrack_helper
*helper
;
199 helper
= rcu_dereference(help
->helper
);
203 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
205 goto nla_put_failure
;
206 if (nla_put_string(skb
, CTA_HELP_NAME
, helper
->name
))
207 goto nla_put_failure
;
209 if (helper
->to_nlattr
)
210 helper
->to_nlattr(skb
, ct
);
212 nla_nest_end(skb
, nest_helper
);
221 dump_counters(struct sk_buff
*skb
, struct nf_conn_acct
*acct
,
222 enum ip_conntrack_dir dir
, int type
)
224 enum ctattr_type attr
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
225 struct nf_conn_counter
*counter
= acct
->counter
;
226 struct nlattr
*nest_count
;
229 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
230 pkts
= atomic64_xchg(&counter
[dir
].packets
, 0);
231 bytes
= atomic64_xchg(&counter
[dir
].bytes
, 0);
233 pkts
= atomic64_read(&counter
[dir
].packets
);
234 bytes
= atomic64_read(&counter
[dir
].bytes
);
237 nest_count
= nla_nest_start(skb
, attr
| NLA_F_NESTED
);
239 goto nla_put_failure
;
241 if (nla_put_be64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
),
243 nla_put_be64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
),
245 goto nla_put_failure
;
247 nla_nest_end(skb
, nest_count
);
256 ctnetlink_dump_acct(struct sk_buff
*skb
, const struct nf_conn
*ct
, int type
)
258 struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
263 if (dump_counters(skb
, acct
, IP_CT_DIR_ORIGINAL
, type
) < 0)
265 if (dump_counters(skb
, acct
, IP_CT_DIR_REPLY
, type
) < 0)
272 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
274 struct nlattr
*nest_count
;
275 const struct nf_conn_tstamp
*tstamp
;
277 tstamp
= nf_conn_tstamp_find(ct
);
281 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
| NLA_F_NESTED
);
283 goto nla_put_failure
;
285 if (nla_put_be64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
),
286 CTA_TIMESTAMP_PAD
) ||
287 (tstamp
->stop
!= 0 && nla_put_be64(skb
, CTA_TIMESTAMP_STOP
,
288 cpu_to_be64(tstamp
->stop
),
290 goto nla_put_failure
;
291 nla_nest_end(skb
, nest_count
);
299 #ifdef CONFIG_NF_CONNTRACK_MARK
300 static int ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
302 if (nla_put_be32(skb
, CTA_MARK
, htonl(ct
->mark
)))
303 goto nla_put_failure
;
310 #define ctnetlink_dump_mark(a, b) (0)
313 #ifdef CONFIG_NF_CONNTRACK_SECMARK
314 static int ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
316 struct nlattr
*nest_secctx
;
320 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
325 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
327 goto nla_put_failure
;
329 if (nla_put_string(skb
, CTA_SECCTX_NAME
, secctx
))
330 goto nla_put_failure
;
331 nla_nest_end(skb
, nest_secctx
);
335 security_release_secctx(secctx
, len
);
339 #define ctnetlink_dump_secctx(a, b) (0)
342 #ifdef CONFIG_NF_CONNTRACK_LABELS
343 static inline int ctnetlink_label_size(const struct nf_conn
*ct
)
345 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
349 return nla_total_size(sizeof(labels
->bits
));
353 ctnetlink_dump_labels(struct sk_buff
*skb
, const struct nf_conn
*ct
)
355 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
363 if (labels
->bits
[i
] != 0)
364 return nla_put(skb
, CTA_LABELS
, sizeof(labels
->bits
),
367 } while (i
< ARRAY_SIZE(labels
->bits
));
372 #define ctnetlink_dump_labels(a, b) (0)
373 #define ctnetlink_label_size(a) (0)
376 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
378 static int ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
380 struct nlattr
*nest_parms
;
382 if (!(ct
->status
& IPS_EXPECTED
))
385 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
387 goto nla_put_failure
;
388 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
389 goto nla_put_failure
;
390 nla_nest_end(skb
, nest_parms
);
399 dump_ct_seq_adj(struct sk_buff
*skb
, const struct nf_ct_seqadj
*seq
, int type
)
401 struct nlattr
*nest_parms
;
403 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
405 goto nla_put_failure
;
407 if (nla_put_be32(skb
, CTA_SEQADJ_CORRECTION_POS
,
408 htonl(seq
->correction_pos
)) ||
409 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_BEFORE
,
410 htonl(seq
->offset_before
)) ||
411 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_AFTER
,
412 htonl(seq
->offset_after
)))
413 goto nla_put_failure
;
415 nla_nest_end(skb
, nest_parms
);
423 static int ctnetlink_dump_ct_seq_adj(struct sk_buff
*skb
,
424 const struct nf_conn
*ct
)
426 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
427 struct nf_ct_seqadj
*seq
;
429 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !seqadj
)
432 seq
= &seqadj
->seq
[IP_CT_DIR_ORIGINAL
];
433 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_ORIG
) == -1)
436 seq
= &seqadj
->seq
[IP_CT_DIR_REPLY
];
437 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_REPLY
) == -1)
443 static int ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
445 if (nla_put_be32(skb
, CTA_ID
, htonl((unsigned long)ct
)))
446 goto nla_put_failure
;
453 static int ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
455 if (nla_put_be32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
))))
456 goto nla_put_failure
;
464 ctnetlink_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
467 const struct nf_conntrack_zone
*zone
;
468 struct nlmsghdr
*nlh
;
469 struct nfgenmsg
*nfmsg
;
470 struct nlattr
*nest_parms
;
471 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
473 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_NEW
);
474 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
478 nfmsg
= nlmsg_data(nlh
);
479 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
480 nfmsg
->version
= NFNETLINK_V0
;
483 zone
= nf_ct_zone(ct
);
485 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
487 goto nla_put_failure
;
488 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
489 goto nla_put_failure
;
490 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
491 NF_CT_ZONE_DIR_ORIG
) < 0)
492 goto nla_put_failure
;
493 nla_nest_end(skb
, nest_parms
);
495 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
497 goto nla_put_failure
;
498 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
499 goto nla_put_failure
;
500 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
501 NF_CT_ZONE_DIR_REPL
) < 0)
502 goto nla_put_failure
;
503 nla_nest_end(skb
, nest_parms
);
505 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
506 NF_CT_DEFAULT_ZONE_DIR
) < 0)
507 goto nla_put_failure
;
509 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
510 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
511 ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
512 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
513 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
514 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
515 ctnetlink_dump_mark(skb
, ct
) < 0 ||
516 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
517 ctnetlink_dump_labels(skb
, ct
) < 0 ||
518 ctnetlink_dump_id(skb
, ct
) < 0 ||
519 ctnetlink_dump_use(skb
, ct
) < 0 ||
520 ctnetlink_dump_master(skb
, ct
) < 0 ||
521 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
522 goto nla_put_failure
;
529 nlmsg_cancel(skb
, nlh
);
533 static inline size_t ctnetlink_proto_size(const struct nf_conn
*ct
)
535 struct nf_conntrack_l3proto
*l3proto
;
536 struct nf_conntrack_l4proto
*l4proto
;
540 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
541 len
+= l3proto
->nla_size
;
543 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
544 len
+= l4proto
->nla_size
;
550 static inline size_t ctnetlink_acct_size(const struct nf_conn
*ct
)
552 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
554 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
555 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
556 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
560 static inline int ctnetlink_secctx_size(const struct nf_conn
*ct
)
562 #ifdef CONFIG_NF_CONNTRACK_SECMARK
565 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
569 return nla_total_size(0) /* CTA_SECCTX */
570 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
576 static inline size_t ctnetlink_timestamp_size(const struct nf_conn
*ct
)
578 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
579 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
581 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
587 #ifdef CONFIG_NF_CONNTRACK_EVENTS
588 static size_t ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
590 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
591 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
592 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
593 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
594 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
595 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
596 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
597 + ctnetlink_acct_size(ct
)
598 + ctnetlink_timestamp_size(ct
)
599 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
600 + nla_total_size(0) /* CTA_PROTOINFO */
601 + nla_total_size(0) /* CTA_HELP */
602 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
603 + ctnetlink_secctx_size(ct
)
604 #ifdef CONFIG_NF_NAT_NEEDED
605 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
606 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
608 #ifdef CONFIG_NF_CONNTRACK_MARK
609 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
611 #ifdef CONFIG_NF_CONNTRACK_ZONES
612 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
614 + ctnetlink_proto_size(ct
)
615 + ctnetlink_label_size(ct
)
620 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
622 const struct nf_conntrack_zone
*zone
;
624 struct nlmsghdr
*nlh
;
625 struct nfgenmsg
*nfmsg
;
626 struct nlattr
*nest_parms
;
627 struct nf_conn
*ct
= item
->ct
;
630 unsigned int flags
= 0, group
;
633 /* ignore our fake conntrack entry */
634 if (nf_ct_is_untracked(ct
))
637 if (events
& (1 << IPCT_DESTROY
)) {
638 type
= IPCTNL_MSG_CT_DELETE
;
639 group
= NFNLGRP_CONNTRACK_DESTROY
;
640 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
641 type
= IPCTNL_MSG_CT_NEW
;
642 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
643 group
= NFNLGRP_CONNTRACK_NEW
;
645 type
= IPCTNL_MSG_CT_NEW
;
646 group
= NFNLGRP_CONNTRACK_UPDATE
;
651 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
654 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
658 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
659 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
663 nfmsg
= nlmsg_data(nlh
);
664 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
665 nfmsg
->version
= NFNETLINK_V0
;
669 zone
= nf_ct_zone(ct
);
671 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
673 goto nla_put_failure
;
674 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
675 goto nla_put_failure
;
676 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
677 NF_CT_ZONE_DIR_ORIG
) < 0)
678 goto nla_put_failure
;
679 nla_nest_end(skb
, nest_parms
);
681 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
683 goto nla_put_failure
;
684 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
685 goto nla_put_failure
;
686 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
687 NF_CT_ZONE_DIR_REPL
) < 0)
688 goto nla_put_failure
;
689 nla_nest_end(skb
, nest_parms
);
691 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
692 NF_CT_DEFAULT_ZONE_DIR
) < 0)
693 goto nla_put_failure
;
695 if (ctnetlink_dump_id(skb
, ct
) < 0)
696 goto nla_put_failure
;
698 if (ctnetlink_dump_status(skb
, ct
) < 0)
699 goto nla_put_failure
;
701 if (events
& (1 << IPCT_DESTROY
)) {
702 if (ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
703 ctnetlink_dump_timestamp(skb
, ct
) < 0)
704 goto nla_put_failure
;
706 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
707 goto nla_put_failure
;
709 if (events
& (1 << IPCT_PROTOINFO
)
710 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
711 goto nla_put_failure
;
713 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
714 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
715 goto nla_put_failure
;
717 #ifdef CONFIG_NF_CONNTRACK_SECMARK
718 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
719 && ctnetlink_dump_secctx(skb
, ct
) < 0)
720 goto nla_put_failure
;
722 if (events
& (1 << IPCT_LABEL
) &&
723 ctnetlink_dump_labels(skb
, ct
) < 0)
724 goto nla_put_failure
;
726 if (events
& (1 << IPCT_RELATED
) &&
727 ctnetlink_dump_master(skb
, ct
) < 0)
728 goto nla_put_failure
;
730 if (events
& (1 << IPCT_SEQADJ
) &&
731 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
732 goto nla_put_failure
;
735 #ifdef CONFIG_NF_CONNTRACK_MARK
736 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
737 && ctnetlink_dump_mark(skb
, ct
) < 0)
738 goto nla_put_failure
;
743 err
= nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
,
745 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
752 nlmsg_cancel(skb
, nlh
);
756 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
761 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
763 static int ctnetlink_done(struct netlink_callback
*cb
)
766 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
771 struct ctnetlink_filter
{
778 static struct ctnetlink_filter
*
779 ctnetlink_alloc_filter(const struct nlattr
* const cda
[])
781 #ifdef CONFIG_NF_CONNTRACK_MARK
782 struct ctnetlink_filter
*filter
;
784 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
786 return ERR_PTR(-ENOMEM
);
788 filter
->mark
.val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
789 filter
->mark
.mask
= ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
793 return ERR_PTR(-EOPNOTSUPP
);
797 static int ctnetlink_filter_match(struct nf_conn
*ct
, void *data
)
799 struct ctnetlink_filter
*filter
= data
;
804 #ifdef CONFIG_NF_CONNTRACK_MARK
805 if ((ct
->mark
& filter
->mark
.mask
) == filter
->mark
.val
)
813 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
815 struct net
*net
= sock_net(skb
->sk
);
816 struct nf_conn
*ct
, *last
;
817 struct nf_conntrack_tuple_hash
*h
;
818 struct hlist_nulls_node
*n
;
819 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
820 u_int8_t l3proto
= nfmsg
->nfgen_family
;
824 last
= (struct nf_conn
*)cb
->args
[1];
827 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
829 lockp
= &nf_conntrack_locks
[cb
->args
[0] % CONNTRACK_LOCKS
];
830 nf_conntrack_lock(lockp
);
831 if (cb
->args
[0] >= nf_conntrack_htable_size
) {
835 hlist_nulls_for_each_entry(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
837 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
839 ct
= nf_ct_tuplehash_to_ctrack(h
);
840 if (!net_eq(net
, nf_ct_net(ct
)))
843 /* Dump entries of a given L3 protocol number.
844 * If it is not specified, ie. l3proto == 0,
845 * then dump everything. */
846 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
853 if (!ctnetlink_filter_match(ct
, cb
->data
))
858 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
860 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
864 nf_conntrack_get(&ct
->ct_general
);
865 cb
->args
[1] = (unsigned long)ct
;
884 static int ctnetlink_parse_tuple_ip(struct nlattr
*attr
,
885 struct nf_conntrack_tuple
*tuple
)
887 struct nlattr
*tb
[CTA_IP_MAX
+1];
888 struct nf_conntrack_l3proto
*l3proto
;
891 ret
= nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
896 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
898 if (likely(l3proto
->nlattr_to_tuple
)) {
899 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
900 l3proto
->nla_policy
);
902 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
910 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
911 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
914 static int ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
915 struct nf_conntrack_tuple
*tuple
)
917 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
918 struct nf_conntrack_l4proto
*l4proto
;
921 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
925 if (!tb
[CTA_PROTO_NUM
])
927 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
930 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
932 if (likely(l4proto
->nlattr_to_tuple
)) {
933 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
934 l4proto
->nla_policy
);
936 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
945 ctnetlink_parse_zone(const struct nlattr
*attr
,
946 struct nf_conntrack_zone
*zone
)
948 nf_ct_zone_init(zone
, NF_CT_DEFAULT_ZONE_ID
,
949 NF_CT_DEFAULT_ZONE_DIR
, 0);
950 #ifdef CONFIG_NF_CONNTRACK_ZONES
952 zone
->id
= ntohs(nla_get_be16(attr
));
961 ctnetlink_parse_tuple_zone(struct nlattr
*attr
, enum ctattr_type type
,
962 struct nf_conntrack_zone
*zone
)
966 if (zone
->id
!= NF_CT_DEFAULT_ZONE_ID
)
969 ret
= ctnetlink_parse_zone(attr
, zone
);
973 if (type
== CTA_TUPLE_REPLY
)
974 zone
->dir
= NF_CT_ZONE_DIR_REPL
;
976 zone
->dir
= NF_CT_ZONE_DIR_ORIG
;
981 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
982 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
983 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
984 [CTA_TUPLE_ZONE
] = { .type
= NLA_U16
},
988 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
989 struct nf_conntrack_tuple
*tuple
,
990 enum ctattr_type type
, u_int8_t l3num
,
991 struct nf_conntrack_zone
*zone
)
993 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
996 memset(tuple
, 0, sizeof(*tuple
));
998 err
= nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
1002 if (!tb
[CTA_TUPLE_IP
])
1005 tuple
->src
.l3num
= l3num
;
1007 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
1011 if (!tb
[CTA_TUPLE_PROTO
])
1014 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
1018 if (tb
[CTA_TUPLE_ZONE
]) {
1022 err
= ctnetlink_parse_tuple_zone(tb
[CTA_TUPLE_ZONE
],
1028 /* orig and expect tuples get DIR_ORIGINAL */
1029 if (type
== CTA_TUPLE_REPLY
)
1030 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
1032 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
1037 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
1038 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
1039 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1042 static int ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
,
1043 struct nlattr
**helpinfo
)
1046 struct nlattr
*tb
[CTA_HELP_MAX
+1];
1048 err
= nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
1052 if (!tb
[CTA_HELP_NAME
])
1055 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
1057 if (tb
[CTA_HELP_INFO
])
1058 *helpinfo
= tb
[CTA_HELP_INFO
];
1063 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
1064 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
1065 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
1066 [CTA_STATUS
] = { .type
= NLA_U32
},
1067 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
1068 [CTA_HELP
] = { .type
= NLA_NESTED
},
1069 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
1070 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
1071 [CTA_MARK
] = { .type
= NLA_U32
},
1072 [CTA_ID
] = { .type
= NLA_U32
},
1073 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
1074 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
1075 [CTA_NAT_SEQ_ADJ_ORIG
] = { .type
= NLA_NESTED
},
1076 [CTA_NAT_SEQ_ADJ_REPLY
] = { .type
= NLA_NESTED
},
1077 [CTA_ZONE
] = { .type
= NLA_U16
},
1078 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
1079 [CTA_LABELS
] = { .type
= NLA_BINARY
,
1080 .len
= NF_CT_LABELS_MAX_SIZE
},
1081 [CTA_LABELS_MASK
] = { .type
= NLA_BINARY
,
1082 .len
= NF_CT_LABELS_MAX_SIZE
},
1085 static int ctnetlink_flush_conntrack(struct net
*net
,
1086 const struct nlattr
* const cda
[],
1087 u32 portid
, int report
)
1089 struct ctnetlink_filter
*filter
= NULL
;
1091 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1092 filter
= ctnetlink_alloc_filter(cda
);
1094 return PTR_ERR(filter
);
1097 nf_ct_iterate_cleanup(net
, ctnetlink_filter_match
, filter
,
1104 static int ctnetlink_del_conntrack(struct net
*net
, struct sock
*ctnl
,
1105 struct sk_buff
*skb
,
1106 const struct nlmsghdr
*nlh
,
1107 const struct nlattr
* const cda
[])
1109 struct nf_conntrack_tuple_hash
*h
;
1110 struct nf_conntrack_tuple tuple
;
1112 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1113 u_int8_t u3
= nfmsg
->nfgen_family
;
1114 struct nf_conntrack_zone zone
;
1117 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1121 if (cda
[CTA_TUPLE_ORIG
])
1122 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1124 else if (cda
[CTA_TUPLE_REPLY
])
1125 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1128 return ctnetlink_flush_conntrack(net
, cda
,
1129 NETLINK_CB(skb
).portid
,
1136 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1140 ct
= nf_ct_tuplehash_to_ctrack(h
);
1143 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
1144 if (id
!= (u32
)(unsigned long)ct
) {
1150 if (del_timer(&ct
->timeout
))
1151 nf_ct_delete(ct
, NETLINK_CB(skb
).portid
, nlmsg_report(nlh
));
1158 static int ctnetlink_get_conntrack(struct net
*net
, struct sock
*ctnl
,
1159 struct sk_buff
*skb
,
1160 const struct nlmsghdr
*nlh
,
1161 const struct nlattr
* const cda
[])
1163 struct nf_conntrack_tuple_hash
*h
;
1164 struct nf_conntrack_tuple tuple
;
1166 struct sk_buff
*skb2
= NULL
;
1167 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1168 u_int8_t u3
= nfmsg
->nfgen_family
;
1169 struct nf_conntrack_zone zone
;
1172 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1173 struct netlink_dump_control c
= {
1174 .dump
= ctnetlink_dump_table
,
1175 .done
= ctnetlink_done
,
1178 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1179 struct ctnetlink_filter
*filter
;
1181 filter
= ctnetlink_alloc_filter(cda
);
1183 return PTR_ERR(filter
);
1187 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1190 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1194 if (cda
[CTA_TUPLE_ORIG
])
1195 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1197 else if (cda
[CTA_TUPLE_REPLY
])
1198 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1206 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1210 ct
= nf_ct_tuplehash_to_ctrack(h
);
1213 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1220 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
1221 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1227 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
1236 /* this avoids a loop in nfnetlink. */
1237 return err
== -EAGAIN
? -ENOBUFS
: err
;
1240 static int ctnetlink_done_list(struct netlink_callback
*cb
)
1243 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
1248 ctnetlink_dump_list(struct sk_buff
*skb
, struct netlink_callback
*cb
, bool dying
)
1250 struct nf_conn
*ct
, *last
;
1251 struct nf_conntrack_tuple_hash
*h
;
1252 struct hlist_nulls_node
*n
;
1253 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1254 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1257 struct hlist_nulls_head
*list
;
1258 struct net
*net
= sock_net(skb
->sk
);
1263 last
= (struct nf_conn
*)cb
->args
[1];
1265 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
1266 struct ct_pcpu
*pcpu
;
1268 if (!cpu_possible(cpu
))
1271 pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1272 spin_lock_bh(&pcpu
->lock
);
1273 list
= dying
? &pcpu
->dying
: &pcpu
->unconfirmed
;
1275 hlist_nulls_for_each_entry(h
, n
, list
, hnnode
) {
1276 ct
= nf_ct_tuplehash_to_ctrack(h
);
1277 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
1285 res
= ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1287 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
1291 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
1294 cb
->args
[1] = (unsigned long)ct
;
1295 spin_unlock_bh(&pcpu
->lock
);
1303 spin_unlock_bh(&pcpu
->lock
);
1314 ctnetlink_dump_dying(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1316 return ctnetlink_dump_list(skb
, cb
, true);
1319 static int ctnetlink_get_ct_dying(struct net
*net
, struct sock
*ctnl
,
1320 struct sk_buff
*skb
,
1321 const struct nlmsghdr
*nlh
,
1322 const struct nlattr
* const cda
[])
1324 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1325 struct netlink_dump_control c
= {
1326 .dump
= ctnetlink_dump_dying
,
1327 .done
= ctnetlink_done_list
,
1329 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1336 ctnetlink_dump_unconfirmed(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1338 return ctnetlink_dump_list(skb
, cb
, false);
1341 static int ctnetlink_get_ct_unconfirmed(struct net
*net
, struct sock
*ctnl
,
1342 struct sk_buff
*skb
,
1343 const struct nlmsghdr
*nlh
,
1344 const struct nlattr
* const cda
[])
1346 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1347 struct netlink_dump_control c
= {
1348 .dump
= ctnetlink_dump_unconfirmed
,
1349 .done
= ctnetlink_done_list
,
1351 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1357 #ifdef CONFIG_NF_NAT_NEEDED
1359 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1360 enum nf_nat_manip_type manip
,
1361 const struct nlattr
*attr
)
1363 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1366 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1367 if (!parse_nat_setup
) {
1368 #ifdef CONFIG_MODULES
1370 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1371 if (request_module("nf-nat") < 0) {
1372 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1376 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1378 if (nfnetlink_parse_nat_setup_hook
)
1384 err
= parse_nat_setup(ct
, manip
, attr
);
1385 if (err
== -EAGAIN
) {
1386 #ifdef CONFIG_MODULES
1388 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1389 if (request_module("nf-nat-%u", nf_ct_l3num(ct
)) < 0) {
1390 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1394 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1405 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1408 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1409 d
= ct
->status
^ status
;
1411 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1415 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1416 /* SEEN_REPLY bit can only be set */
1419 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1420 /* ASSURED bit can only be set */
1423 /* Be careful here, modifying NAT bits can screw up things,
1424 * so don't let users modify them directly if they don't pass
1426 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1431 ctnetlink_setup_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1433 #ifdef CONFIG_NF_NAT_NEEDED
1436 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1439 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_DST
,
1444 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_SRC
,
1448 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1454 static int ctnetlink_change_helper(struct nf_conn
*ct
,
1455 const struct nlattr
* const cda
[])
1457 struct nf_conntrack_helper
*helper
;
1458 struct nf_conn_help
*help
= nfct_help(ct
);
1459 char *helpname
= NULL
;
1460 struct nlattr
*helpinfo
= NULL
;
1463 /* don't change helper of sibling connections */
1467 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1471 if (!strcmp(helpname
, "")) {
1472 if (help
&& help
->helper
) {
1473 /* we had a helper before ... */
1474 nf_ct_remove_expectations(ct
);
1475 RCU_INIT_POINTER(help
->helper
, NULL
);
1481 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1482 nf_ct_protonum(ct
));
1483 if (helper
== NULL
) {
1484 #ifdef CONFIG_MODULES
1485 spin_unlock_bh(&nf_conntrack_expect_lock
);
1487 if (request_module("nfct-helper-%s", helpname
) < 0) {
1488 spin_lock_bh(&nf_conntrack_expect_lock
);
1492 spin_lock_bh(&nf_conntrack_expect_lock
);
1493 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1494 nf_ct_protonum(ct
));
1502 if (help
->helper
== helper
) {
1503 /* update private helper data if allowed. */
1504 if (helper
->from_nlattr
)
1505 helper
->from_nlattr(helpinfo
, ct
);
1511 /* we cannot set a helper for an existing conntrack */
1515 static int ctnetlink_change_timeout(struct nf_conn
*ct
,
1516 const struct nlattr
* const cda
[])
1518 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1520 if (!del_timer(&ct
->timeout
))
1523 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1524 add_timer(&ct
->timeout
);
1529 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1530 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1531 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1532 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1535 static int ctnetlink_change_protoinfo(struct nf_conn
*ct
,
1536 const struct nlattr
* const cda
[])
1538 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1539 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1540 struct nf_conntrack_l4proto
*l4proto
;
1543 err
= nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1548 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1549 if (l4proto
->from_nlattr
)
1550 err
= l4proto
->from_nlattr(tb
, ct
);
1556 static const struct nla_policy seqadj_policy
[CTA_SEQADJ_MAX
+1] = {
1557 [CTA_SEQADJ_CORRECTION_POS
] = { .type
= NLA_U32
},
1558 [CTA_SEQADJ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1559 [CTA_SEQADJ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1562 static int change_seq_adj(struct nf_ct_seqadj
*seq
,
1563 const struct nlattr
* const attr
)
1566 struct nlattr
*cda
[CTA_SEQADJ_MAX
+1];
1568 err
= nla_parse_nested(cda
, CTA_SEQADJ_MAX
, attr
, seqadj_policy
);
1572 if (!cda
[CTA_SEQADJ_CORRECTION_POS
])
1575 seq
->correction_pos
=
1576 ntohl(nla_get_be32(cda
[CTA_SEQADJ_CORRECTION_POS
]));
1578 if (!cda
[CTA_SEQADJ_OFFSET_BEFORE
])
1581 seq
->offset_before
=
1582 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_BEFORE
]));
1584 if (!cda
[CTA_SEQADJ_OFFSET_AFTER
])
1588 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_AFTER
]));
1594 ctnetlink_change_seq_adj(struct nf_conn
*ct
,
1595 const struct nlattr
* const cda
[])
1597 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
1603 if (cda
[CTA_SEQ_ADJ_ORIG
]) {
1604 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_ORIGINAL
],
1605 cda
[CTA_SEQ_ADJ_ORIG
]);
1609 ct
->status
|= IPS_SEQ_ADJUST
;
1612 if (cda
[CTA_SEQ_ADJ_REPLY
]) {
1613 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_REPLY
],
1614 cda
[CTA_SEQ_ADJ_REPLY
]);
1618 ct
->status
|= IPS_SEQ_ADJUST
;
1625 ctnetlink_attach_labels(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1627 #ifdef CONFIG_NF_CONNTRACK_LABELS
1628 size_t len
= nla_len(cda
[CTA_LABELS
]);
1629 const void *mask
= cda
[CTA_LABELS_MASK
];
1631 if (len
& (sizeof(u32
)-1)) /* must be multiple of u32 */
1635 if (nla_len(cda
[CTA_LABELS_MASK
]) == 0 ||
1636 nla_len(cda
[CTA_LABELS_MASK
]) != len
)
1638 mask
= nla_data(cda
[CTA_LABELS_MASK
]);
1643 return nf_connlabels_replace(ct
, nla_data(cda
[CTA_LABELS
]), mask
, len
);
1650 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1651 const struct nlattr
* const cda
[])
1655 /* only allow NAT changes and master assignation for new conntracks */
1656 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1659 if (cda
[CTA_HELP
]) {
1660 err
= ctnetlink_change_helper(ct
, cda
);
1665 if (cda
[CTA_TIMEOUT
]) {
1666 err
= ctnetlink_change_timeout(ct
, cda
);
1671 if (cda
[CTA_STATUS
]) {
1672 err
= ctnetlink_change_status(ct
, cda
);
1677 if (cda
[CTA_PROTOINFO
]) {
1678 err
= ctnetlink_change_protoinfo(ct
, cda
);
1683 #if defined(CONFIG_NF_CONNTRACK_MARK)
1685 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1688 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1689 err
= ctnetlink_change_seq_adj(ct
, cda
);
1694 if (cda
[CTA_LABELS
]) {
1695 err
= ctnetlink_attach_labels(ct
, cda
);
1703 static struct nf_conn
*
1704 ctnetlink_create_conntrack(struct net
*net
,
1705 const struct nf_conntrack_zone
*zone
,
1706 const struct nlattr
* const cda
[],
1707 struct nf_conntrack_tuple
*otuple
,
1708 struct nf_conntrack_tuple
*rtuple
,
1713 struct nf_conntrack_helper
*helper
;
1714 struct nf_conn_tstamp
*tstamp
;
1716 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1718 return ERR_PTR(-ENOMEM
);
1720 if (!cda
[CTA_TIMEOUT
])
1722 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1724 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1727 if (cda
[CTA_HELP
]) {
1728 char *helpname
= NULL
;
1729 struct nlattr
*helpinfo
= NULL
;
1731 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1735 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1736 nf_ct_protonum(ct
));
1737 if (helper
== NULL
) {
1739 #ifdef CONFIG_MODULES
1740 if (request_module("nfct-helper-%s", helpname
) < 0) {
1746 helper
= __nf_conntrack_helper_find(helpname
,
1748 nf_ct_protonum(ct
));
1758 struct nf_conn_help
*help
;
1760 help
= nf_ct_helper_ext_add(ct
, helper
, GFP_ATOMIC
);
1765 /* set private helper data if allowed. */
1766 if (helper
->from_nlattr
)
1767 helper
->from_nlattr(helpinfo
, ct
);
1769 /* not in hash table yet so not strictly necessary */
1770 RCU_INIT_POINTER(help
->helper
, helper
);
1773 /* try an implicit helper assignation */
1774 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1779 err
= ctnetlink_setup_nat(ct
, cda
);
1783 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1784 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1785 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1786 nf_ct_labels_ext_add(ct
);
1788 /* we must add conntrack extensions before confirmation. */
1789 ct
->status
|= IPS_CONFIRMED
;
1791 if (cda
[CTA_STATUS
]) {
1792 err
= ctnetlink_change_status(ct
, cda
);
1797 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1798 err
= ctnetlink_change_seq_adj(ct
, cda
);
1803 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1804 if (cda
[CTA_PROTOINFO
]) {
1805 err
= ctnetlink_change_protoinfo(ct
, cda
);
1810 #if defined(CONFIG_NF_CONNTRACK_MARK)
1812 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1815 /* setup master conntrack: this is a confirmed expectation */
1816 if (cda
[CTA_TUPLE_MASTER
]) {
1817 struct nf_conntrack_tuple master
;
1818 struct nf_conntrack_tuple_hash
*master_h
;
1819 struct nf_conn
*master_ct
;
1821 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
,
1826 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1827 if (master_h
== NULL
) {
1831 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1832 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1833 ct
->master
= master_ct
;
1835 tstamp
= nf_conn_tstamp_find(ct
);
1837 tstamp
->start
= ktime_get_real_ns();
1839 err
= nf_conntrack_hash_check_insert(ct
);
1850 nf_conntrack_free(ct
);
1851 return ERR_PTR(err
);
1854 static int ctnetlink_new_conntrack(struct net
*net
, struct sock
*ctnl
,
1855 struct sk_buff
*skb
,
1856 const struct nlmsghdr
*nlh
,
1857 const struct nlattr
* const cda
[])
1859 struct nf_conntrack_tuple otuple
, rtuple
;
1860 struct nf_conntrack_tuple_hash
*h
= NULL
;
1861 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1863 u_int8_t u3
= nfmsg
->nfgen_family
;
1864 struct nf_conntrack_zone zone
;
1867 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1871 if (cda
[CTA_TUPLE_ORIG
]) {
1872 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
,
1878 if (cda
[CTA_TUPLE_REPLY
]) {
1879 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
,
1885 if (cda
[CTA_TUPLE_ORIG
])
1886 h
= nf_conntrack_find_get(net
, &zone
, &otuple
);
1887 else if (cda
[CTA_TUPLE_REPLY
])
1888 h
= nf_conntrack_find_get(net
, &zone
, &rtuple
);
1892 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1893 enum ip_conntrack_events events
;
1895 if (!cda
[CTA_TUPLE_ORIG
] || !cda
[CTA_TUPLE_REPLY
])
1898 ct
= ctnetlink_create_conntrack(net
, &zone
, cda
, &otuple
,
1904 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1905 events
= IPCT_RELATED
;
1909 if (cda
[CTA_LABELS
] &&
1910 ctnetlink_attach_labels(ct
, cda
) == 0)
1911 events
|= (1 << IPCT_LABEL
);
1913 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1914 (1 << IPCT_ASSURED
) |
1915 (1 << IPCT_HELPER
) |
1916 (1 << IPCT_PROTOINFO
) |
1917 (1 << IPCT_SEQADJ
) |
1918 (1 << IPCT_MARK
) | events
,
1919 ct
, NETLINK_CB(skb
).portid
,
1926 /* implicit 'else' */
1929 ct
= nf_ct_tuplehash_to_ctrack(h
);
1930 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1931 spin_lock_bh(&nf_conntrack_expect_lock
);
1932 err
= ctnetlink_change_conntrack(ct
, cda
);
1933 spin_unlock_bh(&nf_conntrack_expect_lock
);
1935 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1936 (1 << IPCT_ASSURED
) |
1937 (1 << IPCT_HELPER
) |
1939 (1 << IPCT_PROTOINFO
) |
1940 (1 << IPCT_SEQADJ
) |
1942 ct
, NETLINK_CB(skb
).portid
,
1952 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1953 __u16 cpu
, const struct ip_conntrack_stat
*st
)
1955 struct nlmsghdr
*nlh
;
1956 struct nfgenmsg
*nfmsg
;
1957 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
1959 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS_CPU
);
1960 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
1964 nfmsg
= nlmsg_data(nlh
);
1965 nfmsg
->nfgen_family
= AF_UNSPEC
;
1966 nfmsg
->version
= NFNETLINK_V0
;
1967 nfmsg
->res_id
= htons(cpu
);
1969 if (nla_put_be32(skb
, CTA_STATS_SEARCHED
, htonl(st
->searched
)) ||
1970 nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
1971 nla_put_be32(skb
, CTA_STATS_NEW
, htonl(st
->new)) ||
1972 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
1973 nla_put_be32(skb
, CTA_STATS_IGNORE
, htonl(st
->ignore
)) ||
1974 nla_put_be32(skb
, CTA_STATS_DELETE
, htonl(st
->delete)) ||
1975 nla_put_be32(skb
, CTA_STATS_DELETE_LIST
, htonl(st
->delete_list
)) ||
1976 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
1977 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
1978 htonl(st
->insert_failed
)) ||
1979 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
1980 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
1981 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
1982 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
1983 htonl(st
->search_restart
)))
1984 goto nla_put_failure
;
1986 nlmsg_end(skb
, nlh
);
1991 nlmsg_cancel(skb
, nlh
);
1996 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1999 struct net
*net
= sock_net(skb
->sk
);
2001 if (cb
->args
[0] == nr_cpu_ids
)
2004 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2005 const struct ip_conntrack_stat
*st
;
2007 if (!cpu_possible(cpu
))
2010 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2011 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
2012 NETLINK_CB(cb
->skb
).portid
,
2022 static int ctnetlink_stat_ct_cpu(struct net
*net
, struct sock
*ctnl
,
2023 struct sk_buff
*skb
,
2024 const struct nlmsghdr
*nlh
,
2025 const struct nlattr
* const cda
[])
2027 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2028 struct netlink_dump_control c
= {
2029 .dump
= ctnetlink_ct_stat_cpu_dump
,
2031 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2038 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
2041 struct nlmsghdr
*nlh
;
2042 struct nfgenmsg
*nfmsg
;
2043 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2044 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
2046 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS
);
2047 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2051 nfmsg
= nlmsg_data(nlh
);
2052 nfmsg
->nfgen_family
= AF_UNSPEC
;
2053 nfmsg
->version
= NFNETLINK_V0
;
2056 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
2057 goto nla_put_failure
;
2059 nlmsg_end(skb
, nlh
);
2064 nlmsg_cancel(skb
, nlh
);
2068 static int ctnetlink_stat_ct(struct net
*net
, struct sock
*ctnl
,
2069 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2070 const struct nlattr
* const cda
[])
2072 struct sk_buff
*skb2
;
2075 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2079 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2081 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
2086 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2095 /* this avoids a loop in nfnetlink. */
2096 return err
== -EAGAIN
? -ENOBUFS
: err
;
2099 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2100 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2101 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2102 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2103 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2104 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2105 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
2106 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
2107 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2108 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2109 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2110 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2111 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2114 static struct nf_conntrack_expect
*
2115 ctnetlink_alloc_expect(const struct nlattr
*const cda
[], struct nf_conn
*ct
,
2116 struct nf_conntrack_helper
*helper
,
2117 struct nf_conntrack_tuple
*tuple
,
2118 struct nf_conntrack_tuple
*mask
);
2120 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2122 ctnetlink_glue_build_size(const struct nf_conn
*ct
)
2124 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2125 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2126 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2127 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
2128 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
2129 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
2130 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
2131 + nla_total_size(0) /* CTA_PROTOINFO */
2132 + nla_total_size(0) /* CTA_HELP */
2133 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
2134 + ctnetlink_secctx_size(ct
)
2135 #ifdef CONFIG_NF_NAT_NEEDED
2136 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2137 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
2139 #ifdef CONFIG_NF_CONNTRACK_MARK
2140 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
2142 #ifdef CONFIG_NF_CONNTRACK_ZONES
2143 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2145 + ctnetlink_proto_size(ct
)
2149 static struct nf_conn
*ctnetlink_glue_get_ct(const struct sk_buff
*skb
,
2150 enum ip_conntrack_info
*ctinfo
)
2154 ct
= nf_ct_get(skb
, ctinfo
);
2155 if (ct
&& nf_ct_is_untracked(ct
))
2161 static int __ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
2163 const struct nf_conntrack_zone
*zone
;
2164 struct nlattr
*nest_parms
;
2167 zone
= nf_ct_zone(ct
);
2169 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
2171 goto nla_put_failure
;
2172 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
2173 goto nla_put_failure
;
2174 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2175 NF_CT_ZONE_DIR_ORIG
) < 0)
2176 goto nla_put_failure
;
2177 nla_nest_end(skb
, nest_parms
);
2179 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
2181 goto nla_put_failure
;
2182 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
2183 goto nla_put_failure
;
2184 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2185 NF_CT_ZONE_DIR_REPL
) < 0)
2186 goto nla_put_failure
;
2187 nla_nest_end(skb
, nest_parms
);
2189 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
2190 NF_CT_DEFAULT_ZONE_DIR
) < 0)
2191 goto nla_put_failure
;
2193 if (ctnetlink_dump_id(skb
, ct
) < 0)
2194 goto nla_put_failure
;
2196 if (ctnetlink_dump_status(skb
, ct
) < 0)
2197 goto nla_put_failure
;
2199 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
2200 goto nla_put_failure
;
2202 if (ctnetlink_dump_protoinfo(skb
, ct
) < 0)
2203 goto nla_put_failure
;
2205 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
2206 goto nla_put_failure
;
2208 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2209 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
2210 goto nla_put_failure
;
2212 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
2213 goto nla_put_failure
;
2215 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
2216 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
2217 goto nla_put_failure
;
2219 #ifdef CONFIG_NF_CONNTRACK_MARK
2220 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
2221 goto nla_put_failure
;
2223 if (ctnetlink_dump_labels(skb
, ct
) < 0)
2224 goto nla_put_failure
;
2234 ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
,
2235 enum ip_conntrack_info ctinfo
,
2236 u_int16_t ct_attr
, u_int16_t ct_info_attr
)
2238 struct nlattr
*nest_parms
;
2240 nest_parms
= nla_nest_start(skb
, ct_attr
| NLA_F_NESTED
);
2242 goto nla_put_failure
;
2244 if (__ctnetlink_glue_build(skb
, ct
) < 0)
2245 goto nla_put_failure
;
2247 nla_nest_end(skb
, nest_parms
);
2249 if (nla_put_be32(skb
, ct_info_attr
, htonl(ctinfo
)))
2250 goto nla_put_failure
;
2259 ctnetlink_glue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
2263 if (cda
[CTA_TIMEOUT
]) {
2264 err
= ctnetlink_change_timeout(ct
, cda
);
2268 if (cda
[CTA_STATUS
]) {
2269 err
= ctnetlink_change_status(ct
, cda
);
2273 if (cda
[CTA_HELP
]) {
2274 err
= ctnetlink_change_helper(ct
, cda
);
2278 if (cda
[CTA_LABELS
]) {
2279 err
= ctnetlink_attach_labels(ct
, cda
);
2283 #if defined(CONFIG_NF_CONNTRACK_MARK)
2284 if (cda
[CTA_MARK
]) {
2285 u32 mask
= 0, mark
, newmark
;
2286 if (cda
[CTA_MARK_MASK
])
2287 mask
= ~ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
2289 mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
2290 newmark
= (ct
->mark
& mask
) ^ mark
;
2291 if (newmark
!= ct
->mark
)
2299 ctnetlink_glue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
2301 struct nlattr
*cda
[CTA_MAX
+1];
2304 ret
= nla_parse_nested(cda
, CTA_MAX
, attr
, ct_nla_policy
);
2308 spin_lock_bh(&nf_conntrack_expect_lock
);
2309 ret
= ctnetlink_glue_parse_ct((const struct nlattr
**)cda
, ct
);
2310 spin_unlock_bh(&nf_conntrack_expect_lock
);
2315 static int ctnetlink_glue_exp_parse(const struct nlattr
* const *cda
,
2316 const struct nf_conn
*ct
,
2317 struct nf_conntrack_tuple
*tuple
,
2318 struct nf_conntrack_tuple
*mask
)
2322 err
= ctnetlink_parse_tuple(cda
, tuple
, CTA_EXPECT_TUPLE
,
2323 nf_ct_l3num(ct
), NULL
);
2327 return ctnetlink_parse_tuple(cda
, mask
, CTA_EXPECT_MASK
,
2328 nf_ct_l3num(ct
), NULL
);
2332 ctnetlink_glue_attach_expect(const struct nlattr
*attr
, struct nf_conn
*ct
,
2333 u32 portid
, u32 report
)
2335 struct nlattr
*cda
[CTA_EXPECT_MAX
+1];
2336 struct nf_conntrack_tuple tuple
, mask
;
2337 struct nf_conntrack_helper
*helper
= NULL
;
2338 struct nf_conntrack_expect
*exp
;
2341 err
= nla_parse_nested(cda
, CTA_EXPECT_MAX
, attr
, exp_nla_policy
);
2345 err
= ctnetlink_glue_exp_parse((const struct nlattr
* const *)cda
,
2350 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2351 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2353 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2354 nf_ct_protonum(ct
));
2359 exp
= ctnetlink_alloc_expect((const struct nlattr
* const *)cda
, ct
,
2360 helper
, &tuple
, &mask
);
2362 return PTR_ERR(exp
);
2364 err
= nf_ct_expect_related_report(exp
, portid
, report
);
2366 nf_ct_expect_put(exp
);
2373 static void ctnetlink_glue_seqadj(struct sk_buff
*skb
, struct nf_conn
*ct
,
2374 enum ip_conntrack_info ctinfo
, int diff
)
2376 if (!(ct
->status
& IPS_NAT_MASK
))
2379 nf_ct_tcp_seqadj_set(skb
, ct
, ctinfo
, diff
);
2382 static struct nfnl_ct_hook ctnetlink_glue_hook
= {
2383 .get_ct
= ctnetlink_glue_get_ct
,
2384 .build_size
= ctnetlink_glue_build_size
,
2385 .build
= ctnetlink_glue_build
,
2386 .parse
= ctnetlink_glue_parse
,
2387 .attach_expect
= ctnetlink_glue_attach_expect
,
2388 .seq_adjust
= ctnetlink_glue_seqadj
,
2390 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2392 /***********************************************************************
2394 ***********************************************************************/
2396 static int ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
2397 const struct nf_conntrack_tuple
*tuple
,
2398 enum ctattr_expect type
)
2400 struct nlattr
*nest_parms
;
2402 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
2404 goto nla_put_failure
;
2405 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
2406 goto nla_put_failure
;
2407 nla_nest_end(skb
, nest_parms
);
2415 static int ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
2416 const struct nf_conntrack_tuple
*tuple
,
2417 const struct nf_conntrack_tuple_mask
*mask
)
2420 struct nf_conntrack_l3proto
*l3proto
;
2421 struct nf_conntrack_l4proto
*l4proto
;
2422 struct nf_conntrack_tuple m
;
2423 struct nlattr
*nest_parms
;
2425 memset(&m
, 0xFF, sizeof(m
));
2426 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
2427 m
.src
.u
.all
= mask
->src
.u
.all
;
2428 m
.dst
.protonum
= tuple
->dst
.protonum
;
2430 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
2432 goto nla_put_failure
;
2435 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
2436 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
2438 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
2439 tuple
->dst
.protonum
);
2440 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
2444 if (unlikely(ret
< 0))
2445 goto nla_put_failure
;
2447 nla_nest_end(skb
, nest_parms
);
2455 static const union nf_inet_addr any_addr
;
2458 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
2459 const struct nf_conntrack_expect
*exp
)
2461 struct nf_conn
*master
= exp
->master
;
2462 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
2463 struct nf_conn_help
*help
;
2464 #ifdef CONFIG_NF_NAT_NEEDED
2465 struct nlattr
*nest_parms
;
2466 struct nf_conntrack_tuple nat_tuple
= {};
2468 struct nf_ct_helper_expectfn
*expfn
;
2473 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
2474 goto nla_put_failure
;
2475 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
2476 goto nla_put_failure
;
2477 if (ctnetlink_exp_dump_tuple(skb
,
2478 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
2479 CTA_EXPECT_MASTER
) < 0)
2480 goto nla_put_failure
;
2482 #ifdef CONFIG_NF_NAT_NEEDED
2483 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
2484 exp
->saved_proto
.all
) {
2485 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
2487 goto nla_put_failure
;
2489 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
2490 goto nla_put_failure
;
2492 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
2493 nat_tuple
.src
.u3
= exp
->saved_addr
;
2494 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
2495 nat_tuple
.src
.u
= exp
->saved_proto
;
2497 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
2498 CTA_EXPECT_NAT_TUPLE
) < 0)
2499 goto nla_put_failure
;
2500 nla_nest_end(skb
, nest_parms
);
2503 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
2504 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
2505 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
2506 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
2507 goto nla_put_failure
;
2508 help
= nfct_help(master
);
2510 struct nf_conntrack_helper
*helper
;
2512 helper
= rcu_dereference(help
->helper
);
2514 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
2515 goto nla_put_failure
;
2517 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
2518 if (expfn
!= NULL
&&
2519 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
2520 goto nla_put_failure
;
2529 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2530 int event
, const struct nf_conntrack_expect
*exp
)
2532 struct nlmsghdr
*nlh
;
2533 struct nfgenmsg
*nfmsg
;
2534 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
2536 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2537 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2541 nfmsg
= nlmsg_data(nlh
);
2542 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2543 nfmsg
->version
= NFNETLINK_V0
;
2546 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2547 goto nla_put_failure
;
2549 nlmsg_end(skb
, nlh
);
2554 nlmsg_cancel(skb
, nlh
);
2558 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2560 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
2562 struct nf_conntrack_expect
*exp
= item
->exp
;
2563 struct net
*net
= nf_ct_exp_net(exp
);
2564 struct nlmsghdr
*nlh
;
2565 struct nfgenmsg
*nfmsg
;
2566 struct sk_buff
*skb
;
2567 unsigned int type
, group
;
2570 if (events
& (1 << IPEXP_DESTROY
)) {
2571 type
= IPCTNL_MSG_EXP_DELETE
;
2572 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
2573 } else if (events
& (1 << IPEXP_NEW
)) {
2574 type
= IPCTNL_MSG_EXP_NEW
;
2575 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
2576 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
2580 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
2583 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
2587 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2588 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
2592 nfmsg
= nlmsg_data(nlh
);
2593 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2594 nfmsg
->version
= NFNETLINK_V0
;
2598 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2599 goto nla_put_failure
;
2602 nlmsg_end(skb
, nlh
);
2603 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
2608 nlmsg_cancel(skb
, nlh
);
2612 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
2616 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
2619 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
2624 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2626 struct net
*net
= sock_net(skb
->sk
);
2627 struct nf_conntrack_expect
*exp
, *last
;
2628 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2629 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2632 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2633 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
2635 hlist_for_each_entry(exp
, &nf_ct_expect_hash
[cb
->args
[0]],
2637 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2640 if (!net_eq(nf_ct_net(exp
->master
), net
))
2648 if (ctnetlink_exp_fill_info(skb
,
2649 NETLINK_CB(cb
->skb
).portid
,
2653 if (!atomic_inc_not_zero(&exp
->use
))
2655 cb
->args
[1] = (unsigned long)exp
;
2667 nf_ct_expect_put(last
);
2673 ctnetlink_exp_ct_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2675 struct nf_conntrack_expect
*exp
, *last
;
2676 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2677 struct nf_conn
*ct
= cb
->data
;
2678 struct nf_conn_help
*help
= nfct_help(ct
);
2679 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2685 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2687 hlist_for_each_entry(exp
, &help
->expectations
, lnode
) {
2688 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2695 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
2699 if (!atomic_inc_not_zero(&exp
->use
))
2701 cb
->args
[1] = (unsigned long)exp
;
2713 nf_ct_expect_put(last
);
2718 static int ctnetlink_dump_exp_ct(struct net
*net
, struct sock
*ctnl
,
2719 struct sk_buff
*skb
,
2720 const struct nlmsghdr
*nlh
,
2721 const struct nlattr
* const cda
[])
2724 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2725 u_int8_t u3
= nfmsg
->nfgen_family
;
2726 struct nf_conntrack_tuple tuple
;
2727 struct nf_conntrack_tuple_hash
*h
;
2729 struct nf_conntrack_zone zone
;
2730 struct netlink_dump_control c
= {
2731 .dump
= ctnetlink_exp_ct_dump_table
,
2732 .done
= ctnetlink_exp_done
,
2735 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2740 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2744 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
2748 ct
= nf_ct_tuplehash_to_ctrack(h
);
2751 err
= netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2757 static int ctnetlink_get_expect(struct net
*net
, struct sock
*ctnl
,
2758 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2759 const struct nlattr
* const cda
[])
2761 struct nf_conntrack_tuple tuple
;
2762 struct nf_conntrack_expect
*exp
;
2763 struct sk_buff
*skb2
;
2764 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2765 u_int8_t u3
= nfmsg
->nfgen_family
;
2766 struct nf_conntrack_zone zone
;
2769 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2770 if (cda
[CTA_EXPECT_MASTER
])
2771 return ctnetlink_dump_exp_ct(net
, ctnl
, skb
, nlh
, cda
);
2773 struct netlink_dump_control c
= {
2774 .dump
= ctnetlink_exp_dump_table
,
2775 .done
= ctnetlink_exp_done
,
2777 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2781 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2785 if (cda
[CTA_EXPECT_TUPLE
])
2786 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2788 else if (cda
[CTA_EXPECT_MASTER
])
2789 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2797 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2801 if (cda
[CTA_EXPECT_ID
]) {
2802 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2803 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2804 nf_ct_expect_put(exp
);
2810 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2812 nf_ct_expect_put(exp
);
2817 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2818 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
2820 nf_ct_expect_put(exp
);
2824 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2833 /* this avoids a loop in nfnetlink. */
2834 return err
== -EAGAIN
? -ENOBUFS
: err
;
2837 static int ctnetlink_del_expect(struct net
*net
, struct sock
*ctnl
,
2838 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2839 const struct nlattr
* const cda
[])
2841 struct nf_conntrack_expect
*exp
;
2842 struct nf_conntrack_tuple tuple
;
2843 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2844 struct hlist_node
*next
;
2845 u_int8_t u3
= nfmsg
->nfgen_family
;
2846 struct nf_conntrack_zone zone
;
2850 if (cda
[CTA_EXPECT_TUPLE
]) {
2851 /* delete a single expect by tuple */
2852 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2856 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2861 /* bump usage count to 2 */
2862 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2866 if (cda
[CTA_EXPECT_ID
]) {
2867 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2868 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2869 nf_ct_expect_put(exp
);
2874 /* after list removal, usage count == 1 */
2875 spin_lock_bh(&nf_conntrack_expect_lock
);
2876 if (del_timer(&exp
->timeout
)) {
2877 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
2879 nf_ct_expect_put(exp
);
2881 spin_unlock_bh(&nf_conntrack_expect_lock
);
2882 /* have to put what we 'get' above.
2883 * after this line usage count == 0 */
2884 nf_ct_expect_put(exp
);
2885 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2886 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2887 struct nf_conn_help
*m_help
;
2889 /* delete all expectations for this helper */
2890 spin_lock_bh(&nf_conntrack_expect_lock
);
2891 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2892 hlist_for_each_entry_safe(exp
, next
,
2893 &nf_ct_expect_hash
[i
],
2896 if (!net_eq(nf_ct_exp_net(exp
), net
))
2899 m_help
= nfct_help(exp
->master
);
2900 if (!strcmp(m_help
->helper
->name
, name
) &&
2901 del_timer(&exp
->timeout
)) {
2902 nf_ct_unlink_expect_report(exp
,
2903 NETLINK_CB(skb
).portid
,
2905 nf_ct_expect_put(exp
);
2909 spin_unlock_bh(&nf_conntrack_expect_lock
);
2911 /* This basically means we have to flush everything*/
2912 spin_lock_bh(&nf_conntrack_expect_lock
);
2913 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2914 hlist_for_each_entry_safe(exp
, next
,
2915 &nf_ct_expect_hash
[i
],
2918 if (!net_eq(nf_ct_exp_net(exp
), net
))
2921 if (del_timer(&exp
->timeout
)) {
2922 nf_ct_unlink_expect_report(exp
,
2923 NETLINK_CB(skb
).portid
,
2925 nf_ct_expect_put(exp
);
2929 spin_unlock_bh(&nf_conntrack_expect_lock
);
2935 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2936 const struct nlattr
* const cda
[])
2938 if (cda
[CTA_EXPECT_TIMEOUT
]) {
2939 if (!del_timer(&x
->timeout
))
2942 x
->timeout
.expires
= jiffies
+
2943 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2944 add_timer(&x
->timeout
);
2949 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2950 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2951 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2955 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2956 struct nf_conntrack_expect
*exp
,
2959 #ifdef CONFIG_NF_NAT_NEEDED
2960 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2961 struct nf_conntrack_tuple nat_tuple
= {};
2964 err
= nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2968 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2971 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2972 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
,
2977 exp
->saved_addr
= nat_tuple
.src
.u3
;
2978 exp
->saved_proto
= nat_tuple
.src
.u
;
2979 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2987 static struct nf_conntrack_expect
*
2988 ctnetlink_alloc_expect(const struct nlattr
* const cda
[], struct nf_conn
*ct
,
2989 struct nf_conntrack_helper
*helper
,
2990 struct nf_conntrack_tuple
*tuple
,
2991 struct nf_conntrack_tuple
*mask
)
2993 u_int32_t
class = 0;
2994 struct nf_conntrack_expect
*exp
;
2995 struct nf_conn_help
*help
;
2998 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2999 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
3000 if (class > helper
->expect_class_max
)
3001 return ERR_PTR(-EINVAL
);
3003 exp
= nf_ct_expect_alloc(ct
);
3005 return ERR_PTR(-ENOMEM
);
3007 help
= nfct_help(ct
);
3009 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
3013 exp
->timeout
.expires
=
3014 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
3016 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
3017 if (cda
[CTA_EXPECT_FLAGS
]) {
3019 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
3022 if (cda
[CTA_EXPECT_FLAGS
]) {
3023 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
3024 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
3028 if (cda
[CTA_EXPECT_FN
]) {
3029 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
3030 struct nf_ct_helper_expectfn
*expfn
;
3032 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
3033 if (expfn
== NULL
) {
3037 exp
->expectfn
= expfn
->expectfn
;
3039 exp
->expectfn
= NULL
;
3043 exp
->helper
= helper
;
3044 exp
->tuple
= *tuple
;
3045 exp
->mask
.src
.u3
= mask
->src
.u3
;
3046 exp
->mask
.src
.u
.all
= mask
->src
.u
.all
;
3048 if (cda
[CTA_EXPECT_NAT
]) {
3049 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
3050 exp
, nf_ct_l3num(ct
));
3056 nf_ct_expect_put(exp
);
3057 return ERR_PTR(err
);
3061 ctnetlink_create_expect(struct net
*net
,
3062 const struct nf_conntrack_zone
*zone
,
3063 const struct nlattr
* const cda
[],
3064 u_int8_t u3
, u32 portid
, int report
)
3066 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
3067 struct nf_conntrack_tuple_hash
*h
= NULL
;
3068 struct nf_conntrack_helper
*helper
= NULL
;
3069 struct nf_conntrack_expect
*exp
;
3073 /* caller guarantees that those three CTA_EXPECT_* exist */
3074 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3078 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
,
3082 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
,
3087 /* Look for master conntrack of this expectation */
3088 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
3091 ct
= nf_ct_tuplehash_to_ctrack(h
);
3093 if (cda
[CTA_EXPECT_HELP_NAME
]) {
3094 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3096 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3097 nf_ct_protonum(ct
));
3098 if (helper
== NULL
) {
3099 #ifdef CONFIG_MODULES
3100 if (request_module("nfct-helper-%s", helpname
) < 0) {
3104 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3105 nf_ct_protonum(ct
));
3116 exp
= ctnetlink_alloc_expect(cda
, ct
, helper
, &tuple
, &mask
);
3122 err
= nf_ct_expect_related_report(exp
, portid
, report
);
3123 nf_ct_expect_put(exp
);
3129 static int ctnetlink_new_expect(struct net
*net
, struct sock
*ctnl
,
3130 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3131 const struct nlattr
* const cda
[])
3133 struct nf_conntrack_tuple tuple
;
3134 struct nf_conntrack_expect
*exp
;
3135 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3136 u_int8_t u3
= nfmsg
->nfgen_family
;
3137 struct nf_conntrack_zone zone
;
3140 if (!cda
[CTA_EXPECT_TUPLE
]
3141 || !cda
[CTA_EXPECT_MASK
]
3142 || !cda
[CTA_EXPECT_MASTER
])
3145 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3149 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3154 spin_lock_bh(&nf_conntrack_expect_lock
);
3155 exp
= __nf_ct_expect_find(net
, &zone
, &tuple
);
3157 spin_unlock_bh(&nf_conntrack_expect_lock
);
3159 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
3160 err
= ctnetlink_create_expect(net
, &zone
, cda
, u3
,
3161 NETLINK_CB(skb
).portid
,
3168 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
3169 err
= ctnetlink_change_expect(exp
, cda
);
3170 spin_unlock_bh(&nf_conntrack_expect_lock
);
3176 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
3177 const struct ip_conntrack_stat
*st
)
3179 struct nlmsghdr
*nlh
;
3180 struct nfgenmsg
*nfmsg
;
3181 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
3183 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_EXP_GET_STATS_CPU
);
3184 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
3188 nfmsg
= nlmsg_data(nlh
);
3189 nfmsg
->nfgen_family
= AF_UNSPEC
;
3190 nfmsg
->version
= NFNETLINK_V0
;
3191 nfmsg
->res_id
= htons(cpu
);
3193 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
3194 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
3195 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
3196 goto nla_put_failure
;
3198 nlmsg_end(skb
, nlh
);
3203 nlmsg_cancel(skb
, nlh
);
3208 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3211 struct net
*net
= sock_net(skb
->sk
);
3213 if (cb
->args
[0] == nr_cpu_ids
)
3216 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
3217 const struct ip_conntrack_stat
*st
;
3219 if (!cpu_possible(cpu
))
3222 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
3223 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3233 static int ctnetlink_stat_exp_cpu(struct net
*net
, struct sock
*ctnl
,
3234 struct sk_buff
*skb
,
3235 const struct nlmsghdr
*nlh
,
3236 const struct nlattr
* const cda
[])
3238 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3239 struct netlink_dump_control c
= {
3240 .dump
= ctnetlink_exp_stat_cpu_dump
,
3242 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
3248 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3249 static struct nf_ct_event_notifier ctnl_notifier
= {
3250 .fcn
= ctnetlink_conntrack_event
,
3253 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
3254 .fcn
= ctnetlink_expect_event
,
3258 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
3259 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
3260 .attr_count
= CTA_MAX
,
3261 .policy
= ct_nla_policy
},
3262 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
3263 .attr_count
= CTA_MAX
,
3264 .policy
= ct_nla_policy
},
3265 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
3266 .attr_count
= CTA_MAX
,
3267 .policy
= ct_nla_policy
},
3268 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
3269 .attr_count
= CTA_MAX
,
3270 .policy
= ct_nla_policy
},
3271 [IPCTNL_MSG_CT_GET_STATS_CPU
] = { .call
= ctnetlink_stat_ct_cpu
},
3272 [IPCTNL_MSG_CT_GET_STATS
] = { .call
= ctnetlink_stat_ct
},
3273 [IPCTNL_MSG_CT_GET_DYING
] = { .call
= ctnetlink_get_ct_dying
},
3274 [IPCTNL_MSG_CT_GET_UNCONFIRMED
] = { .call
= ctnetlink_get_ct_unconfirmed
},
3277 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
3278 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
3279 .attr_count
= CTA_EXPECT_MAX
,
3280 .policy
= exp_nla_policy
},
3281 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
3282 .attr_count
= CTA_EXPECT_MAX
,
3283 .policy
= exp_nla_policy
},
3284 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
3285 .attr_count
= CTA_EXPECT_MAX
,
3286 .policy
= exp_nla_policy
},
3287 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = { .call
= ctnetlink_stat_exp_cpu
},
3290 static const struct nfnetlink_subsystem ctnl_subsys
= {
3291 .name
= "conntrack",
3292 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
3293 .cb_count
= IPCTNL_MSG_MAX
,
3297 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
3298 .name
= "conntrack_expect",
3299 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
3300 .cb_count
= IPCTNL_MSG_EXP_MAX
,
3304 MODULE_ALIAS("ip_conntrack_netlink");
3305 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
3306 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
3308 static int __net_init
ctnetlink_net_init(struct net
*net
)
3310 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3313 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
3315 pr_err("ctnetlink_init: cannot register notifier.\n");
3319 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
3321 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3322 goto err_unreg_notifier
;
3327 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3329 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3335 static void ctnetlink_net_exit(struct net
*net
)
3337 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3338 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
3339 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3343 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
3347 list_for_each_entry(net
, net_exit_list
, exit_list
)
3348 ctnetlink_net_exit(net
);
3351 static struct pernet_operations ctnetlink_net_ops
= {
3352 .init
= ctnetlink_net_init
,
3353 .exit_batch
= ctnetlink_net_exit_batch
,
3356 static int __init
ctnetlink_init(void)
3360 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
3361 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
3363 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3367 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
3369 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3370 goto err_unreg_subsys
;
3373 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
3375 pr_err("ctnetlink_init: cannot register pernet operations\n");
3376 goto err_unreg_exp_subsys
;
3378 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3379 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3380 RCU_INIT_POINTER(nfnl_ct_hook
, &ctnetlink_glue_hook
);
3384 err_unreg_exp_subsys
:
3385 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3387 nfnetlink_subsys_unregister(&ctnl_subsys
);
3392 static void __exit
ctnetlink_exit(void)
3394 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3396 unregister_pernet_subsys(&ctnetlink_net_ops
);
3397 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3398 nfnetlink_subsys_unregister(&ctnl_subsys
);
3399 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3400 RCU_INIT_POINTER(nfnl_ct_hook
, NULL
);
3404 module_init(ctnetlink_init
);
3405 module_exit(ctnetlink_exit
);