Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <arpa/inet.h> |
| 32 | #include <time.h> |
| 33 | |
| 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/core/rate_ctr.h> |
| 37 | #include <osmocom/core/stats.h> |
| 38 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 39 | #include <osmocom/gprs/gprs_ns2.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 40 | #include <osmocom/gprs/gprs_bssgp.h> |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 41 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 42 | |
| 43 | #include <osmocom/gsm/gsm_utils.h> |
| 44 | |
| 45 | #include <osmocom/sgsn/signal.h> |
| 46 | #include <osmocom/sgsn/debug.h> |
| 47 | #include <osmocom/sgsn/gprs_gb_parse.h> |
| 48 | #include <osmocom/sgsn/gb_proxy.h> |
| 49 | |
| 50 | #include <osmocom/sgsn/gprs_llc.h> |
| 51 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
| 52 | #include <osmocom/sgsn/gprs_utils.h> |
| 53 | |
| 54 | extern void *tall_sgsn_ctx; |
| 55 | |
| 56 | static const struct rate_ctr_desc global_ctr_description[] = { |
| 57 | { "inv-bvci", "Invalid BVC Identifier " }, |
| 58 | { "inv-lai", "Invalid Location Area Identifier" }, |
| 59 | { "inv-rai", "Invalid Routing Area Identifier " }, |
| 60 | { "inv-nsei", "No BVC established for NSEI " }, |
| 61 | { "proto-err:bss", "BSSGP protocol error (BSS )" }, |
| 62 | { "proto-err:sgsn", "BSSGP protocol error (SGSN)" }, |
| 63 | { "not-supp:bss", "Feature not supported (BSS )" }, |
| 64 | { "not-supp:sgsn", "Feature not supported (SGSN)" }, |
| 65 | { "restart:sgsn", "Restarted RESET procedure (SGSN)" }, |
| 66 | { "tx-err:sgsn", "NS Transmission error (SGSN)" }, |
| 67 | { "error", "Other error " }, |
| 68 | { "mod-peer-err", "Patch error: no peer " }, |
| 69 | }; |
| 70 | |
| 71 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
| 72 | .group_name_prefix = "gbproxy:global", |
| 73 | .group_description = "GBProxy Global Statistics", |
| 74 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 75 | .ctr_desc = global_ctr_description, |
| 76 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 77 | }; |
| 78 | |
| 79 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer, |
Daniel Willmann | 35f7d33 | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 80 | uint16_t ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 81 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
| 82 | uint16_t ns_bvci, uint16_t sgsn_nsei); |
| 83 | static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info); |
| 84 | |
| 85 | static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei) |
| 86 | { |
| 87 | if (peer->nsei != nsei) { |
| 88 | LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI " |
| 89 | "BVCI=%u via NSEI=%u (expected NSEI=%u)\n", |
| 90 | peer->bvci, nsei, peer->nsei); |
| 91 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | /* strip off the NS header */ |
| 99 | static void strip_ns_hdr(struct msgb *msg) |
| 100 | { |
| 101 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 102 | msgb_pull(msg, strip_len); |
| 103 | } |
| 104 | |
| 105 | /* Transmit Chapter 9.2.10 Identity Request */ |
| 106 | static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type) |
| 107 | { |
| 108 | struct gsm48_hdr *gh; |
| 109 | |
| 110 | id_type &= GSM_MI_TYPE_MASK; |
| 111 | |
| 112 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 113 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 114 | gh->msg_type = GSM48_MT_GMM_ID_REQ; |
| 115 | gh->data[0] = id_type; |
| 116 | } |
| 117 | |
| 118 | /* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */ |
| 119 | static void gprs_put_mo_detach_acc(struct msgb *msg) |
| 120 | { |
| 121 | struct gsm48_hdr *gh; |
| 122 | |
| 123 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 124 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 125 | gh->msg_type = GSM48_MT_GMM_DETACH_ACK; |
| 126 | gh->data[0] = 0; /* no force to standby */ |
| 127 | } |
| 128 | |
| 129 | static void gprs_push_llc_ui(struct msgb *msg, |
| 130 | int is_uplink, unsigned sapi, unsigned nu) |
| 131 | { |
| 132 | const uint8_t e_bit = 0; |
| 133 | const uint8_t pm_bit = 1; |
| 134 | const uint8_t cr_bit = is_uplink ? 0 : 1; |
| 135 | uint8_t *llc; |
| 136 | uint8_t *fcs_field; |
| 137 | uint32_t fcs; |
| 138 | |
| 139 | nu &= 0x01ff; /* 9 Bit */ |
| 140 | |
| 141 | llc = msgb_push(msg, 3); |
| 142 | llc[0] = (cr_bit << 6) | (sapi & 0x0f); |
| 143 | llc[1] = 0xc0 | (nu >> 6); /* UI frame */ |
| 144 | llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1); |
| 145 | |
| 146 | fcs = gprs_llc_fcs(llc, msgb_length(msg)); |
| 147 | fcs_field = msgb_put(msg, 3); |
| 148 | fcs_field[0] = (uint8_t)(fcs >> 0); |
| 149 | fcs_field[1] = (uint8_t)(fcs >> 8); |
| 150 | fcs_field[2] = (uint8_t)(fcs >> 16); |
| 151 | } |
| 152 | |
| 153 | static void gprs_push_bssgp_dl_unitdata(struct msgb *msg, |
| 154 | uint32_t tlli) |
| 155 | { |
| 156 | struct bssgp_ud_hdr *budh; |
| 157 | uint8_t *llc = msgb_data(msg); |
| 158 | size_t llc_size = msgb_length(msg); |
| 159 | const size_t llc_ie_hdr_size = 3; |
| 160 | const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */ |
| 161 | const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */ |
| 162 | |
| 163 | const size_t bssgp_overhead = sizeof(*budh) + |
| 164 | TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size; |
| 165 | uint8_t *ie; |
| 166 | uint32_t tlli_be = htonl(tlli); |
| 167 | |
| 168 | budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead); |
| 169 | |
| 170 | budh->pdu_type = BSSGP_PDUT_DL_UNITDATA; |
| 171 | memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli)); |
| 172 | memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile)); |
| 173 | |
| 174 | ie = budh->data; |
| 175 | tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime); |
| 176 | ie += TVLV_GROSS_LEN(sizeof(lifetime)); |
| 177 | |
| 178 | /* Note: Add alignment before the LLC IE if inserting other IE */ |
| 179 | |
| 180 | *(ie++) = BSSGP_IE_LLC_PDU; |
| 181 | *(ie++) = llc_size / 256; |
| 182 | *(ie++) = llc_size % 256; |
| 183 | |
| 184 | OSMO_ASSERT(ie == llc); |
| 185 | |
| 186 | msgb_bssgph(msg) = (uint8_t *)budh; |
| 187 | msgb_tlli(msg) = tlli; |
| 188 | } |
| 189 | |
| 190 | /* update peer according to the BSS message */ |
| 191 | static void gbprox_update_current_raid(uint8_t *raid_enc, |
| 192 | struct gbproxy_peer *peer, |
| 193 | const char *log_text) |
| 194 | { |
| 195 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 196 | const struct osmo_plmn_id old_plmn = state->local_plmn; |
| 197 | struct gprs_ra_id raid; |
| 198 | |
| 199 | if (!raid_enc) |
| 200 | return; |
| 201 | |
| 202 | gsm48_parse_ra(&raid, raid_enc); |
| 203 | |
| 204 | /* save source side MCC/MNC */ |
| 205 | if (!peer->cfg->core_plmn.mcc || raid.mcc == peer->cfg->core_plmn.mcc) { |
| 206 | state->local_plmn.mcc = 0; |
| 207 | } else { |
| 208 | state->local_plmn.mcc = raid.mcc; |
| 209 | } |
| 210 | |
| 211 | if (!peer->cfg->core_plmn.mnc |
| 212 | || !osmo_mnc_cmp(raid.mnc, raid.mnc_3_digits, |
| 213 | peer->cfg->core_plmn.mnc, peer->cfg->core_plmn.mnc_3_digits)) { |
| 214 | state->local_plmn.mnc = 0; |
| 215 | state->local_plmn.mnc_3_digits = false; |
| 216 | } else { |
| 217 | state->local_plmn.mnc = raid.mnc; |
| 218 | state->local_plmn.mnc_3_digits = raid.mnc_3_digits; |
| 219 | } |
| 220 | |
| 221 | if (osmo_plmn_cmp(&old_plmn, &state->local_plmn)) |
| 222 | LOGP(DGPRS, LOGL_NOTICE, |
| 223 | "Patching RAID %sactivated, msg: %s, " |
| 224 | "local: %s, core: %s\n", |
| 225 | state->local_plmn.mcc || state->local_plmn.mnc ? |
| 226 | "" : "de", |
| 227 | log_text, |
| 228 | osmo_plmn_name(&state->local_plmn), |
| 229 | osmo_plmn_name2(&peer->cfg->core_plmn)); |
| 230 | } |
| 231 | |
| 232 | uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, |
| 233 | uint32_t sgsn_ptmsi) |
| 234 | { |
| 235 | uint32_t bss_ptmsi; |
| 236 | int max_retries = 23, rc = 0; |
| 237 | if (!peer->cfg->patch_ptmsi) { |
| 238 | bss_ptmsi = sgsn_ptmsi; |
| 239 | } else { |
| 240 | do { |
| 241 | rc = osmo_get_rand_id((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi)); |
| 242 | if (rc < 0) { |
| 243 | bss_ptmsi = GSM_RESERVED_TMSI; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | bss_ptmsi = bss_ptmsi | GSM23003_TMSI_SGSN_MASK; |
| 248 | |
| 249 | if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi)) |
| 250 | bss_ptmsi = GSM_RESERVED_TMSI; |
| 251 | } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--); |
| 252 | } |
| 253 | |
| 254 | if (bss_ptmsi == GSM_RESERVED_TMSI) |
| 255 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI: %d (%s)\n", rc, strerror(-rc)); |
| 256 | |
| 257 | return bss_ptmsi; |
| 258 | } |
| 259 | |
| 260 | uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer, |
| 261 | struct gbproxy_link_info *link_info, |
| 262 | uint32_t bss_tlli) |
| 263 | { |
| 264 | uint32_t sgsn_tlli; |
| 265 | int max_retries = 23, rc = 0; |
| 266 | if (!peer->cfg->patch_ptmsi) { |
| 267 | sgsn_tlli = bss_tlli; |
| 268 | } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI && |
| 269 | gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) { |
| 270 | sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi, |
| 271 | TLLI_FOREIGN); |
| 272 | } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI && |
| 273 | gprs_tlli_type(bss_tlli) == TLLI_LOCAL) { |
| 274 | sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi, |
| 275 | TLLI_LOCAL); |
| 276 | } else { |
| 277 | do { |
| 278 | /* create random TLLI, 0b01111xxx... */ |
| 279 | rc = osmo_get_rand_id((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli)); |
| 280 | if (rc < 0) { |
| 281 | sgsn_tlli = 0; |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000; |
| 286 | |
| 287 | if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli)) |
| 288 | sgsn_tlli = 0; |
| 289 | } while (!sgsn_tlli && max_retries--); |
| 290 | } |
| 291 | |
| 292 | if (!sgsn_tlli) |
| 293 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI: %d (%s)\n", rc, strerror(-rc)); |
| 294 | |
| 295 | return sgsn_tlli; |
| 296 | } |
| 297 | |
| 298 | void gbproxy_reset_link(struct gbproxy_link_info *link_info) |
| 299 | { |
| 300 | gbproxy_reset_imsi_acquisition(link_info); |
| 301 | } |
| 302 | |
| 303 | /* Returns != 0 iff IMSI acquisition was in progress */ |
| 304 | static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info) |
| 305 | { |
| 306 | int in_progress = 0; |
| 307 | if (!link_info) |
| 308 | return 0; |
| 309 | |
| 310 | if (link_info->imsi_acq_pending) |
| 311 | in_progress = 1; |
| 312 | |
| 313 | gbproxy_link_info_discard_messages(link_info); |
| 314 | link_info->imsi_acq_pending = false; |
| 315 | |
| 316 | return in_progress; |
| 317 | } |
| 318 | |
| 319 | static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info) |
| 320 | { |
| 321 | gbproxy_restart_imsi_acquisition(link_info); |
| 322 | link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX; |
| 323 | } |
| 324 | |
| 325 | /* Got identity response with IMSI, assuming the request had |
| 326 | * been generated by the gbproxy */ |
| 327 | static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer, |
| 328 | time_t now, |
| 329 | struct gbproxy_link_info* link_info) |
| 330 | { |
| 331 | int rc; |
| 332 | struct msgb *stored_msg; |
| 333 | |
| 334 | /* Patch and flush stored messages towards the SGSN */ |
| 335 | while ((stored_msg = msgb_dequeue_count(&link_info->stored_msgs, |
| 336 | &link_info->stored_msgs_len))) { |
| 337 | struct gprs_gb_parse_context tmp_parse_ctx = {0}; |
| 338 | tmp_parse_ctx.to_bss = 0; |
| 339 | tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg); |
| 340 | int len_change = 0; |
| 341 | |
| 342 | gprs_gb_parse_bssgp(msgb_bssgph(stored_msg), |
| 343 | msgb_bssgp_len(stored_msg), |
| 344 | &tmp_parse_ctx); |
| 345 | gbproxy_patch_bssgp(stored_msg, msgb_bssgph(stored_msg), |
| 346 | msgb_bssgp_len(stored_msg), |
| 347 | peer, link_info, &len_change, |
| 348 | &tmp_parse_ctx); |
| 349 | |
| 350 | rc = gbproxy_update_link_state_after(peer, link_info, now, |
| 351 | &tmp_parse_ctx); |
| 352 | if (rc == 1) { |
| 353 | LOGP(DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n"); |
| 354 | msgb_free(stored_msg); |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | rc = gbprox_relay2sgsn(peer->cfg, stored_msg, |
| 359 | msgb_bvci(stored_msg), link_info->sgsn_nsei); |
| 360 | |
| 361 | if (rc < 0) |
| 362 | LOGP(DLLC, LOGL_ERROR, |
| 363 | "NSEI=%d(BSS) failed to send stored message " |
| 364 | "(%s)\n", |
| 365 | tmp_parse_ctx.peer_nsei, |
| 366 | tmp_parse_ctx.llc_msg_name ? |
| 367 | tmp_parse_ctx.llc_msg_name : "BSSGP"); |
| 368 | msgb_free(stored_msg); |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer, |
| 375 | struct gbproxy_link_info* link_info, |
| 376 | uint16_t bvci, |
| 377 | struct msgb *msg /* Takes msg ownership */) |
| 378 | { |
| 379 | int rc; |
| 380 | |
| 381 | /* Workaround to avoid N(U) collisions and to enable a restart |
| 382 | * of the IMSI acquisition procedure. This will work unless the |
| 383 | * SGSN has an initial V(UT) within [256-32, 256+n_retries] |
| 384 | * (see GSM 04.64, 8.4.2). */ |
| 385 | gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss); |
| 386 | link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512; |
| 387 | |
| 388 | gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 389 | msg->l3h = msg->data; |
| 390 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 391 | rc = gbprox_relay2peer(msg, peer, bvci); |
| 392 | msgb_free(msg); |
| 393 | return rc; |
| 394 | } |
| 395 | |
| 396 | static void gbproxy_acquire_imsi(struct gbproxy_peer *peer, |
| 397 | struct gbproxy_link_info* link_info, |
| 398 | uint16_t bvci) |
| 399 | { |
| 400 | struct msgb *idreq_msg; |
| 401 | |
| 402 | /* Send IDENT REQ */ |
| 403 | idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI"); |
| 404 | gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI); |
| 405 | gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg); |
| 406 | } |
| 407 | |
| 408 | static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer, |
| 409 | struct gbproxy_link_info* link_info, |
| 410 | uint16_t bvci) |
| 411 | { |
| 412 | struct msgb *detacc_msg; |
| 413 | |
| 414 | /* Send DETACH ACC */ |
| 415 | detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC"); |
| 416 | gprs_put_mo_detach_acc(detacc_msg); |
| 417 | gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg); |
| 418 | } |
| 419 | |
| 420 | /* Return != 0 iff msg still needs to be processed */ |
| 421 | static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer, |
| 422 | struct msgb *msg, |
| 423 | time_t now, |
| 424 | struct gbproxy_link_info* link_info, |
| 425 | struct gprs_gb_parse_context *parse_ctx) |
| 426 | { |
| 427 | struct msgb *stored_msg; |
| 428 | |
| 429 | if (!link_info) |
| 430 | return 1; |
| 431 | |
| 432 | if (!link_info->imsi_acq_pending && link_info->imsi_len > 0) |
| 433 | return 1; |
| 434 | |
| 435 | if (parse_ctx->g48_hdr) |
| 436 | switch (parse_ctx->g48_hdr->msg_type) |
| 437 | { |
| 438 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 439 | case GSM48_MT_GMM_ATTACH_REQ: |
| 440 | if (gbproxy_restart_imsi_acquisition(link_info)) { |
| 441 | LOGP(DLLC, LOGL_INFO, |
| 442 | "NSEI=%d(BSS) IMSI acquisition was in progress " |
| 443 | "when receiving an %s.\n", |
| 444 | msgb_nsei(msg), parse_ctx->llc_msg_name); |
| 445 | } |
| 446 | break; |
| 447 | case GSM48_MT_GMM_DETACH_REQ: |
| 448 | /* Nothing has been sent to the SGSN yet */ |
| 449 | if (link_info->imsi_acq_pending) { |
| 450 | LOGP(DLLC, LOGL_INFO, |
| 451 | "NSEI=%d(BSS) IMSI acquisition was in progress " |
| 452 | "when receiving a DETACH_REQ.\n", |
| 453 | msgb_nsei(msg)); |
| 454 | } |
| 455 | if (!parse_ctx->invalidate_tlli) { |
| 456 | LOGP(DLLC, LOGL_INFO, |
| 457 | "NSEI=%d(BSS) IMSI not yet acquired, " |
| 458 | "faking a DETACH_ACC.\n", |
| 459 | msgb_nsei(msg)); |
| 460 | gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg)); |
| 461 | parse_ctx->invalidate_tlli = 1; |
| 462 | } |
| 463 | gbproxy_reset_imsi_acquisition(link_info); |
| 464 | gbproxy_update_link_state_after(peer, link_info, now, |
| 465 | parse_ctx); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | if (link_info->imsi_acq_pending && link_info->imsi_len > 0) { |
| 470 | int is_ident_resp = |
| 471 | parse_ctx->g48_hdr && |
| 472 | gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS && |
| 473 | gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP; |
| 474 | |
| 475 | LOGP(DLLC, LOGL_DEBUG, |
| 476 | "NSEI=%d(BSS) IMSI acquisition succeeded, " |
| 477 | "flushing stored messages\n", |
| 478 | msgb_nsei(msg)); |
| 479 | /* The IMSI is now available. If flushing the messages fails, |
| 480 | * then link_info has been deleted and we should return |
| 481 | * immediately. */ |
| 482 | if (gbproxy_flush_stored_messages(peer, now, link_info) < 0) |
| 483 | return 0; |
| 484 | |
| 485 | gbproxy_reset_imsi_acquisition(link_info); |
| 486 | |
| 487 | /* This message is most probably the response to the ident |
| 488 | * request sent by gbproxy_acquire_imsi(). Don't forward it to |
| 489 | * the SGSN. */ |
| 490 | return !is_ident_resp; |
| 491 | } |
| 492 | |
| 493 | /* The message cannot be processed since the IMSI is still missing */ |
| 494 | |
| 495 | /* If queue is getting too large, drop oldest msgb before adding new one */ |
| 496 | if (peer->cfg->stored_msgs_max_len > 0) { |
| 497 | int exceeded_max_len = link_info->stored_msgs_len |
| 498 | + 1 - peer->cfg->stored_msgs_max_len; |
| 499 | |
| 500 | for (; exceeded_max_len > 0; exceeded_max_len--) { |
| 501 | struct msgb *msgb_drop; |
| 502 | msgb_drop = msgb_dequeue_count(&link_info->stored_msgs, |
| 503 | &link_info->stored_msgs_len); |
| 504 | LOGP(DLLC, LOGL_INFO, |
| 505 | "NSEI=%d(BSS) Dropping stored msgb from list " |
| 506 | "(!acq imsi, length %d, max_len exceeded)\n", |
| 507 | msgb_nsei(msgb_drop), link_info->stored_msgs_len); |
| 508 | |
| 509 | msgb_free(msgb_drop); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* Enqueue unpatched messages */ |
| 514 | LOGP(DLLC, LOGL_INFO, |
| 515 | "NSEI=%d(BSS) IMSI acquisition in progress, " |
| 516 | "storing message (%s)\n", |
| 517 | msgb_nsei(msg), |
| 518 | parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP"); |
| 519 | |
| 520 | stored_msg = bssgp_msgb_copy(msg, "process_bssgp_ul"); |
| 521 | msgb_enqueue_count(&link_info->stored_msgs, stored_msg, |
| 522 | &link_info->stored_msgs_len); |
| 523 | |
| 524 | if (!link_info->imsi_acq_pending) { |
| 525 | LOGP(DLLC, LOGL_INFO, |
| 526 | "NSEI=%d(BSS) IMSI is required but not available, " |
| 527 | "initiating identification procedure (%s)\n", |
| 528 | msgb_nsei(msg), |
| 529 | parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP"); |
| 530 | |
| 531 | gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg)); |
| 532 | |
| 533 | /* There is no explicit retransmission handling, the |
| 534 | * implementation relies on the MS doing proper retransmissions |
| 535 | * of the triggering message instead */ |
| 536 | |
| 537 | link_info->imsi_acq_pending = true; |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg, |
| 544 | struct msgb *msg, |
| 545 | struct gprs_gb_parse_context *parse_ctx) |
| 546 | { |
| 547 | struct gbproxy_peer *peer = NULL; |
| 548 | |
| 549 | if (msgb_bvci(msg) >= 2) |
| 550 | peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg)); |
| 551 | |
| 552 | if (!peer && !parse_ctx->to_bss) |
| 553 | peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg)); |
| 554 | |
| 555 | if (!peer) |
| 556 | peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp); |
| 557 | |
| 558 | if (!peer) { |
| 559 | LOGP(DLLC, LOGL_INFO, |
| 560 | "NSEI=%d(%s) patching: didn't find peer for message, " |
| 561 | "PDU %d\n", |
| 562 | msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN", |
| 563 | parse_ctx->pdu_type); |
| 564 | /* Increment counter */ |
| 565 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]); |
| 566 | } |
| 567 | return peer; |
| 568 | } |
| 569 | |
| 570 | /* patch BSSGP message */ |
| 571 | static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg, |
| 572 | struct msgb *msg, |
| 573 | struct gbproxy_peer *peer) |
| 574 | { |
| 575 | struct gprs_gb_parse_context parse_ctx = {0}; |
| 576 | int rc; |
| 577 | int len_change = 0; |
| 578 | time_t now; |
| 579 | struct timespec ts = {0,}; |
| 580 | struct gbproxy_link_info *link_info = NULL; |
| 581 | uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei; |
| 582 | |
| 583 | if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn && |
| 584 | !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2) |
| 585 | return 1; |
| 586 | |
| 587 | parse_ctx.to_bss = 0; |
| 588 | parse_ctx.peer_nsei = msgb_nsei(msg); |
| 589 | |
| 590 | /* Parse BSSGP/LLC */ |
| 591 | rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 592 | &parse_ctx); |
| 593 | |
| 594 | if (!rc && !parse_ctx.need_decryption) { |
| 595 | LOGP(DGPRS, LOGL_ERROR, |
| 596 | "NSEI=%u(BSS) patching: failed to parse invalid %s message\n", |
| 597 | msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA")); |
| 598 | gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA"); |
| 599 | LOGP(DGPRS, LOGL_NOTICE, |
| 600 | "NSEI=%u(BSS) invalid message was: %s\n", |
| 601 | msgb_nsei(msg), msgb_hexdump(msg)); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /* Get peer */ |
| 606 | if (!peer) |
| 607 | peer = gbproxy_find_peer(cfg, msg, &parse_ctx); |
| 608 | |
| 609 | if (!peer) |
| 610 | return 0; |
| 611 | |
| 612 | |
| 613 | osmo_clock_gettime(CLOCK_MONOTONIC, &ts); |
| 614 | now = ts.tv_sec; |
| 615 | |
| 616 | gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer, |
| 617 | parse_ctx.llc_msg_name); |
| 618 | |
| 619 | gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA"); |
| 620 | |
| 621 | link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx); |
| 622 | |
| 623 | if (parse_ctx.g48_hdr) { |
| 624 | switch (parse_ctx.g48_hdr->msg_type) { |
| 625 | case GSM48_MT_GMM_ATTACH_REQ: |
| 626 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]); |
| 627 | break; |
| 628 | case GSM48_MT_GMM_DETACH_REQ: |
| 629 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]); |
| 630 | break; |
| 631 | case GSM48_MT_GMM_ATTACH_COMPL: |
| 632 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]); |
| 633 | break; |
| 634 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 635 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]); |
| 636 | break; |
| 637 | case GSM48_MT_GMM_RA_UPD_COMPL: |
| 638 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]); |
| 639 | break; |
| 640 | case GSM48_MT_GMM_STATUS: |
| 641 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]); |
| 642 | break; |
| 643 | case GSM48_MT_GSM_ACT_PDP_REQ: |
| 644 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]); |
| 645 | break; |
| 646 | case GSM48_MT_GSM_DEACT_PDP_REQ: |
| 647 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]); |
| 648 | break; |
| 649 | |
| 650 | default: |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if (link_info && cfg->route_to_sgsn2) { |
| 656 | if (cfg->acquire_imsi && link_info->imsi_len == 0) |
| 657 | sgsn_nsei = 0xffff; |
| 658 | else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING, |
| 659 | link_info)) |
| 660 | sgsn_nsei = cfg->nsip_sgsn2_nsei; |
| 661 | } |
| 662 | |
| 663 | if (link_info) |
| 664 | link_info->sgsn_nsei = sgsn_nsei; |
| 665 | |
| 666 | /* Handle IMSI acquisition */ |
| 667 | if (cfg->acquire_imsi) { |
| 668 | rc = gbproxy_imsi_acquisition(peer, msg, now, link_info, |
| 669 | &parse_ctx); |
| 670 | if (rc <= 0) |
| 671 | return rc; |
| 672 | } |
| 673 | |
| 674 | gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 675 | peer, link_info, &len_change, &parse_ctx); |
| 676 | |
| 677 | gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx); |
| 678 | |
| 679 | if (sgsn_nsei != cfg->nsip_sgsn_nsei) { |
| 680 | /* Send message directly to the selected SGSN */ |
| 681 | rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei); |
| 682 | /* Don't let the calling code handle the transmission */ |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | return 1; |
| 687 | } |
| 688 | |
| 689 | /* patch BSSGP message to use core_plmn.mcc/mnc on the SGSN side */ |
| 690 | static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg, |
| 691 | struct msgb *msg, |
| 692 | struct gbproxy_peer *peer) |
| 693 | { |
| 694 | struct gprs_gb_parse_context parse_ctx = {0}; |
| 695 | int rc; |
| 696 | int len_change = 0; |
| 697 | time_t now; |
| 698 | struct timespec ts = {0,}; |
| 699 | struct gbproxy_link_info *link_info = NULL; |
| 700 | |
| 701 | if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn && |
| 702 | !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2) |
| 703 | return; |
| 704 | |
| 705 | parse_ctx.to_bss = 1; |
| 706 | parse_ctx.peer_nsei = msgb_nsei(msg); |
| 707 | |
| 708 | rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 709 | &parse_ctx); |
| 710 | |
| 711 | if (!rc && !parse_ctx.need_decryption) { |
| 712 | LOGP(DGPRS, LOGL_ERROR, |
| 713 | "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n", |
| 714 | msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA")); |
| 715 | gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA"); |
| 716 | LOGP(DGPRS, LOGL_NOTICE, |
| 717 | "NSEI=%u(SGSN) invalid message was: %s\n", |
| 718 | msgb_nsei(msg), msgb_hexdump(msg)); |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | /* Get peer */ |
| 723 | if (!peer) |
| 724 | peer = gbproxy_find_peer(cfg, msg, &parse_ctx); |
| 725 | |
| 726 | if (!peer) |
| 727 | return; |
| 728 | |
| 729 | osmo_clock_gettime(CLOCK_MONOTONIC, &ts); |
| 730 | now = ts.tv_sec; |
| 731 | |
| 732 | if (parse_ctx.g48_hdr) { |
| 733 | switch (parse_ctx.g48_hdr->msg_type) { |
| 734 | case GSM48_MT_GMM_ATTACH_ACK: |
| 735 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]); |
| 736 | break; |
| 737 | case GSM48_MT_GMM_ATTACH_REJ: |
| 738 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]); |
| 739 | break; |
| 740 | case GSM48_MT_GMM_DETACH_ACK: |
| 741 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]); |
| 742 | break; |
| 743 | case GSM48_MT_GMM_RA_UPD_ACK: |
| 744 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]); |
| 745 | break; |
| 746 | case GSM48_MT_GMM_RA_UPD_REJ: |
| 747 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]); |
| 748 | break; |
| 749 | case GSM48_MT_GMM_STATUS: |
| 750 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]); |
| 751 | break; |
| 752 | case GSM48_MT_GSM_ACT_PDP_ACK: |
| 753 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]); |
| 754 | break; |
| 755 | case GSM48_MT_GSM_ACT_PDP_REJ: |
| 756 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]); |
| 757 | break; |
| 758 | case GSM48_MT_GSM_DEACT_PDP_ACK: |
| 759 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]); |
| 760 | break; |
| 761 | |
| 762 | default: |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA"); |
| 768 | |
| 769 | link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx); |
| 770 | |
| 771 | gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 772 | peer, link_info, &len_change, &parse_ctx); |
| 773 | |
| 774 | gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx); |
| 775 | |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | /* feed a message down the NS-VC associated with the specified peer */ |
| 780 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
| 781 | uint16_t ns_bvci, uint16_t sgsn_nsei) |
| 782 | { |
| 783 | /* create a copy of the message so the old one can |
| 784 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 785 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 786 | struct osmo_gprs_ns2_prim nsp = {}; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 787 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn"); |
| 788 | int rc; |
| 789 | |
| 790 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
| 791 | msgb_nsei(msg), ns_bvci, sgsn_nsei); |
| 792 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 793 | nsp.bvci = ns_bvci; |
| 794 | nsp.nsei = sgsn_nsei; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 795 | |
| 796 | strip_ns_hdr(msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 797 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 798 | PRIM_OP_REQUEST, msg); |
| 799 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 800 | if (rc < 0) |
| 801 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 802 | return rc; |
| 803 | } |
| 804 | |
| 805 | /* feed a message down the NS-VC associated with the specified peer */ |
| 806 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer, |
Daniel Willmann | 35f7d33 | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 807 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 808 | { |
| 809 | /* create a copy of the message so the old one can |
| 810 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 811 | struct gprs_ns2_inst *nsi = peer->cfg->nsi; |
| 812 | struct osmo_gprs_ns2_prim nsp = {}; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 813 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2peer"); |
| 814 | int rc; |
| 815 | |
| 816 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
| 817 | msgb_nsei(msg), ns_bvci, peer->nsei); |
| 818 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 819 | nsp.bvci = ns_bvci; |
| 820 | nsp.nsei = peer->nsei; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 821 | |
| 822 | /* Strip the old NS header, it will be replaced with a new one */ |
| 823 | strip_ns_hdr(msg); |
| 824 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 825 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 826 | PRIM_OP_REQUEST, msg); |
| 827 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 828 | if (rc < 0) |
| 829 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
| 830 | |
| 831 | return rc; |
| 832 | } |
| 833 | |
| 834 | static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type) |
| 835 | { |
| 836 | struct gbproxy_peer *peer; |
| 837 | |
| 838 | peer = gbproxy_peer_by_bvci(cfg, ptp_bvci); |
| 839 | if (!peer) { |
| 840 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 841 | ptp_bvci); |
| 842 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 843 | return -ENOENT; |
| 844 | } |
| 845 | |
| 846 | switch (pdu_type) { |
| 847 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 848 | peer->blocked = true; |
| 849 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
| 850 | break; |
| 851 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 852 | peer->blocked = false; |
| 853 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
| 854 | break; |
| 855 | default: |
| 856 | break; |
| 857 | } |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 862 | * in the NS hdr */ |
| 863 | static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci, |
| 864 | uint16_t ns_bvci) |
| 865 | { |
| 866 | struct gbproxy_peer *peer; |
| 867 | |
| 868 | peer = gbproxy_peer_by_bvci(cfg, ptp_bvci); |
| 869 | if (!peer) { |
| 870 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 871 | ptp_bvci); |
| 872 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 873 | return -ENOENT; |
| 874 | } |
| 875 | |
| 876 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 877 | } |
| 878 | |
| 879 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 880 | { |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | /* Receive an incoming PTP message from a BSS-side NS-VC */ |
| 885 | static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg, |
| 886 | struct msgb *msg, uint16_t nsei, |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 887 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 888 | { |
| 889 | struct gbproxy_peer *peer; |
| 890 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 891 | uint8_t pdu_type = bgph->pdu_type; |
| 892 | int rc; |
| 893 | |
| 894 | peer = gbproxy_peer_by_bvci(cfg, ns_bvci); |
| 895 | if (!peer) { |
| 896 | LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for " |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 897 | "BVCI=%u for PTP message from NSEI=%u (BSS), " |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 898 | "discarding message\n", |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 899 | ns_bvci, nsei); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 900 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 901 | &ns_bvci, msg); |
| 902 | } |
| 903 | |
| 904 | check_peer_nsei(peer, nsei); |
| 905 | |
| 906 | rc = gbprox_process_bssgp_ul(cfg, msg, peer); |
| 907 | if (!rc) |
| 908 | return 0; |
| 909 | |
| 910 | switch (pdu_type) { |
| 911 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
| 912 | if (!cfg->route_to_sgsn2) |
| 913 | break; |
| 914 | |
| 915 | /* Send a copy to the secondary SGSN */ |
| 916 | gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei); |
| 917 | break; |
| 918 | default: |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei); |
| 924 | } |
| 925 | |
| 926 | /* Receive an incoming PTP message from a SGSN-side NS-VC */ |
| 927 | static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg, |
| 928 | struct msgb *msg, uint16_t nsei, |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 929 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 930 | { |
| 931 | struct gbproxy_peer *peer; |
| 932 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 933 | uint8_t pdu_type = bgph->pdu_type; |
| 934 | |
| 935 | peer = gbproxy_peer_by_bvci(cfg, ns_bvci); |
| 936 | |
| 937 | /* Send status messages before patching */ |
| 938 | |
| 939 | if (!peer) { |
| 940 | LOGP(DGPRS, LOGL_INFO, "Didn't find peer for " |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 941 | "BVCI=%u for message from NSEI=%u (SGSN)\n", |
| 942 | ns_bvci, nsei); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 943 | rate_ctr_inc(&cfg->ctrg-> |
| 944 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 945 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 946 | &ns_bvci, msg); |
| 947 | } |
| 948 | |
| 949 | if (peer->blocked) { |
| 950 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 951 | "blocked BVCI=%u via NSEI=%u\n", |
| 952 | ns_bvci, nsei); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 953 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
| 954 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg); |
| 955 | } |
| 956 | |
| 957 | switch (pdu_type) { |
| 958 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 959 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 960 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 961 | if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei) |
| 962 | /* Hide ACKs from the secondary SGSN, the primary SGSN |
| 963 | * is responsible to send them. */ |
| 964 | return 0; |
| 965 | break; |
| 966 | default: |
| 967 | break; |
| 968 | } |
| 969 | |
| 970 | /* Optionally patch the message */ |
| 971 | gbprox_process_bssgp_dl(cfg, msg, peer); |
| 972 | |
| 973 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 974 | } |
| 975 | |
| 976 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
| 977 | static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg, |
| 978 | struct msgb *msg, uint16_t nsei, |
| 979 | uint16_t ns_bvci) |
| 980 | { |
| 981 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 982 | struct tlv_parsed tp; |
| 983 | uint8_t pdu_type = bgph->pdu_type; |
| 984 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 985 | struct gbproxy_peer *from_peer = NULL; |
| 986 | struct gprs_ra_id raid; |
| 987 | int copy_to_sgsn2 = 0; |
| 988 | int rc; |
| 989 | |
| 990 | if (ns_bvci != 0 && ns_bvci != 1) { |
| 991 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
| 992 | nsei, ns_bvci); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | |
| 996 | /* we actually should never see those two for BVCI == 0, but double-check |
| 997 | * just to make sure */ |
| 998 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 999 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 1000 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
| 1001 | "signalling\n", nsei); |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 1006 | |
| 1007 | switch (pdu_type) { |
| 1008 | case BSSGP_PDUT_SUSPEND: |
| 1009 | case BSSGP_PDUT_RESUME: |
| 1010 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
| 1011 | * establishes a relationsip between BVCI/peer and the routeing |
| 1012 | * area identification. The snooped information is then used |
| 1013 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 1014 | * BSSGP */ |
| 1015 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 1016 | goto err_mand_ie; |
| 1017 | from_peer = gbproxy_peer_by_nsei(cfg, nsei); |
| 1018 | if (!from_peer) |
| 1019 | goto err_no_peer; |
| 1020 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 1021 | sizeof(from_peer->ra)); |
| 1022 | gsm48_parse_ra(&raid, from_peer->ra); |
| 1023 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
| 1024 | "RAI snooping: RAI %s behind BVCI=%u\n", |
| 1025 | nsei, osmo_rai_name(&raid), from_peer->bvci); |
| 1026 | /* FIXME: This only supports one BSS per RA */ |
| 1027 | break; |
| 1028 | case BSSGP_PDUT_BVC_RESET: |
| 1029 | /* If we receive a BVC reset on the signalling endpoint, we |
| 1030 | * don't want the SGSN to reset, as the signalling endpoint |
| 1031 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 1032 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
| 1033 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1034 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
| 1035 | nsei, bvci); |
| 1036 | if (bvci == 0) { |
| 1037 | /* FIXME: only do this if SGSN is alive! */ |
| 1038 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
| 1039 | "BVC RESET ACK of BVCI=0\n", nsei); |
| 1040 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
| 1041 | nsei, 0, ns_bvci); |
| 1042 | } |
| 1043 | from_peer = gbproxy_peer_by_bvci(cfg, bvci); |
| 1044 | if (!from_peer) { |
| 1045 | /* if a PTP-BVC is reset, and we don't know that |
| 1046 | * PTP-BVCI yet, we should allocate a new peer */ |
| 1047 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for BVCI=%u via NSEI=%u\n", bvci, nsei); |
| 1048 | from_peer = gbproxy_peer_alloc(cfg, bvci); |
| 1049 | OSMO_ASSERT(from_peer); |
| 1050 | from_peer->nsei = nsei; |
| 1051 | } |
| 1052 | |
| 1053 | if (!check_peer_nsei(from_peer, nsei)) |
| 1054 | from_peer->nsei = nsei; |
| 1055 | |
| 1056 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 1057 | struct gprs_ra_id raid; |
| 1058 | /* We have a Cell Identifier present in this |
| 1059 | * PDU, this means we can extend our local |
| 1060 | * state information about this particular cell |
| 1061 | * */ |
| 1062 | memcpy(from_peer->ra, |
| 1063 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 1064 | sizeof(from_peer->ra)); |
| 1065 | gsm48_parse_ra(&raid, from_peer->ra); |
| 1066 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u Cell ID %s\n", |
| 1067 | nsei, bvci, osmo_rai_name(&raid)); |
| 1068 | } |
| 1069 | if (cfg->route_to_sgsn2) |
| 1070 | copy_to_sgsn2 = 1; |
| 1071 | } |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 1076 | * SGSN */ |
| 1077 | rc = gbprox_process_bssgp_ul(cfg, msg, from_peer); |
| 1078 | if (!rc) |
| 1079 | return 0; |
| 1080 | |
| 1081 | if (copy_to_sgsn2) |
| 1082 | gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei); |
| 1083 | |
| 1084 | return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei); |
| 1085 | err_no_peer: |
| 1086 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
| 1087 | nsei); |
| 1088 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
| 1089 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
| 1090 | err_mand_ie: |
| 1091 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
| 1092 | nsei); |
| 1093 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
| 1094 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 1095 | } |
| 1096 | |
| 1097 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
| 1098 | static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp, |
| 1099 | uint32_t nsei, uint16_t ns_bvci) |
| 1100 | { |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame^] | 1101 | struct gbproxy_peer *peer; |
| 1102 | unsigned int n_peers = 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1103 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
| 1104 | |
| 1105 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
| 1106 | nsei); |
| 1107 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 1108 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1109 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame^] | 1110 | peer = gbproxy_peer_by_bvci(cfg, bvci); |
| 1111 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", bvci); |
| 1112 | if (!peer) { |
| 1113 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP PAGING: " |
| 1114 | "unable to route: BVCI=%u unknown\n", nsei, bvci); |
| 1115 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 1116 | return -EINVAL; |
| 1117 | } |
| 1118 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1119 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1120 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame^] | 1121 | /* iterate over all peers and dispatch the paging to each matching one */ |
| 1122 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 1123 | if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) { |
| 1124 | LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", peer->bvci); |
| 1125 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 1126 | n_peers++; |
| 1127 | } |
| 1128 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1129 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1130 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame^] | 1131 | /* iterate over all peers and dispatch the paging to each matching one */ |
| 1132 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 1133 | if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) { |
| 1134 | LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", peer->bvci); |
| 1135 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 1136 | n_peers++; |
| 1137 | } |
| 1138 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1139 | } else |
| 1140 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 1141 | |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame^] | 1142 | if (n_peers == 0) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1143 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
| 1144 | "unable to route, missing IE\n", nsei); |
| 1145 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 1146 | return -EINVAL; |
| 1147 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame^] | 1148 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /* Receive an incoming BVC-RESET message from the SGSN */ |
| 1152 | static int rx_reset_from_sgsn(struct gbproxy_config *cfg, |
| 1153 | struct msgb *orig_msg, |
| 1154 | struct msgb *msg, struct tlv_parsed *tp, |
| 1155 | uint32_t nsei, uint16_t ns_bvci) |
| 1156 | { |
| 1157 | struct gbproxy_peer *peer; |
| 1158 | uint16_t ptp_bvci; |
| 1159 | |
| 1160 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 1161 | rate_ctr_inc(&cfg->ctrg-> |
| 1162 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
| 1163 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 1164 | NULL, orig_msg); |
| 1165 | } |
| 1166 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 1167 | |
| 1168 | if (ptp_bvci >= 2) { |
| 1169 | /* A reset for a PTP BVC was received, forward it to its |
| 1170 | * respective peer */ |
| 1171 | peer = gbproxy_peer_by_bvci(cfg, ptp_bvci); |
| 1172 | if (!peer) { |
| 1173 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
| 1174 | nsei, ptp_bvci); |
| 1175 | rate_ctr_inc(&cfg->ctrg-> |
| 1176 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 1177 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 1178 | &ptp_bvci, orig_msg); |
| 1179 | } |
| 1180 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 1181 | } |
| 1182 | |
| 1183 | /* A reset for the Signalling entity has been received |
| 1184 | * from the SGSN. As the signalling BVCI is shared |
| 1185 | * among all the BSS's that we multiplex, it needs to |
| 1186 | * be relayed */ |
| 1187 | llist_for_each_entry(peer, &cfg->bts_peers, list) |
| 1188 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
| 1194 | static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg, |
| 1195 | struct msgb *orig_msg, uint32_t nsei, |
| 1196 | uint16_t ns_bvci) |
| 1197 | { |
| 1198 | struct bssgp_normal_hdr *bgph = |
| 1199 | (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg); |
| 1200 | struct tlv_parsed tp; |
| 1201 | uint8_t pdu_type = bgph->pdu_type; |
| 1202 | int data_len; |
| 1203 | struct gbproxy_peer *peer; |
| 1204 | uint16_t bvci; |
| 1205 | struct msgb *msg; |
| 1206 | int rc = 0; |
| 1207 | int cause; |
| 1208 | |
| 1209 | if (ns_bvci != 0 && ns_bvci != 1) { |
| 1210 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
| 1211 | "signalling\n", nsei, ns_bvci); |
| 1212 | /* FIXME: Send proper error message */ |
| 1213 | return -EINVAL; |
| 1214 | } |
| 1215 | |
| 1216 | /* we actually should never see those two for BVCI == 0, but double-check |
| 1217 | * just to make sure */ |
| 1218 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 1219 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 1220 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
| 1221 | "signalling\n", nsei); |
| 1222 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
| 1223 | } |
| 1224 | |
| 1225 | msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn"); |
| 1226 | gbprox_process_bssgp_dl(cfg, msg, NULL); |
| 1227 | /* Update message info */ |
| 1228 | bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 1229 | data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph); |
| 1230 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 1231 | |
| 1232 | switch (pdu_type) { |
| 1233 | case BSSGP_PDUT_BVC_RESET: |
| 1234 | rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci); |
| 1235 | break; |
| 1236 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 1237 | if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei) |
| 1238 | break; |
Daniel Willmann | 8489e7a | 2020-11-03 21:12:42 +0100 | [diff] [blame] | 1239 | /* simple case: BVCI IE is mandatory */ |
| 1240 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1241 | goto err_mand_ie; |
| 1242 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1243 | if (bvci == BVCI_SIGNALLING) { |
| 1244 | /* TODO: Reset all PTP BVCIs */ |
| 1245 | } else { |
| 1246 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
| 1247 | } |
| 1248 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1249 | case BSSGP_PDUT_FLUSH_LL: |
| 1250 | /* simple case: BVCI IE is mandatory */ |
| 1251 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1252 | goto err_mand_ie; |
| 1253 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1254 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
| 1255 | break; |
| 1256 | case BSSGP_PDUT_PAGING_PS: |
| 1257 | case BSSGP_PDUT_PAGING_CS: |
| 1258 | /* process the paging request (LAI/RAI lookup) */ |
| 1259 | rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci); |
| 1260 | break; |
| 1261 | case BSSGP_PDUT_STATUS: |
| 1262 | /* Some exception has occurred */ |
| 1263 | LOGP(DGPRS, LOGL_NOTICE, |
| 1264 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
| 1265 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 1266 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 1267 | goto err_mand_ie; |
| 1268 | } |
| 1269 | cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE); |
| 1270 | LOGPC(DGPRS, LOGL_NOTICE, |
| 1271 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 1272 | bssgp_cause_str(cause)); |
| 1273 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
| 1274 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1275 | LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci); |
| 1276 | |
| 1277 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI) |
| 1278 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
| 1279 | } else |
| 1280 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 1281 | break; |
| 1282 | /* those only exist in the SGSN -> BSS direction */ |
| 1283 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1284 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1285 | case BSSGP_PDUT_RESUME_ACK: |
| 1286 | case BSSGP_PDUT_RESUME_NACK: |
| 1287 | /* RAI IE is mandatory */ |
| 1288 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 1289 | goto err_mand_ie; |
| 1290 | peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
| 1291 | if (!peer) |
| 1292 | goto err_no_peer; |
| 1293 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
| 1294 | break; |
| 1295 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1296 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1297 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1298 | goto err_mand_ie; |
| 1299 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1300 | if (bvci == 0) { |
| 1301 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
| 1302 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
| 1303 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 1304 | /* should we send STATUS ? */ |
| 1305 | rate_ctr_inc(&cfg->ctrg-> |
| 1306 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 1307 | } else { |
| 1308 | /* Mark BVC as (un)blocked */ |
| 1309 | block_unblock_peer(cfg, bvci, pdu_type); |
| 1310 | } |
| 1311 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
| 1312 | break; |
| 1313 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
| 1314 | LOGP(DGPRS, LOGL_ERROR, |
| 1315 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
| 1316 | rate_ctr_inc(&cfg->ctrg-> |
| 1317 | ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]); |
| 1318 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg); |
| 1319 | break; |
| 1320 | default: |
| 1321 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type %s not supported\n", bssgp_pdu_str(pdu_type)); |
| 1322 | rate_ctr_inc(&cfg->ctrg-> |
| 1323 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
| 1324 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
| 1325 | break; |
| 1326 | } |
| 1327 | |
| 1328 | msgb_free(msg); |
| 1329 | |
| 1330 | return rc; |
| 1331 | err_mand_ie: |
| 1332 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
| 1333 | nsei); |
| 1334 | rate_ctr_inc(&cfg->ctrg-> |
| 1335 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
| 1336 | msgb_free(msg); |
| 1337 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg); |
| 1338 | err_no_peer: |
| 1339 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n", |
| 1340 | nsei); |
| 1341 | rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
| 1342 | msgb_free(msg); |
| 1343 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg); |
| 1344 | } |
| 1345 | |
| 1346 | static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 1347 | { |
| 1348 | return nsei == cfg->nsip_sgsn_nsei || |
| 1349 | (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei); |
| 1350 | } |
| 1351 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1352 | int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1353 | { |
| 1354 | int rc; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1355 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 1356 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 1357 | struct osmo_gprs_ns2_prim nsp = {}; |
| 1358 | |
| 1359 | nsp.bvci = msgb_bvci(msg); |
| 1360 | nsp.nsei = msgb_nsei(msg); |
| 1361 | |
| 1362 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg); |
| 1363 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
| 1364 | |
| 1365 | return rc; |
| 1366 | } |
| 1367 | |
| 1368 | /* Main input function for Gb proxy */ |
| 1369 | int gbprox_rcvmsg(void *ctx, struct msgb *msg) |
| 1370 | { |
| 1371 | int rc; |
| 1372 | uint16_t nsei = msgb_nsei(msg); |
| 1373 | uint16_t ns_bvci = msgb_bvci(msg); |
| 1374 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 1375 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1376 | int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei); |
| 1377 | |
| 1378 | /* Only BVCI=0 messages need special treatment */ |
| 1379 | if (ns_bvci == 0 || ns_bvci == 1) { |
| 1380 | if (remote_end_is_sgsn) |
| 1381 | rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci); |
| 1382 | else |
| 1383 | rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci); |
| 1384 | } else { |
| 1385 | /* All other BVCI are PTP */ |
| 1386 | if (remote_end_is_sgsn) |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1387 | rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1388 | ns_bvci); |
| 1389 | else |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1390 | rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1391 | ns_bvci); |
| 1392 | } |
| 1393 | |
| 1394 | return rc; |
| 1395 | } |
| 1396 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1397 | /* TODO: What about handling: |
| 1398 | * NS_AFF_CAUSE_VC_FAILURE, |
| 1399 | NS_AFF_CAUSE_VC_RECOVERY, |
| 1400 | NS_AFF_CAUSE_FAILURE, |
| 1401 | NS_AFF_CAUSE_RECOVERY, |
| 1402 | osmocom own causes |
| 1403 | NS_AFF_CAUSE_SNS_CONFIGURED, |
| 1404 | NS_AFF_CAUSE_SNS_FAILURE, |
| 1405 | */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1406 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1407 | void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1408 | { |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1409 | /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, peer->bvci, 0); |
| 1410 | * TODO: sgsn nsei available/unavailable |
| 1411 | */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1412 | struct gbproxy_peer *peer; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1413 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1414 | switch (nsp->u.status.cause) { |
| 1415 | case NS_AFF_CAUSE_SNS_FAILURE: |
| 1416 | case NS_AFF_CAUSE_SNS_CONFIGURED: |
| 1417 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1418 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1419 | case NS_AFF_CAUSE_RECOVERY: |
| 1420 | LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei); |
| 1421 | if (nsp->nsei == cfg->nsip_sgsn_nsei) { |
| 1422 | /* look-up or create the BTS context for this BVC */ |
| 1423 | struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei); |
| 1424 | if (!bctx) |
| 1425 | bctx = btsctx_alloc(nsp->bvci, nsp->nsei); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1426 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1427 | bssgp_tx_bvc_reset_nsei_bvci(cfg->nsip_sgsn_nsei, 0, BSSGP_CAUSE_OML_INTERV, NULL, 0); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1428 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1429 | break; |
| 1430 | case NS_AFF_CAUSE_FAILURE: |
| 1431 | if (nsp->nsei == cfg->nsip_sgsn_nsei) { |
| 1432 | /* sgsn */ |
| 1433 | /* TODO: BSVC: block all PtP towards bss */ |
| 1434 | rate_ctr_inc(&cfg->ctrg-> |
| 1435 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
| 1436 | } else { |
| 1437 | /* bss became unavailable */ |
| 1438 | peer = gbproxy_peer_by_nsei(cfg, nsp->nsei); |
| 1439 | if (!peer) { |
| 1440 | /* TODO: use primitive name + status cause name */ |
| 1441 | LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown peer NSEI=%u\n", |
| 1442 | nsp->u.status.cause, nsp->nsei); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1443 | break; |
| 1444 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1445 | |
| 1446 | if (!peer->blocked) |
| 1447 | break; |
| 1448 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei, |
| 1449 | peer->bvci, 0); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1450 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1451 | LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei); |
| 1452 | break; |
| 1453 | default: |
| 1454 | LOGP(DPCU, LOGL_NOTICE, |
| 1455 | "NS: %s Unknown prim %d from NS\n", |
| 1456 | get_value_string(osmo_prim_op_names, nsp->oph.operation), |
| 1457 | nsp->oph.primitive); |
| 1458 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1459 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | /* called by the ns layer */ |
| 1464 | int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 1465 | { |
| 1466 | struct osmo_gprs_ns2_prim *nsp; |
| 1467 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 1468 | int rc = 0; |
| 1469 | |
| 1470 | if (oph->sap != SAP_NS) |
| 1471 | return 0; |
| 1472 | |
| 1473 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 1474 | |
| 1475 | if (oph->operation != PRIM_OP_INDICATION) { |
| 1476 | LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n", |
| 1477 | get_value_string(osmo_prim_op_names, oph->operation), |
| 1478 | oph->operation); |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | switch (oph->primitive) { |
| 1483 | case PRIM_NS_UNIT_DATA: |
| 1484 | /* hand the message into the BSSGP implementation */ |
| 1485 | msgb_bssgph(oph->msg) = oph->msg->l3h; |
| 1486 | msgb_bvci(oph->msg) = nsp->bvci; |
| 1487 | msgb_nsei(oph->msg) = nsp->nsei; |
| 1488 | |
| 1489 | rc = gbprox_rcvmsg(cfg, oph->msg); |
Daniel Willmann | b655010 | 2020-11-04 17:32:56 +0100 | [diff] [blame] | 1490 | msgb_free(oph->msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1491 | break; |
| 1492 | case PRIM_NS_STATUS: |
| 1493 | gprs_ns_prim_status_cb(cfg, nsp); |
| 1494 | break; |
| 1495 | default: |
| 1496 | LOGP(DPCU, LOGL_NOTICE, |
| 1497 | "NS: %s Unknown prim %d from NS\n", |
| 1498 | get_value_string(osmo_prim_op_names, oph->operation), |
| 1499 | oph->primitive); |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | return rc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | void gbprox_reset(struct gbproxy_config *cfg) |
| 1507 | { |
| 1508 | struct gbproxy_peer *peer, *tmp; |
| 1509 | |
| 1510 | llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) |
| 1511 | gbproxy_peer_free(peer); |
| 1512 | |
| 1513 | rate_ctr_group_free(cfg->ctrg); |
| 1514 | gbproxy_init_config(cfg); |
| 1515 | } |
| 1516 | |
| 1517 | int gbproxy_init_config(struct gbproxy_config *cfg) |
| 1518 | { |
| 1519 | struct timespec tp; |
| 1520 | |
| 1521 | INIT_LLIST_HEAD(&cfg->bts_peers); |
| 1522 | cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0); |
| 1523 | if (!cfg->ctrg) { |
| 1524 | LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n"); |
| 1525 | return -1; |
| 1526 | } |
| 1527 | osmo_clock_gettime(CLOCK_REALTIME, &tp); |
| 1528 | |
| 1529 | return 0; |
| 1530 | } |