blob: c9a41c9a2b0bce3073b45317adbeed28341e230a [file] [log] [blame]
Jacob Erlbeck53670862015-05-12 17:54:33 +02001/* gprs_ms_storage.cpp
2 *
3 * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH
4 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (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 General Public License for more details.
Jacob Erlbeck53670862015-05-12 17:54:33 +020015 */
16
17
18#include "gprs_ms_storage.h"
19
20#include "tbf.h"
Jacob Erlbeckf5898a02015-11-27 19:05:13 +010021#include "bts.h"
Max1187a772018-01-26 13:31:42 +010022
23extern "C" {
24 #include <osmocom/core/linuxlist.h>
Vadim Yanitskiycb988942020-11-08 13:27:35 +070025 #include <osmocom/gsm/gsm48.h>
Max1187a772018-01-26 13:31:42 +010026}
Jacob Erlbeck53670862015-05-12 17:54:33 +020027
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010028static void ms_storage_ms_idle_cb(struct GprsMs *ms)
29{
30 llist_del(&ms->list);
Pau Espin Pedrolf3561b52022-05-09 16:25:55 +020031 bts_stat_item_dec(ms->bts, STAT_MS_PRESENT);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010032 if (ms_is_idle(ms))
33 talloc_free(ms);
34}
35
36static void ms_storage_ms_active_cb(struct GprsMs *ms)
37{
38 /* Nothing to do */
39}
40
41static struct gpr_ms_callback ms_storage_ms_cb = {
42 .ms_idle = ms_storage_ms_idle_cb,
43 .ms_active = ms_storage_ms_active_cb,
44};
45
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010046GprsMsStorage::GprsMsStorage(struct gprs_rlcmac_bts *bts) :
Jacob Erlbeck17214bb2015-06-02 14:06:12 +020047 m_bts(bts)
Jacob Erlbeck53670862015-05-12 17:54:33 +020048{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010049 INIT_LLIST_HEAD(&m_list);
Jacob Erlbeck53670862015-05-12 17:54:33 +020050}
51
52GprsMsStorage::~GprsMsStorage()
53{
Jacob Erlbeckc362df22016-01-20 22:02:19 +010054 cleanup();
55}
56
57void GprsMsStorage::cleanup()
58{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010059 struct llist_head *pos, *tmp;
Jacob Erlbeck53670862015-05-12 17:54:33 +020060
61 llist_for_each_safe(pos, tmp, &m_list) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010062 struct GprsMs *ms = llist_entry(pos, typeof(*ms), list);
63 ms_set_callback(ms, NULL);
Pau Espin Pedrolcd18a502022-05-09 16:31:04 +020064 ms_set_timeout(ms, 0);
65 llist_del(&ms->list);
66 bts_stat_item_dec(ms->bts, STAT_MS_PRESENT);
67 talloc_free(ms);
Jacob Erlbeck53670862015-05-12 17:54:33 +020068 }
69}
70
Jacob Erlbeck53670862015-05-12 17:54:33 +020071GprsMs *GprsMsStorage::get_ms(uint32_t tlli, uint32_t old_tlli, const char *imsi) const
72{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010073 struct llist_head *tmp;
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020074 GprsMs *ms;
Jacob Erlbeck53670862015-05-12 17:54:33 +020075
Vadim Yanitskiycb988942020-11-08 13:27:35 +070076 if (tlli != GSM_RESERVED_TMSI || old_tlli != GSM_RESERVED_TMSI) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010077 llist_for_each(tmp, &m_list) {
78 ms = llist_entry(tmp, typeof(*ms), list);
79 if (ms_check_tlli(ms, tlli))
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020080 return ms;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010081 if (ms_check_tlli(ms, old_tlli))
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020082 return ms;
83 }
Jacob Erlbeck53670862015-05-12 17:54:33 +020084 }
85
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020086 /* not found by TLLI */
87
Pau Espin Pedrol5deac142021-11-12 18:01:50 +010088 if (imsi && imsi[0] != '\0') {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010089 llist_for_each(tmp, &m_list) {
90 ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrol5deac142021-11-12 18:01:50 +010091 if (ms_imsi_is_valid(ms) && strcmp(imsi, ms_imsi(ms)) == 0)
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020092 return ms;
93 }
94 }
95
96 return NULL;
Jacob Erlbeck53670862015-05-12 17:54:33 +020097}
98
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +020099GprsMs *GprsMsStorage::create_ms()
100{
101 GprsMs *ms;
102
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100103 ms = ms_alloc(m_bts, GSM_RESERVED_TMSI);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200104
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100105 ms_set_callback(ms, &ms_storage_ms_cb);
106 llist_add(&ms->list, &m_list);
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100107 if (m_bts)
Pau Espin Pedrolf3561b52022-05-09 16:25:55 +0200108 bts_stat_item_inc(m_bts, STAT_MS_PRESENT);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200109
110 return ms;
111}