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> |
| 26 | |
| 27 | #include "gprs_debug.h" |
| 28 | #include "gprs_pcu.h" |
| 29 | |
| 30 | #define LOGPRIM(nsei, level, fmt, args...) \ |
| 31 | LOGP(DRIM, level, "(NSEI=%u) " fmt, nsei, ## args) |
| 32 | |
| 33 | static inline void gprs_ra_id_ci_to_cgi_ps(struct osmo_cell_global_id_ps *cgi_ps, |
| 34 | const struct gprs_ra_id *raid, uint16_t cid) |
| 35 | { |
| 36 | *cgi_ps = (struct osmo_cell_global_id_ps) { |
| 37 | .rai.lac.plmn.mcc = raid->mcc, |
| 38 | .rai.lac.plmn.mnc = raid->mnc, |
| 39 | .rai.lac.plmn.mnc_3_digits = raid->mnc_3_digits, |
| 40 | .rai.lac.lac = raid->lac, |
| 41 | .rai.rac = raid->rac, |
| 42 | .cell_identity = cid, |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | /* Mirror RIM routing information of a given PDU, see also 3GPP TS 48.018, section 8c.1.4.3 */ |
| 47 | static void mirror_rim_routing_info(struct bssgp_ran_information_pdu *to_pdu, |
| 48 | const struct bssgp_ran_information_pdu *from_pdu) |
| 49 | { |
| 50 | memcpy(&to_pdu->routing_info_dest, &from_pdu->routing_info_src, sizeof(to_pdu->routing_info_dest)); |
| 51 | memcpy(&to_pdu->routing_info_src, &from_pdu->routing_info_dest, sizeof(to_pdu->routing_info_src)); |
| 52 | } |
| 53 | |
| 54 | /* Format a RAN INFORMATION ERROR PDU */ |
| 55 | static void format_response_pdu_err(struct bssgp_ran_information_pdu *resp_pdu, |
| 56 | const struct bssgp_ran_information_pdu *req_pdu) |
| 57 | { |
| 58 | memset(resp_pdu, 0, sizeof(*resp_pdu)); |
| 59 | mirror_rim_routing_info(resp_pdu, req_pdu); |
| 60 | |
| 61 | resp_pdu->decoded.err_rim_cont = (struct bssgp_ran_inf_err_rim_cont) { |
| 62 | .app_id = BSSGP_RAN_INF_APP_ID_NACC, |
| 63 | .prot_ver = 1, |
| 64 | .err_pdu = req_pdu->rim_cont, |
| 65 | .err_pdu_len = req_pdu->rim_cont_len, |
| 66 | }; |
| 67 | |
| 68 | resp_pdu->decoded_present = true; |
| 69 | resp_pdu->rim_cont_iei = BSSGP_IE_RI_ERROR_RIM_COINTAINER; |
| 70 | } |
| 71 | |
| 72 | /* Check if the application ID in the request PDU is actually BSSGP_RAN_INF_APP_ID_NACC */ |
| 73 | static const enum bssgp_ran_inf_app_id *get_app_id(const struct bssgp_ran_information_pdu *pdu) |
| 74 | { |
| 75 | switch (pdu->rim_cont_iei) { |
| 76 | case BSSGP_IE_RI_REQ_RIM_CONTAINER: |
| 77 | return &pdu->decoded.req_rim_cont.app_id; |
| 78 | case BSSGP_IE_RI_RIM_CONTAINER: |
| 79 | return &pdu->decoded.rim_cont.app_id; |
| 80 | case BSSGP_IE_RI_APP_ERROR_RIM_CONT: |
| 81 | return &pdu->decoded.app_err_rim_cont.app_id; |
| 82 | case BSSGP_IE_RI_ACK_RIM_CONTAINER: |
| 83 | return &pdu->decoded.ack_rim_cont.app_id; |
| 84 | case BSSGP_IE_RI_ERROR_RIM_COINTAINER: |
| 85 | return &pdu->decoded.err_rim_cont.app_id; |
| 86 | default: |
| 87 | return NULL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Check if the application ID in the request PDU is of a certain type */ |
| 92 | static bool match_app_id(const struct bssgp_ran_information_pdu *pdu, enum bssgp_ran_inf_app_id exp_app_id) |
| 93 | { |
| 94 | const enum bssgp_ran_inf_app_id *app_id = get_app_id(pdu); |
| 95 | if (app_id && *app_id == exp_app_id) |
| 96 | return true; |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | int handle_rim(struct osmo_bssgp_prim *bp) |
| 101 | { |
| 102 | struct msgb *msg = bp->oph.msg; |
| 103 | uint16_t nsei = msgb_nsei(msg); |
| 104 | struct bssgp_ran_information_pdu *pdu = &bp->u.rim_pdu; |
| 105 | struct bssgp_ran_information_pdu resp_pdu; |
| 106 | struct osmo_cell_global_id_ps dst_addr; |
| 107 | struct gprs_rlcmac_bts *bts; |
| 108 | |
| 109 | OSMO_ASSERT (bp->oph.sap == SAP_BSSGP_RIM); |
| 110 | |
| 111 | /* At the moment we only support GERAN, so we block all other network |
| 112 | * types here. */ |
| 113 | if (pdu->routing_info_dest.discr != BSSGP_RIM_ROUTING_INFO_GERAN) { |
| 114 | LOGPRIM(nsei, LOGL_ERROR, |
| 115 | "Only GERAN supported, destination cell is not a GERAN cell -- rejected.\n"); |
| 116 | return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
| 117 | } |
| 118 | |
| 119 | /* Check if the RIM pdu is really addressed to this PCU. In case we |
| 120 | * receive a RIM PDU for a cell that is not parented by this PCU we |
| 121 | * are supposed to reject it with a BSSGP STATUS. |
| 122 | * see also: 3GPP TS 48.018, section 8c.3.1.2 */ |
| 123 | gprs_ra_id_ci_to_cgi_ps(&dst_addr, &pdu->routing_info_dest.geran.raid, |
| 124 | pdu->routing_info_dest.geran.cid); |
| 125 | bts = gprs_pcu_get_bts_by_cgi_ps(the_pcu, &dst_addr); |
| 126 | if (!bts) { |
| 127 | LOGPRIM(nsei, LOGL_ERROR, "Cell %s unknown to this pcu\n", |
| 128 | osmo_cgi_ps_name(&dst_addr)); |
| 129 | return bssgp_tx_status(BSSGP_CAUSE_UNKN_DST, NULL, msg); |
| 130 | } |
| 131 | |
| 132 | /* Check if the incoming RIM PDU is parseable, if not we must report |
| 133 | * an error to the controlling BSS 3GPP TS 48.018, 8c.3.4 and 8c.3.4.2 */ |
| 134 | if (!pdu->decoded_present) { |
| 135 | LOGPRIM(nsei, LOGL_ERROR, "Errornous RIM PDU received -- rejected.\n"); |
| 136 | format_response_pdu_err(&resp_pdu, pdu); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* Check if the RIM container inside the incoming RIM PDU has the correct |
| 141 | * application ID */ |
| 142 | if (!match_app_id(pdu, BSSGP_RAN_INF_APP_ID_NACC)) { |
| 143 | LOGPRIM(nsei, LOGL_ERROR, "RIM PDU with unknown/wrong application ID received -- rejected.\n"); |
| 144 | format_response_pdu_err(&resp_pdu, pdu); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* Handle incoming RIM container */ |
| 149 | switch (pdu->rim_cont_iei) { |
| 150 | case BSSGP_IE_RI_REQ_RIM_CONTAINER: |
| 151 | LOGPRIM(nsei, LOGL_NOTICE, "Responding to RAN INFORMATION REQUEST not yet implemented!\n"); |
| 152 | break; |
| 153 | case BSSGP_IE_RI_RIM_CONTAINER: |
| 154 | LOGPRIM(nsei, LOGL_NOTICE, "Responding to RAN INFORMATION not yet implemented!\n"); |
| 155 | break; |
| 156 | case BSSGP_IE_RI_APP_ERROR_RIM_CONT: |
| 157 | case BSSGP_IE_RI_ACK_RIM_CONTAINER: |
| 158 | case BSSGP_IE_RI_ERROR_RIM_COINTAINER: |
| 159 | LOGPRIM(nsei, LOGL_ERROR, "RIM PDU not handled by this application\n"); |
| 160 | return -EINVAL; |
| 161 | default: |
| 162 | /* This should never happen. If the RIM PDU is parsed correctly, then the rim_cont_iei will |
| 163 | * be set to one of the cases above and if parsing fails this switch statement is guarded |
| 164 | * by the check on decoded_present above */ |
| 165 | OSMO_ASSERT(false); |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |