config.c 86 KB

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