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 #include <sys/cdefs.h> 23 #ifndef lint 24 __RCSID("$NetBSD: print-udld.c,v 1.5 2024/09/02 16:15:33 christos Exp $"); 25 #endif 26 27 #include <config.h> 28 29 #include "netdissect-stdinc.h" 30 31 #define ND_LONGJMP_FROM_TCHECK 32 #include "netdissect.h" 33 #include "extract.h" 34 35 36 #define UDLD_HEADER_LEN 4 37 #define UDLD_TLV_HEADER_LEN 4 38 #define UDLD_DEVICE_ID_TLV 0x0001 39 #define UDLD_PORT_ID_TLV 0x0002 40 #define UDLD_ECHO_TLV 0x0003 41 #define UDLD_MESSAGE_INTERVAL_TLV 0x0004 42 #define UDLD_TIMEOUT_INTERVAL_TLV 0x0005 43 #define UDLD_DEVICE_NAME_TLV 0x0006 44 #define UDLD_SEQ_NUMBER_TLV 0x0007 45 46 static const struct tok udld_tlv_values[] = { 47 { UDLD_DEVICE_ID_TLV, "Device-ID TLV"}, 48 { UDLD_PORT_ID_TLV, "Port-ID TLV"}, 49 { UDLD_ECHO_TLV, "Echo TLV"}, 50 { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"}, 51 { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"}, 52 { UDLD_DEVICE_NAME_TLV, "Device Name TLV"}, 53 { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"}, 54 { 0, NULL} 55 }; 56 57 static const struct tok udld_code_values[] = { 58 { 0x00, "Reserved"}, 59 { 0x01, "Probe message"}, 60 { 0x02, "Echo message"}, 61 { 0x03, "Flush message"}, 62 { 0, NULL} 63 }; 64 65 static const struct tok udld_flags_bitmap_str[] = { 66 { 1U << 0, "RT" }, 67 { 1U << 1, "RSY" }, 68 { 1U << 2, "MBZ-2" }, 69 { 1U << 3, "MBZ-3" }, 70 { 1U << 4, "MBZ-4" }, 71 { 1U << 5, "MBZ-5" }, 72 { 1U << 6, "MBZ-6" }, 73 { 1U << 7, "MBZ-7" }, 74 { 0, NULL} 75 }; 76 77 /* 78 * UDLD's Protocol Data Unit format: 79 * 80 * 0 1 2 3 81 * 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 82 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 83 * | Ver | Opcode | Flags | Checksum | 84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 85 * | List of TLVs (variable length list) | 86 * | ... | 87 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 88 * 89 * TLV format: 90 * 91 * 0 1 2 3 92 * 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 93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 94 * | TYPE | LENGTH | 95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 96 * | VALUE | 97 * | ... | 98 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 99 * 100 * LENGTH: Length in bytes of the Type, Length, and Value fields. 101 */ 102 103 #define UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5) 104 #define UDLD_EXTRACT_OPCODE(x) ((x)&0x1f) 105 106 void 107 udld_print(netdissect_options *ndo, 108 const u_char *tptr, u_int length) 109 { 110 uint8_t ver, code, flags; 111 112 ndo->ndo_protocol = "udld"; 113 if (length < UDLD_HEADER_LEN) 114 goto invalid; 115 116 ver = UDLD_EXTRACT_VERSION(GET_U_1(tptr)); 117 code = UDLD_EXTRACT_OPCODE(GET_U_1(tptr)); 118 tptr += 1; 119 length -= 1; 120 121 flags = GET_U_1(tptr); 122 tptr += 1; 123 length -= 1; 124 125 ND_PRINT("UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u", 126 ver, 127 tok2str(udld_code_values, "Reserved", code), 128 code, 129 bittok2str(udld_flags_bitmap_str, "none", flags), 130 flags, 131 length + 2); 132 133 /* 134 * In non-verbose mode, just print version and opcode type 135 */ 136 if (ndo->ndo_vflag < 1) { 137 goto tcheck_remainder; 138 } 139 140 ND_PRINT("\n\tChecksum 0x%04x (unverified)", GET_BE_U_2(tptr)); 141 tptr += 2; 142 length -= 2; 143 144 while (length) { 145 uint16_t type, len; 146 147 if (length < UDLD_TLV_HEADER_LEN) 148 goto invalid; 149 150 type = GET_BE_U_2(tptr); 151 tptr += 2; 152 length -= 2; 153 154 len = GET_BE_U_2(tptr); 155 tptr += 2; 156 length -= 2; 157 158 ND_PRINT("\n\t%s (0x%04x) TLV, length %u", 159 tok2str(udld_tlv_values, "Unknown", type), 160 type, len); 161 162 /* infinite loop check */ 163 if (len <= UDLD_TLV_HEADER_LEN) 164 goto invalid; 165 166 len -= UDLD_TLV_HEADER_LEN; 167 if (length < len) 168 goto invalid; 169 170 switch (type) { 171 case UDLD_DEVICE_ID_TLV: 172 case UDLD_PORT_ID_TLV: 173 case UDLD_DEVICE_NAME_TLV: 174 ND_PRINT(", "); 175 nd_printjnp(ndo, tptr, len); 176 break; 177 178 case UDLD_ECHO_TLV: 179 ND_PRINT(", "); 180 (void)nd_printn(ndo, tptr, len, NULL); 181 break; 182 183 case UDLD_MESSAGE_INTERVAL_TLV: 184 case UDLD_TIMEOUT_INTERVAL_TLV: 185 if (len != 1) 186 goto invalid; 187 ND_PRINT(", %us", (GET_U_1(tptr))); 188 break; 189 190 case UDLD_SEQ_NUMBER_TLV: 191 if (len != 4) 192 goto invalid; 193 ND_PRINT(", %u", GET_BE_U_4(tptr)); 194 break; 195 196 default: 197 ND_TCHECK_LEN(tptr, len); 198 break; 199 } 200 tptr += len; 201 length -= len; 202 } 203 204 return; 205 206 invalid: 207 nd_print_invalid(ndo); 208 tcheck_remainder: 209 ND_TCHECK_LEN(tptr, length); 210 } 211