nfnetlink.c 13 KB

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