Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* 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 Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <osmocom/core/msgb.h> |
| 24 | |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 25 | #include "ranap_common.h" |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 26 | #include "hnbgw.h" |
| 27 | |
| 28 | extern int asn1_xer_print; |
| 29 | |
| 30 | static struct msgb *ranap_msgb_alloc(void) |
| 31 | { |
| 32 | return msgb_alloc(1024, "RANAP Tx"); |
| 33 | } |
| 34 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 35 | static struct msgb *_ranap_gen_msg(RANAP_RANAP_PDU_t *pdu) |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 36 | { |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 37 | struct msgb *msg = ranap_msgb_alloc(); |
| 38 | asn_enc_rval_t rval; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 39 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 40 | if (!msg) |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 41 | return NULL; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 42 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 43 | rval = aper_encode_to_buffer(&asn_DEF_RANAP_RANAP_PDU, pdu, |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 44 | msg->data, msgb_tailroom(msg)); |
| 45 | if (rval.encoded < 0) { |
| 46 | LOGP(DMAIN, LOGL_ERROR, "Error encoding type: %s\n", |
| 47 | rval.failed_type->name); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | msgb_put(msg, rval.encoded/8); |
| 52 | |
| 53 | return msg; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 54 | } |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 55 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 56 | struct 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 Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 61 | struct msgb *msg; |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 62 | 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) { |
| 71 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Harald Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 75 | msg = _ranap_gen_msg(&pdu); |
| 76 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu); |
| 77 | |
| 78 | return msg; |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 81 | struct 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 Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 87 | RANAP_RANAP_PDU_t pdu; |
Harald Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 88 | struct msgb *msg; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 89 | int rc; |
| 90 | |
| 91 | memset(&pdu, 0, sizeof(pdu)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 92 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 93 | 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) { |
| 98 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
| 101 | |
Harald Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 102 | msg = _ranap_gen_msg(&pdu); |
| 103 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu); |
| 104 | |
| 105 | return msg; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 108 | struct msgb *ranap_generate_unsuccessful_outcome( |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 109 | e_RANAP_ProcedureCode procedureCode, |
| 110 | RANAP_Criticality_t criticality, |
| 111 | asn_TYPE_descriptor_t * td, |
| 112 | void *sptr) |
| 113 | { |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 114 | RANAP_RANAP_PDU_t pdu; |
Harald Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 115 | struct msgb *msg; |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 116 | int rc; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 117 | |
| 118 | memset(&pdu, 0, sizeof(pdu)); |
| 119 | |
| 120 | pdu.present = RANAP_RANAP_PDU_PR_unsuccessfulOutcome; |
Harald Welte | c16117a | 2015-12-16 20:30:11 +0100 | [diff] [blame] | 121 | pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode; |
| 122 | pdu.choice.unsuccessfulOutcome.criticality = criticality; |
| 123 | rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr); |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 124 | if (rc < 0) { |
| 125 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 126 | return NULL; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Harald Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 129 | msg = _ranap_gen_msg(&pdu); |
| 130 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu); |
| 131 | |
| 132 | return msg; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 133 | } |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 134 | |
Harald Welte | c16117a | 2015-12-16 20:30:11 +0100 | [diff] [blame] | 135 | struct 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 Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 142 | struct msgb *msg; |
Harald Welte | c16117a | 2015-12-16 20:30:11 +0100 | [diff] [blame] | 143 | 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) { |
| 152 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Harald Welte | 2676554 | 2015-12-18 13:33:00 +0100 | [diff] [blame] | 156 | msg = _ranap_gen_msg(&pdu); |
| 157 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RANAP_RANAP_PDU, &pdu); |
| 158 | |
| 159 | return msg; |
Harald Welte | c16117a | 2015-12-16 20:30:11 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 163 | RANAP_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 Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 167 | RANAP_IE_t *buff; |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 168 | int rc; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 169 | |
Harald Welte | 9c397d4 | 2015-12-18 13:33:20 +0100 | [diff] [blame] | 170 | if ((buff = CALLOC(1, sizeof(*buff))) == NULL) { |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 171 | // Possible error on malloc |
| 172 | return NULL; |
| 173 | } |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 174 | |
| 175 | buff->id = id; |
| 176 | buff->criticality = criticality; |
| 177 | |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 178 | rc = ANY_fromType_aper(&buff->value, type, sptr); |
| 179 | if (rc < 0) { |
| 180 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 181 | FREEMEM(buff); |
| 182 | return NULL; |
| 183 | } |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 184 | |
| 185 | if (asn1_xer_print) |
| 186 | if (xer_fprint(stdout, &asn_DEF_RANAP_IE, buff) < 0) { |
Harald Welte | 6293913 | 2015-12-18 14:57:04 +0100 | [diff] [blame] | 187 | FREEMEM(buff); |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | return buff; |
| 192 | } |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 193 | |
| 194 | RANAP_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 Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 200 | RANAP_ProtocolIE_FieldPair_t *buff; |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 201 | int rc; |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 202 | |
Harald Welte | 9c397d4 | 2015-12-18 13:33:20 +0100 | [diff] [blame] | 203 | if ((buff = CALLOC(1, sizeof(*buff))) == NULL) { |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 204 | // Possible error on malloc |
| 205 | return NULL; |
| 206 | } |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 207 | |
| 208 | buff->id = id; |
| 209 | buff->firstCriticality = criticality1; |
| 210 | buff->secondCriticality = criticality2; |
| 211 | |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 212 | rc = ANY_fromType_aper(&buff->firstValue, type1, sptr1); |
| 213 | if (rc < 0) { |
| 214 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 215 | FREEMEM(buff); |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | rc = ANY_fromType_aper(&buff->secondValue, type2, sptr2); |
| 220 | if (rc < 0) { |
| 221 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | 1623278 | 2015-12-18 17:22:04 +0100 | [diff] [blame] | 222 | ASN_STRUCT_FREE(asn_DEF_RANAP_ProtocolIE_FieldPair, buff); |
Harald Welte | 04329dc | 2015-12-18 15:17:21 +0100 | [diff] [blame] | 223 | return NULL; |
| 224 | } |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 225 | |
| 226 | if (asn1_xer_print) |
Harald Welte | 6293913 | 2015-12-18 14:57:04 +0100 | [diff] [blame] | 227 | if (xer_fprint(stdout, &asn_DEF_RANAP_ProtocolIE_FieldPair, buff) < 0) { |
Harald Welte | 1623278 | 2015-12-18 17:22:04 +0100 | [diff] [blame] | 228 | ASN_STRUCT_FREE(asn_DEF_RANAP_ProtocolIE_FieldPair, buff); |
Harald Welte | ace1d24 | 2015-12-16 23:07:19 +0100 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | return buff; |
| 233 | } |