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