nfnetlink.c 16 KB

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