config.c 74 KB

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