Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 1 | /* 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 Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "gprs_ms_storage.h" |
| 19 | |
| 20 | #include "tbf.h" |
Jacob Erlbeck | f5898a0 | 2015-11-27 19:05:13 +0100 | [diff] [blame] | 21 | #include "bts.h" |
Max | 1187a77 | 2018-01-26 13:31:42 +0100 | [diff] [blame] | 22 | |
| 23 | extern "C" { |
| 24 | #include <osmocom/core/linuxlist.h> |
Vadim Yanitskiy | cb98894 | 2020-11-08 13:27:35 +0700 | [diff] [blame] | 25 | #include <osmocom/gsm/gsm48.h> |
Max | 1187a77 | 2018-01-26 13:31:42 +0100 | [diff] [blame] | 26 | } |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 27 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 28 | static void ms_storage_ms_idle_cb(struct GprsMs *ms) |
| 29 | { |
| 30 | llist_del(&ms->list); |
Pau Espin Pedrol | f3561b5 | 2022-05-09 16:25:55 +0200 | [diff] [blame] | 31 | bts_stat_item_dec(ms->bts, STAT_MS_PRESENT); |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 32 | if (ms_is_idle(ms)) |
| 33 | talloc_free(ms); |
| 34 | } |
| 35 | |
| 36 | static void ms_storage_ms_active_cb(struct GprsMs *ms) |
| 37 | { |
| 38 | /* Nothing to do */ |
| 39 | } |
| 40 | |
| 41 | static 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 Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 46 | GprsMsStorage::GprsMsStorage(struct gprs_rlcmac_bts *bts) : |
Jacob Erlbeck | 17214bb | 2015-06-02 14:06:12 +0200 | [diff] [blame] | 47 | m_bts(bts) |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 48 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 49 | INIT_LLIST_HEAD(&m_list); |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | GprsMsStorage::~GprsMsStorage() |
| 53 | { |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 54 | cleanup(); |
| 55 | } |
| 56 | |
| 57 | void GprsMsStorage::cleanup() |
| 58 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 59 | struct llist_head *pos, *tmp; |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 60 | |
| 61 | llist_for_each_safe(pos, tmp, &m_list) { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 62 | struct GprsMs *ms = llist_entry(pos, typeof(*ms), list); |
| 63 | ms_set_callback(ms, NULL); |
Pau Espin Pedrol | cd18a50 | 2022-05-09 16:31:04 +0200 | [diff] [blame] | 64 | 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 Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 71 | GprsMs *GprsMsStorage::get_ms(uint32_t tlli, uint32_t old_tlli, const char *imsi) const |
| 72 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 73 | struct llist_head *tmp; |
Jacob Erlbeck | 7b9f825 | 2015-05-21 11:07:53 +0200 | [diff] [blame] | 74 | GprsMs *ms; |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 75 | |
Vadim Yanitskiy | cb98894 | 2020-11-08 13:27:35 +0700 | [diff] [blame] | 76 | if (tlli != GSM_RESERVED_TMSI || old_tlli != GSM_RESERVED_TMSI) { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 77 | llist_for_each(tmp, &m_list) { |
| 78 | ms = llist_entry(tmp, typeof(*ms), list); |
| 79 | if (ms_check_tlli(ms, tlli)) |
Jacob Erlbeck | 7b9f825 | 2015-05-21 11:07:53 +0200 | [diff] [blame] | 80 | return ms; |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 81 | if (ms_check_tlli(ms, old_tlli)) |
Jacob Erlbeck | 7b9f825 | 2015-05-21 11:07:53 +0200 | [diff] [blame] | 82 | return ms; |
| 83 | } |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Jacob Erlbeck | 7b9f825 | 2015-05-21 11:07:53 +0200 | [diff] [blame] | 86 | /* not found by TLLI */ |
| 87 | |
Pau Espin Pedrol | 5deac14 | 2021-11-12 18:01:50 +0100 | [diff] [blame] | 88 | if (imsi && imsi[0] != '\0') { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 89 | llist_for_each(tmp, &m_list) { |
| 90 | ms = llist_entry(tmp, typeof(*ms), list); |
Pau Espin Pedrol | 5deac14 | 2021-11-12 18:01:50 +0100 | [diff] [blame] | 91 | if (ms_imsi_is_valid(ms) && strcmp(imsi, ms_imsi(ms)) == 0) |
Jacob Erlbeck | 7b9f825 | 2015-05-21 11:07:53 +0200 | [diff] [blame] | 92 | return ms; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return NULL; |
Jacob Erlbeck | 5367086 | 2015-05-12 17:54:33 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Jacob Erlbeck | cb1b494 | 2015-06-19 10:59:58 +0200 | [diff] [blame] | 99 | GprsMs *GprsMsStorage::create_ms() |
| 100 | { |
| 101 | GprsMs *ms; |
| 102 | |
Pau Espin Pedrol | f80cc5b | 2023-04-17 14:25:51 +0200 | [diff] [blame] | 103 | ms = ms_alloc(m_bts); |
Jacob Erlbeck | cb1b494 | 2015-06-19 10:59:58 +0200 | [diff] [blame] | 104 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 105 | ms_set_callback(ms, &ms_storage_ms_cb); |
| 106 | llist_add(&ms->list, &m_list); |
Jacob Erlbeck | f5898a0 | 2015-11-27 19:05:13 +0100 | [diff] [blame] | 107 | if (m_bts) |
Pau Espin Pedrol | f3561b5 | 2022-05-09 16:25:55 +0200 | [diff] [blame] | 108 | bts_stat_item_inc(m_bts, STAT_MS_PRESENT); |
Jacob Erlbeck | cb1b494 | 2015-06-19 10:59:58 +0200 | [diff] [blame] | 109 | |
| 110 | return ms; |
| 111 | } |
Pau Espin Pedrol | 8abe13c | 2022-10-21 18:49:48 +0200 | [diff] [blame] | 112 | |
| 113 | struct GprsMs *ms_store_get_ms(struct GprsMsStorage *ms_store, uint32_t tlli, uint32_t old_tlli, const char *imsi) |
| 114 | { |
| 115 | return ms_store->get_ms(tlli, old_tlli, imsi); |
| 116 | } |