aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/gprs/gb_proxy_tlli.c
blob: 3b3b976a5785c747771e53039a2834c381d2aa3b (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/* Gb-proxy TLLI state handling */

/* (C) 2014 by On-Waves
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <osmocom/gsm/gsm48.h>

#include <openbsc/gb_proxy.h>

#include <openbsc/gprs_utils.h>
#include <openbsc/gprs_gb_parse.h>

#include <openbsc/debug.h>

#include <osmocom/gsm/gsm_utils.h>

#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/talloc.h>

struct gbproxy_link_info *gbproxy_link_info_by_tlli(struct gbproxy_peer *peer,
					    uint32_t tlli)
{
	struct gbproxy_link_info *link_info;
	struct gbproxy_patch_state *state = &peer->patch_state;

	if (!tlli)
		return NULL;

	llist_for_each_entry(link_info, &state->logical_links, list)
		if (link_info->tlli.current == tlli ||
		    link_info->tlli.assigned == tlli)
			return link_info;

	return NULL;
}

struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
	struct gbproxy_peer *peer,
	uint32_t ptmsi)
{
	struct gbproxy_link_info *link_info;
	struct gbproxy_patch_state *state = &peer->patch_state;

	if (ptmsi == GSM_RESERVED_TMSI)
		return NULL;

	llist_for_each_entry(link_info, &state->logical_links, list)
		if (link_info->tlli.ptmsi == ptmsi)
			return link_info;

	return NULL;
}

struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
	struct gbproxy_peer *peer,
	uint32_t tlli)
{
	struct gbproxy_link_info *link_info;
	struct gbproxy_patch_state *state = &peer->patch_state;

	if (!tlli)
		return NULL;

	/* Don't care about the NSEI */
	llist_for_each_entry(link_info, &state->logical_links, list)
		if (link_info->sgsn_tlli.current == tlli ||
		     link_info->sgsn_tlli.assigned == tlli)
			return link_info;

	return NULL;
}

struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
	struct gbproxy_peer *peer,
	uint32_t tlli, uint32_t sgsn_nsei)
{
	struct gbproxy_link_info *link_info;
	struct gbproxy_patch_state *state = &peer->patch_state;

	if (!tlli)
		return NULL;

	llist_for_each_entry(link_info, &state->logical_links, list)
		if ((link_info->sgsn_tlli.current == tlli ||
		     link_info->sgsn_tlli.assigned == tlli) &&
		    link_info->sgsn_nsei == sgsn_nsei)
			return link_info;

	return NULL;
}

struct gbproxy_link_info *gbproxy_link_info_by_imsi(
	struct gbproxy_peer *peer,
	const uint8_t *imsi,
	size_t imsi_len)
{
	struct gbproxy_link_info *link_info;
	struct gbproxy_patch_state *state = &peer->patch_state;

	if (!gprs_is_mi_imsi(imsi, imsi_len))
		return NULL;

	llist_for_each_entry(link_info, &state->logical_links, list) {
		if (link_info->imsi_len != imsi_len)
			continue;
		if (memcmp(link_info->imsi, imsi, imsi_len) != 0)
			continue;

		return link_info;
	}

	return NULL;
}

void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info)
{
	struct msgb *msg, *nxt;

	llist_for_each_entry_safe(msg, nxt, &link_info->stored_msgs, list) {
		llist_del(&msg->list);
		msgb_free(msg);
	}
}

void gbproxy_delete_link_info(struct gbproxy_peer *peer,
			 struct gbproxy_link_info *link_info)
{
	struct gbproxy_patch_state *state = &peer->patch_state;

	gbproxy_link_info_discard_messages(link_info);

	llist_del(&link_info->list);
	talloc_free(link_info);
	state->logical_link_count -= 1;

	peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
		state->logical_link_count;
}

void gbproxy_delete_link_infos(struct gbproxy_peer *peer)
{
	struct gbproxy_link_info *link_info, *nxt;
	struct gbproxy_patch_state *state = &peer->patch_state;

	llist_for_each_entry_safe(link_info, nxt, &state->logical_links, list)
		gbproxy_delete_link_info(peer, link_info);

	OSMO_ASSERT(state->logical_link_count == 0);
	OSMO_ASSERT(llist_empty(&state->logical_links));
}

