Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* common RUA (RANAP User Adaption) 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 | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | |
| 24 | #include <osmocom/core/msgb.h> |
| 25 | |
| 26 | #include "rua_common.h" |
| 27 | #include "hnbgw.h" |
| 28 | |
| 29 | extern int asn1_xer_print; |
| 30 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 31 | static const struct value_string rua_cause_radio_vals[] = { |
| 32 | { RUA_CauseRadioNetwork_normal, "normal" }, |
| 33 | { RUA_CauseRadioNetwork_connect_failed, "connect failed" }, |
| 34 | { RUA_CauseRadioNetwork_network_release, "network release" }, |
| 35 | { RUA_CauseRadioNetwork_unspecified, "unspecified" }, |
| 36 | { 0, NULL } |
| 37 | }; |
| 38 | |
| 39 | static const struct value_string rua_cause_transp_vals[] = { |
| 40 | { RUA_CauseTransport_transport_resource_unavailable, "resource unavailable" }, |
| 41 | { RUA_CauseTransport_unspecified, "unspecified" }, |
| 42 | { 0, NULL } |
| 43 | }; |
| 44 | |
| 45 | static const struct value_string rua_cause_prot_vals[] = { |
| 46 | { RUA_CauseProtocol_transfer_syntax_error, "syntax error" }, |
| 47 | { RUA_CauseProtocol_abstract_syntax_error_reject, |
| 48 | "abstract syntax error; reject" }, |
| 49 | { RUA_CauseProtocol_abstract_syntax_error_ignore_and_notify, |
| 50 | "abstract syntax error; ignore and notify" }, |
| 51 | { RUA_CauseProtocol_message_not_compatible_with_receiver_state, |
| 52 | "message not compatible with receiver state" }, |
| 53 | { RUA_CauseProtocol_semantic_error, "semantic error" }, |
| 54 | { RUA_CauseProtocol_unspecified, "unspecified" }, |
| 55 | { RUA_CauseProtocol_abstract_syntax_error_falsely_constructed_message, |
| 56 | "falsely constructed message" }, |
| 57 | { 0, NULL } |
| 58 | }; |
| 59 | |
| 60 | static const struct value_string rua_cause_misc_vals[] = { |
| 61 | { RUA_CauseMisc_processing_overload, "processing overload" }, |
| 62 | { RUA_CauseMisc_hardware_failure, "hardware failure" }, |
| 63 | { RUA_CauseMisc_o_and_m_intervention, "OAM intervention" }, |
| 64 | { RUA_CauseMisc_unspecified, "unspecified" }, |
| 65 | { 0, NULL } |
| 66 | }; |
| 67 | |
| 68 | char *rua_cause_str(RUA_Cause_t *cause) |
| 69 | { |
| 70 | static char buf[32]; |
| 71 | |
| 72 | switch (cause->present) { |
| 73 | case RUA_Cause_PR_radioNetwork: |
| 74 | snprintf(buf, sizeof(buf), "radio(%s)", |
| 75 | get_value_string(rua_cause_radio_vals, |
| 76 | cause->choice.radioNetwork)); |
| 77 | break; |
| 78 | case RUA_Cause_PR_transport: |
| 79 | snprintf(buf, sizeof(buf), "transport(%s)", |
| 80 | get_value_string(rua_cause_transp_vals, |
| 81 | cause->choice.transport)); |
| 82 | break; |
| 83 | case RUA_Cause_PR_protocol: |
| 84 | snprintf(buf, sizeof(buf), "protocol(%s)", |
| 85 | get_value_string(rua_cause_prot_vals, |
| 86 | cause->choice.protocol)); |
| 87 | break; |
| 88 | case RUA_Cause_PR_misc: |
| 89 | snprintf(buf, sizeof(buf), "misc(%s)", |
| 90 | get_value_string(rua_cause_misc_vals, |
| 91 | cause->choice.misc)); |
| 92 | break; |
| 93 | } |
| 94 | return buf; |
| 95 | } |
| 96 | |
| 97 | |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 98 | static struct msgb *rua_msgb_alloc(void) |
| 99 | { |
| 100 | return msgb_alloc(1024, "RUA Tx"); |
| 101 | } |
| 102 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 103 | static struct msgb *_rua_gen_msg(RUA_RUA_PDU_t *pdu) |
| 104 | { |
| 105 | struct msgb *msg = rua_msgb_alloc(); |
| 106 | asn_enc_rval_t rval; |
| 107 | |
| 108 | if (!msg) |
| 109 | return NULL; |
| 110 | |
| 111 | rval = aper_encode_to_buffer(&asn_DEF_RUA_RUA_PDU, pdu, |
| 112 | msg->data, msgb_tailroom(msg)); |
| 113 | if (rval.encoded < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 114 | LOGP(DRUA, LOGL_ERROR, "Error encoding type: %s\n", |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 115 | rval.failed_type->name); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | msgb_put(msg, rval.encoded/8); |
| 120 | |
| 121 | return msg; |
| 122 | } |
| 123 | |
| 124 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 125 | struct msgb *rua_generate_initiating_message( |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 126 | e_RUA_ProcedureCode procedureCode, |
| 127 | RUA_Criticality_t criticality, |
| 128 | asn_TYPE_descriptor_t * td, void *sptr) |
| 129 | { |
| 130 | RUA_RUA_PDU_t pdu; |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 131 | int rc; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 132 | |
| 133 | memset(&pdu, 0, sizeof(pdu)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 134 | |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 135 | pdu.present = RUA_RUA_PDU_PR_initiatingMessage; |
| 136 | pdu.choice.initiatingMessage.procedureCode = procedureCode; |
| 137 | pdu.choice.initiatingMessage.criticality = criticality; |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 138 | rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr); |
| 139 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 140 | LOGP(DRUA, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 141 | return NULL; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 144 | return _rua_gen_msg(&pdu); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 145 | } |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 146 | |
| 147 | struct msgb *rua_generate_successful_outcome( |
| 148 | e_RUA_ProcedureCode procedureCode, |
| 149 | RUA_Criticality_t criticality, |
| 150 | asn_TYPE_descriptor_t * td, |
| 151 | void *sptr) |
| 152 | { |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 153 | RUA_RUA_PDU_t pdu; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 154 | int rc; |
| 155 | |
| 156 | memset(&pdu, 0, sizeof(pdu)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 157 | |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 158 | pdu.present = RUA_RUA_PDU_PR_successfulOutcome; |
| 159 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 160 | pdu.choice.successfulOutcome.criticality = criticality; |
| 161 | rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 162 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 163 | LOGP(DRUA, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 164 | return NULL; |
| 165 | } |
| 166 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 167 | return _rua_gen_msg(&pdu); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 170 | struct msgb *rua_generate_unsuccessful_outcome( |
| 171 | e_RUA_ProcedureCode procedureCode, |
| 172 | RUA_Criticality_t criticality, |
| 173 | asn_TYPE_descriptor_t * td, |
| 174 | void *sptr) |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 175 | { |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 176 | RUA_RUA_PDU_t pdu; |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 177 | int rc; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 178 | |
| 179 | memset(&pdu, 0, sizeof(pdu)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 180 | |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 181 | pdu.present = RUA_RUA_PDU_PR_unsuccessfulOutcome; |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 182 | pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode; |
| 183 | pdu.choice.unsuccessfulOutcome.criticality = criticality; |
| 184 | rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr); |
| 185 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 186 | LOGP(DRUA, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 187 | return NULL; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 190 | return _rua_gen_msg(&pdu); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 191 | } |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 192 | |
| 193 | RUA_IE_t *rua_new_ie(RUA_ProtocolIE_ID_t id, |
| 194 | RUA_Criticality_t criticality, |
| 195 | asn_TYPE_descriptor_t * type, void *sptr) |
| 196 | { |
| 197 | |
| 198 | RUA_IE_t *buff; |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 199 | int rc; |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 200 | |
Harald Welte | 8526d15 | 2015-12-18 13:41:39 +0100 | [diff] [blame] | 201 | if ((buff = CALLOC(1, sizeof(*buff))) == NULL) { |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 202 | // Possible error on malloc |
| 203 | return NULL; |
| 204 | } |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 205 | |
| 206 | buff->id = id; |
| 207 | buff->criticality = criticality; |
| 208 | |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 209 | rc = ANY_fromType_aper(&buff->value, type, sptr); |
| 210 | if (rc < 0) { |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 211 | LOGP(DRUA, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 212 | FREEMEM(buff); |
| 213 | return NULL; |
| 214 | } |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 215 | |
| 216 | if (asn1_xer_print) |
| 217 | if (xer_fprint(stdout, &asn_DEF_RUA_IE, buff) < 0) { |
Harald Welte | 6293913 | 2015-12-18 14:57:04 +0100 | [diff] [blame] | 218 | FREEMEM(buff); |
Harald Welte | 656ad30 | 2015-09-10 18:33:47 +0200 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | return buff; |
| 223 | } |