nfnetlink.c 13 KB

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