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