]> The Tcpdump Group git mirrors - tcpdump/blob - print-bootp.c
remove non-STDC code
[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.49 2000-07-01 03:39:01 assar 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 #include <net/if.h>
39
40 #include <netinet/in.h>
41 #include <netinet/if_ether.h>
42
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "bootp.h"
50
51 static void rfc1048_print(const u_char *, u_int);
52 static void cmu_print(const u_char *, u_int);
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 u_char vm_cmu[4] = VM_CMU;
65 static u_char vm_rfc1048[4] = VM_RFC1048;
66
67 bp = (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 = (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((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 length -= sizeof(*bp) - sizeof(bp->bp_vend);
168 if (memcmp((char *)bp->bp_vend, (char *)vm_rfc1048,
169 sizeof(u_int32_t)) == 0)
170 rfc1048_print(bp->bp_vend, length);
171 else if (memcmp((char *)bp->bp_vend, (char *)vm_cmu,
172 sizeof(u_int32_t)) == 0)
173 cmu_print(bp->bp_vend, length);
174 else {
175 u_int32_t ul;
176
177 memcpy((char *)&ul, (char *)bp->bp_vend, sizeof(ul));
178 if (ul != 0)
179 printf("vend-#0x%x", ul);
180 }
181
182 return;
183 trunc:
184 fputs(tstr, stdout);
185 }
186
187 /* The first character specifies the format to print */
188 static struct tok tag2str[] = {
189 /* RFC1048 tags */
190 { TAG_PAD, " PAD" },
191 { TAG_SUBNET_MASK, "iSM" }, /* subnet mask (RFC950) */
192 { TAG_TIME_OFFSET, "lTZ" }, /* seconds from UTC */
193 { TAG_GATEWAY, "iDG" }, /* default gateway */
194 { TAG_TIME_SERVER, "iTS" }, /* time servers (RFC868) */
195 { TAG_NAME_SERVER, "iIEN" }, /* IEN name servers (IEN116) */
196 { TAG_DOMAIN_SERVER, "iNS" }, /* domain name (RFC1035) */
197 { TAG_LOG_SERVER, "iLOG" }, /* MIT log servers */
198 { TAG_COOKIE_SERVER, "iCS" }, /* cookie servers (RFC865) */
199 { TAG_LPR_SERVER, "iLPR" }, /* lpr server (RFC1179) */
200 { TAG_IMPRESS_SERVER, "iIM" }, /* impress servers (Imagen) */
201 { TAG_RLP_SERVER, "iRL" }, /* resource location (RFC887) */
202 { TAG_HOSTNAME, "aHN" }, /* ascii hostname */
203 { TAG_BOOTSIZE, "sBS" }, /* 512 byte blocks */
204 { TAG_END, " END" },
205 /* RFC1497 tags */
206 { TAG_DUMPPATH, "aDP" },
207 { TAG_DOMAINNAME, "aDN" },
208 { TAG_SWAP_SERVER, "iSS" },
209 { TAG_ROOTPATH, "aRP" },
210 { TAG_EXTPATH, "aEP" },
211 /* RFC2132 tags */
212 { TAG_IP_FORWARD, "BIPF" },
213 { TAG_NL_SRCRT, "BSRT" },
214 { TAG_PFILTERS, "pPF" },
215 { TAG_REASS_SIZE, "sRSZ" },
216 { TAG_DEF_TTL, "bTTL" },
217 { TAG_MTU_TIMEOUT, "lMA" },
218 { TAG_MTU_TABLE, "sMT" },
219 { TAG_INT_MTU, "sMTU" },
220 { TAG_LOCAL_SUBNETS, "BLSN" },
221 { TAG_BROAD_ADDR, "iBR" },
222 { TAG_DO_MASK_DISC, "BMD" },
223 { TAG_SUPPLY_MASK, "BMS" },
224 { TAG_DO_RDISC, "BRD" },
225 { TAG_RTR_SOL_ADDR, "iRSA" },
226 { TAG_STATIC_ROUTE, "pSR" },
227 { TAG_USE_TRAILERS, "BUT" },
228 { TAG_ARP_TIMEOUT, "lAT" },
229 { TAG_ETH_ENCAP, "BIE" },
230 { TAG_TCP_TTL, "bTT" },
231 { TAG_TCP_KEEPALIVE, "lKI" },
232 { TAG_KEEPALIVE_GO, "BKG" },
233 { TAG_NIS_DOMAIN, "aYD" },
234 { TAG_NIS_SERVERS, "iYS" },
235 { TAG_NTP_SERVERS, "iNTP" },
236 { TAG_VENDOR_OPTS, "bVO" },
237 { TAG_NETBIOS_NS, "iWNS" },
238 { TAG_NETBIOS_DDS, "iWDD" },
239 { TAG_NETBIOS_NODE, "bWNT" },
240 { TAG_NETBIOS_SCOPE, "aWSC" },
241 { TAG_XWIN_FS, "iXFS" },
242 { TAG_XWIN_DM, "iXDM" },
243 { TAG_NIS_P_DOMAIN, "sN+D" },
244 { TAG_NIS_P_SERVERS, "iN+S" },
245 { TAG_MOBILE_HOME, "iMH" },
246 { TAG_SMPT_SERVER, "iSMTP" },
247 { TAG_POP3_SERVER, "iPOP3" },
248 { TAG_NNTP_SERVER, "iNNTP" },
249 { TAG_WWW_SERVER, "iWWW" },
250 { TAG_FINGER_SERVER, "iFG" },
251 { TAG_IRC_SERVER, "iIRC" },
252 { TAG_STREETTALK_SRVR, "iSTS" },
253 { TAG_STREETTALK_STDA, "iSTDA" },
254 { TAG_REQUESTED_IP, "iRQ" },
255 { TAG_IP_LEASE, "lLT" },
256 { TAG_OPT_OVERLOAD, "bOO" },
257 { TAG_TFTP_SERVER, "aTFTP" },
258 { TAG_BOOTFILENAME, "aBF" },
259 { TAG_DHCP_MESSAGE, " DHCP" },
260 { TAG_SERVER_ID, "iSID" },
261 { TAG_PARM_REQUEST, "bPR" },
262 { TAG_MESSAGE, "aMSG" },
263 { TAG_MAX_MSG_SIZE, "sMSZ" },
264 { TAG_RENEWAL_TIME, "lRN" },
265 { TAG_REBIND_TIME, "lRB" },
266 { TAG_VENDOR_CLASS, "bVC" },
267 { TAG_CLIENT_ID, "bCID" },
268 { 0, NULL }
269 };
270
271 static void
272 rfc1048_print(register const u_char *bp, register u_int length)
273 {
274 register u_char tag;
275 register u_int len, size;
276 register const char *cp;
277 register char c;
278 int first;
279 u_int32_t ul;
280 u_short us;
281
282 printf(" vend-rfc1048");
283
284 /* Step over magic cookie */
285 bp += sizeof(int32_t);
286
287 /* Loop while we there is a tag left in the buffer */
288 while (bp + 1 < snapend) {
289 tag = *bp++;
290 if (tag == TAG_PAD)
291 continue;
292 if (tag == TAG_END)
293 return;
294 cp = tok2str(tag2str, "?T%d", tag);
295 c = *cp++;
296 printf(" %s:", cp);
297
298 /* Get the length; check for truncation */
299 if (bp + 1 >= snapend) {
300 fputs(tstr, stdout);
301 return;
302 }
303 len = *bp++;
304 if (bp + len >= snapend) {
305 fputs(tstr, stdout);
306 return;
307 }
308
309 if (tag == TAG_DHCP_MESSAGE && len == 1) {
310 c = *bp++;
311 switch (c) {
312 case DHCPDISCOVER: printf("DISCOVER"); break;
313 case DHCPOFFER: printf("OFFER"); break;
314 case DHCPREQUEST: printf("REQUEST"); break;
315 case DHCPDECLINE: printf("DECLINE"); break;
316 case DHCPACK: printf("ACK"); break;
317 case DHCPNAK: printf("NACK"); break;
318 case DHCPRELEASE: printf("RELEASE"); break;
319 case DHCPINFORM: printf("INFORM"); break;
320 default: printf("%u", c); break;
321 }
322 continue;
323 }
324
325 if (tag == TAG_PARM_REQUEST) {
326 first = 1;
327 while (len-- > 0) {
328 c = *bp++;
329 cp = tok2str(tag2str, "?%d", c);
330 if (!first)
331 putchar('+');
332 printf("%s", cp + 1);
333 first = 0;
334 }
335 continue;
336 }
337
338 /* Print data */
339 size = len;
340 if (c == '?') {
341 /* Base default formats for unknown tags on data size */
342 if (size & 1)
343 c = 'b';
344 else if (size & 2)
345 c = 's';
346 else
347 c = 'l';
348 }
349 first = 1;
350 switch (c) {
351
352 case 'a':
353 /* ascii strings */
354 putchar('"');
355 (void)fn_printn(bp, size, NULL);
356 putchar('"');
357 bp += size;
358 size = 0;
359 break;
360
361 case 'i':
362 case 'l':
363 /* ip addresses/32-bit words */
364 while (size >= sizeof(ul)) {
365 if (!first)
366 putchar(',');
367 memcpy((char *)&ul, (char *)bp, sizeof(ul));
368 if (c == 'i')
369 printf("%s", ipaddr_string(&ul));
370 else
371 printf("%u", ul);
372 bp += sizeof(ul);
373 size -= sizeof(ul);
374 first = 0;
375 }
376 break;
377
378 case 'p':
379 /* IP address pairs */
380 while (size >= 2*sizeof(ul)) {
381 if (!first)
382 putchar(',');
383 memcpy((char *)&ul, (char *)bp, sizeof(ul));
384 printf("(%s:", ipaddr_string(&ul));
385 bp += sizeof(ul);
386 memcpy((char *)&ul, (char *)bp, sizeof(ul));
387 printf("%s)", ipaddr_string(&ul));
388 bp += sizeof(ul);
389 size -= 2*sizeof(ul);
390 first = 0;
391 }
392 break;
393
394 case 's':
395 /* shorts */
396 while (size >= sizeof(us)) {
397 if (!first)
398 putchar(',');
399 memcpy((char *)&us, (char *)bp, sizeof(us));
400 printf("%d", us);
401 bp += sizeof(us);
402 size -= sizeof(us);
403 first = 0;
404 }
405 break;
406
407 case 'B':
408 /* boolean */
409 while (size > 0) {
410 if (!first)
411 putchar(',');
412 switch (*bp) {
413 case 0:
414 putchar('N');
415 break;
416 case 1:
417 putchar('Y');
418 break;
419 default:
420 printf("%d?", *bp);
421 break;
422 }
423 ++bp;
424 --size;
425 first = 0;
426 }
427 break;
428
429 case 'b':
430 default:
431 /* Bytes */
432 while (size > 0) {
433 if (!first)
434 putchar('.');
435 printf("%d", *bp);
436 ++bp;
437 --size;
438 first = 0;
439 }
440 break;
441 }
442 /* Data left over? */
443 if (size)
444 printf("[len %d]", len);
445 }
446 }
447
448 static void
449 cmu_print(register const u_char *bp, register u_int length)
450 {
451 register const struct cmu_vend *cmu;
452 char *fmt = " %s:%s";
453
454 #define PRINTCMUADDR(m, s) { TCHECK(cmu->m); \
455 if (cmu->m.s_addr != 0) \
456 printf(fmt, s, ipaddr_string(&cmu->m.s_addr)); }
457
458 printf(" vend-cmu");
459 cmu = (struct cmu_vend *)bp;
460
461 /* Only print if there are unknown bits */
462 TCHECK(cmu->v_flags);
463 if ((cmu->v_flags & ~(VF_SMASK)) != 0)
464 printf(" F:0x%x", cmu->v_flags);
465 PRINTCMUADDR(v_dgate, "DG");
466 PRINTCMUADDR(v_smask, cmu->v_flags & VF_SMASK ? "SM" : "SM*");
467 PRINTCMUADDR(v_dns1, "NS1");
468 PRINTCMUADDR(v_dns2, "NS2");
469 PRINTCMUADDR(v_ins1, "IEN1");
470 PRINTCMUADDR(v_ins2, "IEN2");
471 PRINTCMUADDR(v_ts1, "TS1");
472 PRINTCMUADDR(v_ts2, "TS2");
473 return;
474
475 trunc:
476 fputs(tstr, stdout);
477 #undef PRINTCMUADDR
478 }