blob: e9b9ef8b007afe8655c3cfb256bab7f96ae5605f [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
139/* Check if the application ID in the request PDU is actually BSSGP_RAN_INF_APP_ID_NACC */
140static const enum bssgp_ran_inf_app_id *get_app_id(const struct bssgp_ran_information_pdu *pdu)
141{
142 switch (pdu->rim_cont_iei) {
143 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
144 return &pdu->decoded.req_rim_cont.app_id;
145 case BSSGP_IE_RI_RIM_CONTAINER:
146 return &pdu->decoded.rim_cont.app_id;
147 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
148 return &pdu->decoded.app_err_rim_cont.app_id;
149 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
150 return &pdu->decoded.ack_rim_cont.app_id;
151 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
152 return &pdu->decoded.err_rim_cont.app_id;
153 default:
154 return NULL;
155 }
156}
157
158/* Check if the application ID in the request PDU is of a certain type */
159static bool match_app_id(const struct bssgp_ran_information_pdu *pdu, enum bssgp_ran_inf_app_id exp_app_id)
160{
161 const enum bssgp_ran_inf_app_id *app_id = get_app_id(pdu);
162 if (app_id && *app_id == exp_app_id)
163 return true;
164 return false;
165}
166
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100167static int handle_ran_info_response_nacc(const struct bssgp_ran_inf_app_cont_nacc *nacc, struct gprs_rlcmac_bts *bts)
168{
169 struct si_cache_value val;
170 struct si_cache_entry *entry;
171 struct llist_head *tmp;
172 int i;
173
174 LOGP(DRIM, LOGL_INFO, "Rx RAN-INFO cell=%s type=%sBCCH num_si=%d\n",
175 osmo_cgi_ps_name(&nacc->reprt_cell),
176 nacc->type_psi ? "P" : "", nacc->num_si);
177
178 val.type_psi = nacc->type_psi;
179 val.si_len = 0;
180 for (i = 0; i < nacc->num_si; i++) {
181 size_t len = val.type_psi ? BSSGP_RIM_PSI_LEN : BSSGP_RIM_SI_LEN;
182 memcpy(&val.si_buf[val.si_len], nacc->si[i], len);
183 val.si_len += len;
184 }
185 entry = si_cache_add(bts->pcu->si_cache, &nacc->reprt_cell, &val);
186
187 llist_for_each(tmp, bts_ms_list(bts)) {
188 struct GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
189 if (!ms->nacc)
190 continue;
191 if (ms->nacc->fi->state != NACC_ST_WAIT_REQUEST_SI)
192 continue;
193 if (osmo_cgi_ps_cmp(&nacc->reprt_cell, &ms->nacc->cgi_ps) != 0)
194 continue;
195 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_SI, entry);
196 }
197 return 0;
198}
199
200static 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
205 if (ran_info->app_err) {
206 LOGP(DRIM, LOGL_ERROR,
207 "%s Rx RAN-INFO with an app error! cause: %s\n",
208 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
209 bssgp_nacc_cause_str(ran_info->u.app_err_cont_nacc.nacc_cause));
210 return -1;
211 }
212
213 switch (pdu->decoded.rim_cont.app_id) {
214 case BSSGP_RAN_INF_APP_ID_NACC:
215 handle_ran_info_response_nacc(&ran_info->u.app_cont_nacc, bts);
216 break;
217 default:
218 LOGP(DRIM, LOGL_ERROR, "%s Rx RAN-INFO with unknown/wrong application ID %s received\n",
219 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
220 bssgp_ran_inf_app_id_str(pdu->decoded.rim_cont.app_id));
221 return -1;
222 }
223 return 0;
224}
225
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100226int handle_rim(struct osmo_bssgp_prim *bp)
227{
228 struct msgb *msg = bp->oph.msg;
229 uint16_t nsei = msgb_nsei(msg);
230 struct bssgp_ran_information_pdu *pdu = &bp->u.rim_pdu;
231 struct bssgp_ran_information_pdu resp_pdu;
232 struct osmo_cell_global_id_ps dst_addr;
233 struct gprs_rlcmac_bts *bts;
Philipp Maiera58ec612021-01-25 23:43:52 +0100234 int rc;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100235
236 OSMO_ASSERT (bp->oph.sap == SAP_BSSGP_RIM);
237
238 /* At the moment we only support GERAN, so we block all other network
239 * types here. */
240 if (pdu->routing_info_dest.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
241 LOGPRIM(nsei, LOGL_ERROR,
242 "Only GERAN supported, destination cell is not a GERAN cell -- rejected.\n");
243 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
244 }
245
246 /* Check if the RIM pdu is really addressed to this PCU. In case we
247 * receive a RIM PDU for a cell that is not parented by this PCU we
248 * are supposed to reject it with a BSSGP STATUS.
249 * see also: 3GPP TS 48.018, section 8c.3.1.2 */
250 gprs_ra_id_ci_to_cgi_ps(&dst_addr, &pdu->routing_info_dest.geran.raid,
251 pdu->routing_info_dest.geran.cid);
252 bts = gprs_pcu_get_bts_by_cgi_ps(the_pcu, &dst_addr);
253 if (!bts) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200254 LOGPRIM(nsei, LOGL_ERROR, "Destination cell %s unknown to this pcu\n",
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100255 osmo_cgi_ps_name(&dst_addr));
256 return bssgp_tx_status(BSSGP_CAUSE_UNKN_DST, NULL, msg);
257 }
258
259 /* Check if the incoming RIM PDU is parseable, if not we must report
260 * an error to the controlling BSS 3GPP TS 48.018, 8c.3.4 and 8c.3.4.2 */
261 if (!pdu->decoded_present) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200262 LOGPRIM(nsei, LOGL_ERROR, "Erroneous RIM PDU received for cell %s -- reject.\n",
263 osmo_cgi_ps_name(&dst_addr));
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100264 format_response_pdu_err(&resp_pdu, pdu);
265 return 0;
266 }
267
268 /* Check if the RIM container inside the incoming RIM PDU has the correct
269 * application ID */
270 if (!match_app_id(pdu, BSSGP_RAN_INF_APP_ID_NACC)) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200271 LOGPRIM(nsei, LOGL_ERROR,
272 "RIM PDU for cell %s with unknown/wrong application ID received -- reject.\n",
273 osmo_cgi_ps_name(&dst_addr));
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100274 format_response_pdu_err(&resp_pdu, pdu);
275 return 0;
276 }
277
278 /* Handle incoming RIM container */
279 switch (pdu->rim_cont_iei) {
280 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
Philipp Maiera58ec612021-01-25 23:43:52 +0100281 rc = osmo_cgi_ps_cmp(&dst_addr, &pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell);
282 if (rc != 0) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200283 LOGPRIM(nsei, LOGL_ERROR, "reporting cell in RIM application container %s "
284 "does not match destination cell in RIM routing info %s -- rejected.\n",
285 osmo_cgi_ps_name(&pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell),
286 osmo_cgi_ps_name2(&dst_addr));
Philipp Maiera58ec612021-01-25 23:43:52 +0100287 format_response_pdu_err(&resp_pdu, pdu);
288 } else {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200289 LOGPRIM(nsei, LOGL_INFO, "Responding to RAN INFORMATION REQUEST %s ...\n",
290 osmo_cgi_ps_name(&pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell));
Philipp Maiera58ec612021-01-25 23:43:52 +0100291 format_response_pdu(&resp_pdu, pdu, bts);
292 }
293 bssgp_tx_rim(&resp_pdu, nsei);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100294 break;
295 case BSSGP_IE_RI_RIM_CONTAINER:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100296 return handle_ran_info_response(pdu, bts);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100297 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
298 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
299 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
300 LOGPRIM(nsei, LOGL_ERROR, "RIM PDU not handled by this application\n");
301 return -EINVAL;
302 default:
303 /* This should never happen. If the RIM PDU is parsed correctly, then the rim_cont_iei will
304 * be set to one of the cases above and if parsing fails this switch statement is guarded
305 * by the check on decoded_present above */
306 OSMO_ASSERT(false);
307 }
308
309 return 0;
310}