if_gif.c revision 151266
1/*	$FreeBSD: head/sys/net/if_gif.c 151266 2005-10-12 19:52:16Z thompsa $	*/
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 */
93static struct mtx gif_mtx;
94static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
95static LIST_HEAD(, gif_softc) gif_softc_list;
96
97void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
98void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
99void	(*ng_gif_attach_p)(struct ifnet *ifp);
100void	(*ng_gif_detach_p)(struct ifnet *ifp);
101
102static int	gif_clone_create(struct if_clone *, int);
103static void	gif_clone_destroy(struct ifnet *);
104
105IFC_SIMPLE_DECLARE(gif, 0);
106
107static int gifmodevent(module_t, int, void *);
108
109SYSCTL_DECL(_net_link);
110SYSCTL_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
123static int max_gif_nesting = MAX_GIF_NEST;
124SYSCTL_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
133static int parallel_tunnels = 1;
134#else
135static int parallel_tunnels = 0;
136#endif
137SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
138    &parallel_tunnels, 0, "Allow parallel tunnels?");
139
140static int
141gif_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
165void
166gifattach0(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
188static void
189gif_clone_destroy(ifp)
190	struct ifnet *ifp;
191{
192	int err;
193	struct gif_softc *sc = ifp->if_softc;
194
195	mtx_lock(&gif_mtx);
196	LIST_REMOVE(sc, gif_list);
197	mtx_unlock(&gif_mtx);
198
199	gif_delete_tunnel(ifp);
200#ifdef INET6
201	if (sc->encap_cookie6 != NULL) {
202		err = encap_detach(sc->encap_cookie6);
203		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
204	}
205#endif
206#ifdef INET
207	if (sc->encap_cookie4 != NULL) {
208		err = encap_detach(sc->encap_cookie4);
209		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
210	}
211#endif
212
213	if (ng_gif_detach_p != NULL)
214		(*ng_gif_detach_p)(ifp);
215	bpfdetach(ifp);
216	if_detach(ifp);
217	if_free(ifp);
218
219	free(sc, M_GIF);
220}
221
222static int
223gifmodevent(mod, type, data)
224	module_t mod;
225	int type;
226	void *data;
227{
228	struct gif_softc *sc;
229
230	switch (type) {
231	case MOD_LOAD:
232		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
233		LIST_INIT(&gif_softc_list);
234		if_clone_attach(&gif_cloner);
235
236#ifdef INET6
237		ip6_gif_hlim = GIF_HLIM;
238#endif
239
240		break;
241	case MOD_UNLOAD:
242		if_clone_detach(&gif_cloner);
243
244		mtx_lock(&gif_mtx);
245		while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) {
246			mtx_unlock(&gif_mtx);
247			ifc_simple_destroy(&gif_cloner, GIF2IFP(sc));
248			mtx_lock(&gif_mtx);
249		}
250		mtx_unlock(&gif_mtx);
251		mtx_destroy(&gif_mtx);
252#ifdef INET6
253		ip6_gif_hlim = 0;
254#endif
255		break;
256	default:
257		return EOPNOTSUPP;
258	}
259	return 0;
260}
261
262static moduledata_t gif_mod = {
263	"if_gif",
264	gifmodevent,
265	0
266};
267
268DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
269MODULE_VERSION(if_gif, 1);
270
271int
272gif_encapcheck(m, off, proto, arg)
273	const struct mbuf *m;
274	int off;
275	int proto;
276	void *arg;
277{
278	struct ip ip;
279	struct gif_softc *sc;
280
281	sc = (struct gif_softc *)arg;
282	if (sc == NULL)
283		return 0;
284
285	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
286		return 0;
287
288	/* no physical address */
289	if (!sc->gif_psrc || !sc->gif_pdst)
290		return 0;
291
292	switch (proto) {
293#ifdef INET
294	case IPPROTO_IPV4:
295		break;
296#endif
297#ifdef INET6
298	case IPPROTO_IPV6:
299		break;
300#endif
301	default:
302		return 0;
303	}
304
305	/* Bail on short packets */
306	if (m->m_pkthdr.len < sizeof(ip))
307		return 0;
308
309	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
310
311	switch (ip.ip_v) {
312#ifdef INET
313	case 4:
314		if (sc->gif_psrc->sa_family != AF_INET ||
315		    sc->gif_pdst->sa_family != AF_INET)
316			return 0;
317		return gif_encapcheck4(m, off, proto, arg);
318#endif
319#ifdef INET6
320	case 6:
321		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
322			return 0;
323		if (sc->gif_psrc->sa_family != AF_INET6 ||
324		    sc->gif_pdst->sa_family != AF_INET6)
325			return 0;
326		return gif_encapcheck6(m, off, proto, arg);
327#endif
328	default:
329		return 0;
330	}
331}
332
333int
334gif_output(ifp, m, dst, rt)
335	struct ifnet *ifp;
336	struct mbuf *m;
337	struct sockaddr *dst;
338	struct rtentry *rt;	/* added in net2 */
339{
340	struct gif_softc *sc = ifp->if_softc;
341	struct m_tag *mtag;
342	int error = 0;
343	int gif_called;
344	u_int32_t af;
345
346#ifdef MAC
347	error = mac_check_ifnet_transmit(ifp, m);
348	if (error) {
349		m_freem(m);
350		goto end;
351	}
352#endif
353
354	/*
355	 * gif may cause infinite recursion calls when misconfigured.
356	 * We'll prevent this by detecting loops.
357	 *
358	 * High nesting level may cause stack exhaustion.
359	 * We'll prevent this by introducing upper limit.
360	 */
361	gif_called = 1;
362	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
363	while (mtag != NULL) {
364		if (*(struct ifnet **)(mtag + 1) == ifp) {
365			log(LOG_NOTICE,
366			    "gif_output: loop detected on %s\n",
367			    (*(struct ifnet **)(mtag + 1))->if_xname);
368			m_freem(m);
369			error = EIO;	/* is there better errno? */
370			goto end;
371		}
372		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
373		gif_called++;
374	}
375	if (gif_called > max_gif_nesting) {
376		log(LOG_NOTICE,
377		    "gif_output: recursively called too many times(%d)\n",
378		    gif_called);
379		m_freem(m);
380		error = EIO;	/* is there better errno? */
381		goto end;
382	}
383	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
384	    M_NOWAIT);
385	if (mtag == NULL) {
386		m_freem(m);
387		error = ENOMEM;
388		goto end;
389	}
390	*(struct ifnet **)(mtag + 1) = ifp;
391	m_tag_prepend(m, mtag);
392
393	m->m_flags &= ~(M_BCAST|M_MCAST);
394	if (!(ifp->if_flags & IFF_UP) ||
395	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
396		m_freem(m);
397		error = ENETDOWN;
398		goto end;
399	}
400
401	/* BPF writes need to be handled specially. */
402	if (dst->sa_family == AF_UNSPEC) {
403		bcopy(dst->sa_data, &af, sizeof(af));
404		dst->sa_family = af;
405	}
406
407	if (ifp->if_bpf) {
408		af = dst->sa_family;
409		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
410	}
411	ifp->if_opackets++;
412	ifp->if_obytes += m->m_pkthdr.len;
413
414	/* inner AF-specific encapsulation */
415
416	/* XXX should we check if our outer source is legal? */
417
418	/* dispatch to output logic based on outer AF */
419	switch (sc->gif_psrc->sa_family) {
420#ifdef INET
421	case AF_INET:
422		error = in_gif_output(ifp, dst->sa_family, m);
423		break;
424#endif
425#ifdef INET6
426	case AF_INET6:
427		error = in6_gif_output(ifp, dst->sa_family, m);
428		break;
429#endif
430	default:
431		m_freem(m);
432		error = ENETDOWN;
433		goto end;
434	}
435
436  end:
437	if (error)
438		ifp->if_oerrors++;
439	return error;
440}
441
442void
443gif_input(m, af, ifp)
444	struct mbuf *m;
445	int af;
446	struct ifnet *ifp;
447{
448	int isr;
449
450	if (ifp == NULL) {
451		/* just in case */
452		m_freem(m);
453		return;
454	}
455
456	m->m_pkthdr.rcvif = ifp;
457
458#ifdef MAC
459	mac_create_mbuf_from_ifnet(ifp, m);
460#endif
461
462	if (ifp->if_bpf) {
463		u_int32_t af1 = af;
464		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
465	}
466
467	if (ng_gif_input_p != NULL) {
468		(*ng_gif_input_p)(ifp, &m, af);
469		if (m == NULL)
470			return;
471	}
472
473	/*
474	 * Put the packet to the network layer input queue according to the
475	 * specified address family.
476	 * Note: older versions of gif_input directly called network layer
477	 * input functions, e.g. ip6_input, here.  We changed the policy to
478	 * prevent too many recursive calls of such input functions, which
479	 * might cause kernel panic.  But the change may introduce another
480	 * problem; if the input queue is full, packets are discarded.
481	 * The kernel stack overflow really happened, and we believed
482	 * queue-full rarely occurs, so we changed the policy.
483	 */
484	switch (af) {
485#ifdef INET
486	case AF_INET:
487		isr = NETISR_IP;
488		break;
489#endif
490#ifdef INET6
491	case AF_INET6:
492		isr = NETISR_IPV6;
493		break;
494#endif
495	default:
496		if (ng_gif_input_orphan_p != NULL)
497			(*ng_gif_input_orphan_p)(ifp, m, af);
498		else
499			m_freem(m);
500		return;
501	}
502
503	ifp->if_ipackets++;
504	ifp->if_ibytes += m->m_pkthdr.len;
505	netisr_dispatch(isr, m);
506}
507
508/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
509int
510gif_ioctl(ifp, cmd, data)
511	struct ifnet *ifp;
512	u_long cmd;
513	caddr_t data;
514{
515	struct gif_softc *sc  = ifp->if_softc;
516	struct ifreq     *ifr = (struct ifreq*)data;
517	int error = 0, size;
518	struct sockaddr *dst, *src;
519#ifdef	SIOCSIFMTU /* xxx */
520	u_long mtu;
521#endif
522
523	switch (cmd) {
524	case SIOCSIFADDR:
525		ifp->if_flags |= IFF_UP;
526		break;
527
528	case SIOCSIFDSTADDR:
529		break;
530
531	case SIOCADDMULTI:
532	case SIOCDELMULTI:
533		break;
534
535#ifdef	SIOCSIFMTU /* xxx */
536	case SIOCGIFMTU:
537		break;
538
539	case SIOCSIFMTU:
540		mtu = ifr->ifr_mtu;
541		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
542			return (EINVAL);
543		ifp->if_mtu = mtu;
544		break;
545#endif /* SIOCSIFMTU */
546
547#ifdef INET
548	case SIOCSIFPHYADDR:
549#endif
550#ifdef INET6
551	case SIOCSIFPHYADDR_IN6:
552#endif /* INET6 */
553	case SIOCSLIFPHYADDR:
554		switch (cmd) {
555#ifdef INET
556		case SIOCSIFPHYADDR:
557			src = (struct sockaddr *)
558				&(((struct in_aliasreq *)data)->ifra_addr);
559			dst = (struct sockaddr *)
560				&(((struct in_aliasreq *)data)->ifra_dstaddr);
561			break;
562#endif
563#ifdef INET6
564		case SIOCSIFPHYADDR_IN6:
565			src = (struct sockaddr *)
566				&(((struct in6_aliasreq *)data)->ifra_addr);
567			dst = (struct sockaddr *)
568				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
569			break;
570#endif
571		case SIOCSLIFPHYADDR:
572			src = (struct sockaddr *)
573				&(((struct if_laddrreq *)data)->addr);
574			dst = (struct sockaddr *)
575				&(((struct if_laddrreq *)data)->dstaddr);
576			break;
577		default:
578			return EINVAL;
579		}
580
581		/* sa_family must be equal */
582		if (src->sa_family != dst->sa_family)
583			return EINVAL;
584
585		/* validate sa_len */
586		switch (src->sa_family) {
587#ifdef INET
588		case AF_INET:
589			if (src->sa_len != sizeof(struct sockaddr_in))
590				return EINVAL;
591			break;
592#endif
593#ifdef INET6
594		case AF_INET6:
595			if (src->sa_len != sizeof(struct sockaddr_in6))
596				return EINVAL;
597			break;
598#endif
599		default:
600			return EAFNOSUPPORT;
601		}
602		switch (dst->sa_family) {
603#ifdef INET
604		case AF_INET:
605			if (dst->sa_len != sizeof(struct sockaddr_in))
606				return EINVAL;
607			break;
608#endif
609#ifdef INET6
610		case AF_INET6:
611			if (dst->sa_len != sizeof(struct sockaddr_in6))
612				return EINVAL;
613			break;
614#endif
615		default:
616			return EAFNOSUPPORT;
617		}
618
619		/* check sa_family looks sane for the cmd */
620		switch (cmd) {
621		case SIOCSIFPHYADDR:
622			if (src->sa_family == AF_INET)
623				break;
624			return EAFNOSUPPORT;
625#ifdef INET6
626		case SIOCSIFPHYADDR_IN6:
627			if (src->sa_family == AF_INET6)
628				break;
629			return EAFNOSUPPORT;
630#endif /* INET6 */
631		case SIOCSLIFPHYADDR:
632			/* checks done in the above */
633			break;
634		}
635
636		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
637		break;
638
639#ifdef SIOCDIFPHYADDR
640	case SIOCDIFPHYADDR:
641		gif_delete_tunnel(GIF2IFP(sc));
642		break;
643#endif
644
645	case SIOCGIFPSRCADDR:
646#ifdef INET6
647	case SIOCGIFPSRCADDR_IN6:
648#endif /* INET6 */
649		if (sc->gif_psrc == NULL) {
650			error = EADDRNOTAVAIL;
651			goto bad;
652		}
653		src = sc->gif_psrc;
654		switch (cmd) {
655#ifdef INET
656		case SIOCGIFPSRCADDR:
657			dst = &ifr->ifr_addr;
658			size = sizeof(ifr->ifr_addr);
659			break;
660#endif /* INET */
661#ifdef INET6
662		case SIOCGIFPSRCADDR_IN6:
663			dst = (struct sockaddr *)
664				&(((struct in6_ifreq *)data)->ifr_addr);
665			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
666			break;
667#endif /* INET6 */
668		default:
669			error = EADDRNOTAVAIL;
670			goto bad;
671		}
672		if (src->sa_len > size)
673			return EINVAL;
674		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
675#ifdef INET6
676		if (dst->sa_family == AF_INET6) {
677			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
678			if (error != 0)
679				return (error);
680		}
681#endif
682		break;
683
684	case SIOCGIFPDSTADDR:
685#ifdef INET6
686	case SIOCGIFPDSTADDR_IN6:
687#endif /* INET6 */
688		if (sc->gif_pdst == NULL) {
689			error = EADDRNOTAVAIL;
690			goto bad;
691		}
692		src = sc->gif_pdst;
693		switch (cmd) {
694#ifdef INET
695		case SIOCGIFPDSTADDR:
696			dst = &ifr->ifr_addr;
697			size = sizeof(ifr->ifr_addr);
698			break;
699#endif /* INET */
700#ifdef INET6
701		case SIOCGIFPDSTADDR_IN6:
702			dst = (struct sockaddr *)
703				&(((struct in6_ifreq *)data)->ifr_addr);
704			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
705			break;
706#endif /* INET6 */
707		default:
708			error = EADDRNOTAVAIL;
709			goto bad;
710		}
711		if (src->sa_len > size)
712			return EINVAL;
713		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
714#ifdef INET6
715		if (dst->sa_family == AF_INET6) {
716			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
717			if (error != 0)
718				return (error);
719		}
720#endif
721		break;
722
723	case SIOCGLIFPHYADDR:
724		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
725			error = EADDRNOTAVAIL;
726			goto bad;
727		}
728
729		/* copy src */
730		src = sc->gif_psrc;
731		dst = (struct sockaddr *)
732			&(((struct if_laddrreq *)data)->addr);
733		size = sizeof(((struct if_laddrreq *)data)->addr);
734		if (src->sa_len > size)
735			return EINVAL;
736		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
737
738		/* copy dst */
739		src = sc->gif_pdst;
740		dst = (struct sockaddr *)
741			&(((struct if_laddrreq *)data)->dstaddr);
742		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
743		if (src->sa_len > size)
744			return EINVAL;
745		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
746		break;
747
748	case SIOCSIFFLAGS:
749		/* if_ioctl() takes care of it */
750		break;
751
752	default:
753		error = EINVAL;
754		break;
755	}
756 bad:
757	return error;
758}
759
760/*
761 * XXXRW: There's a general event-ordering issue here: the code to check
762 * if a given tunnel is already present happens before we perform a
763 * potentially blocking setup of the tunnel.  This code needs to be
764 * re-ordered so that the check and replacement can be atomic using
765 * a mutex.
766 */
767int
768gif_set_tunnel(ifp, src, dst)
769	struct ifnet *ifp;
770	struct sockaddr *src;
771	struct sockaddr *dst;
772{
773	struct gif_softc *sc = ifp->if_softc;
774	struct gif_softc *sc2;
775	struct sockaddr *osrc, *odst, *sa;
776	int s;
777	int error = 0;
778
779	s = splnet();
780
781	mtx_lock(&gif_mtx);
782	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
783		if (sc2 == sc)
784			continue;
785		if (!sc2->gif_pdst || !sc2->gif_psrc)
786			continue;
787		if (sc2->gif_pdst->sa_family != dst->sa_family ||
788		    sc2->gif_pdst->sa_len != dst->sa_len ||
789		    sc2->gif_psrc->sa_family != src->sa_family ||
790		    sc2->gif_psrc->sa_len != src->sa_len)
791			continue;
792
793		/*
794		 * Disallow parallel tunnels unless instructed
795		 * otherwise.
796		 */
797		if (!parallel_tunnels &&
798		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
799		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
800			error = EADDRNOTAVAIL;
801			mtx_unlock(&gif_mtx);
802			goto bad;
803		}
804
805		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
806	}
807	mtx_unlock(&gif_mtx);
808
809	/* XXX we can detach from both, but be polite just in case */
810	if (sc->gif_psrc)
811		switch (sc->gif_psrc->sa_family) {
812#ifdef INET
813		case AF_INET:
814			(void)in_gif_detach(sc);
815			break;
816#endif
817#ifdef INET6
818		case AF_INET6:
819			(void)in6_gif_detach(sc);
820			break;
821#endif
822		}
823
824	osrc = sc->gif_psrc;
825	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
826	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
827	sc->gif_psrc = sa;
828
829	odst = sc->gif_pdst;
830	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
831	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
832	sc->gif_pdst = sa;
833
834	switch (sc->gif_psrc->sa_family) {
835#ifdef INET
836	case AF_INET:
837		error = in_gif_attach(sc);
838		break;
839#endif
840#ifdef INET6
841	case AF_INET6:
842		/*
843		 * Check validity of the scope zone ID of the addresses, and
844		 * convert it into the kernel internal form if necessary.
845		 */
846		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
847		if (error != 0)
848			break;
849		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
850		if (error != 0)
851			break;
852		error = in6_gif_attach(sc);
853		break;
854#endif
855	}
856	if (error) {
857		/* rollback */
858		free((caddr_t)sc->gif_psrc, M_IFADDR);
859		free((caddr_t)sc->gif_pdst, M_IFADDR);
860		sc->gif_psrc = osrc;
861		sc->gif_pdst = odst;
862		goto bad;
863	}
864
865	if (osrc)
866		free((caddr_t)osrc, M_IFADDR);
867	if (odst)
868		free((caddr_t)odst, M_IFADDR);
869
870	if (sc->gif_psrc && sc->gif_pdst)
871		ifp->if_drv_flags |= IFF_DRV_RUNNING;
872	else
873		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
874	splx(s);
875
876	return 0;
877
878 bad:
879	if (sc->gif_psrc && sc->gif_pdst)
880		ifp->if_drv_flags |= IFF_DRV_RUNNING;
881	else
882		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
883	splx(s);
884
885	return error;
886}
887
888void
889gif_delete_tunnel(ifp)
890	struct ifnet *ifp;
891{
892	struct gif_softc *sc = ifp->if_softc;
893	int s;
894
895	s = splnet();
896
897	if (sc->gif_psrc) {
898		free((caddr_t)sc->gif_psrc, M_IFADDR);
899		sc->gif_psrc = NULL;
900	}
901	if (sc->gif_pdst) {
902		free((caddr_t)sc->gif_pdst, M_IFADDR);
903		sc->gif_pdst = NULL;
904	}
905	/* it is safe to detach from both */
906#ifdef INET
907	(void)in_gif_detach(sc);
908#endif
909#ifdef INET6
910	(void)in6_gif_detach(sc);
911#endif
912
913	if (sc->gif_psrc && sc->gif_pdst)
914		ifp->if_drv_flags |= IFF_DRV_RUNNING;
915	else
916		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
917	splx(s);
918}
919