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