ieee802_11_auth.c 12 KB

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