nfnetlink.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void nfnl_lock(void)
  42. {
  43. mutex_lock(&nfnl_mutex);
  44. }
  45. EXPORT_SYMBOL_GPL(nfnl_lock);
  46. void nfnl_unlock(void)
  47. {
  48. mutex_unlock(&nfnl_mutex);
  49. }
  50. EXPORT_SYMBOL_GPL(nfnl_unlock);
  51. int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
  52. {
  53. nfnl_lock();
  54. if (subsys_table[n->subsys_id]) {
  55. nfnl_unlock();
  56. return -EBUSY;
  57. }
  58. subsys_table[n->subsys_id] = n;
  59. nfnl_unlock();
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
  63. int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
  64. {
  65. nfnl_lock();
  66. subsys_table[n->subsys_id] = NULL;
  67. nfnl_unlock();
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  71. static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
  72. {
  73. u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
  74. if (subsys_id >= NFNL_SUBSYS_COUNT)
  75. return NULL;
  76. return subsys_table[subsys_id];
  77. }
  78. static inline const struct nfnl_callback *
  79. nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
  80. {
  81. u_int8_t cb_id = NFNL_MSG_TYPE(type);
  82. if (cb_id >= ss->cb_count)
  83. return NULL;
  84. return &ss->cb[cb_id];
  85. }
  86. int nfnetlink_has_listeners(unsigned int group)
  87. {
  88. return netlink_has_listeners(nfnl, group);
  89. }
  90. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  91. int nfnetlink_send(struct sk_buff *skb, u32 pid,
  92. unsigned group, int echo, gfp_t flags)
  93. {
  94. return nlmsg_notify(nfnl, skb, pid, group, echo, flags);
  95. }
  96. EXPORT_SYMBOL_GPL(nfnetlink_send);
  97. void nfnetlink_set_err(u32 pid, u32 group, int error)
  98. {
  99. netlink_set_err(nfnl, pid, group, error);
  100. }
  101. EXPORT_SYMBOL_GPL(nfnetlink_set_err);
  102. int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
  103. {
  104. return netlink_unicast(nfnl, skb, pid, flags);
  105. }
  106. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  107. /* Process one complete nfnetlink message. */
  108. static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  109. {
  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(nfnl, skb, nlh, 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 void __exit nfnetlink_exit(void)
  157. {
  158. printk("Removing netfilter NETLINK layer.\n");
  159. netlink_kernel_release(nfnl);
  160. return;
  161. }
  162. static int __init nfnetlink_init(void)
  163. {
  164. printk("Netfilter messages via NETLINK v%s.\n", nfversion);
  165. nfnl = netlink_kernel_create(&init_net, NETLINK_NETFILTER, NFNLGRP_MAX,
  166. nfnetlink_rcv, NULL, THIS_MODULE);
  167. if (!nfnl) {
  168. printk(KERN_ERR "cannot initialize nfnetlink!\n");
  169. return -ENOMEM;
  170. }
  171. return 0;
  172. }
  173. module_init(nfnetlink_init);
  174. module_exit(nfnetlink_exit);