ieee802_11_auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 "common.h"
  22. #include "hostapd.h"
  23. #include "config.h"
  24. #include "ieee802_11.h"
  25. #include "ieee802_11_auth.h"
  26. #include "radius/radius.h"
  27. #include "radius/radius_client.h"
  28. #include "eloop.h"
  29. #define RADIUS_ACL_TIMEOUT 30
  30. struct hostapd_cached_radius_acl {
  31. time_t timestamp;
  32. macaddr addr;
  33. int accepted; /* HOSTAPD_ACL_* */
  34. struct hostapd_cached_radius_acl *next;
  35. u32 session_timeout;
  36. u32 acct_interim_interval;
  37. int vlan_id;
  38. };
  39. struct hostapd_acl_query_data {
  40. time_t timestamp;
  41. u8 radius_id;
  42. macaddr addr;
  43. u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
  44. size_t auth_msg_len;
  45. struct hostapd_acl_query_data *next;
  46. };
  47. #ifndef CONFIG_NO_RADIUS
  48. static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
  49. {
  50. struct hostapd_cached_radius_acl *prev;
  51. while (acl_cache) {
  52. prev = acl_cache;
  53. acl_cache = acl_cache->next;
  54. os_free(prev);
  55. }
  56. }
  57. static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
  58. u32 *session_timeout,
  59. u32 *acct_interim_interval, int *vlan_id)
  60. {
  61. struct hostapd_cached_radius_acl *entry;
  62. time_t now;
  63. time(&now);
  64. entry = hapd->acl_cache;
  65. while (entry) {
  66. if (os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
  67. if (now - entry->timestamp > RADIUS_ACL_TIMEOUT)
  68. return -1; /* entry has expired */
  69. if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
  70. if (session_timeout)
  71. *session_timeout =
  72. entry->session_timeout;
  73. if (acct_interim_interval)
  74. *acct_interim_interval =
  75. entry->acct_interim_interval;
  76. if (vlan_id)
  77. *vlan_id = entry->vlan_id;
  78. return entry->accepted;
  79. }
  80. entry = entry->next;
  81. }
  82. return -1;
  83. }
  84. #endif /* CONFIG_NO_RADIUS */
  85. static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
  86. {
  87. if (query == NULL)
  88. return;
  89. os_free(query->auth_msg);
  90. os_free(query);
  91. }
  92. #ifndef CONFIG_NO_RADIUS
  93. static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
  94. struct hostapd_acl_query_data *query)
  95. {
  96. struct radius_msg *msg;
  97. char buf[128];
  98. query->radius_id = radius_client_get_id(hapd->radius);
  99. msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
  100. if (msg == NULL)
  101. return -1;
  102. radius_msg_make_authenticator(msg, addr, ETH_ALEN);
  103. os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
  104. if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
  105. os_strlen(buf))) {
  106. wpa_printf(MSG_DEBUG, "Could not add User-Name");
  107. goto fail;
  108. }
  109. if (!radius_msg_add_attr_user_password(
  110. msg, (u8 *) buf, os_strlen(buf),
  111. hapd->conf->radius->auth_server->shared_secret,
  112. hapd->conf->radius->auth_server->shared_secret_len)) {
  113. wpa_printf(MSG_DEBUG, "Could not add User-Password");
  114. goto fail;
  115. }
  116. if (hapd->conf->own_ip_addr.af == AF_INET &&
  117. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  118. (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
  119. wpa_printf(MSG_DEBUG, "Could not add NAS-IP-Address");
  120. goto fail;
  121. }
  122. #ifdef CONFIG_IPV6
  123. if (hapd->conf->own_ip_addr.af == AF_INET6 &&
  124. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
  125. (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
  126. wpa_printf(MSG_DEBUG, "Could not add NAS-IPv6-Address");
  127. goto fail;
  128. }
  129. #endif /* CONFIG_IPV6 */
  130. if (hapd->conf->nas_identifier &&
  131. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
  132. (u8 *) hapd->conf->nas_identifier,
  133. os_strlen(hapd->conf->nas_identifier))) {
  134. wpa_printf(MSG_DEBUG, "Could not add NAS-Identifier");
  135. goto fail;
  136. }
  137. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
  138. MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
  139. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
  140. (u8 *) buf, os_strlen(buf))) {
  141. wpa_printf(MSG_DEBUG, "Could not add Called-Station-Id");
  142. goto fail;
  143. }
  144. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
  145. MAC2STR(addr));
  146. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
  147. (u8 *) buf, os_strlen(buf))) {
  148. wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
  149. goto fail;
  150. }
  151. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
  152. RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
  153. wpa_printf(MSG_DEBUG, "Could not add NAS-Port-Type");
  154. goto fail;
  155. }
  156. os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
  157. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
  158. (u8 *) buf, os_strlen(buf))) {
  159. wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
  160. goto fail;
  161. }
  162. radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr);
  163. return 0;
  164. fail:
  165. radius_msg_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. hapd->drv.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. struct radius_hdr *hdr = radius_msg_get_hdr(msg);
  338. query = hapd->acl_queries;
  339. prev = NULL;
  340. while (query) {
  341. if (query->radius_id == hdr->identifier)
  342. break;
  343. prev = query;
  344. query = query->next;
  345. }
  346. if (query == NULL)
  347. return RADIUS_RX_UNKNOWN;
  348. wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
  349. "message (id=%d)", query->radius_id);
  350. if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
  351. wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
  352. "correct authenticator - dropped\n");
  353. return RADIUS_RX_INVALID_AUTHENTICATOR;
  354. }
  355. if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  356. hdr->code != RADIUS_CODE_ACCESS_REJECT) {
  357. wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
  358. "query", hdr->code);
  359. return RADIUS_RX_UNKNOWN;
  360. }
  361. /* Insert Accept/Reject info into ACL cache */
  362. cache = os_zalloc(sizeof(*cache));
  363. if (cache == NULL) {
  364. wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
  365. goto done;
  366. }
  367. time(&cache->timestamp);
  368. os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
  369. if (hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
  370. if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
  371. &cache->session_timeout) == 0)
  372. cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
  373. else
  374. cache->accepted = HOSTAPD_ACL_ACCEPT;
  375. if (radius_msg_get_attr_int32(
  376. msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
  377. &cache->acct_interim_interval) == 0 &&
  378. cache->acct_interim_interval < 60) {
  379. wpa_printf(MSG_DEBUG, "Ignored too small "
  380. "Acct-Interim-Interval %d for STA " MACSTR,
  381. cache->acct_interim_interval,
  382. MAC2STR(query->addr));
  383. cache->acct_interim_interval = 0;
  384. }
  385. cache->vlan_id = radius_msg_get_vlanid(msg);
  386. } else
  387. cache->accepted = HOSTAPD_ACL_REJECT;
  388. cache->next = hapd->acl_cache;
  389. hapd->acl_cache = cache;
  390. #ifdef CONFIG_DRIVER_RADIUS_ACL
  391. hapd->drv.set_radius_acl_auth(hapd, query->addr, cache->accepted,
  392. cache->session_timeout);
  393. #else /* CONFIG_DRIVER_RADIUS_ACL */
  394. #ifdef NEED_AP_MLME
  395. /* Re-send original authentication frame for 802.11 processing */
  396. wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
  397. "successful RADIUS ACL query");
  398. ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len, NULL);
  399. #endif /* NEED_AP_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. #endif /* CONFIG_NATIVE_WINDOWS */