nfnetlink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* Netfilter messages via netlink socket. Allows for user space
  2. * protocol helpers and general trouble making from userspace.
  3. *
  4. * (C) 2001 by Jay Schulist <jschlst@samba.org>,
  5. * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
  6. * (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org>
  7. *
  8. * Initial netfilter messages via netlink development funded and
  9. * generally made possible by Network Robots, Inc. (www.networkrobots.com)
  10. *
  11. * Further development of this code funded by Astaro AG (http://www.astaro.com)
  12. *
  13. * This software may be used and distributed according to the terms
  14. * of the GNU General Public License, incorporated herein by reference.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/socket.h>
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include <linux/sockios.h>
  22. #include <linux/net.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/uaccess.h>
  25. #include <net/sock.h>
  26. #include <linux/init.h>
  27. #include <linux/sched/signal.h>
  28. #include <net/netlink.h>
  29. #include <linux/netfilter/nfnetlink.h>
  30. MODULE_LICENSE("GPL");
  31. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  32. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
  33. #define nfnl_dereference_protected(id) \
  34. rcu_dereference_protected(table[(id)].subsys, \
  35. lockdep_nfnl_is_held((id)))
  36. #define NFNL_MAX_ATTR_COUNT 32
  37. static struct {
  38. struct mutex mutex;
  39. const struct nfnetlink_subsystem __rcu *subsys;
  40. } table[NFNL_SUBSYS_COUNT];
  41. static const int nfnl_group2type[NFNLGRP_MAX+1] = {
  42. [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
  43. [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
  44. [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
  45. [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
  46. [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
  47. [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
  48. [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
  49. [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
  50. [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
  51. };
  52. void nfnl_lock(__u8 subsys_id)
  53. {
  54. mutex_lock(&table[subsys_id].mutex);
  55. }
  56. EXPORT_SYMBOL_GPL(nfnl_lock);
  57. void nfnl_unlock(__u8 subsys_id)
  58. {
  59. mutex_unlock(&table[subsys_id].mutex);
  60. }
  61. EXPORT_SYMBOL_GPL(nfnl_unlock);
  62. #ifdef CONFIG_PROVE_LOCKING
  63. bool lockdep_nfnl_is_held(u8 subsys_id)
  64. {
  65. return lockdep_is_held(&table[subsys_id].mutex);
  66. }
  67. EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
  68. #endif
  69. int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
  70. {
  71. u8 cb_id;
  72. /* Sanity-check attr_count size to avoid stack buffer overflow. */
  73. for (cb_id = 0; cb_id < n->cb_count; cb_id++)
  74. if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT))
  75. return -EINVAL;
  76. nfnl_lock(n->subsys_id);
  77. if (table[n->subsys_id].subsys) {
  78. nfnl_unlock(n->subsys_id);
  79. return -EBUSY;
  80. }
  81. rcu_assign_pointer(table[n->subsys_id].subsys, n);
  82. nfnl_unlock(n->subsys_id);
  83. return 0;
  84. }
  85. EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
  86. int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
  87. {
  88. nfnl_lock(n->subsys_id);
  89. table[n->subsys_id].subsys = NULL;
  90. nfnl_unlock(n->subsys_id);
  91. synchronize_rcu();
  92. return 0;
  93. }
  94. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  95. static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
  96. {
  97. u8 subsys_id = NFNL_SUBSYS_ID(type);
  98. if (subsys_id >= NFNL_SUBSYS_COUNT)
  99. return NULL;
  100. return rcu_dereference(table[subsys_id].subsys);
  101. }
  102. static inline const struct nfnl_callback *
  103. nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
  104. {
  105. u8 cb_id = NFNL_MSG_TYPE(type);
  106. if (cb_id >= ss->cb_count)
  107. return NULL;
  108. return &ss->cb[cb_id];
  109. }
  110. int nfnetlink_has_listeners(struct net *net, unsigned int group)
  111. {
  112. return netlink_has_listeners(net->nfnl, group);
  113. }
  114. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  115. int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
  116. unsigned int group, int echo, gfp_t flags)
  117. {
  118. return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags);
  119. }
  120. EXPORT_SYMBOL_GPL(nfnetlink_send);
  121. int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
  122. {
  123. return netlink_set_err(net->nfnl, portid, group, error);
  124. }
  125. EXPORT_SYMBOL_GPL(nfnetlink_set_err);
  126. int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid)
  127. {
  128. int err;
  129. err = nlmsg_unicast(net->nfnl, skb, portid);
  130. if (err == -EAGAIN)
  131. err = -ENOBUFS;
  132. return err;
  133. }
  134. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  135. /* Process one complete nfnetlink message. */
  136. static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  137. struct netlink_ext_ack *extack)
  138. {
  139. struct net *net = sock_net(skb->sk);
  140. const struct nfnl_callback *nc;
  141. const struct nfnetlink_subsystem *ss;
  142. int type, err;
  143. /* All the messages must at least contain nfgenmsg */
  144. if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
  145. return 0;
  146. type = nlh->nlmsg_type;
  147. replay:
  148. rcu_read_lock();
  149. ss = nfnetlink_get_subsys(type);
  150. if (!ss) {
  151. #ifdef CONFIG_MODULES
  152. rcu_read_unlock();
  153. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  154. rcu_read_lock();
  155. ss = nfnetlink_get_subsys(type);
  156. if (!ss)
  157. #endif
  158. {
  159. rcu_read_unlock();
  160. return -EINVAL;
  161. }
  162. }
  163. nc = nfnetlink_find_client(type, ss);
  164. if (!nc) {
  165. rcu_read_unlock();
  166. return -EINVAL;
  167. }
  168. {
  169. int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
  170. u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  171. struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
  172. struct nlattr *attr = (void *)nlh + min_len;
  173. int attrlen = nlh->nlmsg_len - min_len;
  174. __u8 subsys_id = NFNL_SUBSYS_ID(type);
  175. /* Sanity-check NFNL_MAX_ATTR_COUNT */
  176. if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
  177. rcu_read_unlock();
  178. return -ENOMEM;
  179. }
  180. err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen,
  181. ss->cb[cb_id].policy, extack);
  182. if (err < 0) {
  183. rcu_read_unlock();
  184. return err;
  185. }
  186. if (nc->call_rcu) {
  187. err = nc->call_rcu(net, net->nfnl, skb, nlh,
  188. (const struct nlattr **)cda,
  189. extack);
  190. rcu_read_unlock();
  191. } else {
  192. rcu_read_unlock();
  193. nfnl_lock(subsys_id);
  194. if (nfnl_dereference_protected(subsys_id) != ss ||
  195. nfnetlink_find_client(type, ss) != nc)
  196. err = -EAGAIN;
  197. else if (nc->call)
  198. err = nc->call(net, net->nfnl, skb, nlh,
  199. (const struct nlattr **)cda,
  200. extack);
  201. else
  202. err = -EINVAL;
  203. nfnl_unlock(subsys_id);
  204. }
  205. if (err == -EAGAIN)
  206. goto replay;
  207. return err;
  208. }
  209. }
  210. struct nfnl_err {
  211. struct list_head head;
  212. struct nlmsghdr *nlh;
  213. int err;
  214. struct netlink_ext_ack extack;
  215. };
  216. static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
  217. const struct netlink_ext_ack *extack)
  218. {
  219. struct nfnl_err *nfnl_err;
  220. nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
  221. if (nfnl_err == NULL)
  222. return -ENOMEM;
  223. nfnl_err->nlh = nlh;
  224. nfnl_err->err = err;
  225. nfnl_err->extack = *extack;
  226. list_add_tail(&nfnl_err->head, list);
  227. return 0;
  228. }
  229. static void nfnl_err_del(struct nfnl_err *nfnl_err)
  230. {
  231. list_del(&nfnl_err->head);
  232. kfree(nfnl_err);
  233. }
  234. static void nfnl_err_reset(struct list_head *err_list)
  235. {
  236. struct nfnl_err *nfnl_err, *next;
  237. list_for_each_entry_safe(nfnl_err, next, err_list, head)
  238. nfnl_err_del(nfnl_err);
  239. }
  240. static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
  241. {
  242. struct nfnl_err *nfnl_err, *next;
  243. list_for_each_entry_safe(nfnl_err, next, err_list, head) {
  244. netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
  245. &nfnl_err->extack);
  246. nfnl_err_del(nfnl_err);
  247. }
  248. }
  249. enum {
  250. NFNL_BATCH_FAILURE = (1 << 0),
  251. NFNL_BATCH_DONE = (1 << 1),
  252. NFNL_BATCH_REPLAY = (1 << 2),
  253. };
  254. static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
  255. u16 subsys_id, u32 genid)
  256. {
  257. struct sk_buff *oskb = skb;
  258. struct net *net = sock_net(skb->sk);
  259. const struct nfnetlink_subsystem *ss;
  260. const struct nfnl_callback *nc;
  261. struct netlink_ext_ack extack;
  262. LIST_HEAD(err_list);
  263. u32 status;
  264. int err;
  265. if (subsys_id >= NFNL_SUBSYS_COUNT)
  266. return netlink_ack(skb, nlh, -EINVAL, NULL);
  267. replay:
  268. status = 0;
  269. skb = netlink_skb_clone(oskb, GFP_KERNEL);
  270. if (!skb)
  271. return netlink_ack(oskb, nlh, -ENOMEM, NULL);
  272. nfnl_lock(subsys_id);
  273. ss = nfnl_dereference_protected(subsys_id);
  274. if (!ss) {
  275. #ifdef CONFIG_MODULES
  276. nfnl_unlock(subsys_id);
  277. request_module("nfnetlink-subsys-%d", subsys_id);
  278. nfnl_lock(subsys_id);
  279. ss = nfnl_dereference_protected(subsys_id);
  280. if (!ss)
  281. #endif
  282. {
  283. nfnl_unlock(subsys_id);
  284. netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
  285. return kfree_skb(skb);
  286. }
  287. }
  288. if (!ss->valid_genid || !ss->commit || !ss->abort) {
  289. nfnl_unlock(subsys_id);
  290. netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
  291. return kfree_skb(skb);
  292. }
  293. if (!try_module_get(ss->owner)) {
  294. nfnl_unlock(subsys_id);
  295. netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
  296. return kfree_skb(skb);
  297. }
  298. if (!ss->valid_genid(net, genid)) {
  299. module_put(ss->owner);
  300. nfnl_unlock(subsys_id);
  301. netlink_ack(oskb, nlh, -ERESTART, NULL);
  302. return kfree_skb(skb);
  303. }
  304. nfnl_unlock(subsys_id);
  305. while (skb->len >= nlmsg_total_size(0)) {
  306. int msglen, type;
  307. if (fatal_signal_pending(current)) {
  308. nfnl_err_reset(&err_list);
  309. err = -EINTR;
  310. status = NFNL_BATCH_FAILURE;
  311. goto done;
  312. }
  313. memset(&extack, 0, sizeof(extack));
  314. nlh = nlmsg_hdr(skb);
  315. err = 0;
  316. if (nlh->nlmsg_len < NLMSG_HDRLEN ||
  317. skb->len < nlh->nlmsg_len ||
  318. nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
  319. nfnl_err_reset(&err_list);
  320. status |= NFNL_BATCH_FAILURE;
  321. goto done;
  322. }
  323. /* Only requests are handled by the kernel */
  324. if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
  325. err = -EINVAL;
  326. goto ack;
  327. }
  328. type = nlh->nlmsg_type;
  329. if (type == NFNL_MSG_BATCH_BEGIN) {
  330. /* Malformed: Batch begin twice */
  331. nfnl_err_reset(&err_list);
  332. status |= NFNL_BATCH_FAILURE;
  333. goto done;
  334. } else if (type == NFNL_MSG_BATCH_END) {
  335. status |= NFNL_BATCH_DONE;
  336. goto done;
  337. } else if (type < NLMSG_MIN_TYPE) {
  338. err = -EINVAL;
  339. goto ack;
  340. }
  341. /* We only accept a batch with messages for the same
  342. * subsystem.
  343. */
  344. if (NFNL_SUBSYS_ID(type) != subsys_id) {
  345. err = -EINVAL;
  346. goto ack;
  347. }
  348. nc = nfnetlink_find_client(type, ss);
  349. if (!nc) {
  350. err = -EINVAL;
  351. goto ack;
  352. }
  353. {
  354. int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
  355. u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  356. struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
  357. struct nlattr *attr = (void *)nlh + min_len;
  358. int attrlen = nlh->nlmsg_len - min_len;
  359. /* Sanity-check NFTA_MAX_ATTR */
  360. if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
  361. err = -ENOMEM;
  362. goto ack;
  363. }
  364. err = nla_parse(cda, ss->cb[cb_id].attr_count, attr,
  365. attrlen, ss->cb[cb_id].policy, NULL);
  366. if (err < 0)
  367. goto ack;
  368. if (nc->call_batch) {
  369. err = nc->call_batch(net, net->nfnl, skb, nlh,
  370. (const struct nlattr **)cda,
  371. &extack);
  372. }
  373. /* The lock was released to autoload some module, we
  374. * have to abort and start from scratch using the
  375. * original skb.
  376. */
  377. if (err == -EAGAIN) {
  378. status |= NFNL_BATCH_REPLAY;
  379. goto done;
  380. }
  381. }
  382. ack:
  383. if (nlh->nlmsg_flags & NLM_F_ACK || err) {
  384. /* Errors are delivered once the full batch has been
  385. * processed, this avoids that the same error is
  386. * reported several times when replaying the batch.
  387. */
  388. if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
  389. /* We failed to enqueue an error, reset the
  390. * list of errors and send OOM to userspace
  391. * pointing to the batch header.
  392. */
  393. nfnl_err_reset(&err_list);
  394. netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
  395. NULL);
  396. status |= NFNL_BATCH_FAILURE;
  397. goto done;
  398. }
  399. /* We don't stop processing the batch on errors, thus,
  400. * userspace gets all the errors that the batch
  401. * triggers.
  402. */
  403. if (err)
  404. status |= NFNL_BATCH_FAILURE;
  405. }
  406. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  407. if (msglen > skb->len)
  408. msglen = skb->len;
  409. skb_pull(skb, msglen);
  410. }
  411. done:
  412. if (status & NFNL_BATCH_REPLAY) {
  413. ss->abort(net, oskb);
  414. nfnl_err_reset(&err_list);
  415. kfree_skb(skb);
  416. module_put(ss->owner);
  417. goto replay;
  418. } else if (status == NFNL_BATCH_DONE) {
  419. err = ss->commit(net, oskb);
  420. if (err == -EAGAIN) {
  421. status |= NFNL_BATCH_REPLAY;
  422. goto done;
  423. } else if (err) {
  424. ss->abort(net, oskb);
  425. netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
  426. }
  427. } else {
  428. ss->abort(net, oskb);
  429. }
  430. if (ss->cleanup)
  431. ss->cleanup(net);
  432. nfnl_err_deliver(&err_list, oskb);
  433. kfree_skb(skb);
  434. module_put(ss->owner);
  435. }
  436. static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
  437. [NFNL_BATCH_GENID] = { .type = NLA_U32 },
  438. };
  439. static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
  440. {
  441. int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
  442. struct nlattr *attr = (void *)nlh + min_len;
  443. struct nlattr *cda[NFNL_BATCH_MAX + 1];
  444. int attrlen = nlh->nlmsg_len - min_len;
  445. struct nfgenmsg *nfgenmsg;
  446. int msglen, err;
  447. u32 gen_id = 0;
  448. u16 res_id;
  449. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  450. if (msglen > skb->len)
  451. msglen = skb->len;
  452. if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
  453. return;
  454. err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy,
  455. NULL);
  456. if (err < 0) {
  457. netlink_ack(skb, nlh, err, NULL);
  458. return;
  459. }
  460. if (cda[NFNL_BATCH_GENID])
  461. gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
  462. nfgenmsg = nlmsg_data(nlh);
  463. skb_pull(skb, msglen);
  464. /* Work around old nft using host byte order */
  465. if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
  466. res_id = NFNL_SUBSYS_NFTABLES;
  467. else
  468. res_id = ntohs(nfgenmsg->res_id);
  469. nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
  470. }
  471. static void nfnetlink_rcv(struct sk_buff *skb)
  472. {
  473. struct nlmsghdr *nlh = nlmsg_hdr(skb);
  474. if (skb->len < NLMSG_HDRLEN ||
  475. nlh->nlmsg_len < NLMSG_HDRLEN ||
  476. skb->len < nlh->nlmsg_len)
  477. return;
  478. if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
  479. netlink_ack(skb, nlh, -EPERM, NULL);
  480. return;
  481. }
  482. if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
  483. nfnetlink_rcv_skb_batch(skb, nlh);
  484. else
  485. netlink_rcv_skb(skb, nfnetlink_rcv_msg);
  486. }
  487. #ifdef CONFIG_MODULES
  488. static int nfnetlink_bind(struct net *net, int group)
  489. {
  490. const struct nfnetlink_subsystem *ss;
  491. int type;
  492. if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
  493. return 0;
  494. type = nfnl_group2type[group];
  495. rcu_read_lock();
  496. ss = nfnetlink_get_subsys(type << 8);
  497. rcu_read_unlock();
  498. if (!ss)
  499. request_module_nowait("nfnetlink-subsys-%d", type);
  500. return 0;
  501. }
  502. #endif
  503. static int __net_init nfnetlink_net_init(struct net *net)
  504. {
  505. struct sock *nfnl;
  506. struct netlink_kernel_cfg cfg = {
  507. .groups = NFNLGRP_MAX,
  508. .input = nfnetlink_rcv,
  509. #ifdef CONFIG_MODULES
  510. .bind = nfnetlink_bind,
  511. #endif
  512. };
  513. nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
  514. if (!nfnl)
  515. return -ENOMEM;
  516. net->nfnl_stash = nfnl;
  517. rcu_assign_pointer(net->nfnl, nfnl);
  518. return 0;
  519. }
  520. static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
  521. {
  522. struct net *net;
  523. list_for_each_entry(net, net_exit_list, exit_list)
  524. RCU_INIT_POINTER(net->nfnl, NULL);
  525. synchronize_net();
  526. list_for_each_entry(net, net_exit_list, exit_list)
  527. netlink_kernel_release(net->nfnl_stash);
  528. }
  529. static struct pernet_operations nfnetlink_net_ops = {
  530. .init = nfnetlink_net_init,
  531. .exit_batch = nfnetlink_net_exit_batch,
  532. };
  533. static int __init nfnetlink_init(void)
  534. {
  535. int i;
  536. for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
  537. BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
  538. for (i=0; i<NFNL_SUBSYS_COUNT; i++)
  539. mutex_init(&table[i].mutex);
  540. return register_pernet_subsys(&nfnetlink_net_ops);
  541. }
  542. static void __exit nfnetlink_exit(void)
  543. {
  544. unregister_pernet_subsys(&nfnetlink_net_ops);
  545. }
  546. module_init(nfnetlink_init);
  547. module_exit(nfnetlink_exit);