accounting.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * hostapd / RADIUS Accounting
  3. * Copyright (c) 2002-2007, 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 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. void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
  193. {
  194. struct radius_msg *msg;
  195. int interval;
  196. if (sta->acct_session_started)
  197. return;
  198. time(&sta->acct_session_start);
  199. sta->last_rx_bytes = sta->last_tx_bytes = 0;
  200. sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
  201. hostapd_sta_clear_stats(hapd, sta->addr);
  202. if (!hapd->conf->radius->acct_server)
  203. return;
  204. if (sta->acct_interim_interval)
  205. interval = sta->acct_interim_interval;
  206. else
  207. interval = ACCT_DEFAULT_UPDATE_INTERVAL;
  208. eloop_register_timeout(interval, 0, accounting_interim_update,
  209. hapd, sta);
  210. msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
  211. if (msg)
  212. radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
  213. sta->acct_session_started = 1;
  214. }
  215. void accounting_sta_report(struct hostapd_data *hapd, struct sta_info *sta,
  216. int stop)
  217. {
  218. struct radius_msg *msg;
  219. int cause = sta->acct_terminate_cause;
  220. struct hostap_sta_driver_data data;
  221. u32 gigawords;
  222. if (!hapd->conf->radius->acct_server)
  223. return;
  224. msg = accounting_msg(hapd, sta,
  225. stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
  226. RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
  227. if (!msg) {
  228. printf("Could not create RADIUS Accounting message\n");
  229. return;
  230. }
  231. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
  232. time(NULL) - sta->acct_session_start)) {
  233. printf("Could not add Acct-Session-Time\n");
  234. goto fail;
  235. }
  236. if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
  237. if (!radius_msg_add_attr_int32(msg,
  238. RADIUS_ATTR_ACCT_INPUT_PACKETS,
  239. data.rx_packets)) {
  240. printf("Could not add Acct-Input-Packets\n");
  241. goto fail;
  242. }
  243. if (!radius_msg_add_attr_int32(msg,
  244. RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
  245. data.tx_packets)) {
  246. printf("Could not add Acct-Output-Packets\n");
  247. goto fail;
  248. }
  249. if (!radius_msg_add_attr_int32(msg,
  250. RADIUS_ATTR_ACCT_INPUT_OCTETS,
  251. data.rx_bytes)) {
  252. printf("Could not add Acct-Input-Octets\n");
  253. goto fail;
  254. }
  255. gigawords = sta->acct_input_gigawords;
  256. #if __WORDSIZE == 64
  257. gigawords += data.rx_bytes >> 32;
  258. #endif
  259. if (gigawords &&
  260. !radius_msg_add_attr_int32(
  261. msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
  262. gigawords)) {
  263. printf("Could not add Acct-Input-Gigawords\n");
  264. goto fail;
  265. }
  266. if (!radius_msg_add_attr_int32(msg,
  267. RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
  268. data.tx_bytes)) {
  269. printf("Could not add Acct-Output-Octets\n");
  270. goto fail;
  271. }
  272. gigawords = sta->acct_output_gigawords;
  273. #if __WORDSIZE == 64
  274. gigawords += data.tx_bytes >> 32;
  275. #endif
  276. if (gigawords &&
  277. !radius_msg_add_attr_int32(
  278. msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
  279. gigawords)) {
  280. printf("Could not add Acct-Output-Gigawords\n");
  281. goto fail;
  282. }
  283. }
  284. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
  285. time(NULL))) {
  286. printf("Could not add Event-Timestamp\n");
  287. goto fail;
  288. }
  289. if (eloop_terminated())
  290. cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
  291. if (stop && cause &&
  292. !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
  293. cause)) {
  294. printf("Could not add Acct-Terminate-Cause\n");
  295. goto fail;
  296. }
  297. radius_client_send(hapd->radius, msg,
  298. stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
  299. sta->addr);
  300. return;
  301. fail:
  302. radius_msg_free(msg);
  303. os_free(msg);
  304. }
  305. void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
  306. {
  307. if (sta->acct_session_started)
  308. accounting_sta_report(hapd, sta, 0);
  309. }
  310. void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
  311. {
  312. if (sta->acct_session_started) {
  313. accounting_sta_report(hapd, sta, 1);
  314. eloop_cancel_timeout(accounting_interim_update, hapd, sta);
  315. sta->acct_session_started = 0;
  316. }
  317. }
  318. void accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
  319. {
  320. sta->acct_session_id_lo = hapd->acct_session_id_lo++;
  321. if (hapd->acct_session_id_lo == 0) {
  322. hapd->acct_session_id_hi++;
  323. }
  324. sta->acct_session_id_hi = hapd->acct_session_id_hi;
  325. }
  326. /* Process the RADIUS frames from Accounting Server */
  327. static RadiusRxResult
  328. accounting_receive(struct radius_msg *msg, struct radius_msg *req,
  329. u8 *shared_secret, size_t shared_secret_len, void *data)
  330. {
  331. if (msg->hdr->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
  332. printf("Unknown RADIUS message code\n");
  333. return RADIUS_RX_UNKNOWN;
  334. }
  335. if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
  336. printf("Incoming RADIUS packet did not have correct "
  337. "Authenticator - dropped\n");
  338. return RADIUS_RX_INVALID_AUTHENTICATOR;
  339. }
  340. return RADIUS_RX_PROCESSED;
  341. }
  342. static void accounting_report_state(struct hostapd_data *hapd, int on)
  343. {
  344. struct radius_msg *msg;
  345. if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
  346. return;
  347. /* Inform RADIUS server that accounting will start/stop so that the
  348. * server can close old accounting sessions. */
  349. msg = accounting_msg(hapd, NULL,
  350. on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
  351. RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
  352. if (!msg)
  353. return;
  354. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
  355. RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
  356. {
  357. printf("Could not add Acct-Terminate-Cause\n");
  358. radius_msg_free(msg);
  359. os_free(msg);
  360. return;
  361. }
  362. radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
  363. }
  364. int accounting_init(struct hostapd_data *hapd)
  365. {
  366. /* Acct-Session-Id should be unique over reboots. If reliable clock is
  367. * not available, this could be replaced with reboot counter, etc. */
  368. hapd->acct_session_id_hi = time(NULL);
  369. if (radius_client_register(hapd->radius, RADIUS_ACCT,
  370. accounting_receive, hapd))
  371. return -1;
  372. accounting_report_state(hapd, 1);
  373. return 0;
  374. }
  375. void accounting_deinit(struct hostapd_data *hapd)
  376. {
  377. accounting_report_state(hapd, 0);
  378. }
  379. int accounting_reconfig(struct hostapd_data *hapd,
  380. struct hostapd_config *oldconf)
  381. {
  382. if (!hapd->radius_client_reconfigured)
  383. return 0;
  384. accounting_deinit(hapd);
  385. return accounting_init(hapd);
  386. }