blob: 8e2cc91d1bd1c6e51a7d3a5e7cc2f2507e017e6e [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 Welte8dacb072015-12-16 20:27:14 +0100103static 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) {
114 LOGP(DMAIN, LOGL_ERROR, "Error encoding type: %s\n",
115 rval.failed_type->name);
116
117 }
118
119 msgb_put(msg, rval.encoded/8);
120
121 return msg;
122}
123
124
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200125struct msgb *rua_generate_initiating_message(
Harald Welte656ad302015-09-10 18:33:47 +0200126 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 Welte8dacb072015-12-16 20:27:14 +0100131 int rc;
Harald Welte656ad302015-09-10 18:33:47 +0200132
133 memset(&pdu, 0, sizeof(pdu));
Harald Welte8dacb072015-12-16 20:27:14 +0100134
Harald Welte656ad302015-09-10 18:33:47 +0200135 pdu.present = RUA_RUA_PDU_PR_initiatingMessage;
136 pdu.choice.initiatingMessage.procedureCode = procedureCode;
137 pdu.choice.initiatingMessage.criticality = criticality;
Harald Welte8dacb072015-12-16 20:27:14 +0100138 rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
139 if (rc < 0) {
140 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200141 return NULL;
Harald Welte656ad302015-09-10 18:33:47 +0200142 }
143
Harald Welte8dacb072015-12-16 20:27:14 +0100144 return _rua_gen_msg(&pdu);
Harald Welte656ad302015-09-10 18:33:47 +0200145}
Harald Welte656ad302015-09-10 18:33:47 +0200146
147struct 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 Welte656ad302015-09-10 18:33:47 +0200153 RUA_RUA_PDU_t pdu;
Harald Welte656ad302015-09-10 18:33:47 +0200154 int rc;
155
156 memset(&pdu, 0, sizeof(pdu));
Harald Welte8dacb072015-12-16 20:27:14 +0100157
Harald Welte656ad302015-09-10 18:33:47 +0200158 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) {
163 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte656ad302015-09-10 18:33:47 +0200164 return NULL;
165 }
166
Harald Welte8dacb072015-12-16 20:27:14 +0100167 return _rua_gen_msg(&pdu);
Harald Welte656ad302015-09-10 18:33:47 +0200168}
169
Harald Weltecbaaeef2015-12-16 20:17:26 +0100170struct 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 Welte656ad302015-09-10 18:33:47 +0200175{
Harald Welte656ad302015-09-10 18:33:47 +0200176 RUA_RUA_PDU_t pdu;
Harald Weltecbaaeef2015-12-16 20:17:26 +0100177 int rc;
Harald Welte656ad302015-09-10 18:33:47 +0200178
179 memset(&pdu, 0, sizeof(pdu));
Harald Welte8dacb072015-12-16 20:27:14 +0100180
Harald Welte656ad302015-09-10 18:33:47 +0200181 pdu.present = RUA_RUA_PDU_PR_unsuccessfulOutcome;
Harald Weltecbaaeef2015-12-16 20:17:26 +0100182 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) {
186 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltecbaaeef2015-12-16 20:17:26 +0100187 return NULL;
Harald Welte656ad302015-09-10 18:33:47 +0200188 }
189
Harald Welte8dacb072015-12-16 20:27:14 +0100190 return _rua_gen_msg(&pdu);
Harald Welte656ad302015-09-10 18:33:47 +0200191}
Harald Welte656ad302015-09-10 18:33:47 +0200192
193RUA_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;
199
200 if ((buff = malloc(sizeof(*buff))) == NULL) {
201 // Possible error on malloc
202 return NULL;
203 }
204 memset((void *)buff, 0, sizeof(*buff));
205
206 buff->id = id;
207 buff->criticality = criticality;
208
209 ANY_fromType_aper(&buff->value, type, sptr);
210
211 if (asn1_xer_print)
212 if (xer_fprint(stdout, &asn_DEF_RUA_IE, buff) < 0) {
213 free(buff);
214 return NULL;
215 }
216
217 return buff;
218}