blob: 554896b210c0959bb8789e7036c30f90bf2d907b [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* 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 Welte656ad302015-09-10 18:33:47 +020022#include <stdint.h>
23
24#include <osmocom/core/msgb.h>
25
26#include "rua_common.h"
27#include "hnbgw.h"
28
29extern int asn1_xer_print;
30
Harald Weltee2e5d4d2015-09-10 23:49:45 +020031static 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
39static 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
45static 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
60static 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
68char *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 Welte656ad302015-09-10 18:33:47 +020098static struct msgb *rua_msgb_alloc(void)
99{
100 return msgb_alloc(1024, "RUA Tx");
101}
102
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200103struct msgb *rua_generate_initiating_message(
Harald Welte656ad302015-09-10 18:33:47 +0200104 e_RUA_ProcedureCode procedureCode,
105 RUA_Criticality_t criticality,
106 asn_TYPE_descriptor_t * td, void *sptr)
107{
108 RUA_RUA_PDU_t pdu;
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200109 struct msgb *msg = rua_msgb_alloc();
110 asn_enc_rval_t rval;
Harald Welte656ad302015-09-10 18:33:47 +0200111 ssize_t encoded;
112
113 memset(&pdu, 0, sizeof(pdu));
Harald Welte656ad302015-09-10 18:33:47 +0200114 pdu.present = RUA_RUA_PDU_PR_initiatingMessage;
115 pdu.choice.initiatingMessage.procedureCode = procedureCode;
116 pdu.choice.initiatingMessage.criticality = criticality;
117 ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
118
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200119 rval = aper_encode_to_buffer(&asn_DEF_RUA_RUA_PDU, &pdu,
120 msg->data, msgb_tailroom(msg));
121 if (rval.encoded < 0) {
122 LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name);
123 msgb_free(msg);
124 return NULL;
Harald Welte656ad302015-09-10 18:33:47 +0200125 }
126
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200127 msgb_put(msg, rval.encoded/8);
128
129 return msg;
Harald Welte656ad302015-09-10 18:33:47 +0200130}
Harald Welte656ad302015-09-10 18:33:47 +0200131
132struct msgb *rua_generate_successful_outcome(
133 e_RUA_ProcedureCode procedureCode,
134 RUA_Criticality_t criticality,
135 asn_TYPE_descriptor_t * td,
136 void *sptr)
137{
138
139 RUA_RUA_PDU_t pdu;
140 struct msgb *msg = rua_msgb_alloc();
141 asn_enc_rval_t rval;
142 int rc;
143
144 memset(&pdu, 0, sizeof(pdu));
145 pdu.present = RUA_RUA_PDU_PR_successfulOutcome;
146 pdu.choice.successfulOutcome.procedureCode = procedureCode;
147 pdu.choice.successfulOutcome.criticality = criticality;
148 rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
149 if (rc < 0) {
150 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
151 msgb_free(msg);
152 return NULL;
153 }
154
155 rval = aper_encode_to_buffer(&asn_DEF_RUA_RUA_PDU, &pdu,
156 msg->data, msgb_tailroom(msg));
157 if (rval.encoded < 0) {
158 LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name);
159 msgb_free(msg);
160 return NULL;
161 }
162
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200163 msgb_put(msg, rval.encoded/8);
Harald Welte656ad302015-09-10 18:33:47 +0200164
165 return msg;
166}
167
168#if 0
169ssize_t rua_generate_unsuccessful_outcome(uint8_t ** buffer,
170 uint32_t * length,
171 e_RUA_ProcedureCode procedureCode,
172 RUA_Criticality_t criticality,
173 asn_TYPE_descriptor_t * td,
174 void *sptr)
175{
176
177 RUA_RUA_PDU_t pdu;
178 ssize_t encoded;
179
180 memset(&pdu, 0, sizeof(pdu));
181
182 pdu.present = RUA_RUA_PDU_PR_unsuccessfulOutcome;
183 pdu.choice.successfulOutcome.procedureCode = procedureCode;
184 pdu.choice.successfulOutcome.criticality = criticality;
185 ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
186
187 if ((encoded =
188 aper_encode_to_new_buffer(&asn_DEF_RUA_RUA_PDU, 0, &pdu,
189 (void **)buffer)) < 0) {
190 return -1;
191 }
192
193 *length = encoded;
194
195 return encoded;
196}
197#endif
198
199RUA_IE_t *rua_new_ie(RUA_ProtocolIE_ID_t id,
200 RUA_Criticality_t criticality,
201 asn_TYPE_descriptor_t * type, void *sptr)
202{
203
204 RUA_IE_t *buff;
205
206 if ((buff = malloc(sizeof(*buff))) == NULL) {
207 // Possible error on malloc
208 return NULL;
209 }
210 memset((void *)buff, 0, sizeof(*buff));
211
212 buff->id = id;
213 buff->criticality = criticality;
214
215 ANY_fromType_aper(&buff->value, type, sptr);
216
217 if (asn1_xer_print)
218 if (xer_fprint(stdout, &asn_DEF_RUA_IE, buff) < 0) {
219 free(buff);
220 return NULL;
221 }
222
223 return buff;
224}