Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 1 | /* 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 Maier | a58ec61 | 2021-01-25 23:43:52 +0100 | [diff] [blame] | 26 | #include <pcu_l1_if.h> |
| 27 | #include <gprs_rlcmac.h> |
| 28 | #include <bts.h> |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | #include "gprs_debug.h" |
| 31 | #include "gprs_pcu.h" |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 32 | #include "bts.h" |
| 33 | #include "gprs_ms.h" |
| 34 | #include "nacc_fsm.h" |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | #define LOGPRIM(nsei, level, fmt, args...) \ |
| 37 | LOGP(DRIM, level, "(NSEI=%u) " fmt, nsei, ## args) |
| 38 | |
| 39 | static 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 */ |
| 53 | static 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 Maier | a58ec61 | 2021-01-25 23:43:52 +0100 | [diff] [blame] | 60 | /* Fill NACC application container with data (cell identifier, sysinfo) */ |
| 61 | #define SI_HDR_LEN 2 |
| 62 | static 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 */ |
| 90 | } |
| 91 | |
| 92 | /* Format a RAN INFORMATION PDU that contains the requested system information */ |
| 93 | static void format_response_pdu(struct bssgp_ran_information_pdu *resp_pdu, struct bssgp_ran_information_pdu *req_pdu, |
| 94 | const struct gprs_rlcmac_bts *bts) |
| 95 | { |
| 96 | memset(resp_pdu, 0, sizeof(*resp_pdu)); |
| 97 | mirror_rim_routing_info(resp_pdu, req_pdu); |
| 98 | |
| 99 | resp_pdu->decoded.rim_cont = (struct bssgp_ran_inf_rim_cont) { |
| 100 | .app_id = BSSGP_RAN_INF_APP_ID_NACC, |
| 101 | .seq_num = 1, /* single report has only one message in response */ |
| 102 | .pdu_ind = { |
| 103 | .pdu_type_ext = RIM_PDU_TYPE_SING_REP, |
| 104 | }, |
| 105 | .prot_ver = 1, |
| 106 | }; |
| 107 | |
| 108 | fill_app_cont_nacc(&resp_pdu->decoded.rim_cont.u.app_cont_nacc, bts); |
| 109 | resp_pdu->decoded_present = true; |
| 110 | resp_pdu->rim_cont_iei = BSSGP_IE_RI_RIM_CONTAINER; |
| 111 | } |
| 112 | |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 113 | /* Format a RAN INFORMATION ERROR PDU */ |
| 114 | static void format_response_pdu_err(struct bssgp_ran_information_pdu *resp_pdu, |
| 115 | const struct bssgp_ran_information_pdu *req_pdu) |
| 116 | { |
| 117 | memset(resp_pdu, 0, sizeof(*resp_pdu)); |
| 118 | mirror_rim_routing_info(resp_pdu, req_pdu); |
| 119 | |
| 120 | resp_pdu->decoded.err_rim_cont = (struct bssgp_ran_inf_err_rim_cont) { |
| 121 | .app_id = BSSGP_RAN_INF_APP_ID_NACC, |
| 122 | .prot_ver = 1, |
| 123 | .err_pdu = req_pdu->rim_cont, |
| 124 | .err_pdu_len = req_pdu->rim_cont_len, |
| 125 | }; |
| 126 | |
| 127 | resp_pdu->decoded_present = true; |
| 128 | resp_pdu->rim_cont_iei = BSSGP_IE_RI_ERROR_RIM_COINTAINER; |
| 129 | } |
| 130 | |
| 131 | /* Check if the application ID in the request PDU is actually BSSGP_RAN_INF_APP_ID_NACC */ |
| 132 | static const enum bssgp_ran_inf_app_id *get_app_id(const struct bssgp_ran_information_pdu *pdu) |
| 133 | { |
| 134 | switch (pdu->rim_cont_iei) { |
| 135 | case BSSGP_IE_RI_REQ_RIM_CONTAINER: |
| 136 | return &pdu->decoded.req_rim_cont.app_id; |
| 137 | case BSSGP_IE_RI_RIM_CONTAINER: |
| 138 | return &pdu->decoded.rim_cont.app_id; |
| 139 | case BSSGP_IE_RI_APP_ERROR_RIM_CONT: |
| 140 | return &pdu->decoded.app_err_rim_cont.app_id; |
| 141 | case BSSGP_IE_RI_ACK_RIM_CONTAINER: |
| 142 | return &pdu->decoded.ack_rim_cont.app_id; |
| 143 | case BSSGP_IE_RI_ERROR_RIM_COINTAINER: |
| 144 | return &pdu->decoded.err_rim_cont.app_id; |
| 145 | default: |
| 146 | return NULL; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* Check if the application ID in the request PDU is of a certain type */ |
| 151 | static bool match_app_id(const struct bssgp_ran_information_pdu *pdu, enum bssgp_ran_inf_app_id exp_app_id) |
| 152 | { |
| 153 | const enum bssgp_ran_inf_app_id *app_id = get_app_id(pdu); |
| 154 | if (app_id && *app_id == exp_app_id) |
| 155 | return true; |
| 156 | return false; |
| 157 | } |
| 158 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 159 | static int handle_ran_info_response_nacc(const struct bssgp_ran_inf_app_cont_nacc *nacc, struct gprs_rlcmac_bts *bts) |
| 160 | { |
| 161 | struct si_cache_value val; |
| 162 | struct si_cache_entry *entry; |
| 163 | struct llist_head *tmp; |
| 164 | int i; |
| 165 | |
| 166 | LOGP(DRIM, LOGL_INFO, "Rx RAN-INFO cell=%s type=%sBCCH num_si=%d\n", |
| 167 | osmo_cgi_ps_name(&nacc->reprt_cell), |
| 168 | nacc->type_psi ? "P" : "", nacc->num_si); |
| 169 | |
| 170 | val.type_psi = nacc->type_psi; |
| 171 | val.si_len = 0; |
| 172 | for (i = 0; i < nacc->num_si; i++) { |
| 173 | size_t len = val.type_psi ? BSSGP_RIM_PSI_LEN : BSSGP_RIM_SI_LEN; |
| 174 | memcpy(&val.si_buf[val.si_len], nacc->si[i], len); |
| 175 | val.si_len += len; |
| 176 | } |
| 177 | entry = si_cache_add(bts->pcu->si_cache, &nacc->reprt_cell, &val); |
| 178 | |
| 179 | llist_for_each(tmp, bts_ms_list(bts)) { |
| 180 | struct GprsMs *ms = llist_entry(tmp, typeof(*ms), list); |
| 181 | if (!ms->nacc) |
| 182 | continue; |
| 183 | if (ms->nacc->fi->state != NACC_ST_WAIT_REQUEST_SI) |
| 184 | continue; |
| 185 | if (osmo_cgi_ps_cmp(&nacc->reprt_cell, &ms->nacc->cgi_ps) != 0) |
| 186 | continue; |
| 187 | osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_SI, entry); |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int handle_ran_info_response(const struct bssgp_ran_information_pdu *pdu, struct gprs_rlcmac_bts *bts) |
| 193 | { |
| 194 | const struct bssgp_ran_inf_rim_cont *ran_info = &pdu->decoded.rim_cont; |
| 195 | char ri_src_str[64]; |
| 196 | |
| 197 | if (ran_info->app_err) { |
| 198 | LOGP(DRIM, LOGL_ERROR, |
| 199 | "%s Rx RAN-INFO with an app error! cause: %s\n", |
| 200 | bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src), |
| 201 | bssgp_nacc_cause_str(ran_info->u.app_err_cont_nacc.nacc_cause)); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | switch (pdu->decoded.rim_cont.app_id) { |
| 206 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 207 | handle_ran_info_response_nacc(&ran_info->u.app_cont_nacc, bts); |
| 208 | break; |
| 209 | default: |
| 210 | LOGP(DRIM, LOGL_ERROR, "%s Rx RAN-INFO with unknown/wrong application ID %s received\n", |
| 211 | bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src), |
| 212 | bssgp_ran_inf_app_id_str(pdu->decoded.rim_cont.app_id)); |
| 213 | return -1; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 218 | int handle_rim(struct osmo_bssgp_prim *bp) |
| 219 | { |
| 220 | struct msgb *msg = bp->oph.msg; |
| 221 | uint16_t nsei = msgb_nsei(msg); |
| 222 | struct bssgp_ran_information_pdu *pdu = &bp->u.rim_pdu; |
| 223 | struct bssgp_ran_information_pdu resp_pdu; |
| 224 | struct osmo_cell_global_id_ps dst_addr; |
| 225 | struct gprs_rlcmac_bts *bts; |
Philipp Maier | a58ec61 | 2021-01-25 23:43:52 +0100 | [diff] [blame] | 226 | int rc; |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 227 | |
| 228 | OSMO_ASSERT (bp->oph.sap == SAP_BSSGP_RIM); |
| 229 | |
| 230 | /* At the moment we only support GERAN, so we block all other network |
| 231 | * types here. */ |
| 232 | if (pdu->routing_info_dest.discr != BSSGP_RIM_ROUTING_INFO_GERAN) { |
| 233 | LOGPRIM(nsei, LOGL_ERROR, |
| 234 | "Only GERAN supported, destination cell is not a GERAN cell -- rejected.\n"); |
| 235 | return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
| 236 | } |
| 237 | |
| 238 | /* Check if the RIM pdu is really addressed to this PCU. In case we |
| 239 | * receive a RIM PDU for a cell that is not parented by this PCU we |
| 240 | * are supposed to reject it with a BSSGP STATUS. |
| 241 | * see also: 3GPP TS 48.018, section 8c.3.1.2 */ |
| 242 | gprs_ra_id_ci_to_cgi_ps(&dst_addr, &pdu->routing_info_dest.geran.raid, |
| 243 | pdu->routing_info_dest.geran.cid); |
| 244 | bts = gprs_pcu_get_bts_by_cgi_ps(the_pcu, &dst_addr); |
| 245 | if (!bts) { |
| 246 | LOGPRIM(nsei, LOGL_ERROR, "Cell %s unknown to this pcu\n", |
| 247 | osmo_cgi_ps_name(&dst_addr)); |
| 248 | return bssgp_tx_status(BSSGP_CAUSE_UNKN_DST, NULL, msg); |
| 249 | } |
| 250 | |
| 251 | /* Check if the incoming RIM PDU is parseable, if not we must report |
| 252 | * an error to the controlling BSS 3GPP TS 48.018, 8c.3.4 and 8c.3.4.2 */ |
| 253 | if (!pdu->decoded_present) { |
| 254 | LOGPRIM(nsei, LOGL_ERROR, "Errornous RIM PDU received -- rejected.\n"); |
| 255 | format_response_pdu_err(&resp_pdu, pdu); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* Check if the RIM container inside the incoming RIM PDU has the correct |
| 260 | * application ID */ |
| 261 | if (!match_app_id(pdu, BSSGP_RAN_INF_APP_ID_NACC)) { |
| 262 | LOGPRIM(nsei, LOGL_ERROR, "RIM PDU with unknown/wrong application ID received -- rejected.\n"); |
| 263 | format_response_pdu_err(&resp_pdu, pdu); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* Handle incoming RIM container */ |
| 268 | switch (pdu->rim_cont_iei) { |
| 269 | case BSSGP_IE_RI_REQ_RIM_CONTAINER: |
Philipp Maier | a58ec61 | 2021-01-25 23:43:52 +0100 | [diff] [blame] | 270 | rc = osmo_cgi_ps_cmp(&dst_addr, &pdu->decoded.req_rim_cont.u.app_cont_nacc.reprt_cell); |
| 271 | if (rc != 0) { |
| 272 | LOGPRIM(nsei, LOGL_ERROR, "reporting cell in RIM application container does not match destination cell in RIM routing info -- rejected.\n"); |
| 273 | format_response_pdu_err(&resp_pdu, pdu); |
| 274 | } else { |
| 275 | LOGPRIM(nsei, LOGL_INFO, "Responding to RAN INFORMATION REQUEST ...\n"); |
| 276 | format_response_pdu(&resp_pdu, pdu, bts); |
| 277 | } |
| 278 | bssgp_tx_rim(&resp_pdu, nsei); |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 279 | break; |
| 280 | case BSSGP_IE_RI_RIM_CONTAINER: |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 281 | return handle_ran_info_response(pdu, bts); |
Pau Espin Pedrol | 3a27102 | 2021-01-21 18:44:23 +0100 | [diff] [blame] | 282 | case BSSGP_IE_RI_APP_ERROR_RIM_CONT: |
| 283 | case BSSGP_IE_RI_ACK_RIM_CONTAINER: |
| 284 | case BSSGP_IE_RI_ERROR_RIM_COINTAINER: |
| 285 | LOGPRIM(nsei, LOGL_ERROR, "RIM PDU not handled by this application\n"); |
| 286 | return -EINVAL; |
| 287 | default: |
| 288 | /* This should never happen. If the RIM PDU is parsed correctly, then the rim_cont_iei will |
| 289 | * be set to one of the cases above and if parsing fails this switch statement is guarded |
| 290 | * by the check on decoded_present above */ |
| 291 | OSMO_ASSERT(false); |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |