nfnetlink.c 4.8 KB

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