blob: 04084f5b284a304fc653f8e486672ccb135f156c [file] [log] [blame]
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +02001/* GPRS SGSN CDR dumper */
2
3/* (C) 2015 by Holger Hans Peter Freyther
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <openbsc/sgsn.h>
22#include <openbsc/signal.h>
23#include <openbsc/gprs_utils.h>
24#include <openbsc/debug.h>
25
26#include <openbsc/vty.h>
27
28#include <gtp.h>
29#include <pdp.h>
30
31#include <arpa/inet.h>
32
33#include <time.h>
34
35#include <stdio.h>
36#include <inttypes.h>
37
38/* TODO...avoid going through a global */
39extern struct sgsn_instance *sgsn;
40
41/**
42 * The CDR module will generate an entry like:
43 *
44 * IMSI, # Subscriber IMSI
45 * IMEI, # Subscriber IMEI
46 * MSISDN, # Subscriber MISDN
47 * Charging_Timestamp, # Event start Time
48 * Charging_UTC, # Time zone of event start time
49 * Duration, # Session DURATION
50 * Cell_Id, # CELL_ID
51 * Location_Area, # LAC
52 * GGSN_ADDR, # GGSN_ADDR
53 * SGSN_ADDR, # SGSN_ADDR
54 * APNI, # APNI
55 * PDP_ADDR, # PDP_ADDR
56 * VOL_IN, # VOL_IN in Bytes
57 * VOL_OUT, # VOL_OUT in Bytes
58 * CAUSE_FOR_TERM, # CAUSE_FOR_TERM
59 */
60
61
62static void maybe_print_header(FILE *cdr_file)
63{
64 if (ftell(cdr_file) != 0)
65 return;
66
Holger Hans Peter Freyther77ff1c42015-05-12 21:08:42 +020067 fprintf(cdr_file, "timestamp,imsi,imei,msisdn,cell_id,lac,event,pdp_duration,ggsn_addr,sgsn_addr,apni,eua_addr,vol_in,vol_out,charging_id\n");
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +020068}
69
70static void cdr_log_mm(struct sgsn_instance *inst, const char *ev,
71 struct sgsn_mm_ctx *mmctx)
72{
73 FILE *cdr_file;
74 struct tm tm;
75 struct timeval tv;
76
77 if (!inst->cfg.cdr.filename)
78 return;
79
80 cdr_file = fopen(inst->cfg.cdr.filename, "a");
81 if (!cdr_file) {
82 LOGP(DGPRS, LOGL_ERROR, "Failed to open %s\n",
83 inst->cfg.cdr.filename);
84 return;
85 }
86
87 maybe_print_header(cdr_file);
88 gettimeofday(&tv, NULL);
89 gmtime_r(&tv.tv_sec, &tm);
90 fprintf(cdr_file, "%04d%02d%02d%02d%02d%02d%03d,%s,%s,%s,%d,%d,%s\n",
91 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
92 tm.tm_hour, tm.tm_min, tm.tm_sec,
93 (int)(tv.tv_usec / 1000),
94 mmctx->imsi,
95 mmctx->imei,
96 mmctx->msisdn,
97 mmctx->cell_id,
98 mmctx->ra.lac,
99 ev);
100
101 fclose(cdr_file);
102}
103
104static void extract_eua(struct ul66_t *eua, char *eua_addr)
105{
106 if (eua->l < 2)
107 return;
108
109 /* there is no addr for ETSI/PPP */
110 if ((eua->v[0] & 0x0F) != 1) {
111 strcpy(eua_addr, "ETSI");
112 return;
113 }
114
115 if (eua->v[1] == 0x21 && eua->l == 6)
116 inet_ntop(AF_INET, &eua->v[2], eua_addr, INET_ADDRSTRLEN);
117 else if (eua->v[1] == 0x57 && eua->l == 18)
118 inet_ntop(AF_INET6, &eua->v[2], eua_addr, INET6_ADDRSTRLEN);
119 else {
120 /* e.g. both IPv4 and IPv6 */
121 strcpy(eua_addr, "Unknown address");
122 }
123}
124
125static void cdr_log_pdp(struct sgsn_instance *inst, const char *ev,
126 struct sgsn_pdp_ctx *pdp)
127{
128 FILE *cdr_file;
129 char apni[(pdp->lib ? pdp->lib->apn_use.l : 0) + 1];
130 char ggsn_addr[INET_ADDRSTRLEN + 1];
131 char sgsn_addr[INET_ADDRSTRLEN + 1];
132 char eua_addr[INET6_ADDRSTRLEN + 1];
133 struct tm tm;
134 struct timeval tv;
135 time_t duration;
136 struct timespec tp;
137
138 if (!inst->cfg.cdr.filename)
139 return;
140
141 memset(apni, 0, sizeof(apni));
142 memset(ggsn_addr, 0, sizeof(ggsn_addr));
143 memset(eua_addr, 0, sizeof(eua_addr));
144
145
146 if (pdp->lib) {
147 gprs_apn_to_str(apni, pdp->lib->apn_use.v, pdp->lib->apn_use.l);
148 inet_ntop(AF_INET, &pdp->lib->hisaddr0.s_addr, ggsn_addr, sizeof(ggsn_addr));
149 extract_eua(&pdp->lib->eua, eua_addr);
150 }
151
152 if (pdp->ggsn)
153 inet_ntop(AF_INET, &pdp->ggsn->gsn->gsnc.s_addr, sgsn_addr, sizeof(sgsn_addr));
154
155 cdr_file = fopen(inst->cfg.cdr.filename, "a");
156 if (!cdr_file) {
157 LOGP(DGPRS, LOGL_ERROR, "Failed to open %s\n",
158 inst->cfg.cdr.filename);
159 return;
160 }
161
162 maybe_print_header(cdr_file);
163
164 clock_gettime(CLOCK_MONOTONIC, &tp);
165 gettimeofday(&tv, NULL);
166
167 /* convert the timestamp to UTC */
168 gmtime_r(&tv.tv_sec, &tm);
169
170 /* Check the duration of the PDP context */
171 duration = tp.tv_sec - pdp->cdr_start.tv_sec;
172
173 fprintf(cdr_file,
Holger Hans Peter Freyther77ff1c42015-05-12 21:08:42 +0200174 "%04d%02d%02d%02d%02d%02d%03d,%s,%s,%s,%d,%d,%s,%ld,%s,%s,%s,%s,%" PRIu64 ",%" PRIu64 ",%u\n",
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +0200175 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
176 tm.tm_hour, tm.tm_min, tm.tm_sec,
177 (int)(tv.tv_usec / 1000),
178 pdp->mm ? pdp->mm->imsi : "N/A",
179 pdp->mm ? pdp->mm->imei : "N/A",
180 pdp->mm ? pdp->mm->msisdn : "N/A",
181 pdp->mm ? pdp->mm->cell_id : -1,
182 pdp->mm ? pdp->mm->ra.lac : -1,
183 ev,
184 (unsigned long ) duration,
185 ggsn_addr,
186 sgsn_addr,
187 apni,
188 eua_addr,
189 pdp->cdr_bytes_in,
Holger Hans Peter Freyther77ff1c42015-05-12 21:08:42 +0200190 pdp->cdr_bytes_out,
191 pdp->cdr_charging_id);
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +0200192 fclose(cdr_file);
193}
194
195static void cdr_pdp_timeout(void *_data)
196{
197 struct sgsn_pdp_ctx *pdp = _data;
198 cdr_log_pdp(sgsn, "pdp-periodic", pdp);
199 osmo_timer_schedule(&pdp->cdr_timer, sgsn->cfg.cdr.interval, 0);
200}
201
202static int handle_sgsn_sig(unsigned int subsys, unsigned int signal,
203 void *handler_data, void *_signal_data)
204{
205 struct sgsn_signal_data *signal_data = _signal_data;
206 struct sgsn_instance *inst = handler_data;
207
208 if (subsys != SS_SGSN)
209 return 0;
210
211 switch (signal) {
212 case S_SGSN_ATTACH:
213 cdr_log_mm(inst, "attach", signal_data->mm);
214 break;
215 case S_SGSN_UPDATE:
216 cdr_log_mm(inst, "update", signal_data->mm);
217 break;
218 case S_SGSN_DETACH:
219 cdr_log_mm(inst, "detach", signal_data->mm);
220 break;
221 case S_SGSN_MM_FREE:
222 cdr_log_mm(inst, "free", signal_data->mm);
223 break;
224 case S_SGSN_PDP_ACT:
225 clock_gettime(CLOCK_MONOTONIC, &signal_data->pdp->cdr_start);
Holger Hans Peter Freyther77ff1c42015-05-12 21:08:42 +0200226 signal_data->pdp->cdr_charging_id = signal_data->pdp->lib->cid;
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +0200227 cdr_log_pdp(inst, "pdp-act", signal_data->pdp);
228 signal_data->pdp->cdr_timer.cb = cdr_pdp_timeout;
229 signal_data->pdp->cdr_timer.data = signal_data->pdp;
230 osmo_timer_schedule(&signal_data->pdp->cdr_timer, inst->cfg.cdr.interval, 0);
231 break;
232 case S_SGSN_PDP_DEACT:
233 cdr_log_pdp(inst, "pdp-deact", signal_data->pdp);
234 osmo_timer_del(&signal_data->pdp->cdr_timer);
235 break;
236 case S_SGSN_PDP_TERMINATE:
237 cdr_log_pdp(inst, "pdp-terminate", signal_data->pdp);
238 osmo_timer_del(&signal_data->pdp->cdr_timer);
239 break;
240 case S_SGSN_PDP_FREE:
241 cdr_log_pdp(inst, "pdp-free", signal_data->pdp);
242 osmo_timer_del(&signal_data->pdp->cdr_timer);
243 break;
244 }
245
246 return 0;
247}
248
249int sgsn_cdr_init(struct sgsn_instance *sgsn)
250{
251 /* register for CDR related events */
252 sgsn->cfg.cdr.interval = 10 * 60;
253 osmo_signal_register_handler(SS_SGSN, handle_sgsn_sig, sgsn);
254
255 return 0;
256}