1 /*      $NetBSD: ip6_etherip.c,v 1.18 2016/06/10 13:27:16 ozaki-r Exp $        */
2 
3 /*
4  *  Copyright (c) 2006, Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
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 Hans Rosenfeld nor the names of his
16  *     contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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  *
32  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. Neither the name of the project nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: ip6_etherip.c,v 1.18 2016/06/10 13:27:16 ozaki-r Exp $");
62 
63 #ifdef _KERNEL_OPT
64 #include "opt_inet.h"
65 #endif
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/mbuf.h>
72 #include <sys/device.h>
73 #include <sys/errno.h>
74 #include <sys/ioctl.h>
75 #include <sys/syslog.h>
76 #include <sys/protosw.h>
77 #include <sys/kernel.h>
78 
79 #include <net/if.h>
80 #include <net/route.h>
81 
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #ifdef INET
85 #include <netinet/ip.h>
86 #endif
87 #ifdef INET6
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/ip6_private.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet6/ip6_etherip.h>
93 #endif
94 
95 #include <net/if_ether.h>
96 #include <net/if_media.h>
97 #include <net/if_etherip.h>
98 #include <net/bpf.h>
99 
100 int
ip6_etherip_output(struct ifnet * ifp,struct mbuf * m)101 ip6_etherip_output(struct ifnet *ifp, struct mbuf *m)
102 {
103 	struct rtentry *rt;
104 	struct etherip_softc *sc = (struct etherip_softc *)ifp->if_softc;
105 	struct sockaddr_in6 *sin6_src, *sin6_dst;
106 	struct ip6_hdr *ip6;    /* capsule IP header, host byte ordered */
107 	struct etherip_header eiphdr;
108 	int proto, error;
109 	union {
110 		struct sockaddr		dst;
111 		struct sockaddr_in6	dst6;
112 	} u;
113 
114 	sin6_src = (struct sockaddr_in6 *)sc->sc_src;
115 	sin6_dst = (struct sockaddr_in6 *)sc->sc_dst;
116 
117 	if (sin6_src == NULL ||
118 	    sin6_dst == NULL ||
119 	    sin6_src->sin6_family != AF_INET6 ||
120 	    sin6_dst->sin6_family != AF_INET6) {
121 		m_freem(m);
122 		return EAFNOSUPPORT;
123 	}
124 
125 	/* reset broadcast/multicast flags */
126 	m->m_flags &= ~(M_BCAST|M_MCAST);
127 
128 	m->m_flags |= M_PKTHDR;
129 	proto = IPPROTO_ETHERIP;
130 
131 	/* fill and prepend Ethernet-in-IP header */
132 	eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
133 	eiphdr.eip_pad = 0;
134 	M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
135 	if (m == NULL)
136 		return ENOBUFS;
137 	if (M_UNWRITABLE(m, sizeof(struct etherip_header))) {
138 		m = m_pullup(m, sizeof(struct etherip_header));
139 		if (m == NULL)
140 			return ENOBUFS;
141 	}
142 	memcpy(mtod(m, struct etherip_header *), &eiphdr,
143 	       sizeof(struct etherip_header));
144 
145 	/* prepend new IP header */
146 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
147 	if (m && m->m_len < sizeof(struct ip6_hdr))
148 		m = m_pullup(m, sizeof(struct ip6_hdr));
149 	if (m == NULL)
150 		return ENOBUFS;
151 
152 	ip6 = mtod(m, struct ip6_hdr *);
153 	ip6->ip6_flow = 0;
154 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
155 	ip6->ip6_vfc |= IPV6_VERSION;
156 	ip6->ip6_nxt  = proto;
157 	ip6->ip6_hlim = ETHERIP_TTL;
158 	ip6->ip6_src  = sin6_src->sin6_addr;
159 
160 	/* bidirectional configured tunnel mode */
161 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
162 		ip6->ip6_dst = sin6_dst->sin6_addr;
163 	else  {
164 		m_freem(m);
165 		return ENETUNREACH;
166 	}
167 
168 	sockaddr_in6_init(&u.dst6, &sin6_dst->sin6_addr, 0, 0, 0);
169 	if ((rt = rtcache_lookup(&sc->sc_ro, &u.dst)) == NULL) {
170 		m_freem(m);
171 		return ENETUNREACH;
172 	}
173 	/* if it constitutes infinite encapsulation, punt. */
174 	if (rt->rt_ifp == ifp) {
175 		rtcache_free(&sc->sc_ro);
176 		m_freem(m);
177 		return ENETUNREACH;     /* XXX */
178 	}
179 
180 	/*
181 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
182 	 * it is too painful to ask for resend of inner packet, to achieve
183 	 * path MTU discovery for encapsulated packets.
184 	 */
185 	error = ip6_output(m, 0, &sc->sc_ro, IPV6_MINMTU, NULL, NULL, NULL);
186 
187 	return error;
188 }
189 
190 int
ip6_etherip_input(struct mbuf ** mp,int * offp,int proto)191 ip6_etherip_input(struct mbuf **mp, int *offp, int proto)
192 {
193 	struct mbuf *m = *mp;
194 	int off = *offp;
195 	struct etherip_softc *sc;
196 	const struct ip6_hdr *ip6;
197 	struct sockaddr_in6 *src6, *dst6;
198 	struct ifnet *ifp = NULL;
199 	int s;
200 
201 	if (proto != IPPROTO_ETHERIP) {
202 		m_freem(m);
203 		IP6_STATINC(IP6_STAT_NOGIF);
204 		return IPPROTO_DONE;
205 	}
206 
207 	ip6 = mtod(m, const struct ip6_hdr *);
208 
209 	/* find device configured for this packets src and dst */
210 	LIST_FOREACH(sc, &etherip_softc_list, etherip_list) {
211 		if( !sc->sc_src || !sc->sc_dst)
212 			continue;
213 		if (sc->sc_src->sa_family != AF_INET6 ||
214 		    sc->sc_dst->sa_family != AF_INET6)
215 			continue;
216 
217 		src6 = (struct sockaddr_in6 *)sc->sc_src;
218 		dst6 = (struct sockaddr_in6 *)sc->sc_dst;
219 
220 		if (!IN6_ARE_ADDR_EQUAL(&src6->sin6_addr, &ip6->ip6_dst) ||
221 		    !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_src))
222 			continue;
223 
224 		ifp = &sc->sc_ec.ec_if;
225 		break;
226 	}
227 
228 	/* no matching device found */
229 	if (!ifp) {
230 		m_freem(m);
231 		IP6_STATINC(IP6_STAT_ODROPPED);
232 		return IPPROTO_DONE;
233 	}
234 
235 	m_adj(m, off);
236 
237 	/*
238 	 * Section 4 of RFC 3378 requires that the EtherIP header of incoming
239 	 * packets is verified to contain the correct values in the version and
240 	 * reserved fields, and packets with wrong values be dropped.
241 	 *
242 	 * There is some discussion about what exactly the header should look
243 	 * like, the RFC is not very clear there. To be compatible with broken
244 	 * implementations, we don't check the header on incoming packets,
245 	 * relying on the ethernet code to filter out garbage.
246 	 *
247 	 * The header we use for sending is compatible with the original
248 	 * implementation in OpenBSD, which was used in former NetBSD versions
249 	 * and is used in FreeBSD. One Linux implementation is known to use the
250 	 * same value.
251 	 */
252 	m_adj(m, sizeof(struct etherip_header));
253 	m = m_pullup(m, sizeof(struct ether_header));
254 	if (m == NULL) {
255 		ifp->if_ierrors++;
256 		return IPPROTO_DONE;
257 	}
258 
259 	m_set_rcvif(m, ifp);
260 	m->m_flags &= ~(M_BCAST|M_MCAST);
261 
262 	bpf_mtap(ifp, m);
263 
264 	ifp->if_ipackets++;
265 
266 	s = splnet();
267 	if_input(ifp, m);
268 	splx(s);
269 
270 	return IPPROTO_DONE;
271 }
272