ieee802_11_auth.c 14 KB

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