blob: 3b3b976a5785c747771e53039a2834c381d2aa3b [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
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020023#include <openbsc/gb_proxy.h>
24
25#include <openbsc/gprs_utils.h>
26#include <openbsc/gprs_gb_parse.h>
27
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020028#include <openbsc/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
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;
186
187 if (peer->cfg->tlli_max_len > 0)
188 exceeded_max_len =
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200189 state->logical_link_count - peer->cfg->tlli_max_len;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200190
191 check_for_age = peer->cfg->tlli_max_age > 0;
192
193 for (; exceeded_max_len > 0; exceeded_max_len--) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200194 struct gbproxy_link_info *link_info;
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200195 OSMO_ASSERT(!llist_empty(&state->logical_links));
196 link_info = llist_entry(state->logical_links.prev,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200197 struct gbproxy_link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200198 list);
199 LOGP(DGPRS, LOGL_INFO,
200 "Removing TLLI %08x from list "
201 "(stale, length %d, max_len exceeded)\n",
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200202 link_info->tlli.current, state->logical_link_count);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200203
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200204 gbproxy_delete_link_info(peer, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200205 deleted_count += 1;
206 }
207
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200208 while (check_for_age && !llist_empty(&state->logical_links)) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200209 time_t age;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200210 struct gbproxy_link_info *link_info;
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200211 link_info = llist_entry(state->logical_links.prev,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200212 struct gbproxy_link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200213 list);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200214 age = now - link_info->timestamp;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200215 /* age < 0 only happens after system time jumps, discard entry */
216 if (age <= peer->cfg->tlli_max_age && age >= 0) {
217 check_for_age = 0;
218 continue;
219 }
220
221 LOGP(DGPRS, LOGL_INFO,
222 "Removing TLLI %08x from list "
223 "(stale, age %d, max_age exceeded)\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200224 link_info->tlli.current, (int)age);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200225
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200226 gbproxy_delete_link_info(peer, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200227 deleted_count += 1;
228 }
229
230 return deleted_count;
231}
232
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200233struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200234{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200235 struct gbproxy_link_info *link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200236
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200237 link_info = talloc_zero(peer, struct gbproxy_link_info);
238 link_info->tlli.ptmsi = GSM_RESERVED_TMSI;
239 link_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200240
Jacob Erlbeck65750282014-09-22 15:41:21 +0200241 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
242
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200243 INIT_LLIST_HEAD(&link_info->stored_msgs);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200244
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200245 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200246}
247
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200248void gbproxy_detach_link_info(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200249 struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200250 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200251{
252 struct gbproxy_patch_state *state = &peer->patch_state;
253
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200254 llist_del(&link_info->list);
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200255 OSMO_ASSERT(state->logical_link_count > 0);
256 state->logical_link_count -= 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200257
258 peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200259 state->logical_link_count;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200260}
261
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200262void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200263 const uint8_t *imsi, size_t imsi_len)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200264{
265 if (!gprs_is_mi_imsi(imsi, imsi_len))
266 return;
267
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200268 link_info->imsi_len = imsi_len;
269 link_info->imsi =
270 talloc_realloc_size(link_info, link_info->imsi, imsi_len);
271 OSMO_ASSERT(link_info->imsi != NULL);
272 memcpy(link_info->imsi, imsi, imsi_len);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200273}
274
275void gbproxy_reassign_tlli(struct gbproxy_tlli_state *tlli_state,
276 struct gbproxy_peer *peer, uint32_t new_tlli)
277{
278 if (new_tlli == tlli_state->current)
279 return;
280
281 LOGP(DGPRS, LOGL_INFO,
282 "The TLLI has been reassigned from %08x to %08x\n",
283 tlli_state->current, new_tlli);
284
285 /* Remember assigned TLLI */
286 tlli_state->assigned = new_tlli;
287 tlli_state->bss_validated = 0;
288 tlli_state->net_validated = 0;
289}
290
291uint32_t gbproxy_map_tlli(uint32_t other_tlli,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200292 struct gbproxy_link_info *link_info, int to_bss)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200293{
294 uint32_t tlli = 0;
295 struct gbproxy_tlli_state *src, *dst;
296 if (to_bss) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200297 src = &link_info->sgsn_tlli;
298 dst = &link_info->tlli;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200299 } else {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200300 src = &link_info->tlli;
301 dst = &link_info->sgsn_tlli;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200302 }
303 if (src->current == other_tlli)
304 tlli = dst->current;
305 else if (src->assigned == other_tlli)
306 tlli = dst->assigned;
307
308 return tlli;
309}
310
311static void gbproxy_validate_tlli(struct gbproxy_tlli_state *tlli_state,
312 uint32_t tlli, int to_bss)
313{
314 LOGP(DGPRS, LOGL_DEBUG,
315 "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n",
316 __func__, tlli_state->current, tlli_state->assigned,
317 tlli_state->net_validated, tlli_state->bss_validated, tlli);
318
319 if (!tlli_state->assigned || tlli_state->assigned != tlli)
320 return;
321
322 /* TODO: Is this ok? Check spec */
323 if (gprs_tlli_type(tlli) != TLLI_LOCAL)
324 return;
325
326 /* See GSM 04.08, 4.7.1.5 */
327 if (to_bss)
328 tlli_state->net_validated = 1;
329 else
330 tlli_state->bss_validated = 1;
331
332 if (!tlli_state->bss_validated || !tlli_state->net_validated)
333 return;
334
335 LOGP(DGPRS, LOGL_INFO,
336 "The TLLI %08x has been validated (was %08x)\n",
337 tlli_state->assigned, tlli_state->current);
338
339 tlli_state->current = tlli;
340 tlli_state->assigned = 0;
341}
342
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200343static void gbproxy_touch_link_info(struct gbproxy_peer *peer,
344 struct gbproxy_link_info *link_info,
Jacob Erlbeck9a7b0d52014-09-19 13:30:14 +0200345 time_t now)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200346{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200347 gbproxy_detach_link_info(peer, link_info);
348 gbproxy_attach_link_info(peer, now, link_info);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200349}
350
Daniel Willmannbeade312016-11-07 17:54:29 +0100351static int gbproxy_unregister_link_info(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200352 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200353{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200354 if (!link_info)
Daniel Willmannbeade312016-11-07 17:54:29 +0100355 return 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200356
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200357 if (link_info->tlli.ptmsi == GSM_RESERVED_TMSI && !link_info->imsi_len) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200358 LOGP(DGPRS, LOGL_INFO,
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200359 "Removing TLLI %08x from list (P-TMSI or IMSI are not set)\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200360 link_info->tlli.current);
361 gbproxy_delete_link_info(peer, link_info);
Daniel Willmannbeade312016-11-07 17:54:29 +0100362 return 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200363 }
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200364
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200365 link_info->tlli.current = 0;
366 link_info->tlli.assigned = 0;
367 link_info->sgsn_tlli.current = 0;
368 link_info->sgsn_tlli.assigned = 0;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200369
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200370 link_info->is_deregistered = 1;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200371
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +0200372 gbproxy_reset_link(link_info);
373
Daniel Willmannbeade312016-11-07 17:54:29 +0100374 return 0;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200375}
376
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200377int gbproxy_imsi_matches(struct gbproxy_config *cfg,
378 enum gbproxy_match_id match_id,
379 struct gbproxy_link_info *link_info)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200380{
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200381 struct gbproxy_match *match;
382 OSMO_ASSERT(match_id >= 0 && match_id < ARRAY_SIZE(cfg->matches));
383
384 match = &cfg->matches[match_id];
385 if (!match->enable)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200386 return 1;
387
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200388 return link_info != NULL && link_info->is_matching[match_id];
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200389}
390
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200391void gbproxy_assign_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200392 struct gbproxy_link_info *link_info,
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200393 struct gprs_gb_parse_context *parse_ctx)
394{
Jacob Erlbeck8992f302014-09-19 13:17:55 +0200395 int imsi_matches;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200396 struct gbproxy_link_info *other_link_info;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200397 enum gbproxy_match_id match_id;
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200398
399 /* Make sure that there is a second entry with the same IMSI */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200400 other_link_info = gbproxy_link_info_by_imsi(
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200401 peer, parse_ctx->imsi, parse_ctx->imsi_len);
402
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200403 if (other_link_info && other_link_info != link_info) {
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200404 char mi_buf[200];
405 mi_buf[0] = '\0';
406 gsm48_mi_to_string(mi_buf, sizeof(mi_buf),
407 parse_ctx->imsi, parse_ctx->imsi_len);
408 LOGP(DGPRS, LOGL_INFO,
409 "Removing TLLI %08x from list (IMSI %s re-used)\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200410 other_link_info->tlli.current, mi_buf);
411 gbproxy_delete_link_info(peer, other_link_info);
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200412 }
413
414 /* Update the IMSI field */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200415 gbproxy_update_link_info(link_info,
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200416 parse_ctx->imsi, parse_ctx->imsi_len);
417
418 /* Check, whether the IMSI matches */
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200419 OSMO_ASSERT(ARRAY_SIZE(link_info->is_matching) ==
420 ARRAY_SIZE(peer->cfg->matches));
421 for (match_id = 0; match_id < ARRAY_SIZE(link_info->is_matching);
422 ++match_id) {
423 imsi_matches = gbproxy_check_imsi(
424 &peer->cfg->matches[match_id],
425 parse_ctx->imsi, parse_ctx->imsi_len);
426 if (imsi_matches >= 0)
427 link_info->is_matching[match_id] = imsi_matches;
428 }
Jacob Erlbeck16a3cd32014-09-15 11:46:42 +0200429}
430
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200431static int gbproxy_tlli_match(const struct gbproxy_tlli_state *a,
432 const struct gbproxy_tlli_state *b)
433{
434 if (a->current && a->current == b->current)
435 return 1;
436
437 if (a->assigned && a->assigned == b->assigned)
438 return 1;
439
440 if (a->ptmsi != GSM_RESERVED_TMSI && a->ptmsi == b->ptmsi)
441 return 1;
442
443 return 0;
444}
445
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200446static void gbproxy_remove_matching_link_infos(
447 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info)
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200448{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200449 struct gbproxy_link_info *info, *nxt;
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200450 struct gbproxy_patch_state *state = &peer->patch_state;
451
452 /* Make sure that there is no second entry with the same P-TMSI or TLLI */
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200453 llist_for_each_entry_safe(info, nxt, &state->logical_links, list) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200454 if (info == link_info)
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200455 continue;
456
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200457 if (!gbproxy_tlli_match(&link_info->tlli, &info->tlli) &&
458 (link_info->sgsn_nsei != info->sgsn_nsei ||
459 !gbproxy_tlli_match(&link_info->sgsn_tlli, &info->sgsn_tlli)))
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200460 continue;
461
462 LOGP(DGPRS, LOGL_INFO,
463 "Removing TLLI %08x from list (P-TMSI/TLLI re-used)\n",
464 info->tlli.current);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200465 gbproxy_delete_link_info(peer, info);
Jacob Erlbeck1a024422014-09-16 14:10:27 +0200466 }
467}
468
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100469static struct gbproxy_link_info *gbproxy_get_link_info_ul(
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200470 struct gbproxy_peer *peer,
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100471 int *tlli_is_valid,
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200472 struct gprs_gb_parse_context *parse_ctx)
473{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200474 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200475
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100476 if (parse_ctx->tlli_enc) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200477 link_info = gbproxy_link_info_by_tlli(peer, parse_ctx->tlli);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200478
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100479 if (link_info) {
480 *tlli_is_valid = 1;
481 return link_info;
482 }
483 }
484
485 *tlli_is_valid = 0;
486
487 if (!link_info && parse_ctx->imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200488 link_info = gbproxy_link_info_by_imsi(
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200489 peer, parse_ctx->imsi, parse_ctx->imsi_len);
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100490 }
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200491
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200492 if (!link_info && parse_ctx->ptmsi_enc && !parse_ctx->old_raid_is_foreign) {
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200493 uint32_t bss_ptmsi;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200494 gprs_parse_tmsi(parse_ctx->ptmsi_enc, &bss_ptmsi);
495 link_info = gbproxy_link_info_by_ptmsi(peer, bss_ptmsi);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200496 }
497
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100498 if (!link_info)
499 return NULL;
500
501 link_info->is_deregistered = 0;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200502
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200503 return link_info;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200504}
505
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200506struct gbproxy_link_info *gbproxy_update_link_state_ul(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200507 struct gbproxy_peer *peer,
508 time_t now,
509 struct gprs_gb_parse_context *parse_ctx)
510{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200511 struct gbproxy_link_info *link_info;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100512 int tlli_is_valid;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200513
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100514 link_info = gbproxy_get_link_info_ul(peer, &tlli_is_valid, parse_ctx);
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200515
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200516 if (parse_ctx->tlli_enc && parse_ctx->llc) {
517 uint32_t sgsn_tlli;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100518
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200519 if (!link_info) {
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200520 LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n",
521 parse_ctx->tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200522 link_info = gbproxy_link_info_alloc(peer);
523 gbproxy_attach_link_info(peer, now, link_info);
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200524
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200525 /* Setup TLLIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200526 sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200527 parse_ctx->tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200528 link_info->sgsn_tlli.current = sgsn_tlli;
Jacob Erlbeckf349bae2014-09-29 12:45:36 +0200529 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100530 } else if (!tlli_is_valid) {
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200531 /* New TLLI (info found by IMSI or P-TMSI) */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200532 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100533 link_info->tlli.assigned = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200534 link_info->sgsn_tlli.current =
535 gbproxy_make_sgsn_tlli(peer, link_info,
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200536 parse_ctx->tlli);
Jacob Erlbeck59ac49d2014-10-30 17:15:43 +0100537 link_info->sgsn_tlli.assigned = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200538 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200539 } else {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200540 sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, link_info, 0);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200541 if (!sgsn_tlli)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200542 sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200543 parse_ctx->tlli);
544
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200545 gbproxy_validate_tlli(&link_info->tlli,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200546 parse_ctx->tlli, 0);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200547 gbproxy_validate_tlli(&link_info->sgsn_tlli,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200548 sgsn_tlli, 0);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200549 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200550 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200551 } else if (link_info) {
552 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200553 }
554
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200555 if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
556 gbproxy_assign_imsi(peer, link_info, parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200557
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200558 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200559}
560
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200561static struct gbproxy_link_info *gbproxy_get_link_info_dl(
562 struct gbproxy_peer *peer,
563 struct gprs_gb_parse_context *parse_ctx)
564{
565 struct gbproxy_link_info *link_info = NULL;
566
567 /* Which key to use depends on its availability only, if that fails, do
568 * not retry it with another key (e.g. IMSI). */
569 if (parse_ctx->tlli_enc)
570 link_info = gbproxy_link_info_by_sgsn_tlli(peer, parse_ctx->tlli,
571 parse_ctx->peer_nsei);
572
573 /* TODO: Get link_info by (SGSN) P-TMSI if that is available (see
574 * GSM 08.18, 7.2) instead of using the IMSI as key. */
575 else if (parse_ctx->imsi)
576 link_info = gbproxy_link_info_by_imsi(
577 peer, parse_ctx->imsi, parse_ctx->imsi_len);
578
579 if (link_info)
580 link_info->is_deregistered = 0;
581
582 return link_info;
583}
584
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200585struct gbproxy_link_info *gbproxy_update_link_state_dl(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200586 struct gbproxy_peer *peer,
587 time_t now,
588 struct gprs_gb_parse_context *parse_ctx)
589{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200590 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200591
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200592 link_info = gbproxy_get_link_info_dl(peer, parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200593
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200594 if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && link_info) {
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200595 /* A new P-TMSI has been signalled in the message,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200596 * register new TLLI */
597 uint32_t new_sgsn_ptmsi;
Jacob Erlbeck91e9f552014-10-20 16:30:06 +0200598 uint32_t new_bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200599 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_sgsn_ptmsi);
Jacob Erlbeck91e9f552014-10-20 16:30:06 +0200600
601 if (link_info->sgsn_tlli.ptmsi == new_sgsn_ptmsi)
602 new_bss_ptmsi = link_info->tlli.ptmsi;
603
604 if (new_bss_ptmsi == GSM_RESERVED_TMSI)
605 new_bss_ptmsi = gbproxy_make_bss_ptmsi(peer, new_sgsn_ptmsi);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200606
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200607 LOGP(DGPRS, LOGL_INFO,
608 "Got new PTMSI %08x from SGSN, using %08x for BSS\n",
609 new_sgsn_ptmsi, new_bss_ptmsi);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200610 /* Setup PTMSIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200611 link_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi;
612 link_info->tlli.ptmsi = new_bss_ptmsi;
613 } else if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && !link_info &&
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200614 !peer->cfg->patch_ptmsi) {
615 /* A new P-TMSI has been signalled in the message with an unknown
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200616 * TLLI, create a new link_info */
Jacob Erlbecka42fe9f2014-09-12 14:15:02 +0200617 /* TODO: Add a test case for this branch */
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200618 uint32_t new_ptmsi;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200619 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200620
621 LOGP(DGPRS, LOGL_INFO,
622 "Adding TLLI %08x to list (SGSN, new P-TMSI is %08x)\n",
623 parse_ctx->tlli, new_ptmsi);
624
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200625 link_info = gbproxy_link_info_alloc(peer);
Jacob Erlbeckf349bae2014-09-29 12:45:36 +0200626 link_info->sgsn_tlli.current = parse_ctx->tlli;
627 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200628 link_info->sgsn_tlli.ptmsi = new_ptmsi;
629 link_info->tlli.ptmsi = new_ptmsi;
630 gbproxy_attach_link_info(peer, now, link_info);
631 } else if (parse_ctx->tlli_enc && parse_ctx->llc && !link_info &&
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200632 !peer->cfg->patch_ptmsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200633 /* Unknown SGSN TLLI, create a new link_info */
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200634 uint32_t new_ptmsi;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200635 link_info = gbproxy_link_info_alloc(peer);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200636 LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n",
637 parse_ctx->tlli);
638
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200639 gbproxy_attach_link_info(peer, now, link_info);
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200640
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200641 /* Setup TLLIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200642 link_info->sgsn_tlli.current = parse_ctx->tlli;
643 link_info->tlli.current = parse_ctx->tlli;
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200644
645 if (!parse_ctx->new_ptmsi_enc)
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200646 return link_info;
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200647 /* A new P-TMSI has been signalled in the message */
648
Jacob Erlbeck49389172014-10-02 16:14:47 +0200649 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
Jacob Erlbeck37fda772014-09-05 10:22:27 +0200650 LOGP(DGPRS, LOGL_INFO,
651 "Assigning new P-TMSI %08x\n", new_ptmsi);
652 /* Setup P-TMSIs */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200653 link_info->sgsn_tlli.ptmsi = new_ptmsi;
654 link_info->tlli.ptmsi = new_ptmsi;
655 } else if (parse_ctx->tlli_enc && parse_ctx->llc && link_info) {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200656 uint32_t bss_tlli = gbproxy_map_tlli(parse_ctx->tlli,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200657 link_info, 1);
658 gbproxy_validate_tlli(&link_info->sgsn_tlli, parse_ctx->tlli, 1);
659 gbproxy_validate_tlli(&link_info->tlli, bss_tlli, 1);
660 gbproxy_touch_link_info(peer, link_info, now);
661 } else if (link_info) {
662 gbproxy_touch_link_info(peer, link_info, now);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200663 }
664
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200665 if (parse_ctx->imsi && link_info && link_info->imsi_len == 0)
666 gbproxy_assign_imsi(peer, link_info, parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200667
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200668 return link_info;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200669}
670
Daniel Willmannbeade312016-11-07 17:54:29 +0100671int gbproxy_update_link_state_after(
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200672 struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200673 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200674 time_t now,
675 struct gprs_gb_parse_context *parse_ctx)
676{
Daniel Willmannbeade312016-11-07 17:54:29 +0100677 int rc = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200678 if (parse_ctx->invalidate_tlli && link_info) {
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200679 int keep_info =
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200680 peer->cfg->keep_link_infos == GBPROX_KEEP_ALWAYS ||
681 (peer->cfg->keep_link_infos == GBPROX_KEEP_REATTACH &&
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200682 parse_ctx->await_reattach) ||
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200683 (peer->cfg->keep_link_infos == GBPROX_KEEP_IDENTIFIED &&
684 link_info->imsi_len > 0);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200685 if (keep_info) {
686 LOGP(DGPRS, LOGL_INFO, "Unregistering TLLI %08x\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200687 link_info->tlli.current);
Daniel Willmannbeade312016-11-07 17:54:29 +0100688 rc = gbproxy_unregister_link_info(peer, link_info);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200689 } else {
690 LOGP(DGPRS, LOGL_INFO, "Removing TLLI %08x from list\n",
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200691 link_info->tlli.current);
692 gbproxy_delete_link_info(peer, link_info);
Daniel Willmannbeade312016-11-07 17:54:29 +0100693 rc = 1;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200694 }
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200695 } else if (parse_ctx->to_bss && parse_ctx->tlli_enc &&
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200696 parse_ctx->new_ptmsi_enc && link_info) {
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200697 /* A new PTMSI has been signaled in the message,
698 * register new TLLI */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200699 uint32_t new_sgsn_ptmsi = link_info->sgsn_tlli.ptmsi;
700 uint32_t new_bss_ptmsi = link_info->tlli.ptmsi;
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200701 uint32_t new_sgsn_tlli;
702 uint32_t new_bss_tlli = 0;
703
704 new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL);
705 if (new_bss_ptmsi != GSM_RESERVED_TMSI)
706 new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL);
707 LOGP(DGPRS, LOGL_INFO,
708 "Assigning new TLLI %08x to SGSN, %08x to BSS\n",
709 new_sgsn_tlli, new_bss_tlli);
710
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200711 gbproxy_reassign_tlli(&link_info->sgsn_tlli,
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200712 peer, new_sgsn_tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200713 gbproxy_reassign_tlli(&link_info->tlli,
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200714 peer, new_bss_tlli);
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200715 gbproxy_remove_matching_link_infos(peer, link_info);
Jacob Erlbeckb1ee5cd2014-09-04 14:53:30 +0200716 }
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200717
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200718 gbproxy_remove_stale_link_infos(peer, now);
Daniel Willmannbeade312016-11-07 17:54:29 +0100719
720 return rc;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200721}
722
723