Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1 | /* Gb-proxy TLLI state handling */ |
| 2 | |
| 3 | /* (C) 2014 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (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 Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/gsm/gsm48.h> |
| 22 | |
| 23 | #include <osmocom/sgsn/gb_proxy.h> |
| 24 | |
| 25 | #include <osmocom/sgsn/gprs_utils.h> |
| 26 | #include <osmocom/sgsn/gprs_gb_parse.h> |
| 27 | |
| 28 | #include <osmocom/sgsn/debug.h> |
| 29 | |
| 30 | #include <osmocom/gsm/gsm_utils.h> |
| 31 | |
| 32 | #include <osmocom/core/rate_ctr.h> |
| 33 | #include <osmocom/core/talloc.h> |
| 34 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 35 | struct gbproxy_link_info *gbproxy_link_info_by_tlli(struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 36 | uint32_t tlli) |
| 37 | { |
| 38 | struct gbproxy_link_info *link_info; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 39 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 40 | |
| 41 | if (!tlli) |
| 42 | return NULL; |
| 43 | |
| 44 | llist_for_each_entry(link_info, &state->logical_links, list) |
| 45 | if (link_info->tlli.current == tlli || |
| 46 | link_info->tlli.assigned == tlli) |
| 47 | return link_info; |
| 48 | |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | struct gbproxy_link_info *gbproxy_link_info_by_ptmsi( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 53 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 54 | uint32_t ptmsi) |
| 55 | { |
| 56 | struct gbproxy_link_info *link_info; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 57 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 58 | |
| 59 | if (ptmsi == GSM_RESERVED_TMSI) |
| 60 | return NULL; |
| 61 | |
| 62 | llist_for_each_entry(link_info, &state->logical_links, list) |
| 63 | if (link_info->tlli.ptmsi == ptmsi) |
| 64 | return link_info; |
| 65 | |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 70 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 71 | uint32_t tlli) |
| 72 | { |
| 73 | struct gbproxy_link_info *link_info; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 74 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 75 | |
| 76 | if (!tlli) |
| 77 | return NULL; |
| 78 | |
| 79 | /* Don't care about the NSEI */ |
| 80 | llist_for_each_entry(link_info, &state->logical_links, list) |
| 81 | if (link_info->sgsn_tlli.current == tlli || |
| 82 | link_info->sgsn_tlli.assigned == tlli) |
| 83 | return link_info; |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 89 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 90 | uint32_t tlli, uint32_t sgsn_nsei) |
| 91 | { |
| 92 | struct gbproxy_link_info *link_info; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 93 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 94 | |
| 95 | if (!tlli) |
| 96 | return NULL; |
| 97 | |
| 98 | llist_for_each_entry(link_info, &state->logical_links, list) |
| 99 | if ((link_info->sgsn_tlli.current == tlli || |
| 100 | link_info->sgsn_tlli.assigned == tlli) && |
| 101 | link_info->sgsn_nsei == sgsn_nsei) |
| 102 | return link_info; |
| 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | struct gbproxy_link_info *gbproxy_link_info_by_imsi( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 108 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 109 | const uint8_t *imsi, |
| 110 | size_t imsi_len) |
| 111 | { |
| 112 | struct gbproxy_link_info *link_info; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 113 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 114 | |
| 115 | if (!gprs_is_mi_imsi(imsi, imsi_len)) |
| 116 | return NULL; |
| 117 | |
| 118 | llist_for_each_entry(link_info, &state->logical_links, list) { |
| 119 | if (link_info->imsi_len != imsi_len) |
| 120 | continue; |
| 121 | if (memcmp(link_info->imsi, imsi, imsi_len) != 0) |
| 122 | continue; |
| 123 | |
| 124 | return link_info; |
| 125 | } |
| 126 | |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info) |
| 131 | { |
| 132 | struct msgb *msg, *nxt; |
| 133 | |
| 134 | llist_for_each_entry_safe(msg, nxt, &link_info->stored_msgs, list) { |
| 135 | llist_del(&msg->list); |
| 136 | msgb_free(msg); |
| 137 | } |
| 138 | } |
| 139 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 140 | void gbproxy_delete_link_info(struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 141 | struct gbproxy_link_info *link_info) |
| 142 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 143 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 144 | |
| 145 | gbproxy_link_info_discard_messages(link_info); |
| 146 | |
| 147 | llist_del(&link_info->list); |
| 148 | talloc_free(link_info); |
| 149 | state->logical_link_count -= 1; |
| 150 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 151 | bvc->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 152 | state->logical_link_count; |
| 153 | } |
| 154 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 155 | void gbproxy_delete_link_infos(struct gbproxy_bvc *bvc) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 156 | { |
| 157 | struct gbproxy_link_info *link_info, *nxt; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 158 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 159 | |
| 160 | llist_for_each_entry_safe(link_info, nxt, &state->logical_links, list) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 161 | gbproxy_delete_link_info(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 162 | |
| 163 | OSMO_ASSERT(state->logical_link_count == 0); |
| 164 | OSMO_ASSERT(llist_empty(&state->logical_links)); |
| 165 | } |
| 166 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 167 | void gbproxy_attach_link_info(struct gbproxy_bvc *bvc, time_t now, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 168 | struct gbproxy_link_info *link_info) |
| 169 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 170 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 171 | |
| 172 | link_info->timestamp = now; |
| 173 | llist_add(&link_info->list, &state->logical_links); |
| 174 | state->logical_link_count += 1; |
| 175 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 176 | bvc->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 177 | state->logical_link_count; |
| 178 | } |
| 179 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 180 | int gbproxy_remove_stale_link_infos(struct gbproxy_bvc *bvc, time_t now) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 181 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 182 | OSMO_ASSERT(bvc); |
| 183 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 184 | int exceeded_max_len = 0; |
| 185 | int deleted_count = 0; |
| 186 | int check_for_age; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 187 | OSMO_ASSERT(bvc->nse); |
| 188 | struct gbproxy_config *cfg = bvc->nse->cfg; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 189 | OSMO_ASSERT(cfg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 190 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 191 | if (cfg->tlli_max_len > 0) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 192 | exceeded_max_len = |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 193 | state->logical_link_count - cfg->tlli_max_len; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 194 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 195 | check_for_age = cfg->tlli_max_age > 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 196 | |
| 197 | for (; exceeded_max_len > 0; exceeded_max_len--) { |
| 198 | struct gbproxy_link_info *link_info; |
| 199 | OSMO_ASSERT(!llist_empty(&state->logical_links)); |
| 200 | link_info = llist_entry(state->logical_links.prev, |
| 201 | struct gbproxy_link_info, |
| 202 | list); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 203 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 204 | "Removing TLLI %08x from list " |
| 205 | "(stale, length %d, max_len exceeded)\n", |
| 206 | link_info->tlli.current, state->logical_link_count); |
| 207 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 208 | gbproxy_delete_link_info(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 209 | deleted_count += 1; |
| 210 | } |
| 211 | |
| 212 | while (check_for_age && !llist_empty(&state->logical_links)) { |
| 213 | time_t age; |
| 214 | struct gbproxy_link_info *link_info; |
| 215 | link_info = llist_entry(state->logical_links.prev, |
| 216 | struct gbproxy_link_info, |
| 217 | list); |
| 218 | age = now - link_info->timestamp; |
| 219 | /* age < 0 only happens after system time jumps, discard entry */ |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 220 | if (age <= cfg->tlli_max_age && age >= 0) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 221 | check_for_age = 0; |
| 222 | continue; |
| 223 | } |
| 224 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 225 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 226 | "Removing TLLI %08x from list " |
| 227 | "(stale, age %d, max_age exceeded)\n", |
| 228 | link_info->tlli.current, (int)age); |
| 229 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 230 | gbproxy_delete_link_info(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 231 | deleted_count += 1; |
| 232 | } |
| 233 | |
| 234 | return deleted_count; |
| 235 | } |
| 236 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 237 | struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_bvc *bvc) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 238 | { |
| 239 | struct gbproxy_link_info *link_info; |
| 240 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 241 | link_info = talloc_zero(bvc, struct gbproxy_link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 242 | link_info->tlli.ptmsi = GSM_RESERVED_TMSI; |
| 243 | link_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI; |
| 244 | |
| 245 | link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX; |
| 246 | |
| 247 | INIT_LLIST_HEAD(&link_info->stored_msgs); |
| 248 | |
| 249 | return link_info; |
| 250 | } |
| 251 | |
| 252 | void gbproxy_detach_link_info( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 253 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 254 | struct gbproxy_link_info *link_info) |
| 255 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 256 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 257 | |
| 258 | llist_del(&link_info->list); |
| 259 | OSMO_ASSERT(state->logical_link_count > 0); |
| 260 | state->logical_link_count -= 1; |
| 261 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 262 | bvc->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 263 | state->logical_link_count; |
| 264 | } |
| 265 | |
| 266 | void gbproxy_update_link_info(struct gbproxy_link_info *link_info, |
| 267 | const uint8_t *imsi, size_t imsi_len) |
| 268 | { |
| 269 | if (!gprs_is_mi_imsi(imsi, imsi_len)) |
| 270 | return; |
| 271 | |
| 272 | link_info->imsi_len = imsi_len; |
| 273 | link_info->imsi = |
| 274 | talloc_realloc_size(link_info, link_info->imsi, imsi_len); |
| 275 | OSMO_ASSERT(link_info->imsi != NULL); |
| 276 | memcpy(link_info->imsi, imsi, imsi_len); |
| 277 | } |
| 278 | |
| 279 | void gbproxy_reassign_tlli(struct gbproxy_tlli_state *tlli_state, |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 280 | struct gbproxy_bvc *bvc, uint32_t new_tlli) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 281 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 282 | OSMO_ASSERT(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 283 | if (new_tlli == tlli_state->current) |
| 284 | return; |
| 285 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 286 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 287 | "The TLLI has been reassigned from %08x to %08x\n", |
| 288 | tlli_state->current, new_tlli); |
| 289 | |
| 290 | /* Remember assigned TLLI */ |
| 291 | tlli_state->assigned = new_tlli; |
| 292 | tlli_state->bss_validated = false; |
| 293 | tlli_state->net_validated = false; |
| 294 | } |
| 295 | |
| 296 | uint32_t gbproxy_map_tlli(uint32_t other_tlli, |
| 297 | struct gbproxy_link_info *link_info, int to_bss) |
| 298 | { |
| 299 | uint32_t tlli = 0; |
| 300 | struct gbproxy_tlli_state *src, *dst; |
| 301 | if (to_bss) { |
| 302 | src = &link_info->sgsn_tlli; |
| 303 | dst = &link_info->tlli; |
| 304 | } else { |
| 305 | src = &link_info->tlli; |
| 306 | dst = &link_info->sgsn_tlli; |
| 307 | } |
| 308 | if (src->current == other_tlli) |
| 309 | tlli = dst->current; |
| 310 | else if (src->assigned == other_tlli) |
| 311 | tlli = dst->assigned; |
| 312 | |
| 313 | return tlli; |
| 314 | } |
| 315 | |
| 316 | static void gbproxy_validate_tlli(struct gbproxy_tlli_state *tlli_state, |
| 317 | uint32_t tlli, int to_bss) |
| 318 | { |
| 319 | LOGP(DGPRS, LOGL_DEBUG, |
| 320 | "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n", |
| 321 | __func__, tlli_state->current, tlli_state->assigned, |
| 322 | tlli_state->net_validated, tlli_state->bss_validated, tlli); |
| 323 | |
| 324 | if (!tlli_state->assigned || tlli_state->assigned != tlli) |
| 325 | return; |
| 326 | |
| 327 | /* TODO: Is this ok? Check spec */ |
| 328 | if (gprs_tlli_type(tlli) != TLLI_LOCAL) |
| 329 | return; |
| 330 | |
| 331 | /* See GSM 04.08, 4.7.1.5 */ |
| 332 | if (to_bss) |
| 333 | tlli_state->net_validated = true; |
| 334 | else |
| 335 | tlli_state->bss_validated = true; |
| 336 | |
| 337 | if (!tlli_state->bss_validated || !tlli_state->net_validated) |
| 338 | return; |
| 339 | |
| 340 | LOGP(DGPRS, LOGL_INFO, |
| 341 | "The TLLI %08x has been validated (was %08x)\n", |
| 342 | tlli_state->assigned, tlli_state->current); |
| 343 | |
| 344 | tlli_state->current = tlli; |
| 345 | tlli_state->assigned = 0; |
| 346 | } |
| 347 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 348 | static void gbproxy_touch_link_info(struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 349 | struct gbproxy_link_info *link_info, |
| 350 | time_t now) |
| 351 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 352 | gbproxy_detach_link_info(bvc, link_info); |
| 353 | gbproxy_attach_link_info(bvc, now, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 356 | static int gbproxy_unregister_link_info(struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 357 | struct gbproxy_link_info *link_info) |
| 358 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 359 | OSMO_ASSERT(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 360 | if (!link_info) |
| 361 | return 1; |
| 362 | |
| 363 | if (link_info->tlli.ptmsi == GSM_RESERVED_TMSI && !link_info->imsi_len) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 364 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 365 | "Removing TLLI %08x from list (P-TMSI or IMSI are not set)\n", |
| 366 | link_info->tlli.current); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 367 | gbproxy_delete_link_info(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | link_info->tlli.current = 0; |
| 372 | link_info->tlli.assigned = 0; |
| 373 | link_info->sgsn_tlli.current = 0; |
| 374 | link_info->sgsn_tlli.assigned = 0; |
| 375 | |
| 376 | link_info->is_deregistered = true; |
| 377 | |
| 378 | gbproxy_reset_link(link_info); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | int gbproxy_imsi_matches(struct gbproxy_config *cfg, |
| 384 | enum gbproxy_match_id match_id, |
| 385 | struct gbproxy_link_info *link_info) |
| 386 | { |
| 387 | struct gbproxy_match *match; |
| 388 | OSMO_ASSERT(match_id >= 0 && match_id < ARRAY_SIZE(cfg->matches)); |
| 389 | |
| 390 | match = &cfg->matches[match_id]; |
| 391 | if (!match->enable) |
| 392 | return 1; |
| 393 | |
| 394 | return link_info != NULL && link_info->is_matching[match_id]; |
| 395 | } |
| 396 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 397 | static void gbproxy_assign_imsi(struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 398 | struct gbproxy_link_info *link_info, |
| 399 | struct gprs_gb_parse_context *parse_ctx) |
| 400 | { |
| 401 | int imsi_matches; |
| 402 | struct gbproxy_link_info *other_link_info; |
| 403 | enum gbproxy_match_id match_id; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 404 | OSMO_ASSERT(bvc); |
| 405 | OSMO_ASSERT(bvc->nse); |
| 406 | struct gbproxy_config *cfg = bvc->nse->cfg; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 407 | OSMO_ASSERT(cfg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 408 | |
| 409 | /* Make sure that there is a second entry with the same IMSI */ |
| 410 | other_link_info = gbproxy_link_info_by_imsi( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 411 | bvc, parse_ctx->imsi, parse_ctx->imsi_len); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 412 | |
| 413 | if (other_link_info && other_link_info != link_info) { |
Neels Hofmeyr | 7facc86 | 2020-05-29 16:53:23 +0200 | [diff] [blame] | 414 | struct osmo_mobile_identity mi; |
| 415 | if (osmo_mobile_identity_decode(&mi, parse_ctx->imsi, parse_ctx->imsi_len, false) |
| 416 | || mi.type != GSM_MI_TYPE_IMSI) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 417 | LOGPBVC(bvc, LOGL_ERROR, "Failed to decode Mobile Identity\n"); |
Neels Hofmeyr | 7facc86 | 2020-05-29 16:53:23 +0200 | [diff] [blame] | 418 | } else { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 419 | LOGPBVC(bvc, LOGL_INFO, |
Neels Hofmeyr | 7facc86 | 2020-05-29 16:53:23 +0200 | [diff] [blame] | 420 | "Removing TLLI %08x from list (IMSI %s re-used)\n", |
| 421 | other_link_info->tlli.current, mi.imsi); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 422 | gbproxy_delete_link_info(bvc, other_link_info); |
Neels Hofmeyr | 7facc86 | 2020-05-29 16:53:23 +0200 | [diff] [blame] | 423 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Update the IMSI field */ |
| 427 | gbproxy_update_link_info(link_info, |
| 428 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 429 | |
| 430 | /* Check, whether the IMSI matches */ |
| 431 | OSMO_ASSERT(ARRAY_SIZE(link_info->is_matching) == |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 432 | ARRAY_SIZE(cfg->matches)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 433 | for (match_id = 0; match_id < ARRAY_SIZE(link_info->is_matching); |
| 434 | ++match_id) { |
| 435 | imsi_matches = gbproxy_check_imsi( |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 436 | &cfg->matches[match_id], |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 437 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 438 | if (imsi_matches >= 0) |
| 439 | link_info->is_matching[match_id] = imsi_matches ? true : false; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static int gbproxy_tlli_match(const struct gbproxy_tlli_state *a, |
| 444 | const struct gbproxy_tlli_state *b) |
| 445 | { |
| 446 | if (a->current && a->current == b->current) |
| 447 | return 1; |
| 448 | |
| 449 | if (a->assigned && a->assigned == b->assigned) |
| 450 | return 1; |
| 451 | |
| 452 | if (a->ptmsi != GSM_RESERVED_TMSI && a->ptmsi == b->ptmsi) |
| 453 | return 1; |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static void gbproxy_remove_matching_link_infos( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 459 | struct gbproxy_bvc *bvc, struct gbproxy_link_info *link_info) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 460 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 461 | OSMO_ASSERT(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 462 | struct gbproxy_link_info *info, *nxt; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 463 | struct gbproxy_patch_state *state = &bvc->patch_state; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 464 | |
| 465 | /* Make sure that there is no second entry with the same P-TMSI or TLLI */ |
| 466 | llist_for_each_entry_safe(info, nxt, &state->logical_links, list) { |
| 467 | if (info == link_info) |
| 468 | continue; |
| 469 | |
| 470 | if (!gbproxy_tlli_match(&link_info->tlli, &info->tlli) && |
| 471 | (link_info->sgsn_nsei != info->sgsn_nsei || |
| 472 | !gbproxy_tlli_match(&link_info->sgsn_tlli, &info->sgsn_tlli))) |
| 473 | continue; |
| 474 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 475 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 476 | "Removing TLLI %08x from list (P-TMSI/TLLI re-used)\n", |
| 477 | info->tlli.current); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 478 | gbproxy_delete_link_info(bvc, info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | static struct gbproxy_link_info *gbproxy_get_link_info_ul( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 483 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 484 | int *tlli_is_valid, |
| 485 | struct gprs_gb_parse_context *parse_ctx) |
| 486 | { |
| 487 | struct gbproxy_link_info *link_info = NULL; |
| 488 | |
| 489 | if (parse_ctx->tlli_enc) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 490 | link_info = gbproxy_link_info_by_tlli(bvc, parse_ctx->tlli); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 491 | |
| 492 | if (link_info) { |
| 493 | *tlli_is_valid = 1; |
| 494 | return link_info; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | *tlli_is_valid = 0; |
| 499 | |
| 500 | if (!link_info && parse_ctx->imsi) { |
| 501 | link_info = gbproxy_link_info_by_imsi( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 502 | bvc, parse_ctx->imsi, parse_ctx->imsi_len); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (!link_info && parse_ctx->ptmsi_enc && !parse_ctx->old_raid_is_foreign) { |
| 506 | uint32_t bss_ptmsi; |
| 507 | gprs_parse_tmsi(parse_ctx->ptmsi_enc, &bss_ptmsi); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 508 | link_info = gbproxy_link_info_by_ptmsi(bvc, bss_ptmsi); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | if (!link_info) |
| 512 | return NULL; |
| 513 | |
| 514 | link_info->is_deregistered = false; |
| 515 | |
| 516 | return link_info; |
| 517 | } |
| 518 | |
| 519 | struct gbproxy_link_info *gbproxy_update_link_state_ul( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 520 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 521 | time_t now, |
| 522 | struct gprs_gb_parse_context *parse_ctx) |
| 523 | { |
| 524 | struct gbproxy_link_info *link_info; |
| 525 | int tlli_is_valid; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 526 | OSMO_ASSERT(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 527 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 528 | link_info = gbproxy_get_link_info_ul(bvc, &tlli_is_valid, parse_ctx); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 529 | |
| 530 | if (parse_ctx->tlli_enc && parse_ctx->llc) { |
| 531 | uint32_t sgsn_tlli; |
| 532 | |
| 533 | if (!link_info) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 534 | LOGPBVC(bvc, LOGL_INFO, "Adding TLLI %08x to list\n", |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 535 | parse_ctx->tlli); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 536 | link_info = gbproxy_link_info_alloc(bvc); |
| 537 | gbproxy_attach_link_info(bvc, now, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 538 | |
| 539 | /* Setup TLLIs */ |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 540 | sgsn_tlli = gbproxy_make_sgsn_tlli(bvc, link_info, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 541 | parse_ctx->tlli); |
| 542 | link_info->sgsn_tlli.current = sgsn_tlli; |
| 543 | link_info->tlli.current = parse_ctx->tlli; |
| 544 | } else if (!tlli_is_valid) { |
| 545 | /* New TLLI (info found by IMSI or P-TMSI) */ |
| 546 | link_info->tlli.current = parse_ctx->tlli; |
| 547 | link_info->tlli.assigned = 0; |
| 548 | link_info->sgsn_tlli.current = |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 549 | gbproxy_make_sgsn_tlli(bvc, link_info, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 550 | parse_ctx->tlli); |
| 551 | link_info->sgsn_tlli.assigned = 0; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 552 | gbproxy_touch_link_info(bvc, link_info, now); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 553 | } else { |
| 554 | sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, link_info, 0); |
| 555 | if (!sgsn_tlli) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 556 | sgsn_tlli = gbproxy_make_sgsn_tlli(bvc, link_info, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 557 | parse_ctx->tlli); |
| 558 | |
| 559 | gbproxy_validate_tlli(&link_info->tlli, |
| 560 | parse_ctx->tlli, 0); |
| 561 | gbproxy_validate_tlli(&link_info->sgsn_tlli, |
| 562 | sgsn_tlli, 0); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 563 | gbproxy_touch_link_info(bvc, link_info, now); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 564 | } |
| 565 | } else if (link_info) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 566 | gbproxy_touch_link_info(bvc, link_info, now); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | if (parse_ctx->imsi && link_info && link_info->imsi_len == 0) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 570 | gbproxy_assign_imsi(bvc, link_info, parse_ctx); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 571 | |
| 572 | return link_info; |
| 573 | } |
| 574 | |
| 575 | static struct gbproxy_link_info *gbproxy_get_link_info_dl( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 576 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 577 | struct gprs_gb_parse_context *parse_ctx) |
| 578 | { |
| 579 | struct gbproxy_link_info *link_info = NULL; |
| 580 | |
| 581 | /* Which key to use depends on its availability only, if that fails, do |
| 582 | * not retry it with another key (e.g. IMSI). */ |
| 583 | if (parse_ctx->tlli_enc) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 584 | link_info = gbproxy_link_info_by_sgsn_tlli(bvc, parse_ctx->tlli, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 585 | parse_ctx->peer_nsei); |
| 586 | |
| 587 | /* TODO: Get link_info by (SGSN) P-TMSI if that is available (see |
| 588 | * GSM 08.18, 7.2) instead of using the IMSI as key. */ |
| 589 | else if (parse_ctx->imsi) |
| 590 | link_info = gbproxy_link_info_by_imsi( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 591 | bvc, parse_ctx->imsi, parse_ctx->imsi_len); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 592 | |
| 593 | if (link_info) |
| 594 | link_info->is_deregistered = false; |
| 595 | |
| 596 | return link_info; |
| 597 | } |
| 598 | |
| 599 | struct gbproxy_link_info *gbproxy_update_link_state_dl( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 600 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 601 | time_t now, |
| 602 | struct gprs_gb_parse_context *parse_ctx) |
| 603 | { |
| 604 | struct gbproxy_link_info *link_info = NULL; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 605 | OSMO_ASSERT(bvc); |
| 606 | OSMO_ASSERT(bvc->nse); |
| 607 | struct gbproxy_config *cfg = bvc->nse->cfg; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 608 | OSMO_ASSERT(cfg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 609 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 610 | link_info = gbproxy_get_link_info_dl(bvc, parse_ctx); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 611 | |
| 612 | if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && link_info) { |
| 613 | /* A new P-TMSI has been signalled in the message, |
| 614 | * register new TLLI */ |
| 615 | uint32_t new_sgsn_ptmsi; |
| 616 | uint32_t new_bss_ptmsi = GSM_RESERVED_TMSI; |
| 617 | gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_sgsn_ptmsi); |
| 618 | |
| 619 | if (link_info->sgsn_tlli.ptmsi == new_sgsn_ptmsi) |
| 620 | new_bss_ptmsi = link_info->tlli.ptmsi; |
| 621 | |
| 622 | if (new_bss_ptmsi == GSM_RESERVED_TMSI) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 623 | new_bss_ptmsi = gbproxy_make_bss_ptmsi(bvc, new_sgsn_ptmsi); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 624 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 625 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 626 | "Got new PTMSI %08x from SGSN, using %08x for BSS\n", |
| 627 | new_sgsn_ptmsi, new_bss_ptmsi); |
| 628 | /* Setup PTMSIs */ |
| 629 | link_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi; |
| 630 | link_info->tlli.ptmsi = new_bss_ptmsi; |
| 631 | } else if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && !link_info && |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 632 | !cfg->patch_ptmsi) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 633 | /* A new P-TMSI has been signalled in the message with an unknown |
| 634 | * TLLI, create a new link_info */ |
| 635 | /* TODO: Add a test case for this branch */ |
| 636 | uint32_t new_ptmsi; |
| 637 | gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi); |
| 638 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 639 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 640 | "Adding TLLI %08x to list (SGSN, new P-TMSI is %08x)\n", |
| 641 | parse_ctx->tlli, new_ptmsi); |
| 642 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 643 | link_info = gbproxy_link_info_alloc(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 644 | link_info->sgsn_tlli.current = parse_ctx->tlli; |
| 645 | link_info->tlli.current = parse_ctx->tlli; |
| 646 | link_info->sgsn_tlli.ptmsi = new_ptmsi; |
| 647 | link_info->tlli.ptmsi = new_ptmsi; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 648 | gbproxy_attach_link_info(bvc, now, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 649 | } else if (parse_ctx->tlli_enc && parse_ctx->llc && !link_info && |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 650 | !cfg->patch_ptmsi) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 651 | /* Unknown SGSN TLLI, create a new link_info */ |
| 652 | uint32_t new_ptmsi; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 653 | link_info = gbproxy_link_info_alloc(bvc); |
| 654 | LOGPBVC(bvc, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n", |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 655 | parse_ctx->tlli); |
| 656 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 657 | gbproxy_attach_link_info(bvc, now, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 658 | |
| 659 | /* Setup TLLIs */ |
| 660 | link_info->sgsn_tlli.current = parse_ctx->tlli; |
| 661 | link_info->tlli.current = parse_ctx->tlli; |
| 662 | |
| 663 | if (!parse_ctx->new_ptmsi_enc) |
| 664 | return link_info; |
| 665 | /* A new P-TMSI has been signalled in the message */ |
| 666 | |
| 667 | gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 668 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 669 | "Assigning new P-TMSI %08x\n", new_ptmsi); |
| 670 | /* Setup P-TMSIs */ |
| 671 | link_info->sgsn_tlli.ptmsi = new_ptmsi; |
| 672 | link_info->tlli.ptmsi = new_ptmsi; |
| 673 | } else if (parse_ctx->tlli_enc && parse_ctx->llc && link_info) { |
| 674 | uint32_t bss_tlli = gbproxy_map_tlli(parse_ctx->tlli, |
| 675 | link_info, 1); |
| 676 | gbproxy_validate_tlli(&link_info->sgsn_tlli, parse_ctx->tlli, 1); |
| 677 | gbproxy_validate_tlli(&link_info->tlli, bss_tlli, 1); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 678 | gbproxy_touch_link_info(bvc, link_info, now); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 679 | } else if (link_info) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 680 | gbproxy_touch_link_info(bvc, link_info, now); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | if (parse_ctx->imsi && link_info && link_info->imsi_len == 0) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 684 | gbproxy_assign_imsi(bvc, link_info, parse_ctx); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 685 | |
| 686 | return link_info; |
| 687 | } |
| 688 | |
| 689 | int gbproxy_update_link_state_after( |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 690 | struct gbproxy_bvc *bvc, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 691 | struct gbproxy_link_info *link_info, |
| 692 | time_t now, |
| 693 | struct gprs_gb_parse_context *parse_ctx) |
| 694 | { |
| 695 | int rc = 0; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 696 | OSMO_ASSERT(bvc); |
| 697 | OSMO_ASSERT(bvc->nse); |
| 698 | struct gbproxy_config *cfg = bvc->nse->cfg; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 699 | OSMO_ASSERT(cfg); |
| 700 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 701 | if (parse_ctx->invalidate_tlli && link_info) { |
| 702 | int keep_info = |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 703 | cfg->keep_link_infos == GBPROX_KEEP_ALWAYS || |
| 704 | (cfg->keep_link_infos == GBPROX_KEEP_REATTACH && |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 705 | parse_ctx->await_reattach) || |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 706 | (cfg->keep_link_infos == GBPROX_KEEP_IDENTIFIED && |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 707 | link_info->imsi_len > 0); |
| 708 | if (keep_info) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 709 | LOGPBVC(bvc, LOGL_INFO, "Unregistering TLLI %08x\n", |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 710 | link_info->tlli.current); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 711 | rc = gbproxy_unregister_link_info(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 712 | } else { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 713 | LOGPBVC(bvc, LOGL_INFO, "Removing TLLI %08x from list\n", |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 714 | link_info->tlli.current); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 715 | gbproxy_delete_link_info(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 716 | rc = 1; |
| 717 | } |
| 718 | } else if (parse_ctx->to_bss && parse_ctx->tlli_enc && |
| 719 | parse_ctx->new_ptmsi_enc && link_info) { |
| 720 | /* A new PTMSI has been signaled in the message, |
| 721 | * register new TLLI */ |
| 722 | uint32_t new_sgsn_ptmsi = link_info->sgsn_tlli.ptmsi; |
| 723 | uint32_t new_bss_ptmsi = link_info->tlli.ptmsi; |
| 724 | uint32_t new_sgsn_tlli; |
| 725 | uint32_t new_bss_tlli = 0; |
| 726 | |
| 727 | new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL); |
| 728 | if (new_bss_ptmsi != GSM_RESERVED_TMSI) |
| 729 | new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 730 | LOGPBVC(bvc, LOGL_INFO, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 731 | "Assigning new TLLI %08x to SGSN, %08x to BSS\n", |
| 732 | new_sgsn_tlli, new_bss_tlli); |
| 733 | |
| 734 | gbproxy_reassign_tlli(&link_info->sgsn_tlli, |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 735 | bvc, new_sgsn_tlli); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 736 | gbproxy_reassign_tlli(&link_info->tlli, |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 737 | bvc, new_bss_tlli); |
| 738 | gbproxy_remove_matching_link_infos(bvc, link_info); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame^] | 741 | gbproxy_remove_stale_link_infos(bvc, now); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 742 | |
| 743 | return rc; |
| 744 | } |
| 745 | |
| 746 | |