blob: a191fffc4d43a5f9d93d4e298b74621a1eb8face [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>
26
27#include "gprs_debug.h"
28#include "gprs_pcu.h"
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010029#include "bts.h"
30#include "gprs_ms.h"
31#include "nacc_fsm.h"
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010032
33#define LOGPRIM(nsei, level, fmt, args...) \
34 LOGP(DRIM, level, "(NSEI=%u) " fmt, nsei, ## args)
35
36static inline void gprs_ra_id_ci_to_cgi_ps(struct osmo_cell_global_id_ps *cgi_ps,
37 const struct gprs_ra_id *raid, uint16_t cid)
38{
39 *cgi_ps = (struct osmo_cell_global_id_ps) {
40 .rai.lac.plmn.mcc = raid->mcc,
41 .rai.lac.plmn.mnc = raid->mnc,
42 .rai.lac.plmn.mnc_3_digits = raid->mnc_3_digits,
43 .rai.lac.lac = raid->lac,
44 .rai.rac = raid->rac,
45 .cell_identity = cid,
46 };
47}
48
49/* Mirror RIM routing information of a given PDU, see also 3GPP TS 48.018, section 8c.1.4.3 */
50static void mirror_rim_routing_info(struct bssgp_ran_information_pdu *to_pdu,
51 const struct bssgp_ran_information_pdu *from_pdu)
52{
53 memcpy(&to_pdu->routing_info_dest, &from_pdu->routing_info_src, sizeof(to_pdu->routing_info_dest));
54 memcpy(&to_pdu->routing_info_src, &from_pdu->routing_info_dest, sizeof(to_pdu->routing_info_src));
55}
56
57/* Format a RAN INFORMATION ERROR PDU */
58static void format_response_pdu_err(struct bssgp_ran_information_pdu *resp_pdu,
59 const struct bssgp_ran_information_pdu *req_pdu)
60{
61 memset(resp_pdu, 0, sizeof(*resp_pdu));
62 mirror_rim_routing_info(resp_pdu, req_pdu);
63
64 resp_pdu->decoded.err_rim_cont = (struct bssgp_ran_inf_err_rim_cont) {
65 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
66 .prot_ver = 1,
67 .err_pdu = req_pdu->rim_cont,
68 .err_pdu_len = req_pdu->rim_cont_len,
69 };
70
71 resp_pdu->decoded_present = true;
72 resp_pdu->rim_cont_iei = BSSGP_IE_RI_ERROR_RIM_COINTAINER;
73}
74
75/* Check if the application ID in the request PDU is actually BSSGP_RAN_INF_APP_ID_NACC */
76static const enum bssgp_ran_inf_app_id *get_app_id(const struct bssgp_ran_information_pdu *pdu)
77{
78 switch (pdu->rim_cont_iei) {
79 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
80 return &pdu->decoded.req_rim_cont.app_id;
81 case BSSGP_IE_RI_RIM_CONTAINER:
82 return &pdu->decoded.rim_cont.app_id;
83 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
84 return &pdu->decoded.app_err_rim_cont.app_id;
85 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
86 return &pdu->decoded.ack_rim_cont.app_id;
87 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
88 return &pdu->decoded.err_rim_cont.app_id;
89 default:
90 return NULL;
91 }
92}
93
94/* Check if the application ID in the request PDU is of a certain type */
95static bool match_app_id(const struct bssgp_ran_information_pdu *pdu, enum bssgp_ran_inf_app_id exp_app_id)
96{
97 const enum bssgp_ran_inf_app_id *app_id = get_app_id(pdu);
98 if (app_id && *app_id == exp_app_id)
99 return true;
100 return false;
101}
102
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100103static int handle_ran_info_response_nacc(const struct bssgp_ran_inf_app_cont_nacc *nacc, struct gprs_rlcmac_bts *bts)
104{
105 struct si_cache_value val;
106 struct si_cache_entry *entry;
107 struct llist_head *tmp;
108 int i;
109
110 LOGP(DRIM, LOGL_INFO, "Rx RAN-INFO cell=%s type=%sBCCH num_si=%d\n",
111 osmo_cgi_ps_name(&nacc->reprt_cell),
112 nacc->type_psi ? "P" : "", nacc->num_si);
113
114 val.type_psi = nacc->type_psi;
115 val.si_len = 0;
116 for (i = 0; i < nacc->num_si; i++) {
117 size_t len = val.type_psi ? BSSGP_RIM_PSI_LEN : BSSGP_RIM_SI_LEN;
118 memcpy(&val.si_buf[val.si_len], nacc->si[i], len);
119 val.si_len += len;
120 }
121 entry = si_cache_add(bts->pcu->si_cache, &nacc->reprt_cell, &val);
122
123 llist_for_each(tmp, bts_ms_list(bts)) {
124 struct GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
125 if (!ms->nacc)
126 continue;
127 if (ms->nacc->fi->state != NACC_ST_WAIT_REQUEST_SI)
128 continue;
129 if (osmo_cgi_ps_cmp(&nacc->reprt_cell, &ms->nacc->cgi_ps) != 0)
130 continue;
131 osmo_fsm_inst_dispatch(ms->nacc->fi, NACC_EV_RX_SI, entry);
132 }
133 return 0;
134}
135
136static int handle_ran_info_response(const struct bssgp_ran_information_pdu *pdu, struct gprs_rlcmac_bts *bts)
137{
138 const struct bssgp_ran_inf_rim_cont *ran_info = &pdu->decoded.rim_cont;
139 char ri_src_str[64];
140
141 if (ran_info->app_err) {
142 LOGP(DRIM, LOGL_ERROR,
143 "%s Rx RAN-INFO with an app error! cause: %s\n",
144 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
145 bssgp_nacc_cause_str(ran_info->u.app_err_cont_nacc.nacc_cause));
146 return -1;
147 }
148
149 switch (pdu->decoded.rim_cont.app_id) {
150 case BSSGP_RAN_INF_APP_ID_NACC:
151 handle_ran_info_response_nacc(&ran_info->u.app_cont_nacc, bts);
152 break;
153 default:
154 LOGP(DRIM, LOGL_ERROR, "%s Rx RAN-INFO with unknown/wrong application ID %s received\n",
155 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &pdu->routing_info_src),
156 bssgp_ran_inf_app_id_str(pdu->decoded.rim_cont.app_id));
157 return -1;
158 }
159 return 0;
160}
161
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100162int handle_rim(struct osmo_bssgp_prim *bp)
163{
164 struct msgb *msg = bp->oph.msg;
165 uint16_t nsei = msgb_nsei(msg);
166 struct bssgp_ran_information_pdu *pdu = &bp->u.rim_pdu;
167 struct bssgp_ran_information_pdu resp_pdu;
168 struct osmo_cell_global_id_ps dst_addr;
169 struct gprs_rlcmac_bts *bts;
170
171 OSMO_ASSERT (bp->oph.sap == SAP_BSSGP_RIM);
172
173 /* At the moment we only support GERAN, so we block all other network
174 * types here. */
175 if (pdu->routing_info_dest.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
176 LOGPRIM(nsei, LOGL_ERROR,
177 "Only GERAN supported, destination cell is not a GERAN cell -- rejected.\n");
178 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
179 }
180
181 /* Check if the RIM pdu is really addressed to this PCU. In case we
182 * receive a RIM PDU for a cell that is not parented by this PCU we
183 * are supposed to reject it with a BSSGP STATUS.
184 * see also: 3GPP TS 48.018, section 8c.3.1.2 */
185 gprs_ra_id_ci_to_cgi_ps(&dst_addr, &pdu->routing_info_dest.geran.raid,
186 pdu->routing_info_dest.geran.cid);
187 bts = gprs_pcu_get_bts_by_cgi_ps(the_pcu, &dst_addr);
188 if (!bts) {
189 LOGPRIM(nsei, LOGL_ERROR, "Cell %s unknown to this pcu\n",
190 osmo_cgi_ps_name(&dst_addr));
191 return bssgp_tx_status(BSSGP_CAUSE_UNKN_DST, NULL, msg);
192 }
193
194 /* Check if the incoming RIM PDU is parseable, if not we must report
195 * an error to the controlling BSS 3GPP TS 48.018, 8c.3.4 and 8c.3.4.2 */
196 if (!pdu->decoded_present) {
197 LOGPRIM(nsei, LOGL_ERROR, "Errornous RIM PDU received -- rejected.\n");
198 format_response_pdu_err(&resp_pdu, pdu);
199 return 0;
200 }
201
202 /* Check if the RIM container inside the incoming RIM PDU has the correct
203 * application ID */
204 if (!match_app_id(pdu, BSSGP_RAN_INF_APP_ID_NACC)) {
205 LOGPRIM(nsei, LOGL_ERROR, "RIM PDU with unknown/wrong application ID received -- rejected.\n");
206 format_response_pdu_err(&resp_pdu, pdu);
207 return 0;
208 }
209
210 /* Handle incoming RIM container */
211 switch (pdu->rim_cont_iei) {
212 case BSSGP_IE_RI_REQ_RIM_CONTAINER:
213 LOGPRIM(nsei, LOGL_NOTICE, "Responding to RAN INFORMATION REQUEST not yet implemented!\n");
214 break;
215 case BSSGP_IE_RI_RIM_CONTAINER:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100216 return handle_ran_info_response(pdu, bts);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100217 case BSSGP_IE_RI_APP_ERROR_RIM_CONT:
218 case BSSGP_IE_RI_ACK_RIM_CONTAINER:
219 case BSSGP_IE_RI_ERROR_RIM_COINTAINER:
220 LOGPRIM(nsei, LOGL_ERROR, "RIM PDU not handled by this application\n");
221 return -EINVAL;
222 default:
223 /* This should never happen. If the RIM PDU is parsed correctly, then the rim_cont_iei will
224 * be set to one of the cases above and if parsing fails this switch statement is guarded
225 * by the check on decoded_present above */
226 OSMO_ASSERT(false);
227 }
228
229 return 0;
230}