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: stable/11/sys/netinet/in_var.h 309337 2016-11-30 21:53:06Z vangyzen $ 311541Srgrimes */ 321541Srgrimes 332169Spaul#ifndef _NETINET_IN_VAR_H_ 342169Spaul#define _NETINET_IN_VAR_H_ 352169Spaul 36279031Sglebius/* 37279031Sglebius * Argument structure for SIOCAIFADDR. 38279031Sglebius */ 39279031Sglebiusstruct in_aliasreq { 40279031Sglebius char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 41279031Sglebius struct sockaddr_in ifra_addr; 42279031Sglebius struct sockaddr_in ifra_broadaddr; 43279031Sglebius#define ifra_dstaddr ifra_broadaddr 44279031Sglebius struct sockaddr_in ifra_mask; 45279031Sglebius int ifra_vhid; 46279031Sglebius}; 47279031Sglebius 48279031Sglebius#ifdef _KERNEL 497280Swollman#include <sys/queue.h> 5084102Sjlemon#include <sys/fnv_hash.h> 51189592Sbms#include <sys/tree.h> 527280Swollman 53279026Sglebiusstruct igmp_ifsoftc; 54189592Sbmsstruct in_multi; 55189592Sbmsstruct lltable; 56189592Sbms 571541Srgrimes/* 58189592Sbms * IPv4 per-interface state. 59189592Sbms */ 60189592Sbmsstruct in_ifinfo { 61189592Sbms struct lltable *ii_llt; /* ARP state */ 62279026Sglebius struct igmp_ifsoftc *ii_igmp; /* IGMP state */ 63189592Sbms struct in_multi *ii_allhosts; /* 224.0.0.1 membership */ 64189592Sbms}; 65189592Sbms 66189592Sbms/* 671541Srgrimes * Interface address, Internet version. One of these structures 6821666Swollman * is allocated for each Internet address on an interface. 691541Srgrimes * The ifaddr structure contains the protocol-independent part 701541Srgrimes * of the structure and is assumed to be first. 711541Srgrimes */ 721541Srgrimesstruct in_ifaddr { 731541Srgrimes struct ifaddr ia_ifa; /* protocol-independent info */ 741541Srgrimes#define ia_ifp ia_ifa.ifa_ifp 751541Srgrimes#define ia_flags ia_ifa.ifa_flags 76226401Sglebius /* ia_subnet{,mask} in host order */ 77226401Sglebius u_long ia_subnet; /* subnet address */ 78226401Sglebius u_long ia_subnetmask; /* mask of subnet */ 7984102Sjlemon LIST_ENTRY(in_ifaddr) ia_hash; /* entry in bucket of inet addresses */ 8084102Sjlemon TAILQ_ENTRY(in_ifaddr) ia_link; /* list of internet addresses */ 811541Srgrimes struct sockaddr_in ia_addr; /* reserve space for interface name */ 821541Srgrimes struct sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */ 831541Srgrimes#define ia_broadaddr ia_dstaddr 841541Srgrimes struct sockaddr_in ia_sockmask; /* reserve space for general netmask */ 85309337Svangyzen struct callout ia_garp_timer; /* timer for retransmitting GARPs */ 86309337Svangyzen int ia_garp_count; /* count of retransmitted GARPs */ 871541Srgrimes}; 881541Srgrimes 891541Srgrimes/* 901541Srgrimes * Given a pointer to an in_ifaddr (ifaddr), 911541Srgrimes * return a pointer to the addr as a sockaddr_in. 921541Srgrimes */ 933865Sswallace#define IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr)) 943865Sswallace#define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr)) 95229815Sglebius#define IA_MASKSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_sockmask)) 961541Srgrimes 971541Srgrimes#define IN_LNAOF(in, ifa) \ 981541Srgrimes ((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask)) 991541Srgrimes 1007090Sbdeextern u_char inetctlerrmap[]; 1011541Srgrimes 102191120Skmacy#define LLTABLE(ifp) \ 103191120Skmacy ((struct in_ifinfo *)(ifp)->if_afdata[AF_INET])->ii_llt 104133874Srwatson/* 10584102Sjlemon * Hash table for IP addresses. 10684102Sjlemon */ 107186048SbzTAILQ_HEAD(in_ifaddrhead, in_ifaddr); 108186048SbzLIST_HEAD(in_ifaddrhashhead, in_ifaddr); 10984102Sjlemon 110195699SrwatsonVNET_DECLARE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); 111195699SrwatsonVNET_DECLARE(struct in_ifaddrhead, in_ifaddrhead); 112195699SrwatsonVNET_DECLARE(u_long, in_ifaddrhmask); /* mask for hash table */ 113195699Srwatson 114195727Srwatson#define V_in_ifaddrhashtbl VNET(in_ifaddrhashtbl) 115195727Srwatson#define V_in_ifaddrhead VNET(in_ifaddrhead) 116195727Srwatson#define V_in_ifaddrhmask VNET(in_ifaddrhmask) 117195699Srwatson 11884102Sjlemon#define INADDR_NHASH_LOG2 9 11984102Sjlemon#define INADDR_NHASH (1 << INADDR_NHASH_LOG2) 12084102Sjlemon#define INADDR_HASHVAL(x) fnv_32_buf((&(x)), sizeof(x), FNV1_32_INIT) 12184102Sjlemon#define INADDR_HASH(x) \ 122181803Sbz (&V_in_ifaddrhashtbl[INADDR_HASHVAL(x) & V_in_ifaddrhmask]) 12384102Sjlemon 124286001Saeextern struct rmlock in_ifaddr_lock; 125194951Srwatson 126286001Sae#define IN_IFADDR_LOCK_ASSERT() rm_assert(&in_ifaddr_lock, RA_LOCKED) 127286001Sae#define IN_IFADDR_RLOCK(t) rm_rlock(&in_ifaddr_lock, (t)) 128286001Sae#define IN_IFADDR_RLOCK_ASSERT() rm_assert(&in_ifaddr_lock, RA_RLOCKED) 129286001Sae#define IN_IFADDR_RUNLOCK(t) rm_runlock(&in_ifaddr_lock, (t)) 130286001Sae#define IN_IFADDR_WLOCK() rm_wlock(&in_ifaddr_lock) 131286001Sae#define IN_IFADDR_WLOCK_ASSERT() rm_assert(&in_ifaddr_lock, RA_WLOCKED) 132286001Sae#define IN_IFADDR_WUNLOCK() rm_wunlock(&in_ifaddr_lock) 133194951Srwatson 134162615Sbms/* 135162625Sbms * Macro for finding the internet address structure (in_ifaddr) 136162615Sbms * corresponding to one of our IP addresses (in_addr). 137162615Sbms */ 138162615Sbms#define INADDR_TO_IFADDR(addr, ia) \ 139162615Sbms /* struct in_addr addr; */ \ 140162615Sbms /* struct in_ifaddr *ia; */ \ 141162615Sbmsdo { \ 142162615Sbms\ 143162615Sbms LIST_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) \ 144162615Sbms if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \ 145162615Sbms break; \ 146162615Sbms} while (0) 14784102Sjlemon 1481541Srgrimes/* 1491541Srgrimes * Macro for finding the interface (ifnet structure) corresponding to one 1501541Srgrimes * of our IP addresses. 1511541Srgrimes */ 1521541Srgrimes#define INADDR_TO_IFP(addr, ifp) \ 1531541Srgrimes /* struct in_addr addr; */ \ 1541541Srgrimes /* struct ifnet *ifp; */ \ 1551541Srgrimes{ \ 15679821Sru struct in_ifaddr *ia; \ 1571541Srgrimes\ 158162615Sbms INADDR_TO_IFADDR(addr, ia); \ 1591541Srgrimes (ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \ 1601541Srgrimes} 1611541Srgrimes 1621541Srgrimes/* 1631541Srgrimes * Macro for finding the internet address structure (in_ifaddr) corresponding 1641541Srgrimes * to a given interface (ifnet structure). 1651541Srgrimes */ 166286001Sae#define IFP_TO_IA(ifp, ia, t) \ 167194760Srwatson /* struct ifnet *ifp; */ \ 168194760Srwatson /* struct in_ifaddr *ia; */ \ 169286001Sae /* struct rm_priotracker *t; */ \ 170238572Sglebiusdo { \ 171286001Sae IN_IFADDR_RLOCK((t)); \ 172194760Srwatson for ((ia) = TAILQ_FIRST(&V_in_ifaddrhead); \ 173194760Srwatson (ia) != NULL && (ia)->ia_ifp != (ifp); \ 174194760Srwatson (ia) = TAILQ_NEXT((ia), ia_link)) \ 175194760Srwatson continue; \ 176194760Srwatson if ((ia) != NULL) \ 177194760Srwatson ifa_ref(&(ia)->ia_ifa); \ 178286001Sae IN_IFADDR_RUNLOCK((t)); \ 179238572Sglebius} while (0) 1801541Srgrimes 1811541Srgrimes/* 182189592Sbms * Legacy IPv4 IGMP per-link structure. 1832531Swollman */ 1842531Swollmanstruct router_info { 18514622Sfenner struct ifnet *rti_ifp; 18614622Sfenner int rti_type; /* type of router which is querier on this interface */ 18714622Sfenner int rti_time; /* # of slow timeouts since last old query */ 188119180Srwatson SLIST_ENTRY(router_info) rti_list; 1892531Swollman}; 1902531Swollman 1912531Swollman/* 192189592Sbms * IPv4 multicast IGMP-layer source entry. 193189592Sbms */ 194189592Sbmsstruct ip_msource { 195189592Sbms RB_ENTRY(ip_msource) ims_link; /* RB tree links */ 196189592Sbms in_addr_t ims_haddr; /* host byte order */ 197189592Sbms struct ims_st { 198189592Sbms uint16_t ex; /* # of exclusive members */ 199189592Sbms uint16_t in; /* # of inclusive members */ 200189592Sbms } ims_st[2]; /* state at t0, t1 */ 201189592Sbms uint8_t ims_stp; /* pending query */ 202189592Sbms}; 203189592Sbms 204189592Sbms/* 205189592Sbms * IPv4 multicast PCB-layer source entry. 206189592Sbms */ 207189592Sbmsstruct in_msource { 208189592Sbms RB_ENTRY(ip_msource) ims_link; /* RB tree links */ 209189592Sbms in_addr_t ims_haddr; /* host byte order */ 210189592Sbms uint8_t imsl_st[2]; /* state before/at commit */ 211189592Sbms}; 212189592Sbms 213189592SbmsRB_HEAD(ip_msource_tree, ip_msource); /* define struct ip_msource_tree */ 214189592Sbms 215189592Sbmsstatic __inline int 216189592Sbmsip_msource_cmp(const struct ip_msource *a, const struct ip_msource *b) 217189592Sbms{ 218189592Sbms 219189592Sbms if (a->ims_haddr < b->ims_haddr) 220189592Sbms return (-1); 221189592Sbms if (a->ims_haddr == b->ims_haddr) 222189592Sbms return (0); 223189592Sbms return (1); 224189592Sbms} 225189592SbmsRB_PROTOTYPE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp); 226189592Sbms 227189592Sbms/* 228189592Sbms * IPv4 multicast PCB-layer group filter descriptor. 229189592Sbms */ 230189592Sbmsstruct in_mfilter { 231189592Sbms struct ip_msource_tree imf_sources; /* source list for (S,G) */ 232189592Sbms u_long imf_nsrc; /* # of source entries */ 233189592Sbms uint8_t imf_st[2]; /* state before/at commit */ 234189592Sbms}; 235189592Sbms 236189592Sbms/* 237189592Sbms * IPv4 group descriptor. 238189592Sbms * 239189592Sbms * For every entry on an ifnet's if_multiaddrs list which represents 240189592Sbms * an IP multicast group, there is one of these structures. 241189592Sbms * 242189592Sbms * If any source filters are present, then a node will exist in the RB-tree 243189592Sbms * to permit fast lookup by source whenever an operation takes place. 244189592Sbms * This permits pre-order traversal when we issue reports. 245189592Sbms * Source filter trees are kept separately from the socket layer to 246189592Sbms * greatly simplify locking. 247189592Sbms * 248189592Sbms * When IGMPv3 is active, inm_timer is the response to group query timer. 249189592Sbms * The state-change timer inm_sctimer is separate; whenever state changes 250189592Sbms * for the group the state change record is generated and transmitted, 251189592Sbms * and kept if retransmissions are necessary. 252189592Sbms * 253189592Sbms * FUTURE: inm_link is now only used when groups are being purged 254189592Sbms * on a detaching ifnet. It could be demoted to a SLIST_ENTRY, but 255189592Sbms * because it is at the very start of the struct, we can't do this 256189592Sbms * w/o breaking the ABI for ifmcstat. 257189592Sbms */ 2581541Srgrimesstruct in_multi { 259189592Sbms LIST_ENTRY(in_multi) inm_link; /* to-be-released by in_ifdetach */ 26021666Swollman struct in_addr inm_addr; /* IP multicast address, convenience */ 2611541Srgrimes struct ifnet *inm_ifp; /* back pointer to ifnet */ 26221666Swollman struct ifmultiaddr *inm_ifma; /* back pointer to ifmultiaddr */ 263189592Sbms u_int inm_timer; /* IGMPv1/v2 group / v3 query timer */ 264189592Sbms u_int inm_state; /* state of the membership */ 265189592Sbms void *inm_rti; /* unused, legacy field */ 266167729Sbms u_int inm_refcount; /* reference count */ 267189592Sbms 268189592Sbms /* New fields for IGMPv3 follow. */ 269279026Sglebius struct igmp_ifsoftc *inm_igi; /* IGMP info */ 270189592Sbms SLIST_ENTRY(in_multi) inm_nrele; /* to-be-released by IGMP */ 271189592Sbms struct ip_msource_tree inm_srcs; /* tree of sources */ 272189592Sbms u_long inm_nsrc; /* # of tree entries */ 273189592Sbms 274278978Sglebius struct mbufq inm_scq; /* queue of pending 275189592Sbms * state-change packets */ 276189592Sbms struct timeval inm_lastgsrtv; /* Time of last G-S-R query */ 277189592Sbms uint16_t inm_sctimer; /* state-change timer */ 278189592Sbms uint16_t inm_scrv; /* state-change rexmit count */ 279189592Sbms 280189592Sbms /* 281189592Sbms * SSM state counters which track state at T0 (the time the last 282189592Sbms * state-change report's RV timer went to zero) and T1 283189592Sbms * (time of pending report, i.e. now). 284189592Sbms * Used for computing IGMPv3 state-change reports. Several refcounts 285189592Sbms * are maintained here to optimize for common use-cases. 286189592Sbms */ 287189592Sbms struct inm_st { 288189592Sbms uint16_t iss_fmode; /* IGMP filter mode */ 289189592Sbms uint16_t iss_asm; /* # of ASM listeners */ 290189592Sbms uint16_t iss_ex; /* # of exclusive members */ 291189592Sbms uint16_t iss_in; /* # of inclusive members */ 292189592Sbms uint16_t iss_rec; /* # of recorded sources */ 293189592Sbms } inm_st[2]; /* state at t0, t1 */ 2941541Srgrimes}; 2951541Srgrimes 296170613Sbms/* 297189592Sbms * Helper function to derive the filter mode on a source entry 298189592Sbms * from its internal counters. Predicates are: 299189592Sbms * A source is only excluded if all listeners exclude it. 300189592Sbms * A source is only included if no listeners exclude it, 301189592Sbms * and at least one listener includes it. 302189592Sbms * May be used by ifmcstat(8). 303170613Sbms */ 304189592Sbmsstatic __inline uint8_t 305189592Sbmsims_get_mode(const struct in_multi *inm, const struct ip_msource *ims, 306189592Sbms uint8_t t) 307189592Sbms{ 308170613Sbms 309189592Sbms t = !!t; 310189592Sbms if (inm->inm_st[t].iss_ex > 0 && 311189592Sbms inm->inm_st[t].iss_ex == ims->ims_st[t].ex) 312189592Sbms return (MCAST_EXCLUDE); 313189592Sbms else if (ims->ims_st[t].in > 0 && ims->ims_st[t].ex == 0) 314189592Sbms return (MCAST_INCLUDE); 315189592Sbms return (MCAST_UNDEFINED); 316189592Sbms} 317189592Sbms 31844078Sdfr#ifdef SYSCTL_DECL 319136713SandreSYSCTL_DECL(_net_inet); 32044078SdfrSYSCTL_DECL(_net_inet_ip); 32144078SdfrSYSCTL_DECL(_net_inet_raw); 32244078Sdfr#endif 32344078Sdfr 3241541Srgrimes/* 325148682Srwatson * Lock macros for IPv4 layer multicast address lists. IPv4 lock goes 326148682Srwatson * before link layer multicast locks in the lock order. In most cases, 327148682Srwatson * consumers of IN_*_MULTI() macros should acquire the locks before 328148682Srwatson * calling them; users of the in_{add,del}multi() functions should not. 329148682Srwatson */ 330148682Srwatsonextern struct mtx in_multi_mtx; 331148682Srwatson#define IN_MULTI_LOCK() mtx_lock(&in_multi_mtx) 332148682Srwatson#define IN_MULTI_UNLOCK() mtx_unlock(&in_multi_mtx) 333148682Srwatson#define IN_MULTI_LOCK_ASSERT() mtx_assert(&in_multi_mtx, MA_OWNED) 334189592Sbms#define IN_MULTI_UNLOCK_ASSERT() mtx_assert(&in_multi_mtx, MA_NOTOWNED) 335148682Srwatson 336189592Sbms/* Acquire an in_multi record. */ 337189592Sbmsstatic __inline void 338189592Sbmsinm_acquire_locked(struct in_multi *inm) 339189592Sbms{ 340189592Sbms 341189592Sbms IN_MULTI_LOCK_ASSERT(); 342189592Sbms ++inm->inm_refcount; 343189592Sbms} 344189592Sbms 3451541Srgrimes/* 346189592Sbms * Return values for imo_multi_filter(). 3471541Srgrimes */ 348189592Sbms#define MCAST_PASS 0 /* Pass */ 349189592Sbms#define MCAST_NOTGMEMBER 1 /* This host not a member of group */ 350189592Sbms#define MCAST_NOTSMEMBER 2 /* This host excluded source */ 351189592Sbms#define MCAST_MUTED 3 /* [deprecated] */ 3521541Srgrimes 353178888Sjulianstruct rtentry; 35436192Sdgstruct route; 355170613Sbmsstruct ip_moptions; 356170613Sbms 357257325Sglebiusstruct in_multi *inm_lookup_locked(struct ifnet *, const struct in_addr); 358257325Sglebiusstruct in_multi *inm_lookup(struct ifnet *, const struct in_addr); 359189592Sbmsint imo_multi_filter(const struct ip_moptions *, const struct ifnet *, 360189592Sbms const struct sockaddr *, const struct sockaddr *); 361189592Sbmsvoid inm_commit(struct in_multi *); 362189592Sbmsvoid inm_clear_recorded(struct in_multi *); 363189592Sbmsvoid inm_print(const struct in_multi *); 364189592Sbmsint inm_record_source(struct in_multi *inm, const in_addr_t); 365189592Sbmsvoid inm_release(struct in_multi *); 366189592Sbmsvoid inm_release_locked(struct in_multi *); 367189592Sbmsstruct in_multi * 368189592Sbms in_addmulti(struct in_addr *, struct ifnet *); 36992723Salfredvoid in_delmulti(struct in_multi *); 370189592Sbmsint in_joingroup(struct ifnet *, const struct in_addr *, 371189592Sbms /*const*/ struct in_mfilter *, struct in_multi **); 372189592Sbmsint in_joingroup_locked(struct ifnet *, const struct in_addr *, 373189592Sbms /*const*/ struct in_mfilter *, struct in_multi **); 374189592Sbmsint in_leavegroup(struct in_multi *, /*const*/ struct in_mfilter *); 375189592Sbmsint in_leavegroup_locked(struct in_multi *, 376189592Sbms /*const*/ struct in_mfilter *); 37792723Salfredint in_control(struct socket *, u_long, caddr_t, struct ifnet *, 37893085Sbde struct thread *); 379228571Sglebiusint in_addprefix(struct in_ifaddr *, int); 380228571Sglebiusint in_scrubprefix(struct in_ifaddr *, u_int); 381302054Sbzvoid in_ifscrub_all(void); 38292723Salfredvoid ip_input(struct mbuf *); 383271300Sadrianvoid ip_direct_input(struct mbuf *); 384257500Sglebiusvoid in_ifadown(struct ifaddr *ifa, int); 385290383Sgnnstruct mbuf *ip_tryforward(struct mbuf *); 386186119Sqinglivoid *in_domifattach(struct ifnet *); 387186119Sqinglivoid in_domifdetach(struct ifnet *, void *); 3882169Spaul 389186119Sqingli 390178888Sjulian/* XXX */ 391178888Sjulianvoid in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum); 392178888Sjulianvoid in_rtredirect(struct sockaddr *, struct sockaddr *, 393178888Sjulian struct sockaddr *, int, struct sockaddr *, u_int); 39455205Speter#endif /* _KERNEL */ 39510939Swollman 39652904Sshin/* INET6 stuff */ 39752904Sshin#include <netinet6/in6_var.h> 39852904Sshin 39910939Swollman#endif /* _NETINET_IN_VAR_H_ */ 400