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