void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
			      struct gbproxy_link_info *link_info)
{
	struct gbproxy_patch_state *state = &peer->patch_state;

	link_info->timestamp = now;
	llist_add(&link_info->list, &state->logical_links);
	state->logical_link_count += 1;

	peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
		state->logical_link_count;
}

int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now)
{
	struct gbproxy_patch_state *state = &peer->patch_state;
	int exceeded_max_len = 0;
	int deleted_count = 0;
	int check_for_age;

	if (peer->cfg->tlli_max_len > 0)
		exceeded_max_len =
			state->logical_link_count - peer->cfg->tlli_max_len;

	check_for_age = peer->cfg->tlli_max_age > 0;

	for (; exceeded_max_len > 0; exceeded_max_len--) {
		struct gbproxy_link_info *link_info;
		OSMO_ASSERT(!llist_empty(&state->logical_links));
		link_info = llist_entry(state->logical_links.prev,
					struct gbproxy_link_info,
					list);
		LOGP(DGPRS, LOGL_INFO,
		     "Removing TLLI %08x from list "
		     "(stale, length %d, max_len exceeded)\n",
		     link_info->tlli.current, state->logical_link_count);

		gbproxy_delete_link_info(peer, link_info);
		deleted_count += 1;
	}

	while (check_for_age && !llist_empty(&state->logical_links)) {
		time_t age;
		struct gbproxy_link_info *link_info;
		link_info = llist_entry(state->logical_links.prev,
					struct gbproxy_link_info,
					list);
		age = now - link_info->timestamp;
		/* age < 0 only happens after system time jumps, discard entry */
		if (age <= peer->cfg->tlli_max_age && age >= 0) {
			check_for_age = 0;
			continue;
		}

		LOGP(DGPRS, LOGL_INFO,
		     "Removing TLLI %08x from list "
		     "(stale, age %d, max_age exceeded)\n",
		     link_info->tlli.current, (int)age);

		gbproxy_delete_link_info(peer, link_info);
		deleted_count += 1;
	}

	return deleted_count;
}

struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer)
{
	struct gbproxy_link_info *link_info;

	link_info = talloc_zero(peer, struct gbproxy_link_info);
	link_info->tlli.ptmsi = GSM_RESERVED_TMSI;
	link_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI;

	link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;

	INIT_LLIST_HEAD(&link_info->stored_msgs);

	return link_info;
}

void gbproxy_detach_link_info(
	struct gbproxy_peer *peer,
	struct gbproxy_link_info *link_info)
{
	struct gbproxy_patch_state *state = &peer->patch_state;

	llist_del(&link_info->list);
	OSMO_ASSERT(state->logical_link_count > 0);
	state->logical_link_count -= 1;

	peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
		state->logical_link_count;
}

void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
			      const uint8_t *imsi, size_t imsi_len)
{
	if (!gprs_is_mi_imsi(imsi, imsi_len))
		return;

	link_info->imsi_len = imsi_len;
	link_info->imsi =
		talloc_realloc_size(link_info, link_info->imsi, imsi_len);
	OSMO_ASSERT(link_info->imsi != NULL);
	memcpy(link_info->imsi, imsi, imsi_len);
}

void gbproxy_reassign_tlli(struct gbproxy_tlli_state *tlli_state,
			   struct gbproxy_peer *peer, uint32_t new_tlli)
{
	if (new_tlli == tlli_state->current)
		return;

	LOGP(DGPRS, LOGL_INFO,
	     "The TLLI has been reassigned from %08x to %08x\n",
	     tlli_state->current, new_tlli);

	/* Remember assigned TLLI */
	tlli_state->assigned = new_tlli;
	tlli_state->bss_validated = 0;
	tlli_state->net_validated = 0;
}

