xref: /xnu-8796.121.2/bsd/net/ether_inet6_pr_module.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions /*
29*c54f35caSApple OSS Distributions  * Copyright (c) 1982, 1989, 1993
30*c54f35caSApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*c54f35caSApple OSS Distributions  *
32*c54f35caSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*c54f35caSApple OSS Distributions  * modification, are permitted provided that the following conditions
34*c54f35caSApple OSS Distributions  * are met:
35*c54f35caSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*c54f35caSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*c54f35caSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*c54f35caSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*c54f35caSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*c54f35caSApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*c54f35caSApple OSS Distributions  *    must display the following acknowledgement:
42*c54f35caSApple OSS Distributions  *	This product includes software developed by the University of
43*c54f35caSApple OSS Distributions  *	California, Berkeley and its contributors.
44*c54f35caSApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*c54f35caSApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*c54f35caSApple OSS Distributions  *    without specific prior written permission.
47*c54f35caSApple OSS Distributions  *
48*c54f35caSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*c54f35caSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*c54f35caSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*c54f35caSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*c54f35caSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*c54f35caSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*c54f35caSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*c54f35caSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*c54f35caSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*c54f35caSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*c54f35caSApple OSS Distributions  * SUCH DAMAGE.
59*c54f35caSApple OSS Distributions  *
60*c54f35caSApple OSS Distributions  */
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions #include <sys/param.h>
63*c54f35caSApple OSS Distributions #include <sys/systm.h>
64*c54f35caSApple OSS Distributions #include <sys/kernel.h>
65*c54f35caSApple OSS Distributions #include <sys/malloc.h>
66*c54f35caSApple OSS Distributions #include <sys/mbuf.h>
67*c54f35caSApple OSS Distributions #include <sys/socket.h>
68*c54f35caSApple OSS Distributions #include <sys/sockio.h>
69*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
70*c54f35caSApple OSS Distributions #include <sys/socketvar.h>
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions #include <net/dlil.h>
73*c54f35caSApple OSS Distributions #include <net/if.h>
74*c54f35caSApple OSS Distributions #include <net/route.h>
75*c54f35caSApple OSS Distributions #include <net/if_llc.h>
76*c54f35caSApple OSS Distributions #include <net/if_dl.h>
77*c54f35caSApple OSS Distributions #include <net/if_types.h>
78*c54f35caSApple OSS Distributions #include <net/ndrv.h>
79*c54f35caSApple OSS Distributions #include <net/kpi_protocol.h>
80*c54f35caSApple OSS Distributions #include <net/dlil.h>
81*c54f35caSApple OSS Distributions 
82*c54f35caSApple OSS Distributions #include <netinet/in.h>
83*c54f35caSApple OSS Distributions #include <netinet/in_var.h>
84*c54f35caSApple OSS Distributions #include <netinet/if_ether.h>
85*c54f35caSApple OSS Distributions #include <netinet/in_systm.h>
86*c54f35caSApple OSS Distributions #include <netinet/ip.h>
87*c54f35caSApple OSS Distributions 
88*c54f35caSApple OSS Distributions #include <netinet6/nd6.h>
89*c54f35caSApple OSS Distributions #include <netinet6/in6_ifattach.h>
90*c54f35caSApple OSS Distributions #include <netinet6/ip6_var.h>
91*c54f35caSApple OSS Distributions 
92*c54f35caSApple OSS Distributions /* #include "vlan.h" */
93*c54f35caSApple OSS Distributions #if NVLAN > 0
94*c54f35caSApple OSS Distributions #include <net/if_vlan_var.h>
95*c54f35caSApple OSS Distributions #endif /* NVLAN > 0 */
96*c54f35caSApple OSS Distributions 
97*c54f35caSApple OSS Distributions #include <net/ether_if_module.h>
98*c54f35caSApple OSS Distributions 
99*c54f35caSApple OSS Distributions static const u_char etherip6allnodes[ETHER_ADDR_LEN] =
100*c54f35caSApple OSS Distributions { 0x33, 0x33, 0, 0, 0, 1 };
101*c54f35caSApple OSS Distributions 
102*c54f35caSApple OSS Distributions /*
103*c54f35caSApple OSS Distributions  * Process a received Ethernet packet;
104*c54f35caSApple OSS Distributions  * the packet is in the mbuf chain m without
105*c54f35caSApple OSS Distributions  * the ether header, which is provided separately.
106*c54f35caSApple OSS Distributions  */
107*c54f35caSApple OSS Distributions static errno_t
ether_inet6_input(ifnet_t ifp,protocol_family_t protocol,mbuf_t packet,char * header)108*c54f35caSApple OSS Distributions ether_inet6_input(ifnet_t ifp, protocol_family_t protocol,
109*c54f35caSApple OSS Distributions     mbuf_t packet, char *header)
110*c54f35caSApple OSS Distributions {
111*c54f35caSApple OSS Distributions #pragma unused(ifp, protocol)
112*c54f35caSApple OSS Distributions 	struct ether_header *eh = (struct ether_header *)(void *)header;
113*c54f35caSApple OSS Distributions 	u_int16_t ether_type;
114*c54f35caSApple OSS Distributions 
115*c54f35caSApple OSS Distributions 	bcopy(&eh->ether_type, &ether_type, sizeof(ether_type));
116*c54f35caSApple OSS Distributions 
117*c54f35caSApple OSS Distributions 	if (ether_type == htons(ETHERTYPE_IPV6)) {
118*c54f35caSApple OSS Distributions 		struct ifnet *mifp;
119*c54f35caSApple OSS Distributions 		/*
120*c54f35caSApple OSS Distributions 		 * Trust the ifp in the mbuf, rather than ifproto's
121*c54f35caSApple OSS Distributions 		 * since the packet could have been injected via
122*c54f35caSApple OSS Distributions 		 * a dlil_input_packet_list() using an ifp that is
123*c54f35caSApple OSS Distributions 		 * different than the one where the packet really
124*c54f35caSApple OSS Distributions 		 * came from.
125*c54f35caSApple OSS Distributions 		 */
126*c54f35caSApple OSS Distributions 		mifp = mbuf_pkthdr_rcvif(packet);
127*c54f35caSApple OSS Distributions 
128*c54f35caSApple OSS Distributions 		/* Update L2 reachability record, if present (and not bcast) */
129*c54f35caSApple OSS Distributions 		if (bcmp(eh->ether_shost, etherbroadcastaddr,
130*c54f35caSApple OSS Distributions 		    ETHER_ADDR_LEN) != 0) {
131*c54f35caSApple OSS Distributions 			nd6_llreach_set_reachable(mifp, eh->ether_shost,
132*c54f35caSApple OSS Distributions 			    ETHER_ADDR_LEN);
133*c54f35caSApple OSS Distributions 		}
134*c54f35caSApple OSS Distributions 
135*c54f35caSApple OSS Distributions 		/* Save the Ethernet source address for all-nodes multicasts */
136*c54f35caSApple OSS Distributions 		if (!bcmp(eh->ether_dhost, etherip6allnodes, ETHER_ADDR_LEN)) {
137*c54f35caSApple OSS Distributions 			struct ip6aux *ip6a;
138*c54f35caSApple OSS Distributions 
139*c54f35caSApple OSS Distributions 			ip6a = ip6_addaux(packet);
140*c54f35caSApple OSS Distributions 			if (ip6a) {
141*c54f35caSApple OSS Distributions 				ip6a->ip6a_flags |= IP6A_HASEEN;
142*c54f35caSApple OSS Distributions 				bcopy(eh->ether_shost, ip6a->ip6a_ehsrc,
143*c54f35caSApple OSS Distributions 				    ETHER_ADDR_LEN);
144*c54f35caSApple OSS Distributions 			}
145*c54f35caSApple OSS Distributions 		}
146*c54f35caSApple OSS Distributions 
147*c54f35caSApple OSS Distributions 		if (proto_input(protocol, packet) != 0) {
148*c54f35caSApple OSS Distributions 			m_freem(packet);
149*c54f35caSApple OSS Distributions 		}
150*c54f35caSApple OSS Distributions 	} else {
151*c54f35caSApple OSS Distributions 		m_freem(packet);
152*c54f35caSApple OSS Distributions 	}
153*c54f35caSApple OSS Distributions 
154*c54f35caSApple OSS Distributions 	return EJUSTRETURN;
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions 
157*c54f35caSApple OSS Distributions static errno_t
ether_inet6_pre_output(ifnet_t ifp,protocol_family_t protocol_family,mbuf_t * m0,const struct sockaddr * dst_netaddr,void * route,char * type,char * edst)158*c54f35caSApple OSS Distributions ether_inet6_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
159*c54f35caSApple OSS Distributions     mbuf_t *m0, const struct sockaddr *dst_netaddr, void *route,
160*c54f35caSApple OSS Distributions     char *type, char *edst)
161*c54f35caSApple OSS Distributions {
162*c54f35caSApple OSS Distributions #pragma unused(protocol_family)
163*c54f35caSApple OSS Distributions 	errno_t result;
164*c54f35caSApple OSS Distributions 	struct sockaddr_dl sdl = {};
165*c54f35caSApple OSS Distributions 	struct mbuf *m = *m0;
166*c54f35caSApple OSS Distributions 
167*c54f35caSApple OSS Distributions 	/*
168*c54f35caSApple OSS Distributions 	 * Tell ether_frameout it's ok to loop packet if necessary
169*c54f35caSApple OSS Distributions 	 */
170*c54f35caSApple OSS Distributions 	m->m_flags |= M_LOOP;
171*c54f35caSApple OSS Distributions 
172*c54f35caSApple OSS Distributions 	result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6 *)
173*c54f35caSApple OSS Distributions 	    (uintptr_t)(size_t)dst_netaddr, &sdl, sizeof(sdl), route, *m0);
174*c54f35caSApple OSS Distributions 
175*c54f35caSApple OSS Distributions 	if (result == 0) {
176*c54f35caSApple OSS Distributions 		u_int16_t ethertype_ipv6 = htons(ETHERTYPE_IPV6);
177*c54f35caSApple OSS Distributions 
178*c54f35caSApple OSS Distributions 		bcopy(&ethertype_ipv6, type, sizeof(ethertype_ipv6));
179*c54f35caSApple OSS Distributions 		bcopy(LLADDR(&sdl), edst, sdl.sdl_alen);
180*c54f35caSApple OSS Distributions 	}
181*c54f35caSApple OSS Distributions 
182*c54f35caSApple OSS Distributions 	return result;
183*c54f35caSApple OSS Distributions }
184*c54f35caSApple OSS Distributions 
185*c54f35caSApple OSS Distributions static int
ether_inet6_resolve_multi(ifnet_t ifp,const struct sockaddr * proto_addr,struct sockaddr_dl * out_ll,size_t ll_len)186*c54f35caSApple OSS Distributions ether_inet6_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
187*c54f35caSApple OSS Distributions     struct sockaddr_dl *out_ll, size_t ll_len)
188*c54f35caSApple OSS Distributions {
189*c54f35caSApple OSS Distributions 	static const size_t minsize =
190*c54f35caSApple OSS Distributions 	    offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
191*c54f35caSApple OSS Distributions 	const struct sockaddr_in6 *sin6 =
192*c54f35caSApple OSS Distributions 	    (const struct sockaddr_in6 *)(uintptr_t)(size_t)proto_addr;
193*c54f35caSApple OSS Distributions 
194*c54f35caSApple OSS Distributions 	if (proto_addr->sa_family != AF_INET6) {
195*c54f35caSApple OSS Distributions 		return EAFNOSUPPORT;
196*c54f35caSApple OSS Distributions 	}
197*c54f35caSApple OSS Distributions 
198*c54f35caSApple OSS Distributions 	if (proto_addr->sa_len < sizeof(struct sockaddr_in6)) {
199*c54f35caSApple OSS Distributions 		return EINVAL;
200*c54f35caSApple OSS Distributions 	}
201*c54f35caSApple OSS Distributions 
202*c54f35caSApple OSS Distributions 	if (ll_len < minsize) {
203*c54f35caSApple OSS Distributions 		return EMSGSIZE;
204*c54f35caSApple OSS Distributions 	}
205*c54f35caSApple OSS Distributions 
206*c54f35caSApple OSS Distributions 	bzero(out_ll, minsize);
207*c54f35caSApple OSS Distributions 	out_ll->sdl_len = minsize;
208*c54f35caSApple OSS Distributions 	out_ll->sdl_family = AF_LINK;
209*c54f35caSApple OSS Distributions 	out_ll->sdl_index = ifp->if_index;
210*c54f35caSApple OSS Distributions 	out_ll->sdl_type = IFT_ETHER;
211*c54f35caSApple OSS Distributions 	out_ll->sdl_nlen = 0;
212*c54f35caSApple OSS Distributions 	out_ll->sdl_alen = ETHER_ADDR_LEN;
213*c54f35caSApple OSS Distributions 	out_ll->sdl_slen = 0;
214*c54f35caSApple OSS Distributions 	ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
215*c54f35caSApple OSS Distributions 
216*c54f35caSApple OSS Distributions 	return 0;
217*c54f35caSApple OSS Distributions }
218*c54f35caSApple OSS Distributions 
219*c54f35caSApple OSS Distributions static errno_t
ether_inet6_prmod_ioctl(ifnet_t ifp,protocol_family_t protocol_family,u_long command,void * data)220*c54f35caSApple OSS Distributions ether_inet6_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
221*c54f35caSApple OSS Distributions     u_long command, void *data)
222*c54f35caSApple OSS Distributions {
223*c54f35caSApple OSS Distributions #pragma unused(protocol_family)
224*c54f35caSApple OSS Distributions 	int error = 0;
225*c54f35caSApple OSS Distributions 
226*c54f35caSApple OSS Distributions 	switch (command) {
227*c54f35caSApple OSS Distributions 	case SIOCSIFADDR:               /* struct ifaddr pointer */
228*c54f35caSApple OSS Distributions 		/*
229*c54f35caSApple OSS Distributions 		 * Note: caller of ifnet_ioctl() passes in pointer to
230*c54f35caSApple OSS Distributions 		 * struct ifaddr as parameter to SIOCSIFADDR, for legacy
231*c54f35caSApple OSS Distributions 		 * reasons.
232*c54f35caSApple OSS Distributions 		 */
233*c54f35caSApple OSS Distributions 		if ((ifp->if_flags & IFF_RUNNING) == 0) {
234*c54f35caSApple OSS Distributions 			ifnet_set_flags(ifp, IFF_UP, IFF_UP);
235*c54f35caSApple OSS Distributions 			ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
236*c54f35caSApple OSS Distributions 		}
237*c54f35caSApple OSS Distributions 		break;
238*c54f35caSApple OSS Distributions 
239*c54f35caSApple OSS Distributions 	case SIOCGIFADDR: {             /* struct ifreq */
240*c54f35caSApple OSS Distributions 		struct ifreq *ifr = (struct ifreq *)(void *)data;
241*c54f35caSApple OSS Distributions 		(void) ifnet_guarded_lladdr_copy_bytes(ifp,
242*c54f35caSApple OSS Distributions 		    ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
243*c54f35caSApple OSS Distributions 		break;
244*c54f35caSApple OSS Distributions 	}
245*c54f35caSApple OSS Distributions 
246*c54f35caSApple OSS Distributions 	default:
247*c54f35caSApple OSS Distributions 		error = EOPNOTSUPP;
248*c54f35caSApple OSS Distributions 		break;
249*c54f35caSApple OSS Distributions 	}
250*c54f35caSApple OSS Distributions 	return error;
251*c54f35caSApple OSS Distributions }
252*c54f35caSApple OSS Distributions 
253*c54f35caSApple OSS Distributions errno_t
ether_attach_inet6(struct ifnet * ifp,protocol_family_t protocol_family)254*c54f35caSApple OSS Distributions ether_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
255*c54f35caSApple OSS Distributions {
256*c54f35caSApple OSS Distributions #pragma unused(protocol_family)
257*c54f35caSApple OSS Distributions 	struct ifnet_attach_proto_param proto;
258*c54f35caSApple OSS Distributions 	struct ifnet_demux_desc demux[1];
259*c54f35caSApple OSS Distributions 	u_short en_6native = htons(ETHERTYPE_IPV6);
260*c54f35caSApple OSS Distributions 	errno_t error;
261*c54f35caSApple OSS Distributions 
262*c54f35caSApple OSS Distributions 	bzero(&proto, sizeof(proto));
263*c54f35caSApple OSS Distributions 	demux[0].type = DLIL_DESC_ETYPE2;
264*c54f35caSApple OSS Distributions 	demux[0].data = &en_6native;
265*c54f35caSApple OSS Distributions 	demux[0].datalen = sizeof(en_6native);
266*c54f35caSApple OSS Distributions 	proto.demux_list = demux;
267*c54f35caSApple OSS Distributions 	proto.demux_count = 1;
268*c54f35caSApple OSS Distributions 	proto.input = ether_inet6_input;
269*c54f35caSApple OSS Distributions 	proto.pre_output = ether_inet6_pre_output;
270*c54f35caSApple OSS Distributions 	proto.ioctl = ether_inet6_prmod_ioctl;
271*c54f35caSApple OSS Distributions 	proto.resolve = ether_inet6_resolve_multi;
272*c54f35caSApple OSS Distributions 	error = ifnet_attach_protocol(ifp, protocol_family, &proto);
273*c54f35caSApple OSS Distributions 	if (error && error != EEXIST) {
274*c54f35caSApple OSS Distributions 		printf("WARNING: %s can't attach ipv6 to %s\n", __func__,
275*c54f35caSApple OSS Distributions 		    if_name(ifp));
276*c54f35caSApple OSS Distributions 	}
277*c54f35caSApple OSS Distributions 
278*c54f35caSApple OSS Distributions 	return error;
279*c54f35caSApple OSS Distributions }
280*c54f35caSApple OSS Distributions 
281*c54f35caSApple OSS Distributions void
ether_detach_inet6(struct ifnet * ifp,protocol_family_t protocol_family)282*c54f35caSApple OSS Distributions ether_detach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
283*c54f35caSApple OSS Distributions {
284*c54f35caSApple OSS Distributions 	(void) ifnet_detach_protocol(ifp, protocol_family);
285*c54f35caSApple OSS Distributions }
286