blob: aa0fc9e76ff528376be9e9f0f3a57bd6f5723665 [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);
31 if (ms->bts)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010032 bts_stat_item_add(ms->bts, STAT_MS_PRESENT, -1);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010033 if (ms_is_idle(ms))
34 talloc_free(ms);
35}
36
37static void ms_storage_ms_active_cb(struct GprsMs *ms)
38{
39 /* Nothing to do */
40}
41
42static struct gpr_ms_callback ms_storage_ms_cb = {
43 .ms_idle = ms_storage_ms_idle_cb,
44 .ms_active = ms_storage_ms_active_cb,
45};
46
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010047GprsMsStorage::GprsMsStorage(struct gprs_rlcmac_bts *bts) :
Jacob Erlbeck17214bb2015-06-02 14:06:12 +020048 m_bts(bts)
Jacob Erlbeck53670862015-05-12 17:54:33 +020049{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010050 INIT_LLIST_HEAD(&m_list);
Jacob Erlbeck53670862015-05-12 17:54:33 +020051}
52
53GprsMsStorage::~GprsMsStorage()
54{
Jacob Erlbeckc362df22016-01-20 22:02:19 +010055 cleanup();
56}
57
58void GprsMsStorage::cleanup()
59{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010060 struct llist_head *pos, *tmp;
Jacob Erlbeck53670862015-05-12 17:54:33 +020061
62 llist_for_each_safe(pos, tmp, &m_list) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010063 struct GprsMs *ms = llist_entry(pos, typeof(*ms), list);
64 ms_set_callback(ms, NULL);
65 ms_storage_ms_idle_cb(ms);
Jacob Erlbeck53670862015-05-12 17:54:33 +020066 }
67}
68
Jacob Erlbeck53670862015-05-12 17:54:33 +020069GprsMs *GprsMsStorage::get_ms(uint32_t tlli, uint32_t old_tlli, const char *imsi) const
70{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010071 struct llist_head *tmp;
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020072 GprsMs *ms;
Jacob Erlbeck53670862015-05-12 17:54:33 +020073
Vadim Yanitskiycb988942020-11-08 13:27:35 +070074 if (tlli != GSM_RESERVED_TMSI || old_tlli != GSM_RESERVED_TMSI) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010075 llist_for_each(tmp, &m_list) {
76 ms = llist_entry(tmp, typeof(*ms), list);
77 if (ms_check_tlli(ms, tlli))
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020078 return ms;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010079 if (ms_check_tlli(ms, old_tlli))
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020080 return ms;
81 }
Jacob Erlbeck53670862015-05-12 17:54:33 +020082 }
83
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020084 /* not found by TLLI */
85
Pau Espin Pedrol5deac142021-11-12 18:01:50 +010086 if (imsi && imsi[0] != '\0') {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010087 llist_for_each(tmp, &m_list) {
88 ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrol5deac142021-11-12 18:01:50 +010089 if (ms_imsi_is_valid(ms) && strcmp(imsi, ms_imsi(ms)) == 0)
Jacob Erlbeck7b9f8252015-05-21 11:07:53 +020090 return ms;
91 }
92 }
93
94 return NULL;
Jacob Erlbeck53670862015-05-12 17:54:33 +020095}
96
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +020097GprsMs *GprsMsStorage::create_ms()
98{
99 GprsMs *ms;
100
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100101 ms = ms_alloc(m_bts, GSM_RESERVED_TMSI);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200102
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100103 ms_set_callback(ms, &ms_storage_ms_cb);
104 llist_add(&ms->list, &m_list);
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100105 if (m_bts)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100106 bts_stat_item_add(m_bts, STAT_MS_PRESENT, 1);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200107
108 return ms;
109}