uint32_t gbproxy_map_tlli(uint32_t other_tlli,
			  struct gbproxy_link_info *link_info, int to_bss)
{
	uint32_t tlli = 0;
	struct gbproxy_tlli_state *src, *dst;
	if (to_bss) {
		src = &link_info->sgsn_tlli;
		dst = &link_info->tlli;
	} else {
		src = &link_info->tlli;
		dst = &link_info->sgsn_tlli;
	}
	if (src->current == other_tlli)
		tlli = dst->current;
	else if (src->assigned == other_tlli)
		tlli = dst->assigned;

	return tlli;
}

static void gbproxy_validate_tlli(struct gbproxy_tlli_state *tlli_state,
				  uint32_t tlli, int to_bss)
{
	LOGP(DGPRS, LOGL_DEBUG,
	     "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n",
	     __func__, tlli_state->current, tlli_state->assigned,
	     tlli_state->net_validated, tlli_state->bss_validated, tlli);

	if (!tlli_state->assigned || tlli_state->assigned != tlli)
		return;

	/* TODO: Is this ok? Check spec */
	if (gprs_tlli_type(tlli) != TLLI_LOCAL)
		return;

	/* See GSM 04.08, 4.7.1.5 */
	if (to_bss)
		tlli_state->net_validated = 1;
	else
		tlli_state->bss_validated = 1;

	if (!tlli_state->bss_validated || !tlli_state->net_validated)
		return;

	LOGP(DGPRS, LOGL_INFO,
	     "The TLLI %08x has been validated (was %08x)\n",
	     tlli_state->assigned, tlli_state->current);

	tlli_state->current = tlli;
	tlli_state->assigned = 0;
}

static void gbproxy_touch_link_info(struct gbproxy_peer *peer,
				    struct gbproxy_link_info *link_info,
				    time_t now)
{
	gbproxy_detach_link_info(peer, link_info);
	gbproxy_attach_link_info(peer, now, link_info);
}

static int gbproxy_unregister_link_info(struct gbproxy_peer *peer,
					 struct gbproxy_link_info *link_info)
{
	if (!link_info)
		return 1;

	if (link_info->tlli.ptmsi == GSM_RESERVED_TMSI && !link_info->imsi_len) {
		LOGP(DGPRS, LOGL_INFO,
		     "Removing TLLI %08x from list (P-TMSI or IMSI are not set)\n",
		     link_info->tlli.current);
		gbproxy_delete_link_info(peer, link_info);
		return 1;
	}

	link_info->tlli.current = 0;
	link_info->tlli.assigned = 0;
	link_info->sgsn_tlli.current = 0;
	link_info->sgsn_tlli.assigned = 0;

	link_info->is_deregistered = 1;

	gbproxy_reset_link(link_info);

	return 0;
}

int gbproxy_imsi_matches(struct gbproxy_config *cfg,
			 enum gbproxy_match_id match_id,
			 struct gbproxy_link_info *link_info)
{
	struct gbproxy_match *match;
	OSMO_ASSERT(match_id >= 0 && match_id < ARRAY_SIZE(cfg->matches));

	match = &cfg->matches[match_id];
	if (!match->enable)
		return 1;

	return link_info != NULL && link_info->is_matching[match_id];
}

void gbproxy_assign_imsi(struct gbproxy_peer *peer,
			 struct gbproxy_link_info *link_info,
			 struct gprs_gb_parse_context *parse_ctx)
{
	int imsi_matches;
	struct gbproxy_link_info *other_link_info;
	enum gbproxy_match_id match_id;

	/* Make sure that there is a second entry with the same IMSI */
	other_link_info = gbproxy_link_info_by_imsi(
		peer, parse_ctx->imsi, parse_ctx->imsi_len);

	if (other_link_info && other_link_info != link_info) {
		char mi_buf[200];
		mi_buf[0] = '\0';
		gsm48_mi_to_string(mi_buf, sizeof(mi_buf),
				   parse_ctx->imsi, parse_ctx->imsi_len);
		LOGP(DGPRS, LOGL_INFO,
		     "Removing TLLI %08x from list (IMSI %s re-used)\n",
		     other_link_info->tlli.current, mi_buf);
		gbproxy_delete_link_info(peer, other_link_info);
	}

