blob: c1f9cec5a169f2798f500f65f668aa90f6c59bfa [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 */
100static void format_response_pdu(struct bssgp_ran_information_pdu *resp_pdu, struct bssgp_ran_information_pdu *req_pdu,
101 const struct gprs_rlcmac_bts *bts)
102{
103 memset(resp_pdu, 0, sizeof(*resp_pdu));
104 mirror_rim_routing_info(resp_pdu, req_pdu);
105
106 resp_pdu->decoded.rim_cont = (struct bssgp_ran_inf_rim_cont) {
107 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
108 .seq_num = 1, /* single report has only one message in response */
109 .pdu_ind = {
110 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
111 },
112 .prot_ver = 1,
113 };
114
115 fill_app_cont_nacc(&resp_pdu->decoded.rim_cont.u.app_cont_nacc, bts);
116 resp_pdu->decoded_present = true;
117 resp_pdu->rim_cont_iei = BSSGP_IE_RI_RIM_CONTAINER;
118}
119
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100120/* Format a RAN INFORMATION ERROR PDU */
121static void format_response_pdu_err(struct bssgp_ran_information_pdu *resp_pdu,
122 const struct bssgp_ran_information_pdu *req_pdu)
123{
124 memset(resp_pdu, 0, sizeof(*resp_pdu));
125 mirror_rim_routing_info(resp_pdu, req_pdu);
126
127 resp_pdu->decoded.err_rim_cont = (struct bssgp_ran_inf_err_rim_cont) {
128 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
129 .prot_ver = 1,
130 .err_pdu = req_pdu->rim_cont,
131 .err_pdu_len = req_pdu->rim_cont_len,
132 };
133
134 resp_pdu->decoded_present = true;
135 resp_pdu->rim_cont_iei = BSSGP_IE_RI_ERROR_RIM_COINTAINER;
136}
137
138/* Check if the application ID in the request PDU is actually BSSGP_RAN_INF_APP_ID_NACC */
139static const enum bssgp_ran_inf_app_id *get_app_id(const struct bssgp_ran_information_pdu *pdu)
140{
141 switch (pdu->rim_cont_iei) {
142 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
143 return &pdu->decoded.req_rim_cont.app_id;
144 case BSSGP_IE_RI_RIM_CONTAINER:
145 return &pdu->decoded.rim_cont.app_id;
146 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
147 return &pdu->decoded.app_err_rim_cont.app_id;
148 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
149 return &pdu->decoded.ack_rim_cont.app_id;
150 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
151 return &pdu->decoded.err_rim_cont.app_id;
152 default:
153 return NULL;
154 }
155}
156
157/* Check if the application ID in the request PDU is of a certain type */
158static bool match_app_id(const struct bssgp_ran_information_pdu *pdu, enum bssgp_ran_inf_app_id exp_app_id)
159{
160 const enum bssgp_ran_inf_app_id *app_id = get_app_id(pdu);
161 if (app_id && *app_id == exp_app_id)
162 return true;
163 return false;
164}
165
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100166static int handle_ran_info_response_nacc(const struct bssgp_ran_inf_app_cont_nacc *nacc, struct gprs_rlcmac_bts *bts)
167{
168 struct si_cache_value val;
169 struct si_cache_entry *entry;
170 struct llist_head *tmp;
171 int i;
172
173 LOGP(DRIM, LOGL_INFO, "Rx RAN-INFO cell=%s type=%sBCCH num_si=%d\n",
174 osmo_cgi_ps_name(&nacc->reprt_cell),
175 nacc->type_psi ? "P" : "", nacc->num_si);
176
177 val.type_psi = nacc->type_psi;
178 val.si_len = 0;
179 for (i = 0; i < nacc->num_si; i++) {
180 size_t len = val.type_psi ? BSSGP_RIM_PSI_LEN : BSSGP_RIM_SI_LEN;
181 memcpy(&val.si_buf[val.si_len], nacc->si[i], len);
182 val.si_len += len;
183 }
184 entry = si_cache_add(bts->pcu->si_cache, &nacc->reprt_cell, &val);
185
186 llist_for_each(tmp, bts_ms_list(bts)) {
187 struct GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
188 if (!ms->nacc)
189 continue;
190 if (ms->nacc->fi->state != NACC_ST_WAIT_REQUEST_SI)
191 continue;
192 if (osmo_cgi_ps_cmp(&nacc->reprt_cell, &ms->nacc->cgi_ps) != 0)
193 continue;
194 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_SI, entry);
195 }
196 return 0;
197}
198
199static int handle_ran_info_response(const struct bssgp_ran_information_pdu *pdu, struct gprs_rlcmac_bts *bts)
200{
201 const struct bssgp_ran_inf_rim_cont *ran_info = &pdu->decoded.rim_cont;
202 char ri_src_str[64];
203
204 if (ran_info->app_err) {
205 LOGP(DRIM, LOGL_ERROR,
206 "%s Rx RAN-INFO with an app error! cause: %s\n",
207 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
208 bssgp_nacc_cause_str(ran_info->u.app_err_cont_nacc.nacc_cause));
209 return -1;
210 }
211
212 switch (pdu->decoded.rim_cont.app_id) {
213 case BSSGP_RAN_INF_APP_ID_NACC:
214 handle_ran_info_response_nacc(&ran_info->u.app_cont_nacc, bts);
215 break;
216 default:
217 LOGP(DRIM, LOGL_ERROR, "%s Rx RAN-INFO with unknown/wrong application ID %s received\n",
218 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
219 bssgp_ran_inf_app_id_str(pdu->decoded.rim_cont.app_id));
220 return -1;
221 }
222 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;
Philipp Maiera58ec612021-01-25 23:43:52 +0100233 int rc;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100234
235 OSMO_ASSERT (bp->oph.sap == SAP_BSSGP_RIM);
236
237 /* At the moment we only support GERAN, so we block all other network
238 * types here. */
239 if (pdu->routing_info_dest.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
240 LOGPRIM(nsei, LOGL_ERROR,
241 "Only GERAN supported, destination cell is not a GERAN cell -- rejected.\n");
242 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
243 }
244
245 /* Check if the RIM pdu is really addressed to this PCU. In case we
246 * receive a RIM PDU for a cell that is not parented by this PCU we
247 * are supposed to reject it with a BSSGP STATUS.
248 * see also: 3GPP TS 48.018, section 8c.3.1.2 */
249 gprs_ra_id_ci_to_cgi_ps(&dst_addr, &pdu->routing_info_dest.geran.raid,
250 pdu->routing_info_dest.geran.cid);
251 bts = gprs_pcu_get_bts_by_cgi_ps(the_pcu, &dst_addr);
252 if (!bts) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200253 LOGPRIM(nsei, LOGL_ERROR, "Destination cell %s unknown to this pcu\n",
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100254 osmo_cgi_ps_name(&dst_addr));
255 return bssgp_tx_status(BSSGP_CAUSE_UNKN_DST, NULL, msg);
256 }
257
258 /* Check if the incoming RIM PDU is parseable, if not we must report
259 * an error to the controlling BSS 3GPP TS 48.018, 8c.3.4 and 8c.3.4.2 */
260 if (!pdu->decoded_present) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200261 LOGPRIM(nsei, LOGL_ERROR, "Erroneous RIM PDU received for cell %s -- reject.\n",
262 osmo_cgi_ps_name(&dst_addr));
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100263 format_response_pdu_err(&resp_pdu, pdu);
264 return 0;
265 }
266
267 /* Check if the RIM container inside the incoming RIM PDU has the correct
268 * application ID */
269 if (!match_app_id(pdu, BSSGP_RAN_INF_APP_ID_NACC)) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200270 LOGPRIM(nsei, LOGL_ERROR,
271 "RIM PDU for cell %s with unknown/wrong application ID received -- reject.\n",
272 osmo_cgi_ps_name(&dst_addr));
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100273 format_response_pdu_err(&resp_pdu, pdu);
274 return 0;
275 }
276
277 /* Handle incoming RIM container */
278 switch (pdu->rim_cont_iei) {
279 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
Philipp Maiera58ec612021-01-25 23:43:52 +0100280 rc = osmo_cgi_ps_cmp(&dst_addr, &pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell);
281 if (rc != 0) {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200282 LOGPRIM(nsei, LOGL_ERROR, "reporting cell in RIM application container %s "
283 "does not match destination cell in RIM routing info %s -- rejected.\n",
284 osmo_cgi_ps_name(&pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell),
285 osmo_cgi_ps_name2(&dst_addr));
Philipp Maiera58ec612021-01-25 23:43:52 +0100286 format_response_pdu_err(&resp_pdu, pdu);
287 } else {
Pau Espin Pedrolab3aca62021-04-19 17:23:45 +0200288 LOGPRIM(nsei, LOGL_INFO, "Responding to RAN INFORMATION REQUEST %s ...\n",
289 osmo_cgi_ps_name(&pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell));
Philipp Maiera58ec612021-01-25 23:43:52 +0100290 format_response_pdu(&resp_pdu, pdu, bts);
291 }
292 bssgp_tx_rim(&resp_pdu, nsei);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100293 break;
294 case BSSGP_IE_RI_RIM_CONTAINER:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100295 return handle_ran_info_response(pdu, bts);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100296 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
297 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
298 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
299 LOGPRIM(nsei, LOGL_ERROR, "RIM PDU not handled by this application\n");
300 return -EINVAL;
301 default:
302 /* This should never happen. If the RIM PDU is parsed correctly, then the rim_cont_iei will
303 * be set to one of the cases above and if parsing fails this switch statement is guarded
304 * by the check on decoded_present above */
305 OSMO_ASSERT(false);
306 }
307
308 return 0;
309}