l2_packet_privsep.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * WPA Supplicant - Layer2 packet handling with privilege separation
  3. * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include <sys/un.h>
  10. #include "common.h"
  11. #include "eloop.h"
  12. #include "l2_packet.h"
  13. #include "common/privsep_commands.h"
  14. struct l2_packet_data {
  15. int fd; /* UNIX domain socket for privsep access */
  16. void (*rx_callback)(void *ctx, const u8 *src_addr,
  17. const u8 *buf, size_t len);
  18. void *rx_callback_ctx;
  19. u8 own_addr[ETH_ALEN];
  20. char *own_socket_path;
  21. struct sockaddr_un priv_addr;
  22. };
  23. static int wpa_priv_cmd(struct l2_packet_data *l2, int cmd,
  24. const void *data, size_t data_len)
  25. {
  26. struct msghdr msg;
  27. struct iovec io[2];
  28. io[0].iov_base = &cmd;
  29. io[0].iov_len = sizeof(cmd);
  30. io[1].iov_base = (u8 *) data;
  31. io[1].iov_len = data_len;
  32. os_memset(&msg, 0, sizeof(msg));
  33. msg.msg_iov = io;
  34. msg.msg_iovlen = data ? 2 : 1;
  35. msg.msg_name = &l2->priv_addr;
  36. msg.msg_namelen = sizeof(l2->priv_addr);
  37. if (sendmsg(l2->fd, &msg, 0) < 0) {
  38. perror("L2: sendmsg(cmd)");
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
  44. {
  45. os_memcpy(addr, l2->own_addr, ETH_ALEN);
  46. return 0;
  47. }
  48. int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
  49. const u8 *buf, size_t len)
  50. {
  51. struct msghdr msg;
  52. struct iovec io[4];
  53. int cmd = PRIVSEP_CMD_L2_SEND;
  54. io[0].iov_base = &cmd;
  55. io[0].iov_len = sizeof(cmd);
  56. io[1].iov_base = &dst_addr;
  57. io[1].iov_len = ETH_ALEN;
  58. io[2].iov_base = &proto;
  59. io[2].iov_len = 2;
  60. io[3].iov_base = (u8 *) buf;
  61. io[3].iov_len = len;
  62. os_memset(&msg, 0, sizeof(msg));
  63. msg.msg_iov = io;
  64. msg.msg_iovlen = 4;
  65. msg.msg_name = &l2->priv_addr;
  66. msg.msg_namelen = sizeof(l2->priv_addr);
  67. if (sendmsg(l2->fd, &msg, 0) < 0) {
  68. perror("L2: sendmsg(packet_send)");
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
  74. {
  75. struct l2_packet_data *l2 = eloop_ctx;
  76. u8 buf[2300];
  77. int res;
  78. struct sockaddr_un from;
  79. socklen_t fromlen = sizeof(from);
  80. os_memset(&from, 0, sizeof(from));
  81. res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
  82. &fromlen);
  83. if (res < 0) {
  84. perror("l2_packet_receive - recvfrom");
  85. return;
  86. }
  87. if (res < ETH_ALEN) {
  88. wpa_printf(MSG_DEBUG, "L2: Too show packet received");
  89. return;
  90. }
  91. if (from.sun_family != AF_UNIX ||
  92. os_strncmp(from.sun_path, l2->priv_addr.sun_path,
  93. sizeof(from.sun_path)) != 0) {
  94. wpa_printf(MSG_DEBUG, "L2: Received message from unexpected "
  95. "source");
  96. return;
  97. }
  98. l2->rx_callback(l2->rx_callback_ctx, buf, buf + ETH_ALEN,
  99. res - ETH_ALEN);
  100. }
  101. struct l2_packet_data * l2_packet_init(
  102. const char *ifname, const u8 *own_addr, unsigned short protocol,
  103. void (*rx_callback)(void *ctx, const u8 *src_addr,
  104. const u8 *buf, size_t len),
  105. void *rx_callback_ctx, int l2_hdr)
  106. {
  107. struct l2_packet_data *l2;
  108. char *own_dir = "/tmp";
  109. char *priv_dir = "/var/run/wpa_priv";
  110. size_t len;
  111. static unsigned int counter = 0;
  112. struct sockaddr_un addr;
  113. fd_set rfds;
  114. struct timeval tv;
  115. int res;
  116. u8 reply[ETH_ALEN + 1];
  117. int reg_cmd[2];
  118. l2 = os_zalloc(sizeof(struct l2_packet_data));
  119. if (l2 == NULL)
  120. return NULL;
  121. l2->rx_callback = rx_callback;
  122. l2->rx_callback_ctx = rx_callback_ctx;
  123. len = os_strlen(own_dir) + 50;
  124. l2->own_socket_path = os_malloc(len);
  125. if (l2->own_socket_path == NULL) {
  126. os_free(l2);
  127. return NULL;
  128. }
  129. os_snprintf(l2->own_socket_path, len, "%s/wpa_privsep-l2-%d-%d",
  130. own_dir, getpid(), counter++);
  131. l2->priv_addr.sun_family = AF_UNIX;
  132. os_snprintf(l2->priv_addr.sun_path, sizeof(l2->priv_addr.sun_path),
  133. "%s/%s", priv_dir, ifname);
  134. l2->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
  135. if (l2->fd < 0) {
  136. perror("socket(PF_UNIX)");
  137. os_free(l2->own_socket_path);
  138. l2->own_socket_path = NULL;
  139. os_free(l2);
  140. return NULL;
  141. }
  142. os_memset(&addr, 0, sizeof(addr));
  143. addr.sun_family = AF_UNIX;
  144. os_strlcpy(addr.sun_path, l2->own_socket_path, sizeof(addr.sun_path));
  145. if (bind(l2->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  146. perror("l2-pkt-privsep: bind(PF_UNIX)");
  147. goto fail;
  148. }
  149. reg_cmd[0] = protocol;
  150. reg_cmd[1] = l2_hdr;
  151. if (wpa_priv_cmd(l2, PRIVSEP_CMD_L2_REGISTER, reg_cmd, sizeof(reg_cmd))
  152. < 0) {
  153. wpa_printf(MSG_ERROR, "L2: Failed to register with wpa_priv");
  154. goto fail;
  155. }
  156. FD_ZERO(&rfds);
  157. FD_SET(l2->fd, &rfds);
  158. tv.tv_sec = 5;
  159. tv.tv_usec = 0;
  160. res = select(l2->fd + 1, &rfds, NULL, NULL, &tv);
  161. if (res < 0 && errno != EINTR) {
  162. perror("select");
  163. goto fail;
  164. }
  165. if (FD_ISSET(l2->fd, &rfds)) {
  166. res = recv(l2->fd, reply, sizeof(reply), 0);
  167. if (res < 0) {
  168. perror("recv");
  169. goto fail;
  170. }
  171. } else {
  172. wpa_printf(MSG_DEBUG, "L2: Timeout while waiting for "
  173. "registration reply");
  174. goto fail;
  175. }
  176. if (res != ETH_ALEN) {
  177. wpa_printf(MSG_DEBUG, "L2: Unexpected registration reply "
  178. "(len=%d)", res);
  179. }
  180. os_memcpy(l2->own_addr, reply, ETH_ALEN);
  181. eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
  182. return l2;
  183. fail:
  184. close(l2->fd);
  185. l2->fd = -1;
  186. unlink(l2->own_socket_path);
  187. os_free(l2->own_socket_path);
  188. l2->own_socket_path = NULL;
  189. os_free(l2);
  190. return NULL;
  191. }
  192. void l2_packet_deinit(struct l2_packet_data *l2)
  193. {
  194. if (l2 == NULL)
  195. return;
  196. if (l2->fd >= 0) {
  197. wpa_priv_cmd(l2, PRIVSEP_CMD_L2_UNREGISTER, NULL, 0);
  198. eloop_unregister_read_sock(l2->fd);
  199. close(l2->fd);
  200. }
  201. if (l2->own_socket_path) {
  202. unlink(l2->own_socket_path);
  203. os_free(l2->own_socket_path);
  204. }
  205. os_free(l2);
  206. }
  207. int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
  208. {
  209. /* TODO */
  210. return -1;
  211. }
  212. void l2_packet_notify_auth_start(struct l2_packet_data *l2)
  213. {
  214. wpa_priv_cmd(l2, PRIVSEP_CMD_L2_NOTIFY_AUTH_START, NULL, 0);
  215. }