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