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