netlink.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2004-2006 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  6. * Tyler Hicks <tyhicks@ou.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <net/sock.h>
  23. #include <linux/hash.h>
  24. #include <linux/random.h>
  25. #include "ecryptfs_kernel.h"
  26. static struct sock *ecryptfs_nl_sock;
  27. /**
  28. * ecryptfs_send_netlink
  29. * @data: The data to include as the payload
  30. * @data_len: The byte count of the data
  31. * @msg_ctx: The netlink context that will be used to handle the
  32. * response message
  33. * @msg_type: The type of netlink message to send
  34. * @msg_flags: The flags to include in the netlink header
  35. * @daemon_pid: The process id of the daemon to send the message to
  36. *
  37. * Sends the data to the specified daemon pid and uses the netlink
  38. * context element to store the data needed for validation upon
  39. * receiving the response. The data and the netlink context can be
  40. * null if just sending a netlink header is sufficient. Returns zero
  41. * upon sending the message; non-zero upon error.
  42. */
  43. int ecryptfs_send_netlink(char *data, int data_len,
  44. struct ecryptfs_msg_ctx *msg_ctx, u16 msg_type,
  45. u16 msg_flags, pid_t daemon_pid)
  46. {
  47. struct sk_buff *skb;
  48. struct nlmsghdr *nlh;
  49. struct ecryptfs_message *msg;
  50. size_t payload_len;
  51. int rc;
  52. payload_len = ((data && data_len) ? (sizeof(*msg) + data_len) : 0);
  53. skb = alloc_skb(NLMSG_SPACE(payload_len), GFP_KERNEL);
  54. if (!skb) {
  55. rc = -ENOMEM;
  56. ecryptfs_printk(KERN_ERR, "Failed to allocate socket buffer\n");
  57. goto out;
  58. }
  59. nlh = NLMSG_PUT(skb, daemon_pid, msg_ctx ? msg_ctx->counter : 0,
  60. msg_type, payload_len);
  61. nlh->nlmsg_flags = msg_flags;
  62. if (msg_ctx && payload_len) {
  63. msg = (struct ecryptfs_message *)NLMSG_DATA(nlh);
  64. msg->index = msg_ctx->index;
  65. msg->data_len = data_len;
  66. memcpy(msg->data, data, data_len);
  67. }
  68. rc = netlink_unicast(ecryptfs_nl_sock, skb, daemon_pid, 0);
  69. if (rc < 0) {
  70. ecryptfs_printk(KERN_ERR, "Failed to send eCryptfs netlink "
  71. "message; rc = [%d]\n", rc);
  72. goto out;
  73. }
  74. rc = 0;
  75. goto out;
  76. nlmsg_failure:
  77. rc = -EMSGSIZE;
  78. kfree_skb(skb);
  79. out:
  80. return rc;
  81. }
  82. /**
  83. * ecryptfs_process_nl_reponse
  84. * @skb: The socket buffer containing the netlink message of state
  85. * RESPONSE
  86. *
  87. * Processes a response message after sending a operation request to
  88. * userspace. Attempts to assign the msg to a netlink context element
  89. * at the index specified in the msg. The sk_buff and nlmsghdr must
  90. * be validated before this function. Returns zero upon delivery to
  91. * desired context element; non-zero upon delivery failure or error.
  92. */
  93. static int ecryptfs_process_nl_response(struct sk_buff *skb)
  94. {
  95. struct nlmsghdr *nlh = (struct nlmsghdr*)skb->data;
  96. struct ecryptfs_message *msg = NLMSG_DATA(nlh);
  97. int rc;
  98. if (skb->len - NLMSG_HDRLEN - sizeof(*msg) != msg->data_len) {
  99. rc = -EINVAL;
  100. ecryptfs_printk(KERN_ERR, "Received netlink message with "
  101. "incorrectly specified data length\n");
  102. goto out;
  103. }
  104. rc = ecryptfs_process_response(msg, NETLINK_CREDS(skb)->uid,
  105. NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq);
  106. if (rc)
  107. printk(KERN_ERR
  108. "Error processing response message; rc = [%d]\n", rc);
  109. out:
  110. return rc;
  111. }
  112. /**
  113. * ecryptfs_process_nl_helo
  114. * @skb: The socket buffer containing the nlmsghdr in HELO state
  115. *
  116. * Gets uid and pid of the skb and adds the values to the daemon id
  117. * hash. Returns zero after adding a new daemon id to the hash list;
  118. * non-zero otherwise.
  119. */
  120. static int ecryptfs_process_nl_helo(struct sk_buff *skb)
  121. {
  122. int rc;
  123. rc = ecryptfs_process_helo(ECRYPTFS_TRANSPORT_NETLINK,
  124. NETLINK_CREDS(skb)->uid,
  125. NETLINK_CREDS(skb)->pid);
  126. if (rc)
  127. printk(KERN_WARNING "Error processing HELO; rc = [%d]\n", rc);
  128. return rc;
  129. }
  130. /**
  131. * ecryptfs_process_nl_quit
  132. * @skb: The socket buffer containing the nlmsghdr in QUIT state
  133. *
  134. * Gets uid and pid of the skb and deletes the corresponding daemon
  135. * id, if it is the registered that is requesting the
  136. * deletion. Returns zero after deleting the desired daemon id;
  137. * non-zero otherwise.
  138. */
  139. static int ecryptfs_process_nl_quit(struct sk_buff *skb)
  140. {
  141. int rc;
  142. rc = ecryptfs_process_quit(NETLINK_CREDS(skb)->uid,
  143. NETLINK_CREDS(skb)->pid);
  144. if (rc)
  145. printk(KERN_WARNING
  146. "Error processing QUIT message; rc = [%d]\n", rc);
  147. return rc;
  148. }
  149. /**
  150. * ecryptfs_receive_nl_message
  151. *
  152. * Callback function called by netlink system when a message arrives.
  153. * If the message looks to be valid, then an attempt is made to assign
  154. * it to its desired netlink context element and wake up the process
  155. * that is waiting for a response.
  156. */
  157. static void ecryptfs_receive_nl_message(struct sock *sk, int len)
  158. {
  159. struct sk_buff *skb;
  160. struct nlmsghdr *nlh;
  161. int rc = 0; /* skb_recv_datagram requires this */
  162. receive:
  163. skb = skb_recv_datagram(sk, 0, 0, &rc);
  164. if (rc == -EINTR)
  165. goto receive;
  166. else if (rc < 0) {
  167. ecryptfs_printk(KERN_ERR, "Error occurred while "
  168. "receiving eCryptfs netlink message; "
  169. "rc = [%d]\n", rc);
  170. return;
  171. }
  172. nlh = (struct nlmsghdr *)skb->data;
  173. if (!NLMSG_OK(nlh, skb->len)) {
  174. ecryptfs_printk(KERN_ERR, "Received corrupt netlink "
  175. "message\n");
  176. goto free;
  177. }
  178. switch (nlh->nlmsg_type) {
  179. case ECRYPTFS_NLMSG_RESPONSE:
  180. if (ecryptfs_process_nl_response(skb)) {
  181. ecryptfs_printk(KERN_WARNING, "Failed to "
  182. "deliver netlink response to "
  183. "requesting operation\n");
  184. }
  185. break;
  186. case ECRYPTFS_NLMSG_HELO:
  187. if (ecryptfs_process_nl_helo(skb)) {
  188. ecryptfs_printk(KERN_WARNING, "Failed to "
  189. "fulfill HELO request\n");
  190. }
  191. break;
  192. case ECRYPTFS_NLMSG_QUIT:
  193. if (ecryptfs_process_nl_quit(skb)) {
  194. ecryptfs_printk(KERN_WARNING, "Failed to "
  195. "fulfill QUIT request\n");
  196. }
  197. break;
  198. default:
  199. ecryptfs_printk(KERN_WARNING, "Dropping netlink "
  200. "message of unrecognized type [%d]\n",
  201. nlh->nlmsg_type);
  202. break;
  203. }
  204. free:
  205. kfree_skb(skb);
  206. }
  207. /**
  208. * ecryptfs_init_netlink
  209. *
  210. * Initializes the daemon id hash list, netlink context array, and
  211. * necessary locks. Returns zero upon success; non-zero upon error.
  212. */
  213. int ecryptfs_init_netlink(void)
  214. {
  215. int rc;
  216. ecryptfs_nl_sock = netlink_kernel_create(NETLINK_ECRYPTFS, 0,
  217. ecryptfs_receive_nl_message,
  218. THIS_MODULE);
  219. if (!ecryptfs_nl_sock) {
  220. rc = -EIO;
  221. ecryptfs_printk(KERN_ERR, "Failed to create netlink socket\n");
  222. goto out;
  223. }
  224. ecryptfs_nl_sock->sk_sndtimeo = ECRYPTFS_DEFAULT_SEND_TIMEOUT;
  225. rc = 0;
  226. out:
  227. return rc;
  228. }
  229. /**
  230. * ecryptfs_release_netlink
  231. *
  232. * Frees all memory used by the netlink context array and releases the
  233. * netlink socket.
  234. */
  235. void ecryptfs_release_netlink(void)
  236. {
  237. if (ecryptfs_nl_sock && ecryptfs_nl_sock->sk_socket)
  238. sock_release(ecryptfs_nl_sock->sk_socket);
  239. ecryptfs_nl_sock = NULL;
  240. }