111820Sjulian/* 211820Sjulian * Copyright (c) 1983, 1993 311820Sjulian * The Regents of the University of California. All rights reserved. 411820Sjulian * 511820Sjulian * Copyright (c) 1995 John Hay. All rights reserved. 611820Sjulian * 711820Sjulian * Redistribution and use in source and binary forms, with or without 811820Sjulian * modification, are permitted provided that the following conditions 911820Sjulian * are met: 1011820Sjulian * 1. Redistributions of source code must retain the above copyright 1111820Sjulian * notice, this list of conditions and the following disclaimer. 1211820Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1311820Sjulian * notice, this list of conditions and the following disclaimer in the 1411820Sjulian * documentation and/or other materials provided with the distribution. 1511820Sjulian * 3. All advertising materials mentioning features or use of this software 1611820Sjulian * must display the following acknowledgement: 1711820Sjulian * This product includes software developed by the University of 1811820Sjulian * California, Berkeley and its contributors. 1911820Sjulian * 4. Neither the name of the University nor the names of its contributors 2011820Sjulian * may be used to endorse or promote products derived from this software 2111820Sjulian * without specific prior written permission. 2211820Sjulian * 2311820Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2411820Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2511820Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2611820Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2711820Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2811820Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2911820Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3011820Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3111820Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3211820Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3311820Sjulian * SUCH DAMAGE. 3411820Sjulian * 3511820Sjulian * static char sccsid[] = "@(#)if.c 5.1 (Berkeley) 6/4/85"; (routed/if.c) 3611820Sjulian * 3750479Speter * $FreeBSD$ 3811820Sjulian */ 3911820Sjulian 4011820Sjulian#ifndef lint 41122760Strhodesstatic const char sccsid[] = "@(#)if.c 8.1 (Berkeley) 6/5/93"; 4211820Sjulian#endif /* not lint */ 4311820Sjulian 4411820Sjulian/* 4511820Sjulian * Routing Table Management Daemon 4611820Sjulian */ 4711820Sjulian#include "defs.h" 4811820Sjulian 4911820Sjulianextern struct interface *ifnet; 5011820Sjulian 5111820Sjulian/* 5211820Sjulian * Find the interface with address addr. 5311820Sjulian */ 5411820Sjulianstruct interface * 5511820Sjulianif_ifwithaddr(addr) 5611820Sjulian struct sockaddr *addr; 5711820Sjulian{ 5811820Sjulian register struct interface *ifp; 5911820Sjulian 6011820Sjulian#define same(a1, a2) \ 6127244Sjhay (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 10) == 0) 6211820Sjulian for (ifp = ifnet; ifp; ifp = ifp->int_next) { 6311820Sjulian if (ifp->int_flags & IFF_REMOTE) 6411820Sjulian continue; 6511820Sjulian if (ifp->int_addr.sa_family != addr->sa_family) 6611820Sjulian continue; 6711820Sjulian if (same(&ifp->int_addr, addr)) 6811820Sjulian break; 6911820Sjulian if ((ifp->int_flags & IFF_BROADCAST) && 7011820Sjulian same(&ifp->int_broadaddr, addr)) 7111820Sjulian break; 7211820Sjulian } 7311820Sjulian return (ifp); 7411820Sjulian} 7511820Sjulian 7611820Sjulian/* 7711820Sjulian * Find the point-to-point interface with destination address addr. 7811820Sjulian */ 7911820Sjulianstruct interface * 8011820Sjulianif_ifwithdstaddr(addr) 8111820Sjulian struct sockaddr *addr; 8211820Sjulian{ 8311820Sjulian register struct interface *ifp; 8411820Sjulian 8511820Sjulian for (ifp = ifnet; ifp; ifp = ifp->int_next) { 8611820Sjulian if ((ifp->int_flags & IFF_POINTOPOINT) == 0) 8711820Sjulian continue; 8811820Sjulian if (same(&ifp->int_dstaddr, addr)) 8911820Sjulian break; 9011820Sjulian } 9111820Sjulian return (ifp); 9211820Sjulian} 9311820Sjulian 9411820Sjulian/* 9511820Sjulian * Find the interface on the network 9611820Sjulian * of the specified address. 9711820Sjulian */ 9811820Sjulianstruct interface * 9911820Sjulianif_ifwithnet(addr) 10011820Sjulian register struct sockaddr *addr; 10111820Sjulian{ 10211820Sjulian register struct interface *ifp; 10311820Sjulian register int af = addr->sa_family; 10411820Sjulian register int (*netmatch)(); 10511820Sjulian 10611820Sjulian if (af >= AF_MAX) 10711820Sjulian return (0); 10811820Sjulian netmatch = afswitch[af].af_netmatch; 10911820Sjulian for (ifp = ifnet; ifp; ifp = ifp->int_next) { 11011820Sjulian if (ifp->int_flags & IFF_REMOTE) 11111820Sjulian continue; 11211820Sjulian if (af != ifp->int_addr.sa_family) 11311820Sjulian continue; 11411820Sjulian if ((*netmatch)(addr, &ifp->int_addr)) 11511820Sjulian break; 11611820Sjulian } 11711820Sjulian return (ifp); 11811820Sjulian} 11911820Sjulian 12011820Sjulian/* 12111820Sjulian * Find an interface from which the specified address 12211820Sjulian * should have come from. Used for figuring out which 12311820Sjulian * interface a packet came in on -- for tracing. 12411820Sjulian */ 12511820Sjulianstruct interface * 12611820Sjulianif_iflookup(addr) 12711820Sjulian struct sockaddr *addr; 12811820Sjulian{ 12911820Sjulian register struct interface *ifp, *maybe; 13011820Sjulian register int af = addr->sa_family; 13111820Sjulian register int (*netmatch)(); 13211820Sjulian 13311820Sjulian if (af >= AF_MAX) 13411820Sjulian return (0); 13511820Sjulian maybe = 0; 13611820Sjulian netmatch = afswitch[af].af_netmatch; 13711820Sjulian for (ifp = ifnet; ifp; ifp = ifp->int_next) { 13811820Sjulian if (ifp->int_addr.sa_family != af) 13911820Sjulian continue; 14011820Sjulian if (same(&ifp->int_addr, addr)) 14111820Sjulian break; 14211820Sjulian if ((ifp->int_flags & IFF_BROADCAST) && 14311820Sjulian same(&ifp->int_broadaddr, addr)) 14411820Sjulian break; 14511820Sjulian if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr)) 14611820Sjulian maybe = ifp; 14711820Sjulian } 14811820Sjulian if (ifp == 0) 14911820Sjulian ifp = maybe; 15011820Sjulian return (ifp); 15111820Sjulian} 152