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 nf_ct_zone_init(zone
, NF_CT_DEFAULT_ZONE_ID
,
960 NF_CT_DEFAULT_ZONE_DIR
, 0);
961 #ifdef CONFIG_NF_CONNTRACK_ZONES
963 zone
->id
= ntohs(nla_get_be16(attr
));
972 ctnetlink_parse_tuple_zone(struct nlattr
*attr
, enum ctattr_type type
,
973 struct nf_conntrack_zone
*zone
)
977 if (zone
->id
!= NF_CT_DEFAULT_ZONE_ID
)
980 ret
= ctnetlink_parse_zone(attr
, zone
);
984 if (type
== CTA_TUPLE_REPLY
)
985 zone
->dir
= NF_CT_ZONE_DIR_REPL
;
987 zone
->dir
= NF_CT_ZONE_DIR_ORIG
;
992 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
993 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
994 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
995 [CTA_TUPLE_ZONE
] = { .type
= NLA_U16
},
999 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
1000 struct nf_conntrack_tuple
*tuple
,
1001 enum ctattr_type type
, u_int8_t l3num
,
1002 struct nf_conntrack_zone
*zone
)
1004 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
1007 memset(tuple
, 0, sizeof(*tuple
));
1009 err
= nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], tuple_nla_policy
);
1013 if (!tb
[CTA_TUPLE_IP
])
1016 tuple
->src
.l3num
= l3num
;
1018 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
1022 if (!tb
[CTA_TUPLE_PROTO
])
1025 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
1029 if (tb
[CTA_TUPLE_ZONE
]) {
1033 err
= ctnetlink_parse_tuple_zone(tb
[CTA_TUPLE_ZONE
],
1039 /* orig and expect tuples get DIR_ORIGINAL */
1040 if (type
== CTA_TUPLE_REPLY
)
1041 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
1043 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
1048 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
1049 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
1050 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1054 ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
,
1055 struct nlattr
**helpinfo
)
1058 struct nlattr
*tb
[CTA_HELP_MAX
+1];
1060 err
= nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, help_nla_policy
);
1064 if (!tb
[CTA_HELP_NAME
])
1067 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
1069 if (tb
[CTA_HELP_INFO
])
1070 *helpinfo
= tb
[CTA_HELP_INFO
];
1075 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
1076 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
1077 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
1078 [CTA_STATUS
] = { .type
= NLA_U32
},
1079 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
1080 [CTA_HELP
] = { .type
= NLA_NESTED
},
1081 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
1082 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
1083 [CTA_MARK
] = { .type
= NLA_U32
},
1084 [CTA_ID
] = { .type
= NLA_U32
},
1085 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
1086 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
1087 [CTA_NAT_SEQ_ADJ_ORIG
] = { .type
= NLA_NESTED
},
1088 [CTA_NAT_SEQ_ADJ_REPLY
] = { .type
= NLA_NESTED
},
1089 [CTA_ZONE
] = { .type
= NLA_U16
},
1090 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
1091 [CTA_LABELS
] = { .type
= NLA_BINARY
,
1092 .len
= NF_CT_LABELS_MAX_SIZE
},
1093 [CTA_LABELS_MASK
] = { .type
= NLA_BINARY
,
1094 .len
= NF_CT_LABELS_MAX_SIZE
},
1097 static int ctnetlink_flush_conntrack(struct net
*net
,
1098 const struct nlattr
* const cda
[],
1099 u32 portid
, int report
)
1101 struct ctnetlink_filter
*filter
= NULL
;
1103 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1104 filter
= ctnetlink_alloc_filter(cda
);
1106 return PTR_ERR(filter
);
1109 nf_ct_iterate_cleanup(net
, ctnetlink_filter_match
, filter
,
1117 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1118 const struct nlmsghdr
*nlh
,
1119 const struct nlattr
* const cda
[])
1121 struct net
*net
= sock_net(ctnl
);
1122 struct nf_conntrack_tuple_hash
*h
;
1123 struct nf_conntrack_tuple tuple
;
1125 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1126 u_int8_t u3
= nfmsg
->nfgen_family
;
1127 struct nf_conntrack_zone zone
;
1130 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1134 if (cda
[CTA_TUPLE_ORIG
])
1135 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1137 else if (cda
[CTA_TUPLE_REPLY
])
1138 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1141 return ctnetlink_flush_conntrack(net
, cda
,
1142 NETLINK_CB(skb
).portid
,
1149 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1153 ct
= nf_ct_tuplehash_to_ctrack(h
);
1156 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
1157 if (id
!= (u32
)(unsigned long)ct
) {
1163 if (del_timer(&ct
->timeout
))
1164 nf_ct_delete(ct
, NETLINK_CB(skb
).portid
, nlmsg_report(nlh
));
1172 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1173 const struct nlmsghdr
*nlh
,
1174 const struct nlattr
* const cda
[])
1176 struct net
*net
= sock_net(ctnl
);
1177 struct nf_conntrack_tuple_hash
*h
;
1178 struct nf_conntrack_tuple tuple
;
1180 struct sk_buff
*skb2
= NULL
;
1181 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1182 u_int8_t u3
= nfmsg
->nfgen_family
;
1183 struct nf_conntrack_zone zone
;
1186 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1187 struct netlink_dump_control c
= {
1188 .dump
= ctnetlink_dump_table
,
1189 .done
= ctnetlink_done
,
1192 if (cda
[CTA_MARK
] && cda
[CTA_MARK_MASK
]) {
1193 struct ctnetlink_filter
*filter
;
1195 filter
= ctnetlink_alloc_filter(cda
);
1197 return PTR_ERR(filter
);
1201 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1204 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1208 if (cda
[CTA_TUPLE_ORIG
])
1209 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1211 else if (cda
[CTA_TUPLE_REPLY
])
1212 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1220 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
1224 ct
= nf_ct_tuplehash_to_ctrack(h
);
1227 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1234 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
1235 NFNL_MSG_TYPE(nlh
->nlmsg_type
), ct
);
1241 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
1250 /* this avoids a loop in nfnetlink. */
1251 return err
== -EAGAIN
? -ENOBUFS
: err
;
1254 static int ctnetlink_done_list(struct netlink_callback
*cb
)
1257 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
1262 ctnetlink_dump_list(struct sk_buff
*skb
, struct netlink_callback
*cb
, bool dying
)
1264 struct nf_conn
*ct
, *last
;
1265 struct nf_conntrack_tuple_hash
*h
;
1266 struct hlist_nulls_node
*n
;
1267 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1268 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1271 struct hlist_nulls_head
*list
;
1272 struct net
*net
= sock_net(skb
->sk
);
1277 last
= (struct nf_conn
*)cb
->args
[1];
1279 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
1280 struct ct_pcpu
*pcpu
;
1282 if (!cpu_possible(cpu
))
1285 pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1286 spin_lock_bh(&pcpu
->lock
);
1287 list
= dying
? &pcpu
->dying
: &pcpu
->unconfirmed
;
1289 hlist_nulls_for_each_entry(h
, n
, list
, hnnode
) {
1290 ct
= nf_ct_tuplehash_to_ctrack(h
);
1291 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
1299 res
= ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1301 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
1305 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
1308 cb
->args
[1] = (unsigned long)ct
;
1309 spin_unlock_bh(&pcpu
->lock
);
1317 spin_unlock_bh(&pcpu
->lock
);
1328 ctnetlink_dump_dying(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1330 return ctnetlink_dump_list(skb
, cb
, true);
1334 ctnetlink_get_ct_dying(struct sock
*ctnl
, struct sk_buff
*skb
,
1335 const struct nlmsghdr
*nlh
,
1336 const struct nlattr
* const cda
[])
1338 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1339 struct netlink_dump_control c
= {
1340 .dump
= ctnetlink_dump_dying
,
1341 .done
= ctnetlink_done_list
,
1343 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1350 ctnetlink_dump_unconfirmed(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1352 return ctnetlink_dump_list(skb
, cb
, false);
1356 ctnetlink_get_ct_unconfirmed(struct sock
*ctnl
, struct sk_buff
*skb
,
1357 const struct nlmsghdr
*nlh
,
1358 const struct nlattr
* const cda
[])
1360 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1361 struct netlink_dump_control c
= {
1362 .dump
= ctnetlink_dump_unconfirmed
,
1363 .done
= ctnetlink_done_list
,
1365 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
1371 #ifdef CONFIG_NF_NAT_NEEDED
1373 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1374 enum nf_nat_manip_type manip
,
1375 const struct nlattr
*attr
)
1377 typeof(nfnetlink_parse_nat_setup_hook
) parse_nat_setup
;
1380 parse_nat_setup
= rcu_dereference(nfnetlink_parse_nat_setup_hook
);
1381 if (!parse_nat_setup
) {
1382 #ifdef CONFIG_MODULES
1384 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1385 if (request_module("nf-nat") < 0) {
1386 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1390 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1392 if (nfnetlink_parse_nat_setup_hook
)
1398 err
= parse_nat_setup(ct
, manip
, attr
);
1399 if (err
== -EAGAIN
) {
1400 #ifdef CONFIG_MODULES
1402 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1403 if (request_module("nf-nat-%u", nf_ct_l3num(ct
)) < 0) {
1404 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1408 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1419 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1422 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1423 d
= ct
->status
^ status
;
1425 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1429 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1430 /* SEEN_REPLY bit can only be set */
1433 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1434 /* ASSURED bit can only be set */
1437 /* Be careful here, modifying NAT bits can screw up things,
1438 * so don't let users modify them directly if they don't pass
1440 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
1445 ctnetlink_setup_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1447 #ifdef CONFIG_NF_NAT_NEEDED
1450 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1453 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_DST
,
1458 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_SRC
,
1462 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1469 ctnetlink_change_helper(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1471 struct nf_conntrack_helper
*helper
;
1472 struct nf_conn_help
*help
= nfct_help(ct
);
1473 char *helpname
= NULL
;
1474 struct nlattr
*helpinfo
= NULL
;
1477 /* don't change helper of sibling connections */
1481 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1485 if (!strcmp(helpname
, "")) {
1486 if (help
&& help
->helper
) {
1487 /* we had a helper before ... */
1488 nf_ct_remove_expectations(ct
);
1489 RCU_INIT_POINTER(help
->helper
, NULL
);
1495 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1496 nf_ct_protonum(ct
));
1497 if (helper
== NULL
) {
1498 #ifdef CONFIG_MODULES
1499 spin_unlock_bh(&nf_conntrack_expect_lock
);
1501 if (request_module("nfct-helper-%s", helpname
) < 0) {
1502 spin_lock_bh(&nf_conntrack_expect_lock
);
1506 spin_lock_bh(&nf_conntrack_expect_lock
);
1507 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1508 nf_ct_protonum(ct
));
1516 if (help
->helper
== helper
) {
1517 /* update private helper data if allowed. */
1518 if (helper
->from_nlattr
)
1519 helper
->from_nlattr(helpinfo
, ct
);
1525 /* we cannot set a helper for an existing conntrack */
1530 ctnetlink_change_timeout(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1532 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1534 if (!del_timer(&ct
->timeout
))
1537 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1538 add_timer(&ct
->timeout
);
1543 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
1544 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
1545 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
1546 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
1550 ctnetlink_change_protoinfo(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1552 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
1553 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
1554 struct nf_conntrack_l4proto
*l4proto
;
1557 err
= nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, protoinfo_policy
);
1562 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1563 if (l4proto
->from_nlattr
)
1564 err
= l4proto
->from_nlattr(tb
, ct
);
1570 static const struct nla_policy seqadj_policy
[CTA_SEQADJ_MAX
+1] = {
1571 [CTA_SEQADJ_CORRECTION_POS
] = { .type
= NLA_U32
},
1572 [CTA_SEQADJ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
1573 [CTA_SEQADJ_OFFSET_AFTER
] = { .type
= NLA_U32
},
1577 change_seq_adj(struct nf_ct_seqadj
*seq
, const struct nlattr
* const attr
)
1580 struct nlattr
*cda
[CTA_SEQADJ_MAX
+1];
1582 err
= nla_parse_nested(cda
, CTA_SEQADJ_MAX
, attr
, seqadj_policy
);
1586 if (!cda
[CTA_SEQADJ_CORRECTION_POS
])
1589 seq
->correction_pos
=
1590 ntohl(nla_get_be32(cda
[CTA_SEQADJ_CORRECTION_POS
]));
1592 if (!cda
[CTA_SEQADJ_OFFSET_BEFORE
])
1595 seq
->offset_before
=
1596 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_BEFORE
]));
1598 if (!cda
[CTA_SEQADJ_OFFSET_AFTER
])
1602 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_AFTER
]));
1608 ctnetlink_change_seq_adj(struct nf_conn
*ct
,
1609 const struct nlattr
* const cda
[])
1611 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
1617 if (cda
[CTA_SEQ_ADJ_ORIG
]) {
1618 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_ORIGINAL
],
1619 cda
[CTA_SEQ_ADJ_ORIG
]);
1623 ct
->status
|= IPS_SEQ_ADJUST
;
1626 if (cda
[CTA_SEQ_ADJ_REPLY
]) {
1627 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_REPLY
],
1628 cda
[CTA_SEQ_ADJ_REPLY
]);
1632 ct
->status
|= IPS_SEQ_ADJUST
;
1639 ctnetlink_attach_labels(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1641 #ifdef CONFIG_NF_CONNTRACK_LABELS
1642 size_t len
= nla_len(cda
[CTA_LABELS
]);
1643 const void *mask
= cda
[CTA_LABELS_MASK
];
1645 if (len
& (sizeof(u32
)-1)) /* must be multiple of u32 */
1649 if (nla_len(cda
[CTA_LABELS_MASK
]) == 0 ||
1650 nla_len(cda
[CTA_LABELS_MASK
]) != len
)
1652 mask
= nla_data(cda
[CTA_LABELS_MASK
]);
1657 return nf_connlabels_replace(ct
, nla_data(cda
[CTA_LABELS
]), mask
, len
);
1664 ctnetlink_change_conntrack(struct nf_conn
*ct
,
1665 const struct nlattr
* const cda
[])
1669 /* only allow NAT changes and master assignation for new conntracks */
1670 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
1673 if (cda
[CTA_HELP
]) {
1674 err
= ctnetlink_change_helper(ct
, cda
);
1679 if (cda
[CTA_TIMEOUT
]) {
1680 err
= ctnetlink_change_timeout(ct
, cda
);
1685 if (cda
[CTA_STATUS
]) {
1686 err
= ctnetlink_change_status(ct
, cda
);
1691 if (cda
[CTA_PROTOINFO
]) {
1692 err
= ctnetlink_change_protoinfo(ct
, cda
);
1697 #if defined(CONFIG_NF_CONNTRACK_MARK)
1699 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1702 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1703 err
= ctnetlink_change_seq_adj(ct
, cda
);
1708 if (cda
[CTA_LABELS
]) {
1709 err
= ctnetlink_attach_labels(ct
, cda
);
1717 static struct nf_conn
*
1718 ctnetlink_create_conntrack(struct net
*net
,
1719 const struct nf_conntrack_zone
*zone
,
1720 const struct nlattr
* const cda
[],
1721 struct nf_conntrack_tuple
*otuple
,
1722 struct nf_conntrack_tuple
*rtuple
,
1727 struct nf_conntrack_helper
*helper
;
1728 struct nf_conn_tstamp
*tstamp
;
1730 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
1732 return ERR_PTR(-ENOMEM
);
1734 if (!cda
[CTA_TIMEOUT
])
1736 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1738 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1741 if (cda
[CTA_HELP
]) {
1742 char *helpname
= NULL
;
1743 struct nlattr
*helpinfo
= NULL
;
1745 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1749 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1750 nf_ct_protonum(ct
));
1751 if (helper
== NULL
) {
1753 #ifdef CONFIG_MODULES
1754 if (request_module("nfct-helper-%s", helpname
) < 0) {
1760 helper
= __nf_conntrack_helper_find(helpname
,
1762 nf_ct_protonum(ct
));
1772 struct nf_conn_help
*help
;
1774 help
= nf_ct_helper_ext_add(ct
, helper
, GFP_ATOMIC
);
1779 /* set private helper data if allowed. */
1780 if (helper
->from_nlattr
)
1781 helper
->from_nlattr(helpinfo
, ct
);
1783 /* not in hash table yet so not strictly necessary */
1784 RCU_INIT_POINTER(help
->helper
, helper
);
1787 /* try an implicit helper assignation */
1788 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1793 err
= ctnetlink_setup_nat(ct
, cda
);
1797 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
1798 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
1799 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
1800 nf_ct_labels_ext_add(ct
);
1802 /* we must add conntrack extensions before confirmation. */
1803 ct
->status
|= IPS_CONFIRMED
;
1805 if (cda
[CTA_STATUS
]) {
1806 err
= ctnetlink_change_status(ct
, cda
);
1811 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
1812 err
= ctnetlink_change_seq_adj(ct
, cda
);
1817 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
1818 if (cda
[CTA_PROTOINFO
]) {
1819 err
= ctnetlink_change_protoinfo(ct
, cda
);
1824 #if defined(CONFIG_NF_CONNTRACK_MARK)
1826 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1829 /* setup master conntrack: this is a confirmed expectation */
1830 if (cda
[CTA_TUPLE_MASTER
]) {
1831 struct nf_conntrack_tuple master
;
1832 struct nf_conntrack_tuple_hash
*master_h
;
1833 struct nf_conn
*master_ct
;
1835 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
,
1840 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
1841 if (master_h
== NULL
) {
1845 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1846 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1847 ct
->master
= master_ct
;
1849 tstamp
= nf_conn_tstamp_find(ct
);
1851 tstamp
->start
= ktime_get_real_ns();
1853 err
= nf_conntrack_hash_check_insert(ct
);
1864 nf_conntrack_free(ct
);
1865 return ERR_PTR(err
);
1869 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1870 const struct nlmsghdr
*nlh
,
1871 const struct nlattr
* const cda
[])
1873 struct net
*net
= sock_net(ctnl
);
1874 struct nf_conntrack_tuple otuple
, rtuple
;
1875 struct nf_conntrack_tuple_hash
*h
= NULL
;
1876 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1878 u_int8_t u3
= nfmsg
->nfgen_family
;
1879 struct nf_conntrack_zone zone
;
1882 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1886 if (cda
[CTA_TUPLE_ORIG
]) {
1887 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
,
1893 if (cda
[CTA_TUPLE_REPLY
]) {
1894 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
,
1900 if (cda
[CTA_TUPLE_ORIG
])
1901 h
= nf_conntrack_find_get(net
, &zone
, &otuple
);
1902 else if (cda
[CTA_TUPLE_REPLY
])
1903 h
= nf_conntrack_find_get(net
, &zone
, &rtuple
);
1907 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
1908 enum ip_conntrack_events events
;
1910 if (!cda
[CTA_TUPLE_ORIG
] || !cda
[CTA_TUPLE_REPLY
])
1913 ct
= ctnetlink_create_conntrack(net
, &zone
, cda
, &otuple
,
1919 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1920 events
= IPCT_RELATED
;
1924 if (cda
[CTA_LABELS
] &&
1925 ctnetlink_attach_labels(ct
, cda
) == 0)
1926 events
|= (1 << IPCT_LABEL
);
1928 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1929 (1 << IPCT_ASSURED
) |
1930 (1 << IPCT_HELPER
) |
1931 (1 << IPCT_PROTOINFO
) |
1932 (1 << IPCT_SEQADJ
) |
1933 (1 << IPCT_MARK
) | events
,
1934 ct
, NETLINK_CB(skb
).portid
,
1941 /* implicit 'else' */
1944 ct
= nf_ct_tuplehash_to_ctrack(h
);
1945 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1946 spin_lock_bh(&nf_conntrack_expect_lock
);
1947 err
= ctnetlink_change_conntrack(ct
, cda
);
1948 spin_unlock_bh(&nf_conntrack_expect_lock
);
1950 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
1951 (1 << IPCT_ASSURED
) |
1952 (1 << IPCT_HELPER
) |
1954 (1 << IPCT_PROTOINFO
) |
1955 (1 << IPCT_SEQADJ
) |
1957 ct
, NETLINK_CB(skb
).portid
,
1967 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1968 __u16 cpu
, const struct ip_conntrack_stat
*st
)
1970 struct nlmsghdr
*nlh
;
1971 struct nfgenmsg
*nfmsg
;
1972 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
1974 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS_CPU
);
1975 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
1979 nfmsg
= nlmsg_data(nlh
);
1980 nfmsg
->nfgen_family
= AF_UNSPEC
;
1981 nfmsg
->version
= NFNETLINK_V0
;
1982 nfmsg
->res_id
= htons(cpu
);
1984 if (nla_put_be32(skb
, CTA_STATS_SEARCHED
, htonl(st
->searched
)) ||
1985 nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
1986 nla_put_be32(skb
, CTA_STATS_NEW
, htonl(st
->new)) ||
1987 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
1988 nla_put_be32(skb
, CTA_STATS_IGNORE
, htonl(st
->ignore
)) ||
1989 nla_put_be32(skb
, CTA_STATS_DELETE
, htonl(st
->delete)) ||
1990 nla_put_be32(skb
, CTA_STATS_DELETE_LIST
, htonl(st
->delete_list
)) ||
1991 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
1992 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
1993 htonl(st
->insert_failed
)) ||
1994 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
1995 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
1996 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
1997 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
1998 htonl(st
->search_restart
)))
1999 goto nla_put_failure
;
2001 nlmsg_end(skb
, nlh
);
2006 nlmsg_cancel(skb
, nlh
);
2011 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2014 struct net
*net
= sock_net(skb
->sk
);
2016 if (cb
->args
[0] == nr_cpu_ids
)
2019 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2020 const struct ip_conntrack_stat
*st
;
2022 if (!cpu_possible(cpu
))
2025 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2026 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
2027 NETLINK_CB(cb
->skb
).portid
,
2038 ctnetlink_stat_ct_cpu(struct sock
*ctnl
, struct sk_buff
*skb
,
2039 const struct nlmsghdr
*nlh
,
2040 const struct nlattr
* const cda
[])
2042 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2043 struct netlink_dump_control c
= {
2044 .dump
= ctnetlink_ct_stat_cpu_dump
,
2046 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2053 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
2056 struct nlmsghdr
*nlh
;
2057 struct nfgenmsg
*nfmsg
;
2058 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2059 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
2061 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_CT_GET_STATS
);
2062 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2066 nfmsg
= nlmsg_data(nlh
);
2067 nfmsg
->nfgen_family
= AF_UNSPEC
;
2068 nfmsg
->version
= NFNETLINK_V0
;
2071 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
2072 goto nla_put_failure
;
2074 nlmsg_end(skb
, nlh
);
2079 nlmsg_cancel(skb
, nlh
);
2084 ctnetlink_stat_ct(struct sock
*ctnl
, struct sk_buff
*skb
,
2085 const struct nlmsghdr
*nlh
,
2086 const struct nlattr
* const cda
[])
2088 struct sk_buff
*skb2
;
2091 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2095 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2097 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
2102 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2111 /* this avoids a loop in nfnetlink. */
2112 return err
== -EAGAIN
? -ENOBUFS
: err
;
2115 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2116 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2117 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2118 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2119 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2120 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2121 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
2122 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
2123 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2124 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2125 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2126 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2127 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2130 static struct nf_conntrack_expect
*
2131 ctnetlink_alloc_expect(const struct nlattr
*const cda
[], struct nf_conn
*ct
,
2132 struct nf_conntrack_helper
*helper
,
2133 struct nf_conntrack_tuple
*tuple
,
2134 struct nf_conntrack_tuple
*mask
);
2136 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2138 ctnetlink_nfqueue_build_size(const struct nf_conn
*ct
)
2140 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2141 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2142 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2143 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
2144 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
2145 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
2146 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
2147 + nla_total_size(0) /* CTA_PROTOINFO */
2148 + nla_total_size(0) /* CTA_HELP */
2149 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
2150 + ctnetlink_secctx_size(ct
)
2151 #ifdef CONFIG_NF_NAT_NEEDED
2152 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2153 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
2155 #ifdef CONFIG_NF_CONNTRACK_MARK
2156 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
2158 #ifdef CONFIG_NF_CONNTRACK_ZONES
2159 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2161 + ctnetlink_proto_size(ct
)
2166 ctnetlink_nfqueue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
2168 const struct nf_conntrack_zone
*zone
;
2169 struct nlattr
*nest_parms
;
2172 zone
= nf_ct_zone(ct
);
2174 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
2176 goto nla_put_failure
;
2177 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
2178 goto nla_put_failure
;
2179 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2180 NF_CT_ZONE_DIR_ORIG
) < 0)
2181 goto nla_put_failure
;
2182 nla_nest_end(skb
, nest_parms
);
2184 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
2186 goto nla_put_failure
;
2187 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
2188 goto nla_put_failure
;
2189 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2190 NF_CT_ZONE_DIR_REPL
) < 0)
2191 goto nla_put_failure
;
2192 nla_nest_end(skb
, nest_parms
);
2194 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
2195 NF_CT_DEFAULT_ZONE_DIR
) < 0)
2196 goto nla_put_failure
;
2198 if (ctnetlink_dump_id(skb
, ct
) < 0)
2199 goto nla_put_failure
;
2201 if (ctnetlink_dump_status(skb
, ct
) < 0)
2202 goto nla_put_failure
;
2204 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
2205 goto nla_put_failure
;
2207 if (ctnetlink_dump_protoinfo(skb
, ct
) < 0)
2208 goto nla_put_failure
;
2210 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
2211 goto nla_put_failure
;
2213 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2214 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
2215 goto nla_put_failure
;
2217 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
2218 goto nla_put_failure
;
2220 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
2221 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
2222 goto nla_put_failure
;
2224 #ifdef CONFIG_NF_CONNTRACK_MARK
2225 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
2226 goto nla_put_failure
;
2228 if (ctnetlink_dump_labels(skb
, ct
) < 0)
2229 goto nla_put_failure
;
2239 ctnetlink_nfqueue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
2243 if (cda
[CTA_TIMEOUT
]) {
2244 err
= ctnetlink_change_timeout(ct
, cda
);
2248 if (cda
[CTA_STATUS
]) {
2249 err
= ctnetlink_change_status(ct
, cda
);
2253 if (cda
[CTA_HELP
]) {
2254 err
= ctnetlink_change_helper(ct
, cda
);
2258 if (cda
[CTA_LABELS
]) {
2259 err
= ctnetlink_attach_labels(ct
, cda
);
2263 #if defined(CONFIG_NF_CONNTRACK_MARK)
2264 if (cda
[CTA_MARK
]) {
2265 u32 mask
= 0, mark
, newmark
;
2266 if (cda
[CTA_MARK_MASK
])
2267 mask
= ~ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
2269 mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
2270 newmark
= (ct
->mark
& mask
) ^ mark
;
2271 if (newmark
!= ct
->mark
)
2279 ctnetlink_nfqueue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
2281 struct nlattr
*cda
[CTA_MAX
+1];
2284 ret
= nla_parse_nested(cda
, CTA_MAX
, attr
, ct_nla_policy
);
2288 spin_lock_bh(&nf_conntrack_expect_lock
);
2289 ret
= ctnetlink_nfqueue_parse_ct((const struct nlattr
**)cda
, ct
);
2290 spin_unlock_bh(&nf_conntrack_expect_lock
);
2295 static int ctnetlink_nfqueue_exp_parse(const struct nlattr
* const *cda
,
2296 const struct nf_conn
*ct
,
2297 struct nf_conntrack_tuple
*tuple
,
2298 struct nf_conntrack_tuple
*mask
)
2302 err
= ctnetlink_parse_tuple(cda
, tuple
, CTA_EXPECT_TUPLE
,
2303 nf_ct_l3num(ct
), NULL
);
2307 return ctnetlink_parse_tuple(cda
, mask
, CTA_EXPECT_MASK
,
2308 nf_ct_l3num(ct
), NULL
);
2312 ctnetlink_nfqueue_attach_expect(const struct nlattr
*attr
, struct nf_conn
*ct
,
2313 u32 portid
, u32 report
)
2315 struct nlattr
*cda
[CTA_EXPECT_MAX
+1];
2316 struct nf_conntrack_tuple tuple
, mask
;
2317 struct nf_conntrack_helper
*helper
= NULL
;
2318 struct nf_conntrack_expect
*exp
;
2321 err
= nla_parse_nested(cda
, CTA_EXPECT_MAX
, attr
, exp_nla_policy
);
2325 err
= ctnetlink_nfqueue_exp_parse((const struct nlattr
* const *)cda
,
2330 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2331 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2333 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2334 nf_ct_protonum(ct
));
2339 exp
= ctnetlink_alloc_expect((const struct nlattr
* const *)cda
, ct
,
2340 helper
, &tuple
, &mask
);
2342 return PTR_ERR(exp
);
2344 err
= nf_ct_expect_related_report(exp
, portid
, report
);
2346 nf_ct_expect_put(exp
);
2353 static struct nfq_ct_hook ctnetlink_nfqueue_hook
= {
2354 .build_size
= ctnetlink_nfqueue_build_size
,
2355 .build
= ctnetlink_nfqueue_build
,
2356 .parse
= ctnetlink_nfqueue_parse
,
2357 .attach_expect
= ctnetlink_nfqueue_attach_expect
,
2358 .seq_adjust
= nf_ct_tcp_seqadj_set
,
2360 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2362 /***********************************************************************
2364 ***********************************************************************/
2367 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
2368 const struct nf_conntrack_tuple
*tuple
,
2369 enum ctattr_expect type
)
2371 struct nlattr
*nest_parms
;
2373 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
2375 goto nla_put_failure
;
2376 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
2377 goto nla_put_failure
;
2378 nla_nest_end(skb
, nest_parms
);
2387 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
2388 const struct nf_conntrack_tuple
*tuple
,
2389 const struct nf_conntrack_tuple_mask
*mask
)
2392 struct nf_conntrack_l3proto
*l3proto
;
2393 struct nf_conntrack_l4proto
*l4proto
;
2394 struct nf_conntrack_tuple m
;
2395 struct nlattr
*nest_parms
;
2397 memset(&m
, 0xFF, sizeof(m
));
2398 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
2399 m
.src
.u
.all
= mask
->src
.u
.all
;
2400 m
.dst
.protonum
= tuple
->dst
.protonum
;
2402 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
2404 goto nla_put_failure
;
2407 l3proto
= __nf_ct_l3proto_find(tuple
->src
.l3num
);
2408 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
2410 l4proto
= __nf_ct_l4proto_find(tuple
->src
.l3num
,
2411 tuple
->dst
.protonum
);
2412 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
2416 if (unlikely(ret
< 0))
2417 goto nla_put_failure
;
2419 nla_nest_end(skb
, nest_parms
);
2427 static const union nf_inet_addr any_addr
;
2430 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
2431 const struct nf_conntrack_expect
*exp
)
2433 struct nf_conn
*master
= exp
->master
;
2434 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
2435 struct nf_conn_help
*help
;
2436 #ifdef CONFIG_NF_NAT_NEEDED
2437 struct nlattr
*nest_parms
;
2438 struct nf_conntrack_tuple nat_tuple
= {};
2440 struct nf_ct_helper_expectfn
*expfn
;
2445 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
2446 goto nla_put_failure
;
2447 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
2448 goto nla_put_failure
;
2449 if (ctnetlink_exp_dump_tuple(skb
,
2450 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
2451 CTA_EXPECT_MASTER
) < 0)
2452 goto nla_put_failure
;
2454 #ifdef CONFIG_NF_NAT_NEEDED
2455 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
2456 exp
->saved_proto
.all
) {
2457 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
| NLA_F_NESTED
);
2459 goto nla_put_failure
;
2461 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
2462 goto nla_put_failure
;
2464 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
2465 nat_tuple
.src
.u3
= exp
->saved_addr
;
2466 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
2467 nat_tuple
.src
.u
= exp
->saved_proto
;
2469 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
2470 CTA_EXPECT_NAT_TUPLE
) < 0)
2471 goto nla_put_failure
;
2472 nla_nest_end(skb
, nest_parms
);
2475 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
2476 nla_put_be32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
)) ||
2477 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
2478 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
2479 goto nla_put_failure
;
2480 help
= nfct_help(master
);
2482 struct nf_conntrack_helper
*helper
;
2484 helper
= rcu_dereference(help
->helper
);
2486 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
2487 goto nla_put_failure
;
2489 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
2490 if (expfn
!= NULL
&&
2491 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
2492 goto nla_put_failure
;
2501 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2502 int event
, const struct nf_conntrack_expect
*exp
)
2504 struct nlmsghdr
*nlh
;
2505 struct nfgenmsg
*nfmsg
;
2506 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
2508 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2509 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
2513 nfmsg
= nlmsg_data(nlh
);
2514 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2515 nfmsg
->version
= NFNETLINK_V0
;
2518 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2519 goto nla_put_failure
;
2521 nlmsg_end(skb
, nlh
);
2526 nlmsg_cancel(skb
, nlh
);
2530 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2532 ctnetlink_expect_event(unsigned int events
, struct nf_exp_event
*item
)
2534 struct nf_conntrack_expect
*exp
= item
->exp
;
2535 struct net
*net
= nf_ct_exp_net(exp
);
2536 struct nlmsghdr
*nlh
;
2537 struct nfgenmsg
*nfmsg
;
2538 struct sk_buff
*skb
;
2539 unsigned int type
, group
;
2542 if (events
& (1 << IPEXP_DESTROY
)) {
2543 type
= IPCTNL_MSG_EXP_DELETE
;
2544 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
2545 } else if (events
& (1 << IPEXP_NEW
)) {
2546 type
= IPCTNL_MSG_EXP_NEW
;
2547 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
2548 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
2552 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
2555 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
2559 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
2560 nlh
= nlmsg_put(skb
, item
->portid
, 0, type
, sizeof(*nfmsg
), flags
);
2564 nfmsg
= nlmsg_data(nlh
);
2565 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
2566 nfmsg
->version
= NFNETLINK_V0
;
2570 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
2571 goto nla_put_failure
;
2574 nlmsg_end(skb
, nlh
);
2575 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
2580 nlmsg_cancel(skb
, nlh
);
2584 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
2588 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
2591 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
2596 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2598 struct net
*net
= sock_net(skb
->sk
);
2599 struct nf_conntrack_expect
*exp
, *last
;
2600 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2601 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2604 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2605 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
2607 hlist_for_each_entry(exp
, &net
->ct
.expect_hash
[cb
->args
[0]],
2609 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2616 if (ctnetlink_exp_fill_info(skb
,
2617 NETLINK_CB(cb
->skb
).portid
,
2621 if (!atomic_inc_not_zero(&exp
->use
))
2623 cb
->args
[1] = (unsigned long)exp
;
2635 nf_ct_expect_put(last
);
2641 ctnetlink_exp_ct_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2643 struct nf_conntrack_expect
*exp
, *last
;
2644 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2645 struct nf_conn
*ct
= cb
->data
;
2646 struct nf_conn_help
*help
= nfct_help(ct
);
2647 u_int8_t l3proto
= nfmsg
->nfgen_family
;
2653 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
2655 hlist_for_each_entry(exp
, &help
->expectations
, lnode
) {
2656 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
2663 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
2667 if (!atomic_inc_not_zero(&exp
->use
))
2669 cb
->args
[1] = (unsigned long)exp
;
2681 nf_ct_expect_put(last
);
2686 static int ctnetlink_dump_exp_ct(struct sock
*ctnl
, struct sk_buff
*skb
,
2687 const struct nlmsghdr
*nlh
,
2688 const struct nlattr
* const cda
[])
2691 struct net
*net
= sock_net(ctnl
);
2692 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2693 u_int8_t u3
= nfmsg
->nfgen_family
;
2694 struct nf_conntrack_tuple tuple
;
2695 struct nf_conntrack_tuple_hash
*h
;
2697 struct nf_conntrack_zone zone
;
2698 struct netlink_dump_control c
= {
2699 .dump
= ctnetlink_exp_ct_dump_table
,
2700 .done
= ctnetlink_exp_done
,
2703 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2708 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2712 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
2716 ct
= nf_ct_tuplehash_to_ctrack(h
);
2719 err
= netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2726 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2727 const struct nlmsghdr
*nlh
,
2728 const struct nlattr
* const cda
[])
2730 struct net
*net
= sock_net(ctnl
);
2731 struct nf_conntrack_tuple tuple
;
2732 struct nf_conntrack_expect
*exp
;
2733 struct sk_buff
*skb2
;
2734 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2735 u_int8_t u3
= nfmsg
->nfgen_family
;
2736 struct nf_conntrack_zone zone
;
2739 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2740 if (cda
[CTA_EXPECT_MASTER
])
2741 return ctnetlink_dump_exp_ct(ctnl
, skb
, nlh
, cda
);
2743 struct netlink_dump_control c
= {
2744 .dump
= ctnetlink_exp_dump_table
,
2745 .done
= ctnetlink_exp_done
,
2747 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
2751 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2755 if (cda
[CTA_EXPECT_TUPLE
])
2756 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2758 else if (cda
[CTA_EXPECT_MASTER
])
2759 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
2767 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2771 if (cda
[CTA_EXPECT_ID
]) {
2772 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2773 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2774 nf_ct_expect_put(exp
);
2780 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2782 nf_ct_expect_put(exp
);
2787 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2788 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
, exp
);
2790 nf_ct_expect_put(exp
);
2794 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).portid
, MSG_DONTWAIT
);
2803 /* this avoids a loop in nfnetlink. */
2804 return err
== -EAGAIN
? -ENOBUFS
: err
;
2808 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
2809 const struct nlmsghdr
*nlh
,
2810 const struct nlattr
* const cda
[])
2812 struct net
*net
= sock_net(ctnl
);
2813 struct nf_conntrack_expect
*exp
;
2814 struct nf_conntrack_tuple tuple
;
2815 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2816 struct hlist_node
*next
;
2817 u_int8_t u3
= nfmsg
->nfgen_family
;
2818 struct nf_conntrack_zone zone
;
2822 if (cda
[CTA_EXPECT_TUPLE
]) {
2823 /* delete a single expect by tuple */
2824 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
2828 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
2833 /* bump usage count to 2 */
2834 exp
= nf_ct_expect_find_get(net
, &zone
, &tuple
);
2838 if (cda
[CTA_EXPECT_ID
]) {
2839 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
2840 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
2841 nf_ct_expect_put(exp
);
2846 /* after list removal, usage count == 1 */
2847 spin_lock_bh(&nf_conntrack_expect_lock
);
2848 if (del_timer(&exp
->timeout
)) {
2849 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
2851 nf_ct_expect_put(exp
);
2853 spin_unlock_bh(&nf_conntrack_expect_lock
);
2854 /* have to put what we 'get' above.
2855 * after this line usage count == 0 */
2856 nf_ct_expect_put(exp
);
2857 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
2858 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2859 struct nf_conn_help
*m_help
;
2861 /* delete all expectations for this helper */
2862 spin_lock_bh(&nf_conntrack_expect_lock
);
2863 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2864 hlist_for_each_entry_safe(exp
, next
,
2865 &net
->ct
.expect_hash
[i
],
2867 m_help
= nfct_help(exp
->master
);
2868 if (!strcmp(m_help
->helper
->name
, name
) &&
2869 del_timer(&exp
->timeout
)) {
2870 nf_ct_unlink_expect_report(exp
,
2871 NETLINK_CB(skb
).portid
,
2873 nf_ct_expect_put(exp
);
2877 spin_unlock_bh(&nf_conntrack_expect_lock
);
2879 /* This basically means we have to flush everything*/
2880 spin_lock_bh(&nf_conntrack_expect_lock
);
2881 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
2882 hlist_for_each_entry_safe(exp
, next
,
2883 &net
->ct
.expect_hash
[i
],
2885 if (del_timer(&exp
->timeout
)) {
2886 nf_ct_unlink_expect_report(exp
,
2887 NETLINK_CB(skb
).portid
,
2889 nf_ct_expect_put(exp
);
2893 spin_unlock_bh(&nf_conntrack_expect_lock
);
2899 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
2900 const struct nlattr
* const cda
[])
2902 if (cda
[CTA_EXPECT_TIMEOUT
]) {
2903 if (!del_timer(&x
->timeout
))
2906 x
->timeout
.expires
= jiffies
+
2907 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2908 add_timer(&x
->timeout
);
2913 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
2914 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
2915 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
2919 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
2920 struct nf_conntrack_expect
*exp
,
2923 #ifdef CONFIG_NF_NAT_NEEDED
2924 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
2925 struct nf_conntrack_tuple nat_tuple
= {};
2928 err
= nla_parse_nested(tb
, CTA_EXPECT_NAT_MAX
, attr
, exp_nat_nla_policy
);
2932 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
2935 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
2936 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
,
2941 exp
->saved_addr
= nat_tuple
.src
.u3
;
2942 exp
->saved_proto
= nat_tuple
.src
.u
;
2943 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
2951 static struct nf_conntrack_expect
*
2952 ctnetlink_alloc_expect(const struct nlattr
* const cda
[], struct nf_conn
*ct
,
2953 struct nf_conntrack_helper
*helper
,
2954 struct nf_conntrack_tuple
*tuple
,
2955 struct nf_conntrack_tuple
*mask
)
2957 u_int32_t
class = 0;
2958 struct nf_conntrack_expect
*exp
;
2959 struct nf_conn_help
*help
;
2962 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
2963 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
2964 if (class > helper
->expect_class_max
)
2965 return ERR_PTR(-EINVAL
);
2967 exp
= nf_ct_expect_alloc(ct
);
2969 return ERR_PTR(-ENOMEM
);
2971 help
= nfct_help(ct
);
2973 if (!cda
[CTA_EXPECT_TIMEOUT
]) {
2977 exp
->timeout
.expires
=
2978 jiffies
+ ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
2980 exp
->flags
= NF_CT_EXPECT_USERSPACE
;
2981 if (cda
[CTA_EXPECT_FLAGS
]) {
2983 ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2986 if (cda
[CTA_EXPECT_FLAGS
]) {
2987 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
2988 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
2992 if (cda
[CTA_EXPECT_FN
]) {
2993 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
2994 struct nf_ct_helper_expectfn
*expfn
;
2996 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
2997 if (expfn
== NULL
) {
3001 exp
->expectfn
= expfn
->expectfn
;
3003 exp
->expectfn
= NULL
;
3007 exp
->helper
= helper
;
3008 exp
->tuple
= *tuple
;
3009 exp
->mask
.src
.u3
= mask
->src
.u3
;
3010 exp
->mask
.src
.u
.all
= mask
->src
.u
.all
;
3012 if (cda
[CTA_EXPECT_NAT
]) {
3013 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
3014 exp
, nf_ct_l3num(ct
));
3020 nf_ct_expect_put(exp
);
3021 return ERR_PTR(err
);
3025 ctnetlink_create_expect(struct net
*net
,
3026 const struct nf_conntrack_zone
*zone
,
3027 const struct nlattr
* const cda
[],
3028 u_int8_t u3
, u32 portid
, int report
)
3030 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
3031 struct nf_conntrack_tuple_hash
*h
= NULL
;
3032 struct nf_conntrack_helper
*helper
= NULL
;
3033 struct nf_conntrack_expect
*exp
;
3037 /* caller guarantees that those three CTA_EXPECT_* exist */
3038 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3042 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
,
3046 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
,
3051 /* Look for master conntrack of this expectation */
3052 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
3055 ct
= nf_ct_tuplehash_to_ctrack(h
);
3057 if (cda
[CTA_EXPECT_HELP_NAME
]) {
3058 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3060 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3061 nf_ct_protonum(ct
));
3062 if (helper
== NULL
) {
3063 #ifdef CONFIG_MODULES
3064 if (request_module("nfct-helper-%s", helpname
) < 0) {
3068 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3069 nf_ct_protonum(ct
));
3080 exp
= ctnetlink_alloc_expect(cda
, ct
, helper
, &tuple
, &mask
);
3086 err
= nf_ct_expect_related_report(exp
, portid
, report
);
3087 nf_ct_expect_put(exp
);
3094 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
3095 const struct nlmsghdr
*nlh
,
3096 const struct nlattr
* const cda
[])
3098 struct net
*net
= sock_net(ctnl
);
3099 struct nf_conntrack_tuple tuple
;
3100 struct nf_conntrack_expect
*exp
;
3101 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3102 u_int8_t u3
= nfmsg
->nfgen_family
;
3103 struct nf_conntrack_zone zone
;
3106 if (!cda
[CTA_EXPECT_TUPLE
]
3107 || !cda
[CTA_EXPECT_MASK
]
3108 || !cda
[CTA_EXPECT_MASTER
])
3111 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3115 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3120 spin_lock_bh(&nf_conntrack_expect_lock
);
3121 exp
= __nf_ct_expect_find(net
, &zone
, &tuple
);
3123 spin_unlock_bh(&nf_conntrack_expect_lock
);
3125 if (nlh
->nlmsg_flags
& NLM_F_CREATE
) {
3126 err
= ctnetlink_create_expect(net
, &zone
, cda
, u3
,
3127 NETLINK_CB(skb
).portid
,
3134 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
3135 err
= ctnetlink_change_expect(exp
, cda
);
3136 spin_unlock_bh(&nf_conntrack_expect_lock
);
3142 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
3143 const struct ip_conntrack_stat
*st
)
3145 struct nlmsghdr
*nlh
;
3146 struct nfgenmsg
*nfmsg
;
3147 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
3149 event
= (NFNL_SUBSYS_CTNETLINK
<< 8 | IPCTNL_MSG_EXP_GET_STATS_CPU
);
3150 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
3154 nfmsg
= nlmsg_data(nlh
);
3155 nfmsg
->nfgen_family
= AF_UNSPEC
;
3156 nfmsg
->version
= NFNETLINK_V0
;
3157 nfmsg
->res_id
= htons(cpu
);
3159 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
3160 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
3161 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
3162 goto nla_put_failure
;
3164 nlmsg_end(skb
, nlh
);
3169 nlmsg_cancel(skb
, nlh
);
3174 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3177 struct net
*net
= sock_net(skb
->sk
);
3179 if (cb
->args
[0] == nr_cpu_ids
)
3182 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
3183 const struct ip_conntrack_stat
*st
;
3185 if (!cpu_possible(cpu
))
3188 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
3189 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3200 ctnetlink_stat_exp_cpu(struct sock
*ctnl
, struct sk_buff
*skb
,
3201 const struct nlmsghdr
*nlh
,
3202 const struct nlattr
* const cda
[])
3204 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3205 struct netlink_dump_control c
= {
3206 .dump
= ctnetlink_exp_stat_cpu_dump
,
3208 return netlink_dump_start(ctnl
, skb
, nlh
, &c
);
3214 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3215 static struct nf_ct_event_notifier ctnl_notifier
= {
3216 .fcn
= ctnetlink_conntrack_event
,
3219 static struct nf_exp_event_notifier ctnl_notifier_exp
= {
3220 .fcn
= ctnetlink_expect_event
,
3224 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
3225 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
3226 .attr_count
= CTA_MAX
,
3227 .policy
= ct_nla_policy
},
3228 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
3229 .attr_count
= CTA_MAX
,
3230 .policy
= ct_nla_policy
},
3231 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
3232 .attr_count
= CTA_MAX
,
3233 .policy
= ct_nla_policy
},
3234 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
3235 .attr_count
= CTA_MAX
,
3236 .policy
= ct_nla_policy
},
3237 [IPCTNL_MSG_CT_GET_STATS_CPU
] = { .call
= ctnetlink_stat_ct_cpu
},
3238 [IPCTNL_MSG_CT_GET_STATS
] = { .call
= ctnetlink_stat_ct
},
3239 [IPCTNL_MSG_CT_GET_DYING
] = { .call
= ctnetlink_get_ct_dying
},
3240 [IPCTNL_MSG_CT_GET_UNCONFIRMED
] = { .call
= ctnetlink_get_ct_unconfirmed
},
3243 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
3244 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
3245 .attr_count
= CTA_EXPECT_MAX
,
3246 .policy
= exp_nla_policy
},
3247 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
3248 .attr_count
= CTA_EXPECT_MAX
,
3249 .policy
= exp_nla_policy
},
3250 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
3251 .attr_count
= CTA_EXPECT_MAX
,
3252 .policy
= exp_nla_policy
},
3253 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = { .call
= ctnetlink_stat_exp_cpu
},
3256 static const struct nfnetlink_subsystem ctnl_subsys
= {
3257 .name
= "conntrack",
3258 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
3259 .cb_count
= IPCTNL_MSG_MAX
,
3263 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
3264 .name
= "conntrack_expect",
3265 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
3266 .cb_count
= IPCTNL_MSG_EXP_MAX
,
3270 MODULE_ALIAS("ip_conntrack_netlink");
3271 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
3272 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
3274 static int __net_init
ctnetlink_net_init(struct net
*net
)
3276 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3279 ret
= nf_conntrack_register_notifier(net
, &ctnl_notifier
);
3281 pr_err("ctnetlink_init: cannot register notifier.\n");
3285 ret
= nf_ct_expect_register_notifier(net
, &ctnl_notifier_exp
);
3287 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3288 goto err_unreg_notifier
;
3293 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3295 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3301 static void ctnetlink_net_exit(struct net
*net
)
3303 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3304 nf_ct_expect_unregister_notifier(net
, &ctnl_notifier_exp
);
3305 nf_conntrack_unregister_notifier(net
, &ctnl_notifier
);
3309 static void __net_exit
ctnetlink_net_exit_batch(struct list_head
*net_exit_list
)
3313 list_for_each_entry(net
, net_exit_list
, exit_list
)
3314 ctnetlink_net_exit(net
);
3317 static struct pernet_operations ctnetlink_net_ops
= {
3318 .init
= ctnetlink_net_init
,
3319 .exit_batch
= ctnetlink_net_exit_batch
,
3322 static int __init
ctnetlink_init(void)
3326 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version
);
3327 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
3329 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3333 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
3335 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3336 goto err_unreg_subsys
;
3339 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
3341 pr_err("ctnetlink_init: cannot register pernet operations\n");
3342 goto err_unreg_exp_subsys
;
3344 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3345 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3346 RCU_INIT_POINTER(nfq_ct_hook
, &ctnetlink_nfqueue_hook
);
3350 err_unreg_exp_subsys
:
3351 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3353 nfnetlink_subsys_unregister(&ctnl_subsys
);
3358 static void __exit
ctnetlink_exit(void)
3360 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3362 unregister_pernet_subsys(&ctnetlink_net_ops
);
3363 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3364 nfnetlink_subsys_unregister(&ctnl_subsys
);
3365 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3366 RCU_INIT_POINTER(nfq_ct_hook
, NULL
);
3370 module_init(ctnetlink_init
);
3371 module_exit(ctnetlink_exit
);