config.c 103 KB

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