config.c 112 KB

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