Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +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 | |
Harald Welte | 642b5c4 | 2014-08-24 17:38:18 +0200 | [diff] [blame] | 21 | #include <osmocom/gsm/gsm48.h> |
| 22 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 23 | #include <openbsc/gb_proxy.h> |
| 24 | |
| 25 | #include <openbsc/gprs_utils.h> |
| 26 | #include <openbsc/gprs_gb_parse.h> |
| 27 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 28 | #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 Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 35 | struct gbproxy_link_info *gbproxy_link_info_by_tlli(struct gbproxy_peer *peer, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 36 | uint32_t tlli) |
| 37 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 38 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 39 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 40 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 41 | llist_for_each_entry(link_info, &state->logical_links, list) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 42 | if (link_info->tlli.current == tlli || |
| 43 | link_info->tlli.assigned == tlli) |
| 44 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 45 | |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 49 | struct gbproxy_link_info *gbproxy_link_info_by_ptmsi( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 50 | struct gbproxy_peer *peer, |
| 51 | uint32_t ptmsi) |
| 52 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 53 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 54 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 55 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 56 | llist_for_each_entry(link_info, &state->logical_links, list) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 57 | if (link_info->tlli.ptmsi == ptmsi) |
| 58 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 59 | |
| 60 | return NULL; |
| 61 | } |
| 62 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 63 | struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 64 | struct gbproxy_peer *peer, |
| 65 | uint32_t tlli) |
| 66 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 67 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 68 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 69 | |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 70 | /* Don't care about the NSEI */ |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 71 | llist_for_each_entry(link_info, &state->logical_links, list) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 72 | if (link_info->sgsn_tlli.current == tlli || |
| 73 | link_info->sgsn_tlli.assigned == tlli) |
| 74 | return link_info; |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 75 | |
| 76 | return NULL; |
| 77 | } |
| 78 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 79 | struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli( |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 80 | struct gbproxy_peer *peer, |
| 81 | uint32_t tlli, uint32_t sgsn_nsei) |
| 82 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 83 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 84 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 85 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 86 | llist_for_each_entry(link_info, &state->logical_links, list) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 87 | if ((link_info->sgsn_tlli.current == tlli || |
| 88 | link_info->sgsn_tlli.assigned == tlli) && |
| 89 | link_info->sgsn_nsei == sgsn_nsei) |
| 90 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 95 | struct gbproxy_link_info *gbproxy_link_info_by_imsi( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 96 | struct gbproxy_peer *peer, |
Jacob Erlbeck | 155dc9c | 2014-09-11 14:57:03 +0200 | [diff] [blame] | 97 | const uint8_t *imsi, |
| 98 | size_t imsi_len) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 99 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 100 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 101 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 102 | |
Jacob Erlbeck | 155dc9c | 2014-09-11 14:57:03 +0200 | [diff] [blame] | 103 | if (!gprs_is_mi_imsi(imsi, imsi_len)) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 104 | return NULL; |
| 105 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 106 | llist_for_each_entry(link_info, &state->logical_links, list) { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 107 | if (link_info->imsi_len != imsi_len) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 108 | continue; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 109 | if (memcmp(link_info->imsi, imsi, imsi_len) != 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 110 | continue; |
| 111 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 112 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 118 | void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info) |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 119 | { |
| 120 | struct msgb *msg, *nxt; |
| 121 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 122 | llist_for_each_entry_safe(msg, nxt, &link_info->stored_msgs, list) { |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 123 | llist_del(&msg->list); |
| 124 | msgb_free(msg); |
| 125 | } |
| 126 | } |
| 127 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 128 | void gbproxy_delete_link_info(struct gbproxy_peer *peer, |
| 129 | struct gbproxy_link_info *link_info) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 130 | { |
| 131 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 132 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 133 | gbproxy_link_info_discard_messages(link_info); |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 134 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 135 | llist_del(&link_info->list); |
| 136 | talloc_free(link_info); |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 137 | state->logical_link_count -= 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 138 | |
| 139 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 140 | state->logical_link_count; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 143 | void gbproxy_delete_link_infos(struct gbproxy_peer *peer) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 144 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 145 | struct gbproxy_link_info *link_info, *nxt; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 146 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 147 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 148 | llist_for_each_entry_safe(link_info, nxt, &state->logical_links, list) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 149 | gbproxy_delete_link_info(peer, link_info); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 150 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 151 | OSMO_ASSERT(state->logical_link_count == 0); |
| 152 | OSMO_ASSERT(llist_empty(&state->logical_links)); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 155 | void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now, |
| 156 | struct gbproxy_link_info *link_info) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 157 | { |
| 158 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 159 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 160 | link_info->timestamp = now; |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 161 | llist_add(&link_info->list, &state->logical_links); |
| 162 | state->logical_link_count += 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 163 | |
| 164 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 165 | state->logical_link_count; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 168 | int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 169 | { |
| 170 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 171 | int exceeded_max_len = 0; |
| 172 | int deleted_count = 0; |
| 173 | int check_for_age; |
| 174 | |
| 175 | if (peer->cfg->tlli_max_len > 0) |
| 176 | exceeded_max_len = |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 177 | state->logical_link_count - peer->cfg->tlli_max_len; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 178 | |
| 179 | check_for_age = peer->cfg->tlli_max_age > 0; |
| 180 | |
| 181 | for (; exceeded_max_len > 0; exceeded_max_len--) { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 182 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 183 | OSMO_ASSERT(!llist_empty(&state->logical_links)); |
| 184 | link_info = llist_entry(state->logical_links.prev, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 185 | struct gbproxy_link_info, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 186 | list); |
| 187 | LOGP(DGPRS, LOGL_INFO, |
| 188 | "Removing TLLI %08x from list " |
| 189 | "(stale, length %d, max_len exceeded)\n", |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 190 | link_info->tlli.current, state->logical_link_count); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 191 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 192 | gbproxy_delete_link_info(peer, link_info); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 193 | deleted_count += 1; |
| 194 | } |
| 195 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 196 | while (check_for_age && !llist_empty(&state->logical_links)) { |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 197 | time_t age; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 198 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 199 | link_info = llist_entry(state->logical_links.prev, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 200 | struct gbproxy_link_info, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 201 | list); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 202 | age = now - link_info->timestamp; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 203 | /* age < 0 only happens after system time jumps, discard entry */ |
| 204 | if (age <= peer->cfg->tlli_max_age && age >= 0) { |
| 205 | check_for_age = 0; |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | LOGP(DGPRS, LOGL_INFO, |
| 210 | "Removing TLLI %08x from list " |
| 211 | "(stale, age %d, max_age exceeded)\n", |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 212 | link_info->tlli.current, (int)age); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 213 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 214 | gbproxy_delete_link_info(peer, link_info); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 215 | deleted_count += 1; |
| 216 | } |
| 217 | |
| 218 | return deleted_count; |
| 219 | } |
| 220 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 221 | struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 222 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 223 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 224 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 225 | link_info = talloc_zero(peer, struct gbproxy_link_info); |
| 226 | link_info->tlli.ptmsi = GSM_RESERVED_TMSI; |
| 227 | link_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 228 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 229 | INIT_LLIST_HEAD(&link_info->stored_msgs); |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 230 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 231 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 234 | void gbproxy_detach_link_info( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 235 | struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 236 | struct gbproxy_link_info *link_info) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 237 | { |
| 238 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 239 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 240 | llist_del(&link_info->list); |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 241 | OSMO_ASSERT(state->logical_link_count > 0); |
| 242 | state->logical_link_count -= 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 243 | |
| 244 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 245 | state->logical_link_count; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 248 | void gbproxy_update_link_info(struct gbproxy_link_info *link_info, |
Jacob Erlbeck | c68f66c | 2014-09-12 14:15:02 +0200 | [diff] [blame] | 249 | const uint8_t *imsi, size_t imsi_len) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 250 | { |
| 251 | if (!gprs_is_mi_imsi(imsi, imsi_len)) |
| 252 | return; |
| 253 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 254 | link_info->imsi_len = imsi_len; |
| 255 | link_info->imsi = |
| 256 | talloc_realloc_size(link_info, link_info->imsi, imsi_len); |
| 257 | OSMO_ASSERT(link_info->imsi != NULL); |
| 258 | memcpy(link_info->imsi, imsi, imsi_len); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void gbproxy_reassign_tlli(struct gbproxy_tlli_state *tlli_state, |
| 262 | struct gbproxy_peer *peer, uint32_t new_tlli) |
| 263 | { |
| 264 | if (new_tlli == tlli_state->current) |
| 265 | return; |
| 266 | |
| 267 | LOGP(DGPRS, LOGL_INFO, |
| 268 | "The TLLI has been reassigned from %08x to %08x\n", |
| 269 | tlli_state->current, new_tlli); |
| 270 | |
| 271 | /* Remember assigned TLLI */ |
| 272 | tlli_state->assigned = new_tlli; |
| 273 | tlli_state->bss_validated = 0; |
| 274 | tlli_state->net_validated = 0; |
| 275 | } |
| 276 | |
| 277 | uint32_t gbproxy_map_tlli(uint32_t other_tlli, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 278 | struct gbproxy_link_info *link_info, int to_bss) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 279 | { |
| 280 | uint32_t tlli = 0; |
| 281 | struct gbproxy_tlli_state *src, *dst; |
| 282 | if (to_bss) { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 283 | src = &link_info->sgsn_tlli; |
| 284 | dst = &link_info->tlli; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 285 | } else { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 286 | src = &link_info->tlli; |
| 287 | dst = &link_info->sgsn_tlli; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 288 | } |
| 289 | if (src->current == other_tlli) |
| 290 | tlli = dst->current; |
| 291 | else if (src->assigned == other_tlli) |
| 292 | tlli = dst->assigned; |
| 293 | |
| 294 | return tlli; |
| 295 | } |
| 296 | |
| 297 | static void gbproxy_validate_tlli(struct gbproxy_tlli_state *tlli_state, |
| 298 | uint32_t tlli, int to_bss) |
| 299 | { |
| 300 | LOGP(DGPRS, LOGL_DEBUG, |
| 301 | "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n", |
| 302 | __func__, tlli_state->current, tlli_state->assigned, |
| 303 | tlli_state->net_validated, tlli_state->bss_validated, tlli); |
| 304 | |
| 305 | if (!tlli_state->assigned || tlli_state->assigned != tlli) |
| 306 | return; |
| 307 | |
| 308 | /* TODO: Is this ok? Check spec */ |
| 309 | if (gprs_tlli_type(tlli) != TLLI_LOCAL) |
| 310 | return; |
| 311 | |
| 312 | /* See GSM 04.08, 4.7.1.5 */ |
| 313 | if (to_bss) |
| 314 | tlli_state->net_validated = 1; |
| 315 | else |
| 316 | tlli_state->bss_validated = 1; |
| 317 | |
| 318 | if (!tlli_state->bss_validated || !tlli_state->net_validated) |
| 319 | return; |
| 320 | |
| 321 | LOGP(DGPRS, LOGL_INFO, |
| 322 | "The TLLI %08x has been validated (was %08x)\n", |
| 323 | tlli_state->assigned, tlli_state->current); |
| 324 | |
| 325 | tlli_state->current = tlli; |
| 326 | tlli_state->assigned = 0; |
| 327 | } |
| 328 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 329 | static void gbproxy_touch_link_info(struct gbproxy_peer *peer, |
| 330 | struct gbproxy_link_info *link_info, |
Jacob Erlbeck | ee621a7 | 2014-09-19 13:30:14 +0200 | [diff] [blame] | 331 | time_t now) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 332 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 333 | gbproxy_detach_link_info(peer, link_info); |
| 334 | gbproxy_attach_link_info(peer, now, link_info); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 337 | static void gbproxy_unregister_link_info(struct gbproxy_peer *peer, |
| 338 | struct gbproxy_link_info *link_info) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 339 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 340 | if (!link_info) |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 341 | return; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 342 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 343 | if (link_info->tlli.ptmsi == GSM_RESERVED_TMSI && !link_info->imsi_len) { |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 344 | LOGP(DGPRS, LOGL_INFO, |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 345 | "Removing TLLI %08x from list (P-TMSI or IMSI are not set)\n", |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 346 | link_info->tlli.current); |
| 347 | gbproxy_delete_link_info(peer, link_info); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 348 | return; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 349 | } |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 350 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 351 | link_info->tlli.current = 0; |
| 352 | link_info->tlli.assigned = 0; |
| 353 | link_info->sgsn_tlli.current = 0; |
| 354 | link_info->sgsn_tlli.assigned = 0; |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 355 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 356 | link_info->is_deregistered = 1; |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 357 | |
| 358 | return; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Jacob Erlbeck | 070702b | 2014-09-19 13:17:55 +0200 | [diff] [blame] | 361 | int gbproxy_imsi_matches(struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 362 | struct gbproxy_link_info *link_info) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 363 | { |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 364 | if (!peer->cfg->check_imsi) |
| 365 | return 1; |
| 366 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 367 | return link_info != NULL && link_info->imsi_matches; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 370 | void gbproxy_assign_imsi(struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 371 | struct gbproxy_link_info *link_info, |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 372 | struct gprs_gb_parse_context *parse_ctx) |
| 373 | { |
Jacob Erlbeck | 070702b | 2014-09-19 13:17:55 +0200 | [diff] [blame] | 374 | int imsi_matches; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 375 | struct gbproxy_link_info *other_link_info; |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 376 | |
| 377 | /* Make sure that there is a second entry with the same IMSI */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 378 | other_link_info = gbproxy_link_info_by_imsi( |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 379 | peer, parse_ctx->imsi, parse_ctx->imsi_len); |
| 380 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 381 | if (other_link_info && other_link_info != link_info) { |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 382 | char mi_buf[200]; |
| 383 | mi_buf[0] = '\0'; |
| 384 | gsm48_mi_to_string(mi_buf, sizeof(mi_buf), |
| 385 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 386 | LOGP(DGPRS, LOGL_INFO, |
| 387 | "Removing TLLI %08x from list (IMSI %s re-used)\n", |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 388 | other_link_info->tlli.current, mi_buf); |
| 389 | gbproxy_delete_link_info(peer, other_link_info); |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* Update the IMSI field */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 393 | gbproxy_update_link_info(link_info, |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 394 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 395 | |
| 396 | /* Check, whether the IMSI matches */ |
Jacob Erlbeck | 070702b | 2014-09-19 13:17:55 +0200 | [diff] [blame] | 397 | imsi_matches = gbproxy_check_imsi(peer, parse_ctx->imsi, |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 398 | parse_ctx->imsi_len); |
Jacob Erlbeck | 070702b | 2014-09-19 13:17:55 +0200 | [diff] [blame] | 399 | if (imsi_matches >= 0) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 400 | link_info->imsi_matches = imsi_matches; |
Jacob Erlbeck | 3b589a9 | 2014-09-15 11:46:42 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Jacob Erlbeck | 0a6ea84 | 2014-09-16 14:10:27 +0200 | [diff] [blame] | 403 | static int gbproxy_tlli_match(const struct gbproxy_tlli_state *a, |
| 404 | const struct gbproxy_tlli_state *b) |
| 405 | { |
| 406 | if (a->current && a->current == b->current) |
| 407 | return 1; |
| 408 | |
| 409 | if (a->assigned && a->assigned == b->assigned) |
| 410 | return 1; |
| 411 | |
| 412 | if (a->ptmsi != GSM_RESERVED_TMSI && a->ptmsi == b->ptmsi) |
| 413 | return 1; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 418 | static void gbproxy_remove_matching_link_infos( |
| 419 | struct gbproxy_peer *peer, struct gbproxy_link_info *link_info) |
Jacob Erlbeck | 0a6ea84 | 2014-09-16 14:10:27 +0200 | [diff] [blame] | 420 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 421 | struct gbproxy_link_info *info, *nxt; |
Jacob Erlbeck | 0a6ea84 | 2014-09-16 14:10:27 +0200 | [diff] [blame] | 422 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 423 | |
| 424 | /* Make sure that there is no second entry with the same P-TMSI or TLLI */ |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 425 | llist_for_each_entry_safe(info, nxt, &state->logical_links, list) { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 426 | if (info == link_info) |
Jacob Erlbeck | 0a6ea84 | 2014-09-16 14:10:27 +0200 | [diff] [blame] | 427 | continue; |
| 428 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 429 | if (!gbproxy_tlli_match(&link_info->tlli, &info->tlli) && |
| 430 | (link_info->sgsn_nsei != info->sgsn_nsei || |
| 431 | !gbproxy_tlli_match(&link_info->sgsn_tlli, &info->sgsn_tlli))) |
Jacob Erlbeck | 0a6ea84 | 2014-09-16 14:10:27 +0200 | [diff] [blame] | 432 | continue; |
| 433 | |
| 434 | LOGP(DGPRS, LOGL_INFO, |
| 435 | "Removing TLLI %08x from list (P-TMSI/TLLI re-used)\n", |
| 436 | info->tlli.current); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 437 | gbproxy_delete_link_info(peer, info); |
Jacob Erlbeck | 0a6ea84 | 2014-09-16 14:10:27 +0200 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 441 | struct gbproxy_link_info *gbproxy_get_link_info_ul( |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 442 | struct gbproxy_peer *peer, |
| 443 | struct gprs_gb_parse_context *parse_ctx) |
| 444 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 445 | struct gbproxy_link_info *link_info = NULL; |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 446 | |
| 447 | if (parse_ctx->tlli_enc) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 448 | link_info = gbproxy_link_info_by_tlli(peer, parse_ctx->tlli); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 449 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 450 | if (!link_info && parse_ctx->imsi) |
| 451 | link_info = gbproxy_link_info_by_imsi( |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 452 | peer, parse_ctx->imsi, parse_ctx->imsi_len); |
| 453 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 454 | if (!link_info && parse_ctx->ptmsi_enc && !parse_ctx->old_raid_is_foreign) { |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 455 | uint32_t bss_ptmsi; |
| 456 | if (!gprs_parse_mi_tmsi(parse_ctx->ptmsi_enc, GSM48_TMSI_LEN, |
| 457 | &bss_ptmsi)) |
| 458 | LOGP(DGPRS, LOGL_ERROR, |
| 459 | "Failed to parse P-TMSI (TLLI is %08x)\n", |
| 460 | parse_ctx->tlli); |
| 461 | else |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 462 | link_info = gbproxy_link_info_by_ptmsi(peer, bss_ptmsi); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 465 | if (link_info) |
| 466 | link_info->is_deregistered = 0; |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 467 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 468 | return link_info; |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 471 | struct gbproxy_link_info *gbproxy_update_link_state_ul( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 472 | struct gbproxy_peer *peer, |
| 473 | time_t now, |
| 474 | struct gprs_gb_parse_context *parse_ctx) |
| 475 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 476 | struct gbproxy_link_info *link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 477 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 478 | link_info = gbproxy_get_link_info_ul(peer, parse_ctx); |
Jacob Erlbeck | c68f66c | 2014-09-12 14:15:02 +0200 | [diff] [blame] | 479 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 480 | if (parse_ctx->tlli_enc && parse_ctx->llc) { |
| 481 | uint32_t sgsn_tlli; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 482 | if (!link_info) { |
Jacob Erlbeck | c68f66c | 2014-09-12 14:15:02 +0200 | [diff] [blame] | 483 | LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n", |
| 484 | parse_ctx->tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 485 | link_info = gbproxy_link_info_alloc(peer); |
| 486 | gbproxy_attach_link_info(peer, now, link_info); |
Jacob Erlbeck | c68f66c | 2014-09-12 14:15:02 +0200 | [diff] [blame] | 487 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 488 | /* Setup TLLIs */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 489 | sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 490 | parse_ctx->tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 491 | link_info->sgsn_tlli.current = sgsn_tlli; |
| 492 | link_info->tlli.current = parse_ctx->tlli;; |
| 493 | } else if (!link_info->tlli.current) { |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 494 | /* New TLLI (info found by IMSI or P-TMSI) */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 495 | link_info->tlli.current = parse_ctx->tlli; |
| 496 | link_info->sgsn_tlli.current = |
| 497 | gbproxy_make_sgsn_tlli(peer, link_info, |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 498 | parse_ctx->tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 499 | gbproxy_touch_link_info(peer, link_info, now); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 500 | } else { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 501 | sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, link_info, 0); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 502 | if (!sgsn_tlli) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 503 | sgsn_tlli = gbproxy_make_sgsn_tlli(peer, link_info, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 504 | parse_ctx->tlli); |
| 505 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 506 | gbproxy_validate_tlli(&link_info->tlli, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 507 | parse_ctx->tlli, 0); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 508 | gbproxy_validate_tlli(&link_info->sgsn_tlli, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 509 | sgsn_tlli, 0); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 510 | gbproxy_touch_link_info(peer, link_info, now); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 511 | } |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 512 | } else if (link_info) { |
| 513 | gbproxy_touch_link_info(peer, link_info, now); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 516 | if (parse_ctx->imsi && link_info && link_info->imsi_len == 0) |
| 517 | gbproxy_assign_imsi(peer, link_info, parse_ctx); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 518 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 519 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 522 | struct gbproxy_link_info *gbproxy_update_link_state_dl( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 523 | struct gbproxy_peer *peer, |
| 524 | time_t now, |
| 525 | struct gprs_gb_parse_context *parse_ctx) |
| 526 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 527 | struct gbproxy_link_info *link_info = NULL; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 528 | |
| 529 | if (parse_ctx->tlli_enc) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 530 | link_info = gbproxy_link_info_by_sgsn_tlli( |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 531 | peer, parse_ctx->tlli, parse_ctx->peer_nsei); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 532 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 533 | if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && link_info) { |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 534 | /* A new P-TMSI has been signalled in the message, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 535 | * register new TLLI */ |
| 536 | uint32_t new_sgsn_ptmsi; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 537 | uint32_t new_bss_ptmsi; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 538 | if (!gprs_parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN, |
| 539 | &new_sgsn_ptmsi)) { |
| 540 | LOGP(DGPRS, LOGL_ERROR, |
| 541 | "Failed to parse new TLLI/PTMSI (current is %08x)\n", |
| 542 | parse_ctx->tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 543 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 544 | } |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 545 | new_bss_ptmsi = gbproxy_make_bss_ptmsi(peer, new_sgsn_ptmsi); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 546 | |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 547 | LOGP(DGPRS, LOGL_INFO, |
| 548 | "Got new PTMSI %08x from SGSN, using %08x for BSS\n", |
| 549 | new_sgsn_ptmsi, new_bss_ptmsi); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 550 | /* Setup PTMSIs */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 551 | link_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi; |
| 552 | link_info->tlli.ptmsi = new_bss_ptmsi; |
| 553 | } else if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc && !link_info && |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 554 | !peer->cfg->patch_ptmsi) { |
| 555 | /* A new P-TMSI has been signalled in the message with an unknown |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 556 | * TLLI, create a new link_info */ |
Jacob Erlbeck | c68f66c | 2014-09-12 14:15:02 +0200 | [diff] [blame] | 557 | /* TODO: Add a test case for this branch */ |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 558 | uint32_t new_ptmsi; |
| 559 | if (!gprs_parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN, |
| 560 | &new_ptmsi)) { |
| 561 | LOGP(DGPRS, LOGL_ERROR, |
| 562 | "Failed to parse new PTMSI (TLLI is %08x)\n", |
| 563 | parse_ctx->tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 564 | return link_info; |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | LOGP(DGPRS, LOGL_INFO, |
| 568 | "Adding TLLI %08x to list (SGSN, new P-TMSI is %08x)\n", |
| 569 | parse_ctx->tlli, new_ptmsi); |
| 570 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 571 | link_info = gbproxy_link_info_alloc(peer); |
| 572 | link_info->sgsn_tlli.current = parse_ctx->tlli;; |
| 573 | link_info->tlli.current = parse_ctx->tlli;; |
| 574 | link_info->sgsn_tlli.ptmsi = new_ptmsi; |
| 575 | link_info->tlli.ptmsi = new_ptmsi; |
| 576 | gbproxy_attach_link_info(peer, now, link_info); |
| 577 | } else if (parse_ctx->tlli_enc && parse_ctx->llc && !link_info && |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 578 | !peer->cfg->patch_ptmsi) { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 579 | /* Unknown SGSN TLLI, create a new link_info */ |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 580 | uint32_t new_ptmsi; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 581 | link_info = gbproxy_link_info_alloc(peer); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 582 | LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n", |
| 583 | parse_ctx->tlli); |
| 584 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 585 | gbproxy_attach_link_info(peer, now, link_info); |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 586 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 587 | /* Setup TLLIs */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 588 | link_info->sgsn_tlli.current = parse_ctx->tlli; |
| 589 | link_info->tlli.current = parse_ctx->tlli; |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 590 | |
| 591 | if (!parse_ctx->new_ptmsi_enc) |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 592 | return link_info; |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 593 | /* A new P-TMSI has been signalled in the message */ |
| 594 | |
| 595 | if (!gprs_parse_mi_tmsi(parse_ctx->new_ptmsi_enc, |
| 596 | GSM48_TMSI_LEN, &new_ptmsi)) { |
| 597 | LOGP(DGPRS, LOGL_ERROR, |
| 598 | "Failed to parse new PTMSI (TLLI is %08x)\n", |
| 599 | parse_ctx->tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 600 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 601 | } |
Jacob Erlbeck | 38f1fa5 | 2014-09-05 10:22:27 +0200 | [diff] [blame] | 602 | LOGP(DGPRS, LOGL_INFO, |
| 603 | "Assigning new P-TMSI %08x\n", new_ptmsi); |
| 604 | /* Setup P-TMSIs */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 605 | link_info->sgsn_tlli.ptmsi = new_ptmsi; |
| 606 | link_info->tlli.ptmsi = new_ptmsi; |
| 607 | } else if (parse_ctx->tlli_enc && parse_ctx->llc && link_info) { |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 608 | uint32_t bss_tlli = gbproxy_map_tlli(parse_ctx->tlli, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 609 | link_info, 1); |
| 610 | gbproxy_validate_tlli(&link_info->sgsn_tlli, parse_ctx->tlli, 1); |
| 611 | gbproxy_validate_tlli(&link_info->tlli, bss_tlli, 1); |
| 612 | gbproxy_touch_link_info(peer, link_info, now); |
| 613 | } else if (link_info) { |
| 614 | gbproxy_touch_link_info(peer, link_info, now); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 617 | if (parse_ctx->imsi && link_info && link_info->imsi_len == 0) |
| 618 | gbproxy_assign_imsi(peer, link_info, parse_ctx); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 619 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 620 | return link_info; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 623 | void gbproxy_update_link_state_after( |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 624 | struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 625 | struct gbproxy_link_info *link_info, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 626 | time_t now, |
| 627 | struct gprs_gb_parse_context *parse_ctx) |
| 628 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 629 | if (parse_ctx->invalidate_tlli && link_info) { |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 630 | int keep_info = |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 631 | peer->cfg->keep_link_infos == GBPROX_KEEP_ALWAYS || |
| 632 | (peer->cfg->keep_link_infos == GBPROX_KEEP_REATTACH && |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 633 | parse_ctx->await_reattach) || |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 634 | (peer->cfg->keep_link_infos == GBPROX_KEEP_IDENTIFIED && |
| 635 | link_info->imsi_len > 0); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 636 | if (keep_info) { |
| 637 | LOGP(DGPRS, LOGL_INFO, "Unregistering TLLI %08x\n", |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 638 | link_info->tlli.current); |
| 639 | gbproxy_unregister_link_info(peer, link_info); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 640 | } else { |
| 641 | LOGP(DGPRS, LOGL_INFO, "Removing TLLI %08x from list\n", |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 642 | link_info->tlli.current); |
| 643 | gbproxy_delete_link_info(peer, link_info); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 644 | } |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 645 | } else if (parse_ctx->to_bss && parse_ctx->tlli_enc && |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 646 | parse_ctx->new_ptmsi_enc && link_info) { |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 647 | /* A new PTMSI has been signaled in the message, |
| 648 | * register new TLLI */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 649 | uint32_t new_sgsn_ptmsi = link_info->sgsn_tlli.ptmsi; |
| 650 | uint32_t new_bss_ptmsi = link_info->tlli.ptmsi; |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 651 | uint32_t new_sgsn_tlli; |
| 652 | uint32_t new_bss_tlli = 0; |
| 653 | |
| 654 | new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL); |
| 655 | if (new_bss_ptmsi != GSM_RESERVED_TMSI) |
| 656 | new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL); |
| 657 | LOGP(DGPRS, LOGL_INFO, |
| 658 | "Assigning new TLLI %08x to SGSN, %08x to BSS\n", |
| 659 | new_sgsn_tlli, new_bss_tlli); |
| 660 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 661 | gbproxy_reassign_tlli(&link_info->sgsn_tlli, |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 662 | peer, new_sgsn_tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 663 | gbproxy_reassign_tlli(&link_info->tlli, |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 664 | peer, new_bss_tlli); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 665 | gbproxy_remove_matching_link_infos(peer, link_info); |
Jacob Erlbeck | d7c923a | 2014-09-04 14:53:30 +0200 | [diff] [blame] | 666 | } |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 667 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 668 | gbproxy_remove_stale_link_infos(peer, now); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |