2003-12-30 11:41:43 +00:00
|
|
|
/* $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2002-09-06 17:12:50 +00:00
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Heiko W.Rupp <hwr@pilhuhn.de>
|
|
|
|
*
|
2005-08-01 08:14:21 +00:00
|
|
|
* IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
|
|
|
|
*
|
2002-09-06 17:12:50 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deencapsulate tunneled packets and send them on
|
|
|
|
* output half is in net/if_gre.[ch]
|
|
|
|
* This currently handles IPPROTO_GRE, IPPROTO_MOBILE
|
|
|
|
*/
|
|
|
|
|
2007-10-07 20:44:24 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-09-06 17:12:50 +00:00
|
|
|
#include "opt_inet.h"
|
2005-08-01 08:14:21 +00:00
|
|
|
#include "opt_inet6.h"
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <net/bpf.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/if.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
2002-09-06 17:12:50 +00:00
|
|
|
#include <net/netisr.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/raw_cb.h>
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/ip_gre.h>
|
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
#else
|
2012-01-22 10:41:58 +00:00
|
|
|
#error "ip_gre requires INET"
|
2002-09-06 17:12:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Needs IP headers. */
|
|
|
|
#include <net/if_gre.h>
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
#if 1
|
2004-08-16 18:32:07 +00:00
|
|
|
void gre_inet_ntoa(struct in_addr in); /* XXX */
|
2002-09-06 17:12:50 +00:00
|
|
|
#endif
|
|
|
|
|
2002-10-16 22:27:27 +00:00
|
|
|
static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2006-05-16 11:15:22 +00:00
|
|
|
static struct mbuf *gre_input2(struct mbuf *, int, u_char);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* De-encapsulate a packet and feed it back through ip input (this
|
|
|
|
* routine is called whenever IP gets a packet with proto type
|
|
|
|
* IPPROTO_GRE and a local destination address).
|
|
|
|
* This really is simple
|
|
|
|
*/
|
2014-08-08 01:57:15 +00:00
|
|
|
int
|
|
|
|
gre_input(struct mbuf **mp, int *offp, int proto)
|
2002-09-06 17:12:50 +00:00
|
|
|
{
|
2014-08-08 01:57:15 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
int off;
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2014-08-08 01:57:15 +00:00
|
|
|
m = *mp;
|
|
|
|
off = *offp;
|
|
|
|
*mp = NULL;
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2006-05-16 11:15:22 +00:00
|
|
|
m = gre_input2(m, off, proto);
|
|
|
|
|
2002-09-06 17:12:50 +00:00
|
|
|
/*
|
2006-05-16 11:15:22 +00:00
|
|
|
* If no matching tunnel that is up is found. We inject
|
|
|
|
* the mbuf to raw ip socket to see if anyone picks it up.
|
2002-09-06 17:12:50 +00:00
|
|
|
*/
|
2014-08-08 01:57:15 +00:00
|
|
|
if (m != NULL) {
|
|
|
|
*mp = m;
|
|
|
|
rip_input(mp, offp, proto);
|
|
|
|
}
|
|
|
|
return (IPPROTO_DONE);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-05-16 11:15:22 +00:00
|
|
|
* Decapsulate. Does the real work and is called from gre_input()
|
|
|
|
* (above). Returns an mbuf back if packet is not yet processed,
|
|
|
|
* and NULL if it needs no further processing. proto is the protocol
|
|
|
|
* number of the "calling" foo_input() routine.
|
2002-09-06 17:12:50 +00:00
|
|
|
*/
|
2006-05-16 11:15:22 +00:00
|
|
|
static struct mbuf *
|
2002-09-06 17:12:50 +00:00
|
|
|
gre_input2(struct mbuf *m ,int hlen, u_char proto)
|
|
|
|
{
|
2003-12-30 11:41:43 +00:00
|
|
|
struct greip *gip;
|
2003-03-04 23:19:55 +00:00
|
|
|
int isr;
|
2002-09-06 17:12:50 +00:00
|
|
|
struct gre_softc *sc;
|
2003-12-30 11:41:43 +00:00
|
|
|
u_int16_t flags;
|
2005-08-01 08:14:21 +00:00
|
|
|
u_int32_t af;
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
if ((sc = gre_lookup(m, proto)) == NULL) {
|
|
|
|
/* No matching tunnel or tunnel is down. */
|
2006-05-16 11:15:22 +00:00
|
|
|
return (m);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 11:41:43 +00:00
|
|
|
if (m->m_len < sizeof(*gip)) {
|
|
|
|
m = m_pullup(m, sizeof(*gip));
|
|
|
|
if (m == NULL)
|
2006-05-16 11:15:22 +00:00
|
|
|
return (NULL);
|
2003-12-30 11:41:43 +00:00
|
|
|
}
|
|
|
|
gip = mtod(m, struct greip *);
|
|
|
|
|
2014-09-19 10:19:51 +00:00
|
|
|
if_inc_counter(GRE2IFP(sc), IFCOUNTER_IPACKETS, 1);
|
|
|
|
if_inc_counter(GRE2IFP(sc), IFCOUNTER_IBYTES, m->m_pkthdr.len);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_GRE:
|
2003-12-30 11:41:43 +00:00
|
|
|
hlen += sizeof(struct gre_h);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
/* process GRE flags as packet can be of variable len */
|
|
|
|
flags = ntohs(gip->gi_flags);
|
|
|
|
|
|
|
|
/* Checksum & Offset are present */
|
|
|
|
if ((flags & GRE_CP) | (flags & GRE_RP))
|
|
|
|
hlen += 4;
|
|
|
|
/* We don't support routing fields (variable length) */
|
|
|
|
if (flags & GRE_RP)
|
2006-05-16 11:15:22 +00:00
|
|
|
return (m);
|
2002-09-06 17:12:50 +00:00
|
|
|
if (flags & GRE_KP)
|
|
|
|
hlen += 4;
|
|
|
|
if (flags & GRE_SP)
|
2003-12-30 11:41:43 +00:00
|
|
|
hlen += 4;
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
switch (ntohs(gip->gi_ptype)) { /* ethertypes */
|
2004-01-26 12:33:56 +00:00
|
|
|
case WCCP_PROTOCOL_TYPE:
|
|
|
|
if (sc->wccp_ver == WCCP_V2)
|
|
|
|
hlen += 4;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */
|
|
|
|
isr = NETISR_IP;/* as we are in ip_input */
|
2005-08-01 08:14:21 +00:00
|
|
|
af = AF_INET;
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case ETHERTYPE_IPV6:
|
|
|
|
isr = NETISR_IPV6;
|
|
|
|
af = AF_INET6;
|
2002-09-06 17:12:50 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2006-05-16 11:15:22 +00:00
|
|
|
default:
|
|
|
|
/* Others not yet supported. */
|
|
|
|
return (m);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2006-05-16 11:15:22 +00:00
|
|
|
/* Others not yet supported. */
|
|
|
|
return (m);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 11:41:43 +00:00
|
|
|
if (hlen > m->m_pkthdr.len) {
|
|
|
|
m_freem(m);
|
2006-05-16 11:15:22 +00:00
|
|
|
return (NULL);
|
2003-12-30 11:41:43 +00:00
|
|
|
}
|
2004-01-30 09:03:01 +00:00
|
|
|
/* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */
|
2003-12-30 11:41:43 +00:00
|
|
|
m_adj(m, hlen);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2006-06-02 19:59:33 +00:00
|
|
|
if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) {
|
2005-06-10 16:49:24 +00:00
|
|
|
bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
|
2002-10-02 09:38:17 +00:00
|
|
|
}
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2013-05-11 19:05:38 +00:00
|
|
|
if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) {
|
|
|
|
m_freem(m);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2005-06-10 16:49:24 +00:00
|
|
|
m->m_pkthdr.rcvif = GRE2IFP(sc);
|
2014-10-08 21:23:34 +00:00
|
|
|
m_clrprotoflags(m);
|
2008-07-04 21:01:30 +00:00
|
|
|
netisr_queue(isr, m);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2006-05-16 11:15:22 +00:00
|
|
|
/* Packet is done, no further processing needed. */
|
|
|
|
return (NULL);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* input routine for IPPRPOTO_MOBILE
|
|
|
|
* This is a little bit diffrent from the other modes, as the
|
|
|
|
* encapsulating header was not prepended, but instead inserted
|
|
|
|
* between IP header and payload
|
|
|
|
*/
|
|
|
|
|
2014-08-08 01:57:15 +00:00
|
|
|
int
|
|
|
|
gre_mobile_input(struct mbuf **mp, int *offp, int proto)
|
2002-09-06 17:12:50 +00:00
|
|
|
{
|
2003-12-30 11:41:43 +00:00
|
|
|
struct ip *ip;
|
|
|
|
struct mobip_h *mip;
|
2014-08-08 01:57:15 +00:00
|
|
|
struct mbuf *m;
|
2002-09-06 17:12:50 +00:00
|
|
|
struct gre_softc *sc;
|
|
|
|
int msiz;
|
|
|
|
|
2014-08-08 01:57:15 +00:00
|
|
|
m = *mp;
|
2002-09-06 17:12:50 +00:00
|
|
|
if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) {
|
|
|
|
/* No matching tunnel or tunnel is down. */
|
|
|
|
m_freem(m);
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 11:41:43 +00:00
|
|
|
if (m->m_len < sizeof(*mip)) {
|
|
|
|
m = m_pullup(m, sizeof(*mip));
|
|
|
|
if (m == NULL)
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2003-12-30 11:41:43 +00:00
|
|
|
}
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
mip = mtod(m, struct mobip_h *);
|
|
|
|
|
2014-09-19 10:19:51 +00:00
|
|
|
if_inc_counter(GRE2IFP(sc), IFCOUNTER_IPACKETS, 1);
|
|
|
|
if_inc_counter(GRE2IFP(sc), IFCOUNTER_IBYTES, m->m_pkthdr.len);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2003-12-30 11:41:43 +00:00
|
|
|
if (ntohs(mip->mh.proto) & MOB_H_SBIT) {
|
2002-09-06 17:12:50 +00:00
|
|
|
msiz = MOB_H_SIZ_L;
|
|
|
|
mip->mi.ip_src.s_addr = mip->mh.osrc;
|
2003-12-30 11:41:43 +00:00
|
|
|
} else
|
2002-09-06 17:12:50 +00:00
|
|
|
msiz = MOB_H_SIZ_S;
|
2003-12-30 11:41:43 +00:00
|
|
|
|
|
|
|
if (m->m_len < (ip->ip_hl << 2) + msiz) {
|
|
|
|
m = m_pullup(m, (ip->ip_hl << 2) + msiz);
|
|
|
|
if (m == NULL)
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2003-12-30 11:41:43 +00:00
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
mip = mtod(m, struct mobip_h *);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
2003-12-30 11:41:43 +00:00
|
|
|
|
2002-09-06 17:12:50 +00:00
|
|
|
mip->mi.ip_dst.s_addr = mip->mh.odst;
|
|
|
|
mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8);
|
|
|
|
|
2003-12-30 11:41:43 +00:00
|
|
|
if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) {
|
2002-09-06 17:12:50 +00:00
|
|
|
m_freem(m);
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) +
|
|
|
|
(ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2));
|
|
|
|
m->m_len -= msiz;
|
|
|
|
m->m_pkthdr.len -= msiz;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On FreeBSD, rip_input() supplies us with ip->ip_len
|
2012-10-22 21:09:03 +00:00
|
|
|
* decreased by the lengh of IP header, however, ip_input()
|
|
|
|
* expects it to be full size of IP packet, so adjust accordingly.
|
2002-09-06 17:12:50 +00:00
|
|
|
*/
|
2012-10-22 21:09:03 +00:00
|
|
|
ip->ip_len = htons(ntohs(ip->ip_len) + sizeof(struct ip) - msiz);
|
2004-01-26 12:21:59 +00:00
|
|
|
|
2002-09-06 17:12:50 +00:00
|
|
|
ip->ip_sum = 0;
|
|
|
|
ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));
|
|
|
|
|
2006-06-02 19:59:33 +00:00
|
|
|
if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) {
|
2003-12-28 03:56:00 +00:00
|
|
|
u_int32_t af = AF_INET;
|
2005-06-10 16:49:24 +00:00
|
|
|
bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m);
|
2002-10-02 09:38:17 +00:00
|
|
|
}
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2013-05-11 19:05:38 +00:00
|
|
|
if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) {
|
|
|
|
m_freem(m);
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2013-05-11 19:05:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-10 16:49:24 +00:00
|
|
|
m->m_pkthdr.rcvif = GRE2IFP(sc);
|
2002-09-06 17:12:50 +00:00
|
|
|
|
2008-07-04 21:01:30 +00:00
|
|
|
netisr_queue(NETISR_IP, m);
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the gre interface associated with our src/dst/proto set.
|
2004-03-22 16:04:43 +00:00
|
|
|
*
|
|
|
|
* XXXRW: Need some sort of drain/refcount mechanism so that the softc
|
|
|
|
* reference remains valid after it's returned from gre_lookup(). Right
|
|
|
|
* now, I'm thinking it should be reference-counted with a gre_dropref()
|
|
|
|
* when the caller is done with the softc. This is complicated by how
|
|
|
|
* to handle destroying the gre softc; probably using a gre_drain() in
|
|
|
|
* in_gre.c during destroy.
|
2002-09-06 17:12:50 +00:00
|
|
|
*/
|
2002-09-06 18:16:03 +00:00
|
|
|
static struct gre_softc *
|
2006-01-22 01:08:30 +00:00
|
|
|
gre_lookup(struct mbuf *m, u_int8_t proto)
|
2002-09-06 17:12:50 +00:00
|
|
|
{
|
|
|
|
struct ip *ip = mtod(m, struct ip *);
|
|
|
|
struct gre_softc *sc;
|
|
|
|
|
2014-09-21 03:56:06 +00:00
|
|
|
GRE_LIST_LOCK();
|
|
|
|
for (sc = LIST_FIRST(&V_gre_softc_list); sc != NULL;
|
2002-09-06 17:12:50 +00:00
|
|
|
sc = LIST_NEXT(sc, sc_list)) {
|
|
|
|
if ((sc->g_dst.s_addr == ip->ip_src.s_addr) &&
|
|
|
|
(sc->g_src.s_addr == ip->ip_dst.s_addr) &&
|
|
|
|
(sc->g_proto == proto) &&
|
2005-06-10 16:49:24 +00:00
|
|
|
((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) {
|
2014-09-21 03:56:06 +00:00
|
|
|
GRE_LIST_UNLOCK();
|
2002-09-06 17:12:50 +00:00
|
|
|
return (sc);
|
2004-03-22 16:04:43 +00:00
|
|
|
}
|
2002-09-06 17:12:50 +00:00
|
|
|
}
|
2014-09-21 03:56:06 +00:00
|
|
|
GRE_LIST_UNLOCK();
|
2002-09-06 17:12:50 +00:00
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|