config.c 114 KB

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