xref: /freebsd/sys/netinet/in_var.h (revision 130aebbab0d38da85f7d32b6d4227f95a2cd9ec7)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1985, 1986, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
312180b925SGarrett Wollman  *	@(#)in_var.h	8.2 (Berkeley) 1/9/95
32c3aac50fSPeter Wemm  * $FreeBSD$
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
35707f139eSPaul Richards #ifndef _NETINET_IN_VAR_H_
36707f139eSPaul Richards #define _NETINET_IN_VAR_H_
37707f139eSPaul Richards 
3826d50672SGleb Smirnoff /*
3926d50672SGleb Smirnoff  * Argument structure for SIOCAIFADDR.
4026d50672SGleb Smirnoff  */
4126d50672SGleb Smirnoff struct	in_aliasreq {
4226d50672SGleb Smirnoff 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
4326d50672SGleb Smirnoff 	struct	sockaddr_in ifra_addr;
4426d50672SGleb Smirnoff 	struct	sockaddr_in ifra_broadaddr;
4526d50672SGleb Smirnoff #define ifra_dstaddr ifra_broadaddr
4626d50672SGleb Smirnoff 	struct	sockaddr_in ifra_mask;
4726d50672SGleb Smirnoff 	int	ifra_vhid;
4826d50672SGleb Smirnoff };
4926d50672SGleb Smirnoff 
5026d50672SGleb Smirnoff #ifdef _KERNEL
51ffa5b11aSGarrett Wollman #include <sys/queue.h>
52ca925d9cSJonathan Lemon #include <sys/fnv_hash.h>
53d10910e6SBruce M Simpson #include <sys/tree.h>
54d10910e6SBruce M Simpson 
550d159406SGleb Smirnoff struct igmp_ifsoftc;
56d10910e6SBruce M Simpson struct in_multi;
57d10910e6SBruce M Simpson struct lltable;
58f3e1324bSStephen Hurd SLIST_HEAD(in_multi_head, in_multi);
59d10910e6SBruce M Simpson 
60d10910e6SBruce M Simpson /*
61d10910e6SBruce M Simpson  * IPv4 per-interface state.
62d10910e6SBruce M Simpson  */
63d10910e6SBruce M Simpson struct in_ifinfo {
64d10910e6SBruce M Simpson 	struct lltable		*ii_llt;	/* ARP state */
650d159406SGleb Smirnoff 	struct igmp_ifsoftc	*ii_igmp;	/* IGMP state */
66d10910e6SBruce M Simpson 	struct in_multi		*ii_allhosts;	/* 224.0.0.1 membership */
67d10910e6SBruce M Simpson };
68ffa5b11aSGarrett Wollman 
69df8bae1dSRodney W. Grimes /*
70df8bae1dSRodney W. Grimes  * Interface address, Internet version.  One of these structures
71477180fbSGarrett Wollman  * is allocated for each Internet address on an interface.
72df8bae1dSRodney W. Grimes  * The ifaddr structure contains the protocol-independent part
73df8bae1dSRodney W. Grimes  * of the structure and is assumed to be first.
74df8bae1dSRodney W. Grimes  */
75df8bae1dSRodney W. Grimes struct in_ifaddr {
76df8bae1dSRodney W. Grimes 	struct	ifaddr ia_ifa;		/* protocol-independent info */
77df8bae1dSRodney W. Grimes #define	ia_ifp		ia_ifa.ifa_ifp
78df8bae1dSRodney W. Grimes #define ia_flags	ia_ifa.ifa_flags
79b365d954SGleb Smirnoff 					/* ia_subnet{,mask} in host order */
80b365d954SGleb Smirnoff 	u_long	ia_subnet;		/* subnet address */
81b365d954SGleb Smirnoff 	u_long	ia_subnetmask;		/* mask of subnet */
82ca925d9cSJonathan Lemon 	LIST_ENTRY(in_ifaddr) ia_hash;	/* entry in bucket of inet addresses */
83d7c5a620SMatt Macy 	CK_STAILQ_ENTRY(in_ifaddr) ia_link;	/* list of internet addresses */
84df8bae1dSRodney W. Grimes 	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
85df8bae1dSRodney W. Grimes 	struct	sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
86df8bae1dSRodney W. Grimes #define	ia_broadaddr	ia_dstaddr
87df8bae1dSRodney W. Grimes 	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
882d9db0bcSEric van Gyzen 	struct	callout ia_garp_timer;	/* timer for retransmitting GARPs */
892d9db0bcSEric van Gyzen 	int	ia_garp_count;		/* count of retransmitted GARPs */
90df8bae1dSRodney W. Grimes };
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Given a pointer to an in_ifaddr (ifaddr),
94df8bae1dSRodney W. Grimes  * return a pointer to the addr as a sockaddr_in.
95df8bae1dSRodney W. Grimes  */
96df8bae1dSRodney W. Grimes #define IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
971bc8a809SSteven Wallace #define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
98d2945360SGleb Smirnoff #define IA_MASKSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_sockmask))
99df8bae1dSRodney W. Grimes 
100df8bae1dSRodney W. Grimes #define IN_LNAOF(in, ifa) \
101df8bae1dSRodney W. Grimes 	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
102df8bae1dSRodney W. Grimes 
103b5e8ce9fSBruce Evans extern	u_char	inetctlerrmap[];
104df8bae1dSRodney W. Grimes 
105582b6122SKip Macy #define LLTABLE(ifp)	\
106582b6122SKip Macy 	((struct in_ifinfo *)(ifp)->if_afdata[AF_INET])->ii_llt
107df8bae1dSRodney W. Grimes /*
108ca925d9cSJonathan Lemon  * Hash table for IP addresses.
109ca925d9cSJonathan Lemon  */
110d7c5a620SMatt Macy CK_STAILQ_HEAD(in_ifaddrhead, in_ifaddr);
1111b193af6SBjoern A. Zeeb LIST_HEAD(in_ifaddrhashhead, in_ifaddr);
112eddfbb76SRobert Watson 
113eddfbb76SRobert Watson VNET_DECLARE(struct in_ifaddrhashhead *, in_ifaddrhashtbl);
114eddfbb76SRobert Watson VNET_DECLARE(struct in_ifaddrhead, in_ifaddrhead);
115eddfbb76SRobert Watson VNET_DECLARE(u_long, in_ifaddrhmask);		/* mask for hash table */
116eddfbb76SRobert Watson 
1171e77c105SRobert Watson #define	V_in_ifaddrhashtbl	VNET(in_ifaddrhashtbl)
1181e77c105SRobert Watson #define	V_in_ifaddrhead		VNET(in_ifaddrhead)
1191e77c105SRobert Watson #define	V_in_ifaddrhmask	VNET(in_ifaddrhmask)
120ca925d9cSJonathan Lemon 
121ca925d9cSJonathan Lemon #define INADDR_NHASH_LOG2       9
122ca925d9cSJonathan Lemon #define INADDR_NHASH		(1 << INADDR_NHASH_LOG2)
123ca925d9cSJonathan Lemon #define INADDR_HASHVAL(x)	fnv_32_buf((&(x)), sizeof(x), FNV1_32_INIT)
124ca925d9cSJonathan Lemon #define INADDR_HASH(x) \
125603724d3SBjoern A. Zeeb 	(&V_in_ifaddrhashtbl[INADDR_HASHVAL(x) & V_in_ifaddrhmask])
126ca925d9cSJonathan Lemon 
127cc0a3c8cSAndrey V. Elsukov extern	struct rmlock in_ifaddr_lock;
1282d9cfabaSRobert Watson 
129cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_LOCK_ASSERT()	rm_assert(&in_ifaddr_lock, RA_LOCKED)
130cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_RLOCK(t)	rm_rlock(&in_ifaddr_lock, (t))
131cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_RLOCK_ASSERT()	rm_assert(&in_ifaddr_lock, RA_RLOCKED)
132cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_RUNLOCK(t)	rm_runlock(&in_ifaddr_lock, (t))
133cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_WLOCK()	rm_wlock(&in_ifaddr_lock)
134cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_WLOCK_ASSERT()	rm_assert(&in_ifaddr_lock, RA_WLOCKED)
135cc0a3c8cSAndrey V. Elsukov #define	IN_IFADDR_WUNLOCK()	rm_wunlock(&in_ifaddr_lock)
1362d9cfabaSRobert Watson 
13707ea6709SBruce M Simpson /*
138e2fd806bSBruce M Simpson  * Macro for finding the internet address structure (in_ifaddr)
13907ea6709SBruce M Simpson  * corresponding to one of our IP addresses (in_addr).
14007ea6709SBruce M Simpson  */
14107ea6709SBruce M Simpson #define INADDR_TO_IFADDR(addr, ia) \
14207ea6709SBruce M Simpson 	/* struct in_addr addr; */ \
14307ea6709SBruce M Simpson 	/* struct in_ifaddr *ia; */ \
14407ea6709SBruce M Simpson do { \
14507ea6709SBruce M Simpson \
14607ea6709SBruce M Simpson 	LIST_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) \
14707ea6709SBruce M Simpson 		if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \
14807ea6709SBruce M Simpson 			break; \
14907ea6709SBruce M Simpson } while (0)
150ca925d9cSJonathan Lemon 
151ca925d9cSJonathan Lemon /*
152df8bae1dSRodney W. Grimes  * Macro for finding the interface (ifnet structure) corresponding to one
153df8bae1dSRodney W. Grimes  * of our IP addresses.
154df8bae1dSRodney W. Grimes  */
155df8bae1dSRodney W. Grimes #define INADDR_TO_IFP(addr, ifp) \
156df8bae1dSRodney W. Grimes 	/* struct in_addr addr; */ \
157df8bae1dSRodney W. Grimes 	/* struct ifnet *ifp; */ \
158df8bae1dSRodney W. Grimes { \
1599f81cc84SRuslan Ermilov 	struct in_ifaddr *ia; \
160df8bae1dSRodney W. Grimes \
16107ea6709SBruce M Simpson 	INADDR_TO_IFADDR(addr, ia); \
162df8bae1dSRodney W. Grimes 	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166df8bae1dSRodney W. Grimes  * Macro for finding the internet address structure (in_ifaddr) corresponding
167df8bae1dSRodney W. Grimes  * to a given interface (ifnet structure).
168df8bae1dSRodney W. Grimes  */
169cc0a3c8cSAndrey V. Elsukov #define IFP_TO_IA(ifp, ia, t)						\
170df8bae1dSRodney W. Grimes 	/* struct ifnet *ifp; */					\
171df8bae1dSRodney W. Grimes 	/* struct in_ifaddr *ia; */					\
172cc0a3c8cSAndrey V. Elsukov 	/* struct rm_priotracker *t; */					\
173b9abeb9dSGleb Smirnoff do {									\
174b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();						\
175cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RLOCK((t));						\
176d7c5a620SMatt Macy 	for ((ia) = CK_STAILQ_FIRST(&V_in_ifaddrhead);			\
177df8bae1dSRodney W. Grimes 	    (ia) != NULL && (ia)->ia_ifp != (ifp);			\
178d7c5a620SMatt Macy 	    (ia) = CK_STAILQ_NEXT((ia), ia_link))				\
179df8bae1dSRodney W. Grimes 		continue;						\
180cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RUNLOCK((t));						\
181b9abeb9dSGleb Smirnoff } while (0)
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes /*
184d10910e6SBruce M Simpson  * Legacy IPv4 IGMP per-link structure.
185f0068c4aSGarrett Wollman  */
186f0068c4aSGarrett Wollman struct router_info {
18749fa849bSBill Fenner 	struct ifnet *rti_ifp;
18849fa849bSBill Fenner 	int    rti_type; /* type of router which is querier on this interface */
18949fa849bSBill Fenner 	int    rti_time; /* # of slow timeouts since last old query */
1906c4b2ad3SRobert Watson 	SLIST_ENTRY(router_info) rti_list;
191f0068c4aSGarrett Wollman };
192f0068c4aSGarrett Wollman 
193f0068c4aSGarrett Wollman /*
194d10910e6SBruce M Simpson  * IPv4 multicast IGMP-layer source entry.
195d10910e6SBruce M Simpson  */
196d10910e6SBruce M Simpson struct ip_msource {
197d10910e6SBruce M Simpson 	RB_ENTRY(ip_msource)	ims_link;	/* RB tree links */
198d10910e6SBruce M Simpson 	in_addr_t		ims_haddr;	/* host byte order */
199d10910e6SBruce M Simpson 	struct ims_st {
200d10910e6SBruce M Simpson 		uint16_t	ex;		/* # of exclusive members */
201d10910e6SBruce M Simpson 		uint16_t	in;		/* # of inclusive members */
202d10910e6SBruce M Simpson 	}			ims_st[2];	/* state at t0, t1 */
203d10910e6SBruce M Simpson 	uint8_t			ims_stp;	/* pending query */
204d10910e6SBruce M Simpson };
205d10910e6SBruce M Simpson 
206d10910e6SBruce M Simpson /*
207d10910e6SBruce M Simpson  * IPv4 multicast PCB-layer source entry.
208d10910e6SBruce M Simpson  */
209d10910e6SBruce M Simpson struct in_msource {
210d10910e6SBruce M Simpson 	RB_ENTRY(ip_msource)	ims_link;	/* RB tree links */
211d10910e6SBruce M Simpson 	in_addr_t		ims_haddr;	/* host byte order */
212d10910e6SBruce M Simpson 	uint8_t			imsl_st[2];	/* state before/at commit */
213d10910e6SBruce M Simpson };
214d10910e6SBruce M Simpson 
215d10910e6SBruce M Simpson RB_HEAD(ip_msource_tree, ip_msource);	/* define struct ip_msource_tree */
216d10910e6SBruce M Simpson 
217d10910e6SBruce M Simpson static __inline int
218d10910e6SBruce M Simpson ip_msource_cmp(const struct ip_msource *a, const struct ip_msource *b)
219d10910e6SBruce M Simpson {
220d10910e6SBruce M Simpson 
221d10910e6SBruce M Simpson 	if (a->ims_haddr < b->ims_haddr)
222d10910e6SBruce M Simpson 		return (-1);
223d10910e6SBruce M Simpson 	if (a->ims_haddr == b->ims_haddr)
224d10910e6SBruce M Simpson 		return (0);
225d10910e6SBruce M Simpson 	return (1);
226d10910e6SBruce M Simpson }
227d10910e6SBruce M Simpson RB_PROTOTYPE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);
228d10910e6SBruce M Simpson 
229d10910e6SBruce M Simpson /*
230d10910e6SBruce M Simpson  * IPv4 multicast PCB-layer group filter descriptor.
231d10910e6SBruce M Simpson  */
232d10910e6SBruce M Simpson struct in_mfilter {
233d10910e6SBruce M Simpson 	struct ip_msource_tree	imf_sources; /* source list for (S,G) */
234d10910e6SBruce M Simpson 	u_long			imf_nsrc;    /* # of source entries */
235d10910e6SBruce M Simpson 	uint8_t			imf_st[2];   /* state before/at commit */
23659854ecfSHans Petter Selasky 	struct in_multi	       *imf_inm;     /* associated multicast address */
23759854ecfSHans Petter Selasky 	STAILQ_ENTRY(in_mfilter) imf_entry;  /* list entry */
238d10910e6SBruce M Simpson };
239d10910e6SBruce M Simpson 
240d10910e6SBruce M Simpson /*
24159854ecfSHans Petter Selasky  * Helper types and functions for IPv4 multicast filters.
24259854ecfSHans Petter Selasky  */
24359854ecfSHans Petter Selasky STAILQ_HEAD(ip_mfilter_head, in_mfilter);
24459854ecfSHans Petter Selasky 
24559854ecfSHans Petter Selasky struct in_mfilter *ip_mfilter_alloc(int mflags, int st0, int st1);
24659854ecfSHans Petter Selasky void ip_mfilter_free(struct in_mfilter *);
24759854ecfSHans Petter Selasky 
24859854ecfSHans Petter Selasky static inline void
24959854ecfSHans Petter Selasky ip_mfilter_init(struct ip_mfilter_head *head)
25059854ecfSHans Petter Selasky {
25159854ecfSHans Petter Selasky 
25259854ecfSHans Petter Selasky 	STAILQ_INIT(head);
25359854ecfSHans Petter Selasky }
25459854ecfSHans Petter Selasky 
25559854ecfSHans Petter Selasky static inline struct in_mfilter *
25659854ecfSHans Petter Selasky ip_mfilter_first(const struct ip_mfilter_head *head)
25759854ecfSHans Petter Selasky {
25859854ecfSHans Petter Selasky 
25959854ecfSHans Petter Selasky 	return (STAILQ_FIRST(head));
26059854ecfSHans Petter Selasky }
26159854ecfSHans Petter Selasky 
26259854ecfSHans Petter Selasky static inline void
26359854ecfSHans Petter Selasky ip_mfilter_insert(struct ip_mfilter_head *head, struct in_mfilter *imf)
26459854ecfSHans Petter Selasky {
26559854ecfSHans Petter Selasky 
26659854ecfSHans Petter Selasky 	STAILQ_INSERT_TAIL(head, imf, imf_entry);
26759854ecfSHans Petter Selasky }
26859854ecfSHans Petter Selasky 
26959854ecfSHans Petter Selasky static inline void
27059854ecfSHans Petter Selasky ip_mfilter_remove(struct ip_mfilter_head *head, struct in_mfilter *imf)
27159854ecfSHans Petter Selasky {
27259854ecfSHans Petter Selasky 
27359854ecfSHans Petter Selasky 	STAILQ_REMOVE(head, imf, in_mfilter, imf_entry);
27459854ecfSHans Petter Selasky }
27559854ecfSHans Petter Selasky 
27659854ecfSHans Petter Selasky #define	IP_MFILTER_FOREACH(imf, head) \
27759854ecfSHans Petter Selasky 	STAILQ_FOREACH(imf, head, imf_entry)
27859854ecfSHans Petter Selasky 
27959854ecfSHans Petter Selasky static inline size_t
28059854ecfSHans Petter Selasky ip_mfilter_count(struct ip_mfilter_head *head)
28159854ecfSHans Petter Selasky {
28259854ecfSHans Petter Selasky 	struct in_mfilter *imf;
28359854ecfSHans Petter Selasky 	size_t num = 0;
28459854ecfSHans Petter Selasky 
28559854ecfSHans Petter Selasky 	STAILQ_FOREACH(imf, head, imf_entry)
28659854ecfSHans Petter Selasky 		num++;
28759854ecfSHans Petter Selasky 	return (num);
28859854ecfSHans Petter Selasky }
28959854ecfSHans Petter Selasky 
29059854ecfSHans Petter Selasky /*
291d10910e6SBruce M Simpson  * IPv4 group descriptor.
292d10910e6SBruce M Simpson  *
293d10910e6SBruce M Simpson  * For every entry on an ifnet's if_multiaddrs list which represents
294d10910e6SBruce M Simpson  * an IP multicast group, there is one of these structures.
295d10910e6SBruce M Simpson  *
296d10910e6SBruce M Simpson  * If any source filters are present, then a node will exist in the RB-tree
297d10910e6SBruce M Simpson  * to permit fast lookup by source whenever an operation takes place.
298d10910e6SBruce M Simpson  * This permits pre-order traversal when we issue reports.
299d10910e6SBruce M Simpson  * Source filter trees are kept separately from the socket layer to
300d10910e6SBruce M Simpson  * greatly simplify locking.
301d10910e6SBruce M Simpson  *
302d10910e6SBruce M Simpson  * When IGMPv3 is active, inm_timer is the response to group query timer.
303d10910e6SBruce M Simpson  * The state-change timer inm_sctimer is separate; whenever state changes
304d10910e6SBruce M Simpson  * for the group the state change record is generated and transmitted,
305d10910e6SBruce M Simpson  * and kept if retransmissions are necessary.
306d10910e6SBruce M Simpson  *
307d10910e6SBruce M Simpson  * FUTURE: inm_link is now only used when groups are being purged
308d10910e6SBruce M Simpson  * on a detaching ifnet. It could be demoted to a SLIST_ENTRY, but
309d10910e6SBruce M Simpson  * because it is at the very start of the struct, we can't do this
310d10910e6SBruce M Simpson  * w/o breaking the ABI for ifmcstat.
311df8bae1dSRodney W. Grimes  */
312df8bae1dSRodney W. Grimes struct in_multi {
313d10910e6SBruce M Simpson 	LIST_ENTRY(in_multi) inm_link;	/* to-be-released by in_ifdetach */
314477180fbSGarrett Wollman 	struct	in_addr inm_addr;	/* IP multicast address, convenience */
315df8bae1dSRodney W. Grimes 	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
316477180fbSGarrett Wollman 	struct	ifmultiaddr *inm_ifma;	/* back pointer to ifmultiaddr */
317d10910e6SBruce M Simpson 	u_int	inm_timer;		/* IGMPv1/v2 group / v3 query timer */
318f0068c4aSGarrett Wollman 	u_int	inm_state;		/* state of the membership */
319d10910e6SBruce M Simpson 	void	*inm_rti;		/* unused, legacy field */
320ec002feeSBruce M Simpson 	u_int	inm_refcount;		/* reference count */
321d10910e6SBruce M Simpson 
322d10910e6SBruce M Simpson 	/* New fields for IGMPv3 follow. */
3230d159406SGleb Smirnoff 	struct igmp_ifsoftc	*inm_igi;	/* IGMP info */
324d10910e6SBruce M Simpson 	SLIST_ENTRY(in_multi)	 inm_nrele;	/* to-be-released by IGMP */
325d10910e6SBruce M Simpson 	struct ip_msource_tree	 inm_srcs;	/* tree of sources */
326d10910e6SBruce M Simpson 	u_long			 inm_nsrc;	/* # of tree entries */
327d10910e6SBruce M Simpson 
328058e08beSGleb Smirnoff 	struct mbufq		 inm_scq;	/* queue of pending
329d10910e6SBruce M Simpson 						 * state-change packets */
330d10910e6SBruce M Simpson 	struct timeval		 inm_lastgsrtv;	/* Time of last G-S-R query */
331d10910e6SBruce M Simpson 	uint16_t		 inm_sctimer;	/* state-change timer */
332d10910e6SBruce M Simpson 	uint16_t		 inm_scrv;	/* state-change rexmit count */
333d10910e6SBruce M Simpson 
334d10910e6SBruce M Simpson 	/*
335d10910e6SBruce M Simpson 	 * SSM state counters which track state at T0 (the time the last
336d10910e6SBruce M Simpson 	 * state-change report's RV timer went to zero) and T1
337d10910e6SBruce M Simpson 	 * (time of pending report, i.e. now).
338d10910e6SBruce M Simpson 	 * Used for computing IGMPv3 state-change reports. Several refcounts
339d10910e6SBruce M Simpson 	 * are maintained here to optimize for common use-cases.
340d10910e6SBruce M Simpson 	 */
341d10910e6SBruce M Simpson 	struct inm_st {
342d10910e6SBruce M Simpson 		uint16_t	iss_fmode;	/* IGMP filter mode */
343d10910e6SBruce M Simpson 		uint16_t	iss_asm;	/* # of ASM listeners */
344d10910e6SBruce M Simpson 		uint16_t	iss_ex;		/* # of exclusive members */
345d10910e6SBruce M Simpson 		uint16_t	iss_in;		/* # of inclusive members */
346d10910e6SBruce M Simpson 		uint16_t	iss_rec;	/* # of recorded sources */
347d10910e6SBruce M Simpson 	}			inm_st[2];	/* state at t0, t1 */
348df8bae1dSRodney W. Grimes };
349df8bae1dSRodney W. Grimes 
35071498f30SBruce M Simpson /*
351d10910e6SBruce M Simpson  * Helper function to derive the filter mode on a source entry
352d10910e6SBruce M Simpson  * from its internal counters. Predicates are:
353d10910e6SBruce M Simpson  *  A source is only excluded if all listeners exclude it.
354d10910e6SBruce M Simpson  *  A source is only included if no listeners exclude it,
355d10910e6SBruce M Simpson  *  and at least one listener includes it.
356d10910e6SBruce M Simpson  * May be used by ifmcstat(8).
35771498f30SBruce M Simpson  */
358d10910e6SBruce M Simpson static __inline uint8_t
359d10910e6SBruce M Simpson ims_get_mode(const struct in_multi *inm, const struct ip_msource *ims,
360d10910e6SBruce M Simpson     uint8_t t)
361d10910e6SBruce M Simpson {
362d10910e6SBruce M Simpson 
363d10910e6SBruce M Simpson 	t = !!t;
364d10910e6SBruce M Simpson 	if (inm->inm_st[t].iss_ex > 0 &&
365d10910e6SBruce M Simpson 	    inm->inm_st[t].iss_ex == ims->ims_st[t].ex)
366d10910e6SBruce M Simpson 		return (MCAST_EXCLUDE);
367d10910e6SBruce M Simpson 	else if (ims->ims_st[t].in > 0 && ims->ims_st[t].ex == 0)
368d10910e6SBruce M Simpson 		return (MCAST_INCLUDE);
369d10910e6SBruce M Simpson 	return (MCAST_UNDEFINED);
370d10910e6SBruce M Simpson }
37171498f30SBruce M Simpson 
372ce02431fSDoug Rabson #ifdef SYSCTL_DECL
373969bb53eSAndre Oppermann SYSCTL_DECL(_net_inet);
374ce02431fSDoug Rabson SYSCTL_DECL(_net_inet_ip);
375ce02431fSDoug Rabson SYSCTL_DECL(_net_inet_raw);
376ce02431fSDoug Rabson #endif
377ce02431fSDoug Rabson 
378df8bae1dSRodney W. Grimes /*
379dd5a318bSRobert Watson  * Lock macros for IPv4 layer multicast address lists.  IPv4 lock goes
380dd5a318bSRobert Watson  * before link layer multicast locks in the lock order.  In most cases,
381dd5a318bSRobert Watson  * consumers of IN_*_MULTI() macros should acquire the locks before
382dd5a318bSRobert Watson  * calling them; users of the in_{add,del}multi() functions should not.
383dd5a318bSRobert Watson  */
384f3e1324bSStephen Hurd extern struct mtx in_multi_list_mtx;
385f3e1324bSStephen Hurd extern struct sx in_multi_sx;
386f3e1324bSStephen Hurd 
387f3e1324bSStephen Hurd #define	IN_MULTI_LIST_LOCK()		mtx_lock(&in_multi_list_mtx)
388f3e1324bSStephen Hurd #define	IN_MULTI_LIST_UNLOCK()	mtx_unlock(&in_multi_list_mtx)
389f3e1324bSStephen Hurd #define	IN_MULTI_LIST_LOCK_ASSERT()	mtx_assert(&in_multi_list_mtx, MA_OWNED)
390f3e1324bSStephen Hurd #define	IN_MULTI_LIST_UNLOCK_ASSERT() mtx_assert(&in_multi_list_mtx, MA_NOTOWNED)
391f3e1324bSStephen Hurd 
392f3e1324bSStephen Hurd #define	IN_MULTI_LOCK()		sx_xlock(&in_multi_sx)
393f3e1324bSStephen Hurd #define	IN_MULTI_UNLOCK()	sx_xunlock(&in_multi_sx)
394f3e1324bSStephen Hurd #define	IN_MULTI_LOCK_ASSERT()	sx_assert(&in_multi_sx, SA_XLOCKED)
395f3e1324bSStephen Hurd #define	IN_MULTI_UNLOCK_ASSERT() sx_assert(&in_multi_sx, SA_XUNLOCKED)
396dd5a318bSRobert Watson 
397b6f6f880SMatt Macy void inm_disconnect(struct in_multi *inm);
398b6f6f880SMatt Macy extern int ifma_restart;
399b6f6f880SMatt Macy 
400d10910e6SBruce M Simpson /* Acquire an in_multi record. */
401d10910e6SBruce M Simpson static __inline void
402d10910e6SBruce M Simpson inm_acquire_locked(struct in_multi *inm)
403d10910e6SBruce M Simpson {
404d10910e6SBruce M Simpson 
405f3e1324bSStephen Hurd 	IN_MULTI_LIST_LOCK_ASSERT();
406d10910e6SBruce M Simpson 	++inm->inm_refcount;
407d10910e6SBruce M Simpson }
408df8bae1dSRodney W. Grimes 
409f3e1324bSStephen Hurd static __inline void
410f3e1324bSStephen Hurd inm_acquire(struct in_multi *inm)
411f3e1324bSStephen Hurd {
412f3e1324bSStephen Hurd 	IN_MULTI_LIST_LOCK();
413f3e1324bSStephen Hurd 	inm_acquire_locked(inm);
414f3e1324bSStephen Hurd 	IN_MULTI_LIST_UNLOCK();
415f3e1324bSStephen Hurd }
416f3e1324bSStephen Hurd 
417f3e1324bSStephen Hurd static __inline void
418f3e1324bSStephen Hurd inm_rele_locked(struct in_multi_head *inmh, struct in_multi *inm)
419f3e1324bSStephen Hurd {
420f3e1324bSStephen Hurd 	MPASS(inm->inm_refcount > 0);
421f3e1324bSStephen Hurd 	IN_MULTI_LIST_LOCK_ASSERT();
422f3e1324bSStephen Hurd 
423f3e1324bSStephen Hurd 	if (--inm->inm_refcount == 0) {
424f3e1324bSStephen Hurd 		MPASS(inmh != NULL);
425b6f6f880SMatt Macy 		inm_disconnect(inm);
426f3e1324bSStephen Hurd 		inm->inm_ifma->ifma_protospec = NULL;
427f3e1324bSStephen Hurd 		SLIST_INSERT_HEAD(inmh, inm, inm_nrele);
428f3e1324bSStephen Hurd 	}
429f3e1324bSStephen Hurd }
430f3e1324bSStephen Hurd 
431df8bae1dSRodney W. Grimes /*
432d10910e6SBruce M Simpson  * Return values for imo_multi_filter().
433df8bae1dSRodney W. Grimes  */
434d10910e6SBruce M Simpson #define MCAST_PASS		0	/* Pass */
435d10910e6SBruce M Simpson #define MCAST_NOTGMEMBER	1	/* This host not a member of group */
436d10910e6SBruce M Simpson #define MCAST_NOTSMEMBER	2	/* This host excluded source */
437d10910e6SBruce M Simpson #define MCAST_MUTED		3	/* [deprecated] */
438df8bae1dSRodney W. Grimes 
4392f23f45bSAlexander V. Chernikov struct rib_head;
44071498f30SBruce M Simpson struct	ip_moptions;
44171498f30SBruce M Simpson 
4428d7cf9b5SGleb Smirnoff struct in_multi *inm_lookup_locked(struct ifnet *, const struct in_addr);
4438d7cf9b5SGleb Smirnoff struct in_multi *inm_lookup(struct ifnet *, const struct in_addr);
444d10910e6SBruce M Simpson int	imo_multi_filter(const struct ip_moptions *, const struct ifnet *,
445d10910e6SBruce M Simpson 	    const struct sockaddr *, const struct sockaddr *);
446d10910e6SBruce M Simpson void	inm_commit(struct in_multi *);
447d10910e6SBruce M Simpson void	inm_clear_recorded(struct in_multi *);
448d10910e6SBruce M Simpson void	inm_print(const struct in_multi *);
449d10910e6SBruce M Simpson int	inm_record_source(struct in_multi *inm, const in_addr_t);
450f3e1324bSStephen Hurd void	inm_release_deferred(struct in_multi *);
451f3e1324bSStephen Hurd void	inm_release_list_deferred(struct in_multi_head *);
4523689652cSHans Petter Selasky void	inm_release_wait(void *);
453d10910e6SBruce M Simpson struct	in_multi *
454d10910e6SBruce M Simpson in_addmulti(struct in_addr *, struct ifnet *);
455d10910e6SBruce M Simpson int	in_joingroup(struct ifnet *, const struct in_addr *,
456d10910e6SBruce M Simpson 	    /*const*/ struct in_mfilter *, struct in_multi **);
457d10910e6SBruce M Simpson int	in_joingroup_locked(struct ifnet *, const struct in_addr *,
458d10910e6SBruce M Simpson 	    /*const*/ struct in_mfilter *, struct in_multi **);
459d10910e6SBruce M Simpson int	in_leavegroup(struct in_multi *, /*const*/ struct in_mfilter *);
460d10910e6SBruce M Simpson int	in_leavegroup_locked(struct in_multi *,
461d10910e6SBruce M Simpson 	    /*const*/ struct in_mfilter *);
4624d77a549SAlfred Perlstein int	in_control(struct socket *, u_long, caddr_t, struct ifnet *,
4634d77a549SAlfred Perlstein 	    struct thread *);
464*130aebbaSAlexander V. Chernikov int	in_addprefix(struct in_ifaddr *);
46508b68b0eSGleb Smirnoff int	in_scrubprefix(struct in_ifaddr *, u_int);
46689856f7eSBjoern A. Zeeb void	in_ifscrub_all(void);
46781728a53SAlexander V. Chernikov int	in_handle_ifaddr_route(int, struct in_ifaddr *);
4684d77a549SAlfred Perlstein void	ip_input(struct mbuf *);
469b8bc95cdSAdrian Chadd void	ip_direct_input(struct mbuf *);
470586904c2SGleb Smirnoff void	in_ifadown(struct ifaddr *ifa, int);
47133872124SGeorge V. Neville-Neil struct	mbuf	*ip_tryforward(struct mbuf *);
4726e6b3f7cSQing Li void	*in_domifattach(struct ifnet *);
4736e6b3f7cSQing Li void	in_domifdetach(struct ifnet *, void *);
4742f23f45bSAlexander V. Chernikov struct rib_head *in_inithead(uint32_t fibnum);
4752f23f45bSAlexander V. Chernikov #ifdef VIMAGE
4762f23f45bSAlexander V. Chernikov void	in_detachhead(struct rib_head *rh);
4772f23f45bSAlexander V. Chernikov #endif
4786e6b3f7cSQing Li 
479664a31e4SPeter Wemm #endif /* _KERNEL */
4802180b925SGarrett Wollman 
48176429de4SYoshinobu Inoue /* INET6 stuff */
48276429de4SYoshinobu Inoue #include <netinet6/in6_var.h>
48376429de4SYoshinobu Inoue 
4842180b925SGarrett Wollman #endif /* _NETINET_IN_VAR_H_ */
485