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 "ap/hostapd.h"
  20. #include "ap/ap_config.h"
  21. #include "ap/ieee802_1x.h"
  22. #include "ap/wpa_auth.h"
  23. #include "ap/ieee802_11.h"
  24. #include "ap/sta_info.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "ap/ap_drv_ops.h"
  28. #include "ap/wnm_ap.h"
  29. #include "ap/wpa_auth.h"
  30. #include "wps/wps_defs.h"
  31. #include "wps/wps.h"
  32. #include "config_file.h"
  33. #include "ctrl_iface.h"
  34. struct wpa_ctrl_dst {
  35. struct wpa_ctrl_dst *next;
  36. struct sockaddr_un addr;
  37. socklen_t addrlen;
  38. int debug_level;
  39. int errors;
  40. };
  41. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  42. const char *buf, size_t len);
  43. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  44. struct sockaddr_un *from,
  45. socklen_t fromlen)
  46. {
  47. struct wpa_ctrl_dst *dst;
  48. dst = os_zalloc(sizeof(*dst));
  49. if (dst == NULL)
  50. return -1;
  51. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  52. dst->addrlen = fromlen;
  53. dst->debug_level = MSG_INFO;
  54. dst->next = hapd->ctrl_dst;
  55. hapd->ctrl_dst = dst;
  56. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  57. (u8 *) from->sun_path,
  58. fromlen - offsetof(struct sockaddr_un, sun_path));
  59. return 0;
  60. }
  61. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  62. struct sockaddr_un *from,
  63. socklen_t fromlen)
  64. {
  65. struct wpa_ctrl_dst *dst, *prev = NULL;
  66. dst = hapd->ctrl_dst;
  67. while (dst) {
  68. if (fromlen == dst->addrlen &&
  69. os_memcmp(from->sun_path, dst->addr.sun_path,
  70. fromlen - offsetof(struct sockaddr_un, sun_path))
  71. == 0) {
  72. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  73. (u8 *) from->sun_path,
  74. fromlen -
  75. offsetof(struct sockaddr_un, sun_path));
  76. if (prev == NULL)
  77. hapd->ctrl_dst = dst->next;
  78. else
  79. prev->next = dst->next;
  80. os_free(dst);
  81. return 0;
  82. }
  83. prev = dst;
  84. dst = dst->next;
  85. }
  86. return -1;
  87. }
  88. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  89. struct sockaddr_un *from,
  90. socklen_t fromlen,
  91. char *level)
  92. {
  93. struct wpa_ctrl_dst *dst;
  94. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  95. dst = hapd->ctrl_dst;
  96. while (dst) {
  97. if (fromlen == dst->addrlen &&
  98. os_memcmp(from->sun_path, dst->addr.sun_path,
  99. fromlen - offsetof(struct sockaddr_un, sun_path))
  100. == 0) {
  101. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  102. "level", (u8 *) from->sun_path, fromlen -
  103. offsetof(struct sockaddr_un, sun_path));
  104. dst->debug_level = atoi(level);
  105. return 0;
  106. }
  107. dst = dst->next;
  108. }
  109. return -1;
  110. }
  111. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  112. const char *txtaddr)
  113. {
  114. u8 addr[ETH_ALEN];
  115. struct sta_info *sta;
  116. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  117. if (hwaddr_aton(txtaddr, addr))
  118. return -1;
  119. sta = ap_get_sta(hapd, addr);
  120. if (sta)
  121. return 0;
  122. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  123. "notification", MAC2STR(addr));
  124. sta = ap_sta_add(hapd, addr);
  125. if (sta == NULL)
  126. return -1;
  127. hostapd_new_assoc_sta(hapd, sta, 0);
  128. return 0;
  129. }
  130. #ifdef CONFIG_IEEE80211W
  131. #ifdef NEED_AP_MLME
  132. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  133. const char *txtaddr)
  134. {
  135. u8 addr[ETH_ALEN];
  136. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  137. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  138. if (hwaddr_aton(txtaddr, addr) ||
  139. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  140. return -1;
  141. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  142. return 0;
  143. }
  144. #endif /* NEED_AP_MLME */
  145. #endif /* CONFIG_IEEE80211W */
  146. #ifdef CONFIG_WPS
  147. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  148. {
  149. char *pin = os_strchr(txt, ' ');
  150. char *timeout_txt;
  151. int timeout;
  152. u8 addr_buf[ETH_ALEN], *addr = NULL;
  153. char *pos;
  154. if (pin == NULL)
  155. return -1;
  156. *pin++ = '\0';
  157. timeout_txt = os_strchr(pin, ' ');
  158. if (timeout_txt) {
  159. *timeout_txt++ = '\0';
  160. timeout = atoi(timeout_txt);
  161. pos = os_strchr(timeout_txt, ' ');
  162. if (pos) {
  163. *pos++ = '\0';
  164. if (hwaddr_aton(pos, addr_buf) == 0)
  165. addr = addr_buf;
  166. }
  167. } else
  168. timeout = 0;
  169. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  170. }
  171. static int hostapd_ctrl_iface_wps_check_pin(
  172. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  173. {
  174. char pin[9];
  175. size_t len;
  176. char *pos;
  177. int ret;
  178. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  179. (u8 *) cmd, os_strlen(cmd));
  180. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  181. if (*pos < '0' || *pos > '9')
  182. continue;
  183. pin[len++] = *pos;
  184. if (len == 9) {
  185. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  186. return -1;
  187. }
  188. }
  189. if (len != 4 && len != 8) {
  190. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  191. return -1;
  192. }
  193. pin[len] = '\0';
  194. if (len == 8) {
  195. unsigned int pin_val;
  196. pin_val = atoi(pin);
  197. if (!wps_pin_valid(pin_val)) {
  198. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  199. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  200. if (ret < 0 || (size_t) ret >= buflen)
  201. return -1;
  202. return ret;
  203. }
  204. }
  205. ret = os_snprintf(buf, buflen, "%s", pin);
  206. if (ret < 0 || (size_t) ret >= buflen)
  207. return -1;
  208. return ret;
  209. }
  210. #ifdef CONFIG_WPS_NFC
  211. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  212. char *pos)
  213. {
  214. size_t len;
  215. struct wpabuf *buf;
  216. int ret;
  217. len = os_strlen(pos);
  218. if (len & 0x01)
  219. return -1;
  220. len /= 2;
  221. buf = wpabuf_alloc(len);
  222. if (buf == NULL)
  223. return -1;
  224. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  225. wpabuf_free(buf);
  226. return -1;
  227. }
  228. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  229. wpabuf_free(buf);
  230. return ret;
  231. }
  232. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  233. char *cmd, char *reply,
  234. size_t max_len)
  235. {
  236. int ndef;
  237. struct wpabuf *buf;
  238. int res;
  239. if (os_strcmp(cmd, "WPS") == 0)
  240. ndef = 0;
  241. else if (os_strcmp(cmd, "NDEF") == 0)
  242. ndef = 1;
  243. else
  244. return -1;
  245. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  246. if (buf == NULL)
  247. return -1;
  248. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  249. wpabuf_len(buf));
  250. reply[res++] = '\n';
  251. reply[res] = '\0';
  252. wpabuf_free(buf);
  253. return res;
  254. }
  255. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  256. char *reply, size_t max_len,
  257. int ndef)
  258. {
  259. struct wpabuf *buf;
  260. int res;
  261. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  262. if (buf == NULL)
  263. return -1;
  264. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  265. wpabuf_len(buf));
  266. reply[res++] = '\n';
  267. reply[res] = '\0';
  268. wpabuf_free(buf);
  269. return res;
  270. }
  271. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  272. char *cmd, char *reply,
  273. size_t max_len)
  274. {
  275. if (os_strcmp(cmd, "WPS") == 0)
  276. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  277. max_len, 0);
  278. if (os_strcmp(cmd, "NDEF") == 0)
  279. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  280. max_len, 1);
  281. if (os_strcmp(cmd, "enable") == 0)
  282. return hostapd_wps_nfc_token_enable(hapd);
  283. if (os_strcmp(cmd, "disable") == 0) {
  284. hostapd_wps_nfc_token_disable(hapd);
  285. return 0;
  286. }
  287. return -1;
  288. }
  289. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  290. char *cmd, char *reply,
  291. size_t max_len)
  292. {
  293. struct wpabuf *buf;
  294. int res;
  295. char *pos;
  296. int ndef;
  297. pos = os_strchr(cmd, ' ');
  298. if (pos == NULL)
  299. return -1;
  300. *pos++ = '\0';
  301. if (os_strcmp(cmd, "WPS") == 0)
  302. ndef = 0;
  303. else if (os_strcmp(cmd, "NDEF") == 0)
  304. ndef = 1;
  305. else
  306. return -1;
  307. if (os_strcmp(pos, "WPS-CR") == 0)
  308. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  309. else
  310. buf = NULL;
  311. if (buf == NULL)
  312. return -1;
  313. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  314. wpabuf_len(buf));
  315. reply[res++] = '\n';
  316. reply[res] = '\0';
  317. wpabuf_free(buf);
  318. return res;
  319. }
  320. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  321. char *cmd)
  322. {
  323. /*
  324. * Since NFC connection handover provided full WPS Credential, there is
  325. * no need for additional operations within hostapd. Just report this in
  326. * debug log.
  327. */
  328. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
  329. return 0;
  330. }
  331. #endif /* CONFIG_WPS_NFC */
  332. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  333. char *buf, size_t buflen)
  334. {
  335. int timeout = 300;
  336. char *pos;
  337. const char *pin_txt;
  338. pos = os_strchr(txt, ' ');
  339. if (pos)
  340. *pos++ = '\0';
  341. if (os_strcmp(txt, "disable") == 0) {
  342. hostapd_wps_ap_pin_disable(hapd);
  343. return os_snprintf(buf, buflen, "OK\n");
  344. }
  345. if (os_strcmp(txt, "random") == 0) {
  346. if (pos)
  347. timeout = atoi(pos);
  348. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  349. if (pin_txt == NULL)
  350. return -1;
  351. return os_snprintf(buf, buflen, "%s", pin_txt);
  352. }
  353. if (os_strcmp(txt, "get") == 0) {
  354. pin_txt = hostapd_wps_ap_pin_get(hapd);
  355. if (pin_txt == NULL)
  356. return -1;
  357. return os_snprintf(buf, buflen, "%s", pin_txt);
  358. }
  359. if (os_strcmp(txt, "set") == 0) {
  360. char *pin;
  361. if (pos == NULL)
  362. return -1;
  363. pin = pos;
  364. pos = os_strchr(pos, ' ');
  365. if (pos) {
  366. *pos++ = '\0';
  367. timeout = atoi(pos);
  368. }
  369. if (os_strlen(pin) > buflen)
  370. return -1;
  371. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  372. return -1;
  373. return os_snprintf(buf, buflen, "%s", pin);
  374. }
  375. return -1;
  376. }
  377. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  378. {
  379. char *pos;
  380. char *ssid, *auth, *encr = NULL, *key = NULL;
  381. ssid = txt;
  382. pos = os_strchr(txt, ' ');
  383. if (!pos)
  384. return -1;
  385. *pos++ = '\0';
  386. auth = pos;
  387. pos = os_strchr(pos, ' ');
  388. if (pos) {
  389. *pos++ = '\0';
  390. encr = pos;
  391. pos = os_strchr(pos, ' ');
  392. if (pos) {
  393. *pos++ = '\0';
  394. key = pos;
  395. }
  396. }
  397. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  398. }
  399. static const char * pbc_status_str(enum pbc_status status)
  400. {
  401. switch (status) {
  402. case WPS_PBC_STATUS_DISABLE:
  403. return "Disabled";
  404. case WPS_PBC_STATUS_ACTIVE:
  405. return "Active";
  406. case WPS_PBC_STATUS_TIMEOUT:
  407. return "Timed-out";
  408. case WPS_PBC_STATUS_OVERLAP:
  409. return "Overlap";
  410. default:
  411. return "Unknown";
  412. }
  413. }
  414. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  415. char *buf, size_t buflen)
  416. {
  417. int ret;
  418. char *pos, *end;
  419. pos = buf;
  420. end = buf + buflen;
  421. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  422. pbc_status_str(hapd->wps_stats.pbc_status));
  423. if (ret < 0 || ret >= end - pos)
  424. return pos - buf;
  425. pos += ret;
  426. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  427. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  428. "Success":
  429. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  430. "Failed" : "None")));
  431. if (ret < 0 || ret >= end - pos)
  432. return pos - buf;
  433. pos += ret;
  434. /* If status == Failure - Add possible Reasons */
  435. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  436. hapd->wps_stats.failure_reason > 0) {
  437. ret = os_snprintf(pos, end - pos,
  438. "Failure Reason: %s\n",
  439. wps_ei_str(hapd->wps_stats.failure_reason));
  440. if (ret < 0 || ret >= end - pos)
  441. return pos - buf;
  442. pos += ret;
  443. }
  444. if (hapd->wps_stats.status) {
  445. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  446. MAC2STR(hapd->wps_stats.peer_addr));
  447. if (ret < 0 || ret >= end - pos)
  448. return pos - buf;
  449. pos += ret;
  450. }
  451. return pos - buf;
  452. }
  453. #endif /* CONFIG_WPS */
  454. #ifdef CONFIG_INTERWORKING
  455. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  456. const char *cmd)
  457. {
  458. u8 qos_map_set[16 + 2 * 21], count = 0;
  459. const char *pos = cmd;
  460. int val, ret;
  461. for (;;) {
  462. if (count == sizeof(qos_map_set)) {
  463. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  464. return -1;
  465. }
  466. val = atoi(pos);
  467. if (val < 0 || val > 255) {
  468. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  469. return -1;
  470. }
  471. qos_map_set[count++] = val;
  472. pos = os_strchr(pos, ',');
  473. if (!pos)
  474. break;
  475. pos++;
  476. }
  477. if (count < 16 || count & 1) {
  478. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  479. return -1;
  480. }
  481. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  482. if (ret) {
  483. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  484. return -1;
  485. }
  486. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  487. hapd->conf->qos_map_set_len = count;
  488. return 0;
  489. }
  490. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  491. const char *cmd)
  492. {
  493. u8 addr[ETH_ALEN];
  494. struct sta_info *sta;
  495. struct wpabuf *buf;
  496. u8 *qos_map_set = hapd->conf->qos_map_set;
  497. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  498. int ret;
  499. if (!qos_map_set_len) {
  500. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  501. return -1;
  502. }
  503. if (hwaddr_aton(cmd, addr))
  504. return -1;
  505. sta = ap_get_sta(hapd, addr);
  506. if (sta == NULL) {
  507. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  508. "for QoS Map Configuration message",
  509. MAC2STR(addr));
  510. return -1;
  511. }
  512. if (!sta->qos_map_enabled) {
  513. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  514. "support for QoS Map", MAC2STR(addr));
  515. return -1;
  516. }
  517. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  518. if (buf == NULL)
  519. return -1;
  520. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  521. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  522. /* QoS Map Set Element */
  523. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  524. wpabuf_put_u8(buf, qos_map_set_len);
  525. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  526. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  527. wpabuf_head(buf), wpabuf_len(buf));
  528. wpabuf_free(buf);
  529. return ret;
  530. }
  531. #endif /* CONFIG_INTERWORKING */
  532. #ifdef CONFIG_WNM
  533. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  534. const char *cmd)
  535. {
  536. u8 addr[ETH_ALEN];
  537. u8 buf[1000], *pos;
  538. struct ieee80211_mgmt *mgmt;
  539. int disassoc_timer;
  540. if (hwaddr_aton(cmd, addr))
  541. return -1;
  542. if (cmd[17] != ' ')
  543. return -1;
  544. disassoc_timer = atoi(cmd + 17);
  545. os_memset(buf, 0, sizeof(buf));
  546. mgmt = (struct ieee80211_mgmt *) buf;
  547. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  548. WLAN_FC_STYPE_ACTION);
  549. os_memcpy(mgmt->da, addr, ETH_ALEN);
  550. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  551. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  552. mgmt->u.action.category = WLAN_ACTION_WNM;
  553. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  554. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  555. mgmt->u.action.u.bss_tm_req.req_mode =
  556. WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  557. mgmt->u.action.u.bss_tm_req.disassoc_timer =
  558. host_to_le16(disassoc_timer);
  559. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  560. pos = mgmt->u.action.u.bss_tm_req.variable;
  561. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  562. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  563. "Management Request frame");
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  569. const char *cmd)
  570. {
  571. u8 addr[ETH_ALEN];
  572. const char *url, *timerstr;
  573. int disassoc_timer;
  574. struct sta_info *sta;
  575. if (hwaddr_aton(cmd, addr))
  576. return -1;
  577. sta = ap_get_sta(hapd, addr);
  578. if (sta == NULL) {
  579. wpa_printf(MSG_DEBUG, "Station " MACSTR
  580. " not found for ESS disassociation imminent message",
  581. MAC2STR(addr));
  582. return -1;
  583. }
  584. timerstr = cmd + 17;
  585. if (*timerstr != ' ')
  586. return -1;
  587. timerstr++;
  588. disassoc_timer = atoi(timerstr);
  589. if (disassoc_timer < 0 || disassoc_timer > 65535)
  590. return -1;
  591. url = os_strchr(timerstr, ' ');
  592. if (url == NULL)
  593. return -1;
  594. url++;
  595. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  596. }
  597. #endif /* CONFIG_WNM */
  598. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  599. char *buf, size_t buflen)
  600. {
  601. int ret;
  602. char *pos, *end;
  603. pos = buf;
  604. end = buf + buflen;
  605. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  606. "ssid=%s\n",
  607. MAC2STR(hapd->own_addr),
  608. wpa_ssid_txt(hapd->conf->ssid.ssid,
  609. hapd->conf->ssid.ssid_len));
  610. if (ret < 0 || ret >= end - pos)
  611. return pos - buf;
  612. pos += ret;
  613. #ifdef CONFIG_WPS
  614. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  615. hapd->conf->wps_state == 0 ? "disabled" :
  616. (hapd->conf->wps_state == 1 ? "not configured" :
  617. "configured"));
  618. if (ret < 0 || ret >= end - pos)
  619. return pos - buf;
  620. pos += ret;
  621. if (hapd->conf->wps_state && hapd->conf->wpa &&
  622. hapd->conf->ssid.wpa_passphrase) {
  623. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  624. hapd->conf->ssid.wpa_passphrase);
  625. if (ret < 0 || ret >= end - pos)
  626. return pos - buf;
  627. pos += ret;
  628. }
  629. if (hapd->conf->wps_state && hapd->conf->wpa &&
  630. hapd->conf->ssid.wpa_psk &&
  631. hapd->conf->ssid.wpa_psk->group) {
  632. char hex[PMK_LEN * 2 + 1];
  633. wpa_snprintf_hex(hex, sizeof(hex),
  634. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  635. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  636. if (ret < 0 || ret >= end - pos)
  637. return pos - buf;
  638. pos += ret;
  639. }
  640. #endif /* CONFIG_WPS */
  641. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  642. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  643. if (ret < 0 || ret >= end - pos)
  644. return pos - buf;
  645. pos += ret;
  646. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  647. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  648. if (ret < 0 || ret >= end - pos)
  649. return pos - buf;
  650. pos += ret;
  651. }
  652. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  653. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  654. if (ret < 0 || ret >= end - pos)
  655. return pos - buf;
  656. pos += ret;
  657. }
  658. #ifdef CONFIG_IEEE80211R
  659. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  660. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  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_FT_IEEE8021X) {
  666. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  667. if (ret < 0 || ret >= end - pos)
  668. return pos - buf;
  669. pos += ret;
  670. }
  671. #endif /* CONFIG_IEEE80211R */
  672. #ifdef CONFIG_IEEE80211W
  673. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  674. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  675. if (ret < 0 || ret >= end - pos)
  676. return pos - buf;
  677. pos += ret;
  678. }
  679. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  680. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  681. if (ret < 0 || ret >= end - pos)
  682. return pos - buf;
  683. pos += ret;
  684. }
  685. #endif /* CONFIG_IEEE80211W */
  686. ret = os_snprintf(pos, end - pos, "\n");
  687. if (ret < 0 || ret >= end - pos)
  688. return pos - buf;
  689. pos += ret;
  690. }
  691. if (hapd->conf->wpa) {
  692. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  693. wpa_cipher_txt(hapd->conf->wpa_group));
  694. if (ret < 0 || ret >= end - pos)
  695. return pos - buf;
  696. pos += ret;
  697. }
  698. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  699. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  700. if (ret < 0 || ret >= end - pos)
  701. return pos - buf;
  702. pos += ret;
  703. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  704. " ");
  705. if (ret < 0)
  706. return pos - buf;
  707. pos += ret;
  708. ret = os_snprintf(pos, end - pos, "\n");
  709. if (ret < 0 || ret >= end - pos)
  710. return pos - buf;
  711. pos += ret;
  712. }
  713. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  714. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  715. if (ret < 0 || ret >= end - pos)
  716. return pos - buf;
  717. pos += ret;
  718. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  719. " ");
  720. if (ret < 0)
  721. return pos - buf;
  722. pos += ret;
  723. ret = os_snprintf(pos, end - pos, "\n");
  724. if (ret < 0 || ret >= end - pos)
  725. return pos - buf;
  726. pos += ret;
  727. }
  728. return pos - buf;
  729. }
  730. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  731. {
  732. char *value;
  733. int ret = 0;
  734. value = os_strchr(cmd, ' ');
  735. if (value == NULL)
  736. return -1;
  737. *value++ = '\0';
  738. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  739. if (0) {
  740. #ifdef CONFIG_WPS_TESTING
  741. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  742. long int val;
  743. val = strtol(value, NULL, 0);
  744. if (val < 0 || val > 0xff) {
  745. ret = -1;
  746. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  747. "wps_version_number %ld", val);
  748. } else {
  749. wps_version_number = val;
  750. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  751. "version %u.%u",
  752. (wps_version_number & 0xf0) >> 4,
  753. wps_version_number & 0x0f);
  754. hostapd_wps_update_ie(hapd);
  755. }
  756. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  757. wps_testing_dummy_cred = atoi(value);
  758. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  759. wps_testing_dummy_cred);
  760. #endif /* CONFIG_WPS_TESTING */
  761. #ifdef CONFIG_INTERWORKING
  762. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  763. int val = atoi(value);
  764. if (val <= 0)
  765. ret = -1;
  766. else
  767. hapd->gas_frag_limit = val;
  768. #endif /* CONFIG_INTERWORKING */
  769. #ifdef CONFIG_TESTING_OPTIONS
  770. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  771. hapd->ext_mgmt_frame_handling = atoi(value);
  772. #endif /* CONFIG_TESTING_OPTIONS */
  773. } else {
  774. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  775. }
  776. return ret;
  777. }
  778. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  779. char *buf, size_t buflen)
  780. {
  781. int res;
  782. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  783. if (os_strcmp(cmd, "version") == 0) {
  784. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  785. if (res < 0 || (unsigned int) res >= buflen)
  786. return -1;
  787. return res;
  788. }
  789. return -1;
  790. }
  791. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  792. {
  793. if (hostapd_enable_iface(iface) < 0) {
  794. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  795. return -1;
  796. }
  797. return 0;
  798. }
  799. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  800. {
  801. if (hostapd_reload_iface(iface) < 0) {
  802. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  803. return -1;
  804. }
  805. return 0;
  806. }
  807. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  808. {
  809. if (hostapd_disable_iface(iface) < 0) {
  810. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  811. return -1;
  812. }
  813. return 0;
  814. }
  815. #ifdef CONFIG_TESTING_OPTIONS
  816. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  817. {
  818. union wpa_event_data data;
  819. char *pos, *param;
  820. enum wpa_event_type event;
  821. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  822. os_memset(&data, 0, sizeof(data));
  823. param = os_strchr(cmd, ' ');
  824. if (param == NULL)
  825. return -1;
  826. *param++ = '\0';
  827. if (os_strcmp(cmd, "DETECTED") == 0)
  828. event = EVENT_DFS_RADAR_DETECTED;
  829. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  830. event = EVENT_DFS_CAC_FINISHED;
  831. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  832. event = EVENT_DFS_CAC_ABORTED;
  833. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  834. event = EVENT_DFS_NOP_FINISHED;
  835. else {
  836. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  837. cmd);
  838. return -1;
  839. }
  840. pos = os_strstr(param, "freq=");
  841. if (pos)
  842. data.dfs_event.freq = atoi(pos + 5);
  843. pos = os_strstr(param, "ht_enabled=1");
  844. if (pos)
  845. data.dfs_event.ht_enabled = 1;
  846. pos = os_strstr(param, "chan_offset=");
  847. if (pos)
  848. data.dfs_event.chan_offset = atoi(pos + 12);
  849. pos = os_strstr(param, "chan_width=");
  850. if (pos)
  851. data.dfs_event.chan_width = atoi(pos + 11);
  852. pos = os_strstr(param, "cf1=");
  853. if (pos)
  854. data.dfs_event.cf1 = atoi(pos + 4);
  855. pos = os_strstr(param, "cf2=");
  856. if (pos)
  857. data.dfs_event.cf2 = atoi(pos + 4);
  858. wpa_supplicant_event(hapd, event, &data);
  859. return 0;
  860. }
  861. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  862. {
  863. size_t len;
  864. u8 *buf;
  865. int res;
  866. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  867. len = os_strlen(cmd);
  868. if (len & 1)
  869. return -1;
  870. len /= 2;
  871. buf = os_malloc(len);
  872. if (buf == NULL)
  873. return -1;
  874. if (hexstr2bin(cmd, buf, len) < 0) {
  875. os_free(buf);
  876. return -1;
  877. }
  878. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  879. os_free(buf);
  880. return res;
  881. }
  882. #endif /* CONFIG_TESTING_OPTIONS */
  883. static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
  884. {
  885. #ifdef NEED_AP_MLME
  886. struct csa_settings settings;
  887. int ret = hostapd_parse_csa_settings(pos, &settings);
  888. if (ret)
  889. return ret;
  890. return hostapd_switch_channel(hapd, &settings);
  891. #else /* NEED_AP_MLME */
  892. return -1;
  893. #endif /* NEED_AP_MLME */
  894. }
  895. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  896. void *sock_ctx)
  897. {
  898. struct hostapd_data *hapd = eloop_ctx;
  899. char buf[256];
  900. int res;
  901. struct sockaddr_un from;
  902. socklen_t fromlen = sizeof(from);
  903. char *reply;
  904. const int reply_size = 4096;
  905. int reply_len;
  906. int level = MSG_DEBUG;
  907. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  908. (struct sockaddr *) &from, &fromlen);
  909. if (res < 0) {
  910. perror("recvfrom(ctrl_iface)");
  911. return;
  912. }
  913. buf[res] = '\0';
  914. if (os_strcmp(buf, "PING") == 0)
  915. level = MSG_EXCESSIVE;
  916. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  917. reply = os_malloc(reply_size);
  918. if (reply == NULL) {
  919. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  920. fromlen);
  921. return;
  922. }
  923. os_memcpy(reply, "OK\n", 3);
  924. reply_len = 3;
  925. if (os_strcmp(buf, "PING") == 0) {
  926. os_memcpy(reply, "PONG\n", 5);
  927. reply_len = 5;
  928. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  929. if (wpa_debug_reopen_file() < 0)
  930. reply_len = -1;
  931. } else if (os_strcmp(buf, "STATUS") == 0) {
  932. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  933. reply_size);
  934. } else if (os_strcmp(buf, "MIB") == 0) {
  935. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  936. if (reply_len >= 0) {
  937. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  938. reply_size - reply_len);
  939. if (res < 0)
  940. reply_len = -1;
  941. else
  942. reply_len += res;
  943. }
  944. if (reply_len >= 0) {
  945. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  946. reply_size - reply_len);
  947. if (res < 0)
  948. reply_len = -1;
  949. else
  950. reply_len += res;
  951. }
  952. #ifndef CONFIG_NO_RADIUS
  953. if (reply_len >= 0) {
  954. res = radius_client_get_mib(hapd->radius,
  955. reply + reply_len,
  956. reply_size - reply_len);
  957. if (res < 0)
  958. reply_len = -1;
  959. else
  960. reply_len += res;
  961. }
  962. #endif /* CONFIG_NO_RADIUS */
  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 */