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