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