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