Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-msdp.c
1 /* packet-msdp.c
2  * Routines for Multicast Source Discovery Protocol (MSDP) dissection.
3  * draft-ietf-msdp-spec-10.txt
4  *
5  * Copyright 2001, Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id$
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdlib.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37
38 /* MSDP message types. The messages are TLV (Type-Length-Value) encoded */
39 enum { MSDP_SA     = 1,
40        MSDP_SA_REQ,
41        MSDP_SA_RSP,
42        MSDP_KEEP_ALIVE,
43        MSDP_NOTIFICATION,
44
45        /* Theses are only assigned in MSDP spec. Their use is specifed
46         * elsewhere */
47        MSDP_TRACE_IN_PROGRESS,
48        MSDP_TRACE_REPLY
49 };
50
51 static const value_string msdp_types[] = {
52         { MSDP_SA,                "IPv4 Source-Active"           },
53         { MSDP_SA_REQ,            "IPv4 Source-Active Request"   },
54         { MSDP_SA_RSP,            "IPv4 Source-Active Response"  },
55         { MSDP_KEEP_ALIVE,        "KeepAlive"                    },
56         { MSDP_NOTIFICATION,      "Notification"                 },
57
58         { MSDP_TRACE_IN_PROGRESS, "MSDP traceroute in progress"  },
59         { MSDP_TRACE_REPLY,       "MSDP traceroute reply"        },
60         { 0, NULL },
61 };
62
63
64 /* Error codes */
65 enum { MESSAGE_HEADER_ERROR = 1,
66        SA_REQUEST_ERROR,
67        SA_MESSAGE_SA_RESPONSE_ERROR,
68        HOLD_TIMER_EXPIRED,
69        FSM_ERROR,
70        NOTIFICATION,
71        CEASE
72 };
73
74 static const value_string error_vals[] = {
75         { MESSAGE_HEADER_ERROR,         "Message Header Error"         },
76         { SA_REQUEST_ERROR,             "SA-Request Error"             },
77         { SA_MESSAGE_SA_RESPONSE_ERROR, "SA-Message/SA-Response Error" },
78         { HOLD_TIMER_EXPIRED,           "Hold Timer Expired"           },
79         { FSM_ERROR,                    "Finite State Machine Error"   },
80         { NOTIFICATION,                 "Notification"                 },
81         { CEASE,                        "Cease"                        },
82         { 0, NULL },
83 };
84
85
86 /* Message Header Error subcodes */
87 static const value_string hdr_error_vals[] = {
88         { 0, "Unspecific"         },
89         { 2, "Bad Message Length" },
90         { 3, "Bad Message Type"   },
91         { 0, NULL },
92 };
93
94 /* SA-Request Error subcodes (the O-bit is always clear) */
95 static const value_string sa_req_error_vals[] = {
96         { 0, "Unspecific"    },
97         { 1, "Invalid Group" },
98         { 0, NULL },
99 };
100
101 /* SA-Message/SA-Response Error subcodes */
102 static const value_string sa_msg_error_vals[] = {
103         { 0, "Unspecific"                             },
104         { 1, "Invalid Entry Count"                    },
105         { 2, "Invalid RP Address"                     },
106         { 3, "Invalid Group Address"                  },
107         { 4, "Invalid Source Address"                 },
108         { 5, "Invalid Sprefix Length"                 },
109         { 6, "Looping SA (Self is RP)"                },
110         { 7, "Unknown Encapsulation"                  },
111         { 8, "Administrative Scope Boundary Violated" },
112         { 0, NULL },
113 };
114
115 /* Finite State Machine Error subcodes (the O-bit is always clear) */
116 static const value_string fsm_error_vals[] = {
117         { 0, "Unspecific"                        },
118         { 1, "Unexpected Message Type FSM Error" },
119         { 0, NULL },
120 };
121
122 /*
123  * Hold Timer Expired subcodes (the O-bit is always clear):
124  * Notification subcodes (the O-bit is always clear):
125  * Cease subcodes (the O-bit is always clear):
126  *
127  * These have only "Unspecific" specified.
128  */
129 static const value_string sa_unspec_error_vals[] = {
130         { 0, "Unspecific" },
131         { 0, NULL },
132 };
133
134
135 /* Initialize the protocol and registered fields */
136 static int proto_msdp = -1;
137 static int hf_msdp_type = -1;
138 static int hf_msdp_length = -1;
139
140 static int hf_msdp_sa_entry_count = -1;
141 static int hf_msdp_sa_rp_addr = -1;
142 static int hf_msdp_sa_reserved = -1;
143 static int hf_msdp_sa_sprefix_len = -1;
144 static int hf_msdp_sa_group_addr = -1;
145 static int hf_msdp_sa_src_addr = -1;
146
147 static int hf_msdp_sa_req_res = -1;
148 static int hf_msdp_sa_req_group = -1;
149
150 static int hf_msdp_not_o = -1;
151 static int hf_msdp_not_error = -1;
152 static int hf_msdp_not_error_sub = -1;
153
154 static int hf_msdp_not_ipv4 = -1;
155 static int hf_msdp_not_res = -1;
156 static int hf_msdp_not_entry_count = -1;
157 static int hf_msdp_not_sprefix_len = -1;
158
159
160 static gint ett_msdp = -1;
161 static gint ett_msdp_sa_entry = -1;
162 static gint ett_msdp_sa_enc_data = -1;
163 static gint ett_msdp_not_data = -1;
164
165
166 static dissector_handle_t ip_handle;
167
168
169 static void
170 dissect_msdp_sa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
171     int *offset, int len);
172 static void
173 dissect_msdp_notification(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, guint16 tlv_len);
174
175
176 static void
177 dissect_msdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
178 {
179         proto_item *ti;
180         proto_tree *msdp_tree;
181         int offset;
182
183         col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSDP");
184
185         if (check_col(pinfo->cinfo, COL_INFO))
186                 col_add_str(pinfo->cinfo, COL_INFO, val_to_str(tvb_get_guint8(tvb, 0),
187                                                             msdp_types,
188                                                             "<Unknown MSDP message type>"));
189
190         if (tree) {
191                 guint8 type;
192                 guint16 length;
193
194                 ti = proto_tree_add_item(tree, proto_msdp, tvb, 0, -1, FALSE);
195                 msdp_tree = proto_item_add_subtree(ti, ett_msdp);
196
197                 offset = 0;
198                 while (tvb_reported_length_remaining(tvb, offset) >= 3) {
199                         type = tvb_get_guint8(tvb, offset);
200                         length = tvb_get_ntohs(tvb, offset + 1);
201                         if (length < 3)
202                                 break;
203                         proto_tree_add_uint(msdp_tree, hf_msdp_type, tvb, offset, 1, type);
204                         proto_tree_add_uint(msdp_tree, hf_msdp_length, tvb, offset + 1, 2, length);
205                         offset += 3;
206                         length -= 3;
207
208                         switch (type) {
209                         case MSDP_SA:
210                         case MSDP_SA_RSP:
211                                 dissect_msdp_sa(tvb, pinfo, msdp_tree, &offset,
212                                     length);
213                                 break;
214                         case MSDP_SA_REQ:
215                                 proto_tree_add_item(msdp_tree, hf_msdp_sa_req_res, tvb, offset, 1, FALSE);
216                                 proto_tree_add_item(msdp_tree, hf_msdp_sa_req_group, tvb, offset + 1, 4, FALSE);
217                                 offset += 5;
218                                 break;
219                         case MSDP_NOTIFICATION:
220                                 dissect_msdp_notification(tvb, pinfo, msdp_tree, &offset, length);
221                                 break;
222                         default:
223                                 if (length > 0)
224                                         proto_tree_add_text(msdp_tree, tvb, offset, length, "TLV contents");
225                                 offset += length;
226                                 break;
227                         }
228                 }
229
230                 if (tvb_length_remaining(tvb, offset) > 0)
231                         proto_tree_add_text(msdp_tree, tvb, offset,
232                                             -1, "Trailing junk");
233         }
234
235         return;
236 }
237
238 /* Both Source-Active and Source-Active Response have the same format
239  * with one exception. Encapsulated multicast data is not allowed in
240  * SA Response.
241  */
242 static void dissect_msdp_sa(tvbuff_t *tvb, packet_info *pinfo,
243     proto_tree *tree, int *offset, int length)
244 {
245         guint8 entries;
246
247         if (length < 1)
248                 return;
249         entries = tvb_get_guint8(tvb, *offset);
250         proto_tree_add_uint(tree, hf_msdp_sa_entry_count, tvb, *offset, 1, entries);
251         *offset += 1;
252         length -= 1;
253
254         if (length < 4) {
255                 *offset += length;
256                 return;
257         }
258         proto_tree_add_item(tree, hf_msdp_sa_rp_addr, tvb, *offset, 4, FALSE);
259         *offset += 4;
260         length -= 4;
261
262         /* Put each of the (S,G) entries in their own subtree.
263          * This is probably visually better.
264          */
265         while (entries-- > 0) {
266                 proto_item *ei;
267                 proto_tree *entry_tree;
268
269                 if (length < 12) {
270                         *offset += length;
271                         return;
272                 }
273                 ei = proto_tree_add_text(tree, tvb, *offset, 12, "(S,G) block: %s/%u -> %s",
274                                          ip_to_str(tvb_get_ptr(tvb, *offset + 8, 4)),
275                                          tvb_get_guint8(tvb, *offset + 3),
276                                          ip_to_str(tvb_get_ptr(tvb, *offset + 4, 4)));
277                 entry_tree = proto_item_add_subtree(ei, ett_msdp_sa_entry);
278
279                 proto_tree_add_item(entry_tree, hf_msdp_sa_reserved, tvb, *offset, 3, FALSE);
280                 *offset += 3;
281                 length -= 3;
282                 proto_tree_add_item(entry_tree, hf_msdp_sa_sprefix_len, tvb, *offset, 1, FALSE);
283                 *offset += 1;
284                 length -= 1;
285                 proto_tree_add_item(entry_tree, hf_msdp_sa_group_addr, tvb, *offset, 4, FALSE);
286                 *offset += 4;
287                 length -= 4;
288                 proto_tree_add_item(entry_tree, hf_msdp_sa_src_addr, tvb, *offset, 4, FALSE);
289                 *offset += 4;
290                 length -= 4;
291         }
292
293         /*
294          * Check if an encapsulated multicast IPv4 packet follows
295          */
296         if (length > 0) {
297                 proto_item *ei;
298                 proto_tree *enc_tree;
299                 gint available_length, reported_length;
300                 tvbuff_t *next_tvb;
301
302                 ei = proto_tree_add_text(tree, tvb, *offset, length,
303                                          "Encapsulated IPv4 packet: %u bytes",
304                                          length);
305                 enc_tree = proto_item_add_subtree(ei, ett_msdp_sa_enc_data);
306
307                 available_length = tvb_length_remaining(tvb, *offset);
308                 reported_length = tvb_reported_length_remaining(tvb, *offset);
309                 DISSECTOR_ASSERT(available_length >= 0);
310                 DISSECTOR_ASSERT(reported_length >= 0);
311                 if (available_length > reported_length)
312                         available_length = reported_length;
313                 if (available_length > length)
314                         available_length = length;
315                 if (reported_length > length)
316                         reported_length = length;
317
318                 next_tvb = tvb_new_subset(tvb, *offset, available_length,
319                                           reported_length);
320                 /* Set the information columns read-only so that they
321                  * reflect the MSDP packet rather than the
322                  * encapsulated packet.
323                  */
324                 col_set_writable(pinfo->cinfo, FALSE);
325                 call_dissector(ip_handle, next_tvb, pinfo, enc_tree);
326         }
327         *offset += length;
328
329         return;
330 }
331
332 /* Note: updates *offset */
333 static void add_notification_data_ipv4addr(tvbuff_t *tvb, proto_tree *tree, int *offset, const char *addrtype)
334 {
335         guint32 ipaddr;
336
337         proto_tree_add_item(tree, hf_msdp_not_res, tvb, *offset, 3, FALSE);
338         *offset += 3;
339         ipaddr = tvb_get_ipv4(tvb, *offset);
340         proto_tree_add_ipv4_format(tree, hf_msdp_not_ipv4, tvb, *offset, 4, ipaddr,
341                                    "%s: %s", addrtype, ip_to_str((guint8 *)&ipaddr));
342         *offset += 4;
343
344         return;
345 }
346
347 static void dissect_msdp_notification(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, guint16 tlv_len)
348 {
349         guint8 error, error_sub;
350         const value_string *vals;
351
352         proto_tree_add_item(tree, hf_msdp_not_o, tvb, *offset, 1, FALSE);
353         proto_tree_add_item(tree, hf_msdp_not_error, tvb, *offset, 1, FALSE);
354         error = tvb_get_guint8(tvb, *offset);
355         error &= 0x7F;             /* Error is 7-bit field. O-bit is bit 8 */
356         *offset += 1;
357
358         /* Depending on the Error Code, we collect the correct
359          * value_strings for the Error subcode
360          */
361         switch (error) {
362         case MESSAGE_HEADER_ERROR:
363                 vals = hdr_error_vals;
364                 break;
365         case SA_REQUEST_ERROR:
366                 vals = sa_req_error_vals;
367                 break;
368         case SA_MESSAGE_SA_RESPONSE_ERROR:
369                 vals = sa_msg_error_vals;
370                 break;
371         case FSM_ERROR:
372                 vals = fsm_error_vals;
373                 break;
374         case HOLD_TIMER_EXPIRED:
375         case NOTIFICATION:
376         case CEASE:
377                 vals = sa_unspec_error_vals;
378                 break;
379         default:
380                 vals = sa_unspec_error_vals;
381                 break;
382         }
383
384         error_sub = tvb_get_guint8(tvb, *offset);
385         proto_tree_add_uint_format(tree, hf_msdp_not_error_sub, tvb, *offset, 1,
386                                    error_sub, "Error subcode: %s (%u)",
387                                    val_to_str(error_sub, vals, "<Unknown Error subcode>"),
388                                    error_sub);
389         *offset += 1;
390
391         /* Do switch again, this time to dissect the data portion
392          * correctly. Ugly.
393          */
394         switch (error) {
395                 tvbuff_t *next_tvb;
396         case SA_REQUEST_ERROR:
397                 add_notification_data_ipv4addr(tvb, tree, offset, "Group address");
398                 break;
399         case SA_MESSAGE_SA_RESPONSE_ERROR:
400                 if (error_sub == 0) {
401                         break;
402                 } else if (error_sub == 1) {
403                         proto_tree_add_item(tree, hf_msdp_not_entry_count, tvb, *offset, 1, FALSE);
404                         *offset += 1;
405                         break;
406                 } else if (error_sub == 2) {
407                         add_notification_data_ipv4addr(tvb, tree, offset, "RP address");
408                         break;
409                 } else if (error_sub == 3 || error_sub == 8) {
410                         add_notification_data_ipv4addr(tvb, tree, offset, "Group address");
411                         break;
412                 } else if (error_sub == 4) {
413                         add_notification_data_ipv4addr(tvb, tree, offset, "Source address");
414                         break;
415                 } else if (error_sub == 5) {
416                         proto_tree_add_item(tree, hf_msdp_not_sprefix_len, tvb, *offset, 1, FALSE);
417                         *offset += 1;
418                         break;
419                 } else if (error_sub == 6) {
420                         /* No break, causes fall through to next label */
421                 } else if (error_sub == 7) {
422                         proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
423                                             "Packet with unknown encapsulation: %u bytes",
424                                             tlv_len - 5);
425                         *offset += tlv_len - 5;
426                         break;
427                 } else {
428                         proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
429                                             "<Unknown data>: %u bytes",
430                                             tlv_len -5);
431                         *offset += tlv_len - 5;
432                         break;
433                 }
434                 /* Fall through */
435         case MESSAGE_HEADER_ERROR:
436         case NOTIFICATION:
437                 /* Data contains the message that had an error. Even a
438                  * broken Notification message causes a Notification
439                  * message with Error Code set to Notification to be
440                  * sent back.
441                  */
442                 next_tvb = tvb_new_subset_remaining(tvb, *offset);
443                 dissect_msdp(next_tvb, pinfo, tree);
444                 break;
445         case FSM_ERROR:
446         case HOLD_TIMER_EXPIRED:
447         case CEASE:
448                 /* Do nothing. These contain no data */
449                 break;
450         default:
451                 if (tlv_len - 5 > 0)
452                 proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
453                                     "<Unknown data>: %u bytes",
454                                     tlv_len -5);
455                 *offset += tlv_len - 5;
456                 break;
457         }
458
459         return;
460 }
461
462 void
463 proto_register_msdp(void)
464 {
465         static hf_register_info hf[] = {
466                 { &hf_msdp_type,
467                         { "Type",           "msdp.type",
468                         FT_UINT8, BASE_DEC, VALS(msdp_types), 0,
469                         "MSDP TLV type", HFILL }
470                 },
471                 { &hf_msdp_length,
472                         { "Length",           "msdp.length",
473                         FT_UINT16, BASE_DEC, NULL, 0,
474                         "MSDP TLV Length", HFILL }
475                 },
476                 { &hf_msdp_sa_entry_count,
477                         { "Entry Count",           "msdp.sa.entry_count",
478                         FT_UINT8, BASE_DEC, NULL, 0,
479                         "MSDP SA Entry Count", HFILL }
480                 },
481                 { &hf_msdp_sa_rp_addr,
482                         { "RP Address",           "msdp.sa.rp_addr",
483                         FT_IPv4, BASE_NONE, NULL, 0,
484                         "Active source's RP address", HFILL }
485                 },
486                 { &hf_msdp_sa_reserved,
487                         { "Reserved",           "msdp.sa.reserved",
488                         FT_UINT24, BASE_HEX, NULL, 0,
489                         "Transmitted as zeros and ignored by a receiver", HFILL }
490                 },
491                 { &hf_msdp_sa_sprefix_len,
492                         { "Sprefix len",           "msdp.sa.sprefix_len",
493                         FT_UINT8, BASE_DEC, NULL, 0,
494                         "The route prefix length associated with source address", HFILL }
495                 },
496                 { &hf_msdp_sa_group_addr,
497                         { "Group Address",           "msdp.sa.group_addr",
498                         FT_IPv4, BASE_NONE, NULL, 0,
499                         "The group address the active source has sent data to", HFILL }
500                 },
501                 { &hf_msdp_sa_src_addr,
502                         { "Source Address",           "msdp.sa.src_addr",
503                         FT_IPv4, BASE_NONE, NULL, 0,
504                         "The IP address of the active source", HFILL }
505                 },
506                 { &hf_msdp_sa_req_res,
507                         { "Reserved",           "msdp.sa_req.res",
508                         FT_UINT8, BASE_HEX, NULL, 0,
509                         "Transmitted as zeros and ignored by a receiver", HFILL }
510                 },
511                 { &hf_msdp_sa_req_group,
512                         { "Group Address",           "msdp.sa_req.group_addr",
513                         FT_IPv4, BASE_NONE, NULL, 0,
514                         "The group address the MSDP peer is requesting", HFILL }
515                 },
516                 { &hf_msdp_not_o,
517                         { "Open-bit",           "msdp.not.o",
518                         FT_UINT8, BASE_HEX, NULL, 0x80,
519                         "If clear, the connection will be closed", HFILL }
520                 },
521                 { &hf_msdp_not_error,
522                         { "Error Code",           "msdp.not.error",
523                         FT_UINT8, BASE_DEC, VALS(error_vals), 0x7F,
524                         "Indicates the type of Notification", HFILL }
525                 },
526                 { &hf_msdp_not_error_sub,
527                         { "Error subode",           "msdp.not.error_sub",
528                         FT_UINT8, BASE_DEC, NULL, 0,
529                         "Error subcode", HFILL }
530                 },
531                 { &hf_msdp_not_ipv4,
532                         { "IPv4 address",           "msdp.not.ipv4",
533                         FT_IPv4, BASE_NONE, NULL, 0,
534                         "Group/RP/Source address in Notification messages", HFILL }
535                 },
536                 { &hf_msdp_not_res,
537                         { "Reserved",           "msdp.not.res",
538                         FT_UINT24, BASE_HEX, NULL, 0,
539                         "Reserved field in Notification messages", HFILL }
540                 },
541                 { &hf_msdp_not_entry_count,
542                         { "Entry Count",           "msdp.not.entry_count",
543                         FT_UINT24, BASE_HEX, NULL, 0,
544                         "Entry Count in Notification messages", HFILL }
545                 },
546                 { &hf_msdp_not_sprefix_len,
547                         { "Sprefix len",           "msdp.not.sprefix_len",
548                         FT_UINT8, BASE_DEC, NULL, 0,
549                         "Source prefix length in Notification messages", HFILL }
550                 },
551         };
552
553         static gint *ett[] = {
554                 &ett_msdp,
555                 &ett_msdp_sa_entry,
556                 &ett_msdp_sa_enc_data,
557                 &ett_msdp_not_data,
558         };
559
560         proto_msdp = proto_register_protocol("Multicast Source Discovery Protocol",
561             "MSDP", "msdp");
562
563         proto_register_field_array(proto_msdp, hf, array_length(hf));
564         proto_register_subtree_array(ett, array_length(ett));
565 }
566
567 void
568 proto_reg_handoff_msdp(void)
569 {
570         dissector_handle_t msdp_handle;
571
572         msdp_handle = create_dissector_handle(dissect_msdp, proto_msdp);
573         dissector_add_uint("tcp.port", 639, msdp_handle);
574
575         ip_handle = find_dissector("ip");
576
577         return;
578 }