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