blob: 948745943fdf73d2179c9dcf59565d3ae2e6818a [file] [log] [blame]
Jacob Erlbeck9114bee2014-08-19 12:21:01 +02001/* 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
Harald Welte6e688082014-08-24 17:38:18 +020021#include <osmocom/gsm/gsm48.h>
22
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020023#include <osmocom/sgsn/gb_proxy.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020024
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020025#include <osmocom/sgsn/gprs_utils.h>
26#include <osmocom/sgsn/gprs_gb_parse.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020027
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020028#include <osmocom/sgsn/debug.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020029
30#include <osmocom/gsm/gsm_utils.h>
31
32#include <osmocom/core/rate_ctr.h>
33#include <osmocom/core/talloc.h>
34
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020035struct gbproxy_link_info *gbproxy_link_info_by_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020036 uint32_t tlli)
37{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020038 struct gbproxy_link_info *link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020039 struct gbproxy_patch_state *state = &peer->patch_state;
40
Jacob Erlbeckba6267f2014-09-19 16:14:14 +020041 if (!tlli)
42 return NULL;
43
Jacob Erlbeckf8562e32014-09-19 16:03:07 +020044 llist_for_each_entry(link_info, &state->logical_links, list)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020045 if (link_info->tlli.current == tlli ||
46 link_info->tlli.assigned == tlli)
47 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020048
49 return NULL;
50}
51
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020052struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020053 struct gbproxy_peer *peer,
54 uint32_t ptmsi)
55{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020056 struct gbproxy_link_info *link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020057 struct gbproxy_patch_state *state = &peer->patch_state;
58
Jacob Erlbeckba6267f2014-09-19 16:14:14 +020059 if (ptmsi == GSM_RESERVED_TMSI)
60 return NULL;
61
Jacob Erlbeckf8562e32014-09-19 16:03:07 +020062 llist_for_each_entry(link_info, &state->logical_links, list)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020063 if (link_info->tlli.ptmsi == ptmsi)
64 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020065
66 return NULL;
67}
68
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020069struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020070 struct gbproxy_peer *peer,
71 uint32_t tlli)
72{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020073 struct gbproxy_link_info *link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020074 struct gbproxy_patch_state *state = &peer->patch_state;
75
Jacob Erlbeckba6267f2014-09-19 16:14:14 +020076 if (!tlli)
77 return NULL;
78
Jacob Erlbeck91a0e862014-09-17 10:56:38 +020079 /* Don't care about the NSEI */
Jacob Erlbeckf8562e32014-09-19 16:03:07 +020080 llist_for_each_entry(link_info, &state->logical_links, list)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020081 if (link_info->sgsn_tlli.current == tlli ||
82 link_info->sgsn_tlli.assigned == tlli)
83 return link_info;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +020084
85 return NULL;
86}
87
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020088struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
Jacob Erlbeck91a0e862014-09-17 10:56:38 +020089 struct gbproxy_peer *peer,
90 uint32_t tlli, uint32_t sgsn_nsei)
91{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020092 struct gbproxy_link_info *link_info;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +020093 struct gbproxy_patch_state *state = &peer->patch_state;
94
Jacob Erlbeckba6267f2014-09-19 16:14:14 +020095 if (!tlli)
96 return NULL;
97
Jacob Erlbeckf8562e32014-09-19 16:03:07 +020098 llist_for_each_entry(link_info, &state->logical_links, list)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +020099 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;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200103
104 return NULL;
105}
106
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200107struct gbproxy_link_info *gbproxy_link_info_by_imsi(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200108 struct gbproxy_peer *peer,
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200109 const uint8_t *imsi,
110 size_t imsi_len)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200111{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200112 struct gbproxy_link_info *link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200113 struct gbproxy_patch_state *state = &peer->patch_state;
114
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200115 if (!gprs_is_mi_imsi(imsi, imsi_len))
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200116 return NULL;
117
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200118 llist_for_each_entry(link_info, &state->logical_links, list) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200119 if (link_info->imsi_len != imsi_len)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200120 continue;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200121 if (memcmp(link_info->imsi, imsi, imsi_len) != 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200122 continue;
123
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200124 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200125 }
126
127 return NULL;
128}
129
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200130void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200131{
132 struct msgb *msg, *nxt;
133
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200134 llist_for_each_entry_safe(msg, nxt, &link_info->stored_msgs, list) {
Jacob Erlbeck31591142014-09-03 11:59:48 +0200135 llist_del(&msg->list);
136 msgb_free(msg);
137 }
138}
139
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200140void gbproxy_delete_link_info(struct gbproxy_peer *peer,
141 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200142{
143 struct gbproxy_patch_state *state = &peer->patch_state;
144
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200145 gbproxy_link_info_discard_messages(link_info);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200146
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200147 llist_del(&link_info->list);
148 talloc_free(link_info);
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200149 state->logical_link_count -= 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200150
151 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200152 state->logical_link_count;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200153}
154
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200155void gbproxy_delete_link_infos(struct gbproxy_peer *peer)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200156{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200157 struct gbproxy_link_info *link_info, *nxt;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200158 struct gbproxy_patch_state *state = &peer->patch_state;
159
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200160 llist_for_each_entry_safe(link_info, nxt, &state->logical_links, list)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200161 gbproxy_delete_link_info(peer, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200162
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200163 OSMO_ASSERT(state->logical_link_count == 0);
164 OSMO_ASSERT(llist_empty(&state->logical_links));
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200165}
166
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200167void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
168 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200169{
170 struct gbproxy_patch_state *state = &peer->patch_state;
171
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200172 link_info->timestamp = now;
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200173 llist_add(&link_info->list, &state->logical_links);
174 state->logical_link_count += 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200175
176 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200177 state->logical_link_count;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200178}
179
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200180int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200181{
182 struct gbproxy_patch_state *state = &peer->patch_state;
183 int exceeded_max_len = 0;
184 int deleted_count = 0;
185 int check_for_age;
Daniel Willmann447ad442020-11-26 18:19:21 +0100186 OSMO_ASSERT(peer->nse);
187 struct gbproxy_config *cfg = peer->nse->cfg;
188 OSMO_ASSERT(cfg);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200189
Daniel Willmann447ad442020-11-26 18:19:21 +0100190 if (cfg->tlli_max_len > 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200191 exceeded_max_len =
Daniel Willmann447ad442020-11-26 18:19:21 +0100192 state->logical_link_count - cfg->tlli_max_len;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200193
Daniel Willmann447ad442020-11-26 18:19:21 +0100194 check_for_age = cfg->tlli_max_age > 0;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200195
196 for (; exceeded_max_len > 0; exceeded_max_len--) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200197 struct gbproxy_link_info *link_info;
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200198 OSMO_ASSERT(!llist_empty(&state->logical_links));
199 link_info = llist_entry(state->logical_links.prev,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200200 struct gbproxy_link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200201 list);
202 LOGP(DGPRS, LOGL_INFO,
203 "Removing TLLI %08x from list "
204 "(stale, length %d, max_len exceeded)\n",
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200205 link_info->tlli.current, state->logical_link_count);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200206
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200207 gbproxy_delete_link_info(peer, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200208 deleted_count += 1;
209 }
210
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200211 while (check_for_age && !llist_empty(&state->logical_links)) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200212 time_t age;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200213 struct gbproxy_link_info *link_info;
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200214 link_info = llist_entry(state->logical_links.prev,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200215 struct gbproxy_link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200216 list);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200217 age = now - link_info->timestamp;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200218 /* age < 0 only happens after system time jumps, discard entry */
Daniel Willmann447ad442020-11-26 18:19:21 +0100219 if (age <= cfg->tlli_max_age && age >= 0) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200220 check_for_age = 0;
221 continue;
222 }
223
224 LOGP(DGPRS, LOGL_INFO,
225 "Removing TLLI %08x from list "
226 "(stale, age %d, max_age exceeded)\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200227 link_info->tlli.current, (int)age);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200228
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200229 gbproxy_delete_link_info(peer, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200230 deleted_count += 1;
231 }
232
233 return deleted_count;
234}
235
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200236struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200237{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200238 struct gbproxy_link_info *link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200239
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200240 link_info = talloc_zero(peer, struct gbproxy_link_info);
241 link_info->tlli.ptmsi = GSM_RESERVED_TMSI;
242 link_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200243
Jacob Erlbeck65750282014-09-22 15:41:21 +0200244 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
245
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200246 INIT_LLIST_HEAD(&link_info->stored_msgs);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200247
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200248 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200249}
250
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200251void gbproxy_detach_link_info(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200252 struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200253 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200254{
255 struct gbproxy_patch_state *state = &peer->patch_state;
256
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200257 llist_del(&link_info->list);
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200258 OSMO_ASSERT(state->logical_link_count > 0);
259 state->logical_link_count -= 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200260
261 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200262 state->logical_link_count;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200263}
264
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200265void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200266 const uint8_t *imsi, size_t imsi_len)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200267{
268 if (!gprs_is_mi_imsi(imsi, imsi_len))
269 return;
270
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200271 link_info->imsi_len = imsi_len;
272 link_info->imsi =
273 talloc_realloc_size(link_info, link_info->imsi, imsi_len);
274 OSMO_ASSERT(link_info->imsi != NULL);
275 memcpy(link_info->imsi, imsi, imsi_len);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200276}
277
278void gbproxy_reassign_tlli(struct gbproxy_tlli_state *tlli_state,
279 struct gbproxy_peer *peer, uint32_t new_tlli)
280{
281 if (new_tlli == tlli_state->current)
282 return;
283
284 LOGP(DGPRS, LOGL_INFO,
285 "The TLLI has been reassigned from %08x to %08x\n",
286 tlli_state->current, new_tlli);
287
288 /* Remember assigned TLLI */
289 tlli_state->assigned = new_tlli;
Harald Welteaed46ec2019-03-22 09:44:42 +0100290 tlli_state->bss_validated = false;
291 tlli_state->net_validated = false;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200292}
293
294uint32_t gbproxy_map_tlli(uint32_t other_tlli,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200295 struct gbproxy_link_info *link_info, int to_bss)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200296{
297 uint32_t tlli = 0;
298 struct gbproxy_tlli_state *src, *dst;
299 if (to_bss) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200300 src = &link_info->sgsn_tlli;
301 dst = &link_info->tlli;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200302 } else {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200303 src = &link_info->tlli;
304 dst = &link_info->sgsn_tlli;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200305 }
306 if (src->current == other_tlli)
307 tlli = dst->current;
308 else if (src->assigned == other_tlli)
309 tlli = dst->assigned;
310
311 return tlli;
312}
313
314static void gbproxy_validate_tlli(struct gbproxy_tlli_state *tlli_state,
315 uint32_t tlli, int to_bss)
316{
317 LOGP(DGPRS, LOGL_DEBUG,
318 "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n",
319 __func__, tlli_state->current, tlli_state->assigned,
320 tlli_state->net_validated, tlli_state->bss_validated, tlli);
321
322 if (!tlli_state->assigned || tlli_state->assigned != tlli)
323 return;
324
325 /* TODO: Is this ok? Check spec */
326 if (gprs_tlli_type(tlli) != TLLI_LOCAL)
327 return;
328
329 /* See GSM 04.08, 4.7.1.5 */
330 if (to_bss)
Harald Welteaed46ec2019-03-22 09:44:42 +0100331 tlli_state->net_validated = true;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200332 else
Harald Welteaed46ec2019-03-22 09:44:42 +0100333 tlli_state->bss_validated = true;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200334
335 if (!tlli_state->bss_validated || !tlli_state->net_validated)
336 return;
337
338 LOGP(DGPRS, LOGL_INFO,
339 "The TLLI %08x has been validated (was %08x)\n",
340 tlli_state->assigned, tlli_state->current);
341
342 tlli_state->current = tlli;
343 tlli_state->assigned = 0;
344}
345
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200346static void gbproxy_touch_link_info(struct gbproxy_peer *peer,
347 struct gbproxy_link_info *link_info,
Jacob Erlbeck9a7b0d52014-09-19 13:30:14 +0200348 time_t now)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200349{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200350 gbproxy_detach_link_info(peer, link_info);
351 gbproxy_attach_link_info(peer, now, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200352}
353
Daniel Willmannbeade312016-11-07 17:54:29 +0100354static int gbproxy_unregister_link_info(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200355 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200356{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200357 if (!link_info)
Daniel Willmannbeade312016-11-07 17:54:29 +0100358 return 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200359
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200360 if (link_info->tlli.ptmsi == GSM_RESERVED_TMSI && !link_info->imsi_len) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200361 LOGP(DGPRS, LOGL_INFO,
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200362 "Removing TLLI %08x from list (P-TMSI or IMSI are not set)\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200363 link_info->tlli.current);
364 gbproxy_delete_link_info(peer, link_info);
Daniel Willmannbeade312016-11-07 17:54:29 +0100365 return 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200366 }
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200367
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200368 link_info->tlli.current = 0;
369 link_info->tlli.assigned = 0;
370 link_info->sgsn_tlli.current = 0;
371 link_info->sgsn_tlli.assigned = 0;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200372
Harald Welteaed46ec2019-03-22 09:44:42 +0100373 link_info->is_deregistered = true;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200374
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +0200375 gbproxy_reset_link(link_info);
376
Daniel Willmannbeade312016-11-07 17:54:29 +0100377 return 0;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200378}
379
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200380int gbproxy_imsi_matches(struct gbproxy_config *cfg,
381 enum gbproxy_match_id match_id,
382 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200383{
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200384 struct gbproxy_match *match;
385 OSMO_ASSERT(match_id >= 0 && match_id < ARRAY_SIZE(cfg->matches));
386
387 match = &cfg->matches[match_id];
388 if (!match->enable)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200389 return 1;
390
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200391 return link_info != NULL && link_info->is_matching[match_id];
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200392}
393
Pau Espin Pedrold72f31b2018-11-22 16:52:54 +0100394static void gbproxy_assign_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200395 struct gbproxy_link_info *link_info,
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200396 struct gprs_gb_parse_context *parse_ctx)
397{
Jacob Erlbeck8992f302014-09-19 13:17:55 +0200398 int imsi_matches;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200399 struct gbproxy_link_info *other_link_info;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200400 enum gbproxy_match_id match_id;
Daniel Willmann447ad442020-11-26 18:19:21 +0100401 OSMO_ASSERT(peer->nse);
402 struct gbproxy_config *cfg = peer->nse->cfg;
403 OSMO_ASSERT(cfg);
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200404
405 /* Make sure that there is a second entry with the same IMSI */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200406 other_link_info = gbproxy_link_info_by_imsi(
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200407 peer, parse_ctx->imsi, parse_ctx->imsi_len);
408
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200409 if (other_link_info && other_link_info != link_info) {
Neels Hofmeyrb26a5a82020-05-29 16:53:23 +0200410 struct osmo_mobile_identity mi;
411 if (osmo_mobile_identity_decode(&mi, parse_ctx->imsi, parse_ctx->imsi_len, false)
412 || mi.type != GSM_MI_TYPE_IMSI) {
413 LOGP(DGPRS, LOGL_ERROR, "Failed to decode Mobile Identity\n");
414 } else {
415 LOGP(DGPRS, LOGL_INFO,
416 "Removing TLLI %08x from list (IMSI %s re-used)\n",
417 other_link_info->tlli.current, mi.imsi);
418 gbproxy_delete_link_info(peer, other_link_info);
419 }
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200420 }
421
422 /* Update the IMSI field */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200423 gbproxy_update_link_info(link_info,
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200424 parse_ctx->imsi, parse_ctx->imsi_len);
425
426 /* Check, whether the IMSI matches */
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200427 OSMO_ASSERT(ARRAY_SIZE(link_info->is_matching) ==
Daniel Willmann447ad442020-11-26 18:19:21 +0100428 ARRAY_SIZE(cfg->matches));
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200429 for (match_id = 0; match_id < ARRAY_SIZE(link_info->is_matching);
430 ++match_id) {
431 imsi_matches = gbproxy_check_imsi(
Daniel Willmann447ad442020-11-26 18:19:21 +0100432 &cfg->matches[match_id],
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200433 parse_ctx->imsi, parse_ctx->imsi_len);
434 if (imsi_matches >= 0)
Harald Welteaed46ec2019-03-22 09:44:42 +0100435 link_info->is_matching[match_id] = imsi_matches ? true : false;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200436 }
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200437}
438
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200439static int gbproxy_tlli_match(const struct gbproxy_tlli_state *a,
440 const struct gbproxy_tlli_state *b)
441{
442 if (a->current && a->current == b->current)
443 return 1;
444
445 if (a->assigned && a->assigned == b->assigned)
446 return 1;
447
448 if (a->ptmsi != GSM_RESERVED_TMSI && a->ptmsi == b->ptmsi)
449 return 1;
450
451 return 0;
452}
453
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200454static void gbproxy_remove_matching_link_infos(
455 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info)
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200456{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200457 struct gbproxy_link_info *info, *nxt;
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200458 struct gbproxy_patch_state *state = &peer->patch_state;
459
460 /* Make sure that there is no second entry with the same P-TMSI or TLLI */
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200461 llist_for_each_entry_safe(info, nxt, &state->logical_links, list) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200462 if (info == link_info)
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200463 continue;
464
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200465 if (!gbproxy_tlli_match(&link_info->tlli, &info->tlli) &&
466 (link_info->sgsn_nsei != info->sgsn_nsei ||
467 !gbproxy_tlli_match(&link_info->sgsn_tlli, &info->sgsn_tlli)))
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200468 continue;
469
470 LOGP(DGPRS, LOGL_INFO,
471 "Removing TLLI %08x from list (P-TMSI/TLLI re-used)\n",
472 info->tlli.current);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200473 gbproxy_delete_link_info(peer, info);
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200474 }
475}
476
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100477static struct gbproxy_link_info *gbproxy_get_link_info_ul(
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200478 struct gbproxy_peer *peer,
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100479 int *tlli_is_valid,
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200480 struct gprs_gb_parse_context *parse_ctx)
481{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200482 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200483
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100484 if (parse_ctx->tlli_enc) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200485 link_info = gbproxy_link_info_by_tlli(peer, parse_ctx->tlli);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200486
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100487 if (link_info) {
488 *tlli_is_valid = 1;
489 return link_info;
490 }
491 }
492
493 *tlli_is_valid = 0;
494
495 if (!link_info && parse_ctx->imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200496 link_info = gbproxy_link_info_by_imsi(
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200497 peer, parse_ctx->imsi, parse_ctx->imsi_len);
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100498 }
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200499
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200500 if (!link_info && parse_ctx->ptmsi_enc && !parse_ctx->old_raid_is_foreign) {
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200501 uint32_t bss_ptmsi;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200502 gprs_parse_tmsi(parse_ctx->ptmsi_enc, &bss_ptmsi);
503 link_info = gbproxy_link_info_by_ptmsi(peer, bss_ptmsi);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200504 }
505
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100506 if (!link_info)
507 return NULL;
508
Harald Welteaed46ec2019-03-22 09:44:42 +0100509 link_info->is_deregistered = false;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200510
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200511 return link_info;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200512}
513
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200514struct gbproxy_link_info *gbproxy_update_link_state_ul(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200515 struct gbproxy_peer *peer,
516 time_t now,
517 struct gprs_gb_parse_context *parse_ctx)
518{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200519 struct gbproxy_link_info *link_info;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100520 int tlli_is_valid;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200521
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100522 link_info = gbproxy_get_link_info_ul(peer, &tlli_is_valid, parse_ctx);
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200523
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200524 if (parse_ctx->tlli_enc && parse_ctx->llc) {
525 uint32_t sgsn_tlli;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100526
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200527 if (!link_info) {
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200528 LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n",
529 parse_ctx->tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200530 link_info = gbproxy_link_info_alloc(peer);
531 gbproxy_attach_link_info(peer, now, link_info);
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200532
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200533 /* Setup TLLIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200534 sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200535 parse_ctx->tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200536 link_info->sgsn_tlli.current = sgsn_tlli;
Jacob Erlbeckf349bae2014-09-29 12:45:36 +0200537 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100538 } else if (!tlli_is_valid) {
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200539 /* New TLLI (info found by IMSI or P-TMSI) */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200540 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100541 link_info->tlli.assigned = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200542 link_info->sgsn_tlli.current =
543 gbproxy_make_sgsn_tlli(peer, link_info,
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200544 parse_ctx->tlli);
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100545 link_info->sgsn_tlli.assigned = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200546 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200547 } else {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200548 sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, link_info, 0);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200549 if (!sgsn_tlli)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200550 sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200551 parse_ctx->tlli);
552
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200553 gbproxy_validate_tlli(&link_info->tlli,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200554 parse_ctx->tlli, 0);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200555 gbproxy_validate_tlli(&link_info->sgsn_tlli,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200556 sgsn_tlli, 0);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200557 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200558 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200559 } else if (link_info) {
560 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200561 }
562
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200563 if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
564 gbproxy_assign_imsi(peer, link_info, parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200565
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200566 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200567}
568
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200569static struct gbproxy_link_info *gbproxy_get_link_info_dl(
570 struct gbproxy_peer *peer,
571 struct gprs_gb_parse_context *parse_ctx)
572{
573 struct gbproxy_link_info *link_info = NULL;
574
575 /* Which key to use depends on its availability only, if that fails, do
576 * not retry it with another key (e.g. IMSI). */
577 if (parse_ctx->tlli_enc)
578 link_info = gbproxy_link_info_by_sgsn_tlli(peer, parse_ctx->tlli,
579 parse_ctx->peer_nsei);
580
581 /* TODO: Get link_info by (SGSN) P-TMSI if that is available (see
582 * GSM 08.18, 7.2) instead of using the IMSI as key. */
583 else if (parse_ctx->imsi)
584 link_info = gbproxy_link_info_by_imsi(
585 peer, parse_ctx->imsi, parse_ctx->imsi_len);
586
587 if (link_info)
Harald Welteaed46ec2019-03-22 09:44:42 +0100588 link_info->is_deregistered = false;
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200589
590 return link_info;
591}
592
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200593struct gbproxy_link_info *gbproxy_update_link_state_dl(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200594 struct gbproxy_peer *peer,
595 time_t now,
596 struct gprs_gb_parse_context *parse_ctx)
597{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200598 struct gbproxy_link_info *link_info = NULL;
Daniel Willmann447ad442020-11-26 18:19:21 +0100599 OSMO_ASSERT(peer->nse);
600 struct gbproxy_config *cfg = peer->nse->cfg;
601 OSMO_ASSERT(cfg);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200602
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200603 link_info = gbproxy_get_link_info_dl(peer, parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200604
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200605 if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && link_info) {
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200606 /* A new P-TMSI has been signalled in the message,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200607 * register new TLLI */
608 uint32_t new_sgsn_ptmsi;
Jacob Erlbeck91e9f552014-10-20 16:30:06 +0200609 uint32_t new_bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200610 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_sgsn_ptmsi);
Jacob Erlbeck91e9f552014-10-20 16:30:06 +0200611
612 if (link_info->sgsn_tlli.ptmsi == new_sgsn_ptmsi)
613 new_bss_ptmsi = link_info->tlli.ptmsi;
614
615 if (new_bss_ptmsi == GSM_RESERVED_TMSI)
616 new_bss_ptmsi = gbproxy_make_bss_ptmsi(peer, new_sgsn_ptmsi);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200617
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200618 LOGP(DGPRS, LOGL_INFO,
619 "Got new PTMSI %08x from SGSN, using %08x for BSS\n",
620 new_sgsn_ptmsi, new_bss_ptmsi);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200621 /* Setup PTMSIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200622 link_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi;
623 link_info->tlli.ptmsi = new_bss_ptmsi;
624 } else if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && !link_info &&
Daniel Willmann447ad442020-11-26 18:19:21 +0100625 !cfg->patch_ptmsi) {
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200626 /* A new P-TMSI has been signalled in the message with an unknown
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200627 * TLLI, create a new link_info */
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200628 /* TODO: Add a test case for this branch */
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200629 uint32_t new_ptmsi;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200630 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200631
632 LOGP(DGPRS, LOGL_INFO,
633 "Adding TLLI %08x to list (SGSN, new P-TMSI is %08x)\n",
634 parse_ctx->tlli, new_ptmsi);
635
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200636 link_info = gbproxy_link_info_alloc(peer);
Jacob Erlbeckf349bae2014-09-29 12:45:36 +0200637 link_info->sgsn_tlli.current = parse_ctx->tlli;
638 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200639 link_info->sgsn_tlli.ptmsi = new_ptmsi;
640 link_info->tlli.ptmsi = new_ptmsi;
641 gbproxy_attach_link_info(peer, now, link_info);
642 } else if (parse_ctx->tlli_enc && parse_ctx->llc && !link_info &&
Daniel Willmann447ad442020-11-26 18:19:21 +0100643 !cfg->patch_ptmsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200644 /* Unknown SGSN TLLI, create a new link_info */
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200645 uint32_t new_ptmsi;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200646 link_info = gbproxy_link_info_alloc(peer);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200647 LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n",
648 parse_ctx->tlli);
649
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200650 gbproxy_attach_link_info(peer, now, link_info);
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200651
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200652 /* Setup TLLIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200653 link_info->sgsn_tlli.current = parse_ctx->tlli;
654 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200655
656 if (!parse_ctx->new_ptmsi_enc)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200657 return link_info;
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200658 /* A new P-TMSI has been signalled in the message */
659
Jacob Erlbeck49389172014-10-02 16:14:47 +0200660 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200661 LOGP(DGPRS, LOGL_INFO,
662 "Assigning new P-TMSI %08x\n", new_ptmsi);
663 /* Setup P-TMSIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200664 link_info->sgsn_tlli.ptmsi = new_ptmsi;
665 link_info->tlli.ptmsi = new_ptmsi;
666 } else if (parse_ctx->tlli_enc && parse_ctx->llc && link_info) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200667 uint32_t bss_tlli = gbproxy_map_tlli(parse_ctx->tlli,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200668 link_info, 1);
669 gbproxy_validate_tlli(&link_info->sgsn_tlli, parse_ctx->tlli, 1);
670 gbproxy_validate_tlli(&link_info->tlli, bss_tlli, 1);
671 gbproxy_touch_link_info(peer, link_info, now);
672 } else if (link_info) {
673 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200674 }
675
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200676 if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
677 gbproxy_assign_imsi(peer, link_info, parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200678
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200679 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200680}
681
Daniel Willmannbeade312016-11-07 17:54:29 +0100682int gbproxy_update_link_state_after(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200683 struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200684 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200685 time_t now,
686 struct gprs_gb_parse_context *parse_ctx)
687{
Daniel Willmannbeade312016-11-07 17:54:29 +0100688 int rc = 0;
Daniel Willmann447ad442020-11-26 18:19:21 +0100689 OSMO_ASSERT(peer->nse);
690 struct gbproxy_config *cfg = peer->nse->cfg;
691 OSMO_ASSERT(cfg);
692
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200693 if (parse_ctx->invalidate_tlli && link_info) {
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200694 int keep_info =
Daniel Willmann447ad442020-11-26 18:19:21 +0100695 cfg->keep_link_infos == GBPROX_KEEP_ALWAYS ||
696 (cfg->keep_link_infos == GBPROX_KEEP_REATTACH &&
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200697 parse_ctx->await_reattach) ||
Daniel Willmann447ad442020-11-26 18:19:21 +0100698 (cfg->keep_link_infos == GBPROX_KEEP_IDENTIFIED &&
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200699 link_info->imsi_len > 0);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200700 if (keep_info) {
701 LOGP(DGPRS, LOGL_INFO, "Unregistering TLLI %08x\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200702 link_info->tlli.current);
Daniel Willmannbeade312016-11-07 17:54:29 +0100703 rc = gbproxy_unregister_link_info(peer, link_info);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200704 } else {
705 LOGP(DGPRS, LOGL_INFO, "Removing TLLI %08x from list\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200706 link_info->tlli.current);
707 gbproxy_delete_link_info(peer, link_info);
Daniel Willmannbeade312016-11-07 17:54:29 +0100708 rc = 1;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200709 }
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200710 } else if (parse_ctx->to_bss && parse_ctx->tlli_enc &&
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200711 parse_ctx->new_ptmsi_enc && link_info) {
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200712 /* A new PTMSI has been signaled in the message,
713 * register new TLLI */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200714 uint32_t new_sgsn_ptmsi = link_info->sgsn_tlli.ptmsi;
715 uint32_t new_bss_ptmsi = link_info->tlli.ptmsi;
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200716 uint32_t new_sgsn_tlli;
717 uint32_t new_bss_tlli = 0;
718
719 new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL);
720 if (new_bss_ptmsi != GSM_RESERVED_TMSI)
721 new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL);
722 LOGP(DGPRS, LOGL_INFO,
723 "Assigning new TLLI %08x to SGSN, %08x to BSS\n",
724 new_sgsn_tlli, new_bss_tlli);
725
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200726 gbproxy_reassign_tlli(&link_info->sgsn_tlli,
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200727 peer, new_sgsn_tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200728 gbproxy_reassign_tlli(&link_info->tlli,
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200729 peer, new_bss_tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200730 gbproxy_remove_matching_link_infos(peer, link_info);
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200731 }
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200732
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200733 gbproxy_remove_stale_link_infos(peer, now);
Daniel Willmannbeade312016-11-07 17:54:29 +0100734
735 return rc;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200736}
737
738