Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* hnb-gw specific code for RANAP */ |
| 2 | |
| 3 | /* (C) 2015 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/core/utils.h> |
Harald Welte | 80401ad | 2015-09-11 19:48:06 +0200 | [diff] [blame] | 25 | #include <osmocom/gsm/gsm48.h> |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 26 | |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "asn1helpers.h" |
| 32 | |
| 33 | #include "hnbgw.h" |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 34 | #include "hnbgw_rua.h" |
Harald Welte | 08d5da6 | 2015-12-16 17:31:43 +0100 | [diff] [blame] | 35 | #include "ranap_common.h" |
| 36 | #include "ranap_ies_defs.h" |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 37 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 38 | static int ranap_tx_reset_ack(struct hnb_context *hnb, |
| 39 | RANAP_CN_DomainIndicator_t domain) |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 40 | { |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 41 | struct msgb *msg; |
| 42 | int rc; |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 43 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 44 | msg = ranap_new_msg_reset_ack(domain, NULL); |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 45 | if (!msg) |
| 46 | return -1; |
| 47 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 48 | rc = rua_tx_udt(hnb, msg->data, msgb_length(msg)); |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 49 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 50 | msgb_free(msg); |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 51 | |
| 52 | return rc; |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 53 | } |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 54 | |
| 55 | static int ranap_rx_init_reset(struct hnb_context *hnb, ANY_t *in) |
| 56 | { |
| 57 | RANAP_ResetIEs_t ies; |
| 58 | int rc; |
| 59 | |
| 60 | rc = ranap_decode_reseties(&ies, in); |
| 61 | if (rc < 0) |
| 62 | return rc; |
| 63 | |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 64 | DEBUGP(DRANAP, "RESET.req\n"); |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 65 | |
Harald Welte | 80401ad | 2015-09-11 19:48:06 +0200 | [diff] [blame] | 66 | /* FIXME: Actually we have to wait for some guard time? */ |
| 67 | /* FIXME: Reset all resources related to this HNB/RNC */ |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 68 | ranap_tx_reset_ack(hnb, ies.cN_DomainIndicator); |
| 69 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Harald Welte | 80401ad | 2015-09-11 19:48:06 +0200 | [diff] [blame] | 73 | int ranap_parse_lai(struct gprs_ra_id *ra_id, const RANAP_LAI_t *lai) |
| 74 | { |
| 75 | uint8_t *ptr = lai->pLMNidentity.buf; |
| 76 | |
| 77 | /* TS 25.413 9.2.3.55 */ |
| 78 | if (lai->pLMNidentity.size != 3) |
| 79 | return -1; |
| 80 | |
| 81 | ra_id->mcc = (ptr[0] & 0xF) * 100 + |
| 82 | (ptr[0] >> 4) * 10 + |
| 83 | (ptr[1] & 0xF); |
| 84 | ra_id->mnc = (ptr[2] & 0xF) + |
| 85 | (ptr[2] >> 4) * 10; |
| 86 | if ((ptr[1] >> 4) != 0xF) |
| 87 | ra_id->mnc += (ptr[1] >> 4) * 100; |
| 88 | |
| 89 | ra_id->lac = asn1str_to_u16(&lai->lAC); |
| 90 | |
| 91 | /* TS 25.413 9.2.3.6 */ |
| 92 | if (ra_id->lac == 0 || ra_id->lac == 0xfffe) |
| 93 | return -1; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Harald Welte | 1c2ada0 | 2015-09-11 20:10:17 +0200 | [diff] [blame] | 98 | static int ranap_rx_dt(struct hnb_context *hnb, ANY_t *in) |
| 99 | { |
| 100 | RANAP_DirectTransferIEs_t ies; |
| 101 | int sapi = 0; |
| 102 | int rc; |
| 103 | |
| 104 | rc = ranap_decode_directtransferies(&ies, in); |
| 105 | if (rc < 0) |
| 106 | return rc; |
| 107 | |
| 108 | if (ies.presenceMask & DIRECTTRANSFERIES_RANAP_SAPI_PRESENT) |
| 109 | sapi = ies.sapi; |
| 110 | |
| 111 | if (ies.presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) { |
| 112 | /* FIXME: Update LAI associated with UE */ |
| 113 | } |
| 114 | |
| 115 | if (ies.presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) { |
| 116 | /* FIXME: Update RAC associated with UE */ |
| 117 | } |
| 118 | |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 119 | DEBUGP(DRANAP, "DirectTransfer: %s\n", |
Harald Welte | 1c2ada0 | 2015-09-11 20:10:17 +0200 | [diff] [blame] | 120 | osmo_hexdump(ies.nas_pdu.buf, ies.nas_pdu.size)); |
| 121 | /* FIXME: hand NAS PDU into MSC */ |
| 122 | } |
| 123 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 124 | static int ranap_rx_initiating_msg(struct hnb_context *hnb, RANAP_InitiatingMessage_t *imsg) |
| 125 | { |
| 126 | int rc; |
| 127 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 128 | /* according tot the spec, we can primarily receive Overload, |
| 129 | * Reset, Reset ACK, Error Indication, reset Resource, Reset |
| 130 | * Resurce Acknowledge as connecitonless RANAP. There are some |
| 131 | * more messages regarding Information Transfer, Direct |
| 132 | * Information Transfer and Uplink Information Trnansfer that we |
| 133 | * can ignore. In either case, it is RANAP that we need to |
| 134 | * decode... */ |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 135 | switch (imsg->procedureCode) { |
| 136 | case RANAP_ProcedureCode_id_Reset: |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 137 | /* Reset request */ |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 138 | rc = ranap_rx_init_reset(hnb, &imsg->value); |
| 139 | break; |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 140 | case RANAP_ProcedureCode_id_OverloadControl: /* Overload ind */ |
Harald Welte | 80401ad | 2015-09-11 19:48:06 +0200 | [diff] [blame] | 141 | break; |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 142 | case RANAP_ProcedureCode_id_ErrorIndication: /* Error ind */ |
| 143 | break; |
| 144 | case RANAP_ProcedureCode_id_ResetResource: /* request */ |
| 145 | case RANAP_ProcedureCode_id_InformationTransfer: |
| 146 | case RANAP_ProcedureCode_id_DirectInformationTransfer: |
| 147 | case RANAP_ProcedureCode_id_UplinkInformationExchange: |
| 148 | LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP " |
| 149 | "Procedure %u from HNB, ignoring\n", imsg->procedureCode); |
| 150 | break; |
| 151 | default: |
| 152 | LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP " |
| 153 | "Procedure %u from HNB, ignoring\n", imsg->procedureCode); |
Harald Welte | 1c2ada0 | 2015-09-11 20:10:17 +0200 | [diff] [blame] | 154 | break; |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 158 | static int ranap_rx_successful_msg(struct hnb_context *hnb, RANAP_SuccessfulOutcome_t *imsg) |
| 159 | { |
| 160 | int rc; |
| 161 | |
| 162 | /* according tot the spec, we can primarily receive Overload, |
| 163 | * Reset, Reset ACK, Error Indication, reset Resource, Reset |
| 164 | * Resurce Acknowledge as connecitonless RANAP. There are some |
| 165 | * more messages regarding Information Transfer, Direct |
| 166 | * Information Transfer and Uplink Information Trnansfer that we |
| 167 | * can ignore. In either case, it is RANAP that we need to |
| 168 | * decode... */ |
| 169 | switch (imsg->procedureCode) { |
| 170 | case RANAP_ProcedureCode_id_Reset: /* Reset acknowledge */ |
| 171 | break; |
| 172 | case RANAP_ProcedureCode_id_ResetResource: /* response */ |
| 173 | case RANAP_ProcedureCode_id_InformationTransfer: |
| 174 | case RANAP_ProcedureCode_id_DirectInformationTransfer: |
| 175 | case RANAP_ProcedureCode_id_UplinkInformationExchange: |
| 176 | LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP " |
| 177 | "Procedure %u from HNB, ignoring\n", imsg->procedureCode); |
| 178 | break; |
| 179 | default: |
| 180 | LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP " |
| 181 | "Procedure %u from HNB, ignoring\n", imsg->procedureCode); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 188 | static int _hnbgw_ranap_rx(struct hnb_context *hnb, RANAP_RANAP_PDU_t *pdu) |
| 189 | { |
| 190 | int rc; |
| 191 | |
| 192 | switch (pdu->present) { |
| 193 | case RANAP_RANAP_PDU_PR_initiatingMessage: |
| 194 | rc = ranap_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage); |
| 195 | break; |
| 196 | case RANAP_RANAP_PDU_PR_successfulOutcome: |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 197 | rc = ranap_rx_successful_msg(hnb, &pdu->choice.successfulOutcome); |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 198 | break; |
| 199 | case RANAP_RANAP_PDU_PR_unsuccessfulOutcome: |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 200 | LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP " |
| 201 | "unsuccessful outcome procedure %u from HNB, ignoring\n", |
| 202 | pdu->choice.unsuccessfulOutcome.procedureCode); |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 203 | break; |
| 204 | default: |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 205 | LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP " |
| 206 | "presence %u from HNB, ignoring\n", pdu->present); |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | int hnbgw_ranap_rx(struct msgb *msg, uint8_t *data, size_t len) |
| 213 | { |
| 214 | RANAP_RANAP_PDU_t _pdu, *pdu = &_pdu; |
| 215 | asn_dec_rval_t dec_ret; |
| 216 | int rc; |
| 217 | |
| 218 | memset(pdu, 0, sizeof(*pdu)); |
| 219 | dec_ret = aper_decode(NULL,&asn_DEF_RANAP_RANAP_PDU, (void **) &pdu, |
| 220 | data, len, 0, 0); |
| 221 | if (dec_ret.code != RC_OK) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 222 | LOGP(DRANAP, LOGL_ERROR, "Error in RANAP ASN.1 decode\n"); |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | rc = _hnbgw_ranap_rx(msg->dst, pdu); |
| 227 | |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | int hnbgw_ranap_init(void) |
| 232 | { |
| 233 | |
| 234 | } |