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  * $FreeBSD: src/libexec/bootpd/tools/bootptest/print-bootp.c,v 1.6.2.1 2001/10/14 21:39:54 iedowse Exp $
27  * $DragonFly: src/libexec/bootpd/tools/bootptest/print-bootp.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
28  */
29 
30 #include <stdio.h>
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <sys/time.h>	/* for struct timeval in net/if.h */
37 #include <net/if.h>
38 #include <netinet/in.h>
39 
40 #include <string.h>
41 #include <ctype.h>
42 
43 #include "bootp.h"
44 #include "bootptest.h"
45 
46 /* These decode the vendor data. */
47 extern int printfn();
48 static void rfc1048_print();
49 static void cmu_print();
50 static void other_print();
51 static void dump_hex();
52 
53 /*
54  * Print bootp requests
55  */
56 void
57 bootp_print(bp, length, sport, dport)
58 	struct bootp *bp;
59 	int length;
60 	u_short sport, dport;
61 {
62 	static char tstr[] = " [|bootp]";
63 	static unsigned char vm_cmu[4] = VM_CMU;
64 	static unsigned char vm_rfc1048[4] = VM_RFC1048;
65 	u_char *ep;
66 	int vdlen;
67 
68 #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
69 
70 	/* Note funny sized packets */
71 	if (length != sizeof(struct bootp))
72 		(void) printf(" [len=%d]", length);
73 
74 	/* 'ep' points to the end of avaible data. */
75 	ep = (u_char *) snapend;
76 
77 	switch (bp->bp_op) {
78 
79 	case BOOTREQUEST:
80 		/* Usually, a request goes from a client to a server */
81 		if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
82 			printf(" (request)");
83 		break;
84 
85 	case BOOTREPLY:
86 		/* Usually, a reply goes from a server to a client */
87 		if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
88 			printf(" (reply)");
89 		break;
90 
91 	default:
92 		printf(" bootp-#%d", bp->bp_op);
93 	}
94 
95 	/* The usual hardware address type is 1 (10Mb Ethernet) */
96 	if (bp->bp_htype != 1)
97 		printf(" htype:%d", bp->bp_htype);
98 
99 	/* The usual length for 10Mb Ethernet address is 6 bytes */
100 	if (bp->bp_hlen != 6)
101 		printf(" hlen:%d", bp->bp_hlen);
102 
103 	/* Client's Hardware address */
104 	if (bp->bp_hlen) {
105 		register struct ether_header *eh;
106 		register char *e;
107 
108 		TCHECK(bp->bp_chaddr[0], 6);
109 		eh = (struct ether_header *) packetp;
110 		if (bp->bp_op == BOOTREQUEST)
111 			e = (char *) ESRC(eh);
112 		else if (bp->bp_op == BOOTREPLY)
113 			e = (char *) EDST(eh);
114 		else
115 			e = 0;
116 		if (e == 0 || bcmp((char *) bp->bp_chaddr, e, 6))
117 			dump_hex(bp->bp_chaddr, bp->bp_hlen);
118 	}
119 	/* Only print interesting fields */
120 	if (bp->bp_hops)
121 		printf(" hops:%d", bp->bp_hops);
122 
123 	if (bp->bp_xid)
124 		printf(" xid:%ld", (long)ntohl(bp->bp_xid));
125 
126 	if (bp->bp_secs)
127 		printf(" secs:%d", ntohs(bp->bp_secs));
128 
129 	/* Client's ip address */
130 	TCHECK(bp->bp_ciaddr, sizeof(bp->bp_ciaddr));
131 	if (bp->bp_ciaddr.s_addr)
132 		printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
133 
134 	/* 'your' ip address (bootp client) */
135 	TCHECK(bp->bp_yiaddr, sizeof(bp->bp_yiaddr));
136 	if (bp->bp_yiaddr.s_addr)
137 		printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
138 
139 	/* Server's ip address */
140 	TCHECK(bp->bp_siaddr, sizeof(bp->bp_siaddr));
141 	if (bp->bp_siaddr.s_addr)
142 		printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
143 
144 	/* Gateway's ip address */
145 	TCHECK(bp->bp_giaddr, sizeof(bp->bp_giaddr));
146 	if (bp->bp_giaddr.s_addr)
147 		printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
148 
149 	TCHECK(bp->bp_sname[0], sizeof(bp->bp_sname));
150 	if (*bp->bp_sname) {
151 		printf(" sname:");
152 		if (printfn(bp->bp_sname, ep)) {
153 			fputs(tstr + 1, stdout);
154 			return;
155 		}
156 	}
157 	TCHECK(bp->bp_file[0], sizeof(bp->bp_file));
158 	if (*bp->bp_file) {
159 		printf(" file:");
160 		if (printfn(bp->bp_file, ep)) {
161 			fputs(tstr + 1, stdout);
162 			return;
163 		}
164 	}
165 	/* Don't try to decode the vendor buffer unless we're verbose */
166 	if (vflag <= 0)
167 		return;
168 
169 	vdlen = sizeof(bp->bp_vend);
170 	/* Vendor data can extend to the end of the packet. */
171 	if (vdlen < (ep - bp->bp_vend))
172 		vdlen = (ep - bp->bp_vend);
173 
174 	TCHECK(bp->bp_vend[0], vdlen);
175 	printf(" vend");
176 	if (!bcmp(bp->bp_vend, vm_rfc1048, sizeof(u_int32)))
177 		rfc1048_print(bp->bp_vend, vdlen);
178 	else if (!bcmp(bp->bp_vend, vm_cmu, sizeof(u_int32)))
179 		cmu_print(bp->bp_vend, vdlen);
180 	else
181 		other_print(bp->bp_vend, vdlen);
182 
183 	return;
184  trunc:
185 	fputs(tstr, stdout);
186 #undef TCHECK
187 }
188 
189 /*
190  * Option description data follows.
191  * These are decribed in: RFC-1048, RFC-1395, RFC-1497, RFC-1533
192  *
193  * The first char of each option string encodes the data format:
194  * ?: unknown
195  * a: ASCII
196  * b: byte (8-bit)
197  * i: inet address
198  * l: int32
199  * s: short (16-bit)
200  */
201 char *
202 rfc1048_opts[] = {
203 	/* Originally from RFC-1048: */
204 	"?PAD",				/*  0: Padding - special, no data. */
205 	"iSM",				/*  1: subnet mask (RFC950)*/
206 	"lTZ",				/*  2: time offset, seconds from UTC */
207 	"iGW",				/*  3: gateways (or routers) */
208 	"iTS",				/*  4: time servers (RFC868) */
209 	"iINS",				/*  5: IEN name servers (IEN116) */
210 	"iDNS",				/*  6: domain name servers (RFC1035)(1034?) */
211 	"iLOG",				/*  7: MIT log servers */
212 	"iCS",				/*  8: cookie servers (RFC865) */
213 	"iLPR",				/*  9: lpr server (RFC1179) */
214 	"iIPS",				/* 10: impress servers (Imagen) */
215 	"iRLP",				/* 11: resource location servers (RFC887) */
216 	"aHN",				/* 12: host name (ASCII) */
217 	"sBFS",				/* 13: boot file size (in 512 byte blocks) */
218 
219 	/* Added by RFC-1395: */
220 	"aDUMP",			/* 14: Merit Dump File */
221 	"aDNAM",			/* 15: Domain Name (for DNS) */
222 	"iSWAP",			/* 16: Swap Server */
223 	"aROOT",			/* 17: Root Path */
224 
225 	/* Added by RFC-1497: */
226 	"aEXTF",			/* 18: Extensions Path (more options) */
227 
228 	/* Added by RFC-1533: (many, many options...) */
229 #if 1	/* These might not be worth recognizing by name. */
230 
231 	/* IP Layer Parameters, per-host (RFC-1533, sect. 4) */
232 	"bIP-forward",		/* 19: IP Forwarding flag */
233 	"bIP-srcroute",		/* 20: IP Source Routing Enable flag */
234 	"iIP-filters",		/* 21: IP Policy Filter (addr pairs) */
235 	"sIP-maxudp",		/* 22: IP Max-UDP reassembly size */
236 	"bIP-ttlive",		/* 23: IP Time to Live */
237 	"lIP-pmtuage",		/* 24: IP Path MTU aging timeout */
238 	"sIP-pmtutab",		/* 25: IP Path MTU plateau table */
239 
240 	/* IP parameters, per-interface (RFC-1533, sect. 5) */
241 	"sIP-mtu-sz",		/* 26: IP MTU size */
242 	"bIP-mtu-sl",		/* 27: IP MTU all subnets local */
243 	"bIP-bcast1",		/* 28: IP Broadcast Addr ones flag */
244 	"bIP-mask-d",		/* 29: IP do mask discovery */
245 	"bIP-mask-s",		/* 30: IP do mask supplier */
246 	"bIP-rt-dsc",		/* 31: IP do router discovery */
247 	"iIP-rt-sa",		/* 32: IP router solicitation addr */
248 	"iIP-routes",		/* 33: IP static routes (dst,router) */
249 
250 	/* Link Layer parameters, per-interface (RFC-1533, sect. 6) */
251 	"bLL-trailer",		/* 34: do tralier encapsulation */
252 	"lLL-arp-tmo",		/* 35: ARP cache timeout */
253 	"bLL-ether2",		/* 36: Ethernet version 2 (IEEE 802.3) */
254 
255 	/* TCP parameters (RFC-1533, sect. 7) */
256 	"bTCP-def-ttl",		/* 37: default time to live */
257 	"lTCP-KA-tmo",		/* 38: keepalive time interval */
258 	"bTCP-KA-junk",		/* 39: keepalive sends extra junk */
259 
260 	/* Application and Service Parameters (RFC-1533, sect. 8) */
261 	"aNISDOM",			/* 40: NIS Domain (Sun YP) */
262 	"iNISSRV",			/* 41: NIS Servers */
263 	"iNTPSRV",			/* 42: NTP (time) Servers (RFC 1129) */
264 	"?VSINFO",			/* 43: Vendor Specific Info (encapsulated) */
265 	"iNBiosNS",			/* 44: NetBIOS Name Server (RFC-1001,1..2) */
266 	"iNBiosDD",			/* 45: NetBIOS Datagram Dist. Server. */
267 	"bNBiosNT",			/* 46: NetBIOS Note Type */
268 	"?NBiosS",			/* 47: NetBIOS Scope */
269 	"iXW-FS",			/* 48: X Window System Font Servers */
270 	"iXW-DM",			/* 49: X Window System Display Managers */
271 
272 	/* DHCP extensions (RFC-1533, sect. 9) */
273 #endif
274 };
275 #define	KNOWN_OPTIONS (sizeof(rfc1048_opts) / sizeof(rfc1048_opts[0]))
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;
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("%ld", (long)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 
438 	/* Setup end pointer */
439 	ep = bp + length;
440 
441 	/* Find the last non-zero byte. */
442 	for (zp = ep; zp > bp; zp--) {
443 		if (zp[-1] != 0)
444 			break;
445 	}
446 
447 	/* Print the all-zero case in a compact representation. */
448 	if (zp == bp) {
449 		printf("-all-zero");
450 		return;
451 	}
452 	printf("-unknown");
453 
454 	/* Are there enough trailing zeros to make "00..." worthwhile? */
455 	if (zp + 2 > ep)
456 		zp = ep;				/* print them all normally */
457 
458 	/* Now just print all the non-zero data. */
459 	while (bp < zp) {
460 		printf(".%02X", *bp);
461 		bp++;
462 	}
463 
464 	if (zp < ep)
465 		printf(".00...");
466 
467 	return;
468 }
469 
470 static void
471 dump_hex(bp, len)
472 	u_char *bp;
473 	int len;
474 {
475 	while (len > 0) {
476 		printf("%02X", *bp);
477 		bp++;
478 		len--;
479 		if (len) printf(".");
480 	}
481 }
482 
483 /*
484  * Local Variables:
485  * tab-width: 4
486  * c-indent-level: 4
487  * c-argdecl-indent: 4
488  * c-continued-statement-offset: 4
489  * c-continued-brace-offset: -4
490  * c-label-offset: -4
491  * c-brace-offset: 0
492  * End:
493  */
494