blob: 997085bbefa2f68aa7f88f73a903db987db4356f [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 Willmanne50550e2020-11-26 18:19:21 +010087 OSMO_ASSERT(peer->nse);
88
89 if (peer->nse->nsei != nsei) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020090 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
91 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
Daniel Willmanne50550e2020-11-26 18:19:21 +010092 peer->bvci, nsei, peer->nse->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))
227 LOGP(DGPRS, LOGL_NOTICE,
228 "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)
260 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI: %d (%s)\n", rc, strerror(-rc));
261
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)
298 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI: %d (%s)\n", rc, strerror(-rc));
299
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 Willmanne50550e2020-11-26 18:19:21 +0100338 OSMO_ASSERT(peer->nse);
339 struct gbproxy_config *cfg = peer->nse->cfg;
340 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200341
342 /* Patch and flush stored messages towards the SGSN */
343 while ((stored_msg = msgb_dequeue_count(&link_info->stored_msgs,
344 &link_info->stored_msgs_len))) {
345 struct gprs_gb_parse_context tmp_parse_ctx = {0};
346 tmp_parse_ctx.to_bss = 0;
347 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
348 int len_change = 0;
349
350 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
351 msgb_bssgp_len(stored_msg),
352 &tmp_parse_ctx);
353 gbproxy_patch_bssgp(stored_msg, msgb_bssgph(stored_msg),
354 msgb_bssgp_len(stored_msg),
355 peer, link_info, &len_change,
356 &tmp_parse_ctx);
357
358 rc = gbproxy_update_link_state_after(peer, link_info, now,
359 &tmp_parse_ctx);
360 if (rc == 1) {
361 LOGP(DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
362 msgb_free(stored_msg);
363 return -1;
364 }
365
Daniel Willmanne50550e2020-11-26 18:19:21 +0100366 rc = gbprox_relay2sgsn(cfg, stored_msg,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200367 msgb_bvci(stored_msg), link_info->sgsn_nsei);
368
369 if (rc < 0)
370 LOGP(DLLC, LOGL_ERROR,
371 "NSEI=%d(BSS) failed to send stored message "
372 "(%s)\n",
373 tmp_parse_ctx.peer_nsei,
374 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 Willmanne50550e2020-11-26 18:19:21 +0100436 OSMO_ASSERT(peer->nse);
437 struct gbproxy_config *cfg = peer->nse->cfg;
438 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200439
440 if (!link_info)
441 return 1;
442
443 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
444 return 1;
445
446 if (parse_ctx->g48_hdr)
447 switch (parse_ctx->g48_hdr->msg_type)
448 {
449 case GSM48_MT_GMM_RA_UPD_REQ:
450 case GSM48_MT_GMM_ATTACH_REQ:
451 if (gbproxy_restart_imsi_acquisition(link_info)) {
452 LOGP(DLLC, LOGL_INFO,
453 "NSEI=%d(BSS) IMSI acquisition was in progress "
454 "when receiving an %s.\n",
455 msgb_nsei(msg), parse_ctx->llc_msg_name);
456 }
457 break;
458 case GSM48_MT_GMM_DETACH_REQ:
459 /* Nothing has been sent to the SGSN yet */
460 if (link_info->imsi_acq_pending) {
461 LOGP(DLLC, LOGL_INFO,
462 "NSEI=%d(BSS) IMSI acquisition was in progress "
463 "when receiving a DETACH_REQ.\n",
464 msgb_nsei(msg));
465 }
466 if (!parse_ctx->invalidate_tlli) {
467 LOGP(DLLC, LOGL_INFO,
468 "NSEI=%d(BSS) IMSI not yet acquired, "
469 "faking a DETACH_ACC.\n",
470 msgb_nsei(msg));
471 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
472 parse_ctx->invalidate_tlli = 1;
473 }
474 gbproxy_reset_imsi_acquisition(link_info);
475 gbproxy_update_link_state_after(peer, link_info, now,
476 parse_ctx);
477 return 0;
478 }
479
480 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
481 int is_ident_resp =
482 parse_ctx->g48_hdr &&
483 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
484 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
485
486 LOGP(DLLC, LOGL_DEBUG,
487 "NSEI=%d(BSS) IMSI acquisition succeeded, "
488 "flushing stored messages\n",
489 msgb_nsei(msg));
490 /* The IMSI is now available. If flushing the messages fails,
491 * then link_info has been deleted and we should return
492 * immediately. */
493 if (gbproxy_flush_stored_messages(peer, now, link_info) < 0)
494 return 0;
495
496 gbproxy_reset_imsi_acquisition(link_info);
497
498 /* This message is most probably the response to the ident
499 * request sent by gbproxy_acquire_imsi(). Don't forward it to
500 * the SGSN. */
501 return !is_ident_resp;
502 }
503
504 /* The message cannot be processed since the IMSI is still missing */
505
506 /* If queue is getting too large, drop oldest msgb before adding new one */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100507 if (cfg->stored_msgs_max_len > 0) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200508 int exceeded_max_len = link_info->stored_msgs_len
Daniel Willmanne50550e2020-11-26 18:19:21 +0100509 + 1 - cfg->stored_msgs_max_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200510
511 for (; exceeded_max_len > 0; exceeded_max_len--) {
512 struct msgb *msgb_drop;
513 msgb_drop = msgb_dequeue_count(&link_info->stored_msgs,
514 &link_info->stored_msgs_len);
515 LOGP(DLLC, LOGL_INFO,
516 "NSEI=%d(BSS) Dropping stored msgb from list "
517 "(!acq imsi, length %d, max_len exceeded)\n",
518 msgb_nsei(msgb_drop), link_info->stored_msgs_len);
519
520 msgb_free(msgb_drop);
521 }
522 }
523
524 /* Enqueue unpatched messages */
525 LOGP(DLLC, LOGL_INFO,
526 "NSEI=%d(BSS) IMSI acquisition in progress, "
527 "storing message (%s)\n",
528 msgb_nsei(msg),
529 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
530
531 stored_msg = bssgp_msgb_copy(msg, "process_bssgp_ul");
532 msgb_enqueue_count(&link_info->stored_msgs, stored_msg,
533 &link_info->stored_msgs_len);
534
535 if (!link_info->imsi_acq_pending) {
536 LOGP(DLLC, LOGL_INFO,
537 "NSEI=%d(BSS) IMSI is required but not available, "
538 "initiating identification procedure (%s)\n",
539 msgb_nsei(msg),
540 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
541
542 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
543
544 /* There is no explicit retransmission handling, the
545 * implementation relies on the MS doing proper retransmissions
546 * of the triggering message instead */
547
548 link_info->imsi_acq_pending = true;
549 }
550
551 return 0;
552}
553
554struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
555 struct msgb *msg,
556 struct gprs_gb_parse_context *parse_ctx)
557{
558 struct gbproxy_peer *peer = NULL;
559
560 if (msgb_bvci(msg) >= 2)
561 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
562
563 if (!peer && !parse_ctx->to_bss)
564 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
565
566 if (!peer)
567 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
568
569 if (!peer) {
570 LOGP(DLLC, LOGL_INFO,
571 "NSEI=%d(%s) patching: didn't find peer for message, "
572 "PDU %d\n",
573 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
574 parse_ctx->pdu_type);
575 /* Increment counter */
576 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
577 }
578 return peer;
579}
580
581/* patch BSSGP message */
582static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
583 struct msgb *msg,
584 struct gbproxy_peer *peer)
585{
586 struct gprs_gb_parse_context parse_ctx = {0};
587 int rc;
588 int len_change = 0;
589 time_t now;
590 struct timespec ts = {0,};
591 struct gbproxy_link_info *link_info = NULL;
592 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
593
594 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
595 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
596 return 1;
597
598 parse_ctx.to_bss = 0;
599 parse_ctx.peer_nsei = msgb_nsei(msg);
600
601 /* Parse BSSGP/LLC */
602 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
603 &parse_ctx);
604
605 if (!rc && !parse_ctx.need_decryption) {
606 LOGP(DGPRS, LOGL_ERROR,
607 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
608 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
609 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
610 LOGP(DGPRS, LOGL_NOTICE,
611 "NSEI=%u(BSS) invalid message was: %s\n",
612 msgb_nsei(msg), msgb_hexdump(msg));
613 return 0;
614 }
615
616 /* Get peer */
617 if (!peer)
618 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
619
620 if (!peer)
621 return 0;
622
623
624 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
625 now = ts.tv_sec;
626
627 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
628 parse_ctx.llc_msg_name);
629
630 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
631
632 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
633
634 if (parse_ctx.g48_hdr) {
635 switch (parse_ctx.g48_hdr->msg_type) {
636 case GSM48_MT_GMM_ATTACH_REQ:
637 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
638 break;
639 case GSM48_MT_GMM_DETACH_REQ:
640 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
641 break;
642 case GSM48_MT_GMM_ATTACH_COMPL:
643 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
644 break;
645 case GSM48_MT_GMM_RA_UPD_REQ:
646 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
647 break;
648 case GSM48_MT_GMM_RA_UPD_COMPL:
649 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
650 break;
651 case GSM48_MT_GMM_STATUS:
652 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
653 break;
654 case GSM48_MT_GSM_ACT_PDP_REQ:
655 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
656 break;
657 case GSM48_MT_GSM_DEACT_PDP_REQ:
658 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
659 break;
660
661 default:
662 break;
663 }
664 }
665
666 if (link_info && cfg->route_to_sgsn2) {
667 if (cfg->acquire_imsi && link_info->imsi_len == 0)
668 sgsn_nsei = 0xffff;
669 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
670 link_info))
671 sgsn_nsei = cfg->nsip_sgsn2_nsei;
672 }
673
674 if (link_info)
675 link_info->sgsn_nsei = sgsn_nsei;
676
677 /* Handle IMSI acquisition */
678 if (cfg->acquire_imsi) {
679 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
680 &parse_ctx);
681 if (rc <= 0)
682 return rc;
683 }
684
685 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
686 peer, link_info, &len_change, &parse_ctx);
687
688 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
689
690 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
691 /* Send message directly to the selected SGSN */
692 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
693 /* Don't let the calling code handle the transmission */
694 return 0;
695 }
696
697 return 1;
698}
699
700/* patch BSSGP message to use core_plmn.mcc/mnc on the SGSN side */
701static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
702 struct msgb *msg,
703 struct gbproxy_peer *peer)
704{
705 struct gprs_gb_parse_context parse_ctx = {0};
706 int rc;
707 int len_change = 0;
708 time_t now;
709 struct timespec ts = {0,};
710 struct gbproxy_link_info *link_info = NULL;
711
712 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
713 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
714 return;
715
716 parse_ctx.to_bss = 1;
717 parse_ctx.peer_nsei = msgb_nsei(msg);
718
719 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
720 &parse_ctx);
721
722 if (!rc && !parse_ctx.need_decryption) {
723 LOGP(DGPRS, LOGL_ERROR,
724 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
725 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
726 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
727 LOGP(DGPRS, LOGL_NOTICE,
728 "NSEI=%u(SGSN) invalid message was: %s\n",
729 msgb_nsei(msg), msgb_hexdump(msg));
730 return;
731 }
732
733 /* Get peer */
734 if (!peer)
735 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
736
737 if (!peer)
738 return;
739
740 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
741 now = ts.tv_sec;
742
743 if (parse_ctx.g48_hdr) {
744 switch (parse_ctx.g48_hdr->msg_type) {
745 case GSM48_MT_GMM_ATTACH_ACK:
746 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
747 break;
748 case GSM48_MT_GMM_ATTACH_REJ:
749 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
750 break;
751 case GSM48_MT_GMM_DETACH_ACK:
752 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
753 break;
754 case GSM48_MT_GMM_RA_UPD_ACK:
755 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
756 break;
757 case GSM48_MT_GMM_RA_UPD_REJ:
758 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
759 break;
760 case GSM48_MT_GMM_STATUS:
761 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
762 break;
763 case GSM48_MT_GSM_ACT_PDP_ACK:
764 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
765 break;
766 case GSM48_MT_GSM_ACT_PDP_REJ:
767 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
768 break;
769 case GSM48_MT_GSM_DEACT_PDP_ACK:
770 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
771 break;
772
773 default:
774 break;
775 }
776 }
777
778 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
779
780 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
781
782 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
783 peer, link_info, &len_change, &parse_ctx);
784
785 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
786
787 return;
788}
789
790/* feed a message down the NS-VC associated with the specified peer */
791static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
792 uint16_t ns_bvci, uint16_t sgsn_nsei)
793{
794 /* create a copy of the message so the old one can
795 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200796 struct gprs_ns2_inst *nsi = cfg->nsi;
797 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200798 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
799 int rc;
800
801 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
802 msgb_nsei(msg), ns_bvci, sgsn_nsei);
803
Alexander Couzens951e1332020-09-22 13:21:46 +0200804 nsp.bvci = ns_bvci;
805 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200806
807 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200808 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
809 PRIM_OP_REQUEST, msg);
810 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200811 if (rc < 0)
812 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200813 return rc;
814}
815
Daniel Willmann76205712020-11-30 17:08:58 +0100816/* feed a message down the NSE */
817static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100818 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200819{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100820 OSMO_ASSERT(nse);
821 OSMO_ASSERT(nse->cfg);
822
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200823 /* create a copy of the message so the old one can
824 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100825 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Alexander Couzens951e1332020-09-22 13:21:46 +0200826 struct osmo_gprs_ns2_prim nsp = {};
Daniel Willmann76205712020-11-30 17:08:58 +0100827 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Harald Weltefe059582020-11-18 12:01:46 +0100828 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200829 int rc;
830
831 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Daniel Willmanne50550e2020-11-26 18:19:21 +0100832 msgb_nsei(msg), ns_bvci, nse->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200833
Alexander Couzens951e1332020-09-22 13:21:46 +0200834 nsp.bvci = ns_bvci;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100835 nsp.nsei = nse->nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200836
837 /* Strip the old NS header, it will be replaced with a new one */
838 strip_ns_hdr(msg);
839
Harald Weltefe059582020-11-18 12:01:46 +0100840 /* TS 48.018 Section 5.4.2: The link selector parameter is
841 * defined in 3GPP TS 48.016. At one side of the Gb interface,
842 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
843 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
844 * underlying network service. */
845 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
846 nsp.u.unitdata.link_selector = tlli;
847
Alexander Couzens951e1332020-09-22 13:21:46 +0200848 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
849 PRIM_OP_REQUEST, msg);
850 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Daniel Willmann76205712020-11-30 17:08:58 +0100851 /* FIXME: We need a counter group for gbproxy_nse */
852 //if (rc < 0)
853 // rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
854
855 return rc;
856}
857
858/* feed a message down the NS-VC associated with the specified peer */
859static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
860 uint16_t ns_bvci)
861{
862 int rc;
863 struct gbproxy_nse *nse = peer->nse;
864 OSMO_ASSERT(nse);
865
866 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200867 if (rc < 0)
868 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
869
870 return rc;
871}
872
873static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
874{
875 struct gbproxy_peer *peer;
876
877 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
878 if (!peer) {
879 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
880 ptp_bvci);
881 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
882 return -ENOENT;
883 }
884
885 switch (pdu_type) {
886 case BSSGP_PDUT_BVC_BLOCK_ACK:
887 peer->blocked = true;
888 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
889 break;
890 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
891 peer->blocked = false;
892 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
893 break;
894 default:
895 break;
896 }
897 return 0;
898}
899
900/* Send a message to a peer identified by ptp_bvci but using ns_bvci
901 * in the NS hdr */
902static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
903 uint16_t ns_bvci)
904{
905 struct gbproxy_peer *peer;
906
907 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
908 if (!peer) {
909 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
910 ptp_bvci);
911 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
912 return -ENOENT;
913 }
914
915 return gbprox_relay2peer(msg, peer, ns_bvci);
916}
917
918int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
919{
920 return 0;
921}
922
923/* Receive an incoming PTP message from a BSS-side NS-VC */
924static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
925 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200926 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200927{
928 struct gbproxy_peer *peer;
929 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
930 uint8_t pdu_type = bgph->pdu_type;
931 int rc;
932
933 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
934 if (!peer) {
935 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
Alexander Couzens951e1332020-09-22 13:21:46 +0200936 "BVCI=%u for PTP message from NSEI=%u (BSS), "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200937 "discarding message\n",
Alexander Couzens951e1332020-09-22 13:21:46 +0200938 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200939 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
940 &ns_bvci, msg);
941 }
942
943 check_peer_nsei(peer, nsei);
944
945 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
946 if (!rc)
947 return 0;
948
949 switch (pdu_type) {
950 case BSSGP_PDUT_FLOW_CONTROL_BVC:
951 if (!cfg->route_to_sgsn2)
952 break;
953
954 /* Send a copy to the secondary SGSN */
955 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
956 break;
957 default:
958 break;
959 }
960
961
962 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
963}
964
965/* Receive an incoming PTP message from a SGSN-side NS-VC */
966static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
967 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200968 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200969{
970 struct gbproxy_peer *peer;
971 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
972 uint8_t pdu_type = bgph->pdu_type;
973
974 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
975
976 /* Send status messages before patching */
977
978 if (!peer) {
979 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
Alexander Couzens951e1332020-09-22 13:21:46 +0200980 "BVCI=%u for message from NSEI=%u (SGSN)\n",
981 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200982 rate_ctr_inc(&cfg->ctrg->
983 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
984 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
985 &ns_bvci, msg);
986 }
987
988 if (peer->blocked) {
989 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
Alexander Couzens951e1332020-09-22 13:21:46 +0200990 "blocked BVCI=%u via NSEI=%u\n",
991 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200992 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
993 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
994 }
995
996 switch (pdu_type) {
997 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
998 case BSSGP_PDUT_BVC_BLOCK_ACK:
999 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1000 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1001 /* Hide ACKs from the secondary SGSN, the primary SGSN
1002 * is responsible to send them. */
1003 return 0;
1004 break;
1005 default:
1006 break;
1007 }
1008
1009 /* Optionally patch the message */
1010 gbprox_process_bssgp_dl(cfg, msg, peer);
1011
1012 return gbprox_relay2peer(msg, peer, ns_bvci);
1013}
1014
1015/* Receive an incoming signalling message from a BSS-side NS-VC */
1016static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
1017 struct msgb *msg, uint16_t nsei,
1018 uint16_t ns_bvci)
1019{
1020 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1021 struct tlv_parsed tp;
1022 uint8_t pdu_type = bgph->pdu_type;
1023 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1024 struct gbproxy_peer *from_peer = NULL;
1025 struct gprs_ra_id raid;
1026 int copy_to_sgsn2 = 0;
1027 int rc;
1028
1029 if (ns_bvci != 0 && ns_bvci != 1) {
1030 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
1031 nsei, ns_bvci);
1032 return -EINVAL;
1033 }
1034
1035 /* we actually should never see those two for BVCI == 0, but double-check
1036 * just to make sure */
1037 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1038 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
1039 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
1040 "signalling\n", nsei);
1041 return -EINVAL;
1042 }
1043
1044 bssgp_tlv_parse(&tp, bgph->data, data_len);
1045
1046 switch (pdu_type) {
1047 case BSSGP_PDUT_SUSPEND:
1048 case BSSGP_PDUT_RESUME:
1049 /* We implement RAI snooping during SUSPEND/RESUME, since it
1050 * establishes a relationsip between BVCI/peer and the routeing
1051 * area identification. The snooped information is then used
1052 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
1053 * BSSGP */
1054 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1055 goto err_mand_ie;
1056 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
1057 if (!from_peer)
1058 goto err_no_peer;
1059 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
1060 sizeof(from_peer->ra));
1061 gsm48_parse_ra(&raid, from_peer->ra);
1062 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
1063 "RAI snooping: RAI %s behind BVCI=%u\n",
1064 nsei, osmo_rai_name(&raid), from_peer->bvci);
1065 /* FIXME: This only supports one BSS per RA */
1066 break;
1067 case BSSGP_PDUT_BVC_RESET:
1068 /* If we receive a BVC reset on the signalling endpoint, we
1069 * don't want the SGSN to reset, as the signalling endpoint
1070 * is common for all point-to-point BVCs (and thus all BTS) */
1071 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1072 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1073 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
1074 nsei, bvci);
1075 if (bvci == 0) {
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001076 struct gbproxy_nse *nse;
1077 /* Ensure the NSE peer is there and clear all PtP BVCs */
1078 nse = gbproxy_nse_by_nsei_or_new(cfg, nsei);
1079 if (!nse)
1080 LOGP(DGPRS, LOGL_ERROR, "Could not allocate NSE for NSEI=%u\n", nsei);
1081
1082 gbproxy_cleanup_peers(cfg, nsei, 0);
1083
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001084 /* FIXME: only do this if SGSN is alive! */
1085 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
1086 "BVC RESET ACK of BVCI=0\n", nsei);
1087 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
1088 nsei, 0, ns_bvci);
1089 }
1090 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
1091 if (!from_peer) {
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001092 struct gbproxy_nse *nse = gbproxy_nse_by_nsei(cfg, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001093 if (!nse) {
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001094 LOGP(DGPRS, LOGL_NOTICE, "Got PtP BVC reset before signalling reset for "
1095 "BVCI=%u NSEI=%u\n", bvci, nsei);
1096 return bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001097 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001098 /* if a PTP-BVC is reset, and we don't know that
1099 * PTP-BVCI yet, we should allocate a new peer */
1100 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for BVCI=%u via NSEI=%u\n", bvci, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001101 from_peer = gbproxy_peer_alloc(nse, bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001102 OSMO_ASSERT(from_peer);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001103 }
1104
Daniel Willmanne50550e2020-11-26 18:19:21 +01001105 /* Could have moved to a different NSE */
1106 if (!check_peer_nsei(from_peer, nsei)) {
1107 struct gbproxy_nse *nse_old = from_peer->nse;
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001108 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001109 if (!nse_new) {
Daniel Willmannaf7398a2020-11-30 14:28:52 +01001110 LOGP(DGPRS, LOGL_NOTICE, "Got PtP BVC reset before signalling reset for "
1111 "BVCI=%u NSEI=%u\n", bvci, nsei);
1112 return bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001113 }
1114 LOGP(DGPRS, LOGL_NOTICE, "Peer for BVCI=%u moved from NSEI=%u to NSEI=%u\n", bvci, nse_old->nsei, nsei);
1115
1116 /* Move peer to different NSE */
Daniel Willmann559edac2020-11-30 17:14:24 +01001117 gbproxy_peer_move(from_peer, nse_new);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001118 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001119
1120 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
1121 struct gprs_ra_id raid;
1122 /* We have a Cell Identifier present in this
1123 * PDU, this means we can extend our local
1124 * state information about this particular cell
1125 * */
1126 memcpy(from_peer->ra,
1127 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
1128 sizeof(from_peer->ra));
1129 gsm48_parse_ra(&raid, from_peer->ra);
1130 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u Cell ID %s\n",
1131 nsei, bvci, osmo_rai_name(&raid));
1132 }
1133 if (cfg->route_to_sgsn2)
1134 copy_to_sgsn2 = 1;
1135 }
1136 break;
1137 }
1138
1139 /* Normally, we can simply pass on all signalling messages from BSS to
1140 * SGSN */
1141 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1142 if (!rc)
1143 return 0;
1144
1145 if (copy_to_sgsn2)
1146 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1147
1148 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
1149err_no_peer:
1150 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
1151 nsei);
1152 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
1153 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1154err_mand_ie:
1155 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
1156 nsei);
1157 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
1158 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1159}
1160
1161/* Receive paging request from SGSN, we need to relay to proper BSS */
1162static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
1163 uint32_t nsei, uint16_t ns_bvci)
1164{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001165 struct gbproxy_nse *nse;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001166 struct gbproxy_peer *peer;
Daniel Willmann76205712020-11-30 17:08:58 +01001167 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001168 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
1169
Daniel Willmanne50550e2020-11-26 18:19:21 +01001170 /* FIXME: Handle paging logic to only page each matching NSE */
1171
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001172 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
1173 nsei);
1174 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1175 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001176 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001177 peer = gbproxy_peer_by_bvci(cfg, bvci);
1178 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", bvci);
1179 if (!peer) {
1180 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP PAGING: "
1181 "unable to route: BVCI=%u unknown\n", nsei, bvci);
1182 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1183 return -EINVAL;
1184 }
1185 return gbprox_relay2peer(msg, peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001186 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001187 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001188 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001189 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1190 llist_for_each_entry(peer, &nse->bts_peers, list) {
1191 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
Daniel Willmann76205712020-11-30 17:08:58 +01001192 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer NSEI=%u\n", peer->bvci);
1193 gbprox_relay2nse(msg, nse, ns_bvci);
1194 n_nses++;
1195 /* Only send it once to each NSE */
1196 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001197 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001198 }
1199 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001200 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001201 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001202 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001203 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1204 llist_for_each_entry(peer, &nse->bts_peers, list) {
1205 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
1206 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", peer->bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001207 gbprox_relay2nse(msg, nse, ns_bvci);
1208 n_nses++;
1209 /* Only send it once to each NSE */
1210 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001211 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001212 }
1213 }
Harald Welte53ee2062020-11-24 11:31:13 +01001214 } else if (TLVP_PRESENT(tp, BSSGP_IE_BSS_AREA_ID)) {
1215 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001216 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1217 llist_for_each_entry(peer, &nse->bts_peers, list) {
1218 LOGPC(DGPRS, LOGL_INFO, "broadcasting to peer BVCI=%u\n", peer->bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001219 gbprox_relay2nse(msg, nse, ns_bvci);
1220 n_nses++;
1221 /* Only send it once to each NSE */
1222 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001223 }
Harald Welte53ee2062020-11-24 11:31:13 +01001224 }
1225 } else {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001226 LOGPC(DGPRS, LOGL_INFO, "\n");
Harald Welte53ee2062020-11-24 11:31:13 +01001227 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
1228 "unable to route, missing IE\n", nsei);
1229 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1230 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001231
Daniel Willmann76205712020-11-30 17:08:58 +01001232 if (n_nses == 0) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001233 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Harald Welte53ee2062020-11-24 11:31:13 +01001234 "unable to route, no destination found\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001235 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1236 return -EINVAL;
1237 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001238 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001239}
1240
1241/* Receive an incoming BVC-RESET message from the SGSN */
1242static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
1243 struct msgb *orig_msg,
1244 struct msgb *msg, struct tlv_parsed *tp,
1245 uint32_t nsei, uint16_t ns_bvci)
1246{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001247 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001248 struct gbproxy_peer *peer;
1249 uint16_t ptp_bvci;
1250
1251 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1252 rate_ctr_inc(&cfg->ctrg->
1253 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1254 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
1255 NULL, orig_msg);
1256 }
1257 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1258
1259 if (ptp_bvci >= 2) {
1260 /* A reset for a PTP BVC was received, forward it to its
1261 * respective peer */
1262 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
1263 if (!peer) {
1264 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
1265 nsei, ptp_bvci);
1266 rate_ctr_inc(&cfg->ctrg->
1267 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1268 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
1269 &ptp_bvci, orig_msg);
1270 }
1271 return gbprox_relay2peer(msg, peer, ns_bvci);
1272 }
1273
1274 /* A reset for the Signalling entity has been received
1275 * from the SGSN. As the signalling BVCI is shared
1276 * among all the BSS's that we multiplex, it needs to
1277 * be relayed */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001278 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1279 llist_for_each_entry(peer, &nse->bts_peers, list)
1280 gbprox_relay2peer(msg, peer, ns_bvci);
1281 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001282
1283 return 0;
1284}
1285
1286/* Receive an incoming signalling message from the SGSN-side NS-VC */
1287static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
1288 struct msgb *orig_msg, uint32_t nsei,
1289 uint16_t ns_bvci)
1290{
1291 struct bssgp_normal_hdr *bgph =
1292 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
1293 struct tlv_parsed tp;
1294 uint8_t pdu_type = bgph->pdu_type;
1295 int data_len;
1296 struct gbproxy_peer *peer;
1297 uint16_t bvci;
1298 struct msgb *msg;
1299 int rc = 0;
1300 int cause;
1301
1302 if (ns_bvci != 0 && ns_bvci != 1) {
1303 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
1304 "signalling\n", nsei, ns_bvci);
1305 /* FIXME: Send proper error message */
1306 return -EINVAL;
1307 }
1308
1309 /* we actually should never see those two for BVCI == 0, but double-check
1310 * just to make sure */
1311 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1312 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
1313 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
1314 "signalling\n", nsei);
1315 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1316 }
1317
1318 msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1319 gbprox_process_bssgp_dl(cfg, msg, NULL);
1320 /* Update message info */
1321 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1322 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
1323 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1324
1325 switch (pdu_type) {
1326 case BSSGP_PDUT_BVC_RESET:
1327 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
1328 break;
1329 case BSSGP_PDUT_BVC_RESET_ACK:
1330 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1331 break;
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001332 /* simple case: BVCI IE is mandatory */
1333 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1334 goto err_mand_ie;
1335 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1336 if (bvci == BVCI_SIGNALLING) {
1337 /* TODO: Reset all PTP BVCIs */
1338 } else {
1339 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1340 }
1341 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001342 case BSSGP_PDUT_FLUSH_LL:
1343 /* simple case: BVCI IE is mandatory */
1344 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1345 goto err_mand_ie;
1346 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1347 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1348 break;
1349 case BSSGP_PDUT_PAGING_PS:
1350 case BSSGP_PDUT_PAGING_CS:
1351 /* process the paging request (LAI/RAI lookup) */
1352 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
1353 break;
1354 case BSSGP_PDUT_STATUS:
1355 /* Some exception has occurred */
1356 LOGP(DGPRS, LOGL_NOTICE,
1357 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
1358 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1359 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1360 goto err_mand_ie;
1361 }
1362 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
1363 LOGPC(DGPRS, LOGL_NOTICE,
1364 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
1365 bssgp_cause_str(cause));
1366 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1367 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1368 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1369
1370 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1371 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1372 } else
1373 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1374 break;
1375 /* those only exist in the SGSN -> BSS direction */
1376 case BSSGP_PDUT_SUSPEND_ACK:
1377 case BSSGP_PDUT_SUSPEND_NACK:
1378 case BSSGP_PDUT_RESUME_ACK:
1379 case BSSGP_PDUT_RESUME_NACK:
1380 /* RAI IE is mandatory */
1381 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1382 goto err_mand_ie;
1383 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
1384 if (!peer)
1385 goto err_no_peer;
1386 rc = gbprox_relay2peer(msg, peer, ns_bvci);
1387 break;
1388 case BSSGP_PDUT_BVC_BLOCK_ACK:
1389 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1390 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1391 goto err_mand_ie;
1392 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1393 if (bvci == 0) {
1394 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
1395 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
1396 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1397 /* should we send STATUS ? */
1398 rate_ctr_inc(&cfg->ctrg->
1399 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1400 } else {
1401 /* Mark BVC as (un)blocked */
1402 block_unblock_peer(cfg, bvci, pdu_type);
1403 }
1404 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1405 break;
1406 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
1407 LOGP(DGPRS, LOGL_ERROR,
1408 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
1409 rate_ctr_inc(&cfg->ctrg->
1410 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
1411 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
1412 break;
1413 default:
1414 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type %s not supported\n", bssgp_pdu_str(pdu_type));
1415 rate_ctr_inc(&cfg->ctrg->
1416 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1417 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1418 break;
1419 }
1420
1421 msgb_free(msg);
1422
1423 return rc;
1424err_mand_ie:
1425 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
1426 nsei);
1427 rate_ctr_inc(&cfg->ctrg->
1428 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1429 msgb_free(msg);
1430 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
1431err_no_peer:
1432 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
1433 nsei);
1434 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
1435 msgb_free(msg);
1436 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
1437}
1438
1439static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1440{
1441 return nsei == cfg->nsip_sgsn_nsei ||
1442 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1443}
1444
Alexander Couzens951e1332020-09-22 13:21:46 +02001445int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001446{
1447 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001448 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1449 struct gprs_ns2_inst *nsi = cfg->nsi;
1450 struct osmo_gprs_ns2_prim nsp = {};
1451
1452 nsp.bvci = msgb_bvci(msg);
1453 nsp.nsei = msgb_nsei(msg);
1454
1455 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
1456 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1457
1458 return rc;
1459}
1460
1461/* Main input function for Gb proxy */
1462int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1463{
1464 int rc;
1465 uint16_t nsei = msgb_nsei(msg);
1466 uint16_t ns_bvci = msgb_bvci(msg);
1467 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1468
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001469 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
1470
1471 /* Only BVCI=0 messages need special treatment */
1472 if (ns_bvci == 0 || ns_bvci == 1) {
1473 if (remote_end_is_sgsn)
1474 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
1475 else
1476 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
1477 } else {
1478 /* All other BVCI are PTP */
1479 if (remote_end_is_sgsn)
Alexander Couzens951e1332020-09-22 13:21:46 +02001480 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001481 ns_bvci);
1482 else
Alexander Couzens951e1332020-09-22 13:21:46 +02001483 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001484 ns_bvci);
1485 }
1486
1487 return rc;
1488}
1489
Alexander Couzens951e1332020-09-22 13:21:46 +02001490/* TODO: What about handling:
1491 * NS_AFF_CAUSE_VC_FAILURE,
1492 NS_AFF_CAUSE_VC_RECOVERY,
1493 NS_AFF_CAUSE_FAILURE,
1494 NS_AFF_CAUSE_RECOVERY,
1495 osmocom own causes
1496 NS_AFF_CAUSE_SNS_CONFIGURED,
1497 NS_AFF_CAUSE_SNS_FAILURE,
1498 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001499
Alexander Couzens951e1332020-09-22 13:21:46 +02001500void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001501{
Alexander Couzens951e1332020-09-22 13:21:46 +02001502 /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, peer->bvci, 0);
1503 * TODO: sgsn nsei available/unavailable
1504 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001505 struct gbproxy_peer *peer;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001506
Alexander Couzens951e1332020-09-22 13:21:46 +02001507 switch (nsp->u.status.cause) {
1508 case NS_AFF_CAUSE_SNS_FAILURE:
1509 case NS_AFF_CAUSE_SNS_CONFIGURED:
1510 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001511
Alexander Couzens951e1332020-09-22 13:21:46 +02001512 case NS_AFF_CAUSE_RECOVERY:
1513 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
1514 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1515 /* look-up or create the BTS context for this BVC */
1516 struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei);
1517 if (!bctx)
1518 bctx = btsctx_alloc(nsp->bvci, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001519
Alexander Couzens951e1332020-09-22 13:21:46 +02001520 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 +02001521 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001522 break;
1523 case NS_AFF_CAUSE_FAILURE:
1524 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1525 /* sgsn */
1526 /* TODO: BSVC: block all PtP towards bss */
1527 rate_ctr_inc(&cfg->ctrg->
1528 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1529 } else {
Daniel Willmanne50550e2020-11-26 18:19:21 +01001530 /* bss became unavailable
1531 * TODO: Block all BVC belonging to that NSE */
Alexander Couzens951e1332020-09-22 13:21:46 +02001532 peer = gbproxy_peer_by_nsei(cfg, nsp->nsei);
1533 if (!peer) {
1534 /* TODO: use primitive name + status cause name */
1535 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown peer NSEI=%u\n",
1536 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001537 break;
1538 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001539
1540 if (!peer->blocked)
1541 break;
1542 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei,
1543 peer->bvci, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001544 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001545 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
1546 break;
1547 default:
Harald Welte95cf9fb2020-11-30 10:55:22 +01001548 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
1549 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001550 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001551 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001552}
1553
1554
1555/* called by the ns layer */
1556int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1557{
1558 struct osmo_gprs_ns2_prim *nsp;
1559 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1560 int rc = 0;
1561
1562 if (oph->sap != SAP_NS)
1563 return 0;
1564
1565 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1566
1567 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte95cf9fb2020-11-30 10:55:22 +01001568 LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
1569 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001570 return 0;
1571 }
1572
1573 switch (oph->primitive) {
1574 case PRIM_NS_UNIT_DATA:
1575 /* hand the message into the BSSGP implementation */
1576 msgb_bssgph(oph->msg) = oph->msg->l3h;
1577 msgb_bvci(oph->msg) = nsp->bvci;
1578 msgb_nsei(oph->msg) = nsp->nsei;
1579
1580 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001581 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001582 break;
1583 case PRIM_NS_STATUS:
1584 gprs_ns_prim_status_cb(cfg, nsp);
1585 break;
1586 default:
Harald Welte95cf9fb2020-11-30 10:55:22 +01001587 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
1588 gprs_ns2_prim_str(oph->primitive),
1589 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001590 break;
1591 }
1592
1593 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001594}
1595
1596void gbprox_reset(struct gbproxy_config *cfg)
1597{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001598 struct gbproxy_nse *nse, *ntmp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001599
Daniel Willmanne50550e2020-11-26 18:19:21 +01001600 llist_for_each_entry_safe(nse, ntmp, &cfg->nse_peers, list) {
1601 struct gbproxy_peer *peer, *tmp;
1602 llist_for_each_entry_safe(peer, tmp, &nse->bts_peers, list)
1603 gbproxy_peer_free(peer);
1604
1605 gbproxy_nse_free(nse);
1606 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001607
1608 rate_ctr_group_free(cfg->ctrg);
1609 gbproxy_init_config(cfg);
1610}
1611
1612int gbproxy_init_config(struct gbproxy_config *cfg)
1613{
1614 struct timespec tp;
1615
Daniel Willmanne50550e2020-11-26 18:19:21 +01001616 INIT_LLIST_HEAD(&cfg->nse_peers);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001617 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1618 if (!cfg->ctrg) {
1619 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1620 return -1;
1621 }
1622 osmo_clock_gettime(CLOCK_REALTIME, &tp);
1623
1624 return 0;
1625}