blob: 221a1f742779ab771ed62603351766860ce5668d [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>
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +020043
44#include <nacc_fsm.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020045}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040046
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040047#include <gprs_rlcmac.h>
48#include <pcu_l1_if.h>
49#include <gprs_debug.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040050#include <gprs_bssgp_pcu.h>
Harald Welte68fc1272016-11-16 22:48:33 +010051#include <osmocom/pcu/pcuif_proto.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020052#include <bts.h>
Max6dc90b82018-02-19 17:17:28 +010053#include <pdch.h>
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +020054#include <tbf_ul.h>
55#include <tbf_dl.h>
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010056#include <gprs_ms_storage.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020057
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010058// FIXME: move this, when changed from c++ to c.
59extern "C" {
Max58b66462016-05-13 10:34:15 +020060void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1,
61 struct gsmtap_inst *gsmtap);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010062int l1if_connect_pdch(void *obj, uint8_t ts);
63int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
64 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len);
65}
66
Andreas Eversberge266bd42012-07-13 14:00:21 +020067extern void *tall_pcu_ctx;
68
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010069#define PAGING_GROUP_LEN 3
70
71/* returns [0,999] on success, > 999 on error */
72uint16_t imsi2paging_group(const char* imsi)
73{
74 uint16_t pgroup = 0;
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070075 size_t len;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010076
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070077 len = (imsi != NULL) ? strlen(imsi) : 0;
78 if (len < PAGING_GROUP_LEN)
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010079 return 0xFFFF;
80 imsi += len - PAGING_GROUP_LEN;
81
82 while (*imsi != '\0') {
83 if (!isdigit(*imsi))
84 return 0xFFFF;
85 pgroup *= 10;
86 pgroup += *imsi - '0';
87 imsi++;
88 }
89 return pgroup;
90}
91
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040092/*
93 * PCU messages
94 */
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040095
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +020096/* Can be used to allocate message with non-variable size */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040097struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
98{
99 struct msgb *msg;
100 struct gsm_pcu_if *pcu_prim;
101
102 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
103 if (!msg)
104 return NULL;
105 msgb_put(msg, sizeof(struct gsm_pcu_if));
106 pcu_prim = (struct gsm_pcu_if *) msg->data;
107 pcu_prim->msg_type = msg_type;
108 pcu_prim->bts_nr = bts_nr;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400109
110 return msg;
111}
112
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200113/* Allocate message with extra size, only reserve pcuif msg hdr */
114static struct msgb *pcu_msgb_alloc_ext_size(uint8_t msg_type, uint8_t bts_nr, size_t extra_size)
115{
116 struct msgb *msg;
117 struct gsm_pcu_if *pcu_prim;
118 msg = msgb_alloc(sizeof(struct gsm_pcu_if) + extra_size, "pcu_sock_tx");
119 /* Only header is filled, caller is responible for reserving + filling
120 * message type specific contents: */
121 msgb_put(msg, PCUIF_HDR_SIZE);
122 pcu_prim = (struct gsm_pcu_if *) msgb_data(msg);
123 pcu_prim->msg_type = msg_type;
124 pcu_prim->bts_nr = bts_nr;
125 return msg;
126}
127
Max0a8fae82017-03-08 18:53:30 +0100128const struct value_string gsm_pcu_if_text_type_names[] = {
129 OSMO_VALUE_STRING(PCU_VERSION),
130 OSMO_VALUE_STRING(PCU_OML_ALERT),
131 { 0, NULL }
132};
133
134int pcu_tx_txt_ind(enum gsm_pcu_if_text_type t, const char *fmt, ...)
135{
136 struct gsm_pcu_if *pcu_prim;
137 struct gsm_pcu_if_txt_ind *txt;
138 va_list ap;
139 char *rep;
140 struct msgb *msg = pcu_msgb_alloc(PCU_IF_MSG_TXT_IND, 0);
141 if (!msg)
142 return -ENOMEM;
143
144 pcu_prim = (struct gsm_pcu_if *) msg->data;
145 txt = &pcu_prim->u.txt_ind;
146 txt->type = t;
147
148 va_start(ap, fmt);
149 rep = talloc_vasprintf(tall_pcu_ctx, fmt, ap);
150 va_end(ap);
151
152 if (!rep)
153 return -ENOMEM;
154
155 osmo_strlcpy(txt->text, rep, TXT_MAX_LEN);
156 talloc_free(rep);
157
158 LOGP(DL1IF, LOGL_INFO, "Sending %s TXT as %s to BTS\n", txt->text,
159 get_value_string(gsm_pcu_if_text_type_names, t));
160
161 return pcu_sock_send(msg);
162}
163
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100164static int pcu_tx_act_req(struct gprs_rlcmac_bts *bts, const struct gprs_rlcmac_pdch *pdch,
165 uint8_t activate)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400166{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400167 struct msgb *msg;
168 struct gsm_pcu_if *pcu_prim;
169 struct gsm_pcu_if_act_req *act_req;
170
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100171 LOGPDCH(pdch, DL1IF, LOGL_INFO, "Sending %s request\n",
172 (activate) ? "activate" : "deactivate");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400173
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100174 msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, bts->nr);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400175 if (!msg)
176 return -ENOMEM;
177 pcu_prim = (struct gsm_pcu_if *) msg->data;
178 act_req = &pcu_prim->u.act_req;
179 act_req->activate = activate;
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100180 act_req->trx_nr = pdch->trx_no();
181 act_req->ts_nr = pdch->ts_no;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400182
183 return pcu_sock_send(msg);
184}
185
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100186static 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 +0400187 uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
188 uint8_t len)
189{
190 struct msgb *msg;
191 struct gsm_pcu_if *pcu_prim;
192 struct gsm_pcu_if_data *data_req;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100193 int current_fn = bts_current_frame_number(bts);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400194
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100195 LOGP(DL1IF, LOGL_DEBUG, "(bts=%u,trx=%u,ts=%u) FN=%u Sending data request: sapi=%d "
196 "arfcn=%d cur_fn=%d block=%d data=%s\n", bts->nr, trx, ts, fn, sapi,
197 arfcn, current_fn, block_nr, osmo_hexdump(data, len));
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400198
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100199 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, bts->nr);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400200 if (!msg)
201 return -ENOMEM;
202 pcu_prim = (struct gsm_pcu_if *) msg->data;
203 data_req = &pcu_prim->u.data_req;
204
205 data_req->sapi = sapi;
206 data_req->fn = fn;
207 data_req->arfcn = arfcn;
208 data_req->trx_nr = trx;
209 data_req->ts_nr = ts;
210 data_req->block_nr = block_nr;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200211 if (len)
212 memcpy(data_req->data, data, len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400213 data_req->len = len;
214
215 return pcu_sock_send(msg);
216}
217
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100218void 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 +0400219 uint32_t fn, uint8_t block_nr)
220{
Maxb216c6b2017-09-01 11:00:39 +0200221#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100222 if (bts->trx[trx].fl1h) {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200223 if (!msg) /* Simply skip sending idle frames to L1 */
224 return;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100225 l1if_pdch_req(bts->trx[trx].fl1h, ts, 0, fn, arfcn, block_nr,
226 msg->data, msg->len);
Max528ff392016-02-22 11:42:33 +0100227 msgb_free(msg);
228 return;
229 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100230#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200231 if (!msg) {
232 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
233 NULL, 0);
234 return;
235 }
236
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100237 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100238 msg->data, msg->len);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200239 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400240}
241
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100242void pcu_l1if_tx_ptcch(struct gprs_rlcmac_bts *bts,
243 uint8_t trx, uint8_t ts, uint16_t arfcn,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700244 uint32_t fn, uint8_t block_nr,
245 uint8_t *data, size_t data_len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400246{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100247 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PTCCH))
248 gsmtap_send(the_pcu->gsmtap, arfcn, ts, GSMTAP_CHANNEL_PTCCH, 0, fn, 0, 0, data, data_len);
Maxcad867e2016-04-21 14:35:55 +0200249#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100250 if (bts->trx[trx].fl1h) {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200251 if (!data_len) /* Simply skip sending idle frames to L1 */
252 return;
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700253 l1if_pdch_req(bts->trx[trx].fl1h, ts, 1, fn, arfcn, block_nr, data, data_len);
Max528ff392016-02-22 11:42:33 +0100254 return;
255 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100256#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200257 if (!data_len) {
258 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, NULL, 0);
259 return;
260 }
261
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100262 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 +0400263}
264
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100265void pcu_l1if_tx_agch(struct gprs_rlcmac_bts *bts, bitvec * block, int plen)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400266{
Pau Espin Pedrold752d7c2019-10-07 20:56:48 +0200267 uint8_t data[GSM_MACBLOCK_LEN]; /* prefix PLEN */
268
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400269 /* FIXME: why does OpenBTS has no PLEN and no fill in message? */
270 bitvec_pack(block, data + 1);
271 data[0] = (plen << 2) | 0x01;
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200272
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100273 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_AGCH))
274 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 +0200275
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100276 pcu_tx_data_req(bts, 0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, GSM_MACBLOCK_LEN);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400277}
278
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100279void pcu_l1if_tx_pch(struct gprs_rlcmac_bts *bts, bitvec * block, int plen, uint16_t pgroup)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400280{
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200281 uint8_t data[PAGING_GROUP_LEN + GSM_MACBLOCK_LEN];
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100282 int i;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400283
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200284 /* prepend paging group */
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100285 for (i = 0; i < PAGING_GROUP_LEN; i++) {
286 data[PAGING_GROUP_LEN - 1 - i] = '0' + (char)(pgroup % 10);
287 pgroup = pgroup / 10;
288 }
289 OSMO_ASSERT(pgroup == 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400290
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200291 /* block provided by upper layer comes without first byte (plen),
292 * prepend it manually:
293 */
294 OSMO_ASSERT(sizeof(data) >= PAGING_GROUP_LEN + 1 + block->data_len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400295 data[3] = (plen << 2) | 0x01;
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200296 bitvec_pack(block, data + PAGING_GROUP_LEN + 1);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200297
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100298 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PCH))
299 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 +0200300
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100301 pcu_tx_data_req(bts, 0, 0, PCU_IF_SAPI_PCH, 0, 0, 0, data, PAGING_GROUP_LEN + GSM_MACBLOCK_LEN);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400302}
303
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200304int pcu_tx_neigh_addr_res_req(struct gprs_rlcmac_bts *bts, const struct neigh_cache_entry_key *neigh_key)
305{
306 struct msgb *msg;
307 struct gsm_pcu_if *pcu_prim;
308 struct gsm_pcu_if_neigh_addr_req *naddr_req;
309
310 LOGP(DL1IF, LOGL_DEBUG, "(bts=%u) Tx Neighbor Address Resolution Request: " NEIGH_CACHE_ENTRY_KEY_FMT "\n",
311 bts->nr, NEIGH_CACHE_ENTRY_KEY_ARGS(neigh_key));
312
313 msg = pcu_msgb_alloc_ext_size(PCU_IF_MSG_CONTAINER, bts->nr, sizeof(struct gsm_pcu_if_neigh_addr_req));
314 if (!msg)
315 return -ENOMEM;
316 pcu_prim = (struct gsm_pcu_if *) msgb_data(msg);
317 naddr_req = (struct gsm_pcu_if_neigh_addr_req *)&pcu_prim->u.container.data[0];
318
319 msgb_put(msg, sizeof(pcu_prim->u.container) + sizeof(struct gsm_pcu_if_neigh_addr_req));
320 pcu_prim->u.container.msg_type = PCU_IF_MSG_NEIGH_ADDR_REQ;
321 osmo_store16be(sizeof(struct gsm_pcu_if_neigh_addr_req), &pcu_prim->u.container.length);
322
323 osmo_store16be(neigh_key->local_lac, &naddr_req->local_lac);
324 osmo_store16be(neigh_key->local_ci, &naddr_req->local_ci);
325 osmo_store16be(neigh_key->tgt_arfcn, &naddr_req->tgt_arfcn);
326 naddr_req->tgt_bsic = neigh_key->tgt_bsic;
327
328 return pcu_sock_send(msg);
329}
330
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100331void 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 +0200332{
Pau Espin Pedrolfecab502021-03-17 15:26:37 +0100333 bts_set_current_block_frame_number(bts, fn);
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200334}
335
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100336int 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 +0200337 uint8_t len, uint32_t fn, struct pcu_l1_meas *meas)
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100338{
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100339 int rc;
340
Pau Espin Pedrol3bbb3cc2021-08-31 14:09:42 +0200341 if (!pdch->is_enabled()) {
342 LOGPDCH(pdch, DL1IF, LOGL_INFO, "Received DATA.ind (PDTCH) on disabled TS\n");
343 return -EINVAL;
344 }
345
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100346 rc = pdch->rcv_block(data, len, fn, meas);
347 pdch_ulc_expire_fn(pdch->ulc, fn);
348 return rc;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100349}
350
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200351static int list_arfcn(const struct gprs_rlcmac_bts *bts, const struct gsm_sysinfo_freq *freq, const char *text)
352{
353 int n = 0, i;
354 for (i = 0; i < 1024; i++) {
355 if (freq[i].mask) {
356 if (!n)
357 LOGP(DL1IF, LOGL_INFO, "BTS%d: %s", bts->nr, text);
358 LOGPC(DL1IF, LOGL_INFO, " %d", i);
359 n++;
360 }
361 }
362 if (n)
363 LOGPC(DL1IF, LOGL_INFO, "\n");
364
365 return n;
366}
367
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100368static int pcu_rx_data_ind_bcch(struct gprs_rlcmac_bts *bts, uint8_t *data, uint8_t len)
Max84bf0fa2017-09-01 11:02:40 +0200369{
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200370 struct gsm48_system_information_type_2 *si2;
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100371 const uint8_t *si_ro;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200372
Philipp Maiera58ec612021-01-25 23:43:52 +0100373 switch (len) {
374 case 0:
375 /* Due to historical reasons also accept a completely empty message as
376 * revoke command for SI13. */
377 LOGP(DL1IF, LOGL_ERROR,
378 "Received PCU data indication that contains no data -- Revoked SI13.\n");
Max84bf0fa2017-09-01 11:02:40 +0200379 bts->si13_is_set = false;
Max84bf0fa2017-09-01 11:02:40 +0200380
Philipp Maiera58ec612021-01-25 23:43:52 +0100381 return 0;
382 case 1:
383 /* Revoke SI, type is identified by a single byte which is coded after
384 * enum osmo_sysinfo_type. */
385 switch (data[0]) {
386 case SYSINFO_TYPE_1:
387 bts->si1_is_set = false;
388 break;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200389 case SYSINFO_TYPE_2:
390 bts->si2_is_set = false;
391 break;
Philipp Maiera58ec612021-01-25 23:43:52 +0100392 case SYSINFO_TYPE_3:
393 bts->si3_is_set = false;
394 break;
395 case SYSINFO_TYPE_13:
396 bts->si13_is_set = false;
397 break;
398 default:
399 LOGP(DL1IF, LOGL_ERROR,
400 "Received PCU data indication that contains an unsupported system information identifier (%02x,OSMO) -- ignored.\n", data[0]);
401 return -EINVAL;
402 }
403 LOGP(DPCU, LOGL_DEBUG,
404 "Received PCU data indication: Revoked SI%s\n",
405 get_value_string(osmo_sitype_strs, data[0]));
406 return 0;
407 case GSM_MACBLOCK_LEN:
408 /* Update SI, type is identified by the RR sysinfo type, which is the
409 * 3rd byte in the buffer. */
410 switch (data[2]) {
411 case GSM48_MT_RR_SYSINFO_1:
412 memcpy(bts->si1, data, GSM_MACBLOCK_LEN);
413 bts->si1_is_set = true;
414 break;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200415 case GSM48_MT_RR_SYSINFO_2:
416 memcpy(bts->si2, data, GSM_MACBLOCK_LEN);
417 bts->si2_is_set = true;
418 si2 = (struct gsm48_system_information_type_2 *)bts->si2;
419 gsm48_decode_freq_list(bts->si2_bcch_cell_list, si2->bcch_frequency_list,
420 sizeof(si2->bcch_frequency_list), 0xce, 1);
421 list_arfcn(bts, bts->si2_bcch_cell_list, "SI2 Neighbour cells in same band:");
422 break;
Philipp Maiera58ec612021-01-25 23:43:52 +0100423 case GSM48_MT_RR_SYSINFO_3:
424 memcpy(bts->si3, data, GSM_MACBLOCK_LEN);
425 bts->si3_is_set = true;
426 break;
427 case GSM48_MT_RR_SYSINFO_13:
428 memcpy(bts->si13, data, GSM_MACBLOCK_LEN);
429 bts->si13_is_set = true;
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100430 si_ro = ((struct gsm48_system_information_type_13*)data)->rest_octets;
Pau Espin Pedrold65bd9d2021-06-01 16:43:41 +0200431 if (osmo_gsm48_rest_octets_si13_decode(&bts->si13_ro_decoded, si_ro) < 0)
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100432 LOGP(DPCU, LOGL_ERROR, "Error decoding SI13\n");
Philipp Maiera58ec612021-01-25 23:43:52 +0100433 break;
434 default:
435 LOGP(DL1IF, LOGL_ERROR,
436 "Received PCU data indication that contains an unsupported system information identifier (%02x,RR) -- ignored.\n", data[2]);
437 return -EINVAL;
438 }
439 LOGP(DPCU, LOGL_DEBUG,
440 "Received PCU data indication: Updated %s: %s\n",
441 gsm48_pdisc_msgtype_name(data[1], data[2]),
442 osmo_hexdump_nospc(data + 1, GSM_MACBLOCK_LEN));
443 return 0;
444 default:
445 LOGP(DL1IF, LOGL_ERROR,
446 "Received PCU data indication with unexpected data length: %u -- ignored.\n",
447 len);
Max84bf0fa2017-09-01 11:02:40 +0200448 return -EINVAL;
449 }
Max84bf0fa2017-09-01 11:02:40 +0200450}
451
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100452static int pcu_rx_data_ind(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400453{
Max528ff392016-02-22 11:42:33 +0100454 int rc;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100455 int current_fn = bts_current_frame_number(bts);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100456 struct pcu_l1_meas meas = {0};
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100457 struct gprs_rlcmac_pdch *pdch;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200458 uint8_t gsmtap_chantype;
Maxb216c6b2017-09-01 11:00:39 +0200459
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100460 LOGP(DL1IF, LOGL_DEBUG, "(bts=%" PRIu8 ",trx=%" PRIu8 ",ts=%" PRIu8 ") FN=%u "
461 "Rx DATA.ind: sapi=%d arfcn=%d cur_fn=%d "
462 "block=%d data=%s\n", bts->nr, data_ind->trx_nr, data_ind->ts_nr,
463 data_ind->fn, data_ind->sapi, data_ind->arfcn, current_fn,
464 data_ind->block_nr, osmo_hexdump(data_ind->data, data_ind->len));
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400465
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400466 switch (data_ind->sapi) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400467 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100468 pdch = &bts->trx[data_ind->trx_nr].pdch[data_ind->ts_nr];
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100469 pcu_l1_meas_set_rssi(&meas, data_ind->rssi);
Maxb216c6b2017-09-01 11:00:39 +0200470 /* convert BER to % value */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100471 pcu_l1_meas_set_ber(&meas, data_ind->ber10k / 100);
472 pcu_l1_meas_set_bto(&meas, data_ind->ta_offs_qbits);
473 pcu_l1_meas_set_link_qual(&meas, data_ind->lqual_cb / 10);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700474
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100475 LOGPDCH(pdch, DL1IF, LOGL_DEBUG, "FN=%u Rx DATA.ind PDTCH: "
476 "BER10k = %d, BTO = %d, Q = %d\n", data_ind->fn,
477 data_ind->ber10k, data_ind->ta_offs_qbits, data_ind->lqual_cb);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700478
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100479 rc = pcu_rx_data_ind_pdtch(bts, pdch, data_ind->data, data_ind->len,
480 data_ind->fn, &meas);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200481 gsmtap_chantype = GSMTAP_CHANNEL_PDTCH;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400482 break;
Max84bf0fa2017-09-01 11:02:40 +0200483 case PCU_IF_SAPI_BCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100484 rc = pcu_rx_data_ind_bcch(bts, data_ind->data, data_ind->len);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200485 gsmtap_chantype = GSMTAP_CHANNEL_BCCH;
Max84bf0fa2017-09-01 11:02:40 +0200486 break;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400487 default:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100488 LOGP(DL1IF, LOGL_ERROR, "(bts=%" PRIu8 ",trx=%" PRIu8 ",ts=%" PRIu8 ") "
489 "FN=%u Rx DATA.ind with unsupported sapi %d\n",
490 bts->nr, data_ind->trx_nr, data_ind->ts_nr, data_ind->fn, data_ind->sapi);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400491 rc = -EINVAL;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200492 gsmtap_chantype = GSMTAP_CHANNEL_UNKNOWN;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400493 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400494
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100495 if (rc < 0 && (the_pcu->gsmtap_categ_mask & (1 <<PCU_GSMTAP_C_UL_UNKNOWN))) {
496 gsmtap_send(the_pcu->gsmtap, data_ind->arfcn | GSMTAP_ARFCN_F_UPLINK, data_ind->ts_nr,
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200497 gsmtap_chantype, 0, data_ind->fn, meas.rssi, meas.link_qual, data_ind->data, data_ind->len);
Harald Welte717cdf52017-07-21 21:56:23 +0200498 }
499
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400500 return rc;
501}
502
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100503static int pcu_rx_data_cnf(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data *data_cnf)
Andreas Eversberga9be1542012-09-27 09:23:24 +0200504{
505 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100506 int current_fn = bts_current_frame_number(bts);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200507
Philipp Maier7e8e3972018-04-10 13:31:45 +0200508 LOGP(DL1IF, LOGL_DEBUG, "Data confirm received: sapi=%d fn=%d cur_fn=%d\n",
509 data_cnf->sapi, data_cnf->fn, current_fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200510
511 switch (data_cnf->sapi) {
512 case PCU_IF_SAPI_PCH:
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200513 if (data_cnf->data[2] == 0x3f)
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100514 bts_rcv_imm_ass_cnf(bts, data_cnf->data, data_cnf->fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200515 break;
516 default:
517 LOGP(DL1IF, LOGL_ERROR, "Received PCU data confirm with "
518 "unsupported sapi %d\n", data_cnf->sapi);
519 rc = -EINVAL;
520 }
521
522 return rc;
523}
524
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100525// FIXME: remove this, when changed from c++ to c.
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100526int pcu_rx_rts_req_pdtch(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100527 uint32_t fn, uint8_t block_nr)
528{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100529 return gprs_rlcmac_rcv_rts_block(bts,
Max878bd1f2016-07-20 13:05:05 +0200530 trx, ts, fn, block_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100531}
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100532int pcu_rx_rts_req_ptcch(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700533 uint32_t fn, uint8_t block_nr)
534{
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700535 struct gprs_rlcmac_pdch *pdch;
536
537 /* Prevent buffer overflow */
538 if (trx >= ARRAY_SIZE(bts->trx) || ts >= 8)
539 return -EINVAL;
540
541 /* Make sure PDCH time-slot is enabled */
542 pdch = &bts->trx[trx].pdch[ts];
Pau Espin Pedrol9c2512a2022-04-01 18:27:04 +0200543 if (!pdch_is_enabled(pdch))
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700544 return -EAGAIN;
545
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200546 /* If there's no TBF attached to this PDCH, we can skip Tx of PTCCH
547 * since there's nothing worthy of being transmitted. This way BTS can
548 * identify idle blocks and send nothing or dumy blocks with reduced
549 * energy for the sake of energy saving.
550 */
551 const unsigned num_tbfs = pdch->num_tbfs(GPRS_RLCMAC_DL_TBF)
552 + pdch->num_tbfs(GPRS_RLCMAC_UL_TBF);
553 bool skip_idle = (num_tbfs == 0);
554#ifdef ENABLE_DIRECT_PHY
555 /* In DIRECT_PHY mode we want to always submit something to L1 in
556 * TRX0, since BTS is not preparing dummy bursts on idle TS for us: */
557 skip_idle = skip_idle && trx != 0;
558#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200559 if (skip_idle) {
560 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
561 NULL, 0);
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200562 return 0;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200563 }
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200564
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100565 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700566 pdch->ptcch_msg, GSM_MACBLOCK_LEN);
567 return 0;
568}
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100569
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100570static 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 +0400571{
572 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100573 int current_fn = bts_current_frame_number(bts);
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100574 const struct gprs_rlcmac_pdch *pdch;
575 pdch = &bts->trx[rts_req->trx_nr].pdch[rts_req->ts_nr];
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400576
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100577 LOGPDCH(pdch, DL1IF, LOGL_DEBUG, "FN=%u RX RTS.req: sapi=%d "
578 "arfcn=%d cur_fn=%d block=%d\n", rts_req->fn,
579 rts_req->sapi, rts_req->arfcn, current_fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400580
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400581 switch (rts_req->sapi) {
582 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100583 pcu_rx_rts_req_pdtch(bts, rts_req->trx_nr, rts_req->ts_nr,
Max878bd1f2016-07-20 13:05:05 +0200584 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400585 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400586 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100587 pcu_rx_rts_req_ptcch(bts, rts_req->trx_nr, rts_req->ts_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700588 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400589 break;
590 default:
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100591 LOGP(DL1IF, LOGL_ERROR, "(bts=%u,trx=%u,ts=%u) FN=%u RX RTS.req with "
592 "unsupported sapi %d\n", bts->nr, rts_req->trx_nr, rts_req->ts_nr,
593 rts_req->fn, rts_req->sapi);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400594 rc = -EINVAL;
595 }
596
597 return rc;
598}
599
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700600/* C -> C++ adapter for direct DSP access code (e.g. osmo-bts-sysmo) */
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100601extern "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 +0700602{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700603 struct rach_ind_params rip = {
604 /* The content of RA is not of interest on PTCCH/U */
605 .burst_type = GSM_L1_BURST_TYPE_ACCESS_0,
606 .is_11bit = false,
607 .ra = 0x00,
608 .trx_nr = trx_nr,
609 .ts_nr = ts_nr,
610 .rfn = fn,
611 .qta = qta,
612 };
613
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100614 return bts_rcv_ptcch_rach(bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700615}
616
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100617static 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 +0400618{
619 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100620 int current_fn = bts_current_frame_number(bts);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400621
622 LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
Vadim Yanitskiyb30f28f2020-03-29 19:01:58 +0700623 "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 +0200624 rach_ind->ra, rach_ind->fn, current_fn, rach_ind->is_11bit);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400625
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700626 struct rach_ind_params rip = {
627 .burst_type = (enum ph_burst_type) rach_ind->burst_type,
628 .is_11bit = rach_ind->is_11bit > 0,
629 .ra = rach_ind->ra,
630 .trx_nr = rach_ind->trx_nr,
631 .ts_nr = rach_ind->ts_nr,
632 .rfn = rach_ind->fn,
633 .qta = rach_ind->qta,
634 };
635
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400636 switch (rach_ind->sapi) {
637 case PCU_IF_SAPI_RACH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100638 rc = bts_rcv_rach(bts, &rip);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400639 break;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700640 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100641 rc = bts_rcv_ptcch_rach(bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700642 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400643 default:
644 LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
645 "unsupported sapi %d\n", rach_ind->sapi);
646 rc = -EINVAL;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400647 }
648
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400649 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400650}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200651
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200652static int pcu_info_ind_ns(struct gprs_rlcmac_bts *bts,
653 const struct gsm_pcu_if_info_ind *info_ind)
654{
655 struct osmo_sockaddr remote[PCU_IF_NUM_NSVC] = { };
656 struct osmo_sockaddr local[PCU_IF_NUM_NSVC] = { };
657 uint16_t nsvci[PCU_IF_NUM_NSVC] = { };
658 uint16_t valid = 0;
659
660 for (unsigned int i = 0; i < PCU_IF_NUM_NSVC; i++) {
661 struct osmo_sockaddr_str sockstr;
662
663 switch (info_ind->address_type[i]) {
664 case PCU_IF_ADDR_TYPE_IPV4:
665 local[i].u.sin.sin_family = AF_INET;
666 local[i].u.sin.sin_addr.s_addr = INADDR_ANY;
667 local[i].u.sin.sin_port = htons(info_ind->local_port[i]);
668
669 remote[i].u.sin.sin_family = AF_INET;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100670 memcpy(&remote[i].u.sin.sin_addr, &info_ind->remote_ip[i].v4,
671 sizeof(struct in_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200672 remote[i].u.sin.sin_port = htons(info_ind->remote_port[i]);
673 break;
674 case PCU_IF_ADDR_TYPE_IPV6:
675 local[i].u.sin6.sin6_family = AF_INET6;
676 local[i].u.sin6.sin6_addr = in6addr_any;
677 local[i].u.sin6.sin6_port = htons(info_ind->local_port[i]);
678
679 remote[i].u.sin6.sin6_family = AF_INET6;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100680 memcpy(&remote[i].u.sin6.sin6_addr,
681 &info_ind->remote_ip[i].v6,
682 sizeof(struct in6_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200683 remote[i].u.sin6.sin6_port = htons(info_ind->remote_port[i]);
684 break;
685 default:
686 continue;
687 }
688 nsvci[i] = info_ind->nsvci[i];
689
690 LOGP(DL1IF, LOGL_DEBUG, " NS%u nsvci=%u\n", i, nsvci[i]);
691 if (osmo_sockaddr_str_from_sockaddr(&sockstr, &remote[i].u.sas))
692 strcpy(sockstr.ip, "invalid");
693
694 LOGP(DL1IF, LOGL_DEBUG, " NS%u address: r=%s:%u<->l=NULL:%u\n",
695 i, sockstr.ip, sockstr.port, info_ind->local_port[i]);
696
697 valid |= 1 << i;
698 }
699
700 if (valid == 0) {
701 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
702 return -EINVAL;
703 }
704
Alexander Couzens94a367f2021-02-16 16:57:15 +0100705 return gprs_ns_update_config(bts, info_ind->nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200706}
707
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100708static 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 +0200709{
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +0200710 struct gprs_bssgp_pcu *pcu;
Harald Weltebb47d952016-11-17 18:40:02 +0100711 int rc = 0;
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700712 unsigned int trx_nr, ts_nr;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100713 unsigned int i;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200714
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200715 if (info_ind->version != PCU_IF_VERSION) {
Vadim Yanitskiyfad21282020-07-19 02:05:33 +0700716 fprintf(stderr, "PCU interface version number of BTS (%u) is "
717 "different (%u).\nPlease re-compile!\n",
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200718 info_ind->version, PCU_IF_VERSION);
719 exit(-1);
720 }
721
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400722 LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200723
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400724 if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
725 LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100726 if (!bts->active)
727 return -EAGAIN;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400728bssgp_failed:
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100729 bts->active = false;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400730 /* free all TBF */
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700731 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
732 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
733 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++)
Pau Espin Pedrol2d92e392022-03-30 22:09:06 +0200734 if (bts->trx[trx_nr].pdch[ts_nr].is_enabled())
735 bts->trx[trx_nr].pdch[ts_nr].disable();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400736 }
Alexander Couzens290d9032020-09-16 21:52:02 +0200737 gprs_bssgp_destroy(bts);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200738 exit(0);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200739 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400740 LOGP(DL1IF, LOGL_INFO, "BTS available\n");
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100741 LOGP(DL1IF, LOGL_DEBUG, " mcc=%03u\n", info_ind->mcc);
742 LOGP(DL1IF, LOGL_DEBUG, " mnc=%0*u\n", info_ind->mnc_3_digits, info_ind->mnc);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400743 LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
744 LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
Harald Welte1473b372019-03-21 19:48:55 +0100745 LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100746 LOGP(DL1IF, LOGL_DEBUG, " bsic=%d\n", info_ind->bsic);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400747 LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
748 LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
749 info_ind->nse_timer[0], info_ind->nse_timer[1],
750 info_ind->nse_timer[2], info_ind->nse_timer[3],
751 info_ind->nse_timer[4], info_ind->nse_timer[5],
752 info_ind->nse_timer[6]);
753 LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
754 "%d\n",
755 info_ind->cell_timer[0], info_ind->cell_timer[1],
756 info_ind->cell_timer[2], info_ind->cell_timer[3],
757 info_ind->cell_timer[4], info_ind->cell_timer[5],
758 info_ind->cell_timer[6], info_ind->cell_timer[7],
759 info_ind->cell_timer[8], info_ind->cell_timer[9],
760 info_ind->cell_timer[10]);
761 LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
762 LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
763 LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
764 LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
765 LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
766 LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
767 LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
768 LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
769 LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
770 LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
771 LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
772 LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
773 LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
774 LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
Pau Espin Pedrol2e6b60d2021-01-20 13:48:31 +0100775 bts->cgi_ps.rai.lac.plmn.mcc = info_ind->mcc;
776 bts->cgi_ps.rai.lac.plmn.mnc = info_ind->mnc;
777 bts->cgi_ps.rai.lac.plmn.mnc_3_digits = info_ind->mnc_3_digits;
778 bts->cgi_ps.rai.lac.lac = info_ind->lac;
779 bts->cgi_ps.rai.rac = info_ind->rac;
780 bts->cgi_ps.cell_identity = info_ind->cell_id;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100781 bts->bsic = info_ind->bsic;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100782
783 bts->cs_mask = 1 << 0; /* We need at least 1 CS, let's enable CS1 */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400784 for (i = 0; i < 4; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100785 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_CS1 << i));
786 bts->cs_mask |= allowed << i;
787 if (allowed)
788 LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i + 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400789 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100790 bts_recalc_max_cs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100791
792 bts->mcs_mask = 0;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400793 for (i = 0; i < 9; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100794 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_MCS1 << i));
795 bts->mcs_mask |= allowed << i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100796 if (allowed)
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100797 LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i + 1);
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100798
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400799 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100800 bts_recalc_max_mcs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100801
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100802 LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%u%s\n", info_ind->initial_cs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100803 the_pcu->vty.force_initial_cs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100804 bts->pcuif_info_ind.initial_cs = info_ind->initial_cs;
805 bts_recalc_initial_cs(bts);
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100806
807 LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%u%s\n", info_ind->initial_mcs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100808 the_pcu->vty.force_initial_mcs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100809 bts->pcuif_info_ind.initial_mcs = info_ind->initial_mcs;
810 bts_recalc_initial_mcs(bts);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200811
Alexander Couzens290d9032020-09-16 21:52:02 +0200812 pcu = gprs_bssgp_init(
813 bts,
814 info_ind->nsei, info_ind->bvci,
815 info_ind->mcc, info_ind->mnc, info_ind->mnc_3_digits,
816 info_ind->lac, info_ind->rac, info_ind->cell_id);
817 if (!pcu) {
Vadim Yanitskiyc1a726c2020-10-08 22:46:26 +0700818 LOGP(DL1IF, LOGL_ERROR, "Failed to init BSSGP\n");
Alexander Couzens9b5c9602020-07-28 13:47:18 +0200819 goto bssgp_failed;
820 }
821
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200822 rc = pcu_info_ind_ns(pcu->bts, info_ind);
823 if (rc < 0) {
Alexander Couzens290d9032020-09-16 21:52:02 +0200824 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400825 goto bssgp_failed;
826 }
827
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400828 if (info_ind->t3142) { /* if timer values are set */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200829 osmo_tdef_set(bts->T_defs_bts, 3142, info_ind->t3142, OSMO_TDEF_S);
830 osmo_tdef_set(bts->T_defs_bts, 3169, info_ind->t3169, OSMO_TDEF_S);
831 osmo_tdef_set(bts->T_defs_bts, 3191, info_ind->t3191, OSMO_TDEF_S);
832 osmo_tdef_set(bts->T_defs_bts, 3193, info_ind->t3193_10ms * 10, OSMO_TDEF_MS);
833 osmo_tdef_set(bts->T_defs_bts, 3195, info_ind->t3195, OSMO_TDEF_S);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400834 bts->n3101 = info_ind->n3101;
835 bts->n3103 = info_ind->n3103;
836 bts->n3105 = info_ind->n3105;
837 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400838
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700839 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
840 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100841 if ((info_ind->flags & PCU_IF_FLAG_SYSMO)
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700842 && info_ind->trx[trx_nr].hlayer1) {
Maxcad867e2016-04-21 14:35:55 +0200843#ifdef ENABLE_DIRECT_PHY
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700844 LOGP(DL1IF, LOGL_DEBUG, " TRX %d hlayer1=%x\n", trx_nr,
845 info_ind->trx[trx_nr].hlayer1);
846 if (!bts->trx[trx_nr].fl1h)
847 bts->trx[trx_nr].fl1h = l1if_open_pdch(
848 trx_nr,
849 info_ind->trx[trx_nr].hlayer1,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100850 the_pcu->gsmtap);
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700851 if (!bts->trx[trx_nr].fl1h) {
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100852 LOGP(DL1IF, LOGL_FATAL, "Failed to open direct "
853 "DSP access for PDCH.\n");
854 exit(0);
855 }
856#else
857 LOGP(DL1IF, LOGL_FATAL, "Compiled without direct DSP "
858 "access for PDCH, but enabled at "
859 "BTS. Please deactivate it!\n");
860 exit(0);
861#endif
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100862 }
863
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700864 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++) {
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700865 const struct gsm_pcu_if_info_ts *its = &info_ind->trx[trx_nr].ts[ts_nr];
Vadim Yanitskiy0f41b712020-07-24 01:28:13 +0700866 struct gprs_rlcmac_pdch *pdch = &bts->trx[trx_nr].pdch[ts_nr];
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700867 if ((info_ind->trx[trx_nr].pdch_mask & (1 << ts_nr))) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400868 /* FIXME: activate dynamically at RLCMAC */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200869 if (!pdch->is_enabled()) {
Maxcad867e2016-04-21 14:35:55 +0200870#ifdef ENABLE_DIRECT_PHY
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100871 if ((info_ind->flags &
872 PCU_IF_FLAG_SYSMO))
873 l1if_connect_pdch(
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700874 bts->trx[trx_nr].fl1h, ts_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100875#endif
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100876 pcu_tx_act_req(bts, pdch, 1);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200877 pdch->enable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200878 }
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700879
880 pdch->tsc = its->tsc;
881
882 /* (Optional) frequency hopping parameters */
883 if (its->h) {
884 pdch->fh.enabled = true;
885 pdch->fh.maio = its->maio;
886 pdch->fh.hsn = its->hsn;
887
888 OSMO_ASSERT(its->ma_bit_len <= sizeof(pdch->fh.ma) * 8);
889 pdch->fh.ma_oct_len = OSMO_BYTES_FOR_BITS(its->ma_bit_len);
890 pdch->fh.ma_bit_len = its->ma_bit_len;
891
892 /* Mobile Allocation + padding (byte/bit order as on the wire):
893 * | 00 00 00 00 00 cc bb aa | -> | cc bb aa 00 00 00 00 00 | */
894 unsigned int offset = sizeof(pdch->fh.ma) - pdch->fh.ma_oct_len;
895 memcpy(pdch->fh.ma, its->ma + offset, pdch->fh.ma_oct_len);
896 }
897
898 LOGP(DL1IF, LOGL_INFO, "PDCH (trx=%u, ts=%u): tsc=%u, hopping=%s\n",
899 trx_nr, ts_nr, pdch->tsc, pdch->fh.enabled ? "yes" : "no");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400900 } else {
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200901 if (pdch->is_enabled()) {
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100902 pcu_tx_act_req(bts, pdch, 0);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200903 pdch->disable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200904 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400905 }
906 }
907 }
908
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100909 bts->active = true;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200910 return rc;
911}
912
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100913static 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 +0200914{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400915 uint8_t fn13 = time_ind->fn % 13;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200916
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400917 /* omit frame numbers not starting at a MAC block */
918 if (fn13 != 0 && fn13 != 4 && fn13 != 8)
919 return 0;
Max3741f142017-06-26 12:10:12 +0200920
921 LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n", time_ind->fn % 52);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400922
Pau Espin Pedrol30617112021-03-05 18:59:05 +0100923 /* Ignore TIME.ind completely, we nowadays relay on DATA.ind always
924 * providing all block FNs. */
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200925 return 0;
926}
927
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100928static 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 +0200929{
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200930 struct osmo_mobile_identity mi;
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200931 struct GprsMs *ms = NULL;
932 struct paging_req_cs req = { .chan_needed = pag_req->chan_needed,
933 .tlli = GSM_RESERVED_TMSI };
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200934 int rc;
935
Andreas Eversberg2b914642012-07-19 13:06:26 +0200936 LOGP(DL1IF, LOGL_DEBUG, "Paging request received: chan_needed=%d "
937 "length=%d\n", pag_req->chan_needed, pag_req->identity_lv[0]);
938
Pau Espin Pedrol32499b62019-12-09 13:55:12 +0100939 /* check if identity does not fit: length > sizeof(lv) - 1 */
940 if (pag_req->identity_lv[0] >= sizeof(pag_req->identity_lv)) {
941 LOGP(DL1IF, LOGL_ERROR, "Paging identity too large (%" PRIu8 ")\n",
942 pag_req->identity_lv[0]);
943 return -EINVAL;
944 }
945
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200946 rc = osmo_mobile_identity_decode(&mi, &pag_req->identity_lv[1], pag_req->identity_lv[0], true);
947 if (rc < 0) {
948 LOGP(DL1IF, LOGL_ERROR, "Failed to decode Mobile Identity in Paging Request (rc=%d)\n", rc);
949 return -EINVAL;
950 }
951
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200952 switch (mi.type) {
953 case GSM_MI_TYPE_TMSI:
954 req.mi_tmsi = mi;
955 req.mi_tmsi_present = true;
956 /* TODO: look up MS by TMSI? Derive TLLI? */
957 break;
958 case GSM_MI_TYPE_IMSI:
959 req.mi_imsi = mi;
960 req.mi_imsi_present = true;
961 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
962 break;
963 default:
964 LOGP(DL1IF, LOGL_ERROR, "Unexpected MI type %u\n", mi.type);
965 return -EINVAL;
966 }
967
968 return bts_add_paging(bts, &req, ms);
Andreas Eversberg2b914642012-07-19 13:06:26 +0200969}
970
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100971static 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 +0200972{
Philipp Maiera58ec612021-01-25 23:43:52 +0100973 struct bssgp_bvc_ctx *bctx = the_pcu->bssgp.bctx;
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200974 GprsMs *ms;
975 struct gprs_rlcmac_dl_tbf *dl_tbf;
976 struct gprs_rlcmac_ul_tbf *ul_tbf;
Harald Welte3447c4a2017-07-11 00:06:38 +0200977 struct gprs_ra_id ra_id;
978
979 gsm48_parse_ra(&ra_id, susp_req->ra_id);
980
981 LOGP(DL1IF, LOGL_INFO, "GPRS Suspend request received: TLLI=0x%08x RAI=%s\n",
982 susp_req->tlli, osmo_rai_name(&ra_id));
983
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100984 if ((ms = bts_ms_store(bts)->get_ms(susp_req->tlli))) {
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200985 /* We need to catch both pointers here since MS may become freed
986 after first tbf_free(dl_tbf) if only DL TBF was available */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100987 dl_tbf = ms_dl_tbf(ms);
988 ul_tbf = ms_ul_tbf(ms);
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200989 if (dl_tbf)
990 tbf_free(dl_tbf);
991 if (ul_tbf)
992 tbf_free(ul_tbf);
993 }
994
Harald Welte3447c4a2017-07-11 00:06:38 +0200995 if (!bctx)
996 return -1;
997
998 return bssgp_tx_suspend(bctx->nsei, susp_req->tlli, &ra_id);
999}
1000
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001001static 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 +02001002{
Pau Espin Pedrol54faf022021-01-14 12:12:43 +01001003 struct llist_head *tmp;
Oliver Smithcfb63212019-09-05 17:13:33 +02001004
1005 LOGP(DL1IF, LOGL_DEBUG, "Application Information Request received: type=0x%08x len=%i\n",
1006 app_info_req->application_type, app_info_req->len);
1007
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001008 bts->app_info_pending = 0;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001009 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
Pau Espin Pedrol54faf022021-01-14 12:12:43 +01001010 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001011 if (!ms_dl_tbf(ms))
Oliver Smithcfb63212019-09-05 17:13:33 +02001012 continue;
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001013 bts->app_info_pending++;
Oliver Smithcfb63212019-09-05 17:13:33 +02001014 ms->app_info_pending = true;
1015 }
1016
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001017 if (!bts->app_info_pending) {
Oliver Smithcfb63212019-09-05 17:13:33 +02001018 LOGP(DL1IF, LOGL_NOTICE, "Packet Application Information will not be sent, no subscribers with active"
1019 " TBF\n");
1020 return -1;
1021 }
1022
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001023 if (bts->app_info) {
Oliver Smithcfb63212019-09-05 17:13:33 +02001024 LOGP(DL1IF, LOGL_NOTICE, "Previous Packet Application Information was not sent to all subscribers,"
1025 " overwriting with new one\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001026 msgb_free(bts->app_info);
Oliver Smithcfb63212019-09-05 17:13:33 +02001027 }
1028
1029 LOGP(DL1IF, LOGL_INFO, "Sending Packet Application Information to %i subscribers with active TBF\n",
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001030 bts->app_info_pending);
1031 bts->app_info = gprs_rlcmac_app_info_msg(app_info_req);
Oliver Smithcfb63212019-09-05 17:13:33 +02001032 return 0;
1033}
1034
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001035static int pcu_rx_neigh_addr_cnf(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_neigh_addr_cnf *naddr_cnf)
1036{
1037 struct llist_head *tmp;
1038 struct osmo_cell_global_id_ps cgi_ps;
1039 struct osmo_cell_global_id_ps *cgi_ps_ptr = &cgi_ps;
1040
1041 struct neigh_cache_entry_key neigh_key = {
1042 .local_lac = osmo_load16be(&naddr_cnf->orig_req.local_lac),
1043 .local_ci = osmo_load16be(&naddr_cnf->orig_req.local_ci),
1044 .tgt_arfcn = osmo_load16be(&naddr_cnf->orig_req.tgt_arfcn),
1045 .tgt_bsic = naddr_cnf->orig_req.tgt_bsic,
1046 };
1047
1048 if (naddr_cnf->err_code == 0) {
1049 cgi_ps.rai.lac.plmn.mcc = osmo_load16be(&naddr_cnf->cgi_ps.mcc);
1050 cgi_ps.rai.lac.plmn.mnc = osmo_load16be(&naddr_cnf->cgi_ps.mnc);
1051 cgi_ps.rai.lac.plmn.mnc_3_digits = naddr_cnf->cgi_ps.mnc_3_digits;
1052 cgi_ps.rai.lac.lac = osmo_load16be(&naddr_cnf->cgi_ps.lac);
1053 cgi_ps.rai.rac = naddr_cnf->cgi_ps.rac;
1054 cgi_ps.cell_identity = osmo_load16be(&naddr_cnf->cgi_ps.cell_identity);
1055
1056 LOGP(DL1IF, LOGL_INFO, "Rx Neighbor Address Resolution Confirmation for " NEIGH_CACHE_ENTRY_KEY_FMT ": %s\n",
1057 NEIGH_CACHE_ENTRY_KEY_ARGS(&neigh_key), osmo_cgi_ps_name(&cgi_ps));
1058
1059 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
1060 neigh_cache_add(bts->pcu->neigh_cache, &neigh_key, &cgi_ps);
1061 } else {
1062 cgi_ps_ptr = NULL;
1063 LOGP(DL1IF, LOGL_INFO, "Rx Neighbor Address Resolution Confirmation for " NEIGH_CACHE_ENTRY_KEY_FMT ": failed with err_code=%u\n",
1064 NEIGH_CACHE_ENTRY_KEY_ARGS(&neigh_key), naddr_cnf->err_code);
1065 }
1066
1067 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
1068 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
1069 if (ms->nacc && nacc_fsm_is_waiting_addr_resolution(ms->nacc, &neigh_key))
1070 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_RAC_CI, cgi_ps_ptr);
1071 }
1072 return 0;
1073}
1074
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001075static int pcu_rx_container(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_container *container)
1076{
1077 int rc;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001078 uint16_t data_length = osmo_load16be(&container->length);
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001079
1080 switch (container->msg_type) {
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001081 case PCU_IF_MSG_NEIGH_ADDR_CNF:
1082 if (data_length < sizeof(struct gsm_pcu_if_neigh_addr_cnf)) {
1083 LOGP(DL1IF, LOGL_ERROR, "Rx container(NEIGH_ADDR_CNF) message too short: %u vs exp %zu\n",
1084 data_length, sizeof(struct gsm_pcu_if_neigh_addr_cnf));
1085 return -EINVAL;
1086 }
1087 rc = pcu_rx_neigh_addr_cnf(bts, (struct gsm_pcu_if_neigh_addr_cnf*)&container->data);
1088 break;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001089 default:
1090 LOGP(DL1IF, LOGL_NOTICE, "(bts=%d) Rx unexpected msg type (%u) inside container!\n",
1091 bts->nr, container->msg_type);
1092 rc = -1;
1093 }
1094 return rc;
1095}
1096
1097#define CHECK_IF_MSG_SIZE(prim_len, prim_msg) \
1098 do { \
1099 size_t _len = PCUIF_HDR_SIZE + sizeof(prim_msg); \
1100 if (prim_len < _len) { \
1101 LOGP(DL1IF, LOGL_ERROR, "Received %zu bytes on PCU Socket, but primitive %s " \
1102 "size is %zu, discarding\n", prim_len, #prim_msg, _len); \
1103 return -EINVAL; \
1104 } \
1105 } while(0);
1106int pcu_rx(struct gsm_pcu_if *pcu_prim, size_t pcu_prim_length)
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001107{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001108 int rc = 0;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001109 size_t exp_len;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001110 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, pcu_prim->bts_nr);
1111 if (!bts) {
1112 LOGP(DL1IF, LOGL_NOTICE, "Received message for new BTS%d\n", pcu_prim->bts_nr);
1113 bts = bts_alloc(the_pcu, pcu_prim->bts_nr);
1114 if (!bts) {
1115 LOGP(DL1IF, LOGL_ERROR, "Failed to create object for BTS%d!\n", pcu_prim->bts_nr);
1116 return -EAGAIN;
1117 }
1118 }
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001119
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001120 switch (pcu_prim->msg_type) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001121 case PCU_IF_MSG_DATA_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001122 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001123 rc = pcu_rx_data_ind(bts, &pcu_prim->u.data_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001124 break;
Andreas Eversberga9be1542012-09-27 09:23:24 +02001125 case PCU_IF_MSG_DATA_CNF:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001126 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_cnf);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001127 rc = pcu_rx_data_cnf(bts, &pcu_prim->u.data_cnf);
Andreas Eversberga9be1542012-09-27 09:23:24 +02001128 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001129 case PCU_IF_MSG_RTS_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001130 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.rts_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001131 rc = pcu_rx_rts_req(bts, &pcu_prim->u.rts_req);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001132 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001133 case PCU_IF_MSG_RACH_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001134 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.rach_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001135 rc = pcu_rx_rach_ind(bts, &pcu_prim->u.rach_ind);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001136 break;
1137 case PCU_IF_MSG_INFO_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001138 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.info_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001139 rc = pcu_rx_info_ind(bts, &pcu_prim->u.info_ind);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001140 break;
1141 case PCU_IF_MSG_TIME_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001142 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.time_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001143 rc = pcu_rx_time_ind(bts, &pcu_prim->u.time_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001144 break;
Andreas Eversberg2b914642012-07-19 13:06:26 +02001145 case PCU_IF_MSG_PAG_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001146 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.pag_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001147 rc = pcu_rx_pag_req(bts, &pcu_prim->u.pag_req);
Andreas Eversberg2b914642012-07-19 13:06:26 +02001148 break;
Harald Welte3447c4a2017-07-11 00:06:38 +02001149 case PCU_IF_MSG_SUSP_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001150 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.susp_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001151 rc = pcu_rx_susp_req(bts, &pcu_prim->u.susp_req);
Harald Welte3447c4a2017-07-11 00:06:38 +02001152 break;
Oliver Smithcfb63212019-09-05 17:13:33 +02001153 case PCU_IF_MSG_APP_INFO_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001154 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.app_info_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001155 rc = pcu_rx_app_info_req(bts, &pcu_prim->u.app_info_req);
Oliver Smithcfb63212019-09-05 17:13:33 +02001156 break;
Vadim Yanitskiyb6572132021-06-20 21:05:24 +02001157 case PCU_IF_MSG_INTERF_IND:
1158 /* TODO: handle interference reports */
1159 break;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001160 case PCU_IF_MSG_CONTAINER:
1161 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.container);
1162 /* ^ check if we can access container fields, v check with container data length */
1163 exp_len = PCUIF_HDR_SIZE + sizeof(pcu_prim->u.container) + osmo_load16be(&pcu_prim->u.container.length);
1164 if (pcu_prim_length < exp_len) {
1165 LOGP(DL1IF, LOGL_ERROR, "Received %zu bytes on PCU Socket, but primitive container size" \
1166 "is %zu, discarding\n", pcu_prim_length, exp_len);
1167 }
1168 rc = pcu_rx_container(bts, &pcu_prim->u.container);
1169 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001170 default:
Thorsten Alteholz8bb79042018-10-06 10:42:58 +02001171 LOGP(DL1IF, LOGL_ERROR, "Received unknown PCU msg type %d\n",
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001172 pcu_prim->msg_type);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001173 rc = -EINVAL;
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001174 }
Philipp Maier8ccf7042018-04-10 13:12:42 +02001175
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001176 return rc;
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001177}