config.c 109 KB

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