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