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