]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_gif.c
Import the NetBSD test suite from ^/vendor/NetBSD/tests/09.30.2014_20.45 ,
[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/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/syslog.h>
47 #include <sys/sysctl.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/route.h>
54 #include <net/vnet.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #ifdef INET
59 #include <netinet/ip.h>
60 #endif
61 #include <netinet/ip_encap.h>
62 #ifdef INET6
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/in6_gif.h>
66 #include <netinet6/in6_var.h>
67 #endif
68 #include <netinet/ip_ecn.h>
69 #ifdef INET6
70 #include <netinet6/ip6_ecn.h>
71 #endif
72
73 #include <net/if_gif.h>
74
75 VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
76 #define V_ip6_gif_hlim                  VNET(ip6_gif_hlim)
77
78 SYSCTL_DECL(_net_inet6_ip6);
79 SYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_RW,
80     &VNET_NAME(ip6_gif_hlim), 0, "");
81
82 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
83                          struct ifnet *);
84
85 extern  struct domain inet6domain;
86 struct protosw in6_gif_protosw = {
87         .pr_type =      SOCK_RAW,
88         .pr_domain =    &inet6domain,
89         .pr_protocol =  0,                      /* IPPROTO_IPV[46] */
90         .pr_flags =     PR_ATOMIC|PR_ADDR,
91         .pr_input =     in6_gif_input,
92         .pr_output =    rip6_output,
93         .pr_ctloutput = rip6_ctloutput,
94         .pr_usrreqs =   &rip6_usrreqs
95 };
96
97 int
98 in6_gif_output(struct ifnet *ifp,
99     int family,                 /* family of the packet to be encapsulate */
100     struct mbuf *m)
101 {
102         struct gif_softc *sc = ifp->if_softc;
103         struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
104         struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
105         struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
106         struct ip6_hdr *ip6;
107         struct etherip_header eiphdr;
108         int error, len, proto;
109         u_int8_t itos, otos;
110
111         GIF_LOCK_ASSERT(sc);
112
113         if (sin6_src == NULL || sin6_dst == NULL ||
114             sin6_src->sin6_family != AF_INET6 ||
115             sin6_dst->sin6_family != AF_INET6) {
116                 m_freem(m);
117                 return EAFNOSUPPORT;
118         }
119
120         switch (family) {
121 #ifdef INET
122         case AF_INET:
123             {
124                 struct ip *ip;
125
126                 proto = IPPROTO_IPV4;
127                 if (m->m_len < sizeof(*ip)) {
128                         m = m_pullup(m, sizeof(*ip));
129                         if (!m)
130                                 return ENOBUFS;
131                 }
132                 ip = mtod(m, struct ip *);
133                 itos = ip->ip_tos;
134                 break;
135             }
136 #endif
137 #ifdef INET6
138         case AF_INET6:
139             {
140                 struct ip6_hdr *ip6;
141                 proto = IPPROTO_IPV6;
142                 if (m->m_len < sizeof(*ip6)) {
143                         m = m_pullup(m, sizeof(*ip6));
144                         if (!m)
145                                 return ENOBUFS;
146                 }
147                 ip6 = mtod(m, struct ip6_hdr *);
148                 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
149                 break;
150             }
151 #endif
152         case AF_LINK:
153                 proto = IPPROTO_ETHERIP;
154
155                 /*
156                  * GIF_SEND_REVETHIP (disabled by default) intentionally
157                  * sends an EtherIP packet with revered version field in
158                  * the header.  This is a knob for backward compatibility
159                  * with FreeBSD 7.2R or prior.
160                  */
161                 if ((sc->gif_options & GIF_SEND_REVETHIP)) {
162                         eiphdr.eip_ver = 0;
163                         eiphdr.eip_resvl = ETHERIP_VERSION;
164                         eiphdr.eip_resvh = 0;
165                 } else {
166                         eiphdr.eip_ver = ETHERIP_VERSION;
167                         eiphdr.eip_resvl = 0;
168                         eiphdr.eip_resvh = 0;
169                 }
170                 /* prepend Ethernet-in-IP header */
171                 M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
172                 if (m && m->m_len < sizeof(struct etherip_header))
173                         m = m_pullup(m, sizeof(struct etherip_header));
174                 if (m == NULL)
175                         return ENOBUFS;
176                 bcopy(&eiphdr, mtod(m, struct etherip_header *),
177                     sizeof(struct etherip_header));
178                 itos = 0;
179                 break;
180
181         default:
182 #ifdef DEBUG
183                 printf("in6_gif_output: warning: unknown family %d passed\n",
184                         family);
185 #endif
186                 m_freem(m);
187                 return EAFNOSUPPORT;
188         }
189
190         /* prepend new IP header */
191         len = sizeof(struct ip6_hdr);
192 #ifndef __NO_STRICT_ALIGNMENT
193         if (family == AF_LINK)
194                 len += ETHERIP_ALIGN;
195 #endif
196         M_PREPEND(m, len, M_NOWAIT);
197         if (m != NULL && m->m_len < len)
198                 m = m_pullup(m, len);
199         if (m == NULL) {
200                 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
201                 return ENOBUFS;
202         }
203 #ifndef __NO_STRICT_ALIGNMENT
204         if (family == AF_LINK) {
205                 len = mtod(m, vm_offset_t) & 3;
206                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
207                     ("in6_gif_output: unexpected misalignment"));
208                 m->m_data += len;
209                 m->m_len -= ETHERIP_ALIGN;
210         }
211 #endif
212
213         ip6 = mtod(m, struct ip6_hdr *);
214         ip6->ip6_flow   = 0;
215         ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
216         ip6->ip6_vfc    |= IPV6_VERSION;
217         ip6->ip6_plen   = htons((u_short)m->m_pkthdr.len);
218         ip6->ip6_nxt    = proto;
219         ip6->ip6_hlim   = V_ip6_gif_hlim;
220         ip6->ip6_src    = sin6_src->sin6_addr;
221         /* bidirectional configured tunnel mode */
222         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
223                 ip6->ip6_dst = sin6_dst->sin6_addr;
224         else  {
225                 m_freem(m);
226                 return ENETUNREACH;
227         }
228         ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
229                        &otos, &itos);
230         ip6->ip6_flow &= ~htonl(0xff << 20);
231         ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
232
233         M_SETFIB(m, sc->gif_fibnum);
234
235         if (dst->sin6_family != sin6_dst->sin6_family ||
236              !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
237                 /* cache route doesn't match */
238                 bzero(dst, sizeof(*dst));
239                 dst->sin6_family = sin6_dst->sin6_family;
240                 dst->sin6_len = sizeof(struct sockaddr_in6);
241                 dst->sin6_addr = sin6_dst->sin6_addr;
242                 if (sc->gif_ro6.ro_rt) {
243                         RTFREE(sc->gif_ro6.ro_rt);
244                         sc->gif_ro6.ro_rt = NULL;
245                 }
246 #if 0
247                 GIF2IFP(sc)->if_mtu = GIF_MTU;
248 #endif
249         }
250
251         if (sc->gif_ro6.ro_rt == NULL) {
252                 in6_rtalloc(&sc->gif_ro6, sc->gif_fibnum);
253                 if (sc->gif_ro6.ro_rt == NULL) {
254                         m_freem(m);
255                         return ENETUNREACH;
256                 }
257
258                 /* if it constitutes infinite encapsulation, punt. */
259                 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
260                         m_freem(m);
261                         return ENETUNREACH;     /*XXX*/
262                 }
263 #if 0
264                 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
265                         - sizeof(struct ip6_hdr);
266 #endif
267         }
268
269         m->m_flags &= ~(M_BCAST|M_MCAST);
270 #ifdef IPV6_MINMTU
271         /*
272          * force fragmentation to minimum MTU, to avoid path MTU discovery.
273          * it is too painful to ask for resend of inner packet, to achieve
274          * path MTU discovery for encapsulated packets.
275          */
276         error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
277 #else
278         error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
279 #endif
280
281         if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
282             sc->gif_ro6.ro_rt != NULL) {
283                 RTFREE(sc->gif_ro6.ro_rt);
284                 sc->gif_ro6.ro_rt = NULL;
285         }
286
287         return (error);
288 }
289
290 int
291 in6_gif_input(struct mbuf **mp, int *offp, int proto)
292 {
293         struct mbuf *m = *mp;
294         struct ifnet *gifp = NULL;
295         struct gif_softc *sc;
296         struct ip6_hdr *ip6;
297         int af = 0;
298         u_int32_t otos;
299
300         ip6 = mtod(m, struct ip6_hdr *);
301
302         sc = (struct gif_softc *)encap_getarg(m);
303         if (sc == NULL) {
304                 m_freem(m);
305                 IP6STAT_INC(ip6s_nogif);
306                 return IPPROTO_DONE;
307         }
308
309         gifp = GIF2IFP(sc);
310         if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
311                 m_freem(m);
312                 IP6STAT_INC(ip6s_nogif);
313                 return IPPROTO_DONE;
314         }
315
316         otos = ip6->ip6_flow;
317         m_adj(m, *offp);
318
319         switch (proto) {
320 #ifdef INET
321         case IPPROTO_IPV4:
322             {
323                 struct ip *ip;
324                 u_int8_t otos8;
325                 af = AF_INET;
326                 otos8 = (ntohl(otos) >> 20) & 0xff;
327                 if (m->m_len < sizeof(*ip)) {
328                         m = m_pullup(m, sizeof(*ip));
329                         if (!m)
330                                 return IPPROTO_DONE;
331                 }
332                 ip = mtod(m, struct ip *);
333                 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
334                                   ECN_ALLOWED : ECN_NOCARE,
335                                   &otos8, &ip->ip_tos) == 0) {
336                         m_freem(m);
337                         return IPPROTO_DONE;
338                 }
339                 break;
340             }
341 #endif /* INET */
342 #ifdef INET6
343         case IPPROTO_IPV6:
344             {
345                 struct ip6_hdr *ip6;
346                 af = AF_INET6;
347                 if (m->m_len < sizeof(*ip6)) {
348                         m = m_pullup(m, sizeof(*ip6));
349                         if (!m)
350                                 return IPPROTO_DONE;
351                 }
352                 ip6 = mtod(m, struct ip6_hdr *);
353                 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
354                                    ECN_ALLOWED : ECN_NOCARE,
355                                    &otos, &ip6->ip6_flow) == 0) {
356                         m_freem(m);
357                         return IPPROTO_DONE;
358                 }
359                 break;
360             }
361 #endif
362         case IPPROTO_ETHERIP:
363                 af = AF_LINK;
364                 break;
365
366         default:
367                 IP6STAT_INC(ip6s_nogif);
368                 m_freem(m);
369                 return IPPROTO_DONE;
370         }
371
372         gif_input(m, af, gifp);
373         return IPPROTO_DONE;
374 }
375
376 /*
377  * validate outer address.
378  */
379 static int
380 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
381     struct ifnet *ifp)
382 {
383         struct sockaddr_in6 *src, *dst;
384
385         src = (struct sockaddr_in6 *)sc->gif_psrc;
386         dst = (struct sockaddr_in6 *)sc->gif_pdst;
387
388         /*
389          * Check for address match.  Note that the check is for an incoming
390          * packet.  We should compare the *source* address in our configuration
391          * and the *destination* address of the packet, and vice versa.
392          */
393         if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
394             !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
395                 return 0;
396
397         /* martian filters on outer source - done in ip6_input */
398
399         /* ingress filters on outer source */
400         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
401                 struct sockaddr_in6 sin6;
402                 struct rtentry *rt;
403
404                 bzero(&sin6, sizeof(sin6));
405                 sin6.sin6_family = AF_INET6;
406                 sin6.sin6_len = sizeof(struct sockaddr_in6);
407                 sin6.sin6_addr = ip6->ip6_src;
408                 sin6.sin6_scope_id = 0; /* XXX */
409
410                 rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
411                     sc->gif_fibnum);
412                 if (!rt || rt->rt_ifp != ifp) {
413 #if 0
414                         char ip6buf[INET6_ADDRSTRLEN];
415                         log(LOG_WARNING, "%s: packet from %s dropped "
416                             "due to ingress filter\n", if_name(GIF2IFP(sc)),
417                             ip6_sprintf(ip6buf, &sin6.sin6_addr));
418 #endif
419                         if (rt)
420                                 RTFREE_LOCKED(rt);
421                         return 0;
422                 }
423                 RTFREE_LOCKED(rt);
424         }
425
426         return 128 * 2;
427 }
428
429 /*
430  * we know that we are in IFF_UP, outer address available, and outer family
431  * matched the physical addr family.  see gif_encapcheck().
432  * sanity check for arg should have been done in the caller.
433  */
434 int
435 gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
436 {
437         struct ip6_hdr ip6;
438         struct gif_softc *sc;
439         struct ifnet *ifp;
440
441         /* sanity check done in caller */
442         sc = (struct gif_softc *)arg;
443
444         /* LINTED const cast */
445         m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
446         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
447
448         return gif_validate6(&ip6, sc, ifp);
449 }
450
451 int
452 in6_gif_attach(struct gif_softc *sc)
453 {
454         sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
455             (void *)&in6_gif_protosw, sc);
456         if (sc->encap_cookie6 == NULL)
457                 return EEXIST;
458         return 0;
459 }
460
461 int
462 in6_gif_detach(struct gif_softc *sc)
463 {
464         int error;
465
466         error = encap_detach(sc->encap_cookie6);
467         if (error == 0)
468                 sc->encap_cookie6 = NULL;
469         return error;
470 }