1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
* Copyright (c) 2015 Vincent Bernat <bernat@luffy.cx>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include "pcap-hdr.h"
#include "../src/daemon/lldpd.h"
#define BUFSIZE 2000
static char *
tohex(char *str, size_t len)
{
static char *hex = NULL;
free(hex);
hex = NULL;
if ((hex = malloc(len * 3 + 1)) == NULL) return NULL;
for (size_t i = 0; i < len; i++)
snprintf(hex + 3 * i, 4, "%02X ", (unsigned char)str[i]);
return hex;
}
/* We need an assert macro which doesn't abort */
#define assert(x) \
while (!(x)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.\n", __FILE__, __LINE__, \
__func__, #x); \
exit(5); \
}
static int
decode(char *frame, int size, struct lldpd_hardware *hardware,
struct lldpd_chassis **nchassis, struct lldpd_port **nport)
{
/* For decoding, we only need a very basic hardware */
memset(hardware, 0, sizeof(struct lldpd_hardware));
hardware->h_mtu = 1500;
strlcpy(hardware->h_ifname, "test", sizeof(hardware->h_ifname));
int decoded = 0;
if (lldp_decode(NULL, frame, size, hardware, nchassis, nport) == -1) {
fprintf(stderr, "Not decoded as a LLDP frame\n");
} else {
fprintf(stderr, "Decoded as a LLDP frame\n");
decoded = 1;
}
#if defined ENABLE_CDP || defined ENABLE_FDP
if (cdp_decode(NULL, frame, size, hardware, nchassis, nport) == -1) {
fprintf(stderr, "Not decoded as a CDP frame\n");
} else {
fprintf(stderr, "Decoded as a CDP frame\n");
decoded = 1;
}
#endif
#ifdef ENABLE_SONMP
if (sonmp_decode(NULL, frame, size, hardware, nchassis, nport) == -1) {
fprintf(stderr, "Not decoded as a SONMP frame\n");
} else {
fprintf(stderr, "Decoded as a SONMP frame\n");
decoded = 1;
}
#endif
#ifdef ENABLE_EDP
if (edp_decode(NULL, frame, size, hardware, nchassis, nport) == -1) {
fprintf(stderr, "Not decoded as a EDP frame\n");
} else {
fprintf(stderr, "Decoded as a EDP frame\n");
decoded = 1;
}
#endif
return decoded;
}
static void
usage(void)
{
fprintf(stderr, "Usage: %s PCAP\n", "decode");
fprintf(stderr, "Version: %s\n", PACKAGE_STRING);
fprintf(stderr, "\n");
fprintf(stderr, "Decode content of PCAP files and display a summary\n");
fprintf(stderr, "on standard output. Only the first packet is decoded.\n");
exit(1);
}
int
main(int argc, char **argv)
{
if (argc != 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) usage();
int fd = open(argv[1], O_RDONLY);
assert(fd != -1);
char buf[BUFSIZE];
ssize_t len = read(fd, buf, BUFSIZE);
assert(len != -1);
struct pcap_hdr hdr;
assert(len >= sizeof(hdr));
memcpy(&hdr, buf, sizeof(hdr));
assert(hdr.magic_number == 0xa1b2c3d4); /* Assume the same byte order as us */
assert(hdr.version_major == 2);
assert(hdr.version_minor == 4);
assert(hdr.thiszone == 0);
/* Don't care about other flags */
struct pcaprec_hdr rechdr;
assert(len >= sizeof(hdr) + sizeof(rechdr));
memcpy(&rechdr, buf + sizeof(hdr), sizeof(rechdr));
assert(len >= sizeof(hdr) + sizeof(rechdr) + rechdr.incl_len);
struct lldpd_hardware hardware;
struct lldpd_chassis *nchassis = NULL;
struct lldpd_port *nport = NULL;
if (!decode(buf + sizeof(hdr) + sizeof(rechdr), rechdr.incl_len, &hardware,
&nchassis, &nport))
exit(1);
printf("Chassis:\n");
printf(" Index: %" PRIu16 "\n", nchassis->c_index);
printf(" Protocol: %" PRIu8 "\n", nchassis->c_protocol);
printf(" ID subtype: %" PRIu8 "\n", nchassis->c_id_subtype);
printf(" ID: %s\n", tohex(nchassis->c_id, nchassis->c_id_len));
printf(" Name: %s\n", nchassis->c_name ? nchassis->c_name : "(null)");
printf(" Description: %s\n", nchassis->c_descr ? nchassis->c_descr : "(null)");
printf(" Cap available: %" PRIu16 "\n", nchassis->c_cap_available);
printf(" Cap enabled: %" PRIu16 "\n", nchassis->c_cap_enabled);
struct lldpd_mgmt *mgmt;
TAILQ_FOREACH (mgmt, &nchassis->c_mgmt, m_entries) {
char ipaddress[INET6_ADDRSTRLEN + 1];
int af;
size_t alen;
switch (mgmt->m_family) {
case LLDPD_AF_IPV4:
alen = INET_ADDRSTRLEN + 1;
af = AF_INET;
break;
case LLDPD_AF_IPV6:
alen = INET6_ADDRSTRLEN + 1;
af = AF_INET6;
break;
default:
len = 0;
}
if (len == 0) continue;
if (inet_ntop(af, &mgmt->m_addr, ipaddress, alen) == NULL) break;
printf(" mgmt: %s\n", ipaddress);
}
#ifdef ENABLE_LLDPMED
printf(" MED cap: %" PRIu16 "\n", nchassis->c_med_cap_available);
printf(" MED type: %" PRIu8 "\n", nchassis->c_med_type);
printf(" MED HW: %s\n", nchassis->c_med_hw ? nchassis->c_med_hw : "(null)");
printf(" MED FW: %s\n", nchassis->c_med_fw ? nchassis->c_med_fw : "(null)");
printf(" MED SW: %s\n", nchassis->c_med_sw ? nchassis->c_med_sw : "(null)");
printf(" MED SN: %s\n", nchassis->c_med_sn ? nchassis->c_med_sn : "(null)");
printf(" MED manufacturer: %s\n",
nchassis->c_med_manuf ? nchassis->c_med_manuf : "(null)");
printf(" MED model: %s\n",
nchassis->c_med_model ? nchassis->c_med_model : "(null)");
printf(" MED asset: %s\n",
nchassis->c_med_asset ? nchassis->c_med_asset : "(null)");
#endif
printf("Port:\n");
printf(" ID subtype: %" PRIu8 "\n", nport->p_id_subtype);
printf(" ID: %s\n", tohex(nport->p_id, nport->p_id_len));
printf(" Description: %s\n", nport->p_descr ? nport->p_descr : "(null)");
printf(" MFS: %" PRIu16 "\n", nport->p_mfs);
printf(" TTL: %" PRIu16 "\n", nport->p_ttl);
#ifdef ENABLE_DOT3
printf(" Dot3 aggrID: %" PRIu32 "\n", nport->p_aggregid);
printf(" Dot3 MAC/phy autoneg supported: %" PRIu8 "\n",
nport->p_macphy.autoneg_support);
printf(" Dot3 MAC/phy autoneg enabled: %" PRIu8 "\n",
nport->p_macphy.autoneg_enabled);
printf(" Dot3 MAC/phy autoneg advertised: %" PRIu16 "\n",
nport->p_macphy.autoneg_advertised);
printf(" Dot3 MAC/phy MAU type: %" PRIu16 "\n", nport->p_macphy.mau_type);
printf(" Dot3 power device type: %" PRIu8 "\n", nport->p_power.devicetype);
printf(" Dot3 power supported: %" PRIu8 "\n", nport->p_power.supported);
printf(" Dot3 power enabled: %" PRIu8 "\n", nport->p_power.enabled);
printf(" Dot3 power pair control: %" PRIu8 "\n", nport->p_power.paircontrol);
printf(" Dot3 power pairs: %" PRIu8 "\n", nport->p_power.pairs);
printf(" Dot3 power class: %" PRIu8 "\n", nport->p_power.class);
printf(" Dot3 power type: %" PRIu8 "\n", nport->p_power.powertype);
printf(" Dot3 power source: %" PRIu8 "\n", nport->p_power.source);
printf(" Dot3 power priority: %" PRIu8 "\n", nport->p_power.priority);
printf(" Dot3 power requested: %" PRIu16 "\n", nport->p_power.requested);
printf(" Dot3 power allocated: %" PRIu16 "\n", nport->p_power.allocated);
#endif
#ifdef ENABLE_LLDPMED
printf(" MED cap: %" PRIu16 "\n", nport->p_med_cap_enabled);
for (int i = 0; i < LLDP_MED_APPTYPE_LAST; i++) {
if (nport->p_med_policy[i].type == 0) continue;
printf(" MED policy type: %" PRIu8 "\n", nport->p_med_policy[i].type);
printf(" MED policy unknown: %" PRIu8 "\n",
nport->p_med_policy[i].unknown);
printf(" MED policy tagged: %" PRIu8 "\n",
nport->p_med_policy[i].tagged);
printf(" MED policy vid: %" PRIu16 "\n", nport->p_med_policy[i].vid);
printf(" MED policy priority: %" PRIu8 "\n",
nport->p_med_policy[i].priority);
printf(" MED policy dscp: %" PRIu8 "\n", nport->p_med_policy[i].dscp);
}
for (int i = 0; i < LLDP_MED_LOCFORMAT_LAST; i++) {
if (nport->p_med_location[i].format == 0) continue;
printf(" MED location format: %" PRIu8 "\n",
nport->p_med_location[i].format);
printf(" MED location: %s\n",
tohex(nport->p_med_location[i].data,
nport->p_med_location[i].data_len));
}
printf(" MED power device type: %" PRIu8 "\n", nport->p_med_power.devicetype);
printf(" MED power source: %" PRIu8 "\n", nport->p_med_power.source);
printf(" MED power priority: %" PRIu8 "\n", nport->p_med_power.priority);
printf(" MED power value: %" PRIu16 "\n", nport->p_med_power.val);
#endif
#ifdef ENABLE_DOT1
printf(" Dot1 PVID: %" PRIu16 "\n", nport->p_pvid);
struct lldpd_vlan *vlan;
TAILQ_FOREACH (vlan, &nport->p_vlans, v_entries) {
printf(" Dot1 VLAN: %s (%" PRIu16 ")\n", vlan->v_name, vlan->v_vid);
}
struct lldpd_ppvid *ppvid;
TAILQ_FOREACH (ppvid, &nport->p_ppvids, p_entries) {
printf(" Dot1 PPVID: %" PRIu16 " (status: %" PRIu8 ")\n",
ppvid->p_ppvid, ppvid->p_cap_status);
}
struct lldpd_pi *pid;
TAILQ_FOREACH (pid, &nport->p_pids, p_entries) {
printf(" Dot1 PI: %s\n", tohex(pid->p_pi, pid->p_pi_len));
}
#endif
#ifdef ENABLE_CUSTOM
struct lldpd_custom *custom;
TAILQ_FOREACH (custom, &nport->p_custom_list, next) {
printf(" Custom OUI: %s\n",
tohex((char *)custom->oui, sizeof(custom->oui)));
printf(" Custom subtype: %" PRIu8 "\n", custom->subtype);
printf(" Custom info: %s\n",
tohex((char *)custom->oui_info, custom->oui_info_len));
}
#endif
exit(0);
}
|