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