1/*
2 * Copyright (c) 1998-2007 The TCPDUMP project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
16 */
17
18/* \summary: Cisco UniDirectional Link Detection (UDLD) protocol printer */
19
20/* specification: RFC 5171 */
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#include "netdissect-stdinc.h"
27
28#define ND_LONGJMP_FROM_TCHECK
29#include "netdissect.h"
30#include "extract.h"
31
32
33#define UDLD_HEADER_LEN			4
34#define UDLD_TLV_HEADER_LEN		4
35#define UDLD_DEVICE_ID_TLV		0x0001
36#define UDLD_PORT_ID_TLV		0x0002
37#define UDLD_ECHO_TLV			0x0003
38#define UDLD_MESSAGE_INTERVAL_TLV	0x0004
39#define UDLD_TIMEOUT_INTERVAL_TLV	0x0005
40#define UDLD_DEVICE_NAME_TLV		0x0006
41#define UDLD_SEQ_NUMBER_TLV		0x0007
42
43static const struct tok udld_tlv_values[] = {
44    { UDLD_DEVICE_ID_TLV, "Device-ID TLV"},
45    { UDLD_PORT_ID_TLV, "Port-ID TLV"},
46    { UDLD_ECHO_TLV, "Echo TLV"},
47    { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"},
48    { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"},
49    { UDLD_DEVICE_NAME_TLV, "Device Name TLV"},
50    { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"},
51    { 0, NULL}
52};
53
54static const struct tok udld_code_values[] = {
55    { 0x00, "Reserved"},
56    { 0x01, "Probe message"},
57    { 0x02, "Echo message"},
58    { 0x03, "Flush message"},
59    { 0, NULL}
60};
61
62static const struct tok udld_flags_bitmap_str[] = {
63    { 1U << 0, "RT"    },
64    { 1U << 1, "RSY"   },
65    { 1U << 2, "MBZ-2" },
66    { 1U << 3, "MBZ-3" },
67    { 1U << 4, "MBZ-4" },
68    { 1U << 5, "MBZ-5" },
69    { 1U << 6, "MBZ-6" },
70    { 1U << 7, "MBZ-7" },
71    { 0, NULL}
72};
73
74/*
75 * UDLD's Protocol Data Unit format:
76 *
77 *  0                   1                   2                   3
78 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
79 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 * | Ver | Opcode  |     Flags     |           Checksum            |
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 * |               List of TLVs (variable length list)             |
83 * |                              ...                              |
84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 *
86 * TLV format:
87 *
88 *  0                   1                   2                   3
89 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * |             TYPE              |            LENGTH             |
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 * |                             VALUE                             |
94 * |                              ...                              |
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 *
97 * LENGTH: Length in bytes of the Type, Length, and Value fields.
98 */
99
100#define	UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
101#define	UDLD_EXTRACT_OPCODE(x) ((x)&0x1f)
102
103void
104udld_print(netdissect_options *ndo,
105           const u_char *tptr, u_int length)
106{
107    uint8_t ver, code, flags;
108
109    ndo->ndo_protocol = "udld";
110    if (length < UDLD_HEADER_LEN)
111        goto invalid;
112
113    ver = UDLD_EXTRACT_VERSION(GET_U_1(tptr));
114    code = UDLD_EXTRACT_OPCODE(GET_U_1(tptr));
115    tptr += 1;
116    length -= 1;
117
118    flags = GET_U_1(tptr);
119    tptr += 1;
120    length -= 1;
121
122    ND_PRINT("UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
123           ver,
124           tok2str(udld_code_values, "Reserved", code),
125           code,
126           bittok2str(udld_flags_bitmap_str, "none", flags),
127           flags,
128           length + 2);
129
130    /*
131     * In non-verbose mode, just print version and opcode type
132     */
133    if (ndo->ndo_vflag < 1) {
134        goto tcheck_remainder;
135    }
136
137    ND_PRINT("\n\tChecksum 0x%04x (unverified)", GET_BE_U_2(tptr));
138    tptr += 2;
139    length -= 2;
140
141    while (length) {
142        uint16_t type, len;
143
144        if (length < UDLD_TLV_HEADER_LEN)
145            goto invalid;
146
147	type = GET_BE_U_2(tptr);
148        tptr += 2;
149        length -= 2;
150
151        len  = GET_BE_U_2(tptr);
152        tptr += 2;
153        length -= 2;
154
155        ND_PRINT("\n\t%s (0x%04x) TLV, length %u",
156               tok2str(udld_tlv_values, "Unknown", type),
157               type, len);
158
159        /* infinite loop check */
160        if (len <= UDLD_TLV_HEADER_LEN)
161            goto invalid;
162
163        len -= UDLD_TLV_HEADER_LEN;
164        if (length < len)
165            goto invalid;
166
167        switch (type) {
168        case UDLD_DEVICE_ID_TLV:
169        case UDLD_PORT_ID_TLV:
170        case UDLD_DEVICE_NAME_TLV:
171            ND_PRINT(", ");
172            nd_printjnp(ndo, tptr, len);
173            break;
174
175        case UDLD_ECHO_TLV:
176            ND_PRINT(", ");
177            (void)nd_printn(ndo, tptr, len, NULL);
178            break;
179
180        case UDLD_MESSAGE_INTERVAL_TLV:
181        case UDLD_TIMEOUT_INTERVAL_TLV:
182            if (len != 1)
183                goto invalid;
184            ND_PRINT(", %us", (GET_U_1(tptr)));
185            break;
186
187        case UDLD_SEQ_NUMBER_TLV:
188            if (len != 4)
189                goto invalid;
190            ND_PRINT(", %u", GET_BE_U_4(tptr));
191            break;
192
193        default:
194            ND_TCHECK_LEN(tptr, len);
195            break;
196        }
197        tptr += len;
198        length -= len;
199    }
200
201    return;
202
203invalid:
204    nd_print_invalid(ndo);
205tcheck_remainder:
206    ND_TCHECK_LEN(tptr, length);
207}
208