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