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