config.c 98 KB

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