]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
eb6b6ca3 | 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
b0d623f7 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
b0d623f7 A |
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. | |
0a7de745 | 14 | * |
b0d623f7 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
b0d623f7 A |
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. | |
0a7de745 | 25 | * |
b0d623f7 A |
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 | * | |
0a7de745 | 97 | * stf interface does not have, and will not need, a link-local address. |
9bccf70c A |
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 | ||
0a7de745 | 149 | #define GET_V4(x) ((const struct in_addr *)(const void *)(&(x)->s6_addr16[1])) |
9bccf70c | 150 | |
6d2010ae A |
151 | static lck_grp_t *stf_mtx_grp; |
152 | ||
9bccf70c | 153 | struct stf_softc { |
0a7de745 A |
154 | ifnet_t sc_if; /* common area */ |
155 | u_int32_t sc_protocol_family; /* dlil protocol attached */ | |
9bccf70c A |
156 | union { |
157 | struct route __sc_ro4; | |
158 | struct route_in6 __sc_ro6; /* just for safety */ | |
159 | } __sc_ro46; | |
0a7de745 | 160 | #define sc_ro __sc_ro46.__sc_ro4 |
6d2010ae | 161 | decl_lck_mtx_data(, sc_ro_mtx); |
9bccf70c | 162 | const struct encaptab *encap_cookie; |
0a7de745 A |
163 | bpf_tap_mode tap_mode; |
164 | bpf_packet_func tap_callback; | |
9bccf70c A |
165 | }; |
166 | ||
0a7de745 | 167 | void stfattach(void); |
9bccf70c | 168 | |
9bccf70c | 169 | static int ip_stf_ttl = 40; |
6d2010ae | 170 | static int stf_init_done; |
9bccf70c | 171 | |
91447636 | 172 | static void in_stf_input(struct mbuf *, int); |
6d2010ae | 173 | static void stfinit(void); |
39236c6e A |
174 | |
175 | static struct protosw in_stf_protosw = | |
176 | { | |
0a7de745 A |
177 | .pr_type = SOCK_RAW, |
178 | .pr_protocol = IPPROTO_IPV6, | |
179 | .pr_flags = PR_ATOMIC | PR_ADDR, | |
180 | .pr_input = in_stf_input, | |
181 | .pr_ctloutput = rip_ctloutput, | |
182 | .pr_usrreqs = &rip_usrreqs, | |
183 | .pr_unlock = rip_unlock, | |
9bccf70c A |
184 | }; |
185 | ||
91447636 A |
186 | static int stf_encapcheck(const struct mbuf *, int, int, void *); |
187 | static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *); | |
2d21ac55 | 188 | int stf_pre_output(struct ifnet *, protocol_family_t, struct mbuf **, |
0a7de745 | 189 | const struct sockaddr *, void *, char *, char *); |
2d21ac55 | 190 | static int stf_checkaddr4(struct stf_softc *, const struct in_addr *, |
0a7de745 | 191 | struct ifnet *); |
91447636 | 192 | static int stf_checkaddr6(struct stf_softc *, struct in6_addr *, |
0a7de745 | 193 | struct ifnet *); |
91447636 | 194 | static void stf_rtrequest(int, struct rtentry *, struct sockaddr *); |
b0d623f7 | 195 | static errno_t stf_ioctl(ifnet_t ifp, u_long cmd, void *data); |
2d21ac55 A |
196 | static errno_t stf_output(ifnet_t ifp, mbuf_t m); |
197 | ||
6d2010ae A |
198 | static void |
199 | stfinit(void) | |
200 | { | |
201 | if (!stf_init_done) { | |
202 | stf_mtx_grp = lck_grp_alloc_init("stf", LCK_GRP_ATTR_NULL); | |
203 | stf_init_done = 1; | |
204 | } | |
205 | } | |
206 | ||
2d21ac55 A |
207 | /* |
208 | * gif_input is the input handler for IP and IPv6 attached to gif | |
209 | */ | |
210 | static errno_t | |
211 | stf_media_input( | |
0a7de745 A |
212 | __unused ifnet_t ifp, |
213 | protocol_family_t protocol_family, | |
214 | mbuf_t m, | |
215 | __unused char *frame_header) | |
2d21ac55 | 216 | { |
0a7de745 | 217 | if (proto_input(protocol_family, m) != 0) { |
6d2010ae | 218 | m_freem(m); |
0a7de745 | 219 | } |
2d21ac55 | 220 | |
0a7de745 | 221 | return 0; |
2d21ac55 A |
222 | } |
223 | ||
224 | ||
225 | ||
226 | static errno_t | |
227 | stf_add_proto( | |
0a7de745 A |
228 | ifnet_t ifp, |
229 | protocol_family_t protocol_family, | |
230 | __unused const struct ifnet_demux_desc *demux_array, | |
231 | __unused u_int32_t demux_count) | |
2d21ac55 | 232 | { |
9bccf70c | 233 | /* Only one protocol may be attached at a time */ |
2d21ac55 | 234 | struct stf_softc* stf = ifnet_softc(ifp); |
0a7de745 | 235 | if (stf->sc_protocol_family == 0) { |
91447636 | 236 | stf->sc_protocol_family = protocol_family; |
0a7de745 | 237 | } else { |
9bccf70c | 238 | printf("stf_add_proto: stf already has a proto\n"); |
91447636 | 239 | return EBUSY; |
9bccf70c | 240 | } |
0a7de745 | 241 | |
91447636 | 242 | return 0; |
9bccf70c A |
243 | } |
244 | ||
2d21ac55 A |
245 | static errno_t |
246 | stf_del_proto( | |
0a7de745 A |
247 | ifnet_t ifp, |
248 | protocol_family_t protocol_family) | |
2d21ac55 | 249 | { |
0a7de745 | 250 | if (((struct stf_softc*)ifnet_softc(ifp))->sc_protocol_family == protocol_family) { |
2d21ac55 | 251 | ((struct stf_softc*)ifnet_softc(ifp))->sc_protocol_family = 0; |
0a7de745 A |
252 | } |
253 | ||
9bccf70c A |
254 | return 0; |
255 | } | |
256 | ||
2d21ac55 A |
257 | static errno_t |
258 | stf_attach_inet6( | |
0a7de745 A |
259 | ifnet_t ifp, |
260 | protocol_family_t protocol_family) | |
2d21ac55 | 261 | { |
0a7de745 A |
262 | struct ifnet_attach_proto_param reg; |
263 | errno_t stat; | |
264 | ||
265 | if (protocol_family != PF_INET6) { | |
266 | return EPROTONOSUPPORT; | |
267 | } | |
9bccf70c | 268 | |
91447636 | 269 | bzero(®, sizeof(reg)); |
0a7de745 A |
270 | reg.input = stf_media_input; |
271 | reg.pre_output = stf_pre_output; | |
9bccf70c | 272 | |
0a7de745 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 | |
0a7de745 | 279 | return stat; |
9bccf70c A |
280 | } |
281 | ||
2d21ac55 | 282 | static errno_t |
91447636 | 283 | stf_demux( |
0a7de745 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( | |
0a7de745 A |
296 | ifnet_t ifp, |
297 | bpf_tap_mode mode, | |
298 | bpf_packet_func callback) | |
2d21ac55 | 299 | { |
0a7de745 A |
300 | struct stf_softc *sc = ifnet_softc(ifp); |
301 | ||
2d21ac55 A |
302 | sc->tap_mode = mode; |
303 | sc->tap_callback = callback; | |
0a7de745 | 304 | |
2d21ac55 | 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; |
0a7de745 | 314 | struct ifnet_init_eparams stf_init; |
9bccf70c | 315 | |
6d2010ae A |
316 | stfinit(); |
317 | ||
2d21ac55 | 318 | error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_STF, |
d9a64523 | 319 | stf_attach_inet6, NULL); |
0a7de745 | 320 | if (error != 0) { |
2d21ac55 | 321 | printf("proto_register_plumber failed for AF_INET6 error=%d\n", error); |
0a7de745 | 322 | } |
9bccf70c | 323 | |
3e170ce0 | 324 | sc = _MALLOC(sizeof(struct stf_softc), M_DEVBUF, M_WAITOK | M_ZERO); |
9bccf70c A |
325 | if (sc == 0) { |
326 | printf("stf softc attach failed\n" ); | |
327 | return; | |
328 | } | |
0a7de745 | 329 | |
9bccf70c A |
330 | p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck, |
331 | &in_stf_protosw, sc); | |
332 | if (p == NULL) { | |
2d21ac55 | 333 | printf("sftattach encap_attach_func failed\n"); |
9bccf70c A |
334 | FREE(sc, M_DEVBUF); |
335 | return; | |
336 | } | |
337 | sc->encap_cookie = p; | |
6d2010ae | 338 | lck_mtx_init(&sc->sc_ro_mtx, stf_mtx_grp, LCK_ATTR_NULL); |
0a7de745 | 339 | |
2d21ac55 | 340 | bzero(&stf_init, sizeof(stf_init)); |
5ba3f43e | 341 | stf_init.ver = IFNET_INIT_CURRENT_VERSION; |
0a7de745 | 342 | stf_init.len = sizeof(stf_init); |
5ba3f43e | 343 | stf_init.flags = IFNET_INIT_LEGACY; |
2d21ac55 A |
344 | stf_init.name = "stf"; |
345 | stf_init.unit = 0; | |
346 | stf_init.type = IFT_STF; | |
347 | stf_init.family = IFNET_FAMILY_STF; | |
348 | stf_init.output = stf_output; | |
349 | stf_init.demux = stf_demux; | |
350 | stf_init.add_proto = stf_add_proto; | |
351 | stf_init.del_proto = stf_del_proto; | |
352 | stf_init.softc = sc; | |
353 | stf_init.ioctl = stf_ioctl; | |
354 | stf_init.set_bpf_tap = stf_set_bpf_tap; | |
0a7de745 | 355 | |
5ba3f43e | 356 | error = ifnet_allocate_extended(&stf_init, &sc->sc_if); |
2d21ac55 A |
357 | if (error != 0) { |
358 | printf("stfattach, ifnet_allocate failed - %d\n", error); | |
359 | encap_detach(sc->encap_cookie); | |
6d2010ae | 360 | lck_mtx_destroy(&sc->sc_ro_mtx, stf_mtx_grp); |
2d21ac55 A |
361 | FREE(sc, M_DEVBUF); |
362 | return; | |
363 | } | |
364 | ifnet_set_mtu(sc->sc_if, IPV6_MMTU); | |
365 | ifnet_set_flags(sc->sc_if, 0, 0xffff); /* clear all flags */ | |
9bccf70c A |
366 | #if 0 |
367 | /* turn off ingress filter */ | |
2d21ac55 | 368 | ifnet_set_flags(sc->sc_if, IFF_LINK2, IFF_LINK2); |
9bccf70c | 369 | #endif |
9bccf70c | 370 | |
2d21ac55 A |
371 | error = ifnet_attach(sc->sc_if, NULL); |
372 | if (error != 0) { | |
373 | printf("stfattach: ifnet_attach returned error=%d\n", error); | |
374 | encap_detach(sc->encap_cookie); | |
375 | ifnet_release(sc->sc_if); | |
6d2010ae | 376 | lck_mtx_destroy(&sc->sc_ro_mtx, stf_mtx_grp); |
2d21ac55 A |
377 | FREE(sc, M_DEVBUF); |
378 | return; | |
379 | } | |
0a7de745 | 380 | |
2d21ac55 | 381 | bpfattach(sc->sc_if, DLT_NULL, sizeof(u_int)); |
0a7de745 | 382 | |
2d21ac55 | 383 | return; |
9bccf70c A |
384 | } |
385 | ||
386 | static int | |
2d21ac55 A |
387 | stf_encapcheck( |
388 | const struct mbuf *m, | |
389 | __unused int off, | |
390 | int proto, | |
391 | void *arg) | |
9bccf70c A |
392 | { |
393 | struct ip ip; | |
394 | struct in6_ifaddr *ia6; | |
395 | struct stf_softc *sc; | |
396 | struct in_addr a, b; | |
397 | ||
398 | sc = (struct stf_softc *)arg; | |
0a7de745 | 399 | if (sc == NULL) { |
9bccf70c | 400 | return 0; |
0a7de745 | 401 | } |
9bccf70c | 402 | |
0a7de745 | 403 | if ((ifnet_flags(sc->sc_if) & IFF_UP) == 0) { |
9bccf70c | 404 | return 0; |
0a7de745 | 405 | } |
9bccf70c A |
406 | |
407 | /* IFF_LINK0 means "no decapsulation" */ | |
0a7de745 | 408 | if ((ifnet_flags(sc->sc_if) & IFF_LINK0) != 0) { |
9bccf70c | 409 | return 0; |
0a7de745 | 410 | } |
9bccf70c | 411 | |
0a7de745 | 412 | if (proto != IPPROTO_IPV6) { |
9bccf70c | 413 | return 0; |
0a7de745 | 414 | } |
9bccf70c | 415 | |
b0d623f7 | 416 | mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof(ip), &ip); |
9bccf70c | 417 | |
0a7de745 | 418 | if (ip.ip_v != 4) { |
9bccf70c | 419 | return 0; |
0a7de745 | 420 | } |
9bccf70c | 421 | |
2d21ac55 | 422 | ia6 = stf_getsrcifa6(sc->sc_if); |
0a7de745 | 423 | if (ia6 == NULL) { |
9bccf70c | 424 | return 0; |
0a7de745 | 425 | } |
9bccf70c A |
426 | |
427 | /* | |
428 | * check if IPv4 dst matches the IPv4 address derived from the | |
429 | * local 6to4 address. | |
430 | * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:... | |
431 | */ | |
6d2010ae | 432 | IFA_LOCK(&ia6->ia_ifa); |
9bccf70c | 433 | if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst, |
b0d623f7 | 434 | sizeof(ip.ip_dst)) != 0) { |
6d2010ae A |
435 | IFA_UNLOCK(&ia6->ia_ifa); |
436 | IFA_REMREF(&ia6->ia_ifa); | |
9bccf70c | 437 | return 0; |
b0d623f7 | 438 | } |
9bccf70c A |
439 | /* |
440 | * check if IPv4 src matches the IPv4 address derived from the | |
441 | * local 6to4 address masked by prefixmask. | |
442 | * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 | |
443 | * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 | |
444 | */ | |
445 | bzero(&a, sizeof(a)); | |
446 | a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr; | |
447 | a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr; | |
448 | b = ip.ip_src; | |
449 | b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr; | |
b0d623f7 | 450 | if (a.s_addr != b.s_addr) { |
6d2010ae A |
451 | IFA_UNLOCK(&ia6->ia_ifa); |
452 | IFA_REMREF(&ia6->ia_ifa); | |
9bccf70c | 453 | return 0; |
b0d623f7 | 454 | } |
9bccf70c | 455 | /* stf interface makes single side match only */ |
6d2010ae A |
456 | IFA_UNLOCK(&ia6->ia_ifa); |
457 | IFA_REMREF(&ia6->ia_ifa); | |
9bccf70c A |
458 | return 32; |
459 | } | |
460 | ||
461 | static struct in6_ifaddr * | |
2d21ac55 | 462 | stf_getsrcifa6(struct ifnet *ifp) |
9bccf70c A |
463 | { |
464 | struct ifaddr *ia; | |
465 | struct in_ifaddr *ia4; | |
466 | struct sockaddr_in6 *sin6; | |
467 | struct in_addr in; | |
468 | ||
91447636 | 469 | ifnet_lock_shared(ifp); |
6d2010ae A |
470 | for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next) { |
471 | IFA_LOCK(ia); | |
472 | if (ia->ifa_addr == NULL) { | |
473 | IFA_UNLOCK(ia); | |
9bccf70c | 474 | continue; |
6d2010ae A |
475 | } |
476 | if (ia->ifa_addr->sa_family != AF_INET6) { | |
477 | IFA_UNLOCK(ia); | |
9bccf70c | 478 | continue; |
6d2010ae | 479 | } |
316670eb | 480 | sin6 = (struct sockaddr_in6 *)(void *)ia->ifa_addr; |
6d2010ae A |
481 | if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { |
482 | IFA_UNLOCK(ia); | |
9bccf70c | 483 | continue; |
6d2010ae | 484 | } |
9bccf70c | 485 | bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in)); |
6d2010ae | 486 | IFA_UNLOCK(ia); |
b0d623f7 | 487 | lck_rw_lock_shared(in_ifaddr_rwlock); |
9bccf70c | 488 | for (ia4 = TAILQ_FIRST(&in_ifaddrhead); |
0a7de745 A |
489 | ia4; |
490 | ia4 = TAILQ_NEXT(ia4, ia_link)) { | |
6d2010ae A |
491 | IFA_LOCK(&ia4->ia_ifa); |
492 | if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) { | |
493 | IFA_UNLOCK(&ia4->ia_ifa); | |
9bccf70c | 494 | break; |
6d2010ae A |
495 | } |
496 | IFA_UNLOCK(&ia4->ia_ifa); | |
9bccf70c | 497 | } |
b0d623f7 | 498 | lck_rw_done(in_ifaddr_rwlock); |
0a7de745 | 499 | if (ia4 == NULL) { |
9bccf70c | 500 | continue; |
0a7de745 | 501 | } |
9bccf70c | 502 | |
0a7de745 | 503 | IFA_ADDREF(ia); /* for caller */ |
91447636 | 504 | ifnet_lock_done(ifp); |
0a7de745 | 505 | return (struct in6_ifaddr *)ia; |
9bccf70c | 506 | } |
91447636 | 507 | ifnet_lock_done(ifp); |
9bccf70c | 508 | |
0a7de745 | 509 | return NULL; |
9bccf70c A |
510 | } |
511 | ||
512 | int | |
91447636 | 513 | stf_pre_output( |
0a7de745 | 514 | struct ifnet *ifp, |
2d21ac55 | 515 | __unused protocol_family_t protocol_family, |
0a7de745 A |
516 | struct mbuf **m0, |
517 | const struct sockaddr *dst, | |
2d21ac55 A |
518 | __unused void *route, |
519 | __unused char *desk_linkaddr, | |
520 | __unused char *frame_type) | |
9bccf70c | 521 | { |
2d21ac55 | 522 | struct mbuf *m = *m0; |
9bccf70c | 523 | struct stf_softc *sc; |
2d21ac55 A |
524 | const struct sockaddr_in6 *dst6; |
525 | const struct in_addr *in4; | |
9bccf70c A |
526 | u_int8_t tos; |
527 | struct ip *ip; | |
528 | struct ip6_hdr *ip6; | |
529 | struct in6_ifaddr *ia6; | |
0a7de745 | 530 | struct sockaddr_in *dst4; |
a39ff7e2 | 531 | struct ip_out_args ipoa; |
0a7de745 | 532 | errno_t result = 0; |
a39ff7e2 A |
533 | |
534 | bzero(&ipoa, sizeof(ipoa)); | |
535 | ipoa.ipoa_boundif = IFSCOPE_NONE; | |
536 | ipoa.ipoa_flags = IPOAF_SELECT_SRCIF; | |
537 | ipoa.ipoa_sotc = SO_TC_UNSPEC; | |
538 | ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
9bccf70c | 539 | |
2d21ac55 | 540 | sc = ifnet_softc(ifp); |
316670eb | 541 | dst6 = (const struct sockaddr_in6 *)(const void *)dst; |
9bccf70c A |
542 | |
543 | /* just in case */ | |
2d21ac55 | 544 | if ((ifnet_flags(ifp) & IFF_UP) == 0) { |
9bccf70c A |
545 | printf("stf: IFF_DOWN\n"); |
546 | return ENETDOWN; | |
547 | } | |
548 | ||
549 | /* | |
550 | * If we don't have an ip4 address that match my inner ip6 address, | |
551 | * we shouldn't generate output. Without this check, we'll end up | |
552 | * using wrong IPv4 source. | |
553 | */ | |
554 | ia6 = stf_getsrcifa6(ifp); | |
555 | if (ia6 == NULL) { | |
556 | return ENETDOWN; | |
557 | } | |
558 | ||
2d21ac55 | 559 | if (mbuf_len(m) < sizeof(*ip6)) { |
9bccf70c | 560 | m = m_pullup(m, sizeof(*ip6)); |
cc9f6e38 A |
561 | if (!m) { |
562 | *m0 = NULL; /* makes sure this won't be double freed */ | |
6d2010ae | 563 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c | 564 | return ENOBUFS; |
cc9f6e38 | 565 | } |
eb6b6ca3 | 566 | *m0 = m; |
9bccf70c A |
567 | } |
568 | ip6 = mtod(m, struct ip6_hdr *); | |
569 | tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; | |
570 | ||
571 | /* | |
572 | * Pickup the right outer dst addr from the list of candidates. | |
573 | * ip6_dst has priority as it may be able to give us shorter IPv4 hops. | |
574 | */ | |
0a7de745 | 575 | if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst)) { |
9bccf70c | 576 | in4 = GET_V4(&ip6->ip6_dst); |
0a7de745 | 577 | } else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr)) { |
9bccf70c | 578 | in4 = GET_V4(&dst6->sin6_addr); |
0a7de745 | 579 | } else { |
6d2010ae | 580 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c A |
581 | return ENETUNREACH; |
582 | } | |
583 | ||
584 | if (ifp->if_bpf) { | |
2d21ac55 | 585 | /* We need to prepend the address family as a four byte field. */ |
9bccf70c | 586 | u_int32_t af = AF_INET6; |
0a7de745 | 587 | |
2d21ac55 | 588 | bpf_tap_out(ifp, 0, m, &af, sizeof(af)); |
9bccf70c A |
589 | } |
590 | ||
3e170ce0 | 591 | M_PREPEND(m, sizeof(struct ip), M_DONTWAIT, 1); |
0a7de745 | 592 | if (m && mbuf_len(m) < sizeof(struct ip)) { |
9bccf70c | 593 | m = m_pullup(m, sizeof(struct ip)); |
0a7de745 | 594 | } |
cc9f6e38 | 595 | if (m == NULL) { |
0a7de745 | 596 | *m0 = NULL; |
6d2010ae | 597 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c | 598 | return ENOBUFS; |
cc9f6e38 | 599 | } |
eb6b6ca3 A |
600 | |
601 | *m0 = m; | |
9bccf70c A |
602 | ip = mtod(m, struct ip *); |
603 | ||
604 | bzero(ip, sizeof(*ip)); | |
605 | ||
6d2010ae | 606 | IFA_LOCK_SPIN(&ia6->ia_ifa); |
9bccf70c A |
607 | bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr), |
608 | &ip->ip_src, sizeof(ip->ip_src)); | |
6d2010ae | 609 | IFA_UNLOCK(&ia6->ia_ifa); |
9bccf70c A |
610 | bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst)); |
611 | ip->ip_p = IPPROTO_IPV6; | |
612 | ip->ip_ttl = ip_stf_ttl; | |
0a7de745 A |
613 | ip->ip_len = m->m_pkthdr.len; /*host order*/ |
614 | if (ifp->if_flags & IFF_LINK1) { | |
3e170ce0 | 615 | ip_ecn_ingress(ECN_NORMAL, &ip->ip_tos, &tos); |
0a7de745 | 616 | } else { |
9bccf70c | 617 | ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos); |
0a7de745 | 618 | } |
9bccf70c | 619 | |
6d2010ae | 620 | lck_mtx_lock(&sc->sc_ro_mtx); |
316670eb | 621 | dst4 = (struct sockaddr_in *)(void *)&sc->sc_ro.ro_dst; |
39236c6e | 622 | if (ROUTE_UNUSABLE(&sc->sc_ro) || dst4->sin_family != AF_INET || |
9bccf70c | 623 | bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) { |
39236c6e | 624 | ROUTE_RELEASE(&sc->sc_ro); |
6d2010ae | 625 | /* cache route doesn't match: always the case during the first use */ |
9bccf70c A |
626 | dst4->sin_family = AF_INET; |
627 | dst4->sin_len = sizeof(struct sockaddr_in); | |
628 | bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr)); | |
9bccf70c A |
629 | } |
630 | ||
39236c6e | 631 | result = ip_output(m, NULL, &sc->sc_ro, IP_OUTARGS, NULL, &ipoa); |
6d2010ae | 632 | lck_mtx_unlock(&sc->sc_ro_mtx); |
9bccf70c | 633 | |
2d21ac55 A |
634 | /* Assumption: ip_output will free mbuf on errors */ |
635 | /* All the output processing is done here, don't let stf_output be called */ | |
0a7de745 | 636 | if (result == 0) { |
2d21ac55 | 637 | result = EJUSTRETURN; |
0a7de745 | 638 | } |
2d21ac55 | 639 | *m0 = NULL; |
6d2010ae | 640 | IFA_REMREF(&ia6->ia_ifa); |
2d21ac55 | 641 | return result; |
9bccf70c | 642 | } |
2d21ac55 A |
643 | static errno_t |
644 | stf_output( | |
0a7de745 A |
645 | __unused ifnet_t ifp, |
646 | __unused mbuf_t m) | |
2d21ac55 A |
647 | { |
648 | /* All processing is done in stf_pre_output | |
649 | * this shouldn't be called as the pre_output returns "EJUSTRETURN" | |
650 | */ | |
651 | return 0; | |
0a7de745 | 652 | } |
9bccf70c A |
653 | |
654 | static int | |
2d21ac55 A |
655 | stf_checkaddr4( |
656 | struct stf_softc *sc, | |
657 | const struct in_addr *in, | |
0a7de745 | 658 | struct ifnet *inifp) /* incoming interface */ |
9bccf70c A |
659 | { |
660 | struct in_ifaddr *ia4; | |
661 | ||
662 | /* | |
663 | * reject packets with the following address: | |
664 | * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 | |
665 | */ | |
0a7de745 | 666 | if (IN_MULTICAST(ntohl(in->s_addr))) { |
9bccf70c | 667 | return -1; |
0a7de745 | 668 | } |
9bccf70c A |
669 | switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { |
670 | case 0: case 127: case 255: | |
671 | return -1; | |
672 | } | |
673 | ||
674 | /* | |
675 | * reject packets with broadcast | |
676 | */ | |
b0d623f7 | 677 | lck_rw_lock_shared(in_ifaddr_rwlock); |
9bccf70c | 678 | for (ia4 = TAILQ_FIRST(&in_ifaddrhead); |
0a7de745 A |
679 | ia4; |
680 | ia4 = TAILQ_NEXT(ia4, ia_link)) { | |
6d2010ae A |
681 | IFA_LOCK(&ia4->ia_ifa); |
682 | if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) { | |
683 | IFA_UNLOCK(&ia4->ia_ifa); | |
9bccf70c | 684 | continue; |
6d2010ae | 685 | } |
91447636 | 686 | if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) { |
6d2010ae | 687 | IFA_UNLOCK(&ia4->ia_ifa); |
b0d623f7 | 688 | lck_rw_done(in_ifaddr_rwlock); |
9bccf70c | 689 | return -1; |
91447636 | 690 | } |
6d2010ae | 691 | IFA_UNLOCK(&ia4->ia_ifa); |
9bccf70c | 692 | } |
b0d623f7 | 693 | lck_rw_done(in_ifaddr_rwlock); |
9bccf70c A |
694 | |
695 | /* | |
696 | * perform ingress filter | |
697 | */ | |
2d21ac55 | 698 | if (sc && (ifnet_flags(sc->sc_if) & IFF_LINK2) == 0 && inifp) { |
9bccf70c A |
699 | struct sockaddr_in sin; |
700 | struct rtentry *rt; | |
701 | ||
702 | bzero(&sin, sizeof(sin)); | |
703 | sin.sin_family = AF_INET; | |
704 | sin.sin_len = sizeof(struct sockaddr_in); | |
705 | sin.sin_addr = *in; | |
b0d623f7 | 706 | rt = rtalloc1((struct sockaddr *)&sin, 0, 0); |
0a7de745 | 707 | if (rt != NULL) { |
b0d623f7 | 708 | RT_LOCK(rt); |
0a7de745 | 709 | } |
b0d623f7 | 710 | if (rt == NULL || rt->rt_ifp != inifp) { |
9bccf70c A |
711 | #if 1 |
712 | log(LOG_WARNING, "%s: packet from 0x%x dropped " | |
2d21ac55 | 713 | "due to ingress filter\n", if_name(sc->sc_if), |
9bccf70c A |
714 | (u_int32_t)ntohl(sin.sin_addr.s_addr)); |
715 | #endif | |
b0d623f7 A |
716 | if (rt != NULL) { |
717 | RT_UNLOCK(rt); | |
9bccf70c | 718 | rtfree(rt); |
b0d623f7 | 719 | } |
9bccf70c A |
720 | return -1; |
721 | } | |
b0d623f7 | 722 | RT_UNLOCK(rt); |
9bccf70c A |
723 | rtfree(rt); |
724 | } | |
725 | ||
726 | return 0; | |
727 | } | |
728 | ||
729 | static int | |
2d21ac55 A |
730 | stf_checkaddr6( |
731 | struct stf_softc *sc, | |
732 | struct in6_addr *in6, | |
0a7de745 | 733 | struct ifnet *inifp) /* incoming interface */ |
9bccf70c A |
734 | { |
735 | /* | |
736 | * check 6to4 addresses | |
737 | */ | |
0a7de745 | 738 | if (IN6_IS_ADDR_6TO4(in6)) { |
9bccf70c | 739 | return stf_checkaddr4(sc, GET_V4(in6), inifp); |
0a7de745 | 740 | } |
9bccf70c A |
741 | |
742 | /* | |
743 | * reject anything that look suspicious. the test is implemented | |
744 | * in ip6_input too, but we check here as well to | |
745 | * (1) reject bad packets earlier, and | |
746 | * (2) to be safe against future ip6_input change. | |
747 | */ | |
0a7de745 | 748 | if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6)) { |
9bccf70c | 749 | return -1; |
0a7de745 | 750 | } |
9bccf70c A |
751 | |
752 | return 0; | |
753 | } | |
754 | ||
91447636 | 755 | static void |
2d21ac55 A |
756 | in_stf_input( |
757 | struct mbuf *m, | |
758 | int off) | |
9bccf70c A |
759 | { |
760 | struct stf_softc *sc; | |
761 | struct ip *ip; | |
2d21ac55 | 762 | struct ip6_hdr ip6; |
9bccf70c | 763 | u_int8_t otos, itos; |
91447636 | 764 | int proto; |
9bccf70c | 765 | struct ifnet *ifp; |
0a7de745 | 766 | struct ifnet_stat_increment_param stats; |
9bccf70c A |
767 | |
768 | ip = mtod(m, struct ip *); | |
769 | proto = ip->ip_p; | |
770 | ||
9bccf70c A |
771 | if (proto != IPPROTO_IPV6) { |
772 | m_freem(m); | |
773 | return; | |
774 | } | |
775 | ||
776 | ip = mtod(m, struct ip *); | |
777 | ||
778 | sc = (struct stf_softc *)encap_getarg(m); | |
779 | ||
2d21ac55 | 780 | if (sc == NULL || (ifnet_flags(sc->sc_if) & IFF_UP) == 0) { |
9bccf70c A |
781 | m_freem(m); |
782 | return; | |
783 | } | |
784 | ||
2d21ac55 A |
785 | ifp = sc->sc_if; |
786 | ||
9bccf70c A |
787 | /* |
788 | * perform sanity check against outer src/dst. | |
789 | * for source, perform ingress filter as well. | |
790 | */ | |
791 | if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 || | |
792 | stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) { | |
793 | m_freem(m); | |
794 | return; | |
795 | } | |
796 | ||
797 | otos = ip->ip_tos; | |
2d21ac55 | 798 | mbuf_copydata(m, off, sizeof(ip6), &ip6); |
9bccf70c A |
799 | |
800 | /* | |
801 | * perform sanity check against inner src/dst. | |
802 | * for source, perform ingress filter as well. | |
803 | */ | |
2d21ac55 A |
804 | if (stf_checkaddr6(sc, &ip6.ip6_dst, NULL) < 0 || |
805 | stf_checkaddr6(sc, &ip6.ip6_src, m->m_pkthdr.rcvif) < 0) { | |
9bccf70c A |
806 | m_freem(m); |
807 | return; | |
808 | } | |
809 | ||
2d21ac55 | 810 | itos = (ntohl(ip6.ip6_flow) >> 20) & 0xff; |
0a7de745 | 811 | if ((ifnet_flags(ifp) & IFF_LINK1) != 0) { |
3e170ce0 | 812 | ip_ecn_egress(ECN_NORMAL, &otos, &itos); |
0a7de745 | 813 | } else { |
9bccf70c | 814 | ip_ecn_egress(ECN_NOCARE, &otos, &itos); |
0a7de745 | 815 | } |
2d21ac55 A |
816 | ip6.ip6_flow &= ~htonl(0xff << 20); |
817 | ip6.ip6_flow |= htonl((u_int32_t)itos << 20); | |
9bccf70c A |
818 | |
819 | m->m_pkthdr.rcvif = ifp; | |
2d21ac55 A |
820 | mbuf_pkthdr_setheader(m, mbuf_data(m)); |
821 | mbuf_adj(m, off); | |
0a7de745 | 822 | |
9bccf70c | 823 | if (ifp->if_bpf) { |
2d21ac55 | 824 | /* We need to prepend the address family as a four byte field. */ |
9bccf70c | 825 | u_int32_t af = AF_INET6; |
2d21ac55 | 826 | bpf_tap_in(ifp, 0, m, &af, sizeof(af)); |
9bccf70c A |
827 | } |
828 | ||
829 | /* | |
830 | * Put the packet to the network layer input queue according to the | |
831 | * specified address family. | |
832 | * See net/if_gif.c for possible issues with packet processing | |
833 | * reorder due to extra queueing. | |
834 | */ | |
2d21ac55 A |
835 | bzero(&stats, sizeof(stats)); |
836 | stats.packets_in = 1; | |
837 | stats.bytes_in = mbuf_pkthdr_len(m); | |
838 | mbuf_pkthdr_setrcvif(m, ifp); | |
839 | ifnet_input(ifp, m, &stats); | |
0a7de745 | 840 | |
55e303ae | 841 | return; |
9bccf70c A |
842 | } |
843 | ||
9bccf70c | 844 | static void |
2d21ac55 A |
845 | stf_rtrequest( |
846 | __unused int cmd, | |
847 | struct rtentry *rt, | |
848 | __unused struct sockaddr *sa) | |
9bccf70c | 849 | { |
b0d623f7 A |
850 | if (rt != NULL) { |
851 | RT_LOCK_ASSERT_HELD(rt); | |
9bccf70c | 852 | rt->rt_rmx.rmx_mtu = IPV6_MMTU; |
b0d623f7 | 853 | } |
9bccf70c A |
854 | } |
855 | ||
2d21ac55 A |
856 | static errno_t |
857 | stf_ioctl( | |
0a7de745 A |
858 | ifnet_t ifp, |
859 | u_long cmd, | |
860 | void *data) | |
9bccf70c A |
861 | { |
862 | struct ifaddr *ifa; | |
863 | struct ifreq *ifr; | |
864 | struct sockaddr_in6 *sin6; | |
865 | int error; | |
866 | ||
867 | error = 0; | |
868 | switch (cmd) { | |
869 | case SIOCSIFADDR: | |
870 | ifa = (struct ifaddr *)data; | |
6d2010ae A |
871 | if (ifa == NULL) { |
872 | error = EAFNOSUPPORT; | |
873 | break; | |
874 | } | |
875 | IFA_LOCK(ifa); | |
876 | if (ifa->ifa_addr->sa_family != AF_INET6) { | |
877 | IFA_UNLOCK(ifa); | |
9bccf70c A |
878 | error = EAFNOSUPPORT; |
879 | break; | |
880 | } | |
316670eb | 881 | sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr; |
9bccf70c | 882 | if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { |
0a7de745 A |
883 | if (!(ifnet_flags( ifp ) & IFF_UP)) { |
884 | /* do this only if the interface is not already up */ | |
91447636 | 885 | ifa->ifa_rtrequest = stf_rtrequest; |
6d2010ae | 886 | IFA_UNLOCK(ifa); |
91447636 | 887 | ifnet_set_flags(ifp, IFF_UP, IFF_UP); |
6d2010ae A |
888 | } else { |
889 | IFA_UNLOCK(ifa); | |
91447636 | 890 | } |
6d2010ae A |
891 | } else { |
892 | IFA_UNLOCK(ifa); | |
9bccf70c | 893 | error = EINVAL; |
6d2010ae A |
894 | } |
895 | IFA_LOCK_ASSERT_NOTHELD(ifa); | |
9bccf70c A |
896 | break; |
897 | ||
898 | case SIOCADDMULTI: | |
899 | case SIOCDELMULTI: | |
900 | ifr = (struct ifreq *)data; | |
0a7de745 | 901 | if (ifr && ifr->ifr_addr.sa_family == AF_INET6) { |
9bccf70c | 902 | ; |
0a7de745 | 903 | } else { |
9bccf70c | 904 | error = EAFNOSUPPORT; |
0a7de745 | 905 | } |
9bccf70c A |
906 | break; |
907 | ||
908 | default: | |
39236c6e | 909 | error = EOPNOTSUPP; |
9bccf70c A |
910 | break; |
911 | } | |
912 | ||
913 | return error; | |
914 | } |