config.c 111 KB

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