nfnetlink.c 7.0 KB

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