config.c 106 KB

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