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