sensitivity of packet range options fine tuning:
[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.8 2003/01/20 06:24:37 guy 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,
173     int *offset, int len);
174 static void
175 dissect_msdp_notification(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, guint16 tlv_len);
176
177
178 static void
179 dissect_msdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
180 {
181         proto_item *ti;
182         proto_tree *msdp_tree;
183         int offset;
184
185         if (check_col(pinfo->cinfo, COL_PROTOCOL))
186                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSDP");
187
188         if (check_col(pinfo->cinfo, COL_INFO))
189                 col_set_str(pinfo->cinfo, COL_INFO, val_to_str(tvb_get_guint8(tvb, 0),
190                                                             msdp_types,
191                                                             "<Unknown MSDP message type>"));
192
193         if (tree) {
194                 guint8 type;
195                 guint16 length;
196
197                 ti = proto_tree_add_item(tree, proto_msdp, tvb, 0, -1, FALSE);
198                 msdp_tree = proto_item_add_subtree(ti, ett_msdp);
199
200                 offset = 0;
201                 while (tvb_reported_length_remaining(tvb, offset) >= 3) {
202                         type = tvb_get_guint8(tvb, offset);
203                         length = tvb_get_ntohs(tvb, offset + 1);
204                         if (length < 3)
205                                 break;
206                         proto_tree_add_uint(msdp_tree, hf_msdp_type, tvb, offset, 1, type);
207                         proto_tree_add_uint(msdp_tree, hf_msdp_length, tvb, offset + 1, 2, length);
208                         offset += 3;
209                         length -= 3;
210
211                         switch (type) {
212                         case MSDP_SA:
213                         case MSDP_SA_RSP:
214                                 dissect_msdp_sa(tvb, pinfo, msdp_tree, &offset,
215                                     length);
216                                 break;
217                         case MSDP_SA_REQ:
218                                 proto_tree_add_item(msdp_tree, hf_msdp_sa_req_res, tvb, offset, 1, FALSE);
219                                 proto_tree_add_item(msdp_tree, hf_msdp_sa_req_group, tvb, offset + 1, 4, FALSE);
220                                 offset += 5;
221                                 break;
222                         case MSDP_NOTIFICATION:
223                                 dissect_msdp_notification(tvb, pinfo, msdp_tree, &offset, length);
224                                 break;
225                         default:
226                                 if (length > 0)
227                                         proto_tree_add_text(msdp_tree, tvb, offset, length, "TLV contents");
228                                 offset += length;
229                                 break;
230                         }
231                 }
232
233                 if (tvb_length_remaining(tvb, offset) > 0)
234                         proto_tree_add_text(msdp_tree, tvb, offset,
235                                             -1, "Trailing junk");
236         }
237
238         return;
239 }
240
241 /* Both Source-Active and Source-Active Response have the same format
242  * with one exception. Encapsulated multicast data is not allowed in
243  * SA Response.
244  */
245 static void dissect_msdp_sa(tvbuff_t *tvb, packet_info *pinfo,
246     proto_tree *tree, int *offset, int length)
247 {
248         guint8 entries;
249         guint32 rp_addr;
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         tvb_memcpy(tvb, (guint8 *)&rp_addr, *offset, 4);
264         proto_tree_add_item(tree, hf_msdp_sa_rp_addr, tvb, *offset, 4, FALSE);
265         *offset += 4;
266         length -= 4;
267
268         /* Put each of the (S,G) entries in their own subtree.
269          * This is probably visually better.
270          */
271         while (entries-- > 0) {
272                 proto_item *ei;
273                 proto_tree *entry_tree;
274
275                 if (length < 12) {
276                         *offset += length;
277                         length = 0;
278                         return;
279                 }
280                 ei = proto_tree_add_text(tree, tvb, *offset, 12, "(S,G) block: %s/%u -> %s",
281                                          ip_to_str(tvb_get_ptr(tvb, *offset + 8, 4)),
282                                          tvb_get_guint8(tvb, *offset + 3),
283                                          ip_to_str(tvb_get_ptr(tvb, *offset + 4, 4)));
284                 entry_tree = proto_item_add_subtree(ei, ett_msdp_sa_entry);
285
286                 proto_tree_add_item(entry_tree, hf_msdp_sa_reserved, tvb, *offset, 3, FALSE);
287                 *offset += 3;
288                 length -= 3;
289                 proto_tree_add_item(entry_tree, hf_msdp_sa_sprefix_len, tvb, *offset, 1, FALSE);
290                 *offset += 1;
291                 length -= 1;
292                 proto_tree_add_item(entry_tree, hf_msdp_sa_group_addr, tvb, *offset, 4, FALSE);
293                 *offset += 4;
294                 length -= 4;
295                 proto_tree_add_item(entry_tree, hf_msdp_sa_src_addr, tvb, *offset, 4, FALSE);
296                 *offset += 4;
297                 length -= 4;
298         }
299
300         /*
301          * Check if an encapsulated multicast IPv4 packet follows
302          */
303         if (length > 0) {
304                 proto_item *ei;
305                 proto_tree *enc_tree;
306                 gint available_length, reported_length;
307                 tvbuff_t *next_tvb;
308
309                 ei = proto_tree_add_text(tree, tvb, *offset, length,
310                                          "Encapsulated IPv4 packet: %u bytes",
311                                          length);
312                 enc_tree = proto_item_add_subtree(ei, ett_msdp_sa_enc_data);
313
314                 available_length = tvb_length_remaining(tvb, *offset);
315                 reported_length = tvb_reported_length_remaining(tvb, *offset);
316                 g_assert(available_length >= 0);
317                 g_assert(reported_length >= 0);
318                 if (available_length > reported_length)
319                         available_length = reported_length;
320                 if (available_length > length)
321                         available_length = length;
322                 if (reported_length > length)
323                         reported_length = length;
324
325                 next_tvb = tvb_new_subset(tvb, *offset, available_length,
326                                           reported_length);
327                 /* Set the information columns read-only so that they
328                  * reflect the MSDP packet rather than the
329                  * encapsulated packet.
330                  */
331                 col_set_writable(pinfo->cinfo, FALSE);
332                 call_dissector(ip_handle, next_tvb, pinfo, enc_tree);
333         }
334         *offset += length;
335
336         return;
337 }
338
339 /* Note: updates *offset */
340 static void add_notification_data_ipv4addr(tvbuff_t *tvb, proto_tree *tree, int *offset, const char *addrtype)
341 {
342         guint32 ipaddr;
343
344         proto_tree_add_item(tree, hf_msdp_not_res, tvb, *offset, 3, FALSE);
345         *offset += 3;
346         tvb_memcpy(tvb, (guint8 *)&ipaddr, *offset, 4);
347         proto_tree_add_ipv4_format(tree, hf_msdp_not_ipv4, tvb, *offset, 4, ipaddr,
348                                    "%s: %s", addrtype, ip_to_str((guint8 *)&ipaddr));
349         *offset += 4;
350
351         return;
352 }
353
354 static void dissect_msdp_notification(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, guint16 tlv_len)
355 {
356         guint8 error, error_sub;
357         const value_string *vals;
358
359         proto_tree_add_item(tree, hf_msdp_not_o, tvb, *offset, 1, FALSE);
360         proto_tree_add_item(tree, hf_msdp_not_error, tvb, *offset, 1, FALSE);
361         error = tvb_get_guint8(tvb, *offset);
362         error &= 0x7F;             /* Error is 7-bit field. O-bit is bit 8 */
363         *offset += 1;
364
365         /* Depending on the Error Code, we collect the correct
366          * value_strings for the Error subcode
367          */
368         switch (error) {
369         case MESSAGE_HEADER_ERROR:
370                 vals = hdr_error_vals;
371                 break;
372         case SA_REQUEST_ERROR:
373                 vals = sa_req_error_vals;
374                 break;
375         case SA_MESSAGE_SA_RESPONSE_ERROR:
376                 vals = sa_msg_error_vals;
377                 break;
378         case FSM_ERROR:
379                 vals = fsm_error_vals;
380                 break;
381         case HOLD_TIMER_EXPIRED:
382         case NOTIFICATION:
383         case CEASE:
384                 vals = sa_unspec_error_vals;
385                 break;
386         default:
387                 vals = sa_unspec_error_vals;
388                 break;
389         }
390
391         error_sub = tvb_get_guint8(tvb, *offset);
392         proto_tree_add_uint_format(tree, hf_msdp_not_error_sub, tvb, *offset, 1,
393                                    error_sub, "Error subcode: %s (%u)",
394                                    val_to_str(error_sub, vals, "<Unknown Error subcode>"),
395                                    error_sub);
396         *offset += 1;
397
398         /* Do switch again, this time to dissect the data portion
399          * correctly. Ugly.
400          */
401         switch (error) {
402                 tvbuff_t *next_tvb;
403         case SA_REQUEST_ERROR:
404                 add_notification_data_ipv4addr(tvb, tree, offset, "Group address");
405                 break;
406         case SA_MESSAGE_SA_RESPONSE_ERROR:
407                 if (error_sub == 0) {
408                         break;
409                 } else if (error_sub == 1) {
410                         proto_tree_add_item(tree, hf_msdp_not_entry_count, tvb, *offset, 1, FALSE);
411                         *offset += 1;
412                         break;
413                 } else if (error_sub == 2) {
414                         add_notification_data_ipv4addr(tvb, tree, offset, "RP address");
415                         break;
416                 } else if (error_sub == 3 || error_sub == 8) {
417                         add_notification_data_ipv4addr(tvb, tree, offset, "Group address");
418                         break;
419                 } else if (error_sub == 4) {
420                         add_notification_data_ipv4addr(tvb, tree, offset, "Source address");
421                         break;
422                 } else if (error_sub == 5) {
423                         proto_tree_add_item(tree, hf_msdp_not_sprefix_len, tvb, *offset, 1, FALSE);
424                         *offset += 1;
425                         break;
426                 } else if (error_sub == 6) {
427                         /* No break, causes fall through to next label */
428                 } else if (error_sub == 7) {
429                         proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
430                                             "Packet with unknown encapsulation: %u bytes",
431                                             tlv_len - 5);
432                         *offset += tlv_len - 5;
433                         break;
434                 } else {
435                         proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
436                                             "<Unknown data>: %u bytes",
437                                             tlv_len -5);
438                         *offset += tlv_len - 5;
439                         break;
440                 }
441                 /* Fall through */
442         case MESSAGE_HEADER_ERROR:
443         case NOTIFICATION:
444                 /* Data contains the message that had an error. Even a
445                  * broken Notification message causes a Notification
446                  * message with Error Code set to Notification to be
447                  * sent back.
448                  */
449                 next_tvb = tvb_new_subset(tvb, *offset, -1, -1);
450                 dissect_msdp(next_tvb, pinfo, tree);
451                 break;
452         case FSM_ERROR:
453         case HOLD_TIMER_EXPIRED:
454         case CEASE:
455                 /* Do nothing. These contain no data */
456                 break;
457         default:
458                 if (tlv_len - 5 > 0)
459                 proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
460                                     "<Unknown data>: %u bytes",
461                                     tlv_len -5);
462                 *offset += tlv_len - 5;
463                 break;
464         }
465
466         return;
467 }
468
469 void
470 proto_register_msdp(void)
471 {
472         static hf_register_info hf[] = {
473                 { &hf_msdp_type,
474                         { "Type",           "msdp.type",
475                         FT_UINT8, BASE_DEC, VALS(msdp_types), 0,
476                         "MSDP TLV type", HFILL }
477                 },
478                 { &hf_msdp_length,
479                         { "Length",           "msdp.length",
480                         FT_UINT16, BASE_DEC, NULL, 0,
481                         "MSDP TLV Length", HFILL }
482                 },
483                 { &hf_msdp_sa_entry_count,
484                         { "Entry Count",           "msdp.sa.entry_count",
485                         FT_UINT8, BASE_DEC, NULL, 0,
486                         "MSDP SA Entry Count", HFILL }
487                 },
488                 { &hf_msdp_sa_rp_addr,
489                         { "RP Address",           "msdp.sa.rp_addr",
490                         FT_IPv4, 0, NULL, 0,
491                         "Active source's RP address", HFILL }
492                 },
493                 { &hf_msdp_sa_reserved,
494                         { "Reserved",           "msdp.sa.reserved",
495                         FT_UINT24, BASE_HEX, NULL, 0,
496                         "Transmitted as zeros and ignored by a receiver", HFILL }
497                 },
498                 { &hf_msdp_sa_sprefix_len,
499                         { "Sprefix len",           "msdp.sa.sprefix_len",
500                         FT_UINT8, BASE_DEC, NULL, 0,
501                         "The route prefix length associated with source address", HFILL }
502                 },
503                 { &hf_msdp_sa_group_addr,
504                         { "Group Address",           "msdp.sa.group_addr",
505                         FT_IPv4, 0, NULL, 0,
506                         "The group address the active source has sent data to", HFILL }
507                 },
508                 { &hf_msdp_sa_src_addr,
509                         { "Source Address",           "msdp.sa.src_addr",
510                         FT_IPv4, 0, NULL, 0,
511                         "The IP address of the active source", HFILL }
512                 },
513                 { &hf_msdp_sa_req_res,
514                         { "Reserved",           "msdp.sa_req.res",
515                         FT_UINT8, BASE_HEX, NULL, 0,
516                         "Transmitted as zeros and ignored by a receiver", HFILL }
517                 },
518                 { &hf_msdp_sa_req_group,
519                         { "Group Address",           "msdp.sa_req.group_addr",
520                         FT_IPv4, 0, NULL, 0,
521                         "The group address the MSDP peer is requesting", HFILL }
522                 },
523                 { &hf_msdp_not_o,
524                         { "Open-bit",           "msdp.not.o",
525                         FT_UINT8, BASE_HEX, NULL, 0x80,
526                         "If clear, the connection will be closed", HFILL }
527                 },
528                 { &hf_msdp_not_error,
529                         { "Error Code",           "msdp.not.error",
530                         FT_UINT8, BASE_DEC, VALS(error_vals), 0x7F,
531                         "Indicates the type of Notification", HFILL }
532                 },
533                 { &hf_msdp_not_error_sub,
534                         { "Error subode",           "msdp.not.error_sub",
535                         FT_UINT8, BASE_DEC, NULL, 0,
536                         "Error subcode", HFILL }
537                 },
538                 { &hf_msdp_not_ipv4,
539                         { "IPv4 address",           "msdp.not.ipv4",
540                         FT_IPv4, 0, NULL, 0,
541                         "Group/RP/Source address in Notification messages", HFILL }
542                 },
543                 { &hf_msdp_not_res,
544                         { "Reserved",           "msdp.not.res",
545                         FT_UINT24, BASE_HEX, NULL, 0,
546                         "Reserved field in Notification messages", HFILL }
547                 },
548                 { &hf_msdp_not_entry_count,
549                         { "Entry Count",           "msdp.not.entry_count",
550                         FT_UINT24, BASE_HEX, NULL, 0,
551                         "Entry Count in Notification messages", HFILL }
552                 },
553                 { &hf_msdp_not_sprefix_len,
554                         { "Sprefix len",           "msdp.not.sprefix_len",
555                         FT_UINT8, BASE_DEC, NULL, 0,
556                         "Source prefix length in Notification messages", HFILL }
557                 },
558         };
559
560         static gint *ett[] = {
561                 &ett_msdp,
562                 &ett_msdp_sa_entry,
563                 &ett_msdp_sa_enc_data,
564                 &ett_msdp_not_data,
565         };
566
567         proto_msdp = proto_register_protocol("Multicast Source Discovery Protocol",
568             "MSDP", "msdp");
569
570         proto_register_field_array(proto_msdp, hf, array_length(hf));
571         proto_register_subtree_array(ett, array_length(ett));
572 }
573
574 void
575 proto_reg_handoff_msdp(void)
576 {
577         dissector_handle_t msdp_handle;
578
579         msdp_handle = create_dissector_handle(dissect_msdp, proto_msdp);
580         dissector_add("tcp.port", 639, msdp_handle);
581
582         ip_handle = find_dissector("ip");
583
584         return;
585 }