1 /* packet-cattp.c
2  * Routines for packet dissection of
3  *      ETSI TS 102 127 v6.13.0  (Release 6 / 2009-0r45)
4  *      Card Application Toolkit - Transport Protocol over UDP
5  *
6  * Copyright 2014-2014 by Sebastian Kloeppel <sk [at] nakedape.net>
7  *                        Cristina E. Vintila <cristina.vintila [at] gmail.com>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * SPDX-License-Identifier: GPL-2.0-or-later
14  */
15 #include "config.h"
16 
17 #include <epan/packet.h>
18 #include <epan/prefs.h>
19 #include <epan/expert.h>
20 #include <epan/in_cksum.h>
21 
22 #define CATTP_SHORTNAME "CAT-TP"
23 #define CATTP_HBLEN 18
24 
25 #define F_SYN 0x80
26 #define F_ACK 0x40
27 #define F_EAK 0x20
28 #define F_RST 0x10
29 #define F_NUL 0x08
30 #define F_SEG 0x04
31 
32 /* bit masks for the first header byte. */
33 #define M_FLAGS   0xFC  /* flags only, no version */
34 #define M_PDU_SYN 0xB8  /* SYN (ACK, SEG don't care) without version */
35 #define M_PDU_ACK 0xD0  /* ACK (EAK, SEG, NUL don't care) without version */
36 #define M_PDU_RST 0xBC  /* RST (ACK don't care) without version */
37 #define M_VERSION 0x03  /* only Version */
38 
39 #define ICCID_PREFIX 0x98
40 
41 static int proto_cattp = -1;
42 
43 static gint ett_cattp = -1;
44 static gint ett_cattp_id = -1;
45 static gint ett_cattp_flags = -1;
46 static gint ett_cattp_eaks = -1;
47 
48 static int hf_cattp_flags = -1;
49 
50 /* flag components */
51 static int hf_cattp_flag_syn = -1;
52 static int hf_cattp_flag_ack = -1;
53 static int hf_cattp_flag_eak = -1;
54 static int hf_cattp_flag_rst = -1;
55 static int hf_cattp_flag_nul = -1;
56 static int hf_cattp_flag_seg = -1;
57 static int hf_cattp_version = -1;
58 
59 /* structure of flag components */
60 static int * const cattp_flags[] = {
61     &hf_cattp_flag_syn,
62     &hf_cattp_flag_ack,
63     &hf_cattp_flag_eak,
64     &hf_cattp_flag_rst,
65     &hf_cattp_flag_nul,
66     &hf_cattp_flag_seg,
67     &hf_cattp_version,
68     NULL
69 };
70 
71 static int hf_cattp_hlen = -1;
72 static int hf_cattp_srcport = -1;
73 static int hf_cattp_dstport = -1;
74 static int hf_cattp_datalen = -1;
75 static int hf_cattp_seq = -1;
76 static int hf_cattp_ack = -1;
77 static int hf_cattp_windowsize = -1;
78 static int hf_cattp_checksum = -1;
79 static int hf_cattp_checksum_status = -1;
80 static int hf_cattp_identification = -1;
81 static int hf_cattp_iccid = -1;
82 static int hf_cattp_idlen = -1;
83 static int hf_cattp_maxpdu = -1;
84 static int hf_cattp_maxsdu = -1;
85 static int hf_cattp_rc = -1;
86 static int hf_cattp_eaklen = -1;
87 static int hf_cattp_eaks = -1;
88 
89 static expert_field ei_cattp_checksum = EI_INIT;
90 
91 /* Preference to control whether to check the CATTP checksum */
92 static gboolean cattp_check_checksum = TRUE;
93 
94 /* Reason code mapping */
95 static const value_string cattp_reset_reason[] = {
96     { 0, "Normal Ending" },
97     { 1, "Connection set-up failed, illegal parameters" },
98     { 2, "Temporarily unable to set up this connection" },
99     { 3, "Requested Port not available" },
100     { 4, "Unexpected PDU received" },
101     { 5, "Maximum retries exceeded" },
102     { 6, "Version not supported" },
103     { 7, "RFU" },
104     { 0, NULL }
105 };
106 
107 static const unit_name_string units_pdu = { "PDU", "PDUs" };
108 
109 /* Forward declartion due to use of heuristic dissection preference. */
110 void proto_reg_handoff_cattp(void);
111 void proto_register_cattp(void);
112 
113 /* Dissection of SYN PDUs */
114 static guint32
dissect_cattp_synpdu(tvbuff_t * tvb,packet_info * pinfo,proto_tree * cattp_tree,guint32 offset)115 dissect_cattp_synpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
116 {
117     proto_item *idi, *id_tree;
118     guint8      idlen;
119 
120     proto_tree_add_item(cattp_tree, hf_cattp_maxpdu, tvb, offset, 2, ENC_BIG_ENDIAN);
121     offset += 2;
122 
123     proto_tree_add_item(cattp_tree, hf_cattp_maxsdu, tvb, offset, 2, ENC_BIG_ENDIAN);
124     offset += 2;
125 
126     idlen = tvb_get_guint8(tvb, offset);
127     idi = proto_tree_add_uint(cattp_tree, hf_cattp_idlen, tvb, offset, 1, idlen);
128     offset += 1;
129 
130     col_append_fstr(pinfo->cinfo, COL_INFO, " IdLen=%u ", idlen);
131 
132     id_tree = proto_item_add_subtree(idi, ett_cattp_id);
133 
134     if (idlen > 0) {
135         guint8 first_id_byte;
136 
137         first_id_byte = tvb_get_guint8(tvb, offset);
138         proto_tree_add_item(id_tree, hf_cattp_identification, tvb, offset, idlen, ENC_NA);
139 
140         /* Optional code. Checks whether identification field may be an ICCID.
141          * It has to be considered to move this logic to another layer / dissector.
142          * However it is common to send ICCID as Identification for OTA download. */
143         if (idlen <= 10 && idlen >= 9 && ICCID_PREFIX == first_id_byte) {
144             wmem_strbuf_t *buf;
145             int i;
146 
147             buf = wmem_strbuf_new(pinfo->pool, "");
148 
149             /* switch nibbles */
150             for (i = 0; i < idlen; i++) {
151                 guint8 c, n;
152 
153                 c = tvb_get_guint8(tvb, offset + i);
154                 n = ((c & 0xF0) >> 4) + ((c & 0x0F) << 4);
155                 wmem_strbuf_append_printf(buf, "%02X", n);
156             }
157 
158             proto_tree_add_string(id_tree, hf_cattp_iccid, tvb, offset,
159                                  idlen, wmem_strbuf_get_str(buf));
160         }
161         offset += idlen;
162     }
163     return offset;
164 }
165 
166 /* Dissection of Extended Acknowledgement PDUs */
167 static guint32
dissect_cattp_eakpdu(tvbuff_t * tvb,packet_info * pinfo,proto_tree * cattp_tree,guint32 offset,guint8 hlen)168 dissect_cattp_eakpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset, guint8 hlen)
169 {
170     proto_item *eaki;
171     guint8      eak_count;
172 
173     eak_count = (hlen - offset) >> 1;
174     eaki = proto_tree_add_uint(cattp_tree, hf_cattp_eaklen, tvb, offset, eak_count * 2, eak_count);
175 
176     if (eak_count > 0) {
177         proto_item *eak_tree;
178         int i;
179 
180         col_append_fstr(pinfo->cinfo, COL_INFO, " EAKs=%u", eak_count);
181         eak_tree = proto_item_add_subtree(eaki, ett_cattp_eaks);
182 
183         for (i = 0; i < eak_count; i++) {
184             proto_tree_add_item(eak_tree, hf_cattp_eaks, tvb, offset, 2, ENC_BIG_ENDIAN);
185             offset += 2;
186         }
187     }
188 
189     return offset;
190 }
191 
192 /* Dissection of Extended Acknowledgement PDUs */
193 static guint32
dissect_cattp_rstpdu(tvbuff_t * tvb,packet_info * pinfo,proto_tree * cattp_tree,guint32 offset)194 dissect_cattp_rstpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
195 {
196     guint8       rc;
197     const gchar *rc_str;
198 
199     rc = tvb_get_guint8(tvb, offset); /* reason code of RST */
200     rc_str = val_to_str(rc, cattp_reset_reason, "Unknown reason code: 0x%02x");
201     col_append_fstr(pinfo->cinfo, COL_INFO, " Reason=\"%s\" ", rc_str);
202 
203     proto_tree_add_item(cattp_tree, hf_cattp_rc, tvb, offset, 1, ENC_BIG_ENDIAN);
204     return ++offset;
205 }
206 
207 /* Dissection of the base header */
208 static int
dissect_cattp(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)209 dissect_cattp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
210 {
211     const char *pdutype = "[Unknown PDU]";
212     proto_item *ti, *cattp_tree;
213     guint32     offset;
214     vec_t       cksum_vec[1];
215     int         header_offset;
216     guint       cksum_data_len;
217     guint8      flags, first_byte, hlen, ver;
218     guint16     plen, ackno, seqno, wsize, sport, dport;
219 
220     col_set_str(pinfo->cinfo, COL_PROTOCOL, CATTP_SHORTNAME);
221 
222     /* Clear out stuff in the info column */
223     col_clear(pinfo->cinfo, COL_INFO);
224 
225     hlen = tvb_get_guint8(tvb, 3); /* lookahead header len. */
226 
227     offset = 0;
228     ti = proto_tree_add_protocol_format(tree, proto_cattp, tvb, offset, hlen,
229                                         "Card Application Toolkit Transport Protocol");
230 
231     cattp_tree = proto_item_add_subtree(ti, ett_cattp);
232 
233     /* render flags tree */
234     first_byte = tvb_get_guint8(tvb, offset);
235     flags = first_byte & M_FLAGS; /* discard version from first byte for flags */
236     ver   = first_byte & M_VERSION; /* discard flags for version */
237     proto_tree_add_bitmask(cattp_tree, tvb, offset, hf_cattp_flags, ett_cattp_flags, cattp_flags, ENC_BIG_ENDIAN);
238     offset += 3; /* skip RFU and header len */
239 
240     /* Header length, varies for SYN(identification) and EAKs */
241     proto_tree_add_uint(cattp_tree, hf_cattp_hlen, tvb, offset, 1, hlen);
242     offset += 1;
243 
244     /* Parse cattp source port. */
245     sport = tvb_get_ntohs(tvb, offset);
246     proto_tree_add_uint(cattp_tree, hf_cattp_srcport, tvb, offset, 2, sport);
247     offset += 2;
248 
249     /* Parse cattp destination port. */
250     dport = tvb_get_ntohs(tvb, offset);
251     proto_tree_add_uint(cattp_tree, hf_cattp_dstport, tvb, offset, 2, dport);
252     offset += 2;
253 
254     proto_item_append_text(ti, " (v%u, Src Port: %u, Dst Port: %u)", ver, sport, dport);
255     col_add_fstr(pinfo->cinfo, COL_INFO, "%u > %u ", sport, dport);
256 
257     /* Parse length of payload. */
258     plen = tvb_get_ntohs(tvb, offset);
259     proto_tree_add_uint(cattp_tree, hf_cattp_datalen, tvb, offset, 2, plen);
260     offset += 2;
261 
262     /* Parse sequence number. */
263     seqno = tvb_get_ntohs(tvb, offset);
264     proto_tree_add_uint(cattp_tree, hf_cattp_seq, tvb, offset, 2, seqno);
265     offset += 2;
266 
267     /* Parse acknowledgement number. */
268     ackno = tvb_get_ntohs(tvb, offset);
269     proto_tree_add_uint(cattp_tree, hf_cattp_ack, tvb, offset, 2, ackno);
270     offset += 2;
271 
272     /* Parse window size. */
273     wsize = tvb_get_ntohs(tvb, offset);
274     proto_tree_add_uint(cattp_tree, hf_cattp_windowsize, tvb, offset, 2, wsize);
275     offset += 2;
276 
277     if (flags & F_SYN)
278            pdutype = "[SYN PDU]";
279     else if (flags & F_ACK)
280            pdutype = "[ACK PDU]";
281     else if (flags & F_RST)
282            pdutype = "[RST PDU]";
283 
284     col_append_fstr(pinfo->cinfo, COL_INFO, "%s Flags=0x%02X Ack=%u Seq=%u WSize=%u", pdutype, flags, ackno, seqno, wsize);
285 
286     /* Parse and verify checksum */
287     header_offset  = 0;
288     cksum_data_len = hlen + plen;
289     if (!cattp_check_checksum) {
290         /* We have turned checksum checking off; we do NOT checksum it. */
291         proto_tree_add_checksum(cattp_tree, tvb, offset, hf_cattp_checksum, hf_cattp_checksum_status, &ei_cattp_checksum,
292                                 pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
293     } else {
294         /* We haven't turned checksum checking off; checksum it. */
295 
296         /* Unlike TCP, CATTP does not make use of a pseudo-header for checksum */
297         SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, header_offset, cksum_data_len);
298         proto_tree_add_checksum(cattp_tree, tvb, offset, hf_cattp_checksum, hf_cattp_checksum_status, &ei_cattp_checksum,
299                                 pinfo, in_cksum(cksum_vec, 1), ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_IN_CKSUM);
300     } /* End of checksum code */
301     offset += 2;
302 
303     if (flags & F_SYN)
304         offset = dissect_cattp_synpdu(tvb, pinfo, cattp_tree, offset);
305     else if (flags & F_EAK)
306         offset = dissect_cattp_eakpdu(tvb, pinfo, cattp_tree, offset, hlen);
307     else if (flags & F_RST)
308         offset = dissect_cattp_rstpdu(tvb, pinfo, cattp_tree, offset);
309     /* for other PDU types nothing special to be displayed in detail tree. */
310 
311     if (plen > 0) { /* Call generic data handle if data exists. */
312        col_append_fstr(pinfo->cinfo, COL_INFO, " DataLen=%u", plen);
313        tvb = tvb_new_subset_remaining(tvb, offset);
314        call_data_dissector(tvb, pinfo, tree);
315     }
316     return tvb_captured_length(tvb);
317 }
318 
319 /* The heuristic dissector function checks if the UDP packet may be a cattp packet */
320 static gboolean
dissect_cattp_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)321 dissect_cattp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
322 {
323     if (tvb_captured_length(tvb) >= CATTP_HBLEN) { /* check of data is big enough for base header. */
324         guint8  flags, ver, hlen;
325         guint16 plen;
326 
327         hlen = tvb_get_guint8(tvb, 3); /* header len  */
328         plen = tvb_get_ntohs(tvb, 8);  /* payload len */
329 
330         if (hlen+plen != tvb_reported_length(tvb)) /* check if data length is ok. */
331             return FALSE;
332 
333         /* ETSI TS 102 127 V15.0.0 and earlier releases say explicitly that
334            the version bits must be 0. */
335         ver = tvb_get_guint8(tvb, 0) & M_VERSION;
336         if (ver != 0)
337             return FALSE;
338 
339         flags = tvb_get_guint8(tvb, 0) & M_FLAGS;
340         if ( (flags & M_PDU_SYN) == F_SYN ||
341              (flags & M_PDU_RST) == F_RST ||
342              (flags & M_PDU_ACK) == F_ACK ) { /* check if flag combi is valid */
343             dissect_cattp(tvb, pinfo, tree, data);
344             return TRUE;
345         }
346     }
347     return FALSE;
348 }
349 
350 /* Function to register the dissector, called by infrastructure. */
351 void
proto_register_cattp(void)352 proto_register_cattp(void)
353 {
354     static hf_register_info hf[] = {
355         {
356             &hf_cattp_flags,
357             {
358                 "Flags", "cattp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
359                 NULL, HFILL
360             }
361         },
362         {
363             &hf_cattp_flag_syn,
364             {
365                 "Synchronize Flag", "cattp.flags.syn", FT_UINT8, BASE_DEC, NULL, F_SYN,
366                 NULL, HFILL
367             }
368         },
369         {
370             &hf_cattp_flag_ack,
371             {
372                 "Acknowledge Flag", "cattp.flags.ack", FT_UINT8, BASE_DEC, NULL, F_ACK,
373                 NULL, HFILL
374             }
375         },
376         {
377             &hf_cattp_flag_eak,
378             {
379                 "Extended Acknowledge Flag", "cattp.flags.eak", FT_UINT8, BASE_DEC, NULL, F_EAK,
380                 NULL, HFILL
381             }
382         },
383         {
384             &hf_cattp_flag_rst,
385             {
386                 "Reset Flag", "cattp.flags.rst", FT_UINT8, BASE_DEC, NULL, F_RST,
387                 NULL, HFILL
388             }
389         },
390         {
391             &hf_cattp_flag_nul,
392             {
393                 "NULL Flag", "cattp.flags.nul", FT_UINT8, BASE_DEC, NULL, F_NUL,
394                 NULL, HFILL
395             }
396         },
397         {
398             &hf_cattp_flag_seg,
399             {
400                 "Segmentation Flag", "cattp.flags.seg", FT_UINT8, BASE_DEC, NULL, F_SEG,
401                 NULL, HFILL
402             }
403         },
404         {
405             &hf_cattp_version,
406             {
407                 "Version", "cattp.version", FT_UINT8, BASE_HEX, NULL, M_VERSION,
408                 NULL, HFILL
409             }
410         },
411         {
412             &hf_cattp_hlen,
413             {
414                 "Header Length", "cattp.hlen", FT_UINT8, BASE_DEC, NULL, 0x0,
415                 NULL, HFILL
416             }
417         },
418         {
419             &hf_cattp_srcport,
420             {
421                 "Source Port", "cattp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
422                 NULL, HFILL
423             }
424         },
425         {
426             &hf_cattp_dstport,
427             {
428                 "Destination Port", "cattp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
429                 NULL, HFILL
430             }
431         },
432         {
433             &hf_cattp_datalen,
434             {
435                 "Data Length", "cattp.datalen", FT_UINT16, BASE_DEC, NULL, 0x0,
436                 NULL, HFILL
437             }
438         },
439         {
440             &hf_cattp_seq,
441             {
442                 "Sequence Number", "cattp.seq", FT_UINT16, BASE_DEC, NULL, 0x0,
443                 NULL, HFILL
444             }
445         },
446         {
447             &hf_cattp_ack,
448             {
449                 "Acknowledgement Number", "cattp.ack", FT_UINT16, BASE_DEC, NULL, 0x0,
450                 NULL, HFILL
451             }
452         },
453         {
454             &hf_cattp_windowsize,
455             {
456                 "Window Size", "cattp.windowsize", FT_UINT16, BASE_DEC, NULL, 0x0,
457                 NULL, HFILL
458             }
459         },
460         {
461             &hf_cattp_checksum,
462             {
463                 "Checksum", "cattp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
464                 NULL, HFILL
465             }
466         },
467         {
468             &hf_cattp_checksum_status,
469             {
470                 "Checksum Status", "cattp.checksum.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
471                 NULL, HFILL
472             }
473         },
474         {
475             &hf_cattp_identification,
476             {
477                 "Identification", "cattp.identification", FT_BYTES, BASE_NONE, NULL, 0x0,
478                 NULL, HFILL
479             }
480         },
481         {
482             &hf_cattp_iccid,
483             {
484                 "ICCID", "cattp.iccid", FT_STRING, BASE_NONE, NULL, 0x0,
485                 NULL, HFILL
486             }
487         },
488         {
489             &hf_cattp_maxpdu,
490             {
491                 "Maxpdu", "cattp.maxpdu", FT_UINT16, BASE_DEC, NULL, 0x0,
492                 NULL, HFILL
493             }
494         },
495         {
496             &hf_cattp_maxsdu,
497             {
498                 "Maxsdu", "cattp.maxsdu", FT_UINT16, BASE_DEC, NULL, 0x0,
499                 NULL, HFILL
500             }
501         },
502         {
503             &hf_cattp_rc,
504             {
505                 "Reason Code", "cattp.rc", FT_UINT8, BASE_DEC, VALS(cattp_reset_reason), 0x0,
506                 NULL, HFILL
507             }
508         },
509         {
510             &hf_cattp_idlen,
511             {
512                 "Identification Length", "cattp.idlen", FT_UINT8, BASE_DEC, NULL, 0x0,
513                 NULL, HFILL
514             }
515         },
516         {
517             &hf_cattp_eaks,
518             {
519                 "Acknowledgement Number", "cattp.eak", FT_UINT16, BASE_DEC, NULL, 0x0,
520                 NULL, HFILL
521             }
522         },
523         {
524             &hf_cattp_eaklen,
525             {
526                 "Extended Acknowledgement Numbers", "cattp.eaks", FT_UINT16, BASE_DEC|BASE_UNIT_STRING, &units_pdu, 0x0,
527                 NULL, HFILL
528             }
529         }
530     };
531 
532     /* Setup protocol subtree array */
533     static gint *ett[] = {
534         &ett_cattp,
535         &ett_cattp_flags,
536         &ett_cattp_id,
537         &ett_cattp_eaks
538     };
539 
540     static ei_register_info ei[] = {
541         { &ei_cattp_checksum, { "cattp.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
542     };
543 
544     module_t *cattp_module;
545     expert_module_t* expert_cattp;
546 
547     proto_cattp = proto_register_protocol (
548                       "ETSI Card Application Toolkit Transport Protocol",    /* name */
549                       CATTP_SHORTNAME, /* short name */
550                       "cattp"          /* abbrev */
551                   );
552 
553     proto_register_field_array(proto_cattp, hf, array_length(hf));
554     proto_register_subtree_array(ett, array_length(ett));
555     expert_cattp = expert_register_protocol(proto_cattp);
556     expert_register_field_array(expert_cattp, ei, array_length(ei));
557 
558     cattp_module = prefs_register_protocol(proto_cattp, NULL);
559     prefs_register_bool_preference(cattp_module, "checksum",
560                                    "Validate checksum of all messages",
561                                    "Whether the checksum of all messages should be validated or not",
562                                    &cattp_check_checksum);
563 
564     prefs_register_obsolete_preference(cattp_module, "enable");
565 }
566 
567 /* Handoff */
568 void
proto_reg_handoff_cattp(void)569 proto_reg_handoff_cattp(void)
570 {
571     dissector_handle_t cattp_handle;
572 
573     /* Create dissector handle */
574     cattp_handle = create_dissector_handle(dissect_cattp, proto_cattp);
575 
576     heur_dissector_add("udp", dissect_cattp_heur, "CAT-TP over UDP", "cattp_udp", proto_cattp, HEURISTIC_DISABLE);
577     dissector_add_for_decode_as_with_preference("udp.port", cattp_handle);
578 }
579 
580 /*
581  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
582  *
583  * Local variables:
584  * c-basic-offset: 4
585  * tab-width: 8
586  * indent-tabs-mode: nil
587  * End:
588  *
589  * vi: set shiftwidth=4 tabstop=8 expandtab:
590  * :indentSize=4:tabSize=8:noTabs=true:
591  */
592