JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 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 | * |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 15 | * Original code by Carles Kishimoto <carles.kishimoto@gmail.com> |
| 16 | */ |
| 17 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 18 | /* \summary: Cisco UniDirectional Link Detection (UDLD) protocol printer */ |
| 19 | |
| 20 | /* specification: RFC 5171 */ |
| 21 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 22 | #ifdef HAVE_CONFIG_H |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 23 | #include <config.h> |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 24 | #endif |
| 25 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 26 | #include "netdissect-stdinc.h" |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 28 | #define ND_LONGJMP_FROM_TCHECK |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 29 | #include "netdissect.h" |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 30 | #include "extract.h" |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 31 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 32 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 33 | #define UDLD_HEADER_LEN 4 |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 34 | #define UDLD_TLV_HEADER_LEN 4 |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 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 | |
| 43 | static 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 | |
| 54 | static 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 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 62 | static 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" }, |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 71 | { 0, NULL} |
| 72 | }; |
| 73 | |
| 74 | /* |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 75 | * UDLD's Protocol Data Unit format: |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 76 | * |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 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 |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 79 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 80 | * | Ver | Opcode | Flags | Checksum | |
| 81 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 82 | * | List of TLVs (variable length list) | |
| 83 | * | ... | |
| 84 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 85 | * |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 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. |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 98 | */ |
| 99 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 100 | #define UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5) |
| 101 | #define UDLD_EXTRACT_OPCODE(x) ((x)&0x1f) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 102 | |
| 103 | void |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 104 | udld_print(netdissect_options *ndo, |
| 105 | const u_char *tptr, u_int length) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 106 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 107 | uint8_t ver, code, flags; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 108 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 109 | ndo->ndo_protocol = "udld"; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 110 | if (length < UDLD_HEADER_LEN) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 111 | goto invalid; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 112 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 113 | ver = UDLD_EXTRACT_VERSION(GET_U_1(tptr)); |
| 114 | code = UDLD_EXTRACT_OPCODE(GET_U_1(tptr)); |
| 115 | tptr += 1; |
| 116 | length -= 1; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 117 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 118 | flags = GET_U_1(tptr); |
| 119 | tptr += 1; |
| 120 | length -= 1; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 121 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 122 | ND_PRINT("UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u", |
| 123 | ver, |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 124 | tok2str(udld_code_values, "Reserved", code), |
| 125 | code, |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 126 | bittok2str(udld_flags_bitmap_str, "none", flags), |
| 127 | flags, |
| 128 | length + 2); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * In non-verbose mode, just print version and opcode type |
| 132 | */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 133 | if (ndo->ndo_vflag < 1) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 134 | goto tcheck_remainder; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 137 | ND_PRINT("\n\tChecksum 0x%04x (unverified)", GET_BE_U_2(tptr)); |
| 138 | tptr += 2; |
| 139 | length -= 2; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 140 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 141 | while (length) { |
| 142 | uint16_t type, len; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 143 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 144 | if (length < UDLD_TLV_HEADER_LEN) |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 145 | goto invalid; |
| 146 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 159 | /* infinite loop check */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 160 | if (len <= UDLD_TLV_HEADER_LEN) |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 161 | goto invalid; |
| 162 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 163 | len -= UDLD_TLV_HEADER_LEN; |
| 164 | if (length < len) |
| 165 | goto invalid; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 166 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 167 | switch (type) { |
| 168 | case UDLD_DEVICE_ID_TLV: |
| 169 | case UDLD_PORT_ID_TLV: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 170 | case UDLD_DEVICE_NAME_TLV: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 171 | ND_PRINT(", "); |
| 172 | nd_printjnp(ndo, tptr, len); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 173 | break; |
| 174 | |
| 175 | case UDLD_ECHO_TLV: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 176 | ND_PRINT(", "); |
| 177 | (void)nd_printn(ndo, tptr, len, NULL); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 178 | break; |
| 179 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 180 | case UDLD_MESSAGE_INTERVAL_TLV: |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 181 | case UDLD_TIMEOUT_INTERVAL_TLV: |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 182 | if (len != 1) |
| 183 | goto invalid; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 184 | ND_PRINT(", %us", (GET_U_1(tptr))); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 185 | break; |
| 186 | |
| 187 | case UDLD_SEQ_NUMBER_TLV: |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 188 | if (len != 4) |
| 189 | goto invalid; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 190 | ND_PRINT(", %u", GET_BE_U_4(tptr)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 191 | break; |
| 192 | |
| 193 | default: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 194 | ND_TCHECK_LEN(tptr, len); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 195 | break; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 196 | } |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 197 | tptr += len; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 198 | length -= len; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return; |
| 202 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 203 | invalid: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 204 | nd_print_invalid(ndo); |
| 205 | tcheck_remainder: |
| 206 | ND_TCHECK_LEN(tptr, length); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 207 | } |