blob: faf4a8bf212f623cb80404da2c3f4b3d8cb288ff [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02004 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
Harald Welte9f75c352010-04-30 20:26:32 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * 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 Welte9f75c352010-04-30 20:26:32 +020011 * (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 Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * 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 Welte9f75c352010-04-30 20:26:32 +020020 *
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 Welte7fc98222010-05-11 10:15:42 +020031#include <arpa/inet.h>
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020032#include <time.h>
Harald Welte9f75c352010-04-30 20:26:32 +020033
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020036#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010037#include <osmocom/core/stats.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020038
Harald Welteea34a4e2012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_ns.h>
40#include <osmocom/gprs/gprs_bssgp.h>
41
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020042#include <osmocom/gsm/gsm_utils.h>
43
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020044#include <osmocom/sgsn/signal.h>
45#include <osmocom/sgsn/debug.h>
46#include <osmocom/sgsn/gprs_gb_parse.h>
47#include <osmocom/sgsn/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020048
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020049#include <osmocom/sgsn/gprs_llc.h>
Harald Welte53373bc2016-04-20 17:11:43 +020050#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020051#include <osmocom/sgsn/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020052
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010053extern void *tall_sgsn_ctx;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020054
Jacob Erlbeckbc555742013-10-18 14:34:55 +020055static const struct rate_ctr_desc global_ctr_description[] = {
56 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020057 { "inv-lai", "Invalid Location Area Identifier" },
58 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020059 { "inv-nsei", "No BVC established for NSEI " },
Pau Espin Pedrol437e2092018-07-24 12:41:26 +020060 { "proto-err:bss", "BSSGP protocol error (BSS )" },
61 { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
62 { "not-supp:bss", "Feature not supported (BSS )" },
63 { "not-supp:sgsn", "Feature not supported (SGSN)" },
64 { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
65 { "tx-err:sgsn", "NS Transmission error (SGSN)" },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020066 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020067 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020068};
69
70static const struct rate_ctr_group_desc global_ctrg_desc = {
Max8a01a802017-12-20 13:10:11 +010071 .group_name_prefix = "gbproxy:global",
Jacob Erlbeckbc555742013-10-18 14:34:55 +020072 .group_description = "GBProxy Global Statistics",
73 .num_ctr = ARRAY_SIZE(global_ctr_description),
74 .ctr_desc = global_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010075 .class_id = OSMO_STATS_CLASS_GLOBAL,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020076};
77
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020078static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
79 uint16_t ns_bvci);
80static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020081 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +020082static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020083
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020084static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020085{
86 if (peer->nsei != nsei) {
87 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
88 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
89 peer->bvci, nsei, peer->nsei);
90 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020091 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020092 }
93
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020094 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020095}
96
Harald Welte69619e32010-05-03 19:05:10 +020097/* strip off the NS header */
98static void strip_ns_hdr(struct msgb *msg)
99{
100 int strip_len = msgb_bssgph(msg) - msg->data;
101 msgb_pull(msg, strip_len);
102}
103
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200104/* Transmit Chapter 9.2.10 Identity Request */
105static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
106{
107 struct gsm48_hdr *gh;
108
109 id_type &= GSM_MI_TYPE_MASK;
110
111 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
112 gh->proto_discr = GSM48_PDISC_MM_GPRS;
113 gh->msg_type = GSM48_MT_GMM_ID_REQ;
114 gh->data[0] = id_type;
115}
116
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200117/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
118static void gprs_put_mo_detach_acc(struct msgb *msg)
119{
120 struct gsm48_hdr *gh;
121
122 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
123 gh->proto_discr = GSM48_PDISC_MM_GPRS;
124 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
125 gh->data[0] = 0; /* no force to standby */
126}
127
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200128static void gprs_push_llc_ui(struct msgb *msg,
129 int is_uplink, unsigned sapi, unsigned nu)
130{
131 const uint8_t e_bit = 0;
132 const uint8_t pm_bit = 1;
133 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200134 uint8_t *llc;
135 uint8_t *fcs_field;
136 uint32_t fcs;
137
138 nu &= 0x01ff; /* 9 Bit */
139
140 llc = msgb_push(msg, 3);
141 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
142 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
143 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
144
145 fcs = gprs_llc_fcs(llc, msgb_length(msg));
146 fcs_field = msgb_put(msg, 3);
147 fcs_field[0] = (uint8_t)(fcs >> 0);
148 fcs_field[1] = (uint8_t)(fcs >> 8);
149 fcs_field[2] = (uint8_t)(fcs >> 16);
150}
151
152static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
153 uint32_t tlli)
154{
155 struct bssgp_ud_hdr *budh;
156 uint8_t *llc = msgb_data(msg);
157 size_t llc_size = msgb_length(msg);
158 const size_t llc_ie_hdr_size = 3;
159 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
160 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
161
162 const size_t bssgp_overhead = sizeof(*budh) +
163 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
164 uint8_t *ie;
165 uint32_t tlli_be = htonl(tlli);
166
167 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
168
169 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
170 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
171 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
172
173 ie = budh->data;
174 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
175 ie += TVLV_GROSS_LEN(sizeof(lifetime));
176
177 /* Note: Add alignment before the LLC IE if inserting other IE */
178
179 *(ie++) = BSSGP_IE_LLC_PDU;
180 *(ie++) = llc_size / 256;
181 *(ie++) = llc_size % 256;
182
183 OSMO_ASSERT(ie == llc);
184
185 msgb_bssgph(msg) = (uint8_t *)budh;
186 msgb_tlli(msg) = tlli;
187}
188
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200189/* update peer according to the BSS message */
190static void gbprox_update_current_raid(uint8_t *raid_enc,
191 struct gbproxy_peer *peer,
192 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200193{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200194 struct gbproxy_patch_state *state = &peer->patch_state;
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100195 const struct osmo_plmn_id old_plmn = state->local_plmn;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200196 struct gprs_ra_id raid;
197
198 if (!raid_enc)
199 return;
200
201 gsm48_parse_ra(&raid, raid_enc);
202
203 /* save source side MCC/MNC */
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100204 if (!peer->cfg->core_plmn.mcc || raid.mcc == peer->cfg->core_plmn.mcc) {
205 state->local_plmn.mcc = 0;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200206 } else {
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100207 state->local_plmn.mcc = raid.mcc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200208 }
209
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100210 if (!peer->cfg->core_plmn.mnc
211 || !osmo_mnc_cmp(raid.mnc, raid.mnc_3_digits,
212 peer->cfg->core_plmn.mnc, peer->cfg->core_plmn.mnc_3_digits)) {
213 state->local_plmn.mnc = 0;
214 state->local_plmn.mnc_3_digits = false;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200215 } else {
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100216 state->local_plmn.mnc = raid.mnc;
217 state->local_plmn.mnc_3_digits = raid.mnc_3_digits;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200218 }
219
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100220 if (osmo_plmn_cmp(&old_plmn, &state->local_plmn))
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200221 LOGP(DGPRS, LOGL_NOTICE,
222 "Patching RAID %sactivated, msg: %s, "
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100223 "local: %s, core: %s\n",
224 state->local_plmn.mcc || state->local_plmn.mnc ?
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200225 "" : "de",
226 log_text,
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100227 osmo_plmn_name(&state->local_plmn),
228 osmo_plmn_name2(&peer->cfg->core_plmn));
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200229}
230
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200231uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
232 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200233{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200234 uint32_t bss_ptmsi;
Max3b6332f2017-11-01 13:28:38 +0100235 int max_retries = 23, rc = 0;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200236 if (!peer->cfg->patch_ptmsi) {
237 bss_ptmsi = sgsn_ptmsi;
238 } else {
239 do {
Max3b6332f2017-11-01 13:28:38 +0100240 rc = osmo_get_rand_id((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi));
241 if (rc < 0) {
Daniel Willmann537d4802015-10-12 19:36:35 +0200242 bss_ptmsi = GSM_RESERVED_TMSI;
243 break;
244 }
245
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200246 bss_ptmsi = bss_ptmsi | 0xC0000000;
247
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200248 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200249 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200250 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200251 }
252
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200253 if (bss_ptmsi == GSM_RESERVED_TMSI)
Max3b6332f2017-11-01 13:28:38 +0100254 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI: %d (%s)\n", rc, strerror(-rc));
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200255
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200256 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200257}
258
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200259uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200260 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200261 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200262{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200263 uint32_t sgsn_tlli;
Max3b6332f2017-11-01 13:28:38 +0100264 int max_retries = 23, rc = 0;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200265 if (!peer->cfg->patch_ptmsi) {
266 sgsn_tlli = bss_tlli;
Jacob Erlbeck41108682014-10-31 10:47:29 +0100267 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
268 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200269 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200270 TLLI_FOREIGN);
Jacob Erlbeck41108682014-10-31 10:47:29 +0100271 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
272 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
273 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
274 TLLI_LOCAL);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200275 } else {
276 do {
277 /* create random TLLI, 0b01111xxx... */
Max3b6332f2017-11-01 13:28:38 +0100278 rc = osmo_get_rand_id((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli));
279 if (rc < 0) {
Daniel Willmann537d4802015-10-12 19:36:35 +0200280 sgsn_tlli = 0;
281 break;
282 }
283
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200284 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
285
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200286 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200287 sgsn_tlli = 0;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200288 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200289 }
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200290
291 if (!sgsn_tlli)
Max3b6332f2017-11-01 13:28:38 +0100292 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI: %d (%s)\n", rc, strerror(-rc));
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200293
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200294 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200295}
296
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +0200297void gbproxy_reset_link(struct gbproxy_link_info *link_info)
298{
299 gbproxy_reset_imsi_acquisition(link_info);
300}
301
Jacob Erlbeck31591142014-09-03 11:59:48 +0200302/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200303static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200304{
305 int in_progress = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200306 if (!link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200307 return 0;
308
Jacob Erlbeck65750282014-09-22 15:41:21 +0200309 if (link_info->imsi_acq_pending)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200310 in_progress = 1;
311
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200312 gbproxy_link_info_discard_messages(link_info);
313 link_info->imsi_acq_pending = 0;
Jacob Erlbeck31591142014-09-03 11:59:48 +0200314
315 return in_progress;
316}
317
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200318static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200319{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200320 gbproxy_restart_imsi_acquisition(link_info);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200321 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200322}
323
Daniel Willmannbeade312016-11-07 17:54:29 +0100324static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200325 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200326 time_t now,
Pau Espin Pedrolb0b1e3c2018-11-22 17:41:16 +0100327 struct gbproxy_link_info* link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200328{
329 int rc;
330 struct msgb *stored_msg;
331 /* Got identity response with IMSI, assuming the request had
332 * been generated by the gbproxy */
333
334 LOGP(DLLC, LOGL_DEBUG,
335 "NSEI=%d(BSS) IMSI acquisition succeeded, "
336 "flushing stored messages\n",
337 msgb_nsei(msg));
338
339 /* Patch and flush stored messages towards the SGSN */
Pau Espin Pedrol67f1d1e2018-08-16 12:11:46 +0200340 while ((stored_msg = msgb_dequeue_count(&link_info->stored_msgs,
341 &link_info->stored_msgs_len))) {
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200342 struct gprs_gb_parse_context tmp_parse_ctx = {0};
343 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200344 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200345 int len_change = 0;
346
347 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
348 msgb_bssgp_len(stored_msg),
349 &tmp_parse_ctx);
Pau Espin Pedrol8b672022018-11-22 17:25:49 +0100350 gbproxy_patch_bssgp(stored_msg, msgb_bssgph(stored_msg),
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200351 msgb_bssgp_len(stored_msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200352 peer, link_info, &len_change,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200353 &tmp_parse_ctx);
354
Daniel Willmannbeade312016-11-07 17:54:29 +0100355 rc = gbproxy_update_link_state_after(peer, link_info, now,
356 &tmp_parse_ctx);
357 if (rc == 1) {
358 LOGP(DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
359 msgb_free(stored_msg);
360 return -1;
361 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200362
363 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200364 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200365
366 if (rc < 0)
367 LOGP(DLLC, LOGL_ERROR,
368 "NSEI=%d(BSS) failed to send stored message "
369 "(%s)\n",
370 msgb_nsei(msg),
Pau Espin Pedrolb0b1e3c2018-11-22 17:41:16 +0100371 tmp_parse_ctx.llc_msg_name ?
372 tmp_parse_ctx.llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200373 msgb_free(stored_msg);
374 }
Daniel Willmannbeade312016-11-07 17:54:29 +0100375
376 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200377}
378
Jacob Erlbeck65750282014-09-22 15:41:21 +0200379static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
380 struct gbproxy_link_info* link_info,
381 uint16_t bvci,
382 struct msgb *msg /* Takes msg ownership */)
383{
384 int rc;
385
386 /* Workaround to avoid N(U) collisions and to enable a restart
387 * of the IMSI acquisition procedure. This will work unless the
388 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
389 * (see GSM 04.64, 8.4.2). */
390 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
391 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
392
393 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
394 rc = gbprox_relay2peer(msg, peer, bvci);
395 msgb_free(msg);
396 return rc;
397}
398
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200399static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200400 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200401 uint16_t bvci)
402{
403 struct msgb *idreq_msg;
404
405 /* Send IDENT REQ */
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100406 idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200407 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200408 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200409}
410
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200411static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200412 struct gbproxy_link_info* link_info,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200413 uint16_t bvci)
414{
415 struct msgb *detacc_msg;
416
417 /* Send DETACH ACC */
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100418 detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC");
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200419 gprs_put_mo_detach_acc(detacc_msg);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200420 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200421}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200422
423/* Return != 0 iff msg still needs to be processed */
424static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
425 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200426 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200427 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200428 struct gprs_gb_parse_context *parse_ctx)
429{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200430 struct msgb *stored_msg;
431
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200432 if (!link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200433 return 1;
434
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200435 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200436 return 1;
437
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200438 if (parse_ctx->g48_hdr)
439 switch (parse_ctx->g48_hdr->msg_type)
440 {
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200441 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200442 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200443 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200444 LOGP(DLLC, LOGL_INFO,
445 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200446 "when receiving an %s.\n",
447 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200448 }
449 break;
450 case GSM48_MT_GMM_DETACH_REQ:
451 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200452 if (link_info->imsi_acq_pending) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200453 LOGP(DLLC, LOGL_INFO,
454 "NSEI=%d(BSS) IMSI acquisition was in progress "
455 "when receiving a DETACH_REQ.\n",
456 msgb_nsei(msg));
457 }
458 if (!parse_ctx->invalidate_tlli) {
459 LOGP(DLLC, LOGL_INFO,
460 "NSEI=%d(BSS) IMSI not yet acquired, "
461 "faking a DETACH_ACC.\n",
462 msgb_nsei(msg));
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200463 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200464 parse_ctx->invalidate_tlli = 1;
465 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200466 gbproxy_reset_imsi_acquisition(link_info);
467 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200468 parse_ctx);
469 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200470 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200471
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200472 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200473 int is_ident_resp =
474 parse_ctx->g48_hdr &&
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100475 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
476 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200477
Daniel Willmannbeade312016-11-07 17:54:29 +0100478 /* The IMSI is now available. If flushing the messages fails,
479 * then link_info has been deleted and we should return
480 * immediately. */
Pau Espin Pedrolb0b1e3c2018-11-22 17:41:16 +0100481 if (gbproxy_flush_stored_messages(peer, msg, now, link_info) < 0)
Daniel Willmannbeade312016-11-07 17:54:29 +0100482 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200483
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200484 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200485
486 /* This message is most probably the response to the ident
487 * request sent by gbproxy_acquire_imsi(). Don't forward it to
488 * the SGSN. */
489 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200490 }
491
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200492 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200493
Pau Espin Pedrol67f1d1e2018-08-16 12:11:46 +0200494 /* If queue is getting too large, drop oldest msgb before adding new one */
495 if (peer->cfg->stored_msgs_max_len > 0) {
496 int exceeded_max_len = link_info->stored_msgs_len
497 + 1 - peer->cfg->stored_msgs_max_len;
498
499 for (; exceeded_max_len > 0; exceeded_max_len--) {
500 struct msgb *msgb_drop;
501 msgb_drop = msgb_dequeue_count(&link_info->stored_msgs,
502 &link_info->stored_msgs_len);
503 LOGP(DLLC, LOGL_INFO,
504 "NSEI=%d(BSS) Dropping stored msgb from list "
505 "(!acq imsi, length %d, max_len exceeded)\n",
506 msgb_nsei(msgb_drop), link_info->stored_msgs_len);
507
508 msgb_free(msgb_drop);
509 }
510 }
511
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200512 /* Enqueue unpatched messages */
513 LOGP(DLLC, LOGL_INFO,
514 "NSEI=%d(BSS) IMSI acquisition in progress, "
515 "storing message (%s)\n",
516 msgb_nsei(msg),
517 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200518
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +0200519 stored_msg = bssgp_msgb_copy(msg, "process_bssgp_ul");
Pau Espin Pedrol67f1d1e2018-08-16 12:11:46 +0200520 msgb_enqueue_count(&link_info->stored_msgs, stored_msg,
521 &link_info->stored_msgs_len);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200522
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200523 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200524 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200525 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200526 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200527 msgb_nsei(msg),
528 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
529
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200530 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200531
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200532 /* There is no explicit retransmission handling, the
533 * implementation relies on the MS doing proper retransmissions
534 * of the triggering message instead */
535
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200536 link_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200537 }
538
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200539 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200540}
541
542struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
543 struct msgb *msg,
544 struct gprs_gb_parse_context *parse_ctx)
545{
546 struct gbproxy_peer *peer = NULL;
547
548 if (msgb_bvci(msg) >= 2)
549 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
550
551 if (!peer && !parse_ctx->to_bss)
552 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
553
554 if (!peer)
555 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
556
557 if (!peer) {
558 LOGP(DLLC, LOGL_INFO,
559 "NSEI=%d(%s) patching: didn't find peer for message, "
560 "PDU %d\n",
561 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
562 parse_ctx->pdu_type);
563 /* Increment counter */
564 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
565 }
566 return peer;
567}
568
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200569/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200570static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
571 struct msgb *msg,
572 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200573{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200574 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200575 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200576 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200577 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200578 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200579 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200580 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200581
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100582 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200583 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200584 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200585
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200586 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200587 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200588
589 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200590 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
591 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200592
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200593 if (!rc && !parse_ctx.need_decryption) {
594 LOGP(DGPRS, LOGL_ERROR,
595 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
596 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
597 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
598 LOGP(DGPRS, LOGL_NOTICE,
599 "NSEI=%u(BSS) invalid message was: %s\n",
600 msgb_nsei(msg), msgb_hexdump(msg));
601 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200602 }
603
604 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200605 if (!peer)
606 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200607
608 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200609 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200610
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200611
Pau Espin Pedrol36abead2018-08-17 13:27:20 +0200612 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200613 now = ts.tv_sec;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200614
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200615 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
616 parse_ctx.llc_msg_name);
617
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200618 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck31591142014-09-03 11:59:48 +0200619
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200620 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200621
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200622 if (parse_ctx.g48_hdr) {
623 switch (parse_ctx.g48_hdr->msg_type) {
624 case GSM48_MT_GMM_ATTACH_REQ:
625 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
626 break;
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100627 case GSM48_MT_GMM_DETACH_REQ:
628 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
629 break;
630 case GSM48_MT_GMM_ATTACH_COMPL:
631 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
632 break;
633 case GSM48_MT_GMM_RA_UPD_REQ:
634 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
635 break;
636 case GSM48_MT_GMM_RA_UPD_COMPL:
637 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
638 break;
639 case GSM48_MT_GMM_STATUS:
640 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
641 break;
642 case GSM48_MT_GSM_ACT_PDP_REQ:
643 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
644 break;
645 case GSM48_MT_GSM_DEACT_PDP_REQ:
646 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
647 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200648
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200649 default:
650 break;
651 }
652 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200653
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200654 if (link_info && cfg->route_to_sgsn2) {
655 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200656 sgsn_nsei = 0xffff;
Jacob Erlbeckb36032c2014-09-25 13:21:48 +0200657 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200658 link_info))
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200659 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200660 }
661
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200662 if (link_info)
663 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200664
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200665 /* Handle IMSI acquisition */
666 if (cfg->acquire_imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200667 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200668 &parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200669 if (rc <= 0)
670 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200671 }
672
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200673 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200674 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200675
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200676 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200677
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200678 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200679 /* Send message directly to the selected SGSN */
680 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
681 /* Don't let the calling code handle the transmission */
682 return 0;
683 }
684
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200685 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200686}
687
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100688/* patch BSSGP message to use core_plmn.mcc/mnc on the SGSN side */
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200689static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
690 struct msgb *msg,
691 struct gbproxy_peer *peer)
692{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200693 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200694 int rc;
695 int len_change = 0;
696 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200697 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200698 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200699
Neels Hofmeyr10719b72018-02-21 00:39:36 +0100700 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200701 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
702 return;
703
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200704 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200705 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200706
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200707 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
708 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200709
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200710 if (!rc && !parse_ctx.need_decryption) {
711 LOGP(DGPRS, LOGL_ERROR,
712 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
713 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
714 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
715 LOGP(DGPRS, LOGL_NOTICE,
716 "NSEI=%u(SGSN) invalid message was: %s\n",
717 msgb_nsei(msg), msgb_hexdump(msg));
718 return;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200719 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200720
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200721 /* Get peer */
722 if (!peer)
723 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200724
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200725 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200726 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200727
Pau Espin Pedrol36abead2018-08-17 13:27:20 +0200728 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200729 now = ts.tv_sec;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200730
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200731 if (parse_ctx.g48_hdr) {
732 switch (parse_ctx.g48_hdr->msg_type) {
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100733 case GSM48_MT_GMM_ATTACH_ACK:
734 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
735 break;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200736 case GSM48_MT_GMM_ATTACH_REJ:
737 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
738 break;
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100739 case GSM48_MT_GMM_DETACH_ACK:
740 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
741 break;
742 case GSM48_MT_GMM_RA_UPD_ACK:
743 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
744 break;
745 case GSM48_MT_GMM_RA_UPD_REJ:
746 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
747 break;
748 case GSM48_MT_GMM_STATUS:
749 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
750 break;
751 case GSM48_MT_GSM_ACT_PDP_ACK:
752 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
753 break;
754 case GSM48_MT_GSM_ACT_PDP_REJ:
755 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
756 break;
757 case GSM48_MT_GSM_DEACT_PDP_ACK:
758 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
759 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200760
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200761 default:
762 break;
763 }
764 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200765
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200766 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200767
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200768 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200769
770 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200771 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200772
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200773 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200774
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200775 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200776}
777
Harald Welte9f75c352010-04-30 20:26:32 +0200778/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200779static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200780 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200781{
Harald Welte39d0bb52010-05-12 18:10:25 +0000782 /* create a copy of the message so the old one can
783 * be free()d safely when we return from gbprox_rcvmsg() */
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +0200784 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200785 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000786
Harald Weltee9ea2692010-05-11 20:20:13 +0200787 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200788 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200789
Harald Welte672f5c42010-05-03 18:54:58 +0200790 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200791 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200792
Harald Welte69619e32010-05-03 19:05:10 +0200793 strip_ns_hdr(msg);
794
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200795 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
796 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200797 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200798
799 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200800}
801
Harald Welte672f5c42010-05-03 18:54:58 +0200802/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200803static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200804 uint16_t ns_bvci)
805{
Harald Welte39d0bb52010-05-12 18:10:25 +0000806 /* create a copy of the message so the old one can
807 * be free()d safely when we return from gbprox_rcvmsg() */
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +0200808 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200809 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000810
Harald Welte0ab535b2010-05-13 10:34:56 +0200811 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200812 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200813
Harald Welte9f75c352010-04-30 20:26:32 +0200814 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200815 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200816
Harald Welte0ab535b2010-05-13 10:34:56 +0200817 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200818 strip_ns_hdr(msg);
819
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200820 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
821 if (rc < 0)
822 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
823
824 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200825}
826
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200827static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100828{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200829 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100830
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200831 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100832 if (!peer) {
833 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
834 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200835 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100836 return -ENOENT;
837 }
838
839 switch (pdu_type) {
840 case BSSGP_PDUT_BVC_BLOCK_ACK:
841 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200842 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100843 break;
844 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
845 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200846 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100847 break;
848 default:
849 break;
850 }
851 return 0;
852}
853
Harald Welte9f75c352010-04-30 20:26:32 +0200854/* Send a message to a peer identified by ptp_bvci but using ns_bvci
855 * in the NS hdr */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200856static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200857 uint16_t ns_bvci)
858{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200859 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200860
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200861 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200862 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200863 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200864 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200865 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200866 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200867 }
Harald Welte9f75c352010-04-30 20:26:32 +0200868
Harald Welte69619e32010-05-03 19:05:10 +0200869 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200870}
871
Harald Welteb1fd9022012-06-17 12:16:31 +0800872int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
873{
874 return 0;
875}
876
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200877/* Receive an incoming PTP message from a BSS-side NS-VC */
878static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
879 struct msgb *msg, uint16_t nsei,
880 uint16_t nsvci, uint16_t ns_bvci)
881{
882 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200883 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
884 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200885 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200886
887 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200888 if (!peer) {
889 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
890 "BVCI=%u for PTP message from NSVC=%u/NSEI=%u (BSS), "
891 "discarding message\n",
892 ns_bvci, nsvci, nsei);
893 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
894 &ns_bvci, msg);
895 }
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200896
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200897 check_peer_nsei(peer, nsei);
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200898
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200899 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
900 if (!rc)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200901 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200902
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200903 switch (pdu_type) {
904 case BSSGP_PDUT_FLOW_CONTROL_BVC:
905 if (!cfg->route_to_sgsn2)
906 break;
907
908 /* Send a copy to the secondary SGSN */
909 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
910 break;
911 default:
912 break;
913 }
914
915
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200916 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200917}
918
919/* Receive an incoming PTP message from a SGSN-side NS-VC */
920static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
921 struct msgb *msg, uint16_t nsei,
922 uint16_t nsvci, uint16_t ns_bvci)
923{
924 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200925 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
926 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200927
928 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
929
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200930 /* Send status messages before patching */
931
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200932 if (!peer) {
933 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
934 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
935 ns_bvci, nsvci, nsei);
936 rate_ctr_inc(&cfg->ctrg->
937 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
938 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
939 &ns_bvci, msg);
940 }
941
942 if (peer->blocked) {
943 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
944 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
945 ns_bvci, nsvci, nsei);
946 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200947 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200948 }
949
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200950 switch (pdu_type) {
951 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
952 case BSSGP_PDUT_BVC_BLOCK_ACK:
953 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
954 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
955 /* Hide ACKs from the secondary SGSN, the primary SGSN
956 * is responsible to send them. */
957 return 0;
958 break;
959 default:
960 break;
961 }
962
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200963 /* Optionally patch the message */
964 gbprox_process_bssgp_dl(cfg, msg, peer);
965
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200966 return gbprox_relay2peer(msg, peer, ns_bvci);
967}
968
Harald Welte9f75c352010-04-30 20:26:32 +0200969/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200970static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
971 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200972 uint16_t ns_bvci)
973{
Harald Welteca3620a2010-05-03 16:30:59 +0200974 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200975 struct tlv_parsed tp;
976 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200977 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200978 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200979 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200980 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200981 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200982
Harald Weltec471d3d2011-02-06 17:13:12 +0100983 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200984 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200985 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200986 return -EINVAL;
987 }
988
989 /* we actually should never see those two for BVCI == 0, but double-check
990 * just to make sure */
991 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
992 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200993 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200994 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200995 return -EINVAL;
996 }
997
998 bssgp_tlv_parse(&tp, bgph->data, data_len);
999
1000 switch (pdu_type) {
1001 case BSSGP_PDUT_SUSPEND:
1002 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001003 /* We implement RAI snooping during SUSPEND/RESUME, since it
1004 * establishes a relationsip between BVCI/peer and the routeing
1005 * area identification. The snooped information is then used
1006 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
1007 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +02001008 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1009 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001010 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001011 if (!from_peer)
1012 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +02001013 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
1014 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +02001015 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +02001016 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Neels Hofmeyr10719b72018-02-21 00:39:36 +01001017 "RAI snooping: RAI %s behind BVCI=%u\n",
1018 nsei, osmo_rai_name(&raid), from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001019 /* FIXME: This only supports one BSS per RA */
1020 break;
Harald Welte44c48302010-05-03 19:22:32 +02001021 case BSSGP_PDUT_BVC_RESET:
1022 /* If we receive a BVC reset on the signalling endpoint, we
1023 * don't want the SGSN to reset, as the signalling endpoint
1024 * is common for all point-to-point BVCs (and thus all BTS) */
1025 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001026 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +02001027 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001028 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +02001029 if (bvci == 0) {
1030 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +02001031 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001032 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +02001033 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001034 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +02001035 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001036 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +02001037 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +02001038 /* if a PTP-BVC is reset, and we don't know that
1039 * PTP-BVCI yet, we should allocate a new peer */
Max79605f62017-09-18 16:10:18 +02001040 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001041 from_peer = gbproxy_peer_alloc(cfg, bvci);
Max79605f62017-09-18 16:10:18 +02001042 OSMO_ASSERT(from_peer);
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001043 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +02001044 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +02001045
1046 if (!check_peer_nsei(from_peer, nsei))
1047 from_peer->nsei = nsei;
1048
Harald Welte1174c082010-05-12 00:07:29 +02001049 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
1050 struct gprs_ra_id raid;
1051 /* We have a Cell Identifier present in this
1052 * PDU, this means we can extend our local
1053 * state information about this particular cell
1054 * */
1055 memcpy(from_peer->ra,
1056 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
1057 sizeof(from_peer->ra));
1058 gsm48_parse_ra(&raid, from_peer->ra);
Neels Hofmeyr10719b72018-02-21 00:39:36 +01001059 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u Cell ID %s\n",
1060 nsei, bvci, osmo_rai_name(&raid));
Harald Welte1174c082010-05-12 00:07:29 +02001061 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001062 if (cfg->route_to_sgsn2)
1063 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +02001064 }
1065 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001066 }
1067
Harald Weltee9ea2692010-05-11 20:20:13 +02001068 /* Normally, we can simply pass on all signalling messages from BSS to
1069 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +02001070 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1071 if (!rc)
1072 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001073
1074 if (copy_to_sgsn2)
1075 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1076
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001077 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001078err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +02001079 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001080 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001081 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001082 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001083err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +02001084 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001085 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001086 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001087 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001088}
1089
1090/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001091static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001092 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001093{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001094 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001095 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +02001096
Harald Welte4cf12e92010-05-13 14:14:56 +02001097 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001098 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001099 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001100 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +02001101 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
1102 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001103 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +02001104 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001105 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001106 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001107 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001108 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001109 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001110 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001111 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001112 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001113 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001114 } else
Harald Welte4cf12e92010-05-13 14:14:56 +02001115 LOGPC(DGPRS, LOGL_INFO, "\n");
1116
1117 if (!peer) {
1118 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001119 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001120 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +02001121 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001122 }
1123 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001124}
1125
Harald Welte0a4050c2010-05-11 10:01:17 +02001126/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001127static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001128 struct msgb *orig_msg,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001129 struct msgb *msg, struct tlv_parsed *tp,
1130 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001131{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001132 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001133 uint16_t ptp_bvci;
1134
1135 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001136 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001137 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001138 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001139 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001140 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001141 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001142
1143 if (ptp_bvci >= 2) {
1144 /* A reset for a PTP BVC was received, forward it to its
1145 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001146 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001147 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001148 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001149 nsei, ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001150 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001151 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001152 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001153 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001154 }
1155 return gbprox_relay2peer(msg, peer, ns_bvci);
1156 }
1157
1158 /* A reset for the Signalling entity has been received
1159 * from the SGSN. As the signalling BVCI is shared
1160 * among all the BSS's that we multiplex, it needs to
1161 * be relayed */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001162 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001163 gbprox_relay2peer(msg, peer, ns_bvci);
1164
1165 return 0;
1166}
1167
Harald Welte9f75c352010-04-30 20:26:32 +02001168/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001169static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001170 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001171 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001172{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001173 struct bssgp_normal_hdr *bgph =
1174 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001175 struct tlv_parsed tp;
1176 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001177 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001178 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001179 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001180 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001181 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001182 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001183
Harald Weltec471d3d2011-02-06 17:13:12 +01001184 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001185 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001186 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001187 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001188 return -EINVAL;
1189 }
1190
1191 /* we actually should never see those two for BVCI == 0, but double-check
1192 * just to make sure */
1193 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1194 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001195 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001196 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001197 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001198 }
1199
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +02001200 msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn");
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001201 gbprox_process_bssgp_dl(cfg, msg, NULL);
1202 /* Update message info */
1203 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1204 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001205 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1206
1207 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001208 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001209 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001210 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001211 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001212 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1213 break;
1214 /* fall through */
1215 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001216 /* simple case: BVCI IE is mandatory */
1217 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1218 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001219 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001220 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001221 break;
1222 case BSSGP_PDUT_PAGING_PS:
1223 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001224 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001225 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001226 break;
1227 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001228 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001229 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001230 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001231 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1232 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1233 goto err_mand_ie;
1234 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001235 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001236 LOGPC(DGPRS, LOGL_NOTICE,
1237 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001238 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001239 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001240 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1241 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1242
1243 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1244 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001245 } else
1246 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001247 break;
1248 /* those only exist in the SGSN -> BSS direction */
1249 case BSSGP_PDUT_SUSPEND_ACK:
1250 case BSSGP_PDUT_SUSPEND_NACK:
1251 case BSSGP_PDUT_RESUME_ACK:
1252 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001253 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001254 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1255 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001256 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001257 if (!peer)
1258 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001259 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001260 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001261 case BSSGP_PDUT_BVC_BLOCK_ACK:
1262 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1263 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1264 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001265 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001266 if (bvci == 0) {
1267 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001268 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001269 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1270 /* should we send STATUS ? */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001271 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001272 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001273 } else {
1274 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001275 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001276 }
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001277 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001278 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001279 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001280 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001281 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001282 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001283 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001284 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001285 break;
1286 default:
Max537da6c2017-08-17 19:33:11 +02001287 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type %s not supported\n", bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001288 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001289 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001290 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001291 break;
1292 }
1293
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001294 msgb_free(msg);
1295
Harald Welte9f75c352010-04-30 20:26:32 +02001296 return rc;
1297err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001298 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001299 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001300 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001301 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001302 msgb_free(msg);
1303 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001304err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001305 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001306 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001307 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001308 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001309 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001310}
1311
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001312static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1313{
1314 return nsei == cfg->nsip_sgsn_nsei ||
1315 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1316}
1317
Harald Welte9f75c352010-04-30 20:26:32 +02001318/* Main input function for Gb proxy */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001319int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1320 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001321{
Harald Welte672f5c42010-05-03 18:54:58 +02001322 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001323 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001324
1325 /* Only BVCI=0 messages need special treatment */
1326 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001327 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001328 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001329 else
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001330 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001331 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001332 /* All other BVCI are PTP */
1333 if (remote_end_is_sgsn)
1334 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1335 ns_bvci);
1336 else
1337 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1338 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001339 }
1340
Harald Welte672f5c42010-05-03 18:54:58 +02001341 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001342}
Harald Welte85801d02010-05-11 05:49:43 +02001343
Harald Welte1ccbf442010-05-14 11:53:08 +00001344int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1345{
1346 struct gprs_nsvc *nsvc;
1347
1348 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1349 if (!nsvc->persistent)
1350 continue;
1351 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1352 }
1353 return 0;
1354}
1355
Harald Weltec1c1dd22010-05-11 06:34:24 +02001356/* Signal handler for signals from NS layer */
1357int gbprox_signal(unsigned int subsys, unsigned int signal,
1358 void *handler_data, void *signal_data)
1359{
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001360 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001361 struct ns_signal_data *nssd = signal_data;
1362 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001363 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001364 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001365
Harald Weltea6a20b42012-06-16 16:40:42 +08001366 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001367 return 0;
1368
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001369 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001370 /* We have received a NS-RESET from the NSEI and NSVC
1371 * of the SGSN. This might happen with SGSN that start
1372 * their own NS-RESET procedure without waiting for our
1373 * NS-RESET */
1374 nsvc->remote_end_is_sgsn = 1;
1375 }
1376
Harald Welteb778d2c2010-05-12 13:28:25 +00001377 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1378 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1379 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001380 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001381 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001382 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1383 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001384 }
1385
Harald Welte5e106d72011-02-06 16:33:29 +01001386 if (!nsvc->remote_end_is_sgsn) {
1387 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001388 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001389 if (!peer) {
Maxa441e9d2017-08-18 16:23:28 +02001390 LOGP(DGPRS, LOGL_NOTICE, "signal '%s' for unknown peer NSEI=%u/NSVCI=%u\n",
1391 get_value_string(gprs_ns_signal_ns_names, signal), nsvc->nsei, nsvc->nsvci);
Harald Welte5e106d72011-02-06 16:33:29 +01001392 return 0;
1393 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001394 switch (signal) {
1395 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001396 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001397 if (!peer->blocked)
1398 break;
Maxa441e9d2017-08-18 16:23:28 +02001399 LOGP(DGPRS, LOGL_NOTICE, "Converting '%s' from NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1400 get_value_string(gprs_ns_signal_ns_names, signal), nsvc->nsei, nsvc->nsvci);
Harald Welte5e106d72011-02-06 16:33:29 +01001401 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1402 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001403 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001404 }
1405 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001406 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001407 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001408 struct gprs_nsvc *next_nsvc;
1409
1410 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1411 if (next_nsvc->remote_end_is_sgsn)
1412 continue;
1413
1414 /* Note that the following does not start the full
1415 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001416 switch (signal) {
1417 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001418 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001419 break;
1420 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001421 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001422 break;
1423 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001424 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001425 break;
1426 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001427 }
1428 }
1429 return 0;
1430}
1431
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001432void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001433{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001434 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001435
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001436 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001437 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001438
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001439 rate_ctr_group_free(cfg->ctrg);
1440 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001441}
1442
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001443int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001444{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001445 struct timespec tp;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001446
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001447 INIT_LLIST_HEAD(&cfg->bts_peers);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +01001448 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
Harald Welte26c14652017-07-12 00:25:51 +02001449 if (!cfg->ctrg) {
1450 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1451 return -1;
1452 }
Pau Espin Pedrol36abead2018-08-17 13:27:20 +02001453 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001454
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001455 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001456}