Home | History | Annotate | Line # | Download | only in bootptest
print-bootp.c revision 1.2
      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 (at) ee.lbl.gov>
     25  */
     26 #ifndef lint
     27 static char rcsid[] = "$Id: print-bootp.c,v 1.2 1994/08/22 22:15:01 gwr Exp $";
     28 /* 93/10/10 <gwr (at) mc.com> New data-driven option print routine. */
     29 #endif
     30 
     31 #include <stdio.h>
     32 
     33 #include <sys/param.h>
     34 #include <sys/types.h>
     35 #include <sys/socket.h>
     36 #include <net/if.h>
     37 #include <netinet/in.h>
     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 static void rfc1048_print();
     46 static void cmu_print();
     47 static void other_print();
     48 static void dump_hex();
     49 
     50 /*
     51  * Print bootp requests
     52  */
     53 void
     54 bootp_print(bp, length, sport, dport)
     55 	struct bootp *bp;
     56 	int length;
     57 	u_short sport, dport;
     58 {
     59 	static char tstr[] = " [|bootp]";
     60 	static unsigned char vm_cmu[4] = VM_CMU;
     61 	static unsigned char vm_rfc1048[4] = VM_RFC1048;
     62 	u_char *ep;
     63 	int vdlen;
     64 
     65 #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
     66 
     67 	/* Note funny sized packets */
     68 	if (length != sizeof(struct bootp))
     69 		(void) printf(" [len=%d]", length);
     70 
     71 	/* 'ep' points to the end of avaible data. */
     72 	ep = (u_char *) snapend;
     73 
     74 	switch (bp->bp_op) {
     75 
     76 	case BOOTREQUEST:
     77 		/* Usually, a request goes from a client to a server */
     78 		if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
     79 			printf(" (request)");
     80 		break;
     81 
     82 	case BOOTREPLY:
     83 		/* Usually, a reply goes from a server to a client */
     84 		if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
     85 			printf(" (reply)");
     86 		break;
     87 
     88 	default:
     89 		printf(" bootp-#%d", bp->bp_op);
     90 	}
     91 
     92 	/* The usual hardware address type is 1 (10Mb Ethernet) */
     93 	if (bp->bp_htype != 1)
     94 		printf(" htype:%d", bp->bp_htype);
     95 
     96 	/* The usual length for 10Mb Ethernet address is 6 bytes */
     97 	if (bp->bp_hlen != 6)
     98 		printf(" hlen:%d", bp->bp_hlen);
     99 
    100 	/* Client's Hardware address */
    101 	if (bp->bp_hlen) {
    102 		register struct ether_header *eh;
    103 		register char *e;
    104 
    105 		TCHECK(bp->bp_chaddr[0], 6);
    106 		eh = (struct ether_header *) packetp;
    107 		if (bp->bp_op == BOOTREQUEST)
    108 			e = (char *) ESRC(eh);
    109 		else if (bp->bp_op == BOOTREPLY)
    110 			e = (char *) EDST(eh);
    111 		else
    112 			e = 0;
    113 		if (e == 0 || bcmp((char *) bp->bp_chaddr, e, 6))
    114 			dump_hex(bp->bp_chaddr, bp->bp_hlen);
    115 	}
    116 	/* Only print interesting fields */
    117 	if (bp->bp_hops)
    118 		printf(" hops:%d", bp->bp_hops);
    119 
    120 	if (bp->bp_xid)
    121 		printf(" xid:%d", ntohl(bp->bp_xid));
    122 
    123 	if (bp->bp_secs)
    124 		printf(" secs:%d", ntohs(bp->bp_secs));
    125 
    126 	/* Client's ip address */
    127 	TCHECK(bp->bp_ciaddr, sizeof(bp->bp_ciaddr));
    128 	if (bp->bp_ciaddr.s_addr)
    129 		printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
    130 
    131 	/* 'your' ip address (bootp client) */
    132 	TCHECK(bp->bp_yiaddr, sizeof(bp->bp_yiaddr));
    133 	if (bp->bp_yiaddr.s_addr)
    134 		printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
    135 
    136 	/* Server's ip address */
    137 	TCHECK(bp->bp_siaddr, sizeof(bp->bp_siaddr));
    138 	if (bp->bp_siaddr.s_addr)
    139 		printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
    140 
    141 	/* Gateway's ip address */
    142 	TCHECK(bp->bp_giaddr, sizeof(bp->bp_giaddr));
    143 	if (bp->bp_giaddr.s_addr)
    144 		printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
    145 
    146 	TCHECK(bp->bp_sname[0], sizeof(bp->bp_sname));
    147 	if (*bp->bp_sname) {
    148 		printf(" sname:");
    149 		if (printfn(bp->bp_sname, ep)) {
    150 			fputs(tstr + 1, stdout);
    151 			return;
    152 		}
    153 	}
    154 	TCHECK(bp->bp_file[0], sizeof(bp->bp_file));
    155 	if (*bp->bp_file) {
    156 		printf(" file:");
    157 		if (printfn(bp->bp_file, ep)) {
    158 			fputs(tstr + 1, stdout);
    159 			return;
    160 		}
    161 	}
    162 	/* Don't try to decode the vendor buffer unless we're verbose */
    163 	if (vflag <= 0)
    164 		return;
    165 
    166 	vdlen = sizeof(bp->bp_vend);
    167 	/* Vendor data can extend to the end of the packet. */
    168 	if (vdlen < (ep - bp->bp_vend))
    169 		vdlen = (ep - bp->bp_vend);
    170 
    171 	TCHECK(bp->bp_vend[0], vdlen);
    172 	printf(" vend");
    173 	if (!bcmp(bp->bp_vend, vm_rfc1048, sizeof(u_int32)))
    174 		rfc1048_print(bp->bp_vend, vdlen);
    175 	else if (!bcmp(bp->bp_vend, vm_cmu, sizeof(u_int32)))
    176 		cmu_print(bp->bp_vend, vdlen);
    177 	else
    178 		other_print(bp->bp_vend, vdlen);
    179 
    180 	return;
    181  trunc:
    182 	fputs(tstr, stdout);
    183 #undef TCHECK
    184 }
    185 
    186 /*
    188  * Option description data follows.
    189  * These are decribed 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 print_string();
    276 
    277 static void
    278 rfc1048_print(bp, length)
    279 	register u_char *bp;
    280 	int length;
    281 {
    282 	u_char tag;
    283 	u_char *ep;
    284 	register int len, j;
    285 	u_int32 ul;
    286 	u_short us;
    287 	struct in_addr ia;
    288 	char *optstr;
    289 
    290 	printf("-rfc1395");
    291 
    292 	/* Step over magic cookie */
    293 	bp += sizeof(int32);
    294 	/* Setup end pointer */
    295 	ep = bp + length;
    296 	while (bp < ep) {
    297 		tag = *bp++;
    298 		/* Check for tags with no data first. */
    299 		if (tag == TAG_PAD)
    300 			continue;
    301 		if (tag == TAG_END)
    302 			return;
    303 		if (tag < KNOWN_OPTIONS) {
    304 			optstr = rfc1048_opts[tag];
    305 			printf(" %s:", optstr + 1);
    306 		} else {
    307 			printf(" T%d:", tag);
    308 			optstr = "?";
    309 		}
    310 		/* Now scan the length byte. */
    311 		len = *bp++;
    312 		if (bp + len > ep) {
    313 			/* truncated option */
    314 			printf(" |(%d>%d)", len, ep - bp);
    315 			return;
    316 		}
    317 		/* Print the option value(s). */
    318 		switch (optstr[0]) {
    319 
    320 		case 'a':				/* ASCII string */
    321 			printfn(bp, bp + len);
    322 			bp += len;
    323 			len = 0;
    324 			break;
    325 
    326 		case 's':				/* Word formats */
    327 			while (len >= 2) {
    328 				bcopy((char *) bp, (char *) &us, 2);
    329 				printf("%d", ntohs(us));
    330 				bp += 2;
    331 				len -= 2;
    332 				if (len) printf(",");
    333 			}
    334 			if (len) printf("(junk=%d)", len);
    335 			break;
    336 
    337 		case 'l':				/* Long words */
    338 			while (len >= 4) {
    339 				bcopy((char *) bp, (char *) &ul, 4);
    340 				printf("%d", ntohl(ul));
    341 				bp += 4;
    342 				len -= 4;
    343 				if (len) printf(",");
    344 			}
    345 			if (len) printf("(junk=%d)", len);
    346 			break;
    347 
    348 		case 'i':				/* INET addresses */
    349 			while (len >= 4) {
    350 				bcopy((char *) bp, (char *) &ia, 4);
    351 				printf("%s", ipaddr_string(&ia));
    352 				bp += 4;
    353 				len -= 4;
    354 				if (len) printf(",");
    355 			}
    356 			if (len) printf("(junk=%d)", len);
    357 			break;
    358 
    359 		case 'b':
    360 		default:
    361 			break;
    362 
    363 		}						/* switch */
    364 
    365 		/* Print as characters, if appropriate. */
    366 		if (len) {
    367 			dump_hex(bp, len);
    368 			if (isascii(*bp) && isprint(*bp)) {
    369 				printf("(");
    370 				printfn(bp, bp + len);
    371 				printf(")");
    372 			}
    373 			bp += len;
    374 			len = 0;
    375 		}
    376 	} /* while bp < ep */
    377 }
    378 
    379 static void
    380 cmu_print(bp, length)
    381 	register u_char *bp;
    382 	int length;
    383 {
    384 	struct cmu_vend *v;
    385 	u_char *ep;
    386 
    387 	printf("-cmu");
    388 
    389 	v = (struct cmu_vend *) bp;
    390 	if (length < sizeof(*v)) {
    391 		printf(" |L=%d", length);
    392 		return;
    393 	}
    394 	/* Setup end pointer */
    395 	ep = bp + length;
    396 
    397 	/* Subnet mask */
    398 	if (v->v_flags & VF_SMASK) {
    399 		printf(" SM:%s", ipaddr_string(&v->v_smask));
    400 	}
    401 	/* Default gateway */
    402 	if (v->v_dgate.s_addr)
    403 		printf(" GW:%s", ipaddr_string(&v->v_dgate));
    404 
    405 	/* Domain name servers */
    406 	if (v->v_dns1.s_addr)
    407 		printf(" DNS1:%s", ipaddr_string(&v->v_dns1));
    408 	if (v->v_dns2.s_addr)
    409 		printf(" DNS2:%s", ipaddr_string(&v->v_dns2));
    410 
    411 	/* IEN-116 name servers */
    412 	if (v->v_ins1.s_addr)
    413 		printf(" INS1:%s", ipaddr_string(&v->v_ins1));
    414 	if (v->v_ins2.s_addr)
    415 		printf(" INS2:%s", ipaddr_string(&v->v_ins2));
    416 
    417 	/* Time servers */
    418 	if (v->v_ts1.s_addr)
    419 		printf(" TS1:%s", ipaddr_string(&v->v_ts1));
    420 	if (v->v_ts2.s_addr)
    421 		printf(" TS2:%s", ipaddr_string(&v->v_ts2));
    422 
    423 }
    424 
    425 
    426 /*
    427  * Print out arbitrary, unknown vendor data.
    428  */
    429 
    430 static void
    431 other_print(bp, length)
    432 	register u_char *bp;
    433 	int length;
    434 {
    435 	u_char *ep;					/* end pointer */
    436 	u_char *zp;					/* points one past last non-zero byte */
    437 	register int i, j;
    438 
    439 	/* Setup end pointer */
    440 	ep = bp + length;
    441 
    442 	/* Find the last non-zero byte. */
    443 	for (zp = ep; zp > bp; zp--) {
    444 		if (zp[-1] != 0)
    445 			break;
    446 	}
    447 
    448 	/* Print the all-zero case in a compact representation. */
    449 	if (zp == bp) {
    450 		printf("-all-zero");
    451 		return;
    452 	}
    453 	printf("-unknown");
    454 
    455 	/* Are there enough trailing zeros to make "00..." worthwhile? */
    456 	if (zp + 2 > ep)
    457 		zp = ep;				/* print them all normally */
    458 
    459 	/* Now just print all the non-zero data. */
    460 	while (bp < zp) {
    461 		printf(".%02X", *bp);
    462 		bp++;
    463 	}
    464 
    465 	if (zp < ep)
    466 		printf(".00...");
    467 
    468 	return;
    469 }
    470 
    471 static void
    472 dump_hex(bp, len)
    473 	u_char *bp;
    474 	int len;
    475 {
    476 	while (len > 0) {
    477 		printf("%02X", *bp);
    478 		bp++;
    479 		len--;
    480 		if (len) printf(".");
    481 	}
    482 }
    483 
    484 /*
    485  * Local Variables:
    486  * tab-width: 4
    487  * c-indent-level: 4
    488  * c-argdecl-indent: 4
    489  * c-continued-statement-offset: 4
    490  * c-continued-brace-offset: -4
    491  * c-label-offset: -4
    492  * c-brace-offset: 0
    493  * End:
    494  */
    495