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