blob: 8947ca541cf1f9da22edcab87eb07064b23efcc4 [file] [log] [blame]
Harald Welte350814a2015-09-10 22:32:15 +02001#include <osmocom/core/msgb.h>
2#include <osmocom/core/utils.h>
3
4#include <unistd.h>
5#include <errno.h>
6#include <string.h>
7
8#include "asn1helpers.h"
9
10#include "hnbgw.h"
Harald Weltee2e5d4d2015-09-10 23:49:45 +020011#include "hnbgw_rua.h"
Harald Welte350814a2015-09-10 22:32:15 +020012//#include "ranap_common.h"
13
14#include "ranap/RANAP_RANAP-PDU.h"
15#include "ranap/RANAP_ResetAcknowledge.h"
16#include "ranap/RANAP_Reset.h"
17#include "ranap/RANAP_ProtocolIE-ID.h"
18#include "ranap/RANAP_Cause.h"
19#include "ranap/RANAP_CN-DomainIndicator.h"
20#include "ranap/RANAP_GlobalRNC-ID.h"
21#include "ranap/RANAP_CriticalityDiagnostics.h"
22
Harald Welte350814a2015-09-10 22:32:15 +020023/***********************************************************************
24 * BEGIN auto-generated copy+pasted
25 ***********************************************************************/
26
27/* this is copy+pasted from the asn1tostruct generated code that doesn't
28 * compile as a whole */
29
30#define RANAP_DEBUG(x, args ...) DEBUGP(0, x, ## args)
31
32extern int asn1_xer_print;
33
34struct msgb *ranap_generate_successful_outcome(
35 e_RANAP_ProcedureCode procedureCode,
36 RANAP_Criticality_t criticality,
37 asn_TYPE_descriptor_t * td,
38 void *sptr);
39
40RANAP_IE_t *ranap_new_ie(RANAP_ProtocolIE_ID_t id, RANAP_Criticality_t criticality,
41 asn_TYPE_descriptor_t *type, void *sptr);
42
43#define RESETIES_RANAP_GLOBALRNC_ID_PRESENT (1 << 0)
44
45typedef struct RANAP_ResetIEs_s {
46 uint16_t presenceMask;
47 RANAP_Cause_t cause;
48 RANAP_CN_DomainIndicator_t cN_DomainIndicator;
49 RANAP_GlobalRNC_ID_t globalRNC_ID; ///< Optional field
50} RANAP_ResetIEs_t;
51
52#define RESETACKNOWLEDGEIES_RANAP_CRITICALITYDIAGNOSTICS_PRESENT (1 << 0)
53#define RESETACKNOWLEDGEIES_RANAP_GLOBALRNC_ID_PRESENT (1 << 1)
54
55typedef struct RANAP_ResetAcknowledgeIEs_s {
56 uint16_t presenceMask;
57 RANAP_CN_DomainIndicator_t cN_DomainIndicator;
58 RANAP_CriticalityDiagnostics_t criticalityDiagnostics; ///< Optional field
59 RANAP_GlobalRNC_ID_t globalRNC_ID; ///< Optional field
60} RANAP_ResetAcknowledgeIEs_t;
61
62int ranap_decode_reseties(
63 RANAP_ResetIEs_t *resetIEs,
64 ANY_t *any_p) {
65
66 RANAP_Reset_t reset;
67 RANAP_Reset_t *reset_p = &reset;
68 int i, decoded = 0;
69 int tempDecoded = 0;
70 assert(any_p != NULL);
71 assert(resetIEs != NULL);
72
73 RANAP_DEBUG("Decoding message RANAP_ResetIEs (%s:%d)\n", __FILE__, __LINE__);
74
75 ANY_to_type_aper(any_p, &asn_DEF_RANAP_Reset, (void**)&reset_p);
76
77 for (i = 0; i < reset_p->reset_ies.list.count; i++) {
78 RANAP_IE_t *ie_p;
79 ie_p = reset_p->reset_ies.list.array[i];
80 switch(ie_p->id) {
81 case RANAP_ProtocolIE_ID_id_Cause:
82 {
83 RANAP_Cause_t ranaP_Cause;
84 RANAP_Cause_t *ranaP_Cause_p = &ranaP_Cause;
85 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_Cause, (void**)&ranaP_Cause_p);
86 if (tempDecoded < 0) {
87 RANAP_DEBUG("Decoding of IE cause failed\n");
88 return -1;
89 }
90 decoded += tempDecoded;
91 if (asn1_xer_print)
92 xer_fprint(stdout, &asn_DEF_RANAP_Cause, ranaP_Cause_p);
93 memcpy(&resetIEs->cause, ranaP_Cause_p, sizeof(RANAP_Cause_t));
94 } break;
95 case RANAP_ProtocolIE_ID_id_CN_DomainIndicator:
96 {
97 RANAP_CN_DomainIndicator_t ranaP_CNDomainIndicator;
98 RANAP_CN_DomainIndicator_t *ranaP_CNDomainIndicator_p = &ranaP_CNDomainIndicator;
99 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_CN_DomainIndicator, (void**)&ranaP_CNDomainIndicator_p);
100 if (tempDecoded < 0) {
101 RANAP_DEBUG("Decoding of IE cN_DomainIndicator failed\n");
102 return -1;
103 }
104 decoded += tempDecoded;
105 if (asn1_xer_print)
106 xer_fprint(stdout, &asn_DEF_RANAP_CN_DomainIndicator, ranaP_CNDomainIndicator_p);
107 memcpy(&resetIEs->cN_DomainIndicator, ranaP_CNDomainIndicator_p, sizeof(RANAP_CN_DomainIndicator_t));
108 } break;
109 /* Optional field */
110 case RANAP_ProtocolIE_ID_id_GlobalRNC_ID:
111 {
112#if 0
113 RANAP_GlobalRNC_ID_t ranaP_GlobalRNCID;
114 RANAP_GlobalRNC_ID_t *ranaP_GlobalRNCID_p = &ranaP_GlobalRNCID;
115 resetIEs->presenceMask |= RESETIES_RANAP_GLOBALRNC_ID_PRESENT;
116 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_GlobalRNC_ID, (void**)&ranaP_GlobalRNCID_p);
117 if (tempDecoded < 0) {
118 RANAP_DEBUG("Decoding of IE globalRNC_ID failed\n");
119 return -1;
120 }
121 decoded += tempDecoded;
122 if (asn1_xer_print)
123 xer_fprint(stdout, &asn_DEF_RANAP_GlobalRNC_ID, ranaP_GlobalRNCID_p);
124 memcpy(&resetIEs->globalRNC_ID, ranaP_GlobalRNCID_p, sizeof(RANAP_GlobalRNC_ID_t));
125#endif
126 } break;
127 default:
128 RANAP_DEBUG("Unknown protocol IE id (%d) for message reseties\n", (int)ie_p->id);
129 return -1;
130 }
131 }
132 return decoded;
133}
134
135int ranap_encode_resetacknowledgeies(
136 RANAP_ResetAcknowledge_t *resetAcknowledge,
137 RANAP_ResetAcknowledgeIEs_t *resetAcknowledgeIEs) {
138
139 RANAP_IE_t *ie;
140
141 if ((ie = ranap_new_ie(RANAP_ProtocolIE_ID_id_CN_DomainIndicator,
142 RANAP_Criticality_reject,
143 &asn_DEF_RANAP_CN_DomainIndicator,
144 &resetAcknowledgeIEs->cN_DomainIndicator)) == NULL) {
145 return -1;
146 }
147 ASN_SEQUENCE_ADD(&resetAcknowledge->resetAcknowledge_ies.list, ie);
148
149 /* Optional field */
150 if ((resetAcknowledgeIEs->presenceMask & RESETACKNOWLEDGEIES_RANAP_CRITICALITYDIAGNOSTICS_PRESENT)
151 == RESETACKNOWLEDGEIES_RANAP_CRITICALITYDIAGNOSTICS_PRESENT) {
152 if ((ie = ranap_new_ie(RANAP_ProtocolIE_ID_id_CriticalityDiagnostics,
153 RANAP_Criticality_ignore,
154 &asn_DEF_RANAP_CriticalityDiagnostics,
155 &resetAcknowledgeIEs->criticalityDiagnostics)) == NULL) {
156 return -1;
157 }
158 ASN_SEQUENCE_ADD(&resetAcknowledge->resetAcknowledge_ies.list, ie);
159 }
160
161 /* Optional field */
162 if ((resetAcknowledgeIEs->presenceMask & RESETACKNOWLEDGEIES_RANAP_GLOBALRNC_ID_PRESENT)
163 == RESETACKNOWLEDGEIES_RANAP_GLOBALRNC_ID_PRESENT) {
164 if ((ie = ranap_new_ie(RANAP_ProtocolIE_ID_id_GlobalRNC_ID,
165 RANAP_Criticality_ignore,
166 &asn_DEF_RANAP_GlobalRNC_ID,
167 &resetAcknowledgeIEs->globalRNC_ID)) == NULL) {
168 return -1;
169 }
170 ASN_SEQUENCE_ADD(&resetAcknowledge->resetAcknowledge_ies.list, ie);
171 }
172
173 return 0;
174}
175
Harald Weltecc27beb2015-09-11 18:45:39 +0200176#include "ranap/RANAP_InitialUE-Message.h"
177#include "ranap/RANAP_CN-DomainIndicator.h"
178#include "ranap/RANAP_LAI.h"
179#include "ranap/RANAP_SAI.h"
180#include "ranap/RANAP_NAS-PDU.h"
181#include "ranap/RANAP_IuSignallingConnectionIdentifier.h"
182#include "ranap/RANAP_GlobalCN-ID.h"
183
184typedef struct RANAP_InitialUE_MessageIEs_s {
185 RANAP_CN_DomainIndicator_t cN_DomainIndicator;
186 RANAP_LAI_t lai;
187 RANAP_SAI_t sai;
188 RANAP_NAS_PDU_t nas_pdu;
189 RANAP_IuSignallingConnectionIdentifier_t iuSigConId;
190 RANAP_GlobalRNC_ID_t globalRNC_ID;
191} RANAP_InitialUE_MessageIEs_t;
192
193int ranap_decode_initialue_messageies(
194 RANAP_InitialUE_MessageIEs_t *initialUE_MessageIEs,
195 ANY_t *any_p) {
196
197 RANAP_InitialUE_Message_t initialUE_Message;
198 RANAP_InitialUE_Message_t *initialUE_Message_p = &initialUE_Message;
199 int i, decoded = 0;
200 int tempDecoded = 0;
201 assert(any_p != NULL);
202 assert(initialUE_MessageIEs != NULL);
203
204 RANAP_DEBUG("Decoding message RANAP_InitialUE_MessageIEs (%s:%d)\n", __FILE__, __LINE__);
205
206 ANY_to_type_aper(any_p, &asn_DEF_RANAP_InitialUE_Message, (void**)&initialUE_Message_p);
207
208 for (i = 0; i < initialUE_Message_p->initialUE_Message_ies.list.count; i++) {
209 RANAP_IE_t *ie_p;
210 ie_p = initialUE_Message_p->initialUE_Message_ies.list.array[i];
211 switch(ie_p->id) {
212 case RANAP_ProtocolIE_ID_id_CN_DomainIndicator:
213 {
214 RANAP_CN_DomainIndicator_t ranaP_CNDomainIndicator;
215 RANAP_CN_DomainIndicator_t *ranaP_CNDomainIndicator_p = &ranaP_CNDomainIndicator;
216 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_CN_DomainIndicator, (void**)&ranaP_CNDomainIndicator_p);
217 if (tempDecoded < 0) {
218 RANAP_DEBUG("Decoding of IE cN_DomainIndicator failed\n");
219 return -1;
220 }
221 decoded += tempDecoded;
222 if (asn1_xer_print)
223 xer_fprint(stdout, &asn_DEF_RANAP_CN_DomainIndicator, ranaP_CNDomainIndicator_p);
224 memcpy(&initialUE_MessageIEs->cN_DomainIndicator, ranaP_CNDomainIndicator_p, sizeof(RANAP_CN_DomainIndicator_t));
225 } break;
226 case RANAP_ProtocolIE_ID_id_LAI:
227 {
228 RANAP_LAI_t ranap_lai;
229 RANAP_LAI_t *ranap_lai_p = &ranap_lai;
230 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_LAI, (void**)&ranap_lai_p);
231 if (tempDecoded < 0) {
232 RANAP_DEBUG("Decoding of IE lai failed\n");
233 return -1;
234 }
235 decoded += tempDecoded;
236 if (asn1_xer_print)
237 xer_fprint(stdout, &asn_DEF_RANAP_LAI, ranap_lai_p);
238 memcpy(&initialUE_MessageIEs->lai, ranap_lai_p, sizeof(RANAP_LAI_t));
239 } break;
240 case RANAP_ProtocolIE_ID_id_SAI:
241 {
242 RANAP_SAI_t ranap_sai;
243 RANAP_SAI_t *ranap_sai_p = &ranap_sai;
244 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_SAI, (void**)&ranap_sai_p);
245 if (tempDecoded < 0) {
246 RANAP_DEBUG("Decoding of IE sai failed\n");
247 return -1;
248 }
249 decoded += tempDecoded;
250 if (asn1_xer_print)
251 xer_fprint(stdout, &asn_DEF_RANAP_SAI, ranap_sai_p);
252 memcpy(&initialUE_MessageIEs->sai, ranap_sai_p, sizeof(RANAP_SAI_t));
253 } break;
254 case RANAP_ProtocolIE_ID_id_NAS_PDU:
255 {
256 RANAP_NAS_PDU_t ranap_naspdu;
257 RANAP_NAS_PDU_t *ranap_naspdu_p = &ranap_naspdu;
258 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_NAS_PDU, (void**)&ranap_naspdu_p);
259 if (tempDecoded < 0) {
260 RANAP_DEBUG("Decoding of IE nas_pdu failed\n");
261 return -1;
262 }
263 decoded += tempDecoded;
264 if (asn1_xer_print)
265 xer_fprint(stdout, &asn_DEF_RANAP_NAS_PDU, ranap_naspdu_p);
266 memcpy(&initialUE_MessageIEs->nas_pdu, ranap_naspdu_p, sizeof(RANAP_NAS_PDU_t));
267 } break;
268 case RANAP_ProtocolIE_ID_id_IuSigConId:
269 {
270 RANAP_IuSignallingConnectionIdentifier_t ranaP_IuSignallingConnectionIdentifier;
271 RANAP_IuSignallingConnectionIdentifier_t *ranaP_IuSignallingConnectionIdentifier_p = &ranaP_IuSignallingConnectionIdentifier;
272 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_IuSignallingConnectionIdentifier, (void**)&ranaP_IuSignallingConnectionIdentifier_p);
273 if (tempDecoded < 0) {
274 RANAP_DEBUG("Decoding of IE iuSigConId failed\n");
275 return -1;
276 }
277 decoded += tempDecoded;
278 if (asn1_xer_print)
279 xer_fprint(stdout, &asn_DEF_RANAP_IuSignallingConnectionIdentifier, ranaP_IuSignallingConnectionIdentifier_p);
280 memcpy(&initialUE_MessageIEs->iuSigConId, ranaP_IuSignallingConnectionIdentifier_p, sizeof(RANAP_IuSignallingConnectionIdentifier_t));
281 } break;
282 case RANAP_ProtocolIE_ID_id_GlobalRNC_ID:
283 {
284 RANAP_GlobalRNC_ID_t ranaP_GlobalRNCID;
285 RANAP_GlobalRNC_ID_t *ranaP_GlobalRNCID_p = &ranaP_GlobalRNCID;
286 tempDecoded = ANY_to_type_aper(&ie_p->value, &asn_DEF_RANAP_GlobalRNC_ID, (void**)&ranaP_GlobalRNCID_p);
287 if (tempDecoded < 0) {
288 RANAP_DEBUG("Decoding of IE globalRNC_ID failed\n");
289 return -1;
290 }
291 decoded += tempDecoded;
292 if (asn1_xer_print)
293 xer_fprint(stdout, &asn_DEF_RANAP_GlobalRNC_ID, ranaP_GlobalRNCID_p);
294 memcpy(&initialUE_MessageIEs->globalRNC_ID, ranaP_GlobalRNCID_p, sizeof(RANAP_GlobalRNC_ID_t));
295 } break;
296 default:
297 RANAP_DEBUG("Unknown protocol IE id (%d) for message initialue_messageies\n", (int)ie_p->id);
298 return -1;
299 }
300 }
301 return decoded;
302}
303
304
Harald Welte350814a2015-09-10 22:32:15 +0200305/***********************************************************************
306 * END auto-generated copy+pasted
307 ***********************************************************************/
308
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200309static int ranap_tx_reset_ack(struct hnb_context *hnb,
310 RANAP_CN_DomainIndicator_t domain)
Harald Welte350814a2015-09-10 22:32:15 +0200311{
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200312 RANAP_ResetAcknowledge_t out;
313 RANAP_ResetAcknowledgeIEs_t ies;
314 struct msgb *msg;
315 int rc;
Harald Welte350814a2015-09-10 22:32:15 +0200316
317 memset(&ies, 0, sizeof(ies));
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200318 ies.cN_DomainIndicator = domain;
Harald Welte350814a2015-09-10 22:32:15 +0200319
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200320 memset(&out, 0, sizeof(out));
321 rc = ranap_encode_resetacknowledgeies(&out, &ies);
322 if (rc < 0) {
323 LOGP(DMAIN, LOGL_ERROR, "error encoding reset ack IEs: %d\n", rc);
324 return rc;
325 }
326
327 msg = ranap_generate_successful_outcome(RANAP_ProcedureCode_id_Reset,
328 RANAP_Criticality_reject,
329 &asn_DEF_RANAP_ResetAcknowledge,
330 &out);
331 if (!msg)
332 return -1;
333
334 msg->dst = hnb;
335
336 rc = rua_tx_udt(msg);
337
338 return rc;
Harald Welte350814a2015-09-10 22:32:15 +0200339}
Harald Welte350814a2015-09-10 22:32:15 +0200340
341static int ranap_rx_init_reset(struct hnb_context *hnb, ANY_t *in)
342{
343 RANAP_ResetIEs_t ies;
344 int rc;
345
346 rc = ranap_decode_reseties(&ies, in);
347 if (rc < 0)
348 return rc;
349
350 DEBUGP(DMAIN, "RESET.req\n");
351
Harald Weltee2e5d4d2015-09-10 23:49:45 +0200352 ranap_tx_reset_ack(hnb, ies.cN_DomainIndicator);
353
Harald Welte350814a2015-09-10 22:32:15 +0200354 return 0;
355}
356
357static int ranap_rx_initiating_msg(struct hnb_context *hnb, RANAP_InitiatingMessage_t *imsg)
358{
359 int rc;
360
361 switch (imsg->procedureCode) {
362 case RANAP_ProcedureCode_id_Reset:
363 rc = ranap_rx_init_reset(hnb, &imsg->value);
364 break;
365 }
366}
367
368static int _hnbgw_ranap_rx(struct hnb_context *hnb, RANAP_RANAP_PDU_t *pdu)
369{
370 int rc;
371
372 switch (pdu->present) {
373 case RANAP_RANAP_PDU_PR_initiatingMessage:
374 rc = ranap_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
375 break;
376 case RANAP_RANAP_PDU_PR_successfulOutcome:
377 break;
378 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
379 break;
380 default:
381 break;
382 }
383}
384
385
386int hnbgw_ranap_rx(struct msgb *msg, uint8_t *data, size_t len)
387{
388 RANAP_RANAP_PDU_t _pdu, *pdu = &_pdu;
389 asn_dec_rval_t dec_ret;
390 int rc;
391
392 memset(pdu, 0, sizeof(*pdu));
393 dec_ret = aper_decode(NULL,&asn_DEF_RANAP_RANAP_PDU, (void **) &pdu,
394 data, len, 0, 0);
395 if (dec_ret.code != RC_OK) {
396 LOGP(DMAIN, LOGL_ERROR, "Error in RANAP ASN.1 decode\n");
397 return rc;
398 }
399
400 rc = _hnbgw_ranap_rx(msg->dst, pdu);
401
402 return rc;
403}
404
405int hnbgw_ranap_init(void)
406{
407
408}