nfnetlink.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 by Pablo Neira Ayuso <pablo@eurodev.net>
  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/spinlock.h>
  33. #include <linux/netfilter.h>
  34. #include <linux/netlink.h>
  35. #include <linux/netfilter/nfnetlink.h>
  36. MODULE_LICENSE("GPL");
  37. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  38. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
  39. static char __initdata nfversion[] = "0.30";
  40. #if 0
  41. #define DEBUGP(format, args...) \
  42. printk(KERN_DEBUG "%s(%d):%s(): " format, __FILE__, \
  43. __LINE__, __FUNCTION__, ## args)
  44. #else
  45. #define DEBUGP(format, args...)
  46. #endif
  47. static struct sock *nfnl = NULL;
  48. static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
  49. static DEFINE_MUTEX(nfnl_mutex);
  50. static void nfnl_lock(void)
  51. {
  52. mutex_lock(&nfnl_mutex);
  53. }
  54. static int nfnl_trylock(void)
  55. {
  56. return !mutex_trylock(&nfnl_mutex);
  57. }
  58. static void __nfnl_unlock(void)
  59. {
  60. mutex_unlock(&nfnl_mutex);
  61. }
  62. static void nfnl_unlock(void)
  63. {
  64. mutex_unlock(&nfnl_mutex);
  65. if (nfnl->sk_receive_queue.qlen)
  66. nfnl->sk_data_ready(nfnl, 0);
  67. }
  68. int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
  69. {
  70. DEBUGP("registering subsystem ID %u\n", n->subsys_id);
  71. nfnl_lock();
  72. if (subsys_table[n->subsys_id]) {
  73. nfnl_unlock();
  74. return -EBUSY;
  75. }
  76. subsys_table[n->subsys_id] = n;
  77. nfnl_unlock();
  78. return 0;
  79. }
  80. int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
  81. {
  82. DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
  83. nfnl_lock();
  84. subsys_table[n->subsys_id] = NULL;
  85. nfnl_unlock();
  86. return 0;
  87. }
  88. static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
  89. {
  90. u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
  91. if (subsys_id >= NFNL_SUBSYS_COUNT
  92. || subsys_table[subsys_id] == NULL)
  93. return NULL;
  94. return subsys_table[subsys_id];
  95. }
  96. static inline struct nfnl_callback *
  97. nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
  98. {
  99. u_int8_t cb_id = NFNL_MSG_TYPE(type);
  100. if (cb_id >= ss->cb_count) {
  101. DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
  102. return NULL;
  103. }
  104. return &ss->cb[cb_id];
  105. }
  106. void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
  107. const void *data)
  108. {
  109. struct nfattr *nfa;
  110. int size = NFA_LENGTH(attrlen);
  111. nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
  112. nfa->nfa_type = attrtype;
  113. nfa->nfa_len = size;
  114. memcpy(NFA_DATA(nfa), data, attrlen);
  115. memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
  116. }
  117. void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
  118. {
  119. memset(tb, 0, sizeof(struct nfattr *) * maxattr);
  120. while (NFA_OK(nfa, len)) {
  121. unsigned flavor = NFA_TYPE(nfa);
  122. if (flavor && flavor <= maxattr)
  123. tb[flavor-1] = nfa;
  124. nfa = NFA_NEXT(nfa, len);
  125. }
  126. }
  127. /**
  128. * nfnetlink_check_attributes - check and parse nfnetlink attributes
  129. *
  130. * subsys: nfnl subsystem for which this message is to be parsed
  131. * nlmsghdr: netlink message to be checked/parsed
  132. * cda: array of pointers, needs to be at least subsys->attr_count big
  133. *
  134. */
  135. static int
  136. nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
  137. struct nlmsghdr *nlh, struct nfattr *cda[])
  138. {
  139. int min_len;
  140. u_int16_t attr_count;
  141. u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
  142. if (unlikely(cb_id >= subsys->cb_count)) {
  143. DEBUGP("msgtype %u >= %u, returning\n",
  144. cb_id, subsys->cb_count);
  145. return -EINVAL;
  146. }
  147. min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
  148. if (unlikely(nlh->nlmsg_len < min_len))
  149. return -EINVAL;
  150. attr_count = subsys->cb[cb_id].attr_count;
  151. memset(cda, 0, sizeof(struct nfattr *) * attr_count);
  152. /* check attribute lengths. */
  153. if (likely(nlh->nlmsg_len > min_len)) {
  154. struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
  155. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  156. while (NFA_OK(attr, attrlen)) {
  157. unsigned flavor = NFA_TYPE(attr);
  158. if (flavor) {
  159. if (flavor > attr_count)
  160. return -EINVAL;
  161. cda[flavor - 1] = attr;
  162. }
  163. attr = NFA_NEXT(attr, attrlen);
  164. }
  165. }
  166. /* implicit: if nlmsg_len == min_len, we return 0, and an empty
  167. * (zeroed) cda[] array. The message is valid, but empty. */
  168. return 0;
  169. }
  170. int nfnetlink_has_listeners(unsigned int group)
  171. {
  172. return netlink_has_listeners(nfnl, group);
  173. }
  174. EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
  175. int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  176. {
  177. int err = 0;
  178. NETLINK_CB(skb).dst_group = group;
  179. if (echo)
  180. atomic_inc(&skb->users);
  181. netlink_broadcast(nfnl, skb, pid, group, gfp_any());
  182. if (echo)
  183. err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
  184. return err;
  185. }
  186. int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
  187. {
  188. return netlink_unicast(nfnl, skb, pid, flags);
  189. }
  190. /* Process one complete nfnetlink message. */
  191. static int nfnetlink_rcv_msg(struct sk_buff *skb,
  192. struct nlmsghdr *nlh, int *errp)
  193. {
  194. struct nfnl_callback *nc;
  195. struct nfnetlink_subsystem *ss;
  196. int type, err = 0;
  197. DEBUGP("entered; subsys=%u, msgtype=%u\n",
  198. NFNL_SUBSYS_ID(nlh->nlmsg_type),
  199. NFNL_MSG_TYPE(nlh->nlmsg_type));
  200. if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
  201. DEBUGP("missing CAP_NET_ADMIN\n");
  202. *errp = -EPERM;
  203. return -1;
  204. }
  205. /* Only requests are handled by kernel now. */
  206. if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
  207. DEBUGP("received non-request message\n");
  208. return 0;
  209. }
  210. /* All the messages must at least contain nfgenmsg */
  211. if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg))) {
  212. DEBUGP("received message was too short\n");
  213. return 0;
  214. }
  215. type = nlh->nlmsg_type;
  216. ss = nfnetlink_get_subsys(type);
  217. if (!ss) {
  218. #ifdef CONFIG_KMOD
  219. /* don't call nfnl_unlock, since it would reenter
  220. * with further packet processing */
  221. __nfnl_unlock();
  222. request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
  223. nfnl_lock();
  224. ss = nfnetlink_get_subsys(type);
  225. if (!ss)
  226. #endif
  227. goto err_inval;
  228. }
  229. nc = nfnetlink_find_client(type, ss);
  230. if (!nc) {
  231. DEBUGP("unable to find client for type %d\n", type);
  232. goto err_inval;
  233. }
  234. {
  235. u_int16_t attr_count =
  236. ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
  237. struct nfattr *cda[attr_count];
  238. memset(cda, 0, sizeof(struct nfattr *) * attr_count);
  239. err = nfnetlink_check_attributes(ss, nlh, cda);
  240. if (err < 0)
  241. goto err_inval;
  242. DEBUGP("calling handler\n");
  243. err = nc->call(nfnl, skb, nlh, cda, errp);
  244. *errp = err;
  245. return err;
  246. }
  247. err_inval:
  248. DEBUGP("returning -EINVAL\n");
  249. *errp = -EINVAL;
  250. return -1;
  251. }
  252. /* Process one packet of messages. */
  253. static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
  254. {
  255. int err;
  256. struct nlmsghdr *nlh;
  257. while (skb->len >= NLMSG_SPACE(0)) {
  258. u32 rlen;
  259. nlh = (struct nlmsghdr *)skb->data;
  260. if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
  261. || skb->len < nlh->nlmsg_len)
  262. return 0;
  263. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  264. if (rlen > skb->len)
  265. rlen = skb->len;
  266. if (nfnetlink_rcv_msg(skb, nlh, &err)) {
  267. if (!err)
  268. return -1;
  269. netlink_ack(skb, nlh, err);
  270. } else
  271. if (nlh->nlmsg_flags & NLM_F_ACK)
  272. netlink_ack(skb, nlh, 0);
  273. skb_pull(skb, rlen);
  274. }
  275. return 0;
  276. }
  277. static void nfnetlink_rcv(struct sock *sk, int len)
  278. {
  279. do {
  280. struct sk_buff *skb;
  281. if (nfnl_trylock())
  282. return;
  283. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  284. if (nfnetlink_rcv_skb(skb)) {
  285. if (skb->len)
  286. skb_queue_head(&sk->sk_receive_queue,
  287. skb);
  288. else
  289. kfree_skb(skb);
  290. break;
  291. }
  292. kfree_skb(skb);
  293. }
  294. /* don't call nfnl_unlock, since it would reenter
  295. * with further packet processing */
  296. __nfnl_unlock();
  297. } while(nfnl && nfnl->sk_receive_queue.qlen);
  298. }
  299. static void __exit nfnetlink_exit(void)
  300. {
  301. printk("Removing netfilter NETLINK layer.\n");
  302. sock_release(nfnl->sk_socket);
  303. return;
  304. }
  305. static int __init nfnetlink_init(void)
  306. {
  307. printk("Netfilter messages via NETLINK v%s.\n", nfversion);
  308. nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
  309. nfnetlink_rcv, THIS_MODULE);
  310. if (!nfnl) {
  311. printk(KERN_ERR "cannot initialize nfnetlink!\n");
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. module_init(nfnetlink_init);
  317. module_exit(nfnetlink_exit);
  318. EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
  319. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  320. EXPORT_SYMBOL_GPL(nfnetlink_send);
  321. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  322. EXPORT_SYMBOL_GPL(nfattr_parse);
  323. EXPORT_SYMBOL_GPL(__nfa_fill);