xref: /titanic_50/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_capture.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS */
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <setjmp.h>
33 #include <sys/types.h>
34 #include <sys/signal.h>
35 #include <sys/time.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <net/if.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/in.h>
41 #include <netinet/ip.h>
42 #include <sys/pfmod.h>
43 #include <netinet/if_ether.h>
44 #include <sys/mman.h>
45 #include <sys/stat.h>
46 #include <sys/stropts.h>
47 #include <sys/bufmod.h>
48 
49 #include <unistd.h>
50 #include <stropts.h>
51 #include <stdlib.h>
52 #include <ctype.h>
53 #include <values.h>
54 #include <libdlpi.h>
55 
56 #include "snoop.h"
57 
58 /*
59  * Old header format.
60  * Actually two concatenated structs:  nit_bufhdr + nit_head
61  */
62 struct ohdr {
63 	/* nit_bufhdr */
64 	int	o_msglen;
65 	int	o_totlen;
66 	/* nit_head */
67 	struct timeval o_time;
68 	int	o_drops;
69 	int	o_len;
70 };
71 
72 static void scan(char *, int, int, int, int, void (*)(), int, int, int);
73 void convert_to_network();
74 void convert_from_network();
75 static void convert_old(struct ohdr *);
76 extern sigjmp_buf jmp_env, ojmp_env;
77 static dlpi_info_t dlinfo;
78 static char *bufp;	/* pointer to read buffer */
79 
80 static int strioctl(int, int, int, int, void *);
81 
82 /*
83  * Open up the device in raw mode and passive mode
84  * (see dlpi_open(3DLPI)) and start finding out something about it,
85  * especially stuff about the data link headers. We need that information
86  * to build the proper packet filters.
87  */
88 boolean_t
89 check_device(dlpi_handle_t *dhp, char **devicep)
90 {
91 	int	retval;
92 
93 	/*
94 	 * Determine which network device
95 	 * to use if none given.
96 	 * Should get back a value like "le0".
97 	 */
98 	if (*devicep == NULL) {
99 		char *cbuf;
100 		static struct ifconf ifc;
101 		static struct ifreq *ifr;
102 		int s;
103 		int n;
104 		int numifs;
105 		unsigned bufsize;
106 
107 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
108 		    pr_err("socket");
109 
110 		if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
111 			pr_err("check_device: ioctl SIOCGIFNUM");
112 			(void) close(s);
113 			s = -1;
114 			return (B_FALSE);
115 		}
116 
117 		bufsize = numifs * sizeof (struct ifreq);
118 		cbuf = (char *)malloc(bufsize);
119 		if (cbuf == NULL) {
120 			pr_err("out of memory\n");
121 			(void) close(s);
122 			s = -1;
123 			return (B_FALSE);
124 		}
125 		ifc.ifc_len = bufsize;
126 		ifc.ifc_buf = cbuf;
127 		if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
128 			pr_err("check_device: ioctl SIOCGIFCONF");
129 			(void) close(s);
130 			s = -1;
131 			(void) free(cbuf);
132 			return (B_FALSE);
133 		}
134 		n = ifc.ifc_len / sizeof (struct ifreq);
135 		ifr = ifc.ifc_req;
136 		for (; n > 0; n--, ifr++) {
137 			if (strchr(ifr->ifr_name, ':') != NULL)
138 				continue;
139 			if (ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0)
140 				pr_err("ioctl SIOCGIFFLAGS");
141 			if ((ifr->ifr_flags &
142 				(IFF_VIRTUAL|IFF_LOOPBACK|IFF_UP|
143 				IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))
144 				break;
145 		}
146 
147 		if (n == 0)
148 			pr_err("No network interface devices found");
149 
150 		*devicep = ifr->ifr_name;
151 		(void) close(s);
152 	}
153 
154 	retval = dlpi_open(*devicep, dhp, DLPI_PASSIVE|DLPI_RAW);
155 	if (retval != DLPI_SUCCESS) {
156 		pr_err("cannot open \"%s\": %s", *devicep,
157 		    dlpi_strerror(retval));
158 	}
159 
160 	if ((retval = dlpi_info(*dhp, &dlinfo, 0)) != DLPI_SUCCESS)
161 		pr_errdlpi(*dhp, "dlpi_info failed", retval);
162 
163 	for (interface = &INTERFACES[0]; interface->mac_type != -1; interface++)
164 		if (interface->mac_type == dlinfo.di_mactype)
165 			break;
166 
167 	/* allow limited functionality even if interface isn't known */
168 	if (interface->mac_type == -1) {
169 		(void) fprintf(stderr, "snoop: WARNING: Mac Type = %x "
170 		    "not supported\n", dlinfo.di_mactype);
171 	}
172 
173 	/* for backward compatibility, allow known interface mtu_sizes */
174 	if (interface->mtu_size > dlinfo.di_max_sdu)
175 		dlinfo.di_max_sdu = interface->mtu_size;
176 
177 	return (interface->try_kernel_filter);
178 }
179 
180 /*
181  * Do whatever is necessary to initialize the interface
182  * for packet capture. Bind the device opened in check_device(), set
183  * promiscuous mode, push the streams buffer module and packet filter
184  * module, set various buffer parameters.
185  */
186 void
187 initdevice(dlpi_handle_t dh, ulong_t snaplen, ulong_t chunksize,
188     struct timeval *timeout, struct Pf_ext_packetfilt *fp)
189 {
190 	extern int Pflg;
191 	int 	retv;
192 	int 	netfd;
193 
194 	retv = dlpi_bind(dh, DLPI_ANY_SAP, NULL);
195 	if (retv != DLPI_SUCCESS)
196 		pr_errdlpi(dh, "cannot bind on", retv);
197 
198 	(void) fprintf(stderr, "Using device %s ", dlpi_linkname(dh));
199 
200 	/*
201 	 * If Pflg not set - use physical level
202 	 * promiscuous mode.  Otherwise - just SAP level.
203 	 */
204 	if (!Pflg) {
205 		(void) fprintf(stderr, "(promiscuous mode)\n");
206 		retv = dlpi_promiscon(dh, DL_PROMISC_PHYS);
207 		if (retv != DLPI_SUCCESS) {
208 			pr_errdlpi(dh, "promiscuous mode(physical) failed",
209 			    retv);
210 		}
211 	} else {
212 		(void) fprintf(stderr, "(non promiscuous)\n");
213 		retv = dlpi_promiscon(dh, DL_PROMISC_MULTI);
214 		if (retv != DLPI_SUCCESS) {
215 			pr_errdlpi(dh, "promiscuous mode(multicast) failed",
216 			    retv);
217 		}
218 	}
219 
220 	retv = dlpi_promiscon(dh, DL_PROMISC_SAP);
221 	if (retv != DLPI_SUCCESS)
222 		pr_errdlpi(dh, "promiscuous mode(SAP) failed", retv);
223 
224 	netfd = dlpi_fd(dh);
225 
226 	if (fp) {
227 		/*
228 		 * push and configure the packet filtering module
229 		 */
230 		if (ioctl(netfd, I_PUSH, "pfmod") < 0)
231 			pr_errdlpi(dh, "cannot push \"pfmod\"", DL_SYSERR);
232 
233 		if (strioctl(netfd, PFIOCSETF, -1, sizeof (*fp),
234 		    (char *)fp) < 0)
235 			pr_errdlpi(dh, "PFIOCSETF", DL_SYSERR);
236 	}
237 
238 	if (ioctl(netfd, I_PUSH, "bufmod") < 0)
239 		pr_errdlpi(dh, "cannot push \"bufmod\"", DL_SYSERR);
240 
241 	if (strioctl(netfd, SBIOCSTIME, -1, sizeof (struct timeval),
242 	    (char *)timeout) < 0)
243 		pr_errdlpi(dh, "SBIOCSTIME", DL_SYSERR);
244 
245 	if (strioctl(netfd, SBIOCSCHUNK, -1, sizeof (uint_t),
246 	    (char *)&chunksize) < 0)
247 		pr_errdlpi(dh, "SBIOCGCHUNK", DL_SYSERR);
248 
249 	if (strioctl(netfd, SBIOCSSNAP, -1, sizeof (uint_t),
250 	    (char *)&snaplen) < 0)
251 		pr_errdlpi(dh, "SBIOCSSNAP", DL_SYSERR);
252 
253 	/*
254 	 * Flush the read queue, to get rid of anything that
255 	 * accumulated before the device reached its final configuration.
256 	 */
257 	if (ioctl(netfd, I_FLUSH, FLUSHR) < 0)
258 		pr_errdlpi(dh, "cannot flush \"I_FLUSH\"", DL_SYSERR);
259 }
260 
261 /*
262  * Read packets from the network.  Initdevice is called in
263  * here to set up the network interface for reading of
264  * raw ethernet packets in promiscuous mode into a buffer.
265  * Packets are read and either written directly to a file
266  * or interpreted for display on the fly.
267  */
268 void
269 net_read(dlpi_handle_t dh, size_t chunksize, int filter, void (*proc)(),
270     int flags)
271 {
272 	int 	retval;
273 	extern int count;
274 	size_t	msglen;
275 
276 	count = 0;
277 
278 	/* allocate a read buffer */
279 	bufp = malloc(chunksize);
280 	if (bufp == NULL)
281 		pr_err("no memory for %d buffer", chunksize);
282 
283 	/*
284 	 * read frames
285 	 */
286 	for (;;) {
287 		msglen = chunksize;
288 		retval = dlpi_recv(dh, NULL, NULL, bufp, &msglen, -1, NULL);
289 
290 		if (retval != DLPI_SUCCESS || quitting)
291 			break;
292 
293 		if (msglen != 0)
294 			scan(bufp, msglen, filter, 0, 0, proc, 0, 0, flags);
295 	}
296 
297 	free(bufp);
298 
299 	if (!quitting)
300 		pr_errdlpi(dh, "network read failed", retval);
301 }
302 
303 #ifdef DEBUG
304 /*
305  * corrupt: simulate packet corruption for debugging interpreters
306  */
307 void
308 corrupt(volatile char *pktp, volatile char *pstop, char *buf,
309 	volatile char *bufstop)
310 {
311 	int c;
312 	int i;
313 	int p;
314 	int li = rand() % (pstop - pktp - 1) + 1;
315 	volatile char *pp = pktp;
316 	volatile char *pe = bufstop < pstop ? bufstop : pstop;
317 
318 	if (pktp < buf || pktp > bufstop)
319 		return;
320 
321 	for (pp = pktp; pp < pe; pp += li) {
322 		c = ((pe - pp) < li ? pe - pp : li);
323 		i = (rand() % c)>>1;
324 		while (--i > 0) {
325 			p = (rand() % c);
326 			pp[p] = (unsigned char)(rand() & 0xFF);
327 		}
328 	}
329 }
330 #endif /* DEBUG */
331 
332 static void
333 scan(char *buf, int len, int filter, int cap, int old, void (*proc)(),
334     int first, int last, int flags)
335 {
336 	volatile char *bp, *bufstop;
337 	volatile struct sb_hdr *hdrp;
338 	volatile struct sb_hdr nhdr, *nhdrp;
339 	volatile char *pktp;
340 	volatile struct timeval last_timestamp;
341 	volatile int header_okay;
342 	extern int count, maxcount;
343 	extern int snoop_nrecover;
344 #ifdef	DEBUG
345 	extern int zflg;
346 #endif	/* DEBUG */
347 
348 	proc(0, 0, 0);
349 	bufstop = buf + len;
350 
351 	/*
352 	 *
353 	 * Loop through each packet in the buffer
354 	 */
355 	last_timestamp.tv_sec = 0;
356 	(void) memcpy((char *)ojmp_env, (char *)jmp_env, sizeof (jmp_env));
357 	for (bp = buf; bp < bufstop; bp += nhdrp->sbh_totlen) {
358 		/*
359 		 * Gracefully exit if user terminates
360 		 */
361 		if (quitting)
362 			break;
363 		/*
364 		 * Global error recocery: Prepare to continue when a corrupt
365 		 * packet or header is encountered.
366 		 */
367 		if (sigsetjmp(jmp_env, 1)) {
368 			goto err;
369 		}
370 
371 		header_okay = 0;
372 		hdrp = (struct sb_hdr *)bp;
373 		nhdrp = hdrp;
374 		pktp = (char *)hdrp + sizeof (*hdrp);
375 
376 		/*
377 		 * If reading a capture file
378 		 * convert the headers from network
379 		 * byte order (for little-endians like X86)
380 		 */
381 		if (cap) {
382 			/*
383 			 * If the packets come from an old
384 			 * capture file, convert the header.
385 			 */
386 			if (old) {
387 				convert_old((struct ohdr *)hdrp);
388 			}
389 
390 			nhdrp = &nhdr;
391 
392 			nhdrp->sbh_origlen = ntohl(hdrp->sbh_origlen);
393 			nhdrp->sbh_msglen = ntohl(hdrp->sbh_msglen);
394 			nhdrp->sbh_totlen = ntohl(hdrp->sbh_totlen);
395 			nhdrp->sbh_drops = ntohl(hdrp->sbh_drops);
396 			nhdrp->sbh_timestamp.tv_sec =
397 				ntohl(hdrp->sbh_timestamp.tv_sec);
398 			nhdrp->sbh_timestamp.tv_usec =
399 				ntohl(hdrp->sbh_timestamp.tv_usec);
400 		}
401 
402 		/* Enhanced check for valid header */
403 
404 		if ((nhdrp->sbh_totlen == 0) ||
405 		    (bp + nhdrp->sbh_totlen) < bp ||
406 		    (bp + nhdrp->sbh_totlen) > bufstop ||
407 		    (nhdrp->sbh_origlen == 0) ||
408 		    (bp + nhdrp->sbh_origlen) < bp ||
409 		    (nhdrp->sbh_msglen == 0) ||
410 		    (bp + nhdrp->sbh_msglen) < bp ||
411 		    (bp + nhdrp->sbh_msglen) > bufstop ||
412 		    (nhdrp->sbh_msglen > nhdrp->sbh_origlen) ||
413 		    (nhdrp->sbh_totlen < nhdrp->sbh_msglen) ||
414 		    (nhdrp->sbh_timestamp.tv_sec == 0)) {
415 			if (cap)
416 				(void) fprintf(stderr, "(warning) bad packet "
417 				    "header in capture file");
418 			else
419 				(void) fprintf(stderr, "(warning) bad packet "
420 				    "header in buffer");
421 			(void) fprintf(stderr, " offset %d: length=%d\n",
422 				bp - buf, nhdrp->sbh_totlen);
423 			goto err;
424 		}
425 
426 		if (nhdrp->sbh_totlen >
427 		    (dlinfo.di_max_sdu + MAX_HDRTRAILER)) {
428 			if (cap)
429 				(void) fprintf(stderr, "(warning) packet length"
430 				    " greater than MTU in capture file");
431 			else
432 				(void) fprintf(stderr, "(warning) packet length"
433 				    " greater than MTU in buffer");
434 
435 			(void) fprintf(stderr, " offset %d: length=%d\n",
436 				bp - buf, nhdrp->sbh_totlen);
437 		}
438 
439 		/*
440 		 * Check for incomplete packet.  We are conservative here,
441 		 * since we don't know how good the checking is in other
442 		 * parts of the code.  We pass a partial packet, with
443 		 * a warning.
444 		 */
445 		if (pktp + nhdrp->sbh_msglen > bufstop) {
446 			(void) fprintf(stderr, "truncated packet buffer\n");
447 			nhdrp->sbh_msglen = bufstop - pktp;
448 		}
449 
450 #ifdef DEBUG
451 		if (zflg)
452 			corrupt(pktp, pktp + nhdrp->sbh_msglen, buf, bufstop);
453 #endif /* DEBUG */
454 
455 		header_okay = 1;
456 		if (!filter ||
457 			want_packet(pktp,
458 				nhdrp->sbh_msglen,
459 				nhdrp->sbh_origlen)) {
460 			count++;
461 
462 			/*
463 			 * Start deadman timer for interpreter processing
464 			 */
465 			(void) snoop_alarm(SNOOP_ALARM_GRAN*SNOOP_MAXRECOVER,
466 				NULL);
467 
468 			encap_levels = 0;
469 			if (!cap || count >= first)
470 				proc(nhdrp, pktp, count, flags);
471 
472 			if (cap && count >= last) {
473 				(void) snoop_alarm(0, NULL);
474 				break;
475 			}
476 
477 			if (maxcount && count >= maxcount) {
478 				(void) fprintf(stderr, "%d packets captured\n",
479 				    count);
480 				exit(0);
481 			}
482 
483 			snoop_nrecover = 0;			/* success */
484 			(void) snoop_alarm(0, NULL);
485 			last_timestamp = hdrp->sbh_timestamp;	/* save stamp */
486 		}
487 		continue;
488 err:
489 		/*
490 		 * Corruption has been detected. Reset errors.
491 		 */
492 		snoop_recover();
493 
494 		/*
495 		 * packet header was apparently okay. Continue.
496 		 */
497 		if (header_okay)
498 			continue;
499 
500 		/*
501 		 * Otherwise try to scan forward to the next packet, using
502 		 * the last known timestamp if it is available.
503 		 */
504 		nhdrp = &nhdr;
505 		nhdrp->sbh_totlen = 0;
506 		if (last_timestamp.tv_sec == 0) {
507 			bp += sizeof (int);
508 		} else {
509 			for (bp += sizeof (int); bp <= bufstop;
510 				bp += sizeof (int)) {
511 				hdrp = (struct sb_hdr *)bp;
512 				/* An approximate timestamp located */
513 				if ((hdrp->sbh_timestamp.tv_sec >> 8) ==
514 				    (last_timestamp.tv_sec >> 8))
515 					break;
516 			}
517 		}
518 	}
519 	/* reset jmp_env for program exit */
520 	(void) memcpy((char *)jmp_env, (char *)ojmp_env, sizeof (jmp_env));
521 	proc(0, -1, 0);
522 }
523 
524 /*
525  * Called if nwrite() encounters write problems.
526  */
527 static void
528 cap_write_error(const char *msgtype)
529 {
530 	(void) fprintf(stderr,
531 		    "snoop: cannot write %s to capture file: %s\n",
532 		    msgtype, strerror(errno));
533 	exit(1);
534 }
535 
536 /*
537  * Writes target buffer to the open file descriptor. Upon detection of a short
538  * write, an attempt to process the remaining bytes occurs until all anticipated
539  * bytes are written. An error status is returned to indicate any serious write
540  * failures.
541  */
542 static int
543 nwrite(int fd, const void *buffer, size_t buflen)
544 {
545 	size_t nwritten;
546 	ssize_t nbytes = 0;
547 	const char *buf = buffer;
548 
549 	for (nwritten = 0; nwritten < buflen; nwritten += nbytes) {
550 		nbytes = write(fd, &buf[nwritten], buflen - nwritten);
551 		if (nbytes == -1)
552 			return (-1);
553 		if (nbytes == 0) {
554 			errno = EIO;
555 			return (-1);
556 		}
557 	}
558 	return (0);
559 }
560 
561 /*
562  * Routines for opening, closing, reading and writing
563  * a capture file of packets saved with the -o option.
564  */
565 static int capfile_out;
566 
567 /*
568  * The snoop capture file has a header to identify
569  * it as a capture file and record its version.
570  * A file without this header is assumed to be an
571  * old format snoop file.
572  *
573  * A version 1 header looks like this:
574  *
575  *   0   1   2   3   4   5   6   7   8   9  10  11
576  * +---+---+---+---+---+---+---+---+---+---+---+---+---+
577  * | s | n | o | o | p | \0| \0| \0|    version    |  data
578  * +---+---+---+---+---+---+---+---+---+---+---+---+---+
579  * |	 word 0	   |	 word 1	   |	 word 2	   |
580  *
581  *
582  * A version 2 header adds a word that identifies the MAC type.
583  * This allows for capture files from FDDI etc.
584  *
585  *   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
586  * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
587  * | s | n | o | o | p | \0| \0| \0|    version    |    MAC type   | data
588  * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
589  * |	 word 0	   |	 word 1	   |	 word 2	   |	 word 3
590  *
591  */
592 static const char *snoop_id = "snoop\0\0\0";
593 static const int snoop_idlen = 8;
594 static const int snoop_version = 2;
595 
596 void
597 cap_open_write(const char *name)
598 {
599 	int vers;
600 
601 	capfile_out = open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
602 	if (capfile_out < 0)
603 		pr_err("%s: %m", name);
604 
605 	vers = htonl(snoop_version);
606 	if (nwrite(capfile_out, snoop_id, snoop_idlen) == -1)
607 		cap_write_error("snoop_id");
608 
609 	if (nwrite(capfile_out, &vers, sizeof (int)) == -1)
610 		cap_write_error("version");
611 }
612 
613 
614 void
615 cap_close(void)
616 {
617 	(void) close(capfile_out);
618 }
619 
620 static char *cap_buffp = NULL;
621 static int cap_len = 0;
622 static int cap_new;
623 
624 void
625 cap_open_read(const char *name)
626 {
627 	struct stat st;
628 	int cap_vers;
629 	int *word, device_mac_type;
630 	int capfile_in;
631 
632 	capfile_in = open(name, O_RDONLY);
633 	if (capfile_in < 0)
634 		pr_err("couldn't open %s: %m", name);
635 
636 	if (fstat(capfile_in, &st) < 0)
637 		pr_err("couldn't stat %s: %m", name);
638 	cap_len = st.st_size;
639 
640 	cap_buffp = mmap(0, cap_len, PROT_READ, MAP_PRIVATE, capfile_in, 0);
641 	(void) close(capfile_in);
642 	if ((int)cap_buffp == -1)
643 		pr_err("couldn't mmap %s: %m", name);
644 
645 	/* Check if new snoop capture file format */
646 
647 	cap_new = bcmp(cap_buffp, snoop_id, snoop_idlen) == 0;
648 
649 	/*
650 	 * If new file - check version and
651 	 * set buffer pointer to point at first packet
652 	 */
653 	if (cap_new) {
654 		cap_vers = ntohl(*(int *)(cap_buffp + snoop_idlen));
655 		cap_buffp += snoop_idlen + sizeof (int);
656 		cap_len   -= snoop_idlen + sizeof (int);
657 
658 		switch (cap_vers) {
659 		case 1:
660 			device_mac_type = DL_ETHER;
661 			break;
662 
663 		case 2:
664 			device_mac_type = ntohl(*((int *)cap_buffp));
665 			cap_buffp += sizeof (int);
666 			cap_len   -= sizeof (int);
667 			break;
668 
669 		default:
670 			pr_err("capture file: %s: Version %d unrecognized\n",
671 				name, cap_vers);
672 		}
673 
674 		for (interface = &INTERFACES[0]; interface->mac_type != -1;
675 				interface++)
676 			if (interface->mac_type == device_mac_type)
677 				break;
678 
679 		if (interface->mac_type == -1)
680 			pr_err("Mac Type = %x is not supported\n",
681 				device_mac_type);
682 	} else {
683 		/* Use heuristic to check if it's an old-style file */
684 
685 		device_mac_type = DL_ETHER;
686 		word = (int *)cap_buffp;
687 
688 		if (!((word[0] < 1600 && word[1] < 1600) &&
689 		    (word[0] < word[1]) &&
690 		    (word[2] > 610000000 && word[2] < 770000000)))
691 			pr_err("not a capture file: %s", name);
692 
693 		/* Change protection so's we can fix the headers */
694 
695 		if (mprotect(cap_buffp, cap_len, PROT_READ | PROT_WRITE) < 0)
696 			pr_err("mprotect: %s: %m", name);
697 	}
698 	dlinfo.di_max_sdu = MAXINT;	/* Decode any stored packet. */
699 }
700 
701 void
702 cap_read(int first, int last, int filter, void (*proc)(), int flags)
703 {
704 	extern int count;
705 
706 	count = 0;
707 
708 	scan(cap_buffp, cap_len, filter, 1, !cap_new, proc, first, last, flags);
709 
710 	(void) munmap(cap_buffp, cap_len);
711 }
712 
713 /* ARGSUSED */
714 void
715 cap_write(struct sb_hdr *hdrp, char *pktp, int num, int flags)
716 {
717 	int pktlen, mac;
718 	static int first = 1;
719 	struct sb_hdr nhdr;
720 	extern boolean_t qflg;
721 
722 	if (hdrp == NULL)
723 		return;
724 
725 	if (first) {
726 		first = 0;
727 		mac = htonl(interface->mac_type);
728 		if (nwrite(capfile_out, &mac, sizeof (int)) == -1)
729 			cap_write_error("mac_type");
730 	}
731 
732 	pktlen = hdrp->sbh_totlen - sizeof (*hdrp);
733 
734 	/*
735 	 * Convert sb_hdr to network byte order
736 	 */
737 	nhdr.sbh_origlen = htonl(hdrp->sbh_origlen);
738 	nhdr.sbh_msglen = htonl(hdrp->sbh_msglen);
739 	nhdr.sbh_totlen = htonl(hdrp->sbh_totlen);
740 	nhdr.sbh_drops = htonl(hdrp->sbh_drops);
741 	nhdr.sbh_timestamp.tv_sec = htonl(hdrp->sbh_timestamp.tv_sec);
742 	nhdr.sbh_timestamp.tv_usec = htonl(hdrp->sbh_timestamp.tv_usec);
743 
744 	if (nwrite(capfile_out, &nhdr, sizeof (nhdr)) == -1)
745 		cap_write_error("packet header");
746 
747 	if (nwrite(capfile_out, pktp, pktlen) == -1)
748 		cap_write_error("packet");
749 
750 	if (! qflg)
751 		show_count();
752 }
753 
754 /*
755  * Convert a packet header from
756  * old to new format.
757  */
758 static void
759 convert_old(struct ohdr *ohdrp)
760 {
761 	struct sb_hdr nhdr;
762 
763 	nhdr.sbh_origlen = ohdrp->o_len;
764 	nhdr.sbh_msglen  = ohdrp->o_msglen;
765 	nhdr.sbh_totlen  = ohdrp->o_totlen;
766 	nhdr.sbh_drops   = ohdrp->o_drops;
767 	nhdr.sbh_timestamp = ohdrp->o_time;
768 
769 	*(struct sb_hdr *)ohdrp = nhdr;
770 }
771 
772 static int
773 strioctl(int fd, int cmd, int timout, int len, void *dp)
774 {
775 	struct	strioctl	sioc;
776 	int	rc;
777 
778 	sioc.ic_cmd = cmd;
779 	sioc.ic_timout = timout;
780 	sioc.ic_len = len;
781 	sioc.ic_dp = dp;
782 	rc = ioctl(fd, I_STR, &sioc);
783 
784 	if (rc < 0)
785 		return (rc);
786 	else
787 		return (sioc.ic_len);
788 }
789