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
6 * Copyright 2014-2014 by Sebastian Kloeppel <sk [at] nakedape.net>
7 * Cristina E. Vintila <cristina.vintila [at] gmail.com>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/packet.h>
30 #include <epan/prefs.h>
31 #include <epan/in_cksum.h>
33 #define CATTP_SHORTNAME "CAT-TP"
34 #define CATTP_HBLEN 18
43 /* bit masks for the first header byte. */
44 #define M_FLAGS 0xFC /* flags only, no version */
45 #define M_PDU_SYN 0xB8 /* SYN (ACK, SEG dont care) without version */
46 #define M_PDU_ACK 0xD0 /* ACK (EAK, SEG, NUL dont care) without version */
47 #define M_PDU_RST 0xBC /* RST (ACK dont care) without version */
48 #define M_VERSION 0x03 /* only Version */
50 #define ICCID_PREFIX 0x98
52 static int proto_cattp = -1;
54 static gint ett_cattp = -1;
55 static gint ett_cattp_id = -1;
56 static gint ett_cattp_flags = -1;
57 static gint ett_cattp_eaks = -1;
59 static int hf_cattp_flags = -1;
62 static int hf_cattp_flag_syn = -1;
63 static int hf_cattp_flag_ack = -1;
64 static int hf_cattp_flag_eak = -1;
65 static int hf_cattp_flag_rst = -1;
66 static int hf_cattp_flag_nul = -1;
67 static int hf_cattp_flag_seg = -1;
68 static int hf_cattp_version = -1;
70 /* structure of flag components */
71 static const int *cattp_flags[] = {
82 static int hf_cattp_hlen = -1;
83 static int hf_cattp_srcport = -1;
84 static int hf_cattp_dstport = -1;
85 static int hf_cattp_datalen = -1;
86 static int hf_cattp_seq = -1;
87 static int hf_cattp_ack = -1;
88 static int hf_cattp_windowsize = -1;
89 static int hf_cattp_checksum = -1;
90 static int hf_cattp_identification = -1;
91 static int hf_cattp_iccid = -1;
92 static int hf_cattp_idlen = -1;
93 static int hf_cattp_maxpdu = -1;
94 static int hf_cattp_maxsdu = -1;
95 static int hf_cattp_rc = -1;
96 static int hf_cattp_eaklen = -1;
97 static int hf_cattp_eaks = -1;
99 static dissector_handle_t data_handle;
101 /* Preference to control whether to check the CATTP checksum */
102 static gboolean cattp_check_checksum = TRUE;
104 /* Preference to control whether to heuristically detect CATTP packets */
105 static gboolean cattp_enable_heuristic_dissection = TRUE;
107 /* Reason code mapping */
108 static const value_string cattp_reset_reason[] = {
109 { 0, "Normal Ending" },
110 { 1, "Connection set-up failed, illegal parameters" },
111 { 2, "Temporarily unable to set up this connection" },
112 { 3, "Requested Port not available" },
113 { 4, "Unexpected PDU received" },
114 { 5, "Maximum retries exceeded" },
115 { 6, "Version not supported" },
120 /* Forward declartion due to use of heuristic dissection preference. */
121 void proto_reg_handoff_cattp(void);
122 void proto_register_cattp(void);
124 /* Dissection of SYN PDUs */
126 dissect_cattp_synpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
128 proto_item *idi, *id_tree;
131 proto_tree_add_item(cattp_tree, hf_cattp_maxpdu, tvb, offset, 2, ENC_BIG_ENDIAN);
134 proto_tree_add_item(cattp_tree, hf_cattp_maxsdu, tvb, offset, 2, ENC_BIG_ENDIAN);
137 idlen = tvb_get_guint8(tvb, offset);
138 idi = proto_tree_add_uint(cattp_tree, hf_cattp_idlen, tvb, offset, 1, idlen);
141 col_append_fstr(pinfo->cinfo, COL_INFO, " IdLen=%u ", idlen);
143 id_tree = proto_item_add_subtree(idi, ett_cattp_id);
146 guint8 first_id_byte;
148 first_id_byte = tvb_get_guint8(tvb, offset);
149 proto_tree_add_item(id_tree, hf_cattp_identification, tvb, offset, idlen, ENC_NA);
151 /* Optional code. Checks whether identification field may be an ICCID.
152 * It has to be considered to move this logic to another layer / dissector.
153 * However it is common to send ICCID as Identification for OTA download. */
154 if (idlen <= 10 && idlen >= 9 && ICCID_PREFIX == first_id_byte) {
158 buf = wmem_strbuf_new(wmem_packet_scope(), "");
161 for (i = 0; i < idlen; i++) {
164 c = tvb_get_guint8(tvb, offset + i);
165 n = ((c & 0xF0) >> 4) + ((c & 0x0F) << 4);
166 wmem_strbuf_append_printf(buf, "%02X", n);
169 proto_tree_add_string(id_tree, hf_cattp_iccid, tvb, offset,
170 idlen, wmem_strbuf_get_str(buf));
177 /* Dissection of Extended Acknowledgement PDUs */
179 dissect_cattp_eakpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset, guint8 hlen)
184 eak_count = (hlen - offset) >> 1;
185 eaki = proto_tree_add_uint_format_value(cattp_tree, hf_cattp_eaklen, tvb, offset, eak_count * 2,
186 eak_count, "%u PDUs", eak_count);
189 proto_item *eak_tree;
192 col_append_fstr(pinfo->cinfo, COL_INFO, " EAKs=%u", eak_count);
193 eak_tree = proto_item_add_subtree(eaki, ett_cattp_eaks);
195 for (i = 0; i < eak_count; i++) {
196 proto_tree_add_item(eak_tree, hf_cattp_eaks, tvb, offset, 2, ENC_BIG_ENDIAN);
204 /* Dissection of Extended Acknowledgement PDUs */
206 dissect_cattp_rstpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
211 rc = tvb_get_guint8(tvb, offset); /* reason code of RST */
212 rc_str = val_to_str(rc, cattp_reset_reason, "Unknown reason code: 0x%02x");
213 col_append_fstr(pinfo->cinfo, COL_INFO, " Reason=\"%s\" ", rc_str);
215 proto_tree_add_item(cattp_tree, hf_cattp_rc, tvb, offset, 1, ENC_BIG_ENDIAN);
219 /* Dissection of the base header */
221 dissect_cattp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
223 const char *pdutype = "[Unknown PDU]";
224 proto_item *ti, *cattp_tree;
226 gushort computed_chksum;
229 guint cksum_data_len;
230 guint8 flags, first_byte, hlen, ver;
231 guint16 plen, chksum, ackno, seqno, wsize, sport, dport;
233 col_set_str(pinfo->cinfo, COL_PROTOCOL, CATTP_SHORTNAME);
235 /* Clear out stuff in the info column */
236 col_clear(pinfo->cinfo, COL_INFO);
238 hlen = tvb_get_guint8(tvb, 3); /* lookahead header len. */
241 ti = proto_tree_add_protocol_format(tree, proto_cattp, tvb, offset, hlen,
242 "Card Application Toolkit Transport Protocol");
244 cattp_tree = proto_item_add_subtree(ti, ett_cattp);
246 /* render flags tree */
247 first_byte = tvb_get_guint8(tvb, offset);
248 flags = first_byte & M_FLAGS; /* discard version from first byte for flags */
249 ver = first_byte & M_VERSION; /* discard flags for version */
250 proto_tree_add_bitmask(cattp_tree, tvb, offset, hf_cattp_flags, ett_cattp_flags, cattp_flags, ENC_BIG_ENDIAN);
251 offset += 3; /* skip RFU and header len */
253 /* Header length, varies for SYN(identification) and EAKs */
254 proto_tree_add_uint(cattp_tree, hf_cattp_hlen, tvb, offset, 1, hlen);
257 /* Parse cattp source port. */
258 sport = tvb_get_ntohs(tvb, offset);
259 proto_tree_add_uint(cattp_tree, hf_cattp_srcport, tvb, offset, 2, sport);
262 /* Parse cattp destination port. */
263 dport = tvb_get_ntohs(tvb, offset);
264 proto_tree_add_uint(cattp_tree, hf_cattp_dstport, tvb, offset, 2, dport);
267 proto_item_append_text(ti, " (v%u, Src Port: %u, Dst Port: %u)", ver, sport, dport);
268 col_add_fstr(pinfo->cinfo, COL_INFO, "%u > %u ", sport, dport);
270 /* Parse length of payload. */
271 plen = tvb_get_ntohs(tvb, offset);
272 proto_tree_add_uint(cattp_tree, hf_cattp_datalen, tvb, offset, 2, plen);
275 /* Parse sequence number. */
276 seqno = tvb_get_ntohs(tvb, offset);
277 proto_tree_add_uint(cattp_tree, hf_cattp_seq, tvb, offset, 2, seqno);
280 /* Parse acknowledgement number. */
281 ackno = tvb_get_ntohs(tvb, offset);
282 proto_tree_add_uint(cattp_tree, hf_cattp_ack, tvb, offset, 2, ackno);
285 /* Parse window size. */
286 wsize = tvb_get_ntohs(tvb, offset);
287 proto_tree_add_uint(cattp_tree, hf_cattp_windowsize, tvb, offset, 2, wsize);
291 pdutype = "[SYN PDU]";
292 else if (flags & F_ACK)
293 pdutype = "[ACK PDU]";
294 else if (flags & F_RST)
295 pdutype = "[RST PDU]";
297 col_append_fstr(pinfo->cinfo, COL_INFO, "%s Flags=0x%02X Ack=%u Seq=%u WSize=%u", pdutype, flags, ackno, seqno, wsize);
299 /* Parse and verify checksum */
301 cksum_data_len = hlen + plen;
302 chksum = tvb_get_ntohs(tvb, offset);
303 if (!cattp_check_checksum) {
304 /* We have turned checksum checking off; we do NOT checksum it. */
305 proto_tree_add_uint_format_value(cattp_tree, hf_cattp_checksum, tvb, offset, 2,
306 chksum, "0x%X [validation disabled]", chksum);
308 /* We haven't turned checksum checking off; checksum it. */
310 /* Unlike TCP, CATTP does not make use of a pseudo-header for checksum */
311 SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, header_offset, cksum_data_len);
312 computed_chksum = in_cksum(cksum_vec, 1);
314 if (computed_chksum == 0) {
315 /* Checksum is valid */
316 proto_tree_add_uint_format_value(cattp_tree, hf_cattp_checksum, tvb, offset, 2,
317 chksum, "0x%X [validated]", chksum);
319 /* Checksum is invalid. Let's compute the expected checksum, based on the data we have */
320 proto_tree_add_uint_format_value(cattp_tree, hf_cattp_checksum, tvb, offset, 2, chksum,
321 "0x%X [incorrect, correct: 0x%X]", chksum,
322 in_cksum_shouldbe(chksum, computed_chksum));
324 } /* End of checksum code */
328 offset = dissect_cattp_synpdu(tvb, pinfo, cattp_tree, offset);
329 else if (flags & F_EAK)
330 offset = dissect_cattp_eakpdu(tvb, pinfo, cattp_tree, offset, hlen);
331 else if (flags & F_RST)
332 offset = dissect_cattp_rstpdu(tvb, pinfo, cattp_tree, offset);
333 /* for other PDU types nothing special to be displayed in detail tree. */
335 if (plen > 0) { /* Call generic data handle if data exists. */
336 col_append_fstr(pinfo->cinfo, COL_INFO, " DataLen=%u", plen);
337 tvb = tvb_new_subset_remaining(tvb, offset);
338 call_dissector(data_handle, tvb, pinfo, tree);
342 /* The heuristic dissector function checks if the UDP packet may be a cattp packet */
344 dissect_cattp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
346 if (tvb_captured_length(tvb) >= CATTP_HBLEN) { /* check of data is big enough for base header. */
350 hlen = tvb_get_guint8(tvb, 3); /* header len */
351 plen = tvb_get_ntohs(tvb, 8); /* payload len */
353 if (hlen+plen != tvb_reported_length(tvb)) /* check if data length is ok. */
356 flags = tvb_get_guint8(tvb, 0) & M_FLAGS;
357 if ( (flags & M_PDU_SYN) == F_SYN ||
358 (flags & M_PDU_RST) == F_RST ||
359 (flags & M_PDU_ACK) == F_ACK ) { /* check if flag combi is valid */
360 dissect_cattp(tvb, pinfo, tree);
367 /* Function to register the dissector, called by infrastructure. */
369 proto_register_cattp(void)
371 static hf_register_info hf[] = {
375 "Flags", "cattp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
382 "Synchronize Flag", "cattp.flags.syn", FT_UINT8, BASE_DEC, NULL, F_SYN,
389 "Acknowledge Flag", "cattp.flags.ack", FT_UINT8, BASE_DEC, NULL, F_ACK,
396 "Extended Acknowledge Flag", "cattp.flags.eak", FT_UINT8, BASE_DEC, NULL, F_EAK,
403 "Reset Flag", "cattp.flags.rst", FT_UINT8, BASE_DEC, NULL, F_RST,
410 "NULL Flag", "cattp.flags.nul", FT_UINT8, BASE_DEC, NULL, F_NUL,
417 "Segmentation Flag", "cattp.flags.seg", FT_UINT8, BASE_DEC, NULL, F_SEG,
424 "Version", "cattp.version", FT_UINT8, BASE_HEX, NULL, M_VERSION,
431 "Header Length", "cattp.hlen", FT_UINT8, BASE_DEC, NULL, 0x0,
438 "Source Port", "cattp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
445 "Destination Port", "cattp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
452 "Data Length", "cattp.datalen", FT_UINT16, BASE_DEC, NULL, 0x0,
459 "Sequence Number", "cattp.seq", FT_UINT16, BASE_DEC, NULL, 0x0,
466 "Acknowledgement Number", "cattp.ack", FT_UINT16, BASE_DEC, NULL, 0x0,
471 &hf_cattp_windowsize,
473 "Window Size", "cattp.windowsize", FT_UINT16, BASE_DEC, NULL, 0x0,
480 "Checksum", "cattp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
485 &hf_cattp_identification,
487 "Identification", "cattp.identification", FT_BYTES, BASE_NONE, NULL, 0x0,
494 "ICCID", "cattp.iccid", FT_STRING, BASE_NONE, NULL, 0x0,
501 "Maxpdu", "cattp.maxpdu", FT_UINT16, BASE_DEC, NULL, 0x0,
508 "Maxsdu", "cattp.maxsdu", FT_UINT16, BASE_DEC, NULL, 0x0,
515 "Reason Code", "cattp.rc", FT_UINT8, BASE_DEC, VALS(cattp_reset_reason), 0x0,
522 "Identification Length", "cattp.idlen", FT_UINT8, BASE_DEC, NULL, 0x0,
529 "Acknowledgement Number", "cattp.eak", FT_UINT16, BASE_DEC, NULL, 0x0,
536 "Extended Acknowledgement Numbers", "cattp.eaks", FT_UINT16, BASE_DEC, NULL, 0x0,
542 /* Setup protocol subtree array */
543 static gint *ett[] = {
550 module_t *cattp_module;
552 proto_cattp = proto_register_protocol (
553 "ETSI Card Application Toolkit Transport Protocol", /* name */
554 CATTP_SHORTNAME, /* short name */
558 proto_register_field_array(proto_cattp, hf, array_length(hf));
559 proto_register_subtree_array(ett, array_length(ett));
561 cattp_module = prefs_register_protocol(proto_cattp, proto_reg_handoff_cattp);
562 prefs_register_bool_preference(cattp_module, "checksum",
563 "Validate checksum of all messages",
564 "Whether the checksum of all messages should be validated or not",
565 &cattp_check_checksum);
567 prefs_register_bool_preference(cattp_module, "enable",
568 "Enable " CATTP_SHORTNAME " heuristic dissection",
569 "Enable " CATTP_SHORTNAME " heuristic dissection (default is enabled)",
570 &cattp_enable_heuristic_dissection);
575 proto_reg_handoff_cattp(void)
577 static gboolean initialized = FALSE;
580 dissector_handle_t cattp_handle;
582 /* Create dissector handle */
583 cattp_handle = create_dissector_handle(dissect_cattp, proto_cattp);
585 /* find data handle */
586 data_handle = find_dissector("data");
588 heur_dissector_add("udp", dissect_cattp_heur, "CAT-TP over UDP", "cattp_udp", proto_cattp);
589 dissector_add_for_decode_as("udp.port", cattp_handle);
593 heur_dissector_set_enabled("udp", dissect_cattp_heur, proto_cattp,
594 cattp_enable_heuristic_dissection);
598 * Editor modelines - https://www.wireshark.org/tools/modelines.html
603 * indent-tabs-mode: nil
606 * vi: set shiftwidth=4 tabstop=8 expandtab:
607 * :indentSize=4:tabSize=8:noTabs=true: