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 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * deencapsulate tunneled packets and send them on
44 * output half is in net/if_gre.[ch]
45 * This currently handles IPPROTO_GRE, IPPROTO_MOBILE
49 #include "opt_atalk.h"
50 #include "opt_inet6.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/protosw.h>
58 #include <sys/errno.h>
60 #include <sys/kernel.h>
61 #include <sys/syslog.h>
63 #include <net/ethernet.h>
65 #include <net/netisr.h>
66 #include <net/route.h>
67 #include <net/raw_cb.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_gre.h>
76 #include <machine/in_cksum.h>
78 #error ip_gre input without IP?
82 #include <netatalk/at.h>
83 #include <netatalk/at_var.h>
84 #include <netatalk/at_extern.h>
87 /* Needs IP headers. */
88 #include <net/if_gre.h>
90 #include <machine/stdarg.h>
93 void gre_inet_ntoa(struct in_addr in); /* XXX */
96 static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
98 static int gre_input2(struct mbuf *, int, u_char);
101 * De-encapsulate a packet and feed it back through ip input (this
102 * routine is called whenever IP gets a packet with proto type
103 * IPPROTO_GRE and a local destination address).
104 * This really is simple
108 gre_input(struct mbuf *m, ...)
110 gre_input(m, va_alist)
119 off = va_arg(ap, int);
121 proto = (mtod(m, struct ip *))->ip_p;
123 ret = gre_input2(m, off, proto);
125 * ret == 0 : packet not processed, meaning that
126 * no matching tunnel that is up is found.
127 * we inject it to raw ip socket to see if anyone picks it up.
135 * Does the real work and is called from gre_input() (above)
136 * returns 0 if packet is not yet processed
137 * and 1 if it needs no further processing
138 * proto is the protocol number of the "calling" foo_input()
142 gre_input2(struct mbuf *m ,int hlen, u_char proto)
146 struct gre_softc *sc;
150 if ((sc = gre_lookup(m, proto)) == NULL) {
151 /* No matching tunnel or tunnel is down. */
155 if (m->m_len < sizeof(*gip)) {
156 m = m_pullup(m, sizeof(*gip));
160 gip = mtod(m, struct greip *);
162 GRE2IFP(sc)->if_ipackets++;
163 GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len;
167 hlen += sizeof(struct gre_h);
169 /* process GRE flags as packet can be of variable len */
170 flags = ntohs(gip->gi_flags);
172 /* Checksum & Offset are present */
173 if ((flags & GRE_CP) | (flags & GRE_RP))
175 /* We don't support routing fields (variable length) */
183 switch (ntohs(gip->gi_ptype)) { /* ethertypes */
184 case WCCP_PROTOCOL_TYPE:
185 if (sc->wccp_ver == WCCP_V2)
188 case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */
189 isr = NETISR_IP;/* as we are in ip_input */
199 case ETHERTYPE_ATALK:
204 default: /* others not yet supported */
209 /* others not yet supported */
213 if (hlen > m->m_pkthdr.len) {
217 /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */
220 if (GRE2IFP(sc)->if_bpf) {
221 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
224 m->m_pkthdr.rcvif = GRE2IFP(sc);
226 netisr_dispatch(isr, m);
228 return (1); /* packet is done, no further processing needed */
232 * input routine for IPPRPOTO_MOBILE
233 * This is a little bit diffrent from the other modes, as the
234 * encapsulating header was not prepended, but instead inserted
235 * between IP header and payload
240 gre_mobile_input(struct mbuf *m, ...)
242 gre_mobile_input(m, va_alist)
249 struct gre_softc *sc;
255 hlen = va_arg(ap, int);
258 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
259 /* No matching tunnel or tunnel is down. */
264 if (m->m_len < sizeof(*mip)) {
265 m = m_pullup(m, sizeof(*mip));
269 ip = mtod(m, struct ip *);
270 mip = mtod(m, struct mobip_h *);
272 GRE2IFP(sc)->if_ipackets++;
273 GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len;
275 if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
277 mip->mi.ip_src.s_addr = mip->mh.osrc;
281 if (m->m_len < (ip->ip_hl << 2) + msiz) {
282 m = m_pullup(m, (ip->ip_hl << 2) + msiz);
285 ip = mtod(m, struct ip *);
286 mip = mtod(m, struct mobip_h *);
289 mip->mi.ip_dst.s_addr = mip->mh.odst;
290 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
292 if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
297 bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
298 (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
300 m->m_pkthdr.len -= msiz;
303 * On FreeBSD, rip_input() supplies us with ip->ip_len
304 * already converted into host byteorder and also decreases
305 * it by the lengh of IP header, however, ip_input() expects
306 * that this field is in the original format (network byteorder
307 * and full size of IP packet), so that adjust accordingly.
309 ip->ip_len = htons(ip->ip_len + sizeof(struct ip) - msiz);
312 ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
314 if (GRE2IFP(sc)->if_bpf) {
315 u_int32_t af = AF_INET;
316 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
319 m->m_pkthdr.rcvif = GRE2IFP(sc);
321 netisr_dispatch(NETISR_IP, m);
325 * Find the gre interface associated with our src/dst/proto set.
327 * XXXRW: Need some sort of drain/refcount mechanism so that the softc
328 * reference remains valid after it's returned from gre_lookup(). Right
329 * now, I'm thinking it should be reference-counted with a gre_dropref()
330 * when the caller is done with the softc. This is complicated by how
331 * to handle destroying the gre softc; probably using a gre_drain() in
332 * in_gre.c during destroy.
334 static struct gre_softc *
339 struct ip *ip = mtod(m, struct ip *);
340 struct gre_softc *sc;
343 for (sc = LIST_FIRST(&gre_softc_list); sc != NULL;
344 sc = LIST_NEXT(sc, sc_list)) {
345 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
346 (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
347 (sc->g_proto == proto) &&
348 ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) {
349 mtx_unlock(&gre_mtx);
353 mtx_unlock(&gre_mtx);