nfnetlink_cttimeout.c 15 KB

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