	/* Update the IMSI field */
	gbproxy_update_link_info(link_info,
				 parse_ctx->imsi, parse_ctx->imsi_len);

	/* Check, whether the IMSI matches */
	OSMO_ASSERT(ARRAY_SIZE(link_info->is_matching) ==
		    ARRAY_SIZE(peer->cfg->matches));
	for (match_id = 0; match_id < ARRAY_SIZE(link_info->is_matching);
	     ++match_id) {
		imsi_matches = gbproxy_check_imsi(
			&peer->cfg->matches[match_id],
			parse_ctx->imsi, parse_ctx->imsi_len);
		if (imsi_matches >= 0)
			link_info->is_matching[match_id] = imsi_matches;
	}
}

static int gbproxy_tlli_match(const struct gbproxy_tlli_state *a,
			      const struct gbproxy_tlli_state *b)
{
	if (a->current && a->current == b->current)
		return 1;

	if (a->assigned && a->assigned == b->assigned)
		return 1;

	if (a->ptmsi != GSM_RESERVED_TMSI && a->ptmsi == b->ptmsi)
		return 1;

	return 0;
}

static void gbproxy_remove_matching_link_infos(
	struct gbproxy_peer *peer, struct gbproxy_link_info *link_info)
{
	struct gbproxy_link_info *info, *nxt;
	struct gbproxy_patch_state *state = &peer->patch_state;

	/* Make sure that there is no second entry with the same P-TMSI or TLLI */
	llist_for_each_entry_safe(info, nxt, &state->logical_links, list) {
		if (info == link_info)
			continue;

		if (!gbproxy_tlli_match(&link_info->tlli, &info->tlli) &&
		    (link_info->sgsn_nsei != info->sgsn_nsei ||
		     !gbproxy_tlli_match(&link_info->sgsn_tlli, &info->sgsn_tlli)))
			continue;

		LOGP(DGPRS, LOGL_INFO,
		     "Removing TLLI %08x from list (P-TMSI/TLLI re-used)\n",
		     info->tlli.current);
		gbproxy_delete_link_info(peer, info);
	}
}

static struct gbproxy_link_info *gbproxy_get_link_info_ul(
	struct gbproxy_peer *peer,
	int *tlli_is_valid,
	struct gprs_gb_parse_context *parse_ctx)
{
	struct gbproxy_link_info *link_info = NULL;

	if (parse_ctx->tlli_enc) {
		link_info = gbproxy_link_info_by_tlli(peer, parse_ctx->tlli);

		if (link_info) {
			*tlli_is_valid = 1;
			return link_info;
		}
	}

	*tlli_is_valid = 0;

	if (!link_info && parse_ctx->imsi) {
		link_info = gbproxy_link_info_by_imsi(
			peer, parse_ctx->imsi, parse_ctx->imsi_len);
	}

	if (!link_info && parse_ctx->ptmsi_enc && !parse_ctx->old_raid_is_foreign) {
		uint32_t bss_ptmsi;
		gprs_parse_tmsi(parse_ctx->ptmsi_enc, &bss_ptmsi);
		link_info = gbproxy_link_info_by_ptmsi(peer, bss_ptmsi);
	}

	if (!link_info)
		return NULL;

	link_info->is_deregistered = 0;

	return link_info;
}

struct gbproxy_link_info *gbproxy_update_link_state_ul(
	struct gbproxy_peer *peer,
	time_t now,
	struct gprs_gb_parse_context *parse_ctx)
{
	struct gbproxy_link_info *link_info;
	int tlli_is_valid;

	link_info = gbproxy_get_link_info_ul(peer, &tlli_is_valid, parse_ctx);

