blob: 033426b1764f7b1c7c8623fd8c7756aa74df3537 [file] [log] [blame]
Pau Espin Pedrol3a271022021-01-21 18:44:23 +01001/* gprs_bssgp_pcu.cpp
2 *
3 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 *
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.
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010014 */
15
16#include <osmocom/gprs/gprs_bssgp.h>
17#include <osmocom/gprs/gprs_bssgp_rim.h>
18#include <osmocom/core/msgb.h>
19#include <osmocom/gsm/tlv.h>
20#include <osmocom/gprs/gprs_ns.h>
21#include <osmocom/core/prim.h>
Philipp Maiera58ec612021-01-25 23:43:52 +010022#include <pcu_l1_if.h>
23#include <gprs_rlcmac.h>
24#include <bts.h>
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010025
26#include "gprs_debug.h"
27#include "gprs_pcu.h"
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010028#include "bts.h"
29#include "gprs_ms.h"
30#include "nacc_fsm.h"
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010031
32#define LOGPRIM(nsei, level, fmt, args...) \
33 LOGP(DRIM, level, "(NSEI=%u) " fmt, nsei, ## args)
34
35static inline void gprs_ra_id_ci_to_cgi_ps(struct osmo_cell_global_id_ps *cgi_ps,
36 const struct gprs_ra_id *raid, uint16_t cid)
37{
38 *cgi_ps = (struct osmo_cell_global_id_ps) {
39 .rai.lac.plmn.mcc = raid->mcc,
40 .rai.lac.plmn.mnc = raid->mnc,
41 .rai.lac.plmn.mnc_3_digits = raid->mnc_3_digits,
42 .rai.lac.lac = raid->lac,
43 .rai.rac = raid->rac,
44 .cell_identity = cid,
45 };
46}
47
48/* Mirror RIM routing information of a given PDU, see also 3GPP TS 48.018, section 8c.1.4.3 */
49static void mirror_rim_routing_info(struct bssgp_ran_information_pdu *to_pdu,
50 const struct bssgp_ran_information_pdu *from_pdu)
51{
52 memcpy(&to_pdu->routing_info_dest, &from_pdu->routing_info_src, sizeof(to_pdu->routing_info_dest));
53 memcpy(&to_pdu->routing_info_src, &from_pdu->routing_info_dest, sizeof(to_pdu->routing_info_src));
54}
55
Philipp Maiera58ec612021-01-25 23:43:52 +010056/* Fill NACC application container with data (cell identifier, sysinfo) */
57#define SI_HDR_LEN 2
58static void fill_app_cont_nacc(struct bssgp_ran_inf_app_cont_nacc *app_cont, const struct gprs_rlcmac_bts *bts)
59{
60 struct bssgp_bvc_ctx *bctx = the_pcu->bssgp.bctx;
61
62 gprs_ra_id_ci_to_cgi_ps(&app_cont->reprt_cell, &bctx->ra_id, bctx->cell_id);
63 app_cont->num_si = 0;
64
65 /* Note: The BTS struct stores the system information including its pseudo header. The RIM application
66 * container defines the system information without pseudo header, so we need to chop it off. */
67
68 if (bts->si1_is_set) {
69 app_cont->si[app_cont->num_si] = bts->si1 + SI_HDR_LEN;
70 app_cont->num_si++;
71 }
72
73 if (bts->si3_is_set) {
74 app_cont->si[app_cont->num_si] = bts->si3 + SI_HDR_LEN;
75 app_cont->num_si++;
76 }
77
78 if (bts->si13_is_set) {
79 app_cont->si[app_cont->num_si] = bts->si13 + SI_HDR_LEN;
80 app_cont->num_si++;
81 }
82
83 /* Note: It is possible that the resulting PDU will not contain any system information, even if this is
84 * an unlikely case since the BTS immediately updates the system information after startup. The
85 * specification permits to send zero system information, see also: 3GPP TS 48.018 section 11.3.63.2.1 */
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +020086
87 if (!bts->si1_is_set || !bts->si3_is_set || !bts->si13_is_set)
88 LOGP(DNACC, LOGL_INFO, "TX RAN INFO RESPONSE (NACC) %s: Some SI are missing:%s%s%s\n",
89 osmo_cgi_ps_name(&app_cont->reprt_cell),
90 bts->si1_is_set ? "" : " SI1",
91 bts->si3_is_set ? "" : " SI3",
92 bts->si13_is_set ? "" : " SI13");
Philipp Maiera58ec612021-01-25 23:43:52 +010093}
94
95/* Format a RAN INFORMATION PDU that contains the requested system information */
Pau Espin Pedrol0e35aee2021-05-06 19:47:15 +020096static void format_response_pdu(struct bssgp_ran_information_pdu *resp_pdu,
97 const struct bssgp_ran_information_pdu *req_pdu,
Philipp Maiera58ec612021-01-25 23:43:52 +010098 const struct gprs_rlcmac_bts *bts)
99{
100 memset(resp_pdu, 0, sizeof(*resp_pdu));
101 mirror_rim_routing_info(resp_pdu, req_pdu);
102
103 resp_pdu->decoded.rim_cont = (struct bssgp_ran_inf_rim_cont) {
104 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
105 .seq_num = 1, /* single report has only one message in response */
106 .pdu_ind = {
107 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
108 },
109 .prot_ver = 1,
110 };
111
112 fill_app_cont_nacc(&resp_pdu->decoded.rim_cont.u.app_cont_nacc, bts);
113 resp_pdu->decoded_present = true;
114 resp_pdu->rim_cont_iei = BSSGP_IE_RI_RIM_CONTAINER;
115}
116
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100117/* Format a RAN INFORMATION ERROR PDU */
118static void format_response_pdu_err(struct bssgp_ran_information_pdu *resp_pdu,
119 const struct bssgp_ran_information_pdu *req_pdu)
120{
121 memset(resp_pdu, 0, sizeof(*resp_pdu));
122 mirror_rim_routing_info(resp_pdu, req_pdu);
123
124 resp_pdu->decoded.err_rim_cont = (struct bssgp_ran_inf_err_rim_cont) {
125 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
126 .prot_ver = 1,
127 .err_pdu = req_pdu->rim_cont,
128 .err_pdu_len = req_pdu->rim_cont_len,
129 };
130
131 resp_pdu->decoded_present = true;
132 resp_pdu->rim_cont_iei = BSSGP_IE_RI_ERROR_RIM_COINTAINER;
133}
134
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100135static int handle_ran_info_response_nacc(const struct bssgp_ran_inf_app_cont_nacc *nacc, struct gprs_rlcmac_bts *bts)
136{
137 struct si_cache_value val;
138 struct si_cache_entry *entry;
139 struct llist_head *tmp;
140 int i;
141
142 LOGP(DRIM, LOGL_INFO, "Rx RAN-INFO cell=%s type=%sBCCH num_si=%d\n",
143 osmo_cgi_ps_name(&nacc->reprt_cell),
144 nacc->type_psi ? "P" : "", nacc->num_si);
145
146 val.type_psi = nacc->type_psi;
147 val.si_len = 0;
148 for (i = 0; i < nacc->num_si; i++) {
149 size_t len = val.type_psi ? BSSGP_RIM_PSI_LEN : BSSGP_RIM_SI_LEN;
150 memcpy(&val.si_buf[val.si_len], nacc->si[i], len);
151 val.si_len += len;
152 }
153 entry = si_cache_add(bts->pcu->si_cache, &nacc->reprt_cell, &val);
154
155 llist_for_each(tmp, bts_ms_list(bts)) {
156 struct GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrol6c81add2021-09-07 14:43:46 +0200157 if (ms->nacc && nacc_fsm_is_waiting_si_resolution(ms->nacc, &nacc->reprt_cell))
158 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_SI, entry);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100159 }
160 return 0;
161}
162
Pau Espin Pedrolc43570c2021-05-06 19:48:05 +0200163static int handle_ran_info_request(const struct bssgp_ran_information_pdu *pdu,
164 struct gprs_rlcmac_bts *bts, uint16_t nsei)
165{
166 const struct bssgp_ran_inf_req_rim_cont *ri_req = &pdu->decoded.req_rim_cont;
167 const struct bssgp_ran_inf_req_app_cont_nacc *nacc_req;
168 struct bssgp_ran_information_pdu resp_pdu;
169 int rc;
170
171 /* Check if we support the application ID */
172 if (ri_req->app_id != BSSGP_RAN_INF_APP_ID_NACC) {
173 LOGPRIM(nsei, LOGL_ERROR,
174 "Rx RAN-INFO-REQ for cell %s with unknown/wrong application ID %s -- reject.\n",
175 osmo_cgi_ps_name(&bts->cgi_ps), bssgp_ran_inf_app_id_str(ri_req->app_id));
176 format_response_pdu_err(&resp_pdu, pdu);
177 rc = -ENOTSUP;
178 goto tx_ret;
179 }
180
181 nacc_req = &ri_req->u.app_cont_nacc;
182 rc = osmo_cgi_ps_cmp(&bts->cgi_ps, &nacc_req->reprt_cell);
183 if (rc != 0) {
184 LOGPRIM(nsei, LOGL_ERROR, "reporting cell in RIM application container %s "
185 "does not match destination cell in RIM routing info %s -- rejected.\n",
186 osmo_cgi_ps_name(&nacc_req->reprt_cell),
187 osmo_cgi_ps_name2(&bts->cgi_ps));
188 format_response_pdu_err(&resp_pdu, pdu);
189 } else {
190 LOGPRIM(nsei, LOGL_INFO, "Responding to RAN INFORMATION REQUEST %s ...\n",
191 osmo_cgi_ps_name(&nacc_req->reprt_cell));
192 format_response_pdu(&resp_pdu, pdu, bts);
193 }
194
195tx_ret:
196 bssgp_tx_rim(&resp_pdu, nsei);
197 return rc;
198}
199
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100200static int handle_ran_info_response(const struct bssgp_ran_information_pdu *pdu, struct gprs_rlcmac_bts *bts)
201{
202 const struct bssgp_ran_inf_rim_cont *ran_info = &pdu->decoded.rim_cont;
203 char ri_src_str[64];
204
Pau Espin Pedrolc43570c2021-05-06 19:48:05 +0200205 /* Check if we support the application ID */
206 if (ran_info->app_id != BSSGP_RAN_INF_APP_ID_NACC) {
207 LOGP(DRIM, LOGL_ERROR,
208 "Rx RAN-INFO for cell %s with unknown/wrong application ID %s received -- reject.\n",
209 osmo_cgi_ps_name(&bts->cgi_ps), bssgp_ran_inf_app_id_str(ran_info->app_id));
210 return -1;
211 }
212
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100213 if (ran_info->app_err) {
214 LOGP(DRIM, LOGL_ERROR,
215 "%s Rx RAN-INFO with an app error! cause: %s\n",
216 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
217 bssgp_nacc_cause_str(ran_info->u.app_err_cont_nacc.nacc_cause));
218 return -1;
219 }
220
Pau Espin Pedrolc43570c2021-05-06 19:48:05 +0200221 handle_ran_info_response_nacc(&ran_info->u.app_cont_nacc, bts);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100222 return 0;
223}
224
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100225int handle_rim(struct osmo_bssgp_prim *bp)
226{
227 struct msgb *msg = bp->oph.msg;
228 uint16_t nsei = msgb_nsei(msg);
229 struct bssgp_ran_information_pdu *pdu = &bp->u.rim_pdu;
230 struct bssgp_ran_information_pdu resp_pdu;
231 struct osmo_cell_global_id_ps dst_addr;
232 struct gprs_rlcmac_bts *bts;
233
234 OSMO_ASSERT (bp->oph.sap == SAP_BSSGP_RIM);
235
236 /* At the moment we only support GERAN, so we block all other network
237 * types here. */
238 if (pdu->routing_info_dest.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
239 LOGPRIM(nsei, LOGL_ERROR,
240 "Only GERAN supported, destination cell is not a GERAN cell -- rejected.\n");
241 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
242 }
243
244 /* Check if the RIM pdu is really addressed to this PCU. In case we
245 * receive a RIM PDU for a cell that is not parented by this PCU we
246 * are supposed to reject it with a BSSGP STATUS.
247 * see also: 3GPP TS 48.018, section 8c.3.1.2 */
248 gprs_ra_id_ci_to_cgi_ps(&dst_addr, &pdu->routing_info_dest.geran.raid,
249 pdu->routing_info_dest.geran.cid);
250 bts = gprs_pcu_get_bts_by_cgi_ps(the_pcu, &dst_addr);
251 if (!bts) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200252 LOGPRIM(nsei, LOGL_ERROR, "Destination cell %s unknown to this pcu\n",
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100253 osmo_cgi_ps_name(&dst_addr));
254 return bssgp_tx_status(BSSGP_CAUSE_UNKN_DST, NULL, msg);
255 }
256
257 /* Check if the incoming RIM PDU is parseable, if not we must report
258 * an error to the controlling BSS 3GPP TS 48.018, 8c.3.4 and 8c.3.4.2 */
259 if (!pdu->decoded_present) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200260 LOGPRIM(nsei, LOGL_ERROR, "Erroneous RIM PDU received for cell %s -- reject.\n",
261 osmo_cgi_ps_name(&dst_addr));
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100262 format_response_pdu_err(&resp_pdu, pdu);
263 return 0;
264 }
265
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100266 /* Handle incoming RIM container */
267 switch (pdu->rim_cont_iei) {
268 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
Pau Espin Pedrolc43570c2021-05-06 19:48:05 +0200269 return handle_ran_info_request(pdu, bts, nsei);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100270 case BSSGP_IE_RI_RIM_CONTAINER:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100271 return handle_ran_info_response(pdu, bts);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100272 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
273 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
274 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
275 LOGPRIM(nsei, LOGL_ERROR, "RIM PDU not handled by this application\n");
276 return -EINVAL;
277 default:
278 /* This should never happen. If the RIM PDU is parsed correctly, then the rim_cont_iei will
279 * be set to one of the cases above and if parsing fails this switch statement is guarded
280 * by the check on decoded_present above */
281 OSMO_ASSERT(false);
282 }
283
284 return 0;
285}