1 /* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 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 * Support for splitting captures into multiple files with a maximum 22 * file size: 23 * 24 * Copyright (c) 2001 25 * Seth Webster <swebster@sst.ll.mit.edu> 26 */ 27 28 #include <sys/cdefs.h> 29 #ifndef lint 30 __RCSID("$NetBSD: print.c,v 1.4 2017/09/08 14:01:13 christos Exp $"); 31 #endif 32 33 #ifdef HAVE_CONFIG_H 34 #include "config.h" 35 #endif 36 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include <netdissect-stdinc.h> 41 42 #include "netdissect.h" 43 #include "addrtoname.h" 44 #include "print.h" 45 46 struct printer { 47 if_printer f; 48 int type; 49 }; 50 51 static const struct printer printers[] = { 52 { ether_if_print, DLT_EN10MB }, 53 #ifdef DLT_IPNET 54 { ipnet_if_print, DLT_IPNET }, 55 #endif 56 #ifdef DLT_IEEE802_15_4 57 { ieee802_15_4_if_print, DLT_IEEE802_15_4 }, 58 #endif 59 #ifdef DLT_IEEE802_15_4_NOFCS 60 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS }, 61 #endif 62 #ifdef DLT_PPI 63 { ppi_if_print, DLT_PPI }, 64 #endif 65 #ifdef DLT_NETANALYZER 66 { netanalyzer_if_print, DLT_NETANALYZER }, 67 #endif 68 #ifdef DLT_NETANALYZER_TRANSPARENT 69 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT }, 70 #endif 71 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H) 72 { nflog_if_print, DLT_NFLOG}, 73 #endif 74 #ifdef DLT_CIP 75 { cip_if_print, DLT_CIP }, 76 #endif 77 #ifdef DLT_ATM_CLIP 78 { cip_if_print, DLT_ATM_CLIP }, 79 #endif 80 #ifdef DLT_IP_OVER_FC 81 { ipfc_if_print, DLT_IP_OVER_FC }, 82 #endif 83 { null_if_print, DLT_NULL }, 84 #ifdef DLT_LOOP 85 { null_if_print, DLT_LOOP }, 86 #endif 87 #ifdef DLT_APPLE_IP_OVER_IEEE1394 88 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 }, 89 #endif 90 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H) 91 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR}, 92 #endif 93 #ifdef DLT_LANE8023 94 { lane_if_print, DLT_LANE8023 }, 95 #endif 96 { arcnet_if_print, DLT_ARCNET }, 97 #ifdef DLT_ARCNET_LINUX 98 { arcnet_linux_if_print, DLT_ARCNET_LINUX }, 99 #endif 100 { raw_if_print, DLT_RAW }, 101 #ifdef DLT_IPV4 102 { raw_if_print, DLT_IPV4 }, 103 #endif 104 #ifdef DLT_IPV6 105 { raw_if_print, DLT_IPV6 }, 106 #endif 107 #ifdef HAVE_PCAP_USB_H 108 #ifdef DLT_USB_LINUX 109 { usb_linux_48_byte_print, DLT_USB_LINUX}, 110 #endif /* DLT_USB_LINUX */ 111 #ifdef DLT_USB_LINUX_MMAPPED 112 { usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED}, 113 #endif /* DLT_USB_LINUX_MMAPPED */ 114 #endif /* HAVE_PCAP_USB_H */ 115 #ifdef DLT_SYMANTEC_FIREWALL 116 { symantec_if_print, DLT_SYMANTEC_FIREWALL }, 117 #endif 118 #ifdef DLT_C_HDLC 119 { chdlc_if_print, DLT_C_HDLC }, 120 #endif 121 #ifdef DLT_HDLC 122 { chdlc_if_print, DLT_HDLC }, 123 #endif 124 #ifdef DLT_PPP_ETHER 125 { pppoe_if_print, DLT_PPP_ETHER }, 126 #endif 127 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H) 128 { pflog_if_print, DLT_PFLOG }, 129 #endif 130 { token_if_print, DLT_IEEE802 }, 131 { fddi_if_print, DLT_FDDI }, 132 #ifdef DLT_LINUX_SLL 133 { sll_if_print, DLT_LINUX_SLL }, 134 #endif 135 #ifdef DLT_FR 136 { fr_if_print, DLT_FR }, 137 #endif 138 #ifdef DLT_FRELAY 139 { fr_if_print, DLT_FRELAY }, 140 #endif 141 #ifdef DLT_MFR 142 { mfr_if_print, DLT_MFR }, 143 #endif 144 { atm_if_print, DLT_ATM_RFC1483 }, 145 #ifdef DLT_SUNATM 146 { sunatm_if_print, DLT_SUNATM }, 147 #endif 148 #ifdef DLT_ENC 149 { enc_if_print, DLT_ENC }, 150 #endif 151 { sl_if_print, DLT_SLIP }, 152 #ifdef DLT_SLIP_BSDOS 153 { sl_bsdos_if_print, DLT_SLIP_BSDOS }, 154 #endif 155 #ifdef DLT_LTALK 156 { ltalk_if_print, DLT_LTALK }, 157 #endif 158 #ifdef DLT_JUNIPER_ATM1 159 { juniper_atm1_print, DLT_JUNIPER_ATM1 }, 160 #endif 161 #ifdef DLT_JUNIPER_ATM2 162 { juniper_atm2_print, DLT_JUNIPER_ATM2 }, 163 #endif 164 #ifdef DLT_JUNIPER_MFR 165 { juniper_mfr_print, DLT_JUNIPER_MFR }, 166 #endif 167 #ifdef DLT_JUNIPER_MLFR 168 { juniper_mlfr_print, DLT_JUNIPER_MLFR }, 169 #endif 170 #ifdef DLT_JUNIPER_MLPPP 171 { juniper_mlppp_print, DLT_JUNIPER_MLPPP }, 172 #endif 173 #ifdef DLT_JUNIPER_PPPOE 174 { juniper_pppoe_print, DLT_JUNIPER_PPPOE }, 175 #endif 176 #ifdef DLT_JUNIPER_PPPOE_ATM 177 { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM }, 178 #endif 179 #ifdef DLT_JUNIPER_GGSN 180 { juniper_ggsn_print, DLT_JUNIPER_GGSN }, 181 #endif 182 #ifdef DLT_JUNIPER_ES 183 { juniper_es_print, DLT_JUNIPER_ES }, 184 #endif 185 #ifdef DLT_JUNIPER_MONITOR 186 { juniper_monitor_print, DLT_JUNIPER_MONITOR }, 187 #endif 188 #ifdef DLT_JUNIPER_SERVICES 189 { juniper_services_print, DLT_JUNIPER_SERVICES }, 190 #endif 191 #ifdef DLT_JUNIPER_ETHER 192 { juniper_ether_print, DLT_JUNIPER_ETHER }, 193 #endif 194 #ifdef DLT_JUNIPER_PPP 195 { juniper_ppp_print, DLT_JUNIPER_PPP }, 196 #endif 197 #ifdef DLT_JUNIPER_FRELAY 198 { juniper_frelay_print, DLT_JUNIPER_FRELAY }, 199 #endif 200 #ifdef DLT_JUNIPER_CHDLC 201 { juniper_chdlc_print, DLT_JUNIPER_CHDLC }, 202 #endif 203 #ifdef DLT_PKTAP 204 { pktap_if_print, DLT_PKTAP }, 205 #endif 206 #ifdef DLT_IEEE802_11_RADIO 207 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO }, 208 #endif 209 #ifdef DLT_IEEE802_11 210 { ieee802_11_if_print, DLT_IEEE802_11}, 211 #endif 212 #ifdef DLT_IEEE802_11_RADIO_AVS 213 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS }, 214 #endif 215 #ifdef DLT_PRISM_HEADER 216 { prism_if_print, DLT_PRISM_HEADER }, 217 #endif 218 { ppp_if_print, DLT_PPP }, 219 #ifdef DLT_PPP_WITHDIRECTION 220 { ppp_if_print, DLT_PPP_WITHDIRECTION }, 221 #endif 222 #ifdef DLT_PPP_BSDOS 223 { ppp_bsdos_if_print, DLT_PPP_BSDOS }, 224 #endif 225 #ifdef DLT_PPP_SERIAL 226 { ppp_hdlc_if_print, DLT_PPP_SERIAL }, 227 #endif 228 #ifdef DLT_PFSYNC 229 { pfsync_if_print, DLT_PFSYNC }, 230 #endif 231 { NULL, 0 }, 232 }; 233 234 static void ndo_default_print(netdissect_options *ndo, const u_char *bp, 235 u_int length); 236 237 static void ndo_error(netdissect_options *ndo, 238 FORMAT_STRING(const char *fmt), ...) 239 NORETURN PRINTFLIKE(2, 3); 240 static void ndo_warning(netdissect_options *ndo, 241 FORMAT_STRING(const char *fmt), ...) 242 PRINTFLIKE(2, 3); 243 244 static int ndo_printf(netdissect_options *ndo, 245 FORMAT_STRING(const char *fmt), ...) 246 PRINTFLIKE(2, 3); 247 248 void 249 init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask, 250 uint32_t timezone_offset) 251 { 252 253 thiszone = timezone_offset; 254 init_addrtoname(ndo, localnet, mask); 255 init_checksum(); 256 } 257 258 if_printer 259 lookup_printer(int type) 260 { 261 const struct printer *p; 262 263 for (p = printers; p->f; ++p) 264 if (type == p->type) 265 return p->f; 266 267 #if defined(DLT_USER2) && defined(DLT_PKTAP) 268 /* 269 * Apple incorrectly chose to use DLT_USER2 for their PKTAP 270 * header. 271 * 272 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin- 273 * based OSes or the same value as LINKTYPE_PKTAP as it is on 274 * other OSes, to LINKTYPE_PKTAP, so files written with 275 * this version of libpcap for a DLT_PKTAP capture have a link- 276 * layer header type of LINKTYPE_PKTAP. 277 * 278 * However, files written on OS X Mavericks for a DLT_PKTAP 279 * capture have a link-layer header type of LINKTYPE_USER2. 280 * If we don't have a printer for DLT_USER2, and type is 281 * DLT_USER2, we look up the printer for DLT_PKTAP and use 282 * that. 283 */ 284 if (type == DLT_USER2) { 285 for (p = printers; p->f; ++p) 286 if (DLT_PKTAP == p->type) 287 return p->f; 288 } 289 #endif 290 291 return NULL; 292 /* NOTREACHED */ 293 } 294 295 int 296 has_printer(int type) 297 { 298 return (lookup_printer(type) != NULL); 299 } 300 301 if_printer 302 get_if_printer(netdissect_options *ndo, int type) 303 { 304 const char *dltname; 305 if_printer printer; 306 307 printer = lookup_printer(type); 308 if (printer == NULL) { 309 dltname = pcap_datalink_val_to_name(type); 310 if (dltname != NULL) 311 (*ndo->ndo_error)(ndo, 312 "packet printing is not supported for link type %s: use -w", 313 dltname); 314 else 315 (*ndo->ndo_error)(ndo, 316 "packet printing is not supported for link type %d: use -w", type); 317 } 318 return printer; 319 } 320 321 void 322 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h, 323 const u_char *sp, u_int packets_captured) 324 { 325 u_int hdrlen; 326 327 if(ndo->ndo_packet_number) 328 ND_PRINT((ndo, "%5u ", packets_captured)); 329 330 ts_print(ndo, &h->ts); 331 332 /* 333 * Some printers want to check that they're not walking off the 334 * end of the packet. 335 * Rather than pass it all the way down, we set this member 336 * of the netdissect_options structure. 337 */ 338 ndo->ndo_snapend = sp + h->caplen; 339 340 hdrlen = (ndo->ndo_if_printer)(ndo, h, sp); 341 342 /* 343 * Restore the original snapend, as a printer might have 344 * changed it. 345 */ 346 ndo->ndo_snapend = sp + h->caplen; 347 if (ndo->ndo_Xflag) { 348 /* 349 * Print the raw packet data in hex and ASCII. 350 */ 351 if (ndo->ndo_Xflag > 1) { 352 /* 353 * Include the link-layer header. 354 */ 355 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen); 356 } else { 357 /* 358 * Don't include the link-layer header - and if 359 * we have nothing past the link-layer header, 360 * print nothing. 361 */ 362 if (h->caplen > hdrlen) 363 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen, 364 h->caplen - hdrlen); 365 } 366 } else if (ndo->ndo_xflag) { 367 /* 368 * Print the raw packet data in hex. 369 */ 370 if (ndo->ndo_xflag > 1) { 371 /* 372 * Include the link-layer header. 373 */ 374 hex_print(ndo, "\n\t", sp, h->caplen); 375 } else { 376 /* 377 * Don't include the link-layer header - and if 378 * we have nothing past the link-layer header, 379 * print nothing. 380 */ 381 if (h->caplen > hdrlen) 382 hex_print(ndo, "\n\t", sp + hdrlen, 383 h->caplen - hdrlen); 384 } 385 } else if (ndo->ndo_Aflag) { 386 /* 387 * Print the raw packet data in ASCII. 388 */ 389 if (ndo->ndo_Aflag > 1) { 390 /* 391 * Include the link-layer header. 392 */ 393 ascii_print(ndo, sp, h->caplen); 394 } else { 395 /* 396 * Don't include the link-layer header - and if 397 * we have nothing past the link-layer header, 398 * print nothing. 399 */ 400 if (h->caplen > hdrlen) 401 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen); 402 } 403 } 404 405 ND_PRINT((ndo, "\n")); 406 } 407 408 /* 409 * By default, print the specified data out in hex and ASCII. 410 */ 411 static void 412 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length) 413 { 414 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */ 415 } 416 417 /* VARARGS */ 418 static void 419 ndo_error(netdissect_options *ndo, const char *fmt, ...) 420 { 421 va_list ap; 422 423 if(ndo->program_name) 424 (void)fprintf(stderr, "%s: ", ndo->program_name); 425 va_start(ap, fmt); 426 (void)vfprintf(stderr, fmt, ap); 427 va_end(ap); 428 if (*fmt) { 429 fmt += strlen(fmt); 430 if (fmt[-1] != '\n') 431 (void)fputc('\n', stderr); 432 } 433 nd_cleanup(); 434 exit(1); 435 /* NOTREACHED */ 436 } 437 438 /* VARARGS */ 439 static void 440 ndo_warning(netdissect_options *ndo, const char *fmt, ...) 441 { 442 va_list ap; 443 444 if(ndo->program_name) 445 (void)fprintf(stderr, "%s: ", ndo->program_name); 446 (void)fprintf(stderr, "WARNING: "); 447 va_start(ap, fmt); 448 (void)vfprintf(stderr, fmt, ap); 449 va_end(ap); 450 if (*fmt) { 451 fmt += strlen(fmt); 452 if (fmt[-1] != '\n') 453 (void)fputc('\n', stderr); 454 } 455 } 456 457 static int 458 ndo_printf(netdissect_options *ndo, const char *fmt, ...) 459 { 460 va_list args; 461 int ret; 462 463 va_start(args, fmt); 464 ret = vfprintf(stdout, fmt, args); 465 va_end(args); 466 467 if (ret < 0) 468 ndo_error(ndo, "Unable to write output: %s", pcap_strerror(errno)); 469 return (ret); 470 } 471 472 void 473 ndo_set_function_pointers(netdissect_options *ndo) 474 { 475 ndo->ndo_default_print=ndo_default_print; 476 ndo->ndo_printf=ndo_printf; 477 ndo->ndo_error=ndo_error; 478 ndo->ndo_warning=ndo_warning; 479 } 480 /* 481 * Local Variables: 482 * c-style: whitesmith 483 * c-basic-offset: 8 484 * End: 485 */ 486