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
])
1897 if (otuple
.dst
.protonum
!= rtuple
.dst
.protonum
)
1900 ct
= ctnetlink_create_conntrack(net
, &zone
, cda
, &otuple
,
1906 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1907 events
= IPCT_RELATED
;
1911 if (cda
[CTA_LABELS
] &&
1912 ctnetlink_attach_labels(ct
, cda
) == 0)
1913 events
|= (1 << IPCT_LABEL
);
1915 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1916 (1 << IPCT_ASSURED
) |
1917 (1 << IPCT_HELPER
) |
1918 (1 << IPCT_PROTOINFO
) |
1919 (1 << IPCT_SEQADJ
) |
1920 (1 << IPCT_MARK
) | events
,
1921 ct
, NETLINK_CB(skb
).portid
,
1928 /* implicit 'else' */
1931 ct
= nf_ct_tuplehash_to_ctrack(h
);
1932 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1933 spin_lock_bh(&nf_conntrack_expect_lock
);
1934 err
= ctnetlink_change_conntrack(ct
, cda
);
1935 spin_unlock_bh(&nf_conntrack_expect_lock
);
1937 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1938 (1 << IPCT_ASSURED
) |
1939 (1 << IPCT_HELPER
) |
1941 (1 << IPCT_PROTOINFO
) |
1942 (1 << IPCT_SEQADJ
) |
1944 ct
, NETLINK_CB(skb
).portid
,
1954 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1955 __u16 cpu
, const struct ip_conntrack_stat
*st
)
1957 struct nlmsghdr
*nlh
;
1958 struct nfgenmsg
*nfmsg
;
1959 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
1961 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS_CPU
);
1962 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
1966 nfmsg
= nlmsg_data(nlh
);
1967 nfmsg
->nfgen_family
= AF_UNSPEC
;
1968 nfmsg
->version
= NFNETLINK_V0
;
1969 nfmsg
->res_id
= htons(cpu
);
1971 if (nla_put_be32(skb
, CTA_STATS_SEARCHED
, htonl(st
->searched
)) ||
1972 nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
1973 nla_put_be32(skb
, CTA_STATS_NEW
, htonl(st
->new)) ||
1974 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
1975 nla_put_be32(skb
, CTA_STATS_IGNORE
, htonl(st
->ignore
)) ||
1976 nla_put_be32(skb
, CTA_STATS_DELETE
, htonl(st
->delete)) ||
1977 nla_put_be32(skb
, CTA_STATS_DELETE_LIST
, htonl(st
->delete_list
)) ||
1978 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
1979 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
1980 htonl(st
->insert_failed
)) ||
1981 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
1982 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
1983 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
1984 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
1985 htonl(st
->search_restart
)))
1986 goto nla_put_failure
;
1988 nlmsg_end(skb
, nlh
);
1993 nlmsg_cancel(skb
, nlh
);
1998 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2001 struct net
*net
= sock_net(skb
->sk
);
2003 if (cb
->args
[0] == nr_cpu_ids
)
2006 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2007 const struct ip_conntrack_stat
*st
;
2009 if (!cpu_possible(cpu
))
2012 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2013 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
2014 NETLINK_CB(cb
->skb
).portid
,
2024 static int ctnetlink_stat_ct_cpu(struct net
*net
, struct sock
*ctnl
,
2025 struct sk_buff
*skb
,
2026 const struct nlmsghdr
*nlh
,
2027 const struct nlattr
* const cda
[])
2029 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2030 struct netlink_dump_control c
= {
2031 .dump
= ctnetlink_ct_stat_cpu_dump
,
2033 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2040 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
2043 struct nlmsghdr
*nlh
;
2044 struct nfgenmsg
*nfmsg
;
2045 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2046 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
2048 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS
);
2049 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2053 nfmsg
= nlmsg_data(nlh
);
2054 nfmsg
->nfgen_family
= AF_UNSPEC
;
2055 nfmsg
->version
= NFNETLINK_V0
;
2058 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
2059 goto nla_put_failure
;
2061 nlmsg_end(skb
, nlh
);
2066 nlmsg_cancel(skb
, nlh
);
2070 static int ctnetlink_stat_ct(struct net
*net
, struct sock
*ctnl
,
2071 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2072 const struct nlattr
* const cda
[])
2074 struct sk_buff
*skb2
;
2077 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2081 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2083 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
2088 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2097 /* this avoids a loop in nfnetlink. */
2098 return err
== -EAGAIN
? -ENOBUFS
: err
;
2101 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2102 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2103 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2104 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2105 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2106 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2107 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
2108 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
2109 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2110 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2111 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2112 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2113 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2116 static struct nf_conntrack_expect
*
2117 ctnetlink_alloc_expect(const struct nlattr
*const cda
[], struct nf_conn
*ct
,
2118 struct nf_conntrack_helper
*helper
,
2119 struct nf_conntrack_tuple
*tuple
,
2120 struct nf_conntrack_tuple
*mask
);
2122 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2124 ctnetlink_glue_build_size(const struct nf_conn
*ct
)
2126 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2127 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2128 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2129 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
2130 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
2131 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
2132 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
2133 + nla_total_size(0) /* CTA_PROTOINFO */
2134 + nla_total_size(0) /* CTA_HELP */
2135 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
2136 + ctnetlink_secctx_size(ct
)
2137 #ifdef CONFIG_NF_NAT_NEEDED
2138 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2139 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
2141 #ifdef CONFIG_NF_CONNTRACK_MARK
2142 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
2144 #ifdef CONFIG_NF_CONNTRACK_ZONES
2145 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2147 + ctnetlink_proto_size(ct
)
2151 static struct nf_conn
*ctnetlink_glue_get_ct(const struct sk_buff
*skb
,
2152 enum ip_conntrack_info
*ctinfo
)
2156 ct
= nf_ct_get(skb
, ctinfo
);
2157 if (ct
&& nf_ct_is_untracked(ct
))
2163 static int __ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
2165 const struct nf_conntrack_zone
*zone
;
2166 struct nlattr
*nest_parms
;
2169 zone
= nf_ct_zone(ct
);
2171 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
2173 goto nla_put_failure
;
2174 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
2175 goto nla_put_failure
;
2176 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2177 NF_CT_ZONE_DIR_ORIG
) < 0)
2178 goto nla_put_failure
;
2179 nla_nest_end(skb
, nest_parms
);
2181 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
2183 goto nla_put_failure
;
2184 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
2185 goto nla_put_failure
;
2186 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2187 NF_CT_ZONE_DIR_REPL
) < 0)
2188 goto nla_put_failure
;
2189 nla_nest_end(skb
, nest_parms
);
2191 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
2192 NF_CT_DEFAULT_ZONE_DIR
) < 0)
2193 goto nla_put_failure
;
2195 if (ctnetlink_dump_id(skb
, ct
) < 0)
2196 goto nla_put_failure
;
2198 if (ctnetlink_dump_status(skb
, ct
) < 0)
2199 goto nla_put_failure
;
2201 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
2202 goto nla_put_failure
;
2204 if (ctnetlink_dump_protoinfo(skb
, ct
) < 0)
2205 goto nla_put_failure
;
2207 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
2208 goto nla_put_failure
;
2210 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2211 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
2212 goto nla_put_failure
;
2214 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
2215 goto nla_put_failure
;
2217 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
2218 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
2219 goto nla_put_failure
;
2221 #ifdef CONFIG_NF_CONNTRACK_MARK
2222 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
2223 goto nla_put_failure
;
2225 if (ctnetlink_dump_labels(skb
, ct
) < 0)
2226 goto nla_put_failure
;
2236 ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
,
2237 enum ip_conntrack_info ctinfo
,
2238 u_int16_t ct_attr
, u_int16_t ct_info_attr
)
2240 struct nlattr
*nest_parms
;
2242 nest_parms
= nla_nest_start(skb
, ct_attr
| NLA_F_NESTED
);
2244 goto nla_put_failure
;
2246 if (__ctnetlink_glue_build(skb
, ct
) < 0)
2247 goto nla_put_failure
;
2249 nla_nest_end(skb
, nest_parms
);
2251 if (nla_put_be32(skb
, ct_info_attr
, htonl(ctinfo
)))
2252 goto nla_put_failure
;
2261 ctnetlink_glue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
2265 if (cda
[CTA_TIMEOUT
]) {
2266 err
= ctnetlink_change_timeout(ct
, cda
);
2270 if (cda
[CTA_STATUS
]) {
2271 err
= ctnetlink_change_status(ct
, cda
);
2275 if (cda
[CTA_HELP
]) {
2276 err
= ctnetlink_change_helper(ct
, cda
);
2280 if (cda
[CTA_LABELS
]) {
2281 err
= ctnetlink_attach_labels(ct
, cda
);
2285 #if defined(CONFIG_NF_CONNTRACK_MARK)
2286 if (cda
[CTA_MARK
]) {
2287 u32 mask
= 0, mark
, newmark
;
2288 if (cda
[CTA_MARK_MASK
])
2289 mask
= ~ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
2291 mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
2292 newmark
= (ct
->mark
& mask
) ^ mark
;
2293 if (newmark
!= ct
->mark
)
2301 ctnetlink_glue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
2303 struct nlattr
*cda
[CTA_MAX
+1];
2306 ret
= nla_parse_nested(cda
, CTA_MAX
, attr
, ct_nla_policy
);
2310 spin_lock_bh(&nf_conntrack_expect_lock
);
2311 ret
= ctnetlink_glue_parse_ct((const struct nlattr
**)cda
, ct
);
2312 spin_unlock_bh(&nf_conntrack_expect_lock
);
2317 static int ctnetlink_glue_exp_parse(const struct nlattr
* const *cda
,
2318 const struct nf_conn
*ct
,
2319 struct nf_conntrack_tuple
*tuple
,
2320 struct nf_conntrack_tuple
*mask
)
2324 err
= ctnetlink_parse_tuple(cda
, tuple
, CTA_EXPECT_TUPLE
,
2325 nf_ct_l3num(ct
), NULL
);
2329 return ctnetlink_parse_tuple(cda
, mask
, CTA_EXPECT_MASK
,
2330 nf_ct_l3num(ct
), NULL
);
2334 ctnetlink_glue_attach_expect(const struct nlattr
*attr
, struct nf_conn
*ct
,
2335 u32 portid
, u32 report
)
2337 struct nlattr
*cda
[CTA_EXPECT_MAX
+1];
2338 struct nf_conntrack_tuple tuple
, mask
;
2339 struct nf_conntrack_helper
*helper
= NULL
;
2340 struct nf_conntrack_expect
*exp
;
2343 err
= nla_parse_nested(cda
, CTA_EXPECT_MAX
, attr
, exp_nla_policy
);
2347 err
= ctnetlink_glue_exp_parse((const struct nlattr
* const *)cda
,
2352 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2353 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2355 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2356 nf_ct_protonum(ct
));
2361 exp
= ctnetlink_alloc_expect((const struct nlattr
* const *)cda
, ct
,
2362 helper
, &tuple
, &mask
);
2364 return PTR_ERR(exp
);
2366 err
= nf_ct_expect_related_report(exp
, portid
, report
);
2367 nf_ct_expect_put(exp
);
2371 static void ctnetlink_glue_seqadj(struct sk_buff
*skb
, struct nf_conn
*ct
,
2372 enum ip_conntrack_info ctinfo
, int diff
)
2374 if (!(ct
->status
& IPS_NAT_MASK
))
2377 nf_ct_tcp_seqadj_set(skb
, ct
, ctinfo
, diff
);
2380 static struct nfnl_ct_hook ctnetlink_glue_hook
= {
2381 .get_ct
= ctnetlink_glue_get_ct
,
2382 .build_size
= ctnetlink_glue_build_size
,
2383 .build
= ctnetlink_glue_build
,
2384 .parse
= ctnetlink_glue_parse
,
2385 .attach_expect
= ctnetlink_glue_attach_expect
,
2386 .seq_adjust
= ctnetlink_glue_seqadj
,
2388 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2390 /***********************************************************************
2392 ***********************************************************************/
2394 static int ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
2395 const struct nf_conntrack_tuple
*tuple
,
2396 enum ctattr_expect type
)
2398 struct nlattr
*nest_parms
;
2400 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
2402 goto nla_put_failure
;
2403 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
2404 goto nla_put_failure
;
2405 nla_nest_end(skb
, nest_parms
);
2413 static int ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
2414 const struct nf_conntrack_tuple
*tuple
,
2415 const struct nf_conntrack_tuple_mask
*mask
)
2418 struct nf_conntrack_l3proto
*l3proto
;
2419 struct nf_conntrack_l4proto
*l4proto
;
2420 struct nf_conntrack_tuple m
;
2421 struct nlattr
*nest_parms
;
2423 memset(&m
, 0xFF, sizeof(m
));
2424 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
2425 m
.src
.u
.all
= mask
->src
.u
.all
;
2426 m
.dst
.protonum
= tuple
->dst
.protonum
;
2428 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
2430 goto nla_put_failure
;
2433 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
2434 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
2436 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
2437 tuple
->dst
.protonum
);
2438 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
2442 if (unlikely(ret
< 0))
2443 goto nla_put_failure
;
2445 nla_nest_end(skb
, nest_parms
);
2453 static const union nf_inet_addr any_addr
;
2456 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
2457 const struct nf_conntrack_expect
*exp
)
2459 struct nf_conn
*master
= exp
->master
;
2460 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
2461 struct nf_conn_help
*help
;
2462 #ifdef CONFIG_NF_NAT_NEEDED
2463 struct nlattr
*nest_parms
;
2464 struct nf_conntrack_tuple nat_tuple
= {};
2466 struct nf_ct_helper_expectfn
*expfn
;
2471 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
2472 goto nla_put_failure
;
2473 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
2474 goto nla_put_failure
;
2475 if (ctnetlink_exp_dump_tuple(skb
,
2476 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
2477 CTA_EXPECT_MASTER
) < 0)
2478 goto nla_put_failure
;
2480 #ifdef CONFIG_NF_NAT_NEEDED
2481 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
2482 exp
->saved_proto
.all
) {
2483 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
2485 goto nla_put_failure
;
2487 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
2488 goto nla_put_failure
;
2490 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
2491 nat_tuple
.src
.u3
= exp
->saved_addr
;
2492 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
2493 nat_tuple
.src
.u
= exp
->saved_proto
;
2495 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
2496 CTA_EXPECT_NAT_TUPLE
) < 0)
2497 goto nla_put_failure
;
2498 nla_nest_end(skb
, nest_parms
);
2501 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
2502 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
2503 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
2504 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
2505 goto nla_put_failure
;
2506 help
= nfct_help(master
);
2508 struct nf_conntrack_helper
*helper
;
2510 helper
= rcu_dereference(help
->helper
);
2512 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
2513 goto nla_put_failure
;
2515 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
2516 if (expfn
!= NULL
&&
2517 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
2518 goto nla_put_failure
;
2527 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2528 int event
, const struct nf_conntrack_expect
*exp
)
2530 struct nlmsghdr
*nlh
;
2531 struct nfgenmsg
*nfmsg
;
2532 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
2534 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2535 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2539 nfmsg
= nlmsg_data(nlh
);
2540 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2541 nfmsg
->version
= NFNETLINK_V0
;
2544 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2545 goto nla_put_failure
;
2547 nlmsg_end(skb
, nlh
);
2552 nlmsg_cancel(skb
, nlh
);
2556 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2558 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
2560 struct nf_conntrack_expect
*exp
= item
->exp
;
2561 struct net
*net
= nf_ct_exp_net(exp
);
2562 struct nlmsghdr
*nlh
;
2563 struct nfgenmsg
*nfmsg
;
2564 struct sk_buff
*skb
;
2565 unsigned int type
, group
;
2568 if (events
& (1 << IPEXP_DESTROY
)) {
2569 type
= IPCTNL_MSG_EXP_DELETE
;
2570 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
2571 } else if (events
& (1 << IPEXP_NEW
)) {
2572 type
= IPCTNL_MSG_EXP_NEW
;
2573 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
2574 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
2578 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
2581 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
2585 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2586 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
2590 nfmsg
= nlmsg_data(nlh
);
2591 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2592 nfmsg
->version
= NFNETLINK_V0
;
2596 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2597 goto nla_put_failure
;
2600 nlmsg_end(skb
, nlh
);
2601 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
2606 nlmsg_cancel(skb
, nlh
);
2610 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
2614 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
2617 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
2622 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2624 struct net
*net
= sock_net(skb
->sk
);
2625 struct nf_conntrack_expect
*exp
, *last
;
2626 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2627 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2630 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2631 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
2633 hlist_for_each_entry(exp
, &nf_ct_expect_hash
[cb
->args
[0]],
2635 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2638 if (!net_eq(nf_ct_net(exp
->master
), net
))
2646 if (ctnetlink_exp_fill_info(skb
,
2647 NETLINK_CB(cb
->skb
).portid
,
2651 if (!atomic_inc_not_zero(&exp
->use
))
2653 cb
->args
[1] = (unsigned long)exp
;
2665 nf_ct_expect_put(last
);
2671 ctnetlink_exp_ct_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2673 struct nf_conntrack_expect
*exp
, *last
;
2674 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2675 struct nf_conn
*ct
= cb
->data
;
2676 struct nf_conn_help
*help
= nfct_help(ct
);
2677 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2683 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2685 hlist_for_each_entry(exp
, &help
->expectations
, lnode
) {
2686 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2693 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
2697 if (!atomic_inc_not_zero(&exp
->use
))
2699 cb
->args
[1] = (unsigned long)exp
;
2711 nf_ct_expect_put(last
);
2716 static int ctnetlink_dump_exp_ct(struct net
*net
, struct sock
*ctnl
,
2717 struct sk_buff
*skb
,
2718 const struct nlmsghdr
*nlh
,
2719 const struct nlattr
* const cda
[])
2722 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2723 u_int8_t u3
= nfmsg
->nfgen_family
;
2724 struct nf_conntrack_tuple tuple
;
2725 struct nf_conntrack_tuple_hash
*h
;
2727 struct nf_conntrack_zone zone
;
2728 struct netlink_dump_control c
= {
2729 .dump
= ctnetlink_exp_ct_dump_table
,
2730 .done
= ctnetlink_exp_done
,
2733 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2738 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2742 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
2746 ct
= nf_ct_tuplehash_to_ctrack(h
);
2749 err
= netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2755 static int ctnetlink_get_expect(struct net
*net
, struct sock
*ctnl
,
2756 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2757 const struct nlattr
* const cda
[])
2759 struct nf_conntrack_tuple tuple
;
2760 struct nf_conntrack_expect
*exp
;
2761 struct sk_buff
*skb2
;
2762 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2763 u_int8_t u3
= nfmsg
->nfgen_family
;
2764 struct nf_conntrack_zone zone
;
2767 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2768 if (cda
[CTA_EXPECT_MASTER
])
2769 return ctnetlink_dump_exp_ct(net
, ctnl
, skb
, nlh
, cda
);
2771 struct netlink_dump_control c
= {
2772 .dump
= ctnetlink_exp_dump_table
,
2773 .done
= ctnetlink_exp_done
,
2775 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2779 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2783 if (cda
[CTA_EXPECT_TUPLE
])
2784 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2786 else if (cda
[CTA_EXPECT_MASTER
])
2787 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2795 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2799 if (cda
[CTA_EXPECT_ID
]) {
2800 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2801 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2802 nf_ct_expect_put(exp
);
2808 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2810 nf_ct_expect_put(exp
);
2815 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2816 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
2818 nf_ct_expect_put(exp
);
2822 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2831 /* this avoids a loop in nfnetlink. */
2832 return err
== -EAGAIN
? -ENOBUFS
: err
;
2835 static int ctnetlink_del_expect(struct net
*net
, struct sock
*ctnl
,
2836 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2837 const struct nlattr
* const cda
[])
2839 struct nf_conntrack_expect
*exp
;
2840 struct nf_conntrack_tuple tuple
;
2841 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2842 struct hlist_node
*next
;
2843 u_int8_t u3
= nfmsg
->nfgen_family
;
2844 struct nf_conntrack_zone zone
;
2848 if (cda
[CTA_EXPECT_TUPLE
]) {
2849 /* delete a single expect by tuple */
2850 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2854 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2859 /* bump usage count to 2 */
2860 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2864 if (cda
[CTA_EXPECT_ID
]) {
2865 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2866 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2867 nf_ct_expect_put(exp
);
2872 /* after list removal, usage count == 1 */
2873 spin_lock_bh(&nf_conntrack_expect_lock
);
2874 if (del_timer(&exp
->timeout
)) {
2875 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
2877 nf_ct_expect_put(exp
);
2879 spin_unlock_bh(&nf_conntrack_expect_lock
);
2880 /* have to put what we 'get' above.
2881 * after this line usage count == 0 */
2882 nf_ct_expect_put(exp
);
2883 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2884 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2885 struct nf_conn_help
*m_help
;
2887 /* delete all expectations for this helper */
2888 spin_lock_bh(&nf_conntrack_expect_lock
);
2889 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2890 hlist_for_each_entry_safe(exp
, next
,
2891 &nf_ct_expect_hash
[i
],
2894 if (!net_eq(nf_ct_exp_net(exp
), net
))
2897 m_help
= nfct_help(exp
->master
);
2898 if (!strcmp(m_help
->helper
->name
, name
) &&
2899 del_timer(&exp
->timeout
)) {
2900 nf_ct_unlink_expect_report(exp
,
2901 NETLINK_CB(skb
).portid
,
2903 nf_ct_expect_put(exp
);
2907 spin_unlock_bh(&nf_conntrack_expect_lock
);
2909 /* This basically means we have to flush everything*/
2910 spin_lock_bh(&nf_conntrack_expect_lock
);
2911 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2912 hlist_for_each_entry_safe(exp
, next
,
2913 &nf_ct_expect_hash
[i
],
2916 if (!net_eq(nf_ct_exp_net(exp
), net
))
2919 if (del_timer(&exp
->timeout
)) {
2920 nf_ct_unlink_expect_report(exp
,
2921 NETLINK_CB(skb
).portid
,
2923 nf_ct_expect_put(exp
);
2927 spin_unlock_bh(&nf_conntrack_expect_lock
);
2933 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2934 const struct nlattr
* const cda
[])
2936 if (cda
[CTA_EXPECT_TIMEOUT
]) {
2937 if (!del_timer(&x
->timeout
))
2940 x
->timeout
.expires
= jiffies
+
2941 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2942 add_timer(&x
->timeout
);
2947 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2948 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2949 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2953 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2954 struct nf_conntrack_expect
*exp
,
2957 #ifdef CONFIG_NF_NAT_NEEDED
2958 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2959 struct nf_conntrack_tuple nat_tuple
= {};
2962 err
= nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2966 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2969 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2970 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
,
2975 exp
->saved_addr
= nat_tuple
.src
.u3
;
2976 exp
->saved_proto
= nat_tuple
.src
.u
;
2977 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2985 static struct nf_conntrack_expect
*
2986 ctnetlink_alloc_expect(const struct nlattr
* const cda
[], struct nf_conn
*ct
,
2987 struct nf_conntrack_helper
*helper
,
2988 struct nf_conntrack_tuple
*tuple
,
2989 struct nf_conntrack_tuple
*mask
)
2991 u_int32_t
class = 0;
2992 struct nf_conntrack_expect
*exp
;
2993 struct nf_conn_help
*help
;
2996 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2997 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
2998 if (class > helper
->expect_class_max
)
2999 return ERR_PTR(-EINVAL
);
3001 exp
= nf_ct_expect_alloc(ct
);
3003 return ERR_PTR(-ENOMEM
);
3005 help
= nfct_help(ct
);
3007 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
3011 exp
->timeout
.expires
=
3012 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
3014 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
3015 if (cda
[CTA_EXPECT_FLAGS
]) {
3017 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
3020 if (cda
[CTA_EXPECT_FLAGS
]) {
3021 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
3022 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
3026 if (cda
[CTA_EXPECT_FN
]) {
3027 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
3028 struct nf_ct_helper_expectfn
*expfn
;
3030 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
3031 if (expfn
== NULL
) {
3035 exp
->expectfn
= expfn
->expectfn
;
3037 exp
->expectfn
= NULL
;
3041 exp
->helper
= helper
;
3042 exp
->tuple
= *tuple
;
3043 exp
->mask
.src
.u3
= mask
->src
.u3
;
3044 exp
->mask
.src
.u
.all
= mask
->src
.u
.all
;
3046 if (cda
[CTA_EXPECT_NAT
]) {
3047 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
3048 exp
, nf_ct_l3num(ct
));
3054 nf_ct_expect_put(exp
);
3055 return ERR_PTR(err
);
3059 ctnetlink_create_expect(struct net
*net
,
3060 const struct nf_conntrack_zone
*zone
,
3061 const struct nlattr
* const cda
[],
3062 u_int8_t u3
, u32 portid
, int report
)
3064 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
3065 struct nf_conntrack_tuple_hash
*h
= NULL
;
3066 struct nf_conntrack_helper
*helper
= NULL
;
3067 struct nf_conntrack_expect
*exp
;
3071 /* caller guarantees that those three CTA_EXPECT_* exist */
3072 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3076 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
,
3080 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
,
3085 /* Look for master conntrack of this expectation */
3086 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
3089 ct
= nf_ct_tuplehash_to_ctrack(h
);
3091 if (cda
[CTA_EXPECT_HELP_NAME
]) {
3092 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3094 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3095 nf_ct_protonum(ct
));
3096 if (helper
== NULL
) {
3097 #ifdef CONFIG_MODULES
3098 if (request_module("nfct-helper-%s", helpname
) < 0) {
3102 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3103 nf_ct_protonum(ct
));
3114 exp
= ctnetlink_alloc_expect(cda
, ct
, helper
, &tuple
, &mask
);
3120 err
= nf_ct_expect_related_report(exp
, portid
, report
);
3121 nf_ct_expect_put(exp
);
3127 static int ctnetlink_new_expect(struct net
*net
, struct sock
*ctnl
,
3128 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3129 const struct nlattr
* const cda
[])
3131 struct nf_conntrack_tuple tuple
;
3132 struct nf_conntrack_expect
*exp
;
3133 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3134 u_int8_t u3
= nfmsg
->nfgen_family
;
3135 struct nf_conntrack_zone zone
;
3138 if (!cda
[CTA_EXPECT_TUPLE
]
3139 || !cda
[CTA_EXPECT_MASK
]
3140 || !cda
[CTA_EXPECT_MASTER
])
3143 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3147 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3152 spin_lock_bh(&nf_conntrack_expect_lock
);
3153 exp
= __nf_ct_expect_find(net
, &zone
, &tuple
);
3155 spin_unlock_bh(&nf_conntrack_expect_lock
);
3157 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
3158 err
= ctnetlink_create_expect(net
, &zone
, cda
, u3
,
3159 NETLINK_CB(skb
).portid
,
3166 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
3167 err
= ctnetlink_change_expect(exp
, cda
);
3168 spin_unlock_bh(&nf_conntrack_expect_lock
);
3174 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
3175 const struct ip_conntrack_stat
*st
)
3177 struct nlmsghdr
*nlh
;
3178 struct nfgenmsg
*nfmsg
;
3179 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
3181 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_EXP_GET_STATS_CPU
);
3182 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
3186 nfmsg
= nlmsg_data(nlh
);
3187 nfmsg
->nfgen_family
= AF_UNSPEC
;
3188 nfmsg
->version
= NFNETLINK_V0
;
3189 nfmsg
->res_id
= htons(cpu
);
3191 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
3192 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
3193 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
3194 goto nla_put_failure
;
3196 nlmsg_end(skb
, nlh
);
3201 nlmsg_cancel(skb
, nlh
);
3206 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3209 struct net
*net
= sock_net(skb
->sk
);
3211 if (cb
->args
[0] == nr_cpu_ids
)
3214 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
3215 const struct ip_conntrack_stat
*st
;
3217 if (!cpu_possible(cpu
))
3220 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
3221 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3231 static int ctnetlink_stat_exp_cpu(struct net
*net
, struct sock
*ctnl
,
3232 struct sk_buff
*skb
,
3233 const struct nlmsghdr
*nlh
,
3234 const struct nlattr
* const cda
[])
3236 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3237 struct netlink_dump_control c
= {
3238 .dump
= ctnetlink_exp_stat_cpu_dump
,
3240 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
3246 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3247 static struct nf_ct_event_notifier ctnl_notifier
= {
3248 .fcn
= ctnetlink_conntrack_event
,
3251 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
3252 .fcn
= ctnetlink_expect_event
,
3256 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
3257 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
3258 .attr_count
= CTA_MAX
,
3259 .policy
= ct_nla_policy
},
3260 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
3261 .attr_count
= CTA_MAX
,
3262 .policy
= ct_nla_policy
},
3263 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
3264 .attr_count
= CTA_MAX
,
3265 .policy
= ct_nla_policy
},
3266 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
3267 .attr_count
= CTA_MAX
,
3268 .policy
= ct_nla_policy
},
3269 [IPCTNL_MSG_CT_GET_STATS_CPU
] = { .call
= ctnetlink_stat_ct_cpu
},
3270 [IPCTNL_MSG_CT_GET_STATS
] = { .call
= ctnetlink_stat_ct
},
3271 [IPCTNL_MSG_CT_GET_DYING
] = { .call
= ctnetlink_get_ct_dying
},
3272 [IPCTNL_MSG_CT_GET_UNCONFIRMED
] = { .call
= ctnetlink_get_ct_unconfirmed
},
3275 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
3276 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
3277 .attr_count
= CTA_EXPECT_MAX
,
3278 .policy
= exp_nla_policy
},
3279 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
3280 .attr_count
= CTA_EXPECT_MAX
,
3281 .policy
= exp_nla_policy
},
3282 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
3283 .attr_count
= CTA_EXPECT_MAX
,
3284 .policy
= exp_nla_policy
},
3285 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = { .call
= ctnetlink_stat_exp_cpu
},
3288 static const struct nfnetlink_subsystem ctnl_subsys
= {
3289 .name
= "conntrack",
3290 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
3291 .cb_count
= IPCTNL_MSG_MAX
,
3295 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
3296 .name
= "conntrack_expect",
3297 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
3298 .cb_count
= IPCTNL_MSG_EXP_MAX
,
3302 MODULE_ALIAS("ip_conntrack_netlink");
3303 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
3304 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
3306 static int __net_init
ctnetlink_net_init(struct net
*net
)
3308 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3311 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
3313 pr_err("ctnetlink_init: cannot register notifier.\n");
3317 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
3319 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3320 goto err_unreg_notifier
;
3325 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3327 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3333 static void ctnetlink_net_exit(struct net
*net
)
3335 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3336 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
3337 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3341 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
3345 list_for_each_entry(net
, net_exit_list
, exit_list
)
3346 ctnetlink_net_exit(net
);
3349 static struct pernet_operations ctnetlink_net_ops
= {
3350 .init
= ctnetlink_net_init
,
3351 .exit_batch
= ctnetlink_net_exit_batch
,
3354 static int __init
ctnetlink_init(void)
3358 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
3359 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
3361 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3365 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
3367 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3368 goto err_unreg_subsys
;
3371 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
3373 pr_err("ctnetlink_init: cannot register pernet operations\n");
3374 goto err_unreg_exp_subsys
;
3376 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3377 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3378 RCU_INIT_POINTER(nfnl_ct_hook
, &ctnetlink_glue_hook
);
3382 err_unreg_exp_subsys
:
3383 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3385 nfnetlink_subsys_unregister(&ctnl_subsys
);
3390 static void __exit
ctnetlink_exit(void)
3392 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3394 unregister_pernet_subsys(&ctnetlink_net_ops
);
3395 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3396 nfnetlink_subsys_unregister(&ctnl_subsys
);
3397 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3398 RCU_INIT_POINTER(nfnl_ct_hook
, NULL
);
3402 module_init(ctnetlink_init
);
3403 module_exit(ctnetlink_exit
);