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