if_gif.c revision 101739
1/*	$FreeBSD: head/sys/net/if_gif.c 101739 2002-08-12 16:08:23Z rwatson $	*/
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	int s;
525	struct ifnet *ifp2;
526	struct gif_softc *sc2;
527
528	switch (cmd) {
529	case SIOCSIFADDR:
530		break;
531
532	case SIOCSIFDSTADDR:
533		break;
534
535	case SIOCADDMULTI:
536	case SIOCDELMULTI:
537		break;
538
539#ifdef	SIOCSIFMTU /* xxx */
540	case SIOCGIFMTU:
541		break;
542
543	case SIOCSIFMTU:
544		{
545			u_long mtu;
546			mtu = ifr->ifr_mtu;
547			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
548				return (EINVAL);
549			}
550			ifp->if_mtu = mtu;
551		}
552		break;
553#endif /* SIOCSIFMTU */
554
555	case SIOCSIFPHYADDR:
556#ifdef INET6
557	case SIOCSIFPHYADDR_IN6:
558#endif /* INET6 */
559	case SIOCSLIFPHYADDR:
560		switch (cmd) {
561#ifdef INET
562		case SIOCSIFPHYADDR:
563			src = (struct sockaddr *)
564				&(((struct in_aliasreq *)data)->ifra_addr);
565			dst = (struct sockaddr *)
566				&(((struct in_aliasreq *)data)->ifra_dstaddr);
567			break;
568#endif
569#ifdef INET6
570		case SIOCSIFPHYADDR_IN6:
571			src = (struct sockaddr *)
572				&(((struct in6_aliasreq *)data)->ifra_addr);
573			dst = (struct sockaddr *)
574				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
575			break;
576#endif
577		case SIOCSLIFPHYADDR:
578			src = (struct sockaddr *)
579				&(((struct if_laddrreq *)data)->addr);
580			dst = (struct sockaddr *)
581				&(((struct if_laddrreq *)data)->dstaddr);
582		default:
583			error = EADDRNOTAVAIL;
584			goto bad;
585		}
586
587		/* sa_family must be equal */
588		if (src->sa_family != dst->sa_family)
589			return EINVAL;
590
591		/* validate sa_len */
592		switch (src->sa_family) {
593#ifdef INET
594		case AF_INET:
595			if (src->sa_len != sizeof(struct sockaddr_in))
596				return EINVAL;
597			break;
598#endif
599#ifdef INET6
600		case AF_INET6:
601			if (src->sa_len != sizeof(struct sockaddr_in6))
602				return EINVAL;
603			break;
604#endif
605		default:
606			return EAFNOSUPPORT;
607		}
608		switch (dst->sa_family) {
609#ifdef INET
610		case AF_INET:
611			if (dst->sa_len != sizeof(struct sockaddr_in))
612				return EINVAL;
613			break;
614#endif
615#ifdef INET6
616		case AF_INET6:
617			if (dst->sa_len != sizeof(struct sockaddr_in6))
618				return EINVAL;
619			break;
620#endif
621		default:
622			return EAFNOSUPPORT;
623		}
624
625		/* check sa_family looks sane for the cmd */
626		switch (cmd) {
627		case SIOCSIFPHYADDR:
628			if (src->sa_family == AF_INET)
629				break;
630			return EAFNOSUPPORT;
631#ifdef INET6
632		case SIOCSIFPHYADDR_IN6:
633			if (src->sa_family == AF_INET6)
634				break;
635			return EAFNOSUPPORT;
636#endif /* INET6 */
637		case SIOCSLIFPHYADDR:
638			/* checks done in the above */
639			break;
640		}
641
642		TAILQ_FOREACH(ifp2, &ifnet, if_link) {
643			if (strcmp(ifp2->if_name, GIFNAME) != 0)
644				continue;
645			sc2 = ifp2->if_softc;
646			if (sc2 == sc)
647				continue;
648			if (!sc2->gif_pdst || !sc2->gif_psrc)
649				continue;
650			if (sc2->gif_pdst->sa_family != dst->sa_family ||
651			    sc2->gif_pdst->sa_len != dst->sa_len ||
652			    sc2->gif_psrc->sa_family != src->sa_family ||
653			    sc2->gif_psrc->sa_len != src->sa_len)
654				continue;
655
656			/*
657			 * Disallow parallel tunnels unless instructed
658			 * otherwise.
659			 */
660			if (!parallel_tunnels &&
661			    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
662			    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
663				error = EADDRNOTAVAIL;
664				goto bad;
665			}
666
667			/* can't configure multiple multi-dest interfaces */
668#define multidest(x) \
669	(((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
670#ifdef INET6
671#define multidest6(x) \
672	(IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
673#endif
674			if (dst->sa_family == AF_INET &&
675			    multidest(dst) && multidest(sc2->gif_pdst)) {
676				error = EADDRNOTAVAIL;
677				goto bad;
678			}
679#ifdef INET6
680			if (dst->sa_family == AF_INET6 &&
681			    multidest6(dst) && multidest6(sc2->gif_pdst)) {
682				error = EADDRNOTAVAIL;
683				goto bad;
684			}
685#endif
686		}
687
688		if (sc->gif_psrc)
689			free((caddr_t)sc->gif_psrc, M_IFADDR);
690		sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
691		bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
692		sc->gif_psrc = sa;
693
694		if (sc->gif_pdst)
695			free((caddr_t)sc->gif_pdst, M_IFADDR);
696		sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
697		bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
698		sc->gif_pdst = sa;
699
700		ifp->if_flags |= IFF_RUNNING;
701		s = splimp();
702		if_up(ifp);	/* mark interface UP and send up RTM_IFINFO */
703		splx(s);
704
705		error = 0;
706		break;
707
708#ifdef SIOCDIFPHYADDR
709	case SIOCDIFPHYADDR:
710		if (sc->gif_psrc) {
711			free((caddr_t)sc->gif_psrc, M_IFADDR);
712			sc->gif_psrc = NULL;
713		}
714		if (sc->gif_pdst) {
715			free((caddr_t)sc->gif_pdst, M_IFADDR);
716			sc->gif_pdst = NULL;
717		}
718		/* change the IFF_{UP, RUNNING} flag as well? */
719		break;
720#endif
721
722	case SIOCGIFPSRCADDR:
723#ifdef INET6
724	case SIOCGIFPSRCADDR_IN6:
725#endif /* INET6 */
726		if (sc->gif_psrc == NULL) {
727			error = EADDRNOTAVAIL;
728			goto bad;
729		}
730		src = sc->gif_psrc;
731		switch (cmd) {
732#ifdef INET
733		case SIOCGIFPSRCADDR:
734			dst = &ifr->ifr_addr;
735			size = sizeof(ifr->ifr_addr);
736			break;
737#endif /* INET */
738#ifdef INET6
739		case SIOCGIFPSRCADDR_IN6:
740			dst = (struct sockaddr *)
741				&(((struct in6_ifreq *)data)->ifr_addr);
742			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
743			break;
744#endif /* INET6 */
745		default:
746			error = EADDRNOTAVAIL;
747			goto bad;
748		}
749		if (src->sa_len > size)
750			return EINVAL;
751		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
752		break;
753
754	case SIOCGIFPDSTADDR:
755#ifdef INET6
756	case SIOCGIFPDSTADDR_IN6:
757#endif /* INET6 */
758		if (sc->gif_pdst == NULL) {
759			error = EADDRNOTAVAIL;
760			goto bad;
761		}
762		src = sc->gif_pdst;
763		switch (cmd) {
764#ifdef INET
765		case SIOCGIFPDSTADDR:
766			dst = &ifr->ifr_addr;
767			size = sizeof(ifr->ifr_addr);
768			break;
769#endif /* INET */
770#ifdef INET6
771		case SIOCGIFPDSTADDR_IN6:
772			dst = (struct sockaddr *)
773				&(((struct in6_ifreq *)data)->ifr_addr);
774			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
775			break;
776#endif /* INET6 */
777		default:
778			error = EADDRNOTAVAIL;
779			goto bad;
780		}
781		if (src->sa_len > size)
782			return EINVAL;
783		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
784		break;
785
786	case SIOCGLIFPHYADDR:
787		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
788			error = EADDRNOTAVAIL;
789			goto bad;
790		}
791
792		/* copy src */
793		src = sc->gif_psrc;
794		dst = (struct sockaddr *)
795			&(((struct if_laddrreq *)data)->addr);
796		size = sizeof(((struct if_laddrreq *)data)->addr);
797		if (src->sa_len > size)
798			return EINVAL;
799		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
800
801		/* copy dst */
802		src = sc->gif_pdst;
803		dst = (struct sockaddr *)
804			&(((struct if_laddrreq *)data)->dstaddr);
805		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
806		if (src->sa_len > size)
807			return EINVAL;
808		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
809		break;
810
811	case SIOCSIFFLAGS:
812		/* if_ioctl() takes care of it */
813		break;
814
815	default:
816		error = EINVAL;
817		break;
818	}
819 bad:
820	return error;
821}
822
823void
824gif_delete_tunnel(sc)
825	struct gif_softc *sc;
826{
827	/* XXX: NetBSD protects this function with splsoftnet() */
828
829	if (sc->gif_psrc) {
830		free((caddr_t)sc->gif_psrc, M_IFADDR);
831		sc->gif_psrc = NULL;
832	}
833	if (sc->gif_pdst) {
834		free((caddr_t)sc->gif_pdst, M_IFADDR);
835		sc->gif_pdst = NULL;
836	}
837	/* change the IFF_UP flag as well? */
838}
839