accounting.c 13 KB

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