blob: 901b870d2cfd8140eb619d239cc28c1d88ad9b95 [file] [log] [blame]
Harald Welte4f8e34b2016-05-06 23:27:38 +02001#include <string.h>
2#include <stdio.h>
3#include <errno.h>
4#include <signal.h>
5
6#include <osmocom/core/linuxlist.h>
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/select.h>
9#include <osmocom/core/application.h>
10#include <osmocom/gsm/gsup.h>
11
12#include <openbsc/gprs_gsup_client.h>
13#include <openbsc/debug.h>
14
Neels Hofmeyr814fef02016-12-08 21:19:57 +010015static struct gsup_client *g_gc;
Harald Welte4f8e34b2016-05-06 23:27:38 +020016
17
18/***********************************************************************
19 * IMSI Operation
20 ***********************************************************************/
21static LLIST_HEAD(g_imsi_ops);
22
23struct imsi_op_stats {
24 uint32_t num_alloc;
25 uint32_t num_released;
26 uint32_t num_rx_success;
27 uint32_t num_rx_error;
28 uint32_t num_timeout;
29};
30
31enum imsi_op_type {
32 IMSI_OP_SAI,
33 IMSI_OP_LU,
34 IMSI_OP_ISD,
35 _NUM_IMSI_OP
36};
37
38static const struct value_string imsi_op_names[] = {
39 { IMSI_OP_SAI, "SAI" },
40 { IMSI_OP_LU, "LU" },
41 { IMSI_OP_ISD, "ISD" },
42 { 0, NULL }
43};
44
45static struct imsi_op_stats imsi_op_stats[_NUM_IMSI_OP];
46
47struct imsi_op {
48 struct llist_head list;
49 char imsi[17];
50 enum imsi_op_type type;
51 struct osmo_timer_list timer;
52};
53
54static struct imsi_op *imsi_op_find(const char *imsi,
55 enum imsi_op_type type)
56{
57 struct imsi_op *io;
58
59 llist_for_each_entry(io, &g_imsi_ops, list) {
60 if (!strcmp(io->imsi, imsi) && io->type == type)
61 return io;
62 }
63 return NULL;
64}
65
66static void imsi_op_timer_cb(void *data);
67
68static struct imsi_op *imsi_op_alloc(void *ctx, const char *imsi,
69 enum imsi_op_type type)
70{
71 struct imsi_op *io;
72
73 if (imsi_op_find(imsi, type))
74 return NULL;
75
76 io = talloc_zero(ctx, struct imsi_op);
77 strncpy(io->imsi, imsi, sizeof(io->imsi));
78 io->imsi[sizeof(io->imsi)-1] = '\0';
79 io->type = type;
80 io->timer.cb = imsi_op_timer_cb;
81 io->timer.data = io;
82 llist_add(&io->list, &g_imsi_ops);
83 imsi_op_stats[type].num_alloc++;
84
85 return io;
86}
87
88static void imsi_op_release(struct imsi_op *io)
89{
90 osmo_timer_del(&io->timer);
91 llist_del(&io->list);
92 imsi_op_stats[io->type].num_released++;
93 talloc_free(io);
94}
95
96static void imsi_op_timer_cb(void *data)
97{
98 struct imsi_op *io = data;
99 printf("%s: Timer expiration\n", io->imsi);
100 imsi_op_stats[io->type].num_timeout++;
101 imsi_op_release(io);
102}
103
104/* allocate + generate + send Send-Auth-Info */
105int req_auth_info(const char *imsi)
106{
107 struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_SAI);
108 struct osmo_gsup_message gsup = {0};
109 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
110
111 strncpy(gsup.imsi, io->imsi, sizeof(gsup.imsi));
112 gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
113
114 osmo_gsup_encode(msg, &gsup);
115
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100116 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200117}
118
119/* allocate + generate + send Send-Auth-Info */
120int req_loc_upd(const char *imsi)
121{
122 struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_LU);
123 struct osmo_gsup_message gsup = {0};
124 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
125
126 strncpy(gsup.imsi, io->imsi, sizeof(gsup.imsi));
127 gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
128
129 osmo_gsup_encode(msg, &gsup);
130
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100131 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200132}
133
134int resp_isd(struct imsi_op *io)
135{
136 struct osmo_gsup_message gsup = {0};
137 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
138
139 strncpy(gsup.imsi, io->imsi, sizeof(gsup.imsi));
140 gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
141
142 osmo_gsup_encode(msg, &gsup);
143
144 imsi_op_release(io);
145
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100146 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200147}
148
149/* receive an incoming GSUP message */
150static void imsi_op_rx_gsup(struct imsi_op *io, const struct osmo_gsup_message *gsup)
151{
152 int is_error = 0;
153
154 if (OSMO_GSUP_IS_MSGT_ERROR(gsup->message_type)) {
155 imsi_op_stats[io->type].num_rx_error++;
156 is_error = 1;
157 } else
158 imsi_op_stats[io->type].num_rx_success++;
159
160 switch (io->type) {
161 case IMSI_OP_SAI:
162 printf("%s; SAI Response%s\n", io->imsi, is_error ? ": ERROR" : "");
163 /* now that we have auth tuples, request LU */
164 req_loc_upd(io->imsi);
165 imsi_op_release(io);
166 break;
167 case IMSI_OP_LU:
168 printf("%s; LU Response%s\n", io->imsi, is_error ? ": ERROR" : "");
169 imsi_op_release(io);
170 break;
171 case IMSI_OP_ISD:
172 printf("%s; ISD Request%s\n", io->imsi, is_error ? ": ERROR" : "");
173 resp_isd(io);
174 break;
175 default:
176 printf("%s: Unknown\n", io->imsi);
177 imsi_op_release(io);
178 break;
179 }
180}
181
182static int op_type_by_gsup_msgt(enum osmo_gsup_message_type msg_type)
183{
184 switch (msg_type) {
185 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
186 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
187 return IMSI_OP_SAI;
188 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
189 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
190 return IMSI_OP_LU;
191 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
192 return IMSI_OP_ISD;
193 default:
194 printf("Unknown GSUP msg_type %u\n", msg_type);
195 return -1;
196 }
197}
198
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100199static int gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
Harald Welte4f8e34b2016-05-06 23:27:38 +0200200{
201 struct osmo_gsup_message gsup_msg = {0};
202 struct imsi_op *io;
203 int rc;
204
205 DEBUGP(DGPRS, "Rx GSUP %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
206
207 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg);
208 if (rc < 0)
209 return rc;
210
211 if (!gsup_msg.imsi[0])
212 return -1;
213
214 rc = op_type_by_gsup_msgt(gsup_msg.message_type);
215 if (rc < 0)
216 return rc;
217
218 switch (rc) {
219 case IMSI_OP_SAI:
220 case IMSI_OP_LU:
221 io = imsi_op_find(gsup_msg.imsi, rc);
222 if (!io)
223 return -1;
224 break;
225 case IMSI_OP_ISD:
226 /* ISD is an inbound transaction */
227 io = imsi_op_alloc(g_gc, gsup_msg.imsi, IMSI_OP_ISD);
228 break;
229 }
230
231 imsi_op_rx_gsup(io, &gsup_msg);
232 msgb_free(msg);
233
234 return 0;
235}
236
237static void print_report(void)
238{
239 unsigned int i;
240
241 for (i = 0; i < ARRAY_SIZE(imsi_op_stats); i++) {
242 struct imsi_op_stats *st = &imsi_op_stats[i];
243 const char *name = get_value_string(imsi_op_names, i);
244 printf("%s: %u alloc, %u released, %u success, %u error , %u tout\n",
245 name, st->num_alloc, st->num_released, st->num_rx_success,
246 st->num_rx_error, st->num_timeout);
247 }
248}
249
250static void sig_cb(int sig)
251{
252 switch (sig) {
253 case SIGINT:
254 print_report();
255 exit(0);
256 break;
257 }
258}
259
260void *tall_bsc_ctx = NULL;
261
262/* default categories */
263static struct log_info_cat gprs_categories[] = {
264 [DGPRS] = {
265 .name = "DGPRS",
266 .description = "GPRS Packet Service",
267 .enabled = 1, .loglevel = LOGL_INFO,
268 },
269};
270
271static const struct log_info gprs_log_info = {
272 .cat = gprs_categories,
273 .num_cat = ARRAY_SIZE(gprs_categories),
274};
275
276int main(int argc, char **argv)
277{
278 unsigned long long i;
279 char *server_host = "127.0.0.1";
280 uint16_t server_port = 2222;
281
282 osmo_init_logging(&gprs_log_info);
283
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100284 g_gc = gsup_client_create(server_host, server_port, gsupc_read_cb,
285 NULL);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200286
287
288 signal(SIGINT, sig_cb);
289
290 for (i = 0; i < 10000; i++) {
291 unsigned long long imsi = 901790000000000 + i;
292 char imsi_buf[17];
293 snprintf(imsi_buf, sizeof(imsi_buf), "%015llu", imsi);
294 req_auth_info(imsi_buf);
295 osmo_select_main(0);
296 }
297
298 while (1) {
299 osmo_select_main(0);
300 }
301
302 print_report();
303 exit(0);
304}