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