Deleted Added
full compact
in_gif.c (185571) in_gif.c (189494)
1/* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
2
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
32#include <sys/cdefs.h>
1/* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
2
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
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet/in_gif.c 185571 2008-12-02 21:37:28Z bz $");
33__FBSDID("$FreeBSD: head/sys/netinet/in_gif.c 189494 2009-03-07 19:08:58Z marius $");
34
35#include "opt_mrouting.h"
36#include "opt_inet.h"
37#include "opt_inet6.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/socket.h>
42#include <sys/sockio.h>
43#include <sys/mbuf.h>
44#include <sys/errno.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/protosw.h>
48#include <sys/malloc.h>
49#include <sys/vimage.h>
50
51#include <net/if.h>
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/ip.h>
57#include <netinet/ip_var.h>
58#include <netinet/in_gif.h>
59#include <netinet/in_var.h>
60#include <netinet/ip_encap.h>
61#include <netinet/ip_ecn.h>
62#include <netinet/vinet.h>
63
64#ifdef INET6
65#include <netinet/ip6.h>
66#endif
67
68#ifdef MROUTING
69#include <netinet/ip_mroute.h>
70#endif /* MROUTING */
71
72#include <net/if_gif.h>
73
74static int gif_validate4(const struct ip *, struct gif_softc *,
75 struct ifnet *);
76
77extern struct domain inetdomain;
78struct protosw in_gif_protosw = {
79 .pr_type = SOCK_RAW,
80 .pr_domain = &inetdomain,
81 .pr_protocol = 0/* IPPROTO_IPV[46] */,
82 .pr_flags = PR_ATOMIC|PR_ADDR,
83 .pr_input = in_gif_input,
84 .pr_output = (pr_output_t*)rip_output,
85 .pr_ctloutput = rip_ctloutput,
86 .pr_usrreqs = &rip_usrreqs
87};
88
89#ifdef VIMAGE_GLOBALS
90extern int ip_gif_ttl;
91#endif
92SYSCTL_V_INT(V_NET, vnet_gif, _net_inet_ip, IPCTL_GIF_TTL, gifttl,
93 CTLFLAG_RW, ip_gif_ttl, 0, "");
94
95int
96in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
97{
98 INIT_VNET_GIF(ifp->if_vnet);
99 struct gif_softc *sc = ifp->if_softc;
100 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
101 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
102 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
103 struct ip iphdr; /* capsule IP header, host byte ordered */
104 struct etherip_header eiphdr;
34
35#include "opt_mrouting.h"
36#include "opt_inet.h"
37#include "opt_inet6.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/socket.h>
42#include <sys/sockio.h>
43#include <sys/mbuf.h>
44#include <sys/errno.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/protosw.h>
48#include <sys/malloc.h>
49#include <sys/vimage.h>
50
51#include <net/if.h>
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/ip.h>
57#include <netinet/ip_var.h>
58#include <netinet/in_gif.h>
59#include <netinet/in_var.h>
60#include <netinet/ip_encap.h>
61#include <netinet/ip_ecn.h>
62#include <netinet/vinet.h>
63
64#ifdef INET6
65#include <netinet/ip6.h>
66#endif
67
68#ifdef MROUTING
69#include <netinet/ip_mroute.h>
70#endif /* MROUTING */
71
72#include <net/if_gif.h>
73
74static int gif_validate4(const struct ip *, struct gif_softc *,
75 struct ifnet *);
76
77extern struct domain inetdomain;
78struct protosw in_gif_protosw = {
79 .pr_type = SOCK_RAW,
80 .pr_domain = &inetdomain,
81 .pr_protocol = 0/* IPPROTO_IPV[46] */,
82 .pr_flags = PR_ATOMIC|PR_ADDR,
83 .pr_input = in_gif_input,
84 .pr_output = (pr_output_t*)rip_output,
85 .pr_ctloutput = rip_ctloutput,
86 .pr_usrreqs = &rip_usrreqs
87};
88
89#ifdef VIMAGE_GLOBALS
90extern int ip_gif_ttl;
91#endif
92SYSCTL_V_INT(V_NET, vnet_gif, _net_inet_ip, IPCTL_GIF_TTL, gifttl,
93 CTLFLAG_RW, ip_gif_ttl, 0, "");
94
95int
96in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
97{
98 INIT_VNET_GIF(ifp->if_vnet);
99 struct gif_softc *sc = ifp->if_softc;
100 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
101 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
102 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
103 struct ip iphdr; /* capsule IP header, host byte ordered */
104 struct etherip_header eiphdr;
105 int proto, error;
105 int error, len, proto;
106 u_int8_t tos;
107
108 GIF_LOCK_ASSERT(sc);
109
110 if (sin_src == NULL || sin_dst == NULL ||
111 sin_src->sin_family != AF_INET ||
112 sin_dst->sin_family != AF_INET) {
113 m_freem(m);
114 return EAFNOSUPPORT;
115 }
116
117 switch (family) {
118#ifdef INET
119 case AF_INET:
120 {
121 struct ip *ip;
122
123 proto = IPPROTO_IPV4;
124 if (m->m_len < sizeof(*ip)) {
125 m = m_pullup(m, sizeof(*ip));
126 if (!m)
127 return ENOBUFS;
128 }
129 ip = mtod(m, struct ip *);
130 tos = ip->ip_tos;
131 break;
132 }
133#endif /* INET */
134#ifdef INET6
135 case AF_INET6:
136 {
137 struct ip6_hdr *ip6;
138 proto = IPPROTO_IPV6;
139 if (m->m_len < sizeof(*ip6)) {
140 m = m_pullup(m, sizeof(*ip6));
141 if (!m)
142 return ENOBUFS;
143 }
144 ip6 = mtod(m, struct ip6_hdr *);
145 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
146 break;
147 }
148#endif /* INET6 */
149 case AF_LINK:
150 proto = IPPROTO_ETHERIP;
151 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
152 eiphdr.eip_pad = 0;
153 /* prepend Ethernet-in-IP header */
154 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
155 if (m && m->m_len < sizeof(struct etherip_header))
156 m = m_pullup(m, sizeof(struct etherip_header));
157 if (m == NULL)
158 return ENOBUFS;
159 bcopy(&eiphdr, mtod(m, struct etherip_header *),
160 sizeof(struct etherip_header));
161 break;
162
163 default:
164#ifdef DEBUG
165 printf("in_gif_output: warning: unknown family %d passed\n",
166 family);
167#endif
168 m_freem(m);
169 return EAFNOSUPPORT;
170 }
171
172 bzero(&iphdr, sizeof(iphdr));
173 iphdr.ip_src = sin_src->sin_addr;
174 /* bidirectional configured tunnel mode */
175 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
176 iphdr.ip_dst = sin_dst->sin_addr;
177 else {
178 m_freem(m);
179 return ENETUNREACH;
180 }
181 iphdr.ip_p = proto;
182 /* version will be set in ip_output() */
183 iphdr.ip_ttl = V_ip_gif_ttl;
184 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
185 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
186 &iphdr.ip_tos, &tos);
187
188 /* prepend new IP header */
106 u_int8_t tos;
107
108 GIF_LOCK_ASSERT(sc);
109
110 if (sin_src == NULL || sin_dst == NULL ||
111 sin_src->sin_family != AF_INET ||
112 sin_dst->sin_family != AF_INET) {
113 m_freem(m);
114 return EAFNOSUPPORT;
115 }
116
117 switch (family) {
118#ifdef INET
119 case AF_INET:
120 {
121 struct ip *ip;
122
123 proto = IPPROTO_IPV4;
124 if (m->m_len < sizeof(*ip)) {
125 m = m_pullup(m, sizeof(*ip));
126 if (!m)
127 return ENOBUFS;
128 }
129 ip = mtod(m, struct ip *);
130 tos = ip->ip_tos;
131 break;
132 }
133#endif /* INET */
134#ifdef INET6
135 case AF_INET6:
136 {
137 struct ip6_hdr *ip6;
138 proto = IPPROTO_IPV6;
139 if (m->m_len < sizeof(*ip6)) {
140 m = m_pullup(m, sizeof(*ip6));
141 if (!m)
142 return ENOBUFS;
143 }
144 ip6 = mtod(m, struct ip6_hdr *);
145 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
146 break;
147 }
148#endif /* INET6 */
149 case AF_LINK:
150 proto = IPPROTO_ETHERIP;
151 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
152 eiphdr.eip_pad = 0;
153 /* prepend Ethernet-in-IP header */
154 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
155 if (m && m->m_len < sizeof(struct etherip_header))
156 m = m_pullup(m, sizeof(struct etherip_header));
157 if (m == NULL)
158 return ENOBUFS;
159 bcopy(&eiphdr, mtod(m, struct etherip_header *),
160 sizeof(struct etherip_header));
161 break;
162
163 default:
164#ifdef DEBUG
165 printf("in_gif_output: warning: unknown family %d passed\n",
166 family);
167#endif
168 m_freem(m);
169 return EAFNOSUPPORT;
170 }
171
172 bzero(&iphdr, sizeof(iphdr));
173 iphdr.ip_src = sin_src->sin_addr;
174 /* bidirectional configured tunnel mode */
175 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
176 iphdr.ip_dst = sin_dst->sin_addr;
177 else {
178 m_freem(m);
179 return ENETUNREACH;
180 }
181 iphdr.ip_p = proto;
182 /* version will be set in ip_output() */
183 iphdr.ip_ttl = V_ip_gif_ttl;
184 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
185 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
186 &iphdr.ip_tos, &tos);
187
188 /* prepend new IP header */
189 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
190 if (m && m->m_len < sizeof(struct ip))
191 m = m_pullup(m, sizeof(struct ip));
189 len = sizeof(struct ip);
190#ifndef __NO_STRICT_ALIGNMENT
191 if (family == AF_LINK)
192 len += ETHERIP_ALIGN;
193#endif
194 M_PREPEND(m, len, M_DONTWAIT);
195 if (m != NULL && m->m_len < len)
196 m = m_pullup(m, len);
192 if (m == NULL) {
193 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
194 return ENOBUFS;
195 }
197 if (m == NULL) {
198 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
199 return ENOBUFS;
200 }
201#ifndef __NO_STRICT_ALIGNMENT
202 if (family == AF_LINK) {
203 len = mtod(m, vm_offset_t) & 3;
204 KASSERT(len == 0 || len == ETHERIP_ALIGN,
205 ("in_gif_output: unexpected misalignment"));
206 m->m_data += len;
207 m->m_len -= ETHERIP_ALIGN;
208 }
209#endif
196 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
197
198 M_SETFIB(m, sc->gif_fibnum);
199
200 if (dst->sin_family != sin_dst->sin_family ||
201 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
202 /* cache route doesn't match */
203 bzero(dst, sizeof(*dst));
204 dst->sin_family = sin_dst->sin_family;
205 dst->sin_len = sizeof(struct sockaddr_in);
206 dst->sin_addr = sin_dst->sin_addr;
207 if (sc->gif_ro.ro_rt) {
208 RTFREE(sc->gif_ro.ro_rt);
209 sc->gif_ro.ro_rt = NULL;
210 }
211#if 0
212 GIF2IFP(sc)->if_mtu = GIF_MTU;
213#endif
214 }
215
216 if (sc->gif_ro.ro_rt == NULL) {
217 in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
218 if (sc->gif_ro.ro_rt == NULL) {
219 m_freem(m);
220 return ENETUNREACH;
221 }
222
223 /* if it constitutes infinite encapsulation, punt. */
224 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
225 m_freem(m);
226 return ENETUNREACH; /* XXX */
227 }
228#if 0
229 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
230 - sizeof(struct ip);
231#endif
232 }
233
234 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
235
236 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
237 sc->gif_ro.ro_rt != NULL) {
238 RTFREE(sc->gif_ro.ro_rt);
239 sc->gif_ro.ro_rt = NULL;
240 }
241
242 return (error);
243}
244
245void
246in_gif_input(struct mbuf *m, int off)
247{
248 INIT_VNET_INET(curvnet);
249 struct ifnet *gifp = NULL;
250 struct gif_softc *sc;
251 struct ip *ip;
252 int af;
253 u_int8_t otos;
254 int proto;
255
256 ip = mtod(m, struct ip *);
257 proto = ip->ip_p;
258
259 sc = (struct gif_softc *)encap_getarg(m);
260 if (sc == NULL) {
261 m_freem(m);
262 V_ipstat.ips_nogif++;
263 return;
264 }
265
266 gifp = GIF2IFP(sc);
267 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
268 m_freem(m);
269 V_ipstat.ips_nogif++;
270 return;
271 }
272
273 otos = ip->ip_tos;
274 m_adj(m, off);
275
276 switch (proto) {
277#ifdef INET
278 case IPPROTO_IPV4:
279 {
280 struct ip *ip;
281 af = AF_INET;
282 if (m->m_len < sizeof(*ip)) {
283 m = m_pullup(m, sizeof(*ip));
284 if (!m)
285 return;
286 }
287 ip = mtod(m, struct ip *);
288 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
289 ECN_ALLOWED : ECN_NOCARE,
290 &otos, &ip->ip_tos) == 0) {
291 m_freem(m);
292 return;
293 }
294 break;
295 }
296#endif
297#ifdef INET6
298 case IPPROTO_IPV6:
299 {
300 struct ip6_hdr *ip6;
301 u_int8_t itos, oitos;
302
303 af = AF_INET6;
304 if (m->m_len < sizeof(*ip6)) {
305 m = m_pullup(m, sizeof(*ip6));
306 if (!m)
307 return;
308 }
309 ip6 = mtod(m, struct ip6_hdr *);
310 itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
311 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
312 ECN_ALLOWED : ECN_NOCARE,
313 &otos, &itos) == 0) {
314 m_freem(m);
315 return;
316 }
317 if (itos != oitos) {
318 ip6->ip6_flow &= ~htonl(0xff << 20);
319 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
320 }
321 break;
322 }
323#endif /* INET6 */
324 case IPPROTO_ETHERIP:
325 af = AF_LINK;
326 break;
327
328 default:
329 V_ipstat.ips_nogif++;
330 m_freem(m);
331 return;
332 }
333 gif_input(m, af, gifp);
334 return;
335}
336
337/*
338 * validate outer address.
339 */
340static int
341gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
342{
343 INIT_VNET_INET(curvnet);
344 struct sockaddr_in *src, *dst;
345 struct in_ifaddr *ia4;
346
347 src = (struct sockaddr_in *)sc->gif_psrc;
348 dst = (struct sockaddr_in *)sc->gif_pdst;
349
350 /* check for address match */
351 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
352 dst->sin_addr.s_addr != ip->ip_src.s_addr)
353 return 0;
354
355 /* martian filters on outer source - NOT done in ip_input! */
356 if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
357 return 0;
358 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
359 case 0: case 127: case 255:
360 return 0;
361 }
362 /* reject packets with broadcast on source */
363 TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
364 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
365 continue;
366 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
367 return 0;
368 }
369
370 /* ingress filters on outer source */
371 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
372 struct sockaddr_in sin;
373 struct rtentry *rt;
374
375 bzero(&sin, sizeof(sin));
376 sin.sin_family = AF_INET;
377 sin.sin_len = sizeof(struct sockaddr_in);
378 sin.sin_addr = ip->ip_src;
379 /* XXX MRT check for the interface we would use on output */
380 rt = in_rtalloc1((struct sockaddr *)&sin, 0,
381 0UL, sc->gif_fibnum);
382 if (!rt || rt->rt_ifp != ifp) {
383#if 0
384 log(LOG_WARNING, "%s: packet from 0x%x dropped "
385 "due to ingress filter\n", if_name(GIF2IFP(sc)),
386 (u_int32_t)ntohl(sin.sin_addr.s_addr));
387#endif
388 if (rt)
389 RTFREE_LOCKED(rt);
390 return 0;
391 }
392 RTFREE_LOCKED(rt);
393 }
394
395 return 32 * 2;
396}
397
398/*
399 * we know that we are in IFF_UP, outer address available, and outer family
400 * matched the physical addr family. see gif_encapcheck().
401 */
402int
403gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
404{
405 struct ip ip;
406 struct gif_softc *sc;
407 struct ifnet *ifp;
408
409 /* sanity check done in caller */
410 sc = (struct gif_softc *)arg;
411
412 /* LINTED const cast */
413 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
414 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
415
416 return gif_validate4(&ip, sc, ifp);
417}
418
419int
420in_gif_attach(struct gif_softc *sc)
421{
422 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
423 &in_gif_protosw, sc);
424 if (sc->encap_cookie4 == NULL)
425 return EEXIST;
426 return 0;
427}
428
429int
430in_gif_detach(struct gif_softc *sc)
431{
432 int error;
433
434 error = encap_detach(sc->encap_cookie4);
435 if (error == 0)
436 sc->encap_cookie4 = NULL;
437 return error;
438}
210 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
211
212 M_SETFIB(m, sc->gif_fibnum);
213
214 if (dst->sin_family != sin_dst->sin_family ||
215 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
216 /* cache route doesn't match */
217 bzero(dst, sizeof(*dst));
218 dst->sin_family = sin_dst->sin_family;
219 dst->sin_len = sizeof(struct sockaddr_in);
220 dst->sin_addr = sin_dst->sin_addr;
221 if (sc->gif_ro.ro_rt) {
222 RTFREE(sc->gif_ro.ro_rt);
223 sc->gif_ro.ro_rt = NULL;
224 }
225#if 0
226 GIF2IFP(sc)->if_mtu = GIF_MTU;
227#endif
228 }
229
230 if (sc->gif_ro.ro_rt == NULL) {
231 in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
232 if (sc->gif_ro.ro_rt == NULL) {
233 m_freem(m);
234 return ENETUNREACH;
235 }
236
237 /* if it constitutes infinite encapsulation, punt. */
238 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
239 m_freem(m);
240 return ENETUNREACH; /* XXX */
241 }
242#if 0
243 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
244 - sizeof(struct ip);
245#endif
246 }
247
248 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
249
250 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
251 sc->gif_ro.ro_rt != NULL) {
252 RTFREE(sc->gif_ro.ro_rt);
253 sc->gif_ro.ro_rt = NULL;
254 }
255
256 return (error);
257}
258
259void
260in_gif_input(struct mbuf *m, int off)
261{
262 INIT_VNET_INET(curvnet);
263 struct ifnet *gifp = NULL;
264 struct gif_softc *sc;
265 struct ip *ip;
266 int af;
267 u_int8_t otos;
268 int proto;
269
270 ip = mtod(m, struct ip *);
271 proto = ip->ip_p;
272
273 sc = (struct gif_softc *)encap_getarg(m);
274 if (sc == NULL) {
275 m_freem(m);
276 V_ipstat.ips_nogif++;
277 return;
278 }
279
280 gifp = GIF2IFP(sc);
281 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
282 m_freem(m);
283 V_ipstat.ips_nogif++;
284 return;
285 }
286
287 otos = ip->ip_tos;
288 m_adj(m, off);
289
290 switch (proto) {
291#ifdef INET
292 case IPPROTO_IPV4:
293 {
294 struct ip *ip;
295 af = AF_INET;
296 if (m->m_len < sizeof(*ip)) {
297 m = m_pullup(m, sizeof(*ip));
298 if (!m)
299 return;
300 }
301 ip = mtod(m, struct ip *);
302 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
303 ECN_ALLOWED : ECN_NOCARE,
304 &otos, &ip->ip_tos) == 0) {
305 m_freem(m);
306 return;
307 }
308 break;
309 }
310#endif
311#ifdef INET6
312 case IPPROTO_IPV6:
313 {
314 struct ip6_hdr *ip6;
315 u_int8_t itos, oitos;
316
317 af = AF_INET6;
318 if (m->m_len < sizeof(*ip6)) {
319 m = m_pullup(m, sizeof(*ip6));
320 if (!m)
321 return;
322 }
323 ip6 = mtod(m, struct ip6_hdr *);
324 itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
325 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
326 ECN_ALLOWED : ECN_NOCARE,
327 &otos, &itos) == 0) {
328 m_freem(m);
329 return;
330 }
331 if (itos != oitos) {
332 ip6->ip6_flow &= ~htonl(0xff << 20);
333 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
334 }
335 break;
336 }
337#endif /* INET6 */
338 case IPPROTO_ETHERIP:
339 af = AF_LINK;
340 break;
341
342 default:
343 V_ipstat.ips_nogif++;
344 m_freem(m);
345 return;
346 }
347 gif_input(m, af, gifp);
348 return;
349}
350
351/*
352 * validate outer address.
353 */
354static int
355gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
356{
357 INIT_VNET_INET(curvnet);
358 struct sockaddr_in *src, *dst;
359 struct in_ifaddr *ia4;
360
361 src = (struct sockaddr_in *)sc->gif_psrc;
362 dst = (struct sockaddr_in *)sc->gif_pdst;
363
364 /* check for address match */
365 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
366 dst->sin_addr.s_addr != ip->ip_src.s_addr)
367 return 0;
368
369 /* martian filters on outer source - NOT done in ip_input! */
370 if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
371 return 0;
372 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
373 case 0: case 127: case 255:
374 return 0;
375 }
376 /* reject packets with broadcast on source */
377 TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
378 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
379 continue;
380 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
381 return 0;
382 }
383
384 /* ingress filters on outer source */
385 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
386 struct sockaddr_in sin;
387 struct rtentry *rt;
388
389 bzero(&sin, sizeof(sin));
390 sin.sin_family = AF_INET;
391 sin.sin_len = sizeof(struct sockaddr_in);
392 sin.sin_addr = ip->ip_src;
393 /* XXX MRT check for the interface we would use on output */
394 rt = in_rtalloc1((struct sockaddr *)&sin, 0,
395 0UL, sc->gif_fibnum);
396 if (!rt || rt->rt_ifp != ifp) {
397#if 0
398 log(LOG_WARNING, "%s: packet from 0x%x dropped "
399 "due to ingress filter\n", if_name(GIF2IFP(sc)),
400 (u_int32_t)ntohl(sin.sin_addr.s_addr));
401#endif
402 if (rt)
403 RTFREE_LOCKED(rt);
404 return 0;
405 }
406 RTFREE_LOCKED(rt);
407 }
408
409 return 32 * 2;
410}
411
412/*
413 * we know that we are in IFF_UP, outer address available, and outer family
414 * matched the physical addr family. see gif_encapcheck().
415 */
416int
417gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
418{
419 struct ip ip;
420 struct gif_softc *sc;
421 struct ifnet *ifp;
422
423 /* sanity check done in caller */
424 sc = (struct gif_softc *)arg;
425
426 /* LINTED const cast */
427 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
428 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
429
430 return gif_validate4(&ip, sc, ifp);
431}
432
433int
434in_gif_attach(struct gif_softc *sc)
435{
436 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
437 &in_gif_protosw, sc);
438 if (sc->encap_cookie4 == NULL)
439 return EEXIST;
440 return 0;
441}
442
443int
444in_gif_detach(struct gif_softc *sc)
445{
446 int error;
447
448 error = encap_detach(sc->encap_cookie4);
449 if (error == 0)
450 sc->encap_cookie4 = NULL;
451 return error;
452}