1 /* $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Heiko W.Rupp <hwr@pilhuhn.de> 9 * 10 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * deencapsulate tunneled packets and send them on 36 * output half is in net/if_gre.[ch] 37 * This currently handles IPPROTO_GRE, IPPROTO_MOBILE 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include "opt_inet.h" 44 #include "opt_inet6.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/protosw.h> 52 #include <sys/errno.h> 53 #include <sys/time.h> 54 #include <sys/kernel.h> 55 #include <sys/syslog.h> 56 #include <net/bpf.h> 57 #include <net/ethernet.h> 58 #include <net/if.h> 59 #include <net/if_var.h> 60 #include <net/netisr.h> 61 #include <net/route.h> 62 #include <net/raw_cb.h> 63 64 #ifdef INET 65 #include <netinet/in.h> 66 #include <netinet/in_var.h> 67 #include <netinet/in_systm.h> 68 #include <netinet/ip.h> 69 #include <netinet/ip_var.h> 70 #include <netinet/ip_gre.h> 71 #include <machine/in_cksum.h> 72 #else 73 #error "ip_gre requires INET" 74 #endif 75 76 /* Needs IP headers. */ 77 #include <net/if_gre.h> 78 79 #include <machine/stdarg.h> 80 81 #if 1 82 void gre_inet_ntoa(struct in_addr in); /* XXX */ 83 #endif 84 85 static struct gre_softc *gre_lookup(struct mbuf *, u_int8_t); 86 87 static struct mbuf *gre_input2(struct mbuf *, int, u_char); 88 89 /* 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 94 */ 95 void 96 gre_input(struct mbuf *m, int off) 97 { 98 int proto; 99 100 proto = (mtod(m, struct ip *))->ip_p; 101 102 m = gre_input2(m, off, proto); 103 104 /* 105 * If no matching tunnel that is up is found. We inject 106 * the mbuf to raw ip socket to see if anyone picks it up. 107 */ 108 if (m != NULL) 109 rip_input(m, off); 110 } 111 112 /* 113 * Decapsulate. Does the real work and is called from gre_input() 114 * (above). Returns an mbuf back if packet is not yet processed, 115 * and NULL if it needs no further processing. proto is the protocol 116 * number of the "calling" foo_input() routine. 117 */ 118 static struct mbuf * 119 gre_input2(struct mbuf *m ,int hlen, u_char proto) 120 { 121 struct greip *gip; 122 int isr; 123 struct gre_softc *sc; 124 u_int16_t flags; 125 u_int32_t af; 126 127 if ((sc = gre_lookup(m, proto)) == NULL) { 128 /* No matching tunnel or tunnel is down. */ 129 return (m); 130 } 131 132 if (m->m_len < sizeof(*gip)) { 133 m = m_pullup(m, sizeof(*gip)); 134 if (m == NULL) 135 return (NULL); 136 } 137 gip = mtod(m, struct greip *); 138 139 GRE2IFP(sc)->if_ipackets++; 140 GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len; 141 142 switch (proto) { 143 case IPPROTO_GRE: 144 hlen += sizeof(struct gre_h); 145 146 /* process GRE flags as packet can be of variable len */ 147 flags = ntohs(gip->gi_flags); 148 149 /* Checksum & Offset are present */ 150 if ((flags & GRE_CP) | (flags & GRE_RP)) 151 hlen += 4; 152 /* We don't support routing fields (variable length) */ 153 if (flags & GRE_RP) 154 return (m); 155 if (flags & GRE_KP) 156 hlen += 4; 157 if (flags & GRE_SP) 158 hlen += 4; 159 160 switch (ntohs(gip->gi_ptype)) { /* ethertypes */ 161 case WCCP_PROTOCOL_TYPE: 162 if (sc->wccp_ver == WCCP_V2) 163 hlen += 4; 164 /* FALLTHROUGH */ 165 case ETHERTYPE_IP: /* shouldn't need a schednetisr(), */ 166 isr = NETISR_IP;/* as we are in ip_input */ 167 af = AF_INET; 168 break; 169 #ifdef INET6 170 case ETHERTYPE_IPV6: 171 isr = NETISR_IPV6; 172 af = AF_INET6; 173 break; 174 #endif 175 default: 176 /* Others not yet supported. */ 177 return (m); 178 } 179 break; 180 default: 181 /* Others not yet supported. */ 182 return (m); 183 } 184 185 if (hlen > m->m_pkthdr.len) { 186 m_freem(m); 187 return (NULL); 188 } 189 /* Unlike NetBSD, in FreeBSD m_adj() adjusts m->m_pkthdr.len as well */ 190 m_adj(m, hlen); 191 192 if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) { 193 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m); 194 } 195 196 if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) { 197 m_freem(m); 198 return(NULL); 199 } 200 201 m->m_pkthdr.rcvif = GRE2IFP(sc); 202 203 netisr_queue(isr, m); 204 205 /* Packet is done, no further processing needed. */ 206 return (NULL); 207 } 208 209 /* 210 * input routine for IPPRPOTO_MOBILE 211 * This is a little bit diffrent from the other modes, as the 212 * encapsulating header was not prepended, but instead inserted 213 * between IP header and payload 214 */ 215 216 void 217 gre_mobile_input(struct mbuf *m, int hlen) 218 { 219 struct ip *ip; 220 struct mobip_h *mip; 221 struct gre_softc *sc; 222 int msiz; 223 224 if ((sc = gre_lookup(m, IPPROTO_MOBILE)) == NULL) { 225 /* No matching tunnel or tunnel is down. */ 226 m_freem(m); 227 return; 228 } 229 230 if (m->m_len < sizeof(*mip)) { 231 m = m_pullup(m, sizeof(*mip)); 232 if (m == NULL) 233 return; 234 } 235 ip = mtod(m, struct ip *); 236 mip = mtod(m, struct mobip_h *); 237 238 GRE2IFP(sc)->if_ipackets++; 239 GRE2IFP(sc)->if_ibytes += m->m_pkthdr.len; 240 241 if (ntohs(mip->mh.proto) & MOB_H_SBIT) { 242 msiz = MOB_H_SIZ_L; 243 mip->mi.ip_src.s_addr = mip->mh.osrc; 244 } else 245 msiz = MOB_H_SIZ_S; 246 247 if (m->m_len < (ip->ip_hl << 2) + msiz) { 248 m = m_pullup(m, (ip->ip_hl << 2) + msiz); 249 if (m == NULL) 250 return; 251 ip = mtod(m, struct ip *); 252 mip = mtod(m, struct mobip_h *); 253 } 254 255 mip->mi.ip_dst.s_addr = mip->mh.odst; 256 mip->mi.ip_p = (ntohs(mip->mh.proto) >> 8); 257 258 if (gre_in_cksum((u_int16_t *)&mip->mh, msiz) != 0) { 259 m_freem(m); 260 return; 261 } 262 263 bcopy((caddr_t)(ip) + (ip->ip_hl << 2) + msiz, (caddr_t)(ip) + 264 (ip->ip_hl << 2), m->m_len - msiz - (ip->ip_hl << 2)); 265 m->m_len -= msiz; 266 m->m_pkthdr.len -= msiz; 267 268 /* 269 * On FreeBSD, rip_input() supplies us with ip->ip_len 270 * decreased by the lengh of IP header, however, ip_input() 271 * expects it to be full size of IP packet, so adjust accordingly. 272 */ 273 ip->ip_len = htons(ntohs(ip->ip_len) + sizeof(struct ip) - msiz); 274 275 ip->ip_sum = 0; 276 ip->ip_sum = in_cksum(m, (ip->ip_hl << 2)); 277 278 if (bpf_peers_present(GRE2IFP(sc)->if_bpf)) { 279 u_int32_t af = AF_INET; 280 bpf_mtap2(GRE2IFP(sc)->if_bpf, &af, sizeof(af), m); 281 } 282 283 if ((GRE2IFP(sc)->if_flags & IFF_MONITOR) != 0) { 284 m_freem(m); 285 return; 286 } 287 288 m->m_pkthdr.rcvif = GRE2IFP(sc); 289 290 netisr_queue(NETISR_IP, m); 291 } 292 293 /* 294 * Find the gre interface associated with our src/dst/proto set. 295 * 296 * XXXRW: Need some sort of drain/refcount mechanism so that the softc 297 * reference remains valid after it's returned from gre_lookup(). Right 298 * now, I'm thinking it should be reference-counted with a gre_dropref() 299 * when the caller is done with the softc. This is complicated by how 300 * to handle destroying the gre softc; probably using a gre_drain() in 301 * in_gre.c during destroy. 302 */ 303 static struct gre_softc * 304 gre_lookup(struct mbuf *m, u_int8_t proto) 305 { 306 struct ip *ip = mtod(m, struct ip *); 307 struct gre_softc *sc; 308 309 mtx_lock(&gre_mtx); 310 for (sc = LIST_FIRST(&gre_softc_list); sc != NULL; 311 sc = LIST_NEXT(sc, sc_list)) { 312 if ((sc->g_dst.s_addr == ip->ip_src.s_addr) && 313 (sc->g_src.s_addr == ip->ip_dst.s_addr) && 314 (sc->g_proto == proto) && 315 ((GRE2IFP(sc)->if_flags & IFF_UP) != 0)) { 316 mtx_unlock(&gre_mtx); 317 return (sc); 318 } 319 } 320 mtx_unlock(&gre_mtx); 321 322 return (NULL); 323 } 324