config.c 95 KB

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