accounting.c 12 KB

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