blob: 8d9defc10b621a699ae3476fbf76e767bc0d28a0 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040020#include <stdio.h>
21#include <unistd.h>
Pau Espin Pedrol32499b62019-12-09 13:55:12 +010022#include <inttypes.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040023#include <stdlib.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020024#include <string.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040025#include <errno.h>
26#include <assert.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020027#include <sys/socket.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040028#include <sys/un.h>
Andreas Eversberg81a12be2013-03-16 16:16:41 +010029#include <arpa/inet.h>
Pau Espin Pedrol754b0932020-03-26 14:47:13 +010030#include <ctype.h>
Max1187a772018-01-26 13:31:42 +010031
Andreas Eversberg0aed6542012-06-23 10:33:16 +020032extern "C" {
33#include <osmocom/core/talloc.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040034#include <osmocom/core/select.h>
35#include <osmocom/core/msgb.h>
Max528ff392016-02-22 11:42:33 +010036#include <osmocom/core/gsmtap_util.h>
37#include <osmocom/core/gsmtap.h>
Max1187a772018-01-26 13:31:42 +010038#include <osmocom/core/bitvec.h>
Alexander Couzens290d9032020-09-16 21:52:02 +020039#include <osmocom/core/sockaddr_str.h>
Max1187a772018-01-26 13:31:42 +010040#include <osmocom/core/logging.h>
41#include <osmocom/core/utils.h>
Alexander Couzens5bece2a2020-10-12 02:27:22 +020042#include <osmocom/gprs/gprs_ns2.h>
Max1187a772018-01-26 13:31:42 +010043#include <osmocom/gsm/l1sap.h>
44#include <osmocom/gsm/protocol/gsm_04_08.h>
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +010045#include <osmocom/gsm/gsm48_rest_octets.h>
Philipp Maiera58ec612021-01-25 23:43:52 +010046#include <osmocom/gsm/sysinfo.h>
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +020047
48#include <nacc_fsm.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020049}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040050
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040051#include <gprs_rlcmac.h>
52#include <pcu_l1_if.h>
53#include <gprs_debug.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040054#include <gprs_bssgp_pcu.h>
Harald Welte68fc1272016-11-16 22:48:33 +010055#include <osmocom/pcu/pcuif_proto.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020056#include <bts.h>
Max6dc90b82018-02-19 17:17:28 +010057#include <pdch.h>
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +020058#include <tbf_ul.h>
59#include <tbf_dl.h>
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010060#include <gprs_ms_storage.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020061
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010062// FIXME: move this, when changed from c++ to c.
63extern "C" {
Max58b66462016-05-13 10:34:15 +020064void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1,
65 struct gsmtap_inst *gsmtap);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010066int l1if_connect_pdch(void *obj, uint8_t ts);
67int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
68 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len);
69}
70
Andreas Eversberge266bd42012-07-13 14:00:21 +020071extern void *tall_pcu_ctx;
72
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010073#define PAGING_GROUP_LEN 3
74
75/* returns [0,999] on success, > 999 on error */
76uint16_t imsi2paging_group(const char* imsi)
77{
78 uint16_t pgroup = 0;
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070079 size_t len;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010080
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070081 len = (imsi != NULL) ? strlen(imsi) : 0;
82 if (len < PAGING_GROUP_LEN)
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010083 return 0xFFFF;
84 imsi += len - PAGING_GROUP_LEN;
85
86 while (*imsi != '\0') {
87 if (!isdigit(*imsi))
88 return 0xFFFF;
89 pgroup *= 10;
90 pgroup += *imsi - '0';
91 imsi++;
92 }
93 return pgroup;
94}
95
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040096/*
97 * PCU messages
98 */
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040099
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200100/* Can be used to allocate message with non-variable size */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400101struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
102{
103 struct msgb *msg;
104 struct gsm_pcu_if *pcu_prim;
105
106 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
107 if (!msg)
108 return NULL;
109 msgb_put(msg, sizeof(struct gsm_pcu_if));
110 pcu_prim = (struct gsm_pcu_if *) msg->data;
111 pcu_prim->msg_type = msg_type;
112 pcu_prim->bts_nr = bts_nr;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400113
114 return msg;
115}
116
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200117/* Allocate message with extra size, only reserve pcuif msg hdr */
118static struct msgb *pcu_msgb_alloc_ext_size(uint8_t msg_type, uint8_t bts_nr, size_t extra_size)
119{
120 struct msgb *msg;
121 struct gsm_pcu_if *pcu_prim;
122 msg = msgb_alloc(sizeof(struct gsm_pcu_if) + extra_size, "pcu_sock_tx");
123 /* Only header is filled, caller is responible for reserving + filling
124 * message type specific contents: */
125 msgb_put(msg, PCUIF_HDR_SIZE);
126 pcu_prim = (struct gsm_pcu_if *) msgb_data(msg);
127 pcu_prim->msg_type = msg_type;
128 pcu_prim->bts_nr = bts_nr;
129 return msg;
130}
131
Max0a8fae82017-03-08 18:53:30 +0100132const struct value_string gsm_pcu_if_text_type_names[] = {
133 OSMO_VALUE_STRING(PCU_VERSION),
134 OSMO_VALUE_STRING(PCU_OML_ALERT),
135 { 0, NULL }
136};
137
138int pcu_tx_txt_ind(enum gsm_pcu_if_text_type t, const char *fmt, ...)
139{
140 struct gsm_pcu_if *pcu_prim;
141 struct gsm_pcu_if_txt_ind *txt;
142 va_list ap;
143 char *rep;
144 struct msgb *msg = pcu_msgb_alloc(PCU_IF_MSG_TXT_IND, 0);
145 if (!msg)
146 return -ENOMEM;
147
148 pcu_prim = (struct gsm_pcu_if *) msg->data;
149 txt = &pcu_prim->u.txt_ind;
150 txt->type = t;
151
152 va_start(ap, fmt);
153 rep = talloc_vasprintf(tall_pcu_ctx, fmt, ap);
154 va_end(ap);
155
156 if (!rep)
157 return -ENOMEM;
158
159 osmo_strlcpy(txt->text, rep, TXT_MAX_LEN);
160 talloc_free(rep);
161
162 LOGP(DL1IF, LOGL_INFO, "Sending %s TXT as %s to BTS\n", txt->text,
163 get_value_string(gsm_pcu_if_text_type_names, t));
164
165 return pcu_sock_send(msg);
166}
167
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100168static int pcu_tx_act_req(struct gprs_rlcmac_bts *bts, const struct gprs_rlcmac_pdch *pdch,
169 uint8_t activate)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400170{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400171 struct msgb *msg;
172 struct gsm_pcu_if *pcu_prim;
173 struct gsm_pcu_if_act_req *act_req;
174
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100175 LOGPDCH(pdch, DL1IF, LOGL_INFO, "Sending %s request\n",
176 (activate) ? "activate" : "deactivate");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400177
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100178 msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, bts->nr);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400179 if (!msg)
180 return -ENOMEM;
181 pcu_prim = (struct gsm_pcu_if *) msg->data;
182 act_req = &pcu_prim->u.act_req;
183 act_req->activate = activate;
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100184 act_req->trx_nr = pdch->trx_no();
185 act_req->ts_nr = pdch->ts_no;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400186
187 return pcu_sock_send(msg);
188}
189
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100190static 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 +0400191 uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
192 uint8_t len)
193{
194 struct msgb *msg;
195 struct gsm_pcu_if *pcu_prim;
196 struct gsm_pcu_if_data *data_req;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100197 int current_fn = bts_current_frame_number(bts);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400198
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100199 LOGP(DL1IF, LOGL_DEBUG, "(bts=%u,trx=%u,ts=%u) FN=%u Sending data request: sapi=%d "
200 "arfcn=%d cur_fn=%d block=%d data=%s\n", bts->nr, trx, ts, fn, sapi,
201 arfcn, current_fn, block_nr, osmo_hexdump(data, len));
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400202
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100203 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, bts->nr);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400204 if (!msg)
205 return -ENOMEM;
206 pcu_prim = (struct gsm_pcu_if *) msg->data;
207 data_req = &pcu_prim->u.data_req;
208
209 data_req->sapi = sapi;
210 data_req->fn = fn;
211 data_req->arfcn = arfcn;
212 data_req->trx_nr = trx;
213 data_req->ts_nr = ts;
214 data_req->block_nr = block_nr;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200215 if (len)
216 memcpy(data_req->data, data, len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400217 data_req->len = len;
218
219 return pcu_sock_send(msg);
220}
221
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100222void 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 +0400223 uint32_t fn, uint8_t block_nr)
224{
Maxb216c6b2017-09-01 11:00:39 +0200225#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100226 if (bts->trx[trx].fl1h) {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200227 if (!msg) /* Simply skip sending idle frames to L1 */
228 return;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100229 l1if_pdch_req(bts->trx[trx].fl1h, ts, 0, fn, arfcn, block_nr,
230 msg->data, msg->len);
Max528ff392016-02-22 11:42:33 +0100231 msgb_free(msg);
232 return;
233 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100234#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200235 if (!msg) {
236 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
237 NULL, 0);
238 return;
239 }
240
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100241 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100242 msg->data, msg->len);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200243 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400244}
245
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100246void pcu_l1if_tx_ptcch(struct gprs_rlcmac_bts *bts,
247 uint8_t trx, uint8_t ts, uint16_t arfcn,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700248 uint32_t fn, uint8_t block_nr,
249 uint8_t *data, size_t data_len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400250{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100251 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PTCCH))
252 gsmtap_send(the_pcu->gsmtap, arfcn, ts, GSMTAP_CHANNEL_PTCCH, 0, fn, 0, 0, data, data_len);
Maxcad867e2016-04-21 14:35:55 +0200253#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100254 if (bts->trx[trx].fl1h) {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200255 if (!data_len) /* Simply skip sending idle frames to L1 */
256 return;
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700257 l1if_pdch_req(bts->trx[trx].fl1h, ts, 1, fn, arfcn, block_nr, data, data_len);
Max528ff392016-02-22 11:42:33 +0100258 return;
259 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100260#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200261 if (!data_len) {
262 pcu_tx_data_req(bts, trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, NULL, 0);
263 return;
264 }
265
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100266 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 +0400267}
268
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100269void pcu_l1if_tx_agch(struct gprs_rlcmac_bts *bts, bitvec * block, int plen)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400270{
Pau Espin Pedrold752d7c2019-10-07 20:56:48 +0200271 uint8_t data[GSM_MACBLOCK_LEN]; /* prefix PLEN */
272
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400273 /* FIXME: why does OpenBTS has no PLEN and no fill in message? */
274 bitvec_pack(block, data + 1);
275 data[0] = (plen << 2) | 0x01;
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200276
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100277 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_AGCH))
278 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 +0200279
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100280 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 +0400281}
282
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100283void pcu_l1if_tx_pch(struct gprs_rlcmac_bts *bts, bitvec * block, int plen, uint16_t pgroup)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400284{
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200285 uint8_t data[PAGING_GROUP_LEN + GSM_MACBLOCK_LEN];
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100286 int i;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400287
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200288 /* prepend paging group */
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100289 for (i = 0; i < PAGING_GROUP_LEN; i++) {
290 data[PAGING_GROUP_LEN - 1 - i] = '0' + (char)(pgroup % 10);
291 pgroup = pgroup / 10;
292 }
293 OSMO_ASSERT(pgroup == 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400294
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200295 /* block provided by upper layer comes without first byte (plen),
296 * prepend it manually:
297 */
298 OSMO_ASSERT(sizeof(data) >= PAGING_GROUP_LEN + 1 + block->data_len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400299 data[3] = (plen << 2) | 0x01;
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200300 bitvec_pack(block, data + PAGING_GROUP_LEN + 1);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200301
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100302 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PCH))
303 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 +0200304
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100305 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 +0400306}
307
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200308int pcu_tx_neigh_addr_res_req(struct gprs_rlcmac_bts *bts, const struct neigh_cache_entry_key *neigh_key)
309{
310 struct msgb *msg;
311 struct gsm_pcu_if *pcu_prim;
312 struct gsm_pcu_if_neigh_addr_req *naddr_req;
313
314 LOGP(DL1IF, LOGL_DEBUG, "(bts=%u) Tx Neighbor Address Resolution Request: " NEIGH_CACHE_ENTRY_KEY_FMT "\n",
315 bts->nr, NEIGH_CACHE_ENTRY_KEY_ARGS(neigh_key));
316
317 msg = pcu_msgb_alloc_ext_size(PCU_IF_MSG_CONTAINER, bts->nr, sizeof(struct gsm_pcu_if_neigh_addr_req));
318 if (!msg)
319 return -ENOMEM;
320 pcu_prim = (struct gsm_pcu_if *) msgb_data(msg);
321 naddr_req = (struct gsm_pcu_if_neigh_addr_req *)&pcu_prim->u.container.data[0];
322
323 msgb_put(msg, sizeof(pcu_prim->u.container) + sizeof(struct gsm_pcu_if_neigh_addr_req));
324 pcu_prim->u.container.msg_type = PCU_IF_MSG_NEIGH_ADDR_REQ;
325 osmo_store16be(sizeof(struct gsm_pcu_if_neigh_addr_req), &pcu_prim->u.container.length);
326
327 osmo_store16be(neigh_key->local_lac, &naddr_req->local_lac);
328 osmo_store16be(neigh_key->local_ci, &naddr_req->local_ci);
329 osmo_store16be(neigh_key->tgt_arfcn, &naddr_req->tgt_arfcn);
330 naddr_req->tgt_bsic = neigh_key->tgt_bsic;
331
332 return pcu_sock_send(msg);
333}
334
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100335void 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 +0200336{
Pau Espin Pedrolfecab502021-03-17 15:26:37 +0100337 bts_set_current_block_frame_number(bts, fn);
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200338}
339
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100340int 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 +0200341 uint8_t len, uint32_t fn, struct pcu_l1_meas *meas)
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100342{
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100343 int rc;
344
Pau Espin Pedrol3bbb3cc2021-08-31 14:09:42 +0200345 if (!pdch->is_enabled()) {
346 LOGPDCH(pdch, DL1IF, LOGL_INFO, "Received DATA.ind (PDTCH) on disabled TS\n");
347 return -EINVAL;
348 }
349
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100350 rc = pdch->rcv_block(data, len, fn, meas);
351 pdch_ulc_expire_fn(pdch->ulc, fn);
352 return rc;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100353}
354
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200355static int list_arfcn(const struct gprs_rlcmac_bts *bts, const struct gsm_sysinfo_freq *freq, const char *text)
356{
357 int n = 0, i;
358 for (i = 0; i < 1024; i++) {
359 if (freq[i].mask) {
360 if (!n)
361 LOGP(DL1IF, LOGL_INFO, "BTS%d: %s", bts->nr, text);
362 LOGPC(DL1IF, LOGL_INFO, " %d", i);
363 n++;
364 }
365 }
366 if (n)
367 LOGPC(DL1IF, LOGL_INFO, "\n");
368
369 return n;
370}
371
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100372static int pcu_rx_data_ind_bcch(struct gprs_rlcmac_bts *bts, uint8_t *data, uint8_t len)
Max84bf0fa2017-09-01 11:02:40 +0200373{
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200374 struct gsm48_system_information_type_2 *si2;
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100375 const uint8_t *si_ro;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200376
Philipp Maiera58ec612021-01-25 23:43:52 +0100377 switch (len) {
378 case 0:
379 /* Due to historical reasons also accept a completely empty message as
380 * revoke command for SI13. */
381 LOGP(DL1IF, LOGL_ERROR,
382 "Received PCU data indication that contains no data -- Revoked SI13.\n");
Max84bf0fa2017-09-01 11:02:40 +0200383 bts->si13_is_set = false;
Max84bf0fa2017-09-01 11:02:40 +0200384
Philipp Maiera58ec612021-01-25 23:43:52 +0100385 return 0;
386 case 1:
387 /* Revoke SI, type is identified by a single byte which is coded after
388 * enum osmo_sysinfo_type. */
389 switch (data[0]) {
390 case SYSINFO_TYPE_1:
391 bts->si1_is_set = false;
392 break;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200393 case SYSINFO_TYPE_2:
394 bts->si2_is_set = false;
395 break;
Philipp Maiera58ec612021-01-25 23:43:52 +0100396 case SYSINFO_TYPE_3:
397 bts->si3_is_set = false;
398 break;
399 case SYSINFO_TYPE_13:
400 bts->si13_is_set = false;
401 break;
402 default:
403 LOGP(DL1IF, LOGL_ERROR,
404 "Received PCU data indication that contains an unsupported system information identifier (%02x,OSMO) -- ignored.\n", data[0]);
405 return -EINVAL;
406 }
407 LOGP(DPCU, LOGL_DEBUG,
408 "Received PCU data indication: Revoked SI%s\n",
409 get_value_string(osmo_sitype_strs, data[0]));
410 return 0;
411 case GSM_MACBLOCK_LEN:
412 /* Update SI, type is identified by the RR sysinfo type, which is the
413 * 3rd byte in the buffer. */
414 switch (data[2]) {
415 case GSM48_MT_RR_SYSINFO_1:
416 memcpy(bts->si1, data, GSM_MACBLOCK_LEN);
417 bts->si1_is_set = true;
418 break;
Pau Espin Pedrol86f4c092021-06-08 18:53:40 +0200419 case GSM48_MT_RR_SYSINFO_2:
420 memcpy(bts->si2, data, GSM_MACBLOCK_LEN);
421 bts->si2_is_set = true;
422 si2 = (struct gsm48_system_information_type_2 *)bts->si2;
423 gsm48_decode_freq_list(bts->si2_bcch_cell_list, si2->bcch_frequency_list,
424 sizeof(si2->bcch_frequency_list), 0xce, 1);
425 list_arfcn(bts, bts->si2_bcch_cell_list, "SI2 Neighbour cells in same band:");
426 break;
Philipp Maiera58ec612021-01-25 23:43:52 +0100427 case GSM48_MT_RR_SYSINFO_3:
428 memcpy(bts->si3, data, GSM_MACBLOCK_LEN);
429 bts->si3_is_set = true;
430 break;
431 case GSM48_MT_RR_SYSINFO_13:
432 memcpy(bts->si13, data, GSM_MACBLOCK_LEN);
433 bts->si13_is_set = true;
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100434 si_ro = ((struct gsm48_system_information_type_13*)data)->rest_octets;
Pau Espin Pedrold65bd9d2021-06-01 16:43:41 +0200435 if (osmo_gsm48_rest_octets_si13_decode(&bts->si13_ro_decoded, si_ro) < 0)
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100436 LOGP(DPCU, LOGL_ERROR, "Error decoding SI13\n");
Philipp Maiera58ec612021-01-25 23:43:52 +0100437 break;
438 default:
439 LOGP(DL1IF, LOGL_ERROR,
440 "Received PCU data indication that contains an unsupported system information identifier (%02x,RR) -- ignored.\n", data[2]);
441 return -EINVAL;
442 }
443 LOGP(DPCU, LOGL_DEBUG,
444 "Received PCU data indication: Updated %s: %s\n",
445 gsm48_pdisc_msgtype_name(data[1], data[2]),
446 osmo_hexdump_nospc(data + 1, GSM_MACBLOCK_LEN));
447 return 0;
448 default:
449 LOGP(DL1IF, LOGL_ERROR,
450 "Received PCU data indication with unexpected data length: %u -- ignored.\n",
451 len);
Max84bf0fa2017-09-01 11:02:40 +0200452 return -EINVAL;
453 }
Max84bf0fa2017-09-01 11:02:40 +0200454}
455
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100456static int pcu_rx_data_ind(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400457{
Max528ff392016-02-22 11:42:33 +0100458 int rc;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100459 int current_fn = bts_current_frame_number(bts);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100460 struct pcu_l1_meas meas = {0};
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100461 struct gprs_rlcmac_pdch *pdch;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200462 uint8_t gsmtap_chantype;
Maxb216c6b2017-09-01 11:00:39 +0200463
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100464 LOGP(DL1IF, LOGL_DEBUG, "(bts=%" PRIu8 ",trx=%" PRIu8 ",ts=%" PRIu8 ") FN=%u "
465 "Rx DATA.ind: sapi=%d arfcn=%d cur_fn=%d "
466 "block=%d data=%s\n", bts->nr, data_ind->trx_nr, data_ind->ts_nr,
467 data_ind->fn, data_ind->sapi, data_ind->arfcn, current_fn,
468 data_ind->block_nr, osmo_hexdump(data_ind->data, data_ind->len));
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400469
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400470 switch (data_ind->sapi) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400471 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100472 pdch = &bts->trx[data_ind->trx_nr].pdch[data_ind->ts_nr];
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100473 pcu_l1_meas_set_rssi(&meas, data_ind->rssi);
Maxb216c6b2017-09-01 11:00:39 +0200474 /* convert BER to % value */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100475 pcu_l1_meas_set_ber(&meas, data_ind->ber10k / 100);
476 pcu_l1_meas_set_bto(&meas, data_ind->ta_offs_qbits);
477 pcu_l1_meas_set_link_qual(&meas, data_ind->lqual_cb / 10);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700478
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100479 LOGPDCH(pdch, DL1IF, LOGL_DEBUG, "FN=%u Rx DATA.ind PDTCH: "
480 "BER10k = %d, BTO = %d, Q = %d\n", data_ind->fn,
481 data_ind->ber10k, data_ind->ta_offs_qbits, data_ind->lqual_cb);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700482
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100483 rc = pcu_rx_data_ind_pdtch(bts, pdch, data_ind->data, data_ind->len,
484 data_ind->fn, &meas);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200485 gsmtap_chantype = GSMTAP_CHANNEL_PDTCH;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400486 break;
Max84bf0fa2017-09-01 11:02:40 +0200487 case PCU_IF_SAPI_BCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100488 rc = pcu_rx_data_ind_bcch(bts, data_ind->data, data_ind->len);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200489 gsmtap_chantype = GSMTAP_CHANNEL_BCCH;
Max84bf0fa2017-09-01 11:02:40 +0200490 break;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400491 default:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100492 LOGP(DL1IF, LOGL_ERROR, "(bts=%" PRIu8 ",trx=%" PRIu8 ",ts=%" PRIu8 ") "
493 "FN=%u Rx DATA.ind with unsupported sapi %d\n",
494 bts->nr, data_ind->trx_nr, data_ind->ts_nr, data_ind->fn, data_ind->sapi);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400495 rc = -EINVAL;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200496 gsmtap_chantype = GSMTAP_CHANNEL_UNKNOWN;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400497 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400498
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100499 if (rc < 0 && (the_pcu->gsmtap_categ_mask & (1 <<PCU_GSMTAP_C_UL_UNKNOWN))) {
500 gsmtap_send(the_pcu->gsmtap, data_ind->arfcn | GSMTAP_ARFCN_F_UPLINK, data_ind->ts_nr,
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200501 gsmtap_chantype, 0, data_ind->fn, meas.rssi, meas.link_qual, data_ind->data, data_ind->len);
Harald Welte717cdf52017-07-21 21:56:23 +0200502 }
503
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400504 return rc;
505}
506
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100507static int pcu_rx_data_cnf(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_data *data_cnf)
Andreas Eversberga9be1542012-09-27 09:23:24 +0200508{
509 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100510 int current_fn = bts_current_frame_number(bts);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200511
Philipp Maier7e8e3972018-04-10 13:31:45 +0200512 LOGP(DL1IF, LOGL_DEBUG, "Data confirm received: sapi=%d fn=%d cur_fn=%d\n",
513 data_cnf->sapi, data_cnf->fn, current_fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200514
515 switch (data_cnf->sapi) {
516 case PCU_IF_SAPI_PCH:
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200517 if (data_cnf->data[2] == 0x3f)
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100518 bts_rcv_imm_ass_cnf(bts, data_cnf->data, data_cnf->fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200519 break;
520 default:
521 LOGP(DL1IF, LOGL_ERROR, "Received PCU data confirm with "
522 "unsupported sapi %d\n", data_cnf->sapi);
523 rc = -EINVAL;
524 }
525
526 return rc;
527}
528
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100529// FIXME: remove this, when changed from c++ to c.
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100530int pcu_rx_rts_req_pdtch(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100531 uint32_t fn, uint8_t block_nr)
532{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100533 return gprs_rlcmac_rcv_rts_block(bts,
Max878bd1f2016-07-20 13:05:05 +0200534 trx, ts, fn, block_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100535}
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100536int pcu_rx_rts_req_ptcch(struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700537 uint32_t fn, uint8_t block_nr)
538{
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700539 struct gprs_rlcmac_pdch *pdch;
540
541 /* Prevent buffer overflow */
542 if (trx >= ARRAY_SIZE(bts->trx) || ts >= 8)
543 return -EINVAL;
544
545 /* Make sure PDCH time-slot is enabled */
546 pdch = &bts->trx[trx].pdch[ts];
547 if (!pdch->m_is_enabled)
548 return -EAGAIN;
549
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200550 /* If there's no TBF attached to this PDCH, we can skip Tx of PTCCH
551 * since there's nothing worthy of being transmitted. This way BTS can
552 * identify idle blocks and send nothing or dumy blocks with reduced
553 * energy for the sake of energy saving.
554 */
555 const unsigned num_tbfs = pdch->num_tbfs(GPRS_RLCMAC_DL_TBF)
556 + pdch->num_tbfs(GPRS_RLCMAC_UL_TBF);
557 bool skip_idle = (num_tbfs == 0);
558#ifdef ENABLE_DIRECT_PHY
559 /* In DIRECT_PHY mode we want to always submit something to L1 in
560 * TRX0, since BTS is not preparing dummy bursts on idle TS for us: */
561 skip_idle = skip_idle && trx != 0;
562#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200563 if (skip_idle) {
564 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
565 NULL, 0);
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200566 return 0;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200567 }
Pau Espin Pedroleeae7762021-09-20 11:20:09 +0200568
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100569 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700570 pdch->ptcch_msg, GSM_MACBLOCK_LEN);
571 return 0;
572}
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100573
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100574static 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 +0400575{
576 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100577 int current_fn = bts_current_frame_number(bts);
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100578 const struct gprs_rlcmac_pdch *pdch;
579 pdch = &bts->trx[rts_req->trx_nr].pdch[rts_req->ts_nr];
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400580
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100581 LOGPDCH(pdch, DL1IF, LOGL_DEBUG, "FN=%u RX RTS.req: sapi=%d "
582 "arfcn=%d cur_fn=%d block=%d\n", rts_req->fn,
583 rts_req->sapi, rts_req->arfcn, current_fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400584
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400585 switch (rts_req->sapi) {
586 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100587 pcu_rx_rts_req_pdtch(bts, rts_req->trx_nr, rts_req->ts_nr,
Max878bd1f2016-07-20 13:05:05 +0200588 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400589 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400590 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100591 pcu_rx_rts_req_ptcch(bts, rts_req->trx_nr, rts_req->ts_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700592 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400593 break;
594 default:
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100595 LOGP(DL1IF, LOGL_ERROR, "(bts=%u,trx=%u,ts=%u) FN=%u RX RTS.req with "
596 "unsupported sapi %d\n", bts->nr, rts_req->trx_nr, rts_req->ts_nr,
597 rts_req->fn, rts_req->sapi);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400598 rc = -EINVAL;
599 }
600
601 return rc;
602}
603
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700604/* C -> C++ adapter for direct DSP access code (e.g. osmo-bts-sysmo) */
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100605extern "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 +0700606{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700607 struct rach_ind_params rip = {
608 /* The content of RA is not of interest on PTCCH/U */
609 .burst_type = GSM_L1_BURST_TYPE_ACCESS_0,
610 .is_11bit = false,
611 .ra = 0x00,
612 .trx_nr = trx_nr,
613 .ts_nr = ts_nr,
614 .rfn = fn,
615 .qta = qta,
616 };
617
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100618 return bts_rcv_ptcch_rach(bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700619}
620
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100621static 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 +0400622{
623 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100624 int current_fn = bts_current_frame_number(bts);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400625
626 LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
Vadim Yanitskiyb30f28f2020-03-29 19:01:58 +0700627 "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 +0200628 rach_ind->ra, rach_ind->fn, current_fn, rach_ind->is_11bit);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400629
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700630 struct rach_ind_params rip = {
631 .burst_type = (enum ph_burst_type) rach_ind->burst_type,
632 .is_11bit = rach_ind->is_11bit > 0,
633 .ra = rach_ind->ra,
634 .trx_nr = rach_ind->trx_nr,
635 .ts_nr = rach_ind->ts_nr,
636 .rfn = rach_ind->fn,
637 .qta = rach_ind->qta,
638 };
639
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400640 switch (rach_ind->sapi) {
641 case PCU_IF_SAPI_RACH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100642 rc = bts_rcv_rach(bts, &rip);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400643 break;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700644 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100645 rc = bts_rcv_ptcch_rach(bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700646 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400647 default:
648 LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
649 "unsupported sapi %d\n", rach_ind->sapi);
650 rc = -EINVAL;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400651 }
652
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400653 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400654}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200655
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200656static int pcu_info_ind_ns(struct gprs_rlcmac_bts *bts,
657 const struct gsm_pcu_if_info_ind *info_ind)
658{
659 struct osmo_sockaddr remote[PCU_IF_NUM_NSVC] = { };
660 struct osmo_sockaddr local[PCU_IF_NUM_NSVC] = { };
661 uint16_t nsvci[PCU_IF_NUM_NSVC] = { };
662 uint16_t valid = 0;
663
664 for (unsigned int i = 0; i < PCU_IF_NUM_NSVC; i++) {
665 struct osmo_sockaddr_str sockstr;
666
667 switch (info_ind->address_type[i]) {
668 case PCU_IF_ADDR_TYPE_IPV4:
669 local[i].u.sin.sin_family = AF_INET;
670 local[i].u.sin.sin_addr.s_addr = INADDR_ANY;
671 local[i].u.sin.sin_port = htons(info_ind->local_port[i]);
672
673 remote[i].u.sin.sin_family = AF_INET;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100674 memcpy(&remote[i].u.sin.sin_addr, &info_ind->remote_ip[i].v4,
675 sizeof(struct in_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200676 remote[i].u.sin.sin_port = htons(info_ind->remote_port[i]);
677 break;
678 case PCU_IF_ADDR_TYPE_IPV6:
679 local[i].u.sin6.sin6_family = AF_INET6;
680 local[i].u.sin6.sin6_addr = in6addr_any;
681 local[i].u.sin6.sin6_port = htons(info_ind->local_port[i]);
682
683 remote[i].u.sin6.sin6_family = AF_INET6;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100684 memcpy(&remote[i].u.sin6.sin6_addr,
685 &info_ind->remote_ip[i].v6,
686 sizeof(struct in6_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200687 remote[i].u.sin6.sin6_port = htons(info_ind->remote_port[i]);
688 break;
689 default:
690 continue;
691 }
692 nsvci[i] = info_ind->nsvci[i];
693
694 LOGP(DL1IF, LOGL_DEBUG, " NS%u nsvci=%u\n", i, nsvci[i]);
695 if (osmo_sockaddr_str_from_sockaddr(&sockstr, &remote[i].u.sas))
696 strcpy(sockstr.ip, "invalid");
697
698 LOGP(DL1IF, LOGL_DEBUG, " NS%u address: r=%s:%u<->l=NULL:%u\n",
699 i, sockstr.ip, sockstr.port, info_ind->local_port[i]);
700
701 valid |= 1 << i;
702 }
703
704 if (valid == 0) {
705 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
706 return -EINVAL;
707 }
708
Alexander Couzens94a367f2021-02-16 16:57:15 +0100709 return gprs_ns_update_config(bts, info_ind->nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200710}
711
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100712static 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 +0200713{
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +0200714 struct gprs_bssgp_pcu *pcu;
Harald Weltebb47d952016-11-17 18:40:02 +0100715 int rc = 0;
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700716 unsigned int trx_nr, ts_nr;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100717 unsigned int i;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200718
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200719 if (info_ind->version != PCU_IF_VERSION) {
Vadim Yanitskiyfad21282020-07-19 02:05:33 +0700720 fprintf(stderr, "PCU interface version number of BTS (%u) is "
721 "different (%u).\nPlease re-compile!\n",
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200722 info_ind->version, PCU_IF_VERSION);
723 exit(-1);
724 }
725
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400726 LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200727
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400728 if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
729 LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100730 if (!bts->active)
731 return -EAGAIN;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400732bssgp_failed:
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100733 bts->active = false;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400734 /* free all TBF */
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700735 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
736 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
737 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++)
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200738 bts->trx[trx_nr].pdch[ts_nr].disable();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400739 }
Alexander Couzens290d9032020-09-16 21:52:02 +0200740 gprs_bssgp_destroy(bts);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200741 exit(0);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200742 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400743 LOGP(DL1IF, LOGL_INFO, "BTS available\n");
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100744 LOGP(DL1IF, LOGL_DEBUG, " mcc=%03u\n", info_ind->mcc);
745 LOGP(DL1IF, LOGL_DEBUG, " mnc=%0*u\n", info_ind->mnc_3_digits, info_ind->mnc);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400746 LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
747 LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
Harald Welte1473b372019-03-21 19:48:55 +0100748 LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100749 LOGP(DL1IF, LOGL_DEBUG, " bsic=%d\n", info_ind->bsic);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400750 LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
751 LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
752 info_ind->nse_timer[0], info_ind->nse_timer[1],
753 info_ind->nse_timer[2], info_ind->nse_timer[3],
754 info_ind->nse_timer[4], info_ind->nse_timer[5],
755 info_ind->nse_timer[6]);
756 LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
757 "%d\n",
758 info_ind->cell_timer[0], info_ind->cell_timer[1],
759 info_ind->cell_timer[2], info_ind->cell_timer[3],
760 info_ind->cell_timer[4], info_ind->cell_timer[5],
761 info_ind->cell_timer[6], info_ind->cell_timer[7],
762 info_ind->cell_timer[8], info_ind->cell_timer[9],
763 info_ind->cell_timer[10]);
764 LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
765 LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
766 LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
767 LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
768 LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
769 LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
770 LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
771 LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
772 LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
773 LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
774 LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
775 LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
776 LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
777 LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
Pau Espin Pedrol2e6b60d2021-01-20 13:48:31 +0100778 bts->cgi_ps.rai.lac.plmn.mcc = info_ind->mcc;
779 bts->cgi_ps.rai.lac.plmn.mnc = info_ind->mnc;
780 bts->cgi_ps.rai.lac.plmn.mnc_3_digits = info_ind->mnc_3_digits;
781 bts->cgi_ps.rai.lac.lac = info_ind->lac;
782 bts->cgi_ps.rai.rac = info_ind->rac;
783 bts->cgi_ps.cell_identity = info_ind->cell_id;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100784 bts->bsic = info_ind->bsic;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100785
786 bts->cs_mask = 1 << 0; /* We need at least 1 CS, let's enable CS1 */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400787 for (i = 0; i < 4; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100788 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_CS1 << i));
789 bts->cs_mask |= allowed << i;
790 if (allowed)
791 LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i + 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400792 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100793 bts_recalc_max_cs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100794
795 bts->mcs_mask = 0;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400796 for (i = 0; i < 9; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100797 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_MCS1 << i));
798 bts->mcs_mask |= allowed << i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100799 if (allowed)
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100800 LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i + 1);
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100801
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400802 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100803 bts_recalc_max_mcs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100804
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100805 LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%u%s\n", info_ind->initial_cs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100806 the_pcu->vty.force_initial_cs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100807 bts->pcuif_info_ind.initial_cs = info_ind->initial_cs;
808 bts_recalc_initial_cs(bts);
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100809
810 LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%u%s\n", info_ind->initial_mcs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100811 the_pcu->vty.force_initial_mcs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100812 bts->pcuif_info_ind.initial_mcs = info_ind->initial_mcs;
813 bts_recalc_initial_mcs(bts);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200814
Alexander Couzens290d9032020-09-16 21:52:02 +0200815 pcu = gprs_bssgp_init(
816 bts,
817 info_ind->nsei, info_ind->bvci,
818 info_ind->mcc, info_ind->mnc, info_ind->mnc_3_digits,
819 info_ind->lac, info_ind->rac, info_ind->cell_id);
820 if (!pcu) {
Vadim Yanitskiyc1a726c2020-10-08 22:46:26 +0700821 LOGP(DL1IF, LOGL_ERROR, "Failed to init BSSGP\n");
Alexander Couzens9b5c9602020-07-28 13:47:18 +0200822 goto bssgp_failed;
823 }
824
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200825 rc = pcu_info_ind_ns(pcu->bts, info_ind);
826 if (rc < 0) {
Alexander Couzens290d9032020-09-16 21:52:02 +0200827 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400828 goto bssgp_failed;
829 }
830
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400831 if (info_ind->t3142) { /* if timer values are set */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200832 osmo_tdef_set(bts->T_defs_bts, 3142, info_ind->t3142, OSMO_TDEF_S);
833 osmo_tdef_set(bts->T_defs_bts, 3169, info_ind->t3169, OSMO_TDEF_S);
834 osmo_tdef_set(bts->T_defs_bts, 3191, info_ind->t3191, OSMO_TDEF_S);
835 osmo_tdef_set(bts->T_defs_bts, 3193, info_ind->t3193_10ms * 10, OSMO_TDEF_MS);
836 osmo_tdef_set(bts->T_defs_bts, 3195, info_ind->t3195, OSMO_TDEF_S);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400837 bts->n3101 = info_ind->n3101;
838 bts->n3103 = info_ind->n3103;
839 bts->n3105 = info_ind->n3105;
840 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400841
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700842 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
843 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100844 if ((info_ind->flags & PCU_IF_FLAG_SYSMO)
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700845 && info_ind->trx[trx_nr].hlayer1) {
Maxcad867e2016-04-21 14:35:55 +0200846#ifdef ENABLE_DIRECT_PHY
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700847 LOGP(DL1IF, LOGL_DEBUG, " TRX %d hlayer1=%x\n", trx_nr,
848 info_ind->trx[trx_nr].hlayer1);
849 if (!bts->trx[trx_nr].fl1h)
850 bts->trx[trx_nr].fl1h = l1if_open_pdch(
851 trx_nr,
852 info_ind->trx[trx_nr].hlayer1,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100853 the_pcu->gsmtap);
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700854 if (!bts->trx[trx_nr].fl1h) {
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100855 LOGP(DL1IF, LOGL_FATAL, "Failed to open direct "
856 "DSP access for PDCH.\n");
857 exit(0);
858 }
859#else
860 LOGP(DL1IF, LOGL_FATAL, "Compiled without direct DSP "
861 "access for PDCH, but enabled at "
862 "BTS. Please deactivate it!\n");
863 exit(0);
864#endif
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100865 }
866
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700867 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++) {
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700868 const struct gsm_pcu_if_info_ts *its = &info_ind->trx[trx_nr].ts[ts_nr];
Vadim Yanitskiy0f41b712020-07-24 01:28:13 +0700869 struct gprs_rlcmac_pdch *pdch = &bts->trx[trx_nr].pdch[ts_nr];
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700870 if ((info_ind->trx[trx_nr].pdch_mask & (1 << ts_nr))) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400871 /* FIXME: activate dynamically at RLCMAC */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200872 if (!pdch->is_enabled()) {
Maxcad867e2016-04-21 14:35:55 +0200873#ifdef ENABLE_DIRECT_PHY
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100874 if ((info_ind->flags &
875 PCU_IF_FLAG_SYSMO))
876 l1if_connect_pdch(
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700877 bts->trx[trx_nr].fl1h, ts_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100878#endif
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100879 pcu_tx_act_req(bts, pdch, 1);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200880 pdch->enable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200881 }
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700882
883 pdch->tsc = its->tsc;
884
885 /* (Optional) frequency hopping parameters */
886 if (its->h) {
887 pdch->fh.enabled = true;
888 pdch->fh.maio = its->maio;
889 pdch->fh.hsn = its->hsn;
890
891 OSMO_ASSERT(its->ma_bit_len <= sizeof(pdch->fh.ma) * 8);
892 pdch->fh.ma_oct_len = OSMO_BYTES_FOR_BITS(its->ma_bit_len);
893 pdch->fh.ma_bit_len = its->ma_bit_len;
894
895 /* Mobile Allocation + padding (byte/bit order as on the wire):
896 * | 00 00 00 00 00 cc bb aa | -> | cc bb aa 00 00 00 00 00 | */
897 unsigned int offset = sizeof(pdch->fh.ma) - pdch->fh.ma_oct_len;
898 memcpy(pdch->fh.ma, its->ma + offset, pdch->fh.ma_oct_len);
899 }
900
901 LOGP(DL1IF, LOGL_INFO, "PDCH (trx=%u, ts=%u): tsc=%u, hopping=%s\n",
902 trx_nr, ts_nr, pdch->tsc, pdch->fh.enabled ? "yes" : "no");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400903 } else {
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200904 if (pdch->is_enabled()) {
Pau Espin Pedroldf58ddf2021-03-09 16:19:22 +0100905 pcu_tx_act_req(bts, pdch, 0);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200906 pdch->disable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200907 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400908 }
909 }
910 }
911
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100912 bts->active = true;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200913 return rc;
914}
915
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100916static 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 +0200917{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400918 uint8_t fn13 = time_ind->fn % 13;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200919
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400920 /* omit frame numbers not starting at a MAC block */
921 if (fn13 != 0 && fn13 != 4 && fn13 != 8)
922 return 0;
Max3741f142017-06-26 12:10:12 +0200923
924 LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n", time_ind->fn % 52);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400925
Pau Espin Pedrol30617112021-03-05 18:59:05 +0100926 /* Ignore TIME.ind completely, we nowadays relay on DATA.ind always
927 * providing all block FNs. */
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200928 return 0;
929}
930
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100931static 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 +0200932{
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200933 struct osmo_mobile_identity mi;
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200934 struct GprsMs *ms = NULL;
935 struct paging_req_cs req = { .chan_needed = pag_req->chan_needed,
936 .tlli = GSM_RESERVED_TMSI };
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200937 int rc;
938
Andreas Eversberg2b914642012-07-19 13:06:26 +0200939 LOGP(DL1IF, LOGL_DEBUG, "Paging request received: chan_needed=%d "
940 "length=%d\n", pag_req->chan_needed, pag_req->identity_lv[0]);
941
Pau Espin Pedrol32499b62019-12-09 13:55:12 +0100942 /* check if identity does not fit: length > sizeof(lv) - 1 */
943 if (pag_req->identity_lv[0] >= sizeof(pag_req->identity_lv)) {
944 LOGP(DL1IF, LOGL_ERROR, "Paging identity too large (%" PRIu8 ")\n",
945 pag_req->identity_lv[0]);
946 return -EINVAL;
947 }
948
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200949 rc = osmo_mobile_identity_decode(&mi, &pag_req->identity_lv[1], pag_req->identity_lv[0], true);
950 if (rc < 0) {
951 LOGP(DL1IF, LOGL_ERROR, "Failed to decode Mobile Identity in Paging Request (rc=%d)\n", rc);
952 return -EINVAL;
953 }
954
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200955 switch (mi.type) {
956 case GSM_MI_TYPE_TMSI:
957 req.mi_tmsi = mi;
958 req.mi_tmsi_present = true;
959 /* TODO: look up MS by TMSI? Derive TLLI? */
960 break;
961 case GSM_MI_TYPE_IMSI:
962 req.mi_imsi = mi;
963 req.mi_imsi_present = true;
964 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
965 break;
966 default:
967 LOGP(DL1IF, LOGL_ERROR, "Unexpected MI type %u\n", mi.type);
968 return -EINVAL;
969 }
970
971 return bts_add_paging(bts, &req, ms);
Andreas Eversberg2b914642012-07-19 13:06:26 +0200972}
973
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100974static 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 +0200975{
Philipp Maiera58ec612021-01-25 23:43:52 +0100976 struct bssgp_bvc_ctx *bctx = the_pcu->bssgp.bctx;
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200977 GprsMs *ms;
978 struct gprs_rlcmac_dl_tbf *dl_tbf;
979 struct gprs_rlcmac_ul_tbf *ul_tbf;
Harald Welte3447c4a2017-07-11 00:06:38 +0200980 struct gprs_ra_id ra_id;
981
982 gsm48_parse_ra(&ra_id, susp_req->ra_id);
983
984 LOGP(DL1IF, LOGL_INFO, "GPRS Suspend request received: TLLI=0x%08x RAI=%s\n",
985 susp_req->tlli, osmo_rai_name(&ra_id));
986
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100987 if ((ms = bts_ms_store(bts)->get_ms(susp_req->tlli))) {
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200988 /* We need to catch both pointers here since MS may become freed
989 after first tbf_free(dl_tbf) if only DL TBF was available */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100990 dl_tbf = ms_dl_tbf(ms);
991 ul_tbf = ms_ul_tbf(ms);
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200992 if (dl_tbf)
993 tbf_free(dl_tbf);
994 if (ul_tbf)
995 tbf_free(ul_tbf);
996 }
997
Harald Welte3447c4a2017-07-11 00:06:38 +0200998 if (!bctx)
999 return -1;
1000
1001 return bssgp_tx_suspend(bctx->nsei, susp_req->tlli, &ra_id);
1002}
1003
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001004static 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 +02001005{
Pau Espin Pedrol54faf022021-01-14 12:12:43 +01001006 struct llist_head *tmp;
Oliver Smithcfb63212019-09-05 17:13:33 +02001007
1008 LOGP(DL1IF, LOGL_DEBUG, "Application Information Request received: type=0x%08x len=%i\n",
1009 app_info_req->application_type, app_info_req->len);
1010
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001011 bts->app_info_pending = 0;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001012 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
Pau Espin Pedrol54faf022021-01-14 12:12:43 +01001013 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001014 if (!ms_dl_tbf(ms))
Oliver Smithcfb63212019-09-05 17:13:33 +02001015 continue;
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001016 bts->app_info_pending++;
Oliver Smithcfb63212019-09-05 17:13:33 +02001017 ms->app_info_pending = true;
1018 }
1019
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001020 if (!bts->app_info_pending) {
Oliver Smithcfb63212019-09-05 17:13:33 +02001021 LOGP(DL1IF, LOGL_NOTICE, "Packet Application Information will not be sent, no subscribers with active"
1022 " TBF\n");
1023 return -1;
1024 }
1025
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001026 if (bts->app_info) {
Oliver Smithcfb63212019-09-05 17:13:33 +02001027 LOGP(DL1IF, LOGL_NOTICE, "Previous Packet Application Information was not sent to all subscribers,"
1028 " overwriting with new one\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001029 msgb_free(bts->app_info);
Oliver Smithcfb63212019-09-05 17:13:33 +02001030 }
1031
1032 LOGP(DL1IF, LOGL_INFO, "Sending Packet Application Information to %i subscribers with active TBF\n",
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +01001033 bts->app_info_pending);
1034 bts->app_info = gprs_rlcmac_app_info_msg(app_info_req);
Oliver Smithcfb63212019-09-05 17:13:33 +02001035 return 0;
1036}
1037
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001038static int pcu_rx_neigh_addr_cnf(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_neigh_addr_cnf *naddr_cnf)
1039{
1040 struct llist_head *tmp;
1041 struct osmo_cell_global_id_ps cgi_ps;
1042 struct osmo_cell_global_id_ps *cgi_ps_ptr = &cgi_ps;
1043
1044 struct neigh_cache_entry_key neigh_key = {
1045 .local_lac = osmo_load16be(&naddr_cnf->orig_req.local_lac),
1046 .local_ci = osmo_load16be(&naddr_cnf->orig_req.local_ci),
1047 .tgt_arfcn = osmo_load16be(&naddr_cnf->orig_req.tgt_arfcn),
1048 .tgt_bsic = naddr_cnf->orig_req.tgt_bsic,
1049 };
1050
1051 if (naddr_cnf->err_code == 0) {
1052 cgi_ps.rai.lac.plmn.mcc = osmo_load16be(&naddr_cnf->cgi_ps.mcc);
1053 cgi_ps.rai.lac.plmn.mnc = osmo_load16be(&naddr_cnf->cgi_ps.mnc);
1054 cgi_ps.rai.lac.plmn.mnc_3_digits = naddr_cnf->cgi_ps.mnc_3_digits;
1055 cgi_ps.rai.lac.lac = osmo_load16be(&naddr_cnf->cgi_ps.lac);
1056 cgi_ps.rai.rac = naddr_cnf->cgi_ps.rac;
1057 cgi_ps.cell_identity = osmo_load16be(&naddr_cnf->cgi_ps.cell_identity);
1058
1059 LOGP(DL1IF, LOGL_INFO, "Rx Neighbor Address Resolution Confirmation for " NEIGH_CACHE_ENTRY_KEY_FMT ": %s\n",
1060 NEIGH_CACHE_ENTRY_KEY_ARGS(&neigh_key), osmo_cgi_ps_name(&cgi_ps));
1061
1062 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
1063 neigh_cache_add(bts->pcu->neigh_cache, &neigh_key, &cgi_ps);
1064 } else {
1065 cgi_ps_ptr = NULL;
1066 LOGP(DL1IF, LOGL_INFO, "Rx Neighbor Address Resolution Confirmation for " NEIGH_CACHE_ENTRY_KEY_FMT ": failed with err_code=%u\n",
1067 NEIGH_CACHE_ENTRY_KEY_ARGS(&neigh_key), naddr_cnf->err_code);
1068 }
1069
1070 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
1071 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
1072 if (ms->nacc && nacc_fsm_is_waiting_addr_resolution(ms->nacc, &neigh_key))
1073 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_RAC_CI, cgi_ps_ptr);
1074 }
1075 return 0;
1076}
1077
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001078static int pcu_rx_container(struct gprs_rlcmac_bts *bts, struct gsm_pcu_if_container *container)
1079{
1080 int rc;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001081 uint16_t data_length = osmo_load16be(&container->length);
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001082
1083 switch (container->msg_type) {
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +02001084 case PCU_IF_MSG_NEIGH_ADDR_CNF:
1085 if (data_length < sizeof(struct gsm_pcu_if_neigh_addr_cnf)) {
1086 LOGP(DL1IF, LOGL_ERROR, "Rx container(NEIGH_ADDR_CNF) message too short: %u vs exp %zu\n",
1087 data_length, sizeof(struct gsm_pcu_if_neigh_addr_cnf));
1088 return -EINVAL;
1089 }
1090 rc = pcu_rx_neigh_addr_cnf(bts, (struct gsm_pcu_if_neigh_addr_cnf*)&container->data);
1091 break;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001092 default:
1093 LOGP(DL1IF, LOGL_NOTICE, "(bts=%d) Rx unexpected msg type (%u) inside container!\n",
1094 bts->nr, container->msg_type);
1095 rc = -1;
1096 }
1097 return rc;
1098}
1099
1100#define CHECK_IF_MSG_SIZE(prim_len, prim_msg) \
1101 do { \
1102 size_t _len = PCUIF_HDR_SIZE + sizeof(prim_msg); \
1103 if (prim_len < _len) { \
1104 LOGP(DL1IF, LOGL_ERROR, "Received %zu bytes on PCU Socket, but primitive %s " \
1105 "size is %zu, discarding\n", prim_len, #prim_msg, _len); \
1106 return -EINVAL; \
1107 } \
1108 } while(0);
1109int pcu_rx(struct gsm_pcu_if *pcu_prim, size_t pcu_prim_length)
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001110{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001111 int rc = 0;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001112 size_t exp_len;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001113 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, pcu_prim->bts_nr);
1114 if (!bts) {
1115 LOGP(DL1IF, LOGL_NOTICE, "Received message for new BTS%d\n", pcu_prim->bts_nr);
1116 bts = bts_alloc(the_pcu, pcu_prim->bts_nr);
1117 if (!bts) {
1118 LOGP(DL1IF, LOGL_ERROR, "Failed to create object for BTS%d!\n", pcu_prim->bts_nr);
1119 return -EAGAIN;
1120 }
1121 }
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001122
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001123 switch (pcu_prim->msg_type) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001124 case PCU_IF_MSG_DATA_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001125 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001126 rc = pcu_rx_data_ind(bts, &pcu_prim->u.data_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001127 break;
Andreas Eversberga9be1542012-09-27 09:23:24 +02001128 case PCU_IF_MSG_DATA_CNF:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001129 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.data_cnf);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001130 rc = pcu_rx_data_cnf(bts, &pcu_prim->u.data_cnf);
Andreas Eversberga9be1542012-09-27 09:23:24 +02001131 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001132 case PCU_IF_MSG_RTS_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001133 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.rts_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001134 rc = pcu_rx_rts_req(bts, &pcu_prim->u.rts_req);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001135 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001136 case PCU_IF_MSG_RACH_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001137 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.rach_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001138 rc = pcu_rx_rach_ind(bts, &pcu_prim->u.rach_ind);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001139 break;
1140 case PCU_IF_MSG_INFO_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001141 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.info_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001142 rc = pcu_rx_info_ind(bts, &pcu_prim->u.info_ind);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001143 break;
1144 case PCU_IF_MSG_TIME_IND:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001145 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.time_ind);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001146 rc = pcu_rx_time_ind(bts, &pcu_prim->u.time_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001147 break;
Andreas Eversberg2b914642012-07-19 13:06:26 +02001148 case PCU_IF_MSG_PAG_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001149 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.pag_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001150 rc = pcu_rx_pag_req(bts, &pcu_prim->u.pag_req);
Andreas Eversberg2b914642012-07-19 13:06:26 +02001151 break;
Harald Welte3447c4a2017-07-11 00:06:38 +02001152 case PCU_IF_MSG_SUSP_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001153 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.susp_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001154 rc = pcu_rx_susp_req(bts, &pcu_prim->u.susp_req);
Harald Welte3447c4a2017-07-11 00:06:38 +02001155 break;
Oliver Smithcfb63212019-09-05 17:13:33 +02001156 case PCU_IF_MSG_APP_INFO_REQ:
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001157 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.app_info_req);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001158 rc = pcu_rx_app_info_req(bts, &pcu_prim->u.app_info_req);
Oliver Smithcfb63212019-09-05 17:13:33 +02001159 break;
Vadim Yanitskiyb6572132021-06-20 21:05:24 +02001160 case PCU_IF_MSG_INTERF_IND:
1161 /* TODO: handle interference reports */
1162 break;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001163 case PCU_IF_MSG_CONTAINER:
1164 CHECK_IF_MSG_SIZE(pcu_prim_length, pcu_prim->u.container);
1165 /* ^ check if we can access container fields, v check with container data length */
1166 exp_len = PCUIF_HDR_SIZE + sizeof(pcu_prim->u.container) + osmo_load16be(&pcu_prim->u.container.length);
1167 if (pcu_prim_length < exp_len) {
1168 LOGP(DL1IF, LOGL_ERROR, "Received %zu bytes on PCU Socket, but primitive container size" \
1169 "is %zu, discarding\n", pcu_prim_length, exp_len);
1170 }
1171 rc = pcu_rx_container(bts, &pcu_prim->u.container);
1172 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001173 default:
Thorsten Alteholz8bb79042018-10-06 10:42:58 +02001174 LOGP(DL1IF, LOGL_ERROR, "Received unknown PCU msg type %d\n",
Pau Espin Pedrol1989a192021-06-23 19:46:07 +02001175 pcu_prim->msg_type);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001176 rc = -EINVAL;
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001177 }
Philipp Maier8ccf7042018-04-10 13:12:42 +02001178
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001179 return rc;
Andreas Eversberg0aed6542012-06-23 10:33:16 +02001180}