ctrl_iface.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2015, 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 "crypto/tls.h"
  22. #include "drivers/driver.h"
  23. #include "eapol_auth/eapol_auth_sm.h"
  24. #include "radius/radius_client.h"
  25. #include "radius/radius_server.h"
  26. #include "l2_packet/l2_packet.h"
  27. #include "ap/hostapd.h"
  28. #include "ap/ap_config.h"
  29. #include "ap/ieee802_1x.h"
  30. #include "ap/wpa_auth.h"
  31. #include "ap/ieee802_11.h"
  32. #include "ap/sta_info.h"
  33. #include "ap/wps_hostapd.h"
  34. #include "ap/ctrl_iface_ap.h"
  35. #include "ap/ap_drv_ops.h"
  36. #include "ap/hs20.h"
  37. #include "ap/wnm_ap.h"
  38. #include "ap/wpa_auth.h"
  39. #include "ap/beacon.h"
  40. #include "wps/wps_defs.h"
  41. #include "wps/wps.h"
  42. #include "fst/fst_ctrl_iface.h"
  43. #include "config_file.h"
  44. #include "ctrl_iface.h"
  45. #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
  46. struct wpa_ctrl_dst {
  47. struct wpa_ctrl_dst *next;
  48. struct sockaddr_un addr;
  49. socklen_t addrlen;
  50. int debug_level;
  51. int errors;
  52. };
  53. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  54. enum wpa_msg_type type,
  55. const char *buf, size_t len);
  56. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  57. struct sockaddr_un *from,
  58. socklen_t fromlen)
  59. {
  60. struct wpa_ctrl_dst *dst;
  61. dst = os_zalloc(sizeof(*dst));
  62. if (dst == NULL)
  63. return -1;
  64. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  65. dst->addrlen = fromlen;
  66. dst->debug_level = MSG_INFO;
  67. dst->next = hapd->ctrl_dst;
  68. hapd->ctrl_dst = dst;
  69. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  70. (u8 *) from->sun_path,
  71. fromlen - offsetof(struct sockaddr_un, sun_path));
  72. return 0;
  73. }
  74. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  75. struct sockaddr_un *from,
  76. socklen_t fromlen)
  77. {
  78. struct wpa_ctrl_dst *dst, *prev = NULL;
  79. dst = hapd->ctrl_dst;
  80. while (dst) {
  81. if (fromlen == dst->addrlen &&
  82. os_memcmp(from->sun_path, dst->addr.sun_path,
  83. fromlen - offsetof(struct sockaddr_un, sun_path))
  84. == 0) {
  85. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  86. (u8 *) from->sun_path,
  87. fromlen -
  88. offsetof(struct sockaddr_un, sun_path));
  89. if (prev == NULL)
  90. hapd->ctrl_dst = dst->next;
  91. else
  92. prev->next = dst->next;
  93. os_free(dst);
  94. return 0;
  95. }
  96. prev = dst;
  97. dst = dst->next;
  98. }
  99. return -1;
  100. }
  101. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  102. struct sockaddr_un *from,
  103. socklen_t fromlen,
  104. char *level)
  105. {
  106. struct wpa_ctrl_dst *dst;
  107. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  108. dst = hapd->ctrl_dst;
  109. while (dst) {
  110. if (fromlen == dst->addrlen &&
  111. os_memcmp(from->sun_path, dst->addr.sun_path,
  112. fromlen - offsetof(struct sockaddr_un, sun_path))
  113. == 0) {
  114. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  115. "level", (u8 *) from->sun_path, fromlen -
  116. offsetof(struct sockaddr_un, sun_path));
  117. dst->debug_level = atoi(level);
  118. return 0;
  119. }
  120. dst = dst->next;
  121. }
  122. return -1;
  123. }
  124. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  125. const char *txtaddr)
  126. {
  127. u8 addr[ETH_ALEN];
  128. struct sta_info *sta;
  129. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  130. if (hwaddr_aton(txtaddr, addr))
  131. return -1;
  132. sta = ap_get_sta(hapd, addr);
  133. if (sta)
  134. return 0;
  135. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  136. "notification", MAC2STR(addr));
  137. sta = ap_sta_add(hapd, addr);
  138. if (sta == NULL)
  139. return -1;
  140. hostapd_new_assoc_sta(hapd, sta, 0);
  141. return 0;
  142. }
  143. #ifdef CONFIG_IEEE80211W
  144. #ifdef NEED_AP_MLME
  145. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  146. const char *txtaddr)
  147. {
  148. u8 addr[ETH_ALEN];
  149. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  150. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  151. if (hwaddr_aton(txtaddr, addr) ||
  152. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  153. return -1;
  154. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  155. return 0;
  156. }
  157. #endif /* NEED_AP_MLME */
  158. #endif /* CONFIG_IEEE80211W */
  159. #ifdef CONFIG_WPS
  160. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  161. {
  162. char *pin = os_strchr(txt, ' ');
  163. char *timeout_txt;
  164. int timeout;
  165. u8 addr_buf[ETH_ALEN], *addr = NULL;
  166. char *pos;
  167. if (pin == NULL)
  168. return -1;
  169. *pin++ = '\0';
  170. timeout_txt = os_strchr(pin, ' ');
  171. if (timeout_txt) {
  172. *timeout_txt++ = '\0';
  173. timeout = atoi(timeout_txt);
  174. pos = os_strchr(timeout_txt, ' ');
  175. if (pos) {
  176. *pos++ = '\0';
  177. if (hwaddr_aton(pos, addr_buf) == 0)
  178. addr = addr_buf;
  179. }
  180. } else
  181. timeout = 0;
  182. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  183. }
  184. static int hostapd_ctrl_iface_wps_check_pin(
  185. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  186. {
  187. char pin[9];
  188. size_t len;
  189. char *pos;
  190. int ret;
  191. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  192. (u8 *) cmd, os_strlen(cmd));
  193. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  194. if (*pos < '0' || *pos > '9')
  195. continue;
  196. pin[len++] = *pos;
  197. if (len == 9) {
  198. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  199. return -1;
  200. }
  201. }
  202. if (len != 4 && len != 8) {
  203. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  204. return -1;
  205. }
  206. pin[len] = '\0';
  207. if (len == 8) {
  208. unsigned int pin_val;
  209. pin_val = atoi(pin);
  210. if (!wps_pin_valid(pin_val)) {
  211. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  212. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  213. if (os_snprintf_error(buflen, ret))
  214. return -1;
  215. return ret;
  216. }
  217. }
  218. ret = os_snprintf(buf, buflen, "%s", pin);
  219. if (os_snprintf_error(buflen, ret))
  220. return -1;
  221. return ret;
  222. }
  223. #ifdef CONFIG_WPS_NFC
  224. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  225. char *pos)
  226. {
  227. size_t len;
  228. struct wpabuf *buf;
  229. int ret;
  230. len = os_strlen(pos);
  231. if (len & 0x01)
  232. return -1;
  233. len /= 2;
  234. buf = wpabuf_alloc(len);
  235. if (buf == NULL)
  236. return -1;
  237. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  238. wpabuf_free(buf);
  239. return -1;
  240. }
  241. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  242. wpabuf_free(buf);
  243. return ret;
  244. }
  245. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  246. char *cmd, char *reply,
  247. size_t max_len)
  248. {
  249. int ndef;
  250. struct wpabuf *buf;
  251. int res;
  252. if (os_strcmp(cmd, "WPS") == 0)
  253. ndef = 0;
  254. else if (os_strcmp(cmd, "NDEF") == 0)
  255. ndef = 1;
  256. else
  257. return -1;
  258. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  259. if (buf == NULL)
  260. return -1;
  261. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  262. wpabuf_len(buf));
  263. reply[res++] = '\n';
  264. reply[res] = '\0';
  265. wpabuf_free(buf);
  266. return res;
  267. }
  268. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  269. char *reply, size_t max_len,
  270. int ndef)
  271. {
  272. struct wpabuf *buf;
  273. int res;
  274. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  275. if (buf == NULL)
  276. return -1;
  277. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  278. wpabuf_len(buf));
  279. reply[res++] = '\n';
  280. reply[res] = '\0';
  281. wpabuf_free(buf);
  282. return res;
  283. }
  284. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  285. char *cmd, char *reply,
  286. size_t max_len)
  287. {
  288. if (os_strcmp(cmd, "WPS") == 0)
  289. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  290. max_len, 0);
  291. if (os_strcmp(cmd, "NDEF") == 0)
  292. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  293. max_len, 1);
  294. if (os_strcmp(cmd, "enable") == 0)
  295. return hostapd_wps_nfc_token_enable(hapd);
  296. if (os_strcmp(cmd, "disable") == 0) {
  297. hostapd_wps_nfc_token_disable(hapd);
  298. return 0;
  299. }
  300. return -1;
  301. }
  302. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  303. char *cmd, char *reply,
  304. size_t max_len)
  305. {
  306. struct wpabuf *buf;
  307. int res;
  308. char *pos;
  309. int ndef;
  310. pos = os_strchr(cmd, ' ');
  311. if (pos == NULL)
  312. return -1;
  313. *pos++ = '\0';
  314. if (os_strcmp(cmd, "WPS") == 0)
  315. ndef = 0;
  316. else if (os_strcmp(cmd, "NDEF") == 0)
  317. ndef = 1;
  318. else
  319. return -1;
  320. if (os_strcmp(pos, "WPS-CR") == 0)
  321. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  322. else
  323. buf = NULL;
  324. if (buf == NULL)
  325. return -1;
  326. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  327. wpabuf_len(buf));
  328. reply[res++] = '\n';
  329. reply[res] = '\0';
  330. wpabuf_free(buf);
  331. return res;
  332. }
  333. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  334. char *cmd)
  335. {
  336. size_t len;
  337. struct wpabuf *req, *sel;
  338. int ret;
  339. char *pos, *role, *type, *pos2;
  340. role = cmd;
  341. pos = os_strchr(role, ' ');
  342. if (pos == NULL)
  343. return -1;
  344. *pos++ = '\0';
  345. type = pos;
  346. pos = os_strchr(type, ' ');
  347. if (pos == NULL)
  348. return -1;
  349. *pos++ = '\0';
  350. pos2 = os_strchr(pos, ' ');
  351. if (pos2 == NULL)
  352. return -1;
  353. *pos2++ = '\0';
  354. len = os_strlen(pos);
  355. if (len & 0x01)
  356. return -1;
  357. len /= 2;
  358. req = wpabuf_alloc(len);
  359. if (req == NULL)
  360. return -1;
  361. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  362. wpabuf_free(req);
  363. return -1;
  364. }
  365. len = os_strlen(pos2);
  366. if (len & 0x01) {
  367. wpabuf_free(req);
  368. return -1;
  369. }
  370. len /= 2;
  371. sel = wpabuf_alloc(len);
  372. if (sel == NULL) {
  373. wpabuf_free(req);
  374. return -1;
  375. }
  376. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  377. wpabuf_free(req);
  378. wpabuf_free(sel);
  379. return -1;
  380. }
  381. if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
  382. ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
  383. } else {
  384. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  385. "reported: role=%s type=%s", role, type);
  386. ret = -1;
  387. }
  388. wpabuf_free(req);
  389. wpabuf_free(sel);
  390. return ret;
  391. }
  392. #endif /* CONFIG_WPS_NFC */
  393. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  394. char *buf, size_t buflen)
  395. {
  396. int timeout = 300;
  397. char *pos;
  398. const char *pin_txt;
  399. pos = os_strchr(txt, ' ');
  400. if (pos)
  401. *pos++ = '\0';
  402. if (os_strcmp(txt, "disable") == 0) {
  403. hostapd_wps_ap_pin_disable(hapd);
  404. return os_snprintf(buf, buflen, "OK\n");
  405. }
  406. if (os_strcmp(txt, "random") == 0) {
  407. if (pos)
  408. timeout = atoi(pos);
  409. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  410. if (pin_txt == NULL)
  411. return -1;
  412. return os_snprintf(buf, buflen, "%s", pin_txt);
  413. }
  414. if (os_strcmp(txt, "get") == 0) {
  415. pin_txt = hostapd_wps_ap_pin_get(hapd);
  416. if (pin_txt == NULL)
  417. return -1;
  418. return os_snprintf(buf, buflen, "%s", pin_txt);
  419. }
  420. if (os_strcmp(txt, "set") == 0) {
  421. char *pin;
  422. if (pos == NULL)
  423. return -1;
  424. pin = pos;
  425. pos = os_strchr(pos, ' ');
  426. if (pos) {
  427. *pos++ = '\0';
  428. timeout = atoi(pos);
  429. }
  430. if (os_strlen(pin) > buflen)
  431. return -1;
  432. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  433. return -1;
  434. return os_snprintf(buf, buflen, "%s", pin);
  435. }
  436. return -1;
  437. }
  438. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  439. {
  440. char *pos;
  441. char *ssid, *auth, *encr = NULL, *key = NULL;
  442. ssid = txt;
  443. pos = os_strchr(txt, ' ');
  444. if (!pos)
  445. return -1;
  446. *pos++ = '\0';
  447. auth = pos;
  448. pos = os_strchr(pos, ' ');
  449. if (pos) {
  450. *pos++ = '\0';
  451. encr = pos;
  452. pos = os_strchr(pos, ' ');
  453. if (pos) {
  454. *pos++ = '\0';
  455. key = pos;
  456. }
  457. }
  458. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  459. }
  460. static const char * pbc_status_str(enum pbc_status status)
  461. {
  462. switch (status) {
  463. case WPS_PBC_STATUS_DISABLE:
  464. return "Disabled";
  465. case WPS_PBC_STATUS_ACTIVE:
  466. return "Active";
  467. case WPS_PBC_STATUS_TIMEOUT:
  468. return "Timed-out";
  469. case WPS_PBC_STATUS_OVERLAP:
  470. return "Overlap";
  471. default:
  472. return "Unknown";
  473. }
  474. }
  475. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  476. char *buf, size_t buflen)
  477. {
  478. int ret;
  479. char *pos, *end;
  480. pos = buf;
  481. end = buf + buflen;
  482. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  483. pbc_status_str(hapd->wps_stats.pbc_status));
  484. if (os_snprintf_error(end - pos, ret))
  485. return pos - buf;
  486. pos += ret;
  487. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  488. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  489. "Success":
  490. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  491. "Failed" : "None")));
  492. if (os_snprintf_error(end - pos, ret))
  493. return pos - buf;
  494. pos += ret;
  495. /* If status == Failure - Add possible Reasons */
  496. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  497. hapd->wps_stats.failure_reason > 0) {
  498. ret = os_snprintf(pos, end - pos,
  499. "Failure Reason: %s\n",
  500. wps_ei_str(hapd->wps_stats.failure_reason));
  501. if (os_snprintf_error(end - pos, ret))
  502. return pos - buf;
  503. pos += ret;
  504. }
  505. if (hapd->wps_stats.status) {
  506. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  507. MAC2STR(hapd->wps_stats.peer_addr));
  508. if (os_snprintf_error(end - pos, ret))
  509. return pos - buf;
  510. pos += ret;
  511. }
  512. return pos - buf;
  513. }
  514. #endif /* CONFIG_WPS */
  515. #ifdef CONFIG_HS20
  516. static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
  517. const char *cmd)
  518. {
  519. u8 addr[ETH_ALEN];
  520. const char *url;
  521. if (hwaddr_aton(cmd, addr))
  522. return -1;
  523. url = cmd + 17;
  524. if (*url == '\0') {
  525. url = NULL;
  526. } else {
  527. if (*url != ' ')
  528. return -1;
  529. url++;
  530. if (*url == '\0')
  531. url = NULL;
  532. }
  533. return hs20_send_wnm_notification(hapd, addr, 1, url);
  534. }
  535. static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
  536. const char *cmd)
  537. {
  538. u8 addr[ETH_ALEN];
  539. int code, reauth_delay, ret;
  540. const char *pos;
  541. size_t url_len;
  542. struct wpabuf *req;
  543. /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
  544. if (hwaddr_aton(cmd, addr))
  545. return -1;
  546. pos = os_strchr(cmd, ' ');
  547. if (pos == NULL)
  548. return -1;
  549. pos++;
  550. code = atoi(pos);
  551. pos = os_strchr(pos, ' ');
  552. if (pos == NULL)
  553. return -1;
  554. pos++;
  555. reauth_delay = atoi(pos);
  556. url_len = 0;
  557. pos = os_strchr(pos, ' ');
  558. if (pos) {
  559. pos++;
  560. url_len = os_strlen(pos);
  561. }
  562. req = wpabuf_alloc(4 + url_len);
  563. if (req == NULL)
  564. return -1;
  565. wpabuf_put_u8(req, code);
  566. wpabuf_put_le16(req, reauth_delay);
  567. wpabuf_put_u8(req, url_len);
  568. if (pos)
  569. wpabuf_put_data(req, pos, url_len);
  570. wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
  571. " to indicate imminent deauthentication (code=%d "
  572. "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
  573. ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
  574. wpabuf_free(req);
  575. return ret;
  576. }
  577. #endif /* CONFIG_HS20 */
  578. #ifdef CONFIG_INTERWORKING
  579. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  580. const char *cmd)
  581. {
  582. u8 qos_map_set[16 + 2 * 21], count = 0;
  583. const char *pos = cmd;
  584. int val, ret;
  585. for (;;) {
  586. if (count == sizeof(qos_map_set)) {
  587. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  588. return -1;
  589. }
  590. val = atoi(pos);
  591. if (val < 0 || val > 255) {
  592. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  593. return -1;
  594. }
  595. qos_map_set[count++] = val;
  596. pos = os_strchr(pos, ',');
  597. if (!pos)
  598. break;
  599. pos++;
  600. }
  601. if (count < 16 || count & 1) {
  602. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  603. return -1;
  604. }
  605. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  606. if (ret) {
  607. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  608. return -1;
  609. }
  610. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  611. hapd->conf->qos_map_set_len = count;
  612. return 0;
  613. }
  614. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  615. const char *cmd)
  616. {
  617. u8 addr[ETH_ALEN];
  618. struct sta_info *sta;
  619. struct wpabuf *buf;
  620. u8 *qos_map_set = hapd->conf->qos_map_set;
  621. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  622. int ret;
  623. if (!qos_map_set_len) {
  624. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  625. return -1;
  626. }
  627. if (hwaddr_aton(cmd, addr))
  628. return -1;
  629. sta = ap_get_sta(hapd, addr);
  630. if (sta == NULL) {
  631. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  632. "for QoS Map Configuration message",
  633. MAC2STR(addr));
  634. return -1;
  635. }
  636. if (!sta->qos_map_enabled) {
  637. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  638. "support for QoS Map", MAC2STR(addr));
  639. return -1;
  640. }
  641. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  642. if (buf == NULL)
  643. return -1;
  644. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  645. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  646. /* QoS Map Set Element */
  647. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  648. wpabuf_put_u8(buf, qos_map_set_len);
  649. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  650. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  651. wpabuf_head(buf), wpabuf_len(buf));
  652. wpabuf_free(buf);
  653. return ret;
  654. }
  655. #endif /* CONFIG_INTERWORKING */
  656. #ifdef CONFIG_WNM
  657. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  658. const char *cmd)
  659. {
  660. u8 addr[ETH_ALEN];
  661. int disassoc_timer;
  662. struct sta_info *sta;
  663. if (hwaddr_aton(cmd, addr))
  664. return -1;
  665. if (cmd[17] != ' ')
  666. return -1;
  667. disassoc_timer = atoi(cmd + 17);
  668. sta = ap_get_sta(hapd, addr);
  669. if (sta == NULL) {
  670. wpa_printf(MSG_DEBUG, "Station " MACSTR
  671. " not found for disassociation imminent message",
  672. MAC2STR(addr));
  673. return -1;
  674. }
  675. return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
  676. }
  677. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  678. const char *cmd)
  679. {
  680. u8 addr[ETH_ALEN];
  681. const char *url, *timerstr;
  682. int disassoc_timer;
  683. struct sta_info *sta;
  684. if (hwaddr_aton(cmd, addr))
  685. return -1;
  686. sta = ap_get_sta(hapd, addr);
  687. if (sta == NULL) {
  688. wpa_printf(MSG_DEBUG, "Station " MACSTR
  689. " not found for ESS disassociation imminent message",
  690. MAC2STR(addr));
  691. return -1;
  692. }
  693. timerstr = cmd + 17;
  694. if (*timerstr != ' ')
  695. return -1;
  696. timerstr++;
  697. disassoc_timer = atoi(timerstr);
  698. if (disassoc_timer < 0 || disassoc_timer > 65535)
  699. return -1;
  700. url = os_strchr(timerstr, ' ');
  701. if (url == NULL)
  702. return -1;
  703. url++;
  704. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  705. }
  706. static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
  707. const char *cmd)
  708. {
  709. u8 addr[ETH_ALEN];
  710. const char *pos, *end;
  711. int disassoc_timer = 0;
  712. struct sta_info *sta;
  713. u8 req_mode = 0, valid_int = 0x01;
  714. u8 bss_term_dur[12];
  715. char *url = NULL;
  716. int ret;
  717. u8 nei_rep[1000];
  718. u8 *nei_pos = nei_rep;
  719. if (hwaddr_aton(cmd, addr)) {
  720. wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
  721. return -1;
  722. }
  723. sta = ap_get_sta(hapd, addr);
  724. if (sta == NULL) {
  725. wpa_printf(MSG_DEBUG, "Station " MACSTR
  726. " not found for BSS TM Request message",
  727. MAC2STR(addr));
  728. return -1;
  729. }
  730. pos = os_strstr(cmd, " disassoc_timer=");
  731. if (pos) {
  732. pos += 16;
  733. disassoc_timer = atoi(pos);
  734. if (disassoc_timer < 0 || disassoc_timer > 65535) {
  735. wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
  736. return -1;
  737. }
  738. }
  739. pos = os_strstr(cmd, " valid_int=");
  740. if (pos) {
  741. pos += 11;
  742. valid_int = atoi(pos);
  743. }
  744. pos = os_strstr(cmd, " bss_term=");
  745. if (pos) {
  746. pos += 10;
  747. req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
  748. /* TODO: TSF configurable/learnable */
  749. bss_term_dur[0] = 4; /* Subelement ID */
  750. bss_term_dur[1] = 10; /* Length */
  751. os_memset(bss_term_dur, 2, 8);
  752. end = os_strchr(pos, ',');
  753. if (end == NULL) {
  754. wpa_printf(MSG_DEBUG, "Invalid bss_term data");
  755. return -1;
  756. }
  757. end++;
  758. WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
  759. }
  760. /*
  761. * BSS Transition Candidate List Entries - Neighbor Report elements
  762. * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
  763. * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
  764. */
  765. pos = cmd;
  766. while (pos) {
  767. u8 *nei_start;
  768. long int val;
  769. char *endptr, *tmp;
  770. pos = os_strstr(pos, " neighbor=");
  771. if (!pos)
  772. break;
  773. if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
  774. wpa_printf(MSG_DEBUG,
  775. "Not enough room for additional neighbor");
  776. return -1;
  777. }
  778. pos += 10;
  779. nei_start = nei_pos;
  780. *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
  781. nei_pos++; /* length to be filled in */
  782. if (hwaddr_aton(pos, nei_pos)) {
  783. wpa_printf(MSG_DEBUG, "Invalid BSSID");
  784. return -1;
  785. }
  786. nei_pos += ETH_ALEN;
  787. pos += 17;
  788. if (*pos != ',') {
  789. wpa_printf(MSG_DEBUG, "Missing BSSID Information");
  790. return -1;
  791. }
  792. pos++;
  793. val = strtol(pos, &endptr, 0);
  794. WPA_PUT_LE32(nei_pos, val);
  795. nei_pos += 4;
  796. if (*endptr != ',') {
  797. wpa_printf(MSG_DEBUG, "Missing Operating Class");
  798. return -1;
  799. }
  800. pos = endptr + 1;
  801. *nei_pos++ = atoi(pos); /* Operating Class */
  802. pos = os_strchr(pos, ',');
  803. if (pos == NULL) {
  804. wpa_printf(MSG_DEBUG, "Missing Channel Number");
  805. return -1;
  806. }
  807. pos++;
  808. *nei_pos++ = atoi(pos); /* Channel Number */
  809. pos = os_strchr(pos, ',');
  810. if (pos == NULL) {
  811. wpa_printf(MSG_DEBUG, "Missing PHY Type");
  812. return -1;
  813. }
  814. pos++;
  815. *nei_pos++ = atoi(pos); /* PHY Type */
  816. end = os_strchr(pos, ' ');
  817. tmp = os_strchr(pos, ',');
  818. if (tmp && (!end || tmp < end)) {
  819. /* Optional Subelements (hexdump) */
  820. size_t len;
  821. pos = tmp + 1;
  822. end = os_strchr(pos, ' ');
  823. if (end)
  824. len = end - pos;
  825. else
  826. len = os_strlen(pos);
  827. if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
  828. wpa_printf(MSG_DEBUG,
  829. "Not enough room for neighbor subelements");
  830. return -1;
  831. }
  832. if (len & 0x01 ||
  833. hexstr2bin(pos, nei_pos, len / 2) < 0) {
  834. wpa_printf(MSG_DEBUG,
  835. "Invalid neighbor subelement info");
  836. return -1;
  837. }
  838. nei_pos += len / 2;
  839. pos = end;
  840. }
  841. nei_start[1] = nei_pos - nei_start - 2;
  842. }
  843. pos = os_strstr(cmd, " url=");
  844. if (pos) {
  845. size_t len;
  846. pos += 5;
  847. end = os_strchr(pos, ' ');
  848. if (end)
  849. len = end - pos;
  850. else
  851. len = os_strlen(pos);
  852. url = os_malloc(len + 1);
  853. if (url == NULL)
  854. return -1;
  855. os_memcpy(url, pos, len);
  856. url[len] = '\0';
  857. req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  858. }
  859. if (os_strstr(cmd, " pref=1"))
  860. req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
  861. if (os_strstr(cmd, " abridged=1"))
  862. req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
  863. if (os_strstr(cmd, " disassoc_imminent=1"))
  864. req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  865. ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
  866. valid_int, bss_term_dur, url,
  867. nei_pos > nei_rep ? nei_rep : NULL,
  868. nei_pos - nei_rep);
  869. os_free(url);
  870. return ret;
  871. }
  872. #endif /* CONFIG_WNM */
  873. static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
  874. char *buf, size_t buflen)
  875. {
  876. int ret = 0;
  877. char *pos, *end;
  878. pos = buf;
  879. end = buf + buflen;
  880. WPA_ASSERT(hapd->conf->wpa_key_mgmt);
  881. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  882. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  883. if (os_snprintf_error(end - pos, ret))
  884. return pos - buf;
  885. pos += ret;
  886. }
  887. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  888. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  889. if (os_snprintf_error(end - pos, ret))
  890. return pos - buf;
  891. pos += ret;
  892. }
  893. #ifdef CONFIG_IEEE80211R
  894. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  895. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  896. if (os_snprintf_error(end - pos, ret))
  897. return pos - buf;
  898. pos += ret;
  899. }
  900. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  901. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  902. if (os_snprintf_error(end - pos, ret))
  903. return pos - buf;
  904. pos += ret;
  905. }
  906. #ifdef CONFIG_SAE
  907. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  908. ret = os_snprintf(pos, end - pos, "FT-SAE ");
  909. if (os_snprintf_error(end - pos, ret))
  910. return pos - buf;
  911. pos += ret;
  912. }
  913. #endif /* CONFIG_SAE */
  914. #endif /* CONFIG_IEEE80211R */
  915. #ifdef CONFIG_IEEE80211W
  916. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  917. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  918. if (os_snprintf_error(end - pos, ret))
  919. return pos - buf;
  920. pos += ret;
  921. }
  922. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  923. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  924. if (os_snprintf_error(end - pos, ret))
  925. return pos - buf;
  926. pos += ret;
  927. }
  928. #endif /* CONFIG_IEEE80211W */
  929. #ifdef CONFIG_SAE
  930. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
  931. ret = os_snprintf(pos, end - pos, "SAE ");
  932. if (os_snprintf_error(end - pos, ret))
  933. return pos - buf;
  934. pos += ret;
  935. }
  936. #endif /* CONFIG_SAE */
  937. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  938. ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
  939. if (os_snprintf_error(end - pos, ret))
  940. return pos - buf;
  941. pos += ret;
  942. }
  943. if (hapd->conf->wpa_key_mgmt &
  944. WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  945. ret = os_snprintf(pos, end - pos,
  946. "WPA-EAP-SUITE-B-192 ");
  947. if (os_snprintf_error(end - pos, ret))
  948. return pos - buf;
  949. pos += ret;
  950. }
  951. if (pos > buf && *(pos - 1) == ' ') {
  952. *(pos - 1) = '\0';
  953. pos--;
  954. }
  955. return pos - buf;
  956. }
  957. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  958. char *buf, size_t buflen)
  959. {
  960. int ret;
  961. char *pos, *end;
  962. pos = buf;
  963. end = buf + buflen;
  964. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  965. "ssid=%s\n",
  966. MAC2STR(hapd->own_addr),
  967. wpa_ssid_txt(hapd->conf->ssid.ssid,
  968. hapd->conf->ssid.ssid_len));
  969. if (os_snprintf_error(end - pos, ret))
  970. return pos - buf;
  971. pos += ret;
  972. #ifdef CONFIG_WPS
  973. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  974. hapd->conf->wps_state == 0 ? "disabled" :
  975. (hapd->conf->wps_state == 1 ? "not configured" :
  976. "configured"));
  977. if (os_snprintf_error(end - pos, ret))
  978. return pos - buf;
  979. pos += ret;
  980. if (hapd->conf->wps_state && hapd->conf->wpa &&
  981. hapd->conf->ssid.wpa_passphrase) {
  982. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  983. hapd->conf->ssid.wpa_passphrase);
  984. if (os_snprintf_error(end - pos, ret))
  985. return pos - buf;
  986. pos += ret;
  987. }
  988. if (hapd->conf->wps_state && hapd->conf->wpa &&
  989. hapd->conf->ssid.wpa_psk &&
  990. hapd->conf->ssid.wpa_psk->group) {
  991. char hex[PMK_LEN * 2 + 1];
  992. wpa_snprintf_hex(hex, sizeof(hex),
  993. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  994. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  995. if (os_snprintf_error(end - pos, ret))
  996. return pos - buf;
  997. pos += ret;
  998. }
  999. #endif /* CONFIG_WPS */
  1000. if (hapd->conf->wpa) {
  1001. ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
  1002. if (os_snprintf_error(end - pos, ret))
  1003. return pos - buf;
  1004. pos += ret;
  1005. }
  1006. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  1007. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  1008. if (os_snprintf_error(end - pos, ret))
  1009. return pos - buf;
  1010. pos += ret;
  1011. pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
  1012. ret = os_snprintf(pos, end - pos, "\n");
  1013. if (os_snprintf_error(end - pos, ret))
  1014. return pos - buf;
  1015. pos += ret;
  1016. }
  1017. if (hapd->conf->wpa) {
  1018. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  1019. wpa_cipher_txt(hapd->conf->wpa_group));
  1020. if (os_snprintf_error(end - pos, ret))
  1021. return pos - buf;
  1022. pos += ret;
  1023. }
  1024. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  1025. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  1026. if (os_snprintf_error(end - pos, ret))
  1027. return pos - buf;
  1028. pos += ret;
  1029. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  1030. " ");
  1031. if (ret < 0)
  1032. return pos - buf;
  1033. pos += ret;
  1034. ret = os_snprintf(pos, end - pos, "\n");
  1035. if (os_snprintf_error(end - pos, ret))
  1036. return pos - buf;
  1037. pos += ret;
  1038. }
  1039. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  1040. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  1041. if (os_snprintf_error(end - pos, ret))
  1042. return pos - buf;
  1043. pos += ret;
  1044. ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
  1045. " ");
  1046. if (ret < 0)
  1047. return pos - buf;
  1048. pos += ret;
  1049. ret = os_snprintf(pos, end - pos, "\n");
  1050. if (os_snprintf_error(end - pos, ret))
  1051. return pos - buf;
  1052. pos += ret;
  1053. }
  1054. return pos - buf;
  1055. }
  1056. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  1057. {
  1058. char *value;
  1059. int ret = 0;
  1060. value = os_strchr(cmd, ' ');
  1061. if (value == NULL)
  1062. return -1;
  1063. *value++ = '\0';
  1064. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  1065. if (0) {
  1066. #ifdef CONFIG_WPS_TESTING
  1067. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  1068. long int val;
  1069. val = strtol(value, NULL, 0);
  1070. if (val < 0 || val > 0xff) {
  1071. ret = -1;
  1072. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  1073. "wps_version_number %ld", val);
  1074. } else {
  1075. wps_version_number = val;
  1076. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  1077. "version %u.%u",
  1078. (wps_version_number & 0xf0) >> 4,
  1079. wps_version_number & 0x0f);
  1080. hostapd_wps_update_ie(hapd);
  1081. }
  1082. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  1083. wps_testing_dummy_cred = atoi(value);
  1084. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  1085. wps_testing_dummy_cred);
  1086. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  1087. wps_corrupt_pkhash = atoi(value);
  1088. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  1089. wps_corrupt_pkhash);
  1090. #endif /* CONFIG_WPS_TESTING */
  1091. #ifdef CONFIG_INTERWORKING
  1092. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  1093. int val = atoi(value);
  1094. if (val <= 0)
  1095. ret = -1;
  1096. else
  1097. hapd->gas_frag_limit = val;
  1098. #endif /* CONFIG_INTERWORKING */
  1099. #ifdef CONFIG_TESTING_OPTIONS
  1100. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  1101. hapd->ext_mgmt_frame_handling = atoi(value);
  1102. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  1103. hapd->ext_eapol_frame_io = atoi(value);
  1104. #endif /* CONFIG_TESTING_OPTIONS */
  1105. } else {
  1106. struct sta_info *sta;
  1107. struct vlan_description vlan_id;
  1108. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  1109. if (ret)
  1110. return ret;
  1111. if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
  1112. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1113. if (hostapd_maclist_found(
  1114. hapd->conf->deny_mac,
  1115. hapd->conf->num_deny_mac, sta->addr,
  1116. &vlan_id) &&
  1117. (!vlan_id.notempty ||
  1118. !vlan_compare(&vlan_id, sta->vlan_desc)))
  1119. ap_sta_disconnect(
  1120. hapd, sta, sta->addr,
  1121. WLAN_REASON_UNSPECIFIED);
  1122. }
  1123. } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
  1124. os_strcasecmp(cmd, "accept_mac_file") == 0) {
  1125. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1126. if (!hostapd_maclist_found(
  1127. hapd->conf->accept_mac,
  1128. hapd->conf->num_accept_mac,
  1129. sta->addr, &vlan_id) ||
  1130. (vlan_id.notempty &&
  1131. vlan_compare(&vlan_id, sta->vlan_desc)))
  1132. ap_sta_disconnect(
  1133. hapd, sta, sta->addr,
  1134. WLAN_REASON_UNSPECIFIED);
  1135. }
  1136. }
  1137. }
  1138. return ret;
  1139. }
  1140. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  1141. char *buf, size_t buflen)
  1142. {
  1143. int res;
  1144. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  1145. if (os_strcmp(cmd, "version") == 0) {
  1146. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  1147. if (os_snprintf_error(buflen, res))
  1148. return -1;
  1149. return res;
  1150. } else if (os_strcmp(cmd, "tls_library") == 0) {
  1151. res = tls_get_library_version(buf, buflen);
  1152. if (os_snprintf_error(buflen, res))
  1153. return -1;
  1154. return res;
  1155. }
  1156. return -1;
  1157. }
  1158. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  1159. {
  1160. if (hostapd_enable_iface(iface) < 0) {
  1161. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  1162. return -1;
  1163. }
  1164. return 0;
  1165. }
  1166. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  1167. {
  1168. if (hostapd_reload_iface(iface) < 0) {
  1169. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  1170. return -1;
  1171. }
  1172. return 0;
  1173. }
  1174. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  1175. {
  1176. if (hostapd_disable_iface(iface) < 0) {
  1177. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  1178. return -1;
  1179. }
  1180. return 0;
  1181. }
  1182. #ifdef CONFIG_TESTING_OPTIONS
  1183. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  1184. {
  1185. union wpa_event_data data;
  1186. char *pos, *param;
  1187. enum wpa_event_type event;
  1188. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  1189. os_memset(&data, 0, sizeof(data));
  1190. param = os_strchr(cmd, ' ');
  1191. if (param == NULL)
  1192. return -1;
  1193. *param++ = '\0';
  1194. if (os_strcmp(cmd, "DETECTED") == 0)
  1195. event = EVENT_DFS_RADAR_DETECTED;
  1196. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  1197. event = EVENT_DFS_CAC_FINISHED;
  1198. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  1199. event = EVENT_DFS_CAC_ABORTED;
  1200. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  1201. event = EVENT_DFS_NOP_FINISHED;
  1202. else {
  1203. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  1204. cmd);
  1205. return -1;
  1206. }
  1207. pos = os_strstr(param, "freq=");
  1208. if (pos)
  1209. data.dfs_event.freq = atoi(pos + 5);
  1210. pos = os_strstr(param, "ht_enabled=1");
  1211. if (pos)
  1212. data.dfs_event.ht_enabled = 1;
  1213. pos = os_strstr(param, "chan_offset=");
  1214. if (pos)
  1215. data.dfs_event.chan_offset = atoi(pos + 12);
  1216. pos = os_strstr(param, "chan_width=");
  1217. if (pos)
  1218. data.dfs_event.chan_width = atoi(pos + 11);
  1219. pos = os_strstr(param, "cf1=");
  1220. if (pos)
  1221. data.dfs_event.cf1 = atoi(pos + 4);
  1222. pos = os_strstr(param, "cf2=");
  1223. if (pos)
  1224. data.dfs_event.cf2 = atoi(pos + 4);
  1225. wpa_supplicant_event(hapd, event, &data);
  1226. return 0;
  1227. }
  1228. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  1229. {
  1230. size_t len;
  1231. u8 *buf;
  1232. int res;
  1233. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  1234. len = os_strlen(cmd);
  1235. if (len & 1)
  1236. return -1;
  1237. len /= 2;
  1238. buf = os_malloc(len);
  1239. if (buf == NULL)
  1240. return -1;
  1241. if (hexstr2bin(cmd, buf, len) < 0) {
  1242. os_free(buf);
  1243. return -1;
  1244. }
  1245. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  1246. os_free(buf);
  1247. return res;
  1248. }
  1249. static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
  1250. {
  1251. char *pos;
  1252. u8 src[ETH_ALEN], *buf;
  1253. int used;
  1254. size_t len;
  1255. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  1256. pos = cmd;
  1257. used = hwaddr_aton2(pos, src);
  1258. if (used < 0)
  1259. return -1;
  1260. pos += used;
  1261. while (*pos == ' ')
  1262. pos++;
  1263. len = os_strlen(pos);
  1264. if (len & 1)
  1265. return -1;
  1266. len /= 2;
  1267. buf = os_malloc(len);
  1268. if (buf == NULL)
  1269. return -1;
  1270. if (hexstr2bin(pos, buf, len) < 0) {
  1271. os_free(buf);
  1272. return -1;
  1273. }
  1274. ieee802_1x_receive(hapd, src, buf, len);
  1275. os_free(buf);
  1276. return 0;
  1277. }
  1278. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  1279. {
  1280. size_t i;
  1281. u32 sum = 0;
  1282. const u16 *pos = buf;
  1283. for (i = 0; i < len / 2; i++)
  1284. sum += *pos++;
  1285. while (sum >> 16)
  1286. sum = (sum & 0xffff) + (sum >> 16);
  1287. return sum ^ 0xffff;
  1288. }
  1289. #define HWSIM_PACKETLEN 1500
  1290. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  1291. void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  1292. size_t len)
  1293. {
  1294. struct hostapd_data *hapd = ctx;
  1295. const struct ether_header *eth;
  1296. struct iphdr ip;
  1297. const u8 *pos;
  1298. unsigned int i;
  1299. if (len != HWSIM_PACKETLEN)
  1300. return;
  1301. eth = (const struct ether_header *) buf;
  1302. os_memcpy(&ip, eth + 1, sizeof(ip));
  1303. pos = &buf[sizeof(*eth) + sizeof(ip)];
  1304. if (ip.ihl != 5 || ip.version != 4 ||
  1305. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  1306. return;
  1307. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  1308. if (*pos != (u8) i)
  1309. return;
  1310. pos++;
  1311. }
  1312. wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  1313. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  1314. }
  1315. static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
  1316. char *cmd)
  1317. {
  1318. int enabled = atoi(cmd);
  1319. char *pos;
  1320. const char *ifname;
  1321. if (!enabled) {
  1322. if (hapd->l2_test) {
  1323. l2_packet_deinit(hapd->l2_test);
  1324. hapd->l2_test = NULL;
  1325. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  1326. "test data: Disabled");
  1327. }
  1328. return 0;
  1329. }
  1330. if (hapd->l2_test)
  1331. return 0;
  1332. pos = os_strstr(cmd, " ifname=");
  1333. if (pos)
  1334. ifname = pos + 8;
  1335. else
  1336. ifname = hapd->conf->iface;
  1337. hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
  1338. ETHERTYPE_IP, hostapd_data_test_rx,
  1339. hapd, 1);
  1340. if (hapd->l2_test == NULL)
  1341. return -1;
  1342. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
  1343. return 0;
  1344. }
  1345. static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
  1346. {
  1347. u8 dst[ETH_ALEN], src[ETH_ALEN];
  1348. char *pos;
  1349. int used;
  1350. long int val;
  1351. u8 tos;
  1352. u8 buf[2 + HWSIM_PACKETLEN];
  1353. struct ether_header *eth;
  1354. struct iphdr *ip;
  1355. u8 *dpos;
  1356. unsigned int i;
  1357. if (hapd->l2_test == NULL)
  1358. return -1;
  1359. /* format: <dst> <src> <tos> */
  1360. pos = cmd;
  1361. used = hwaddr_aton2(pos, dst);
  1362. if (used < 0)
  1363. return -1;
  1364. pos += used;
  1365. while (*pos == ' ')
  1366. pos++;
  1367. used = hwaddr_aton2(pos, src);
  1368. if (used < 0)
  1369. return -1;
  1370. pos += used;
  1371. val = strtol(pos, NULL, 0);
  1372. if (val < 0 || val > 0xff)
  1373. return -1;
  1374. tos = val;
  1375. eth = (struct ether_header *) &buf[2];
  1376. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  1377. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  1378. eth->ether_type = htons(ETHERTYPE_IP);
  1379. ip = (struct iphdr *) (eth + 1);
  1380. os_memset(ip, 0, sizeof(*ip));
  1381. ip->ihl = 5;
  1382. ip->version = 4;
  1383. ip->ttl = 64;
  1384. ip->tos = tos;
  1385. ip->tot_len = htons(HWSIM_IP_LEN);
  1386. ip->protocol = 1;
  1387. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  1388. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  1389. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  1390. dpos = (u8 *) (ip + 1);
  1391. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  1392. *dpos++ = i;
  1393. if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
  1394. HWSIM_PACKETLEN) < 0)
  1395. return -1;
  1396. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
  1397. " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  1398. return 0;
  1399. }
  1400. static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
  1401. char *cmd)
  1402. {
  1403. u8 *buf;
  1404. struct ether_header *eth;
  1405. struct l2_packet_data *l2 = NULL;
  1406. size_t len;
  1407. u16 ethertype;
  1408. int res = -1;
  1409. const char *ifname = hapd->conf->iface;
  1410. if (os_strncmp(cmd, "ifname=", 7) == 0) {
  1411. cmd += 7;
  1412. ifname = cmd;
  1413. cmd = os_strchr(cmd, ' ');
  1414. if (cmd == NULL)
  1415. return -1;
  1416. *cmd++ = '\0';
  1417. }
  1418. len = os_strlen(cmd);
  1419. if (len & 1 || len < ETH_HLEN * 2)
  1420. return -1;
  1421. len /= 2;
  1422. buf = os_malloc(len);
  1423. if (buf == NULL)
  1424. return -1;
  1425. if (hexstr2bin(cmd, buf, len) < 0)
  1426. goto done;
  1427. eth = (struct ether_header *) buf;
  1428. ethertype = ntohs(eth->ether_type);
  1429. l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
  1430. hostapd_data_test_rx, hapd, 1);
  1431. if (l2 == NULL)
  1432. goto done;
  1433. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  1434. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
  1435. done:
  1436. if (l2)
  1437. l2_packet_deinit(l2);
  1438. os_free(buf);
  1439. return res < 0 ? -1 : 0;
  1440. }
  1441. static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
  1442. {
  1443. #ifdef WPA_TRACE_BFD
  1444. extern char wpa_trace_fail_func[256];
  1445. extern unsigned int wpa_trace_fail_after;
  1446. char *pos;
  1447. wpa_trace_fail_after = atoi(cmd);
  1448. pos = os_strchr(cmd, ':');
  1449. if (pos) {
  1450. pos++;
  1451. os_strlcpy(wpa_trace_fail_func, pos,
  1452. sizeof(wpa_trace_fail_func));
  1453. } else {
  1454. wpa_trace_fail_after = 0;
  1455. }
  1456. return 0;
  1457. #else /* WPA_TRACE_BFD */
  1458. return -1;
  1459. #endif /* WPA_TRACE_BFD */
  1460. }
  1461. static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
  1462. char *buf, size_t buflen)
  1463. {
  1464. #ifdef WPA_TRACE_BFD
  1465. extern char wpa_trace_fail_func[256];
  1466. extern unsigned int wpa_trace_fail_after;
  1467. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  1468. wpa_trace_fail_func);
  1469. #else /* WPA_TRACE_BFD */
  1470. return -1;
  1471. #endif /* WPA_TRACE_BFD */
  1472. }
  1473. static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
  1474. {
  1475. #ifdef WPA_TRACE_BFD
  1476. extern char wpa_trace_test_fail_func[256];
  1477. extern unsigned int wpa_trace_test_fail_after;
  1478. char *pos;
  1479. wpa_trace_test_fail_after = atoi(cmd);
  1480. pos = os_strchr(cmd, ':');
  1481. if (pos) {
  1482. pos++;
  1483. os_strlcpy(wpa_trace_test_fail_func, pos,
  1484. sizeof(wpa_trace_test_fail_func));
  1485. } else {
  1486. wpa_trace_test_fail_after = 0;
  1487. }
  1488. return 0;
  1489. #else /* WPA_TRACE_BFD */
  1490. return -1;
  1491. #endif /* WPA_TRACE_BFD */
  1492. }
  1493. static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
  1494. char *buf, size_t buflen)
  1495. {
  1496. #ifdef WPA_TRACE_BFD
  1497. extern char wpa_trace_test_fail_func[256];
  1498. extern unsigned int wpa_trace_test_fail_after;
  1499. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  1500. wpa_trace_test_fail_func);
  1501. #else /* WPA_TRACE_BFD */
  1502. return -1;
  1503. #endif /* WPA_TRACE_BFD */
  1504. }
  1505. #endif /* CONFIG_TESTING_OPTIONS */
  1506. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1507. char *pos)
  1508. {
  1509. #ifdef NEED_AP_MLME
  1510. struct csa_settings settings;
  1511. int ret;
  1512. unsigned int i;
  1513. ret = hostapd_parse_csa_settings(pos, &settings);
  1514. if (ret)
  1515. return ret;
  1516. for (i = 0; i < iface->num_bss; i++) {
  1517. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1518. if (ret) {
  1519. /* FIX: What do we do if CSA fails in the middle of
  1520. * submitting multi-BSS CSA requests? */
  1521. return ret;
  1522. }
  1523. }
  1524. return 0;
  1525. #else /* NEED_AP_MLME */
  1526. return -1;
  1527. #endif /* NEED_AP_MLME */
  1528. }
  1529. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1530. int reply_size, const char *param)
  1531. {
  1532. #ifdef RADIUS_SERVER
  1533. if (os_strcmp(param, "radius_server") == 0) {
  1534. return radius_server_get_mib(hapd->radius_srv, reply,
  1535. reply_size);
  1536. }
  1537. #endif /* RADIUS_SERVER */
  1538. return -1;
  1539. }
  1540. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1541. char *buf, size_t buflen)
  1542. {
  1543. int ret;
  1544. char *pos;
  1545. u8 *data = NULL;
  1546. unsigned int vendor_id, subcmd;
  1547. struct wpabuf *reply;
  1548. size_t data_len = 0;
  1549. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1550. vendor_id = strtoul(cmd, &pos, 16);
  1551. if (!isblank((unsigned char) *pos))
  1552. return -EINVAL;
  1553. subcmd = strtoul(pos, &pos, 10);
  1554. if (*pos != '\0') {
  1555. if (!isblank((unsigned char) *pos++))
  1556. return -EINVAL;
  1557. data_len = os_strlen(pos);
  1558. }
  1559. if (data_len) {
  1560. data_len /= 2;
  1561. data = os_malloc(data_len);
  1562. if (!data)
  1563. return -ENOBUFS;
  1564. if (hexstr2bin(pos, data, data_len)) {
  1565. wpa_printf(MSG_DEBUG,
  1566. "Vendor command: wrong parameter format");
  1567. os_free(data);
  1568. return -EINVAL;
  1569. }
  1570. }
  1571. reply = wpabuf_alloc((buflen - 1) / 2);
  1572. if (!reply) {
  1573. os_free(data);
  1574. return -ENOBUFS;
  1575. }
  1576. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1577. reply);
  1578. if (ret == 0)
  1579. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1580. wpabuf_len(reply));
  1581. wpabuf_free(reply);
  1582. os_free(data);
  1583. return ret;
  1584. }
  1585. static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
  1586. const char *cmd)
  1587. {
  1588. u8 addr[ETH_ALEN];
  1589. struct sta_info *sta;
  1590. if (hwaddr_aton(cmd, addr))
  1591. return -1;
  1592. sta = ap_get_sta(hapd, addr);
  1593. if (!sta || !sta->eapol_sm)
  1594. return -1;
  1595. eapol_auth_reauthenticate(sta->eapol_sm);
  1596. return 0;
  1597. }
  1598. static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
  1599. {
  1600. u8 addr[ETH_ALEN];
  1601. struct sta_info *sta;
  1602. char *pos = cmd, *param;
  1603. if (hwaddr_aton(pos, addr) || pos[17] != ' ')
  1604. return -1;
  1605. pos += 18;
  1606. param = pos;
  1607. pos = os_strchr(pos, ' ');
  1608. if (!pos)
  1609. return -1;
  1610. *pos++ = '\0';
  1611. sta = ap_get_sta(hapd, addr);
  1612. if (!sta || !sta->eapol_sm)
  1613. return -1;
  1614. return eapol_auth_set_conf(sta->eapol_sm, param, pos);
  1615. }
  1616. static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
  1617. char *buf, size_t buflen)
  1618. {
  1619. char *pos, *end, *stamp;
  1620. int ret;
  1621. /* cmd: "LOG_LEVEL [<level>]" */
  1622. if (*cmd == '\0') {
  1623. pos = buf;
  1624. end = buf + buflen;
  1625. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1626. "Timestamp: %d\n",
  1627. debug_level_str(wpa_debug_level),
  1628. wpa_debug_timestamp);
  1629. if (os_snprintf_error(end - pos, ret))
  1630. ret = 0;
  1631. return ret;
  1632. }
  1633. while (*cmd == ' ')
  1634. cmd++;
  1635. stamp = os_strchr(cmd, ' ');
  1636. if (stamp) {
  1637. *stamp++ = '\0';
  1638. while (*stamp == ' ') {
  1639. stamp++;
  1640. }
  1641. }
  1642. if (os_strlen(cmd)) {
  1643. int level = str_to_debug_level(cmd);
  1644. if (level < 0)
  1645. return -1;
  1646. wpa_debug_level = level;
  1647. }
  1648. if (stamp && os_strlen(stamp))
  1649. wpa_debug_timestamp = atoi(stamp);
  1650. os_memcpy(buf, "OK\n", 3);
  1651. return 3;
  1652. }
  1653. #ifdef NEED_AP_MLME
  1654. static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
  1655. char *buf, size_t buflen)
  1656. {
  1657. struct hostapd_iface *iface = hapd->iface;
  1658. char *pos, *end;
  1659. struct hostapd_sta_info *info;
  1660. struct os_reltime now;
  1661. sta_track_expire(iface, 0);
  1662. pos = buf;
  1663. end = buf + buflen;
  1664. os_get_reltime(&now);
  1665. dl_list_for_each_reverse(info, &iface->sta_seen,
  1666. struct hostapd_sta_info, list) {
  1667. struct os_reltime age;
  1668. int ret;
  1669. os_reltime_sub(&now, &info->last_seen, &age);
  1670. ret = os_snprintf(pos, end - pos, MACSTR " %u\n",
  1671. MAC2STR(info->addr), (unsigned int) age.sec);
  1672. if (os_snprintf_error(end - pos, ret))
  1673. break;
  1674. pos += ret;
  1675. }
  1676. return pos - buf;
  1677. }
  1678. #endif /* NEED_AP_MLME */
  1679. static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
  1680. char *buf, char *reply,
  1681. int reply_size,
  1682. struct sockaddr_un *from,
  1683. socklen_t fromlen)
  1684. {
  1685. int reply_len, res;
  1686. os_memcpy(reply, "OK\n", 3);
  1687. reply_len = 3;
  1688. if (os_strcmp(buf, "PING") == 0) {
  1689. os_memcpy(reply, "PONG\n", 5);
  1690. reply_len = 5;
  1691. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1692. if (wpa_debug_reopen_file() < 0)
  1693. reply_len = -1;
  1694. } else if (os_strcmp(buf, "STATUS") == 0) {
  1695. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  1696. reply_size);
  1697. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  1698. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  1699. } else if (os_strcmp(buf, "MIB") == 0) {
  1700. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  1701. if (reply_len >= 0) {
  1702. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  1703. reply_size - reply_len);
  1704. if (res < 0)
  1705. reply_len = -1;
  1706. else
  1707. reply_len += res;
  1708. }
  1709. if (reply_len >= 0) {
  1710. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  1711. reply_size - reply_len);
  1712. if (res < 0)
  1713. reply_len = -1;
  1714. else
  1715. reply_len += res;
  1716. }
  1717. #ifndef CONFIG_NO_RADIUS
  1718. if (reply_len >= 0) {
  1719. res = radius_client_get_mib(hapd->radius,
  1720. reply + reply_len,
  1721. reply_size - reply_len);
  1722. if (res < 0)
  1723. reply_len = -1;
  1724. else
  1725. reply_len += res;
  1726. }
  1727. #endif /* CONFIG_NO_RADIUS */
  1728. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  1729. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  1730. buf + 4);
  1731. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1732. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  1733. reply_size);
  1734. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1735. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  1736. reply_size);
  1737. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1738. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  1739. reply_size);
  1740. } else if (os_strcmp(buf, "ATTACH") == 0) {
  1741. if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
  1742. reply_len = -1;
  1743. } else if (os_strcmp(buf, "DETACH") == 0) {
  1744. if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
  1745. reply_len = -1;
  1746. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  1747. if (hostapd_ctrl_iface_level(hapd, from, fromlen,
  1748. buf + 6))
  1749. reply_len = -1;
  1750. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  1751. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  1752. reply_len = -1;
  1753. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  1754. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  1755. reply_len = -1;
  1756. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  1757. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  1758. reply_len = -1;
  1759. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  1760. if (hostapd_ctrl_iface_stop_ap(hapd))
  1761. reply_len = -1;
  1762. #ifdef CONFIG_IEEE80211W
  1763. #ifdef NEED_AP_MLME
  1764. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  1765. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  1766. reply_len = -1;
  1767. #endif /* NEED_AP_MLME */
  1768. #endif /* CONFIG_IEEE80211W */
  1769. #ifdef CONFIG_WPS
  1770. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1771. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1772. reply_len = -1;
  1773. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1774. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1775. hapd, buf + 14, reply, reply_size);
  1776. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1777. if (hostapd_wps_button_pushed(hapd, NULL))
  1778. reply_len = -1;
  1779. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1780. if (hostapd_wps_cancel(hapd))
  1781. reply_len = -1;
  1782. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1783. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1784. reply, reply_size);
  1785. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1786. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1787. reply_len = -1;
  1788. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1789. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1790. reply_size);
  1791. #ifdef CONFIG_WPS_NFC
  1792. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1793. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1794. reply_len = -1;
  1795. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1796. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1797. hapd, buf + 21, reply, reply_size);
  1798. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1799. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1800. hapd, buf + 14, reply, reply_size);
  1801. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1802. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1803. hapd, buf + 21, reply, reply_size);
  1804. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1805. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1806. reply_len = -1;
  1807. #endif /* CONFIG_WPS_NFC */
  1808. #endif /* CONFIG_WPS */
  1809. #ifdef CONFIG_INTERWORKING
  1810. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1811. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1812. reply_len = -1;
  1813. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1814. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1815. reply_len = -1;
  1816. #endif /* CONFIG_INTERWORKING */
  1817. #ifdef CONFIG_HS20
  1818. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  1819. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  1820. reply_len = -1;
  1821. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  1822. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  1823. reply_len = -1;
  1824. #endif /* CONFIG_HS20 */
  1825. #ifdef CONFIG_WNM
  1826. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1827. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1828. reply_len = -1;
  1829. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1830. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1831. reply_len = -1;
  1832. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  1833. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  1834. reply_len = -1;
  1835. #endif /* CONFIG_WNM */
  1836. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1837. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1838. reply_size);
  1839. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1840. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1841. reply_len = -1;
  1842. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1843. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1844. reply_size);
  1845. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1846. if (hostapd_ctrl_iface_enable(hapd->iface))
  1847. reply_len = -1;
  1848. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1849. if (hostapd_ctrl_iface_reload(hapd->iface))
  1850. reply_len = -1;
  1851. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1852. if (hostapd_ctrl_iface_disable(hapd->iface))
  1853. reply_len = -1;
  1854. } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
  1855. if (ieee802_11_set_beacon(hapd))
  1856. reply_len = -1;
  1857. #ifdef CONFIG_TESTING_OPTIONS
  1858. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1859. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1860. reply_len = -1;
  1861. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1862. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1863. reply_len = -1;
  1864. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  1865. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  1866. reply_len = -1;
  1867. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  1868. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  1869. reply_len = -1;
  1870. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  1871. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  1872. reply_len = -1;
  1873. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  1874. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  1875. reply_len = -1;
  1876. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  1877. if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
  1878. reply_len = -1;
  1879. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  1880. reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
  1881. reply_size);
  1882. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  1883. if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
  1884. reply_len = -1;
  1885. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  1886. reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
  1887. #endif /* CONFIG_TESTING_OPTIONS */
  1888. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1889. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  1890. reply_len = -1;
  1891. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  1892. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  1893. reply_size);
  1894. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  1895. ieee802_1x_erp_flush(hapd);
  1896. #ifdef RADIUS_SERVER
  1897. radius_server_erp_flush(hapd->radius_srv);
  1898. #endif /* RADIUS_SERVER */
  1899. } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
  1900. if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
  1901. reply_len = -1;
  1902. } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
  1903. if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
  1904. reply_len = -1;
  1905. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  1906. reply_len = hostapd_ctrl_iface_log_level(
  1907. hapd, buf + 9, reply, reply_size);
  1908. #ifdef NEED_AP_MLME
  1909. } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
  1910. reply_len = hostapd_ctrl_iface_track_sta_list(
  1911. hapd, reply, reply_size);
  1912. #endif /* NEED_AP_MLME */
  1913. } else {
  1914. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1915. reply_len = 16;
  1916. }
  1917. if (reply_len < 0) {
  1918. os_memcpy(reply, "FAIL\n", 5);
  1919. reply_len = 5;
  1920. }
  1921. return reply_len;
  1922. }
  1923. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  1924. void *sock_ctx)
  1925. {
  1926. struct hostapd_data *hapd = eloop_ctx;
  1927. char buf[4096];
  1928. int res;
  1929. struct sockaddr_un from;
  1930. socklen_t fromlen = sizeof(from);
  1931. char *reply;
  1932. const int reply_size = 4096;
  1933. int reply_len;
  1934. int level = MSG_DEBUG;
  1935. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1936. (struct sockaddr *) &from, &fromlen);
  1937. if (res < 0) {
  1938. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  1939. strerror(errno));
  1940. return;
  1941. }
  1942. buf[res] = '\0';
  1943. if (os_strcmp(buf, "PING") == 0)
  1944. level = MSG_EXCESSIVE;
  1945. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  1946. reply = os_malloc(reply_size);
  1947. if (reply == NULL) {
  1948. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  1949. fromlen) < 0) {
  1950. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1951. strerror(errno));
  1952. }
  1953. return;
  1954. }
  1955. reply_len = hostapd_ctrl_iface_receive_process(hapd, buf,
  1956. reply, reply_size,
  1957. &from, fromlen);
  1958. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  1959. fromlen) < 0) {
  1960. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1961. strerror(errno));
  1962. }
  1963. os_free(reply);
  1964. }
  1965. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1966. {
  1967. char *buf;
  1968. size_t len;
  1969. if (hapd->conf->ctrl_interface == NULL)
  1970. return NULL;
  1971. len = os_strlen(hapd->conf->ctrl_interface) +
  1972. os_strlen(hapd->conf->iface) + 2;
  1973. buf = os_malloc(len);
  1974. if (buf == NULL)
  1975. return NULL;
  1976. os_snprintf(buf, len, "%s/%s",
  1977. hapd->conf->ctrl_interface, hapd->conf->iface);
  1978. buf[len - 1] = '\0';
  1979. return buf;
  1980. }
  1981. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  1982. enum wpa_msg_type type,
  1983. const char *txt, size_t len)
  1984. {
  1985. struct hostapd_data *hapd = ctx;
  1986. if (hapd == NULL)
  1987. return;
  1988. hostapd_ctrl_iface_send(hapd, level, type, txt, len);
  1989. }
  1990. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1991. {
  1992. struct sockaddr_un addr;
  1993. int s = -1;
  1994. char *fname = NULL;
  1995. if (hapd->ctrl_sock > -1) {
  1996. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1997. return 0;
  1998. }
  1999. if (hapd->conf->ctrl_interface == NULL)
  2000. return 0;
  2001. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2002. if (errno == EEXIST) {
  2003. wpa_printf(MSG_DEBUG, "Using existing control "
  2004. "interface directory.");
  2005. } else {
  2006. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2007. strerror(errno));
  2008. goto fail;
  2009. }
  2010. }
  2011. if (hapd->conf->ctrl_interface_gid_set &&
  2012. chown(hapd->conf->ctrl_interface, -1,
  2013. hapd->conf->ctrl_interface_gid) < 0) {
  2014. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2015. strerror(errno));
  2016. return -1;
  2017. }
  2018. if (!hapd->conf->ctrl_interface_gid_set &&
  2019. hapd->iface->interfaces->ctrl_iface_group &&
  2020. chown(hapd->conf->ctrl_interface, -1,
  2021. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2022. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2023. strerror(errno));
  2024. return -1;
  2025. }
  2026. #ifdef ANDROID
  2027. /*
  2028. * Android is using umask 0077 which would leave the control interface
  2029. * directory without group access. This breaks things since Wi-Fi
  2030. * framework assumes that this directory can be accessed by other
  2031. * applications in the wifi group. Fix this by adding group access even
  2032. * if umask value would prevent this.
  2033. */
  2034. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2035. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  2036. strerror(errno));
  2037. /* Try to continue anyway */
  2038. }
  2039. #endif /* ANDROID */
  2040. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  2041. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  2042. goto fail;
  2043. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2044. if (s < 0) {
  2045. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2046. goto fail;
  2047. }
  2048. os_memset(&addr, 0, sizeof(addr));
  2049. #ifdef __FreeBSD__
  2050. addr.sun_len = sizeof(addr);
  2051. #endif /* __FreeBSD__ */
  2052. addr.sun_family = AF_UNIX;
  2053. fname = hostapd_ctrl_iface_path(hapd);
  2054. if (fname == NULL)
  2055. goto fail;
  2056. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2057. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2058. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2059. strerror(errno));
  2060. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2061. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2062. " allow connections - assuming it was left"
  2063. "over from forced program termination");
  2064. if (unlink(fname) < 0) {
  2065. wpa_printf(MSG_ERROR,
  2066. "Could not unlink existing ctrl_iface socket '%s': %s",
  2067. fname, strerror(errno));
  2068. goto fail;
  2069. }
  2070. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2071. 0) {
  2072. wpa_printf(MSG_ERROR,
  2073. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  2074. strerror(errno));
  2075. goto fail;
  2076. }
  2077. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2078. "ctrl_iface socket '%s'", fname);
  2079. } else {
  2080. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2081. "be in use - cannot override it");
  2082. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2083. "not used anymore", fname);
  2084. os_free(fname);
  2085. fname = NULL;
  2086. goto fail;
  2087. }
  2088. }
  2089. if (hapd->conf->ctrl_interface_gid_set &&
  2090. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  2091. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2092. strerror(errno));
  2093. goto fail;
  2094. }
  2095. if (!hapd->conf->ctrl_interface_gid_set &&
  2096. hapd->iface->interfaces->ctrl_iface_group &&
  2097. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2098. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2099. strerror(errno));
  2100. goto fail;
  2101. }
  2102. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2103. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2104. strerror(errno));
  2105. goto fail;
  2106. }
  2107. os_free(fname);
  2108. hapd->ctrl_sock = s;
  2109. if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  2110. NULL) < 0) {
  2111. hostapd_ctrl_iface_deinit(hapd);
  2112. return -1;
  2113. }
  2114. hapd->msg_ctx = hapd;
  2115. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2116. return 0;
  2117. fail:
  2118. if (s >= 0)
  2119. close(s);
  2120. if (fname) {
  2121. unlink(fname);
  2122. os_free(fname);
  2123. }
  2124. return -1;
  2125. }
  2126. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  2127. {
  2128. struct wpa_ctrl_dst *dst, *prev;
  2129. if (hapd->ctrl_sock > -1) {
  2130. char *fname;
  2131. eloop_unregister_read_sock(hapd->ctrl_sock);
  2132. close(hapd->ctrl_sock);
  2133. hapd->ctrl_sock = -1;
  2134. fname = hostapd_ctrl_iface_path(hapd);
  2135. if (fname)
  2136. unlink(fname);
  2137. os_free(fname);
  2138. if (hapd->conf->ctrl_interface &&
  2139. rmdir(hapd->conf->ctrl_interface) < 0) {
  2140. if (errno == ENOTEMPTY) {
  2141. wpa_printf(MSG_DEBUG, "Control interface "
  2142. "directory not empty - leaving it "
  2143. "behind");
  2144. } else {
  2145. wpa_printf(MSG_ERROR,
  2146. "rmdir[ctrl_interface=%s]: %s",
  2147. hapd->conf->ctrl_interface,
  2148. strerror(errno));
  2149. }
  2150. }
  2151. }
  2152. dst = hapd->ctrl_dst;
  2153. hapd->ctrl_dst = NULL;
  2154. while (dst) {
  2155. prev = dst;
  2156. dst = dst->next;
  2157. os_free(prev);
  2158. }
  2159. #ifdef CONFIG_TESTING_OPTIONS
  2160. l2_packet_deinit(hapd->l2_test);
  2161. hapd->l2_test = NULL;
  2162. #endif /* CONFIG_TESTING_OPTIONS */
  2163. }
  2164. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  2165. char *buf)
  2166. {
  2167. if (hostapd_add_iface(interfaces, buf) < 0) {
  2168. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  2169. return -1;
  2170. }
  2171. return 0;
  2172. }
  2173. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  2174. char *buf)
  2175. {
  2176. if (hostapd_remove_iface(interfaces, buf) < 0) {
  2177. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  2178. return -1;
  2179. }
  2180. return 0;
  2181. }
  2182. static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
  2183. struct sockaddr_un *from,
  2184. socklen_t fromlen)
  2185. {
  2186. struct wpa_ctrl_dst *dst;
  2187. dst = os_zalloc(sizeof(*dst));
  2188. if (dst == NULL)
  2189. return -1;
  2190. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  2191. dst->addrlen = fromlen;
  2192. dst->debug_level = MSG_INFO;
  2193. dst->next = interfaces->global_ctrl_dst;
  2194. interfaces->global_ctrl_dst = dst;
  2195. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached (global)",
  2196. from->sun_path,
  2197. fromlen - offsetof(struct sockaddr_un, sun_path));
  2198. return 0;
  2199. }
  2200. static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
  2201. struct sockaddr_un *from,
  2202. socklen_t fromlen)
  2203. {
  2204. struct wpa_ctrl_dst *dst, *prev = NULL;
  2205. dst = interfaces->global_ctrl_dst;
  2206. while (dst) {
  2207. if (fromlen == dst->addrlen &&
  2208. os_memcmp(from->sun_path, dst->addr.sun_path,
  2209. fromlen - offsetof(struct sockaddr_un, sun_path))
  2210. == 0) {
  2211. wpa_hexdump(MSG_DEBUG,
  2212. "CTRL_IFACE monitor detached (global)",
  2213. from->sun_path,
  2214. fromlen -
  2215. offsetof(struct sockaddr_un, sun_path));
  2216. if (prev == NULL)
  2217. interfaces->global_ctrl_dst = dst->next;
  2218. else
  2219. prev->next = dst->next;
  2220. os_free(dst);
  2221. return 0;
  2222. }
  2223. prev = dst;
  2224. dst = dst->next;
  2225. }
  2226. return -1;
  2227. }
  2228. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  2229. {
  2230. #ifdef CONFIG_WPS_TESTING
  2231. wps_version_number = 0x20;
  2232. wps_testing_dummy_cred = 0;
  2233. wps_corrupt_pkhash = 0;
  2234. #endif /* CONFIG_WPS_TESTING */
  2235. }
  2236. #ifdef CONFIG_FST
  2237. static int
  2238. hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
  2239. const char *cmd)
  2240. {
  2241. char ifname[IFNAMSIZ + 1];
  2242. struct fst_iface_cfg cfg;
  2243. struct hostapd_data *hapd;
  2244. struct fst_wpa_obj iface_obj;
  2245. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  2246. hapd = hostapd_get_iface(interfaces, ifname);
  2247. if (hapd) {
  2248. if (hapd->iface->fst) {
  2249. wpa_printf(MSG_INFO, "FST: Already attached");
  2250. return -1;
  2251. }
  2252. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  2253. hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
  2254. &iface_obj, &cfg);
  2255. if (hapd->iface->fst)
  2256. return 0;
  2257. }
  2258. }
  2259. return -EINVAL;
  2260. }
  2261. static int
  2262. hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
  2263. const char *cmd)
  2264. {
  2265. char ifname[IFNAMSIZ + 1];
  2266. struct hostapd_data * hapd;
  2267. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  2268. hapd = hostapd_get_iface(interfaces, ifname);
  2269. if (hapd) {
  2270. if (!fst_iface_detach(ifname)) {
  2271. hapd->iface->fst = NULL;
  2272. hapd->iface->fst_ies = NULL;
  2273. return 0;
  2274. }
  2275. }
  2276. }
  2277. return -EINVAL;
  2278. }
  2279. #endif /* CONFIG_FST */
  2280. static struct hostapd_data *
  2281. hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
  2282. const char *ifname)
  2283. {
  2284. size_t i, j;
  2285. for (i = 0; i < interfaces->count; i++) {
  2286. struct hostapd_iface *iface = interfaces->iface[i];
  2287. for (j = 0; j < iface->num_bss; j++) {
  2288. struct hostapd_data *hapd;
  2289. hapd = iface->bss[j];
  2290. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  2291. return hapd;
  2292. }
  2293. }
  2294. return NULL;
  2295. }
  2296. static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
  2297. struct hostapd_data *dst_hapd,
  2298. const char *param)
  2299. {
  2300. int res;
  2301. char *value;
  2302. value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2303. if (!value) {
  2304. wpa_printf(MSG_ERROR,
  2305. "DUP: cannot allocate buffer to stringify %s",
  2306. param);
  2307. goto error_return;
  2308. }
  2309. if (os_strcmp(param, "wpa") == 0) {
  2310. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
  2311. src_hapd->conf->wpa);
  2312. } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
  2313. src_hapd->conf->wpa_key_mgmt) {
  2314. res = hostapd_ctrl_iface_get_key_mgmt(
  2315. src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2316. if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
  2317. goto error_stringify;
  2318. } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
  2319. src_hapd->conf->wpa_pairwise) {
  2320. res = wpa_write_ciphers(value,
  2321. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2322. src_hapd->conf->wpa_pairwise, " ");
  2323. if (res < 0)
  2324. goto error_stringify;
  2325. } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
  2326. src_hapd->conf->rsn_pairwise) {
  2327. res = wpa_write_ciphers(value,
  2328. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2329. src_hapd->conf->rsn_pairwise, " ");
  2330. if (res < 0)
  2331. goto error_stringify;
  2332. } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
  2333. src_hapd->conf->ssid.wpa_passphrase) {
  2334. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
  2335. src_hapd->conf->ssid.wpa_passphrase);
  2336. } else if (os_strcmp(param, "wpa_psk") == 0 &&
  2337. src_hapd->conf->ssid.wpa_psk_set) {
  2338. wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2339. src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  2340. } else {
  2341. wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
  2342. goto error_return;
  2343. }
  2344. res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
  2345. os_free(value);
  2346. return res;
  2347. error_stringify:
  2348. wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
  2349. error_return:
  2350. os_free(value);
  2351. return -1;
  2352. }
  2353. static int
  2354. hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
  2355. char *cmd)
  2356. {
  2357. char *p_start = cmd, *p_end;
  2358. struct hostapd_data *src_hapd, *dst_hapd;
  2359. /* cmd: "<src ifname> <dst ifname> <variable name> */
  2360. p_end = os_strchr(p_start, ' ');
  2361. if (!p_end) {
  2362. wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
  2363. cmd);
  2364. return -1;
  2365. }
  2366. *p_end = '\0';
  2367. src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2368. if (!src_hapd) {
  2369. wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
  2370. p_start);
  2371. return -1;
  2372. }
  2373. p_start = p_end + 1;
  2374. p_end = os_strchr(p_start, ' ');
  2375. if (!p_end) {
  2376. wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
  2377. cmd);
  2378. return -1;
  2379. }
  2380. *p_end = '\0';
  2381. dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2382. if (!dst_hapd) {
  2383. wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
  2384. p_start);
  2385. return -1;
  2386. }
  2387. p_start = p_end + 1;
  2388. return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
  2389. }
  2390. static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
  2391. const char *ifname,
  2392. char *buf, char *reply,
  2393. int reply_size,
  2394. struct sockaddr_un *from,
  2395. socklen_t fromlen)
  2396. {
  2397. struct hostapd_data *hapd;
  2398. hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
  2399. if (hapd == NULL) {
  2400. int res;
  2401. res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
  2402. if (os_snprintf_error(reply_size, res))
  2403. return -1;
  2404. return res;
  2405. }
  2406. return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
  2407. from, fromlen);
  2408. }
  2409. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  2410. void *sock_ctx)
  2411. {
  2412. void *interfaces = eloop_ctx;
  2413. char buf[256];
  2414. int res;
  2415. struct sockaddr_un from;
  2416. socklen_t fromlen = sizeof(from);
  2417. char *reply;
  2418. int reply_len;
  2419. const int reply_size = 4096;
  2420. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  2421. (struct sockaddr *) &from, &fromlen);
  2422. if (res < 0) {
  2423. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2424. strerror(errno));
  2425. return;
  2426. }
  2427. buf[res] = '\0';
  2428. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  2429. reply = os_malloc(reply_size);
  2430. if (reply == NULL) {
  2431. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2432. fromlen) < 0) {
  2433. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2434. strerror(errno));
  2435. }
  2436. return;
  2437. }
  2438. os_memcpy(reply, "OK\n", 3);
  2439. reply_len = 3;
  2440. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  2441. char *pos = os_strchr(buf + 7, ' ');
  2442. if (pos) {
  2443. *pos++ = '\0';
  2444. reply_len = hostapd_global_ctrl_iface_ifname(
  2445. interfaces, buf + 7, pos, reply, reply_size,
  2446. &from, fromlen);
  2447. goto send_reply;
  2448. }
  2449. }
  2450. if (os_strcmp(buf, "PING") == 0) {
  2451. os_memcpy(reply, "PONG\n", 5);
  2452. reply_len = 5;
  2453. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  2454. if (wpa_debug_reopen_file() < 0)
  2455. reply_len = -1;
  2456. } else if (os_strcmp(buf, "FLUSH") == 0) {
  2457. hostapd_ctrl_iface_flush(interfaces);
  2458. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  2459. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  2460. reply_len = -1;
  2461. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  2462. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  2463. reply_len = -1;
  2464. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2465. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  2466. fromlen))
  2467. reply_len = -1;
  2468. } else if (os_strcmp(buf, "DETACH") == 0) {
  2469. if (hostapd_global_ctrl_iface_detach(interfaces, &from,
  2470. fromlen))
  2471. reply_len = -1;
  2472. #ifdef CONFIG_MODULE_TESTS
  2473. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  2474. int hapd_module_tests(void);
  2475. if (hapd_module_tests() < 0)
  2476. reply_len = -1;
  2477. #endif /* CONFIG_MODULE_TESTS */
  2478. #ifdef CONFIG_FST
  2479. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  2480. if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
  2481. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2482. else
  2483. reply_len = -1;
  2484. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  2485. if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
  2486. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2487. else
  2488. reply_len = -1;
  2489. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  2490. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  2491. #endif /* CONFIG_FST */
  2492. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  2493. if (!hostapd_global_ctrl_iface_dup_network(interfaces,
  2494. buf + 12))
  2495. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2496. else
  2497. reply_len = -1;
  2498. } else {
  2499. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  2500. "ignored");
  2501. reply_len = -1;
  2502. }
  2503. send_reply:
  2504. if (reply_len < 0) {
  2505. os_memcpy(reply, "FAIL\n", 5);
  2506. reply_len = 5;
  2507. }
  2508. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2509. fromlen) < 0) {
  2510. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2511. strerror(errno));
  2512. }
  2513. os_free(reply);
  2514. }
  2515. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  2516. {
  2517. char *buf;
  2518. size_t len;
  2519. if (interface->global_iface_path == NULL)
  2520. return NULL;
  2521. len = os_strlen(interface->global_iface_path) +
  2522. os_strlen(interface->global_iface_name) + 2;
  2523. buf = os_malloc(len);
  2524. if (buf == NULL)
  2525. return NULL;
  2526. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  2527. interface->global_iface_name);
  2528. buf[len - 1] = '\0';
  2529. return buf;
  2530. }
  2531. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  2532. {
  2533. struct sockaddr_un addr;
  2534. int s = -1;
  2535. char *fname = NULL;
  2536. if (interface->global_iface_path == NULL) {
  2537. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  2538. return 0;
  2539. }
  2540. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  2541. if (errno == EEXIST) {
  2542. wpa_printf(MSG_DEBUG, "Using existing control "
  2543. "interface directory.");
  2544. } else {
  2545. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2546. strerror(errno));
  2547. goto fail;
  2548. }
  2549. } else if (interface->ctrl_iface_group &&
  2550. chown(interface->global_iface_path, -1,
  2551. interface->ctrl_iface_group) < 0) {
  2552. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2553. strerror(errno));
  2554. goto fail;
  2555. }
  2556. if (os_strlen(interface->global_iface_path) + 1 +
  2557. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  2558. goto fail;
  2559. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2560. if (s < 0) {
  2561. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2562. goto fail;
  2563. }
  2564. os_memset(&addr, 0, sizeof(addr));
  2565. #ifdef __FreeBSD__
  2566. addr.sun_len = sizeof(addr);
  2567. #endif /* __FreeBSD__ */
  2568. addr.sun_family = AF_UNIX;
  2569. fname = hostapd_global_ctrl_iface_path(interface);
  2570. if (fname == NULL)
  2571. goto fail;
  2572. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2573. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2574. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2575. strerror(errno));
  2576. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2577. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2578. " allow connections - assuming it was left"
  2579. "over from forced program termination");
  2580. if (unlink(fname) < 0) {
  2581. wpa_printf(MSG_ERROR,
  2582. "Could not unlink existing ctrl_iface socket '%s': %s",
  2583. fname, strerror(errno));
  2584. goto fail;
  2585. }
  2586. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2587. 0) {
  2588. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  2589. strerror(errno));
  2590. goto fail;
  2591. }
  2592. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2593. "ctrl_iface socket '%s'", fname);
  2594. } else {
  2595. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2596. "be in use - cannot override it");
  2597. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2598. "not used anymore", fname);
  2599. os_free(fname);
  2600. fname = NULL;
  2601. goto fail;
  2602. }
  2603. }
  2604. if (interface->ctrl_iface_group &&
  2605. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  2606. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2607. strerror(errno));
  2608. goto fail;
  2609. }
  2610. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2611. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2612. strerror(errno));
  2613. goto fail;
  2614. }
  2615. os_free(fname);
  2616. interface->global_ctrl_sock = s;
  2617. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  2618. interface, NULL);
  2619. return 0;
  2620. fail:
  2621. if (s >= 0)
  2622. close(s);
  2623. if (fname) {
  2624. unlink(fname);
  2625. os_free(fname);
  2626. }
  2627. return -1;
  2628. }
  2629. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  2630. {
  2631. char *fname = NULL;
  2632. struct wpa_ctrl_dst *dst, *prev;
  2633. if (interfaces->global_ctrl_sock > -1) {
  2634. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  2635. close(interfaces->global_ctrl_sock);
  2636. interfaces->global_ctrl_sock = -1;
  2637. fname = hostapd_global_ctrl_iface_path(interfaces);
  2638. if (fname) {
  2639. unlink(fname);
  2640. os_free(fname);
  2641. }
  2642. if (interfaces->global_iface_path &&
  2643. rmdir(interfaces->global_iface_path) < 0) {
  2644. if (errno == ENOTEMPTY) {
  2645. wpa_printf(MSG_DEBUG, "Control interface "
  2646. "directory not empty - leaving it "
  2647. "behind");
  2648. } else {
  2649. wpa_printf(MSG_ERROR,
  2650. "rmdir[ctrl_interface=%s]: %s",
  2651. interfaces->global_iface_path,
  2652. strerror(errno));
  2653. }
  2654. }
  2655. }
  2656. os_free(interfaces->global_iface_path);
  2657. interfaces->global_iface_path = NULL;
  2658. dst = interfaces->global_ctrl_dst;
  2659. interfaces->global_ctrl_dst = NULL;
  2660. while (dst) {
  2661. prev = dst;
  2662. dst = dst->next;
  2663. os_free(prev);
  2664. }
  2665. }
  2666. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  2667. enum wpa_msg_type type,
  2668. const char *buf, size_t len)
  2669. {
  2670. struct wpa_ctrl_dst *dst, *next;
  2671. struct msghdr msg;
  2672. int idx;
  2673. struct iovec io[2];
  2674. char levelstr[10];
  2675. int s;
  2676. if (type != WPA_MSG_ONLY_GLOBAL) {
  2677. s = hapd->ctrl_sock;
  2678. dst = hapd->ctrl_dst;
  2679. } else {
  2680. s = hapd->iface->interfaces->global_ctrl_sock;
  2681. dst = hapd->iface->interfaces->global_ctrl_dst;
  2682. }
  2683. if (s < 0 || dst == NULL)
  2684. return;
  2685. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  2686. io[0].iov_base = levelstr;
  2687. io[0].iov_len = os_strlen(levelstr);
  2688. io[1].iov_base = (char *) buf;
  2689. io[1].iov_len = len;
  2690. os_memset(&msg, 0, sizeof(msg));
  2691. msg.msg_iov = io;
  2692. msg.msg_iovlen = 2;
  2693. idx = 0;
  2694. while (dst) {
  2695. next = dst->next;
  2696. if (level >= dst->debug_level) {
  2697. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  2698. (u8 *) dst->addr.sun_path, dst->addrlen -
  2699. offsetof(struct sockaddr_un, sun_path));
  2700. msg.msg_name = &dst->addr;
  2701. msg.msg_namelen = dst->addrlen;
  2702. if (sendmsg(s, &msg, 0) < 0) {
  2703. int _errno = errno;
  2704. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  2705. "%d - %s",
  2706. idx, errno, strerror(errno));
  2707. dst->errors++;
  2708. if (dst->errors > 10 || _errno == ENOENT) {
  2709. if (type != WPA_MSG_ONLY_GLOBAL)
  2710. hostapd_ctrl_iface_detach(
  2711. hapd, &dst->addr,
  2712. dst->addrlen);
  2713. else
  2714. hostapd_global_ctrl_iface_detach(
  2715. hapd->iface->interfaces,
  2716. &dst->addr,
  2717. dst->addrlen);
  2718. }
  2719. } else
  2720. dst->errors = 0;
  2721. }
  2722. idx++;
  2723. dst = next;
  2724. }
  2725. }
  2726. #endif /* CONFIG_NATIVE_WINDOWS */