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