blob: 6245ed9fe8d677f38a1afeae0ddf698dccf462f2 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21
22#include "gprs_ms_storage.h"
23
24#include "tbf.h"
Jacob Erlbeckf5898a02015-11-27 19:05:13 +010025#include "bts.h"
Max1187a772018-01-26 13:31:42 +010026
27extern "C" {
28 #include <osmocom/core/linuxlist.h>
Vadim Yanitskiycb988942020-11-08 13:27:35 +070029 #include <osmocom/gsm/gsm48.h>
Max1187a772018-01-26 13:31:42 +010030}
Jacob Erlbeck53670862015-05-12 17:54:33 +020031
Jacob Erlbeckfea17f82015-08-16 21:36:32 +020032#define GPRS_UNDEFINED_IMSI "000"
33
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010034static void ms_storage_ms_idle_cb(struct GprsMs *ms)
35{
36 llist_del(&ms->list);
37 if (ms->bts)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010038 bts_stat_item_add(ms->bts, STAT_MS_PRESENT, -1);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010039 if (ms_is_idle(ms))
40 talloc_free(ms);
41}
42
43static void ms_storage_ms_active_cb(struct GprsMs *ms)
44{
45 /* Nothing to do */
46}
47
48static struct gpr_ms_callback ms_storage_ms_cb = {
49 .ms_idle = ms_storage_ms_idle_cb,
50 .ms_active = ms_storage_ms_active_cb,
51};
52
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010053GprsMsStorage::GprsMsStorage(struct gprs_rlcmac_bts *bts) :
Jacob Erlbeck17214bb2015-06-02 14:06:12 +020054 m_bts(bts)
Jacob Erlbeck53670862015-05-12 17:54:33 +020055{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010056 INIT_LLIST_HEAD(&m_list);
Jacob Erlbeck53670862015-05-12 17:54:33 +020057}
58
59GprsMsStorage::~GprsMsStorage()
60{
Jacob Erlbeckc362df22016-01-20 22:02:19 +010061 cleanup();
62}
63
64void GprsMsStorage::cleanup()
65{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010066 struct llist_head *pos, *tmp;
Jacob Erlbeck53670862015-05-12 17:54:33 +020067
68 llist_for_each_safe(pos, tmp, &m_list) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010069 struct GprsMs *ms = llist_entry(pos, typeof(*ms), list);
70 ms_set_callback(ms, NULL);
71 ms_storage_ms_idle_cb(ms);
Jacob Erlbeck53670862015-05-12 17:54:33 +020072 }
73}
74
Jacob Erlbeck53670862015-05-12 17:54:33 +020075GprsMs *GprsMsStorage::get_ms(uint32_t tlli, uint32_t old_tlli, const char *imsi) const
76{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010077 struct llist_head *tmp;
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020078 GprsMs *ms;
Jacob Erlbeck53670862015-05-12 17:54:33 +020079
Vadim Yanitskiycb988942020-11-08 13:27:35 +070080 if (tlli != GSM_RESERVED_TMSI || old_tlli != GSM_RESERVED_TMSI) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010081 llist_for_each(tmp, &m_list) {
82 ms = llist_entry(tmp, typeof(*ms), list);
83 if (ms_check_tlli(ms, tlli))
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020084 return ms;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010085 if (ms_check_tlli(ms, old_tlli))
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020086 return ms;
87 }
Jacob Erlbeck53670862015-05-12 17:54:33 +020088 }
89
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020090 /* not found by TLLI */
91
Jacob Erlbeckfea17f82015-08-16 21:36:32 +020092 if (imsi && imsi[0] && strcmp(imsi, GPRS_UNDEFINED_IMSI) != 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010093 llist_for_each(tmp, &m_list) {
94 ms = llist_entry(tmp, typeof(*ms), list);
95 if (strcmp(imsi, ms_imsi(ms)) == 0)
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020096 return ms;
97 }
98 }
99
100 return NULL;
Jacob Erlbeck53670862015-05-12 17:54:33 +0200101}
102
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200103GprsMs *GprsMsStorage::create_ms()
104{
105 GprsMs *ms;
106
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100107 ms = ms_alloc(m_bts, GSM_RESERVED_TMSI);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200108
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100109 ms_set_callback(ms, &ms_storage_ms_cb);
110 llist_add(&ms->list, &m_list);
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100111 if (m_bts)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100112 bts_stat_item_add(m_bts, STAT_MS_PRESENT, 1);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200113
114 return ms;
115}