ctrl_iface_udp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * WPA Supplicant / UDP socket -based control interface
  3. * Copyright (c) 2004-2016, 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 "common.h"
  10. #include "eloop.h"
  11. #include "config.h"
  12. #include "eapol_supp/eapol_supp_sm.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "ctrl_iface.h"
  15. #include "common/wpa_ctrl.h"
  16. #define COOKIE_LEN 8
  17. /* Per-interface ctrl_iface */
  18. /**
  19. * struct wpa_ctrl_dst - Internal data structure of control interface monitors
  20. *
  21. * This structure is used to store information about registered control
  22. * interface monitors into struct wpa_supplicant. This data is private to
  23. * ctrl_iface_udp.c and should not be touched directly from other files.
  24. */
  25. struct wpa_ctrl_dst {
  26. struct wpa_ctrl_dst *next;
  27. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  28. struct sockaddr_in6 addr;
  29. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  30. struct sockaddr_in addr;
  31. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  32. socklen_t addrlen;
  33. int debug_level;
  34. int errors;
  35. };
  36. struct ctrl_iface_priv {
  37. struct wpa_supplicant *wpa_s;
  38. int sock;
  39. struct wpa_ctrl_dst *ctrl_dst;
  40. u8 cookie[COOKIE_LEN];
  41. };
  42. struct ctrl_iface_global_priv {
  43. int sock;
  44. struct wpa_ctrl_dst *ctrl_dst;
  45. u8 cookie[COOKIE_LEN];
  46. };
  47. static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
  48. const char *ifname, int sock,
  49. struct wpa_ctrl_dst **head,
  50. int level, const char *buf,
  51. size_t len);
  52. static void wpas_ctrl_iface_free_dst(struct wpa_ctrl_dst *dst)
  53. {
  54. struct wpa_ctrl_dst *prev;
  55. while (dst) {
  56. prev = dst;
  57. dst = dst->next;
  58. os_free(prev);
  59. }
  60. }
  61. static int wpa_supplicant_ctrl_iface_attach(struct wpa_ctrl_dst **head,
  62. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  63. struct sockaddr_in6 *from,
  64. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  65. struct sockaddr_in *from,
  66. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  67. socklen_t fromlen)
  68. {
  69. struct wpa_ctrl_dst *dst;
  70. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  71. char addr[INET6_ADDRSTRLEN];
  72. #endif /* CONFIG_UDP_IPV6 */
  73. dst = os_zalloc(sizeof(*dst));
  74. if (dst == NULL)
  75. return -1;
  76. os_memcpy(&dst->addr, from, sizeof(*from));
  77. dst->addrlen = fromlen;
  78. dst->debug_level = MSG_INFO;
  79. dst->next = *head;
  80. *head = dst;
  81. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  82. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
  83. inet_ntop(AF_INET6, &from->sin6_addr, addr, sizeof(*from)),
  84. ntohs(from->sin6_port));
  85. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  86. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
  87. inet_ntoa(from->sin_addr), ntohs(from->sin_port));
  88. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  89. return 0;
  90. }
  91. static int wpa_supplicant_ctrl_iface_detach(struct wpa_ctrl_dst **head,
  92. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  93. struct sockaddr_in6 *from,
  94. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  95. struct sockaddr_in *from,
  96. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  97. socklen_t fromlen)
  98. {
  99. struct wpa_ctrl_dst *dst, *prev = NULL;
  100. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  101. char addr[INET6_ADDRSTRLEN];
  102. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  103. dst = *head;
  104. while (dst) {
  105. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  106. if (from->sin6_port == dst->addr.sin6_port &&
  107. !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
  108. sizeof(from->sin6_addr))) {
  109. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s:%d",
  110. inet_ntop(AF_INET6, &from->sin6_addr, addr,
  111. sizeof(*from)),
  112. ntohs(from->sin6_port));
  113. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  114. if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
  115. from->sin_port == dst->addr.sin_port) {
  116. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached "
  117. "%s:%d", inet_ntoa(from->sin_addr),
  118. ntohs(from->sin_port));
  119. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  120. if (prev == NULL)
  121. *head = dst->next;
  122. else
  123. prev->next = dst->next;
  124. os_free(dst);
  125. return 0;
  126. }
  127. prev = dst;
  128. dst = dst->next;
  129. }
  130. return -1;
  131. }
  132. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  133. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  134. struct sockaddr_in6 *from,
  135. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  136. struct sockaddr_in *from,
  137. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  138. socklen_t fromlen,
  139. char *level)
  140. {
  141. struct wpa_ctrl_dst *dst;
  142. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  143. char addr[INET6_ADDRSTRLEN];
  144. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  145. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  146. dst = priv->ctrl_dst;
  147. while (dst) {
  148. #if CONFIG_CTRL_IFACE_UDP_IPV6
  149. if (from->sin6_port == dst->addr.sin6_port &&
  150. !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
  151. sizeof(from->sin6_addr))) {
  152. wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level %s:%d",
  153. inet_ntop(AF_INET6, &from->sin6_addr, addr,
  154. sizeof(*from)),
  155. ntohs(from->sin6_port));
  156. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  157. if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
  158. from->sin_port == dst->addr.sin_port) {
  159. wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor "
  160. "level %s:%d", inet_ntoa(from->sin_addr),
  161. ntohs(from->sin_port));
  162. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  163. dst->debug_level = atoi(level);
  164. return 0;
  165. }
  166. dst = dst->next;
  167. }
  168. return -1;
  169. }
  170. static char *
  171. wpa_supplicant_ctrl_iface_get_cookie(struct ctrl_iface_priv *priv,
  172. size_t *reply_len)
  173. {
  174. char *reply;
  175. reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
  176. if (reply == NULL) {
  177. *reply_len = 1;
  178. return NULL;
  179. }
  180. os_memcpy(reply, "COOKIE=", 7);
  181. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  182. priv->cookie, COOKIE_LEN);
  183. *reply_len = 7 + 2 * COOKIE_LEN;
  184. return reply;
  185. }
  186. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  187. void *sock_ctx)
  188. {
  189. struct wpa_supplicant *wpa_s = eloop_ctx;
  190. struct ctrl_iface_priv *priv = sock_ctx;
  191. char buf[4096], *pos;
  192. int res;
  193. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  194. struct sockaddr_in6 from;
  195. #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
  196. char addr[INET6_ADDRSTRLEN];
  197. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  198. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  199. struct sockaddr_in from;
  200. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  201. socklen_t fromlen = sizeof(from);
  202. char *reply = NULL;
  203. size_t reply_len = 0;
  204. int new_attached = 0;
  205. u8 cookie[COOKIE_LEN];
  206. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  207. (struct sockaddr *) &from, &fromlen);
  208. if (res < 0) {
  209. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  210. strerror(errno));
  211. return;
  212. }
  213. #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
  214. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  215. inet_ntop(AF_INET6, &from.sin6_addr, addr, sizeof(from));
  216. if (os_strcmp(addr, "::1")) {
  217. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected source %s",
  218. addr);
  219. }
  220. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  221. if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
  222. /*
  223. * The OS networking stack is expected to drop this kind of
  224. * frames since the socket is bound to only localhost address.
  225. * Just in case, drop the frame if it is coming from any other
  226. * address.
  227. */
  228. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
  229. "source %s", inet_ntoa(from.sin_addr));
  230. return;
  231. }
  232. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  233. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  234. buf[res] = '\0';
  235. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  236. reply = wpa_supplicant_ctrl_iface_get_cookie(priv, &reply_len);
  237. goto done;
  238. }
  239. /*
  240. * Require that the client includes a prefix with the 'cookie' value
  241. * fetched with GET_COOKIE command. This is used to verify that the
  242. * client has access to a bidirectional link over UDP in order to
  243. * avoid attacks using forged localhost IP address even if the OS does
  244. * not block such frames from remote destinations.
  245. */
  246. if (os_strncmp(buf, "COOKIE=", 7) != 0) {
  247. wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
  248. "drop request");
  249. return;
  250. }
  251. if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
  252. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
  253. "request - drop request");
  254. return;
  255. }
  256. if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
  257. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
  258. "drop request");
  259. return;
  260. }
  261. pos = buf + 7 + 2 * COOKIE_LEN;
  262. while (*pos == ' ')
  263. pos++;
  264. if (os_strcmp(pos, "ATTACH") == 0) {
  265. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
  266. &from, fromlen))
  267. reply_len = 1;
  268. else {
  269. new_attached = 1;
  270. reply_len = 2;
  271. }
  272. } else if (os_strcmp(pos, "DETACH") == 0) {
  273. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst,
  274. &from, fromlen))
  275. reply_len = 1;
  276. else
  277. reply_len = 2;
  278. } else if (os_strncmp(pos, "LEVEL ", 6) == 0) {
  279. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  280. pos + 6))
  281. reply_len = 1;
  282. else
  283. reply_len = 2;
  284. } else {
  285. reply = wpa_supplicant_ctrl_iface_process(wpa_s, pos,
  286. &reply_len);
  287. }
  288. done:
  289. if (reply) {
  290. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  291. fromlen);
  292. os_free(reply);
  293. } else if (reply_len == 1) {
  294. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  295. fromlen);
  296. } else if (reply_len == 2) {
  297. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  298. fromlen);
  299. }
  300. if (new_attached)
  301. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  302. }
  303. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  304. enum wpa_msg_type type,
  305. const char *txt, size_t len)
  306. {
  307. struct wpa_supplicant *wpa_s = ctx;
  308. if (!wpa_s)
  309. return;
  310. if (type != WPA_MSG_NO_GLOBAL && wpa_s->global->ctrl_iface) {
  311. struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
  312. if (priv->ctrl_dst) {
  313. wpa_supplicant_ctrl_iface_send(
  314. wpa_s,
  315. type != WPA_MSG_PER_INTERFACE ?
  316. NULL : wpa_s->ifname,
  317. priv->sock, &priv->ctrl_dst, level, txt, len);
  318. }
  319. }
  320. if (type == WPA_MSG_ONLY_GLOBAL || !wpa_s->ctrl_iface)
  321. return;
  322. wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
  323. &wpa_s->ctrl_iface->ctrl_dst,
  324. level, txt, len);
  325. }
  326. struct ctrl_iface_priv *
  327. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  328. {
  329. struct ctrl_iface_priv *priv;
  330. char port_str[40];
  331. int port = WPA_CTRL_IFACE_PORT;
  332. char *pos;
  333. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  334. struct sockaddr_in6 addr;
  335. int domain = PF_INET6;
  336. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  337. struct sockaddr_in addr;
  338. int domain = PF_INET;
  339. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  340. priv = os_zalloc(sizeof(*priv));
  341. if (priv == NULL)
  342. return NULL;
  343. priv->wpa_s = wpa_s;
  344. priv->sock = -1;
  345. os_get_random(priv->cookie, COOKIE_LEN);
  346. if (wpa_s->conf->ctrl_interface == NULL)
  347. return priv;
  348. pos = os_strstr(wpa_s->conf->ctrl_interface, "udp:");
  349. if (pos) {
  350. pos += 4;
  351. port = atoi(pos);
  352. if (port <= 0) {
  353. wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port: %s",
  354. wpa_s->conf->ctrl_interface);
  355. goto fail;
  356. }
  357. }
  358. priv->sock = socket(domain, SOCK_DGRAM, 0);
  359. if (priv->sock < 0) {
  360. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  361. goto fail;
  362. }
  363. os_memset(&addr, 0, sizeof(addr));
  364. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  365. addr.sin6_family = AF_INET6;
  366. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  367. addr.sin6_addr = in6addr_any;
  368. #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  369. inet_pton(AF_INET6, "::1", &addr.sin6_addr);
  370. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  371. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  372. addr.sin_family = AF_INET;
  373. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  374. addr.sin_addr.s_addr = INADDR_ANY;
  375. #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  376. addr.sin_addr.s_addr = htonl((127 << 24) | 1);
  377. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  378. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  379. try_again:
  380. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  381. addr.sin6_port = htons(port);
  382. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  383. addr.sin_port = htons(port);
  384. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  385. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  386. port--;
  387. if ((WPA_CTRL_IFACE_PORT - port) < WPA_CTRL_IFACE_PORT_LIMIT)
  388. goto try_again;
  389. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  390. goto fail;
  391. }
  392. /* Update the ctrl_interface value to match the selected port */
  393. os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
  394. os_free(wpa_s->conf->ctrl_interface);
  395. wpa_s->conf->ctrl_interface = os_strdup(port_str);
  396. if (!wpa_s->conf->ctrl_interface) {
  397. wpa_msg(wpa_s, MSG_ERROR, "Failed to malloc ctrl_interface");
  398. goto fail;
  399. }
  400. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  401. wpa_msg(wpa_s, MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
  402. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  403. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  404. wpa_s, priv);
  405. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  406. return priv;
  407. fail:
  408. if (priv->sock >= 0)
  409. close(priv->sock);
  410. os_free(priv);
  411. return NULL;
  412. }
  413. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  414. {
  415. if (priv->sock > -1) {
  416. eloop_unregister_read_sock(priv->sock);
  417. if (priv->ctrl_dst) {
  418. /*
  419. * Wait before closing the control socket if
  420. * there are any attached monitors in order to allow
  421. * them to receive any pending messages.
  422. */
  423. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  424. "monitors to receive messages");
  425. os_sleep(0, 100000);
  426. }
  427. close(priv->sock);
  428. priv->sock = -1;
  429. }
  430. wpas_ctrl_iface_free_dst(priv->ctrl_dst);
  431. os_free(priv);
  432. }
  433. static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
  434. const char *ifname, int sock,
  435. struct wpa_ctrl_dst **head,
  436. int level, const char *buf,
  437. size_t len)
  438. {
  439. struct wpa_ctrl_dst *dst, *next;
  440. char levelstr[64];
  441. int idx;
  442. char *sbuf;
  443. int llen;
  444. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  445. char addr[INET6_ADDRSTRLEN];
  446. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  447. dst = *head;
  448. if (sock < 0 || dst == NULL)
  449. return;
  450. if (ifname)
  451. os_snprintf(levelstr, sizeof(levelstr), "IFACE=%s <%d>",
  452. ifname, level);
  453. else
  454. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  455. llen = os_strlen(levelstr);
  456. sbuf = os_malloc(llen + len);
  457. if (sbuf == NULL)
  458. return;
  459. os_memcpy(sbuf, levelstr, llen);
  460. os_memcpy(sbuf + llen, buf, len);
  461. idx = 0;
  462. while (dst) {
  463. next = dst->next;
  464. if (level >= dst->debug_level) {
  465. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  466. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
  467. inet_ntop(AF_INET6, &dst->addr.sin6_addr,
  468. addr, sizeof(dst->addr)),
  469. ntohs(dst->addr.sin6_port));
  470. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  471. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
  472. inet_ntoa(dst->addr.sin_addr),
  473. ntohs(dst->addr.sin_port));
  474. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  475. if (sendto(sock, sbuf, llen + len, 0,
  476. (struct sockaddr *) &dst->addr,
  477. sizeof(dst->addr)) < 0) {
  478. wpa_printf(MSG_ERROR,
  479. "sendto(CTRL_IFACE monitor): %s",
  480. strerror(errno));
  481. dst->errors++;
  482. if (dst->errors > 10) {
  483. wpa_supplicant_ctrl_iface_detach(
  484. head, &dst->addr,
  485. dst->addrlen);
  486. }
  487. } else
  488. dst->errors = 0;
  489. }
  490. idx++;
  491. dst = next;
  492. }
  493. os_free(sbuf);
  494. }
  495. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  496. {
  497. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
  498. priv->wpa_s->ifname);
  499. eloop_wait_for_read_sock(priv->sock);
  500. }
  501. /* Global ctrl_iface */
  502. static char *
  503. wpa_supplicant_global_get_cookie(struct ctrl_iface_global_priv *priv,
  504. size_t *reply_len)
  505. {
  506. char *reply;
  507. reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
  508. if (reply == NULL) {
  509. *reply_len = 1;
  510. return NULL;
  511. }
  512. os_memcpy(reply, "COOKIE=", 7);
  513. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  514. priv->cookie, COOKIE_LEN);
  515. *reply_len = 7 + 2 * COOKIE_LEN;
  516. return reply;
  517. }
  518. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  519. void *sock_ctx)
  520. {
  521. struct wpa_global *global = eloop_ctx;
  522. struct ctrl_iface_global_priv *priv = sock_ctx;
  523. char buf[4096], *pos;
  524. int res;
  525. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  526. struct sockaddr_in6 from;
  527. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  528. struct sockaddr_in from;
  529. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  530. socklen_t fromlen = sizeof(from);
  531. char *reply = NULL;
  532. size_t reply_len;
  533. u8 cookie[COOKIE_LEN];
  534. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  535. (struct sockaddr *) &from, &fromlen);
  536. if (res < 0) {
  537. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  538. strerror(errno));
  539. return;
  540. }
  541. #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
  542. #ifndef CONFIG_CTRL_IFACE_UDP_IPV6
  543. if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
  544. /*
  545. * The OS networking stack is expected to drop this kind of
  546. * frames since the socket is bound to only localhost address.
  547. * Just in case, drop the frame if it is coming from any other
  548. * address.
  549. */
  550. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
  551. "source %s", inet_ntoa(from.sin_addr));
  552. return;
  553. }
  554. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  555. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  556. buf[res] = '\0';
  557. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  558. reply = wpa_supplicant_global_get_cookie(priv, &reply_len);
  559. goto done;
  560. }
  561. if (os_strncmp(buf, "COOKIE=", 7) != 0) {
  562. wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
  563. "drop request");
  564. return;
  565. }
  566. if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
  567. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
  568. "request - drop request");
  569. return;
  570. }
  571. if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
  572. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
  573. "drop request");
  574. return;
  575. }
  576. pos = buf + 7 + 2 * COOKIE_LEN;
  577. while (*pos == ' ')
  578. pos++;
  579. if (os_strcmp(pos, "ATTACH") == 0) {
  580. if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
  581. &from, fromlen))
  582. reply_len = 1;
  583. else
  584. reply_len = 2;
  585. } else if (os_strcmp(pos, "DETACH") == 0) {
  586. if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst,
  587. &from, fromlen))
  588. reply_len = 1;
  589. else
  590. reply_len = 2;
  591. } else {
  592. reply = wpa_supplicant_global_ctrl_iface_process(global, pos,
  593. &reply_len);
  594. }
  595. done:
  596. if (reply) {
  597. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  598. fromlen);
  599. os_free(reply);
  600. } else if (reply_len == 1) {
  601. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  602. fromlen);
  603. } else if (reply_len == 2) {
  604. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  605. fromlen);
  606. }
  607. }
  608. struct ctrl_iface_global_priv *
  609. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  610. {
  611. struct ctrl_iface_global_priv *priv;
  612. struct sockaddr_in addr;
  613. char *pos;
  614. int port = WPA_GLOBAL_CTRL_IFACE_PORT;
  615. priv = os_zalloc(sizeof(*priv));
  616. if (priv == NULL)
  617. return NULL;
  618. priv->sock = -1;
  619. os_get_random(priv->cookie, COOKIE_LEN);
  620. if (global->params.ctrl_interface == NULL)
  621. return priv;
  622. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  623. global->params.ctrl_interface);
  624. pos = os_strstr(global->params.ctrl_interface, "udp:");
  625. if (pos) {
  626. pos += 4;
  627. port = atoi(pos);
  628. if (port <= 0) {
  629. wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port %s",
  630. global->params.ctrl_interface);
  631. goto fail;
  632. }
  633. }
  634. priv->sock = socket(PF_INET, SOCK_DGRAM, 0);
  635. if (priv->sock < 0) {
  636. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  637. goto fail;
  638. }
  639. os_memset(&addr, 0, sizeof(addr));
  640. addr.sin_family = AF_INET;
  641. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  642. addr.sin_addr.s_addr = INADDR_ANY;
  643. #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  644. addr.sin_addr.s_addr = htonl((127 << 24) | 1);
  645. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  646. try_again:
  647. addr.sin_port = htons(port);
  648. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  649. port++;
  650. if ((port - WPA_GLOBAL_CTRL_IFACE_PORT) <
  651. WPA_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
  652. goto try_again;
  653. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  654. goto fail;
  655. }
  656. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  657. wpa_printf(MSG_DEBUG, "global_ctrl_iface_init UDP port: %d", port);
  658. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  659. eloop_register_read_sock(priv->sock,
  660. wpa_supplicant_global_ctrl_iface_receive,
  661. global, priv);
  662. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  663. return priv;
  664. fail:
  665. if (priv->sock >= 0)
  666. close(priv->sock);
  667. os_free(priv);
  668. return NULL;
  669. }
  670. void
  671. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  672. {
  673. if (priv->sock >= 0) {
  674. eloop_unregister_read_sock(priv->sock);
  675. close(priv->sock);
  676. }
  677. wpas_ctrl_iface_free_dst(priv->ctrl_dst);
  678. os_free(priv);
  679. }