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