blob: 88798801d06c03e1506cbc65e4360bbae75cb0af [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 Welteace1d242015-12-16 23:07:19 +010025#include "ranap_common.h"
Harald Welteac9c0242015-09-10 21:18:16 +020026#include "hnbgw.h"
27
28extern int asn1_xer_print;
29
30static struct msgb *ranap_msgb_alloc(void)
31{
32 return msgb_alloc(1024, "RANAP Tx");
33}
34
Harald Welte8dacb072015-12-16 20:27:14 +010035static struct msgb *_ranap_gen_msg(RANAP_RANAP_PDU_t *pdu)
Harald Welteac9c0242015-09-10 21:18:16 +020036{
Harald Weltecbaaeef2015-12-16 20:17:26 +010037 struct msgb *msg = ranap_msgb_alloc();
38 asn_enc_rval_t rval;
Harald Welteac9c0242015-09-10 21:18:16 +020039
Harald Welte8dacb072015-12-16 20:27:14 +010040 if (!msg)
Harald Weltecbaaeef2015-12-16 20:17:26 +010041 return NULL;
Harald Welteac9c0242015-09-10 21:18:16 +020042
Harald Welte8dacb072015-12-16 20:27:14 +010043 rval = aper_encode_to_buffer(&asn_DEF_RANAP_RANAP_PDU, pdu,
Harald Weltecbaaeef2015-12-16 20:17:26 +010044 msg->data, msgb_tailroom(msg));
45 if (rval.encoded < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +010046 LOGP(DRANAP, LOGL_ERROR, "Error encoding type: %s\n",
Harald Weltecbaaeef2015-12-16 20:17:26 +010047 rval.failed_type->name);
48
49 }
50
51 msgb_put(msg, rval.encoded/8);
52
53 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +020054}
Harald Welteac9c0242015-09-10 21:18:16 +020055
Harald Welte8dacb072015-12-16 20:27:14 +010056struct msgb *ranap_generate_initiating_message(e_RANAP_ProcedureCode procedureCode,
57 RANAP_Criticality_t criticality,
58 asn_TYPE_descriptor_t *td, void *sptr)
59{
60 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +010061 struct msgb *msg;
Harald Welte8dacb072015-12-16 20:27:14 +010062 int rc;
63
64 memset(&pdu, 0, sizeof(pdu));
65
66 pdu.present = RANAP_RANAP_PDU_PR_initiatingMessage;
67 pdu.choice.initiatingMessage.procedureCode = procedureCode;
68 pdu.choice.initiatingMessage.criticality = criticality;
69 rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr);
70 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +010071 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte8dacb072015-12-16 20:27:14 +010072 return NULL;
73 }
74
Harald Welte26765542015-12-18 13:33:00 +010075 msg = _ranap_gen_msg(&pdu);
76 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
77
78 return msg;
Harald Welte8dacb072015-12-16 20:27:14 +010079}
80
Harald Welteac9c0242015-09-10 21:18:16 +020081struct msgb *ranap_generate_successful_outcome(
82 e_RANAP_ProcedureCode procedureCode,
83 RANAP_Criticality_t criticality,
84 asn_TYPE_descriptor_t * td,
85 void *sptr)
86{
Harald Welteac9c0242015-09-10 21:18:16 +020087 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +010088 struct msgb *msg;
Harald Welteac9c0242015-09-10 21:18:16 +020089 int rc;
90
91 memset(&pdu, 0, sizeof(pdu));
Harald Welte8dacb072015-12-16 20:27:14 +010092
Harald Welteac9c0242015-09-10 21:18:16 +020093 pdu.present = RANAP_RANAP_PDU_PR_successfulOutcome;
94 pdu.choice.successfulOutcome.procedureCode = procedureCode;
95 pdu.choice.successfulOutcome.criticality = criticality;
96 rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr);
97 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +010098 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welteac9c0242015-09-10 21:18:16 +020099 return NULL;
100 }
101
Harald Welte26765542015-12-18 13:33:00 +0100102 msg = _ranap_gen_msg(&pdu);
103 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
104
105 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200106}
107
Harald Weltecbaaeef2015-12-16 20:17:26 +0100108struct msgb *ranap_generate_unsuccessful_outcome(
Harald Welteac9c0242015-09-10 21:18:16 +0200109 e_RANAP_ProcedureCode procedureCode,
110 RANAP_Criticality_t criticality,
111 asn_TYPE_descriptor_t * td,
112 void *sptr)
113{
Harald Welteac9c0242015-09-10 21:18:16 +0200114 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +0100115 struct msgb *msg;
Harald Weltecbaaeef2015-12-16 20:17:26 +0100116 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +0200117
118 memset(&pdu, 0, sizeof(pdu));
119
120 pdu.present = RANAP_RANAP_PDU_PR_unsuccessfulOutcome;
Harald Weltec16117a2015-12-16 20:30:11 +0100121 pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode;
122 pdu.choice.unsuccessfulOutcome.criticality = criticality;
123 rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr);
Harald Weltecbaaeef2015-12-16 20:17:26 +0100124 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100125 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltecbaaeef2015-12-16 20:17:26 +0100126 return NULL;
Harald Welteac9c0242015-09-10 21:18:16 +0200127 }
128
Harald Welte26765542015-12-18 13:33:00 +0100129 msg = _ranap_gen_msg(&pdu);
130 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
131
132 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200133}
Harald Welteac9c0242015-09-10 21:18:16 +0200134
Harald Weltec16117a2015-12-16 20:30:11 +0100135struct msgb *ranap_generate_outcome(
136 e_RANAP_ProcedureCode procedureCode,
137 RANAP_Criticality_t criticality,
138 asn_TYPE_descriptor_t * td,
139 void *sptr)
140{
141 RANAP_RANAP_PDU_t pdu;
Harald Welte26765542015-12-18 13:33:00 +0100142 struct msgb *msg;
Harald Weltec16117a2015-12-16 20:30:11 +0100143 int rc;
144
145 memset(&pdu, 0, sizeof(pdu));
146
147 pdu.present = RANAP_RANAP_PDU_PR_outcome;
148 pdu.choice.outcome.procedureCode = procedureCode;
149 pdu.choice.outcome.criticality = criticality;
150 rc = ANY_fromType_aper(&pdu.choice.outcome.value, td, sptr);
151 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100152 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Weltec16117a2015-12-16 20:30:11 +0100153 return NULL;
154 }
155
Harald Welte26765542015-12-18 13:33:00 +0100156 msg = _ranap_gen_msg(&pdu);
157 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu);
158
159 return msg;
Harald Weltec16117a2015-12-16 20:30:11 +0100160}
161
162
Harald Welteac9c0242015-09-10 21:18:16 +0200163RANAP_IE_t *ranap_new_ie(RANAP_ProtocolIE_ID_t id,
164 RANAP_Criticality_t criticality,
165 asn_TYPE_descriptor_t * type, void *sptr)
166{
Harald Welteac9c0242015-09-10 21:18:16 +0200167 RANAP_IE_t *buff;
Harald Welte04329dc2015-12-18 15:17:21 +0100168 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +0200169
Harald Welte9c397d42015-12-18 13:33:20 +0100170 if ((buff = CALLOC(1, sizeof(*buff))) == NULL) {
Harald Welteac9c0242015-09-10 21:18:16 +0200171 // Possible error on malloc
172 return NULL;
173 }
Harald Welteac9c0242015-09-10 21:18:16 +0200174
175 buff->id = id;
176 buff->criticality = criticality;
177
Harald Welte04329dc2015-12-18 15:17:21 +0100178 rc = ANY_fromType_aper(&buff->value, type, sptr);
179 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100180 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte04329dc2015-12-18 15:17:21 +0100181 FREEMEM(buff);
182 return NULL;
183 }
Harald Welteac9c0242015-09-10 21:18:16 +0200184
185 if (asn1_xer_print)
186 if (xer_fprint(stdout, &asn_DEF_RANAP_IE, buff) < 0) {
Harald Welte62939132015-12-18 14:57:04 +0100187 FREEMEM(buff);
Harald Welteac9c0242015-09-10 21:18:16 +0200188 return NULL;
189 }
190
191 return buff;
192}
Harald Welteace1d242015-12-16 23:07:19 +0100193
194RANAP_ProtocolIE_FieldPair_t *ranap_new_ie_pair(RANAP_ProtocolIE_ID_t id,
195 RANAP_Criticality_t criticality1,
196 asn_TYPE_descriptor_t *type1, void *sptr1,
197 RANAP_Criticality_t criticality2,
198 asn_TYPE_descriptor_t *type2, void *sptr2)
199{
Harald Welteace1d242015-12-16 23:07:19 +0100200 RANAP_ProtocolIE_FieldPair_t *buff;
Harald Welte04329dc2015-12-18 15:17:21 +0100201 int rc;
Harald Welteace1d242015-12-16 23:07:19 +0100202
Harald Welte9c397d42015-12-18 13:33:20 +0100203 if ((buff = CALLOC(1, sizeof(*buff))) == NULL) {
Harald Welteace1d242015-12-16 23:07:19 +0100204 // Possible error on malloc
205 return NULL;
206 }
Harald Welteace1d242015-12-16 23:07:19 +0100207
208 buff->id = id;
209 buff->firstCriticality = criticality1;
210 buff->secondCriticality = criticality2;
211
Harald Welte04329dc2015-12-18 15:17:21 +0100212 rc = ANY_fromType_aper(&buff->firstValue, type1, sptr1);
213 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100214 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte04329dc2015-12-18 15:17:21 +0100215 FREEMEM(buff);
216 return NULL;
217 }
218
219 rc = ANY_fromType_aper(&buff->secondValue, type2, sptr2);
220 if (rc < 0) {
Harald Weltef42317b2015-12-23 15:36:31 +0100221 LOGP(DRANAP, LOGL_ERROR, "Error in ANY_fromType_aper\n");
Harald Welte16232782015-12-18 17:22:04 +0100222 ASN_STRUCT_FREE(asn_DEF_RANAP_ProtocolIE_FieldPair, buff);
Harald Welte04329dc2015-12-18 15:17:21 +0100223 return NULL;
224 }
Harald Welteace1d242015-12-16 23:07:19 +0100225
226 if (asn1_xer_print)
Harald Welte62939132015-12-18 14:57:04 +0100227 if (xer_fprint(stdout, &asn_DEF_RANAP_ProtocolIE_FieldPair, buff) < 0) {
Harald Welte16232782015-12-18 17:22:04 +0100228 ASN_STRUCT_FREE(asn_DEF_RANAP_ProtocolIE_FieldPair, buff);
Harald Welteace1d242015-12-16 23:07:19 +0100229 return NULL;
230 }
231
232 return buff;
233}