config.c 109 KB

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