nfnetlink.c 13 KB

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