xref: /freebsd/libexec/bootpd/tools/bootptest/print-bootp.c (revision 5477372324b92240a96310ef2d45fa44ce8d0a93)
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 static void rfc1048_print(u_char *bp, int length);
46 static void cmu_print(u_char *bp, int length);
47 static void other_print(u_char *bp, int length);
48 static void dump_hex(u_char *bp, int len);
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 		struct ether_header *eh;
103 		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 = NULL;
113 		if (e == NULL || 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:%ld", (long)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 /*
187  * Option description data follows.
188  * These are described in: RFC-1048, RFC-1395, RFC-1497, RFC-1533
189  *
190  * The first char of each option string encodes the data format:
191  * ?: unknown
192  * a: ASCII
193  * b: byte (8-bit)
194  * i: inet address
195  * l: int32
196  * s: short (16-bit)
197  */
198 char *
199 rfc1048_opts[] = {
200 	/* Originally from RFC-1048: */
201 	"?PAD",				/*  0: Padding - special, no data. */
202 	"iSM",				/*  1: subnet mask (RFC950)*/
203 	"lTZ",				/*  2: time offset, seconds from UTC */
204 	"iGW",				/*  3: gateways (or routers) */
205 	"iTS",				/*  4: time servers (RFC868) */
206 	"iINS",				/*  5: IEN name servers (IEN116) */
207 	"iDNS",				/*  6: domain name servers (RFC1035)(1034?) */
208 	"iLOG",				/*  7: MIT log servers */
209 	"iCS",				/*  8: cookie servers (RFC865) */
210 	"iLPR",				/*  9: lpr server (RFC1179) */
211 	"iIPS",				/* 10: impress servers (Imagen) */
212 	"iRLP",				/* 11: resource location servers (RFC887) */
213 	"aHN",				/* 12: host name (ASCII) */
214 	"sBFS",				/* 13: boot file size (in 512 byte blocks) */
215 
216 	/* Added by RFC-1395: */
217 	"aDUMP",			/* 14: Merit Dump File */
218 	"aDNAM",			/* 15: Domain Name (for DNS) */
219 	"iSWAP",			/* 16: Swap Server */
220 	"aROOT",			/* 17: Root Path */
221 
222 	/* Added by RFC-1497: */
223 	"aEXTF",			/* 18: Extensions Path (more options) */
224 
225 	/* Added by RFC-1533: (many, many options...) */
226 #if 1	/* These might not be worth recognizing by name. */
227 
228 	/* IP Layer Parameters, per-host (RFC-1533, sect. 4) */
229 	"bIP-forward",		/* 19: IP Forwarding flag */
230 	"bIP-srcroute",		/* 20: IP Source Routing Enable flag */
231 	"iIP-filters",		/* 21: IP Policy Filter (addr pairs) */
232 	"sIP-maxudp",		/* 22: IP Max-UDP reassembly size */
233 	"bIP-ttlive",		/* 23: IP Time to Live */
234 	"lIP-pmtuage",		/* 24: IP Path MTU aging timeout */
235 	"sIP-pmtutab",		/* 25: IP Path MTU plateau table */
236 
237 	/* IP parameters, per-interface (RFC-1533, sect. 5) */
238 	"sIP-mtu-sz",		/* 26: IP MTU size */
239 	"bIP-mtu-sl",		/* 27: IP MTU all subnets local */
240 	"bIP-bcast1",		/* 28: IP Broadcast Addr ones flag */
241 	"bIP-mask-d",		/* 29: IP do mask discovery */
242 	"bIP-mask-s",		/* 30: IP do mask supplier */
243 	"bIP-rt-dsc",		/* 31: IP do router discovery */
244 	"iIP-rt-sa",		/* 32: IP router solicitation addr */
245 	"iIP-routes",		/* 33: IP static routes (dst,router) */
246 
247 	/* Link Layer parameters, per-interface (RFC-1533, sect. 6) */
248 	"bLL-trailer",		/* 34: do tralier encapsulation */
249 	"lLL-arp-tmo",		/* 35: ARP cache timeout */
250 	"bLL-ether2",		/* 36: Ethernet version 2 (IEEE 802.3) */
251 
252 	/* TCP parameters (RFC-1533, sect. 7) */
253 	"bTCP-def-ttl",		/* 37: default time to live */
254 	"lTCP-KA-tmo",		/* 38: keepalive time interval */
255 	"bTCP-KA-junk",		/* 39: keepalive sends extra junk */
256 
257 	/* Application and Service Parameters (RFC-1533, sect. 8) */
258 	"aNISDOM",			/* 40: NIS Domain (Sun YP) */
259 	"iNISSRV",			/* 41: NIS Servers */
260 	"iNTPSRV",			/* 42: NTP (time) Servers (RFC 1129) */
261 	"?VSINFO",			/* 43: Vendor Specific Info (encapsulated) */
262 	"iNBiosNS",			/* 44: NetBIOS Name Server (RFC-1001,1..2) */
263 	"iNBiosDD",			/* 45: NetBIOS Datagram Dist. Server. */
264 	"bNBiosNT",			/* 46: NetBIOS Note Type */
265 	"?NBiosS",			/* 47: NetBIOS Scope */
266 	"iXW-FS",			/* 48: X Window System Font Servers */
267 	"iXW-DM",			/* 49: X Window System Display Managers */
268 
269 	/* DHCP extensions (RFC-1533, sect. 9) */
270 #endif
271 };
272 #define	KNOWN_OPTIONS (sizeof(rfc1048_opts) / sizeof(rfc1048_opts[0]))
273 
274 static void
275 rfc1048_print(bp, length)
276 	u_char *bp;
277 	int length;
278 {
279 	u_char tag;
280 	u_char *ep;
281 	int len;
282 	u_int32 ul;
283 	u_short us;
284 	struct in_addr ia;
285 	char *optstr;
286 
287 	printf("-rfc1395");
288 
289 	/* Step over magic cookie */
290 	bp += sizeof(int32);
291 	/* Setup end pointer */
292 	ep = bp + length;
293 	while (bp < ep) {
294 		tag = *bp++;
295 		/* Check for tags with no data first. */
296 		if (tag == TAG_PAD)
297 			continue;
298 		if (tag == TAG_END)
299 			return;
300 		if (tag < KNOWN_OPTIONS) {
301 			optstr = rfc1048_opts[tag];
302 			printf(" %s:", optstr + 1);
303 		} else {
304 			printf(" T%d:", tag);
305 			optstr = "?";
306 		}
307 		/* Now scan the length byte. */
308 		len = *bp++;
309 		if (bp + len > ep) {
310 			/* truncated option */
311 			printf(" |(%d>%td)", len, ep - bp);
312 			return;
313 		}
314 		/* Print the option value(s). */
315 		switch (optstr[0]) {
316 
317 		case 'a':				/* ASCII string */
318 			printfn(bp, bp + len);
319 			bp += len;
320 			len = 0;
321 			break;
322 
323 		case 's':				/* Word formats */
324 			while (len >= 2) {
325 				bcopy((char *) bp, (char *) &us, 2);
326 				printf("%d", ntohs(us));
327 				bp += 2;
328 				len -= 2;
329 				if (len) printf(",");
330 			}
331 			if (len) printf("(junk=%d)", len);
332 			break;
333 
334 		case 'l':				/* Long words */
335 			while (len >= 4) {
336 				bcopy((char *) bp, (char *) &ul, 4);
337 				printf("%ld", (long)ntohl(ul));
338 				bp += 4;
339 				len -= 4;
340 				if (len) printf(",");
341 			}
342 			if (len) printf("(junk=%d)", len);
343 			break;
344 
345 		case 'i':				/* INET addresses */
346 			while (len >= 4) {
347 				bcopy((char *) bp, (char *) &ia, 4);
348 				printf("%s", ipaddr_string(&ia));
349 				bp += 4;
350 				len -= 4;
351 				if (len) printf(",");
352 			}
353 			if (len) printf("(junk=%d)", len);
354 			break;
355 
356 		case 'b':
357 		default:
358 			break;
359 
360 		}						/* switch */
361 
362 		/* Print as characters, if appropriate. */
363 		if (len) {
364 			dump_hex(bp, len);
365 			if (isascii(*bp) && isprint(*bp)) {
366 				printf("(");
367 				printfn(bp, bp + len);
368 				printf(")");
369 			}
370 			bp += len;
371 			len = 0;
372 		}
373 	} /* while bp < ep */
374 }
375 
376 static void
377 cmu_print(bp, length)
378 	u_char *bp;
379 	int length;
380 {
381 	struct cmu_vend *v;
382 
383 	printf("-cmu");
384 
385 	v = (struct cmu_vend *) bp;
386 	if (length < sizeof(*v)) {
387 		printf(" |L=%d", length);
388 		return;
389 	}
390 
391 	/* Subnet mask */
392 	if (v->v_flags & VF_SMASK) {
393 		printf(" SM:%s", ipaddr_string(&v->v_smask));
394 	}
395 	/* Default gateway */
396 	if (v->v_dgate.s_addr)
397 		printf(" GW:%s", ipaddr_string(&v->v_dgate));
398 
399 	/* Domain name servers */
400 	if (v->v_dns1.s_addr)
401 		printf(" DNS1:%s", ipaddr_string(&v->v_dns1));
402 	if (v->v_dns2.s_addr)
403 		printf(" DNS2:%s", ipaddr_string(&v->v_dns2));
404 
405 	/* IEN-116 name servers */
406 	if (v->v_ins1.s_addr)
407 		printf(" INS1:%s", ipaddr_string(&v->v_ins1));
408 	if (v->v_ins2.s_addr)
409 		printf(" INS2:%s", ipaddr_string(&v->v_ins2));
410 
411 	/* Time servers */
412 	if (v->v_ts1.s_addr)
413 		printf(" TS1:%s", ipaddr_string(&v->v_ts1));
414 	if (v->v_ts2.s_addr)
415 		printf(" TS2:%s", ipaddr_string(&v->v_ts2));
416 
417 }
418 
419 
420 /*
421  * Print out arbitrary, unknown vendor data.
422  */
423 
424 static void
425 other_print(bp, length)
426 	u_char *bp;
427 	int length;
428 {
429 	u_char *ep;					/* end pointer */
430 	u_char *zp;					/* points one past last non-zero byte */
431 
432 	/* Setup end pointer */
433 	ep = bp + length;
434 
435 	/* Find the last non-zero byte. */
436 	for (zp = ep; zp > bp; zp--) {
437 		if (zp[-1] != 0)
438 			break;
439 	}
440 
441 	/* Print the all-zero case in a compact representation. */
442 	if (zp == bp) {
443 		printf("-all-zero");
444 		return;
445 	}
446 	printf("-unknown");
447 
448 	/* Are there enough trailing zeros to make "00..." worthwhile? */
449 	if (zp + 2 > ep)
450 		zp = ep;				/* print them all normally */
451 
452 	/* Now just print all the non-zero data. */
453 	while (bp < zp) {
454 		printf(".%02X", *bp);
455 		bp++;
456 	}
457 
458 	if (zp < ep)
459 		printf(".00...");
460 
461 	return;
462 }
463 
464 static void
465 dump_hex(bp, len)
466 	u_char *bp;
467 	int len;
468 {
469 	while (len > 0) {
470 		printf("%02X", *bp);
471 		bp++;
472 		len--;
473 		if (len) printf(".");
474 	}
475 }
476 
477 /*
478  * Local Variables:
479  * tab-width: 4
480  * c-indent-level: 4
481  * c-argdecl-indent: 4
482  * c-continued-statement-offset: 4
483  * c-continued-brace-offset: -4
484  * c-label-offset: -4
485  * c-brace-offset: 0
486  * End:
487  */
488