config.c 85 KB

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