blob: a4ad7af09d1fb9a735d5edd87627b8100cbc7bb7 [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
Daniel Willmann8f407b12020-12-02 19:33:50 +010034#include <osmocom/core/logging.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020035#include <osmocom/core/talloc.h>
36#include <osmocom/core/select.h>
37#include <osmocom/core/rate_ctr.h>
38#include <osmocom/core/stats.h>
39
Alexander Couzens951e1332020-09-22 13:21:46 +020040#include <osmocom/gprs/gprs_ns2.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020041#include <osmocom/gprs/gprs_bssgp.h>
Alexander Couzens951e1332020-09-22 13:21:46 +020042#include <osmocom/gprs/gprs_bssgp_bss.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020043
44#include <osmocom/gsm/gsm_utils.h>
45
46#include <osmocom/sgsn/signal.h>
47#include <osmocom/sgsn/debug.h>
48#include <osmocom/sgsn/gprs_gb_parse.h>
49#include <osmocom/sgsn/gb_proxy.h>
50
51#include <osmocom/sgsn/gprs_llc.h>
52#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
53#include <osmocom/sgsn/gprs_utils.h>
54
55extern void *tall_sgsn_ctx;
56
57static const struct rate_ctr_desc global_ctr_description[] = {
58 { "inv-bvci", "Invalid BVC Identifier " },
59 { "inv-lai", "Invalid Location Area Identifier" },
60 { "inv-rai", "Invalid Routing Area Identifier " },
61 { "inv-nsei", "No BVC established for NSEI " },
62 { "proto-err:bss", "BSSGP protocol error (BSS )" },
63 { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
64 { "not-supp:bss", "Feature not supported (BSS )" },
65 { "not-supp:sgsn", "Feature not supported (SGSN)" },
66 { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
67 { "tx-err:sgsn", "NS Transmission error (SGSN)" },
68 { "error", "Other error " },
69 { "mod-peer-err", "Patch error: no peer " },
70};
71
72static const struct rate_ctr_group_desc global_ctrg_desc = {
73 .group_name_prefix = "gbproxy:global",
74 .group_description = "GBProxy Global Statistics",
75 .num_ctr = ARRAY_SIZE(global_ctr_description),
76 .ctr_desc = global_ctr_description,
77 .class_id = OSMO_STATS_CLASS_GLOBAL,
78};
79
80static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Daniel Willmann35f7d332020-11-03 21:11:45 +010081 uint16_t ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020082static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
83 uint16_t ns_bvci, uint16_t sgsn_nsei);
84static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
85
86static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
87{
Daniel Willmann5e595ca2020-12-02 13:54:21 +010088 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +010089 OSMO_ASSERT(peer->nse);
90
91 if (peer->nse->nsei != nsei) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +010092 LOGPBVC(peer, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
93 "via NSE(%05u/BSS)\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020094 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
95 return 0;
96 }
97
98 return 1;
99}
100
101/* strip off the NS header */
102static void strip_ns_hdr(struct msgb *msg)
103{
104 int strip_len = msgb_bssgph(msg) - msg->data;
105 msgb_pull(msg, strip_len);
106}
107
108/* Transmit Chapter 9.2.10 Identity Request */
109static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
110{
111 struct gsm48_hdr *gh;
112
113 id_type &= GSM_MI_TYPE_MASK;
114
115 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
116 gh->proto_discr = GSM48_PDISC_MM_GPRS;
117 gh->msg_type = GSM48_MT_GMM_ID_REQ;
118 gh->data[0] = id_type;
119}
120
121/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
122static void gprs_put_mo_detach_acc(struct msgb *msg)
123{
124 struct gsm48_hdr *gh;
125
126 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
127 gh->proto_discr = GSM48_PDISC_MM_GPRS;
128 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
129 gh->data[0] = 0; /* no force to standby */
130}
131
132static void gprs_push_llc_ui(struct msgb *msg,
133 int is_uplink, unsigned sapi, unsigned nu)
134{
135 const uint8_t e_bit = 0;
136 const uint8_t pm_bit = 1;
137 const uint8_t cr_bit = is_uplink ? 0 : 1;
138 uint8_t *llc;
139 uint8_t *fcs_field;
140 uint32_t fcs;
141
142 nu &= 0x01ff; /* 9 Bit */
143
144 llc = msgb_push(msg, 3);
145 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
146 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
147 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
148
149 fcs = gprs_llc_fcs(llc, msgb_length(msg));
150 fcs_field = msgb_put(msg, 3);
151 fcs_field[0] = (uint8_t)(fcs >> 0);
152 fcs_field[1] = (uint8_t)(fcs >> 8);
153 fcs_field[2] = (uint8_t)(fcs >> 16);
154}
155
156static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
157 uint32_t tlli)
158{
159 struct bssgp_ud_hdr *budh;
160 uint8_t *llc = msgb_data(msg);
161 size_t llc_size = msgb_length(msg);
162 const size_t llc_ie_hdr_size = 3;
163 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
164 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
165
166 const size_t bssgp_overhead = sizeof(*budh) +
167 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
168 uint8_t *ie;
169 uint32_t tlli_be = htonl(tlli);
170
171 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
172
173 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
174 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
175 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
176
177 ie = budh->data;
178 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
179 ie += TVLV_GROSS_LEN(sizeof(lifetime));
180
181 /* Note: Add alignment before the LLC IE if inserting other IE */
182
183 *(ie++) = BSSGP_IE_LLC_PDU;
184 *(ie++) = llc_size / 256;
185 *(ie++) = llc_size % 256;
186
187 OSMO_ASSERT(ie == llc);
188
189 msgb_bssgph(msg) = (uint8_t *)budh;
190 msgb_tlli(msg) = tlli;
191}
192
193/* update peer according to the BSS message */
194static void gbprox_update_current_raid(uint8_t *raid_enc,
195 struct gbproxy_peer *peer,
196 const char *log_text)
197{
198 struct gbproxy_patch_state *state = &peer->patch_state;
199 const struct osmo_plmn_id old_plmn = state->local_plmn;
200 struct gprs_ra_id raid;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100201 OSMO_ASSERT(peer->nse);
202 struct gbproxy_config *cfg = peer->nse->cfg;
203 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200204
205 if (!raid_enc)
206 return;
207
208 gsm48_parse_ra(&raid, raid_enc);
209
210 /* save source side MCC/MNC */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100211 if (!cfg->core_plmn.mcc || raid.mcc == cfg->core_plmn.mcc) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200212 state->local_plmn.mcc = 0;
213 } else {
214 state->local_plmn.mcc = raid.mcc;
215 }
216
Daniel Willmanne50550e2020-11-26 18:19:21 +0100217 if (!cfg->core_plmn.mnc
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200218 || !osmo_mnc_cmp(raid.mnc, raid.mnc_3_digits,
Daniel Willmanne50550e2020-11-26 18:19:21 +0100219 cfg->core_plmn.mnc, cfg->core_plmn.mnc_3_digits)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200220 state->local_plmn.mnc = 0;
221 state->local_plmn.mnc_3_digits = false;
222 } else {
223 state->local_plmn.mnc = raid.mnc;
224 state->local_plmn.mnc_3_digits = raid.mnc_3_digits;
225 }
226
227 if (osmo_plmn_cmp(&old_plmn, &state->local_plmn))
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100228 LOGPBVC(peer, LOGL_NOTICE,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200229 "Patching RAID %sactivated, msg: %s, "
230 "local: %s, core: %s\n",
231 state->local_plmn.mcc || state->local_plmn.mnc ?
232 "" : "de",
233 log_text,
234 osmo_plmn_name(&state->local_plmn),
Daniel Willmanne50550e2020-11-26 18:19:21 +0100235 osmo_plmn_name2(&cfg->core_plmn));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200236}
237
238uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
239 uint32_t sgsn_ptmsi)
240{
241 uint32_t bss_ptmsi;
242 int max_retries = 23, rc = 0;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100243 if (!peer->nse->cfg->patch_ptmsi) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200244 bss_ptmsi = sgsn_ptmsi;
245 } else {
246 do {
247 rc = osmo_get_rand_id((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi));
248 if (rc < 0) {
249 bss_ptmsi = GSM_RESERVED_TMSI;
250 break;
251 }
252
253 bss_ptmsi = bss_ptmsi | GSM23003_TMSI_SGSN_MASK;
254
255 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
256 bss_ptmsi = GSM_RESERVED_TMSI;
257 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
258 }
259
260 if (bss_ptmsi == GSM_RESERVED_TMSI)
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100261 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 +0200262
263 return bss_ptmsi;
264}
265
266uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
267 struct gbproxy_link_info *link_info,
268 uint32_t bss_tlli)
269{
270 uint32_t sgsn_tlli;
271 int max_retries = 23, rc = 0;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100272 if (!peer->nse->cfg->patch_ptmsi) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200273 sgsn_tlli = bss_tlli;
274 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
275 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
276 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
277 TLLI_FOREIGN);
278 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
279 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
280 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
281 TLLI_LOCAL);
282 } else {
283 do {
284 /* create random TLLI, 0b01111xxx... */
285 rc = osmo_get_rand_id((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli));
286 if (rc < 0) {
287 sgsn_tlli = 0;
288 break;
289 }
290
291 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
292
293 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
294 sgsn_tlli = 0;
295 } while (!sgsn_tlli && max_retries--);
296 }
297
298 if (!sgsn_tlli)
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100299 LOGPBVC(peer, LOGL_ERROR, "Failed to allocate an SGSN TLLI: %d (%s)\n", rc, strerror(-rc));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200300
301 return sgsn_tlli;
302}
303
304void gbproxy_reset_link(struct gbproxy_link_info *link_info)
305{
306 gbproxy_reset_imsi_acquisition(link_info);
307}
308
309/* Returns != 0 iff IMSI acquisition was in progress */
310static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
311{
312 int in_progress = 0;
313 if (!link_info)
314 return 0;
315
316 if (link_info->imsi_acq_pending)
317 in_progress = 1;
318
319 gbproxy_link_info_discard_messages(link_info);
320 link_info->imsi_acq_pending = false;
321
322 return in_progress;
323}
324
325static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
326{
327 gbproxy_restart_imsi_acquisition(link_info);
328 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
329}
330
331/* Got identity response with IMSI, assuming the request had
332 * been generated by the gbproxy */
333static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
334 time_t now,
335 struct gbproxy_link_info* link_info)
336{
337 int rc;
338 struct msgb *stored_msg;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100339 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100340 OSMO_ASSERT(peer->nse);
341 struct gbproxy_config *cfg = peer->nse->cfg;
342 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200343
344 /* Patch and flush stored messages towards the SGSN */
345 while ((stored_msg = msgb_dequeue_count(&link_info->stored_msgs,
346 &link_info->stored_msgs_len))) {
347 struct gprs_gb_parse_context tmp_parse_ctx = {0};
348 tmp_parse_ctx.to_bss = 0;
349 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
350 int len_change = 0;
351
352 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
353 msgb_bssgp_len(stored_msg),
354 &tmp_parse_ctx);
355 gbproxy_patch_bssgp(stored_msg, msgb_bssgph(stored_msg),
356 msgb_bssgp_len(stored_msg),
357 peer, link_info, &len_change,
358 &tmp_parse_ctx);
359
360 rc = gbproxy_update_link_state_after(peer, link_info, now,
361 &tmp_parse_ctx);
362 if (rc == 1) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100363 LOGPBVC_CAT(peer, DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200364 msgb_free(stored_msg);
365 return -1;
366 }
367
Daniel Willmanne50550e2020-11-26 18:19:21 +0100368 rc = gbprox_relay2sgsn(cfg, stored_msg,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200369 msgb_bvci(stored_msg), link_info->sgsn_nsei);
370
371 if (rc < 0)
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100372 LOGPBVC_CAT(peer, DLLC, LOGL_ERROR,
373 "failed to send stored message "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200374 "(%s)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200375 tmp_parse_ctx.llc_msg_name ?
376 tmp_parse_ctx.llc_msg_name : "BSSGP");
377 msgb_free(stored_msg);
378 }
379
380 return 0;
381}
382
383static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
384 struct gbproxy_link_info* link_info,
385 uint16_t bvci,
386 struct msgb *msg /* Takes msg ownership */)
387{
388 int rc;
389
390 /* Workaround to avoid N(U) collisions and to enable a restart
391 * of the IMSI acquisition procedure. This will work unless the
392 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
393 * (see GSM 04.64, 8.4.2). */
394 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
395 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
396
397 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
Alexander Couzens951e1332020-09-22 13:21:46 +0200398 msg->l3h = msg->data;
399
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200400 rc = gbprox_relay2peer(msg, peer, bvci);
401 msgb_free(msg);
402 return rc;
403}
404
405static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
406 struct gbproxy_link_info* link_info,
407 uint16_t bvci)
408{
409 struct msgb *idreq_msg;
410
411 /* Send IDENT REQ */
412 idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI");
413 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
414 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
415}
416
417static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
418 struct gbproxy_link_info* link_info,
419 uint16_t bvci)
420{
421 struct msgb *detacc_msg;
422
423 /* Send DETACH ACC */
424 detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC");
425 gprs_put_mo_detach_acc(detacc_msg);
426 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
427}
428
429/* Return != 0 iff msg still needs to be processed */
430static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
431 struct msgb *msg,
432 time_t now,
433 struct gbproxy_link_info* link_info,
434 struct gprs_gb_parse_context *parse_ctx)
435{
436 struct msgb *stored_msg;
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100437 OSMO_ASSERT(peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100438 OSMO_ASSERT(peer->nse);
439 struct gbproxy_config *cfg = peer->nse->cfg;
440 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200441
442 if (!link_info)
443 return 1;
444
445 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
446 return 1;
447
448 if (parse_ctx->g48_hdr)
449 switch (parse_ctx->g48_hdr->msg_type)
450 {
451 case GSM48_MT_GMM_RA_UPD_REQ:
452 case GSM48_MT_GMM_ATTACH_REQ:
453 if (gbproxy_restart_imsi_acquisition(link_info)) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100454 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
455 " IMSI acquisition was in progress "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200456 "when receiving an %s.\n",
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100457 parse_ctx->llc_msg_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200458 }
459 break;
460 case GSM48_MT_GMM_DETACH_REQ:
461 /* Nothing has been sent to the SGSN yet */
462 if (link_info->imsi_acq_pending) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100463 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
464 "IMSI acquisition was in progress "
465 "when receiving a DETACH_REQ.\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200466 }
467 if (!parse_ctx->invalidate_tlli) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100468 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
469 "IMSI not yet acquired, "
470 "faking a DETACH_ACC.\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200471 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
472 parse_ctx->invalidate_tlli = 1;
473 }
474 gbproxy_reset_imsi_acquisition(link_info);
475 gbproxy_update_link_state_after(peer, link_info, now,
476 parse_ctx);
477 return 0;
478 }
479
480 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
481 int is_ident_resp =
482 parse_ctx->g48_hdr &&
483 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
484 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
485
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100486 LOGPBVC_CAT(peer, DLLC, LOGL_DEBUG,
487 "IMSI acquisition succeeded, "
488 "flushing stored messages\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200489 /* The IMSI is now available. If flushing the messages fails,
490 * then link_info has been deleted and we should return
491 * immediately. */
492 if (gbproxy_flush_stored_messages(peer, now, link_info) < 0)
493 return 0;
494
495 gbproxy_reset_imsi_acquisition(link_info);
496
497 /* This message is most probably the response to the ident
498 * request sent by gbproxy_acquire_imsi(). Don't forward it to
499 * the SGSN. */
500 return !is_ident_resp;
501 }
502
503 /* The message cannot be processed since the IMSI is still missing */
504
505 /* If queue is getting too large, drop oldest msgb before adding new one */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100506 if (cfg->stored_msgs_max_len > 0) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200507 int exceeded_max_len = link_info->stored_msgs_len
Daniel Willmanne50550e2020-11-26 18:19:21 +0100508 + 1 - cfg->stored_msgs_max_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200509
510 for (; exceeded_max_len > 0; exceeded_max_len--) {
511 struct msgb *msgb_drop;
512 msgb_drop = msgb_dequeue_count(&link_info->stored_msgs,
513 &link_info->stored_msgs_len);
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100514 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
515 "Dropping stored msgb from list "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200516 "(!acq imsi, length %d, max_len exceeded)\n",
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100517 link_info->stored_msgs_len);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200518
519 msgb_free(msgb_drop);
520 }
521 }
522
523 /* Enqueue unpatched messages */
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100524 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
525 "IMSI acquisition in progress, "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200526 "storing message (%s)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200527 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
528
529 stored_msg = bssgp_msgb_copy(msg, "process_bssgp_ul");
530 msgb_enqueue_count(&link_info->stored_msgs, stored_msg,
531 &link_info->stored_msgs_len);
532
533 if (!link_info->imsi_acq_pending) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100534 LOGPBVC_CAT(peer, DLLC, LOGL_INFO,
535 "IMSI is required but not available, "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200536 "initiating identification procedure (%s)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200537 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
538
539 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
540
541 /* There is no explicit retransmission handling, the
542 * implementation relies on the MS doing proper retransmissions
543 * of the triggering message instead */
544
545 link_info->imsi_acq_pending = true;
546 }
547
548 return 0;
549}
550
551struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
552 struct msgb *msg,
553 struct gprs_gb_parse_context *parse_ctx)
554{
555 struct gbproxy_peer *peer = NULL;
556
557 if (msgb_bvci(msg) >= 2)
558 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
559
560 if (!peer && !parse_ctx->to_bss)
561 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
562
563 if (!peer)
564 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
565
566 if (!peer) {
567 LOGP(DLLC, LOGL_INFO,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100568 "NSE(%05u/%s) patching: didn't find peer for message, "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200569 "PDU %d\n",
570 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
571 parse_ctx->pdu_type);
572 /* Increment counter */
573 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
574 }
575 return peer;
576}
577
578/* patch BSSGP message */
579static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
580 struct msgb *msg,
581 struct gbproxy_peer *peer)
582{
583 struct gprs_gb_parse_context parse_ctx = {0};
584 int rc;
585 int len_change = 0;
586 time_t now;
587 struct timespec ts = {0,};
588 struct gbproxy_link_info *link_info = NULL;
589 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
590
591 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
592 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
593 return 1;
594
595 parse_ctx.to_bss = 0;
596 parse_ctx.peer_nsei = msgb_nsei(msg);
597
598 /* Parse BSSGP/LLC */
599 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
600 &parse_ctx);
601
602 if (!rc && !parse_ctx.need_decryption) {
603 LOGP(DGPRS, LOGL_ERROR,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100604 "NSE(%05u/BSS) patching: failed to parse invalid %s message\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200605 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
606 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
607 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100608 "NSE(%05u/BSS) invalid message was: %s\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200609 msgb_nsei(msg), msgb_hexdump(msg));
610 return 0;
611 }
612
613 /* Get peer */
614 if (!peer)
615 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
616
617 if (!peer)
618 return 0;
619
620
621 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
622 now = ts.tv_sec;
623
624 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
625 parse_ctx.llc_msg_name);
626
627 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
628
629 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
630
631 if (parse_ctx.g48_hdr) {
632 switch (parse_ctx.g48_hdr->msg_type) {
633 case GSM48_MT_GMM_ATTACH_REQ:
634 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
635 break;
636 case GSM48_MT_GMM_DETACH_REQ:
637 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
638 break;
639 case GSM48_MT_GMM_ATTACH_COMPL:
640 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
641 break;
642 case GSM48_MT_GMM_RA_UPD_REQ:
643 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
644 break;
645 case GSM48_MT_GMM_RA_UPD_COMPL:
646 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
647 break;
648 case GSM48_MT_GMM_STATUS:
649 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
650 break;
651 case GSM48_MT_GSM_ACT_PDP_REQ:
652 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
653 break;
654 case GSM48_MT_GSM_DEACT_PDP_REQ:
655 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
656 break;
657
658 default:
659 break;
660 }
661 }
662
663 if (link_info && cfg->route_to_sgsn2) {
664 if (cfg->acquire_imsi && link_info->imsi_len == 0)
665 sgsn_nsei = 0xffff;
666 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
667 link_info))
668 sgsn_nsei = cfg->nsip_sgsn2_nsei;
669 }
670
671 if (link_info)
672 link_info->sgsn_nsei = sgsn_nsei;
673
674 /* Handle IMSI acquisition */
675 if (cfg->acquire_imsi) {
676 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
677 &parse_ctx);
678 if (rc <= 0)
679 return rc;
680 }
681
682 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
683 peer, link_info, &len_change, &parse_ctx);
684
685 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
686
687 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
688 /* Send message directly to the selected SGSN */
689 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
690 /* Don't let the calling code handle the transmission */
691 return 0;
692 }
693
694 return 1;
695}
696
697/* patch BSSGP message to use core_plmn.mcc/mnc on the SGSN side */
698static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
699 struct msgb *msg,
700 struct gbproxy_peer *peer)
701{
702 struct gprs_gb_parse_context parse_ctx = {0};
703 int rc;
704 int len_change = 0;
705 time_t now;
706 struct timespec ts = {0,};
707 struct gbproxy_link_info *link_info = NULL;
708
709 if (!cfg->core_plmn.mcc && !cfg->core_plmn.mnc && !cfg->core_apn &&
710 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
711 return;
712
713 parse_ctx.to_bss = 1;
714 parse_ctx.peer_nsei = msgb_nsei(msg);
715
716 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
717 &parse_ctx);
718
719 if (!rc && !parse_ctx.need_decryption) {
720 LOGP(DGPRS, LOGL_ERROR,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100721 "NSE(%05u/SGSN) patching: failed to parse invalid %s message\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200722 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
723 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
724 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann3696dce2020-12-02 16:08:02 +0100725 "NSE(%05u/SGSN) invalid message was: %s\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200726 msgb_nsei(msg), msgb_hexdump(msg));
727 return;
728 }
729
730 /* Get peer */
731 if (!peer)
732 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
733
734 if (!peer)
735 return;
736
737 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
738 now = ts.tv_sec;
739
740 if (parse_ctx.g48_hdr) {
741 switch (parse_ctx.g48_hdr->msg_type) {
742 case GSM48_MT_GMM_ATTACH_ACK:
743 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
744 break;
745 case GSM48_MT_GMM_ATTACH_REJ:
746 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
747 break;
748 case GSM48_MT_GMM_DETACH_ACK:
749 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
750 break;
751 case GSM48_MT_GMM_RA_UPD_ACK:
752 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
753 break;
754 case GSM48_MT_GMM_RA_UPD_REJ:
755 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
756 break;
757 case GSM48_MT_GMM_STATUS:
758 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
759 break;
760 case GSM48_MT_GSM_ACT_PDP_ACK:
761 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
762 break;
763 case GSM48_MT_GSM_ACT_PDP_REJ:
764 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
765 break;
766 case GSM48_MT_GSM_DEACT_PDP_ACK:
767 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
768 break;
769
770 default:
771 break;
772 }
773 }
774
775 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
776
777 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
778
779 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
780 peer, link_info, &len_change, &parse_ctx);
781
782 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
783
784 return;
785}
786
787/* feed a message down the NS-VC associated with the specified peer */
788static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
789 uint16_t ns_bvci, uint16_t sgsn_nsei)
790{
791 /* create a copy of the message so the old one can
792 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200793 struct gprs_ns2_inst *nsi = cfg->nsi;
794 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200795 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
796 int rc;
797
Daniel Willmann3696dce2020-12-02 16:08:02 +0100798 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200799 msgb_nsei(msg), ns_bvci, sgsn_nsei);
800
Alexander Couzens951e1332020-09-22 13:21:46 +0200801 nsp.bvci = ns_bvci;
802 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200803
804 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200805 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
806 PRIM_OP_REQUEST, msg);
807 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200808 if (rc < 0)
809 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200810 return rc;
811}
812
Daniel Willmann76205712020-11-30 17:08:58 +0100813/* feed a message down the NSE */
814static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100815 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200816{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100817 OSMO_ASSERT(nse);
818 OSMO_ASSERT(nse->cfg);
819
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200820 /* create a copy of the message so the old one can
821 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100822 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Alexander Couzens951e1332020-09-22 13:21:46 +0200823 struct osmo_gprs_ns2_prim nsp = {};
Daniel Willmann76205712020-11-30 17:08:58 +0100824 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Harald Weltefe059582020-11-18 12:01:46 +0100825 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200826 int rc;
827
Daniel Willmann3696dce2020-12-02 16:08:02 +0100828 DEBUGP(DGPRS, "NSE(%05u/SGSN)-BVC(%05u) proxying SGSN->BSS NSE(%05u/BSS)\n",
Daniel Willmanne50550e2020-11-26 18:19:21 +0100829 msgb_nsei(msg), ns_bvci, nse->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200830
Alexander Couzens951e1332020-09-22 13:21:46 +0200831 nsp.bvci = ns_bvci;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100832 nsp.nsei = nse->nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200833
834 /* Strip the old NS header, it will be replaced with a new one */
835 strip_ns_hdr(msg);
836
Harald Weltefe059582020-11-18 12:01:46 +0100837 /* TS 48.018 Section 5.4.2: The link selector parameter is
838 * defined in 3GPP TS 48.016. At one side of the Gb interface,
839 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
840 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
841 * underlying network service. */
842 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
843 nsp.u.unitdata.link_selector = tlli;
844
Alexander Couzens951e1332020-09-22 13:21:46 +0200845 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
846 PRIM_OP_REQUEST, msg);
847 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Daniel Willmann76205712020-11-30 17:08:58 +0100848 /* FIXME: We need a counter group for gbproxy_nse */
849 //if (rc < 0)
850 // rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
851
852 return rc;
853}
854
855/* feed a message down the NS-VC associated with the specified peer */
856static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
857 uint16_t ns_bvci)
858{
859 int rc;
860 struct gbproxy_nse *nse = peer->nse;
861 OSMO_ASSERT(nse);
862
863 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200864 if (rc < 0)
865 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
866
867 return rc;
868}
869
870static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
871{
872 struct gbproxy_peer *peer;
873
874 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
875 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100876 LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200877 ptp_bvci);
878 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
879 return -ENOENT;
880 }
881
882 switch (pdu_type) {
883 case BSSGP_PDUT_BVC_BLOCK_ACK:
884 peer->blocked = true;
885 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
886 break;
887 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
888 peer->blocked = false;
889 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
890 break;
891 default:
892 break;
893 }
894 return 0;
895}
896
897/* Send a message to a peer identified by ptp_bvci but using ns_bvci
898 * in the NS hdr */
899static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
900 uint16_t ns_bvci)
901{
902 struct gbproxy_peer *peer;
903
904 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
905 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100906 LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200907 ptp_bvci);
908 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
909 return -ENOENT;
910 }
911
912 return gbprox_relay2peer(msg, peer, ns_bvci);
913}
914
915int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
916{
917 return 0;
918}
919
920/* Receive an incoming PTP message from a BSS-side NS-VC */
921static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
922 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200923 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200924{
925 struct gbproxy_peer *peer;
926 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
927 uint8_t pdu_type = bgph->pdu_type;
928 int rc;
929
930 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
931 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100932 LOGP(DGPRS, LOGL_NOTICE, "BVC(%05u/??) Didn't find peer "
933 "for PTP message from NSE(%05u/BSS), "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200934 "discarding message\n",
Alexander Couzens951e1332020-09-22 13:21:46 +0200935 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200936 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
937 &ns_bvci, msg);
938 }
939
Daniel Willmann65bfbaf2020-12-02 17:46:56 +0100940 /* TODO: Should we discard this message if the check fails */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200941 check_peer_nsei(peer, nsei);
942
943 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
944 if (!rc)
945 return 0;
946
947 switch (pdu_type) {
948 case BSSGP_PDUT_FLOW_CONTROL_BVC:
949 if (!cfg->route_to_sgsn2)
950 break;
951
952 /* Send a copy to the secondary SGSN */
953 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
954 break;
955 default:
956 break;
957 }
958
959
960 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
961}
962
963/* Receive an incoming PTP message from a SGSN-side NS-VC */
964static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
965 struct msgb *msg, uint16_t nsei,
Alexander Couzens951e1332020-09-22 13:21:46 +0200966 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200967{
968 struct gbproxy_peer *peer;
969 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
970 uint8_t pdu_type = bgph->pdu_type;
971
972 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
973
974 /* Send status messages before patching */
975
976 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +0100977 LOGP(DGPRS, LOGL_INFO, "BVC(%05u/??) Didn't find peer for "
978 "for message from NSE(%05u/SGSN)\n",
Alexander Couzens951e1332020-09-22 13:21:46 +0200979 ns_bvci, nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200980 rate_ctr_inc(&cfg->ctrg->
981 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
982 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
983 &ns_bvci, msg);
984 }
985
986 if (peer->blocked) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +0100987 LOGPBVC(peer, LOGL_NOTICE, "Dropping PDU for "
988 "blocked BVC via NSE(%05u/SGSN)\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200989 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
990 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
991 }
992
993 switch (pdu_type) {
994 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
995 case BSSGP_PDUT_BVC_BLOCK_ACK:
996 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
997 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
998 /* Hide ACKs from the secondary SGSN, the primary SGSN
999 * is responsible to send them. */
1000 return 0;
1001 break;
1002 default:
1003 break;
1004 }
1005
1006 /* Optionally patch the message */
1007 gbprox_process_bssgp_dl(cfg, msg, peer);
1008
1009 return gbprox_relay2peer(msg, peer, ns_bvci);
1010}
1011
Harald Welte7df1e5a2020-12-02 22:53:26 +01001012/* process a BVC-RESET message from the BSS side */
1013static int gbprox_rx_bvc_reset_from_bss(struct gbproxy_config *cfg, struct msgb *msg,
1014 uint16_t nsei, struct tlv_parsed *tp,
1015 int *copy_to_sgsn2)
1016{
1017 struct gbproxy_peer *from_peer = NULL;
Harald Welte314647b2020-12-02 23:03:22 +01001018 uint16_t bvci;
Harald Welte7df1e5a2020-12-02 22:53:26 +01001019
Harald Welte314647b2020-12-02 23:03:22 +01001020 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
1021 return 0;
Harald Welte7df1e5a2020-12-02 22:53:26 +01001022
Harald Welte314647b2020-12-02 23:03:22 +01001023 bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1024 LOGP(DGPRS, LOGL_INFO, "NSE(%05u) Rx BVC RESET (BVCI=%05u)\n", nsei, bvci);
1025 if (bvci == 0) {
1026 /* If we receive a BVC reset on the signalling endpoint, we
1027 * don't want the SGSN to reset, as the signalling endpoint
1028 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte324f0652020-12-02 23:06:37 +01001029
Harald Welte314647b2020-12-02 23:03:22 +01001030 /* Ensure the NSE peer is there and clear all PtP BVCs */
Harald Welte324f0652020-12-02 23:06:37 +01001031 struct gbproxy_nse *nse = gbproxy_nse_by_nsei_or_new(cfg, nsei);
Harald Welte314647b2020-12-02 23:03:22 +01001032 if (!nse) {
1033 LOGP(DGPRS, LOGL_ERROR, "Could not create NSE(%05u)\n", nsei);
1034 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
Harald Welte7df1e5a2020-12-02 22:53:26 +01001035 return 0;
1036 }
Harald Welte314647b2020-12-02 23:03:22 +01001037
1038 gbproxy_cleanup_peers(cfg, nsei, 0);
1039
1040 /* FIXME: only do this if SGSN is alive! */
Harald Welte324f0652020-12-02 23:06:37 +01001041 LOGPNSE(nse, LOGL_INFO, "Tx fake BVC RESET ACK of BVCI=0\n");
Harald Welte314647b2020-12-02 23:03:22 +01001042 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 * */
Harald Welte324f0652020-12-02 23:06:37 +01001083 memcpy(from_peer->ra, TLVP_VAL(tp, BSSGP_IE_CELL_ID), sizeof(from_peer->ra));
Harald Welte7df1e5a2020-12-02 22:53:26 +01001084 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte324f0652020-12-02 23:06:37 +01001085 LOGPBVC(from_peer, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welte7df1e5a2020-12-02 22:53:26 +01001086 }
1087 if (cfg->route_to_sgsn2)
1088 *copy_to_sgsn2 = 1;
1089 }
1090 /* continue processing / relaying to SGSN[s] */
1091 return 1;
1092}
1093
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001094/* Receive an incoming signalling message from a BSS-side NS-VC */
1095static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
1096 struct msgb *msg, uint16_t nsei,
1097 uint16_t ns_bvci)
1098{
1099 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1100 struct tlv_parsed tp;
1101 uint8_t pdu_type = bgph->pdu_type;
1102 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1103 struct gbproxy_peer *from_peer = NULL;
1104 struct gprs_ra_id raid;
1105 int copy_to_sgsn2 = 0;
1106 int rc;
1107
1108 if (ns_bvci != 0 && ns_bvci != 1) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001109 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) BVCI=%05u is not signalling\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001110 nsei, ns_bvci);
1111 return -EINVAL;
1112 }
1113
1114 /* we actually should never see those two for BVCI == 0, but double-check
1115 * just to make sure */
1116 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1117 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001118 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) UNITDATA not allowed in "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001119 "signalling\n", nsei);
1120 return -EINVAL;
1121 }
1122
1123 bssgp_tlv_parse(&tp, bgph->data, data_len);
1124
1125 switch (pdu_type) {
1126 case BSSGP_PDUT_SUSPEND:
1127 case BSSGP_PDUT_RESUME:
1128 /* We implement RAI snooping during SUSPEND/RESUME, since it
1129 * establishes a relationsip between BVCI/peer and the routeing
1130 * area identification. The snooped information is then used
1131 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
1132 * BSSGP */
1133 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1134 goto err_mand_ie;
1135 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
1136 if (!from_peer)
1137 goto err_no_peer;
1138 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
1139 sizeof(from_peer->ra));
1140 gsm48_parse_ra(&raid, from_peer->ra);
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001141 LOGPBVC(from_peer, LOGL_INFO, "BSSGP SUSPEND/RESUME "
1142 "RAI snooping: RAI %s\n",
1143 osmo_rai_name(&raid));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001144 /* FIXME: This only supports one BSS per RA */
1145 break;
1146 case BSSGP_PDUT_BVC_RESET:
Harald Welte7df1e5a2020-12-02 22:53:26 +01001147 rc = gbprox_rx_bvc_reset_from_bss(cfg, msg, nsei, &tp, &copy_to_sgsn2);
1148 /* if function retruns 0, we terminate processing here */
1149 if (rc == 0)
1150 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001151 break;
1152 }
1153
1154 /* Normally, we can simply pass on all signalling messages from BSS to
1155 * SGSN */
1156 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1157 if (!rc)
1158 return 0;
1159
1160 if (copy_to_sgsn2)
1161 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1162
1163 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
1164err_no_peer:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001165 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) cannot find peer based on NSEI\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001166 nsei);
1167 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
1168 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1169err_mand_ie:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001170 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) missing mandatory RA IE\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001171 nsei);
1172 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
1173 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1174}
1175
1176/* Receive paging request from SGSN, we need to relay to proper BSS */
1177static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
1178 uint32_t nsei, uint16_t ns_bvci)
1179{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001180 struct gbproxy_nse *nse;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001181 struct gbproxy_peer *peer;
Daniel Willmann76205712020-11-30 17:08:58 +01001182 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001183 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
1184
Daniel Willmanne50550e2020-11-26 18:19:21 +01001185 /* FIXME: Handle paging logic to only page each matching NSE */
1186
Daniel Willmann3696dce2020-12-02 16:08:02 +01001187 LOGP(DGPRS, LOGL_INFO, "NSE(%05u/SGSN) BSSGP PAGING\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001188 nsei);
1189 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1190 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001191 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001192 peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001193 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001194 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PAGING: "
1195 "unable to route: BVCI=%05u unknown\n", nsei, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001196 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1197 return -EINVAL;
1198 }
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001199 LOGPBVC(peer, LOGL_INFO, "routing by BVCI\n");
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001200 return gbprox_relay2peer(msg, peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001201 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001202 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001203 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001204 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1205 llist_for_each_entry(peer, &nse->bts_peers, list) {
1206 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
Daniel Willmann4becbcb2020-12-04 01:24:34 +01001207 LOGPNSE(nse, LOGL_INFO, "routing to NSE (RAI match)\n");
Daniel Willmann76205712020-11-30 17:08:58 +01001208 gbprox_relay2nse(msg, nse, ns_bvci);
1209 n_nses++;
1210 /* Only send it once to each NSE */
1211 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001212 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001213 }
1214 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001215 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001216 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001217 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001218 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1219 llist_for_each_entry(peer, &nse->bts_peers, list) {
1220 if (!memcmp(peer->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001221 LOGPNSE(nse, LOGL_INFO, "routing to NSE (LAI match)\n");
Daniel Willmann76205712020-11-30 17:08:58 +01001222 gbprox_relay2nse(msg, nse, ns_bvci);
1223 n_nses++;
1224 /* Only send it once to each NSE */
1225 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001226 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001227 }
1228 }
Harald Welte53ee2062020-11-24 11:31:13 +01001229 } else if (TLVP_PRESENT(tp, BSSGP_IE_BSS_AREA_ID)) {
1230 /* iterate over all peers and dispatch the paging to each matching one */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001231 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1232 llist_for_each_entry(peer, &nse->bts_peers, list) {
Daniel Willmann5e595ca2020-12-02 13:54:21 +01001233 LOGPNSE(nse, LOGL_INFO, "routing to NSE (broadcast)\n");
Daniel Willmann76205712020-11-30 17:08:58 +01001234 gbprox_relay2nse(msg, nse, ns_bvci);
1235 n_nses++;
1236 /* Only send it once to each NSE */
1237 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001238 }
Harald Welte53ee2062020-11-24 11:31:13 +01001239 }
1240 } else {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001241 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: "
Harald Welte53ee2062020-11-24 11:31:13 +01001242 "unable to route, missing IE\n", nsei);
1243 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1244 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001245
Daniel Willmann76205712020-11-30 17:08:58 +01001246 if (n_nses == 0) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001247 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: "
Harald Welte53ee2062020-11-24 11:31:13 +01001248 "unable to route, no destination found\n", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001249 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1250 return -EINVAL;
1251 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001252 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001253}
1254
1255/* Receive an incoming BVC-RESET message from the SGSN */
1256static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
1257 struct msgb *orig_msg,
1258 struct msgb *msg, struct tlv_parsed *tp,
1259 uint32_t nsei, uint16_t ns_bvci)
1260{
Daniel Willmanne50550e2020-11-26 18:19:21 +01001261 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001262 struct gbproxy_peer *peer;
1263 uint16_t ptp_bvci;
1264
1265 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1266 rate_ctr_inc(&cfg->ctrg->
1267 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1268 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
1269 NULL, orig_msg);
1270 }
1271 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1272
1273 if (ptp_bvci >= 2) {
1274 /* A reset for a PTP BVC was received, forward it to its
1275 * respective peer */
1276 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
1277 if (!peer) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001278 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BVCI=%05u: Cannot find BSS\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001279 nsei, ptp_bvci);
1280 rate_ctr_inc(&cfg->ctrg->
1281 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1282 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
1283 &ptp_bvci, orig_msg);
1284 }
1285 return gbprox_relay2peer(msg, peer, ns_bvci);
1286 }
1287
1288 /* A reset for the Signalling entity has been received
1289 * from the SGSN. As the signalling BVCI is shared
1290 * among all the BSS's that we multiplex, it needs to
1291 * be relayed */
Daniel Willmanne50550e2020-11-26 18:19:21 +01001292 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1293 llist_for_each_entry(peer, &nse->bts_peers, list)
1294 gbprox_relay2peer(msg, peer, ns_bvci);
1295 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001296
1297 return 0;
1298}
1299
1300/* Receive an incoming signalling message from the SGSN-side NS-VC */
1301static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
1302 struct msgb *orig_msg, uint32_t nsei,
1303 uint16_t ns_bvci)
1304{
1305 struct bssgp_normal_hdr *bgph =
1306 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
1307 struct tlv_parsed tp;
1308 uint8_t pdu_type = bgph->pdu_type;
1309 int data_len;
Harald Welte7479c4d2020-12-02 20:06:04 +01001310 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001311 struct gbproxy_peer *peer;
1312 uint16_t bvci;
1313 struct msgb *msg;
1314 int rc = 0;
1315 int cause;
1316
1317 if (ns_bvci != 0 && ns_bvci != 1) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001318 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BVCI=%05u is not "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001319 "signalling\n", nsei, ns_bvci);
1320 /* FIXME: Send proper error message */
1321 return -EINVAL;
1322 }
1323
1324 /* we actually should never see those two for BVCI == 0, but double-check
1325 * just to make sure */
1326 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1327 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001328 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) UNITDATA not allowed in "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001329 "signalling\n", nsei);
1330 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1331 }
1332
1333 msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1334 gbprox_process_bssgp_dl(cfg, msg, NULL);
1335 /* Update message info */
1336 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1337 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
1338 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1339
1340 switch (pdu_type) {
1341 case BSSGP_PDUT_BVC_RESET:
1342 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
1343 break;
1344 case BSSGP_PDUT_BVC_RESET_ACK:
1345 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1346 break;
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001347 /* simple case: BVCI IE is mandatory */
1348 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1349 goto err_mand_ie;
1350 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1351 if (bvci == BVCI_SIGNALLING) {
1352 /* TODO: Reset all PTP BVCIs */
1353 } else {
1354 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1355 }
1356 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001357 case BSSGP_PDUT_FLUSH_LL:
1358 /* simple case: BVCI IE is mandatory */
1359 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1360 goto err_mand_ie;
1361 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1362 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1363 break;
1364 case BSSGP_PDUT_PAGING_PS:
1365 case BSSGP_PDUT_PAGING_CS:
1366 /* process the paging request (LAI/RAI lookup) */
1367 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
1368 break;
1369 case BSSGP_PDUT_STATUS:
1370 /* Some exception has occurred */
1371 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann3696dce2020-12-02 16:08:02 +01001372 "NSE(%05u/SGSN) BSSGP STATUS ", nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001373 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1374 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1375 goto err_mand_ie;
1376 }
1377 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
1378 LOGPC(DGPRS, LOGL_NOTICE,
1379 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
1380 bssgp_cause_str(cause));
1381 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
1382 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001383 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001384
1385 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1386 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1387 } else
1388 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1389 break;
1390 /* those only exist in the SGSN -> BSS direction */
1391 case BSSGP_PDUT_SUSPEND_ACK:
1392 case BSSGP_PDUT_SUSPEND_NACK:
1393 case BSSGP_PDUT_RESUME_ACK:
1394 case BSSGP_PDUT_RESUME_NACK:
1395 /* RAI IE is mandatory */
1396 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1397 goto err_mand_ie;
1398 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
1399 if (!peer)
1400 goto err_no_peer;
1401 rc = gbprox_relay2peer(msg, peer, ns_bvci);
1402 break;
1403 case BSSGP_PDUT_BVC_BLOCK_ACK:
1404 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1405 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1406 goto err_mand_ie;
1407 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1408 if (bvci == 0) {
Daniel Willmann3696dce2020-12-02 16:08:02 +01001409 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP "
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001410 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
1411 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
Daniel Willmann65bfbaf2020-12-02 17:46:56 +01001412 /* TODO: should we send STATUS ? */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001413 rate_ctr_inc(&cfg->ctrg->
1414 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1415 } else {
1416 /* Mark BVC as (un)blocked */
1417 block_unblock_peer(cfg, bvci, pdu_type);
1418 }
1419 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
1420 break;
1421 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte7479c4d2020-12-02 20:06:04 +01001422 case BSSGP_PDUT_OVERLOAD:
1423 LOGP(DGPRS, LOGL_DEBUG,
1424 "NSE(%05u/SGSN) BSSGP %s: broadcasting\n", nsei, bssgp_pdu_str(pdu_type));
1425 /* broadcast to all BSS-side peers */
1426 llist_for_each_entry(nse, &cfg->nse_peers, list) {
1427 gbprox_relay2nse(msg, nse, 0);
1428 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001429 break;
1430 default:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001431 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PDU type %s not supported\n", nsei,
1432 bssgp_pdu_str(pdu_type));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001433 rate_ctr_inc(&cfg->ctrg->
1434 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1435 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
1436 break;
1437 }
1438
1439 msgb_free(msg);
1440
1441 return rc;
1442err_mand_ie:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001443 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) missing mandatory IE\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001444 nsei);
1445 rate_ctr_inc(&cfg->ctrg->
1446 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1447 msgb_free(msg);
1448 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
1449err_no_peer:
Daniel Willmann3696dce2020-12-02 16:08:02 +01001450 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) cannot find peer based on RAI\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001451 nsei);
1452 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
1453 msgb_free(msg);
1454 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
1455}
1456
1457static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1458{
1459 return nsei == cfg->nsip_sgsn_nsei ||
1460 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1461}
1462
Alexander Couzens951e1332020-09-22 13:21:46 +02001463int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001464{
1465 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001466 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1467 struct gprs_ns2_inst *nsi = cfg->nsi;
1468 struct osmo_gprs_ns2_prim nsp = {};
1469
1470 nsp.bvci = msgb_bvci(msg);
1471 nsp.nsei = msgb_nsei(msg);
1472
1473 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
1474 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1475
1476 return rc;
1477}
1478
1479/* Main input function for Gb proxy */
1480int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1481{
1482 int rc;
1483 uint16_t nsei = msgb_nsei(msg);
1484 uint16_t ns_bvci = msgb_bvci(msg);
1485 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1486
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001487 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
1488
1489 /* Only BVCI=0 messages need special treatment */
1490 if (ns_bvci == 0 || ns_bvci == 1) {
1491 if (remote_end_is_sgsn)
1492 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
1493 else
1494 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
1495 } else {
1496 /* All other BVCI are PTP */
1497 if (remote_end_is_sgsn)
Alexander Couzens951e1332020-09-22 13:21:46 +02001498 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001499 ns_bvci);
1500 else
Alexander Couzens951e1332020-09-22 13:21:46 +02001501 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001502 ns_bvci);
1503 }
1504
1505 return rc;
1506}
1507
Alexander Couzens951e1332020-09-22 13:21:46 +02001508/* TODO: What about handling:
1509 * NS_AFF_CAUSE_VC_FAILURE,
1510 NS_AFF_CAUSE_VC_RECOVERY,
1511 NS_AFF_CAUSE_FAILURE,
1512 NS_AFF_CAUSE_RECOVERY,
1513 osmocom own causes
1514 NS_AFF_CAUSE_SNS_CONFIGURED,
1515 NS_AFF_CAUSE_SNS_FAILURE,
1516 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001517
Alexander Couzens951e1332020-09-22 13:21:46 +02001518void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001519{
Alexander Couzens951e1332020-09-22 13:21:46 +02001520 /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, peer->bvci, 0);
1521 * TODO: sgsn nsei available/unavailable
1522 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001523 struct gbproxy_peer *peer;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001524
Alexander Couzens951e1332020-09-22 13:21:46 +02001525 switch (nsp->u.status.cause) {
1526 case NS_AFF_CAUSE_SNS_FAILURE:
1527 case NS_AFF_CAUSE_SNS_CONFIGURED:
1528 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001529
Alexander Couzens951e1332020-09-22 13:21:46 +02001530 case NS_AFF_CAUSE_RECOVERY:
1531 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
1532 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1533 /* look-up or create the BTS context for this BVC */
1534 struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei);
1535 if (!bctx)
1536 bctx = btsctx_alloc(nsp->bvci, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001537
Alexander Couzens951e1332020-09-22 13:21:46 +02001538 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 +02001539 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001540 break;
1541 case NS_AFF_CAUSE_FAILURE:
1542 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
1543 /* sgsn */
1544 /* TODO: BSVC: block all PtP towards bss */
1545 rate_ctr_inc(&cfg->ctrg->
1546 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1547 } else {
Daniel Willmanne50550e2020-11-26 18:19:21 +01001548 /* bss became unavailable
1549 * TODO: Block all BVC belonging to that NSE */
Alexander Couzens951e1332020-09-22 13:21:46 +02001550 peer = gbproxy_peer_by_nsei(cfg, nsp->nsei);
1551 if (!peer) {
1552 /* TODO: use primitive name + status cause name */
1553 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown peer NSEI=%u\n",
1554 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001555 break;
1556 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001557
1558 if (!peer->blocked)
1559 break;
1560 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei,
1561 peer->bvci, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001562 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001563 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
1564 break;
1565 default:
Harald Welte95cf9fb2020-11-30 10:55:22 +01001566 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
1567 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001568 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001569 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001570}
1571
Alexander Couzens951e1332020-09-22 13:21:46 +02001572/* called by the ns layer */
1573int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1574{
1575 struct osmo_gprs_ns2_prim *nsp;
1576 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001577 uintptr_t bvci;
Alexander Couzens951e1332020-09-22 13:21:46 +02001578 int rc = 0;
1579
1580 if (oph->sap != SAP_NS)
1581 return 0;
1582
1583 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1584
1585 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte95cf9fb2020-11-30 10:55:22 +01001586 LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
1587 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001588 return 0;
1589 }
1590
1591 switch (oph->primitive) {
1592 case PRIM_NS_UNIT_DATA:
Daniel Willmann8f407b12020-12-02 19:33:50 +01001593
Alexander Couzens951e1332020-09-22 13:21:46 +02001594 /* hand the message into the BSSGP implementation */
1595 msgb_bssgph(oph->msg) = oph->msg->l3h;
1596 msgb_bvci(oph->msg) = nsp->bvci;
1597 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001598 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens951e1332020-09-22 13:21:46 +02001599
Daniel Willmann8f407b12020-12-02 19:33:50 +01001600 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens951e1332020-09-22 13:21:46 +02001601 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}