config.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377
  1. /*
  2. * WPA Supplicant / Configuration parser and common functions
  3. * Copyright (c) 2003-2012, 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 "includes.h"
  9. #include "common.h"
  10. #include "utils/uuid.h"
  11. #include "crypto/sha1.h"
  12. #include "rsn_supp/wpa.h"
  13. #include "eap_peer/eap.h"
  14. #include "p2p/p2p.h"
  15. #include "config.h"
  16. #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
  17. #define NO_CONFIG_WRITE
  18. #endif
  19. /*
  20. * Structure for network configuration parsing. This data is used to implement
  21. * a generic parser for each network block variable. The table of configuration
  22. * variables is defined below in this file (ssid_fields[]).
  23. */
  24. struct parse_data {
  25. /* Configuration variable name */
  26. char *name;
  27. /* Parser function for this variable */
  28. int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
  29. int line, const char *value);
  30. #ifndef NO_CONFIG_WRITE
  31. /* Writer function (i.e., to get the variable in text format from
  32. * internal presentation). */
  33. char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
  34. #endif /* NO_CONFIG_WRITE */
  35. /* Variable specific parameters for the parser. */
  36. void *param1, *param2, *param3, *param4;
  37. /* 0 = this variable can be included in debug output and ctrl_iface
  38. * 1 = this variable contains key/private data and it must not be
  39. * included in debug output unless explicitly requested. In
  40. * addition, this variable will not be readable through the
  41. * ctrl_iface.
  42. */
  43. int key_data;
  44. };
  45. static int wpa_config_parse_str(const struct parse_data *data,
  46. struct wpa_ssid *ssid,
  47. int line, const char *value)
  48. {
  49. size_t res_len, *dst_len;
  50. char **dst, *tmp;
  51. if (os_strcmp(value, "NULL") == 0) {
  52. wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
  53. data->name);
  54. tmp = NULL;
  55. res_len = 0;
  56. goto set;
  57. }
  58. tmp = wpa_config_parse_string(value, &res_len);
  59. if (tmp == NULL) {
  60. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
  61. line, data->name,
  62. data->key_data ? "[KEY DATA REMOVED]" : value);
  63. return -1;
  64. }
  65. if (data->key_data) {
  66. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  67. (u8 *) tmp, res_len);
  68. } else {
  69. wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
  70. (u8 *) tmp, res_len);
  71. }
  72. if (data->param3 && res_len < (size_t) data->param3) {
  73. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  74. "min_len=%ld)", line, data->name,
  75. (unsigned long) res_len, (long) data->param3);
  76. os_free(tmp);
  77. return -1;
  78. }
  79. if (data->param4 && res_len > (size_t) data->param4) {
  80. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  81. "max_len=%ld)", line, data->name,
  82. (unsigned long) res_len, (long) data->param4);
  83. os_free(tmp);
  84. return -1;
  85. }
  86. set:
  87. dst = (char **) (((u8 *) ssid) + (long) data->param1);
  88. dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
  89. os_free(*dst);
  90. *dst = tmp;
  91. if (data->param2)
  92. *dst_len = res_len;
  93. return 0;
  94. }
  95. #ifndef NO_CONFIG_WRITE
  96. static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
  97. {
  98. char *buf;
  99. buf = os_malloc(len + 3);
  100. if (buf == NULL)
  101. return NULL;
  102. buf[0] = '"';
  103. os_memcpy(buf + 1, value, len);
  104. buf[len + 1] = '"';
  105. buf[len + 2] = '\0';
  106. return buf;
  107. }
  108. static char * wpa_config_write_string_hex(const u8 *value, size_t len)
  109. {
  110. char *buf;
  111. buf = os_zalloc(2 * len + 1);
  112. if (buf == NULL)
  113. return NULL;
  114. wpa_snprintf_hex(buf, 2 * len + 1, value, len);
  115. return buf;
  116. }
  117. static char * wpa_config_write_string(const u8 *value, size_t len)
  118. {
  119. if (value == NULL)
  120. return NULL;
  121. if (is_hex(value, len))
  122. return wpa_config_write_string_hex(value, len);
  123. else
  124. return wpa_config_write_string_ascii(value, len);
  125. }
  126. static char * wpa_config_write_str(const struct parse_data *data,
  127. struct wpa_ssid *ssid)
  128. {
  129. size_t len;
  130. char **src;
  131. src = (char **) (((u8 *) ssid) + (long) data->param1);
  132. if (*src == NULL)
  133. return NULL;
  134. if (data->param2)
  135. len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
  136. else
  137. len = os_strlen(*src);
  138. return wpa_config_write_string((const u8 *) *src, len);
  139. }
  140. #endif /* NO_CONFIG_WRITE */
  141. static int wpa_config_parse_int(const struct parse_data *data,
  142. struct wpa_ssid *ssid,
  143. int line, const char *value)
  144. {
  145. int val, *dst;
  146. char *end;
  147. dst = (int *) (((u8 *) ssid) + (long) data->param1);
  148. val = strtol(value, &end, 0);
  149. if (*end) {
  150. wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
  151. line, value);
  152. return -1;
  153. }
  154. *dst = val;
  155. wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
  156. if (data->param3 && *dst < (long) data->param3) {
  157. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  158. "min_value=%ld)", line, data->name, *dst,
  159. (long) data->param3);
  160. *dst = (long) data->param3;
  161. return -1;
  162. }
  163. if (data->param4 && *dst > (long) data->param4) {
  164. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  165. "max_value=%ld)", line, data->name, *dst,
  166. (long) data->param4);
  167. *dst = (long) data->param4;
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. #ifndef NO_CONFIG_WRITE
  173. static char * wpa_config_write_int(const struct parse_data *data,
  174. struct wpa_ssid *ssid)
  175. {
  176. int *src, res;
  177. char *value;
  178. src = (int *) (((u8 *) ssid) + (long) data->param1);
  179. value = os_malloc(20);
  180. if (value == NULL)
  181. return NULL;
  182. res = os_snprintf(value, 20, "%d", *src);
  183. if (res < 0 || res >= 20) {
  184. os_free(value);
  185. return NULL;
  186. }
  187. value[20 - 1] = '\0';
  188. return value;
  189. }
  190. #endif /* NO_CONFIG_WRITE */
  191. static int wpa_config_parse_bssid(const struct parse_data *data,
  192. struct wpa_ssid *ssid, int line,
  193. const char *value)
  194. {
  195. if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
  196. os_strcmp(value, "any") == 0) {
  197. ssid->bssid_set = 0;
  198. wpa_printf(MSG_MSGDUMP, "BSSID any");
  199. return 0;
  200. }
  201. if (hwaddr_aton(value, ssid->bssid)) {
  202. wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
  203. line, value);
  204. return -1;
  205. }
  206. ssid->bssid_set = 1;
  207. wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
  208. return 0;
  209. }
  210. #ifndef NO_CONFIG_WRITE
  211. static char * wpa_config_write_bssid(const struct parse_data *data,
  212. struct wpa_ssid *ssid)
  213. {
  214. char *value;
  215. int res;
  216. if (!ssid->bssid_set)
  217. return NULL;
  218. value = os_malloc(20);
  219. if (value == NULL)
  220. return NULL;
  221. res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
  222. if (res < 0 || res >= 20) {
  223. os_free(value);
  224. return NULL;
  225. }
  226. value[20 - 1] = '\0';
  227. return value;
  228. }
  229. #endif /* NO_CONFIG_WRITE */
  230. static int wpa_config_parse_psk(const struct parse_data *data,
  231. struct wpa_ssid *ssid, int line,
  232. const char *value)
  233. {
  234. #ifdef CONFIG_EXT_PASSWORD
  235. if (os_strncmp(value, "ext:", 4) == 0) {
  236. os_free(ssid->passphrase);
  237. ssid->passphrase = NULL;
  238. ssid->psk_set = 0;
  239. os_free(ssid->ext_psk);
  240. ssid->ext_psk = os_strdup(value + 4);
  241. if (ssid->ext_psk == NULL)
  242. return -1;
  243. wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
  244. ssid->ext_psk);
  245. return 0;
  246. }
  247. #endif /* CONFIG_EXT_PASSWORD */
  248. if (*value == '"') {
  249. #ifndef CONFIG_NO_PBKDF2
  250. const char *pos;
  251. size_t len;
  252. value++;
  253. pos = os_strrchr(value, '"');
  254. if (pos)
  255. len = pos - value;
  256. else
  257. len = os_strlen(value);
  258. if (len < 8 || len > 63) {
  259. wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
  260. "length %lu (expected: 8..63) '%s'.",
  261. line, (unsigned long) len, value);
  262. return -1;
  263. }
  264. wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
  265. (u8 *) value, len);
  266. if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
  267. os_memcmp(ssid->passphrase, value, len) == 0)
  268. return 0;
  269. ssid->psk_set = 0;
  270. os_free(ssid->passphrase);
  271. ssid->passphrase = dup_binstr(value, len);
  272. if (ssid->passphrase == NULL)
  273. return -1;
  274. return 0;
  275. #else /* CONFIG_NO_PBKDF2 */
  276. wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
  277. "supported.", line);
  278. return -1;
  279. #endif /* CONFIG_NO_PBKDF2 */
  280. }
  281. if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
  282. value[PMK_LEN * 2] != '\0') {
  283. wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
  284. line, value);
  285. return -1;
  286. }
  287. os_free(ssid->passphrase);
  288. ssid->passphrase = NULL;
  289. ssid->psk_set = 1;
  290. wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
  291. return 0;
  292. }
  293. #ifndef NO_CONFIG_WRITE
  294. static char * wpa_config_write_psk(const struct parse_data *data,
  295. struct wpa_ssid *ssid)
  296. {
  297. #ifdef CONFIG_EXT_PASSWORD
  298. if (ssid->ext_psk) {
  299. size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
  300. char *buf = os_malloc(len);
  301. if (buf == NULL)
  302. return NULL;
  303. os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
  304. return buf;
  305. }
  306. #endif /* CONFIG_EXT_PASSWORD */
  307. if (ssid->passphrase)
  308. return wpa_config_write_string_ascii(
  309. (const u8 *) ssid->passphrase,
  310. os_strlen(ssid->passphrase));
  311. if (ssid->psk_set)
  312. return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
  313. return NULL;
  314. }
  315. #endif /* NO_CONFIG_WRITE */
  316. static int wpa_config_parse_proto(const struct parse_data *data,
  317. struct wpa_ssid *ssid, int line,
  318. const char *value)
  319. {
  320. int val = 0, last, errors = 0;
  321. char *start, *end, *buf;
  322. buf = os_strdup(value);
  323. if (buf == NULL)
  324. return -1;
  325. start = buf;
  326. while (*start != '\0') {
  327. while (*start == ' ' || *start == '\t')
  328. start++;
  329. if (*start == '\0')
  330. break;
  331. end = start;
  332. while (*end != ' ' && *end != '\t' && *end != '\0')
  333. end++;
  334. last = *end == '\0';
  335. *end = '\0';
  336. if (os_strcmp(start, "WPA") == 0)
  337. val |= WPA_PROTO_WPA;
  338. else if (os_strcmp(start, "RSN") == 0 ||
  339. os_strcmp(start, "WPA2") == 0)
  340. val |= WPA_PROTO_RSN;
  341. else {
  342. wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
  343. line, start);
  344. errors++;
  345. }
  346. if (last)
  347. break;
  348. start = end + 1;
  349. }
  350. os_free(buf);
  351. if (val == 0) {
  352. wpa_printf(MSG_ERROR,
  353. "Line %d: no proto values configured.", line);
  354. errors++;
  355. }
  356. wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
  357. ssid->proto = val;
  358. return errors ? -1 : 0;
  359. }
  360. #ifndef NO_CONFIG_WRITE
  361. static char * wpa_config_write_proto(const struct parse_data *data,
  362. struct wpa_ssid *ssid)
  363. {
  364. int first = 1, ret;
  365. char *buf, *pos, *end;
  366. pos = buf = os_zalloc(10);
  367. if (buf == NULL)
  368. return NULL;
  369. end = buf + 10;
  370. if (ssid->proto & WPA_PROTO_WPA) {
  371. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  372. if (ret < 0 || ret >= end - pos)
  373. return buf;
  374. pos += ret;
  375. first = 0;
  376. }
  377. if (ssid->proto & WPA_PROTO_RSN) {
  378. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  379. if (ret < 0 || ret >= end - pos)
  380. return buf;
  381. pos += ret;
  382. first = 0;
  383. }
  384. return buf;
  385. }
  386. #endif /* NO_CONFIG_WRITE */
  387. static int wpa_config_parse_key_mgmt(const struct parse_data *data,
  388. struct wpa_ssid *ssid, int line,
  389. const char *value)
  390. {
  391. int val = 0, last, errors = 0;
  392. char *start, *end, *buf;
  393. buf = os_strdup(value);
  394. if (buf == NULL)
  395. return -1;
  396. start = buf;
  397. while (*start != '\0') {
  398. while (*start == ' ' || *start == '\t')
  399. start++;
  400. if (*start == '\0')
  401. break;
  402. end = start;
  403. while (*end != ' ' && *end != '\t' && *end != '\0')
  404. end++;
  405. last = *end == '\0';
  406. *end = '\0';
  407. if (os_strcmp(start, "WPA-PSK") == 0)
  408. val |= WPA_KEY_MGMT_PSK;
  409. else if (os_strcmp(start, "WPA-EAP") == 0)
  410. val |= WPA_KEY_MGMT_IEEE8021X;
  411. else if (os_strcmp(start, "IEEE8021X") == 0)
  412. val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  413. else if (os_strcmp(start, "NONE") == 0)
  414. val |= WPA_KEY_MGMT_NONE;
  415. else if (os_strcmp(start, "WPA-NONE") == 0)
  416. val |= WPA_KEY_MGMT_WPA_NONE;
  417. #ifdef CONFIG_IEEE80211R
  418. else if (os_strcmp(start, "FT-PSK") == 0)
  419. val |= WPA_KEY_MGMT_FT_PSK;
  420. else if (os_strcmp(start, "FT-EAP") == 0)
  421. val |= WPA_KEY_MGMT_FT_IEEE8021X;
  422. #endif /* CONFIG_IEEE80211R */
  423. #ifdef CONFIG_IEEE80211W
  424. else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
  425. val |= WPA_KEY_MGMT_PSK_SHA256;
  426. else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
  427. val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
  428. #endif /* CONFIG_IEEE80211W */
  429. #ifdef CONFIG_WPS
  430. else if (os_strcmp(start, "WPS") == 0)
  431. val |= WPA_KEY_MGMT_WPS;
  432. #endif /* CONFIG_WPS */
  433. #ifdef CONFIG_SAE
  434. else if (os_strcmp(start, "SAE") == 0)
  435. val |= WPA_KEY_MGMT_SAE;
  436. else if (os_strcmp(start, "FT-SAE") == 0)
  437. val |= WPA_KEY_MGMT_FT_SAE;
  438. #endif /* CONFIG_SAE */
  439. else {
  440. wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
  441. line, start);
  442. errors++;
  443. }
  444. if (last)
  445. break;
  446. start = end + 1;
  447. }
  448. os_free(buf);
  449. if (val == 0) {
  450. wpa_printf(MSG_ERROR,
  451. "Line %d: no key_mgmt values configured.", line);
  452. errors++;
  453. }
  454. wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
  455. ssid->key_mgmt = val;
  456. return errors ? -1 : 0;
  457. }
  458. #ifndef NO_CONFIG_WRITE
  459. static char * wpa_config_write_key_mgmt(const struct parse_data *data,
  460. struct wpa_ssid *ssid)
  461. {
  462. char *buf, *pos, *end;
  463. int ret;
  464. pos = buf = os_zalloc(100);
  465. if (buf == NULL)
  466. return NULL;
  467. end = buf + 100;
  468. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  469. ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
  470. pos == buf ? "" : " ");
  471. if (ret < 0 || ret >= end - pos) {
  472. end[-1] = '\0';
  473. return buf;
  474. }
  475. pos += ret;
  476. }
  477. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  478. ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
  479. pos == buf ? "" : " ");
  480. if (ret < 0 || ret >= end - pos) {
  481. end[-1] = '\0';
  482. return buf;
  483. }
  484. pos += ret;
  485. }
  486. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  487. ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
  488. pos == buf ? "" : " ");
  489. if (ret < 0 || ret >= end - pos) {
  490. end[-1] = '\0';
  491. return buf;
  492. }
  493. pos += ret;
  494. }
  495. if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
  496. ret = os_snprintf(pos, end - pos, "%sNONE",
  497. pos == buf ? "" : " ");
  498. if (ret < 0 || ret >= end - pos) {
  499. end[-1] = '\0';
  500. return buf;
  501. }
  502. pos += ret;
  503. }
  504. if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  505. ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
  506. pos == buf ? "" : " ");
  507. if (ret < 0 || ret >= end - pos) {
  508. end[-1] = '\0';
  509. return buf;
  510. }
  511. pos += ret;
  512. }
  513. #ifdef CONFIG_IEEE80211R
  514. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  515. ret = os_snprintf(pos, end - pos, "%sFT-PSK",
  516. pos == buf ? "" : " ");
  517. if (ret < 0 || ret >= end - pos) {
  518. end[-1] = '\0';
  519. return buf;
  520. }
  521. pos += ret;
  522. }
  523. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  524. ret = os_snprintf(pos, end - pos, "%sFT-EAP",
  525. pos == buf ? "" : " ");
  526. if (ret < 0 || ret >= end - pos) {
  527. end[-1] = '\0';
  528. return buf;
  529. }
  530. pos += ret;
  531. }
  532. #endif /* CONFIG_IEEE80211R */
  533. #ifdef CONFIG_IEEE80211W
  534. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  535. ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
  536. pos == buf ? "" : " ");
  537. if (ret < 0 || ret >= end - pos) {
  538. end[-1] = '\0';
  539. return buf;
  540. }
  541. pos += ret;
  542. }
  543. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  544. ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
  545. pos == buf ? "" : " ");
  546. if (ret < 0 || ret >= end - pos) {
  547. end[-1] = '\0';
  548. return buf;
  549. }
  550. pos += ret;
  551. }
  552. #endif /* CONFIG_IEEE80211W */
  553. #ifdef CONFIG_WPS
  554. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  555. ret = os_snprintf(pos, end - pos, "%sWPS",
  556. pos == buf ? "" : " ");
  557. if (ret < 0 || ret >= end - pos) {
  558. end[-1] = '\0';
  559. return buf;
  560. }
  561. pos += ret;
  562. }
  563. #endif /* CONFIG_WPS */
  564. return buf;
  565. }
  566. #endif /* NO_CONFIG_WRITE */
  567. static int wpa_config_parse_cipher(int line, const char *value)
  568. {
  569. int val = wpa_parse_cipher(value);
  570. if (val < 0) {
  571. wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
  572. line, value);
  573. return -1;
  574. }
  575. if (val == 0) {
  576. wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
  577. line);
  578. return -1;
  579. }
  580. return val;
  581. }
  582. #ifndef NO_CONFIG_WRITE
  583. static char * wpa_config_write_cipher(int cipher)
  584. {
  585. char *buf = os_zalloc(50);
  586. if (buf == NULL)
  587. return NULL;
  588. if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
  589. os_free(buf);
  590. return NULL;
  591. }
  592. return buf;
  593. }
  594. #endif /* NO_CONFIG_WRITE */
  595. static int wpa_config_parse_pairwise(const struct parse_data *data,
  596. struct wpa_ssid *ssid, int line,
  597. const char *value)
  598. {
  599. int val;
  600. val = wpa_config_parse_cipher(line, value);
  601. if (val == -1)
  602. return -1;
  603. if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
  604. wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
  605. "(0x%x).", line, val);
  606. return -1;
  607. }
  608. wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
  609. ssid->pairwise_cipher = val;
  610. return 0;
  611. }
  612. #ifndef NO_CONFIG_WRITE
  613. static char * wpa_config_write_pairwise(const struct parse_data *data,
  614. struct wpa_ssid *ssid)
  615. {
  616. return wpa_config_write_cipher(ssid->pairwise_cipher);
  617. }
  618. #endif /* NO_CONFIG_WRITE */
  619. static int wpa_config_parse_group(const struct parse_data *data,
  620. struct wpa_ssid *ssid, int line,
  621. const char *value)
  622. {
  623. int val;
  624. val = wpa_config_parse_cipher(line, value);
  625. if (val == -1)
  626. return -1;
  627. if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
  628. wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
  629. "(0x%x).", line, val);
  630. return -1;
  631. }
  632. wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
  633. ssid->group_cipher = val;
  634. return 0;
  635. }
  636. #ifndef NO_CONFIG_WRITE
  637. static char * wpa_config_write_group(const struct parse_data *data,
  638. struct wpa_ssid *ssid)
  639. {
  640. return wpa_config_write_cipher(ssid->group_cipher);
  641. }
  642. #endif /* NO_CONFIG_WRITE */
  643. static int wpa_config_parse_auth_alg(const struct parse_data *data,
  644. struct wpa_ssid *ssid, int line,
  645. const char *value)
  646. {
  647. int val = 0, last, errors = 0;
  648. char *start, *end, *buf;
  649. buf = os_strdup(value);
  650. if (buf == NULL)
  651. return -1;
  652. start = buf;
  653. while (*start != '\0') {
  654. while (*start == ' ' || *start == '\t')
  655. start++;
  656. if (*start == '\0')
  657. break;
  658. end = start;
  659. while (*end != ' ' && *end != '\t' && *end != '\0')
  660. end++;
  661. last = *end == '\0';
  662. *end = '\0';
  663. if (os_strcmp(start, "OPEN") == 0)
  664. val |= WPA_AUTH_ALG_OPEN;
  665. else if (os_strcmp(start, "SHARED") == 0)
  666. val |= WPA_AUTH_ALG_SHARED;
  667. else if (os_strcmp(start, "LEAP") == 0)
  668. val |= WPA_AUTH_ALG_LEAP;
  669. else {
  670. wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
  671. line, start);
  672. errors++;
  673. }
  674. if (last)
  675. break;
  676. start = end + 1;
  677. }
  678. os_free(buf);
  679. if (val == 0) {
  680. wpa_printf(MSG_ERROR,
  681. "Line %d: no auth_alg values configured.", line);
  682. errors++;
  683. }
  684. wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
  685. ssid->auth_alg = val;
  686. return errors ? -1 : 0;
  687. }
  688. #ifndef NO_CONFIG_WRITE
  689. static char * wpa_config_write_auth_alg(const struct parse_data *data,
  690. struct wpa_ssid *ssid)
  691. {
  692. char *buf, *pos, *end;
  693. int ret;
  694. pos = buf = os_zalloc(30);
  695. if (buf == NULL)
  696. return NULL;
  697. end = buf + 30;
  698. if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
  699. ret = os_snprintf(pos, end - pos, "%sOPEN",
  700. pos == buf ? "" : " ");
  701. if (ret < 0 || ret >= end - pos) {
  702. end[-1] = '\0';
  703. return buf;
  704. }
  705. pos += ret;
  706. }
  707. if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
  708. ret = os_snprintf(pos, end - pos, "%sSHARED",
  709. pos == buf ? "" : " ");
  710. if (ret < 0 || ret >= end - pos) {
  711. end[-1] = '\0';
  712. return buf;
  713. }
  714. pos += ret;
  715. }
  716. if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
  717. ret = os_snprintf(pos, end - pos, "%sLEAP",
  718. pos == buf ? "" : " ");
  719. if (ret < 0 || ret >= end - pos) {
  720. end[-1] = '\0';
  721. return buf;
  722. }
  723. pos += ret;
  724. }
  725. return buf;
  726. }
  727. #endif /* NO_CONFIG_WRITE */
  728. static int * wpa_config_parse_int_array(const char *value)
  729. {
  730. int *freqs;
  731. size_t used, len;
  732. const char *pos;
  733. used = 0;
  734. len = 10;
  735. freqs = os_calloc(len + 1, sizeof(int));
  736. if (freqs == NULL)
  737. return NULL;
  738. pos = value;
  739. while (pos) {
  740. while (*pos == ' ')
  741. pos++;
  742. if (used == len) {
  743. int *n;
  744. size_t i;
  745. n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
  746. if (n == NULL) {
  747. os_free(freqs);
  748. return NULL;
  749. }
  750. for (i = len; i <= len * 2; i++)
  751. n[i] = 0;
  752. freqs = n;
  753. len *= 2;
  754. }
  755. freqs[used] = atoi(pos);
  756. if (freqs[used] == 0)
  757. break;
  758. used++;
  759. pos = os_strchr(pos + 1, ' ');
  760. }
  761. return freqs;
  762. }
  763. static int wpa_config_parse_scan_freq(const struct parse_data *data,
  764. struct wpa_ssid *ssid, int line,
  765. const char *value)
  766. {
  767. int *freqs;
  768. freqs = wpa_config_parse_int_array(value);
  769. if (freqs == NULL)
  770. return -1;
  771. if (freqs[0] == 0) {
  772. os_free(freqs);
  773. freqs = NULL;
  774. }
  775. os_free(ssid->scan_freq);
  776. ssid->scan_freq = freqs;
  777. return 0;
  778. }
  779. static int wpa_config_parse_freq_list(const struct parse_data *data,
  780. struct wpa_ssid *ssid, int line,
  781. const char *value)
  782. {
  783. int *freqs;
  784. freqs = wpa_config_parse_int_array(value);
  785. if (freqs == NULL)
  786. return -1;
  787. if (freqs[0] == 0) {
  788. os_free(freqs);
  789. freqs = NULL;
  790. }
  791. os_free(ssid->freq_list);
  792. ssid->freq_list = freqs;
  793. return 0;
  794. }
  795. #ifndef NO_CONFIG_WRITE
  796. static char * wpa_config_write_freqs(const struct parse_data *data,
  797. const int *freqs)
  798. {
  799. char *buf, *pos, *end;
  800. int i, ret;
  801. size_t count;
  802. if (freqs == NULL)
  803. return NULL;
  804. count = 0;
  805. for (i = 0; freqs[i]; i++)
  806. count++;
  807. pos = buf = os_zalloc(10 * count + 1);
  808. if (buf == NULL)
  809. return NULL;
  810. end = buf + 10 * count + 1;
  811. for (i = 0; freqs[i]; i++) {
  812. ret = os_snprintf(pos, end - pos, "%s%u",
  813. i == 0 ? "" : " ", freqs[i]);
  814. if (ret < 0 || ret >= end - pos) {
  815. end[-1] = '\0';
  816. return buf;
  817. }
  818. pos += ret;
  819. }
  820. return buf;
  821. }
  822. static char * wpa_config_write_scan_freq(const struct parse_data *data,
  823. struct wpa_ssid *ssid)
  824. {
  825. return wpa_config_write_freqs(data, ssid->scan_freq);
  826. }
  827. static char * wpa_config_write_freq_list(const struct parse_data *data,
  828. struct wpa_ssid *ssid)
  829. {
  830. return wpa_config_write_freqs(data, ssid->freq_list);
  831. }
  832. #endif /* NO_CONFIG_WRITE */
  833. #ifdef IEEE8021X_EAPOL
  834. static int wpa_config_parse_eap(const struct parse_data *data,
  835. struct wpa_ssid *ssid, int line,
  836. const char *value)
  837. {
  838. int last, errors = 0;
  839. char *start, *end, *buf;
  840. struct eap_method_type *methods = NULL, *tmp;
  841. size_t num_methods = 0;
  842. buf = os_strdup(value);
  843. if (buf == NULL)
  844. return -1;
  845. start = buf;
  846. while (*start != '\0') {
  847. while (*start == ' ' || *start == '\t')
  848. start++;
  849. if (*start == '\0')
  850. break;
  851. end = start;
  852. while (*end != ' ' && *end != '\t' && *end != '\0')
  853. end++;
  854. last = *end == '\0';
  855. *end = '\0';
  856. tmp = methods;
  857. methods = os_realloc_array(methods, num_methods + 1,
  858. sizeof(*methods));
  859. if (methods == NULL) {
  860. os_free(tmp);
  861. os_free(buf);
  862. return -1;
  863. }
  864. methods[num_methods].method = eap_peer_get_type(
  865. start, &methods[num_methods].vendor);
  866. if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  867. methods[num_methods].method == EAP_TYPE_NONE) {
  868. wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
  869. "'%s'", line, start);
  870. wpa_printf(MSG_ERROR, "You may need to add support for"
  871. " this EAP method during wpa_supplicant\n"
  872. "build time configuration.\n"
  873. "See README for more information.");
  874. errors++;
  875. } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  876. methods[num_methods].method == EAP_TYPE_LEAP)
  877. ssid->leap++;
  878. else
  879. ssid->non_leap++;
  880. num_methods++;
  881. if (last)
  882. break;
  883. start = end + 1;
  884. }
  885. os_free(buf);
  886. tmp = methods;
  887. methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
  888. if (methods == NULL) {
  889. os_free(tmp);
  890. return -1;
  891. }
  892. methods[num_methods].vendor = EAP_VENDOR_IETF;
  893. methods[num_methods].method = EAP_TYPE_NONE;
  894. num_methods++;
  895. wpa_hexdump(MSG_MSGDUMP, "eap methods",
  896. (u8 *) methods, num_methods * sizeof(*methods));
  897. os_free(ssid->eap.eap_methods);
  898. ssid->eap.eap_methods = methods;
  899. return errors ? -1 : 0;
  900. }
  901. static char * wpa_config_write_eap(const struct parse_data *data,
  902. struct wpa_ssid *ssid)
  903. {
  904. int i, ret;
  905. char *buf, *pos, *end;
  906. const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
  907. const char *name;
  908. if (eap_methods == NULL)
  909. return NULL;
  910. pos = buf = os_zalloc(100);
  911. if (buf == NULL)
  912. return NULL;
  913. end = buf + 100;
  914. for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
  915. eap_methods[i].method != EAP_TYPE_NONE; i++) {
  916. name = eap_get_name(eap_methods[i].vendor,
  917. eap_methods[i].method);
  918. if (name) {
  919. ret = os_snprintf(pos, end - pos, "%s%s",
  920. pos == buf ? "" : " ", name);
  921. if (ret < 0 || ret >= end - pos)
  922. break;
  923. pos += ret;
  924. }
  925. }
  926. end[-1] = '\0';
  927. return buf;
  928. }
  929. static int wpa_config_parse_password(const struct parse_data *data,
  930. struct wpa_ssid *ssid, int line,
  931. const char *value)
  932. {
  933. u8 *hash;
  934. if (os_strcmp(value, "NULL") == 0) {
  935. wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
  936. os_free(ssid->eap.password);
  937. ssid->eap.password = NULL;
  938. ssid->eap.password_len = 0;
  939. return 0;
  940. }
  941. #ifdef CONFIG_EXT_PASSWORD
  942. if (os_strncmp(value, "ext:", 4) == 0) {
  943. char *name = os_strdup(value + 4);
  944. if (name == NULL)
  945. return -1;
  946. os_free(ssid->eap.password);
  947. ssid->eap.password = (u8 *) name;
  948. ssid->eap.password_len = os_strlen(name);
  949. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  950. ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
  951. return 0;
  952. }
  953. #endif /* CONFIG_EXT_PASSWORD */
  954. if (os_strncmp(value, "hash:", 5) != 0) {
  955. char *tmp;
  956. size_t res_len;
  957. tmp = wpa_config_parse_string(value, &res_len);
  958. if (tmp == NULL) {
  959. wpa_printf(MSG_ERROR, "Line %d: failed to parse "
  960. "password.", line);
  961. return -1;
  962. }
  963. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  964. (u8 *) tmp, res_len);
  965. os_free(ssid->eap.password);
  966. ssid->eap.password = (u8 *) tmp;
  967. ssid->eap.password_len = res_len;
  968. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  969. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
  970. return 0;
  971. }
  972. /* NtPasswordHash: hash:<32 hex digits> */
  973. if (os_strlen(value + 5) != 2 * 16) {
  974. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
  975. "(expected 32 hex digits)", line);
  976. return -1;
  977. }
  978. hash = os_malloc(16);
  979. if (hash == NULL)
  980. return -1;
  981. if (hexstr2bin(value + 5, hash, 16)) {
  982. os_free(hash);
  983. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
  984. return -1;
  985. }
  986. wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
  987. os_free(ssid->eap.password);
  988. ssid->eap.password = hash;
  989. ssid->eap.password_len = 16;
  990. ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  991. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
  992. return 0;
  993. }
  994. static char * wpa_config_write_password(const struct parse_data *data,
  995. struct wpa_ssid *ssid)
  996. {
  997. char *buf;
  998. if (ssid->eap.password == NULL)
  999. return NULL;
  1000. #ifdef CONFIG_EXT_PASSWORD
  1001. if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
  1002. buf = os_zalloc(4 + ssid->eap.password_len + 1);
  1003. if (buf == NULL)
  1004. return NULL;
  1005. os_memcpy(buf, "ext:", 4);
  1006. os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
  1007. return buf;
  1008. }
  1009. #endif /* CONFIG_EXT_PASSWORD */
  1010. if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
  1011. return wpa_config_write_string(
  1012. ssid->eap.password, ssid->eap.password_len);
  1013. }
  1014. buf = os_malloc(5 + 32 + 1);
  1015. if (buf == NULL)
  1016. return NULL;
  1017. os_memcpy(buf, "hash:", 5);
  1018. wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
  1019. return buf;
  1020. }
  1021. #endif /* IEEE8021X_EAPOL */
  1022. static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
  1023. const char *value, int idx)
  1024. {
  1025. char *buf, title[20];
  1026. int res;
  1027. buf = wpa_config_parse_string(value, len);
  1028. if (buf == NULL) {
  1029. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
  1030. line, idx, value);
  1031. return -1;
  1032. }
  1033. if (*len > MAX_WEP_KEY_LEN) {
  1034. wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
  1035. line, idx, value);
  1036. os_free(buf);
  1037. return -1;
  1038. }
  1039. if (*len && *len != 5 && *len != 13 && *len != 16) {
  1040. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
  1041. "this network block will be ignored",
  1042. line, (unsigned int) *len);
  1043. }
  1044. os_memcpy(key, buf, *len);
  1045. os_free(buf);
  1046. res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
  1047. if (res >= 0 && (size_t) res < sizeof(title))
  1048. wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
  1049. return 0;
  1050. }
  1051. static int wpa_config_parse_wep_key0(const struct parse_data *data,
  1052. struct wpa_ssid *ssid, int line,
  1053. const char *value)
  1054. {
  1055. return wpa_config_parse_wep_key(ssid->wep_key[0],
  1056. &ssid->wep_key_len[0], line,
  1057. value, 0);
  1058. }
  1059. static int wpa_config_parse_wep_key1(const struct parse_data *data,
  1060. struct wpa_ssid *ssid, int line,
  1061. const char *value)
  1062. {
  1063. return wpa_config_parse_wep_key(ssid->wep_key[1],
  1064. &ssid->wep_key_len[1], line,
  1065. value, 1);
  1066. }
  1067. static int wpa_config_parse_wep_key2(const struct parse_data *data,
  1068. struct wpa_ssid *ssid, int line,
  1069. const char *value)
  1070. {
  1071. return wpa_config_parse_wep_key(ssid->wep_key[2],
  1072. &ssid->wep_key_len[2], line,
  1073. value, 2);
  1074. }
  1075. static int wpa_config_parse_wep_key3(const struct parse_data *data,
  1076. struct wpa_ssid *ssid, int line,
  1077. const char *value)
  1078. {
  1079. return wpa_config_parse_wep_key(ssid->wep_key[3],
  1080. &ssid->wep_key_len[3], line,
  1081. value, 3);
  1082. }
  1083. #ifndef NO_CONFIG_WRITE
  1084. static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
  1085. {
  1086. if (ssid->wep_key_len[idx] == 0)
  1087. return NULL;
  1088. return wpa_config_write_string(ssid->wep_key[idx],
  1089. ssid->wep_key_len[idx]);
  1090. }
  1091. static char * wpa_config_write_wep_key0(const struct parse_data *data,
  1092. struct wpa_ssid *ssid)
  1093. {
  1094. return wpa_config_write_wep_key(ssid, 0);
  1095. }
  1096. static char * wpa_config_write_wep_key1(const struct parse_data *data,
  1097. struct wpa_ssid *ssid)
  1098. {
  1099. return wpa_config_write_wep_key(ssid, 1);
  1100. }
  1101. static char * wpa_config_write_wep_key2(const struct parse_data *data,
  1102. struct wpa_ssid *ssid)
  1103. {
  1104. return wpa_config_write_wep_key(ssid, 2);
  1105. }
  1106. static char * wpa_config_write_wep_key3(const struct parse_data *data,
  1107. struct wpa_ssid *ssid)
  1108. {
  1109. return wpa_config_write_wep_key(ssid, 3);
  1110. }
  1111. #endif /* NO_CONFIG_WRITE */
  1112. #ifdef CONFIG_P2P
  1113. static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
  1114. struct wpa_ssid *ssid, int line,
  1115. const char *value)
  1116. {
  1117. const char *pos;
  1118. u8 *buf, *n, addr[ETH_ALEN];
  1119. size_t count;
  1120. buf = NULL;
  1121. count = 0;
  1122. pos = value;
  1123. while (pos && *pos) {
  1124. while (*pos == ' ')
  1125. pos++;
  1126. if (hwaddr_aton(pos, addr)) {
  1127. if (count == 0) {
  1128. wpa_printf(MSG_ERROR, "Line %d: Invalid "
  1129. "p2p_client_list address '%s'.",
  1130. line, value);
  1131. os_free(buf);
  1132. return -1;
  1133. }
  1134. /* continue anyway since this could have been from a
  1135. * truncated configuration file line */
  1136. wpa_printf(MSG_INFO, "Line %d: Ignore likely "
  1137. "truncated p2p_client_list address '%s'",
  1138. line, pos);
  1139. } else {
  1140. n = os_realloc_array(buf, count + 1, ETH_ALEN);
  1141. if (n == NULL) {
  1142. os_free(buf);
  1143. return -1;
  1144. }
  1145. buf = n;
  1146. os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
  1147. os_memcpy(buf, addr, ETH_ALEN);
  1148. count++;
  1149. wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
  1150. addr, ETH_ALEN);
  1151. }
  1152. pos = os_strchr(pos, ' ');
  1153. }
  1154. os_free(ssid->p2p_client_list);
  1155. ssid->p2p_client_list = buf;
  1156. ssid->num_p2p_clients = count;
  1157. return 0;
  1158. }
  1159. #ifndef NO_CONFIG_WRITE
  1160. static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
  1161. struct wpa_ssid *ssid)
  1162. {
  1163. char *value, *end, *pos;
  1164. int res;
  1165. size_t i;
  1166. if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
  1167. return NULL;
  1168. value = os_malloc(20 * ssid->num_p2p_clients);
  1169. if (value == NULL)
  1170. return NULL;
  1171. pos = value;
  1172. end = value + 20 * ssid->num_p2p_clients;
  1173. for (i = ssid->num_p2p_clients; i > 0; i--) {
  1174. res = os_snprintf(pos, end - pos, MACSTR " ",
  1175. MAC2STR(ssid->p2p_client_list +
  1176. (i - 1) * ETH_ALEN));
  1177. if (res < 0 || res >= end - pos) {
  1178. os_free(value);
  1179. return NULL;
  1180. }
  1181. pos += res;
  1182. }
  1183. if (pos > value)
  1184. pos[-1] = '\0';
  1185. return value;
  1186. }
  1187. #endif /* NO_CONFIG_WRITE */
  1188. static int wpa_config_parse_psk_list(const struct parse_data *data,
  1189. struct wpa_ssid *ssid, int line,
  1190. const char *value)
  1191. {
  1192. struct psk_list_entry *p;
  1193. const char *pos;
  1194. p = os_zalloc(sizeof(*p));
  1195. if (p == NULL)
  1196. return -1;
  1197. pos = value;
  1198. if (os_strncmp(pos, "P2P-", 4) == 0) {
  1199. p->p2p = 1;
  1200. pos += 4;
  1201. }
  1202. if (hwaddr_aton(pos, p->addr)) {
  1203. wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
  1204. line, pos);
  1205. os_free(p);
  1206. return -1;
  1207. }
  1208. pos += 17;
  1209. if (*pos != '-') {
  1210. wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
  1211. line, pos);
  1212. os_free(p);
  1213. return -1;
  1214. }
  1215. pos++;
  1216. if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
  1217. wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
  1218. line, pos);
  1219. os_free(p);
  1220. return -1;
  1221. }
  1222. dl_list_add(&ssid->psk_list, &p->list);
  1223. return 0;
  1224. }
  1225. #ifndef NO_CONFIG_WRITE
  1226. static char * wpa_config_write_psk_list(const struct parse_data *data,
  1227. struct wpa_ssid *ssid)
  1228. {
  1229. return NULL;
  1230. }
  1231. #endif /* NO_CONFIG_WRITE */
  1232. #endif /* CONFIG_P2P */
  1233. /* Helper macros for network block parser */
  1234. #ifdef OFFSET
  1235. #undef OFFSET
  1236. #endif /* OFFSET */
  1237. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1238. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1239. /* STR: Define a string variable for an ASCII string; f = field name */
  1240. #ifdef NO_CONFIG_WRITE
  1241. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1242. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1243. #else /* NO_CONFIG_WRITE */
  1244. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1245. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1246. #endif /* NO_CONFIG_WRITE */
  1247. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1248. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1249. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1250. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1251. /* STR_LEN: Define a string variable with a separate variable for storing the
  1252. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1253. * (i.e., even nul termination character). */
  1254. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1255. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1256. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1257. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1258. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1259. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1260. * explicitly specified. */
  1261. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1262. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1263. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1264. #ifdef NO_CONFIG_WRITE
  1265. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1266. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1267. #else /* NO_CONFIG_WRITE */
  1268. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1269. OFFSET(f), (void *) 0
  1270. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1271. OFFSET(eap.f), (void *) 0
  1272. #endif /* NO_CONFIG_WRITE */
  1273. /* INT: Define an integer variable */
  1274. #define INT(f) _INT(f), NULL, NULL, 0
  1275. #define INTe(f) _INTe(f), NULL, NULL, 0
  1276. /* INT_RANGE: Define an integer variable with allowed value range */
  1277. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1278. /* FUNC: Define a configuration variable that uses a custom function for
  1279. * parsing and writing the value. */
  1280. #ifdef NO_CONFIG_WRITE
  1281. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1282. #else /* NO_CONFIG_WRITE */
  1283. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1284. NULL, NULL, NULL, NULL
  1285. #endif /* NO_CONFIG_WRITE */
  1286. #define FUNC(f) _FUNC(f), 0
  1287. #define FUNC_KEY(f) _FUNC(f), 1
  1288. /*
  1289. * Table of network configuration variables. This table is used to parse each
  1290. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1291. * that is inside a network block.
  1292. *
  1293. * This table is generated using the helper macros defined above and with
  1294. * generous help from the C pre-processor. The field name is stored as a string
  1295. * into .name and for STR and INT types, the offset of the target buffer within
  1296. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1297. * offset to the field containing the length of the configuration variable.
  1298. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1299. * and value for INT).
  1300. *
  1301. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1302. * this table and select the entry that matches with the field name. The parser
  1303. * function (.parser) is then called to parse the actual value of the field.
  1304. *
  1305. * This kind of mechanism makes it easy to add new configuration parameters,
  1306. * since only one line needs to be added into this table and into the
  1307. * struct wpa_ssid definition if the new variable is either a string or
  1308. * integer. More complex types will need to use their own parser and writer
  1309. * functions.
  1310. */
  1311. static const struct parse_data ssid_fields[] = {
  1312. { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
  1313. { INT_RANGE(scan_ssid, 0, 1) },
  1314. { FUNC(bssid) },
  1315. { FUNC_KEY(psk) },
  1316. { FUNC(proto) },
  1317. { FUNC(key_mgmt) },
  1318. { INT(bg_scan_period) },
  1319. { FUNC(pairwise) },
  1320. { FUNC(group) },
  1321. { FUNC(auth_alg) },
  1322. { FUNC(scan_freq) },
  1323. { FUNC(freq_list) },
  1324. #ifdef IEEE8021X_EAPOL
  1325. { FUNC(eap) },
  1326. { STR_LENe(identity) },
  1327. { STR_LENe(anonymous_identity) },
  1328. { FUNC_KEY(password) },
  1329. { STRe(ca_cert) },
  1330. { STRe(ca_path) },
  1331. { STRe(client_cert) },
  1332. { STRe(private_key) },
  1333. { STR_KEYe(private_key_passwd) },
  1334. { STRe(dh_file) },
  1335. { STRe(subject_match) },
  1336. { STRe(altsubject_match) },
  1337. { STRe(domain_suffix_match) },
  1338. { STRe(ca_cert2) },
  1339. { STRe(ca_path2) },
  1340. { STRe(client_cert2) },
  1341. { STRe(private_key2) },
  1342. { STR_KEYe(private_key2_passwd) },
  1343. { STRe(dh_file2) },
  1344. { STRe(subject_match2) },
  1345. { STRe(altsubject_match2) },
  1346. { STRe(domain_suffix_match2) },
  1347. { STRe(phase1) },
  1348. { STRe(phase2) },
  1349. { STRe(pcsc) },
  1350. { STR_KEYe(pin) },
  1351. { STRe(engine_id) },
  1352. { STRe(key_id) },
  1353. { STRe(cert_id) },
  1354. { STRe(ca_cert_id) },
  1355. { STR_KEYe(pin2) },
  1356. { STRe(engine2_id) },
  1357. { STRe(key2_id) },
  1358. { STRe(cert2_id) },
  1359. { STRe(ca_cert2_id) },
  1360. { INTe(engine) },
  1361. { INTe(engine2) },
  1362. { INT(eapol_flags) },
  1363. #endif /* IEEE8021X_EAPOL */
  1364. { FUNC_KEY(wep_key0) },
  1365. { FUNC_KEY(wep_key1) },
  1366. { FUNC_KEY(wep_key2) },
  1367. { FUNC_KEY(wep_key3) },
  1368. { INT(wep_tx_keyidx) },
  1369. { INT(priority) },
  1370. #ifdef IEEE8021X_EAPOL
  1371. { INT(eap_workaround) },
  1372. { STRe(pac_file) },
  1373. { INTe(fragment_size) },
  1374. { INTe(ocsp) },
  1375. #endif /* IEEE8021X_EAPOL */
  1376. { INT_RANGE(mode, 0, 4) },
  1377. { INT_RANGE(proactive_key_caching, 0, 1) },
  1378. { INT_RANGE(disabled, 0, 2) },
  1379. { STR(id_str) },
  1380. #ifdef CONFIG_IEEE80211W
  1381. { INT_RANGE(ieee80211w, 0, 2) },
  1382. #endif /* CONFIG_IEEE80211W */
  1383. { INT_RANGE(peerkey, 0, 1) },
  1384. { INT_RANGE(mixed_cell, 0, 1) },
  1385. { INT_RANGE(frequency, 0, 65000) },
  1386. { INT(wpa_ptk_rekey) },
  1387. { STR(bgscan) },
  1388. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1389. #ifdef CONFIG_P2P
  1390. { FUNC(p2p_client_list) },
  1391. { FUNC(psk_list) },
  1392. #endif /* CONFIG_P2P */
  1393. #ifdef CONFIG_HT_OVERRIDES
  1394. { INT_RANGE(disable_ht, 0, 1) },
  1395. { INT_RANGE(disable_ht40, -1, 1) },
  1396. { INT_RANGE(disable_sgi, 0, 1) },
  1397. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1398. { INT_RANGE(ampdu_factor, -1, 3) },
  1399. { INT_RANGE(ampdu_density, -1, 7) },
  1400. { STR(ht_mcs) },
  1401. #endif /* CONFIG_HT_OVERRIDES */
  1402. #ifdef CONFIG_VHT_OVERRIDES
  1403. { INT_RANGE(disable_vht, 0, 1) },
  1404. { INT(vht_capa) },
  1405. { INT(vht_capa_mask) },
  1406. { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
  1407. { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
  1408. { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
  1409. { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
  1410. { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
  1411. { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
  1412. { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
  1413. { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
  1414. { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
  1415. { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
  1416. { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
  1417. { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
  1418. { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
  1419. { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
  1420. { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
  1421. { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
  1422. #endif /* CONFIG_VHT_OVERRIDES */
  1423. { INT(ap_max_inactivity) },
  1424. { INT(dtim_period) },
  1425. { INT(beacon_int) },
  1426. };
  1427. #undef OFFSET
  1428. #undef _STR
  1429. #undef STR
  1430. #undef STR_KEY
  1431. #undef _STR_LEN
  1432. #undef STR_LEN
  1433. #undef STR_LEN_KEY
  1434. #undef _STR_RANGE
  1435. #undef STR_RANGE
  1436. #undef STR_RANGE_KEY
  1437. #undef _INT
  1438. #undef INT
  1439. #undef INT_RANGE
  1440. #undef _FUNC
  1441. #undef FUNC
  1442. #undef FUNC_KEY
  1443. #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
  1444. /**
  1445. * wpa_config_add_prio_network - Add a network to priority lists
  1446. * @config: Configuration data from wpa_config_read()
  1447. * @ssid: Pointer to the network configuration to be added to the list
  1448. * Returns: 0 on success, -1 on failure
  1449. *
  1450. * This function is used to add a network block to the priority list of
  1451. * networks. This must be called for each network when reading in the full
  1452. * configuration. In addition, this can be used indirectly when updating
  1453. * priorities by calling wpa_config_update_prio_list().
  1454. */
  1455. int wpa_config_add_prio_network(struct wpa_config *config,
  1456. struct wpa_ssid *ssid)
  1457. {
  1458. int prio;
  1459. struct wpa_ssid *prev, **nlist;
  1460. /*
  1461. * Add to an existing priority list if one is available for the
  1462. * configured priority level for this network.
  1463. */
  1464. for (prio = 0; prio < config->num_prio; prio++) {
  1465. prev = config->pssid[prio];
  1466. if (prev->priority == ssid->priority) {
  1467. while (prev->pnext)
  1468. prev = prev->pnext;
  1469. prev->pnext = ssid;
  1470. return 0;
  1471. }
  1472. }
  1473. /* First network for this priority - add a new priority list */
  1474. nlist = os_realloc_array(config->pssid, config->num_prio + 1,
  1475. sizeof(struct wpa_ssid *));
  1476. if (nlist == NULL)
  1477. return -1;
  1478. for (prio = 0; prio < config->num_prio; prio++) {
  1479. if (nlist[prio]->priority < ssid->priority) {
  1480. os_memmove(&nlist[prio + 1], &nlist[prio],
  1481. (config->num_prio - prio) *
  1482. sizeof(struct wpa_ssid *));
  1483. break;
  1484. }
  1485. }
  1486. nlist[prio] = ssid;
  1487. config->num_prio++;
  1488. config->pssid = nlist;
  1489. return 0;
  1490. }
  1491. /**
  1492. * wpa_config_update_prio_list - Update network priority list
  1493. * @config: Configuration data from wpa_config_read()
  1494. * Returns: 0 on success, -1 on failure
  1495. *
  1496. * This function is called to update the priority list of networks in the
  1497. * configuration when a network is being added or removed. This is also called
  1498. * if a priority for a network is changed.
  1499. */
  1500. int wpa_config_update_prio_list(struct wpa_config *config)
  1501. {
  1502. struct wpa_ssid *ssid;
  1503. int ret = 0;
  1504. os_free(config->pssid);
  1505. config->pssid = NULL;
  1506. config->num_prio = 0;
  1507. ssid = config->ssid;
  1508. while (ssid) {
  1509. ssid->pnext = NULL;
  1510. if (wpa_config_add_prio_network(config, ssid) < 0)
  1511. ret = -1;
  1512. ssid = ssid->next;
  1513. }
  1514. return ret;
  1515. }
  1516. #ifdef IEEE8021X_EAPOL
  1517. static void eap_peer_config_free(struct eap_peer_config *eap)
  1518. {
  1519. os_free(eap->eap_methods);
  1520. os_free(eap->identity);
  1521. os_free(eap->anonymous_identity);
  1522. os_free(eap->password);
  1523. os_free(eap->ca_cert);
  1524. os_free(eap->ca_path);
  1525. os_free(eap->client_cert);
  1526. os_free(eap->private_key);
  1527. os_free(eap->private_key_passwd);
  1528. os_free(eap->dh_file);
  1529. os_free(eap->subject_match);
  1530. os_free(eap->altsubject_match);
  1531. os_free(eap->domain_suffix_match);
  1532. os_free(eap->ca_cert2);
  1533. os_free(eap->ca_path2);
  1534. os_free(eap->client_cert2);
  1535. os_free(eap->private_key2);
  1536. os_free(eap->private_key2_passwd);
  1537. os_free(eap->dh_file2);
  1538. os_free(eap->subject_match2);
  1539. os_free(eap->altsubject_match2);
  1540. os_free(eap->domain_suffix_match2);
  1541. os_free(eap->phase1);
  1542. os_free(eap->phase2);
  1543. os_free(eap->pcsc);
  1544. os_free(eap->pin);
  1545. os_free(eap->engine_id);
  1546. os_free(eap->key_id);
  1547. os_free(eap->cert_id);
  1548. os_free(eap->ca_cert_id);
  1549. os_free(eap->key2_id);
  1550. os_free(eap->cert2_id);
  1551. os_free(eap->ca_cert2_id);
  1552. os_free(eap->pin2);
  1553. os_free(eap->engine2_id);
  1554. os_free(eap->otp);
  1555. os_free(eap->pending_req_otp);
  1556. os_free(eap->pac_file);
  1557. os_free(eap->new_password);
  1558. os_free(eap->external_sim_resp);
  1559. }
  1560. #endif /* IEEE8021X_EAPOL */
  1561. /**
  1562. * wpa_config_free_ssid - Free network/ssid configuration data
  1563. * @ssid: Configuration data for the network
  1564. *
  1565. * This function frees all resources allocated for the network configuration
  1566. * data.
  1567. */
  1568. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1569. {
  1570. struct psk_list_entry *psk;
  1571. os_free(ssid->ssid);
  1572. os_free(ssid->passphrase);
  1573. os_free(ssid->ext_psk);
  1574. #ifdef IEEE8021X_EAPOL
  1575. eap_peer_config_free(&ssid->eap);
  1576. #endif /* IEEE8021X_EAPOL */
  1577. os_free(ssid->id_str);
  1578. os_free(ssid->scan_freq);
  1579. os_free(ssid->freq_list);
  1580. os_free(ssid->bgscan);
  1581. os_free(ssid->p2p_client_list);
  1582. #ifdef CONFIG_HT_OVERRIDES
  1583. os_free(ssid->ht_mcs);
  1584. #endif /* CONFIG_HT_OVERRIDES */
  1585. while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
  1586. list))) {
  1587. dl_list_del(&psk->list);
  1588. os_free(psk);
  1589. }
  1590. os_free(ssid);
  1591. }
  1592. void wpa_config_free_cred(struct wpa_cred *cred)
  1593. {
  1594. size_t i;
  1595. os_free(cred->realm);
  1596. os_free(cred->username);
  1597. os_free(cred->password);
  1598. os_free(cred->ca_cert);
  1599. os_free(cred->client_cert);
  1600. os_free(cred->private_key);
  1601. os_free(cred->private_key_passwd);
  1602. os_free(cred->imsi);
  1603. os_free(cred->milenage);
  1604. for (i = 0; i < cred->num_domain; i++)
  1605. os_free(cred->domain[i]);
  1606. os_free(cred->domain);
  1607. os_free(cred->domain_suffix_match);
  1608. os_free(cred->eap_method);
  1609. os_free(cred->phase1);
  1610. os_free(cred->phase2);
  1611. os_free(cred->excluded_ssid);
  1612. os_free(cred);
  1613. }
  1614. /**
  1615. * wpa_config_free - Free configuration data
  1616. * @config: Configuration data from wpa_config_read()
  1617. *
  1618. * This function frees all resources allocated for the configuration data by
  1619. * wpa_config_read().
  1620. */
  1621. void wpa_config_free(struct wpa_config *config)
  1622. {
  1623. #ifndef CONFIG_NO_CONFIG_BLOBS
  1624. struct wpa_config_blob *blob, *prevblob;
  1625. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1626. struct wpa_ssid *ssid, *prev = NULL;
  1627. struct wpa_cred *cred, *cprev;
  1628. ssid = config->ssid;
  1629. while (ssid) {
  1630. prev = ssid;
  1631. ssid = ssid->next;
  1632. wpa_config_free_ssid(prev);
  1633. }
  1634. cred = config->cred;
  1635. while (cred) {
  1636. cprev = cred;
  1637. cred = cred->next;
  1638. wpa_config_free_cred(cprev);
  1639. }
  1640. #ifndef CONFIG_NO_CONFIG_BLOBS
  1641. blob = config->blobs;
  1642. prevblob = NULL;
  1643. while (blob) {
  1644. prevblob = blob;
  1645. blob = blob->next;
  1646. wpa_config_free_blob(prevblob);
  1647. }
  1648. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1649. wpabuf_free(config->wps_vendor_ext_m1);
  1650. os_free(config->ctrl_interface);
  1651. os_free(config->ctrl_interface_group);
  1652. os_free(config->opensc_engine_path);
  1653. os_free(config->pkcs11_engine_path);
  1654. os_free(config->pkcs11_module_path);
  1655. os_free(config->pcsc_reader);
  1656. os_free(config->pcsc_pin);
  1657. os_free(config->driver_param);
  1658. os_free(config->device_name);
  1659. os_free(config->manufacturer);
  1660. os_free(config->model_name);
  1661. os_free(config->model_number);
  1662. os_free(config->serial_number);
  1663. os_free(config->config_methods);
  1664. os_free(config->p2p_ssid_postfix);
  1665. os_free(config->pssid);
  1666. os_free(config->p2p_pref_chan);
  1667. os_free(config->p2p_no_go_freq.range);
  1668. os_free(config->autoscan);
  1669. os_free(config->freq_list);
  1670. wpabuf_free(config->wps_nfc_dh_pubkey);
  1671. wpabuf_free(config->wps_nfc_dh_privkey);
  1672. wpabuf_free(config->wps_nfc_dev_pw);
  1673. os_free(config->ext_password_backend);
  1674. os_free(config->sae_groups);
  1675. wpabuf_free(config->ap_vendor_elements);
  1676. os_free(config);
  1677. }
  1678. /**
  1679. * wpa_config_foreach_network - Iterate over each configured network
  1680. * @config: Configuration data from wpa_config_read()
  1681. * @func: Callback function to process each network
  1682. * @arg: Opaque argument to pass to callback function
  1683. *
  1684. * Iterate over the set of configured networks calling the specified
  1685. * function for each item. We guard against callbacks removing the
  1686. * supplied network.
  1687. */
  1688. void wpa_config_foreach_network(struct wpa_config *config,
  1689. void (*func)(void *, struct wpa_ssid *),
  1690. void *arg)
  1691. {
  1692. struct wpa_ssid *ssid, *next;
  1693. ssid = config->ssid;
  1694. while (ssid) {
  1695. next = ssid->next;
  1696. func(arg, ssid);
  1697. ssid = next;
  1698. }
  1699. }
  1700. /**
  1701. * wpa_config_get_network - Get configured network based on id
  1702. * @config: Configuration data from wpa_config_read()
  1703. * @id: Unique network id to search for
  1704. * Returns: Network configuration or %NULL if not found
  1705. */
  1706. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1707. {
  1708. struct wpa_ssid *ssid;
  1709. ssid = config->ssid;
  1710. while (ssid) {
  1711. if (id == ssid->id)
  1712. break;
  1713. ssid = ssid->next;
  1714. }
  1715. return ssid;
  1716. }
  1717. /**
  1718. * wpa_config_add_network - Add a new network with empty configuration
  1719. * @config: Configuration data from wpa_config_read()
  1720. * Returns: The new network configuration or %NULL if operation failed
  1721. */
  1722. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1723. {
  1724. int id;
  1725. struct wpa_ssid *ssid, *last = NULL;
  1726. id = -1;
  1727. ssid = config->ssid;
  1728. while (ssid) {
  1729. if (ssid->id > id)
  1730. id = ssid->id;
  1731. last = ssid;
  1732. ssid = ssid->next;
  1733. }
  1734. id++;
  1735. ssid = os_zalloc(sizeof(*ssid));
  1736. if (ssid == NULL)
  1737. return NULL;
  1738. ssid->id = id;
  1739. dl_list_init(&ssid->psk_list);
  1740. if (last)
  1741. last->next = ssid;
  1742. else
  1743. config->ssid = ssid;
  1744. wpa_config_update_prio_list(config);
  1745. return ssid;
  1746. }
  1747. /**
  1748. * wpa_config_remove_network - Remove a configured network based on id
  1749. * @config: Configuration data from wpa_config_read()
  1750. * @id: Unique network id to search for
  1751. * Returns: 0 on success, or -1 if the network was not found
  1752. */
  1753. int wpa_config_remove_network(struct wpa_config *config, int id)
  1754. {
  1755. struct wpa_ssid *ssid, *prev = NULL;
  1756. ssid = config->ssid;
  1757. while (ssid) {
  1758. if (id == ssid->id)
  1759. break;
  1760. prev = ssid;
  1761. ssid = ssid->next;
  1762. }
  1763. if (ssid == NULL)
  1764. return -1;
  1765. if (prev)
  1766. prev->next = ssid->next;
  1767. else
  1768. config->ssid = ssid->next;
  1769. wpa_config_update_prio_list(config);
  1770. wpa_config_free_ssid(ssid);
  1771. return 0;
  1772. }
  1773. /**
  1774. * wpa_config_set_network_defaults - Set network default values
  1775. * @ssid: Pointer to network configuration data
  1776. */
  1777. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  1778. {
  1779. ssid->proto = DEFAULT_PROTO;
  1780. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  1781. ssid->group_cipher = DEFAULT_GROUP;
  1782. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  1783. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  1784. #ifdef IEEE8021X_EAPOL
  1785. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  1786. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  1787. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  1788. #endif /* IEEE8021X_EAPOL */
  1789. #ifdef CONFIG_HT_OVERRIDES
  1790. ssid->disable_ht = DEFAULT_DISABLE_HT;
  1791. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  1792. ssid->disable_sgi = DEFAULT_DISABLE_SGI;
  1793. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  1794. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  1795. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  1796. #endif /* CONFIG_HT_OVERRIDES */
  1797. #ifdef CONFIG_VHT_OVERRIDES
  1798. ssid->vht_rx_mcs_nss_1 = -1;
  1799. ssid->vht_rx_mcs_nss_2 = -1;
  1800. ssid->vht_rx_mcs_nss_3 = -1;
  1801. ssid->vht_rx_mcs_nss_4 = -1;
  1802. ssid->vht_rx_mcs_nss_5 = -1;
  1803. ssid->vht_rx_mcs_nss_6 = -1;
  1804. ssid->vht_rx_mcs_nss_7 = -1;
  1805. ssid->vht_rx_mcs_nss_8 = -1;
  1806. ssid->vht_tx_mcs_nss_1 = -1;
  1807. ssid->vht_tx_mcs_nss_2 = -1;
  1808. ssid->vht_tx_mcs_nss_3 = -1;
  1809. ssid->vht_tx_mcs_nss_4 = -1;
  1810. ssid->vht_tx_mcs_nss_5 = -1;
  1811. ssid->vht_tx_mcs_nss_6 = -1;
  1812. ssid->vht_tx_mcs_nss_7 = -1;
  1813. ssid->vht_tx_mcs_nss_8 = -1;
  1814. #endif /* CONFIG_VHT_OVERRIDES */
  1815. ssid->proactive_key_caching = -1;
  1816. #ifdef CONFIG_IEEE80211W
  1817. ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
  1818. #endif /* CONFIG_IEEE80211W */
  1819. }
  1820. /**
  1821. * wpa_config_set - Set a variable in network configuration
  1822. * @ssid: Pointer to network configuration data
  1823. * @var: Variable name, e.g., "ssid"
  1824. * @value: Variable value
  1825. * @line: Line number in configuration file or 0 if not used
  1826. * Returns: 0 on success, -1 on failure
  1827. *
  1828. * This function can be used to set network configuration variables based on
  1829. * both the configuration file and management interface input. The value
  1830. * parameter must be in the same format as the text-based configuration file is
  1831. * using. For example, strings are using double quotation marks.
  1832. */
  1833. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  1834. int line)
  1835. {
  1836. size_t i;
  1837. int ret = 0;
  1838. if (ssid == NULL || var == NULL || value == NULL)
  1839. return -1;
  1840. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1841. const struct parse_data *field = &ssid_fields[i];
  1842. if (os_strcmp(var, field->name) != 0)
  1843. continue;
  1844. if (field->parser(field, ssid, line, value)) {
  1845. if (line) {
  1846. wpa_printf(MSG_ERROR, "Line %d: failed to "
  1847. "parse %s '%s'.", line, var, value);
  1848. }
  1849. ret = -1;
  1850. }
  1851. break;
  1852. }
  1853. if (i == NUM_SSID_FIELDS) {
  1854. if (line) {
  1855. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  1856. "'%s'.", line, var);
  1857. }
  1858. ret = -1;
  1859. }
  1860. return ret;
  1861. }
  1862. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  1863. const char *value)
  1864. {
  1865. size_t len;
  1866. char *buf;
  1867. int ret;
  1868. len = os_strlen(value);
  1869. buf = os_malloc(len + 3);
  1870. if (buf == NULL)
  1871. return -1;
  1872. buf[0] = '"';
  1873. os_memcpy(buf + 1, value, len);
  1874. buf[len + 1] = '"';
  1875. buf[len + 2] = '\0';
  1876. ret = wpa_config_set(ssid, var, buf, 0);
  1877. os_free(buf);
  1878. return ret;
  1879. }
  1880. /**
  1881. * wpa_config_get_all - Get all options from network configuration
  1882. * @ssid: Pointer to network configuration data
  1883. * @get_keys: Determines if keys/passwords will be included in returned list
  1884. * (if they may be exported)
  1885. * Returns: %NULL terminated list of all set keys and their values in the form
  1886. * of [key1, val1, key2, val2, ... , NULL]
  1887. *
  1888. * This function can be used to get list of all configured network properties.
  1889. * The caller is responsible for freeing the returned list and all its
  1890. * elements.
  1891. */
  1892. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  1893. {
  1894. const struct parse_data *field;
  1895. char *key, *value;
  1896. size_t i;
  1897. char **props;
  1898. int fields_num;
  1899. get_keys = get_keys && ssid->export_keys;
  1900. props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
  1901. if (!props)
  1902. return NULL;
  1903. fields_num = 0;
  1904. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1905. field = &ssid_fields[i];
  1906. if (field->key_data && !get_keys)
  1907. continue;
  1908. value = field->writer(field, ssid);
  1909. if (value == NULL)
  1910. continue;
  1911. if (os_strlen(value) == 0) {
  1912. os_free(value);
  1913. continue;
  1914. }
  1915. key = os_strdup(field->name);
  1916. if (key == NULL) {
  1917. os_free(value);
  1918. goto err;
  1919. }
  1920. props[fields_num * 2] = key;
  1921. props[fields_num * 2 + 1] = value;
  1922. fields_num++;
  1923. }
  1924. return props;
  1925. err:
  1926. value = *props;
  1927. while (value)
  1928. os_free(value++);
  1929. os_free(props);
  1930. return NULL;
  1931. }
  1932. #ifndef NO_CONFIG_WRITE
  1933. /**
  1934. * wpa_config_get - Get a variable in network configuration
  1935. * @ssid: Pointer to network configuration data
  1936. * @var: Variable name, e.g., "ssid"
  1937. * Returns: Value of the variable or %NULL on failure
  1938. *
  1939. * This function can be used to get network configuration variables. The
  1940. * returned value is a copy of the configuration variable in text format, i.e,.
  1941. * the same format that the text-based configuration file and wpa_config_set()
  1942. * are using for the value. The caller is responsible for freeing the returned
  1943. * value.
  1944. */
  1945. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  1946. {
  1947. size_t i;
  1948. if (ssid == NULL || var == NULL)
  1949. return NULL;
  1950. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1951. const struct parse_data *field = &ssid_fields[i];
  1952. if (os_strcmp(var, field->name) == 0)
  1953. return field->writer(field, ssid);
  1954. }
  1955. return NULL;
  1956. }
  1957. /**
  1958. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  1959. * @ssid: Pointer to network configuration data
  1960. * @var: Variable name, e.g., "ssid"
  1961. * Returns: Value of the variable or %NULL on failure
  1962. *
  1963. * This function can be used to get network configuration variable like
  1964. * wpa_config_get(). The only difference is that this functions does not expose
  1965. * key/password material from the configuration. In case a key/password field
  1966. * is requested, the returned value is an empty string or %NULL if the variable
  1967. * is not set or "*" if the variable is set (regardless of its value). The
  1968. * returned value is a copy of the configuration variable in text format, i.e,.
  1969. * the same format that the text-based configuration file and wpa_config_set()
  1970. * are using for the value. The caller is responsible for freeing the returned
  1971. * value.
  1972. */
  1973. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  1974. {
  1975. size_t i;
  1976. if (ssid == NULL || var == NULL)
  1977. return NULL;
  1978. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  1979. const struct parse_data *field = &ssid_fields[i];
  1980. if (os_strcmp(var, field->name) == 0) {
  1981. char *res = field->writer(field, ssid);
  1982. if (field->key_data) {
  1983. if (res && res[0]) {
  1984. wpa_printf(MSG_DEBUG, "Do not allow "
  1985. "key_data field to be "
  1986. "exposed");
  1987. os_free(res);
  1988. return os_strdup("*");
  1989. }
  1990. os_free(res);
  1991. return NULL;
  1992. }
  1993. return res;
  1994. }
  1995. }
  1996. return NULL;
  1997. }
  1998. #endif /* NO_CONFIG_WRITE */
  1999. /**
  2000. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  2001. * @ssid: Pointer to network configuration data
  2002. *
  2003. * This function must be called to update WPA PSK when either SSID or the
  2004. * passphrase has changed for the network configuration.
  2005. */
  2006. void wpa_config_update_psk(struct wpa_ssid *ssid)
  2007. {
  2008. #ifndef CONFIG_NO_PBKDF2
  2009. pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
  2010. ssid->psk, PMK_LEN);
  2011. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  2012. ssid->psk, PMK_LEN);
  2013. ssid->psk_set = 1;
  2014. #endif /* CONFIG_NO_PBKDF2 */
  2015. }
  2016. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  2017. const char *value, int line)
  2018. {
  2019. char *val;
  2020. size_t len;
  2021. if (os_strcmp(var, "priority") == 0) {
  2022. cred->priority = atoi(value);
  2023. return 0;
  2024. }
  2025. if (os_strcmp(var, "pcsc") == 0) {
  2026. cred->pcsc = atoi(value);
  2027. return 0;
  2028. }
  2029. if (os_strcmp(var, "eap") == 0) {
  2030. struct eap_method_type method;
  2031. method.method = eap_peer_get_type(value, &method.vendor);
  2032. if (method.vendor == EAP_VENDOR_IETF &&
  2033. method.method == EAP_TYPE_NONE) {
  2034. wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
  2035. "for a credential", line, value);
  2036. return -1;
  2037. }
  2038. os_free(cred->eap_method);
  2039. cred->eap_method = os_malloc(sizeof(*cred->eap_method));
  2040. if (cred->eap_method == NULL)
  2041. return -1;
  2042. os_memcpy(cred->eap_method, &method, sizeof(method));
  2043. return 0;
  2044. }
  2045. if (os_strcmp(var, "password") == 0 &&
  2046. os_strncmp(value, "ext:", 4) == 0) {
  2047. os_free(cred->password);
  2048. cred->password = os_strdup(value);
  2049. cred->ext_password = 1;
  2050. return 0;
  2051. }
  2052. val = wpa_config_parse_string(value, &len);
  2053. if (val == NULL) {
  2054. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  2055. "value '%s'.", line, var, value);
  2056. return -1;
  2057. }
  2058. if (os_strcmp(var, "realm") == 0) {
  2059. os_free(cred->realm);
  2060. cred->realm = val;
  2061. return 0;
  2062. }
  2063. if (os_strcmp(var, "username") == 0) {
  2064. os_free(cred->username);
  2065. cred->username = val;
  2066. return 0;
  2067. }
  2068. if (os_strcmp(var, "password") == 0) {
  2069. os_free(cred->password);
  2070. cred->password = val;
  2071. cred->ext_password = 0;
  2072. return 0;
  2073. }
  2074. if (os_strcmp(var, "ca_cert") == 0) {
  2075. os_free(cred->ca_cert);
  2076. cred->ca_cert = val;
  2077. return 0;
  2078. }
  2079. if (os_strcmp(var, "client_cert") == 0) {
  2080. os_free(cred->client_cert);
  2081. cred->client_cert = val;
  2082. return 0;
  2083. }
  2084. if (os_strcmp(var, "private_key") == 0) {
  2085. os_free(cred->private_key);
  2086. cred->private_key = val;
  2087. return 0;
  2088. }
  2089. if (os_strcmp(var, "private_key_passwd") == 0) {
  2090. os_free(cred->private_key_passwd);
  2091. cred->private_key_passwd = val;
  2092. return 0;
  2093. }
  2094. if (os_strcmp(var, "imsi") == 0) {
  2095. os_free(cred->imsi);
  2096. cred->imsi = val;
  2097. return 0;
  2098. }
  2099. if (os_strcmp(var, "milenage") == 0) {
  2100. os_free(cred->milenage);
  2101. cred->milenage = val;
  2102. return 0;
  2103. }
  2104. if (os_strcmp(var, "domain_suffix_match") == 0) {
  2105. os_free(cred->domain_suffix_match);
  2106. cred->domain_suffix_match = val;
  2107. return 0;
  2108. }
  2109. if (os_strcmp(var, "domain") == 0) {
  2110. char **new_domain;
  2111. new_domain = os_realloc_array(cred->domain,
  2112. cred->num_domain + 1,
  2113. sizeof(char *));
  2114. if (new_domain == NULL) {
  2115. os_free(val);
  2116. return -1;
  2117. }
  2118. new_domain[cred->num_domain++] = val;
  2119. cred->domain = new_domain;
  2120. return 0;
  2121. }
  2122. if (os_strcmp(var, "phase1") == 0) {
  2123. os_free(cred->phase1);
  2124. cred->phase1 = val;
  2125. return 0;
  2126. }
  2127. if (os_strcmp(var, "phase2") == 0) {
  2128. os_free(cred->phase2);
  2129. cred->phase2 = val;
  2130. return 0;
  2131. }
  2132. if (os_strcmp(var, "roaming_consortium") == 0) {
  2133. if (len < 3 || len > sizeof(cred->roaming_consortium)) {
  2134. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2135. "roaming_consortium length %d (3..15 "
  2136. "expected)", line, (int) len);
  2137. os_free(val);
  2138. return -1;
  2139. }
  2140. os_memcpy(cred->roaming_consortium, val, len);
  2141. cred->roaming_consortium_len = len;
  2142. os_free(val);
  2143. return 0;
  2144. }
  2145. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  2146. if (len < 3 || len > sizeof(cred->required_roaming_consortium))
  2147. {
  2148. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2149. "required_roaming_consortium length %d "
  2150. "(3..15 expected)", line, (int) len);
  2151. os_free(val);
  2152. return -1;
  2153. }
  2154. os_memcpy(cred->required_roaming_consortium, val, len);
  2155. cred->required_roaming_consortium_len = len;
  2156. os_free(val);
  2157. return 0;
  2158. }
  2159. if (os_strcmp(var, "excluded_ssid") == 0) {
  2160. struct excluded_ssid *e;
  2161. if (len > MAX_SSID_LEN) {
  2162. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2163. "excluded_ssid length %d", line, (int) len);
  2164. os_free(val);
  2165. return -1;
  2166. }
  2167. e = os_realloc_array(cred->excluded_ssid,
  2168. cred->num_excluded_ssid + 1,
  2169. sizeof(struct excluded_ssid));
  2170. if (e == NULL) {
  2171. os_free(val);
  2172. return -1;
  2173. }
  2174. cred->excluded_ssid = e;
  2175. e = &cred->excluded_ssid[cred->num_excluded_ssid++];
  2176. os_memcpy(e->ssid, val, len);
  2177. e->ssid_len = len;
  2178. os_free(val);
  2179. return 0;
  2180. }
  2181. if (line) {
  2182. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  2183. line, var);
  2184. }
  2185. os_free(val);
  2186. return -1;
  2187. }
  2188. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  2189. {
  2190. struct wpa_cred *cred;
  2191. cred = config->cred;
  2192. while (cred) {
  2193. if (id == cred->id)
  2194. break;
  2195. cred = cred->next;
  2196. }
  2197. return cred;
  2198. }
  2199. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  2200. {
  2201. int id;
  2202. struct wpa_cred *cred, *last = NULL;
  2203. id = -1;
  2204. cred = config->cred;
  2205. while (cred) {
  2206. if (cred->id > id)
  2207. id = cred->id;
  2208. last = cred;
  2209. cred = cred->next;
  2210. }
  2211. id++;
  2212. cred = os_zalloc(sizeof(*cred));
  2213. if (cred == NULL)
  2214. return NULL;
  2215. cred->id = id;
  2216. if (last)
  2217. last->next = cred;
  2218. else
  2219. config->cred = cred;
  2220. return cred;
  2221. }
  2222. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2223. {
  2224. struct wpa_cred *cred, *prev = NULL;
  2225. cred = config->cred;
  2226. while (cred) {
  2227. if (id == cred->id)
  2228. break;
  2229. prev = cred;
  2230. cred = cred->next;
  2231. }
  2232. if (cred == NULL)
  2233. return -1;
  2234. if (prev)
  2235. prev->next = cred->next;
  2236. else
  2237. config->cred = cred->next;
  2238. wpa_config_free_cred(cred);
  2239. return 0;
  2240. }
  2241. #ifndef CONFIG_NO_CONFIG_BLOBS
  2242. /**
  2243. * wpa_config_get_blob - Get a named configuration blob
  2244. * @config: Configuration data from wpa_config_read()
  2245. * @name: Name of the blob
  2246. * Returns: Pointer to blob data or %NULL if not found
  2247. */
  2248. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  2249. const char *name)
  2250. {
  2251. struct wpa_config_blob *blob = config->blobs;
  2252. while (blob) {
  2253. if (os_strcmp(blob->name, name) == 0)
  2254. return blob;
  2255. blob = blob->next;
  2256. }
  2257. return NULL;
  2258. }
  2259. /**
  2260. * wpa_config_set_blob - Set or add a named configuration blob
  2261. * @config: Configuration data from wpa_config_read()
  2262. * @blob: New value for the blob
  2263. *
  2264. * Adds a new configuration blob or replaces the current value of an existing
  2265. * blob.
  2266. */
  2267. void wpa_config_set_blob(struct wpa_config *config,
  2268. struct wpa_config_blob *blob)
  2269. {
  2270. wpa_config_remove_blob(config, blob->name);
  2271. blob->next = config->blobs;
  2272. config->blobs = blob;
  2273. }
  2274. /**
  2275. * wpa_config_free_blob - Free blob data
  2276. * @blob: Pointer to blob to be freed
  2277. */
  2278. void wpa_config_free_blob(struct wpa_config_blob *blob)
  2279. {
  2280. if (blob) {
  2281. os_free(blob->name);
  2282. os_free(blob->data);
  2283. os_free(blob);
  2284. }
  2285. }
  2286. /**
  2287. * wpa_config_remove_blob - Remove a named configuration blob
  2288. * @config: Configuration data from wpa_config_read()
  2289. * @name: Name of the blob to remove
  2290. * Returns: 0 if blob was removed or -1 if blob was not found
  2291. */
  2292. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  2293. {
  2294. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  2295. while (pos) {
  2296. if (os_strcmp(pos->name, name) == 0) {
  2297. if (prev)
  2298. prev->next = pos->next;
  2299. else
  2300. config->blobs = pos->next;
  2301. wpa_config_free_blob(pos);
  2302. return 0;
  2303. }
  2304. prev = pos;
  2305. pos = pos->next;
  2306. }
  2307. return -1;
  2308. }
  2309. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2310. /**
  2311. * wpa_config_alloc_empty - Allocate an empty configuration
  2312. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  2313. * socket
  2314. * @driver_param: Driver parameters
  2315. * Returns: Pointer to allocated configuration data or %NULL on failure
  2316. */
  2317. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  2318. const char *driver_param)
  2319. {
  2320. struct wpa_config *config;
  2321. const int aCWmin = 4, aCWmax = 10;
  2322. const struct hostapd_wmm_ac_params ac_bk =
  2323. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  2324. const struct hostapd_wmm_ac_params ac_be =
  2325. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  2326. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  2327. { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
  2328. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  2329. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
  2330. config = os_zalloc(sizeof(*config));
  2331. if (config == NULL)
  2332. return NULL;
  2333. config->eapol_version = DEFAULT_EAPOL_VERSION;
  2334. config->ap_scan = DEFAULT_AP_SCAN;
  2335. config->fast_reauth = DEFAULT_FAST_REAUTH;
  2336. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  2337. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  2338. config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
  2339. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  2340. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  2341. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  2342. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  2343. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  2344. config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
  2345. config->wmm_ac_params[0] = ac_be;
  2346. config->wmm_ac_params[1] = ac_bk;
  2347. config->wmm_ac_params[2] = ac_vi;
  2348. config->wmm_ac_params[3] = ac_vo;
  2349. if (ctrl_interface)
  2350. config->ctrl_interface = os_strdup(ctrl_interface);
  2351. if (driver_param)
  2352. config->driver_param = os_strdup(driver_param);
  2353. return config;
  2354. }
  2355. #ifndef CONFIG_NO_STDOUT_DEBUG
  2356. /**
  2357. * wpa_config_debug_dump_networks - Debug dump of configured networks
  2358. * @config: Configuration data from wpa_config_read()
  2359. */
  2360. void wpa_config_debug_dump_networks(struct wpa_config *config)
  2361. {
  2362. int prio;
  2363. struct wpa_ssid *ssid;
  2364. for (prio = 0; prio < config->num_prio; prio++) {
  2365. ssid = config->pssid[prio];
  2366. wpa_printf(MSG_DEBUG, "Priority group %d",
  2367. ssid->priority);
  2368. while (ssid) {
  2369. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  2370. ssid->id,
  2371. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2372. ssid = ssid->pnext;
  2373. }
  2374. }
  2375. }
  2376. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2377. struct global_parse_data {
  2378. char *name;
  2379. int (*parser)(const struct global_parse_data *data,
  2380. struct wpa_config *config, int line, const char *value);
  2381. void *param1, *param2, *param3;
  2382. unsigned int changed_flag;
  2383. };
  2384. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  2385. struct wpa_config *config, int line,
  2386. const char *pos)
  2387. {
  2388. int val, *dst;
  2389. char *end;
  2390. dst = (int *) (((u8 *) config) + (long) data->param1);
  2391. val = strtol(pos, &end, 0);
  2392. if (*end) {
  2393. wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
  2394. line, pos);
  2395. return -1;
  2396. }
  2397. *dst = val;
  2398. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  2399. if (data->param2 && *dst < (long) data->param2) {
  2400. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  2401. "min_value=%ld)", line, data->name, *dst,
  2402. (long) data->param2);
  2403. *dst = (long) data->param2;
  2404. return -1;
  2405. }
  2406. if (data->param3 && *dst > (long) data->param3) {
  2407. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  2408. "max_value=%ld)", line, data->name, *dst,
  2409. (long) data->param3);
  2410. *dst = (long) data->param3;
  2411. return -1;
  2412. }
  2413. return 0;
  2414. }
  2415. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  2416. struct wpa_config *config, int line,
  2417. const char *pos)
  2418. {
  2419. size_t len;
  2420. char **dst, *tmp;
  2421. len = os_strlen(pos);
  2422. if (data->param2 && len < (size_t) data->param2) {
  2423. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  2424. "min_len=%ld)", line, data->name,
  2425. (unsigned long) len, (long) data->param2);
  2426. return -1;
  2427. }
  2428. if (data->param3 && len > (size_t) data->param3) {
  2429. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  2430. "max_len=%ld)", line, data->name,
  2431. (unsigned long) len, (long) data->param3);
  2432. return -1;
  2433. }
  2434. tmp = os_strdup(pos);
  2435. if (tmp == NULL)
  2436. return -1;
  2437. dst = (char **) (((u8 *) config) + (long) data->param1);
  2438. os_free(*dst);
  2439. *dst = tmp;
  2440. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  2441. return 0;
  2442. }
  2443. static int wpa_config_process_bgscan(const struct global_parse_data *data,
  2444. struct wpa_config *config, int line,
  2445. const char *pos)
  2446. {
  2447. size_t len;
  2448. char *tmp;
  2449. tmp = wpa_config_parse_string(pos, &len);
  2450. if (tmp == NULL) {
  2451. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
  2452. line, data->name);
  2453. return -1;
  2454. }
  2455. return wpa_global_config_parse_str(data, config, line, tmp);
  2456. }
  2457. static int wpa_global_config_parse_bin(const struct global_parse_data *data,
  2458. struct wpa_config *config, int line,
  2459. const char *pos)
  2460. {
  2461. size_t len;
  2462. struct wpabuf **dst, *tmp;
  2463. len = os_strlen(pos);
  2464. if (len & 0x01)
  2465. return -1;
  2466. tmp = wpabuf_alloc(len / 2);
  2467. if (tmp == NULL)
  2468. return -1;
  2469. if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
  2470. wpabuf_free(tmp);
  2471. return -1;
  2472. }
  2473. dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
  2474. wpabuf_free(*dst);
  2475. *dst = tmp;
  2476. wpa_printf(MSG_DEBUG, "%s", data->name);
  2477. return 0;
  2478. }
  2479. static int wpa_config_process_freq_list(const struct global_parse_data *data,
  2480. struct wpa_config *config, int line,
  2481. const char *value)
  2482. {
  2483. int *freqs;
  2484. freqs = wpa_config_parse_int_array(value);
  2485. if (freqs == NULL)
  2486. return -1;
  2487. if (freqs[0] == 0) {
  2488. os_free(freqs);
  2489. freqs = NULL;
  2490. }
  2491. os_free(config->freq_list);
  2492. config->freq_list = freqs;
  2493. return 0;
  2494. }
  2495. static int wpa_config_process_country(const struct global_parse_data *data,
  2496. struct wpa_config *config, int line,
  2497. const char *pos)
  2498. {
  2499. if (!pos[0] || !pos[1]) {
  2500. wpa_printf(MSG_DEBUG, "Invalid country set");
  2501. return -1;
  2502. }
  2503. config->country[0] = pos[0];
  2504. config->country[1] = pos[1];
  2505. wpa_printf(MSG_DEBUG, "country='%c%c'",
  2506. config->country[0], config->country[1]);
  2507. return 0;
  2508. }
  2509. static int wpa_config_process_load_dynamic_eap(
  2510. const struct global_parse_data *data, struct wpa_config *config,
  2511. int line, const char *so)
  2512. {
  2513. int ret;
  2514. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  2515. ret = eap_peer_method_load(so);
  2516. if (ret == -2) {
  2517. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  2518. "reloading.");
  2519. } else if (ret) {
  2520. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  2521. "method '%s'.", line, so);
  2522. return -1;
  2523. }
  2524. return 0;
  2525. }
  2526. #ifdef CONFIG_WPS
  2527. static int wpa_config_process_uuid(const struct global_parse_data *data,
  2528. struct wpa_config *config, int line,
  2529. const char *pos)
  2530. {
  2531. char buf[40];
  2532. if (uuid_str2bin(pos, config->uuid)) {
  2533. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  2534. return -1;
  2535. }
  2536. uuid_bin2str(config->uuid, buf, sizeof(buf));
  2537. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  2538. return 0;
  2539. }
  2540. static int wpa_config_process_device_type(
  2541. const struct global_parse_data *data,
  2542. struct wpa_config *config, int line, const char *pos)
  2543. {
  2544. return wps_dev_type_str2bin(pos, config->device_type);
  2545. }
  2546. static int wpa_config_process_os_version(const struct global_parse_data *data,
  2547. struct wpa_config *config, int line,
  2548. const char *pos)
  2549. {
  2550. if (hexstr2bin(pos, config->os_version, 4)) {
  2551. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  2552. return -1;
  2553. }
  2554. wpa_printf(MSG_DEBUG, "os_version=%08x",
  2555. WPA_GET_BE32(config->os_version));
  2556. return 0;
  2557. }
  2558. static int wpa_config_process_wps_vendor_ext_m1(
  2559. const struct global_parse_data *data,
  2560. struct wpa_config *config, int line, const char *pos)
  2561. {
  2562. struct wpabuf *tmp;
  2563. int len = os_strlen(pos) / 2;
  2564. u8 *p;
  2565. if (!len) {
  2566. wpa_printf(MSG_ERROR, "Line %d: "
  2567. "invalid wps_vendor_ext_m1", line);
  2568. return -1;
  2569. }
  2570. tmp = wpabuf_alloc(len);
  2571. if (tmp) {
  2572. p = wpabuf_put(tmp, len);
  2573. if (hexstr2bin(pos, p, len)) {
  2574. wpa_printf(MSG_ERROR, "Line %d: "
  2575. "invalid wps_vendor_ext_m1", line);
  2576. wpabuf_free(tmp);
  2577. return -1;
  2578. }
  2579. wpabuf_free(config->wps_vendor_ext_m1);
  2580. config->wps_vendor_ext_m1 = tmp;
  2581. } else {
  2582. wpa_printf(MSG_ERROR, "Can not allocate "
  2583. "memory for wps_vendor_ext_m1");
  2584. return -1;
  2585. }
  2586. return 0;
  2587. }
  2588. #endif /* CONFIG_WPS */
  2589. #ifdef CONFIG_P2P
  2590. static int wpa_config_process_sec_device_type(
  2591. const struct global_parse_data *data,
  2592. struct wpa_config *config, int line, const char *pos)
  2593. {
  2594. int idx;
  2595. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  2596. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  2597. "items", line);
  2598. return -1;
  2599. }
  2600. idx = config->num_sec_device_types;
  2601. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  2602. return -1;
  2603. config->num_sec_device_types++;
  2604. return 0;
  2605. }
  2606. static int wpa_config_process_p2p_pref_chan(
  2607. const struct global_parse_data *data,
  2608. struct wpa_config *config, int line, const char *pos)
  2609. {
  2610. struct p2p_channel *pref = NULL, *n;
  2611. unsigned int num = 0;
  2612. const char *pos2;
  2613. u8 op_class, chan;
  2614. /* format: class:chan,class:chan,... */
  2615. while (*pos) {
  2616. op_class = atoi(pos);
  2617. pos2 = os_strchr(pos, ':');
  2618. if (pos2 == NULL)
  2619. goto fail;
  2620. pos2++;
  2621. chan = atoi(pos2);
  2622. n = os_realloc_array(pref, num + 1,
  2623. sizeof(struct p2p_channel));
  2624. if (n == NULL)
  2625. goto fail;
  2626. pref = n;
  2627. pref[num].op_class = op_class;
  2628. pref[num].chan = chan;
  2629. num++;
  2630. pos = os_strchr(pos2, ',');
  2631. if (pos == NULL)
  2632. break;
  2633. pos++;
  2634. }
  2635. os_free(config->p2p_pref_chan);
  2636. config->p2p_pref_chan = pref;
  2637. config->num_p2p_pref_chan = num;
  2638. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  2639. (u8 *) config->p2p_pref_chan,
  2640. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  2641. return 0;
  2642. fail:
  2643. os_free(pref);
  2644. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  2645. return -1;
  2646. }
  2647. static int wpa_config_process_p2p_no_go_freq(
  2648. const struct global_parse_data *data,
  2649. struct wpa_config *config, int line, const char *pos)
  2650. {
  2651. int ret;
  2652. ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
  2653. if (ret < 0) {
  2654. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
  2655. return -1;
  2656. }
  2657. wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
  2658. config->p2p_no_go_freq.num);
  2659. return 0;
  2660. }
  2661. #endif /* CONFIG_P2P */
  2662. static int wpa_config_process_hessid(
  2663. const struct global_parse_data *data,
  2664. struct wpa_config *config, int line, const char *pos)
  2665. {
  2666. if (hwaddr_aton2(pos, config->hessid) < 0) {
  2667. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  2668. line, pos);
  2669. return -1;
  2670. }
  2671. return 0;
  2672. }
  2673. static int wpa_config_process_sae_groups(
  2674. const struct global_parse_data *data,
  2675. struct wpa_config *config, int line, const char *pos)
  2676. {
  2677. int *groups = wpa_config_parse_int_array(pos);
  2678. if (groups == NULL) {
  2679. wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
  2680. line, pos);
  2681. return -1;
  2682. }
  2683. os_free(config->sae_groups);
  2684. config->sae_groups = groups;
  2685. return 0;
  2686. }
  2687. static int wpa_config_process_ap_vendor_elements(
  2688. const struct global_parse_data *data,
  2689. struct wpa_config *config, int line, const char *pos)
  2690. {
  2691. struct wpabuf *tmp;
  2692. int len = os_strlen(pos) / 2;
  2693. u8 *p;
  2694. if (!len) {
  2695. wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
  2696. line);
  2697. return -1;
  2698. }
  2699. tmp = wpabuf_alloc(len);
  2700. if (tmp) {
  2701. p = wpabuf_put(tmp, len);
  2702. if (hexstr2bin(pos, p, len)) {
  2703. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2704. "ap_vendor_elements", line);
  2705. wpabuf_free(tmp);
  2706. return -1;
  2707. }
  2708. wpabuf_free(config->ap_vendor_elements);
  2709. config->ap_vendor_elements = tmp;
  2710. } else {
  2711. wpa_printf(MSG_ERROR, "Cannot allocate memory for "
  2712. "ap_vendor_elements");
  2713. return -1;
  2714. }
  2715. return 0;
  2716. }
  2717. #ifdef CONFIG_CTRL_IFACE
  2718. static int wpa_config_process_no_ctrl_interface(
  2719. const struct global_parse_data *data,
  2720. struct wpa_config *config, int line, const char *pos)
  2721. {
  2722. wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
  2723. os_free(config->ctrl_interface);
  2724. config->ctrl_interface = NULL;
  2725. return 0;
  2726. }
  2727. #endif /* CONFIG_CTRL_IFACE */
  2728. #ifdef OFFSET
  2729. #undef OFFSET
  2730. #endif /* OFFSET */
  2731. /* OFFSET: Get offset of a variable within the wpa_config structure */
  2732. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  2733. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  2734. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  2735. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  2736. #define INT(f) _INT(f), NULL, NULL
  2737. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  2738. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  2739. #define STR(f) _STR(f), NULL, NULL
  2740. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  2741. #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
  2742. static const struct global_parse_data global_fields[] = {
  2743. #ifdef CONFIG_CTRL_IFACE
  2744. { STR(ctrl_interface), 0 },
  2745. { FUNC_NO_VAR(no_ctrl_interface), 0 },
  2746. { STR(ctrl_interface_group), 0 } /* deprecated */,
  2747. #endif /* CONFIG_CTRL_IFACE */
  2748. { INT_RANGE(eapol_version, 1, 2), 0 },
  2749. { INT(ap_scan), 0 },
  2750. { FUNC(bgscan), 0 },
  2751. { INT(disable_scan_offload), 0 },
  2752. { INT(fast_reauth), 0 },
  2753. { STR(opensc_engine_path), 0 },
  2754. { STR(pkcs11_engine_path), 0 },
  2755. { STR(pkcs11_module_path), 0 },
  2756. { STR(pcsc_reader), 0 },
  2757. { STR(pcsc_pin), 0 },
  2758. { INT(external_sim), 0 },
  2759. { STR(driver_param), 0 },
  2760. { INT(dot11RSNAConfigPMKLifetime), 0 },
  2761. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  2762. { INT(dot11RSNAConfigSATimeout), 0 },
  2763. #ifndef CONFIG_NO_CONFIG_WRITE
  2764. { INT(update_config), 0 },
  2765. #endif /* CONFIG_NO_CONFIG_WRITE */
  2766. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  2767. #ifdef CONFIG_WPS
  2768. { FUNC(uuid), CFG_CHANGED_UUID },
  2769. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  2770. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  2771. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  2772. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2773. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  2774. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  2775. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  2776. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  2777. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  2778. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  2779. #endif /* CONFIG_WPS */
  2780. #ifdef CONFIG_P2P
  2781. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  2782. { INT(p2p_listen_reg_class), 0 },
  2783. { INT(p2p_listen_channel), 0 },
  2784. { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
  2785. { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
  2786. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  2787. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  2788. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  2789. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  2790. { INT(p2p_group_idle), 0 },
  2791. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  2792. { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
  2793. { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
  2794. { INT(p2p_go_ht40), 0 },
  2795. { INT(p2p_go_vht), 0 },
  2796. { INT(p2p_disabled), 0 },
  2797. { INT(p2p_no_group_iface), 0 },
  2798. { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
  2799. #endif /* CONFIG_P2P */
  2800. { FUNC(country), CFG_CHANGED_COUNTRY },
  2801. { INT(bss_max_count), 0 },
  2802. { INT(bss_expiration_age), 0 },
  2803. { INT(bss_expiration_scan_count), 0 },
  2804. { INT_RANGE(filter_ssids, 0, 1), 0 },
  2805. { INT_RANGE(filter_rssi, -100, 0), 0 },
  2806. { INT(max_num_sta), 0 },
  2807. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  2808. #ifdef CONFIG_HS20
  2809. { INT_RANGE(hs20, 0, 1), 0 },
  2810. #endif /* CONFIG_HS20 */
  2811. { INT_RANGE(interworking, 0, 1), 0 },
  2812. { FUNC(hessid), 0 },
  2813. { INT_RANGE(access_network_type, 0, 15), 0 },
  2814. { INT_RANGE(pbc_in_m1, 0, 1), 0 },
  2815. { STR(autoscan), 0 },
  2816. { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
  2817. CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2818. { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2819. { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2820. { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  2821. { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
  2822. { INT(p2p_go_max_inactivity), 0 },
  2823. { INT_RANGE(auto_interworking, 0, 1), 0 },
  2824. { INT(okc), 0 },
  2825. { INT(pmf), 0 },
  2826. { FUNC(sae_groups), 0 },
  2827. { INT(dtim_period), 0 },
  2828. { INT(beacon_int), 0 },
  2829. { FUNC(ap_vendor_elements), 0 },
  2830. { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
  2831. { FUNC(freq_list), 0 },
  2832. { INT(scan_cur_freq), 0 },
  2833. { INT(sched_scan_interval), 0 },
  2834. };
  2835. #undef FUNC
  2836. #undef _INT
  2837. #undef INT
  2838. #undef INT_RANGE
  2839. #undef _STR
  2840. #undef STR
  2841. #undef STR_RANGE
  2842. #undef BIN
  2843. #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
  2844. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  2845. {
  2846. size_t i;
  2847. int ret = 0;
  2848. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  2849. const struct global_parse_data *field = &global_fields[i];
  2850. size_t flen = os_strlen(field->name);
  2851. if (os_strncmp(pos, field->name, flen) != 0 ||
  2852. pos[flen] != '=')
  2853. continue;
  2854. if (field->parser(field, config, line, pos + flen + 1)) {
  2855. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2856. "parse '%s'.", line, pos);
  2857. ret = -1;
  2858. }
  2859. if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
  2860. config->wps_nfc_pw_from_config = 1;
  2861. config->changed_parameters |= field->changed_flag;
  2862. break;
  2863. }
  2864. if (i == NUM_GLOBAL_FIELDS) {
  2865. #ifdef CONFIG_AP
  2866. if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
  2867. char *tmp = os_strchr(pos, '=');
  2868. if (tmp == NULL) {
  2869. if (line < 0)
  2870. return -1;
  2871. wpa_printf(MSG_ERROR, "Line %d: invalid line "
  2872. "'%s'", line, pos);
  2873. return -1;
  2874. }
  2875. *tmp++ = '\0';
  2876. if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
  2877. tmp)) {
  2878. wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
  2879. "AC item", line);
  2880. return -1;
  2881. }
  2882. }
  2883. #endif /* CONFIG_AP */
  2884. if (line < 0)
  2885. return -1;
  2886. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  2887. line, pos);
  2888. ret = -1;
  2889. }
  2890. return ret;
  2891. }