blob: 02f56ac01d716527cb1867f41cc579fd74d8668f [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_l1_if.cpp
2 *
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04003 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040014 */
15
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040016#include <stdio.h>
17#include <unistd.h>
Pau Espin Pedrol32499b62019-12-09 13:55:12 +010018#include <inttypes.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040019#include <stdlib.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020020#include <string.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040021#include <errno.h>
22#include <assert.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020023#include <sys/socket.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040024#include <sys/un.h>
Andreas Eversberg81a12be2013-03-16 16:16:41 +010025#include <arpa/inet.h>
Pau Espin Pedrol754b0932020-03-26 14:47:13 +010026#include <ctype.h>
Max1187a772018-01-26 13:31:42 +010027
Andreas Eversberg0aed6542012-06-23 10:33:16 +020028extern "C" {
29#include <osmocom/core/talloc.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040030#include <osmocom/core/select.h>
31#include <osmocom/core/msgb.h>
Max528ff392016-02-22 11:42:33 +010032#include <osmocom/core/gsmtap_util.h>
33#include <osmocom/core/gsmtap.h>
Max1187a772018-01-26 13:31:42 +010034#include <osmocom/core/bitvec.h>
Alexander Couzens290d9032020-09-16 21:52:02 +020035#include <osmocom/core/sockaddr_str.h>
Max1187a772018-01-26 13:31:42 +010036#include <osmocom/core/logging.h>
37#include <osmocom/core/utils.h>
Alexander Couzens5bece2a2020-10-12 02:27:22 +020038#include <osmocom/gprs/gprs_ns2.h>
Max1187a772018-01-26 13:31:42 +010039#include <osmocom/gsm/l1sap.h>
40#include <osmocom/gsm/protocol/gsm_04_08.h>
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +010041#include <osmocom/gsm/gsm48_rest_octets.h>
Philipp Maiera58ec612021-01-25 23:43:52 +010042#include <osmocom/gsm/sysinfo.h>
Philipp Maier40db4c32023-01-10 12:49:28 +010043#include <osmocom/gsm/gsm0502.h>
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +020044
45#include <nacc_fsm.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020046}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040047
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040048#include <gprs_rlcmac.h>
49#include <pcu_l1_if.h>
50#include <gprs_debug.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040051#include <gprs_bssgp_pcu.h>
Harald Welte68fc1272016-11-16 22:48:33 +010052#include <osmocom/pcu/pcuif_proto.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020053#include <bts.h>
Max6dc90b82018-02-19 17:17:28 +010054#include <pdch.h>
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +020055#include <tbf_ul.h>
56#include <tbf_dl.h>
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010057#include <gprs_ms_storage.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020058
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010059// FIXME: move this, when changed from c++ to c.
60extern "C" {
Max58b66462016-05-13 10:34:15 +020061void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1,
62 struct gsmtap_inst *gsmtap);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010063int l1if_connect_pdch(void *obj, uint8_t ts);
64int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
65 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len);
66}
67
Andreas Eversberge266bd42012-07-13 14:00:21 +020068extern void *tall_pcu_ctx;
69
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010070#define PAGING_GROUP_LEN 3
71
72/* returns [0,999] on success, > 999 on error */
73uint16_t imsi2paging_group(const char* imsi)
74{
75 uint16_t pgroup = 0;
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070076 size_t len;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010077
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070078 len = (imsi != NULL) ? strlen(imsi) : 0;
79 if (len < PAGING_GROUP_LEN)
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010080 return 0xFFFF;
81 imsi += len - PAGING_GROUP_LEN;
82
83 while (*imsi != '\0') {
84 if (!isdigit(*imsi))
85 return 0xFFFF;
86 pgroup *= 10;
87 pgroup += *imsi - '0';
88 imsi++;
89 }
90 return pgroup;
91}
92
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040093/*
94 * PCU messages
95 */
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040096
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +020097/* Can be used to allocate message with non-variable size */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040098struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
99{
100 struct msgb *msg;
101 struct gsm_pcu_if *pcu_prim;
102
103 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
104 if (!msg)
105 return NULL;
106 msgb_put(msg, sizeof(struct gsm_pcu_if));
107 pcu_prim = (struct gsm_pcu_if *) msg->data;
108 pcu_prim->msg_type = msg_type;
109 pcu_prim->bts_nr = bts_nr;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400110
111 return msg;
112}
113
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200114/* Allocate message with extra size, only reserve pcuif msg hdr */
115static struct msgb *pcu_msgb_alloc_ext_size(uint8_t msg_type, uint8_t bts_nr, size_t extra_size)
116{
117 struct msgb *msg;
118 struct gsm_pcu_if *pcu_prim;
119 msg = msgb_alloc(sizeof(struct gsm_pcu_if) + extra_size, "pcu_sock_tx");
120 /* Only header is filled, caller is responible for reserving + filling
121 * message type specific contents: */
122 msgb_put(msg, PCUIF_HDR_SIZE);
123 pcu_prim = (struct gsm_pcu_if *) msgb_data(msg);
124 pcu_prim->msg_type = msg_type;
125 pcu_prim->bts_nr = bts_nr;
126 return msg;
127}
128
Max0a8fae82017-03-08 18:53:30 +0100129const struct value_string gsm_pcu_if_text_type_names[] = {
130 OSMO_VALUE_STRING(PCU_VERSION),
131 OSMO_VALUE_STRING(PCU_OML_ALERT),
132 { 0, NULL }
133};
134
135int pcu_tx_txt_ind(enum gsm_pcu_if_text_type t, const char *fmt, ...)
136{
137 struct gsm_pcu_if *pcu_prim;
138 struct gsm_pcu_if_txt_ind *txt;
139 va_list ap;
140 char *rep;
141 struct msgb *msg = pcu_msgb_alloc(PCU_IF_MSG_TXT_IND, 0);
142 if (!msg)
143 return -ENOMEM;
144
145 pcu_prim = (struct gsm_pcu_if *) msg->data;
146 txt = &pcu_prim->u.txt_ind;
147 txt->type = t;
148
149 va_start(ap, fmt);
150 rep = talloc_vasprintf(tall_pcu_ctx, fmt, ap);
151 va_end(ap);
152
153 if (!rep)
154 return -ENOMEM;
155
156 osmo_strlcpy(txt->text, rep, TXT_MAX_LEN);
157 talloc_free(rep);
158
159 LOGP(DL1IF, LOGL_INFO, "Sending %s TXT as %s to BTS\n", txt->text,
160 get_value_string(gsm_pcu_if_text_type_names, t));
161
162 return pcu_sock_send(msg);
163}
164
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100165static int pcu_tx_act_req(struct gprs_rlcmac_bts *bts, const struct gprs_rlcmac_pdch *pdch,
166 uint8_t activate)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400167{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400168 struct msgb *msg;
169 struct gsm_pcu_if *pcu_prim;
170 struct gsm_pcu_if_act_req *act_req;
171
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100172 LOGPDCH(pdch, DL1IF, LOGL_INFO, "Sending %s request\n",
173 (activate) ? "activate" : "deactivate");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400174
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100175 msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, bts->nr);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400176 if (!msg)
177 return -ENOMEM;
178 pcu_prim = (struct gsm_pcu_if *) msg->data;
179 act_req = &pcu_prim->u.act_req;
180 act_req->activate = activate;
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100181 act_req->trx_nr = pdch->trx_no();
182 act_req->ts_nr = pdch->ts_no;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400183
184 return pcu_sock_send(msg);
185}
186
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100187static int pcu_tx_data_req(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts, uint8_t sapi,
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400188 uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
189 uint8_t len)
190{
191 struct msgb *msg;
192 struct gsm_pcu_if *pcu_prim;
193 struct gsm_pcu_if_data *data_req;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100194 int current_fn = bts_current_frame_number(bts);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400195
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100196 LOGP(DL1IF, LOGL_DEBUG, "(bts=%u,trx=%u,ts=%u) FN=%u Sending data request: sapi=%d "
197 "arfcn=%d cur_fn=%d block=%d data=%s\n", bts->nr, trx, ts, fn, sapi,
198 arfcn, current_fn, block_nr, osmo_hexdump(data, len));
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400199
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100200 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, bts->nr);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400201 if (!msg)
202 return -ENOMEM;
203 pcu_prim = (struct gsm_pcu_if *) msg->data;
204 data_req = &pcu_prim->u.data_req;
205
206 data_req->sapi = sapi;
207 data_req->fn = fn;
208 data_req->arfcn = arfcn;
209 data_req->trx_nr = trx;
210 data_req->ts_nr = ts;
211 data_req->block_nr = block_nr;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200212 if (len)
213 memcpy(data_req->data, data, len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400214 data_req->len = len;
215
216 return pcu_sock_send(msg);
217}
218
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100219void pcu_l1if_tx_pdtch(msgb *msg, struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts, uint16_t arfcn,
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400220 uint32_t fn, uint8_t block_nr)
221{
Maxb216c6b2017-09-01 11:00:39 +0200222#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100223 if (bts->trx[trx].fl1h) {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200224 if (!msg) /* Simply skip sending idle frames to L1 */
225 return;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100226 l1if_pdch_req(bts->trx[trx].fl1h, ts, 0, fn, arfcn, block_nr,
227 msg->data, msg->len);
Max528ff392016-02-22 11:42:33 +0100228 msgb_free(msg);
229 return;
230 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100231#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200232 if (!msg) {
233 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
234 NULL, 0);
235 return;
236 }
237
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100238 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100239 msg->data, msg->len);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200240 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400241}
242
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100243void pcu_l1if_tx_ptcch(struct gprs_rlcmac_bts *bts,
244 uint8_t trx, uint8_t ts, uint16_t arfcn,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700245 uint32_t fn, uint8_t block_nr,
246 uint8_t *data, size_t data_len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400247{
Vadim Yanitskiyf5dde9f2022-09-30 02:21:59 +0700248 if (data_len && the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PTCCH))
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100249 gsmtap_send(the_pcu->gsmtap, arfcn, ts, GSMTAP_CHANNEL_PTCCH, 0, fn, 0, 0, data, data_len);
Maxcad867e2016-04-21 14:35:55 +0200250#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100251 if (bts->trx[trx].fl1h) {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200252 if (!data_len) /* Simply skip sending idle frames to L1 */
253 return;
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700254 l1if_pdch_req(bts->trx[trx].fl1h, ts, 1, fn, arfcn, block_nr, data, data_len);
Max528ff392016-02-22 11:42:33 +0100255 return;
256 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100257#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200258 if (!data_len) {
259 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, NULL, 0);
260 return;
261 }
262
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100263 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, data, data_len);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400264}
265
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100266void pcu_l1if_tx_agch(struct gprs_rlcmac_bts *bts, bitvec * block, int plen)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400267{
Pau Espin Pedrold752d7c2019-10-07 20:56:48 +0200268 uint8_t data[GSM_MACBLOCK_LEN]; /* prefix PLEN */
269
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400270 /* FIXME: why does OpenBTS has no PLEN and no fill in message? */
271 bitvec_pack(block, data + 1);
272 data[0] = (plen << 2) | 0x01;
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200273
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100274 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_AGCH))
275 gsmtap_send(the_pcu->gsmtap, 0, 0, GSMTAP_CHANNEL_AGCH, 0, 0, 0, 0, data, GSM_MACBLOCK_LEN);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200276
Philipp Maier3b66bbf2023-02-03 15:45:14 +0100277 pcu_tx_data_req(bts, 0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, sizeof(data));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400278}
279
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100280void pcu_l1if_tx_pch(struct gprs_rlcmac_bts *bts, bitvec * block, int plen, uint16_t pgroup)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400281{
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200282 uint8_t data[PAGING_GROUP_LEN + GSM_MACBLOCK_LEN];
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100283 int i;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400284
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200285 /* prepend paging group */
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100286 for (i = 0; i < PAGING_GROUP_LEN; i++) {
287 data[PAGING_GROUP_LEN - 1 - i] = '0' + (char)(pgroup % 10);
288 pgroup = pgroup / 10;
289 }
290 OSMO_ASSERT(pgroup == 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400291
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200292 /* block provided by upper layer comes without first byte (plen),
293 * prepend it manually:
294 */
295 OSMO_ASSERT(sizeof(data) >= PAGING_GROUP_LEN + 1 + block->data_len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400296 data[3] = (plen << 2) | 0x01;
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200297 bitvec_pack(block, data + PAGING_GROUP_LEN + 1);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200298
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100299 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PCH))
300 gsmtap_send(the_pcu->gsmtap, 0, 0, GSMTAP_CHANNEL_PCH, 0, 0, 0, 0, data + 3, GSM_MACBLOCK_LEN);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200301
Philipp Maier3b66bbf2023-02-03 15:45:14 +0100302 pcu_tx_data_req(bts, 0, 0, PCU_IF_SAPI_PCH, 0, 0, 0, data, sizeof(data));
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400303}
304
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200305int pcu_tx_neigh_addr_res_req(struct gprs_rlcmac_bts *bts, const struct neigh_cache_entry_key *neigh_key)
306{
307 struct msgb *msg;
308 struct gsm_pcu_if *pcu_prim;
309 struct gsm_pcu_if_neigh_addr_req *naddr_req;
310
311 LOGP(DL1IF, LOGL_DEBUG, "(bts=%u) Tx Neighbor Address Resolution Request: " NEIGH_CACHE_ENTRY_KEY_FMT "\n",
312 bts->nr, NEIGH_CACHE_ENTRY_KEY_ARGS(neigh_key));
313
314 msg = pcu_msgb_alloc_ext_size(PCU_IF_MSG_CONTAINER, bts->nr, sizeof(struct gsm_pcu_if_neigh_addr_req));
315 if (!msg)
316 return -ENOMEM;
317 pcu_prim = (struct gsm_pcu_if *) msgb_data(msg);
318 naddr_req = (struct gsm_pcu_if_neigh_addr_req *)&pcu_prim->u.container.data[0];
319
320 msgb_put(msg, sizeof(pcu_prim->u.container) + sizeof(struct gsm_pcu_if_neigh_addr_req));
321 pcu_prim->u.container.msg_type = PCU_IF_MSG_NEIGH_ADDR_REQ;
322 osmo_store16be(sizeof(struct gsm_pcu_if_neigh_addr_req), &pcu_prim->u.container.length);
323
324 osmo_store16be(neigh_key->local_lac, &naddr_req->local_lac);
325 osmo_store16be(neigh_key->local_ci, &naddr_req->local_ci);
326 osmo_store16be(neigh_key->tgt_arfcn, &naddr_req->tgt_arfcn);
327 naddr_req->tgt_bsic = neigh_key->tgt_bsic;
328
329 return pcu_sock_send(msg);
330}
331
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100332void pcu_rx_block_time(struct gprs_rlcmac_bts *bts, uint16_t arfcn, uint32_t fn, uint8_t ts_no)
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200333{
Pau Espin Pedrolfecab502021-03-17 15:26:37 +0100334 bts_set_current_block_frame_number(bts, fn);
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200335}
336
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100337int pcu_rx_data_ind_pdtch(struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_pdch *pdch, uint8_t *data,
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200338 uint8_t len, uint32_t fn, struct pcu_l1_meas *meas)
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100339{
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100340 int rc;
341
Pau Espin Pedrol3bbb3cc2021-08-31 14:09:42 +0200342 if (!pdch->is_enabled()) {
343 LOGPDCH(pdch, DL1IF, LOGL_INFO, "Received DATA.ind (PDTCH) on disabled TS\n");
344 return -EINVAL;
345 }
346
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100347 rc = pdch->rcv_block(data, len, fn, meas);
348 pdch_ulc_expire_fn(pdch->ulc, fn);
349 return rc;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100350}
351
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200352static int list_arfcn(const struct gprs_rlcmac_bts *bts, const struct gsm_sysinfo_freq *freq, const char *text)
353{
354 int n = 0, i;
355 for (i = 0; i < 1024; i++) {
356 if (freq[i].mask) {
357 if (!n)
358 LOGP(DL1IF, LOGL_INFO, "BTS%d: %s", bts->nr, text);
359 LOGPC(DL1IF, LOGL_INFO, " %d", i);
360 n++;
361 }
362 }
363 if (n)
364 LOGPC(DL1IF, LOGL_INFO, "\n");
365
366 return n;
367}
368
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100369static int pcu_rx_data_ind_bcch(struct gprs_rlcmac_bts *bts, uint8_t *data, uint8_t len)
Max84bf0fa2017-09-01 11:02:40 +0200370{
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200371 struct gsm48_system_information_type_2 *si2;
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100372 const uint8_t *si_ro;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200373
Philipp Maiera58ec612021-01-25 23:43:52 +0100374 switch (len) {
375 case 0:
376 /* Due to historical reasons also accept a completely empty message as
377 * revoke command for SI13. */
378 LOGP(DL1IF, LOGL_ERROR,
379 "Received PCU data indication that contains no data -- Revoked SI13.\n");
Max84bf0fa2017-09-01 11:02:40 +0200380 bts->si13_is_set = false;
Max84bf0fa2017-09-01 11:02:40 +0200381
Philipp Maiera58ec612021-01-25 23:43:52 +0100382 return 0;
383 case 1:
384 /* Revoke SI, type is identified by a single byte which is coded after
385 * enum osmo_sysinfo_type. */
386 switch (data[0]) {
387 case SYSINFO_TYPE_1:
388 bts->si1_is_set = false;
389 break;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200390 case SYSINFO_TYPE_2:
391 bts->si2_is_set = false;
392 break;
Philipp Maiera58ec612021-01-25 23:43:52 +0100393 case SYSINFO_TYPE_3:
394 bts->si3_is_set = false;
395 break;
396 case SYSINFO_TYPE_13:
397 bts->si13_is_set = false;
398 break;
399 default:
400 LOGP(DL1IF, LOGL_ERROR,
401 "Received PCU data indication that contains an unsupported system information identifier (%02x,OSMO) -- ignored.\n", data[0]);
402 return -EINVAL;
403 }
404 LOGP(DPCU, LOGL_DEBUG,
405 "Received PCU data indication: Revoked SI%s\n",
406 get_value_string(osmo_sitype_strs, data[0]));
407 return 0;
408 case GSM_MACBLOCK_LEN:
409 /* Update SI, type is identified by the RR sysinfo type, which is the
410 * 3rd byte in the buffer. */
411 switch (data[2]) {
412 case GSM48_MT_RR_SYSINFO_1:
413 memcpy(bts->si1, data, GSM_MACBLOCK_LEN);
414 bts->si1_is_set = true;
415 break;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200416 case GSM48_MT_RR_SYSINFO_2:
417 memcpy(bts->si2, data, GSM_MACBLOCK_LEN);
418 bts->si2_is_set = true;
419 si2 = (struct gsm48_system_information_type_2 *)bts->si2;
420 gsm48_decode_freq_list(bts->si2_bcch_cell_list, si2->bcch_frequency_list,
421 sizeof(si2->bcch_frequency_list), 0xce, 1);
422 list_arfcn(bts, bts->si2_bcch_cell_list, "SI2 Neighbour cells in same band:");
423 break;
Philipp Maiera58ec612021-01-25 23:43:52 +0100424 case GSM48_MT_RR_SYSINFO_3:
425 memcpy(bts->si3, data, GSM_MACBLOCK_LEN);
426 bts->si3_is_set = true;
427 break;
428 case GSM48_MT_RR_SYSINFO_13:
429 memcpy(bts->si13, data, GSM_MACBLOCK_LEN);
430 bts->si13_is_set = true;
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100431 si_ro = ((struct gsm48_system_information_type_13*)data)->rest_octets;
Pau Espin Pedrold65bd9d2021-06-01 16:43:41 +0200432 if (osmo_gsm48_rest_octets_si13_decode(&bts->si13_ro_decoded, si_ro) < 0)
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100433 LOGP(DPCU, LOGL_ERROR, "Error decoding SI13\n");
Philipp Maiera58ec612021-01-25 23:43:52 +0100434 break;
435 default:
436 LOGP(DL1IF, LOGL_ERROR,
437 "Received PCU data indication that contains an unsupported system information identifier (%02x,RR) -- ignored.\n", data[2]);
438 return -EINVAL;
439 }
440 LOGP(DPCU, LOGL_DEBUG,
441 "Received PCU data indication: Updated %s: %s\n",
442 gsm48_pdisc_msgtype_name(data[1], data[2]),
443 osmo_hexdump_nospc(data + 1, GSM_MACBLOCK_LEN));
444 return 0;
445 default:
446 LOGP(DL1IF, LOGL_ERROR,
447 "Received PCU data indication with unexpected data length: %u -- ignored.\n",
448 len);
Max84bf0fa2017-09-01 11:02:40 +0200449 return -EINVAL;
450 }
Max84bf0fa2017-09-01 11:02:40 +0200451}
452
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100453static int pcu_rx_data_ind(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400454{
Max528ff392016-02-22 11:42:33 +0100455 int rc;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100456 int current_fn = bts_current_frame_number(bts);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100457 struct pcu_l1_meas meas = {0};
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100458 struct gprs_rlcmac_pdch *pdch;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200459 uint8_t gsmtap_chantype;
Maxb216c6b2017-09-01 11:00:39 +0200460
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100461 LOGP(DL1IF, LOGL_DEBUG, "(bts=%" PRIu8 ",trx=%" PRIu8 ",ts=%" PRIu8 ") FN=%u "
462 "Rx DATA.ind: sapi=%d arfcn=%d cur_fn=%d "
463 "block=%d data=%s\n", bts->nr, data_ind->trx_nr, data_ind->ts_nr,
464 data_ind->fn, data_ind->sapi, data_ind->arfcn, current_fn,
465 data_ind->block_nr, osmo_hexdump(data_ind->data, data_ind->len));
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400466
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400467 switch (data_ind->sapi) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400468 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100469 pdch = &bts->trx[data_ind->trx_nr].pdch[data_ind->ts_nr];
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100470 pcu_l1_meas_set_rssi(&meas, data_ind->rssi);
Maxb216c6b2017-09-01 11:00:39 +0200471 /* convert BER to % value */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100472 pcu_l1_meas_set_ber(&meas, data_ind->ber10k / 100);
473 pcu_l1_meas_set_bto(&meas, data_ind->ta_offs_qbits);
474 pcu_l1_meas_set_link_qual(&meas, data_ind->lqual_cb / 10);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700475
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100476 LOGPDCH(pdch, DL1IF, LOGL_DEBUG, "FN=%u Rx DATA.ind PDTCH: "
477 "BER10k = %d, BTO = %d, Q = %d\n", data_ind->fn,
478 data_ind->ber10k, data_ind->ta_offs_qbits, data_ind->lqual_cb);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700479
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100480 rc = pcu_rx_data_ind_pdtch(bts, pdch, data_ind->data, data_ind->len,
481 data_ind->fn, &meas);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200482 gsmtap_chantype = GSMTAP_CHANNEL_PDTCH;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400483 break;
Max84bf0fa2017-09-01 11:02:40 +0200484 case PCU_IF_SAPI_BCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100485 rc = pcu_rx_data_ind_bcch(bts, data_ind->data, data_ind->len);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200486 gsmtap_chantype = GSMTAP_CHANNEL_BCCH;
Max84bf0fa2017-09-01 11:02:40 +0200487 break;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400488 default:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100489 LOGP(DL1IF, LOGL_ERROR, "(bts=%" PRIu8 ",trx=%" PRIu8 ",ts=%" PRIu8 ") "
490 "FN=%u Rx DATA.ind with unsupported sapi %d\n",
491 bts->nr, data_ind->trx_nr, data_ind->ts_nr, data_ind->fn, data_ind->sapi);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400492 rc = -EINVAL;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200493 gsmtap_chantype = GSMTAP_CHANNEL_UNKNOWN;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400494 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400495
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100496 if (rc < 0 && (the_pcu->gsmtap_categ_mask & (1 <<PCU_GSMTAP_C_UL_UNKNOWN))) {
497 gsmtap_send(the_pcu->gsmtap, data_ind->arfcn | GSMTAP_ARFCN_F_UPLINK, data_ind->ts_nr,
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200498 gsmtap_chantype, 0, data_ind->fn, meas.rssi, meas.link_qual, data_ind->data, data_ind->len);
Harald Welte717cdf52017-07-21 21:56:23 +0200499 }
500
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400501 return rc;
502}
503
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100504static int pcu_rx_data_cnf(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data *data_cnf)
Andreas Eversberga9be1542012-09-27 09:23:24 +0200505{
506 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100507 int current_fn = bts_current_frame_number(bts);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200508
Philipp Maier7e8e3972018-04-10 13:31:45 +0200509 LOGP(DL1IF, LOGL_DEBUG, "Data confirm received: sapi=%d fn=%d cur_fn=%d\n",
510 data_cnf->sapi, data_cnf->fn, current_fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200511
512 switch (data_cnf->sapi) {
513 case PCU_IF_SAPI_PCH:
Pau Espin Pedrolf510f5b2022-10-14 16:37:18 +0200514 if (data_cnf->data[2] == GSM48_MT_RR_IMM_ASS)
Philipp Maier7c3fd982023-02-01 16:10:52 +0100515 bts_rcv_imm_ass_cnf(bts, data_cnf->data, GSM_RESERVED_TMSI, data_cnf->fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200516 break;
517 default:
518 LOGP(DL1IF, LOGL_ERROR, "Received PCU data confirm with "
519 "unsupported sapi %d\n", data_cnf->sapi);
520 rc = -EINVAL;
521 }
522
523 return rc;
524}
525
Philipp Maier7c3fd982023-02-01 16:10:52 +0100526static int pcu_rx_data_cnf_dt(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data_cnf_dt *data_cnf_dt)
527{
528 int rc = 0;
529 int current_fn = bts_current_frame_number(bts);
530
531 LOGP(DL1IF, LOGL_DEBUG, "Data confirm received: sapi=%d fn=%d cur_fn=%d\n",
532 data_cnf_dt->sapi, data_cnf_dt->fn, current_fn);
533
534 switch (data_cnf_dt->sapi) {
535 case PCU_IF_SAPI_PCH:
536 bts_rcv_imm_ass_cnf(bts, NULL, data_cnf_dt->tlli, data_cnf_dt->fn);
537 break;
538 default:
539 LOGP(DL1IF, LOGL_ERROR, "Received PCU data confirm with unsupported sapi %d\n", data_cnf_dt->sapi);
540 rc = -EINVAL;
541 }
542
543 return rc;
544}
545
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100546// FIXME: remove this, when changed from c++ to c.
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100547int pcu_rx_rts_req_pdtch(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100548 uint32_t fn, uint8_t block_nr)
549{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100550 return gprs_rlcmac_rcv_rts_block(bts,
Max878bd1f2016-07-20 13:05:05 +0200551 trx, ts, fn, block_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100552}
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100553int pcu_rx_rts_req_ptcch(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700554 uint32_t fn, uint8_t block_nr)
555{
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700556 struct gprs_rlcmac_pdch *pdch;
557
558 /* Prevent buffer overflow */
559 if (trx >= ARRAY_SIZE(bts->trx) || ts >= 8)
560 return -EINVAL;
561
562 /* Make sure PDCH time-slot is enabled */
563 pdch = &bts->trx[trx].pdch[ts];
Pau Espin Pedrol9c2512a2022-04-01 18:27:04 +0200564 if (!pdch_is_enabled(pdch))
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700565 return -EAGAIN;
566
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200567 /* If there's no TBF attached to this PDCH, we can skip Tx of PTCCH
568 * since there's nothing worthy of being transmitted. This way BTS can
569 * identify idle blocks and send nothing or dumy blocks with reduced
570 * energy for the sake of energy saving.
571 */
572 const unsigned num_tbfs = pdch->num_tbfs(GPRS_RLCMAC_DL_TBF)
573 + pdch->num_tbfs(GPRS_RLCMAC_UL_TBF);
574 bool skip_idle = (num_tbfs == 0);
575#ifdef ENABLE_DIRECT_PHY
576 /* In DIRECT_PHY mode we want to always submit something to L1 in
577 * TRX0, since BTS is not preparing dummy bursts on idle TS for us: */
578 skip_idle = skip_idle && trx != 0;
579#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200580 if (skip_idle) {
581 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
582 NULL, 0);
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200583 return 0;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200584 }
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200585
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100586 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700587 pdch->ptcch_msg, GSM_MACBLOCK_LEN);
588 return 0;
589}
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100590
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100591static int pcu_rx_rts_req(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_rts_req *rts_req)
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400592{
593 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100594 int current_fn = bts_current_frame_number(bts);
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100595 const struct gprs_rlcmac_pdch *pdch;
596 pdch = &bts->trx[rts_req->trx_nr].pdch[rts_req->ts_nr];
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400597
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100598 LOGPDCH(pdch, DL1IF, LOGL_DEBUG, "FN=%u RX RTS.req: sapi=%d "
599 "arfcn=%d cur_fn=%d block=%d\n", rts_req->fn,
600 rts_req->sapi, rts_req->arfcn, current_fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400601
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400602 switch (rts_req->sapi) {
603 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100604 pcu_rx_rts_req_pdtch(bts, rts_req->trx_nr, rts_req->ts_nr,
Max878bd1f2016-07-20 13:05:05 +0200605 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400606 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400607 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100608 pcu_rx_rts_req_ptcch(bts, rts_req->trx_nr, rts_req->ts_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700609 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400610 break;
611 default:
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100612 LOGP(DL1IF, LOGL_ERROR, "(bts=%u,trx=%u,ts=%u) FN=%u RX RTS.req with "
613 "unsupported sapi %d\n", bts->nr, rts_req->trx_nr, rts_req->ts_nr,
614 rts_req->fn, rts_req->sapi);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400615 rc = -EINVAL;
616 }
617
618 return rc;
619}
620
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700621/* C -> C++ adapter for direct DSP access code (e.g. osmo-bts-sysmo) */
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100622extern "C" int pcu_rx_rach_ind_ptcch(struct gprs_rlcmac_bts *bts, uint8_t trx_nr, uint8_t ts_nr, uint32_t fn, int16_t qta)
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700623{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700624 struct rach_ind_params rip = {
625 /* The content of RA is not of interest on PTCCH/U */
626 .burst_type = GSM_L1_BURST_TYPE_ACCESS_0,
627 .is_11bit = false,
628 .ra = 0x00,
629 .trx_nr = trx_nr,
630 .ts_nr = ts_nr,
631 .rfn = fn,
632 .qta = qta,
633 };
634
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100635 return bts_rcv_ptcch_rach(bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700636}
637
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100638static int pcu_rx_rach_ind(struct gprs_rlcmac_bts *bts, const struct gsm_pcu_if_rach_ind *rach_ind)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400639{
640 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100641 int current_fn = bts_current_frame_number(bts);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400642
643 LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
Vadim Yanitskiyb30f28f2020-03-29 19:01:58 +0700644 "qta=%d, ra=0x%02x, fn=%u, cur_fn=%d, is_11bit=%d\n", rach_ind->sapi, rach_ind->qta,
Philipp Maier7e8e3972018-04-10 13:31:45 +0200645 rach_ind->ra, rach_ind->fn, current_fn, rach_ind->is_11bit);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400646
Philipp Maier40db4c32023-01-10 12:49:28 +0100647 if (OSMO_UNLIKELY(rach_ind->fn > GSM_TDMA_HYPERFRAME - 1)) {
648 LOGP(DL1IF, LOGL_ERROR, "RACH request contains fn=%u that exceeds valid limits (0-%u) -- ignored!\n",
649 rach_ind->fn, GSM_TDMA_HYPERFRAME - 1);
650 return -EINVAL;
651 }
652
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700653 struct rach_ind_params rip = {
654 .burst_type = (enum ph_burst_type) rach_ind->burst_type,
655 .is_11bit = rach_ind->is_11bit > 0,
656 .ra = rach_ind->ra,
657 .trx_nr = rach_ind->trx_nr,
658 .ts_nr = rach_ind->ts_nr,
659 .rfn = rach_ind->fn,
660 .qta = rach_ind->qta,
661 };
662
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400663 switch (rach_ind->sapi) {
664 case PCU_IF_SAPI_RACH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100665 rc = bts_rcv_rach(bts, &rip);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400666 break;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700667 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100668 rc = bts_rcv_ptcch_rach(bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700669 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400670 default:
671 LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
672 "unsupported sapi %d\n", rach_ind->sapi);
673 rc = -EINVAL;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400674 }
675
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400676 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400677}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200678
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200679static int pcu_info_ind_ns(struct gprs_rlcmac_bts *bts,
680 const struct gsm_pcu_if_info_ind *info_ind)
681{
682 struct osmo_sockaddr remote[PCU_IF_NUM_NSVC] = { };
683 struct osmo_sockaddr local[PCU_IF_NUM_NSVC] = { };
684 uint16_t nsvci[PCU_IF_NUM_NSVC] = { };
685 uint16_t valid = 0;
686
687 for (unsigned int i = 0; i < PCU_IF_NUM_NSVC; i++) {
688 struct osmo_sockaddr_str sockstr;
689
690 switch (info_ind->address_type[i]) {
691 case PCU_IF_ADDR_TYPE_IPV4:
692 local[i].u.sin.sin_family = AF_INET;
693 local[i].u.sin.sin_addr.s_addr = INADDR_ANY;
694 local[i].u.sin.sin_port = htons(info_ind->local_port[i]);
695
696 remote[i].u.sin.sin_family = AF_INET;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100697 memcpy(&remote[i].u.sin.sin_addr, &info_ind->remote_ip[i].v4,
698 sizeof(struct in_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200699 remote[i].u.sin.sin_port = htons(info_ind->remote_port[i]);
700 break;
701 case PCU_IF_ADDR_TYPE_IPV6:
702 local[i].u.sin6.sin6_family = AF_INET6;
703 local[i].u.sin6.sin6_addr = in6addr_any;
704 local[i].u.sin6.sin6_port = htons(info_ind->local_port[i]);
705
706 remote[i].u.sin6.sin6_family = AF_INET6;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100707 memcpy(&remote[i].u.sin6.sin6_addr,
708 &info_ind->remote_ip[i].v6,
709 sizeof(struct in6_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200710 remote[i].u.sin6.sin6_port = htons(info_ind->remote_port[i]);
711 break;
712 default:
713 continue;
714 }
715 nsvci[i] = info_ind->nsvci[i];
716
717 LOGP(DL1IF, LOGL_DEBUG, " NS%u nsvci=%u\n", i, nsvci[i]);
718 if (osmo_sockaddr_str_from_sockaddr(&sockstr, &remote[i].u.sas))
719 strcpy(sockstr.ip, "invalid");
720
721 LOGP(DL1IF, LOGL_DEBUG, " NS%u address: r=%s:%u<->l=NULL:%u\n",
722 i, sockstr.ip, sockstr.port, info_ind->local_port[i]);
723
724 valid |= 1 << i;
725 }
726
727 if (valid == 0) {
728 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
729 return -EINVAL;
730 }
731
Alexander Couzens94a367f2021-02-16 16:57:15 +0100732 return gprs_ns_update_config(bts, info_ind->nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200733}
734
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100735static int pcu_rx_info_ind(struct gprs_rlcmac_bts *bts, const struct gsm_pcu_if_info_ind *info_ind)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200736{
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +0200737 struct gprs_bssgp_pcu *pcu;
Harald Weltebb47d952016-11-17 18:40:02 +0100738 int rc = 0;
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700739 unsigned int trx_nr, ts_nr;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100740 unsigned int i;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200741
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200742 if (info_ind->version != PCU_IF_VERSION) {
Vadim Yanitskiyfad21282020-07-19 02:05:33 +0700743 fprintf(stderr, "PCU interface version number of BTS (%u) is "
744 "different (%u).\nPlease re-compile!\n",
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200745 info_ind->version, PCU_IF_VERSION);
746 exit(-1);
747 }
748
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400749 LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200750
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400751 if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
752 LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100753 if (!bts->active)
754 return -EAGAIN;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400755bssgp_failed:
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100756 bts->active = false;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400757 /* free all TBF */
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700758 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
759 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
760 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++)
Pau Espin Pedrol2d92e392022-03-30 22:09:06 +0200761 if (bts->trx[trx_nr].pdch[ts_nr].is_enabled())
762 bts->trx[trx_nr].pdch[ts_nr].disable();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400763 }
Alexander Couzens290d9032020-09-16 21:52:02 +0200764 gprs_bssgp_destroy(bts);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200765 exit(0);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200766 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400767 LOGP(DL1IF, LOGL_INFO, "BTS available\n");
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100768 LOGP(DL1IF, LOGL_DEBUG, " mcc=%03u\n", info_ind->mcc);
769 LOGP(DL1IF, LOGL_DEBUG, " mnc=%0*u\n", info_ind->mnc_3_digits, info_ind->mnc);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400770 LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
771 LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
Harald Welte1473b372019-03-21 19:48:55 +0100772 LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100773 LOGP(DL1IF, LOGL_DEBUG, " bsic=%d\n", info_ind->bsic);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400774 LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
775 LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
776 info_ind->nse_timer[0], info_ind->nse_timer[1],
777 info_ind->nse_timer[2], info_ind->nse_timer[3],
778 info_ind->nse_timer[4], info_ind->nse_timer[5],
779 info_ind->nse_timer[6]);
780 LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
781 "%d\n",
782 info_ind->cell_timer[0], info_ind->cell_timer[1],
783 info_ind->cell_timer[2], info_ind->cell_timer[3],
784 info_ind->cell_timer[4], info_ind->cell_timer[5],
785 info_ind->cell_timer[6], info_ind->cell_timer[7],
786 info_ind->cell_timer[8], info_ind->cell_timer[9],
787 info_ind->cell_timer[10]);
788 LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
789 LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
790 LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
791 LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
792 LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
793 LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
794 LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
795 LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
796 LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
797 LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
798 LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
799 LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
800 LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
801 LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
Pau Espin Pedrol2e6b60d2021-01-20 13:48:31 +0100802 bts->cgi_ps.rai.lac.plmn.mcc = info_ind->mcc;
803 bts->cgi_ps.rai.lac.plmn.mnc = info_ind->mnc;
804 bts->cgi_ps.rai.lac.plmn.mnc_3_digits = info_ind->mnc_3_digits;
805 bts->cgi_ps.rai.lac.lac = info_ind->lac;
806 bts->cgi_ps.rai.rac = info_ind->rac;
807 bts->cgi_ps.cell_identity = info_ind->cell_id;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100808 bts->bsic = info_ind->bsic;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100809
810 bts->cs_mask = 1 << 0; /* We need at least 1 CS, let's enable CS1 */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400811 for (i = 0; i < 4; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100812 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_CS1 << i));
813 bts->cs_mask |= allowed << i;
814 if (allowed)
815 LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i + 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400816 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100817 bts_recalc_max_cs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100818
819 bts->mcs_mask = 0;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400820 for (i = 0; i < 9; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100821 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_MCS1 << i));
822 bts->mcs_mask |= allowed << i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100823 if (allowed)
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100824 LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i + 1);
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100825
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400826 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100827 bts_recalc_max_mcs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100828
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100829 LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%u%s\n", info_ind->initial_cs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100830 the_pcu->vty.force_initial_cs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100831 bts->pcuif_info_ind.initial_cs = info_ind->initial_cs;
832 bts_recalc_initial_cs(bts);
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100833
834 LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%u%s\n", info_ind->initial_mcs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100835 the_pcu->vty.force_initial_mcs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100836 bts->pcuif_info_ind.initial_mcs = info_ind->initial_mcs;
837 bts_recalc_initial_mcs(bts);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200838
Alexander Couzens290d9032020-09-16 21:52:02 +0200839 pcu = gprs_bssgp_init(
840 bts,
841 info_ind->nsei, info_ind->bvci,
842 info_ind->mcc, info_ind->mnc, info_ind->mnc_3_digits,
843 info_ind->lac, info_ind->rac, info_ind->cell_id);
844 if (!pcu) {
Vadim Yanitskiyc1a726c2020-10-08 22:46:26 +0700845 LOGP(DL1IF, LOGL_ERROR, "Failed to init BSSGP\n");
Alexander Couzens9b5c9602020-07-28 13:47:18 +0200846 goto bssgp_failed;
847 }
848
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200849 rc = pcu_info_ind_ns(pcu->bts, info_ind);
850 if (rc < 0) {
Alexander Couzens290d9032020-09-16 21:52:02 +0200851 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400852 goto bssgp_failed;
853 }
854
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400855 if (info_ind->t3142) { /* if timer values are set */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200856 osmo_tdef_set(bts->T_defs_bts, 3142, info_ind->t3142, OSMO_TDEF_S);
857 osmo_tdef_set(bts->T_defs_bts, 3169, info_ind->t3169, OSMO_TDEF_S);
858 osmo_tdef_set(bts->T_defs_bts, 3191, info_ind->t3191, OSMO_TDEF_S);
859 osmo_tdef_set(bts->T_defs_bts, 3193, info_ind->t3193_10ms * 10, OSMO_TDEF_MS);
860 osmo_tdef_set(bts->T_defs_bts, 3195, info_ind->t3195, OSMO_TDEF_S);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400861 bts->n3101 = info_ind->n3101;
862 bts->n3103 = info_ind->n3103;
863 bts->n3105 = info_ind->n3105;
864 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400865
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700866 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
867 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100868 if ((info_ind->flags & PCU_IF_FLAG_SYSMO)
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700869 && info_ind->trx[trx_nr].hlayer1) {
Maxcad867e2016-04-21 14:35:55 +0200870#ifdef ENABLE_DIRECT_PHY
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700871 LOGP(DL1IF, LOGL_DEBUG, " TRX %d hlayer1=%x\n", trx_nr,
872 info_ind->trx[trx_nr].hlayer1);
873 if (!bts->trx[trx_nr].fl1h)
874 bts->trx[trx_nr].fl1h = l1if_open_pdch(
875 trx_nr,
876 info_ind->trx[trx_nr].hlayer1,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100877 the_pcu->gsmtap);
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700878 if (!bts->trx[trx_nr].fl1h) {
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100879 LOGP(DL1IF, LOGL_FATAL, "Failed to open direct "
880 "DSP access for PDCH.\n");
881 exit(0);
882 }
883#else
884 LOGP(DL1IF, LOGL_FATAL, "Compiled without direct DSP "
885 "access for PDCH, but enabled at "
886 "BTS. Please deactivate it!\n");
887 exit(0);
888#endif
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100889 }
890
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700891 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++) {
Philipp Maier2d222a12022-10-27 15:25:31 +0200892 const struct gsm_pcu_if_info_trx_ts *its = &info_ind->trx[trx_nr].ts[ts_nr];
Vadim Yanitskiy0f41b712020-07-24 01:28:13 +0700893 struct gprs_rlcmac_pdch *pdch = &bts->trx[trx_nr].pdch[ts_nr];
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700894 if ((info_ind->trx[trx_nr].pdch_mask & (1 << ts_nr))) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400895 /* FIXME: activate dynamically at RLCMAC */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200896 if (!pdch->is_enabled()) {
Maxcad867e2016-04-21 14:35:55 +0200897#ifdef ENABLE_DIRECT_PHY
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100898 if ((info_ind->flags &
899 PCU_IF_FLAG_SYSMO))
900 l1if_connect_pdch(
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700901 bts->trx[trx_nr].fl1h, ts_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100902#endif
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100903 pcu_tx_act_req(bts, pdch, 1);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200904 pdch->enable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200905 }
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700906
907 pdch->tsc = its->tsc;
908
909 /* (Optional) frequency hopping parameters */
Philipp Maier2e490582022-10-27 15:07:57 +0200910 if (its->hopping) {
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700911 pdch->fh.enabled = true;
912 pdch->fh.maio = its->maio;
913 pdch->fh.hsn = its->hsn;
914
915 OSMO_ASSERT(its->ma_bit_len <= sizeof(pdch->fh.ma) * 8);
916 pdch->fh.ma_oct_len = OSMO_BYTES_FOR_BITS(its->ma_bit_len);
917 pdch->fh.ma_bit_len = its->ma_bit_len;
918
919 /* Mobile Allocation + padding (byte/bit order as on the wire):
920 * | 00 00 00 00 00 cc bb aa | -> | cc bb aa 00 00 00 00 00 | */
921 unsigned int offset = sizeof(pdch->fh.ma) - pdch->fh.ma_oct_len;
922 memcpy(pdch->fh.ma, its->ma + offset, pdch->fh.ma_oct_len);
923 }
924
925 LOGP(DL1IF, LOGL_INFO, "PDCH (trx=%u, ts=%u): tsc=%u, hopping=%s\n",
926 trx_nr, ts_nr, pdch->tsc, pdch->fh.enabled ? "yes" : "no");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400927 } else {
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200928 if (pdch->is_enabled()) {
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100929 pcu_tx_act_req(bts, pdch, 0);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200930 pdch->disable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200931 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400932 }
933 }
934 }
935
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100936 bts->active = true;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200937 return rc;
938}
939
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100940static int pcu_rx_time_ind(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_time_ind *time_ind)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200941{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400942 uint8_t fn13 = time_ind->fn % 13;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200943
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400944 /* omit frame numbers not starting at a MAC block */
945 if (fn13 != 0 && fn13 != 4 && fn13 != 8)
946 return 0;
Max3741f142017-06-26 12:10:12 +0200947
948 LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n", time_ind->fn % 52);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400949
Pau Espin Pedrol30617112021-03-05 18:59:05 +0100950 /* Ignore TIME.ind completely, we nowadays relay on DATA.ind always
951 * providing all block FNs. */
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200952 return 0;
953}
954
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100955static int pcu_rx_pag_req(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_pag_req *pag_req)
Andreas Eversberg2b914642012-07-19 13:06:26 +0200956{
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200957 struct osmo_mobile_identity mi;
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200958 struct GprsMs *ms = NULL;
959 struct paging_req_cs req = { .chan_needed = pag_req->chan_needed,
960 .tlli = GSM_RESERVED_TMSI };
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200961 int rc;
962
Andreas Eversberg2b914642012-07-19 13:06:26 +0200963 LOGP(DL1IF, LOGL_DEBUG, "Paging request received: chan_needed=%d "
964 "length=%d\n", pag_req->chan_needed, pag_req->identity_lv[0]);
965
Pau Espin Pedrol32499b62019-12-09 13:55:12 +0100966 /* check if identity does not fit: length > sizeof(lv) - 1 */
967 if (pag_req->identity_lv[0] >= sizeof(pag_req->identity_lv)) {
968 LOGP(DL1IF, LOGL_ERROR, "Paging identity too large (%" PRIu8 ")\n",
969 pag_req->identity_lv[0]);
970 return -EINVAL;
971 }
972
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200973 rc = osmo_mobile_identity_decode(&mi, &pag_req->identity_lv[1], pag_req->identity_lv[0], true);
974 if (rc < 0) {
975 LOGP(DL1IF, LOGL_ERROR, "Failed to decode Mobile Identity in Paging Request (rc=%d)\n", rc);
976 return -EINVAL;
977 }
978
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200979 switch (mi.type) {
980 case GSM_MI_TYPE_TMSI:
981 req.mi_tmsi = mi;
982 req.mi_tmsi_present = true;
983 /* TODO: look up MS by TMSI? Derive TLLI? */
984 break;
985 case GSM_MI_TYPE_IMSI:
986 req.mi_imsi = mi;
987 req.mi_imsi_present = true;
988 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
989 break;
990 default:
991 LOGP(DL1IF, LOGL_ERROR, "Unexpected MI type %u\n", mi.type);
992 return -EINVAL;
993 }
994
995 return bts_add_paging(bts, &req, ms);
Andreas Eversberg2b914642012-07-19 13:06:26 +0200996}
997
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100998static int pcu_rx_susp_req(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_susp_req *susp_req)
Harald Welte3447c4a2017-07-11 00:06:38 +0200999{
Philipp Maiera58ec612021-01-25 23:43:52 +01001000 struct bssgp_bvc_ctx *bctx = the_pcu->bssgp.bctx;
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +02001001 GprsMs *ms;
1002 struct gprs_rlcmac_dl_tbf *dl_tbf;
1003 struct gprs_rlcmac_ul_tbf *ul_tbf;
Harald Welte3447c4a2017-07-11 00:06:38 +02001004 struct gprs_ra_id ra_id;
1005
1006 gsm48_parse_ra(&ra_id, susp_req->ra_id);
1007
1008 LOGP(DL1IF, LOGL_INFO, "GPRS Suspend request received: TLLI=0x%08x RAI=%s\n",
1009 susp_req->tlli, osmo_rai_name(&ra_id));
1010
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001011 if ((ms = bts_ms_store(bts)->get_ms(susp_req->tlli))) {
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +02001012 /* We need to catch both pointers here since MS may become freed
1013 after first tbf_free(dl_tbf) if only DL TBF was available */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001014 dl_tbf = ms_dl_tbf(ms);
1015 ul_tbf = ms_ul_tbf(ms);
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +02001016 if (dl_tbf)
1017 tbf_free(dl_tbf);
1018 if (ul_tbf)
1019 tbf_free(ul_tbf);
1020 }
1021
Harald Welte3447c4a2017-07-11 00:06:38 +02001022 if (!bctx)
1023 return -1;
1024
1025 return bssgp_tx_suspend(bctx->nsei, susp_req->tlli, &ra_id);
1026}
1027
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001028static int pcu_rx_app_info_req(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_app_info_req *app_info_req)
Oliver Smithcfb63212019-09-05 17:13:33 +02001029{
Pau Espin Pedrol54faf022021-01-14 12:12:43 +01001030 struct llist_head *tmp;
Oliver Smithcfb63212019-09-05 17:13:33 +02001031
1032 LOGP(DL1IF, LOGL_DEBUG, "Application Information Request received: type=0x%08x len=%i\n",
1033 app_info_req->application_type, app_info_req->len);
1034
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001035 bts->app_info_pending = 0;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001036 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
Pau Espin Pedrol54faf022021-01-14 12:12:43 +01001037 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001038 if (!ms_dl_tbf(ms))
Oliver Smithcfb63212019-09-05 17:13:33 +02001039 continue;
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001040 bts->app_info_pending++;
Oliver Smithcfb63212019-09-05 17:13:33 +02001041 ms->app_info_pending = true;
1042 }
1043
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001044 if (!bts->app_info_pending) {
Oliver Smithcfb63212019-09-05 17:13:33 +02001045 LOGP(DL1IF, LOGL_NOTICE, "Packet Application Information will not be sent, no subscribers with active"
1046 " TBF\n");
1047 return -1;
1048 }
1049
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001050 if (bts->app_info) {
Oliver Smithcfb63212019-09-05 17:13:33 +02001051 LOGP(DL1IF, LOGL_NOTICE, "Previous Packet Application Information was not sent to all subscribers,"
1052 " overwriting with new one\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001053 msgb_free(bts->app_info);
Oliver Smithcfb63212019-09-05 17:13:33 +02001054 }
1055
1056 LOGP(DL1IF, LOGL_INFO, "Sending Packet Application Information to %i subscribers with active TBF\n",
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001057 bts->app_info_pending);
1058 bts->app_info = gprs_rlcmac_app_info_msg(app_info_req);
Oliver Smithcfb63212019-09-05 17:13:33 +02001059 return 0;
1060}
1061
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001062static int pcu_rx_neigh_addr_cnf(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_neigh_addr_cnf *naddr_cnf)
1063{
1064 struct llist_head *tmp;
1065 struct osmo_cell_global_id_ps cgi_ps;
1066 struct osmo_cell_global_id_ps *cgi_ps_ptr = &cgi_ps;
1067
1068 struct neigh_cache_entry_key neigh_key = {
1069 .local_lac = osmo_load16be(&naddr_cnf->orig_req.local_lac),
1070 .local_ci = osmo_load16be(&naddr_cnf->orig_req.local_ci),
1071 .tgt_arfcn = osmo_load16be(&naddr_cnf->orig_req.tgt_arfcn),
1072 .tgt_bsic = naddr_cnf->orig_req.tgt_bsic,
1073 };
1074
1075 if (naddr_cnf->err_code == 0) {
1076 cgi_ps.rai.lac.plmn.mcc = osmo_load16be(&naddr_cnf->cgi_ps.mcc);
1077 cgi_ps.rai.lac.plmn.mnc = osmo_load16be(&naddr_cnf->cgi_ps.mnc);
1078 cgi_ps.rai.lac.plmn.mnc_3_digits = naddr_cnf->cgi_ps.mnc_3_digits;
1079 cgi_ps.rai.lac.lac = osmo_load16be(&naddr_cnf->cgi_ps.lac);
1080 cgi_ps.rai.rac = naddr_cnf->cgi_ps.rac;
1081 cgi_ps.cell_identity = osmo_load16be(&naddr_cnf->cgi_ps.cell_identity);
1082
1083 LOGP(DL1IF, LOGL_INFO, "Rx Neighbor Address Resolution Confirmation for " NEIGH_CACHE_ENTRY_KEY_FMT ": %s\n",
1084 NEIGH_CACHE_ENTRY_KEY_ARGS(&neigh_key), osmo_cgi_ps_name(&cgi_ps));
1085
1086 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
1087 neigh_cache_add(bts->pcu->neigh_cache, &neigh_key, &cgi_ps);
1088 } else {
1089 cgi_ps_ptr = NULL;
1090 LOGP(DL1IF, LOGL_INFO, "Rx Neighbor Address Resolution Confirmation for " NEIGH_CACHE_ENTRY_KEY_FMT ": failed with err_code=%u\n",
1091 NEIGH_CACHE_ENTRY_KEY_ARGS(&neigh_key), naddr_cnf->err_code);
1092 }
1093
1094 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
1095 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
1096 if (ms->nacc && nacc_fsm_is_waiting_addr_resolution(ms->nacc, &neigh_key))
1097 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_RAC_CI, cgi_ps_ptr);
1098 }
1099 return 0;
1100}
1101
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001102static int pcu_rx_container(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_container *container)
1103{
1104 int rc;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001105 uint16_t data_length = osmo_load16be(&container->length);
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001106
1107 switch (container->msg_type) {
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001108 case PCU_IF_MSG_NEIGH_ADDR_CNF:
1109 if (data_length < sizeof(struct gsm_pcu_if_neigh_addr_cnf)) {
1110 LOGP(DL1IF, LOGL_ERROR, "Rx container(NEIGH_ADDR_CNF) message too short: %u vs exp %zu\n",
1111 data_length, sizeof(struct gsm_pcu_if_neigh_addr_cnf));
1112 return -EINVAL;
1113 }
1114 rc = pcu_rx_neigh_addr_cnf(bts, (struct gsm_pcu_if_neigh_addr_cnf*)&container->data);
1115 break;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001116 default:
1117 LOGP(DL1IF, LOGL_NOTICE, "(bts=%d) Rx unexpected msg type (%u) inside container!\n",
1118 bts->nr, container->msg_type);
1119 rc = -1;
1120 }
1121 return rc;
1122}
1123
1124#define CHECK_IF_MSG_SIZE(prim_len, prim_msg) \
1125 do { \
1126 size_t _len = PCUIF_HDR_SIZE + sizeof(prim_msg); \
1127 if (prim_len < _len) { \
1128 LOGP(DL1IF, LOGL_ERROR, "Received %zu bytes on PCU Socket, but primitive %s " \
1129 "size is %zu, discarding\n", prim_len, #prim_msg, _len); \
1130 return -EINVAL; \
1131 } \
1132 } while(0);
1133int pcu_rx(struct gsm_pcu_if *pcu_prim, size_t pcu_prim_length)
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001134{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001135 int rc = 0;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001136 size_t exp_len;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001137 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, pcu_prim->bts_nr);
1138 if (!bts) {
1139 LOGP(DL1IF, LOGL_NOTICE, "Received message for new BTS%d\n", pcu_prim->bts_nr);
1140 bts = bts_alloc(the_pcu, pcu_prim->bts_nr);
1141 if (!bts) {
1142 LOGP(DL1IF, LOGL_ERROR, "Failed to create object for BTS%d!\n", pcu_prim->bts_nr);
1143 return -EAGAIN;
1144 }
1145 }
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001146
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001147 switch (pcu_prim->msg_type) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001148 case PCU_IF_MSG_DATA_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001149 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001150 rc = pcu_rx_data_ind(bts, &pcu_prim->u.data_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001151 break;
Andreas Eversberga9be1542012-09-27 09:23:24 +02001152 case PCU_IF_MSG_DATA_CNF:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001153 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_cnf);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001154 rc = pcu_rx_data_cnf(bts, &pcu_prim->u.data_cnf);
Andreas Eversberga9be1542012-09-27 09:23:24 +02001155 break;
Philipp Maier7c3fd982023-02-01 16:10:52 +01001156 case PCU_IF_MSG_DATA_CNF_DT:
1157 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_cnf_dt);
1158 rc = pcu_rx_data_cnf_dt(bts, &pcu_prim->u.data_cnf_dt);
1159 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001160 case PCU_IF_MSG_RTS_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001161 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.rts_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001162 rc = pcu_rx_rts_req(bts, &pcu_prim->u.rts_req);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001163 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001164 case PCU_IF_MSG_RACH_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001165 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.rach_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001166 rc = pcu_rx_rach_ind(bts, &pcu_prim->u.rach_ind);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001167 break;
1168 case PCU_IF_MSG_INFO_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001169 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.info_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001170 rc = pcu_rx_info_ind(bts, &pcu_prim->u.info_ind);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001171 break;
1172 case PCU_IF_MSG_TIME_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001173 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.time_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001174 rc = pcu_rx_time_ind(bts, &pcu_prim->u.time_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001175 break;
Andreas Eversberg2b914642012-07-19 13:06:26 +02001176 case PCU_IF_MSG_PAG_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001177 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.pag_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001178 rc = pcu_rx_pag_req(bts, &pcu_prim->u.pag_req);
Andreas Eversberg2b914642012-07-19 13:06:26 +02001179 break;
Harald Welte3447c4a2017-07-11 00:06:38 +02001180 case PCU_IF_MSG_SUSP_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001181 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.susp_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001182 rc = pcu_rx_susp_req(bts, &pcu_prim->u.susp_req);
Harald Welte3447c4a2017-07-11 00:06:38 +02001183 break;
Oliver Smithcfb63212019-09-05 17:13:33 +02001184 case PCU_IF_MSG_APP_INFO_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001185 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.app_info_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001186 rc = pcu_rx_app_info_req(bts, &pcu_prim->u.app_info_req);
Oliver Smithcfb63212019-09-05 17:13:33 +02001187 break;
Vadim Yanitskiyb6572132021-06-20 21:05:24 +02001188 case PCU_IF_MSG_INTERF_IND:
1189 /* TODO: handle interference reports */
1190 break;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001191 case PCU_IF_MSG_CONTAINER:
1192 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.container);
1193 /* ^ check if we can access container fields, v check with container data length */
1194 exp_len = PCUIF_HDR_SIZE + sizeof(pcu_prim->u.container) + osmo_load16be(&pcu_prim->u.container.length);
1195 if (pcu_prim_length < exp_len) {
1196 LOGP(DL1IF, LOGL_ERROR, "Received %zu bytes on PCU Socket, but primitive container size" \
1197 "is %zu, discarding\n", pcu_prim_length, exp_len);
1198 }
1199 rc = pcu_rx_container(bts, &pcu_prim->u.container);
1200 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001201 default:
Thorsten Alteholz8bb79042018-10-06 10:42:58 +02001202 LOGP(DL1IF, LOGL_ERROR, "Received unknown PCU msg type %d\n",
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001203 pcu_prim->msg_type);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001204 rc = -EINVAL;
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001205 }
Philipp Maier8ccf7042018-04-10 13:12:42 +02001206
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001207 return rc;
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001208}