ctrl_iface.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579
  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. #endif /* CONFIG_TESTING_OPTIONS */
  1400. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1401. char *pos)
  1402. {
  1403. #ifdef NEED_AP_MLME
  1404. struct csa_settings settings;
  1405. int ret;
  1406. unsigned int i;
  1407. ret = hostapd_parse_csa_settings(pos, &settings);
  1408. if (ret)
  1409. return ret;
  1410. for (i = 0; i < iface->num_bss; i++) {
  1411. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1412. if (ret) {
  1413. /* FIX: What do we do if CSA fails in the middle of
  1414. * submitting multi-BSS CSA requests? */
  1415. return ret;
  1416. }
  1417. }
  1418. return 0;
  1419. #else /* NEED_AP_MLME */
  1420. return -1;
  1421. #endif /* NEED_AP_MLME */
  1422. }
  1423. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1424. int reply_size, const char *param)
  1425. {
  1426. #ifdef RADIUS_SERVER
  1427. if (os_strcmp(param, "radius_server") == 0) {
  1428. return radius_server_get_mib(hapd->radius_srv, reply,
  1429. reply_size);
  1430. }
  1431. #endif /* RADIUS_SERVER */
  1432. return -1;
  1433. }
  1434. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1435. char *buf, size_t buflen)
  1436. {
  1437. int ret;
  1438. char *pos;
  1439. u8 *data = NULL;
  1440. unsigned int vendor_id, subcmd;
  1441. struct wpabuf *reply;
  1442. size_t data_len = 0;
  1443. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1444. vendor_id = strtoul(cmd, &pos, 16);
  1445. if (!isblank(*pos))
  1446. return -EINVAL;
  1447. subcmd = strtoul(pos, &pos, 10);
  1448. if (*pos != '\0') {
  1449. if (!isblank(*pos++))
  1450. return -EINVAL;
  1451. data_len = os_strlen(pos);
  1452. }
  1453. if (data_len) {
  1454. data_len /= 2;
  1455. data = os_malloc(data_len);
  1456. if (!data)
  1457. return -ENOBUFS;
  1458. if (hexstr2bin(pos, data, data_len)) {
  1459. wpa_printf(MSG_DEBUG,
  1460. "Vendor command: wrong parameter format");
  1461. os_free(data);
  1462. return -EINVAL;
  1463. }
  1464. }
  1465. reply = wpabuf_alloc((buflen - 1) / 2);
  1466. if (!reply) {
  1467. os_free(data);
  1468. return -ENOBUFS;
  1469. }
  1470. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1471. reply);
  1472. if (ret == 0)
  1473. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1474. wpabuf_len(reply));
  1475. wpabuf_free(reply);
  1476. os_free(data);
  1477. return ret;
  1478. }
  1479. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  1480. void *sock_ctx)
  1481. {
  1482. struct hostapd_data *hapd = eloop_ctx;
  1483. char buf[4096];
  1484. int res;
  1485. struct sockaddr_un from;
  1486. socklen_t fromlen = sizeof(from);
  1487. char *reply;
  1488. const int reply_size = 4096;
  1489. int reply_len;
  1490. int level = MSG_DEBUG;
  1491. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1492. (struct sockaddr *) &from, &fromlen);
  1493. if (res < 0) {
  1494. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  1495. strerror(errno));
  1496. return;
  1497. }
  1498. buf[res] = '\0';
  1499. if (os_strcmp(buf, "PING") == 0)
  1500. level = MSG_EXCESSIVE;
  1501. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  1502. reply = os_malloc(reply_size);
  1503. if (reply == NULL) {
  1504. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  1505. fromlen) < 0) {
  1506. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1507. strerror(errno));
  1508. }
  1509. return;
  1510. }
  1511. os_memcpy(reply, "OK\n", 3);
  1512. reply_len = 3;
  1513. if (os_strcmp(buf, "PING") == 0) {
  1514. os_memcpy(reply, "PONG\n", 5);
  1515. reply_len = 5;
  1516. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1517. if (wpa_debug_reopen_file() < 0)
  1518. reply_len = -1;
  1519. } else if (os_strcmp(buf, "STATUS") == 0) {
  1520. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  1521. reply_size);
  1522. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  1523. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  1524. } else if (os_strcmp(buf, "MIB") == 0) {
  1525. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  1526. if (reply_len >= 0) {
  1527. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  1528. reply_size - reply_len);
  1529. if (res < 0)
  1530. reply_len = -1;
  1531. else
  1532. reply_len += res;
  1533. }
  1534. if (reply_len >= 0) {
  1535. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  1536. reply_size - reply_len);
  1537. if (res < 0)
  1538. reply_len = -1;
  1539. else
  1540. reply_len += res;
  1541. }
  1542. #ifndef CONFIG_NO_RADIUS
  1543. if (reply_len >= 0) {
  1544. res = radius_client_get_mib(hapd->radius,
  1545. reply + reply_len,
  1546. reply_size - reply_len);
  1547. if (res < 0)
  1548. reply_len = -1;
  1549. else
  1550. reply_len += res;
  1551. }
  1552. #endif /* CONFIG_NO_RADIUS */
  1553. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  1554. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  1555. buf + 4);
  1556. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1557. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  1558. reply_size);
  1559. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1560. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  1561. reply_size);
  1562. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1563. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  1564. reply_size);
  1565. } else if (os_strcmp(buf, "ATTACH") == 0) {
  1566. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  1567. reply_len = -1;
  1568. } else if (os_strcmp(buf, "DETACH") == 0) {
  1569. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  1570. reply_len = -1;
  1571. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  1572. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  1573. buf + 6))
  1574. reply_len = -1;
  1575. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  1576. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  1577. reply_len = -1;
  1578. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  1579. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  1580. reply_len = -1;
  1581. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  1582. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  1583. reply_len = -1;
  1584. #ifdef CONFIG_IEEE80211W
  1585. #ifdef NEED_AP_MLME
  1586. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  1587. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  1588. reply_len = -1;
  1589. #endif /* NEED_AP_MLME */
  1590. #endif /* CONFIG_IEEE80211W */
  1591. #ifdef CONFIG_WPS
  1592. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1593. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1594. reply_len = -1;
  1595. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1596. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1597. hapd, buf + 14, reply, reply_size);
  1598. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1599. if (hostapd_wps_button_pushed(hapd, NULL))
  1600. reply_len = -1;
  1601. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1602. if (hostapd_wps_cancel(hapd))
  1603. reply_len = -1;
  1604. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1605. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1606. reply, reply_size);
  1607. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1608. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1609. reply_len = -1;
  1610. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1611. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1612. reply_size);
  1613. #ifdef CONFIG_WPS_NFC
  1614. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1615. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1616. reply_len = -1;
  1617. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1618. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1619. hapd, buf + 21, reply, reply_size);
  1620. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1621. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1622. hapd, buf + 14, reply, reply_size);
  1623. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1624. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1625. hapd, buf + 21, reply, reply_size);
  1626. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1627. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1628. reply_len = -1;
  1629. #endif /* CONFIG_WPS_NFC */
  1630. #endif /* CONFIG_WPS */
  1631. #ifdef CONFIG_INTERWORKING
  1632. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1633. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1634. reply_len = -1;
  1635. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1636. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1637. reply_len = -1;
  1638. #endif /* CONFIG_INTERWORKING */
  1639. #ifdef CONFIG_HS20
  1640. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  1641. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  1642. reply_len = -1;
  1643. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  1644. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  1645. reply_len = -1;
  1646. #endif /* CONFIG_HS20 */
  1647. #ifdef CONFIG_WNM
  1648. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1649. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1650. reply_len = -1;
  1651. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1652. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1653. reply_len = -1;
  1654. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  1655. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  1656. reply_len = -1;
  1657. #endif /* CONFIG_WNM */
  1658. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1659. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1660. reply_size);
  1661. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1662. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1663. reply_len = -1;
  1664. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1665. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1666. reply_size);
  1667. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1668. if (hostapd_ctrl_iface_enable(hapd->iface))
  1669. reply_len = -1;
  1670. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1671. if (hostapd_ctrl_iface_reload(hapd->iface))
  1672. reply_len = -1;
  1673. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1674. if (hostapd_ctrl_iface_disable(hapd->iface))
  1675. reply_len = -1;
  1676. #ifdef CONFIG_TESTING_OPTIONS
  1677. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1678. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1679. reply_len = -1;
  1680. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1681. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1682. reply_len = -1;
  1683. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  1684. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  1685. reply_len = -1;
  1686. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  1687. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  1688. reply_len = -1;
  1689. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  1690. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  1691. reply_len = -1;
  1692. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  1693. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  1694. reply_len = -1;
  1695. #endif /* CONFIG_TESTING_OPTIONS */
  1696. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1697. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  1698. reply_len = -1;
  1699. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  1700. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  1701. reply_size);
  1702. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  1703. ieee802_1x_erp_flush(hapd);
  1704. #ifdef RADIUS_SERVER
  1705. radius_server_erp_flush(hapd->radius_srv);
  1706. #endif /* RADIUS_SERVER */
  1707. } else {
  1708. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1709. reply_len = 16;
  1710. }
  1711. if (reply_len < 0) {
  1712. os_memcpy(reply, "FAIL\n", 5);
  1713. reply_len = 5;
  1714. }
  1715. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  1716. fromlen) < 0) {
  1717. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1718. strerror(errno));
  1719. }
  1720. os_free(reply);
  1721. }
  1722. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1723. {
  1724. char *buf;
  1725. size_t len;
  1726. if (hapd->conf->ctrl_interface == NULL)
  1727. return NULL;
  1728. len = os_strlen(hapd->conf->ctrl_interface) +
  1729. os_strlen(hapd->conf->iface) + 2;
  1730. buf = os_malloc(len);
  1731. if (buf == NULL)
  1732. return NULL;
  1733. os_snprintf(buf, len, "%s/%s",
  1734. hapd->conf->ctrl_interface, hapd->conf->iface);
  1735. buf[len - 1] = '\0';
  1736. return buf;
  1737. }
  1738. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  1739. const char *txt, size_t len)
  1740. {
  1741. struct hostapd_data *hapd = ctx;
  1742. if (hapd == NULL)
  1743. return;
  1744. hostapd_ctrl_iface_send(hapd, level, txt, len);
  1745. }
  1746. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1747. {
  1748. struct sockaddr_un addr;
  1749. int s = -1;
  1750. char *fname = NULL;
  1751. if (hapd->ctrl_sock > -1) {
  1752. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1753. return 0;
  1754. }
  1755. if (hapd->conf->ctrl_interface == NULL)
  1756. return 0;
  1757. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1758. if (errno == EEXIST) {
  1759. wpa_printf(MSG_DEBUG, "Using existing control "
  1760. "interface directory.");
  1761. } else {
  1762. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  1763. strerror(errno));
  1764. goto fail;
  1765. }
  1766. }
  1767. if (hapd->conf->ctrl_interface_gid_set &&
  1768. chown(hapd->conf->ctrl_interface, -1,
  1769. hapd->conf->ctrl_interface_gid) < 0) {
  1770. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  1771. strerror(errno));
  1772. return -1;
  1773. }
  1774. if (!hapd->conf->ctrl_interface_gid_set &&
  1775. hapd->iface->interfaces->ctrl_iface_group &&
  1776. chown(hapd->conf->ctrl_interface, -1,
  1777. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1778. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  1779. strerror(errno));
  1780. return -1;
  1781. }
  1782. #ifdef ANDROID
  1783. /*
  1784. * Android is using umask 0077 which would leave the control interface
  1785. * directory without group access. This breaks things since Wi-Fi
  1786. * framework assumes that this directory can be accessed by other
  1787. * applications in the wifi group. Fix this by adding group access even
  1788. * if umask value would prevent this.
  1789. */
  1790. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1791. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1792. strerror(errno));
  1793. /* Try to continue anyway */
  1794. }
  1795. #endif /* ANDROID */
  1796. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1797. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1798. goto fail;
  1799. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1800. if (s < 0) {
  1801. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  1802. goto fail;
  1803. }
  1804. os_memset(&addr, 0, sizeof(addr));
  1805. #ifdef __FreeBSD__
  1806. addr.sun_len = sizeof(addr);
  1807. #endif /* __FreeBSD__ */
  1808. addr.sun_family = AF_UNIX;
  1809. fname = hostapd_ctrl_iface_path(hapd);
  1810. if (fname == NULL)
  1811. goto fail;
  1812. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1813. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1814. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1815. strerror(errno));
  1816. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1817. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1818. " allow connections - assuming it was left"
  1819. "over from forced program termination");
  1820. if (unlink(fname) < 0) {
  1821. wpa_printf(MSG_ERROR,
  1822. "Could not unlink existing ctrl_iface socket '%s': %s",
  1823. fname, strerror(errno));
  1824. goto fail;
  1825. }
  1826. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1827. 0) {
  1828. wpa_printf(MSG_ERROR,
  1829. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  1830. strerror(errno));
  1831. goto fail;
  1832. }
  1833. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1834. "ctrl_iface socket '%s'", fname);
  1835. } else {
  1836. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1837. "be in use - cannot override it");
  1838. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1839. "not used anymore", fname);
  1840. os_free(fname);
  1841. fname = NULL;
  1842. goto fail;
  1843. }
  1844. }
  1845. if (hapd->conf->ctrl_interface_gid_set &&
  1846. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1847. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  1848. strerror(errno));
  1849. goto fail;
  1850. }
  1851. if (!hapd->conf->ctrl_interface_gid_set &&
  1852. hapd->iface->interfaces->ctrl_iface_group &&
  1853. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1854. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  1855. strerror(errno));
  1856. goto fail;
  1857. }
  1858. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1859. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  1860. strerror(errno));
  1861. goto fail;
  1862. }
  1863. os_free(fname);
  1864. hapd->ctrl_sock = s;
  1865. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1866. NULL);
  1867. hapd->msg_ctx = hapd;
  1868. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1869. return 0;
  1870. fail:
  1871. if (s >= 0)
  1872. close(s);
  1873. if (fname) {
  1874. unlink(fname);
  1875. os_free(fname);
  1876. }
  1877. return -1;
  1878. }
  1879. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1880. {
  1881. struct wpa_ctrl_dst *dst, *prev;
  1882. if (hapd->ctrl_sock > -1) {
  1883. char *fname;
  1884. eloop_unregister_read_sock(hapd->ctrl_sock);
  1885. close(hapd->ctrl_sock);
  1886. hapd->ctrl_sock = -1;
  1887. fname = hostapd_ctrl_iface_path(hapd);
  1888. if (fname)
  1889. unlink(fname);
  1890. os_free(fname);
  1891. if (hapd->conf->ctrl_interface &&
  1892. rmdir(hapd->conf->ctrl_interface) < 0) {
  1893. if (errno == ENOTEMPTY) {
  1894. wpa_printf(MSG_DEBUG, "Control interface "
  1895. "directory not empty - leaving it "
  1896. "behind");
  1897. } else {
  1898. wpa_printf(MSG_ERROR,
  1899. "rmdir[ctrl_interface=%s]: %s",
  1900. hapd->conf->ctrl_interface,
  1901. strerror(errno));
  1902. }
  1903. }
  1904. }
  1905. dst = hapd->ctrl_dst;
  1906. while (dst) {
  1907. prev = dst;
  1908. dst = dst->next;
  1909. os_free(prev);
  1910. }
  1911. #ifdef CONFIG_TESTING_OPTIONS
  1912. l2_packet_deinit(hapd->l2_test);
  1913. hapd->l2_test = NULL;
  1914. #endif /* CONFIG_TESTING_OPTIONS */
  1915. }
  1916. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1917. char *buf)
  1918. {
  1919. if (hostapd_add_iface(interfaces, buf) < 0) {
  1920. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1921. return -1;
  1922. }
  1923. return 0;
  1924. }
  1925. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1926. char *buf)
  1927. {
  1928. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1929. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1930. return -1;
  1931. }
  1932. return 0;
  1933. }
  1934. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  1935. {
  1936. #ifdef CONFIG_WPS_TESTING
  1937. wps_version_number = 0x20;
  1938. wps_testing_dummy_cred = 0;
  1939. wps_corrupt_pkhash = 0;
  1940. #endif /* CONFIG_WPS_TESTING */
  1941. }
  1942. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1943. void *sock_ctx)
  1944. {
  1945. void *interfaces = eloop_ctx;
  1946. char buf[256];
  1947. int res;
  1948. struct sockaddr_un from;
  1949. socklen_t fromlen = sizeof(from);
  1950. char reply[24];
  1951. int reply_len;
  1952. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1953. (struct sockaddr *) &from, &fromlen);
  1954. if (res < 0) {
  1955. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  1956. strerror(errno));
  1957. return;
  1958. }
  1959. buf[res] = '\0';
  1960. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  1961. os_memcpy(reply, "OK\n", 3);
  1962. reply_len = 3;
  1963. if (os_strcmp(buf, "PING") == 0) {
  1964. os_memcpy(reply, "PONG\n", 5);
  1965. reply_len = 5;
  1966. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1967. if (wpa_debug_reopen_file() < 0)
  1968. reply_len = -1;
  1969. } else if (os_strcmp(buf, "FLUSH") == 0) {
  1970. hostapd_ctrl_iface_flush(interfaces);
  1971. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1972. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1973. reply_len = -1;
  1974. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1975. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1976. reply_len = -1;
  1977. #ifdef CONFIG_MODULE_TESTS
  1978. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  1979. int hapd_module_tests(void);
  1980. if (hapd_module_tests() < 0)
  1981. reply_len = -1;
  1982. #endif /* CONFIG_MODULE_TESTS */
  1983. } else {
  1984. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1985. "ignored");
  1986. reply_len = -1;
  1987. }
  1988. if (reply_len < 0) {
  1989. os_memcpy(reply, "FAIL\n", 5);
  1990. reply_len = 5;
  1991. }
  1992. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  1993. fromlen) < 0) {
  1994. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1995. strerror(errno));
  1996. }
  1997. }
  1998. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1999. {
  2000. char *buf;
  2001. size_t len;
  2002. if (interface->global_iface_path == NULL)
  2003. return NULL;
  2004. len = os_strlen(interface->global_iface_path) +
  2005. os_strlen(interface->global_iface_name) + 2;
  2006. buf = os_malloc(len);
  2007. if (buf == NULL)
  2008. return NULL;
  2009. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  2010. interface->global_iface_name);
  2011. buf[len - 1] = '\0';
  2012. return buf;
  2013. }
  2014. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  2015. {
  2016. struct sockaddr_un addr;
  2017. int s = -1;
  2018. char *fname = NULL;
  2019. if (interface->global_iface_path == NULL) {
  2020. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  2021. return 0;
  2022. }
  2023. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  2024. if (errno == EEXIST) {
  2025. wpa_printf(MSG_DEBUG, "Using existing control "
  2026. "interface directory.");
  2027. } else {
  2028. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2029. strerror(errno));
  2030. goto fail;
  2031. }
  2032. } else if (interface->ctrl_iface_group &&
  2033. chown(interface->global_iface_path, -1,
  2034. interface->ctrl_iface_group) < 0) {
  2035. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2036. strerror(errno));
  2037. goto fail;
  2038. }
  2039. if (os_strlen(interface->global_iface_path) + 1 +
  2040. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  2041. goto fail;
  2042. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2043. if (s < 0) {
  2044. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2045. goto fail;
  2046. }
  2047. os_memset(&addr, 0, sizeof(addr));
  2048. #ifdef __FreeBSD__
  2049. addr.sun_len = sizeof(addr);
  2050. #endif /* __FreeBSD__ */
  2051. addr.sun_family = AF_UNIX;
  2052. fname = hostapd_global_ctrl_iface_path(interface);
  2053. if (fname == NULL)
  2054. goto fail;
  2055. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2056. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2057. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2058. strerror(errno));
  2059. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2060. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2061. " allow connections - assuming it was left"
  2062. "over from forced program termination");
  2063. if (unlink(fname) < 0) {
  2064. wpa_printf(MSG_ERROR,
  2065. "Could not unlink existing ctrl_iface socket '%s': %s",
  2066. fname, strerror(errno));
  2067. goto fail;
  2068. }
  2069. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2070. 0) {
  2071. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  2072. strerror(errno));
  2073. goto fail;
  2074. }
  2075. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2076. "ctrl_iface socket '%s'", fname);
  2077. } else {
  2078. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2079. "be in use - cannot override it");
  2080. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2081. "not used anymore", fname);
  2082. os_free(fname);
  2083. fname = NULL;
  2084. goto fail;
  2085. }
  2086. }
  2087. if (interface->ctrl_iface_group &&
  2088. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  2089. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2090. strerror(errno));
  2091. goto fail;
  2092. }
  2093. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2094. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2095. strerror(errno));
  2096. goto fail;
  2097. }
  2098. os_free(fname);
  2099. interface->global_ctrl_sock = s;
  2100. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  2101. interface, NULL);
  2102. return 0;
  2103. fail:
  2104. if (s >= 0)
  2105. close(s);
  2106. if (fname) {
  2107. unlink(fname);
  2108. os_free(fname);
  2109. }
  2110. return -1;
  2111. }
  2112. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  2113. {
  2114. char *fname = NULL;
  2115. if (interfaces->global_ctrl_sock > -1) {
  2116. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  2117. close(interfaces->global_ctrl_sock);
  2118. interfaces->global_ctrl_sock = -1;
  2119. fname = hostapd_global_ctrl_iface_path(interfaces);
  2120. if (fname) {
  2121. unlink(fname);
  2122. os_free(fname);
  2123. }
  2124. if (interfaces->global_iface_path &&
  2125. rmdir(interfaces->global_iface_path) < 0) {
  2126. if (errno == ENOTEMPTY) {
  2127. wpa_printf(MSG_DEBUG, "Control interface "
  2128. "directory not empty - leaving it "
  2129. "behind");
  2130. } else {
  2131. wpa_printf(MSG_ERROR,
  2132. "rmdir[ctrl_interface=%s]: %s",
  2133. interfaces->global_iface_path,
  2134. strerror(errno));
  2135. }
  2136. }
  2137. os_free(interfaces->global_iface_path);
  2138. interfaces->global_iface_path = NULL;
  2139. }
  2140. }
  2141. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  2142. const char *buf, size_t len)
  2143. {
  2144. struct wpa_ctrl_dst *dst, *next;
  2145. struct msghdr msg;
  2146. int idx;
  2147. struct iovec io[2];
  2148. char levelstr[10];
  2149. dst = hapd->ctrl_dst;
  2150. if (hapd->ctrl_sock < 0 || dst == NULL)
  2151. return;
  2152. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  2153. io[0].iov_base = levelstr;
  2154. io[0].iov_len = os_strlen(levelstr);
  2155. io[1].iov_base = (char *) buf;
  2156. io[1].iov_len = len;
  2157. os_memset(&msg, 0, sizeof(msg));
  2158. msg.msg_iov = io;
  2159. msg.msg_iovlen = 2;
  2160. idx = 0;
  2161. while (dst) {
  2162. next = dst->next;
  2163. if (level >= dst->debug_level) {
  2164. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  2165. (u8 *) dst->addr.sun_path, dst->addrlen -
  2166. offsetof(struct sockaddr_un, sun_path));
  2167. msg.msg_name = &dst->addr;
  2168. msg.msg_namelen = dst->addrlen;
  2169. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  2170. int _errno = errno;
  2171. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  2172. "%d - %s",
  2173. idx, errno, strerror(errno));
  2174. dst->errors++;
  2175. if (dst->errors > 10 || _errno == ENOENT) {
  2176. hostapd_ctrl_iface_detach(
  2177. hapd, &dst->addr,
  2178. dst->addrlen);
  2179. }
  2180. } else
  2181. dst->errors = 0;
  2182. }
  2183. idx++;
  2184. dst = next;
  2185. }
  2186. }
  2187. #endif /* CONFIG_NATIVE_WINDOWS */