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