in_var.h revision 148682
1139823Simp/*- 21541Srgrimes * Copyright (c) 1985, 1986, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 2910939Swollman * @(#)in_var.h 8.2 (Berkeley) 1/9/95 3050477Speter * $FreeBSD: head/sys/netinet/in_var.h 148682 2005-08-03 19:29:47Z rwatson $ 311541Srgrimes */ 321541Srgrimes 332169Spaul#ifndef _NETINET_IN_VAR_H_ 342169Spaul#define _NETINET_IN_VAR_H_ 352169Spaul 367280Swollman#include <sys/queue.h> 3784102Sjlemon#include <sys/fnv_hash.h> 387280Swollman 391541Srgrimes/* 401541Srgrimes * Interface address, Internet version. One of these structures 4121666Swollman * is allocated for each Internet address on an interface. 421541Srgrimes * The ifaddr structure contains the protocol-independent part 431541Srgrimes * of the structure and is assumed to be first. 441541Srgrimes */ 451541Srgrimesstruct in_ifaddr { 461541Srgrimes struct ifaddr ia_ifa; /* protocol-independent info */ 471541Srgrimes#define ia_ifp ia_ifa.ifa_ifp 481541Srgrimes#define ia_flags ia_ifa.ifa_flags 491541Srgrimes /* ia_{,sub}net{,mask} in host order */ 501541Srgrimes u_long ia_net; /* network number of interface */ 511541Srgrimes u_long ia_netmask; /* mask of net part */ 521541Srgrimes u_long ia_subnet; /* subnet number, including net */ 531541Srgrimes u_long ia_subnetmask; /* mask of subnet part */ 541541Srgrimes struct in_addr ia_netbroadcast; /* to recognize net broadcasts */ 5584102Sjlemon LIST_ENTRY(in_ifaddr) ia_hash; /* entry in bucket of inet addresses */ 5684102Sjlemon TAILQ_ENTRY(in_ifaddr) ia_link; /* list of internet addresses */ 571541Srgrimes struct sockaddr_in ia_addr; /* reserve space for interface name */ 581541Srgrimes struct sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */ 591541Srgrimes#define ia_broadaddr ia_dstaddr 601541Srgrimes struct sockaddr_in ia_sockmask; /* reserve space for general netmask */ 611541Srgrimes}; 621541Srgrimes 631541Srgrimesstruct in_aliasreq { 641541Srgrimes char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 651541Srgrimes struct sockaddr_in ifra_addr; 661541Srgrimes struct sockaddr_in ifra_broadaddr; 671541Srgrimes#define ifra_dstaddr ifra_broadaddr 681541Srgrimes struct sockaddr_in ifra_mask; 691541Srgrimes}; 701541Srgrimes/* 711541Srgrimes * Given a pointer to an in_ifaddr (ifaddr), 721541Srgrimes * return a pointer to the addr as a sockaddr_in. 731541Srgrimes */ 743865Sswallace#define IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr)) 753865Sswallace#define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr)) 761541Srgrimes 771541Srgrimes#define IN_LNAOF(in, ifa) \ 781541Srgrimes ((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask)) 791541Srgrimes 808876Srgrimes 8155205Speter#ifdef _KERNEL 827090Sbdeextern u_char inetctlerrmap[]; 831541Srgrimes 84133874Srwatson/* 8584102Sjlemon * Hash table for IP addresses. 8684102Sjlemon */ 8784102Sjlemonextern LIST_HEAD(in_ifaddrhashhead, in_ifaddr) *in_ifaddrhashtbl; 8884102Sjlemonextern TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead; 8984102Sjlemonextern u_long in_ifaddrhmask; /* mask for hash table */ 9084102Sjlemon 9184102Sjlemon#define INADDR_NHASH_LOG2 9 9284102Sjlemon#define INADDR_NHASH (1 << INADDR_NHASH_LOG2) 9384102Sjlemon#define INADDR_HASHVAL(x) fnv_32_buf((&(x)), sizeof(x), FNV1_32_INIT) 9484102Sjlemon#define INADDR_HASH(x) \ 9584102Sjlemon (&in_ifaddrhashtbl[INADDR_HASHVAL(x) & in_ifaddrhmask]) 9684102Sjlemon 9784102Sjlemon 981541Srgrimes/* 991541Srgrimes * Macro for finding the interface (ifnet structure) corresponding to one 1001541Srgrimes * of our IP addresses. 1011541Srgrimes */ 1021541Srgrimes#define INADDR_TO_IFP(addr, ifp) \ 1031541Srgrimes /* struct in_addr addr; */ \ 1041541Srgrimes /* struct ifnet *ifp; */ \ 1051541Srgrimes{ \ 10679821Sru struct in_ifaddr *ia; \ 1071541Srgrimes\ 10884109Sjlemon LIST_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) \ 10979821Sru if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \ 11079821Sru break; \ 1111541Srgrimes (ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \ 1121541Srgrimes} 1131541Srgrimes 1141541Srgrimes/* 1151541Srgrimes * Macro for finding the internet address structure (in_ifaddr) corresponding 1161541Srgrimes * to a given interface (ifnet structure). 1171541Srgrimes */ 1181541Srgrimes#define IFP_TO_IA(ifp, ia) \ 1191541Srgrimes /* struct ifnet *ifp; */ \ 1201541Srgrimes /* struct in_ifaddr *ia; */ \ 1211541Srgrimes{ \ 12271998Sphk for ((ia) = TAILQ_FIRST(&in_ifaddrhead); \ 1231541Srgrimes (ia) != NULL && (ia)->ia_ifp != (ifp); \ 12471998Sphk (ia) = TAILQ_NEXT((ia), ia_link)) \ 1251541Srgrimes continue; \ 1261541Srgrimes} 1271541Srgrimes#endif 1281541Srgrimes 1291541Srgrimes/* 1302531Swollman * This information should be part of the ifnet structure but we don't wish 1312531Swollman * to change that - as it might break a number of things 1322531Swollman */ 1332531Swollman 1342531Swollmanstruct router_info { 13514622Sfenner struct ifnet *rti_ifp; 13614622Sfenner int rti_type; /* type of router which is querier on this interface */ 13714622Sfenner int rti_time; /* # of slow timeouts since last old query */ 138119180Srwatson SLIST_ENTRY(router_info) rti_list; 1392531Swollman}; 1402531Swollman 1412531Swollman/* 1421541Srgrimes * Internet multicast address structure. There is one of these for each IP 1431541Srgrimes * multicast group to which this host belongs on a given network interface. 14421666Swollman * For every entry on the interface's if_multiaddrs list which represents 14521666Swollman * an IP multicast group, there is one of these structures. They are also 14621666Swollman * kept on a system-wide list to make it easier to keep our legacy IGMP code 14721666Swollman * compatible with the rest of the world (see IN_FIRST_MULTI et al, below). 1481541Srgrimes */ 1491541Srgrimesstruct in_multi { 15060938Sjake LIST_ENTRY(in_multi) inm_link; /* queue macro glue */ 15121666Swollman struct in_addr inm_addr; /* IP multicast address, convenience */ 1521541Srgrimes struct ifnet *inm_ifp; /* back pointer to ifnet */ 15321666Swollman struct ifmultiaddr *inm_ifma; /* back pointer to ifmultiaddr */ 1541541Srgrimes u_int inm_timer; /* IGMP membership report timer */ 1552531Swollman u_int inm_state; /* state of the membership */ 1562531Swollman struct router_info *inm_rti; /* router info*/ 1571541Srgrimes}; 1581541Srgrimes 15955205Speter#ifdef _KERNEL 16044078Sdfr 16144078Sdfr#ifdef SYSCTL_DECL 162136713SandreSYSCTL_DECL(_net_inet); 16344078SdfrSYSCTL_DECL(_net_inet_ip); 16444078SdfrSYSCTL_DECL(_net_inet_raw); 16544078Sdfr#endif 16644078Sdfr 16760938Sjakeextern LIST_HEAD(in_multihead, in_multi) in_multihead; 16821666Swollman 1691541Srgrimes/* 170148682Srwatson * Lock macros for IPv4 layer multicast address lists. IPv4 lock goes 171148682Srwatson * before link layer multicast locks in the lock order. In most cases, 172148682Srwatson * consumers of IN_*_MULTI() macros should acquire the locks before 173148682Srwatson * calling them; users of the in_{add,del}multi() functions should not. 174148682Srwatson */ 175148682Srwatsonextern struct mtx in_multi_mtx; 176148682Srwatson#define IN_MULTI_LOCK() mtx_lock(&in_multi_mtx) 177148682Srwatson#define IN_MULTI_UNLOCK() mtx_unlock(&in_multi_mtx) 178148682Srwatson#define IN_MULTI_LOCK_ASSERT() mtx_assert(&in_multi_mtx, MA_OWNED) 179148682Srwatson 180148682Srwatson/* 1811541Srgrimes * Structure used by macros below to remember position when stepping through 1821541Srgrimes * all of the in_multi records. 1831541Srgrimes */ 1841541Srgrimesstruct in_multistep { 1851541Srgrimes struct in_multi *i_inm; 1861541Srgrimes}; 1871541Srgrimes 1881541Srgrimes/* 1891541Srgrimes * Macro for looking up the in_multi record for a given IP multicast address 19021666Swollman * on a given interface. If no matching record is found, "inm" is set null. 1911541Srgrimes */ 1921541Srgrimes#define IN_LOOKUP_MULTI(addr, ifp, inm) \ 1931541Srgrimes /* struct in_addr addr; */ \ 1941541Srgrimes /* struct ifnet *ifp; */ \ 1951541Srgrimes /* struct in_multi *inm; */ \ 19620407Swollmando { \ 19779821Sru struct ifmultiaddr *ifma; \ 1981541Srgrimes\ 199148682Srwatson IN_MULTI_LOCK_ASSERT(); \ 200148653Srwatson IF_ADDR_LOCK(ifp); \ 20172084Sphk TAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) { \ 20221666Swollman if (ifma->ifma_addr->sa_family == AF_INET \ 20321929Swollman && ((struct sockaddr_in *)ifma->ifma_addr)->sin_addr.s_addr == \ 20421666Swollman (addr).s_addr) \ 20521666Swollman break; \ 20621666Swollman } \ 20721666Swollman (inm) = ifma ? ifma->ifma_protospec : 0; \ 208148653Srwatson IF_ADDR_UNLOCK(ifp); \ 20920407Swollman} while(0) 2101541Srgrimes 2111541Srgrimes/* 2121541Srgrimes * Macro to step through all of the in_multi records, one at a time. 2131541Srgrimes * The current position is remembered in "step", which the caller must 2141541Srgrimes * provide. IN_FIRST_MULTI(), below, must be called to initialize "step" 2151541Srgrimes * and get the first record. Both macros return a NULL "inm" when there 2161541Srgrimes * are no remaining records. 2171541Srgrimes */ 2181541Srgrimes#define IN_NEXT_MULTI(step, inm) \ 2191541Srgrimes /* struct in_multistep step; */ \ 2201541Srgrimes /* struct in_multi *inm; */ \ 22120407Swollmando { \ 222148682Srwatson IN_MULTI_LOCK_ASSERT(); \ 2231541Srgrimes if (((inm) = (step).i_inm) != NULL) \ 22471998Sphk (step).i_inm = LIST_NEXT((step).i_inm, inm_link); \ 22520407Swollman} while(0) 2261541Srgrimes 2271541Srgrimes#define IN_FIRST_MULTI(step, inm) \ 2281541Srgrimes /* struct in_multistep step; */ \ 2291541Srgrimes /* struct in_multi *inm; */ \ 23020407Swollmando { \ 231148682Srwatson IN_MULTI_LOCK_ASSERT(); \ 23271998Sphk (step).i_inm = LIST_FIRST(&in_multihead); \ 2331541Srgrimes IN_NEXT_MULTI((step), (inm)); \ 23420407Swollman} while(0) 2351541Srgrimes 23636192Sdgstruct route; 23792723Salfredstruct in_multi *in_addmulti(struct in_addr *, struct ifnet *); 23892723Salfredvoid in_delmulti(struct in_multi *); 23992723Salfredint in_control(struct socket *, u_long, caddr_t, struct ifnet *, 24093085Sbde struct thread *); 24192723Salfredvoid in_rtqdrain(void); 24292723Salfredvoid ip_input(struct mbuf *); 24392723Salfredint in_ifadown(struct ifaddr *ifa, int); 24492723Salfredvoid in_ifscrub(struct ifnet *, struct in_ifaddr *); 245122702Sandreint ip_fastforward(struct mbuf *); 2462169Spaul 24755205Speter#endif /* _KERNEL */ 24810939Swollman 24952904Sshin/* INET6 stuff */ 25052904Sshin#include <netinet6/in6_var.h> 25152904Sshin 25210939Swollman#endif /* _NETINET_IN_VAR_H_ */ 253