1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 40 #include <sys/param.h> 41 #include <sys/lock.h> 42 #include <sys/rmlock.h> 43 #include <sys/systm.h> 44 #include <sys/socket.h> 45 #include <sys/sockio.h> 46 #include <sys/mbuf.h> 47 #include <sys/errno.h> 48 #include <sys/kernel.h> 49 #include <sys/sysctl.h> 50 #include <sys/protosw.h> 51 #include <sys/malloc.h> 52 53 #include <net/if.h> 54 #include <net/if_var.h> 55 #include <net/route.h> 56 #include <net/vnet.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/ip.h> 61 #include <netinet/ip_var.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip_encap.h> 64 #include <netinet/ip_ecn.h> 65 #include <netinet/in_fib.h> 66 67 #ifdef INET6 68 #include <netinet/ip6.h> 69 #endif 70 71 #include <net/if_gif.h> 72 73 static int in_gif_input(struct mbuf **, int *, int); 74 75 extern struct domain inetdomain; 76 static struct protosw in_gif_protosw = { 77 .pr_type = SOCK_RAW, 78 .pr_domain = &inetdomain, 79 .pr_protocol = 0/* IPPROTO_IPV[46] */, 80 .pr_flags = PR_ATOMIC|PR_ADDR, 81 .pr_input = in_gif_input, 82 .pr_output = rip_output, 83 .pr_ctloutput = rip_ctloutput, 84 .pr_usrreqs = &rip_usrreqs 85 }; 86 87 #define GIF_TTL 30 88 static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL; 89 #define V_ip_gif_ttl VNET(ip_gif_ttl) 90 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW, 91 &VNET_NAME(ip_gif_ttl), 0, ""); 92 93 int 94 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn) 95 { 96 GIF_RLOCK_TRACKER; 97 struct gif_softc *sc = ifp->if_softc; 98 struct ip *ip; 99 int len; 100 101 /* prepend new IP header */ 102 len = sizeof(struct ip); 103 #ifndef __NO_STRICT_ALIGNMENT 104 if (proto == IPPROTO_ETHERIP) 105 len += ETHERIP_ALIGN; 106 #endif 107 M_PREPEND(m, len, M_NOWAIT); 108 if (m == NULL) 109 return (ENOBUFS); 110 #ifndef __NO_STRICT_ALIGNMENT 111 if (proto == IPPROTO_ETHERIP) { 112 len = mtod(m, vm_offset_t) & 3; 113 KASSERT(len == 0 || len == ETHERIP_ALIGN, 114 ("in_gif_output: unexpected misalignment")); 115 m->m_data += len; 116 m->m_len -= ETHERIP_ALIGN; 117 } 118 #endif 119 ip = mtod(m, struct ip *); 120 GIF_RLOCK(sc); 121 if (sc->gif_family != AF_INET) { 122 m_freem(m); 123 GIF_RUNLOCK(sc); 124 return (ENETDOWN); 125 } 126 bcopy(sc->gif_iphdr, ip, sizeof(struct ip)); 127 GIF_RUNLOCK(sc); 128 129 ip->ip_p = proto; 130 /* version will be set in ip_output() */ 131 ip->ip_ttl = V_ip_gif_ttl; 132 ip->ip_len = htons(m->m_pkthdr.len); 133 ip->ip_tos = ecn; 134 135 return (ip_output(m, NULL, NULL, 0, NULL, NULL)); 136 } 137 138 static int 139 in_gif_input(struct mbuf **mp, int *offp, int proto) 140 { 141 struct mbuf *m = *mp; 142 struct gif_softc *sc; 143 struct ifnet *gifp; 144 struct ip *ip; 145 uint8_t ecn; 146 147 sc = encap_getarg(m); 148 if (sc == NULL) { 149 m_freem(m); 150 KMOD_IPSTAT_INC(ips_nogif); 151 return (IPPROTO_DONE); 152 } 153 gifp = GIF2IFP(sc); 154 if ((gifp->if_flags & IFF_UP) != 0) { 155 ip = mtod(m, struct ip *); 156 ecn = ip->ip_tos; 157 m_adj(m, *offp); 158 gif_input(m, gifp, proto, ecn); 159 } else { 160 m_freem(m); 161 KMOD_IPSTAT_INC(ips_nogif); 162 } 163 return (IPPROTO_DONE); 164 } 165 166 /* 167 * we know that we are in IFF_UP, outer address available, and outer family 168 * matched the physical addr family. see gif_encapcheck(). 169 */ 170 int 171 in_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 172 { 173 const struct ip *ip; 174 struct gif_softc *sc; 175 int ret; 176 177 /* sanity check done in caller */ 178 sc = (struct gif_softc *)arg; 179 GIF_RLOCK_ASSERT(sc); 180 181 /* check for address match */ 182 ip = mtod(m, const struct ip *); 183 if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr) 184 return (0); 185 ret = 32; 186 if (sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr) { 187 if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0) 188 return (0); 189 } else 190 ret += 32; 191 192 /* ingress filters on outer source */ 193 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) { 194 struct nhop4_basic nh4; 195 struct in_addr dst; 196 197 dst = ip->ip_src; 198 199 if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0) 200 return (0); 201 202 if (nh4.nh_ifp != m->m_pkthdr.rcvif) 203 return (0); 204 } 205 return (ret); 206 } 207 208 int 209 in_gif_attach(struct gif_softc *sc) 210 { 211 212 KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL")); 213 sc->gif_ecookie = encap_attach_func(AF_INET, -1, gif_encapcheck, 214 &in_gif_protosw, sc); 215 if (sc->gif_ecookie == NULL) 216 return (EEXIST); 217 return (0); 218 } 219