if_gif.c revision 103994
1/*	$FreeBSD: head/sys/net/if_gif.c 103994 2002-09-26 07:22:29Z sobomax $	*/
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/socket.h>
44#include <sys/sockio.h>
45#include <sys/errno.h>
46#include <sys/time.h>
47#include <sys/sysctl.h>
48#include <sys/syslog.h>
49#include <sys/protosw.h>
50#include <sys/conf.h>
51#include <machine/cpu.h>
52
53#include <net/if.h>
54#include <net/if_types.h>
55#include <net/netisr.h>
56#include <net/route.h>
57#include <net/bpf.h>
58
59#include <netinet/in.h>
60#include <netinet/in_systm.h>
61#include <netinet/ip.h>
62#ifdef	INET
63#include <netinet/in_var.h>
64#include <netinet/in_gif.h>
65#include <netinet/ip_var.h>
66#endif	/* INET */
67
68#ifdef INET6
69#ifndef INET
70#include <netinet/in.h>
71#endif
72#include <netinet6/in6_var.h>
73#include <netinet/ip6.h>
74#include <netinet6/ip6_var.h>
75#include <netinet6/in6_gif.h>
76#include <netinet6/ip6protosw.h>
77#endif /* INET6 */
78
79#include <netinet/ip_encap.h>
80#include <net/if_gif.h>
81
82#include <net/net_osdep.h>
83
84#define GIFNAME		"gif"
85
86static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
87static LIST_HEAD(, gif_softc) gif_softc_list;
88
89void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
90void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
91void	(*ng_gif_attach_p)(struct ifnet *ifp);
92void	(*ng_gif_detach_p)(struct ifnet *ifp);
93
94int	gif_clone_create(struct if_clone *, int);
95void	gif_clone_destroy(struct ifnet *);
96
97struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif",
98    gif_clone_create, gif_clone_destroy, 0, IF_MAXUNIT);
99
100static int gifmodevent(module_t, int, void *);
101void gif_delete_tunnel(struct gif_softc *);
102static int gif_encapcheck(const struct mbuf *, int, int, void *);
103
104#ifdef INET
105extern  struct domain inetdomain;
106struct protosw in_gif_protosw =
107{ SOCK_RAW,	&inetdomain,	0/*IPPROTO_IPV[46]*/,	PR_ATOMIC|PR_ADDR,
108  in_gif_input,	(pr_output_t*)rip_output, 0,		rip_ctloutput,
109  0,
110  0,		0,		0,		0,
111  &rip_usrreqs
112};
113#endif
114#ifdef INET6
115extern  struct domain inet6domain;
116struct ip6protosw in6_gif_protosw =
117{ SOCK_RAW,	&inet6domain,	0/*IPPROTO_IPV[46]*/,	PR_ATOMIC|PR_ADDR,
118  in6_gif_input, rip6_output,	0,		rip6_ctloutput,
119  0,
120  0,		0,		0,		0,
121  &rip6_usrreqs
122};
123#endif
124
125SYSCTL_DECL(_net_link);
126SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
127    "Generic Tunnel Interface");
128#ifndef MAX_GIF_NEST
129/*
130 * This macro controls the default upper limitation on nesting of gif tunnels.
131 * Since, setting a large value to this macro with a careless configuration
132 * may introduce system crash, we don't allow any nestings by default.
133 * If you need to configure nested gif tunnels, you can define this macro
134 * in your kernel configuration file.  However, if you do so, please be
135 * careful to configure the tunnels so that it won't make a loop.
136 */
137#define MAX_GIF_NEST 1
138#endif
139static int max_gif_nesting = MAX_GIF_NEST;
140SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
141    &max_gif_nesting, 0, "Max nested tunnels");
142
143/*
144 * By default, we disallow creation of multiple tunnels between the same
145 * pair of addresses.  Some applications require this functionality so
146 * we allow control over this check here.
147 */
148#ifdef XBONEHACK
149static int parallel_tunnels = 1;
150#else
151static int parallel_tunnels = 0;
152#endif
153SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
154    &parallel_tunnels, 0, "Allow parallel tunnels?");
155
156int
157gif_clone_create(ifc, unit)
158	struct if_clone *ifc;
159	int unit;
160{
161	struct gif_softc *sc;
162
163	sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
164	bzero(sc, sizeof(struct gif_softc));
165
166	sc->gif_if.if_softc = sc;
167	sc->gif_if.if_name = GIFNAME;
168	sc->gif_if.if_unit = unit;
169
170	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
171#ifdef INET
172	sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
173	    gif_encapcheck, (struct protosw*)&in_gif_protosw, sc);
174	if (sc->encap_cookie4 == NULL) {
175		printf("%s: unable to attach encap4\n", if_name(&sc->gif_if));
176		free(sc, M_GIF);
177		return (EIO);	/* XXX */
178	}
179#endif
180#ifdef INET6
181	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
182	    gif_encapcheck, (struct protosw *)&in6_gif_protosw, sc);
183	if (sc->encap_cookie6 == NULL) {
184		if (sc->encap_cookie4) {
185			encap_detach(sc->encap_cookie4);
186			sc->encap_cookie4 = NULL;
187		}
188		printf("%s: unable to attach encap6\n", if_name(&sc->gif_if));
189		free(sc, M_GIF);
190		return (EIO);	/* XXX */
191	}
192#endif
193
194	sc->gif_if.if_mtu    = GIF_MTU;
195	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
196#if 0
197	/* turn off ingress filter */
198	sc->gif_if.if_flags  |= IFF_LINK2;
199#endif
200	sc->gif_if.if_ioctl  = gif_ioctl;
201	sc->gif_if.if_output = gif_output;
202	sc->gif_if.if_type   = IFT_GIF;
203	sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
204	if_attach(&sc->gif_if);
205	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
206	if (ng_gif_attach_p != NULL)
207		(*ng_gif_attach_p)(&sc->gif_if);
208	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_link);
209	return (0);
210}
211
212void
213gif_clone_destroy(ifp)
214	struct ifnet *ifp;
215{
216	int err;
217	struct gif_softc *sc = ifp->if_softc;
218
219	gif_delete_tunnel(sc);
220	LIST_REMOVE(sc, gif_link);
221	if (sc->encap_cookie4 != NULL) {
222		err = encap_detach(sc->encap_cookie4);
223		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
224	}
225	if (sc->encap_cookie6 != NULL) {
226		err = encap_detach(sc->encap_cookie6);
227		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
228	}
229
230	if (ng_gif_detach_p != NULL)
231		(*ng_gif_detach_p)(ifp);
232	bpfdetach(ifp);
233	if_detach(ifp);
234
235	free(sc, M_GIF);
236}
237
238static int
239gifmodevent(mod, type, data)
240	module_t mod;
241	int type;
242	void *data;
243{
244
245	switch (type) {
246	case MOD_LOAD:
247		LIST_INIT(&gif_softc_list);
248		if_clone_attach(&gif_cloner);
249
250#ifdef INET6
251		ip6_gif_hlim = GIF_HLIM;
252#endif
253
254		break;
255	case MOD_UNLOAD:
256		if_clone_detach(&gif_cloner);
257
258		while (!LIST_EMPTY(&gif_softc_list))
259			gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
260
261#ifdef INET6
262		ip6_gif_hlim = 0;
263#endif
264		break;
265	}
266	return 0;
267}
268
269static moduledata_t gif_mod = {
270	"if_gif",
271	gifmodevent,
272	0
273};
274
275DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
276MODULE_VERSION(if_gif, 1);
277
278static int
279gif_encapcheck(m, off, proto, arg)
280	const struct mbuf *m;
281	int off;
282	int proto;
283	void *arg;
284{
285	struct ip ip;
286	struct gif_softc *sc;
287
288	sc = (struct gif_softc *)arg;
289	if (sc == NULL)
290		return 0;
291
292	if ((sc->gif_if.if_flags & IFF_UP) == 0)
293		return 0;
294
295	/* no physical address */
296	if (!sc->gif_psrc || !sc->gif_pdst)
297		return 0;
298
299	switch (proto) {
300#ifdef INET
301	case IPPROTO_IPV4:
302		break;
303#endif
304#ifdef INET6
305	case IPPROTO_IPV6:
306		break;
307#endif
308	default:
309		return 0;
310	}
311
312	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
313
314	switch (ip.ip_v) {
315#ifdef INET
316	case 4:
317		if (sc->gif_psrc->sa_family != AF_INET ||
318		    sc->gif_pdst->sa_family != AF_INET)
319			return 0;
320		return gif_encapcheck4(m, off, proto, arg);
321#endif
322#ifdef INET6
323	case 6:
324		if (sc->gif_psrc->sa_family != AF_INET6 ||
325		    sc->gif_pdst->sa_family != AF_INET6)
326			return 0;
327		return gif_encapcheck6(m, off, proto, arg);
328#endif
329	default:
330		return 0;
331	}
332}
333
334int
335gif_output(ifp, m, dst, rt)
336	struct ifnet *ifp;
337	struct mbuf *m;
338	struct sockaddr *dst;
339	struct rtentry *rt;	/* added in net2 */
340{
341	struct gif_softc *sc = (struct gif_softc*)ifp;
342	int error = 0;
343	static int called = 0;	/* XXX: MUTEX */
344
345#ifdef MAC
346	error = mac_check_ifnet_transmit(ifp, m);
347	if (error) {
348		m_freem(m);
349		goto end;
350	}
351#endif
352
353	/*
354	 * gif may cause infinite recursion calls when misconfigured.
355	 * We'll prevent this by introducing upper limit.
356	 * XXX: this mechanism may introduce another problem about
357	 *      mutual exclusion of the variable CALLED, especially if we
358	 *      use kernel thread.
359	 */
360	if (++called > max_gif_nesting) {
361		log(LOG_NOTICE,
362		    "gif_output: recursively called too many times(%d)\n",
363		    called);
364		m_freem(m);
365		error = EIO;	/* is there better errno? */
366		goto end;
367	}
368
369	m->m_flags &= ~(M_BCAST|M_MCAST);
370	if (!(ifp->if_flags & IFF_UP) ||
371	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
372		m_freem(m);
373		error = ENETDOWN;
374		goto end;
375	}
376
377	if (ifp->if_bpf) {
378		/*
379		 * We need to prepend the address family as
380		 * a four byte field.  Cons up a dummy header
381		 * to pacify bpf.  This is safe because bpf
382		 * will only read from the mbuf (i.e., it won't
383		 * try to free it or keep a pointer a to it).
384		 */
385		struct mbuf m0;
386		u_int32_t af = dst->sa_family;
387
388		m0.m_next = m;
389		m0.m_len = 4;
390		m0.m_data = (char *)&af;
391
392		bpf_mtap(ifp, &m0);
393	}
394	ifp->if_opackets++;
395	ifp->if_obytes += m->m_pkthdr.len;
396
397	/* inner AF-specific encapsulation */
398
399	/* XXX should we check if our outer source is legal? */
400
401	/* dispatch to output logic based on outer AF */
402	switch (sc->gif_psrc->sa_family) {
403#ifdef INET
404	case AF_INET:
405		error = in_gif_output(ifp, dst->sa_family, m, rt);
406		break;
407#endif
408#ifdef INET6
409	case AF_INET6:
410		error = in6_gif_output(ifp, dst->sa_family, m, rt);
411		break;
412#endif
413	default:
414		m_freem(m);
415		error = ENETDOWN;
416		goto end;
417	}
418
419  end:
420	called = 0;		/* reset recursion counter */
421	if (error)
422		ifp->if_oerrors++;
423	return error;
424}
425
426void
427gif_input(m, af, gifp)
428	struct mbuf *m;
429	int af;
430	struct ifnet *gifp;
431{
432	int isr;
433	struct ifqueue *ifq = 0;
434
435	if (gifp == NULL) {
436		/* just in case */
437		m_freem(m);
438		return;
439	}
440
441	m->m_pkthdr.rcvif = gifp;
442
443#ifdef MAC
444	mac_create_mbuf_from_ifnet(gifp, m);
445#endif
446
447	if (gifp->if_bpf) {
448		/*
449		 * We need to prepend the address family as
450		 * a four byte field.  Cons up a dummy header
451		 * to pacify bpf.  This is safe because bpf
452		 * will only read from the mbuf (i.e., it won't
453		 * try to free it or keep a pointer a to it).
454		 */
455		struct mbuf m0;
456		u_int32_t af1 = af;
457
458		m0.m_next = m;
459		m0.m_len = 4;
460		m0.m_data = (char *)&af1;
461
462		bpf_mtap(gifp, &m0);
463	}
464
465	if (ng_gif_input_p != NULL) {
466		(*ng_gif_input_p)(gifp, &m, af);
467		if (m == NULL)
468			return;
469	}
470
471	/*
472	 * Put the packet to the network layer input queue according to the
473	 * specified address family.
474	 * Note: older versions of gif_input directly called network layer
475	 * input functions, e.g. ip6_input, here.  We changed the policy to
476	 * prevent too many recursive calls of such input functions, which
477	 * might cause kernel panic.  But the change may introduce another
478	 * problem; if the input queue is full, packets are discarded.
479	 * The kernel stack overflow really happened, and we believed
480	 * queue-full rarely occurs, so we changed the policy.
481	 */
482	switch (af) {
483#ifdef INET
484	case AF_INET:
485		ifq = &ipintrq;
486		isr = NETISR_IP;
487		break;
488#endif
489#ifdef INET6
490	case AF_INET6:
491		ifq = &ip6intrq;
492		isr = NETISR_IPV6;
493		break;
494#endif
495	default:
496		if (ng_gif_input_orphan_p != NULL)
497			(*ng_gif_input_orphan_p)(gifp, m, af);
498		else
499			m_freem(m);
500		return;
501	}
502
503	gifp->if_ipackets++;
504	gifp->if_ibytes += m->m_pkthdr.len;
505	(void) IF_HANDOFF(ifq, m, NULL);
506	/* we need schednetisr since the address family may change */
507	schednetisr(isr);
508
509	return;
510}
511
512/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
513int
514gif_ioctl(ifp, cmd, data)
515	struct ifnet *ifp;
516	u_long cmd;
517	caddr_t data;
518{
519	struct gif_softc *sc  = (struct gif_softc*)ifp;
520	struct ifreq     *ifr = (struct ifreq*)data;
521	int error = 0, size;
522	struct sockaddr *dst, *src;
523	struct sockaddr *sa;
524	struct ifnet *ifp2;
525	struct gif_softc *sc2;
526
527	switch (cmd) {
528	case SIOCSIFADDR:
529		break;
530
531	case SIOCSIFDSTADDR:
532		break;
533
534	case SIOCADDMULTI:
535	case SIOCDELMULTI:
536		break;
537
538#ifdef	SIOCSIFMTU /* xxx */
539	case SIOCGIFMTU:
540		break;
541
542	case SIOCSIFMTU:
543		{
544			u_long mtu;
545			mtu = ifr->ifr_mtu;
546			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
547				return (EINVAL);
548			}
549			ifp->if_mtu = mtu;
550		}
551		break;
552#endif /* SIOCSIFMTU */
553
554	case SIOCSIFPHYADDR:
555#ifdef INET6
556	case SIOCSIFPHYADDR_IN6:
557#endif /* INET6 */
558	case SIOCSLIFPHYADDR:
559		switch (cmd) {
560#ifdef INET
561		case SIOCSIFPHYADDR:
562			src = (struct sockaddr *)
563				&(((struct in_aliasreq *)data)->ifra_addr);
564			dst = (struct sockaddr *)
565				&(((struct in_aliasreq *)data)->ifra_dstaddr);
566			break;
567#endif
568#ifdef INET6
569		case SIOCSIFPHYADDR_IN6:
570			src = (struct sockaddr *)
571				&(((struct in6_aliasreq *)data)->ifra_addr);
572			dst = (struct sockaddr *)
573				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
574			break;
575#endif
576		case SIOCSLIFPHYADDR:
577			src = (struct sockaddr *)
578				&(((struct if_laddrreq *)data)->addr);
579			dst = (struct sockaddr *)
580				&(((struct if_laddrreq *)data)->dstaddr);
581		default:
582			error = EADDRNOTAVAIL;
583			goto bad;
584		}
585
586		/* sa_family must be equal */
587		if (src->sa_family != dst->sa_family)
588			return EINVAL;
589
590		/* validate sa_len */
591		switch (src->sa_family) {
592#ifdef INET
593		case AF_INET:
594			if (src->sa_len != sizeof(struct sockaddr_in))
595				return EINVAL;
596			break;
597#endif
598#ifdef INET6
599		case AF_INET6:
600			if (src->sa_len != sizeof(struct sockaddr_in6))
601				return EINVAL;
602			break;
603#endif
604		default:
605			return EAFNOSUPPORT;
606		}
607		switch (dst->sa_family) {
608#ifdef INET
609		case AF_INET:
610			if (dst->sa_len != sizeof(struct sockaddr_in))
611				return EINVAL;
612			break;
613#endif
614#ifdef INET6
615		case AF_INET6:
616			if (dst->sa_len != sizeof(struct sockaddr_in6))
617				return EINVAL;
618			break;
619#endif
620		default:
621			return EAFNOSUPPORT;
622		}
623
624		/* check sa_family looks sane for the cmd */
625		switch (cmd) {
626		case SIOCSIFPHYADDR:
627			if (src->sa_family == AF_INET)
628				break;
629			return EAFNOSUPPORT;
630#ifdef INET6
631		case SIOCSIFPHYADDR_IN6:
632			if (src->sa_family == AF_INET6)
633				break;
634			return EAFNOSUPPORT;
635#endif /* INET6 */
636		case SIOCSLIFPHYADDR:
637			/* checks done in the above */
638			break;
639		}
640
641		TAILQ_FOREACH(ifp2, &ifnet, if_link) {
642			if (strcmp(ifp2->if_name, GIFNAME) != 0)
643				continue;
644			sc2 = ifp2->if_softc;
645			if (sc2 == sc)
646				continue;
647			if (!sc2->gif_pdst || !sc2->gif_psrc)
648				continue;
649			if (sc2->gif_pdst->sa_family != dst->sa_family ||
650			    sc2->gif_pdst->sa_len != dst->sa_len ||
651			    sc2->gif_psrc->sa_family != src->sa_family ||
652			    sc2->gif_psrc->sa_len != src->sa_len)
653				continue;
654
655			/*
656			 * Disallow parallel tunnels unless instructed
657			 * otherwise.
658			 */
659			if (!parallel_tunnels &&
660			    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
661			    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
662				error = EADDRNOTAVAIL;
663				goto bad;
664			}
665
666			/* can't configure multiple multi-dest interfaces */
667#define multidest(x) \
668	(((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
669#ifdef INET6
670#define multidest6(x) \
671	(IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
672#endif
673			if (dst->sa_family == AF_INET &&
674			    multidest(dst) && multidest(sc2->gif_pdst)) {
675				error = EADDRNOTAVAIL;
676				goto bad;
677			}
678#ifdef INET6
679			if (dst->sa_family == AF_INET6 &&
680			    multidest6(dst) && multidest6(sc2->gif_pdst)) {
681				error = EADDRNOTAVAIL;
682				goto bad;
683			}
684#endif
685		}
686
687		if (sc->gif_psrc)
688			free((caddr_t)sc->gif_psrc, M_IFADDR);
689		sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
690		bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
691		sc->gif_psrc = sa;
692
693		if (sc->gif_pdst)
694			free((caddr_t)sc->gif_pdst, M_IFADDR);
695		sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
696		bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
697		sc->gif_pdst = sa;
698
699		ifp->if_flags |= IFF_RUNNING;
700
701		error = 0;
702		break;
703
704#ifdef SIOCDIFPHYADDR
705	case SIOCDIFPHYADDR:
706		if (sc->gif_psrc) {
707			free((caddr_t)sc->gif_psrc, M_IFADDR);
708			sc->gif_psrc = NULL;
709		}
710		if (sc->gif_pdst) {
711			free((caddr_t)sc->gif_pdst, M_IFADDR);
712			sc->gif_pdst = NULL;
713		}
714		/* change the IFF_{UP, RUNNING} flag as well? */
715		break;
716#endif
717
718	case SIOCGIFPSRCADDR:
719#ifdef INET6
720	case SIOCGIFPSRCADDR_IN6:
721#endif /* INET6 */
722		if (sc->gif_psrc == NULL) {
723			error = EADDRNOTAVAIL;
724			goto bad;
725		}
726		src = sc->gif_psrc;
727		switch (cmd) {
728#ifdef INET
729		case SIOCGIFPSRCADDR:
730			dst = &ifr->ifr_addr;
731			size = sizeof(ifr->ifr_addr);
732			break;
733#endif /* INET */
734#ifdef INET6
735		case SIOCGIFPSRCADDR_IN6:
736			dst = (struct sockaddr *)
737				&(((struct in6_ifreq *)data)->ifr_addr);
738			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
739			break;
740#endif /* INET6 */
741		default:
742			error = EADDRNOTAVAIL;
743			goto bad;
744		}
745		if (src->sa_len > size)
746			return EINVAL;
747		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
748		break;
749
750	case SIOCGIFPDSTADDR:
751#ifdef INET6
752	case SIOCGIFPDSTADDR_IN6:
753#endif /* INET6 */
754		if (sc->gif_pdst == NULL) {
755			error = EADDRNOTAVAIL;
756			goto bad;
757		}
758		src = sc->gif_pdst;
759		switch (cmd) {
760#ifdef INET
761		case SIOCGIFPDSTADDR:
762			dst = &ifr->ifr_addr;
763			size = sizeof(ifr->ifr_addr);
764			break;
765#endif /* INET */
766#ifdef INET6
767		case SIOCGIFPDSTADDR_IN6:
768			dst = (struct sockaddr *)
769				&(((struct in6_ifreq *)data)->ifr_addr);
770			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
771			break;
772#endif /* INET6 */
773		default:
774			error = EADDRNOTAVAIL;
775			goto bad;
776		}
777		if (src->sa_len > size)
778			return EINVAL;
779		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
780		break;
781
782	case SIOCGLIFPHYADDR:
783		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
784			error = EADDRNOTAVAIL;
785			goto bad;
786		}
787
788		/* copy src */
789		src = sc->gif_psrc;
790		dst = (struct sockaddr *)
791			&(((struct if_laddrreq *)data)->addr);
792		size = sizeof(((struct if_laddrreq *)data)->addr);
793		if (src->sa_len > size)
794			return EINVAL;
795		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
796
797		/* copy dst */
798		src = sc->gif_pdst;
799		dst = (struct sockaddr *)
800			&(((struct if_laddrreq *)data)->dstaddr);
801		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
802		if (src->sa_len > size)
803			return EINVAL;
804		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
805		break;
806
807	case SIOCSIFFLAGS:
808		/* if_ioctl() takes care of it */
809		break;
810
811	default:
812		error = EINVAL;
813		break;
814	}
815 bad:
816	return error;
817}
818
819void
820gif_delete_tunnel(sc)
821	struct gif_softc *sc;
822{
823	/* XXX: NetBSD protects this function with splsoftnet() */
824
825	if (sc->gif_psrc) {
826		free((caddr_t)sc->gif_psrc, M_IFADDR);
827		sc->gif_psrc = NULL;
828	}
829	if (sc->gif_pdst) {
830		free((caddr_t)sc->gif_pdst, M_IFADDR);
831		sc->gif_pdst = NULL;
832	}
833	/* change the IFF_UP flag as well? */
834}
835