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