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