nfnetlink.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 struct sock *nfnl = NULL;
  39. static const struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
  40. static DEFINE_MUTEX(nfnl_mutex);
  41. static void nfnl_lock(void)
  42. {
  43. mutex_lock(&nfnl_mutex);
  44. }
  45. static int nfnl_trylock(void)
  46. {
  47. return !mutex_trylock(&nfnl_mutex);
  48. }
  49. static void __nfnl_unlock(void)
  50. {
  51. mutex_unlock(&nfnl_mutex);
  52. }
  53. static void nfnl_unlock(void)
  54. {
  55. mutex_unlock(&nfnl_mutex);
  56. if (nfnl->sk_receive_queue.qlen)
  57. nfnl->sk_data_ready(nfnl, 0);
  58. }
  59. int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
  60. {
  61. nfnl_lock();
  62. if (subsys_table[n->subsys_id]) {
  63. nfnl_unlock();
  64. return -EBUSY;
  65. }
  66. subsys_table[n->subsys_id] = n;
  67. nfnl_unlock();
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
  71. int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
  72. {
  73. nfnl_lock();
  74. subsys_table[n->subsys_id] = NULL;
  75. nfnl_unlock();
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  79. static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
  80. {
  81. u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
  82. if (subsys_id >= NFNL_SUBSYS_COUNT)
  83. return NULL;
  84. return subsys_table[subsys_id];
  85. }
  86. static inline const struct nfnl_callback *
  87. nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
  88. {
  89. u_int8_t cb_id = NFNL_MSG_TYPE(type);
  90. if (cb_id >= ss->cb_count)
  91. return NULL;
  92. return &ss->cb[cb_id];
  93. }
  94. int nfnetlink_has_listeners(unsigned int group)
  95. {
  96. return netlink_has_listeners(nfnl, group);
  97. }
  98. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  99. int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  100. {
  101. return nlmsg_notify(nfnl, skb, pid, group, echo, gfp_any());
  102. }
  103. EXPORT_SYMBOL_GPL(nfnetlink_send);
  104. int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
  105. {
  106. return netlink_unicast(nfnl, skb, pid, flags);
  107. }
  108. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  109. /* Process one complete nfnetlink message. */
  110. static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  111. {
  112. const struct nfnl_callback *nc;
  113. const struct nfnetlink_subsystem *ss;
  114. int type, err;
  115. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  116. return -EPERM;
  117. /* All the messages must at least contain nfgenmsg */
  118. if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
  119. return 0;
  120. type = nlh->nlmsg_type;
  121. ss = nfnetlink_get_subsys(type);
  122. if (!ss) {
  123. #ifdef CONFIG_KMOD
  124. /* don't call nfnl_unlock, since it would reenter
  125. * with further packet processing */
  126. __nfnl_unlock();
  127. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  128. nfnl_lock();
  129. ss = nfnetlink_get_subsys(type);
  130. if (!ss)
  131. #endif
  132. return -EINVAL;
  133. }
  134. nc = nfnetlink_find_client(type, ss);
  135. if (!nc)
  136. return -EINVAL;
  137. {
  138. int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
  139. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  140. u_int16_t attr_count = ss->cb[cb_id].attr_count;
  141. struct nlattr *cda[attr_count+1];
  142. if (likely(nlh->nlmsg_len >= min_len)) {
  143. struct nlattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
  144. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  145. err = nla_parse(cda, attr_count, attr, attrlen,
  146. ss->cb[cb_id].policy);
  147. if (err < 0)
  148. return err;
  149. } else
  150. return -EINVAL;
  151. return nc->call(nfnl, skb, nlh, cda);
  152. }
  153. }
  154. static void nfnetlink_rcv(struct sock *sk, int len)
  155. {
  156. unsigned int qlen = 0;
  157. do {
  158. if (nfnl_trylock())
  159. return;
  160. qlen = netlink_run_queue(sk, qlen, nfnetlink_rcv_msg);
  161. __nfnl_unlock();
  162. } while (qlen);
  163. }
  164. static void __exit nfnetlink_exit(void)
  165. {
  166. printk("Removing netfilter NETLINK layer.\n");
  167. sock_release(nfnl->sk_socket);
  168. return;
  169. }
  170. static int __init nfnetlink_init(void)
  171. {
  172. printk("Netfilter messages via NETLINK v%s.\n", nfversion);
  173. nfnl = netlink_kernel_create(&init_net, NETLINK_NETFILTER, NFNLGRP_MAX,
  174. nfnetlink_rcv, NULL, THIS_MODULE);
  175. if (!nfnl) {
  176. printk(KERN_ERR "cannot initialize nfnetlink!\n");
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. module_init(nfnetlink_init);
  182. module_exit(nfnetlink_exit);