9e293f40025cf0abfb564d641e34205ee4103b9d
[metze/wireshark/wip.git] / epan / dissectors / packet-cattp.c
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  * 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.
17  *
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.
22  *
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.
26  */
27 #include "config.h"
28
29 #include <epan/packet.h>
30 #include <epan/prefs.h>
31 #include <epan/expert.h>
32 #include <epan/in_cksum.h>
33
34 #define CATTP_SHORTNAME "CAT-TP"
35 #define CATTP_HBLEN 18
36
37 #define F_SYN 0x80
38 #define F_ACK 0x40
39 #define F_EAK 0x20
40 #define F_RST 0x10
41 #define F_NUL 0x08
42 #define F_SEG 0x04
43
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 */
50
51 #define ICCID_PREFIX 0x98
52
53 static int proto_cattp = -1;
54
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;
59
60 static int hf_cattp_flags = -1;
61
62 /* flag components */
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;
70
71 /* structure of flag components */
72 static const int *cattp_flags[] = {
73     &hf_cattp_flag_syn,
74     &hf_cattp_flag_ack,
75     &hf_cattp_flag_eak,
76     &hf_cattp_flag_rst,
77     &hf_cattp_flag_nul,
78     &hf_cattp_flag_seg,
79     &hf_cattp_version,
80     NULL
81 };
82
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;
100
101 static expert_field ei_cattp_checksum = EI_INIT;
102
103 /* Preference to control whether to check the CATTP checksum */
104 static gboolean cattp_check_checksum = TRUE;
105
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" },
115     { 7, "RFU" },
116     { 0, NULL }
117 };
118
119 /* Forward declartion due to use of heuristic dissection preference. */
120 void proto_reg_handoff_cattp(void);
121 void proto_register_cattp(void);
122
123 /* Dissection of SYN PDUs */
124 static guint32
125 dissect_cattp_synpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
126 {
127     proto_item *idi, *id_tree;
128     guint8      idlen;
129
130     proto_tree_add_item(cattp_tree, hf_cattp_maxpdu, tvb, offset, 2, ENC_BIG_ENDIAN);
131     offset += 2;
132
133     proto_tree_add_item(cattp_tree, hf_cattp_maxsdu, tvb, offset, 2, ENC_BIG_ENDIAN);
134     offset += 2;
135
136     idlen = tvb_get_guint8(tvb, offset);
137     idi = proto_tree_add_uint(cattp_tree, hf_cattp_idlen, tvb, offset, 1, idlen);
138     offset += 1;
139
140     col_append_fstr(pinfo->cinfo, COL_INFO, " IdLen=%u ", idlen);
141
142     id_tree = proto_item_add_subtree(idi, ett_cattp_id);
143
144     if (idlen > 0) {
145         guint8 first_id_byte;
146
147         first_id_byte = tvb_get_guint8(tvb, offset);
148         proto_tree_add_item(id_tree, hf_cattp_identification, tvb, offset, idlen, ENC_NA);
149
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) {
154             wmem_strbuf_t *buf;
155             int i;
156
157             buf = wmem_strbuf_new(wmem_packet_scope(), "");
158
159             /* switch nibbles */
160             for (i = 0; i < idlen; i++) {
161                 guint8 c, n;
162
163                 c = tvb_get_guint8(tvb, offset + i);
164                 n = ((c & 0xF0) >> 4) + ((c & 0x0F) << 4);
165                 wmem_strbuf_append_printf(buf, "%02X", n);
166             }
167
168             proto_tree_add_string(id_tree, hf_cattp_iccid, tvb, offset,
169                                  idlen, wmem_strbuf_get_str(buf));
170         }
171         offset += idlen;
172     }
173     return offset;
174 }
175
176 /* Dissection of Extended Acknowledgement PDUs */
177 static guint32
178 dissect_cattp_eakpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset, guint8 hlen)
179 {
180     proto_item *eaki;
181     guint8      eak_count;
182
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);
186
187     if (eak_count > 0) {
188         proto_item *eak_tree;
189         int i;
190
191         col_append_fstr(pinfo->cinfo, COL_INFO, " EAKs=%u", eak_count);
192         eak_tree = proto_item_add_subtree(eaki, ett_cattp_eaks);
193
194         for (i = 0; i < eak_count; i++) {
195             proto_tree_add_item(eak_tree, hf_cattp_eaks, tvb, offset, 2, ENC_BIG_ENDIAN);
196             offset += 2;
197         }
198     }
199
200     return offset;
201 }
202
203 /* Dissection of Extended Acknowledgement PDUs */
204 static guint32
205 dissect_cattp_rstpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *cattp_tree, guint32 offset)
206 {
207     guint8       rc;
208     const gchar *rc_str;
209
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);
213
214     proto_tree_add_item(cattp_tree, hf_cattp_rc, tvb, offset, 1, ENC_BIG_ENDIAN);
215     return ++offset;
216 }
217
218 /* Dissection of the base header */
219 static int
220 dissect_cattp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
221 {
222     const char *pdutype = "[Unknown PDU]";
223     proto_item *ti, *cattp_tree;
224     guint32     offset;
225     vec_t       cksum_vec[1];
226     int         header_offset;
227     guint       cksum_data_len;
228     guint8      flags, first_byte, hlen, ver;
229     guint16     plen, ackno, seqno, wsize, sport, dport;
230
231     col_set_str(pinfo->cinfo, COL_PROTOCOL, CATTP_SHORTNAME);
232
233     /* Clear out stuff in the info column */
234     col_clear(pinfo->cinfo, COL_INFO);
235
236     hlen = tvb_get_guint8(tvb, 3); /* lookahead header len. */
237
238     offset = 0;
239     ti = proto_tree_add_protocol_format(tree, proto_cattp, tvb, offset, hlen,
240                                         "Card Application Toolkit Transport Protocol");
241
242     cattp_tree = proto_item_add_subtree(ti, ett_cattp);
243
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 */
250
251     /* Header length, varies for SYN(identification) and EAKs */
252     proto_tree_add_uint(cattp_tree, hf_cattp_hlen, tvb, offset, 1, hlen);
253     offset += 1;
254
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);
258     offset += 2;
259
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);
263     offset += 2;
264
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);
267
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);
271     offset += 2;
272
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);
276     offset += 2;
277
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);
281     offset += 2;
282
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);
286     offset += 2;
287
288     if (flags & F_SYN)
289            pdutype = "[SYN PDU]";
290     else if (flags & F_ACK)
291            pdutype = "[ACK PDU]";
292     else if (flags & F_RST)
293            pdutype = "[RST PDU]";
294
295     col_append_fstr(pinfo->cinfo, COL_INFO, "%s Flags=0x%02X Ack=%u Seq=%u WSize=%u", pdutype, flags, ackno, seqno, wsize);
296
297     /* Parse and verify checksum */
298     header_offset  = 0;
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);
304     } else {
305         /* We haven't turned checksum checking off; checksum it. */
306
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 */
312     offset += 2;
313
314     if (flags & F_SYN)
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. */
321
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);
326     }
327     return tvb_captured_length(tvb);
328 }
329
330 /* The heuristic dissector function checks if the UDP packet may be a cattp packet */
331 static gboolean
332 dissect_cattp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
333 {
334     if (tvb_captured_length(tvb) >= CATTP_HBLEN) { /* check of data is big enough for base header. */
335         guint8  flags, hlen;
336         guint16 plen;
337
338         hlen = tvb_get_guint8(tvb, 3); /* header len  */
339         plen = tvb_get_ntohs(tvb, 8);  /* payload len */
340
341         if (hlen+plen != tvb_reported_length(tvb)) /* check if data length is ok. */
342             return FALSE;
343
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);
349             return TRUE;
350         }
351     }
352     return FALSE;
353 }
354
355 /* Function to register the dissector, called by infrastructure. */
356 void
357 proto_register_cattp(void)
358 {
359     static hf_register_info hf[] = {
360         {
361             &hf_cattp_flags,
362             {
363                 "Flags", "cattp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
364                 NULL, HFILL
365             }
366         },
367         {
368             &hf_cattp_flag_syn,
369             {
370                 "Synchronize Flag", "cattp.flags.syn", FT_UINT8, BASE_DEC, NULL, F_SYN,
371                 NULL, HFILL
372             }
373         },
374         {
375             &hf_cattp_flag_ack,
376             {
377                 "Acknowledge Flag", "cattp.flags.ack", FT_UINT8, BASE_DEC, NULL, F_ACK,
378                 NULL, HFILL
379             }
380         },
381         {
382             &hf_cattp_flag_eak,
383             {
384                 "Extended Acknowledge Flag", "cattp.flags.eak", FT_UINT8, BASE_DEC, NULL, F_EAK,
385                 NULL, HFILL
386             }
387         },
388         {
389             &hf_cattp_flag_rst,
390             {
391                 "Reset Flag", "cattp.flags.rst", FT_UINT8, BASE_DEC, NULL, F_RST,
392                 NULL, HFILL
393             }
394         },
395         {
396             &hf_cattp_flag_nul,
397             {
398                 "NULL Flag", "cattp.flags.nul", FT_UINT8, BASE_DEC, NULL, F_NUL,
399                 NULL, HFILL
400             }
401         },
402         {
403             &hf_cattp_flag_seg,
404             {
405                 "Segmentation Flag", "cattp.flags.seg", FT_UINT8, BASE_DEC, NULL, F_SEG,
406                 NULL, HFILL
407             }
408         },
409         {
410             &hf_cattp_version,
411             {
412                 "Version", "cattp.version", FT_UINT8, BASE_HEX, NULL, M_VERSION,
413                 NULL, HFILL
414             }
415         },
416         {
417             &hf_cattp_hlen,
418             {
419                 "Header Length", "cattp.hlen", FT_UINT8, BASE_DEC, NULL, 0x0,
420                 NULL, HFILL
421             }
422         },
423         {
424             &hf_cattp_srcport,
425             {
426                 "Source Port", "cattp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
427                 NULL, HFILL
428             }
429         },
430         {
431             &hf_cattp_dstport,
432             {
433                 "Destination Port", "cattp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
434                 NULL, HFILL
435             }
436         },
437         {
438             &hf_cattp_datalen,
439             {
440                 "Data Length", "cattp.datalen", FT_UINT16, BASE_DEC, NULL, 0x0,
441                 NULL, HFILL
442             }
443         },
444         {
445             &hf_cattp_seq,
446             {
447                 "Sequence Number", "cattp.seq", FT_UINT16, BASE_DEC, NULL, 0x0,
448                 NULL, HFILL
449             }
450         },
451         {
452             &hf_cattp_ack,
453             {
454                 "Acknowledgement Number", "cattp.ack", FT_UINT16, BASE_DEC, NULL, 0x0,
455                 NULL, HFILL
456             }
457         },
458         {
459             &hf_cattp_windowsize,
460             {
461                 "Window Size", "cattp.windowsize", FT_UINT16, BASE_DEC, NULL, 0x0,
462                 NULL, HFILL
463             }
464         },
465         {
466             &hf_cattp_checksum,
467             {
468                 "Checksum", "cattp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
469                 NULL, HFILL
470             }
471         },
472         {
473             &hf_cattp_checksum_status,
474             {
475                 "Checksum Status", "cattp.checksum.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
476                 NULL, HFILL
477             }
478         },
479         {
480             &hf_cattp_identification,
481             {
482                 "Identification", "cattp.identification", FT_BYTES, BASE_NONE, NULL, 0x0,
483                 NULL, HFILL
484             }
485         },
486         {
487             &hf_cattp_iccid,
488             {
489                 "ICCID", "cattp.iccid", FT_STRING, BASE_NONE, NULL, 0x0,
490                 NULL, HFILL
491             }
492         },
493         {
494             &hf_cattp_maxpdu,
495             {
496                 "Maxpdu", "cattp.maxpdu", FT_UINT16, BASE_DEC, NULL, 0x0,
497                 NULL, HFILL
498             }
499         },
500         {
501             &hf_cattp_maxsdu,
502             {
503                 "Maxsdu", "cattp.maxsdu", FT_UINT16, BASE_DEC, NULL, 0x0,
504                 NULL, HFILL
505             }
506         },
507         {
508             &hf_cattp_rc,
509             {
510                 "Reason Code", "cattp.rc", FT_UINT8, BASE_DEC, VALS(cattp_reset_reason), 0x0,
511                 NULL, HFILL
512             }
513         },
514         {
515             &hf_cattp_idlen,
516             {
517                 "Identification Length", "cattp.idlen", FT_UINT8, BASE_DEC, NULL, 0x0,
518                 NULL, HFILL
519             }
520         },
521         {
522             &hf_cattp_eaks,
523             {
524                 "Acknowledgement Number", "cattp.eak", FT_UINT16, BASE_DEC, NULL, 0x0,
525                 NULL, HFILL
526             }
527         },
528         {
529             &hf_cattp_eaklen,
530             {
531                 "Extended Acknowledgement Numbers", "cattp.eaks", FT_UINT16, BASE_DEC, NULL, 0x0,
532                 NULL, HFILL
533             }
534         }
535     };
536
537     /* Setup protocol subtree array */
538     static gint *ett[] = {
539         &ett_cattp,
540         &ett_cattp_flags,
541         &ett_cattp_id,
542         &ett_cattp_eaks
543     };
544
545     static ei_register_info ei[] = {
546         { &ei_cattp_checksum, { "cattp.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
547     };
548
549     module_t *cattp_module;
550     expert_module_t* expert_cattp;
551
552     proto_cattp = proto_register_protocol (
553                       "ETSI Card Application Toolkit Transport Protocol",    /* name */
554                       CATTP_SHORTNAME, /* short name */
555                       "cattp"          /* abbrev */
556                   );
557
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));
562
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);
568
569     prefs_register_obsolete_preference(cattp_module, "enable");
570 }
571
572 /* Handoff */
573 void
574 proto_reg_handoff_cattp(void)
575 {
576     dissector_handle_t cattp_handle;
577
578     /* Create dissector handle */
579     cattp_handle = create_dissector_handle(dissect_cattp, proto_cattp);
580
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);
583 }
584
585 /*
586  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
587  *
588  * Local variables:
589  * c-basic-offset: 4
590  * tab-width: 8
591  * indent-tabs-mode: nil
592  * End:
593  *
594  * vi: set shiftwidth=4 tabstop=8 expandtab:
595  * :indentSize=4:tabSize=8:noTabs=true:
596  */