blob: b84d1ea4ed05f20ae13039da32f0bc5a3e66a5c3 [file] [log] [blame]
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001/* Gb-proxy TLLI state handling */
2
3/* (C) 2014 by On-Waves
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 <osmocom/gsm/gsm48.h>
22
23#include <osmocom/sgsn/gb_proxy.h>
24
25#include <osmocom/sgsn/gprs_utils.h>
26#include <osmocom/sgsn/gprs_gb_parse.h>
27
28#include <osmocom/sgsn/debug.h>
29
30#include <osmocom/gsm/gsm_utils.h>
31
32#include <osmocom/core/rate_ctr.h>
33#include <osmocom/core/talloc.h>
34
35struct gbproxy_link_info *gbproxy_link_info_by_tlli(struct gbproxy_peer *peer,
36 uint32_t tlli)
37{
38 struct gbproxy_link_info *link_info;
39 struct gbproxy_patch_state *state = &peer->patch_state;
40
41 if (!tlli)
42 return NULL;
43
44 llist_for_each_entry(link_info, &state->logical_links, list)
45 if (link_info->tlli.current == tlli ||
46 link_info->tlli.assigned == tlli)
47 return link_info;
48
49 return NULL;
50}
51
52struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
53 struct gbproxy_peer *peer,
54 uint32_t ptmsi)
55{
56 struct gbproxy_link_info *link_info;
57 struct gbproxy_patch_state *state = &peer->patch_state;
58
59 if (ptmsi == GSM_RESERVED_TMSI)
60 return NULL;
61
62 llist_for_each_entry(link_info, &state->logical_links, list)
63 if (link_info->tlli.ptmsi == ptmsi)
64 return link_info;
65
66 return NULL;
67}
68
69struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
70 struct gbproxy_peer *peer,
71 uint32_t tlli)
72{
73 struct gbproxy_link_info *link_info;
74 struct gbproxy_patch_state *state = &peer->patch_state;
75
76 if (!tlli)
77 return NULL;
78
79 /* Don't care about the NSEI */
80 llist_for_each_entry(link_info, &state->logical_links, list)
81 if (link_info->sgsn_tlli.current == tlli ||
82 link_info->sgsn_tlli.assigned == tlli)
83 return link_info;
84
85 return NULL;
86}
87
88struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
89 struct gbproxy_peer *peer,
90 uint32_t tlli, uint32_t sgsn_nsei)
91{
92 struct gbproxy_link_info *link_info;
93 struct gbproxy_patch_state *state = &peer->patch_state;
94
95 if (!tlli)
96 return NULL;
97
98 llist_for_each_entry(link_info, &state->logical_links, list)
99 if ((link_info->sgsn_tlli.current == tlli ||
100 link_info->sgsn_tlli.assigned == tlli) &&
101 link_info->sgsn_nsei == sgsn_nsei)
102 return link_info;
103
104 return NULL;
105}
106
107struct gbproxy_link_info *gbproxy_link_info_by_imsi(
108 struct gbproxy_peer *peer,
109 const uint8_t *imsi,
110 size_t imsi_len)
111{
112 struct gbproxy_link_info *link_info;
113 struct gbproxy_patch_state *state = &peer->patch_state;
114
115 if (!gprs_is_mi_imsi(imsi, imsi_len))
116 return NULL;
117
118 llist_for_each_entry(link_info, &state->logical_links, list) {
119 if (link_info->imsi_len != imsi_len)
120 continue;
121 if (memcmp(link_info->imsi, imsi, imsi_len) != 0)
122 continue;
123
124 return link_info;
125 }
126
127 return NULL;
128}
129
130void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info)
131{
132 struct msgb *msg, *nxt;
133
134 llist_for_each_entry_safe(msg, nxt, &link_info->stored_msgs, list) {
135 llist_del(&msg->list);
136 msgb_free(msg);
137 }
138}
139
140void gbproxy_delete_link_info(struct gbproxy_peer *peer,
141 struct gbproxy_link_info *link_info)
142{
143 struct gbproxy_patch_state *state = &peer->patch_state;
144
145 gbproxy_link_info_discard_messages(link_info);
146
147 llist_del(&link_info->list);
148 talloc_free(link_info);
149 state->logical_link_count -= 1;
150
151 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
152 state->logical_link_count;
153}
154
155void gbproxy_delete_link_infos(struct gbproxy_peer *peer)
156{
157 struct gbproxy_link_info *link_info, *nxt;
158 struct gbproxy_patch_state *state = &peer->patch_state;
159
160 llist_for_each_entry_safe(link_info, nxt, &state->logical_links, list)
161 gbproxy_delete_link_info(peer, link_info);
162
163 OSMO_ASSERT(state->logical_link_count == 0);
164 OSMO_ASSERT(llist_empty(&state->logical_links));
165}
166
167void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
168 struct gbproxy_link_info *link_info)
169{
170 struct gbproxy_patch_state *state = &peer->patch_state;
171
172 link_info->timestamp = now;
173 llist_add(&link_info->list, &state->logical_links);
174 state->logical_link_count += 1;
175
176 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
177 state->logical_link_count;
178}
179
180int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now)
181{
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100182 OSMO_ASSERT(peer);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200183 struct gbproxy_patch_state *state = &peer->patch_state;
184 int exceeded_max_len = 0;
185 int deleted_count = 0;
186 int check_for_age;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100187 OSMO_ASSERT(peer->nse);
188 struct gbproxy_config *cfg = peer->nse->cfg;
189 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200190
Daniel Willmanne50550e2020-11-26 18:19:21 +0100191 if (cfg->tlli_max_len > 0)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200192 exceeded_max_len =
Daniel Willmanne50550e2020-11-26 18:19:21 +0100193 state->logical_link_count - cfg->tlli_max_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200194
Daniel Willmanne50550e2020-11-26 18:19:21 +0100195 check_for_age = cfg->tlli_max_age > 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200196
197 for (; exceeded_max_len > 0; exceeded_max_len--) {
198 struct gbproxy_link_info *link_info;
199 OSMO_ASSERT(!llist_empty(&state->logical_links));
200 link_info = llist_entry(state->logical_links.prev,
201 struct gbproxy_link_info,
202 list);
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100203 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200204 "Removing TLLI %08x from list "
205 "(stale, length %d, max_len exceeded)\n",
206 link_info->tlli.current, state->logical_link_count);
207
208 gbproxy_delete_link_info(peer, link_info);
209 deleted_count += 1;
210 }
211
212 while (check_for_age && !llist_empty(&state->logical_links)) {
213 time_t age;
214 struct gbproxy_link_info *link_info;
215 link_info = llist_entry(state->logical_links.prev,
216 struct gbproxy_link_info,
217 list);
218 age = now - link_info->timestamp;
219 /* age < 0 only happens after system time jumps, discard entry */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100220 if (age <= cfg->tlli_max_age && age >= 0) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200221 check_for_age = 0;
222 continue;
223 }
224
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100225 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200226 "Removing TLLI %08x from list "
227 "(stale, age %d, max_age exceeded)\n",
228 link_info->tlli.current, (int)age);
229
230 gbproxy_delete_link_info(peer, link_info);
231 deleted_count += 1;
232 }
233
234 return deleted_count;
235}
236
237struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer)
238{
239 struct gbproxy_link_info *link_info;
240
241 link_info = talloc_zero(peer, struct gbproxy_link_info);
242 link_info->tlli.ptmsi = GSM_RESERVED_TMSI;
243 link_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI;
244
245 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
246
247 INIT_LLIST_HEAD(&link_info->stored_msgs);
248
249 return link_info;
250}
251
252void gbproxy_detach_link_info(
253 struct gbproxy_peer *peer,
254 struct gbproxy_link_info *link_info)
255{
256 struct gbproxy_patch_state *state = &peer->patch_state;
257
258 llist_del(&link_info->list);
259 OSMO_ASSERT(state->logical_link_count > 0);
260 state->logical_link_count -= 1;
261
262 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
263 state->logical_link_count;
264}
265
266void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
267 const uint8_t *imsi, size_t imsi_len)
268{
269 if (!gprs_is_mi_imsi(imsi, imsi_len))
270 return;
271
272 link_info->imsi_len = imsi_len;
273 link_info->imsi =
274 talloc_realloc_size(link_info, link_info->imsi, imsi_len);
275 OSMO_ASSERT(link_info->imsi != NULL);
276 memcpy(link_info->imsi, imsi, imsi_len);
277}
278
279void gbproxy_reassign_tlli(struct gbproxy_tlli_state *tlli_state,
280 struct gbproxy_peer *peer, uint32_t new_tlli)
281{
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100282 OSMO_ASSERT(peer);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200283 if (new_tlli == tlli_state->current)
284 return;
285
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100286 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200287 "The TLLI has been reassigned from %08x to %08x\n",
288 tlli_state->current, new_tlli);
289
290 /* Remember assigned TLLI */
291 tlli_state->assigned = new_tlli;
292 tlli_state->bss_validated = false;
293 tlli_state->net_validated = false;
294}
295
296uint32_t gbproxy_map_tlli(uint32_t other_tlli,
297 struct gbproxy_link_info *link_info, int to_bss)
298{
299 uint32_t tlli = 0;
300 struct gbproxy_tlli_state *src, *dst;
301 if (to_bss) {
302 src = &link_info->sgsn_tlli;
303 dst = &link_info->tlli;
304 } else {
305 src = &link_info->tlli;
306 dst = &link_info->sgsn_tlli;
307 }
308 if (src->current == other_tlli)
309 tlli = dst->current;
310 else if (src->assigned == other_tlli)
311 tlli = dst->assigned;
312
313 return tlli;
314}
315
316static void gbproxy_validate_tlli(struct gbproxy_tlli_state *tlli_state,
317 uint32_t tlli, int to_bss)
318{
319 LOGP(DGPRS, LOGL_DEBUG,
320 "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n",
321 __func__, tlli_state->current, tlli_state->assigned,
322 tlli_state->net_validated, tlli_state->bss_validated, tlli);
323
324 if (!tlli_state->assigned || tlli_state->assigned != tlli)
325 return;
326
327 /* TODO: Is this ok? Check spec */
328 if (gprs_tlli_type(tlli) != TLLI_LOCAL)
329 return;
330
331 /* See GSM 04.08, 4.7.1.5 */
332 if (to_bss)
333 tlli_state->net_validated = true;
334 else
335 tlli_state->bss_validated = true;
336
337 if (!tlli_state->bss_validated || !tlli_state->net_validated)
338 return;
339
340 LOGP(DGPRS, LOGL_INFO,
341 "The TLLI %08x has been validated (was %08x)\n",
342 tlli_state->assigned, tlli_state->current);
343
344 tlli_state->current = tlli;
345 tlli_state->assigned = 0;
346}
347
348static void gbproxy_touch_link_info(struct gbproxy_peer *peer,
349 struct gbproxy_link_info *link_info,
350 time_t now)
351{
352 gbproxy_detach_link_info(peer, link_info);
353 gbproxy_attach_link_info(peer, now, link_info);
354}
355
356static int gbproxy_unregister_link_info(struct gbproxy_peer *peer,
357 struct gbproxy_link_info *link_info)
358{
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100359 OSMO_ASSERT(peer);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200360 if (!link_info)
361 return 1;
362
363 if (link_info->tlli.ptmsi == GSM_RESERVED_TMSI && !link_info->imsi_len) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100364 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200365 "Removing TLLI %08x from list (P-TMSI or IMSI are not set)\n",
366 link_info->tlli.current);
367 gbproxy_delete_link_info(peer, link_info);
368 return 1;
369 }
370
371 link_info->tlli.current = 0;
372 link_info->tlli.assigned = 0;
373 link_info->sgsn_tlli.current = 0;
374 link_info->sgsn_tlli.assigned = 0;
375
376 link_info->is_deregistered = true;
377
378 gbproxy_reset_link(link_info);
379
380 return 0;
381}
382
383int gbproxy_imsi_matches(struct gbproxy_config *cfg,
384 enum gbproxy_match_id match_id,
385 struct gbproxy_link_info *link_info)
386{
387 struct gbproxy_match *match;
388 OSMO_ASSERT(match_id >= 0 && match_id < ARRAY_SIZE(cfg->matches));
389
390 match = &cfg->matches[match_id];
391 if (!match->enable)
392 return 1;
393
394 return link_info != NULL && link_info->is_matching[match_id];
395}
396
397static void gbproxy_assign_imsi(struct gbproxy_peer *peer,
398 struct gbproxy_link_info *link_info,
399 struct gprs_gb_parse_context *parse_ctx)
400{
401 int imsi_matches;
402 struct gbproxy_link_info *other_link_info;
403 enum gbproxy_match_id match_id;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100404 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100405 OSMO_ASSERT(peer->nse);
406 struct gbproxy_config *cfg = peer->nse->cfg;
407 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200408
409 /* Make sure that there is a second entry with the same IMSI */
410 other_link_info = gbproxy_link_info_by_imsi(
411 peer, parse_ctx->imsi, parse_ctx->imsi_len);
412
413 if (other_link_info && other_link_info != link_info) {
Neels Hofmeyr7facc862020-05-29 16:53:23 +0200414 struct osmo_mobile_identity mi;
415 if (osmo_mobile_identity_decode(&mi, parse_ctx->imsi, parse_ctx->imsi_len, false)
416 || mi.type != GSM_MI_TYPE_IMSI) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100417 LOGPBVC(peer, LOGL_ERROR, "Failed to decode Mobile Identity\n");
Neels Hofmeyr7facc862020-05-29 16:53:23 +0200418 } else {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100419 LOGPBVC(peer, LOGL_INFO,
Neels Hofmeyr7facc862020-05-29 16:53:23 +0200420 "Removing TLLI %08x from list (IMSI %s re-used)\n",
421 other_link_info->tlli.current, mi.imsi);
422 gbproxy_delete_link_info(peer, other_link_info);
423 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200424 }
425
426 /* Update the IMSI field */
427 gbproxy_update_link_info(link_info,
428 parse_ctx->imsi, parse_ctx->imsi_len);
429
430 /* Check, whether the IMSI matches */
431 OSMO_ASSERT(ARRAY_SIZE(link_info->is_matching) ==
Daniel Willmanne50550e2020-11-26 18:19:21 +0100432 ARRAY_SIZE(cfg->matches));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200433 for (match_id = 0; match_id < ARRAY_SIZE(link_info->is_matching);
434 ++match_id) {
435 imsi_matches = gbproxy_check_imsi(
Daniel Willmanne50550e2020-11-26 18:19:21 +0100436 &cfg->matches[match_id],
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200437 parse_ctx->imsi, parse_ctx->imsi_len);
438 if (imsi_matches >= 0)
439 link_info->is_matching[match_id] = imsi_matches ? true : false;
440 }
441}
442
443static int gbproxy_tlli_match(const struct gbproxy_tlli_state *a,
444 const struct gbproxy_tlli_state *b)
445{
446 if (a->current && a->current == b->current)
447 return 1;
448
449 if (a->assigned && a->assigned == b->assigned)
450 return 1;
451
452 if (a->ptmsi != GSM_RESERVED_TMSI && a->ptmsi == b->ptmsi)
453 return 1;
454
455 return 0;
456}
457
458static void gbproxy_remove_matching_link_infos(
459 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info)
460{
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100461 OSMO_ASSERT(peer);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200462 struct gbproxy_link_info *info, *nxt;
463 struct gbproxy_patch_state *state = &peer->patch_state;
464
465 /* Make sure that there is no second entry with the same P-TMSI or TLLI */
466 llist_for_each_entry_safe(info, nxt, &state->logical_links, list) {
467 if (info == link_info)
468 continue;
469
470 if (!gbproxy_tlli_match(&link_info->tlli, &info->tlli) &&
471 (link_info->sgsn_nsei != info->sgsn_nsei ||
472 !gbproxy_tlli_match(&link_info->sgsn_tlli, &info->sgsn_tlli)))
473 continue;
474
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100475 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200476 "Removing TLLI %08x from list (P-TMSI/TLLI re-used)\n",
477 info->tlli.current);
478 gbproxy_delete_link_info(peer, info);
479 }
480}
481
482static struct gbproxy_link_info *gbproxy_get_link_info_ul(
483 struct gbproxy_peer *peer,
484 int *tlli_is_valid,
485 struct gprs_gb_parse_context *parse_ctx)
486{
487 struct gbproxy_link_info *link_info = NULL;
488
489 if (parse_ctx->tlli_enc) {
490 link_info = gbproxy_link_info_by_tlli(peer, parse_ctx->tlli);
491
492 if (link_info) {
493 *tlli_is_valid = 1;
494 return link_info;
495 }
496 }
497
498 *tlli_is_valid = 0;
499
500 if (!link_info && parse_ctx->imsi) {
501 link_info = gbproxy_link_info_by_imsi(
502 peer, parse_ctx->imsi, parse_ctx->imsi_len);
503 }
504
505 if (!link_info && parse_ctx->ptmsi_enc && !parse_ctx->old_raid_is_foreign) {
506 uint32_t bss_ptmsi;
507 gprs_parse_tmsi(parse_ctx->ptmsi_enc, &bss_ptmsi);
508 link_info = gbproxy_link_info_by_ptmsi(peer, bss_ptmsi);
509 }
510
511 if (!link_info)
512 return NULL;
513
514 link_info->is_deregistered = false;
515
516 return link_info;
517}
518
519struct gbproxy_link_info *gbproxy_update_link_state_ul(
520 struct gbproxy_peer *peer,
521 time_t now,
522 struct gprs_gb_parse_context *parse_ctx)
523{
524 struct gbproxy_link_info *link_info;
525 int tlli_is_valid;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100526 OSMO_ASSERT(peer);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200527
528 link_info = gbproxy_get_link_info_ul(peer, &tlli_is_valid, parse_ctx);
529
530 if (parse_ctx->tlli_enc && parse_ctx->llc) {
531 uint32_t sgsn_tlli;
532
533 if (!link_info) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100534 LOGPBVC(peer, LOGL_INFO, "Adding TLLI %08x to list\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200535 parse_ctx->tlli);
536 link_info = gbproxy_link_info_alloc(peer);
537 gbproxy_attach_link_info(peer, now, link_info);
538
539 /* Setup TLLIs */
540 sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
541 parse_ctx->tlli);
542 link_info->sgsn_tlli.current = sgsn_tlli;
543 link_info->tlli.current = parse_ctx->tlli;
544 } else if (!tlli_is_valid) {
545 /* New TLLI (info found by IMSI or P-TMSI) */
546 link_info->tlli.current = parse_ctx->tlli;
547 link_info->tlli.assigned = 0;
548 link_info->sgsn_tlli.current =
549 gbproxy_make_sgsn_tlli(peer, link_info,
550 parse_ctx->tlli);
551 link_info->sgsn_tlli.assigned = 0;
552 gbproxy_touch_link_info(peer, link_info, now);
553 } else {
554 sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, link_info, 0);
555 if (!sgsn_tlli)
556 sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
557 parse_ctx->tlli);
558
559 gbproxy_validate_tlli(&link_info->tlli,
560 parse_ctx->tlli, 0);
561 gbproxy_validate_tlli(&link_info->sgsn_tlli,
562 sgsn_tlli, 0);
563 gbproxy_touch_link_info(peer, link_info, now);
564 }
565 } else if (link_info) {
566 gbproxy_touch_link_info(peer, link_info, now);
567 }
568
569 if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
570 gbproxy_assign_imsi(peer, link_info, parse_ctx);
571
572 return link_info;
573}
574
575static struct gbproxy_link_info *gbproxy_get_link_info_dl(
576 struct gbproxy_peer *peer,
577 struct gprs_gb_parse_context *parse_ctx)
578{
579 struct gbproxy_link_info *link_info = NULL;
580
581 /* Which key to use depends on its availability only, if that fails, do
582 * not retry it with another key (e.g. IMSI). */
583 if (parse_ctx->tlli_enc)
584 link_info = gbproxy_link_info_by_sgsn_tlli(peer, parse_ctx->tlli,
585 parse_ctx->peer_nsei);
586
587 /* TODO: Get link_info by (SGSN) P-TMSI if that is available (see
588 * GSM 08.18, 7.2) instead of using the IMSI as key. */
589 else if (parse_ctx->imsi)
590 link_info = gbproxy_link_info_by_imsi(
591 peer, parse_ctx->imsi, parse_ctx->imsi_len);
592
593 if (link_info)
594 link_info->is_deregistered = false;
595
596 return link_info;
597}
598
599struct gbproxy_link_info *gbproxy_update_link_state_dl(
600 struct gbproxy_peer *peer,
601 time_t now,
602 struct gprs_gb_parse_context *parse_ctx)
603{
604 struct gbproxy_link_info *link_info = NULL;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100605 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100606 OSMO_ASSERT(peer->nse);
607 struct gbproxy_config *cfg = peer->nse->cfg;
608 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200609
610 link_info = gbproxy_get_link_info_dl(peer, parse_ctx);
611
612 if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && link_info) {
613 /* A new P-TMSI has been signalled in the message,
614 * register new TLLI */
615 uint32_t new_sgsn_ptmsi;
616 uint32_t new_bss_ptmsi = GSM_RESERVED_TMSI;
617 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_sgsn_ptmsi);
618
619 if (link_info->sgsn_tlli.ptmsi == new_sgsn_ptmsi)
620 new_bss_ptmsi = link_info->tlli.ptmsi;
621
622 if (new_bss_ptmsi == GSM_RESERVED_TMSI)
623 new_bss_ptmsi = gbproxy_make_bss_ptmsi(peer, new_sgsn_ptmsi);
624
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100625 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200626 "Got new PTMSI %08x from SGSN, using %08x for BSS\n",
627 new_sgsn_ptmsi, new_bss_ptmsi);
628 /* Setup PTMSIs */
629 link_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi;
630 link_info->tlli.ptmsi = new_bss_ptmsi;
631 } else if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && !link_info &&
Daniel Willmanne50550e2020-11-26 18:19:21 +0100632 !cfg->patch_ptmsi) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200633 /* A new P-TMSI has been signalled in the message with an unknown
634 * TLLI, create a new link_info */
635 /* TODO: Add a test case for this branch */
636 uint32_t new_ptmsi;
637 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
638
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100639 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200640 "Adding TLLI %08x to list (SGSN, new P-TMSI is %08x)\n",
641 parse_ctx->tlli, new_ptmsi);
642
643 link_info = gbproxy_link_info_alloc(peer);
644 link_info->sgsn_tlli.current = parse_ctx->tlli;
645 link_info->tlli.current = parse_ctx->tlli;
646 link_info->sgsn_tlli.ptmsi = new_ptmsi;
647 link_info->tlli.ptmsi = new_ptmsi;
648 gbproxy_attach_link_info(peer, now, link_info);
649 } else if (parse_ctx->tlli_enc && parse_ctx->llc && !link_info &&
Daniel Willmanne50550e2020-11-26 18:19:21 +0100650 !cfg->patch_ptmsi) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200651 /* Unknown SGSN TLLI, create a new link_info */
652 uint32_t new_ptmsi;
653 link_info = gbproxy_link_info_alloc(peer);
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100654 LOGPBVC(peer, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200655 parse_ctx->tlli);
656
657 gbproxy_attach_link_info(peer, now, link_info);
658
659 /* Setup TLLIs */
660 link_info->sgsn_tlli.current = parse_ctx->tlli;
661 link_info->tlli.current = parse_ctx->tlli;
662
663 if (!parse_ctx->new_ptmsi_enc)
664 return link_info;
665 /* A new P-TMSI has been signalled in the message */
666
667 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100668 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200669 "Assigning new P-TMSI %08x\n", new_ptmsi);
670 /* Setup P-TMSIs */
671 link_info->sgsn_tlli.ptmsi = new_ptmsi;
672 link_info->tlli.ptmsi = new_ptmsi;
673 } else if (parse_ctx->tlli_enc && parse_ctx->llc && link_info) {
674 uint32_t bss_tlli = gbproxy_map_tlli(parse_ctx->tlli,
675 link_info, 1);
676 gbproxy_validate_tlli(&link_info->sgsn_tlli, parse_ctx->tlli, 1);
677 gbproxy_validate_tlli(&link_info->tlli, bss_tlli, 1);
678 gbproxy_touch_link_info(peer, link_info, now);
679 } else if (link_info) {
680 gbproxy_touch_link_info(peer, link_info, now);
681 }
682
683 if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
684 gbproxy_assign_imsi(peer, link_info, parse_ctx);
685
686 return link_info;
687}
688
689int gbproxy_update_link_state_after(
690 struct gbproxy_peer *peer,
691 struct gbproxy_link_info *link_info,
692 time_t now,
693 struct gprs_gb_parse_context *parse_ctx)
694{
695 int rc = 0;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100696 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100697 OSMO_ASSERT(peer->nse);
698 struct gbproxy_config *cfg = peer->nse->cfg;
699 OSMO_ASSERT(cfg);
700
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200701 if (parse_ctx->invalidate_tlli && link_info) {
702 int keep_info =
Daniel Willmanne50550e2020-11-26 18:19:21 +0100703 cfg->keep_link_infos == GBPROX_KEEP_ALWAYS ||
704 (cfg->keep_link_infos == GBPROX_KEEP_REATTACH &&
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200705 parse_ctx->await_reattach) ||
Daniel Willmanne50550e2020-11-26 18:19:21 +0100706 (cfg->keep_link_infos == GBPROX_KEEP_IDENTIFIED &&
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200707 link_info->imsi_len > 0);
708 if (keep_info) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100709 LOGPBVC(peer, LOGL_INFO, "Unregistering TLLI %08x\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200710 link_info->tlli.current);
711 rc = gbproxy_unregister_link_info(peer, link_info);
712 } else {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100713 LOGPBVC(peer, LOGL_INFO, "Removing TLLI %08x from list\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200714 link_info->tlli.current);
715 gbproxy_delete_link_info(peer, link_info);
716 rc = 1;
717 }
718 } else if (parse_ctx->to_bss && parse_ctx->tlli_enc &&
719 parse_ctx->new_ptmsi_enc && link_info) {
720 /* A new PTMSI has been signaled in the message,
721 * register new TLLI */
722 uint32_t new_sgsn_ptmsi = link_info->sgsn_tlli.ptmsi;
723 uint32_t new_bss_ptmsi = link_info->tlli.ptmsi;
724 uint32_t new_sgsn_tlli;
725 uint32_t new_bss_tlli = 0;
726
727 new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL);
728 if (new_bss_ptmsi != GSM_RESERVED_TMSI)
729 new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL);
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100730 LOGPBVC(peer, LOGL_INFO,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200731 "Assigning new TLLI %08x to SGSN, %08x to BSS\n",
732 new_sgsn_tlli, new_bss_tlli);
733
734 gbproxy_reassign_tlli(&link_info->sgsn_tlli,
735 peer, new_sgsn_tlli);
736 gbproxy_reassign_tlli(&link_info->tlli,
737 peer, new_bss_tlli);
738 gbproxy_remove_matching_link_infos(peer, link_info);
739 }
740
741 gbproxy_remove_stale_link_infos(peer, now);
742
743 return rc;
744}
745
746