]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_gif.c
MfP4: Minor portability fix.
[FreeBSD/FreeBSD.git] / sys / netinet6 / in6_gif.c
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/mbuf.h>
43 #include <sys/errno.h>
44 #include <sys/queue.h>
45 #include <sys/syslog.h>
46 #include <sys/protosw.h>
47
48 #include <sys/malloc.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #ifdef INET
56 #include <netinet/ip.h>
57 #endif
58 #include <netinet/ip_encap.h>
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/in6_gif.h>
63 #include <netinet6/in6_var.h>
64 #endif
65 #include <netinet6/ip6protosw.h>
66 #include <netinet/ip_ecn.h>
67 #ifdef INET6
68 #include <netinet6/ip6_ecn.h>
69 #endif
70
71 #include <net/if_gif.h>
72
73 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
74                          struct ifnet *);
75
76 extern  struct domain inet6domain;
77 struct ip6protosw in6_gif_protosw =
78 { SOCK_RAW,     &inet6domain,   0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
79   in6_gif_input, rip6_output,   0,              rip6_ctloutput,
80   0,
81   0,            0,              0,              0,
82   &rip6_usrreqs
83 };
84
85 int
86 in6_gif_output(struct ifnet *ifp,
87     int family,                 /* family of the packet to be encapsulate */
88     struct mbuf *m)
89 {
90         struct gif_softc *sc = ifp->if_softc;
91         struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
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         struct etherip_header eiphdr;
96         int proto, error;
97         u_int8_t itos, otos;
98
99         GIF_LOCK_ASSERT(sc);
100
101         if (sin6_src == NULL || sin6_dst == NULL ||
102             sin6_src->sin6_family != AF_INET6 ||
103             sin6_dst->sin6_family != AF_INET6) {
104                 m_freem(m);
105                 return EAFNOSUPPORT;
106         }
107
108         switch (family) {
109 #ifdef INET
110         case AF_INET:
111             {
112                 struct ip *ip;
113
114                 proto = IPPROTO_IPV4;
115                 if (m->m_len < sizeof(*ip)) {
116                         m = m_pullup(m, sizeof(*ip));
117                         if (!m)
118                                 return ENOBUFS;
119                 }
120                 ip = mtod(m, struct ip *);
121                 itos = ip->ip_tos;
122                 break;
123             }
124 #endif
125 #ifdef INET6
126         case AF_INET6:
127             {
128                 struct ip6_hdr *ip6;
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         case AF_LINK:
141                 proto = IPPROTO_ETHERIP;
142                 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
143                 eiphdr.eip_pad = 0;
144                 /* prepend Ethernet-in-IP header */
145                 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
146                 if (m && m->m_len < sizeof(struct etherip_header))
147                         m = m_pullup(m, sizeof(struct etherip_header));
148                 if (m == NULL)
149                         return ENOBUFS;
150                 bcopy(&eiphdr, mtod(m, struct etherip_header *),
151                     sizeof(struct etherip_header));
152                 break;
153
154         default:
155 #ifdef DEBUG
156                 printf("in6_gif_output: warning: unknown family %d passed\n",
157                         family);
158 #endif
159                 m_freem(m);
160                 return EAFNOSUPPORT;
161         }
162
163         /* prepend new IP header */
164         M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
165         if (m && m->m_len < sizeof(struct ip6_hdr))
166                 m = m_pullup(m, sizeof(struct ip6_hdr));
167         if (m == NULL) {
168                 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
169                 return ENOBUFS;
170         }
171
172         ip6 = mtod(m, struct ip6_hdr *);
173         ip6->ip6_flow   = 0;
174         ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
175         ip6->ip6_vfc    |= IPV6_VERSION;
176         ip6->ip6_plen   = htons((u_short)m->m_pkthdr.len);
177         ip6->ip6_nxt    = proto;
178         ip6->ip6_hlim   = ip6_gif_hlim;
179         ip6->ip6_src    = sin6_src->sin6_addr;
180         /* bidirectional configured tunnel mode */
181         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
182                 ip6->ip6_dst = sin6_dst->sin6_addr;
183         else  {
184                 m_freem(m);
185                 return ENETUNREACH;
186         }
187         ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
188                        &otos, &itos);
189         ip6->ip6_flow &= ~htonl(0xff << 20);
190         ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
191
192         if (dst->sin6_family != sin6_dst->sin6_family ||
193              !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
194                 /* cache route doesn't match */
195                 bzero(dst, sizeof(*dst));
196                 dst->sin6_family = sin6_dst->sin6_family;
197                 dst->sin6_len = sizeof(struct sockaddr_in6);
198                 dst->sin6_addr = sin6_dst->sin6_addr;
199                 if (sc->gif_ro6.ro_rt) {
200                         RTFREE(sc->gif_ro6.ro_rt);
201                         sc->gif_ro6.ro_rt = NULL;
202                 }
203 #if 0
204                 GIF2IFP(sc)->if_mtu = GIF_MTU;
205 #endif
206         }
207
208         if (sc->gif_ro6.ro_rt == NULL) {
209                 rtalloc((struct route *)&sc->gif_ro6);
210                 if (sc->gif_ro6.ro_rt == NULL) {
211                         m_freem(m);
212                         return ENETUNREACH;
213                 }
214
215                 /* if it constitutes infinite encapsulation, punt. */
216                 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
217                         m_freem(m);
218                         return ENETUNREACH;     /*XXX*/
219                 }
220 #if 0
221                 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
222                         - sizeof(struct ip6_hdr);
223 #endif
224         }
225
226 #ifdef IPV6_MINMTU
227         /*
228          * force fragmentation to minimum MTU, to avoid path MTU discovery.
229          * it is too painful to ask for resend of inner packet, to achieve
230          * path MTU discovery for encapsulated packets.
231          */
232         error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
233 #else
234         error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
235 #endif
236
237         if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
238             sc->gif_ro6.ro_rt != NULL) {
239                 RTFREE(sc->gif_ro6.ro_rt);
240                 sc->gif_ro6.ro_rt = NULL;
241         }
242
243         return (error);
244 }
245
246 int
247 in6_gif_input(struct mbuf **mp, int *offp, int proto)
248 {
249         struct mbuf *m = *mp;
250         struct ifnet *gifp = NULL;
251         struct gif_softc *sc;
252         struct ip6_hdr *ip6;
253         int af = 0;
254         u_int32_t otos;
255
256         ip6 = mtod(m, struct ip6_hdr *);
257
258         sc = (struct gif_softc *)encap_getarg(m);
259         if (sc == NULL) {
260                 m_freem(m);
261                 ip6stat.ip6s_nogif++;
262                 return IPPROTO_DONE;
263         }
264
265         gifp = GIF2IFP(sc);
266         if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
267                 m_freem(m);
268                 ip6stat.ip6s_nogif++;
269                 return IPPROTO_DONE;
270         }
271
272         otos = ip6->ip6_flow;
273         m_adj(m, *offp);
274
275         switch (proto) {
276 #ifdef INET
277         case IPPROTO_IPV4:
278             {
279                 struct ip *ip;
280                 u_int8_t otos8;
281                 af = AF_INET;
282                 otos8 = (ntohl(otos) >> 20) & 0xff;
283                 if (m->m_len < sizeof(*ip)) {
284                         m = m_pullup(m, sizeof(*ip));
285                         if (!m)
286                                 return IPPROTO_DONE;
287                 }
288                 ip = mtod(m, struct ip *);
289                 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
290                                   ECN_ALLOWED : ECN_NOCARE,
291                                   &otos8, &ip->ip_tos) == 0) {
292                         m_freem(m);
293                         return IPPROTO_DONE;
294                 }
295                 break;
296             }
297 #endif /* INET */
298 #ifdef INET6
299         case IPPROTO_IPV6:
300             {
301                 struct ip6_hdr *ip6;
302                 af = AF_INET6;
303                 if (m->m_len < sizeof(*ip6)) {
304                         m = m_pullup(m, sizeof(*ip6));
305                         if (!m)
306                                 return IPPROTO_DONE;
307                 }
308                 ip6 = mtod(m, struct ip6_hdr *);
309                 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
310                                    ECN_ALLOWED : ECN_NOCARE,
311                                    &otos, &ip6->ip6_flow) == 0) {
312                         m_freem(m);
313                         return IPPROTO_DONE;
314                 }
315                 break;
316             }
317 #endif
318         case IPPROTO_ETHERIP:
319                 af = AF_LINK;
320                 break;
321
322         default:
323                 ip6stat.ip6s_nogif++;
324                 m_freem(m);
325                 return IPPROTO_DONE;
326         }
327
328         gif_input(m, af, gifp);
329         return IPPROTO_DONE;
330 }
331
332 /*
333  * validate outer address.
334  */
335 static int
336 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
337     struct ifnet *ifp)
338 {
339         struct sockaddr_in6 *src, *dst;
340
341         src = (struct sockaddr_in6 *)sc->gif_psrc;
342         dst = (struct sockaddr_in6 *)sc->gif_pdst;
343
344         /*
345          * Check for address match.  Note that the check is for an incoming
346          * packet.  We should compare the *source* address in our configuration
347          * and the *destination* address of the packet, and vice versa.
348          */
349         if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
350             !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
351                 return 0;
352
353         /* martian filters on outer source - done in ip6_input */
354
355         /* ingress filters on outer source */
356         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
357                 struct sockaddr_in6 sin6;
358                 struct rtentry *rt;
359
360                 bzero(&sin6, sizeof(sin6));
361                 sin6.sin6_family = AF_INET6;
362                 sin6.sin6_len = sizeof(struct sockaddr_in6);
363                 sin6.sin6_addr = ip6->ip6_src;
364                 sin6.sin6_scope_id = 0; /* XXX */
365
366                 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
367                 if (!rt || rt->rt_ifp != ifp) {
368 #if 0
369                         char ip6buf[INET6_ADDRSTRLEN];
370                         log(LOG_WARNING, "%s: packet from %s dropped "
371                             "due to ingress filter\n", if_name(GIF2IFP(sc)),
372                             ip6_sprintf(ip6buf, &sin6.sin6_addr));
373 #endif
374                         if (rt)
375                                 rtfree(rt);
376                         return 0;
377                 }
378                 rtfree(rt);
379         }
380
381         return 128 * 2;
382 }
383
384 /*
385  * we know that we are in IFF_UP, outer address available, and outer family
386  * matched the physical addr family.  see gif_encapcheck().
387  * sanity check for arg should have been done in the caller.
388  */
389 int
390 gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
391 {
392         struct ip6_hdr ip6;
393         struct gif_softc *sc;
394         struct ifnet *ifp;
395
396         /* sanity check done in caller */
397         sc = (struct gif_softc *)arg;
398
399         /* LINTED const cast */
400         m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
401         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
402
403         return gif_validate6(&ip6, sc, ifp);
404 }
405
406 int
407 in6_gif_attach(struct gif_softc *sc)
408 {
409         sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
410             (void *)&in6_gif_protosw, sc);
411         if (sc->encap_cookie6 == NULL)
412                 return EEXIST;
413         return 0;
414 }
415
416 int
417 in6_gif_detach(struct gif_softc *sc)
418 {
419         int error;
420
421         error = encap_detach(sc->encap_cookie6);
422         if (error == 0)
423                 sc->encap_cookie6 = NULL;
424         return error;
425 }