1 /* $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * deencapsulate tunneled packets and send them on
42 * output half is in net/if_gre.[ch]
43 * This currently handles IPPROTO_GRE, IPPROTO_MOBILE
47 #include "opt_atalk.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/protosw.h>
55 #include <sys/errno.h>
57 #include <sys/kernel.h>
58 #include <sys/syslog.h>
60 #include <net/ethernet.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/raw_cb.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/ip_gre.h>
73 #include <machine/in_cksum.h>
75 #error ip_gre input without IP?
79 #include <netatalk/at.h>
80 #include <netatalk/at_var.h>
81 #include <netatalk/at_extern.h>
84 /* Needs IP headers. */
85 #include <net/if_gre.h>
87 #include <machine/stdarg.h>
90 void gre_inet_ntoa(struct in_addr in); /* XXX */
93 static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
95 static int gre_input2(struct mbuf *, int, u_char);
98 * De-encapsulate a packet and feed it back through ip input (this
99 * routine is called whenever IP gets a packet with proto type
100 * IPPROTO_GRE and a local destination address).
101 * This really is simple
105 gre_input(struct mbuf *m, ...)
107 gre_input(m, va_alist)
116 off = va_arg(ap, int);
118 proto = (mtod(m, struct ip *))->ip_p;
120 ret = gre_input2(m, off, proto);
122 * ret == 0 : packet not processed, meaning that
123 * no matching tunnel that is up is found.
124 * we inject it to raw ip socket to see if anyone picks it up.
132 * Does the real work and is called from gre_input() (above)
133 * returns 0 if packet is not yet processed
134 * and 1 if it needs no further processing
135 * proto is the protocol number of the "calling" foo_input()
139 gre_input2(struct mbuf *m ,int hlen, u_char proto)
143 struct gre_softc *sc;
146 if ((sc = gre_lookup(m, proto)) == NULL) {
147 /* No matching tunnel or tunnel is down. */
151 if (m->m_len < sizeof(*gip)) {
152 m = m_pullup(m, sizeof(*gip));
156 gip = mtod(m, struct greip *);
158 sc->sc_if.if_ipackets++;
159 sc->sc_if.if_ibytes += m->m_pkthdr.len;
163 hlen += sizeof(struct gre_h);
165 /* process GRE flags as packet can be of variable len */
166 flags = ntohs(gip->gi_flags);
168 /* Checksum & Offset are present */
169 if ((flags & GRE_CP) | (flags & GRE_RP))
171 /* We don't support routing fields (variable length) */
179 switch (ntohs(gip->gi_ptype)) { /* ethertypes */
180 case WCCP_PROTOCOL_TYPE:
181 if (sc->wccp_ver == WCCP_V2)
184 case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */
185 isr = NETISR_IP;/* as we are in ip_input */
188 case ETHERTYPE_ATALK:
194 default: /* others not yet supported */
199 /* others not yet supported */
203 if (hlen > m->m_pkthdr.len) {
207 /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */
210 if (sc->sc_if.if_bpf) {
211 u_int32_t af = AF_INET;
212 bpf_mtap2(sc->sc_if.if_bpf, &af, sizeof(af), m);
215 m->m_pkthdr.rcvif = &sc->sc_if;
217 netisr_dispatch(isr, m);
219 return (1); /* packet is done, no further processing needed */
223 * input routine for IPPRPOTO_MOBILE
224 * This is a little bit diffrent from the other modes, as the
225 * encapsulating header was not prepended, but instead inserted
226 * between IP header and payload
231 gre_mobile_input(struct mbuf *m, ...)
233 gre_mobile_input(m, va_alist)
240 struct gre_softc *sc;
246 hlen = va_arg(ap, int);
249 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
250 /* No matching tunnel or tunnel is down. */
255 if (m->m_len < sizeof(*mip)) {
256 m = m_pullup(m, sizeof(*mip));
260 ip = mtod(m, struct ip *);
261 mip = mtod(m, struct mobip_h *);
263 sc->sc_if.if_ipackets++;
264 sc->sc_if.if_ibytes += m->m_pkthdr.len;
266 if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
268 mip->mi.ip_src.s_addr = mip->mh.osrc;
272 if (m->m_len < (ip->ip_hl << 2) + msiz) {
273 m = m_pullup(m, (ip->ip_hl << 2) + msiz);
276 ip = mtod(m, struct ip *);
277 mip = mtod(m, struct mobip_h *);
280 mip->mi.ip_dst.s_addr = mip->mh.odst;
281 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
283 if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
288 bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
289 (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
291 m->m_pkthdr.len -= msiz;
294 * On FreeBSD, rip_input() supplies us with ip->ip_len
295 * already converted into host byteorder and also decreases
296 * it by the lengh of IP header, however, ip_input() expects
297 * that this field is in the original format (network byteorder
298 * and full size of IP packet), so that adjust accordingly.
300 ip->ip_len = htons(ip->ip_len + sizeof(struct ip) - msiz);
303 ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
305 if (sc->sc_if.if_bpf) {
306 u_int32_t af = AF_INET;
307 bpf_mtap2(sc->sc_if.if_bpf, &af, sizeof(af), m);
310 m->m_pkthdr.rcvif = &sc->sc_if;
312 netisr_dispatch(NETISR_IP, m);
316 * Find the gre interface associated with our src/dst/proto set.
318 * XXXRW: Need some sort of drain/refcount mechanism so that the softc
319 * reference remains valid after it's returned from gre_lookup(). Right
320 * now, I'm thinking it should be reference-counted with a gre_dropref()
321 * when the caller is done with the softc. This is complicated by how
322 * to handle destroying the gre softc; probably using a gre_drain() in
323 * in_gre.c during destroy.
325 static struct gre_softc *
330 struct ip *ip = mtod(m, struct ip *);
331 struct gre_softc *sc;
334 for (sc = LIST_FIRST(&gre_softc_list); sc != NULL;
335 sc = LIST_NEXT(sc, sc_list)) {
336 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
337 (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
338 (sc->g_proto == proto) &&
339 ((sc->sc_if.if_flags & IFF_UP) != 0)) {
340 mtx_unlock(&gre_mtx);
344 mtx_unlock(&gre_mtx);