xref: /freebsd/sys/net/if_gif.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*	$FreeBSD$	*/
2 /*	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $	*/
3 
4 /*-
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/protosw.h>
51 #include <sys/conf.h>
52 #include <machine/cpu.h>
53 
54 #include <net/if.h>
55 #include <net/if_clone.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/bpf.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #ifdef	INET
65 #include <netinet/in_var.h>
66 #include <netinet/in_gif.h>
67 #include <netinet/ip_var.h>
68 #endif	/* INET */
69 
70 #ifdef INET6
71 #ifndef INET
72 #include <netinet/in.h>
73 #endif
74 #include <netinet6/in6_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/scope6_var.h>
78 #include <netinet6/in6_gif.h>
79 #include <netinet6/ip6protosw.h>
80 #endif /* INET6 */
81 
82 #include <netinet/ip_encap.h>
83 #include <net/if_gif.h>
84 
85 #include <net/net_osdep.h>
86 
87 #define GIFNAME		"gif"
88 
89 /*
90  * gif_mtx protects the global gif_softc_list.
91  * XXX: Per-softc locking is still required.
92  */
93 static struct mtx gif_mtx;
94 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
95 static LIST_HEAD(, gif_softc) gif_softc_list;
96 
97 void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
98 void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
99 void	(*ng_gif_attach_p)(struct ifnet *ifp);
100 void	(*ng_gif_detach_p)(struct ifnet *ifp);
101 
102 static int	gif_clone_create(struct if_clone *, int);
103 static void	gif_clone_destroy(struct ifnet *);
104 
105 IFC_SIMPLE_DECLARE(gif, 0);
106 
107 static int gifmodevent(module_t, int, void *);
108 
109 SYSCTL_DECL(_net_link);
110 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
111     "Generic Tunnel Interface");
112 #ifndef MAX_GIF_NEST
113 /*
114  * This macro controls the default upper limitation on nesting of gif tunnels.
115  * Since, setting a large value to this macro with a careless configuration
116  * may introduce system crash, we don't allow any nestings by default.
117  * If you need to configure nested gif tunnels, you can define this macro
118  * in your kernel configuration file.  However, if you do so, please be
119  * careful to configure the tunnels so that it won't make a loop.
120  */
121 #define MAX_GIF_NEST 1
122 #endif
123 static int max_gif_nesting = MAX_GIF_NEST;
124 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
125     &max_gif_nesting, 0, "Max nested tunnels");
126 
127 /*
128  * By default, we disallow creation of multiple tunnels between the same
129  * pair of addresses.  Some applications require this functionality so
130  * we allow control over this check here.
131  */
132 #ifdef XBONEHACK
133 static int parallel_tunnels = 1;
134 #else
135 static int parallel_tunnels = 0;
136 #endif
137 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
138     &parallel_tunnels, 0, "Allow parallel tunnels?");
139 
140 static int
141 gif_clone_create(ifc, unit)
142 	struct if_clone *ifc;
143 	int unit;
144 {
145 	struct gif_softc *sc;
146 
147 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
148 	GIF2IFP(sc) = if_alloc(IFT_GIF);
149 	if (GIF2IFP(sc) == NULL) {
150 		free(sc, M_GIF);
151 		return (ENOSPC);
152 	}
153 
154 	GIF2IFP(sc)->if_softc = sc;
155 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
156 
157 	gifattach0(sc);
158 
159 	mtx_lock(&gif_mtx);
160 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
161 	mtx_unlock(&gif_mtx);
162 	return (0);
163 }
164 
165 void
166 gifattach0(sc)
167 	struct gif_softc *sc;
168 {
169 
170 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
171 
172 	GIF2IFP(sc)->if_addrlen = 0;
173 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
174 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
175 #if 0
176 	/* turn off ingress filter */
177 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
178 #endif
179 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
180 	GIF2IFP(sc)->if_output = gif_output;
181 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
182 	if_attach(GIF2IFP(sc));
183 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
184 	if (ng_gif_attach_p != NULL)
185 		(*ng_gif_attach_p)(GIF2IFP(sc));
186 }
187 
188 static void
189 gif_destroy(struct gif_softc *sc)
190 {
191 	struct ifnet *ifp = GIF2IFP(sc);
192 	int err;
193 
194 	gif_delete_tunnel(ifp);
195 #ifdef INET6
196 	if (sc->encap_cookie6 != NULL) {
197 		err = encap_detach(sc->encap_cookie6);
198 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
199 	}
200 #endif
201 #ifdef INET
202 	if (sc->encap_cookie4 != NULL) {
203 		err = encap_detach(sc->encap_cookie4);
204 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
205 	}
206 #endif
207 
208 	if (ng_gif_detach_p != NULL)
209 		(*ng_gif_detach_p)(ifp);
210 	bpfdetach(ifp);
211 	if_detach(ifp);
212 	if_free(ifp);
213 
214 	free(sc, M_GIF);
215 }
216 
217 static void
218 gif_clone_destroy(ifp)
219 	struct ifnet *ifp;
220 {
221 	struct gif_softc *sc = ifp->if_softc;
222 
223 	mtx_lock(&gif_mtx);
224 	LIST_REMOVE(sc, gif_list);
225 	mtx_unlock(&gif_mtx);
226 	gif_destroy(sc);
227 }
228 
229 static int
230 gifmodevent(mod, type, data)
231 	module_t mod;
232 	int type;
233 	void *data;
234 {
235 	struct gif_softc *sc;
236 
237 	switch (type) {
238 	case MOD_LOAD:
239 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
240 		LIST_INIT(&gif_softc_list);
241 		if_clone_attach(&gif_cloner);
242 
243 #ifdef INET6
244 		ip6_gif_hlim = GIF_HLIM;
245 #endif
246 
247 		break;
248 	case MOD_UNLOAD:
249 		if_clone_detach(&gif_cloner);
250 
251 		mtx_lock(&gif_mtx);
252 		while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) {
253 			LIST_REMOVE(sc, gif_list);
254 			mtx_unlock(&gif_mtx);
255 			gif_destroy(sc);
256 			mtx_lock(&gif_mtx);
257 		}
258 		mtx_unlock(&gif_mtx);
259 		mtx_destroy(&gif_mtx);
260 #ifdef INET6
261 		ip6_gif_hlim = 0;
262 #endif
263 		break;
264 	default:
265 		return EOPNOTSUPP;
266 	}
267 	return 0;
268 }
269 
270 static moduledata_t gif_mod = {
271 	"if_gif",
272 	gifmodevent,
273 	0
274 };
275 
276 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
277 MODULE_VERSION(if_gif, 1);
278 
279 int
280 gif_encapcheck(m, off, proto, arg)
281 	const struct mbuf *m;
282 	int off;
283 	int proto;
284 	void *arg;
285 {
286 	struct ip ip;
287 	struct gif_softc *sc;
288 
289 	sc = (struct gif_softc *)arg;
290 	if (sc == NULL)
291 		return 0;
292 
293 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
294 		return 0;
295 
296 	/* no physical address */
297 	if (!sc->gif_psrc || !sc->gif_pdst)
298 		return 0;
299 
300 	switch (proto) {
301 #ifdef INET
302 	case IPPROTO_IPV4:
303 		break;
304 #endif
305 #ifdef INET6
306 	case IPPROTO_IPV6:
307 		break;
308 #endif
309 	default:
310 		return 0;
311 	}
312 
313 	/* Bail on short packets */
314 	if (m->m_pkthdr.len < sizeof(ip))
315 		return 0;
316 
317 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
318 
319 	switch (ip.ip_v) {
320 #ifdef INET
321 	case 4:
322 		if (sc->gif_psrc->sa_family != AF_INET ||
323 		    sc->gif_pdst->sa_family != AF_INET)
324 			return 0;
325 		return gif_encapcheck4(m, off, proto, arg);
326 #endif
327 #ifdef INET6
328 	case 6:
329 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
330 			return 0;
331 		if (sc->gif_psrc->sa_family != AF_INET6 ||
332 		    sc->gif_pdst->sa_family != AF_INET6)
333 			return 0;
334 		return gif_encapcheck6(m, off, proto, arg);
335 #endif
336 	default:
337 		return 0;
338 	}
339 }
340 
341 int
342 gif_output(ifp, m, dst, rt)
343 	struct ifnet *ifp;
344 	struct mbuf *m;
345 	struct sockaddr *dst;
346 	struct rtentry *rt;	/* added in net2 */
347 {
348 	struct gif_softc *sc = ifp->if_softc;
349 	struct m_tag *mtag;
350 	int error = 0;
351 	int gif_called;
352 	u_int32_t af;
353 
354 #ifdef MAC
355 	error = mac_check_ifnet_transmit(ifp, m);
356 	if (error) {
357 		m_freem(m);
358 		goto end;
359 	}
360 #endif
361 
362 	/*
363 	 * gif may cause infinite recursion calls when misconfigured.
364 	 * We'll prevent this by detecting loops.
365 	 *
366 	 * High nesting level may cause stack exhaustion.
367 	 * We'll prevent this by introducing upper limit.
368 	 */
369 	gif_called = 1;
370 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
371 	while (mtag != NULL) {
372 		if (*(struct ifnet **)(mtag + 1) == ifp) {
373 			log(LOG_NOTICE,
374 			    "gif_output: loop detected on %s\n",
375 			    (*(struct ifnet **)(mtag + 1))->if_xname);
376 			m_freem(m);
377 			error = EIO;	/* is there better errno? */
378 			goto end;
379 		}
380 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
381 		gif_called++;
382 	}
383 	if (gif_called > max_gif_nesting) {
384 		log(LOG_NOTICE,
385 		    "gif_output: recursively called too many times(%d)\n",
386 		    gif_called);
387 		m_freem(m);
388 		error = EIO;	/* is there better errno? */
389 		goto end;
390 	}
391 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
392 	    M_NOWAIT);
393 	if (mtag == NULL) {
394 		m_freem(m);
395 		error = ENOMEM;
396 		goto end;
397 	}
398 	*(struct ifnet **)(mtag + 1) = ifp;
399 	m_tag_prepend(m, mtag);
400 
401 	m->m_flags &= ~(M_BCAST|M_MCAST);
402 	if (!(ifp->if_flags & IFF_UP) ||
403 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
404 		m_freem(m);
405 		error = ENETDOWN;
406 		goto end;
407 	}
408 
409 	/* BPF writes need to be handled specially. */
410 	if (dst->sa_family == AF_UNSPEC) {
411 		bcopy(dst->sa_data, &af, sizeof(af));
412 		dst->sa_family = af;
413 	}
414 
415 	if (ifp->if_bpf) {
416 		af = dst->sa_family;
417 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
418 	}
419 	ifp->if_opackets++;
420 	ifp->if_obytes += m->m_pkthdr.len;
421 
422 	/* inner AF-specific encapsulation */
423 
424 	/* XXX should we check if our outer source is legal? */
425 
426 	/* dispatch to output logic based on outer AF */
427 	switch (sc->gif_psrc->sa_family) {
428 #ifdef INET
429 	case AF_INET:
430 		error = in_gif_output(ifp, dst->sa_family, m);
431 		break;
432 #endif
433 #ifdef INET6
434 	case AF_INET6:
435 		error = in6_gif_output(ifp, dst->sa_family, m);
436 		break;
437 #endif
438 	default:
439 		m_freem(m);
440 		error = ENETDOWN;
441 		goto end;
442 	}
443 
444   end:
445 	if (error)
446 		ifp->if_oerrors++;
447 	return error;
448 }
449 
450 void
451 gif_input(m, af, ifp)
452 	struct mbuf *m;
453 	int af;
454 	struct ifnet *ifp;
455 {
456 	int isr;
457 
458 	if (ifp == NULL) {
459 		/* just in case */
460 		m_freem(m);
461 		return;
462 	}
463 
464 	m->m_pkthdr.rcvif = ifp;
465 
466 #ifdef MAC
467 	mac_create_mbuf_from_ifnet(ifp, m);
468 #endif
469 
470 	if (ifp->if_bpf) {
471 		u_int32_t af1 = af;
472 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
473 	}
474 
475 	if (ng_gif_input_p != NULL) {
476 		(*ng_gif_input_p)(ifp, &m, af);
477 		if (m == NULL)
478 			return;
479 	}
480 
481 	/*
482 	 * Put the packet to the network layer input queue according to the
483 	 * specified address family.
484 	 * Note: older versions of gif_input directly called network layer
485 	 * input functions, e.g. ip6_input, here.  We changed the policy to
486 	 * prevent too many recursive calls of such input functions, which
487 	 * might cause kernel panic.  But the change may introduce another
488 	 * problem; if the input queue is full, packets are discarded.
489 	 * The kernel stack overflow really happened, and we believed
490 	 * queue-full rarely occurs, so we changed the policy.
491 	 */
492 	switch (af) {
493 #ifdef INET
494 	case AF_INET:
495 		isr = NETISR_IP;
496 		break;
497 #endif
498 #ifdef INET6
499 	case AF_INET6:
500 		isr = NETISR_IPV6;
501 		break;
502 #endif
503 	default:
504 		if (ng_gif_input_orphan_p != NULL)
505 			(*ng_gif_input_orphan_p)(ifp, m, af);
506 		else
507 			m_freem(m);
508 		return;
509 	}
510 
511 	ifp->if_ipackets++;
512 	ifp->if_ibytes += m->m_pkthdr.len;
513 	netisr_dispatch(isr, m);
514 }
515 
516 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
517 int
518 gif_ioctl(ifp, cmd, data)
519 	struct ifnet *ifp;
520 	u_long cmd;
521 	caddr_t data;
522 {
523 	struct gif_softc *sc  = ifp->if_softc;
524 	struct ifreq     *ifr = (struct ifreq*)data;
525 	int error = 0, size;
526 	struct sockaddr *dst, *src;
527 #ifdef	SIOCSIFMTU /* xxx */
528 	u_long mtu;
529 #endif
530 
531 	switch (cmd) {
532 	case SIOCSIFADDR:
533 		ifp->if_flags |= IFF_UP;
534 		break;
535 
536 	case SIOCSIFDSTADDR:
537 		break;
538 
539 	case SIOCADDMULTI:
540 	case SIOCDELMULTI:
541 		break;
542 
543 #ifdef	SIOCSIFMTU /* xxx */
544 	case SIOCGIFMTU:
545 		break;
546 
547 	case SIOCSIFMTU:
548 		mtu = ifr->ifr_mtu;
549 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
550 			return (EINVAL);
551 		ifp->if_mtu = mtu;
552 		break;
553 #endif /* SIOCSIFMTU */
554 
555 #ifdef INET
556 	case SIOCSIFPHYADDR:
557 #endif
558 #ifdef INET6
559 	case SIOCSIFPHYADDR_IN6:
560 #endif /* INET6 */
561 	case SIOCSLIFPHYADDR:
562 		switch (cmd) {
563 #ifdef INET
564 		case SIOCSIFPHYADDR:
565 			src = (struct sockaddr *)
566 				&(((struct in_aliasreq *)data)->ifra_addr);
567 			dst = (struct sockaddr *)
568 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
569 			break;
570 #endif
571 #ifdef INET6
572 		case SIOCSIFPHYADDR_IN6:
573 			src = (struct sockaddr *)
574 				&(((struct in6_aliasreq *)data)->ifra_addr);
575 			dst = (struct sockaddr *)
576 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
577 			break;
578 #endif
579 		case SIOCSLIFPHYADDR:
580 			src = (struct sockaddr *)
581 				&(((struct if_laddrreq *)data)->addr);
582 			dst = (struct sockaddr *)
583 				&(((struct if_laddrreq *)data)->dstaddr);
584 			break;
585 		default:
586 			return EINVAL;
587 		}
588 
589 		/* sa_family must be equal */
590 		if (src->sa_family != dst->sa_family)
591 			return EINVAL;
592 
593 		/* validate sa_len */
594 		switch (src->sa_family) {
595 #ifdef INET
596 		case AF_INET:
597 			if (src->sa_len != sizeof(struct sockaddr_in))
598 				return EINVAL;
599 			break;
600 #endif
601 #ifdef INET6
602 		case AF_INET6:
603 			if (src->sa_len != sizeof(struct sockaddr_in6))
604 				return EINVAL;
605 			break;
606 #endif
607 		default:
608 			return EAFNOSUPPORT;
609 		}
610 		switch (dst->sa_family) {
611 #ifdef INET
612 		case AF_INET:
613 			if (dst->sa_len != sizeof(struct sockaddr_in))
614 				return EINVAL;
615 			break;
616 #endif
617 #ifdef INET6
618 		case AF_INET6:
619 			if (dst->sa_len != sizeof(struct sockaddr_in6))
620 				return EINVAL;
621 			break;
622 #endif
623 		default:
624 			return EAFNOSUPPORT;
625 		}
626 
627 		/* check sa_family looks sane for the cmd */
628 		switch (cmd) {
629 		case SIOCSIFPHYADDR:
630 			if (src->sa_family == AF_INET)
631 				break;
632 			return EAFNOSUPPORT;
633 #ifdef INET6
634 		case SIOCSIFPHYADDR_IN6:
635 			if (src->sa_family == AF_INET6)
636 				break;
637 			return EAFNOSUPPORT;
638 #endif /* INET6 */
639 		case SIOCSLIFPHYADDR:
640 			/* checks done in the above */
641 			break;
642 		}
643 
644 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
645 		break;
646 
647 #ifdef SIOCDIFPHYADDR
648 	case SIOCDIFPHYADDR:
649 		gif_delete_tunnel(GIF2IFP(sc));
650 		break;
651 #endif
652 
653 	case SIOCGIFPSRCADDR:
654 #ifdef INET6
655 	case SIOCGIFPSRCADDR_IN6:
656 #endif /* INET6 */
657 		if (sc->gif_psrc == NULL) {
658 			error = EADDRNOTAVAIL;
659 			goto bad;
660 		}
661 		src = sc->gif_psrc;
662 		switch (cmd) {
663 #ifdef INET
664 		case SIOCGIFPSRCADDR:
665 			dst = &ifr->ifr_addr;
666 			size = sizeof(ifr->ifr_addr);
667 			break;
668 #endif /* INET */
669 #ifdef INET6
670 		case SIOCGIFPSRCADDR_IN6:
671 			dst = (struct sockaddr *)
672 				&(((struct in6_ifreq *)data)->ifr_addr);
673 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
674 			break;
675 #endif /* INET6 */
676 		default:
677 			error = EADDRNOTAVAIL;
678 			goto bad;
679 		}
680 		if (src->sa_len > size)
681 			return EINVAL;
682 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
683 #ifdef INET6
684 		if (dst->sa_family == AF_INET6) {
685 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
686 			if (error != 0)
687 				return (error);
688 		}
689 #endif
690 		break;
691 
692 	case SIOCGIFPDSTADDR:
693 #ifdef INET6
694 	case SIOCGIFPDSTADDR_IN6:
695 #endif /* INET6 */
696 		if (sc->gif_pdst == NULL) {
697 			error = EADDRNOTAVAIL;
698 			goto bad;
699 		}
700 		src = sc->gif_pdst;
701 		switch (cmd) {
702 #ifdef INET
703 		case SIOCGIFPDSTADDR:
704 			dst = &ifr->ifr_addr;
705 			size = sizeof(ifr->ifr_addr);
706 			break;
707 #endif /* INET */
708 #ifdef INET6
709 		case SIOCGIFPDSTADDR_IN6:
710 			dst = (struct sockaddr *)
711 				&(((struct in6_ifreq *)data)->ifr_addr);
712 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
713 			break;
714 #endif /* INET6 */
715 		default:
716 			error = EADDRNOTAVAIL;
717 			goto bad;
718 		}
719 		if (src->sa_len > size)
720 			return EINVAL;
721 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
722 #ifdef INET6
723 		if (dst->sa_family == AF_INET6) {
724 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
725 			if (error != 0)
726 				return (error);
727 		}
728 #endif
729 		break;
730 
731 	case SIOCGLIFPHYADDR:
732 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
733 			error = EADDRNOTAVAIL;
734 			goto bad;
735 		}
736 
737 		/* copy src */
738 		src = sc->gif_psrc;
739 		dst = (struct sockaddr *)
740 			&(((struct if_laddrreq *)data)->addr);
741 		size = sizeof(((struct if_laddrreq *)data)->addr);
742 		if (src->sa_len > size)
743 			return EINVAL;
744 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
745 
746 		/* copy dst */
747 		src = sc->gif_pdst;
748 		dst = (struct sockaddr *)
749 			&(((struct if_laddrreq *)data)->dstaddr);
750 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
751 		if (src->sa_len > size)
752 			return EINVAL;
753 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
754 		break;
755 
756 	case SIOCSIFFLAGS:
757 		/* if_ioctl() takes care of it */
758 		break;
759 
760 	default:
761 		error = EINVAL;
762 		break;
763 	}
764  bad:
765 	return error;
766 }
767 
768 /*
769  * XXXRW: There's a general event-ordering issue here: the code to check
770  * if a given tunnel is already present happens before we perform a
771  * potentially blocking setup of the tunnel.  This code needs to be
772  * re-ordered so that the check and replacement can be atomic using
773  * a mutex.
774  */
775 int
776 gif_set_tunnel(ifp, src, dst)
777 	struct ifnet *ifp;
778 	struct sockaddr *src;
779 	struct sockaddr *dst;
780 {
781 	struct gif_softc *sc = ifp->if_softc;
782 	struct gif_softc *sc2;
783 	struct sockaddr *osrc, *odst, *sa;
784 	int s;
785 	int error = 0;
786 
787 	s = splnet();
788 
789 	mtx_lock(&gif_mtx);
790 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
791 		if (sc2 == sc)
792 			continue;
793 		if (!sc2->gif_pdst || !sc2->gif_psrc)
794 			continue;
795 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
796 		    sc2->gif_pdst->sa_len != dst->sa_len ||
797 		    sc2->gif_psrc->sa_family != src->sa_family ||
798 		    sc2->gif_psrc->sa_len != src->sa_len)
799 			continue;
800 
801 		/*
802 		 * Disallow parallel tunnels unless instructed
803 		 * otherwise.
804 		 */
805 		if (!parallel_tunnels &&
806 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
807 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
808 			error = EADDRNOTAVAIL;
809 			mtx_unlock(&gif_mtx);
810 			goto bad;
811 		}
812 
813 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
814 	}
815 	mtx_unlock(&gif_mtx);
816 
817 	/* XXX we can detach from both, but be polite just in case */
818 	if (sc->gif_psrc)
819 		switch (sc->gif_psrc->sa_family) {
820 #ifdef INET
821 		case AF_INET:
822 			(void)in_gif_detach(sc);
823 			break;
824 #endif
825 #ifdef INET6
826 		case AF_INET6:
827 			(void)in6_gif_detach(sc);
828 			break;
829 #endif
830 		}
831 
832 	osrc = sc->gif_psrc;
833 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
834 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
835 	sc->gif_psrc = sa;
836 
837 	odst = sc->gif_pdst;
838 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
839 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
840 	sc->gif_pdst = sa;
841 
842 	switch (sc->gif_psrc->sa_family) {
843 #ifdef INET
844 	case AF_INET:
845 		error = in_gif_attach(sc);
846 		break;
847 #endif
848 #ifdef INET6
849 	case AF_INET6:
850 		/*
851 		 * Check validity of the scope zone ID of the addresses, and
852 		 * convert it into the kernel internal form if necessary.
853 		 */
854 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
855 		if (error != 0)
856 			break;
857 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
858 		if (error != 0)
859 			break;
860 		error = in6_gif_attach(sc);
861 		break;
862 #endif
863 	}
864 	if (error) {
865 		/* rollback */
866 		free((caddr_t)sc->gif_psrc, M_IFADDR);
867 		free((caddr_t)sc->gif_pdst, M_IFADDR);
868 		sc->gif_psrc = osrc;
869 		sc->gif_pdst = odst;
870 		goto bad;
871 	}
872 
873 	if (osrc)
874 		free((caddr_t)osrc, M_IFADDR);
875 	if (odst)
876 		free((caddr_t)odst, M_IFADDR);
877 
878 	if (sc->gif_psrc && sc->gif_pdst)
879 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
880 	else
881 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
882 	splx(s);
883 
884 	return 0;
885 
886  bad:
887 	if (sc->gif_psrc && sc->gif_pdst)
888 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
889 	else
890 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
891 	splx(s);
892 
893 	return error;
894 }
895 
896 void
897 gif_delete_tunnel(ifp)
898 	struct ifnet *ifp;
899 {
900 	struct gif_softc *sc = ifp->if_softc;
901 	int s;
902 
903 	s = splnet();
904 
905 	if (sc->gif_psrc) {
906 		free((caddr_t)sc->gif_psrc, M_IFADDR);
907 		sc->gif_psrc = NULL;
908 	}
909 	if (sc->gif_pdst) {
910 		free((caddr_t)sc->gif_pdst, M_IFADDR);
911 		sc->gif_pdst = NULL;
912 	}
913 	/* it is safe to detach from both */
914 #ifdef INET
915 	(void)in_gif_detach(sc);
916 #endif
917 #ifdef INET6
918 	(void)in6_gif_detach(sc);
919 #endif
920 
921 	if (sc->gif_psrc && sc->gif_pdst)
922 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
923 	else
924 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
925 	splx(s);
926 }
927