1c398230bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-NetBSD 3fe267a55SPedro F. Giffuni * 48e96e13eSMaxim Sobolev * Copyright (c) 1998 The NetBSD Foundation, Inc. 5f325335cSAndrey V. Elsukov * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org> 68e96e13eSMaxim Sobolev * All rights reserved. 78e96e13eSMaxim Sobolev * 88e96e13eSMaxim Sobolev * This code is derived from software contributed to The NetBSD Foundation 98e96e13eSMaxim Sobolev * by Heiko W.Rupp <hwr@pilhuhn.de> 108e96e13eSMaxim Sobolev * 119e669156SBjoern A. Zeeb * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> 129e669156SBjoern A. Zeeb * 138e96e13eSMaxim Sobolev * Redistribution and use in source and binary forms, with or without 148e96e13eSMaxim Sobolev * modification, are permitted provided that the following conditions 158e96e13eSMaxim Sobolev * are met: 168e96e13eSMaxim Sobolev * 1. Redistributions of source code must retain the above copyright 178e96e13eSMaxim Sobolev * notice, this list of conditions and the following disclaimer. 188e96e13eSMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright 198e96e13eSMaxim Sobolev * notice, this list of conditions and the following disclaimer in the 208e96e13eSMaxim Sobolev * documentation and/or other materials provided with the distribution. 218e96e13eSMaxim Sobolev * 228e96e13eSMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 238e96e13eSMaxim Sobolev * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 248e96e13eSMaxim Sobolev * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 258e96e13eSMaxim Sobolev * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 268e96e13eSMaxim Sobolev * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 278e96e13eSMaxim Sobolev * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 288e96e13eSMaxim Sobolev * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 298e96e13eSMaxim Sobolev * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 308e96e13eSMaxim Sobolev * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 318e96e13eSMaxim Sobolev * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 328e96e13eSMaxim Sobolev * POSSIBILITY OF SUCH DAMAGE. 33f325335cSAndrey V. Elsukov * 34f325335cSAndrey V. Elsukov * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ 358e96e13eSMaxim Sobolev */ 368e96e13eSMaxim Sobolev 374b421e2dSMike Silbersack #include <sys/cdefs.h> 384b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 394b421e2dSMike Silbersack 408e96e13eSMaxim Sobolev #include "opt_inet.h" 419e669156SBjoern A. Zeeb #include "opt_inet6.h" 428e96e13eSMaxim Sobolev 438e96e13eSMaxim Sobolev #include <sys/param.h> 448e96e13eSMaxim Sobolev #include <sys/systm.h> 458e96e13eSMaxim Sobolev #include <sys/mbuf.h> 468e96e13eSMaxim Sobolev #include <sys/socket.h> 478e96e13eSMaxim Sobolev #include <sys/socketvar.h> 488e96e13eSMaxim Sobolev #include <sys/errno.h> 498e96e13eSMaxim Sobolev #include <sys/time.h> 508e96e13eSMaxim Sobolev #include <sys/kernel.h> 51f325335cSAndrey V. Elsukov #include <sys/lock.h> 52f325335cSAndrey V. Elsukov #include <sys/rmlock.h> 53f325335cSAndrey V. Elsukov #include <sys/sysctl.h> 548e96e13eSMaxim Sobolev #include <net/ethernet.h> 558e96e13eSMaxim Sobolev #include <net/if.h> 5676039bc8SGleb Smirnoff #include <net/if_var.h> 57f325335cSAndrey V. Elsukov #include <net/vnet.h> 588e96e13eSMaxim Sobolev 598e96e13eSMaxim Sobolev #include <netinet/in.h> 608e96e13eSMaxim Sobolev #include <netinet/in_var.h> 618e96e13eSMaxim Sobolev #include <netinet/ip.h> 62f325335cSAndrey V. Elsukov #include <netinet/ip_encap.h> 638e96e13eSMaxim Sobolev #include <netinet/ip_var.h> 64f325335cSAndrey V. Elsukov 65f325335cSAndrey V. Elsukov #ifdef INET6 66f325335cSAndrey V. Elsukov #include <netinet/ip6.h> 678e96e13eSMaxim Sobolev #endif 688e96e13eSMaxim Sobolev 698e96e13eSMaxim Sobolev #include <net/if_gre.h> 708e96e13eSMaxim Sobolev 71f325335cSAndrey V. Elsukov #define GRE_TTL 30 72f325335cSAndrey V. Elsukov VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL; 73f325335cSAndrey V. Elsukov #define V_ip_gre_ttl VNET(ip_gre_ttl) 74f325335cSAndrey V. Elsukov SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW, 75*6d8fdfa9SAndrey V. Elsukov &VNET_NAME(ip_gre_ttl), 0, "Default TTL value for encapsulated packets"); 768e96e13eSMaxim Sobolev 77f325335cSAndrey V. Elsukov static int 78f325335cSAndrey V. Elsukov in_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 798e96e13eSMaxim Sobolev { 80f325335cSAndrey V. Elsukov GRE_RLOCK_TRACKER; 818e96e13eSMaxim Sobolev struct gre_softc *sc; 82f325335cSAndrey V. Elsukov struct ip *ip; 838e96e13eSMaxim Sobolev 84f325335cSAndrey V. Elsukov sc = (struct gre_softc *)arg; 85f325335cSAndrey V. Elsukov if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) 86f325335cSAndrey V. Elsukov return (0); 87f325335cSAndrey V. Elsukov 88f325335cSAndrey V. Elsukov M_ASSERTPKTHDR(m); 89f325335cSAndrey V. Elsukov 90f325335cSAndrey V. Elsukov GRE_RLOCK(sc); 91f325335cSAndrey V. Elsukov if (sc->gre_family == 0) 92f325335cSAndrey V. Elsukov goto bad; 93f325335cSAndrey V. Elsukov 94f325335cSAndrey V. Elsukov KASSERT(sc->gre_family == AF_INET, 95f325335cSAndrey V. Elsukov ("wrong gre_family: %d", sc->gre_family)); 96f325335cSAndrey V. Elsukov 97f325335cSAndrey V. Elsukov ip = mtod(m, struct ip *); 98f325335cSAndrey V. Elsukov if (sc->gre_oip.ip_src.s_addr != ip->ip_dst.s_addr || 99f325335cSAndrey V. Elsukov sc->gre_oip.ip_dst.s_addr != ip->ip_src.s_addr) 100f325335cSAndrey V. Elsukov goto bad; 101f325335cSAndrey V. Elsukov 102f325335cSAndrey V. Elsukov GRE_RUNLOCK(sc); 103*6d8fdfa9SAndrey V. Elsukov return (32 * 3); /* src + dst + gre_hdr */ 104f325335cSAndrey V. Elsukov bad: 105f325335cSAndrey V. Elsukov GRE_RUNLOCK(sc); 106f325335cSAndrey V. Elsukov return (0); 1078e96e13eSMaxim Sobolev } 1088e96e13eSMaxim Sobolev 109f325335cSAndrey V. Elsukov int 110f325335cSAndrey V. Elsukov in_gre_output(struct mbuf *m, int af, int hlen) 111f325335cSAndrey V. Elsukov { 112f325335cSAndrey V. Elsukov struct greip *gi; 11373d7ddbcSMaxim Sobolev 114f325335cSAndrey V. Elsukov gi = mtod(m, struct greip *); 115f325335cSAndrey V. Elsukov switch (af) { 116f325335cSAndrey V. Elsukov case AF_INET: 117f325335cSAndrey V. Elsukov /* 118f325335cSAndrey V. Elsukov * gre_transmit() has used M_PREPEND() that doesn't guarantee 119f325335cSAndrey V. Elsukov * m_data is contiguous more than hlen bytes. Use m_copydata() 120f325335cSAndrey V. Elsukov * here to avoid m_pullup(). 121f325335cSAndrey V. Elsukov */ 122f325335cSAndrey V. Elsukov m_copydata(m, hlen + offsetof(struct ip, ip_tos), 123f325335cSAndrey V. Elsukov sizeof(u_char), &gi->gi_ip.ip_tos); 124f325335cSAndrey V. Elsukov m_copydata(m, hlen + offsetof(struct ip, ip_id), 125f325335cSAndrey V. Elsukov sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id); 1268e96e13eSMaxim Sobolev break; 1279e669156SBjoern A. Zeeb #ifdef INET6 128f325335cSAndrey V. Elsukov case AF_INET6: 129f325335cSAndrey V. Elsukov gi->gi_ip.ip_tos = 0; /* XXX */ 1306d947416SGleb Smirnoff ip_fillid(&gi->gi_ip); 1319e669156SBjoern A. Zeeb break; 1329e669156SBjoern A. Zeeb #endif 1338e96e13eSMaxim Sobolev } 134f325335cSAndrey V. Elsukov gi->gi_ip.ip_ttl = V_ip_gre_ttl; 135f325335cSAndrey V. Elsukov gi->gi_ip.ip_len = htons(m->m_pkthdr.len); 136f325335cSAndrey V. Elsukov return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL)); 1378e96e13eSMaxim Sobolev } 1388e96e13eSMaxim Sobolev 139*6d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = { 140*6d8fdfa9SAndrey V. Elsukov .proto = IPPROTO_GRE, 141*6d8fdfa9SAndrey V. Elsukov .min_length = sizeof(struct greip) + sizeof(struct ip), 142*6d8fdfa9SAndrey V. Elsukov .exact_match = (sizeof(in_addr_t) << 4) + 32, 143*6d8fdfa9SAndrey V. Elsukov .check = in_gre_encapcheck, 144*6d8fdfa9SAndrey V. Elsukov .input = gre_input 145*6d8fdfa9SAndrey V. Elsukov }; 146*6d8fdfa9SAndrey V. Elsukov 1478f5a8818SKevin Lo int 148f325335cSAndrey V. Elsukov in_gre_attach(struct gre_softc *sc) 1498e96e13eSMaxim Sobolev { 1508e96e13eSMaxim Sobolev 151f325335cSAndrey V. Elsukov KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL")); 152*6d8fdfa9SAndrey V. Elsukov sc->gre_ecookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK); 153f325335cSAndrey V. Elsukov return (0); 1548e96e13eSMaxim Sobolev } 155