]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_stf.c
xnu-344.12.2.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 #include <machine/cpu.h>
87
88 #include <sys/malloc.h>
89
90 #include <net/if.h>
91 #include <net/route.h>
92 #include <net/netisr.h>
93 #include <net/if_types.h>
94 #include <net/if_stf.h>
95
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_var.h>
100 #include <netinet/in_var.h>
101
102 #include <netinet/ip6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/in6_var.h>
105 #include <netinet/ip_ecn.h>
106
107 #include <netinet/ip_encap.h>
108 #include <net/dlil.h>
109
110
111 #include <net/net_osdep.h>
112
113 #include <net/bpf.h>
114
115 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
116 #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1]))
117
118 struct stf_softc {
119 struct ifnet sc_if; /* common area */
120 #ifdef __APPLE__
121 struct if_proto *stf_proto; /* dlil protocol attached */
122 #endif
123 union {
124 struct route __sc_ro4;
125 struct route_in6 __sc_ro6; /* just for safety */
126 } __sc_ro46;
127 #define sc_ro __sc_ro46.__sc_ro4
128 const struct encaptab *encap_cookie;
129 };
130
131 static struct stf_softc *stf;
132
133 #ifdef __APPLE__
134 void stfattach __P((void));
135 int stf_pre_output __P((struct ifnet *, register struct mbuf **, struct sockaddr *,
136 caddr_t, char *, char *, u_long));
137 static u_long stf_dl_tag=0;
138 #endif
139
140 #ifndef __APPLE__
141 static MALLOC_DEFINE(M_STF, "stf", "6to4 Tunnel Interface");
142 #endif
143 static int ip_stf_ttl = 40;
144
145 extern struct domain inetdomain;
146 struct protosw in_stf_protosw =
147 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR,
148 in_stf_input, rip_output, 0, rip_ctloutput,
149 0,
150 0, 0, 0, 0,
151 0,
152 &rip_usrreqs
153 };
154
155 static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
156 static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
157 int stf_pre_output __P((struct ifnet *, register struct mbuf **, struct sockaddr *,
158 caddr_t, char *, char *, u_long));
159 static int stf_checkaddr4 __P((struct stf_softc *, struct in_addr *,
160 struct ifnet *));
161 static int stf_checkaddr6 __P((struct stf_softc *, struct in6_addr *,
162 struct ifnet *));
163 static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
164 int stf_ioctl __P((struct ifnet *, u_long, void *));
165
166
167 static
168 int stf_add_if(struct ifnet *ifp)
169 {
170 ifp->if_demux = 0;
171 ifp->if_framer = 0;
172 return 0;
173 }
174
175 static
176 int stf_del_if(struct ifnet *ifp)
177 {
178 return 0;
179 }
180
181 static
182 int stf_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag)
183 {
184 /* Only one protocol may be attached at a time */
185 struct stf_softc* stf = (struct stf_softc*)proto->ifp;
186 if (stf->stf_proto == NULL)
187 stf->stf_proto = proto;
188 else {
189 printf("stf_add_proto: stf already has a proto\n");
190 return (EBUSY);
191 }
192
193 return (0);
194 }
195
196 static
197 int stf_del_proto(struct if_proto *proto, u_long dl_tag)
198 {
199 if (((struct stf_softc*)proto->ifp)->stf_proto == proto)
200 ((struct stf_softc*)proto->ifp)->stf_proto = NULL;
201 else
202 return ENOENT;
203
204 return 0;
205 }
206
207 int stf_shutdown()
208 {
209 return 0;
210 }
211
212 void stf_reg_if_mods()
213 {
214 struct dlil_ifmod_reg_str stf_ifmod;
215
216 bzero(&stf_ifmod, sizeof(stf_ifmod));
217 stf_ifmod.add_if = stf_add_if;
218 stf_ifmod.del_if = stf_del_if;
219 stf_ifmod.add_proto = stf_add_proto;
220 stf_ifmod.del_proto = stf_del_proto;
221 stf_ifmod.ifmod_ioctl = 0;
222 stf_ifmod.shutdown = stf_shutdown;
223
224
225 if (dlil_reg_if_modules(APPLE_IF_FAM_STF, &stf_ifmod))
226 panic("Couldn't register stf modules\n");
227
228 }
229
230 u_long stf_attach_inet6(struct ifnet *ifp)
231 {
232 struct dlil_proto_reg_str reg;
233 struct dlil_demux_desc desc;
234 short native=0;
235 int stat, i;
236
237 if (stf_dl_tag != 0)
238 return stf_dl_tag;
239
240 TAILQ_INIT(&reg.demux_desc_head);
241 desc.type = DLIL_DESC_RAW;
242 desc.variants.bitmask.proto_id_length = 0;
243 desc.variants.bitmask.proto_id = 0;
244 desc.variants.bitmask.proto_id_mask = 0;
245 desc.native_type = (char *) &native;
246 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
247 reg.interface_family = ifp->if_family;
248 reg.unit_number = ifp->if_unit;
249 reg.input = 0;
250 reg.pre_output = stf_pre_output;
251 reg.event = 0;
252 reg.offer = 0;
253 reg.ioctl = 0;
254 reg.default_proto = 0;
255 reg.protocol_family = PF_INET6;
256
257 stat = dlil_attach_protocol(&reg, &stf_dl_tag);
258 if (stat) {
259 panic("stf_attach_inet6 can't attach interface\n");
260 }
261
262 return stf_dl_tag;
263 }
264
265 u_long stf_detach_inet6(struct ifnet *ifp)
266 {
267 u_long ip_dl_tag = 0;
268 int stat;
269
270 stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, AF_INET6, &ip_dl_tag);
271 if (stat == 0) {
272 stat = dlil_detach_protocol(ip_dl_tag);
273 if (stat) {
274 printf("WARNING: stf_detach can't detach IP AF_INET6 from interface\n");
275 }
276 }
277 return (stat);
278 }
279
280
281 void
282 stfattach(void)
283 {
284 struct ifnet *ifp;
285 struct stf_softc *sc;
286 int i, error;
287
288
289 int err;
290 const struct encaptab *p;
291
292 stf_reg_if_mods(); /* DLIL modules */
293
294 sc = _MALLOC(sizeof(struct stf_softc), M_DEVBUF, M_WAITOK);
295 if (sc == 0) {
296 printf("stf softc attach failed\n" );
297 return;
298 }
299
300 bzero(sc, sizeof(*sc));
301 sc->sc_if.if_name = "stf";
302 sc->sc_if.if_unit = 0;
303
304 p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
305 &in_stf_protosw, sc);
306 if (p == NULL) {
307 printf("%s: attach failed\n", if_name(&sc->sc_if));
308 FREE(sc, M_DEVBUF);
309 return;
310 }
311 sc->encap_cookie = p;
312 sc->sc_if.if_mtu = IPV6_MMTU;
313 sc->sc_if.if_flags = 0;
314 sc->sc_if.if_ioctl = stf_ioctl;
315 sc->sc_if.if_output = NULL; /* processing done in pre_output */
316 sc->sc_if.if_type = IFT_STF;
317 sc->sc_if.if_family= APPLE_IF_FAM_STF;
318 #if 0
319 /* turn off ingress filter */
320 sc->sc_if.if_flags |= IFF_LINK2;
321 #endif
322 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
323
324 if (error = dlil_if_attach(&sc->sc_if))
325 printf("stfattach: can't dlil_if_attach error=%d\n");
326 else
327 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
328
329 return ;
330 }
331
332 static int
333 stf_encapcheck(m, off, proto, arg)
334 const struct mbuf *m;
335 int off;
336 int proto;
337 void *arg;
338 {
339 struct ip ip;
340 struct in6_ifaddr *ia6;
341 struct stf_softc *sc;
342 struct in_addr a, b;
343
344 sc = (struct stf_softc *)arg;
345 if (sc == NULL)
346 return 0;
347
348 if ((sc->sc_if.if_flags & IFF_UP) == 0)
349 return 0;
350
351 /* IFF_LINK0 means "no decapsulation" */
352 if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
353 return 0;
354
355 if (proto != IPPROTO_IPV6)
356 return 0;
357
358 /* LINTED const cast */
359 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
360
361 if (ip.ip_v != 4)
362 return 0;
363
364 ia6 = stf_getsrcifa6(&sc->sc_if);
365 if (ia6 == NULL)
366 return 0;
367
368 /*
369 * check if IPv4 dst matches the IPv4 address derived from the
370 * local 6to4 address.
371 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
372 */
373 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
374 sizeof(ip.ip_dst)) != 0)
375 return 0;
376
377 /*
378 * check if IPv4 src matches the IPv4 address derived from the
379 * local 6to4 address masked by prefixmask.
380 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
381 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
382 */
383 bzero(&a, sizeof(a));
384 a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
385 a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
386 b = ip.ip_src;
387 b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
388 if (a.s_addr != b.s_addr)
389 return 0;
390
391 /* stf interface makes single side match only */
392 return 32;
393 }
394
395 static struct in6_ifaddr *
396 stf_getsrcifa6(ifp)
397 struct ifnet *ifp;
398 {
399 struct ifaddr *ia;
400 struct in_ifaddr *ia4;
401 struct sockaddr_in6 *sin6;
402 struct in_addr in;
403
404 for (ia = ifp->if_addrlist.tqh_first;
405 ia;
406 ia = ia->ifa_list.tqe_next)
407 {
408 if (ia->ifa_addr == NULL)
409 continue;
410 if (ia->ifa_addr->sa_family != AF_INET6)
411 continue;
412 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
413 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
414 continue;
415
416 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
417 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
418 ia4;
419 ia4 = TAILQ_NEXT(ia4, ia_link))
420 {
421 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
422 break;
423 }
424 if (ia4 == NULL)
425 continue;
426
427 return (struct in6_ifaddr *)ia;
428 }
429
430 return NULL;
431 }
432
433 int
434 stf_pre_output(ifp, m0, dst, rt, frame_type, address, dl_tag)
435 struct ifnet *ifp;
436 register struct mbuf **m0;
437 struct sockaddr *dst;
438 caddr_t rt;
439 char *frame_type;
440 char *address;
441 u_long dl_tag;
442 {
443 register struct mbuf *m = *m0;
444 struct stf_softc *sc;
445 struct sockaddr_in6 *dst6;
446 struct in_addr *in4;
447 struct sockaddr_in *dst4;
448 u_int8_t tos;
449 struct ip *ip;
450 struct ip6_hdr *ip6;
451 struct in6_ifaddr *ia6;
452 int error = 0 ;
453
454 sc = (struct stf_softc*)ifp;
455 dst6 = (struct sockaddr_in6 *)dst;
456
457 /* just in case */
458 if ((ifp->if_flags & IFF_UP) == 0) {
459 printf("stf: IFF_DOWN\n");
460 return ENETDOWN;
461 }
462
463 /*
464 * If we don't have an ip4 address that match my inner ip6 address,
465 * we shouldn't generate output. Without this check, we'll end up
466 * using wrong IPv4 source.
467 */
468 ia6 = stf_getsrcifa6(ifp);
469 if (ia6 == NULL) {
470 return ENETDOWN;
471 }
472
473 if (m->m_len < sizeof(*ip6)) {
474 m = m_pullup(m, sizeof(*ip6));
475 if (!m)
476 return ENOBUFS;
477 }
478 ip6 = mtod(m, struct ip6_hdr *);
479 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
480
481 /*
482 * Pickup the right outer dst addr from the list of candidates.
483 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
484 */
485 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
486 in4 = GET_V4(&ip6->ip6_dst);
487 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
488 in4 = GET_V4(&dst6->sin6_addr);
489 else {
490 return ENETUNREACH;
491 }
492
493 if (ifp->if_bpf) {
494 /*
495 * We need to prepend the address family as
496 * a four byte field. Cons up a dummy header
497 * to pacify bpf. This is safe because bpf
498 * will only read from the mbuf (i.e., it won't
499 * try to free it or keep a pointer a to it).
500 */
501 struct mbuf m0;
502 u_int32_t af = AF_INET6;
503
504 m0.m_next = m;
505 m0.m_len = 4;
506 m0.m_data = (char *)&af;
507
508 bpf_mtap(ifp, &m0);
509 }
510
511 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
512 if (m && m->m_len < sizeof(struct ip))
513 m = m_pullup(m, sizeof(struct ip));
514 if (m == NULL)
515 return ENOBUFS;
516 ip = mtod(m, struct ip *);
517
518 bzero(ip, sizeof(*ip));
519
520 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
521 &ip->ip_src, sizeof(ip->ip_src));
522 bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
523 ip->ip_p = IPPROTO_IPV6;
524 ip->ip_ttl = ip_stf_ttl;
525 ip->ip_len = m->m_pkthdr.len; /*host order*/
526 if (ifp->if_flags & IFF_LINK1)
527 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
528 else
529 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
530
531 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
532 if (dst4->sin_family != AF_INET ||
533 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
534 /* cache route doesn't match */
535 dst4->sin_family = AF_INET;
536 dst4->sin_len = sizeof(struct sockaddr_in);
537 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
538 if (sc->sc_ro.ro_rt) {
539 RTFREE(sc->sc_ro.ro_rt);
540 sc->sc_ro.ro_rt = NULL;
541 }
542 }
543
544 if (sc->sc_ro.ro_rt == NULL) {
545 rtalloc(&sc->sc_ro);
546 if (sc->sc_ro.ro_rt == NULL) {
547 return ENETUNREACH;
548 }
549 }
550
551 error = ip_output(m, NULL, &sc->sc_ro, 0, NULL);
552 if (error == 0)
553 return EJUSTRETURN;
554 }
555
556 static int
557 stf_checkaddr4(sc, in, inifp)
558 struct stf_softc *sc;
559 struct in_addr *in;
560 struct ifnet *inifp; /* incoming interface */
561 {
562 struct in_ifaddr *ia4;
563
564 /*
565 * reject packets with the following address:
566 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
567 */
568 if (IN_MULTICAST(ntohl(in->s_addr)))
569 return -1;
570 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
571 case 0: case 127: case 255:
572 return -1;
573 }
574
575 /*
576 * reject packets with broadcast
577 */
578 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
579 ia4;
580 ia4 = TAILQ_NEXT(ia4, ia_link))
581 {
582 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
583 continue;
584 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
585 return -1;
586 }
587
588 /*
589 * perform ingress filter
590 */
591 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
592 struct sockaddr_in sin;
593 struct rtentry *rt;
594
595 bzero(&sin, sizeof(sin));
596 sin.sin_family = AF_INET;
597 sin.sin_len = sizeof(struct sockaddr_in);
598 sin.sin_addr = *in;
599 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
600 if (!rt || rt->rt_ifp != inifp) {
601 #if 1
602 log(LOG_WARNING, "%s: packet from 0x%x dropped "
603 "due to ingress filter\n", if_name(&sc->sc_if),
604 (u_int32_t)ntohl(sin.sin_addr.s_addr));
605 #endif
606 if (rt)
607 rtfree(rt);
608 return -1;
609 }
610 rtfree(rt);
611 }
612
613 return 0;
614 }
615
616 static int
617 stf_checkaddr6(sc, in6, inifp)
618 struct stf_softc *sc;
619 struct in6_addr *in6;
620 struct ifnet *inifp; /* incoming interface */
621 {
622 /*
623 * check 6to4 addresses
624 */
625 if (IN6_IS_ADDR_6TO4(in6))
626 return stf_checkaddr4(sc, GET_V4(in6), inifp);
627
628 /*
629 * reject anything that look suspicious. the test is implemented
630 * in ip6_input too, but we check here as well to
631 * (1) reject bad packets earlier, and
632 * (2) to be safe against future ip6_input change.
633 */
634 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
635 return -1;
636
637 return 0;
638 }
639
640 void
641 in_stf_input(m, off)
642 struct mbuf *m;
643 int off;
644 {
645 struct stf_softc *sc;
646 struct ip *ip;
647 struct ip6_hdr *ip6;
648 u_int8_t otos, itos;
649 int s, isr, proto;
650 struct ifqueue *ifq = NULL;
651 struct ifnet *ifp;
652
653 ip = mtod(m, struct ip *);
654 proto = ip->ip_p;
655
656
657 if (proto != IPPROTO_IPV6) {
658 m_freem(m);
659 return;
660 }
661
662 ip = mtod(m, struct ip *);
663
664 sc = (struct stf_softc *)encap_getarg(m);
665
666 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
667 m_freem(m);
668 return;
669 }
670
671 ifp = &sc->sc_if;
672
673 /*
674 * perform sanity check against outer src/dst.
675 * for source, perform ingress filter as well.
676 */
677 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
678 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
679 m_freem(m);
680 return;
681 }
682
683 otos = ip->ip_tos;
684 m_adj(m, off);
685
686 if (m->m_len < sizeof(*ip6)) {
687 m = m_pullup(m, sizeof(*ip6));
688 if (!m)
689 return;
690 }
691 ip6 = mtod(m, struct ip6_hdr *);
692
693 /*
694 * perform sanity check against inner src/dst.
695 * for source, perform ingress filter as well.
696 */
697 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
698 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
699 m_freem(m);
700 return;
701 }
702
703 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
704 if ((ifp->if_flags & IFF_LINK1) != 0)
705 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
706 else
707 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
708 ip6->ip6_flow &= ~htonl(0xff << 20);
709 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
710
711 m->m_pkthdr.rcvif = ifp;
712
713 if (ifp->if_bpf) {
714 /*
715 * We need to prepend the address family as
716 * a four byte field. Cons up a dummy header
717 * to pacify bpf. This is safe because bpf
718 * will only read from the mbuf (i.e., it won't
719 * try to free it or keep a pointer a to it).
720 */
721 struct mbuf m0;
722 u_int32_t af = AF_INET6;
723
724 m0.m_next = m;
725 m0.m_len = 4;
726 m0.m_data = (char *)&af;
727
728 #ifdef HAVE_OLD_BPF
729 bpf_mtap(ifp, &m0);
730 #else
731 bpf_mtap(ifp->if_bpf, &m0);
732 #endif
733 }
734
735 /*
736 * Put the packet to the network layer input queue according to the
737 * specified address family.
738 * See net/if_gif.c for possible issues with packet processing
739 * reorder due to extra queueing.
740 */
741 ifq = &ip6intrq;
742 isr = NETISR_IPV6;
743
744 s = splimp();
745 if (IF_QFULL(ifq)) {
746 IF_DROP(ifq); /* update statistics */
747 m_freem(m);
748 splx(s);
749 return;
750 }
751 IF_ENQUEUE(ifq, m);
752 schednetisr(isr);
753 ifp->if_ipackets++;
754 ifp->if_ibytes += m->m_pkthdr.len;
755 splx(s);
756 }
757
758 /* ARGSUSED */
759 static void
760 stf_rtrequest(cmd, rt, sa)
761 int cmd;
762 struct rtentry *rt;
763 struct sockaddr *sa;
764 {
765
766 if (rt)
767 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
768 }
769
770 int
771 stf_ioctl(ifp, cmd, data)
772 struct ifnet *ifp;
773 u_long cmd;
774 void *data;
775 {
776 struct ifaddr *ifa;
777 struct ifreq *ifr;
778 struct sockaddr_in6 *sin6;
779 int error;
780
781 error = 0;
782 switch (cmd) {
783 case SIOCSIFADDR:
784 ifa = (struct ifaddr *)data;
785 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
786 error = EAFNOSUPPORT;
787 break;
788 }
789 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
790 if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
791 ifa->ifa_rtrequest = stf_rtrequest;
792 ifp->if_flags |= IFF_UP;
793 } else
794 error = EINVAL;
795 break;
796
797 case SIOCADDMULTI:
798 case SIOCDELMULTI:
799 ifr = (struct ifreq *)data;
800 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
801 ;
802 else
803 error = EAFNOSUPPORT;
804 break;
805
806 default:
807 error = EINVAL;
808 break;
809 }
810
811 return error;
812 }