Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netinet / ip_etherip.c
blob8abb5e948dde3a3cbc6ea4ad70c6795739714570
1 /* $NetBSD: ip_etherip.c,v 1.10 2008/10/16 18:56:56 hans Exp $ */
3 /*
4 * Copyright (c) 2006, Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
5 * All rights reserved.
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.
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.
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
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.
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.
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: ip_etherip.c,v 1.10 2008/10/16 18:56:56 hans Exp $");
63 #include "opt_inet.h"
64 #include "bpfilter.h"
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>
78 #include <net/if.h>
79 #include <net/route.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip_etherip.h>
88 #include <net/if_ether.h>
89 #include <net/if_media.h>
90 #include <net/if_etherip.h>
91 #if NBPFILTER > 0
92 #include <net/bpf.h>
93 #endif
95 #include <machine/stdarg.h>
97 int
98 ip_etherip_output(struct ifnet *ifp, struct mbuf *m)
100 struct rtentry *rt;
101 struct etherip_softc *sc = (struct etherip_softc*)ifp->if_softc;
102 struct sockaddr_in *sin_src, *sin_dst;
103 struct ip iphdr; /* capsule IP header, host byte ordered */
104 struct etherip_header eiphdr;
105 int proto, error;
106 union {
107 struct sockaddr dst;
108 struct sockaddr_in dst4;
109 } u;
111 sin_src = (struct sockaddr_in *)sc->sc_src;
112 sin_dst = (struct sockaddr_in *)sc->sc_dst;
114 if (sin_src == NULL ||
115 sin_dst == NULL ||
116 sin_src->sin_family != AF_INET ||
117 sin_dst->sin_family != AF_INET) {
118 m_freem(m);
119 return EAFNOSUPPORT;
122 /* reset broadcast/multicast flags */
123 m->m_flags &= ~(M_BCAST|M_MCAST);
125 m->m_flags |= M_PKTHDR;
126 proto = IPPROTO_ETHERIP;
128 /* fill and prepend Ethernet-in-IP header */
129 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
130 eiphdr.eip_pad = 0;
131 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
132 if (m == NULL)
133 return ENOBUFS;
134 if (M_UNWRITABLE(m, sizeof(struct etherip_header))) {
135 m = m_pullup(m, sizeof(struct etherip_header));
136 if (m == NULL)
137 return ENOBUFS;
139 memcpy(mtod(m, struct etherip_header *), &eiphdr,
140 sizeof(struct etherip_header));
142 /* fill new IP header */
143 memset(&iphdr, 0, sizeof(struct ip));
144 iphdr.ip_src = sin_src->sin_addr;
145 /* bidirectional configured tunnel mode */
146 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
147 iphdr.ip_dst = sin_dst->sin_addr;
148 else {
149 m_freem(m);
150 return ENETUNREACH;
152 iphdr.ip_p = proto;
153 /* version will be set in ip_output() */
154 iphdr.ip_ttl = ETHERIP_TTL;
155 iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
157 /* prepend new IP header */
158 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
159 if (m == NULL)
160 return ENOBUFS;
161 if (M_UNWRITABLE(m, sizeof(struct ip)))
162 m = m_pullup(m, sizeof(struct ip));
163 memcpy(mtod(m, struct ip *), &iphdr, sizeof(struct ip));
165 sockaddr_in_init(&u.dst4, &sin_dst->sin_addr, 0);
166 if ((rt = rtcache_lookup(&sc->sc_ro, &u.dst)) == NULL) {
167 m_freem(m);
168 return ENETUNREACH;
171 /* if it constitutes infinite encapsulation, punt. */
172 if (rt->rt_ifp == ifp) {
173 rtcache_free(&sc->sc_ro);
174 m_freem(m);
175 return ENETUNREACH; /*XXX*/
178 error = ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
180 return error;
183 void
184 ip_etherip_input(struct mbuf *m, ...)
186 struct etherip_softc *sc;
187 const struct ip *ip;
188 struct sockaddr_in *src, *dst;
189 struct ifnet *ifp = NULL;
190 int off, proto, s;
191 va_list ap;
193 va_start(ap, m);
194 off = va_arg(ap, int);
195 proto = va_arg(ap, int);
196 va_end(ap);
198 if (proto != IPPROTO_ETHERIP) {
199 m_freem(m);
200 ip_statinc(IP_STAT_NOPROTO);
201 return;
204 ip = mtod(m, const struct ip *);
206 /* find device configured for this packets src and dst */
207 LIST_FOREACH(sc, &etherip_softc_list, etherip_list) {
208 if (!sc->sc_src || !sc->sc_dst)
209 continue;
211 if (sc->sc_src->sa_family != AF_INET ||
212 sc->sc_dst->sa_family != AF_INET)
213 continue;
215 src = (struct sockaddr_in *)sc->sc_src;
216 dst = (struct sockaddr_in *)sc->sc_dst;
218 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
219 dst->sin_addr.s_addr != ip->ip_src.s_addr)
220 continue;
222 ifp = &sc->sc_ec.ec_if;
223 break;
226 /* no matching device found */
227 if (!ifp) {
228 m_freem(m);
229 ip_statinc(IP_STAT_ODROPPED);
230 return;
233 m_adj(m, off);
236 * Section 4 of RFC 3378 requires that the EtherIP header of incoming
237 * packets is verified to contain the correct values in the version and
238 * reserved fields, and packets with wrong values be dropped.
240 * There is some discussion about what exactly the header should look
241 * like, the RFC is not very clear there. To be compatible with broken
242 * implementations, we don't check the header on incoming packets,
243 * relying on the ethernet code to filter out garbage.
245 * The header we use for sending is compatible with the original
246 * implementation in OpenBSD, which was used in former NetBSD versions
247 * and is used in FreeBSD. One Linux implementation is known to use the
248 * same value.
250 m_adj(m, sizeof(struct etherip_header));
251 m = m_pullup(m, sizeof(struct ether_header));
252 if (m == NULL) {
253 ifp->if_ierrors++;
254 return;
257 m->m_pkthdr.rcvif = ifp;
258 m->m_flags &= ~(M_BCAST|M_MCAST);
260 #if NBPFILTER > 0
261 if (ifp->if_bpf)
262 bpf_mtap(ifp->if_bpf, m);
263 #endif
265 ifp->if_ipackets++;
267 s = splnet();
268 (ifp->if_input)(ifp, m);
269 splx(s);
271 return;