]> The Tcpdump Group git mirrors - tcpdump/blob - print-bootp.c
Added support for Win32, based on WinPcap.
[tcpdump] / print-bootp.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. 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 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/tcpdump/print-bootp.c,v 1.64 2002-08-01 08:53:02 risso Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <tcpdump-stdinc.h>
33
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "extract.h"
41 #include "ether.h"
42 #include "bootp.h"
43
44 static void rfc1048_print(const u_char *);
45 static void cmu_print(const u_char *);
46
47 static char tstr[] = " [|bootp]";
48
49 /*
50 * Print bootp requests
51 */
52 void
53 bootp_print(register const u_char *cp, u_int length,
54 u_short sport, u_short dport)
55 {
56 register const struct bootp *bp;
57 static const u_char vm_cmu[4] = VM_CMU;
58 static const u_char vm_rfc1048[4] = VM_RFC1048;
59
60 bp = (const struct bootp *)cp;
61 TCHECK(bp->bp_op);
62 switch (bp->bp_op) {
63
64 case BOOTREQUEST:
65 /* Usually, a request goes from a client to a server */
66 if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
67 printf(" (request)");
68 break;
69
70 case BOOTREPLY:
71 /* Usually, a reply goes from a server to a client */
72 if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
73 printf(" (reply)");
74 break;
75
76 default:
77 printf(" bootp-#%d", bp->bp_op);
78 }
79
80 TCHECK(bp->bp_secs);
81
82 /* The usual hardware address type is 1 (10Mb Ethernet) */
83 if (bp->bp_htype != 1)
84 printf(" htype-#%d", bp->bp_htype);
85
86 /* The usual length for 10Mb Ethernet address is 6 bytes */
87 if (bp->bp_htype != 1 || bp->bp_hlen != 6)
88 printf(" hlen:%d", bp->bp_hlen);
89
90 /* Only print interesting fields */
91 if (bp->bp_hops)
92 printf(" hops:%d", bp->bp_hops);
93 if (bp->bp_xid)
94 printf(" xid:0x%x", (u_int32_t)ntohl(bp->bp_xid));
95 if (bp->bp_secs)
96 printf(" secs:%d", ntohs(bp->bp_secs));
97 if (bp->bp_flags)
98 printf(" flags:0x%x", ntohs(bp->bp_flags));
99
100 /* Client's ip address */
101 TCHECK(bp->bp_ciaddr);
102 if (bp->bp_ciaddr.s_addr)
103 printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
104
105 /* 'your' ip address (bootp client) */
106 TCHECK(bp->bp_yiaddr);
107 if (bp->bp_yiaddr.s_addr)
108 printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
109
110 /* Server's ip address */
111 TCHECK(bp->bp_siaddr);
112 if (bp->bp_siaddr.s_addr)
113 printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
114
115 /* Gateway's ip address */
116 TCHECK(bp->bp_giaddr);
117 if (bp->bp_giaddr.s_addr)
118 printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
119
120 /* Client's Ethernet address */
121 if (bp->bp_htype == 1 && bp->bp_hlen == 6) {
122 register const struct ether_header *eh;
123 register const char *e;
124
125 TCHECK2(bp->bp_chaddr[0], 6);
126 eh = (const struct ether_header *)packetp;
127 if (bp->bp_op == BOOTREQUEST)
128 e = (const char *)ESRC(eh);
129 else if (bp->bp_op == BOOTREPLY)
130 e = (const char *)EDST(eh);
131 else
132 e = 0;
133 if (e == 0 || memcmp((const char *)bp->bp_chaddr, e, 6) != 0)
134 printf(" ether %s", etheraddr_string(bp->bp_chaddr));
135 }
136
137 TCHECK2(bp->bp_sname[0], 1); /* check first char only */
138 if (*bp->bp_sname) {
139 printf(" sname \"");
140 if (fn_print(bp->bp_sname, snapend)) {
141 putchar('"');
142 fputs(tstr + 1, stdout);
143 return;
144 }
145 putchar('"');
146 }
147 TCHECK2(bp->bp_sname[0], 1); /* check first char only */
148 if (*bp->bp_file) {
149 printf(" file \"");
150 if (fn_print(bp->bp_file, snapend)) {
151 putchar('"');
152 fputs(tstr + 1, stdout);
153 return;
154 }
155 putchar('"');
156 }
157
158 /* Decode the vendor buffer */
159 TCHECK(bp->bp_vend[0]);
160 if (memcmp((const char *)bp->bp_vend, vm_rfc1048,
161 sizeof(u_int32_t)) == 0)
162 rfc1048_print(bp->bp_vend);
163 else if (memcmp((const char *)bp->bp_vend, vm_cmu,
164 sizeof(u_int32_t)) == 0)
165 cmu_print(bp->bp_vend);
166 else {
167 u_int32_t ul;
168
169 ul = EXTRACT_32BITS(&bp->bp_vend);
170 if (ul != 0)
171 printf("vend-#0x%x", ul);
172 }
173
174 return;
175 trunc:
176 fputs(tstr, stdout);
177 }
178
179 /*
180 * The first character specifies the format to print:
181 * i - ip address (32 bits)
182 * p - ip address pairs (32 bits + 32 bits)
183 * l - long (32 bits)
184 * L - unsigned long (32 bits)
185 * s - short (16 bits)
186 * b - period-seperated decimal bytes (variable length)
187 * x - colon-seperated hex bytes (variable length)
188 * a - ascii string (variable length)
189 * B - on/off (8 bits)
190 * $ - special (explicit code to handle)
191 */
192 static struct tok tag2str[] = {
193 /* RFC1048 tags */
194 { TAG_PAD, " PAD" },
195 { TAG_SUBNET_MASK, "iSM" }, /* subnet mask (RFC950) */
196 { TAG_TIME_OFFSET, "LTZ" }, /* seconds from UTC */
197 { TAG_GATEWAY, "iDG" }, /* default gateway */
198 { TAG_TIME_SERVER, "iTS" }, /* time servers (RFC868) */
199 { TAG_NAME_SERVER, "iIEN" }, /* IEN name servers (IEN116) */
200 { TAG_DOMAIN_SERVER, "iNS" }, /* domain name (RFC1035) */
201 { TAG_LOG_SERVER, "iLOG" }, /* MIT log servers */
202 { TAG_COOKIE_SERVER, "iCS" }, /* cookie servers (RFC865) */
203 { TAG_LPR_SERVER, "iLPR" }, /* lpr server (RFC1179) */
204 { TAG_IMPRESS_SERVER, "iIM" }, /* impress servers (Imagen) */
205 { TAG_RLP_SERVER, "iRL" }, /* resource location (RFC887) */
206 { TAG_HOSTNAME, "aHN" }, /* ascii hostname */
207 { TAG_BOOTSIZE, "sBS" }, /* 512 byte blocks */
208 { TAG_END, " END" },
209 /* RFC1497 tags */
210 { TAG_DUMPPATH, "aDP" },
211 { TAG_DOMAINNAME, "aDN" },
212 { TAG_SWAP_SERVER, "iSS" },
213 { TAG_ROOTPATH, "aRP" },
214 { TAG_EXTPATH, "aEP" },
215 /* RFC2132 tags */
216 { TAG_IP_FORWARD, "BIPF" },
217 { TAG_NL_SRCRT, "BSRT" },
218 { TAG_PFILTERS, "pPF" },
219 { TAG_REASS_SIZE, "sRSZ" },
220 { TAG_DEF_TTL, "bTTL" },
221 { TAG_MTU_TIMEOUT, "lMA" },
222 { TAG_MTU_TABLE, "sMT" },
223 { TAG_INT_MTU, "sMTU" },
224 { TAG_LOCAL_SUBNETS, "BLSN" },
225 { TAG_BROAD_ADDR, "iBR" },
226 { TAG_DO_MASK_DISC, "BMD" },
227 { TAG_SUPPLY_MASK, "BMS" },
228 { TAG_DO_RDISC, "BRD" },
229 { TAG_RTR_SOL_ADDR, "iRSA" },
230 { TAG_STATIC_ROUTE, "pSR" },
231 { TAG_USE_TRAILERS, "BUT" },
232 { TAG_ARP_TIMEOUT, "lAT" },
233 { TAG_ETH_ENCAP, "BIE" },
234 { TAG_TCP_TTL, "bTT" },
235 { TAG_TCP_KEEPALIVE, "lKI" },
236 { TAG_KEEPALIVE_GO, "BKG" },
237 { TAG_NIS_DOMAIN, "aYD" },
238 { TAG_NIS_SERVERS, "iYS" },
239 { TAG_NTP_SERVERS, "iNTP" },
240 { TAG_VENDOR_OPTS, "bVO" },
241 { TAG_NETBIOS_NS, "iWNS" },
242 { TAG_NETBIOS_DDS, "iWDD" },
243 { TAG_NETBIOS_NODE, "$WNT" },
244 { TAG_NETBIOS_SCOPE, "aWSC" },
245 { TAG_XWIN_FS, "iXFS" },
246 { TAG_XWIN_DM, "iXDM" },
247 { TAG_NIS_P_DOMAIN, "sN+D" },
248 { TAG_NIS_P_SERVERS, "iN+S" },
249 { TAG_MOBILE_HOME, "iMH" },
250 { TAG_SMPT_SERVER, "iSMTP" },
251 { TAG_POP3_SERVER, "iPOP3" },
252 { TAG_NNTP_SERVER, "iNNTP" },
253 { TAG_WWW_SERVER, "iWWW" },
254 { TAG_FINGER_SERVER, "iFG" },
255 { TAG_IRC_SERVER, "iIRC" },
256 { TAG_STREETTALK_SRVR, "iSTS" },
257 { TAG_STREETTALK_STDA, "iSTDA" },
258 { TAG_REQUESTED_IP, "iRQ" },
259 { TAG_IP_LEASE, "lLT" },
260 { TAG_OPT_OVERLOAD, "$OO" },
261 { TAG_TFTP_SERVER, "aTFTP" },
262 { TAG_BOOTFILENAME, "aBF" },
263 { TAG_DHCP_MESSAGE, " DHCP" },
264 { TAG_SERVER_ID, "iSID" },
265 { TAG_PARM_REQUEST, "bPR" },
266 { TAG_MESSAGE, "aMSG" },
267 { TAG_MAX_MSG_SIZE, "sMSZ" },
268 { TAG_RENEWAL_TIME, "lRN" },
269 { TAG_REBIND_TIME, "lRB" },
270 { TAG_VENDOR_CLASS, "aVC" },
271 { TAG_CLIENT_ID, "$CID" },
272 /* RFC 2485 */
273 { TAG_OPEN_GROUP_UAP, "aUAP" },
274 /* RFC 2563 */
275 { TAG_DISABLE_AUTOCONF, "BNOAUTO" },
276 /* RFC 2610 */
277 { TAG_SLP_DA, "bSLP-DA" }, /*"b" is a little wrong */
278 { TAG_SLP_SCOPE, "bSLP-SCOPE" }, /*"b" is a little wrong */
279 /* RFC 2937 */
280 { TAG_NS_SEARCH, "sNSSEARCH" }, /* XXX 's' */
281 /* RFC 3011 */
282 { TAG_IP4_SUBNET_SELECT, "iSUBNET" },
283 /* ftp://ftp.isi.edu/.../assignments/bootp-dhcp-extensions */
284 { TAG_USER_CLASS, "aCLASS" },
285 { TAG_SLP_NAMING_AUTH, "aSLP-NA" },
286 { TAG_CLIENT_FQDN, "$FQDN" },
287 { TAG_AGENT_CIRCUIT, "bACKT" },
288 { TAG_AGENT_REMOTE, "bARMT" },
289 { TAG_AGENT_MASK, "bAMSK" },
290 { TAG_TZ_STRING, "aTZSTR" },
291 { TAG_FQDN_OPTION, "bFQDNS" }, /* XXX 'b' */
292 { TAG_AUTH, "bAUTH" }, /* XXX 'b' */
293 { TAG_VINES_SERVERS, "iVINES" },
294 { TAG_SERVER_RANK, "sRANK" },
295 { TAG_CLIENT_ARCH, "sARCH" },
296 { TAG_CLIENT_NDI, "bNDI" }, /* XXX 'b' */
297 { TAG_CLIENT_GUID, "bGUID" }, /* XXX 'b' */
298 { TAG_LDAP_URL, "aLDAP" },
299 { TAG_6OVER4, "i6o4" },
300 { TAG_PRINTER_NAME, "aPRTR" },
301 { TAG_MDHCP_SERVER, "bMDHCP" }, /* XXX 'b' */
302 { TAG_IPX_COMPAT, "bIPX" }, /* XXX 'b' */
303 { TAG_NETINFO_PARENT, "iNI" },
304 { TAG_NETINFO_PARENT_TAG, "aNITAG" },
305 { TAG_URL, "aURL" },
306 { TAG_FAILOVER, "bFAIL" }, /* XXX 'b' */
307 { 0, NULL }
308 };
309 /* 2-byte extended tags */
310 static struct tok xtag2str[] = {
311 { 0, NULL }
312 };
313
314 /* DHCP "options overload" types */
315 static struct tok oo2str[] = {
316 { 1, "file" },
317 { 2, "sname" },
318 { 3, "file+sname" },
319 { 0, NULL }
320 };
321
322 /* NETBIOS over TCP/IP node type options */
323 static struct tok nbo2str[] = {
324 { 0x1, "b-node" },
325 { 0x2, "p-node" },
326 { 0x4, "m-node" },
327 { 0x8, "h-node" },
328 { 0, NULL }
329 };
330
331 /* ARP Hardware types, for Client-ID option */
332 static struct tok arp2str[] = {
333 { 0x1, "ether" },
334 { 0x6, "ieee802" },
335 { 0x7, "arcnet" },
336 { 0xf, "frelay" },
337 { 0x17, "strip" },
338 { 0x18, "ieee1394" },
339 { 0, NULL }
340 };
341
342 static void
343 rfc1048_print(register const u_char *bp)
344 {
345 register u_int16_t tag;
346 register u_int len, size;
347 register const char *cp;
348 register char c;
349 int first;
350 u_int32_t ul;
351 u_int16_t us;
352 u_int8_t uc;
353
354 printf(" vend-rfc1048");
355
356 /* Step over magic cookie */
357 bp += sizeof(int32_t);
358
359 /* Loop while we there is a tag left in the buffer */
360 while (bp + 1 < snapend) {
361 tag = *bp++;
362 if (tag == TAG_PAD)
363 continue;
364 if (tag == TAG_END)
365 return;
366 if (tag == TAG_EXTENDED_OPTION) {
367 TCHECK2(*(bp + 1), 2);
368 tag = EXTRACT_16BITS(bp + 1);
369 /* XXX we don't know yet if the IANA will
370 * preclude overlap of 1-byte and 2-byte spaces.
371 * If not, we need to offset tag after this step.
372 */
373 cp = tok2str(xtag2str, "?xT%u", tag);
374 } else
375 cp = tok2str(tag2str, "?T%u", tag);
376 c = *cp++;
377 printf(" %s:", cp);
378
379 /* Get the length; check for truncation */
380 if (bp + 1 >= snapend) {
381 fputs(tstr, stdout);
382 return;
383 }
384 len = *bp++;
385 if (bp + len >= snapend) {
386 fputs(tstr, stdout);
387 return;
388 }
389
390 if (tag == TAG_DHCP_MESSAGE && len == 1) {
391 uc = *bp++;
392 switch (uc) {
393 case DHCPDISCOVER: printf("DISCOVER"); break;
394 case DHCPOFFER: printf("OFFER"); break;
395 case DHCPREQUEST: printf("REQUEST"); break;
396 case DHCPDECLINE: printf("DECLINE"); break;
397 case DHCPACK: printf("ACK"); break;
398 case DHCPNAK: printf("NACK"); break;
399 case DHCPRELEASE: printf("RELEASE"); break;
400 case DHCPINFORM: printf("INFORM"); break;
401 default: printf("%u", uc); break;
402 }
403 continue;
404 }
405
406 if (tag == TAG_PARM_REQUEST) {
407 first = 1;
408 while (len-- > 0) {
409 uc = *bp++;
410 cp = tok2str(tag2str, "?T%u", uc);
411 if (!first)
412 putchar('+');
413 printf("%s", cp + 1);
414 first = 0;
415 }
416 continue;
417 }
418 if (tag == TAG_EXTENDED_REQUEST) {
419 first = 1;
420 while (len > 1) {
421 len -= 2;
422 us = EXTRACT_16BITS(bp);
423 bp += 2;
424 cp = tok2str(xtag2str, "?xT%u", us);
425 if (!first)
426 putchar('+');
427 printf("%s", cp + 1);
428 first = 0;
429 }
430 continue;
431 }
432
433 /* Print data */
434 size = len;
435 if (c == '?') {
436 /* Base default formats for unknown tags on data size */
437 if (size & 1)
438 c = 'b';
439 else if (size & 2)
440 c = 's';
441 else
442 c = 'l';
443 }
444 first = 1;
445 switch (c) {
446
447 case 'a':
448 /* ascii strings */
449 putchar('"');
450 (void)fn_printn(bp, size, NULL);
451 putchar('"');
452 bp += size;
453 size = 0;
454 break;
455
456 case 'i':
457 case 'l':
458 case 'L':
459 /* ip addresses/32-bit words */
460 while (size >= sizeof(ul)) {
461 if (!first)
462 putchar(',');
463 ul = EXTRACT_32BITS(bp);
464 if (c == 'i') {
465 ul = htonl(ul);
466 printf("%s", ipaddr_string(&ul));
467 } else if (c == 'L')
468 printf("%d", ul);
469 else
470 printf("%u", ul);
471 bp += sizeof(ul);
472 size -= sizeof(ul);
473 first = 0;
474 }
475 break;
476
477 case 'p':
478 /* IP address pairs */
479 while (size >= 2*sizeof(ul)) {
480 if (!first)
481 putchar(',');
482 memcpy((char *)&ul, (const char *)bp, sizeof(ul));
483 printf("(%s:", ipaddr_string(&ul));
484 bp += sizeof(ul);
485 memcpy((char *)&ul, (const char *)bp, sizeof(ul));
486 printf("%s)", ipaddr_string(&ul));
487 bp += sizeof(ul);
488 size -= 2*sizeof(ul);
489 first = 0;
490 }
491 break;
492
493 case 's':
494 /* shorts */
495 while (size >= sizeof(us)) {
496 if (!first)
497 putchar(',');
498 us = EXTRACT_16BITS(bp);
499 printf("%u", us);
500 bp += sizeof(us);
501 size -= sizeof(us);
502 first = 0;
503 }
504 break;
505
506 case 'B':
507 /* boolean */
508 while (size > 0) {
509 if (!first)
510 putchar(',');
511 switch (*bp) {
512 case 0:
513 putchar('N');
514 break;
515 case 1:
516 putchar('Y');
517 break;
518 default:
519 printf("%u?", *bp);
520 break;
521 }
522 ++bp;
523 --size;
524 first = 0;
525 }
526 break;
527
528 case 'b':
529 case 'x':
530 default:
531 /* Bytes */
532 while (size > 0) {
533 if (!first)
534 putchar(c == 'x' ? ':' : '.');
535 if (c == 'x')
536 printf("%02x", *bp);
537 else
538 printf("%u", *bp);
539 ++bp;
540 --size;
541 first = 0;
542 }
543 break;
544
545 case '$':
546 /* Guys we can't handle with one of the usual cases */
547 switch (tag) {
548
549 case TAG_NETBIOS_NODE:
550 tag = *bp++;
551 --size;
552 fputs(tok2str(nbo2str, NULL, tag), stdout);
553 break;
554
555 case TAG_OPT_OVERLOAD:
556 tag = *bp++;
557 --size;
558 fputs(tok2str(oo2str, NULL, tag), stdout);
559 break;
560
561 case TAG_CLIENT_FQDN:
562 if (*bp++)
563 printf("[svrreg]");
564 if (*bp)
565 printf("%u/%u/", *bp, *(bp+1));
566 bp += 2;
567 putchar('"');
568 (void)fn_printn(bp, size - 3, NULL);
569 putchar('"');
570 bp += size - 3;
571 size = 0;
572 break;
573
574 case TAG_CLIENT_ID:
575 { int type = *bp++;
576 size--;
577 if (type == 0) {
578 putchar('"');
579 (void)fn_printn(bp, size, NULL);
580 putchar('"');
581 break;
582 } else {
583 printf("[%s]", tok2str(arp2str, "type-%d", type));
584 }
585 while (size > 0) {
586 if (!first)
587 putchar(':');
588 printf("%02x", *bp);
589 ++bp;
590 --size;
591 first = 0;
592 }
593 break;
594 }
595
596 default:
597 printf("[unknown special tag %u, size %u]",
598 tag, size);
599 bp += size;
600 size = 0;
601 break;
602 }
603 break;
604 }
605 /* Data left over? */
606 if (size)
607 printf("[len %u]", len);
608 }
609 return;
610 trunc:
611 printf("|[rfc1048]");
612 }
613
614 static void
615 cmu_print(register const u_char *bp)
616 {
617 register const struct cmu_vend *cmu;
618
619 #define PRINTCMUADDR(m, s) { TCHECK(cmu->m); \
620 if (cmu->m.s_addr != 0) \
621 printf(" %s:%s", s, ipaddr_string(&cmu->m.s_addr)); }
622
623 printf(" vend-cmu");
624 cmu = (const struct cmu_vend *)bp;
625
626 /* Only print if there are unknown bits */
627 TCHECK(cmu->v_flags);
628 if ((cmu->v_flags & ~(VF_SMASK)) != 0)
629 printf(" F:0x%x", cmu->v_flags);
630 PRINTCMUADDR(v_dgate, "DG");
631 PRINTCMUADDR(v_smask, cmu->v_flags & VF_SMASK ? "SM" : "SM*");
632 PRINTCMUADDR(v_dns1, "NS1");
633 PRINTCMUADDR(v_dns2, "NS2");
634 PRINTCMUADDR(v_ins1, "IEN1");
635 PRINTCMUADDR(v_ins2, "IEN2");
636 PRINTCMUADDR(v_ts1, "TS1");
637 PRINTCMUADDR(v_ts2, "TS2");
638 return;
639
640 trunc:
641 fputs(tstr, stdout);
642 #undef PRINTCMUADDR
643 }