config.c 93 KB

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