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