2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
40 #include <sys/rmlock.h>
41 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/protosw.h>
49 #include <sys/malloc.h>
52 #include <net/if_var.h>
53 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_encap.h>
62 #include <netinet/ip_ecn.h>
63 #include <netinet/in_fib.h>
66 #include <netinet/ip6.h>
69 #include <net/if_gif.h>
71 static int in_gif_input(struct mbuf **, int *, int);
73 extern struct domain inetdomain;
74 static struct protosw in_gif_protosw = {
76 .pr_domain = &inetdomain,
77 .pr_protocol = 0/* IPPROTO_IPV[46] */,
78 .pr_flags = PR_ATOMIC|PR_ADDR,
79 .pr_input = in_gif_input,
80 .pr_output = rip_output,
81 .pr_ctloutput = rip_ctloutput,
82 .pr_usrreqs = &rip_usrreqs
86 static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
87 #define V_ip_gif_ttl VNET(ip_gif_ttl)
88 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
89 &VNET_NAME(ip_gif_ttl), 0, "");
92 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
95 struct gif_softc *sc = ifp->if_softc;
99 /* prepend new IP header */
100 len = sizeof(struct ip);
101 #ifndef __NO_STRICT_ALIGNMENT
102 if (proto == IPPROTO_ETHERIP)
103 len += ETHERIP_ALIGN;
105 M_PREPEND(m, len, M_NOWAIT);
108 #ifndef __NO_STRICT_ALIGNMENT
109 if (proto == IPPROTO_ETHERIP) {
110 len = mtod(m, vm_offset_t) & 3;
111 KASSERT(len == 0 || len == ETHERIP_ALIGN,
112 ("in_gif_output: unexpected misalignment"));
114 m->m_len -= ETHERIP_ALIGN;
117 ip = mtod(m, struct ip *);
119 if (sc->gif_family != AF_INET) {
124 bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
128 /* version will be set in ip_output() */
129 ip->ip_ttl = V_ip_gif_ttl;
130 ip->ip_len = htons(m->m_pkthdr.len);
133 return (ip_output(m, NULL, NULL, 0, NULL, NULL));
137 in_gif_input(struct mbuf **mp, int *offp, int proto)
139 struct mbuf *m = *mp;
140 struct gif_softc *sc;
145 sc = encap_getarg(m);
148 KMOD_IPSTAT_INC(ips_nogif);
149 return (IPPROTO_DONE);
152 if ((gifp->if_flags & IFF_UP) != 0) {
153 ip = mtod(m, struct ip *);
156 gif_input(m, gifp, proto, ecn);
159 KMOD_IPSTAT_INC(ips_nogif);
161 return (IPPROTO_DONE);
165 * we know that we are in IFF_UP, outer address available, and outer family
166 * matched the physical addr family. see gif_encapcheck().
169 in_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
172 struct gif_softc *sc;
175 /* sanity check done in caller */
176 sc = (struct gif_softc *)arg;
177 GIF_RLOCK_ASSERT(sc);
179 /* check for address match */
180 ip = mtod(m, const struct ip *);
181 if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr)
184 if (sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr) {
185 if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
190 /* ingress filters on outer source */
191 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
192 struct nhop4_basic nh4;
197 if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0)
200 if (nh4.nh_ifp != m->m_pkthdr.rcvif)
207 in_gif_attach(struct gif_softc *sc)
210 KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
211 sc->gif_ecookie = encap_attach_func(AF_INET, -1, gif_encapcheck,
212 &in_gif_protosw, sc);
213 if (sc->gif_ecookie == NULL)