]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_gif.c
Upgrade to OpenSSH 7.6p1. This will be followed shortly by 7.7p1.
[FreeBSD/FreeBSD.git] / sys / netinet6 / in6_gif.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39
40 #include <sys/param.h>
41 #include <sys/lock.h>
42 #include <sys/rmlock.h>
43 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/syslog.h>
51 #include <sys/sysctl.h>
52 #include <sys/protosw.h>
53 #include <sys/malloc.h>
54
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/route.h>
58 #include <net/vnet.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #ifdef INET
63 #include <netinet/ip.h>
64 #endif
65 #include <netinet/ip_encap.h>
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/in6_var.h>
70 #endif
71 #include <netinet/ip_ecn.h>
72 #ifdef INET6
73 #include <netinet6/ip6_ecn.h>
74 #include <netinet6/in6_fib.h>
75 #endif
76
77 #include <net/if_gif.h>
78
79 #define GIF_HLIM        30
80 static VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
81 #define V_ip6_gif_hlim                  VNET(ip6_gif_hlim)
82
83 SYSCTL_DECL(_net_inet6_ip6);
84 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_VNET | CTLFLAG_RW,
85     &VNET_NAME(ip6_gif_hlim), 0, "");
86
87 static int in6_gif_input(struct mbuf **, int *, int);
88
89 extern  struct domain inet6domain;
90 static struct protosw in6_gif_protosw = {
91         .pr_type =      SOCK_RAW,
92         .pr_domain =    &inet6domain,
93         .pr_protocol =  0,                      /* IPPROTO_IPV[46] */
94         .pr_flags =     PR_ATOMIC|PR_ADDR,
95         .pr_input =     in6_gif_input,
96         .pr_output =    rip6_output,
97         .pr_ctloutput = rip6_ctloutput,
98         .pr_usrreqs =   &rip6_usrreqs
99 };
100
101 int
102 in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
103 {
104         GIF_RLOCK_TRACKER;
105         struct gif_softc *sc = ifp->if_softc;
106         struct ip6_hdr *ip6;
107         int len;
108
109         /* prepend new IP header */
110         len = sizeof(struct ip6_hdr);
111 #ifndef __NO_STRICT_ALIGNMENT
112         if (proto == IPPROTO_ETHERIP)
113                 len += ETHERIP_ALIGN;
114 #endif
115         M_PREPEND(m, len, M_NOWAIT);
116         if (m == NULL)
117                 return (ENOBUFS);
118 #ifndef __NO_STRICT_ALIGNMENT
119         if (proto == IPPROTO_ETHERIP) {
120                 len = mtod(m, vm_offset_t) & 3;
121                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
122                     ("in6_gif_output: unexpected misalignment"));
123                 m->m_data += len;
124                 m->m_len -= ETHERIP_ALIGN;
125         }
126 #endif
127
128         ip6 = mtod(m, struct ip6_hdr *);
129         GIF_RLOCK(sc);
130         if (sc->gif_family != AF_INET6) {
131                 m_freem(m);
132                 GIF_RUNLOCK(sc);
133                 return (ENETDOWN);
134         }
135         bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
136         GIF_RUNLOCK(sc);
137
138         ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
139         ip6->ip6_nxt    = proto;
140         ip6->ip6_hlim   = V_ip6_gif_hlim;
141         /*
142          * force fragmentation to minimum MTU, to avoid path MTU discovery.
143          * it is too painful to ask for resend of inner packet, to achieve
144          * path MTU discovery for encapsulated packets.
145          */
146         return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
147 }
148
149 static int
150 in6_gif_input(struct mbuf **mp, int *offp, int proto)
151 {
152         struct mbuf *m = *mp;
153         struct ifnet *gifp;
154         struct gif_softc *sc;
155         struct ip6_hdr *ip6;
156         uint8_t ecn;
157
158         sc = encap_getarg(m);
159         if (sc == NULL) {
160                 m_freem(m);
161                 IP6STAT_INC(ip6s_nogif);
162                 return (IPPROTO_DONE);
163         }
164         gifp = GIF2IFP(sc);
165         if ((gifp->if_flags & IFF_UP) != 0) {
166                 ip6 = mtod(m, struct ip6_hdr *);
167                 ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
168                 m_adj(m, *offp);
169                 gif_input(m, gifp, proto, ecn);
170         } else {
171                 m_freem(m);
172                 IP6STAT_INC(ip6s_nogif);
173         }
174         return (IPPROTO_DONE);
175 }
176
177 /*
178  * we know that we are in IFF_UP, outer address available, and outer family
179  * matched the physical addr family.  see gif_encapcheck().
180  */
181 int
182 in6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
183 {
184         const struct ip6_hdr *ip6;
185         struct gif_softc *sc;
186         int ret;
187
188         /* sanity check done in caller */
189         sc = (struct gif_softc *)arg;
190         GIF_RLOCK_ASSERT(sc);
191
192         /*
193          * Check for address match.  Note that the check is for an incoming
194          * packet.  We should compare the *source* address in our configuration
195          * and the *destination* address of the packet, and vice versa.
196          */
197         ip6 = mtod(m, const struct ip6_hdr *);
198         if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst))
199                 return (0);
200         ret = 128;
201         if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src)) {
202                 if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
203                         return (0);
204         } else
205                 ret += 128;
206
207         /* ingress filters on outer source */
208         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
209                 struct nhop6_basic nh6;
210
211                 /* XXX empty scope id */
212                 if (fib6_lookup_nh_basic(sc->gif_fibnum, &ip6->ip6_src, 0, 0, 0,
213                     &nh6) != 0)
214                         return (0);
215
216                 if (nh6.nh_ifp != m->m_pkthdr.rcvif)
217                         return (0);
218         }
219         return (ret);
220 }
221
222 int
223 in6_gif_attach(struct gif_softc *sc)
224 {
225
226         KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
227         sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
228             (void *)&in6_gif_protosw, sc);
229         if (sc->gif_ecookie == NULL)
230                 return (EEXIST);
231         return (0);
232 }