	if (parse_ctx->tlli_enc && parse_ctx->llc) {
		uint32_t sgsn_tlli;

		if (!link_info) {
			LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n",
			    parse_ctx->tlli);
			link_info = gbproxy_link_info_alloc(peer);
			gbproxy_attach_link_info(peer, now, link_info);

			/* Setup TLLIs */
			sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
							   parse_ctx->tlli);
			link_info->sgsn_tlli.current = sgsn_tlli;
			link_info->tlli.current = parse_ctx->tlli;
		} else if (!tlli_is_valid) {
			/* New TLLI (info found by IMSI or P-TMSI) */
			link_info->tlli.current = parse_ctx->tlli;
			link_info->tlli.assigned = 0;
			link_info->sgsn_tlli.current =
				gbproxy_make_sgsn_tlli(peer, link_info,
						       parse_ctx->tlli);
			link_info->sgsn_tlli.assigned = 0;
			gbproxy_touch_link_info(peer, link_info, now);
		} else {
			sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, link_info, 0);
			if (!sgsn_tlli)
				sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
								   parse_ctx->tlli);

			gbproxy_validate_tlli(&link_info->tlli,
					      parse_ctx->tlli, 0);
			gbproxy_validate_tlli(&link_info->sgsn_tlli,
					      sgsn_tlli, 0);
			gbproxy_touch_link_info(peer, link_info, now);
		}
	} else if (link_info) {
		gbproxy_touch_link_info(peer, link_info, now);
	}

	if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
		gbproxy_assign_imsi(peer, link_info, parse_ctx);

	return link_info;
}

static struct gbproxy_link_info *gbproxy_get_link_info_dl(
	struct gbproxy_peer *peer,
	struct gprs_gb_parse_context *parse_ctx)
{
	struct gbproxy_link_info *link_info = NULL;

	/* Which key to use depends on its availability only, if that fails, do
	 * not retry it with another key (e.g. IMSI). */
	if (parse_ctx->tlli_enc)
		link_info = gbproxy_link_info_by_sgsn_tlli(peer, parse_ctx->tlli,
							   parse_ctx->peer_nsei);

	/* TODO: Get link_info by (SGSN) P-TMSI if that is available (see
	 * GSM 08.18, 7.2) instead of using the IMSI as key. */
	else if (parse_ctx->imsi)
		link_info = gbproxy_link_info_by_imsi(
			peer, parse_ctx->imsi, parse_ctx->imsi_len);

	if (link_info)
		link_info->is_deregistered = 0;

	return link_info;
}

struct gbproxy_link_info *gbproxy_update_link_state_dl(
	struct gbproxy_peer *peer,
	time_t now,
	struct gprs_gb_parse_context *parse_ctx)
{
	struct gbproxy_link_info *link_info = NULL;

	link_info = gbproxy_get_link_info_dl(peer, parse_ctx);

