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/expert.h>
32 #include <epan/in_cksum.h>
34 #define CATTP_SHORTNAME "CAT-TP"
35 #define CATTP_HBLEN 18
44 /* bit masks for the first header byte. */
45 #define M_FLAGS 0xFC /* flags only, no version */
46 #define M_PDU_SYN 0xB8 /* SYN (ACK, SEG dont care) without version */
47 #define M_PDU_ACK 0xD0 /* ACK (EAK, SEG, NUL dont care) without version */
48 #define M_PDU_RST 0xBC /* RST (ACK dont care) without version */
49 #define M_VERSION 0x03 /* only Version */
51 #define ICCID_PREFIX 0x98
53 static int proto_cattp = -1;
55 static gint ett_cattp = -1;
56 static gint ett_cattp_id = -1;
57 static gint ett_cattp_flags = -1;
58 static gint ett_cattp_eaks = -1;
60 static int hf_cattp_flags = -1;
63 static int hf_cattp_flag_syn = -1;
64 static int hf_cattp_flag_ack = -1;
65 static int hf_cattp_flag_eak = -1;
66 static int hf_cattp_flag_rst = -1;
67 static int hf_cattp_flag_nul = -1;
68 static int hf_cattp_flag_seg = -1;
69 static int hf_cattp_version = -1;
71 /* structure of flag components */
72 static const int *cattp_flags[] = {
83 static int hf_cattp_hlen = -1;
84 static int hf_cattp_srcport = -1;
85 static int hf_cattp_dstport = -1;
86 static int hf_cattp_datalen = -1;
87 static int hf_cattp_seq = -1;
88 static int hf_cattp_ack = -1;
89 static int hf_cattp_windowsize = -1;
90 static int hf_cattp_checksum = -1;
91 static int hf_cattp_checksum_status = -1;
92 static int hf_cattp_identification = -1;
93 static int hf_cattp_iccid = -1;
94 static int hf_cattp_idlen = -1;
95 static int hf_cattp_maxpdu = -1;
96 static int hf_cattp_maxsdu = -1;
97 static int hf_cattp_rc = -1;
98 static int hf_cattp_eaklen = -1;
99 static int hf_cattp_eaks = -1;
101 static expert_field ei_cattp_checksum = EI_INIT;
103 /* Preference to control whether to check the CATTP checksum */
104 static gboolean cattp_check_checksum = TRUE;
106 /* Reason code mapping */
107 static const value_string cattp_reset_reason[] = {
108 { 0, "Normal Ending" },
109 { 1, "Connection set-up failed, illegal parameters" },
110 { 2, "Temporarily unable to set up this connection" },
111 { 3, "Requested Port not available" },
112 { 4, "Unexpected PDU received" },
113 { 5, "Maximum retries exceeded" },
114 { 6, "Version not supported" },
119 /* Forward declartion due to use of heuristic dissection preference. */
120 void proto_reg_handoff_cattp(void);
121 void proto_register_cattp(void);
123 /* Dissection of SYN PDUs */
125 dissect_cattp_synpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
127 proto_item *idi, *id_tree;
130 proto_tree_add_item(cattp_tree, hf_cattp_maxpdu, tvb, offset, 2, ENC_BIG_ENDIAN);
133 proto_tree_add_item(cattp_tree, hf_cattp_maxsdu, tvb, offset, 2, ENC_BIG_ENDIAN);
136 idlen = tvb_get_guint8(tvb, offset);
137 idi = proto_tree_add_uint(cattp_tree, hf_cattp_idlen, tvb, offset, 1, idlen);
140 col_append_fstr(pinfo->cinfo, COL_INFO, " IdLen=%u ", idlen);
142 id_tree = proto_item_add_subtree(idi, ett_cattp_id);
145 guint8 first_id_byte;
147 first_id_byte = tvb_get_guint8(tvb, offset);
148 proto_tree_add_item(id_tree, hf_cattp_identification, tvb, offset, idlen, ENC_NA);
150 /* Optional code. Checks whether identification field may be an ICCID.
151 * It has to be considered to move this logic to another layer / dissector.
152 * However it is common to send ICCID as Identification for OTA download. */
153 if (idlen <= 10 && idlen >= 9 && ICCID_PREFIX == first_id_byte) {
157 buf = wmem_strbuf_new(wmem_packet_scope(), "");
160 for (i = 0; i < idlen; i++) {
163 c = tvb_get_guint8(tvb, offset + i);
164 n = ((c & 0xF0) >> 4) + ((c & 0x0F) << 4);
165 wmem_strbuf_append_printf(buf, "%02X", n);
168 proto_tree_add_string(id_tree, hf_cattp_iccid, tvb, offset,
169 idlen, wmem_strbuf_get_str(buf));
176 /* Dissection of Extended Acknowledgement PDUs */
178 dissect_cattp_eakpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset, guint8 hlen)
183 eak_count = (hlen - offset) >> 1;
184 eaki = proto_tree_add_uint_format_value(cattp_tree, hf_cattp_eaklen, tvb, offset, eak_count * 2,
185 eak_count, "%u PDUs", eak_count);
188 proto_item *eak_tree;
191 col_append_fstr(pinfo->cinfo, COL_INFO, " EAKs=%u", eak_count);
192 eak_tree = proto_item_add_subtree(eaki, ett_cattp_eaks);
194 for (i = 0; i < eak_count; i++) {
195 proto_tree_add_item(eak_tree, hf_cattp_eaks, tvb, offset, 2, ENC_BIG_ENDIAN);
203 /* Dissection of Extended Acknowledgement PDUs */
205 dissect_cattp_rstpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
210 rc = tvb_get_guint8(tvb, offset); /* reason code of RST */
211 rc_str = val_to_str(rc, cattp_reset_reason, "Unknown reason code: 0x%02x");
212 col_append_fstr(pinfo->cinfo, COL_INFO, " Reason=\"%s\" ", rc_str);
214 proto_tree_add_item(cattp_tree, hf_cattp_rc, tvb, offset, 1, ENC_BIG_ENDIAN);
218 /* Dissection of the base header */
220 dissect_cattp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
222 const char *pdutype = "[Unknown PDU]";
223 proto_item *ti, *cattp_tree;
227 guint cksum_data_len;
228 guint8 flags, first_byte, hlen, ver;
229 guint16 plen, ackno, seqno, wsize, sport, dport;
231 col_set_str(pinfo->cinfo, COL_PROTOCOL, CATTP_SHORTNAME);
233 /* Clear out stuff in the info column */
234 col_clear(pinfo->cinfo, COL_INFO);
236 hlen = tvb_get_guint8(tvb, 3); /* lookahead header len. */
239 ti = proto_tree_add_protocol_format(tree, proto_cattp, tvb, offset, hlen,
240 "Card Application Toolkit Transport Protocol");
242 cattp_tree = proto_item_add_subtree(ti, ett_cattp);
244 /* render flags tree */
245 first_byte = tvb_get_guint8(tvb, offset);
246 flags = first_byte & M_FLAGS; /* discard version from first byte for flags */
247 ver = first_byte & M_VERSION; /* discard flags for version */
248 proto_tree_add_bitmask(cattp_tree, tvb, offset, hf_cattp_flags, ett_cattp_flags, cattp_flags, ENC_BIG_ENDIAN);
249 offset += 3; /* skip RFU and header len */
251 /* Header length, varies for SYN(identification) and EAKs */
252 proto_tree_add_uint(cattp_tree, hf_cattp_hlen, tvb, offset, 1, hlen);
255 /* Parse cattp source port. */
256 sport = tvb_get_ntohs(tvb, offset);
257 proto_tree_add_uint(cattp_tree, hf_cattp_srcport, tvb, offset, 2, sport);
260 /* Parse cattp destination port. */
261 dport = tvb_get_ntohs(tvb, offset);
262 proto_tree_add_uint(cattp_tree, hf_cattp_dstport, tvb, offset, 2, dport);
265 proto_item_append_text(ti, " (v%u, Src Port: %u, Dst Port: %u)", ver, sport, dport);
266 col_add_fstr(pinfo->cinfo, COL_INFO, "%u > %u ", sport, dport);
268 /* Parse length of payload. */
269 plen = tvb_get_ntohs(tvb, offset);
270 proto_tree_add_uint(cattp_tree, hf_cattp_datalen, tvb, offset, 2, plen);
273 /* Parse sequence number. */
274 seqno = tvb_get_ntohs(tvb, offset);
275 proto_tree_add_uint(cattp_tree, hf_cattp_seq, tvb, offset, 2, seqno);
278 /* Parse acknowledgement number. */
279 ackno = tvb_get_ntohs(tvb, offset);
280 proto_tree_add_uint(cattp_tree, hf_cattp_ack, tvb, offset, 2, ackno);
283 /* Parse window size. */
284 wsize = tvb_get_ntohs(tvb, offset);
285 proto_tree_add_uint(cattp_tree, hf_cattp_windowsize, tvb, offset, 2, wsize);
289 pdutype = "[SYN PDU]";
290 else if (flags & F_ACK)
291 pdutype = "[ACK PDU]";
292 else if (flags & F_RST)
293 pdutype = "[RST PDU]";
295 col_append_fstr(pinfo->cinfo, COL_INFO, "%s Flags=0x%02X Ack=%u Seq=%u WSize=%u", pdutype, flags, ackno, seqno, wsize);
297 /* Parse and verify checksum */
299 cksum_data_len = hlen + plen;
300 if (!cattp_check_checksum) {
301 /* We have turned checksum checking off; we do NOT checksum it. */
302 proto_tree_add_checksum(cattp_tree, tvb, offset, hf_cattp_checksum, hf_cattp_checksum_status, &ei_cattp_checksum,
303 pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
305 /* We haven't turned checksum checking off; checksum it. */
307 /* Unlike TCP, CATTP does not make use of a pseudo-header for checksum */
308 SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, header_offset, cksum_data_len);
309 proto_tree_add_checksum(cattp_tree, tvb, offset, hf_cattp_checksum, hf_cattp_checksum_status, &ei_cattp_checksum,
310 pinfo, in_cksum(cksum_vec, 1), ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_IN_CKSUM);
311 } /* End of checksum code */
315 offset = dissect_cattp_synpdu(tvb, pinfo, cattp_tree, offset);
316 else if (flags & F_EAK)
317 offset = dissect_cattp_eakpdu(tvb, pinfo, cattp_tree, offset, hlen);
318 else if (flags & F_RST)
319 offset = dissect_cattp_rstpdu(tvb, pinfo, cattp_tree, offset);
320 /* for other PDU types nothing special to be displayed in detail tree. */
322 if (plen > 0) { /* Call generic data handle if data exists. */
323 col_append_fstr(pinfo->cinfo, COL_INFO, " DataLen=%u", plen);
324 tvb = tvb_new_subset_remaining(tvb, offset);
325 call_data_dissector(tvb, pinfo, tree);
327 return tvb_captured_length(tvb);
330 /* The heuristic dissector function checks if the UDP packet may be a cattp packet */
332 dissect_cattp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
334 if (tvb_captured_length(tvb) >= CATTP_HBLEN) { /* check of data is big enough for base header. */
338 hlen = tvb_get_guint8(tvb, 3); /* header len */
339 plen = tvb_get_ntohs(tvb, 8); /* payload len */
341 if (hlen+plen != tvb_reported_length(tvb)) /* check if data length is ok. */
344 flags = tvb_get_guint8(tvb, 0) & M_FLAGS;
345 if ( (flags & M_PDU_SYN) == F_SYN ||
346 (flags & M_PDU_RST) == F_RST ||
347 (flags & M_PDU_ACK) == F_ACK ) { /* check if flag combi is valid */
348 dissect_cattp(tvb, pinfo, tree, data);
355 /* Function to register the dissector, called by infrastructure. */
357 proto_register_cattp(void)
359 static hf_register_info hf[] = {
363 "Flags", "cattp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
370 "Synchronize Flag", "cattp.flags.syn", FT_UINT8, BASE_DEC, NULL, F_SYN,
377 "Acknowledge Flag", "cattp.flags.ack", FT_UINT8, BASE_DEC, NULL, F_ACK,
384 "Extended Acknowledge Flag", "cattp.flags.eak", FT_UINT8, BASE_DEC, NULL, F_EAK,
391 "Reset Flag", "cattp.flags.rst", FT_UINT8, BASE_DEC, NULL, F_RST,
398 "NULL Flag", "cattp.flags.nul", FT_UINT8, BASE_DEC, NULL, F_NUL,
405 "Segmentation Flag", "cattp.flags.seg", FT_UINT8, BASE_DEC, NULL, F_SEG,
412 "Version", "cattp.version", FT_UINT8, BASE_HEX, NULL, M_VERSION,
419 "Header Length", "cattp.hlen", FT_UINT8, BASE_DEC, NULL, 0x0,
426 "Source Port", "cattp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
433 "Destination Port", "cattp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
440 "Data Length", "cattp.datalen", FT_UINT16, BASE_DEC, NULL, 0x0,
447 "Sequence Number", "cattp.seq", FT_UINT16, BASE_DEC, NULL, 0x0,
454 "Acknowledgement Number", "cattp.ack", FT_UINT16, BASE_DEC, NULL, 0x0,
459 &hf_cattp_windowsize,
461 "Window Size", "cattp.windowsize", FT_UINT16, BASE_DEC, NULL, 0x0,
468 "Checksum", "cattp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
473 &hf_cattp_checksum_status,
475 "Checksum Status", "cattp.checksum.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
480 &hf_cattp_identification,
482 "Identification", "cattp.identification", FT_BYTES, BASE_NONE, NULL, 0x0,
489 "ICCID", "cattp.iccid", FT_STRING, BASE_NONE, NULL, 0x0,
496 "Maxpdu", "cattp.maxpdu", FT_UINT16, BASE_DEC, NULL, 0x0,
503 "Maxsdu", "cattp.maxsdu", FT_UINT16, BASE_DEC, NULL, 0x0,
510 "Reason Code", "cattp.rc", FT_UINT8, BASE_DEC, VALS(cattp_reset_reason), 0x0,
517 "Identification Length", "cattp.idlen", FT_UINT8, BASE_DEC, NULL, 0x0,
524 "Acknowledgement Number", "cattp.eak", FT_UINT16, BASE_DEC, NULL, 0x0,
531 "Extended Acknowledgement Numbers", "cattp.eaks", FT_UINT16, BASE_DEC, NULL, 0x0,
537 /* Setup protocol subtree array */
538 static gint *ett[] = {
545 static ei_register_info ei[] = {
546 { &ei_cattp_checksum, { "cattp.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
549 module_t *cattp_module;
550 expert_module_t* expert_cattp;
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));
560 expert_cattp = expert_register_protocol(proto_cattp);
561 expert_register_field_array(expert_cattp, ei, array_length(ei));
563 cattp_module = prefs_register_protocol(proto_cattp, NULL);
564 prefs_register_bool_preference(cattp_module, "checksum",
565 "Validate checksum of all messages",
566 "Whether the checksum of all messages should be validated or not",
567 &cattp_check_checksum);
569 prefs_register_obsolete_preference(cattp_module, "enable");
574 proto_reg_handoff_cattp(void)
576 dissector_handle_t cattp_handle;
578 /* Create dissector handle */
579 cattp_handle = create_dissector_handle(dissect_cattp, proto_cattp);
581 heur_dissector_add("udp", dissect_cattp_heur, "CAT-TP over UDP", "cattp_udp", proto_cattp, HEURISTIC_ENABLE);
582 dissector_add_for_decode_as_with_preference("udp.port", cattp_handle);
586 * Editor modelines - https://www.wireshark.org/tools/modelines.html
591 * indent-tabs-mode: nil
594 * vi: set shiftwidth=4 tabstop=8 expandtab:
595 * :indentSize=4:tabSize=8:noTabs=true: