config.c 92 KB

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