blob: b3c769ff3bf006150760c006f12f916eaed68e8e [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 Welteeae30c32019-03-30 17:32:37 +0100279RsproPDU_t *rspro_gen_ConfigClientIdReq(const ClientSlot_t *client)
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 Welted571a3e2019-03-11 22:09:50 +0100285 pdu->msg.present = RsproPDUchoice_PR_configClientIdReq;
286 pdu->msg.choice.configClientIdReq.clientSlot = *client;
Harald Welte3aa901d2018-08-13 18:32:36 +0200287
288 return pdu;
289}
290
Harald Welted571a3e2019-03-11 22:09:50 +0100291RsproPDU_t *rspro_gen_ConfigClientIdRes(e_ResultCode res)
Harald Weltec0097b12019-03-02 14:22:24 +0100292{
293 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
294 if (!pdu)
295 return NULL;
296 pdu->version = 2;
Harald Welted571a3e2019-03-11 22:09:50 +0100297 pdu->msg.present = RsproPDUchoice_PR_configClientIdRes;
298 pdu->msg.choice.configClientIdRes.result = res;
299
300 return pdu;
301}
302
303RsproPDU_t *rspro_gen_ConfigClientBankReq(const BankSlot_t *bank, uint32_t ip, uint16_t port)
304{
305 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
306 if (!pdu)
307 return NULL;
308 pdu->version = 2;
309 pdu->msg.present = RsproPDUchoice_PR_configClientBankReq;
310 pdu->msg.choice.configClientBankReq.bankSlot = *bank;
311 fill_ip4_port(&pdu->msg.choice.configClientBankReq.bankd, ip, port);
312
313 return pdu;
314}
315
316RsproPDU_t *rspro_gen_ConfigClientBankRes(e_ResultCode res)
317{
318 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
319 if (!pdu)
320 return NULL;
321 pdu->version = 2;
322 pdu->msg.present = RsproPDUchoice_PR_configClientBankRes;
323 pdu->msg.choice.configClientBankRes.result = res;
Harald Weltec0097b12019-03-02 14:22:24 +0100324
325 return pdu;
326}
327
Harald Welte3aa901d2018-08-13 18:32:36 +0200328RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr,
329 unsigned int atr_len)
330{
331 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
332 if (!pdu)
333 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200334 pdu->version = 2;
Harald Welte3aa901d2018-08-13 18:32:36 +0200335 pdu->msg.present = RsproPDUchoice_PR_setAtrReq;
336 pdu->msg.choice.setAtrReq.slot.clientId = client_id;
337 pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr;
338 OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len);
339
340 return pdu;
341}
Harald Welte5d16b1c2018-09-23 19:25:46 +0200342
Harald Weltefa365592019-03-28 20:28:57 +0100343RsproPDU_t *rspro_gen_SetAtrRes(e_ResultCode res)
344{
345 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
346 if (!pdu)
347 return NULL;
348 pdu->version = 2;
349 pdu->msg.present = RsproPDUchoice_PR_setAtrRes;
350 pdu->msg.choice.setAtrRes.result = res;
351
352 return pdu;
353}
354
Harald Welte5d16b1c2018-09-23 19:25:46 +0200355RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank,
356 const uint8_t *tpdu, unsigned int tpdu_len)
357{
358 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
359 if (!pdu)
360 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200361 pdu->version = 2;
Harald Welte5d16b1c2018-09-23 19:25:46 +0200362 pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200363 OSMO_ASSERT(client);
Harald Welte5d16b1c2018-09-23 19:25:46 +0200364 pdu->msg.choice.tpduModemToCard.fromClientSlot = *client;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200365 OSMO_ASSERT(bank);
Harald Welte5d16b1c2018-09-23 19:25:46 +0200366 pdu->msg.choice.tpduModemToCard.toBankSlot = *bank;
367 /* TODO: flags? */
368 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len);
369
370 return pdu;
371}
372
373RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client,
374 const uint8_t *tpdu, unsigned int tpdu_len)
375{
376 RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu));
377 if (!pdu)
378 return NULL;
Harald Welte62e3f5f2018-10-15 00:44:25 +0200379 pdu->version = 2;
Harald Welte5d16b1c2018-09-23 19:25:46 +0200380 pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200381 OSMO_ASSERT(bank);
Harald Welte5d16b1c2018-09-23 19:25:46 +0200382 pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank;
Kévin Redon6811e4f2018-10-11 08:37:38 +0200383 OSMO_ASSERT(client)
Harald Welte5d16b1c2018-09-23 19:25:46 +0200384 pdu->msg.choice.tpduCardToModem.toClientSlot = *client;
385 /* TODO: flags? */
386 OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len);
387
388 return pdu;
389}
Harald Weltecf8b89a2019-03-11 22:16:22 +0100390
Harald Welte4e7a2852019-03-17 21:01:50 +0100391e_ResultCode rspro_get_result(const RsproPDU_t *pdu)
392{
393 switch (pdu->msg.present) {
394 case RsproPDUchoice_PR_connectBankRes:
395 return pdu->msg.choice.connectBankRes.result;
396 case RsproPDUchoice_PR_connectClientRes:
397 return pdu->msg.choice.connectClientRes.result;
398 case RsproPDUchoice_PR_createMappingRes:
399 return pdu->msg.choice.createMappingRes.result;
400 case RsproPDUchoice_PR_removeMappingRes:
401 return pdu->msg.choice.removeMappingRes.result;
402 case RsproPDUchoice_PR_configClientIdRes:
403 return pdu->msg.choice.configClientIdRes.result;
404 case RsproPDUchoice_PR_configClientBankRes:
405 return pdu->msg.choice.configClientBankRes.result;
406 case RsproPDUchoice_PR_setAtrRes:
407 return pdu->msg.choice.setAtrRes.result;
408 default:
409 OSMO_ASSERT(0);
410 }
411}
412
Harald Weltecf8b89a2019-03-11 22:16:22 +0100413void rspro2bank_slot(struct bank_slot *out, const BankSlot_t *in)
414{
415 out->bank_id = in->bankId;
416 out->slot_nr = in->slotNr;
417}
418
419void bank_slot2rspro(BankSlot_t *out, const struct bank_slot *in)
420{
421 out->bankId = in->bank_id;
422 out->slotNr = in->slot_nr;
423}
424
425void rspro2client_slot(struct client_slot *out, const ClientSlot_t *in)
426{
427 out->client_id = in->clientId;
428 out->slot_nr = in->slotNr;
429}
430
431void client_slot2rspro(ClientSlot_t *out, const struct client_slot *in)
432{
433 out->clientId = in->client_id;
434 out->slotNr = in->slot_nr;
435}