config.c 92 KB

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