accounting.c 13 KB

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