Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* HNBAP common code */ |
| 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 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | |
| 24 | #include <osmocom/core/msgb.h> |
| 25 | |
Harald Welte | bea34f2 | 2016-01-05 14:24:08 +0100 | [diff] [blame] | 26 | #include <osmocom/hnbap/HNBAP-PDU.h> |
Neels Hofmeyr | 8345792 | 2016-08-26 23:56:44 +0200 | [diff] [blame] | 27 | #include <osmocom/hnbap/hnbap_common.h> |
Neels Hofmeyr | df63de2 | 2016-08-18 13:13:55 +0200 | [diff] [blame] | 28 | #include <osmocom/iuh/hnbgw.h> |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 29 | |
Harald Welte | 3af1db8 | 2015-09-11 17:03:16 +0200 | [diff] [blame] | 30 | static const struct value_string hnbap_cause_radio_vals[] = { |
| 31 | { CauseRadioNetwork_overload, "overload" }, |
| 32 | { CauseRadioNetwork_unauthorised_Location, "unauthorized location" }, |
| 33 | { CauseRadioNetwork_unauthorised_HNB, "unauthorized HNB" }, |
| 34 | { CauseRadioNetwork_hNB_parameter_mismatch, "HNB parameter mismatch" }, |
| 35 | { CauseRadioNetwork_invalid_UE_identity, "invalid UE identity" }, |
| 36 | { CauseRadioNetwork_uE_not_allowed_on_this_HNB, |
| 37 | "UE not allowed on this HNB" }, |
| 38 | { CauseRadioNetwork_uE_unauthorised, "unauthorised UE" }, |
| 39 | { CauseRadioNetwork_connection_with_UE_lost, "connection with UE lost" }, |
| 40 | { CauseRadioNetwork_ue_RRC_release, "UE RRC release" }, |
| 41 | { CauseRadioNetwork_hNB_not_registered, "HNB not registered" }, |
| 42 | { CauseRadioNetwork_unspecified, "unspecified" }, |
| 43 | { CauseRadioNetwork_normal, "normal" }, |
| 44 | { CauseRadioNetwork_uE_relocated, "UE relocated" }, |
| 45 | { CauseRadioNetwork_ue_registered_in_another_HNB, |
| 46 | "UE registered in another HNB" }, |
| 47 | { 0, NULL } |
| 48 | }; |
| 49 | |
| 50 | static const struct value_string hnbap_cause_transp_vals[] = { |
| 51 | { CauseTransport_transport_resource_unavailable, |
| 52 | "transport resource unavailable" }, |
| 53 | { CauseTransport_unspecified, "unspecified" }, |
| 54 | { 0, NULL } |
| 55 | }; |
| 56 | |
| 57 | static const struct value_string hnbap_cause_prot_vals[] = { |
| 58 | { CauseProtocol_transfer_syntax_error, "syntax error" }, |
| 59 | { CauseProtocol_abstract_syntax_error_reject, |
| 60 | "abstract syntax error; reject" }, |
| 61 | { CauseProtocol_abstract_syntax_error_ignore_and_notify, |
| 62 | "abstract syntax error; ignore and notify" }, |
| 63 | { CauseProtocol_message_not_compatible_with_receiver_state, |
| 64 | "message not compatible with receiver state" }, |
| 65 | { CauseProtocol_semantic_error, "semantic error" }, |
| 66 | { CauseProtocol_unspecified, "unspecified" }, |
| 67 | { CauseProtocol_abstract_syntax_error_falsely_constructed_message, |
| 68 | "falsely constructed message" }, |
| 69 | { 0, NULL } |
| 70 | }; |
| 71 | |
| 72 | static const struct value_string hnbap_cause_misc_vals[] = { |
| 73 | { CauseMisc_processing_overload, "processing overload" }, |
| 74 | { CauseMisc_hardware_failure, "hardware failure" }, |
| 75 | { CauseMisc_o_and_m_intervention, "OAM intervention" }, |
| 76 | { CauseMisc_unspecified, "unspecified" }, |
| 77 | { 0, NULL } |
| 78 | }; |
| 79 | |
| 80 | char *hnbap_cause_str(Cause_t *cause) |
| 81 | { |
| 82 | static char buf[32]; |
| 83 | |
| 84 | switch (cause->present) { |
| 85 | case Cause_PR_radioNetwork: |
| 86 | snprintf(buf, sizeof(buf), "radio(%s)", |
| 87 | get_value_string(hnbap_cause_radio_vals, |
| 88 | cause->choice.radioNetwork)); |
| 89 | break; |
| 90 | case Cause_PR_transport: |
| 91 | snprintf(buf, sizeof(buf), "transport(%s)", |
| 92 | get_value_string(hnbap_cause_transp_vals, |
| 93 | cause->choice.transport)); |
| 94 | break; |
| 95 | case Cause_PR_protocol: |
| 96 | snprintf(buf, sizeof(buf), "protocol(%s)", |
| 97 | get_value_string(hnbap_cause_prot_vals, |
| 98 | cause->choice.protocol)); |
| 99 | break; |
| 100 | case Cause_PR_misc: |
| 101 | snprintf(buf, sizeof(buf), "misc(%s)", |
| 102 | get_value_string(hnbap_cause_misc_vals, |
| 103 | cause->choice.misc)); |
| 104 | break; |
| 105 | } |
| 106 | return buf; |
| 107 | } |
| 108 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 109 | static struct msgb *hnbap_msgb_alloc(void) |
| 110 | { |
| 111 | return msgb_alloc(1024, "HNBAP Tx"); |
| 112 | } |
| 113 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 114 | static struct msgb *_hnbap_gen_msg(HNBAP_PDU_t *pdu) |
| 115 | { |
Harald Welte | 462db35 | 2015-12-16 23:06:59 +0100 | [diff] [blame] | 116 | struct msgb *msg = hnbap_msgb_alloc(); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 117 | asn_enc_rval_t rval; |
| 118 | |
| 119 | if (!msg) |
| 120 | return NULL; |
| 121 | |
| 122 | rval = aper_encode_to_buffer(&asn_DEF_HNBAP_PDU, pdu, |
| 123 | msg->data, msgb_tailroom(msg)); |
| 124 | if (rval.encoded < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 125 | LOGP(DHNBAP, LOGL_ERROR, "Error encoding type: %s\n", |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 126 | rval.failed_type->name); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | msgb_put(msg, rval.encoded/8); |
| 131 | |
| 132 | return msg; |
| 133 | } |
| 134 | |
Daniel Willmann | 0e8ef67 | 2015-12-07 17:19:40 +0100 | [diff] [blame] | 135 | struct msgb *hnbap_generate_initiating_message( |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 136 | e_ProcedureCode procedureCode, |
| 137 | Criticality_t criticality, |
| 138 | asn_TYPE_descriptor_t * td, void *sptr) |
| 139 | { |
Daniel Willmann | 59d17d8 | 2015-12-17 17:56:56 +0100 | [diff] [blame] | 140 | struct msgb *msg; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 141 | HNBAP_PDU_t pdu; |
Daniel Willmann | 0e8ef67 | 2015-12-07 17:19:40 +0100 | [diff] [blame] | 142 | int rc; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 143 | |
| 144 | memset(&pdu, 0, sizeof(HNBAP_PDU_t)); |
| 145 | |
| 146 | pdu.present = HNBAP_PDU_PR_initiatingMessage; |
| 147 | pdu.choice.initiatingMessage.procedureCode = procedureCode; |
| 148 | pdu.choice.initiatingMessage.criticality = criticality; |
Daniel Willmann | 0e8ef67 | 2015-12-07 17:19:40 +0100 | [diff] [blame] | 149 | rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr); |
| 150 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 151 | LOGP(DHNBAP, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Daniel Willmann | 0e8ef67 | 2015-12-07 17:19:40 +0100 | [diff] [blame] | 152 | return NULL; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Daniel Willmann | 59d17d8 | 2015-12-17 17:56:56 +0100 | [diff] [blame] | 155 | msg = _hnbap_gen_msg(&pdu); |
| 156 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, &pdu); |
| 157 | |
| 158 | return msg; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 159 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 160 | |
Harald Welte | 339b8e2 | 2015-08-30 23:08:32 +0200 | [diff] [blame] | 161 | struct msgb *hnbap_generate_successful_outcome( |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 162 | e_ProcedureCode procedureCode, |
| 163 | Criticality_t criticality, |
| 164 | asn_TYPE_descriptor_t * td, |
| 165 | void *sptr) |
| 166 | { |
Daniel Willmann | 59d17d8 | 2015-12-17 17:56:56 +0100 | [diff] [blame] | 167 | struct msgb *msg; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 168 | HNBAP_PDU_t pdu; |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 169 | int rc; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 170 | |
| 171 | memset(&pdu, 0, sizeof(HNBAP_PDU_t)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 172 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 173 | pdu.present = HNBAP_PDU_PR_successfulOutcome; |
| 174 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 175 | pdu.choice.successfulOutcome.criticality = criticality; |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 176 | rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 177 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 178 | LOGP(DHNBAP, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | c060b7b | 2015-09-07 22:40:41 +0200 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 181 | |
Daniel Willmann | 59d17d8 | 2015-12-17 17:56:56 +0100 | [diff] [blame] | 182 | msg = _hnbap_gen_msg(&pdu); |
| 183 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, &pdu); |
| 184 | |
| 185 | return msg; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 188 | struct msgb *hnbap_generate_unsuccessful_outcome( |
| 189 | e_ProcedureCode procedureCode, |
| 190 | Criticality_t criticality, |
| 191 | asn_TYPE_descriptor_t * td, |
| 192 | void *sptr) |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 193 | { |
Daniel Willmann | 59d17d8 | 2015-12-17 17:56:56 +0100 | [diff] [blame] | 194 | struct msgb *msg; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 195 | HNBAP_PDU_t pdu; |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 196 | int rc; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 197 | |
| 198 | memset(&pdu, 0, sizeof(HNBAP_PDU_t)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 199 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 200 | pdu.present = HNBAP_PDU_PR_unsuccessfulOutcome; |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 201 | pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode; |
| 202 | pdu.choice.unsuccessfulOutcome.criticality = criticality; |
| 203 | rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr); |
| 204 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 205 | LOGP(DHNBAP, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 206 | return NULL; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Daniel Willmann | 59d17d8 | 2015-12-17 17:56:56 +0100 | [diff] [blame] | 209 | msg = _hnbap_gen_msg(&pdu); |
| 210 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, &pdu); |
| 211 | |
| 212 | return msg; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 213 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 214 | |
| 215 | IE_t *hnbap_new_ie(ProtocolIE_ID_t id, |
| 216 | Criticality_t criticality, |
| 217 | asn_TYPE_descriptor_t * type, void *sptr) |
| 218 | { |
| 219 | |
| 220 | IE_t *buff; |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 221 | int rc; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 222 | |
Harald Welte | 8526d15 | 2015-12-18 13:41:39 +0100 | [diff] [blame] | 223 | if ((buff = CALLOC(1, sizeof(IE_t))) == NULL) { |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 224 | // Possible error on malloc |
| 225 | return NULL; |
| 226 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 227 | |
| 228 | buff->id = id; |
| 229 | buff->criticality = criticality; |
| 230 | |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 231 | rc = ANY_fromType_aper(&buff->value, type, sptr); |
| 232 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 233 | LOGP(DHNBAP, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 234 | FREEMEM(buff); |
| 235 | return NULL; |
| 236 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 237 | |
| 238 | if (asn1_xer_print) |
| 239 | if (xer_fprint(stdout, &asn_DEF_IE, buff) < 0) { |
Harald Welte | 6293913 | 2015-12-18 14:57:04 +0100 | [diff] [blame] | 240 | FREEMEM(buff); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | return buff; |
| 245 | } |