blob: 271ae86e18ae5b94511ba9eff730603a40a410f2 [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
Harald Welte3aa901d2018-08-13 18:32:36 +020022
23
Harald Welte0eaa0ef2019-03-02 15:12:42 +010024#include <netinet/in.h>
25#include <arpa/inet.h>
26
Kévin Redonff5db6e2018-10-11 17:16:18 +020027#include <asn_application.h>
28#include <der_encoder.h>
Harald Welte3aa901d2018-08-13 18:32:36 +020029
Harald Welteb49ac9c2019-03-09 20:36:07 +010030#include "asn1c_helpers.h"
31
Harald Welte3aa901d2018-08-13 18:32:36 +020032#include <osmocom/core/msgb.h>
33#include <osmocom/rspro/RsproPDU.h>
34
Harald Welte137c4402018-08-17 21:25:56 +020035#include "rspro_util.h"
36
Harald Welte57555aa2018-08-17 22:05:06 +020037#define ASN_ALLOC_COPY(out, in) \
38do { \
39 if (in) { \
40 out = CALLOC(1, sizeof(*in)); \
41 OSMO_ASSERT(out); \
42 memcpy(out, in, sizeof(*in)); \
43 } \
44} while (0)
45
46
Harald Welteb49ac9c2019-03-09 20:36:07 +010047const char *rspro_msgt_name(const RsproPDU_t *pdu)
48{
49 return asn_choice_name(&asn_DEF_RsproPDUchoice, &pdu->msg);
50}
51
Harald Welte3aa901d2018-08-13 18:32:36 +020052struct msgb *rspro_msgb_alloc(void)
53{
54 return msgb_alloc_headroom(1024, 8, "RSPRO");
55}
56
57/*! BER-Encode an RSPRO message into msgb.
58 * \param[in] pdu Structure describing RSPRO PDU. Is freed by this function on success
59 * \returns callee-allocated message buffer containing encoded RSPRO PDU; NULL on error.
60 */
61struct msgb *rspro_enc_msg(RsproPDU_t *pdu)
62{
63 struct msgb *msg = rspro_msgb_alloc();
64 asn_enc_rval_t rval;
65
66 if (!msg)
67 return NULL;
68
Harald Weltefd471192018-09-24 14:51:14 +020069 msg->l2h = msg->data;
Harald Welte6b8d4f82018-08-17 22:06:24 +020070 rval = der_encode_to_buffer(&asn_DEF_RsproPDU, pdu, msgb_data(msg), msgb_tailroom(msg));
Harald Welte3aa901d2018-08-13 18:32:36 +020071 if (rval.encoded < 0) {
Harald Weltea2b23c32018-08-17 22:09:06 +020072 fprintf(stderr, "Failed to encode %s\n", rval.failed_type->name);
Harald Welte703d6862018-09-24 17:44:50 +020073 msgb_free(msg);
Harald Welte3aa901d2018-08-13 18:32:36 +020074 return NULL;
75 }
Harald Welted5c5c0b2018-08-17 22:04:01 +020076 msgb_put(msg, rval.encoded);
Harald Welte3aa901d2018-08-13 18:32:36 +020077
78 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
79
80 return msg;
81}
82
83/* consumes 'msg' _if_ it is successful */
84RsproPDU_t *rspro_dec_msg(struct msgb *msg)
85{
Harald Welte9ebbacc2018-09-24 17:43:39 +020086 RsproPDU_t *pdu = NULL;
Harald Welte3aa901d2018-08-13 18:32:36 +020087 asn_dec_rval_t rval;
88
Harald Welte75852862018-09-24 14:55:34 +020089 printf("decoding %s\n", msgb_hexdump(msg));
90 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, msgb_l2(msg), msgb_l2len(msg));
Harald Welte3aa901d2018-08-13 18:32:36 +020091 if (rval.code != RC_OK) {
Harald Welte75852862018-09-24 14:55:34 +020092 fprintf(stderr, "Failed to decode: %d. Consumed %lu of %u bytes\n",
93 rval.code, rval.consumed, msgb_length(msg));
94 msgb_free(msg);
Harald Welte3aa901d2018-08-13 18:32:36 +020095 return NULL;
96 }
97
98 msgb_free(msg);
99
100 return pdu;
101}
102
Harald Welte3aa901d2018-08-13 18:32:36 +0200103static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in)
104{
Harald Welte137c4402018-08-17 21:25:56 +0200105 out->type = in->type;
Harald Welte3aa901d2018-08-13 18:32:36 +0200106 OCTET_STRING_fromString(&out->name, in->name);
107 OCTET_STRING_fromString(&out->software, in->software);
108 OCTET_STRING_fromString(&out->swVersion, in->sw_version);
109 if (strlen(in->hw_manufacturer))
110 out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName,
111 in->hw_manufacturer, -1);
112 if (strlen(in->hw_model))
113 out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1);
114 if (strlen(in->hw_serial_nr))
115 out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1);
116 if (strlen(in->hw_version))
117 out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1);
118 if (strlen(in->fw_version))
119 out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1);
120}
121
Harald Welte92bd8812019-03-02 14:52:17 +0100122void string_fromOCTET_STRING(char *out, size_t out_size, const OCTET_STRING_t *in)
123{
Harald Weltef5a0fa32019-03-03 15:44:18 +0100124 if (!in) {
Harald Welte92bd8812019-03-02 14:52:17 +0100125 out[0] = '\0';
Harald Weltef5a0fa32019-03-03 15:44:18 +0100126 return;
127 }
Harald Welte92bd8812019-03-02 14:52:17 +0100128 memcpy(out, in->buf, out_size < in->size ? out_size : in->size);
129 if (in->size < out_size)
130 out[in->size] = '\0';
131 else
132 out[out_size-1] = '\0';
133}
134#define string_fromOCTET_STRING_ARRAY(out, in) string_fromOCTET_STRING(out, ARRAY_SIZE(out), in)
135
136
137void rspro_comp_id_retrieve(struct app_comp_id *out, const ComponentIdentity_t *in)
138{
139 memset(out, 0, sizeof(*out));
140 out->type = in->type;
141 string_fromOCTET_STRING_ARRAY(out->name, &in->name);
142 string_fromOCTET_STRING_ARRAY(out->software, &in->software);
143 string_fromOCTET_STRING_ARRAY(out->sw_version, &in->swVersion);
144 string_fromOCTET_STRING_ARRAY(out->hw_manufacturer, in->hwManufacturer);
145 string_fromOCTET_STRING_ARRAY(out->hw_serial_nr, in->hwSerialNr);
146 string_fromOCTET_STRING_ARRAY(out->hw_version, in->hwVersion);
147 string_fromOCTET_STRING_ARRAY(out->fw_version, in->fwVersion);
148}
149
Harald Welte0eaa0ef2019-03-02 15:12:42 +0100150const char *rspro_IpAddr2str(const IpAddress_t *in)
151{
152 static char buf[128];
153
154 switch (in->present) {
155 case IpAddress_PR_ipv4:
156 return inet_ntop(AF_INET, in->choice.ipv4.buf, buf, sizeof(buf));
157 case IpAddress_PR_ipv6:
158 return inet_ntop(AF_INET6, in->choice.ipv6.buf, buf, sizeof(buf));
159 default:
160 return NULL;
161 }
162}
163
Harald Welte3aa901d2018-08-13 18:32:36 +0200164static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port)
165{
166 uint32_t ip_n = htonl(ip);
167 out->ip.present = IpAddress_PR_ipv4;
168 OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4);
169 out->port = htons(port);
170}
171
172
173RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid,
174 uint16_t bank_id, uint16_t num_slots)
175{
176 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
177 if (!pdu)
178 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200179 pdu->version = 2;
Harald Welte3aa901d2018-08-13 18:32:36 +0200180 pdu->msg.present = RsproPDUchoice_PR_connectBankReq;
181 fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid);
182 pdu->msg.choice.connectBankReq.bankId = bank_id;
183 pdu->msg.choice.connectBankReq.numberOfSlots = num_slots;
184
185 return pdu;
186}
187
Harald Weltef5a0fa32019-03-03 15:44:18 +0100188RsproPDU_t *rspro_gen_ConnectBankRes(const struct app_comp_id *a_cid, e_ResultCode res)
189{
190 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
191 if (!pdu)
192 return NULL;
193 pdu->version = 2;
194 pdu->msg.present = RsproPDUchoice_PR_connectBankRes;
195 fill_comp_id(&pdu->msg.choice.connectBankRes.identity, a_cid);
196 pdu->msg.choice.connectBankRes.result = res;
197
198 return pdu;
199}
200
Harald Welte57555aa2018-08-17 22:05:06 +0200201RsproPDU_t *rspro_gen_ConnectClientReq(const struct app_comp_id *a_cid, const ClientSlot_t *client)
Harald Welte3aa901d2018-08-13 18:32:36 +0200202{
203 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
204 if (!pdu)
205 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200206 pdu->version = 2;
Harald Welte3aa901d2018-08-13 18:32:36 +0200207 pdu->msg.present = RsproPDUchoice_PR_connectClientReq;
208 fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid);
Harald Weltec3632a72018-10-14 20:38:34 +0200209 if (client)
210 ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client);
Harald Welte3aa901d2018-08-13 18:32:36 +0200211
212 return pdu;
213}
214
Harald Welte10f6c212018-09-24 14:55:55 +0200215RsproPDU_t *rspro_gen_ConnectClientRes(const struct app_comp_id *a_cid, e_ResultCode res)
216{
217 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
218 if (!pdu)
219 return NULL;
220 pdu->version = 2;
221 pdu->tag = 2342;
222 pdu->msg.present = RsproPDUchoice_PR_connectClientRes;
223 fill_comp_id(&pdu->msg.choice.connectClientRes.identity, a_cid);
224 pdu->msg.choice.connectClientRes.result = res;
225
226 return pdu;
227}
228
Harald Welte3aa901d2018-08-13 18:32:36 +0200229RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank)
230{
231 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
232 if (!pdu)
233 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200234 pdu->version = 2;
Harald Welte3aa901d2018-08-13 18:32:36 +0200235 pdu->msg.present = RsproPDUchoice_PR_createMappingReq;
236 pdu->msg.choice.createMappingReq.client = *client;
237 pdu->msg.choice.createMappingReq.bank = *bank;
238
239 return pdu;
240}
241
Harald Weltec0a4ae42018-10-15 00:44:53 +0200242RsproPDU_t *rspro_gen_CreateMappingRes(e_ResultCode res)
243{
244 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
245 if (!pdu)
246 return NULL;
247 pdu->version = 2;
248 pdu->msg.present = RsproPDUchoice_PR_createMappingRes;
249 pdu->msg.choice.createMappingRes.result = res;
250
251 return pdu;
252}
253
254RsproPDU_t *rspro_gen_RemoveMappingReq(const ClientSlot_t *client, const BankSlot_t *bank)
255{
256 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
257 if (!pdu)
258 return NULL;
259 pdu->version = 2;
260 pdu->msg.present = RsproPDUchoice_PR_removeMappingReq;
261 pdu->msg.choice.removeMappingReq.client = *client;
262 pdu->msg.choice.removeMappingReq.bank = *bank;
263
264 return pdu;
265}
266
267RsproPDU_t *rspro_gen_RemoveMappingRes(e_ResultCode res)
268{
269 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
270 if (!pdu)
271 return NULL;
272 pdu->version = 2;
273 pdu->msg.present = RsproPDUchoice_PR_removeMappingRes;
274 pdu->msg.choice.removeMappingRes.result = res;
275
276 return pdu;
277}
278
Harald Welte371d0262018-08-16 15:23:58 +0200279RsproPDU_t *rspro_gen_ConfigClientReq(const ClientSlot_t *client, uint32_t ip, uint16_t port)
Harald Welte3aa901d2018-08-13 18:32:36 +0200280{
281 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
282 if (!pdu)
283 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200284 pdu->version = 2;
Harald Welte3aa901d2018-08-13 18:32:36 +0200285 pdu->msg.present = RsproPDUchoice_PR_configClientReq;
Harald Welte371d0262018-08-16 15:23:58 +0200286 pdu->msg.choice.configClientReq.clientSlot = *client;
Harald Welte3aa901d2018-08-13 18:32:36 +0200287 fill_ip4_port(&pdu->msg.choice.configClientReq.bankd, ip, port);
288
289 return pdu;
290}
291
Harald Weltec0097b12019-03-02 14:22:24 +0100292RsproPDU_t *rspro_gen_ConfigClientRes(e_ResultCode res)
293{
294 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
295 if (!pdu)
296 return NULL;
297 pdu->version = 2;
298 pdu->msg.present = RsproPDUchoice_PR_configClientRes;
299 pdu->msg.choice.configClientRes.result = res;
300
301 return pdu;
302}
303
Harald Welte3aa901d2018-08-13 18:32:36 +0200304RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr,
305 unsigned int atr_len)
306{
307 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
308 if (!pdu)
309 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200310 pdu->version = 2;
Harald Welte3aa901d2018-08-13 18:32:36 +0200311 pdu->msg.present = RsproPDUchoice_PR_setAtrReq;
312 pdu->msg.choice.setAtrReq.slot.clientId = client_id;
313 pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr;
314 OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len);
315
316 return pdu;
317}
Harald Welte5d16b1c2018-09-23 19:25:46 +0200318
319RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank,
320 const uint8_t *tpdu, unsigned int tpdu_len)
321{
322 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
323 if (!pdu)
324 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200325 pdu->version = 2;
Harald Welte5d16b1c2018-09-23 19:25:46 +0200326 pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200327 OSMO_ASSERT(client);
Harald Welte5d16b1c2018-09-23 19:25:46 +0200328 pdu->msg.choice.tpduModemToCard.fromClientSlot = *client;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200329 OSMO_ASSERT(bank);
Harald Welte5d16b1c2018-09-23 19:25:46 +0200330 pdu->msg.choice.tpduModemToCard.toBankSlot = *bank;
331 /* TODO: flags? */
332 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len);
333
334 return pdu;
335}
336
337RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client,
338 const uint8_t *tpdu, unsigned int tpdu_len)
339{
340 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
341 if (!pdu)
342 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200343 pdu->version = 2;
Harald Welte5d16b1c2018-09-23 19:25:46 +0200344 pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200345 OSMO_ASSERT(bank);
Harald Welte5d16b1c2018-09-23 19:25:46 +0200346 pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200347 OSMO_ASSERT(client)
Harald Welte5d16b1c2018-09-23 19:25:46 +0200348 pdu->msg.choice.tpduCardToModem.toClientSlot = *client;
349 /* TODO: flags? */
350 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len);
351
352 return pdu;
353}