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