]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_stf.c
xnu-792.tar.gz
[apple/xnu.git] / bsd / net / if_stf.c
1 /* $FreeBSD: src/sys/net/if_stf.c,v 1.1.2.6 2001/07/24 19:10:18 brooks Exp $ */
2 /* $KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun Exp $ */
3
4 /*
5 * Copyright (C) 2000 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * 6to4 interface, based on RFC3056.
35 *
36 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
37 * There is no address mapping defined from IPv6 multicast address to IPv4
38 * address. Therefore, we do not have IFF_MULTICAST on the interface.
39 *
40 * Due to the lack of address mapping for link-local addresses, we cannot
41 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
42 * packets to link-local multicast addresses (ff02::x).
43 *
44 * Here are interesting symptoms due to the lack of link-local address:
45 *
46 * Unicast routing exchange:
47 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
48 * and link-local addresses as nexthop.
49 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
50 * assigned to the link, and makes use of them. Also, HELLO packets use
51 * link-local multicast addresses (ff02::5 and ff02::6).
52 * - BGP4+: Maybe. You can only use global address as nexthop, and global
53 * address as TCP endpoint address.
54 *
55 * Multicast routing protocols:
56 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
57 * Adjacent PIM routers must be configured manually (is it really spec-wise
58 * correct thing to do?).
59 *
60 * ICMPv6:
61 * - Redirects cannot be used due to the lack of link-local address.
62 *
63 * stf interface does not have, and will not need, a link-local address.
64 * It seems to have no real benefit and does not help the above symptoms much.
65 * Even if we assign link-locals to interface, we cannot really
66 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
67 * encapsulation defined for link-local address), and the above analysis does
68 * not change. RFC3056 does not mandate the assignment of link-local address
69 * either.
70 *
71 * 6to4 interface has security issues. Refer to
72 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
73 * for details. The code tries to filter out some of malicious packets.
74 * Note that there is no way to be 100% secure.
75 */
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/socket.h>
80 #include <sys/sockio.h>
81 #include <sys/mbuf.h>
82 #include <sys/errno.h>
83 #include <sys/protosw.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
86
87 #include <sys/malloc.h>
88
89 #include <net/if.h>
90 #include <net/route.h>
91 #include <net/if_types.h>
92
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/in_var.h>
98
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip_ecn.h>
103
104 #include <netinet/ip_encap.h>
105 #include <net/dlil.h>
106
107
108 #include <net/net_osdep.h>
109
110 #include <net/bpf.h>
111
112 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
113 #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1]))
114
115 struct stf_softc {
116 struct ifnet sc_if; /* common area */
117 #ifdef __APPLE__
118 u_long sc_protocol_family; /* dlil protocol attached */
119 #endif
120 union {
121 struct route __sc_ro4;
122 struct route_in6 __sc_ro6; /* just for safety */
123 } __sc_ro46;
124 #define sc_ro __sc_ro46.__sc_ro4
125 const struct encaptab *encap_cookie;
126 };
127
128 static struct stf_softc *stf;
129
130 #ifdef __APPLE__
131 void stfattach (void);
132 #endif
133
134 #ifndef __APPLE__
135 static MALLOC_DEFINE(M_STF, "stf", "6to4 Tunnel Interface");
136 #endif
137 static int ip_stf_ttl = 40;
138
139 static void in_stf_input(struct mbuf *, int);
140 extern struct domain inetdomain;
141 struct protosw in_stf_protosw =
142 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR,
143 in_stf_input, 0, 0, rip_ctloutput,
144 0,
145 0,
146 &rip_usrreqs,
147 0, rip_unlock, 0
148 };
149
150 static int stf_encapcheck(const struct mbuf *, int, int, void *);
151 static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
152 int stf_pre_output(struct ifnet *, u_long, register struct mbuf **,
153 const struct sockaddr *, caddr_t, char *, char *);
154 static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
155 struct ifnet *);
156 static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
157 struct ifnet *);
158 static void stf_rtrequest(int, struct rtentry *, struct sockaddr *);
159 int stf_ioctl(struct ifnet *, u_long, void *);
160
161 static
162 int stf_add_proto(
163 struct ifnet *ifp,
164 u_long protocol_family,
165 struct ddesc_head_str *desc_head)
166 {
167 /* Only one protocol may be attached at a time */
168 struct stf_softc* stf = (struct stf_softc*)ifp;
169 if (stf->sc_protocol_family == 0)
170 stf->sc_protocol_family = protocol_family;
171 else {
172 printf("stf_add_proto: stf already has a proto\n");
173 return EBUSY;
174 }
175
176 return 0;
177 }
178
179 static
180 int stf_del_proto(
181 struct ifnet *ifp,
182 u_long protocol_family)
183 {
184 if (((struct stf_softc*)ifp)->sc_protocol_family == protocol_family)
185 ((struct stf_softc*)ifp)->sc_protocol_family = 0;
186 else
187 return ENOENT;
188
189 return 0;
190 }
191
192 static int
193 stf_attach_inet6(struct ifnet *ifp, u_long protocol_family)
194 {
195 struct dlil_proto_reg_str reg;
196 int stat, i;
197
198 bzero(&reg, sizeof(reg));
199 TAILQ_INIT(&reg.demux_desc_head);
200 reg.interface_family = ifp->if_family;
201 reg.unit_number = ifp->if_unit;
202 reg.pre_output = stf_pre_output;
203 reg.protocol_family = PF_INET6;
204
205 stat = dlil_attach_protocol(&reg);
206
207 return stat;
208 }
209
210 static int
211 stf_demux(
212 struct ifnet *ifp,
213 struct mbuf *m,
214 char *frame_ptr,
215 u_long *protocol_family)
216 {
217 *protocol_family = PF_INET6;
218 return 0;
219 }
220
221 void stf_reg_if_mods()
222 {
223 int error;
224
225 /* Register protocol registration functions */
226 if ( error = dlil_reg_proto_module(AF_INET6, APPLE_IF_FAM_STF, stf_attach_inet6, NULL) != 0)
227 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error);
228 }
229
230 void
231 stfattach(void)
232 {
233 struct ifnet *ifp;
234 struct stf_softc *sc;
235 int i, error;
236 int err;
237 const struct encaptab *p;
238
239 stf_reg_if_mods(); /* DLIL modules */
240
241 sc = _MALLOC(sizeof(struct stf_softc), M_DEVBUF, M_WAITOK);
242 if (sc == 0) {
243 printf("stf softc attach failed\n" );
244 return;
245 }
246
247 bzero(sc, sizeof(*sc));
248 sc->sc_if.if_name = "stf";
249 sc->sc_if.if_unit = 0;
250
251 p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
252 &in_stf_protosw, sc);
253 if (p == NULL) {
254 printf("%s: attach failed\n", if_name(&sc->sc_if));
255 FREE(sc, M_DEVBUF);
256 return;
257 }
258 sc->encap_cookie = p;
259 sc->sc_if.if_mtu = IPV6_MMTU;
260 sc->sc_if.if_flags = 0;
261 sc->sc_if.if_ioctl = stf_ioctl;
262 sc->sc_if.if_output = NULL; /* processing done in pre_output */
263 sc->sc_if.if_type = IFT_STF;
264 sc->sc_if.if_family= APPLE_IF_FAM_STF;
265 sc->sc_if.if_add_proto = stf_add_proto;
266 sc->sc_if.if_del_proto = stf_del_proto;
267 sc->sc_if.if_demux = stf_demux;
268 #if 0
269 /* turn off ingress filter */
270 sc->sc_if.if_flags |= IFF_LINK2;
271 #endif
272 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
273
274 if (error = dlil_if_attach(&sc->sc_if))
275 printf("stfattach: can't dlil_if_attach error=%d\n");
276 else
277 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
278
279 return ;
280 }
281
282 static int
283 stf_encapcheck(m, off, proto, arg)
284 const struct mbuf *m;
285 int off;
286 int proto;
287 void *arg;
288 {
289 struct ip ip;
290 struct in6_ifaddr *ia6;
291 struct stf_softc *sc;
292 struct in_addr a, b;
293
294 sc = (struct stf_softc *)arg;
295 if (sc == NULL)
296 return 0;
297
298 if ((sc->sc_if.if_flags & IFF_UP) == 0)
299 return 0;
300
301 /* IFF_LINK0 means "no decapsulation" */
302 if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
303 return 0;
304
305 if (proto != IPPROTO_IPV6)
306 return 0;
307
308 /* LINTED const cast */
309 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
310
311 if (ip.ip_v != 4)
312 return 0;
313
314 ia6 = stf_getsrcifa6(&sc->sc_if);
315 if (ia6 == NULL)
316 return 0;
317
318 /*
319 * check if IPv4 dst matches the IPv4 address derived from the
320 * local 6to4 address.
321 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
322 */
323 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
324 sizeof(ip.ip_dst)) != 0)
325 return 0;
326
327 /*
328 * check if IPv4 src matches the IPv4 address derived from the
329 * local 6to4 address masked by prefixmask.
330 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
331 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
332 */
333 bzero(&a, sizeof(a));
334 a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
335 a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
336 b = ip.ip_src;
337 b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
338 if (a.s_addr != b.s_addr)
339 return 0;
340
341 /* stf interface makes single side match only */
342 return 32;
343 }
344
345 static struct in6_ifaddr *
346 stf_getsrcifa6(ifp)
347 struct ifnet *ifp;
348 {
349 struct ifaddr *ia;
350 struct in_ifaddr *ia4;
351 struct sockaddr_in6 *sin6;
352 struct in_addr in;
353
354 ifnet_lock_shared(ifp);
355 for (ia = ifp->if_addrlist.tqh_first;
356 ia;
357 ia = ia->ifa_list.tqe_next)
358 {
359 if (ia->ifa_addr == NULL)
360 continue;
361 if (ia->ifa_addr->sa_family != AF_INET6)
362 continue;
363 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
364 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
365 continue;
366
367 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
368 lck_mtx_lock(rt_mtx);
369 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
370 ia4;
371 ia4 = TAILQ_NEXT(ia4, ia_link))
372 {
373 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
374 break;
375 }
376 lck_mtx_unlock(rt_mtx);
377 if (ia4 == NULL)
378 continue;
379
380 ifnet_lock_done(ifp);
381 return (struct in6_ifaddr *)ia;
382 }
383 ifnet_lock_done(ifp);
384
385 return NULL;
386 }
387
388 int
389 stf_pre_output(
390 struct ifnet *ifp,
391 u_long protocol_family,
392 register struct mbuf **m0,
393 const struct sockaddr *dst,
394 caddr_t rt,
395 char *frame_type,
396 char *address)
397 {
398 register struct mbuf *m = *m0;
399 struct stf_softc *sc;
400 struct sockaddr_in6 *dst6;
401 struct in_addr *in4;
402 struct sockaddr_in *dst4;
403 u_int8_t tos;
404 struct ip *ip;
405 struct ip6_hdr *ip6;
406 struct in6_ifaddr *ia6;
407 int error = 0 ;
408
409 sc = (struct stf_softc*)ifp;
410 dst6 = (struct sockaddr_in6 *)dst;
411
412 /* just in case */
413 if ((ifp->if_flags & IFF_UP) == 0) {
414 printf("stf: IFF_DOWN\n");
415 return ENETDOWN;
416 }
417
418 /*
419 * If we don't have an ip4 address that match my inner ip6 address,
420 * we shouldn't generate output. Without this check, we'll end up
421 * using wrong IPv4 source.
422 */
423 ia6 = stf_getsrcifa6(ifp);
424 if (ia6 == NULL) {
425 return ENETDOWN;
426 }
427
428 if (m->m_len < sizeof(*ip6)) {
429 m = m_pullup(m, sizeof(*ip6));
430 if (!m)
431 return ENOBUFS;
432 }
433 ip6 = mtod(m, struct ip6_hdr *);
434 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
435
436 /*
437 * Pickup the right outer dst addr from the list of candidates.
438 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
439 */
440 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
441 in4 = GET_V4(&ip6->ip6_dst);
442 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
443 in4 = GET_V4(&dst6->sin6_addr);
444 else {
445 return ENETUNREACH;
446 }
447
448 if (ifp->if_bpf) {
449 /*
450 * We need to prepend the address family as
451 * a four byte field. Cons up a dummy header
452 * to pacify bpf. This is safe because bpf
453 * will only read from the mbuf (i.e., it won't
454 * try to free it or keep a pointer a to it).
455 */
456 struct mbuf m1;
457 u_int32_t af = AF_INET6;
458
459 m1.m_next = m;
460 m1.m_len = 4;
461 m1.m_data = (char *)&af;
462
463 bpf_mtap(ifp, &m1);
464 }
465
466 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
467 if (m && m->m_len < sizeof(struct ip))
468 m = m_pullup(m, sizeof(struct ip));
469 if (m == NULL)
470 return ENOBUFS;
471 ip = mtod(m, struct ip *);
472
473 bzero(ip, sizeof(*ip));
474
475 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
476 &ip->ip_src, sizeof(ip->ip_src));
477 bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
478 ip->ip_p = IPPROTO_IPV6;
479 ip->ip_ttl = ip_stf_ttl;
480 ip->ip_len = m->m_pkthdr.len; /*host order*/
481 if (ifp->if_flags & IFF_LINK1)
482 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
483 else
484 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
485
486 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
487 if (dst4->sin_family != AF_INET ||
488 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
489 /* cache route doesn't match */
490 dst4->sin_family = AF_INET;
491 dst4->sin_len = sizeof(struct sockaddr_in);
492 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
493 if (sc->sc_ro.ro_rt) {
494 rtfree(sc->sc_ro.ro_rt);
495 sc->sc_ro.ro_rt = NULL;
496 }
497 }
498
499 if (sc->sc_ro.ro_rt == NULL) {
500 rtalloc(&sc->sc_ro);
501 if (sc->sc_ro.ro_rt == NULL) {
502 return ENETUNREACH;
503 }
504 }
505
506 error = ip_output(m, NULL, &sc->sc_ro, 0, NULL);
507 if (error == 0)
508 return EJUSTRETURN;
509
510 return error;
511 }
512
513 static int
514 stf_checkaddr4(sc, in, inifp)
515 struct stf_softc *sc;
516 struct in_addr *in;
517 struct ifnet *inifp; /* incoming interface */
518 {
519 struct in_ifaddr *ia4;
520
521 /*
522 * reject packets with the following address:
523 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
524 */
525 if (IN_MULTICAST(ntohl(in->s_addr)))
526 return -1;
527 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
528 case 0: case 127: case 255:
529 return -1;
530 }
531
532 /*
533 * reject packets with broadcast
534 */
535 lck_mtx_lock(rt_mtx);
536 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
537 ia4;
538 ia4 = TAILQ_NEXT(ia4, ia_link))
539 {
540 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
541 continue;
542 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
543 lck_mtx_unlock(rt_mtx);
544 return -1;
545 }
546 }
547 lck_mtx_unlock(rt_mtx);
548
549 /*
550 * perform ingress filter
551 */
552 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
553 struct sockaddr_in sin;
554 struct rtentry *rt;
555
556 bzero(&sin, sizeof(sin));
557 sin.sin_family = AF_INET;
558 sin.sin_len = sizeof(struct sockaddr_in);
559 sin.sin_addr = *in;
560 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
561 if (!rt || rt->rt_ifp != inifp) {
562 #if 1
563 log(LOG_WARNING, "%s: packet from 0x%x dropped "
564 "due to ingress filter\n", if_name(&sc->sc_if),
565 (u_int32_t)ntohl(sin.sin_addr.s_addr));
566 #endif
567 if (rt)
568 rtfree(rt);
569 return -1;
570 }
571 rtfree(rt);
572 }
573
574 return 0;
575 }
576
577 static int
578 stf_checkaddr6(sc, in6, inifp)
579 struct stf_softc *sc;
580 struct in6_addr *in6;
581 struct ifnet *inifp; /* incoming interface */
582 {
583 /*
584 * check 6to4 addresses
585 */
586 if (IN6_IS_ADDR_6TO4(in6))
587 return stf_checkaddr4(sc, GET_V4(in6), inifp);
588
589 /*
590 * reject anything that look suspicious. the test is implemented
591 * in ip6_input too, but we check here as well to
592 * (1) reject bad packets earlier, and
593 * (2) to be safe against future ip6_input change.
594 */
595 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
596 return -1;
597
598 return 0;
599 }
600
601 static void
602 in_stf_input(m, off)
603 struct mbuf *m;
604 int off;
605 {
606 struct stf_softc *sc;
607 struct ip *ip;
608 struct ip6_hdr *ip6;
609 u_int8_t otos, itos;
610 int proto;
611 struct ifnet *ifp;
612
613 ip = mtod(m, struct ip *);
614 proto = ip->ip_p;
615
616
617 if (proto != IPPROTO_IPV6) {
618 m_freem(m);
619 return;
620 }
621
622 ip = mtod(m, struct ip *);
623
624 sc = (struct stf_softc *)encap_getarg(m);
625
626 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
627 m_freem(m);
628 return;
629 }
630
631 ifp = &sc->sc_if;
632
633 /*
634 * perform sanity check against outer src/dst.
635 * for source, perform ingress filter as well.
636 */
637 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
638 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
639 m_freem(m);
640 return;
641 }
642
643 otos = ip->ip_tos;
644 m_adj(m, off);
645
646 if (m->m_len < sizeof(*ip6)) {
647 m = m_pullup(m, sizeof(*ip6));
648 if (!m)
649 return;
650 }
651 ip6 = mtod(m, struct ip6_hdr *);
652
653 /*
654 * perform sanity check against inner src/dst.
655 * for source, perform ingress filter as well.
656 */
657 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
658 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
659 m_freem(m);
660 return;
661 }
662
663 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
664 if ((ifp->if_flags & IFF_LINK1) != 0)
665 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
666 else
667 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
668 ip6->ip6_flow &= ~htonl(0xff << 20);
669 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
670
671 m->m_pkthdr.rcvif = ifp;
672
673 if (ifp->if_bpf) {
674 /*
675 * We need to prepend the address family as
676 * a four byte field. Cons up a dummy header
677 * to pacify bpf. This is safe because bpf
678 * will only read from the mbuf (i.e., it won't
679 * try to free it or keep a pointer a to it).
680 */
681 struct mbuf m0;
682 u_int32_t af = AF_INET6;
683
684 m0.m_next = m;
685 m0.m_len = 4;
686 m0.m_data = (char *)&af;
687
688 #ifdef HAVE_OLD_BPF
689 bpf_mtap(ifp, &m0);
690 #else
691 bpf_mtap(ifp->if_bpf, &m0);
692 #endif
693 }
694
695 /*
696 * Put the packet to the network layer input queue according to the
697 * specified address family.
698 * See net/if_gif.c for possible issues with packet processing
699 * reorder due to extra queueing.
700 */
701 proto_input(PF_INET6, m);
702 ifp->if_ipackets++;
703 ifp->if_ibytes += m->m_pkthdr.len;
704
705 return;
706 }
707
708 /* ARGSUSED */
709 static void
710 stf_rtrequest(cmd, rt, sa)
711 int cmd;
712 struct rtentry *rt;
713 struct sockaddr *sa;
714 {
715
716 if (rt)
717 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
718 }
719
720 int
721 stf_ioctl(ifp, cmd, data)
722 struct ifnet *ifp;
723 u_long cmd;
724 void *data;
725 {
726 struct ifaddr *ifa;
727 struct ifreq *ifr;
728 struct sockaddr_in6 *sin6;
729 int error;
730
731 error = 0;
732 switch (cmd) {
733 case SIOCSIFADDR:
734 ifa = (struct ifaddr *)data;
735 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
736 error = EAFNOSUPPORT;
737 break;
738 }
739 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
740 if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
741 if ( !(ifnet_flags( ifp ) & IFF_UP) ) {
742 /* do this only if the interface is not already up */
743 ifa->ifa_rtrequest = stf_rtrequest;
744 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
745 }
746 } else
747 error = EINVAL;
748 break;
749
750 case SIOCADDMULTI:
751 case SIOCDELMULTI:
752 ifr = (struct ifreq *)data;
753 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
754 ;
755 else
756 error = EAFNOSUPPORT;
757 break;
758
759 default:
760 error = EINVAL;
761 break;
762 }
763
764 return error;
765 }