Add in some heuristics to try to detect AIX libpcap format. (This works
[obnox/wireshark/wip.git] / packet-sap.c
1 /* packet-sap.c
2  * Routines for sap packet dissection
3  * RFC 2974
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-sap.c,v 1.23 2001/06/18 02:17:52 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from packet-tftp.c
14  * 
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  * 
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45 #include "packet-ipv6.h"
46
47 #define UDP_PORT_SAP    9875
48
49 #define MCAST_SAP_VERSION_MASK 0xE0 /* 3 bits for  SAP version*/
50 #define MCAST_SAP_VERSION_SHIFT 5   /* Right shift 5 bits to get the version */
51 #define MCAST_SAP_VER0 0            /* Version 0 */
52 #define MCAST_SAP_VER1PLUS 1        /* Version 1 or later */
53 static const value_string mcast_sap_ver[] = {
54                   { MCAST_SAP_VER0,     "SAPv0"},
55                   { MCAST_SAP_VER1PLUS, "SAPv1 or later"},
56                   { 0,                  NULL} };
57
58 static const true_false_string mcast_sap_address_type = {
59         "IPv6",
60         "IPv4"
61 };
62
63 static const true_false_string flags_set_truth = {
64         "Set",
65         "Not set"
66 };
67
68 static const true_false_string mcast_sap_message_type = {
69         "Deletion",
70         "Announcement"
71 };
72
73 static const true_false_string mcast_sap_crypt_type = {
74         "Payload encrypted",
75         "Payload not encrypted "
76 };
77
78 static const true_false_string mcast_sap_comp_type = {
79         "Payload compressed",
80         "Payload not compressed"
81 };
82
83 static const value_string mcast_sap_auth_ver[] = {
84                   { 1, "SAP authentication header v1"},
85                   { 0,                  NULL} };
86
87 static const true_false_string mcast_sap_auth_pad = {
88         "Authentication subheader padded to 32 bits",
89         "No padding required for the authentication subheader"
90 };
91
92 #define MCAST_SAP_AUTH_TYPE_MASK 0x0F /* 4 bits for the type of the authentication header */
93 #define MCAST_SAP_AUTH_TYPE_PGP 0
94 #define MCAST_SAP_AUTH_TYPE_CMS 1
95 static const value_string mcast_sap_auth_type[] = {
96                   { MCAST_SAP_AUTH_TYPE_PGP,  "PGP"},
97                   { MCAST_SAP_AUTH_TYPE_CMS,  "CMS"},
98                   { 0,                   NULL} };
99
100 #define MCAST_SAP_BIT_A 0x10 /* Address type: 0 IPv4, 1 IPv6 */
101 #define MCAST_SAP_BIT_R 0x08 /* Reserved: Must be 0 */
102 #define MCAST_SAP_BIT_T 0x04 /* Message Type: 0 announcement, 1 deletion */
103 #define MCAST_SAP_BIT_E 0x02 /* Encryption Bit: 1 payload encrypted */
104 #define MCAST_SAP_BIT_C 0x01 /* Compressed Bit: 1 payload zlib compressed */
105
106 #define MCAST_SAP_AUTH_BIT_P 0x10 /* Padding required for the authentication header */
107
108
109 static int proto_sap = -1;
110 static int hf_sap_flags = -1;
111 static int hf_sap_flags_v = -1;
112 static int hf_sap_flags_a = -1;
113 static int hf_sap_flags_r = -1;
114 static int hf_sap_flags_t = -1;
115 static int hf_sap_flags_e = -1;
116 static int hf_sap_flags_c = -1;
117 static int hf_auth_data = -1;
118 static int hf_auth_flags = -1;
119 static int hf_auth_flags_v = -1;
120 static int hf_auth_flags_p = -1;
121 static int hf_auth_flags_t = -1;
122
123 static gint ett_sap = -1;
124 static gint ett_sap_flags = -1;
125 static gint ett_sap_auth = -1;
126 static gint ett_sap_authf = -1;
127
128 static dissector_handle_t sdp_handle;
129
130 static void
131 dissect_sap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
132 {
133         int offset = 0;
134         int sap_version, is_ipv6, is_del, is_enc, is_comp, addr_len;
135         guint8 vers_flags;
136         guint8 auth_len;
137         guint16 tmp1;
138         const guint8 *addr;
139         guint8 auth_flags;
140         tvbuff_t *next_tvb;
141
142         proto_item *si, *sif;
143         proto_tree *sap_tree, *sap_flags_tree;
144
145         if (check_col(pinfo->fd, COL_PROTOCOL))
146                 col_set_str(pinfo->fd, COL_PROTOCOL, "SAP");
147         if (check_col(pinfo->fd, COL_INFO))
148                 col_clear(pinfo->fd, COL_INFO);
149         
150         vers_flags = tvb_get_guint8(tvb, offset);
151         is_ipv6 = vers_flags&MCAST_SAP_BIT_A;
152         is_del = vers_flags&MCAST_SAP_BIT_T;
153         is_enc = vers_flags&MCAST_SAP_BIT_E;
154         is_comp = vers_flags&MCAST_SAP_BIT_C;
155
156         sap_version = (vers_flags&MCAST_SAP_VERSION_MASK)>>MCAST_SAP_VERSION_SHIFT;
157         addr_len = (is_ipv6) ? sizeof(struct e_in6_addr) : 4;
158
159         if (check_col(pinfo->fd, COL_INFO)) {
160                 col_add_fstr(pinfo->fd, COL_INFO, "%s (v%u)",
161                              (is_del) ? "Deletion" : "Announcement", sap_version);
162         }
163
164         if (tree) {
165           si = proto_tree_add_item(tree, proto_sap, tvb, offset,
166               tvb_length_remaining(tvb, offset), FALSE);
167           sap_tree = proto_item_add_subtree(si, ett_sap);
168
169           sif = proto_tree_add_uint(sap_tree, hf_sap_flags, tvb, offset, 1, vers_flags);
170           sap_flags_tree = proto_item_add_subtree(sif, ett_sap_flags);
171           proto_tree_add_uint(sap_flags_tree, hf_sap_flags_v, tvb, offset, 1, vers_flags);
172           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_a, tvb, offset, 1, vers_flags);
173           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_r, tvb, offset, 1, vers_flags);
174           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_t, tvb, offset, 1, vers_flags);
175           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_e, tvb, offset, 1, vers_flags);
176           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_c, tvb, offset, 1, vers_flags);
177           offset++;
178
179           auth_len = tvb_get_guint8(tvb, offset);
180           proto_tree_add_text(sap_tree, tvb, offset, 1, "Authentication Length: %u", auth_len);
181           offset++;
182
183           tmp1 = tvb_get_ntohs(tvb, offset);
184           proto_tree_add_text(sap_tree, tvb, offset, 2, "Message Identifier Hash: 0x%x", tmp1);
185           offset +=2;
186
187           addr = tvb_get_ptr(tvb, offset, addr_len);
188           proto_tree_add_text(sap_tree, tvb, offset, addr_len, "Originating Source: %s",
189               (is_ipv6) ? ip6_to_str((struct e_in6_addr*)addr) : ip_to_str(addr));
190           offset += addr_len;
191
192           /* Authentication data lives in its own subtree */
193           if (auth_len > 0) {
194                   guint32 auth_data_len;
195                   proto_item *sdi, *sai;
196                   proto_tree *sa_tree, *saf_tree;
197                   int has_pad;
198                   guint8 pad_len = 0;
199
200                   auth_data_len = auth_len * sizeof(guint32);
201
202                   sdi = proto_tree_add_item(sap_tree, hf_auth_data, tvb, offset, auth_data_len, FALSE);
203                   sa_tree = proto_item_add_subtree(sdi, ett_sap_auth);
204
205                   auth_flags = tvb_get_guint8(tvb, offset);
206                   sai = proto_tree_add_uint(sa_tree, hf_auth_flags, tvb, offset, 1, auth_flags);
207                   saf_tree = proto_item_add_subtree(sai, ett_sap_authf);
208                   proto_tree_add_uint(saf_tree, hf_auth_flags_v, tvb, offset, 1, auth_flags);
209                   proto_tree_add_boolean(saf_tree, hf_auth_flags_p, tvb, offset, 1, auth_flags);
210                   proto_tree_add_uint(saf_tree, hf_auth_flags_t, tvb, offset, 1, auth_flags);
211
212                   has_pad = auth_flags&MCAST_SAP_AUTH_BIT_P;
213                   if (has_pad)
214                          pad_len = tvb_get_guint8(tvb, offset+auth_data_len-1);
215
216                   proto_tree_add_text(sa_tree, tvb, offset+1, auth_data_len-pad_len-1,
217                                       "Authentication subheader: (%u byte%s)",
218                                       auth_data_len-1, plurality(auth_data_len-1, "", "s"));
219                   if (has_pad) {
220                           proto_tree_add_text(sa_tree, tvb, offset+auth_data_len-pad_len, pad_len,
221                                               "Authentication data padding: (%u byte%s)",
222                                               pad_len, plurality(pad_len, "", "s"));
223                           proto_tree_add_text(sa_tree, tvb, offset+auth_data_len-1, 1,
224                                               "Authentication data pad count: %u byte%s",
225                                               pad_len, plurality(pad_len, "", "s"));
226                   }
227
228                   offset += auth_data_len;
229           }
230           if (is_enc || is_comp) {
231                   char *mangle;
232                   if (is_enc && is_comp) mangle = "compressed and encrypted";
233                   else if (is_enc) mangle = "encrypted";
234                   else mangle = "compressed";
235                   proto_tree_add_text(sap_tree, tvb, offset,
236                                       tvb_length_remaining(tvb, offset),
237                                       "The rest of the packet is %s", mangle);
238                   return;
239           }
240
241           /* Do we have the optional payload type aka. MIME content specifier */
242           if (tvb_strneql(tvb, offset, "v=", strlen("v="))) {
243                   gint remaining_len;
244                   guint32 pt_len;
245                   int pt_string_len;
246
247                   remaining_len = tvb_length_remaining(tvb, offset);
248                   if (remaining_len == 0) {
249                       /*
250                        * "tvb_strneql()" failed because there was no
251                        * data left in the packet.
252                        *
253                        * Set the remaining length to 1, so that
254                        * we throw the appropriate exception in
255                        * "tvb_get_ptr()", rather than displaying
256                        * the payload type.
257                        */
258                       remaining_len = 1;
259                   }
260                   pt_string_len = tvb_strnlen(tvb, offset, remaining_len);
261                   if (pt_string_len == -1) {
262                       /*
263                        * We didn't find a terminating '\0'; run to the
264                        * end of the buffer.
265                        */
266                       pt_string_len = remaining_len;
267                       pt_len = pt_string_len;
268                   } else {
269                       /*
270                        * Include the '\0' in the total item length.
271                        */
272                       pt_len = pt_string_len + 1;
273                   }
274                   proto_tree_add_text(sap_tree, tvb, offset, pt_len,
275                       "Payload type: %.*s", pt_string_len,
276                       tvb_get_ptr(tvb, offset, pt_string_len));
277                   offset += pt_len;
278           }
279         }
280
281         /* Done with SAP */
282         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
283         call_dissector(sdp_handle, next_tvb, pinfo, tree);
284
285         return;
286 }
287
288 void proto_register_sap(void)
289 {
290
291   static hf_register_info hf[] = {
292     { &hf_sap_flags,
293       { "Flags",         "sap.flags",
294         FT_UINT8, BASE_HEX, NULL, 0x0,
295         "Bits in the beginning of the SAP header", HFILL }},
296
297     { &hf_sap_flags_v,
298       { "Version Number",         "sap.flags.v",
299         FT_UINT8, BASE_DEC, VALS(mcast_sap_ver), MCAST_SAP_VERSION_MASK,
300         "3 bit version field in the SAP header", HFILL }},
301
302     { &hf_sap_flags_a,
303       { "Address Type",           "sap.flags.a",
304         FT_BOOLEAN, 8, TFS(&mcast_sap_address_type), MCAST_SAP_BIT_A,
305         "Originating source address type", HFILL }},
306
307     { &hf_sap_flags_r,
308       { "Reserved",               "sap.flags.r",
309         FT_BOOLEAN, 8, TFS(&flags_set_truth), MCAST_SAP_BIT_R,
310         "Reserved", HFILL }},
311
312     { &hf_sap_flags_t,
313       { "Message Type",           "sap.flags.t",
314         FT_BOOLEAN, 8, TFS(&mcast_sap_message_type), MCAST_SAP_BIT_T,
315         "Announcement type", HFILL }},
316
317     { &hf_sap_flags_e,
318       { "Encryption Bit",         "sap.flags.e",
319         FT_BOOLEAN, 8, TFS(&mcast_sap_crypt_type), MCAST_SAP_BIT_E,
320         "Encryption", HFILL }},
321
322     { &hf_sap_flags_c,
323       { "Compression Bit",         "sap.flags.c",
324         FT_BOOLEAN, 8, TFS(&mcast_sap_comp_type), MCAST_SAP_BIT_C,
325         "Compression", HFILL }},
326
327     { &hf_auth_data,
328       { "Authentication data",     "sap.auth",
329         FT_NONE, BASE_NONE, NULL, 0x0,
330         "Auth data", HFILL }},
331
332     { &hf_auth_flags,
333       { "Authentication data flags", "sap.auth.flags",
334         FT_UINT8, BASE_HEX, NULL, 0x0,
335         "Auth flags", HFILL }},
336
337     { &hf_auth_flags_v,
338       { "Version Number",         "sap.auth.flags.v",
339         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_ver), MCAST_SAP_VERSION_MASK,
340         "Version", HFILL }},
341
342     { &hf_auth_flags_p,
343       { "Padding Bit",            "sap.auth.flags.p",
344         FT_BOOLEAN, 8, TFS(&mcast_sap_auth_pad), MCAST_SAP_AUTH_BIT_P,
345         "Compression", HFILL }},
346
347     { &hf_auth_flags_t,
348       { "Authentication Type",         "sap.auth.flags.t",
349         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_type), MCAST_SAP_AUTH_TYPE_MASK,
350         "Auth type", HFILL }}
351   };
352   static gint *ett[] = {
353     &ett_sap,
354     &ett_sap_flags,
355     &ett_sap_auth,
356     &ett_sap_authf,
357   };
358
359   proto_sap = proto_register_protocol("Session Announcement Protocol", "SAP",
360                                       "sap");
361   proto_register_field_array(proto_sap, hf, array_length(hf));
362   proto_register_subtree_array(ett, array_length(ett));
363 }
364
365 void
366 proto_reg_handoff_sap(void)
367 {
368   dissector_add("udp.port", UDP_PORT_SAP, dissect_sap, proto_sap);
369
370   /*
371    * Get a handle for the SDP dissector.
372    */
373   sdp_handle = find_dissector("sdp");
374 }