ctrl_iface.c 75 KB

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