nfnetlink.c 8.9 KB

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