nfnetlink.c 5.6 KB

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