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