Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 1 | |
| 2 | |
Harald Welte | 0eaa0ef | 2019-03-02 15:12:42 +0100 | [diff] [blame] | 3 | #include <netinet/in.h> |
| 4 | #include <arpa/inet.h> |
| 5 | |
Kévin Redon | ff5db6e | 2018-10-11 17:16:18 +0200 | [diff] [blame] | 6 | #include <asn_application.h> |
| 7 | #include <der_encoder.h> |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 8 | |
| 9 | #include <osmocom/core/msgb.h> |
| 10 | #include <osmocom/rspro/RsproPDU.h> |
| 11 | |
Harald Welte | 137c440 | 2018-08-17 21:25:56 +0200 | [diff] [blame] | 12 | #include "rspro_util.h" |
| 13 | |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 14 | #define ASN_ALLOC_COPY(out, in) \ |
| 15 | do { \ |
| 16 | if (in) { \ |
| 17 | out = CALLOC(1, sizeof(*in)); \ |
| 18 | OSMO_ASSERT(out); \ |
| 19 | memcpy(out, in, sizeof(*in)); \ |
| 20 | } \ |
| 21 | } while (0) |
| 22 | |
| 23 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 24 | struct msgb *rspro_msgb_alloc(void) |
| 25 | { |
| 26 | return msgb_alloc_headroom(1024, 8, "RSPRO"); |
| 27 | } |
| 28 | |
| 29 | /*! BER-Encode an RSPRO message into msgb. |
| 30 | * \param[in] pdu Structure describing RSPRO PDU. Is freed by this function on success |
| 31 | * \returns callee-allocated message buffer containing encoded RSPRO PDU; NULL on error. |
| 32 | */ |
| 33 | struct msgb *rspro_enc_msg(RsproPDU_t *pdu) |
| 34 | { |
| 35 | struct msgb *msg = rspro_msgb_alloc(); |
| 36 | asn_enc_rval_t rval; |
| 37 | |
| 38 | if (!msg) |
| 39 | return NULL; |
| 40 | |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 41 | msg->l2h = msg->data; |
Harald Welte | 6b8d4f8 | 2018-08-17 22:06:24 +0200 | [diff] [blame] | 42 | rval = der_encode_to_buffer(&asn_DEF_RsproPDU, pdu, msgb_data(msg), msgb_tailroom(msg)); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 43 | if (rval.encoded < 0) { |
Harald Welte | a2b23c3 | 2018-08-17 22:09:06 +0200 | [diff] [blame] | 44 | fprintf(stderr, "Failed to encode %s\n", rval.failed_type->name); |
Harald Welte | 703d686 | 2018-09-24 17:44:50 +0200 | [diff] [blame] | 45 | msgb_free(msg); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 46 | return NULL; |
| 47 | } |
Harald Welte | d5c5c0b | 2018-08-17 22:04:01 +0200 | [diff] [blame] | 48 | msgb_put(msg, rval.encoded); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 49 | |
| 50 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
| 51 | |
| 52 | return msg; |
| 53 | } |
| 54 | |
| 55 | /* consumes 'msg' _if_ it is successful */ |
| 56 | RsproPDU_t *rspro_dec_msg(struct msgb *msg) |
| 57 | { |
Harald Welte | 9ebbacc | 2018-09-24 17:43:39 +0200 | [diff] [blame] | 58 | RsproPDU_t *pdu = NULL; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 59 | asn_dec_rval_t rval; |
| 60 | |
Harald Welte | 7585286 | 2018-09-24 14:55:34 +0200 | [diff] [blame] | 61 | printf("decoding %s\n", msgb_hexdump(msg)); |
| 62 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, msgb_l2(msg), msgb_l2len(msg)); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 63 | if (rval.code != RC_OK) { |
Harald Welte | 7585286 | 2018-09-24 14:55:34 +0200 | [diff] [blame] | 64 | fprintf(stderr, "Failed to decode: %d. Consumed %lu of %u bytes\n", |
| 65 | rval.code, rval.consumed, msgb_length(msg)); |
| 66 | msgb_free(msg); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | msgb_free(msg); |
| 71 | |
| 72 | return pdu; |
| 73 | } |
| 74 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 75 | static void fill_comp_id(ComponentIdentity_t *out, const struct app_comp_id *in) |
| 76 | { |
Harald Welte | 137c440 | 2018-08-17 21:25:56 +0200 | [diff] [blame] | 77 | out->type = in->type; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 78 | OCTET_STRING_fromString(&out->name, in->name); |
| 79 | OCTET_STRING_fromString(&out->software, in->software); |
| 80 | OCTET_STRING_fromString(&out->swVersion, in->sw_version); |
| 81 | if (strlen(in->hw_manufacturer)) |
| 82 | out->hwManufacturer = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, |
| 83 | in->hw_manufacturer, -1); |
| 84 | if (strlen(in->hw_model)) |
| 85 | out->hwModel = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_model, -1); |
| 86 | if (strlen(in->hw_serial_nr)) |
| 87 | out->hwSerialNr = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_serial_nr, -1); |
| 88 | if (strlen(in->hw_version)) |
| 89 | out->hwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->hw_version, -1); |
| 90 | if (strlen(in->fw_version)) |
| 91 | out->fwVersion = OCTET_STRING_new_fromBuf(&asn_DEF_ComponentName, in->fw_version, -1); |
| 92 | } |
| 93 | |
Harald Welte | 92bd881 | 2019-03-02 14:52:17 +0100 | [diff] [blame] | 94 | void string_fromOCTET_STRING(char *out, size_t out_size, const OCTET_STRING_t *in) |
| 95 | { |
| 96 | if (!in) |
| 97 | out[0] = '\0'; |
| 98 | memcpy(out, in->buf, out_size < in->size ? out_size : in->size); |
| 99 | if (in->size < out_size) |
| 100 | out[in->size] = '\0'; |
| 101 | else |
| 102 | out[out_size-1] = '\0'; |
| 103 | } |
| 104 | #define string_fromOCTET_STRING_ARRAY(out, in) string_fromOCTET_STRING(out, ARRAY_SIZE(out), in) |
| 105 | |
| 106 | |
| 107 | void rspro_comp_id_retrieve(struct app_comp_id *out, const ComponentIdentity_t *in) |
| 108 | { |
| 109 | memset(out, 0, sizeof(*out)); |
| 110 | out->type = in->type; |
| 111 | string_fromOCTET_STRING_ARRAY(out->name, &in->name); |
| 112 | string_fromOCTET_STRING_ARRAY(out->software, &in->software); |
| 113 | string_fromOCTET_STRING_ARRAY(out->sw_version, &in->swVersion); |
| 114 | string_fromOCTET_STRING_ARRAY(out->hw_manufacturer, in->hwManufacturer); |
| 115 | string_fromOCTET_STRING_ARRAY(out->hw_serial_nr, in->hwSerialNr); |
| 116 | string_fromOCTET_STRING_ARRAY(out->hw_version, in->hwVersion); |
| 117 | string_fromOCTET_STRING_ARRAY(out->fw_version, in->fwVersion); |
| 118 | } |
| 119 | |
Harald Welte | 0eaa0ef | 2019-03-02 15:12:42 +0100 | [diff] [blame] | 120 | const char *rspro_IpAddr2str(const IpAddress_t *in) |
| 121 | { |
| 122 | static char buf[128]; |
| 123 | |
| 124 | switch (in->present) { |
| 125 | case IpAddress_PR_ipv4: |
| 126 | return inet_ntop(AF_INET, in->choice.ipv4.buf, buf, sizeof(buf)); |
| 127 | case IpAddress_PR_ipv6: |
| 128 | return inet_ntop(AF_INET6, in->choice.ipv6.buf, buf, sizeof(buf)); |
| 129 | default: |
| 130 | return NULL; |
| 131 | } |
| 132 | } |
| 133 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 134 | static void fill_ip4_port(IpPort_t *out, uint32_t ip, uint16_t port) |
| 135 | { |
| 136 | uint32_t ip_n = htonl(ip); |
| 137 | out->ip.present = IpAddress_PR_ipv4; |
| 138 | OCTET_STRING_fromBuf(&out->ip.choice.ipv4, (const char *) &ip_n, 4); |
| 139 | out->port = htons(port); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | RsproPDU_t *rspro_gen_ConnectBankReq(const struct app_comp_id *a_cid, |
| 144 | uint16_t bank_id, uint16_t num_slots) |
| 145 | { |
| 146 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 147 | if (!pdu) |
| 148 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 149 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 150 | pdu->msg.present = RsproPDUchoice_PR_connectBankReq; |
| 151 | fill_comp_id(&pdu->msg.choice.connectBankReq.identity, a_cid); |
| 152 | pdu->msg.choice.connectBankReq.bankId = bank_id; |
| 153 | pdu->msg.choice.connectBankReq.numberOfSlots = num_slots; |
| 154 | |
| 155 | return pdu; |
| 156 | } |
| 157 | |
Harald Welte | 57555aa | 2018-08-17 22:05:06 +0200 | [diff] [blame] | 158 | RsproPDU_t *rspro_gen_ConnectClientReq(const struct app_comp_id *a_cid, const ClientSlot_t *client) |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 159 | { |
| 160 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 161 | if (!pdu) |
| 162 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 163 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 164 | pdu->msg.present = RsproPDUchoice_PR_connectClientReq; |
| 165 | fill_comp_id(&pdu->msg.choice.connectClientReq.identity, a_cid); |
Harald Welte | c3632a7 | 2018-10-14 20:38:34 +0200 | [diff] [blame] | 166 | if (client) |
| 167 | ASN_ALLOC_COPY(pdu->msg.choice.connectClientReq.clientSlot, client); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 168 | |
| 169 | return pdu; |
| 170 | } |
| 171 | |
Harald Welte | 10f6c21 | 2018-09-24 14:55:55 +0200 | [diff] [blame] | 172 | RsproPDU_t *rspro_gen_ConnectClientRes(const struct app_comp_id *a_cid, e_ResultCode res) |
| 173 | { |
| 174 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 175 | if (!pdu) |
| 176 | return NULL; |
| 177 | pdu->version = 2; |
| 178 | pdu->tag = 2342; |
| 179 | pdu->msg.present = RsproPDUchoice_PR_connectClientRes; |
| 180 | fill_comp_id(&pdu->msg.choice.connectClientRes.identity, a_cid); |
| 181 | pdu->msg.choice.connectClientRes.result = res; |
| 182 | |
| 183 | return pdu; |
| 184 | } |
| 185 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 186 | RsproPDU_t *rspro_gen_CreateMappingReq(const ClientSlot_t *client, const BankSlot_t *bank) |
| 187 | { |
| 188 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 189 | if (!pdu) |
| 190 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 191 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 192 | pdu->msg.present = RsproPDUchoice_PR_createMappingReq; |
| 193 | pdu->msg.choice.createMappingReq.client = *client; |
| 194 | pdu->msg.choice.createMappingReq.bank = *bank; |
| 195 | |
| 196 | return pdu; |
| 197 | } |
| 198 | |
Harald Welte | c0a4ae4 | 2018-10-15 00:44:53 +0200 | [diff] [blame] | 199 | RsproPDU_t *rspro_gen_CreateMappingRes(e_ResultCode res) |
| 200 | { |
| 201 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 202 | if (!pdu) |
| 203 | return NULL; |
| 204 | pdu->version = 2; |
| 205 | pdu->msg.present = RsproPDUchoice_PR_createMappingRes; |
| 206 | pdu->msg.choice.createMappingRes.result = res; |
| 207 | |
| 208 | return pdu; |
| 209 | } |
| 210 | |
| 211 | RsproPDU_t *rspro_gen_RemoveMappingReq(const ClientSlot_t *client, const BankSlot_t *bank) |
| 212 | { |
| 213 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 214 | if (!pdu) |
| 215 | return NULL; |
| 216 | pdu->version = 2; |
| 217 | pdu->msg.present = RsproPDUchoice_PR_removeMappingReq; |
| 218 | pdu->msg.choice.removeMappingReq.client = *client; |
| 219 | pdu->msg.choice.removeMappingReq.bank = *bank; |
| 220 | |
| 221 | return pdu; |
| 222 | } |
| 223 | |
| 224 | RsproPDU_t *rspro_gen_RemoveMappingRes(e_ResultCode res) |
| 225 | { |
| 226 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 227 | if (!pdu) |
| 228 | return NULL; |
| 229 | pdu->version = 2; |
| 230 | pdu->msg.present = RsproPDUchoice_PR_removeMappingRes; |
| 231 | pdu->msg.choice.removeMappingRes.result = res; |
| 232 | |
| 233 | return pdu; |
| 234 | } |
| 235 | |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 236 | RsproPDU_t *rspro_gen_ConfigClientReq(const ClientSlot_t *client, uint32_t ip, uint16_t port) |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 237 | { |
| 238 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 239 | if (!pdu) |
| 240 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 241 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 242 | pdu->msg.present = RsproPDUchoice_PR_configClientReq; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 243 | pdu->msg.choice.configClientReq.clientSlot = *client; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 244 | fill_ip4_port(&pdu->msg.choice.configClientReq.bankd, ip, port); |
| 245 | |
| 246 | return pdu; |
| 247 | } |
| 248 | |
Harald Welte | c0097b1 | 2019-03-02 14:22:24 +0100 | [diff] [blame] | 249 | RsproPDU_t *rspro_gen_ConfigClientRes(e_ResultCode res) |
| 250 | { |
| 251 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 252 | if (!pdu) |
| 253 | return NULL; |
| 254 | pdu->version = 2; |
| 255 | pdu->msg.present = RsproPDUchoice_PR_configClientRes; |
| 256 | pdu->msg.choice.configClientRes.result = res; |
| 257 | |
| 258 | return pdu; |
| 259 | } |
| 260 | |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 261 | RsproPDU_t *rspro_gen_SetAtrReq(uint16_t client_id, uint16_t slot_nr, const uint8_t *atr, |
| 262 | unsigned int atr_len) |
| 263 | { |
| 264 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 265 | if (!pdu) |
| 266 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 267 | pdu->version = 2; |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 268 | pdu->msg.present = RsproPDUchoice_PR_setAtrReq; |
| 269 | pdu->msg.choice.setAtrReq.slot.clientId = client_id; |
| 270 | pdu->msg.choice.setAtrReq.slot.slotNr = slot_nr; |
| 271 | OCTET_STRING_fromBuf(&pdu->msg.choice.setAtrReq.atr, (const char *)atr, atr_len); |
| 272 | |
| 273 | return pdu; |
| 274 | } |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 275 | |
| 276 | RsproPDU_t *rspro_gen_TpduModem2Card(const ClientSlot_t *client, const BankSlot_t *bank, |
| 277 | const uint8_t *tpdu, unsigned int tpdu_len) |
| 278 | { |
| 279 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 280 | if (!pdu) |
| 281 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 282 | pdu->version = 2; |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 283 | pdu->msg.present = RsproPDUchoice_PR_tpduModemToCard; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 284 | OSMO_ASSERT(client); |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 285 | pdu->msg.choice.tpduModemToCard.fromClientSlot = *client; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 286 | OSMO_ASSERT(bank); |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 287 | pdu->msg.choice.tpduModemToCard.toBankSlot = *bank; |
| 288 | /* TODO: flags? */ |
| 289 | OCTET_STRING_fromBuf(&pdu->msg.choice.tpduModemToCard.data, (const char *)tpdu, tpdu_len); |
| 290 | |
| 291 | return pdu; |
| 292 | } |
| 293 | |
| 294 | RsproPDU_t *rspro_gen_TpduCard2Modem(const BankSlot_t *bank, const ClientSlot_t *client, |
| 295 | const uint8_t *tpdu, unsigned int tpdu_len) |
| 296 | { |
| 297 | RsproPDU_t *pdu = CALLOC(1, sizeof(*pdu)); |
| 298 | if (!pdu) |
| 299 | return NULL; |
Harald Welte | 62e3f5f | 2018-10-15 00:44:25 +0200 | [diff] [blame] | 300 | pdu->version = 2; |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 301 | pdu->msg.present = RsproPDUchoice_PR_tpduCardToModem; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 302 | OSMO_ASSERT(bank); |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 303 | pdu->msg.choice.tpduCardToModem.fromBankSlot = *bank; |
Kévin Redon | 6811e4f | 2018-10-11 08:37:38 +0200 | [diff] [blame] | 304 | OSMO_ASSERT(client) |
Harald Welte | 5d16b1c | 2018-09-23 19:25:46 +0200 | [diff] [blame] | 305 | pdu->msg.choice.tpduCardToModem.toClientSlot = *client; |
| 306 | /* TODO: flags? */ |
| 307 | OCTET_STRING_fromBuf(&pdu->msg.choice.tpduCardToModem.data, (const char *)tpdu, tpdu_len); |
| 308 | |
| 309 | return pdu; |
| 310 | } |