]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_gif.c
IFC @r273206
[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_gif.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip_encap.h>
63 #include <netinet/ip_ecn.h>
64
65 #ifdef INET6
66 #include <netinet/ip6.h>
67 #endif
68
69 #include <net/if_gif.h>
70
71 static int gif_validate4(const struct ip *, struct gif_softc *,
72         struct ifnet *);
73
74 extern  struct domain inetdomain;
75 struct protosw in_gif_protosw = {
76         .pr_type =              SOCK_RAW,
77         .pr_domain =            &inetdomain,
78         .pr_protocol =          0/* IPPROTO_IPV[46] */,
79         .pr_flags =             PR_ATOMIC|PR_ADDR,
80         .pr_input =             in_gif_input,
81         .pr_output =            rip_output,
82         .pr_ctloutput =         rip_ctloutput,
83         .pr_usrreqs =           &rip_usrreqs
84 };
85
86 VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
87 #define V_ip_gif_ttl            VNET(ip_gif_ttl)
88 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
89         &VNET_NAME(ip_gif_ttl), 0, "");
90
91 int
92 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
93 {
94         GIF_RLOCK_TRACKER;
95         struct gif_softc *sc = ifp->if_softc;
96         struct ip *ip;
97         int len;
98
99         /* prepend new IP header */
100         len = sizeof(struct ip);
101 #ifndef __NO_STRICT_ALIGNMENT
102         if (proto == IPPROTO_ETHERIP)
103                 len += ETHERIP_ALIGN;
104 #endif
105         M_PREPEND(m, len, M_NOWAIT);
106         if (m != NULL && m->m_len < len)
107                 m = m_pullup(m, len);
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 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  * validate outer address.
168  */
169 static int
170 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
171 {
172         struct in_ifaddr *ia4;
173
174         GIF_RLOCK_ASSERT(sc);
175
176         /* check for address match */
177         if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr ||
178             sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr)
179                 return (0);
180
181         /* martian filters on outer source - NOT done in ip_input! */
182         if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
183                 return (0);
184         switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
185         case 0:
186         case 127:
187         case 255:
188                 return (0);
189         }
190
191         /* reject packets with broadcast on source */
192         /* XXXRW: should use hash lists? */
193         IN_IFADDR_RLOCK();
194         TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
195                 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
196                         continue;
197                 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
198                         IN_IFADDR_RUNLOCK();
199                         return (0);
200                 }
201         }
202         IN_IFADDR_RUNLOCK();
203
204         /* ingress filters on outer source */
205         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
206                 struct sockaddr_in sin;
207                 struct rtentry *rt;
208
209                 bzero(&sin, sizeof(sin));
210                 sin.sin_family = AF_INET;
211                 sin.sin_len = sizeof(struct sockaddr_in);
212                 sin.sin_addr = ip->ip_src;
213                 /* XXX MRT  check for the interface we would use on output */
214                 rt = in_rtalloc1((struct sockaddr *)&sin, 0,
215                     0UL, sc->gif_fibnum);
216                 if (!rt || rt->rt_ifp != ifp) {
217                         if (rt)
218                                 RTFREE_LOCKED(rt);
219                         return (0);
220                 }
221                 RTFREE_LOCKED(rt);
222         }
223         return (32 * 2);
224 }
225
226 /*
227  * we know that we are in IFF_UP, outer address available, and outer family
228  * matched the physical addr family.  see gif_encapcheck().
229  */
230 int
231 in_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
232 {
233         struct ip ip;
234         struct gif_softc *sc;
235         struct ifnet *ifp;
236
237         /* sanity check done in caller */
238         sc = (struct gif_softc *)arg;
239         GIF_RLOCK_ASSERT(sc);
240
241         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
242         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
243         return (gif_validate4(&ip, sc, ifp));
244 }
245
246 int
247 in_gif_attach(struct gif_softc *sc)
248 {
249
250         KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
251         sc->gif_ecookie = encap_attach_func(AF_INET, -1, gif_encapcheck,
252             &in_gif_protosw, sc);
253         if (sc->gif_ecookie == NULL)
254                 return (EEXIST);
255         return (0);
256 }