1123992Ssobomax/* $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */ 2103026Ssobomax 3139823Simp/*- 4103026Ssobomax * Copyright (c) 1998 The NetBSD Foundation, Inc. 5103026Ssobomax * All rights reserved. 6103026Ssobomax * 7103026Ssobomax * This code is derived from software contributed to The NetBSD Foundation 8103026Ssobomax * by Heiko W.Rupp <hwr@pilhuhn.de> 9103026Ssobomax * 10148613Sbz * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> 11148613Sbz * 12103026Ssobomax * Redistribution and use in source and binary forms, with or without 13103026Ssobomax * modification, are permitted provided that the following conditions 14103026Ssobomax * are met: 15103026Ssobomax * 1. Redistributions of source code must retain the above copyright 16103026Ssobomax * notice, this list of conditions and the following disclaimer. 17103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 18103026Ssobomax * notice, this list of conditions and the following disclaimer in the 19103026Ssobomax * documentation and/or other materials provided with the distribution. 20103026Ssobomax * 21103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24103026Ssobomax * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31103026Ssobomax * POSSIBILITY OF SUCH DAMAGE. 32103026Ssobomax */ 33103026Ssobomax 34103026Ssobomax/* 35103026Ssobomax * deencapsulate tunneled packets and send them on 36103026Ssobomax * output half is in net/if_gre.[ch] 37103026Ssobomax * This currently handles IPPROTO_GRE, IPPROTO_MOBILE 38103026Ssobomax */ 39103026Ssobomax 40172467Ssilby#include <sys/cdefs.h> 41172467Ssilby__FBSDID("$FreeBSD$"); 42172467Ssilby 43103026Ssobomax#include "opt_inet.h" 44103026Ssobomax#include "opt_atalk.h" 45148613Sbz#include "opt_inet6.h" 46103026Ssobomax 47103026Ssobomax#include <sys/param.h> 48103026Ssobomax#include <sys/systm.h> 49103026Ssobomax#include <sys/mbuf.h> 50103026Ssobomax#include <sys/socket.h> 51103026Ssobomax#include <sys/socketvar.h> 52103026Ssobomax#include <sys/protosw.h> 53103026Ssobomax#include <sys/errno.h> 54103026Ssobomax#include <sys/time.h> 55103026Ssobomax#include <sys/kernel.h> 56103026Ssobomax#include <sys/syslog.h> 57103026Ssobomax#include <net/bpf.h> 58103026Ssobomax#include <net/ethernet.h> 59103026Ssobomax#include <net/if.h> 60103026Ssobomax#include <net/netisr.h> 61103026Ssobomax#include <net/route.h> 62103026Ssobomax#include <net/raw_cb.h> 63103026Ssobomax 64103026Ssobomax#ifdef INET 65103026Ssobomax#include <netinet/in.h> 66103026Ssobomax#include <netinet/in_var.h> 67103026Ssobomax#include <netinet/in_systm.h> 68103026Ssobomax#include <netinet/ip.h> 69103026Ssobomax#include <netinet/ip_var.h> 70103026Ssobomax#include <netinet/ip_gre.h> 71103026Ssobomax#include <machine/in_cksum.h> 72103026Ssobomax#else 73230452Sbz#error "ip_gre requires INET" 74103026Ssobomax#endif 75103026Ssobomax 76103026Ssobomax#ifdef NETATALK 77103026Ssobomax#include <netatalk/at.h> 78103026Ssobomax#include <netatalk/at_var.h> 79103026Ssobomax#include <netatalk/at_extern.h> 80103026Ssobomax#endif 81103026Ssobomax 82103026Ssobomax/* Needs IP headers. */ 83103026Ssobomax#include <net/if_gre.h> 84103026Ssobomax 85103026Ssobomax#include <machine/stdarg.h> 86103026Ssobomax 87103026Ssobomax#if 1 88133874Srwatsonvoid gre_inet_ntoa(struct in_addr in); /* XXX */ 89103026Ssobomax#endif 90103026Ssobomax 91105301Salfredstatic struct gre_softc *gre_lookup(struct mbuf *, u_int8_t); 92103026Ssobomax 93158645Sglebiusstatic struct mbuf *gre_input2(struct mbuf *, int, u_char); 94103026Ssobomax 95103026Ssobomax/* 96103026Ssobomax * De-encapsulate a packet and feed it back through ip input (this 97103026Ssobomax * routine is called whenever IP gets a packet with proto type 98103026Ssobomax * IPPROTO_GRE and a local destination address). 99103026Ssobomax * This really is simple 100103026Ssobomax */ 101103026Ssobomaxvoid 102154625Sbzgre_input(struct mbuf *m, int off) 103103026Ssobomax{ 104158645Sglebius int proto; 105103026Ssobomax 106103026Ssobomax proto = (mtod(m, struct ip *))->ip_p; 107103026Ssobomax 108158645Sglebius m = gre_input2(m, off, proto); 109158645Sglebius 110103026Ssobomax /* 111158645Sglebius * If no matching tunnel that is up is found. We inject 112158645Sglebius * the mbuf to raw ip socket to see if anyone picks it up. 113103026Ssobomax */ 114158645Sglebius if (m != NULL) 115103026Ssobomax rip_input(m, off); 116103026Ssobomax} 117103026Ssobomax 118103026Ssobomax/* 119158645Sglebius * Decapsulate. Does the real work and is called from gre_input() 120158645Sglebius * (above). Returns an mbuf back if packet is not yet processed, 121158645Sglebius * and NULL if it needs no further processing. proto is the protocol 122158645Sglebius * number of the "calling" foo_input() routine. 123103026Ssobomax */ 124158645Sglebiusstatic struct mbuf * 125103026Ssobomaxgre_input2(struct mbuf *m ,int hlen, u_char proto) 126103026Ssobomax{ 127123992Ssobomax struct greip *gip; 128111888Sjlemon int isr; 129103026Ssobomax struct gre_softc *sc; 130123992Ssobomax u_int16_t flags; 131148613Sbz u_int32_t af; 132103026Ssobomax 133103026Ssobomax if ((sc = gre_lookup(m, proto)) == NULL) { 134103026Ssobomax /* No matching tunnel or tunnel is down. */ 135158645Sglebius return (m); 136103026Ssobomax } 137103026Ssobomax 138123992Ssobomax if (m->m_len < sizeof(*gip)) { 139123992Ssobomax m = m_pullup(m, sizeof(*gip)); 140123992Ssobomax if (m == NULL) 141158645Sglebius return (NULL); 142123992Ssobomax } 143123992Ssobomax gip = mtod(m, struct greip *); 144123992Ssobomax 145147256Sbrooks GRE2IFP(sc)->if_ipackets++; 146147256Sbrooks GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len; 147103026Ssobomax 148103026Ssobomax switch (proto) { 149103026Ssobomax case IPPROTO_GRE: 150123992Ssobomax hlen += sizeof(struct gre_h); 151103026Ssobomax 152103026Ssobomax /* process GRE flags as packet can be of variable len */ 153103026Ssobomax flags = ntohs(gip->gi_flags); 154103026Ssobomax 155103026Ssobomax /* Checksum & Offset are present */ 156103026Ssobomax if ((flags & GRE_CP) | (flags & GRE_RP)) 157103026Ssobomax hlen += 4; 158103026Ssobomax /* We don't support routing fields (variable length) */ 159103026Ssobomax if (flags & GRE_RP) 160158645Sglebius return (m); 161103026Ssobomax if (flags & GRE_KP) 162103026Ssobomax hlen += 4; 163103026Ssobomax if (flags & GRE_SP) 164123992Ssobomax hlen += 4; 165103026Ssobomax 166103026Ssobomax switch (ntohs(gip->gi_ptype)) { /* ethertypes */ 167125024Ssobomax case WCCP_PROTOCOL_TYPE: 168125024Ssobomax if (sc->wccp_ver == WCCP_V2) 169125024Ssobomax hlen += 4; 170125024Ssobomax /* FALLTHROUGH */ 171125024Ssobomax case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */ 172125024Ssobomax isr = NETISR_IP;/* as we are in ip_input */ 173148613Sbz af = AF_INET; 174103026Ssobomax break; 175148613Sbz#ifdef INET6 176148613Sbz case ETHERTYPE_IPV6: 177148613Sbz isr = NETISR_IPV6; 178148613Sbz af = AF_INET6; 179148613Sbz break; 180148613Sbz#endif 181103026Ssobomax#ifdef NETATALK 182103026Ssobomax case ETHERTYPE_ATALK: 183111888Sjlemon isr = NETISR_ATALK1; 184148613Sbz af = AF_APPLETALK; 185103026Ssobomax break; 186103026Ssobomax#endif 187158645Sglebius default: 188158645Sglebius /* Others not yet supported. */ 189158645Sglebius return (m); 190103026Ssobomax } 191103026Ssobomax break; 192103026Ssobomax default: 193158645Sglebius /* Others not yet supported. */ 194158645Sglebius return (m); 195103026Ssobomax } 196103026Ssobomax 197123992Ssobomax if (hlen > m->m_pkthdr.len) { 198123992Ssobomax m_freem(m); 199158645Sglebius return (NULL); 200123992Ssobomax } 201125226Ssobomax /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */ 202123992Ssobomax m_adj(m, hlen); 203103026Ssobomax 204159180Scsjp if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) { 205147256Sbrooks bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m); 206104366Ssobomax } 207103026Ssobomax 208250523Shrs if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) { 209250523Shrs m_freem(m); 210250523Shrs return(NULL); 211250523Shrs } 212250523Shrs 213147256Sbrooks m->m_pkthdr.rcvif = GRE2IFP(sc); 214103026Ssobomax 215180264Sgonzo netisr_queue(isr, m); 216103026Ssobomax 217158645Sglebius /* Packet is done, no further processing needed. */ 218158645Sglebius return (NULL); 219103026Ssobomax} 220103026Ssobomax 221103026Ssobomax/* 222103026Ssobomax * input routine for IPPRPOTO_MOBILE 223103026Ssobomax * This is a little bit diffrent from the other modes, as the 224103026Ssobomax * encapsulating header was not prepended, but instead inserted 225103026Ssobomax * between IP header and payload 226103026Ssobomax */ 227103026Ssobomax 228103026Ssobomaxvoid 229154625Sbzgre_mobile_input(struct mbuf *m, int hlen) 230103026Ssobomax{ 231123992Ssobomax struct ip *ip; 232123992Ssobomax struct mobip_h *mip; 233103026Ssobomax struct gre_softc *sc; 234103026Ssobomax int msiz; 235103026Ssobomax 236103026Ssobomax if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) { 237103026Ssobomax /* No matching tunnel or tunnel is down. */ 238103026Ssobomax m_freem(m); 239103026Ssobomax return; 240103026Ssobomax } 241103026Ssobomax 242123992Ssobomax if (m->m_len < sizeof(*mip)) { 243123992Ssobomax m = m_pullup(m, sizeof(*mip)); 244123992Ssobomax if (m == NULL) 245123992Ssobomax return; 246123992Ssobomax } 247123992Ssobomax ip = mtod(m, struct ip *); 248123992Ssobomax mip = mtod(m, struct mobip_h *); 249123992Ssobomax 250147256Sbrooks GRE2IFP(sc)->if_ipackets++; 251147256Sbrooks GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len; 252103026Ssobomax 253123992Ssobomax if (ntohs(mip->mh.proto) & MOB_H_SBIT) { 254103026Ssobomax msiz = MOB_H_SIZ_L; 255103026Ssobomax mip->mi.ip_src.s_addr = mip->mh.osrc; 256123992Ssobomax } else 257103026Ssobomax msiz = MOB_H_SIZ_S; 258123992Ssobomax 259123992Ssobomax if (m->m_len < (ip->ip_hl << 2) + msiz) { 260123992Ssobomax m = m_pullup(m, (ip->ip_hl << 2) + msiz); 261123992Ssobomax if (m == NULL) 262123992Ssobomax return; 263123992Ssobomax ip = mtod(m, struct ip *); 264123992Ssobomax mip = mtod(m, struct mobip_h *); 265103026Ssobomax } 266123992Ssobomax 267103026Ssobomax mip->mi.ip_dst.s_addr = mip->mh.odst; 268103026Ssobomax mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8); 269103026Ssobomax 270123992Ssobomax if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) { 271103026Ssobomax m_freem(m); 272103026Ssobomax return; 273103026Ssobomax } 274103026Ssobomax 275103026Ssobomax bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) + 276103026Ssobomax (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2)); 277103026Ssobomax m->m_len -= msiz; 278103026Ssobomax m->m_pkthdr.len -= msiz; 279103026Ssobomax 280103026Ssobomax /* 281103026Ssobomax * On FreeBSD, rip_input() supplies us with ip->ip_len 282241913Sglebius * decreased by the lengh of IP header, however, ip_input() 283241913Sglebius * expects it to be full size of IP packet, so adjust accordingly. 284103026Ssobomax */ 285241913Sglebius ip->ip_len = htons(ntohs(ip->ip_len) + sizeof(struct ip) - msiz); 286125020Ssobomax 287103026Ssobomax ip->ip_sum = 0; 288103026Ssobomax ip->ip_sum = in_cksum(m, (ip->ip_hl << 2)); 289103026Ssobomax 290159180Scsjp if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) { 291123922Ssam u_int32_t af = AF_INET; 292147256Sbrooks bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m); 293104366Ssobomax } 294103026Ssobomax 295250523Shrs if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) { 296250523Shrs m_freem(m); 297250523Shrs return; 298250523Shrs } 299250523Shrs 300147256Sbrooks m->m_pkthdr.rcvif = GRE2IFP(sc); 301103026Ssobomax 302180264Sgonzo netisr_queue(NETISR_IP, m); 303103026Ssobomax} 304103026Ssobomax 305103026Ssobomax/* 306103026Ssobomax * Find the gre interface associated with our src/dst/proto set. 307127307Srwatson * 308127307Srwatson * XXXRW: Need some sort of drain/refcount mechanism so that the softc 309127307Srwatson * reference remains valid after it's returned from gre_lookup(). Right 310127307Srwatson * now, I'm thinking it should be reference-counted with a gre_dropref() 311127307Srwatson * when the caller is done with the softc. This is complicated by how 312127307Srwatson * to handle destroying the gre softc; probably using a gre_drain() in 313127307Srwatson * in_gre.c during destroy. 314103026Ssobomax */ 315103032Ssobomaxstatic struct gre_softc * 316154665Srwatsongre_lookup(struct mbuf *m, u_int8_t proto) 317103026Ssobomax{ 318103026Ssobomax struct ip *ip = mtod(m, struct ip *); 319103026Ssobomax struct gre_softc *sc; 320103026Ssobomax 321127307Srwatson mtx_lock(&gre_mtx); 322103026Ssobomax for (sc = LIST_FIRST(&gre_softc_list); sc != NULL; 323103026Ssobomax sc = LIST_NEXT(sc, sc_list)) { 324103026Ssobomax if ((sc->g_dst.s_addr == ip->ip_src.s_addr) && 325103026Ssobomax (sc->g_src.s_addr == ip->ip_dst.s_addr) && 326103026Ssobomax (sc->g_proto == proto) && 327147256Sbrooks ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) { 328127307Srwatson mtx_unlock(&gre_mtx); 329103026Ssobomax return (sc); 330127307Srwatson } 331103026Ssobomax } 332127307Srwatson mtx_unlock(&gre_mtx); 333103026Ssobomax 334103026Ssobomax return (NULL); 335103026Ssobomax} 336