1 /* 2 * (C) 2012 by Pablo Neira Ayuso <[email protected]> 3 * (C) 2012 by Vyatta Inc. <http://www.vyatta.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation (or any later at your option). 8 */ 9 #include <linux/init.h> 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/rculist.h> 13 #include <linux/rculist_nulls.h> 14 #include <linux/types.h> 15 #include <linux/timer.h> 16 #include <linux/security.h> 17 #include <linux/skbuff.h> 18 #include <linux/errno.h> 19 #include <linux/netlink.h> 20 #include <linux/spinlock.h> 21 #include <linux/interrupt.h> 22 #include <linux/slab.h> 23 24 #include <linux/netfilter.h> 25 #include <net/netlink.h> 26 #include <net/sock.h> 27 #include <net/netfilter/nf_conntrack.h> 28 #include <net/netfilter/nf_conntrack_core.h> 29 #include <net/netfilter/nf_conntrack_l4proto.h> 30 #include <net/netfilter/nf_conntrack_tuple.h> 31 #include <net/netfilter/nf_conntrack_timeout.h> 32 33 #include <linux/netfilter/nfnetlink.h> 34 #include <linux/netfilter/nfnetlink_cttimeout.h> 35 36 MODULE_LICENSE("GPL"); 37 MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>"); 38 MODULE_DESCRIPTION("cttimeout: Extended Netfilter Connection Tracking timeout tuning"); 39 40 static const struct nla_policy cttimeout_nla_policy[CTA_TIMEOUT_MAX+1] = { 41 [CTA_TIMEOUT_NAME] = { .type = NLA_NUL_STRING, 42 .len = CTNL_TIMEOUT_NAME_MAX - 1}, 43 [CTA_TIMEOUT_L3PROTO] = { .type = NLA_U16 }, 44 [CTA_TIMEOUT_L4PROTO] = { .type = NLA_U8 }, 45 [CTA_TIMEOUT_DATA] = { .type = NLA_NESTED }, 46 }; 47 48 static int 49 ctnl_timeout_parse_policy(void *timeout, 50 const struct nf_conntrack_l4proto *l4proto, 51 struct net *net, const struct nlattr *attr) 52 { 53 struct nlattr **tb; 54 int ret = 0; 55 56 tb = kcalloc(l4proto->ctnl_timeout.nlattr_max + 1, sizeof(*tb), 57 GFP_KERNEL); 58 59 if (!tb) 60 return -ENOMEM; 61 62 ret = nla_parse_nested_deprecated(tb, 63 l4proto->ctnl_timeout.nlattr_max, 64 attr, 65 l4proto->ctnl_timeout.nla_policy, 66 NULL); 67 if (ret < 0) 68 goto err; 69 70 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, timeout); 71 72 err: 73 kfree(tb); 74 return ret; 75 } 76 77 static int cttimeout_new_timeout(struct net *net, struct sock *ctnl, 78 struct sk_buff *skb, 79 const struct nlmsghdr *nlh, 80 const struct nlattr * const cda[], 81 struct netlink_ext_ack *extack) 82 { 83 __u16 l3num; 84 __u8 l4num; 85 const struct nf_conntrack_l4proto *l4proto; 86 struct ctnl_timeout *timeout, *matching = NULL; 87 char *name; 88 int ret; 89 90 if (!cda[CTA_TIMEOUT_NAME] || 91 !cda[CTA_TIMEOUT_L3PROTO] || 92 !cda[CTA_TIMEOUT_L4PROTO] || 93 !cda[CTA_TIMEOUT_DATA]) 94 return -EINVAL; 95 96 name = nla_data(cda[CTA_TIMEOUT_NAME]); 97 l3num = ntohs(nla_get_be16(cda[CTA_TIMEOUT_L3PROTO])); 98 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]); 99 100 list_for_each_entry(timeout, &net->nfct_timeout_list, head) { 101 if (strncmp(timeout->name, name, CTNL_TIMEOUT_NAME_MAX) != 0) 102 continue; 103 104 if (nlh->nlmsg_flags & NLM_F_EXCL) 105 return -EEXIST; 106 107 matching = timeout; 108 break; 109 } 110 111 if (matching) { 112 if (nlh->nlmsg_flags & NLM_F_REPLACE) { 113 /* You cannot replace one timeout policy by another of 114 * different kind, sorry. 115 */ 116 if (matching->timeout.l3num != l3num || 117 matching->timeout.l4proto->l4proto != l4num) 118 return -EINVAL; 119 120 return ctnl_timeout_parse_policy(&matching->timeout.data, 121 matching->timeout.l4proto, 122 net, cda[CTA_TIMEOUT_DATA]); 123 } 124 125 return -EBUSY; 126 } 127 128 l4proto = nf_ct_l4proto_find(l4num); 129 130 /* This protocol is not supportted, skip. */ 131 if (l4proto->l4proto != l4num) { 132 ret = -EOPNOTSUPP; 133 goto err_proto_put; 134 } 135 136 timeout = kzalloc(sizeof(struct ctnl_timeout) + 137 l4proto->ctnl_timeout.obj_size, GFP_KERNEL); 138 if (timeout == NULL) { 139 ret = -ENOMEM; 140 goto err_proto_put; 141 } 142 143 ret = ctnl_timeout_parse_policy(&timeout->timeout.data, l4proto, net, 144 cda[CTA_TIMEOUT_DATA]); 145 if (ret < 0) 146 goto err; 147 148 strcpy(timeout->name, nla_data(cda[CTA_TIMEOUT_NAME])); 149 timeout->timeout.l3num = l3num; 150 timeout->timeout.l4proto = l4proto; 151 refcount_set(&timeout->refcnt, 1); 152 list_add_tail_rcu(&timeout->head, &net->nfct_timeout_list); 153 154 return 0; 155 err: 156 kfree(timeout); 157 err_proto_put: 158 return ret; 159 } 160 161 static int 162 ctnl_timeout_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, 163 int event, struct ctnl_timeout *timeout) 164 { 165 struct nlmsghdr *nlh; 166 struct nfgenmsg *nfmsg; 167 unsigned int flags = portid ? NLM_F_MULTI : 0; 168 const struct nf_conntrack_l4proto *l4proto = timeout->timeout.l4proto; 169 struct nlattr *nest_parms; 170 int ret; 171 172 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_TIMEOUT, event); 173 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); 174 if (nlh == NULL) 175 goto nlmsg_failure; 176 177 nfmsg = nlmsg_data(nlh); 178 nfmsg->nfgen_family = AF_UNSPEC; 179 nfmsg->version = NFNETLINK_V0; 180 nfmsg->res_id = 0; 181 182 if (nla_put_string(skb, CTA_TIMEOUT_NAME, timeout->name) || 183 nla_put_be16(skb, CTA_TIMEOUT_L3PROTO, 184 htons(timeout->timeout.l3num)) || 185 nla_put_u8(skb, CTA_TIMEOUT_L4PROTO, l4proto->l4proto) || 186 nla_put_be32(skb, CTA_TIMEOUT_USE, 187 htonl(refcount_read(&timeout->refcnt)))) 188 goto nla_put_failure; 189 190 nest_parms = nla_nest_start(skb, CTA_TIMEOUT_DATA); 191 if (!nest_parms) 192 goto nla_put_failure; 193 194 ret = l4proto->ctnl_timeout.obj_to_nlattr(skb, &timeout->timeout.data); 195 if (ret < 0) 196 goto nla_put_failure; 197 198 nla_nest_end(skb, nest_parms); 199 200 nlmsg_end(skb, nlh); 201 return skb->len; 202 203 nlmsg_failure: 204 nla_put_failure: 205 nlmsg_cancel(skb, nlh); 206 return -1; 207 } 208 209 static int 210 ctnl_timeout_dump(struct sk_buff *skb, struct netlink_callback *cb) 211 { 212 struct net *net = sock_net(skb->sk); 213 struct ctnl_timeout *cur, *last; 214 215 if (cb->args[2]) 216 return 0; 217 218 last = (struct ctnl_timeout *)cb->args[1]; 219 if (cb->args[1]) 220 cb->args[1] = 0; 221 222 rcu_read_lock(); 223 list_for_each_entry_rcu(cur, &net->nfct_timeout_list, head) { 224 if (last) { 225 if (cur != last) 226 continue; 227 228 last = NULL; 229 } 230 if (ctnl_timeout_fill_info(skb, NETLINK_CB(cb->skb).portid, 231 cb->nlh->nlmsg_seq, 232 NFNL_MSG_TYPE(cb->nlh->nlmsg_type), 233 IPCTNL_MSG_TIMEOUT_NEW, cur) < 0) { 234 cb->args[1] = (unsigned long)cur; 235 break; 236 } 237 } 238 if (!cb->args[1]) 239 cb->args[2] = 1; 240 rcu_read_unlock(); 241 return skb->len; 242 } 243 244 static int cttimeout_get_timeout(struct net *net, struct sock *ctnl, 245 struct sk_buff *skb, 246 const struct nlmsghdr *nlh, 247 const struct nlattr * const cda[], 248 struct netlink_ext_ack *extack) 249 { 250 int ret = -ENOENT; 251 char *name; 252 struct ctnl_timeout *cur; 253 254 if (nlh->nlmsg_flags & NLM_F_DUMP) { 255 struct netlink_dump_control c = { 256 .dump = ctnl_timeout_dump, 257 }; 258 return netlink_dump_start(ctnl, skb, nlh, &c); 259 } 260 261 if (!cda[CTA_TIMEOUT_NAME]) 262 return -EINVAL; 263 name = nla_data(cda[CTA_TIMEOUT_NAME]); 264 265 list_for_each_entry(cur, &net->nfct_timeout_list, head) { 266 struct sk_buff *skb2; 267 268 if (strncmp(cur->name, name, CTNL_TIMEOUT_NAME_MAX) != 0) 269 continue; 270 271 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 272 if (skb2 == NULL) { 273 ret = -ENOMEM; 274 break; 275 } 276 277 ret = ctnl_timeout_fill_info(skb2, NETLINK_CB(skb).portid, 278 nlh->nlmsg_seq, 279 NFNL_MSG_TYPE(nlh->nlmsg_type), 280 IPCTNL_MSG_TIMEOUT_NEW, cur); 281 if (ret <= 0) { 282 kfree_skb(skb2); 283 break; 284 } 285 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, 286 MSG_DONTWAIT); 287 if (ret > 0) 288 ret = 0; 289 290 /* this avoids a loop in nfnetlink. */ 291 return ret == -EAGAIN ? -ENOBUFS : ret; 292 } 293 return ret; 294 } 295 296 /* try to delete object, fail if it is still in use. */ 297 static int ctnl_timeout_try_del(struct net *net, struct ctnl_timeout *timeout) 298 { 299 int ret = 0; 300 301 /* We want to avoid races with ctnl_timeout_put. So only when the 302 * current refcnt is 1, we decrease it to 0. 303 */ 304 if (refcount_dec_if_one(&timeout->refcnt)) { 305 /* We are protected by nfnl mutex. */ 306 list_del_rcu(&timeout->head); 307 nf_ct_untimeout(net, &timeout->timeout); 308 kfree_rcu(timeout, rcu_head); 309 } else { 310 ret = -EBUSY; 311 } 312 return ret; 313 } 314 315 static int cttimeout_del_timeout(struct net *net, struct sock *ctnl, 316 struct sk_buff *skb, 317 const struct nlmsghdr *nlh, 318 const struct nlattr * const cda[], 319 struct netlink_ext_ack *extack) 320 { 321 struct ctnl_timeout *cur, *tmp; 322 int ret = -ENOENT; 323 char *name; 324 325 if (!cda[CTA_TIMEOUT_NAME]) { 326 list_for_each_entry_safe(cur, tmp, &net->nfct_timeout_list, 327 head) 328 ctnl_timeout_try_del(net, cur); 329 330 return 0; 331 } 332 name = nla_data(cda[CTA_TIMEOUT_NAME]); 333 334 list_for_each_entry(cur, &net->nfct_timeout_list, head) { 335 if (strncmp(cur->name, name, CTNL_TIMEOUT_NAME_MAX) != 0) 336 continue; 337 338 ret = ctnl_timeout_try_del(net, cur); 339 if (ret < 0) 340 return ret; 341 342 break; 343 } 344 return ret; 345 } 346 347 static int cttimeout_default_set(struct net *net, struct sock *ctnl, 348 struct sk_buff *skb, 349 const struct nlmsghdr *nlh, 350 const struct nlattr * const cda[], 351 struct netlink_ext_ack *extack) 352 { 353 const struct nf_conntrack_l4proto *l4proto; 354 __u8 l4num; 355 int ret; 356 357 if (!cda[CTA_TIMEOUT_L3PROTO] || 358 !cda[CTA_TIMEOUT_L4PROTO] || 359 !cda[CTA_TIMEOUT_DATA]) 360 return -EINVAL; 361 362 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]); 363 l4proto = nf_ct_l4proto_find(l4num); 364 365 /* This protocol is not supported, skip. */ 366 if (l4proto->l4proto != l4num) { 367 ret = -EOPNOTSUPP; 368 goto err; 369 } 370 371 ret = ctnl_timeout_parse_policy(NULL, l4proto, net, 372 cda[CTA_TIMEOUT_DATA]); 373 if (ret < 0) 374 goto err; 375 376 return 0; 377 err: 378 return ret; 379 } 380 381 static int 382 cttimeout_default_fill_info(struct net *net, struct sk_buff *skb, u32 portid, 383 u32 seq, u32 type, int event, u16 l3num, 384 const struct nf_conntrack_l4proto *l4proto, 385 const unsigned int *timeouts) 386 { 387 struct nlmsghdr *nlh; 388 struct nfgenmsg *nfmsg; 389 unsigned int flags = portid ? NLM_F_MULTI : 0; 390 struct nlattr *nest_parms; 391 int ret; 392 393 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_TIMEOUT, event); 394 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); 395 if (nlh == NULL) 396 goto nlmsg_failure; 397 398 nfmsg = nlmsg_data(nlh); 399 nfmsg->nfgen_family = AF_UNSPEC; 400 nfmsg->version = NFNETLINK_V0; 401 nfmsg->res_id = 0; 402 403 if (nla_put_be16(skb, CTA_TIMEOUT_L3PROTO, htons(l3num)) || 404 nla_put_u8(skb, CTA_TIMEOUT_L4PROTO, l4proto->l4proto)) 405 goto nla_put_failure; 406 407 nest_parms = nla_nest_start(skb, CTA_TIMEOUT_DATA); 408 if (!nest_parms) 409 goto nla_put_failure; 410 411 ret = l4proto->ctnl_timeout.obj_to_nlattr(skb, timeouts); 412 if (ret < 0) 413 goto nla_put_failure; 414 415 nla_nest_end(skb, nest_parms); 416 417 nlmsg_end(skb, nlh); 418 return skb->len; 419 420 nlmsg_failure: 421 nla_put_failure: 422 nlmsg_cancel(skb, nlh); 423 return -1; 424 } 425 426 static int cttimeout_default_get(struct net *net, struct sock *ctnl, 427 struct sk_buff *skb, 428 const struct nlmsghdr *nlh, 429 const struct nlattr * const cda[], 430 struct netlink_ext_ack *extack) 431 { 432 const struct nf_conntrack_l4proto *l4proto; 433 unsigned int *timeouts = NULL; 434 struct sk_buff *skb2; 435 int ret, err; 436 __u16 l3num; 437 __u8 l4num; 438 439 if (!cda[CTA_TIMEOUT_L3PROTO] || !cda[CTA_TIMEOUT_L4PROTO]) 440 return -EINVAL; 441 442 l3num = ntohs(nla_get_be16(cda[CTA_TIMEOUT_L3PROTO])); 443 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]); 444 l4proto = nf_ct_l4proto_find(l4num); 445 446 err = -EOPNOTSUPP; 447 if (l4proto->l4proto != l4num) 448 goto err; 449 450 switch (l4proto->l4proto) { 451 case IPPROTO_ICMP: 452 timeouts = &nf_icmp_pernet(net)->timeout; 453 break; 454 case IPPROTO_TCP: 455 timeouts = nf_tcp_pernet(net)->timeouts; 456 break; 457 case IPPROTO_UDP: /* fallthrough */ 458 case IPPROTO_UDPLITE: 459 timeouts = nf_udp_pernet(net)->timeouts; 460 break; 461 case IPPROTO_DCCP: 462 #ifdef CONFIG_NF_CT_PROTO_DCCP 463 timeouts = nf_dccp_pernet(net)->dccp_timeout; 464 #endif 465 break; 466 case IPPROTO_ICMPV6: 467 timeouts = &nf_icmpv6_pernet(net)->timeout; 468 break; 469 case IPPROTO_SCTP: 470 #ifdef CONFIG_NF_CT_PROTO_SCTP 471 timeouts = nf_sctp_pernet(net)->timeouts; 472 #endif 473 break; 474 case IPPROTO_GRE: 475 #ifdef CONFIG_NF_CT_PROTO_GRE 476 timeouts = nf_gre_pernet(net)->timeouts; 477 #endif 478 break; 479 case 255: 480 timeouts = &nf_generic_pernet(net)->timeout; 481 break; 482 default: 483 WARN_ONCE(1, "Missing timeouts for proto %d", l4proto->l4proto); 484 break; 485 } 486 487 if (!timeouts) 488 goto err; 489 490 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 491 if (skb2 == NULL) { 492 err = -ENOMEM; 493 goto err; 494 } 495 496 ret = cttimeout_default_fill_info(net, skb2, NETLINK_CB(skb).portid, 497 nlh->nlmsg_seq, 498 NFNL_MSG_TYPE(nlh->nlmsg_type), 499 IPCTNL_MSG_TIMEOUT_DEFAULT_SET, 500 l3num, l4proto, timeouts); 501 if (ret <= 0) { 502 kfree_skb(skb2); 503 err = -ENOMEM; 504 goto err; 505 } 506 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT); 507 if (ret > 0) 508 ret = 0; 509 510 /* this avoids a loop in nfnetlink. */ 511 return ret == -EAGAIN ? -ENOBUFS : ret; 512 err: 513 return err; 514 } 515 516 static struct nf_ct_timeout *ctnl_timeout_find_get(struct net *net, 517 const char *name) 518 { 519 struct ctnl_timeout *timeout, *matching = NULL; 520 521 list_for_each_entry_rcu(timeout, &net->nfct_timeout_list, head) { 522 if (strncmp(timeout->name, name, CTNL_TIMEOUT_NAME_MAX) != 0) 523 continue; 524 525 if (!try_module_get(THIS_MODULE)) 526 goto err; 527 528 if (!refcount_inc_not_zero(&timeout->refcnt)) { 529 module_put(THIS_MODULE); 530 goto err; 531 } 532 matching = timeout; 533 break; 534 } 535 err: 536 return matching ? &matching->timeout : NULL; 537 } 538 539 static void ctnl_timeout_put(struct nf_ct_timeout *t) 540 { 541 struct ctnl_timeout *timeout = 542 container_of(t, struct ctnl_timeout, timeout); 543 544 if (refcount_dec_and_test(&timeout->refcnt)) 545 kfree_rcu(timeout, rcu_head); 546 547 module_put(THIS_MODULE); 548 } 549 550 static const struct nfnl_callback cttimeout_cb[IPCTNL_MSG_TIMEOUT_MAX] = { 551 [IPCTNL_MSG_TIMEOUT_NEW] = { .call = cttimeout_new_timeout, 552 .attr_count = CTA_TIMEOUT_MAX, 553 .policy = cttimeout_nla_policy }, 554 [IPCTNL_MSG_TIMEOUT_GET] = { .call = cttimeout_get_timeout, 555 .attr_count = CTA_TIMEOUT_MAX, 556 .policy = cttimeout_nla_policy }, 557 [IPCTNL_MSG_TIMEOUT_DELETE] = { .call = cttimeout_del_timeout, 558 .attr_count = CTA_TIMEOUT_MAX, 559 .policy = cttimeout_nla_policy }, 560 [IPCTNL_MSG_TIMEOUT_DEFAULT_SET]= { .call = cttimeout_default_set, 561 .attr_count = CTA_TIMEOUT_MAX, 562 .policy = cttimeout_nla_policy }, 563 [IPCTNL_MSG_TIMEOUT_DEFAULT_GET]= { .call = cttimeout_default_get, 564 .attr_count = CTA_TIMEOUT_MAX, 565 .policy = cttimeout_nla_policy }, 566 }; 567 568 static const struct nfnetlink_subsystem cttimeout_subsys = { 569 .name = "conntrack_timeout", 570 .subsys_id = NFNL_SUBSYS_CTNETLINK_TIMEOUT, 571 .cb_count = IPCTNL_MSG_TIMEOUT_MAX, 572 .cb = cttimeout_cb, 573 }; 574 575 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_TIMEOUT); 576 577 static int __net_init cttimeout_net_init(struct net *net) 578 { 579 INIT_LIST_HEAD(&net->nfct_timeout_list); 580 581 return 0; 582 } 583 584 static void __net_exit cttimeout_net_exit(struct net *net) 585 { 586 struct ctnl_timeout *cur, *tmp; 587 588 nf_ct_unconfirmed_destroy(net); 589 nf_ct_untimeout(net, NULL); 590 591 list_for_each_entry_safe(cur, tmp, &net->nfct_timeout_list, head) { 592 list_del_rcu(&cur->head); 593 594 if (refcount_dec_and_test(&cur->refcnt)) 595 kfree_rcu(cur, rcu_head); 596 } 597 } 598 599 static struct pernet_operations cttimeout_ops = { 600 .init = cttimeout_net_init, 601 .exit = cttimeout_net_exit, 602 }; 603 604 static int __init cttimeout_init(void) 605 { 606 int ret; 607 608 ret = register_pernet_subsys(&cttimeout_ops); 609 if (ret < 0) 610 return ret; 611 612 ret = nfnetlink_subsys_register(&cttimeout_subsys); 613 if (ret < 0) { 614 pr_err("cttimeout_init: cannot register cttimeout with " 615 "nfnetlink.\n"); 616 goto err_out; 617 } 618 RCU_INIT_POINTER(nf_ct_timeout_find_get_hook, ctnl_timeout_find_get); 619 RCU_INIT_POINTER(nf_ct_timeout_put_hook, ctnl_timeout_put); 620 return 0; 621 622 err_out: 623 unregister_pernet_subsys(&cttimeout_ops); 624 return ret; 625 } 626 627 static void __exit cttimeout_exit(void) 628 { 629 nfnetlink_subsys_unregister(&cttimeout_subsys); 630 631 unregister_pernet_subsys(&cttimeout_ops); 632 RCU_INIT_POINTER(nf_ct_timeout_find_get_hook, NULL); 633 RCU_INIT_POINTER(nf_ct_timeout_put_hook, NULL); 634 synchronize_rcu(); 635 } 636 637 module_init(cttimeout_init); 638 module_exit(cttimeout_exit); 639