config.c 101 KB

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