ieee802_11_auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * hostapd / IEEE 802.11 authentication (ACL)
  3. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * Access control list for IEEE 802.11 authentication can uses statically
  15. * configured ACL from configuration files or an external RADIUS server.
  16. * Results from external RADIUS queries are cached to allow faster
  17. * authentication frame processing.
  18. */
  19. #include "includes.h"
  20. #ifndef CONFIG_NATIVE_WINDOWS
  21. #include "hostapd.h"
  22. #include "config.h"
  23. #include "ieee802_11.h"
  24. #include "ieee802_11_auth.h"
  25. #include "radius/radius.h"
  26. #include "radius/radius_client.h"
  27. #include "eloop.h"
  28. #ifdef CONFIG_DRIVER_RADIUS_ACL
  29. #include "driver_i.h"
  30. #endif /* CONFIG_DRIVER_RADIUS_ACL */
  31. #define RADIUS_ACL_TIMEOUT 30
  32. struct hostapd_cached_radius_acl {
  33. time_t timestamp;
  34. macaddr addr;
  35. int accepted; /* HOSTAPD_ACL_* */
  36. struct hostapd_cached_radius_acl *next;
  37. u32 session_timeout;
  38. u32 acct_interim_interval;
  39. int vlan_id;
  40. };
  41. struct hostapd_acl_query_data {
  42. time_t timestamp;
  43. u8 radius_id;
  44. macaddr addr;
  45. u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
  46. size_t auth_msg_len;
  47. struct hostapd_acl_query_data *next;
  48. };
  49. #ifndef CONFIG_NO_RADIUS
  50. static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
  51. {
  52. struct hostapd_cached_radius_acl *prev;
  53. while (acl_cache) {
  54. prev = acl_cache;
  55. acl_cache = acl_cache->next;
  56. os_free(prev);
  57. }
  58. }
  59. static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
  60. u32 *session_timeout,
  61. u32 *acct_interim_interval, int *vlan_id)
  62. {
  63. struct hostapd_cached_radius_acl *entry;
  64. time_t now;
  65. time(&now);
  66. entry = hapd->acl_cache;
  67. while (entry) {
  68. if (os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
  69. if (now - entry->timestamp > RADIUS_ACL_TIMEOUT)
  70. return -1; /* entry has expired */
  71. if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  72. if (session_timeout)
  73. *session_timeout =
  74. entry->session_timeout;
  75. if (acct_interim_interval)
  76. *acct_interim_interval =
  77. entry->acct_interim_interval;
  78. if (vlan_id)
  79. *vlan_id = entry->vlan_id;
  80. return entry->accepted;
  81. }
  82. entry = entry->next;
  83. }
  84. return -1;
  85. }
  86. #endif /* CONFIG_NO_RADIUS */
  87. static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
  88. {
  89. if (query == NULL)
  90. return;
  91. os_free(query->auth_msg);
  92. os_free(query);
  93. }
  94. #ifndef CONFIG_NO_RADIUS
  95. static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
  96. struct hostapd_acl_query_data *query)
  97. {
  98. struct radius_msg *msg;
  99. char buf[128];
  100. query->radius_id = radius_client_get_id(hapd->radius);
  101. msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
  102. if (msg == NULL)
  103. return -1;
  104. radius_msg_make_authenticator(msg, addr, ETH_ALEN);
  105. os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
  106. if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
  107. os_strlen(buf))) {
  108. wpa_printf(MSG_DEBUG, "Could not add User-Name");
  109. goto fail;
  110. }
  111. if (!radius_msg_add_attr_user_password(
  112. msg, (u8 *) buf, os_strlen(buf),
  113. hapd->conf->radius->auth_server->shared_secret,
  114. hapd->conf->radius->auth_server->shared_secret_len)) {
  115. wpa_printf(MSG_DEBUG, "Could not add User-Password");
  116. goto fail;
  117. }
  118. if (hapd->conf->own_ip_addr.af == AF_INET &&
  119. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  120. (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
  121. wpa_printf(MSG_DEBUG, "Could not add NAS-IP-Address");
  122. goto fail;
  123. }
  124. #ifdef CONFIG_IPV6
  125. if (hapd->conf->own_ip_addr.af == AF_INET6 &&
  126. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
  127. (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
  128. wpa_printf(MSG_DEBUG, "Could not add NAS-IPv6-Address");
  129. goto fail;
  130. }
  131. #endif /* CONFIG_IPV6 */
  132. if (hapd->conf->nas_identifier &&
  133. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
  134. (u8 *) hapd->conf->nas_identifier,
  135. os_strlen(hapd->conf->nas_identifier))) {
  136. wpa_printf(MSG_DEBUG, "Could not add NAS-Identifier");
  137. goto fail;
  138. }
  139. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
  140. MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
  141. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
  142. (u8 *) buf, os_strlen(buf))) {
  143. wpa_printf(MSG_DEBUG, "Could not add Called-Station-Id");
  144. goto fail;
  145. }
  146. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
  147. MAC2STR(addr));
  148. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
  149. (u8 *) buf, os_strlen(buf))) {
  150. wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
  151. goto fail;
  152. }
  153. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
  154. RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
  155. wpa_printf(MSG_DEBUG, "Could not add NAS-Port-Type");
  156. goto fail;
  157. }
  158. os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
  159. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
  160. (u8 *) buf, os_strlen(buf))) {
  161. wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
  162. goto fail;
  163. }
  164. radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr);
  165. return 0;
  166. fail:
  167. radius_msg_free(msg);
  168. os_free(msg);
  169. return -1;
  170. }
  171. #endif /* CONFIG_NO_RADIUS */
  172. /**
  173. * hostapd_allowed_address - Check whether a specified STA can be authenticated
  174. * @hapd: hostapd BSS data
  175. * @addr: MAC address of the STA
  176. * @msg: Authentication message
  177. * @len: Length of msg in octets
  178. * @session_timeout: Buffer for returning session timeout (from RADIUS)
  179. * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
  180. * @vlan_id: Buffer for returning VLAN ID
  181. * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
  182. */
  183. int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
  184. const u8 *msg, size_t len, u32 *session_timeout,
  185. u32 *acct_interim_interval, int *vlan_id)
  186. {
  187. if (session_timeout)
  188. *session_timeout = 0;
  189. if (acct_interim_interval)
  190. *acct_interim_interval = 0;
  191. if (vlan_id)
  192. *vlan_id = 0;
  193. if (hostapd_maclist_found(hapd->conf->accept_mac,
  194. hapd->conf->num_accept_mac, addr, vlan_id))
  195. return HOSTAPD_ACL_ACCEPT;
  196. if (hostapd_maclist_found(hapd->conf->deny_mac,
  197. hapd->conf->num_deny_mac, addr, vlan_id))
  198. return HOSTAPD_ACL_REJECT;
  199. if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
  200. return HOSTAPD_ACL_ACCEPT;
  201. if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
  202. return HOSTAPD_ACL_REJECT;
  203. if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
  204. #ifdef CONFIG_NO_RADIUS
  205. return HOSTAPD_ACL_REJECT;
  206. #else /* CONFIG_NO_RADIUS */
  207. struct hostapd_acl_query_data *query;
  208. /* Check whether ACL cache has an entry for this station */
  209. int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
  210. acct_interim_interval,
  211. vlan_id);
  212. if (res == HOSTAPD_ACL_ACCEPT ||
  213. res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  214. return res;
  215. if (res == HOSTAPD_ACL_REJECT)
  216. return HOSTAPD_ACL_REJECT;
  217. query = hapd->acl_queries;
  218. while (query) {
  219. if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
  220. /* pending query in RADIUS retransmit queue;
  221. * do not generate a new one */
  222. return HOSTAPD_ACL_PENDING;
  223. }
  224. query = query->next;
  225. }
  226. if (!hapd->conf->radius->auth_server)
  227. return HOSTAPD_ACL_REJECT;
  228. /* No entry in the cache - query external RADIUS server */
  229. query = os_zalloc(sizeof(*query));
  230. if (query == NULL) {
  231. wpa_printf(MSG_ERROR, "malloc for query data failed");
  232. return HOSTAPD_ACL_REJECT;
  233. }
  234. time(&query->timestamp);
  235. os_memcpy(query->addr, addr, ETH_ALEN);
  236. if (hostapd_radius_acl_query(hapd, addr, query)) {
  237. wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
  238. "for ACL query.");
  239. hostapd_acl_query_free(query);
  240. return HOSTAPD_ACL_REJECT;
  241. }
  242. query->auth_msg = os_malloc(len);
  243. if (query->auth_msg == NULL) {
  244. wpa_printf(MSG_ERROR, "Failed to allocate memory for "
  245. "auth frame.");
  246. hostapd_acl_query_free(query);
  247. return HOSTAPD_ACL_REJECT;
  248. }
  249. os_memcpy(query->auth_msg, msg, len);
  250. query->auth_msg_len = len;
  251. query->next = hapd->acl_queries;
  252. hapd->acl_queries = query;
  253. /* Queued data will be processed in hostapd_acl_recv_radius()
  254. * when RADIUS server replies to the sent Access-Request. */
  255. return HOSTAPD_ACL_PENDING;
  256. #endif /* CONFIG_NO_RADIUS */
  257. }
  258. return HOSTAPD_ACL_REJECT;
  259. }
  260. #ifndef CONFIG_NO_RADIUS
  261. static void hostapd_acl_expire_cache(struct hostapd_data *hapd, time_t now)
  262. {
  263. struct hostapd_cached_radius_acl *prev, *entry, *tmp;
  264. prev = NULL;
  265. entry = hapd->acl_cache;
  266. while (entry) {
  267. if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
  268. wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
  269. " has expired.", MAC2STR(entry->addr));
  270. if (prev)
  271. prev->next = entry->next;
  272. else
  273. hapd->acl_cache = entry->next;
  274. #ifdef CONFIG_DRIVER_RADIUS_ACL
  275. hostapd_set_radius_acl_expire(hapd, entry->addr);
  276. #endif /* CONFIG_DRIVER_RADIUS_ACL */
  277. tmp = entry;
  278. entry = entry->next;
  279. os_free(tmp);
  280. continue;
  281. }
  282. prev = entry;
  283. entry = entry->next;
  284. }
  285. }
  286. static void hostapd_acl_expire_queries(struct hostapd_data *hapd, time_t now)
  287. {
  288. struct hostapd_acl_query_data *prev, *entry, *tmp;
  289. prev = NULL;
  290. entry = hapd->acl_queries;
  291. while (entry) {
  292. if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
  293. wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
  294. " has expired.", MAC2STR(entry->addr));
  295. if (prev)
  296. prev->next = entry->next;
  297. else
  298. hapd->acl_queries = entry->next;
  299. tmp = entry;
  300. entry = entry->next;
  301. hostapd_acl_query_free(tmp);
  302. continue;
  303. }
  304. prev = entry;
  305. entry = entry->next;
  306. }
  307. }
  308. /**
  309. * hostapd_acl_expire - ACL cache expiration callback
  310. * @eloop_ctx: struct hostapd_data *
  311. * @timeout_ctx: Not used
  312. */
  313. static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
  314. {
  315. struct hostapd_data *hapd = eloop_ctx;
  316. time_t now;
  317. time(&now);
  318. hostapd_acl_expire_cache(hapd, now);
  319. hostapd_acl_expire_queries(hapd, now);
  320. eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
  321. }
  322. /**
  323. * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
  324. * @msg: RADIUS response message
  325. * @req: RADIUS request message
  326. * @shared_secret: RADIUS shared secret
  327. * @shared_secret_len: Length of shared_secret in octets
  328. * @data: Context data (struct hostapd_data *)
  329. * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
  330. * was processed here) or RADIUS_RX_UNKNOWN if not.
  331. */
  332. static RadiusRxResult
  333. hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
  334. const u8 *shared_secret, size_t shared_secret_len,
  335. void *data)
  336. {
  337. struct hostapd_data *hapd = data;
  338. struct hostapd_acl_query_data *query, *prev;
  339. struct hostapd_cached_radius_acl *cache;
  340. query = hapd->acl_queries;
  341. prev = NULL;
  342. while (query) {
  343. if (query->radius_id == msg->hdr->identifier)
  344. break;
  345. prev = query;
  346. query = query->next;
  347. }
  348. if (query == NULL)
  349. return RADIUS_RX_UNKNOWN;
  350. wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
  351. "message (id=%d)", query->radius_id);
  352. if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
  353. wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
  354. "correct authenticator - dropped\n");
  355. return RADIUS_RX_INVALID_AUTHENTICATOR;
  356. }
  357. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  358. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT) {
  359. wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
  360. "query", msg->hdr->code);
  361. return RADIUS_RX_UNKNOWN;
  362. }
  363. /* Insert Accept/Reject info into ACL cache */
  364. cache = os_zalloc(sizeof(*cache));
  365. if (cache == NULL) {
  366. wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
  367. goto done;
  368. }
  369. time(&cache->timestamp);
  370. os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
  371. if (msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
  372. if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
  373. &cache->session_timeout) == 0)
  374. cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
  375. else
  376. cache->accepted = HOSTAPD_ACL_ACCEPT;
  377. if (radius_msg_get_attr_int32(
  378. msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
  379. &cache->acct_interim_interval) == 0 &&
  380. cache->acct_interim_interval < 60) {
  381. wpa_printf(MSG_DEBUG, "Ignored too small "
  382. "Acct-Interim-Interval %d for STA " MACSTR,
  383. cache->acct_interim_interval,
  384. MAC2STR(query->addr));
  385. cache->acct_interim_interval = 0;
  386. }
  387. cache->vlan_id = radius_msg_get_vlanid(msg);
  388. } else
  389. cache->accepted = HOSTAPD_ACL_REJECT;
  390. cache->next = hapd->acl_cache;
  391. hapd->acl_cache = cache;
  392. #ifdef CONFIG_DRIVER_RADIUS_ACL
  393. hostapd_set_radius_acl_auth(hapd, query->addr, cache->accepted,
  394. cache->session_timeout);
  395. #else /* CONFIG_DRIVER_RADIUS_ACL */
  396. #ifdef NEED_AP_MLME
  397. /* Re-send original authentication frame for 802.11 processing */
  398. wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
  399. "successful RADIUS ACL query");
  400. ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len,
  401. WLAN_FC_STYPE_AUTH, NULL);
  402. #endif /* NEED_AP_MLME */
  403. #endif /* CONFIG_DRIVER_RADIUS_ACL */
  404. done:
  405. if (prev == NULL)
  406. hapd->acl_queries = query->next;
  407. else
  408. prev->next = query->next;
  409. hostapd_acl_query_free(query);
  410. return RADIUS_RX_PROCESSED;
  411. }
  412. #endif /* CONFIG_NO_RADIUS */
  413. /**
  414. * hostapd_acl_init: Initialize IEEE 802.11 ACL
  415. * @hapd: hostapd BSS data
  416. * Returns: 0 on success, -1 on failure
  417. */
  418. int hostapd_acl_init(struct hostapd_data *hapd)
  419. {
  420. #ifndef CONFIG_NO_RADIUS
  421. if (radius_client_register(hapd->radius, RADIUS_AUTH,
  422. hostapd_acl_recv_radius, hapd))
  423. return -1;
  424. eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
  425. #endif /* CONFIG_NO_RADIUS */
  426. return 0;
  427. }
  428. /**
  429. * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
  430. * @hapd: hostapd BSS data
  431. */
  432. void hostapd_acl_deinit(struct hostapd_data *hapd)
  433. {
  434. struct hostapd_acl_query_data *query, *prev;
  435. #ifndef CONFIG_NO_RADIUS
  436. eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
  437. hostapd_acl_cache_free(hapd->acl_cache);
  438. #endif /* CONFIG_NO_RADIUS */
  439. query = hapd->acl_queries;
  440. while (query) {
  441. prev = query;
  442. query = query->next;
  443. hostapd_acl_query_free(prev);
  444. }
  445. }
  446. int hostapd_acl_reconfig(struct hostapd_data *hapd,
  447. struct hostapd_config *oldconf)
  448. {
  449. if (!hapd->radius_client_reconfigured)
  450. return 0;
  451. hostapd_acl_deinit(hapd);
  452. return hostapd_acl_init(hapd);
  453. }
  454. #endif /* CONFIG_NATIVE_WINDOWS */