ctrl_iface.c 69 KB

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