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 extern int gre_input(struct mbuf **, int *, int); 72 73 int in_gre_attach(struct gre_softc *); 74 int in_gre_output(struct mbuf *, int, int); 75 76 static const struct protosw in_gre_protosw = { 77 .pr_type = SOCK_RAW, 78 .pr_domain = &inetdomain, 79 .pr_protocol = IPPROTO_GRE, 80 .pr_flags = PR_ATOMIC|PR_ADDR, 81 .pr_input = gre_input, 82 .pr_output = rip_output, 83 .pr_ctlinput = rip_ctlinput, 84 .pr_ctloutput = rip_ctloutput, 85 .pr_usrreqs = &rip_usrreqs 86 }; 87 88 #define GRE_TTL 30 89 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL; 90 #define V_ip_gre_ttl VNET(ip_gre_ttl) 91 SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW, 92 &VNET_NAME(ip_gre_ttl), 0, ""); 93 94 static int 95 in_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 96 { 97 GRE_RLOCK_TRACKER; 98 struct gre_softc *sc; 99 struct ip *ip; 100 101 sc = (struct gre_softc *)arg; 102 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) 103 return (0); 104 105 M_ASSERTPKTHDR(m); 106 /* 107 * We expect that payload contains at least IPv4 108 * or IPv6 packet. 109 */ 110 if (m->m_pkthdr.len < sizeof(struct greip) + sizeof(struct ip)) 111 return (0); 112 113 GRE_RLOCK(sc); 114 if (sc->gre_family == 0) 115 goto bad; 116 117 KASSERT(sc->gre_family == AF_INET, 118 ("wrong gre_family: %d", sc->gre_family)); 119 120 ip = mtod(m, struct ip *); 121 if (sc->gre_oip.ip_src.s_addr != ip->ip_dst.s_addr || 122 sc->gre_oip.ip_dst.s_addr != ip->ip_src.s_addr) 123 goto bad; 124 125 GRE_RUNLOCK(sc); 126 return (32 * 2); 127 bad: 128 GRE_RUNLOCK(sc); 129 return (0); 130 } 131 132 int 133 in_gre_output(struct mbuf *m, int af, int hlen) 134 { 135 struct greip *gi; 136 137 gi = mtod(m, struct greip *); 138 switch (af) { 139 case AF_INET: 140 /* 141 * gre_transmit() has used M_PREPEND() that doesn't guarantee 142 * m_data is contiguous more than hlen bytes. Use m_copydata() 143 * here to avoid m_pullup(). 144 */ 145 m_copydata(m, hlen + offsetof(struct ip, ip_tos), 146 sizeof(u_char), &gi->gi_ip.ip_tos); 147 m_copydata(m, hlen + offsetof(struct ip, ip_id), 148 sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id); 149 break; 150 #ifdef INET6 151 case AF_INET6: 152 gi->gi_ip.ip_tos = 0; /* XXX */ 153 gi->gi_ip.ip_id = ip_newid(); 154 break; 155 #endif 156 } 157 gi->gi_ip.ip_ttl = V_ip_gre_ttl; 158 gi->gi_ip.ip_len = htons(m->m_pkthdr.len); 159 return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL)); 160 } 161 162 int 163 in_gre_attach(struct gre_softc *sc) 164 { 165 166 KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL")); 167 sc->gre_ecookie = encap_attach_func(AF_INET, IPPROTO_GRE, 168 in_gre_encapcheck, &in_gre_protosw, sc); 169 if (sc->gre_ecookie == NULL) 170 return (EEXIST); 171 return (0); 172 } 173