aboutsummaryrefslogtreecommitdiffstats
path: root/src/proxy_mm.c
blob: 12d3e5c2665d1fef600d6aa27f41c19524e43688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/fsm.h>
#include <osmocom/core/tdef.h>
#include <osmocom/gsm/apn.h>
#include <osmocom/hlr/hlr.h>
#include <osmocom/hlr/proxy_mm.h>
#include <osmocom/hlr/proxy_db.h>

enum proxy_mm_fsm_state {
	PROXY_MM_ST_READY,
	PROXY_MM_ST_WAIT_SUBSCR_DATA,
	PROXY_MM_ST_WAIT_GSUP_ISD_RESULT,
	PROXY_MM_ST_WAIT_AUTH_TUPLES,
};

static const struct value_string proxy_mm_fsm_event_names[] = {
	OSMO_VALUE_STRING(PROXY_MM_EV_SUBSCR_INVALID),
	OSMO_VALUE_STRING(PROXY_MM_EV_RX_GSUP_LU),
	OSMO_VALUE_STRING(PROXY_MM_EV_RX_GSUP_SAI),
	OSMO_VALUE_STRING(PROXY_MM_EV_RX_SUBSCR_DATA),
	OSMO_VALUE_STRING(PROXY_MM_EV_RX_GSUP_ISD_RESULT),
	OSMO_VALUE_STRING(PROXY_MM_EV_RX_AUTH_TUPLES),
	{}
};

static struct osmo_fsm proxy_mm_fsm;
static struct osmo_fsm proxy_to_home_fsm;

struct osmo_tdef proxy_mm_tdefs[] = {
// FIXME
	{ .T=-1, .default_val=5, .desc="proxy_mm ready timeout" },
	{ .T=-2, .default_val=5, .desc="proxy_mm wait_subscr_data timeout" },
	{ .T=-3, .default_val=5, .desc="proxy_mm wait_gsup_isd_result timeout" },
	{ .T=-4, .default_val=5, .desc="proxy_mm wait_auth_tuples timeout" },
	{}
};

static const struct osmo_tdef_state_timeout proxy_mm_fsm_timeouts[32] = {
// FIXME
	[PROXY_MM_ST_READY] = { .T=-1 },
	[PROXY_MM_ST_WAIT_SUBSCR_DATA] = { .T=-2 },
	[PROXY_MM_ST_WAIT_GSUP_ISD_RESULT] = { .T=-3 },
	[PROXY_MM_ST_WAIT_AUTH_TUPLES] = { .T=-4 },
};

#define proxy_mm_fsm_state_chg(state) \
	osmo_tdef_fsm_inst_state_chg(mm_fi, state, \
				     proxy_mm_fsm_timeouts, \
				     proxy_mm_tdefs, \
				     5)

LLIST_HEAD(proxy_mm_list);

struct proxy_mm *proxy_mm_alloc(const struct osmo_gsup_peer_id *vlr_name,
				bool is_ps,
				const char *imsi)
{
	struct proxy_mm *proxy_mm;

	struct osmo_fsm_inst *mm_fi = osmo_fsm_inst_alloc(&proxy_mm_fsm, g_hlr, NULL, LOGL_DEBUG, imsi);
	OSMO_ASSERT(mm_fi);

	proxy_mm = talloc(mm_fi, struct proxy_mm);
	OSMO_ASSERT(proxy_mm);
	mm_fi->priv = proxy_mm;
	*proxy_mm = (struct proxy_mm){
		.mm_fi = mm_fi,
		.is_ps = is_ps,
	};
	OSMO_STRLCPY_ARRAY(proxy_mm->imsi, imsi);
	INIT_LLIST_HEAD(&proxy_mm->auth_cache);

	llist_add(&proxy_mm->entry, &proxy_mm_list);

	proxy_mm->to_home_fi = osmo_fsm_inst_alloc_child(&proxy_to_home_fsm, mm_fi, PROXY_MM_EV_SUBSCR_INVALID);
	proxy_mm->to_home_fi->priv = proxy_mm;

	/* Do a state change to activate timeout */
	proxy_mm_fsm_state_chg(PROXY_MM_ST_READY);

	return proxy_mm;
}

