ieee802_11_auth.c 14 KB

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