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