2 * $NetBSD: ip_gre.c,v 1.21 2002/08/14 00:23:30 itojun Exp $
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * deencapsulate tunneled packets and send them on
41 * output half is in net/if_gre.[ch]
42 * This currently handles IPPROTO_GRE, IPPROTO_MOBILE
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/protosw.h>
53 #include <sys/errno.h>
55 #include <sys/kernel.h>
56 #include <sys/syslog.h>
57 #include <sys/in_cksum.h>
59 #include <net/ethernet.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/raw_cb.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_gre.h>
73 #error ip_gre input without IP?
76 /* Needs IP headers. */
77 #include <net/gre/if_gre.h>
79 #include <machine/stdarg.h>
82 void gre_inet_ntoa(struct in_addr in); /* XXX */
85 static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
87 static int gre_input2(struct mbuf *, int, u_char);
90 * De-encapsulate a packet and feed it back through ip input (this
91 * routine is called whenever IP gets a packet with proto type
92 * IPPROTO_GRE and a local destination address).
93 * This really is simple
96 gre_input(struct mbuf **mp, int *offp, int proto)
105 proto = (mtod(m, struct ip *))->ip_p;
107 ret = gre_input2(m, off, proto);
109 * ret == 0 : packet not processed, meaning that
110 * no matching tunnel that is up is found.
111 * we inject it to raw ip socket to see if anyone picks it up.
115 rip_input(mp, offp, proto);
117 return(IPPROTO_DONE);
122 * Does the real work and is called from gre_input() (above)
123 * returns 0 if packet is not yet processed
124 * and 1 if it needs no further processing
125 * proto is the protocol number of the "calling" foo_input()
130 gre_input2(struct mbuf *m ,int hlen, u_char proto)
132 static const uint32_t af = AF_INET;
133 struct greip *gip = mtod(m, struct greip *);
135 struct gre_softc *sc;
138 if ((sc = gre_lookup(m, proto)) == NULL) {
139 /* No matching tunnel or tunnel is down. */
143 sc->sc_if.if_ipackets++;
144 sc->sc_if.if_ibytes += m->m_pkthdr.len;
148 hlen += sizeof (struct gre_h);
150 /* process GRE flags as packet can be of variable len */
151 flags = ntohs(gip->gi_flags);
153 /* Checksum & Offset are present */
154 if ((flags & GRE_CP) | (flags & GRE_RP))
156 /* We don't support routing fields (variable length) */
164 switch (ntohs(gip->gi_ptype)) { /* ethertypes */
166 case WCCP_PROTOCOL_TYPE:
171 default: /* others not yet supported */
176 /* others not yet supported */
182 m->m_pkthdr.len -= hlen;
184 if (sc->sc_if.if_bpf)
185 bpf_ptap(sc->sc_if.if_bpf, m, &af, sizeof(af));
187 m->m_pkthdr.rcvif = &sc->sc_if;
188 netisr_queue(isr, m);
189 return(1); /* packet is done, no further processing needed */
193 * input routine for IPPRPOTO_MOBILE
194 * This is a little bit diffrent from the other modes, as the
195 * encapsulating header was not prepended, but instead inserted
196 * between IP header and payload
200 gre_mobile_input(struct mbuf **mp, int *offp, int proto)
202 static const uint32_t af = AF_INET;
203 struct mbuf *m = *mp;
204 struct ip *ip = mtod(m, struct ip *);
205 struct mobip_h *mip = mtod(m, struct mobip_h *);
206 struct gre_softc *sc;
211 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
212 /* No matching tunnel or tunnel is down. */
214 return(IPPROTO_DONE);
217 sc->sc_if.if_ipackets++;
218 sc->sc_if.if_ibytes += m->m_pkthdr.len;
220 if(ntohs(mip->mh.proto) & MOB_H_SBIT) {
222 mip->mi.ip_src.s_addr = mip->mh.osrc;
226 mip->mi.ip_dst.s_addr = mip->mh.odst;
227 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
229 if (gre_in_cksum((u_short*)&mip->mh,msiz) != 0) {
231 return(IPPROTO_DONE);
234 bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
235 (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
237 m->m_pkthdr.len -= msiz;
240 * On FreeBSD, rip_input() supplies us with ip->ip_len
241 * already converted into host byteorder and also decreases
242 * it by the lengh of IP header, however, ip_input() expects
243 * that this field is in the original format (network byteorder
244 * and full size of IP packet), so that adjust accordingly.
246 ip->ip_len = htons(ip->ip_len + sizeof(struct ip) - msiz);
249 ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
251 if (sc->sc_if.if_bpf)
252 bpf_ptap(sc->sc_if.if_bpf, m, &af, sizeof(af));
254 m->m_pkthdr.rcvif = &sc->sc_if;
256 netisr_queue(NETISR_IP, m);
257 return(IPPROTO_DONE);
261 * Find the gre interface associated with our src/dst/proto set.
263 static struct gre_softc *
264 gre_lookup(struct mbuf *m, u_int8_t proto)
266 struct ip *ip = mtod(m, struct ip *);
267 struct gre_softc *sc;
269 for (sc = LIST_FIRST(&gre_softc_list); sc != NULL;
270 sc = LIST_NEXT(sc, sc_list)) {
271 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
272 (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
273 (sc->g_proto == proto) &&
274 ((sc->sc_if.if_flags & IFF_UP) != 0))