]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_stf.c
xnu-792.6.76.tar.gz
[apple/xnu.git] / bsd / net / if_stf.c
CommitLineData
9bccf70c
A
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>
9bccf70c
A
86
87#include <sys/malloc.h>
88
89#include <net/if.h>
90#include <net/route.h>
9bccf70c 91#include <net/if_types.h>
9bccf70c
A
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
115struct stf_softc {
116 struct ifnet sc_if; /* common area */
117#ifdef __APPLE__
91447636 118 u_long sc_protocol_family; /* dlil protocol attached */
9bccf70c
A
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
128static struct stf_softc *stf;
129
130#ifdef __APPLE__
91447636 131void stfattach (void);
9bccf70c
A
132#endif
133
134#ifndef __APPLE__
135static MALLOC_DEFINE(M_STF, "stf", "6to4 Tunnel Interface");
136#endif
137static int ip_stf_ttl = 40;
138
91447636 139static void in_stf_input(struct mbuf *, int);
9bccf70c
A
140extern struct domain inetdomain;
141struct protosw in_stf_protosw =
142{ SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR,
55e303ae 143 in_stf_input, 0, 0, rip_ctloutput,
9bccf70c 144 0,
9bccf70c 145 0,
91447636
A
146 &rip_usrreqs,
147 0, rip_unlock, 0
9bccf70c
A
148};
149
91447636
A
150static int stf_encapcheck(const struct mbuf *, int, int, void *);
151static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
152int stf_pre_output(struct ifnet *, u_long, register struct mbuf **,
153 const struct sockaddr *, caddr_t, char *, char *);
154static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
155 struct ifnet *);
156static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
157 struct ifnet *);
158static void stf_rtrequest(int, struct rtentry *, struct sockaddr *);
159int stf_ioctl(struct ifnet *, u_long, void *);
9bccf70c
A
160
161static
91447636
A
162int stf_add_proto(
163 struct ifnet *ifp,
164 u_long protocol_family,
165 struct ddesc_head_str *desc_head)
9bccf70c
A
166{
167 /* Only one protocol may be attached at a time */
91447636
A
168 struct stf_softc* stf = (struct stf_softc*)ifp;
169 if (stf->sc_protocol_family == 0)
170 stf->sc_protocol_family = protocol_family;
9bccf70c
A
171 else {
172 printf("stf_add_proto: stf already has a proto\n");
91447636 173 return EBUSY;
9bccf70c 174 }
91447636
A
175
176 return 0;
9bccf70c
A
177}
178
179static
91447636
A
180int stf_del_proto(
181 struct ifnet *ifp,
182 u_long protocol_family)
9bccf70c 183{
91447636
A
184 if (((struct stf_softc*)ifp)->sc_protocol_family == protocol_family)
185 ((struct stf_softc*)ifp)->sc_protocol_family = 0;
9bccf70c
A
186 else
187 return ENOENT;
188
189 return 0;
190}
191
91447636
A
192static int
193stf_attach_inet6(struct ifnet *ifp, u_long protocol_family)
9bccf70c
A
194{
195 struct dlil_proto_reg_str reg;
9bccf70c
A
196 int stat, i;
197
91447636 198 bzero(&reg, sizeof(reg));
9bccf70c 199 TAILQ_INIT(&reg.demux_desc_head);
9bccf70c
A
200 reg.interface_family = ifp->if_family;
201 reg.unit_number = ifp->if_unit;
9bccf70c 202 reg.pre_output = stf_pre_output;
9bccf70c
A
203 reg.protocol_family = PF_INET6;
204
91447636 205 stat = dlil_attach_protocol(&reg);
9bccf70c 206
55e303ae 207 return stat;
9bccf70c
A
208}
209
91447636
A
210static int
211stf_demux(
212 struct ifnet *ifp,
213 struct mbuf *m,
214 char *frame_ptr,
215 u_long *protocol_family)
9bccf70c 216{
91447636
A
217 *protocol_family = PF_INET6;
218 return 0;
9bccf70c
A
219}
220
55e303ae
A
221void stf_reg_if_mods()
222{
55e303ae
A
223 int error;
224
55e303ae 225 /* Register protocol registration functions */
91447636 226 if ( error = dlil_reg_proto_module(AF_INET6, APPLE_IF_FAM_STF, stf_attach_inet6, NULL) != 0)
55e303ae
A
227 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error);
228}
9bccf70c
A
229
230void
231stfattach(void)
232{
233 struct ifnet *ifp;
234 struct stf_softc *sc;
235 int i, error;
9bccf70c
A
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;
91447636
A
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;
9bccf70c
A
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
282static int
283stf_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
345static struct in6_ifaddr *
346stf_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
91447636 354 ifnet_lock_shared(ifp);
9bccf70c
A
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));
91447636 368 lck_mtx_lock(rt_mtx);
9bccf70c
A
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 }
91447636 376 lck_mtx_unlock(rt_mtx);
9bccf70c
A
377 if (ia4 == NULL)
378 continue;
379
91447636 380 ifnet_lock_done(ifp);
9bccf70c
A
381 return (struct in6_ifaddr *)ia;
382 }
91447636 383 ifnet_lock_done(ifp);
9bccf70c
A
384
385 return NULL;
386}
387
388int
91447636
A
389stf_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)
9bccf70c
A
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));
cc9f6e38
A
430 if (!m) {
431 *m0 = NULL; /* makes sure this won't be double freed */
9bccf70c 432 return ENOBUFS;
cc9f6e38 433 }
9bccf70c
A
434 }
435 ip6 = mtod(m, struct ip6_hdr *);
436 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
437
438 /*
439 * Pickup the right outer dst addr from the list of candidates.
440 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
441 */
442 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
443 in4 = GET_V4(&ip6->ip6_dst);
444 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
445 in4 = GET_V4(&dst6->sin6_addr);
446 else {
447 return ENETUNREACH;
448 }
449
450 if (ifp->if_bpf) {
451 /*
452 * We need to prepend the address family as
453 * a four byte field. Cons up a dummy header
454 * to pacify bpf. This is safe because bpf
455 * will only read from the mbuf (i.e., it won't
456 * try to free it or keep a pointer a to it).
457 */
91447636 458 struct mbuf m1;
9bccf70c
A
459 u_int32_t af = AF_INET6;
460
91447636
A
461 m1.m_next = m;
462 m1.m_len = 4;
463 m1.m_data = (char *)&af;
9bccf70c 464
91447636 465 bpf_mtap(ifp, &m1);
9bccf70c
A
466 }
467
468 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
469 if (m && m->m_len < sizeof(struct ip))
470 m = m_pullup(m, sizeof(struct ip));
cc9f6e38
A
471 if (m == NULL) {
472 *m0 = NULL;
9bccf70c 473 return ENOBUFS;
cc9f6e38 474 }
9bccf70c
A
475 ip = mtod(m, struct ip *);
476
477 bzero(ip, sizeof(*ip));
478
479 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
480 &ip->ip_src, sizeof(ip->ip_src));
481 bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
482 ip->ip_p = IPPROTO_IPV6;
483 ip->ip_ttl = ip_stf_ttl;
484 ip->ip_len = m->m_pkthdr.len; /*host order*/
485 if (ifp->if_flags & IFF_LINK1)
486 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
487 else
488 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
489
490 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
491 if (dst4->sin_family != AF_INET ||
492 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
493 /* cache route doesn't match */
494 dst4->sin_family = AF_INET;
495 dst4->sin_len = sizeof(struct sockaddr_in);
496 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
497 if (sc->sc_ro.ro_rt) {
91447636 498 rtfree(sc->sc_ro.ro_rt);
9bccf70c
A
499 sc->sc_ro.ro_rt = NULL;
500 }
501 }
502
503 if (sc->sc_ro.ro_rt == NULL) {
504 rtalloc(&sc->sc_ro);
505 if (sc->sc_ro.ro_rt == NULL) {
506 return ENETUNREACH;
507 }
508 }
509
510 error = ip_output(m, NULL, &sc->sc_ro, 0, NULL);
511 if (error == 0)
512 return EJUSTRETURN;
91447636 513
cc9f6e38 514 *m0 = NULL;
91447636 515 return error;
9bccf70c
A
516}
517
518static int
519stf_checkaddr4(sc, in, inifp)
520 struct stf_softc *sc;
521 struct in_addr *in;
522 struct ifnet *inifp; /* incoming interface */
523{
524 struct in_ifaddr *ia4;
525
526 /*
527 * reject packets with the following address:
528 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
529 */
530 if (IN_MULTICAST(ntohl(in->s_addr)))
531 return -1;
532 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
533 case 0: case 127: case 255:
534 return -1;
535 }
536
537 /*
538 * reject packets with broadcast
539 */
91447636 540 lck_mtx_lock(rt_mtx);
9bccf70c
A
541 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
542 ia4;
543 ia4 = TAILQ_NEXT(ia4, ia_link))
544 {
545 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
546 continue;
91447636
A
547 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
548 lck_mtx_unlock(rt_mtx);
9bccf70c 549 return -1;
91447636 550 }
9bccf70c 551 }
91447636 552 lck_mtx_unlock(rt_mtx);
9bccf70c
A
553
554 /*
555 * perform ingress filter
556 */
557 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
558 struct sockaddr_in sin;
559 struct rtentry *rt;
560
561 bzero(&sin, sizeof(sin));
562 sin.sin_family = AF_INET;
563 sin.sin_len = sizeof(struct sockaddr_in);
564 sin.sin_addr = *in;
565 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
566 if (!rt || rt->rt_ifp != inifp) {
567#if 1
568 log(LOG_WARNING, "%s: packet from 0x%x dropped "
569 "due to ingress filter\n", if_name(&sc->sc_if),
570 (u_int32_t)ntohl(sin.sin_addr.s_addr));
571#endif
572 if (rt)
573 rtfree(rt);
574 return -1;
575 }
576 rtfree(rt);
577 }
578
579 return 0;
580}
581
582static int
583stf_checkaddr6(sc, in6, inifp)
584 struct stf_softc *sc;
585 struct in6_addr *in6;
586 struct ifnet *inifp; /* incoming interface */
587{
588 /*
589 * check 6to4 addresses
590 */
591 if (IN6_IS_ADDR_6TO4(in6))
592 return stf_checkaddr4(sc, GET_V4(in6), inifp);
593
594 /*
595 * reject anything that look suspicious. the test is implemented
596 * in ip6_input too, but we check here as well to
597 * (1) reject bad packets earlier, and
598 * (2) to be safe against future ip6_input change.
599 */
600 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
601 return -1;
602
603 return 0;
604}
605
91447636 606static void
9bccf70c
A
607in_stf_input(m, off)
608 struct mbuf *m;
609 int off;
610{
611 struct stf_softc *sc;
612 struct ip *ip;
613 struct ip6_hdr *ip6;
614 u_int8_t otos, itos;
91447636 615 int proto;
9bccf70c
A
616 struct ifnet *ifp;
617
618 ip = mtod(m, struct ip *);
619 proto = ip->ip_p;
620
621
622 if (proto != IPPROTO_IPV6) {
623 m_freem(m);
624 return;
625 }
626
627 ip = mtod(m, struct ip *);
628
629 sc = (struct stf_softc *)encap_getarg(m);
630
631 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
632 m_freem(m);
633 return;
634 }
635
636 ifp = &sc->sc_if;
637
638 /*
639 * perform sanity check against outer src/dst.
640 * for source, perform ingress filter as well.
641 */
642 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
643 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
644 m_freem(m);
645 return;
646 }
647
648 otos = ip->ip_tos;
649 m_adj(m, off);
650
651 if (m->m_len < sizeof(*ip6)) {
652 m = m_pullup(m, sizeof(*ip6));
653 if (!m)
654 return;
655 }
656 ip6 = mtod(m, struct ip6_hdr *);
657
658 /*
659 * perform sanity check against inner src/dst.
660 * for source, perform ingress filter as well.
661 */
662 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
663 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
664 m_freem(m);
665 return;
666 }
667
668 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
669 if ((ifp->if_flags & IFF_LINK1) != 0)
670 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
671 else
672 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
673 ip6->ip6_flow &= ~htonl(0xff << 20);
674 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
675
676 m->m_pkthdr.rcvif = ifp;
677
678 if (ifp->if_bpf) {
679 /*
680 * We need to prepend the address family as
681 * a four byte field. Cons up a dummy header
682 * to pacify bpf. This is safe because bpf
683 * will only read from the mbuf (i.e., it won't
684 * try to free it or keep a pointer a to it).
685 */
686 struct mbuf m0;
687 u_int32_t af = AF_INET6;
688
689 m0.m_next = m;
690 m0.m_len = 4;
691 m0.m_data = (char *)&af;
692
693#ifdef HAVE_OLD_BPF
694 bpf_mtap(ifp, &m0);
695#else
696 bpf_mtap(ifp->if_bpf, &m0);
697#endif
698 }
699
700 /*
701 * Put the packet to the network layer input queue according to the
702 * specified address family.
703 * See net/if_gif.c for possible issues with packet processing
704 * reorder due to extra queueing.
705 */
91447636 706 proto_input(PF_INET6, m);
9bccf70c
A
707 ifp->if_ipackets++;
708 ifp->if_ibytes += m->m_pkthdr.len;
55e303ae
A
709
710 return;
9bccf70c
A
711}
712
713/* ARGSUSED */
714static void
715stf_rtrequest(cmd, rt, sa)
716 int cmd;
717 struct rtentry *rt;
718 struct sockaddr *sa;
719{
720
721 if (rt)
722 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
723}
724
725int
726stf_ioctl(ifp, cmd, data)
727 struct ifnet *ifp;
728 u_long cmd;
729 void *data;
730{
731 struct ifaddr *ifa;
732 struct ifreq *ifr;
733 struct sockaddr_in6 *sin6;
734 int error;
735
736 error = 0;
737 switch (cmd) {
738 case SIOCSIFADDR:
739 ifa = (struct ifaddr *)data;
740 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
741 error = EAFNOSUPPORT;
742 break;
743 }
744 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
745 if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
91447636
A
746 if ( !(ifnet_flags( ifp ) & IFF_UP) ) {
747 /* do this only if the interface is not already up */
748 ifa->ifa_rtrequest = stf_rtrequest;
749 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
750 }
9bccf70c
A
751 } else
752 error = EINVAL;
753 break;
754
755 case SIOCADDMULTI:
756 case SIOCDELMULTI:
757 ifr = (struct ifreq *)data;
758 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
759 ;
760 else
761 error = EAFNOSUPPORT;
762 break;
763
764 default:
765 error = EINVAL;
766 break;
767 }
768
769 return error;
770}