config.c 98 KB

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