accounting.c 12 KB

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