Merge branch 'vendor/OPENSSL' into HEAD
[dragonfly.git] / sys / net / gif / if_gif.c
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 #ifdef INET6
208                 if (sc->gif_ro6[n].ro_rt) {
209                         RTFREE(sc->gif_ro6[n].ro_rt);
210                         sc->gif_ro6[n].ro_rt = NULL;
211                 }
212 #endif
213         }
214 }
215
216 static int
217 gifmodevent(module_t mod, int type, void *data)
218 {
219
220         switch (type) {
221         case MOD_LOAD:
222                 LIST_INIT(&gif_softc_list);
223                 if_clone_attach(&gif_cloner);
224
225 #ifdef INET6
226                 ip6_gif_hlim = GIF_HLIM;
227 #endif
228
229                 break;
230         case MOD_UNLOAD:
231                 if_clone_detach(&gif_cloner);
232
233                 while (!LIST_EMPTY(&gif_softc_list))
234                         gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
235
236 #ifdef INET6
237                 ip6_gif_hlim = 0;
238 #endif
239                 break;
240         }
241         return 0;
242 }
243
244 static moduledata_t gif_mod = {
245         "if_gif",
246         gifmodevent,
247         0
248 };
249
250 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
251
252 int
253 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
254 {
255         struct ip ip;
256         struct gif_softc *sc;
257
258         sc = (struct gif_softc *)arg;
259         if (sc == NULL)
260                 return 0;
261
262         if ((sc->gif_if.if_flags & IFF_UP) == 0)
263                 return 0;
264
265         /* no physical address */
266         if (!sc->gif_psrc || !sc->gif_pdst)
267                 return 0;
268
269         switch (proto) {
270 #ifdef INET
271         case IPPROTO_IPV4:
272                 break;
273 #endif
274 #ifdef INET6
275         case IPPROTO_IPV6:
276                 break;
277 #endif
278         default:
279                 return 0;
280         }
281
282         /* Bail on short packets */
283         if (m->m_pkthdr.len < sizeof(ip))
284                 return 0;
285
286         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
287
288         switch (ip.ip_v) {
289 #ifdef INET
290         case 4:
291                 if (sc->gif_psrc->sa_family != AF_INET ||
292                     sc->gif_pdst->sa_family != AF_INET)
293                         return 0;
294                 return gif_encapcheck4(m, off, proto, arg);
295 #endif
296 #ifdef INET6
297         case 6:
298                 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
299                         return 0;
300                 if (sc->gif_psrc->sa_family != AF_INET6 ||
301                     sc->gif_pdst->sa_family != AF_INET6)
302                         return 0;
303                 return gif_encapcheck6(m, off, proto, arg);
304 #endif
305         default:
306                 return 0;
307         }
308 }
309
310 /*
311  * Parameters:
312  *      rt:     added in net2
313  */
314 static int
315 gif_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
316                       struct rtentry *rt)
317 {
318         struct gif_softc *sc = (struct gif_softc*)ifp;
319         int error = 0;
320         static int called = 0;  /* XXX: MUTEX */
321
322         /*
323          * gif may cause infinite recursion calls when misconfigured.
324          * We'll prevent this by introducing upper limit.
325          * XXX: this mechanism may introduce another problem about
326          *      mutual exclusion of the variable CALLED, especially if we
327          *      use kernel thread.
328          */
329         if (++called > max_gif_nesting) {
330                 log(LOG_NOTICE,
331                     "gif_output: recursively called too many times(%d)\n",
332                     called);
333                 m_freem(m);
334                 error = EIO;    /* is there better errno? */
335                 goto end;
336         }
337
338         m->m_flags &= ~(M_BCAST|M_MCAST);
339         if (!(ifp->if_flags & IFF_UP) ||
340             sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
341                 m_freem(m);
342                 error = ENETDOWN;
343                 goto end;
344         }
345
346         if (ifp->if_bpf) {
347                 /*
348                  * We need to prepend the address family as
349                  * a four byte field.
350                  */
351                 uint32_t af = dst->sa_family;
352
353                 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
354         }
355         ifp->if_opackets++;     
356         ifp->if_obytes += m->m_pkthdr.len;
357
358         /* inner AF-specific encapsulation */
359
360         /* XXX should we check if our outer source is legal? */
361
362         /* dispatch to output logic based on outer AF */
363         switch (sc->gif_psrc->sa_family) {
364 #ifdef INET
365         case AF_INET:
366                 error = in_gif_output(ifp, dst->sa_family, m);
367                 break;
368 #endif
369 #ifdef INET6
370         case AF_INET6:
371                 error = in6_gif_output(ifp, dst->sa_family, m);
372                 break;
373 #endif
374         default:
375                 m_freem(m);             
376                 error = ENETDOWN;
377                 goto end;
378         }
379
380   end:
381         called = 0;             /* reset recursion counter */
382         if (error)
383                 ifp->if_oerrors++;
384         return error;
385 }
386
387 int
388 gif_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
389            struct rtentry *rt)
390 {
391         int error;
392
393         ifnet_serialize_tx(ifp);
394         error = gif_output_serialized(ifp, m, dst, rt);
395         ifnet_deserialize_tx(ifp);
396         return error;
397 }
398
399 void
400 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
401 {
402         int isr;
403
404         if (ifp == NULL) {
405                 /* just in case */
406                 m_freem(m);
407                 return;
408         }
409
410         m->m_pkthdr.rcvif = ifp;
411         
412         if (ifp->if_bpf) {
413                 /*
414                  * We need to prepend the address family as
415                  * a four byte field.
416                  */
417                 uint32_t af1 = af;
418
419                 bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
420         }
421
422         /*
423          * Put the packet to the network layer input queue according to the
424          * specified address family.
425          * Note: older versions of gif_input directly called network layer
426          * input functions, e.g. ip6_input, here.  We changed the policy to
427          * prevent too many recursive calls of such input functions, which
428          * might cause kernel panic.  But the change may introduce another
429          * problem; if the input queue is full, packets are discarded.
430          * The kernel stack overflow really happened, and we believed
431          * queue-full rarely occurs, so we changed the policy.
432          */
433         switch (af) {
434 #ifdef INET
435         case AF_INET:
436                 isr = NETISR_IP;
437                 break;
438 #endif
439 #ifdef INET6
440         case AF_INET6:
441                 isr = NETISR_IPV6;
442                 break;
443 #endif
444         default:
445                 m_freem(m);
446                 return;
447         }
448
449         ifp->if_ipackets++;
450         ifp->if_ibytes += m->m_pkthdr.len;
451         m->m_flags &= ~M_HASH;
452         netisr_queue(isr, m);
453
454         return;
455 }
456
457 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
458 int
459 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
460 {
461         struct gif_softc *sc  = (struct gif_softc*)ifp;
462         struct ifreq     *ifr = (struct ifreq*)data;
463         int error = 0, size;
464         struct sockaddr *dst, *src;
465 #ifdef  SIOCSIFMTU /* xxx */
466         u_long mtu;
467 #endif
468
469         switch (cmd) {
470         case SIOCSIFADDR:
471                 ifp->if_flags |= IFF_UP;
472                 break;
473                 
474         case SIOCSIFDSTADDR:
475                 break;
476
477         case SIOCADDMULTI:
478         case SIOCDELMULTI:
479                 break;
480
481 #ifdef  SIOCSIFMTU /* xxx */
482         case SIOCGIFMTU:
483                 break;
484
485         case SIOCSIFMTU:
486                 mtu = ifr->ifr_mtu;
487                 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
488                         return (EINVAL);
489                 ifp->if_mtu = mtu;
490                 break;
491 #endif /* SIOCSIFMTU */
492
493 #ifdef INET
494         case SIOCSIFPHYADDR:
495 #endif
496 #ifdef INET6
497         case SIOCSIFPHYADDR_IN6:
498 #endif /* INET6 */
499         case SIOCSLIFPHYADDR:
500                 switch (cmd) {
501 #ifdef INET
502                 case SIOCSIFPHYADDR:
503                         src = (struct sockaddr *)
504                                 &(((struct in_aliasreq *)data)->ifra_addr);
505                         dst = (struct sockaddr *)
506                                 &(((struct in_aliasreq *)data)->ifra_dstaddr);
507                         break;
508 #endif
509 #ifdef INET6
510                 case SIOCSIFPHYADDR_IN6:
511                         src = (struct sockaddr *)
512                                 &(((struct in6_aliasreq *)data)->ifra_addr);
513                         dst = (struct sockaddr *)
514                                 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
515                         break;
516 #endif
517                 case SIOCSLIFPHYADDR:
518                         src = (struct sockaddr *)
519                                 &(((struct if_laddrreq *)data)->addr);
520                         dst = (struct sockaddr *)
521                                 &(((struct if_laddrreq *)data)->dstaddr);
522                         break;
523                 default:
524                         return EINVAL;
525                 }
526
527                 /* sa_family must be equal */
528                 if (src->sa_family != dst->sa_family)
529                         return EINVAL;
530
531                 /* validate sa_len */
532                 switch (src->sa_family) {
533 #ifdef INET
534                 case AF_INET:
535                         if (src->sa_len != sizeof(struct sockaddr_in))
536                                 return EINVAL;
537                         break;
538 #endif
539 #ifdef INET6
540                 case AF_INET6:
541                         if (src->sa_len != sizeof(struct sockaddr_in6))
542                                 return EINVAL;
543                         break;
544 #endif
545                 default:
546                         return EAFNOSUPPORT;
547                 }
548                 switch (dst->sa_family) {
549 #ifdef INET
550                 case AF_INET:
551                         if (dst->sa_len != sizeof(struct sockaddr_in))
552                                 return EINVAL;
553                         break;
554 #endif
555 #ifdef INET6
556                 case AF_INET6:
557                         if (dst->sa_len != sizeof(struct sockaddr_in6))
558                                 return EINVAL;
559                         break;
560 #endif
561                 default:
562                         return EAFNOSUPPORT;
563                 }
564
565                 /* check sa_family looks sane for the cmd */
566                 switch (cmd) {
567                 case SIOCSIFPHYADDR:
568                         if (src->sa_family == AF_INET)
569                                 break;
570                         return EAFNOSUPPORT;
571 #ifdef INET6
572                 case SIOCSIFPHYADDR_IN6:
573                         if (src->sa_family == AF_INET6)
574                                 break;
575                         return EAFNOSUPPORT;
576 #endif /* INET6 */
577                 case SIOCSLIFPHYADDR:
578                         /* checks done in the above */
579                         break;
580                 }
581
582                 error = gif_set_tunnel(&sc->gif_if, src, dst);
583                 break;
584
585 #ifdef SIOCDIFPHYADDR
586         case SIOCDIFPHYADDR:
587                 gif_delete_tunnel(&sc->gif_if);
588                 break;
589 #endif
590                         
591         case SIOCGIFPSRCADDR:
592 #ifdef INET6
593         case SIOCGIFPSRCADDR_IN6:
594 #endif /* INET6 */
595                 if (sc->gif_psrc == NULL) {
596                         error = EADDRNOTAVAIL;
597                         goto bad;
598                 }
599                 src = sc->gif_psrc;
600                 switch (cmd) {
601 #ifdef INET
602                 case SIOCGIFPSRCADDR:
603                         dst = &ifr->ifr_addr;
604                         size = sizeof(ifr->ifr_addr);
605                         break;
606 #endif /* INET */
607 #ifdef INET6
608                 case SIOCGIFPSRCADDR_IN6:
609                         dst = (struct sockaddr *)
610                                 &(((struct in6_ifreq *)data)->ifr_addr);
611                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
612                         break;
613 #endif /* INET6 */
614                 default:
615                         error = EADDRNOTAVAIL;
616                         goto bad;
617                 }
618                 if (src->sa_len > size)
619                         return EINVAL;
620                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
621                 break;
622                         
623         case SIOCGIFPDSTADDR:
624 #ifdef INET6
625         case SIOCGIFPDSTADDR_IN6:
626 #endif /* INET6 */
627                 if (sc->gif_pdst == NULL) {
628                         error = EADDRNOTAVAIL;
629                         goto bad;
630                 }
631                 src = sc->gif_pdst;
632                 switch (cmd) {
633 #ifdef INET
634                 case SIOCGIFPDSTADDR:
635                         dst = &ifr->ifr_addr;
636                         size = sizeof(ifr->ifr_addr);
637                         break;
638 #endif /* INET */
639 #ifdef INET6
640                 case SIOCGIFPDSTADDR_IN6:
641                         dst = (struct sockaddr *)
642                                 &(((struct in6_ifreq *)data)->ifr_addr);
643                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
644                         break;
645 #endif /* INET6 */
646                 default:
647                         error = EADDRNOTAVAIL;
648                         goto bad;
649                 }
650                 if (src->sa_len > size)
651                         return EINVAL;
652                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
653                 break;
654
655         case SIOCGLIFPHYADDR:
656                 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
657                         error = EADDRNOTAVAIL;
658                         goto bad;
659                 }
660
661                 /* copy src */
662                 src = sc->gif_psrc;
663                 dst = (struct sockaddr *)
664                         &(((struct if_laddrreq *)data)->addr);
665                 size = sizeof(((struct if_laddrreq *)data)->addr);
666                 if (src->sa_len > size)
667                         return EINVAL;
668                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
669
670                 /* copy dst */
671                 src = sc->gif_pdst;
672                 dst = (struct sockaddr *)
673                         &(((struct if_laddrreq *)data)->dstaddr);
674                 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
675                 if (src->sa_len > size)
676                         return EINVAL;
677                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
678                 break;
679
680         case SIOCSIFFLAGS:
681                 /* if_ioctl() takes care of it */
682                 break;
683
684         default:
685                 error = EINVAL;
686                 break;
687         }
688  bad:
689         return error;
690 }
691
692 int
693 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
694 {
695         struct gif_softc *sc = (struct gif_softc *)ifp;
696         struct gif_softc *sc2;
697         struct sockaddr *osrc, *odst, *sa;
698         int error = 0; 
699
700         crit_enter();
701
702         LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
703                 if (sc2 == sc)
704                         continue;
705                 if (!sc2->gif_pdst || !sc2->gif_psrc)
706                         continue;
707                 if (sc2->gif_pdst->sa_family != dst->sa_family ||
708                     sc2->gif_pdst->sa_len != dst->sa_len ||
709                     sc2->gif_psrc->sa_family != src->sa_family ||
710                     sc2->gif_psrc->sa_len != src->sa_len)
711                         continue;
712
713                 /*
714                  * Disallow parallel tunnels unless instructed
715                  * otherwise.
716                  */
717                 if (!parallel_tunnels &&
718                     bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
719                     bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
720                         error = EADDRNOTAVAIL;
721                         goto bad;
722                 }
723
724                 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
725         }
726
727         /* XXX we can detach from both, but be polite just in case */
728         if (sc->gif_psrc) {
729                 switch (sc->gif_psrc->sa_family) {
730 #ifdef INET
731                 case AF_INET:
732                         in_gif_detach(sc);
733                         break;
734 #endif
735 #ifdef INET6
736                 case AF_INET6:
737                         in6_gif_detach(sc);
738                         break;
739 #endif
740                 }
741                 gif_clear_cache(sc);
742         }
743
744         osrc = sc->gif_psrc;
745         sa = (struct sockaddr *)kmalloc(src->sa_len, M_IFADDR, M_WAITOK);
746         bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
747         sc->gif_psrc = sa;
748
749         odst = sc->gif_pdst;
750         sa = (struct sockaddr *)kmalloc(dst->sa_len, M_IFADDR, M_WAITOK);
751         bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
752         sc->gif_pdst = sa;
753
754         switch (sc->gif_psrc->sa_family) {
755 #ifdef INET
756         case AF_INET:
757                 error = in_gif_attach(sc);
758                 break;
759 #endif
760 #ifdef INET6
761         case AF_INET6:
762                 error = in6_gif_attach(sc);
763                 break;
764 #endif
765         }
766         if (error) {
767                 /* rollback */
768                 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
769                 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
770                 sc->gif_psrc = osrc;
771                 sc->gif_pdst = odst;
772                 goto bad;
773         }
774
775         if (osrc)
776                 kfree((caddr_t)osrc, M_IFADDR);
777         if (odst)
778                 kfree((caddr_t)odst, M_IFADDR);
779
780         if (sc->gif_psrc && sc->gif_pdst)
781                 ifp->if_flags |= IFF_RUNNING;
782         else
783                 ifp->if_flags &= ~IFF_RUNNING;
784         crit_exit();
785
786         return 0;
787
788  bad:
789         if (sc->gif_psrc && sc->gif_pdst)
790                 ifp->if_flags |= IFF_RUNNING;
791         else
792                 ifp->if_flags &= ~IFF_RUNNING;
793         crit_exit();
794
795         return error;
796 }
797
798 void
799 gif_delete_tunnel(struct ifnet *ifp)
800 {
801         struct gif_softc *sc = (struct gif_softc *)ifp;
802
803         crit_enter();
804
805         if (sc->gif_psrc) {
806                 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
807                 sc->gif_psrc = NULL;
808         }
809         if (sc->gif_pdst) {
810                 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
811                 sc->gif_pdst = NULL;
812         }
813         /* it is safe to detach from both */
814 #ifdef INET
815         in_gif_detach(sc);
816 #endif
817 #ifdef INET6
818         in6_gif_detach(sc);
819 #endif
820         gif_clear_cache(sc);
821
822         if (sc->gif_psrc && sc->gif_pdst)
823                 ifp->if_flags |= IFF_RUNNING;
824         else
825                 ifp->if_flags &= ~IFF_RUNNING;
826         crit_exit();
827 }