blob: fd3f0db961103dcb85fc3adf1dcbe659afa92ca6 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* gprs_bssgp_pcu.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
Holger Hans Peter Freyther416ce692013-08-22 08:44:04 +02004 * Copyright (C) 2013 by Holger Hans Peter Freyther
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <gprs_rlcmac.h>
22#include <gprs_bssgp_pcu.h>
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010023#include <gprs_bssgp_rim.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040024#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010025#include <gprs_debug.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020026#include <bts.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020027#include <tbf.h>
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +020028#include <coding_scheme.h>
Max6dc90b82018-02-19 17:17:28 +010029#include <pdch.h>
Pau Espin Pedrolde7bb752020-01-23 12:37:15 +010030#include <decoding.h>
Max136ebcc2019-03-05 14:59:03 +010031
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010032#include <osmocom/gprs/gprs_ns2.h>
33#include <osmocom/gsm/protocol/gsm_23_003.h>
34#include <osmocom/gprs/protocol/gsm_08_16.h>
35#include <osmocom/core/utils.h>
36#include <osmocom/gsm/gsm48.h>
37#include "coding_scheme.h"
38#include "tbf_dl.h"
39#include "llc.h"
40#include "gprs_rlcmac.h"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040041
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020042/* Tuning parameters for BSSGP flow control */
43#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
44#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
45#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
46#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
47
48/* Constants for BSSGP flow control */
49#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
50#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
51
Andreas Eversberg71cce912013-01-16 13:49:00 +010052extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020053extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010054extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040055
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020056static void bvc_timeout(void *_priv);
57
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010058static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020059{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010060 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020061 uint8_t cap_len;
62 uint8_t *cap;
63
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010064 memset(rac, 0, sizeof(*rac));
65
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020066 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010067 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020068
69 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
70 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
71
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010072 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
73
Alexander Couzensccde5c92017-02-04 03:10:08 +010074 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020075 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010076
77 /* TS 24.008, 10.5.5.12a */
78 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020079
80 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010081 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020082}
83
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020084static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040085{
86 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040087
Andreas Eversberge6228b32012-07-03 13:36:03 +020088 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +070089 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +020090 uint8_t *data;
91 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010092 uint8_t ms_class = 0;
93 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +070094 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010095 MS_Radio_Access_capability_t rac;
Neels Hofmeyr00520512020-08-21 15:44:58 +020096 /* TODO: is it really necessary to initialize this as a "000" IMSI? It seems, the function should just return an
97 * error if no IMSI IE was found. */
98 struct osmo_mobile_identity mi_imsi = {
99 .type = GSM_MI_TYPE_TMSI,
100 };
101 OSMO_STRLCPY_ARRAY(mi_imsi.imsi, "000");
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200102
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400103 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200104 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400105
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400106 /* LLC_PDU is mandatory IE */
107 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400108 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200109 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400110 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
111 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400112
Andreas Eversberge6228b32012-07-03 13:36:03 +0200113 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
114 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700115 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400116 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200117 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD IE_LLC_PDU too large\n", tlli);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200118 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400119 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400120
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200121 /* read IMSI. if no IMSI exists, use first paging block (any paging),
122 * because during attachment the IMSI might not be known, so the MS
123 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700124 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
125 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200126 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
127 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200128 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700129 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
130 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
131 }
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700132 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200133
134 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100135 if (parse_ra_cap(tp, &rac) >= 0) {
136 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100137 ms_class = get_ms_class_by_capability(&rac);
138 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100139 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
140 ms_class, egprs_ms_class);
141 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200142
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200143 /* get lifetime */
144 uint16_t delay_csec = 0xffff;
145 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
146 {
147 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200148 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200149 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200150 else
151 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
152 "PDU_LIFETIME IE\n");
153 } else
154 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
155 "PDU_LIFETIME IE\n");
156
Jacob Erlbeck93990462015-05-15 15:50:43 +0200157 /* get optional TLLI old */
158 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
159 {
160 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200161 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200162 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200163 else
164 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
165 "TLLI (old) IE\n");
166 }
167
Neels Hofmeyr00520512020-08-21 15:44:58 +0200168 LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n", tlli, mi_imsi.imsi, len);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400169
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100170 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, mi_imsi.imsi,
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100171 ms_class, egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400172}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400173
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700174/* Returns 0 on success, suggested BSSGP cause otherwise */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200175static unsigned int get_paging_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700176{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700177 /* Use TMSI (if present) or IMSI */
178 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
179 /* Be safe against an evil SGSN - check the length */
180 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
181 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
182 return BSSGP_CAUSE_COND_IE_ERR;
183 }
184
185 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200186 *mi = (struct osmo_mobile_identity){
187 .type = GSM_MI_TYPE_TMSI,
188 };
189 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700190 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200191 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
192 true);
193 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
194 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
195 return BSSGP_CAUSE_COND_IE_ERR;
196 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700197 } else {
198 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
199 return BSSGP_CAUSE_MISSING_COND_IE;
200 }
201
202 return 0;
203}
204
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700205static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100206{
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200207 struct osmo_mobile_identity mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100208 struct gprs_rlcmac_bts *bts;
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700209 int rc;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100210
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200211 if ((rc = get_paging_mi(&mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700212 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100213
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100214 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
215 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
216 bts_add_paging(bts, tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0), &mi);
217 }
218 return 0;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100219}
220
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700221static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200222{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200223 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200224 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100225 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100226 uint16_t pgroup;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700227 int rc;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200228
Maxf3038e72019-03-19 15:39:05 +0100229 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200230 LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n");
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100231 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200232 }
233
Neels Hofmeyr00520512020-08-21 15:44:58 +0200234 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI), true);
235 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700236 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100237 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
238 }
Neels Hofmeyr00520512020-08-21 15:44:58 +0200239 pgroup = imsi2paging_group(mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100240 if (pgroup > 999) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200241 LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100242 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700243 }
Maxf3038e72019-03-19 15:39:05 +0100244
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200245 if ((rc = get_paging_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700246 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100247
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100248 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
249 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
250 gprs_rlcmac_paging_request(bts, &paging_mi, pgroup);
251 }
252 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200253}
254
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400255/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200256static int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400257{
258 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200259 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200260 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400261 unsigned rc = 0;
262
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200263 if (!bctx)
264 return -EINVAL;
265
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400266 /* If traffic is received on a BVC that is marked as blocked, the
267 * received PDU shall not be accepted and a STATUS PDU (Cause value:
268 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
269 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
270 {
271 uint16_t bvci = msgb_bvci(msg);
272 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
273 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
274 }
275
276 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200277 case BSSGP_PDUT_STATUS:
278 /* already handled in libosmogb */
279 OSMO_ASSERT(0);
280 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400281 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200282 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100283 if (the_pcu->bssgp.on_dl_unit_data)
284 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400285 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400286 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400287 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400288 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200289 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
290 bvci, bssgp_pdu_str(pdu_type));
291 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200292 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100293 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
294 break;
295 case BSSGP_PDUT_PAGING_PS:
296 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
297 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200298 case BSSGP_PDUT_RA_CAPABILITY:
299 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
300 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
301 bvci, bssgp_pdu_str(pdu_type));
302 break;
303 /* See TS 08.18 5.4.1 */
304 case BSSGP_PDUT_SUSPEND:
305 case BSSGP_PDUT_SUSPEND_ACK:
306 case BSSGP_PDUT_SUSPEND_NACK:
307 case BSSGP_PDUT_RESUME:
308 case BSSGP_PDUT_RESUME_ACK:
309 case BSSGP_PDUT_RESUME_NACK:
310 case BSSGP_PDUT_FLUSH_LL:
311 case BSSGP_PDUT_FLUSH_LL_ACK:
312 case BSSGP_PDUT_LLC_DISCARD:
313 case BSSGP_PDUT_BVC_BLOCK:
314 case BSSGP_PDUT_BVC_BLOCK_ACK:
315 case BSSGP_PDUT_BVC_UNBLOCK:
316 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
317 case BSSGP_PDUT_BVC_RESET:
318 case BSSGP_PDUT_BVC_RESET_ACK:
319 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
320 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
321 bctx->bvci, bssgp_pdu_str(pdu_type));
322 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400323 break;
324 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200325 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200326 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400327 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
328 break;
329 }
330 return rc;
331}
332
333/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200334static int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400335{
336 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200337 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400338 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700339 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200340 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400341 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200342 /* already handled in libosmogb */
343 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400344 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200345 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200346 case BSSGP_PDUT_RESUME_ACK:
347 case BSSGP_PDUT_BVC_BLOCK_ACK:
348 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
349 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200350 break;
351 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200352 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100353 if (!the_pcu->bssgp.bvc_sig_reset)
354 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200355 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100356 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200357 bvc_timeout(NULL);
358 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100359 case BSSGP_PDUT_PAGING_CS:
360 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
361 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200362 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200363 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
364 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200365 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200366 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100367 the_pcu->bssgp.bvc_unblocked = 1;
368 if (the_pcu->bssgp.on_unblock_ack)
369 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200370 bvc_timeout(NULL);
371 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200372 case BSSGP_PDUT_SUSPEND_NACK:
373 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200374 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200375 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200376 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
377 bvci, bssgp_pdu_str(pdu_type));
378 break;
379 /* See TS 08.18 5.4.1 */
380 case BSSGP_PDUT_UL_UNITDATA:
381 case BSSGP_PDUT_DL_UNITDATA:
382 case BSSGP_PDUT_RA_CAPABILITY:
383 case BSSGP_PDUT_PTM_UNITDATA:
384 case BSSGP_PDUT_RA_CAPA_UDPATE:
385 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
386 case BSSGP_PDUT_RADIO_STATUS:
387 case BSSGP_PDUT_FLOW_CONTROL_BVC:
388 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
389 case BSSGP_PDUT_FLOW_CONTROL_MS:
390 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
391 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
392 case BSSGP_PDUT_CREATE_BSS_PFC:
393 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
394 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
395 case BSSGP_PDUT_MODIFY_BSS_PFC:
396 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
397 case BSSGP_PDUT_DELETE_BSS_PFC:
398 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
399 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
400 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200401 break;
402 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200403 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200404 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200405 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
406 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400407 }
408 return rc;
409}
410
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200411static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400412{
413 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
414 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
415 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200416 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200417 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400418 int data_len;
419 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200420 struct bssgp_bvc_ctx *bctx;
421
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100422 switch (pdu_type) {
423 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100424 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200425 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100426 case BSSGP_PDUT_RAN_INFO:
427 case BSSGP_PDUT_RAN_INFO_REQ:
428 case BSSGP_PDUT_RAN_INFO_ACK:
429 case BSSGP_PDUT_RAN_INFO_ERROR:
430 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
431 /* Also pass all RIM related messages to the generic BSSGP
432 * parser so that it can deliver primitive to the RIM SAP
433 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100434 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100435 default:
436 break;
437 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400438
439 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
440
441 /* UNITDATA BSSGP headers have TLLI in front */
442 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
443 {
444 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
445 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
446 }
447 else
448 {
449 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
450 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
451 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200452 if (rc < 0) {
453 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
454 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200455 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200456 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400457
Max2813f932017-06-27 17:18:40 +0200458 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100459 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200460 }
461
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400462 /* look-up or create the BTS context for this BVC */
463 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
464
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700465 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400466 {
Max2813f932017-06-27 17:18:40 +0200467 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
468 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400469 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
470 }
471
472 if (bctx)
473 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100474 log_set_context(LOG_CTX_GB_BVC, bctx);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400475 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
476 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg));
477 }
478
479 if (ns_bvci == BVCI_SIGNALLING)
480 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200481 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400482 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
483 }
484 else if (ns_bvci == BVCI_PTM)
485 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200486 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400487 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
488 }
489 else
490 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100491 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400492 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
493 }
494 return rc;
495}
Harald Welte477e79e2012-06-18 12:21:03 +0800496
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100497static void handle_nm_status(struct osmo_bssgp_prim *bp)
498{
499 enum gprs_bssgp_cause cause;
500
501 LOGP(DPCU, LOGL_DEBUG,
502 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
503 bp->bvci, bp->nsei);
504
505 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
506 return;
507
508 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
509
510 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
511 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
512 return;
513
514 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
515 return;
516
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100517 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100518 LOGP(DPCU, LOGL_NOTICE,
519 "Received BSSGP STATUS message for an unknown BVCI (%d), "
520 "ignored\n",
521 bp->bvci);
522 return;
523 }
524
525 switch (cause) {
526 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100527 if (the_pcu->bssgp.bvc_unblocked) {
528 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100529 bvc_timeout(NULL);
530 }
531 break;
532
533 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100534 if (the_pcu->bssgp.bvc_reset) {
535 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100536 bvc_timeout(NULL);
537 }
538 break;
539 default:
540 break;
541 }
542}
543
Harald Welte477e79e2012-06-18 12:21:03 +0800544int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
545{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100546 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100547 int rc;
548 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100549 bp = container_of(oph, struct osmo_bssgp_prim, oph);
550
551 switch (oph->sap) {
552 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100553 switch (oph->primitive) {
554 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100555 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100556 break;
557 case PRIM_NM_BVC_RESET:
558 /* received a BVC PTP reset */
559 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
560 /* Rx Reset from SGSN */
561 if (bp->bvci == BVCI_SIGNALLING) {
562 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
563 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
564 else {
565 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
566 break;
567 }
568
569 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
570 if (rc < 0) {
571 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
572 break;
573 }
574 the_pcu->bssgp.bvc_sig_reset = 1;
575 the_pcu->bssgp.bvc_reset = 0;
576 the_pcu->bssgp.bvc_unblocked = 0;
577 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
578 the_pcu->bssgp.bvc_reset = 1;
579 the_pcu->bssgp.bvc_unblocked = 0;
580 bvc_timeout(NULL);
581 }
582 break;
583 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100584 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100585 case SAP_BSSGP_RIM:
586 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100587 default:
588 break;
589 }
Harald Welte477e79e2012-06-18 12:21:03 +0800590 return 0;
591}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200592
Alexander Couzens290d9032020-09-16 21:52:02 +0200593void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200594{
Alexander Couzens290d9032020-09-16 21:52:02 +0200595 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100596 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200597 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200598 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100599 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200600 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100601 if (!the_pcu->bssgp.nsvc_unblocked) {
602 the_pcu->bssgp.bvc_sig_reset = 0;
603 the_pcu->bssgp.bvc_reset = 0;
604 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200605 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200606 }
607 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100608 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200609 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100610 if (the_pcu->bssgp.nsvc_unblocked) {
611 the_pcu->bssgp.nsvc_unblocked = 0;
612 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
613 the_pcu->bssgp.bvc_sig_reset = 0;
614 the_pcu->bssgp.bvc_reset = 0;
615 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200616 }
617 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100618 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100619 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200620 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100621 LOGP(DPCU, LOGL_DEBUG,
622 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200623 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100624 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200625 break;
626 }
627}
628
629/* called by the ns layer */
630int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
631{
632 struct osmo_gprs_ns2_prim *nsp;
633 int rc = 0;
634
635 if (oph->sap != SAP_NS)
636 return 0;
637
638 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
639
640 if (oph->operation != PRIM_OP_INDICATION) {
641 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
642 get_value_string(osmo_prim_op_names, oph->operation),
643 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100644 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200645 }
646
647 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100648 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200649 /* hand the message into the BSSGP implementation */
650 /* add required msg fields for Gb layer */
651 msgb_bssgph(oph->msg) = oph->msg->l3h;
652 msgb_bvci(oph->msg) = nsp->bvci;
653 msgb_nsei(oph->msg) = nsp->nsei;
654 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
655 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100656 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200657 gprs_ns_prim_status_cb(nsp);
658 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100659 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100660 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200661 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100662 LOGP(DPCU, LOGL_DEBUG,
663 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200664 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100665 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200666 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200667 }
668
Alexander Couzensa2848542020-10-26 00:33:31 +0100669out:
670 if (oph->msg)
671 msgb_free(oph->msg);
672
Alexander Couzens290d9032020-09-16 21:52:02 +0200673 return rc;
674}
675
676/* called by the bssgp layer to send NS PDUs */
677int gprs_gp_send_cb(void *ctx, struct msgb *msg)
678{
679 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
680 struct osmo_gprs_ns2_prim nsp = {};
681 nsp.nsei = msgb_nsei(msg);
682 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100683 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200684 PRIM_OP_REQUEST, msg);
685 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200686}
687
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200688static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
689{
690 size_t trx_no, ts_no;
691 unsigned num_pdch = 0;
692
693 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
694 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
695
696 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
697 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
698
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100699 if (pdch->m_is_enabled)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200700 num_pdch += 1;
701 }
702 }
703
704 return num_pdch;
705}
706
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200707static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200708{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200709 int bytes_per_rlc_block = mcs_max_data_block_bytes(cs) * num_data_blocks(mcs_header_type(cs));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200710
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100711 /* n byte payload per 20ms */
712 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200713}
714
715static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
716 uint32_t leak_rate, uint32_t fallback)
717{
718 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100719 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200720
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200721 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100722 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200723
724 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200725 bucket_size = FC_MAX_BUCKET_SIZE;
726
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200727 if (bucket_size == 0 && bucket_time && leak_rate)
728 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200729
730 if (bucket_size == 0 && leak_rate)
731 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
732
733 if (bucket_size == 0)
734 bucket_size = fallback;
735
736 if (bucket_size > FC_MAX_BUCKET_SIZE)
737 bucket_size = FC_MAX_BUCKET_SIZE;
738
739 return bucket_size;
740}
741
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200742static uint32_t get_and_reset_avg_queue_delay(void)
743{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100744 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200745 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100746 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200747 uint32_t avg_delay_ms = 0;
748
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100749 if (the_pcu->bssgp.queue_delay_count > 0)
750 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200751
752 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100753 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100754 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200755
756 return avg_delay_ms;
757}
758
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200759static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
760{
761 int rate; /* byte per second */
762
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100763 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200764 return -1;
765
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100766 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200767 return -1;
768
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100769 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
770 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200771
772 /* 20ms/num_pdch is the average RLC block duration, so the rate is
773 * calculated as:
774 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100775 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
776 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200777
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100778 the_pcu->bssgp.queue_frames_sent = 0;
779 the_pcu->bssgp.queue_bytes_recv = 0;
780 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200781
782 return rate;
783}
784
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200785static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100786{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100787 int num = 0;
788 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100789 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100790
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100791 /* First check if we support any MCS: */
792 for (i = 8; i >= 0; i--) {
793 if (bts->mcs_mask & (1 << i)) {
794 num = i + 1;
795 mcs_any = true;
796 break;
797 }
798 }
799
800 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100801 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100802 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100803 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100804 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100805 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100806 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100807 } else if (bts_max_mcs_dl(bts)) {
808 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100809 } else {
810 num = 9;
811 }
812
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100813 if (num)
814 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100815 }
816
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100817 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100818 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100819 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100820 } else {
821 for (i = 3; i >= 0; i--) {
822 if (bts->cs_mask & (1 << i)) {
823 num = i + 1;
824 break;
825 }
826 }
827 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100828 } else if (bts_max_cs_dl(bts)) {
829 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100830 }
831
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100832 if (!num)
833 num = 4;
834
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200835 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100836}
837
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100838static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200839{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200840 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200841 uint32_t bucket_size; /* oct */
842 uint32_t ms_bucket_size; /* oct */
843 uint32_t leak_rate; /* oct/s */
844 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200845 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200846 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200847 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200848
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100849 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200850 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
851 return -EIO;
852 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100853
854 /* FIXME: This calculation needs to be redone to support multiple BTS */
855 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
856 if (!bts) {
857 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
858 return -EIO;
859 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200860
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100861 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200862
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100863 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
864 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
865 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
866 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200867
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100868 /* FIXME: This calculation is mostly wrong. It should be done based on
869 currently established TBF (and whether the related (egprs)_ms_class
870 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200871 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200872 int meas_rate;
873 int usage; /* in 0..1000 */
874
875 if (num_pdch < 0)
876 num_pdch = count_pdch(bts);
877
878 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
879 if (meas_rate > 0) {
880 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
881 leak_rate =
882 (meas_rate * usage + leak_rate * (1000 - usage)) /
883 1000;
884 LOGP(DBSSGP, LOGL_DEBUG,
885 "Estimated BVC leak rate = %d "
886 "(measured %d, usage %d%%)\n",
887 leak_rate, meas_rate, usage/10);
888 }
889 }
890
891 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200892 if (num_pdch < 0)
893 num_pdch = count_pdch(bts);
894
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200895 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200896
897 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100898 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100899 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200900 };
901
902 if (ms_leak_rate == 0) {
903 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100904 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200905
906 if (num_pdch < 0)
907 num_pdch = count_pdch(bts);
908
909 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200910 if (max_pdch > FC_MS_MAX_RX_SLOTS)
911 max_pdch = FC_MS_MAX_RX_SLOTS;
912 if (ms_num_pdch > max_pdch)
913 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200914
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200915 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200916
917 /* TODO: To properly support multiple TRX, the per MS leak rate
918 * should be derived from the max number of PDCH TS per TRX.
919 */
920 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100921 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
922 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100923 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200924 };
925
926 /* TODO: Force leak_rate to 0 on buffer bloat */
927
928 if (bucket_size == 0)
929 bucket_size = compute_bucket_size(bts, leak_rate,
930 FC_FALLBACK_BVC_BUCKET_SIZE);
931
932 if (ms_bucket_size == 0)
933 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
934 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
935
936 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
937 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
938
939 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
940 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
941
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200942 /* Avg queue delay monitoring */
943 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200944
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +0200945 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100946 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +0200947
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200948 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200949 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
950 "R_MS = %d, avg_dly = %d\n",
951 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
952 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200953
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100954 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200955 bucket_size, leak_rate,
956 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200957 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200958}
959
960static void bvc_timeout(void *_priv)
961{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +0200962 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100963 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200964 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100965 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol924aaad2021-01-14 12:01:42 +0100966 secs = osmo_tdef_get(the_pcu->T_defs, 2, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100967 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200968 return;
969 }
970
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100971 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200972 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100973 the_pcu->bssgp.bctx->bvci);
974 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, the_pcu->bssgp.bctx->bvci, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol924aaad2021-01-14 12:01:42 +0100975 secs = osmo_tdef_get(the_pcu->T_defs, 2, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100976 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200977 return;
978 }
979
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100980 if (!the_pcu->bssgp.bvc_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200981 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100982 the_pcu->bssgp.bctx->bvci);
983 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol924aaad2021-01-14 12:01:42 +0100984 secs = osmo_tdef_get(the_pcu->T_defs, 1, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100985 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200986 return;
987 }
988
989 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100990 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200991 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100992 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200993}
994
Alexander Couzens13a12e22021-02-15 05:50:10 +0100995/*! configure NS layer
996 *
997 * \param bts pointer to the bts object
998 * \param nsei the NSEI of the BSS
999 * \param local pointer to an array of local address to bind on.
1000 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1001 * \param nsvci pointer to an array of nsvcis
1002 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1003 * \returns 0 if the configuration has succeeded. on error != 0
1004 */
1005static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1006 uint16_t nsei,
1007 const struct osmo_sockaddr *local,
1008 const struct osmo_sockaddr *remote,
1009 const uint16_t *nsvci,
1010 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001011{
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001012 int i, rc;
1013 uint16_t binds = 0;
1014 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001015 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001016 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Alexander Couzensb644fd12020-12-06 01:24:02 +01001017 char name[5];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001018 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001019
1020 if (!valid)
1021 return -1;
1022
Alexander Couzens13a12e22021-02-15 05:50:10 +01001023 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1024 if (!bts->nse)
1025 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1026 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1027 if (!bts->nse) {
1028 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1029 return -1;
1030 }
1031
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001032 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1033 if (!(valid & (1 << i)))
1034 continue;
1035
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001036 if (!gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i])) {
Alexander Couzensb644fd12020-12-06 01:24:02 +01001037 snprintf(name, sizeof(name), "pcu%d", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001038 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001039 if (rc < 0) {
1040 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1041 continue;
1042 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001043
1044 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1045 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1046 if (rc < 0) {
1047 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1048 continue;
1049 }
1050 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001051 }
1052
1053 binds |= 1 << i;
1054 }
1055
1056 if (!binds) {
1057 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001058 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001059 return -1;
1060 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001061
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001062 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1063 if (!(binds & (1 << i)))
1064 continue;
1065
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001066 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001067 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001068 if (rc && rc != -EALREADY) {
1069 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001070 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001071 } else {
1072 sns_configured = true;
1073 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001074 } else {
1075 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1076 if (nsvc)
1077 nsvcs = true;
1078 else
1079 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1080 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001081 }
1082
Alexander Couzens13a12e22021-02-15 05:50:10 +01001083 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1084 return sns_configured ? 0 : -1;
1085 else
1086 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001087}
1088
1089struct nsvc_cb {
1090 const struct osmo_sockaddr *local;
1091 const struct osmo_sockaddr *remote;
1092 const uint16_t *nsvci;
1093 /* [in] bitmask of valid nsvc in local/remote */
1094 uint16_t valid;
1095 /* [out] bitmask of found nsvcs */
1096 uint16_t found;
1097};
1098
1099static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1100{
1101 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001102 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001103
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001104 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001105 if (!(data->valid & (1 << i)))
1106 continue;
1107 if (data->found & (1 << i))
1108 continue;
1109
1110 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1111 &data->remote[i],
1112 data->nsvci[i])) {
1113 data->found |= 1 << i;
1114 return 0;
1115 }
1116 }
1117
1118 /* Found an extra nsvc */
1119 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1120 gprs_ns2_free_nsvc(nsvc);
1121
1122 return 0;
1123}
1124
Alexander Couzens94a367f2021-02-16 16:57:15 +01001125/* update the ns configuration if needed */
1126int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1127 const struct osmo_sockaddr *local,
1128 const struct osmo_sockaddr *remote,
1129 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001130{
1131 int rc = 0;
1132 if (!bts->nse) {
1133 /* there shouldn't any previous state. */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001134 gprs_ns2_free_nses(the_pcu->nsi);
1135 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001136 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001137 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1138 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001139 gprs_ns2_free_nses(the_pcu->nsi);
1140 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001141 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001142 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001143 /* SNS: check if the initial nsvc is the same, if not recreate it */
1144 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001145 unsigned int i;
1146 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001147 if (!(valid & (1 << i)))
1148 continue;
1149
1150 /* found the initial - everything should be fine */
1151 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1152 return 0;
1153 }
1154
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001155 gprs_ns2_free_nses(the_pcu->nsi);
1156 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001157 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001158 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001159 /* check if all NSVC are still the same. */
1160 struct nsvc_cb data = {
1161 .local = &local[0],
1162 .remote = &remote[0],
1163 .nsvci = &nsvci[0],
1164 .valid = valid,
1165 .found = 0,
1166 };
1167
1168 /* search the current active nsvcs */
1169 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1170
1171 /* we found all our valid nsvcs and might have removed all other nsvcs */
1172 if (valid == data.found)
1173 return 0;
1174
1175 /* remove all found nsvcs from the valid field */
1176 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001177 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001178 }
1179
1180 if (rc)
1181 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1182
1183 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001184}
1185
Alexander Couzens290d9032020-09-16 21:52:02 +02001186struct gprs_bssgp_pcu *gprs_bssgp_init(
1187 struct gprs_rlcmac_bts *bts,
1188 uint16_t nsei, uint16_t bvci,
1189 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1190 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001191{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001192
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001193 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001194 if (the_pcu->bssgp.bctx)
1195 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001196
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001197 the_pcu->bssgp.bts = bts;
1198 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1199 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001200 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001201 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001202 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001203 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001204 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001205 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001206 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001207 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1208 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001209 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001210 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1211 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001212 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001213 the_pcu->bssgp.bctx->ra_id.lac = lac;
1214 the_pcu->bssgp.bctx->ra_id.rac = rac;
1215 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001216
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001217 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001218
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001219 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001220}
1221
Alexander Couzens290d9032020-09-16 21:52:02 +02001222void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001223{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001224 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001225
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001226 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001227 the_pcu->bssgp.nsvc_unblocked = 0;
1228 the_pcu->bssgp.bvc_sig_reset = 0;
1229 the_pcu->bssgp.bvc_reset = 0;
1230 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001231
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001232 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1233 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001234
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001235 gprs_ns2_free(the_pcu->nsi);
1236 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001237 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001238}
1239
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001240void gprs_bssgp_update_frames_sent()
1241{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001242 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001243}
1244
1245void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1246{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001247 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1248 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001249}
1250
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001251void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1252 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001253{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001254 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001255 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001256
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001257 timespecsub(tv_now, tv_recv, &tv_delay);
1258 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001259
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001260 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001261}