blob: 9009651f4536931041183706224ff539b5c406b1 [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{
87 if (peer->nsei != nsei) {
88 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
89 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
90 peer->bvci, nsei, peer->nsei);
91 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
92 return 0;
93 }
94
95 return 1;
96}
97
98/* strip off the NS header */
99static void strip_ns_hdr(struct msgb *msg)
100{
101 int strip_len = msgb_bssgph(msg) - msg->data;
102 msgb_pull(msg, strip_len);
103}
104
105/* Transmit Chapter 9.2.10 Identity Request */
106static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
107{
108 struct gsm48_hdr *gh;
109
110 id_type &= GSM_MI_TYPE_MASK;
111
112 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
113 gh->proto_discr = GSM48_PDISC_MM_GPRS;
114 gh->msg_type = GSM48_MT_GMM_ID_REQ;
115 gh->data[0] = id_type;
116}
117
118/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
119static void gprs_put_mo_detach_acc(struct msgb *msg)
120{
121 struct gsm48_hdr *gh;
122
123 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
124 gh->proto_discr = GSM48_PDISC_MM_GPRS;
125 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
126 gh->data[0] = 0; /* no force to standby */
127}
128
129static void gprs_push_llc_ui(struct msgb *msg,
130 int is_uplink, unsigned sapi, unsigned nu)
131{
132 const uint8_t e_bit = 0;
133 const uint8_t pm_bit = 1;
134 const uint8_t cr_bit = is_uplink ? 0 : 1;
135 uint8_t *llc;
136 uint8_t *fcs_field;
137 uint32_t fcs;
138
139 nu &= 0x01ff; /* 9 Bit */
140
141 llc = msgb_push(msg, 3);
142 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
143 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
144 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
145
146 fcs = gprs_llc_fcs(llc, msgb_length(msg));
147 fcs_field = msgb_put(msg, 3);
148 fcs_field[0] = (uint8_t)(fcs >> 0);
149 fcs_field[1] = (uint8_t)(fcs >> 8);
150 fcs_field[2] = (uint8_t)(fcs >> 16);
151}
152
153static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
154 uint32_t tlli)
155{
156 struct bssgp_ud_hdr *budh;
157 uint8_t *llc = msgb_data(msg);
158 size_t llc_size = msgb_length(msg);
159 const size_t llc_ie_hdr_size = 3;
160 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
161 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
162
163 const size_t bssgp_overhead = sizeof(*budh) +
164 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
165 uint8_t *ie;
166 uint32_t tlli_be = htonl(tlli);
167
168 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
169
170 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
171 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
172 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
173
174 ie = budh->data;
175 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
176 ie += TVLV_GROSS_LEN(sizeof(lifetime));
177
178 /* Note: Add alignment before the LLC IE if inserting other IE */
179
180 *(ie++) = BSSGP_IE_LLC_PDU;
181 *(ie++) = llc_size / 256;
182 *(ie++) = llc_size % 256;
183
184 OSMO_ASSERT(ie == llc);
185
186 msgb_bssgph(msg) = (uint8_t *)budh;
187 msgb_tlli(msg) = tlli;
188}
189
190/* update peer according to the BSS message */
191static void gbprox_update_current_raid(uint8_t *raid_enc,
192 struct gbproxy_peer *peer,
193 const char *log_text)
194{
195 struct gbproxy_patch_state *state = &peer->patch_state;
196 const struct osmo_plmn_id old_plmn = state->local_plmn;
197 struct gprs_ra_id raid;
198
199 if (!raid_enc)
200 return;
201
202 gsm48_parse_ra(&raid, raid_enc);
203
204 /* save source side MCC/MNC */
205 if (!peer->cfg->core_plmn.mcc || raid.mcc == peer->cfg->core_plmn.mcc) {
206 state->local_plmn.mcc = 0;
207 } else {
208 state->local_plmn.mcc = raid.mcc;
209 }
210
211 if (!peer->cfg->core_plmn.mnc
212 || !osmo_mnc_cmp(raid.mnc, raid.mnc_3_digits,
213 peer->cfg->core_plmn.mnc, peer->cfg->core_plmn.mnc_3_digits)) {
214 state->local_plmn.mnc = 0;
215 state->local_plmn.mnc_3_digits = false;
216 } else {
217 state->local_plmn.mnc = raid.mnc;
218 state->local_plmn.mnc_3_digits = raid.mnc_3_digits;
219 }
220
221 if (osmo_plmn_cmp(&old_plmn, &state->local_plmn))
222 LOGP(DGPRS, LOGL_NOTICE,
223 "Patching RAID %sactivated, msg: %s, "
224 "local: %s, core: %s\n",
225 state->local_plmn.mcc || state->local_plmn.mnc ?
226 "" : "de",
227 log_text,
228 osmo_plmn_name(&state->local_plmn),
229 osmo_plmn_name2(&peer->cfg->core_plmn));
230}
231
232uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
233 uint32_t sgsn_ptmsi)
234{
235 uint32_t bss_ptmsi;
236 int max_retries = 23, rc = 0;
237 if (!peer->cfg->patch_ptmsi) {
238 bss_ptmsi = sgsn_ptmsi;
239 } else {
240 do {
241 rc = osmo_get_rand_id((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi));
242 if (rc < 0) {
243 bss_ptmsi = GSM_RESERVED_TMSI;
244 break;
245 }
246
247 bss_ptmsi = bss_ptmsi | GSM23003_TMSI_SGSN_MASK;
248
249 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
250 bss_ptmsi = GSM_RESERVED_TMSI;
251 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
252 }
253
254 if (bss_ptmsi == GSM_RESERVED_TMSI)
255 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI: %d (%s)\n", rc, strerror(-rc));
256
257 return bss_ptmsi;
258}
259
260uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
261 struct gbproxy_link_info *link_info,
262 uint32_t bss_tlli)
263{
264 uint32_t sgsn_tlli;
265 int max_retries = 23, rc = 0;
266 if (!peer->cfg->patch_ptmsi) {
267 sgsn_tlli = bss_tlli;
268 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
269 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
270 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
271 TLLI_FOREIGN);
272 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
273 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
274 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
275 TLLI_LOCAL);
276 } else {
277 do {
278 /* create random TLLI, 0b01111xxx... */
279 rc = osmo_get_rand_id((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli));
280 if (rc < 0) {
281 sgsn_tlli = 0;
282 break;
283 }
284
285 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
286
287 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
288 sgsn_tlli = 0;
289 } while (!sgsn_tlli && max_retries--);
290 }
291
292 if (!sgsn_tlli)
293 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI: %d (%s)\n", rc, strerror(-rc));
294
295 return sgsn_tlli;
296}
297
298void gbproxy_reset_link(struct gbproxy_link_info *link_info)
299{
300 gbproxy_reset_imsi_acquisition(link_info);
301}
302
303/* Returns != 0 iff IMSI acquisition was in progress */
304static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
305{
306 int in_progress = 0;
307 if (!link_info)
308 return 0;
309
310 if (link_info->imsi_acq_pending)
311 in_progress = 1;
312
313 gbproxy_link_info_discard_messages(link_info);
314 link_info->imsi_acq_pending = false;
315
316 return in_progress;
317}
318
319static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
320{
321 gbproxy_restart_imsi_acquisition(link_info);
322 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
323}
324
325/* Got identity response with IMSI, assuming the request had
326 * been generated by the gbproxy */
327static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
328 time_t now,
329 struct gbproxy_link_info* link_info)
330{
331 int rc;
332 struct msgb *stored_msg;
333
334 /* Patch and flush stored messages towards the SGSN */
335 while ((stored_msg = msgb_dequeue_count(&link_info->stored_msgs,
336 &link_info->stored_msgs_len))) {
337 struct gprs_gb_parse_context tmp_parse_ctx = {0};
338 tmp_parse_ctx.to_bss = 0;
339 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
340 int len_change = 0;
341
342 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
343 msgb_bssgp_len(stored_msg),
344 &tmp_parse_ctx);
345 gbproxy_patch_bssgp(stored_msg, msgb_bssgph(stored_msg),
346 msgb_bssgp_len(stored_msg),
347 peer, link_info, &len_change,
348 &tmp_parse_ctx);
349
350 rc = gbproxy_update_link_state_after(peer, link_info, now,
351 &tmp_parse_ctx);
352 if (rc == 1) {
353 LOGP(DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
354 msgb_free(stored_msg);
355 return -1;
356 }
357
358 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
359 msgb_bvci(stored_msg), link_info->sgsn_nsei);
360
361 if (rc < 0)
362 LOGP(DLLC, LOGL_ERROR,
363 "NSEI=%d(BSS) failed to send stored message "
364 "(%s)\n",
365 tmp_parse_ctx.peer_nsei,
366 tmp_parse_ctx.llc_msg_name ?
367 tmp_parse_ctx.llc_msg_name : "BSSGP");
368 msgb_free(stored_msg);
369 }
370
371 return 0;
372}
373
374static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
375 struct gbproxy_link_info* link_info,
376 uint16_t bvci,
377 struct msgb *msg /* Takes msg ownership */)
378{
379 int rc;
380
381 /* Workaround to avoid N(U) collisions and to enable a restart
382 * of the IMSI acquisition procedure. This will work unless the
383 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
384 * (see GSM 04.64, 8.4.2). */
385 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
386 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
387
388 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
Alexander Couzens951e1332020-09-22 13:21:46 +0200389 msg->l3h = msg->data;
390
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200391 rc = gbprox_relay2peer(msg, peer, bvci);
392 msgb_free(msg);
393 return rc;
394}
395
396static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
397 struct gbproxy_link_info* link_info,
398 uint16_t bvci)
399{
400 struct msgb *idreq_msg;
401
402 /* Send IDENT REQ */
403 idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI");
404 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
405 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
406}
407
408static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
409 struct gbproxy_link_info* link_info,
410 uint16_t bvci)
411{
412 struct msgb *detacc_msg;
413
414 /* Send DETACH ACC */
415 detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC");
416 gprs_put_mo_detach_acc(detacc_msg);
417 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
418}
419
420/* Return != 0 iff msg still needs to be processed */
421static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
422 struct msgb *msg,
423 time_t now,
424 struct gbproxy_link_info* link_info,
425 struct gprs_gb_parse_context *parse_ctx)
426{
427 struct msgb *stored_msg;
428
429 if (!link_info)
430 return 1;
431
432 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
433 return 1;
434
435 if (parse_ctx->g48_hdr)
436 switch (parse_ctx->g48_hdr->msg_type)
437 {
438 case GSM48_MT_GMM_RA_UPD_REQ:
439 case GSM48_MT_GMM_ATTACH_REQ:
440 if (gbproxy_restart_imsi_acquisition(link_info)) {
441 LOGP(DLLC, LOGL_INFO,
442 "NSEI=%d(BSS) IMSI acquisition was in progress "
443 "when receiving an %s.\n",
444 msgb_nsei(msg), parse_ctx->llc_msg_name);
445 }
446 break;
447 case GSM48_MT_GMM_DETACH_REQ:
448 /* Nothing has been sent to the SGSN yet */
449 if (link_info->imsi_acq_pending) {
450 LOGP(DLLC, LOGL_INFO,
451 "NSEI=%d(BSS) IMSI acquisition was in progress "
452 "when receiving a DETACH_REQ.\n",
453 msgb_nsei(msg));
454 }
455 if (!parse_ctx->invalidate_tlli) {
456 LOGP(DLLC, LOGL_INFO,
457 "NSEI=%d(BSS) IMSI not yet acquired, "
458 "faking a DETACH_ACC.\n",
459 msgb_nsei(msg));
460 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
461 parse_ctx->invalidate_tlli = 1;
462 }
463 gbproxy_reset_imsi_acquisition(link_info);
464 gbproxy_update_link_state_after(peer, link_info, now,
465 parse_ctx);
466 return 0;
467 }
468
469 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
470 int is_ident_resp =
471 parse_ctx->g48_hdr &&
472 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
473 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
474
475 LOGP(DLLC, LOGL_DEBUG,
476 "NSEI=%d(BSS) IMSI acquisition succeeded, "
477 "flushing stored messages\n",
478 msgb_nsei(msg));
479 /* The IMSI is now available. If flushing the messages fails,
480 * then link_info has been deleted and we should return
481 * immediately. */
482 if (gbproxy_flush_stored_messages(peer, now, link_info) < 0)
483 return 0;
484
485 gbproxy_reset_imsi_acquisition(link_info);
486
487 /* This message is most probably the response to the ident
488 * request sent by gbproxy_acquire_imsi(). Don't forward it to
489 * the SGSN. */
490 return !is_ident_resp;
491 }
492
493 /* The message cannot be processed since the IMSI is still missing */
494
495 /* If queue is getting too large, drop oldest msgb before adding new one */
496 if (peer->cfg->stored_msgs_max_len > 0) {
497 int exceeded_max_len = link_info->stored_msgs_len
498 + 1 - peer->cfg->stored_msgs_max_len;
499
500 for (; exceeded_max_len > 0; exceeded_max_len--) {
501 struct msgb *msgb_drop;
502 msgb_drop = msgb_dequeue_count(&link_info->stored_msgs,
503 &link_info->stored_msgs_len);
504 LOGP(DLLC, LOGL_INFO,
505 "NSEI=%d(BSS) Dropping stored msgb from list "
506 "(!acq imsi, length %d, max_len exceeded)\n",
507 msgb_nsei(msgb_drop), link_info->stored_msgs_len);
508
509 msgb_free(msgb_drop);
510 }
511 }
512
513 /* Enqueue unpatched messages */
514 LOGP(DLLC, LOGL_INFO,
515 "NSEI=%d(BSS) IMSI acquisition in progress, "
516 "storing message (%s)\n",
517 msgb_nsei(msg),
518 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
519
520 stored_msg = bssgp_msgb_copy(msg, "process_bssgp_ul");
521 msgb_enqueue_count(&link_info->stored_msgs, stored_msg,
522 &link_info->stored_msgs_len);
523
524 if (!link_info->imsi_acq_pending) {
525 LOGP(DLLC, LOGL_INFO,
526 "NSEI=%d(BSS) IMSI is required but not available, "
527 "initiating identification procedure (%s)\n",
528 msgb_nsei(msg),
529 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
530
531 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
532
533 /* There is no explicit retransmission handling, the
534 * implementation relies on the MS doing proper retransmissions
535 * of the triggering message instead */
536
537 link_info->imsi_acq_pending = true;
538 }
539
540 return 0;
541}
542
543struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
544 struct msgb *msg,
545 struct gprs_gb_parse_context *parse_ctx)
546{
547 struct gbproxy_peer *peer = NULL;
548
549 if (msgb_bvci(msg) >= 2)
550 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
551
552 if (!peer && !parse_ctx->to_bss)
553 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
554
555 if (!peer)
556 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
557
558 if (!peer) {
559 LOGP(DLLC, LOGL_INFO,
560 "NSEI=%d(%s) patching: didn't find peer for message, "
561 "PDU %d\n",
562 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
563 parse_ctx->pdu_type);
564 /* Increment counter */
565 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
566 }
567 return peer;
568}
569
570/* patch BSSGP message */
571static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
572 struct msgb *msg,
573 struct gbproxy_peer *peer)
574{
575 struct gprs_gb_parse_context parse_ctx = {0};
576 int rc;
577 int len_change = 0;
578 time_t now;
579 struct timespec ts = {0,};
580 struct gbproxy_link_info *link_info = NULL;
581 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
582
583 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
584 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
585 return 1;
586
587 parse_ctx.to_bss = 0;
588 parse_ctx.peer_nsei = msgb_nsei(msg);
589
590 /* Parse BSSGP/LLC */
591 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
592 &parse_ctx);
593
594 if (!rc && !parse_ctx.need_decryption) {
595 LOGP(DGPRS, LOGL_ERROR,
596 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
597 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
598 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
599 LOGP(DGPRS, LOGL_NOTICE,
600 "NSEI=%u(BSS) invalid message was: %s\n",
601 msgb_nsei(msg), msgb_hexdump(msg));
602 return 0;
603 }
604
605 /* Get peer */
606 if (!peer)
607 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
608
609 if (!peer)
610 return 0;
611
612
613 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
614 now = ts.tv_sec;
615
616 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
617 parse_ctx.llc_msg_name);
618
619 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
620
621 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
622
623 if (parse_ctx.g48_hdr) {
624 switch (parse_ctx.g48_hdr->msg_type) {
625 case GSM48_MT_GMM_ATTACH_REQ:
626 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
627 break;
628 case GSM48_MT_GMM_DETACH_REQ:
629 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
630 break;
631 case GSM48_MT_GMM_ATTACH_COMPL:
632 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
633 break;
634 case GSM48_MT_GMM_RA_UPD_REQ:
635 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
636 break;
637 case GSM48_MT_GMM_RA_UPD_COMPL:
638 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
639 break;
640 case GSM48_MT_GMM_STATUS:
641 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
642 break;
643 case GSM48_MT_GSM_ACT_PDP_REQ:
644 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
645 break;
646 case GSM48_MT_GSM_DEACT_PDP_REQ:
647 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
648 break;
649
650 default:
651 break;
652 }
653 }
654
655 if (link_info && cfg->route_to_sgsn2) {
656 if (cfg->acquire_imsi && link_info->imsi_len == 0)
657 sgsn_nsei = 0xffff;
658 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
659 link_info))
660 sgsn_nsei = cfg->nsip_sgsn2_nsei;
661 }
662
663 if (link_info)
664 link_info->sgsn_nsei = sgsn_nsei;
665
666 /* Handle IMSI acquisition */
667 if (cfg->acquire_imsi) {
668 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
669 &parse_ctx);
670 if (rc <= 0)
671 return rc;
672 }
673
674 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
675 peer, link_info, &len_change, &parse_ctx);
676
677 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
678
679 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
680 /* Send message directly to the selected SGSN */
681 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
682 /* Don't let the calling code handle the transmission */
683 return 0;
684 }
685
686 return 1;
687}
688
689/* patch BSSGP message to use core_plmn.mcc/mnc on the SGSN side */
690static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
691 struct msgb *msg,
692 struct gbproxy_peer *peer)
693{
694 struct gprs_gb_parse_context parse_ctx = {0};
695 int rc;
696 int len_change = 0;
697 time_t now;
698 struct timespec ts = {0,};
699 struct gbproxy_link_info *link_info = NULL;
700
701 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
702 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
703 return;
704
705 parse_ctx.to_bss = 1;
706 parse_ctx.peer_nsei = msgb_nsei(msg);
707
708 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
709 &parse_ctx);
710
711 if (!rc && !parse_ctx.need_decryption) {
712 LOGP(DGPRS, LOGL_ERROR,
713 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
714 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
715 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
716 LOGP(DGPRS, LOGL_NOTICE,
717 "NSEI=%u(SGSN) invalid message was: %s\n",
718 msgb_nsei(msg), msgb_hexdump(msg));
719 return;
720 }
721
722 /* Get peer */
723 if (!peer)
724 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
725
726 if (!peer)
727 return;
728
729 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
730 now = ts.tv_sec;
731
732 if (parse_ctx.g48_hdr) {
733 switch (parse_ctx.g48_hdr->msg_type) {
734 case GSM48_MT_GMM_ATTACH_ACK:
735 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
736 break;
737 case GSM48_MT_GMM_ATTACH_REJ:
738 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
739 break;
740 case GSM48_MT_GMM_DETACH_ACK:
741 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
742 break;
743 case GSM48_MT_GMM_RA_UPD_ACK:
744 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
745 break;
746 case GSM48_MT_GMM_RA_UPD_REJ:
747 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
748 break;
749 case GSM48_MT_GMM_STATUS:
750 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
751 break;
752 case GSM48_MT_GSM_ACT_PDP_ACK:
753 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
754 break;
755 case GSM48_MT_GSM_ACT_PDP_REJ:
756 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
757 break;
758 case GSM48_MT_GSM_DEACT_PDP_ACK:
759 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
760 break;
761
762 default:
763 break;
764 }
765 }
766
767 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
768
769 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
770
771 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
772 peer, link_info, &len_change, &parse_ctx);
773
774 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
775
776 return;
777}
778
779/* feed a message down the NS-VC associated with the specified peer */
780static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
781 uint16_t ns_bvci, uint16_t sgsn_nsei)
782{
783 /* create a copy of the message so the old one can
784 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200785 struct gprs_ns2_inst *nsi = cfg->nsi;
786 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200787 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
788 int rc;
789
790 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
791 msgb_nsei(msg), ns_bvci, sgsn_nsei);
792
Alexander Couzens951e1332020-09-22 13:21:46 +0200793 nsp.bvci = ns_bvci;
794 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200795
796 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200797 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
798 PRIM_OP_REQUEST, msg);
799 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200800 if (rc < 0)
801 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200802 return rc;
803}
804
805/* feed a message down the NS-VC associated with the specified peer */
806static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100807 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200808{
809 /* create a copy of the message so the old one can
810 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200811 struct gprs_ns2_inst *nsi = peer->cfg->nsi;
812 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200813 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2peer");
Harald Weltefe059582020-11-18 12:01:46 +0100814 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200815 int rc;
816
817 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
818 msgb_nsei(msg), ns_bvci, peer->nsei);
819
Alexander Couzens951e1332020-09-22 13:21:46 +0200820 nsp.bvci = ns_bvci;
821 nsp.nsei = peer->nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200822
823 /* Strip the old NS header, it will be replaced with a new one */
824 strip_ns_hdr(msg);
825
Harald Weltefe059582020-11-18 12:01:46 +0100826 /* TS 48.018 Section 5.4.2: The link selector parameter is
827 * defined in 3GPP TS 48.016. At one side of the Gb interface,
828 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
829 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
830 * underlying network service. */
831 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
832 nsp.u.unitdata.link_selector = tlli;
833
Alexander Couzens951e1332020-09-22 13:21:46 +0200834 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
835 PRIM_OP_REQUEST, msg);
836 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200837 if (rc < 0)
838 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
839
840 return rc;
841}
842
843static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
844{
845 struct gbproxy_peer *peer;
846
847 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
848 if (!peer) {
849 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
850 ptp_bvci);
851 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
852 return -ENOENT;
853 }
854
855 switch (pdu_type) {
856 case BSSGP_PDUT_BVC_BLOCK_ACK:
857 peer->blocked = true;
858 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
859 break;
860 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
861 peer->blocked = false;
862 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
863 break;
864 default:
865 break;
866 }
867 return 0;
868}
869
870/* Send a message to a peer identified by ptp_bvci but using ns_bvci
871 * in the NS hdr */
872static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
873 uint16_t ns_bvci)
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 return gbprox_relay2peer(msg, peer, ns_bvci);
886}
887
888int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
889{
890 return 0;
891}
892
893/* Receive an incoming PTP message from a BSS-side NS-VC */
894static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
895 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200896 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200897{
898 struct gbproxy_peer *peer;
899 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
900 uint8_t pdu_type = bgph->pdu_type;
901 int rc;
902
903 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
904 if (!peer) {
905 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
Alexander Couzens951e1332020-09-22 13:21:46 +0200906 "BVCI=%u for PTP message from NSEI=%u (BSS), "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200907 "discarding message\n",
Alexander Couzens951e1332020-09-22 13:21:46 +0200908 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200909 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
910 &ns_bvci, msg);
911 }
912
913 check_peer_nsei(peer, nsei);
914
915 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
916 if (!rc)
917 return 0;
918
919 switch (pdu_type) {
920 case BSSGP_PDUT_FLOW_CONTROL_BVC:
921 if (!cfg->route_to_sgsn2)
922 break;
923
924 /* Send a copy to the secondary SGSN */
925 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
926 break;
927 default:
928 break;
929 }
930
931
932 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
933}
934
935/* Receive an incoming PTP message from a SGSN-side NS-VC */
936static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
937 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200938 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200939{
940 struct gbproxy_peer *peer;
941 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
942 uint8_t pdu_type = bgph->pdu_type;
943
944 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
945
946 /* Send status messages before patching */
947
948 if (!peer) {
949 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
Alexander Couzens951e1332020-09-22 13:21:46 +0200950 "BVCI=%u for message from NSEI=%u (SGSN)\n",
951 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200952 rate_ctr_inc(&cfg->ctrg->
953 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
954 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
955 &ns_bvci, msg);
956 }
957
958 if (peer->blocked) {
959 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
Alexander Couzens951e1332020-09-22 13:21:46 +0200960 "blocked BVCI=%u via NSEI=%u\n",
961 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200962 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
963 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
964 }
965
966 switch (pdu_type) {
967 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
968 case BSSGP_PDUT_BVC_BLOCK_ACK:
969 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
970 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
971 /* Hide ACKs from the secondary SGSN, the primary SGSN
972 * is responsible to send them. */
973 return 0;
974 break;
975 default:
976 break;
977 }
978
979 /* Optionally patch the message */
980 gbprox_process_bssgp_dl(cfg, msg, peer);
981
982 return gbprox_relay2peer(msg, peer, ns_bvci);
983}
984
985/* Receive an incoming signalling message from a BSS-side NS-VC */
986static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
987 struct msgb *msg, uint16_t nsei,
988 uint16_t ns_bvci)
989{
990 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
991 struct tlv_parsed tp;
992 uint8_t pdu_type = bgph->pdu_type;
993 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
994 struct gbproxy_peer *from_peer = NULL;
995 struct gprs_ra_id raid;
996 int copy_to_sgsn2 = 0;
997 int rc;
998
999 if (ns_bvci != 0 && ns_bvci != 1) {
1000 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
1001 nsei, ns_bvci);
1002 return -EINVAL;
1003 }
1004
1005 /* we actually should never see those two for BVCI == 0, but double-check
1006 * just to make sure */
1007 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1008 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
1009 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
1010 "signalling\n", nsei);
1011 return -EINVAL;
1012 }
1013
1014 bssgp_tlv_parse(&tp, bgph->data, data_len);
1015
1016 switch (pdu_type) {
1017 case BSSGP_PDUT_SUSPEND:
1018 case BSSGP_PDUT_RESUME:
1019 /* We implement RAI snooping during SUSPEND/RESUME, since it
1020 * establishes a relationsip between BVCI/peer and the routeing
1021 * area identification. The snooped information is then used
1022 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
1023 * BSSGP */
1024 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1025 goto err_mand_ie;
1026 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
1027 if (!from_peer)
1028 goto err_no_peer;
1029 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
1030 sizeof(from_peer->ra));
1031 gsm48_parse_ra(&raid, from_peer->ra);
1032 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
1033 "RAI snooping: RAI %s behind BVCI=%u\n",
1034 nsei, osmo_rai_name(&raid), from_peer->bvci);
1035 /* FIXME: This only supports one BSS per RA */
1036 break;
1037 case BSSGP_PDUT_BVC_RESET:
1038 /* If we receive a BVC reset on the signalling endpoint, we
1039 * don't want the SGSN to reset, as the signalling endpoint
1040 * is common for all point-to-point BVCs (and thus all BTS) */
1041 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1042 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1043 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
1044 nsei, bvci);
1045 if (bvci == 0) {
1046 /* FIXME: only do this if SGSN is alive! */
1047 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
1048 "BVC RESET ACK of BVCI=0\n", nsei);
1049 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
1050 nsei, 0, ns_bvci);
1051 }
1052 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
1053 if (!from_peer) {
1054 /* if a PTP-BVC is reset, and we don't know that
1055 * PTP-BVCI yet, we should allocate a new peer */
1056 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for BVCI=%u via NSEI=%u\n", bvci, nsei);
1057 from_peer = gbproxy_peer_alloc(cfg, bvci);
1058 OSMO_ASSERT(from_peer);
1059 from_peer->nsei = nsei;
1060 }
1061
1062 if (!check_peer_nsei(from_peer, nsei))
1063 from_peer->nsei = nsei;
1064
1065 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
1066 struct gprs_ra_id raid;
1067 /* We have a Cell Identifier present in this
1068 * PDU, this means we can extend our local
1069 * state information about this particular cell
1070 * */
1071 memcpy(from_peer->ra,
1072 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
1073 sizeof(from_peer->ra));
1074 gsm48_parse_ra(&raid, from_peer->ra);
1075 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u Cell ID %s\n",
1076 nsei, bvci, osmo_rai_name(&raid));
1077 }
1078 if (cfg->route_to_sgsn2)
1079 copy_to_sgsn2 = 1;
1080 }
1081 break;
1082 }
1083
1084 /* Normally, we can simply pass on all signalling messages from BSS to
1085 * SGSN */
1086 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1087 if (!rc)
1088 return 0;
1089
1090 if (copy_to_sgsn2)
1091 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1092
1093 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
1094err_no_peer:
1095 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
1096 nsei);
1097 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
1098 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1099err_mand_ie:
1100 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
1101 nsei);
1102 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
1103 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1104}
1105
1106/* Receive paging request from SGSN, we need to relay to proper BSS */
1107static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
1108 uint32_t nsei, uint16_t ns_bvci)
1109{
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001110 struct gbproxy_peer *peer;
1111 unsigned int n_peers = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001112 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
1113
1114 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
1115 nsei);
1116 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1117 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001118 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001119 peer = gbproxy_peer_by_bvci(cfg, bvci);
1120 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", bvci);
1121 if (!peer) {
1122 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP PAGING: "
1123 "unable to route: BVCI=%u unknown\n", nsei, bvci);
1124 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1125 return -EINVAL;
1126 }
1127 return gbprox_relay2peer(msg, peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001128 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001129 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001130 /* iterate over all peers and dispatch the paging to each matching one */
1131 llist_for_each_entry(peer, &cfg->bts_peers, list) {
1132 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
1133 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", peer->bvci);
1134 gbprox_relay2peer(msg, peer, ns_bvci);
1135 n_peers++;
1136 }
1137 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001138 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001139 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001140 /* iterate over all peers and dispatch the paging to each matching one */
1141 llist_for_each_entry(peer, &cfg->bts_peers, list) {
1142 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
1143 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", peer->bvci);
1144 gbprox_relay2peer(msg, peer, ns_bvci);
1145 n_peers++;
1146 }
1147 }
Harald Welte53ee2062020-11-24 11:31:13 +01001148 } else if (TLVP_PRESENT(tp, BSSGP_IE_BSS_AREA_ID)) {
1149 /* iterate over all peers and dispatch the paging to each matching one */
1150 llist_for_each_entry(peer, &cfg->bts_peers, list) {
1151 LOGPC(DGPRS, LOGL_INFO, "broadcasting to peer BVCI=%u\n", peer->bvci);
1152 gbprox_relay2peer(msg, peer, ns_bvci);
1153 n_peers++;
1154 }
1155 } else {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001156 LOGPC(DGPRS, LOGL_INFO, "\n");
Harald Welte53ee2062020-11-24 11:31:13 +01001157 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
1158 "unable to route, missing IE\n", nsei);
1159 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1160 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001161
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001162 if (n_peers == 0) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001163 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Harald Welte53ee2062020-11-24 11:31:13 +01001164 "unable to route, no destination found\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001165 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1166 return -EINVAL;
1167 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001168 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001169}
1170
1171/* Receive an incoming BVC-RESET message from the SGSN */
1172static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
1173 struct msgb *orig_msg,
1174 struct msgb *msg, struct tlv_parsed *tp,
1175 uint32_t nsei, uint16_t ns_bvci)
1176{
1177 struct gbproxy_peer *peer;
1178 uint16_t ptp_bvci;
1179
1180 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1181 rate_ctr_inc(&cfg->ctrg->
1182 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1183 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
1184 NULL, orig_msg);
1185 }
1186 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1187
1188 if (ptp_bvci >= 2) {
1189 /* A reset for a PTP BVC was received, forward it to its
1190 * respective peer */
1191 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
1192 if (!peer) {
1193 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
1194 nsei, ptp_bvci);
1195 rate_ctr_inc(&cfg->ctrg->
1196 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1197 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
1198 &ptp_bvci, orig_msg);
1199 }
1200 return gbprox_relay2peer(msg, peer, ns_bvci);
1201 }
1202
1203 /* A reset for the Signalling entity has been received
1204 * from the SGSN. As the signalling BVCI is shared
1205 * among all the BSS's that we multiplex, it needs to
1206 * be relayed */
1207 llist_for_each_entry(peer, &cfg->bts_peers, list)
1208 gbprox_relay2peer(msg, peer, ns_bvci);
1209
1210 return 0;
1211}
1212
1213/* Receive an incoming signalling message from the SGSN-side NS-VC */
1214static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
1215 struct msgb *orig_msg, uint32_t nsei,
1216 uint16_t ns_bvci)
1217{
1218 struct bssgp_normal_hdr *bgph =
1219 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
1220 struct tlv_parsed tp;
1221 uint8_t pdu_type = bgph->pdu_type;
1222 int data_len;
1223 struct gbproxy_peer *peer;
1224 uint16_t bvci;
1225 struct msgb *msg;
1226 int rc = 0;
1227 int cause;
1228
1229 if (ns_bvci != 0 && ns_bvci != 1) {
1230 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
1231 "signalling\n", nsei, ns_bvci);
1232 /* FIXME: Send proper error message */
1233 return -EINVAL;
1234 }
1235
1236 /* we actually should never see those two for BVCI == 0, but double-check
1237 * just to make sure */
1238 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1239 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
1240 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
1241 "signalling\n", nsei);
1242 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1243 }
1244
1245 msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1246 gbprox_process_bssgp_dl(cfg, msg, NULL);
1247 /* Update message info */
1248 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1249 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
1250 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1251
1252 switch (pdu_type) {
1253 case BSSGP_PDUT_BVC_RESET:
1254 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
1255 break;
1256 case BSSGP_PDUT_BVC_RESET_ACK:
1257 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1258 break;
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001259 /* simple case: BVCI IE is mandatory */
1260 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1261 goto err_mand_ie;
1262 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1263 if (bvci == BVCI_SIGNALLING) {
1264 /* TODO: Reset all PTP BVCIs */
1265 } else {
1266 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1267 }
1268 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001269 case BSSGP_PDUT_FLUSH_LL:
1270 /* simple case: BVCI IE is mandatory */
1271 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1272 goto err_mand_ie;
1273 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1274 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1275 break;
1276 case BSSGP_PDUT_PAGING_PS:
1277 case BSSGP_PDUT_PAGING_CS:
1278 /* process the paging request (LAI/RAI lookup) */
1279 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
1280 break;
1281 case BSSGP_PDUT_STATUS:
1282 /* Some exception has occurred */
1283 LOGP(DGPRS, LOGL_NOTICE,
1284 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
1285 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1286 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1287 goto err_mand_ie;
1288 }
1289 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
1290 LOGPC(DGPRS, LOGL_NOTICE,
1291 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
1292 bssgp_cause_str(cause));
1293 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1294 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1295 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1296
1297 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1298 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1299 } else
1300 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1301 break;
1302 /* those only exist in the SGSN -> BSS direction */
1303 case BSSGP_PDUT_SUSPEND_ACK:
1304 case BSSGP_PDUT_SUSPEND_NACK:
1305 case BSSGP_PDUT_RESUME_ACK:
1306 case BSSGP_PDUT_RESUME_NACK:
1307 /* RAI IE is mandatory */
1308 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1309 goto err_mand_ie;
1310 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
1311 if (!peer)
1312 goto err_no_peer;
1313 rc = gbprox_relay2peer(msg, peer, ns_bvci);
1314 break;
1315 case BSSGP_PDUT_BVC_BLOCK_ACK:
1316 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1317 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1318 goto err_mand_ie;
1319 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1320 if (bvci == 0) {
1321 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
1322 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
1323 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1324 /* should we send STATUS ? */
1325 rate_ctr_inc(&cfg->ctrg->
1326 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1327 } else {
1328 /* Mark BVC as (un)blocked */
1329 block_unblock_peer(cfg, bvci, pdu_type);
1330 }
1331 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1332 break;
1333 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
1334 LOGP(DGPRS, LOGL_ERROR,
1335 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
1336 rate_ctr_inc(&cfg->ctrg->
1337 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
1338 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
1339 break;
1340 default:
1341 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type %s not supported\n", bssgp_pdu_str(pdu_type));
1342 rate_ctr_inc(&cfg->ctrg->
1343 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1344 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1345 break;
1346 }
1347
1348 msgb_free(msg);
1349
1350 return rc;
1351err_mand_ie:
1352 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
1353 nsei);
1354 rate_ctr_inc(&cfg->ctrg->
1355 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1356 msgb_free(msg);
1357 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
1358err_no_peer:
1359 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
1360 nsei);
1361 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
1362 msgb_free(msg);
1363 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
1364}
1365
1366static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1367{
1368 return nsei == cfg->nsip_sgsn_nsei ||
1369 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1370}
1371
Alexander Couzens951e1332020-09-22 13:21:46 +02001372int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001373{
1374 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001375 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1376 struct gprs_ns2_inst *nsi = cfg->nsi;
1377 struct osmo_gprs_ns2_prim nsp = {};
1378
1379 nsp.bvci = msgb_bvci(msg);
1380 nsp.nsei = msgb_nsei(msg);
1381
1382 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
1383 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1384
1385 return rc;
1386}
1387
1388/* Main input function for Gb proxy */
1389int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1390{
1391 int rc;
1392 uint16_t nsei = msgb_nsei(msg);
1393 uint16_t ns_bvci = msgb_bvci(msg);
1394 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1395
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001396 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
1397
1398 /* Only BVCI=0 messages need special treatment */
1399 if (ns_bvci == 0 || ns_bvci == 1) {
1400 if (remote_end_is_sgsn)
1401 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
1402 else
1403 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
1404 } else {
1405 /* All other BVCI are PTP */
1406 if (remote_end_is_sgsn)
Alexander Couzens951e1332020-09-22 13:21:46 +02001407 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001408 ns_bvci);
1409 else
Alexander Couzens951e1332020-09-22 13:21:46 +02001410 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001411 ns_bvci);
1412 }
1413
1414 return rc;
1415}
1416
Alexander Couzens951e1332020-09-22 13:21:46 +02001417/* TODO: What about handling:
1418 * NS_AFF_CAUSE_VC_FAILURE,
1419 NS_AFF_CAUSE_VC_RECOVERY,
1420 NS_AFF_CAUSE_FAILURE,
1421 NS_AFF_CAUSE_RECOVERY,
1422 osmocom own causes
1423 NS_AFF_CAUSE_SNS_CONFIGURED,
1424 NS_AFF_CAUSE_SNS_FAILURE,
1425 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001426
Alexander Couzens951e1332020-09-22 13:21:46 +02001427void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001428{
Alexander Couzens951e1332020-09-22 13:21:46 +02001429 /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, peer->bvci, 0);
1430 * TODO: sgsn nsei available/unavailable
1431 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001432 struct gbproxy_peer *peer;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001433
Alexander Couzens951e1332020-09-22 13:21:46 +02001434 switch (nsp->u.status.cause) {
1435 case NS_AFF_CAUSE_SNS_FAILURE:
1436 case NS_AFF_CAUSE_SNS_CONFIGURED:
1437 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001438
Alexander Couzens951e1332020-09-22 13:21:46 +02001439 case NS_AFF_CAUSE_RECOVERY:
1440 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
1441 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1442 /* look-up or create the BTS context for this BVC */
1443 struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei);
1444 if (!bctx)
1445 bctx = btsctx_alloc(nsp->bvci, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001446
Alexander Couzens951e1332020-09-22 13:21:46 +02001447 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 +02001448 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001449 break;
1450 case NS_AFF_CAUSE_FAILURE:
1451 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1452 /* sgsn */
1453 /* TODO: BSVC: block all PtP towards bss */
1454 rate_ctr_inc(&cfg->ctrg->
1455 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1456 } else {
1457 /* bss became unavailable */
1458 peer = gbproxy_peer_by_nsei(cfg, nsp->nsei);
1459 if (!peer) {
1460 /* TODO: use primitive name + status cause name */
1461 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown peer NSEI=%u\n",
1462 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001463 break;
1464 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001465
1466 if (!peer->blocked)
1467 break;
1468 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei,
1469 peer->bvci, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001470 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001471 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
1472 break;
1473 default:
1474 LOGP(DPCU, LOGL_NOTICE,
1475 "NS: %s Unknown prim %d from NS\n",
1476 get_value_string(osmo_prim_op_names, nsp->oph.operation),
1477 nsp->oph.primitive);
1478 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001479 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001480}
1481
1482
1483/* called by the ns layer */
1484int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1485{
1486 struct osmo_gprs_ns2_prim *nsp;
1487 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1488 int rc = 0;
1489
1490 if (oph->sap != SAP_NS)
1491 return 0;
1492
1493 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1494
1495 if (oph->operation != PRIM_OP_INDICATION) {
1496 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
1497 get_value_string(osmo_prim_op_names, oph->operation),
1498 oph->operation);
1499 return 0;
1500 }
1501
1502 switch (oph->primitive) {
1503 case PRIM_NS_UNIT_DATA:
1504 /* hand the message into the BSSGP implementation */
1505 msgb_bssgph(oph->msg) = oph->msg->l3h;
1506 msgb_bvci(oph->msg) = nsp->bvci;
1507 msgb_nsei(oph->msg) = nsp->nsei;
1508
1509 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001510 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001511 break;
1512 case PRIM_NS_STATUS:
1513 gprs_ns_prim_status_cb(cfg, nsp);
1514 break;
1515 default:
1516 LOGP(DPCU, LOGL_NOTICE,
1517 "NS: %s Unknown prim %d from NS\n",
1518 get_value_string(osmo_prim_op_names, oph->operation),
1519 oph->primitive);
1520 break;
1521 }
1522
1523 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001524}
1525
1526void gbprox_reset(struct gbproxy_config *cfg)
1527{
1528 struct gbproxy_peer *peer, *tmp;
1529
1530 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
1531 gbproxy_peer_free(peer);
1532
1533 rate_ctr_group_free(cfg->ctrg);
1534 gbproxy_init_config(cfg);
1535}
1536
1537int gbproxy_init_config(struct gbproxy_config *cfg)
1538{
1539 struct timespec tp;
1540
1541 INIT_LLIST_HEAD(&cfg->bts_peers);
1542 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1543 if (!cfg->ctrg) {
1544 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1545 return -1;
1546 }
1547 osmo_clock_gettime(CLOCK_REALTIME, &tp);
1548
1549 return 0;
1550}