]> The Tcpdump Group git mirrors - tcpdump/blob - print-bootp.c
s/u_short/u_int16_t/ for KAME-origin source codes
[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.48 1999-11-21 09:36:49 fenner 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 #if __STDC__
37 struct mbuf;
38 struct rtentry;
39 #endif
40 #include <net/if.h>
41
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44
45 #include <ctype.h>
46 #ifdef HAVE_MEMORY_H
47 #include <memory.h>
48 #endif
49 #include <stdio.h>
50 #include <string.h>
51
52 #include "interface.h"
53 #include "addrtoname.h"
54 #include "bootp.h"
55
56 static void rfc1048_print(const u_char *, u_int);
57 static void cmu_print(const u_char *, u_int);
58
59 static char tstr[] = " [|bootp]";
60
61 /*
62 * Print bootp requests
63 */
64 void
65 bootp_print(register const u_char *cp, u_int length,
66 u_short sport, u_short dport)
67 {
68 register const struct bootp *bp;
69 static u_char vm_cmu[4] = VM_CMU;
70 static u_char vm_rfc1048[4] = VM_RFC1048;
71
72 bp = (struct bootp *)cp;
73 TCHECK(bp->bp_op);
74 switch (bp->bp_op) {
75
76 case BOOTREQUEST:
77 /* Usually, a request goes from a client to a server */
78 if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
79 printf(" (request)");
80 break;
81
82 case BOOTREPLY:
83 /* Usually, a reply goes from a server to a client */
84 if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
85 printf(" (reply)");
86 break;
87
88 default:
89 printf(" bootp-#%d", bp->bp_op);
90 }
91
92 TCHECK(bp->bp_secs);
93
94 /* The usual hardware address type is 1 (10Mb Ethernet) */
95 if (bp->bp_htype != 1)
96 printf(" htype-#%d", bp->bp_htype);
97
98 /* The usual length for 10Mb Ethernet address is 6 bytes */
99 if (bp->bp_htype != 1 || bp->bp_hlen != 6)
100 printf(" hlen:%d", bp->bp_hlen);
101
102 /* Only print interesting fields */
103 if (bp->bp_hops)
104 printf(" hops:%d", bp->bp_hops);
105 if (bp->bp_xid)
106 printf(" xid:0x%x", (u_int32_t)ntohl(bp->bp_xid));
107 if (bp->bp_secs)
108 printf(" secs:%d", ntohs(bp->bp_secs));
109 if (bp->bp_flags)
110 printf(" flags:0x%x", ntohs(bp->bp_flags));
111
112 /* Client's ip address */
113 TCHECK(bp->bp_ciaddr);
114 if (bp->bp_ciaddr.s_addr)
115 printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
116
117 /* 'your' ip address (bootp client) */
118 TCHECK(bp->bp_yiaddr);
119 if (bp->bp_yiaddr.s_addr)
120 printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
121
122 /* Server's ip address */
123 TCHECK(bp->bp_siaddr);
124 if (bp->bp_siaddr.s_addr)
125 printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
126
127 /* Gateway's ip address */
128 TCHECK(bp->bp_giaddr);
129 if (bp->bp_giaddr.s_addr)
130 printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
131
132 /* Client's Ethernet address */
133 if (bp->bp_htype == 1 && bp->bp_hlen == 6) {
134 register const struct ether_header *eh;
135 register const char *e;
136
137 TCHECK2(bp->bp_chaddr[0], 6);
138 eh = (struct ether_header *)packetp;
139 if (bp->bp_op == BOOTREQUEST)
140 e = (const char *)ESRC(eh);
141 else if (bp->bp_op == BOOTREPLY)
142 e = (const char *)EDST(eh);
143 else
144 e = 0;
145 if (e == 0 || memcmp((char *)bp->bp_chaddr, e, 6) != 0)
146 printf(" ether %s", etheraddr_string(bp->bp_chaddr));
147 }
148
149 TCHECK2(bp->bp_sname[0], 1); /* check first char only */
150 if (*bp->bp_sname) {
151 printf(" sname \"");
152 if (fn_print(bp->bp_sname, snapend)) {
153 putchar('"');
154 fputs(tstr + 1, stdout);
155 return;
156 }
157 putchar('"');
158 }
159 TCHECK2(bp->bp_sname[0], 1); /* check first char only */
160 if (*bp->bp_file) {
161 printf(" file \"");
162 if (fn_print(bp->bp_file, snapend)) {
163 putchar('"');
164 fputs(tstr + 1, stdout);
165 return;
166 }
167 putchar('"');
168 }
169
170 /* Decode the vendor buffer */
171 TCHECK(bp->bp_vend[0]);
172 length -= sizeof(*bp) - sizeof(bp->bp_vend);
173 if (memcmp((char *)bp->bp_vend, (char *)vm_rfc1048,
174 sizeof(u_int32_t)) == 0)
175 rfc1048_print(bp->bp_vend, length);
176 else if (memcmp((char *)bp->bp_vend, (char *)vm_cmu,
177 sizeof(u_int32_t)) == 0)
178 cmu_print(bp->bp_vend, length);
179 else {
180 u_int32_t ul;
181
182 memcpy((char *)&ul, (char *)bp->bp_vend, sizeof(ul));
183 if (ul != 0)
184 printf("vend-#0x%x", ul);
185 }
186
187 return;
188 trunc:
189 fputs(tstr, stdout);
190 }
191
192 /* The first character specifies the format to print */
193 static struct tok tag2str[] = {
194 /* RFC1048 tags */
195 { TAG_PAD, " PAD" },
196 { TAG_SUBNET_MASK, "iSM" }, /* subnet mask (RFC950) */
197 { TAG_TIME_OFFSET, "lTZ" }, /* seconds from UTC */
198 { TAG_GATEWAY, "iDG" }, /* default gateway */
199 { TAG_TIME_SERVER, "iTS" }, /* time servers (RFC868) */
200 { TAG_NAME_SERVER, "iIEN" }, /* IEN name servers (IEN116) */
201 { TAG_DOMAIN_SERVER, "iNS" }, /* domain name (RFC1035) */
202 { TAG_LOG_SERVER, "iLOG" }, /* MIT log servers */
203 { TAG_COOKIE_SERVER, "iCS" }, /* cookie servers (RFC865) */
204 { TAG_LPR_SERVER, "iLPR" }, /* lpr server (RFC1179) */
205 { TAG_IMPRESS_SERVER, "iIM" }, /* impress servers (Imagen) */
206 { TAG_RLP_SERVER, "iRL" }, /* resource location (RFC887) */
207 { TAG_HOSTNAME, "aHN" }, /* ascii hostname */
208 { TAG_BOOTSIZE, "sBS" }, /* 512 byte blocks */
209 { TAG_END, " END" },
210 /* RFC1497 tags */
211 { TAG_DUMPPATH, "aDP" },
212 { TAG_DOMAINNAME, "aDN" },
213 { TAG_SWAP_SERVER, "iSS" },
214 { TAG_ROOTPATH, "aRP" },
215 { TAG_EXTPATH, "aEP" },
216 /* RFC2132 tags */
217 { TAG_IP_FORWARD, "BIPF" },
218 { TAG_NL_SRCRT, "BSRT" },
219 { TAG_PFILTERS, "pPF" },
220 { TAG_REASS_SIZE, "sRSZ" },
221 { TAG_DEF_TTL, "bTTL" },
222 { TAG_MTU_TIMEOUT, "lMA" },
223 { TAG_MTU_TABLE, "sMT" },
224 { TAG_INT_MTU, "sMTU" },
225 { TAG_LOCAL_SUBNETS, "BLSN" },
226 { TAG_BROAD_ADDR, "iBR" },
227 { TAG_DO_MASK_DISC, "BMD" },
228 { TAG_SUPPLY_MASK, "BMS" },
229 { TAG_DO_RDISC, "BRD" },
230 { TAG_RTR_SOL_ADDR, "iRSA" },
231 { TAG_STATIC_ROUTE, "pSR" },
232 { TAG_USE_TRAILERS, "BUT" },
233 { TAG_ARP_TIMEOUT, "lAT" },
234 { TAG_ETH_ENCAP, "BIE" },
235 { TAG_TCP_TTL, "bTT" },
236 { TAG_TCP_KEEPALIVE, "lKI" },
237 { TAG_KEEPALIVE_GO, "BKG" },
238 { TAG_NIS_DOMAIN, "aYD" },
239 { TAG_NIS_SERVERS, "iYS" },
240 { TAG_NTP_SERVERS, "iNTP" },
241 { TAG_VENDOR_OPTS, "bVO" },
242 { TAG_NETBIOS_NS, "iWNS" },
243 { TAG_NETBIOS_DDS, "iWDD" },
244 { TAG_NETBIOS_NODE, "bWNT" },
245 { TAG_NETBIOS_SCOPE, "aWSC" },
246 { TAG_XWIN_FS, "iXFS" },
247 { TAG_XWIN_DM, "iXDM" },
248 { TAG_NIS_P_DOMAIN, "sN+D" },
249 { TAG_NIS_P_SERVERS, "iN+S" },
250 { TAG_MOBILE_HOME, "iMH" },
251 { TAG_SMPT_SERVER, "iSMTP" },
252 { TAG_POP3_SERVER, "iPOP3" },
253 { TAG_NNTP_SERVER, "iNNTP" },
254 { TAG_WWW_SERVER, "iWWW" },
255 { TAG_FINGER_SERVER, "iFG" },
256 { TAG_IRC_SERVER, "iIRC" },
257 { TAG_STREETTALK_SRVR, "iSTS" },
258 { TAG_STREETTALK_STDA, "iSTDA" },
259 { TAG_REQUESTED_IP, "iRQ" },
260 { TAG_IP_LEASE, "lLT" },
261 { TAG_OPT_OVERLOAD, "bOO" },
262 { TAG_TFTP_SERVER, "aTFTP" },
263 { TAG_BOOTFILENAME, "aBF" },
264 { TAG_DHCP_MESSAGE, " DHCP" },
265 { TAG_SERVER_ID, "iSID" },
266 { TAG_PARM_REQUEST, "bPR" },
267 { TAG_MESSAGE, "aMSG" },
268 { TAG_MAX_MSG_SIZE, "sMSZ" },
269 { TAG_RENEWAL_TIME, "lRN" },
270 { TAG_REBIND_TIME, "lRB" },
271 { TAG_VENDOR_CLASS, "bVC" },
272 { TAG_CLIENT_ID, "bCID" },
273 { 0, NULL }
274 };
275
276 static void
277 rfc1048_print(register const u_char *bp, register u_int length)
278 {
279 register u_char tag;
280 register u_int len, size;
281 register const char *cp;
282 register char c;
283 int first;
284 u_int32_t ul;
285 u_short us;
286
287 printf(" vend-rfc1048");
288
289 /* Step over magic cookie */
290 bp += sizeof(int32_t);
291
292 /* Loop while we there is a tag left in the buffer */
293 while (bp + 1 < snapend) {
294 tag = *bp++;
295 if (tag == TAG_PAD)
296 continue;
297 if (tag == TAG_END)
298 return;
299 cp = tok2str(tag2str, "?T%d", tag);
300 c = *cp++;
301 printf(" %s:", cp);
302
303 /* Get the length; check for truncation */
304 if (bp + 1 >= snapend) {
305 fputs(tstr, stdout);
306 return;
307 }
308 len = *bp++;
309 if (bp + len >= snapend) {
310 fputs(tstr, stdout);
311 return;
312 }
313
314 if (tag == TAG_DHCP_MESSAGE && len == 1) {
315 c = *bp++;
316 switch (c) {
317 case DHCPDISCOVER: printf("DISCOVER"); break;
318 case DHCPOFFER: printf("OFFER"); break;
319 case DHCPREQUEST: printf("REQUEST"); break;
320 case DHCPDECLINE: printf("DECLINE"); break;
321 case DHCPACK: printf("ACK"); break;
322 case DHCPNAK: printf("NACK"); break;
323 case DHCPRELEASE: printf("RELEASE"); break;
324 case DHCPINFORM: printf("INFORM"); break;
325 default: printf("%u", c); break;
326 }
327 continue;
328 }
329
330 if (tag == TAG_PARM_REQUEST) {
331 first = 1;
332 while (len-- > 0) {
333 c = *bp++;
334 cp = tok2str(tag2str, "?%d", c);
335 if (!first)
336 putchar('+');
337 printf("%s", cp + 1);
338 first = 0;
339 }
340 continue;
341 }
342
343 /* Print data */
344 size = len;
345 if (c == '?') {
346 /* Base default formats for unknown tags on data size */
347 if (size & 1)
348 c = 'b';
349 else if (size & 2)
350 c = 's';
351 else
352 c = 'l';
353 }
354 first = 1;
355 switch (c) {
356
357 case 'a':
358 /* ascii strings */
359 putchar('"');
360 (void)fn_printn(bp, size, NULL);
361 putchar('"');
362 bp += size;
363 size = 0;
364 break;
365
366 case 'i':
367 case 'l':
368 /* ip addresses/32-bit words */
369 while (size >= sizeof(ul)) {
370 if (!first)
371 putchar(',');
372 memcpy((char *)&ul, (char *)bp, sizeof(ul));
373 if (c == 'i')
374 printf("%s", ipaddr_string(&ul));
375 else
376 printf("%u", ul);
377 bp += sizeof(ul);
378 size -= sizeof(ul);
379 first = 0;
380 }
381 break;
382
383 case 'p':
384 /* IP address pairs */
385 while (size >= 2*sizeof(ul)) {
386 if (!first)
387 putchar(',');
388 memcpy((char *)&ul, (char *)bp, sizeof(ul));
389 printf("(%s:", ipaddr_string(&ul));
390 bp += sizeof(ul);
391 memcpy((char *)&ul, (char *)bp, sizeof(ul));
392 printf("%s)", ipaddr_string(&ul));
393 bp += sizeof(ul);
394 size -= 2*sizeof(ul);
395 first = 0;
396 }
397 break;
398
399 case 's':
400 /* shorts */
401 while (size >= sizeof(us)) {
402 if (!first)
403 putchar(',');
404 memcpy((char *)&us, (char *)bp, sizeof(us));
405 printf("%d", us);
406 bp += sizeof(us);
407 size -= sizeof(us);
408 first = 0;
409 }
410 break;
411
412 case 'B':
413 /* boolean */
414 while (size > 0) {
415 if (!first)
416 putchar(',');
417 switch (*bp) {
418 case 0:
419 putchar('N');
420 break;
421 case 1:
422 putchar('Y');
423 break;
424 default:
425 printf("%d?", *bp);
426 break;
427 }
428 ++bp;
429 --size;
430 first = 0;
431 }
432 break;
433
434 case 'b':
435 default:
436 /* Bytes */
437 while (size > 0) {
438 if (!first)
439 putchar('.');
440 printf("%d", *bp);
441 ++bp;
442 --size;
443 first = 0;
444 }
445 break;
446 }
447 /* Data left over? */
448 if (size)
449 printf("[len %d]", len);
450 }
451 }
452
453 static void
454 cmu_print(register const u_char *bp, register u_int length)
455 {
456 register const struct cmu_vend *cmu;
457 char *fmt = " %s:%s";
458
459 #define PRINTCMUADDR(m, s) { TCHECK(cmu->m); \
460 if (cmu->m.s_addr != 0) \
461 printf(fmt, s, ipaddr_string(&cmu->m.s_addr)); }
462
463 printf(" vend-cmu");
464 cmu = (struct cmu_vend *)bp;
465
466 /* Only print if there are unknown bits */
467 TCHECK(cmu->v_flags);
468 if ((cmu->v_flags & ~(VF_SMASK)) != 0)
469 printf(" F:0x%x", cmu->v_flags);
470 PRINTCMUADDR(v_dgate, "DG");
471 PRINTCMUADDR(v_smask, cmu->v_flags & VF_SMASK ? "SM" : "SM*");
472 PRINTCMUADDR(v_dns1, "NS1");
473 PRINTCMUADDR(v_dns2, "NS2");
474 PRINTCMUADDR(v_ins1, "IEN1");
475 PRINTCMUADDR(v_ins2, "IEN2");
476 PRINTCMUADDR(v_ts1, "TS1");
477 PRINTCMUADDR(v_ts2, "TS2");
478 return;
479
480 trunc:
481 fputs(tstr, stdout);
482 #undef PRINTCMUADDR
483 }