Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <assert.h> |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 5 | #include <string.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 6 | |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/utils.h> |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 9 | #include <osmocom/core/logging.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 10 | |
| 11 | #include "ccid_proto.h" |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 12 | #include "ccid_device.h" |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 13 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 14 | /* decode on-the-wire T0 parameters into their parsed form */ |
| 15 | static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in) |
| 16 | { |
| 17 | /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */ |
| 18 | if (in->bmTCCKST0 & 0xFD) |
| 19 | return -11; |
| 20 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 21 | if (in->bClockStop & 0xFC) |
| 22 | return -14; |
| 23 | |
| 24 | out->fi = in->bmFindexDindex >> 4; |
| 25 | out->di = in->bmFindexDindex & 0xF; |
| 26 | if (in->bmTCCKST0 & 2) |
| 27 | out->inverse_convention = true; |
| 28 | else |
| 29 | out->inverse_convention = false; |
| 30 | if (in->bGuardTimeT0 == 0xff) |
| 31 | out->t0.guard_time_etu = 0; |
| 32 | else |
| 33 | out->t0.guard_time_etu = in->bGuardTimeT0; |
| 34 | out->t0.waiting_integer = in->bWaitingIntegerT0; |
| 35 | out->clock_stop = in->bClockStop & 0x03; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | /* encode T0 parameters from parsed form into on-the-wire encoding */ |
| 41 | static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in) |
| 42 | { |
| 43 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 44 | if (in->inverse_convention) |
| 45 | out->bmTCCKST0 = 0x02; |
| 46 | else |
| 47 | out->bmTCCKST0 = 0x00; |
| 48 | out->bGuardTimeT0 = in->t0.guard_time_etu; |
| 49 | out->bWaitingIntegerT0 = in->t0.waiting_integer; |
| 50 | out->bClockStop = in->clock_stop & 0x03; |
| 51 | } |
| 52 | |
| 53 | /* decode on-the-wire T1 parameters into their parsed form */ |
| 54 | static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in) |
| 55 | { |
| 56 | /* input validation: only some values permitted for bmTCCKST0 */ |
| 57 | if (in->bmTCCKST1 & 0xE8) |
| 58 | return -11; |
| 59 | /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */ |
| 60 | if (in->bWaitingIntegersT1 > 0x9F) |
| 61 | return -13; |
| 62 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 63 | if (in->bClockStop & 0xFC) |
| 64 | return -14; |
| 65 | /* input validation: only 0x00 to 0xFE permitted for bIFSC */ |
| 66 | if (in->bIFSC > 0xFE) |
| 67 | return -15; |
| 68 | |
| 69 | out->fi = in->bmFindexDindex >> 4; |
| 70 | out->di = in->bmFindexDindex & 0xF; |
| 71 | if (in->bmTCCKST1 & 1) |
| 72 | out->t1.csum_type = CCID_CSUM_TYPE_CRC; |
| 73 | else |
| 74 | out->t1.csum_type = CCID_CSUM_TYPE_LRC; |
| 75 | if (in->bmTCCKST1 & 2) |
| 76 | out->inverse_convention = true; |
| 77 | else |
| 78 | out->inverse_convention = false; |
| 79 | out->t1.guard_time_t1 = in->bGuardTimeT1; |
| 80 | out->t1.bwi = in->bWaitingIntegersT1 >> 4; |
| 81 | out->t1.cwi = in->bWaitingIntegersT1 & 0xF; |
| 82 | out->clock_stop = in->bClockStop & 0x03; |
| 83 | out->t1.ifsc = in->bIFSC; |
| 84 | out->t1.nad = in->bNadValue; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* encode T1 parameters from parsed form into on-the-wire encoding */ |
| 90 | static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in) |
| 91 | { |
| 92 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 93 | out->bmTCCKST1 = 0x10; |
| 94 | if (in->t1.csum_type == CCID_CSUM_TYPE_CRC) |
| 95 | out->bmTCCKST1 |= 0x01; |
| 96 | if (in->inverse_convention) |
| 97 | out->bmTCCKST1 |= 0x02; |
| 98 | out->bGuardTimeT1 = in->t1.guard_time_t1; |
| 99 | out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F); |
| 100 | out->bClockStop = in->clock_stop & 0x03; |
| 101 | out->bIFSC = in->t1.ifsc; |
| 102 | out->bNadValue = in->t1.nad; |
| 103 | } |
| 104 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 105 | #define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x) |
| 106 | #define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x) |
| 107 | |
| 108 | static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr) |
| 109 | { |
| 110 | if (slot_nr >= sizeof(ci->slot)) |
| 111 | return NULL; |
| 112 | else |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 113 | return &ci->slot[slot_nr]; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static uint8_t get_icc_status(const struct ccid_slot *cs) |
| 117 | { |
| 118 | if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset) |
| 119 | return CCID_ICC_STATUS_PRES_ACT; |
| 120 | else if (!cs->icc_present) |
| 121 | return CCID_ICC_STATUS_NO_ICC; |
| 122 | else |
| 123 | return CCID_ICC_STATUS_PRES_INACT; |
| 124 | } |
| 125 | |
| 126 | #define SET_HDR(x, msg_type, slot, seq) do { \ |
| 127 | (x)->hdr.bMessageType = msg_type; \ |
| 128 | (x)->hdr.dwLength = 0; \ |
| 129 | (x)->hdr.bSlot = slot; \ |
| 130 | (x)->hdr.bSeq = seq; \ |
| 131 | } while (0) |
| 132 | |
| 133 | #define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \ |
| 134 | SET_HDR(&(x)->hdr, msg_type, slot, seq); \ |
| 135 | (x)->hdr.bStatus = status; \ |
| 136 | (x)->hdr.bError = error; \ |
| 137 | } while (0) |
| 138 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 139 | #if 0 |
| 140 | static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u) |
| 141 | { |
| 142 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 143 | return ch->bSeq; |
| 144 | } |
| 145 | #endif |
| 146 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 147 | /*********************************************************************** |
| 148 | * Message generation / sending |
| 149 | ***********************************************************************/ |
| 150 | |
| 151 | static struct msgb *ccid_msgb_alloc(void) |
| 152 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 153 | struct msgb *msg = msgb_alloc(512, "ccid"); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 154 | OSMO_ASSERT(msg); |
| 155 | return msg; |
| 156 | } |
| 157 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 158 | /* Send given CCID message */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 159 | static int ccid_send(struct ccid_instance *ci, struct msgb *msg) |
| 160 | { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 161 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
| 162 | struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot); |
| 163 | if (cs) { |
| 164 | LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 165 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 166 | } else { |
| 167 | LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 168 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 169 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 170 | return ci->ops->send_in(ci, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 173 | /* Send given CCID message for given slot; patch bSlot into message */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 174 | static int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg) |
| 175 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 176 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 177 | |
| 178 | /* patch bSlotNr into message */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 179 | ch->bSlot = cs->slot_nr; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 180 | return ccid_send(cs->ci, msg); |
| 181 | } |
| 182 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 183 | /* Send given CCID message and mark slot as un-busy */ |
| 184 | static int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg) |
| 185 | { |
| 186 | cs->cmd_busy = false; |
| 187 | return ccid_slot_send(cs, msg); |
| 188 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 189 | |
| 190 | /* Section 6.2.1 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 191 | static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 192 | uint8_t cmd_sts, enum ccid_error_code err, |
| 193 | const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 194 | { |
| 195 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 196 | struct ccid_rdr_to_pc_data_block *db = |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 197 | (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 198 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 199 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 200 | SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 201 | osmo_store32le(data_len, &db->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 202 | memcpy(db->abData, data, data_len); |
| 203 | return msg; |
| 204 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 205 | static struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 206 | enum ccid_error_code err, const uint8_t *data, |
| 207 | uint32_t data_len) |
| 208 | { |
| 209 | return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 210 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 211 | |
| 212 | /* Section 6.2.2 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 213 | static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status, |
| 214 | uint8_t seq, uint8_t cmd_sts, |
| 215 | enum ccid_error_code err) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 216 | { |
| 217 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 218 | struct ccid_rdr_to_pc_slot_status *ss = |
| 219 | (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 220 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 221 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 222 | SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 223 | return msg; |
| 224 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 225 | static struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 226 | enum ccid_error_code err) |
| 227 | { |
| 228 | return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err); |
| 229 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 230 | |
| 231 | /* Section 6.2.3 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 232 | static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status, |
| 233 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 234 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 235 | { |
| 236 | struct msgb *msg = ccid_msgb_alloc(); |
| 237 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 238 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 239 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 240 | |
| 241 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 242 | if (dec_par) { |
| 243 | osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength); |
| 244 | encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 245 | } |
| 246 | return msg; |
| 247 | } |
| 248 | static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 249 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 250 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 251 | return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status, |
| 255 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 256 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 257 | { |
| 258 | struct msgb *msg = ccid_msgb_alloc(); |
| 259 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 260 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 261 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 262 | |
| 263 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 264 | if (dec_par) { |
| 265 | osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength); |
| 266 | encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 267 | } |
| 268 | return msg; |
| 269 | } |
| 270 | static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 271 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 272 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 273 | return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 276 | |
| 277 | /* Section 6.2.4 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 278 | static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts, |
| 279 | enum ccid_error_code err, const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 280 | { |
| 281 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 282 | struct ccid_rdr_to_pc_escape *esc = |
| 283 | (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 284 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 285 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 286 | SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 287 | osmo_store32le(data_len, &esc->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 288 | memcpy(esc->abData, data, data_len); |
| 289 | return msg; |
| 290 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 291 | static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 292 | enum ccid_error_code err, const uint8_t *data, |
| 293 | uint32_t data_len) |
| 294 | { |
| 295 | return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 296 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 297 | |
| 298 | /* Section 6.2.5 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 299 | static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 300 | uint8_t cmd_sts, enum ccid_error_code err, |
| 301 | uint32_t clock_khz, uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 302 | { |
| 303 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 304 | struct ccid_rdr_to_pc_data_rate_and_clock *drc = |
| 305 | (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 306 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 307 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 308 | SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 309 | osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */ |
| 310 | osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */ |
| 311 | osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 312 | return msg; |
| 313 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 314 | static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 315 | enum ccid_error_code err, uint32_t clock_khz, |
| 316 | uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 317 | { |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 318 | return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, |
| 319 | clock_khz, rate_bps); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 320 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 321 | |
| 322 | /*! generate an error response for given input message_type/slot_nr/seq |
| 323 | * \param[in] msg_type CCID Message Type against which response is to be created |
| 324 | * \param[in] slot_nr CCID Slot Number |
| 325 | * \param[in] icc_status ICC Status of the slot |
| 326 | * \param[in] seq CCID Sequence number |
| 327 | * \param[in] err_code CCID Error Code to send |
| 328 | * \returns dynamically-allocated message buffer containing error response */ |
| 329 | static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status, |
| 330 | uint8_t seq, enum ccid_error_code err_code) |
| 331 | { |
| 332 | struct msgb *resp = NULL; |
| 333 | |
| 334 | switch (msg_type) { |
| 335 | case PC_to_RDR_IccPowerOn: |
| 336 | case PC_to_RDR_XfrBlock: |
| 337 | case PC_to_RDR_Secure: |
| 338 | /* Return RDR_to_PC_DataBlock */ |
| 339 | resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 340 | err_code, NULL, 0); |
| 341 | break; |
| 342 | |
| 343 | case PC_to_RDR_IccPowerOff: |
| 344 | case PC_to_RDR_GetSlotStatus: |
| 345 | case PC_to_RDR_IccClock: |
| 346 | case PC_to_RDR_T0APDU: |
| 347 | case PC_to_RDR_Mechanical: |
| 348 | case PC_to_RDR_Abort: |
| 349 | /* Return RDR_to_PC_SlotStatus */ |
| 350 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 351 | err_code); |
| 352 | break; |
| 353 | |
| 354 | case PC_to_RDR_GetParameters: |
| 355 | case PC_to_RDR_ResetParameters: |
| 356 | case PC_to_RDR_SetParameters: |
| 357 | /* Return RDR_to_PC_Parameters */ |
| 358 | resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 359 | err_code, NULL); /* FIXME: parameters? */ |
| 360 | break; |
| 361 | |
| 362 | case PC_to_RDR_Escape: |
| 363 | /* Return RDR_to_PC_Escape */ |
| 364 | resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 365 | err_code, NULL, 0); |
| 366 | break; |
| 367 | |
| 368 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 369 | /* Return RDR_to_PC_SlotStatus */ |
| 370 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 371 | err_code); |
| 372 | break; |
| 373 | |
| 374 | default: |
| 375 | /* generate general error */ |
| 376 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 377 | CCID_ERR_CMD_NOT_SUPPORTED); |
| 378 | break; |
| 379 | } |
| 380 | return resp; |
| 381 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 382 | |
| 383 | /*********************************************************************** |
| 384 | * Message reception / parsing |
| 385 | ***********************************************************************/ |
| 386 | |
| 387 | /* Section 6.1.3 */ |
| 388 | static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg) |
| 389 | { |
| 390 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 391 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 392 | uint8_t seq = u->get_slot_status.hdr.bSeq; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 393 | struct msgb *resp; |
| 394 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 395 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 396 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 397 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
| 401 | /* Section 6.1.1 */ |
| 402 | static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg) |
| 403 | { |
| 404 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 405 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 406 | uint8_t seq = u->icc_power_on.hdr.bSeq; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 407 | struct msgb *resp; |
| 408 | |
| 409 | /* TODO: send actual ATR; handle error cases */ |
| 410 | /* TODO: handle this asynchronously */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 411 | resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 412 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 413 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* Section 6.1.2 */ |
| 417 | static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg) |
| 418 | { |
| 419 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 420 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 421 | uint8_t seq = u->icc_power_off.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 422 | struct msgb *resp; |
| 423 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 424 | /* FIXME */ |
| 425 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 426 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Section 6.1.4 */ |
| 430 | static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg) |
| 431 | { |
| 432 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 433 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 434 | uint8_t seq = u->xfr_block.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 435 | struct msgb *resp; |
| 436 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 437 | /* FIXME */ |
| 438 | resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0); |
| 439 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Section 6.1.5 */ |
| 443 | static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 444 | { |
| 445 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 446 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 447 | uint8_t seq = u->get_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 448 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 449 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 450 | /* FIXME: T=1 */ |
| 451 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 452 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* Section 6.1.6 */ |
| 456 | static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 457 | { |
| 458 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 459 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 460 | uint8_t seq = u->reset_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 461 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 462 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 463 | /* FIXME: copy default parameters from somewhere */ |
| 464 | /* FIXME: T=1 */ |
| 465 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 466 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Section 6.1.7 */ |
| 470 | static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 471 | { |
| 472 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 473 | const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 474 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 475 | uint8_t seq = u->set_parameters.hdr.bSeq; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 476 | struct ccid_pars_decoded pars_dec; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 477 | struct msgb *resp; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 478 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 479 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 480 | switch (spar->bProtocolNum) { |
| 481 | case CCID_PROTOCOL_NUM_T0: |
| 482 | rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 483 | if (rc < 0) { |
| 484 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse T0: %d\n", rc); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 485 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 486 | goto out; |
| 487 | } |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 488 | /* FIXME: validate parameters; abort if they are not supported */ |
| 489 | cs->pars = pars_dec; |
| 490 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 491 | break; |
| 492 | case CCID_PROTOCOL_NUM_T1: |
| 493 | rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 494 | if (rc < 0) { |
| 495 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse T1: %d\n", rc); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 496 | resp = ccid_gen_parameters_t1(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 497 | goto out; |
| 498 | } |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 499 | /* FIXME: validate parameters; abort if they are not supported */ |
| 500 | cs->pars = pars_dec; |
| 501 | resp = ccid_gen_parameters_t1(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 502 | break; |
| 503 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 504 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 505 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0); |
| 506 | break; |
| 507 | } |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 508 | out: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 509 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* Section 6.1.8 */ |
| 513 | static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg) |
| 514 | { |
| 515 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 516 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 517 | uint8_t seq = u->escape.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 518 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 519 | |
| 520 | resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0); |
| 521 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* Section 6.1.9 */ |
| 525 | static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg) |
| 526 | { |
| 527 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 528 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 529 | uint8_t seq = u->icc_clock.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 530 | struct msgb *resp; |
| 531 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 532 | /* FIXME: Actually Stop/Start the clock */ |
| 533 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 534 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* Section 6.1.10 */ |
| 538 | static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg) |
| 539 | { |
| 540 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 541 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 542 | uint8_t seq = u->t0apdu.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 543 | struct msgb *resp; |
| 544 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 545 | /* FIXME */ |
| 546 | //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 547 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 548 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* Section 6.1.11 */ |
| 552 | static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg) |
| 553 | { |
| 554 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 555 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 556 | uint8_t seq = u->secure.hdr.bSeq; |
| 557 | struct msgb *resp; |
| 558 | |
| 559 | /* FIXME */ |
| 560 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 561 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* Section 6.1.12 */ |
| 565 | static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg) |
| 566 | { |
| 567 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 568 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 569 | uint8_t seq = u->mechanical.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 570 | struct msgb *resp; |
| 571 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 572 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 573 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* Section 6.1.13 */ |
| 577 | static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg) |
| 578 | { |
| 579 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 580 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 581 | uint8_t seq = u->abort.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 582 | struct msgb *resp; |
| 583 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 584 | /* Check if the currently in-progress message is Abortable */ |
| 585 | switch (0/* FIXME */) { |
| 586 | case PC_to_RDR_IccPowerOn: |
| 587 | case PC_to_RDR_XfrBlock: |
| 588 | case PC_to_RDR_Escape: |
| 589 | case PC_to_RDR_Secure: |
| 590 | case PC_to_RDR_Mechanical: |
| 591 | //case PC_to_RDR_Abort: /* seriously? WTF! */ |
| 592 | break; |
| 593 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 594 | LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 595 | /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */ |
| 596 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 597 | return ccid_slot_send_unbusy(cs, resp); |
| 598 | } |
| 599 | |
| 600 | /* FIXME */ |
| 601 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 602 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* Section 6.1.14 */ |
| 606 | static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg) |
| 607 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 608 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 609 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 610 | uint8_t seq = u->set_rate_and_clock.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 611 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 612 | |
| 613 | /* FIXME */ |
| 614 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, 9600, 2500000); |
| 615 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 618 | /*! Handle data arriving from the host on the OUT endpoint. |
| 619 | * \param[in] cs CCID Instance on which to operate |
Harald Welte | e6ac416 | 2019-05-16 11:18:00 +0200 | [diff] [blame] | 620 | * \param[in] msgb received message buffer containing one CCID OUT EP message from the host. |
| 621 | * Ownership of message buffer is transferred, i.e. it's our job to msgb_free() |
| 622 | * it eventually, after we're done with it (could be asynchronously). |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 623 | * \returns 0 on success; negative on error */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 624 | int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg) |
| 625 | { |
| 626 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
| 627 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 628 | unsigned int len = msgb_length(msg); |
| 629 | struct ccid_slot *cs; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 630 | struct msgb *resp; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 631 | int rc; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 632 | |
| 633 | if (len < sizeof(*ch)) { |
| 634 | /* FIXME */ |
| 635 | return -1; |
| 636 | } |
| 637 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 638 | /* Check for invalid slot number */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 639 | cs = get_ccid_slot(ci, ch->bSlot); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 640 | if (!cs) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 641 | LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 642 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5); |
| 643 | return ccid_send(ci, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 646 | /* Check if slot is already busy; Reject any additional commands meanwhile */ |
| 647 | if (cs->cmd_busy) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 648 | LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 649 | /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */ |
| 650 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq, |
| 651 | CCID_ERR_CMD_SLOT_BUSY); |
| 652 | return ccid_send(ci, resp); |
| 653 | } |
| 654 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 655 | LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n", |
| 656 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 657 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 658 | /* we're now processing a command for the slot; mark slot as busy */ |
| 659 | cs->cmd_busy = true; |
| 660 | |
| 661 | /* TODO: enqueue into the per-slot specific input queue */ |
| 662 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 663 | switch (ch->bMessageType) { |
| 664 | case PC_to_RDR_GetSlotStatus: |
| 665 | if (len != sizeof(u->get_slot_status)) |
| 666 | goto short_msg; |
| 667 | rc = ccid_handle_get_slot_status(cs, msg); |
| 668 | break; |
| 669 | case PC_to_RDR_IccPowerOn: |
| 670 | if (len != sizeof(u->icc_power_on)) |
| 671 | goto short_msg; |
| 672 | rc = ccid_handle_icc_power_on(cs, msg); |
| 673 | break; |
| 674 | case PC_to_RDR_IccPowerOff: |
| 675 | if (len != sizeof(u->icc_power_off)) |
| 676 | goto short_msg; |
| 677 | rc = ccid_handle_icc_power_off(cs, msg); |
| 678 | break; |
| 679 | case PC_to_RDR_XfrBlock: |
| 680 | if (len < sizeof(u->xfr_block)) |
| 681 | goto short_msg; |
| 682 | rc = ccid_handle_xfr_block(cs, msg); |
| 683 | break; |
| 684 | case PC_to_RDR_GetParameters: |
| 685 | if (len != sizeof(u->get_parameters)) |
| 686 | goto short_msg; |
| 687 | rc = ccid_handle_get_parameters(cs, msg); |
| 688 | break; |
| 689 | case PC_to_RDR_ResetParameters: |
| 690 | if (len != sizeof(u->reset_parameters)) |
| 691 | goto short_msg; |
| 692 | rc = ccid_handle_reset_parameters(cs, msg); |
| 693 | break; |
| 694 | case PC_to_RDR_SetParameters: |
| 695 | if (len != sizeof(u->set_parameters)) |
| 696 | goto short_msg; |
| 697 | rc = ccid_handle_set_parameters(cs, msg); |
| 698 | break; |
| 699 | case PC_to_RDR_Escape: |
| 700 | if (len < sizeof(u->escape)) |
| 701 | goto short_msg; |
| 702 | rc = ccid_handle_escape(cs, msg); |
| 703 | break; |
| 704 | case PC_to_RDR_IccClock: |
| 705 | if (len != sizeof(u->icc_clock)) |
| 706 | goto short_msg; |
| 707 | rc = ccid_handle_icc_clock(cs, msg); |
| 708 | break; |
| 709 | case PC_to_RDR_T0APDU: |
| 710 | if (len != /*FIXME*/ sizeof(u->t0apdu)) |
| 711 | goto short_msg; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 712 | rc = ccid_handle_t0apdu(cs, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 713 | break; |
| 714 | case PC_to_RDR_Secure: |
| 715 | if (len < sizeof(u->secure)) |
| 716 | goto short_msg; |
| 717 | rc = ccid_handle_secure(cs, msg); |
| 718 | break; |
| 719 | case PC_to_RDR_Mechanical: |
| 720 | if (len != sizeof(u->mechanical)) |
| 721 | goto short_msg; |
| 722 | rc = ccid_handle_mechanical(cs, msg); |
| 723 | break; |
| 724 | case PC_to_RDR_Abort: |
| 725 | if (len != sizeof(u->abort)) |
| 726 | goto short_msg; |
| 727 | rc = ccid_handle_abort(cs, msg); |
| 728 | break; |
| 729 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 730 | if (len != sizeof(u->set_rate_and_clock)) |
| 731 | goto short_msg; |
| 732 | rc = ccid_handle_set_rate_and_clock(cs, msg); |
| 733 | break; |
| 734 | default: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 735 | /* generic response with bERror = 0 (command not supported) */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 736 | LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 737 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, |
| 738 | CCID_ERR_CMD_NOT_SUPPORTED); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame^] | 739 | msgb_free(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 740 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 741 | } |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame^] | 742 | /* the various ccid_handle_* functions can return '1' to tell us that they took ownership |
| 743 | * of the msgb */ |
| 744 | if (rc != 1) |
| 745 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 746 | return 0; |
| 747 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 748 | short_msg: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 749 | LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg)); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame^] | 750 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 751 | return -1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 752 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 753 | |
| 754 | void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops, const char *name, |
| 755 | void *priv) |
| 756 | { |
| 757 | int i; |
| 758 | |
| 759 | for (i = 0; i < ARRAY_SIZE(ci->slot); i++) { |
| 760 | struct ccid_slot *cs = &ci->slot[i]; |
| 761 | cs->slot_nr = i; |
| 762 | cs->ci = ci; |
| 763 | } |
| 764 | ci->ops= ops; |
| 765 | ci->name = name; |
| 766 | ci->priv = priv; |
| 767 | } |