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