blob: 8c212d32445327c877bd26af3545be6ba3bb0915 [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* common RANAP 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
Harald Welteac9c0242015-09-10 21:18:16 +020021#include <stdint.h>
22
23#include <osmocom/core/msgb.h>
24
Harald Welte350814a2015-09-10 22:32:15 +020025//#include "ranap_common.h"
26#include "ranap/RANAP_RANAP-PDU.h"
27#include "ranap/RANAP_ProtocolIE-ID.h"
28#include "ranap/RANAP_IE.h"
Harald Welteac9c0242015-09-10 21:18:16 +020029#include "hnbgw.h"
30
31extern int asn1_xer_print;
32
33static struct msgb *ranap_msgb_alloc(void)
34{
35 return msgb_alloc(1024, "RANAP Tx");
36}
37
Harald Welte8dacb072015-12-16 20:27:14 +010038static struct msgb *_ranap_gen_msg(RANAP_RANAP_PDU_t *pdu)
Harald Welteac9c0242015-09-10 21:18:16 +020039{
Harald Weltecbaaeef2015-12-16 20:17:26 +010040 struct msgb *msg = ranap_msgb_alloc();
41 asn_enc_rval_t rval;
Harald Welteac9c0242015-09-10 21:18:16 +020042
Harald Welte8dacb072015-12-16 20:27:14 +010043 if (!msg)
Harald Weltecbaaeef2015-12-16 20:17:26 +010044 return NULL;
Harald Welteac9c0242015-09-10 21:18:16 +020045
Harald Welte8dacb072015-12-16 20:27:14 +010046 rval = aper_encode_to_buffer(&asn_DEF_RANAP_RANAP_PDU, pdu,
Harald Weltecbaaeef2015-12-16 20:17:26 +010047 msg->data, msgb_tailroom(msg));
48 if (rval.encoded < 0) {
49 LOGP(DMAIN, LOGL_ERROR, "Error encoding type: %s\n",
50 rval.failed_type->name);
51
52 }
53
54 msgb_put(msg, rval.encoded/8);
55
56 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +020057}
Harald Welteac9c0242015-09-10 21:18:16 +020058
Harald Welte8dacb072015-12-16 20:27:14 +010059struct msgb *ranap_generate_initiating_message(e_RANAP_ProcedureCode procedureCode,
60 RANAP_Criticality_t criticality,
61 asn_TYPE_descriptor_t *td, void *sptr)
62{
63 RANAP_RANAP_PDU_t pdu;
64 int rc;
65
66 memset(&pdu, 0, sizeof(pdu));
67
68 pdu.present = RANAP_RANAP_PDU_PR_initiatingMessage;
69 pdu.choice.initiatingMessage.procedureCode = procedureCode;
70 pdu.choice.initiatingMessage.criticality = criticality;
71 rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
72 if (rc < 0) {
73 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
74 return NULL;
75 }
76
77 return _ranap_gen_msg(&pdu);
78}
79
Harald Welteac9c0242015-09-10 21:18:16 +020080struct msgb *ranap_generate_successful_outcome(
81 e_RANAP_ProcedureCode procedureCode,
82 RANAP_Criticality_t criticality,
83 asn_TYPE_descriptor_t * td,
84 void *sptr)
85{
Harald Welteac9c0242015-09-10 21:18:16 +020086 RANAP_RANAP_PDU_t pdu;
Harald Welteac9c0242015-09-10 21:18:16 +020087 int rc;
88
89 memset(&pdu, 0, sizeof(pdu));
Harald Welte8dacb072015-12-16 20:27:14 +010090
Harald Welteac9c0242015-09-10 21:18:16 +020091 pdu.present = RANAP_RANAP_PDU_PR_successfulOutcome;
92 pdu.choice.successfulOutcome.procedureCode = procedureCode;
93 pdu.choice.successfulOutcome.criticality = criticality;
94 rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
95 if (rc < 0) {
96 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welteac9c0242015-09-10 21:18:16 +020097 return NULL;
98 }
99
Harald Welte8dacb072015-12-16 20:27:14 +0100100 return _ranap_gen_msg(&pdu);
Harald Welteac9c0242015-09-10 21:18:16 +0200101}
102
Harald Weltecbaaeef2015-12-16 20:17:26 +0100103struct msgb *ranap_generate_unsuccessful_outcome(
Harald Welteac9c0242015-09-10 21:18:16 +0200104 e_RANAP_ProcedureCode procedureCode,
105 RANAP_Criticality_t criticality,
106 asn_TYPE_descriptor_t * td,
107 void *sptr)
108{
Harald Welteac9c0242015-09-10 21:18:16 +0200109 RANAP_RANAP_PDU_t pdu;
Harald Weltecbaaeef2015-12-16 20:17:26 +0100110 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +0200111
112 memset(&pdu, 0, sizeof(pdu));
113
114 pdu.present = RANAP_RANAP_PDU_PR_unsuccessfulOutcome;
Harald Weltec16117a2015-12-16 20:30:11 +0100115 pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode;
116 pdu.choice.unsuccessfulOutcome.criticality = criticality;
117 rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr);
Harald Weltecbaaeef2015-12-16 20:17:26 +0100118 if (rc < 0) {
119 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltecbaaeef2015-12-16 20:17:26 +0100120 return NULL;
Harald Welteac9c0242015-09-10 21:18:16 +0200121 }
122
Harald Welte8dacb072015-12-16 20:27:14 +0100123 return _ranap_gen_msg(&pdu);
Harald Welteac9c0242015-09-10 21:18:16 +0200124}
Harald Welteac9c0242015-09-10 21:18:16 +0200125
Harald Weltec16117a2015-12-16 20:30:11 +0100126struct msgb *ranap_generate_outcome(
127 e_RANAP_ProcedureCode procedureCode,
128 RANAP_Criticality_t criticality,
129 asn_TYPE_descriptor_t * td,
130 void *sptr)
131{
132 RANAP_RANAP_PDU_t pdu;
133 int rc;
134
135 memset(&pdu, 0, sizeof(pdu));
136
137 pdu.present = RANAP_RANAP_PDU_PR_outcome;
138 pdu.choice.outcome.procedureCode = procedureCode;
139 pdu.choice.outcome.criticality = criticality;
140 rc = ANY_fromType_aper(&pdu.choice.outcome.value, td, sptr);
141 if (rc < 0) {
142 LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n");
143 return NULL;
144 }
145
146 return _ranap_gen_msg(&pdu);
147}
148
149
Harald Welteac9c0242015-09-10 21:18:16 +0200150RANAP_IE_t *ranap_new_ie(RANAP_ProtocolIE_ID_t id,
151 RANAP_Criticality_t criticality,
152 asn_TYPE_descriptor_t * type, void *sptr)
153{
154
155 RANAP_IE_t *buff;
156
157 if ((buff = malloc(sizeof(*buff))) == NULL) {
158 // Possible error on malloc
159 return NULL;
160 }
161 memset((void *)buff, 0, sizeof(*buff));
162
163 buff->id = id;
164 buff->criticality = criticality;
165
166 ANY_fromType_aper(&buff->value, type, sptr);
167
168 if (asn1_xer_print)
169 if (xer_fprint(stdout, &asn_DEF_RANAP_IE, buff) < 0) {
170 free(buff);
171 return NULL;
172 }
173
174 return buff;
175}