17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22e11c3f44Smeem * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*7d897698SMilan Jurik * Copyright 2012 Milan Jurik. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
29b78ff649Smeem #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <setjmp.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/signal.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <sys/socket.h>
377c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
397c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
407c478bd9Sstevel@tonic-gate #include <sys/pfmod.h>
417c478bd9Sstevel@tonic-gate #include <sys/mman.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/bufmod.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <stropts.h>
477c478bd9Sstevel@tonic-gate #include <stdlib.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <values.h>
507c478bd9Sstevel@tonic-gate #include <libdlpi.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #include "snoop.h"
537c478bd9Sstevel@tonic-gate
5445916cd2Sjpk /*
5545916cd2Sjpk * Old header format.
5645916cd2Sjpk * Actually two concatenated structs: nit_bufhdr + nit_head
5745916cd2Sjpk */
5845916cd2Sjpk struct ohdr {
5945916cd2Sjpk /* nit_bufhdr */
6045916cd2Sjpk int o_msglen;
6145916cd2Sjpk int o_totlen;
6245916cd2Sjpk /* nit_head */
6345916cd2Sjpk struct timeval o_time;
6445916cd2Sjpk int o_drops;
6545916cd2Sjpk int o_len;
6645916cd2Sjpk };
6745916cd2Sjpk
6845916cd2Sjpk static void scan(char *, int, int, int, int, void (*)(), int, int, int);
697c478bd9Sstevel@tonic-gate void convert_to_network();
707c478bd9Sstevel@tonic-gate void convert_from_network();
7145916cd2Sjpk static void convert_old(struct ohdr *);
727c478bd9Sstevel@tonic-gate extern sigjmp_buf jmp_env, ojmp_env;
7345916cd2Sjpk static char *bufp; /* pointer to read buffer */
747c478bd9Sstevel@tonic-gate
7545916cd2Sjpk static int strioctl(int, int, int, int, void *);
762e3b6467Skcpoon
77b78ff649Smeem enum { DWA_NONE, DWA_EXISTS, DWA_PLUMBED };
78b78ff649Smeem
79b78ff649Smeem typedef struct dlpi_walk_arg {
80b78ff649Smeem char dwa_linkname[MAXLINKNAMELEN];
81b78ff649Smeem int dwa_type; /* preference type above */
82b78ff649Smeem int dwa_s4; /* IPv4 socket */
83b78ff649Smeem int dwa_s6; /* IPv6 socket */
84b78ff649Smeem } dlpi_walk_arg_t;
85b78ff649Smeem
86b78ff649Smeem static boolean_t
select_datalink(const char * linkname,void * arg)87b78ff649Smeem select_datalink(const char *linkname, void *arg)
88b78ff649Smeem {
89b78ff649Smeem struct lifreq lifr;
90b78ff649Smeem dlpi_walk_arg_t *dwap = arg;
91b78ff649Smeem int s4 = dwap->dwa_s4;
92b78ff649Smeem int s6 = dwap->dwa_s6;
93b78ff649Smeem
94b78ff649Smeem (void) strlcpy(dwap->dwa_linkname, linkname, MAXLINKNAMELEN);
95b78ff649Smeem dwap->dwa_type = DWA_EXISTS;
96b78ff649Smeem
977c478bd9Sstevel@tonic-gate /*
98b78ff649Smeem * See if it's plumbed by IP. We prefer such links because they're
99b78ff649Smeem * more likely to have interesting traffic.
100b78ff649Smeem */
101b78ff649Smeem bzero(&lifr, sizeof (lifr));
102b78ff649Smeem (void) strlcpy(lifr.lifr_name, linkname, LIFNAMSIZ);
103b78ff649Smeem if ((s4 != -1 && ioctl(s4, SIOCGLIFFLAGS, &lifr) != -1) ||
104b78ff649Smeem (s6 != -1 && ioctl(s6, SIOCGLIFFLAGS, &lifr) != -1)) {
105b78ff649Smeem dwap->dwa_type = DWA_PLUMBED;
106b78ff649Smeem return (B_TRUE);
107b78ff649Smeem }
108b78ff649Smeem return (B_FALSE);
109b78ff649Smeem }
110b78ff649Smeem
111b78ff649Smeem /*
112b78ff649Smeem * Open `linkname' in raw/passive mode (see dlpi_open(3DLPI)). If `linkname'
113b78ff649Smeem * is NULL, pick a datalink as per snoop(1M). Also gather some information
114b78ff649Smeem * about the datalink useful for building the proper packet filters.
1157c478bd9Sstevel@tonic-gate */
1162e3b6467Skcpoon boolean_t
open_datalink(dlpi_handle_t * dhp,const char * linkname)117b78ff649Smeem open_datalink(dlpi_handle_t *dhp, const char *linkname)
1187c478bd9Sstevel@tonic-gate {
119c7e4935fSss150715 int retval;
120b127ac41SPhilip Kirk int flags = DLPI_PASSIVE | DLPI_RAW;
121b78ff649Smeem dlpi_walk_arg_t dwa;
1225fead0e0SRoamer dlpi_info_t dlinfo;
123c7e4935fSss150715
124b78ff649Smeem if (linkname == NULL) {
1257c478bd9Sstevel@tonic-gate /*
126b78ff649Smeem * Select a datalink to use by default. Prefer datalinks that
127b78ff649Smeem * are plumbed by IP.
1287c478bd9Sstevel@tonic-gate */
129b78ff649Smeem bzero(&dwa, sizeof (dwa));
130b78ff649Smeem dwa.dwa_s4 = socket(AF_INET, SOCK_DGRAM, 0);
131b78ff649Smeem dwa.dwa_s6 = socket(AF_INET6, SOCK_DGRAM, 0);
132b78ff649Smeem dlpi_walk(select_datalink, &dwa, 0);
133b78ff649Smeem (void) close(dwa.dwa_s4);
134b78ff649Smeem (void) close(dwa.dwa_s6);
1357c478bd9Sstevel@tonic-gate
136b78ff649Smeem if (dwa.dwa_type == DWA_NONE)
137b78ff649Smeem pr_err("no datalinks found");
138b78ff649Smeem if (dwa.dwa_type == DWA_EXISTS) {
139b78ff649Smeem (void) fprintf(stderr, "snoop: WARNING: "
140b78ff649Smeem "no datalinks plumbed for IP traffic\n");
1417c478bd9Sstevel@tonic-gate }
142b78ff649Smeem linkname = dwa.dwa_linkname;
1437c478bd9Sstevel@tonic-gate }
144b127ac41SPhilip Kirk if (Iflg)
145b127ac41SPhilip Kirk flags |= DLPI_DEVIPNET;
146b78ff649Smeem if (Iflg || strcmp(linkname, "lo0") == 0)
147b127ac41SPhilip Kirk flags |= DLPI_IPNETINFO;
148b78ff649Smeem if ((retval = dlpi_open(linkname, dhp, flags)) != DLPI_SUCCESS) {
149b78ff649Smeem pr_err("cannot open \"%s\": %s", linkname,
150c7e4935fSss150715 dlpi_strerror(retval));
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
153c7e4935fSss150715 if ((retval = dlpi_info(*dhp, &dlinfo, 0)) != DLPI_SUCCESS)
154c7e4935fSss150715 pr_errdlpi(*dhp, "dlpi_info failed", retval);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate for (interface = &INTERFACES[0]; interface->mac_type != -1; interface++)
157c7e4935fSss150715 if (interface->mac_type == dlinfo.di_mactype)
1587c478bd9Sstevel@tonic-gate break;
1597c478bd9Sstevel@tonic-gate
160c7e4935fSss150715 /* allow limited functionality even if interface isn't known */
1617c478bd9Sstevel@tonic-gate if (interface->mac_type == -1) {
162c7e4935fSss150715 (void) fprintf(stderr, "snoop: WARNING: Mac Type = %x "
163c7e4935fSss150715 "not supported\n", dlinfo.di_mactype);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
166605445d5Sdg199075 return (interface->try_kernel_filter);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
170b78ff649Smeem * Initialize `dh' for packet capture using the provided arguments.
1717c478bd9Sstevel@tonic-gate */
1727c478bd9Sstevel@tonic-gate void
init_datalink(dlpi_handle_t dh,ulong_t snaplen,ulong_t chunksize,struct timeval * timeout,struct Pf_ext_packetfilt * fp)173b78ff649Smeem init_datalink(dlpi_handle_t dh, ulong_t snaplen, ulong_t chunksize,
174c7e4935fSss150715 struct timeval *timeout, struct Pf_ext_packetfilt *fp)
1757c478bd9Sstevel@tonic-gate {
176c7e4935fSss150715 int retv;
177c7e4935fSss150715 int netfd;
1787c478bd9Sstevel@tonic-gate
179c7e4935fSss150715 retv = dlpi_bind(dh, DLPI_ANY_SAP, NULL);
180c7e4935fSss150715 if (retv != DLPI_SUCCESS)
181c7e4935fSss150715 pr_errdlpi(dh, "cannot bind on", retv);
1827c478bd9Sstevel@tonic-gate
183b127ac41SPhilip Kirk if (Iflg) {
184b127ac41SPhilip Kirk (void) fprintf(stderr, "Using device ipnet/%s ",
185b127ac41SPhilip Kirk dlpi_linkname(dh));
186b127ac41SPhilip Kirk } else {
187c7e4935fSss150715 (void) fprintf(stderr, "Using device %s ", dlpi_linkname(dh));
188b127ac41SPhilip Kirk }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * If Pflg not set - use physical level
1927c478bd9Sstevel@tonic-gate * promiscuous mode. Otherwise - just SAP level.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate if (!Pflg) {
1957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "(promiscuous mode)\n");
196c7e4935fSss150715 retv = dlpi_promiscon(dh, DL_PROMISC_PHYS);
197c7e4935fSss150715 if (retv != DLPI_SUCCESS) {
198c7e4935fSss150715 pr_errdlpi(dh, "promiscuous mode(physical) failed",
199c7e4935fSss150715 retv);
200c7e4935fSss150715 }
2017c478bd9Sstevel@tonic-gate } else {
2027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "(non promiscuous)\n");
203c7e4935fSss150715 retv = dlpi_promiscon(dh, DL_PROMISC_MULTI);
204c7e4935fSss150715 if (retv != DLPI_SUCCESS) {
205c7e4935fSss150715 pr_errdlpi(dh, "promiscuous mode(multicast) failed",
206c7e4935fSss150715 retv);
207c7e4935fSss150715 }
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
210c7e4935fSss150715 retv = dlpi_promiscon(dh, DL_PROMISC_SAP);
211c7e4935fSss150715 if (retv != DLPI_SUCCESS)
212c7e4935fSss150715 pr_errdlpi(dh, "promiscuous mode(SAP) failed", retv);
2137c478bd9Sstevel@tonic-gate
214c7e4935fSss150715 netfd = dlpi_fd(dh);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (fp) {
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * push and configure the packet filtering module
2197c478bd9Sstevel@tonic-gate */
220c7e4935fSss150715 if (ioctl(netfd, I_PUSH, "pfmod") < 0)
221c7e4935fSss150715 pr_errdlpi(dh, "cannot push \"pfmod\"", DL_SYSERR);
2227c478bd9Sstevel@tonic-gate
2232e3b6467Skcpoon if (strioctl(netfd, PFIOCSETF, -1, sizeof (*fp),
224c7e4935fSss150715 (char *)fp) < 0)
225c7e4935fSss150715 pr_errdlpi(dh, "PFIOCSETF", DL_SYSERR);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
228c7e4935fSss150715 if (ioctl(netfd, I_PUSH, "bufmod") < 0)
229c7e4935fSss150715 pr_errdlpi(dh, "cannot push \"bufmod\"", DL_SYSERR);
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (strioctl(netfd, SBIOCSTIME, -1, sizeof (struct timeval),
232c7e4935fSss150715 (char *)timeout) < 0)
233c7e4935fSss150715 pr_errdlpi(dh, "SBIOCSTIME", DL_SYSERR);
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate if (strioctl(netfd, SBIOCSCHUNK, -1, sizeof (uint_t),
236c7e4935fSss150715 (char *)&chunksize) < 0)
237c7e4935fSss150715 pr_errdlpi(dh, "SBIOCGCHUNK", DL_SYSERR);
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate if (strioctl(netfd, SBIOCSSNAP, -1, sizeof (uint_t),
240c7e4935fSss150715 (char *)&snaplen) < 0)
241c7e4935fSss150715 pr_errdlpi(dh, "SBIOCSSNAP", DL_SYSERR);
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * Flush the read queue, to get rid of anything that
2457c478bd9Sstevel@tonic-gate * accumulated before the device reached its final configuration.
2467c478bd9Sstevel@tonic-gate */
247c7e4935fSss150715 if (ioctl(netfd, I_FLUSH, FLUSHR) < 0)
248c7e4935fSss150715 pr_errdlpi(dh, "cannot flush \"I_FLUSH\"", DL_SYSERR);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
252b78ff649Smeem * Read packets from the network. init_datalink() is called in
2537c478bd9Sstevel@tonic-gate * here to set up the network interface for reading of
2547c478bd9Sstevel@tonic-gate * raw ethernet packets in promiscuous mode into a buffer.
2557c478bd9Sstevel@tonic-gate * Packets are read and either written directly to a file
2567c478bd9Sstevel@tonic-gate * or interpreted for display on the fly.
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate void
net_read(dlpi_handle_t dh,size_t chunksize,int filter,void (* proc)(),int flags)259c7e4935fSss150715 net_read(dlpi_handle_t dh, size_t chunksize, int filter, void (*proc)(),
260c7e4935fSss150715 int flags)
2617c478bd9Sstevel@tonic-gate {
262c7e4935fSss150715 int retval;
2637c478bd9Sstevel@tonic-gate extern int count;
264c7e4935fSss150715 size_t msglen;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate count = 0;
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /* allocate a read buffer */
2697c478bd9Sstevel@tonic-gate bufp = malloc(chunksize);
2707c478bd9Sstevel@tonic-gate if (bufp == NULL)
271c7e4935fSss150715 pr_err("no memory for %d buffer", chunksize);
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * read frames
2757c478bd9Sstevel@tonic-gate */
2767c478bd9Sstevel@tonic-gate for (;;) {
277c7e4935fSss150715 msglen = chunksize;
278c7e4935fSss150715 retval = dlpi_recv(dh, NULL, NULL, bufp, &msglen, -1, NULL);
2797c478bd9Sstevel@tonic-gate
280c7e4935fSss150715 if (retval != DLPI_SUCCESS || quitting)
2817c478bd9Sstevel@tonic-gate break;
2827c478bd9Sstevel@tonic-gate
283c7e4935fSss150715 if (msglen != 0)
284c7e4935fSss150715 scan(bufp, msglen, filter, 0, 0, proc, 0, 0, flags);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate free(bufp);
2887c478bd9Sstevel@tonic-gate
289c7e4935fSss150715 if (!quitting)
290c7e4935fSss150715 pr_errdlpi(dh, "network read failed", retval);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate #ifdef DEBUG
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate * corrupt: simulate packet corruption for debugging interpreters
2967c478bd9Sstevel@tonic-gate */
2977c478bd9Sstevel@tonic-gate void
corrupt(volatile char * pktp,volatile char * pstop,char * buf,volatile char * bufstop)2987c478bd9Sstevel@tonic-gate corrupt(volatile char *pktp, volatile char *pstop, char *buf,
2997c478bd9Sstevel@tonic-gate volatile char *bufstop)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate int c;
3027c478bd9Sstevel@tonic-gate int i;
3037c478bd9Sstevel@tonic-gate int p;
3047c478bd9Sstevel@tonic-gate int li = rand() % (pstop - pktp - 1) + 1;
3057c478bd9Sstevel@tonic-gate volatile char *pp = pktp;
3067c478bd9Sstevel@tonic-gate volatile char *pe = bufstop < pstop ? bufstop : pstop;
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate if (pktp < buf || pktp > bufstop)
3097c478bd9Sstevel@tonic-gate return;
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate for (pp = pktp; pp < pe; pp += li) {
3127c478bd9Sstevel@tonic-gate c = ((pe - pp) < li ? pe - pp : li);
3137c478bd9Sstevel@tonic-gate i = (rand() % c)>>1;
3147c478bd9Sstevel@tonic-gate while (--i > 0) {
3157c478bd9Sstevel@tonic-gate p = (rand() % c);
3167c478bd9Sstevel@tonic-gate pp[p] = (unsigned char)(rand() & 0xFF);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3217c478bd9Sstevel@tonic-gate
32245916cd2Sjpk static void
scan(char * buf,int len,int filter,int cap,int old,void (* proc)(),int first,int last,int flags)32345916cd2Sjpk scan(char *buf, int len, int filter, int cap, int old, void (*proc)(),
32445916cd2Sjpk int first, int last, int flags)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate volatile char *bp, *bufstop;
3277c478bd9Sstevel@tonic-gate volatile struct sb_hdr *hdrp;
3287c478bd9Sstevel@tonic-gate volatile struct sb_hdr nhdr, *nhdrp;
3297c478bd9Sstevel@tonic-gate volatile char *pktp;
3307c478bd9Sstevel@tonic-gate volatile struct timeval last_timestamp;
3317c478bd9Sstevel@tonic-gate volatile int header_okay;
3327c478bd9Sstevel@tonic-gate extern int count, maxcount;
3337c478bd9Sstevel@tonic-gate extern int snoop_nrecover;
3347c478bd9Sstevel@tonic-gate #ifdef DEBUG
3357c478bd9Sstevel@tonic-gate extern int zflg;
3367c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate proc(0, 0, 0);
3397c478bd9Sstevel@tonic-gate bufstop = buf + len;
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate *
3437c478bd9Sstevel@tonic-gate * Loop through each packet in the buffer
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate last_timestamp.tv_sec = 0;
3467c478bd9Sstevel@tonic-gate (void) memcpy((char *)ojmp_env, (char *)jmp_env, sizeof (jmp_env));
3477c478bd9Sstevel@tonic-gate for (bp = buf; bp < bufstop; bp += nhdrp->sbh_totlen) {
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate * Gracefully exit if user terminates
3507c478bd9Sstevel@tonic-gate */
3517c478bd9Sstevel@tonic-gate if (quitting)
3527c478bd9Sstevel@tonic-gate break;
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate * Global error recocery: Prepare to continue when a corrupt
3557c478bd9Sstevel@tonic-gate * packet or header is encountered.
3567c478bd9Sstevel@tonic-gate */
3577c478bd9Sstevel@tonic-gate if (sigsetjmp(jmp_env, 1)) {
3587c478bd9Sstevel@tonic-gate goto err;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate header_okay = 0;
3627c478bd9Sstevel@tonic-gate hdrp = (struct sb_hdr *)bp;
3637c478bd9Sstevel@tonic-gate nhdrp = hdrp;
3647c478bd9Sstevel@tonic-gate pktp = (char *)hdrp + sizeof (*hdrp);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate * If reading a capture file
3687c478bd9Sstevel@tonic-gate * convert the headers from network
3697c478bd9Sstevel@tonic-gate * byte order (for little-endians like X86)
3707c478bd9Sstevel@tonic-gate */
3717c478bd9Sstevel@tonic-gate if (cap) {
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate * If the packets come from an old
3747c478bd9Sstevel@tonic-gate * capture file, convert the header.
3757c478bd9Sstevel@tonic-gate */
3767c478bd9Sstevel@tonic-gate if (old) {
37745916cd2Sjpk convert_old((struct ohdr *)hdrp);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate nhdrp = &nhdr;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate nhdrp->sbh_origlen = ntohl(hdrp->sbh_origlen);
3837c478bd9Sstevel@tonic-gate nhdrp->sbh_msglen = ntohl(hdrp->sbh_msglen);
3847c478bd9Sstevel@tonic-gate nhdrp->sbh_totlen = ntohl(hdrp->sbh_totlen);
3857c478bd9Sstevel@tonic-gate nhdrp->sbh_drops = ntohl(hdrp->sbh_drops);
3867c478bd9Sstevel@tonic-gate nhdrp->sbh_timestamp.tv_sec =
3877c478bd9Sstevel@tonic-gate ntohl(hdrp->sbh_timestamp.tv_sec);
3887c478bd9Sstevel@tonic-gate nhdrp->sbh_timestamp.tv_usec =
3897c478bd9Sstevel@tonic-gate ntohl(hdrp->sbh_timestamp.tv_usec);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /* Enhanced check for valid header */
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate if ((nhdrp->sbh_totlen == 0) ||
3957c478bd9Sstevel@tonic-gate (bp + nhdrp->sbh_totlen) < bp ||
3967c478bd9Sstevel@tonic-gate (bp + nhdrp->sbh_totlen) > bufstop ||
3977c478bd9Sstevel@tonic-gate (nhdrp->sbh_origlen == 0) ||
3987c478bd9Sstevel@tonic-gate (bp + nhdrp->sbh_origlen) < bp ||
3997c478bd9Sstevel@tonic-gate (nhdrp->sbh_msglen == 0) ||
4007c478bd9Sstevel@tonic-gate (bp + nhdrp->sbh_msglen) < bp ||
4017c478bd9Sstevel@tonic-gate (bp + nhdrp->sbh_msglen) > bufstop ||
4027c478bd9Sstevel@tonic-gate (nhdrp->sbh_msglen > nhdrp->sbh_origlen) ||
4037c478bd9Sstevel@tonic-gate (nhdrp->sbh_totlen < nhdrp->sbh_msglen) ||
4047c478bd9Sstevel@tonic-gate (nhdrp->sbh_timestamp.tv_sec == 0)) {
405b127ac41SPhilip Kirk if (cap) {
406c7e4935fSss150715 (void) fprintf(stderr, "(warning) bad packet "
407c7e4935fSss150715 "header in capture file");
408b127ac41SPhilip Kirk } else {
409c7e4935fSss150715 (void) fprintf(stderr, "(warning) bad packet "
410c7e4935fSss150715 "header in buffer");
411b127ac41SPhilip Kirk }
4127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " offset %d: length=%d\n",
4137c478bd9Sstevel@tonic-gate bp - buf, nhdrp->sbh_totlen);
4147c478bd9Sstevel@tonic-gate goto err;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate * Check for incomplete packet. We are conservative here,
4197c478bd9Sstevel@tonic-gate * since we don't know how good the checking is in other
4207c478bd9Sstevel@tonic-gate * parts of the code. We pass a partial packet, with
4217c478bd9Sstevel@tonic-gate * a warning.
4227c478bd9Sstevel@tonic-gate */
4237c478bd9Sstevel@tonic-gate if (pktp + nhdrp->sbh_msglen > bufstop) {
424c7e4935fSss150715 (void) fprintf(stderr, "truncated packet buffer\n");
4257c478bd9Sstevel@tonic-gate nhdrp->sbh_msglen = bufstop - pktp;
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate #ifdef DEBUG
4297c478bd9Sstevel@tonic-gate if (zflg)
4307c478bd9Sstevel@tonic-gate corrupt(pktp, pktp + nhdrp->sbh_msglen, buf, bufstop);
4317c478bd9Sstevel@tonic-gate #endif /* DEBUG */
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate header_okay = 1;
4347c478bd9Sstevel@tonic-gate if (!filter ||
4352b24ab6bSSebastien Roy want_packet((uchar_t *)pktp,
4367c478bd9Sstevel@tonic-gate nhdrp->sbh_msglen,
4377c478bd9Sstevel@tonic-gate nhdrp->sbh_origlen)) {
4387c478bd9Sstevel@tonic-gate count++;
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate * Start deadman timer for interpreter processing
4427c478bd9Sstevel@tonic-gate */
4437c478bd9Sstevel@tonic-gate (void) snoop_alarm(SNOOP_ALARM_GRAN*SNOOP_MAXRECOVER,
4447c478bd9Sstevel@tonic-gate NULL);
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate encap_levels = 0;
4477c478bd9Sstevel@tonic-gate if (!cap || count >= first)
4487c478bd9Sstevel@tonic-gate proc(nhdrp, pktp, count, flags);
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate if (cap && count >= last) {
4517c478bd9Sstevel@tonic-gate (void) snoop_alarm(0, NULL);
4527c478bd9Sstevel@tonic-gate break;
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate if (maxcount && count >= maxcount) {
456c7e4935fSss150715 (void) fprintf(stderr, "%d packets captured\n",
457c7e4935fSss150715 count);
4587c478bd9Sstevel@tonic-gate exit(0);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate snoop_nrecover = 0; /* success */
4627c478bd9Sstevel@tonic-gate (void) snoop_alarm(0, NULL);
4637c478bd9Sstevel@tonic-gate last_timestamp = hdrp->sbh_timestamp; /* save stamp */
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate continue;
4667c478bd9Sstevel@tonic-gate err:
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate * Corruption has been detected. Reset errors.
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate snoop_recover();
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate * packet header was apparently okay. Continue.
4747c478bd9Sstevel@tonic-gate */
4757c478bd9Sstevel@tonic-gate if (header_okay)
4767c478bd9Sstevel@tonic-gate continue;
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * Otherwise try to scan forward to the next packet, using
4807c478bd9Sstevel@tonic-gate * the last known timestamp if it is available.
4817c478bd9Sstevel@tonic-gate */
4827c478bd9Sstevel@tonic-gate nhdrp = &nhdr;
4837c478bd9Sstevel@tonic-gate nhdrp->sbh_totlen = 0;
4847c478bd9Sstevel@tonic-gate if (last_timestamp.tv_sec == 0) {
4857c478bd9Sstevel@tonic-gate bp += sizeof (int);
4867c478bd9Sstevel@tonic-gate } else {
4877c478bd9Sstevel@tonic-gate for (bp += sizeof (int); bp <= bufstop;
4887c478bd9Sstevel@tonic-gate bp += sizeof (int)) {
4897c478bd9Sstevel@tonic-gate hdrp = (struct sb_hdr *)bp;
4907c478bd9Sstevel@tonic-gate /* An approximate timestamp located */
4917c478bd9Sstevel@tonic-gate if ((hdrp->sbh_timestamp.tv_sec >> 8) ==
4927c478bd9Sstevel@tonic-gate (last_timestamp.tv_sec >> 8))
4937c478bd9Sstevel@tonic-gate break;
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate /* reset jmp_env for program exit */
4987c478bd9Sstevel@tonic-gate (void) memcpy((char *)jmp_env, (char *)ojmp_env, sizeof (jmp_env));
4997c478bd9Sstevel@tonic-gate proc(0, -1, 0);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate * Called if nwrite() encounters write problems.
5047c478bd9Sstevel@tonic-gate */
5057c478bd9Sstevel@tonic-gate static void
cap_write_error(const char * msgtype)5067c478bd9Sstevel@tonic-gate cap_write_error(const char *msgtype)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5097c478bd9Sstevel@tonic-gate "snoop: cannot write %s to capture file: %s\n",
5107c478bd9Sstevel@tonic-gate msgtype, strerror(errno));
5117c478bd9Sstevel@tonic-gate exit(1);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate * Writes target buffer to the open file descriptor. Upon detection of a short
5167c478bd9Sstevel@tonic-gate * write, an attempt to process the remaining bytes occurs until all anticipated
5177c478bd9Sstevel@tonic-gate * bytes are written. An error status is returned to indicate any serious write
5187c478bd9Sstevel@tonic-gate * failures.
5197c478bd9Sstevel@tonic-gate */
5207c478bd9Sstevel@tonic-gate static int
nwrite(int fd,const void * buffer,size_t buflen)5217c478bd9Sstevel@tonic-gate nwrite(int fd, const void *buffer, size_t buflen)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate size_t nwritten;
5247c478bd9Sstevel@tonic-gate ssize_t nbytes = 0;
5257c478bd9Sstevel@tonic-gate const char *buf = buffer;
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate for (nwritten = 0; nwritten < buflen; nwritten += nbytes) {
5287c478bd9Sstevel@tonic-gate nbytes = write(fd, &buf[nwritten], buflen - nwritten);
5297c478bd9Sstevel@tonic-gate if (nbytes == -1)
5307c478bd9Sstevel@tonic-gate return (-1);
5317c478bd9Sstevel@tonic-gate if (nbytes == 0) {
5327c478bd9Sstevel@tonic-gate errno = EIO;
5337c478bd9Sstevel@tonic-gate return (-1);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate return (0);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate * Routines for opening, closing, reading and writing
5417c478bd9Sstevel@tonic-gate * a capture file of packets saved with the -o option.
5427c478bd9Sstevel@tonic-gate */
5437c478bd9Sstevel@tonic-gate static int capfile_out;
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * The snoop capture file has a header to identify
5477c478bd9Sstevel@tonic-gate * it as a capture file and record its version.
5487c478bd9Sstevel@tonic-gate * A file without this header is assumed to be an
5497c478bd9Sstevel@tonic-gate * old format snoop file.
5507c478bd9Sstevel@tonic-gate *
5517c478bd9Sstevel@tonic-gate * A version 1 header looks like this:
5527c478bd9Sstevel@tonic-gate *
5537c478bd9Sstevel@tonic-gate * 0 1 2 3 4 5 6 7 8 9 10 11
5547c478bd9Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+
5557c478bd9Sstevel@tonic-gate * | s | n | o | o | p | \0| \0| \0| version | data
5567c478bd9Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+
5577c478bd9Sstevel@tonic-gate * | word 0 | word 1 | word 2 |
5587c478bd9Sstevel@tonic-gate *
5597c478bd9Sstevel@tonic-gate *
5607c478bd9Sstevel@tonic-gate * A version 2 header adds a word that identifies the MAC type.
5617c478bd9Sstevel@tonic-gate * This allows for capture files from FDDI etc.
5627c478bd9Sstevel@tonic-gate *
5637c478bd9Sstevel@tonic-gate * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
5647c478bd9Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
5657c478bd9Sstevel@tonic-gate * | s | n | o | o | p | \0| \0| \0| version | MAC type | data
5667c478bd9Sstevel@tonic-gate * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
5677c478bd9Sstevel@tonic-gate * | word 0 | word 1 | word 2 | word 3
5687c478bd9Sstevel@tonic-gate *
5697c478bd9Sstevel@tonic-gate */
57045916cd2Sjpk static const char *snoop_id = "snoop\0\0\0";
57145916cd2Sjpk static const int snoop_idlen = 8;
57245916cd2Sjpk static const int snoop_version = 2;
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate void
cap_open_write(const char * name)575c7e4935fSss150715 cap_open_write(const char *name)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate int vers;
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate capfile_out = open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
5807c478bd9Sstevel@tonic-gate if (capfile_out < 0)
5817c478bd9Sstevel@tonic-gate pr_err("%s: %m", name);
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate vers = htonl(snoop_version);
58445916cd2Sjpk if (nwrite(capfile_out, snoop_id, snoop_idlen) == -1)
5857c478bd9Sstevel@tonic-gate cap_write_error("snoop_id");
5867c478bd9Sstevel@tonic-gate
58745916cd2Sjpk if (nwrite(capfile_out, &vers, sizeof (int)) == -1)
5887c478bd9Sstevel@tonic-gate cap_write_error("version");
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate void
cap_close(void)59345916cd2Sjpk cap_close(void)
5947c478bd9Sstevel@tonic-gate {
59545916cd2Sjpk (void) close(capfile_out);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate static char *cap_buffp = NULL;
5997c478bd9Sstevel@tonic-gate static int cap_len = 0;
6007c478bd9Sstevel@tonic-gate static int cap_new;
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate void
cap_open_read(const char * name)603c7e4935fSss150715 cap_open_read(const char *name)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate struct stat st;
6067c478bd9Sstevel@tonic-gate int cap_vers;
607*7d897698SMilan Jurik int *word;
608*7d897698SMilan Jurik int device_mac_type = -1;
6097c478bd9Sstevel@tonic-gate int capfile_in;
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate capfile_in = open(name, O_RDONLY);
6127c478bd9Sstevel@tonic-gate if (capfile_in < 0)
6137c478bd9Sstevel@tonic-gate pr_err("couldn't open %s: %m", name);
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate if (fstat(capfile_in, &st) < 0)
6167c478bd9Sstevel@tonic-gate pr_err("couldn't stat %s: %m", name);
6177c478bd9Sstevel@tonic-gate cap_len = st.st_size;
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate cap_buffp = mmap(0, cap_len, PROT_READ, MAP_PRIVATE, capfile_in, 0);
62045916cd2Sjpk (void) close(capfile_in);
6217c478bd9Sstevel@tonic-gate if ((int)cap_buffp == -1)
6227c478bd9Sstevel@tonic-gate pr_err("couldn't mmap %s: %m", name);
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate /* Check if new snoop capture file format */
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate cap_new = bcmp(cap_buffp, snoop_id, snoop_idlen) == 0;
6277c478bd9Sstevel@tonic-gate
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate * If new file - check version and
6307c478bd9Sstevel@tonic-gate * set buffer pointer to point at first packet
6317c478bd9Sstevel@tonic-gate */
6327c478bd9Sstevel@tonic-gate if (cap_new) {
6337c478bd9Sstevel@tonic-gate cap_vers = ntohl(*(int *)(cap_buffp + snoop_idlen));
6347c478bd9Sstevel@tonic-gate cap_buffp += snoop_idlen + sizeof (int);
6357c478bd9Sstevel@tonic-gate cap_len -= snoop_idlen + sizeof (int);
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate switch (cap_vers) {
6387c478bd9Sstevel@tonic-gate case 1:
6397c478bd9Sstevel@tonic-gate device_mac_type = DL_ETHER;
6407c478bd9Sstevel@tonic-gate break;
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate case 2:
6437c478bd9Sstevel@tonic-gate device_mac_type = ntohl(*((int *)cap_buffp));
6447c478bd9Sstevel@tonic-gate cap_buffp += sizeof (int);
6457c478bd9Sstevel@tonic-gate cap_len -= sizeof (int);
6467c478bd9Sstevel@tonic-gate break;
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate default:
6497c478bd9Sstevel@tonic-gate pr_err("capture file: %s: Version %d unrecognized\n",
6507c478bd9Sstevel@tonic-gate name, cap_vers);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate for (interface = &INTERFACES[0]; interface->mac_type != -1;
6547c478bd9Sstevel@tonic-gate interface++)
6557c478bd9Sstevel@tonic-gate if (interface->mac_type == device_mac_type)
6567c478bd9Sstevel@tonic-gate break;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate if (interface->mac_type == -1)
6597c478bd9Sstevel@tonic-gate pr_err("Mac Type = %x is not supported\n",
6607c478bd9Sstevel@tonic-gate device_mac_type);
6617c478bd9Sstevel@tonic-gate } else {
6627c478bd9Sstevel@tonic-gate /* Use heuristic to check if it's an old-style file */
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate device_mac_type = DL_ETHER;
6657c478bd9Sstevel@tonic-gate word = (int *)cap_buffp;
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate if (!((word[0] < 1600 && word[1] < 1600) &&
6687c478bd9Sstevel@tonic-gate (word[0] < word[1]) &&
6697c478bd9Sstevel@tonic-gate (word[2] > 610000000 && word[2] < 770000000)))
6707c478bd9Sstevel@tonic-gate pr_err("not a capture file: %s", name);
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate /* Change protection so's we can fix the headers */
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate if (mprotect(cap_buffp, cap_len, PROT_READ | PROT_WRITE) < 0)
6757c478bd9Sstevel@tonic-gate pr_err("mprotect: %s: %m", name);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate void
cap_read(int first,int last,int filter,void (* proc)(),int flags)680c7e4935fSss150715 cap_read(int first, int last, int filter, void (*proc)(), int flags)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate extern int count;
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate count = 0;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate scan(cap_buffp, cap_len, filter, 1, !cap_new, proc, first, last, flags);
6877c478bd9Sstevel@tonic-gate
68845916cd2Sjpk (void) munmap(cap_buffp, cap_len);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate
69145916cd2Sjpk /* ARGSUSED */
6927c478bd9Sstevel@tonic-gate void
cap_write(struct sb_hdr * hdrp,char * pktp,int num,int flags)693c7e4935fSss150715 cap_write(struct sb_hdr *hdrp, char *pktp, int num, int flags)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate int pktlen, mac;
6967c478bd9Sstevel@tonic-gate static int first = 1;
6977c478bd9Sstevel@tonic-gate struct sb_hdr nhdr;
6987c478bd9Sstevel@tonic-gate extern boolean_t qflg;
6997c478bd9Sstevel@tonic-gate
7007c478bd9Sstevel@tonic-gate if (hdrp == NULL)
7017c478bd9Sstevel@tonic-gate return;
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate if (first) {
7047c478bd9Sstevel@tonic-gate first = 0;
7057c478bd9Sstevel@tonic-gate mac = htonl(interface->mac_type);
70645916cd2Sjpk if (nwrite(capfile_out, &mac, sizeof (int)) == -1)
7077c478bd9Sstevel@tonic-gate cap_write_error("mac_type");
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate pktlen = hdrp->sbh_totlen - sizeof (*hdrp);
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate * Convert sb_hdr to network byte order
7147c478bd9Sstevel@tonic-gate */
7157c478bd9Sstevel@tonic-gate nhdr.sbh_origlen = htonl(hdrp->sbh_origlen);
7167c478bd9Sstevel@tonic-gate nhdr.sbh_msglen = htonl(hdrp->sbh_msglen);
7177c478bd9Sstevel@tonic-gate nhdr.sbh_totlen = htonl(hdrp->sbh_totlen);
7187c478bd9Sstevel@tonic-gate nhdr.sbh_drops = htonl(hdrp->sbh_drops);
7197c478bd9Sstevel@tonic-gate nhdr.sbh_timestamp.tv_sec = htonl(hdrp->sbh_timestamp.tv_sec);
7207c478bd9Sstevel@tonic-gate nhdr.sbh_timestamp.tv_usec = htonl(hdrp->sbh_timestamp.tv_usec);
7217c478bd9Sstevel@tonic-gate
72245916cd2Sjpk if (nwrite(capfile_out, &nhdr, sizeof (nhdr)) == -1)
7237c478bd9Sstevel@tonic-gate cap_write_error("packet header");
7247c478bd9Sstevel@tonic-gate
72545916cd2Sjpk if (nwrite(capfile_out, pktp, pktlen) == -1)
7267c478bd9Sstevel@tonic-gate cap_write_error("packet");
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate if (! qflg)
7297c478bd9Sstevel@tonic-gate show_count();
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate /*
7337c478bd9Sstevel@tonic-gate * Convert a packet header from
7347c478bd9Sstevel@tonic-gate * old to new format.
7357c478bd9Sstevel@tonic-gate */
73645916cd2Sjpk static void
convert_old(struct ohdr * ohdrp)73745916cd2Sjpk convert_old(struct ohdr *ohdrp)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate struct sb_hdr nhdr;
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate nhdr.sbh_origlen = ohdrp->o_len;
7427c478bd9Sstevel@tonic-gate nhdr.sbh_msglen = ohdrp->o_msglen;
7437c478bd9Sstevel@tonic-gate nhdr.sbh_totlen = ohdrp->o_totlen;
7447c478bd9Sstevel@tonic-gate nhdr.sbh_drops = ohdrp->o_drops;
7457c478bd9Sstevel@tonic-gate nhdr.sbh_timestamp = ohdrp->o_time;
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate *(struct sb_hdr *)ohdrp = nhdr;
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate
7502e3b6467Skcpoon static int
strioctl(int fd,int cmd,int timout,int len,void * dp)75145916cd2Sjpk strioctl(int fd, int cmd, int timout, int len, void *dp)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate struct strioctl sioc;
7547c478bd9Sstevel@tonic-gate int rc;
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate sioc.ic_cmd = cmd;
7577c478bd9Sstevel@tonic-gate sioc.ic_timout = timout;
7587c478bd9Sstevel@tonic-gate sioc.ic_len = len;
7597c478bd9Sstevel@tonic-gate sioc.ic_dp = dp;
7607c478bd9Sstevel@tonic-gate rc = ioctl(fd, I_STR, &sioc);
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate if (rc < 0)
7637c478bd9Sstevel@tonic-gate return (rc);
7647c478bd9Sstevel@tonic-gate else
7657c478bd9Sstevel@tonic-gate return (sioc.ic_len);
7667c478bd9Sstevel@tonic-gate }
767