ctrl_iface_udp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * WPA Supplicant / UDP socket -based control interface
  3. * Copyright (c) 2004-2005, 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. struct sockaddr_in addr;
  28. socklen_t addrlen;
  29. int debug_level;
  30. int errors;
  31. };
  32. struct ctrl_iface_priv {
  33. struct wpa_supplicant *wpa_s;
  34. int sock;
  35. struct wpa_ctrl_dst *ctrl_dst;
  36. u8 cookie[COOKIE_LEN];
  37. };
  38. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  39. int level, const char *buf,
  40. size_t len);
  41. static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
  42. struct sockaddr_in *from,
  43. socklen_t fromlen)
  44. {
  45. struct wpa_ctrl_dst *dst;
  46. dst = os_zalloc(sizeof(*dst));
  47. if (dst == NULL)
  48. return -1;
  49. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_in));
  50. dst->addrlen = fromlen;
  51. dst->debug_level = MSG_INFO;
  52. dst->next = priv->ctrl_dst;
  53. priv->ctrl_dst = dst;
  54. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
  55. inet_ntoa(from->sin_addr), ntohs(from->sin_port));
  56. return 0;
  57. }
  58. static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
  59. struct sockaddr_in *from,
  60. socklen_t fromlen)
  61. {
  62. struct wpa_ctrl_dst *dst, *prev = NULL;
  63. dst = priv->ctrl_dst;
  64. while (dst) {
  65. if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
  66. from->sin_port == dst->addr.sin_port) {
  67. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached "
  68. "%s:%d", inet_ntoa(from->sin_addr),
  69. ntohs(from->sin_port));
  70. if (prev == NULL)
  71. priv->ctrl_dst = dst->next;
  72. else
  73. prev->next = dst->next;
  74. os_free(dst);
  75. return 0;
  76. }
  77. prev = dst;
  78. dst = dst->next;
  79. }
  80. return -1;
  81. }
  82. static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
  83. struct sockaddr_in *from,
  84. socklen_t fromlen,
  85. char *level)
  86. {
  87. struct wpa_ctrl_dst *dst;
  88. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  89. dst = priv->ctrl_dst;
  90. while (dst) {
  91. if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
  92. from->sin_port == dst->addr.sin_port) {
  93. wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor "
  94. "level %s:%d", inet_ntoa(from->sin_addr),
  95. ntohs(from->sin_port));
  96. dst->debug_level = atoi(level);
  97. return 0;
  98. }
  99. dst = dst->next;
  100. }
  101. return -1;
  102. }
  103. static char *
  104. wpa_supplicant_ctrl_iface_get_cookie(struct ctrl_iface_priv *priv,
  105. size_t *reply_len)
  106. {
  107. char *reply;
  108. reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
  109. if (reply == NULL) {
  110. *reply_len = 1;
  111. return NULL;
  112. }
  113. os_memcpy(reply, "COOKIE=", 7);
  114. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  115. priv->cookie, COOKIE_LEN);
  116. *reply_len = 7 + 2 * COOKIE_LEN;
  117. return reply;
  118. }
  119. static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
  120. void *sock_ctx)
  121. {
  122. struct wpa_supplicant *wpa_s = eloop_ctx;
  123. struct ctrl_iface_priv *priv = sock_ctx;
  124. char buf[256], *pos;
  125. int res;
  126. struct sockaddr_in from;
  127. socklen_t fromlen = sizeof(from);
  128. char *reply = NULL;
  129. size_t reply_len = 0;
  130. int new_attached = 0;
  131. u8 cookie[COOKIE_LEN];
  132. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  133. (struct sockaddr *) &from, &fromlen);
  134. if (res < 0) {
  135. perror("recvfrom(ctrl_iface)");
  136. return;
  137. }
  138. #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
  139. if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
  140. /*
  141. * The OS networking stack is expected to drop this kind of
  142. * frames since the socket is bound to only localhost address.
  143. * Just in case, drop the frame if it is coming from any other
  144. * address.
  145. */
  146. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
  147. "source %s", inet_ntoa(from.sin_addr));
  148. return;
  149. }
  150. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  151. buf[res] = '\0';
  152. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  153. reply = wpa_supplicant_ctrl_iface_get_cookie(priv, &reply_len);
  154. goto done;
  155. }
  156. /*
  157. * Require that the client includes a prefix with the 'cookie' value
  158. * fetched with GET_COOKIE command. This is used to verify that the
  159. * client has access to a bidirectional link over UDP in order to
  160. * avoid attacks using forged localhost IP address even if the OS does
  161. * not block such frames from remote destinations.
  162. */
  163. if (os_strncmp(buf, "COOKIE=", 7) != 0) {
  164. wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
  165. "drop request");
  166. return;
  167. }
  168. if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
  169. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
  170. "request - drop request");
  171. return;
  172. }
  173. if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
  174. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
  175. "drop request");
  176. return;
  177. }
  178. pos = buf + 7 + 2 * COOKIE_LEN;
  179. while (*pos == ' ')
  180. pos++;
  181. if (os_strcmp(pos, "ATTACH") == 0) {
  182. if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
  183. reply_len = 1;
  184. else {
  185. new_attached = 1;
  186. reply_len = 2;
  187. }
  188. } else if (os_strcmp(pos, "DETACH") == 0) {
  189. if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
  190. reply_len = 1;
  191. else
  192. reply_len = 2;
  193. } else if (os_strncmp(pos, "LEVEL ", 6) == 0) {
  194. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  195. pos + 6))
  196. reply_len = 1;
  197. else
  198. reply_len = 2;
  199. } else {
  200. reply = wpa_supplicant_ctrl_iface_process(wpa_s, pos,
  201. &reply_len);
  202. }
  203. done:
  204. if (reply) {
  205. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  206. fromlen);
  207. os_free(reply);
  208. } else if (reply_len == 1) {
  209. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  210. fromlen);
  211. } else if (reply_len == 2) {
  212. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  213. fromlen);
  214. }
  215. if (new_attached)
  216. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  217. }
  218. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
  219. const char *txt, size_t len)
  220. {
  221. struct wpa_supplicant *wpa_s = ctx;
  222. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  223. return;
  224. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  225. }
  226. struct ctrl_iface_priv *
  227. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  228. {
  229. struct ctrl_iface_priv *priv;
  230. struct sockaddr_in addr;
  231. int port = WPA_CTRL_IFACE_PORT;
  232. priv = os_zalloc(sizeof(*priv));
  233. if (priv == NULL)
  234. return NULL;
  235. priv->wpa_s = wpa_s;
  236. priv->sock = -1;
  237. os_get_random(priv->cookie, COOKIE_LEN);
  238. if (wpa_s->conf->ctrl_interface == NULL)
  239. return priv;
  240. priv->sock = socket(PF_INET, SOCK_DGRAM, 0);
  241. if (priv->sock < 0) {
  242. perror("socket(PF_INET)");
  243. goto fail;
  244. }
  245. os_memset(&addr, 0, sizeof(addr));
  246. addr.sin_family = AF_INET;
  247. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  248. addr.sin_addr.s_addr = INADDR_ANY;
  249. #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  250. addr.sin_addr.s_addr = htonl((127 << 24) | 1);
  251. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  252. try_again:
  253. addr.sin_port = htons(port);
  254. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  255. port--;
  256. if ((WPA_CTRL_IFACE_PORT - port) < WPA_CTRL_IFACE_PORT_LIMIT)
  257. goto try_again;
  258. perror("bind(AF_INET)");
  259. goto fail;
  260. }
  261. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  262. wpa_msg(wpa_s, MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
  263. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  264. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  265. wpa_s, priv);
  266. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  267. return priv;
  268. fail:
  269. if (priv->sock >= 0)
  270. close(priv->sock);
  271. os_free(priv);
  272. return NULL;
  273. }
  274. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  275. {
  276. struct wpa_ctrl_dst *dst, *prev;
  277. if (priv->sock > -1) {
  278. eloop_unregister_read_sock(priv->sock);
  279. if (priv->ctrl_dst) {
  280. /*
  281. * Wait before closing the control socket if
  282. * there are any attached monitors in order to allow
  283. * them to receive any pending messages.
  284. */
  285. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  286. "monitors to receive messages");
  287. os_sleep(0, 100000);
  288. }
  289. close(priv->sock);
  290. priv->sock = -1;
  291. }
  292. dst = priv->ctrl_dst;
  293. while (dst) {
  294. prev = dst;
  295. dst = dst->next;
  296. os_free(prev);
  297. }
  298. os_free(priv);
  299. }
  300. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  301. int level, const char *buf,
  302. size_t len)
  303. {
  304. struct wpa_ctrl_dst *dst, *next;
  305. char levelstr[10];
  306. int idx;
  307. char *sbuf;
  308. int llen;
  309. dst = priv->ctrl_dst;
  310. if (priv->sock < 0 || dst == NULL)
  311. return;
  312. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  313. llen = os_strlen(levelstr);
  314. sbuf = os_malloc(llen + len);
  315. if (sbuf == NULL)
  316. return;
  317. os_memcpy(sbuf, levelstr, llen);
  318. os_memcpy(sbuf + llen, buf, len);
  319. idx = 0;
  320. while (dst) {
  321. next = dst->next;
  322. if (level >= dst->debug_level) {
  323. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
  324. inet_ntoa(dst->addr.sin_addr),
  325. ntohs(dst->addr.sin_port));
  326. if (sendto(priv->sock, sbuf, llen + len, 0,
  327. (struct sockaddr *) &dst->addr,
  328. sizeof(dst->addr)) < 0) {
  329. perror("sendto(CTRL_IFACE monitor)");
  330. dst->errors++;
  331. if (dst->errors > 10) {
  332. wpa_supplicant_ctrl_iface_detach(
  333. priv, &dst->addr,
  334. dst->addrlen);
  335. }
  336. } else
  337. dst->errors = 0;
  338. }
  339. idx++;
  340. dst = next;
  341. }
  342. os_free(sbuf);
  343. }
  344. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  345. {
  346. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
  347. priv->wpa_s->ifname);
  348. eloop_wait_for_read_sock(priv->sock);
  349. }
  350. /* Global ctrl_iface */
  351. struct ctrl_iface_global_priv {
  352. int sock;
  353. u8 cookie[COOKIE_LEN];
  354. };
  355. static char *
  356. wpa_supplicant_global_get_cookie(struct ctrl_iface_global_priv *priv,
  357. size_t *reply_len)
  358. {
  359. char *reply;
  360. reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
  361. if (reply == NULL) {
  362. *reply_len = 1;
  363. return NULL;
  364. }
  365. os_memcpy(reply, "COOKIE=", 7);
  366. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  367. priv->cookie, COOKIE_LEN);
  368. *reply_len = 7 + 2 * COOKIE_LEN;
  369. return reply;
  370. }
  371. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  372. void *sock_ctx)
  373. {
  374. struct wpa_global *global = eloop_ctx;
  375. struct ctrl_iface_global_priv *priv = sock_ctx;
  376. char buf[256], *pos;
  377. int res;
  378. struct sockaddr_in from;
  379. socklen_t fromlen = sizeof(from);
  380. char *reply;
  381. size_t reply_len;
  382. u8 cookie[COOKIE_LEN];
  383. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  384. (struct sockaddr *) &from, &fromlen);
  385. if (res < 0) {
  386. perror("recvfrom(ctrl_iface)");
  387. return;
  388. }
  389. #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
  390. if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
  391. /*
  392. * The OS networking stack is expected to drop this kind of
  393. * frames since the socket is bound to only localhost address.
  394. * Just in case, drop the frame if it is coming from any other
  395. * address.
  396. */
  397. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
  398. "source %s", inet_ntoa(from.sin_addr));
  399. return;
  400. }
  401. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  402. buf[res] = '\0';
  403. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  404. reply = wpa_supplicant_global_get_cookie(priv, &reply_len);
  405. goto done;
  406. }
  407. if (os_strncmp(buf, "COOKIE=", 7) != 0) {
  408. wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
  409. "drop request");
  410. return;
  411. }
  412. if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
  413. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
  414. "request - drop request");
  415. return;
  416. }
  417. if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
  418. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
  419. "drop request");
  420. return;
  421. }
  422. pos = buf + 7 + 2 * COOKIE_LEN;
  423. while (*pos == ' ')
  424. pos++;
  425. reply = wpa_supplicant_global_ctrl_iface_process(global, pos,
  426. &reply_len);
  427. done:
  428. if (reply) {
  429. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  430. fromlen);
  431. os_free(reply);
  432. } else if (reply_len) {
  433. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  434. fromlen);
  435. }
  436. }
  437. struct ctrl_iface_global_priv *
  438. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  439. {
  440. struct ctrl_iface_global_priv *priv;
  441. struct sockaddr_in addr;
  442. int port = WPA_GLOBAL_CTRL_IFACE_PORT;
  443. priv = os_zalloc(sizeof(*priv));
  444. if (priv == NULL)
  445. return NULL;
  446. priv->sock = -1;
  447. os_get_random(priv->cookie, COOKIE_LEN);
  448. if (global->params.ctrl_interface == NULL)
  449. return priv;
  450. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  451. global->params.ctrl_interface);
  452. priv->sock = socket(PF_INET, SOCK_DGRAM, 0);
  453. if (priv->sock < 0) {
  454. perror("socket(PF_INET)");
  455. goto fail;
  456. }
  457. os_memset(&addr, 0, sizeof(addr));
  458. addr.sin_family = AF_INET;
  459. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  460. addr.sin_addr.s_addr = INADDR_ANY;
  461. #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  462. addr.sin_addr.s_addr = htonl((127 << 24) | 1);
  463. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  464. try_again:
  465. addr.sin_port = htons(port);
  466. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  467. port++;
  468. if ((port - WPA_GLOBAL_CTRL_IFACE_PORT) <
  469. WPA_GLOBAL_CTRL_IFACE_PORT_LIMIT)
  470. goto try_again;
  471. perror("bind(AF_INET)");
  472. goto fail;
  473. }
  474. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  475. wpa_printf(MSG_DEBUG, "global_ctrl_iface_init UDP port: %d", port);
  476. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  477. eloop_register_read_sock(priv->sock,
  478. wpa_supplicant_global_ctrl_iface_receive,
  479. global, priv);
  480. return priv;
  481. fail:
  482. if (priv->sock >= 0)
  483. close(priv->sock);
  484. os_free(priv);
  485. return NULL;
  486. }
  487. void
  488. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  489. {
  490. if (priv->sock >= 0) {
  491. eloop_unregister_read_sock(priv->sock);
  492. close(priv->sock);
  493. }
  494. os_free(priv);
  495. }