ctrl_iface.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2014, 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. #ifdef CONFIG_TESTING_OPTIONS
  11. #include <net/ethernet.h>
  12. #include <netinet/ip.h>
  13. #endif /* CONFIG_TESTING_OPTIONS */
  14. #include <sys/un.h>
  15. #include <sys/stat.h>
  16. #include <stddef.h>
  17. #include "utils/common.h"
  18. #include "utils/eloop.h"
  19. #include "common/version.h"
  20. #include "common/ieee802_11_defs.h"
  21. #include "drivers/driver.h"
  22. #include "radius/radius_client.h"
  23. #include "radius/radius_server.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "ap/hostapd.h"
  26. #include "ap/ap_config.h"
  27. #include "ap/ieee802_1x.h"
  28. #include "ap/wpa_auth.h"
  29. #include "ap/ieee802_11.h"
  30. #include "ap/sta_info.h"
  31. #include "ap/wps_hostapd.h"
  32. #include "ap/ctrl_iface_ap.h"
  33. #include "ap/ap_drv_ops.h"
  34. #include "ap/hs20.h"
  35. #include "ap/wnm_ap.h"
  36. #include "ap/wpa_auth.h"
  37. #include "wps/wps_defs.h"
  38. #include "wps/wps.h"
  39. #include "config_file.h"
  40. #include "ctrl_iface.h"
  41. struct wpa_ctrl_dst {
  42. struct wpa_ctrl_dst *next;
  43. struct sockaddr_un addr;
  44. socklen_t addrlen;
  45. int debug_level;
  46. int errors;
  47. };
  48. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  49. const char *buf, size_t len);
  50. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  51. struct sockaddr_un *from,
  52. socklen_t fromlen)
  53. {
  54. struct wpa_ctrl_dst *dst;
  55. dst = os_zalloc(sizeof(*dst));
  56. if (dst == NULL)
  57. return -1;
  58. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  59. dst->addrlen = fromlen;
  60. dst->debug_level = MSG_INFO;
  61. dst->next = hapd->ctrl_dst;
  62. hapd->ctrl_dst = dst;
  63. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  64. (u8 *) from->sun_path,
  65. fromlen - offsetof(struct sockaddr_un, sun_path));
  66. return 0;
  67. }
  68. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  69. struct sockaddr_un *from,
  70. socklen_t fromlen)
  71. {
  72. struct wpa_ctrl_dst *dst, *prev = NULL;
  73. dst = hapd->ctrl_dst;
  74. while (dst) {
  75. if (fromlen == dst->addrlen &&
  76. os_memcmp(from->sun_path, dst->addr.sun_path,
  77. fromlen - offsetof(struct sockaddr_un, sun_path))
  78. == 0) {
  79. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  80. (u8 *) from->sun_path,
  81. fromlen -
  82. offsetof(struct sockaddr_un, sun_path));
  83. if (prev == NULL)
  84. hapd->ctrl_dst = dst->next;
  85. else
  86. prev->next = dst->next;
  87. os_free(dst);
  88. return 0;
  89. }
  90. prev = dst;
  91. dst = dst->next;
  92. }
  93. return -1;
  94. }
  95. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  96. struct sockaddr_un *from,
  97. socklen_t fromlen,
  98. char *level)
  99. {
  100. struct wpa_ctrl_dst *dst;
  101. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  102. dst = hapd->ctrl_dst;
  103. while (dst) {
  104. if (fromlen == dst->addrlen &&
  105. os_memcmp(from->sun_path, dst->addr.sun_path,
  106. fromlen - offsetof(struct sockaddr_un, sun_path))
  107. == 0) {
  108. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  109. "level", (u8 *) from->sun_path, fromlen -
  110. offsetof(struct sockaddr_un, sun_path));
  111. dst->debug_level = atoi(level);
  112. return 0;
  113. }
  114. dst = dst->next;
  115. }
  116. return -1;
  117. }
  118. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  119. const char *txtaddr)
  120. {
  121. u8 addr[ETH_ALEN];
  122. struct sta_info *sta;
  123. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  124. if (hwaddr_aton(txtaddr, addr))
  125. return -1;
  126. sta = ap_get_sta(hapd, addr);
  127. if (sta)
  128. return 0;
  129. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  130. "notification", MAC2STR(addr));
  131. sta = ap_sta_add(hapd, addr);
  132. if (sta == NULL)
  133. return -1;
  134. hostapd_new_assoc_sta(hapd, sta, 0);
  135. return 0;
  136. }
  137. #ifdef CONFIG_IEEE80211W
  138. #ifdef NEED_AP_MLME
  139. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  140. const char *txtaddr)
  141. {
  142. u8 addr[ETH_ALEN];
  143. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  144. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  145. if (hwaddr_aton(txtaddr, addr) ||
  146. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  147. return -1;
  148. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  149. return 0;
  150. }
  151. #endif /* NEED_AP_MLME */
  152. #endif /* CONFIG_IEEE80211W */
  153. #ifdef CONFIG_WPS
  154. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  155. {
  156. char *pin = os_strchr(txt, ' ');
  157. char *timeout_txt;
  158. int timeout;
  159. u8 addr_buf[ETH_ALEN], *addr = NULL;
  160. char *pos;
  161. if (pin == NULL)
  162. return -1;
  163. *pin++ = '\0';
  164. timeout_txt = os_strchr(pin, ' ');
  165. if (timeout_txt) {
  166. *timeout_txt++ = '\0';
  167. timeout = atoi(timeout_txt);
  168. pos = os_strchr(timeout_txt, ' ');
  169. if (pos) {
  170. *pos++ = '\0';
  171. if (hwaddr_aton(pos, addr_buf) == 0)
  172. addr = addr_buf;
  173. }
  174. } else
  175. timeout = 0;
  176. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  177. }
  178. static int hostapd_ctrl_iface_wps_check_pin(
  179. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  180. {
  181. char pin[9];
  182. size_t len;
  183. char *pos;
  184. int ret;
  185. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  186. (u8 *) cmd, os_strlen(cmd));
  187. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  188. if (*pos < '0' || *pos > '9')
  189. continue;
  190. pin[len++] = *pos;
  191. if (len == 9) {
  192. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  193. return -1;
  194. }
  195. }
  196. if (len != 4 && len != 8) {
  197. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  198. return -1;
  199. }
  200. pin[len] = '\0';
  201. if (len == 8) {
  202. unsigned int pin_val;
  203. pin_val = atoi(pin);
  204. if (!wps_pin_valid(pin_val)) {
  205. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  206. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  207. if (ret < 0 || (size_t) ret >= buflen)
  208. return -1;
  209. return ret;
  210. }
  211. }
  212. ret = os_snprintf(buf, buflen, "%s", pin);
  213. if (ret < 0 || (size_t) ret >= buflen)
  214. return -1;
  215. return ret;
  216. }
  217. #ifdef CONFIG_WPS_NFC
  218. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  219. char *pos)
  220. {
  221. size_t len;
  222. struct wpabuf *buf;
  223. int ret;
  224. len = os_strlen(pos);
  225. if (len & 0x01)
  226. return -1;
  227. len /= 2;
  228. buf = wpabuf_alloc(len);
  229. if (buf == NULL)
  230. return -1;
  231. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  232. wpabuf_free(buf);
  233. return -1;
  234. }
  235. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  236. wpabuf_free(buf);
  237. return ret;
  238. }
  239. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  240. char *cmd, char *reply,
  241. size_t max_len)
  242. {
  243. int ndef;
  244. struct wpabuf *buf;
  245. int res;
  246. if (os_strcmp(cmd, "WPS") == 0)
  247. ndef = 0;
  248. else if (os_strcmp(cmd, "NDEF") == 0)
  249. ndef = 1;
  250. else
  251. return -1;
  252. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  253. if (buf == NULL)
  254. return -1;
  255. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  256. wpabuf_len(buf));
  257. reply[res++] = '\n';
  258. reply[res] = '\0';
  259. wpabuf_free(buf);
  260. return res;
  261. }
  262. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  263. char *reply, size_t max_len,
  264. int ndef)
  265. {
  266. struct wpabuf *buf;
  267. int res;
  268. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  269. if (buf == NULL)
  270. return -1;
  271. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  272. wpabuf_len(buf));
  273. reply[res++] = '\n';
  274. reply[res] = '\0';
  275. wpabuf_free(buf);
  276. return res;
  277. }
  278. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  279. char *cmd, char *reply,
  280. size_t max_len)
  281. {
  282. if (os_strcmp(cmd, "WPS") == 0)
  283. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  284. max_len, 0);
  285. if (os_strcmp(cmd, "NDEF") == 0)
  286. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  287. max_len, 1);
  288. if (os_strcmp(cmd, "enable") == 0)
  289. return hostapd_wps_nfc_token_enable(hapd);
  290. if (os_strcmp(cmd, "disable") == 0) {
  291. hostapd_wps_nfc_token_disable(hapd);
  292. return 0;
  293. }
  294. return -1;
  295. }
  296. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  297. char *cmd, char *reply,
  298. size_t max_len)
  299. {
  300. struct wpabuf *buf;
  301. int res;
  302. char *pos;
  303. int ndef;
  304. pos = os_strchr(cmd, ' ');
  305. if (pos == NULL)
  306. return -1;
  307. *pos++ = '\0';
  308. if (os_strcmp(cmd, "WPS") == 0)
  309. ndef = 0;
  310. else if (os_strcmp(cmd, "NDEF") == 0)
  311. ndef = 1;
  312. else
  313. return -1;
  314. if (os_strcmp(pos, "WPS-CR") == 0)
  315. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  316. else
  317. buf = NULL;
  318. if (buf == NULL)
  319. return -1;
  320. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  321. wpabuf_len(buf));
  322. reply[res++] = '\n';
  323. reply[res] = '\0';
  324. wpabuf_free(buf);
  325. return res;
  326. }
  327. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  328. char *cmd)
  329. {
  330. size_t len;
  331. struct wpabuf *req, *sel;
  332. int ret;
  333. char *pos, *role, *type, *pos2;
  334. role = cmd;
  335. pos = os_strchr(role, ' ');
  336. if (pos == NULL)
  337. return -1;
  338. *pos++ = '\0';
  339. type = pos;
  340. pos = os_strchr(type, ' ');
  341. if (pos == NULL)
  342. return -1;
  343. *pos++ = '\0';
  344. pos2 = os_strchr(pos, ' ');
  345. if (pos2 == NULL)
  346. return -1;
  347. *pos2++ = '\0';
  348. len = os_strlen(pos);
  349. if (len & 0x01)
  350. return -1;
  351. len /= 2;
  352. req = wpabuf_alloc(len);
  353. if (req == NULL)
  354. return -1;
  355. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  356. wpabuf_free(req);
  357. return -1;
  358. }
  359. len = os_strlen(pos2);
  360. if (len & 0x01) {
  361. wpabuf_free(req);
  362. return -1;
  363. }
  364. len /= 2;
  365. sel = wpabuf_alloc(len);
  366. if (sel == NULL) {
  367. wpabuf_free(req);
  368. return -1;
  369. }
  370. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  371. wpabuf_free(req);
  372. wpabuf_free(sel);
  373. return -1;
  374. }
  375. if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
  376. ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
  377. } else {
  378. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  379. "reported: role=%s type=%s", role, type);
  380. ret = -1;
  381. }
  382. wpabuf_free(req);
  383. wpabuf_free(sel);
  384. return ret;
  385. }
  386. #endif /* CONFIG_WPS_NFC */
  387. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  388. char *buf, size_t buflen)
  389. {
  390. int timeout = 300;
  391. char *pos;
  392. const char *pin_txt;
  393. pos = os_strchr(txt, ' ');
  394. if (pos)
  395. *pos++ = '\0';
  396. if (os_strcmp(txt, "disable") == 0) {
  397. hostapd_wps_ap_pin_disable(hapd);
  398. return os_snprintf(buf, buflen, "OK\n");
  399. }
  400. if (os_strcmp(txt, "random") == 0) {
  401. if (pos)
  402. timeout = atoi(pos);
  403. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  404. if (pin_txt == NULL)
  405. return -1;
  406. return os_snprintf(buf, buflen, "%s", pin_txt);
  407. }
  408. if (os_strcmp(txt, "get") == 0) {
  409. pin_txt = hostapd_wps_ap_pin_get(hapd);
  410. if (pin_txt == NULL)
  411. return -1;
  412. return os_snprintf(buf, buflen, "%s", pin_txt);
  413. }
  414. if (os_strcmp(txt, "set") == 0) {
  415. char *pin;
  416. if (pos == NULL)
  417. return -1;
  418. pin = pos;
  419. pos = os_strchr(pos, ' ');
  420. if (pos) {
  421. *pos++ = '\0';
  422. timeout = atoi(pos);
  423. }
  424. if (os_strlen(pin) > buflen)
  425. return -1;
  426. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  427. return -1;
  428. return os_snprintf(buf, buflen, "%s", pin);
  429. }
  430. return -1;
  431. }
  432. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  433. {
  434. char *pos;
  435. char *ssid, *auth, *encr = NULL, *key = NULL;
  436. ssid = txt;
  437. pos = os_strchr(txt, ' ');
  438. if (!pos)
  439. return -1;
  440. *pos++ = '\0';
  441. auth = pos;
  442. pos = os_strchr(pos, ' ');
  443. if (pos) {
  444. *pos++ = '\0';
  445. encr = pos;
  446. pos = os_strchr(pos, ' ');
  447. if (pos) {
  448. *pos++ = '\0';
  449. key = pos;
  450. }
  451. }
  452. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  453. }
  454. static const char * pbc_status_str(enum pbc_status status)
  455. {
  456. switch (status) {
  457. case WPS_PBC_STATUS_DISABLE:
  458. return "Disabled";
  459. case WPS_PBC_STATUS_ACTIVE:
  460. return "Active";
  461. case WPS_PBC_STATUS_TIMEOUT:
  462. return "Timed-out";
  463. case WPS_PBC_STATUS_OVERLAP:
  464. return "Overlap";
  465. default:
  466. return "Unknown";
  467. }
  468. }
  469. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  470. char *buf, size_t buflen)
  471. {
  472. int ret;
  473. char *pos, *end;
  474. pos = buf;
  475. end = buf + buflen;
  476. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  477. pbc_status_str(hapd->wps_stats.pbc_status));
  478. if (ret < 0 || ret >= end - pos)
  479. return pos - buf;
  480. pos += ret;
  481. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  482. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  483. "Success":
  484. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  485. "Failed" : "None")));
  486. if (ret < 0 || ret >= end - pos)
  487. return pos - buf;
  488. pos += ret;
  489. /* If status == Failure - Add possible Reasons */
  490. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  491. hapd->wps_stats.failure_reason > 0) {
  492. ret = os_snprintf(pos, end - pos,
  493. "Failure Reason: %s\n",
  494. wps_ei_str(hapd->wps_stats.failure_reason));
  495. if (ret < 0 || ret >= end - pos)
  496. return pos - buf;
  497. pos += ret;
  498. }
  499. if (hapd->wps_stats.status) {
  500. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  501. MAC2STR(hapd->wps_stats.peer_addr));
  502. if (ret < 0 || ret >= end - pos)
  503. return pos - buf;
  504. pos += ret;
  505. }
  506. return pos - buf;
  507. }
  508. #endif /* CONFIG_WPS */
  509. #ifdef CONFIG_HS20
  510. static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
  511. const char *cmd)
  512. {
  513. u8 addr[ETH_ALEN];
  514. const char *url;
  515. if (hwaddr_aton(cmd, addr))
  516. return -1;
  517. url = cmd + 17;
  518. if (*url == '\0') {
  519. url = NULL;
  520. } else {
  521. if (*url != ' ')
  522. return -1;
  523. url++;
  524. if (*url == '\0')
  525. url = NULL;
  526. }
  527. return hs20_send_wnm_notification(hapd, addr, 1, url);
  528. }
  529. static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
  530. const char *cmd)
  531. {
  532. u8 addr[ETH_ALEN];
  533. int code, reauth_delay, ret;
  534. const char *pos;
  535. size_t url_len;
  536. struct wpabuf *req;
  537. /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
  538. if (hwaddr_aton(cmd, addr))
  539. return -1;
  540. pos = os_strchr(cmd, ' ');
  541. if (pos == NULL)
  542. return -1;
  543. pos++;
  544. code = atoi(pos);
  545. pos = os_strchr(pos, ' ');
  546. if (pos == NULL)
  547. return -1;
  548. pos++;
  549. reauth_delay = atoi(pos);
  550. url_len = 0;
  551. pos = os_strchr(pos, ' ');
  552. if (pos) {
  553. pos++;
  554. url_len = os_strlen(pos);
  555. }
  556. req = wpabuf_alloc(4 + url_len);
  557. if (req == NULL)
  558. return -1;
  559. wpabuf_put_u8(req, code);
  560. wpabuf_put_le16(req, reauth_delay);
  561. wpabuf_put_u8(req, url_len);
  562. if (pos)
  563. wpabuf_put_data(req, pos, url_len);
  564. wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
  565. " to indicate imminent deauthentication (code=%d "
  566. "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
  567. ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
  568. wpabuf_free(req);
  569. return ret;
  570. }
  571. #endif /* CONFIG_HS20 */
  572. #ifdef CONFIG_INTERWORKING
  573. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  574. const char *cmd)
  575. {
  576. u8 qos_map_set[16 + 2 * 21], count = 0;
  577. const char *pos = cmd;
  578. int val, ret;
  579. for (;;) {
  580. if (count == sizeof(qos_map_set)) {
  581. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  582. return -1;
  583. }
  584. val = atoi(pos);
  585. if (val < 0 || val > 255) {
  586. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  587. return -1;
  588. }
  589. qos_map_set[count++] = val;
  590. pos = os_strchr(pos, ',');
  591. if (!pos)
  592. break;
  593. pos++;
  594. }
  595. if (count < 16 || count & 1) {
  596. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  597. return -1;
  598. }
  599. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  600. if (ret) {
  601. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  602. return -1;
  603. }
  604. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  605. hapd->conf->qos_map_set_len = count;
  606. return 0;
  607. }
  608. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  609. const char *cmd)
  610. {
  611. u8 addr[ETH_ALEN];
  612. struct sta_info *sta;
  613. struct wpabuf *buf;
  614. u8 *qos_map_set = hapd->conf->qos_map_set;
  615. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  616. int ret;
  617. if (!qos_map_set_len) {
  618. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  619. return -1;
  620. }
  621. if (hwaddr_aton(cmd, addr))
  622. return -1;
  623. sta = ap_get_sta(hapd, addr);
  624. if (sta == NULL) {
  625. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  626. "for QoS Map Configuration message",
  627. MAC2STR(addr));
  628. return -1;
  629. }
  630. if (!sta->qos_map_enabled) {
  631. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  632. "support for QoS Map", MAC2STR(addr));
  633. return -1;
  634. }
  635. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  636. if (buf == NULL)
  637. return -1;
  638. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  639. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  640. /* QoS Map Set Element */
  641. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  642. wpabuf_put_u8(buf, qos_map_set_len);
  643. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  644. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  645. wpabuf_head(buf), wpabuf_len(buf));
  646. wpabuf_free(buf);
  647. return ret;
  648. }
  649. #endif /* CONFIG_INTERWORKING */
  650. #ifdef CONFIG_WNM
  651. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  652. const char *cmd)
  653. {
  654. u8 addr[ETH_ALEN];
  655. int disassoc_timer;
  656. struct sta_info *sta;
  657. if (hwaddr_aton(cmd, addr))
  658. return -1;
  659. if (cmd[17] != ' ')
  660. return -1;
  661. disassoc_timer = atoi(cmd + 17);
  662. sta = ap_get_sta(hapd, addr);
  663. if (sta == NULL) {
  664. wpa_printf(MSG_DEBUG, "Station " MACSTR
  665. " not found for disassociation imminent message",
  666. MAC2STR(addr));
  667. return -1;
  668. }
  669. return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
  670. }
  671. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  672. const char *cmd)
  673. {
  674. u8 addr[ETH_ALEN];
  675. const char *url, *timerstr;
  676. int disassoc_timer;
  677. struct sta_info *sta;
  678. if (hwaddr_aton(cmd, addr))
  679. return -1;
  680. sta = ap_get_sta(hapd, addr);
  681. if (sta == NULL) {
  682. wpa_printf(MSG_DEBUG, "Station " MACSTR
  683. " not found for ESS disassociation imminent message",
  684. MAC2STR(addr));
  685. return -1;
  686. }
  687. timerstr = cmd + 17;
  688. if (*timerstr != ' ')
  689. return -1;
  690. timerstr++;
  691. disassoc_timer = atoi(timerstr);
  692. if (disassoc_timer < 0 || disassoc_timer > 65535)
  693. return -1;
  694. url = os_strchr(timerstr, ' ');
  695. if (url == NULL)
  696. return -1;
  697. url++;
  698. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  699. }
  700. #endif /* CONFIG_WNM */
  701. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  702. char *buf, size_t buflen)
  703. {
  704. int ret;
  705. char *pos, *end;
  706. pos = buf;
  707. end = buf + buflen;
  708. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  709. "ssid=%s\n",
  710. MAC2STR(hapd->own_addr),
  711. wpa_ssid_txt(hapd->conf->ssid.ssid,
  712. hapd->conf->ssid.ssid_len));
  713. if (ret < 0 || ret >= end - pos)
  714. return pos - buf;
  715. pos += ret;
  716. #ifdef CONFIG_WPS
  717. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  718. hapd->conf->wps_state == 0 ? "disabled" :
  719. (hapd->conf->wps_state == 1 ? "not configured" :
  720. "configured"));
  721. if (ret < 0 || ret >= end - pos)
  722. return pos - buf;
  723. pos += ret;
  724. if (hapd->conf->wps_state && hapd->conf->wpa &&
  725. hapd->conf->ssid.wpa_passphrase) {
  726. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  727. hapd->conf->ssid.wpa_passphrase);
  728. if (ret < 0 || ret >= end - pos)
  729. return pos - buf;
  730. pos += ret;
  731. }
  732. if (hapd->conf->wps_state && hapd->conf->wpa &&
  733. hapd->conf->ssid.wpa_psk &&
  734. hapd->conf->ssid.wpa_psk->group) {
  735. char hex[PMK_LEN * 2 + 1];
  736. wpa_snprintf_hex(hex, sizeof(hex),
  737. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  738. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  739. if (ret < 0 || ret >= end - pos)
  740. return pos - buf;
  741. pos += ret;
  742. }
  743. #endif /* CONFIG_WPS */
  744. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  745. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  746. if (ret < 0 || ret >= end - pos)
  747. return pos - buf;
  748. pos += ret;
  749. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  750. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  751. if (ret < 0 || ret >= end - pos)
  752. return pos - buf;
  753. pos += ret;
  754. }
  755. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  756. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  757. if (ret < 0 || ret >= end - pos)
  758. return pos - buf;
  759. pos += ret;
  760. }
  761. #ifdef CONFIG_IEEE80211R
  762. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  763. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  764. if (ret < 0 || ret >= end - pos)
  765. return pos - buf;
  766. pos += ret;
  767. }
  768. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  769. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  770. if (ret < 0 || ret >= end - pos)
  771. return pos - buf;
  772. pos += ret;
  773. }
  774. #ifdef CONFIG_SAE
  775. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  776. ret = os_snprintf(pos, end - pos, "FT-SAE ");
  777. if (ret < 0 || ret >= end - pos)
  778. return pos - buf;
  779. pos += ret;
  780. }
  781. #endif /* CONFIG_SAE */
  782. #endif /* CONFIG_IEEE80211R */
  783. #ifdef CONFIG_IEEE80211W
  784. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  785. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  786. if (ret < 0 || ret >= end - pos)
  787. return pos - buf;
  788. pos += ret;
  789. }
  790. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  791. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  792. if (ret < 0 || ret >= end - pos)
  793. return pos - buf;
  794. pos += ret;
  795. }
  796. #endif /* CONFIG_IEEE80211W */
  797. #ifdef CONFIG_SAE
  798. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
  799. ret = os_snprintf(pos, end - pos, "SAE ");
  800. if (ret < 0 || ret >= end - pos)
  801. return pos - buf;
  802. pos += ret;
  803. }
  804. #endif /* CONFIG_SAE */
  805. ret = os_snprintf(pos, end - pos, "\n");
  806. if (ret < 0 || ret >= end - pos)
  807. return pos - buf;
  808. pos += ret;
  809. }
  810. if (hapd->conf->wpa) {
  811. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  812. wpa_cipher_txt(hapd->conf->wpa_group));
  813. if (ret < 0 || ret >= end - pos)
  814. return pos - buf;
  815. pos += ret;
  816. }
  817. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  818. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  819. if (ret < 0 || ret >= end - pos)
  820. return pos - buf;
  821. pos += ret;
  822. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  823. " ");
  824. if (ret < 0)
  825. return pos - buf;
  826. pos += ret;
  827. ret = os_snprintf(pos, end - pos, "\n");
  828. if (ret < 0 || ret >= end - pos)
  829. return pos - buf;
  830. pos += ret;
  831. }
  832. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  833. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  834. if (ret < 0 || ret >= end - pos)
  835. return pos - buf;
  836. pos += ret;
  837. ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
  838. " ");
  839. if (ret < 0)
  840. return pos - buf;
  841. pos += ret;
  842. ret = os_snprintf(pos, end - pos, "\n");
  843. if (ret < 0 || ret >= end - pos)
  844. return pos - buf;
  845. pos += ret;
  846. }
  847. return pos - buf;
  848. }
  849. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  850. {
  851. char *value;
  852. int ret = 0;
  853. value = os_strchr(cmd, ' ');
  854. if (value == NULL)
  855. return -1;
  856. *value++ = '\0';
  857. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  858. if (0) {
  859. #ifdef CONFIG_WPS_TESTING
  860. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  861. long int val;
  862. val = strtol(value, NULL, 0);
  863. if (val < 0 || val > 0xff) {
  864. ret = -1;
  865. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  866. "wps_version_number %ld", val);
  867. } else {
  868. wps_version_number = val;
  869. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  870. "version %u.%u",
  871. (wps_version_number & 0xf0) >> 4,
  872. wps_version_number & 0x0f);
  873. hostapd_wps_update_ie(hapd);
  874. }
  875. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  876. wps_testing_dummy_cred = atoi(value);
  877. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  878. wps_testing_dummy_cred);
  879. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  880. wps_corrupt_pkhash = atoi(value);
  881. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  882. wps_corrupt_pkhash);
  883. #endif /* CONFIG_WPS_TESTING */
  884. #ifdef CONFIG_INTERWORKING
  885. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  886. int val = atoi(value);
  887. if (val <= 0)
  888. ret = -1;
  889. else
  890. hapd->gas_frag_limit = val;
  891. #endif /* CONFIG_INTERWORKING */
  892. #ifdef CONFIG_TESTING_OPTIONS
  893. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  894. hapd->ext_mgmt_frame_handling = atoi(value);
  895. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  896. hapd->ext_eapol_frame_io = atoi(value);
  897. #endif /* CONFIG_TESTING_OPTIONS */
  898. } else {
  899. struct sta_info *sta;
  900. int vlan_id;
  901. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  902. if (ret)
  903. return ret;
  904. if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
  905. for (sta = hapd->sta_list; sta; sta = sta->next) {
  906. if (hostapd_maclist_found(
  907. hapd->conf->deny_mac,
  908. hapd->conf->num_deny_mac, sta->addr,
  909. &vlan_id) &&
  910. (!vlan_id || vlan_id == sta->vlan_id))
  911. ap_sta_disconnect(
  912. hapd, sta, sta->addr,
  913. WLAN_REASON_UNSPECIFIED);
  914. }
  915. } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
  916. os_strcasecmp(cmd, "accept_mac_file") == 0) {
  917. for (sta = hapd->sta_list; sta; sta = sta->next) {
  918. if (!hostapd_maclist_found(
  919. hapd->conf->accept_mac,
  920. hapd->conf->num_accept_mac,
  921. sta->addr, &vlan_id) ||
  922. (vlan_id && vlan_id != sta->vlan_id))
  923. ap_sta_disconnect(
  924. hapd, sta, sta->addr,
  925. WLAN_REASON_UNSPECIFIED);
  926. }
  927. }
  928. }
  929. return ret;
  930. }
  931. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  932. char *buf, size_t buflen)
  933. {
  934. int res;
  935. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  936. if (os_strcmp(cmd, "version") == 0) {
  937. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  938. if (res < 0 || (unsigned int) res >= buflen)
  939. return -1;
  940. return res;
  941. }
  942. return -1;
  943. }
  944. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  945. {
  946. if (hostapd_enable_iface(iface) < 0) {
  947. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  948. return -1;
  949. }
  950. return 0;
  951. }
  952. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  953. {
  954. if (hostapd_reload_iface(iface) < 0) {
  955. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  956. return -1;
  957. }
  958. return 0;
  959. }
  960. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  961. {
  962. if (hostapd_disable_iface(iface) < 0) {
  963. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  964. return -1;
  965. }
  966. return 0;
  967. }
  968. #ifdef CONFIG_TESTING_OPTIONS
  969. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  970. {
  971. union wpa_event_data data;
  972. char *pos, *param;
  973. enum wpa_event_type event;
  974. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  975. os_memset(&data, 0, sizeof(data));
  976. param = os_strchr(cmd, ' ');
  977. if (param == NULL)
  978. return -1;
  979. *param++ = '\0';
  980. if (os_strcmp(cmd, "DETECTED") == 0)
  981. event = EVENT_DFS_RADAR_DETECTED;
  982. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  983. event = EVENT_DFS_CAC_FINISHED;
  984. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  985. event = EVENT_DFS_CAC_ABORTED;
  986. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  987. event = EVENT_DFS_NOP_FINISHED;
  988. else {
  989. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  990. cmd);
  991. return -1;
  992. }
  993. pos = os_strstr(param, "freq=");
  994. if (pos)
  995. data.dfs_event.freq = atoi(pos + 5);
  996. pos = os_strstr(param, "ht_enabled=1");
  997. if (pos)
  998. data.dfs_event.ht_enabled = 1;
  999. pos = os_strstr(param, "chan_offset=");
  1000. if (pos)
  1001. data.dfs_event.chan_offset = atoi(pos + 12);
  1002. pos = os_strstr(param, "chan_width=");
  1003. if (pos)
  1004. data.dfs_event.chan_width = atoi(pos + 11);
  1005. pos = os_strstr(param, "cf1=");
  1006. if (pos)
  1007. data.dfs_event.cf1 = atoi(pos + 4);
  1008. pos = os_strstr(param, "cf2=");
  1009. if (pos)
  1010. data.dfs_event.cf2 = atoi(pos + 4);
  1011. wpa_supplicant_event(hapd, event, &data);
  1012. return 0;
  1013. }
  1014. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  1015. {
  1016. size_t len;
  1017. u8 *buf;
  1018. int res;
  1019. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  1020. len = os_strlen(cmd);
  1021. if (len & 1)
  1022. return -1;
  1023. len /= 2;
  1024. buf = os_malloc(len);
  1025. if (buf == NULL)
  1026. return -1;
  1027. if (hexstr2bin(cmd, buf, len) < 0) {
  1028. os_free(buf);
  1029. return -1;
  1030. }
  1031. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  1032. os_free(buf);
  1033. return res;
  1034. }
  1035. static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
  1036. {
  1037. char *pos;
  1038. u8 src[ETH_ALEN], *buf;
  1039. int used;
  1040. size_t len;
  1041. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  1042. pos = cmd;
  1043. used = hwaddr_aton2(pos, src);
  1044. if (used < 0)
  1045. return -1;
  1046. pos += used;
  1047. while (*pos == ' ')
  1048. pos++;
  1049. len = os_strlen(pos);
  1050. if (len & 1)
  1051. return -1;
  1052. len /= 2;
  1053. buf = os_malloc(len);
  1054. if (buf == NULL)
  1055. return -1;
  1056. if (hexstr2bin(pos, buf, len) < 0) {
  1057. os_free(buf);
  1058. return -1;
  1059. }
  1060. ieee802_1x_receive(hapd, src, buf, len);
  1061. os_free(buf);
  1062. return 0;
  1063. }
  1064. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  1065. {
  1066. size_t i;
  1067. u32 sum = 0;
  1068. const u16 *pos = buf;
  1069. for (i = 0; i < len / 2; i++)
  1070. sum += *pos++;
  1071. while (sum >> 16)
  1072. sum = (sum & 0xffff) + (sum >> 16);
  1073. return sum ^ 0xffff;
  1074. }
  1075. #define HWSIM_PACKETLEN 1500
  1076. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  1077. void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  1078. size_t len)
  1079. {
  1080. struct hostapd_data *hapd = ctx;
  1081. const struct ether_header *eth;
  1082. const struct iphdr *ip;
  1083. const u8 *pos;
  1084. unsigned int i;
  1085. if (len != HWSIM_PACKETLEN)
  1086. return;
  1087. eth = (const struct ether_header *) buf;
  1088. ip = (const struct iphdr *) (eth + 1);
  1089. pos = (const u8 *) (ip + 1);
  1090. if (ip->ihl != 5 || ip->version != 4 ||
  1091. ntohs(ip->tot_len) != HWSIM_IP_LEN)
  1092. return;
  1093. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
  1094. if (*pos != (u8) i)
  1095. return;
  1096. pos++;
  1097. }
  1098. wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  1099. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  1100. }
  1101. static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
  1102. char *cmd)
  1103. {
  1104. int enabled = atoi(cmd);
  1105. if (!enabled) {
  1106. if (hapd->l2_test) {
  1107. l2_packet_deinit(hapd->l2_test);
  1108. hapd->l2_test = NULL;
  1109. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  1110. "test data: Disabled");
  1111. }
  1112. return 0;
  1113. }
  1114. if (hapd->l2_test)
  1115. return 0;
  1116. hapd->l2_test = l2_packet_init(hapd->conf->iface, hapd->own_addr,
  1117. ETHERTYPE_IP, hostapd_data_test_rx,
  1118. hapd, 1);
  1119. if (hapd->l2_test == NULL)
  1120. return -1;
  1121. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
  1122. return 0;
  1123. }
  1124. static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
  1125. {
  1126. u8 dst[ETH_ALEN], src[ETH_ALEN];
  1127. char *pos;
  1128. int used;
  1129. long int val;
  1130. u8 tos;
  1131. u8 buf[HWSIM_PACKETLEN];
  1132. struct ether_header *eth;
  1133. struct iphdr *ip;
  1134. u8 *dpos;
  1135. unsigned int i;
  1136. if (hapd->l2_test == NULL)
  1137. return -1;
  1138. /* format: <dst> <src> <tos> */
  1139. pos = cmd;
  1140. used = hwaddr_aton2(pos, dst);
  1141. if (used < 0)
  1142. return -1;
  1143. pos += used;
  1144. while (*pos == ' ')
  1145. pos++;
  1146. used = hwaddr_aton2(pos, src);
  1147. if (used < 0)
  1148. return -1;
  1149. pos += used;
  1150. val = strtol(pos, NULL, 0);
  1151. if (val < 0 || val > 0xff)
  1152. return -1;
  1153. tos = val;
  1154. eth = (struct ether_header *) buf;
  1155. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  1156. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  1157. eth->ether_type = htons(ETHERTYPE_IP);
  1158. ip = (struct iphdr *) (eth + 1);
  1159. os_memset(ip, 0, sizeof(*ip));
  1160. ip->ihl = 5;
  1161. ip->version = 4;
  1162. ip->ttl = 64;
  1163. ip->tos = tos;
  1164. ip->tot_len = htons(HWSIM_IP_LEN);
  1165. ip->protocol = 1;
  1166. ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
  1167. ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
  1168. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  1169. dpos = (u8 *) (ip + 1);
  1170. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  1171. *dpos++ = i;
  1172. if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, buf,
  1173. HWSIM_PACKETLEN) < 0)
  1174. return -1;
  1175. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
  1176. " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  1177. return 0;
  1178. }
  1179. #endif /* CONFIG_TESTING_OPTIONS */
  1180. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1181. char *pos)
  1182. {
  1183. #ifdef NEED_AP_MLME
  1184. struct csa_settings settings;
  1185. int ret;
  1186. unsigned int i;
  1187. ret = hostapd_parse_csa_settings(pos, &settings);
  1188. if (ret)
  1189. return ret;
  1190. for (i = 0; i < iface->num_bss; i++) {
  1191. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1192. if (ret) {
  1193. /* FIX: What do we do if CSA fails in the middle of
  1194. * submitting multi-BSS CSA requests? */
  1195. return ret;
  1196. }
  1197. }
  1198. return 0;
  1199. #else /* NEED_AP_MLME */
  1200. return -1;
  1201. #endif /* NEED_AP_MLME */
  1202. }
  1203. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1204. int reply_size, const char *param)
  1205. {
  1206. #ifdef RADIUS_SERVER
  1207. if (os_strcmp(param, "radius_server") == 0) {
  1208. return radius_server_get_mib(hapd->radius_srv, reply,
  1209. reply_size);
  1210. }
  1211. #endif /* RADIUS_SERVER */
  1212. return -1;
  1213. }
  1214. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1215. char *buf, size_t buflen)
  1216. {
  1217. int ret;
  1218. char *pos;
  1219. u8 *data = NULL;
  1220. unsigned int vendor_id, subcmd;
  1221. struct wpabuf *reply;
  1222. size_t data_len = 0;
  1223. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1224. vendor_id = strtoul(cmd, &pos, 16);
  1225. if (!isblank(*pos))
  1226. return -EINVAL;
  1227. subcmd = strtoul(pos, &pos, 10);
  1228. if (*pos != '\0') {
  1229. if (!isblank(*pos++))
  1230. return -EINVAL;
  1231. data_len = os_strlen(pos);
  1232. }
  1233. if (data_len) {
  1234. data_len /= 2;
  1235. data = os_malloc(data_len);
  1236. if (!data)
  1237. return -ENOBUFS;
  1238. if (hexstr2bin(pos, data, data_len)) {
  1239. wpa_printf(MSG_DEBUG,
  1240. "Vendor command: wrong parameter format");
  1241. os_free(data);
  1242. return -EINVAL;
  1243. }
  1244. }
  1245. reply = wpabuf_alloc((buflen - 1) / 2);
  1246. if (!reply) {
  1247. os_free(data);
  1248. return -ENOBUFS;
  1249. }
  1250. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1251. reply);
  1252. if (ret == 0)
  1253. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1254. wpabuf_len(reply));
  1255. wpabuf_free(reply);
  1256. os_free(data);
  1257. return ret;
  1258. }
  1259. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  1260. void *sock_ctx)
  1261. {
  1262. struct hostapd_data *hapd = eloop_ctx;
  1263. char buf[4096];
  1264. int res;
  1265. struct sockaddr_un from;
  1266. socklen_t fromlen = sizeof(from);
  1267. char *reply;
  1268. const int reply_size = 4096;
  1269. int reply_len;
  1270. int level = MSG_DEBUG;
  1271. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1272. (struct sockaddr *) &from, &fromlen);
  1273. if (res < 0) {
  1274. perror("recvfrom(ctrl_iface)");
  1275. return;
  1276. }
  1277. buf[res] = '\0';
  1278. if (os_strcmp(buf, "PING") == 0)
  1279. level = MSG_EXCESSIVE;
  1280. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  1281. reply = os_malloc(reply_size);
  1282. if (reply == NULL) {
  1283. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  1284. fromlen) < 0) {
  1285. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1286. strerror(errno));
  1287. }
  1288. return;
  1289. }
  1290. os_memcpy(reply, "OK\n", 3);
  1291. reply_len = 3;
  1292. if (os_strcmp(buf, "PING") == 0) {
  1293. os_memcpy(reply, "PONG\n", 5);
  1294. reply_len = 5;
  1295. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1296. if (wpa_debug_reopen_file() < 0)
  1297. reply_len = -1;
  1298. } else if (os_strcmp(buf, "STATUS") == 0) {
  1299. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  1300. reply_size);
  1301. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  1302. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  1303. } else if (os_strcmp(buf, "MIB") == 0) {
  1304. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  1305. if (reply_len >= 0) {
  1306. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  1307. reply_size - reply_len);
  1308. if (res < 0)
  1309. reply_len = -1;
  1310. else
  1311. reply_len += res;
  1312. }
  1313. if (reply_len >= 0) {
  1314. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  1315. reply_size - reply_len);
  1316. if (res < 0)
  1317. reply_len = -1;
  1318. else
  1319. reply_len += res;
  1320. }
  1321. #ifndef CONFIG_NO_RADIUS
  1322. if (reply_len >= 0) {
  1323. res = radius_client_get_mib(hapd->radius,
  1324. reply + reply_len,
  1325. reply_size - reply_len);
  1326. if (res < 0)
  1327. reply_len = -1;
  1328. else
  1329. reply_len += res;
  1330. }
  1331. #endif /* CONFIG_NO_RADIUS */
  1332. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  1333. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  1334. buf + 4);
  1335. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1336. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  1337. reply_size);
  1338. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1339. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  1340. reply_size);
  1341. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1342. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  1343. reply_size);
  1344. } else if (os_strcmp(buf, "ATTACH") == 0) {
  1345. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  1346. reply_len = -1;
  1347. } else if (os_strcmp(buf, "DETACH") == 0) {
  1348. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  1349. reply_len = -1;
  1350. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  1351. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  1352. buf + 6))
  1353. reply_len = -1;
  1354. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  1355. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  1356. reply_len = -1;
  1357. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  1358. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  1359. reply_len = -1;
  1360. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  1361. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  1362. reply_len = -1;
  1363. #ifdef CONFIG_IEEE80211W
  1364. #ifdef NEED_AP_MLME
  1365. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  1366. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  1367. reply_len = -1;
  1368. #endif /* NEED_AP_MLME */
  1369. #endif /* CONFIG_IEEE80211W */
  1370. #ifdef CONFIG_WPS
  1371. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1372. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1373. reply_len = -1;
  1374. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1375. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1376. hapd, buf + 14, reply, reply_size);
  1377. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1378. if (hostapd_wps_button_pushed(hapd, NULL))
  1379. reply_len = -1;
  1380. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1381. if (hostapd_wps_cancel(hapd))
  1382. reply_len = -1;
  1383. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1384. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1385. reply, reply_size);
  1386. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1387. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1388. reply_len = -1;
  1389. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1390. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1391. reply_size);
  1392. #ifdef CONFIG_WPS_NFC
  1393. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1394. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1395. reply_len = -1;
  1396. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1397. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1398. hapd, buf + 21, reply, reply_size);
  1399. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1400. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1401. hapd, buf + 14, reply, reply_size);
  1402. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1403. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1404. hapd, buf + 21, reply, reply_size);
  1405. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1406. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1407. reply_len = -1;
  1408. #endif /* CONFIG_WPS_NFC */
  1409. #endif /* CONFIG_WPS */
  1410. #ifdef CONFIG_INTERWORKING
  1411. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1412. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1413. reply_len = -1;
  1414. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1415. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1416. reply_len = -1;
  1417. #endif /* CONFIG_INTERWORKING */
  1418. #ifdef CONFIG_HS20
  1419. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  1420. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  1421. reply_len = -1;
  1422. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  1423. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  1424. reply_len = -1;
  1425. #endif /* CONFIG_HS20 */
  1426. #ifdef CONFIG_WNM
  1427. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1428. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1429. reply_len = -1;
  1430. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1431. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1432. reply_len = -1;
  1433. #endif /* CONFIG_WNM */
  1434. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1435. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1436. reply_size);
  1437. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1438. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1439. reply_len = -1;
  1440. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1441. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1442. reply_size);
  1443. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1444. if (hostapd_ctrl_iface_enable(hapd->iface))
  1445. reply_len = -1;
  1446. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1447. if (hostapd_ctrl_iface_reload(hapd->iface))
  1448. reply_len = -1;
  1449. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1450. if (hostapd_ctrl_iface_disable(hapd->iface))
  1451. reply_len = -1;
  1452. #ifdef CONFIG_TESTING_OPTIONS
  1453. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1454. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1455. reply_len = -1;
  1456. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1457. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1458. reply_len = -1;
  1459. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  1460. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  1461. reply_len = -1;
  1462. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  1463. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  1464. reply_len = -1;
  1465. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  1466. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  1467. reply_len = -1;
  1468. #endif /* CONFIG_TESTING_OPTIONS */
  1469. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1470. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  1471. reply_len = -1;
  1472. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  1473. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  1474. reply_size);
  1475. } else {
  1476. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1477. reply_len = 16;
  1478. }
  1479. if (reply_len < 0) {
  1480. os_memcpy(reply, "FAIL\n", 5);
  1481. reply_len = 5;
  1482. }
  1483. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  1484. fromlen) < 0) {
  1485. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1486. strerror(errno));
  1487. }
  1488. os_free(reply);
  1489. }
  1490. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1491. {
  1492. char *buf;
  1493. size_t len;
  1494. if (hapd->conf->ctrl_interface == NULL)
  1495. return NULL;
  1496. len = os_strlen(hapd->conf->ctrl_interface) +
  1497. os_strlen(hapd->conf->iface) + 2;
  1498. buf = os_malloc(len);
  1499. if (buf == NULL)
  1500. return NULL;
  1501. os_snprintf(buf, len, "%s/%s",
  1502. hapd->conf->ctrl_interface, hapd->conf->iface);
  1503. buf[len - 1] = '\0';
  1504. return buf;
  1505. }
  1506. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  1507. const char *txt, size_t len)
  1508. {
  1509. struct hostapd_data *hapd = ctx;
  1510. if (hapd == NULL)
  1511. return;
  1512. hostapd_ctrl_iface_send(hapd, level, txt, len);
  1513. }
  1514. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1515. {
  1516. struct sockaddr_un addr;
  1517. int s = -1;
  1518. char *fname = NULL;
  1519. if (hapd->ctrl_sock > -1) {
  1520. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1521. return 0;
  1522. }
  1523. if (hapd->conf->ctrl_interface == NULL)
  1524. return 0;
  1525. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1526. if (errno == EEXIST) {
  1527. wpa_printf(MSG_DEBUG, "Using existing control "
  1528. "interface directory.");
  1529. } else {
  1530. perror("mkdir[ctrl_interface]");
  1531. goto fail;
  1532. }
  1533. }
  1534. if (hapd->conf->ctrl_interface_gid_set &&
  1535. chown(hapd->conf->ctrl_interface, -1,
  1536. hapd->conf->ctrl_interface_gid) < 0) {
  1537. perror("chown[ctrl_interface]");
  1538. return -1;
  1539. }
  1540. if (!hapd->conf->ctrl_interface_gid_set &&
  1541. hapd->iface->interfaces->ctrl_iface_group &&
  1542. chown(hapd->conf->ctrl_interface, -1,
  1543. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1544. perror("chown[ctrl_interface]");
  1545. return -1;
  1546. }
  1547. #ifdef ANDROID
  1548. /*
  1549. * Android is using umask 0077 which would leave the control interface
  1550. * directory without group access. This breaks things since Wi-Fi
  1551. * framework assumes that this directory can be accessed by other
  1552. * applications in the wifi group. Fix this by adding group access even
  1553. * if umask value would prevent this.
  1554. */
  1555. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1556. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1557. strerror(errno));
  1558. /* Try to continue anyway */
  1559. }
  1560. #endif /* ANDROID */
  1561. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1562. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1563. goto fail;
  1564. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1565. if (s < 0) {
  1566. perror("socket(PF_UNIX)");
  1567. goto fail;
  1568. }
  1569. os_memset(&addr, 0, sizeof(addr));
  1570. #ifdef __FreeBSD__
  1571. addr.sun_len = sizeof(addr);
  1572. #endif /* __FreeBSD__ */
  1573. addr.sun_family = AF_UNIX;
  1574. fname = hostapd_ctrl_iface_path(hapd);
  1575. if (fname == NULL)
  1576. goto fail;
  1577. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1578. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1579. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1580. strerror(errno));
  1581. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1582. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1583. " allow connections - assuming it was left"
  1584. "over from forced program termination");
  1585. if (unlink(fname) < 0) {
  1586. perror("unlink[ctrl_iface]");
  1587. wpa_printf(MSG_ERROR, "Could not unlink "
  1588. "existing ctrl_iface socket '%s'",
  1589. fname);
  1590. goto fail;
  1591. }
  1592. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1593. 0) {
  1594. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1595. goto fail;
  1596. }
  1597. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1598. "ctrl_iface socket '%s'", fname);
  1599. } else {
  1600. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1601. "be in use - cannot override it");
  1602. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1603. "not used anymore", fname);
  1604. os_free(fname);
  1605. fname = NULL;
  1606. goto fail;
  1607. }
  1608. }
  1609. if (hapd->conf->ctrl_interface_gid_set &&
  1610. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1611. perror("chown[ctrl_interface/ifname]");
  1612. goto fail;
  1613. }
  1614. if (!hapd->conf->ctrl_interface_gid_set &&
  1615. hapd->iface->interfaces->ctrl_iface_group &&
  1616. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1617. perror("chown[ctrl_interface/ifname]");
  1618. goto fail;
  1619. }
  1620. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1621. perror("chmod[ctrl_interface/ifname]");
  1622. goto fail;
  1623. }
  1624. os_free(fname);
  1625. hapd->ctrl_sock = s;
  1626. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1627. NULL);
  1628. hapd->msg_ctx = hapd;
  1629. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1630. return 0;
  1631. fail:
  1632. if (s >= 0)
  1633. close(s);
  1634. if (fname) {
  1635. unlink(fname);
  1636. os_free(fname);
  1637. }
  1638. return -1;
  1639. }
  1640. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1641. {
  1642. struct wpa_ctrl_dst *dst, *prev;
  1643. if (hapd->ctrl_sock > -1) {
  1644. char *fname;
  1645. eloop_unregister_read_sock(hapd->ctrl_sock);
  1646. close(hapd->ctrl_sock);
  1647. hapd->ctrl_sock = -1;
  1648. fname = hostapd_ctrl_iface_path(hapd);
  1649. if (fname)
  1650. unlink(fname);
  1651. os_free(fname);
  1652. if (hapd->conf->ctrl_interface &&
  1653. rmdir(hapd->conf->ctrl_interface) < 0) {
  1654. if (errno == ENOTEMPTY) {
  1655. wpa_printf(MSG_DEBUG, "Control interface "
  1656. "directory not empty - leaving it "
  1657. "behind");
  1658. } else {
  1659. wpa_printf(MSG_ERROR,
  1660. "rmdir[ctrl_interface=%s]: %s",
  1661. hapd->conf->ctrl_interface,
  1662. strerror(errno));
  1663. }
  1664. }
  1665. }
  1666. dst = hapd->ctrl_dst;
  1667. while (dst) {
  1668. prev = dst;
  1669. dst = dst->next;
  1670. os_free(prev);
  1671. }
  1672. #ifdef CONFIG_TESTING_OPTIONS
  1673. l2_packet_deinit(hapd->l2_test);
  1674. hapd->l2_test = NULL;
  1675. #endif /* CONFIG_TESTING_OPTIONS */
  1676. }
  1677. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1678. char *buf)
  1679. {
  1680. if (hostapd_add_iface(interfaces, buf) < 0) {
  1681. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1682. return -1;
  1683. }
  1684. return 0;
  1685. }
  1686. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1687. char *buf)
  1688. {
  1689. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1690. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1691. return -1;
  1692. }
  1693. return 0;
  1694. }
  1695. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  1696. {
  1697. #ifdef CONFIG_WPS_TESTING
  1698. wps_version_number = 0x20;
  1699. wps_testing_dummy_cred = 0;
  1700. wps_corrupt_pkhash = 0;
  1701. #endif /* CONFIG_WPS_TESTING */
  1702. }
  1703. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1704. void *sock_ctx)
  1705. {
  1706. void *interfaces = eloop_ctx;
  1707. char buf[256];
  1708. int res;
  1709. struct sockaddr_un from;
  1710. socklen_t fromlen = sizeof(from);
  1711. char reply[24];
  1712. int reply_len;
  1713. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1714. (struct sockaddr *) &from, &fromlen);
  1715. if (res < 0) {
  1716. perror("recvfrom(ctrl_iface)");
  1717. return;
  1718. }
  1719. buf[res] = '\0';
  1720. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  1721. os_memcpy(reply, "OK\n", 3);
  1722. reply_len = 3;
  1723. if (os_strcmp(buf, "PING") == 0) {
  1724. os_memcpy(reply, "PONG\n", 5);
  1725. reply_len = 5;
  1726. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1727. if (wpa_debug_reopen_file() < 0)
  1728. reply_len = -1;
  1729. } else if (os_strcmp(buf, "FLUSH") == 0) {
  1730. hostapd_ctrl_iface_flush(interfaces);
  1731. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1732. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1733. reply_len = -1;
  1734. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1735. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1736. reply_len = -1;
  1737. #ifdef CONFIG_MODULE_TESTS
  1738. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  1739. int hapd_module_tests(void);
  1740. if (hapd_module_tests() < 0)
  1741. reply_len = -1;
  1742. #endif /* CONFIG_MODULE_TESTS */
  1743. } else {
  1744. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1745. "ignored");
  1746. reply_len = -1;
  1747. }
  1748. if (reply_len < 0) {
  1749. os_memcpy(reply, "FAIL\n", 5);
  1750. reply_len = 5;
  1751. }
  1752. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  1753. fromlen) < 0) {
  1754. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1755. strerror(errno));
  1756. }
  1757. }
  1758. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1759. {
  1760. char *buf;
  1761. size_t len;
  1762. if (interface->global_iface_path == NULL)
  1763. return NULL;
  1764. len = os_strlen(interface->global_iface_path) +
  1765. os_strlen(interface->global_iface_name) + 2;
  1766. buf = os_malloc(len);
  1767. if (buf == NULL)
  1768. return NULL;
  1769. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1770. interface->global_iface_name);
  1771. buf[len - 1] = '\0';
  1772. return buf;
  1773. }
  1774. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1775. {
  1776. struct sockaddr_un addr;
  1777. int s = -1;
  1778. char *fname = NULL;
  1779. if (interface->global_iface_path == NULL) {
  1780. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1781. return 0;
  1782. }
  1783. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1784. if (errno == EEXIST) {
  1785. wpa_printf(MSG_DEBUG, "Using existing control "
  1786. "interface directory.");
  1787. } else {
  1788. perror("mkdir[ctrl_interface]");
  1789. goto fail;
  1790. }
  1791. } else if (interface->ctrl_iface_group &&
  1792. chown(interface->global_iface_path, -1,
  1793. interface->ctrl_iface_group) < 0) {
  1794. perror("chown[ctrl_interface]");
  1795. goto fail;
  1796. }
  1797. if (os_strlen(interface->global_iface_path) + 1 +
  1798. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1799. goto fail;
  1800. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1801. if (s < 0) {
  1802. perror("socket(PF_UNIX)");
  1803. goto fail;
  1804. }
  1805. os_memset(&addr, 0, sizeof(addr));
  1806. #ifdef __FreeBSD__
  1807. addr.sun_len = sizeof(addr);
  1808. #endif /* __FreeBSD__ */
  1809. addr.sun_family = AF_UNIX;
  1810. fname = hostapd_global_ctrl_iface_path(interface);
  1811. if (fname == NULL)
  1812. goto fail;
  1813. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1814. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1815. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1816. strerror(errno));
  1817. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1818. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1819. " allow connections - assuming it was left"
  1820. "over from forced program termination");
  1821. if (unlink(fname) < 0) {
  1822. perror("unlink[ctrl_iface]");
  1823. wpa_printf(MSG_ERROR, "Could not unlink "
  1824. "existing ctrl_iface socket '%s'",
  1825. fname);
  1826. goto fail;
  1827. }
  1828. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1829. 0) {
  1830. perror("bind(PF_UNIX)");
  1831. goto fail;
  1832. }
  1833. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1834. "ctrl_iface socket '%s'", fname);
  1835. } else {
  1836. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1837. "be in use - cannot override it");
  1838. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1839. "not used anymore", fname);
  1840. os_free(fname);
  1841. fname = NULL;
  1842. goto fail;
  1843. }
  1844. }
  1845. if (interface->ctrl_iface_group &&
  1846. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1847. perror("chown[ctrl_interface]");
  1848. goto fail;
  1849. }
  1850. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1851. perror("chmod[ctrl_interface/ifname]");
  1852. goto fail;
  1853. }
  1854. os_free(fname);
  1855. interface->global_ctrl_sock = s;
  1856. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1857. interface, NULL);
  1858. return 0;
  1859. fail:
  1860. if (s >= 0)
  1861. close(s);
  1862. if (fname) {
  1863. unlink(fname);
  1864. os_free(fname);
  1865. }
  1866. return -1;
  1867. }
  1868. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1869. {
  1870. char *fname = NULL;
  1871. if (interfaces->global_ctrl_sock > -1) {
  1872. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1873. close(interfaces->global_ctrl_sock);
  1874. interfaces->global_ctrl_sock = -1;
  1875. fname = hostapd_global_ctrl_iface_path(interfaces);
  1876. if (fname) {
  1877. unlink(fname);
  1878. os_free(fname);
  1879. }
  1880. if (interfaces->global_iface_path &&
  1881. rmdir(interfaces->global_iface_path) < 0) {
  1882. if (errno == ENOTEMPTY) {
  1883. wpa_printf(MSG_DEBUG, "Control interface "
  1884. "directory not empty - leaving it "
  1885. "behind");
  1886. } else {
  1887. wpa_printf(MSG_ERROR,
  1888. "rmdir[ctrl_interface=%s]: %s",
  1889. interfaces->global_iface_path,
  1890. strerror(errno));
  1891. }
  1892. }
  1893. os_free(interfaces->global_iface_path);
  1894. interfaces->global_iface_path = NULL;
  1895. }
  1896. }
  1897. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1898. const char *buf, size_t len)
  1899. {
  1900. struct wpa_ctrl_dst *dst, *next;
  1901. struct msghdr msg;
  1902. int idx;
  1903. struct iovec io[2];
  1904. char levelstr[10];
  1905. dst = hapd->ctrl_dst;
  1906. if (hapd->ctrl_sock < 0 || dst == NULL)
  1907. return;
  1908. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1909. io[0].iov_base = levelstr;
  1910. io[0].iov_len = os_strlen(levelstr);
  1911. io[1].iov_base = (char *) buf;
  1912. io[1].iov_len = len;
  1913. os_memset(&msg, 0, sizeof(msg));
  1914. msg.msg_iov = io;
  1915. msg.msg_iovlen = 2;
  1916. idx = 0;
  1917. while (dst) {
  1918. next = dst->next;
  1919. if (level >= dst->debug_level) {
  1920. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1921. (u8 *) dst->addr.sun_path, dst->addrlen -
  1922. offsetof(struct sockaddr_un, sun_path));
  1923. msg.msg_name = &dst->addr;
  1924. msg.msg_namelen = dst->addrlen;
  1925. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1926. int _errno = errno;
  1927. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1928. "%d - %s",
  1929. idx, errno, strerror(errno));
  1930. dst->errors++;
  1931. if (dst->errors > 10 || _errno == ENOENT) {
  1932. hostapd_ctrl_iface_detach(
  1933. hapd, &dst->addr,
  1934. dst->addrlen);
  1935. }
  1936. } else
  1937. dst->errors = 0;
  1938. }
  1939. idx++;
  1940. dst = next;
  1941. }
  1942. }
  1943. #endif /* CONFIG_NATIVE_WINDOWS */