void proxy_mm_add_auth_vectors(struct proxy_mm *proxy_mm,
			       const struct osmo_auth_vector *auth_vectors, size_t num_auth_vectors)
{
	struct proxy_mm_auth_cache *ac = talloc_zero(proxy_mm, struct proxy_mm_auth_cache);
	int i;
	OSMO_ASSERT(ac);
	ac->num_auth_vectors = num_auth_vectors;
	for (i = 0; i < num_auth_vectors; i++)
		ac->auth_vectors[i] = auth_vectors[i];
	if (proxy_db_add_auth_vectors(&proxy_mm->vlr_name, ac)) {
		talloc_free(ac);
		return;
	}
	llist_add(&ac->entry, &proxy_mm->auth_cache);
}

struct proxy_mm_auth_cache *proxy_mm_get_auth_vectors(struct proxy_mm *proxy_mm)
{
	struct proxy_mm_auth_cache *i;
	struct proxy_mm_auth_cache *ac = NULL;

	llist_for_each_entry(i, &proxy_mm->auth_cache, entry) {
		if (!ac || i->sent_to_vlr_count < ac->sent_to_vlr_count) {
			ac = i;
		}
	}

	/* ac now points to (one of) the least used auth cache entries (or NULL if none). */
	return ac;
}

void proxy_mm_discard_auth_vectors(struct proxy_mm *proxy_mm, struct proxy_mm_auth_cache *ac)
{
	proxy_db_drop_auth_vectors(ac->db_id);
	llist_del(&ac->entry);
	talloc_free(ac);
}

/* Mark given auth cache entries as sent to the VLR and clean up if necessary. */
void proxy_mm_use_auth_vectors(struct proxy_mm *proxy_mm, struct proxy_mm_auth_cache *ac)
{
	struct proxy_mm_auth_cache *i, *i_next;
	bool found_fresh_ac = false;

	/* The aim is to keep at least one set of already used auth tuples in the cache. If there are still fresh ones,
	 * all used auth vectors can be discarded. If there are no fresh ones left, keep only this last set. */

	llist_for_each_entry_safe(i, i_next, &proxy_mm->auth_cache, entry) {
		if (i == ac)
			continue;
		if (i->sent_to_vlr_count) {
			/* An auth entry other than this freshly used one, which has been used before.
			 * No need to keep it. */
			proxy_mm_discard_auth_vectors(proxy_mm, i);
			continue;
		}
		if (!i->sent_to_vlr_count)
			found_fresh_ac = true;
	}

	if (found_fresh_ac) {
		/* There are still other, fresh auth vectors. */
		proxy_mm_discard_auth_vectors(proxy_mm, ac);
	} else {
		/* else, only this ac remains in the list */
		ac->sent_to_vlr_count++;
		proxy_db_auth_vectors_update_sent_count(ac);
	}
}

