ctrl_iface.c 83 KB

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