1 /* $NetBSD: ip_etherip.c,v 1.1.2.1 2006/12/09 11:51:47 bouyer 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
62 #include "opt_inet.h"
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
68 #include <sys/mbuf.h>
69 #include <sys/device.h>
70 #include <sys/errno.h>
71 #include <sys/ioctl.h>
72 #include <sys/syslog.h>
73 #include <sys/protosw.h>
74 #include <sys/kernel.h>
75
76 #include <net/if.h>
77 #include <net/route.h>
78
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip_etherip.h>
85
86 #include <net/if_ether.h>
87 #include <net/if_media.h>
88 #include <net/if_etherip.h>
89
90 #include <machine/stdarg.h>
91
92 int
93 ip_etherip_output(struct ifnet *ifp, struct mbuf *m)
94 {
95 struct etherip_softc *sc = (struct etherip_softc*)ifp->if_softc;
96 struct sockaddr_in *dst, *sin_src, *sin_dst;
97 struct ip iphdr; /* capsule IP header, host byte ordered */
98 struct etherip_header eiphdr;
99 int proto, error;
100
101 dst = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
102 sin_src = (struct sockaddr_in *)sc->sc_src;
103 sin_dst = (struct sockaddr_in *)sc->sc_dst;
104
105 if (sin_src == NULL ||
106 sin_dst == NULL ||
107 sin_src->sin_family != AF_INET ||
108 sin_dst->sin_family != AF_INET) {
109 m_freem(m);
110 return EAFNOSUPPORT;
111 }
112
113 /* reset broadcast/multicast flags */
114 m->m_flags &= ~(M_BCAST|M_MCAST);
115
116 m->m_flags |= M_PKTHDR;
117 proto = IPPROTO_ETHERIP;
118
119 /* fill and prepend Ethernet-in-IP header */
120 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
121 eiphdr.eip_pad = 0;
122 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
123 if (m == NULL)
124 return ENOBUFS;
125 if (M_UNWRITABLE(m, sizeof(struct etherip_header))) {
126 m = m_pullup(m, sizeof(struct etherip_header));
127 if (m == NULL)
128 return ENOBUFS;
129 }
130 memcpy(mtod(m, struct etherip_header *), &eiphdr,
131 sizeof(struct etherip_header));
132
133 /* fill new IP header */
134 memset(&iphdr, 0, sizeof(struct ip));
135 iphdr.ip_src = sin_src->sin_addr;
136 /* bidirectional configured tunnel mode */
137 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
138 iphdr.ip_dst = sin_dst->sin_addr;
139 else {
140 m_freem(m);
141 return ENETUNREACH;
142 }
143 iphdr.ip_p = proto;
144 /* version will be set in ip_output() */
145 iphdr.ip_ttl = ETHERIP_TTL;
146 iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
147
148 /* prepend new IP header */
149 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
150 if (m == NULL)
151 return ENOBUFS;
152 if (M_UNWRITABLE(m, sizeof(struct ip)))
153 m = m_pullup(m, sizeof(struct ip));
154 memcpy(mtod(m, struct ip *), &iphdr, sizeof(struct ip));
155
156 if (sc->sc_route_expire - time_second <= 0 ||
157 dst->sin_family != sin_dst->sin_family ||
158 !in_hosteq(dst->sin_addr, sin_dst->sin_addr)) {
159 /* cache route doesn't match */
160 memset(dst, 0, sizeof(struct sockaddr_in));
161 dst->sin_family = sin_dst->sin_family;
162 dst->sin_len = sizeof(struct sockaddr_in);
163 dst->sin_addr = sin_dst->sin_addr;
164 if (sc->sc_ro.ro_rt) {
165 RTFREE(sc->sc_ro.ro_rt);
166 sc->sc_ro.ro_rt = NULL;
167 }
168 }
169
170 if (sc->sc_ro.ro_rt == NULL) {
171 rtalloc(&sc->sc_ro);
172 if (sc->sc_ro.ro_rt == NULL) {
173 m_freem(m);
174 return ENETUNREACH ;
175 }
176
177 /* if it constitutes infinite encapsulation, punt. */
178 if (sc->sc_ro.ro_rt->rt_ifp == ifp) {
179 m_freem(m);
180 return ENETUNREACH; /*XXX*/
181 }
182
183 sc->sc_route_expire = time_second + ETHERIP_ROUTE_TTL;
184 }
185
186 error = ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
187
188 return error;
189 }
190
191 void
192 ip_etherip_input(struct mbuf *m, ...)
193 {
194 struct etherip_softc *sc;
195 const struct ip *ip;
196 struct sockaddr_in *src, *dst;
197 struct ifnet *ifp = NULL;
198 int off, proto;
199 va_list ap;
200
201 va_start(ap, m);
202 off = va_arg(ap, int);
203 proto = va_arg(ap, int);
204 va_end(ap);
205
206 if (proto != IPPROTO_ETHERIP) {
207 m_freem(m);
208 ipstat.ips_noproto++;
209 return;
210 }
211
212 ip = mtod(m, const struct ip *);
213
214 /* find device configured for this packets src and dst */
215 LIST_FOREACH(sc, ðerip_softc_list, etherip_list) {
216 if (!sc->sc_src || !sc->sc_dst)
217 continue;
218
219 if (sc->sc_src->sa_family != AF_INET ||
220 sc->sc_dst->sa_family != AF_INET)
221 continue;
222
223 src = (struct sockaddr_in *)sc->sc_src;
224 dst = (struct sockaddr_in *)sc->sc_dst;
225
226 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
227 dst->sin_addr.s_addr != ip->ip_src.s_addr)
228 continue;
229
230 ifp = &sc->sc_ec.ec_if;
231 break;
232 }
233
234 /* no matching device found */
235 if (!ifp) {
236 m_freem(m);
237 ipstat.ips_odropped++;
238 return;
239 }
240
241 m_adj(m, off);
242
243 /*
244 * Section 4 of RFC 3378 requires that the EtherIP header of incoming
245 * packets is verified to contain the correct values in the version and
246 * reserved fields, and packets with wrong values be dropped.
247 *
248 * There is some discussion about what exactly the header should look
249 * like, the RFC is not very clear there. To be compatible with broken
250 * implementations, we don't check the header on incoming packets,
251 * relying on the ethernet code to filter out garbage.
252 *
253 * The header we use for sending is compatible with the original
254 * implementation in OpenBSD, which was used in former NetBSD versions
255 * and is used in FreeBSD. One Linux implementation is known to use the
256 * same value.
257 */
258 m_adj(m, sizeof(struct etherip_header));
259 m = m_pullup(m, sizeof(struct ether_header));
260 if (m == NULL) {
261 ifp->if_ierrors++;
262 return;
263 }
264
265 m->m_pkthdr.rcvif = ifp;
266 m->m_flags &= ~(M_BCAST|M_MCAST);
267
268 #if NBPFILTER > 0
269 if (ifp->if_bpf)
270 bpf_mtap(ifp->if_bpf, m);
271 #endif
272
273 ifp->if_ipackets++;
274 (ifp->if_input)(ifp, m);
275
276 return;
277 }
278
Cache object: da2c27dfecaf771009e401bd872b9f08
|