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