xref: /xnu-11215/bsd/net/ether_inet6_pr_module.c (revision 8d741a5d)
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) 1982, 1989, 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  */
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/mbuf.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
69 #include <sys/sysctl.h>
70 #include <sys/socketvar.h>
71 
72 #include <net/dlil.h>
73 #include <net/if.h>
74 #include <net/route.h>
75 #include <net/if_llc.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/ndrv.h>
79 #include <net/kpi_protocol.h>
80 #include <net/dlil.h>
81 
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 
88 #include <netinet6/nd6.h>
89 #include <netinet6/in6_ifattach.h>
90 #include <netinet6/ip6_var.h>
91 
92 /* #include "vlan.h" */
93 #if NVLAN > 0
94 #include <net/if_vlan_var.h>
95 #endif /* NVLAN > 0 */
96 
97 #include <net/ether_if_module.h>
98 
99 #include <net/sockaddr_utils.h>
100 
101 static const u_char etherip6allnodes[ETHER_ADDR_LEN] =
102 { 0x33, 0x33, 0, 0, 0, 1 };
103 
104 /*
105  * Process a received Ethernet packet;
106  * the packet is in the mbuf chain m without
107  * the ether header, which is provided separately.
108  */
109 static errno_t
ether_inet6_input(ifnet_t ifp,protocol_family_t protocol,mbuf_t packet,char * header)110 ether_inet6_input(ifnet_t ifp, protocol_family_t protocol,
111     mbuf_t packet, char *header)
112 {
113 #pragma unused(ifp, protocol)
114 	struct ether_header *eh = (struct ether_header *)(void *)header;
115 	u_int16_t ether_type;
116 
117 	bcopy(&eh->ether_type, &ether_type, sizeof(ether_type));
118 
119 	if (ether_type == htons(ETHERTYPE_IPV6)) {
120 		struct ifnet *mifp;
121 		/*
122 		 * Trust the ifp in the mbuf, rather than ifproto's
123 		 * since the packet could have been injected via
124 		 * a dlil_input_packet_list() using an ifp that is
125 		 * different than the one where the packet really
126 		 * came from.
127 		 */
128 		mifp = mbuf_pkthdr_rcvif(packet);
129 
130 		/* Update L2 reachability record, if present (and not bcast) */
131 		if (bcmp(eh->ether_shost, etherbroadcastaddr,
132 		    ETHER_ADDR_LEN) != 0) {
133 			nd6_llreach_set_reachable(mifp, eh->ether_shost,
134 			    ETHER_ADDR_LEN);
135 		}
136 
137 		/* Save the Ethernet source address for all-nodes multicasts */
138 		if (!bcmp(eh->ether_dhost, etherip6allnodes, ETHER_ADDR_LEN)) {
139 			struct ip6aux *ip6a;
140 
141 			ip6a = ip6_addaux(packet);
142 			if (ip6a) {
143 				ip6a->ip6a_flags |= IP6A_HASEEN;
144 				bcopy(eh->ether_shost, ip6a->ip6a_ehsrc,
145 				    ETHER_ADDR_LEN);
146 			}
147 		}
148 
149 		if (proto_input(protocol, packet) != 0) {
150 			m_freem(packet);
151 		}
152 	} else {
153 		m_freem(packet);
154 	}
155 
156 	return EJUSTRETURN;
157 }
158 
159 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 ether_inet6_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
161     mbuf_t *m0, const struct sockaddr *dst_netaddr, void *route,
162     char *type, char *edst)
163 {
164 #pragma unused(protocol_family)
165 	errno_t result;
166 	struct sockaddr_dl sdl = {};
167 	struct mbuf *m = *m0;
168 
169 	/*
170 	 * Tell ether_frameout it's ok to loop packet if necessary
171 	 */
172 	m->m_flags |= M_LOOP;
173 
174 	result = nd6_lookup_ipv6(ifp, SIN6(dst_netaddr), &sdl, sizeof(sdl), route, *m0);
175 
176 	if (result == 0) {
177 		u_int16_t ethertype_ipv6 = htons(ETHERTYPE_IPV6);
178 
179 		uint8_t *frametype = dlil_frame_type(type);
180 		bcopy(&ethertype_ipv6, frametype, sizeof(ethertype_ipv6));
181 
182 		uint8_t *laddr = dlil_link_addr(edst);
183 		bcopy(LLADDR(&sdl), laddr, sdl.sdl_alen);
184 	}
185 
186 	return result;
187 }
188 
189 static int
ether_inet6_resolve_multi(ifnet_t ifp,const struct sockaddr * proto_addr,struct sockaddr_dl * out_ll,size_t ll_len)190 ether_inet6_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
191     struct sockaddr_dl *out_ll, size_t ll_len)
192 {
193 	static const size_t minsize =
194 	    offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
195 	const struct sockaddr_in6 *sin6 =
196 	    SIN6(proto_addr);
197 
198 	if (proto_addr->sa_family != AF_INET6) {
199 		return EAFNOSUPPORT;
200 	}
201 
202 	if (proto_addr->sa_len < sizeof(struct sockaddr_in6)) {
203 		return EINVAL;
204 	}
205 
206 	if (ll_len < minsize) {
207 		return EMSGSIZE;
208 	}
209 
210 	SOCKADDR_ZERO(out_ll, minsize);
211 	out_ll->sdl_len = minsize;
212 	out_ll->sdl_family = AF_LINK;
213 	out_ll->sdl_index = ifp->if_index;
214 	out_ll->sdl_type = IFT_ETHER;
215 	out_ll->sdl_nlen = 0;
216 	out_ll->sdl_alen = ETHER_ADDR_LEN;
217 	out_ll->sdl_slen = 0;
218 	ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
219 
220 	return 0;
221 }
222 
223 static errno_t
ether_inet6_prmod_ioctl(ifnet_t ifp,protocol_family_t protocol_family,u_long command,void * data)224 ether_inet6_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
225     u_long command, void *data)
226 {
227 #pragma unused(protocol_family)
228 	int error = 0;
229 
230 	switch (command) {
231 	case SIOCSIFADDR:               /* struct ifaddr pointer */
232 		/*
233 		 * Note: caller of ifnet_ioctl() passes in pointer to
234 		 * struct ifaddr as parameter to SIOCSIFADDR, for legacy
235 		 * reasons.
236 		 */
237 		if ((ifp->if_flags & IFF_RUNNING) == 0) {
238 			ifnet_set_flags(ifp, IFF_UP, IFF_UP);
239 			ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
240 		}
241 		break;
242 
243 	case SIOCGIFADDR: {             /* struct ifreq */
244 		struct ifreq *ifr = (struct ifreq *)(void *)data;
245 		(void) ifnet_guarded_lladdr_copy_bytes(ifp,
246 		    ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
247 		break;
248 	}
249 
250 	default:
251 		error = EOPNOTSUPP;
252 		break;
253 	}
254 	return error;
255 }
256 
257 errno_t
ether_attach_inet6(struct ifnet * ifp,protocol_family_t protocol_family)258 ether_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
259 {
260 #pragma unused(protocol_family)
261 	struct ifnet_attach_proto_param proto = {};
262 	u_short en_6native = htons(ETHERTYPE_IPV6);
263 	struct ifnet_demux_desc demux[1] = {
264 		{ .type = DLIL_DESC_ETYPE2, .data = &en_6native,
265 		  .datalen = sizeof(en_6native) }
266 	};
267 	errno_t error;
268 
269 	proto.demux_list = demux;
270 	proto.demux_count = 1;
271 	proto.input = ether_inet6_input;
272 	proto.pre_output = ether_inet6_pre_output;
273 	proto.ioctl = ether_inet6_prmod_ioctl;
274 	proto.resolve = ether_inet6_resolve_multi;
275 	error = ifnet_attach_protocol(ifp, protocol_family, &proto);
276 	if (error && error != EEXIST) {
277 		printf("WARNING: %s can't attach ipv6 to %s\n", __func__,
278 		    if_name(ifp));
279 	}
280 
281 	return error;
282 }
283 
284 void
ether_detach_inet6(struct ifnet * ifp,protocol_family_t protocol_family)285 ether_detach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
286 {
287 	(void) ifnet_detach_protocol(ifp, protocol_family);
288 }
289