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