accounting.c 13 KB

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