ctrl_iface_udp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. if (prev == NULL)
  68. priv->ctrl_dst = dst->next;
  69. else
  70. prev->next = dst->next;
  71. os_free(dst);
  72. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached "
  73. "%s:%d", inet_ntoa(from->sin_addr),
  74. ntohs(from->sin_port));
  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. if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
  139. /*
  140. * The OS networking stack is expected to drop this kind of
  141. * frames since the socket is bound to only localhost address.
  142. * Just in case, drop the frame if it is coming from any other
  143. * address.
  144. */
  145. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
  146. "source %s", inet_ntoa(from.sin_addr));
  147. return;
  148. }
  149. buf[res] = '\0';
  150. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  151. reply = wpa_supplicant_ctrl_iface_get_cookie(priv, &reply_len);
  152. goto done;
  153. }
  154. /*
  155. * Require that the client includes a prefix with the 'cookie' value
  156. * fetched with GET_COOKIE command. This is used to verify that the
  157. * client has access to a bidirectional link over UDP in order to
  158. * avoid attacks using forged localhost IP address even if the OS does
  159. * not block such frames from remote destinations.
  160. */
  161. if (os_strncmp(buf, "COOKIE=", 7) != 0) {
  162. wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
  163. "drop request");
  164. return;
  165. }
  166. if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
  167. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
  168. "request - drop request");
  169. return;
  170. }
  171. if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
  172. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
  173. "drop request");
  174. return;
  175. }
  176. pos = buf + 7 + 2 * COOKIE_LEN;
  177. while (*pos == ' ')
  178. pos++;
  179. if (os_strcmp(pos, "ATTACH") == 0) {
  180. if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
  181. reply_len = 1;
  182. else {
  183. new_attached = 1;
  184. reply_len = 2;
  185. }
  186. } else if (os_strcmp(pos, "DETACH") == 0) {
  187. if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
  188. reply_len = 1;
  189. else
  190. reply_len = 2;
  191. } else if (os_strncmp(pos, "LEVEL ", 6) == 0) {
  192. if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
  193. pos + 6))
  194. reply_len = 1;
  195. else
  196. reply_len = 2;
  197. } else {
  198. reply = wpa_supplicant_ctrl_iface_process(wpa_s, pos,
  199. &reply_len);
  200. }
  201. done:
  202. if (reply) {
  203. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  204. fromlen);
  205. os_free(reply);
  206. } else if (reply_len == 1) {
  207. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  208. fromlen);
  209. } else if (reply_len == 2) {
  210. sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
  211. fromlen);
  212. }
  213. if (new_attached)
  214. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  215. }
  216. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  217. const char *txt, size_t len)
  218. {
  219. struct wpa_supplicant *wpa_s = ctx;
  220. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  221. return;
  222. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  223. }
  224. struct ctrl_iface_priv *
  225. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  226. {
  227. struct ctrl_iface_priv *priv;
  228. struct sockaddr_in addr;
  229. priv = os_zalloc(sizeof(*priv));
  230. if (priv == NULL)
  231. return NULL;
  232. priv->wpa_s = wpa_s;
  233. priv->sock = -1;
  234. os_get_random(priv->cookie, COOKIE_LEN);
  235. if (wpa_s->conf->ctrl_interface == NULL)
  236. return priv;
  237. priv->sock = socket(PF_INET, SOCK_DGRAM, 0);
  238. if (priv->sock < 0) {
  239. perror("socket(PF_INET)");
  240. goto fail;
  241. }
  242. os_memset(&addr, 0, sizeof(addr));
  243. addr.sin_family = AF_INET;
  244. addr.sin_addr.s_addr = htonl((127 << 24) | 1);
  245. addr.sin_port = htons(WPA_CTRL_IFACE_PORT);
  246. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  247. perror("bind(AF_INET)");
  248. goto fail;
  249. }
  250. eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
  251. wpa_s, priv);
  252. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  253. return priv;
  254. fail:
  255. if (priv->sock >= 0)
  256. close(priv->sock);
  257. os_free(priv);
  258. return NULL;
  259. }
  260. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  261. {
  262. struct wpa_ctrl_dst *dst, *prev;
  263. if (priv->sock > -1) {
  264. eloop_unregister_read_sock(priv->sock);
  265. if (priv->ctrl_dst) {
  266. /*
  267. * Wait a second before closing the control socket if
  268. * there are any attached monitors in order to allow
  269. * them to receive any pending messages.
  270. */
  271. wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
  272. "monitors to receive messages");
  273. os_sleep(1, 0);
  274. }
  275. close(priv->sock);
  276. priv->sock = -1;
  277. }
  278. dst = priv->ctrl_dst;
  279. while (dst) {
  280. prev = dst;
  281. dst = dst->next;
  282. os_free(prev);
  283. }
  284. os_free(priv);
  285. }
  286. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  287. int level, const char *buf,
  288. size_t len)
  289. {
  290. struct wpa_ctrl_dst *dst, *next;
  291. char levelstr[10];
  292. int idx;
  293. char *sbuf;
  294. int llen;
  295. dst = priv->ctrl_dst;
  296. if (priv->sock < 0 || dst == NULL)
  297. return;
  298. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  299. llen = os_strlen(levelstr);
  300. sbuf = os_malloc(llen + len);
  301. if (sbuf == NULL)
  302. return;
  303. os_memcpy(sbuf, levelstr, llen);
  304. os_memcpy(sbuf + llen, buf, len);
  305. idx = 0;
  306. while (dst) {
  307. next = dst->next;
  308. if (level >= dst->debug_level) {
  309. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
  310. inet_ntoa(dst->addr.sin_addr),
  311. ntohs(dst->addr.sin_port));
  312. if (sendto(priv->sock, sbuf, llen + len, 0,
  313. (struct sockaddr *) &dst->addr,
  314. sizeof(dst->addr)) < 0) {
  315. perror("sendto(CTRL_IFACE monitor)");
  316. dst->errors++;
  317. if (dst->errors > 10) {
  318. wpa_supplicant_ctrl_iface_detach(
  319. priv, &dst->addr,
  320. dst->addrlen);
  321. }
  322. } else
  323. dst->errors = 0;
  324. }
  325. idx++;
  326. dst = next;
  327. }
  328. os_free(sbuf);
  329. }
  330. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  331. {
  332. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
  333. priv->wpa_s->ifname);
  334. eloop_wait_for_read_sock(priv->sock);
  335. }
  336. /* Global ctrl_iface */
  337. struct ctrl_iface_global_priv {
  338. int sock;
  339. u8 cookie[COOKIE_LEN];
  340. };
  341. static char *
  342. wpa_supplicant_global_get_cookie(struct ctrl_iface_global_priv *priv,
  343. size_t *reply_len)
  344. {
  345. char *reply;
  346. reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
  347. if (reply == NULL) {
  348. *reply_len = 1;
  349. return NULL;
  350. }
  351. os_memcpy(reply, "COOKIE=", 7);
  352. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  353. priv->cookie, COOKIE_LEN);
  354. *reply_len = 7 + 2 * COOKIE_LEN;
  355. return reply;
  356. }
  357. static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  358. void *sock_ctx)
  359. {
  360. struct wpa_global *global = eloop_ctx;
  361. struct ctrl_iface_global_priv *priv = sock_ctx;
  362. char buf[256], *pos;
  363. int res;
  364. struct sockaddr_in from;
  365. socklen_t fromlen = sizeof(from);
  366. char *reply;
  367. size_t reply_len;
  368. u8 cookie[COOKIE_LEN];
  369. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  370. (struct sockaddr *) &from, &fromlen);
  371. if (res < 0) {
  372. perror("recvfrom(ctrl_iface)");
  373. return;
  374. }
  375. if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
  376. /*
  377. * The OS networking stack is expected to drop this kind of
  378. * frames since the socket is bound to only localhost address.
  379. * Just in case, drop the frame if it is coming from any other
  380. * address.
  381. */
  382. wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
  383. "source %s", inet_ntoa(from.sin_addr));
  384. return;
  385. }
  386. buf[res] = '\0';
  387. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  388. reply = wpa_supplicant_global_get_cookie(priv, &reply_len);
  389. goto done;
  390. }
  391. if (os_strncmp(buf, "COOKIE=", 7) != 0) {
  392. wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
  393. "drop request");
  394. return;
  395. }
  396. if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
  397. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
  398. "request - drop request");
  399. return;
  400. }
  401. if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
  402. wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
  403. "drop request");
  404. return;
  405. }
  406. pos = buf + 7 + 2 * COOKIE_LEN;
  407. while (*pos == ' ')
  408. pos++;
  409. reply = wpa_supplicant_global_ctrl_iface_process(global, pos,
  410. &reply_len);
  411. done:
  412. if (reply) {
  413. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  414. fromlen);
  415. os_free(reply);
  416. } else if (reply_len) {
  417. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  418. fromlen);
  419. }
  420. }
  421. struct ctrl_iface_global_priv *
  422. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  423. {
  424. struct ctrl_iface_global_priv *priv;
  425. struct sockaddr_in addr;
  426. priv = os_zalloc(sizeof(*priv));
  427. if (priv == NULL)
  428. return NULL;
  429. priv->sock = -1;
  430. os_get_random(priv->cookie, COOKIE_LEN);
  431. if (global->params.ctrl_interface == NULL)
  432. return priv;
  433. wpa_printf(MSG_DEBUG, "Global control interface '%s'",
  434. global->params.ctrl_interface);
  435. priv->sock = socket(PF_INET, SOCK_DGRAM, 0);
  436. if (priv->sock < 0) {
  437. perror("socket(PF_INET)");
  438. goto fail;
  439. }
  440. os_memset(&addr, 0, sizeof(addr));
  441. addr.sin_family = AF_INET;
  442. addr.sin_addr.s_addr = htonl((127 << 24) | 1);
  443. addr.sin_port = htons(WPA_GLOBAL_CTRL_IFACE_PORT);
  444. if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  445. perror("bind(AF_INET)");
  446. goto fail;
  447. }
  448. eloop_register_read_sock(priv->sock,
  449. wpa_supplicant_global_ctrl_iface_receive,
  450. global, priv);
  451. return priv;
  452. fail:
  453. if (priv->sock >= 0)
  454. close(priv->sock);
  455. os_free(priv);
  456. return NULL;
  457. }
  458. void
  459. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  460. {
  461. if (priv->sock >= 0) {
  462. eloop_unregister_read_sock(priv->sock);
  463. close(priv->sock);
  464. }
  465. os_free(priv);
  466. }