accounting.c 12 KB

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