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