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: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 196019 2009-08-01 19:26:27Z rwatson $");
34
35#include "opt_inet.h"
36#include "opt_inet6.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/socket.h>
41#include <sys/sockio.h>
42#include <sys/mbuf.h>
43#include <sys/errno.h>
44#include <sys/queue.h>
45#include <sys/syslog.h>
46#include <sys/protosw.h>
47#include <sys/malloc.h>
48
49#include <net/if.h>
50#include <net/route.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#ifdef INET
55#include <netinet/ip.h>
56#endif
57#include <netinet/ip_encap.h>
58#ifdef INET6
59#include <netinet/ip6.h>
60#include <netinet6/ip6_var.h>
61#include <netinet6/in6_gif.h>
62#include <netinet6/in6_var.h>
63#endif
64#include <netinet6/ip6protosw.h>
65#include <netinet/ip_ecn.h>
66#ifdef INET6
67#include <netinet6/ip6_ecn.h>
68#endif
69
70#include <net/if_gif.h>
71
72static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
73 struct ifnet *);
74
75extern struct domain inet6domain;
76struct ip6protosw in6_gif_protosw = {
77 .pr_type = SOCK_RAW,
78 .pr_domain = &inet6domain,
79 .pr_protocol = 0, /* IPPROTO_IPV[46] */
80 .pr_flags = PR_ATOMIC|PR_ADDR,
81 .pr_input = in6_gif_input,
82 .pr_output = rip6_output,
83 .pr_ctloutput = rip6_ctloutput,
84 .pr_usrreqs = &rip6_usrreqs
85};
86
87int
88in6_gif_output(struct ifnet *ifp,
89 int family, /* family of the packet to be encapsulate */
90 struct mbuf *m)
91{
92 struct gif_softc *sc = ifp->if_softc;
93 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
94 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
95 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
96 struct ip6_hdr *ip6;
97 struct etherip_header eiphdr;
98 int error, len, proto;
99 u_int8_t itos, otos;
100
101 GIF_LOCK_ASSERT(sc);
102
103 if (sin6_src == NULL || sin6_dst == NULL ||
104 sin6_src->sin6_family != AF_INET6 ||
105 sin6_dst->sin6_family != AF_INET6) {
106 m_freem(m);
107 return EAFNOSUPPORT;
108 }
109
110 switch (family) {
111#ifdef INET
112 case AF_INET:
113 {
114 struct ip *ip;
115
116 proto = IPPROTO_IPV4;
117 if (m->m_len < sizeof(*ip)) {
118 m = m_pullup(m, sizeof(*ip));
119 if (!m)
120 return ENOBUFS;
121 }
122 ip = mtod(m, struct ip *);
123 itos = ip->ip_tos;
124 break;
125 }
126#endif
127#ifdef INET6
128 case AF_INET6:
129 {
130 struct ip6_hdr *ip6;
131 proto = IPPROTO_IPV6;
132 if (m->m_len < sizeof(*ip6)) {
133 m = m_pullup(m, sizeof(*ip6));
134 if (!m)
135 return ENOBUFS;
136 }
137 ip6 = mtod(m, struct ip6_hdr *);
138 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
139 break;
140 }
141#endif
142 case AF_LINK:
143 proto = IPPROTO_ETHERIP;
144
145 /*
146 * GIF_SEND_REVETHIP (disabled by default) intentionally
147 * sends an EtherIP packet with revered version field in
148 * the header. This is a knob for backward compatibility
149 * with FreeBSD 7.2R or prior.
150 */
151 if ((sc->gif_options & GIF_SEND_REVETHIP)) {
152 eiphdr.eip_ver = 0;
153 eiphdr.eip_resvl = ETHERIP_VERSION;
154 eiphdr.eip_resvh = 0;
155 } else {
156 eiphdr.eip_ver = ETHERIP_VERSION;
157 eiphdr.eip_resvl = 0;
158 eiphdr.eip_resvh = 0;
159 }
160 /* prepend Ethernet-in-IP header */
161 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
162 if (m && m->m_len < sizeof(struct etherip_header))
163 m = m_pullup(m, sizeof(struct etherip_header));
164 if (m == NULL)
165 return ENOBUFS;
166 bcopy(&eiphdr, mtod(m, struct etherip_header *),
167 sizeof(struct etherip_header));
168 break;
169
170 default:
171#ifdef DEBUG
172 printf("in6_gif_output: warning: unknown family %d passed\n",
173 family);
174#endif
175 m_freem(m);
176 return EAFNOSUPPORT;
177 }
178
179 /* prepend new IP header */
180 len = sizeof(struct ip6_hdr);
181#ifndef __NO_STRICT_ALIGNMENT
182 if (family == AF_LINK)
183 len += ETHERIP_ALIGN;
184#endif
185 M_PREPEND(m, len, M_DONTWAIT);
186 if (m != NULL && m->m_len < len)
187 m = m_pullup(m, len);
188 if (m == NULL) {
189 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
190 return ENOBUFS;
191 }
192#ifndef __NO_STRICT_ALIGNMENT
193 if (family == AF_LINK) {
194 len = mtod(m, vm_offset_t) & 3;
195 KASSERT(len == 0 || len == ETHERIP_ALIGN,
196 ("in6_gif_output: unexpected misalignment"));
197 m->m_data += len;
198 m->m_len -= ETHERIP_ALIGN;
199 }
200#endif
201
202 ip6 = mtod(m, struct ip6_hdr *);
203 ip6->ip6_flow = 0;
204 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
205 ip6->ip6_vfc |= IPV6_VERSION;
206 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
207 ip6->ip6_nxt = proto;
208 ip6->ip6_hlim = V_ip6_gif_hlim;
209 ip6->ip6_src = sin6_src->sin6_addr;
210 /* bidirectional configured tunnel mode */
211 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
212 ip6->ip6_dst = sin6_dst->sin6_addr;
213 else {
214 m_freem(m);
215 return ENETUNREACH;
216 }
217 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
218 &otos, &itos);
219 ip6->ip6_flow &= ~htonl(0xff << 20);
220 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
221
222 if (dst->sin6_family != sin6_dst->sin6_family ||
223 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
224 /* cache route doesn't match */
225 bzero(dst, sizeof(*dst));
226 dst->sin6_family = sin6_dst->sin6_family;
227 dst->sin6_len = sizeof(struct sockaddr_in6);
228 dst->sin6_addr = sin6_dst->sin6_addr;
229 if (sc->gif_ro6.ro_rt) {
230 RTFREE(sc->gif_ro6.ro_rt);
231 sc->gif_ro6.ro_rt = NULL;
232 }
233#if 0
234 GIF2IFP(sc)->if_mtu = GIF_MTU;
235#endif
236 }
237
238 if (sc->gif_ro6.ro_rt == NULL) {
239 rtalloc((struct route *)&sc->gif_ro6);
240 if (sc->gif_ro6.ro_rt == NULL) {
241 m_freem(m);
242 return ENETUNREACH;
243 }
244
245 /* if it constitutes infinite encapsulation, punt. */
246 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
247 m_freem(m);
248 return ENETUNREACH; /*XXX*/
249 }
250#if 0
251 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
252 - sizeof(struct ip6_hdr);
253#endif
254 }
255
256#ifdef IPV6_MINMTU
257 /*
258 * force fragmentation to minimum MTU, to avoid path MTU discovery.
259 * it is too painful to ask for resend of inner packet, to achieve
260 * path MTU discovery for encapsulated packets.
261 */
262 error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
263#else
264 error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
265#endif
266
267 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
268 sc->gif_ro6.ro_rt != NULL) {
269 RTFREE(sc->gif_ro6.ro_rt);
270 sc->gif_ro6.ro_rt = NULL;
271 }
272
273 return (error);
274}
275
276int
277in6_gif_input(struct mbuf **mp, int *offp, int proto)
278{
279 struct mbuf *m = *mp;
280 struct ifnet *gifp = NULL;
281 struct gif_softc *sc;
282 struct ip6_hdr *ip6;
283 int af = 0;
284 u_int32_t otos;
285
286 ip6 = mtod(m, struct ip6_hdr *);
287
288 sc = (struct gif_softc *)encap_getarg(m);
289 if (sc == NULL) {
290 m_freem(m);
291 V_ip6stat.ip6s_nogif++;
292 return IPPROTO_DONE;
293 }
294
295 gifp = GIF2IFP(sc);
296 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
297 m_freem(m);
298 V_ip6stat.ip6s_nogif++;
299 return IPPROTO_DONE;
300 }
301
302 otos = ip6->ip6_flow;
303 m_adj(m, *offp);
304
305 switch (proto) {
306#ifdef INET
307 case IPPROTO_IPV4:
308 {
309 struct ip *ip;
310 u_int8_t otos8;
311 af = AF_INET;
312 otos8 = (ntohl(otos) >> 20) & 0xff;
313 if (m->m_len < sizeof(*ip)) {
314 m = m_pullup(m, sizeof(*ip));
315 if (!m)
316 return IPPROTO_DONE;
317 }
318 ip = mtod(m, struct ip *);
319 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
320 ECN_ALLOWED : ECN_NOCARE,
321 &otos8, &ip->ip_tos) == 0) {
322 m_freem(m);
323 return IPPROTO_DONE;
324 }
325 break;
326 }
327#endif /* INET */
328#ifdef INET6
329 case IPPROTO_IPV6:
330 {
331 struct ip6_hdr *ip6;
332 af = AF_INET6;
333 if (m->m_len < sizeof(*ip6)) {
334 m = m_pullup(m, sizeof(*ip6));
335 if (!m)
336 return IPPROTO_DONE;
337 }
338 ip6 = mtod(m, struct ip6_hdr *);
339 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
340 ECN_ALLOWED : ECN_NOCARE,
341 &otos, &ip6->ip6_flow) == 0) {
342 m_freem(m);
343 return IPPROTO_DONE;
344 }
345 break;
346 }
347#endif
348 case IPPROTO_ETHERIP:
349 af = AF_LINK;
350 break;
351
352 default:
353 V_ip6stat.ip6s_nogif++;
354 m_freem(m);
355 return IPPROTO_DONE;
356 }
357
358 gif_input(m, af, gifp);
359 return IPPROTO_DONE;
360}
361
362/*
363 * validate outer address.
364 */
365static int
366gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
367 struct ifnet *ifp)
368{
369 struct sockaddr_in6 *src, *dst;
370
371 src = (struct sockaddr_in6 *)sc->gif_psrc;
372 dst = (struct sockaddr_in6 *)sc->gif_pdst;
373
374 /*
375 * Check for address match. Note that the check is for an incoming
376 * packet. We should compare the *source* address in our configuration
377 * and the *destination* address of the packet, and vice versa.
378 */
379 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
380 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
381 return 0;
382
383 /* martian filters on outer source - done in ip6_input */
384
385 /* ingress filters on outer source */
386 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
387 struct sockaddr_in6 sin6;
388 struct rtentry *rt;
389
390 bzero(&sin6, sizeof(sin6));
391 sin6.sin6_family = AF_INET6;
392 sin6.sin6_len = sizeof(struct sockaddr_in6);
393 sin6.sin6_addr = ip6->ip6_src;
394 sin6.sin6_scope_id = 0; /* XXX */
395
396 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
397 if (!rt || rt->rt_ifp != ifp) {
398#if 0
399 char ip6buf[INET6_ADDRSTRLEN];
400 log(LOG_WARNING, "%s: packet from %s dropped "
401 "due to ingress filter\n", if_name(GIF2IFP(sc)),
402 ip6_sprintf(ip6buf, &sin6.sin6_addr));
403#endif
404 if (rt)
405 RTFREE_LOCKED(rt);
406 return 0;
407 }
408 RTFREE_LOCKED(rt);
409 }
410
411 return 128 * 2;
412}
413
414/*
415 * we know that we are in IFF_UP, outer address available, and outer family
416 * matched the physical addr family. see gif_encapcheck().
417 * sanity check for arg should have been done in the caller.
418 */
419int
420gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
421{
422 struct ip6_hdr ip6;
423 struct gif_softc *sc;
424 struct ifnet *ifp;
425
426 /* sanity check done in caller */
427 sc = (struct gif_softc *)arg;
428
429 /* LINTED const cast */
430 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
431 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
432
433 return gif_validate6(&ip6, sc, ifp);
434}
435
436int
437in6_gif_attach(struct gif_softc *sc)
438{
439 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
440 (void *)&in6_gif_protosw, sc);
441 if (sc->encap_cookie6 == NULL)
442 return EEXIST;
443 return 0;
444}
445
446int
447in6_gif_detach(struct gif_softc *sc)
448{
449 int error;
450
451 error = encap_detach(sc->encap_cookie6);
452 if (error == 0)
453 sc->encap_cookie6 = NULL;
454 return error;
455}
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: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 196019 2009-08-01 19:26:27Z rwatson $");
34
35#include "opt_inet.h"
36#include "opt_inet6.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/socket.h>
41#include <sys/sockio.h>
42#include <sys/mbuf.h>
43#include <sys/errno.h>
44#include <sys/queue.h>
45#include <sys/syslog.h>
46#include <sys/protosw.h>
47#include <sys/malloc.h>
48
49#include <net/if.h>
50#include <net/route.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#ifdef INET
55#include <netinet/ip.h>
56#endif
57#include <netinet/ip_encap.h>
58#ifdef INET6
59#include <netinet/ip6.h>
60#include <netinet6/ip6_var.h>
61#include <netinet6/in6_gif.h>
62#include <netinet6/in6_var.h>
63#endif
64#include <netinet6/ip6protosw.h>
65#include <netinet/ip_ecn.h>
66#ifdef INET6
67#include <netinet6/ip6_ecn.h>
68#endif
69
70#include <net/if_gif.h>
71
72static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
73 struct ifnet *);
74
75extern struct domain inet6domain;
76struct ip6protosw in6_gif_protosw = {
77 .pr_type = SOCK_RAW,
78 .pr_domain = &inet6domain,
79 .pr_protocol = 0, /* IPPROTO_IPV[46] */
80 .pr_flags = PR_ATOMIC|PR_ADDR,
81 .pr_input = in6_gif_input,
82 .pr_output = rip6_output,
83 .pr_ctloutput = rip6_ctloutput,
84 .pr_usrreqs = &rip6_usrreqs
85};
86
87int
88in6_gif_output(struct ifnet *ifp,
89 int family, /* family of the packet to be encapsulate */
90 struct mbuf *m)
91{
92 struct gif_softc *sc = ifp->if_softc;
93 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
94 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
95 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
96 struct ip6_hdr *ip6;
97 struct etherip_header eiphdr;
98 int error, len, proto;
99 u_int8_t itos, otos;
100
101 GIF_LOCK_ASSERT(sc);
102
103 if (sin6_src == NULL || sin6_dst == NULL ||
104 sin6_src->sin6_family != AF_INET6 ||
105 sin6_dst->sin6_family != AF_INET6) {
106 m_freem(m);
107 return EAFNOSUPPORT;
108 }
109
110 switch (family) {
111#ifdef INET
112 case AF_INET:
113 {
114 struct ip *ip;
115
116 proto = IPPROTO_IPV4;
117 if (m->m_len < sizeof(*ip)) {
118 m = m_pullup(m, sizeof(*ip));
119 if (!m)
120 return ENOBUFS;
121 }
122 ip = mtod(m, struct ip *);
123 itos = ip->ip_tos;
124 break;
125 }
126#endif
127#ifdef INET6
128 case AF_INET6:
129 {
130 struct ip6_hdr *ip6;
131 proto = IPPROTO_IPV6;
132 if (m->m_len < sizeof(*ip6)) {
133 m = m_pullup(m, sizeof(*ip6));
134 if (!m)
135 return ENOBUFS;
136 }
137 ip6 = mtod(m, struct ip6_hdr *);
138 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
139 break;
140 }
141#endif
142 case AF_LINK:
143 proto = IPPROTO_ETHERIP;
144
145 /*
146 * GIF_SEND_REVETHIP (disabled by default) intentionally
147 * sends an EtherIP packet with revered version field in
148 * the header. This is a knob for backward compatibility
149 * with FreeBSD 7.2R or prior.
150 */
151 if ((sc->gif_options & GIF_SEND_REVETHIP)) {
152 eiphdr.eip_ver = 0;
153 eiphdr.eip_resvl = ETHERIP_VERSION;
154 eiphdr.eip_resvh = 0;
155 } else {
156 eiphdr.eip_ver = ETHERIP_VERSION;
157 eiphdr.eip_resvl = 0;
158 eiphdr.eip_resvh = 0;
159 }
160 /* prepend Ethernet-in-IP header */
161 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
162 if (m && m->m_len < sizeof(struct etherip_header))
163 m = m_pullup(m, sizeof(struct etherip_header));
164 if (m == NULL)
165 return ENOBUFS;
166 bcopy(&eiphdr, mtod(m, struct etherip_header *),
167 sizeof(struct etherip_header));
168 break;
169
170 default:
171#ifdef DEBUG
172 printf("in6_gif_output: warning: unknown family %d passed\n",
173 family);
174#endif
175 m_freem(m);
176 return EAFNOSUPPORT;
177 }
178
179 /* prepend new IP header */
180 len = sizeof(struct ip6_hdr);
181#ifndef __NO_STRICT_ALIGNMENT
182 if (family == AF_LINK)
183 len += ETHERIP_ALIGN;
184#endif
185 M_PREPEND(m, len, M_DONTWAIT);
186 if (m != NULL && m->m_len < len)
187 m = m_pullup(m, len);
188 if (m == NULL) {
189 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
190 return ENOBUFS;
191 }
192#ifndef __NO_STRICT_ALIGNMENT
193 if (family == AF_LINK) {
194 len = mtod(m, vm_offset_t) & 3;
195 KASSERT(len == 0 || len == ETHERIP_ALIGN,
196 ("in6_gif_output: unexpected misalignment"));
197 m->m_data += len;
198 m->m_len -= ETHERIP_ALIGN;
199 }
200#endif
201
202 ip6 = mtod(m, struct ip6_hdr *);
203 ip6->ip6_flow = 0;
204 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
205 ip6->ip6_vfc |= IPV6_VERSION;
206 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
207 ip6->ip6_nxt = proto;
208 ip6->ip6_hlim = V_ip6_gif_hlim;
209 ip6->ip6_src = sin6_src->sin6_addr;
210 /* bidirectional configured tunnel mode */
211 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
212 ip6->ip6_dst = sin6_dst->sin6_addr;
213 else {
214 m_freem(m);
215 return ENETUNREACH;
216 }
217 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
218 &otos, &itos);
219 ip6->ip6_flow &= ~htonl(0xff << 20);
220 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
221
222 if (dst->sin6_family != sin6_dst->sin6_family ||
223 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
224 /* cache route doesn't match */
225 bzero(dst, sizeof(*dst));
226 dst->sin6_family = sin6_dst->sin6_family;
227 dst->sin6_len = sizeof(struct sockaddr_in6);
228 dst->sin6_addr = sin6_dst->sin6_addr;
229 if (sc->gif_ro6.ro_rt) {
230 RTFREE(sc->gif_ro6.ro_rt);
231 sc->gif_ro6.ro_rt = NULL;
232 }
233#if 0
234 GIF2IFP(sc)->if_mtu = GIF_MTU;
235#endif
236 }
237
238 if (sc->gif_ro6.ro_rt == NULL) {
239 rtalloc((struct route *)&sc->gif_ro6);
240 if (sc->gif_ro6.ro_rt == NULL) {
241 m_freem(m);
242 return ENETUNREACH;
243 }
244
245 /* if it constitutes infinite encapsulation, punt. */
246 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
247 m_freem(m);
248 return ENETUNREACH; /*XXX*/
249 }
250#if 0
251 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
252 - sizeof(struct ip6_hdr);
253#endif
254 }
255
256#ifdef IPV6_MINMTU
257 /*
258 * force fragmentation to minimum MTU, to avoid path MTU discovery.
259 * it is too painful to ask for resend of inner packet, to achieve
260 * path MTU discovery for encapsulated packets.
261 */
262 error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
263#else
264 error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
265#endif
266
267 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
268 sc->gif_ro6.ro_rt != NULL) {
269 RTFREE(sc->gif_ro6.ro_rt);
270 sc->gif_ro6.ro_rt = NULL;
271 }
272
273 return (error);
274}
275
276int
277in6_gif_input(struct mbuf **mp, int *offp, int proto)
278{
279 struct mbuf *m = *mp;
280 struct ifnet *gifp = NULL;
281 struct gif_softc *sc;
282 struct ip6_hdr *ip6;
283 int af = 0;
284 u_int32_t otos;
285
286 ip6 = mtod(m, struct ip6_hdr *);
287
288 sc = (struct gif_softc *)encap_getarg(m);
289 if (sc == NULL) {
290 m_freem(m);
291 V_ip6stat.ip6s_nogif++;
292 return IPPROTO_DONE;
293 }
294
295 gifp = GIF2IFP(sc);
296 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
297 m_freem(m);
298 V_ip6stat.ip6s_nogif++;
299 return IPPROTO_DONE;
300 }
301
302 otos = ip6->ip6_flow;
303 m_adj(m, *offp);
304
305 switch (proto) {
306#ifdef INET
307 case IPPROTO_IPV4:
308 {
309 struct ip *ip;
310 u_int8_t otos8;
311 af = AF_INET;
312 otos8 = (ntohl(otos) >> 20) & 0xff;
313 if (m->m_len < sizeof(*ip)) {
314 m = m_pullup(m, sizeof(*ip));
315 if (!m)
316 return IPPROTO_DONE;
317 }
318 ip = mtod(m, struct ip *);
319 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
320 ECN_ALLOWED : ECN_NOCARE,
321 &otos8, &ip->ip_tos) == 0) {
322 m_freem(m);
323 return IPPROTO_DONE;
324 }
325 break;
326 }
327#endif /* INET */
328#ifdef INET6
329 case IPPROTO_IPV6:
330 {
331 struct ip6_hdr *ip6;
332 af = AF_INET6;
333 if (m->m_len < sizeof(*ip6)) {
334 m = m_pullup(m, sizeof(*ip6));
335 if (!m)
336 return IPPROTO_DONE;
337 }
338 ip6 = mtod(m, struct ip6_hdr *);
339 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
340 ECN_ALLOWED : ECN_NOCARE,
341 &otos, &ip6->ip6_flow) == 0) {
342 m_freem(m);
343 return IPPROTO_DONE;
344 }
345 break;
346 }
347#endif
348 case IPPROTO_ETHERIP:
349 af = AF_LINK;
350 break;
351
352 default:
353 V_ip6stat.ip6s_nogif++;
354 m_freem(m);
355 return IPPROTO_DONE;
356 }
357
358 gif_input(m, af, gifp);
359 return IPPROTO_DONE;
360}
361
362/*
363 * validate outer address.
364 */
365static int
366gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
367 struct ifnet *ifp)
368{
369 struct sockaddr_in6 *src, *dst;
370
371 src = (struct sockaddr_in6 *)sc->gif_psrc;
372 dst = (struct sockaddr_in6 *)sc->gif_pdst;
373
374 /*
375 * Check for address match. Note that the check is for an incoming
376 * packet. We should compare the *source* address in our configuration
377 * and the *destination* address of the packet, and vice versa.
378 */
379 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
380 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
381 return 0;
382
383 /* martian filters on outer source - done in ip6_input */
384
385 /* ingress filters on outer source */
386 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
387 struct sockaddr_in6 sin6;
388 struct rtentry *rt;
389
390 bzero(&sin6, sizeof(sin6));
391 sin6.sin6_family = AF_INET6;
392 sin6.sin6_len = sizeof(struct sockaddr_in6);
393 sin6.sin6_addr = ip6->ip6_src;
394 sin6.sin6_scope_id = 0; /* XXX */
395
396 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
397 if (!rt || rt->rt_ifp != ifp) {
398#if 0
399 char ip6buf[INET6_ADDRSTRLEN];
400 log(LOG_WARNING, "%s: packet from %s dropped "
401 "due to ingress filter\n", if_name(GIF2IFP(sc)),
402 ip6_sprintf(ip6buf, &sin6.sin6_addr));
403#endif
404 if (rt)
405 RTFREE_LOCKED(rt);
406 return 0;
407 }
408 RTFREE_LOCKED(rt);
409 }
410
411 return 128 * 2;
412}
413
414/*
415 * we know that we are in IFF_UP, outer address available, and outer family
416 * matched the physical addr family. see gif_encapcheck().
417 * sanity check for arg should have been done in the caller.
418 */
419int
420gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
421{
422 struct ip6_hdr ip6;
423 struct gif_softc *sc;
424 struct ifnet *ifp;
425
426 /* sanity check done in caller */
427 sc = (struct gif_softc *)arg;
428
429 /* LINTED const cast */
430 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
431 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
432
433 return gif_validate6(&ip6, sc, ifp);
434}
435
436int
437in6_gif_attach(struct gif_softc *sc)
438{
439 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
440 (void *)&in6_gif_protosw, sc);
441 if (sc->encap_cookie6 == NULL)
442 return EEXIST;
443 return 0;
444}
445
446int
447in6_gif_detach(struct gif_softc *sc)
448{
449 int error;
450
451 error = encap_detach(sc->encap_cookie6);
452 if (error == 0)
453 sc->encap_cookie6 = NULL;
454 return error;
455}