1/*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1985, 1986, 1993
30 *	The Regents of the University of California.  All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 *    notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 *    must display the following acknowledgement:
42 *	This product includes software developed by the University of
43 *	California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	@(#)in_var.h	8.2 (Berkeley) 1/9/95
61 */
62
63#ifndef _NETINET_IN_VAR_H_
64#define	_NETINET_IN_VAR_H_
65#include <sys/appleapiopts.h>
66
67#include <sys/queue.h>
68#include <sys/kern_event.h>
69
70#ifdef BSD_KERNEL_PRIVATE
71#include <net/route.h>
72
73/*
74 * Interface address, Internet version.  One of these structures
75 * is allocated for each Internet address on an interface.
76 * The ifaddr structure contains the protocol-independent part
77 * of the structure and is assumed to be first.
78 */
79struct in_ifaddr {
80	struct ifaddr ia_ifa;		/* protocol-independent info */
81#define	ia_ifp		ia_ifa.ifa_ifp
82#define	ia_flags	ia_ifa.ifa_flags
83					/* ia_{,sub}net{,mask} in host order */
84	u_int32_t ia_net;		/* network number of interface */
85	u_int32_t ia_netmask;		/* mask of net part */
86	u_int32_t ia_subnet;		/* subnet number, including net */
87	u_int32_t ia_subnetmask;	/* mask of subnet part */
88	struct in_addr ia_netbroadcast;	/* to recognize net broadcasts */
89	TAILQ_ENTRY(in_ifaddr) ia_link;	/* tailq macro glue */
90	struct sockaddr_in ia_addr;	/* reserve space for interface name */
91	struct sockaddr_in ia_dstaddr;	/* reserve space for broadcast addr */
92#define	ia_broadaddr	ia_dstaddr
93	struct sockaddr_in ia_sockmask;	/* reserve space for general netmask */
94	TAILQ_ENTRY(in_ifaddr) ia_hash;	/* hash bucket entry */
95};
96
97#define	ifatoia(ifa)	((struct in_ifaddr *)(void *)(ifa))
98#endif /* BSD_KERNEL_PRIVATE */
99
100struct in_aliasreq {
101	char ifra_name[IFNAMSIZ];	/* if name, e.g. "en0" */
102	struct sockaddr_in ifra_addr;
103	struct sockaddr_in ifra_broadaddr;
104#define	ifra_dstaddr	ifra_broadaddr
105	struct sockaddr_in ifra_mask;
106};
107
108/*
109 * Event data, inet style.
110 */
111struct kev_in_data {
112	struct net_event_data link_data;
113	struct in_addr ia_addr;		/* interface address */
114	u_int32_t ia_net;		/* network number of interface */
115	u_int32_t ia_netmask;		/* mask of net part */
116	u_int32_t ia_subnet;		/* subnet number, including net */
117	u_int32_t ia_subnetmask;	/* mask of subnet part */
118	struct in_addr ia_netbroadcast;	/* to recognize net broadcasts */
119	struct in_addr ia_dstaddr;
120};
121
122struct kev_in_collision {
123	struct net_event_data link_data; /* link where ARP was received on */
124	struct in_addr ia_ipaddr;	/* conflicting IP address */
125	u_char hw_len;			/* length of hardware address */
126	u_char hw_addr[0];		/* variable length hardware address */
127};
128
129#ifdef __APPLE_API_PRIVATE
130struct kev_in_portinuse {
131	u_int16_t port;		/* conflicting port number in host order */
132	u_int32_t req_pid;	/* PID port requestor */
133	u_int32_t reserved[2];
134};
135#endif /* __APPLE_API_PRIVATE */
136
137/*
138 * Define inet event subclass and specific inet events.
139 */
140#define	KEV_INET_SUBCLASS		1 /* inet subclass identifier */
141
142#define	KEV_INET_NEW_ADDR		1 /* Userland configured IP address */
143#define	KEV_INET_CHANGED_ADDR		2 /* Address changed event */
144#define	KEV_INET_ADDR_DELETED		3 /* IPv6 address was deleted */
145#define	KEV_INET_SIFDSTADDR		4 /* Dest. address was set */
146#define	KEV_INET_SIFBRDADDR		5 /* Broadcast address was set */
147#define	KEV_INET_SIFNETMASK		6 /* Netmask was set */
148#define	KEV_INET_ARPCOLLISION		7 /* ARP collision detected */
149#ifdef __APPLE_API_PRIVATE
150#define	KEV_INET_PORTINUSE		8 /* use ken_in_portinuse */
151#endif
152
153#ifdef BSD_KERNEL_PRIVATE
154#include <net/if_var.h>
155#include <kern/locks.h>
156#include <sys/tree.h>
157/*
158 * Given a pointer to an in_ifaddr (ifaddr),
159 * return a pointer to the addr as a sockaddr_in.
160 */
161#define	IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
162#define	IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
163
164#define	IN_LNAOF(in, ifa) \
165	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
166
167/*
168 * Hash table for IPv4 addresses.
169 */
170extern TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead;
171extern TAILQ_HEAD(in_ifaddrhashhead, in_ifaddr) *in_ifaddrhashtbl;
172extern lck_rw_t *in_ifaddr_rwlock;
173
174#define	INADDR_HASH(x)	(&in_ifaddrhashtbl[inaddr_hashval(x)])
175
176extern	u_char	inetctlerrmap[];
177
178/*
179 * Macro for finding the interface (ifnet structure) corresponding to one
180 * of our IP addresses.
181 */
182#define	INADDR_TO_IFP(addr, ifp)					\
183	/* struct in_addr addr; */					\
184	/* struct ifnet *ifp; */					\
185{									\
186	struct in_ifaddr *ia;						\
187									\
188	lck_rw_lock_shared(in_ifaddr_rwlock);				\
189	TAILQ_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) {	\
190		IFA_LOCK_SPIN(&ia->ia_ifa);				\
191		if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) {	\
192			IFA_UNLOCK(&ia->ia_ifa);			\
193			break;						\
194		}							\
195		IFA_UNLOCK(&ia->ia_ifa);				\
196	}								\
197	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp;			\
198	lck_rw_done(in_ifaddr_rwlock);					\
199}
200
201/*
202 * Macro for finding the internet address structure (in_ifaddr) corresponding
203 * to a given interface (ifnet structure).  Caller is responsible for freeing
204 * the reference.
205 */
206#define	IFP_TO_IA(ifp, ia)						\
207	/* struct ifnet *ifp; */					\
208	/* struct in_ifaddr *ia; */					\
209{									\
210	lck_rw_lock_shared(in_ifaddr_rwlock);				\
211	for ((ia) = TAILQ_FIRST(&in_ifaddrhead);			\
212	    (ia) != NULL && (ia)->ia_ifp != (ifp);			\
213	    (ia) = TAILQ_NEXT((ia), ia_link))				\
214		continue;						\
215	if ((ia) != NULL)						\
216		IFA_ADDREF(&(ia)->ia_ifa);				\
217	lck_rw_done(in_ifaddr_rwlock);					\
218}
219
220/*
221 * This information should be part of the ifnet structure but we don't wish
222 * to change that - as it might break a number of things
223 */
224
225/*
226 * Legacy IPv4 IGMP per-link structure.
227 */
228struct router_info {
229	struct ifnet *rti_ifp;
230	int    rti_type; /* type of router which is querier on this interface */
231	int    rti_time; /* # of slow timeouts since last old query */
232	SLIST_ENTRY(router_info) rti_list;
233};
234
235/*
236 * IPv4 multicast IGMP-layer source entry.
237 */
238struct ip_msource {
239	RB_ENTRY(ip_msource)	ims_link;	/* RB tree links */
240	in_addr_t		ims_haddr;	/* host byte order */
241	struct ims_st {
242		uint16_t	ex;		/* # of exclusive members */
243		uint16_t	in;		/* # of inclusive members */
244	}			ims_st[2];	/* state at t0, t1 */
245	uint8_t			ims_stp;	/* pending query */
246};
247
248/*
249 * IPv4 multicast PCB-layer source entry.
250 */
251struct in_msource {
252	RB_ENTRY(ip_msource)	ims_link;	/* RB tree links */
253	in_addr_t		ims_haddr;	/* host byte order */
254	uint8_t			imsl_st[2];	/* state before/at commit */
255};
256
257RB_HEAD(ip_msource_tree, ip_msource);	/* define struct ip_msource_tree */
258
259RB_PROTOTYPE_SC_PREV(__private_extern__, ip_msource_tree, ip_msource,
260    ims_link, ip_msource_cmp);
261
262/*
263 * IPv4 multicast PCB-layer group filter descriptor.
264 */
265struct in_mfilter {
266	struct ip_msource_tree	imf_sources; /* source list for (S,G) */
267	u_long			imf_nsrc;    /* # of source entries */
268	uint8_t			imf_st[2];   /* state before/at commit */
269};
270
271struct igmp_ifinfo;
272
273/*
274 * IPv4 group descriptor.
275 *
276 * For every entry on an ifnet's if_multiaddrs list which represents
277 * an IP multicast group, there is one of these structures.
278 *
279 * If any source filters are present, then a node will exist in the RB-tree
280 * to permit fast lookup by source whenever an operation takes place.
281 * This permits pre-order traversal when we issue reports.
282 * Source filter trees are kept separately from the socket layer to
283 * greatly simplify locking.
284 *
285 * When IGMPv3 is active, inm_timer is the response to group query timer.
286 * The state-change timer inm_sctimer is separate; whenever state changes
287 * for the group the state change record is generated and transmitted,
288 * and kept if retransmissions are necessary.
289 *
290 * The request count here is a count of requests for this address, not a
291 * count of pointers to this structure.
292 *
293 * FUTURE: inm_link is now only used when groups are being purged
294 * on a detaching ifnet.  It could be demoted to a SLIST_ENTRY.
295 */
296struct in_multi {
297	decl_lck_mtx_data(, inm_lock);
298	u_int32_t inm_refcount;		/* reference count */
299	u_int32_t inm_reqcnt;		/* request count for this address */
300	u_int32_t inm_debug;		/* see ifa_debug flags */
301	LIST_ENTRY(in_multi) inm_link;	/* queue macro glue */
302	struct	in_addr inm_addr;	/* IP multicast address, convenience */
303	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
304	struct	ifmultiaddr *inm_ifma;	/* back pointer to ifmultiaddr */
305	u_int	inm_timer;		/* IGMPv1/v2 group / v3 query timer  */
306	u_int	inm_state;		/*  state of the membership */
307	void *inm_rti;			/* unused, legacy field */
308
309	/* New fields for IGMPv3 follow. */
310	struct igmp_ifinfo	*inm_igi;	/* IGMP info */
311	SLIST_ENTRY(in_multi)	 inm_dtle;	/* detached waiting for rele */
312	SLIST_ENTRY(in_multi)	 inm_nrele;	/* to-be-released by IGMP */
313	u_int32_t		 inm_nrelecnt;	/* deferred release count */
314	struct ip_msource_tree	 inm_srcs;	/* tree of sources */
315	u_long			 inm_nsrc;	/* # of tree entries */
316
317	struct ifqueue		 inm_scq;	/* queue of pending
318						 * state-change packets */
319	struct timeval		 inm_lastgsrtv;	/* Time of last G-S-R query */
320	uint16_t		 inm_sctimer;	/* state-change timer */
321	uint16_t		 inm_scrv;	/* state-change rexmit count */
322
323	/*
324	 * SSM state counters which track state at T0 (the time the last
325	 * state-change report's RV timer went to zero) and T1
326	 * (time of pending report, i.e. now).
327	 * Used for computing IGMPv3 state-change reports. Several refcounts
328	 * are maintained here to optimize for common use-cases.
329	 */
330	struct inm_st {
331		uint16_t	iss_fmode;	/* IGMP filter mode */
332		uint16_t	iss_asm;	/* # of ASM listeners */
333		uint16_t	iss_ex;		/* # of exclusive members */
334		uint16_t	iss_in;		/* # of inclusive members */
335		uint16_t	iss_rec;	/* # of recorded sources */
336	}			inm_st[2];	/* state at t0, t1 */
337
338	void (*inm_trace)		/* callback fn for tracing refs */
339	    (struct in_multi *, int);
340};
341
342#define	INM_LOCK_ASSERT_HELD(_inm)					\
343	lck_mtx_assert(&(_inm)->inm_lock, LCK_MTX_ASSERT_OWNED)
344
345#define	INM_LOCK_ASSERT_NOTHELD(_inm)					\
346	lck_mtx_assert(&(_inm)->inm_lock, LCK_MTX_ASSERT_NOTOWNED)
347
348#define	INM_LOCK(_inm)							\
349	lck_mtx_lock(&(_inm)->inm_lock)
350
351#define	INM_LOCK_SPIN(_inm)						\
352	lck_mtx_lock_spin(&(_inm)->inm_lock)
353
354#define	INM_CONVERT_LOCK(_inm) do {					\
355	INM_LOCK_ASSERT_HELD(_inm);					\
356	lck_mtx_convert_spin(&(_inm)->inm_lock);			\
357} while (0)
358
359#define	INM_UNLOCK(_inm)						\
360	lck_mtx_unlock(&(_inm)->inm_lock)
361
362#define	INM_ADDREF(_inm)						\
363	inm_addref(_inm, 0)
364
365#define	INM_ADDREF_LOCKED(_inm)						\
366	inm_addref(_inm, 1)
367
368#define	INM_REMREF(_inm)						\
369	inm_remref(_inm, 0)
370
371#define	INM_REMREF_LOCKED(_inm)						\
372	inm_remref(_inm, 1)
373
374#ifdef SYSCTL_DECL
375SYSCTL_DECL(_net_inet_ip);
376SYSCTL_DECL(_net_inet_raw);
377#endif
378
379extern LIST_HEAD(in_multihead, in_multi) in_multihead;
380
381/*
382 * Structure used by macros below to remember position when stepping through
383 * all of the in_multi records.
384 */
385struct in_multistep {
386	struct in_multi *i_inm;
387};
388
389/*
390 * Macro for looking up the in_multi record for a given IP multicast address
391 * on a given interface.  If no matching record is found, "inm" is set null.
392 *
393 * We do this differently compared other BSD implementations; instead of
394 * walking the if_multiaddrs list at the interface and returning the
395 * ifma_protospec value of a matching entry, we search the global list
396 * of in_multi records and find it that way.  Otherwise either the two
397 * structures (in_multi, ifmultiaddr) need to be ref counted both ways,
398 * which will make things too complicated, or they need to reside in the
399 * same protected domain, which they aren't.
400 *
401 * Must be called with in_multihead_lock held.
402 */
403#define	IN_LOOKUP_MULTI(addr, ifp, inm)					\
404	/* struct in_addr *addr; */					\
405	/* struct ifnet *ifp; */					\
406	/* struct in_multi *inm; */					\
407do {									\
408	struct in_multistep _step;					\
409	IN_FIRST_MULTI(_step, inm);					\
410	while ((inm) != NULL) {						\
411		INM_LOCK_SPIN(inm);					\
412		if ((inm)->inm_ifp == (ifp) &&				\
413		    (inm)->inm_addr.s_addr == (addr)->s_addr) {		\
414			INM_ADDREF_LOCKED(inm);				\
415			INM_UNLOCK(inm);				\
416			break;						\
417		}							\
418		INM_UNLOCK(inm);					\
419		IN_NEXT_MULTI(_step, inm);				\
420	}								\
421} while (0)
422
423/*
424 * Macro to step through all of the in_multi records, one at a time.
425 * The current position is remembered in "step", which the caller must
426 * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
427 * and get the first record.  Both macros return a NULL "inm" when there
428 * are no remaining records.
429 *
430 * Must be called with in_multihead_lock held.
431 */
432#define	IN_NEXT_MULTI(step, inm)					\
433	/* struct in_multistep  step; */				\
434	/* struct in_multi *inm; */					\
435do {									\
436	in_multihead_lock_assert(LCK_RW_ASSERT_HELD);			\
437	if (((inm) = (step).i_inm) != NULL)				\
438		(step).i_inm = LIST_NEXT((step).i_inm, inm_link);	\
439} while (0)
440
441#define	IN_FIRST_MULTI(step, inm)					\
442	/* struct in_multistep step; */					\
443	/* struct in_multi *inm; */					\
444do {									\
445	in_multihead_lock_assert(LCK_RW_ASSERT_HELD);			\
446	(step).i_inm = LIST_FIRST(&in_multihead);			\
447	IN_NEXT_MULTI((step), (inm));					\
448} while (0)
449
450extern lck_mtx_t *inet_domain_mutex;
451extern struct domain *inetdomain;
452
453struct ip_moptions;
454struct inpcb;
455
456/*
457 * Return values for imo_multi_filter().
458 */
459#define	MCAST_PASS		0	/* Pass */
460#define	MCAST_NOTGMEMBER	1	/* This host not a member of group */
461#define	MCAST_NOTSMEMBER	2	/* This host excluded source */
462#define	MCAST_MUTED		3	/* [deprecated] */
463
464extern u_int32_t ipv4_ll_arp_aware;
465
466extern void in_ifaddr_init(void);
467extern int imo_multi_filter(const struct ip_moptions *, const struct ifnet *,
468    const struct sockaddr *, const struct sockaddr *);
469extern int imo_clone(struct inpcb *, struct inpcb *);
470extern void inm_commit(struct in_multi *);
471extern void inm_clear_recorded(struct in_multi *);
472extern void inm_print(const struct in_multi *);
473extern int inm_record_source(struct in_multi *inm, const in_addr_t);
474extern void inm_release(struct in_multi *);
475extern void in_multi_init(void);
476extern struct in_multi *in_addmulti(struct in_addr *, struct ifnet *);
477extern void in_delmulti(struct in_multi *);
478extern int in_leavegroup(struct in_multi *, struct in_mfilter *);
479extern int in_multi_detach(struct in_multi *);
480extern void inm_addref(struct in_multi *, int);
481extern void inm_remref(struct in_multi *, int);
482extern void inm_purge(struct in_multi *);
483extern uint8_t ims_get_mode(const struct in_multi *,
484    const struct ip_msource *, uint8_t);
485extern int in_control(struct socket *, u_long, caddr_t, struct ifnet *,
486    struct proc *);
487extern int in_inithead(void **, int);
488extern void in_rtqdrain(void);
489extern struct radix_node *in_validate(struct radix_node *);
490extern void ip_input(struct mbuf *);
491extern int in_ifadown(struct ifaddr *ifa, int);
492extern void in_ifscrub(struct ifnet *, struct in_ifaddr *, int);
493extern u_int32_t inaddr_hashval(u_int32_t);
494extern void in_purgeaddrs(struct ifnet *);
495extern int in_selectaddrs(int af, struct sockaddr_list **,
496    struct sockaddr_entry **, struct sockaddr_list **,
497    struct sockaddr_entry **);
498extern void gre_input(struct mbuf *, int);
499extern void imf_leave(struct in_mfilter *);
500extern void imf_purge(struct in_mfilter *);
501extern int inp_join_group(struct inpcb *, struct sockopt *);
502extern int inp_leave_group(struct inpcb *, struct sockopt *);
503extern void in_multihead_lock_exclusive(void);
504extern void in_multihead_lock_shared(void);
505extern void in_multihead_lock_assert(int);
506extern void in_multihead_lock_done(void);
507#endif /* BSD_KERNEL_PRIVATE */
508/* INET6 stuff */
509#include <netinet6/in6_var.h>
510#endif /* _NETINET_IN_VAR_H_ */
511