if_stf.c revision 1.2 1 1.2 itojun /* $NetBSD: if_stf.c,v 1.2 2000/04/21 02:40:53 itojun Exp $ */
2 1.2 itojun /* $KAME: if_stf.c,v 1.32 2000/04/21 02:39:43 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.1 itojun * 6to4 interface, based on draft-ietf-ngtrans-6to4-03.txt.
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.1 itojun * 04 draft suggests to have link-local address onto 6to4 interface.
64 1.1 itojun * However, it seems to have no real use and does not help the above symptom
65 1.2 itojun * much. Even if we assign link-locals to interface, we cannot really
66 1.2 itojun * use link-local unicast/multicast on top of 6to4 cloud, and the above
67 1.2 itojun * analysis does not change.
68 1.1 itojun *
69 1.1 itojun * 6to4 interface has security issues. Refer to
70 1.1 itojun * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
71 1.1 itojun * for details. The code tries to filter out some of malicious packets.
72 1.1 itojun * Note that there is no way to be 100% secure.
73 1.1 itojun */
74 1.1 itojun
75 1.1 itojun #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
76 1.1 itojun #include "opt_inet.h"
77 1.1 itojun #endif
78 1.1 itojun
79 1.1 itojun #include <sys/param.h>
80 1.1 itojun #include <sys/systm.h>
81 1.1 itojun #include <sys/socket.h>
82 1.1 itojun #include <sys/sockio.h>
83 1.1 itojun #include <sys/mbuf.h>
84 1.1 itojun #include <sys/errno.h>
85 1.1 itojun #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
86 1.1 itojun #include <sys/ioctl.h>
87 1.1 itojun #endif
88 1.1 itojun #include <sys/protosw.h>
89 1.1 itojun #ifdef __FreeBSD__
90 1.1 itojun #include <sys/kernel.h>
91 1.1 itojun #endif
92 1.1 itojun #include <machine/cpu.h>
93 1.1 itojun
94 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
95 1.1 itojun #include <sys/malloc.h>
96 1.1 itojun #endif
97 1.1 itojun
98 1.1 itojun #include <net/if.h>
99 1.1 itojun #include <net/route.h>
100 1.1 itojun #include <net/netisr.h>
101 1.1 itojun #include <net/if_types.h>
102 1.1 itojun #include <net/if_stf.h>
103 1.1 itojun
104 1.1 itojun #include <netinet/in.h>
105 1.1 itojun #include <netinet/in_systm.h>
106 1.1 itojun #include <netinet/ip.h>
107 1.1 itojun #include <netinet/ip_var.h>
108 1.1 itojun #include <netinet/in_var.h>
109 1.1 itojun
110 1.1 itojun #include <netinet/ip6.h>
111 1.1 itojun #include <netinet6/ip6_var.h>
112 1.1 itojun #include <netinet6/in6_gif.h>
113 1.1 itojun #include <netinet6/in6_var.h>
114 1.1 itojun #include <netinet/ip_ecn.h>
115 1.1 itojun
116 1.1 itojun #include <netinet/ip_encap.h>
117 1.1 itojun
118 1.1 itojun #include <machine/stdarg.h>
119 1.1 itojun
120 1.1 itojun #include <net/net_osdep.h>
121 1.1 itojun
122 1.1 itojun #include "bpfilter.h"
123 1.1 itojun #include "stf.h"
124 1.1 itojun #include "gif.h" /*XXX*/
125 1.1 itojun
126 1.1 itojun #if NBPFILTER > 0
127 1.1 itojun #include <net/bpf.h>
128 1.1 itojun #endif
129 1.1 itojun
130 1.1 itojun #if NGIF > 0
131 1.1 itojun #include <net/if_gif.h>
132 1.1 itojun #endif
133 1.1 itojun
134 1.1 itojun #if NSTF > 0
135 1.1 itojun #if NSTF != 1
136 1.1 itojun # error only single stf interface allowed
137 1.1 itojun #endif
138 1.1 itojun
139 1.1 itojun #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
140 1.1 itojun #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1]))
141 1.1 itojun
142 1.1 itojun struct stf_softc {
143 1.1 itojun struct ifnet sc_if; /* common area */
144 1.1 itojun union {
145 1.1 itojun struct route __sc_ro4;
146 1.1 itojun struct route_in6 __sc_ro6; /* just for safety */
147 1.1 itojun } __sc_ro46;
148 1.1 itojun #define sc_ro __sc_ro46.__sc_ro4
149 1.1 itojun const struct encaptab *encap_cookie;
150 1.1 itojun };
151 1.1 itojun
152 1.1 itojun static struct stf_softc *stf;
153 1.1 itojun static int nstf;
154 1.1 itojun
155 1.1 itojun #if NGIF > 0
156 1.1 itojun extern int ip_gif_ttl; /*XXX*/
157 1.1 itojun #else
158 1.1 itojun static int ip_gif_ttl = 40; /*XXX*/
159 1.1 itojun #endif
160 1.1 itojun
161 1.1 itojun extern struct protosw in_stf_protosw;
162 1.1 itojun
163 1.1 itojun #ifdef __FreeBSD__
164 1.1 itojun void stfattach __P((void *));
165 1.1 itojun #else
166 1.1 itojun void stfattach __P((int));
167 1.1 itojun #endif
168 1.1 itojun static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
169 1.1 itojun static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
170 1.1 itojun static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
171 1.1 itojun struct rtentry *));
172 1.1 itojun static int stf_checkaddr4 __P((struct in_addr *, struct ifnet *));
173 1.1 itojun static int stf_checkaddr6 __P((struct in6_addr *, struct ifnet *));
174 1.1 itojun #if defined(__bsdi__) && _BSDI_VERSION >= 199802
175 1.1 itojun static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
176 1.1 itojun #else
177 1.1 itojun static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
178 1.1 itojun #endif
179 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ < 3
180 1.1 itojun static int stf_ioctl __P((struct ifnet *, int, caddr_t));
181 1.1 itojun #else
182 1.1 itojun static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
183 1.1 itojun #endif
184 1.1 itojun
185 1.1 itojun void
186 1.1 itojun stfattach(dummy)
187 1.1 itojun #ifdef __FreeBSD__
188 1.1 itojun void *dummy;
189 1.1 itojun #else
190 1.1 itojun int dummy;
191 1.1 itojun #endif
192 1.1 itojun {
193 1.1 itojun struct stf_softc *sc;
194 1.1 itojun int i;
195 1.1 itojun const struct encaptab *p;
196 1.1 itojun
197 1.1 itojun #ifdef __NetBSD__
198 1.1 itojun nstf = dummy;
199 1.1 itojun #else
200 1.1 itojun nstf = NSTF;
201 1.1 itojun #endif
202 1.1 itojun stf = malloc(nstf * sizeof(struct stf_softc), M_DEVBUF, M_WAIT);
203 1.1 itojun bzero(stf, nstf * sizeof(struct stf_softc));
204 1.1 itojun sc = stf;
205 1.1 itojun
206 1.1 itojun /* XXX just in case... */
207 1.1 itojun for (i = 0; i < nstf; i++) {
208 1.1 itojun sc = &stf[i];
209 1.1 itojun bzero(sc, sizeof(*sc));
210 1.1 itojun #if defined(__NetBSD__) || defined(__OpenBSD__)
211 1.1 itojun sprintf(sc->sc_if.if_xname, "stf%d", i);
212 1.1 itojun #else
213 1.1 itojun sc->sc_if.if_name = "stf";
214 1.1 itojun sc->sc_if.if_unit = i;
215 1.1 itojun #endif
216 1.1 itojun
217 1.1 itojun p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
218 1.1 itojun &in_stf_protosw, sc);
219 1.1 itojun if (p == NULL) {
220 1.1 itojun printf("%s: attach failed\n", if_name(&sc->sc_if));
221 1.1 itojun continue;
222 1.1 itojun }
223 1.1 itojun sc->encap_cookie = p;
224 1.1 itojun
225 1.1 itojun sc->sc_if.if_mtu = IPV6_MMTU;
226 1.1 itojun sc->sc_if.if_flags = 0;
227 1.1 itojun sc->sc_if.if_ioctl = stf_ioctl;
228 1.1 itojun sc->sc_if.if_output = stf_output;
229 1.1 itojun sc->sc_if.if_type = IFT_STF;
230 1.1 itojun if_attach(&sc->sc_if);
231 1.1 itojun #if NBPFILTER > 0
232 1.1 itojun #ifdef HAVE_OLD_BPF
233 1.1 itojun bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
234 1.1 itojun #else
235 1.1 itojun bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL, sizeof(u_int));
236 1.1 itojun #endif
237 1.1 itojun #endif
238 1.1 itojun }
239 1.1 itojun }
240 1.1 itojun
241 1.1 itojun #ifdef __FreeBSD__
242 1.1 itojun PSEUDO_SET(stfattach, if_stf);
243 1.1 itojun #endif
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.1 itojun if (proto != IPPROTO_IPV6)
265 1.1 itojun return 0;
266 1.1 itojun
267 1.1 itojun /* LINTED const cast */
268 1.1 itojun m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
269 1.1 itojun
270 1.1 itojun if (ip.ip_v != 4)
271 1.1 itojun return 0;
272 1.1 itojun
273 1.1 itojun ia6 = stf_getsrcifa6(&sc->sc_if);
274 1.1 itojun if (ia6 == NULL)
275 1.1 itojun return 0;
276 1.1 itojun
277 1.1 itojun /*
278 1.1 itojun * check if IPv4 dst matches the IPv4 address derived from the
279 1.1 itojun * local 6to4 address.
280 1.1 itojun * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
281 1.1 itojun */
282 1.1 itojun if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
283 1.1 itojun sizeof(ip.ip_dst)) != 0)
284 1.1 itojun return 0;
285 1.1 itojun
286 1.1 itojun /*
287 1.1 itojun * check if IPv4 src matches the IPv4 address derived from the
288 1.1 itojun * local 6to4 address masked by prefixmask.
289 1.1 itojun * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
290 1.1 itojun * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
291 1.1 itojun */
292 1.1 itojun bzero(&a, sizeof(a));
293 1.1 itojun a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
294 1.1 itojun a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
295 1.1 itojun b = ip.ip_src;
296 1.1 itojun b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
297 1.1 itojun if (a.s_addr != b.s_addr)
298 1.1 itojun return 0;
299 1.1 itojun
300 1.1 itojun /* stf interface makes single side match only */
301 1.1 itojun return 32;
302 1.1 itojun }
303 1.1 itojun
304 1.1 itojun static struct in6_ifaddr *
305 1.1 itojun stf_getsrcifa6(ifp)
306 1.1 itojun struct ifnet *ifp;
307 1.1 itojun {
308 1.1 itojun struct ifaddr *ia;
309 1.1 itojun struct in_ifaddr *ia4;
310 1.1 itojun struct sockaddr_in6 *sin6;
311 1.1 itojun struct in_addr in;
312 1.1 itojun
313 1.1 itojun #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
314 1.1 itojun for (ia = ifp->if_addrlist; ia; ia = ia->ifa_next)
315 1.1 itojun #else
316 1.1 itojun for (ia = ifp->if_addrlist.tqh_first;
317 1.1 itojun ia;
318 1.1 itojun ia = ia->ifa_list.tqe_next)
319 1.1 itojun #endif
320 1.1 itojun {
321 1.1 itojun if (ia->ifa_addr == NULL)
322 1.1 itojun continue;
323 1.1 itojun if (ia->ifa_addr->sa_family != AF_INET6)
324 1.1 itojun continue;
325 1.1 itojun sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
326 1.1 itojun if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
327 1.1 itojun continue;
328 1.1 itojun
329 1.1 itojun bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
330 1.1 itojun #ifdef __NetBSD__
331 1.1 itojun INADDR_TO_IA(in, ia4);
332 1.1 itojun #else
333 1.1 itojun #ifdef __OpenBSD__
334 1.1 itojun for (ia4 = in_ifaddr.tqh_first;
335 1.1 itojun ia4;
336 1.1 itojun ia4 = ia4->ia_list.tqe_next)
337 1.1 itojun #else
338 1.1 itojun for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
339 1.1 itojun #endif
340 1.1 itojun {
341 1.1 itojun if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
342 1.1 itojun break;
343 1.1 itojun }
344 1.1 itojun #endif
345 1.1 itojun if (ia4 == NULL)
346 1.1 itojun continue;
347 1.1 itojun
348 1.1 itojun return (struct in6_ifaddr *)ia;
349 1.1 itojun }
350 1.1 itojun
351 1.1 itojun return NULL;
352 1.1 itojun }
353 1.1 itojun
354 1.1 itojun static int
355 1.1 itojun stf_output(ifp, m, dst, rt)
356 1.1 itojun struct ifnet *ifp;
357 1.1 itojun struct mbuf *m;
358 1.1 itojun struct sockaddr *dst;
359 1.1 itojun struct rtentry *rt;
360 1.1 itojun {
361 1.1 itojun struct stf_softc *sc;
362 1.1 itojun struct sockaddr_in6 *dst6;
363 1.1 itojun struct sockaddr_in *dst4;
364 1.1 itojun u_int8_t tos;
365 1.1 itojun struct ip *ip;
366 1.1 itojun struct ip6_hdr *ip6;
367 1.1 itojun struct in6_ifaddr *ia6;
368 1.1 itojun
369 1.1 itojun sc = (struct stf_softc*)ifp;
370 1.1 itojun dst6 = (struct sockaddr_in6 *)dst;
371 1.1 itojun
372 1.1 itojun /* just in case */
373 1.1 itojun if ((ifp->if_flags & IFF_UP) == 0) {
374 1.1 itojun m_freem(m);
375 1.1 itojun return ENETDOWN;
376 1.1 itojun }
377 1.1 itojun
378 1.1 itojun /*
379 1.1 itojun * If we don't have an ip4 address that match my inner ip6 address,
380 1.1 itojun * we shouldn't generate output. Without this check, we'll end up
381 1.1 itojun * using wrong IPv4 source.
382 1.1 itojun */
383 1.1 itojun ia6 = stf_getsrcifa6(ifp);
384 1.1 itojun if (ia6 == NULL) {
385 1.1 itojun m_freem(m);
386 1.1 itojun return ENETDOWN;
387 1.1 itojun }
388 1.1 itojun
389 1.1 itojun if (m->m_len < sizeof(*ip6)) {
390 1.1 itojun m = m_pullup(m, sizeof(*ip6));
391 1.1 itojun if (!m)
392 1.1 itojun return ENOBUFS;
393 1.1 itojun }
394 1.1 itojun ip6 = mtod(m, struct ip6_hdr *);
395 1.1 itojun tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
396 1.1 itojun
397 1.1 itojun M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
398 1.1 itojun if (m && m->m_len < sizeof(struct ip))
399 1.1 itojun m = m_pullup(m, sizeof(struct ip));
400 1.1 itojun if (m == NULL)
401 1.1 itojun return ENOBUFS;
402 1.1 itojun ip = mtod(m, struct ip *);
403 1.1 itojun
404 1.1 itojun bzero(ip, sizeof(*ip));
405 1.1 itojun
406 1.1 itojun bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
407 1.1 itojun &ip->ip_src, sizeof(ip->ip_src));
408 1.1 itojun bcopy(GET_V4(&dst6->sin6_addr), &ip->ip_dst, sizeof(ip->ip_dst));
409 1.1 itojun ip->ip_p = IPPROTO_IPV6;
410 1.1 itojun ip->ip_ttl = ip_gif_ttl; /*XXX*/
411 1.1 itojun ip->ip_len = m->m_pkthdr.len; /*host order*/
412 1.1 itojun if (ifp->if_flags & IFF_LINK1)
413 1.1 itojun ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
414 1.1 itojun
415 1.1 itojun dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
416 1.1 itojun if (dst4->sin_family != AF_INET ||
417 1.1 itojun bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
418 1.1 itojun /* cache route doesn't match */
419 1.1 itojun dst4->sin_family = AF_INET;
420 1.1 itojun dst4->sin_len = sizeof(struct sockaddr_in);
421 1.1 itojun bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
422 1.1 itojun if (sc->sc_ro.ro_rt) {
423 1.1 itojun RTFREE(sc->sc_ro.ro_rt);
424 1.1 itojun sc->sc_ro.ro_rt = NULL;
425 1.1 itojun }
426 1.1 itojun }
427 1.1 itojun
428 1.1 itojun if (sc->sc_ro.ro_rt == NULL) {
429 1.1 itojun rtalloc(&sc->sc_ro);
430 1.1 itojun if (sc->sc_ro.ro_rt == NULL) {
431 1.1 itojun m_freem(m);
432 1.1 itojun return ENETUNREACH;
433 1.1 itojun }
434 1.1 itojun }
435 1.1 itojun
436 1.1 itojun #ifndef __OpenBSD__
437 1.1 itojun return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
438 1.1 itojun #else
439 1.1 itojun return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
440 1.1 itojun #endif
441 1.1 itojun }
442 1.1 itojun
443 1.1 itojun static int
444 1.1 itojun stf_checkaddr4(in, ifp)
445 1.1 itojun struct in_addr *in;
446 1.1 itojun struct ifnet *ifp; /* incoming interface */
447 1.1 itojun {
448 1.1 itojun struct in_ifaddr *ia4;
449 1.1 itojun
450 1.1 itojun /*
451 1.1 itojun * reject packets with the following address:
452 1.1 itojun * 224.0.0.0/4 0.0.0.0/32 255.255.255.255/32
453 1.1 itojun */
454 1.1 itojun if (IN_MULTICAST(in->s_addr) || in->s_addr == INADDR_ANY ||
455 1.1 itojun in->s_addr == INADDR_BROADCAST) {
456 1.1 itojun return -1;
457 1.1 itojun }
458 1.1 itojun
459 1.1 itojun /*
460 1.1 itojun * reject packets with broadcast
461 1.1 itojun */
462 1.1 itojun #if defined(__OpenBSD__) || defined(__NetBSD__)
463 1.1 itojun for (ia4 = in_ifaddr.tqh_first; ia4; ia4 = ia4->ia_list.tqe_next)
464 1.1 itojun #else
465 1.1 itojun for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
466 1.1 itojun #endif
467 1.1 itojun {
468 1.1 itojun if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
469 1.1 itojun continue;
470 1.1 itojun if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
471 1.1 itojun return -1;
472 1.1 itojun }
473 1.1 itojun
474 1.1 itojun /*
475 1.1 itojun * perform ingress filter
476 1.1 itojun */
477 1.1 itojun if (ifp) {
478 1.1 itojun struct sockaddr_in sin;
479 1.1 itojun struct rtentry *rt;
480 1.1 itojun
481 1.1 itojun bzero(&sin, sizeof(sin));
482 1.1 itojun sin.sin_family = AF_INET;
483 1.1 itojun sin.sin_len = sizeof(struct sockaddr_in);
484 1.1 itojun sin.sin_addr = *in;
485 1.1 itojun #ifdef __FreeBSD__
486 1.1 itojun rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
487 1.1 itojun #else
488 1.1 itojun rt = rtalloc1((struct sockaddr *)&sin, 0);
489 1.1 itojun #endif
490 1.1 itojun if (!rt)
491 1.1 itojun return -1;
492 1.1 itojun if (rt->rt_ifp != ifp) {
493 1.1 itojun rtfree(rt);
494 1.1 itojun return -1;
495 1.1 itojun }
496 1.1 itojun rtfree(rt);
497 1.1 itojun }
498 1.1 itojun
499 1.1 itojun return 0;
500 1.1 itojun }
501 1.1 itojun
502 1.1 itojun static int
503 1.1 itojun stf_checkaddr6(in6, ifp)
504 1.1 itojun struct in6_addr *in6;
505 1.1 itojun struct ifnet *ifp; /* incoming interface */
506 1.1 itojun {
507 1.1 itojun /*
508 1.1 itojun * check 6to4 addresses
509 1.1 itojun */
510 1.1 itojun if (IN6_IS_ADDR_6TO4(in6))
511 1.1 itojun return stf_checkaddr4(GET_V4(in6), ifp);
512 1.1 itojun
513 1.1 itojun /*
514 1.1 itojun * reject anything that look suspicious. the test is implemented
515 1.1 itojun * in ip6_input too, but we check here as well to
516 1.1 itojun * (1) reject bad packets earlier, and
517 1.1 itojun * (2) to be safe against future ip6_input change.
518 1.1 itojun */
519 1.1 itojun if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
520 1.1 itojun return -1;
521 1.1 itojun
522 1.1 itojun return 0;
523 1.1 itojun }
524 1.1 itojun
525 1.1 itojun void
526 1.1 itojun #if __STDC__
527 1.1 itojun in_stf_input(struct mbuf *m, ...)
528 1.1 itojun #else
529 1.1 itojun in_stf_input(m, va_alist)
530 1.1 itojun register struct mbuf *m;
531 1.1 itojun #endif
532 1.1 itojun {
533 1.1 itojun int off, proto;
534 1.1 itojun struct stf_softc *sc;
535 1.1 itojun struct ip *ip;
536 1.1 itojun struct ip6_hdr *ip6;
537 1.1 itojun u_int8_t otos, itos;
538 1.1 itojun int s, isr;
539 1.1 itojun struct ifqueue *ifq = NULL;
540 1.1 itojun struct ifnet *ifp;
541 1.1 itojun va_list ap;
542 1.1 itojun
543 1.1 itojun va_start(ap, m);
544 1.1 itojun off = va_arg(ap, int);
545 1.1 itojun proto = va_arg(ap, int);
546 1.1 itojun va_end(ap);
547 1.1 itojun
548 1.1 itojun if (proto != IPPROTO_IPV6) {
549 1.1 itojun m_freem(m);
550 1.1 itojun return;
551 1.1 itojun }
552 1.1 itojun
553 1.1 itojun ip = mtod(m, struct ip *);
554 1.1 itojun
555 1.1 itojun sc = (struct stf_softc *)encap_getarg(m);
556 1.1 itojun
557 1.1 itojun if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
558 1.1 itojun m_freem(m);
559 1.1 itojun return;
560 1.1 itojun }
561 1.1 itojun
562 1.1 itojun ifp = &sc->sc_if;
563 1.1 itojun
564 1.1 itojun /*
565 1.1 itojun * perform sanity check against outer src/dst.
566 1.1 itojun * for source, perform ingress filter as well.
567 1.1 itojun */
568 1.1 itojun if (stf_checkaddr4(&ip->ip_dst, NULL) < 0 ||
569 1.1 itojun stf_checkaddr4(&ip->ip_src, m->m_pkthdr.rcvif) < 0) {
570 1.1 itojun m_freem(m);
571 1.1 itojun return;
572 1.1 itojun }
573 1.1 itojun
574 1.1 itojun otos = ip->ip_tos;
575 1.1 itojun m_adj(m, off);
576 1.1 itojun
577 1.1 itojun if (m->m_len < sizeof(*ip6)) {
578 1.1 itojun m = m_pullup(m, sizeof(*ip6));
579 1.1 itojun if (!m)
580 1.1 itojun return;
581 1.1 itojun }
582 1.1 itojun ip6 = mtod(m, struct ip6_hdr *);
583 1.1 itojun
584 1.1 itojun /*
585 1.1 itojun * perform sanity check against inner src/dst.
586 1.1 itojun * for source, perform ingress filter as well.
587 1.1 itojun */
588 1.1 itojun if (stf_checkaddr6(&ip6->ip6_dst, NULL) < 0 ||
589 1.1 itojun stf_checkaddr6(&ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
590 1.1 itojun m_freem(m);
591 1.1 itojun return;
592 1.1 itojun }
593 1.1 itojun
594 1.1 itojun itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
595 1.1 itojun if ((ifp->if_flags & IFF_LINK1) != 0)
596 1.1 itojun ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
597 1.1 itojun ip6->ip6_flow &= ~htonl(0xff << 20);
598 1.1 itojun ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
599 1.1 itojun
600 1.1 itojun m->m_pkthdr.rcvif = ifp;
601 1.1 itojun
602 1.1 itojun #if NBPFILTER > 0
603 1.1 itojun if (ifp->if_bpf) {
604 1.1 itojun /*
605 1.1 itojun * We need to prepend the address family as
606 1.1 itojun * a four byte field. Cons up a dummy header
607 1.1 itojun * to pacify bpf. This is safe because bpf
608 1.1 itojun * will only read from the mbuf (i.e., it won't
609 1.1 itojun * try to free it or keep a pointer a to it).
610 1.1 itojun */
611 1.1 itojun struct mbuf m0;
612 1.1 itojun u_int af = AF_INET6;
613 1.1 itojun
614 1.1 itojun m0.m_next = m;
615 1.1 itojun m0.m_len = 4;
616 1.1 itojun m0.m_data = (char *)⁡
617 1.1 itojun
618 1.1 itojun #ifdef HAVE_OLD_BPF
619 1.1 itojun bpf_mtap(ifp, &m0);
620 1.1 itojun #else
621 1.1 itojun bpf_mtap(ifp->if_bpf, &m0);
622 1.1 itojun #endif
623 1.1 itojun }
624 1.1 itojun #endif /*NBPFILTER > 0*/
625 1.1 itojun
626 1.1 itojun /*
627 1.1 itojun * Put the packet to the network layer input queue according to the
628 1.1 itojun * specified address family.
629 1.1 itojun * See net/if_gif.c for possible issues with packet processing
630 1.1 itojun * reorder due to extra queueing.
631 1.1 itojun */
632 1.1 itojun ifq = &ip6intrq;
633 1.1 itojun isr = NETISR_IPV6;
634 1.1 itojun
635 1.1 itojun s = splimp();
636 1.1 itojun if (IF_QFULL(ifq)) {
637 1.1 itojun IF_DROP(ifq); /* update statistics */
638 1.1 itojun m_freem(m);
639 1.1 itojun splx(s);
640 1.1 itojun return;
641 1.1 itojun }
642 1.1 itojun IF_ENQUEUE(ifq, m);
643 1.1 itojun schednetisr(isr);
644 1.1 itojun ifp->if_ipackets++;
645 1.1 itojun ifp->if_ibytes += m->m_pkthdr.len;
646 1.1 itojun splx(s);
647 1.1 itojun }
648 1.1 itojun
649 1.1 itojun /* ARGSUSED */
650 1.1 itojun static void
651 1.1 itojun stf_rtrequest(cmd, rt, sa)
652 1.1 itojun int cmd;
653 1.1 itojun struct rtentry *rt;
654 1.1 itojun #if defined(__bsdi__) && _BSDI_VERSION >= 199802
655 1.1 itojun struct rt_addrinfo *sa;
656 1.1 itojun #else
657 1.1 itojun struct sockaddr *sa;
658 1.1 itojun #endif
659 1.1 itojun {
660 1.1 itojun
661 1.1 itojun if (rt)
662 1.1 itojun rt->rt_rmx.rmx_mtu = IPV6_MMTU;
663 1.1 itojun }
664 1.1 itojun
665 1.1 itojun static int
666 1.1 itojun stf_ioctl(ifp, cmd, data)
667 1.1 itojun struct ifnet *ifp;
668 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ < 3
669 1.1 itojun int cmd;
670 1.1 itojun #else
671 1.1 itojun u_long cmd;
672 1.1 itojun #endif
673 1.1 itojun caddr_t data;
674 1.1 itojun {
675 1.1 itojun struct ifaddr *ifa;
676 1.1 itojun struct ifreq *ifr;
677 1.1 itojun struct sockaddr_in6 *sin6;
678 1.1 itojun int error;
679 1.1 itojun
680 1.1 itojun error = 0;
681 1.1 itojun switch (cmd) {
682 1.1 itojun case SIOCSIFADDR:
683 1.1 itojun ifa = (struct ifaddr *)data;
684 1.1 itojun if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
685 1.1 itojun error = EAFNOSUPPORT;
686 1.1 itojun break;
687 1.1 itojun }
688 1.1 itojun sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
689 1.1 itojun if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
690 1.1 itojun ifa->ifa_rtrequest = stf_rtrequest;
691 1.1 itojun ifp->if_flags |= IFF_UP;
692 1.1 itojun } else
693 1.1 itojun error = EINVAL;
694 1.1 itojun break;
695 1.1 itojun
696 1.1 itojun case SIOCADDMULTI:
697 1.1 itojun case SIOCDELMULTI:
698 1.1 itojun ifr = (struct ifreq *)data;
699 1.1 itojun if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
700 1.1 itojun ;
701 1.1 itojun else
702 1.1 itojun error = EAFNOSUPPORT;
703 1.1 itojun break;
704 1.1 itojun
705 1.1 itojun default:
706 1.1 itojun error = EINVAL;
707 1.1 itojun break;
708 1.1 itojun }
709 1.1 itojun
710 1.1 itojun return error;
711 1.1 itojun }
712 1.1 itojun
713 1.1 itojun #endif /* NSTF > 0 */
714