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";
62 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
63 const struct nf_conntrack_tuple
*tuple
,
64 struct nf_conntrack_l4proto
*l4proto
)
67 struct nlattr
*nest_parms
;
69 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
72 if (nla_put_u8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
))
75 if (likely(l4proto
->tuple_to_nlattr
))
76 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
78 nla_nest_end(skb
, nest_parms
);
87 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
88 const struct nf_conntrack_tuple
*tuple
,
89 struct nf_conntrack_l3proto
*l3proto
)
92 struct nlattr
*nest_parms
;
94 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
98 if (likely(l3proto
->tuple_to_nlattr
))
99 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
101 nla_nest_end(skb
, nest_parms
);
110 ctnetlink_dump_tuples(struct sk_buff
*skb
,
111 const struct nf_conntrack_tuple
*tuple
)
114 struct nf_conntrack_l3proto
*l3proto
;
115 struct nf_conntrack_l4proto
*l4proto
;
118 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
119 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
122 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
123 tuple
->dst
.protonum
);
124 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
131 ctnetlink_dump_zone_id(struct sk_buff
*skb
, int attrtype
,
132 const struct nf_conntrack_zone
*zone
, int dir
)
134 if (zone
->id
== NF_CT_DEFAULT_ZONE_ID
|| zone
->dir
!= dir
)
136 if (nla_put_be16(skb
, attrtype
, htons(zone
->id
)))
137 goto nla_put_failure
;
145 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
147 if (nla_put_be32(skb
, CTA_STATUS
, htonl(ct
->status
)))
148 goto nla_put_failure
;
156 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
158 long timeout
= ((long)ct
->timeout
.expires
- (long)jiffies
) / HZ
;
163 if (nla_put_be32(skb
, CTA_TIMEOUT
, htonl(timeout
)))
164 goto nla_put_failure
;
172 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
)
174 struct nf_conntrack_l4proto
*l4proto
;
175 struct nlattr
*nest_proto
;
178 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
179 if (!l4proto
->to_nlattr
)
182 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
184 goto nla_put_failure
;
186 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
188 nla_nest_end(skb
, nest_proto
);
197 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
199 struct nlattr
*nest_helper
;
200 const struct nf_conn_help
*help
= nfct_help(ct
);
201 struct nf_conntrack_helper
*helper
;
206 helper
= rcu_dereference(help
->helper
);
210 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
212 goto nla_put_failure
;
213 if (nla_put_string(skb
, CTA_HELP_NAME
, helper
->name
))
214 goto nla_put_failure
;
216 if (helper
->to_nlattr
)
217 helper
->to_nlattr(skb
, ct
);
219 nla_nest_end(skb
, nest_helper
);
228 dump_counters(struct sk_buff
*skb
, struct nf_conn_acct
*acct
,
229 enum ip_conntrack_dir dir
, int type
)
231 enum ctattr_type attr
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
232 struct nf_conn_counter
*counter
= acct
->counter
;
233 struct nlattr
*nest_count
;
236 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
237 pkts
= atomic64_xchg(&counter
[dir
].packets
, 0);
238 bytes
= atomic64_xchg(&counter
[dir
].bytes
, 0);
240 pkts
= atomic64_read(&counter
[dir
].packets
);
241 bytes
= atomic64_read(&counter
[dir
].bytes
);
244 nest_count
= nla_nest_start(skb
, attr
| NLA_F_NESTED
);
246 goto nla_put_failure
;
248 if (nla_put_be64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
)) ||
249 nla_put_be64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
)))
250 goto nla_put_failure
;
252 nla_nest_end(skb
, nest_count
);
261 ctnetlink_dump_acct(struct sk_buff
*skb
, const struct nf_conn
*ct
, int type
)
263 struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
268 if (dump_counters(skb
, acct
, IP_CT_DIR_ORIGINAL
, type
) < 0)
270 if (dump_counters(skb
, acct
, IP_CT_DIR_REPLY
, type
) < 0)
277 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
279 struct nlattr
*nest_count
;
280 const struct nf_conn_tstamp
*tstamp
;
282 tstamp
= nf_conn_tstamp_find(ct
);
286 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
| NLA_F_NESTED
);
288 goto nla_put_failure
;
290 if (nla_put_be64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
)) ||
291 (tstamp
->stop
!= 0 && nla_put_be64(skb
, CTA_TIMESTAMP_STOP
,
292 cpu_to_be64(tstamp
->stop
))))
293 goto nla_put_failure
;
294 nla_nest_end(skb
, nest_count
);
302 #ifdef CONFIG_NF_CONNTRACK_MARK
304 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
306 if (nla_put_be32(skb
, CTA_MARK
, htonl(ct
->mark
)))
307 goto nla_put_failure
;
314 #define ctnetlink_dump_mark(a, b) (0)
317 #ifdef CONFIG_NF_CONNTRACK_SECMARK
319 ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
321 struct nlattr
*nest_secctx
;
325 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
330 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
| NLA_F_NESTED
);
332 goto nla_put_failure
;
334 if (nla_put_string(skb
, CTA_SECCTX_NAME
, secctx
))
335 goto nla_put_failure
;
336 nla_nest_end(skb
, nest_secctx
);
340 security_release_secctx(secctx
, len
);
344 #define ctnetlink_dump_secctx(a, b) (0)
347 #ifdef CONFIG_NF_CONNTRACK_LABELS
348 static int ctnetlink_label_size(const struct nf_conn
*ct
)
350 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
354 return nla_total_size(labels
->words
* sizeof(long));
358 ctnetlink_dump_labels(struct sk_buff
*skb
, const struct nf_conn
*ct
)
360 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
366 len
= labels
->words
* sizeof(long);
369 if (labels
->bits
[i
] != 0)
370 return nla_put(skb
, CTA_LABELS
, len
, labels
->bits
);
372 } while (i
< labels
->words
);
377 #define ctnetlink_dump_labels(a, b) (0)
378 #define ctnetlink_label_size(a) (0)
381 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
384 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
386 struct nlattr
*nest_parms
;
388 if (!(ct
->status
& IPS_EXPECTED
))
391 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
393 goto nla_put_failure
;
394 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
395 goto nla_put_failure
;
396 nla_nest_end(skb
, nest_parms
);
405 dump_ct_seq_adj(struct sk_buff
*skb
, const struct nf_ct_seqadj
*seq
, int type
)
407 struct nlattr
*nest_parms
;
409 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
411 goto nla_put_failure
;
413 if (nla_put_be32(skb
, CTA_SEQADJ_CORRECTION_POS
,
414 htonl(seq
->correction_pos
)) ||
415 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_BEFORE
,
416 htonl(seq
->offset_before
)) ||
417 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_AFTER
,
418 htonl(seq
->offset_after
)))
419 goto nla_put_failure
;
421 nla_nest_end(skb
, nest_parms
);
430 ctnetlink_dump_ct_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
432 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
433 struct nf_ct_seqadj
*seq
;
435 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !seqadj
)
438 seq
= &seqadj
->seq
[IP_CT_DIR_ORIGINAL
];
439 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_ORIG
) == -1)
442 seq
= &seqadj
->seq
[IP_CT_DIR_REPLY
];
443 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_REPLY
) == -1)
450 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
452 if (nla_put_be32(skb
, CTA_ID
, htonl((unsigned long)ct
)))
453 goto nla_put_failure
;
461 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
463 if (nla_put_be32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
))))
464 goto nla_put_failure
;
472 ctnetlink_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
475 const struct nf_conntrack_zone
*zone
;
476 struct nlmsghdr
*nlh
;
477 struct nfgenmsg
*nfmsg
;
478 struct nlattr
*nest_parms
;
479 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
481 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_NEW
);
482 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
486 nfmsg
= nlmsg_data(nlh
);
487 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
488 nfmsg
->version
= NFNETLINK_V0
;
491 zone
= nf_ct_zone(ct
);
493 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
495 goto nla_put_failure
;
496 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
497 goto nla_put_failure
;
498 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
499 NF_CT_ZONE_DIR_ORIG
) < 0)
500 goto nla_put_failure
;
501 nla_nest_end(skb
, nest_parms
);
503 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
505 goto nla_put_failure
;
506 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
507 goto nla_put_failure
;
508 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
509 NF_CT_ZONE_DIR_REPL
) < 0)
510 goto nla_put_failure
;
511 nla_nest_end(skb
, nest_parms
);
513 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
514 NF_CT_DEFAULT_ZONE_DIR
) < 0)
515 goto nla_put_failure
;
517 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
518 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
519 ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
520 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
521 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
522 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
523 ctnetlink_dump_mark(skb
, ct
) < 0 ||
524 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
525 ctnetlink_dump_labels(skb
, ct
) < 0 ||
526 ctnetlink_dump_id(skb
, ct
) < 0 ||
527 ctnetlink_dump_use(skb
, ct
) < 0 ||
528 ctnetlink_dump_master(skb
, ct
) < 0 ||
529 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
530 goto nla_put_failure
;
537 nlmsg_cancel(skb
, nlh
);
542 ctnetlink_proto_size(const struct nf_conn
*ct
)
544 struct nf_conntrack_l3proto
*l3proto
;
545 struct nf_conntrack_l4proto
*l4proto
;
549 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
550 len
+= l3proto
->nla_size
;
552 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
553 len
+= l4proto
->nla_size
;
560 ctnetlink_acct_size(const struct nf_conn
*ct
)
562 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
564 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
565 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
566 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
571 ctnetlink_secctx_size(const struct nf_conn
*ct
)
573 #ifdef CONFIG_NF_CONNTRACK_SECMARK
576 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
580 return nla_total_size(0) /* CTA_SECCTX */
581 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
588 ctnetlink_timestamp_size(const struct nf_conn
*ct
)
590 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
591 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
593 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
600 ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
602 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
603 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
604 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
605 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
606 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
607 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
608 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
609 + ctnetlink_acct_size(ct
)
610 + ctnetlink_timestamp_size(ct
)
611 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
612 + nla_total_size(0) /* CTA_PROTOINFO */
613 + nla_total_size(0) /* CTA_HELP */
614 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
615 + ctnetlink_secctx_size(ct
)
616 #ifdef CONFIG_NF_NAT_NEEDED
617 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
618 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
620 #ifdef CONFIG_NF_CONNTRACK_MARK
621 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
623 #ifdef CONFIG_NF_CONNTRACK_ZONES
624 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
626 + ctnetlink_proto_size(ct
)
627 + ctnetlink_label_size(ct
)
631 #ifdef CONFIG_NF_CONNTRACK_EVENTS
633 ctnetlink_conntrack_event(unsigned int events
, struct nf_ct_event
*item
)
635 const struct nf_conntrack_zone
*zone
;
637 struct nlmsghdr
*nlh
;
638 struct nfgenmsg
*nfmsg
;
639 struct nlattr
*nest_parms
;
640 struct nf_conn
*ct
= item
->ct
;
643 unsigned int flags
= 0, group
;
646 /* ignore our fake conntrack entry */
647 if (nf_ct_is_untracked(ct
))
650 if (events
& (1 << IPCT_DESTROY
)) {
651 type
= IPCTNL_MSG_CT_DELETE
;
652 group
= NFNLGRP_CONNTRACK_DESTROY
;
653 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
654 type
= IPCTNL_MSG_CT_NEW
;
655 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
656 group
= NFNLGRP_CONNTRACK_NEW
;
658 type
= IPCTNL_MSG_CT_NEW
;
659 group
= NFNLGRP_CONNTRACK_UPDATE
;
664 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
667 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
671 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
672 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
676 nfmsg
= nlmsg_data(nlh
);
677 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
678 nfmsg
->version
= NFNETLINK_V0
;
682 zone
= nf_ct_zone(ct
);
684 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
686 goto nla_put_failure
;
687 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
688 goto nla_put_failure
;
689 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
690 NF_CT_ZONE_DIR_ORIG
) < 0)
691 goto nla_put_failure
;
692 nla_nest_end(skb
, nest_parms
);
694 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
696 goto nla_put_failure
;
697 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
698 goto nla_put_failure
;
699 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
700 NF_CT_ZONE_DIR_REPL
) < 0)
701 goto nla_put_failure
;
702 nla_nest_end(skb
, nest_parms
);
704 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
705 NF_CT_DEFAULT_ZONE_DIR
) < 0)
706 goto nla_put_failure
;
708 if (ctnetlink_dump_id(skb
, ct
) < 0)
709 goto nla_put_failure
;
711 if (ctnetlink_dump_status(skb
, ct
) < 0)
712 goto nla_put_failure
;
714 if (events
& (1 << IPCT_DESTROY
)) {
715 if (ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
716 ctnetlink_dump_timestamp(skb
, ct
) < 0)
717 goto nla_put_failure
;
719 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
720 goto nla_put_failure
;
722 if (events
& (1 << IPCT_PROTOINFO
)
723 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
724 goto nla_put_failure
;
726 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
727 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
728 goto nla_put_failure
;
730 #ifdef CONFIG_NF_CONNTRACK_SECMARK
731 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
732 && ctnetlink_dump_secctx(skb
, ct
) < 0)
733 goto nla_put_failure
;
735 if (events
& (1 << IPCT_LABEL
) &&
736 ctnetlink_dump_labels(skb
, ct
) < 0)
737 goto nla_put_failure
;
739 if (events
& (1 << IPCT_RELATED
) &&
740 ctnetlink_dump_master(skb
, ct
) < 0)
741 goto nla_put_failure
;
743 if (events
& (1 << IPCT_SEQADJ
) &&
744 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
745 goto nla_put_failure
;
748 #ifdef CONFIG_NF_CONNTRACK_MARK
749 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
750 && ctnetlink_dump_mark(skb
, ct
) < 0)
751 goto nla_put_failure
;
756 err
= nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
,
758 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
765 nlmsg_cancel(skb
, nlh
);
769 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
774 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
776 static int ctnetlink_done(struct netlink_callback
*cb
)
779 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
784 struct ctnetlink_filter
{
791 static struct ctnetlink_filter
*
792 ctnetlink_alloc_filter(const struct nlattr
* const cda
[])
794 #ifdef CONFIG_NF_CONNTRACK_MARK
795 struct ctnetlink_filter
*filter
;
797 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
799 return ERR_PTR(-ENOMEM
);
801 filter
->mark
.val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
802 filter
->mark
.mask
= ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
806 return ERR_PTR(-EOPNOTSUPP
);
810 static int ctnetlink_filter_match(struct nf_conn
*ct
, void *data
)
812 struct ctnetlink_filter
*filter
= data
;
817 #ifdef CONFIG_NF_CONNTRACK_MARK
818 if ((ct
->mark
& filter
->mark
.mask
) == filter
->mark
.val
)
826 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
828 struct net
*net
= sock_net(skb
->sk
);
829 struct nf_conn
*ct
, *last
;
830 struct nf_conntrack_tuple_hash
*h
;
831 struct hlist_nulls_node
*n
;
832 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
833 u_int8_t l3proto
= nfmsg
->nfgen_family
;
837 last
= (struct nf_conn
*)cb
->args
[1];
840 for (; cb
->args
[0] < net
->ct
.htable_size
; cb
->args
[0]++) {
842 lockp
= &nf_conntrack_locks
[cb
->args
[0] % CONNTRACK_LOCKS
];
844 if (cb
->args
[0] >= net
->ct
.htable_size
) {
848 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[cb
->args
[0]],
850 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
852 ct
= nf_ct_tuplehash_to_ctrack(h
);
853 /* Dump entries of a given L3 protocol number.
854 * If it is not specified, ie. l3proto == 0,
855 * then dump everything. */
856 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
863 if (!ctnetlink_filter_match(ct
, cb
->data
))
868 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
870 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
874 nf_conntrack_get(&ct
->ct_general
);
875 cb
->args
[1] = (unsigned long)ct
;
895 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
897 struct nlattr
*tb
[CTA_IP_MAX
+1];
898 struct nf_conntrack_l3proto
*l3proto
;
901 ret
= nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
906 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
908 if (likely(l3proto
->nlattr_to_tuple
)) {
909 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
910 l3proto
->nla_policy
);
912 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
920 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
921 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
925 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
926 struct nf_conntrack_tuple
*tuple
)
928 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
929 struct nf_conntrack_l4proto
*l4proto
;
932 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
936 if (!tb
[CTA_PROTO_NUM
])
938 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
941 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
, tuple
->dst
.protonum
);
943 if (likely(l4proto
->nlattr_to_tuple
)) {
944 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
945 l4proto
->nla_policy
);
947 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
956 ctnetlink_parse_zone(const struct nlattr
*attr
,
957 struct nf_conntrack_zone
*zone
)
959 zone
->id
= NF_CT_DEFAULT_ZONE_ID
;
960 zone
->dir
= NF_CT_DEFAULT_ZONE_DIR
;
962 #ifdef CONFIG_NF_CONNTRACK_ZONES
964 zone
->id
= ntohs(nla_get_be16(attr
));
973 ctnetlink_parse_tuple_zone(struct nlattr
*attr
, enum ctattr_type type
,
974 struct nf_conntrack_zone
*zone
)
978 if (zone
->id
!= NF_CT_DEFAULT_ZONE_ID
)
981 ret
= ctnetlink_parse_zone(attr
, zone
);
985 if (type
== CTA_TUPLE_REPLY
)
986 zone
->dir
= NF_CT_ZONE_DIR_REPL
;
988 zone
->dir
= NF_CT_ZONE_DIR_ORIG
;
993 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
994 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
995 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
996 [CTA_TUPLE_ZONE
] = { .type
= NLA_U16
},
1000 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
1001 struct nf_conntrack_tuple
*tuple
,
1002 enum ctattr_type type
, u_int8_t l3num
,
1003 struct nf_conntrack_zone
*zone
)
1005 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
1008 memset(tuple
, 0, sizeof(*tuple
));
1010 err
= nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
1014 if (!tb
[CTA_TUPLE_IP
])
1017 tuple
->src
.l3num
= l3num
;
1019 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
1023 if (!tb
[CTA_TUPLE_PROTO
])
1026 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
1030 if (tb
[CTA_TUPLE_ZONE
]) {
1034 err
= ctnetlink_parse_tuple_zone(tb
[CTA_TUPLE_ZONE
],
1040 /* orig and expect tuples get DIR_ORIGINAL */
1041 if (type
== CTA_TUPLE_REPLY
)
1042 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
1044 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
1049 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
1050 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
1051 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1055 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
,
1056 struct nlattr
**helpinfo
)
1059 struct nlattr
*tb
[CTA_HELP_MAX
+1];
1061 err
= nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
1065 if (!tb
[CTA_HELP_NAME
])
1068 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
1070 if (tb
[CTA_HELP_INFO
])
1071 *helpinfo
= tb
[CTA_HELP_INFO
];
1076 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
1077 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
1078 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
1079 [CTA_STATUS
] = { .type
= NLA_U32
},
1080 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
1081 [CTA_HELP
] = { .type
= NLA_NESTED
},
1082 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
1083 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
1084 [CTA_MARK
] = { .type
= NLA_U32
},
1085 [CTA_ID
] = { .type
= NLA_U32
},
1086 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
1087 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
1088 [CTA_NAT_SEQ_ADJ_ORIG
] = { .type
= NLA_NESTED
},
1089 [CTA_NAT_SEQ_ADJ_REPLY
] = { .type
= NLA_NESTED
},
1090 [CTA_ZONE
] = { .type
= NLA_U16
},
1091 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
1092 [CTA_LABELS
] = { .type
= NLA_BINARY
,
1093 .len
= NF_CT_LABELS_MAX_SIZE
},
1094 [CTA_LABELS_MASK
] = { .type
= NLA_BINARY
,
1095 .len
= NF_CT_LABELS_MAX_SIZE
},
1098 static int ctnetlink_flush_conntrack(struct net
*net
,
1099 const struct nlattr
* const cda
[],
1100 u32 portid
, int report
)
1102 struct ctnetlink_filter
*filter
= NULL
;
1104 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1105 filter
= ctnetlink_alloc_filter(cda
);
1107 return PTR_ERR(filter
);
1110 nf_ct_iterate_cleanup(net
, ctnetlink_filter_match
, filter
,
1118 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1119 const struct nlmsghdr
*nlh
,
1120 const struct nlattr
* const cda
[])
1122 struct net
*net
= sock_net(ctnl
);
1123 struct nf_conntrack_tuple_hash
*h
;
1124 struct nf_conntrack_tuple tuple
;
1126 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1127 u_int8_t u3
= nfmsg
->nfgen_family
;
1128 struct nf_conntrack_zone zone
;
1131 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1135 if (cda
[CTA_TUPLE_ORIG
])
1136 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1138 else if (cda
[CTA_TUPLE_REPLY
])
1139 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1142 return ctnetlink_flush_conntrack(net
, cda
,
1143 NETLINK_CB(skb
).portid
,
1150 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1154 ct
= nf_ct_tuplehash_to_ctrack(h
);
1157 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
1158 if (id
!= (u32
)(unsigned long)ct
) {
1164 if (del_timer(&ct
->timeout
))
1165 nf_ct_delete(ct
, NETLINK_CB(skb
).portid
, nlmsg_report(nlh
));
1173 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1174 const struct nlmsghdr
*nlh
,
1175 const struct nlattr
* const cda
[])
1177 struct net
*net
= sock_net(ctnl
);
1178 struct nf_conntrack_tuple_hash
*h
;
1179 struct nf_conntrack_tuple tuple
;
1181 struct sk_buff
*skb2
= NULL
;
1182 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1183 u_int8_t u3
= nfmsg
->nfgen_family
;
1184 struct nf_conntrack_zone zone
;
1187 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1188 struct netlink_dump_control c
= {
1189 .dump
= ctnetlink_dump_table
,
1190 .done
= ctnetlink_done
,
1193 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1194 struct ctnetlink_filter
*filter
;
1196 filter
= ctnetlink_alloc_filter(cda
);
1198 return PTR_ERR(filter
);
1202 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1205 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1209 if (cda
[CTA_TUPLE_ORIG
])
1210 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1212 else if (cda
[CTA_TUPLE_REPLY
])
1213 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1221 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1225 ct
= nf_ct_tuplehash_to_ctrack(h
);
1228 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1235 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
1236 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1242 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
1251 /* this avoids a loop in nfnetlink. */
1252 return err
== -EAGAIN
? -ENOBUFS
: err
;
1255 static int ctnetlink_done_list(struct netlink_callback
*cb
)
1258 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
1263 ctnetlink_dump_list(struct sk_buff
*skb
, struct netlink_callback
*cb
, bool dying
)
1265 struct nf_conn
*ct
, *last
;
1266 struct nf_conntrack_tuple_hash
*h
;
1267 struct hlist_nulls_node
*n
;
1268 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1269 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1272 struct hlist_nulls_head
*list
;
1273 struct net
*net
= sock_net(skb
->sk
);
1278 last
= (struct nf_conn
*)cb
->args
[1];
1280 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
1281 struct ct_pcpu
*pcpu
;
1283 if (!cpu_possible(cpu
))
1286 pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1287 spin_lock_bh(&pcpu
->lock
);
1288 list
= dying
? &pcpu
->dying
: &pcpu
->unconfirmed
;
1290 hlist_nulls_for_each_entry(h
, n
, list
, hnnode
) {
1291 ct
= nf_ct_tuplehash_to_ctrack(h
);
1292 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
1300 res
= ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1302 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
1306 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
1309 cb
->args
[1] = (unsigned long)ct
;
1310 spin_unlock_bh(&pcpu
->lock
);
1318 spin_unlock_bh(&pcpu
->lock
);
1329 ctnetlink_dump_dying(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1331 return ctnetlink_dump_list(skb
, cb
, true);
1335 ctnetlink_get_ct_dying(struct sock
*ctnl
, struct sk_buff
*skb
,
1336 const struct nlmsghdr
*nlh
,
1337 const struct nlattr
* const cda
[])
1339 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1340 struct netlink_dump_control c
= {
1341 .dump
= ctnetlink_dump_dying
,
1342 .done
= ctnetlink_done_list
,
1344 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1351 ctnetlink_dump_unconfirmed(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1353 return ctnetlink_dump_list(skb
, cb
, false);
1357 ctnetlink_get_ct_unconfirmed(struct sock
*ctnl
, struct sk_buff
*skb
,
1358 const struct nlmsghdr
*nlh
,
1359 const struct nlattr
* const cda
[])
1361 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1362 struct netlink_dump_control c
= {
1363 .dump
= ctnetlink_dump_unconfirmed
,
1364 .done
= ctnetlink_done_list
,
1366 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1372 #ifdef CONFIG_NF_NAT_NEEDED
1374 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1375 enum nf_nat_manip_type manip
,
1376 const struct nlattr
*attr
)
1378 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1381 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1382 if (!parse_nat_setup
) {
1383 #ifdef CONFIG_MODULES
1385 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1386 if (request_module("nf-nat") < 0) {
1387 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1391 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1393 if (nfnetlink_parse_nat_setup_hook
)
1399 err
= parse_nat_setup(ct
, manip
, attr
);
1400 if (err
== -EAGAIN
) {
1401 #ifdef CONFIG_MODULES
1403 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1404 if (request_module("nf-nat-%u", nf_ct_l3num(ct
)) < 0) {
1405 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1409 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1420 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1423 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1424 d
= ct
->status
^ status
;
1426 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1430 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1431 /* SEEN_REPLY bit can only be set */
1434 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1435 /* ASSURED bit can only be set */
1438 /* Be careful here, modifying NAT bits can screw up things,
1439 * so don't let users modify them directly if they don't pass
1441 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1446 ctnetlink_setup_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1448 #ifdef CONFIG_NF_NAT_NEEDED
1451 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1454 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_DST
,
1459 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_SRC
,
1463 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1470 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1472 struct nf_conntrack_helper
*helper
;
1473 struct nf_conn_help
*help
= nfct_help(ct
);
1474 char *helpname
= NULL
;
1475 struct nlattr
*helpinfo
= NULL
;
1478 /* don't change helper of sibling connections */
1482 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1486 if (!strcmp(helpname
, "")) {
1487 if (help
&& help
->helper
) {
1488 /* we had a helper before ... */
1489 nf_ct_remove_expectations(ct
);
1490 RCU_INIT_POINTER(help
->helper
, NULL
);
1496 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1497 nf_ct_protonum(ct
));
1498 if (helper
== NULL
) {
1499 #ifdef CONFIG_MODULES
1500 spin_unlock_bh(&nf_conntrack_expect_lock
);
1502 if (request_module("nfct-helper-%s", helpname
) < 0) {
1503 spin_lock_bh(&nf_conntrack_expect_lock
);
1507 spin_lock_bh(&nf_conntrack_expect_lock
);
1508 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1509 nf_ct_protonum(ct
));
1517 if (help
->helper
== helper
) {
1518 /* update private helper data if allowed. */
1519 if (helper
->from_nlattr
)
1520 helper
->from_nlattr(helpinfo
, ct
);
1526 /* we cannot set a helper for an existing conntrack */
1531 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1533 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1535 if (!del_timer(&ct
->timeout
))
1538 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1539 add_timer(&ct
->timeout
);
1544 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1545 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1546 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1547 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1551 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1553 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1554 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1555 struct nf_conntrack_l4proto
*l4proto
;
1558 err
= nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1563 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1564 if (l4proto
->from_nlattr
)
1565 err
= l4proto
->from_nlattr(tb
, ct
);
1571 static const struct nla_policy seqadj_policy
[CTA_SEQADJ_MAX
+1] = {
1572 [CTA_SEQADJ_CORRECTION_POS
] = { .type
= NLA_U32
},
1573 [CTA_SEQADJ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1574 [CTA_SEQADJ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1578 change_seq_adj(struct nf_ct_seqadj
*seq
, const struct nlattr
* const attr
)
1581 struct nlattr
*cda
[CTA_SEQADJ_MAX
+1];
1583 err
= nla_parse_nested(cda
, CTA_SEQADJ_MAX
, attr
, seqadj_policy
);
1587 if (!cda
[CTA_SEQADJ_CORRECTION_POS
])
1590 seq
->correction_pos
=
1591 ntohl(nla_get_be32(cda
[CTA_SEQADJ_CORRECTION_POS
]));
1593 if (!cda
[CTA_SEQADJ_OFFSET_BEFORE
])
1596 seq
->offset_before
=
1597 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_BEFORE
]));
1599 if (!cda
[CTA_SEQADJ_OFFSET_AFTER
])
1603 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_AFTER
]));
1609 ctnetlink_change_seq_adj(struct nf_conn
*ct
,
1610 const struct nlattr
* const cda
[])
1612 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
1618 if (cda
[CTA_SEQ_ADJ_ORIG
]) {
1619 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_ORIGINAL
],
1620 cda
[CTA_SEQ_ADJ_ORIG
]);
1624 ct
->status
|= IPS_SEQ_ADJUST
;
1627 if (cda
[CTA_SEQ_ADJ_REPLY
]) {
1628 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_REPLY
],
1629 cda
[CTA_SEQ_ADJ_REPLY
]);
1633 ct
->status
|= IPS_SEQ_ADJUST
;
1640 ctnetlink_attach_labels(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1642 #ifdef CONFIG_NF_CONNTRACK_LABELS
1643 size_t len
= nla_len(cda
[CTA_LABELS
]);
1644 const void *mask
= cda
[CTA_LABELS_MASK
];
1646 if (len
& (sizeof(u32
)-1)) /* must be multiple of u32 */
1650 if (nla_len(cda
[CTA_LABELS_MASK
]) == 0 ||
1651 nla_len(cda
[CTA_LABELS_MASK
]) != len
)
1653 mask
= nla_data(cda
[CTA_LABELS_MASK
]);
1658 return nf_connlabels_replace(ct
, nla_data(cda
[CTA_LABELS
]), mask
, len
);
1665 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1666 const struct nlattr
* const cda
[])
1670 /* only allow NAT changes and master assignation for new conntracks */
1671 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1674 if (cda
[CTA_HELP
]) {
1675 err
= ctnetlink_change_helper(ct
, cda
);
1680 if (cda
[CTA_TIMEOUT
]) {
1681 err
= ctnetlink_change_timeout(ct
, cda
);
1686 if (cda
[CTA_STATUS
]) {
1687 err
= ctnetlink_change_status(ct
, cda
);
1692 if (cda
[CTA_PROTOINFO
]) {
1693 err
= ctnetlink_change_protoinfo(ct
, cda
);
1698 #if defined(CONFIG_NF_CONNTRACK_MARK)
1700 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1703 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1704 err
= ctnetlink_change_seq_adj(ct
, cda
);
1709 if (cda
[CTA_LABELS
]) {
1710 err
= ctnetlink_attach_labels(ct
, cda
);
1718 static struct nf_conn
*
1719 ctnetlink_create_conntrack(struct net
*net
,
1720 const struct nf_conntrack_zone
*zone
,
1721 const struct nlattr
* const cda
[],
1722 struct nf_conntrack_tuple
*otuple
,
1723 struct nf_conntrack_tuple
*rtuple
,
1728 struct nf_conntrack_helper
*helper
;
1729 struct nf_conn_tstamp
*tstamp
;
1731 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1733 return ERR_PTR(-ENOMEM
);
1735 if (!cda
[CTA_TIMEOUT
])
1737 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1739 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1742 if (cda
[CTA_HELP
]) {
1743 char *helpname
= NULL
;
1744 struct nlattr
*helpinfo
= NULL
;
1746 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1750 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1751 nf_ct_protonum(ct
));
1752 if (helper
== NULL
) {
1754 #ifdef CONFIG_MODULES
1755 if (request_module("nfct-helper-%s", helpname
) < 0) {
1761 helper
= __nf_conntrack_helper_find(helpname
,
1763 nf_ct_protonum(ct
));
1773 struct nf_conn_help
*help
;
1775 help
= nf_ct_helper_ext_add(ct
, helper
, GFP_ATOMIC
);
1780 /* set private helper data if allowed. */
1781 if (helper
->from_nlattr
)
1782 helper
->from_nlattr(helpinfo
, ct
);
1784 /* not in hash table yet so not strictly necessary */
1785 RCU_INIT_POINTER(help
->helper
, helper
);
1788 /* try an implicit helper assignation */
1789 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1794 err
= ctnetlink_setup_nat(ct
, cda
);
1798 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1799 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1800 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1801 nf_ct_labels_ext_add(ct
);
1803 /* we must add conntrack extensions before confirmation. */
1804 ct
->status
|= IPS_CONFIRMED
;
1806 if (cda
[CTA_STATUS
]) {
1807 err
= ctnetlink_change_status(ct
, cda
);
1812 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1813 err
= ctnetlink_change_seq_adj(ct
, cda
);
1818 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1819 if (cda
[CTA_PROTOINFO
]) {
1820 err
= ctnetlink_change_protoinfo(ct
, cda
);
1825 #if defined(CONFIG_NF_CONNTRACK_MARK)
1827 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1830 /* setup master conntrack: this is a confirmed expectation */
1831 if (cda
[CTA_TUPLE_MASTER
]) {
1832 struct nf_conntrack_tuple master
;
1833 struct nf_conntrack_tuple_hash
*master_h
;
1834 struct nf_conn
*master_ct
;
1836 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
,
1841 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1842 if (master_h
== NULL
) {
1846 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1847 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1848 ct
->master
= master_ct
;
1850 tstamp
= nf_conn_tstamp_find(ct
);
1852 tstamp
->start
= ktime_get_real_ns();
1854 err
= nf_conntrack_hash_check_insert(ct
);
1865 nf_conntrack_free(ct
);
1866 return ERR_PTR(err
);
1870 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1871 const struct nlmsghdr
*nlh
,
1872 const struct nlattr
* const cda
[])
1874 struct net
*net
= sock_net(ctnl
);
1875 struct nf_conntrack_tuple otuple
, rtuple
;
1876 struct nf_conntrack_tuple_hash
*h
= NULL
;
1877 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1879 u_int8_t u3
= nfmsg
->nfgen_family
;
1880 struct nf_conntrack_zone zone
;
1883 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1887 if (cda
[CTA_TUPLE_ORIG
]) {
1888 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
,
1894 if (cda
[CTA_TUPLE_REPLY
]) {
1895 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
,
1901 if (cda
[CTA_TUPLE_ORIG
])
1902 h
= nf_conntrack_find_get(net
, &zone
, &otuple
);
1903 else if (cda
[CTA_TUPLE_REPLY
])
1904 h
= nf_conntrack_find_get(net
, &zone
, &rtuple
);
1908 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1909 enum ip_conntrack_events events
;
1911 if (!cda
[CTA_TUPLE_ORIG
] || !cda
[CTA_TUPLE_REPLY
])
1914 ct
= ctnetlink_create_conntrack(net
, &zone
, cda
, &otuple
,
1920 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1921 events
= IPCT_RELATED
;
1925 if (cda
[CTA_LABELS
] &&
1926 ctnetlink_attach_labels(ct
, cda
) == 0)
1927 events
|= (1 << IPCT_LABEL
);
1929 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1930 (1 << IPCT_ASSURED
) |
1931 (1 << IPCT_HELPER
) |
1932 (1 << IPCT_PROTOINFO
) |
1933 (1 << IPCT_SEQADJ
) |
1934 (1 << IPCT_MARK
) | events
,
1935 ct
, NETLINK_CB(skb
).portid
,
1942 /* implicit 'else' */
1945 ct
= nf_ct_tuplehash_to_ctrack(h
);
1946 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1947 spin_lock_bh(&nf_conntrack_expect_lock
);
1948 err
= ctnetlink_change_conntrack(ct
, cda
);
1949 spin_unlock_bh(&nf_conntrack_expect_lock
);
1951 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1952 (1 << IPCT_ASSURED
) |
1953 (1 << IPCT_HELPER
) |
1955 (1 << IPCT_PROTOINFO
) |
1956 (1 << IPCT_SEQADJ
) |
1958 ct
, NETLINK_CB(skb
).portid
,
1968 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1969 __u16 cpu
, const struct ip_conntrack_stat
*st
)
1971 struct nlmsghdr
*nlh
;
1972 struct nfgenmsg
*nfmsg
;
1973 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
1975 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS_CPU
);
1976 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
1980 nfmsg
= nlmsg_data(nlh
);
1981 nfmsg
->nfgen_family
= AF_UNSPEC
;
1982 nfmsg
->version
= NFNETLINK_V0
;
1983 nfmsg
->res_id
= htons(cpu
);
1985 if (nla_put_be32(skb
, CTA_STATS_SEARCHED
, htonl(st
->searched
)) ||
1986 nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
1987 nla_put_be32(skb
, CTA_STATS_NEW
, htonl(st
->new)) ||
1988 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
1989 nla_put_be32(skb
, CTA_STATS_IGNORE
, htonl(st
->ignore
)) ||
1990 nla_put_be32(skb
, CTA_STATS_DELETE
, htonl(st
->delete)) ||
1991 nla_put_be32(skb
, CTA_STATS_DELETE_LIST
, htonl(st
->delete_list
)) ||
1992 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
1993 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
1994 htonl(st
->insert_failed
)) ||
1995 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
1996 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
1997 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
1998 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
1999 htonl(st
->search_restart
)))
2000 goto nla_put_failure
;
2002 nlmsg_end(skb
, nlh
);
2007 nlmsg_cancel(skb
, nlh
);
2012 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2015 struct net
*net
= sock_net(skb
->sk
);
2017 if (cb
->args
[0] == nr_cpu_ids
)
2020 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2021 const struct ip_conntrack_stat
*st
;
2023 if (!cpu_possible(cpu
))
2026 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2027 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
2028 NETLINK_CB(cb
->skb
).portid
,
2039 ctnetlink_stat_ct_cpu(struct sock
*ctnl
, struct sk_buff
*skb
,
2040 const struct nlmsghdr
*nlh
,
2041 const struct nlattr
* const cda
[])
2043 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2044 struct netlink_dump_control c
= {
2045 .dump
= ctnetlink_ct_stat_cpu_dump
,
2047 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2054 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
2057 struct nlmsghdr
*nlh
;
2058 struct nfgenmsg
*nfmsg
;
2059 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2060 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
2062 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS
);
2063 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2067 nfmsg
= nlmsg_data(nlh
);
2068 nfmsg
->nfgen_family
= AF_UNSPEC
;
2069 nfmsg
->version
= NFNETLINK_V0
;
2072 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
2073 goto nla_put_failure
;
2075 nlmsg_end(skb
, nlh
);
2080 nlmsg_cancel(skb
, nlh
);
2085 ctnetlink_stat_ct(struct sock
*ctnl
, struct sk_buff
*skb
,
2086 const struct nlmsghdr
*nlh
,
2087 const struct nlattr
* const cda
[])
2089 struct sk_buff
*skb2
;
2092 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2096 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2098 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
2103 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2112 /* this avoids a loop in nfnetlink. */
2113 return err
== -EAGAIN
? -ENOBUFS
: err
;
2116 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2117 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2118 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2119 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2120 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2121 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2122 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
2123 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
2124 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2125 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2126 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2127 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2128 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2131 static struct nf_conntrack_expect
*
2132 ctnetlink_alloc_expect(const struct nlattr
*const cda
[], struct nf_conn
*ct
,
2133 struct nf_conntrack_helper
*helper
,
2134 struct nf_conntrack_tuple
*tuple
,
2135 struct nf_conntrack_tuple
*mask
);
2137 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2139 ctnetlink_nfqueue_build_size(const struct nf_conn
*ct
)
2141 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2142 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2143 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2144 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
2145 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
2146 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
2147 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
2148 + nla_total_size(0) /* CTA_PROTOINFO */
2149 + nla_total_size(0) /* CTA_HELP */
2150 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
2151 + ctnetlink_secctx_size(ct
)
2152 #ifdef CONFIG_NF_NAT_NEEDED
2153 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2154 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
2156 #ifdef CONFIG_NF_CONNTRACK_MARK
2157 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
2159 #ifdef CONFIG_NF_CONNTRACK_ZONES
2160 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2162 + ctnetlink_proto_size(ct
)
2167 ctnetlink_nfqueue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
2169 const struct nf_conntrack_zone
*zone
;
2170 struct nlattr
*nest_parms
;
2173 zone
= nf_ct_zone(ct
);
2175 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
2177 goto nla_put_failure
;
2178 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
2179 goto nla_put_failure
;
2180 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2181 NF_CT_ZONE_DIR_ORIG
) < 0)
2182 goto nla_put_failure
;
2183 nla_nest_end(skb
, nest_parms
);
2185 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
2187 goto nla_put_failure
;
2188 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
2189 goto nla_put_failure
;
2190 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2191 NF_CT_ZONE_DIR_REPL
) < 0)
2192 goto nla_put_failure
;
2193 nla_nest_end(skb
, nest_parms
);
2195 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
2196 NF_CT_DEFAULT_ZONE_DIR
) < 0)
2197 goto nla_put_failure
;
2199 if (ctnetlink_dump_id(skb
, ct
) < 0)
2200 goto nla_put_failure
;
2202 if (ctnetlink_dump_status(skb
, ct
) < 0)
2203 goto nla_put_failure
;
2205 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
2206 goto nla_put_failure
;
2208 if (ctnetlink_dump_protoinfo(skb
, ct
) < 0)
2209 goto nla_put_failure
;
2211 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
2212 goto nla_put_failure
;
2214 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2215 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
2216 goto nla_put_failure
;
2218 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
2219 goto nla_put_failure
;
2221 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
2222 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
2223 goto nla_put_failure
;
2225 #ifdef CONFIG_NF_CONNTRACK_MARK
2226 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
2227 goto nla_put_failure
;
2229 if (ctnetlink_dump_labels(skb
, ct
) < 0)
2230 goto nla_put_failure
;
2240 ctnetlink_nfqueue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
2244 if (cda
[CTA_TIMEOUT
]) {
2245 err
= ctnetlink_change_timeout(ct
, cda
);
2249 if (cda
[CTA_STATUS
]) {
2250 err
= ctnetlink_change_status(ct
, cda
);
2254 if (cda
[CTA_HELP
]) {
2255 err
= ctnetlink_change_helper(ct
, cda
);
2259 if (cda
[CTA_LABELS
]) {
2260 err
= ctnetlink_attach_labels(ct
, cda
);
2264 #if defined(CONFIG_NF_CONNTRACK_MARK)
2265 if (cda
[CTA_MARK
]) {
2266 u32 mask
= 0, mark
, newmark
;
2267 if (cda
[CTA_MARK_MASK
])
2268 mask
= ~ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
2270 mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
2271 newmark
= (ct
->mark
& mask
) ^ mark
;
2272 if (newmark
!= ct
->mark
)
2280 ctnetlink_nfqueue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
2282 struct nlattr
*cda
[CTA_MAX
+1];
2285 ret
= nla_parse_nested(cda
, CTA_MAX
, attr
, ct_nla_policy
);
2289 spin_lock_bh(&nf_conntrack_expect_lock
);
2290 ret
= ctnetlink_nfqueue_parse_ct((const struct nlattr
**)cda
, ct
);
2291 spin_unlock_bh(&nf_conntrack_expect_lock
);
2296 static int ctnetlink_nfqueue_exp_parse(const struct nlattr
* const *cda
,
2297 const struct nf_conn
*ct
,
2298 struct nf_conntrack_tuple
*tuple
,
2299 struct nf_conntrack_tuple
*mask
)
2303 err
= ctnetlink_parse_tuple(cda
, tuple
, CTA_EXPECT_TUPLE
,
2304 nf_ct_l3num(ct
), NULL
);
2308 return ctnetlink_parse_tuple(cda
, mask
, CTA_EXPECT_MASK
,
2309 nf_ct_l3num(ct
), NULL
);
2313 ctnetlink_nfqueue_attach_expect(const struct nlattr
*attr
, struct nf_conn
*ct
,
2314 u32 portid
, u32 report
)
2316 struct nlattr
*cda
[CTA_EXPECT_MAX
+1];
2317 struct nf_conntrack_tuple tuple
, mask
;
2318 struct nf_conntrack_helper
*helper
= NULL
;
2319 struct nf_conntrack_expect
*exp
;
2322 err
= nla_parse_nested(cda
, CTA_EXPECT_MAX
, attr
, exp_nla_policy
);
2326 err
= ctnetlink_nfqueue_exp_parse((const struct nlattr
* const *)cda
,
2331 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2332 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2334 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2335 nf_ct_protonum(ct
));
2340 exp
= ctnetlink_alloc_expect((const struct nlattr
* const *)cda
, ct
,
2341 helper
, &tuple
, &mask
);
2343 return PTR_ERR(exp
);
2345 err
= nf_ct_expect_related_report(exp
, portid
, report
);
2347 nf_ct_expect_put(exp
);
2354 static struct nfq_ct_hook ctnetlink_nfqueue_hook
= {
2355 .build_size
= ctnetlink_nfqueue_build_size
,
2356 .build
= ctnetlink_nfqueue_build
,
2357 .parse
= ctnetlink_nfqueue_parse
,
2358 .attach_expect
= ctnetlink_nfqueue_attach_expect
,
2359 .seq_adjust
= nf_ct_tcp_seqadj_set
,
2361 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2363 /***********************************************************************
2365 ***********************************************************************/
2368 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
2369 const struct nf_conntrack_tuple
*tuple
,
2370 enum ctattr_expect type
)
2372 struct nlattr
*nest_parms
;
2374 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
2376 goto nla_put_failure
;
2377 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
2378 goto nla_put_failure
;
2379 nla_nest_end(skb
, nest_parms
);
2388 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
2389 const struct nf_conntrack_tuple
*tuple
,
2390 const struct nf_conntrack_tuple_mask
*mask
)
2393 struct nf_conntrack_l3proto
*l3proto
;
2394 struct nf_conntrack_l4proto
*l4proto
;
2395 struct nf_conntrack_tuple m
;
2396 struct nlattr
*nest_parms
;
2398 memset(&m
, 0xFF, sizeof(m
));
2399 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
2400 m
.src
.u
.all
= mask
->src
.u
.all
;
2401 m
.dst
.protonum
= tuple
->dst
.protonum
;
2403 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
2405 goto nla_put_failure
;
2408 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
2409 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
2411 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
2412 tuple
->dst
.protonum
);
2413 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
2417 if (unlikely(ret
< 0))
2418 goto nla_put_failure
;
2420 nla_nest_end(skb
, nest_parms
);
2428 static const union nf_inet_addr any_addr
;
2431 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
2432 const struct nf_conntrack_expect
*exp
)
2434 struct nf_conn
*master
= exp
->master
;
2435 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
2436 struct nf_conn_help
*help
;
2437 #ifdef CONFIG_NF_NAT_NEEDED
2438 struct nlattr
*nest_parms
;
2439 struct nf_conntrack_tuple nat_tuple
= {};
2441 struct nf_ct_helper_expectfn
*expfn
;
2446 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
2447 goto nla_put_failure
;
2448 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
2449 goto nla_put_failure
;
2450 if (ctnetlink_exp_dump_tuple(skb
,
2451 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
2452 CTA_EXPECT_MASTER
) < 0)
2453 goto nla_put_failure
;
2455 #ifdef CONFIG_NF_NAT_NEEDED
2456 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
2457 exp
->saved_proto
.all
) {
2458 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
2460 goto nla_put_failure
;
2462 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
2463 goto nla_put_failure
;
2465 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
2466 nat_tuple
.src
.u3
= exp
->saved_addr
;
2467 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
2468 nat_tuple
.src
.u
= exp
->saved_proto
;
2470 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
2471 CTA_EXPECT_NAT_TUPLE
) < 0)
2472 goto nla_put_failure
;
2473 nla_nest_end(skb
, nest_parms
);
2476 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
2477 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
2478 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
2479 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
2480 goto nla_put_failure
;
2481 help
= nfct_help(master
);
2483 struct nf_conntrack_helper
*helper
;
2485 helper
= rcu_dereference(help
->helper
);
2487 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
2488 goto nla_put_failure
;
2490 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
2491 if (expfn
!= NULL
&&
2492 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
2493 goto nla_put_failure
;
2502 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2503 int event
, const struct nf_conntrack_expect
*exp
)
2505 struct nlmsghdr
*nlh
;
2506 struct nfgenmsg
*nfmsg
;
2507 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
2509 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2510 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2514 nfmsg
= nlmsg_data(nlh
);
2515 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2516 nfmsg
->version
= NFNETLINK_V0
;
2519 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2520 goto nla_put_failure
;
2522 nlmsg_end(skb
, nlh
);
2527 nlmsg_cancel(skb
, nlh
);
2531 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2533 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
2535 struct nf_conntrack_expect
*exp
= item
->exp
;
2536 struct net
*net
= nf_ct_exp_net(exp
);
2537 struct nlmsghdr
*nlh
;
2538 struct nfgenmsg
*nfmsg
;
2539 struct sk_buff
*skb
;
2540 unsigned int type
, group
;
2543 if (events
& (1 << IPEXP_DESTROY
)) {
2544 type
= IPCTNL_MSG_EXP_DELETE
;
2545 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
2546 } else if (events
& (1 << IPEXP_NEW
)) {
2547 type
= IPCTNL_MSG_EXP_NEW
;
2548 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
2549 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
2553 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
2556 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
2560 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2561 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
2565 nfmsg
= nlmsg_data(nlh
);
2566 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2567 nfmsg
->version
= NFNETLINK_V0
;
2571 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2572 goto nla_put_failure
;
2575 nlmsg_end(skb
, nlh
);
2576 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
2581 nlmsg_cancel(skb
, nlh
);
2585 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
2589 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
2592 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
2597 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2599 struct net
*net
= sock_net(skb
->sk
);
2600 struct nf_conntrack_expect
*exp
, *last
;
2601 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2602 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2605 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2606 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
2608 hlist_for_each_entry(exp
, &net
->ct
.expect_hash
[cb
->args
[0]],
2610 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2617 if (ctnetlink_exp_fill_info(skb
,
2618 NETLINK_CB(cb
->skb
).portid
,
2622 if (!atomic_inc_not_zero(&exp
->use
))
2624 cb
->args
[1] = (unsigned long)exp
;
2636 nf_ct_expect_put(last
);
2642 ctnetlink_exp_ct_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2644 struct nf_conntrack_expect
*exp
, *last
;
2645 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2646 struct nf_conn
*ct
= cb
->data
;
2647 struct nf_conn_help
*help
= nfct_help(ct
);
2648 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2654 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2656 hlist_for_each_entry(exp
, &help
->expectations
, lnode
) {
2657 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2664 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
2668 if (!atomic_inc_not_zero(&exp
->use
))
2670 cb
->args
[1] = (unsigned long)exp
;
2682 nf_ct_expect_put(last
);
2687 static int ctnetlink_dump_exp_ct(struct sock
*ctnl
, struct sk_buff
*skb
,
2688 const struct nlmsghdr
*nlh
,
2689 const struct nlattr
* const cda
[])
2692 struct net
*net
= sock_net(ctnl
);
2693 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2694 u_int8_t u3
= nfmsg
->nfgen_family
;
2695 struct nf_conntrack_tuple tuple
;
2696 struct nf_conntrack_tuple_hash
*h
;
2698 struct nf_conntrack_zone zone
;
2699 struct netlink_dump_control c
= {
2700 .dump
= ctnetlink_exp_ct_dump_table
,
2701 .done
= ctnetlink_exp_done
,
2704 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2709 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2713 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
2717 ct
= nf_ct_tuplehash_to_ctrack(h
);
2720 err
= netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2727 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2728 const struct nlmsghdr
*nlh
,
2729 const struct nlattr
* const cda
[])
2731 struct net
*net
= sock_net(ctnl
);
2732 struct nf_conntrack_tuple tuple
;
2733 struct nf_conntrack_expect
*exp
;
2734 struct sk_buff
*skb2
;
2735 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2736 u_int8_t u3
= nfmsg
->nfgen_family
;
2737 struct nf_conntrack_zone zone
;
2740 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2741 if (cda
[CTA_EXPECT_MASTER
])
2742 return ctnetlink_dump_exp_ct(ctnl
, skb
, nlh
, cda
);
2744 struct netlink_dump_control c
= {
2745 .dump
= ctnetlink_exp_dump_table
,
2746 .done
= ctnetlink_exp_done
,
2748 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2752 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2756 if (cda
[CTA_EXPECT_TUPLE
])
2757 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2759 else if (cda
[CTA_EXPECT_MASTER
])
2760 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2768 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2772 if (cda
[CTA_EXPECT_ID
]) {
2773 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2774 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2775 nf_ct_expect_put(exp
);
2781 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2783 nf_ct_expect_put(exp
);
2788 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2789 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
2791 nf_ct_expect_put(exp
);
2795 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2804 /* this avoids a loop in nfnetlink. */
2805 return err
== -EAGAIN
? -ENOBUFS
: err
;
2809 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2810 const struct nlmsghdr
*nlh
,
2811 const struct nlattr
* const cda
[])
2813 struct net
*net
= sock_net(ctnl
);
2814 struct nf_conntrack_expect
*exp
;
2815 struct nf_conntrack_tuple tuple
;
2816 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2817 struct hlist_node
*next
;
2818 u_int8_t u3
= nfmsg
->nfgen_family
;
2819 struct nf_conntrack_zone zone
;
2823 if (cda
[CTA_EXPECT_TUPLE
]) {
2824 /* delete a single expect by tuple */
2825 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2829 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2834 /* bump usage count to 2 */
2835 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2839 if (cda
[CTA_EXPECT_ID
]) {
2840 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2841 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2842 nf_ct_expect_put(exp
);
2847 /* after list removal, usage count == 1 */
2848 spin_lock_bh(&nf_conntrack_expect_lock
);
2849 if (del_timer(&exp
->timeout
)) {
2850 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
2852 nf_ct_expect_put(exp
);
2854 spin_unlock_bh(&nf_conntrack_expect_lock
);
2855 /* have to put what we 'get' above.
2856 * after this line usage count == 0 */
2857 nf_ct_expect_put(exp
);
2858 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2859 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2860 struct nf_conn_help
*m_help
;
2862 /* delete all expectations for this helper */
2863 spin_lock_bh(&nf_conntrack_expect_lock
);
2864 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2865 hlist_for_each_entry_safe(exp
, next
,
2866 &net
->ct
.expect_hash
[i
],
2868 m_help
= nfct_help(exp
->master
);
2869 if (!strcmp(m_help
->helper
->name
, name
) &&
2870 del_timer(&exp
->timeout
)) {
2871 nf_ct_unlink_expect_report(exp
,
2872 NETLINK_CB(skb
).portid
,
2874 nf_ct_expect_put(exp
);
2878 spin_unlock_bh(&nf_conntrack_expect_lock
);
2880 /* This basically means we have to flush everything*/
2881 spin_lock_bh(&nf_conntrack_expect_lock
);
2882 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2883 hlist_for_each_entry_safe(exp
, next
,
2884 &net
->ct
.expect_hash
[i
],
2886 if (del_timer(&exp
->timeout
)) {
2887 nf_ct_unlink_expect_report(exp
,
2888 NETLINK_CB(skb
).portid
,
2890 nf_ct_expect_put(exp
);
2894 spin_unlock_bh(&nf_conntrack_expect_lock
);
2900 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2901 const struct nlattr
* const cda
[])
2903 if (cda
[CTA_EXPECT_TIMEOUT
]) {
2904 if (!del_timer(&x
->timeout
))
2907 x
->timeout
.expires
= jiffies
+
2908 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2909 add_timer(&x
->timeout
);
2914 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2915 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2916 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2920 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2921 struct nf_conntrack_expect
*exp
,
2924 #ifdef CONFIG_NF_NAT_NEEDED
2925 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2926 struct nf_conntrack_tuple nat_tuple
= {};
2929 err
= nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2933 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2936 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2937 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
,
2942 exp
->saved_addr
= nat_tuple
.src
.u3
;
2943 exp
->saved_proto
= nat_tuple
.src
.u
;
2944 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2952 static struct nf_conntrack_expect
*
2953 ctnetlink_alloc_expect(const struct nlattr
* const cda
[], struct nf_conn
*ct
,
2954 struct nf_conntrack_helper
*helper
,
2955 struct nf_conntrack_tuple
*tuple
,
2956 struct nf_conntrack_tuple
*mask
)
2958 u_int32_t
class = 0;
2959 struct nf_conntrack_expect
*exp
;
2960 struct nf_conn_help
*help
;
2963 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2964 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
2965 if (class > helper
->expect_class_max
)
2966 return ERR_PTR(-EINVAL
);
2968 exp
= nf_ct_expect_alloc(ct
);
2970 return ERR_PTR(-ENOMEM
);
2972 help
= nfct_help(ct
);
2974 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
2978 exp
->timeout
.expires
=
2979 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2981 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
2982 if (cda
[CTA_EXPECT_FLAGS
]) {
2984 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2987 if (cda
[CTA_EXPECT_FLAGS
]) {
2988 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2989 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
2993 if (cda
[CTA_EXPECT_FN
]) {
2994 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
2995 struct nf_ct_helper_expectfn
*expfn
;
2997 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
2998 if (expfn
== NULL
) {
3002 exp
->expectfn
= expfn
->expectfn
;
3004 exp
->expectfn
= NULL
;
3008 exp
->helper
= helper
;
3009 exp
->tuple
= *tuple
;
3010 exp
->mask
.src
.u3
= mask
->src
.u3
;
3011 exp
->mask
.src
.u
.all
= mask
->src
.u
.all
;
3013 if (cda
[CTA_EXPECT_NAT
]) {
3014 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
3015 exp
, nf_ct_l3num(ct
));
3021 nf_ct_expect_put(exp
);
3022 return ERR_PTR(err
);
3026 ctnetlink_create_expect(struct net
*net
,
3027 const struct nf_conntrack_zone
*zone
,
3028 const struct nlattr
* const cda
[],
3029 u_int8_t u3
, u32 portid
, int report
)
3031 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
3032 struct nf_conntrack_tuple_hash
*h
= NULL
;
3033 struct nf_conntrack_helper
*helper
= NULL
;
3034 struct nf_conntrack_expect
*exp
;
3038 /* caller guarantees that those three CTA_EXPECT_* exist */
3039 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3043 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
,
3047 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
,
3052 /* Look for master conntrack of this expectation */
3053 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
3056 ct
= nf_ct_tuplehash_to_ctrack(h
);
3058 if (cda
[CTA_EXPECT_HELP_NAME
]) {
3059 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3061 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3062 nf_ct_protonum(ct
));
3063 if (helper
== NULL
) {
3064 #ifdef CONFIG_MODULES
3065 if (request_module("nfct-helper-%s", helpname
) < 0) {
3069 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3070 nf_ct_protonum(ct
));
3081 exp
= ctnetlink_alloc_expect(cda
, ct
, helper
, &tuple
, &mask
);
3087 err
= nf_ct_expect_related_report(exp
, portid
, report
);
3088 nf_ct_expect_put(exp
);
3095 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
3096 const struct nlmsghdr
*nlh
,
3097 const struct nlattr
* const cda
[])
3099 struct net
*net
= sock_net(ctnl
);
3100 struct nf_conntrack_tuple tuple
;
3101 struct nf_conntrack_expect
*exp
;
3102 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3103 u_int8_t u3
= nfmsg
->nfgen_family
;
3104 struct nf_conntrack_zone zone
;
3107 if (!cda
[CTA_EXPECT_TUPLE
]
3108 || !cda
[CTA_EXPECT_MASK
]
3109 || !cda
[CTA_EXPECT_MASTER
])
3112 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3116 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3121 spin_lock_bh(&nf_conntrack_expect_lock
);
3122 exp
= __nf_ct_expect_find(net
, &zone
, &tuple
);
3124 spin_unlock_bh(&nf_conntrack_expect_lock
);
3126 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
3127 err
= ctnetlink_create_expect(net
, &zone
, cda
, u3
,
3128 NETLINK_CB(skb
).portid
,
3135 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
3136 err
= ctnetlink_change_expect(exp
, cda
);
3137 spin_unlock_bh(&nf_conntrack_expect_lock
);
3143 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
3144 const struct ip_conntrack_stat
*st
)
3146 struct nlmsghdr
*nlh
;
3147 struct nfgenmsg
*nfmsg
;
3148 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
3150 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_EXP_GET_STATS_CPU
);
3151 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
3155 nfmsg
= nlmsg_data(nlh
);
3156 nfmsg
->nfgen_family
= AF_UNSPEC
;
3157 nfmsg
->version
= NFNETLINK_V0
;
3158 nfmsg
->res_id
= htons(cpu
);
3160 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
3161 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
3162 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
3163 goto nla_put_failure
;
3165 nlmsg_end(skb
, nlh
);
3170 nlmsg_cancel(skb
, nlh
);
3175 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3178 struct net
*net
= sock_net(skb
->sk
);
3180 if (cb
->args
[0] == nr_cpu_ids
)
3183 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
3184 const struct ip_conntrack_stat
*st
;
3186 if (!cpu_possible(cpu
))
3189 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
3190 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3201 ctnetlink_stat_exp_cpu(struct sock
*ctnl
, struct sk_buff
*skb
,
3202 const struct nlmsghdr
*nlh
,
3203 const struct nlattr
* const cda
[])
3205 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3206 struct netlink_dump_control c
= {
3207 .dump
= ctnetlink_exp_stat_cpu_dump
,
3209 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
3215 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3216 static struct nf_ct_event_notifier ctnl_notifier
= {
3217 .fcn
= ctnetlink_conntrack_event
,
3220 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
3221 .fcn
= ctnetlink_expect_event
,
3225 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
3226 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
3227 .attr_count
= CTA_MAX
,
3228 .policy
= ct_nla_policy
},
3229 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
3230 .attr_count
= CTA_MAX
,
3231 .policy
= ct_nla_policy
},
3232 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
3233 .attr_count
= CTA_MAX
,
3234 .policy
= ct_nla_policy
},
3235 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
3236 .attr_count
= CTA_MAX
,
3237 .policy
= ct_nla_policy
},
3238 [IPCTNL_MSG_CT_GET_STATS_CPU
] = { .call
= ctnetlink_stat_ct_cpu
},
3239 [IPCTNL_MSG_CT_GET_STATS
] = { .call
= ctnetlink_stat_ct
},
3240 [IPCTNL_MSG_CT_GET_DYING
] = { .call
= ctnetlink_get_ct_dying
},
3241 [IPCTNL_MSG_CT_GET_UNCONFIRMED
] = { .call
= ctnetlink_get_ct_unconfirmed
},
3244 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
3245 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
3246 .attr_count
= CTA_EXPECT_MAX
,
3247 .policy
= exp_nla_policy
},
3248 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
3249 .attr_count
= CTA_EXPECT_MAX
,
3250 .policy
= exp_nla_policy
},
3251 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
3252 .attr_count
= CTA_EXPECT_MAX
,
3253 .policy
= exp_nla_policy
},
3254 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = { .call
= ctnetlink_stat_exp_cpu
},
3257 static const struct nfnetlink_subsystem ctnl_subsys
= {
3258 .name
= "conntrack",
3259 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
3260 .cb_count
= IPCTNL_MSG_MAX
,
3264 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
3265 .name
= "conntrack_expect",
3266 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
3267 .cb_count
= IPCTNL_MSG_EXP_MAX
,
3271 MODULE_ALIAS("ip_conntrack_netlink");
3272 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
3273 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
3275 static int __net_init
ctnetlink_net_init(struct net
*net
)
3277 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3280 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
3282 pr_err("ctnetlink_init: cannot register notifier.\n");
3286 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
3288 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3289 goto err_unreg_notifier
;
3294 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3296 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3302 static void ctnetlink_net_exit(struct net
*net
)
3304 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3305 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
3306 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3310 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
3314 list_for_each_entry(net
, net_exit_list
, exit_list
)
3315 ctnetlink_net_exit(net
);
3318 static struct pernet_operations ctnetlink_net_ops
= {
3319 .init
= ctnetlink_net_init
,
3320 .exit_batch
= ctnetlink_net_exit_batch
,
3323 static int __init
ctnetlink_init(void)
3327 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
3328 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
3330 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3334 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
3336 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3337 goto err_unreg_subsys
;
3340 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
3342 pr_err("ctnetlink_init: cannot register pernet operations\n");
3343 goto err_unreg_exp_subsys
;
3345 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3346 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3347 RCU_INIT_POINTER(nfq_ct_hook
, &ctnetlink_nfqueue_hook
);
3351 err_unreg_exp_subsys
:
3352 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3354 nfnetlink_subsys_unregister(&ctnl_subsys
);
3359 static void __exit
ctnetlink_exit(void)
3361 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3363 unregister_pernet_subsys(&ctnetlink_net_ops
);
3364 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3365 nfnetlink_subsys_unregister(&ctnl_subsys
);
3366 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3367 RCU_INIT_POINTER(nfq_ct_hook
, NULL
);
3371 module_init(ctnetlink_init
);
3372 module_exit(ctnetlink_exit
);