1 /* $NetBSD: ip6_etherip.c,v 1.11.4.1 2010/11/21 20:45:39 riz 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.11.4.1 2010/11/21 20:45:39 riz Exp $");
62
63 #include "opt_inet.h"
64 #include "bpfilter.h"
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/socket.h>
69 #include <sys/sockio.h>
70 #include <sys/mbuf.h>
71 #include <sys/device.h>
72 #include <sys/errno.h>
73 #include <sys/ioctl.h>
74 #include <sys/syslog.h>
75 #include <sys/protosw.h>
76 #include <sys/kernel.h>
77
78 #include <net/if.h>
79 #include <net/route.h>
80
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #ifdef INET
84 #include <netinet/ip.h>
85 #endif
86 #ifdef INET6
87 #include <netinet/ip6.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet6/ip6_private.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet6/ip6_etherip.h>
92 #endif
93
94 #include <net/if_ether.h>
95 #include <net/if_media.h>
96 #include <net/if_etherip.h>
97 #if NBPFILTER > 0
98 #include <net/bpf.h>
99 #endif
100
101 #include <machine/stdarg.h>
102
103 int
104 ip6_etherip_output(struct ifnet *ifp, struct mbuf *m)
105 {
106 struct rtentry *rt;
107 struct etherip_softc *sc = (struct etherip_softc *)ifp->if_softc;
108 struct sockaddr_in6 *sin6_src, *sin6_dst;
109 struct ip6_hdr *ip6; /* capsule IP header, host byte ordered */
110 struct etherip_header eiphdr;
111 int proto, error;
112 union {
113 struct sockaddr dst;
114 struct sockaddr_in6 dst6;
115 } u;
116
117 sin6_src = (struct sockaddr_in6 *)sc->sc_src;
118 sin6_dst = (struct sockaddr_in6 *)sc->sc_dst;
119
120 if (sin6_src == NULL ||
121 sin6_dst == NULL ||
122 sin6_src->sin6_family != AF_INET6 ||
123 sin6_dst->sin6_family != AF_INET6) {
124 m_freem(m);
125 return EAFNOSUPPORT;
126 }
127
128 /* reset broadcast/multicast flags */
129 m->m_flags &= ~(M_BCAST|M_MCAST);
130
131 m->m_flags |= M_PKTHDR;
132 proto = IPPROTO_ETHERIP;
133
134 /* fill and prepend Ethernet-in-IP header */
135 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
136 eiphdr.eip_pad = 0;
137 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
138 if (m == NULL)
139 return ENOBUFS;
140 if (M_UNWRITABLE(m, sizeof(struct etherip_header))) {
141 m = m_pullup(m, sizeof(struct etherip_header));
142 if (m == NULL)
143 return ENOBUFS;
144 }
145 memcpy(mtod(m, struct etherip_header *), &eiphdr,
146 sizeof(struct etherip_header));
147
148 /* prepend new IP header */
149 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
150 if (m && m->m_len < sizeof(struct ip6_hdr))
151 m = m_pullup(m, sizeof(struct ip6_hdr));
152 if (m == NULL)
153 return ENOBUFS;
154
155 ip6 = mtod(m, struct ip6_hdr *);
156 ip6->ip6_flow = 0;
157 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
158 ip6->ip6_vfc |= IPV6_VERSION;
159 ip6->ip6_nxt = proto;
160 ip6->ip6_hlim = ETHERIP_TTL;
161 ip6->ip6_src = sin6_src->sin6_addr;
162
163 /* bidirectional configured tunnel mode */
164 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
165 ip6->ip6_dst = sin6_dst->sin6_addr;
166 else {
167 m_freem(m);
168 return ENETUNREACH;
169 }
170
171 sockaddr_in6_init(&u.dst6, &sin6_dst->sin6_addr, 0, 0, 0);
172 if ((rt = rtcache_lookup(&sc->sc_ro, &u.dst)) == NULL) {
173 m_freem(m);
174 return ENETUNREACH;
175 }
176 /* if it constitutes infinite encapsulation, punt. */
177 if (rt->rt_ifp == ifp) {
178 rtcache_free(&sc->sc_ro);
179 m_freem(m);
180 return ENETUNREACH; /* XXX */
181 }
182
183 /*
184 * force fragmentation to minimum MTU, to avoid path MTU discovery.
185 * it is too painful to ask for resend of inner packet, to achieve
186 * path MTU discovery for encapsulated packets.
187 */
188 error = ip6_output(m, 0, &sc->sc_ro, IPV6_MINMTU, NULL, NULL, NULL);
189
190 return error;
191 }
192
193 int
194 ip6_etherip_input(struct mbuf **mp, int *offp, int proto)
195 {
196 struct mbuf *m = *mp;
197 int off = *offp;
198 struct etherip_softc *sc;
199 const struct ip6_hdr *ip6;
200 struct sockaddr_in6 *src6, *dst6;
201 struct ifnet *ifp = NULL;
202 int s;
203
204 if (proto != IPPROTO_ETHERIP) {
205 m_freem(m);
206 IP6_STATINC(IP6_STAT_NOGIF);
207 return IPPROTO_DONE;
208 }
209
210 ip6 = mtod(m, const struct ip6_hdr *);
211
212 /* find device configured for this packets src and dst */
213 LIST_FOREACH(sc, ðerip_softc_list, etherip_list) {
214 if( !sc->sc_src || !sc->sc_dst)
215 continue;
216 if (sc->sc_src->sa_family != AF_INET6 ||
217 sc->sc_dst->sa_family != AF_INET6)
218 continue;
219
220 src6 = (struct sockaddr_in6 *)sc->sc_src;
221 dst6 = (struct sockaddr_in6 *)sc->sc_dst;
222
223 if (!IN6_ARE_ADDR_EQUAL(&src6->sin6_addr, &ip6->ip6_dst) ||
224 !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_src))
225 continue;
226
227 ifp = &sc->sc_ec.ec_if;
228 break;
229 }
230
231 /* no matching device found */
232 if (!ifp) {
233 m_freem(m);
234 IP6_STATINC(IP6_STAT_ODROPPED);
235 return IPPROTO_DONE;
236 }
237
238 m_adj(m, off);
239
240 /*
241 * Section 4 of RFC 3378 requires that the EtherIP header of incoming
242 * packets is verified to contain the correct values in the version and
243 * reserved fields, and packets with wrong values be dropped.
244 *
245 * There is some discussion about what exactly the header should look
246 * like, the RFC is not very clear there. To be compatible with broken
247 * implementations, we don't check the header on incoming packets,
248 * relying on the ethernet code to filter out garbage.
249 *
250 * The header we use for sending is compatible with the original
251 * implementation in OpenBSD, which was used in former NetBSD versions
252 * and is used in FreeBSD. One Linux implementation is known to use the
253 * same value.
254 */
255 m_adj(m, sizeof(struct etherip_header));
256 m = m_pullup(m, sizeof(struct ether_header));
257 if (m == NULL) {
258 ifp->if_ierrors++;
259 return IPPROTO_DONE;
260 }
261
262 m->m_pkthdr.rcvif = ifp;
263 m->m_flags &= ~(M_BCAST|M_MCAST);
264
265 #if NBPFILTER > 0
266 if (ifp->if_bpf)
267 bpf_mtap(ifp->if_bpf, m);
268 #endif
269
270 ifp->if_ipackets++;
271
272 s = splnet();
273 (ifp->if_input)(ifp, m);
274 splx(s);
275
276 return IPPROTO_DONE;
277 }
Cache object: 1fa458b9f8351afc12f32ca2aa6c3057
|