netlink.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, u8 msg_type,
  45. u16 msg_flags, struct pid *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, pid_nr(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, pid_nr(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 = nlmsg_hdr(skb);
  96. struct ecryptfs_message *msg = NLMSG_DATA(nlh);
  97. struct pid *pid;
  98. int rc;
  99. if (skb->len - NLMSG_HDRLEN - sizeof(*msg) != msg->data_len) {
  100. rc = -EINVAL;
  101. ecryptfs_printk(KERN_ERR, "Received netlink message with "
  102. "incorrectly specified data length\n");
  103. goto out;
  104. }
  105. pid = find_get_pid(NETLINK_CREDS(skb)->pid);
  106. rc = ecryptfs_process_response(msg, NETLINK_CREDS(skb)->uid, NULL,
  107. pid, nlh->nlmsg_seq);
  108. put_pid(pid);
  109. if (rc)
  110. printk(KERN_ERR
  111. "Error processing response message; rc = [%d]\n", rc);
  112. out:
  113. return rc;
  114. }
  115. /**
  116. * ecryptfs_process_nl_helo
  117. * @skb: The socket buffer containing the nlmsghdr in HELO state
  118. *
  119. * Gets uid and pid of the skb and adds the values to the daemon id
  120. * hash. Returns zero after adding a new daemon id to the hash list;
  121. * non-zero otherwise.
  122. */
  123. static int ecryptfs_process_nl_helo(struct sk_buff *skb)
  124. {
  125. struct pid *pid;
  126. int rc;
  127. pid = find_get_pid(NETLINK_CREDS(skb)->pid);
  128. rc = ecryptfs_process_helo(ECRYPTFS_TRANSPORT_NETLINK,
  129. NETLINK_CREDS(skb)->uid, NULL, pid);
  130. put_pid(pid);
  131. if (rc)
  132. printk(KERN_WARNING "Error processing HELO; rc = [%d]\n", rc);
  133. return rc;
  134. }
  135. /**
  136. * ecryptfs_process_nl_quit
  137. * @skb: The socket buffer containing the nlmsghdr in QUIT state
  138. *
  139. * Gets uid and pid of the skb and deletes the corresponding daemon
  140. * id, if it is the registered that is requesting the
  141. * deletion. Returns zero after deleting the desired daemon id;
  142. * non-zero otherwise.
  143. */
  144. static int ecryptfs_process_nl_quit(struct sk_buff *skb)
  145. {
  146. struct pid *pid;
  147. int rc;
  148. pid = find_get_pid(NETLINK_CREDS(skb)->pid);
  149. rc = ecryptfs_process_quit(NETLINK_CREDS(skb)->uid, NULL, pid);
  150. put_pid(pid);
  151. if (rc)
  152. printk(KERN_WARNING
  153. "Error processing QUIT message; rc = [%d]\n", rc);
  154. return rc;
  155. }
  156. /**
  157. * ecryptfs_receive_nl_message
  158. *
  159. * Callback function called by netlink system when a message arrives.
  160. * If the message looks to be valid, then an attempt is made to assign
  161. * it to its desired netlink context element and wake up the process
  162. * that is waiting for a response.
  163. */
  164. static void ecryptfs_receive_nl_message(struct sk_buff *skb)
  165. {
  166. struct nlmsghdr *nlh;
  167. nlh = nlmsg_hdr(skb);
  168. if (!NLMSG_OK(nlh, skb->len)) {
  169. ecryptfs_printk(KERN_ERR, "Received corrupt netlink "
  170. "message\n");
  171. goto free;
  172. }
  173. switch (nlh->nlmsg_type) {
  174. case ECRYPTFS_MSG_RESPONSE:
  175. if (ecryptfs_process_nl_response(skb)) {
  176. ecryptfs_printk(KERN_WARNING, "Failed to "
  177. "deliver netlink response to "
  178. "requesting operation\n");
  179. }
  180. break;
  181. case ECRYPTFS_MSG_HELO:
  182. if (ecryptfs_process_nl_helo(skb)) {
  183. ecryptfs_printk(KERN_WARNING, "Failed to "
  184. "fulfill HELO request\n");
  185. }
  186. break;
  187. case ECRYPTFS_MSG_QUIT:
  188. if (ecryptfs_process_nl_quit(skb)) {
  189. ecryptfs_printk(KERN_WARNING, "Failed to "
  190. "fulfill QUIT request\n");
  191. }
  192. break;
  193. default:
  194. ecryptfs_printk(KERN_WARNING, "Dropping netlink "
  195. "message of unrecognized type [%d]\n",
  196. nlh->nlmsg_type);
  197. break;
  198. }
  199. free:
  200. kfree_skb(skb);
  201. }
  202. /**
  203. * ecryptfs_init_netlink
  204. *
  205. * Initializes the daemon id hash list, netlink context array, and
  206. * necessary locks. Returns zero upon success; non-zero upon error.
  207. */
  208. int ecryptfs_init_netlink(void)
  209. {
  210. int rc;
  211. ecryptfs_nl_sock = netlink_kernel_create(&init_net, NETLINK_ECRYPTFS, 0,
  212. ecryptfs_receive_nl_message,
  213. NULL, THIS_MODULE);
  214. if (!ecryptfs_nl_sock) {
  215. rc = -EIO;
  216. ecryptfs_printk(KERN_ERR, "Failed to create netlink socket\n");
  217. goto out;
  218. }
  219. ecryptfs_nl_sock->sk_sndtimeo = ECRYPTFS_DEFAULT_SEND_TIMEOUT;
  220. rc = 0;
  221. out:
  222. return rc;
  223. }
  224. /**
  225. * ecryptfs_release_netlink
  226. *
  227. * Frees all memory used by the netlink context array and releases the
  228. * netlink socket.
  229. */
  230. void ecryptfs_release_netlink(void)
  231. {
  232. netlink_kernel_release(ecryptfs_nl_sock);
  233. ecryptfs_nl_sock = NULL;
  234. }