]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/ether_inet6_pr_module.c
xnu-2050.18.24.tar.gz
[apple/xnu.git] / bsd / net / ether_inet6_pr_module.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
1c79356b
A
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>
6d2010ae 70#include <sys/socketvar.h>
91447636 71#include <kern/lock.h>
1c79356b
A
72
73#include <net/if.h>
1c79356b
A
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>
91447636 79#include <net/kpi_protocol.h>
6d2010ae 80#include <net/dlil.h>
1c79356b
A
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#if INET6
89#include <netinet6/nd6.h>
90#include <netinet6/in6_ifattach.h>
91#endif
92
1c79356b
A
93#if LLC && CCITT
94extern struct ifqueue pkintrq;
95#endif
96
1c79356b
A
97/* #include "vlan.h" */
98#if NVLAN > 0
99#include <net/if_vlan_var.h>
100#endif /* NVLAN > 0 */
101
2d21ac55 102#include <net/ether_if_module.h>
1c79356b
A
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 */
91447636 109static errno_t
6d2010ae
A
110ether_inet6_input(ifnet_t ifp, protocol_family_t protocol,
111 mbuf_t packet, char *header)
1c79356b 112{
6d2010ae 113#pragma unused(ifp, protocol)
316670eb
A
114 struct ether_header *eh = (struct ether_header *)(void *)header;
115 u_int16_t ether_type;
6d2010ae 116
316670eb
A
117 bcopy(&eh->ether_type, &ether_type, sizeof (ether_type));
118
119 if (ether_type == htons(ETHERTYPE_IPV6)) {
6d2010ae
A
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 }
2d21ac55 136
6d2010ae
A
137 if (proto_input(protocol, packet) != 0)
138 m_freem(packet);
139 } else {
2d21ac55 140 m_freem(packet);
6d2010ae
A
141 }
142
143 return (EJUSTRETURN);
91447636 144}
1c79356b 145
91447636 146static errno_t
6d2010ae
A
147ether_inet6_pre_output(ifnet_t ifp, protocol_family_t protocol_family,
148 mbuf_t *m0, const struct sockaddr *dst_netaddr, void *route,
149 char *type, char *edst)
91447636 150{
6d2010ae 151#pragma unused(protocol_family)
91447636 152 errno_t result;
6d2010ae
A
153 struct sockaddr_dl sdl;
154 struct mbuf *m = *m0;
1c79356b 155
91447636
A
156 /*
157 * Tell ether_frameout it's ok to loop packet if necessary
158 */
159 m->m_flags |= M_LOOP;
6d2010ae 160
316670eb
A
161 result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6 *)
162 (uintptr_t)(size_t)dst_netaddr, &sdl, sizeof (sdl), route, *m0);
6d2010ae 163
91447636 164 if (result == 0) {
316670eb
A
165 u_int16_t ethertype_ipv6 = htons(ETHERTYPE_IPV6);
166
167 bcopy(&ethertype_ipv6, type, sizeof (ethertype_ipv6));
91447636 168 bcopy(LLADDR(&sdl), edst, sdl.sdl_alen);
1c79356b 169 }
1c79356b 170
6d2010ae 171 return (result);
1c79356b
A
172}
173
91447636 174static int
6d2010ae
A
175ether_inet6_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr,
176 struct sockaddr_dl *out_ll, size_t ll_len)
1c79356b 177{
6d2010ae
A
178 static const size_t minsize =
179 offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
180 const struct sockaddr_in6 *sin6 =
316670eb 181 (const struct sockaddr_in6 *)(uintptr_t)(size_t)proto_addr;
6d2010ae 182
91447636 183 if (proto_addr->sa_family != AF_INET6)
6d2010ae
A
184 return (EAFNOSUPPORT);
185
186 if (proto_addr->sa_len < sizeof (struct sockaddr_in6))
187 return (EINVAL);
188
91447636 189 if (ll_len < minsize)
6d2010ae
A
190 return (EMSGSIZE);
191
91447636
A
192 bzero(out_ll, minsize);
193 out_ll->sdl_len = minsize;
194 out_ll->sdl_family = AF_LINK;
195 out_ll->sdl_index = ifp->if_index;
196 out_ll->sdl_type = IFT_ETHER;
197 out_ll->sdl_nlen = 0;
198 out_ll->sdl_alen = ETHER_ADDR_LEN;
199 out_ll->sdl_slen = 0;
200 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
1c79356b 201
6d2010ae
A
202 return (0);
203}
1c79356b 204
91447636 205static errno_t
6d2010ae
A
206ether_inet6_prmod_ioctl(ifnet_t ifp, protocol_family_t protocol_family,
207 u_long command, void *data)
1c79356b 208{
6d2010ae 209#pragma unused(protocol_family)
6d2010ae 210 int error = 0;
1c79356b 211
2d21ac55 212 switch (command) {
316670eb
A
213 case SIOCSIFADDR: /* struct ifaddr pointer */
214 /*
215 * Note: caller of ifnet_ioctl() passes in pointer to
216 * struct ifaddr as parameter to SIOCSIFADDR, for legacy
217 * reasons.
218 */
2d21ac55
A
219 if ((ifp->if_flags & IFF_RUNNING) == 0) {
220 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
221 ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
222 }
223 break;
1c79356b 224
316670eb
A
225 case SIOCGIFADDR: { /* struct ifreq */
226 struct ifreq *ifr = (struct ifreq *)(void *)data;
227
6d2010ae
A
228 (void) ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data,
229 ETHER_ADDR_LEN);
230 break;
316670eb 231 }
1c79356b 232
6d2010ae
A
233 default:
234 error = EOPNOTSUPP;
235 break;
236 }
237 return (error);
1c79356b
A
238}
239
2d21ac55 240errno_t
6d2010ae 241ether_attach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
1c79356b 242{
6d2010ae 243#pragma unused(protocol_family)
91447636
A
244 struct ifnet_attach_proto_param proto;
245 struct ifnet_demux_desc demux[1];
6d2010ae 246 u_short en_6native = htons(ETHERTYPE_IPV6);
91447636 247 errno_t error;
6d2010ae
A
248
249 bzero(&proto, sizeof (proto));
91447636
A
250 demux[0].type = DLIL_DESC_ETYPE2;
251 demux[0].data = &en_6native;
6d2010ae 252 demux[0].datalen = sizeof (en_6native);
91447636
A
253 proto.demux_list = demux;
254 proto.demux_count = 1;
2d21ac55
A
255 proto.input = ether_inet6_input;
256 proto.pre_output = ether_inet6_pre_output;
91447636
A
257 proto.ioctl = ether_inet6_prmod_ioctl;
258 proto.resolve = ether_inet6_resolve_multi;
259 error = ifnet_attach_protocol(ifp, protocol_family, &proto);
260 if (error && error != EEXIST) {
6d2010ae
A
261 printf("WARNING: %s can't attach ipv6 to %s%d\n", __func__,
262 ifp->if_name, ifp->if_unit);
91447636 263 }
6d2010ae
A
264
265 return (error);
1c79356b 266}
9bccf70c 267
2d21ac55 268void
6d2010ae 269ether_detach_inet6(struct ifnet *ifp, protocol_family_t protocol_family)
9bccf70c 270{
6d2010ae 271 (void) ifnet_detach_protocol(ifp, protocol_family);
9bccf70c 272}