]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ether_inet6_pr_module.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / net / ether_inet6_pr_module.c
1 /*
2 * Copyright (c) 2000-2009 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
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
72 #include <kern/lock.h>
73
74 #include <net/if.h>
75 #include <net/route.h>
76 #include <net/if_llc.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/ndrv.h>
80 #include <net/kpi_protocol.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 #if INET6
89 #include <netinet6/nd6.h>
90 #include <netinet6/in6_ifattach.h>
91 #endif
92
93
94
95 #include <sys/socketvar.h>
96
97 #include <net/dlil.h>
98
99
100 #if LLC && CCITT
101 extern struct ifqueue pkintrq;
102 #endif
103
104 /* #include "vlan.h" */
105 #if NVLAN > 0
106 #include <net/if_vlan_var.h>
107 #endif /* NVLAN > 0 */
108
109 #include <net/ether_if_module.h>
110
111 /*
112 * Process a received Ethernet packet;
113 * the packet is in the mbuf chain m without
114 * the ether header, which is provided separately.
115 */
116 static errno_t
117 ether_inet6_input(
118 __unused ifnet_t ifp,
119 protocol_family_t protocol,
120 mbuf_t packet,
121 __unused char *header)
122 {
123 errno_t error;
124
125 if ((error = proto_input(protocol, packet)))
126 m_freem(packet);
127 return error;
128 }
129
130 static errno_t
131 ether_inet6_pre_output(
132 ifnet_t ifp,
133 __unused protocol_family_t protocol_family,
134 mbuf_t *m0,
135 const struct sockaddr *dst_netaddr,
136 void *route,
137 char *type,
138 char *edst)
139 {
140 errno_t result;
141 struct sockaddr_dl sdl;
142 register struct mbuf *m = *m0;
143
144 /*
145 * Tell ether_frameout it's ok to loop packet if necessary
146 */
147 m->m_flags |= M_LOOP;
148
149 result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6*)dst_netaddr,
150 &sdl, sizeof(sdl), route, *m0);
151
152 if (result == 0) {
153 *(u_int16_t*)type = htons(ETHERTYPE_IPV6);
154 bcopy(LLADDR(&sdl), edst, sdl.sdl_alen);
155 }
156
157
158
159 return result;
160 }
161
162 static int
163 ether_inet6_resolve_multi(
164 ifnet_t ifp,
165 const struct sockaddr *proto_addr,
166 struct sockaddr_dl *out_ll,
167 size_t ll_len)
168 {
169 static const size_t minsize = offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
170 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6*)proto_addr;
171
172 if (proto_addr->sa_family != AF_INET6)
173 return EAFNOSUPPORT;
174
175 if (proto_addr->sa_len < sizeof(struct sockaddr_in6))
176 return EINVAL;
177
178 if (ll_len < minsize)
179 return EMSGSIZE;
180
181 bzero(out_ll, minsize);
182 out_ll->sdl_len = minsize;
183 out_ll->sdl_family = AF_LINK;
184 out_ll->sdl_index = ifp->if_index;
185 out_ll->sdl_type = IFT_ETHER;
186 out_ll->sdl_nlen = 0;
187 out_ll->sdl_alen = ETHER_ADDR_LEN;
188 out_ll->sdl_slen = 0;
189 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
190
191 return 0;
192 }
193
194
195 static errno_t
196 ether_inet6_prmod_ioctl(
197 ifnet_t ifp,
198 __unused protocol_family_t protocol_family,
199 u_long command,
200 void *data)
201 {
202 struct ifreq *ifr = (struct ifreq *) data;
203 int error = 0;
204
205 switch (command) {
206 case SIOCSIFADDR:
207 if ((ifp->if_flags & IFF_RUNNING) == 0) {
208 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
209 ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
210 }
211 break;
212
213 case SIOCGIFADDR:
214 ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
215 break;
216
217 default:
218 error = EOPNOTSUPP;
219 break;
220 }
221 return (error);
222 }
223
224 errno_t
225 ether_attach_inet6(
226 struct ifnet *ifp,
227 __unused protocol_family_t protocol_family)
228 {
229 struct ifnet_attach_proto_param proto;
230 struct ifnet_demux_desc demux[1];
231 u_short en_6native=htons(ETHERTYPE_IPV6);
232 errno_t error;
233
234 bzero(&proto, sizeof(proto));
235 demux[0].type = DLIL_DESC_ETYPE2;
236 demux[0].data = &en_6native;
237 demux[0].datalen = sizeof(en_6native);
238 proto.demux_list = demux;
239 proto.demux_count = 1;
240 proto.input = ether_inet6_input;
241 proto.pre_output = ether_inet6_pre_output;
242 proto.ioctl = ether_inet6_prmod_ioctl;
243 proto.resolve = ether_inet6_resolve_multi;
244 error = ifnet_attach_protocol(ifp, protocol_family, &proto);
245 if (error && error != EEXIST) {
246 printf("WARNING: ether_attach_inet6 can't attach ipv6 to %s%d\n",
247 ifp->if_name, ifp->if_unit);
248 }
249
250 return error;
251 }
252
253 void
254 ether_detach_inet6(
255 struct ifnet *ifp,
256 protocol_family_t protocol_family)
257 {
258 errno_t error;
259
260 error = ifnet_detach_protocol(ifp, protocol_family);
261 if (error && error != ENOENT) {
262 printf("WARNING: ether_detach_inet6 can't detach ipv6 from %s%d\n",
263 ifp->if_name, ifp->if_unit);
264 }
265 }
266