nfnetlink.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 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(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(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 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 struct nfnl_callback *
  87. nfnetlink_find_client(u_int16_t type, 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. void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
  95. const void *data)
  96. {
  97. struct nfattr *nfa;
  98. int size = NFA_LENGTH(attrlen);
  99. nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
  100. nfa->nfa_type = attrtype;
  101. nfa->nfa_len = size;
  102. memcpy(NFA_DATA(nfa), data, attrlen);
  103. memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
  104. }
  105. EXPORT_SYMBOL_GPL(__nfa_fill);
  106. void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
  107. {
  108. memset(tb, 0, sizeof(struct nfattr *) * maxattr);
  109. while (NFA_OK(nfa, len)) {
  110. unsigned flavor = NFA_TYPE(nfa);
  111. if (flavor && flavor <= maxattr)
  112. tb[flavor-1] = nfa;
  113. nfa = NFA_NEXT(nfa, len);
  114. }
  115. }
  116. EXPORT_SYMBOL_GPL(nfattr_parse);
  117. /**
  118. * nfnetlink_check_attributes - check and parse nfnetlink attributes
  119. *
  120. * subsys: nfnl subsystem for which this message is to be parsed
  121. * nlmsghdr: netlink message to be checked/parsed
  122. * cda: array of pointers, needs to be at least subsys->attr_count big
  123. *
  124. */
  125. static int
  126. nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
  127. struct nlmsghdr *nlh, struct nfattr *cda[])
  128. {
  129. int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
  130. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  131. u_int16_t attr_count = subsys->cb[cb_id].attr_count;
  132. /* check attribute lengths. */
  133. if (likely(nlh->nlmsg_len > min_len)) {
  134. struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
  135. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  136. nfattr_parse(cda, attr_count, attr, attrlen);
  137. }
  138. /* implicit: if nlmsg_len == min_len, we return 0, and an empty
  139. * (zeroed) cda[] array. The message is valid, but empty. */
  140. return 0;
  141. }
  142. int nfnetlink_has_listeners(unsigned int group)
  143. {
  144. return netlink_has_listeners(nfnl, group);
  145. }
  146. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  147. int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  148. {
  149. int err = 0;
  150. NETLINK_CB(skb).dst_group = group;
  151. if (echo)
  152. atomic_inc(&skb->users);
  153. netlink_broadcast(nfnl, skb, pid, group, gfp_any());
  154. if (echo)
  155. err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
  156. return err;
  157. }
  158. EXPORT_SYMBOL_GPL(nfnetlink_send);
  159. int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
  160. {
  161. return netlink_unicast(nfnl, skb, pid, flags);
  162. }
  163. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  164. /* Process one complete nfnetlink message. */
  165. static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  166. {
  167. struct nfnl_callback *nc;
  168. struct nfnetlink_subsystem *ss;
  169. int type, err;
  170. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  171. return -EPERM;
  172. /* All the messages must at least contain nfgenmsg */
  173. if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
  174. return 0;
  175. type = nlh->nlmsg_type;
  176. ss = nfnetlink_get_subsys(type);
  177. if (!ss) {
  178. #ifdef CONFIG_KMOD
  179. /* don't call nfnl_unlock, since it would reenter
  180. * with further packet processing */
  181. __nfnl_unlock();
  182. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  183. nfnl_lock();
  184. ss = nfnetlink_get_subsys(type);
  185. if (!ss)
  186. #endif
  187. return -EINVAL;
  188. }
  189. nc = nfnetlink_find_client(type, ss);
  190. if (!nc)
  191. return -EINVAL;
  192. {
  193. u_int16_t attr_count =
  194. ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
  195. struct nfattr *cda[attr_count];
  196. memset(cda, 0, sizeof(struct nfattr *) * attr_count);
  197. err = nfnetlink_check_attributes(ss, nlh, cda);
  198. if (err < 0)
  199. return err;
  200. return nc->call(nfnl, skb, nlh, cda);
  201. }
  202. }
  203. static void nfnetlink_rcv(struct sock *sk, int len)
  204. {
  205. unsigned int qlen = 0;
  206. do {
  207. if (nfnl_trylock())
  208. return;
  209. netlink_run_queue(sk, &qlen, nfnetlink_rcv_msg);
  210. __nfnl_unlock();
  211. } while (qlen);
  212. }
  213. static void __exit nfnetlink_exit(void)
  214. {
  215. printk("Removing netfilter NETLINK layer.\n");
  216. sock_release(nfnl->sk_socket);
  217. return;
  218. }
  219. static int __init nfnetlink_init(void)
  220. {
  221. printk("Netfilter messages via NETLINK v%s.\n", nfversion);
  222. nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
  223. nfnetlink_rcv, NULL, THIS_MODULE);
  224. if (!nfnl) {
  225. printk(KERN_ERR "cannot initialize nfnetlink!\n");
  226. return -1;
  227. }
  228. return 0;
  229. }
  230. module_init(nfnetlink_init);
  231. module_exit(nfnetlink_exit);