config.c 107 KB

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