Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.30
      1 /*	$NetBSD: in.c,v 1.30 1996/09/06 05:07:43 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)in.c	8.2 (Berkeley) 11/15/93
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/errno.h>
     41 #include <sys/malloc.h>
     42 #include <sys/socket.h>
     43 #include <sys/socketvar.h>
     44 #include <sys/systm.h>
     45 #include <sys/proc.h>
     46 #include <sys/queue.h>
     47 
     48 #include <net/if.h>
     49 #include <net/route.h>
     50 
     51 #include <netinet/in_systm.h>
     52 #include <netinet/in.h>
     53 #include <netinet/ip.h>
     54 #include <netinet/in_var.h>
     55 #include <netinet/if_ether.h>
     56 #include <netinet/ip_mroute.h>
     57 #include <netinet/igmp_var.h>
     58 
     59 #include "ether.h"
     60 
     61 #ifdef INET
     62 
     63 #ifndef SUBNETSARELOCAL
     64 #define	SUBNETSARELOCAL	1
     65 #endif
     66 int subnetsarelocal = SUBNETSARELOCAL;
     67 
     68 #ifdef PACKET_FILTER
     69 LIST_HEAD(, packet_filter_hook) pfil_in_list;
     70 LIST_HEAD(, packet_filter_hook) pfil_out_list;
     71 LIST_HEAD(, packet_filter_hook) pfil_bad_list;
     72 static int done_pfil_init;
     73 #endif /* PACKET_FILTER */
     74 
     75 /*
     76  * Return 1 if an internet address is for a ``local'' host
     77  * (one to which we have a connection).  If subnetsarelocal
     78  * is true, this includes other subnets of the local net.
     79  * Otherwise, it includes only the directly-connected (sub)nets.
     80  */
     81 int
     82 in_localaddr(in)
     83 	struct in_addr in;
     84 {
     85 	register struct in_ifaddr *ia;
     86 
     87 	if (subnetsarelocal) {
     88 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
     89 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
     90 				return (1);
     91 	} else {
     92 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
     93 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
     94 				return (1);
     95 	}
     96 	return (0);
     97 }
     98 
     99 /*
    100  * Determine whether an IP address is in a reserved set of addresses
    101  * that may not be forwarded, or whether datagrams to that destination
    102  * may be forwarded.
    103  */
    104 int
    105 in_canforward(in)
    106 	struct in_addr in;
    107 {
    108 	register u_int32_t net;
    109 
    110 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    111 		return (0);
    112 	if (IN_CLASSA(in.s_addr)) {
    113 		net = in.s_addr & IN_CLASSA_NET;
    114 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    115 			return (0);
    116 	}
    117 	return (1);
    118 }
    119 
    120 /*
    121  * Trim a mask in a sockaddr
    122  */
    123 void
    124 in_socktrim(ap)
    125 	struct sockaddr_in *ap;
    126 {
    127 	register char *cplim = (char *) &ap->sin_addr;
    128 	register char *cp = (char *) (&ap->sin_addr + 1);
    129 
    130 	ap->sin_len = 0;
    131 	while (--cp >= cplim)
    132 		if (*cp) {
    133 			(ap)->sin_len = cp - (char *) (ap) + 1;
    134 			break;
    135 		}
    136 }
    137 
    138 int	in_interfaces;		/* number of external internet interfaces */
    139 
    140 /*
    141  * Generic internet control operations (ioctl's).
    142  * Ifp is 0 if not an interface-specific ioctl.
    143  */
    144 /* ARGSUSED */
    145 int
    146 in_control(so, cmd, data, ifp, p)
    147 	struct socket *so;
    148 	u_long cmd;
    149 	caddr_t data;
    150 	register struct ifnet *ifp;
    151 	struct proc *p;
    152 {
    153 	register struct ifreq *ifr = (struct ifreq *)data;
    154 	register struct in_ifaddr *ia = 0;
    155 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    156 	struct sockaddr_in oldaddr;
    157 	int error, hostIsNew, maskIsNew;
    158 
    159 	/*
    160 	 * Find address for this interface, if it exists.
    161 	 */
    162 	if (ifp)
    163 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    164 			if (ia->ia_ifp == ifp)
    165 				break;
    166 
    167 	switch (cmd) {
    168 
    169 	case SIOCAIFADDR:
    170 	case SIOCDIFADDR:
    171 		if (ifra->ifra_addr.sin_family == AF_INET)
    172 			for (; ia != 0; ia = ia->ia_list.tqe_next) {
    173 				if (ia->ia_ifp == ifp  &&
    174 				    SAME_INADDR(&ia->ia_addr, &ifra->ifra_addr))
    175 					break;
    176 			}
    177 		if (cmd == SIOCDIFADDR && ia == 0)
    178 			return (EADDRNOTAVAIL);
    179 		/* FALLTHROUGH */
    180 	case SIOCSIFADDR:
    181 	case SIOCSIFNETMASK:
    182 	case SIOCSIFDSTADDR:
    183 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    184 			return (EPERM);
    185 
    186 		if (ifp == 0)
    187 			panic("in_control");
    188 		if (ia == 0) {
    189 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
    190 			       M_IFADDR, M_WAITOK);
    191 			if (ia == 0)
    192 				return (ENOBUFS);
    193 			bzero((caddr_t)ia, sizeof *ia);
    194 			TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
    195 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
    196 			    ifa_list);
    197 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    198 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    199 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    200 			ia->ia_sockmask.sin_len = 8;
    201 			if (ifp->if_flags & IFF_BROADCAST) {
    202 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    203 				ia->ia_broadaddr.sin_family = AF_INET;
    204 			}
    205 			ia->ia_ifp = ifp;
    206 			LIST_INIT(&ia->ia_multiaddrs);
    207 			if ((ifp->if_flags & IFF_LOOPBACK) == 0)
    208 				in_interfaces++;
    209 		}
    210 		break;
    211 
    212 	case SIOCSIFBRDADDR:
    213 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    214 			return (EPERM);
    215 		/* FALLTHROUGH */
    216 
    217 	case SIOCGIFADDR:
    218 	case SIOCGIFNETMASK:
    219 	case SIOCGIFDSTADDR:
    220 	case SIOCGIFBRDADDR:
    221 		if (ia == 0)
    222 			return (EADDRNOTAVAIL);
    223 		break;
    224 	}
    225 	switch (cmd) {
    226 
    227 	case SIOCGIFADDR:
    228 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
    229 		break;
    230 
    231 	case SIOCGIFBRDADDR:
    232 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    233 			return (EINVAL);
    234 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
    235 		break;
    236 
    237 	case SIOCGIFDSTADDR:
    238 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    239 			return (EINVAL);
    240 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
    241 		break;
    242 
    243 	case SIOCGIFNETMASK:
    244 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
    245 		break;
    246 
    247 	case SIOCSIFDSTADDR:
    248 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    249 			return (EINVAL);
    250 		oldaddr = ia->ia_dstaddr;
    251 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
    252 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
    253 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    254 			ia->ia_dstaddr = oldaddr;
    255 			return (error);
    256 		}
    257 		if (ia->ia_flags & IFA_ROUTE) {
    258 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    259 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    260 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    261 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    262 		}
    263 		break;
    264 
    265 	case SIOCSIFBRDADDR:
    266 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    267 			return (EINVAL);
    268 		ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
    269 		break;
    270 
    271 	case SIOCSIFADDR:
    272 		return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
    273 
    274 	case SIOCSIFNETMASK:
    275 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
    276 		    ifra->ifra_addr.sin_addr.s_addr;
    277 		break;
    278 
    279 	case SIOCAIFADDR:
    280 		maskIsNew = 0;
    281 		hostIsNew = 1;
    282 		error = 0;
    283 		if (ia->ia_addr.sin_family == AF_INET) {
    284 			if (ifra->ifra_addr.sin_len == 0) {
    285 				ifra->ifra_addr = ia->ia_addr;
    286 				hostIsNew = 0;
    287 			} else if (SAME_INADDR(&ia->ia_addr, &ifra->ifra_addr))
    288 				hostIsNew = 0;
    289 		}
    290 		if (ifra->ifra_mask.sin_len) {
    291 			in_ifscrub(ifp, ia);
    292 			ia->ia_sockmask = ifra->ifra_mask;
    293 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    294 			maskIsNew = 1;
    295 		}
    296 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    297 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    298 			in_ifscrub(ifp, ia);
    299 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    300 			maskIsNew  = 1; /* We lie; but the effect's the same */
    301 		}
    302 		if (ifra->ifra_addr.sin_family == AF_INET &&
    303 		    (hostIsNew || maskIsNew))
    304 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    305 		if ((ifp->if_flags & IFF_BROADCAST) &&
    306 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    307 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    308 		return (error);
    309 
    310 	case SIOCDIFADDR:
    311 		in_ifscrub(ifp, ia);
    312 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    313 		TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    314 		IFAFREE((&ia->ia_ifa));
    315 		break;
    316 
    317 #ifdef MROUTING
    318 	case SIOCGETVIFCNT:
    319 	case SIOCGETSGCNT:
    320 		return (mrt_ioctl(so, cmd, data));
    321 #endif /* MROUTING */
    322 
    323 	default:
    324 		if (ifp == 0 || ifp->if_ioctl == 0)
    325 			return (EOPNOTSUPP);
    326 		return ((*ifp->if_ioctl)(ifp, cmd, data));
    327 	}
    328 	return (0);
    329 }
    330 
    331 /*
    332  * Delete any existing route for an interface.
    333  */
    334 void
    335 in_ifscrub(ifp, ia)
    336 	register struct ifnet *ifp;
    337 	register struct in_ifaddr *ia;
    338 {
    339 
    340 	if ((ia->ia_flags & IFA_ROUTE) == 0)
    341 		return;
    342 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
    343 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    344 	else
    345 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
    346 	ia->ia_flags &= ~IFA_ROUTE;
    347 }
    348 
    349 /*
    350  * Initialize an interface's internet address
    351  * and routing table entry.
    352  */
    353 int
    354 in_ifinit(ifp, ia, sin, scrub)
    355 	register struct ifnet *ifp;
    356 	register struct in_ifaddr *ia;
    357 	struct sockaddr_in *sin;
    358 	int scrub;
    359 {
    360 	register u_int32_t i = sin->sin_addr.s_addr;
    361 	struct sockaddr_in oldaddr;
    362 	int s = splimp(), flags = RTF_UP, error;
    363 
    364 	oldaddr = ia->ia_addr;
    365 	ia->ia_addr = *sin;
    366 	/*
    367 	 * Give the interface a chance to initialize
    368 	 * if this is its first address,
    369 	 * and to validate the address if necessary.
    370 	 */
    371 	if (ifp->if_ioctl &&
    372 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
    373 		splx(s);
    374 		ia->ia_addr = oldaddr;
    375 		return (error);
    376 	}
    377 	splx(s);
    378 	if (scrub) {
    379 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    380 		in_ifscrub(ifp, ia);
    381 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    382 	}
    383 	if (IN_CLASSA(i))
    384 		ia->ia_netmask = IN_CLASSA_NET;
    385 	else if (IN_CLASSB(i))
    386 		ia->ia_netmask = IN_CLASSB_NET;
    387 	else
    388 		ia->ia_netmask = IN_CLASSC_NET;
    389 	/*
    390 	 * The subnet mask usually includes at least the standard network part,
    391 	 * but may may be smaller in the case of supernetting.
    392 	 * If it is set, we believe it.
    393 	 */
    394 	if (ia->ia_subnetmask == 0) {
    395 		ia->ia_subnetmask = ia->ia_netmask;
    396 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    397 	} else
    398 		ia->ia_netmask &= ia->ia_subnetmask;
    399 	ia->ia_net = i & ia->ia_netmask;
    400 	ia->ia_subnet = i & ia->ia_subnetmask;
    401 	in_socktrim(&ia->ia_sockmask);
    402 	/*
    403 	 * Add route for the network.
    404 	 */
    405 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    406 	if (ifp->if_flags & IFF_BROADCAST) {
    407 		ia->ia_broadaddr.sin_addr.s_addr =
    408 			ia->ia_subnet | ~ia->ia_subnetmask;
    409 		ia->ia_netbroadcast.s_addr =
    410 			ia->ia_net | ~ia->ia_netmask;
    411 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    412 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    413 		flags |= RTF_HOST;
    414 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    415 		if (ia->ia_dstaddr.sin_family != AF_INET)
    416 			return (0);
    417 		flags |= RTF_HOST;
    418 	}
    419 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
    420 		ia->ia_flags |= IFA_ROUTE;
    421 	/*
    422 	 * If the interface supports multicast, join the "all hosts"
    423 	 * multicast group on that interface.
    424 	 */
    425 	if (ifp->if_flags & IFF_MULTICAST) {
    426 		struct in_addr addr;
    427 
    428 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    429 		in_addmulti(&addr, ifp);
    430 	}
    431 	return (error);
    432 }
    433 
    434 
    435 /*
    436  * Return 1 if the address might be a local broadcast address.
    437  */
    438 int
    439 in_broadcast(in, ifp)
    440 	struct in_addr in;
    441 	struct ifnet *ifp;
    442 {
    443 	register struct ifaddr *ifa;
    444 
    445 	if (in.s_addr == INADDR_BROADCAST ||
    446 	    in.s_addr == INADDR_ANY)
    447 		return 1;
    448 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
    449 		return 0;
    450 	/*
    451 	 * Look through the list of addresses for a match
    452 	 * with a broadcast address.
    453 	 */
    454 #define ia (ifatoia(ifa))
    455 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    456 		if (ifa->ifa_addr->sa_family == AF_INET &&
    457 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
    458 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
    459 		     /*
    460 		      * Check for old-style (host 0) broadcast.
    461 		      */
    462 		     in.s_addr == ia->ia_subnet ||
    463 		     in.s_addr == ia->ia_net))
    464 			    return 1;
    465 	return (0);
    466 #undef ia
    467 }
    468 
    469 /*
    470  * Add an address to the list of IP multicast addresses for a given interface.
    471  */
    472 struct in_multi *
    473 in_addmulti(ap, ifp)
    474 	register struct in_addr *ap;
    475 	register struct ifnet *ifp;
    476 {
    477 	register struct in_multi *inm;
    478 	struct ifreq ifr;
    479 	struct in_ifaddr *ia;
    480 	int s = splsoftnet();
    481 
    482 	/*
    483 	 * See if address already in list.
    484 	 */
    485 	IN_LOOKUP_MULTI(*ap, ifp, inm);
    486 	if (inm != NULL) {
    487 		/*
    488 		 * Found it; just increment the reference count.
    489 		 */
    490 		++inm->inm_refcount;
    491 	} else {
    492 		/*
    493 		 * New address; allocate a new multicast record
    494 		 * and link it into the interface's multicast list.
    495 		 */
    496 		inm = (struct in_multi *)malloc(sizeof(*inm),
    497 		    M_IPMADDR, M_NOWAIT);
    498 		if (inm == NULL) {
    499 			splx(s);
    500 			return (NULL);
    501 		}
    502 		inm->inm_addr = *ap;
    503 		inm->inm_ifp = ifp;
    504 		inm->inm_refcount = 1;
    505 		IFP_TO_IA(ifp, ia);
    506 		if (ia == NULL) {
    507 			free(inm, M_IPMADDR);
    508 			splx(s);
    509 			return (NULL);
    510 		}
    511 		inm->inm_ia = ia;
    512 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
    513 		/*
    514 		 * Ask the network driver to update its multicast reception
    515 		 * filter appropriately for the new address.
    516 		 */
    517 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    518 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    519 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
    520 		if ((ifp->if_ioctl == NULL) ||
    521 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
    522 			LIST_REMOVE(inm, inm_list);
    523 			free(inm, M_IPMADDR);
    524 			splx(s);
    525 			return (NULL);
    526 		}
    527 		/*
    528 		 * Let IGMP know that we have joined a new IP multicast group.
    529 		 */
    530 		igmp_joingroup(inm);
    531 	}
    532 	splx(s);
    533 	return (inm);
    534 }
    535 
    536 /*
    537  * Delete a multicast address record.
    538  */
    539 void
    540 in_delmulti(inm)
    541 	register struct in_multi *inm;
    542 {
    543 	struct ifreq ifr;
    544 	int s = splsoftnet();
    545 
    546 	if (--inm->inm_refcount == 0) {
    547 		/*
    548 		 * No remaining claims to this record; let IGMP know that
    549 		 * we are leaving the multicast group.
    550 		 */
    551 		igmp_leavegroup(inm);
    552 		/*
    553 		 * Unlink from list.
    554 		 */
    555 		LIST_REMOVE(inm, inm_list);
    556 		/*
    557 		 * Notify the network driver to update its multicast reception
    558 		 * filter.
    559 		 */
    560 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    561 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
    562 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
    563 							     (caddr_t)&ifr);
    564 		free(inm, M_IPMADDR);
    565 	}
    566 	splx(s);
    567 }
    568 #endif
    569 
    570 #ifdef PACKET_FILTER
    571 void pfil_init __P((void));
    572 int pfil_list_remove(struct packet_filter_hook *,
    573     int (*) __P((void *, int, struct ifnet *, int, struct mbuf **)), int,
    574 		 int);
    575 
    576 void
    577 pfil_init()
    578 {
    579 	LIST_INIT(&pfil_in_list);
    580 	LIST_INIT(&pfil_out_list);
    581 	LIST_INIT(&pfil_bad_list);
    582 	done_pfil_init = 1;
    583 }
    584 
    585 /*
    586  * pfil_add_hook() adds a function to the packet filter hook.  the
    587  * flags are:
    588  *	PFIL_IN		call me on incoming packets
    589  *	PFIL_OUT	call me on outgoing packets
    590  *	PFIL_BAD	call me when rejecting a packet (that was
    591  *			not already reject by in/out filters).
    592  *	PFIL_ALL	call me on all of the above
    593  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
    594  */
    595 void
    596 pfil_add_hook(func, flags)
    597 	int	(*func) __P((void *, int, struct ifnet *, int,
    598 			     struct mbuf **));
    599 	int	flags;
    600 {
    601 	struct packet_filter_hook *pfh;
    602 
    603 	if (done_pfil_init == 0)
    604 		pfil_init();
    605 
    606 	pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
    607 	    flags & PFIL_WAITOK ? M_WAITOK : M_NOWAIT);
    608 	if (pfh == NULL)
    609 		panic("no memory for packet filter hook");
    610 
    611 	pfh->pfil_flags = flags;
    612 	pfh->pfil_func = func;
    613 	if (flags & PFIL_IN)
    614 		LIST_INSERT_HEAD(&pfil_in_list, pfh, pfil_link);
    615 	if (flags & PFIL_OUT)
    616 		LIST_INSERT_HEAD(&pfil_out_list, pfh, pfil_link);
    617 	if (flags & PFIL_BAD)
    618 		LIST_INSERT_HEAD(&pfil_bad_list, pfh, pfil_link);
    619 }
    620 
    621 /*
    622  * pfil_remove_hook removes a specific function from the packet filter
    623  * hook list.
    624  */
    625 void
    626 pfil_remove_hook(func, flags)
    627 	int	(*func) __P((void *, int, struct ifnet *, int,
    628 			     struct mbuf **));
    629 	int	flags;
    630 {
    631 
    632 	if (done_pfil_init == 0)
    633 		pfil_init();
    634 
    635 	if (flags & PFIL_IN &&
    636 	    pfil_list_remove(pfil_in_list.lh_first, func, flags, PFIL_IN))
    637 		return;
    638 	if (flags & PFIL_OUT &&
    639 	    pfil_list_remove(pfil_out_list.lh_first, func, flags, PFIL_OUT))
    640 		return;
    641 	if (flags & PFIL_BAD &&
    642 	    pfil_list_remove(pfil_bad_list.lh_first, func, flags, PFIL_BAD))
    643 		return;
    644 }
    645 
    646 int
    647 pfil_list_remove(list, func, flags, flag)
    648 	struct packet_filter_hook *list;
    649 	int	(*func) __P((void *, int, struct ifnet *, int,
    650 			     struct mbuf **));
    651 	int	flags, flag;
    652 {
    653 	struct packet_filter_hook *pfh;
    654 
    655 	for (pfh = list; pfh; pfh = pfh->pfil_link.le_next)
    656 		if (pfh->pfil_func == func) {
    657 			pfh->pfil_flags &= ~flag;
    658 			LIST_REMOVE(pfh, pfil_link);
    659 			if ((flags & PFIL_ALL) == 0) {
    660 				free(pfh, M_IFADDR);
    661 				return 1;
    662 			}
    663 		}
    664 	return 0;
    665 }
    666 
    667 struct packet_filter_hook *
    668 pfil_hook_get(flag)
    669 	int flag;
    670 {
    671 	if (done_pfil_init)
    672 		switch (flag) {
    673 		case PFIL_IN:
    674 			return (pfil_in_list.lh_first);
    675 		case PFIL_OUT:
    676 			return (pfil_out_list.lh_first);
    677 		case PFIL_BAD:
    678 			return (pfil_bad_list.lh_first);
    679 		}
    680 	return NULL;
    681 }
    682 #endif /* PACKET_FILTER */
    683