ieee802_11_auth.c 14 KB

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