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