]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_gif.c
Rework IP encapsulation handling code.
[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/malloc.h>
53
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #ifdef INET
62 #include <netinet/ip.h>
63 #endif
64 #include <netinet/ip_encap.h>
65 #ifdef INET6
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet6/in6_var.h>
69 #endif
70 #include <netinet/ip_ecn.h>
71 #ifdef INET6
72 #include <netinet6/ip6_ecn.h>
73 #include <netinet6/in6_fib.h>
74 #endif
75
76 #include <net/if_gif.h>
77
78 #define GIF_HLIM        30
79 static VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
80 #define V_ip6_gif_hlim                  VNET(ip6_gif_hlim)
81
82 SYSCTL_DECL(_net_inet6_ip6);
83 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim,
84     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_gif_hlim), 0,
85     "Default hop limit for encapsulated packets");
86
87 int
88 in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
89 {
90         GIF_RLOCK_TRACKER;
91         struct gif_softc *sc = ifp->if_softc;
92         struct ip6_hdr *ip6;
93         int len;
94
95         /* prepend new IP header */
96         len = sizeof(struct ip6_hdr);
97 #ifndef __NO_STRICT_ALIGNMENT
98         if (proto == IPPROTO_ETHERIP)
99                 len += ETHERIP_ALIGN;
100 #endif
101         M_PREPEND(m, len, M_NOWAIT);
102         if (m == NULL)
103                 return (ENOBUFS);
104 #ifndef __NO_STRICT_ALIGNMENT
105         if (proto == IPPROTO_ETHERIP) {
106                 len = mtod(m, vm_offset_t) & 3;
107                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
108                     ("in6_gif_output: unexpected misalignment"));
109                 m->m_data += len;
110                 m->m_len -= ETHERIP_ALIGN;
111         }
112 #endif
113
114         ip6 = mtod(m, struct ip6_hdr *);
115         GIF_RLOCK(sc);
116         if (sc->gif_family != AF_INET6) {
117                 m_freem(m);
118                 GIF_RUNLOCK(sc);
119                 return (ENETDOWN);
120         }
121         bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
122         GIF_RUNLOCK(sc);
123
124         ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
125         ip6->ip6_nxt    = proto;
126         ip6->ip6_hlim   = V_ip6_gif_hlim;
127         /*
128          * force fragmentation to minimum MTU, to avoid path MTU discovery.
129          * it is too painful to ask for resend of inner packet, to achieve
130          * path MTU discovery for encapsulated packets.
131          */
132         return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
133 }
134
135 static int
136 in6_gif_input(struct mbuf *m, int off, int proto, void *arg)
137 {
138         struct gif_softc *sc = arg;
139         struct ifnet *gifp;
140         struct ip6_hdr *ip6;
141         uint8_t ecn;
142
143         if (sc == NULL) {
144                 m_freem(m);
145                 IP6STAT_INC(ip6s_nogif);
146                 return (IPPROTO_DONE);
147         }
148         gifp = GIF2IFP(sc);
149         if ((gifp->if_flags & IFF_UP) != 0) {
150                 ip6 = mtod(m, struct ip6_hdr *);
151                 ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
152                 m_adj(m, off);
153                 gif_input(m, gifp, proto, ecn);
154         } else {
155                 m_freem(m);
156                 IP6STAT_INC(ip6s_nogif);
157         }
158         return (IPPROTO_DONE);
159 }
160
161 /*
162  * we know that we are in IFF_UP, outer address available, and outer family
163  * matched the physical addr family.  see gif_encapcheck().
164  */
165 int
166 in6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
167 {
168         const struct ip6_hdr *ip6;
169         struct gif_softc *sc;
170         int ret;
171
172         /* sanity check done in caller */
173         sc = (struct gif_softc *)arg;
174         GIF_RLOCK_ASSERT(sc);
175
176         /*
177          * Check for address match.  Note that the check is for an incoming
178          * packet.  We should compare the *source* address in our configuration
179          * and the *destination* address of the packet, and vice versa.
180          */
181         ip6 = mtod(m, const struct ip6_hdr *);
182         if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst))
183                 return (0);
184         ret = 128;
185         if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src)) {
186                 if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
187                         return (0);
188         } else
189                 ret += 128;
190
191         /* ingress filters on outer source */
192         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
193                 struct nhop6_basic nh6;
194
195                 /* XXX empty scope id */
196                 if (fib6_lookup_nh_basic(sc->gif_fibnum, &ip6->ip6_src, 0, 0, 0,
197                     &nh6) != 0)
198                         return (0);
199
200                 if (nh6.nh_ifp != m->m_pkthdr.rcvif)
201                         return (0);
202         }
203         return (ret);
204 }
205
206 static const struct encap_config ipv6_encap_cfg = {
207         .proto = -1,
208         .min_length = sizeof(struct ip6_hdr),
209         .exact_match = (sizeof(struct in6_addr) << 4) + 8,
210         .check = gif_encapcheck,
211         .input = in6_gif_input
212 };
213
214 int
215 in6_gif_attach(struct gif_softc *sc)
216 {
217
218         KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
219         sc->gif_ecookie = ip6_encap_attach(&ipv6_encap_cfg, sc, M_WAITOK);
220         return (0);
221 }