ip_gre.c revision 148613
1123992Ssobomax/*	$NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/netinet/ip_gre.c 148613 2005-08-01 08:14:21Z bz $ */
3103026Ssobomax
4139823Simp/*-
5103026Ssobomax * Copyright (c) 1998 The NetBSD Foundation, Inc.
6103026Ssobomax * All rights reserved.
7103026Ssobomax *
8103026Ssobomax * This code is derived from software contributed to The NetBSD Foundation
9103026Ssobomax * by Heiko W.Rupp <hwr@pilhuhn.de>
10103026Ssobomax *
11148613Sbz * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12148613Sbz *
13103026Ssobomax * Redistribution and use in source and binary forms, with or without
14103026Ssobomax * modification, are permitted provided that the following conditions
15103026Ssobomax * are met:
16103026Ssobomax * 1. Redistributions of source code must retain the above copyright
17103026Ssobomax *    notice, this list of conditions and the following disclaimer.
18103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
19103026Ssobomax *    notice, this list of conditions and the following disclaimer in the
20103026Ssobomax *    documentation and/or other materials provided with the distribution.
21103026Ssobomax * 3. All advertising materials mentioning features or use of this software
22103026Ssobomax *    must display the following acknowledgement:
23103026Ssobomax *        This product includes software developed by the NetBSD
24103026Ssobomax *        Foundation, Inc. and its contributors.
25103026Ssobomax * 4. Neither the name of The NetBSD Foundation nor the names of its
26103026Ssobomax *    contributors may be used to endorse or promote products derived
27103026Ssobomax *    from this software without specific prior written permission.
28103026Ssobomax *
29103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32103026Ssobomax * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39103026Ssobomax * POSSIBILITY OF SUCH DAMAGE.
40103026Ssobomax */
41103026Ssobomax
42103026Ssobomax/*
43103026Ssobomax * deencapsulate tunneled packets and send them on
44103026Ssobomax * output half is in net/if_gre.[ch]
45103026Ssobomax * This currently handles IPPROTO_GRE, IPPROTO_MOBILE
46103026Ssobomax */
47103026Ssobomax
48103026Ssobomax#include "opt_inet.h"
49103026Ssobomax#include "opt_atalk.h"
50148613Sbz#include "opt_inet6.h"
51103026Ssobomax
52103026Ssobomax#include <sys/param.h>
53103026Ssobomax#include <sys/systm.h>
54103026Ssobomax#include <sys/mbuf.h>
55103026Ssobomax#include <sys/socket.h>
56103026Ssobomax#include <sys/socketvar.h>
57103026Ssobomax#include <sys/protosw.h>
58103026Ssobomax#include <sys/errno.h>
59103026Ssobomax#include <sys/time.h>
60103026Ssobomax#include <sys/kernel.h>
61103026Ssobomax#include <sys/syslog.h>
62103026Ssobomax#include <net/bpf.h>
63103026Ssobomax#include <net/ethernet.h>
64103026Ssobomax#include <net/if.h>
65103026Ssobomax#include <net/netisr.h>
66103026Ssobomax#include <net/route.h>
67103026Ssobomax#include <net/raw_cb.h>
68103026Ssobomax
69103026Ssobomax#ifdef INET
70103026Ssobomax#include <netinet/in.h>
71103026Ssobomax#include <netinet/in_var.h>
72103026Ssobomax#include <netinet/in_systm.h>
73103026Ssobomax#include <netinet/ip.h>
74103026Ssobomax#include <netinet/ip_var.h>
75103026Ssobomax#include <netinet/ip_gre.h>
76103026Ssobomax#include <machine/in_cksum.h>
77103026Ssobomax#else
78103026Ssobomax#error ip_gre input without IP?
79103026Ssobomax#endif
80103026Ssobomax
81103026Ssobomax#ifdef NETATALK
82103026Ssobomax#include <netatalk/at.h>
83103026Ssobomax#include <netatalk/at_var.h>
84103026Ssobomax#include <netatalk/at_extern.h>
85103026Ssobomax#endif
86103026Ssobomax
87103026Ssobomax/* Needs IP headers. */
88103026Ssobomax#include <net/if_gre.h>
89103026Ssobomax
90103026Ssobomax#include <machine/stdarg.h>
91103026Ssobomax
92103026Ssobomax#if 1
93133874Srwatsonvoid gre_inet_ntoa(struct in_addr in);	/* XXX */
94103026Ssobomax#endif
95103026Ssobomax
96105301Salfredstatic struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
97103026Ssobomax
98105301Salfredstatic int	gre_input2(struct mbuf *, int, u_char);
99103026Ssobomax
100103026Ssobomax/*
101103026Ssobomax * De-encapsulate a packet and feed it back through ip input (this
102103026Ssobomax * routine is called whenever IP gets a packet with proto type
103103026Ssobomax * IPPROTO_GRE and a local destination address).
104103026Ssobomax * This really is simple
105103026Ssobomax */
106103026Ssobomaxvoid
107103026Ssobomax#if __STDC__
108103026Ssobomaxgre_input(struct mbuf *m, ...)
109103026Ssobomax#else
110103026Ssobomaxgre_input(m, va_alist)
111123992Ssobomax	struct mbuf *m;
112123992Ssobomax	va_dcl
113103026Ssobomax#endif
114103026Ssobomax{
115103026Ssobomax	int off, ret, proto;
116103026Ssobomax	va_list ap;
117103026Ssobomax
118103026Ssobomax	va_start(ap, m);
119103026Ssobomax	off = va_arg(ap, int);
120103026Ssobomax	va_end(ap);
121103026Ssobomax	proto = (mtod(m, struct ip *))->ip_p;
122103026Ssobomax
123103026Ssobomax	ret = gre_input2(m, off, proto);
124103026Ssobomax	/*
125125020Ssobomax	 * ret == 0 : packet not processed, meaning that
126103026Ssobomax	 * no matching tunnel that is up is found.
127103026Ssobomax	 * we inject it to raw ip socket to see if anyone picks it up.
128103026Ssobomax	 */
129103026Ssobomax	if (ret == 0)
130103026Ssobomax		rip_input(m, off);
131103026Ssobomax}
132103026Ssobomax
133103026Ssobomax/*
134103026Ssobomax * decapsulate.
135103026Ssobomax * Does the real work and is called from gre_input() (above)
136103026Ssobomax * returns 0 if packet is not yet processed
137103026Ssobomax * and 1 if it needs no further processing
138103026Ssobomax * proto is the protocol number of the "calling" foo_input()
139103026Ssobomax * routine.
140103026Ssobomax */
141103032Ssobomaxstatic int
142103026Ssobomaxgre_input2(struct mbuf *m ,int hlen, u_char proto)
143103026Ssobomax{
144123992Ssobomax	struct greip *gip;
145111888Sjlemon	int isr;
146103026Ssobomax	struct gre_softc *sc;
147123992Ssobomax	u_int16_t flags;
148148613Sbz	u_int32_t af;
149103026Ssobomax
150103026Ssobomax	if ((sc = gre_lookup(m, proto)) == NULL) {
151103026Ssobomax		/* No matching tunnel or tunnel is down. */
152103026Ssobomax		return (0);
153103026Ssobomax	}
154103026Ssobomax
155123992Ssobomax	if (m->m_len < sizeof(*gip)) {
156123992Ssobomax		m = m_pullup(m, sizeof(*gip));
157123992Ssobomax		if (m == NULL)
158123992Ssobomax			return (ENOBUFS);
159123992Ssobomax	}
160123992Ssobomax	gip = mtod(m, struct greip *);
161123992Ssobomax
162147256Sbrooks	GRE2IFP(sc)->if_ipackets++;
163147256Sbrooks	GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len;
164103026Ssobomax
165103026Ssobomax	switch (proto) {
166103026Ssobomax	case IPPROTO_GRE:
167123992Ssobomax		hlen += sizeof(struct gre_h);
168103026Ssobomax
169103026Ssobomax		/* process GRE flags as packet can be of variable len */
170103026Ssobomax		flags = ntohs(gip->gi_flags);
171103026Ssobomax
172103026Ssobomax		/* Checksum & Offset are present */
173103026Ssobomax		if ((flags & GRE_CP) | (flags & GRE_RP))
174103026Ssobomax			hlen += 4;
175103026Ssobomax		/* We don't support routing fields (variable length) */
176103026Ssobomax		if (flags & GRE_RP)
177123992Ssobomax			return (0);
178103026Ssobomax		if (flags & GRE_KP)
179103026Ssobomax			hlen += 4;
180103026Ssobomax		if (flags & GRE_SP)
181123992Ssobomax			hlen += 4;
182103026Ssobomax
183103026Ssobomax		switch (ntohs(gip->gi_ptype)) { /* ethertypes */
184125024Ssobomax		case WCCP_PROTOCOL_TYPE:
185125024Ssobomax			if (sc->wccp_ver == WCCP_V2)
186125024Ssobomax				hlen += 4;
187125024Ssobomax			/* FALLTHROUGH */
188125024Ssobomax		case ETHERTYPE_IP:	/* shouldn't need a schednetisr(), */
189125024Ssobomax			isr = NETISR_IP;/* as we are in ip_input */
190148613Sbz			af = AF_INET;
191103026Ssobomax			break;
192148613Sbz#ifdef INET6
193148613Sbz		case ETHERTYPE_IPV6:
194148613Sbz			isr = NETISR_IPV6;
195148613Sbz			af = AF_INET6;
196148613Sbz			break;
197148613Sbz#endif
198103026Ssobomax#ifdef NETATALK
199103026Ssobomax		case ETHERTYPE_ATALK:
200111888Sjlemon			isr = NETISR_ATALK1;
201148613Sbz			af = AF_APPLETALK;
202103026Ssobomax			break;
203103026Ssobomax#endif
204103026Ssobomax		default:	   /* others not yet supported */
205123992Ssobomax			return (0);
206103026Ssobomax		}
207103026Ssobomax		break;
208103026Ssobomax	default:
209103026Ssobomax		/* others not yet supported */
210123992Ssobomax		return (0);
211103026Ssobomax	}
212103026Ssobomax
213123992Ssobomax	if (hlen > m->m_pkthdr.len) {
214123992Ssobomax		m_freem(m);
215123992Ssobomax		return (EINVAL);
216123992Ssobomax	}
217125226Ssobomax	/* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */
218123992Ssobomax	m_adj(m, hlen);
219103026Ssobomax
220147256Sbrooks	if (GRE2IFP(sc)->if_bpf) {
221147256Sbrooks		bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
222104366Ssobomax	}
223103026Ssobomax
224147256Sbrooks	m->m_pkthdr.rcvif = GRE2IFP(sc);
225103026Ssobomax
226111888Sjlemon	netisr_dispatch(isr, m);
227103026Ssobomax
228123992Ssobomax	return (1);	/* packet is done, no further processing needed */
229103026Ssobomax}
230103026Ssobomax
231103026Ssobomax/*
232103026Ssobomax * input routine for IPPRPOTO_MOBILE
233103026Ssobomax * This is a little bit diffrent from the other modes, as the
234103026Ssobomax * encapsulating header was not prepended, but instead inserted
235103026Ssobomax * between IP header and payload
236103026Ssobomax */
237103026Ssobomax
238103026Ssobomaxvoid
239103026Ssobomax#if __STDC__
240103026Ssobomaxgre_mobile_input(struct mbuf *m, ...)
241103026Ssobomax#else
242103026Ssobomaxgre_mobile_input(m, va_alist)
243133874Srwatson	struct mbuf *m;
244133874Srwatson	va_dcl
245103026Ssobomax#endif
246103026Ssobomax{
247123992Ssobomax	struct ip *ip;
248123992Ssobomax	struct mobip_h *mip;
249103026Ssobomax	struct gre_softc *sc;
250111888Sjlemon	int hlen;
251103026Ssobomax	va_list ap;
252103026Ssobomax	int msiz;
253103026Ssobomax
254123992Ssobomax	va_start(ap, m);
255103026Ssobomax	hlen = va_arg(ap, int);
256103026Ssobomax	va_end(ap);
257103026Ssobomax
258103026Ssobomax	if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
259103026Ssobomax		/* No matching tunnel or tunnel is down. */
260103026Ssobomax		m_freem(m);
261103026Ssobomax		return;
262103026Ssobomax	}
263103026Ssobomax
264123992Ssobomax	if (m->m_len < sizeof(*mip)) {
265123992Ssobomax		m = m_pullup(m, sizeof(*mip));
266123992Ssobomax		if (m == NULL)
267123992Ssobomax			return;
268123992Ssobomax	}
269123992Ssobomax	ip = mtod(m, struct ip *);
270123992Ssobomax	mip = mtod(m, struct mobip_h *);
271123992Ssobomax
272147256Sbrooks	GRE2IFP(sc)->if_ipackets++;
273147256Sbrooks	GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len;
274103026Ssobomax
275123992Ssobomax	if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
276103026Ssobomax		msiz = MOB_H_SIZ_L;
277103026Ssobomax		mip->mi.ip_src.s_addr = mip->mh.osrc;
278123992Ssobomax	} else
279103026Ssobomax		msiz = MOB_H_SIZ_S;
280123992Ssobomax
281123992Ssobomax	if (m->m_len < (ip->ip_hl << 2) + msiz) {
282123992Ssobomax		m = m_pullup(m, (ip->ip_hl << 2) + msiz);
283123992Ssobomax		if (m == NULL)
284123992Ssobomax			return;
285123992Ssobomax		ip = mtod(m, struct ip *);
286123992Ssobomax		mip = mtod(m, struct mobip_h *);
287103026Ssobomax	}
288123992Ssobomax
289103026Ssobomax	mip->mi.ip_dst.s_addr = mip->mh.odst;
290103026Ssobomax	mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
291103026Ssobomax
292123992Ssobomax	if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
293103026Ssobomax		m_freem(m);
294103026Ssobomax		return;
295103026Ssobomax	}
296103026Ssobomax
297103026Ssobomax	bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
298103026Ssobomax	    (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
299103026Ssobomax	m->m_len -= msiz;
300103026Ssobomax	m->m_pkthdr.len -= msiz;
301103026Ssobomax
302103026Ssobomax	/*
303103026Ssobomax	 * On FreeBSD, rip_input() supplies us with ip->ip_len
304103026Ssobomax	 * already converted into host byteorder and also decreases
305103026Ssobomax	 * it by the lengh of IP header, however, ip_input() expects
306103026Ssobomax	 * that this field is in the original format (network byteorder
307103026Ssobomax	 * and full size of IP packet), so that adjust accordingly.
308103026Ssobomax	 */
309103026Ssobomax	ip->ip_len = htons(ip->ip_len + sizeof(struct ip) - msiz);
310125020Ssobomax
311103026Ssobomax	ip->ip_sum = 0;
312103026Ssobomax	ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
313103026Ssobomax
314147256Sbrooks	if (GRE2IFP(sc)->if_bpf) {
315123922Ssam		u_int32_t af = AF_INET;
316147256Sbrooks		bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
317104366Ssobomax	}
318103026Ssobomax
319147256Sbrooks	m->m_pkthdr.rcvif = GRE2IFP(sc);
320103026Ssobomax
321111888Sjlemon	netisr_dispatch(NETISR_IP, m);
322103026Ssobomax}
323103026Ssobomax
324103026Ssobomax/*
325103026Ssobomax * Find the gre interface associated with our src/dst/proto set.
326127307Srwatson *
327127307Srwatson * XXXRW: Need some sort of drain/refcount mechanism so that the softc
328127307Srwatson * reference remains valid after it's returned from gre_lookup().  Right
329127307Srwatson * now, I'm thinking it should be reference-counted with a gre_dropref()
330127307Srwatson * when the caller is done with the softc.  This is complicated by how
331127307Srwatson * to handle destroying the gre softc; probably using a gre_drain() in
332127307Srwatson * in_gre.c during destroy.
333103026Ssobomax */
334103032Ssobomaxstatic struct gre_softc *
335103026Ssobomaxgre_lookup(m, proto)
336103026Ssobomax	struct mbuf *m;
337103026Ssobomax	u_int8_t proto;
338103026Ssobomax{
339103026Ssobomax	struct ip *ip = mtod(m, struct ip *);
340103026Ssobomax	struct gre_softc *sc;
341103026Ssobomax
342127307Srwatson	mtx_lock(&gre_mtx);
343103026Ssobomax	for (sc = LIST_FIRST(&gre_softc_list); sc != NULL;
344103026Ssobomax	     sc = LIST_NEXT(sc, sc_list)) {
345103026Ssobomax		if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
346103026Ssobomax		    (sc->g_src.s_addr == ip->ip_dst.s_addr) &&
347103026Ssobomax		    (sc->g_proto == proto) &&
348147256Sbrooks		    ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) {
349127307Srwatson			mtx_unlock(&gre_mtx);
350103026Ssobomax			return (sc);
351127307Srwatson		}
352103026Ssobomax	}
353127307Srwatson	mtx_unlock(&gre_mtx);
354103026Ssobomax
355103026Ssobomax	return (NULL);
356103026Ssobomax}
357