	if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && link_info) {
		/* A new P-TMSI has been signalled in the message,
		 * register new TLLI */
		uint32_t new_sgsn_ptmsi;
		uint32_t new_bss_ptmsi = GSM_RESERVED_TMSI;
		gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_sgsn_ptmsi);

		if (link_info->sgsn_tlli.ptmsi == new_sgsn_ptmsi)
			new_bss_ptmsi = link_info->tlli.ptmsi;

		if (new_bss_ptmsi == GSM_RESERVED_TMSI)
			new_bss_ptmsi = gbproxy_make_bss_ptmsi(peer, new_sgsn_ptmsi);

		LOGP(DGPRS, LOGL_INFO,
		     "Got new PTMSI %08x from SGSN, using %08x for BSS\n",
		     new_sgsn_ptmsi, new_bss_ptmsi);
		/* Setup PTMSIs */
		link_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi;
		link_info->tlli.ptmsi = new_bss_ptmsi;
	} else if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && !link_info &&
		   !peer->cfg->patch_ptmsi) {
		/* A new P-TMSI has been signalled in the message with an unknown
		 * TLLI, create a new link_info */
		/* TODO: Add a test case for this branch */
		uint32_t new_ptmsi;
		gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);

		LOGP(DGPRS, LOGL_INFO,
		     "Adding TLLI %08x to list (SGSN, new P-TMSI is %08x)\n",
		     parse_ctx->tlli, new_ptmsi);

		link_info = gbproxy_link_info_alloc(peer);
		link_info->sgsn_tlli.current = parse_ctx->tlli;
		link_info->tlli.current = parse_ctx->tlli;
		link_info->sgsn_tlli.ptmsi = new_ptmsi;
		link_info->tlli.ptmsi = new_ptmsi;
		gbproxy_attach_link_info(peer, now, link_info);
	} else if (parse_ctx->tlli_enc && parse_ctx->llc && !link_info &&
		   !peer->cfg->patch_ptmsi) {
		/* Unknown SGSN TLLI, create a new link_info */
		uint32_t new_ptmsi;
		link_info = gbproxy_link_info_alloc(peer);
		LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n",
		     parse_ctx->tlli);

		gbproxy_attach_link_info(peer, now, link_info);

		/* Setup TLLIs */
		link_info->sgsn_tlli.current = parse_ctx->tlli;
		link_info->tlli.current = parse_ctx->tlli;

		if (!parse_ctx->new_ptmsi_enc)
			return link_info;
		/* A new P-TMSI has been signalled in the message */

		gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
		LOGP(DGPRS, LOGL_INFO,
		     "Assigning new P-TMSI %08x\n", new_ptmsi);
		/* Setup P-TMSIs */
		link_info->sgsn_tlli.ptmsi = new_ptmsi;
		link_info->tlli.ptmsi = new_ptmsi;
	} else if (parse_ctx->tlli_enc && parse_ctx->llc && link_info) {
		uint32_t bss_tlli = gbproxy_map_tlli(parse_ctx->tlli,
						     link_info, 1);
		gbproxy_validate_tlli(&link_info->sgsn_tlli, parse_ctx->tlli, 1);
		gbproxy_validate_tlli(&link_info->tlli, bss_tlli, 1);
		gbproxy_touch_link_info(peer, link_info, now);
	} else if (link_info) {
		gbproxy_touch_link_info(peer, link_info, now);
	}

	if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
		gbproxy_assign_imsi(peer, link_info, parse_ctx);

	return link_info;
}

int gbproxy_update_link_state_after(
	struct gbproxy_peer *peer,
	struct gbproxy_link_info *link_info,
	time_t now,
	struct gprs_gb_parse_context *parse_ctx)
{
	int rc = 0;
	if (parse_ctx->invalidate_tlli && link_info) {
		int keep_info =
			peer->cfg->keep_link_infos == GBPROX_KEEP_ALWAYS ||
			(peer->cfg->keep_link_infos == GBPROX_KEEP_REATTACH &&
			 parse_ctx->await_reattach) ||
			(peer->cfg->keep_link_infos == GBPROX_KEEP_IDENTIFIED &&
			 link_info->imsi_len > 0);
		if (keep_info) {
			LOGP(DGPRS, LOGL_INFO, "Unregistering TLLI %08x\n",
			     link_info->tlli.current);
			rc = gbproxy_unregister_link_info(peer, link_info);
		} else {
			LOGP(DGPRS, LOGL_INFO, "Removing TLLI %08x from list\n",
			     link_info->tlli.current);
			gbproxy_delete_link_info(peer, link_info);
			rc = 1;
		}
	} else if (parse_ctx->to_bss && parse_ctx->tlli_enc &&
		   parse_ctx->new_ptmsi_enc && link_info) {
		/* A new PTMSI has been signaled in the message,
		 * register new TLLI */
		uint32_t new_sgsn_ptmsi = link_info->sgsn_tlli.ptmsi;
		uint32_t new_bss_ptmsi = link_info->tlli.ptmsi;
		uint32_t new_sgsn_tlli;
		uint32_t new_bss_tlli = 0;

		new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL);
		if (new_bss_ptmsi != GSM_RESERVED_TMSI)
			new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL);
		LOGP(DGPRS, LOGL_INFO,
		     "Assigning new TLLI %08x to SGSN, %08x to BSS\n",
		     new_sgsn_tlli, new_bss_tlli);

		gbproxy_reassign_tlli(&link_info->sgsn_tlli,
				      peer, new_sgsn_tlli);
		gbproxy_reassign_tlli(&link_info->tlli,
				      peer, new_bss_tlli);
		gbproxy_remove_matching_link_infos(peer, link_info);
	}

	gbproxy_remove_stale_link_infos(peer, now);

	return rc;
}