l2_packet_privsep.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. wpa_printf(MSG_ERROR, "L2: sendmsg(cmd): %s", strerror(errno));
  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. wpa_printf(MSG_ERROR, "L2: sendmsg(packet_send): %s",
  69. strerror(errno));
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
  75. {
  76. struct l2_packet_data *l2 = eloop_ctx;
  77. u8 buf[2300];
  78. int res;
  79. struct sockaddr_un from;
  80. socklen_t fromlen = sizeof(from);
  81. os_memset(&from, 0, sizeof(from));
  82. res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
  83. &fromlen);
  84. if (res < 0) {
  85. wpa_printf(MSG_ERROR, "l2_packet_receive - recvfrom: %s",
  86. strerror(errno));
  87. return;
  88. }
  89. if (res < ETH_ALEN) {
  90. wpa_printf(MSG_DEBUG, "L2: Too show packet received");
  91. return;
  92. }
  93. if (from.sun_family != AF_UNIX ||
  94. os_strncmp(from.sun_path, l2->priv_addr.sun_path,
  95. sizeof(from.sun_path)) != 0) {
  96. wpa_printf(MSG_DEBUG, "L2: Received message from unexpected "
  97. "source");
  98. return;
  99. }
  100. l2->rx_callback(l2->rx_callback_ctx, buf, buf + ETH_ALEN,
  101. res - ETH_ALEN);
  102. }
  103. struct l2_packet_data * l2_packet_init(
  104. const char *ifname, const u8 *own_addr, unsigned short protocol,
  105. void (*rx_callback)(void *ctx, const u8 *src_addr,
  106. const u8 *buf, size_t len),
  107. void *rx_callback_ctx, int l2_hdr)
  108. {
  109. struct l2_packet_data *l2;
  110. char *own_dir = "/tmp";
  111. char *priv_dir = "/var/run/wpa_priv";
  112. size_t len;
  113. static unsigned int counter = 0;
  114. struct sockaddr_un addr;
  115. fd_set rfds;
  116. struct timeval tv;
  117. int res;
  118. u8 reply[ETH_ALEN + 1];
  119. int reg_cmd[2];
  120. l2 = os_zalloc(sizeof(struct l2_packet_data));
  121. if (l2 == NULL)
  122. return NULL;
  123. l2->rx_callback = rx_callback;
  124. l2->rx_callback_ctx = rx_callback_ctx;
  125. len = os_strlen(own_dir) + 50;
  126. l2->own_socket_path = os_malloc(len);
  127. if (l2->own_socket_path == NULL) {
  128. os_free(l2);
  129. return NULL;
  130. }
  131. os_snprintf(l2->own_socket_path, len, "%s/wpa_privsep-l2-%d-%d",
  132. own_dir, getpid(), counter++);
  133. l2->priv_addr.sun_family = AF_UNIX;
  134. os_snprintf(l2->priv_addr.sun_path, sizeof(l2->priv_addr.sun_path),
  135. "%s/%s", priv_dir, ifname);
  136. l2->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
  137. if (l2->fd < 0) {
  138. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  139. os_free(l2->own_socket_path);
  140. l2->own_socket_path = NULL;
  141. os_free(l2);
  142. return NULL;
  143. }
  144. os_memset(&addr, 0, sizeof(addr));
  145. addr.sun_family = AF_UNIX;
  146. os_strlcpy(addr.sun_path, l2->own_socket_path, sizeof(addr.sun_path));
  147. if (bind(l2->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  148. wpa_printf(MSG_ERROR, "l2-pkt-privsep: bind(PF_UNIX): %s",
  149. strerror(errno));
  150. goto fail;
  151. }
  152. reg_cmd[0] = protocol;
  153. reg_cmd[1] = l2_hdr;
  154. if (wpa_priv_cmd(l2, PRIVSEP_CMD_L2_REGISTER, reg_cmd, sizeof(reg_cmd))
  155. < 0) {
  156. wpa_printf(MSG_ERROR, "L2: Failed to register with wpa_priv");
  157. goto fail;
  158. }
  159. FD_ZERO(&rfds);
  160. FD_SET(l2->fd, &rfds);
  161. tv.tv_sec = 5;
  162. tv.tv_usec = 0;
  163. res = select(l2->fd + 1, &rfds, NULL, NULL, &tv);
  164. if (res < 0 && errno != EINTR) {
  165. wpa_printf(MSG_ERROR, "select: %s", strerror(errno));
  166. goto fail;
  167. }
  168. if (FD_ISSET(l2->fd, &rfds)) {
  169. res = recv(l2->fd, reply, sizeof(reply), 0);
  170. if (res < 0) {
  171. wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
  172. goto fail;
  173. }
  174. } else {
  175. wpa_printf(MSG_DEBUG, "L2: Timeout while waiting for "
  176. "registration reply");
  177. goto fail;
  178. }
  179. if (res != ETH_ALEN) {
  180. wpa_printf(MSG_DEBUG, "L2: Unexpected registration reply "
  181. "(len=%d)", res);
  182. }
  183. os_memcpy(l2->own_addr, reply, ETH_ALEN);
  184. eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
  185. return l2;
  186. fail:
  187. close(l2->fd);
  188. l2->fd = -1;
  189. unlink(l2->own_socket_path);
  190. os_free(l2->own_socket_path);
  191. l2->own_socket_path = NULL;
  192. os_free(l2);
  193. return NULL;
  194. }
  195. struct l2_packet_data * l2_packet_init_bridge(
  196. const char *br_ifname, const char *ifname, const u8 *own_addr,
  197. unsigned short protocol,
  198. void (*rx_callback)(void *ctx, const u8 *src_addr,
  199. const u8 *buf, size_t len),
  200. void *rx_callback_ctx, int l2_hdr)
  201. {
  202. return l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
  203. rx_callback_ctx, l2_hdr);
  204. }
  205. void l2_packet_deinit(struct l2_packet_data *l2)
  206. {
  207. if (l2 == NULL)
  208. return;
  209. if (l2->fd >= 0) {
  210. wpa_priv_cmd(l2, PRIVSEP_CMD_L2_UNREGISTER, NULL, 0);
  211. eloop_unregister_read_sock(l2->fd);
  212. close(l2->fd);
  213. }
  214. if (l2->own_socket_path) {
  215. unlink(l2->own_socket_path);
  216. os_free(l2->own_socket_path);
  217. }
  218. os_free(l2);
  219. }
  220. int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
  221. {
  222. /* TODO */
  223. return -1;
  224. }
  225. void l2_packet_notify_auth_start(struct l2_packet_data *l2)
  226. {
  227. wpa_priv_cmd(l2, PRIVSEP_CMD_L2_NOTIFY_AUTH_START, NULL, 0);
  228. }
  229. int l2_packet_set_packet_filter(struct l2_packet_data *l2,
  230. enum l2_packet_filter_type type)
  231. {
  232. return -1;
  233. }