blob: 963b4951ba8ed9205f0a90e539528d903e2e1252 [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>
Neels Hofmeyr59504dc2017-01-13 03:10:54 +010010#include <osmocom/core/utils.h>
Harald Welte4f8e34b2016-05-06 23:27:38 +020011#include <osmocom/gsm/gsup.h>
12
Neels Hofmeyr90843962017-09-04 15:04:35 +020013#include <osmocom/msc/gsup_client.h>
14#include <osmocom/msc/debug.h>
Harald Welte4f8e34b2016-05-06 23:27:38 +020015
Neels Hofmeyr814fef02016-12-08 21:19:57 +010016static struct gsup_client *g_gc;
Harald Welte4f8e34b2016-05-06 23:27:38 +020017
18
19/***********************************************************************
20 * IMSI Operation
21 ***********************************************************************/
22static LLIST_HEAD(g_imsi_ops);
23
24struct imsi_op_stats {
25 uint32_t num_alloc;
26 uint32_t num_released;
27 uint32_t num_rx_success;
28 uint32_t num_rx_error;
29 uint32_t num_timeout;
30};
31
32enum imsi_op_type {
33 IMSI_OP_SAI,
34 IMSI_OP_LU,
35 IMSI_OP_ISD,
36 _NUM_IMSI_OP
37};
38
39static const struct value_string imsi_op_names[] = {
40 { IMSI_OP_SAI, "SAI" },
41 { IMSI_OP_LU, "LU" },
42 { IMSI_OP_ISD, "ISD" },
43 { 0, NULL }
44};
45
46static struct imsi_op_stats imsi_op_stats[_NUM_IMSI_OP];
47
48struct imsi_op {
49 struct llist_head list;
50 char imsi[17];
51 enum imsi_op_type type;
52 struct osmo_timer_list timer;
53};
54
55static struct imsi_op *imsi_op_find(const char *imsi,
56 enum imsi_op_type type)
57{
58 struct imsi_op *io;
59
60 llist_for_each_entry(io, &g_imsi_ops, list) {
61 if (!strcmp(io->imsi, imsi) && io->type == type)
62 return io;
63 }
64 return NULL;
65}
66
67static void imsi_op_timer_cb(void *data);
68
69static struct imsi_op *imsi_op_alloc(void *ctx, const char *imsi,
70 enum imsi_op_type type)
71{
72 struct imsi_op *io;
73
74 if (imsi_op_find(imsi, type))
75 return NULL;
76
77 io = talloc_zero(ctx, struct imsi_op);
Max98f74672018-02-05 12:57:06 +010078 OSMO_STRLCPY_ARRAY(io->imsi, imsi);
Harald Welte4f8e34b2016-05-06 23:27:38 +020079 io->type = type;
Pablo Neira Ayuso51215762017-05-08 20:57:52 +020080 osmo_timer_setup(&io->timer, imsi_op_timer_cb, io);
Harald Welte4f8e34b2016-05-06 23:27:38 +020081 llist_add(&io->list, &g_imsi_ops);
82 imsi_op_stats[type].num_alloc++;
83
84 return io;
85}
86
87static void imsi_op_release(struct imsi_op *io)
88{
89 osmo_timer_del(&io->timer);
90 llist_del(&io->list);
91 imsi_op_stats[io->type].num_released++;
92 talloc_free(io);
93}
94
95static void imsi_op_timer_cb(void *data)
96{
97 struct imsi_op *io = data;
98 printf("%s: Timer expiration\n", io->imsi);
99 imsi_op_stats[io->type].num_timeout++;
100 imsi_op_release(io);
101}
102
103/* allocate + generate + send Send-Auth-Info */
Max770fbd22018-01-24 12:48:33 +0100104static int req_auth_info(const char *imsi)
Harald Welte4f8e34b2016-05-06 23:27:38 +0200105{
106 struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_SAI);
107 struct osmo_gsup_message gsup = {0};
108 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
Max770fbd22018-01-24 12:48:33 +0100109 int rc;
Harald Welte4f8e34b2016-05-06 23:27:38 +0200110
Max98f74672018-02-05 12:57:06 +0100111 OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200112 gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
113
Max770fbd22018-01-24 12:48:33 +0100114 rc = osmo_gsup_encode(msg, &gsup);
115 if (rc < 0) {
116 printf("%s: encoding failure (%s)\n", imsi, strerror(-rc));
117 return rc;
118 }
Harald Welte4f8e34b2016-05-06 23:27:38 +0200119
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100120 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200121}
122
123/* allocate + generate + send Send-Auth-Info */
Max770fbd22018-01-24 12:48:33 +0100124static int req_loc_upd(const char *imsi)
Harald Welte4f8e34b2016-05-06 23:27:38 +0200125{
126 struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_LU);
127 struct osmo_gsup_message gsup = {0};
128 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
Max770fbd22018-01-24 12:48:33 +0100129 int rc;
Harald Welte4f8e34b2016-05-06 23:27:38 +0200130
Max98f74672018-02-05 12:57:06 +0100131 OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200132 gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
133
Max770fbd22018-01-24 12:48:33 +0100134 rc = osmo_gsup_encode(msg, &gsup);
135 if (rc < 0) {
136 printf("%s: encoding failure (%s)\n", imsi, strerror(-rc));
137 return rc;
138 }
Harald Welte4f8e34b2016-05-06 23:27:38 +0200139
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100140 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200141}
142
Max770fbd22018-01-24 12:48:33 +0100143static int resp_isd(struct imsi_op *io)
Harald Welte4f8e34b2016-05-06 23:27:38 +0200144{
145 struct osmo_gsup_message gsup = {0};
146 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
Max770fbd22018-01-24 12:48:33 +0100147 int rc;
Harald Welte4f8e34b2016-05-06 23:27:38 +0200148
Max98f74672018-02-05 12:57:06 +0100149 OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200150 gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
151
Max770fbd22018-01-24 12:48:33 +0100152 rc = osmo_gsup_encode(msg, &gsup);
153 if (rc < 0) {
154 printf("%s: encoding failure (%s)\n", io->imsi, strerror(-rc));
155 return rc;
156 }
Harald Welte4f8e34b2016-05-06 23:27:38 +0200157
158 imsi_op_release(io);
159
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100160 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200161}
162
163/* receive an incoming GSUP message */
164static void imsi_op_rx_gsup(struct imsi_op *io, const struct osmo_gsup_message *gsup)
165{
Max770fbd22018-01-24 12:48:33 +0100166 int is_error = 0, rc;
Harald Welte4f8e34b2016-05-06 23:27:38 +0200167
168 if (OSMO_GSUP_IS_MSGT_ERROR(gsup->message_type)) {
169 imsi_op_stats[io->type].num_rx_error++;
170 is_error = 1;
171 } else
172 imsi_op_stats[io->type].num_rx_success++;
173
174 switch (io->type) {
175 case IMSI_OP_SAI:
176 printf("%s; SAI Response%s\n", io->imsi, is_error ? ": ERROR" : "");
177 /* now that we have auth tuples, request LU */
Max770fbd22018-01-24 12:48:33 +0100178 rc = req_loc_upd(io->imsi);
179 if (rc < 0)
180 printf("Failed to request Location Update for %s\n", io->imsi);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200181 imsi_op_release(io);
182 break;
183 case IMSI_OP_LU:
184 printf("%s; LU Response%s\n", io->imsi, is_error ? ": ERROR" : "");
185 imsi_op_release(io);
186 break;
187 case IMSI_OP_ISD:
188 printf("%s; ISD Request%s\n", io->imsi, is_error ? ": ERROR" : "");
Max770fbd22018-01-24 12:48:33 +0100189 rc = resp_isd(io);
190 if (rc < 0)
191 printf("Failed to insert subscriber data for %s\n", io->imsi);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200192 break;
193 default:
194 printf("%s: Unknown\n", io->imsi);
195 imsi_op_release(io);
196 break;
197 }
198}
199
200static int op_type_by_gsup_msgt(enum osmo_gsup_message_type msg_type)
201{
202 switch (msg_type) {
203 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
204 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
205 return IMSI_OP_SAI;
206 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
207 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
208 return IMSI_OP_LU;
209 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
210 return IMSI_OP_ISD;
211 default:
212 printf("Unknown GSUP msg_type %u\n", msg_type);
213 return -1;
214 }
215}
216
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100217static int gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
Harald Welte4f8e34b2016-05-06 23:27:38 +0200218{
219 struct osmo_gsup_message gsup_msg = {0};
220 struct imsi_op *io;
221 int rc;
222
Harald Welte703f2ec2018-01-25 00:36:42 +0100223 DEBUGP(DLGSUP, "Rx GSUP %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte4f8e34b2016-05-06 23:27:38 +0200224
225 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg);
226 if (rc < 0)
227 return rc;
228
229 if (!gsup_msg.imsi[0])
230 return -1;
231
232 rc = op_type_by_gsup_msgt(gsup_msg.message_type);
233 if (rc < 0)
234 return rc;
235
236 switch (rc) {
237 case IMSI_OP_SAI:
238 case IMSI_OP_LU:
239 io = imsi_op_find(gsup_msg.imsi, rc);
240 if (!io)
241 return -1;
242 break;
243 case IMSI_OP_ISD:
244 /* ISD is an inbound transaction */
245 io = imsi_op_alloc(g_gc, gsup_msg.imsi, IMSI_OP_ISD);
246 break;
247 }
248
249 imsi_op_rx_gsup(io, &gsup_msg);
250 msgb_free(msg);
251
252 return 0;
253}
254
255static void print_report(void)
256{
257 unsigned int i;
258
259 for (i = 0; i < ARRAY_SIZE(imsi_op_stats); i++) {
260 struct imsi_op_stats *st = &imsi_op_stats[i];
261 const char *name = get_value_string(imsi_op_names, i);
262 printf("%s: %u alloc, %u released, %u success, %u error , %u tout\n",
263 name, st->num_alloc, st->num_released, st->num_rx_success,
264 st->num_rx_error, st->num_timeout);
265 }
266}
267
268static void sig_cb(int sig)
269{
270 switch (sig) {
271 case SIGINT:
272 print_report();
273 exit(0);
274 break;
275 }
276}
277
278void *tall_bsc_ctx = NULL;
279
280/* default categories */
Neels Hofmeyref022782016-12-08 21:28:29 +0100281static struct log_info_cat default_categories[] = {
Harald Welte4f8e34b2016-05-06 23:27:38 +0200282};
283
Neels Hofmeyref022782016-12-08 21:28:29 +0100284static const struct log_info gsup_test_client_log_info = {
285 .cat = default_categories,
286 .num_cat = ARRAY_SIZE(default_categories),
Harald Welte4f8e34b2016-05-06 23:27:38 +0200287};
288
289int main(int argc, char **argv)
290{
291 unsigned long long i;
292 char *server_host = "127.0.0.1";
Harald Welte2483f1b2016-06-19 18:06:02 +0200293 uint16_t server_port = OSMO_GSUP_PORT;
Harald Welte4f8e34b2016-05-06 23:27:38 +0200294
Neels Hofmeyref022782016-12-08 21:28:29 +0100295 osmo_init_logging(&gsup_test_client_log_info);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200296
Neels Hofmeyrf2ba8132017-03-04 03:15:53 +0100297 g_gc = gsup_client_create("GSUPTEST", server_host, server_port,
298 gsupc_read_cb, NULL);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200299
300
301 signal(SIGINT, sig_cb);
302
303 for (i = 0; i < 10000; i++) {
304 unsigned long long imsi = 901790000000000 + i;
Max770fbd22018-01-24 12:48:33 +0100305 char imsi_buf[17] = { 0 };
306 int rc;
307
Harald Welte4f8e34b2016-05-06 23:27:38 +0200308 snprintf(imsi_buf, sizeof(imsi_buf), "%015llu", imsi);
Max770fbd22018-01-24 12:48:33 +0100309 rc = req_auth_info(imsi_buf);
310 if (rc < 0)
311 printf("Failed to request Auth Info for %s\n", imsi_buf);
312
Harald Welte4f8e34b2016-05-06 23:27:38 +0200313 osmo_select_main(0);
314 }
315
316 while (1) {
317 osmo_select_main(0);
318 }
319
320 print_report();
321 exit(0);
322}