config.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499
  1. /*
  2. * WPA Supplicant / Configuration parser and common functions
  3. * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "utils/uuid.h"
  11. #include "utils/ip_addr.h"
  12. #include "crypto/sha1.h"
  13. #include "rsn_supp/wpa.h"
  14. #include "eap_peer/eap.h"
  15. #include "p2p/p2p.h"
  16. #include "fst/fst.h"
  17. #include "config.h"
  18. #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
  19. #define NO_CONFIG_WRITE
  20. #endif
  21. /*
  22. * Structure for network configuration parsing. This data is used to implement
  23. * a generic parser for each network block variable. The table of configuration
  24. * variables is defined below in this file (ssid_fields[]).
  25. */
  26. struct parse_data {
  27. /* Configuration variable name */
  28. char *name;
  29. /* Parser function for this variable. The parser functions return 0 or 1
  30. * to indicate success. Value 0 indicates that the parameter value may
  31. * have changed while value 1 means that the value did not change.
  32. * Error cases (failure to parse the string) are indicated by returning
  33. * -1. */
  34. int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
  35. int line, const char *value);
  36. #ifndef NO_CONFIG_WRITE
  37. /* Writer function (i.e., to get the variable in text format from
  38. * internal presentation). */
  39. char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
  40. #endif /* NO_CONFIG_WRITE */
  41. /* Variable specific parameters for the parser. */
  42. void *param1, *param2, *param3, *param4;
  43. /* 0 = this variable can be included in debug output and ctrl_iface
  44. * 1 = this variable contains key/private data and it must not be
  45. * included in debug output unless explicitly requested. In
  46. * addition, this variable will not be readable through the
  47. * ctrl_iface.
  48. */
  49. int key_data;
  50. };
  51. static int wpa_config_parse_str(const struct parse_data *data,
  52. struct wpa_ssid *ssid,
  53. int line, const char *value)
  54. {
  55. size_t res_len, *dst_len, prev_len;
  56. char **dst, *tmp;
  57. if (os_strcmp(value, "NULL") == 0) {
  58. wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
  59. data->name);
  60. tmp = NULL;
  61. res_len = 0;
  62. goto set;
  63. }
  64. tmp = wpa_config_parse_string(value, &res_len);
  65. if (tmp == NULL) {
  66. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
  67. line, data->name,
  68. data->key_data ? "[KEY DATA REMOVED]" : value);
  69. return -1;
  70. }
  71. if (data->key_data) {
  72. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  73. (u8 *) tmp, res_len);
  74. } else {
  75. wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
  76. (u8 *) tmp, res_len);
  77. }
  78. if (data->param3 && res_len < (size_t) data->param3) {
  79. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  80. "min_len=%ld)", line, data->name,
  81. (unsigned long) res_len, (long) data->param3);
  82. os_free(tmp);
  83. return -1;
  84. }
  85. if (data->param4 && res_len > (size_t) data->param4) {
  86. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  87. "max_len=%ld)", line, data->name,
  88. (unsigned long) res_len, (long) data->param4);
  89. os_free(tmp);
  90. return -1;
  91. }
  92. set:
  93. dst = (char **) (((u8 *) ssid) + (long) data->param1);
  94. dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
  95. if (data->param2)
  96. prev_len = *dst_len;
  97. else if (*dst)
  98. prev_len = os_strlen(*dst);
  99. else
  100. prev_len = 0;
  101. if ((*dst == NULL && tmp == NULL) ||
  102. (*dst && tmp && prev_len == res_len &&
  103. os_memcmp(*dst, tmp, res_len) == 0)) {
  104. /* No change to the previously configured value */
  105. os_free(tmp);
  106. return 1;
  107. }
  108. os_free(*dst);
  109. *dst = tmp;
  110. if (data->param2)
  111. *dst_len = res_len;
  112. return 0;
  113. }
  114. #ifndef NO_CONFIG_WRITE
  115. static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
  116. {
  117. char *buf;
  118. buf = os_malloc(len + 3);
  119. if (buf == NULL)
  120. return NULL;
  121. buf[0] = '"';
  122. os_memcpy(buf + 1, value, len);
  123. buf[len + 1] = '"';
  124. buf[len + 2] = '\0';
  125. return buf;
  126. }
  127. static char * wpa_config_write_string_hex(const u8 *value, size_t len)
  128. {
  129. char *buf;
  130. buf = os_zalloc(2 * len + 1);
  131. if (buf == NULL)
  132. return NULL;
  133. wpa_snprintf_hex(buf, 2 * len + 1, value, len);
  134. return buf;
  135. }
  136. static char * wpa_config_write_string(const u8 *value, size_t len)
  137. {
  138. if (value == NULL)
  139. return NULL;
  140. if (is_hex(value, len))
  141. return wpa_config_write_string_hex(value, len);
  142. else
  143. return wpa_config_write_string_ascii(value, len);
  144. }
  145. static char * wpa_config_write_str(const struct parse_data *data,
  146. struct wpa_ssid *ssid)
  147. {
  148. size_t len;
  149. char **src;
  150. src = (char **) (((u8 *) ssid) + (long) data->param1);
  151. if (*src == NULL)
  152. return NULL;
  153. if (data->param2)
  154. len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
  155. else
  156. len = os_strlen(*src);
  157. return wpa_config_write_string((const u8 *) *src, len);
  158. }
  159. #endif /* NO_CONFIG_WRITE */
  160. static int wpa_config_parse_int(const struct parse_data *data,
  161. struct wpa_ssid *ssid,
  162. int line, const char *value)
  163. {
  164. int val, *dst;
  165. char *end;
  166. dst = (int *) (((u8 *) ssid) + (long) data->param1);
  167. val = strtol(value, &end, 0);
  168. if (*end) {
  169. wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
  170. line, value);
  171. return -1;
  172. }
  173. if (*dst == val)
  174. return 1;
  175. *dst = val;
  176. wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
  177. if (data->param3 && *dst < (long) data->param3) {
  178. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  179. "min_value=%ld)", line, data->name, *dst,
  180. (long) data->param3);
  181. *dst = (long) data->param3;
  182. return -1;
  183. }
  184. if (data->param4 && *dst > (long) data->param4) {
  185. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  186. "max_value=%ld)", line, data->name, *dst,
  187. (long) data->param4);
  188. *dst = (long) data->param4;
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. #ifndef NO_CONFIG_WRITE
  194. static char * wpa_config_write_int(const struct parse_data *data,
  195. struct wpa_ssid *ssid)
  196. {
  197. int *src, res;
  198. char *value;
  199. src = (int *) (((u8 *) ssid) + (long) data->param1);
  200. value = os_malloc(20);
  201. if (value == NULL)
  202. return NULL;
  203. res = os_snprintf(value, 20, "%d", *src);
  204. if (os_snprintf_error(20, res)) {
  205. os_free(value);
  206. return NULL;
  207. }
  208. value[20 - 1] = '\0';
  209. return value;
  210. }
  211. #endif /* NO_CONFIG_WRITE */
  212. static int wpa_config_parse_addr_list(const struct parse_data *data,
  213. int line, const char *value,
  214. u8 **list, size_t *num, char *name,
  215. u8 abort_on_error, u8 masked)
  216. {
  217. const char *pos;
  218. u8 *buf, *n, addr[2 * ETH_ALEN];
  219. size_t count;
  220. buf = NULL;
  221. count = 0;
  222. pos = value;
  223. while (pos && *pos) {
  224. while (*pos == ' ')
  225. pos++;
  226. if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
  227. if (abort_on_error || count == 0) {
  228. wpa_printf(MSG_ERROR,
  229. "Line %d: Invalid %s address '%s'",
  230. line, name, value);
  231. os_free(buf);
  232. return -1;
  233. }
  234. /* continue anyway since this could have been from a
  235. * truncated configuration file line */
  236. wpa_printf(MSG_INFO,
  237. "Line %d: Ignore likely truncated %s address '%s'",
  238. line, name, pos);
  239. } else {
  240. n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
  241. if (n == NULL) {
  242. os_free(buf);
  243. return -1;
  244. }
  245. buf = n;
  246. os_memmove(buf + 2 * ETH_ALEN, buf,
  247. count * 2 * ETH_ALEN);
  248. os_memcpy(buf, addr, 2 * ETH_ALEN);
  249. count++;
  250. wpa_printf(MSG_MSGDUMP,
  251. "%s: addr=" MACSTR " mask=" MACSTR,
  252. name, MAC2STR(addr),
  253. MAC2STR(&addr[ETH_ALEN]));
  254. }
  255. pos = os_strchr(pos, ' ');
  256. }
  257. os_free(*list);
  258. *list = buf;
  259. *num = count;
  260. return 0;
  261. }
  262. #ifndef NO_CONFIG_WRITE
  263. static char * wpa_config_write_addr_list(const struct parse_data *data,
  264. const u8 *list, size_t num, char *name)
  265. {
  266. char *value, *end, *pos;
  267. int res;
  268. size_t i;
  269. if (list == NULL || num == 0)
  270. return NULL;
  271. value = os_malloc(2 * 20 * num);
  272. if (value == NULL)
  273. return NULL;
  274. pos = value;
  275. end = value + 2 * 20 * num;
  276. for (i = num; i > 0; i--) {
  277. const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
  278. const u8 *m = a + ETH_ALEN;
  279. if (i < num)
  280. *pos++ = ' ';
  281. res = hwaddr_mask_txt(pos, end - pos, a, m);
  282. if (res < 0) {
  283. os_free(value);
  284. return NULL;
  285. }
  286. pos += res;
  287. }
  288. return value;
  289. }
  290. #endif /* NO_CONFIG_WRITE */
  291. static int wpa_config_parse_bssid(const struct parse_data *data,
  292. struct wpa_ssid *ssid, int line,
  293. const char *value)
  294. {
  295. if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
  296. os_strcmp(value, "any") == 0) {
  297. ssid->bssid_set = 0;
  298. wpa_printf(MSG_MSGDUMP, "BSSID any");
  299. return 0;
  300. }
  301. if (hwaddr_aton(value, ssid->bssid)) {
  302. wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
  303. line, value);
  304. return -1;
  305. }
  306. ssid->bssid_set = 1;
  307. wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
  308. return 0;
  309. }
  310. #ifndef NO_CONFIG_WRITE
  311. static char * wpa_config_write_bssid(const struct parse_data *data,
  312. struct wpa_ssid *ssid)
  313. {
  314. char *value;
  315. int res;
  316. if (!ssid->bssid_set)
  317. return NULL;
  318. value = os_malloc(20);
  319. if (value == NULL)
  320. return NULL;
  321. res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
  322. if (os_snprintf_error(20, res)) {
  323. os_free(value);
  324. return NULL;
  325. }
  326. value[20 - 1] = '\0';
  327. return value;
  328. }
  329. #endif /* NO_CONFIG_WRITE */
  330. static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
  331. struct wpa_ssid *ssid, int line,
  332. const char *value)
  333. {
  334. return wpa_config_parse_addr_list(data, line, value,
  335. &ssid->bssid_blacklist,
  336. &ssid->num_bssid_blacklist,
  337. "bssid_blacklist", 1, 1);
  338. }
  339. #ifndef NO_CONFIG_WRITE
  340. static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
  341. struct wpa_ssid *ssid)
  342. {
  343. return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
  344. ssid->num_bssid_blacklist,
  345. "bssid_blacklist");
  346. }
  347. #endif /* NO_CONFIG_WRITE */
  348. static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
  349. struct wpa_ssid *ssid, int line,
  350. const char *value)
  351. {
  352. return wpa_config_parse_addr_list(data, line, value,
  353. &ssid->bssid_whitelist,
  354. &ssid->num_bssid_whitelist,
  355. "bssid_whitelist", 1, 1);
  356. }
  357. #ifndef NO_CONFIG_WRITE
  358. static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
  359. struct wpa_ssid *ssid)
  360. {
  361. return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
  362. ssid->num_bssid_whitelist,
  363. "bssid_whitelist");
  364. }
  365. #endif /* NO_CONFIG_WRITE */
  366. static int wpa_config_parse_psk(const struct parse_data *data,
  367. struct wpa_ssid *ssid, int line,
  368. const char *value)
  369. {
  370. #ifdef CONFIG_EXT_PASSWORD
  371. if (os_strncmp(value, "ext:", 4) == 0) {
  372. str_clear_free(ssid->passphrase);
  373. ssid->passphrase = NULL;
  374. ssid->psk_set = 0;
  375. os_free(ssid->ext_psk);
  376. ssid->ext_psk = os_strdup(value + 4);
  377. if (ssid->ext_psk == NULL)
  378. return -1;
  379. wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
  380. ssid->ext_psk);
  381. return 0;
  382. }
  383. #endif /* CONFIG_EXT_PASSWORD */
  384. if (*value == '"') {
  385. #ifndef CONFIG_NO_PBKDF2
  386. const char *pos;
  387. size_t len;
  388. value++;
  389. pos = os_strrchr(value, '"');
  390. if (pos)
  391. len = pos - value;
  392. else
  393. len = os_strlen(value);
  394. if (len < 8 || len > 63) {
  395. wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
  396. "length %lu (expected: 8..63) '%s'.",
  397. line, (unsigned long) len, value);
  398. return -1;
  399. }
  400. wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
  401. (u8 *) value, len);
  402. if (has_ctrl_char((u8 *) value, len)) {
  403. wpa_printf(MSG_ERROR,
  404. "Line %d: Invalid passphrase character",
  405. line);
  406. return -1;
  407. }
  408. if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
  409. os_memcmp(ssid->passphrase, value, len) == 0) {
  410. /* No change to the previously configured value */
  411. return 1;
  412. }
  413. ssid->psk_set = 0;
  414. str_clear_free(ssid->passphrase);
  415. ssid->passphrase = dup_binstr(value, len);
  416. if (ssid->passphrase == NULL)
  417. return -1;
  418. return 0;
  419. #else /* CONFIG_NO_PBKDF2 */
  420. wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
  421. "supported.", line);
  422. return -1;
  423. #endif /* CONFIG_NO_PBKDF2 */
  424. }
  425. if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
  426. value[PMK_LEN * 2] != '\0') {
  427. wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
  428. line, value);
  429. return -1;
  430. }
  431. str_clear_free(ssid->passphrase);
  432. ssid->passphrase = NULL;
  433. ssid->psk_set = 1;
  434. wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
  435. return 0;
  436. }
  437. #ifndef NO_CONFIG_WRITE
  438. static char * wpa_config_write_psk(const struct parse_data *data,
  439. struct wpa_ssid *ssid)
  440. {
  441. #ifdef CONFIG_EXT_PASSWORD
  442. if (ssid->ext_psk) {
  443. size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
  444. char *buf = os_malloc(len);
  445. int res;
  446. if (buf == NULL)
  447. return NULL;
  448. res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
  449. if (os_snprintf_error(len, res)) {
  450. os_free(buf);
  451. buf = NULL;
  452. }
  453. return buf;
  454. }
  455. #endif /* CONFIG_EXT_PASSWORD */
  456. if (ssid->passphrase)
  457. return wpa_config_write_string_ascii(
  458. (const u8 *) ssid->passphrase,
  459. os_strlen(ssid->passphrase));
  460. if (ssid->psk_set)
  461. return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
  462. return NULL;
  463. }
  464. #endif /* NO_CONFIG_WRITE */
  465. static int wpa_config_parse_proto(const struct parse_data *data,
  466. struct wpa_ssid *ssid, int line,
  467. const char *value)
  468. {
  469. int val = 0, last, errors = 0;
  470. char *start, *end, *buf;
  471. buf = os_strdup(value);
  472. if (buf == NULL)
  473. return -1;
  474. start = buf;
  475. while (*start != '\0') {
  476. while (*start == ' ' || *start == '\t')
  477. start++;
  478. if (*start == '\0')
  479. break;
  480. end = start;
  481. while (*end != ' ' && *end != '\t' && *end != '\0')
  482. end++;
  483. last = *end == '\0';
  484. *end = '\0';
  485. if (os_strcmp(start, "WPA") == 0)
  486. val |= WPA_PROTO_WPA;
  487. else if (os_strcmp(start, "RSN") == 0 ||
  488. os_strcmp(start, "WPA2") == 0)
  489. val |= WPA_PROTO_RSN;
  490. else if (os_strcmp(start, "OSEN") == 0)
  491. val |= WPA_PROTO_OSEN;
  492. else {
  493. wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
  494. line, start);
  495. errors++;
  496. }
  497. if (last)
  498. break;
  499. start = end + 1;
  500. }
  501. os_free(buf);
  502. if (val == 0) {
  503. wpa_printf(MSG_ERROR,
  504. "Line %d: no proto values configured.", line);
  505. errors++;
  506. }
  507. if (!errors && ssid->proto == val)
  508. return 1;
  509. wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
  510. ssid->proto = val;
  511. return errors ? -1 : 0;
  512. }
  513. #ifndef NO_CONFIG_WRITE
  514. static char * wpa_config_write_proto(const struct parse_data *data,
  515. struct wpa_ssid *ssid)
  516. {
  517. int ret;
  518. char *buf, *pos, *end;
  519. pos = buf = os_zalloc(20);
  520. if (buf == NULL)
  521. return NULL;
  522. end = buf + 20;
  523. if (ssid->proto & WPA_PROTO_WPA) {
  524. ret = os_snprintf(pos, end - pos, "%sWPA",
  525. pos == buf ? "" : " ");
  526. if (os_snprintf_error(end - pos, ret))
  527. return buf;
  528. pos += ret;
  529. }
  530. if (ssid->proto & WPA_PROTO_RSN) {
  531. ret = os_snprintf(pos, end - pos, "%sRSN",
  532. pos == buf ? "" : " ");
  533. if (os_snprintf_error(end - pos, ret))
  534. return buf;
  535. pos += ret;
  536. }
  537. if (ssid->proto & WPA_PROTO_OSEN) {
  538. ret = os_snprintf(pos, end - pos, "%sOSEN",
  539. pos == buf ? "" : " ");
  540. if (os_snprintf_error(end - pos, ret))
  541. return buf;
  542. pos += ret;
  543. }
  544. if (pos == buf) {
  545. os_free(buf);
  546. buf = NULL;
  547. }
  548. return buf;
  549. }
  550. #endif /* NO_CONFIG_WRITE */
  551. static int wpa_config_parse_key_mgmt(const struct parse_data *data,
  552. struct wpa_ssid *ssid, int line,
  553. const char *value)
  554. {
  555. int val = 0, last, errors = 0;
  556. char *start, *end, *buf;
  557. buf = os_strdup(value);
  558. if (buf == NULL)
  559. return -1;
  560. start = buf;
  561. while (*start != '\0') {
  562. while (*start == ' ' || *start == '\t')
  563. start++;
  564. if (*start == '\0')
  565. break;
  566. end = start;
  567. while (*end != ' ' && *end != '\t' && *end != '\0')
  568. end++;
  569. last = *end == '\0';
  570. *end = '\0';
  571. if (os_strcmp(start, "WPA-PSK") == 0)
  572. val |= WPA_KEY_MGMT_PSK;
  573. else if (os_strcmp(start, "WPA-EAP") == 0)
  574. val |= WPA_KEY_MGMT_IEEE8021X;
  575. else if (os_strcmp(start, "IEEE8021X") == 0)
  576. val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  577. else if (os_strcmp(start, "NONE") == 0)
  578. val |= WPA_KEY_MGMT_NONE;
  579. else if (os_strcmp(start, "WPA-NONE") == 0)
  580. val |= WPA_KEY_MGMT_WPA_NONE;
  581. #ifdef CONFIG_IEEE80211R
  582. else if (os_strcmp(start, "FT-PSK") == 0)
  583. val |= WPA_KEY_MGMT_FT_PSK;
  584. else if (os_strcmp(start, "FT-EAP") == 0)
  585. val |= WPA_KEY_MGMT_FT_IEEE8021X;
  586. #endif /* CONFIG_IEEE80211R */
  587. #ifdef CONFIG_IEEE80211W
  588. else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
  589. val |= WPA_KEY_MGMT_PSK_SHA256;
  590. else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
  591. val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
  592. #endif /* CONFIG_IEEE80211W */
  593. #ifdef CONFIG_WPS
  594. else if (os_strcmp(start, "WPS") == 0)
  595. val |= WPA_KEY_MGMT_WPS;
  596. #endif /* CONFIG_WPS */
  597. #ifdef CONFIG_SAE
  598. else if (os_strcmp(start, "SAE") == 0)
  599. val |= WPA_KEY_MGMT_SAE;
  600. else if (os_strcmp(start, "FT-SAE") == 0)
  601. val |= WPA_KEY_MGMT_FT_SAE;
  602. #endif /* CONFIG_SAE */
  603. #ifdef CONFIG_HS20
  604. else if (os_strcmp(start, "OSEN") == 0)
  605. val |= WPA_KEY_MGMT_OSEN;
  606. #endif /* CONFIG_HS20 */
  607. #ifdef CONFIG_SUITEB
  608. else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
  609. val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
  610. #endif /* CONFIG_SUITEB */
  611. #ifdef CONFIG_SUITEB192
  612. else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
  613. val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
  614. #endif /* CONFIG_SUITEB192 */
  615. else {
  616. wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
  617. line, start);
  618. errors++;
  619. }
  620. if (last)
  621. break;
  622. start = end + 1;
  623. }
  624. os_free(buf);
  625. if (val == 0) {
  626. wpa_printf(MSG_ERROR,
  627. "Line %d: no key_mgmt values configured.", line);
  628. errors++;
  629. }
  630. if (!errors && ssid->key_mgmt == val)
  631. return 1;
  632. wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
  633. ssid->key_mgmt = val;
  634. return errors ? -1 : 0;
  635. }
  636. #ifndef NO_CONFIG_WRITE
  637. static char * wpa_config_write_key_mgmt(const struct parse_data *data,
  638. struct wpa_ssid *ssid)
  639. {
  640. char *buf, *pos, *end;
  641. int ret;
  642. pos = buf = os_zalloc(100);
  643. if (buf == NULL)
  644. return NULL;
  645. end = buf + 100;
  646. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  647. ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
  648. pos == buf ? "" : " ");
  649. if (os_snprintf_error(end - pos, ret)) {
  650. end[-1] = '\0';
  651. return buf;
  652. }
  653. pos += ret;
  654. }
  655. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  656. ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
  657. pos == buf ? "" : " ");
  658. if (os_snprintf_error(end - pos, ret)) {
  659. end[-1] = '\0';
  660. return buf;
  661. }
  662. pos += ret;
  663. }
  664. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  665. ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
  666. pos == buf ? "" : " ");
  667. if (os_snprintf_error(end - pos, ret)) {
  668. end[-1] = '\0';
  669. return buf;
  670. }
  671. pos += ret;
  672. }
  673. if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
  674. ret = os_snprintf(pos, end - pos, "%sNONE",
  675. pos == buf ? "" : " ");
  676. if (os_snprintf_error(end - pos, ret)) {
  677. end[-1] = '\0';
  678. return buf;
  679. }
  680. pos += ret;
  681. }
  682. if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  683. ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
  684. pos == buf ? "" : " ");
  685. if (os_snprintf_error(end - pos, ret)) {
  686. end[-1] = '\0';
  687. return buf;
  688. }
  689. pos += ret;
  690. }
  691. #ifdef CONFIG_IEEE80211R
  692. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  693. ret = os_snprintf(pos, end - pos, "%sFT-PSK",
  694. pos == buf ? "" : " ");
  695. if (os_snprintf_error(end - pos, ret)) {
  696. end[-1] = '\0';
  697. return buf;
  698. }
  699. pos += ret;
  700. }
  701. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  702. ret = os_snprintf(pos, end - pos, "%sFT-EAP",
  703. pos == buf ? "" : " ");
  704. if (os_snprintf_error(end - pos, ret)) {
  705. end[-1] = '\0';
  706. return buf;
  707. }
  708. pos += ret;
  709. }
  710. #endif /* CONFIG_IEEE80211R */
  711. #ifdef CONFIG_IEEE80211W
  712. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  713. ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
  714. pos == buf ? "" : " ");
  715. if (os_snprintf_error(end - pos, ret)) {
  716. end[-1] = '\0';
  717. return buf;
  718. }
  719. pos += ret;
  720. }
  721. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  722. ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
  723. pos == buf ? "" : " ");
  724. if (os_snprintf_error(end - pos, ret)) {
  725. end[-1] = '\0';
  726. return buf;
  727. }
  728. pos += ret;
  729. }
  730. #endif /* CONFIG_IEEE80211W */
  731. #ifdef CONFIG_WPS
  732. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  733. ret = os_snprintf(pos, end - pos, "%sWPS",
  734. pos == buf ? "" : " ");
  735. if (os_snprintf_error(end - pos, ret)) {
  736. end[-1] = '\0';
  737. return buf;
  738. }
  739. pos += ret;
  740. }
  741. #endif /* CONFIG_WPS */
  742. #ifdef CONFIG_SAE
  743. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
  744. ret = os_snprintf(pos, end - pos, "%sSAE",
  745. pos == buf ? "" : " ");
  746. if (os_snprintf_error(end - pos, ret)) {
  747. end[-1] = '\0';
  748. return buf;
  749. }
  750. pos += ret;
  751. }
  752. if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  753. ret = os_snprintf(pos, end - pos, "%sFT-SAE",
  754. pos == buf ? "" : " ");
  755. if (os_snprintf_error(end - pos, ret)) {
  756. end[-1] = '\0';
  757. return buf;
  758. }
  759. pos += ret;
  760. }
  761. #endif /* CONFIG_SAE */
  762. #ifdef CONFIG_HS20
  763. if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
  764. ret = os_snprintf(pos, end - pos, "%sOSEN",
  765. pos == buf ? "" : " ");
  766. if (os_snprintf_error(end - pos, ret)) {
  767. end[-1] = '\0';
  768. return buf;
  769. }
  770. pos += ret;
  771. }
  772. #endif /* CONFIG_HS20 */
  773. #ifdef CONFIG_SUITEB
  774. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  775. ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
  776. pos == buf ? "" : " ");
  777. if (os_snprintf_error(end - pos, ret)) {
  778. end[-1] = '\0';
  779. return buf;
  780. }
  781. pos += ret;
  782. }
  783. #endif /* CONFIG_SUITEB */
  784. #ifdef CONFIG_SUITEB192
  785. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  786. ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
  787. pos == buf ? "" : " ");
  788. if (os_snprintf_error(end - pos, ret)) {
  789. end[-1] = '\0';
  790. return buf;
  791. }
  792. pos += ret;
  793. }
  794. #endif /* CONFIG_SUITEB192 */
  795. if (pos == buf) {
  796. os_free(buf);
  797. buf = NULL;
  798. }
  799. return buf;
  800. }
  801. #endif /* NO_CONFIG_WRITE */
  802. static int wpa_config_parse_cipher(int line, const char *value)
  803. {
  804. #ifdef CONFIG_NO_WPA
  805. return -1;
  806. #else /* CONFIG_NO_WPA */
  807. int val = wpa_parse_cipher(value);
  808. if (val < 0) {
  809. wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
  810. line, value);
  811. return -1;
  812. }
  813. if (val == 0) {
  814. wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
  815. line);
  816. return -1;
  817. }
  818. return val;
  819. #endif /* CONFIG_NO_WPA */
  820. }
  821. #ifndef NO_CONFIG_WRITE
  822. static char * wpa_config_write_cipher(int cipher)
  823. {
  824. #ifdef CONFIG_NO_WPA
  825. return NULL;
  826. #else /* CONFIG_NO_WPA */
  827. char *buf = os_zalloc(50);
  828. if (buf == NULL)
  829. return NULL;
  830. if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
  831. os_free(buf);
  832. return NULL;
  833. }
  834. return buf;
  835. #endif /* CONFIG_NO_WPA */
  836. }
  837. #endif /* NO_CONFIG_WRITE */
  838. static int wpa_config_parse_pairwise(const struct parse_data *data,
  839. struct wpa_ssid *ssid, int line,
  840. const char *value)
  841. {
  842. int val;
  843. val = wpa_config_parse_cipher(line, value);
  844. if (val == -1)
  845. return -1;
  846. if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
  847. wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
  848. "(0x%x).", line, val);
  849. return -1;
  850. }
  851. if (ssid->pairwise_cipher == val)
  852. return 1;
  853. wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
  854. ssid->pairwise_cipher = val;
  855. return 0;
  856. }
  857. #ifndef NO_CONFIG_WRITE
  858. static char * wpa_config_write_pairwise(const struct parse_data *data,
  859. struct wpa_ssid *ssid)
  860. {
  861. return wpa_config_write_cipher(ssid->pairwise_cipher);
  862. }
  863. #endif /* NO_CONFIG_WRITE */
  864. static int wpa_config_parse_group(const struct parse_data *data,
  865. struct wpa_ssid *ssid, int line,
  866. const char *value)
  867. {
  868. int val;
  869. val = wpa_config_parse_cipher(line, value);
  870. if (val == -1)
  871. return -1;
  872. /*
  873. * Backwards compatibility - filter out WEP ciphers that were previously
  874. * allowed.
  875. */
  876. val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
  877. if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
  878. wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
  879. "(0x%x).", line, val);
  880. return -1;
  881. }
  882. if (ssid->group_cipher == val)
  883. return 1;
  884. wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
  885. ssid->group_cipher = val;
  886. return 0;
  887. }
  888. #ifndef NO_CONFIG_WRITE
  889. static char * wpa_config_write_group(const struct parse_data *data,
  890. struct wpa_ssid *ssid)
  891. {
  892. return wpa_config_write_cipher(ssid->group_cipher);
  893. }
  894. #endif /* NO_CONFIG_WRITE */
  895. static int wpa_config_parse_auth_alg(const struct parse_data *data,
  896. struct wpa_ssid *ssid, int line,
  897. const char *value)
  898. {
  899. int val = 0, last, errors = 0;
  900. char *start, *end, *buf;
  901. buf = os_strdup(value);
  902. if (buf == NULL)
  903. return -1;
  904. start = buf;
  905. while (*start != '\0') {
  906. while (*start == ' ' || *start == '\t')
  907. start++;
  908. if (*start == '\0')
  909. break;
  910. end = start;
  911. while (*end != ' ' && *end != '\t' && *end != '\0')
  912. end++;
  913. last = *end == '\0';
  914. *end = '\0';
  915. if (os_strcmp(start, "OPEN") == 0)
  916. val |= WPA_AUTH_ALG_OPEN;
  917. else if (os_strcmp(start, "SHARED") == 0)
  918. val |= WPA_AUTH_ALG_SHARED;
  919. else if (os_strcmp(start, "LEAP") == 0)
  920. val |= WPA_AUTH_ALG_LEAP;
  921. else {
  922. wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
  923. line, start);
  924. errors++;
  925. }
  926. if (last)
  927. break;
  928. start = end + 1;
  929. }
  930. os_free(buf);
  931. if (val == 0) {
  932. wpa_printf(MSG_ERROR,
  933. "Line %d: no auth_alg values configured.", line);
  934. errors++;
  935. }
  936. if (!errors && ssid->auth_alg == val)
  937. return 1;
  938. wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
  939. ssid->auth_alg = val;
  940. return errors ? -1 : 0;
  941. }
  942. #ifndef NO_CONFIG_WRITE
  943. static char * wpa_config_write_auth_alg(const struct parse_data *data,
  944. struct wpa_ssid *ssid)
  945. {
  946. char *buf, *pos, *end;
  947. int ret;
  948. pos = buf = os_zalloc(30);
  949. if (buf == NULL)
  950. return NULL;
  951. end = buf + 30;
  952. if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
  953. ret = os_snprintf(pos, end - pos, "%sOPEN",
  954. pos == buf ? "" : " ");
  955. if (os_snprintf_error(end - pos, ret)) {
  956. end[-1] = '\0';
  957. return buf;
  958. }
  959. pos += ret;
  960. }
  961. if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
  962. ret = os_snprintf(pos, end - pos, "%sSHARED",
  963. pos == buf ? "" : " ");
  964. if (os_snprintf_error(end - pos, ret)) {
  965. end[-1] = '\0';
  966. return buf;
  967. }
  968. pos += ret;
  969. }
  970. if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
  971. ret = os_snprintf(pos, end - pos, "%sLEAP",
  972. pos == buf ? "" : " ");
  973. if (os_snprintf_error(end - pos, ret)) {
  974. end[-1] = '\0';
  975. return buf;
  976. }
  977. pos += ret;
  978. }
  979. if (pos == buf) {
  980. os_free(buf);
  981. buf = NULL;
  982. }
  983. return buf;
  984. }
  985. #endif /* NO_CONFIG_WRITE */
  986. static int * wpa_config_parse_int_array(const char *value)
  987. {
  988. int *freqs;
  989. size_t used, len;
  990. const char *pos;
  991. used = 0;
  992. len = 10;
  993. freqs = os_calloc(len + 1, sizeof(int));
  994. if (freqs == NULL)
  995. return NULL;
  996. pos = value;
  997. while (pos) {
  998. while (*pos == ' ')
  999. pos++;
  1000. if (used == len) {
  1001. int *n;
  1002. size_t i;
  1003. n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
  1004. if (n == NULL) {
  1005. os_free(freqs);
  1006. return NULL;
  1007. }
  1008. for (i = len; i <= len * 2; i++)
  1009. n[i] = 0;
  1010. freqs = n;
  1011. len *= 2;
  1012. }
  1013. freqs[used] = atoi(pos);
  1014. if (freqs[used] == 0)
  1015. break;
  1016. used++;
  1017. pos = os_strchr(pos + 1, ' ');
  1018. }
  1019. return freqs;
  1020. }
  1021. static int wpa_config_parse_scan_freq(const struct parse_data *data,
  1022. struct wpa_ssid *ssid, int line,
  1023. const char *value)
  1024. {
  1025. int *freqs;
  1026. freqs = wpa_config_parse_int_array(value);
  1027. if (freqs == NULL)
  1028. return -1;
  1029. if (freqs[0] == 0) {
  1030. os_free(freqs);
  1031. freqs = NULL;
  1032. }
  1033. os_free(ssid->scan_freq);
  1034. ssid->scan_freq = freqs;
  1035. return 0;
  1036. }
  1037. static int wpa_config_parse_freq_list(const struct parse_data *data,
  1038. struct wpa_ssid *ssid, int line,
  1039. const char *value)
  1040. {
  1041. int *freqs;
  1042. freqs = wpa_config_parse_int_array(value);
  1043. if (freqs == NULL)
  1044. return -1;
  1045. if (freqs[0] == 0) {
  1046. os_free(freqs);
  1047. freqs = NULL;
  1048. }
  1049. os_free(ssid->freq_list);
  1050. ssid->freq_list = freqs;
  1051. return 0;
  1052. }
  1053. #ifndef NO_CONFIG_WRITE
  1054. static char * wpa_config_write_freqs(const struct parse_data *data,
  1055. const int *freqs)
  1056. {
  1057. char *buf, *pos, *end;
  1058. int i, ret;
  1059. size_t count;
  1060. if (freqs == NULL)
  1061. return NULL;
  1062. count = 0;
  1063. for (i = 0; freqs[i]; i++)
  1064. count++;
  1065. pos = buf = os_zalloc(10 * count + 1);
  1066. if (buf == NULL)
  1067. return NULL;
  1068. end = buf + 10 * count + 1;
  1069. for (i = 0; freqs[i]; i++) {
  1070. ret = os_snprintf(pos, end - pos, "%s%u",
  1071. i == 0 ? "" : " ", freqs[i]);
  1072. if (os_snprintf_error(end - pos, ret)) {
  1073. end[-1] = '\0';
  1074. return buf;
  1075. }
  1076. pos += ret;
  1077. }
  1078. return buf;
  1079. }
  1080. static char * wpa_config_write_scan_freq(const struct parse_data *data,
  1081. struct wpa_ssid *ssid)
  1082. {
  1083. return wpa_config_write_freqs(data, ssid->scan_freq);
  1084. }
  1085. static char * wpa_config_write_freq_list(const struct parse_data *data,
  1086. struct wpa_ssid *ssid)
  1087. {
  1088. return wpa_config_write_freqs(data, ssid->freq_list);
  1089. }
  1090. #endif /* NO_CONFIG_WRITE */
  1091. #ifdef IEEE8021X_EAPOL
  1092. static int wpa_config_parse_eap(const struct parse_data *data,
  1093. struct wpa_ssid *ssid, int line,
  1094. const char *value)
  1095. {
  1096. int last, errors = 0;
  1097. char *start, *end, *buf;
  1098. struct eap_method_type *methods = NULL, *tmp;
  1099. size_t num_methods = 0;
  1100. buf = os_strdup(value);
  1101. if (buf == NULL)
  1102. return -1;
  1103. start = buf;
  1104. while (*start != '\0') {
  1105. while (*start == ' ' || *start == '\t')
  1106. start++;
  1107. if (*start == '\0')
  1108. break;
  1109. end = start;
  1110. while (*end != ' ' && *end != '\t' && *end != '\0')
  1111. end++;
  1112. last = *end == '\0';
  1113. *end = '\0';
  1114. tmp = methods;
  1115. methods = os_realloc_array(methods, num_methods + 1,
  1116. sizeof(*methods));
  1117. if (methods == NULL) {
  1118. os_free(tmp);
  1119. os_free(buf);
  1120. return -1;
  1121. }
  1122. methods[num_methods].method = eap_peer_get_type(
  1123. start, &methods[num_methods].vendor);
  1124. if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  1125. methods[num_methods].method == EAP_TYPE_NONE) {
  1126. wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
  1127. "'%s'", line, start);
  1128. wpa_printf(MSG_ERROR, "You may need to add support for"
  1129. " this EAP method during wpa_supplicant\n"
  1130. "build time configuration.\n"
  1131. "See README for more information.");
  1132. errors++;
  1133. } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
  1134. methods[num_methods].method == EAP_TYPE_LEAP)
  1135. ssid->leap++;
  1136. else
  1137. ssid->non_leap++;
  1138. num_methods++;
  1139. if (last)
  1140. break;
  1141. start = end + 1;
  1142. }
  1143. os_free(buf);
  1144. tmp = methods;
  1145. methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
  1146. if (methods == NULL) {
  1147. os_free(tmp);
  1148. return -1;
  1149. }
  1150. methods[num_methods].vendor = EAP_VENDOR_IETF;
  1151. methods[num_methods].method = EAP_TYPE_NONE;
  1152. num_methods++;
  1153. if (!errors && ssid->eap.eap_methods) {
  1154. struct eap_method_type *prev_m;
  1155. size_t i, j, prev_methods, match = 0;
  1156. prev_m = ssid->eap.eap_methods;
  1157. for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
  1158. prev_m[i].method != EAP_TYPE_NONE; i++) {
  1159. /* Count the methods */
  1160. }
  1161. prev_methods = i + 1;
  1162. for (i = 0; prev_methods == num_methods && i < prev_methods;
  1163. i++) {
  1164. for (j = 0; j < num_methods; j++) {
  1165. if (prev_m[i].vendor == methods[j].vendor &&
  1166. prev_m[i].method == methods[j].method) {
  1167. match++;
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. if (match == num_methods) {
  1173. os_free(methods);
  1174. return 1;
  1175. }
  1176. }
  1177. wpa_hexdump(MSG_MSGDUMP, "eap methods",
  1178. (u8 *) methods, num_methods * sizeof(*methods));
  1179. os_free(ssid->eap.eap_methods);
  1180. ssid->eap.eap_methods = methods;
  1181. return errors ? -1 : 0;
  1182. }
  1183. #ifndef NO_CONFIG_WRITE
  1184. static char * wpa_config_write_eap(const struct parse_data *data,
  1185. struct wpa_ssid *ssid)
  1186. {
  1187. int i, ret;
  1188. char *buf, *pos, *end;
  1189. const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
  1190. const char *name;
  1191. if (eap_methods == NULL)
  1192. return NULL;
  1193. pos = buf = os_zalloc(100);
  1194. if (buf == NULL)
  1195. return NULL;
  1196. end = buf + 100;
  1197. for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
  1198. eap_methods[i].method != EAP_TYPE_NONE; i++) {
  1199. name = eap_get_name(eap_methods[i].vendor,
  1200. eap_methods[i].method);
  1201. if (name) {
  1202. ret = os_snprintf(pos, end - pos, "%s%s",
  1203. pos == buf ? "" : " ", name);
  1204. if (os_snprintf_error(end - pos, ret))
  1205. break;
  1206. pos += ret;
  1207. }
  1208. }
  1209. end[-1] = '\0';
  1210. return buf;
  1211. }
  1212. #endif /* NO_CONFIG_WRITE */
  1213. static int wpa_config_parse_password(const struct parse_data *data,
  1214. struct wpa_ssid *ssid, int line,
  1215. const char *value)
  1216. {
  1217. u8 *hash;
  1218. if (os_strcmp(value, "NULL") == 0) {
  1219. if (!ssid->eap.password)
  1220. return 1; /* Already unset */
  1221. wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
  1222. bin_clear_free(ssid->eap.password, ssid->eap.password_len);
  1223. ssid->eap.password = NULL;
  1224. ssid->eap.password_len = 0;
  1225. return 0;
  1226. }
  1227. #ifdef CONFIG_EXT_PASSWORD
  1228. if (os_strncmp(value, "ext:", 4) == 0) {
  1229. char *name = os_strdup(value + 4);
  1230. if (name == NULL)
  1231. return -1;
  1232. bin_clear_free(ssid->eap.password, ssid->eap.password_len);
  1233. ssid->eap.password = (u8 *) name;
  1234. ssid->eap.password_len = os_strlen(name);
  1235. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  1236. ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
  1237. return 0;
  1238. }
  1239. #endif /* CONFIG_EXT_PASSWORD */
  1240. if (os_strncmp(value, "hash:", 5) != 0) {
  1241. char *tmp;
  1242. size_t res_len;
  1243. tmp = wpa_config_parse_string(value, &res_len);
  1244. if (tmp == NULL) {
  1245. wpa_printf(MSG_ERROR, "Line %d: failed to parse "
  1246. "password.", line);
  1247. return -1;
  1248. }
  1249. wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
  1250. (u8 *) tmp, res_len);
  1251. bin_clear_free(ssid->eap.password, ssid->eap.password_len);
  1252. ssid->eap.password = (u8 *) tmp;
  1253. ssid->eap.password_len = res_len;
  1254. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  1255. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
  1256. return 0;
  1257. }
  1258. /* NtPasswordHash: hash:<32 hex digits> */
  1259. if (os_strlen(value + 5) != 2 * 16) {
  1260. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
  1261. "(expected 32 hex digits)", line);
  1262. return -1;
  1263. }
  1264. hash = os_malloc(16);
  1265. if (hash == NULL)
  1266. return -1;
  1267. if (hexstr2bin(value + 5, hash, 16)) {
  1268. os_free(hash);
  1269. wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
  1270. return -1;
  1271. }
  1272. wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
  1273. if (ssid->eap.password && ssid->eap.password_len == 16 &&
  1274. os_memcmp(ssid->eap.password, hash, 16) == 0 &&
  1275. (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
  1276. bin_clear_free(hash, 16);
  1277. return 1;
  1278. }
  1279. bin_clear_free(ssid->eap.password, ssid->eap.password_len);
  1280. ssid->eap.password = hash;
  1281. ssid->eap.password_len = 16;
  1282. ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
  1283. ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
  1284. return 0;
  1285. }
  1286. #ifndef NO_CONFIG_WRITE
  1287. static char * wpa_config_write_password(const struct parse_data *data,
  1288. struct wpa_ssid *ssid)
  1289. {
  1290. char *buf;
  1291. if (ssid->eap.password == NULL)
  1292. return NULL;
  1293. #ifdef CONFIG_EXT_PASSWORD
  1294. if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
  1295. buf = os_zalloc(4 + ssid->eap.password_len + 1);
  1296. if (buf == NULL)
  1297. return NULL;
  1298. os_memcpy(buf, "ext:", 4);
  1299. os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
  1300. return buf;
  1301. }
  1302. #endif /* CONFIG_EXT_PASSWORD */
  1303. if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
  1304. return wpa_config_write_string(
  1305. ssid->eap.password, ssid->eap.password_len);
  1306. }
  1307. buf = os_malloc(5 + 32 + 1);
  1308. if (buf == NULL)
  1309. return NULL;
  1310. os_memcpy(buf, "hash:", 5);
  1311. wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
  1312. return buf;
  1313. }
  1314. #endif /* NO_CONFIG_WRITE */
  1315. #endif /* IEEE8021X_EAPOL */
  1316. static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
  1317. const char *value, int idx)
  1318. {
  1319. char *buf, title[20];
  1320. int res;
  1321. buf = wpa_config_parse_string(value, len);
  1322. if (buf == NULL) {
  1323. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
  1324. line, idx, value);
  1325. return -1;
  1326. }
  1327. if (*len > MAX_WEP_KEY_LEN) {
  1328. wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
  1329. line, idx, value);
  1330. os_free(buf);
  1331. return -1;
  1332. }
  1333. if (*len && *len != 5 && *len != 13 && *len != 16) {
  1334. wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
  1335. "this network block will be ignored",
  1336. line, (unsigned int) *len);
  1337. }
  1338. os_memcpy(key, buf, *len);
  1339. str_clear_free(buf);
  1340. res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
  1341. if (!os_snprintf_error(sizeof(title), res))
  1342. wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
  1343. return 0;
  1344. }
  1345. static int wpa_config_parse_wep_key0(const struct parse_data *data,
  1346. struct wpa_ssid *ssid, int line,
  1347. const char *value)
  1348. {
  1349. return wpa_config_parse_wep_key(ssid->wep_key[0],
  1350. &ssid->wep_key_len[0], line,
  1351. value, 0);
  1352. }
  1353. static int wpa_config_parse_wep_key1(const struct parse_data *data,
  1354. struct wpa_ssid *ssid, int line,
  1355. const char *value)
  1356. {
  1357. return wpa_config_parse_wep_key(ssid->wep_key[1],
  1358. &ssid->wep_key_len[1], line,
  1359. value, 1);
  1360. }
  1361. static int wpa_config_parse_wep_key2(const struct parse_data *data,
  1362. struct wpa_ssid *ssid, int line,
  1363. const char *value)
  1364. {
  1365. return wpa_config_parse_wep_key(ssid->wep_key[2],
  1366. &ssid->wep_key_len[2], line,
  1367. value, 2);
  1368. }
  1369. static int wpa_config_parse_wep_key3(const struct parse_data *data,
  1370. struct wpa_ssid *ssid, int line,
  1371. const char *value)
  1372. {
  1373. return wpa_config_parse_wep_key(ssid->wep_key[3],
  1374. &ssid->wep_key_len[3], line,
  1375. value, 3);
  1376. }
  1377. #ifndef NO_CONFIG_WRITE
  1378. static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
  1379. {
  1380. if (ssid->wep_key_len[idx] == 0)
  1381. return NULL;
  1382. return wpa_config_write_string(ssid->wep_key[idx],
  1383. ssid->wep_key_len[idx]);
  1384. }
  1385. static char * wpa_config_write_wep_key0(const struct parse_data *data,
  1386. struct wpa_ssid *ssid)
  1387. {
  1388. return wpa_config_write_wep_key(ssid, 0);
  1389. }
  1390. static char * wpa_config_write_wep_key1(const struct parse_data *data,
  1391. struct wpa_ssid *ssid)
  1392. {
  1393. return wpa_config_write_wep_key(ssid, 1);
  1394. }
  1395. static char * wpa_config_write_wep_key2(const struct parse_data *data,
  1396. struct wpa_ssid *ssid)
  1397. {
  1398. return wpa_config_write_wep_key(ssid, 2);
  1399. }
  1400. static char * wpa_config_write_wep_key3(const struct parse_data *data,
  1401. struct wpa_ssid *ssid)
  1402. {
  1403. return wpa_config_write_wep_key(ssid, 3);
  1404. }
  1405. #endif /* NO_CONFIG_WRITE */
  1406. #ifdef CONFIG_P2P
  1407. static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
  1408. struct wpa_ssid *ssid, int line,
  1409. const char *value)
  1410. {
  1411. if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
  1412. os_strcmp(value, "any") == 0) {
  1413. os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
  1414. wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
  1415. return 0;
  1416. }
  1417. if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
  1418. wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
  1419. line, value);
  1420. return -1;
  1421. }
  1422. ssid->bssid_set = 1;
  1423. wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
  1424. MAC2STR(ssid->go_p2p_dev_addr));
  1425. return 0;
  1426. }
  1427. #ifndef NO_CONFIG_WRITE
  1428. static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
  1429. struct wpa_ssid *ssid)
  1430. {
  1431. char *value;
  1432. int res;
  1433. if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
  1434. return NULL;
  1435. value = os_malloc(20);
  1436. if (value == NULL)
  1437. return NULL;
  1438. res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
  1439. if (os_snprintf_error(20, res)) {
  1440. os_free(value);
  1441. return NULL;
  1442. }
  1443. value[20 - 1] = '\0';
  1444. return value;
  1445. }
  1446. #endif /* NO_CONFIG_WRITE */
  1447. static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
  1448. struct wpa_ssid *ssid, int line,
  1449. const char *value)
  1450. {
  1451. return wpa_config_parse_addr_list(data, line, value,
  1452. &ssid->p2p_client_list,
  1453. &ssid->num_p2p_clients,
  1454. "p2p_client_list", 0, 0);
  1455. }
  1456. #ifndef NO_CONFIG_WRITE
  1457. static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
  1458. struct wpa_ssid *ssid)
  1459. {
  1460. return wpa_config_write_addr_list(data, ssid->p2p_client_list,
  1461. ssid->num_p2p_clients,
  1462. "p2p_client_list");
  1463. }
  1464. #endif /* NO_CONFIG_WRITE */
  1465. static int wpa_config_parse_psk_list(const struct parse_data *data,
  1466. struct wpa_ssid *ssid, int line,
  1467. const char *value)
  1468. {
  1469. struct psk_list_entry *p;
  1470. const char *pos;
  1471. p = os_zalloc(sizeof(*p));
  1472. if (p == NULL)
  1473. return -1;
  1474. pos = value;
  1475. if (os_strncmp(pos, "P2P-", 4) == 0) {
  1476. p->p2p = 1;
  1477. pos += 4;
  1478. }
  1479. if (hwaddr_aton(pos, p->addr)) {
  1480. wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
  1481. line, pos);
  1482. os_free(p);
  1483. return -1;
  1484. }
  1485. pos += 17;
  1486. if (*pos != '-') {
  1487. wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
  1488. line, pos);
  1489. os_free(p);
  1490. return -1;
  1491. }
  1492. pos++;
  1493. if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
  1494. wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
  1495. line, pos);
  1496. os_free(p);
  1497. return -1;
  1498. }
  1499. dl_list_add(&ssid->psk_list, &p->list);
  1500. return 0;
  1501. }
  1502. #ifndef NO_CONFIG_WRITE
  1503. static char * wpa_config_write_psk_list(const struct parse_data *data,
  1504. struct wpa_ssid *ssid)
  1505. {
  1506. return NULL;
  1507. }
  1508. #endif /* NO_CONFIG_WRITE */
  1509. #endif /* CONFIG_P2P */
  1510. #ifdef CONFIG_MESH
  1511. static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
  1512. struct wpa_ssid *ssid, int line,
  1513. const char *value)
  1514. {
  1515. int *rates = wpa_config_parse_int_array(value);
  1516. if (rates == NULL) {
  1517. wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
  1518. line, value);
  1519. return -1;
  1520. }
  1521. if (rates[0] == 0) {
  1522. os_free(rates);
  1523. rates = NULL;
  1524. }
  1525. os_free(ssid->mesh_basic_rates);
  1526. ssid->mesh_basic_rates = rates;
  1527. return 0;
  1528. }
  1529. #ifndef NO_CONFIG_WRITE
  1530. static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
  1531. struct wpa_ssid *ssid)
  1532. {
  1533. return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
  1534. }
  1535. #endif /* NO_CONFIG_WRITE */
  1536. #endif /* CONFIG_MESH */
  1537. /* Helper macros for network block parser */
  1538. #ifdef OFFSET
  1539. #undef OFFSET
  1540. #endif /* OFFSET */
  1541. /* OFFSET: Get offset of a variable within the wpa_ssid structure */
  1542. #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
  1543. /* STR: Define a string variable for an ASCII string; f = field name */
  1544. #ifdef NO_CONFIG_WRITE
  1545. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  1546. #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
  1547. #else /* NO_CONFIG_WRITE */
  1548. #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
  1549. #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
  1550. #endif /* NO_CONFIG_WRITE */
  1551. #define STR(f) _STR(f), NULL, NULL, NULL, 0
  1552. #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
  1553. #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
  1554. #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
  1555. /* STR_LEN: Define a string variable with a separate variable for storing the
  1556. * data length. Unlike STR(), this can be used to store arbitrary binary data
  1557. * (i.e., even nul termination character). */
  1558. #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
  1559. #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
  1560. #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
  1561. #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
  1562. #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
  1563. /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
  1564. * explicitly specified. */
  1565. #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
  1566. #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
  1567. #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
  1568. #ifdef NO_CONFIG_WRITE
  1569. #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
  1570. #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
  1571. #else /* NO_CONFIG_WRITE */
  1572. #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1573. OFFSET(f), (void *) 0
  1574. #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
  1575. OFFSET(eap.f), (void *) 0
  1576. #endif /* NO_CONFIG_WRITE */
  1577. /* INT: Define an integer variable */
  1578. #define INT(f) _INT(f), NULL, NULL, 0
  1579. #define INTe(f) _INTe(f), NULL, NULL, 0
  1580. /* INT_RANGE: Define an integer variable with allowed value range */
  1581. #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
  1582. /* FUNC: Define a configuration variable that uses a custom function for
  1583. * parsing and writing the value. */
  1584. #ifdef NO_CONFIG_WRITE
  1585. #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
  1586. #else /* NO_CONFIG_WRITE */
  1587. #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
  1588. NULL, NULL, NULL, NULL
  1589. #endif /* NO_CONFIG_WRITE */
  1590. #define FUNC(f) _FUNC(f), 0
  1591. #define FUNC_KEY(f) _FUNC(f), 1
  1592. /*
  1593. * Table of network configuration variables. This table is used to parse each
  1594. * network configuration variable, e.g., each line in wpa_supplicant.conf file
  1595. * that is inside a network block.
  1596. *
  1597. * This table is generated using the helper macros defined above and with
  1598. * generous help from the C pre-processor. The field name is stored as a string
  1599. * into .name and for STR and INT types, the offset of the target buffer within
  1600. * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
  1601. * offset to the field containing the length of the configuration variable.
  1602. * .param3 and .param4 can be used to mark the allowed range (length for STR
  1603. * and value for INT).
  1604. *
  1605. * For each configuration line in wpa_supplicant.conf, the parser goes through
  1606. * this table and select the entry that matches with the field name. The parser
  1607. * function (.parser) is then called to parse the actual value of the field.
  1608. *
  1609. * This kind of mechanism makes it easy to add new configuration parameters,
  1610. * since only one line needs to be added into this table and into the
  1611. * struct wpa_ssid definition if the new variable is either a string or
  1612. * integer. More complex types will need to use their own parser and writer
  1613. * functions.
  1614. */
  1615. static const struct parse_data ssid_fields[] = {
  1616. { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
  1617. { INT_RANGE(scan_ssid, 0, 1) },
  1618. { FUNC(bssid) },
  1619. { FUNC(bssid_blacklist) },
  1620. { FUNC(bssid_whitelist) },
  1621. { FUNC_KEY(psk) },
  1622. { INT(mem_only_psk) },
  1623. { FUNC(proto) },
  1624. { FUNC(key_mgmt) },
  1625. { INT(bg_scan_period) },
  1626. { FUNC(pairwise) },
  1627. { FUNC(group) },
  1628. { FUNC(auth_alg) },
  1629. { FUNC(scan_freq) },
  1630. { FUNC(freq_list) },
  1631. { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
  1632. VHT_CHANWIDTH_80P80MHZ) },
  1633. #ifdef IEEE8021X_EAPOL
  1634. { FUNC(eap) },
  1635. { STR_LENe(identity) },
  1636. { STR_LENe(anonymous_identity) },
  1637. { FUNC_KEY(password) },
  1638. { STRe(ca_cert) },
  1639. { STRe(ca_path) },
  1640. { STRe(client_cert) },
  1641. { STRe(private_key) },
  1642. { STR_KEYe(private_key_passwd) },
  1643. { STRe(dh_file) },
  1644. { STRe(subject_match) },
  1645. { STRe(altsubject_match) },
  1646. { STRe(domain_suffix_match) },
  1647. { STRe(domain_match) },
  1648. { STRe(ca_cert2) },
  1649. { STRe(ca_path2) },
  1650. { STRe(client_cert2) },
  1651. { STRe(private_key2) },
  1652. { STR_KEYe(private_key2_passwd) },
  1653. { STRe(dh_file2) },
  1654. { STRe(subject_match2) },
  1655. { STRe(altsubject_match2) },
  1656. { STRe(domain_suffix_match2) },
  1657. { STRe(domain_match2) },
  1658. { STRe(phase1) },
  1659. { STRe(phase2) },
  1660. { STRe(pcsc) },
  1661. { STR_KEYe(pin) },
  1662. { STRe(engine_id) },
  1663. { STRe(key_id) },
  1664. { STRe(cert_id) },
  1665. { STRe(ca_cert_id) },
  1666. { STR_KEYe(pin2) },
  1667. { STRe(engine2_id) },
  1668. { STRe(key2_id) },
  1669. { STRe(cert2_id) },
  1670. { STRe(ca_cert2_id) },
  1671. { INTe(engine) },
  1672. { INTe(engine2) },
  1673. { INT(eapol_flags) },
  1674. { INTe(sim_num) },
  1675. { STRe(openssl_ciphers) },
  1676. { INTe(erp) },
  1677. #endif /* IEEE8021X_EAPOL */
  1678. { FUNC_KEY(wep_key0) },
  1679. { FUNC_KEY(wep_key1) },
  1680. { FUNC_KEY(wep_key2) },
  1681. { FUNC_KEY(wep_key3) },
  1682. { INT(wep_tx_keyidx) },
  1683. { INT(priority) },
  1684. #ifdef IEEE8021X_EAPOL
  1685. { INT(eap_workaround) },
  1686. { STRe(pac_file) },
  1687. { INTe(fragment_size) },
  1688. { INTe(ocsp) },
  1689. #endif /* IEEE8021X_EAPOL */
  1690. #ifdef CONFIG_MESH
  1691. { INT_RANGE(mode, 0, 5) },
  1692. { INT_RANGE(no_auto_peer, 0, 1) },
  1693. #else /* CONFIG_MESH */
  1694. { INT_RANGE(mode, 0, 4) },
  1695. #endif /* CONFIG_MESH */
  1696. { INT_RANGE(proactive_key_caching, 0, 1) },
  1697. { INT_RANGE(disabled, 0, 2) },
  1698. { STR(id_str) },
  1699. #ifdef CONFIG_IEEE80211W
  1700. { INT_RANGE(ieee80211w, 0, 2) },
  1701. #endif /* CONFIG_IEEE80211W */
  1702. { INT_RANGE(peerkey, 0, 1) },
  1703. { INT_RANGE(mixed_cell, 0, 1) },
  1704. { INT_RANGE(frequency, 0, 65000) },
  1705. { INT_RANGE(fixed_freq, 0, 1) },
  1706. #ifdef CONFIG_ACS
  1707. { INT_RANGE(acs, 0, 1) },
  1708. #endif /* CONFIG_ACS */
  1709. #ifdef CONFIG_MESH
  1710. { FUNC(mesh_basic_rates) },
  1711. { INT(dot11MeshMaxRetries) },
  1712. { INT(dot11MeshRetryTimeout) },
  1713. { INT(dot11MeshConfirmTimeout) },
  1714. { INT(dot11MeshHoldingTimeout) },
  1715. #endif /* CONFIG_MESH */
  1716. { INT(wpa_ptk_rekey) },
  1717. { INT(group_rekey) },
  1718. { STR(bgscan) },
  1719. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1720. #ifdef CONFIG_P2P
  1721. { FUNC(go_p2p_dev_addr) },
  1722. { FUNC(p2p_client_list) },
  1723. { FUNC(psk_list) },
  1724. #endif /* CONFIG_P2P */
  1725. #ifdef CONFIG_HT_OVERRIDES
  1726. { INT_RANGE(disable_ht, 0, 1) },
  1727. { INT_RANGE(disable_ht40, -1, 1) },
  1728. { INT_RANGE(disable_sgi, 0, 1) },
  1729. { INT_RANGE(disable_ldpc, 0, 1) },
  1730. { INT_RANGE(ht40_intolerant, 0, 1) },
  1731. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1732. { INT_RANGE(ampdu_factor, -1, 3) },
  1733. { INT_RANGE(ampdu_density, -1, 7) },
  1734. { STR(ht_mcs) },
  1735. #endif /* CONFIG_HT_OVERRIDES */
  1736. #ifdef CONFIG_VHT_OVERRIDES
  1737. { INT_RANGE(disable_vht, 0, 1) },
  1738. { INT(vht_capa) },
  1739. { INT(vht_capa_mask) },
  1740. { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
  1741. { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
  1742. { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
  1743. { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
  1744. { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
  1745. { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
  1746. { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
  1747. { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
  1748. { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
  1749. { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
  1750. { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
  1751. { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
  1752. { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
  1753. { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
  1754. { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
  1755. { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
  1756. #endif /* CONFIG_VHT_OVERRIDES */
  1757. { INT(ap_max_inactivity) },
  1758. { INT(dtim_period) },
  1759. { INT(beacon_int) },
  1760. #ifdef CONFIG_MACSEC
  1761. { INT_RANGE(macsec_policy, 0, 1) },
  1762. #endif /* CONFIG_MACSEC */
  1763. #ifdef CONFIG_HS20
  1764. { INT(update_identifier) },
  1765. #endif /* CONFIG_HS20 */
  1766. { INT_RANGE(mac_addr, 0, 2) },
  1767. { INT_RANGE(pbss, 0, 2) },
  1768. { INT_RANGE(wps_disabled, 0, 1) },
  1769. };
  1770. #undef OFFSET
  1771. #undef _STR
  1772. #undef STR
  1773. #undef STR_KEY
  1774. #undef _STR_LEN
  1775. #undef STR_LEN
  1776. #undef STR_LEN_KEY
  1777. #undef _STR_RANGE
  1778. #undef STR_RANGE
  1779. #undef STR_RANGE_KEY
  1780. #undef _INT
  1781. #undef INT
  1782. #undef INT_RANGE
  1783. #undef _FUNC
  1784. #undef FUNC
  1785. #undef FUNC_KEY
  1786. #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
  1787. /**
  1788. * wpa_config_add_prio_network - Add a network to priority lists
  1789. * @config: Configuration data from wpa_config_read()
  1790. * @ssid: Pointer to the network configuration to be added to the list
  1791. * Returns: 0 on success, -1 on failure
  1792. *
  1793. * This function is used to add a network block to the priority list of
  1794. * networks. This must be called for each network when reading in the full
  1795. * configuration. In addition, this can be used indirectly when updating
  1796. * priorities by calling wpa_config_update_prio_list().
  1797. */
  1798. int wpa_config_add_prio_network(struct wpa_config *config,
  1799. struct wpa_ssid *ssid)
  1800. {
  1801. int prio;
  1802. struct wpa_ssid *prev, **nlist;
  1803. /*
  1804. * Add to an existing priority list if one is available for the
  1805. * configured priority level for this network.
  1806. */
  1807. for (prio = 0; prio < config->num_prio; prio++) {
  1808. prev = config->pssid[prio];
  1809. if (prev->priority == ssid->priority) {
  1810. while (prev->pnext)
  1811. prev = prev->pnext;
  1812. prev->pnext = ssid;
  1813. return 0;
  1814. }
  1815. }
  1816. /* First network for this priority - add a new priority list */
  1817. nlist = os_realloc_array(config->pssid, config->num_prio + 1,
  1818. sizeof(struct wpa_ssid *));
  1819. if (nlist == NULL)
  1820. return -1;
  1821. for (prio = 0; prio < config->num_prio; prio++) {
  1822. if (nlist[prio]->priority < ssid->priority) {
  1823. os_memmove(&nlist[prio + 1], &nlist[prio],
  1824. (config->num_prio - prio) *
  1825. sizeof(struct wpa_ssid *));
  1826. break;
  1827. }
  1828. }
  1829. nlist[prio] = ssid;
  1830. config->num_prio++;
  1831. config->pssid = nlist;
  1832. return 0;
  1833. }
  1834. /**
  1835. * wpa_config_update_prio_list - Update network priority list
  1836. * @config: Configuration data from wpa_config_read()
  1837. * Returns: 0 on success, -1 on failure
  1838. *
  1839. * This function is called to update the priority list of networks in the
  1840. * configuration when a network is being added or removed. This is also called
  1841. * if a priority for a network is changed.
  1842. */
  1843. int wpa_config_update_prio_list(struct wpa_config *config)
  1844. {
  1845. struct wpa_ssid *ssid;
  1846. int ret = 0;
  1847. os_free(config->pssid);
  1848. config->pssid = NULL;
  1849. config->num_prio = 0;
  1850. ssid = config->ssid;
  1851. while (ssid) {
  1852. ssid->pnext = NULL;
  1853. if (wpa_config_add_prio_network(config, ssid) < 0)
  1854. ret = -1;
  1855. ssid = ssid->next;
  1856. }
  1857. return ret;
  1858. }
  1859. #ifdef IEEE8021X_EAPOL
  1860. static void eap_peer_config_free(struct eap_peer_config *eap)
  1861. {
  1862. os_free(eap->eap_methods);
  1863. bin_clear_free(eap->identity, eap->identity_len);
  1864. os_free(eap->anonymous_identity);
  1865. bin_clear_free(eap->password, eap->password_len);
  1866. os_free(eap->ca_cert);
  1867. os_free(eap->ca_path);
  1868. os_free(eap->client_cert);
  1869. os_free(eap->private_key);
  1870. str_clear_free(eap->private_key_passwd);
  1871. os_free(eap->dh_file);
  1872. os_free(eap->subject_match);
  1873. os_free(eap->altsubject_match);
  1874. os_free(eap->domain_suffix_match);
  1875. os_free(eap->domain_match);
  1876. os_free(eap->ca_cert2);
  1877. os_free(eap->ca_path2);
  1878. os_free(eap->client_cert2);
  1879. os_free(eap->private_key2);
  1880. str_clear_free(eap->private_key2_passwd);
  1881. os_free(eap->dh_file2);
  1882. os_free(eap->subject_match2);
  1883. os_free(eap->altsubject_match2);
  1884. os_free(eap->domain_suffix_match2);
  1885. os_free(eap->domain_match2);
  1886. os_free(eap->phase1);
  1887. os_free(eap->phase2);
  1888. os_free(eap->pcsc);
  1889. str_clear_free(eap->pin);
  1890. os_free(eap->engine_id);
  1891. os_free(eap->key_id);
  1892. os_free(eap->cert_id);
  1893. os_free(eap->ca_cert_id);
  1894. os_free(eap->key2_id);
  1895. os_free(eap->cert2_id);
  1896. os_free(eap->ca_cert2_id);
  1897. str_clear_free(eap->pin2);
  1898. os_free(eap->engine2_id);
  1899. os_free(eap->otp);
  1900. os_free(eap->pending_req_otp);
  1901. os_free(eap->pac_file);
  1902. bin_clear_free(eap->new_password, eap->new_password_len);
  1903. str_clear_free(eap->external_sim_resp);
  1904. os_free(eap->openssl_ciphers);
  1905. }
  1906. #endif /* IEEE8021X_EAPOL */
  1907. /**
  1908. * wpa_config_free_ssid - Free network/ssid configuration data
  1909. * @ssid: Configuration data for the network
  1910. *
  1911. * This function frees all resources allocated for the network configuration
  1912. * data.
  1913. */
  1914. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1915. {
  1916. struct psk_list_entry *psk;
  1917. os_free(ssid->ssid);
  1918. str_clear_free(ssid->passphrase);
  1919. os_free(ssid->ext_psk);
  1920. #ifdef IEEE8021X_EAPOL
  1921. eap_peer_config_free(&ssid->eap);
  1922. #endif /* IEEE8021X_EAPOL */
  1923. os_free(ssid->id_str);
  1924. os_free(ssid->scan_freq);
  1925. os_free(ssid->freq_list);
  1926. os_free(ssid->bgscan);
  1927. os_free(ssid->p2p_client_list);
  1928. os_free(ssid->bssid_blacklist);
  1929. os_free(ssid->bssid_whitelist);
  1930. #ifdef CONFIG_HT_OVERRIDES
  1931. os_free(ssid->ht_mcs);
  1932. #endif /* CONFIG_HT_OVERRIDES */
  1933. #ifdef CONFIG_MESH
  1934. os_free(ssid->mesh_basic_rates);
  1935. #endif /* CONFIG_MESH */
  1936. while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
  1937. list))) {
  1938. dl_list_del(&psk->list);
  1939. bin_clear_free(psk, sizeof(*psk));
  1940. }
  1941. bin_clear_free(ssid, sizeof(*ssid));
  1942. }
  1943. void wpa_config_free_cred(struct wpa_cred *cred)
  1944. {
  1945. size_t i;
  1946. os_free(cred->realm);
  1947. str_clear_free(cred->username);
  1948. str_clear_free(cred->password);
  1949. os_free(cred->ca_cert);
  1950. os_free(cred->client_cert);
  1951. os_free(cred->private_key);
  1952. str_clear_free(cred->private_key_passwd);
  1953. os_free(cred->imsi);
  1954. str_clear_free(cred->milenage);
  1955. for (i = 0; i < cred->num_domain; i++)
  1956. os_free(cred->domain[i]);
  1957. os_free(cred->domain);
  1958. os_free(cred->domain_suffix_match);
  1959. os_free(cred->eap_method);
  1960. os_free(cred->phase1);
  1961. os_free(cred->phase2);
  1962. os_free(cred->excluded_ssid);
  1963. os_free(cred->roaming_partner);
  1964. os_free(cred->provisioning_sp);
  1965. for (i = 0; i < cred->num_req_conn_capab; i++)
  1966. os_free(cred->req_conn_capab_port[i]);
  1967. os_free(cred->req_conn_capab_port);
  1968. os_free(cred->req_conn_capab_proto);
  1969. os_free(cred);
  1970. }
  1971. void wpa_config_flush_blobs(struct wpa_config *config)
  1972. {
  1973. #ifndef CONFIG_NO_CONFIG_BLOBS
  1974. struct wpa_config_blob *blob, *prev;
  1975. blob = config->blobs;
  1976. config->blobs = NULL;
  1977. while (blob) {
  1978. prev = blob;
  1979. blob = blob->next;
  1980. wpa_config_free_blob(prev);
  1981. }
  1982. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1983. }
  1984. /**
  1985. * wpa_config_free - Free configuration data
  1986. * @config: Configuration data from wpa_config_read()
  1987. *
  1988. * This function frees all resources allocated for the configuration data by
  1989. * wpa_config_read().
  1990. */
  1991. void wpa_config_free(struct wpa_config *config)
  1992. {
  1993. struct wpa_ssid *ssid, *prev = NULL;
  1994. struct wpa_cred *cred, *cprev;
  1995. int i;
  1996. ssid = config->ssid;
  1997. while (ssid) {
  1998. prev = ssid;
  1999. ssid = ssid->next;
  2000. wpa_config_free_ssid(prev);
  2001. }
  2002. cred = config->cred;
  2003. while (cred) {
  2004. cprev = cred;
  2005. cred = cred->next;
  2006. wpa_config_free_cred(cprev);
  2007. }
  2008. wpa_config_flush_blobs(config);
  2009. wpabuf_free(config->wps_vendor_ext_m1);
  2010. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
  2011. wpabuf_free(config->wps_vendor_ext[i]);
  2012. os_free(config->ctrl_interface);
  2013. os_free(config->ctrl_interface_group);
  2014. os_free(config->opensc_engine_path);
  2015. os_free(config->pkcs11_engine_path);
  2016. os_free(config->pkcs11_module_path);
  2017. os_free(config->openssl_ciphers);
  2018. os_free(config->pcsc_reader);
  2019. str_clear_free(config->pcsc_pin);
  2020. os_free(config->driver_param);
  2021. os_free(config->device_name);
  2022. os_free(config->manufacturer);
  2023. os_free(config->model_name);
  2024. os_free(config->model_number);
  2025. os_free(config->serial_number);
  2026. os_free(config->config_methods);
  2027. os_free(config->p2p_ssid_postfix);
  2028. os_free(config->pssid);
  2029. os_free(config->p2p_pref_chan);
  2030. os_free(config->p2p_no_go_freq.range);
  2031. os_free(config->autoscan);
  2032. os_free(config->freq_list);
  2033. wpabuf_free(config->wps_nfc_dh_pubkey);
  2034. wpabuf_free(config->wps_nfc_dh_privkey);
  2035. wpabuf_free(config->wps_nfc_dev_pw);
  2036. os_free(config->ext_password_backend);
  2037. os_free(config->sae_groups);
  2038. wpabuf_free(config->ap_vendor_elements);
  2039. os_free(config->osu_dir);
  2040. os_free(config->bgscan);
  2041. os_free(config->wowlan_triggers);
  2042. os_free(config->fst_group_id);
  2043. os_free(config->sched_scan_plans);
  2044. #ifdef CONFIG_MBO
  2045. os_free(config->non_pref_chan);
  2046. #endif /* CONFIG_MBO */
  2047. os_free(config);
  2048. }
  2049. /**
  2050. * wpa_config_foreach_network - Iterate over each configured network
  2051. * @config: Configuration data from wpa_config_read()
  2052. * @func: Callback function to process each network
  2053. * @arg: Opaque argument to pass to callback function
  2054. *
  2055. * Iterate over the set of configured networks calling the specified
  2056. * function for each item. We guard against callbacks removing the
  2057. * supplied network.
  2058. */
  2059. void wpa_config_foreach_network(struct wpa_config *config,
  2060. void (*func)(void *, struct wpa_ssid *),
  2061. void *arg)
  2062. {
  2063. struct wpa_ssid *ssid, *next;
  2064. ssid = config->ssid;
  2065. while (ssid) {
  2066. next = ssid->next;
  2067. func(arg, ssid);
  2068. ssid = next;
  2069. }
  2070. }
  2071. /**
  2072. * wpa_config_get_network - Get configured network based on id
  2073. * @config: Configuration data from wpa_config_read()
  2074. * @id: Unique network id to search for
  2075. * Returns: Network configuration or %NULL if not found
  2076. */
  2077. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  2078. {
  2079. struct wpa_ssid *ssid;
  2080. ssid = config->ssid;
  2081. while (ssid) {
  2082. if (id == ssid->id)
  2083. break;
  2084. ssid = ssid->next;
  2085. }
  2086. return ssid;
  2087. }
  2088. /**
  2089. * wpa_config_add_network - Add a new network with empty configuration
  2090. * @config: Configuration data from wpa_config_read()
  2091. * Returns: The new network configuration or %NULL if operation failed
  2092. */
  2093. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  2094. {
  2095. int id;
  2096. struct wpa_ssid *ssid, *last = NULL;
  2097. id = -1;
  2098. ssid = config->ssid;
  2099. while (ssid) {
  2100. if (ssid->id > id)
  2101. id = ssid->id;
  2102. last = ssid;
  2103. ssid = ssid->next;
  2104. }
  2105. id++;
  2106. ssid = os_zalloc(sizeof(*ssid));
  2107. if (ssid == NULL)
  2108. return NULL;
  2109. ssid->id = id;
  2110. dl_list_init(&ssid->psk_list);
  2111. if (last)
  2112. last->next = ssid;
  2113. else
  2114. config->ssid = ssid;
  2115. wpa_config_update_prio_list(config);
  2116. return ssid;
  2117. }
  2118. /**
  2119. * wpa_config_remove_network - Remove a configured network based on id
  2120. * @config: Configuration data from wpa_config_read()
  2121. * @id: Unique network id to search for
  2122. * Returns: 0 on success, or -1 if the network was not found
  2123. */
  2124. int wpa_config_remove_network(struct wpa_config *config, int id)
  2125. {
  2126. struct wpa_ssid *ssid, *prev = NULL;
  2127. ssid = config->ssid;
  2128. while (ssid) {
  2129. if (id == ssid->id)
  2130. break;
  2131. prev = ssid;
  2132. ssid = ssid->next;
  2133. }
  2134. if (ssid == NULL)
  2135. return -1;
  2136. if (prev)
  2137. prev->next = ssid->next;
  2138. else
  2139. config->ssid = ssid->next;
  2140. wpa_config_update_prio_list(config);
  2141. wpa_config_free_ssid(ssid);
  2142. return 0;
  2143. }
  2144. /**
  2145. * wpa_config_set_network_defaults - Set network default values
  2146. * @ssid: Pointer to network configuration data
  2147. */
  2148. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  2149. {
  2150. ssid->proto = DEFAULT_PROTO;
  2151. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  2152. ssid->group_cipher = DEFAULT_GROUP;
  2153. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  2154. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  2155. #ifdef IEEE8021X_EAPOL
  2156. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  2157. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  2158. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  2159. ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
  2160. #endif /* IEEE8021X_EAPOL */
  2161. #ifdef CONFIG_MESH
  2162. ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
  2163. ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
  2164. ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
  2165. ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
  2166. #endif /* CONFIG_MESH */
  2167. #ifdef CONFIG_HT_OVERRIDES
  2168. ssid->disable_ht = DEFAULT_DISABLE_HT;
  2169. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  2170. ssid->disable_sgi = DEFAULT_DISABLE_SGI;
  2171. ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
  2172. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  2173. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  2174. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  2175. #endif /* CONFIG_HT_OVERRIDES */
  2176. #ifdef CONFIG_VHT_OVERRIDES
  2177. ssid->vht_rx_mcs_nss_1 = -1;
  2178. ssid->vht_rx_mcs_nss_2 = -1;
  2179. ssid->vht_rx_mcs_nss_3 = -1;
  2180. ssid->vht_rx_mcs_nss_4 = -1;
  2181. ssid->vht_rx_mcs_nss_5 = -1;
  2182. ssid->vht_rx_mcs_nss_6 = -1;
  2183. ssid->vht_rx_mcs_nss_7 = -1;
  2184. ssid->vht_rx_mcs_nss_8 = -1;
  2185. ssid->vht_tx_mcs_nss_1 = -1;
  2186. ssid->vht_tx_mcs_nss_2 = -1;
  2187. ssid->vht_tx_mcs_nss_3 = -1;
  2188. ssid->vht_tx_mcs_nss_4 = -1;
  2189. ssid->vht_tx_mcs_nss_5 = -1;
  2190. ssid->vht_tx_mcs_nss_6 = -1;
  2191. ssid->vht_tx_mcs_nss_7 = -1;
  2192. ssid->vht_tx_mcs_nss_8 = -1;
  2193. #endif /* CONFIG_VHT_OVERRIDES */
  2194. ssid->proactive_key_caching = -1;
  2195. #ifdef CONFIG_IEEE80211W
  2196. ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
  2197. #endif /* CONFIG_IEEE80211W */
  2198. ssid->mac_addr = -1;
  2199. }
  2200. /**
  2201. * wpa_config_set - Set a variable in network configuration
  2202. * @ssid: Pointer to network configuration data
  2203. * @var: Variable name, e.g., "ssid"
  2204. * @value: Variable value
  2205. * @line: Line number in configuration file or 0 if not used
  2206. * Returns: 0 on success with possible change in the value, 1 on success with
  2207. * no change to previously configured value, or -1 on failure
  2208. *
  2209. * This function can be used to set network configuration variables based on
  2210. * both the configuration file and management interface input. The value
  2211. * parameter must be in the same format as the text-based configuration file is
  2212. * using. For example, strings are using double quotation marks.
  2213. */
  2214. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  2215. int line)
  2216. {
  2217. size_t i;
  2218. int ret = 0;
  2219. if (ssid == NULL || var == NULL || value == NULL)
  2220. return -1;
  2221. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2222. const struct parse_data *field = &ssid_fields[i];
  2223. if (os_strcmp(var, field->name) != 0)
  2224. continue;
  2225. ret = field->parser(field, ssid, line, value);
  2226. if (ret < 0) {
  2227. if (line) {
  2228. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2229. "parse %s '%s'.", line, var, value);
  2230. }
  2231. ret = -1;
  2232. }
  2233. break;
  2234. }
  2235. if (i == NUM_SSID_FIELDS) {
  2236. if (line) {
  2237. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  2238. "'%s'.", line, var);
  2239. }
  2240. ret = -1;
  2241. }
  2242. return ret;
  2243. }
  2244. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  2245. const char *value)
  2246. {
  2247. size_t len;
  2248. char *buf;
  2249. int ret;
  2250. len = os_strlen(value);
  2251. buf = os_malloc(len + 3);
  2252. if (buf == NULL)
  2253. return -1;
  2254. buf[0] = '"';
  2255. os_memcpy(buf + 1, value, len);
  2256. buf[len + 1] = '"';
  2257. buf[len + 2] = '\0';
  2258. ret = wpa_config_set(ssid, var, buf, 0);
  2259. os_free(buf);
  2260. return ret;
  2261. }
  2262. /**
  2263. * wpa_config_get_all - Get all options from network configuration
  2264. * @ssid: Pointer to network configuration data
  2265. * @get_keys: Determines if keys/passwords will be included in returned list
  2266. * (if they may be exported)
  2267. * Returns: %NULL terminated list of all set keys and their values in the form
  2268. * of [key1, val1, key2, val2, ... , NULL]
  2269. *
  2270. * This function can be used to get list of all configured network properties.
  2271. * The caller is responsible for freeing the returned list and all its
  2272. * elements.
  2273. */
  2274. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  2275. {
  2276. #ifdef NO_CONFIG_WRITE
  2277. return NULL;
  2278. #else /* NO_CONFIG_WRITE */
  2279. const struct parse_data *field;
  2280. char *key, *value;
  2281. size_t i;
  2282. char **props;
  2283. int fields_num;
  2284. get_keys = get_keys && ssid->export_keys;
  2285. props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
  2286. if (!props)
  2287. return NULL;
  2288. fields_num = 0;
  2289. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2290. field = &ssid_fields[i];
  2291. if (field->key_data && !get_keys)
  2292. continue;
  2293. value = field->writer(field, ssid);
  2294. if (value == NULL)
  2295. continue;
  2296. if (os_strlen(value) == 0) {
  2297. os_free(value);
  2298. continue;
  2299. }
  2300. key = os_strdup(field->name);
  2301. if (key == NULL) {
  2302. os_free(value);
  2303. goto err;
  2304. }
  2305. props[fields_num * 2] = key;
  2306. props[fields_num * 2 + 1] = value;
  2307. fields_num++;
  2308. }
  2309. return props;
  2310. err:
  2311. for (i = 0; props[i]; i++)
  2312. os_free(props[i]);
  2313. os_free(props);
  2314. return NULL;
  2315. #endif /* NO_CONFIG_WRITE */
  2316. }
  2317. #ifndef NO_CONFIG_WRITE
  2318. /**
  2319. * wpa_config_get - Get a variable in network configuration
  2320. * @ssid: Pointer to network configuration data
  2321. * @var: Variable name, e.g., "ssid"
  2322. * Returns: Value of the variable or %NULL on failure
  2323. *
  2324. * This function can be used to get network configuration variables. The
  2325. * returned value is a copy of the configuration variable in text format, i.e,.
  2326. * the same format that the text-based configuration file and wpa_config_set()
  2327. * are using for the value. The caller is responsible for freeing the returned
  2328. * value.
  2329. */
  2330. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  2331. {
  2332. size_t i;
  2333. if (ssid == NULL || var == NULL)
  2334. return NULL;
  2335. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2336. const struct parse_data *field = &ssid_fields[i];
  2337. if (os_strcmp(var, field->name) == 0) {
  2338. char *ret = field->writer(field, ssid);
  2339. if (ret && has_newline(ret)) {
  2340. wpa_printf(MSG_ERROR,
  2341. "Found newline in value for %s; not returning it",
  2342. var);
  2343. os_free(ret);
  2344. ret = NULL;
  2345. }
  2346. return ret;
  2347. }
  2348. }
  2349. return NULL;
  2350. }
  2351. /**
  2352. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  2353. * @ssid: Pointer to network configuration data
  2354. * @var: Variable name, e.g., "ssid"
  2355. * Returns: Value of the variable or %NULL on failure
  2356. *
  2357. * This function can be used to get network configuration variable like
  2358. * wpa_config_get(). The only difference is that this functions does not expose
  2359. * key/password material from the configuration. In case a key/password field
  2360. * is requested, the returned value is an empty string or %NULL if the variable
  2361. * is not set or "*" if the variable is set (regardless of its value). The
  2362. * returned value is a copy of the configuration variable in text format, i.e,.
  2363. * the same format that the text-based configuration file and wpa_config_set()
  2364. * are using for the value. The caller is responsible for freeing the returned
  2365. * value.
  2366. */
  2367. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  2368. {
  2369. size_t i;
  2370. if (ssid == NULL || var == NULL)
  2371. return NULL;
  2372. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2373. const struct parse_data *field = &ssid_fields[i];
  2374. if (os_strcmp(var, field->name) == 0) {
  2375. char *res = field->writer(field, ssid);
  2376. if (field->key_data) {
  2377. if (res && res[0]) {
  2378. wpa_printf(MSG_DEBUG, "Do not allow "
  2379. "key_data field to be "
  2380. "exposed");
  2381. str_clear_free(res);
  2382. return os_strdup("*");
  2383. }
  2384. os_free(res);
  2385. return NULL;
  2386. }
  2387. return res;
  2388. }
  2389. }
  2390. return NULL;
  2391. }
  2392. #endif /* NO_CONFIG_WRITE */
  2393. /**
  2394. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  2395. * @ssid: Pointer to network configuration data
  2396. *
  2397. * This function must be called to update WPA PSK when either SSID or the
  2398. * passphrase has changed for the network configuration.
  2399. */
  2400. void wpa_config_update_psk(struct wpa_ssid *ssid)
  2401. {
  2402. #ifndef CONFIG_NO_PBKDF2
  2403. pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
  2404. ssid->psk, PMK_LEN);
  2405. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  2406. ssid->psk, PMK_LEN);
  2407. ssid->psk_set = 1;
  2408. #endif /* CONFIG_NO_PBKDF2 */
  2409. }
  2410. static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
  2411. const char *value)
  2412. {
  2413. u8 *proto;
  2414. int **port;
  2415. int *ports, *nports;
  2416. const char *pos;
  2417. unsigned int num_ports;
  2418. proto = os_realloc_array(cred->req_conn_capab_proto,
  2419. cred->num_req_conn_capab + 1, sizeof(u8));
  2420. if (proto == NULL)
  2421. return -1;
  2422. cred->req_conn_capab_proto = proto;
  2423. port = os_realloc_array(cred->req_conn_capab_port,
  2424. cred->num_req_conn_capab + 1, sizeof(int *));
  2425. if (port == NULL)
  2426. return -1;
  2427. cred->req_conn_capab_port = port;
  2428. proto[cred->num_req_conn_capab] = atoi(value);
  2429. pos = os_strchr(value, ':');
  2430. if (pos == NULL) {
  2431. port[cred->num_req_conn_capab] = NULL;
  2432. cred->num_req_conn_capab++;
  2433. return 0;
  2434. }
  2435. pos++;
  2436. ports = NULL;
  2437. num_ports = 0;
  2438. while (*pos) {
  2439. nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
  2440. if (nports == NULL) {
  2441. os_free(ports);
  2442. return -1;
  2443. }
  2444. ports = nports;
  2445. ports[num_ports++] = atoi(pos);
  2446. pos = os_strchr(pos, ',');
  2447. if (pos == NULL)
  2448. break;
  2449. pos++;
  2450. }
  2451. nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
  2452. if (nports == NULL) {
  2453. os_free(ports);
  2454. return -1;
  2455. }
  2456. ports = nports;
  2457. ports[num_ports] = -1;
  2458. port[cred->num_req_conn_capab] = ports;
  2459. cred->num_req_conn_capab++;
  2460. return 0;
  2461. }
  2462. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  2463. const char *value, int line)
  2464. {
  2465. char *val;
  2466. size_t len;
  2467. if (os_strcmp(var, "temporary") == 0) {
  2468. cred->temporary = atoi(value);
  2469. return 0;
  2470. }
  2471. if (os_strcmp(var, "priority") == 0) {
  2472. cred->priority = atoi(value);
  2473. return 0;
  2474. }
  2475. if (os_strcmp(var, "sp_priority") == 0) {
  2476. int prio = atoi(value);
  2477. if (prio < 0 || prio > 255)
  2478. return -1;
  2479. cred->sp_priority = prio;
  2480. return 0;
  2481. }
  2482. if (os_strcmp(var, "pcsc") == 0) {
  2483. cred->pcsc = atoi(value);
  2484. return 0;
  2485. }
  2486. if (os_strcmp(var, "eap") == 0) {
  2487. struct eap_method_type method;
  2488. method.method = eap_peer_get_type(value, &method.vendor);
  2489. if (method.vendor == EAP_VENDOR_IETF &&
  2490. method.method == EAP_TYPE_NONE) {
  2491. wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
  2492. "for a credential", line, value);
  2493. return -1;
  2494. }
  2495. os_free(cred->eap_method);
  2496. cred->eap_method = os_malloc(sizeof(*cred->eap_method));
  2497. if (cred->eap_method == NULL)
  2498. return -1;
  2499. os_memcpy(cred->eap_method, &method, sizeof(method));
  2500. return 0;
  2501. }
  2502. if (os_strcmp(var, "password") == 0 &&
  2503. os_strncmp(value, "ext:", 4) == 0) {
  2504. if (has_newline(value))
  2505. return -1;
  2506. str_clear_free(cred->password);
  2507. cred->password = os_strdup(value);
  2508. cred->ext_password = 1;
  2509. return 0;
  2510. }
  2511. if (os_strcmp(var, "update_identifier") == 0) {
  2512. cred->update_identifier = atoi(value);
  2513. return 0;
  2514. }
  2515. if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
  2516. cred->min_dl_bandwidth_home = atoi(value);
  2517. return 0;
  2518. }
  2519. if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
  2520. cred->min_ul_bandwidth_home = atoi(value);
  2521. return 0;
  2522. }
  2523. if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
  2524. cred->min_dl_bandwidth_roaming = atoi(value);
  2525. return 0;
  2526. }
  2527. if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
  2528. cred->min_ul_bandwidth_roaming = atoi(value);
  2529. return 0;
  2530. }
  2531. if (os_strcmp(var, "max_bss_load") == 0) {
  2532. cred->max_bss_load = atoi(value);
  2533. return 0;
  2534. }
  2535. if (os_strcmp(var, "req_conn_capab") == 0)
  2536. return wpa_config_set_cred_req_conn_capab(cred, value);
  2537. if (os_strcmp(var, "ocsp") == 0) {
  2538. cred->ocsp = atoi(value);
  2539. return 0;
  2540. }
  2541. if (os_strcmp(var, "sim_num") == 0) {
  2542. cred->sim_num = atoi(value);
  2543. return 0;
  2544. }
  2545. val = wpa_config_parse_string(value, &len);
  2546. if (val == NULL ||
  2547. (os_strcmp(var, "excluded_ssid") != 0 &&
  2548. os_strcmp(var, "roaming_consortium") != 0 &&
  2549. os_strcmp(var, "required_roaming_consortium") != 0 &&
  2550. has_newline(val))) {
  2551. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  2552. "value '%s'.", line, var, value);
  2553. os_free(val);
  2554. return -1;
  2555. }
  2556. if (os_strcmp(var, "realm") == 0) {
  2557. os_free(cred->realm);
  2558. cred->realm = val;
  2559. return 0;
  2560. }
  2561. if (os_strcmp(var, "username") == 0) {
  2562. str_clear_free(cred->username);
  2563. cred->username = val;
  2564. return 0;
  2565. }
  2566. if (os_strcmp(var, "password") == 0) {
  2567. str_clear_free(cred->password);
  2568. cred->password = val;
  2569. cred->ext_password = 0;
  2570. return 0;
  2571. }
  2572. if (os_strcmp(var, "ca_cert") == 0) {
  2573. os_free(cred->ca_cert);
  2574. cred->ca_cert = val;
  2575. return 0;
  2576. }
  2577. if (os_strcmp(var, "client_cert") == 0) {
  2578. os_free(cred->client_cert);
  2579. cred->client_cert = val;
  2580. return 0;
  2581. }
  2582. if (os_strcmp(var, "private_key") == 0) {
  2583. os_free(cred->private_key);
  2584. cred->private_key = val;
  2585. return 0;
  2586. }
  2587. if (os_strcmp(var, "private_key_passwd") == 0) {
  2588. str_clear_free(cred->private_key_passwd);
  2589. cred->private_key_passwd = val;
  2590. return 0;
  2591. }
  2592. if (os_strcmp(var, "imsi") == 0) {
  2593. os_free(cred->imsi);
  2594. cred->imsi = val;
  2595. return 0;
  2596. }
  2597. if (os_strcmp(var, "milenage") == 0) {
  2598. str_clear_free(cred->milenage);
  2599. cred->milenage = val;
  2600. return 0;
  2601. }
  2602. if (os_strcmp(var, "domain_suffix_match") == 0) {
  2603. os_free(cred->domain_suffix_match);
  2604. cred->domain_suffix_match = val;
  2605. return 0;
  2606. }
  2607. if (os_strcmp(var, "domain") == 0) {
  2608. char **new_domain;
  2609. new_domain = os_realloc_array(cred->domain,
  2610. cred->num_domain + 1,
  2611. sizeof(char *));
  2612. if (new_domain == NULL) {
  2613. os_free(val);
  2614. return -1;
  2615. }
  2616. new_domain[cred->num_domain++] = val;
  2617. cred->domain = new_domain;
  2618. return 0;
  2619. }
  2620. if (os_strcmp(var, "phase1") == 0) {
  2621. os_free(cred->phase1);
  2622. cred->phase1 = val;
  2623. return 0;
  2624. }
  2625. if (os_strcmp(var, "phase2") == 0) {
  2626. os_free(cred->phase2);
  2627. cred->phase2 = val;
  2628. return 0;
  2629. }
  2630. if (os_strcmp(var, "roaming_consortium") == 0) {
  2631. if (len < 3 || len > sizeof(cred->roaming_consortium)) {
  2632. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2633. "roaming_consortium length %d (3..15 "
  2634. "expected)", line, (int) len);
  2635. os_free(val);
  2636. return -1;
  2637. }
  2638. os_memcpy(cred->roaming_consortium, val, len);
  2639. cred->roaming_consortium_len = len;
  2640. os_free(val);
  2641. return 0;
  2642. }
  2643. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  2644. if (len < 3 || len > sizeof(cred->required_roaming_consortium))
  2645. {
  2646. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2647. "required_roaming_consortium length %d "
  2648. "(3..15 expected)", line, (int) len);
  2649. os_free(val);
  2650. return -1;
  2651. }
  2652. os_memcpy(cred->required_roaming_consortium, val, len);
  2653. cred->required_roaming_consortium_len = len;
  2654. os_free(val);
  2655. return 0;
  2656. }
  2657. if (os_strcmp(var, "excluded_ssid") == 0) {
  2658. struct excluded_ssid *e;
  2659. if (len > SSID_MAX_LEN) {
  2660. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2661. "excluded_ssid length %d", line, (int) len);
  2662. os_free(val);
  2663. return -1;
  2664. }
  2665. e = os_realloc_array(cred->excluded_ssid,
  2666. cred->num_excluded_ssid + 1,
  2667. sizeof(struct excluded_ssid));
  2668. if (e == NULL) {
  2669. os_free(val);
  2670. return -1;
  2671. }
  2672. cred->excluded_ssid = e;
  2673. e = &cred->excluded_ssid[cred->num_excluded_ssid++];
  2674. os_memcpy(e->ssid, val, len);
  2675. e->ssid_len = len;
  2676. os_free(val);
  2677. return 0;
  2678. }
  2679. if (os_strcmp(var, "roaming_partner") == 0) {
  2680. struct roaming_partner *p;
  2681. char *pos;
  2682. p = os_realloc_array(cred->roaming_partner,
  2683. cred->num_roaming_partner + 1,
  2684. sizeof(struct roaming_partner));
  2685. if (p == NULL) {
  2686. os_free(val);
  2687. return -1;
  2688. }
  2689. cred->roaming_partner = p;
  2690. p = &cred->roaming_partner[cred->num_roaming_partner];
  2691. pos = os_strchr(val, ',');
  2692. if (pos == NULL) {
  2693. os_free(val);
  2694. return -1;
  2695. }
  2696. *pos++ = '\0';
  2697. if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
  2698. os_free(val);
  2699. return -1;
  2700. }
  2701. os_memcpy(p->fqdn, val, pos - val);
  2702. p->exact_match = atoi(pos);
  2703. pos = os_strchr(pos, ',');
  2704. if (pos == NULL) {
  2705. os_free(val);
  2706. return -1;
  2707. }
  2708. *pos++ = '\0';
  2709. p->priority = atoi(pos);
  2710. pos = os_strchr(pos, ',');
  2711. if (pos == NULL) {
  2712. os_free(val);
  2713. return -1;
  2714. }
  2715. *pos++ = '\0';
  2716. if (os_strlen(pos) >= sizeof(p->country)) {
  2717. os_free(val);
  2718. return -1;
  2719. }
  2720. os_memcpy(p->country, pos, os_strlen(pos) + 1);
  2721. cred->num_roaming_partner++;
  2722. os_free(val);
  2723. return 0;
  2724. }
  2725. if (os_strcmp(var, "provisioning_sp") == 0) {
  2726. os_free(cred->provisioning_sp);
  2727. cred->provisioning_sp = val;
  2728. return 0;
  2729. }
  2730. if (line) {
  2731. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  2732. line, var);
  2733. }
  2734. os_free(val);
  2735. return -1;
  2736. }
  2737. static char * alloc_int_str(int val)
  2738. {
  2739. const unsigned int bufsize = 20;
  2740. char *buf;
  2741. int res;
  2742. buf = os_malloc(bufsize);
  2743. if (buf == NULL)
  2744. return NULL;
  2745. res = os_snprintf(buf, bufsize, "%d", val);
  2746. if (os_snprintf_error(bufsize, res)) {
  2747. os_free(buf);
  2748. buf = NULL;
  2749. }
  2750. return buf;
  2751. }
  2752. static char * alloc_strdup(const char *str)
  2753. {
  2754. if (str == NULL)
  2755. return NULL;
  2756. return os_strdup(str);
  2757. }
  2758. char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
  2759. {
  2760. if (os_strcmp(var, "temporary") == 0)
  2761. return alloc_int_str(cred->temporary);
  2762. if (os_strcmp(var, "priority") == 0)
  2763. return alloc_int_str(cred->priority);
  2764. if (os_strcmp(var, "sp_priority") == 0)
  2765. return alloc_int_str(cred->sp_priority);
  2766. if (os_strcmp(var, "pcsc") == 0)
  2767. return alloc_int_str(cred->pcsc);
  2768. if (os_strcmp(var, "eap") == 0) {
  2769. if (!cred->eap_method)
  2770. return NULL;
  2771. return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
  2772. cred->eap_method[0].method));
  2773. }
  2774. if (os_strcmp(var, "update_identifier") == 0)
  2775. return alloc_int_str(cred->update_identifier);
  2776. if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
  2777. return alloc_int_str(cred->min_dl_bandwidth_home);
  2778. if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
  2779. return alloc_int_str(cred->min_ul_bandwidth_home);
  2780. if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
  2781. return alloc_int_str(cred->min_dl_bandwidth_roaming);
  2782. if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
  2783. return alloc_int_str(cred->min_ul_bandwidth_roaming);
  2784. if (os_strcmp(var, "max_bss_load") == 0)
  2785. return alloc_int_str(cred->max_bss_load);
  2786. if (os_strcmp(var, "req_conn_capab") == 0) {
  2787. unsigned int i;
  2788. char *buf, *end, *pos;
  2789. int ret;
  2790. if (!cred->num_req_conn_capab)
  2791. return NULL;
  2792. buf = os_malloc(4000);
  2793. if (buf == NULL)
  2794. return NULL;
  2795. pos = buf;
  2796. end = pos + 4000;
  2797. for (i = 0; i < cred->num_req_conn_capab; i++) {
  2798. int *ports;
  2799. ret = os_snprintf(pos, end - pos, "%s%u",
  2800. i > 0 ? "\n" : "",
  2801. cred->req_conn_capab_proto[i]);
  2802. if (os_snprintf_error(end - pos, ret))
  2803. return buf;
  2804. pos += ret;
  2805. ports = cred->req_conn_capab_port[i];
  2806. if (ports) {
  2807. int j;
  2808. for (j = 0; ports[j] != -1; j++) {
  2809. ret = os_snprintf(pos, end - pos,
  2810. "%s%d",
  2811. j > 0 ? "," : ":",
  2812. ports[j]);
  2813. if (os_snprintf_error(end - pos, ret))
  2814. return buf;
  2815. pos += ret;
  2816. }
  2817. }
  2818. }
  2819. return buf;
  2820. }
  2821. if (os_strcmp(var, "ocsp") == 0)
  2822. return alloc_int_str(cred->ocsp);
  2823. if (os_strcmp(var, "realm") == 0)
  2824. return alloc_strdup(cred->realm);
  2825. if (os_strcmp(var, "username") == 0)
  2826. return alloc_strdup(cred->username);
  2827. if (os_strcmp(var, "password") == 0) {
  2828. if (!cred->password)
  2829. return NULL;
  2830. return alloc_strdup("*");
  2831. }
  2832. if (os_strcmp(var, "ca_cert") == 0)
  2833. return alloc_strdup(cred->ca_cert);
  2834. if (os_strcmp(var, "client_cert") == 0)
  2835. return alloc_strdup(cred->client_cert);
  2836. if (os_strcmp(var, "private_key") == 0)
  2837. return alloc_strdup(cred->private_key);
  2838. if (os_strcmp(var, "private_key_passwd") == 0) {
  2839. if (!cred->private_key_passwd)
  2840. return NULL;
  2841. return alloc_strdup("*");
  2842. }
  2843. if (os_strcmp(var, "imsi") == 0)
  2844. return alloc_strdup(cred->imsi);
  2845. if (os_strcmp(var, "milenage") == 0) {
  2846. if (!(cred->milenage))
  2847. return NULL;
  2848. return alloc_strdup("*");
  2849. }
  2850. if (os_strcmp(var, "domain_suffix_match") == 0)
  2851. return alloc_strdup(cred->domain_suffix_match);
  2852. if (os_strcmp(var, "domain") == 0) {
  2853. unsigned int i;
  2854. char *buf, *end, *pos;
  2855. int ret;
  2856. if (!cred->num_domain)
  2857. return NULL;
  2858. buf = os_malloc(4000);
  2859. if (buf == NULL)
  2860. return NULL;
  2861. pos = buf;
  2862. end = pos + 4000;
  2863. for (i = 0; i < cred->num_domain; i++) {
  2864. ret = os_snprintf(pos, end - pos, "%s%s",
  2865. i > 0 ? "\n" : "", cred->domain[i]);
  2866. if (os_snprintf_error(end - pos, ret))
  2867. return buf;
  2868. pos += ret;
  2869. }
  2870. return buf;
  2871. }
  2872. if (os_strcmp(var, "phase1") == 0)
  2873. return alloc_strdup(cred->phase1);
  2874. if (os_strcmp(var, "phase2") == 0)
  2875. return alloc_strdup(cred->phase2);
  2876. if (os_strcmp(var, "roaming_consortium") == 0) {
  2877. size_t buflen;
  2878. char *buf;
  2879. if (!cred->roaming_consortium_len)
  2880. return NULL;
  2881. buflen = cred->roaming_consortium_len * 2 + 1;
  2882. buf = os_malloc(buflen);
  2883. if (buf == NULL)
  2884. return NULL;
  2885. wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
  2886. cred->roaming_consortium_len);
  2887. return buf;
  2888. }
  2889. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  2890. size_t buflen;
  2891. char *buf;
  2892. if (!cred->required_roaming_consortium_len)
  2893. return NULL;
  2894. buflen = cred->required_roaming_consortium_len * 2 + 1;
  2895. buf = os_malloc(buflen);
  2896. if (buf == NULL)
  2897. return NULL;
  2898. wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
  2899. cred->required_roaming_consortium_len);
  2900. return buf;
  2901. }
  2902. if (os_strcmp(var, "excluded_ssid") == 0) {
  2903. unsigned int i;
  2904. char *buf, *end, *pos;
  2905. if (!cred->num_excluded_ssid)
  2906. return NULL;
  2907. buf = os_malloc(4000);
  2908. if (buf == NULL)
  2909. return NULL;
  2910. pos = buf;
  2911. end = pos + 4000;
  2912. for (i = 0; i < cred->num_excluded_ssid; i++) {
  2913. struct excluded_ssid *e;
  2914. int ret;
  2915. e = &cred->excluded_ssid[i];
  2916. ret = os_snprintf(pos, end - pos, "%s%s",
  2917. i > 0 ? "\n" : "",
  2918. wpa_ssid_txt(e->ssid, e->ssid_len));
  2919. if (os_snprintf_error(end - pos, ret))
  2920. return buf;
  2921. pos += ret;
  2922. }
  2923. return buf;
  2924. }
  2925. if (os_strcmp(var, "roaming_partner") == 0) {
  2926. unsigned int i;
  2927. char *buf, *end, *pos;
  2928. if (!cred->num_roaming_partner)
  2929. return NULL;
  2930. buf = os_malloc(4000);
  2931. if (buf == NULL)
  2932. return NULL;
  2933. pos = buf;
  2934. end = pos + 4000;
  2935. for (i = 0; i < cred->num_roaming_partner; i++) {
  2936. struct roaming_partner *p;
  2937. int ret;
  2938. p = &cred->roaming_partner[i];
  2939. ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
  2940. i > 0 ? "\n" : "",
  2941. p->fqdn, p->exact_match, p->priority,
  2942. p->country);
  2943. if (os_snprintf_error(end - pos, ret))
  2944. return buf;
  2945. pos += ret;
  2946. }
  2947. return buf;
  2948. }
  2949. if (os_strcmp(var, "provisioning_sp") == 0)
  2950. return alloc_strdup(cred->provisioning_sp);
  2951. return NULL;
  2952. }
  2953. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  2954. {
  2955. struct wpa_cred *cred;
  2956. cred = config->cred;
  2957. while (cred) {
  2958. if (id == cred->id)
  2959. break;
  2960. cred = cred->next;
  2961. }
  2962. return cred;
  2963. }
  2964. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  2965. {
  2966. int id;
  2967. struct wpa_cred *cred, *last = NULL;
  2968. id = -1;
  2969. cred = config->cred;
  2970. while (cred) {
  2971. if (cred->id > id)
  2972. id = cred->id;
  2973. last = cred;
  2974. cred = cred->next;
  2975. }
  2976. id++;
  2977. cred = os_zalloc(sizeof(*cred));
  2978. if (cred == NULL)
  2979. return NULL;
  2980. cred->id = id;
  2981. cred->sim_num = DEFAULT_USER_SELECTED_SIM;
  2982. if (last)
  2983. last->next = cred;
  2984. else
  2985. config->cred = cred;
  2986. return cred;
  2987. }
  2988. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2989. {
  2990. struct wpa_cred *cred, *prev = NULL;
  2991. cred = config->cred;
  2992. while (cred) {
  2993. if (id == cred->id)
  2994. break;
  2995. prev = cred;
  2996. cred = cred->next;
  2997. }
  2998. if (cred == NULL)
  2999. return -1;
  3000. if (prev)
  3001. prev->next = cred->next;
  3002. else
  3003. config->cred = cred->next;
  3004. wpa_config_free_cred(cred);
  3005. return 0;
  3006. }
  3007. #ifndef CONFIG_NO_CONFIG_BLOBS
  3008. /**
  3009. * wpa_config_get_blob - Get a named configuration blob
  3010. * @config: Configuration data from wpa_config_read()
  3011. * @name: Name of the blob
  3012. * Returns: Pointer to blob data or %NULL if not found
  3013. */
  3014. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  3015. const char *name)
  3016. {
  3017. struct wpa_config_blob *blob = config->blobs;
  3018. while (blob) {
  3019. if (os_strcmp(blob->name, name) == 0)
  3020. return blob;
  3021. blob = blob->next;
  3022. }
  3023. return NULL;
  3024. }
  3025. /**
  3026. * wpa_config_set_blob - Set or add a named configuration blob
  3027. * @config: Configuration data from wpa_config_read()
  3028. * @blob: New value for the blob
  3029. *
  3030. * Adds a new configuration blob or replaces the current value of an existing
  3031. * blob.
  3032. */
  3033. void wpa_config_set_blob(struct wpa_config *config,
  3034. struct wpa_config_blob *blob)
  3035. {
  3036. wpa_config_remove_blob(config, blob->name);
  3037. blob->next = config->blobs;
  3038. config->blobs = blob;
  3039. }
  3040. /**
  3041. * wpa_config_free_blob - Free blob data
  3042. * @blob: Pointer to blob to be freed
  3043. */
  3044. void wpa_config_free_blob(struct wpa_config_blob *blob)
  3045. {
  3046. if (blob) {
  3047. os_free(blob->name);
  3048. bin_clear_free(blob->data, blob->len);
  3049. os_free(blob);
  3050. }
  3051. }
  3052. /**
  3053. * wpa_config_remove_blob - Remove a named configuration blob
  3054. * @config: Configuration data from wpa_config_read()
  3055. * @name: Name of the blob to remove
  3056. * Returns: 0 if blob was removed or -1 if blob was not found
  3057. */
  3058. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  3059. {
  3060. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  3061. while (pos) {
  3062. if (os_strcmp(pos->name, name) == 0) {
  3063. if (prev)
  3064. prev->next = pos->next;
  3065. else
  3066. config->blobs = pos->next;
  3067. wpa_config_free_blob(pos);
  3068. return 0;
  3069. }
  3070. prev = pos;
  3071. pos = pos->next;
  3072. }
  3073. return -1;
  3074. }
  3075. #endif /* CONFIG_NO_CONFIG_BLOBS */
  3076. /**
  3077. * wpa_config_alloc_empty - Allocate an empty configuration
  3078. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  3079. * socket
  3080. * @driver_param: Driver parameters
  3081. * Returns: Pointer to allocated configuration data or %NULL on failure
  3082. */
  3083. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  3084. const char *driver_param)
  3085. {
  3086. struct wpa_config *config;
  3087. const int aCWmin = 4, aCWmax = 10;
  3088. const struct hostapd_wmm_ac_params ac_bk =
  3089. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  3090. const struct hostapd_wmm_ac_params ac_be =
  3091. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  3092. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  3093. { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
  3094. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  3095. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
  3096. config = os_zalloc(sizeof(*config));
  3097. if (config == NULL)
  3098. return NULL;
  3099. config->eapol_version = DEFAULT_EAPOL_VERSION;
  3100. config->ap_scan = DEFAULT_AP_SCAN;
  3101. config->user_mpm = DEFAULT_USER_MPM;
  3102. config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
  3103. config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
  3104. config->dot11RSNASAERetransPeriod =
  3105. DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
  3106. config->fast_reauth = DEFAULT_FAST_REAUTH;
  3107. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  3108. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  3109. config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
  3110. config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
  3111. config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
  3112. config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
  3113. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  3114. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  3115. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  3116. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  3117. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  3118. config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
  3119. config->wmm_ac_params[0] = ac_be;
  3120. config->wmm_ac_params[1] = ac_bk;
  3121. config->wmm_ac_params[2] = ac_vi;
  3122. config->wmm_ac_params[3] = ac_vo;
  3123. config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
  3124. config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
  3125. config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
  3126. config->cert_in_cb = DEFAULT_CERT_IN_CB;
  3127. config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
  3128. #ifdef CONFIG_MBO
  3129. config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
  3130. #endif /* CONFIG_MBO */
  3131. if (ctrl_interface)
  3132. config->ctrl_interface = os_strdup(ctrl_interface);
  3133. if (driver_param)
  3134. config->driver_param = os_strdup(driver_param);
  3135. return config;
  3136. }
  3137. #ifndef CONFIG_NO_STDOUT_DEBUG
  3138. /**
  3139. * wpa_config_debug_dump_networks - Debug dump of configured networks
  3140. * @config: Configuration data from wpa_config_read()
  3141. */
  3142. void wpa_config_debug_dump_networks(struct wpa_config *config)
  3143. {
  3144. int prio;
  3145. struct wpa_ssid *ssid;
  3146. for (prio = 0; prio < config->num_prio; prio++) {
  3147. ssid = config->pssid[prio];
  3148. wpa_printf(MSG_DEBUG, "Priority group %d",
  3149. ssid->priority);
  3150. while (ssid) {
  3151. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  3152. ssid->id,
  3153. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  3154. ssid = ssid->pnext;
  3155. }
  3156. }
  3157. }
  3158. #endif /* CONFIG_NO_STDOUT_DEBUG */
  3159. struct global_parse_data {
  3160. char *name;
  3161. int (*parser)(const struct global_parse_data *data,
  3162. struct wpa_config *config, int line, const char *value);
  3163. int (*get)(const char *name, struct wpa_config *config, long offset,
  3164. char *buf, size_t buflen, int pretty_print);
  3165. void *param1, *param2, *param3;
  3166. unsigned int changed_flag;
  3167. };
  3168. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  3169. struct wpa_config *config, int line,
  3170. const char *pos)
  3171. {
  3172. int val, *dst;
  3173. char *end;
  3174. dst = (int *) (((u8 *) config) + (long) data->param1);
  3175. val = strtol(pos, &end, 0);
  3176. if (*end) {
  3177. wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
  3178. line, pos);
  3179. return -1;
  3180. }
  3181. *dst = val;
  3182. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  3183. if (data->param2 && *dst < (long) data->param2) {
  3184. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  3185. "min_value=%ld)", line, data->name, *dst,
  3186. (long) data->param2);
  3187. *dst = (long) data->param2;
  3188. return -1;
  3189. }
  3190. if (data->param3 && *dst > (long) data->param3) {
  3191. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  3192. "max_value=%ld)", line, data->name, *dst,
  3193. (long) data->param3);
  3194. *dst = (long) data->param3;
  3195. return -1;
  3196. }
  3197. return 0;
  3198. }
  3199. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  3200. struct wpa_config *config, int line,
  3201. const char *pos)
  3202. {
  3203. size_t len;
  3204. char **dst, *tmp;
  3205. len = os_strlen(pos);
  3206. if (data->param2 && len < (size_t) data->param2) {
  3207. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  3208. "min_len=%ld)", line, data->name,
  3209. (unsigned long) len, (long) data->param2);
  3210. return -1;
  3211. }
  3212. if (data->param3 && len > (size_t) data->param3) {
  3213. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  3214. "max_len=%ld)", line, data->name,
  3215. (unsigned long) len, (long) data->param3);
  3216. return -1;
  3217. }
  3218. if (has_newline(pos)) {
  3219. wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
  3220. line, data->name);
  3221. return -1;
  3222. }
  3223. tmp = os_strdup(pos);
  3224. if (tmp == NULL)
  3225. return -1;
  3226. dst = (char **) (((u8 *) config) + (long) data->param1);
  3227. os_free(*dst);
  3228. *dst = tmp;
  3229. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  3230. return 0;
  3231. }
  3232. static int wpa_config_process_bgscan(const struct global_parse_data *data,
  3233. struct wpa_config *config, int line,
  3234. const char *pos)
  3235. {
  3236. size_t len;
  3237. char *tmp;
  3238. int res;
  3239. tmp = wpa_config_parse_string(pos, &len);
  3240. if (tmp == NULL) {
  3241. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
  3242. line, data->name);
  3243. return -1;
  3244. }
  3245. res = wpa_global_config_parse_str(data, config, line, tmp);
  3246. os_free(tmp);
  3247. return res;
  3248. }
  3249. static int wpa_global_config_parse_bin(const struct global_parse_data *data,
  3250. struct wpa_config *config, int line,
  3251. const char *pos)
  3252. {
  3253. struct wpabuf **dst, *tmp;
  3254. tmp = wpabuf_parse_bin(pos);
  3255. if (!tmp)
  3256. return -1;
  3257. dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
  3258. wpabuf_free(*dst);
  3259. *dst = tmp;
  3260. wpa_printf(MSG_DEBUG, "%s", data->name);
  3261. return 0;
  3262. }
  3263. static int wpa_config_process_freq_list(const struct global_parse_data *data,
  3264. struct wpa_config *config, int line,
  3265. const char *value)
  3266. {
  3267. int *freqs;
  3268. freqs = wpa_config_parse_int_array(value);
  3269. if (freqs == NULL)
  3270. return -1;
  3271. if (freqs[0] == 0) {
  3272. os_free(freqs);
  3273. freqs = NULL;
  3274. }
  3275. os_free(config->freq_list);
  3276. config->freq_list = freqs;
  3277. return 0;
  3278. }
  3279. #ifdef CONFIG_P2P
  3280. static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
  3281. struct wpa_config *config, int line,
  3282. const char *pos)
  3283. {
  3284. u32 *dst;
  3285. struct hostapd_ip_addr addr;
  3286. if (hostapd_parse_ip_addr(pos, &addr) < 0)
  3287. return -1;
  3288. if (addr.af != AF_INET)
  3289. return -1;
  3290. dst = (u32 *) (((u8 *) config) + (long) data->param1);
  3291. os_memcpy(dst, &addr.u.v4.s_addr, 4);
  3292. wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
  3293. WPA_GET_BE32((u8 *) dst));
  3294. return 0;
  3295. }
  3296. #endif /* CONFIG_P2P */
  3297. static int wpa_config_process_country(const struct global_parse_data *data,
  3298. struct wpa_config *config, int line,
  3299. const char *pos)
  3300. {
  3301. if (!pos[0] || !pos[1]) {
  3302. wpa_printf(MSG_DEBUG, "Invalid country set");
  3303. return -1;
  3304. }
  3305. config->country[0] = pos[0];
  3306. config->country[1] = pos[1];
  3307. wpa_printf(MSG_DEBUG, "country='%c%c'",
  3308. config->country[0], config->country[1]);
  3309. return 0;
  3310. }
  3311. static int wpa_config_process_load_dynamic_eap(
  3312. const struct global_parse_data *data, struct wpa_config *config,
  3313. int line, const char *so)
  3314. {
  3315. int ret;
  3316. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  3317. ret = eap_peer_method_load(so);
  3318. if (ret == -2) {
  3319. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  3320. "reloading.");
  3321. } else if (ret) {
  3322. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  3323. "method '%s'.", line, so);
  3324. return -1;
  3325. }
  3326. return 0;
  3327. }
  3328. #ifdef CONFIG_WPS
  3329. static int wpa_config_process_uuid(const struct global_parse_data *data,
  3330. struct wpa_config *config, int line,
  3331. const char *pos)
  3332. {
  3333. char buf[40];
  3334. if (uuid_str2bin(pos, config->uuid)) {
  3335. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  3336. return -1;
  3337. }
  3338. uuid_bin2str(config->uuid, buf, sizeof(buf));
  3339. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  3340. return 0;
  3341. }
  3342. static int wpa_config_process_device_type(
  3343. const struct global_parse_data *data,
  3344. struct wpa_config *config, int line, const char *pos)
  3345. {
  3346. return wps_dev_type_str2bin(pos, config->device_type);
  3347. }
  3348. static int wpa_config_process_os_version(const struct global_parse_data *data,
  3349. struct wpa_config *config, int line,
  3350. const char *pos)
  3351. {
  3352. if (hexstr2bin(pos, config->os_version, 4)) {
  3353. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  3354. return -1;
  3355. }
  3356. wpa_printf(MSG_DEBUG, "os_version=%08x",
  3357. WPA_GET_BE32(config->os_version));
  3358. return 0;
  3359. }
  3360. static int wpa_config_process_wps_vendor_ext_m1(
  3361. const struct global_parse_data *data,
  3362. struct wpa_config *config, int line, const char *pos)
  3363. {
  3364. struct wpabuf *tmp;
  3365. int len = os_strlen(pos) / 2;
  3366. u8 *p;
  3367. if (!len) {
  3368. wpa_printf(MSG_ERROR, "Line %d: "
  3369. "invalid wps_vendor_ext_m1", line);
  3370. return -1;
  3371. }
  3372. tmp = wpabuf_alloc(len);
  3373. if (tmp) {
  3374. p = wpabuf_put(tmp, len);
  3375. if (hexstr2bin(pos, p, len)) {
  3376. wpa_printf(MSG_ERROR, "Line %d: "
  3377. "invalid wps_vendor_ext_m1", line);
  3378. wpabuf_free(tmp);
  3379. return -1;
  3380. }
  3381. wpabuf_free(config->wps_vendor_ext_m1);
  3382. config->wps_vendor_ext_m1 = tmp;
  3383. } else {
  3384. wpa_printf(MSG_ERROR, "Can not allocate "
  3385. "memory for wps_vendor_ext_m1");
  3386. return -1;
  3387. }
  3388. return 0;
  3389. }
  3390. #endif /* CONFIG_WPS */
  3391. #ifdef CONFIG_P2P
  3392. static int wpa_config_process_sec_device_type(
  3393. const struct global_parse_data *data,
  3394. struct wpa_config *config, int line, const char *pos)
  3395. {
  3396. int idx;
  3397. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  3398. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  3399. "items", line);
  3400. return -1;
  3401. }
  3402. idx = config->num_sec_device_types;
  3403. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  3404. return -1;
  3405. config->num_sec_device_types++;
  3406. return 0;
  3407. }
  3408. static int wpa_config_process_p2p_pref_chan(
  3409. const struct global_parse_data *data,
  3410. struct wpa_config *config, int line, const char *pos)
  3411. {
  3412. struct p2p_channel *pref = NULL, *n;
  3413. unsigned int num = 0;
  3414. const char *pos2;
  3415. u8 op_class, chan;
  3416. /* format: class:chan,class:chan,... */
  3417. while (*pos) {
  3418. op_class = atoi(pos);
  3419. pos2 = os_strchr(pos, ':');
  3420. if (pos2 == NULL)
  3421. goto fail;
  3422. pos2++;
  3423. chan = atoi(pos2);
  3424. n = os_realloc_array(pref, num + 1,
  3425. sizeof(struct p2p_channel));
  3426. if (n == NULL)
  3427. goto fail;
  3428. pref = n;
  3429. pref[num].op_class = op_class;
  3430. pref[num].chan = chan;
  3431. num++;
  3432. pos = os_strchr(pos2, ',');
  3433. if (pos == NULL)
  3434. break;
  3435. pos++;
  3436. }
  3437. os_free(config->p2p_pref_chan);
  3438. config->p2p_pref_chan = pref;
  3439. config->num_p2p_pref_chan = num;
  3440. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  3441. (u8 *) config->p2p_pref_chan,
  3442. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  3443. return 0;
  3444. fail:
  3445. os_free(pref);
  3446. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  3447. return -1;
  3448. }
  3449. static int wpa_config_process_p2p_no_go_freq(
  3450. const struct global_parse_data *data,
  3451. struct wpa_config *config, int line, const char *pos)
  3452. {
  3453. int ret;
  3454. ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
  3455. if (ret < 0) {
  3456. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
  3457. return -1;
  3458. }
  3459. wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
  3460. config->p2p_no_go_freq.num);
  3461. return 0;
  3462. }
  3463. #endif /* CONFIG_P2P */
  3464. static int wpa_config_process_hessid(
  3465. const struct global_parse_data *data,
  3466. struct wpa_config *config, int line, const char *pos)
  3467. {
  3468. if (hwaddr_aton2(pos, config->hessid) < 0) {
  3469. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  3470. line, pos);
  3471. return -1;
  3472. }
  3473. return 0;
  3474. }
  3475. static int wpa_config_process_sae_groups(
  3476. const struct global_parse_data *data,
  3477. struct wpa_config *config, int line, const char *pos)
  3478. {
  3479. int *groups = wpa_config_parse_int_array(pos);
  3480. if (groups == NULL) {
  3481. wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
  3482. line, pos);
  3483. return -1;
  3484. }
  3485. os_free(config->sae_groups);
  3486. config->sae_groups = groups;
  3487. return 0;
  3488. }
  3489. static int wpa_config_process_ap_vendor_elements(
  3490. const struct global_parse_data *data,
  3491. struct wpa_config *config, int line, const char *pos)
  3492. {
  3493. struct wpabuf *tmp;
  3494. int len = os_strlen(pos) / 2;
  3495. u8 *p;
  3496. if (!len) {
  3497. wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
  3498. line);
  3499. return -1;
  3500. }
  3501. tmp = wpabuf_alloc(len);
  3502. if (tmp) {
  3503. p = wpabuf_put(tmp, len);
  3504. if (hexstr2bin(pos, p, len)) {
  3505. wpa_printf(MSG_ERROR, "Line %d: invalid "
  3506. "ap_vendor_elements", line);
  3507. wpabuf_free(tmp);
  3508. return -1;
  3509. }
  3510. wpabuf_free(config->ap_vendor_elements);
  3511. config->ap_vendor_elements = tmp;
  3512. } else {
  3513. wpa_printf(MSG_ERROR, "Cannot allocate memory for "
  3514. "ap_vendor_elements");
  3515. return -1;
  3516. }
  3517. return 0;
  3518. }
  3519. #ifdef CONFIG_CTRL_IFACE
  3520. static int wpa_config_process_no_ctrl_interface(
  3521. const struct global_parse_data *data,
  3522. struct wpa_config *config, int line, const char *pos)
  3523. {
  3524. wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
  3525. os_free(config->ctrl_interface);
  3526. config->ctrl_interface = NULL;
  3527. return 0;
  3528. }
  3529. #endif /* CONFIG_CTRL_IFACE */
  3530. static int wpa_config_get_int(const char *name, struct wpa_config *config,
  3531. long offset, char *buf, size_t buflen,
  3532. int pretty_print)
  3533. {
  3534. int *val = (int *) (((u8 *) config) + (long) offset);
  3535. if (pretty_print)
  3536. return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
  3537. return os_snprintf(buf, buflen, "%d", *val);
  3538. }
  3539. static int wpa_config_get_str(const char *name, struct wpa_config *config,
  3540. long offset, char *buf, size_t buflen,
  3541. int pretty_print)
  3542. {
  3543. char **val = (char **) (((u8 *) config) + (long) offset);
  3544. int res;
  3545. if (pretty_print)
  3546. res = os_snprintf(buf, buflen, "%s=%s\n", name,
  3547. *val ? *val : "null");
  3548. else if (!*val)
  3549. return -1;
  3550. else
  3551. res = os_snprintf(buf, buflen, "%s", *val);
  3552. if (os_snprintf_error(buflen, res))
  3553. res = -1;
  3554. return res;
  3555. }
  3556. #ifdef CONFIG_P2P
  3557. static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
  3558. long offset, char *buf, size_t buflen,
  3559. int pretty_print)
  3560. {
  3561. void *val = ((u8 *) config) + (long) offset;
  3562. int res;
  3563. char addr[INET_ADDRSTRLEN];
  3564. if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
  3565. return -1;
  3566. if (pretty_print)
  3567. res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
  3568. else
  3569. res = os_snprintf(buf, buflen, "%s", addr);
  3570. if (os_snprintf_error(buflen, res))
  3571. res = -1;
  3572. return res;
  3573. }
  3574. #endif /* CONFIG_P2P */
  3575. #ifdef OFFSET
  3576. #undef OFFSET
  3577. #endif /* OFFSET */
  3578. /* OFFSET: Get offset of a variable within the wpa_config structure */
  3579. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  3580. #define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
  3581. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
  3582. #define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
  3583. #define INT(f) _INT(f), NULL, NULL
  3584. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  3585. #define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
  3586. #define STR(f) _STR(f), NULL, NULL
  3587. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  3588. #define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
  3589. #define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
  3590. OFFSET(f), NULL, NULL
  3591. static const struct global_parse_data global_fields[] = {
  3592. #ifdef CONFIG_CTRL_IFACE
  3593. { STR(ctrl_interface), 0 },
  3594. { FUNC_NO_VAR(no_ctrl_interface), 0 },
  3595. { STR(ctrl_interface_group), 0 } /* deprecated */,
  3596. #endif /* CONFIG_CTRL_IFACE */
  3597. #ifdef CONFIG_MACSEC
  3598. { INT_RANGE(eapol_version, 1, 3), 0 },
  3599. #else /* CONFIG_MACSEC */
  3600. { INT_RANGE(eapol_version, 1, 2), 0 },
  3601. #endif /* CONFIG_MACSEC */
  3602. { INT(ap_scan), 0 },
  3603. { FUNC(bgscan), 0 },
  3604. #ifdef CONFIG_MESH
  3605. { INT(user_mpm), 0 },
  3606. { INT_RANGE(max_peer_links, 0, 255), 0 },
  3607. { INT(mesh_max_inactivity), 0 },
  3608. { INT(dot11RSNASAERetransPeriod), 0 },
  3609. #endif /* CONFIG_MESH */
  3610. { INT(disable_scan_offload), 0 },
  3611. { INT(fast_reauth), 0 },
  3612. { STR(opensc_engine_path), 0 },
  3613. { STR(pkcs11_engine_path), 0 },
  3614. { STR(pkcs11_module_path), 0 },
  3615. { STR(openssl_ciphers), 0 },
  3616. { STR(pcsc_reader), 0 },
  3617. { STR(pcsc_pin), 0 },
  3618. { INT(external_sim), 0 },
  3619. { STR(driver_param), 0 },
  3620. { INT(dot11RSNAConfigPMKLifetime), 0 },
  3621. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  3622. { INT(dot11RSNAConfigSATimeout), 0 },
  3623. #ifndef CONFIG_NO_CONFIG_WRITE
  3624. { INT(update_config), 0 },
  3625. #endif /* CONFIG_NO_CONFIG_WRITE */
  3626. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  3627. #ifdef CONFIG_WPS
  3628. { FUNC(uuid), CFG_CHANGED_UUID },
  3629. { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
  3630. CFG_CHANGED_DEVICE_NAME },
  3631. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  3632. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  3633. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  3634. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  3635. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  3636. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  3637. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  3638. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  3639. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  3640. #endif /* CONFIG_WPS */
  3641. #ifdef CONFIG_P2P
  3642. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  3643. { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
  3644. { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
  3645. { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
  3646. { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
  3647. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  3648. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  3649. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  3650. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  3651. { INT(p2p_group_idle), 0 },
  3652. { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
  3653. { INT_RANGE(p2p_passphrase_len, 8, 63),
  3654. CFG_CHANGED_P2P_PASSPHRASE_LEN },
  3655. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  3656. { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
  3657. { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
  3658. { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
  3659. { INT(p2p_go_ht40), 0 },
  3660. { INT(p2p_go_vht), 0 },
  3661. { INT(p2p_disabled), 0 },
  3662. { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
  3663. { INT(p2p_no_group_iface), 0 },
  3664. { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
  3665. { IPV4(ip_addr_go), 0 },
  3666. { IPV4(ip_addr_mask), 0 },
  3667. { IPV4(ip_addr_start), 0 },
  3668. { IPV4(ip_addr_end), 0 },
  3669. { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
  3670. #endif /* CONFIG_P2P */
  3671. { FUNC(country), CFG_CHANGED_COUNTRY },
  3672. { INT(bss_max_count), 0 },
  3673. { INT(bss_expiration_age), 0 },
  3674. { INT(bss_expiration_scan_count), 0 },
  3675. { INT_RANGE(filter_ssids, 0, 1), 0 },
  3676. { INT_RANGE(filter_rssi, -100, 0), 0 },
  3677. { INT(max_num_sta), 0 },
  3678. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  3679. #ifdef CONFIG_HS20
  3680. { INT_RANGE(hs20, 0, 1), 0 },
  3681. #endif /* CONFIG_HS20 */
  3682. { INT_RANGE(interworking, 0, 1), 0 },
  3683. { FUNC(hessid), 0 },
  3684. { INT_RANGE(access_network_type, 0, 15), 0 },
  3685. { INT_RANGE(pbc_in_m1, 0, 1), 0 },
  3686. { STR(autoscan), 0 },
  3687. { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
  3688. CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3689. { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3690. { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3691. { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3692. { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
  3693. { INT(p2p_go_max_inactivity), 0 },
  3694. { INT_RANGE(auto_interworking, 0, 1), 0 },
  3695. { INT(okc), 0 },
  3696. { INT(pmf), 0 },
  3697. { FUNC(sae_groups), 0 },
  3698. { INT(dtim_period), 0 },
  3699. { INT(beacon_int), 0 },
  3700. { FUNC(ap_vendor_elements), 0 },
  3701. { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
  3702. { FUNC(freq_list), 0 },
  3703. { INT(scan_cur_freq), 0 },
  3704. { INT(sched_scan_interval), 0 },
  3705. { INT(tdls_external_control), 0},
  3706. { STR(osu_dir), 0 },
  3707. { STR(wowlan_triggers), 0 },
  3708. { INT(p2p_search_delay), 0},
  3709. { INT(mac_addr), 0 },
  3710. { INT(rand_addr_lifetime), 0 },
  3711. { INT(preassoc_mac_addr), 0 },
  3712. { INT(key_mgmt_offload), 0},
  3713. { INT(passive_scan), 0 },
  3714. { INT(reassoc_same_bss_optim), 0 },
  3715. { INT(wps_priority), 0},
  3716. #ifdef CONFIG_FST
  3717. { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
  3718. { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
  3719. { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
  3720. #endif /* CONFIG_FST */
  3721. { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
  3722. { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
  3723. #ifdef CONFIG_MBO
  3724. { STR(non_pref_chan), 0 },
  3725. { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
  3726. MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
  3727. #endif /*CONFIG_MBO */
  3728. { INT(gas_address3), 0 },
  3729. { INT_RANGE(ftm_responder, 0, 1), 0 },
  3730. { INT_RANGE(ftm_initiator, 0, 1), 0 },
  3731. };
  3732. #undef FUNC
  3733. #undef _INT
  3734. #undef INT
  3735. #undef INT_RANGE
  3736. #undef _STR
  3737. #undef STR
  3738. #undef STR_RANGE
  3739. #undef BIN
  3740. #undef IPV4
  3741. #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
  3742. int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
  3743. {
  3744. int result = 0;
  3745. size_t i;
  3746. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  3747. const struct global_parse_data *field = &global_fields[i];
  3748. int tmp;
  3749. if (!field->get)
  3750. continue;
  3751. tmp = field->get(field->name, config, (long) field->param1,
  3752. buf, buflen, 1);
  3753. if (tmp < 0)
  3754. return -1;
  3755. buf += tmp;
  3756. buflen -= tmp;
  3757. result += tmp;
  3758. }
  3759. return result;
  3760. }
  3761. int wpa_config_get_value(const char *name, struct wpa_config *config,
  3762. char *buf, size_t buflen)
  3763. {
  3764. size_t i;
  3765. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  3766. const struct global_parse_data *field = &global_fields[i];
  3767. if (os_strcmp(name, field->name) != 0)
  3768. continue;
  3769. if (!field->get)
  3770. break;
  3771. return field->get(name, config, (long) field->param1,
  3772. buf, buflen, 0);
  3773. }
  3774. return -1;
  3775. }
  3776. int wpa_config_get_num_global_field_names(void)
  3777. {
  3778. return NUM_GLOBAL_FIELDS;
  3779. }
  3780. const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
  3781. {
  3782. if (i >= NUM_GLOBAL_FIELDS)
  3783. return NULL;
  3784. if (no_var)
  3785. *no_var = !global_fields[i].param1;
  3786. return global_fields[i].name;
  3787. }
  3788. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  3789. {
  3790. size_t i;
  3791. int ret = 0;
  3792. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  3793. const struct global_parse_data *field = &global_fields[i];
  3794. size_t flen = os_strlen(field->name);
  3795. if (os_strncmp(pos, field->name, flen) != 0 ||
  3796. pos[flen] != '=')
  3797. continue;
  3798. if (field->parser(field, config, line, pos + flen + 1)) {
  3799. wpa_printf(MSG_ERROR, "Line %d: failed to "
  3800. "parse '%s'.", line, pos);
  3801. ret = -1;
  3802. }
  3803. if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
  3804. config->wps_nfc_pw_from_config = 1;
  3805. config->changed_parameters |= field->changed_flag;
  3806. break;
  3807. }
  3808. if (i == NUM_GLOBAL_FIELDS) {
  3809. #ifdef CONFIG_AP
  3810. if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
  3811. char *tmp = os_strchr(pos, '=');
  3812. if (tmp == NULL) {
  3813. if (line < 0)
  3814. return -1;
  3815. wpa_printf(MSG_ERROR, "Line %d: invalid line "
  3816. "'%s'", line, pos);
  3817. return -1;
  3818. }
  3819. *tmp++ = '\0';
  3820. if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
  3821. tmp)) {
  3822. wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
  3823. "AC item", line);
  3824. return -1;
  3825. }
  3826. }
  3827. #endif /* CONFIG_AP */
  3828. if (line < 0)
  3829. return -1;
  3830. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  3831. line, pos);
  3832. ret = -1;
  3833. }
  3834. return ret;
  3835. }