nfnetlink.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. static char __initdata nfversion[] = "0.30";
  39. #if 0
  40. #define DEBUGP printk
  41. #else
  42. #define DEBUGP(format, args...)
  43. #endif
  44. static struct sock *nfnl = NULL;
  45. static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
  46. DECLARE_MUTEX(nfnl_sem);
  47. void nfnl_lock(void)
  48. {
  49. nfnl_shlock();
  50. }
  51. void nfnl_unlock(void)
  52. {
  53. nfnl_shunlock();
  54. }
  55. int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
  56. {
  57. DEBUGP("registering subsystem ID %u\n", n->subsys_id);
  58. /* If the netlink socket wasn't created, then fail */
  59. if (!nfnl)
  60. return -1;
  61. nfnl_lock();
  62. subsys_table[n->subsys_id] = n;
  63. nfnl_unlock();
  64. return 0;
  65. }
  66. int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
  67. {
  68. DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
  69. nfnl_lock();
  70. subsys_table[n->subsys_id] = NULL;
  71. nfnl_unlock();
  72. return 0;
  73. }
  74. static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
  75. {
  76. u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
  77. if (subsys_id >= NFNL_SUBSYS_COUNT
  78. || subsys_table[subsys_id] == NULL)
  79. return NULL;
  80. return subsys_table[subsys_id];
  81. }
  82. static inline struct nfnl_callback *
  83. nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
  84. {
  85. u_int8_t cb_id = NFNL_MSG_TYPE(type);
  86. if (cb_id >= ss->cb_count) {
  87. DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
  88. return NULL;
  89. }
  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. int 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->nfa_type;
  108. if (flavor && flavor <= maxattr)
  109. tb[flavor-1] = nfa;
  110. nfa = NFA_NEXT(nfa, len);
  111. }
  112. return 0;
  113. }
  114. /**
  115. * nfnetlink_check_attributes - check and parse nfnetlink attributes
  116. *
  117. * subsys: nfnl subsystem for which this message is to be parsed
  118. * nlmsghdr: netlink message to be checked/parsed
  119. * cda: array of pointers, needs to be at least subsys->attr_count big
  120. *
  121. */
  122. static int
  123. nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
  124. struct nlmsghdr *nlh, struct nfattr *cda[])
  125. {
  126. int min_len;
  127. memset(cda, 0, sizeof(struct nfattr *) * subsys->attr_count);
  128. /* check attribute lengths. */
  129. min_len = NLMSG_ALIGN(sizeof(struct nfgenmsg));
  130. if (nlh->nlmsg_len < min_len)
  131. return -EINVAL;
  132. if (nlh->nlmsg_len > min_len) {
  133. struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
  134. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  135. while (NFA_OK(attr, attrlen)) {
  136. unsigned flavor = attr->nfa_type;
  137. if (flavor) {
  138. if (flavor > subsys->attr_count)
  139. return -EINVAL;
  140. cda[flavor - 1] = attr;
  141. }
  142. attr = NFA_NEXT(attr, attrlen);
  143. }
  144. } else
  145. return -EINVAL;
  146. return 0;
  147. }
  148. int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  149. {
  150. int allocation = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
  151. int err = 0;
  152. NETLINK_CB(skb).dst_groups = group;
  153. if (echo)
  154. atomic_inc(&skb->users);
  155. netlink_broadcast(nfnl, skb, pid, group, allocation);
  156. if (echo)
  157. err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
  158. return err;
  159. }
  160. int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
  161. {
  162. return netlink_unicast(nfnl, skb, pid, flags);
  163. }
  164. /* Process one complete nfnetlink message. */
  165. static inline int nfnetlink_rcv_msg(struct sk_buff *skb,
  166. struct nlmsghdr *nlh, int *errp)
  167. {
  168. struct nfnl_callback *nc;
  169. struct nfnetlink_subsystem *ss;
  170. int type, err = 0;
  171. DEBUGP("entered; subsys=%u, msgtype=%u\n",
  172. NFNL_SUBSYS_ID(nlh->nlmsg_type),
  173. NFNL_MSG_TYPE(nlh->nlmsg_type));
  174. /* Only requests are handled by kernel now. */
  175. if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
  176. DEBUGP("received non-request message\n");
  177. return 0;
  178. }
  179. /* All the messages must at least contain nfgenmsg */
  180. if (nlh->nlmsg_len <
  181. NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg)))) {
  182. DEBUGP("received message was too short\n");
  183. return 0;
  184. }
  185. type = nlh->nlmsg_type;
  186. ss = nfnetlink_get_subsys(type);
  187. if (!ss)
  188. goto err_inval;
  189. nc = nfnetlink_find_client(type, ss);
  190. if (!nc) {
  191. DEBUGP("unable to find client for type %d\n", type);
  192. goto err_inval;
  193. }
  194. if (nc->cap_required &&
  195. !cap_raised(NETLINK_CB(skb).eff_cap, nc->cap_required)) {
  196. DEBUGP("permission denied for type %d\n", type);
  197. *errp = -EPERM;
  198. return -1;
  199. }
  200. {
  201. struct nfattr *cda[ss->attr_count];
  202. memset(cda, 0, ss->attr_count*sizeof(struct nfattr *));
  203. err = nfnetlink_check_attributes(ss, nlh, cda);
  204. if (err < 0)
  205. goto err_inval;
  206. err = nc->call(nfnl, skb, nlh, cda, errp);
  207. *errp = err;
  208. return err;
  209. }
  210. err_inval:
  211. *errp = -EINVAL;
  212. return -1;
  213. }
  214. /* Process one packet of messages. */
  215. static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
  216. {
  217. int err;
  218. struct nlmsghdr *nlh;
  219. while (skb->len >= NLMSG_SPACE(0)) {
  220. u32 rlen;
  221. nlh = (struct nlmsghdr *)skb->data;
  222. if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
  223. || skb->len < nlh->nlmsg_len)
  224. return 0;
  225. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  226. if (rlen > skb->len)
  227. rlen = skb->len;
  228. if (nfnetlink_rcv_msg(skb, nlh, &err)) {
  229. if (!err)
  230. return -1;
  231. netlink_ack(skb, nlh, err);
  232. } else
  233. if (nlh->nlmsg_flags & NLM_F_ACK)
  234. netlink_ack(skb, nlh, 0);
  235. skb_pull(skb, rlen);
  236. }
  237. return 0;
  238. }
  239. static void nfnetlink_rcv(struct sock *sk, int len)
  240. {
  241. do {
  242. struct sk_buff *skb;
  243. if (nfnl_shlock_nowait())
  244. return;
  245. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  246. if (nfnetlink_rcv_skb(skb)) {
  247. if (skb->len)
  248. skb_queue_head(&sk->sk_receive_queue,
  249. skb);
  250. else
  251. kfree_skb(skb);
  252. break;
  253. }
  254. kfree_skb(skb);
  255. }
  256. up(&nfnl_sem);
  257. } while(nfnl && nfnl->sk_receive_queue.qlen);
  258. }
  259. void __exit nfnetlink_exit(void)
  260. {
  261. printk("Removing netfilter NETLINK layer.\n");
  262. sock_release(nfnl->sk_socket);
  263. return;
  264. }
  265. int __init nfnetlink_init(void)
  266. {
  267. printk("Netfilter messages via NETLINK v%s.\n", nfversion);
  268. nfnl = netlink_kernel_create(NETLINK_NETFILTER, nfnetlink_rcv);
  269. if (!nfnl) {
  270. printk(KERN_ERR "cannot initialize nfnetlink!\n");
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. module_init(nfnetlink_init);
  276. module_exit(nfnetlink_exit);
  277. EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
  278. EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
  279. EXPORT_SYMBOL_GPL(nfnetlink_send);
  280. EXPORT_SYMBOL_GPL(nfnetlink_unicast);
  281. EXPORT_SYMBOL_GPL(nfattr_parse);
  282. EXPORT_SYMBOL_GPL(__nfa_fill);