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