config.c 96 KB

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