config.c 73 KB

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