Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.56
      1 /*	$NetBSD: bpf.c,v 1.56 2000/05/28 02:49:35 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1990, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from the Stanford/CMU enet packet filter,
      8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
      9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
     10  * Berkeley Laboratory.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)bpf.c	8.4 (Berkeley) 1/9/95
     41  * static char rcsid[] =
     42  * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
     43  */
     44 
     45 #include "bpfilter.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/buf.h>
     51 #include <sys/time.h>
     52 #include <sys/proc.h>
     53 #include <sys/user.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/map.h>
     56 #include <sys/conf.h>
     57 #include <sys/vnode.h>
     58 
     59 #include <sys/file.h>
     60 #include <sys/tty.h>
     61 #include <sys/uio.h>
     62 
     63 #include <sys/protosw.h>
     64 #include <sys/socket.h>
     65 #include <sys/errno.h>
     66 #include <sys/kernel.h>
     67 #include <sys/poll.h>
     68 
     69 #include <net/if.h>
     70 
     71 #include <net/bpf.h>
     72 #include <net/bpfdesc.h>
     73 
     74 #include <net/if_arc.h>
     75 #include <net/if_ether.h>
     76 
     77 #include <netinet/in.h>
     78 #include <netinet/if_inarp.h>
     79 
     80 #ifndef BPF_BUFSIZE
     81 # define BPF_BUFSIZE 8192		/* 4096 too small for FDDI frames */
     82 #endif
     83 
     84 #define PRINET  26			/* interruptible */
     85 
     86 /*
     87  * The default read buffer size is patchable.
     88  */
     89 int bpf_bufsize = BPF_BUFSIZE;
     90 
     91 /*
     92  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
     93  *  bpf_dtab holds the descriptors, indexed by minor device #
     94  */
     95 struct bpf_if	*bpf_iflist;
     96 struct bpf_d	bpf_dtab[NBPFILTER];
     97 
     98 static int	bpf_allocbufs __P((struct bpf_d *));
     99 static void	bpf_freed __P((struct bpf_d *));
    100 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
    101 static void	*bpf_mcpy __P((void *, const void *, size_t));
    102 static int	bpf_movein __P((struct uio *, int, int,
    103 			        struct mbuf **, struct sockaddr *));
    104 static void	bpf_attachd __P((struct bpf_d *, struct bpf_if *));
    105 static void	bpf_detachd __P((struct bpf_d *));
    106 static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
    107 int		bpfpoll __P((dev_t, int, struct proc *));
    108 static __inline void
    109 		bpf_wakeup __P((struct bpf_d *));
    110 static void	catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
    111 				 void *(*)(void *, const void *, size_t)));
    112 static void	reset_d __P((struct bpf_d *));
    113 
    114 static int
    115 bpf_movein(uio, linktype, mtu, mp, sockp)
    116 	struct uio *uio;
    117 	int linktype;
    118 	int mtu;
    119 	struct mbuf **mp;
    120 	struct sockaddr *sockp;
    121 {
    122 	struct mbuf *m;
    123 	int error;
    124 	int len;
    125 	int hlen;
    126 	int align;
    127 
    128 	/*
    129 	 * Build a sockaddr based on the data link layer type.
    130 	 * We do this at this level because the ethernet header
    131 	 * is copied directly into the data field of the sockaddr.
    132 	 * In the case of SLIP, there is no header and the packet
    133 	 * is forwarded as is.
    134 	 * Also, we are careful to leave room at the front of the mbuf
    135 	 * for the link level header.
    136 	 */
    137 	switch (linktype) {
    138 
    139 	case DLT_SLIP:
    140 		sockp->sa_family = AF_INET;
    141 		hlen = 0;
    142 		align = 0;
    143 		break;
    144 
    145 	case DLT_PPP:
    146 		sockp->sa_family = AF_UNSPEC;
    147 		hlen = 0;
    148 		align = 0;
    149 		break;
    150 
    151 	case DLT_EN10MB:
    152 		sockp->sa_family = AF_UNSPEC;
    153 		/* XXX Would MAXLINKHDR be better? */
    154  		/* 6(dst)+6(src)+2(type) */
    155 		hlen = sizeof(struct ether_header);
    156 		align = 2;
    157 		break;
    158 
    159 	case DLT_ARCNET:
    160 		sockp->sa_family = AF_UNSPEC;
    161 		hlen = ARC_HDRLEN;
    162 		align = 5;
    163 		break;
    164 
    165 	case DLT_FDDI:
    166 		sockp->sa_family = AF_LINK;
    167 		/* XXX 4(FORMAC)+6(dst)+6(src) */
    168 		hlen = 16;
    169 		align = 0;
    170 		break;
    171 
    172 	case DLT_NULL:
    173 		sockp->sa_family = AF_UNSPEC;
    174 		hlen = 0;
    175 		align = 0;
    176 		break;
    177 
    178 	default:
    179 		return (EIO);
    180 	}
    181 
    182 	len = uio->uio_resid;
    183 	/*
    184 	 * If there aren't enough bytes for a link level header or the
    185 	 * packet length exceeds the interface mtu, return an error.
    186 	 */
    187 	if (len < hlen || len - hlen > mtu)
    188 		return (EMSGSIZE);
    189 
    190 	/*
    191 	 * XXX Avoid complicated buffer chaining ---
    192 	 * bail if it won't fit in a single mbuf.
    193 	 * (Take into account possible alignment bytes)
    194 	 */
    195 	if ((unsigned)len > MCLBYTES - align)
    196 		return (EIO);
    197 
    198 	MGETHDR(m, M_WAIT, MT_DATA);
    199 	if (m == 0)
    200 		return (ENOBUFS);
    201 	m->m_pkthdr.rcvif = 0;
    202 	m->m_pkthdr.len = len - hlen;
    203 	if (len > MHLEN - align) {
    204 		MCLGET(m, M_WAIT);
    205 		if ((m->m_flags & M_EXT) == 0) {
    206 			error = ENOBUFS;
    207 			goto bad;
    208 		}
    209 	}
    210 
    211 	/* Insure the data is properly aligned */
    212 	if (align > 0) {
    213 		m->m_data += align;
    214 		m->m_len -= align;
    215 	}
    216 
    217 	error = uiomove(mtod(m, caddr_t), len, uio);
    218 	if (error)
    219 		goto bad;
    220 	if (hlen != 0) {
    221 		memcpy(sockp->sa_data, mtod(m, caddr_t), hlen);
    222 		m->m_data += hlen; /* XXX */
    223 		len -= hlen;
    224 	}
    225 	m->m_len = len;
    226 	*mp = m;
    227 	return (0);
    228 
    229 bad:
    230 	m_freem(m);
    231 	return (error);
    232 }
    233 
    234 /*
    235  * Attach file to the bpf interface, i.e. make d listen on bp.
    236  * Must be called at splimp.
    237  */
    238 static void
    239 bpf_attachd(d, bp)
    240 	struct bpf_d *d;
    241 	struct bpf_if *bp;
    242 {
    243 	/*
    244 	 * Point d at bp, and add d to the interface's list of listeners.
    245 	 * Finally, point the driver's bpf cookie at the interface so
    246 	 * it will divert packets to bpf.
    247 	 */
    248 	d->bd_bif = bp;
    249 	d->bd_next = bp->bif_dlist;
    250 	bp->bif_dlist = d;
    251 
    252 	*bp->bif_driverp = bp;
    253 }
    254 
    255 /*
    256  * Detach a file from its interface.
    257  */
    258 static void
    259 bpf_detachd(d)
    260 	struct bpf_d *d;
    261 {
    262 	struct bpf_d **p;
    263 	struct bpf_if *bp;
    264 
    265 	bp = d->bd_bif;
    266 	/*
    267 	 * Check if this descriptor had requested promiscuous mode.
    268 	 * If so, turn it off.
    269 	 */
    270 	if (d->bd_promisc) {
    271 		int error;
    272 
    273 		d->bd_promisc = 0;
    274 		/*
    275 		 * Take device out of promiscuous mode.  Since we were
    276 		 * able to enter promiscuous mode, we should be able
    277 		 * to turn it off.  But we can get an error if
    278 		 * the interface was configured down, so only panic
    279 		 * if we don't get an unexpected error.
    280 		 */
    281   		error = ifpromisc(bp->bif_ifp, 0);
    282 		if (error && error != EINVAL)
    283 			panic("bpf: ifpromisc failed");
    284 	}
    285 	/* Remove d from the interface's descriptor list. */
    286 	p = &bp->bif_dlist;
    287 	while (*p != d) {
    288 		p = &(*p)->bd_next;
    289 		if (*p == 0)
    290 			panic("bpf_detachd: descriptor not in list");
    291 	}
    292 	*p = (*p)->bd_next;
    293 	if (bp->bif_dlist == 0)
    294 		/*
    295 		 * Let the driver know that there are no more listeners.
    296 		 */
    297 		*d->bd_bif->bif_driverp = 0;
    298 	d->bd_bif = 0;
    299 }
    300 
    301 
    302 /*
    303  * Mark a descriptor free by making it point to itself.
    304  * This is probably cheaper than marking with a constant since
    305  * the address should be in a register anyway.
    306  */
    307 #define D_ISFREE(d) ((d) == (d)->bd_next)
    308 #define D_MARKFREE(d) ((d)->bd_next = (d))
    309 #define D_MARKUSED(d) ((d)->bd_next = 0)
    310 
    311 /*
    312  * bpfilterattach() is called at boot time.
    313  */
    314 /* ARGSUSED */
    315 void
    316 bpfilterattach(n)
    317 	int n;
    318 {
    319 	int i;
    320 	/*
    321 	 * Mark all the descriptors free.
    322 	 */
    323 	for (i = 0; i < NBPFILTER; ++i)
    324 		D_MARKFREE(&bpf_dtab[i]);
    325 
    326 }
    327 
    328 /*
    329  * Open ethernet device.  Returns ENXIO for illegal minor device number,
    330  * EBUSY if file is open by another process.
    331  */
    332 /* ARGSUSED */
    333 int
    334 bpfopen(dev, flag, mode, p)
    335 	dev_t dev;
    336 	int flag;
    337 	int mode;
    338 	struct proc *p;
    339 {
    340 	struct bpf_d *d;
    341 
    342 	if (minor(dev) >= NBPFILTER)
    343 		return (ENXIO);
    344 	/*
    345 	 * Each minor can be opened by only one process.  If the requested
    346 	 * minor is in use, return EBUSY.
    347 	 */
    348 	d = &bpf_dtab[minor(dev)];
    349 	if (!D_ISFREE(d))
    350 		return (EBUSY);
    351 
    352 	/* Mark "free" and do most initialization. */
    353 	memset((char *)d, 0, sizeof(*d));
    354 	d->bd_bufsize = bpf_bufsize;
    355 
    356 	return (0);
    357 }
    358 
    359 /*
    360  * Close the descriptor by detaching it from its interface,
    361  * deallocating its buffers, and marking it free.
    362  */
    363 /* ARGSUSED */
    364 int
    365 bpfclose(dev, flag, mode, p)
    366 	dev_t dev;
    367 	int flag;
    368 	int mode;
    369 	struct proc *p;
    370 {
    371 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    372 	int s;
    373 
    374 	s = splimp();
    375 	if (d->bd_bif)
    376 		bpf_detachd(d);
    377 	splx(s);
    378 	bpf_freed(d);
    379 
    380 	return (0);
    381 }
    382 
    383 /*
    384  * Rotate the packet buffers in descriptor d.  Move the store buffer
    385  * into the hold slot, and the free buffer into the store slot.
    386  * Zero the length of the new store buffer.
    387  */
    388 #define ROTATE_BUFFERS(d) \
    389 	(d)->bd_hbuf = (d)->bd_sbuf; \
    390 	(d)->bd_hlen = (d)->bd_slen; \
    391 	(d)->bd_sbuf = (d)->bd_fbuf; \
    392 	(d)->bd_slen = 0; \
    393 	(d)->bd_fbuf = 0;
    394 /*
    395  *  bpfread - read next chunk of packets from buffers
    396  */
    397 int
    398 bpfread(dev, uio, ioflag)
    399 	dev_t dev;
    400 	struct uio *uio;
    401 	int ioflag;
    402 {
    403 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    404 	int error;
    405 	int s;
    406 
    407 	/*
    408 	 * Restrict application to use a buffer the same size as
    409 	 * as kernel buffers.
    410 	 */
    411 	if (uio->uio_resid != d->bd_bufsize)
    412 		return (EINVAL);
    413 
    414 	s = splimp();
    415 	/*
    416 	 * If the hold buffer is empty, then do a timed sleep, which
    417 	 * ends when the timeout expires or when enough packets
    418 	 * have arrived to fill the store buffer.
    419 	 */
    420 	while (d->bd_hbuf == 0) {
    421 		if (d->bd_immediate) {
    422 			if (d->bd_slen == 0) {
    423 				splx(s);
    424 				return (EWOULDBLOCK);
    425 			}
    426 			/*
    427 			 * A packet(s) either arrived since the previous
    428 			 * read or arrived while we were asleep.
    429 			 * Rotate the buffers and return what's here.
    430 			 */
    431 			ROTATE_BUFFERS(d);
    432 			break;
    433 		}
    434 		if (d->bd_rtout != -1)
    435 			error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf",
    436 					  d->bd_rtout);
    437 		else
    438 			error = EWOULDBLOCK; /* User requested non-blocking I/O */
    439 		if (error == EINTR || error == ERESTART) {
    440 			splx(s);
    441 			return (error);
    442 		}
    443 		if (error == EWOULDBLOCK) {
    444 			/*
    445 			 * On a timeout, return what's in the buffer,
    446 			 * which may be nothing.  If there is something
    447 			 * in the store buffer, we can rotate the buffers.
    448 			 */
    449 			if (d->bd_hbuf)
    450 				/*
    451 				 * We filled up the buffer in between
    452 				 * getting the timeout and arriving
    453 				 * here, so we don't need to rotate.
    454 				 */
    455 				break;
    456 
    457 			if (d->bd_slen == 0) {
    458 				splx(s);
    459 				return (0);
    460 			}
    461 			ROTATE_BUFFERS(d);
    462 			break;
    463 		}
    464 		if (error != 0)
    465 			goto done;
    466 	}
    467 	/*
    468 	 * At this point, we know we have something in the hold slot.
    469 	 */
    470 	splx(s);
    471 
    472 	/*
    473 	 * Move data from hold buffer into user space.
    474 	 * We know the entire buffer is transferred since
    475 	 * we checked above that the read buffer is bpf_bufsize bytes.
    476 	 */
    477 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
    478 
    479 	s = splimp();
    480 	d->bd_fbuf = d->bd_hbuf;
    481 	d->bd_hbuf = 0;
    482 	d->bd_hlen = 0;
    483 done:
    484 	splx(s);
    485 	return (error);
    486 }
    487 
    488 
    489 /*
    490  * If there are processes sleeping on this descriptor, wake them up.
    491  */
    492 static __inline void
    493 bpf_wakeup(d)
    494 	struct bpf_d *d;
    495 {
    496 	struct proc *p;
    497 
    498 	wakeup((caddr_t)d);
    499 	if (d->bd_async) {
    500 		if (d->bd_pgid > 0)
    501 			gsignal (d->bd_pgid, SIGIO);
    502 		else if ((p = pfind (-d->bd_pgid)) != NULL)
    503 			psignal (p, SIGIO);
    504 	}
    505 
    506 	selwakeup(&d->bd_sel);
    507 	/* XXX */
    508 	d->bd_sel.si_pid = 0;
    509 }
    510 
    511 int
    512 bpfwrite(dev, uio, ioflag)
    513 	dev_t dev;
    514 	struct uio *uio;
    515 	int ioflag;
    516 {
    517 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    518 	struct ifnet *ifp;
    519 	struct mbuf *m;
    520 	int error, s;
    521 	static struct sockaddr_storage dst;
    522 
    523 	if (d->bd_bif == 0)
    524 		return (ENXIO);
    525 
    526 	ifp = d->bd_bif->bif_ifp;
    527 
    528 	if (uio->uio_resid == 0)
    529 		return (0);
    530 
    531 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
    532 		(struct sockaddr *) &dst);
    533 	if (error)
    534 		return (error);
    535 
    536 	if (m->m_pkthdr.len > ifp->if_mtu)
    537 		return (EMSGSIZE);
    538 
    539 	if (d->bd_hdrcmplt)
    540 		dst.ss_family = pseudo_AF_HDRCMPLT;
    541 
    542 	s = splsoftnet();
    543 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
    544 	splx(s);
    545 	/*
    546 	 * The driver frees the mbuf.
    547 	 */
    548 	return (error);
    549 }
    550 
    551 /*
    552  * Reset a descriptor by flushing its packet buffer and clearing the
    553  * receive and drop counts.  Should be called at splimp.
    554  */
    555 static void
    556 reset_d(d)
    557 	struct bpf_d *d;
    558 {
    559 	if (d->bd_hbuf) {
    560 		/* Free the hold buffer. */
    561 		d->bd_fbuf = d->bd_hbuf;
    562 		d->bd_hbuf = 0;
    563 	}
    564 	d->bd_slen = 0;
    565 	d->bd_hlen = 0;
    566 	d->bd_rcount = 0;
    567 	d->bd_dcount = 0;
    568 }
    569 
    570 #ifdef BPF_KERN_FILTER
    571 extern struct bpf_insn *bpf_tcp_filter;
    572 extern struct bpf_insn *bpf_udp_filter;
    573 #endif
    574 
    575 /*
    576  *  FIONREAD		Check for read packet available.
    577  *  BIOCGBLEN		Get buffer len [for read()].
    578  *  BIOCSETF		Set ethernet read filter.
    579  *  BIOCFLUSH		Flush read packet buffer.
    580  *  BIOCPROMISC		Put interface into promiscuous mode.
    581  *  BIOCGDLT		Get link layer type.
    582  *  BIOCGETIF		Get interface name.
    583  *  BIOCSETIF		Set interface.
    584  *  BIOCSRTIMEOUT	Set read timeout.
    585  *  BIOCGRTIMEOUT	Get read timeout.
    586  *  BIOCGSTATS		Get packet stats.
    587  *  BIOCIMMEDIATE	Set immediate mode.
    588  *  BIOCVERSION		Get filter language version.
    589  *  BIOGHDRCMPLT	Get "header already complete" flag.
    590  *  BIOSHDRCMPLT	Set "header already complete" flag.
    591  */
    592 /* ARGSUSED */
    593 int
    594 bpfioctl(dev, cmd, addr, flag, p)
    595 	dev_t dev;
    596 	u_long cmd;
    597 	caddr_t addr;
    598 	int flag;
    599 	struct proc *p;
    600 {
    601 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    602 	int s, error = 0;
    603 #ifdef BPF_KERN_FILTER
    604 	struct bpf_insn **p;
    605 #endif
    606 
    607 	switch (cmd) {
    608 
    609 	default:
    610 		error = EINVAL;
    611 		break;
    612 
    613 	/*
    614 	 * Check for read packet available.
    615 	 */
    616 	case FIONREAD:
    617 		{
    618 			int n;
    619 
    620 			s = splimp();
    621 			n = d->bd_slen;
    622 			if (d->bd_hbuf)
    623 				n += d->bd_hlen;
    624 			splx(s);
    625 
    626 			*(int *)addr = n;
    627 			break;
    628 		}
    629 
    630 	/*
    631 	 * Get buffer len [for read()].
    632 	 */
    633 	case BIOCGBLEN:
    634 		*(u_int *)addr = d->bd_bufsize;
    635 		break;
    636 
    637 	/*
    638 	 * Set buffer length.
    639 	 */
    640 	case BIOCSBLEN:
    641 		if (d->bd_bif != 0)
    642 			error = EINVAL;
    643 		else {
    644 			u_int size = *(u_int *)addr;
    645 
    646 			if (size > BPF_MAXBUFSIZE)
    647 				*(u_int *)addr = size = BPF_MAXBUFSIZE;
    648 			else if (size < BPF_MINBUFSIZE)
    649 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    650 			d->bd_bufsize = size;
    651 		}
    652 		break;
    653 
    654 	/*
    655 	 * Set link layer read filter.
    656 	 */
    657 	case BIOCSETF:
    658 		error = bpf_setf(d, (struct bpf_program *)addr);
    659 		break;
    660 
    661 #ifdef BPF_KERN_FILTER
    662 	/*
    663 	 * Set TCP or UDP reject filter.
    664 	 */
    665 	case BIOCSTCPF:
    666 	case BIOCSUDPF:
    667 		if (!suser()) {
    668 			error = EPERM;
    669 			break;
    670 		}
    671 
    672 		/* Validate and store filter */
    673 		error = bpf_setf(d, (struct bpf_program *)addr);
    674 
    675 		/* Free possible old filter */
    676 		if (cmd == BIOCSTCPF)
    677 			p = &bpf_tcp_filter;
    678 		else
    679 			p = &bpf_udp_filter;
    680 		if (*p != NULL)
    681 			free((caddr_t)*p, M_DEVBUF);
    682 
    683 		/* Steal new filter (noop if error) */
    684 		s = splimp();
    685 		*p = d->bd_filter;
    686 		d->bd_filter = NULL;
    687 		splx(s);
    688 		break;
    689 #endif
    690 
    691 	/*
    692 	 * Flush read packet buffer.
    693 	 */
    694 	case BIOCFLUSH:
    695 		s = splimp();
    696 		reset_d(d);
    697 		splx(s);
    698 		break;
    699 
    700 	/*
    701 	 * Put interface into promiscuous mode.
    702 	 */
    703 	case BIOCPROMISC:
    704 		if (d->bd_bif == 0) {
    705 			/*
    706 			 * No interface attached yet.
    707 			 */
    708 			error = EINVAL;
    709 			break;
    710 		}
    711 		s = splimp();
    712 		if (d->bd_promisc == 0) {
    713 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    714 			if (error == 0)
    715 				d->bd_promisc = 1;
    716 		}
    717 		splx(s);
    718 		break;
    719 
    720 	/*
    721 	 * Get device parameters.
    722 	 */
    723 	case BIOCGDLT:
    724 		if (d->bd_bif == 0)
    725 			error = EINVAL;
    726 		else
    727 			*(u_int *)addr = d->bd_bif->bif_dlt;
    728 		break;
    729 
    730 	/*
    731 	 * Set interface name.
    732 	 */
    733 	case BIOCGETIF:
    734 		if (d->bd_bif == 0)
    735 			error = EINVAL;
    736 		else
    737 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
    738 		break;
    739 
    740 	/*
    741 	 * Set interface.
    742 	 */
    743 	case BIOCSETIF:
    744 		error = bpf_setif(d, (struct ifreq *)addr);
    745 		break;
    746 
    747 	/*
    748 	 * Set read timeout.
    749 	 */
    750 	case BIOCSRTIMEOUT:
    751 		{
    752 			struct timeval *tv = (struct timeval *)addr;
    753 
    754 			/* Compute number of ticks. */
    755 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    756 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    757 				d->bd_rtout = 1;
    758 			break;
    759 		}
    760 
    761 	/*
    762 	 * Get read timeout.
    763 	 */
    764 	case BIOCGRTIMEOUT:
    765 		{
    766 			struct timeval *tv = (struct timeval *)addr;
    767 
    768 			tv->tv_sec = d->bd_rtout / hz;
    769 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    770 			break;
    771 		}
    772 
    773 	/*
    774 	 * Get packet stats.
    775 	 */
    776 	case BIOCGSTATS:
    777 		{
    778 			struct bpf_stat *bs = (struct bpf_stat *)addr;
    779 
    780 			bs->bs_recv = d->bd_rcount;
    781 			bs->bs_drop = d->bd_dcount;
    782 			break;
    783 		}
    784 
    785 	/*
    786 	 * Set immediate mode.
    787 	 */
    788 	case BIOCIMMEDIATE:
    789 		d->bd_immediate = *(u_int *)addr;
    790 		break;
    791 
    792 	case BIOCVERSION:
    793 		{
    794 			struct bpf_version *bv = (struct bpf_version *)addr;
    795 
    796 			bv->bv_major = BPF_MAJOR_VERSION;
    797 			bv->bv_minor = BPF_MINOR_VERSION;
    798 			break;
    799 		}
    800 
    801 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    802 		*(u_int *)addr = d->bd_hdrcmplt;
    803 		break;
    804 
    805 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    806 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    807 		break;
    808 
    809 	case FIONBIO:		/* Non-blocking I/O */
    810 		if (*(int *)addr)
    811 			d->bd_rtout = -1;
    812 		else
    813 			d->bd_rtout = 0;
    814 		break;
    815 
    816 	case FIOASYNC:		/* Send signal on receive packets */
    817 		d->bd_async = *(int *)addr;
    818 		break;
    819 
    820 	/*
    821 	 * N.B.  ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
    822 	 * the equivalent of a TIOCSPGRP and hence end up here.  *However*
    823 	 * TIOCSPGRP's arg is a process group if it's positive and a process
    824 	 * id if it's negative.  This is exactly the opposite of what the
    825 	 * other two functions want!  Therefore there is code in ioctl and
    826 	 * fcntl to negate the arg before calling here.
    827 	 */
    828 	case TIOCSPGRP:		/* Process or group to send signals to */
    829 		d->bd_pgid = *(int *)addr;
    830 		break;
    831 
    832 	case TIOCGPGRP:
    833 		*(int *)addr = d->bd_pgid;
    834 		break;
    835 	}
    836 	return (error);
    837 }
    838 
    839 /*
    840  * Set d's packet filter program to fp.  If this file already has a filter,
    841  * free it and replace it.  Returns EINVAL for bogus requests.
    842  */
    843 int
    844 bpf_setf(d, fp)
    845 	struct bpf_d *d;
    846 	struct bpf_program *fp;
    847 {
    848 	struct bpf_insn *fcode, *old;
    849 	u_int flen, size;
    850 	int s;
    851 
    852 	old = d->bd_filter;
    853 	if (fp->bf_insns == 0) {
    854 		if (fp->bf_len != 0)
    855 			return (EINVAL);
    856 		s = splimp();
    857 		d->bd_filter = 0;
    858 		reset_d(d);
    859 		splx(s);
    860 		if (old != 0)
    861 			free((caddr_t)old, M_DEVBUF);
    862 		return (0);
    863 	}
    864 	flen = fp->bf_len;
    865 	if (flen > BPF_MAXINSNS)
    866 		return (EINVAL);
    867 
    868 	size = flen * sizeof(*fp->bf_insns);
    869 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
    870 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
    871 	    bpf_validate(fcode, (int)flen)) {
    872 		s = splimp();
    873 		d->bd_filter = fcode;
    874 		reset_d(d);
    875 		splx(s);
    876 		if (old != 0)
    877 			free((caddr_t)old, M_DEVBUF);
    878 
    879 		return (0);
    880 	}
    881 	free((caddr_t)fcode, M_DEVBUF);
    882 	return (EINVAL);
    883 }
    884 
    885 /*
    886  * Detach a file from its current interface (if attached at all) and attach
    887  * to the interface indicated by the name stored in ifr.
    888  * Return an errno or 0.
    889  */
    890 static int
    891 bpf_setif(d, ifr)
    892 	struct bpf_d *d;
    893 	struct ifreq *ifr;
    894 {
    895 	struct bpf_if *bp;
    896 	char *cp;
    897 	int unit_seen, i, s, error;
    898 
    899 	/*
    900 	 * Make sure the provided name has a unit number, and default
    901 	 * it to '0' if not specified.
    902 	 * XXX This is ugly ... do this differently?
    903 	 */
    904 	unit_seen = 0;
    905 	cp = ifr->ifr_name;
    906 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
    907 	while (*cp++)
    908 		if (*cp >= '0' && *cp <= '9')
    909 			unit_seen = 1;
    910 	if (!unit_seen) {
    911 		/* Make sure to leave room for the '\0'. */
    912 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
    913 			if ((ifr->ifr_name[i] >= 'a' &&
    914 			     ifr->ifr_name[i] <= 'z') ||
    915 			    (ifr->ifr_name[i] >= 'A' &&
    916 			     ifr->ifr_name[i] <= 'Z'))
    917 				continue;
    918 			ifr->ifr_name[i] = '0';
    919 		}
    920 	}
    921 
    922 	/*
    923 	 * Look through attached interfaces for the named one.
    924 	 */
    925 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
    926 		struct ifnet *ifp = bp->bif_ifp;
    927 
    928 		if (ifp == 0 ||
    929 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
    930 			continue;
    931 		/*
    932 		 * We found the requested interface.
    933 		 * If it's not up, return an error.
    934 		 * Allocate the packet buffers if we need to.
    935 		 * If we're already attached to requested interface,
    936 		 * just flush the buffer.
    937 		 */
    938 		if ((ifp->if_flags & IFF_UP) == 0)
    939 			return (ENETDOWN);
    940 
    941 		if (d->bd_sbuf == 0) {
    942 			error = bpf_allocbufs(d);
    943 			if (error != 0)
    944 				return (error);
    945 		}
    946 		s = splimp();
    947 		if (bp != d->bd_bif) {
    948 			if (d->bd_bif)
    949 				/*
    950 				 * Detach if attached to something else.
    951 				 */
    952 				bpf_detachd(d);
    953 
    954 			bpf_attachd(d, bp);
    955 		}
    956 		reset_d(d);
    957 		splx(s);
    958 		return (0);
    959 	}
    960 	/* Not found. */
    961 	return (ENXIO);
    962 }
    963 
    964 /*
    965  * Copy the interface name to the ifreq.
    966  */
    967 static void
    968 bpf_ifname(ifp, ifr)
    969 	struct ifnet *ifp;
    970 	struct ifreq *ifr;
    971 {
    972 
    973 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    974 }
    975 
    976 /*
    977  * Support for poll() system call
    978  *
    979  * Return true iff the specific operation will not block indefinitely.
    980  * Otherwise, return false but make a note that a selwakeup() must be done.
    981  */
    982 int
    983 bpfpoll(dev, events, p)
    984 	dev_t dev;
    985 	int events;
    986 	struct proc *p;
    987 {
    988 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    989 	int revents = 0;
    990 	int s = splimp();
    991 
    992 	/*
    993 	 * An imitation of the FIONREAD ioctl code.
    994 	 */
    995 	if (events & (POLLIN | POLLRDNORM)) {
    996 		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
    997 			revents |= events & (POLLIN | POLLRDNORM);
    998 		else
    999 			selrecord(p, &d->bd_sel);
   1000 	}
   1001 
   1002 	splx(s);
   1003 	return (revents);
   1004 }
   1005 
   1006 /*
   1007  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1008  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1009  * by each process' filter, and if accepted, stashed into the corresponding
   1010  * buffer.
   1011  */
   1012 void
   1013 bpf_tap(arg, pkt, pktlen)
   1014 	caddr_t arg;
   1015 	u_char *pkt;
   1016 	u_int pktlen;
   1017 {
   1018 	struct bpf_if *bp;
   1019 	struct bpf_d *d;
   1020 	u_int slen;
   1021 	/*
   1022 	 * Note that the ipl does not have to be raised at this point.
   1023 	 * The only problem that could arise here is that if two different
   1024 	 * interfaces shared any data.  This is not the case.
   1025 	 */
   1026 	bp = (struct bpf_if *)arg;
   1027 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1028 		++d->bd_rcount;
   1029 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1030 		if (slen != 0)
   1031 			catchpacket(d, pkt, pktlen, slen, memcpy);
   1032 	}
   1033 }
   1034 
   1035 /*
   1036  * Copy data from an mbuf chain into a buffer.  This code is derived
   1037  * from m_copydata in sys/uipc_mbuf.c.
   1038  */
   1039 static void *
   1040 bpf_mcpy(dst_arg, src_arg, len)
   1041 	void *dst_arg;
   1042 	const void *src_arg;
   1043 	size_t len;
   1044 {
   1045 	const struct mbuf *m;
   1046 	u_int count;
   1047 	u_char *dst;
   1048 
   1049 	m = src_arg;
   1050 	dst = dst_arg;
   1051 	while (len > 0) {
   1052 		if (m == 0)
   1053 			panic("bpf_mcpy");
   1054 		count = min(m->m_len, len);
   1055 		memcpy((caddr_t)dst, mtod(m, caddr_t), count);
   1056 		m = m->m_next;
   1057 		dst += count;
   1058 		len -= count;
   1059 	}
   1060 	return(dst_arg);
   1061 }
   1062 
   1063 /*
   1064  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1065  */
   1066 void
   1067 bpf_mtap(arg, m)
   1068 	caddr_t arg;
   1069 	struct mbuf *m;
   1070 {
   1071 	struct bpf_if *bp = (struct bpf_if *)arg;
   1072 	struct bpf_d *d;
   1073 	u_int pktlen, slen;
   1074 	struct mbuf *m0;
   1075 
   1076 	pktlen = 0;
   1077 	for (m0 = m; m0 != 0; m0 = m0->m_next)
   1078 		pktlen += m0->m_len;
   1079 
   1080 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1081 		++d->bd_rcount;
   1082 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
   1083 		if (slen != 0)
   1084 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcpy);
   1085 	}
   1086 }
   1087 
   1088 /*
   1089  * Move the packet data from interface memory (pkt) into the
   1090  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1091  * otherwise 0.  "copy" is the routine called to do the actual data
   1092  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1093  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1094  * pkt is really an mbuf.
   1095  */
   1096 static void
   1097 catchpacket(d, pkt, pktlen, snaplen, cpfn)
   1098 	struct bpf_d *d;
   1099 	u_char *pkt;
   1100 	u_int pktlen, snaplen;
   1101 	void *(*cpfn) __P((void *, const void *, size_t));
   1102 {
   1103 	struct bpf_hdr *hp;
   1104 	int totlen, curlen;
   1105 	int hdrlen = d->bd_bif->bif_hdrlen;
   1106 	/*
   1107 	 * Figure out how many bytes to move.  If the packet is
   1108 	 * greater or equal to the snapshot length, transfer that
   1109 	 * much.  Otherwise, transfer the whole packet (unless
   1110 	 * we hit the buffer size limit).
   1111 	 */
   1112 	totlen = hdrlen + min(snaplen, pktlen);
   1113 	if (totlen > d->bd_bufsize)
   1114 		totlen = d->bd_bufsize;
   1115 
   1116 	/*
   1117 	 * Round up the end of the previous packet to the next longword.
   1118 	 */
   1119 	curlen = BPF_WORDALIGN(d->bd_slen);
   1120 	if (curlen + totlen > d->bd_bufsize) {
   1121 		/*
   1122 		 * This packet will overflow the storage buffer.
   1123 		 * Rotate the buffers if we can, then wakeup any
   1124 		 * pending reads.
   1125 		 */
   1126 		if (d->bd_fbuf == 0) {
   1127 			/*
   1128 			 * We haven't completed the previous read yet,
   1129 			 * so drop the packet.
   1130 			 */
   1131 			++d->bd_dcount;
   1132 			return;
   1133 		}
   1134 		ROTATE_BUFFERS(d);
   1135 		bpf_wakeup(d);
   1136 		curlen = 0;
   1137 	}
   1138 	else if (d->bd_immediate)
   1139 		/*
   1140 		 * Immediate mode is set.  A packet arrived so any
   1141 		 * reads should be woken up.
   1142 		 */
   1143 		bpf_wakeup(d);
   1144 
   1145 	/*
   1146 	 * Append the bpf header.
   1147 	 */
   1148 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1149 	microtime(&hp->bh_tstamp);
   1150 	hp->bh_datalen = pktlen;
   1151 	hp->bh_hdrlen = hdrlen;
   1152 	/*
   1153 	 * Copy the packet data into the store buffer and update its length.
   1154 	 */
   1155 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1156 	d->bd_slen = curlen + totlen;
   1157 }
   1158 
   1159 /*
   1160  * Initialize all nonzero fields of a descriptor.
   1161  */
   1162 static int
   1163 bpf_allocbufs(d)
   1164 	struct bpf_d *d;
   1165 {
   1166 
   1167 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1168 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1169 	d->bd_slen = 0;
   1170 	d->bd_hlen = 0;
   1171 	return (0);
   1172 }
   1173 
   1174 /*
   1175  * Free buffers currently in use by a descriptor.
   1176  * Called on close.
   1177  */
   1178 static void
   1179 bpf_freed(d)
   1180 	struct bpf_d *d;
   1181 {
   1182 	/*
   1183 	 * We don't need to lock out interrupts since this descriptor has
   1184 	 * been detached from its interface and it yet hasn't been marked
   1185 	 * free.
   1186 	 */
   1187 	if (d->bd_sbuf != 0) {
   1188 		free(d->bd_sbuf, M_DEVBUF);
   1189 		if (d->bd_hbuf != 0)
   1190 			free(d->bd_hbuf, M_DEVBUF);
   1191 		if (d->bd_fbuf != 0)
   1192 			free(d->bd_fbuf, M_DEVBUF);
   1193 	}
   1194 	if (d->bd_filter)
   1195 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1196 
   1197 	D_MARKFREE(d);
   1198 }
   1199 
   1200 /*
   1201  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
   1202  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
   1203  * size of the link header (variable length headers not yet supported).
   1204  */
   1205 void
   1206 bpfattach(driverp, ifp, dlt, hdrlen)
   1207 	caddr_t *driverp;
   1208 	struct ifnet *ifp;
   1209 	u_int dlt, hdrlen;
   1210 {
   1211 	struct bpf_if *bp;
   1212 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1213 	if (bp == 0)
   1214 		panic("bpfattach");
   1215 
   1216 	bp->bif_dlist = 0;
   1217 	bp->bif_driverp = (struct bpf_if **)driverp;
   1218 	bp->bif_ifp = ifp;
   1219 	bp->bif_dlt = dlt;
   1220 
   1221 	bp->bif_next = bpf_iflist;
   1222 	bpf_iflist = bp;
   1223 
   1224 	*bp->bif_driverp = 0;
   1225 
   1226 	/*
   1227 	 * Compute the length of the bpf header.  This is not necessarily
   1228 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1229 	 * that the network layer header begins on a longword boundary (for
   1230 	 * performance reasons and to alleviate alignment restrictions).
   1231 	 */
   1232 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1233 
   1234 #if 0
   1235 	printf("bpf: %s attached\n", ifp->if_xname);
   1236 #endif
   1237 }
   1238 
   1239 /*
   1240  * Remove an interface from bpf.
   1241  */
   1242 void
   1243 bpfdetach(ifp)
   1244 	struct ifnet *ifp;
   1245 {
   1246 	struct bpf_if *bp, **pbp;
   1247 	struct bpf_d *d;
   1248 	int i, s, cmaj;
   1249 
   1250 	/* locate the major number */
   1251 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
   1252 		if (cdevsw[cmaj].d_open == bpfopen)
   1253 			break;
   1254 
   1255 	/* Nuke the vnodes for any open instances */
   1256 	for (i = 0; i < NBPFILTER; ++i) {
   1257 		d = &bpf_dtab[i];
   1258 		if (!D_ISFREE(d) && d->bd_bif != NULL &&
   1259 		    d->bd_bif->bif_ifp == ifp) {
   1260 			/*
   1261 			 * Detach the descriptor from an interface now.
   1262 			 * It will be free'ed later by close routine.
   1263 			 */
   1264 			s = splimp();
   1265 			d->bd_promisc = 0;	/* we can't touch device. */
   1266 			bpf_detachd(d);
   1267 			splx(s);
   1268 			vdevgone(cmaj, i, i, VCHR);
   1269 		}
   1270 	}
   1271 
   1272 	for (bp = bpf_iflist, pbp = &bpf_iflist;
   1273 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
   1274 		if (bp->bif_ifp == ifp) {
   1275 			*pbp = bp->bif_next;
   1276 			free(bp, M_DEVBUF);
   1277 			break;
   1278 		}
   1279 	}
   1280 }
   1281 
   1282 /*
   1283  * Change the data link type of a BPF instance.
   1284  */
   1285 void
   1286 bpf_change_type(driverp, dlt, hdrlen)
   1287 	caddr_t *driverp;
   1288 	u_int dlt, hdrlen;
   1289 {
   1290 	struct bpf_if *bp;
   1291 
   1292 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1293 		if (bp->bif_driverp == (struct bpf_if **)driverp)
   1294 			break;
   1295 	}
   1296 	if (bp == NULL)
   1297 		panic("bpf_change_type");
   1298 
   1299 	bp->bif_dlt = dlt;
   1300 
   1301 	/*
   1302 	 * Compute the length of the bpf header.  This is not necessarily
   1303 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1304 	 * that the network layer header begins on a longword boundary (for
   1305 	 * performance reasons and to alleviate alignment restrictions).
   1306 	 */
   1307 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1308 }
   1309 
   1310 /* XXX This routine belongs in net/if.c. */
   1311 /*
   1312  * Set/clear promiscuous mode on interface ifp based on the truth value
   1313  * of pswitch.  The calls are reference counted so that only the first
   1314  * "on" request actually has an effect, as does the final "off" request.
   1315  * Results are undefined if the "off" and "on" requests are not matched.
   1316  */
   1317 int
   1318 ifpromisc(ifp, pswitch)
   1319 	struct ifnet *ifp;
   1320 	int pswitch;
   1321 {
   1322 	int pcount, ret;
   1323 	short flags;
   1324 	struct ifreq ifr;
   1325 
   1326 	pcount = ifp->if_pcount;
   1327 	flags = ifp->if_flags;
   1328 	if (pswitch) {
   1329 		/*
   1330 		 * If the device is not configured up, we cannot put it in
   1331 		 * promiscuous mode.
   1332 		 */
   1333 		if ((ifp->if_flags & IFF_UP) == 0)
   1334 			return (ENETDOWN);
   1335 		if (ifp->if_pcount++ != 0)
   1336 			return (0);
   1337 		ifp->if_flags |= IFF_PROMISC;
   1338 	} else {
   1339 		if (--ifp->if_pcount > 0)
   1340 			return (0);
   1341 		ifp->if_flags &= ~IFF_PROMISC;
   1342 		/*
   1343 		 * If the device is not configured up, we should not need to
   1344 		 * turn off promiscuous mode (device should have turned it
   1345 		 * off when interface went down; and will look at IFF_PROMISC
   1346 		 * again next time interface comes up).
   1347 		 */
   1348 		if ((ifp->if_flags & IFF_UP) == 0)
   1349 			return (0);
   1350 	}
   1351 	memset((caddr_t)&ifr, 0, sizeof(ifr));
   1352 	ifr.ifr_flags = ifp->if_flags;
   1353 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
   1354 	/* Restore interface state if not successful */
   1355 	if (ret != 0) {
   1356 		ifp->if_pcount = pcount;
   1357 		ifp->if_flags = flags;
   1358 	}
   1359 	return (ret);
   1360 }
   1361