print-bootp.c revision 1.5 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 (at) ee.lbl.gov>
25 */
26
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: print-bootp.c,v 1.5 1998/03/26 06:44:19 thorpej Exp $");
30 /* 93/10/10 <gwr (at) mc.com> New data-driven option print routine. */
31 #endif
32
33 #include <stdio.h>
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <string.h>
41 #include <ctype.h>
42
43 #include "bootp.h"
44 #include "bootptest.h"
45
46 #ifdef __STDC__
47 #define P(args) args
48 #else
49 #define P(args) ()
50 #endif
51
52 /* These decode the vendor data. */
53 static void cmu_print P((u_char *, int));
54 static void dump_hex P((u_char *, int));
55 static void other_print P((u_char *, int));
56 static void rfc1048_print P((u_char *, int));
57
58 #undef P
59
60 /*
61 * Print bootp requests
62 */
63 void
64 bootp_print(bp, length, sport, dport)
65 struct bootp *bp;
66 int length;
67 u_short sport, dport;
68 {
69 static char tstr[] = " [|bootp]";
70 static unsigned char vm_cmu[4] = VM_CMU;
71 static unsigned char vm_rfc1048[4] = VM_RFC1048;
72 u_char *ep;
73 int vdlen;
74
75 #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
76
77 /* Note funny sized packets */
78 if (length != sizeof(struct bootp))
79 (void) printf(" [len=%d]", length);
80
81 /* 'ep' points to the end of avaible data. */
82 ep = (u_char *) snapend;
83
84 switch (bp->bp_op) {
85
86 case BOOTREQUEST:
87 /* Usually, a request goes from a client to a server */
88 if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
89 printf(" (request)");
90 break;
91
92 case BOOTREPLY:
93 /* Usually, a reply goes from a server to a client */
94 if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
95 printf(" (reply)");
96 break;
97
98 default:
99 printf(" bootp-#%d", bp->bp_op);
100 }
101
102 /* The usual hardware address type is 1 (10Mb Ethernet) */
103 if (bp->bp_htype != 1)
104 printf(" htype:%d", bp->bp_htype);
105
106 /* The usual length for 10Mb Ethernet address is 6 bytes */
107 if (bp->bp_hlen != 6)
108 printf(" hlen:%d", bp->bp_hlen);
109
110 /* Client's Hardware address */
111 if (bp->bp_hlen) {
112 register struct ether_header *eh;
113 register char *e;
114
115 TCHECK(bp->bp_chaddr[0], 6);
116 eh = (struct ether_header *) packetp;
117 if (bp->bp_op == BOOTREQUEST)
118 e = (char *) ESRC(eh);
119 else if (bp->bp_op == BOOTREPLY)
120 e = (char *) EDST(eh);
121 else
122 e = 0;
123 if (e == 0 || bcmp((char *) bp->bp_chaddr, e, 6))
124 dump_hex(bp->bp_chaddr, bp->bp_hlen);
125 }
126 /* Only print interesting fields */
127 if (bp->bp_hops)
128 printf(" hops:%d", bp->bp_hops);
129
130 if (bp->bp_xid)
131 printf(" xid:%d", ntohl(bp->bp_xid));
132
133 if (bp->bp_secs)
134 printf(" secs:%d", ntohs(bp->bp_secs));
135
136 /* Client's ip address */
137 TCHECK(bp->bp_ciaddr, sizeof(bp->bp_ciaddr));
138 if (bp->bp_ciaddr.s_addr)
139 printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
140
141 /* 'your' ip address (bootp client) */
142 TCHECK(bp->bp_yiaddr, sizeof(bp->bp_yiaddr));
143 if (bp->bp_yiaddr.s_addr)
144 printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
145
146 /* Server's ip address */
147 TCHECK(bp->bp_siaddr, sizeof(bp->bp_siaddr));
148 if (bp->bp_siaddr.s_addr)
149 printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
150
151 /* Gateway's ip address */
152 TCHECK(bp->bp_giaddr, sizeof(bp->bp_giaddr));
153 if (bp->bp_giaddr.s_addr)
154 printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
155
156 TCHECK(bp->bp_sname[0], sizeof(bp->bp_sname));
157 if (*bp->bp_sname) {
158 printf(" sname:");
159 if (printfn(bp->bp_sname, ep)) {
160 fputs(tstr + 1, stdout);
161 return;
162 }
163 }
164 TCHECK(bp->bp_file[0], sizeof(bp->bp_file));
165 if (*bp->bp_file) {
166 printf(" file:");
167 if (printfn(bp->bp_file, ep)) {
168 fputs(tstr + 1, stdout);
169 return;
170 }
171 }
172 /* Don't try to decode the vendor buffer unless we're verbose */
173 if (vflag <= 0)
174 return;
175
176 vdlen = sizeof(bp->bp_vend);
177 /* Vendor data can extend to the end of the packet. */
178 if (vdlen < (ep - bp->bp_vend))
179 vdlen = (ep - bp->bp_vend);
180
181 TCHECK(bp->bp_vend[0], vdlen);
182 printf(" vend");
183 if (!bcmp(bp->bp_vend, vm_rfc1048, sizeof(u_int32)))
184 rfc1048_print(bp->bp_vend, vdlen);
185 else if (!bcmp(bp->bp_vend, vm_cmu, sizeof(u_int32)))
186 cmu_print(bp->bp_vend, vdlen);
187 else
188 other_print(bp->bp_vend, vdlen);
189
190 return;
191 trunc:
192 fputs(tstr, stdout);
193 #undef TCHECK
194 }
195
196 /*
198 * Option description data follows.
199 * These are decribed in: RFC-1048, RFC-1395, RFC-1497, RFC-1533
200 *
201 * The first char of each option string encodes the data format:
202 * ?: unknown
203 * a: ASCII
204 * b: byte (8-bit)
205 * i: inet address
206 * l: int32
207 * s: short (16-bit)
208 */
209 char *
210 rfc1048_opts[] = {
211 /* Originally from RFC-1048: */
212 "?PAD", /* 0: Padding - special, no data. */
213 "iSM", /* 1: subnet mask (RFC950)*/
214 "lTZ", /* 2: time offset, seconds from UTC */
215 "iGW", /* 3: gateways (or routers) */
216 "iTS", /* 4: time servers (RFC868) */
217 "iINS", /* 5: IEN name servers (IEN116) */
218 "iDNS", /* 6: domain name servers (RFC1035)(1034?) */
219 "iLOG", /* 7: MIT log servers */
220 "iCS", /* 8: cookie servers (RFC865) */
221 "iLPR", /* 9: lpr server (RFC1179) */
222 "iIPS", /* 10: impress servers (Imagen) */
223 "iRLP", /* 11: resource location servers (RFC887) */
224 "aHN", /* 12: host name (ASCII) */
225 "sBFS", /* 13: boot file size (in 512 byte blocks) */
226
227 /* Added by RFC-1395: */
228 "aDUMP", /* 14: Merit Dump File */
229 "aDNAM", /* 15: Domain Name (for DNS) */
230 "iSWAP", /* 16: Swap Server */
231 "aROOT", /* 17: Root Path */
232
233 /* Added by RFC-1497: */
234 "aEXTF", /* 18: Extensions Path (more options) */
235
236 /* Added by RFC-1533: (many, many options...) */
237 #if 1 /* These might not be worth recognizing by name. */
238
239 /* IP Layer Parameters, per-host (RFC-1533, sect. 4) */
240 "bIP-forward", /* 19: IP Forwarding flag */
241 "bIP-srcroute", /* 20: IP Source Routing Enable flag */
242 "iIP-filters", /* 21: IP Policy Filter (addr pairs) */
243 "sIP-maxudp", /* 22: IP Max-UDP reassembly size */
244 "bIP-ttlive", /* 23: IP Time to Live */
245 "lIP-pmtuage", /* 24: IP Path MTU aging timeout */
246 "sIP-pmtutab", /* 25: IP Path MTU plateau table */
247
248 /* IP parameters, per-interface (RFC-1533, sect. 5) */
249 "sIP-mtu-sz", /* 26: IP MTU size */
250 "bIP-mtu-sl", /* 27: IP MTU all subnets local */
251 "bIP-bcast1", /* 28: IP Broadcast Addr ones flag */
252 "bIP-mask-d", /* 29: IP do mask discovery */
253 "bIP-mask-s", /* 30: IP do mask supplier */
254 "bIP-rt-dsc", /* 31: IP do router discovery */
255 "iIP-rt-sa", /* 32: IP router solicitation addr */
256 "iIP-routes", /* 33: IP static routes (dst,router) */
257
258 /* Link Layer parameters, per-interface (RFC-1533, sect. 6) */
259 "bLL-trailer", /* 34: do tralier encapsulation */
260 "lLL-arp-tmo", /* 35: ARP cache timeout */
261 "bLL-ether2", /* 36: Ethernet version 2 (IEEE 802.3) */
262
263 /* TCP parameters (RFC-1533, sect. 7) */
264 "bTCP-def-ttl", /* 37: default time to live */
265 "lTCP-KA-tmo", /* 38: keepalive time interval */
266 "bTCP-KA-junk", /* 39: keepalive sends extra junk */
267
268 /* Application and Service Parameters (RFC-1533, sect. 8) */
269 "aNISDOM", /* 40: NIS Domain (Sun YP) */
270 "iNISSRV", /* 41: NIS Servers */
271 "iNTPSRV", /* 42: NTP (time) Servers (RFC 1129) */
272 "?VSINFO", /* 43: Vendor Specific Info (encapsulated) */
273 "iNBiosNS", /* 44: NetBIOS Name Server (RFC-1001,1..2) */
274 "iNBiosDD", /* 45: NetBIOS Datagram Dist. Server. */
275 "bNBiosNT", /* 46: NetBIOS Note Type */
276 "?NBiosS", /* 47: NetBIOS Scope */
277 "iXW-FS", /* 48: X Window System Font Servers */
278 "iXW-DM", /* 49: X Window System Display Managers */
279
280 /* DHCP extensions (RFC-1533, sect. 9) */
281 #endif
282 };
283 #define KNOWN_OPTIONS (sizeof(rfc1048_opts) / sizeof(rfc1048_opts[0]))
284
285 static void
286 rfc1048_print(bp, length)
287 register u_char *bp;
288 int length;
289 {
290 u_char tag;
291 u_char *ep;
292 register int len;
293 u_int32 ul;
294 u_short us;
295 struct in_addr ia;
296 char *optstr;
297
298 printf("-rfc1395");
299
300 /* Step over magic cookie */
301 bp += sizeof(int32);
302 /* Setup end pointer */
303 ep = bp + length;
304 while (bp < ep) {
305 tag = *bp++;
306 /* Check for tags with no data first. */
307 if (tag == TAG_PAD)
308 continue;
309 if (tag == TAG_END)
310 return;
311 if (tag < KNOWN_OPTIONS) {
312 optstr = rfc1048_opts[tag];
313 printf(" %s:", optstr + 1);
314 } else {
315 printf(" T%d:", tag);
316 optstr = "?";
317 }
318 /* Now scan the length byte. */
319 len = *bp++;
320 if (bp + len > ep) {
321 /* truncated option */
322 printf(" |(%d>%ld)", len, (long)(ep - bp));
323 return;
324 }
325 /* Print the option value(s). */
326 switch (optstr[0]) {
327
328 case 'a': /* ASCII string */
329 printfn(bp, bp + len);
330 bp += len;
331 len = 0;
332 break;
333
334 case 's': /* Word formats */
335 while (len >= 2) {
336 bcopy((char *) bp, (char *) &us, 2);
337 printf("%d", ntohs(us));
338 bp += 2;
339 len -= 2;
340 if (len) printf(",");
341 }
342 if (len) printf("(junk=%d)", len);
343 break;
344
345 case 'l': /* Long words */
346 while (len >= 4) {
347 bcopy((char *) bp, (char *) &ul, 4);
348 printf("%d", ntohl(ul));
349 bp += 4;
350 len -= 4;
351 if (len) printf(",");
352 }
353 if (len) printf("(junk=%d)", len);
354 break;
355
356 case 'i': /* INET addresses */
357 while (len >= 4) {
358 bcopy((char *) bp, (char *) &ia, 4);
359 printf("%s", ipaddr_string(&ia));
360 bp += 4;
361 len -= 4;
362 if (len) printf(",");
363 }
364 if (len) printf("(junk=%d)", len);
365 break;
366
367 case 'b':
368 default:
369 break;
370
371 } /* switch */
372
373 /* Print as characters, if appropriate. */
374 if (len) {
375 dump_hex(bp, len);
376 if (isascii(*bp) && isprint(*bp)) {
377 printf("(");
378 printfn(bp, bp + len);
379 printf(")");
380 }
381 bp += len;
382 len = 0;
383 }
384 } /* while bp < ep */
385 }
386
387 static void
388 cmu_print(bp, length)
389 register u_char *bp;
390 int length;
391 {
392 struct cmu_vend *v;
393 u_char *ep;
394
395 printf("-cmu");
396
397 v = (struct cmu_vend *) bp;
398 if (length < sizeof(*v)) {
399 printf(" |L=%d", length);
400 return;
401 }
402 /* Setup end pointer */
403 ep = bp + length;
404
405 /* Subnet mask */
406 if (v->v_flags & VF_SMASK) {
407 printf(" SM:%s", ipaddr_string(&v->v_smask));
408 }
409 /* Default gateway */
410 if (v->v_dgate.s_addr)
411 printf(" GW:%s", ipaddr_string(&v->v_dgate));
412
413 /* Domain name servers */
414 if (v->v_dns1.s_addr)
415 printf(" DNS1:%s", ipaddr_string(&v->v_dns1));
416 if (v->v_dns2.s_addr)
417 printf(" DNS2:%s", ipaddr_string(&v->v_dns2));
418
419 /* IEN-116 name servers */
420 if (v->v_ins1.s_addr)
421 printf(" INS1:%s", ipaddr_string(&v->v_ins1));
422 if (v->v_ins2.s_addr)
423 printf(" INS2:%s", ipaddr_string(&v->v_ins2));
424
425 /* Time servers */
426 if (v->v_ts1.s_addr)
427 printf(" TS1:%s", ipaddr_string(&v->v_ts1));
428 if (v->v_ts2.s_addr)
429 printf(" TS2:%s", ipaddr_string(&v->v_ts2));
430
431 }
432
433
434 /*
435 * Print out arbitrary, unknown vendor data.
436 */
437
438 static void
439 other_print(bp, length)
440 register u_char *bp;
441 int length;
442 {
443 u_char *ep; /* end pointer */
444 u_char *zp; /* points one past last non-zero byte */
445
446 /* Setup end pointer */
447 ep = bp + length;
448
449 /* Find the last non-zero byte. */
450 for (zp = ep; zp > bp; zp--) {
451 if (zp[-1] != 0)
452 break;
453 }
454
455 /* Print the all-zero case in a compact representation. */
456 if (zp == bp) {
457 printf("-all-zero");
458 return;
459 }
460 printf("-unknown");
461
462 /* Are there enough trailing zeros to make "00..." worthwhile? */
463 if (zp + 2 > ep)
464 zp = ep; /* print them all normally */
465
466 /* Now just print all the non-zero data. */
467 while (bp < zp) {
468 printf(".%02X", *bp);
469 bp++;
470 }
471
472 if (zp < ep)
473 printf(".00...");
474
475 return;
476 }
477
478 static void
479 dump_hex(bp, len)
480 u_char *bp;
481 int len;
482 {
483 while (len > 0) {
484 printf("%02X", *bp);
485 bp++;
486 len--;
487 if (len) printf(".");
488 }
489 }
490
491 /*
492 * Local Variables:
493 * tab-width: 4
494 * c-indent-level: 4
495 * c-argdecl-indent: 4
496 * c-continued-statement-offset: 4
497 * c-continued-brace-offset: -4
498 * c-label-offset: -4
499 * c-brace-offset: 0
500 * End:
501 */
502