ctrl_iface.c 61 KB

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