Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.55
      1 /*	$NetBSD: bpf.c,v 1.55 2000/05/12 05:58:01 jonathan 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_UNSPEC;
    167 		/* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
    168 		hlen = 24;
    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 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, &dst);
    532 	if (error)
    533 		return (error);
    534 
    535 	if (m->m_pkthdr.len > ifp->if_mtu)
    536 		return (EMSGSIZE);
    537 
    538 	if (d->bd_hdrcmplt)
    539 		dst.sa_family = pseudo_AF_HDRCMPLT;
    540 
    541 	s = splsoftnet();
    542 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
    543 	splx(s);
    544 	/*
    545 	 * The driver frees the mbuf.
    546 	 */
    547 	return (error);
    548 }
    549 
    550 /*
    551  * Reset a descriptor by flushing its packet buffer and clearing the
    552  * receive and drop counts.  Should be called at splimp.
    553  */
    554 static void
    555 reset_d(d)
    556 	struct bpf_d *d;
    557 {
    558 	if (d->bd_hbuf) {
    559 		/* Free the hold buffer. */
    560 		d->bd_fbuf = d->bd_hbuf;
    561 		d->bd_hbuf = 0;
    562 	}
    563 	d->bd_slen = 0;
    564 	d->bd_hlen = 0;
    565 	d->bd_rcount = 0;
    566 	d->bd_dcount = 0;
    567 }
    568 
    569 #ifdef BPF_KERN_FILTER
    570 extern struct bpf_insn *bpf_tcp_filter;
    571 extern struct bpf_insn *bpf_udp_filter;
    572 #endif
    573 
    574 /*
    575  *  FIONREAD		Check for read packet available.
    576  *  BIOCGBLEN		Get buffer len [for read()].
    577  *  BIOCSETF		Set ethernet read filter.
    578  *  BIOCFLUSH		Flush read packet buffer.
    579  *  BIOCPROMISC		Put interface into promiscuous mode.
    580  *  BIOCGDLT		Get link layer type.
    581  *  BIOCGETIF		Get interface name.
    582  *  BIOCSETIF		Set interface.
    583  *  BIOCSRTIMEOUT	Set read timeout.
    584  *  BIOCGRTIMEOUT	Get read timeout.
    585  *  BIOCGSTATS		Get packet stats.
    586  *  BIOCIMMEDIATE	Set immediate mode.
    587  *  BIOCVERSION		Get filter language version.
    588  *  BIOGHDRCMPLT	Get "header already complete" flag.
    589  *  BIOSHDRCMPLT	Set "header already complete" flag.
    590  */
    591 /* ARGSUSED */
    592 int
    593 bpfioctl(dev, cmd, addr, flag, p)
    594 	dev_t dev;
    595 	u_long cmd;
    596 	caddr_t addr;
    597 	int flag;
    598 	struct proc *p;
    599 {
    600 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    601 	int s, error = 0;
    602 #ifdef BPF_KERN_FILTER
    603 	struct bpf_insn **p;
    604 #endif
    605 
    606 	switch (cmd) {
    607 
    608 	default:
    609 		error = EINVAL;
    610 		break;
    611 
    612 	/*
    613 	 * Check for read packet available.
    614 	 */
    615 	case FIONREAD:
    616 		{
    617 			int n;
    618 
    619 			s = splimp();
    620 			n = d->bd_slen;
    621 			if (d->bd_hbuf)
    622 				n += d->bd_hlen;
    623 			splx(s);
    624 
    625 			*(int *)addr = n;
    626 			break;
    627 		}
    628 
    629 	/*
    630 	 * Get buffer len [for read()].
    631 	 */
    632 	case BIOCGBLEN:
    633 		*(u_int *)addr = d->bd_bufsize;
    634 		break;
    635 
    636 	/*
    637 	 * Set buffer length.
    638 	 */
    639 	case BIOCSBLEN:
    640 		if (d->bd_bif != 0)
    641 			error = EINVAL;
    642 		else {
    643 			u_int size = *(u_int *)addr;
    644 
    645 			if (size > BPF_MAXBUFSIZE)
    646 				*(u_int *)addr = size = BPF_MAXBUFSIZE;
    647 			else if (size < BPF_MINBUFSIZE)
    648 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    649 			d->bd_bufsize = size;
    650 		}
    651 		break;
    652 
    653 	/*
    654 	 * Set link layer read filter.
    655 	 */
    656 	case BIOCSETF:
    657 		error = bpf_setf(d, (struct bpf_program *)addr);
    658 		break;
    659 
    660 #ifdef BPF_KERN_FILTER
    661 	/*
    662 	 * Set TCP or UDP reject filter.
    663 	 */
    664 	case BIOCSTCPF:
    665 	case BIOCSUDPF:
    666 		if (!suser()) {
    667 			error = EPERM;
    668 			break;
    669 		}
    670 
    671 		/* Validate and store filter */
    672 		error = bpf_setf(d, (struct bpf_program *)addr);
    673 
    674 		/* Free possible old filter */
    675 		if (cmd == BIOCSTCPF)
    676 			p = &bpf_tcp_filter;
    677 		else
    678 			p = &bpf_udp_filter;
    679 		if (*p != NULL)
    680 			free((caddr_t)*p, M_DEVBUF);
    681 
    682 		/* Steal new filter (noop if error) */
    683 		s = splimp();
    684 		*p = d->bd_filter;
    685 		d->bd_filter = NULL;
    686 		splx(s);
    687 		break;
    688 #endif
    689 
    690 	/*
    691 	 * Flush read packet buffer.
    692 	 */
    693 	case BIOCFLUSH:
    694 		s = splimp();
    695 		reset_d(d);
    696 		splx(s);
    697 		break;
    698 
    699 	/*
    700 	 * Put interface into promiscuous mode.
    701 	 */
    702 	case BIOCPROMISC:
    703 		if (d->bd_bif == 0) {
    704 			/*
    705 			 * No interface attached yet.
    706 			 */
    707 			error = EINVAL;
    708 			break;
    709 		}
    710 		s = splimp();
    711 		if (d->bd_promisc == 0) {
    712 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    713 			if (error == 0)
    714 				d->bd_promisc = 1;
    715 		}
    716 		splx(s);
    717 		break;
    718 
    719 	/*
    720 	 * Get device parameters.
    721 	 */
    722 	case BIOCGDLT:
    723 		if (d->bd_bif == 0)
    724 			error = EINVAL;
    725 		else
    726 			*(u_int *)addr = d->bd_bif->bif_dlt;
    727 		break;
    728 
    729 	/*
    730 	 * Set interface name.
    731 	 */
    732 	case BIOCGETIF:
    733 		if (d->bd_bif == 0)
    734 			error = EINVAL;
    735 		else
    736 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
    737 		break;
    738 
    739 	/*
    740 	 * Set interface.
    741 	 */
    742 	case BIOCSETIF:
    743 		error = bpf_setif(d, (struct ifreq *)addr);
    744 		break;
    745 
    746 	/*
    747 	 * Set read timeout.
    748 	 */
    749 	case BIOCSRTIMEOUT:
    750 		{
    751 			struct timeval *tv = (struct timeval *)addr;
    752 
    753 			/* Compute number of ticks. */
    754 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    755 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    756 				d->bd_rtout = 1;
    757 			break;
    758 		}
    759 
    760 	/*
    761 	 * Get read timeout.
    762 	 */
    763 	case BIOCGRTIMEOUT:
    764 		{
    765 			struct timeval *tv = (struct timeval *)addr;
    766 
    767 			tv->tv_sec = d->bd_rtout / hz;
    768 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    769 			break;
    770 		}
    771 
    772 	/*
    773 	 * Get packet stats.
    774 	 */
    775 	case BIOCGSTATS:
    776 		{
    777 			struct bpf_stat *bs = (struct bpf_stat *)addr;
    778 
    779 			bs->bs_recv = d->bd_rcount;
    780 			bs->bs_drop = d->bd_dcount;
    781 			break;
    782 		}
    783 
    784 	/*
    785 	 * Set immediate mode.
    786 	 */
    787 	case BIOCIMMEDIATE:
    788 		d->bd_immediate = *(u_int *)addr;
    789 		break;
    790 
    791 	case BIOCVERSION:
    792 		{
    793 			struct bpf_version *bv = (struct bpf_version *)addr;
    794 
    795 			bv->bv_major = BPF_MAJOR_VERSION;
    796 			bv->bv_minor = BPF_MINOR_VERSION;
    797 			break;
    798 		}
    799 
    800 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    801 		*(u_int *)addr = d->bd_hdrcmplt;
    802 		break;
    803 
    804 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    805 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    806 		break;
    807 
    808 	case FIONBIO:		/* Non-blocking I/O */
    809 		if (*(int *)addr)
    810 			d->bd_rtout = -1;
    811 		else
    812 			d->bd_rtout = 0;
    813 		break;
    814 
    815 	case FIOASYNC:		/* Send signal on receive packets */
    816 		d->bd_async = *(int *)addr;
    817 		break;
    818 
    819 	/*
    820 	 * N.B.  ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
    821 	 * the equivalent of a TIOCSPGRP and hence end up here.  *However*
    822 	 * TIOCSPGRP's arg is a process group if it's positive and a process
    823 	 * id if it's negative.  This is exactly the opposite of what the
    824 	 * other two functions want!  Therefore there is code in ioctl and
    825 	 * fcntl to negate the arg before calling here.
    826 	 */
    827 	case TIOCSPGRP:		/* Process or group to send signals to */
    828 		d->bd_pgid = *(int *)addr;
    829 		break;
    830 
    831 	case TIOCGPGRP:
    832 		*(int *)addr = d->bd_pgid;
    833 		break;
    834 	}
    835 	return (error);
    836 }
    837 
    838 /*
    839  * Set d's packet filter program to fp.  If this file already has a filter,
    840  * free it and replace it.  Returns EINVAL for bogus requests.
    841  */
    842 int
    843 bpf_setf(d, fp)
    844 	struct bpf_d *d;
    845 	struct bpf_program *fp;
    846 {
    847 	struct bpf_insn *fcode, *old;
    848 	u_int flen, size;
    849 	int s;
    850 
    851 	old = d->bd_filter;
    852 	if (fp->bf_insns == 0) {
    853 		if (fp->bf_len != 0)
    854 			return (EINVAL);
    855 		s = splimp();
    856 		d->bd_filter = 0;
    857 		reset_d(d);
    858 		splx(s);
    859 		if (old != 0)
    860 			free((caddr_t)old, M_DEVBUF);
    861 		return (0);
    862 	}
    863 	flen = fp->bf_len;
    864 	if (flen > BPF_MAXINSNS)
    865 		return (EINVAL);
    866 
    867 	size = flen * sizeof(*fp->bf_insns);
    868 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
    869 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
    870 	    bpf_validate(fcode, (int)flen)) {
    871 		s = splimp();
    872 		d->bd_filter = fcode;
    873 		reset_d(d);
    874 		splx(s);
    875 		if (old != 0)
    876 			free((caddr_t)old, M_DEVBUF);
    877 
    878 		return (0);
    879 	}
    880 	free((caddr_t)fcode, M_DEVBUF);
    881 	return (EINVAL);
    882 }
    883 
    884 /*
    885  * Detach a file from its current interface (if attached at all) and attach
    886  * to the interface indicated by the name stored in ifr.
    887  * Return an errno or 0.
    888  */
    889 static int
    890 bpf_setif(d, ifr)
    891 	struct bpf_d *d;
    892 	struct ifreq *ifr;
    893 {
    894 	struct bpf_if *bp;
    895 	char *cp;
    896 	int unit_seen, i, s, error;
    897 
    898 	/*
    899 	 * Make sure the provided name has a unit number, and default
    900 	 * it to '0' if not specified.
    901 	 * XXX This is ugly ... do this differently?
    902 	 */
    903 	unit_seen = 0;
    904 	cp = ifr->ifr_name;
    905 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
    906 	while (*cp++)
    907 		if (*cp >= '0' && *cp <= '9')
    908 			unit_seen = 1;
    909 	if (!unit_seen) {
    910 		/* Make sure to leave room for the '\0'. */
    911 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
    912 			if ((ifr->ifr_name[i] >= 'a' &&
    913 			     ifr->ifr_name[i] <= 'z') ||
    914 			    (ifr->ifr_name[i] >= 'A' &&
    915 			     ifr->ifr_name[i] <= 'Z'))
    916 				continue;
    917 			ifr->ifr_name[i] = '0';
    918 		}
    919 	}
    920 
    921 	/*
    922 	 * Look through attached interfaces for the named one.
    923 	 */
    924 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
    925 		struct ifnet *ifp = bp->bif_ifp;
    926 
    927 		if (ifp == 0 ||
    928 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
    929 			continue;
    930 		/*
    931 		 * We found the requested interface.
    932 		 * If it's not up, return an error.
    933 		 * Allocate the packet buffers if we need to.
    934 		 * If we're already attached to requested interface,
    935 		 * just flush the buffer.
    936 		 */
    937 		if ((ifp->if_flags & IFF_UP) == 0)
    938 			return (ENETDOWN);
    939 
    940 		if (d->bd_sbuf == 0) {
    941 			error = bpf_allocbufs(d);
    942 			if (error != 0)
    943 				return (error);
    944 		}
    945 		s = splimp();
    946 		if (bp != d->bd_bif) {
    947 			if (d->bd_bif)
    948 				/*
    949 				 * Detach if attached to something else.
    950 				 */
    951 				bpf_detachd(d);
    952 
    953 			bpf_attachd(d, bp);
    954 		}
    955 		reset_d(d);
    956 		splx(s);
    957 		return (0);
    958 	}
    959 	/* Not found. */
    960 	return (ENXIO);
    961 }
    962 
    963 /*
    964  * Copy the interface name to the ifreq.
    965  */
    966 static void
    967 bpf_ifname(ifp, ifr)
    968 	struct ifnet *ifp;
    969 	struct ifreq *ifr;
    970 {
    971 
    972 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    973 }
    974 
    975 /*
    976  * Support for poll() system call
    977  *
    978  * Return true iff the specific operation will not block indefinitely.
    979  * Otherwise, return false but make a note that a selwakeup() must be done.
    980  */
    981 int
    982 bpfpoll(dev, events, p)
    983 	dev_t dev;
    984 	int events;
    985 	struct proc *p;
    986 {
    987 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    988 	int revents = 0;
    989 	int s = splimp();
    990 
    991 	/*
    992 	 * An imitation of the FIONREAD ioctl code.
    993 	 */
    994 	if (events & (POLLIN | POLLRDNORM)) {
    995 		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
    996 			revents |= events & (POLLIN | POLLRDNORM);
    997 		else
    998 			selrecord(p, &d->bd_sel);
    999 	}
   1000 
   1001 	splx(s);
   1002 	return (revents);
   1003 }
   1004 
   1005 /*
   1006  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1007  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1008  * by each process' filter, and if accepted, stashed into the corresponding
   1009  * buffer.
   1010  */
   1011 void
   1012 bpf_tap(arg, pkt, pktlen)
   1013 	caddr_t arg;
   1014 	u_char *pkt;
   1015 	u_int pktlen;
   1016 {
   1017 	struct bpf_if *bp;
   1018 	struct bpf_d *d;
   1019 	u_int slen;
   1020 	/*
   1021 	 * Note that the ipl does not have to be raised at this point.
   1022 	 * The only problem that could arise here is that if two different
   1023 	 * interfaces shared any data.  This is not the case.
   1024 	 */
   1025 	bp = (struct bpf_if *)arg;
   1026 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1027 		++d->bd_rcount;
   1028 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1029 		if (slen != 0)
   1030 			catchpacket(d, pkt, pktlen, slen, memcpy);
   1031 	}
   1032 }
   1033 
   1034 /*
   1035  * Copy data from an mbuf chain into a buffer.  This code is derived
   1036  * from m_copydata in sys/uipc_mbuf.c.
   1037  */
   1038 static void *
   1039 bpf_mcpy(dst_arg, src_arg, len)
   1040 	void *dst_arg;
   1041 	const void *src_arg;
   1042 	size_t len;
   1043 {
   1044 	const struct mbuf *m;
   1045 	u_int count;
   1046 	u_char *dst;
   1047 
   1048 	m = src_arg;
   1049 	dst = dst_arg;
   1050 	while (len > 0) {
   1051 		if (m == 0)
   1052 			panic("bpf_mcpy");
   1053 		count = min(m->m_len, len);
   1054 		memcpy((caddr_t)dst, mtod(m, caddr_t), count);
   1055 		m = m->m_next;
   1056 		dst += count;
   1057 		len -= count;
   1058 	}
   1059 	return(dst_arg);
   1060 }
   1061 
   1062 /*
   1063  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1064  */
   1065 void
   1066 bpf_mtap(arg, m)
   1067 	caddr_t arg;
   1068 	struct mbuf *m;
   1069 {
   1070 	struct bpf_if *bp = (struct bpf_if *)arg;
   1071 	struct bpf_d *d;
   1072 	u_int pktlen, slen;
   1073 	struct mbuf *m0;
   1074 
   1075 	pktlen = 0;
   1076 	for (m0 = m; m0 != 0; m0 = m0->m_next)
   1077 		pktlen += m0->m_len;
   1078 
   1079 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1080 		++d->bd_rcount;
   1081 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
   1082 		if (slen != 0)
   1083 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcpy);
   1084 	}
   1085 }
   1086 
   1087 /*
   1088  * Move the packet data from interface memory (pkt) into the
   1089  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1090  * otherwise 0.  "copy" is the routine called to do the actual data
   1091  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1092  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1093  * pkt is really an mbuf.
   1094  */
   1095 static void
   1096 catchpacket(d, pkt, pktlen, snaplen, cpfn)
   1097 	struct bpf_d *d;
   1098 	u_char *pkt;
   1099 	u_int pktlen, snaplen;
   1100 	void *(*cpfn) __P((void *, const void *, size_t));
   1101 {
   1102 	struct bpf_hdr *hp;
   1103 	int totlen, curlen;
   1104 	int hdrlen = d->bd_bif->bif_hdrlen;
   1105 	/*
   1106 	 * Figure out how many bytes to move.  If the packet is
   1107 	 * greater or equal to the snapshot length, transfer that
   1108 	 * much.  Otherwise, transfer the whole packet (unless
   1109 	 * we hit the buffer size limit).
   1110 	 */
   1111 	totlen = hdrlen + min(snaplen, pktlen);
   1112 	if (totlen > d->bd_bufsize)
   1113 		totlen = d->bd_bufsize;
   1114 
   1115 	/*
   1116 	 * Round up the end of the previous packet to the next longword.
   1117 	 */
   1118 	curlen = BPF_WORDALIGN(d->bd_slen);
   1119 	if (curlen + totlen > d->bd_bufsize) {
   1120 		/*
   1121 		 * This packet will overflow the storage buffer.
   1122 		 * Rotate the buffers if we can, then wakeup any
   1123 		 * pending reads.
   1124 		 */
   1125 		if (d->bd_fbuf == 0) {
   1126 			/*
   1127 			 * We haven't completed the previous read yet,
   1128 			 * so drop the packet.
   1129 			 */
   1130 			++d->bd_dcount;
   1131 			return;
   1132 		}
   1133 		ROTATE_BUFFERS(d);
   1134 		bpf_wakeup(d);
   1135 		curlen = 0;
   1136 	}
   1137 	else if (d->bd_immediate)
   1138 		/*
   1139 		 * Immediate mode is set.  A packet arrived so any
   1140 		 * reads should be woken up.
   1141 		 */
   1142 		bpf_wakeup(d);
   1143 
   1144 	/*
   1145 	 * Append the bpf header.
   1146 	 */
   1147 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1148 	microtime(&hp->bh_tstamp);
   1149 	hp->bh_datalen = pktlen;
   1150 	hp->bh_hdrlen = hdrlen;
   1151 	/*
   1152 	 * Copy the packet data into the store buffer and update its length.
   1153 	 */
   1154 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1155 	d->bd_slen = curlen + totlen;
   1156 }
   1157 
   1158 /*
   1159  * Initialize all nonzero fields of a descriptor.
   1160  */
   1161 static int
   1162 bpf_allocbufs(d)
   1163 	struct bpf_d *d;
   1164 {
   1165 
   1166 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1167 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1168 	d->bd_slen = 0;
   1169 	d->bd_hlen = 0;
   1170 	return (0);
   1171 }
   1172 
   1173 /*
   1174  * Free buffers currently in use by a descriptor.
   1175  * Called on close.
   1176  */
   1177 static void
   1178 bpf_freed(d)
   1179 	struct bpf_d *d;
   1180 {
   1181 	/*
   1182 	 * We don't need to lock out interrupts since this descriptor has
   1183 	 * been detached from its interface and it yet hasn't been marked
   1184 	 * free.
   1185 	 */
   1186 	if (d->bd_sbuf != 0) {
   1187 		free(d->bd_sbuf, M_DEVBUF);
   1188 		if (d->bd_hbuf != 0)
   1189 			free(d->bd_hbuf, M_DEVBUF);
   1190 		if (d->bd_fbuf != 0)
   1191 			free(d->bd_fbuf, M_DEVBUF);
   1192 	}
   1193 	if (d->bd_filter)
   1194 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1195 
   1196 	D_MARKFREE(d);
   1197 }
   1198 
   1199 /*
   1200  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
   1201  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
   1202  * size of the link header (variable length headers not yet supported).
   1203  */
   1204 void
   1205 bpfattach(driverp, ifp, dlt, hdrlen)
   1206 	caddr_t *driverp;
   1207 	struct ifnet *ifp;
   1208 	u_int dlt, hdrlen;
   1209 {
   1210 	struct bpf_if *bp;
   1211 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1212 	if (bp == 0)
   1213 		panic("bpfattach");
   1214 
   1215 	bp->bif_dlist = 0;
   1216 	bp->bif_driverp = (struct bpf_if **)driverp;
   1217 	bp->bif_ifp = ifp;
   1218 	bp->bif_dlt = dlt;
   1219 
   1220 	bp->bif_next = bpf_iflist;
   1221 	bpf_iflist = bp;
   1222 
   1223 	*bp->bif_driverp = 0;
   1224 
   1225 	/*
   1226 	 * Compute the length of the bpf header.  This is not necessarily
   1227 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1228 	 * that the network layer header begins on a longword boundary (for
   1229 	 * performance reasons and to alleviate alignment restrictions).
   1230 	 */
   1231 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1232 
   1233 #if 0
   1234 	printf("bpf: %s attached\n", ifp->if_xname);
   1235 #endif
   1236 }
   1237 
   1238 /*
   1239  * Remove an interface from bpf.
   1240  */
   1241 void
   1242 bpfdetach(ifp)
   1243 	struct ifnet *ifp;
   1244 {
   1245 	struct bpf_if *bp, **pbp;
   1246 	struct bpf_d *d;
   1247 	int i, s, cmaj;
   1248 
   1249 	/* locate the major number */
   1250 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
   1251 		if (cdevsw[cmaj].d_open == bpfopen)
   1252 			break;
   1253 
   1254 	/* Nuke the vnodes for any open instances */
   1255 	for (i = 0; i < NBPFILTER; ++i) {
   1256 		d = &bpf_dtab[i];
   1257 		if (!D_ISFREE(d) && d->bd_bif != NULL &&
   1258 		    d->bd_bif->bif_ifp == ifp) {
   1259 			/*
   1260 			 * Detach the descriptor from an interface now.
   1261 			 * It will be free'ed later by close routine.
   1262 			 */
   1263 			s = splimp();
   1264 			d->bd_promisc = 0;	/* we can't touch device. */
   1265 			bpf_detachd(d);
   1266 			splx(s);
   1267 			vdevgone(cmaj, i, i, VCHR);
   1268 		}
   1269 	}
   1270 
   1271 	for (bp = bpf_iflist, pbp = &bpf_iflist;
   1272 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
   1273 		if (bp->bif_ifp == ifp) {
   1274 			*pbp = bp->bif_next;
   1275 			free(bp, M_DEVBUF);
   1276 			break;
   1277 		}
   1278 	}
   1279 }
   1280 
   1281 /*
   1282  * Change the data link type of a BPF instance.
   1283  */
   1284 void
   1285 bpf_change_type(driverp, dlt, hdrlen)
   1286 	caddr_t *driverp;
   1287 	u_int dlt, hdrlen;
   1288 {
   1289 	struct bpf_if *bp;
   1290 
   1291 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1292 		if (bp->bif_driverp == (struct bpf_if **)driverp)
   1293 			break;
   1294 	}
   1295 	if (bp == NULL)
   1296 		panic("bpf_change_type");
   1297 
   1298 	bp->bif_dlt = dlt;
   1299 
   1300 	/*
   1301 	 * Compute the length of the bpf header.  This is not necessarily
   1302 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1303 	 * that the network layer header begins on a longword boundary (for
   1304 	 * performance reasons and to alleviate alignment restrictions).
   1305 	 */
   1306 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1307 }
   1308 
   1309 /* XXX This routine belongs in net/if.c. */
   1310 /*
   1311  * Set/clear promiscuous mode on interface ifp based on the truth value
   1312  * of pswitch.  The calls are reference counted so that only the first
   1313  * "on" request actually has an effect, as does the final "off" request.
   1314  * Results are undefined if the "off" and "on" requests are not matched.
   1315  */
   1316 int
   1317 ifpromisc(ifp, pswitch)
   1318 	struct ifnet *ifp;
   1319 	int pswitch;
   1320 {
   1321 	int pcount, ret;
   1322 	short flags;
   1323 	struct ifreq ifr;
   1324 
   1325 	pcount = ifp->if_pcount;
   1326 	flags = ifp->if_flags;
   1327 	if (pswitch) {
   1328 		/*
   1329 		 * If the device is not configured up, we cannot put it in
   1330 		 * promiscuous mode.
   1331 		 */
   1332 		if ((ifp->if_flags & IFF_UP) == 0)
   1333 			return (ENETDOWN);
   1334 		if (ifp->if_pcount++ != 0)
   1335 			return (0);
   1336 		ifp->if_flags |= IFF_PROMISC;
   1337 	} else {
   1338 		if (--ifp->if_pcount > 0)
   1339 			return (0);
   1340 		ifp->if_flags &= ~IFF_PROMISC;
   1341 		/*
   1342 		 * If the device is not configured up, we should not need to
   1343 		 * turn off promiscuous mode (device should have turned it
   1344 		 * off when interface went down; and will look at IFF_PROMISC
   1345 		 * again next time interface comes up).
   1346 		 */
   1347 		if ((ifp->if_flags & IFF_UP) == 0)
   1348 			return (0);
   1349 	}
   1350 	memset((caddr_t)&ifr, 0, sizeof(ifr));
   1351 	ifr.ifr_flags = ifp->if_flags;
   1352 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
   1353 	/* Restore interface state if not successful */
   1354 	if (ret != 0) {
   1355 		ifp->if_pcount = pcount;
   1356 		ifp->if_flags = flags;
   1357 	}
   1358 	return (ret);
   1359 }
   1360