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