Harald Welte | d1991e7 | 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 | 2ea8812 | 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 | d1991e7 | 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 | 0e3e88e | 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 | d1991e7 | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 0e3e88e | 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 | d1991e7 | 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 | f2fdefe | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 32 | #include <time.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 33 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/select.h> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 36 | #include <osmocom/core/rate_ctr.h> |
| 37 | |
Harald Welte | cfb6b28 | 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 | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 41 | #include <osmocom/gsm/gsm_utils.h> |
| 42 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 43 | #include <openbsc/signal.h> |
| 44 | #include <openbsc/debug.h> |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 45 | #include <openbsc/gprs_gb_parse.h> |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 46 | #include <openbsc/gb_proxy.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 47 | |
Jacob Erlbeck | 2504bc4 | 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 | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 51 | #include <openbsc/gprs_utils.h> |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 52 | |
Jacob Erlbeck | 174bad4 | 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 | 9d7268a | 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 | 174bad4 | 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 | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 65 | { "mod-peer-err", "Patch error: no peer " }, |
Jacob Erlbeck | 174bad4 | 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 | 4c0f698 | 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 | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 78 | uint16_t ns_bvci, uint16_t sgsn_nsei); |
Jacob Erlbeck | 4abbbdd | 2014-09-22 13:30:46 +0200 | [diff] [blame] | 79 | static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info); |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 80 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 81 | static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei) |
Jacob Erlbeck | 174bad4 | 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 | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 88 | return 0; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 91 | return 1; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Harald Welte | 3ad3243 | 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 | 4c0f698 | 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 | 9b7cfb0 | 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 | 4c0f698 | 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 | 4c0f698 | 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 | 2cb9e3f | 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 | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 190 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 191 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 2504bc4 | 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 | 2cb9e3f | 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 | cf11e93 | 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 | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 228 | { |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 229 | uint32_t bss_ptmsi; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 230 | int max_retries = 23; |
Jacob Erlbeck | 4c47f2f | 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 | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 238 | if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi)) |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 239 | bss_ptmsi = GSM_RESERVED_TMSI; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 240 | } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--); |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Jacob Erlbeck | 788de3e | 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 | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 246 | return bss_ptmsi; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 249 | uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 250 | struct gbproxy_link_info *link_info, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 251 | uint32_t bss_tlli) |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 252 | { |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 253 | uint32_t sgsn_tlli; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 254 | int max_retries = 23; |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 255 | if (!peer->cfg->patch_ptmsi) { |
| 256 | sgsn_tlli = bss_tlli; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 257 | } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) { |
| 258 | sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi, |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 259 | TLLI_FOREIGN); |
| 260 | } else { |
| 261 | do { |
| 262 | /* create random TLLI, 0b01111xxx... */ |
| 263 | sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state); |
| 264 | sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000; |
| 265 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 266 | if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli)) |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 267 | sgsn_tlli = 0; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 268 | } while (!sgsn_tlli && max_retries--); |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 269 | } |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 270 | |
| 271 | if (!sgsn_tlli) |
| 272 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n"); |
| 273 | |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 274 | return sgsn_tlli; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Jacob Erlbeck | 4abbbdd | 2014-09-22 13:30:46 +0200 | [diff] [blame] | 277 | void gbproxy_reset_link(struct gbproxy_link_info *link_info) |
| 278 | { |
| 279 | gbproxy_reset_imsi_acquisition(link_info); |
| 280 | } |
| 281 | |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 282 | /* Returns != 0 iff IMSI acquisition was in progress */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 283 | static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info) |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 284 | { |
| 285 | int in_progress = 0; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 286 | if (!link_info) |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | |
Jacob Erlbeck | 5e07b81 | 2014-09-22 15:41:21 +0200 | [diff] [blame] | 289 | if (link_info->imsi_acq_pending) |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 290 | in_progress = 1; |
| 291 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 292 | gbproxy_link_info_discard_messages(link_info); |
| 293 | link_info->imsi_acq_pending = 0; |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 294 | |
| 295 | return in_progress; |
| 296 | } |
| 297 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 298 | static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info) |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 299 | { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 300 | gbproxy_restart_imsi_acquisition(link_info); |
Jacob Erlbeck | 5e07b81 | 2014-09-22 15:41:21 +0200 | [diff] [blame] | 301 | link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX; |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer, |
| 305 | struct msgb *msg, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 306 | time_t now, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 307 | struct gbproxy_link_info* link_info, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 308 | struct gprs_gb_parse_context *parse_ctx) |
| 309 | { |
| 310 | int rc; |
| 311 | struct msgb *stored_msg; |
| 312 | /* Got identity response with IMSI, assuming the request had |
| 313 | * been generated by the gbproxy */ |
| 314 | |
| 315 | LOGP(DLLC, LOGL_DEBUG, |
| 316 | "NSEI=%d(BSS) IMSI acquisition succeeded, " |
| 317 | "flushing stored messages\n", |
| 318 | msgb_nsei(msg)); |
| 319 | |
| 320 | /* Patch and flush stored messages towards the SGSN */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 321 | while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) { |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 322 | struct gprs_gb_parse_context tmp_parse_ctx = {0}; |
| 323 | tmp_parse_ctx.to_bss = 0; |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 324 | tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 325 | int len_change = 0; |
| 326 | |
| 327 | gprs_gb_parse_bssgp(msgb_bssgph(stored_msg), |
| 328 | msgb_bssgp_len(stored_msg), |
| 329 | &tmp_parse_ctx); |
| 330 | gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg), |
| 331 | msgb_bssgp_len(stored_msg), |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 332 | peer, link_info, &len_change, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 333 | &tmp_parse_ctx); |
| 334 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 335 | gbproxy_update_link_state_after(peer, link_info, now, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 336 | &tmp_parse_ctx); |
| 337 | |
| 338 | rc = gbprox_relay2sgsn(peer->cfg, stored_msg, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 339 | msgb_bvci(msg), link_info->sgsn_nsei); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 340 | |
| 341 | if (rc < 0) |
| 342 | LOGP(DLLC, LOGL_ERROR, |
| 343 | "NSEI=%d(BSS) failed to send stored message " |
| 344 | "(%s)\n", |
| 345 | msgb_nsei(msg), |
| 346 | parse_ctx->llc_msg_name ? |
| 347 | parse_ctx->llc_msg_name : "BSSGP"); |
| 348 | msgb_free(stored_msg); |
| 349 | } |
| 350 | } |
| 351 | |
Jacob Erlbeck | 5e07b81 | 2014-09-22 15:41:21 +0200 | [diff] [blame] | 352 | static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer, |
| 353 | struct gbproxy_link_info* link_info, |
| 354 | uint16_t bvci, |
| 355 | struct msgb *msg /* Takes msg ownership */) |
| 356 | { |
| 357 | int rc; |
| 358 | |
| 359 | /* Workaround to avoid N(U) collisions and to enable a restart |
| 360 | * of the IMSI acquisition procedure. This will work unless the |
| 361 | * SGSN has an initial V(UT) within [256-32, 256+n_retries] |
| 362 | * (see GSM 04.64, 8.4.2). */ |
| 363 | gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss); |
| 364 | link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512; |
| 365 | |
| 366 | gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current); |
| 367 | rc = gbprox_relay2peer(msg, peer, bvci); |
| 368 | msgb_free(msg); |
| 369 | return rc; |
| 370 | } |
| 371 | |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 372 | static void gbproxy_acquire_imsi(struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 373 | struct gbproxy_link_info* link_info, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 374 | uint16_t bvci) |
| 375 | { |
| 376 | struct msgb *idreq_msg; |
| 377 | |
| 378 | /* Send IDENT REQ */ |
| 379 | idreq_msg = gsm48_msgb_alloc(); |
| 380 | gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI); |
Jacob Erlbeck | 5e07b81 | 2014-09-22 15:41:21 +0200 | [diff] [blame] | 381 | gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 384 | static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 385 | struct gbproxy_link_info* link_info, |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 386 | uint16_t bvci) |
| 387 | { |
| 388 | struct msgb *detacc_msg; |
| 389 | |
| 390 | /* Send DETACH ACC */ |
| 391 | detacc_msg = gsm48_msgb_alloc(); |
| 392 | gprs_put_mo_detach_acc(detacc_msg); |
Jacob Erlbeck | 5e07b81 | 2014-09-22 15:41:21 +0200 | [diff] [blame] | 393 | gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg); |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 394 | } |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 395 | |
| 396 | /* Return != 0 iff msg still needs to be processed */ |
| 397 | static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer, |
| 398 | struct msgb *msg, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 399 | time_t now, |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 400 | struct gbproxy_link_info* link_info, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 401 | struct gprs_gb_parse_context *parse_ctx) |
| 402 | { |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 403 | struct msgb *stored_msg; |
| 404 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 405 | if (!link_info) |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 406 | return 1; |
| 407 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 408 | if (!link_info->imsi_acq_pending && link_info->imsi_len > 0) |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 409 | return 1; |
| 410 | |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 411 | if (parse_ctx->g48_hdr) |
| 412 | switch (parse_ctx->g48_hdr->msg_type) |
| 413 | { |
Jacob Erlbeck | a93b491 | 2014-09-18 10:08:27 +0200 | [diff] [blame] | 414 | case GSM48_MT_GMM_RA_UPD_REQ: |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 415 | case GSM48_MT_GMM_ATTACH_REQ: |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 416 | if (gbproxy_restart_imsi_acquisition(link_info)) { |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 417 | LOGP(DLLC, LOGL_INFO, |
| 418 | "NSEI=%d(BSS) IMSI acquisition was in progress " |
Jacob Erlbeck | a93b491 | 2014-09-18 10:08:27 +0200 | [diff] [blame] | 419 | "when receiving an %s.\n", |
| 420 | msgb_nsei(msg), parse_ctx->llc_msg_name); |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 421 | } |
| 422 | break; |
| 423 | case GSM48_MT_GMM_DETACH_REQ: |
| 424 | /* Nothing has been sent to the SGSN yet */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 425 | if (link_info->imsi_acq_pending) { |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 426 | LOGP(DLLC, LOGL_INFO, |
| 427 | "NSEI=%d(BSS) IMSI acquisition was in progress " |
| 428 | "when receiving a DETACH_REQ.\n", |
| 429 | msgb_nsei(msg)); |
| 430 | } |
| 431 | if (!parse_ctx->invalidate_tlli) { |
| 432 | LOGP(DLLC, LOGL_INFO, |
| 433 | "NSEI=%d(BSS) IMSI not yet acquired, " |
| 434 | "faking a DETACH_ACC.\n", |
| 435 | msgb_nsei(msg)); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 436 | gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg)); |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 437 | parse_ctx->invalidate_tlli = 1; |
| 438 | } |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 439 | gbproxy_reset_imsi_acquisition(link_info); |
| 440 | gbproxy_update_link_state_after(peer, link_info, now, |
Jacob Erlbeck | 9b7cfb0 | 2014-09-12 12:12:31 +0200 | [diff] [blame] | 441 | parse_ctx); |
| 442 | return 0; |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 443 | } |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 444 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 445 | if (link_info->imsi_acq_pending && link_info->imsi_len > 0) { |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 446 | int is_ident_resp = |
| 447 | parse_ctx->g48_hdr && |
| 448 | parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS && |
| 449 | parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP; |
| 450 | |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 451 | /* The IMSI is now available */ |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 452 | gbproxy_flush_stored_messages(peer, msg, now, link_info, |
Jacob Erlbeck | 19a7c7a | 2014-09-19 13:23:21 +0200 | [diff] [blame] | 453 | parse_ctx); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 454 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 455 | gbproxy_reset_imsi_acquisition(link_info); |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 456 | |
| 457 | /* This message is most probably the response to the ident |
| 458 | * request sent by gbproxy_acquire_imsi(). Don't forward it to |
| 459 | * the SGSN. */ |
| 460 | return !is_ident_resp; |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 463 | /* The message cannot be processed since the IMSI is still missing */ |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 464 | |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 465 | /* Enqueue unpatched messages */ |
| 466 | LOGP(DLLC, LOGL_INFO, |
| 467 | "NSEI=%d(BSS) IMSI acquisition in progress, " |
| 468 | "storing message (%s)\n", |
| 469 | msgb_nsei(msg), |
| 470 | parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP"); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 471 | |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 472 | stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul"); |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 473 | msgb_enqueue(&link_info->stored_msgs, stored_msg); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 474 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 475 | if (!link_info->imsi_acq_pending) { |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 476 | LOGP(DLLC, LOGL_INFO, |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 477 | "NSEI=%d(BSS) IMSI is required but not available, " |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 478 | "initiating identification procedure (%s)\n", |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 479 | msgb_nsei(msg), |
| 480 | parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP"); |
| 481 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 482 | gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg)); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 483 | |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 484 | /* There is no explicit retransmission handling, the |
| 485 | * implementation relies on the MS doing proper retransmissions |
| 486 | * of the triggering message instead */ |
| 487 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 488 | link_info->imsi_acq_pending = 1; |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Jacob Erlbeck | 8ee2a5e | 2014-09-11 11:35:17 +0200 | [diff] [blame] | 491 | return 0; |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg, |
| 495 | struct msgb *msg, |
| 496 | struct gprs_gb_parse_context *parse_ctx) |
| 497 | { |
| 498 | struct gbproxy_peer *peer = NULL; |
| 499 | |
| 500 | if (msgb_bvci(msg) >= 2) |
| 501 | peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg)); |
| 502 | |
| 503 | if (!peer && !parse_ctx->to_bss) |
| 504 | peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg)); |
| 505 | |
| 506 | if (!peer) |
| 507 | peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp); |
| 508 | |
| 509 | if (!peer) { |
| 510 | LOGP(DLLC, LOGL_INFO, |
| 511 | "NSEI=%d(%s) patching: didn't find peer for message, " |
| 512 | "PDU %d\n", |
| 513 | msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN", |
| 514 | parse_ctx->pdu_type); |
| 515 | /* Increment counter */ |
| 516 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]); |
| 517 | } |
| 518 | return peer; |
| 519 | } |
| 520 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 521 | /* patch BSSGP message */ |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 522 | static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg, |
| 523 | struct msgb *msg, |
| 524 | struct gbproxy_peer *peer) |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 525 | { |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 526 | struct gprs_gb_parse_context parse_ctx = {0}; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 527 | int rc; |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 528 | int len_change = 0; |
Jacob Erlbeck | c404c08 | 2014-08-08 08:37:37 +0200 | [diff] [blame] | 529 | time_t now; |
Jacob Erlbeck | 7c276fd | 2014-09-19 16:34:01 +0200 | [diff] [blame] | 530 | struct timespec ts = {0,}; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 531 | struct gbproxy_link_info *link_info = NULL; |
Jacob Erlbeck | 02817f0 | 2014-08-27 12:51:07 +0200 | [diff] [blame] | 532 | uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 533 | |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 534 | if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn && |
Jacob Erlbeck | 49f0c3e | 2014-09-04 11:42:08 +0200 | [diff] [blame] | 535 | !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2) |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 536 | return 1; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 537 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 538 | parse_ctx.to_bss = 0; |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 539 | parse_ctx.peer_nsei = msgb_nsei(msg); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 540 | |
| 541 | /* Parse BSSGP/LLC */ |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 542 | rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 543 | &parse_ctx); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 544 | |
| 545 | if (!rc) { |
| 546 | if (!parse_ctx.need_decryption) { |
| 547 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | 4b5070e | 2014-08-18 17:26:04 +0200 | [diff] [blame] | 548 | "NSEI=%u(BSS) patching: " |
| 549 | "failed to parse BSSGP/GMM message\n", |
| 550 | msgb_nsei(msg)); |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 551 | return 0; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | /* Get peer */ |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 556 | if (!peer) |
| 557 | peer = gbproxy_find_peer(cfg, msg, &parse_ctx); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 558 | |
| 559 | if (!peer) |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 560 | return 0; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 561 | |
Jacob Erlbeck | 7c276fd | 2014-09-19 16:34:01 +0200 | [diff] [blame] | 562 | |
| 563 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 564 | now = ts.tv_sec; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 565 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 566 | gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer, |
| 567 | parse_ctx.llc_msg_name); |
| 568 | |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 569 | gprs_gb_log_parse_context(&parse_ctx, "BSSGP"); |
| 570 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 571 | link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx); |
Jacob Erlbeck | 6e291e0 | 2014-09-03 11:59:48 +0200 | [diff] [blame] | 572 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 573 | if (parse_ctx.g48_hdr) { |
| 574 | switch (parse_ctx.g48_hdr->msg_type) { |
| 575 | case GSM48_MT_GMM_ATTACH_REQ: |
| 576 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]); |
| 577 | break; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 578 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 579 | default: |
| 580 | break; |
| 581 | } |
| 582 | } |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 583 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 584 | if (link_info && cfg->route_to_sgsn2) { |
| 585 | if (cfg->acquire_imsi && link_info->imsi_len == 0) |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 586 | sgsn_nsei = 0xffff; |
Jacob Erlbeck | 3d80527 | 2014-09-25 13:21:48 +0200 | [diff] [blame] | 587 | else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING, |
Jacob Erlbeck | f4290b0 | 2014-09-25 11:17:31 +0200 | [diff] [blame] | 588 | link_info)) |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 589 | sgsn_nsei = cfg->nsip_sgsn2_nsei; |
Jacob Erlbeck | 02817f0 | 2014-08-27 12:51:07 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 592 | if (link_info) |
| 593 | link_info->sgsn_nsei = sgsn_nsei; |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 594 | |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 595 | /* Handle IMSI acquisition */ |
| 596 | if (cfg->acquire_imsi) { |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 597 | rc = gbproxy_imsi_acquisition(peer, msg, now, link_info, |
Jacob Erlbeck | 19a7c7a | 2014-09-19 13:23:21 +0200 | [diff] [blame] | 598 | &parse_ctx); |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 599 | if (rc <= 0) |
| 600 | return rc; |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 603 | gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg), |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 604 | peer, link_info, &len_change, &parse_ctx); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 605 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 606 | gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 607 | |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 608 | if (sgsn_nsei != cfg->nsip_sgsn_nsei) { |
Jacob Erlbeck | 02817f0 | 2014-08-27 12:51:07 +0200 | [diff] [blame] | 609 | /* Send message directly to the selected SGSN */ |
| 610 | rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei); |
| 611 | /* Don't let the calling code handle the transmission */ |
| 612 | return 0; |
| 613 | } |
| 614 | |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 615 | return 1; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* patch BSSGP message to use core_mcc/mnc on the SGSN side */ |
| 619 | static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg, |
| 620 | struct msgb *msg, |
| 621 | struct gbproxy_peer *peer) |
| 622 | { |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 623 | struct gprs_gb_parse_context parse_ctx = {0}; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 624 | int rc; |
| 625 | int len_change = 0; |
| 626 | time_t now; |
Jacob Erlbeck | 7c276fd | 2014-09-19 16:34:01 +0200 | [diff] [blame] | 627 | struct timespec ts = {0,}; |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 628 | struct gbproxy_link_info *link_info = NULL; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 629 | |
Jacob Erlbeck | 49f0c3e | 2014-09-04 11:42:08 +0200 | [diff] [blame] | 630 | if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn && |
| 631 | !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2) |
| 632 | return; |
| 633 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 634 | parse_ctx.to_bss = 1; |
Jacob Erlbeck | d918f52 | 2014-09-17 10:56:38 +0200 | [diff] [blame] | 635 | parse_ctx.peer_nsei = msgb_nsei(msg); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 636 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 637 | rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 638 | &parse_ctx); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 639 | |
| 640 | if (!rc) { |
| 641 | if (!parse_ctx.need_decryption) { |
| 642 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | 4b5070e | 2014-08-18 17:26:04 +0200 | [diff] [blame] | 643 | "NSEI=%u(SGSN) patching: " |
| 644 | "failed to parse BSSGP/GMM message\n", |
| 645 | msgb_nsei(msg)); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | } |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 649 | |
Jacob Erlbeck | 97dc4db | 2014-09-11 10:30:21 +0200 | [diff] [blame] | 650 | /* Get peer */ |
| 651 | if (!peer) |
| 652 | peer = gbproxy_find_peer(cfg, msg, &parse_ctx); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 653 | |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 654 | if (!peer) |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 655 | return; |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 656 | |
Jacob Erlbeck | 7c276fd | 2014-09-19 16:34:01 +0200 | [diff] [blame] | 657 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 658 | now = ts.tv_sec; |
Jacob Erlbeck | c404c08 | 2014-08-08 08:37:37 +0200 | [diff] [blame] | 659 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 660 | if (parse_ctx.g48_hdr) { |
| 661 | switch (parse_ctx.g48_hdr->msg_type) { |
| 662 | case GSM48_MT_GMM_ATTACH_REJ: |
| 663 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]); |
| 664 | break; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 665 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 666 | default: |
| 667 | break; |
| 668 | } |
| 669 | } |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 670 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 671 | gprs_gb_log_parse_context(&parse_ctx, "BSSGP"); |
| 672 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 673 | link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 674 | |
| 675 | gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg), |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 676 | peer, link_info, &len_change, &parse_ctx); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 677 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 678 | gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 679 | |
Jacob Erlbeck | f14bb91 | 2014-06-27 19:56:05 +0200 | [diff] [blame] | 680 | return; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 683 | /* feed a message down the NS-VC associated with the specified peer */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 684 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 685 | uint16_t ns_bvci, uint16_t sgsn_nsei) |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 686 | { |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 687 | /* create a copy of the message so the old one can |
| 688 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 689 | struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 690 | int rc; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 691 | |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 692 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 693 | msgb_nsei(msg), ns_bvci, sgsn_nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 694 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 695 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 696 | msgb_nsei(msg) = sgsn_nsei; |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 697 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 698 | strip_ns_hdr(msg); |
| 699 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 700 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 701 | if (rc < 0) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 702 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 703 | |
| 704 | return rc; |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 707 | /* feed a message down the NS-VC associated with the specified peer */ |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 708 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 709 | uint16_t ns_bvci) |
| 710 | { |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 711 | /* create a copy of the message so the old one can |
| 712 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 713 | struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 714 | int rc; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 715 | |
Harald Welte | 0cc900d | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 716 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 717 | msgb_nsei(msg), ns_bvci, peer->nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 718 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 719 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 720 | msgb_nsei(msg) = peer->nsei; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 721 | |
Harald Welte | 0cc900d | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 722 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 723 | strip_ns_hdr(msg); |
| 724 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 725 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 726 | if (rc < 0) |
| 727 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
| 728 | |
| 729 | return rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 732 | static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type) |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 733 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 734 | struct gbproxy_peer *peer; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 735 | |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 736 | peer = gbproxy_peer_by_bvci(cfg, ptp_bvci); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 737 | if (!peer) { |
| 738 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 739 | ptp_bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 740 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 741 | return -ENOENT; |
| 742 | } |
| 743 | |
| 744 | switch (pdu_type) { |
| 745 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 746 | peer->blocked = 1; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 747 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 748 | break; |
| 749 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 750 | peer->blocked = 0; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 751 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 752 | break; |
| 753 | default: |
| 754 | break; |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 759 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 760 | * in the NS hdr */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 761 | static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 762 | uint16_t ns_bvci) |
| 763 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 764 | struct gbproxy_peer *peer; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 765 | |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 766 | peer = gbproxy_peer_by_bvci(cfg, ptp_bvci); |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 767 | if (!peer) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 768 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 769 | ptp_bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 770 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 771 | return -ENOENT; |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 772 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 773 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 774 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Harald Welte | a62e9fe | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 777 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 778 | { |
| 779 | return 0; |
| 780 | } |
| 781 | |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 782 | /* Receive an incoming PTP message from a BSS-side NS-VC */ |
| 783 | static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg, |
| 784 | struct msgb *msg, uint16_t nsei, |
| 785 | uint16_t nsvci, uint16_t ns_bvci) |
| 786 | { |
| 787 | struct gbproxy_peer *peer; |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 788 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 789 | uint8_t pdu_type = bgph->pdu_type; |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 790 | int rc; |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 791 | |
| 792 | peer = gbproxy_peer_by_bvci(cfg, ns_bvci); |
| 793 | |
Jacob Erlbeck | d88dd3c | 2014-10-02 14:56:44 +0200 | [diff] [blame] | 794 | check_peer_nsei(peer, nsei); |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 795 | rc = gbprox_process_bssgp_ul(cfg, msg, peer); |
Jacob Erlbeck | d88dd3c | 2014-10-02 14:56:44 +0200 | [diff] [blame] | 796 | if (!rc) { |
| 797 | if (!peer) { |
| 798 | LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for " |
| 799 | "BVCI=%u for PTP message from NSVC=%u/NSEI=%u (BSS)\n", |
| 800 | ns_bvci, nsvci, nsei); |
| 801 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 802 | &ns_bvci, msg); |
| 803 | } |
| 804 | |
Jacob Erlbeck | 4c0f698 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 805 | return 0; |
Jacob Erlbeck | d88dd3c | 2014-10-02 14:56:44 +0200 | [diff] [blame] | 806 | } |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 807 | |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 808 | switch (pdu_type) { |
| 809 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
| 810 | if (!cfg->route_to_sgsn2) |
| 811 | break; |
| 812 | |
| 813 | /* Send a copy to the secondary SGSN */ |
| 814 | gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei); |
| 815 | break; |
| 816 | default: |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 821 | return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei); |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* Receive an incoming PTP message from a SGSN-side NS-VC */ |
| 825 | static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg, |
| 826 | struct msgb *msg, uint16_t nsei, |
| 827 | uint16_t nsvci, uint16_t ns_bvci) |
| 828 | { |
| 829 | struct gbproxy_peer *peer; |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 830 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 831 | uint8_t pdu_type = bgph->pdu_type; |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 832 | |
| 833 | peer = gbproxy_peer_by_bvci(cfg, ns_bvci); |
| 834 | |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 835 | /* Send status messages before patching */ |
| 836 | |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 837 | if (!peer) { |
| 838 | LOGP(DGPRS, LOGL_INFO, "Didn't find peer for " |
| 839 | "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n", |
| 840 | ns_bvci, nsvci, nsei); |
| 841 | rate_ctr_inc(&cfg->ctrg-> |
| 842 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 843 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 844 | &ns_bvci, msg); |
| 845 | } |
| 846 | |
| 847 | if (peer->blocked) { |
| 848 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
| 849 | "blocked BVCI=%u via NSVC=%u/NSEI=%u\n", |
| 850 | ns_bvci, nsvci, nsei); |
| 851 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Jacob Erlbeck | d9bc3a5 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 852 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg); |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 853 | } |
| 854 | |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 855 | switch (pdu_type) { |
| 856 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 857 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 858 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 859 | if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei) |
| 860 | /* Hide ACKs from the secondary SGSN, the primary SGSN |
| 861 | * is responsible to send them. */ |
| 862 | return 0; |
| 863 | break; |
| 864 | default: |
| 865 | break; |
| 866 | } |
| 867 | |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 868 | /* Optionally patch the message */ |
| 869 | gbprox_process_bssgp_dl(cfg, msg, peer); |
| 870 | |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 871 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 872 | } |
| 873 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 874 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 875 | static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg, |
| 876 | struct msgb *msg, uint16_t nsei, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 877 | uint16_t ns_bvci) |
| 878 | { |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 879 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 880 | struct tlv_parsed tp; |
| 881 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 882 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 883 | struct gbproxy_peer *from_peer = NULL; |
Harald Welte | e49c829 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 884 | struct gprs_ra_id raid; |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 885 | int copy_to_sgsn2 = 0; |
Jacob Erlbeck | 4d16d6c | 2014-09-08 09:04:01 +0200 | [diff] [blame] | 886 | int rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 887 | |
Harald Welte | 4127072 | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 888 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 889 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 890 | nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | /* we actually should never see those two for BVCI == 0, but double-check |
| 895 | * just to make sure */ |
| 896 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 897 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 898 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 899 | "signalling\n", nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | } |
| 902 | |
| 903 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 904 | |
| 905 | switch (pdu_type) { |
| 906 | case BSSGP_PDUT_SUSPEND: |
| 907 | case BSSGP_PDUT_RESUME: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 908 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
| 909 | * establishes a relationsip between BVCI/peer and the routeing |
| 910 | * area identification. The snooped information is then used |
| 911 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 912 | * BSSGP */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 913 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 914 | goto err_mand_ie; |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 915 | from_peer = gbproxy_peer_by_nsei(cfg, nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 916 | if (!from_peer) |
| 917 | goto err_no_peer; |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 918 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 919 | sizeof(from_peer->ra)); |
Harald Welte | f2fdefe | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 920 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 921 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 922 | "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 923 | nsei, raid.mcc, raid.mnc, raid.lac, |
| 924 | raid.rac , from_peer->bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 925 | /* FIXME: This only supports one BSS per RA */ |
| 926 | break; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 927 | case BSSGP_PDUT_BVC_RESET: |
| 928 | /* If we receive a BVC reset on the signalling endpoint, we |
| 929 | * don't want the SGSN to reset, as the signalling endpoint |
| 930 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 931 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 932 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | be6e25a | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 933 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 934 | nsei, bvci); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 935 | if (bvci == 0) { |
| 936 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 937 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 938 | "BVC RESET ACK of BVCI=0\n", nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 939 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 940 | nsei, 0, ns_bvci); |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 941 | } |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 942 | from_peer = gbproxy_peer_by_bvci(cfg, bvci); |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 943 | if (!from_peer) { |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 944 | /* if a PTP-BVC is reset, and we don't know that |
| 945 | * PTP-BVCI yet, we should allocate a new peer */ |
| 946 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 947 | "BVCI=%u via NSEI=%u\n", bvci, nsei); |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 948 | from_peer = gbproxy_peer_alloc(cfg, bvci); |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 949 | from_peer->nsei = nsei; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 950 | } |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 951 | |
| 952 | if (!check_peer_nsei(from_peer, nsei)) |
| 953 | from_peer->nsei = nsei; |
| 954 | |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 955 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 956 | struct gprs_ra_id raid; |
| 957 | /* We have a Cell Identifier present in this |
| 958 | * PDU, this means we can extend our local |
| 959 | * state information about this particular cell |
| 960 | * */ |
| 961 | memcpy(from_peer->ra, |
| 962 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 963 | sizeof(from_peer->ra)); |
| 964 | gsm48_parse_ra(&raid, from_peer->ra); |
| 965 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 966 | "Cell ID %u-%u-%u-%u\n", nsei, |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 967 | bvci, raid.mcc, raid.mnc, raid.lac, |
| 968 | raid.rac); |
| 969 | } |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 970 | if (cfg->route_to_sgsn2) |
| 971 | copy_to_sgsn2 = 1; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 972 | } |
| 973 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 976 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 977 | * SGSN */ |
Jacob Erlbeck | 4d16d6c | 2014-09-08 09:04:01 +0200 | [diff] [blame] | 978 | rc = gbprox_process_bssgp_ul(cfg, msg, from_peer); |
| 979 | if (!rc) |
| 980 | return 0; |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 981 | |
| 982 | if (copy_to_sgsn2) |
| 983 | gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei); |
| 984 | |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 985 | return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 986 | err_no_peer: |
Jacob Erlbeck | 700b873 | 2013-10-18 13:04:48 +0200 | [diff] [blame] | 987 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 988 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 989 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Jacob Erlbeck | d9bc3a5 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 990 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 991 | err_mand_ie: |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 992 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 993 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 994 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 995 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 999 | static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1000 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1001 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1002 | struct gbproxy_peer *peer = NULL; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1003 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1004 | |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1005 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1006 | nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1007 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1008 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1009 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", |
| 1010 | bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1011 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1012 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 1013 | peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1014 | LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 98b82c1 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 1015 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1016 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1017 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 1018 | peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1019 | LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 98b82c1 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 1020 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1021 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1022 | } else |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1023 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 1024 | |
| 1025 | if (!peer) { |
| 1026 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1027 | "unable to route, missing IE\n", nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1028 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1029 | return -EINVAL; |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1030 | } |
| 1031 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1034 | /* Receive an incoming BVC-RESET message from the SGSN */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1035 | static int rx_reset_from_sgsn(struct gbproxy_config *cfg, |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1036 | struct msgb *orig_msg, |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1037 | struct msgb *msg, struct tlv_parsed *tp, |
| 1038 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1039 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1040 | struct gbproxy_peer *peer; |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1041 | uint16_t ptp_bvci; |
| 1042 | |
| 1043 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1044 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1045 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1046 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1047 | NULL, orig_msg); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1048 | } |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1049 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1050 | |
| 1051 | if (ptp_bvci >= 2) { |
| 1052 | /* A reset for a PTP BVC was received, forward it to its |
| 1053 | * respective peer */ |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 1054 | peer = gbproxy_peer_by_bvci(cfg, ptp_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1055 | if (!peer) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1056 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1057 | nsei, ptp_bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1058 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1059 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1060 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
Jacob Erlbeck | d9bc3a5 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 1061 | &ptp_bvci, orig_msg); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1062 | } |
| 1063 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 1064 | } |
| 1065 | |
| 1066 | /* A reset for the Signalling entity has been received |
| 1067 | * from the SGSN. As the signalling BVCI is shared |
| 1068 | * among all the BSS's that we multiplex, it needs to |
| 1069 | * be relayed */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1070 | llist_for_each_entry(peer, &cfg->bts_peers, list) |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1071 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1076 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1077 | static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg, |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1078 | struct msgb *orig_msg, uint32_t nsei, |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1079 | uint16_t ns_bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1080 | { |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1081 | struct bssgp_normal_hdr *bgph = |
| 1082 | (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1083 | struct tlv_parsed tp; |
| 1084 | uint8_t pdu_type = bgph->pdu_type; |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1085 | int data_len; |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1086 | struct gbproxy_peer *peer; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1087 | uint16_t bvci; |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1088 | struct msgb *msg; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1089 | int rc = 0; |
Jacob Erlbeck | 0936c8a | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 1090 | int cause; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1091 | |
Harald Welte | 4127072 | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 1092 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1093 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1094 | "signalling\n", nsei, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1095 | /* FIXME: Send proper error message */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1096 | return -EINVAL; |
| 1097 | } |
| 1098 | |
| 1099 | /* we actually should never see those two for BVCI == 0, but double-check |
| 1100 | * just to make sure */ |
| 1101 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 1102 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1103 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1104 | "signalling\n", nsei); |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1105 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1108 | msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn"); |
| 1109 | gbprox_process_bssgp_dl(cfg, msg, NULL); |
| 1110 | /* Update message info */ |
| 1111 | bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 1112 | data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1113 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 1114 | |
| 1115 | switch (pdu_type) { |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1116 | case BSSGP_PDUT_BVC_RESET: |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1117 | rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1118 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1119 | case BSSGP_PDUT_BVC_RESET_ACK: |
Jacob Erlbeck | 8903378 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 1120 | if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei) |
| 1121 | break; |
| 1122 | /* fall through */ |
| 1123 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1124 | /* simple case: BVCI IE is mandatory */ |
| 1125 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1126 | goto err_mand_ie; |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1127 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1128 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1129 | break; |
| 1130 | case BSSGP_PDUT_PAGING_PS: |
| 1131 | case BSSGP_PDUT_PAGING_CS: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1132 | /* process the paging request (LAI/RAI lookup) */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1133 | rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | case BSSGP_PDUT_STATUS: |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1136 | /* Some exception has occurred */ |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1137 | LOGP(DGPRS, LOGL_NOTICE, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1138 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1139 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 1140 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 1141 | goto err_mand_ie; |
| 1142 | } |
Jacob Erlbeck | 0936c8a | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 1143 | cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1144 | LOGPC(DGPRS, LOGL_NOTICE, |
| 1145 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
Jacob Erlbeck | 0936c8a | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 1146 | bssgp_cause_str(cause)); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1147 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Jacob Erlbeck | 0936c8a | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 1148 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1149 | LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci); |
| 1150 | |
| 1151 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI) |
| 1152 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1153 | } else |
| 1154 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1155 | break; |
| 1156 | /* those only exist in the SGSN -> BSS direction */ |
| 1157 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1158 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1159 | case BSSGP_PDUT_RESUME_ACK: |
| 1160 | case BSSGP_PDUT_RESUME_NACK: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1161 | /* RAI IE is mandatory */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1162 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 1163 | goto err_mand_ie; |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 1164 | peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1165 | if (!peer) |
| 1166 | goto err_no_peer; |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1167 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1168 | break; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1169 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1170 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1171 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1172 | goto err_mand_ie; |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1173 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1174 | if (bvci == 0) { |
| 1175 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1176 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1177 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 1178 | /* should we send STATUS ? */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1179 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1180 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1181 | } else { |
| 1182 | /* Mark BVC as (un)blocked */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1183 | block_unblock_peer(cfg, bvci, pdu_type); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1184 | } |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1185 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1186 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1187 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1188 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1189 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1190 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1191 | ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]); |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1192 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1193 | break; |
| 1194 | default: |
Harald Welte | be6e25a | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 1195 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n", |
| 1196 | pdu_type); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1197 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1198 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1199 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1200 | break; |
| 1201 | } |
| 1202 | |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1203 | msgb_free(msg); |
| 1204 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1205 | return rc; |
| 1206 | err_mand_ie: |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1207 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1208 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1209 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1210 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1211 | msgb_free(msg); |
| 1212 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1213 | err_no_peer: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1214 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1215 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1216 | rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 1217 | msgb_free(msg); |
Jacob Erlbeck | d9bc3a5 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 1218 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 1221 | static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 1222 | { |
| 1223 | return nsei == cfg->nsip_sgsn_nsei || |
| 1224 | (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei); |
| 1225 | } |
| 1226 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1227 | /* Main input function for Gb proxy */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1228 | int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, |
| 1229 | uint16_t ns_bvci, uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1230 | { |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1231 | int rc; |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 1232 | int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1233 | |
| 1234 | /* Only BVCI=0 messages need special treatment */ |
| 1235 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1236 | if (remote_end_is_sgsn) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1237 | rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1238 | else |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1239 | rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci); |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1240 | } else { |
Jacob Erlbeck | 5cd205e | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 1241 | /* All other BVCI are PTP */ |
| 1242 | if (remote_end_is_sgsn) |
| 1243 | rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci, |
| 1244 | ns_bvci); |
| 1245 | else |
| 1246 | rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci, |
| 1247 | ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1250 | return rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1251 | } |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1252 | |
Harald Welte | 9ee404a | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1253 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi) |
| 1254 | { |
| 1255 | struct gprs_nsvc *nsvc; |
| 1256 | |
| 1257 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 1258 | if (!nsvc->persistent) |
| 1259 | continue; |
| 1260 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 1261 | } |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1265 | /* Signal handler for signals from NS layer */ |
| 1266 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 1267 | void *handler_data, void *signal_data) |
| 1268 | { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1269 | struct gbproxy_config *cfg = handler_data; |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1270 | struct ns_signal_data *nssd = signal_data; |
| 1271 | struct gprs_nsvc *nsvc = nssd->nsvc; |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1272 | struct gbproxy_peer *peer; |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 1273 | int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei); |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1274 | |
Harald Welte | 7ed98e3 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 1275 | if (subsys != SS_L_NS) |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1276 | return 0; |
| 1277 | |
Jacob Erlbeck | 3803e9b | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 1278 | if (signal == S_NS_RESET && remote_end_is_sgsn) { |
Harald Welte | 15cbb0e | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 1279 | /* We have received a NS-RESET from the NSEI and NSVC |
| 1280 | * of the SGSN. This might happen with SGSN that start |
| 1281 | * their own NS-RESET procedure without waiting for our |
| 1282 | * NS-RESET */ |
| 1283 | nsvc->remote_end_is_sgsn = 1; |
| 1284 | } |
| 1285 | |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 1286 | if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) { |
| 1287 | LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, " |
| 1288 | "re-starting RESET procedure\n"); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1289 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1290 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
Harald Welte | a529c07 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1291 | gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, |
| 1292 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1295 | if (!nsvc->remote_end_is_sgsn) { |
| 1296 | /* from BSS to SGSN */ |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 1297 | peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1298 | if (!peer) { |
| 1299 | LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer " |
| 1300 | "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei, |
| 1301 | nsvc->nsvci); |
| 1302 | return 0; |
| 1303 | } |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1304 | switch (signal) { |
| 1305 | case S_NS_RESET: |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1306 | case S_NS_BLOCK: |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1307 | if (!peer->blocked) |
| 1308 | break; |
| 1309 | LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from " |
| 1310 | "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n", |
| 1311 | nsvc->nsei, nsvc->nsvci); |
| 1312 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, |
| 1313 | peer->bvci, 0); |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1314 | break; |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1315 | } |
| 1316 | } else { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1317 | /* Forward this message to all NS-VC to BSS */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1318 | struct gprs_ns_inst *nsi = cfg->nsi; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1319 | struct gprs_nsvc *next_nsvc; |
| 1320 | |
| 1321 | llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) { |
| 1322 | if (next_nsvc->remote_end_is_sgsn) |
| 1323 | continue; |
| 1324 | |
| 1325 | /* Note that the following does not start the full |
| 1326 | * procedures including timer based retransmissions. */ |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1327 | switch (signal) { |
| 1328 | case S_NS_RESET: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1329 | gprs_ns_tx_reset(next_nsvc, nssd->cause); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1330 | break; |
| 1331 | case S_NS_BLOCK: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1332 | gprs_ns_tx_block(next_nsvc, nssd->cause); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1333 | break; |
| 1334 | case S_NS_UNBLOCK: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1335 | gprs_ns_tx_unblock(next_nsvc); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1336 | break; |
| 1337 | } |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1343 | void gbprox_reset(struct gbproxy_config *cfg) |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 1344 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1345 | struct gbproxy_peer *peer, *tmp; |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 1346 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1347 | llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 1348 | gbproxy_peer_free(peer); |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 1349 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1350 | rate_ctr_group_free(cfg->ctrg); |
| 1351 | gbproxy_init_config(cfg); |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
Holger Hans Peter Freyther | 5eaf1a2 | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 1354 | int gbproxy_init_config(struct gbproxy_config *cfg) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1355 | { |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 1356 | struct timespec tp; |
Jacob Erlbeck | f4290b0 | 2014-09-25 11:17:31 +0200 | [diff] [blame] | 1357 | |
Holger Hans Peter Freyther | 5eaf1a2 | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 1358 | INIT_LLIST_HEAD(&cfg->bts_peers); |
Holger Hans Peter Freyther | 98e753f | 2014-08-04 11:19:56 +0200 | [diff] [blame] | 1359 | cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0); |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 1360 | clock_gettime(CLOCK_REALTIME, &tp); |
| 1361 | cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec; |
| 1362 | cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec; |
Jacob Erlbeck | f4290b0 | 2014-09-25 11:17:31 +0200 | [diff] [blame] | 1363 | |
Holger Hans Peter Freyther | 5eaf1a2 | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 1364 | return 0; |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1365 | } |