]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_gif.c
Remove in_gif.h and in6_gif.h files. They only contain function
[FreeBSD/FreeBSD.git] / sys / netinet / in_gif.c
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *      $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/param.h>
39 #include <sys/lock.h>
40 #include <sys/rmlock.h>
41 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/mbuf.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>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/route.h>
54 #include <net/vnet.h>
55
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
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #endif
67
68 #include <net/if_gif.h>
69
70 int in_gif_output(struct ifnet *, struct mbuf *, int, uint8_t);
71 int in_gif_encapcheck(const struct mbuf *, int, int, void *);
72 int in_gif_attach(struct gif_softc *);
73
74 static int gif_validate4(const struct ip *, struct gif_softc *,
75         struct ifnet *);
76 static int in_gif_input(struct mbuf **, int *, int);
77
78 extern  struct domain inetdomain;
79 static struct protosw in_gif_protosw = {
80         .pr_type =              SOCK_RAW,
81         .pr_domain =            &inetdomain,
82         .pr_protocol =          0/* IPPROTO_IPV[46] */,
83         .pr_flags =             PR_ATOMIC|PR_ADDR,
84         .pr_input =             in_gif_input,
85         .pr_output =            rip_output,
86         .pr_ctloutput =         rip_ctloutput,
87         .pr_usrreqs =           &rip_usrreqs
88 };
89
90 #define GIF_TTL         30
91 static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
92 #define V_ip_gif_ttl            VNET(ip_gif_ttl)
93 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
94         &VNET_NAME(ip_gif_ttl), 0, "");
95
96 int
97 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
98 {
99         GIF_RLOCK_TRACKER;
100         struct gif_softc *sc = ifp->if_softc;
101         struct ip *ip;
102         int len;
103
104         /* prepend new IP header */
105         len = sizeof(struct ip);
106 #ifndef __NO_STRICT_ALIGNMENT
107         if (proto == IPPROTO_ETHERIP)
108                 len += ETHERIP_ALIGN;
109 #endif
110         M_PREPEND(m, len, M_NOWAIT);
111         if (m == NULL)
112                 return (ENOBUFS);
113 #ifndef __NO_STRICT_ALIGNMENT
114         if (proto == IPPROTO_ETHERIP) {
115                 len = mtod(m, vm_offset_t) & 3;
116                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
117                     ("in_gif_output: unexpected misalignment"));
118                 m->m_data += len;
119                 m->m_len -= ETHERIP_ALIGN;
120         }
121 #endif
122         ip = mtod(m, struct ip *);
123         GIF_RLOCK(sc);
124         if (sc->gif_family != AF_INET) {
125                 m_freem(m);
126                 GIF_RUNLOCK(sc);
127                 return (ENETDOWN);
128         }
129         bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
130         GIF_RUNLOCK(sc);
131
132         ip->ip_p = proto;
133         /* version will be set in ip_output() */
134         ip->ip_ttl = V_ip_gif_ttl;
135         ip->ip_len = htons(m->m_pkthdr.len);
136         ip->ip_tos = ecn;
137
138         return (ip_output(m, NULL, NULL, 0, NULL, NULL));
139 }
140
141 static int
142 in_gif_input(struct mbuf **mp, int *offp, int proto)
143 {
144         struct mbuf *m = *mp;
145         struct gif_softc *sc;
146         struct ifnet *gifp;
147         struct ip *ip;
148         uint8_t ecn;
149
150         sc = encap_getarg(m);
151         if (sc == NULL) {
152                 m_freem(m);
153                 KMOD_IPSTAT_INC(ips_nogif);
154                 return (IPPROTO_DONE);
155         }
156         gifp = GIF2IFP(sc);
157         if ((gifp->if_flags & IFF_UP) != 0) {
158                 ip = mtod(m, struct ip *);
159                 ecn = ip->ip_tos;
160                 m_adj(m, *offp);
161                 gif_input(m, gifp, proto, ecn);
162         } else {
163                 m_freem(m);
164                 KMOD_IPSTAT_INC(ips_nogif);
165         }
166         return (IPPROTO_DONE);
167 }
168
169 /*
170  * validate outer address.
171  */
172 static int
173 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
174 {
175
176         GIF_RLOCK_ASSERT(sc);
177
178         /* check for address match */
179         if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr ||
180             sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr)
181                 return (0);
182
183         /* martian filters on outer source - NOT done in ip_input! */
184         if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
185                 return (0);
186         switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
187         case 0:
188         case 127:
189         case 255:
190                 return (0);
191         }
192
193         /* ingress filters on outer source */
194         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
195                 struct sockaddr_in sin;
196                 struct rtentry *rt;
197
198                 bzero(&sin, sizeof(sin));
199                 sin.sin_family = AF_INET;
200                 sin.sin_len = sizeof(struct sockaddr_in);
201                 sin.sin_addr = ip->ip_src;
202                 /* XXX MRT  check for the interface we would use on output */
203                 rt = in_rtalloc1((struct sockaddr *)&sin, 0,
204                     0UL, sc->gif_fibnum);
205                 if (!rt || rt->rt_ifp != ifp) {
206                         if (rt)
207                                 RTFREE_LOCKED(rt);
208                         return (0);
209                 }
210                 RTFREE_LOCKED(rt);
211         }
212         return (32 * 2);
213 }
214
215 /*
216  * we know that we are in IFF_UP, outer address available, and outer family
217  * matched the physical addr family.  see gif_encapcheck().
218  */
219 int
220 in_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
221 {
222         struct ip ip;
223         struct gif_softc *sc;
224         struct ifnet *ifp;
225
226         /* sanity check done in caller */
227         sc = (struct gif_softc *)arg;
228         GIF_RLOCK_ASSERT(sc);
229
230         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
231         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
232         return (gif_validate4(&ip, sc, ifp));
233 }
234
235 int
236 in_gif_attach(struct gif_softc *sc)
237 {
238
239         KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
240         sc->gif_ecookie = encap_attach_func(AF_INET, -1, gif_encapcheck,
241             &in_gif_protosw, sc);
242         if (sc->gif_ecookie == NULL)
243                 return (EEXIST);
244         return (0);
245 }