1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 * (C) 2012 by Vyatta Inc. <http://www.vyatta.com>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/rculist.h>
10 #include <linux/rculist_nulls.h>
11 #include <linux/types.h>
12 #include <linux/timer.h>
13 #include <linux/security.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <linux/netlink.h>
17 #include <linux/spinlock.h>
18 #include <linux/interrupt.h>
19 #include <linux/slab.h>
21 #include <linux/netfilter.h>
22 #include <net/netlink.h>
23 #include <net/netns/generic.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_tuple.h>
29 #include <net/netfilter/nf_conntrack_timeout.h>
31 #include <linux/netfilter/nfnetlink.h>
32 #include <linux/netfilter/nfnetlink_cttimeout.h>
34 static unsigned int nfct_timeout_id __read_mostly;
36 struct nfct_timeout_pernet {
37 struct list_head nfct_timeout_list;
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
42 MODULE_DESCRIPTION("cttimeout: Extended Netfilter Connection Tracking timeout tuning");
44 static const struct nla_policy cttimeout_nla_policy[CTA_TIMEOUT_MAX+1] = {
45 [CTA_TIMEOUT_NAME] = { .type = NLA_NUL_STRING,
46 .len = CTNL_TIMEOUT_NAME_MAX - 1},
47 [CTA_TIMEOUT_L3PROTO] = { .type = NLA_U16 },
48 [CTA_TIMEOUT_L4PROTO] = { .type = NLA_U8 },
49 [CTA_TIMEOUT_DATA] = { .type = NLA_NESTED },
52 static struct nfct_timeout_pernet *nfct_timeout_pernet(struct net *net)
54 return net_generic(net, nfct_timeout_id);
58 ctnl_timeout_parse_policy(void *timeout,
59 const struct nf_conntrack_l4proto *l4proto,
60 struct net *net, const struct nlattr *attr)
65 tb = kcalloc(l4proto->ctnl_timeout.nlattr_max + 1, sizeof(*tb),
71 ret = nla_parse_nested_deprecated(tb,
72 l4proto->ctnl_timeout.nlattr_max,
74 l4proto->ctnl_timeout.nla_policy,
79 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, timeout);
86 static int cttimeout_new_timeout(struct net *net, struct sock *ctnl,
88 const struct nlmsghdr *nlh,
89 const struct nlattr * const cda[],
90 struct netlink_ext_ack *extack)
92 struct nfct_timeout_pernet *pernet = nfct_timeout_pernet(net);
95 const struct nf_conntrack_l4proto *l4proto;
96 struct ctnl_timeout *timeout, *matching = NULL;
100 if (!cda[CTA_TIMEOUT_NAME] ||
101 !cda[CTA_TIMEOUT_L3PROTO] ||
102 !cda[CTA_TIMEOUT_L4PROTO] ||
103 !cda[CTA_TIMEOUT_DATA])
106 name = nla_data(cda[CTA_TIMEOUT_NAME]);
107 l3num = ntohs(nla_get_be16(cda[CTA_TIMEOUT_L3PROTO]));
108 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]);
110 list_for_each_entry(timeout, &pernet->nfct_timeout_list, head) {
111 if (strncmp(timeout->name, name, CTNL_TIMEOUT_NAME_MAX) != 0)
114 if (nlh->nlmsg_flags & NLM_F_EXCL)
122 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
123 /* You cannot replace one timeout policy by another of
124 * different kind, sorry.
126 if (matching->timeout.l3num != l3num ||
127 matching->timeout.l4proto->l4proto != l4num)
130 return ctnl_timeout_parse_policy(&matching->timeout.data,
131 matching->timeout.l4proto,
132 net, cda[CTA_TIMEOUT_DATA]);
138 l4proto = nf_ct_l4proto_find(l4num);
140 /* This protocol is not supportted, skip. */
141 if (l4proto->l4proto != l4num) {
146 timeout = kzalloc(sizeof(struct ctnl_timeout) +
147 l4proto->ctnl_timeout.obj_size, GFP_KERNEL);
148 if (timeout == NULL) {
153 ret = ctnl_timeout_parse_policy(&timeout->timeout.data, l4proto, net,
154 cda[CTA_TIMEOUT_DATA]);
158 strcpy(timeout->name, nla_data(cda[CTA_TIMEOUT_NAME]));
159 timeout->timeout.l3num = l3num;
160 timeout->timeout.l4proto = l4proto;
161 refcount_set(&timeout->refcnt, 1);
162 list_add_tail_rcu(&timeout->head, &pernet->nfct_timeout_list);
172 ctnl_timeout_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
173 int event, struct ctnl_timeout *timeout)
175 struct nlmsghdr *nlh;
176 unsigned int flags = portid ? NLM_F_MULTI : 0;
177 const struct nf_conntrack_l4proto *l4proto = timeout->timeout.l4proto;
178 struct nlattr *nest_parms;
181 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_TIMEOUT, event);
182 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
187 if (nla_put_string(skb, CTA_TIMEOUT_NAME, timeout->name) ||
188 nla_put_be16(skb, CTA_TIMEOUT_L3PROTO,
189 htons(timeout->timeout.l3num)) ||
190 nla_put_u8(skb, CTA_TIMEOUT_L4PROTO, l4proto->l4proto) ||
191 nla_put_be32(skb, CTA_TIMEOUT_USE,
192 htonl(refcount_read(&timeout->refcnt))))
193 goto nla_put_failure;
195 nest_parms = nla_nest_start(skb, CTA_TIMEOUT_DATA);
197 goto nla_put_failure;
199 ret = l4proto->ctnl_timeout.obj_to_nlattr(skb, &timeout->timeout.data);
201 goto nla_put_failure;
203 nla_nest_end(skb, nest_parms);
210 nlmsg_cancel(skb, nlh);
215 ctnl_timeout_dump(struct sk_buff *skb, struct netlink_callback *cb)
217 struct nfct_timeout_pernet *pernet;
218 struct net *net = sock_net(skb->sk);
219 struct ctnl_timeout *cur, *last;
224 last = (struct ctnl_timeout *)cb->args[1];
229 pernet = nfct_timeout_pernet(net);
230 list_for_each_entry_rcu(cur, &pernet->nfct_timeout_list, head) {
237 if (ctnl_timeout_fill_info(skb, NETLINK_CB(cb->skb).portid,
239 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
240 IPCTNL_MSG_TIMEOUT_NEW, cur) < 0) {
241 cb->args[1] = (unsigned long)cur;
251 static int cttimeout_get_timeout(struct net *net, struct sock *ctnl,
253 const struct nlmsghdr *nlh,
254 const struct nlattr * const cda[],
255 struct netlink_ext_ack *extack)
257 struct nfct_timeout_pernet *pernet = nfct_timeout_pernet(net);
260 struct ctnl_timeout *cur;
262 if (nlh->nlmsg_flags & NLM_F_DUMP) {
263 struct netlink_dump_control c = {
264 .dump = ctnl_timeout_dump,
266 return netlink_dump_start(ctnl, skb, nlh, &c);
269 if (!cda[CTA_TIMEOUT_NAME])
271 name = nla_data(cda[CTA_TIMEOUT_NAME]);
273 list_for_each_entry(cur, &pernet->nfct_timeout_list, head) {
274 struct sk_buff *skb2;
276 if (strncmp(cur->name, name, CTNL_TIMEOUT_NAME_MAX) != 0)
279 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
285 ret = ctnl_timeout_fill_info(skb2, NETLINK_CB(skb).portid,
287 NFNL_MSG_TYPE(nlh->nlmsg_type),
288 IPCTNL_MSG_TIMEOUT_NEW, cur);
293 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid,
298 /* this avoids a loop in nfnetlink. */
299 return ret == -EAGAIN ? -ENOBUFS : ret;
304 /* try to delete object, fail if it is still in use. */
305 static int ctnl_timeout_try_del(struct net *net, struct ctnl_timeout *timeout)
309 /* We want to avoid races with ctnl_timeout_put. So only when the
310 * current refcnt is 1, we decrease it to 0.
312 if (refcount_dec_if_one(&timeout->refcnt)) {
313 /* We are protected by nfnl mutex. */
314 list_del_rcu(&timeout->head);
315 nf_ct_untimeout(net, &timeout->timeout);
316 kfree_rcu(timeout, rcu_head);
323 static int cttimeout_del_timeout(struct net *net, struct sock *ctnl,
325 const struct nlmsghdr *nlh,
326 const struct nlattr * const cda[],
327 struct netlink_ext_ack *extack)
329 struct nfct_timeout_pernet *pernet = nfct_timeout_pernet(net);
330 struct ctnl_timeout *cur, *tmp;
334 if (!cda[CTA_TIMEOUT_NAME]) {
335 list_for_each_entry_safe(cur, tmp, &pernet->nfct_timeout_list,
337 ctnl_timeout_try_del(net, cur);
341 name = nla_data(cda[CTA_TIMEOUT_NAME]);
343 list_for_each_entry(cur, &pernet->nfct_timeout_list, head) {
344 if (strncmp(cur->name, name, CTNL_TIMEOUT_NAME_MAX) != 0)
347 ret = ctnl_timeout_try_del(net, cur);
356 static int cttimeout_default_set(struct net *net, struct sock *ctnl,
358 const struct nlmsghdr *nlh,
359 const struct nlattr * const cda[],
360 struct netlink_ext_ack *extack)
362 const struct nf_conntrack_l4proto *l4proto;
366 if (!cda[CTA_TIMEOUT_L3PROTO] ||
367 !cda[CTA_TIMEOUT_L4PROTO] ||
368 !cda[CTA_TIMEOUT_DATA])
371 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]);
372 l4proto = nf_ct_l4proto_find(l4num);
374 /* This protocol is not supported, skip. */
375 if (l4proto->l4proto != l4num) {
380 ret = ctnl_timeout_parse_policy(NULL, l4proto, net,
381 cda[CTA_TIMEOUT_DATA]);
391 cttimeout_default_fill_info(struct net *net, struct sk_buff *skb, u32 portid,
392 u32 seq, u32 type, int event, u16 l3num,
393 const struct nf_conntrack_l4proto *l4proto,
394 const unsigned int *timeouts)
396 struct nlmsghdr *nlh;
397 unsigned int flags = portid ? NLM_F_MULTI : 0;
398 struct nlattr *nest_parms;
401 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_TIMEOUT, event);
402 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
407 if (nla_put_be16(skb, CTA_TIMEOUT_L3PROTO, htons(l3num)) ||
408 nla_put_u8(skb, CTA_TIMEOUT_L4PROTO, l4proto->l4proto))
409 goto nla_put_failure;
411 nest_parms = nla_nest_start(skb, CTA_TIMEOUT_DATA);
413 goto nla_put_failure;
415 ret = l4proto->ctnl_timeout.obj_to_nlattr(skb, timeouts);
417 goto nla_put_failure;
419 nla_nest_end(skb, nest_parms);
426 nlmsg_cancel(skb, nlh);
430 static int cttimeout_default_get(struct net *net, struct sock *ctnl,
432 const struct nlmsghdr *nlh,
433 const struct nlattr * const cda[],
434 struct netlink_ext_ack *extack)
436 const struct nf_conntrack_l4proto *l4proto;
437 unsigned int *timeouts = NULL;
438 struct sk_buff *skb2;
443 if (!cda[CTA_TIMEOUT_L3PROTO] || !cda[CTA_TIMEOUT_L4PROTO])
446 l3num = ntohs(nla_get_be16(cda[CTA_TIMEOUT_L3PROTO]));
447 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]);
448 l4proto = nf_ct_l4proto_find(l4num);
451 if (l4proto->l4proto != l4num)
454 switch (l4proto->l4proto) {
456 timeouts = &nf_icmp_pernet(net)->timeout;
459 timeouts = nf_tcp_pernet(net)->timeouts;
462 case IPPROTO_UDPLITE:
463 timeouts = nf_udp_pernet(net)->timeouts;
466 #ifdef CONFIG_NF_CT_PROTO_DCCP
467 timeouts = nf_dccp_pernet(net)->dccp_timeout;
471 timeouts = &nf_icmpv6_pernet(net)->timeout;
474 #ifdef CONFIG_NF_CT_PROTO_SCTP
475 timeouts = nf_sctp_pernet(net)->timeouts;
479 #ifdef CONFIG_NF_CT_PROTO_GRE
480 timeouts = nf_gre_pernet(net)->timeouts;
484 timeouts = &nf_generic_pernet(net)->timeout;
487 WARN_ONCE(1, "Missing timeouts for proto %d", l4proto->l4proto);
494 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
500 ret = cttimeout_default_fill_info(net, skb2, NETLINK_CB(skb).portid,
502 NFNL_MSG_TYPE(nlh->nlmsg_type),
503 IPCTNL_MSG_TIMEOUT_DEFAULT_SET,
504 l3num, l4proto, timeouts);
510 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
514 /* this avoids a loop in nfnetlink. */
515 return ret == -EAGAIN ? -ENOBUFS : ret;
520 static struct nf_ct_timeout *ctnl_timeout_find_get(struct net *net,
523 struct nfct_timeout_pernet *pernet = nfct_timeout_pernet(net);
524 struct ctnl_timeout *timeout, *matching = NULL;
526 list_for_each_entry_rcu(timeout, &pernet->nfct_timeout_list, head) {
527 if (strncmp(timeout->name, name, CTNL_TIMEOUT_NAME_MAX) != 0)
530 if (!try_module_get(THIS_MODULE))
533 if (!refcount_inc_not_zero(&timeout->refcnt)) {
534 module_put(THIS_MODULE);
541 return matching ? &matching->timeout : NULL;
544 static void ctnl_timeout_put(struct nf_ct_timeout *t)
546 struct ctnl_timeout *timeout =
547 container_of(t, struct ctnl_timeout, timeout);
549 if (refcount_dec_and_test(&timeout->refcnt))
550 kfree_rcu(timeout, rcu_head);
552 module_put(THIS_MODULE);
555 static const struct nfnl_callback cttimeout_cb[IPCTNL_MSG_TIMEOUT_MAX] = {
556 [IPCTNL_MSG_TIMEOUT_NEW] = { .call = cttimeout_new_timeout,
557 .attr_count = CTA_TIMEOUT_MAX,
558 .policy = cttimeout_nla_policy },
559 [IPCTNL_MSG_TIMEOUT_GET] = { .call = cttimeout_get_timeout,
560 .attr_count = CTA_TIMEOUT_MAX,
561 .policy = cttimeout_nla_policy },
562 [IPCTNL_MSG_TIMEOUT_DELETE] = { .call = cttimeout_del_timeout,
563 .attr_count = CTA_TIMEOUT_MAX,
564 .policy = cttimeout_nla_policy },
565 [IPCTNL_MSG_TIMEOUT_DEFAULT_SET]= { .call = cttimeout_default_set,
566 .attr_count = CTA_TIMEOUT_MAX,
567 .policy = cttimeout_nla_policy },
568 [IPCTNL_MSG_TIMEOUT_DEFAULT_GET]= { .call = cttimeout_default_get,
569 .attr_count = CTA_TIMEOUT_MAX,
570 .policy = cttimeout_nla_policy },
573 static const struct nfnetlink_subsystem cttimeout_subsys = {
574 .name = "conntrack_timeout",
575 .subsys_id = NFNL_SUBSYS_CTNETLINK_TIMEOUT,
576 .cb_count = IPCTNL_MSG_TIMEOUT_MAX,
580 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_TIMEOUT);
582 static int __net_init cttimeout_net_init(struct net *net)
584 struct nfct_timeout_pernet *pernet = nfct_timeout_pernet(net);
586 INIT_LIST_HEAD(&pernet->nfct_timeout_list);
591 static void __net_exit cttimeout_net_exit(struct net *net)
593 struct nfct_timeout_pernet *pernet = nfct_timeout_pernet(net);
594 struct ctnl_timeout *cur, *tmp;
596 nf_ct_unconfirmed_destroy(net);
597 nf_ct_untimeout(net, NULL);
599 list_for_each_entry_safe(cur, tmp, &pernet->nfct_timeout_list, head) {
600 list_del_rcu(&cur->head);
602 if (refcount_dec_and_test(&cur->refcnt))
603 kfree_rcu(cur, rcu_head);
607 static struct pernet_operations cttimeout_ops = {
608 .init = cttimeout_net_init,
609 .exit = cttimeout_net_exit,
610 .id = &nfct_timeout_id,
611 .size = sizeof(struct nfct_timeout_pernet),
614 static int __init cttimeout_init(void)
618 ret = register_pernet_subsys(&cttimeout_ops);
622 ret = nfnetlink_subsys_register(&cttimeout_subsys);
624 pr_err("cttimeout_init: cannot register cttimeout with "
628 RCU_INIT_POINTER(nf_ct_timeout_find_get_hook, ctnl_timeout_find_get);
629 RCU_INIT_POINTER(nf_ct_timeout_put_hook, ctnl_timeout_put);
633 unregister_pernet_subsys(&cttimeout_ops);
637 static void __exit cttimeout_exit(void)
639 nfnetlink_subsys_unregister(&cttimeout_subsys);
641 unregister_pernet_subsys(&cttimeout_ops);
642 RCU_INIT_POINTER(nf_ct_timeout_find_get_hook, NULL);
643 RCU_INIT_POINTER(nf_ct_timeout_put_hook, NULL);
647 module_init(cttimeout_init);
648 module_exit(cttimeout_exit);