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