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