blob: 0fcdd3b2ab765265868a9f44228b47a725ef78c5 [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
67 fprintf(cdr_file, "timestamp,imsi,imei,msisdn,cell_id,lac,event,pdp_duration,ggsn_addr,sgsn_addr,apni,eua_addr,vol_in,vol_out\n");
68}
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,
174 "%04d%02d%02d%02d%02d%02d%03d,%s,%s,%s,%d,%d,%s,%ld,%s,%s,%s,%s,%" PRIu64 ",%" PRIu64 "\n",
175 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,
190 pdp->cdr_bytes_out);
191 fclose(cdr_file);
192}
193
194static void cdr_pdp_timeout(void *_data)
195{
196 struct sgsn_pdp_ctx *pdp = _data;
197 cdr_log_pdp(sgsn, "pdp-periodic", pdp);
198 osmo_timer_schedule(&pdp->cdr_timer, sgsn->cfg.cdr.interval, 0);
199}
200
201static int handle_sgsn_sig(unsigned int subsys, unsigned int signal,
202 void *handler_data, void *_signal_data)
203{
204 struct sgsn_signal_data *signal_data = _signal_data;
205 struct sgsn_instance *inst = handler_data;
206
207 if (subsys != SS_SGSN)
208 return 0;
209
210 switch (signal) {
211 case S_SGSN_ATTACH:
212 cdr_log_mm(inst, "attach", signal_data->mm);
213 break;
214 case S_SGSN_UPDATE:
215 cdr_log_mm(inst, "update", signal_data->mm);
216 break;
217 case S_SGSN_DETACH:
218 cdr_log_mm(inst, "detach", signal_data->mm);
219 break;
220 case S_SGSN_MM_FREE:
221 cdr_log_mm(inst, "free", signal_data->mm);
222 break;
223 case S_SGSN_PDP_ACT:
224 clock_gettime(CLOCK_MONOTONIC, &signal_data->pdp->cdr_start);
225 cdr_log_pdp(inst, "pdp-act", signal_data->pdp);
226 signal_data->pdp->cdr_timer.cb = cdr_pdp_timeout;
227 signal_data->pdp->cdr_timer.data = signal_data->pdp;
228 osmo_timer_schedule(&signal_data->pdp->cdr_timer, inst->cfg.cdr.interval, 0);
229 break;
230 case S_SGSN_PDP_DEACT:
231 cdr_log_pdp(inst, "pdp-deact", signal_data->pdp);
232 osmo_timer_del(&signal_data->pdp->cdr_timer);
233 break;
234 case S_SGSN_PDP_TERMINATE:
235 cdr_log_pdp(inst, "pdp-terminate", signal_data->pdp);
236 osmo_timer_del(&signal_data->pdp->cdr_timer);
237 break;
238 case S_SGSN_PDP_FREE:
239 cdr_log_pdp(inst, "pdp-free", signal_data->pdp);
240 osmo_timer_del(&signal_data->pdp->cdr_timer);
241 break;
242 }
243
244 return 0;
245}
246
247int sgsn_cdr_init(struct sgsn_instance *sgsn)
248{
249 /* register for CDR related events */
250 sgsn->cfg.cdr.interval = 10 * 60;
251 osmo_signal_register_handler(SS_SGSN, handle_sgsn_sig, sgsn);
252
253 return 0;
254}