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