1 /* $NetBSD: in6_gif.c,v 1.56 2008/04/24 11:38:38 ad Exp $ */
2 /* $KAME: in6_gif.c,v 1.62 2001/07/29 04:27:25 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 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 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: in6_gif.c,v 1.56 2008/04/24 11:38:38 ad Exp $");
35
36 #include "opt_inet.h"
37 #include "opt_iso.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/mbuf.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/queue.h>
47 #include <sys/syslog.h>
48 #include <sys/protosw.h>
49 #include <sys/kernel.h>
50
51 #include <net/if.h>
52 #include <net/route.h>
53
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #ifdef INET
57 #include <netinet/ip.h>
58 #endif
59 #include <netinet/ip_encap.h>
60 #ifdef INET6
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/ip6_private.h>
64 #include <netinet6/in6_gif.h>
65 #include <netinet6/in6_var.h>
66 #endif
67 #include <netinet6/ip6protosw.h>
68 #include <netinet/ip_ecn.h>
69
70 #include <net/if_gif.h>
71
72 #include <net/net_osdep.h>
73
74 static int gif_validate6 __P((const struct ip6_hdr *, struct gif_softc *,
75 struct ifnet *));
76
77 int ip6_gif_hlim = GIF_HLIM;
78
79 extern LIST_HEAD(, gif_softc) gif_softc_list;
80
81 extern const struct ip6protosw in6_gif_protosw;
82
83 /*
84 * family - family of the packet to be encapsulate.
85 */
86
87 int
88 in6_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
89 {
90 struct rtentry *rt;
91 struct gif_softc *sc = (struct gif_softc*)ifp;
92 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
93 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
94 struct ip6_hdr *ip6;
95 int proto, error;
96 u_int8_t itos, otos;
97 union {
98 struct sockaddr dst;
99 struct sockaddr_in6 dst6;
100 } u;
101
102 if (sin6_src == NULL || sin6_dst == NULL ||
103 sin6_src->sin6_family != AF_INET6 ||
104 sin6_dst->sin6_family != AF_INET6) {
105 m_freem(m);
106 return EAFNOSUPPORT;
107 }
108
109 switch (family) {
110 #ifdef INET
111 case AF_INET:
112 {
113 struct ip *ip;
114
115 proto = IPPROTO_IPV4;
116 if (m->m_len < sizeof(*ip)) {
117 m = m_pullup(m, sizeof(*ip));
118 if (!m)
119 return ENOBUFS;
120 }
121 ip = mtod(m, struct ip *);
122 itos = ip->ip_tos;
123 break;
124 }
125 #endif
126 #ifdef INET6
127 case AF_INET6:
128 {
129 proto = IPPROTO_IPV6;
130 if (m->m_len < sizeof(*ip6)) {
131 m = m_pullup(m, sizeof(*ip6));
132 if (!m)
133 return ENOBUFS;
134 }
135 ip6 = mtod(m, struct ip6_hdr *);
136 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
137 break;
138 }
139 #endif
140 #ifdef ISO
141 case AF_ISO:
142 proto = IPPROTO_EON;
143 itos = 0;
144 break;
145 #endif
146 default:
147 #ifdef DEBUG
148 printf("in6_gif_output: warning: unknown family %d passed\n",
149 family);
150 #endif
151 m_freem(m);
152 return EAFNOSUPPORT;
153 }
154
155 /* prepend new IP header */
156 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
157 if (m && m->m_len < sizeof(struct ip6_hdr))
158 m = m_pullup(m, sizeof(struct ip6_hdr));
159 if (m == NULL)
160 return ENOBUFS;
161
162 ip6 = mtod(m, struct ip6_hdr *);
163 ip6->ip6_flow = 0;
164 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
165 ip6->ip6_vfc |= IPV6_VERSION;
166 #if 0 /* ip6->ip6_plen will be filled by ip6_output */
167 ip6->ip6_plen = htons((u_int16_t)m->m_pkthdr.len);
168 #endif
169 ip6->ip6_nxt = proto;
170 ip6->ip6_hlim = ip6_gif_hlim;
171 ip6->ip6_src = sin6_src->sin6_addr;
172 /* bidirectional configured tunnel mode */
173 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
174 ip6->ip6_dst = sin6_dst->sin6_addr;
175 else {
176 m_freem(m);
177 return ENETUNREACH;
178 }
179 if (ifp->if_flags & IFF_LINK1)
180 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
181 else
182 ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
183 ip6->ip6_flow &= ~ntohl(0xff00000);
184 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
185
186 sockaddr_in6_init(&u.dst6, &sin6_dst->sin6_addr, 0, 0, 0);
187 if ((rt = rtcache_lookup(&sc->gif_ro, &u.dst)) == NULL) {
188 m_freem(m);
189 return ENETUNREACH;
190 }
191
192 /* If the route constitutes infinite encapsulation, punt. */
193 if (rt->rt_ifp == ifp) {
194 m_freem(m);
195 return ENETUNREACH; /* XXX */
196 }
197
198 #ifdef IPV6_MINMTU
199 /*
200 * force fragmentation to minimum MTU, to avoid path MTU discovery.
201 * it is too painful to ask for resend of inner packet, to achieve
202 * path MTU discovery for encapsulated packets.
203 */
204 error = ip6_output(m, 0, &sc->gif_ro, IPV6_MINMTU,
205 (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
206 #else
207 error = ip6_output(m, 0, &sc->gif_ro, 0,
208 (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
209 #endif
210
211 return (error);
212 }
213
214 int in6_gif_input(struct mbuf **mp, int *offp, int proto)
215 {
216 struct mbuf *m = *mp;
217 struct ifnet *gifp = NULL;
218 struct ip6_hdr *ip6;
219 int af = 0;
220 u_int32_t otos;
221
222 ip6 = mtod(m, struct ip6_hdr *);
223
224 gifp = (struct ifnet *)encap_getarg(m);
225
226 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
227 m_freem(m);
228 IP6_STATINC(IP6_STAT_NOGIF);
229 return IPPROTO_DONE;
230 }
231 #ifndef GIF_ENCAPCHECK
232 if (!gif_validate6(ip6, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
233 m_freem(m);
234 IP6_STATINC(IP6_STAT_NOGIF);
235 return IPPROTO_DONE;
236 }
237 #endif
238
239 otos = ip6->ip6_flow;
240 m_adj(m, *offp);
241
242 switch (proto) {
243 #ifdef INET
244 case IPPROTO_IPV4:
245 {
246 struct ip *ip;
247 u_int8_t otos8;
248 af = AF_INET;
249 otos8 = (ntohl(otos) >> 20) & 0xff;
250 if (m->m_len < sizeof(*ip)) {
251 m = m_pullup(m, sizeof(*ip));
252 if (!m)
253 return IPPROTO_DONE;
254 }
255 ip = mtod(m, struct ip *);
256 if (gifp->if_flags & IFF_LINK1)
257 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
258 else
259 ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
260 break;
261 }
262 #endif /* INET */
263 #ifdef INET6
264 case IPPROTO_IPV6:
265 {
266 struct ip6_hdr *ip6x;
267 af = AF_INET6;
268 if (m->m_len < sizeof(*ip6x)) {
269 m = m_pullup(m, sizeof(*ip6x));
270 if (!m)
271 return IPPROTO_DONE;
272 }
273 ip6x = mtod(m, struct ip6_hdr *);
274 if (gifp->if_flags & IFF_LINK1)
275 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6x->ip6_flow);
276 else
277 ip6_ecn_egress(ECN_NOCARE, &otos, &ip6x->ip6_flow);
278 break;
279 }
280 #endif
281 #ifdef ISO
282 case IPPROTO_EON:
283 af = AF_ISO;
284 break;
285 #endif
286 default:
287 IP6_STATINC(IP6_STAT_NOGIF);
288 m_freem(m);
289 return IPPROTO_DONE;
290 }
291
292 gif_input(m, af, gifp);
293 return IPPROTO_DONE;
294 }
295
296 /*
297 * validate outer address.
298 */
299 static int
300 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
301 struct ifnet *ifp)
302 {
303 const struct sockaddr_in6 *src, *dst;
304
305 src = (struct sockaddr_in6 *)sc->gif_psrc;
306 dst = (struct sockaddr_in6 *)sc->gif_pdst;
307
308 /* check for address match */
309 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
310 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
311 return 0;
312
313 /* martian filters on outer source - done in ip6_input */
314
315 /* ingress filters on outer source */
316 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
317 struct sockaddr_in6 sin6;
318 struct rtentry *rt;
319
320 memset(&sin6, 0, sizeof(sin6));
321 sin6.sin6_family = AF_INET6;
322 sin6.sin6_len = sizeof(struct sockaddr_in6);
323 sin6.sin6_addr = ip6->ip6_src;
324 /* XXX scopeid */
325 rt = rtalloc1((struct sockaddr *)&sin6, 0);
326 if (!rt || rt->rt_ifp != ifp) {
327 #if 0
328 log(LOG_WARNING, "%s: packet from %s dropped "
329 "due to ingress filter\n", if_name(&sc->gif_if),
330 ip6_sprintf(&sin6.sin6_addr));
331 #endif
332 if (rt)
333 rtfree(rt);
334 return 0;
335 }
336 rtfree(rt);
337 }
338
339 return 128 * 2;
340 }
341
342 #ifdef GIF_ENCAPCHECK
343 /*
344 * we know that we are in IFF_UP, outer address available, and outer family
345 * matched the physical addr family. see gif_encapcheck().
346 */
347 int
348 gif_encapcheck6(struct mbuf *m, int off, int proto, void *arg)
349 {
350 struct ip6_hdr ip6;
351 struct gif_softc *sc;
352 struct ifnet *ifp;
353
354 /* sanity check done in caller */
355 sc = (struct gif_softc *)arg;
356
357 m_copydata(m, 0, sizeof(ip6), (void *)&ip6);
358 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
359
360 return gif_validate6(&ip6, sc, ifp);
361 }
362 #endif
363
364 int
365 in6_gif_attach(struct gif_softc *sc)
366 {
367 #ifndef GIF_ENCAPCHECK
368 struct sockaddr_in6 mask6;
369
370 bzero(&mask6, sizeof(mask6));
371 mask6.sin6_len = sizeof(struct sockaddr_in6);
372 mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
373 mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
374
375 if (!sc->gif_psrc || !sc->gif_pdst)
376 return EINVAL;
377 sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc,
378 (struct sockaddr *)&mask6, sc->gif_pdst, (struct sockaddr *)&mask6,
379 (const void *)&in6_gif_protosw, sc);
380 #else
381 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
382 (struct protosw *)&in6_gif_protosw, sc);
383 #endif
384 if (sc->encap_cookie6 == NULL)
385 return EEXIST;
386 return 0;
387 }
388
389 int
390 in6_gif_detach(struct gif_softc *sc)
391 {
392 int error;
393
394 error = encap_detach(sc->encap_cookie6);
395 if (error == 0)
396 sc->encap_cookie6 = NULL;
397
398 rtcache_free(&sc->gif_ro);
399
400 return error;
401 }
402
403 void *
404 in6_gif_ctlinput(int cmd, const struct sockaddr *sa, void *d)
405 {
406 struct gif_softc *sc;
407 struct ip6ctlparam *ip6cp = NULL;
408 struct ip6_hdr *ip6;
409 const struct sockaddr_in6 *dst6;
410
411 if (sa->sa_family != AF_INET6 ||
412 sa->sa_len != sizeof(struct sockaddr_in6))
413 return NULL;
414
415 if ((unsigned)cmd >= PRC_NCMDS)
416 return NULL;
417 if (cmd == PRC_HOSTDEAD)
418 d = NULL;
419 else if (inet6ctlerrmap[cmd] == 0)
420 return NULL;
421
422 /* if the parameter is from icmp6, decode it. */
423 if (d != NULL) {
424 ip6cp = (struct ip6ctlparam *)d;
425 ip6 = ip6cp->ip6c_ip6;
426 } else {
427 ip6 = NULL;
428 }
429
430 if (!ip6)
431 return NULL;
432
433 /*
434 * for now we don't care which type it was, just flush the route cache.
435 * XXX slow. sc (or sc->encap_cookie6) should be passed from
436 * ip_encap.c.
437 */
438 LIST_FOREACH(sc, &gif_softc_list, gif_list) {
439 if ((sc->gif_if.if_flags & IFF_RUNNING) == 0)
440 continue;
441 if (sc->gif_psrc->sa_family != AF_INET6)
442 continue;
443
444 dst6 = satocsin6(rtcache_getdst(&sc->gif_ro));
445 /* XXX scope */
446 if (dst6 == NULL)
447 ;
448 else if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr))
449 rtcache_free(&sc->gif_ro);
450 }
451
452 return NULL;
453 }
454
455 PR_WRAP_CTLINPUT(in6_gif_ctlinput)
456 PR_WRAP_CTLOUTPUT(rip6_ctloutput)
457 PR_WRAP_USRREQ(rip6_usrreq)
458
459 #define in6_gif_ctlinput in6_gif_ctlinput_wrapper
460 #define rip6_ctloutput rip6_ctloutput_wrapper
461 #define rip6_usrreq rip6_usrreq_wrapper
462
463 extern struct domain inet6domain;
464 const struct ip6protosw in6_gif_protosw =
465 { SOCK_RAW, &inet6domain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
466 in6_gif_input, rip6_output, in6_gif_ctlinput, rip6_ctloutput,
467 rip6_usrreq,
468 0, 0, 0, 0,
469 };
Cache object: b0b403be5a7535854da7451fba5935e9
|