config.c 97 KB

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