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