xref: /openbsd-src/usr.sbin/tcpdump/print-bootp.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: print-bootp.c,v 1.11 2000/10/03 14:31:56 ho Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * Format and print bootp packets.
24  */
25 #ifndef lint
26 static const char rcsid[] =
27     "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-bootp.c,v 1.11 2000/10/03 14:31:56 ho Exp $ (LBL)";
28 #endif
29 
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 
34 #ifdef __STDC__
35 struct mbuf;
36 struct rtentry;
37 #endif
38 #include <net/if.h>
39 
40 #include <netinet/in.h>
41 #include <netinet/if_ether.h>
42 
43 #include <ctype.h>
44 #include <memory.h>
45 #include <stdio.h>
46 #include <string.h>
47 
48 #include "interface.h"
49 #include "addrtoname.h"
50 #include "bootp.h"
51 
52 static void rfc1048_print(const u_char *, u_int);
53 static void cmu_print(const u_char *, u_int);
54 
55 static char tstr[] = " [|bootp]";
56 
57 /*
58  * Print bootp requests
59  */
60 void
61 bootp_print(register const u_char *cp, u_int length,
62 	    u_short sport, u_short dport)
63 {
64 	register const struct bootp *bp;
65 	static u_char vm_cmu[4] = VM_CMU;
66 	static u_char vm_rfc1048[4] = VM_RFC1048;
67 
68 	bp = (struct bootp *)cp;
69 	TCHECK(bp->bp_op);
70 	switch (bp->bp_op) {
71 
72 	case BOOTREQUEST:
73 		/* Usually, a request goes from a client to a server */
74 		if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
75 			printf(" (request)");
76 		break;
77 
78 	case BOOTREPLY:
79 		/* Usually, a reply goes from a server to a client */
80 		if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
81 			printf(" (reply)");
82 		break;
83 
84 	default:
85 		printf(" bootp-#%d", bp->bp_op);
86 	}
87 
88 	TCHECK(bp->bp_secs);
89 
90 	/* The usual hardware address type is 1 (10Mb Ethernet) */
91 	if (bp->bp_htype != 1)
92 		printf(" htype-#%d", bp->bp_htype);
93 
94 	/* The usual length for 10Mb Ethernet address is 6 bytes */
95 	if (bp->bp_htype != 1 || bp->bp_hlen != 6)
96 		printf(" hlen:%d", bp->bp_hlen);
97 
98 	/* Only print interesting fields */
99 	if (bp->bp_hops)
100 		printf(" hops:%d", bp->bp_hops);
101 	if (bp->bp_xid)
102 		printf(" xid:0x%x", (u_int32_t)ntohl(bp->bp_xid));
103 	if (bp->bp_secs)
104 		printf(" secs:%d", ntohs(bp->bp_secs));
105 	if (bp->bp_flags)
106 		printf(" flags:0x%x", ntohs(bp->bp_flags));
107 
108 	/* Client's ip address */
109 	TCHECK(bp->bp_ciaddr);
110 	if (bp->bp_ciaddr.s_addr)
111 		printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
112 
113 	/* 'your' ip address (bootp client) */
114 	TCHECK(bp->bp_yiaddr);
115 	if (bp->bp_yiaddr.s_addr)
116 		printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
117 
118 	/* Server's ip address */
119 	TCHECK(bp->bp_siaddr);
120 	if (bp->bp_siaddr.s_addr)
121 		printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
122 
123 	/* Gateway's ip address */
124 	TCHECK(bp->bp_giaddr);
125 	if (bp->bp_giaddr.s_addr)
126 		printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
127 
128 	/* Client's Ethernet address */
129 	if (bp->bp_htype == 1 && bp->bp_hlen == 6) {
130 		register const struct ether_header *eh;
131 		register const char *e;
132 
133 		TCHECK2(bp->bp_chaddr[0], 6);
134 		eh = (struct ether_header *)packetp;
135 		if (bp->bp_op == BOOTREQUEST)
136 			e = (const char *)ESRC(eh);
137 		else if (bp->bp_op == BOOTREPLY)
138 			e = (const char *)EDST(eh);
139 		else
140 			e = 0;
141 		if (e == 0 || memcmp((char *)bp->bp_chaddr, e, 6) != 0)
142 			printf(" ether %s", etheraddr_string(bp->bp_chaddr));
143 	}
144 
145 	TCHECK2(bp->bp_sname[0], 1);		/* check first char only */
146 	if (*bp->bp_sname) {
147 		printf(" sname \"");
148 		if (fn_print(bp->bp_sname, snapend)) {
149 			putchar('"');
150 			fputs(tstr + 1, stdout);
151 			return;
152 		}
153 		putchar('"');
154 	}
155 	TCHECK2(bp->bp_sname[0], 1);		/* check first char only */
156 	if (*bp->bp_file) {
157 		printf(" file \"");
158 		if (fn_print(bp->bp_file, snapend)) {
159 			putchar('"');
160 			fputs(tstr + 1, stdout);
161 			return;
162 		}
163 		putchar('"');
164 	}
165 
166 	/* Decode the vendor buffer */
167 	TCHECK(bp->bp_vend[0]);
168 	length -= sizeof(*bp) - sizeof(bp->bp_vend);
169 	if (memcmp((char *)bp->bp_vend, (char *)vm_rfc1048,
170 		 sizeof(u_int32_t)) == 0)
171 		rfc1048_print(bp->bp_vend, length);
172 	else if (memcmp((char *)bp->bp_vend, (char *)vm_cmu,
173 		      sizeof(u_int32_t)) == 0)
174 		cmu_print(bp->bp_vend, length);
175 	else {
176 		u_int32_t ul;
177 
178 		memcpy((char *)&ul, (char *)bp->bp_vend, sizeof(ul));
179 		if (ul != 0)
180 			printf("vend-#0x%x", ul);
181 	}
182 
183 	return;
184 trunc:
185 	fputs(tstr, stdout);
186 }
187 
188 /* The first character specifies the format to print */
189 static struct tok tag2str[] = {
190 /* RFC1048 tags */
191 	{ TAG_PAD,		" PAD" },
192 	{ TAG_SUBNET_MASK,	"iSM" },	/* subnet mask (RFC950) */
193 	{ TAG_TIME_OFFSET,	"lTZ" },	/* seconds from UTC */
194 	{ TAG_GATEWAY,		"iDG" },	/* default gateway */
195 	{ TAG_TIME_SERVER,	"iTS" },	/* time servers (RFC868) */
196 	{ TAG_NAME_SERVER,	"iIEN" },	/* IEN name servers (IEN116) */
197 	{ TAG_DOMAIN_SERVER,	"iNS" },	/* domain name (RFC1035) */
198 	{ TAG_LOG_SERVER,	"iLOG" },	/* MIT log servers */
199 	{ TAG_COOKIE_SERVER,	"iCS" },	/* cookie servers (RFC865) */
200 	{ TAG_LPR_SERVER,	"iLPR" },	/* lpr server (RFC1179) */
201 	{ TAG_IMPRESS_SERVER,	"iIM" },	/* impress servers (Imagen) */
202 	{ TAG_RLP_SERVER,	"iRL" },	/* resource location (RFC887) */
203 	{ TAG_HOSTNAME,		"aHN" },	/* ascii hostname */
204 	{ TAG_BOOTSIZE,		"sBS" },	/* 512 byte blocks */
205 	{ TAG_END,		" END" },
206 /* RFC1497 tags */
207 	{ TAG_DUMPPATH,		"aDP" },
208 	{ TAG_DOMAINNAME,	"aDN" },
209 	{ TAG_SWAP_SERVER,	"iSS" },
210 	{ TAG_ROOTPATH,		"aRP" },
211 	{ TAG_EXTPATH,		"aEP" },
212 /* RFC2132 tags */
213 	{ TAG_IP_FORWARD,	"BIPF" },
214 	{ TAG_NL_SRCRT,		"BSRT" },
215 	{ TAG_PFILTERS,		"pPF" },
216 	{ TAG_REASS_SIZE,	"sRSZ" },
217 	{ TAG_DEF_TTL,		"bTTL" },
218 	{ TAG_MTU_TIMEOUT,	"lMA" },
219 	{ TAG_MTU_TABLE,	"sMT" },
220 	{ TAG_INT_MTU,		"sMTU" },
221 	{ TAG_LOCAL_SUBNETS,	"BLSN" },
222 	{ TAG_BROAD_ADDR,	"iBR" },
223 	{ TAG_DO_MASK_DISC,	"BMD" },
224 	{ TAG_SUPPLY_MASK,	"BMS" },
225 	{ TAG_DO_RDISC,		"BRD" },
226 	{ TAG_RTR_SOL_ADDR,	"iRSA" },
227 	{ TAG_STATIC_ROUTE,	"pSR" },
228 	{ TAG_USE_TRAILERS,	"BUT" },
229 	{ TAG_ARP_TIMEOUT,	"lAT" },
230 	{ TAG_ETH_ENCAP,	"BIE" },
231 	{ TAG_TCP_TTL,		"bTT" },
232 	{ TAG_TCP_KEEPALIVE,	"lKI" },
233 	{ TAG_KEEPALIVE_GO,	"BKG" },
234 	{ TAG_NIS_DOMAIN,	"aYD" },
235 	{ TAG_NIS_SERVERS,	"iYS" },
236 	{ TAG_NTP_SERVERS,	"iNTP" },
237 	{ TAG_VENDOR_OPTS,	"bVO" },
238 	{ TAG_NETBIOS_NS,	"iWNS" },
239 	{ TAG_NETBIOS_DDS,	"iWDD" },
240 	{ TAG_NETBIOS_NODE,	"bWNT" },
241 	{ TAG_NETBIOS_SCOPE,	"aWSC" },
242 	{ TAG_XWIN_FS,		"iXFS" },
243 	{ TAG_XWIN_DM,		"iXDM" },
244 	{ TAG_NIS_P_DOMAIN,	"sN+D" },
245 	{ TAG_NIS_P_SERVERS,	"iN+S" },
246 	{ TAG_MOBILE_HOME,	"iMH" },
247 	{ TAG_SMPT_SERVER,	"iSMTP" },
248 	{ TAG_POP3_SERVER,	"iPOP3" },
249 	{ TAG_NNTP_SERVER,	"iNNTP" },
250 	{ TAG_WWW_SERVER,	"iWWW" },
251 	{ TAG_FINGER_SERVER,	"iFG" },
252 	{ TAG_IRC_SERVER,	"iIRC" },
253 	{ TAG_STREETTALK_SRVR,	"iSTS" },
254 	{ TAG_STREETTALK_STDA,	"iSTDA" },
255 	{ TAG_REQUESTED_IP,	"iRQ" },
256 	{ TAG_IP_LEASE,		"lLT" },
257 	{ TAG_OPT_OVERLOAD,	"bOO" },
258 	{ TAG_TFTP_SERVER,	"aTFTP" },
259 	{ TAG_BOOTFILENAME,	"aBF" },
260 	{ TAG_DHCP_MESSAGE,	" DHCP" },
261 	{ TAG_SERVER_ID,	"iSID" },
262 	{ TAG_PARM_REQUEST,	"bPR" },
263 	{ TAG_MESSAGE,		"aMSG" },
264 	{ TAG_MAX_MSG_SIZE,	"sMSZ" },
265 	{ TAG_RENEWAL_TIME,	"lRN" },
266 	{ TAG_REBIND_TIME,	"lRB" },
267 	{ TAG_VENDOR_CLASS,	"bVC" },
268 	{ TAG_CLIENT_ID,	"bCID" },
269 	{ 0,			NULL }
270 };
271 
272 static void
273 rfc1048_print(register const u_char *bp, register u_int length)
274 {
275 	register u_char tag;
276 	register u_int len, size;
277 	register const char *cp;
278 	register char c;
279 	int first;
280 	u_int32_t ul;
281 	u_short us;
282 
283 	printf(" vend-rfc1048");
284 
285 	/* Step over magic cookie */
286 	bp += sizeof(int32_t);
287 
288 	/* Loop while we there is a tag left in the buffer */
289 	while (bp + 1 < snapend) {
290 		tag = *bp++;
291 		if (tag == TAG_PAD)
292 			continue;
293 		if (tag == TAG_END)
294 			return;
295 		cp = tok2str(tag2str, "?T%d", tag);
296 		c = *cp++;
297 		printf(" %s:", cp);
298 
299 		/* Get the length; check for truncation */
300 		if (bp + 1 >= snapend) {
301 			fputs(tstr, stdout);
302 			return;
303 		}
304 		len = *bp++;
305 		if (bp + len >= snapend) {
306 			fputs(tstr, stdout);
307 			return;
308 		}
309 
310 		if (tag == TAG_DHCP_MESSAGE && len == 1) {
311 			c = *bp++;
312 			switch (c) {
313 			case DHCPDISCOVER:	printf("DISCOVER");	break;
314 			case DHCPOFFER:		printf("OFFER");	break;
315 			case DHCPREQUEST:	printf("REQUEST");	break;
316 			case DHCPDECLINE:	printf("DECLINE");	break;
317 			case DHCPACK:		printf("ACK");		break;
318 			case DHCPNAK:		printf("NACK");		break;
319 			case DHCPRELEASE:	printf("RELEASE");	break;
320 			case DHCPINFORM:	printf("INFORM");	break;
321 			default:		printf("%u", c);	break;
322 			}
323 			continue;
324 		}
325 
326 		if (tag == TAG_PARM_REQUEST) {
327 			first = 1;
328 			while (len-- > 0) {
329 				c = *bp++;
330 				cp = tok2str(tag2str, "?%d", c);
331 				if (!first)
332 					putchar('+');
333 				printf("%s", cp + 1);
334 				first = 0;
335 			}
336 			continue;
337 		}
338 
339 		/* Print data */
340 		size = len;
341 		if (c == '?') {
342 			/* Base default formats for unknown tags on data size */
343 			if (size & 1)
344 				c = 'b';
345 			else if (size & 2)
346 				c = 's';
347 			else
348 				c = 'l';
349 		}
350 		first = 1;
351 		switch (c) {
352 
353 		case 'a':
354 			/* ascii strings */
355 			putchar('"');
356 			(void)fn_printn(bp, size, NULL);
357 			putchar('"');
358 			bp += size;
359 			size = 0;
360 			break;
361 
362 		case 'i':
363 		case 'l':
364 			/* ip addresses/32-bit words */
365 			while (size >= sizeof(ul)) {
366 				if (!first)
367 					putchar(',');
368 				memcpy((char *)&ul, (char *)bp, sizeof(ul));
369 				if (c == 'i')
370 					printf("%s", ipaddr_string(&ul));
371 				else
372 					printf("%u", ul);
373 				bp += sizeof(ul);
374 				size -= sizeof(ul);
375 				first = 0;
376 			}
377 			break;
378 
379 		case 'p':
380 			/* IP address pairs */
381 			while (size >= 2*sizeof(ul)) {
382 				if (!first)
383 					putchar(',');
384 				memcpy((char *)&ul, (char *)bp, sizeof(ul));
385 				printf("(%s:", ipaddr_string(&ul));
386 				bp += sizeof(ul);
387 				memcpy((char *)&ul, (char *)bp, sizeof(ul));
388 				printf("%s)", ipaddr_string(&ul));
389 				bp += sizeof(ul);
390 				size -= 2*sizeof(ul);
391 				first = 0;
392 			}
393 			break;
394 
395 		case 's':
396 			/* shorts */
397 			while (size >= sizeof(us)) {
398 				if (!first)
399 					putchar(',');
400 				memcpy((char *)&us, (char *)bp, sizeof(us));
401 				printf("%d", us);
402 				bp += sizeof(us);
403 				size -= sizeof(us);
404 				first = 0;
405 			}
406 			break;
407 
408 		case 'B':
409 			/* boolean */
410 			while (size > 0) {
411 				if (!first)
412 					putchar(',');
413 				switch (*bp) {
414 				case 0:
415 					putchar('N');
416 					break;
417 				case 1:
418 					putchar('Y');
419 					break;
420 				default:
421 					printf("%d?", *bp);
422 					break;
423 				}
424 				++bp;
425 				--size;
426 				first = 0;
427 			}
428 			break;
429 
430 		case 'b':
431 		default:
432 			/* Bytes */
433 			while (size > 0) {
434 				if (!first)
435 					putchar('.');
436 				printf("%d", *bp);
437 				++bp;
438 				--size;
439 				first = 0;
440 			}
441 			break;
442 		}
443 		/* Data left over? */
444 		if (size)
445 			printf("[len %d]", len);
446 	}
447 }
448 
449 static void
450 cmu_print(register const u_char *bp, register u_int length)
451 {
452 	register const struct cmu_vend *cmu;
453 	char *fmt = " %s:%s";
454 
455 #define PRINTCMUADDR(m, s) { TCHECK(cmu->m); \
456     if (cmu->m.s_addr != 0) \
457 	printf(fmt, s, ipaddr_string(&cmu->m.s_addr)); }
458 
459 	printf(" vend-cmu");
460 	cmu = (struct cmu_vend *)bp;
461 
462 	/* Only print if there are unknown bits */
463 	TCHECK(cmu->v_flags);
464 	if ((cmu->v_flags & ~(VF_SMASK)) != 0)
465 		printf(" F:0x%x", cmu->v_flags);
466 	PRINTCMUADDR(v_dgate, "DG");
467 	PRINTCMUADDR(v_smask, cmu->v_flags & VF_SMASK ? "SM" : "SM*");
468 	PRINTCMUADDR(v_dns1, "NS1");
469 	PRINTCMUADDR(v_dns2, "NS2");
470 	PRINTCMUADDR(v_ins1, "IEN1");
471 	PRINTCMUADDR(v_ins2, "IEN2");
472 	PRINTCMUADDR(v_ts1, "TS1");
473 	PRINTCMUADDR(v_ts2, "TS2");
474 	return;
475 
476 trunc:
477 	fputs(tstr, stdout);
478 #undef PRINTCMUADDR
479 }
480