1 /* 2 * Copyright (c) 1988-1990 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: 7 * 1. Source code distributions retain the above copyright 8 * notice and this paragraph in its entirety 9 * 2. Distributions including binary code include the above copyright 10 * notice and this paragraph in its entirety in the documentation 11 * or other materials provided with the distribution, and 12 * 3. Neither the name of the University nor the names of its contributors 13 * may be used to endorse or promote products derived from this software 14 * without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * Format and print bootp packets. 21 * 22 * This file was copied from tcpdump-2.1.1 and modified. 23 * There is an e-mail list for tcpdump: <tcpdump@ee.lbl.gov> 24 * 25 * $FreeBSD$ 26 */ 27 28 #include <stdio.h> 29 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 34 #include <sys/time.h> /* for struct timeval in net/if.h */ 35 #include <net/if.h> 36 #include <netinet/in.h> 37 38 #include <string.h> 39 #include <ctype.h> 40 41 #include "bootp.h" 42 #include "bootptest.h" 43 44 /* These decode the vendor data. */ 45 extern int printfn(); 46 static void rfc1048_print(); 47 static void cmu_print(); 48 static void other_print(); 49 static void dump_hex(); 50 51 /* 52 * Print bootp requests 53 */ 54 void 55 bootp_print(bp, length, sport, dport) 56 struct bootp *bp; 57 int length; 58 u_short sport, dport; 59 { 60 static char tstr[] = " [|bootp]"; 61 static unsigned char vm_cmu[4] = VM_CMU; 62 static unsigned char vm_rfc1048[4] = VM_RFC1048; 63 u_char *ep; 64 int vdlen; 65 66 #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc 67 68 /* Note funny sized packets */ 69 if (length != sizeof(struct bootp)) 70 (void) printf(" [len=%d]", length); 71 72 /* 'ep' points to the end of avaible data. */ 73 ep = (u_char *) snapend; 74 75 switch (bp->bp_op) { 76 77 case BOOTREQUEST: 78 /* Usually, a request goes from a client to a server */ 79 if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS) 80 printf(" (request)"); 81 break; 82 83 case BOOTREPLY: 84 /* Usually, a reply goes from a server to a client */ 85 if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC) 86 printf(" (reply)"); 87 break; 88 89 default: 90 printf(" bootp-#%d", bp->bp_op); 91 } 92 93 /* The usual hardware address type is 1 (10Mb Ethernet) */ 94 if (bp->bp_htype != 1) 95 printf(" htype:%d", bp->bp_htype); 96 97 /* The usual length for 10Mb Ethernet address is 6 bytes */ 98 if (bp->bp_hlen != 6) 99 printf(" hlen:%d", bp->bp_hlen); 100 101 /* Client's Hardware address */ 102 if (bp->bp_hlen) { 103 register struct ether_header *eh; 104 register char *e; 105 106 TCHECK(bp->bp_chaddr[0], 6); 107 eh = (struct ether_header *) packetp; 108 if (bp->bp_op == BOOTREQUEST) 109 e = (char *) ESRC(eh); 110 else if (bp->bp_op == BOOTREPLY) 111 e = (char *) EDST(eh); 112 else 113 e = 0; 114 if (e == 0 || bcmp((char *) bp->bp_chaddr, e, 6)) 115 dump_hex(bp->bp_chaddr, bp->bp_hlen); 116 } 117 /* Only print interesting fields */ 118 if (bp->bp_hops) 119 printf(" hops:%d", bp->bp_hops); 120 121 if (bp->bp_xid) 122 printf(" xid:%ld", (long)ntohl(bp->bp_xid)); 123 124 if (bp->bp_secs) 125 printf(" secs:%d", ntohs(bp->bp_secs)); 126 127 /* Client's ip address */ 128 TCHECK(bp->bp_ciaddr, sizeof(bp->bp_ciaddr)); 129 if (bp->bp_ciaddr.s_addr) 130 printf(" C:%s", ipaddr_string(&bp->bp_ciaddr)); 131 132 /* 'your' ip address (bootp client) */ 133 TCHECK(bp->bp_yiaddr, sizeof(bp->bp_yiaddr)); 134 if (bp->bp_yiaddr.s_addr) 135 printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr)); 136 137 /* Server's ip address */ 138 TCHECK(bp->bp_siaddr, sizeof(bp->bp_siaddr)); 139 if (bp->bp_siaddr.s_addr) 140 printf(" S:%s", ipaddr_string(&bp->bp_siaddr)); 141 142 /* Gateway's ip address */ 143 TCHECK(bp->bp_giaddr, sizeof(bp->bp_giaddr)); 144 if (bp->bp_giaddr.s_addr) 145 printf(" G:%s", ipaddr_string(&bp->bp_giaddr)); 146 147 TCHECK(bp->bp_sname[0], sizeof(bp->bp_sname)); 148 if (*bp->bp_sname) { 149 printf(" sname:"); 150 if (printfn(bp->bp_sname, ep)) { 151 fputs(tstr + 1, stdout); 152 return; 153 } 154 } 155 TCHECK(bp->bp_file[0], sizeof(bp->bp_file)); 156 if (*bp->bp_file) { 157 printf(" file:"); 158 if (printfn(bp->bp_file, ep)) { 159 fputs(tstr + 1, stdout); 160 return; 161 } 162 } 163 /* Don't try to decode the vendor buffer unless we're verbose */ 164 if (vflag <= 0) 165 return; 166 167 vdlen = sizeof(bp->bp_vend); 168 /* Vendor data can extend to the end of the packet. */ 169 if (vdlen < (ep - bp->bp_vend)) 170 vdlen = (ep - bp->bp_vend); 171 172 TCHECK(bp->bp_vend[0], vdlen); 173 printf(" vend"); 174 if (!bcmp(bp->bp_vend, vm_rfc1048, sizeof(u_int32))) 175 rfc1048_print(bp->bp_vend, vdlen); 176 else if (!bcmp(bp->bp_vend, vm_cmu, sizeof(u_int32))) 177 cmu_print(bp->bp_vend, vdlen); 178 else 179 other_print(bp->bp_vend, vdlen); 180 181 return; 182 trunc: 183 fputs(tstr, stdout); 184 #undef TCHECK 185 } 186 187 /* 188 * Option description data follows. 189 * These are described in: RFC-1048, RFC-1395, RFC-1497, RFC-1533 190 * 191 * The first char of each option string encodes the data format: 192 * ?: unknown 193 * a: ASCII 194 * b: byte (8-bit) 195 * i: inet address 196 * l: int32 197 * s: short (16-bit) 198 */ 199 char * 200 rfc1048_opts[] = { 201 /* Originally from RFC-1048: */ 202 "?PAD", /* 0: Padding - special, no data. */ 203 "iSM", /* 1: subnet mask (RFC950)*/ 204 "lTZ", /* 2: time offset, seconds from UTC */ 205 "iGW", /* 3: gateways (or routers) */ 206 "iTS", /* 4: time servers (RFC868) */ 207 "iINS", /* 5: IEN name servers (IEN116) */ 208 "iDNS", /* 6: domain name servers (RFC1035)(1034?) */ 209 "iLOG", /* 7: MIT log servers */ 210 "iCS", /* 8: cookie servers (RFC865) */ 211 "iLPR", /* 9: lpr server (RFC1179) */ 212 "iIPS", /* 10: impress servers (Imagen) */ 213 "iRLP", /* 11: resource location servers (RFC887) */ 214 "aHN", /* 12: host name (ASCII) */ 215 "sBFS", /* 13: boot file size (in 512 byte blocks) */ 216 217 /* Added by RFC-1395: */ 218 "aDUMP", /* 14: Merit Dump File */ 219 "aDNAM", /* 15: Domain Name (for DNS) */ 220 "iSWAP", /* 16: Swap Server */ 221 "aROOT", /* 17: Root Path */ 222 223 /* Added by RFC-1497: */ 224 "aEXTF", /* 18: Extensions Path (more options) */ 225 226 /* Added by RFC-1533: (many, many options...) */ 227 #if 1 /* These might not be worth recognizing by name. */ 228 229 /* IP Layer Parameters, per-host (RFC-1533, sect. 4) */ 230 "bIP-forward", /* 19: IP Forwarding flag */ 231 "bIP-srcroute", /* 20: IP Source Routing Enable flag */ 232 "iIP-filters", /* 21: IP Policy Filter (addr pairs) */ 233 "sIP-maxudp", /* 22: IP Max-UDP reassembly size */ 234 "bIP-ttlive", /* 23: IP Time to Live */ 235 "lIP-pmtuage", /* 24: IP Path MTU aging timeout */ 236 "sIP-pmtutab", /* 25: IP Path MTU plateau table */ 237 238 /* IP parameters, per-interface (RFC-1533, sect. 5) */ 239 "sIP-mtu-sz", /* 26: IP MTU size */ 240 "bIP-mtu-sl", /* 27: IP MTU all subnets local */ 241 "bIP-bcast1", /* 28: IP Broadcast Addr ones flag */ 242 "bIP-mask-d", /* 29: IP do mask discovery */ 243 "bIP-mask-s", /* 30: IP do mask supplier */ 244 "bIP-rt-dsc", /* 31: IP do router discovery */ 245 "iIP-rt-sa", /* 32: IP router solicitation addr */ 246 "iIP-routes", /* 33: IP static routes (dst,router) */ 247 248 /* Link Layer parameters, per-interface (RFC-1533, sect. 6) */ 249 "bLL-trailer", /* 34: do tralier encapsulation */ 250 "lLL-arp-tmo", /* 35: ARP cache timeout */ 251 "bLL-ether2", /* 36: Ethernet version 2 (IEEE 802.3) */ 252 253 /* TCP parameters (RFC-1533, sect. 7) */ 254 "bTCP-def-ttl", /* 37: default time to live */ 255 "lTCP-KA-tmo", /* 38: keepalive time interval */ 256 "bTCP-KA-junk", /* 39: keepalive sends extra junk */ 257 258 /* Application and Service Parameters (RFC-1533, sect. 8) */ 259 "aNISDOM", /* 40: NIS Domain (Sun YP) */ 260 "iNISSRV", /* 41: NIS Servers */ 261 "iNTPSRV", /* 42: NTP (time) Servers (RFC 1129) */ 262 "?VSINFO", /* 43: Vendor Specific Info (encapsulated) */ 263 "iNBiosNS", /* 44: NetBIOS Name Server (RFC-1001,1..2) */ 264 "iNBiosDD", /* 45: NetBIOS Datagram Dist. Server. */ 265 "bNBiosNT", /* 46: NetBIOS Note Type */ 266 "?NBiosS", /* 47: NetBIOS Scope */ 267 "iXW-FS", /* 48: X Window System Font Servers */ 268 "iXW-DM", /* 49: X Window System Display Managers */ 269 270 /* DHCP extensions (RFC-1533, sect. 9) */ 271 #endif 272 }; 273 #define KNOWN_OPTIONS (sizeof(rfc1048_opts) / sizeof(rfc1048_opts[0])) 274 275 static void 276 rfc1048_print(bp, length) 277 register u_char *bp; 278 int length; 279 { 280 u_char tag; 281 u_char *ep; 282 register int len; 283 u_int32 ul; 284 u_short us; 285 struct in_addr ia; 286 char *optstr; 287 288 printf("-rfc1395"); 289 290 /* Step over magic cookie */ 291 bp += sizeof(int32); 292 /* Setup end pointer */ 293 ep = bp + length; 294 while (bp < ep) { 295 tag = *bp++; 296 /* Check for tags with no data first. */ 297 if (tag == TAG_PAD) 298 continue; 299 if (tag == TAG_END) 300 return; 301 if (tag < KNOWN_OPTIONS) { 302 optstr = rfc1048_opts[tag]; 303 printf(" %s:", optstr + 1); 304 } else { 305 printf(" T%d:", tag); 306 optstr = "?"; 307 } 308 /* Now scan the length byte. */ 309 len = *bp++; 310 if (bp + len > ep) { 311 /* truncated option */ 312 printf(" |(%d>%td)", len, ep - bp); 313 return; 314 } 315 /* Print the option value(s). */ 316 switch (optstr[0]) { 317 318 case 'a': /* ASCII string */ 319 printfn(bp, bp + len); 320 bp += len; 321 len = 0; 322 break; 323 324 case 's': /* Word formats */ 325 while (len >= 2) { 326 bcopy((char *) bp, (char *) &us, 2); 327 printf("%d", ntohs(us)); 328 bp += 2; 329 len -= 2; 330 if (len) printf(","); 331 } 332 if (len) printf("(junk=%d)", len); 333 break; 334 335 case 'l': /* Long words */ 336 while (len >= 4) { 337 bcopy((char *) bp, (char *) &ul, 4); 338 printf("%ld", (long)ntohl(ul)); 339 bp += 4; 340 len -= 4; 341 if (len) printf(","); 342 } 343 if (len) printf("(junk=%d)", len); 344 break; 345 346 case 'i': /* INET addresses */ 347 while (len >= 4) { 348 bcopy((char *) bp, (char *) &ia, 4); 349 printf("%s", ipaddr_string(&ia)); 350 bp += 4; 351 len -= 4; 352 if (len) printf(","); 353 } 354 if (len) printf("(junk=%d)", len); 355 break; 356 357 case 'b': 358 default: 359 break; 360 361 } /* switch */ 362 363 /* Print as characters, if appropriate. */ 364 if (len) { 365 dump_hex(bp, len); 366 if (isascii(*bp) && isprint(*bp)) { 367 printf("("); 368 printfn(bp, bp + len); 369 printf(")"); 370 } 371 bp += len; 372 len = 0; 373 } 374 } /* while bp < ep */ 375 } 376 377 static void 378 cmu_print(bp, length) 379 register u_char *bp; 380 int length; 381 { 382 struct cmu_vend *v; 383 u_char *ep; 384 385 printf("-cmu"); 386 387 v = (struct cmu_vend *) bp; 388 if (length < sizeof(*v)) { 389 printf(" |L=%d", length); 390 return; 391 } 392 /* Setup end pointer */ 393 ep = bp + length; 394 395 /* Subnet mask */ 396 if (v->v_flags & VF_SMASK) { 397 printf(" SM:%s", ipaddr_string(&v->v_smask)); 398 } 399 /* Default gateway */ 400 if (v->v_dgate.s_addr) 401 printf(" GW:%s", ipaddr_string(&v->v_dgate)); 402 403 /* Domain name servers */ 404 if (v->v_dns1.s_addr) 405 printf(" DNS1:%s", ipaddr_string(&v->v_dns1)); 406 if (v->v_dns2.s_addr) 407 printf(" DNS2:%s", ipaddr_string(&v->v_dns2)); 408 409 /* IEN-116 name servers */ 410 if (v->v_ins1.s_addr) 411 printf(" INS1:%s", ipaddr_string(&v->v_ins1)); 412 if (v->v_ins2.s_addr) 413 printf(" INS2:%s", ipaddr_string(&v->v_ins2)); 414 415 /* Time servers */ 416 if (v->v_ts1.s_addr) 417 printf(" TS1:%s", ipaddr_string(&v->v_ts1)); 418 if (v->v_ts2.s_addr) 419 printf(" TS2:%s", ipaddr_string(&v->v_ts2)); 420 421 } 422 423 424 /* 425 * Print out arbitrary, unknown vendor data. 426 */ 427 428 static void 429 other_print(bp, length) 430 register u_char *bp; 431 int length; 432 { 433 u_char *ep; /* end pointer */ 434 u_char *zp; /* points one past last non-zero byte */ 435 436 /* Setup end pointer */ 437 ep = bp + length; 438 439 /* Find the last non-zero byte. */ 440 for (zp = ep; zp > bp; zp--) { 441 if (zp[-1] != 0) 442 break; 443 } 444 445 /* Print the all-zero case in a compact representation. */ 446 if (zp == bp) { 447 printf("-all-zero"); 448 return; 449 } 450 printf("-unknown"); 451 452 /* Are there enough trailing zeros to make "00..." worthwhile? */ 453 if (zp + 2 > ep) 454 zp = ep; /* print them all normally */ 455 456 /* Now just print all the non-zero data. */ 457 while (bp < zp) { 458 printf(".%02X", *bp); 459 bp++; 460 } 461 462 if (zp < ep) 463 printf(".00..."); 464 465 return; 466 } 467 468 static void 469 dump_hex(bp, len) 470 u_char *bp; 471 int len; 472 { 473 while (len > 0) { 474 printf("%02X", *bp); 475 bp++; 476 len--; 477 if (len) printf("."); 478 } 479 } 480 481 /* 482 * Local Variables: 483 * tab-width: 4 484 * c-indent-level: 4 485 * c-argdecl-indent: 4 486 * c-continued-statement-offset: 4 487 * c-continued-brace-offset: -4 488 * c-label-offset: -4 489 * c-brace-offset: 0 490 * End: 491 */ 492