nfnetlink.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. int 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. static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
  232. u_int16_t subsys_id)
  233. {
  234. struct sk_buff *oskb = skb;
  235. struct net *net = sock_net(skb->sk);
  236. const struct nfnetlink_subsystem *ss;
  237. const struct nfnl_callback *nc;
  238. bool success = true, done = false;
  239. static LIST_HEAD(err_list);
  240. int err;
  241. if (subsys_id >= NFNL_SUBSYS_COUNT)
  242. return netlink_ack(skb, nlh, -EINVAL);
  243. replay:
  244. skb = netlink_skb_clone(oskb, GFP_KERNEL);
  245. if (!skb)
  246. return netlink_ack(oskb, nlh, -ENOMEM);
  247. skb->sk = oskb->sk;
  248. nfnl_lock(subsys_id);
  249. ss = rcu_dereference_protected(table[subsys_id].subsys,
  250. lockdep_is_held(&table[subsys_id].mutex));
  251. if (!ss) {
  252. #ifdef CONFIG_MODULES
  253. nfnl_unlock(subsys_id);
  254. request_module("nfnetlink-subsys-%d", subsys_id);
  255. nfnl_lock(subsys_id);
  256. ss = rcu_dereference_protected(table[subsys_id].subsys,
  257. lockdep_is_held(&table[subsys_id].mutex));
  258. if (!ss)
  259. #endif
  260. {
  261. nfnl_unlock(subsys_id);
  262. netlink_ack(skb, nlh, -EOPNOTSUPP);
  263. return kfree_skb(skb);
  264. }
  265. }
  266. if (!ss->commit || !ss->abort) {
  267. nfnl_unlock(subsys_id);
  268. netlink_ack(skb, nlh, -EOPNOTSUPP);
  269. return kfree_skb(skb);
  270. }
  271. while (skb->len >= nlmsg_total_size(0)) {
  272. int msglen, type;
  273. nlh = nlmsg_hdr(skb);
  274. err = 0;
  275. if (nlmsg_len(nlh) < sizeof(struct nfgenmsg) ||
  276. skb->len < nlh->nlmsg_len) {
  277. err = -EINVAL;
  278. goto ack;
  279. }
  280. /* Only requests are handled by the kernel */
  281. if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
  282. err = -EINVAL;
  283. goto ack;
  284. }
  285. type = nlh->nlmsg_type;
  286. if (type == NFNL_MSG_BATCH_BEGIN) {
  287. /* Malformed: Batch begin twice */
  288. nfnl_err_reset(&err_list);
  289. success = false;
  290. goto done;
  291. } else if (type == NFNL_MSG_BATCH_END) {
  292. done = true;
  293. goto done;
  294. } else if (type < NLMSG_MIN_TYPE) {
  295. err = -EINVAL;
  296. goto ack;
  297. }
  298. /* We only accept a batch with messages for the same
  299. * subsystem.
  300. */
  301. if (NFNL_SUBSYS_ID(type) != subsys_id) {
  302. err = -EINVAL;
  303. goto ack;
  304. }
  305. nc = nfnetlink_find_client(type, ss);
  306. if (!nc) {
  307. err = -EINVAL;
  308. goto ack;
  309. }
  310. {
  311. int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
  312. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  313. struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
  314. struct nlattr *attr = (void *)nlh + min_len;
  315. int attrlen = nlh->nlmsg_len - min_len;
  316. err = nla_parse(cda, ss->cb[cb_id].attr_count,
  317. attr, attrlen, ss->cb[cb_id].policy);
  318. if (err < 0)
  319. goto ack;
  320. if (nc->call_batch) {
  321. err = nc->call_batch(net->nfnl, skb, nlh,
  322. (const struct nlattr **)cda);
  323. }
  324. /* The lock was released to autoload some module, we
  325. * have to abort and start from scratch using the
  326. * original skb.
  327. */
  328. if (err == -EAGAIN) {
  329. nfnl_err_reset(&err_list);
  330. ss->abort(oskb);
  331. nfnl_unlock(subsys_id);
  332. kfree_skb(skb);
  333. goto replay;
  334. }
  335. }
  336. ack:
  337. if (nlh->nlmsg_flags & NLM_F_ACK || err) {
  338. /* Errors are delivered once the full batch has been
  339. * processed, this avoids that the same error is
  340. * reported several times when replaying the batch.
  341. */
  342. if (nfnl_err_add(&err_list, nlh, err) < 0) {
  343. /* We failed to enqueue an error, reset the
  344. * list of errors and send OOM to userspace
  345. * pointing to the batch header.
  346. */
  347. nfnl_err_reset(&err_list);
  348. netlink_ack(skb, nlmsg_hdr(oskb), -ENOMEM);
  349. success = false;
  350. goto done;
  351. }
  352. /* We don't stop processing the batch on errors, thus,
  353. * userspace gets all the errors that the batch
  354. * triggers.
  355. */
  356. if (err)
  357. success = false;
  358. }
  359. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  360. if (msglen > skb->len)
  361. msglen = skb->len;
  362. skb_pull(skb, msglen);
  363. }
  364. done:
  365. if (success && done)
  366. ss->commit(oskb);
  367. else
  368. ss->abort(oskb);
  369. nfnl_err_deliver(&err_list, oskb);
  370. nfnl_unlock(subsys_id);
  371. kfree_skb(skb);
  372. }
  373. static void nfnetlink_rcv(struct sk_buff *skb)
  374. {
  375. struct nlmsghdr *nlh = nlmsg_hdr(skb);
  376. int msglen;
  377. if (nlh->nlmsg_len < NLMSG_HDRLEN ||
  378. skb->len < nlh->nlmsg_len)
  379. return;
  380. if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
  381. netlink_ack(skb, nlh, -EPERM);
  382. return;
  383. }
  384. if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) {
  385. struct nfgenmsg *nfgenmsg;
  386. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  387. if (msglen > skb->len)
  388. msglen = skb->len;
  389. if (nlh->nlmsg_len < NLMSG_HDRLEN ||
  390. skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
  391. return;
  392. nfgenmsg = nlmsg_data(nlh);
  393. skb_pull(skb, msglen);
  394. nfnetlink_rcv_batch(skb, nlh, nfgenmsg->res_id);
  395. } else {
  396. netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
  397. }
  398. }
  399. #ifdef CONFIG_MODULES
  400. static int nfnetlink_bind(struct net *net, int group)
  401. {
  402. const struct nfnetlink_subsystem *ss;
  403. int type;
  404. if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
  405. return 0;
  406. type = nfnl_group2type[group];
  407. rcu_read_lock();
  408. ss = nfnetlink_get_subsys(type);
  409. rcu_read_unlock();
  410. if (!ss)
  411. request_module("nfnetlink-subsys-%d", type);
  412. return 0;
  413. }
  414. #endif
  415. static int __net_init nfnetlink_net_init(struct net *net)
  416. {
  417. struct sock *nfnl;
  418. struct netlink_kernel_cfg cfg = {
  419. .groups = NFNLGRP_MAX,
  420. .input = nfnetlink_rcv,
  421. #ifdef CONFIG_MODULES
  422. .bind = nfnetlink_bind,
  423. #endif
  424. };
  425. nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
  426. if (!nfnl)
  427. return -ENOMEM;
  428. net->nfnl_stash = nfnl;
  429. rcu_assign_pointer(net->nfnl, nfnl);
  430. return 0;
  431. }
  432. static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
  433. {
  434. struct net *net;
  435. list_for_each_entry(net, net_exit_list, exit_list)
  436. RCU_INIT_POINTER(net->nfnl, NULL);
  437. synchronize_net();
  438. list_for_each_entry(net, net_exit_list, exit_list)
  439. netlink_kernel_release(net->nfnl_stash);
  440. }
  441. static struct pernet_operations nfnetlink_net_ops = {
  442. .init = nfnetlink_net_init,
  443. .exit_batch = nfnetlink_net_exit_batch,
  444. };
  445. static int __init nfnetlink_init(void)
  446. {
  447. int i;
  448. for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
  449. BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
  450. for (i=0; i<NFNL_SUBSYS_COUNT; i++)
  451. mutex_init(&table[i].mutex);
  452. pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
  453. return register_pernet_subsys(&nfnetlink_net_ops);
  454. }
  455. static void __exit nfnetlink_exit(void)
  456. {
  457. pr_info("Removing netfilter NETLINK layer.\n");
  458. unregister_pernet_subsys(&nfnetlink_net_ops);
  459. }
  460. module_init(nfnetlink_init);
  461. module_exit(nfnetlink_exit);