1 /*- 2 * Copyright (c) 1998 The NetBSD Foundation, Inc. 3 * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org> 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Heiko W.Rupp <hwr@pilhuhn.de> 8 * 9 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $ 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/protosw.h> 47 #include <sys/errno.h> 48 #include <sys/time.h> 49 #include <sys/kernel.h> 50 #include <sys/lock.h> 51 #include <sys/rmlock.h> 52 #include <sys/sysctl.h> 53 #include <net/ethernet.h> 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <net/vnet.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip.h> 61 #include <netinet/ip_encap.h> 62 #include <netinet/ip_var.h> 63 64 #ifdef INET6 65 #include <netinet/ip6.h> 66 #endif 67 68 #include <net/if_gre.h> 69 70 extern struct domain inetdomain; 71 static const struct protosw in_gre_protosw = { 72 .pr_type = SOCK_RAW, 73 .pr_domain = &inetdomain, 74 .pr_protocol = IPPROTO_GRE, 75 .pr_flags = PR_ATOMIC|PR_ADDR, 76 .pr_input = gre_input, 77 .pr_output = rip_output, 78 .pr_ctlinput = rip_ctlinput, 79 .pr_ctloutput = rip_ctloutput, 80 .pr_usrreqs = &rip_usrreqs 81 }; 82 83 #define GRE_TTL 30 84 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL; 85 #define V_ip_gre_ttl VNET(ip_gre_ttl) 86 SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW, 87 &VNET_NAME(ip_gre_ttl), 0, ""); 88 89 static int 90 in_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 91 { 92 GRE_RLOCK_TRACKER; 93 struct gre_softc *sc; 94 struct ip *ip; 95 96 sc = (struct gre_softc *)arg; 97 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) 98 return (0); 99 100 M_ASSERTPKTHDR(m); 101 /* 102 * We expect that payload contains at least IPv4 103 * or IPv6 packet. 104 */ 105 if (m->m_pkthdr.len < sizeof(struct greip) + sizeof(struct ip)) 106 return (0); 107 108 GRE_RLOCK(sc); 109 if (sc->gre_family == 0) 110 goto bad; 111 112 KASSERT(sc->gre_family == AF_INET, 113 ("wrong gre_family: %d", sc->gre_family)); 114 115 ip = mtod(m, struct ip *); 116 if (sc->gre_oip.ip_src.s_addr != ip->ip_dst.s_addr || 117 sc->gre_oip.ip_dst.s_addr != ip->ip_src.s_addr) 118 goto bad; 119 120 GRE_RUNLOCK(sc); 121 return (32 * 2); 122 bad: 123 GRE_RUNLOCK(sc); 124 return (0); 125 } 126 127 int 128 in_gre_output(struct mbuf *m, int af, int hlen) 129 { 130 struct greip *gi; 131 132 gi = mtod(m, struct greip *); 133 switch (af) { 134 case AF_INET: 135 /* 136 * gre_transmit() has used M_PREPEND() that doesn't guarantee 137 * m_data is contiguous more than hlen bytes. Use m_copydata() 138 * here to avoid m_pullup(). 139 */ 140 m_copydata(m, hlen + offsetof(struct ip, ip_tos), 141 sizeof(u_char), &gi->gi_ip.ip_tos); 142 m_copydata(m, hlen + offsetof(struct ip, ip_id), 143 sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id); 144 break; 145 #ifdef INET6 146 case AF_INET6: 147 gi->gi_ip.ip_tos = 0; /* XXX */ 148 ip_fillid(&gi->gi_ip); 149 break; 150 #endif 151 } 152 gi->gi_ip.ip_ttl = V_ip_gre_ttl; 153 gi->gi_ip.ip_len = htons(m->m_pkthdr.len); 154 return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL)); 155 } 156 157 int 158 in_gre_attach(struct gre_softc *sc) 159 { 160 161 KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL")); 162 sc->gre_ecookie = encap_attach_func(AF_INET, IPPROTO_GRE, 163 in_gre_encapcheck, &in_gre_protosw, sc); 164 if (sc->gre_ecookie == NULL) 165 return (EEXIST); 166 return (0); 167 } 168