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