ctrl_iface.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2013, 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 "utils/includes.h"
  9. #ifndef CONFIG_NATIVE_WINDOWS
  10. #include <sys/un.h>
  11. #include <sys/stat.h>
  12. #include <stddef.h>
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "common/version.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "drivers/driver.h"
  18. #include "radius/radius_client.h"
  19. #include "radius/radius_server.h"
  20. #include "ap/hostapd.h"
  21. #include "ap/ap_config.h"
  22. #include "ap/ieee802_1x.h"
  23. #include "ap/wpa_auth.h"
  24. #include "ap/ieee802_11.h"
  25. #include "ap/sta_info.h"
  26. #include "ap/wps_hostapd.h"
  27. #include "ap/ctrl_iface_ap.h"
  28. #include "ap/ap_drv_ops.h"
  29. #include "ap/wnm_ap.h"
  30. #include "ap/wpa_auth.h"
  31. #include "wps/wps_defs.h"
  32. #include "wps/wps.h"
  33. #include "config_file.h"
  34. #include "ctrl_iface.h"
  35. struct wpa_ctrl_dst {
  36. struct wpa_ctrl_dst *next;
  37. struct sockaddr_un addr;
  38. socklen_t addrlen;
  39. int debug_level;
  40. int errors;
  41. };
  42. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  43. const char *buf, size_t len);
  44. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  45. struct sockaddr_un *from,
  46. socklen_t fromlen)
  47. {
  48. struct wpa_ctrl_dst *dst;
  49. dst = os_zalloc(sizeof(*dst));
  50. if (dst == NULL)
  51. return -1;
  52. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  53. dst->addrlen = fromlen;
  54. dst->debug_level = MSG_INFO;
  55. dst->next = hapd->ctrl_dst;
  56. hapd->ctrl_dst = dst;
  57. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  58. (u8 *) from->sun_path,
  59. fromlen - offsetof(struct sockaddr_un, sun_path));
  60. return 0;
  61. }
  62. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  63. struct sockaddr_un *from,
  64. socklen_t fromlen)
  65. {
  66. struct wpa_ctrl_dst *dst, *prev = NULL;
  67. dst = hapd->ctrl_dst;
  68. while (dst) {
  69. if (fromlen == dst->addrlen &&
  70. os_memcmp(from->sun_path, dst->addr.sun_path,
  71. fromlen - offsetof(struct sockaddr_un, sun_path))
  72. == 0) {
  73. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  74. (u8 *) from->sun_path,
  75. fromlen -
  76. offsetof(struct sockaddr_un, sun_path));
  77. if (prev == NULL)
  78. hapd->ctrl_dst = dst->next;
  79. else
  80. prev->next = dst->next;
  81. os_free(dst);
  82. return 0;
  83. }
  84. prev = dst;
  85. dst = dst->next;
  86. }
  87. return -1;
  88. }
  89. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  90. struct sockaddr_un *from,
  91. socklen_t fromlen,
  92. char *level)
  93. {
  94. struct wpa_ctrl_dst *dst;
  95. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  96. dst = hapd->ctrl_dst;
  97. while (dst) {
  98. if (fromlen == dst->addrlen &&
  99. os_memcmp(from->sun_path, dst->addr.sun_path,
  100. fromlen - offsetof(struct sockaddr_un, sun_path))
  101. == 0) {
  102. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  103. "level", (u8 *) from->sun_path, fromlen -
  104. offsetof(struct sockaddr_un, sun_path));
  105. dst->debug_level = atoi(level);
  106. return 0;
  107. }
  108. dst = dst->next;
  109. }
  110. return -1;
  111. }
  112. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  113. const char *txtaddr)
  114. {
  115. u8 addr[ETH_ALEN];
  116. struct sta_info *sta;
  117. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  118. if (hwaddr_aton(txtaddr, addr))
  119. return -1;
  120. sta = ap_get_sta(hapd, addr);
  121. if (sta)
  122. return 0;
  123. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  124. "notification", MAC2STR(addr));
  125. sta = ap_sta_add(hapd, addr);
  126. if (sta == NULL)
  127. return -1;
  128. hostapd_new_assoc_sta(hapd, sta, 0);
  129. return 0;
  130. }
  131. #ifdef CONFIG_IEEE80211W
  132. #ifdef NEED_AP_MLME
  133. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  134. const char *txtaddr)
  135. {
  136. u8 addr[ETH_ALEN];
  137. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  138. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  139. if (hwaddr_aton(txtaddr, addr) ||
  140. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  141. return -1;
  142. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  143. return 0;
  144. }
  145. #endif /* NEED_AP_MLME */
  146. #endif /* CONFIG_IEEE80211W */
  147. #ifdef CONFIG_WPS
  148. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  149. {
  150. char *pin = os_strchr(txt, ' ');
  151. char *timeout_txt;
  152. int timeout;
  153. u8 addr_buf[ETH_ALEN], *addr = NULL;
  154. char *pos;
  155. if (pin == NULL)
  156. return -1;
  157. *pin++ = '\0';
  158. timeout_txt = os_strchr(pin, ' ');
  159. if (timeout_txt) {
  160. *timeout_txt++ = '\0';
  161. timeout = atoi(timeout_txt);
  162. pos = os_strchr(timeout_txt, ' ');
  163. if (pos) {
  164. *pos++ = '\0';
  165. if (hwaddr_aton(pos, addr_buf) == 0)
  166. addr = addr_buf;
  167. }
  168. } else
  169. timeout = 0;
  170. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  171. }
  172. static int hostapd_ctrl_iface_wps_check_pin(
  173. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  174. {
  175. char pin[9];
  176. size_t len;
  177. char *pos;
  178. int ret;
  179. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  180. (u8 *) cmd, os_strlen(cmd));
  181. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  182. if (*pos < '0' || *pos > '9')
  183. continue;
  184. pin[len++] = *pos;
  185. if (len == 9) {
  186. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  187. return -1;
  188. }
  189. }
  190. if (len != 4 && len != 8) {
  191. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  192. return -1;
  193. }
  194. pin[len] = '\0';
  195. if (len == 8) {
  196. unsigned int pin_val;
  197. pin_val = atoi(pin);
  198. if (!wps_pin_valid(pin_val)) {
  199. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  200. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  201. if (ret < 0 || (size_t) ret >= buflen)
  202. return -1;
  203. return ret;
  204. }
  205. }
  206. ret = os_snprintf(buf, buflen, "%s", pin);
  207. if (ret < 0 || (size_t) ret >= buflen)
  208. return -1;
  209. return ret;
  210. }
  211. #ifdef CONFIG_WPS_NFC
  212. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  213. char *pos)
  214. {
  215. size_t len;
  216. struct wpabuf *buf;
  217. int ret;
  218. len = os_strlen(pos);
  219. if (len & 0x01)
  220. return -1;
  221. len /= 2;
  222. buf = wpabuf_alloc(len);
  223. if (buf == NULL)
  224. return -1;
  225. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  226. wpabuf_free(buf);
  227. return -1;
  228. }
  229. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  230. wpabuf_free(buf);
  231. return ret;
  232. }
  233. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  234. char *cmd, char *reply,
  235. size_t max_len)
  236. {
  237. int ndef;
  238. struct wpabuf *buf;
  239. int res;
  240. if (os_strcmp(cmd, "WPS") == 0)
  241. ndef = 0;
  242. else if (os_strcmp(cmd, "NDEF") == 0)
  243. ndef = 1;
  244. else
  245. return -1;
  246. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  247. if (buf == NULL)
  248. return -1;
  249. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  250. wpabuf_len(buf));
  251. reply[res++] = '\n';
  252. reply[res] = '\0';
  253. wpabuf_free(buf);
  254. return res;
  255. }
  256. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  257. char *reply, size_t max_len,
  258. int ndef)
  259. {
  260. struct wpabuf *buf;
  261. int res;
  262. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  263. if (buf == NULL)
  264. return -1;
  265. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  266. wpabuf_len(buf));
  267. reply[res++] = '\n';
  268. reply[res] = '\0';
  269. wpabuf_free(buf);
  270. return res;
  271. }
  272. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  273. char *cmd, char *reply,
  274. size_t max_len)
  275. {
  276. if (os_strcmp(cmd, "WPS") == 0)
  277. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  278. max_len, 0);
  279. if (os_strcmp(cmd, "NDEF") == 0)
  280. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  281. max_len, 1);
  282. if (os_strcmp(cmd, "enable") == 0)
  283. return hostapd_wps_nfc_token_enable(hapd);
  284. if (os_strcmp(cmd, "disable") == 0) {
  285. hostapd_wps_nfc_token_disable(hapd);
  286. return 0;
  287. }
  288. return -1;
  289. }
  290. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  291. char *cmd, char *reply,
  292. size_t max_len)
  293. {
  294. struct wpabuf *buf;
  295. int res;
  296. char *pos;
  297. int ndef;
  298. pos = os_strchr(cmd, ' ');
  299. if (pos == NULL)
  300. return -1;
  301. *pos++ = '\0';
  302. if (os_strcmp(cmd, "WPS") == 0)
  303. ndef = 0;
  304. else if (os_strcmp(cmd, "NDEF") == 0)
  305. ndef = 1;
  306. else
  307. return -1;
  308. if (os_strcmp(pos, "WPS-CR") == 0)
  309. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  310. else
  311. buf = NULL;
  312. if (buf == NULL)
  313. return -1;
  314. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  315. wpabuf_len(buf));
  316. reply[res++] = '\n';
  317. reply[res] = '\0';
  318. wpabuf_free(buf);
  319. return res;
  320. }
  321. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  322. char *cmd)
  323. {
  324. /*
  325. * Since NFC connection handover provided full WPS Credential, there is
  326. * no need for additional operations within hostapd. Just report this in
  327. * debug log.
  328. */
  329. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
  330. return 0;
  331. }
  332. #endif /* CONFIG_WPS_NFC */
  333. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  334. char *buf, size_t buflen)
  335. {
  336. int timeout = 300;
  337. char *pos;
  338. const char *pin_txt;
  339. pos = os_strchr(txt, ' ');
  340. if (pos)
  341. *pos++ = '\0';
  342. if (os_strcmp(txt, "disable") == 0) {
  343. hostapd_wps_ap_pin_disable(hapd);
  344. return os_snprintf(buf, buflen, "OK\n");
  345. }
  346. if (os_strcmp(txt, "random") == 0) {
  347. if (pos)
  348. timeout = atoi(pos);
  349. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  350. if (pin_txt == NULL)
  351. return -1;
  352. return os_snprintf(buf, buflen, "%s", pin_txt);
  353. }
  354. if (os_strcmp(txt, "get") == 0) {
  355. pin_txt = hostapd_wps_ap_pin_get(hapd);
  356. if (pin_txt == NULL)
  357. return -1;
  358. return os_snprintf(buf, buflen, "%s", pin_txt);
  359. }
  360. if (os_strcmp(txt, "set") == 0) {
  361. char *pin;
  362. if (pos == NULL)
  363. return -1;
  364. pin = pos;
  365. pos = os_strchr(pos, ' ');
  366. if (pos) {
  367. *pos++ = '\0';
  368. timeout = atoi(pos);
  369. }
  370. if (os_strlen(pin) > buflen)
  371. return -1;
  372. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  373. return -1;
  374. return os_snprintf(buf, buflen, "%s", pin);
  375. }
  376. return -1;
  377. }
  378. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  379. {
  380. char *pos;
  381. char *ssid, *auth, *encr = NULL, *key = NULL;
  382. ssid = txt;
  383. pos = os_strchr(txt, ' ');
  384. if (!pos)
  385. return -1;
  386. *pos++ = '\0';
  387. auth = pos;
  388. pos = os_strchr(pos, ' ');
  389. if (pos) {
  390. *pos++ = '\0';
  391. encr = pos;
  392. pos = os_strchr(pos, ' ');
  393. if (pos) {
  394. *pos++ = '\0';
  395. key = pos;
  396. }
  397. }
  398. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  399. }
  400. static const char * pbc_status_str(enum pbc_status status)
  401. {
  402. switch (status) {
  403. case WPS_PBC_STATUS_DISABLE:
  404. return "Disabled";
  405. case WPS_PBC_STATUS_ACTIVE:
  406. return "Active";
  407. case WPS_PBC_STATUS_TIMEOUT:
  408. return "Timed-out";
  409. case WPS_PBC_STATUS_OVERLAP:
  410. return "Overlap";
  411. default:
  412. return "Unknown";
  413. }
  414. }
  415. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  416. char *buf, size_t buflen)
  417. {
  418. int ret;
  419. char *pos, *end;
  420. pos = buf;
  421. end = buf + buflen;
  422. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  423. pbc_status_str(hapd->wps_stats.pbc_status));
  424. if (ret < 0 || ret >= end - pos)
  425. return pos - buf;
  426. pos += ret;
  427. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  428. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  429. "Success":
  430. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  431. "Failed" : "None")));
  432. if (ret < 0 || ret >= end - pos)
  433. return pos - buf;
  434. pos += ret;
  435. /* If status == Failure - Add possible Reasons */
  436. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  437. hapd->wps_stats.failure_reason > 0) {
  438. ret = os_snprintf(pos, end - pos,
  439. "Failure Reason: %s\n",
  440. wps_ei_str(hapd->wps_stats.failure_reason));
  441. if (ret < 0 || ret >= end - pos)
  442. return pos - buf;
  443. pos += ret;
  444. }
  445. if (hapd->wps_stats.status) {
  446. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  447. MAC2STR(hapd->wps_stats.peer_addr));
  448. if (ret < 0 || ret >= end - pos)
  449. return pos - buf;
  450. pos += ret;
  451. }
  452. return pos - buf;
  453. }
  454. #endif /* CONFIG_WPS */
  455. #ifdef CONFIG_INTERWORKING
  456. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  457. const char *cmd)
  458. {
  459. u8 qos_map_set[16 + 2 * 21], count = 0;
  460. const char *pos = cmd;
  461. int val, ret;
  462. for (;;) {
  463. if (count == sizeof(qos_map_set)) {
  464. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  465. return -1;
  466. }
  467. val = atoi(pos);
  468. if (val < 0 || val > 255) {
  469. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  470. return -1;
  471. }
  472. qos_map_set[count++] = val;
  473. pos = os_strchr(pos, ',');
  474. if (!pos)
  475. break;
  476. pos++;
  477. }
  478. if (count < 16 || count & 1) {
  479. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  480. return -1;
  481. }
  482. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  483. if (ret) {
  484. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  485. return -1;
  486. }
  487. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  488. hapd->conf->qos_map_set_len = count;
  489. return 0;
  490. }
  491. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  492. const char *cmd)
  493. {
  494. u8 addr[ETH_ALEN];
  495. struct sta_info *sta;
  496. struct wpabuf *buf;
  497. u8 *qos_map_set = hapd->conf->qos_map_set;
  498. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  499. int ret;
  500. if (!qos_map_set_len) {
  501. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  502. return -1;
  503. }
  504. if (hwaddr_aton(cmd, addr))
  505. return -1;
  506. sta = ap_get_sta(hapd, addr);
  507. if (sta == NULL) {
  508. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  509. "for QoS Map Configuration message",
  510. MAC2STR(addr));
  511. return -1;
  512. }
  513. if (!sta->qos_map_enabled) {
  514. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  515. "support for QoS Map", MAC2STR(addr));
  516. return -1;
  517. }
  518. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  519. if (buf == NULL)
  520. return -1;
  521. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  522. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  523. /* QoS Map Set Element */
  524. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  525. wpabuf_put_u8(buf, qos_map_set_len);
  526. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  527. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  528. wpabuf_head(buf), wpabuf_len(buf));
  529. wpabuf_free(buf);
  530. return ret;
  531. }
  532. #endif /* CONFIG_INTERWORKING */
  533. #ifdef CONFIG_WNM
  534. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  535. const char *cmd)
  536. {
  537. u8 addr[ETH_ALEN];
  538. int disassoc_timer;
  539. struct sta_info *sta;
  540. if (hwaddr_aton(cmd, addr))
  541. return -1;
  542. if (cmd[17] != ' ')
  543. return -1;
  544. disassoc_timer = atoi(cmd + 17);
  545. sta = ap_get_sta(hapd, addr);
  546. if (sta == NULL) {
  547. wpa_printf(MSG_DEBUG, "Station " MACSTR
  548. " not found for disassociation imminent message",
  549. MAC2STR(addr));
  550. return -1;
  551. }
  552. return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
  553. }
  554. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  555. const char *cmd)
  556. {
  557. u8 addr[ETH_ALEN];
  558. const char *url, *timerstr;
  559. int disassoc_timer;
  560. struct sta_info *sta;
  561. if (hwaddr_aton(cmd, addr))
  562. return -1;
  563. sta = ap_get_sta(hapd, addr);
  564. if (sta == NULL) {
  565. wpa_printf(MSG_DEBUG, "Station " MACSTR
  566. " not found for ESS disassociation imminent message",
  567. MAC2STR(addr));
  568. return -1;
  569. }
  570. timerstr = cmd + 17;
  571. if (*timerstr != ' ')
  572. return -1;
  573. timerstr++;
  574. disassoc_timer = atoi(timerstr);
  575. if (disassoc_timer < 0 || disassoc_timer > 65535)
  576. return -1;
  577. url = os_strchr(timerstr, ' ');
  578. if (url == NULL)
  579. return -1;
  580. url++;
  581. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  582. }
  583. #endif /* CONFIG_WNM */
  584. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  585. char *buf, size_t buflen)
  586. {
  587. int ret;
  588. char *pos, *end;
  589. pos = buf;
  590. end = buf + buflen;
  591. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  592. "ssid=%s\n",
  593. MAC2STR(hapd->own_addr),
  594. wpa_ssid_txt(hapd->conf->ssid.ssid,
  595. hapd->conf->ssid.ssid_len));
  596. if (ret < 0 || ret >= end - pos)
  597. return pos - buf;
  598. pos += ret;
  599. #ifdef CONFIG_WPS
  600. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  601. hapd->conf->wps_state == 0 ? "disabled" :
  602. (hapd->conf->wps_state == 1 ? "not configured" :
  603. "configured"));
  604. if (ret < 0 || ret >= end - pos)
  605. return pos - buf;
  606. pos += ret;
  607. if (hapd->conf->wps_state && hapd->conf->wpa &&
  608. hapd->conf->ssid.wpa_passphrase) {
  609. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  610. hapd->conf->ssid.wpa_passphrase);
  611. if (ret < 0 || ret >= end - pos)
  612. return pos - buf;
  613. pos += ret;
  614. }
  615. if (hapd->conf->wps_state && hapd->conf->wpa &&
  616. hapd->conf->ssid.wpa_psk &&
  617. hapd->conf->ssid.wpa_psk->group) {
  618. char hex[PMK_LEN * 2 + 1];
  619. wpa_snprintf_hex(hex, sizeof(hex),
  620. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  621. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  622. if (ret < 0 || ret >= end - pos)
  623. return pos - buf;
  624. pos += ret;
  625. }
  626. #endif /* CONFIG_WPS */
  627. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  628. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  629. if (ret < 0 || ret >= end - pos)
  630. return pos - buf;
  631. pos += ret;
  632. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  633. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  634. if (ret < 0 || ret >= end - pos)
  635. return pos - buf;
  636. pos += ret;
  637. }
  638. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  639. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  640. if (ret < 0 || ret >= end - pos)
  641. return pos - buf;
  642. pos += ret;
  643. }
  644. #ifdef CONFIG_IEEE80211R
  645. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  646. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  647. if (ret < 0 || ret >= end - pos)
  648. return pos - buf;
  649. pos += ret;
  650. }
  651. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  652. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  653. if (ret < 0 || ret >= end - pos)
  654. return pos - buf;
  655. pos += ret;
  656. }
  657. #endif /* CONFIG_IEEE80211R */
  658. #ifdef CONFIG_IEEE80211W
  659. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  660. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  661. if (ret < 0 || ret >= end - pos)
  662. return pos - buf;
  663. pos += ret;
  664. }
  665. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  666. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  667. if (ret < 0 || ret >= end - pos)
  668. return pos - buf;
  669. pos += ret;
  670. }
  671. #endif /* CONFIG_IEEE80211W */
  672. ret = os_snprintf(pos, end - pos, "\n");
  673. if (ret < 0 || ret >= end - pos)
  674. return pos - buf;
  675. pos += ret;
  676. }
  677. if (hapd->conf->wpa) {
  678. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  679. wpa_cipher_txt(hapd->conf->wpa_group));
  680. if (ret < 0 || ret >= end - pos)
  681. return pos - buf;
  682. pos += ret;
  683. }
  684. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  685. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  686. if (ret < 0 || ret >= end - pos)
  687. return pos - buf;
  688. pos += ret;
  689. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  690. " ");
  691. if (ret < 0)
  692. return pos - buf;
  693. pos += ret;
  694. ret = os_snprintf(pos, end - pos, "\n");
  695. if (ret < 0 || ret >= end - pos)
  696. return pos - buf;
  697. pos += ret;
  698. }
  699. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  700. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  701. if (ret < 0 || ret >= end - pos)
  702. return pos - buf;
  703. pos += ret;
  704. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  705. " ");
  706. if (ret < 0)
  707. return pos - buf;
  708. pos += ret;
  709. ret = os_snprintf(pos, end - pos, "\n");
  710. if (ret < 0 || ret >= end - pos)
  711. return pos - buf;
  712. pos += ret;
  713. }
  714. return pos - buf;
  715. }
  716. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  717. {
  718. char *value;
  719. int ret = 0;
  720. value = os_strchr(cmd, ' ');
  721. if (value == NULL)
  722. return -1;
  723. *value++ = '\0';
  724. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  725. if (0) {
  726. #ifdef CONFIG_WPS_TESTING
  727. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  728. long int val;
  729. val = strtol(value, NULL, 0);
  730. if (val < 0 || val > 0xff) {
  731. ret = -1;
  732. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  733. "wps_version_number %ld", val);
  734. } else {
  735. wps_version_number = val;
  736. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  737. "version %u.%u",
  738. (wps_version_number & 0xf0) >> 4,
  739. wps_version_number & 0x0f);
  740. hostapd_wps_update_ie(hapd);
  741. }
  742. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  743. wps_testing_dummy_cred = atoi(value);
  744. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  745. wps_testing_dummy_cred);
  746. #endif /* CONFIG_WPS_TESTING */
  747. #ifdef CONFIG_INTERWORKING
  748. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  749. int val = atoi(value);
  750. if (val <= 0)
  751. ret = -1;
  752. else
  753. hapd->gas_frag_limit = val;
  754. #endif /* CONFIG_INTERWORKING */
  755. #ifdef CONFIG_TESTING_OPTIONS
  756. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  757. hapd->ext_mgmt_frame_handling = atoi(value);
  758. #endif /* CONFIG_TESTING_OPTIONS */
  759. } else {
  760. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  761. }
  762. return ret;
  763. }
  764. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  765. char *buf, size_t buflen)
  766. {
  767. int res;
  768. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  769. if (os_strcmp(cmd, "version") == 0) {
  770. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  771. if (res < 0 || (unsigned int) res >= buflen)
  772. return -1;
  773. return res;
  774. }
  775. return -1;
  776. }
  777. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  778. {
  779. if (hostapd_enable_iface(iface) < 0) {
  780. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  781. return -1;
  782. }
  783. return 0;
  784. }
  785. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  786. {
  787. if (hostapd_reload_iface(iface) < 0) {
  788. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  789. return -1;
  790. }
  791. return 0;
  792. }
  793. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  794. {
  795. if (hostapd_disable_iface(iface) < 0) {
  796. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  797. return -1;
  798. }
  799. return 0;
  800. }
  801. #ifdef CONFIG_TESTING_OPTIONS
  802. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  803. {
  804. union wpa_event_data data;
  805. char *pos, *param;
  806. enum wpa_event_type event;
  807. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  808. os_memset(&data, 0, sizeof(data));
  809. param = os_strchr(cmd, ' ');
  810. if (param == NULL)
  811. return -1;
  812. *param++ = '\0';
  813. if (os_strcmp(cmd, "DETECTED") == 0)
  814. event = EVENT_DFS_RADAR_DETECTED;
  815. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  816. event = EVENT_DFS_CAC_FINISHED;
  817. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  818. event = EVENT_DFS_CAC_ABORTED;
  819. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  820. event = EVENT_DFS_NOP_FINISHED;
  821. else {
  822. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  823. cmd);
  824. return -1;
  825. }
  826. pos = os_strstr(param, "freq=");
  827. if (pos)
  828. data.dfs_event.freq = atoi(pos + 5);
  829. pos = os_strstr(param, "ht_enabled=1");
  830. if (pos)
  831. data.dfs_event.ht_enabled = 1;
  832. pos = os_strstr(param, "chan_offset=");
  833. if (pos)
  834. data.dfs_event.chan_offset = atoi(pos + 12);
  835. pos = os_strstr(param, "chan_width=");
  836. if (pos)
  837. data.dfs_event.chan_width = atoi(pos + 11);
  838. pos = os_strstr(param, "cf1=");
  839. if (pos)
  840. data.dfs_event.cf1 = atoi(pos + 4);
  841. pos = os_strstr(param, "cf2=");
  842. if (pos)
  843. data.dfs_event.cf2 = atoi(pos + 4);
  844. wpa_supplicant_event(hapd, event, &data);
  845. return 0;
  846. }
  847. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  848. {
  849. size_t len;
  850. u8 *buf;
  851. int res;
  852. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  853. len = os_strlen(cmd);
  854. if (len & 1)
  855. return -1;
  856. len /= 2;
  857. buf = os_malloc(len);
  858. if (buf == NULL)
  859. return -1;
  860. if (hexstr2bin(cmd, buf, len) < 0) {
  861. os_free(buf);
  862. return -1;
  863. }
  864. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  865. os_free(buf);
  866. return res;
  867. }
  868. #endif /* CONFIG_TESTING_OPTIONS */
  869. static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
  870. {
  871. #ifdef NEED_AP_MLME
  872. struct csa_settings settings;
  873. int ret = hostapd_parse_csa_settings(pos, &settings);
  874. if (ret)
  875. return ret;
  876. return hostapd_switch_channel(hapd, &settings);
  877. #else /* NEED_AP_MLME */
  878. return -1;
  879. #endif /* NEED_AP_MLME */
  880. }
  881. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  882. int reply_size, const char *param)
  883. {
  884. #ifdef RADIUS_SERVER
  885. if (os_strcmp(param, "radius_server") == 0) {
  886. return radius_server_get_mib(hapd->radius_srv, reply,
  887. reply_size);
  888. }
  889. #endif /* RADIUS_SERVER */
  890. return -1;
  891. }
  892. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  893. void *sock_ctx)
  894. {
  895. struct hostapd_data *hapd = eloop_ctx;
  896. char buf[256];
  897. int res;
  898. struct sockaddr_un from;
  899. socklen_t fromlen = sizeof(from);
  900. char *reply;
  901. const int reply_size = 4096;
  902. int reply_len;
  903. int level = MSG_DEBUG;
  904. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  905. (struct sockaddr *) &from, &fromlen);
  906. if (res < 0) {
  907. perror("recvfrom(ctrl_iface)");
  908. return;
  909. }
  910. buf[res] = '\0';
  911. if (os_strcmp(buf, "PING") == 0)
  912. level = MSG_EXCESSIVE;
  913. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  914. reply = os_malloc(reply_size);
  915. if (reply == NULL) {
  916. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  917. fromlen);
  918. return;
  919. }
  920. os_memcpy(reply, "OK\n", 3);
  921. reply_len = 3;
  922. if (os_strcmp(buf, "PING") == 0) {
  923. os_memcpy(reply, "PONG\n", 5);
  924. reply_len = 5;
  925. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  926. if (wpa_debug_reopen_file() < 0)
  927. reply_len = -1;
  928. } else if (os_strcmp(buf, "STATUS") == 0) {
  929. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  930. reply_size);
  931. } else if (os_strcmp(buf, "MIB") == 0) {
  932. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  933. if (reply_len >= 0) {
  934. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  935. reply_size - reply_len);
  936. if (res < 0)
  937. reply_len = -1;
  938. else
  939. reply_len += res;
  940. }
  941. if (reply_len >= 0) {
  942. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  943. reply_size - reply_len);
  944. if (res < 0)
  945. reply_len = -1;
  946. else
  947. reply_len += res;
  948. }
  949. #ifndef CONFIG_NO_RADIUS
  950. if (reply_len >= 0) {
  951. res = radius_client_get_mib(hapd->radius,
  952. reply + reply_len,
  953. reply_size - reply_len);
  954. if (res < 0)
  955. reply_len = -1;
  956. else
  957. reply_len += res;
  958. }
  959. #endif /* CONFIG_NO_RADIUS */
  960. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  961. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  962. buf + 4);
  963. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  964. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  965. reply_size);
  966. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  967. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  968. reply_size);
  969. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  970. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  971. reply_size);
  972. } else if (os_strcmp(buf, "ATTACH") == 0) {
  973. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  974. reply_len = -1;
  975. } else if (os_strcmp(buf, "DETACH") == 0) {
  976. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  977. reply_len = -1;
  978. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  979. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  980. buf + 6))
  981. reply_len = -1;
  982. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  983. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  984. reply_len = -1;
  985. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  986. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  987. reply_len = -1;
  988. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  989. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  990. reply_len = -1;
  991. #ifdef CONFIG_IEEE80211W
  992. #ifdef NEED_AP_MLME
  993. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  994. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  995. reply_len = -1;
  996. #endif /* NEED_AP_MLME */
  997. #endif /* CONFIG_IEEE80211W */
  998. #ifdef CONFIG_WPS
  999. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1000. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1001. reply_len = -1;
  1002. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1003. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1004. hapd, buf + 14, reply, reply_size);
  1005. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1006. if (hostapd_wps_button_pushed(hapd, NULL))
  1007. reply_len = -1;
  1008. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1009. if (hostapd_wps_cancel(hapd))
  1010. reply_len = -1;
  1011. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1012. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1013. reply, reply_size);
  1014. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1015. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1016. reply_len = -1;
  1017. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1018. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1019. reply_size);
  1020. #ifdef CONFIG_WPS_NFC
  1021. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1022. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1023. reply_len = -1;
  1024. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1025. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1026. hapd, buf + 21, reply, reply_size);
  1027. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1028. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1029. hapd, buf + 14, reply, reply_size);
  1030. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1031. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1032. hapd, buf + 21, reply, reply_size);
  1033. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1034. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1035. reply_len = -1;
  1036. #endif /* CONFIG_WPS_NFC */
  1037. #endif /* CONFIG_WPS */
  1038. #ifdef CONFIG_INTERWORKING
  1039. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1040. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1041. reply_len = -1;
  1042. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1043. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1044. reply_len = -1;
  1045. #endif /* CONFIG_INTERWORKING */
  1046. #ifdef CONFIG_WNM
  1047. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1048. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1049. reply_len = -1;
  1050. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1051. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1052. reply_len = -1;
  1053. #endif /* CONFIG_WNM */
  1054. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1055. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1056. reply_size);
  1057. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1058. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1059. reply_len = -1;
  1060. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1061. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1062. reply_size);
  1063. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1064. if (hostapd_ctrl_iface_enable(hapd->iface))
  1065. reply_len = -1;
  1066. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1067. if (hostapd_ctrl_iface_reload(hapd->iface))
  1068. reply_len = -1;
  1069. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1070. if (hostapd_ctrl_iface_disable(hapd->iface))
  1071. reply_len = -1;
  1072. #ifdef CONFIG_TESTING_OPTIONS
  1073. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1074. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1075. reply_len = -1;
  1076. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1077. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1078. reply_len = -1;
  1079. #endif /* CONFIG_TESTING_OPTIONS */
  1080. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1081. if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
  1082. reply_len = -1;
  1083. } else {
  1084. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1085. reply_len = 16;
  1086. }
  1087. if (reply_len < 0) {
  1088. os_memcpy(reply, "FAIL\n", 5);
  1089. reply_len = 5;
  1090. }
  1091. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1092. os_free(reply);
  1093. }
  1094. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1095. {
  1096. char *buf;
  1097. size_t len;
  1098. if (hapd->conf->ctrl_interface == NULL)
  1099. return NULL;
  1100. len = os_strlen(hapd->conf->ctrl_interface) +
  1101. os_strlen(hapd->conf->iface) + 2;
  1102. buf = os_malloc(len);
  1103. if (buf == NULL)
  1104. return NULL;
  1105. os_snprintf(buf, len, "%s/%s",
  1106. hapd->conf->ctrl_interface, hapd->conf->iface);
  1107. buf[len - 1] = '\0';
  1108. return buf;
  1109. }
  1110. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  1111. const char *txt, size_t len)
  1112. {
  1113. struct hostapd_data *hapd = ctx;
  1114. if (hapd == NULL)
  1115. return;
  1116. hostapd_ctrl_iface_send(hapd, level, txt, len);
  1117. }
  1118. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1119. {
  1120. struct sockaddr_un addr;
  1121. int s = -1;
  1122. char *fname = NULL;
  1123. if (hapd->ctrl_sock > -1) {
  1124. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1125. return 0;
  1126. }
  1127. if (hapd->conf->ctrl_interface == NULL)
  1128. return 0;
  1129. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1130. if (errno == EEXIST) {
  1131. wpa_printf(MSG_DEBUG, "Using existing control "
  1132. "interface directory.");
  1133. } else {
  1134. perror("mkdir[ctrl_interface]");
  1135. goto fail;
  1136. }
  1137. }
  1138. if (hapd->conf->ctrl_interface_gid_set &&
  1139. chown(hapd->conf->ctrl_interface, -1,
  1140. hapd->conf->ctrl_interface_gid) < 0) {
  1141. perror("chown[ctrl_interface]");
  1142. return -1;
  1143. }
  1144. if (!hapd->conf->ctrl_interface_gid_set &&
  1145. hapd->iface->interfaces->ctrl_iface_group &&
  1146. chown(hapd->conf->ctrl_interface, -1,
  1147. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1148. perror("chown[ctrl_interface]");
  1149. return -1;
  1150. }
  1151. #ifdef ANDROID
  1152. /*
  1153. * Android is using umask 0077 which would leave the control interface
  1154. * directory without group access. This breaks things since Wi-Fi
  1155. * framework assumes that this directory can be accessed by other
  1156. * applications in the wifi group. Fix this by adding group access even
  1157. * if umask value would prevent this.
  1158. */
  1159. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1160. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1161. strerror(errno));
  1162. /* Try to continue anyway */
  1163. }
  1164. #endif /* ANDROID */
  1165. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1166. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1167. goto fail;
  1168. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1169. if (s < 0) {
  1170. perror("socket(PF_UNIX)");
  1171. goto fail;
  1172. }
  1173. os_memset(&addr, 0, sizeof(addr));
  1174. #ifdef __FreeBSD__
  1175. addr.sun_len = sizeof(addr);
  1176. #endif /* __FreeBSD__ */
  1177. addr.sun_family = AF_UNIX;
  1178. fname = hostapd_ctrl_iface_path(hapd);
  1179. if (fname == NULL)
  1180. goto fail;
  1181. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1182. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1183. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1184. strerror(errno));
  1185. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1186. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1187. " allow connections - assuming it was left"
  1188. "over from forced program termination");
  1189. if (unlink(fname) < 0) {
  1190. perror("unlink[ctrl_iface]");
  1191. wpa_printf(MSG_ERROR, "Could not unlink "
  1192. "existing ctrl_iface socket '%s'",
  1193. fname);
  1194. goto fail;
  1195. }
  1196. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1197. 0) {
  1198. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1199. goto fail;
  1200. }
  1201. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1202. "ctrl_iface socket '%s'", fname);
  1203. } else {
  1204. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1205. "be in use - cannot override it");
  1206. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1207. "not used anymore", fname);
  1208. os_free(fname);
  1209. fname = NULL;
  1210. goto fail;
  1211. }
  1212. }
  1213. if (hapd->conf->ctrl_interface_gid_set &&
  1214. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1215. perror("chown[ctrl_interface/ifname]");
  1216. goto fail;
  1217. }
  1218. if (!hapd->conf->ctrl_interface_gid_set &&
  1219. hapd->iface->interfaces->ctrl_iface_group &&
  1220. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1221. perror("chown[ctrl_interface/ifname]");
  1222. goto fail;
  1223. }
  1224. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1225. perror("chmod[ctrl_interface/ifname]");
  1226. goto fail;
  1227. }
  1228. os_free(fname);
  1229. hapd->ctrl_sock = s;
  1230. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1231. NULL);
  1232. hapd->msg_ctx = hapd;
  1233. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1234. return 0;
  1235. fail:
  1236. if (s >= 0)
  1237. close(s);
  1238. if (fname) {
  1239. unlink(fname);
  1240. os_free(fname);
  1241. }
  1242. return -1;
  1243. }
  1244. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1245. {
  1246. struct wpa_ctrl_dst *dst, *prev;
  1247. if (hapd->ctrl_sock > -1) {
  1248. char *fname;
  1249. eloop_unregister_read_sock(hapd->ctrl_sock);
  1250. close(hapd->ctrl_sock);
  1251. hapd->ctrl_sock = -1;
  1252. fname = hostapd_ctrl_iface_path(hapd);
  1253. if (fname)
  1254. unlink(fname);
  1255. os_free(fname);
  1256. if (hapd->conf->ctrl_interface &&
  1257. rmdir(hapd->conf->ctrl_interface) < 0) {
  1258. if (errno == ENOTEMPTY) {
  1259. wpa_printf(MSG_DEBUG, "Control interface "
  1260. "directory not empty - leaving it "
  1261. "behind");
  1262. } else {
  1263. wpa_printf(MSG_ERROR,
  1264. "rmdir[ctrl_interface=%s]: %s",
  1265. hapd->conf->ctrl_interface,
  1266. strerror(errno));
  1267. }
  1268. }
  1269. }
  1270. dst = hapd->ctrl_dst;
  1271. while (dst) {
  1272. prev = dst;
  1273. dst = dst->next;
  1274. os_free(prev);
  1275. }
  1276. }
  1277. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1278. char *buf)
  1279. {
  1280. if (hostapd_add_iface(interfaces, buf) < 0) {
  1281. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1282. return -1;
  1283. }
  1284. return 0;
  1285. }
  1286. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1287. char *buf)
  1288. {
  1289. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1290. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1291. return -1;
  1292. }
  1293. return 0;
  1294. }
  1295. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1296. void *sock_ctx)
  1297. {
  1298. void *interfaces = eloop_ctx;
  1299. char buf[256];
  1300. int res;
  1301. struct sockaddr_un from;
  1302. socklen_t fromlen = sizeof(from);
  1303. char reply[24];
  1304. int reply_len;
  1305. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1306. (struct sockaddr *) &from, &fromlen);
  1307. if (res < 0) {
  1308. perror("recvfrom(ctrl_iface)");
  1309. return;
  1310. }
  1311. buf[res] = '\0';
  1312. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  1313. os_memcpy(reply, "OK\n", 3);
  1314. reply_len = 3;
  1315. if (os_strcmp(buf, "PING") == 0) {
  1316. os_memcpy(reply, "PONG\n", 5);
  1317. reply_len = 5;
  1318. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1319. if (wpa_debug_reopen_file() < 0)
  1320. reply_len = -1;
  1321. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1322. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1323. reply_len = -1;
  1324. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1325. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1326. reply_len = -1;
  1327. } else {
  1328. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1329. "ignored");
  1330. reply_len = -1;
  1331. }
  1332. if (reply_len < 0) {
  1333. os_memcpy(reply, "FAIL\n", 5);
  1334. reply_len = 5;
  1335. }
  1336. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1337. }
  1338. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1339. {
  1340. char *buf;
  1341. size_t len;
  1342. if (interface->global_iface_path == NULL)
  1343. return NULL;
  1344. len = os_strlen(interface->global_iface_path) +
  1345. os_strlen(interface->global_iface_name) + 2;
  1346. buf = os_malloc(len);
  1347. if (buf == NULL)
  1348. return NULL;
  1349. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1350. interface->global_iface_name);
  1351. buf[len - 1] = '\0';
  1352. return buf;
  1353. }
  1354. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1355. {
  1356. struct sockaddr_un addr;
  1357. int s = -1;
  1358. char *fname = NULL;
  1359. if (interface->global_iface_path == NULL) {
  1360. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1361. return 0;
  1362. }
  1363. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1364. if (errno == EEXIST) {
  1365. wpa_printf(MSG_DEBUG, "Using existing control "
  1366. "interface directory.");
  1367. } else {
  1368. perror("mkdir[ctrl_interface]");
  1369. goto fail;
  1370. }
  1371. } else if (interface->ctrl_iface_group &&
  1372. chown(interface->global_iface_path, -1,
  1373. interface->ctrl_iface_group) < 0) {
  1374. perror("chown[ctrl_interface]");
  1375. goto fail;
  1376. }
  1377. if (os_strlen(interface->global_iface_path) + 1 +
  1378. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1379. goto fail;
  1380. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1381. if (s < 0) {
  1382. perror("socket(PF_UNIX)");
  1383. goto fail;
  1384. }
  1385. os_memset(&addr, 0, sizeof(addr));
  1386. #ifdef __FreeBSD__
  1387. addr.sun_len = sizeof(addr);
  1388. #endif /* __FreeBSD__ */
  1389. addr.sun_family = AF_UNIX;
  1390. fname = hostapd_global_ctrl_iface_path(interface);
  1391. if (fname == NULL)
  1392. goto fail;
  1393. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1394. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1395. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1396. strerror(errno));
  1397. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1398. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1399. " allow connections - assuming it was left"
  1400. "over from forced program termination");
  1401. if (unlink(fname) < 0) {
  1402. perror("unlink[ctrl_iface]");
  1403. wpa_printf(MSG_ERROR, "Could not unlink "
  1404. "existing ctrl_iface socket '%s'",
  1405. fname);
  1406. goto fail;
  1407. }
  1408. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1409. 0) {
  1410. perror("bind(PF_UNIX)");
  1411. goto fail;
  1412. }
  1413. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1414. "ctrl_iface socket '%s'", fname);
  1415. } else {
  1416. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1417. "be in use - cannot override it");
  1418. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1419. "not used anymore", fname);
  1420. os_free(fname);
  1421. fname = NULL;
  1422. goto fail;
  1423. }
  1424. }
  1425. if (interface->ctrl_iface_group &&
  1426. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1427. perror("chown[ctrl_interface]");
  1428. goto fail;
  1429. }
  1430. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1431. perror("chmod[ctrl_interface/ifname]");
  1432. goto fail;
  1433. }
  1434. os_free(fname);
  1435. interface->global_ctrl_sock = s;
  1436. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1437. interface, NULL);
  1438. return 0;
  1439. fail:
  1440. if (s >= 0)
  1441. close(s);
  1442. if (fname) {
  1443. unlink(fname);
  1444. os_free(fname);
  1445. }
  1446. return -1;
  1447. }
  1448. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1449. {
  1450. char *fname = NULL;
  1451. if (interfaces->global_ctrl_sock > -1) {
  1452. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1453. close(interfaces->global_ctrl_sock);
  1454. interfaces->global_ctrl_sock = -1;
  1455. fname = hostapd_global_ctrl_iface_path(interfaces);
  1456. if (fname) {
  1457. unlink(fname);
  1458. os_free(fname);
  1459. }
  1460. if (interfaces->global_iface_path &&
  1461. rmdir(interfaces->global_iface_path) < 0) {
  1462. if (errno == ENOTEMPTY) {
  1463. wpa_printf(MSG_DEBUG, "Control interface "
  1464. "directory not empty - leaving it "
  1465. "behind");
  1466. } else {
  1467. wpa_printf(MSG_ERROR,
  1468. "rmdir[ctrl_interface=%s]: %s",
  1469. interfaces->global_iface_path,
  1470. strerror(errno));
  1471. }
  1472. }
  1473. os_free(interfaces->global_iface_path);
  1474. interfaces->global_iface_path = NULL;
  1475. }
  1476. }
  1477. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1478. const char *buf, size_t len)
  1479. {
  1480. struct wpa_ctrl_dst *dst, *next;
  1481. struct msghdr msg;
  1482. int idx;
  1483. struct iovec io[2];
  1484. char levelstr[10];
  1485. dst = hapd->ctrl_dst;
  1486. if (hapd->ctrl_sock < 0 || dst == NULL)
  1487. return;
  1488. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1489. io[0].iov_base = levelstr;
  1490. io[0].iov_len = os_strlen(levelstr);
  1491. io[1].iov_base = (char *) buf;
  1492. io[1].iov_len = len;
  1493. os_memset(&msg, 0, sizeof(msg));
  1494. msg.msg_iov = io;
  1495. msg.msg_iovlen = 2;
  1496. idx = 0;
  1497. while (dst) {
  1498. next = dst->next;
  1499. if (level >= dst->debug_level) {
  1500. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1501. (u8 *) dst->addr.sun_path, dst->addrlen -
  1502. offsetof(struct sockaddr_un, sun_path));
  1503. msg.msg_name = &dst->addr;
  1504. msg.msg_namelen = dst->addrlen;
  1505. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1506. int _errno = errno;
  1507. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1508. "%d - %s",
  1509. idx, errno, strerror(errno));
  1510. dst->errors++;
  1511. if (dst->errors > 10 || _errno == ENOENT) {
  1512. hostapd_ctrl_iface_detach(
  1513. hapd, &dst->addr,
  1514. dst->addrlen);
  1515. }
  1516. } else
  1517. dst->errors = 0;
  1518. }
  1519. idx++;
  1520. dst = next;
  1521. }
  1522. }
  1523. #endif /* CONFIG_NATIVE_WINDOWS */