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-2007 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/types.h>
22 #include <linux/timer.h>
23 #include <linux/skbuff.h>
24 #include <linux/errno.h>
25 #include <linux/netlink.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/notifier.h>
30 #include <linux/netfilter.h>
31 #include <net/netlink.h>
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_expect.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_l3proto.h>
37 #include <net/netfilter/nf_conntrack_l4proto.h>
38 #include <net/netfilter/nf_conntrack_tuple.h>
39 #ifdef CONFIG_NF_NAT_NEEDED
40 #include <net/netfilter/nf_nat_core.h>
41 #include <net/netfilter/nf_nat_protocol.h>
44 #include <linux/netfilter/nfnetlink.h>
45 #include <linux/netfilter/nfnetlink_conntrack.h>
47 MODULE_LICENSE("GPL");
49 static char __initdata version
[] = "0.93";
52 ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
53 const struct nf_conntrack_tuple
*tuple
,
54 struct nf_conntrack_l4proto
*l4proto
)
57 struct nlattr
*nest_parms
;
59 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
| NLA_F_NESTED
);
62 NLA_PUT_U8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
);
64 if (likely(l4proto
->tuple_to_nlattr
))
65 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
67 nla_nest_end(skb
, nest_parms
);
76 ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
77 const struct nf_conntrack_tuple
*tuple
,
78 struct nf_conntrack_l3proto
*l3proto
)
81 struct nlattr
*nest_parms
;
83 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
| NLA_F_NESTED
);
87 if (likely(l3proto
->tuple_to_nlattr
))
88 ret
= l3proto
->tuple_to_nlattr(skb
, tuple
);
90 nla_nest_end(skb
, nest_parms
);
99 ctnetlink_dump_tuples(struct sk_buff
*skb
,
100 const struct nf_conntrack_tuple
*tuple
)
103 struct nf_conntrack_l3proto
*l3proto
;
104 struct nf_conntrack_l4proto
*l4proto
;
106 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
107 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
, l3proto
);
108 nf_ct_l3proto_put(l3proto
);
110 if (unlikely(ret
< 0))
113 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
114 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
115 nf_ct_l4proto_put(l4proto
);
121 ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
123 NLA_PUT_BE32(skb
, CTA_STATUS
, htonl(ct
->status
));
131 ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
)
133 long timeout
= (ct
->timeout
.expires
- jiffies
) / HZ
;
138 NLA_PUT_BE32(skb
, CTA_TIMEOUT
, htonl(timeout
));
146 ctnetlink_dump_protoinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
148 struct nf_conntrack_l4proto
*l4proto
;
149 struct nlattr
*nest_proto
;
152 l4proto
= nf_ct_l4proto_find_get(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
153 if (!l4proto
->to_nlattr
) {
154 nf_ct_l4proto_put(l4proto
);
158 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
| NLA_F_NESTED
);
160 goto nla_put_failure
;
162 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
);
164 nf_ct_l4proto_put(l4proto
);
166 nla_nest_end(skb
, nest_proto
);
171 nf_ct_l4proto_put(l4proto
);
176 ctnetlink_dump_helpinfo(struct sk_buff
*skb
, const struct nf_conn
*ct
)
178 struct nlattr
*nest_helper
;
179 const struct nf_conn_help
*help
= nfct_help(ct
);
180 struct nf_conntrack_helper
*helper
;
186 helper
= rcu_dereference(help
->helper
);
190 nest_helper
= nla_nest_start(skb
, CTA_HELP
| NLA_F_NESTED
);
192 goto nla_put_failure
;
193 NLA_PUT_STRING(skb
, CTA_HELP_NAME
, helper
->name
);
195 if (helper
->to_nlattr
)
196 helper
->to_nlattr(skb
, ct
);
198 nla_nest_end(skb
, nest_helper
);
208 #ifdef CONFIG_NF_CT_ACCT
210 ctnetlink_dump_counters(struct sk_buff
*skb
, const struct nf_conn
*ct
,
211 enum ip_conntrack_dir dir
)
213 enum ctattr_type type
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
214 struct nlattr
*nest_count
;
216 nest_count
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
218 goto nla_put_failure
;
220 NLA_PUT_BE32(skb
, CTA_COUNTERS32_PACKETS
,
221 htonl(ct
->counters
[dir
].packets
));
222 NLA_PUT_BE32(skb
, CTA_COUNTERS32_BYTES
,
223 htonl(ct
->counters
[dir
].bytes
));
225 nla_nest_end(skb
, nest_count
);
233 #define ctnetlink_dump_counters(a, b, c) (0)
236 #ifdef CONFIG_NF_CONNTRACK_MARK
238 ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
240 NLA_PUT_BE32(skb
, CTA_MARK
, htonl(ct
->mark
));
247 #define ctnetlink_dump_mark(a, b) (0)
250 #ifdef CONFIG_NF_CONNTRACK_SECMARK
252 ctnetlink_dump_secmark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
254 NLA_PUT_BE32(skb
, CTA_SECMARK
, htonl(ct
->secmark
));
261 #define ctnetlink_dump_secmark(a, b) (0)
264 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
267 ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
269 struct nlattr
*nest_parms
;
271 if (!(ct
->status
& IPS_EXPECTED
))
274 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
| NLA_F_NESTED
);
276 goto nla_put_failure
;
277 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
278 goto nla_put_failure
;
279 nla_nest_end(skb
, nest_parms
);
287 #ifdef CONFIG_NF_NAT_NEEDED
289 dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_nat_seq
*natseq
, int type
)
291 struct nlattr
*nest_parms
;
293 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
295 goto nla_put_failure
;
297 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_CORRECTION_POS
,
298 htonl(natseq
->correction_pos
));
299 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_BEFORE
,
300 htonl(natseq
->offset_before
));
301 NLA_PUT_BE32(skb
, CTA_NAT_SEQ_OFFSET_AFTER
,
302 htonl(natseq
->offset_after
));
304 nla_nest_end(skb
, nest_parms
);
313 ctnetlink_dump_nat_seq_adj(struct sk_buff
*skb
, const struct nf_conn
*ct
)
315 struct nf_nat_seq
*natseq
;
316 struct nf_conn_nat
*nat
= nfct_nat(ct
);
318 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !nat
)
321 natseq
= &nat
->seq
[IP_CT_DIR_ORIGINAL
];
322 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_ORIG
) == -1)
325 natseq
= &nat
->seq
[IP_CT_DIR_REPLY
];
326 if (dump_nat_seq_adj(skb
, natseq
, CTA_NAT_SEQ_ADJ_REPLY
) == -1)
332 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
336 ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
338 NLA_PUT_BE32(skb
, CTA_ID
, htonl((unsigned long)ct
));
346 ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
348 NLA_PUT_BE32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
)));
355 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
358 ctnetlink_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
359 int event
, int nowait
,
360 const struct nf_conn
*ct
)
362 struct nlmsghdr
*nlh
;
363 struct nfgenmsg
*nfmsg
;
364 struct nlattr
*nest_parms
;
365 unsigned char *b
= skb_tail_pointer(skb
);
367 event
|= NFNL_SUBSYS_CTNETLINK
<< 8;
368 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
369 nfmsg
= NLMSG_DATA(nlh
);
371 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
372 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
373 nfmsg
->version
= NFNETLINK_V0
;
376 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
378 goto nla_put_failure
;
379 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
380 goto nla_put_failure
;
381 nla_nest_end(skb
, nest_parms
);
383 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
385 goto nla_put_failure
;
386 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
387 goto nla_put_failure
;
388 nla_nest_end(skb
, nest_parms
);
390 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
391 ctnetlink_dump_timeout(skb
, ct
) < 0 ||
392 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
393 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0 ||
394 ctnetlink_dump_protoinfo(skb
, ct
) < 0 ||
395 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
396 ctnetlink_dump_mark(skb
, ct
) < 0 ||
397 ctnetlink_dump_secmark(skb
, ct
) < 0 ||
398 ctnetlink_dump_id(skb
, ct
) < 0 ||
399 ctnetlink_dump_use(skb
, ct
) < 0 ||
400 ctnetlink_dump_master(skb
, ct
) < 0 ||
401 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
402 goto nla_put_failure
;
404 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
413 #ifdef CONFIG_NF_CONNTRACK_EVENTS
414 static int ctnetlink_conntrack_event(struct notifier_block
*this,
415 unsigned long events
, void *ptr
)
417 struct nlmsghdr
*nlh
;
418 struct nfgenmsg
*nfmsg
;
419 struct nlattr
*nest_parms
;
420 struct nf_conn
*ct
= (struct nf_conn
*)ptr
;
424 unsigned int flags
= 0, group
;
426 /* ignore our fake conntrack entry */
427 if (ct
== &nf_conntrack_untracked
)
430 if (events
& IPCT_DESTROY
) {
431 type
= IPCTNL_MSG_CT_DELETE
;
432 group
= NFNLGRP_CONNTRACK_DESTROY
;
433 } else if (events
& (IPCT_NEW
| IPCT_RELATED
)) {
434 type
= IPCTNL_MSG_CT_NEW
;
435 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
436 group
= NFNLGRP_CONNTRACK_NEW
;
437 } else if (events
& (IPCT_STATUS
| IPCT_PROTOINFO
)) {
438 type
= IPCTNL_MSG_CT_NEW
;
439 group
= NFNLGRP_CONNTRACK_UPDATE
;
443 if (!nfnetlink_has_listeners(group
))
446 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
452 type
|= NFNL_SUBSYS_CTNETLINK
<< 8;
453 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
454 nfmsg
= NLMSG_DATA(nlh
);
456 nlh
->nlmsg_flags
= flags
;
457 nfmsg
->nfgen_family
= nf_ct_l3num(ct
);
458 nfmsg
->version
= NFNETLINK_V0
;
461 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
| NLA_F_NESTED
);
463 goto nla_put_failure
;
464 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
465 goto nla_put_failure
;
466 nla_nest_end(skb
, nest_parms
);
468 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
| NLA_F_NESTED
);
470 goto nla_put_failure
;
471 if (ctnetlink_dump_tuples(skb
, tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
472 goto nla_put_failure
;
473 nla_nest_end(skb
, nest_parms
);
475 if (ctnetlink_dump_id(skb
, ct
) < 0)
476 goto nla_put_failure
;
478 if (events
& IPCT_DESTROY
) {
479 if (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
480 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0)
481 goto nla_put_failure
;
483 if (ctnetlink_dump_status(skb
, ct
) < 0)
484 goto nla_put_failure
;
486 if (ctnetlink_dump_timeout(skb
, ct
) < 0)
487 goto nla_put_failure
;
489 if (events
& IPCT_PROTOINFO
490 && ctnetlink_dump_protoinfo(skb
, ct
) < 0)
491 goto nla_put_failure
;
493 if ((events
& IPCT_HELPER
|| nfct_help(ct
))
494 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
495 goto nla_put_failure
;
497 #ifdef CONFIG_NF_CONNTRACK_SECMARK
498 if ((events
& IPCT_SECMARK
|| ct
->secmark
)
499 && ctnetlink_dump_secmark(skb
, ct
) < 0)
500 goto nla_put_failure
;
503 if (events
& IPCT_COUNTER_FILLING
&&
504 (ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_ORIGINAL
) < 0 ||
505 ctnetlink_dump_counters(skb
, ct
, IP_CT_DIR_REPLY
) < 0))
506 goto nla_put_failure
;
508 if (events
& IPCT_RELATED
&&
509 ctnetlink_dump_master(skb
, ct
) < 0)
510 goto nla_put_failure
;
512 if (events
& IPCT_NATSEQADJ
&&
513 ctnetlink_dump_nat_seq_adj(skb
, ct
) < 0)
514 goto nla_put_failure
;
517 #ifdef CONFIG_NF_CONNTRACK_MARK
518 if ((events
& IPCT_MARK
|| ct
->mark
)
519 && ctnetlink_dump_mark(skb
, ct
) < 0)
520 goto nla_put_failure
;
523 nlh
->nlmsg_len
= skb
->tail
- b
;
524 nfnetlink_send(skb
, 0, group
, 0);
532 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
534 static int ctnetlink_done(struct netlink_callback
*cb
)
537 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
542 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
544 struct nf_conn
*ct
, *last
;
545 struct nf_conntrack_tuple_hash
*h
;
546 struct hlist_node
*n
;
547 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
548 u_int8_t l3proto
= nfmsg
->nfgen_family
;
551 last
= (struct nf_conn
*)cb
->args
[1];
552 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
554 hlist_for_each_entry_rcu(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
556 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
558 ct
= nf_ct_tuplehash_to_ctrack(h
);
559 /* Dump entries of a given L3 protocol number.
560 * If it is not specified, ie. l3proto == 0,
561 * then dump everything. */
562 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
569 if (ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
573 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
575 cb
->args
[1] = (unsigned long)ct
;
578 #ifdef CONFIG_NF_CT_ACCT
579 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) ==
580 IPCTNL_MSG_CT_GET_CTRZERO
)
581 memset(&ct
->counters
, 0, sizeof(ct
->counters
));
598 ctnetlink_parse_tuple_ip(struct nlattr
*attr
, struct nf_conntrack_tuple
*tuple
)
600 struct nlattr
*tb
[CTA_IP_MAX
+1];
601 struct nf_conntrack_l3proto
*l3proto
;
604 nla_parse_nested(tb
, CTA_IP_MAX
, attr
, NULL
);
606 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
608 if (likely(l3proto
->nlattr_to_tuple
)) {
609 ret
= nla_validate_nested(attr
, CTA_IP_MAX
,
610 l3proto
->nla_policy
);
612 ret
= l3proto
->nlattr_to_tuple(tb
, tuple
);
615 nf_ct_l3proto_put(l3proto
);
620 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
621 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
625 ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
626 struct nf_conntrack_tuple
*tuple
)
628 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
629 struct nf_conntrack_l4proto
*l4proto
;
632 ret
= nla_parse_nested(tb
, CTA_PROTO_MAX
, attr
, proto_nla_policy
);
636 if (!tb
[CTA_PROTO_NUM
])
638 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
640 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
642 if (likely(l4proto
->nlattr_to_tuple
)) {
643 ret
= nla_validate_nested(attr
, CTA_PROTO_MAX
,
644 l4proto
->nla_policy
);
646 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
);
649 nf_ct_l4proto_put(l4proto
);
655 ctnetlink_parse_tuple(struct nlattr
*cda
[], struct nf_conntrack_tuple
*tuple
,
656 enum ctattr_tuple type
, u_int8_t l3num
)
658 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
661 memset(tuple
, 0, sizeof(*tuple
));
663 nla_parse_nested(tb
, CTA_TUPLE_MAX
, cda
[type
], NULL
);
665 if (!tb
[CTA_TUPLE_IP
])
668 tuple
->src
.l3num
= l3num
;
670 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
);
674 if (!tb
[CTA_TUPLE_PROTO
])
677 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
);
681 /* orig and expect tuples get DIR_ORIGINAL */
682 if (type
== CTA_TUPLE_REPLY
)
683 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
685 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
690 #ifdef CONFIG_NF_NAT_NEEDED
691 static const struct nla_policy protonat_nla_policy
[CTA_PROTONAT_MAX
+1] = {
692 [CTA_PROTONAT_PORT_MIN
] = { .type
= NLA_U16
},
693 [CTA_PROTONAT_PORT_MAX
] = { .type
= NLA_U16
},
696 static int nfnetlink_parse_nat_proto(struct nlattr
*attr
,
697 const struct nf_conn
*ct
,
698 struct nf_nat_range
*range
)
700 struct nlattr
*tb
[CTA_PROTONAT_MAX
+1];
701 const struct nf_nat_protocol
*npt
;
704 err
= nla_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
, protonat_nla_policy
);
708 npt
= nf_nat_proto_find_get(nf_ct_protonum(ct
));
709 if (npt
->nlattr_to_range
)
710 err
= npt
->nlattr_to_range(tb
, range
);
711 nf_nat_proto_put(npt
);
715 static const struct nla_policy nat_nla_policy
[CTA_NAT_MAX
+1] = {
716 [CTA_NAT_MINIP
] = { .type
= NLA_U32
},
717 [CTA_NAT_MAXIP
] = { .type
= NLA_U32
},
721 nfnetlink_parse_nat(struct nlattr
*nat
,
722 const struct nf_conn
*ct
, struct nf_nat_range
*range
)
724 struct nlattr
*tb
[CTA_NAT_MAX
+1];
727 memset(range
, 0, sizeof(*range
));
729 err
= nla_parse_nested(tb
, CTA_NAT_MAX
, nat
, nat_nla_policy
);
733 if (tb
[CTA_NAT_MINIP
])
734 range
->min_ip
= nla_get_be32(tb
[CTA_NAT_MINIP
]);
736 if (!tb
[CTA_NAT_MAXIP
])
737 range
->max_ip
= range
->min_ip
;
739 range
->max_ip
= nla_get_be32(tb
[CTA_NAT_MAXIP
]);
742 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
744 if (!tb
[CTA_NAT_PROTO
])
747 err
= nfnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
], ct
, range
);
756 ctnetlink_parse_help(struct nlattr
*attr
, char **helper_name
)
758 struct nlattr
*tb
[CTA_HELP_MAX
+1];
760 nla_parse_nested(tb
, CTA_HELP_MAX
, attr
, NULL
);
762 if (!tb
[CTA_HELP_NAME
])
765 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
770 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
771 [CTA_STATUS
] = { .type
= NLA_U32
},
772 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
773 [CTA_MARK
] = { .type
= NLA_U32
},
774 [CTA_USE
] = { .type
= NLA_U32
},
775 [CTA_ID
] = { .type
= NLA_U32
},
779 ctnetlink_del_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
780 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
782 struct nf_conntrack_tuple_hash
*h
;
783 struct nf_conntrack_tuple tuple
;
785 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
786 u_int8_t u3
= nfmsg
->nfgen_family
;
789 if (cda
[CTA_TUPLE_ORIG
])
790 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
791 else if (cda
[CTA_TUPLE_REPLY
])
792 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
794 /* Flush the whole table */
795 nf_conntrack_flush();
802 h
= nf_conntrack_find_get(&tuple
);
806 ct
= nf_ct_tuplehash_to_ctrack(h
);
809 u_int32_t id
= ntohl(nla_get_be32(cda
[CTA_ID
]));
810 if (id
!= (u32
)(unsigned long)ct
) {
815 if (del_timer(&ct
->timeout
))
816 ct
->timeout
.function((unsigned long)ct
);
824 ctnetlink_get_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
825 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
827 struct nf_conntrack_tuple_hash
*h
;
828 struct nf_conntrack_tuple tuple
;
830 struct sk_buff
*skb2
= NULL
;
831 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
832 u_int8_t u3
= nfmsg
->nfgen_family
;
835 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
836 #ifndef CONFIG_NF_CT_ACCT
837 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == IPCTNL_MSG_CT_GET_CTRZERO
)
840 return netlink_dump_start(ctnl
, skb
, nlh
, ctnetlink_dump_table
,
844 if (cda
[CTA_TUPLE_ORIG
])
845 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
, u3
);
846 else if (cda
[CTA_TUPLE_REPLY
])
847 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
, u3
);
854 h
= nf_conntrack_find_get(&tuple
);
858 ct
= nf_ct_tuplehash_to_ctrack(h
);
861 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
867 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
,
868 IPCTNL_MSG_CT_NEW
, 1, ct
);
873 err
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
886 ctnetlink_change_status(struct nf_conn
*ct
, struct nlattr
*cda
[])
889 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
890 d
= ct
->status
^ status
;
892 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
896 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
897 /* SEEN_REPLY bit can only be set */
901 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
902 /* ASSURED bit can only be set */
905 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
906 #ifndef CONFIG_NF_NAT_NEEDED
909 struct nf_nat_range range
;
911 if (cda
[CTA_NAT_DST
]) {
912 if (nfnetlink_parse_nat(cda
[CTA_NAT_DST
], ct
,
915 if (nf_nat_initialized(ct
, IP_NAT_MANIP_DST
))
917 nf_nat_setup_info(ct
, &range
, IP_NAT_MANIP_DST
);
919 if (cda
[CTA_NAT_SRC
]) {
920 if (nfnetlink_parse_nat(cda
[CTA_NAT_SRC
], ct
,
923 if (nf_nat_initialized(ct
, IP_NAT_MANIP_SRC
))
925 nf_nat_setup_info(ct
, &range
, IP_NAT_MANIP_SRC
);
930 /* Be careful here, modifying NAT bits can screw up things,
931 * so don't let users modify them directly if they don't pass
933 ct
->status
|= status
& ~(IPS_NAT_DONE_MASK
| IPS_NAT_MASK
);
939 ctnetlink_change_helper(struct nf_conn
*ct
, struct nlattr
*cda
[])
941 struct nf_conntrack_helper
*helper
;
942 struct nf_conn_help
*help
= nfct_help(ct
);
946 /* don't change helper of sibling connections */
950 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
);
954 if (!strcmp(helpname
, "")) {
955 if (help
&& help
->helper
) {
956 /* we had a helper before ... */
957 nf_ct_remove_expectations(ct
);
958 rcu_assign_pointer(help
->helper
, NULL
);
964 helper
= __nf_conntrack_helper_find_byname(helpname
);
969 if (help
->helper
== helper
)
973 /* need to zero data of old helper */
974 memset(&help
->help
, 0, sizeof(help
->help
));
976 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
981 rcu_assign_pointer(help
->helper
, helper
);
987 ctnetlink_change_timeout(struct nf_conn
*ct
, struct nlattr
*cda
[])
989 u_int32_t timeout
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
991 if (!del_timer(&ct
->timeout
))
994 ct
->timeout
.expires
= jiffies
+ timeout
* HZ
;
995 add_timer(&ct
->timeout
);
1001 ctnetlink_change_protoinfo(struct nf_conn
*ct
, struct nlattr
*cda
[])
1003 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1], *attr
= cda
[CTA_PROTOINFO
];
1004 struct nf_conntrack_l4proto
*l4proto
;
1007 nla_parse_nested(tb
, CTA_PROTOINFO_MAX
, attr
, NULL
);
1009 l4proto
= nf_ct_l4proto_find_get(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
1010 if (l4proto
->from_nlattr
)
1011 err
= l4proto
->from_nlattr(tb
, ct
);
1012 nf_ct_l4proto_put(l4proto
);
1017 #ifdef CONFIG_NF_NAT_NEEDED
1019 change_nat_seq_adj(struct nf_nat_seq
*natseq
, struct nlattr
*attr
)
1021 struct nlattr
*cda
[CTA_NAT_SEQ_MAX
+1];
1023 nla_parse_nested(cda
, CTA_NAT_SEQ_MAX
, attr
, NULL
);
1025 if (!cda
[CTA_NAT_SEQ_CORRECTION_POS
])
1028 natseq
->correction_pos
=
1029 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_CORRECTION_POS
]));
1031 if (!cda
[CTA_NAT_SEQ_OFFSET_BEFORE
])
1034 natseq
->offset_before
=
1035 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_BEFORE
]));
1037 if (!cda
[CTA_NAT_SEQ_OFFSET_AFTER
])
1040 natseq
->offset_after
=
1041 ntohl(nla_get_be32(cda
[CTA_NAT_SEQ_OFFSET_AFTER
]));
1047 ctnetlink_change_nat_seq_adj(struct nf_conn
*ct
, struct nlattr
*cda
[])
1050 struct nf_conn_nat
*nat
= nfct_nat(ct
);
1055 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
]) {
1056 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_ORIGINAL
],
1057 cda
[CTA_NAT_SEQ_ADJ_ORIG
]);
1061 ct
->status
|= IPS_SEQ_ADJUST
;
1064 if (cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1065 ret
= change_nat_seq_adj(&nat
->seq
[IP_CT_DIR_REPLY
],
1066 cda
[CTA_NAT_SEQ_ADJ_REPLY
]);
1070 ct
->status
|= IPS_SEQ_ADJUST
;
1078 ctnetlink_change_conntrack(struct nf_conn
*ct
, struct nlattr
*cda
[])
1082 if (cda
[CTA_HELP
]) {
1083 err
= ctnetlink_change_helper(ct
, cda
);
1088 if (cda
[CTA_TIMEOUT
]) {
1089 err
= ctnetlink_change_timeout(ct
, cda
);
1094 if (cda
[CTA_STATUS
]) {
1095 err
= ctnetlink_change_status(ct
, cda
);
1100 if (cda
[CTA_PROTOINFO
]) {
1101 err
= ctnetlink_change_protoinfo(ct
, cda
);
1106 #if defined(CONFIG_NF_CONNTRACK_MARK)
1108 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1111 #ifdef CONFIG_NF_NAT_NEEDED
1112 if (cda
[CTA_NAT_SEQ_ADJ_ORIG
] || cda
[CTA_NAT_SEQ_ADJ_REPLY
]) {
1113 err
= ctnetlink_change_nat_seq_adj(ct
, cda
);
1123 ctnetlink_create_conntrack(struct nlattr
*cda
[],
1124 struct nf_conntrack_tuple
*otuple
,
1125 struct nf_conntrack_tuple
*rtuple
,
1126 struct nf_conn
*master_ct
)
1130 struct nf_conn_help
*help
;
1131 struct nf_conntrack_helper
*helper
;
1133 ct
= nf_conntrack_alloc(otuple
, rtuple
);
1134 if (ct
== NULL
|| IS_ERR(ct
))
1137 if (!cda
[CTA_TIMEOUT
])
1139 ct
->timeout
.expires
= ntohl(nla_get_be32(cda
[CTA_TIMEOUT
]));
1141 ct
->timeout
.expires
= jiffies
+ ct
->timeout
.expires
* HZ
;
1142 ct
->status
|= IPS_CONFIRMED
;
1144 if (cda
[CTA_STATUS
]) {
1145 err
= ctnetlink_change_status(ct
, cda
);
1150 if (cda
[CTA_PROTOINFO
]) {
1151 err
= ctnetlink_change_protoinfo(ct
, cda
);
1156 #if defined(CONFIG_NF_CONNTRACK_MARK)
1158 ct
->mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
1162 helper
= __nf_ct_helper_find(rtuple
);
1164 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
1170 /* not in hash table yet so not strictly necessary */
1171 rcu_assign_pointer(help
->helper
, helper
);
1174 /* setup master conntrack: this is a confirmed expectation */
1176 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
1177 ct
->master
= master_ct
;
1180 add_timer(&ct
->timeout
);
1181 nf_conntrack_hash_insert(ct
);
1187 nf_conntrack_free(ct
);
1192 ctnetlink_new_conntrack(struct sock
*ctnl
, struct sk_buff
*skb
,
1193 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1195 struct nf_conntrack_tuple otuple
, rtuple
;
1196 struct nf_conntrack_tuple_hash
*h
= NULL
;
1197 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1198 u_int8_t u3
= nfmsg
->nfgen_family
;
1201 if (cda
[CTA_TUPLE_ORIG
]) {
1202 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
, u3
);
1207 if (cda
[CTA_TUPLE_REPLY
]) {
1208 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
, u3
);
1213 spin_lock_bh(&nf_conntrack_lock
);
1214 if (cda
[CTA_TUPLE_ORIG
])
1215 h
= __nf_conntrack_find(&otuple
);
1216 else if (cda
[CTA_TUPLE_REPLY
])
1217 h
= __nf_conntrack_find(&rtuple
);
1220 struct nf_conntrack_tuple master
;
1221 struct nf_conntrack_tuple_hash
*master_h
= NULL
;
1222 struct nf_conn
*master_ct
= NULL
;
1224 if (cda
[CTA_TUPLE_MASTER
]) {
1225 err
= ctnetlink_parse_tuple(cda
,
1232 master_h
= __nf_conntrack_find(&master
);
1233 if (master_h
== NULL
) {
1237 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
1238 atomic_inc(&master_ct
->ct_general
.use
);
1241 spin_unlock_bh(&nf_conntrack_lock
);
1243 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1244 err
= ctnetlink_create_conntrack(cda
,
1248 if (err
< 0 && master_ct
)
1249 nf_ct_put(master_ct
);
1253 /* implicit 'else' */
1255 /* We manipulate the conntrack inside the global conntrack table lock,
1256 * so there's no need to increase the refcount */
1258 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
1259 /* we only allow nat config for new conntracks */
1260 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
]) {
1264 /* can't link an existing conntrack to a master */
1265 if (cda
[CTA_TUPLE_MASTER
]) {
1269 err
= ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h
),
1274 spin_unlock_bh(&nf_conntrack_lock
);
1278 /***********************************************************************
1280 ***********************************************************************/
1283 ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
1284 const struct nf_conntrack_tuple
*tuple
,
1285 enum ctattr_expect type
)
1287 struct nlattr
*nest_parms
;
1289 nest_parms
= nla_nest_start(skb
, type
| NLA_F_NESTED
);
1291 goto nla_put_failure
;
1292 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
1293 goto nla_put_failure
;
1294 nla_nest_end(skb
, nest_parms
);
1303 ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
1304 const struct nf_conntrack_tuple
*tuple
,
1305 const struct nf_conntrack_tuple_mask
*mask
)
1308 struct nf_conntrack_l3proto
*l3proto
;
1309 struct nf_conntrack_l4proto
*l4proto
;
1310 struct nf_conntrack_tuple m
;
1311 struct nlattr
*nest_parms
;
1313 memset(&m
, 0xFF, sizeof(m
));
1314 m
.src
.u
.all
= mask
->src
.u
.all
;
1315 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
1317 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
| NLA_F_NESTED
);
1319 goto nla_put_failure
;
1321 l3proto
= nf_ct_l3proto_find_get(tuple
->src
.l3num
);
1322 ret
= ctnetlink_dump_tuples_ip(skb
, &m
, l3proto
);
1323 nf_ct_l3proto_put(l3proto
);
1325 if (unlikely(ret
< 0))
1326 goto nla_put_failure
;
1328 l4proto
= nf_ct_l4proto_find_get(tuple
->src
.l3num
, tuple
->dst
.protonum
);
1329 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
1330 nf_ct_l4proto_put(l4proto
);
1331 if (unlikely(ret
< 0))
1332 goto nla_put_failure
;
1334 nla_nest_end(skb
, nest_parms
);
1343 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
1344 const struct nf_conntrack_expect
*exp
)
1346 struct nf_conn
*master
= exp
->master
;
1347 long timeout
= (exp
->timeout
.expires
- jiffies
) / HZ
;
1352 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
1353 goto nla_put_failure
;
1354 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
1355 goto nla_put_failure
;
1356 if (ctnetlink_exp_dump_tuple(skb
,
1357 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
1358 CTA_EXPECT_MASTER
) < 0)
1359 goto nla_put_failure
;
1361 NLA_PUT_BE32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
));
1362 NLA_PUT_BE32(skb
, CTA_EXPECT_ID
, htonl((unsigned long)exp
));
1371 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1374 const struct nf_conntrack_expect
*exp
)
1376 struct nlmsghdr
*nlh
;
1377 struct nfgenmsg
*nfmsg
;
1378 unsigned char *b
= skb_tail_pointer(skb
);
1380 event
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1381 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(struct nfgenmsg
));
1382 nfmsg
= NLMSG_DATA(nlh
);
1384 nlh
->nlmsg_flags
= (nowait
&& pid
) ? NLM_F_MULTI
: 0;
1385 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1386 nfmsg
->version
= NFNETLINK_V0
;
1389 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1390 goto nla_put_failure
;
1392 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1401 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1402 static int ctnetlink_expect_event(struct notifier_block
*this,
1403 unsigned long events
, void *ptr
)
1405 struct nlmsghdr
*nlh
;
1406 struct nfgenmsg
*nfmsg
;
1407 struct nf_conntrack_expect
*exp
= (struct nf_conntrack_expect
*)ptr
;
1408 struct sk_buff
*skb
;
1413 if (events
& IPEXP_NEW
) {
1414 type
= IPCTNL_MSG_EXP_NEW
;
1415 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
1419 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW
))
1422 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1428 type
|= NFNL_SUBSYS_CTNETLINK_EXP
<< 8;
1429 nlh
= NLMSG_PUT(skb
, 0, 0, type
, sizeof(struct nfgenmsg
));
1430 nfmsg
= NLMSG_DATA(nlh
);
1432 nlh
->nlmsg_flags
= flags
;
1433 nfmsg
->nfgen_family
= exp
->tuple
.src
.l3num
;
1434 nfmsg
->version
= NFNETLINK_V0
;
1437 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
1438 goto nla_put_failure
;
1440 nlh
->nlmsg_len
= skb
->tail
- b
;
1441 nfnetlink_send(skb
, 0, NFNLGRP_CONNTRACK_EXP_NEW
, 0);
1450 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
1453 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
1458 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1460 struct nf_conntrack_expect
*exp
, *last
;
1461 struct nfgenmsg
*nfmsg
= NLMSG_DATA(cb
->nlh
);
1462 struct hlist_node
*n
;
1463 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1466 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
1467 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
1469 hlist_for_each_entry(exp
, n
, &nf_ct_expect_hash
[cb
->args
[0]],
1471 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
1478 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1482 if (!atomic_inc_not_zero(&exp
->use
))
1484 cb
->args
[1] = (unsigned long)exp
;
1496 nf_ct_expect_put(last
);
1501 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
1502 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1503 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
1507 ctnetlink_get_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1508 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1510 struct nf_conntrack_tuple tuple
;
1511 struct nf_conntrack_expect
*exp
;
1512 struct sk_buff
*skb2
;
1513 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1514 u_int8_t u3
= nfmsg
->nfgen_family
;
1517 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1518 return netlink_dump_start(ctnl
, skb
, nlh
,
1519 ctnetlink_exp_dump_table
,
1520 ctnetlink_exp_done
);
1523 if (cda
[CTA_EXPECT_MASTER
])
1524 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
, u3
);
1531 exp
= nf_ct_expect_find_get(&tuple
);
1535 if (cda
[CTA_EXPECT_ID
]) {
1536 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1537 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1538 nf_ct_expect_put(exp
);
1544 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1548 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).pid
,
1549 nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
1554 nf_ct_expect_put(exp
);
1556 return netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1561 nf_ct_expect_put(exp
);
1566 ctnetlink_del_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1567 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1569 struct nf_conntrack_expect
*exp
;
1570 struct nf_conntrack_tuple tuple
;
1571 struct nf_conntrack_helper
*h
;
1572 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1573 struct hlist_node
*n
, *next
;
1574 u_int8_t u3
= nfmsg
->nfgen_family
;
1578 if (cda
[CTA_EXPECT_TUPLE
]) {
1579 /* delete a single expect by tuple */
1580 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1584 /* bump usage count to 2 */
1585 exp
= nf_ct_expect_find_get(&tuple
);
1589 if (cda
[CTA_EXPECT_ID
]) {
1590 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
1591 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
1592 nf_ct_expect_put(exp
);
1597 /* after list removal, usage count == 1 */
1598 nf_ct_unexpect_related(exp
);
1599 /* have to put what we 'get' above.
1600 * after this line usage count == 0 */
1601 nf_ct_expect_put(exp
);
1602 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
1603 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
1604 struct nf_conn_help
*m_help
;
1606 /* delete all expectations for this helper */
1607 spin_lock_bh(&nf_conntrack_lock
);
1608 h
= __nf_conntrack_helper_find_byname(name
);
1610 spin_unlock_bh(&nf_conntrack_lock
);
1613 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1614 hlist_for_each_entry_safe(exp
, n
, next
,
1615 &nf_ct_expect_hash
[i
],
1617 m_help
= nfct_help(exp
->master
);
1618 if (m_help
->helper
== h
1619 && del_timer(&exp
->timeout
)) {
1620 nf_ct_unlink_expect(exp
);
1621 nf_ct_expect_put(exp
);
1625 spin_unlock_bh(&nf_conntrack_lock
);
1627 /* This basically means we have to flush everything*/
1628 spin_lock_bh(&nf_conntrack_lock
);
1629 for (i
= 0; i
< nf_ct_expect_hsize
; i
++) {
1630 hlist_for_each_entry_safe(exp
, n
, next
,
1631 &nf_ct_expect_hash
[i
],
1633 if (del_timer(&exp
->timeout
)) {
1634 nf_ct_unlink_expect(exp
);
1635 nf_ct_expect_put(exp
);
1639 spin_unlock_bh(&nf_conntrack_lock
);
1645 ctnetlink_change_expect(struct nf_conntrack_expect
*x
, struct nlattr
*cda
[])
1651 ctnetlink_create_expect(struct nlattr
*cda
[], u_int8_t u3
)
1653 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
1654 struct nf_conntrack_tuple_hash
*h
= NULL
;
1655 struct nf_conntrack_expect
*exp
;
1657 struct nf_conn_help
*help
;
1660 /* caller guarantees that those three CTA_EXPECT_* exist */
1661 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1664 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
, u3
);
1667 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
, u3
);
1671 /* Look for master conntrack of this expectation */
1672 h
= nf_conntrack_find_get(&master_tuple
);
1675 ct
= nf_ct_tuplehash_to_ctrack(h
);
1676 help
= nfct_help(ct
);
1678 if (!help
|| !help
->helper
) {
1679 /* such conntrack hasn't got any helper, abort */
1684 exp
= nf_ct_expect_alloc(ct
);
1690 exp
->expectfn
= NULL
;
1694 memcpy(&exp
->tuple
, &tuple
, sizeof(struct nf_conntrack_tuple
));
1695 memcpy(&exp
->mask
.src
.u3
, &mask
.src
.u3
, sizeof(exp
->mask
.src
.u3
));
1696 exp
->mask
.src
.u
.all
= mask
.src
.u
.all
;
1698 err
= nf_ct_expect_related(exp
);
1699 nf_ct_expect_put(exp
);
1702 nf_ct_put(nf_ct_tuplehash_to_ctrack(h
));
1707 ctnetlink_new_expect(struct sock
*ctnl
, struct sk_buff
*skb
,
1708 struct nlmsghdr
*nlh
, struct nlattr
*cda
[])
1710 struct nf_conntrack_tuple tuple
;
1711 struct nf_conntrack_expect
*exp
;
1712 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
1713 u_int8_t u3
= nfmsg
->nfgen_family
;
1716 if (!cda
[CTA_EXPECT_TUPLE
]
1717 || !cda
[CTA_EXPECT_MASK
]
1718 || !cda
[CTA_EXPECT_MASTER
])
1721 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
, u3
);
1725 spin_lock_bh(&nf_conntrack_lock
);
1726 exp
= __nf_ct_expect_find(&tuple
);
1729 spin_unlock_bh(&nf_conntrack_lock
);
1731 if (nlh
->nlmsg_flags
& NLM_F_CREATE
)
1732 err
= ctnetlink_create_expect(cda
, u3
);
1737 if (!(nlh
->nlmsg_flags
& NLM_F_EXCL
))
1738 err
= ctnetlink_change_expect(exp
, cda
);
1739 spin_unlock_bh(&nf_conntrack_lock
);
1744 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1745 static struct notifier_block ctnl_notifier
= {
1746 .notifier_call
= ctnetlink_conntrack_event
,
1749 static struct notifier_block ctnl_notifier_exp
= {
1750 .notifier_call
= ctnetlink_expect_event
,
1754 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
1755 [IPCTNL_MSG_CT_NEW
] = { .call
= ctnetlink_new_conntrack
,
1756 .attr_count
= CTA_MAX
,
1757 .policy
= ct_nla_policy
},
1758 [IPCTNL_MSG_CT_GET
] = { .call
= ctnetlink_get_conntrack
,
1759 .attr_count
= CTA_MAX
,
1760 .policy
= ct_nla_policy
},
1761 [IPCTNL_MSG_CT_DELETE
] = { .call
= ctnetlink_del_conntrack
,
1762 .attr_count
= CTA_MAX
,
1763 .policy
= ct_nla_policy
},
1764 [IPCTNL_MSG_CT_GET_CTRZERO
] = { .call
= ctnetlink_get_conntrack
,
1765 .attr_count
= CTA_MAX
,
1766 .policy
= ct_nla_policy
},
1769 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
1770 [IPCTNL_MSG_EXP_GET
] = { .call
= ctnetlink_get_expect
,
1771 .attr_count
= CTA_EXPECT_MAX
,
1772 .policy
= exp_nla_policy
},
1773 [IPCTNL_MSG_EXP_NEW
] = { .call
= ctnetlink_new_expect
,
1774 .attr_count
= CTA_EXPECT_MAX
,
1775 .policy
= exp_nla_policy
},
1776 [IPCTNL_MSG_EXP_DELETE
] = { .call
= ctnetlink_del_expect
,
1777 .attr_count
= CTA_EXPECT_MAX
,
1778 .policy
= exp_nla_policy
},
1781 static const struct nfnetlink_subsystem ctnl_subsys
= {
1782 .name
= "conntrack",
1783 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
1784 .cb_count
= IPCTNL_MSG_MAX
,
1788 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
1789 .name
= "conntrack_expect",
1790 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
1791 .cb_count
= IPCTNL_MSG_EXP_MAX
,
1795 MODULE_ALIAS("ip_conntrack_netlink");
1796 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
1797 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
1799 static int __init
ctnetlink_init(void)
1803 printk("ctnetlink v%s: registering with nfnetlink.\n", version
);
1804 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
1806 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1810 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
1812 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1813 goto err_unreg_subsys
;
1816 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1817 ret
= nf_conntrack_register_notifier(&ctnl_notifier
);
1819 printk("ctnetlink_init: cannot register notifier.\n");
1820 goto err_unreg_exp_subsys
;
1823 ret
= nf_ct_expect_register_notifier(&ctnl_notifier_exp
);
1825 printk("ctnetlink_init: cannot expect register notifier.\n");
1826 goto err_unreg_notifier
;
1832 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1834 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1835 err_unreg_exp_subsys
:
1836 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1839 nfnetlink_subsys_unregister(&ctnl_subsys
);
1844 static void __exit
ctnetlink_exit(void)
1846 printk("ctnetlink: unregistering from nfnetlink.\n");
1848 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1849 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp
);
1850 nf_conntrack_unregister_notifier(&ctnl_notifier
);
1853 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
1854 nfnetlink_subsys_unregister(&ctnl_subsys
);
1858 module_init(ctnetlink_init
);
1859 module_exit(ctnetlink_exit
);