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