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