ieee802_11.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-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 "utils/includes.h"
  9. #ifndef CONFIG_NATIVE_WINDOWS
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "crypto/crypto.h"
  13. #include "drivers/driver.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "common/ieee802_11_common.h"
  16. #include "common/wpa_ctrl.h"
  17. #include "common/sae.h"
  18. #include "radius/radius.h"
  19. #include "radius/radius_client.h"
  20. #include "p2p/p2p.h"
  21. #include "wps/wps.h"
  22. #include "hostapd.h"
  23. #include "beacon.h"
  24. #include "ieee802_11_auth.h"
  25. #include "sta_info.h"
  26. #include "ieee802_1x.h"
  27. #include "wpa_auth.h"
  28. #include "wmm.h"
  29. #include "ap_list.h"
  30. #include "accounting.h"
  31. #include "ap_config.h"
  32. #include "ap_mlme.h"
  33. #include "p2p_hostapd.h"
  34. #include "ap_drv_ops.h"
  35. #include "wnm_ap.h"
  36. #include "ieee802_11.h"
  37. u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
  38. {
  39. u8 *pos = eid;
  40. int i, num, count;
  41. if (hapd->iface->current_rates == NULL)
  42. return eid;
  43. *pos++ = WLAN_EID_SUPP_RATES;
  44. num = hapd->iface->num_rates;
  45. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
  46. num++;
  47. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
  48. num++;
  49. if (num > 8) {
  50. /* rest of the rates are encoded in Extended supported
  51. * rates element */
  52. num = 8;
  53. }
  54. *pos++ = num;
  55. count = 0;
  56. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
  57. i++) {
  58. count++;
  59. *pos = hapd->iface->current_rates[i].rate / 5;
  60. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  61. *pos |= 0x80;
  62. pos++;
  63. }
  64. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
  65. count++;
  66. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
  67. }
  68. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
  69. count++;
  70. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
  71. }
  72. return pos;
  73. }
  74. u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
  75. {
  76. u8 *pos = eid;
  77. int i, num, count;
  78. if (hapd->iface->current_rates == NULL)
  79. return eid;
  80. num = hapd->iface->num_rates;
  81. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
  82. num++;
  83. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
  84. num++;
  85. if (num <= 8)
  86. return eid;
  87. num -= 8;
  88. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  89. *pos++ = num;
  90. count = 0;
  91. for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
  92. i++) {
  93. count++;
  94. if (count <= 8)
  95. continue; /* already in SuppRates IE */
  96. *pos = hapd->iface->current_rates[i].rate / 5;
  97. if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
  98. *pos |= 0x80;
  99. pos++;
  100. }
  101. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
  102. count++;
  103. if (count > 8)
  104. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
  105. }
  106. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
  107. count++;
  108. if (count > 8)
  109. *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
  110. }
  111. return pos;
  112. }
  113. u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
  114. int probe)
  115. {
  116. int capab = WLAN_CAPABILITY_ESS;
  117. int privacy;
  118. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  119. hapd->iconf->preamble == SHORT_PREAMBLE)
  120. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  121. privacy = hapd->conf->ssid.wep.keys_set;
  122. if (hapd->conf->ieee802_1x &&
  123. (hapd->conf->default_wep_key_len ||
  124. hapd->conf->individual_wep_key_len))
  125. privacy = 1;
  126. if (hapd->conf->wpa)
  127. privacy = 1;
  128. if (sta) {
  129. int policy, def_klen;
  130. if (probe && sta->ssid_probe) {
  131. policy = sta->ssid_probe->security_policy;
  132. def_klen = sta->ssid_probe->wep.default_len;
  133. } else {
  134. policy = sta->ssid->security_policy;
  135. def_klen = sta->ssid->wep.default_len;
  136. }
  137. privacy = policy != SECURITY_PLAINTEXT;
  138. if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
  139. privacy = 0;
  140. }
  141. if (privacy)
  142. capab |= WLAN_CAPABILITY_PRIVACY;
  143. if (hapd->iface->current_mode &&
  144. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  145. hapd->iface->num_sta_no_short_slot_time == 0)
  146. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  147. return capab;
  148. }
  149. void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
  150. {
  151. int i;
  152. if (len > HOSTAPD_MAX_SSID_LEN)
  153. len = HOSTAPD_MAX_SSID_LEN;
  154. for (i = 0; i < len; i++) {
  155. if (ssid[i] >= 32 && ssid[i] < 127)
  156. buf[i] = ssid[i];
  157. else
  158. buf[i] = '.';
  159. }
  160. buf[len] = '\0';
  161. }
  162. static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
  163. u16 auth_transaction, const u8 *challenge,
  164. int iswep)
  165. {
  166. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  167. HOSTAPD_LEVEL_DEBUG,
  168. "authentication (shared key, transaction %d)",
  169. auth_transaction);
  170. if (auth_transaction == 1) {
  171. if (!sta->challenge) {
  172. /* Generate a pseudo-random challenge */
  173. u8 key[8];
  174. struct os_time now;
  175. int r;
  176. sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
  177. if (sta->challenge == NULL)
  178. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  179. os_get_time(&now);
  180. r = os_random();
  181. os_memcpy(key, &now.sec, 4);
  182. os_memcpy(key + 4, &r, 4);
  183. rc4_skip(key, sizeof(key), 0,
  184. sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
  185. }
  186. return 0;
  187. }
  188. if (auth_transaction != 3)
  189. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  190. /* Transaction 3 */
  191. if (!iswep || !sta->challenge || !challenge ||
  192. os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
  193. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  194. HOSTAPD_LEVEL_INFO,
  195. "shared key authentication - invalid "
  196. "challenge-response");
  197. return WLAN_STATUS_CHALLENGE_FAIL;
  198. }
  199. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  200. HOSTAPD_LEVEL_DEBUG,
  201. "authentication OK (shared key)");
  202. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  203. /* Station will be marked authenticated if it ACKs the
  204. * authentication reply. */
  205. #else
  206. sta->flags |= WLAN_STA_AUTH;
  207. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  208. #endif
  209. os_free(sta->challenge);
  210. sta->challenge = NULL;
  211. return 0;
  212. }
  213. static void send_auth_reply(struct hostapd_data *hapd,
  214. const u8 *dst, const u8 *bssid,
  215. u16 auth_alg, u16 auth_transaction, u16 resp,
  216. const u8 *ies, size_t ies_len)
  217. {
  218. struct ieee80211_mgmt *reply;
  219. u8 *buf;
  220. size_t rlen;
  221. rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
  222. buf = os_zalloc(rlen);
  223. if (buf == NULL)
  224. return;
  225. reply = (struct ieee80211_mgmt *) buf;
  226. reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  227. WLAN_FC_STYPE_AUTH);
  228. os_memcpy(reply->da, dst, ETH_ALEN);
  229. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  230. os_memcpy(reply->bssid, bssid, ETH_ALEN);
  231. reply->u.auth.auth_alg = host_to_le16(auth_alg);
  232. reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
  233. reply->u.auth.status_code = host_to_le16(resp);
  234. if (ies && ies_len)
  235. os_memcpy(reply->u.auth.variable, ies, ies_len);
  236. wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
  237. " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
  238. MAC2STR(dst), auth_alg, auth_transaction,
  239. resp, (unsigned long) ies_len);
  240. if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
  241. perror("send_auth_reply: send");
  242. os_free(buf);
  243. }
  244. #ifdef CONFIG_IEEE80211R
  245. static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
  246. u16 auth_transaction, u16 status,
  247. const u8 *ies, size_t ies_len)
  248. {
  249. struct hostapd_data *hapd = ctx;
  250. struct sta_info *sta;
  251. send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
  252. status, ies, ies_len);
  253. if (status != WLAN_STATUS_SUCCESS)
  254. return;
  255. sta = ap_get_sta(hapd, dst);
  256. if (sta == NULL)
  257. return;
  258. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  259. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  260. sta->flags |= WLAN_STA_AUTH;
  261. mlme_authenticate_indication(hapd, sta);
  262. }
  263. #endif /* CONFIG_IEEE80211R */
  264. #ifdef CONFIG_SAE
  265. static struct wpabuf * auth_process_sae_commit(struct hostapd_data *hapd,
  266. struct sta_info *sta)
  267. {
  268. struct wpabuf *buf;
  269. if (hapd->conf->ssid.wpa_passphrase == NULL) {
  270. wpa_printf(MSG_DEBUG, "SAE: No password available");
  271. return NULL;
  272. }
  273. if (sae_prepare_commit(hapd->own_addr, sta->addr,
  274. (u8 *) hapd->conf->ssid.wpa_passphrase,
  275. os_strlen(hapd->conf->ssid.wpa_passphrase),
  276. sta->sae) < 0) {
  277. wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
  278. return NULL;
  279. }
  280. if (sae_process_commit(sta->sae) < 0) {
  281. wpa_printf(MSG_DEBUG, "SAE: Failed to process peer commit");
  282. return NULL;
  283. }
  284. buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
  285. if (buf == NULL)
  286. return NULL;
  287. sae_write_commit(sta->sae, buf);
  288. return buf;
  289. }
  290. static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
  291. struct sta_info *sta)
  292. {
  293. struct wpabuf *buf;
  294. buf = wpabuf_alloc(2);
  295. if (buf == NULL)
  296. return NULL;
  297. wpabuf_put_le16(buf, sta->sae->send_confirm);
  298. sta->sae->send_confirm++;
  299. /* TODO: Confirm */
  300. return buf;
  301. }
  302. static u16 handle_sae_confirm(struct hostapd_data *hapd, struct sta_info *sta,
  303. const u8 *data, size_t len)
  304. {
  305. u16 rc;
  306. wpa_hexdump(MSG_DEBUG, "SAE confirm fields", data, len);
  307. if (len < 2)
  308. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  309. rc = WPA_GET_LE16(data);
  310. wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", rc);
  311. return WLAN_STATUS_SUCCESS;
  312. }
  313. static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
  314. const struct ieee80211_mgmt *mgmt, size_t len,
  315. u8 auth_transaction)
  316. {
  317. u16 resp = WLAN_STATUS_SUCCESS;
  318. struct wpabuf *data = NULL;
  319. if (!sta->sae) {
  320. sta->sae = os_zalloc(sizeof(*sta->sae));
  321. if (sta->sae == NULL)
  322. return;
  323. }
  324. if (auth_transaction == 1) {
  325. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  326. HOSTAPD_LEVEL_DEBUG,
  327. "start SAE authentication (RX commit)");
  328. resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
  329. ((const u8 *) mgmt) + len -
  330. mgmt->u.auth.variable);
  331. if (resp == WLAN_STATUS_SUCCESS) {
  332. sta->sae->state = SAE_COMMIT;
  333. data = auth_process_sae_commit(hapd, sta);
  334. if (data == NULL)
  335. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  336. }
  337. } else if (auth_transaction == 2) {
  338. if (sta->sae->state != SAE_COMMIT) {
  339. hostapd_logger(hapd, sta->addr,
  340. HOSTAPD_MODULE_IEEE80211,
  341. HOSTAPD_LEVEL_DEBUG,
  342. "SAE confirm before commit");
  343. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  344. }
  345. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  346. HOSTAPD_LEVEL_DEBUG,
  347. "SAE authentication (RX confirm)");
  348. resp = handle_sae_confirm(hapd, sta, mgmt->u.auth.variable,
  349. ((u8 *) mgmt) + len -
  350. mgmt->u.auth.variable);
  351. if (resp == WLAN_STATUS_SUCCESS) {
  352. sta->flags |= WLAN_STA_AUTH;
  353. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  354. sta->auth_alg = WLAN_AUTH_SAE;
  355. mlme_authenticate_indication(hapd, sta);
  356. data = auth_build_sae_confirm(hapd, sta);
  357. if (data == NULL)
  358. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  359. }
  360. } else {
  361. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  362. HOSTAPD_LEVEL_DEBUG,
  363. "unexpected SAE authentication transaction %u",
  364. auth_transaction);
  365. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  366. }
  367. sta->auth_alg = WLAN_AUTH_SAE;
  368. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
  369. auth_transaction, resp,
  370. data ? wpabuf_head(data) : (u8 *) "",
  371. data ? wpabuf_len(data) : 0);
  372. wpabuf_free(data);
  373. }
  374. #endif /* CONFIG_SAE */
  375. static void handle_auth(struct hostapd_data *hapd,
  376. const struct ieee80211_mgmt *mgmt, size_t len)
  377. {
  378. u16 auth_alg, auth_transaction, status_code;
  379. u16 resp = WLAN_STATUS_SUCCESS;
  380. struct sta_info *sta = NULL;
  381. int res;
  382. u16 fc;
  383. const u8 *challenge = NULL;
  384. u32 session_timeout, acct_interim_interval;
  385. int vlan_id = 0;
  386. struct hostapd_sta_wpa_psk_short *psk = NULL;
  387. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  388. size_t resp_ies_len = 0;
  389. char *identity = NULL;
  390. char *radius_cui = NULL;
  391. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  392. printf("handle_auth - too short payload (len=%lu)\n",
  393. (unsigned long) len);
  394. return;
  395. }
  396. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  397. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  398. status_code = le_to_host16(mgmt->u.auth.status_code);
  399. fc = le_to_host16(mgmt->frame_control);
  400. if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
  401. 2 + WLAN_AUTH_CHALLENGE_LEN &&
  402. mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
  403. mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
  404. challenge = &mgmt->u.auth.variable[2];
  405. wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
  406. "auth_transaction=%d status_code=%d wep=%d%s",
  407. MAC2STR(mgmt->sa), auth_alg, auth_transaction,
  408. status_code, !!(fc & WLAN_FC_ISWEP),
  409. challenge ? " challenge" : "");
  410. if (hapd->tkip_countermeasures) {
  411. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  412. goto fail;
  413. }
  414. if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
  415. auth_alg == WLAN_AUTH_OPEN) ||
  416. #ifdef CONFIG_IEEE80211R
  417. (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
  418. auth_alg == WLAN_AUTH_FT) ||
  419. #endif /* CONFIG_IEEE80211R */
  420. #ifdef CONFIG_SAE
  421. (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
  422. auth_alg == WLAN_AUTH_SAE) ||
  423. #endif /* CONFIG_SAE */
  424. ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
  425. auth_alg == WLAN_AUTH_SHARED_KEY))) {
  426. printf("Unsupported authentication algorithm (%d)\n",
  427. auth_alg);
  428. resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  429. goto fail;
  430. }
  431. if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
  432. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
  433. printf("Unknown authentication transaction number (%d)\n",
  434. auth_transaction);
  435. resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  436. goto fail;
  437. }
  438. if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
  439. printf("Station " MACSTR " not allowed to authenticate.\n",
  440. MAC2STR(mgmt->sa));
  441. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  442. goto fail;
  443. }
  444. res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
  445. &session_timeout,
  446. &acct_interim_interval, &vlan_id,
  447. &psk, &identity, &radius_cui);
  448. if (res == HOSTAPD_ACL_REJECT) {
  449. printf("Station " MACSTR " not allowed to authenticate.\n",
  450. MAC2STR(mgmt->sa));
  451. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  452. goto fail;
  453. }
  454. if (res == HOSTAPD_ACL_PENDING) {
  455. wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
  456. " waiting for an external authentication",
  457. MAC2STR(mgmt->sa));
  458. /* Authentication code will re-send the authentication frame
  459. * after it has received (and cached) information from the
  460. * external source. */
  461. return;
  462. }
  463. sta = ap_sta_add(hapd, mgmt->sa);
  464. if (!sta) {
  465. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  466. goto fail;
  467. }
  468. if (vlan_id > 0) {
  469. if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  470. vlan_id) == NULL) {
  471. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  472. HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
  473. "%d received from RADIUS server",
  474. vlan_id);
  475. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  476. goto fail;
  477. }
  478. sta->vlan_id = vlan_id;
  479. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  480. HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
  481. }
  482. hostapd_free_psk_list(sta->psk);
  483. if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
  484. sta->psk = psk;
  485. psk = NULL;
  486. } else {
  487. sta->psk = NULL;
  488. }
  489. sta->identity = identity;
  490. identity = NULL;
  491. sta->radius_cui = radius_cui;
  492. radius_cui = NULL;
  493. sta->flags &= ~WLAN_STA_PREAUTH;
  494. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  495. if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
  496. sta->acct_interim_interval = acct_interim_interval;
  497. if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  498. ap_sta_session_timeout(hapd, sta, session_timeout);
  499. else
  500. ap_sta_no_session_timeout(hapd, sta);
  501. switch (auth_alg) {
  502. case WLAN_AUTH_OPEN:
  503. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  504. HOSTAPD_LEVEL_DEBUG,
  505. "authentication OK (open system)");
  506. #ifdef IEEE80211_REQUIRE_AUTH_ACK
  507. /* Station will be marked authenticated if it ACKs the
  508. * authentication reply. */
  509. #else
  510. sta->flags |= WLAN_STA_AUTH;
  511. wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
  512. sta->auth_alg = WLAN_AUTH_OPEN;
  513. mlme_authenticate_indication(hapd, sta);
  514. #endif
  515. break;
  516. case WLAN_AUTH_SHARED_KEY:
  517. resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
  518. fc & WLAN_FC_ISWEP);
  519. sta->auth_alg = WLAN_AUTH_SHARED_KEY;
  520. mlme_authenticate_indication(hapd, sta);
  521. if (sta->challenge && auth_transaction == 1) {
  522. resp_ies[0] = WLAN_EID_CHALLENGE;
  523. resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
  524. os_memcpy(resp_ies + 2, sta->challenge,
  525. WLAN_AUTH_CHALLENGE_LEN);
  526. resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
  527. }
  528. break;
  529. #ifdef CONFIG_IEEE80211R
  530. case WLAN_AUTH_FT:
  531. sta->auth_alg = WLAN_AUTH_FT;
  532. if (sta->wpa_sm == NULL)
  533. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  534. sta->addr);
  535. if (sta->wpa_sm == NULL) {
  536. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  537. "state machine");
  538. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  539. goto fail;
  540. }
  541. wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
  542. auth_transaction, mgmt->u.auth.variable,
  543. len - IEEE80211_HDRLEN -
  544. sizeof(mgmt->u.auth),
  545. handle_auth_ft_finish, hapd);
  546. /* handle_auth_ft_finish() callback will complete auth. */
  547. return;
  548. #endif /* CONFIG_IEEE80211R */
  549. #ifdef CONFIG_SAE
  550. case WLAN_AUTH_SAE:
  551. handle_auth_sae(hapd, sta, mgmt, len, auth_transaction);
  552. return;
  553. #endif /* CONFIG_SAE */
  554. }
  555. fail:
  556. os_free(identity);
  557. os_free(radius_cui);
  558. hostapd_free_psk_list(psk);
  559. send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
  560. auth_transaction + 1, resp, resp_ies, resp_ies_len);
  561. }
  562. static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
  563. {
  564. int i, j = 32, aid;
  565. /* get a unique AID */
  566. if (sta->aid > 0) {
  567. wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
  568. return 0;
  569. }
  570. for (i = 0; i < AID_WORDS; i++) {
  571. if (hapd->sta_aid[i] == (u32) -1)
  572. continue;
  573. for (j = 0; j < 32; j++) {
  574. if (!(hapd->sta_aid[i] & BIT(j)))
  575. break;
  576. }
  577. if (j < 32)
  578. break;
  579. }
  580. if (j == 32)
  581. return -1;
  582. aid = i * 32 + j + 1;
  583. if (aid > 2007)
  584. return -1;
  585. sta->aid = aid;
  586. hapd->sta_aid[i] |= BIT(j);
  587. wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
  588. return 0;
  589. }
  590. static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
  591. const u8 *ssid_ie, size_t ssid_ie_len)
  592. {
  593. if (ssid_ie == NULL)
  594. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  595. if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
  596. os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
  597. char ssid_txt[33];
  598. ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
  599. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  600. HOSTAPD_LEVEL_INFO,
  601. "Station tried to associate with unknown SSID "
  602. "'%s'", ssid_txt);
  603. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  604. }
  605. return WLAN_STATUS_SUCCESS;
  606. }
  607. static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
  608. const u8 *wmm_ie, size_t wmm_ie_len)
  609. {
  610. sta->flags &= ~WLAN_STA_WMM;
  611. sta->qosinfo = 0;
  612. if (wmm_ie && hapd->conf->wmm_enabled) {
  613. struct wmm_information_element *wmm;
  614. if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
  615. hostapd_logger(hapd, sta->addr,
  616. HOSTAPD_MODULE_WPA,
  617. HOSTAPD_LEVEL_DEBUG,
  618. "invalid WMM element in association "
  619. "request");
  620. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  621. }
  622. sta->flags |= WLAN_STA_WMM;
  623. wmm = (struct wmm_information_element *) wmm_ie;
  624. sta->qosinfo = wmm->qos_info;
  625. }
  626. return WLAN_STATUS_SUCCESS;
  627. }
  628. static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
  629. struct ieee802_11_elems *elems)
  630. {
  631. if (!elems->supp_rates) {
  632. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  633. HOSTAPD_LEVEL_DEBUG,
  634. "No supported rates element in AssocReq");
  635. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  636. }
  637. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  638. sizeof(sta->supported_rates)) {
  639. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  640. HOSTAPD_LEVEL_DEBUG,
  641. "Invalid supported rates element length %d+%d",
  642. elems->supp_rates_len,
  643. elems->ext_supp_rates_len);
  644. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  645. }
  646. sta->supported_rates_len = merge_byte_arrays(
  647. sta->supported_rates, sizeof(sta->supported_rates),
  648. elems->supp_rates, elems->supp_rates_len,
  649. elems->ext_supp_rates, elems->ext_supp_rates_len);
  650. return WLAN_STATUS_SUCCESS;
  651. }
  652. static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
  653. const u8 *ies, size_t ies_len, int reassoc)
  654. {
  655. struct ieee802_11_elems elems;
  656. u16 resp;
  657. const u8 *wpa_ie;
  658. size_t wpa_ie_len;
  659. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
  660. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  661. HOSTAPD_LEVEL_INFO, "Station sent an invalid "
  662. "association request");
  663. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  664. }
  665. resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
  666. if (resp != WLAN_STATUS_SUCCESS)
  667. return resp;
  668. resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
  669. if (resp != WLAN_STATUS_SUCCESS)
  670. return resp;
  671. resp = copy_supp_rates(hapd, sta, &elems);
  672. if (resp != WLAN_STATUS_SUCCESS)
  673. return resp;
  674. #ifdef CONFIG_IEEE80211N
  675. resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
  676. elems.ht_capabilities_len);
  677. if (resp != WLAN_STATUS_SUCCESS)
  678. return resp;
  679. if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
  680. !(sta->flags & WLAN_STA_HT)) {
  681. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  682. HOSTAPD_LEVEL_INFO, "Station does not support "
  683. "mandatory HT PHY - reject association");
  684. return WLAN_STATUS_ASSOC_DENIED_NO_HT;
  685. }
  686. #endif /* CONFIG_IEEE80211N */
  687. #ifdef CONFIG_IEEE80211AC
  688. resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
  689. elems.vht_capabilities_len);
  690. if (resp != WLAN_STATUS_SUCCESS)
  691. return resp;
  692. if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
  693. !(sta->flags & WLAN_STA_VHT)) {
  694. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  695. HOSTAPD_LEVEL_INFO, "Station does not support "
  696. "mandatory VHT PHY - reject association");
  697. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  698. }
  699. #endif /* CONFIG_IEEE80211AC */
  700. if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
  701. wpa_ie = elems.rsn_ie;
  702. wpa_ie_len = elems.rsn_ie_len;
  703. } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
  704. elems.wpa_ie) {
  705. wpa_ie = elems.wpa_ie;
  706. wpa_ie_len = elems.wpa_ie_len;
  707. } else {
  708. wpa_ie = NULL;
  709. wpa_ie_len = 0;
  710. }
  711. #ifdef CONFIG_WPS
  712. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
  713. if (hapd->conf->wps_state && elems.wps_ie) {
  714. wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
  715. "Request - assume WPS is used");
  716. sta->flags |= WLAN_STA_WPS;
  717. wpabuf_free(sta->wps_ie);
  718. sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  719. WPS_IE_VENDOR_TYPE);
  720. if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
  721. wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
  722. sta->flags |= WLAN_STA_WPS2;
  723. }
  724. wpa_ie = NULL;
  725. wpa_ie_len = 0;
  726. if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
  727. wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
  728. "(Re)Association Request - reject");
  729. return WLAN_STATUS_INVALID_IE;
  730. }
  731. } else if (hapd->conf->wps_state && wpa_ie == NULL) {
  732. wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
  733. "(Re)Association Request - possible WPS use");
  734. sta->flags |= WLAN_STA_MAYBE_WPS;
  735. } else
  736. #endif /* CONFIG_WPS */
  737. if (hapd->conf->wpa && wpa_ie == NULL) {
  738. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  739. HOSTAPD_LEVEL_INFO,
  740. "No WPA/RSN IE in association request");
  741. return WLAN_STATUS_INVALID_IE;
  742. }
  743. if (hapd->conf->wpa && wpa_ie) {
  744. int res;
  745. wpa_ie -= 2;
  746. wpa_ie_len += 2;
  747. if (sta->wpa_sm == NULL)
  748. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  749. sta->addr);
  750. if (sta->wpa_sm == NULL) {
  751. wpa_printf(MSG_WARNING, "Failed to initialize WPA "
  752. "state machine");
  753. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  754. }
  755. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  756. wpa_ie, wpa_ie_len,
  757. elems.mdie, elems.mdie_len);
  758. if (res == WPA_INVALID_GROUP)
  759. resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  760. else if (res == WPA_INVALID_PAIRWISE)
  761. resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  762. else if (res == WPA_INVALID_AKMP)
  763. resp = WLAN_STATUS_AKMP_NOT_VALID;
  764. else if (res == WPA_ALLOC_FAIL)
  765. resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
  766. #ifdef CONFIG_IEEE80211W
  767. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
  768. resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  769. else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
  770. resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
  771. #endif /* CONFIG_IEEE80211W */
  772. else if (res == WPA_INVALID_MDIE)
  773. resp = WLAN_STATUS_INVALID_MDIE;
  774. else if (res != WPA_IE_OK)
  775. resp = WLAN_STATUS_INVALID_IE;
  776. if (resp != WLAN_STATUS_SUCCESS)
  777. return resp;
  778. #ifdef CONFIG_IEEE80211W
  779. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  780. sta->sa_query_count > 0)
  781. ap_check_sa_query_timeout(hapd, sta);
  782. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  783. (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
  784. /*
  785. * STA has already been associated with MFP and SA
  786. * Query timeout has not been reached. Reject the
  787. * association attempt temporarily and start SA Query,
  788. * if one is not pending.
  789. */
  790. if (sta->sa_query_count == 0)
  791. ap_sta_start_sa_query(hapd, sta);
  792. return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  793. }
  794. if (wpa_auth_uses_mfp(sta->wpa_sm))
  795. sta->flags |= WLAN_STA_MFP;
  796. else
  797. sta->flags &= ~WLAN_STA_MFP;
  798. #endif /* CONFIG_IEEE80211W */
  799. #ifdef CONFIG_IEEE80211R
  800. if (sta->auth_alg == WLAN_AUTH_FT) {
  801. if (!reassoc) {
  802. wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
  803. "to use association (not "
  804. "re-association) with FT auth_alg",
  805. MAC2STR(sta->addr));
  806. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  807. }
  808. resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
  809. ies_len);
  810. if (resp != WLAN_STATUS_SUCCESS)
  811. return resp;
  812. }
  813. #endif /* CONFIG_IEEE80211R */
  814. #ifdef CONFIG_SAE
  815. if (wpa_auth_uses_sae(sta->wpa_sm) &&
  816. sta->auth_alg != WLAN_AUTH_SAE) {
  817. wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
  818. "SAE AKM after non-SAE auth_alg %u",
  819. MAC2STR(sta->addr), sta->auth_alg);
  820. return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
  821. }
  822. #endif /* CONFIG_SAE */
  823. #ifdef CONFIG_IEEE80211N
  824. if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
  825. wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
  826. hostapd_logger(hapd, sta->addr,
  827. HOSTAPD_MODULE_IEEE80211,
  828. HOSTAPD_LEVEL_INFO,
  829. "Station tried to use TKIP with HT "
  830. "association");
  831. return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
  832. }
  833. #endif /* CONFIG_IEEE80211N */
  834. } else
  835. wpa_auth_sta_no_wpa(sta->wpa_sm);
  836. #ifdef CONFIG_P2P
  837. if (elems.p2p) {
  838. wpabuf_free(sta->p2p_ie);
  839. sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
  840. P2P_IE_VENDOR_TYPE);
  841. } else {
  842. wpabuf_free(sta->p2p_ie);
  843. sta->p2p_ie = NULL;
  844. }
  845. p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
  846. #endif /* CONFIG_P2P */
  847. #ifdef CONFIG_HS20
  848. wpabuf_free(sta->hs20_ie);
  849. if (elems.hs20 && elems.hs20_len > 4) {
  850. sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
  851. elems.hs20_len - 4);
  852. } else
  853. sta->hs20_ie = NULL;
  854. #endif /* CONFIG_HS20 */
  855. return WLAN_STATUS_SUCCESS;
  856. }
  857. static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
  858. u16 reason_code)
  859. {
  860. int send_len;
  861. struct ieee80211_mgmt reply;
  862. os_memset(&reply, 0, sizeof(reply));
  863. reply.frame_control =
  864. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
  865. os_memcpy(reply.da, addr, ETH_ALEN);
  866. os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
  867. os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
  868. send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
  869. reply.u.deauth.reason_code = host_to_le16(reason_code);
  870. if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
  871. wpa_printf(MSG_INFO, "Failed to send deauth: %s",
  872. strerror(errno));
  873. }
  874. static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
  875. u16 status_code, int reassoc, const u8 *ies,
  876. size_t ies_len)
  877. {
  878. int send_len;
  879. u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
  880. struct ieee80211_mgmt *reply;
  881. u8 *p;
  882. os_memset(buf, 0, sizeof(buf));
  883. reply = (struct ieee80211_mgmt *) buf;
  884. reply->frame_control =
  885. IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  886. (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
  887. WLAN_FC_STYPE_ASSOC_RESP));
  888. os_memcpy(reply->da, sta->addr, ETH_ALEN);
  889. os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
  890. os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
  891. send_len = IEEE80211_HDRLEN;
  892. send_len += sizeof(reply->u.assoc_resp);
  893. reply->u.assoc_resp.capab_info =
  894. host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
  895. reply->u.assoc_resp.status_code = host_to_le16(status_code);
  896. reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
  897. | BIT(14) | BIT(15));
  898. /* Supported rates */
  899. p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
  900. /* Extended supported rates */
  901. p = hostapd_eid_ext_supp_rates(hapd, p);
  902. #ifdef CONFIG_IEEE80211R
  903. if (status_code == WLAN_STATUS_SUCCESS) {
  904. /* IEEE 802.11r: Mobility Domain Information, Fast BSS
  905. * Transition Information, RSN, [RIC Response] */
  906. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
  907. buf + sizeof(buf) - p,
  908. sta->auth_alg, ies, ies_len);
  909. }
  910. #endif /* CONFIG_IEEE80211R */
  911. #ifdef CONFIG_IEEE80211W
  912. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
  913. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  914. #endif /* CONFIG_IEEE80211W */
  915. #ifdef CONFIG_IEEE80211N
  916. p = hostapd_eid_ht_capabilities(hapd, p);
  917. p = hostapd_eid_ht_operation(hapd, p);
  918. #endif /* CONFIG_IEEE80211N */
  919. #ifdef CONFIG_IEEE80211AC
  920. p = hostapd_eid_vht_capabilities(hapd, p);
  921. p = hostapd_eid_vht_operation(hapd, p);
  922. #endif /* CONFIG_IEEE80211AC */
  923. p = hostapd_eid_ext_capab(hapd, p);
  924. p = hostapd_eid_bss_max_idle_period(hapd, p);
  925. if (sta->flags & WLAN_STA_WMM)
  926. p = hostapd_eid_wmm(hapd, p);
  927. #ifdef CONFIG_WPS
  928. if ((sta->flags & WLAN_STA_WPS) ||
  929. ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
  930. struct wpabuf *wps = wps_build_assoc_resp_ie();
  931. if (wps) {
  932. os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
  933. p += wpabuf_len(wps);
  934. wpabuf_free(wps);
  935. }
  936. }
  937. #endif /* CONFIG_WPS */
  938. #ifdef CONFIG_P2P
  939. if (sta->p2p_ie) {
  940. struct wpabuf *p2p_resp_ie;
  941. enum p2p_status_code status;
  942. switch (status_code) {
  943. case WLAN_STATUS_SUCCESS:
  944. status = P2P_SC_SUCCESS;
  945. break;
  946. case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
  947. status = P2P_SC_FAIL_LIMIT_REACHED;
  948. break;
  949. default:
  950. status = P2P_SC_FAIL_INVALID_PARAMS;
  951. break;
  952. }
  953. p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
  954. if (p2p_resp_ie) {
  955. os_memcpy(p, wpabuf_head(p2p_resp_ie),
  956. wpabuf_len(p2p_resp_ie));
  957. p += wpabuf_len(p2p_resp_ie);
  958. wpabuf_free(p2p_resp_ie);
  959. }
  960. }
  961. #endif /* CONFIG_P2P */
  962. #ifdef CONFIG_P2P_MANAGER
  963. if (hapd->conf->p2p & P2P_MANAGE)
  964. p = hostapd_eid_p2p_manage(hapd, p);
  965. #endif /* CONFIG_P2P_MANAGER */
  966. send_len += p - reply->u.assoc_resp.variable;
  967. if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
  968. wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
  969. strerror(errno));
  970. }
  971. static void handle_assoc(struct hostapd_data *hapd,
  972. const struct ieee80211_mgmt *mgmt, size_t len,
  973. int reassoc)
  974. {
  975. u16 capab_info, listen_interval;
  976. u16 resp = WLAN_STATUS_SUCCESS;
  977. const u8 *pos;
  978. int left, i;
  979. struct sta_info *sta;
  980. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
  981. sizeof(mgmt->u.assoc_req))) {
  982. printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
  983. "\n", reassoc, (unsigned long) len);
  984. return;
  985. }
  986. if (reassoc) {
  987. capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
  988. listen_interval = le_to_host16(
  989. mgmt->u.reassoc_req.listen_interval);
  990. wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
  991. " capab_info=0x%02x listen_interval=%d current_ap="
  992. MACSTR,
  993. MAC2STR(mgmt->sa), capab_info, listen_interval,
  994. MAC2STR(mgmt->u.reassoc_req.current_ap));
  995. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
  996. pos = mgmt->u.reassoc_req.variable;
  997. } else {
  998. capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
  999. listen_interval = le_to_host16(
  1000. mgmt->u.assoc_req.listen_interval);
  1001. wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
  1002. " capab_info=0x%02x listen_interval=%d",
  1003. MAC2STR(mgmt->sa), capab_info, listen_interval);
  1004. left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
  1005. pos = mgmt->u.assoc_req.variable;
  1006. }
  1007. sta = ap_get_sta(hapd, mgmt->sa);
  1008. #ifdef CONFIG_IEEE80211R
  1009. if (sta && sta->auth_alg == WLAN_AUTH_FT &&
  1010. (sta->flags & WLAN_STA_AUTH) == 0) {
  1011. wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
  1012. "prior to authentication since it is using "
  1013. "over-the-DS FT", MAC2STR(mgmt->sa));
  1014. } else
  1015. #endif /* CONFIG_IEEE80211R */
  1016. if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
  1017. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1018. HOSTAPD_LEVEL_INFO, "Station tried to "
  1019. "associate before authentication "
  1020. "(aid=%d flags=0x%x)",
  1021. sta ? sta->aid : -1,
  1022. sta ? sta->flags : 0);
  1023. send_deauth(hapd, mgmt->sa,
  1024. WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
  1025. return;
  1026. }
  1027. if (hapd->tkip_countermeasures) {
  1028. resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
  1029. goto fail;
  1030. }
  1031. if (listen_interval > hapd->conf->max_listen_interval) {
  1032. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1033. HOSTAPD_LEVEL_DEBUG,
  1034. "Too large Listen Interval (%d)",
  1035. listen_interval);
  1036. resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
  1037. goto fail;
  1038. }
  1039. /* followed by SSID and Supported rates; and HT capabilities if 802.11n
  1040. * is used */
  1041. resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
  1042. if (resp != WLAN_STATUS_SUCCESS)
  1043. goto fail;
  1044. if (hostapd_get_aid(hapd, sta) < 0) {
  1045. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1046. HOSTAPD_LEVEL_INFO, "No room for more AIDs");
  1047. resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  1048. goto fail;
  1049. }
  1050. sta->capability = capab_info;
  1051. sta->listen_interval = listen_interval;
  1052. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  1053. sta->flags |= WLAN_STA_NONERP;
  1054. for (i = 0; i < sta->supported_rates_len; i++) {
  1055. if ((sta->supported_rates[i] & 0x7f) > 22) {
  1056. sta->flags &= ~WLAN_STA_NONERP;
  1057. break;
  1058. }
  1059. }
  1060. if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
  1061. sta->nonerp_set = 1;
  1062. hapd->iface->num_sta_non_erp++;
  1063. if (hapd->iface->num_sta_non_erp == 1)
  1064. ieee802_11_set_beacons(hapd->iface);
  1065. }
  1066. if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
  1067. !sta->no_short_slot_time_set) {
  1068. sta->no_short_slot_time_set = 1;
  1069. hapd->iface->num_sta_no_short_slot_time++;
  1070. if (hapd->iface->current_mode->mode ==
  1071. HOSTAPD_MODE_IEEE80211G &&
  1072. hapd->iface->num_sta_no_short_slot_time == 1)
  1073. ieee802_11_set_beacons(hapd->iface);
  1074. }
  1075. if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  1076. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  1077. else
  1078. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  1079. if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  1080. !sta->no_short_preamble_set) {
  1081. sta->no_short_preamble_set = 1;
  1082. hapd->iface->num_sta_no_short_preamble++;
  1083. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  1084. && hapd->iface->num_sta_no_short_preamble == 1)
  1085. ieee802_11_set_beacons(hapd->iface);
  1086. }
  1087. #ifdef CONFIG_IEEE80211N
  1088. update_ht_state(hapd, sta);
  1089. #endif /* CONFIG_IEEE80211N */
  1090. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1091. HOSTAPD_LEVEL_DEBUG,
  1092. "association OK (aid %d)", sta->aid);
  1093. /* Station will be marked associated, after it acknowledges AssocResp
  1094. */
  1095. sta->flags |= WLAN_STA_ASSOC_REQ_OK;
  1096. #ifdef CONFIG_IEEE80211W
  1097. if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
  1098. wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
  1099. "SA Query procedure", reassoc ? "re" : "");
  1100. /* TODO: Send a protected Disassociate frame to the STA using
  1101. * the old key and Reason Code "Previous Authentication no
  1102. * longer valid". Make sure this is only sent protected since
  1103. * unprotected frame would be received by the STA that is now
  1104. * trying to associate.
  1105. */
  1106. }
  1107. #endif /* CONFIG_IEEE80211W */
  1108. if (reassoc) {
  1109. os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
  1110. ETH_ALEN);
  1111. }
  1112. if (sta->last_assoc_req)
  1113. os_free(sta->last_assoc_req);
  1114. sta->last_assoc_req = os_malloc(len);
  1115. if (sta->last_assoc_req)
  1116. os_memcpy(sta->last_assoc_req, mgmt, len);
  1117. /* Make sure that the previously registered inactivity timer will not
  1118. * remove the STA immediately. */
  1119. sta->timeout_next = STA_NULLFUNC;
  1120. fail:
  1121. send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
  1122. }
  1123. static void handle_disassoc(struct hostapd_data *hapd,
  1124. const struct ieee80211_mgmt *mgmt, size_t len)
  1125. {
  1126. struct sta_info *sta;
  1127. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
  1128. printf("handle_disassoc - too short payload (len=%lu)\n",
  1129. (unsigned long) len);
  1130. return;
  1131. }
  1132. wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
  1133. MAC2STR(mgmt->sa),
  1134. le_to_host16(mgmt->u.disassoc.reason_code));
  1135. sta = ap_get_sta(hapd, mgmt->sa);
  1136. if (sta == NULL) {
  1137. printf("Station " MACSTR " trying to disassociate, but it "
  1138. "is not associated.\n", MAC2STR(mgmt->sa));
  1139. return;
  1140. }
  1141. ap_sta_set_authorized(hapd, sta, 0);
  1142. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  1143. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  1144. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1145. HOSTAPD_LEVEL_INFO, "disassociated");
  1146. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1147. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1148. /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
  1149. * authenticated. */
  1150. accounting_sta_stop(hapd, sta);
  1151. ieee802_1x_free_station(sta);
  1152. hostapd_drv_sta_remove(hapd, sta->addr);
  1153. if (sta->timeout_next == STA_NULLFUNC ||
  1154. sta->timeout_next == STA_DISASSOC) {
  1155. sta->timeout_next = STA_DEAUTH;
  1156. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1157. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  1158. hapd, sta);
  1159. }
  1160. mlme_disassociate_indication(
  1161. hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
  1162. }
  1163. static void handle_deauth(struct hostapd_data *hapd,
  1164. const struct ieee80211_mgmt *mgmt, size_t len)
  1165. {
  1166. struct sta_info *sta;
  1167. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
  1168. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
  1169. "payload (len=%lu)", (unsigned long) len);
  1170. return;
  1171. }
  1172. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
  1173. " reason_code=%d",
  1174. MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
  1175. sta = ap_get_sta(hapd, mgmt->sa);
  1176. if (sta == NULL) {
  1177. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
  1178. "to deauthenticate, but it is not authenticated",
  1179. MAC2STR(mgmt->sa));
  1180. return;
  1181. }
  1182. ap_sta_set_authorized(hapd, sta, 0);
  1183. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1184. WLAN_STA_ASSOC_REQ_OK);
  1185. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1186. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1187. HOSTAPD_LEVEL_DEBUG, "deauthenticated");
  1188. mlme_deauthenticate_indication(
  1189. hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
  1190. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  1191. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1192. ap_free_sta(hapd, sta);
  1193. }
  1194. static void handle_beacon(struct hostapd_data *hapd,
  1195. const struct ieee80211_mgmt *mgmt, size_t len,
  1196. struct hostapd_frame_info *fi)
  1197. {
  1198. struct ieee802_11_elems elems;
  1199. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
  1200. printf("handle_beacon - too short payload (len=%lu)\n",
  1201. (unsigned long) len);
  1202. return;
  1203. }
  1204. (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1205. len - (IEEE80211_HDRLEN +
  1206. sizeof(mgmt->u.beacon)), &elems,
  1207. 0);
  1208. ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
  1209. }
  1210. #ifdef CONFIG_IEEE80211W
  1211. static void hostapd_sa_query_action(struct hostapd_data *hapd,
  1212. const struct ieee80211_mgmt *mgmt,
  1213. size_t len)
  1214. {
  1215. const u8 *end;
  1216. end = mgmt->u.action.u.sa_query_resp.trans_id +
  1217. WLAN_SA_QUERY_TR_ID_LEN;
  1218. if (((u8 *) mgmt) + len < end) {
  1219. wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
  1220. "frame (len=%lu)", (unsigned long) len);
  1221. return;
  1222. }
  1223. ieee802_11_sa_query_action(hapd, mgmt->sa,
  1224. mgmt->u.action.u.sa_query_resp.action,
  1225. mgmt->u.action.u.sa_query_resp.trans_id);
  1226. }
  1227. static int robust_action_frame(u8 category)
  1228. {
  1229. return category != WLAN_ACTION_PUBLIC &&
  1230. category != WLAN_ACTION_HT;
  1231. }
  1232. #endif /* CONFIG_IEEE80211W */
  1233. #ifdef CONFIG_WNM
  1234. static void hostapd_wnm_action(struct hostapd_data *hapd, struct sta_info *sta,
  1235. const struct ieee80211_mgmt *mgmt,
  1236. size_t len)
  1237. {
  1238. struct rx_action action;
  1239. if (len < IEEE80211_HDRLEN + 2)
  1240. return;
  1241. os_memset(&action, 0, sizeof(action));
  1242. action.da = mgmt->da;
  1243. action.sa = mgmt->sa;
  1244. action.bssid = mgmt->bssid;
  1245. action.category = mgmt->u.action.category;
  1246. action.data = (const u8 *) &mgmt->u.action.u.wnm_sleep_req.action;
  1247. action.len = len - IEEE80211_HDRLEN - 1;
  1248. action.freq = hapd->iface->freq;
  1249. ieee802_11_rx_wnm_action_ap(hapd, &action);
  1250. }
  1251. #endif /* CONFIG_WNM */
  1252. static void handle_action(struct hostapd_data *hapd,
  1253. const struct ieee80211_mgmt *mgmt, size_t len)
  1254. {
  1255. struct sta_info *sta;
  1256. sta = ap_get_sta(hapd, mgmt->sa);
  1257. if (len < IEEE80211_HDRLEN + 1) {
  1258. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1259. HOSTAPD_LEVEL_DEBUG,
  1260. "handle_action - too short payload (len=%lu)",
  1261. (unsigned long) len);
  1262. return;
  1263. }
  1264. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  1265. (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
  1266. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
  1267. "frame (category=%u) from unassociated STA " MACSTR,
  1268. MAC2STR(mgmt->sa), mgmt->u.action.category);
  1269. return;
  1270. }
  1271. #ifdef CONFIG_IEEE80211W
  1272. if (sta && (sta->flags & WLAN_STA_MFP) &&
  1273. !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
  1274. robust_action_frame(mgmt->u.action.category))) {
  1275. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1276. HOSTAPD_LEVEL_DEBUG,
  1277. "Dropped unprotected Robust Action frame from "
  1278. "an MFP STA");
  1279. return;
  1280. }
  1281. #endif /* CONFIG_IEEE80211W */
  1282. switch (mgmt->u.action.category) {
  1283. #ifdef CONFIG_IEEE80211R
  1284. case WLAN_ACTION_FT:
  1285. if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
  1286. len - IEEE80211_HDRLEN))
  1287. break;
  1288. return;
  1289. #endif /* CONFIG_IEEE80211R */
  1290. case WLAN_ACTION_WMM:
  1291. hostapd_wmm_action(hapd, mgmt, len);
  1292. return;
  1293. #ifdef CONFIG_IEEE80211W
  1294. case WLAN_ACTION_SA_QUERY:
  1295. hostapd_sa_query_action(hapd, mgmt, len);
  1296. return;
  1297. #endif /* CONFIG_IEEE80211W */
  1298. #ifdef CONFIG_WNM
  1299. case WLAN_ACTION_WNM:
  1300. hostapd_wnm_action(hapd, sta, mgmt, len);
  1301. return;
  1302. #endif /* CONFIG_WNM */
  1303. case WLAN_ACTION_PUBLIC:
  1304. if (hapd->public_action_cb) {
  1305. hapd->public_action_cb(hapd->public_action_cb_ctx,
  1306. (u8 *) mgmt, len,
  1307. hapd->iface->freq);
  1308. return;
  1309. }
  1310. break;
  1311. case WLAN_ACTION_VENDOR_SPECIFIC:
  1312. if (hapd->vendor_action_cb) {
  1313. if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
  1314. (u8 *) mgmt, len,
  1315. hapd->iface->freq) == 0)
  1316. return;
  1317. }
  1318. break;
  1319. }
  1320. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1321. HOSTAPD_LEVEL_DEBUG,
  1322. "handle_action - unknown action category %d or invalid "
  1323. "frame",
  1324. mgmt->u.action.category);
  1325. if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
  1326. !(mgmt->sa[0] & 0x01)) {
  1327. struct ieee80211_mgmt *resp;
  1328. /*
  1329. * IEEE 802.11-REVma/D9.0 - 7.3.1.11
  1330. * Return the Action frame to the source without change
  1331. * except that MSB of the Category set to 1.
  1332. */
  1333. wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
  1334. "frame back to sender");
  1335. resp = os_malloc(len);
  1336. if (resp == NULL)
  1337. return;
  1338. os_memcpy(resp, mgmt, len);
  1339. os_memcpy(resp->da, resp->sa, ETH_ALEN);
  1340. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  1341. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  1342. resp->u.action.category |= 0x80;
  1343. if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
  1344. wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
  1345. "Action frame");
  1346. }
  1347. os_free(resp);
  1348. }
  1349. }
  1350. /**
  1351. * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
  1352. * @hapd: hostapd BSS data structure (the BSS to which the management frame was
  1353. * sent to)
  1354. * @buf: management frame data (starting from IEEE 802.11 header)
  1355. * @len: length of frame data in octets
  1356. * @fi: meta data about received frame (signal level, etc.)
  1357. *
  1358. * Process all incoming IEEE 802.11 management frames. This will be called for
  1359. * each frame received from the kernel driver through wlan#ap interface. In
  1360. * addition, it can be called to re-inserted pending frames (e.g., when using
  1361. * external RADIUS server as an MAC ACL).
  1362. */
  1363. void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
  1364. struct hostapd_frame_info *fi)
  1365. {
  1366. struct ieee80211_mgmt *mgmt;
  1367. int broadcast;
  1368. u16 fc, stype;
  1369. if (len < 24)
  1370. return;
  1371. mgmt = (struct ieee80211_mgmt *) buf;
  1372. fc = le_to_host16(mgmt->frame_control);
  1373. stype = WLAN_FC_GET_STYPE(fc);
  1374. if (stype == WLAN_FC_STYPE_BEACON) {
  1375. handle_beacon(hapd, mgmt, len, fi);
  1376. return;
  1377. }
  1378. broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
  1379. mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
  1380. mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
  1381. if (!broadcast &&
  1382. #ifdef CONFIG_P2P
  1383. /* Invitation responses can be sent with the peer MAC as BSSID */
  1384. !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
  1385. stype == WLAN_FC_STYPE_ACTION) &&
  1386. #endif /* CONFIG_P2P */
  1387. os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
  1388. printf("MGMT: BSSID=" MACSTR " not our address\n",
  1389. MAC2STR(mgmt->bssid));
  1390. return;
  1391. }
  1392. if (stype == WLAN_FC_STYPE_PROBE_REQ) {
  1393. handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
  1394. return;
  1395. }
  1396. if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
  1397. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1398. HOSTAPD_LEVEL_DEBUG,
  1399. "MGMT: DA=" MACSTR " not our address",
  1400. MAC2STR(mgmt->da));
  1401. return;
  1402. }
  1403. switch (stype) {
  1404. case WLAN_FC_STYPE_AUTH:
  1405. wpa_printf(MSG_DEBUG, "mgmt::auth");
  1406. handle_auth(hapd, mgmt, len);
  1407. break;
  1408. case WLAN_FC_STYPE_ASSOC_REQ:
  1409. wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
  1410. handle_assoc(hapd, mgmt, len, 0);
  1411. break;
  1412. case WLAN_FC_STYPE_REASSOC_REQ:
  1413. wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
  1414. handle_assoc(hapd, mgmt, len, 1);
  1415. break;
  1416. case WLAN_FC_STYPE_DISASSOC:
  1417. wpa_printf(MSG_DEBUG, "mgmt::disassoc");
  1418. handle_disassoc(hapd, mgmt, len);
  1419. break;
  1420. case WLAN_FC_STYPE_DEAUTH:
  1421. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
  1422. handle_deauth(hapd, mgmt, len);
  1423. break;
  1424. case WLAN_FC_STYPE_ACTION:
  1425. wpa_printf(MSG_DEBUG, "mgmt::action");
  1426. handle_action(hapd, mgmt, len);
  1427. break;
  1428. default:
  1429. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  1430. HOSTAPD_LEVEL_DEBUG,
  1431. "unknown mgmt frame subtype %d", stype);
  1432. break;
  1433. }
  1434. }
  1435. static void handle_auth_cb(struct hostapd_data *hapd,
  1436. const struct ieee80211_mgmt *mgmt,
  1437. size_t len, int ok)
  1438. {
  1439. u16 auth_alg, auth_transaction, status_code;
  1440. struct sta_info *sta;
  1441. if (!ok) {
  1442. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1443. HOSTAPD_LEVEL_NOTICE,
  1444. "did not acknowledge authentication response");
  1445. return;
  1446. }
  1447. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
  1448. printf("handle_auth_cb - too short payload (len=%lu)\n",
  1449. (unsigned long) len);
  1450. return;
  1451. }
  1452. auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  1453. auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
  1454. status_code = le_to_host16(mgmt->u.auth.status_code);
  1455. sta = ap_get_sta(hapd, mgmt->da);
  1456. if (!sta) {
  1457. printf("handle_auth_cb: STA " MACSTR " not found\n",
  1458. MAC2STR(mgmt->da));
  1459. return;
  1460. }
  1461. if (status_code == WLAN_STATUS_SUCCESS &&
  1462. ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
  1463. (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
  1464. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1465. HOSTAPD_LEVEL_INFO, "authenticated");
  1466. sta->flags |= WLAN_STA_AUTH;
  1467. }
  1468. }
  1469. static void handle_assoc_cb(struct hostapd_data *hapd,
  1470. const struct ieee80211_mgmt *mgmt,
  1471. size_t len, int reassoc, int ok)
  1472. {
  1473. u16 status;
  1474. struct sta_info *sta;
  1475. int new_assoc = 1;
  1476. struct ieee80211_ht_capabilities ht_cap;
  1477. struct ieee80211_vht_capabilities vht_cap;
  1478. if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
  1479. sizeof(mgmt->u.assoc_resp))) {
  1480. printf("handle_assoc_cb(reassoc=%d) - too short payload "
  1481. "(len=%lu)\n", reassoc, (unsigned long) len);
  1482. return;
  1483. }
  1484. sta = ap_get_sta(hapd, mgmt->da);
  1485. if (!sta) {
  1486. printf("handle_assoc_cb: STA " MACSTR " not found\n",
  1487. MAC2STR(mgmt->da));
  1488. return;
  1489. }
  1490. if (!ok) {
  1491. hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
  1492. HOSTAPD_LEVEL_DEBUG,
  1493. "did not acknowledge association response");
  1494. sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
  1495. return;
  1496. }
  1497. if (reassoc)
  1498. status = le_to_host16(mgmt->u.reassoc_resp.status_code);
  1499. else
  1500. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1501. if (status != WLAN_STATUS_SUCCESS)
  1502. goto fail;
  1503. /* Stop previous accounting session, if one is started, and allocate
  1504. * new session id for the new session. */
  1505. accounting_sta_stop(hapd, sta);
  1506. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1507. HOSTAPD_LEVEL_INFO,
  1508. "associated (aid %d)",
  1509. sta->aid);
  1510. if (sta->flags & WLAN_STA_ASSOC)
  1511. new_assoc = 0;
  1512. sta->flags |= WLAN_STA_ASSOC;
  1513. if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
  1514. sta->auth_alg == WLAN_AUTH_FT) {
  1515. /*
  1516. * Open, static WEP, or FT protocol; no separate authorization
  1517. * step.
  1518. */
  1519. ap_sta_set_authorized(hapd, sta, 1);
  1520. }
  1521. if (reassoc)
  1522. mlme_reassociate_indication(hapd, sta);
  1523. else
  1524. mlme_associate_indication(hapd, sta);
  1525. #ifdef CONFIG_IEEE80211W
  1526. sta->sa_query_timed_out = 0;
  1527. #endif /* CONFIG_IEEE80211W */
  1528. /*
  1529. * Remove the STA entry in order to make sure the STA PS state gets
  1530. * cleared and configuration gets updated in case of reassociation back
  1531. * to the same AP.
  1532. */
  1533. hostapd_drv_sta_remove(hapd, sta->addr);
  1534. #ifdef CONFIG_IEEE80211N
  1535. if (sta->flags & WLAN_STA_HT)
  1536. hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
  1537. #endif /* CONFIG_IEEE80211N */
  1538. #ifdef CONFIG_IEEE80211AC
  1539. if (sta->flags & WLAN_STA_VHT)
  1540. hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
  1541. #endif /* CONFIG_IEEE80211AC */
  1542. if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
  1543. sta->supported_rates, sta->supported_rates_len,
  1544. sta->listen_interval,
  1545. sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
  1546. sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
  1547. sta->flags, sta->qosinfo)) {
  1548. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  1549. HOSTAPD_LEVEL_NOTICE,
  1550. "Could not add STA to kernel driver");
  1551. ap_sta_disconnect(hapd, sta, sta->addr,
  1552. WLAN_REASON_DISASSOC_AP_BUSY);
  1553. goto fail;
  1554. }
  1555. if (sta->flags & WLAN_STA_WDS)
  1556. hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
  1557. if (sta->eapol_sm == NULL) {
  1558. /*
  1559. * This STA does not use RADIUS server for EAP authentication,
  1560. * so bind it to the selected VLAN interface now, since the
  1561. * interface selection is not going to change anymore.
  1562. */
  1563. if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
  1564. goto fail;
  1565. } else if (sta->vlan_id) {
  1566. /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
  1567. if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
  1568. goto fail;
  1569. }
  1570. hostapd_set_sta_flags(hapd, sta);
  1571. if (sta->auth_alg == WLAN_AUTH_FT)
  1572. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  1573. else
  1574. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  1575. hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
  1576. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  1577. fail:
  1578. /* Copy of the association request is not needed anymore */
  1579. if (sta->last_assoc_req) {
  1580. os_free(sta->last_assoc_req);
  1581. sta->last_assoc_req = NULL;
  1582. }
  1583. }
  1584. static void handle_deauth_cb(struct hostapd_data *hapd,
  1585. const struct ieee80211_mgmt *mgmt,
  1586. size_t len, int ok)
  1587. {
  1588. struct sta_info *sta;
  1589. if (mgmt->da[0] & 0x01)
  1590. return;
  1591. sta = ap_get_sta(hapd, mgmt->da);
  1592. if (!sta) {
  1593. wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
  1594. " not found", MAC2STR(mgmt->da));
  1595. return;
  1596. }
  1597. if (ok)
  1598. wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
  1599. MAC2STR(sta->addr));
  1600. else
  1601. wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
  1602. "deauth", MAC2STR(sta->addr));
  1603. ap_sta_deauth_cb(hapd, sta);
  1604. }
  1605. static void handle_disassoc_cb(struct hostapd_data *hapd,
  1606. const struct ieee80211_mgmt *mgmt,
  1607. size_t len, int ok)
  1608. {
  1609. struct sta_info *sta;
  1610. if (mgmt->da[0] & 0x01)
  1611. return;
  1612. sta = ap_get_sta(hapd, mgmt->da);
  1613. if (!sta) {
  1614. wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
  1615. " not found", MAC2STR(mgmt->da));
  1616. return;
  1617. }
  1618. if (ok)
  1619. wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
  1620. MAC2STR(sta->addr));
  1621. else
  1622. wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
  1623. "disassoc", MAC2STR(sta->addr));
  1624. ap_sta_disassoc_cb(hapd, sta);
  1625. }
  1626. /**
  1627. * ieee802_11_mgmt_cb - Process management frame TX status callback
  1628. * @hapd: hostapd BSS data structure (the BSS from which the management frame
  1629. * was sent from)
  1630. * @buf: management frame data (starting from IEEE 802.11 header)
  1631. * @len: length of frame data in octets
  1632. * @stype: management frame subtype from frame control field
  1633. * @ok: Whether the frame was ACK'ed
  1634. */
  1635. void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
  1636. u16 stype, int ok)
  1637. {
  1638. const struct ieee80211_mgmt *mgmt;
  1639. mgmt = (const struct ieee80211_mgmt *) buf;
  1640. switch (stype) {
  1641. case WLAN_FC_STYPE_AUTH:
  1642. wpa_printf(MSG_DEBUG, "mgmt::auth cb");
  1643. handle_auth_cb(hapd, mgmt, len, ok);
  1644. break;
  1645. case WLAN_FC_STYPE_ASSOC_RESP:
  1646. wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
  1647. handle_assoc_cb(hapd, mgmt, len, 0, ok);
  1648. break;
  1649. case WLAN_FC_STYPE_REASSOC_RESP:
  1650. wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
  1651. handle_assoc_cb(hapd, mgmt, len, 1, ok);
  1652. break;
  1653. case WLAN_FC_STYPE_PROBE_RESP:
  1654. wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
  1655. break;
  1656. case WLAN_FC_STYPE_DEAUTH:
  1657. wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
  1658. handle_deauth_cb(hapd, mgmt, len, ok);
  1659. break;
  1660. case WLAN_FC_STYPE_DISASSOC:
  1661. wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
  1662. handle_disassoc_cb(hapd, mgmt, len, ok);
  1663. break;
  1664. case WLAN_FC_STYPE_ACTION:
  1665. wpa_printf(MSG_DEBUG, "mgmt::action cb");
  1666. break;
  1667. default:
  1668. printf("unknown mgmt cb frame subtype %d\n", stype);
  1669. break;
  1670. }
  1671. }
  1672. int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1673. {
  1674. /* TODO */
  1675. return 0;
  1676. }
  1677. int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1678. char *buf, size_t buflen)
  1679. {
  1680. /* TODO */
  1681. return 0;
  1682. }
  1683. void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
  1684. const u8 *buf, size_t len, int ack)
  1685. {
  1686. struct sta_info *sta;
  1687. struct hostapd_iface *iface = hapd->iface;
  1688. sta = ap_get_sta(hapd, addr);
  1689. if (sta == NULL && iface->num_bss > 1) {
  1690. size_t j;
  1691. for (j = 0; j < iface->num_bss; j++) {
  1692. hapd = iface->bss[j];
  1693. sta = ap_get_sta(hapd, addr);
  1694. if (sta)
  1695. break;
  1696. }
  1697. }
  1698. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
  1699. return;
  1700. if (sta->flags & WLAN_STA_PENDING_POLL) {
  1701. wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
  1702. "activity poll", MAC2STR(sta->addr),
  1703. ack ? "ACKed" : "did not ACK");
  1704. if (ack)
  1705. sta->flags &= ~WLAN_STA_PENDING_POLL;
  1706. }
  1707. ieee802_1x_tx_status(hapd, sta, buf, len, ack);
  1708. }
  1709. void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
  1710. const u8 *data, size_t len, int ack)
  1711. {
  1712. struct sta_info *sta;
  1713. struct hostapd_iface *iface = hapd->iface;
  1714. sta = ap_get_sta(hapd, dst);
  1715. if (sta == NULL && iface->num_bss > 1) {
  1716. size_t j;
  1717. for (j = 0; j < iface->num_bss; j++) {
  1718. hapd = iface->bss[j];
  1719. sta = ap_get_sta(hapd, dst);
  1720. if (sta)
  1721. break;
  1722. }
  1723. }
  1724. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  1725. wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
  1726. MACSTR " that is not currently associated",
  1727. MAC2STR(dst));
  1728. return;
  1729. }
  1730. ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
  1731. }
  1732. void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
  1733. {
  1734. struct sta_info *sta;
  1735. struct hostapd_iface *iface = hapd->iface;
  1736. sta = ap_get_sta(hapd, addr);
  1737. if (sta == NULL && iface->num_bss > 1) {
  1738. size_t j;
  1739. for (j = 0; j < iface->num_bss; j++) {
  1740. hapd = iface->bss[j];
  1741. sta = ap_get_sta(hapd, addr);
  1742. if (sta)
  1743. break;
  1744. }
  1745. }
  1746. if (sta == NULL)
  1747. return;
  1748. if (!(sta->flags & WLAN_STA_PENDING_POLL))
  1749. return;
  1750. wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
  1751. "activity poll", MAC2STR(sta->addr));
  1752. sta->flags &= ~WLAN_STA_PENDING_POLL;
  1753. }
  1754. void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
  1755. int wds)
  1756. {
  1757. struct sta_info *sta;
  1758. sta = ap_get_sta(hapd, src);
  1759. if (sta && (sta->flags & WLAN_STA_ASSOC)) {
  1760. if (!hapd->conf->wds_sta)
  1761. return;
  1762. if (wds && !(sta->flags & WLAN_STA_WDS)) {
  1763. wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
  1764. "STA " MACSTR " (aid %u)",
  1765. MAC2STR(sta->addr), sta->aid);
  1766. sta->flags |= WLAN_STA_WDS;
  1767. hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
  1768. }
  1769. return;
  1770. }
  1771. wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
  1772. MACSTR, MAC2STR(src));
  1773. if (src[0] & 0x01) {
  1774. /* Broadcast bit set in SA?! Ignore the frame silently. */
  1775. return;
  1776. }
  1777. if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
  1778. wpa_printf(MSG_DEBUG, "Association Response to the STA has "
  1779. "already been sent, but no TX status yet known - "
  1780. "ignore Class 3 frame issue with " MACSTR,
  1781. MAC2STR(src));
  1782. return;
  1783. }
  1784. if (sta && (sta->flags & WLAN_STA_AUTH))
  1785. hostapd_drv_sta_disassoc(
  1786. hapd, src,
  1787. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  1788. else
  1789. hostapd_drv_sta_deauth(
  1790. hapd, src,
  1791. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  1792. }
  1793. #endif /* CONFIG_NATIVE_WINDOWS */