nfnetlink.c 14 KB

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