in_gif.c revision 1.86 1 /* $NetBSD: in_gif.c,v 1.86 2016/12/14 11:19:15 knakahara Exp $ */
2 /* $KAME: in_gif.c,v 1.66 2001/07/29 04:46:09 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: in_gif.c,v 1.86 2016/12/14 11:19:15 knakahara Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_inet.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/mbuf.h>
45 #include <sys/errno.h>
46 #include <sys/ioctl.h>
47 #include <sys/syslog.h>
48 #include <sys/kernel.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 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/in_gif.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_encap.h>
60 #include <netinet/ip_ecn.h>
61
62 #ifdef INET6
63 #include <netinet/ip6.h>
64 #endif
65
66 #include <net/if_gif.h>
67
68 #include <net/net_osdep.h>
69
70 static int gif_validate4(const struct ip *, struct gif_softc *,
71 struct ifnet *);
72
73 int ip_gif_ttl = GIF_TTL;
74
75 static const struct encapsw in_gif_encapsw = {
76 .encapsw4 = {
77 .pr_input = in_gif_input,
78 .pr_ctlinput = NULL,
79 }
80 };
81
82 int
83 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
84 {
85 struct rtentry *rt;
86 struct route *ro;
87 struct gif_softc *sc = ifp->if_softc;
88 struct sockaddr_in *sin_src = satosin(sc->gif_psrc);
89 struct sockaddr_in *sin_dst = satosin(sc->gif_pdst);
90 struct ip iphdr; /* capsule IP header, host byte ordered */
91 int proto, error;
92 u_int8_t tos;
93 union {
94 struct sockaddr dst;
95 struct sockaddr_in dst4;
96 } u;
97
98 if (sin_src == NULL || sin_dst == NULL ||
99 sin_src->sin_family != AF_INET ||
100 sin_dst->sin_family != AF_INET) {
101 m_freem(m);
102 return EAFNOSUPPORT;
103 }
104
105 switch (family) {
106 #ifdef INET
107 case AF_INET:
108 {
109 const struct ip *ip;
110
111 proto = IPPROTO_IPV4;
112 if (m->m_len < sizeof(*ip)) {
113 m = m_pullup(m, sizeof(*ip));
114 if (m == NULL)
115 return ENOBUFS;
116 }
117 ip = mtod(m, const struct ip *);
118 tos = ip->ip_tos;
119 break;
120 }
121 #endif /* INET */
122 #ifdef INET6
123 case AF_INET6:
124 {
125 const struct ip6_hdr *ip6;
126 proto = IPPROTO_IPV6;
127 if (m->m_len < sizeof(*ip6)) {
128 m = m_pullup(m, sizeof(*ip6));
129 if (m == NULL)
130 return ENOBUFS;
131 }
132 ip6 = mtod(m, const struct ip6_hdr *);
133 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
134 break;
135 }
136 #endif /* INET6 */
137 default:
138 #ifdef DEBUG
139 printf("in_gif_output: warning: unknown family %d passed\n",
140 family);
141 #endif
142 m_freem(m);
143 return EAFNOSUPPORT;
144 }
145
146 memset(&iphdr, 0, sizeof(iphdr));
147 iphdr.ip_src = sin_src->sin_addr;
148 /* bidirectional configured tunnel mode */
149 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
150 iphdr.ip_dst = sin_dst->sin_addr;
151 else {
152 m_freem(m);
153 return ENETUNREACH;
154 }
155 iphdr.ip_p = proto;
156 /* version will be set in ip_output() */
157 iphdr.ip_ttl = ip_gif_ttl;
158 iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
159 if (ifp->if_flags & IFF_LINK1)
160 ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
161 else
162 ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
163
164 /* prepend new IP header */
165 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
166 /* XXX Is m_pullup really necessary after M_PREPEND? */
167 if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
168 m = m_pullup(m, sizeof(struct ip));
169 if (m == NULL)
170 return ENOBUFS;
171 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
172
173 sockaddr_in_init(&u.dst4, &sin_dst->sin_addr, 0);
174
175 ro = percpu_getref(sc->gif_ro_percpu);
176 if ((rt = rtcache_lookup(ro, &u.dst)) == NULL) {
177 percpu_putref(sc->gif_ro_percpu);
178 m_freem(m);
179 return ENETUNREACH;
180 }
181
182 /* If the route constitutes infinite encapsulation, punt. */
183 if (rt->rt_ifp == ifp) {
184 rtcache_unref(rt, ro);
185 rtcache_free(ro);
186 percpu_putref(sc->gif_ro_percpu);
187 m_freem(m);
188 return ENETUNREACH; /*XXX*/
189 }
190 rtcache_unref(rt, ro);
191
192 error = ip_output(m, NULL, ro, 0, NULL, NULL);
193 percpu_putref(sc->gif_ro_percpu);
194 return (error);
195 }
196
197 void
198 in_gif_input(struct mbuf *m, int off, int proto)
199 {
200 struct ifnet *gifp = NULL;
201 const struct ip *ip;
202 int af;
203 u_int8_t otos;
204
205 ip = mtod(m, const struct ip *);
206
207 gifp = (struct ifnet *)encap_getarg(m);
208
209 if (gifp == NULL || (gifp->if_flags & (IFF_UP|IFF_RUNNING))
210 != (IFF_UP|IFF_RUNNING)) {
211 m_freem(m);
212 ip_statinc(IP_STAT_NOGIF);
213 return;
214 }
215 #ifndef GIF_ENCAPCHECK
216 struct gif_softc *sc = (struct gif_softc *)gifp->if_softc;
217 /* other CPU do delete_tunnel */
218 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
219 m_freem(m);
220 ip_statinc(IP_STAT_NOGIF);
221 return;
222 }
223
224 struct ifnet *rcvif;
225 struct psref psref;
226 rcvif = m_get_rcvif_psref(m, &psref);
227 if (!gif_validate4(ip, sc, rcvif)) {
228 m_put_rcvif_psref(rcvif, &psref);
229 m_freem(m);
230 ip_statinc(IP_STAT_NOGIF);
231 return;
232 }
233 m_put_rcvif_psref(rcvif, &psref);
234 #endif
235 otos = ip->ip_tos;
236 m_adj(m, off);
237
238 switch (proto) {
239 #ifdef INET
240 case IPPROTO_IPV4:
241 {
242 struct ip *xip;
243 af = AF_INET;
244 if (M_UNWRITABLE(m, sizeof(*xip))) {
245 if ((m = m_pullup(m, sizeof(*xip))) == NULL)
246 return;
247 }
248 xip = mtod(m, struct ip *);
249 if (gifp->if_flags & IFF_LINK1)
250 ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
251 else
252 ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
253 break;
254 }
255 #endif
256 #ifdef INET6
257 case IPPROTO_IPV6:
258 {
259 struct ip6_hdr *ip6;
260 u_int8_t itos;
261 af = AF_INET6;
262 if (M_UNWRITABLE(m, sizeof(*ip6))) {
263 if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
264 return;
265 }
266 ip6 = mtod(m, struct ip6_hdr *);
267 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
268 if (gifp->if_flags & IFF_LINK1)
269 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
270 else
271 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
272 ip6->ip6_flow &= ~htonl(0xff << 20);
273 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
274 break;
275 }
276 #endif /* INET6 */
277 default:
278 ip_statinc(IP_STAT_NOGIF);
279 m_freem(m);
280 return;
281 }
282 gif_input(m, af, gifp);
283 return;
284 }
285
286 /*
287 * validate outer address.
288 */
289 static int
290 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
291 {
292 struct sockaddr_in *src, *dst;
293 struct in_ifaddr *ia4;
294 int s;
295
296 src = satosin(sc->gif_psrc);
297 dst = satosin(sc->gif_pdst);
298
299 /* check for address match */
300 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
301 dst->sin_addr.s_addr != ip->ip_src.s_addr)
302 return 0;
303
304 /* martian filters on outer source - NOT done in ip_input! */
305 if (IN_MULTICAST(ip->ip_src.s_addr))
306 return 0;
307 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
308 case 0: case 127: case 255:
309 return 0;
310 }
311 /* reject packets with broadcast on source */
312 s = pserialize_read_enter();
313 IN_ADDRLIST_READER_FOREACH(ia4) {
314 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
315 continue;
316 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
317 pserialize_read_exit(s);
318 return 0;
319 }
320 }
321 pserialize_read_exit(s);
322
323 /* ingress filters on outer source */
324 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
325 union {
326 struct sockaddr sa;
327 struct sockaddr_in sin;
328 } u;
329 struct rtentry *rt;
330
331 sockaddr_in_init(&u.sin, &ip->ip_src, 0);
332 rt = rtalloc1(&u.sa, 0);
333 if (rt == NULL || rt->rt_ifp != ifp) {
334 #if 0
335 log(LOG_WARNING, "%s: packet from 0x%x dropped "
336 "due to ingress filter\n", if_name(&sc->gif_if),
337 (u_int32_t)ntohl(u.sin.sin_addr.s_addr));
338 #endif
339 if (rt != NULL)
340 rt_unref(rt);
341 return 0;
342 }
343 rt_unref(rt);
344 }
345
346 return 32 * 2;
347 }
348
349 #ifdef GIF_ENCAPCHECK
350 /*
351 * we know that we are in IFF_UP, outer address available, and outer family
352 * matched the physical addr family. see gif_encapcheck().
353 */
354 int
355 gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
356 {
357 struct ip ip;
358 struct gif_softc *sc;
359 struct ifnet *ifp = NULL;
360 int r;
361 struct psref psref;
362
363 /* sanity check done in caller */
364 sc = arg;
365
366 m_copydata(m, 0, sizeof(ip), &ip);
367 if ((m->m_flags & M_PKTHDR) != 0)
368 ifp = m_get_rcvif_psref(m, &psref);
369
370 r = gif_validate4(&ip, sc, ifp);
371
372 m_put_rcvif_psref(ifp, &psref);
373 return r;
374 }
375 #endif
376
377 int
378 in_gif_attach(struct gif_softc *sc)
379 {
380 #ifndef GIF_ENCAPCHECK
381 struct sockaddr_in mask4;
382
383 memset(&mask4, 0, sizeof(mask4));
384 mask4.sin_len = sizeof(struct sockaddr_in);
385 mask4.sin_addr.s_addr = ~0;
386
387 if (!sc->gif_psrc || !sc->gif_pdst)
388 return EINVAL;
389 sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
390 (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
391 &in_gif_encapsw, sc);
392 #else
393 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
394 &in_gif_encapsw, sc);
395 #endif
396 if (sc->encap_cookie4 == NULL)
397 return EEXIST;
398 return 0;
399 }
400
401 int
402 in_gif_detach(struct gif_softc *sc)
403 {
404 int error;
405
406 error = in_gif_pause(sc);
407
408 percpu_foreach(sc->gif_ro_percpu, gif_rtcache_free_pc, NULL);
409
410 return error;
411 }
412
413 int
414 in_gif_pause(struct gif_softc *sc)
415 {
416 int error;
417
418 error = encap_detach(sc->encap_cookie4);
419 if (error == 0)
420 sc->encap_cookie4 = NULL;
421
422 return error;
423 }
424