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