static void proxy_mm_ready_action(struct osmo_fsm_inst *mm_fi, uint32_t event, void *data)
{
	struct proxy *proxy = g_hlr->gs->proxy;
	struct proxy_mm *proxy_mm = mm_fi->priv;

	switch (event) {

	case PROXY_MM_EV_SUBSCR_INVALID:
		/* Home HLR has responded and rejected a Location Updating, or no home HLR could be found. The
		 * subscriber is invalid, remove it from the cache. */
		proxy_subscr_del(proxy, proxy_mm->imsi);
		osmo_fsm_inst_term(mm_fi, OSMO_FSM_TERM_REGULAR, NULL);
		break;

	case PROXY_MM_EV_RX_GSUP_LU:
		/* The MSC asks for a LU. If we don't know details about this subscriber, then we'll have to wait for the
		 * home HLR to respond. If we already know details about the subscriber, we respond immediately (with
		 * Insert Subscriber Data and accept the LU), but also ask the home HLR to confirm the LU later. */
		osmo_fsm_inst_dispatch(proxy_mm->to_home_fi, PROXY_TO_HOME_EV_CONFIRM_LU, NULL);

		if (proxy_mm_subscriber_data_known(proxy_mm))
			proxy_mm_fsm_state_chg(PROXY_MM_ST_WAIT_GSUP_ISD_RESULT);
		else
			proxy_mm_fsm_state_chg(PROXY_MM_ST_WAIT_SUBSCR_DATA);
		break;

	case PROXY_MM_EV_RX_GSUP_SAI:
		// FIXME
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static int proxy_mm_ready_timeout(struct osmo_fsm_inst *mm_fi)
{
	/* Return 1 to terminate FSM instance, 0 to keep running */
	return 1;
}

void proxy_mm_wait_subscr_data_onenter(struct osmo_fsm_inst *mm_fi, uint32_t prev_state)
{
	//struct proxy_mm *proxy_mm = mm_fi->priv;
	// FIXME
}

static void proxy_mm_wait_subscr_data_action(struct osmo_fsm_inst *mm_fi, uint32_t event, void *data)
{
	//struct proxy_mm *proxy_mm = mm_fi->priv;

	switch (event) {

	case PROXY_MM_EV_RX_SUBSCR_DATA:
		// FIXME
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static int proxy_mm_wait_subscr_data_timeout(struct osmo_fsm_inst *mm_fi)
{
	/* Return 1 to terminate FSM instance, 0 to keep running */
	return 1;
}

static void proxy_mm_lu_error(struct osmo_fsm_inst *mm_fi)
{
	osmo_gsup_req_respond_err(req, GMM_CAUSE_ROAMING_NOTALLOWED,
				  "LU does not accept GSUP rx");

}

void proxy_mm_wait_gsup_isd_result_onenter(struct osmo_fsm_inst *mm_fi, uint32_t prev_state)
{
	struct proxy_mm *proxy_mm = mm_fi->priv;
	struct proxy_subscr proxy_subscr;
	struct osmo_gsup_message isd_req;

	uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
	uint8_t apn[APN_MAXLEN];

	isd_req.message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST;

	if (proxy_subscr_get_by_imsi(&proxy_subscr, g_hlr->gs->proxy, proxy_mm->imsi)) {
		LOGPFSML(mm_fi, LOGL_ERROR,
			 "Proxy: trying to send cached Subscriber Data, but there is no proxy entry\n");
		proxy_mm_lu_error(mm_fi);
		return;
	}

	if (proxy_subscr.msisdn[0] == '\0') {
		LOGPFSML(mm_fi, LOGL_ERROR,
			 "Proxy: trying to send cached Subscriber Data, but subscriber has no MSISDN in proxy cache\n");
		proxy_mm_lu_error(mm_fi);
		return;
	}

	if (osmo_gsup_create_insert_subscriber_data_msg(&isd_req, proxy_mm->imsi,
							proxy_subscr->msisdn, msisdn_enc, sizeof(msisdn_enc),
							apn, sizeof(apn),
							proxy_mm->is_ps? OSMO_GSUP_CN_DOMAIN_PS : OSMO_GSUP_CN_DOMAIN_CS)) {
		LOGPFSML(mm_fi, LOGL_ERROR, "Proxy: failed to send cached Subscriber Data\n");
		proxy_mm_lu_error(mm_fi);
		return;
	}
}

static void proxy_mm_wait_gsup_isd_result_action(struct osmo_fsm_inst *mm_fi, uint32_t event, void *data)
{
	//struct proxy_mm *proxy_mm = mm_fi->priv;

	switch (event) {

	case PROXY_MM_EV_RX_GSUP_ISD_RESULT:
		// FIXME
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static int proxy_mm_wait_gsup_isd_result_timeout(struct osmo_fsm_inst *mm_fi)
{
	/* Return 1 to terminate FSM instance, 0 to keep running */
	return 1;
}

void proxy_mm_wait_auth_tuples_onenter(struct osmo_fsm_inst *mm_fi, uint32_t prev_state)
{
	//struct proxy_mm *proxy_mm = mm_fi->priv;
	// FIXME
}

static void proxy_mm_wait_auth_tuples_action(struct osmo_fsm_inst *mm_fi, uint32_t event, void *data)
{
	//struct proxy_mm *proxy_mm = mm_fi->priv;

	switch (event) {

	case PROXY_MM_EV_RX_AUTH_TUPLES:
		// FIXME
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static int proxy_mm_wait_auth_tuples_timeout(struct osmo_fsm_inst *mm_fi)
{
	/* Return 1 to terminate FSM instance, 0 to keep running */
	return 1;
}

#define S(x)    (1 << (x))

static const struct osmo_fsm_state proxy_mm_fsm_states[] = {
	[PROXY_MM_ST_READY] = {
		.name = "ready",
		.in_event_mask = 0
			| S(PROXY_MM_EV_SUBSCR_INVALID)
			| S(PROXY_MM_EV_RX_GSUP_LU)
			| S(PROXY_MM_EV_RX_GSUP_SAI)
			,
		.out_state_mask = 0
			| S(PROXY_MM_ST_READY)
			| S(PROXY_MM_ST_WAIT_SUBSCR_DATA)
			| S(PROXY_MM_ST_WAIT_GSUP_ISD_RESULT)
			| S(PROXY_MM_ST_WAIT_AUTH_TUPLES)
			,
		.action = proxy_mm_ready_action,
	},
	[PROXY_MM_ST_WAIT_SUBSCR_DATA] = {
		.name = "wait_subscr_data",
		.in_event_mask = 0
			| S(PROXY_MM_EV_RX_SUBSCR_DATA)
			,
		.out_state_mask = 0
			| S(PROXY_MM_ST_WAIT_GSUP_ISD_RESULT)
			| S(PROXY_MM_ST_READY)
			,
		.onenter = proxy_mm_wait_subscr_data_onenter,
		.action = proxy_mm_wait_subscr_data_action,
	},
	[PROXY_MM_ST_WAIT_GSUP_ISD_RESULT] = {
		.name = "wait_gsup_isd_result",
		.in_event_mask = 0
			| S(PROXY_MM_EV_RX_GSUP_ISD_RESULT)
			,
		.out_state_mask = 0
			| S(PROXY_MM_ST_READY)
			,
		.onenter = proxy_mm_wait_gsup_isd_result_onenter,
		.action = proxy_mm_wait_gsup_isd_result_action,
	},
	[PROXY_MM_ST_WAIT_AUTH_TUPLES] = {
		.name = "wait_auth_tuples",
		.in_event_mask = 0
			| S(PROXY_MM_EV_RX_AUTH_TUPLES)
			,
		.out_state_mask = 0
			| S(PROXY_MM_ST_READY)
			,
		.onenter = proxy_mm_wait_auth_tuples_onenter,
		.action = proxy_mm_wait_auth_tuples_action,
	},
};

static int proxy_mm_fsm_timer_cb(struct osmo_fsm_inst *mm_fi)
{
	//struct proxy_mm *proxy_mm = mm_fi->priv;
	switch (mm_fi->state) {

	case PROXY_MM_ST_READY:
		return proxy_mm_ready_timeout(mm_fi);

	case PROXY_MM_ST_WAIT_SUBSCR_DATA:
		return proxy_mm_wait_subscr_data_timeout(mm_fi);

	case PROXY_MM_ST_WAIT_GSUP_ISD_RESULT:
		return proxy_mm_wait_gsup_isd_result_timeout(mm_fi);

	case PROXY_MM_ST_WAIT_AUTH_TUPLES:
		return proxy_mm_wait_auth_tuples_timeout(mm_fi);

	default:
		/* Return 1 to terminate FSM instance, 0 to keep running */
		return 1;
	}
}

void proxy_mm_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
{
	struct proxy_mm *proxy_mm = fi->priv;
	llist_del(&proxy_mm->entry);
}

static struct osmo_fsm proxy_mm_fsm = {
	.name = "proxy_mm",
	.states = proxy_mm_fsm_states,
	.num_states = ARRAY_SIZE(proxy_mm_fsm_states),
	.log_subsys = DLGLOBAL, // FIXME
	.event_names = proxy_mm_fsm_event_names,
	.timer_cb = proxy_mm_fsm_timer_cb,
	.cleanup = proxy_mm_fsm_cleanup,
};

static __attribute__((constructor)) void proxy_mm_fsm_register(void)
{
	OSMO_ASSERT(osmo_fsm_register(&proxy_mm_fsm) == 0);
}

bool proxy_mm_subscriber_data_known(const struct proxy_mm *proxy_mm)
{
	struct proxy_subscr proxy_subscr;
	if (proxy_subscr_get_by_imsi(&proxy_subscr, g_hlr->gs->proxy, proxy_mm->imsi))
		return false;
	return proxy_subscr.msisdn[0] != '\0';
}