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