nfnetlink.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <asm/system.h>
  26. #include <net/sock.h>
  27. #include <net/netlink.h>
  28. #include <linux/init.h>
  29. #include <linux/netlink.h>
  30. #include <linux/netfilter/nfnetlink.h>
  31. MODULE_LICENSE("GPL");
  32. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  33. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
  34. static char __initdata nfversion[] = "0.30";
  35. static const struct nfnetlink_subsystem __rcu *subsys_table[NFNL_SUBSYS_COUNT];
  36. static DEFINE_MUTEX(nfnl_mutex);
  37. void nfnl_lock(void)
  38. {
  39. mutex_lock(&nfnl_mutex);
  40. }
  41. EXPORT_SYMBOL_GPL(nfnl_lock);
  42. void nfnl_unlock(void)
  43. {
  44. mutex_unlock(&nfnl_mutex);
  45. }
  46. EXPORT_SYMBOL_GPL(nfnl_unlock);
  47. int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
  48. {
  49. nfnl_lock();
  50. if (subsys_table[n->subsys_id]) {
  51. nfnl_unlock();
  52. return -EBUSY;
  53. }
  54. rcu_assign_pointer(subsys_table[n->subsys_id], n);
  55. nfnl_unlock();
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
  59. int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
  60. {
  61. nfnl_lock();
  62. subsys_table[n->subsys_id] = NULL;
  63. nfnl_unlock();
  64. synchronize_rcu();
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  68. static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
  69. {
  70. u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
  71. if (subsys_id >= NFNL_SUBSYS_COUNT)
  72. return NULL;
  73. return rcu_dereference(subsys_table[subsys_id]);
  74. }
  75. static inline const struct nfnl_callback *
  76. nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
  77. {
  78. u_int8_t cb_id = NFNL_MSG_TYPE(type);
  79. if (cb_id >= ss->cb_count)
  80. return NULL;
  81. return &ss->cb[cb_id];
  82. }
  83. int nfnetlink_has_listeners(struct net *net, unsigned int group)
  84. {
  85. return netlink_has_listeners(net->nfnl, group);
  86. }
  87. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  88. int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 pid,
  89. unsigned group, int echo, gfp_t flags)
  90. {
  91. return nlmsg_notify(net->nfnl, skb, pid, group, echo, flags);
  92. }
  93. EXPORT_SYMBOL_GPL(nfnetlink_send);
  94. int nfnetlink_set_err(struct net *net, u32 pid, u32 group, int error)
  95. {
  96. return netlink_set_err(net->nfnl, pid, group, error);
  97. }
  98. EXPORT_SYMBOL_GPL(nfnetlink_set_err);
  99. int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u_int32_t pid, int flags)
  100. {
  101. return netlink_unicast(net->nfnl, skb, pid, flags);
  102. }
  103. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  104. /* Process one complete nfnetlink message. */
  105. static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  106. {
  107. struct net *net = sock_net(skb->sk);
  108. const struct nfnl_callback *nc;
  109. const struct nfnetlink_subsystem *ss;
  110. int type, err;
  111. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  112. return -EPERM;
  113. /* All the messages must at least contain nfgenmsg */
  114. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nfgenmsg)))
  115. return 0;
  116. type = nlh->nlmsg_type;
  117. replay:
  118. rcu_read_lock();
  119. ss = nfnetlink_get_subsys(type);
  120. if (!ss) {
  121. #ifdef CONFIG_MODULES
  122. rcu_read_unlock();
  123. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  124. rcu_read_lock();
  125. ss = nfnetlink_get_subsys(type);
  126. if (!ss)
  127. #endif
  128. {
  129. rcu_read_unlock();
  130. return -EINVAL;
  131. }
  132. }
  133. nc = nfnetlink_find_client(type, ss);
  134. if (!nc) {
  135. rcu_read_unlock();
  136. return -EINVAL;
  137. }
  138. {
  139. int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
  140. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  141. struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
  142. struct nlattr *attr = (void *)nlh + min_len;
  143. int attrlen = nlh->nlmsg_len - min_len;
  144. err = nla_parse(cda, ss->cb[cb_id].attr_count,
  145. attr, attrlen, ss->cb[cb_id].policy);
  146. if (err < 0)
  147. return err;
  148. if (nc->call_rcu) {
  149. err = nc->call_rcu(net->nfnl, skb, nlh,
  150. (const struct nlattr **)cda);
  151. rcu_read_unlock();
  152. } else {
  153. rcu_read_unlock();
  154. nfnl_lock();
  155. if (rcu_dereference_protected(
  156. subsys_table[NFNL_SUBSYS_ID(type)],
  157. lockdep_is_held(&nfnl_mutex)) != ss ||
  158. nfnetlink_find_client(type, ss) != nc)
  159. err = -EAGAIN;
  160. else
  161. err = nc->call(net->nfnl, skb, nlh,
  162. (const struct nlattr **)cda);
  163. nfnl_unlock();
  164. }
  165. if (err == -EAGAIN)
  166. goto replay;
  167. return err;
  168. }
  169. }
  170. static void nfnetlink_rcv(struct sk_buff *skb)
  171. {
  172. netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
  173. }
  174. static int __net_init nfnetlink_net_init(struct net *net)
  175. {
  176. struct sock *nfnl;
  177. nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, NFNLGRP_MAX,
  178. nfnetlink_rcv, NULL, THIS_MODULE);
  179. if (!nfnl)
  180. return -ENOMEM;
  181. net->nfnl_stash = nfnl;
  182. rcu_assign_pointer(net->nfnl, nfnl);
  183. return 0;
  184. }
  185. static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
  186. {
  187. struct net *net;
  188. list_for_each_entry(net, net_exit_list, exit_list)
  189. rcu_assign_pointer(net->nfnl, NULL);
  190. synchronize_net();
  191. list_for_each_entry(net, net_exit_list, exit_list)
  192. netlink_kernel_release(net->nfnl_stash);
  193. }
  194. static struct pernet_operations nfnetlink_net_ops = {
  195. .init = nfnetlink_net_init,
  196. .exit_batch = nfnetlink_net_exit_batch,
  197. };
  198. static int __init nfnetlink_init(void)
  199. {
  200. pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
  201. return register_pernet_subsys(&nfnetlink_net_ops);
  202. }
  203. static void __exit nfnetlink_exit(void)
  204. {
  205. pr_info("Removing netfilter NETLINK layer.\n");
  206. unregister_pernet_subsys(&nfnetlink_net_ops);
  207. }
  208. module_init(nfnetlink_init);
  209. module_exit(nfnetlink_exit);