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