ieee802_11_auth.c 12 KB

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