nfnetlink.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_int16_t attr_count;
  131. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  132. attr_count = subsys->cb[cb_id].attr_count;
  133. memset(cda, 0, sizeof(struct nfattr *) * attr_count);
  134. /* check attribute lengths. */
  135. if (likely(nlh->nlmsg_len > min_len)) {
  136. struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
  137. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  138. while (NFA_OK(attr, attrlen)) {
  139. unsigned flavor = NFA_TYPE(attr);
  140. if (flavor) {
  141. if (flavor > attr_count)
  142. return -EINVAL;
  143. cda[flavor - 1] = attr;
  144. }
  145. attr = NFA_NEXT(attr, attrlen);
  146. }
  147. }
  148. /* implicit: if nlmsg_len == min_len, we return 0, and an empty
  149. * (zeroed) cda[] array. The message is valid, but empty. */
  150. return 0;
  151. }
  152. int nfnetlink_has_listeners(unsigned int group)
  153. {
  154. return netlink_has_listeners(nfnl, group);
  155. }
  156. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  157. int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  158. {
  159. int err = 0;
  160. NETLINK_CB(skb).dst_group = group;
  161. if (echo)
  162. atomic_inc(&skb->users);
  163. netlink_broadcast(nfnl, skb, pid, group, gfp_any());
  164. if (echo)
  165. err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
  166. return err;
  167. }
  168. EXPORT_SYMBOL_GPL(nfnetlink_send);
  169. int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
  170. {
  171. return netlink_unicast(nfnl, skb, pid, flags);
  172. }
  173. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  174. /* Process one complete nfnetlink message. */
  175. static int nfnetlink_rcv_msg(struct sk_buff *skb,
  176. struct nlmsghdr *nlh, int *errp)
  177. {
  178. struct nfnl_callback *nc;
  179. struct nfnetlink_subsystem *ss;
  180. int type, err = 0;
  181. if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
  182. *errp = -EPERM;
  183. return -1;
  184. }
  185. /* Only requests are handled by kernel now. */
  186. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  187. return 0;
  188. /* All the messages must at least contain nfgenmsg */
  189. if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
  190. return 0;
  191. type = nlh->nlmsg_type;
  192. ss = nfnetlink_get_subsys(type);
  193. if (!ss) {
  194. #ifdef CONFIG_KMOD
  195. /* don't call nfnl_unlock, since it would reenter
  196. * with further packet processing */
  197. __nfnl_unlock();
  198. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  199. nfnl_lock();
  200. ss = nfnetlink_get_subsys(type);
  201. if (!ss)
  202. #endif
  203. goto err_inval;
  204. }
  205. nc = nfnetlink_find_client(type, ss);
  206. if (!nc)
  207. goto err_inval;
  208. {
  209. u_int16_t attr_count =
  210. ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
  211. struct nfattr *cda[attr_count];
  212. memset(cda, 0, sizeof(struct nfattr *) * attr_count);
  213. err = nfnetlink_check_attributes(ss, nlh, cda);
  214. if (err < 0)
  215. goto err_inval;
  216. err = nc->call(nfnl, skb, nlh, cda, errp);
  217. *errp = err;
  218. return err;
  219. }
  220. err_inval:
  221. *errp = -EINVAL;
  222. return -1;
  223. }
  224. static void nfnetlink_rcv(struct sock *sk, int len)
  225. {
  226. unsigned int qlen = 0;
  227. do {
  228. if (nfnl_trylock())
  229. return;
  230. netlink_run_queue(sk, &qlen, nfnetlink_rcv_msg);
  231. __nfnl_unlock();
  232. } while (qlen);
  233. }
  234. static void __exit nfnetlink_exit(void)
  235. {
  236. printk("Removing netfilter NETLINK layer.\n");
  237. sock_release(nfnl->sk_socket);
  238. return;
  239. }
  240. static int __init nfnetlink_init(void)
  241. {
  242. printk("Netfilter messages via NETLINK v%s.\n", nfversion);
  243. nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
  244. nfnetlink_rcv, THIS_MODULE);
  245. if (!nfnl) {
  246. printk(KERN_ERR "cannot initialize nfnetlink!\n");
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. module_init(nfnetlink_init);
  252. module_exit(nfnetlink_exit);