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