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