xref: /freebsd/sys/netinet/in_gif.c (revision 6d8fdfa9d5e7d4871c5039b0131829f9cbefeee9)
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: in_gif.c,v 1.54 2001/05/14 14:02:16 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/sysctl.h>
50 #include <sys/malloc.h>
51 
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/route.h>
55 #include <net/vnet.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip_encap.h>
63 #include <netinet/ip_ecn.h>
64 #include <netinet/in_fib.h>
65 
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif
69 
70 #include <net/if_gif.h>
71 
72 #define GIF_TTL		30
73 static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
74 #define	V_ip_gif_ttl		VNET(ip_gif_ttl)
75 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
76     &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets");
77 
78 int
79 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
80 {
81 	GIF_RLOCK_TRACKER;
82 	struct gif_softc *sc = ifp->if_softc;
83 	struct ip *ip;
84 	int len;
85 
86 	/* prepend new IP header */
87 	len = sizeof(struct ip);
88 #ifndef __NO_STRICT_ALIGNMENT
89 	if (proto == IPPROTO_ETHERIP)
90 		len += ETHERIP_ALIGN;
91 #endif
92 	M_PREPEND(m, len, M_NOWAIT);
93 	if (m == NULL)
94 		return (ENOBUFS);
95 #ifndef __NO_STRICT_ALIGNMENT
96 	if (proto == IPPROTO_ETHERIP) {
97 		len = mtod(m, vm_offset_t) & 3;
98 		KASSERT(len == 0 || len == ETHERIP_ALIGN,
99 		    ("in_gif_output: unexpected misalignment"));
100 		m->m_data += len;
101 		m->m_len -= ETHERIP_ALIGN;
102 	}
103 #endif
104 	ip = mtod(m, struct ip *);
105 	GIF_RLOCK(sc);
106 	if (sc->gif_family != AF_INET) {
107 		m_freem(m);
108 		GIF_RUNLOCK(sc);
109 		return (ENETDOWN);
110 	}
111 	bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
112 	GIF_RUNLOCK(sc);
113 
114 	ip->ip_p = proto;
115 	/* version will be set in ip_output() */
116 	ip->ip_ttl = V_ip_gif_ttl;
117 	ip->ip_len = htons(m->m_pkthdr.len);
118 	ip->ip_tos = ecn;
119 
120 	return (ip_output(m, NULL, NULL, 0, NULL, NULL));
121 }
122 
123 static int
124 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
125 {
126 	struct gif_softc *sc = arg;
127 	struct ifnet *gifp;
128 	struct ip *ip;
129 	uint8_t ecn;
130 
131 	if (sc == NULL) {
132 		m_freem(m);
133 		KMOD_IPSTAT_INC(ips_nogif);
134 		return (IPPROTO_DONE);
135 	}
136 	gifp = GIF2IFP(sc);
137 	if ((gifp->if_flags & IFF_UP) != 0) {
138 		ip = mtod(m, struct ip *);
139 		ecn = ip->ip_tos;
140 		m_adj(m, off);
141 		gif_input(m, gifp, proto, ecn);
142 	} else {
143 		m_freem(m);
144 		KMOD_IPSTAT_INC(ips_nogif);
145 	}
146 	return (IPPROTO_DONE);
147 }
148 
149 /*
150  * we know that we are in IFF_UP, outer address available, and outer family
151  * matched the physical addr family.  see gif_encapcheck().
152  */
153 int
154 in_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
155 {
156 	const struct ip *ip;
157 	struct gif_softc *sc;
158 	int ret;
159 
160 	/* sanity check done in caller */
161 	sc = (struct gif_softc *)arg;
162 	GIF_RLOCK_ASSERT(sc);
163 
164 	/* check for address match */
165 	ip = mtod(m, const struct ip *);
166 	if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr)
167 		return (0);
168 	ret = 32 + 8; /* src + proto */
169 	if (sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr) {
170 		if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
171 			return (0);
172 	} else
173 		ret += 32;
174 
175 	/* ingress filters on outer source */
176 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
177 		struct nhop4_basic nh4;
178 		struct in_addr dst;
179 
180 		dst = ip->ip_src;
181 
182 		if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0)
183 			return (0);
184 
185 		if (nh4.nh_ifp != m->m_pkthdr.rcvif)
186 			return (0);
187 	}
188 	return (ret);
189 }
190 
191 static const struct encap_config ipv4_encap_cfg = {
192 	.proto = -1,
193 	.min_length = sizeof(struct ip),
194 	.exact_match = (sizeof(in_addr_t) << 4) + 8,
195 	.check = gif_encapcheck,
196 	.input = in_gif_input
197 };
198 
199 int
200 in_gif_attach(struct gif_softc *sc)
201 {
202 
203 	KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
204 	sc->gif_ecookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
205 	return (0);
206 }
207