config.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217
  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. #ifdef CONFIG_MESH
  1616. { FUNC(mesh_basic_rates) },
  1617. { INT(dot11MeshMaxRetries) },
  1618. { INT(dot11MeshRetryTimeout) },
  1619. { INT(dot11MeshConfirmTimeout) },
  1620. { INT(dot11MeshHoldingTimeout) },
  1621. #endif /* CONFIG_MESH */
  1622. { INT(wpa_ptk_rekey) },
  1623. { STR(bgscan) },
  1624. { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
  1625. #ifdef CONFIG_P2P
  1626. { FUNC(go_p2p_dev_addr) },
  1627. { FUNC(p2p_client_list) },
  1628. { FUNC(psk_list) },
  1629. #endif /* CONFIG_P2P */
  1630. #ifdef CONFIG_HT_OVERRIDES
  1631. { INT_RANGE(disable_ht, 0, 1) },
  1632. { INT_RANGE(disable_ht40, -1, 1) },
  1633. { INT_RANGE(disable_sgi, 0, 1) },
  1634. { INT_RANGE(disable_ldpc, 0, 1) },
  1635. { INT_RANGE(ht40_intolerant, 0, 1) },
  1636. { INT_RANGE(disable_max_amsdu, -1, 1) },
  1637. { INT_RANGE(ampdu_factor, -1, 3) },
  1638. { INT_RANGE(ampdu_density, -1, 7) },
  1639. { STR(ht_mcs) },
  1640. #endif /* CONFIG_HT_OVERRIDES */
  1641. #ifdef CONFIG_VHT_OVERRIDES
  1642. { INT_RANGE(disable_vht, 0, 1) },
  1643. { INT(vht_capa) },
  1644. { INT(vht_capa_mask) },
  1645. { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
  1646. { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
  1647. { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
  1648. { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
  1649. { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
  1650. { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
  1651. { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
  1652. { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
  1653. { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
  1654. { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
  1655. { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
  1656. { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
  1657. { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
  1658. { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
  1659. { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
  1660. { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
  1661. #endif /* CONFIG_VHT_OVERRIDES */
  1662. { INT(ap_max_inactivity) },
  1663. { INT(dtim_period) },
  1664. { INT(beacon_int) },
  1665. #ifdef CONFIG_MACSEC
  1666. { INT_RANGE(macsec_policy, 0, 1) },
  1667. #endif /* CONFIG_MACSEC */
  1668. #ifdef CONFIG_HS20
  1669. { INT(update_identifier) },
  1670. #endif /* CONFIG_HS20 */
  1671. { INT_RANGE(mac_addr, 0, 2) },
  1672. };
  1673. #undef OFFSET
  1674. #undef _STR
  1675. #undef STR
  1676. #undef STR_KEY
  1677. #undef _STR_LEN
  1678. #undef STR_LEN
  1679. #undef STR_LEN_KEY
  1680. #undef _STR_RANGE
  1681. #undef STR_RANGE
  1682. #undef STR_RANGE_KEY
  1683. #undef _INT
  1684. #undef INT
  1685. #undef INT_RANGE
  1686. #undef _FUNC
  1687. #undef FUNC
  1688. #undef FUNC_KEY
  1689. #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
  1690. /**
  1691. * wpa_config_add_prio_network - Add a network to priority lists
  1692. * @config: Configuration data from wpa_config_read()
  1693. * @ssid: Pointer to the network configuration to be added to the list
  1694. * Returns: 0 on success, -1 on failure
  1695. *
  1696. * This function is used to add a network block to the priority list of
  1697. * networks. This must be called for each network when reading in the full
  1698. * configuration. In addition, this can be used indirectly when updating
  1699. * priorities by calling wpa_config_update_prio_list().
  1700. */
  1701. int wpa_config_add_prio_network(struct wpa_config *config,
  1702. struct wpa_ssid *ssid)
  1703. {
  1704. int prio;
  1705. struct wpa_ssid *prev, **nlist;
  1706. /*
  1707. * Add to an existing priority list if one is available for the
  1708. * configured priority level for this network.
  1709. */
  1710. for (prio = 0; prio < config->num_prio; prio++) {
  1711. prev = config->pssid[prio];
  1712. if (prev->priority == ssid->priority) {
  1713. while (prev->pnext)
  1714. prev = prev->pnext;
  1715. prev->pnext = ssid;
  1716. return 0;
  1717. }
  1718. }
  1719. /* First network for this priority - add a new priority list */
  1720. nlist = os_realloc_array(config->pssid, config->num_prio + 1,
  1721. sizeof(struct wpa_ssid *));
  1722. if (nlist == NULL)
  1723. return -1;
  1724. for (prio = 0; prio < config->num_prio; prio++) {
  1725. if (nlist[prio]->priority < ssid->priority) {
  1726. os_memmove(&nlist[prio + 1], &nlist[prio],
  1727. (config->num_prio - prio) *
  1728. sizeof(struct wpa_ssid *));
  1729. break;
  1730. }
  1731. }
  1732. nlist[prio] = ssid;
  1733. config->num_prio++;
  1734. config->pssid = nlist;
  1735. return 0;
  1736. }
  1737. /**
  1738. * wpa_config_update_prio_list - Update network priority list
  1739. * @config: Configuration data from wpa_config_read()
  1740. * Returns: 0 on success, -1 on failure
  1741. *
  1742. * This function is called to update the priority list of networks in the
  1743. * configuration when a network is being added or removed. This is also called
  1744. * if a priority for a network is changed.
  1745. */
  1746. int wpa_config_update_prio_list(struct wpa_config *config)
  1747. {
  1748. struct wpa_ssid *ssid;
  1749. int ret = 0;
  1750. os_free(config->pssid);
  1751. config->pssid = NULL;
  1752. config->num_prio = 0;
  1753. ssid = config->ssid;
  1754. while (ssid) {
  1755. ssid->pnext = NULL;
  1756. if (wpa_config_add_prio_network(config, ssid) < 0)
  1757. ret = -1;
  1758. ssid = ssid->next;
  1759. }
  1760. return ret;
  1761. }
  1762. #ifdef IEEE8021X_EAPOL
  1763. static void eap_peer_config_free(struct eap_peer_config *eap)
  1764. {
  1765. os_free(eap->eap_methods);
  1766. bin_clear_free(eap->identity, eap->identity_len);
  1767. os_free(eap->anonymous_identity);
  1768. bin_clear_free(eap->password, eap->password_len);
  1769. os_free(eap->ca_cert);
  1770. os_free(eap->ca_path);
  1771. os_free(eap->client_cert);
  1772. os_free(eap->private_key);
  1773. str_clear_free(eap->private_key_passwd);
  1774. os_free(eap->dh_file);
  1775. os_free(eap->subject_match);
  1776. os_free(eap->altsubject_match);
  1777. os_free(eap->domain_suffix_match);
  1778. os_free(eap->domain_match);
  1779. os_free(eap->ca_cert2);
  1780. os_free(eap->ca_path2);
  1781. os_free(eap->client_cert2);
  1782. os_free(eap->private_key2);
  1783. str_clear_free(eap->private_key2_passwd);
  1784. os_free(eap->dh_file2);
  1785. os_free(eap->subject_match2);
  1786. os_free(eap->altsubject_match2);
  1787. os_free(eap->domain_suffix_match2);
  1788. os_free(eap->domain_match2);
  1789. os_free(eap->phase1);
  1790. os_free(eap->phase2);
  1791. os_free(eap->pcsc);
  1792. str_clear_free(eap->pin);
  1793. os_free(eap->engine_id);
  1794. os_free(eap->key_id);
  1795. os_free(eap->cert_id);
  1796. os_free(eap->ca_cert_id);
  1797. os_free(eap->key2_id);
  1798. os_free(eap->cert2_id);
  1799. os_free(eap->ca_cert2_id);
  1800. str_clear_free(eap->pin2);
  1801. os_free(eap->engine2_id);
  1802. os_free(eap->otp);
  1803. os_free(eap->pending_req_otp);
  1804. os_free(eap->pac_file);
  1805. bin_clear_free(eap->new_password, eap->new_password_len);
  1806. str_clear_free(eap->external_sim_resp);
  1807. os_free(eap->openssl_ciphers);
  1808. }
  1809. #endif /* IEEE8021X_EAPOL */
  1810. /**
  1811. * wpa_config_free_ssid - Free network/ssid configuration data
  1812. * @ssid: Configuration data for the network
  1813. *
  1814. * This function frees all resources allocated for the network configuration
  1815. * data.
  1816. */
  1817. void wpa_config_free_ssid(struct wpa_ssid *ssid)
  1818. {
  1819. struct psk_list_entry *psk;
  1820. os_free(ssid->ssid);
  1821. str_clear_free(ssid->passphrase);
  1822. os_free(ssid->ext_psk);
  1823. #ifdef IEEE8021X_EAPOL
  1824. eap_peer_config_free(&ssid->eap);
  1825. #endif /* IEEE8021X_EAPOL */
  1826. os_free(ssid->id_str);
  1827. os_free(ssid->scan_freq);
  1828. os_free(ssid->freq_list);
  1829. os_free(ssid->bgscan);
  1830. os_free(ssid->p2p_client_list);
  1831. os_free(ssid->bssid_blacklist);
  1832. os_free(ssid->bssid_whitelist);
  1833. #ifdef CONFIG_HT_OVERRIDES
  1834. os_free(ssid->ht_mcs);
  1835. #endif /* CONFIG_HT_OVERRIDES */
  1836. #ifdef CONFIG_MESH
  1837. os_free(ssid->mesh_basic_rates);
  1838. #endif /* CONFIG_MESH */
  1839. while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
  1840. list))) {
  1841. dl_list_del(&psk->list);
  1842. bin_clear_free(psk, sizeof(*psk));
  1843. }
  1844. bin_clear_free(ssid, sizeof(*ssid));
  1845. }
  1846. void wpa_config_free_cred(struct wpa_cred *cred)
  1847. {
  1848. size_t i;
  1849. os_free(cred->realm);
  1850. str_clear_free(cred->username);
  1851. str_clear_free(cred->password);
  1852. os_free(cred->ca_cert);
  1853. os_free(cred->client_cert);
  1854. os_free(cred->private_key);
  1855. str_clear_free(cred->private_key_passwd);
  1856. os_free(cred->imsi);
  1857. str_clear_free(cred->milenage);
  1858. for (i = 0; i < cred->num_domain; i++)
  1859. os_free(cred->domain[i]);
  1860. os_free(cred->domain);
  1861. os_free(cred->domain_suffix_match);
  1862. os_free(cred->eap_method);
  1863. os_free(cred->phase1);
  1864. os_free(cred->phase2);
  1865. os_free(cred->excluded_ssid);
  1866. os_free(cred->roaming_partner);
  1867. os_free(cred->provisioning_sp);
  1868. for (i = 0; i < cred->num_req_conn_capab; i++)
  1869. os_free(cred->req_conn_capab_port[i]);
  1870. os_free(cred->req_conn_capab_port);
  1871. os_free(cred->req_conn_capab_proto);
  1872. os_free(cred);
  1873. }
  1874. void wpa_config_flush_blobs(struct wpa_config *config)
  1875. {
  1876. #ifndef CONFIG_NO_CONFIG_BLOBS
  1877. struct wpa_config_blob *blob, *prev;
  1878. blob = config->blobs;
  1879. config->blobs = NULL;
  1880. while (blob) {
  1881. prev = blob;
  1882. blob = blob->next;
  1883. wpa_config_free_blob(prev);
  1884. }
  1885. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1886. }
  1887. /**
  1888. * wpa_config_free - Free configuration data
  1889. * @config: Configuration data from wpa_config_read()
  1890. *
  1891. * This function frees all resources allocated for the configuration data by
  1892. * wpa_config_read().
  1893. */
  1894. void wpa_config_free(struct wpa_config *config)
  1895. {
  1896. struct wpa_ssid *ssid, *prev = NULL;
  1897. struct wpa_cred *cred, *cprev;
  1898. int i;
  1899. ssid = config->ssid;
  1900. while (ssid) {
  1901. prev = ssid;
  1902. ssid = ssid->next;
  1903. wpa_config_free_ssid(prev);
  1904. }
  1905. cred = config->cred;
  1906. while (cred) {
  1907. cprev = cred;
  1908. cred = cred->next;
  1909. wpa_config_free_cred(cprev);
  1910. }
  1911. wpa_config_flush_blobs(config);
  1912. wpabuf_free(config->wps_vendor_ext_m1);
  1913. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
  1914. wpabuf_free(config->wps_vendor_ext[i]);
  1915. os_free(config->ctrl_interface);
  1916. os_free(config->ctrl_interface_group);
  1917. os_free(config->opensc_engine_path);
  1918. os_free(config->pkcs11_engine_path);
  1919. os_free(config->pkcs11_module_path);
  1920. os_free(config->openssl_ciphers);
  1921. os_free(config->pcsc_reader);
  1922. str_clear_free(config->pcsc_pin);
  1923. os_free(config->driver_param);
  1924. os_free(config->device_name);
  1925. os_free(config->manufacturer);
  1926. os_free(config->model_name);
  1927. os_free(config->model_number);
  1928. os_free(config->serial_number);
  1929. os_free(config->config_methods);
  1930. os_free(config->p2p_ssid_postfix);
  1931. os_free(config->pssid);
  1932. os_free(config->p2p_pref_chan);
  1933. os_free(config->p2p_no_go_freq.range);
  1934. os_free(config->autoscan);
  1935. os_free(config->freq_list);
  1936. wpabuf_free(config->wps_nfc_dh_pubkey);
  1937. wpabuf_free(config->wps_nfc_dh_privkey);
  1938. wpabuf_free(config->wps_nfc_dev_pw);
  1939. os_free(config->ext_password_backend);
  1940. os_free(config->sae_groups);
  1941. wpabuf_free(config->ap_vendor_elements);
  1942. os_free(config->osu_dir);
  1943. os_free(config->bgscan);
  1944. os_free(config->wowlan_triggers);
  1945. os_free(config);
  1946. }
  1947. /**
  1948. * wpa_config_foreach_network - Iterate over each configured network
  1949. * @config: Configuration data from wpa_config_read()
  1950. * @func: Callback function to process each network
  1951. * @arg: Opaque argument to pass to callback function
  1952. *
  1953. * Iterate over the set of configured networks calling the specified
  1954. * function for each item. We guard against callbacks removing the
  1955. * supplied network.
  1956. */
  1957. void wpa_config_foreach_network(struct wpa_config *config,
  1958. void (*func)(void *, struct wpa_ssid *),
  1959. void *arg)
  1960. {
  1961. struct wpa_ssid *ssid, *next;
  1962. ssid = config->ssid;
  1963. while (ssid) {
  1964. next = ssid->next;
  1965. func(arg, ssid);
  1966. ssid = next;
  1967. }
  1968. }
  1969. /**
  1970. * wpa_config_get_network - Get configured network based on id
  1971. * @config: Configuration data from wpa_config_read()
  1972. * @id: Unique network id to search for
  1973. * Returns: Network configuration or %NULL if not found
  1974. */
  1975. struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
  1976. {
  1977. struct wpa_ssid *ssid;
  1978. ssid = config->ssid;
  1979. while (ssid) {
  1980. if (id == ssid->id)
  1981. break;
  1982. ssid = ssid->next;
  1983. }
  1984. return ssid;
  1985. }
  1986. /**
  1987. * wpa_config_add_network - Add a new network with empty configuration
  1988. * @config: Configuration data from wpa_config_read()
  1989. * Returns: The new network configuration or %NULL if operation failed
  1990. */
  1991. struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
  1992. {
  1993. int id;
  1994. struct wpa_ssid *ssid, *last = NULL;
  1995. id = -1;
  1996. ssid = config->ssid;
  1997. while (ssid) {
  1998. if (ssid->id > id)
  1999. id = ssid->id;
  2000. last = ssid;
  2001. ssid = ssid->next;
  2002. }
  2003. id++;
  2004. ssid = os_zalloc(sizeof(*ssid));
  2005. if (ssid == NULL)
  2006. return NULL;
  2007. ssid->id = id;
  2008. dl_list_init(&ssid->psk_list);
  2009. if (last)
  2010. last->next = ssid;
  2011. else
  2012. config->ssid = ssid;
  2013. wpa_config_update_prio_list(config);
  2014. return ssid;
  2015. }
  2016. /**
  2017. * wpa_config_remove_network - Remove a configured network based on id
  2018. * @config: Configuration data from wpa_config_read()
  2019. * @id: Unique network id to search for
  2020. * Returns: 0 on success, or -1 if the network was not found
  2021. */
  2022. int wpa_config_remove_network(struct wpa_config *config, int id)
  2023. {
  2024. struct wpa_ssid *ssid, *prev = NULL;
  2025. ssid = config->ssid;
  2026. while (ssid) {
  2027. if (id == ssid->id)
  2028. break;
  2029. prev = ssid;
  2030. ssid = ssid->next;
  2031. }
  2032. if (ssid == NULL)
  2033. return -1;
  2034. if (prev)
  2035. prev->next = ssid->next;
  2036. else
  2037. config->ssid = ssid->next;
  2038. wpa_config_update_prio_list(config);
  2039. wpa_config_free_ssid(ssid);
  2040. return 0;
  2041. }
  2042. /**
  2043. * wpa_config_set_network_defaults - Set network default values
  2044. * @ssid: Pointer to network configuration data
  2045. */
  2046. void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
  2047. {
  2048. ssid->proto = DEFAULT_PROTO;
  2049. ssid->pairwise_cipher = DEFAULT_PAIRWISE;
  2050. ssid->group_cipher = DEFAULT_GROUP;
  2051. ssid->key_mgmt = DEFAULT_KEY_MGMT;
  2052. ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
  2053. #ifdef IEEE8021X_EAPOL
  2054. ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
  2055. ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
  2056. ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
  2057. ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
  2058. #endif /* IEEE8021X_EAPOL */
  2059. #ifdef CONFIG_MESH
  2060. ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
  2061. ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
  2062. ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
  2063. ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
  2064. #endif /* CONFIG_MESH */
  2065. #ifdef CONFIG_HT_OVERRIDES
  2066. ssid->disable_ht = DEFAULT_DISABLE_HT;
  2067. ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
  2068. ssid->disable_sgi = DEFAULT_DISABLE_SGI;
  2069. ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
  2070. ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
  2071. ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
  2072. ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
  2073. #endif /* CONFIG_HT_OVERRIDES */
  2074. #ifdef CONFIG_VHT_OVERRIDES
  2075. ssid->vht_rx_mcs_nss_1 = -1;
  2076. ssid->vht_rx_mcs_nss_2 = -1;
  2077. ssid->vht_rx_mcs_nss_3 = -1;
  2078. ssid->vht_rx_mcs_nss_4 = -1;
  2079. ssid->vht_rx_mcs_nss_5 = -1;
  2080. ssid->vht_rx_mcs_nss_6 = -1;
  2081. ssid->vht_rx_mcs_nss_7 = -1;
  2082. ssid->vht_rx_mcs_nss_8 = -1;
  2083. ssid->vht_tx_mcs_nss_1 = -1;
  2084. ssid->vht_tx_mcs_nss_2 = -1;
  2085. ssid->vht_tx_mcs_nss_3 = -1;
  2086. ssid->vht_tx_mcs_nss_4 = -1;
  2087. ssid->vht_tx_mcs_nss_5 = -1;
  2088. ssid->vht_tx_mcs_nss_6 = -1;
  2089. ssid->vht_tx_mcs_nss_7 = -1;
  2090. ssid->vht_tx_mcs_nss_8 = -1;
  2091. #endif /* CONFIG_VHT_OVERRIDES */
  2092. ssid->proactive_key_caching = -1;
  2093. #ifdef CONFIG_IEEE80211W
  2094. ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
  2095. #endif /* CONFIG_IEEE80211W */
  2096. ssid->mac_addr = -1;
  2097. }
  2098. /**
  2099. * wpa_config_set - Set a variable in network configuration
  2100. * @ssid: Pointer to network configuration data
  2101. * @var: Variable name, e.g., "ssid"
  2102. * @value: Variable value
  2103. * @line: Line number in configuration file or 0 if not used
  2104. * Returns: 0 on success, -1 on failure
  2105. *
  2106. * This function can be used to set network configuration variables based on
  2107. * both the configuration file and management interface input. The value
  2108. * parameter must be in the same format as the text-based configuration file is
  2109. * using. For example, strings are using double quotation marks.
  2110. */
  2111. int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
  2112. int line)
  2113. {
  2114. size_t i;
  2115. int ret = 0;
  2116. if (ssid == NULL || var == NULL || value == NULL)
  2117. return -1;
  2118. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2119. const struct parse_data *field = &ssid_fields[i];
  2120. if (os_strcmp(var, field->name) != 0)
  2121. continue;
  2122. if (field->parser(field, ssid, line, value)) {
  2123. if (line) {
  2124. wpa_printf(MSG_ERROR, "Line %d: failed to "
  2125. "parse %s '%s'.", line, var, value);
  2126. }
  2127. ret = -1;
  2128. }
  2129. break;
  2130. }
  2131. if (i == NUM_SSID_FIELDS) {
  2132. if (line) {
  2133. wpa_printf(MSG_ERROR, "Line %d: unknown network field "
  2134. "'%s'.", line, var);
  2135. }
  2136. ret = -1;
  2137. }
  2138. return ret;
  2139. }
  2140. int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
  2141. const char *value)
  2142. {
  2143. size_t len;
  2144. char *buf;
  2145. int ret;
  2146. len = os_strlen(value);
  2147. buf = os_malloc(len + 3);
  2148. if (buf == NULL)
  2149. return -1;
  2150. buf[0] = '"';
  2151. os_memcpy(buf + 1, value, len);
  2152. buf[len + 1] = '"';
  2153. buf[len + 2] = '\0';
  2154. ret = wpa_config_set(ssid, var, buf, 0);
  2155. os_free(buf);
  2156. return ret;
  2157. }
  2158. /**
  2159. * wpa_config_get_all - Get all options from network configuration
  2160. * @ssid: Pointer to network configuration data
  2161. * @get_keys: Determines if keys/passwords will be included in returned list
  2162. * (if they may be exported)
  2163. * Returns: %NULL terminated list of all set keys and their values in the form
  2164. * of [key1, val1, key2, val2, ... , NULL]
  2165. *
  2166. * This function can be used to get list of all configured network properties.
  2167. * The caller is responsible for freeing the returned list and all its
  2168. * elements.
  2169. */
  2170. char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
  2171. {
  2172. const struct parse_data *field;
  2173. char *key, *value;
  2174. size_t i;
  2175. char **props;
  2176. int fields_num;
  2177. get_keys = get_keys && ssid->export_keys;
  2178. props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
  2179. if (!props)
  2180. return NULL;
  2181. fields_num = 0;
  2182. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2183. field = &ssid_fields[i];
  2184. if (field->key_data && !get_keys)
  2185. continue;
  2186. value = field->writer(field, ssid);
  2187. if (value == NULL)
  2188. continue;
  2189. if (os_strlen(value) == 0) {
  2190. os_free(value);
  2191. continue;
  2192. }
  2193. key = os_strdup(field->name);
  2194. if (key == NULL) {
  2195. os_free(value);
  2196. goto err;
  2197. }
  2198. props[fields_num * 2] = key;
  2199. props[fields_num * 2 + 1] = value;
  2200. fields_num++;
  2201. }
  2202. return props;
  2203. err:
  2204. value = *props;
  2205. while (value)
  2206. os_free(value++);
  2207. os_free(props);
  2208. return NULL;
  2209. }
  2210. #ifndef NO_CONFIG_WRITE
  2211. /**
  2212. * wpa_config_get - Get a variable in network configuration
  2213. * @ssid: Pointer to network configuration data
  2214. * @var: Variable name, e.g., "ssid"
  2215. * Returns: Value of the variable or %NULL on failure
  2216. *
  2217. * This function can be used to get network configuration variables. The
  2218. * returned value is a copy of the configuration variable in text format, i.e,.
  2219. * the same format that the text-based configuration file and wpa_config_set()
  2220. * are using for the value. The caller is responsible for freeing the returned
  2221. * value.
  2222. */
  2223. char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
  2224. {
  2225. size_t i;
  2226. if (ssid == NULL || var == NULL)
  2227. return NULL;
  2228. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2229. const struct parse_data *field = &ssid_fields[i];
  2230. if (os_strcmp(var, field->name) == 0)
  2231. return field->writer(field, ssid);
  2232. }
  2233. return NULL;
  2234. }
  2235. /**
  2236. * wpa_config_get_no_key - Get a variable in network configuration (no keys)
  2237. * @ssid: Pointer to network configuration data
  2238. * @var: Variable name, e.g., "ssid"
  2239. * Returns: Value of the variable or %NULL on failure
  2240. *
  2241. * This function can be used to get network configuration variable like
  2242. * wpa_config_get(). The only difference is that this functions does not expose
  2243. * key/password material from the configuration. In case a key/password field
  2244. * is requested, the returned value is an empty string or %NULL if the variable
  2245. * is not set or "*" if the variable is set (regardless of its value). The
  2246. * returned value is a copy of the configuration variable in text format, i.e,.
  2247. * the same format that the text-based configuration file and wpa_config_set()
  2248. * are using for the value. The caller is responsible for freeing the returned
  2249. * value.
  2250. */
  2251. char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
  2252. {
  2253. size_t i;
  2254. if (ssid == NULL || var == NULL)
  2255. return NULL;
  2256. for (i = 0; i < NUM_SSID_FIELDS; i++) {
  2257. const struct parse_data *field = &ssid_fields[i];
  2258. if (os_strcmp(var, field->name) == 0) {
  2259. char *res = field->writer(field, ssid);
  2260. if (field->key_data) {
  2261. if (res && res[0]) {
  2262. wpa_printf(MSG_DEBUG, "Do not allow "
  2263. "key_data field to be "
  2264. "exposed");
  2265. str_clear_free(res);
  2266. return os_strdup("*");
  2267. }
  2268. os_free(res);
  2269. return NULL;
  2270. }
  2271. return res;
  2272. }
  2273. }
  2274. return NULL;
  2275. }
  2276. #endif /* NO_CONFIG_WRITE */
  2277. /**
  2278. * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
  2279. * @ssid: Pointer to network configuration data
  2280. *
  2281. * This function must be called to update WPA PSK when either SSID or the
  2282. * passphrase has changed for the network configuration.
  2283. */
  2284. void wpa_config_update_psk(struct wpa_ssid *ssid)
  2285. {
  2286. #ifndef CONFIG_NO_PBKDF2
  2287. pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
  2288. ssid->psk, PMK_LEN);
  2289. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  2290. ssid->psk, PMK_LEN);
  2291. ssid->psk_set = 1;
  2292. #endif /* CONFIG_NO_PBKDF2 */
  2293. }
  2294. static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
  2295. const char *value)
  2296. {
  2297. u8 *proto;
  2298. int **port;
  2299. int *ports, *nports;
  2300. const char *pos;
  2301. unsigned int num_ports;
  2302. proto = os_realloc_array(cred->req_conn_capab_proto,
  2303. cred->num_req_conn_capab + 1, sizeof(u8));
  2304. if (proto == NULL)
  2305. return -1;
  2306. cred->req_conn_capab_proto = proto;
  2307. port = os_realloc_array(cred->req_conn_capab_port,
  2308. cred->num_req_conn_capab + 1, sizeof(int *));
  2309. if (port == NULL)
  2310. return -1;
  2311. cred->req_conn_capab_port = port;
  2312. proto[cred->num_req_conn_capab] = atoi(value);
  2313. pos = os_strchr(value, ':');
  2314. if (pos == NULL) {
  2315. port[cred->num_req_conn_capab] = NULL;
  2316. cred->num_req_conn_capab++;
  2317. return 0;
  2318. }
  2319. pos++;
  2320. ports = NULL;
  2321. num_ports = 0;
  2322. while (*pos) {
  2323. nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
  2324. if (nports == NULL) {
  2325. os_free(ports);
  2326. return -1;
  2327. }
  2328. ports = nports;
  2329. ports[num_ports++] = atoi(pos);
  2330. pos = os_strchr(pos, ',');
  2331. if (pos == NULL)
  2332. break;
  2333. pos++;
  2334. }
  2335. nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
  2336. if (nports == NULL) {
  2337. os_free(ports);
  2338. return -1;
  2339. }
  2340. ports = nports;
  2341. ports[num_ports] = -1;
  2342. port[cred->num_req_conn_capab] = ports;
  2343. cred->num_req_conn_capab++;
  2344. return 0;
  2345. }
  2346. int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
  2347. const char *value, int line)
  2348. {
  2349. char *val;
  2350. size_t len;
  2351. if (os_strcmp(var, "temporary") == 0) {
  2352. cred->temporary = atoi(value);
  2353. return 0;
  2354. }
  2355. if (os_strcmp(var, "priority") == 0) {
  2356. cred->priority = atoi(value);
  2357. return 0;
  2358. }
  2359. if (os_strcmp(var, "sp_priority") == 0) {
  2360. int prio = atoi(value);
  2361. if (prio < 0 || prio > 255)
  2362. return -1;
  2363. cred->sp_priority = prio;
  2364. return 0;
  2365. }
  2366. if (os_strcmp(var, "pcsc") == 0) {
  2367. cred->pcsc = atoi(value);
  2368. return 0;
  2369. }
  2370. if (os_strcmp(var, "eap") == 0) {
  2371. struct eap_method_type method;
  2372. method.method = eap_peer_get_type(value, &method.vendor);
  2373. if (method.vendor == EAP_VENDOR_IETF &&
  2374. method.method == EAP_TYPE_NONE) {
  2375. wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
  2376. "for a credential", line, value);
  2377. return -1;
  2378. }
  2379. os_free(cred->eap_method);
  2380. cred->eap_method = os_malloc(sizeof(*cred->eap_method));
  2381. if (cred->eap_method == NULL)
  2382. return -1;
  2383. os_memcpy(cred->eap_method, &method, sizeof(method));
  2384. return 0;
  2385. }
  2386. if (os_strcmp(var, "password") == 0 &&
  2387. os_strncmp(value, "ext:", 4) == 0) {
  2388. str_clear_free(cred->password);
  2389. cred->password = os_strdup(value);
  2390. cred->ext_password = 1;
  2391. return 0;
  2392. }
  2393. if (os_strcmp(var, "update_identifier") == 0) {
  2394. cred->update_identifier = atoi(value);
  2395. return 0;
  2396. }
  2397. if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
  2398. cred->min_dl_bandwidth_home = atoi(value);
  2399. return 0;
  2400. }
  2401. if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
  2402. cred->min_ul_bandwidth_home = atoi(value);
  2403. return 0;
  2404. }
  2405. if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
  2406. cred->min_dl_bandwidth_roaming = atoi(value);
  2407. return 0;
  2408. }
  2409. if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
  2410. cred->min_ul_bandwidth_roaming = atoi(value);
  2411. return 0;
  2412. }
  2413. if (os_strcmp(var, "max_bss_load") == 0) {
  2414. cred->max_bss_load = atoi(value);
  2415. return 0;
  2416. }
  2417. if (os_strcmp(var, "req_conn_capab") == 0)
  2418. return wpa_config_set_cred_req_conn_capab(cred, value);
  2419. if (os_strcmp(var, "ocsp") == 0) {
  2420. cred->ocsp = atoi(value);
  2421. return 0;
  2422. }
  2423. if (os_strcmp(var, "sim_num") == 0) {
  2424. cred->sim_num = atoi(value);
  2425. return 0;
  2426. }
  2427. val = wpa_config_parse_string(value, &len);
  2428. if (val == NULL) {
  2429. wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
  2430. "value '%s'.", line, var, value);
  2431. return -1;
  2432. }
  2433. if (os_strcmp(var, "realm") == 0) {
  2434. os_free(cred->realm);
  2435. cred->realm = val;
  2436. return 0;
  2437. }
  2438. if (os_strcmp(var, "username") == 0) {
  2439. str_clear_free(cred->username);
  2440. cred->username = val;
  2441. return 0;
  2442. }
  2443. if (os_strcmp(var, "password") == 0) {
  2444. str_clear_free(cred->password);
  2445. cred->password = val;
  2446. cred->ext_password = 0;
  2447. return 0;
  2448. }
  2449. if (os_strcmp(var, "ca_cert") == 0) {
  2450. os_free(cred->ca_cert);
  2451. cred->ca_cert = val;
  2452. return 0;
  2453. }
  2454. if (os_strcmp(var, "client_cert") == 0) {
  2455. os_free(cred->client_cert);
  2456. cred->client_cert = val;
  2457. return 0;
  2458. }
  2459. if (os_strcmp(var, "private_key") == 0) {
  2460. os_free(cred->private_key);
  2461. cred->private_key = val;
  2462. return 0;
  2463. }
  2464. if (os_strcmp(var, "private_key_passwd") == 0) {
  2465. str_clear_free(cred->private_key_passwd);
  2466. cred->private_key_passwd = val;
  2467. return 0;
  2468. }
  2469. if (os_strcmp(var, "imsi") == 0) {
  2470. os_free(cred->imsi);
  2471. cred->imsi = val;
  2472. return 0;
  2473. }
  2474. if (os_strcmp(var, "milenage") == 0) {
  2475. str_clear_free(cred->milenage);
  2476. cred->milenage = val;
  2477. return 0;
  2478. }
  2479. if (os_strcmp(var, "domain_suffix_match") == 0) {
  2480. os_free(cred->domain_suffix_match);
  2481. cred->domain_suffix_match = val;
  2482. return 0;
  2483. }
  2484. if (os_strcmp(var, "domain") == 0) {
  2485. char **new_domain;
  2486. new_domain = os_realloc_array(cred->domain,
  2487. cred->num_domain + 1,
  2488. sizeof(char *));
  2489. if (new_domain == NULL) {
  2490. os_free(val);
  2491. return -1;
  2492. }
  2493. new_domain[cred->num_domain++] = val;
  2494. cred->domain = new_domain;
  2495. return 0;
  2496. }
  2497. if (os_strcmp(var, "phase1") == 0) {
  2498. os_free(cred->phase1);
  2499. cred->phase1 = val;
  2500. return 0;
  2501. }
  2502. if (os_strcmp(var, "phase2") == 0) {
  2503. os_free(cred->phase2);
  2504. cred->phase2 = val;
  2505. return 0;
  2506. }
  2507. if (os_strcmp(var, "roaming_consortium") == 0) {
  2508. if (len < 3 || len > sizeof(cred->roaming_consortium)) {
  2509. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2510. "roaming_consortium length %d (3..15 "
  2511. "expected)", line, (int) len);
  2512. os_free(val);
  2513. return -1;
  2514. }
  2515. os_memcpy(cred->roaming_consortium, val, len);
  2516. cred->roaming_consortium_len = len;
  2517. os_free(val);
  2518. return 0;
  2519. }
  2520. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  2521. if (len < 3 || len > sizeof(cred->required_roaming_consortium))
  2522. {
  2523. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2524. "required_roaming_consortium length %d "
  2525. "(3..15 expected)", line, (int) len);
  2526. os_free(val);
  2527. return -1;
  2528. }
  2529. os_memcpy(cred->required_roaming_consortium, val, len);
  2530. cred->required_roaming_consortium_len = len;
  2531. os_free(val);
  2532. return 0;
  2533. }
  2534. if (os_strcmp(var, "excluded_ssid") == 0) {
  2535. struct excluded_ssid *e;
  2536. if (len > MAX_SSID_LEN) {
  2537. wpa_printf(MSG_ERROR, "Line %d: invalid "
  2538. "excluded_ssid length %d", line, (int) len);
  2539. os_free(val);
  2540. return -1;
  2541. }
  2542. e = os_realloc_array(cred->excluded_ssid,
  2543. cred->num_excluded_ssid + 1,
  2544. sizeof(struct excluded_ssid));
  2545. if (e == NULL) {
  2546. os_free(val);
  2547. return -1;
  2548. }
  2549. cred->excluded_ssid = e;
  2550. e = &cred->excluded_ssid[cred->num_excluded_ssid++];
  2551. os_memcpy(e->ssid, val, len);
  2552. e->ssid_len = len;
  2553. os_free(val);
  2554. return 0;
  2555. }
  2556. if (os_strcmp(var, "roaming_partner") == 0) {
  2557. struct roaming_partner *p;
  2558. char *pos;
  2559. p = os_realloc_array(cred->roaming_partner,
  2560. cred->num_roaming_partner + 1,
  2561. sizeof(struct roaming_partner));
  2562. if (p == NULL) {
  2563. os_free(val);
  2564. return -1;
  2565. }
  2566. cred->roaming_partner = p;
  2567. p = &cred->roaming_partner[cred->num_roaming_partner];
  2568. pos = os_strchr(val, ',');
  2569. if (pos == NULL) {
  2570. os_free(val);
  2571. return -1;
  2572. }
  2573. *pos++ = '\0';
  2574. if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
  2575. os_free(val);
  2576. return -1;
  2577. }
  2578. os_memcpy(p->fqdn, val, pos - val);
  2579. p->exact_match = atoi(pos);
  2580. pos = os_strchr(pos, ',');
  2581. if (pos == NULL) {
  2582. os_free(val);
  2583. return -1;
  2584. }
  2585. *pos++ = '\0';
  2586. p->priority = atoi(pos);
  2587. pos = os_strchr(pos, ',');
  2588. if (pos == NULL) {
  2589. os_free(val);
  2590. return -1;
  2591. }
  2592. *pos++ = '\0';
  2593. if (os_strlen(pos) >= sizeof(p->country)) {
  2594. os_free(val);
  2595. return -1;
  2596. }
  2597. os_memcpy(p->country, pos, os_strlen(pos) + 1);
  2598. cred->num_roaming_partner++;
  2599. os_free(val);
  2600. return 0;
  2601. }
  2602. if (os_strcmp(var, "provisioning_sp") == 0) {
  2603. os_free(cred->provisioning_sp);
  2604. cred->provisioning_sp = val;
  2605. return 0;
  2606. }
  2607. if (line) {
  2608. wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
  2609. line, var);
  2610. }
  2611. os_free(val);
  2612. return -1;
  2613. }
  2614. static char * alloc_int_str(int val)
  2615. {
  2616. const unsigned int bufsize = 20;
  2617. char *buf;
  2618. int res;
  2619. buf = os_malloc(bufsize);
  2620. if (buf == NULL)
  2621. return NULL;
  2622. res = os_snprintf(buf, bufsize, "%d", val);
  2623. if (os_snprintf_error(bufsize, res)) {
  2624. os_free(buf);
  2625. buf = NULL;
  2626. }
  2627. return buf;
  2628. }
  2629. static char * alloc_strdup(const char *str)
  2630. {
  2631. if (str == NULL)
  2632. return NULL;
  2633. return os_strdup(str);
  2634. }
  2635. char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
  2636. {
  2637. if (os_strcmp(var, "temporary") == 0)
  2638. return alloc_int_str(cred->temporary);
  2639. if (os_strcmp(var, "priority") == 0)
  2640. return alloc_int_str(cred->priority);
  2641. if (os_strcmp(var, "sp_priority") == 0)
  2642. return alloc_int_str(cred->sp_priority);
  2643. if (os_strcmp(var, "pcsc") == 0)
  2644. return alloc_int_str(cred->pcsc);
  2645. if (os_strcmp(var, "eap") == 0) {
  2646. if (!cred->eap_method)
  2647. return NULL;
  2648. return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
  2649. cred->eap_method[0].method));
  2650. }
  2651. if (os_strcmp(var, "update_identifier") == 0)
  2652. return alloc_int_str(cred->update_identifier);
  2653. if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
  2654. return alloc_int_str(cred->min_dl_bandwidth_home);
  2655. if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
  2656. return alloc_int_str(cred->min_ul_bandwidth_home);
  2657. if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
  2658. return alloc_int_str(cred->min_dl_bandwidth_roaming);
  2659. if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
  2660. return alloc_int_str(cred->min_ul_bandwidth_roaming);
  2661. if (os_strcmp(var, "max_bss_load") == 0)
  2662. return alloc_int_str(cred->max_bss_load);
  2663. if (os_strcmp(var, "req_conn_capab") == 0) {
  2664. unsigned int i;
  2665. char *buf, *end, *pos;
  2666. int ret;
  2667. if (!cred->num_req_conn_capab)
  2668. return NULL;
  2669. buf = os_malloc(4000);
  2670. if (buf == NULL)
  2671. return NULL;
  2672. pos = buf;
  2673. end = pos + 4000;
  2674. for (i = 0; i < cred->num_req_conn_capab; i++) {
  2675. int *ports;
  2676. ret = os_snprintf(pos, end - pos, "%s%u",
  2677. i > 0 ? "\n" : "",
  2678. cred->req_conn_capab_proto[i]);
  2679. if (os_snprintf_error(end - pos, ret))
  2680. return buf;
  2681. pos += ret;
  2682. ports = cred->req_conn_capab_port[i];
  2683. if (ports) {
  2684. int j;
  2685. for (j = 0; ports[j] != -1; j++) {
  2686. ret = os_snprintf(pos, end - pos,
  2687. "%s%d",
  2688. j > 0 ? "," : ":",
  2689. ports[j]);
  2690. if (os_snprintf_error(end - pos, ret))
  2691. return buf;
  2692. pos += ret;
  2693. }
  2694. }
  2695. }
  2696. return buf;
  2697. }
  2698. if (os_strcmp(var, "ocsp") == 0)
  2699. return alloc_int_str(cred->ocsp);
  2700. if (os_strcmp(var, "realm") == 0)
  2701. return alloc_strdup(cred->realm);
  2702. if (os_strcmp(var, "username") == 0)
  2703. return alloc_strdup(cred->username);
  2704. if (os_strcmp(var, "password") == 0) {
  2705. if (!cred->password)
  2706. return NULL;
  2707. return alloc_strdup("*");
  2708. }
  2709. if (os_strcmp(var, "ca_cert") == 0)
  2710. return alloc_strdup(cred->ca_cert);
  2711. if (os_strcmp(var, "client_cert") == 0)
  2712. return alloc_strdup(cred->client_cert);
  2713. if (os_strcmp(var, "private_key") == 0)
  2714. return alloc_strdup(cred->private_key);
  2715. if (os_strcmp(var, "private_key_passwd") == 0) {
  2716. if (!cred->private_key_passwd)
  2717. return NULL;
  2718. return alloc_strdup("*");
  2719. }
  2720. if (os_strcmp(var, "imsi") == 0)
  2721. return alloc_strdup(cred->imsi);
  2722. if (os_strcmp(var, "milenage") == 0) {
  2723. if (!(cred->milenage))
  2724. return NULL;
  2725. return alloc_strdup("*");
  2726. }
  2727. if (os_strcmp(var, "domain_suffix_match") == 0)
  2728. return alloc_strdup(cred->domain_suffix_match);
  2729. if (os_strcmp(var, "domain") == 0) {
  2730. unsigned int i;
  2731. char *buf, *end, *pos;
  2732. int ret;
  2733. if (!cred->num_domain)
  2734. return NULL;
  2735. buf = os_malloc(4000);
  2736. if (buf == NULL)
  2737. return NULL;
  2738. pos = buf;
  2739. end = pos + 4000;
  2740. for (i = 0; i < cred->num_domain; i++) {
  2741. ret = os_snprintf(pos, end - pos, "%s%s",
  2742. i > 0 ? "\n" : "", cred->domain[i]);
  2743. if (os_snprintf_error(end - pos, ret))
  2744. return buf;
  2745. pos += ret;
  2746. }
  2747. return buf;
  2748. }
  2749. if (os_strcmp(var, "phase1") == 0)
  2750. return alloc_strdup(cred->phase1);
  2751. if (os_strcmp(var, "phase2") == 0)
  2752. return alloc_strdup(cred->phase2);
  2753. if (os_strcmp(var, "roaming_consortium") == 0) {
  2754. size_t buflen;
  2755. char *buf;
  2756. if (!cred->roaming_consortium_len)
  2757. return NULL;
  2758. buflen = cred->roaming_consortium_len * 2 + 1;
  2759. buf = os_malloc(buflen);
  2760. if (buf == NULL)
  2761. return NULL;
  2762. wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
  2763. cred->roaming_consortium_len);
  2764. return buf;
  2765. }
  2766. if (os_strcmp(var, "required_roaming_consortium") == 0) {
  2767. size_t buflen;
  2768. char *buf;
  2769. if (!cred->required_roaming_consortium_len)
  2770. return NULL;
  2771. buflen = cred->required_roaming_consortium_len * 2 + 1;
  2772. buf = os_malloc(buflen);
  2773. if (buf == NULL)
  2774. return NULL;
  2775. wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
  2776. cred->required_roaming_consortium_len);
  2777. return buf;
  2778. }
  2779. if (os_strcmp(var, "excluded_ssid") == 0) {
  2780. unsigned int i;
  2781. char *buf, *end, *pos;
  2782. if (!cred->num_excluded_ssid)
  2783. return NULL;
  2784. buf = os_malloc(4000);
  2785. if (buf == NULL)
  2786. return NULL;
  2787. pos = buf;
  2788. end = pos + 4000;
  2789. for (i = 0; i < cred->num_excluded_ssid; i++) {
  2790. struct excluded_ssid *e;
  2791. int ret;
  2792. e = &cred->excluded_ssid[i];
  2793. ret = os_snprintf(pos, end - pos, "%s%s",
  2794. i > 0 ? "\n" : "",
  2795. wpa_ssid_txt(e->ssid, e->ssid_len));
  2796. if (os_snprintf_error(end - pos, ret))
  2797. return buf;
  2798. pos += ret;
  2799. }
  2800. return buf;
  2801. }
  2802. if (os_strcmp(var, "roaming_partner") == 0) {
  2803. unsigned int i;
  2804. char *buf, *end, *pos;
  2805. if (!cred->num_roaming_partner)
  2806. return NULL;
  2807. buf = os_malloc(4000);
  2808. if (buf == NULL)
  2809. return NULL;
  2810. pos = buf;
  2811. end = pos + 4000;
  2812. for (i = 0; i < cred->num_roaming_partner; i++) {
  2813. struct roaming_partner *p;
  2814. int ret;
  2815. p = &cred->roaming_partner[i];
  2816. ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
  2817. i > 0 ? "\n" : "",
  2818. p->fqdn, p->exact_match, p->priority,
  2819. p->country);
  2820. if (os_snprintf_error(end - pos, ret))
  2821. return buf;
  2822. pos += ret;
  2823. }
  2824. return buf;
  2825. }
  2826. if (os_strcmp(var, "provisioning_sp") == 0)
  2827. return alloc_strdup(cred->provisioning_sp);
  2828. return NULL;
  2829. }
  2830. struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
  2831. {
  2832. struct wpa_cred *cred;
  2833. cred = config->cred;
  2834. while (cred) {
  2835. if (id == cred->id)
  2836. break;
  2837. cred = cred->next;
  2838. }
  2839. return cred;
  2840. }
  2841. struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
  2842. {
  2843. int id;
  2844. struct wpa_cred *cred, *last = NULL;
  2845. id = -1;
  2846. cred = config->cred;
  2847. while (cred) {
  2848. if (cred->id > id)
  2849. id = cred->id;
  2850. last = cred;
  2851. cred = cred->next;
  2852. }
  2853. id++;
  2854. cred = os_zalloc(sizeof(*cred));
  2855. if (cred == NULL)
  2856. return NULL;
  2857. cred->id = id;
  2858. cred->sim_num = DEFAULT_USER_SELECTED_SIM;
  2859. if (last)
  2860. last->next = cred;
  2861. else
  2862. config->cred = cred;
  2863. return cred;
  2864. }
  2865. int wpa_config_remove_cred(struct wpa_config *config, int id)
  2866. {
  2867. struct wpa_cred *cred, *prev = NULL;
  2868. cred = config->cred;
  2869. while (cred) {
  2870. if (id == cred->id)
  2871. break;
  2872. prev = cred;
  2873. cred = cred->next;
  2874. }
  2875. if (cred == NULL)
  2876. return -1;
  2877. if (prev)
  2878. prev->next = cred->next;
  2879. else
  2880. config->cred = cred->next;
  2881. wpa_config_free_cred(cred);
  2882. return 0;
  2883. }
  2884. #ifndef CONFIG_NO_CONFIG_BLOBS
  2885. /**
  2886. * wpa_config_get_blob - Get a named configuration blob
  2887. * @config: Configuration data from wpa_config_read()
  2888. * @name: Name of the blob
  2889. * Returns: Pointer to blob data or %NULL if not found
  2890. */
  2891. const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
  2892. const char *name)
  2893. {
  2894. struct wpa_config_blob *blob = config->blobs;
  2895. while (blob) {
  2896. if (os_strcmp(blob->name, name) == 0)
  2897. return blob;
  2898. blob = blob->next;
  2899. }
  2900. return NULL;
  2901. }
  2902. /**
  2903. * wpa_config_set_blob - Set or add a named configuration blob
  2904. * @config: Configuration data from wpa_config_read()
  2905. * @blob: New value for the blob
  2906. *
  2907. * Adds a new configuration blob or replaces the current value of an existing
  2908. * blob.
  2909. */
  2910. void wpa_config_set_blob(struct wpa_config *config,
  2911. struct wpa_config_blob *blob)
  2912. {
  2913. wpa_config_remove_blob(config, blob->name);
  2914. blob->next = config->blobs;
  2915. config->blobs = blob;
  2916. }
  2917. /**
  2918. * wpa_config_free_blob - Free blob data
  2919. * @blob: Pointer to blob to be freed
  2920. */
  2921. void wpa_config_free_blob(struct wpa_config_blob *blob)
  2922. {
  2923. if (blob) {
  2924. os_free(blob->name);
  2925. bin_clear_free(blob->data, blob->len);
  2926. os_free(blob);
  2927. }
  2928. }
  2929. /**
  2930. * wpa_config_remove_blob - Remove a named configuration blob
  2931. * @config: Configuration data from wpa_config_read()
  2932. * @name: Name of the blob to remove
  2933. * Returns: 0 if blob was removed or -1 if blob was not found
  2934. */
  2935. int wpa_config_remove_blob(struct wpa_config *config, const char *name)
  2936. {
  2937. struct wpa_config_blob *pos = config->blobs, *prev = NULL;
  2938. while (pos) {
  2939. if (os_strcmp(pos->name, name) == 0) {
  2940. if (prev)
  2941. prev->next = pos->next;
  2942. else
  2943. config->blobs = pos->next;
  2944. wpa_config_free_blob(pos);
  2945. return 0;
  2946. }
  2947. prev = pos;
  2948. pos = pos->next;
  2949. }
  2950. return -1;
  2951. }
  2952. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2953. /**
  2954. * wpa_config_alloc_empty - Allocate an empty configuration
  2955. * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
  2956. * socket
  2957. * @driver_param: Driver parameters
  2958. * Returns: Pointer to allocated configuration data or %NULL on failure
  2959. */
  2960. struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
  2961. const char *driver_param)
  2962. {
  2963. struct wpa_config *config;
  2964. const int aCWmin = 4, aCWmax = 10;
  2965. const struct hostapd_wmm_ac_params ac_bk =
  2966. { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
  2967. const struct hostapd_wmm_ac_params ac_be =
  2968. { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
  2969. const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
  2970. { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
  2971. const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
  2972. { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
  2973. config = os_zalloc(sizeof(*config));
  2974. if (config == NULL)
  2975. return NULL;
  2976. config->eapol_version = DEFAULT_EAPOL_VERSION;
  2977. config->ap_scan = DEFAULT_AP_SCAN;
  2978. config->user_mpm = DEFAULT_USER_MPM;
  2979. config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
  2980. config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
  2981. config->fast_reauth = DEFAULT_FAST_REAUTH;
  2982. config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
  2983. config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
  2984. config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
  2985. config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
  2986. config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
  2987. config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
  2988. config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
  2989. config->max_num_sta = DEFAULT_MAX_NUM_STA;
  2990. config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
  2991. config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
  2992. config->wmm_ac_params[0] = ac_be;
  2993. config->wmm_ac_params[1] = ac_bk;
  2994. config->wmm_ac_params[2] = ac_vi;
  2995. config->wmm_ac_params[3] = ac_vo;
  2996. config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
  2997. config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
  2998. config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
  2999. config->cert_in_cb = DEFAULT_CERT_IN_CB;
  3000. if (ctrl_interface)
  3001. config->ctrl_interface = os_strdup(ctrl_interface);
  3002. if (driver_param)
  3003. config->driver_param = os_strdup(driver_param);
  3004. return config;
  3005. }
  3006. #ifndef CONFIG_NO_STDOUT_DEBUG
  3007. /**
  3008. * wpa_config_debug_dump_networks - Debug dump of configured networks
  3009. * @config: Configuration data from wpa_config_read()
  3010. */
  3011. void wpa_config_debug_dump_networks(struct wpa_config *config)
  3012. {
  3013. int prio;
  3014. struct wpa_ssid *ssid;
  3015. for (prio = 0; prio < config->num_prio; prio++) {
  3016. ssid = config->pssid[prio];
  3017. wpa_printf(MSG_DEBUG, "Priority group %d",
  3018. ssid->priority);
  3019. while (ssid) {
  3020. wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
  3021. ssid->id,
  3022. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  3023. ssid = ssid->pnext;
  3024. }
  3025. }
  3026. }
  3027. #endif /* CONFIG_NO_STDOUT_DEBUG */
  3028. struct global_parse_data {
  3029. char *name;
  3030. int (*parser)(const struct global_parse_data *data,
  3031. struct wpa_config *config, int line, const char *value);
  3032. void *param1, *param2, *param3;
  3033. unsigned int changed_flag;
  3034. };
  3035. static int wpa_global_config_parse_int(const struct global_parse_data *data,
  3036. struct wpa_config *config, int line,
  3037. const char *pos)
  3038. {
  3039. int val, *dst;
  3040. char *end;
  3041. dst = (int *) (((u8 *) config) + (long) data->param1);
  3042. val = strtol(pos, &end, 0);
  3043. if (*end) {
  3044. wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
  3045. line, pos);
  3046. return -1;
  3047. }
  3048. *dst = val;
  3049. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  3050. if (data->param2 && *dst < (long) data->param2) {
  3051. wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
  3052. "min_value=%ld)", line, data->name, *dst,
  3053. (long) data->param2);
  3054. *dst = (long) data->param2;
  3055. return -1;
  3056. }
  3057. if (data->param3 && *dst > (long) data->param3) {
  3058. wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
  3059. "max_value=%ld)", line, data->name, *dst,
  3060. (long) data->param3);
  3061. *dst = (long) data->param3;
  3062. return -1;
  3063. }
  3064. return 0;
  3065. }
  3066. static int wpa_global_config_parse_str(const struct global_parse_data *data,
  3067. struct wpa_config *config, int line,
  3068. const char *pos)
  3069. {
  3070. size_t len;
  3071. char **dst, *tmp;
  3072. len = os_strlen(pos);
  3073. if (data->param2 && len < (size_t) data->param2) {
  3074. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  3075. "min_len=%ld)", line, data->name,
  3076. (unsigned long) len, (long) data->param2);
  3077. return -1;
  3078. }
  3079. if (data->param3 && len > (size_t) data->param3) {
  3080. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  3081. "max_len=%ld)", line, data->name,
  3082. (unsigned long) len, (long) data->param3);
  3083. return -1;
  3084. }
  3085. tmp = os_strdup(pos);
  3086. if (tmp == NULL)
  3087. return -1;
  3088. dst = (char **) (((u8 *) config) + (long) data->param1);
  3089. os_free(*dst);
  3090. *dst = tmp;
  3091. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  3092. return 0;
  3093. }
  3094. static int wpa_config_process_bgscan(const struct global_parse_data *data,
  3095. struct wpa_config *config, int line,
  3096. const char *pos)
  3097. {
  3098. size_t len;
  3099. char *tmp;
  3100. int res;
  3101. tmp = wpa_config_parse_string(pos, &len);
  3102. if (tmp == NULL) {
  3103. wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
  3104. line, data->name);
  3105. return -1;
  3106. }
  3107. res = wpa_global_config_parse_str(data, config, line, tmp);
  3108. os_free(tmp);
  3109. return res;
  3110. }
  3111. static int wpa_global_config_parse_bin(const struct global_parse_data *data,
  3112. struct wpa_config *config, int line,
  3113. const char *pos)
  3114. {
  3115. size_t len;
  3116. struct wpabuf **dst, *tmp;
  3117. len = os_strlen(pos);
  3118. if (len & 0x01)
  3119. return -1;
  3120. tmp = wpabuf_alloc(len / 2);
  3121. if (tmp == NULL)
  3122. return -1;
  3123. if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
  3124. wpabuf_free(tmp);
  3125. return -1;
  3126. }
  3127. dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
  3128. wpabuf_free(*dst);
  3129. *dst = tmp;
  3130. wpa_printf(MSG_DEBUG, "%s", data->name);
  3131. return 0;
  3132. }
  3133. static int wpa_config_process_freq_list(const struct global_parse_data *data,
  3134. struct wpa_config *config, int line,
  3135. const char *value)
  3136. {
  3137. int *freqs;
  3138. freqs = wpa_config_parse_int_array(value);
  3139. if (freqs == NULL)
  3140. return -1;
  3141. if (freqs[0] == 0) {
  3142. os_free(freqs);
  3143. freqs = NULL;
  3144. }
  3145. os_free(config->freq_list);
  3146. config->freq_list = freqs;
  3147. return 0;
  3148. }
  3149. #ifdef CONFIG_P2P
  3150. static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
  3151. struct wpa_config *config, int line,
  3152. const char *pos)
  3153. {
  3154. u32 *dst;
  3155. struct hostapd_ip_addr addr;
  3156. if (hostapd_parse_ip_addr(pos, &addr) < 0)
  3157. return -1;
  3158. if (addr.af != AF_INET)
  3159. return -1;
  3160. dst = (u32 *) (((u8 *) config) + (long) data->param1);
  3161. os_memcpy(dst, &addr.u.v4.s_addr, 4);
  3162. wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
  3163. WPA_GET_BE32((u8 *) dst));
  3164. return 0;
  3165. }
  3166. #endif /* CONFIG_P2P */
  3167. static int wpa_config_process_country(const struct global_parse_data *data,
  3168. struct wpa_config *config, int line,
  3169. const char *pos)
  3170. {
  3171. if (!pos[0] || !pos[1]) {
  3172. wpa_printf(MSG_DEBUG, "Invalid country set");
  3173. return -1;
  3174. }
  3175. config->country[0] = pos[0];
  3176. config->country[1] = pos[1];
  3177. wpa_printf(MSG_DEBUG, "country='%c%c'",
  3178. config->country[0], config->country[1]);
  3179. return 0;
  3180. }
  3181. static int wpa_config_process_load_dynamic_eap(
  3182. const struct global_parse_data *data, struct wpa_config *config,
  3183. int line, const char *so)
  3184. {
  3185. int ret;
  3186. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  3187. ret = eap_peer_method_load(so);
  3188. if (ret == -2) {
  3189. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  3190. "reloading.");
  3191. } else if (ret) {
  3192. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  3193. "method '%s'.", line, so);
  3194. return -1;
  3195. }
  3196. return 0;
  3197. }
  3198. #ifdef CONFIG_WPS
  3199. static int wpa_config_process_uuid(const struct global_parse_data *data,
  3200. struct wpa_config *config, int line,
  3201. const char *pos)
  3202. {
  3203. char buf[40];
  3204. if (uuid_str2bin(pos, config->uuid)) {
  3205. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  3206. return -1;
  3207. }
  3208. uuid_bin2str(config->uuid, buf, sizeof(buf));
  3209. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  3210. return 0;
  3211. }
  3212. static int wpa_config_process_device_type(
  3213. const struct global_parse_data *data,
  3214. struct wpa_config *config, int line, const char *pos)
  3215. {
  3216. return wps_dev_type_str2bin(pos, config->device_type);
  3217. }
  3218. static int wpa_config_process_os_version(const struct global_parse_data *data,
  3219. struct wpa_config *config, int line,
  3220. const char *pos)
  3221. {
  3222. if (hexstr2bin(pos, config->os_version, 4)) {
  3223. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  3224. return -1;
  3225. }
  3226. wpa_printf(MSG_DEBUG, "os_version=%08x",
  3227. WPA_GET_BE32(config->os_version));
  3228. return 0;
  3229. }
  3230. static int wpa_config_process_wps_vendor_ext_m1(
  3231. const struct global_parse_data *data,
  3232. struct wpa_config *config, int line, const char *pos)
  3233. {
  3234. struct wpabuf *tmp;
  3235. int len = os_strlen(pos) / 2;
  3236. u8 *p;
  3237. if (!len) {
  3238. wpa_printf(MSG_ERROR, "Line %d: "
  3239. "invalid wps_vendor_ext_m1", line);
  3240. return -1;
  3241. }
  3242. tmp = wpabuf_alloc(len);
  3243. if (tmp) {
  3244. p = wpabuf_put(tmp, len);
  3245. if (hexstr2bin(pos, p, len)) {
  3246. wpa_printf(MSG_ERROR, "Line %d: "
  3247. "invalid wps_vendor_ext_m1", line);
  3248. wpabuf_free(tmp);
  3249. return -1;
  3250. }
  3251. wpabuf_free(config->wps_vendor_ext_m1);
  3252. config->wps_vendor_ext_m1 = tmp;
  3253. } else {
  3254. wpa_printf(MSG_ERROR, "Can not allocate "
  3255. "memory for wps_vendor_ext_m1");
  3256. return -1;
  3257. }
  3258. return 0;
  3259. }
  3260. #endif /* CONFIG_WPS */
  3261. #ifdef CONFIG_P2P
  3262. static int wpa_config_process_sec_device_type(
  3263. const struct global_parse_data *data,
  3264. struct wpa_config *config, int line, const char *pos)
  3265. {
  3266. int idx;
  3267. if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
  3268. wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
  3269. "items", line);
  3270. return -1;
  3271. }
  3272. idx = config->num_sec_device_types;
  3273. if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
  3274. return -1;
  3275. config->num_sec_device_types++;
  3276. return 0;
  3277. }
  3278. static int wpa_config_process_p2p_pref_chan(
  3279. const struct global_parse_data *data,
  3280. struct wpa_config *config, int line, const char *pos)
  3281. {
  3282. struct p2p_channel *pref = NULL, *n;
  3283. unsigned int num = 0;
  3284. const char *pos2;
  3285. u8 op_class, chan;
  3286. /* format: class:chan,class:chan,... */
  3287. while (*pos) {
  3288. op_class = atoi(pos);
  3289. pos2 = os_strchr(pos, ':');
  3290. if (pos2 == NULL)
  3291. goto fail;
  3292. pos2++;
  3293. chan = atoi(pos2);
  3294. n = os_realloc_array(pref, num + 1,
  3295. sizeof(struct p2p_channel));
  3296. if (n == NULL)
  3297. goto fail;
  3298. pref = n;
  3299. pref[num].op_class = op_class;
  3300. pref[num].chan = chan;
  3301. num++;
  3302. pos = os_strchr(pos2, ',');
  3303. if (pos == NULL)
  3304. break;
  3305. pos++;
  3306. }
  3307. os_free(config->p2p_pref_chan);
  3308. config->p2p_pref_chan = pref;
  3309. config->num_p2p_pref_chan = num;
  3310. wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
  3311. (u8 *) config->p2p_pref_chan,
  3312. config->num_p2p_pref_chan * sizeof(struct p2p_channel));
  3313. return 0;
  3314. fail:
  3315. os_free(pref);
  3316. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
  3317. return -1;
  3318. }
  3319. static int wpa_config_process_p2p_no_go_freq(
  3320. const struct global_parse_data *data,
  3321. struct wpa_config *config, int line, const char *pos)
  3322. {
  3323. int ret;
  3324. ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
  3325. if (ret < 0) {
  3326. wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
  3327. return -1;
  3328. }
  3329. wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
  3330. config->p2p_no_go_freq.num);
  3331. return 0;
  3332. }
  3333. #endif /* CONFIG_P2P */
  3334. static int wpa_config_process_hessid(
  3335. const struct global_parse_data *data,
  3336. struct wpa_config *config, int line, const char *pos)
  3337. {
  3338. if (hwaddr_aton2(pos, config->hessid) < 0) {
  3339. wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
  3340. line, pos);
  3341. return -1;
  3342. }
  3343. return 0;
  3344. }
  3345. static int wpa_config_process_sae_groups(
  3346. const struct global_parse_data *data,
  3347. struct wpa_config *config, int line, const char *pos)
  3348. {
  3349. int *groups = wpa_config_parse_int_array(pos);
  3350. if (groups == NULL) {
  3351. wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
  3352. line, pos);
  3353. return -1;
  3354. }
  3355. os_free(config->sae_groups);
  3356. config->sae_groups = groups;
  3357. return 0;
  3358. }
  3359. static int wpa_config_process_ap_vendor_elements(
  3360. const struct global_parse_data *data,
  3361. struct wpa_config *config, int line, const char *pos)
  3362. {
  3363. struct wpabuf *tmp;
  3364. int len = os_strlen(pos) / 2;
  3365. u8 *p;
  3366. if (!len) {
  3367. wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
  3368. line);
  3369. return -1;
  3370. }
  3371. tmp = wpabuf_alloc(len);
  3372. if (tmp) {
  3373. p = wpabuf_put(tmp, len);
  3374. if (hexstr2bin(pos, p, len)) {
  3375. wpa_printf(MSG_ERROR, "Line %d: invalid "
  3376. "ap_vendor_elements", line);
  3377. wpabuf_free(tmp);
  3378. return -1;
  3379. }
  3380. wpabuf_free(config->ap_vendor_elements);
  3381. config->ap_vendor_elements = tmp;
  3382. } else {
  3383. wpa_printf(MSG_ERROR, "Cannot allocate memory for "
  3384. "ap_vendor_elements");
  3385. return -1;
  3386. }
  3387. return 0;
  3388. }
  3389. #ifdef CONFIG_CTRL_IFACE
  3390. static int wpa_config_process_no_ctrl_interface(
  3391. const struct global_parse_data *data,
  3392. struct wpa_config *config, int line, const char *pos)
  3393. {
  3394. wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
  3395. os_free(config->ctrl_interface);
  3396. config->ctrl_interface = NULL;
  3397. return 0;
  3398. }
  3399. #endif /* CONFIG_CTRL_IFACE */
  3400. #ifdef OFFSET
  3401. #undef OFFSET
  3402. #endif /* OFFSET */
  3403. /* OFFSET: Get offset of a variable within the wpa_config structure */
  3404. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  3405. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  3406. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  3407. #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
  3408. #define INT(f) _INT(f), NULL, NULL
  3409. #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
  3410. #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
  3411. #define STR(f) _STR(f), NULL, NULL
  3412. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  3413. #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
  3414. #define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
  3415. static const struct global_parse_data global_fields[] = {
  3416. #ifdef CONFIG_CTRL_IFACE
  3417. { STR(ctrl_interface), 0 },
  3418. { FUNC_NO_VAR(no_ctrl_interface), 0 },
  3419. { STR(ctrl_interface_group), 0 } /* deprecated */,
  3420. #endif /* CONFIG_CTRL_IFACE */
  3421. #ifdef CONFIG_MACSEC
  3422. { INT_RANGE(eapol_version, 1, 3), 0 },
  3423. #else /* CONFIG_MACSEC */
  3424. { INT_RANGE(eapol_version, 1, 2), 0 },
  3425. #endif /* CONFIG_MACSEC */
  3426. { INT(ap_scan), 0 },
  3427. { FUNC(bgscan), 0 },
  3428. #ifdef CONFIG_MESH
  3429. { INT(user_mpm), 0 },
  3430. { INT_RANGE(max_peer_links, 0, 255), 0 },
  3431. { INT(mesh_max_inactivity), 0 },
  3432. #endif /* CONFIG_MESH */
  3433. { INT(disable_scan_offload), 0 },
  3434. { INT(fast_reauth), 0 },
  3435. { STR(opensc_engine_path), 0 },
  3436. { STR(pkcs11_engine_path), 0 },
  3437. { STR(pkcs11_module_path), 0 },
  3438. { STR(openssl_ciphers), 0 },
  3439. { STR(pcsc_reader), 0 },
  3440. { STR(pcsc_pin), 0 },
  3441. { INT(external_sim), 0 },
  3442. { STR(driver_param), 0 },
  3443. { INT(dot11RSNAConfigPMKLifetime), 0 },
  3444. { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
  3445. { INT(dot11RSNAConfigSATimeout), 0 },
  3446. #ifndef CONFIG_NO_CONFIG_WRITE
  3447. { INT(update_config), 0 },
  3448. #endif /* CONFIG_NO_CONFIG_WRITE */
  3449. { FUNC_NO_VAR(load_dynamic_eap), 0 },
  3450. #ifdef CONFIG_WPS
  3451. { FUNC(uuid), CFG_CHANGED_UUID },
  3452. { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
  3453. { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
  3454. { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
  3455. { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
  3456. { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
  3457. { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
  3458. { FUNC(os_version), CFG_CHANGED_OS_VERSION },
  3459. { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
  3460. { INT_RANGE(wps_cred_processing, 0, 2), 0 },
  3461. { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
  3462. #endif /* CONFIG_WPS */
  3463. #ifdef CONFIG_P2P
  3464. { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
  3465. { INT(p2p_listen_reg_class), 0 },
  3466. { INT(p2p_listen_channel), 0 },
  3467. { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
  3468. { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
  3469. { INT_RANGE(p2p_go_intent, 0, 15), 0 },
  3470. { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
  3471. { INT_RANGE(persistent_reconnect, 0, 1), 0 },
  3472. { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
  3473. { INT(p2p_group_idle), 0 },
  3474. { INT_RANGE(p2p_passphrase_len, 8, 63),
  3475. CFG_CHANGED_P2P_PASSPHRASE_LEN },
  3476. { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
  3477. { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
  3478. { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
  3479. { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
  3480. { INT(p2p_go_ht40), 0 },
  3481. { INT(p2p_go_vht), 0 },
  3482. { INT(p2p_disabled), 0 },
  3483. { INT(p2p_no_group_iface), 0 },
  3484. { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
  3485. { IPV4(ip_addr_go), 0 },
  3486. { IPV4(ip_addr_mask), 0 },
  3487. { IPV4(ip_addr_start), 0 },
  3488. { IPV4(ip_addr_end), 0 },
  3489. #endif /* CONFIG_P2P */
  3490. { FUNC(country), CFG_CHANGED_COUNTRY },
  3491. { INT(bss_max_count), 0 },
  3492. { INT(bss_expiration_age), 0 },
  3493. { INT(bss_expiration_scan_count), 0 },
  3494. { INT_RANGE(filter_ssids, 0, 1), 0 },
  3495. { INT_RANGE(filter_rssi, -100, 0), 0 },
  3496. { INT(max_num_sta), 0 },
  3497. { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
  3498. #ifdef CONFIG_HS20
  3499. { INT_RANGE(hs20, 0, 1), 0 },
  3500. #endif /* CONFIG_HS20 */
  3501. { INT_RANGE(interworking, 0, 1), 0 },
  3502. { FUNC(hessid), 0 },
  3503. { INT_RANGE(access_network_type, 0, 15), 0 },
  3504. { INT_RANGE(pbc_in_m1, 0, 1), 0 },
  3505. { STR(autoscan), 0 },
  3506. { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
  3507. CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3508. { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3509. { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3510. { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
  3511. { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
  3512. { INT(p2p_go_max_inactivity), 0 },
  3513. { INT_RANGE(auto_interworking, 0, 1), 0 },
  3514. { INT(okc), 0 },
  3515. { INT(pmf), 0 },
  3516. { FUNC(sae_groups), 0 },
  3517. { INT(dtim_period), 0 },
  3518. { INT(beacon_int), 0 },
  3519. { FUNC(ap_vendor_elements), 0 },
  3520. { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
  3521. { FUNC(freq_list), 0 },
  3522. { INT(scan_cur_freq), 0 },
  3523. { INT(sched_scan_interval), 0 },
  3524. { INT(tdls_external_control), 0},
  3525. { STR(osu_dir), 0 },
  3526. { STR(wowlan_triggers), 0 },
  3527. { INT(p2p_search_delay), 0},
  3528. { INT(mac_addr), 0 },
  3529. { INT(rand_addr_lifetime), 0 },
  3530. { INT(preassoc_mac_addr), 0 },
  3531. { INT(key_mgmt_offload), 0},
  3532. { INT(passive_scan), 0 },
  3533. };
  3534. #undef FUNC
  3535. #undef _INT
  3536. #undef INT
  3537. #undef INT_RANGE
  3538. #undef _STR
  3539. #undef STR
  3540. #undef STR_RANGE
  3541. #undef BIN
  3542. #undef IPV4
  3543. #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
  3544. int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
  3545. {
  3546. size_t i;
  3547. int ret = 0;
  3548. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  3549. const struct global_parse_data *field = &global_fields[i];
  3550. size_t flen = os_strlen(field->name);
  3551. if (os_strncmp(pos, field->name, flen) != 0 ||
  3552. pos[flen] != '=')
  3553. continue;
  3554. if (field->parser(field, config, line, pos + flen + 1)) {
  3555. wpa_printf(MSG_ERROR, "Line %d: failed to "
  3556. "parse '%s'.", line, pos);
  3557. ret = -1;
  3558. }
  3559. if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
  3560. config->wps_nfc_pw_from_config = 1;
  3561. config->changed_parameters |= field->changed_flag;
  3562. break;
  3563. }
  3564. if (i == NUM_GLOBAL_FIELDS) {
  3565. #ifdef CONFIG_AP
  3566. if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
  3567. char *tmp = os_strchr(pos, '=');
  3568. if (tmp == NULL) {
  3569. if (line < 0)
  3570. return -1;
  3571. wpa_printf(MSG_ERROR, "Line %d: invalid line "
  3572. "'%s'", line, pos);
  3573. return -1;
  3574. }
  3575. *tmp++ = '\0';
  3576. if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
  3577. tmp)) {
  3578. wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
  3579. "AC item", line);
  3580. return -1;
  3581. }
  3582. }
  3583. #endif /* CONFIG_AP */
  3584. if (line < 0)
  3585. return -1;
  3586. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  3587. line, pos);
  3588. ret = -1;
  3589. }
  3590. return ret;
  3591. }