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