ctrl_iface.c 66 KB

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