]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2010 Apple Inc. All rights reserved. |
b0d623f7 A |
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 | ||
9bccf70c A |
29 | /* $FreeBSD: src/sys/net/if_stf.c,v 1.1.2.6 2001/07/24 19:10:18 brooks Exp $ */ |
30 | /* $KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun Exp $ */ | |
31 | ||
32 | /* | |
33 | * Copyright (C) 2000 WIDE Project. | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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 | */ | |
2d21ac55 A |
60 | /* |
61 | * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce | |
62 | * support for mandatory and extensible security protections. This notice | |
63 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
64 | * Version 2.0. | |
65 | */ | |
9bccf70c A |
66 | |
67 | /* | |
68 | * 6to4 interface, based on RFC3056. | |
69 | * | |
70 | * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting. | |
71 | * There is no address mapping defined from IPv6 multicast address to IPv4 | |
72 | * address. Therefore, we do not have IFF_MULTICAST on the interface. | |
73 | * | |
74 | * Due to the lack of address mapping for link-local addresses, we cannot | |
75 | * throw packets toward link-local addresses (fe80::x). Also, we cannot throw | |
76 | * packets to link-local multicast addresses (ff02::x). | |
77 | * | |
78 | * Here are interesting symptoms due to the lack of link-local address: | |
79 | * | |
80 | * Unicast routing exchange: | |
81 | * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9, | |
82 | * and link-local addresses as nexthop. | |
83 | * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address | |
84 | * assigned to the link, and makes use of them. Also, HELLO packets use | |
85 | * link-local multicast addresses (ff02::5 and ff02::6). | |
86 | * - BGP4+: Maybe. You can only use global address as nexthop, and global | |
87 | * address as TCP endpoint address. | |
88 | * | |
89 | * Multicast routing protocols: | |
90 | * - PIM: Hello packet cannot be used to discover adjacent PIM routers. | |
91 | * Adjacent PIM routers must be configured manually (is it really spec-wise | |
92 | * correct thing to do?). | |
93 | * | |
94 | * ICMPv6: | |
95 | * - Redirects cannot be used due to the lack of link-local address. | |
96 | * | |
97 | * stf interface does not have, and will not need, a link-local address. | |
98 | * It seems to have no real benefit and does not help the above symptoms much. | |
99 | * Even if we assign link-locals to interface, we cannot really | |
100 | * use link-local unicast/multicast on top of 6to4 cloud (since there's no | |
101 | * encapsulation defined for link-local address), and the above analysis does | |
102 | * not change. RFC3056 does not mandate the assignment of link-local address | |
103 | * either. | |
104 | * | |
105 | * 6to4 interface has security issues. Refer to | |
106 | * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt | |
107 | * for details. The code tries to filter out some of malicious packets. | |
108 | * Note that there is no way to be 100% secure. | |
109 | */ | |
110 | ||
111 | #include <sys/param.h> | |
112 | #include <sys/systm.h> | |
113 | #include <sys/socket.h> | |
114 | #include <sys/sockio.h> | |
115 | #include <sys/mbuf.h> | |
116 | #include <sys/errno.h> | |
117 | #include <sys/protosw.h> | |
118 | #include <sys/kernel.h> | |
119 | #include <sys/syslog.h> | |
9bccf70c A |
120 | |
121 | #include <sys/malloc.h> | |
122 | ||
6d2010ae A |
123 | #include <kern/locks.h> |
124 | ||
9bccf70c A |
125 | #include <net/if.h> |
126 | #include <net/route.h> | |
9bccf70c | 127 | #include <net/if_types.h> |
9bccf70c A |
128 | |
129 | #include <netinet/in.h> | |
130 | #include <netinet/in_systm.h> | |
131 | #include <netinet/ip.h> | |
132 | #include <netinet/ip_var.h> | |
133 | #include <netinet/in_var.h> | |
134 | ||
135 | #include <netinet/ip6.h> | |
136 | #include <netinet6/ip6_var.h> | |
137 | #include <netinet6/in6_var.h> | |
138 | #include <netinet/ip_ecn.h> | |
139 | ||
140 | #include <netinet/ip_encap.h> | |
2d21ac55 A |
141 | #include <net/kpi_interface.h> |
142 | #include <net/kpi_protocol.h> | |
9bccf70c A |
143 | |
144 | ||
145 | #include <net/net_osdep.h> | |
146 | ||
147 | #include <net/bpf.h> | |
148 | ||
2d21ac55 A |
149 | #if CONFIG_MACF_NET |
150 | #include <security/mac_framework.h> | |
151 | #endif | |
152 | ||
2d21ac55 | 153 | #define GET_V4(x) ((const struct in_addr *)(&(x)->s6_addr16[1])) |
9bccf70c | 154 | |
6d2010ae A |
155 | static lck_grp_t *stf_mtx_grp; |
156 | ||
9bccf70c | 157 | struct stf_softc { |
2d21ac55 | 158 | ifnet_t sc_if; /* common area */ |
b0d623f7 | 159 | u_int32_t sc_protocol_family; /* dlil protocol attached */ |
9bccf70c A |
160 | union { |
161 | struct route __sc_ro4; | |
162 | struct route_in6 __sc_ro6; /* just for safety */ | |
163 | } __sc_ro46; | |
164 | #define sc_ro __sc_ro46.__sc_ro4 | |
6d2010ae | 165 | decl_lck_mtx_data(, sc_ro_mtx); |
9bccf70c | 166 | const struct encaptab *encap_cookie; |
2d21ac55 A |
167 | bpf_tap_mode tap_mode; |
168 | bpf_packet_func tap_callback; | |
9bccf70c A |
169 | }; |
170 | ||
91447636 | 171 | void stfattach (void); |
9bccf70c | 172 | |
9bccf70c | 173 | static int ip_stf_ttl = 40; |
6d2010ae | 174 | static int stf_init_done; |
9bccf70c | 175 | |
91447636 | 176 | static void in_stf_input(struct mbuf *, int); |
6d2010ae | 177 | static void stfinit(void); |
9bccf70c A |
178 | extern struct domain inetdomain; |
179 | struct protosw in_stf_protosw = | |
180 | { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR, | |
2d21ac55 A |
181 | in_stf_input, NULL, NULL, rip_ctloutput, |
182 | NULL, | |
6d2010ae | 183 | NULL, NULL, NULL, NULL, |
2d21ac55 | 184 | NULL, |
91447636 | 185 | &rip_usrreqs, |
2d21ac55 | 186 | NULL, rip_unlock, NULL, {NULL, NULL}, NULL, {0} |
9bccf70c A |
187 | }; |
188 | ||
91447636 A |
189 | static int stf_encapcheck(const struct mbuf *, int, int, void *); |
190 | static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *); | |
2d21ac55 A |
191 | int stf_pre_output(struct ifnet *, protocol_family_t, struct mbuf **, |
192 | const struct sockaddr *, void *, char *, char *); | |
193 | static int stf_checkaddr4(struct stf_softc *, const struct in_addr *, | |
91447636 A |
194 | struct ifnet *); |
195 | static int stf_checkaddr6(struct stf_softc *, struct in6_addr *, | |
196 | struct ifnet *); | |
197 | static void stf_rtrequest(int, struct rtentry *, struct sockaddr *); | |
b0d623f7 | 198 | static errno_t stf_ioctl(ifnet_t ifp, u_long cmd, void *data); |
2d21ac55 A |
199 | static errno_t stf_output(ifnet_t ifp, mbuf_t m); |
200 | ||
6d2010ae A |
201 | static void |
202 | stfinit(void) | |
203 | { | |
204 | if (!stf_init_done) { | |
205 | stf_mtx_grp = lck_grp_alloc_init("stf", LCK_GRP_ATTR_NULL); | |
206 | stf_init_done = 1; | |
207 | } | |
208 | } | |
209 | ||
2d21ac55 A |
210 | /* |
211 | * gif_input is the input handler for IP and IPv6 attached to gif | |
212 | */ | |
213 | static errno_t | |
214 | stf_media_input( | |
215 | __unused ifnet_t ifp, | |
216 | protocol_family_t protocol_family, | |
217 | mbuf_t m, | |
218 | __unused char *frame_header) | |
219 | { | |
6d2010ae A |
220 | if (proto_input(protocol_family, m) != 0) |
221 | m_freem(m); | |
2d21ac55 A |
222 | |
223 | return (0); | |
224 | } | |
225 | ||
226 | ||
227 | ||
228 | static errno_t | |
229 | stf_add_proto( | |
230 | ifnet_t ifp, | |
231 | protocol_family_t protocol_family, | |
232 | __unused const struct ifnet_demux_desc *demux_array, | |
233 | __unused u_int32_t demux_count) | |
234 | { | |
9bccf70c | 235 | /* Only one protocol may be attached at a time */ |
2d21ac55 | 236 | struct stf_softc* stf = ifnet_softc(ifp); |
91447636 A |
237 | if (stf->sc_protocol_family == 0) |
238 | stf->sc_protocol_family = protocol_family; | |
9bccf70c A |
239 | else { |
240 | printf("stf_add_proto: stf already has a proto\n"); | |
91447636 | 241 | return EBUSY; |
9bccf70c | 242 | } |
91447636 A |
243 | |
244 | return 0; | |
9bccf70c A |
245 | } |
246 | ||
2d21ac55 A |
247 | static errno_t |
248 | stf_del_proto( | |
249 | ifnet_t ifp, | |
250 | protocol_family_t protocol_family) | |
251 | { | |
252 | if (((struct stf_softc*)ifnet_softc(ifp))->sc_protocol_family == protocol_family) | |
253 | ((struct stf_softc*)ifnet_softc(ifp))->sc_protocol_family = 0; | |
254 | ||
9bccf70c A |
255 | return 0; |
256 | } | |
257 | ||
2d21ac55 A |
258 | static errno_t |
259 | stf_attach_inet6( | |
260 | ifnet_t ifp, | |
261 | protocol_family_t protocol_family) | |
262 | { | |
263 | struct ifnet_attach_proto_param reg; | |
264 | errno_t stat; | |
265 | ||
266 | if (protocol_family != PF_INET6) | |
267 | return EPROTONOSUPPORT; | |
9bccf70c | 268 | |
91447636 | 269 | bzero(®, sizeof(reg)); |
2d21ac55 A |
270 | reg.input = stf_media_input; |
271 | reg.pre_output = stf_pre_output; | |
9bccf70c | 272 | |
2d21ac55 A |
273 | stat = ifnet_attach_protocol(ifp, protocol_family, ®); |
274 | if (stat && stat != EEXIST) { | |
275 | printf("stf_attach_proto_family can't attach interface fam=%d\n", | |
276 | protocol_family); | |
277 | } | |
9bccf70c | 278 | |
55e303ae | 279 | return stat; |
9bccf70c A |
280 | } |
281 | ||
2d21ac55 | 282 | static errno_t |
91447636 | 283 | stf_demux( |
2d21ac55 A |
284 | ifnet_t ifp, |
285 | __unused mbuf_t m, | |
286 | __unused char *frame_ptr, | |
287 | protocol_family_t *protocol_family) | |
9bccf70c | 288 | { |
2d21ac55 A |
289 | struct stf_softc* stf = ifnet_softc(ifp); |
290 | *protocol_family = stf->sc_protocol_family; | |
91447636 | 291 | return 0; |
9bccf70c A |
292 | } |
293 | ||
2d21ac55 A |
294 | static errno_t |
295 | stf_set_bpf_tap( | |
296 | ifnet_t ifp, | |
297 | bpf_tap_mode mode, | |
298 | bpf_packet_func callback) | |
299 | { | |
300 | struct stf_softc *sc = ifnet_softc(ifp); | |
301 | ||
302 | sc->tap_mode = mode; | |
303 | sc->tap_callback = callback; | |
304 | ||
305 | return 0; | |
55e303ae | 306 | } |
9bccf70c A |
307 | |
308 | void | |
309 | stfattach(void) | |
310 | { | |
9bccf70c | 311 | struct stf_softc *sc; |
2d21ac55 | 312 | int error; |
9bccf70c | 313 | const struct encaptab *p; |
2d21ac55 | 314 | struct ifnet_init_params stf_init; |
9bccf70c | 315 | |
6d2010ae A |
316 | stfinit(); |
317 | ||
2d21ac55 A |
318 | error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_STF, |
319 | stf_attach_inet6, NULL); | |
320 | if (error != 0) | |
321 | printf("proto_register_plumber failed for AF_INET6 error=%d\n", error); | |
9bccf70c A |
322 | |
323 | sc = _MALLOC(sizeof(struct stf_softc), M_DEVBUF, M_WAITOK); | |
324 | if (sc == 0) { | |
325 | printf("stf softc attach failed\n" ); | |
326 | return; | |
327 | } | |
2d21ac55 | 328 | |
9bccf70c | 329 | bzero(sc, sizeof(*sc)); |
2d21ac55 | 330 | |
9bccf70c A |
331 | p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck, |
332 | &in_stf_protosw, sc); | |
333 | if (p == NULL) { | |
2d21ac55 | 334 | printf("sftattach encap_attach_func failed\n"); |
9bccf70c A |
335 | FREE(sc, M_DEVBUF); |
336 | return; | |
337 | } | |
338 | sc->encap_cookie = p; | |
6d2010ae | 339 | lck_mtx_init(&sc->sc_ro_mtx, stf_mtx_grp, LCK_ATTR_NULL); |
2d21ac55 A |
340 | |
341 | bzero(&stf_init, sizeof(stf_init)); | |
342 | stf_init.name = "stf"; | |
343 | stf_init.unit = 0; | |
344 | stf_init.type = IFT_STF; | |
345 | stf_init.family = IFNET_FAMILY_STF; | |
346 | stf_init.output = stf_output; | |
347 | stf_init.demux = stf_demux; | |
348 | stf_init.add_proto = stf_add_proto; | |
349 | stf_init.del_proto = stf_del_proto; | |
350 | stf_init.softc = sc; | |
351 | stf_init.ioctl = stf_ioctl; | |
352 | stf_init.set_bpf_tap = stf_set_bpf_tap; | |
353 | ||
354 | error = ifnet_allocate(&stf_init, &sc->sc_if); | |
355 | if (error != 0) { | |
356 | printf("stfattach, ifnet_allocate failed - %d\n", error); | |
357 | encap_detach(sc->encap_cookie); | |
6d2010ae | 358 | lck_mtx_destroy(&sc->sc_ro_mtx, stf_mtx_grp); |
2d21ac55 A |
359 | FREE(sc, M_DEVBUF); |
360 | return; | |
361 | } | |
362 | ifnet_set_mtu(sc->sc_if, IPV6_MMTU); | |
363 | ifnet_set_flags(sc->sc_if, 0, 0xffff); /* clear all flags */ | |
9bccf70c A |
364 | #if 0 |
365 | /* turn off ingress filter */ | |
2d21ac55 | 366 | ifnet_set_flags(sc->sc_if, IFF_LINK2, IFF_LINK2); |
9bccf70c | 367 | #endif |
9bccf70c | 368 | |
2d21ac55 A |
369 | #if CONFIG_MACF_NET |
370 | mac_ifnet_label_init(&sc->sc_if); | |
371 | #endif | |
372 | ||
373 | error = ifnet_attach(sc->sc_if, NULL); | |
374 | if (error != 0) { | |
375 | printf("stfattach: ifnet_attach returned error=%d\n", error); | |
376 | encap_detach(sc->encap_cookie); | |
377 | ifnet_release(sc->sc_if); | |
6d2010ae | 378 | lck_mtx_destroy(&sc->sc_ro_mtx, stf_mtx_grp); |
2d21ac55 A |
379 | FREE(sc, M_DEVBUF); |
380 | return; | |
381 | } | |
9bccf70c | 382 | |
2d21ac55 A |
383 | bpfattach(sc->sc_if, DLT_NULL, sizeof(u_int)); |
384 | ||
385 | return; | |
9bccf70c A |
386 | } |
387 | ||
388 | static int | |
2d21ac55 A |
389 | stf_encapcheck( |
390 | const struct mbuf *m, | |
391 | __unused int off, | |
392 | int proto, | |
393 | void *arg) | |
9bccf70c A |
394 | { |
395 | struct ip ip; | |
396 | struct in6_ifaddr *ia6; | |
397 | struct stf_softc *sc; | |
398 | struct in_addr a, b; | |
399 | ||
400 | sc = (struct stf_softc *)arg; | |
401 | if (sc == NULL) | |
402 | return 0; | |
403 | ||
2d21ac55 | 404 | if ((ifnet_flags(sc->sc_if) & IFF_UP) == 0) |
9bccf70c A |
405 | return 0; |
406 | ||
407 | /* IFF_LINK0 means "no decapsulation" */ | |
2d21ac55 | 408 | if ((ifnet_flags(sc->sc_if) & IFF_LINK0) != 0) |
9bccf70c A |
409 | return 0; |
410 | ||
411 | if (proto != IPPROTO_IPV6) | |
412 | return 0; | |
413 | ||
b0d623f7 | 414 | mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof(ip), &ip); |
9bccf70c A |
415 | |
416 | if (ip.ip_v != 4) | |
417 | return 0; | |
418 | ||
2d21ac55 | 419 | ia6 = stf_getsrcifa6(sc->sc_if); |
9bccf70c A |
420 | if (ia6 == NULL) |
421 | return 0; | |
422 | ||
423 | /* | |
424 | * check if IPv4 dst matches the IPv4 address derived from the | |
425 | * local 6to4 address. | |
426 | * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:... | |
427 | */ | |
6d2010ae | 428 | IFA_LOCK(&ia6->ia_ifa); |
9bccf70c | 429 | if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst, |
b0d623f7 | 430 | sizeof(ip.ip_dst)) != 0) { |
6d2010ae A |
431 | IFA_UNLOCK(&ia6->ia_ifa); |
432 | IFA_REMREF(&ia6->ia_ifa); | |
9bccf70c | 433 | return 0; |
b0d623f7 | 434 | } |
9bccf70c A |
435 | /* |
436 | * check if IPv4 src matches the IPv4 address derived from the | |
437 | * local 6to4 address masked by prefixmask. | |
438 | * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 | |
439 | * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 | |
440 | */ | |
441 | bzero(&a, sizeof(a)); | |
442 | a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr; | |
443 | a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr; | |
444 | b = ip.ip_src; | |
445 | b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr; | |
b0d623f7 | 446 | if (a.s_addr != b.s_addr) { |
6d2010ae A |
447 | IFA_UNLOCK(&ia6->ia_ifa); |
448 | IFA_REMREF(&ia6->ia_ifa); | |
9bccf70c | 449 | return 0; |
b0d623f7 | 450 | } |
9bccf70c | 451 | /* stf interface makes single side match only */ |
6d2010ae A |
452 | IFA_UNLOCK(&ia6->ia_ifa); |
453 | IFA_REMREF(&ia6->ia_ifa); | |
9bccf70c A |
454 | return 32; |
455 | } | |
456 | ||
457 | static struct in6_ifaddr * | |
2d21ac55 | 458 | stf_getsrcifa6(struct ifnet *ifp) |
9bccf70c A |
459 | { |
460 | struct ifaddr *ia; | |
461 | struct in_ifaddr *ia4; | |
462 | struct sockaddr_in6 *sin6; | |
463 | struct in_addr in; | |
464 | ||
91447636 | 465 | ifnet_lock_shared(ifp); |
6d2010ae A |
466 | for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next) { |
467 | IFA_LOCK(ia); | |
468 | if (ia->ifa_addr == NULL) { | |
469 | IFA_UNLOCK(ia); | |
9bccf70c | 470 | continue; |
6d2010ae A |
471 | } |
472 | if (ia->ifa_addr->sa_family != AF_INET6) { | |
473 | IFA_UNLOCK(ia); | |
9bccf70c | 474 | continue; |
6d2010ae | 475 | } |
9bccf70c | 476 | sin6 = (struct sockaddr_in6 *)ia->ifa_addr; |
6d2010ae A |
477 | if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { |
478 | IFA_UNLOCK(ia); | |
9bccf70c | 479 | continue; |
6d2010ae | 480 | } |
9bccf70c | 481 | bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in)); |
6d2010ae | 482 | IFA_UNLOCK(ia); |
b0d623f7 | 483 | lck_rw_lock_shared(in_ifaddr_rwlock); |
9bccf70c A |
484 | for (ia4 = TAILQ_FIRST(&in_ifaddrhead); |
485 | ia4; | |
486 | ia4 = TAILQ_NEXT(ia4, ia_link)) | |
487 | { | |
6d2010ae A |
488 | IFA_LOCK(&ia4->ia_ifa); |
489 | if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) { | |
490 | IFA_UNLOCK(&ia4->ia_ifa); | |
9bccf70c | 491 | break; |
6d2010ae A |
492 | } |
493 | IFA_UNLOCK(&ia4->ia_ifa); | |
9bccf70c | 494 | } |
b0d623f7 | 495 | lck_rw_done(in_ifaddr_rwlock); |
9bccf70c A |
496 | if (ia4 == NULL) |
497 | continue; | |
498 | ||
6d2010ae | 499 | IFA_ADDREF(ia); /* for caller */ |
91447636 | 500 | ifnet_lock_done(ifp); |
6d2010ae | 501 | return ((struct in6_ifaddr *)ia); |
9bccf70c | 502 | } |
91447636 | 503 | ifnet_lock_done(ifp); |
9bccf70c | 504 | |
6d2010ae | 505 | return (NULL); |
9bccf70c A |
506 | } |
507 | ||
508 | int | |
91447636 | 509 | stf_pre_output( |
2d21ac55 A |
510 | struct ifnet *ifp, |
511 | __unused protocol_family_t protocol_family, | |
512 | struct mbuf **m0, | |
513 | const struct sockaddr *dst, | |
514 | __unused void *route, | |
515 | __unused char *desk_linkaddr, | |
516 | __unused char *frame_type) | |
9bccf70c | 517 | { |
2d21ac55 | 518 | struct mbuf *m = *m0; |
9bccf70c | 519 | struct stf_softc *sc; |
2d21ac55 A |
520 | const struct sockaddr_in6 *dst6; |
521 | const struct in_addr *in4; | |
9bccf70c A |
522 | u_int8_t tos; |
523 | struct ip *ip; | |
524 | struct ip6_hdr *ip6; | |
525 | struct in6_ifaddr *ia6; | |
2d21ac55 | 526 | struct sockaddr_in *dst4; |
6d2010ae | 527 | struct ip_out_args ipoa = { IFSCOPE_NONE, 0 }; |
2d21ac55 | 528 | errno_t result = 0; |
9bccf70c | 529 | |
2d21ac55 A |
530 | sc = ifnet_softc(ifp); |
531 | dst6 = (const struct sockaddr_in6 *)dst; | |
9bccf70c A |
532 | |
533 | /* just in case */ | |
2d21ac55 | 534 | if ((ifnet_flags(ifp) & IFF_UP) == 0) { |
9bccf70c A |
535 | printf("stf: IFF_DOWN\n"); |
536 | return ENETDOWN; | |
537 | } | |
538 | ||
539 | /* | |
540 | * If we don't have an ip4 address that match my inner ip6 address, | |
541 | * we shouldn't generate output. Without this check, we'll end up | |
542 | * using wrong IPv4 source. | |
543 | */ | |
544 | ia6 = stf_getsrcifa6(ifp); | |
545 | if (ia6 == NULL) { | |
546 | return ENETDOWN; | |
547 | } | |
548 | ||
2d21ac55 | 549 | if (mbuf_len(m) < sizeof(*ip6)) { |
9bccf70c | 550 | m = m_pullup(m, sizeof(*ip6)); |
cc9f6e38 A |
551 | if (!m) { |
552 | *m0 = NULL; /* makes sure this won't be double freed */ | |
6d2010ae | 553 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c | 554 | return ENOBUFS; |
cc9f6e38 | 555 | } |
9bccf70c A |
556 | } |
557 | ip6 = mtod(m, struct ip6_hdr *); | |
558 | tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; | |
559 | ||
560 | /* | |
561 | * Pickup the right outer dst addr from the list of candidates. | |
562 | * ip6_dst has priority as it may be able to give us shorter IPv4 hops. | |
563 | */ | |
564 | if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst)) | |
565 | in4 = GET_V4(&ip6->ip6_dst); | |
566 | else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr)) | |
567 | in4 = GET_V4(&dst6->sin6_addr); | |
568 | else { | |
6d2010ae | 569 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c A |
570 | return ENETUNREACH; |
571 | } | |
572 | ||
573 | if (ifp->if_bpf) { | |
2d21ac55 | 574 | /* We need to prepend the address family as a four byte field. */ |
9bccf70c A |
575 | u_int32_t af = AF_INET6; |
576 | ||
2d21ac55 | 577 | bpf_tap_out(ifp, 0, m, &af, sizeof(af)); |
9bccf70c A |
578 | } |
579 | ||
580 | M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); | |
2d21ac55 | 581 | if (m && mbuf_len(m) < sizeof(struct ip)) |
9bccf70c | 582 | m = m_pullup(m, sizeof(struct ip)); |
cc9f6e38 A |
583 | if (m == NULL) { |
584 | *m0 = NULL; | |
6d2010ae | 585 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c | 586 | return ENOBUFS; |
cc9f6e38 | 587 | } |
9bccf70c A |
588 | ip = mtod(m, struct ip *); |
589 | ||
590 | bzero(ip, sizeof(*ip)); | |
591 | ||
6d2010ae | 592 | IFA_LOCK_SPIN(&ia6->ia_ifa); |
9bccf70c A |
593 | bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr), |
594 | &ip->ip_src, sizeof(ip->ip_src)); | |
6d2010ae | 595 | IFA_UNLOCK(&ia6->ia_ifa); |
9bccf70c A |
596 | bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst)); |
597 | ip->ip_p = IPPROTO_IPV6; | |
598 | ip->ip_ttl = ip_stf_ttl; | |
599 | ip->ip_len = m->m_pkthdr.len; /*host order*/ | |
600 | if (ifp->if_flags & IFF_LINK1) | |
601 | ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos); | |
602 | else | |
603 | ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos); | |
604 | ||
6d2010ae | 605 | lck_mtx_lock(&sc->sc_ro_mtx); |
9bccf70c A |
606 | dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst; |
607 | if (dst4->sin_family != AF_INET || | |
608 | bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) { | |
6d2010ae | 609 | /* cache route doesn't match: always the case during the first use */ |
9bccf70c A |
610 | dst4->sin_family = AF_INET; |
611 | dst4->sin_len = sizeof(struct sockaddr_in); | |
612 | bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr)); | |
613 | if (sc->sc_ro.ro_rt) { | |
91447636 | 614 | rtfree(sc->sc_ro.ro_rt); |
9bccf70c A |
615 | sc->sc_ro.ro_rt = NULL; |
616 | } | |
617 | } | |
618 | ||
6d2010ae A |
619 | result = ip_output_list(m, 0, NULL, &sc->sc_ro, IP_OUTARGS, NULL, &ipoa); |
620 | lck_mtx_unlock(&sc->sc_ro_mtx); | |
9bccf70c | 621 | |
2d21ac55 A |
622 | /* Assumption: ip_output will free mbuf on errors */ |
623 | /* All the output processing is done here, don't let stf_output be called */ | |
624 | if (result == 0) | |
625 | result = EJUSTRETURN; | |
626 | *m0 = NULL; | |
6d2010ae | 627 | IFA_REMREF(&ia6->ia_ifa); |
2d21ac55 | 628 | return result; |
9bccf70c | 629 | } |
2d21ac55 A |
630 | static errno_t |
631 | stf_output( | |
632 | __unused ifnet_t ifp, | |
633 | __unused mbuf_t m) | |
634 | { | |
635 | /* All processing is done in stf_pre_output | |
636 | * this shouldn't be called as the pre_output returns "EJUSTRETURN" | |
637 | */ | |
638 | return 0; | |
639 | } | |
9bccf70c A |
640 | |
641 | static int | |
2d21ac55 A |
642 | stf_checkaddr4( |
643 | struct stf_softc *sc, | |
644 | const struct in_addr *in, | |
645 | struct ifnet *inifp) /* incoming interface */ | |
9bccf70c A |
646 | { |
647 | struct in_ifaddr *ia4; | |
648 | ||
649 | /* | |
650 | * reject packets with the following address: | |
651 | * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 | |
652 | */ | |
653 | if (IN_MULTICAST(ntohl(in->s_addr))) | |
654 | return -1; | |
655 | switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { | |
656 | case 0: case 127: case 255: | |
657 | return -1; | |
658 | } | |
659 | ||
660 | /* | |
661 | * reject packets with broadcast | |
662 | */ | |
b0d623f7 | 663 | lck_rw_lock_shared(in_ifaddr_rwlock); |
9bccf70c A |
664 | for (ia4 = TAILQ_FIRST(&in_ifaddrhead); |
665 | ia4; | |
666 | ia4 = TAILQ_NEXT(ia4, ia_link)) | |
667 | { | |
6d2010ae A |
668 | IFA_LOCK(&ia4->ia_ifa); |
669 | if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) { | |
670 | IFA_UNLOCK(&ia4->ia_ifa); | |
9bccf70c | 671 | continue; |
6d2010ae | 672 | } |
91447636 | 673 | if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) { |
6d2010ae | 674 | IFA_UNLOCK(&ia4->ia_ifa); |
b0d623f7 | 675 | lck_rw_done(in_ifaddr_rwlock); |
9bccf70c | 676 | return -1; |
91447636 | 677 | } |
6d2010ae | 678 | IFA_UNLOCK(&ia4->ia_ifa); |
9bccf70c | 679 | } |
b0d623f7 | 680 | lck_rw_done(in_ifaddr_rwlock); |
9bccf70c A |
681 | |
682 | /* | |
683 | * perform ingress filter | |
684 | */ | |
2d21ac55 | 685 | if (sc && (ifnet_flags(sc->sc_if) & IFF_LINK2) == 0 && inifp) { |
9bccf70c A |
686 | struct sockaddr_in sin; |
687 | struct rtentry *rt; | |
688 | ||
689 | bzero(&sin, sizeof(sin)); | |
690 | sin.sin_family = AF_INET; | |
691 | sin.sin_len = sizeof(struct sockaddr_in); | |
692 | sin.sin_addr = *in; | |
b0d623f7 A |
693 | rt = rtalloc1((struct sockaddr *)&sin, 0, 0); |
694 | if (rt != NULL) | |
695 | RT_LOCK(rt); | |
696 | if (rt == NULL || rt->rt_ifp != inifp) { | |
9bccf70c A |
697 | #if 1 |
698 | log(LOG_WARNING, "%s: packet from 0x%x dropped " | |
2d21ac55 | 699 | "due to ingress filter\n", if_name(sc->sc_if), |
9bccf70c A |
700 | (u_int32_t)ntohl(sin.sin_addr.s_addr)); |
701 | #endif | |
b0d623f7 A |
702 | if (rt != NULL) { |
703 | RT_UNLOCK(rt); | |
9bccf70c | 704 | rtfree(rt); |
b0d623f7 | 705 | } |
9bccf70c A |
706 | return -1; |
707 | } | |
b0d623f7 | 708 | RT_UNLOCK(rt); |
9bccf70c A |
709 | rtfree(rt); |
710 | } | |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
715 | static int | |
2d21ac55 A |
716 | stf_checkaddr6( |
717 | struct stf_softc *sc, | |
718 | struct in6_addr *in6, | |
719 | struct ifnet *inifp) /* incoming interface */ | |
9bccf70c A |
720 | { |
721 | /* | |
722 | * check 6to4 addresses | |
723 | */ | |
724 | if (IN6_IS_ADDR_6TO4(in6)) | |
725 | return stf_checkaddr4(sc, GET_V4(in6), inifp); | |
726 | ||
727 | /* | |
728 | * reject anything that look suspicious. the test is implemented | |
729 | * in ip6_input too, but we check here as well to | |
730 | * (1) reject bad packets earlier, and | |
731 | * (2) to be safe against future ip6_input change. | |
732 | */ | |
733 | if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6)) | |
734 | return -1; | |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
91447636 | 739 | static void |
2d21ac55 A |
740 | in_stf_input( |
741 | struct mbuf *m, | |
742 | int off) | |
9bccf70c A |
743 | { |
744 | struct stf_softc *sc; | |
745 | struct ip *ip; | |
2d21ac55 | 746 | struct ip6_hdr ip6; |
9bccf70c | 747 | u_int8_t otos, itos; |
91447636 | 748 | int proto; |
9bccf70c | 749 | struct ifnet *ifp; |
2d21ac55 | 750 | struct ifnet_stat_increment_param stats; |
9bccf70c A |
751 | |
752 | ip = mtod(m, struct ip *); | |
753 | proto = ip->ip_p; | |
754 | ||
9bccf70c A |
755 | if (proto != IPPROTO_IPV6) { |
756 | m_freem(m); | |
757 | return; | |
758 | } | |
759 | ||
760 | ip = mtod(m, struct ip *); | |
761 | ||
762 | sc = (struct stf_softc *)encap_getarg(m); | |
763 | ||
2d21ac55 | 764 | if (sc == NULL || (ifnet_flags(sc->sc_if) & IFF_UP) == 0) { |
9bccf70c A |
765 | m_freem(m); |
766 | return; | |
767 | } | |
768 | ||
2d21ac55 A |
769 | ifp = sc->sc_if; |
770 | ||
771 | #if MAC_LABEL | |
772 | mac_mbuf_label_associate_ifnet(ifp, m); | |
773 | #endif | |
9bccf70c A |
774 | |
775 | /* | |
776 | * perform sanity check against outer src/dst. | |
777 | * for source, perform ingress filter as well. | |
778 | */ | |
779 | if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 || | |
780 | stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) { | |
781 | m_freem(m); | |
782 | return; | |
783 | } | |
784 | ||
785 | otos = ip->ip_tos; | |
2d21ac55 | 786 | mbuf_copydata(m, off, sizeof(ip6), &ip6); |
9bccf70c A |
787 | |
788 | /* | |
789 | * perform sanity check against inner src/dst. | |
790 | * for source, perform ingress filter as well. | |
791 | */ | |
2d21ac55 A |
792 | if (stf_checkaddr6(sc, &ip6.ip6_dst, NULL) < 0 || |
793 | stf_checkaddr6(sc, &ip6.ip6_src, m->m_pkthdr.rcvif) < 0) { | |
9bccf70c A |
794 | m_freem(m); |
795 | return; | |
796 | } | |
797 | ||
2d21ac55 A |
798 | itos = (ntohl(ip6.ip6_flow) >> 20) & 0xff; |
799 | if ((ifnet_flags(ifp) & IFF_LINK1) != 0) | |
9bccf70c A |
800 | ip_ecn_egress(ECN_ALLOWED, &otos, &itos); |
801 | else | |
802 | ip_ecn_egress(ECN_NOCARE, &otos, &itos); | |
2d21ac55 A |
803 | ip6.ip6_flow &= ~htonl(0xff << 20); |
804 | ip6.ip6_flow |= htonl((u_int32_t)itos << 20); | |
9bccf70c A |
805 | |
806 | m->m_pkthdr.rcvif = ifp; | |
2d21ac55 A |
807 | mbuf_pkthdr_setheader(m, mbuf_data(m)); |
808 | mbuf_adj(m, off); | |
9bccf70c A |
809 | |
810 | if (ifp->if_bpf) { | |
2d21ac55 | 811 | /* We need to prepend the address family as a four byte field. */ |
9bccf70c | 812 | u_int32_t af = AF_INET6; |
2d21ac55 | 813 | bpf_tap_in(ifp, 0, m, &af, sizeof(af)); |
9bccf70c A |
814 | } |
815 | ||
816 | /* | |
817 | * Put the packet to the network layer input queue according to the | |
818 | * specified address family. | |
819 | * See net/if_gif.c for possible issues with packet processing | |
820 | * reorder due to extra queueing. | |
821 | */ | |
2d21ac55 A |
822 | bzero(&stats, sizeof(stats)); |
823 | stats.packets_in = 1; | |
824 | stats.bytes_in = mbuf_pkthdr_len(m); | |
825 | mbuf_pkthdr_setrcvif(m, ifp); | |
826 | ifnet_input(ifp, m, &stats); | |
827 | ||
55e303ae | 828 | return; |
9bccf70c A |
829 | } |
830 | ||
9bccf70c | 831 | static void |
2d21ac55 A |
832 | stf_rtrequest( |
833 | __unused int cmd, | |
834 | struct rtentry *rt, | |
835 | __unused struct sockaddr *sa) | |
9bccf70c | 836 | { |
b0d623f7 A |
837 | if (rt != NULL) { |
838 | RT_LOCK_ASSERT_HELD(rt); | |
9bccf70c | 839 | rt->rt_rmx.rmx_mtu = IPV6_MMTU; |
b0d623f7 | 840 | } |
9bccf70c A |
841 | } |
842 | ||
2d21ac55 A |
843 | static errno_t |
844 | stf_ioctl( | |
845 | ifnet_t ifp, | |
b0d623f7 | 846 | u_long cmd, |
2d21ac55 | 847 | void *data) |
9bccf70c A |
848 | { |
849 | struct ifaddr *ifa; | |
850 | struct ifreq *ifr; | |
851 | struct sockaddr_in6 *sin6; | |
852 | int error; | |
853 | ||
854 | error = 0; | |
855 | switch (cmd) { | |
856 | case SIOCSIFADDR: | |
857 | ifa = (struct ifaddr *)data; | |
6d2010ae A |
858 | if (ifa == NULL) { |
859 | error = EAFNOSUPPORT; | |
860 | break; | |
861 | } | |
862 | IFA_LOCK(ifa); | |
863 | if (ifa->ifa_addr->sa_family != AF_INET6) { | |
864 | IFA_UNLOCK(ifa); | |
9bccf70c A |
865 | error = EAFNOSUPPORT; |
866 | break; | |
867 | } | |
868 | sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; | |
869 | if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { | |
91447636 A |
870 | if ( !(ifnet_flags( ifp ) & IFF_UP) ) { |
871 | /* do this only if the interface is not already up */ | |
872 | ifa->ifa_rtrequest = stf_rtrequest; | |
6d2010ae | 873 | IFA_UNLOCK(ifa); |
91447636 | 874 | ifnet_set_flags(ifp, IFF_UP, IFF_UP); |
6d2010ae A |
875 | } else { |
876 | IFA_UNLOCK(ifa); | |
91447636 | 877 | } |
6d2010ae A |
878 | } else { |
879 | IFA_UNLOCK(ifa); | |
9bccf70c | 880 | error = EINVAL; |
6d2010ae A |
881 | } |
882 | IFA_LOCK_ASSERT_NOTHELD(ifa); | |
9bccf70c A |
883 | break; |
884 | ||
885 | case SIOCADDMULTI: | |
886 | case SIOCDELMULTI: | |
887 | ifr = (struct ifreq *)data; | |
888 | if (ifr && ifr->ifr_addr.sa_family == AF_INET6) | |
889 | ; | |
890 | else | |
891 | error = EAFNOSUPPORT; | |
892 | break; | |
893 | ||
894 | default: | |
895 | error = EINVAL; | |
896 | break; | |
897 | } | |
898 | ||
899 | return error; | |
900 | } |