]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
de355530 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
de355530 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
de355530 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1989, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | */ | |
55 | ||
56 | ||
57 | ||
58 | #include <sys/param.h> | |
59 | #include <sys/systm.h> | |
60 | #include <sys/kernel.h> | |
61 | #include <sys/malloc.h> | |
62 | #include <sys/mbuf.h> | |
63 | #include <sys/socket.h> | |
64 | #include <sys/sockio.h> | |
65 | #include <sys/sysctl.h> | |
66 | ||
67 | #include <net/if.h> | |
68 | #include <net/netisr.h> | |
69 | #include <net/route.h> | |
70 | #include <net/if_llc.h> | |
71 | #include <net/if_dl.h> | |
72 | #include <net/if_types.h> | |
73 | #include <net/ndrv.h> | |
74 | ||
75 | #include <netinet/in.h> | |
76 | #include <netinet/in_var.h> | |
77 | #include <netinet/if_ether.h> | |
78 | #include <netinet/in_systm.h> | |
79 | #include <netinet/ip.h> | |
80 | ||
81 | #if INET6 | |
82 | #include <netinet6/nd6.h> | |
83 | #include <netinet6/in6_ifattach.h> | |
84 | #endif | |
85 | ||
86 | ||
87 | ||
88 | #include <sys/socketvar.h> | |
1c79356b A |
89 | |
90 | #include <net/dlil.h> | |
91 | ||
92 | ||
93 | #if LLC && CCITT | |
94 | extern struct ifqueue pkintrq; | |
95 | #endif | |
96 | ||
97 | ||
98 | #if BRIDGE | |
99 | #include <net/bridge.h> | |
100 | #endif | |
101 | ||
102 | /* #include "vlan.h" */ | |
103 | #if NVLAN > 0 | |
104 | #include <net/if_vlan_var.h> | |
105 | #endif /* NVLAN > 0 */ | |
106 | ||
1c79356b A |
107 | static u_long lo_dlt = 0; |
108 | static ivedonethis = 0; | |
109 | static u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | |
110 | ||
111 | #define IFP2AC(IFP) ((struct arpcom *)IFP) | |
112 | ||
113 | ||
114 | ||
115 | ||
116 | /* | |
117 | * Process a received Ethernet packet; | |
118 | * the packet is in the mbuf chain m without | |
119 | * the ether header, which is provided separately. | |
120 | */ | |
121 | int | |
122 | inet6_ether_input(m, frame_header, ifp, dl_tag, sync_ok) | |
123 | struct mbuf *m; | |
124 | char *frame_header; | |
125 | struct ifnet *ifp; | |
126 | u_long dl_tag; | |
127 | int sync_ok; | |
128 | ||
129 | { | |
130 | register struct ether_header *eh = (struct ether_header *) frame_header; | |
131 | register struct ifqueue *inq=0; | |
132 | u_short ether_type; | |
133 | int s; | |
134 | u_int16_t ptype = -1; | |
135 | unsigned char buf[18]; | |
136 | ||
137 | ||
138 | ||
139 | if ((ifp->if_flags & IFF_UP) == 0) { | |
140 | m_freem(m); | |
141 | return EJUSTRETURN; | |
142 | } | |
143 | ||
144 | ifp->if_lastchange = time; | |
145 | ||
146 | if (eh->ether_dhost[0] & 1) { | |
147 | if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, | |
148 | sizeof(etherbroadcastaddr)) == 0) | |
149 | m->m_flags |= M_BCAST; | |
150 | else | |
151 | m->m_flags |= M_MCAST; | |
152 | } | |
153 | if (m->m_flags & (M_BCAST|M_MCAST)) | |
154 | ifp->if_imcasts++; | |
155 | ||
156 | ether_type = ntohs(eh->ether_type); | |
157 | ||
158 | ||
159 | switch (ether_type) { | |
160 | ||
161 | case ETHERTYPE_IPV6: | |
162 | schednetisr(NETISR_IPV6); | |
163 | inq = &ip6intrq; | |
164 | break; | |
165 | ||
166 | default: { | |
167 | return ENOENT; | |
168 | } | |
169 | } | |
170 | ||
171 | if (inq == 0) | |
172 | return ENOENT; | |
173 | ||
174 | s = splimp(); | |
175 | if (IF_QFULL(inq)) { | |
176 | IF_DROP(inq); | |
177 | m_freem(m); | |
178 | splx(s); | |
179 | return EJUSTRETURN; | |
180 | } else | |
181 | IF_ENQUEUE(inq, m); | |
182 | splx(s); | |
183 | ||
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | ||
189 | ||
190 | ||
191 | int | |
192 | inet6_ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag ) | |
193 | struct ifnet *ifp; | |
194 | struct mbuf **m0; | |
195 | struct sockaddr *dst_netaddr; | |
196 | caddr_t route; | |
197 | char *type; | |
198 | char *edst; | |
199 | u_long dl_tag; | |
200 | { | |
201 | struct rtentry *rt0 = (struct rtentry *) route; | |
202 | int s; | |
203 | register struct mbuf *m = *m0; | |
204 | register struct rtentry *rt; | |
205 | register struct ether_header *eh; | |
1c79356b A |
206 | int hlen; /* link layer header lenght */ |
207 | struct arpcom *ac = IFP2AC(ifp); | |
208 | ||
209 | ||
210 | ||
211 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) | |
212 | return ENETDOWN; | |
213 | ||
214 | rt = rt0; | |
215 | if (rt) { | |
216 | if ((rt->rt_flags & RTF_UP) == 0) { | |
217 | rt0 = rt = rtalloc1(dst_netaddr, 1, 0UL); | |
218 | if (rt0) | |
9bccf70c | 219 | rtunref(rt); |
1c79356b A |
220 | else |
221 | return EHOSTUNREACH; | |
222 | } | |
223 | ||
224 | if (rt->rt_flags & RTF_GATEWAY) { | |
225 | if (rt->rt_gwroute == 0) | |
226 | goto lookup; | |
227 | if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { | |
228 | rtfree(rt); rt = rt0; | |
229 | lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, | |
230 | 0UL); | |
231 | if ((rt = rt->rt_gwroute) == 0) | |
232 | return (EHOSTUNREACH); | |
233 | } | |
234 | } | |
235 | ||
236 | ||
237 | if (rt->rt_flags & RTF_REJECT) | |
238 | if (rt->rt_rmx.rmx_expire == 0 || | |
239 | time_second < rt->rt_rmx.rmx_expire) | |
240 | return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); | |
241 | } | |
242 | ||
243 | hlen = ETHER_HDR_LEN; | |
244 | ||
245 | /* | |
246 | * Tell ether_frameout it's ok to loop packet unless negated below. | |
247 | */ | |
248 | m->m_flags |= M_LOOP; | |
249 | ||
250 | switch (dst_netaddr->sa_family) { | |
251 | ||
252 | ||
253 | case AF_INET6: | |
254 | if (!nd6_storelladdr(&ac->ac_if, rt, m, dst_netaddr, (u_char *)edst)) { | |
255 | /* this must be impossible, so we bark */ | |
256 | printf("nd6_storelladdr failed\n"); | |
257 | return(0); | |
258 | } | |
1c79356b A |
259 | *(u_short *)type = htons(ETHERTYPE_IPV6); |
260 | break; | |
261 | ||
262 | default: | |
263 | printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, | |
264 | dst_netaddr->sa_family); | |
265 | ||
266 | return EAFNOSUPPORT; | |
267 | } | |
268 | ||
269 | return (0); | |
270 | } | |
271 | ||
272 | ||
273 | int | |
274 | ether_inet6_prmod_ioctl(dl_tag, ifp, command, data) | |
275 | u_long dl_tag; | |
276 | struct ifnet *ifp; | |
277 | int command; | |
278 | caddr_t data; | |
279 | { | |
280 | struct ifaddr *ifa = (struct ifaddr *) data; | |
281 | struct ifreq *ifr = (struct ifreq *) data; | |
282 | struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data; | |
283 | int error = 0; | |
284 | boolean_t funnel_state; | |
285 | struct arpcom *ac = (struct arpcom *) ifp; | |
286 | struct sockaddr_dl *sdl; | |
287 | struct sockaddr_in *sin; | |
288 | struct sockaddr_in6 *sin6; | |
289 | ||
290 | u_char *e_addr; | |
291 | ||
292 | ||
1c79356b A |
293 | switch (command) { |
294 | case SIOCRSLVMULTI: { | |
295 | switch(rsreq->sa->sa_family) { | |
296 | ||
297 | case AF_INET6: | |
298 | sin6 = (struct sockaddr_in6 *)rsreq->sa; | |
299 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { | |
300 | /* | |
301 | * An IP6 address of 0 means listen to all | |
302 | * of the Ethernet multicast address used for IP6. | |
303 | * (This is used for multicast routers.) | |
304 | */ | |
305 | ifp->if_flags |= IFF_ALLMULTI; | |
306 | *rsreq->llsa = 0; | |
307 | return 0; | |
308 | } | |
309 | MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, | |
310 | M_WAITOK); | |
311 | sdl->sdl_len = sizeof *sdl; | |
312 | sdl->sdl_family = AF_LINK; | |
313 | sdl->sdl_index = ifp->if_index; | |
314 | sdl->sdl_type = IFT_ETHER; | |
315 | sdl->sdl_nlen = 0; | |
316 | sdl->sdl_alen = ETHER_ADDR_LEN; | |
317 | sdl->sdl_slen = 0; | |
318 | e_addr = LLADDR(sdl); | |
319 | ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); | |
9bccf70c | 320 | #ifndef __APPLE__ |
1c79356b A |
321 | printf("ether_resolvemulti AF_INET6 Adding %x:%x:%x:%x:%x:%x\n", |
322 | e_addr[0], e_addr[1], e_addr[2], e_addr[3], e_addr[4], e_addr[5]); | |
9bccf70c | 323 | #endif |
1c79356b A |
324 | *rsreq->llsa = (struct sockaddr *)sdl; |
325 | return 0; | |
326 | ||
327 | default: | |
328 | /* | |
329 | * Well, the text isn't quite right, but it's the name | |
330 | * that counts... | |
331 | */ | |
332 | return EAFNOSUPPORT; | |
333 | } | |
334 | ||
335 | } | |
336 | case SIOCSIFADDR: | |
337 | if ((ifp->if_flags & IFF_RUNNING) == 0) { | |
338 | ifp->if_flags |= IFF_UP; | |
339 | dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0); | |
340 | } | |
341 | ||
342 | break; | |
343 | ||
344 | case SIOCGIFADDR: | |
345 | { | |
346 | struct sockaddr *sa; | |
347 | ||
348 | sa = (struct sockaddr *) & ifr->ifr_data; | |
349 | bcopy(IFP2AC(ifp)->ac_enaddr, | |
350 | (caddr_t) sa->sa_data, ETHER_ADDR_LEN); | |
351 | } | |
352 | break; | |
353 | ||
354 | case SIOCSIFMTU: | |
355 | /* | |
9bccf70c | 356 | * IOKit IONetworkFamily will set the right MTU according to the driver |
1c79356b | 357 | */ |
9bccf70c A |
358 | |
359 | return (0); | |
1c79356b A |
360 | |
361 | default: | |
362 | return EOPNOTSUPP; | |
363 | } | |
364 | ||
1c79356b A |
365 | return (error); |
366 | } | |
367 | ||
368 | ||
369 | ||
370 | ||
371 | ||
372 | u_long ether_attach_inet6(struct ifnet *ifp) | |
373 | { | |
374 | struct dlil_proto_reg_str reg; | |
375 | struct dlil_demux_desc desc; | |
376 | u_long ip_dl_tag=0; | |
377 | u_short en_6native=ETHERTYPE_IPV6; | |
378 | int stat; | |
379 | int i; | |
380 | ||
381 | ||
382 | stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET6, &ip_dl_tag); | |
383 | if (stat == 0) | |
384 | return ip_dl_tag; | |
385 | ||
386 | TAILQ_INIT(®.demux_desc_head); | |
387 | desc.type = DLIL_DESC_RAW; | |
388 | desc.variants.bitmask.proto_id_length = 0; | |
389 | desc.variants.bitmask.proto_id = 0; | |
390 | desc.variants.bitmask.proto_id_mask = 0; | |
391 | desc.native_type = (char *) &en_6native; | |
392 | TAILQ_INSERT_TAIL(®.demux_desc_head, &desc, next); | |
393 | reg.interface_family = ifp->if_family; | |
394 | reg.unit_number = ifp->if_unit; | |
395 | reg.input = inet6_ether_input; | |
396 | reg.pre_output = inet6_ether_pre_output; | |
397 | reg.event = 0; | |
398 | reg.offer = 0; | |
399 | reg.ioctl = ether_inet6_prmod_ioctl; | |
0b4e3aa0 | 400 | reg.default_proto = 0; |
1c79356b A |
401 | reg.protocol_family = PF_INET6; |
402 | ||
403 | stat = dlil_attach_protocol(®, &ip_dl_tag); | |
404 | if (stat) { | |
405 | printf("WARNING: ether_attach_inet6 can't attach ip to interface\n"); | |
406 | return stat; | |
407 | } | |
408 | ||
409 | return ip_dl_tag; | |
410 | } | |
9bccf70c A |
411 | |
412 | int ether_detach_inet6(struct ifnet *ifp) | |
413 | { | |
414 | u_long ip_dl_tag = 0; | |
415 | int stat; | |
416 | ||
417 | stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET6, &ip_dl_tag); | |
418 | if (stat == 0) { | |
419 | stat = dlil_detach_protocol(ip_dl_tag); | |
420 | if (stat) { | |
421 | printf("WARNING: ether_detach_inet6 can't detach ip6 from interface\n"); | |
422 | } | |
423 | } | |
424 | return stat; | |
425 | } | |
426 |