PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/hostapd-1.0/src/ap/accounting.c

#
C | 510 lines | 378 code | 79 blank | 53 comment | 66 complexity | b95f2f7cb3cc5d4f6f999b61eab26ce3 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * hostapd / RADIUS Accounting
  3. * Copyright (c) 2002-2009, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "drivers/driver.h"
  18. #include "radius/radius.h"
  19. #include "radius/radius_client.h"
  20. #include "hostapd.h"
  21. #include "ieee802_1x.h"
  22. #include "ap_config.h"
  23. #include "sta_info.h"
  24. #include "ap_drv_ops.h"
  25. #include "accounting.h"
  26. /* Default interval in seconds for polling TX/RX octets from the driver if
  27. * STA is not using interim accounting. This detects wrap arounds for
  28. * input/output octets and updates Acct-{Input,Output}-Gigawords. */
  29. #define ACCT_DEFAULT_UPDATE_INTERVAL 300
  30. static void accounting_sta_get_id(struct hostapd_data *hapd,
  31. struct sta_info *sta);
  32. static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
  33. struct sta_info *sta,
  34. int status_type)
  35. {
  36. struct radius_msg *msg;
  37. char buf[128];
  38. u8 *val;
  39. size_t len;
  40. int i;
  41. msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
  42. radius_client_get_id(hapd->radius));
  43. if (msg == NULL) {
  44. printf("Could not create net RADIUS packet\n");
  45. return NULL;
  46. }
  47. if (sta) {
  48. radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
  49. os_snprintf(buf, sizeof(buf), "%08X-%08X",
  50. sta->acct_session_id_hi, sta->acct_session_id_lo);
  51. if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
  52. (u8 *) buf, os_strlen(buf))) {
  53. printf("Could not add Acct-Session-Id\n");
  54. goto fail;
  55. }
  56. } else {
  57. radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
  58. }
  59. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
  60. status_type)) {
  61. printf("Could not add Acct-Status-Type\n");
  62. goto fail;
  63. }
  64. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
  65. hapd->conf->ieee802_1x ?
  66. RADIUS_ACCT_AUTHENTIC_RADIUS :
  67. RADIUS_ACCT_AUTHENTIC_LOCAL)) {
  68. printf("Could not add Acct-Authentic\n");
  69. goto fail;
  70. }
  71. if (sta) {
  72. val = ieee802_1x_get_identity(sta->eapol_sm, &len);
  73. if (!val) {
  74. os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
  75. MAC2STR(sta->addr));
  76. val = (u8 *) buf;
  77. len = os_strlen(buf);
  78. }
  79. if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
  80. len)) {
  81. printf("Could not add User-Name\n");
  82. goto fail;
  83. }
  84. }
  85. if (hapd->conf->own_ip_addr.af == AF_INET &&
  86. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  87. (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
  88. printf("Could not add NAS-IP-Address\n");
  89. goto fail;
  90. }
  91. #ifdef CONFIG_IPV6
  92. if (hapd->conf->own_ip_addr.af == AF_INET6 &&
  93. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
  94. (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
  95. printf("Could not add NAS-IPv6-Address\n");
  96. goto fail;
  97. }
  98. #endif /* CONFIG_IPV6 */
  99. if (hapd->conf->nas_identifier &&
  100. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
  101. (u8 *) hapd->conf->nas_identifier,
  102. os_strlen(hapd->conf->nas_identifier))) {
  103. printf("Could not add NAS-Identifier\n");
  104. goto fail;
  105. }
  106. if (sta &&
  107. !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
  108. printf("Could not add NAS-Port\n");
  109. goto fail;
  110. }
  111. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
  112. MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
  113. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
  114. (u8 *) buf, os_strlen(buf))) {
  115. printf("Could not add Called-Station-Id\n");
  116. goto fail;
  117. }
  118. if (sta) {
  119. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
  120. MAC2STR(sta->addr));
  121. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
  122. (u8 *) buf, os_strlen(buf))) {
  123. printf("Could not add Calling-Station-Id\n");
  124. goto fail;
  125. }
  126. if (!radius_msg_add_attr_int32(
  127. msg, RADIUS_ATTR_NAS_PORT_TYPE,
  128. RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
  129. printf("Could not add NAS-Port-Type\n");
  130. goto fail;
  131. }
  132. os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
  133. radius_sta_rate(hapd, sta) / 2,
  134. (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
  135. radius_mode_txt(hapd));
  136. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
  137. (u8 *) buf, os_strlen(buf))) {
  138. printf("Could not add Connect-Info\n");
  139. goto fail;
  140. }
  141. for (i = 0; ; i++) {
  142. val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
  143. i);
  144. if (val == NULL)
  145. break;
  146. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
  147. val, len)) {
  148. printf("Could not add Class\n");
  149. goto fail;
  150. }
  151. }
  152. }
  153. return msg;
  154. fail:
  155. radius_msg_free(msg);
  156. return NULL;
  157. }
  158. static int accounting_sta_update_stats(struct hostapd_data *hapd,
  159. struct sta_info *sta,
  160. struct hostap_sta_driver_data *data)
  161. {
  162. if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
  163. return -1;
  164. if (sta->last_rx_bytes > data->rx_bytes)
  165. sta->acct_input_gigawords++;
  166. if (sta->last_tx_bytes > data->tx_bytes)
  167. sta->acct_output_gigawords++;
  168. sta->last_rx_bytes = data->rx_bytes;
  169. sta->last_tx_bytes = data->tx_bytes;
  170. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  171. HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
  172. "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
  173. "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
  174. sta->last_rx_bytes, sta->acct_input_gigawords,
  175. sta->last_tx_bytes, sta->acct_output_gigawords);
  176. return 0;
  177. }
  178. static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
  179. {
  180. struct hostapd_data *hapd = eloop_ctx;
  181. struct sta_info *sta = timeout_ctx;
  182. int interval;
  183. if (sta->acct_interim_interval) {
  184. accounting_sta_interim(hapd, sta);
  185. interval = sta->acct_interim_interval;
  186. } else {
  187. struct hostap_sta_driver_data data;
  188. accounting_sta_update_stats(hapd, sta, &data);
  189. interval = ACCT_DEFAULT_UPDATE_INTERVAL;
  190. }
  191. eloop_register_timeout(interval, 0, accounting_interim_update,
  192. hapd, sta);
  193. }
  194. /**
  195. * accounting_sta_start - Start STA accounting
  196. * @hapd: hostapd BSS data
  197. * @sta: The station
  198. */
  199. void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
  200. {
  201. struct radius_msg *msg;
  202. struct os_time t;
  203. int interval;
  204. if (sta->acct_session_started)
  205. return;
  206. accounting_sta_get_id(hapd, sta);
  207. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  208. HOSTAPD_LEVEL_INFO,
  209. "starting accounting session %08X-%08X",
  210. sta->acct_session_id_hi, sta->acct_session_id_lo);
  211. os_get_time(&t);
  212. sta->acct_session_start = t.sec;
  213. sta->last_rx_bytes = sta->last_tx_bytes = 0;
  214. sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
  215. hostapd_drv_sta_clear_stats(hapd, sta->addr);
  216. if (!hapd->conf->radius->acct_server)
  217. return;
  218. if (sta->acct_interim_interval)
  219. interval = sta->acct_interim_interval;
  220. else
  221. interval = ACCT_DEFAULT_UPDATE_INTERVAL;
  222. eloop_register_timeout(interval, 0, accounting_interim_update,
  223. hapd, sta);
  224. msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
  225. if (msg &&
  226. radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
  227. radius_msg_free(msg);
  228. sta->acct_session_started = 1;
  229. }
  230. static void accounting_sta_report(struct hostapd_data *hapd,
  231. struct sta_info *sta, int stop)
  232. {
  233. struct radius_msg *msg;
  234. int cause = sta->acct_terminate_cause;
  235. struct hostap_sta_driver_data data;
  236. struct os_time now;
  237. u32 gigawords;
  238. if (!hapd->conf->radius->acct_server)
  239. return;
  240. msg = accounting_msg(hapd, sta,
  241. stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
  242. RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
  243. if (!msg) {
  244. printf("Could not create RADIUS Accounting message\n");
  245. return;
  246. }
  247. os_get_time(&now);
  248. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
  249. now.sec - sta->acct_session_start)) {
  250. printf("Could not add Acct-Session-Time\n");
  251. goto fail;
  252. }
  253. if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
  254. if (!radius_msg_add_attr_int32(msg,
  255. RADIUS_ATTR_ACCT_INPUT_PACKETS,
  256. data.rx_packets)) {
  257. printf("Could not add Acct-Input-Packets\n");
  258. goto fail;
  259. }
  260. if (!radius_msg_add_attr_int32(msg,
  261. RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
  262. data.tx_packets)) {
  263. printf("Could not add Acct-Output-Packets\n");
  264. goto fail;
  265. }
  266. if (!radius_msg_add_attr_int32(msg,
  267. RADIUS_ATTR_ACCT_INPUT_OCTETS,
  268. data.rx_bytes)) {
  269. printf("Could not add Acct-Input-Octets\n");
  270. goto fail;
  271. }
  272. gigawords = sta->acct_input_gigawords;
  273. #if __WORDSIZE == 64
  274. gigawords += data.rx_bytes >> 32;
  275. #endif
  276. if (gigawords &&
  277. !radius_msg_add_attr_int32(
  278. msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
  279. gigawords)) {
  280. printf("Could not add Acct-Input-Gigawords\n");
  281. goto fail;
  282. }
  283. if (!radius_msg_add_attr_int32(msg,
  284. RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
  285. data.tx_bytes)) {
  286. printf("Could not add Acct-Output-Octets\n");
  287. goto fail;
  288. }
  289. gigawords = sta->acct_output_gigawords;
  290. #if __WORDSIZE == 64
  291. gigawords += data.tx_bytes >> 32;
  292. #endif
  293. if (gigawords &&
  294. !radius_msg_add_attr_int32(
  295. msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
  296. gigawords)) {
  297. printf("Could not add Acct-Output-Gigawords\n");
  298. goto fail;
  299. }
  300. }
  301. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
  302. now.sec)) {
  303. printf("Could not add Event-Timestamp\n");
  304. goto fail;
  305. }
  306. if (eloop_terminated())
  307. cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
  308. if (stop && cause &&
  309. !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
  310. cause)) {
  311. printf("Could not add Acct-Terminate-Cause\n");
  312. goto fail;
  313. }
  314. if (radius_client_send(hapd->radius, msg,
  315. stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
  316. sta->addr) < 0)
  317. goto fail;
  318. return;
  319. fail:
  320. radius_msg_free(msg);
  321. }
  322. /**
  323. * accounting_sta_interim - Send a interim STA accounting report
  324. * @hapd: hostapd BSS data
  325. * @sta: The station
  326. */
  327. void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
  328. {
  329. if (sta->acct_session_started)
  330. accounting_sta_report(hapd, sta, 0);
  331. }
  332. /**
  333. * accounting_sta_stop - Stop STA accounting
  334. * @hapd: hostapd BSS data
  335. * @sta: The station
  336. */
  337. void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
  338. {
  339. if (sta->acct_session_started) {
  340. accounting_sta_report(hapd, sta, 1);
  341. eloop_cancel_timeout(accounting_interim_update, hapd, sta);
  342. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
  343. HOSTAPD_LEVEL_INFO,
  344. "stopped accounting session %08X-%08X",
  345. sta->acct_session_id_hi,
  346. sta->acct_session_id_lo);
  347. sta->acct_session_started = 0;
  348. }
  349. }
  350. static void accounting_sta_get_id(struct hostapd_data *hapd,
  351. struct sta_info *sta)
  352. {
  353. sta->acct_session_id_lo = hapd->acct_session_id_lo++;
  354. if (hapd->acct_session_id_lo == 0) {
  355. hapd->acct_session_id_hi++;
  356. }
  357. sta->acct_session_id_hi = hapd->acct_session_id_hi;
  358. }
  359. /**
  360. * accounting_receive - Process the RADIUS frames from Accounting Server
  361. * @msg: RADIUS response message
  362. * @req: RADIUS request message
  363. * @shared_secret: RADIUS shared secret
  364. * @shared_secret_len: Length of shared_secret in octets
  365. * @data: Context data (struct hostapd_data *)
  366. * Returns: Processing status
  367. */
  368. static RadiusRxResult
  369. accounting_receive(struct radius_msg *msg, struct radius_msg *req,
  370. const u8 *shared_secret, size_t shared_secret_len,
  371. void *data)
  372. {
  373. if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
  374. printf("Unknown RADIUS message code\n");
  375. return RADIUS_RX_UNKNOWN;
  376. }
  377. if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
  378. printf("Incoming RADIUS packet did not have correct "
  379. "Authenticator - dropped\n");
  380. return RADIUS_RX_INVALID_AUTHENTICATOR;
  381. }
  382. return RADIUS_RX_PROCESSED;
  383. }
  384. static void accounting_report_state(struct hostapd_data *hapd, int on)
  385. {
  386. struct radius_msg *msg;
  387. if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
  388. return;
  389. /* Inform RADIUS server that accounting will start/stop so that the
  390. * server can close old accounting sessions. */
  391. msg = accounting_msg(hapd, NULL,
  392. on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
  393. RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
  394. if (!msg)
  395. return;
  396. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
  397. RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
  398. {
  399. printf("Could not add Acct-Terminate-Cause\n");
  400. radius_msg_free(msg);
  401. return;
  402. }
  403. if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
  404. radius_msg_free(msg);
  405. }
  406. /**
  407. * accounting_init: Initialize accounting
  408. * @hapd: hostapd BSS data
  409. * Returns: 0 on success, -1 on failure
  410. */
  411. int accounting_init(struct hostapd_data *hapd)
  412. {
  413. struct os_time now;
  414. /* Acct-Session-Id should be unique over reboots. If reliable clock is
  415. * not available, this could be replaced with reboot counter, etc. */
  416. os_get_time(&now);
  417. hapd->acct_session_id_hi = now.sec;
  418. if (radius_client_register(hapd->radius, RADIUS_ACCT,
  419. accounting_receive, hapd))
  420. return -1;
  421. accounting_report_state(hapd, 1);
  422. return 0;
  423. }
  424. /**
  425. * accounting_deinit: Deinitilize accounting
  426. * @hapd: hostapd BSS data
  427. */
  428. void accounting_deinit(struct hostapd_data *hapd)
  429. {
  430. accounting_report_state(hapd, 0);
  431. }