MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-msdp.c
blobe39eabe8150e17e7fc5aaf29e1ad520f63b6a87d
1 /* packet-msdp.c
2 * Routines for Multicast Source Discovery Protocol (MSDP) dissection.
3 * draft-ietf-msdp-spec-10.txt
5 * Copyright 2001, Heikki Vatiainen <hessu@cs.tut.fi>
7 * $Id$
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
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.
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.
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.
28 #include "config.h"
30 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/to_str.h>
35 /* MSDP message types. The messages are TLV (Type-Length-Value) encoded */
36 enum { MSDP_SA = 1,
37 MSDP_SA_REQ,
38 MSDP_SA_RSP,
39 MSDP_KEEP_ALIVE,
40 MSDP_NOTIFICATION,
42 /* Theses are only assigned in MSDP spec. Their use is specifed
43 * elsewhere */
44 MSDP_TRACE_IN_PROGRESS,
45 MSDP_TRACE_REPLY
48 static const value_string msdp_types[] = {
49 { MSDP_SA, "IPv4 Source-Active" },
50 { MSDP_SA_REQ, "IPv4 Source-Active Request" },
51 { MSDP_SA_RSP, "IPv4 Source-Active Response" },
52 { MSDP_KEEP_ALIVE, "KeepAlive" },
53 { MSDP_NOTIFICATION, "Notification" },
55 { MSDP_TRACE_IN_PROGRESS, "MSDP traceroute in progress" },
56 { MSDP_TRACE_REPLY, "MSDP traceroute reply" },
57 { 0, NULL },
61 /* Error codes */
62 enum { MESSAGE_HEADER_ERROR = 1,
63 SA_REQUEST_ERROR,
64 SA_MESSAGE_SA_RESPONSE_ERROR,
65 HOLD_TIMER_EXPIRED,
66 FSM_ERROR,
67 NOTIFICATION,
68 CEASE
71 static const value_string error_vals[] = {
72 { MESSAGE_HEADER_ERROR, "Message Header Error" },
73 { SA_REQUEST_ERROR, "SA-Request Error" },
74 { SA_MESSAGE_SA_RESPONSE_ERROR, "SA-Message/SA-Response Error" },
75 { HOLD_TIMER_EXPIRED, "Hold Timer Expired" },
76 { FSM_ERROR, "Finite State Machine Error" },
77 { NOTIFICATION, "Notification" },
78 { CEASE, "Cease" },
79 { 0, NULL },
83 /* Message Header Error subcodes */
84 static const value_string hdr_error_vals[] = {
85 { 0, "Unspecific" },
86 { 2, "Bad Message Length" },
87 { 3, "Bad Message Type" },
88 { 0, NULL },
91 /* SA-Request Error subcodes (the O-bit is always clear) */
92 static const value_string sa_req_error_vals[] = {
93 { 0, "Unspecific" },
94 { 1, "Invalid Group" },
95 { 0, NULL },
98 /* SA-Message/SA-Response Error subcodes */
99 static const value_string sa_msg_error_vals[] = {
100 { 0, "Unspecific" },
101 { 1, "Invalid Entry Count" },
102 { 2, "Invalid RP Address" },
103 { 3, "Invalid Group Address" },
104 { 4, "Invalid Source Address" },
105 { 5, "Invalid Sprefix Length" },
106 { 6, "Looping SA (Self is RP)" },
107 { 7, "Unknown Encapsulation" },
108 { 8, "Administrative Scope Boundary Violated" },
109 { 0, NULL },
112 /* Finite State Machine Error subcodes (the O-bit is always clear) */
113 static const value_string fsm_error_vals[] = {
114 { 0, "Unspecific" },
115 { 1, "Unexpected Message Type FSM Error" },
116 { 0, NULL },
120 * Hold Timer Expired subcodes (the O-bit is always clear):
121 * Notification subcodes (the O-bit is always clear):
122 * Cease subcodes (the O-bit is always clear):
124 * These have only "Unspecific" specified.
126 static const value_string sa_unspec_error_vals[] = {
127 { 0, "Unspecific" },
128 { 0, NULL },
132 /* Initialize the protocol and registered fields */
133 static int proto_msdp = -1;
134 static int hf_msdp_type = -1;
135 static int hf_msdp_length = -1;
137 static int hf_msdp_sa_entry_count = -1;
138 static int hf_msdp_sa_rp_addr = -1;
139 static int hf_msdp_sa_reserved = -1;
140 static int hf_msdp_sa_sprefix_len = -1;
141 static int hf_msdp_sa_group_addr = -1;
142 static int hf_msdp_sa_src_addr = -1;
144 static int hf_msdp_sa_req_res = -1;
145 static int hf_msdp_sa_req_group = -1;
147 static int hf_msdp_not_o = -1;
148 static int hf_msdp_not_error = -1;
149 static int hf_msdp_not_error_sub = -1;
151 static int hf_msdp_not_group_address = -1;
152 static int hf_msdp_not_rp_address = -1;
153 static int hf_msdp_not_source_address = -1;
154 static int hf_msdp_not_res = -1;
155 static int hf_msdp_not_entry_count = -1;
156 static int hf_msdp_not_sprefix_len = -1;
159 static gint ett_msdp = -1;
160 static gint ett_msdp_sa_entry = -1;
161 static gint ett_msdp_sa_enc_data = -1;
162 static gint ett_msdp_not_data = -1;
165 static dissector_handle_t ip_handle;
168 static void
169 dissect_msdp_sa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
170 int *offset, int len);
171 static void
172 dissect_msdp_notification(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, guint16 tlv_len);
175 static void
176 dissect_msdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
178 proto_item *ti;
179 proto_tree *msdp_tree;
180 int offset;
181 guint8 type;
182 guint16 length;
185 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSDP");
187 col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(tvb_get_guint8(tvb, 0),
188 msdp_types,
189 "<Unknown MSDP message type>"));
191 ti = proto_tree_add_item(tree, proto_msdp, tvb, 0, -1, ENC_NA);
192 msdp_tree = proto_item_add_subtree(ti, ett_msdp);
194 offset = 0;
195 while (tvb_reported_length_remaining(tvb, offset) >= 3) {
196 type = tvb_get_guint8(tvb, offset);
197 length = tvb_get_ntohs(tvb, offset + 1);
198 if (length < 3)
199 break;
200 proto_tree_add_uint(msdp_tree, hf_msdp_type, tvb, offset, 1, type);
201 proto_tree_add_uint(msdp_tree, hf_msdp_length, tvb, offset + 1, 2, length);
202 offset += 3;
203 length -= 3;
205 switch (type) {
206 case MSDP_SA:
207 case MSDP_SA_RSP:
208 dissect_msdp_sa(tvb, pinfo, msdp_tree, &offset,
209 length);
210 break;
211 case MSDP_SA_REQ:
212 proto_tree_add_item(msdp_tree, hf_msdp_sa_req_res, tvb, offset, 1, ENC_BIG_ENDIAN);
213 proto_tree_add_item(msdp_tree, hf_msdp_sa_req_group, tvb, offset + 1, 4, ENC_BIG_ENDIAN);
214 offset += 5;
215 break;
216 case MSDP_NOTIFICATION:
217 dissect_msdp_notification(tvb, pinfo, msdp_tree, &offset, length);
218 break;
219 default:
220 if (length > 0)
221 proto_tree_add_text(msdp_tree, tvb, offset, length, "TLV contents");
222 offset += length;
223 break;
227 if (tvb_length_remaining(tvb, offset) > 0)
228 proto_tree_add_text(msdp_tree, tvb, offset,
229 -1, "Trailing junk");
231 return;
234 /* Both Source-Active and Source-Active Response have the same format
235 * with one exception. Encapsulated multicast data is not allowed in
236 * SA Response.
238 static void dissect_msdp_sa(tvbuff_t *tvb, packet_info *pinfo,
239 proto_tree *tree, int *offset, int length)
241 guint8 entries;
243 if (length < 1)
244 return;
245 entries = tvb_get_guint8(tvb, *offset);
246 proto_tree_add_uint(tree, hf_msdp_sa_entry_count, tvb, *offset, 1, entries);
247 *offset += 1;
248 length -= 1;
250 if (length < 4) {
251 *offset += length;
252 return;
254 proto_tree_add_item(tree, hf_msdp_sa_rp_addr, tvb, *offset, 4, ENC_BIG_ENDIAN);
255 *offset += 4;
256 length -= 4;
258 /* Put each of the (S,G) entries in their own subtree.
259 * This is probably visually better.
261 while (entries-- > 0) {
262 proto_item *ei;
263 proto_tree *entry_tree;
265 if (length < 12) {
266 *offset += length;
267 return;
269 ei = proto_tree_add_text(tree, tvb, *offset, 12, "(S,G) block: %s/%u -> %s",
270 tvb_ip_to_str(tvb, *offset + 8),
271 tvb_get_guint8(tvb, *offset + 3),
272 tvb_ip_to_str(tvb, *offset + 4));
273 entry_tree = proto_item_add_subtree(ei, ett_msdp_sa_entry);
275 proto_tree_add_item(entry_tree, hf_msdp_sa_reserved, tvb, *offset, 3, ENC_BIG_ENDIAN);
276 *offset += 3;
277 length -= 3;
278 proto_tree_add_item(entry_tree, hf_msdp_sa_sprefix_len, tvb, *offset, 1, ENC_BIG_ENDIAN);
279 *offset += 1;
280 length -= 1;
281 proto_tree_add_item(entry_tree, hf_msdp_sa_group_addr, tvb, *offset, 4, ENC_BIG_ENDIAN);
282 *offset += 4;
283 length -= 4;
284 proto_tree_add_item(entry_tree, hf_msdp_sa_src_addr, tvb, *offset, 4, ENC_BIG_ENDIAN);
285 *offset += 4;
286 length -= 4;
290 * Check if an encapsulated multicast IPv4 packet follows
292 if (length > 0) {
293 proto_item *ei;
294 proto_tree *enc_tree;
295 gint available_length, reported_length;
296 tvbuff_t *next_tvb;
298 ei = proto_tree_add_text(tree, tvb, *offset, length,
299 "Encapsulated IPv4 packet: %u bytes",
300 length);
301 enc_tree = proto_item_add_subtree(ei, ett_msdp_sa_enc_data);
303 available_length = tvb_length_remaining(tvb, *offset);
304 reported_length = tvb_reported_length_remaining(tvb, *offset);
305 DISSECTOR_ASSERT(available_length >= 0);
306 DISSECTOR_ASSERT(reported_length >= 0);
307 if (available_length > reported_length)
308 available_length = reported_length;
309 if (available_length > length)
310 available_length = length;
311 if (reported_length > length)
312 reported_length = length;
314 next_tvb = tvb_new_subset(tvb, *offset, available_length,
315 reported_length);
316 /* Set the information columns read-only so that they
317 * reflect the MSDP packet rather than the
318 * encapsulated packet.
320 col_set_writable(pinfo->cinfo, FALSE);
321 call_dissector(ip_handle, next_tvb, pinfo, enc_tree);
323 *offset += length;
325 return;
328 /* Note: updates *offset */
329 static void add_notification_data_ipv4addr(tvbuff_t *tvb, proto_tree *tree, int *offset, int hf_addr)
331 proto_tree_add_item(tree, hf_msdp_not_res, tvb, *offset, 3, ENC_BIG_ENDIAN);
332 *offset += 3;
333 proto_tree_add_item(tree, hf_addr, tvb, *offset, 4, ENC_BIG_ENDIAN);
334 *offset += 4;
336 return;
339 static void dissect_msdp_notification(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, guint16 tlv_len)
341 guint8 error, error_sub;
342 const value_string *vals;
344 proto_tree_add_item(tree, hf_msdp_not_o, tvb, *offset, 1, ENC_BIG_ENDIAN);
345 proto_tree_add_item(tree, hf_msdp_not_error, tvb, *offset, 1, ENC_BIG_ENDIAN);
346 error = tvb_get_guint8(tvb, *offset);
347 error &= 0x7F; /* Error is 7-bit field. O-bit is bit 8 */
348 *offset += 1;
350 /* Depending on the Error Code, we collect the correct
351 * value_strings for the Error subcode
353 switch (error) {
354 case MESSAGE_HEADER_ERROR:
355 vals = hdr_error_vals;
356 break;
357 case SA_REQUEST_ERROR:
358 vals = sa_req_error_vals;
359 break;
360 case SA_MESSAGE_SA_RESPONSE_ERROR:
361 vals = sa_msg_error_vals;
362 break;
363 case FSM_ERROR:
364 vals = fsm_error_vals;
365 break;
366 case HOLD_TIMER_EXPIRED:
367 case NOTIFICATION:
368 case CEASE:
369 vals = sa_unspec_error_vals;
370 break;
371 default:
372 vals = sa_unspec_error_vals;
373 break;
376 error_sub = tvb_get_guint8(tvb, *offset);
377 proto_tree_add_uint_format_value(tree, hf_msdp_not_error_sub, tvb, *offset, 1,
378 error_sub, "%s (%u)",
379 val_to_str_const(error_sub, vals, "<Unknown Error subcode>"),
380 error_sub);
381 *offset += 1;
383 /* Do switch again, this time to dissect the data portion
384 * correctly. Ugly.
386 switch (error) {
387 tvbuff_t *next_tvb;
388 case SA_REQUEST_ERROR:
389 add_notification_data_ipv4addr(tvb, tree, offset, hf_msdp_not_group_address);
390 break;
391 case SA_MESSAGE_SA_RESPONSE_ERROR:
392 if (error_sub == 0) {
393 break;
394 } else if (error_sub == 1) {
395 proto_tree_add_item(tree, hf_msdp_not_entry_count, tvb, *offset, 1, ENC_BIG_ENDIAN);
396 *offset += 1;
397 break;
398 } else if (error_sub == 2) {
399 add_notification_data_ipv4addr(tvb, tree, offset, hf_msdp_not_rp_address);
400 break;
401 } else if (error_sub == 3 || error_sub == 8) {
402 add_notification_data_ipv4addr(tvb, tree, offset, hf_msdp_not_group_address);
403 break;
404 } else if (error_sub == 4) {
405 add_notification_data_ipv4addr(tvb, tree, offset, hf_msdp_not_source_address);
406 break;
407 } else if (error_sub == 5) {
408 proto_tree_add_item(tree, hf_msdp_not_sprefix_len, tvb, *offset, 1, ENC_BIG_ENDIAN);
409 *offset += 1;
410 break;
411 } else if (error_sub == 6) {
412 /* No break, causes fall through to next label */
413 } else if (error_sub == 7) {
414 proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
415 "Packet with unknown encapsulation: %u bytes",
416 tlv_len - 5);
417 *offset += tlv_len - 5;
418 break;
419 } else {
420 proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
421 "<Unknown data>: %u bytes",
422 tlv_len -5);
423 *offset += tlv_len - 5;
424 break;
426 /* Fall through */
427 case MESSAGE_HEADER_ERROR:
428 case NOTIFICATION:
429 /* Data contains the message that had an error. Even a
430 * broken Notification message causes a Notification
431 * message with Error Code set to Notification to be
432 * sent back.
434 next_tvb = tvb_new_subset_remaining(tvb, *offset);
435 dissect_msdp(next_tvb, pinfo, tree);
436 break;
437 case FSM_ERROR:
438 case HOLD_TIMER_EXPIRED:
439 case CEASE:
440 /* Do nothing. These contain no data */
441 break;
442 default:
443 if (tlv_len - 5 > 0)
444 proto_tree_add_text(tree, tvb, *offset, tlv_len - 5,
445 "<Unknown data>: %u bytes",
446 tlv_len -5);
447 *offset += tlv_len - 5;
448 break;
451 return;
454 void
455 proto_register_msdp(void)
457 static hf_register_info hf[] = {
458 { &hf_msdp_type,
459 { "Type", "msdp.type",
460 FT_UINT8, BASE_DEC, VALS(msdp_types), 0,
461 "MSDP TLV type", HFILL }
463 { &hf_msdp_length,
464 { "Length", "msdp.length",
465 FT_UINT16, BASE_DEC, NULL, 0,
466 "MSDP TLV Length", HFILL }
468 { &hf_msdp_sa_entry_count,
469 { "Entry Count", "msdp.sa.entry_count",
470 FT_UINT8, BASE_DEC, NULL, 0,
471 "MSDP SA Entry Count", HFILL }
473 { &hf_msdp_sa_rp_addr,
474 { "RP Address", "msdp.sa.rp_addr",
475 FT_IPv4, BASE_NONE, NULL, 0,
476 "Active source's RP address", HFILL }
478 { &hf_msdp_sa_reserved,
479 { "Reserved", "msdp.sa.reserved",
480 FT_UINT24, BASE_HEX, NULL, 0,
481 "Transmitted as zeros and ignored by a receiver", HFILL }
483 { &hf_msdp_sa_sprefix_len,
484 { "Sprefix len", "msdp.sa.sprefix_len",
485 FT_UINT8, BASE_DEC, NULL, 0,
486 "The route prefix length associated with source address", HFILL }
488 { &hf_msdp_sa_group_addr,
489 { "Group Address", "msdp.sa.group_addr",
490 FT_IPv4, BASE_NONE, NULL, 0,
491 "The group address the active source has sent data to", HFILL }
493 { &hf_msdp_sa_src_addr,
494 { "Source Address", "msdp.sa.src_addr",
495 FT_IPv4, BASE_NONE, NULL, 0,
496 "The IP address of the active source", HFILL }
498 { &hf_msdp_sa_req_res,
499 { "Reserved", "msdp.sa_req.res",
500 FT_UINT8, BASE_HEX, NULL, 0,
501 "Transmitted as zeros and ignored by a receiver", HFILL }
503 { &hf_msdp_sa_req_group,
504 { "Group Address", "msdp.sa_req.group_addr",
505 FT_IPv4, BASE_NONE, NULL, 0,
506 "The group address the MSDP peer is requesting", HFILL }
508 { &hf_msdp_not_o,
509 { "Open-bit", "msdp.not.o",
510 FT_UINT8, BASE_HEX, NULL, 0x80,
511 "If clear, the connection will be closed", HFILL }
513 { &hf_msdp_not_error,
514 { "Error Code", "msdp.not.error",
515 FT_UINT8, BASE_DEC, VALS(error_vals), 0x7F,
516 "Indicates the type of Notification", HFILL }
518 { &hf_msdp_not_error_sub,
519 { "Error subcode", "msdp.not.error_sub",
520 FT_UINT8, BASE_DEC, NULL, 0,
521 NULL, HFILL }
523 { &hf_msdp_not_group_address,
524 { "Group address", "msdp.not.group_address",
525 FT_IPv4, BASE_NONE, NULL, 0,
526 "Group address in Notification messages", HFILL }
528 { &hf_msdp_not_rp_address,
529 { "RP address", "msdp.not.rp_address",
530 FT_IPv4, BASE_NONE, NULL, 0,
531 "RP address in Notification messages", HFILL }
533 { &hf_msdp_not_source_address,
534 { "Source address", "msdp.not.source_address",
535 FT_IPv4, BASE_NONE, NULL, 0,
536 "Source address in Notification messages", HFILL }
538 { &hf_msdp_not_res,
539 { "Reserved", "msdp.not.res",
540 FT_UINT24, BASE_HEX, NULL, 0,
541 "Reserved field in Notification messages", HFILL }
543 { &hf_msdp_not_entry_count,
544 { "Entry Count", "msdp.not.entry_count",
545 FT_UINT24, BASE_HEX, NULL, 0,
546 "Entry Count in Notification messages", HFILL }
548 { &hf_msdp_not_sprefix_len,
549 { "Sprefix len", "msdp.not.sprefix_len",
550 FT_UINT8, BASE_DEC, NULL, 0,
551 "Source prefix length in Notification messages", HFILL }
555 static gint *ett[] = {
556 &ett_msdp,
557 &ett_msdp_sa_entry,
558 &ett_msdp_sa_enc_data,
559 &ett_msdp_not_data,
562 proto_msdp = proto_register_protocol("Multicast Source Discovery Protocol",
563 "MSDP", "msdp");
565 proto_register_field_array(proto_msdp, hf, array_length(hf));
566 proto_register_subtree_array(ett, array_length(ett));
569 void
570 proto_reg_handoff_msdp(void)
572 dissector_handle_t msdp_handle;
574 msdp_handle = create_dissector_handle(dissect_msdp, proto_msdp);
575 dissector_add_uint("tcp.port", 639, msdp_handle);
577 ip_handle = find_dissector("ip");
579 return;