blob: 43d7c3707138dcc9adab89dcb35ab590e15b5439 [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* 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 Welte27f9c4a2015-08-30 22:47:18 +020022#include <stdint.h>
23
24#include <osmocom/core/msgb.h>
25
26#include "HNBAP-PDU.h"
27#include "hnbap_common.h"
Harald Weltec060b7b2015-09-07 22:40:41 +020028#include "hnbgw.h"
Harald Welte27f9c4a2015-08-30 22:47:18 +020029
Harald Welte3af1db82015-09-11 17:03:16 +020030static 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
50static 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
57static 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
72static 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
80char *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
109
Harald Welte27f9c4a2015-08-30 22:47:18 +0200110int asn_debug = 0;
111int asn1_xer_print = 0;
112
113static struct msgb *hnbap_msgb_alloc(void)
114{
115 return msgb_alloc(1024, "HNBAP Tx");
116}
117
Daniel Willmann0e8ef672015-12-07 17:19:40 +0100118struct msgb *hnbap_generate_initiating_message(
Harald Welte27f9c4a2015-08-30 22:47:18 +0200119 e_ProcedureCode procedureCode,
120 Criticality_t criticality,
121 asn_TYPE_descriptor_t * td, void *sptr)
122{
123
124 HNBAP_PDU_t pdu;
Daniel Willmann0e8ef672015-12-07 17:19:40 +0100125 struct msgb *msg = hnbap_msgb_alloc();
126 asn_enc_rval_t rval;
127 int rc;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200128
129 memset(&pdu, 0, sizeof(HNBAP_PDU_t));
130
131 pdu.present = HNBAP_PDU_PR_initiatingMessage;
132 pdu.choice.initiatingMessage.procedureCode = procedureCode;
133 pdu.choice.initiatingMessage.criticality = criticality;
Daniel Willmann0e8ef672015-12-07 17:19:40 +0100134 rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
135 if (rc < 0) {
136 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
137 msgb_free(msg);
138 return NULL;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200139 }
140
Daniel Willmann0e8ef672015-12-07 17:19:40 +0100141 rval = aper_encode_to_buffer(&asn_DEF_HNBAP_PDU, &pdu,
142 msg->data, msgb_tailroom(msg));
143 if (rval.encoded < 0) {
144 LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name);
145 msgb_free(msg);
146 return NULL;
147 }
148
149 msgb_put(msg, rval.encoded/8);
150 return msg;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200151}
Harald Welte27f9c4a2015-08-30 22:47:18 +0200152
Harald Welte339b8e22015-08-30 23:08:32 +0200153struct msgb *hnbap_generate_successful_outcome(
Harald Welte27f9c4a2015-08-30 22:47:18 +0200154 e_ProcedureCode procedureCode,
155 Criticality_t criticality,
156 asn_TYPE_descriptor_t * td,
157 void *sptr)
158{
159
160 HNBAP_PDU_t pdu;
161 struct msgb *msg = hnbap_msgb_alloc();
162 asn_enc_rval_t rval;
Harald Weltec060b7b2015-09-07 22:40:41 +0200163 int rc;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200164
165 memset(&pdu, 0, sizeof(HNBAP_PDU_t));
166 pdu.present = HNBAP_PDU_PR_successfulOutcome;
167 pdu.choice.successfulOutcome.procedureCode = procedureCode;
168 pdu.choice.successfulOutcome.criticality = criticality;
Harald Weltec060b7b2015-09-07 22:40:41 +0200169 rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
170 if (rc < 0) {
171 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
172 msgb_free(msg);
173 return NULL;
174 }
Harald Welte27f9c4a2015-08-30 22:47:18 +0200175
176 rval = aper_encode_to_buffer(&asn_DEF_HNBAP_PDU, &pdu,
Harald Welte1c1c53c2015-09-07 22:41:02 +0200177 msg->data, msgb_tailroom(msg));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200178 if (rval.encoded < 0) {
Harald Weltec060b7b2015-09-07 22:40:41 +0200179 LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200180 msgb_free(msg);
181 return NULL;
182 }
183
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200184 msgb_put(msg, rval.encoded/8);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200185
186 return msg;
187}
188
189#if 0
Harald Welte339b8e22015-08-30 23:08:32 +0200190ssize_t s1ap_generate_unsuccessful_outcome(uint8_t ** buffer,
Harald Welte27f9c4a2015-08-30 22:47:18 +0200191 uint32_t * length,
192 e_ProcedureCode procedureCode,
193 Criticality_t criticality,
194 asn_TYPE_descriptor_t * td,
195 void *sptr)
196{
197
198 HNBAP_PDU_t pdu;
199 ssize_t encoded;
200
201 memset(&pdu, 0, sizeof(HNBAP_PDU_t));
202
203 pdu.present = HNBAP_PDU_PR_unsuccessfulOutcome;
204 pdu.choice.successfulOutcome.procedureCode = procedureCode;
205 pdu.choice.successfulOutcome.criticality = criticality;
206 ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
207
208 if ((encoded =
209 aper_encode_to_new_buffer(&asn_DEF_HNBAP_PDU, 0, &pdu,
210 (void **)buffer)) < 0) {
211 return -1;
212 }
213
214 *length = encoded;
215
216 return encoded;
217}
218#endif
219
220IE_t *hnbap_new_ie(ProtocolIE_ID_t id,
221 Criticality_t criticality,
222 asn_TYPE_descriptor_t * type, void *sptr)
223{
224
225 IE_t *buff;
226
227 if ((buff = malloc(sizeof(IE_t))) == NULL) {
228 // Possible error on malloc
229 return NULL;
230 }
231 memset((void *)buff, 0, sizeof(IE_t));
232
233 buff->id = id;
234 buff->criticality = criticality;
235
236 ANY_fromType_aper(&buff->value, type, sptr);
237
238 if (asn1_xer_print)
239 if (xer_fprint(stdout, &asn_DEF_IE, buff) < 0) {
240 free(buff);
241 return NULL;
242 }
243
244 return buff;
245}