blob: 0d79ff7eb0ba25b13453caeda1d20dc5001575de [file] [log] [blame]
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
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
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
16 * GNU Affero General Public License for more details.
17 *
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/>.
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>
31#include <arpa/inet.h>
32#include <time.h>
33
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
36#include <osmocom/core/rate_ctr.h>
37#include <osmocom/core/stats.h>
38
Alexander Couzens951e1332020-09-22 13:21:46 +020039#include <osmocom/gprs/gprs_ns2.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020040#include <osmocom/gprs/gprs_bssgp.h>
Alexander Couzens951e1332020-09-22 13:21:46 +020041#include <osmocom/gprs/gprs_bssgp_bss.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020042
43#include <osmocom/gsm/gsm_utils.h>
44
45#include <osmocom/sgsn/signal.h>
46#include <osmocom/sgsn/debug.h>
47#include <osmocom/sgsn/gprs_gb_parse.h>
48#include <osmocom/sgsn/gb_proxy.h>
49
50#include <osmocom/sgsn/gprs_llc.h>
51#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
52#include <osmocom/sgsn/gprs_utils.h>
53
54extern void *tall_sgsn_ctx;
55
56static const struct rate_ctr_desc global_ctr_description[] = {
57 { "inv-bvci", "Invalid BVC Identifier " },
58 { "inv-lai", "Invalid Location Area Identifier" },
59 { "inv-rai", "Invalid Routing Area Identifier " },
60 { "inv-nsei", "No BVC established for NSEI " },
61 { "proto-err:bss", "BSSGP protocol error (BSS )" },
62 { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
63 { "not-supp:bss", "Feature not supported (BSS )" },
64 { "not-supp:sgsn", "Feature not supported (SGSN)" },
65 { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
66 { "tx-err:sgsn", "NS Transmission error (SGSN)" },
67 { "error", "Other error " },
68 { "mod-peer-err", "Patch error: no peer " },
69};
70
71static const struct rate_ctr_group_desc global_ctrg_desc = {
72 .group_name_prefix = "gbproxy:global",
73 .group_description = "GBProxy Global Statistics",
74 .num_ctr = ARRAY_SIZE(global_ctr_description),
75 .ctr_desc = global_ctr_description,
76 .class_id = OSMO_STATS_CLASS_GLOBAL,
77};
78
79static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Daniel Willmann35f7d332020-11-03 21:11:45 +010080 uint16_t ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020081static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
82 uint16_t ns_bvci, uint16_t sgsn_nsei);
83static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
84
85static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
86{
Daniel Willmann5e595ca2020-12-02 13:54:21 +010087 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +010088 OSMO_ASSERT(peer->nse);
89
90 if (peer->nse->nsei != nsei) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +010091 LOGPBVC(peer, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
92 "via NSE(%05u/BSS)\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020093 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
94 return 0;
95 }
96
97 return 1;
98}
99
100/* strip off the NS header */
101static void strip_ns_hdr(struct msgb *msg)
102{
103 int strip_len = msgb_bssgph(msg) - msg->data;
104 msgb_pull(msg, strip_len);
105}
106
107/* Transmit Chapter 9.2.10 Identity Request */
108static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
109{
110 struct gsm48_hdr *gh;
111
112 id_type &= GSM_MI_TYPE_MASK;
113
114 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
115 gh->proto_discr = GSM48_PDISC_MM_GPRS;
116 gh->msg_type = GSM48_MT_GMM_ID_REQ;
117 gh->data[0] = id_type;
118}
119
120/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
121static void gprs_put_mo_detach_acc(struct msgb *msg)
122{
123 struct gsm48_hdr *gh;
124
125 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
126 gh->proto_discr = GSM48_PDISC_MM_GPRS;
127 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
128 gh->data[0] = 0; /* no force to standby */
129}
130
131static void gprs_push_llc_ui(struct msgb *msg,
132 int is_uplink, unsigned sapi, unsigned nu)
133{
134 const uint8_t e_bit = 0;
135 const uint8_t pm_bit = 1;
136 const uint8_t cr_bit = is_uplink ? 0 : 1;
137 uint8_t *llc;
138 uint8_t *fcs_field;
139 uint32_t fcs;
140
141 nu &= 0x01ff; /* 9 Bit */
142
143 llc = msgb_push(msg, 3);
144 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
145 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
146 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
147
148 fcs = gprs_llc_fcs(llc, msgb_length(msg));
149 fcs_field = msgb_put(msg, 3);
150 fcs_field[0] = (uint8_t)(fcs >> 0);
151 fcs_field[1] = (uint8_t)(fcs >> 8);
152 fcs_field[2] = (uint8_t)(fcs >> 16);
153}
154
155static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
156 uint32_t tlli)
157{
158 struct bssgp_ud_hdr *budh;
159 uint8_t *llc = msgb_data(msg);
160 size_t llc_size = msgb_length(msg);
161 const size_t llc_ie_hdr_size = 3;
162 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
163 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
164
165 const size_t bssgp_overhead = sizeof(*budh) +
166 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
167 uint8_t *ie;
168 uint32_t tlli_be = htonl(tlli);
169
170 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
171
172 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
173 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
174 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
175
176 ie = budh->data;
177 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
178 ie += TVLV_GROSS_LEN(sizeof(lifetime));
179
180 /* Note: Add alignment before the LLC IE if inserting other IE */
181
182 *(ie++) = BSSGP_IE_LLC_PDU;
183 *(ie++) = llc_size / 256;
184 *(ie++) = llc_size % 256;
185
186 OSMO_ASSERT(ie == llc);
187
188 msgb_bssgph(msg) = (uint8_t *)budh;
189 msgb_tlli(msg) = tlli;
190}
191
192/* update peer according to the BSS message */
193static void gbprox_update_current_raid(uint8_t *raid_enc,
194 struct gbproxy_peer *peer,
195 const char *log_text)
196{
197 struct gbproxy_patch_state *state = &peer->patch_state;
198 const struct osmo_plmn_id old_plmn = state->local_plmn;
199 struct gprs_ra_id raid;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100200 OSMO_ASSERT(peer->nse);
201 struct gbproxy_config *cfg = peer->nse->cfg;
202 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200203
204 if (!raid_enc)
205 return;
206
207 gsm48_parse_ra(&raid, raid_enc);
208
209 /* save source side MCC/MNC */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100210 if (!cfg->core_plmn.mcc || raid.mcc == cfg->core_plmn.mcc) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200211 state->local_plmn.mcc = 0;
212 } else {
213 state->local_plmn.mcc = raid.mcc;
214 }
215
Daniel Willmanne50550e2020-11-26 18:19:21 +0100216 if (!cfg->core_plmn.mnc
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200217 || !osmo_mnc_cmp(raid.mnc, raid.mnc_3_digits,
Daniel Willmanne50550e2020-11-26 18:19:21 +0100218 cfg->core_plmn.mnc, cfg->core_plmn.mnc_3_digits)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200219 state->local_plmn.mnc = 0;
220 state->local_plmn.mnc_3_digits = false;
221 } else {
222 state->local_plmn.mnc = raid.mnc;
223 state->local_plmn.mnc_3_digits = raid.mnc_3_digits;
224 }
225
226 if (osmo_plmn_cmp(&old_plmn, &state->local_plmn))
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100227 LOGPBVC(peer, LOGL_NOTICE,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200228 "Patching RAID %sactivated, msg: %s, "
229 "local: %s, core: %s\n",
230 state->local_plmn.mcc || state->local_plmn.mnc ?
231 "" : "de",
232 log_text,
233 osmo_plmn_name(&state->local_plmn),
Daniel Willmanne50550e2020-11-26 18:19:21 +0100234 osmo_plmn_name2(&cfg->core_plmn));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200235}
236
237uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
238 uint32_t sgsn_ptmsi)
239{
240 uint32_t bss_ptmsi;
241 int max_retries = 23, rc = 0;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100242 if (!peer->nse->cfg->patch_ptmsi) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200243 bss_ptmsi = sgsn_ptmsi;
244 } else {
245 do {
246 rc = osmo_get_rand_id((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi));
247 if (rc < 0) {
248 bss_ptmsi = GSM_RESERVED_TMSI;
249 break;
250 }
251
252 bss_ptmsi = bss_ptmsi | GSM23003_TMSI_SGSN_MASK;
253
254 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
255 bss_ptmsi = GSM_RESERVED_TMSI;
256 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
257 }
258
259 if (bss_ptmsi == GSM_RESERVED_TMSI)
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100260 LOGPBVC(peer, LOGL_ERROR, "Failed to allocate a BSS P-TMSI: %d (%s)\n", rc, strerror(-rc));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200261
262 return bss_ptmsi;
263}
264
265uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
266 struct gbproxy_link_info *link_info,
267 uint32_t bss_tlli)
268{
269 uint32_t sgsn_tlli;
270 int max_retries = 23, rc = 0;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100271 if (!peer->nse->cfg->patch_ptmsi) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200272 sgsn_tlli = bss_tlli;
273 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
274 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
275 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
276 TLLI_FOREIGN);
277 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
278 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
279 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
280 TLLI_LOCAL);
281 } else {
282 do {
283 /* create random TLLI, 0b01111xxx... */
284 rc = osmo_get_rand_id((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli));
285 if (rc < 0) {
286 sgsn_tlli = 0;
287 break;
288 }
289
290 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
291
292 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
293 sgsn_tlli = 0;
294 } while (!sgsn_tlli && max_retries--);
295 }
296
297 if (!sgsn_tlli)
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100298 LOGPBVC(peer, LOGL_ERROR, "Failed to allocate an SGSN TLLI: %d (%s)\n", rc, strerror(-rc));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200299
300 return sgsn_tlli;
301}
302
303void gbproxy_reset_link(struct gbproxy_link_info *link_info)
304{
305 gbproxy_reset_imsi_acquisition(link_info);
306}
307
308/* Returns != 0 iff IMSI acquisition was in progress */
309static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
310{
311 int in_progress = 0;
312 if (!link_info)
313 return 0;
314
315 if (link_info->imsi_acq_pending)
316 in_progress = 1;
317
318 gbproxy_link_info_discard_messages(link_info);
319 link_info->imsi_acq_pending = false;
320
321 return in_progress;
322}
323
324static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
325{
326 gbproxy_restart_imsi_acquisition(link_info);
327 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
328}
329
330/* Got identity response with IMSI, assuming the request had
331 * been generated by the gbproxy */
332static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
333 time_t now,
334 struct gbproxy_link_info* link_info)
335{
336 int rc;
337 struct msgb *stored_msg;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100338 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100339 OSMO_ASSERT(peer->nse);
340 struct gbproxy_config *cfg = peer->nse->cfg;
341 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200342
343 /* Patch and flush stored messages towards the SGSN */
344 while ((stored_msg = msgb_dequeue_count(&link_info->stored_msgs,
345 &link_info->stored_msgs_len))) {
346 struct gprs_gb_parse_context tmp_parse_ctx = {0};
347 tmp_parse_ctx.to_bss = 0;
348 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
349 int len_change = 0;
350
351 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
352 msgb_bssgp_len(stored_msg),
353 &tmp_parse_ctx);
354 gbproxy_patch_bssgp(stored_msg, msgb_bssgph(stored_msg),
355 msgb_bssgp_len(stored_msg),
356 peer, link_info, &len_change,
357 &tmp_parse_ctx);
358
359 rc = gbproxy_update_link_state_after(peer, link_info, now,
360 &tmp_parse_ctx);
361 if (rc == 1) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100362 LOGPBVC_CAT(peer, DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200363 msgb_free(stored_msg);
364 return -1;
365 }
366
Daniel Willmanne50550e2020-11-26 18:19:21 +0100367 rc = gbprox_relay2sgsn(cfg, stored_msg,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200368 msgb_bvci(stored_msg), link_info->sgsn_nsei);
369
370 if (rc < 0)
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100371 LOGPBVC_CAT(peer, DLLC, LOGL_ERROR,
372 "failed to send stored message "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200373 "(%s)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200374 tmp_parse_ctx.llc_msg_name ?
375 tmp_parse_ctx.llc_msg_name : "BSSGP");
376 msgb_free(stored_msg);
377 }
378
379 return 0;
380}
381
382static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
383 struct gbproxy_link_info* link_info,
384 uint16_t bvci,
385 struct msgb *msg /* Takes msg ownership */)
386{
387 int rc;
388
389 /* Workaround to avoid N(U) collisions and to enable a restart
390 * of the IMSI acquisition procedure. This will work unless the
391 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
392 * (see GSM 04.64, 8.4.2). */
393 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
394 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
395
396 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
Alexander Couzens951e1332020-09-22 13:21:46 +0200397 msg->l3h = msg->data;
398
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200399 rc = gbprox_relay2peer(msg, peer, bvci);
400 msgb_free(msg);
401 return rc;
402}
403
404static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
405 struct gbproxy_link_info* link_info,
406 uint16_t bvci)
407{
408 struct msgb *idreq_msg;
409
410 /* Send IDENT REQ */
411 idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI");
412 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
413 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
414}
415
416static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
417 struct gbproxy_link_info* link_info,
418 uint16_t bvci)
419{
420 struct msgb *detacc_msg;
421
422 /* Send DETACH ACC */
423 detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC");
424 gprs_put_mo_detach_acc(detacc_msg);
425 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
426}
427
428/* Return != 0 iff msg still needs to be processed */
429static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
430 struct msgb *msg,
431 time_t now,
432 struct gbproxy_link_info* link_info,
433 struct gprs_gb_parse_context *parse_ctx)
434{
435 struct msgb *stored_msg;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100436 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100437 OSMO_ASSERT(peer->nse);
438 struct gbproxy_config *cfg = peer->nse->cfg;
439 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200440
441 if (!link_info)
442 return 1;
443
444 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
445 return 1;
446
447 if (parse_ctx->g48_hdr)
448 switch (parse_ctx->g48_hdr->msg_type)
449 {
450 case GSM48_MT_GMM_RA_UPD_REQ:
451 case GSM48_MT_GMM_ATTACH_REQ:
452 if (gbproxy_restart_imsi_acquisition(link_info)) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100453 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
454 " IMSI acquisition was in progress "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200455 "when receiving an %s.\n",
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100456 parse_ctx->llc_msg_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200457 }
458 break;
459 case GSM48_MT_GMM_DETACH_REQ:
460 /* Nothing has been sent to the SGSN yet */
461 if (link_info->imsi_acq_pending) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100462 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
463 "IMSI acquisition was in progress "
464 "when receiving a DETACH_REQ.\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200465 }
466 if (!parse_ctx->invalidate_tlli) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100467 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
468 "IMSI not yet acquired, "
469 "faking a DETACH_ACC.\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200470 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
471 parse_ctx->invalidate_tlli = 1;
472 }
473 gbproxy_reset_imsi_acquisition(link_info);
474 gbproxy_update_link_state_after(peer, link_info, now,
475 parse_ctx);
476 return 0;
477 }
478
479 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
480 int is_ident_resp =
481 parse_ctx->g48_hdr &&
482 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
483 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
484
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100485 LOGPBVC_CAT(peer, DLLC, LOGL_DEBUG,
486 "IMSI acquisition succeeded, "
487 "flushing stored messages\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200488 /* The IMSI is now available. If flushing the messages fails,
489 * then link_info has been deleted and we should return
490 * immediately. */
491 if (gbproxy_flush_stored_messages(peer, now, link_info) < 0)
492 return 0;
493
494 gbproxy_reset_imsi_acquisition(link_info);
495
496 /* This message is most probably the response to the ident
497 * request sent by gbproxy_acquire_imsi(). Don't forward it to
498 * the SGSN. */
499 return !is_ident_resp;
500 }
501
502 /* The message cannot be processed since the IMSI is still missing */
503
504 /* If queue is getting too large, drop oldest msgb before adding new one */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100505 if (cfg->stored_msgs_max_len > 0) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200506 int exceeded_max_len = link_info->stored_msgs_len
Daniel Willmanne50550e2020-11-26 18:19:21 +0100507 + 1 - cfg->stored_msgs_max_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200508
509 for (; exceeded_max_len > 0; exceeded_max_len--) {
510 struct msgb *msgb_drop;
511 msgb_drop = msgb_dequeue_count(&link_info->stored_msgs,
512 &link_info->stored_msgs_len);
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100513 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
514 "Dropping stored msgb from list "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200515 "(!acq imsi, length %d, max_len exceeded)\n",
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100516 link_info->stored_msgs_len);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200517
518 msgb_free(msgb_drop);
519 }
520 }
521
522 /* Enqueue unpatched messages */
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100523 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
524 "IMSI acquisition in progress, "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200525 "storing message (%s)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200526 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
527
528 stored_msg = bssgp_msgb_copy(msg, "process_bssgp_ul");
529 msgb_enqueue_count(&link_info->stored_msgs, stored_msg,
530 &link_info->stored_msgs_len);
531
532 if (!link_info->imsi_acq_pending) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100533 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
534 "IMSI is required but not available, "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200535 "initiating identification procedure (%s)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200536 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
537
538 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
539
540 /* There is no explicit retransmission handling, the
541 * implementation relies on the MS doing proper retransmissions
542 * of the triggering message instead */
543
544 link_info->imsi_acq_pending = true;
545 }
546
547 return 0;
548}
549
550struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
551 struct msgb *msg,
552 struct gprs_gb_parse_context *parse_ctx)
553{
554 struct gbproxy_peer *peer = NULL;
555
556 if (msgb_bvci(msg) >= 2)
557 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
558
559 if (!peer && !parse_ctx->to_bss)
560 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
561
562 if (!peer)
563 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
564
565 if (!peer) {
566 LOGP(DLLC, LOGL_INFO,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100567 "NSE(%05u/%s) patching: didn't find peer for message, "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200568 "PDU %d\n",
569 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
570 parse_ctx->pdu_type);
571 /* Increment counter */
572 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
573 }
574 return peer;
575}
576
577/* patch BSSGP message */
578static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
579 struct msgb *msg,
580 struct gbproxy_peer *peer)
581{
582 struct gprs_gb_parse_context parse_ctx = {0};
583 int rc;
584 int len_change = 0;
585 time_t now;
586 struct timespec ts = {0,};
587 struct gbproxy_link_info *link_info = NULL;
588 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
589
590 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
591 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
592 return 1;
593
594 parse_ctx.to_bss = 0;
595 parse_ctx.peer_nsei = msgb_nsei(msg);
596
597 /* Parse BSSGP/LLC */
598 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
599 &parse_ctx);
600
601 if (!rc && !parse_ctx.need_decryption) {
602 LOGP(DGPRS, LOGL_ERROR,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100603 "NSE(%05u/BSS) patching: failed to parse invalid %s message\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200604 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
605 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
606 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100607 "NSE(%05u/BSS) invalid message was: %s\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200608 msgb_nsei(msg), msgb_hexdump(msg));
609 return 0;
610 }
611
612 /* Get peer */
613 if (!peer)
614 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
615
616 if (!peer)
617 return 0;
618
619
620 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
621 now = ts.tv_sec;
622
623 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
624 parse_ctx.llc_msg_name);
625
626 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
627
628 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
629
630 if (parse_ctx.g48_hdr) {
631 switch (parse_ctx.g48_hdr->msg_type) {
632 case GSM48_MT_GMM_ATTACH_REQ:
633 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
634 break;
635 case GSM48_MT_GMM_DETACH_REQ:
636 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
637 break;
638 case GSM48_MT_GMM_ATTACH_COMPL:
639 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
640 break;
641 case GSM48_MT_GMM_RA_UPD_REQ:
642 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
643 break;
644 case GSM48_MT_GMM_RA_UPD_COMPL:
645 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
646 break;
647 case GSM48_MT_GMM_STATUS:
648 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
649 break;
650 case GSM48_MT_GSM_ACT_PDP_REQ:
651 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
652 break;
653 case GSM48_MT_GSM_DEACT_PDP_REQ:
654 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
655 break;
656
657 default:
658 break;
659 }
660 }
661
662 if (link_info && cfg->route_to_sgsn2) {
663 if (cfg->acquire_imsi && link_info->imsi_len == 0)
664 sgsn_nsei = 0xffff;
665 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
666 link_info))
667 sgsn_nsei = cfg->nsip_sgsn2_nsei;
668 }
669
670 if (link_info)
671 link_info->sgsn_nsei = sgsn_nsei;
672
673 /* Handle IMSI acquisition */
674 if (cfg->acquire_imsi) {
675 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
676 &parse_ctx);
677 if (rc <= 0)
678 return rc;
679 }
680
681 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
682 peer, link_info, &len_change, &parse_ctx);
683
684 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
685
686 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
687 /* Send message directly to the selected SGSN */
688 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
689 /* Don't let the calling code handle the transmission */
690 return 0;
691 }
692
693 return 1;
694}
695
696/* patch BSSGP message to use core_plmn.mcc/mnc on the SGSN side */
697static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
698 struct msgb *msg,
699 struct gbproxy_peer *peer)
700{
701 struct gprs_gb_parse_context parse_ctx = {0};
702 int rc;
703 int len_change = 0;
704 time_t now;
705 struct timespec ts = {0,};
706 struct gbproxy_link_info *link_info = NULL;
707
708 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
709 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
710 return;
711
712 parse_ctx.to_bss = 1;
713 parse_ctx.peer_nsei = msgb_nsei(msg);
714
715 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
716 &parse_ctx);
717
718 if (!rc && !parse_ctx.need_decryption) {
719 LOGP(DGPRS, LOGL_ERROR,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100720 "NSE(%05u/SGSN) patching: failed to parse invalid %s message\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200721 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
722 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
723 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100724 "NSE(%05u/SGSN) invalid message was: %s\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200725 msgb_nsei(msg), msgb_hexdump(msg));
726 return;
727 }
728
729 /* Get peer */
730 if (!peer)
731 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
732
733 if (!peer)
734 return;
735
736 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
737 now = ts.tv_sec;
738
739 if (parse_ctx.g48_hdr) {
740 switch (parse_ctx.g48_hdr->msg_type) {
741 case GSM48_MT_GMM_ATTACH_ACK:
742 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
743 break;
744 case GSM48_MT_GMM_ATTACH_REJ:
745 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
746 break;
747 case GSM48_MT_GMM_DETACH_ACK:
748 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
749 break;
750 case GSM48_MT_GMM_RA_UPD_ACK:
751 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
752 break;
753 case GSM48_MT_GMM_RA_UPD_REJ:
754 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
755 break;
756 case GSM48_MT_GMM_STATUS:
757 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
758 break;
759 case GSM48_MT_GSM_ACT_PDP_ACK:
760 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
761 break;
762 case GSM48_MT_GSM_ACT_PDP_REJ:
763 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
764 break;
765 case GSM48_MT_GSM_DEACT_PDP_ACK:
766 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
767 break;
768
769 default:
770 break;
771 }
772 }
773
774 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
775
776 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
777
778 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
779 peer, link_info, &len_change, &parse_ctx);
780
781 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
782
783 return;
784}
785
786/* feed a message down the NS-VC associated with the specified peer */
787static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
788 uint16_t ns_bvci, uint16_t sgsn_nsei)
789{
790 /* create a copy of the message so the old one can
791 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200792 struct gprs_ns2_inst *nsi = cfg->nsi;
793 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200794 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
795 int rc;
796
Daniel Willmann3696dce2020-12-02 16:08:02 +0100797 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200798 msgb_nsei(msg), ns_bvci, sgsn_nsei);
799
Alexander Couzens951e1332020-09-22 13:21:46 +0200800 nsp.bvci = ns_bvci;
801 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200802
803 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200804 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
805 PRIM_OP_REQUEST, msg);
806 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200807 if (rc < 0)
808 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200809 return rc;
810}
811
Daniel Willmann76205712020-11-30 17:08:58 +0100812/* feed a message down the NSE */
813static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100814 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200815{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100816 OSMO_ASSERT(nse);
817 OSMO_ASSERT(nse->cfg);
818
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200819 /* create a copy of the message so the old one can
820 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100821 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Alexander Couzens951e1332020-09-22 13:21:46 +0200822 struct osmo_gprs_ns2_prim nsp = {};
Daniel Willmann76205712020-11-30 17:08:58 +0100823 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Harald Weltefe059582020-11-18 12:01:46 +0100824 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200825 int rc;
826
Daniel Willmann3696dce2020-12-02 16:08:02 +0100827 DEBUGP(DGPRS, "NSE(%05u/SGSN)-BVC(%05u) proxying SGSN->BSS NSE(%05u/BSS)\n",
Daniel Willmanne50550e2020-11-26 18:19:21 +0100828 msgb_nsei(msg), ns_bvci, nse->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200829
Alexander Couzens951e1332020-09-22 13:21:46 +0200830 nsp.bvci = ns_bvci;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100831 nsp.nsei = nse->nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200832
833 /* Strip the old NS header, it will be replaced with a new one */
834 strip_ns_hdr(msg);
835
Harald Weltefe059582020-11-18 12:01:46 +0100836 /* TS 48.018 Section 5.4.2: The link selector parameter is
837 * defined in 3GPP TS 48.016. At one side of the Gb interface,
838 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
839 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
840 * underlying network service. */
841 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
842 nsp.u.unitdata.link_selector = tlli;
843
Alexander Couzens951e1332020-09-22 13:21:46 +0200844 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
845 PRIM_OP_REQUEST, msg);
846 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Daniel Willmann76205712020-11-30 17:08:58 +0100847 /* FIXME: We need a counter group for gbproxy_nse */
848 //if (rc < 0)
849 // rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
850
851 return rc;
852}
853
854/* feed a message down the NS-VC associated with the specified peer */
855static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
856 uint16_t ns_bvci)
857{
858 int rc;
859 struct gbproxy_nse *nse = peer->nse;
860 OSMO_ASSERT(nse);
861
862 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200863 if (rc < 0)
864 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
865
866 return rc;
867}
868
869static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
870{
871 struct gbproxy_peer *peer;
872
873 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
874 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100875 LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200876 ptp_bvci);
877 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
878 return -ENOENT;
879 }
880
881 switch (pdu_type) {
882 case BSSGP_PDUT_BVC_BLOCK_ACK:
883 peer->blocked = true;
884 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
885 break;
886 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
887 peer->blocked = false;
888 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
889 break;
890 default:
891 break;
892 }
893 return 0;
894}
895
896/* Send a message to a peer identified by ptp_bvci but using ns_bvci
897 * in the NS hdr */
898static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
899 uint16_t ns_bvci)
900{
901 struct gbproxy_peer *peer;
902
903 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
904 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100905 LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200906 ptp_bvci);
907 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
908 return -ENOENT;
909 }
910
911 return gbprox_relay2peer(msg, peer, ns_bvci);
912}
913
914int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
915{
916 return 0;
917}
918
919/* Receive an incoming PTP message from a BSS-side NS-VC */
920static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
921 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200922 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200923{
924 struct gbproxy_peer *peer;
925 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
926 uint8_t pdu_type = bgph->pdu_type;
927 int rc;
928
929 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
930 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100931 LOGP(DGPRS, LOGL_NOTICE, "BVC(%05u/??) Didn't find peer "
932 "for PTP message from NSE(%05u/BSS), "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200933 "discarding message\n",
Alexander Couzens951e1332020-09-22 13:21:46 +0200934 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200935 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
936 &ns_bvci, msg);
937 }
938
Daniel Willmann65bfbaf2020-12-02 17:46:56 +0100939 /* TODO: Should we discard this message if the check fails */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200940 check_peer_nsei(peer, nsei);
941
942 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
943 if (!rc)
944 return 0;
945
946 switch (pdu_type) {
947 case BSSGP_PDUT_FLOW_CONTROL_BVC:
948 if (!cfg->route_to_sgsn2)
949 break;
950
951 /* Send a copy to the secondary SGSN */
952 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
953 break;
954 default:
955 break;
956 }
957
958
959 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
960}
961
962/* Receive an incoming PTP message from a SGSN-side NS-VC */
963static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
964 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200965 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200966{
967 struct gbproxy_peer *peer;
968 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
969 uint8_t pdu_type = bgph->pdu_type;
970
971 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
972
973 /* Send status messages before patching */
974
975 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100976 LOGP(DGPRS, LOGL_INFO, "BVC(%05u/??) Didn't find peer for "
977 "for message from NSE(%05u/SGSN)\n",
Alexander Couzens951e1332020-09-22 13:21:46 +0200978 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200979 rate_ctr_inc(&cfg->ctrg->
980 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
981 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
982 &ns_bvci, msg);
983 }
984
985 if (peer->blocked) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100986 LOGPBVC(peer, LOGL_NOTICE, "Dropping PDU for "
987 "blocked BVC via NSE(%05u/SGSN)\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200988 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
989 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
990 }
991
992 switch (pdu_type) {
993 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
994 case BSSGP_PDUT_BVC_BLOCK_ACK:
995 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
996 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
997 /* Hide ACKs from the secondary SGSN, the primary SGSN
998 * is responsible to send them. */
999 return 0;
1000 break;
1001 default:
1002 break;
1003 }
1004
1005 /* Optionally patch the message */
1006 gbprox_process_bssgp_dl(cfg, msg, peer);
1007
1008 return gbprox_relay2peer(msg, peer, ns_bvci);
1009}
1010
1011/* Receive an incoming signalling message from a BSS-side NS-VC */
1012static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
1013 struct msgb *msg, uint16_t nsei,
1014 uint16_t ns_bvci)
1015{
1016 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1017 struct tlv_parsed tp;
1018 uint8_t pdu_type = bgph->pdu_type;
1019 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1020 struct gbproxy_peer *from_peer = NULL;
1021 struct gprs_ra_id raid;
1022 int copy_to_sgsn2 = 0;
1023 int rc;
1024
1025 if (ns_bvci != 0 && ns_bvci != 1) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001026 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) BVCI=%05u is not signalling\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001027 nsei, ns_bvci);
1028 return -EINVAL;
1029 }
1030
1031 /* we actually should never see those two for BVCI == 0, but double-check
1032 * just to make sure */
1033 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1034 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001035 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) UNITDATA not allowed in "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001036 "signalling\n", nsei);
1037 return -EINVAL;
1038 }
1039
1040 bssgp_tlv_parse(&tp, bgph->data, data_len);
1041
1042 switch (pdu_type) {
1043 case BSSGP_PDUT_SUSPEND:
1044 case BSSGP_PDUT_RESUME:
1045 /* We implement RAI snooping during SUSPEND/RESUME, since it
1046 * establishes a relationsip between BVCI/peer and the routeing
1047 * area identification. The snooped information is then used
1048 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
1049 * BSSGP */
1050 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1051 goto err_mand_ie;
1052 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
1053 if (!from_peer)
1054 goto err_no_peer;
1055 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
1056 sizeof(from_peer->ra));
1057 gsm48_parse_ra(&raid, from_peer->ra);
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001058 LOGPBVC(from_peer, LOGL_INFO, "BSSGP SUSPEND/RESUME "
1059 "RAI snooping: RAI %s\n",
1060 osmo_rai_name(&raid));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001061 /* FIXME: This only supports one BSS per RA */
1062 break;
1063 case BSSGP_PDUT_BVC_RESET:
1064 /* If we receive a BVC reset on the signalling endpoint, we
1065 * don't want the SGSN to reset, as the signalling endpoint
1066 * is common for all point-to-point BVCs (and thus all BTS) */
1067 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1068 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001069 LOGP(DGPRS, LOGL_INFO, "NSE(%05u) Rx BVC RESET (BVCI=%05u)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001070 nsei, bvci);
1071 if (bvci == 0) {
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001072 struct gbproxy_nse *nse;
1073 /* Ensure the NSE peer is there and clear all PtP BVCs */
1074 nse = gbproxy_nse_by_nsei_or_new(cfg, nsei);
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001075 if (!nse) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001076 LOGP(DGPRS, LOGL_ERROR, "Could not create NSE(%05u)\n", nsei);
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001077 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1078 }
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001079
1080 gbproxy_cleanup_peers(cfg, nsei, 0);
1081
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001082 /* FIXME: only do this if SGSN is alive! */
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001083 LOGPNSE(nse, LOGL_INFO, "Tx fake "
1084 "BVC RESET ACK of BVCI=0\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001085 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
1086 nsei, 0, ns_bvci);
1087 }
1088 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
1089 if (!from_peer) {
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001090 struct gbproxy_nse *nse = gbproxy_nse_by_nsei(cfg, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001091 if (!nse) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001092 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
1093 "BVCI=%05u\n", nsei, bvci);
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001094 return bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001095 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001096 /* if a PTP-BVC is reset, and we don't know that
1097 * PTP-BVCI yet, we should allocate a new peer */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001098 from_peer = gbproxy_peer_alloc(nse, bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001099 OSMO_ASSERT(from_peer);
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001100 LOGPBVC(from_peer, LOGL_INFO, "Allocated new peer\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001101 }
1102
Daniel Willmanne50550e2020-11-26 18:19:21 +01001103 /* Could have moved to a different NSE */
1104 if (!check_peer_nsei(from_peer, nsei)) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001105 LOGPBVC(from_peer, LOGL_NOTICE, "moving peer to NSE(%05u)\n", nsei);
1106
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001107 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001108 if (!nse_new) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001109 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
1110 "BVCI=%05u\n", bvci, nsei);
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001111 return bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001112 }
Daniel Willmanne50550e2020-11-26 18:19:21 +01001113
1114 /* Move peer to different NSE */
Daniel Willmann559edac2020-11-30 17:14:24 +01001115 gbproxy_peer_move(from_peer, nse_new);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001116 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001117
1118 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
1119 struct gprs_ra_id raid;
1120 /* We have a Cell Identifier present in this
1121 * PDU, this means we can extend our local
1122 * state information about this particular cell
1123 * */
1124 memcpy(from_peer->ra,
1125 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
1126 sizeof(from_peer->ra));
1127 gsm48_parse_ra(&raid, from_peer->ra);
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001128 LOGPBVC(from_peer, LOGL_INFO, "Cell ID %s\n",
1129 osmo_rai_name(&raid));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001130 }
1131 if (cfg->route_to_sgsn2)
1132 copy_to_sgsn2 = 1;
1133 }
1134 break;
1135 }
1136
1137 /* Normally, we can simply pass on all signalling messages from BSS to
1138 * SGSN */
1139 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1140 if (!rc)
1141 return 0;
1142
1143 if (copy_to_sgsn2)
1144 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1145
1146 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
1147err_no_peer:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001148 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) cannot find peer based on NSEI\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001149 nsei);
1150 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
1151 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1152err_mand_ie:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001153 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) missing mandatory RA IE\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001154 nsei);
1155 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
1156 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1157}
1158
1159/* Receive paging request from SGSN, we need to relay to proper BSS */
1160static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
1161 uint32_t nsei, uint16_t ns_bvci)
1162{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001163 struct gbproxy_nse *nse;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001164 struct gbproxy_peer *peer;
Daniel Willmann76205712020-11-30 17:08:58 +01001165 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001166 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
1167
Daniel Willmanne50550e2020-11-26 18:19:21 +01001168 /* FIXME: Handle paging logic to only page each matching NSE */
1169
Daniel Willmann3696dce2020-12-02 16:08:02 +01001170 LOGP(DGPRS, LOGL_INFO, "NSE(%05u/SGSN) BSSGP PAGING\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001171 nsei);
1172 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1173 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001174 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001175 peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001176 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001177 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PAGING: "
1178 "unable to route: BVCI=%05u unknown\n", nsei, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001179 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1180 return -EINVAL;
1181 }
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001182 LOGPBVC(peer, LOGL_INFO, "routing by BVCI\n");
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001183 return gbprox_relay2peer(msg, peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001184 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001185 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001186 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001187 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1188 llist_for_each_entry(peer, &nse->bts_peers, list) {
1189 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001190 LOGPNSE(nse, LOGL_INFO, "routing to NSE (RAI match)");
Daniel Willmann76205712020-11-30 17:08:58 +01001191 gbprox_relay2nse(msg, nse, ns_bvci);
1192 n_nses++;
1193 /* Only send it once to each NSE */
1194 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001195 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001196 }
1197 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001198 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001199 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001200 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001201 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1202 llist_for_each_entry(peer, &nse->bts_peers, list) {
1203 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001204 LOGPNSE(nse, LOGL_INFO, "routing to NSE (LAI match)\n");
Daniel Willmann76205712020-11-30 17:08:58 +01001205 gbprox_relay2nse(msg, nse, ns_bvci);
1206 n_nses++;
1207 /* Only send it once to each NSE */
1208 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001209 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001210 }
1211 }
Harald Welte53ee2062020-11-24 11:31:13 +01001212 } else if (TLVP_PRESENT(tp, BSSGP_IE_BSS_AREA_ID)) {
1213 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001214 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1215 llist_for_each_entry(peer, &nse->bts_peers, list) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001216 LOGPNSE(nse, LOGL_INFO, "routing to NSE (broadcast)\n");
Daniel Willmann76205712020-11-30 17:08:58 +01001217 gbprox_relay2nse(msg, nse, ns_bvci);
1218 n_nses++;
1219 /* Only send it once to each NSE */
1220 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001221 }
Harald Welte53ee2062020-11-24 11:31:13 +01001222 }
1223 } else {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001224 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: "
Harald Welte53ee2062020-11-24 11:31:13 +01001225 "unable to route, missing IE\n", nsei);
1226 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1227 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001228
Daniel Willmann76205712020-11-30 17:08:58 +01001229 if (n_nses == 0) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001230 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: "
Harald Welte53ee2062020-11-24 11:31:13 +01001231 "unable to route, no destination found\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001232 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1233 return -EINVAL;
1234 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001235 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001236}
1237
1238/* Receive an incoming BVC-RESET message from the SGSN */
1239static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
1240 struct msgb *orig_msg,
1241 struct msgb *msg, struct tlv_parsed *tp,
1242 uint32_t nsei, uint16_t ns_bvci)
1243{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001244 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001245 struct gbproxy_peer *peer;
1246 uint16_t ptp_bvci;
1247
1248 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1249 rate_ctr_inc(&cfg->ctrg->
1250 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1251 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
1252 NULL, orig_msg);
1253 }
1254 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1255
1256 if (ptp_bvci >= 2) {
1257 /* A reset for a PTP BVC was received, forward it to its
1258 * respective peer */
1259 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
1260 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001261 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BVCI=%05u: Cannot find BSS\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001262 nsei, ptp_bvci);
1263 rate_ctr_inc(&cfg->ctrg->
1264 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1265 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
1266 &ptp_bvci, orig_msg);
1267 }
1268 return gbprox_relay2peer(msg, peer, ns_bvci);
1269 }
1270
1271 /* A reset for the Signalling entity has been received
1272 * from the SGSN. As the signalling BVCI is shared
1273 * among all the BSS's that we multiplex, it needs to
1274 * be relayed */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001275 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1276 llist_for_each_entry(peer, &nse->bts_peers, list)
1277 gbprox_relay2peer(msg, peer, ns_bvci);
1278 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001279
1280 return 0;
1281}
1282
1283/* Receive an incoming signalling message from the SGSN-side NS-VC */
1284static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
1285 struct msgb *orig_msg, uint32_t nsei,
1286 uint16_t ns_bvci)
1287{
1288 struct bssgp_normal_hdr *bgph =
1289 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
1290 struct tlv_parsed tp;
1291 uint8_t pdu_type = bgph->pdu_type;
1292 int data_len;
1293 struct gbproxy_peer *peer;
1294 uint16_t bvci;
1295 struct msgb *msg;
1296 int rc = 0;
1297 int cause;
1298
1299 if (ns_bvci != 0 && ns_bvci != 1) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001300 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BVCI=%05u is not "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001301 "signalling\n", nsei, ns_bvci);
1302 /* FIXME: Send proper error message */
1303 return -EINVAL;
1304 }
1305
1306 /* we actually should never see those two for BVCI == 0, but double-check
1307 * just to make sure */
1308 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1309 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001310 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) UNITDATA not allowed in "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001311 "signalling\n", nsei);
1312 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1313 }
1314
1315 msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1316 gbprox_process_bssgp_dl(cfg, msg, NULL);
1317 /* Update message info */
1318 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1319 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
1320 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1321
1322 switch (pdu_type) {
1323 case BSSGP_PDUT_BVC_RESET:
1324 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
1325 break;
1326 case BSSGP_PDUT_BVC_RESET_ACK:
1327 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1328 break;
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001329 /* simple case: BVCI IE is mandatory */
1330 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1331 goto err_mand_ie;
1332 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1333 if (bvci == BVCI_SIGNALLING) {
1334 /* TODO: Reset all PTP BVCIs */
1335 } else {
1336 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1337 }
1338 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001339 case BSSGP_PDUT_FLUSH_LL:
1340 /* simple case: BVCI IE is mandatory */
1341 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1342 goto err_mand_ie;
1343 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1344 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1345 break;
1346 case BSSGP_PDUT_PAGING_PS:
1347 case BSSGP_PDUT_PAGING_CS:
1348 /* process the paging request (LAI/RAI lookup) */
1349 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
1350 break;
1351 case BSSGP_PDUT_STATUS:
1352 /* Some exception has occurred */
1353 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann3696dce2020-12-02 16:08:02 +01001354 "NSE(%05u/SGSN) BSSGP STATUS ", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001355 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1356 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1357 goto err_mand_ie;
1358 }
1359 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
1360 LOGPC(DGPRS, LOGL_NOTICE,
1361 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
1362 bssgp_cause_str(cause));
1363 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1364 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001365 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001366
1367 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1368 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1369 } else
1370 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1371 break;
1372 /* those only exist in the SGSN -> BSS direction */
1373 case BSSGP_PDUT_SUSPEND_ACK:
1374 case BSSGP_PDUT_SUSPEND_NACK:
1375 case BSSGP_PDUT_RESUME_ACK:
1376 case BSSGP_PDUT_RESUME_NACK:
1377 /* RAI IE is mandatory */
1378 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1379 goto err_mand_ie;
1380 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
1381 if (!peer)
1382 goto err_no_peer;
1383 rc = gbprox_relay2peer(msg, peer, ns_bvci);
1384 break;
1385 case BSSGP_PDUT_BVC_BLOCK_ACK:
1386 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1387 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1388 goto err_mand_ie;
1389 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1390 if (bvci == 0) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001391 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001392 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
1393 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
Daniel Willmann65bfbaf2020-12-02 17:46:56 +01001394 /* TODO: should we send STATUS ? */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001395 rate_ctr_inc(&cfg->ctrg->
1396 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1397 } else {
1398 /* Mark BVC as (un)blocked */
1399 block_unblock_peer(cfg, bvci, pdu_type);
1400 }
1401 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1402 break;
1403 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
1404 LOGP(DGPRS, LOGL_ERROR,
Daniel Willmann3696dce2020-12-02 16:08:02 +01001405 "NSE(%05u/SGSN) BSSGP INVOKE TRACE not supported\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001406 rate_ctr_inc(&cfg->ctrg->
1407 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
1408 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
1409 break;
1410 default:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001411 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PDU type %s not supported\n", nsei,
1412 bssgp_pdu_str(pdu_type));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001413 rate_ctr_inc(&cfg->ctrg->
1414 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1415 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1416 break;
1417 }
1418
1419 msgb_free(msg);
1420
1421 return rc;
1422err_mand_ie:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001423 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) missing mandatory IE\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001424 nsei);
1425 rate_ctr_inc(&cfg->ctrg->
1426 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1427 msgb_free(msg);
1428 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
1429err_no_peer:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001430 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) cannot find peer based on RAI\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001431 nsei);
1432 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
1433 msgb_free(msg);
1434 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
1435}
1436
1437static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1438{
1439 return nsei == cfg->nsip_sgsn_nsei ||
1440 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1441}
1442
Alexander Couzens951e1332020-09-22 13:21:46 +02001443int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001444{
1445 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001446 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1447 struct gprs_ns2_inst *nsi = cfg->nsi;
1448 struct osmo_gprs_ns2_prim nsp = {};
1449
1450 nsp.bvci = msgb_bvci(msg);
1451 nsp.nsei = msgb_nsei(msg);
1452
1453 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
1454 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1455
1456 return rc;
1457}
1458
1459/* Main input function for Gb proxy */
1460int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1461{
1462 int rc;
1463 uint16_t nsei = msgb_nsei(msg);
1464 uint16_t ns_bvci = msgb_bvci(msg);
1465 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1466
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001467 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
1468
1469 /* Only BVCI=0 messages need special treatment */
1470 if (ns_bvci == 0 || ns_bvci == 1) {
1471 if (remote_end_is_sgsn)
1472 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
1473 else
1474 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
1475 } else {
1476 /* All other BVCI are PTP */
1477 if (remote_end_is_sgsn)
Alexander Couzens951e1332020-09-22 13:21:46 +02001478 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001479 ns_bvci);
1480 else
Alexander Couzens951e1332020-09-22 13:21:46 +02001481 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001482 ns_bvci);
1483 }
1484
1485 return rc;
1486}
1487
Alexander Couzens951e1332020-09-22 13:21:46 +02001488/* TODO: What about handling:
1489 * NS_AFF_CAUSE_VC_FAILURE,
1490 NS_AFF_CAUSE_VC_RECOVERY,
1491 NS_AFF_CAUSE_FAILURE,
1492 NS_AFF_CAUSE_RECOVERY,
1493 osmocom own causes
1494 NS_AFF_CAUSE_SNS_CONFIGURED,
1495 NS_AFF_CAUSE_SNS_FAILURE,
1496 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001497
Alexander Couzens951e1332020-09-22 13:21:46 +02001498void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001499{
Alexander Couzens951e1332020-09-22 13:21:46 +02001500 /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, peer->bvci, 0);
1501 * TODO: sgsn nsei available/unavailable
1502 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001503 struct gbproxy_peer *peer;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001504
Alexander Couzens951e1332020-09-22 13:21:46 +02001505 switch (nsp->u.status.cause) {
1506 case NS_AFF_CAUSE_SNS_FAILURE:
1507 case NS_AFF_CAUSE_SNS_CONFIGURED:
1508 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001509
Alexander Couzens951e1332020-09-22 13:21:46 +02001510 case NS_AFF_CAUSE_RECOVERY:
1511 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
1512 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1513 /* look-up or create the BTS context for this BVC */
1514 struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei);
1515 if (!bctx)
1516 bctx = btsctx_alloc(nsp->bvci, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001517
Alexander Couzens951e1332020-09-22 13:21:46 +02001518 bssgp_tx_bvc_reset_nsei_bvci(cfg->nsip_sgsn_nsei, 0, BSSGP_CAUSE_OML_INTERV, NULL, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001519 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001520 break;
1521 case NS_AFF_CAUSE_FAILURE:
1522 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1523 /* sgsn */
1524 /* TODO: BSVC: block all PtP towards bss */
1525 rate_ctr_inc(&cfg->ctrg->
1526 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1527 } else {
Daniel Willmanne50550e2020-11-26 18:19:21 +01001528 /* bss became unavailable
1529 * TODO: Block all BVC belonging to that NSE */
Alexander Couzens951e1332020-09-22 13:21:46 +02001530 peer = gbproxy_peer_by_nsei(cfg, nsp->nsei);
1531 if (!peer) {
1532 /* TODO: use primitive name + status cause name */
1533 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown peer NSEI=%u\n",
1534 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001535 break;
1536 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001537
1538 if (!peer->blocked)
1539 break;
1540 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei,
1541 peer->bvci, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001542 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001543 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
1544 break;
1545 default:
Harald Welte95cf9fb2020-11-30 10:55:22 +01001546 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
1547 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001548 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001549 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001550}
1551
1552
1553/* called by the ns layer */
1554int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1555{
1556 struct osmo_gprs_ns2_prim *nsp;
1557 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1558 int rc = 0;
1559
1560 if (oph->sap != SAP_NS)
1561 return 0;
1562
1563 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1564
1565 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte95cf9fb2020-11-30 10:55:22 +01001566 LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
1567 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001568 return 0;
1569 }
1570
1571 switch (oph->primitive) {
1572 case PRIM_NS_UNIT_DATA:
1573 /* hand the message into the BSSGP implementation */
1574 msgb_bssgph(oph->msg) = oph->msg->l3h;
1575 msgb_bvci(oph->msg) = nsp->bvci;
1576 msgb_nsei(oph->msg) = nsp->nsei;
1577
1578 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001579 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001580 break;
1581 case PRIM_NS_STATUS:
1582 gprs_ns_prim_status_cb(cfg, nsp);
1583 break;
1584 default:
Harald Welte95cf9fb2020-11-30 10:55:22 +01001585 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
1586 gprs_ns2_prim_str(oph->primitive),
1587 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001588 break;
1589 }
1590
1591 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001592}
1593
1594void gbprox_reset(struct gbproxy_config *cfg)
1595{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001596 struct gbproxy_nse *nse, *ntmp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001597
Daniel Willmanne50550e2020-11-26 18:19:21 +01001598 llist_for_each_entry_safe(nse, ntmp, &cfg->nse_peers, list) {
1599 struct gbproxy_peer *peer, *tmp;
1600 llist_for_each_entry_safe(peer, tmp, &nse->bts_peers, list)
1601 gbproxy_peer_free(peer);
1602
1603 gbproxy_nse_free(nse);
1604 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001605
1606 rate_ctr_group_free(cfg->ctrg);
1607 gbproxy_init_config(cfg);
1608}
1609
1610int gbproxy_init_config(struct gbproxy_config *cfg)
1611{
1612 struct timespec tp;
1613
Daniel Willmanne50550e2020-11-26 18:19:21 +01001614 INIT_LLIST_HEAD(&cfg->nse_peers);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001615 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1616 if (!cfg->ctrg) {
1617 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1618 return -1;
1619 }
1620 osmo_clock_gettime(CLOCK_REALTIME, &tp);
1621
1622 return 0;
1623}