10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SunOS */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <string.h> 310Sstevel@tonic-gate #include <errno.h> 320Sstevel@tonic-gate #include <fcntl.h> 330Sstevel@tonic-gate #include <setjmp.h> 340Sstevel@tonic-gate #include <sys/types.h> 350Sstevel@tonic-gate #include <sys/signal.h> 360Sstevel@tonic-gate #include <sys/time.h> 370Sstevel@tonic-gate #include <sys/socket.h> 380Sstevel@tonic-gate #include <sys/sockio.h> 390Sstevel@tonic-gate #include <net/if.h> 400Sstevel@tonic-gate #include <netinet/in_systm.h> 410Sstevel@tonic-gate #include <netinet/in.h> 420Sstevel@tonic-gate #include <netinet/ip.h> 430Sstevel@tonic-gate #include <sys/pfmod.h> 440Sstevel@tonic-gate #include <netinet/if_ether.h> 450Sstevel@tonic-gate #include <sys/mman.h> 460Sstevel@tonic-gate #include <sys/stat.h> 470Sstevel@tonic-gate #include <sys/stropts.h> 480Sstevel@tonic-gate #include <sys/bufmod.h> 490Sstevel@tonic-gate #include <sys/dlpi.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate #include <unistd.h> 520Sstevel@tonic-gate #include <stropts.h> 530Sstevel@tonic-gate #include <stdlib.h> 540Sstevel@tonic-gate #include <ctype.h> 550Sstevel@tonic-gate #include <values.h> 560Sstevel@tonic-gate #include <libdlpi.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate #include "snoop.h" 590Sstevel@tonic-gate 600Sstevel@tonic-gate void scan(); 610Sstevel@tonic-gate void convert_to_network(); 620Sstevel@tonic-gate void convert_from_network(); 630Sstevel@tonic-gate void convert_old(); 640Sstevel@tonic-gate extern int quitting; 650Sstevel@tonic-gate extern sigjmp_buf jmp_env, ojmp_env; 660Sstevel@tonic-gate int netfd; 670Sstevel@tonic-gate union DL_primitives netdl; /* info_ack for interface */ 680Sstevel@tonic-gate char *bufp; /* pointer to read buffer */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate extern unsigned int encap_levels; 710Sstevel@tonic-gate 72*410Skcpoon static int strioctl(int, int, int, int, char *); 73*410Skcpoon 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Convert a device id to a ppa value 760Sstevel@tonic-gate * e.g. "le0" -> 0 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate int 790Sstevel@tonic-gate device_ppa(device) 800Sstevel@tonic-gate char *device; 810Sstevel@tonic-gate { 820Sstevel@tonic-gate char *p; 830Sstevel@tonic-gate char *tp; 840Sstevel@tonic-gate 850Sstevel@tonic-gate p = strpbrk(device, "0123456789"); 860Sstevel@tonic-gate if (p == NULL) 870Sstevel@tonic-gate return (0); 880Sstevel@tonic-gate /* ignore numbers within device names */ 890Sstevel@tonic-gate for (tp = p; *tp != '\0'; tp++) 900Sstevel@tonic-gate if (!isdigit(*tp)) 910Sstevel@tonic-gate return (device_ppa(tp)); 920Sstevel@tonic-gate return (atoi(p)); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * Convert a device id to a pathname. 970Sstevel@tonic-gate * Level 1 devices: "le0" -> "/dev/le0". 980Sstevel@tonic-gate * Level 2 devices: "le0" -> "/dev/le". 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate char * 1010Sstevel@tonic-gate device_path(device) 1020Sstevel@tonic-gate char *device; 1030Sstevel@tonic-gate { 1040Sstevel@tonic-gate static char buff[IF_NAMESIZE + 1]; 1050Sstevel@tonic-gate struct stat st; 1060Sstevel@tonic-gate char *p; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate (void) strcpy(buff, "/dev/"); 1090Sstevel@tonic-gate (void) strlcat(buff, device, IF_NAMESIZE); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate if (stat(buff, &st) == 0) 1120Sstevel@tonic-gate return (buff); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate for (p = buff + (strlen(buff) - 1); p > buff; p--) { 1150Sstevel@tonic-gate if (isdigit(*p)) 1160Sstevel@tonic-gate *p = '\0'; 1170Sstevel@tonic-gate else 1180Sstevel@tonic-gate break; 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate return (buff); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate /* 1240Sstevel@tonic-gate * Open up the device, and start finding out something about it, 1250Sstevel@tonic-gate * especially stuff about the data link headers. We need that information 1260Sstevel@tonic-gate * to build the proper packet filters. 1270Sstevel@tonic-gate */ 128*410Skcpoon boolean_t 129*410Skcpoon check_device(char **devicep, int *ppap) 1300Sstevel@tonic-gate { 1310Sstevel@tonic-gate char *devname; 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * Determine which network device 1340Sstevel@tonic-gate * to use if none given. 1350Sstevel@tonic-gate * Should get back a value like "le0". 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate if (*devicep == NULL) { 1390Sstevel@tonic-gate char *cbuf; 1400Sstevel@tonic-gate static struct ifconf ifc; 1410Sstevel@tonic-gate static struct ifreq *ifr; 1420Sstevel@tonic-gate int s; 1430Sstevel@tonic-gate int n; 1440Sstevel@tonic-gate int numifs; 1450Sstevel@tonic-gate unsigned bufsize; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1480Sstevel@tonic-gate pr_err("socket"); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) { 1510Sstevel@tonic-gate pr_err("check_device: ioctl SIOCGIFNUM"); 1520Sstevel@tonic-gate (void) close(s); 1530Sstevel@tonic-gate s = -1; 154*410Skcpoon return (B_FALSE); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate bufsize = numifs * sizeof (struct ifreq); 1580Sstevel@tonic-gate cbuf = (char *)malloc(bufsize); 1590Sstevel@tonic-gate if (cbuf == NULL) { 1600Sstevel@tonic-gate pr_err("out of memory\n"); 1610Sstevel@tonic-gate (void) close(s); 1620Sstevel@tonic-gate s = -1; 163*410Skcpoon return (B_FALSE); 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate ifc.ifc_len = bufsize; 1660Sstevel@tonic-gate ifc.ifc_buf = cbuf; 1670Sstevel@tonic-gate if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 1680Sstevel@tonic-gate pr_err("check_device: ioctl SIOCGIFCONF"); 1690Sstevel@tonic-gate (void) close(s); 1700Sstevel@tonic-gate s = -1; 1710Sstevel@tonic-gate (void) free(cbuf); 172*410Skcpoon return (B_FALSE); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate n = ifc.ifc_len / sizeof (struct ifreq); 1750Sstevel@tonic-gate ifr = ifc.ifc_req; 1760Sstevel@tonic-gate for (; n > 0; n--, ifr++) { 1770Sstevel@tonic-gate if (strchr(ifr->ifr_name, ':') != NULL) 1780Sstevel@tonic-gate continue; 1790Sstevel@tonic-gate if (ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0) 1800Sstevel@tonic-gate pr_err("ioctl SIOCGIFFLAGS"); 1810Sstevel@tonic-gate if ((ifr->ifr_flags & 1820Sstevel@tonic-gate (IFF_VIRTUAL|IFF_LOOPBACK|IFF_UP| 1830Sstevel@tonic-gate IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)) 1840Sstevel@tonic-gate break; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate if (n == 0) 1880Sstevel@tonic-gate pr_err("No network interface devices found"); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate *devicep = ifr->ifr_name; 1910Sstevel@tonic-gate (void) close(s); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate devname = device_path(*devicep); 1950Sstevel@tonic-gate if ((netfd = open(devname, O_RDWR)) < 0) 1960Sstevel@tonic-gate pr_err("%s: %m", devname); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate *ppap = device_ppa(*devicep); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* 2010Sstevel@tonic-gate * Check for DLPI Version 2. 2020Sstevel@tonic-gate */ 2030Sstevel@tonic-gate dlinforeq(netfd, &netdl); 2040Sstevel@tonic-gate if (netdl.info_ack.dl_version != DL_VERSION_2) 2050Sstevel@tonic-gate pr_err("DL_INFO_ACK: incompatible version %d", 2060Sstevel@tonic-gate netdl.info_ack.dl_version); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * Attach for DLPI Style 2. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate if (netdl.info_ack.dl_provider_style == DL_STYLE2) { 2120Sstevel@tonic-gate dlattachreq(netfd, *ppap); 2130Sstevel@tonic-gate /* Reread more specific information */ 2140Sstevel@tonic-gate dlinforeq(netfd, &netdl); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* Enable passive mode so that we can snoop on aggregated links. */ 2180Sstevel@tonic-gate dlpi_passive(netfd, -1); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate for (interface = &INTERFACES[0]; interface->mac_type != -1; interface++) 2210Sstevel@tonic-gate if (interface->mac_type == netdl.info_ack.dl_mac_type) 2220Sstevel@tonic-gate break; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* allow limited functionality even is interface isn't known */ 2250Sstevel@tonic-gate if (interface->mac_type == -1) { 2260Sstevel@tonic-gate fprintf(stderr, "snoop: WARNING: Mac Type = %x not supported\n", 2270Sstevel@tonic-gate netdl.info_ack.dl_mac_type); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* for backward compatibility, allow known interface mtu_sizes */ 2310Sstevel@tonic-gate if (interface->mtu_size > (uint_t)netdl.info_ack.dl_max_sdu) 2320Sstevel@tonic-gate netdl.info_ack.dl_max_sdu = (t_scalar_t)interface->mtu_size; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate if (interface->mac_hdr_fixed_size == IF_HDR_FIXED) 235*410Skcpoon return (B_TRUE); 2360Sstevel@tonic-gate 237*410Skcpoon return (B_FALSE); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * Do whatever is necessary to initialize the interface 2420Sstevel@tonic-gate * for packet capture. Bind the device opened and attached (if DL_STYLE2) 2430Sstevel@tonic-gate * in check_device(), request raw ethernet packets and set promiscuous mode, 2440Sstevel@tonic-gate * push the streams buffer module and packet filter module, set various buffer 2450Sstevel@tonic-gate * parameters. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate void 2480Sstevel@tonic-gate initdevice(device, snaplen, chunksize, timeout, fp, ppa) 2490Sstevel@tonic-gate char *device; 2500Sstevel@tonic-gate ulong_t snaplen, chunksize; 2510Sstevel@tonic-gate struct timeval *timeout; 2520Sstevel@tonic-gate struct Pf_ext_packetfilt *fp; 2530Sstevel@tonic-gate int ppa; 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate union DL_primitives dl; 2560Sstevel@tonic-gate extern int Pflg; 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Bind to SAP 2 on token ring, 0 on other interface types. 2600Sstevel@tonic-gate * (SAP 0 has special significance on token ring) 2610Sstevel@tonic-gate */ 2620Sstevel@tonic-gate if (interface->mac_type == DL_TPR) 2630Sstevel@tonic-gate dlbindreq(netfd, 2, 0, DL_CLDLS, 0); 2640Sstevel@tonic-gate else 2650Sstevel@tonic-gate dlbindreq(netfd, 0, 0, DL_CLDLS, 0); 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate (void) fprintf(stderr, "Using device %s ", device_path(device)); 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * If Pflg not set - use physical level 2710Sstevel@tonic-gate * promiscuous mode. Otherwise - just SAP level. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate if (!Pflg) { 2740Sstevel@tonic-gate (void) fprintf(stderr, "(promiscuous mode)\n"); 2750Sstevel@tonic-gate dlpromiscon(netfd, DL_PROMISC_PHYS); 2760Sstevel@tonic-gate } else { 2770Sstevel@tonic-gate (void) fprintf(stderr, "(non promiscuous)\n"); 2780Sstevel@tonic-gate dlpromiscon(netfd, DL_PROMISC_MULTI); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate dlpromiscon(netfd, DL_PROMISC_SAP); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate if (ioctl(netfd, DLIOCRAW, 0) < 0) { 2840Sstevel@tonic-gate close(netfd); 2850Sstevel@tonic-gate pr_err("ioctl: DLIOCRAW: %s: %m", device_path(device)); 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (fp) { 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * push and configure the packet filtering module 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate if (ioctl(netfd, I_PUSH, "pfmod") < 0) { 2930Sstevel@tonic-gate close(netfd); 2940Sstevel@tonic-gate pr_err("ioctl: I_PUSH pfmod: %s: %m", 2950Sstevel@tonic-gate device_path(device)); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 298*410Skcpoon if (strioctl(netfd, PFIOCSETF, -1, sizeof (*fp), 299*410Skcpoon (char *)fp) < 0) { 3000Sstevel@tonic-gate close(netfd); 3010Sstevel@tonic-gate pr_err("PFIOCSETF: %s: %m", device_path(device)); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate if (ioctl(netfd, I_PUSH, "bufmod") < 0) { 3060Sstevel@tonic-gate close(netfd); 3070Sstevel@tonic-gate pr_err("push bufmod: %s: %m", device_path(device)); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (strioctl(netfd, SBIOCSTIME, -1, sizeof (struct timeval), 311*410Skcpoon (char *)timeout) < 0) { 3120Sstevel@tonic-gate close(netfd); 3130Sstevel@tonic-gate pr_err("SBIOCSTIME: %s: %m", device_path(device)); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate if (strioctl(netfd, SBIOCSCHUNK, -1, sizeof (uint_t), 317*410Skcpoon (char *)&chunksize) < 0) { 3180Sstevel@tonic-gate close(netfd); 3190Sstevel@tonic-gate pr_err("SBIOCGCHUNK: %s: %m", device_path(device)); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate if (strioctl(netfd, SBIOCSSNAP, -1, sizeof (uint_t), 323*410Skcpoon (char *)&snaplen) < 0) { 3240Sstevel@tonic-gate close(netfd); 3250Sstevel@tonic-gate pr_err("SBIOCSSNAP: %s: %m", device_path(device)); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Flush the read queue, to get rid of anything that 3300Sstevel@tonic-gate * accumulated before the device reached its final configuration. 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate if (ioctl(netfd, I_FLUSH, FLUSHR) < 0) { 3330Sstevel@tonic-gate close(netfd); 3340Sstevel@tonic-gate pr_err("I_FLUSH: %s: %m", device_path(device)); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate /* 3390Sstevel@tonic-gate * Read packets from the network. Initdevice is called in 3400Sstevel@tonic-gate * here to set up the network interface for reading of 3410Sstevel@tonic-gate * raw ethernet packets in promiscuous mode into a buffer. 3420Sstevel@tonic-gate * Packets are read and either written directly to a file 3430Sstevel@tonic-gate * or interpreted for display on the fly. 3440Sstevel@tonic-gate */ 3450Sstevel@tonic-gate void 3460Sstevel@tonic-gate net_read(chunksize, filter, proc, flags) 3470Sstevel@tonic-gate int chunksize, filter; 3480Sstevel@tonic-gate void (*proc)(); 3490Sstevel@tonic-gate int flags; 3500Sstevel@tonic-gate { 3510Sstevel@tonic-gate int r = 0; 3520Sstevel@tonic-gate struct strbuf data; 3530Sstevel@tonic-gate int flgs; 3540Sstevel@tonic-gate extern int count; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate data.len = 0; 3570Sstevel@tonic-gate count = 0; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* allocate a read buffer */ 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate bufp = malloc(chunksize); 3620Sstevel@tonic-gate if (bufp == NULL) 3630Sstevel@tonic-gate pr_err("no memory for %dk buffer", chunksize); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * read frames 3670Sstevel@tonic-gate */ 3680Sstevel@tonic-gate for (;;) { 3690Sstevel@tonic-gate data.maxlen = chunksize; 3700Sstevel@tonic-gate data.len = 0; 3710Sstevel@tonic-gate data.buf = bufp; 3720Sstevel@tonic-gate flgs = 0; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate r = getmsg(netfd, NULL, &data, &flgs); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate if (r < 0 || quitting) 3770Sstevel@tonic-gate break; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate if (data.len <= 0) 3800Sstevel@tonic-gate continue; 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate scan(bufp, data.len, filter, 0, 0, proc, 0, 0, flags); 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate free(bufp); 3860Sstevel@tonic-gate close(netfd); 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate if (!quitting) { 3890Sstevel@tonic-gate if (r < 0) 3900Sstevel@tonic-gate pr_err("network read failed: %m"); 3910Sstevel@tonic-gate else 3920Sstevel@tonic-gate pr_err("network read returned %d", r); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate #ifdef DEBUG 3970Sstevel@tonic-gate /* 3980Sstevel@tonic-gate * corrupt: simulate packet corruption for debugging interpreters 3990Sstevel@tonic-gate */ 4000Sstevel@tonic-gate void 4010Sstevel@tonic-gate corrupt(volatile char *pktp, volatile char *pstop, char *buf, 4020Sstevel@tonic-gate volatile char *bufstop) 4030Sstevel@tonic-gate { 4040Sstevel@tonic-gate int c; 4050Sstevel@tonic-gate int i; 4060Sstevel@tonic-gate int p; 4070Sstevel@tonic-gate int li = rand() % (pstop - pktp - 1) + 1; 4080Sstevel@tonic-gate volatile char *pp = pktp; 4090Sstevel@tonic-gate volatile char *pe = bufstop < pstop ? bufstop : pstop; 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate if (pktp < buf || pktp > bufstop) 4120Sstevel@tonic-gate return; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate for (pp = pktp; pp < pe; pp += li) { 4150Sstevel@tonic-gate c = ((pe - pp) < li ? pe - pp : li); 4160Sstevel@tonic-gate i = (rand() % c)>>1; 4170Sstevel@tonic-gate while (--i > 0) { 4180Sstevel@tonic-gate p = (rand() % c); 4190Sstevel@tonic-gate pp[p] = (unsigned char)(rand() & 0xFF); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate #endif /* DEBUG */ 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate void 4260Sstevel@tonic-gate scan(buf, len, filter, cap, old, proc, first, last, flags) 4270Sstevel@tonic-gate char *buf; 4280Sstevel@tonic-gate int len, filter, cap, old; 4290Sstevel@tonic-gate void (*proc)(); 4300Sstevel@tonic-gate int first, last; 4310Sstevel@tonic-gate int flags; 4320Sstevel@tonic-gate { 4330Sstevel@tonic-gate volatile char *bp, *bufstop; 4340Sstevel@tonic-gate volatile struct sb_hdr *hdrp; 4350Sstevel@tonic-gate volatile struct sb_hdr nhdr, *nhdrp; 4360Sstevel@tonic-gate volatile char *pktp; 4370Sstevel@tonic-gate volatile struct timeval last_timestamp; 4380Sstevel@tonic-gate volatile int header_okay; 4390Sstevel@tonic-gate extern int count, maxcount; 4400Sstevel@tonic-gate extern int snoop_nrecover; 4410Sstevel@tonic-gate #ifdef DEBUG 4420Sstevel@tonic-gate extern int zflg; 4430Sstevel@tonic-gate #endif /* DEBUG */ 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate proc(0, 0, 0); 4460Sstevel@tonic-gate bufstop = buf + len; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * 4500Sstevel@tonic-gate * Loop through each packet in the buffer 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate last_timestamp.tv_sec = 0; 4530Sstevel@tonic-gate (void) memcpy((char *)ojmp_env, (char *)jmp_env, sizeof (jmp_env)); 4540Sstevel@tonic-gate for (bp = buf; bp < bufstop; bp += nhdrp->sbh_totlen) { 4550Sstevel@tonic-gate /* 4560Sstevel@tonic-gate * Gracefully exit if user terminates 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate if (quitting) 4590Sstevel@tonic-gate break; 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * Global error recocery: Prepare to continue when a corrupt 4620Sstevel@tonic-gate * packet or header is encountered. 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate if (sigsetjmp(jmp_env, 1)) { 4650Sstevel@tonic-gate goto err; 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate header_okay = 0; 4690Sstevel@tonic-gate hdrp = (struct sb_hdr *)bp; 4700Sstevel@tonic-gate nhdrp = hdrp; 4710Sstevel@tonic-gate pktp = (char *)hdrp + sizeof (*hdrp); 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate /* 4740Sstevel@tonic-gate * If reading a capture file 4750Sstevel@tonic-gate * convert the headers from network 4760Sstevel@tonic-gate * byte order (for little-endians like X86) 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate if (cap) { 4790Sstevel@tonic-gate /* 4800Sstevel@tonic-gate * If the packets come from an old 4810Sstevel@tonic-gate * capture file, convert the header. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate if (old) { 4840Sstevel@tonic-gate convert_old(hdrp); 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate nhdrp = &nhdr; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate nhdrp->sbh_origlen = ntohl(hdrp->sbh_origlen); 4900Sstevel@tonic-gate nhdrp->sbh_msglen = ntohl(hdrp->sbh_msglen); 4910Sstevel@tonic-gate nhdrp->sbh_totlen = ntohl(hdrp->sbh_totlen); 4920Sstevel@tonic-gate nhdrp->sbh_drops = ntohl(hdrp->sbh_drops); 4930Sstevel@tonic-gate nhdrp->sbh_timestamp.tv_sec = 4940Sstevel@tonic-gate ntohl(hdrp->sbh_timestamp.tv_sec); 4950Sstevel@tonic-gate nhdrp->sbh_timestamp.tv_usec = 4960Sstevel@tonic-gate ntohl(hdrp->sbh_timestamp.tv_usec); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* Enhanced check for valid header */ 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate if ((nhdrp->sbh_totlen == 0) || 5020Sstevel@tonic-gate (bp + nhdrp->sbh_totlen) < bp || 5030Sstevel@tonic-gate (bp + nhdrp->sbh_totlen) > bufstop || 5040Sstevel@tonic-gate (nhdrp->sbh_origlen == 0) || 5050Sstevel@tonic-gate (bp + nhdrp->sbh_origlen) < bp || 5060Sstevel@tonic-gate (nhdrp->sbh_msglen == 0) || 5070Sstevel@tonic-gate (bp + nhdrp->sbh_msglen) < bp || 5080Sstevel@tonic-gate (bp + nhdrp->sbh_msglen) > bufstop || 5090Sstevel@tonic-gate (nhdrp->sbh_msglen > nhdrp->sbh_origlen) || 5100Sstevel@tonic-gate (nhdrp->sbh_totlen < nhdrp->sbh_msglen) || 5110Sstevel@tonic-gate (nhdrp->sbh_timestamp.tv_sec == 0)) { 5120Sstevel@tonic-gate if (cap) 5130Sstevel@tonic-gate fprintf(stderr, "(warning) bad packet header " 5140Sstevel@tonic-gate "in capture file"); 5150Sstevel@tonic-gate else 5160Sstevel@tonic-gate fprintf(stderr, "(warning) bad packet header " 5170Sstevel@tonic-gate "in buffer"); 5180Sstevel@tonic-gate (void) fprintf(stderr, " offset %d: length=%d\n", 5190Sstevel@tonic-gate bp - buf, nhdrp->sbh_totlen); 5200Sstevel@tonic-gate goto err; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate if (nhdrp->sbh_totlen > 5240Sstevel@tonic-gate (uint_t)(netdl.info_ack.dl_max_sdu + MAX_HDRTRAILER)) { 5250Sstevel@tonic-gate if (cap) 5260Sstevel@tonic-gate fprintf(stderr, "(warning) packet length " 5270Sstevel@tonic-gate "greater than MTU in capture file"); 5280Sstevel@tonic-gate else 5290Sstevel@tonic-gate fprintf(stderr, "(warning) packet length " 5300Sstevel@tonic-gate "greater than MTU in buffer"); 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate (void) fprintf(stderr, " offset %d: length=%d\n", 5330Sstevel@tonic-gate bp - buf, nhdrp->sbh_totlen); 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * Check for incomplete packet. We are conservative here, 5380Sstevel@tonic-gate * since we don't know how good the checking is in other 5390Sstevel@tonic-gate * parts of the code. We pass a partial packet, with 5400Sstevel@tonic-gate * a warning. 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate if (pktp + nhdrp->sbh_msglen > bufstop) { 5430Sstevel@tonic-gate fprintf(stderr, "truncated packet buffer\n"); 5440Sstevel@tonic-gate nhdrp->sbh_msglen = bufstop - pktp; 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate #ifdef DEBUG 5480Sstevel@tonic-gate if (zflg) 5490Sstevel@tonic-gate corrupt(pktp, pktp + nhdrp->sbh_msglen, buf, bufstop); 5500Sstevel@tonic-gate #endif /* DEBUG */ 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate header_okay = 1; 5530Sstevel@tonic-gate if (!filter || 5540Sstevel@tonic-gate want_packet(pktp, 5550Sstevel@tonic-gate nhdrp->sbh_msglen, 5560Sstevel@tonic-gate nhdrp->sbh_origlen)) { 5570Sstevel@tonic-gate count++; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * Start deadman timer for interpreter processing 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate (void) snoop_alarm(SNOOP_ALARM_GRAN*SNOOP_MAXRECOVER, 5630Sstevel@tonic-gate NULL); 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate encap_levels = 0; 5660Sstevel@tonic-gate if (!cap || count >= first) 5670Sstevel@tonic-gate proc(nhdrp, pktp, count, flags); 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate if (cap && count >= last) { 5700Sstevel@tonic-gate (void) snoop_alarm(0, NULL); 5710Sstevel@tonic-gate break; 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (maxcount && count >= maxcount) { 5750Sstevel@tonic-gate fprintf(stderr, "%d packets captured\n", count); 5760Sstevel@tonic-gate exit(0); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate snoop_nrecover = 0; /* success */ 5800Sstevel@tonic-gate (void) snoop_alarm(0, NULL); 5810Sstevel@tonic-gate last_timestamp = hdrp->sbh_timestamp; /* save stamp */ 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate continue; 5840Sstevel@tonic-gate err: 5850Sstevel@tonic-gate /* 5860Sstevel@tonic-gate * Corruption has been detected. Reset errors. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate snoop_recover(); 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * packet header was apparently okay. Continue. 5920Sstevel@tonic-gate */ 5930Sstevel@tonic-gate if (header_okay) 5940Sstevel@tonic-gate continue; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate /* 5970Sstevel@tonic-gate * Otherwise try to scan forward to the next packet, using 5980Sstevel@tonic-gate * the last known timestamp if it is available. 5990Sstevel@tonic-gate */ 6000Sstevel@tonic-gate nhdrp = &nhdr; 6010Sstevel@tonic-gate nhdrp->sbh_totlen = 0; 6020Sstevel@tonic-gate if (last_timestamp.tv_sec == 0) { 6030Sstevel@tonic-gate bp += sizeof (int); 6040Sstevel@tonic-gate } else { 6050Sstevel@tonic-gate for (bp += sizeof (int); bp <= bufstop; 6060Sstevel@tonic-gate bp += sizeof (int)) { 6070Sstevel@tonic-gate hdrp = (struct sb_hdr *)bp; 6080Sstevel@tonic-gate /* An approximate timestamp located */ 6090Sstevel@tonic-gate if ((hdrp->sbh_timestamp.tv_sec >> 8) == 6100Sstevel@tonic-gate (last_timestamp.tv_sec >> 8)) 6110Sstevel@tonic-gate break; 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate /* reset jmp_env for program exit */ 6160Sstevel@tonic-gate (void) memcpy((char *)jmp_env, (char *)ojmp_env, sizeof (jmp_env)); 6170Sstevel@tonic-gate proc(0, -1, 0); 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * Called if nwrite() encounters write problems. 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate static void 6240Sstevel@tonic-gate cap_write_error(const char *msgtype) 6250Sstevel@tonic-gate { 6260Sstevel@tonic-gate (void) fprintf(stderr, 6270Sstevel@tonic-gate "snoop: cannot write %s to capture file: %s\n", 6280Sstevel@tonic-gate msgtype, strerror(errno)); 6290Sstevel@tonic-gate exit(1); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate /* 6330Sstevel@tonic-gate * Writes target buffer to the open file descriptor. Upon detection of a short 6340Sstevel@tonic-gate * write, an attempt to process the remaining bytes occurs until all anticipated 6350Sstevel@tonic-gate * bytes are written. An error status is returned to indicate any serious write 6360Sstevel@tonic-gate * failures. 6370Sstevel@tonic-gate */ 6380Sstevel@tonic-gate static int 6390Sstevel@tonic-gate nwrite(int fd, const void *buffer, size_t buflen) 6400Sstevel@tonic-gate { 6410Sstevel@tonic-gate size_t nwritten; 6420Sstevel@tonic-gate ssize_t nbytes = 0; 6430Sstevel@tonic-gate const char *buf = buffer; 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate for (nwritten = 0; nwritten < buflen; nwritten += nbytes) { 6460Sstevel@tonic-gate nbytes = write(fd, &buf[nwritten], buflen - nwritten); 6470Sstevel@tonic-gate if (nbytes == -1) 6480Sstevel@tonic-gate return (-1); 6490Sstevel@tonic-gate if (nbytes == 0) { 6500Sstevel@tonic-gate errno = EIO; 6510Sstevel@tonic-gate return (-1); 6520Sstevel@tonic-gate } 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate return (0); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* 6580Sstevel@tonic-gate * Routines for opening, closing, reading and writing 6590Sstevel@tonic-gate * a capture file of packets saved with the -o option. 6600Sstevel@tonic-gate */ 6610Sstevel@tonic-gate static int capfile_out; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * The snoop capture file has a header to identify 6650Sstevel@tonic-gate * it as a capture file and record its version. 6660Sstevel@tonic-gate * A file without this header is assumed to be an 6670Sstevel@tonic-gate * old format snoop file. 6680Sstevel@tonic-gate * 6690Sstevel@tonic-gate * A version 1 header looks like this: 6700Sstevel@tonic-gate * 6710Sstevel@tonic-gate * 0 1 2 3 4 5 6 7 8 9 10 11 6720Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+ 6730Sstevel@tonic-gate * | s | n | o | o | p | \0| \0| \0| version | data 6740Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+ 6750Sstevel@tonic-gate * | word 0 | word 1 | word 2 | 6760Sstevel@tonic-gate * 6770Sstevel@tonic-gate * 6780Sstevel@tonic-gate * A version 2 header adds a word that identifies the MAC type. 6790Sstevel@tonic-gate * This allows for capture files from FDDI etc. 6800Sstevel@tonic-gate * 6810Sstevel@tonic-gate * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 6820Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 6830Sstevel@tonic-gate * | s | n | o | o | p | \0| \0| \0| version | MAC type | data 6840Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 6850Sstevel@tonic-gate * | word 0 | word 1 | word 2 | word 3 6860Sstevel@tonic-gate * 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate const char *snoop_id = "snoop\0\0\0"; 6890Sstevel@tonic-gate const int snoop_idlen = 8; 6900Sstevel@tonic-gate const int snoop_version = 2; 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate void 6930Sstevel@tonic-gate cap_open_write(name) 6940Sstevel@tonic-gate char *name; 6950Sstevel@tonic-gate { 6960Sstevel@tonic-gate int vers; 6970Sstevel@tonic-gate int rc; 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate capfile_out = open(name, O_CREAT | O_TRUNC | O_RDWR, 0666); 7000Sstevel@tonic-gate if (capfile_out < 0) 7010Sstevel@tonic-gate pr_err("%s: %m", name); 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate vers = htonl(snoop_version); 7040Sstevel@tonic-gate if ((rc = nwrite(capfile_out, snoop_id, snoop_idlen)) == -1) 7050Sstevel@tonic-gate cap_write_error("snoop_id"); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate if ((rc = nwrite(capfile_out, &vers, sizeof (int))) == -1) 7080Sstevel@tonic-gate cap_write_error("version"); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate void 7130Sstevel@tonic-gate cap_close() 7140Sstevel@tonic-gate { 7150Sstevel@tonic-gate close(capfile_out); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate static char *cap_buffp = NULL; 7190Sstevel@tonic-gate static int cap_len = 0; 7200Sstevel@tonic-gate static int cap_new; 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate void 7230Sstevel@tonic-gate cap_open_read(name) 7240Sstevel@tonic-gate char *name; 7250Sstevel@tonic-gate { 7260Sstevel@tonic-gate struct stat st; 7270Sstevel@tonic-gate int cap_vers; 7280Sstevel@tonic-gate int *word, device_mac_type; 7290Sstevel@tonic-gate int capfile_in; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate capfile_in = open(name, O_RDONLY); 7320Sstevel@tonic-gate if (capfile_in < 0) 7330Sstevel@tonic-gate pr_err("couldn't open %s: %m", name); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate if (fstat(capfile_in, &st) < 0) 7360Sstevel@tonic-gate pr_err("couldn't stat %s: %m", name); 7370Sstevel@tonic-gate cap_len = st.st_size; 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate cap_buffp = mmap(0, cap_len, PROT_READ, MAP_PRIVATE, capfile_in, 0); 7400Sstevel@tonic-gate close(capfile_in); 7410Sstevel@tonic-gate if ((int)cap_buffp == -1) 7420Sstevel@tonic-gate pr_err("couldn't mmap %s: %m", name); 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* Check if new snoop capture file format */ 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate cap_new = bcmp(cap_buffp, snoop_id, snoop_idlen) == 0; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate /* 7490Sstevel@tonic-gate * If new file - check version and 7500Sstevel@tonic-gate * set buffer pointer to point at first packet 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate if (cap_new) { 7530Sstevel@tonic-gate cap_vers = ntohl(*(int *)(cap_buffp + snoop_idlen)); 7540Sstevel@tonic-gate cap_buffp += snoop_idlen + sizeof (int); 7550Sstevel@tonic-gate cap_len -= snoop_idlen + sizeof (int); 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate switch (cap_vers) { 7580Sstevel@tonic-gate case 1: 7590Sstevel@tonic-gate device_mac_type = DL_ETHER; 7600Sstevel@tonic-gate break; 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate case 2: 7630Sstevel@tonic-gate device_mac_type = ntohl(*((int *)cap_buffp)); 7640Sstevel@tonic-gate cap_buffp += sizeof (int); 7650Sstevel@tonic-gate cap_len -= sizeof (int); 7660Sstevel@tonic-gate break; 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate default: 7690Sstevel@tonic-gate pr_err("capture file: %s: Version %d unrecognized\n", 7700Sstevel@tonic-gate name, cap_vers); 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate for (interface = &INTERFACES[0]; interface->mac_type != -1; 7740Sstevel@tonic-gate interface++) 7750Sstevel@tonic-gate if (interface->mac_type == device_mac_type) 7760Sstevel@tonic-gate break; 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate if (interface->mac_type == -1) 7790Sstevel@tonic-gate pr_err("Mac Type = %x is not supported\n", 7800Sstevel@tonic-gate device_mac_type); 7810Sstevel@tonic-gate } else { 7820Sstevel@tonic-gate /* Use heuristic to check if it's an old-style file */ 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate device_mac_type = DL_ETHER; 7850Sstevel@tonic-gate word = (int *)cap_buffp; 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate if (!((word[0] < 1600 && word[1] < 1600) && 7880Sstevel@tonic-gate (word[0] < word[1]) && 7890Sstevel@tonic-gate (word[2] > 610000000 && word[2] < 770000000))) 7900Sstevel@tonic-gate pr_err("not a capture file: %s", name); 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* Change protection so's we can fix the headers */ 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate if (mprotect(cap_buffp, cap_len, PROT_READ | PROT_WRITE) < 0) 7950Sstevel@tonic-gate pr_err("mprotect: %s: %m", name); 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate netdl.info_ack.dl_max_sdu = MAXINT; /* Decode any stored packet. */ 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate void 8010Sstevel@tonic-gate cap_read(first, last, filter, proc, flags) 8020Sstevel@tonic-gate int first, last; 8030Sstevel@tonic-gate int filter; 8040Sstevel@tonic-gate void (*proc)(); 8050Sstevel@tonic-gate int flags; 8060Sstevel@tonic-gate { 8070Sstevel@tonic-gate extern int count; 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate count = 0; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate scan(cap_buffp, cap_len, filter, 1, !cap_new, proc, first, last, flags); 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate munmap(cap_buffp, cap_len); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate void 8170Sstevel@tonic-gate cap_write(hdrp, pktp, num, flags) 8180Sstevel@tonic-gate struct sb_hdr *hdrp; 8190Sstevel@tonic-gate char *pktp; 8200Sstevel@tonic-gate int num, flags; 8210Sstevel@tonic-gate { 8220Sstevel@tonic-gate int pktlen, mac; 8230Sstevel@tonic-gate static int first = 1; 8240Sstevel@tonic-gate struct sb_hdr nhdr; 8250Sstevel@tonic-gate extern boolean_t qflg; 8260Sstevel@tonic-gate int rc; 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate if (hdrp == NULL) 8290Sstevel@tonic-gate return; 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate if (first) { 8320Sstevel@tonic-gate first = 0; 8330Sstevel@tonic-gate mac = htonl(interface->mac_type); 8340Sstevel@tonic-gate if ((rc = nwrite(capfile_out, &mac, sizeof (int))) == -1) 8350Sstevel@tonic-gate cap_write_error("mac_type"); 8360Sstevel@tonic-gate } 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate pktlen = hdrp->sbh_totlen - sizeof (*hdrp); 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate /* 8410Sstevel@tonic-gate * Convert sb_hdr to network byte order 8420Sstevel@tonic-gate */ 8430Sstevel@tonic-gate nhdr.sbh_origlen = htonl(hdrp->sbh_origlen); 8440Sstevel@tonic-gate nhdr.sbh_msglen = htonl(hdrp->sbh_msglen); 8450Sstevel@tonic-gate nhdr.sbh_totlen = htonl(hdrp->sbh_totlen); 8460Sstevel@tonic-gate nhdr.sbh_drops = htonl(hdrp->sbh_drops); 8470Sstevel@tonic-gate nhdr.sbh_timestamp.tv_sec = htonl(hdrp->sbh_timestamp.tv_sec); 8480Sstevel@tonic-gate nhdr.sbh_timestamp.tv_usec = htonl(hdrp->sbh_timestamp.tv_usec); 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate if ((rc = nwrite(capfile_out, &nhdr, sizeof (nhdr))) == -1) 8510Sstevel@tonic-gate cap_write_error("packet header"); 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if ((rc = nwrite(capfile_out, pktp, pktlen)) == -1) 8540Sstevel@tonic-gate cap_write_error("packet"); 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate if (! qflg) 8570Sstevel@tonic-gate show_count(); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate /* 8610Sstevel@tonic-gate * Old header format. 8620Sstevel@tonic-gate * Actually two concatenated structs: nit_bufhdr + nit_head 8630Sstevel@tonic-gate */ 8640Sstevel@tonic-gate struct ohdr { 8650Sstevel@tonic-gate /* nit_bufhdr */ 8660Sstevel@tonic-gate int o_msglen; 8670Sstevel@tonic-gate int o_totlen; 8680Sstevel@tonic-gate /* nit_head */ 8690Sstevel@tonic-gate struct timeval o_time; 8700Sstevel@tonic-gate int o_drops; 8710Sstevel@tonic-gate int o_len; 8720Sstevel@tonic-gate }; 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate /* 8750Sstevel@tonic-gate * Convert a packet header from 8760Sstevel@tonic-gate * old to new format. 8770Sstevel@tonic-gate */ 8780Sstevel@tonic-gate void 8790Sstevel@tonic-gate convert_old(ohdrp) 8800Sstevel@tonic-gate struct ohdr *ohdrp; 8810Sstevel@tonic-gate { 8820Sstevel@tonic-gate struct sb_hdr nhdr; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate nhdr.sbh_origlen = ohdrp->o_len; 8850Sstevel@tonic-gate nhdr.sbh_msglen = ohdrp->o_msglen; 8860Sstevel@tonic-gate nhdr.sbh_totlen = ohdrp->o_totlen; 8870Sstevel@tonic-gate nhdr.sbh_drops = ohdrp->o_drops; 8880Sstevel@tonic-gate nhdr.sbh_timestamp = ohdrp->o_time; 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate *(struct sb_hdr *)ohdrp = nhdr; 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 893*410Skcpoon static int 894*410Skcpoon strioctl(int fd, int cmd, int timout, int len, char *dp) 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate struct strioctl sioc; 8970Sstevel@tonic-gate int rc; 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate sioc.ic_cmd = cmd; 9000Sstevel@tonic-gate sioc.ic_timout = timout; 9010Sstevel@tonic-gate sioc.ic_len = len; 9020Sstevel@tonic-gate sioc.ic_dp = dp; 9030Sstevel@tonic-gate rc = ioctl(fd, I_STR, &sioc); 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate if (rc < 0) 9060Sstevel@tonic-gate return (rc); 9070Sstevel@tonic-gate else 9080Sstevel@tonic-gate return (sioc.ic_len); 9090Sstevel@tonic-gate } 910