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