nfnetlink.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 *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. 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. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  67. static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
  68. {
  69. u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
  70. if (subsys_id >= NFNL_SUBSYS_COUNT)
  71. return NULL;
  72. return subsys_table[subsys_id];
  73. }
  74. static inline const struct nfnl_callback *
  75. nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
  76. {
  77. u_int8_t cb_id = NFNL_MSG_TYPE(type);
  78. if (cb_id >= ss->cb_count)
  79. return NULL;
  80. return &ss->cb[cb_id];
  81. }
  82. int nfnetlink_has_listeners(struct net *net, unsigned int group)
  83. {
  84. return netlink_has_listeners(net->nfnl, group);
  85. }
  86. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  87. int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 pid,
  88. unsigned group, int echo, gfp_t flags)
  89. {
  90. return nlmsg_notify(net->nfnl, skb, pid, group, echo, flags);
  91. }
  92. EXPORT_SYMBOL_GPL(nfnetlink_send);
  93. int nfnetlink_set_err(struct net *net, u32 pid, u32 group, int error)
  94. {
  95. return netlink_set_err(net->nfnl, pid, group, error);
  96. }
  97. EXPORT_SYMBOL_GPL(nfnetlink_set_err);
  98. int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u_int32_t pid, int flags)
  99. {
  100. return netlink_unicast(net->nfnl, skb, pid, flags);
  101. }
  102. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  103. /* Process one complete nfnetlink message. */
  104. static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  105. {
  106. struct net *net = sock_net(skb->sk);
  107. const struct nfnl_callback *nc;
  108. const struct nfnetlink_subsystem *ss;
  109. int type, err;
  110. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  111. return -EPERM;
  112. /* All the messages must at least contain nfgenmsg */
  113. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nfgenmsg)))
  114. return 0;
  115. type = nlh->nlmsg_type;
  116. replay:
  117. ss = nfnetlink_get_subsys(type);
  118. if (!ss) {
  119. #ifdef CONFIG_MODULES
  120. nfnl_unlock();
  121. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  122. nfnl_lock();
  123. ss = nfnetlink_get_subsys(type);
  124. if (!ss)
  125. #endif
  126. return -EINVAL;
  127. }
  128. nc = nfnetlink_find_client(type, ss);
  129. if (!nc)
  130. return -EINVAL;
  131. {
  132. int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
  133. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  134. struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
  135. struct nlattr *attr = (void *)nlh + min_len;
  136. int attrlen = nlh->nlmsg_len - min_len;
  137. err = nla_parse(cda, ss->cb[cb_id].attr_count,
  138. attr, attrlen, ss->cb[cb_id].policy);
  139. if (err < 0)
  140. return err;
  141. err = nc->call(net->nfnl, skb, nlh, (const struct nlattr **)cda);
  142. if (err == -EAGAIN)
  143. goto replay;
  144. return err;
  145. }
  146. }
  147. static void nfnetlink_rcv(struct sk_buff *skb)
  148. {
  149. nfnl_lock();
  150. netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
  151. nfnl_unlock();
  152. }
  153. static int __net_init nfnetlink_net_init(struct net *net)
  154. {
  155. struct sock *nfnl;
  156. nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, NFNLGRP_MAX,
  157. nfnetlink_rcv, NULL, THIS_MODULE);
  158. if (!nfnl)
  159. return -ENOMEM;
  160. net->nfnl_stash = nfnl;
  161. rcu_assign_pointer(net->nfnl, nfnl);
  162. return 0;
  163. }
  164. static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
  165. {
  166. struct net *net;
  167. list_for_each_entry(net, net_exit_list, exit_list)
  168. rcu_assign_pointer(net->nfnl, NULL);
  169. synchronize_net();
  170. list_for_each_entry(net, net_exit_list, exit_list)
  171. netlink_kernel_release(net->nfnl_stash);
  172. }
  173. static struct pernet_operations nfnetlink_net_ops = {
  174. .init = nfnetlink_net_init,
  175. .exit_batch = nfnetlink_net_exit_batch,
  176. };
  177. static int __init nfnetlink_init(void)
  178. {
  179. pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
  180. return register_pernet_subsys(&nfnetlink_net_ops);
  181. }
  182. static void __exit nfnetlink_exit(void)
  183. {
  184. pr_info("Removing netfilter NETLINK layer.\n");
  185. unregister_pernet_subsys(&nfnetlink_net_ops);
  186. }
  187. module_init(nfnetlink_init);
  188. module_exit(nfnetlink_exit);