]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_gif.c
- We don't need to cache_purge() in nfs_reclaim(), vclean() does it for us.
[FreeBSD/FreeBSD.git] / sys / net / if_gif.c
1 /*      $FreeBSD$       */
2 /*      $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */
3
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mac.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 <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 <net/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 static LIST_HEAD(, gif_softc) gif_softc_list;
88
89 void    (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
90 void    (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
91 void    (*ng_gif_attach_p)(struct ifnet *ifp);
92 void    (*ng_gif_detach_p)(struct ifnet *ifp);
93
94 int     gif_clone_create(struct if_clone *, int);
95 void    gif_clone_destroy(struct ifnet *);
96
97 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif",
98     gif_clone_create, gif_clone_destroy, 0, IF_MAXUNIT);
99
100 static int gifmodevent(module_t, int, void *);
101
102 SYSCTL_DECL(_net_link);
103 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
104     "Generic Tunnel Interface");
105 #ifndef MAX_GIF_NEST
106 /*
107  * This macro controls the default upper limitation on nesting of gif tunnels.
108  * Since, setting a large value to this macro with a careless configuration
109  * may introduce system crash, we don't allow any nestings by default.
110  * If you need to configure nested gif tunnels, you can define this macro
111  * in your kernel configuration file.  However, if you do so, please be
112  * careful to configure the tunnels so that it won't make a loop.
113  */
114 #define MAX_GIF_NEST 1
115 #endif
116 static int max_gif_nesting = MAX_GIF_NEST;
117 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
118     &max_gif_nesting, 0, "Max nested tunnels");
119
120 /*
121  * By default, we disallow creation of multiple tunnels between the same
122  * pair of addresses.  Some applications require this functionality so
123  * we allow control over this check here.
124  */
125 #ifdef XBONEHACK
126 static int parallel_tunnels = 1;
127 #else
128 static int parallel_tunnels = 0;
129 #endif
130 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
131     &parallel_tunnels, 0, "Allow parallel tunnels?");
132
133 int
134 gif_clone_create(ifc, unit)
135         struct if_clone *ifc;
136         int unit;
137 {
138         struct gif_softc *sc;
139
140         sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
141         bzero(sc, sizeof(struct gif_softc));
142
143         sc->gif_if.if_softc = sc;
144         sc->gif_if.if_name = GIFNAME;
145         sc->gif_if.if_unit = unit;
146
147         gifattach0(sc);
148
149         LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
150         return (0);
151 }
152
153 void
154 gifattach0(sc)
155         struct gif_softc *sc;
156 {
157
158         sc->encap_cookie4 = sc->encap_cookie6 = NULL;
159
160         sc->gif_if.if_addrlen = 0;
161         sc->gif_if.if_mtu    = GIF_MTU;
162         sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
163 #if 0
164         /* turn off ingress filter */
165         sc->gif_if.if_flags  |= IFF_LINK2;
166 #endif
167         sc->gif_if.if_ioctl  = gif_ioctl;
168         sc->gif_if.if_output = gif_output;
169         sc->gif_if.if_type   = IFT_GIF;
170         sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
171         if_attach(&sc->gif_if);
172         bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
173         if (ng_gif_attach_p != NULL)
174                 (*ng_gif_attach_p)(&sc->gif_if);
175 }
176
177 void
178 gif_clone_destroy(ifp)
179         struct ifnet *ifp;
180 {
181         int err;
182         struct gif_softc *sc = ifp->if_softc;
183
184         gif_delete_tunnel(&sc->gif_if);
185         LIST_REMOVE(sc, gif_list);
186 #ifdef INET6
187         if (sc->encap_cookie6 != NULL) {
188                 err = encap_detach(sc->encap_cookie6);
189                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
190         }
191 #endif
192 #ifdef INET
193         if (sc->encap_cookie4 != NULL) {
194                 err = encap_detach(sc->encap_cookie4);
195                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
196         }
197 #endif
198
199         if (ng_gif_detach_p != NULL)
200                 (*ng_gif_detach_p)(ifp);
201         bpfdetach(ifp);
202         if_detach(ifp);
203
204         free(sc, M_GIF);
205 }
206
207 static int
208 gifmodevent(mod, type, data)
209         module_t mod;
210         int type;
211         void *data;
212 {
213
214         switch (type) {
215         case MOD_LOAD:
216                 LIST_INIT(&gif_softc_list);
217                 if_clone_attach(&gif_cloner);
218
219 #ifdef INET6
220                 ip6_gif_hlim = GIF_HLIM;
221 #endif
222
223                 break;
224         case MOD_UNLOAD:
225                 if_clone_detach(&gif_cloner);
226
227                 while (!LIST_EMPTY(&gif_softc_list))
228                         gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
229
230 #ifdef INET6
231                 ip6_gif_hlim = 0;
232 #endif
233                 break;
234         }
235         return 0;
236 }
237
238 static moduledata_t gif_mod = {
239         "if_gif",
240         gifmodevent,
241         0
242 };
243
244 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
245 MODULE_VERSION(if_gif, 1);
246
247 int
248 gif_encapcheck(m, off, proto, arg)
249         const struct mbuf *m;
250         int off;
251         int proto;
252         void *arg;
253 {
254         struct ip ip;
255         struct gif_softc *sc;
256
257         sc = (struct gif_softc *)arg;
258         if (sc == NULL)
259                 return 0;
260
261         if ((sc->gif_if.if_flags & IFF_UP) == 0)
262                 return 0;
263
264         /* no physical address */
265         if (!sc->gif_psrc || !sc->gif_pdst)
266                 return 0;
267
268         switch (proto) {
269 #ifdef INET
270         case IPPROTO_IPV4:
271                 break;
272 #endif
273 #ifdef INET6
274         case IPPROTO_IPV6:
275                 break;
276 #endif
277         default:
278                 return 0;
279         }
280
281         /* Bail on short packets */
282         if (m->m_pkthdr.len < sizeof(ip))
283                 return 0;
284
285         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
286
287         switch (ip.ip_v) {
288 #ifdef INET
289         case 4:
290                 if (sc->gif_psrc->sa_family != AF_INET ||
291                     sc->gif_pdst->sa_family != AF_INET)
292                         return 0;
293                 return gif_encapcheck4(m, off, proto, arg);
294 #endif
295 #ifdef INET6
296         case 6:
297                 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
298                         return 0;
299                 if (sc->gif_psrc->sa_family != AF_INET6 ||
300                     sc->gif_pdst->sa_family != AF_INET6)
301                         return 0;
302                 return gif_encapcheck6(m, off, proto, arg);
303 #endif
304         default:
305                 return 0;
306         }
307 }
308
309 int
310 gif_output(ifp, m, dst, rt)
311         struct ifnet *ifp;
312         struct mbuf *m;
313         struct sockaddr *dst;
314         struct rtentry *rt;     /* added in net2 */
315 {
316         struct gif_softc *sc = (struct gif_softc*)ifp;
317         int error = 0;
318         static int called = 0;  /* XXX: MUTEX */
319
320 #ifdef MAC
321         error = mac_check_ifnet_transmit(ifp, m);
322         if (error) {
323                 m_freem(m);
324                 goto end;
325         }
326 #endif
327
328         /*
329          * gif may cause infinite recursion calls when misconfigured.
330          * We'll prevent this by introducing upper limit.
331          * XXX: this mechanism may introduce another problem about
332          *      mutual exclusion of the variable CALLED, especially if we
333          *      use kernel thread.
334          */
335         if (++called > max_gif_nesting) {
336                 log(LOG_NOTICE,
337                     "gif_output: recursively called too many times(%d)\n",
338                     called);
339                 m_freem(m);
340                 error = EIO;    /* is there better errno? */
341                 goto end;
342         }
343
344         m->m_flags &= ~(M_BCAST|M_MCAST);
345         if (!(ifp->if_flags & IFF_UP) ||
346             sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
347                 m_freem(m);
348                 error = ENETDOWN;
349                 goto end;
350         }
351
352         if (ifp->if_bpf) {
353                 /*
354                  * We need to prepend the address family as
355                  * a four byte field.  Cons up a dummy header
356                  * to pacify bpf.  This is safe because bpf
357                  * will only read from the mbuf (i.e., it won't
358                  * try to free it or keep a pointer a to it).
359                  */
360                 struct mbuf m0;
361                 u_int32_t af = dst->sa_family;
362
363                 m0.m_next = m;
364                 m0.m_len = 4;
365                 m0.m_data = (char *)&af;
366                 
367                 BPF_MTAP(ifp, &m0);
368         }
369         ifp->if_opackets++;     
370         ifp->if_obytes += m->m_pkthdr.len;
371
372         /* inner AF-specific encapsulation */
373
374         /* XXX should we check if our outer source is legal? */
375
376         /* dispatch to output logic based on outer AF */
377         switch (sc->gif_psrc->sa_family) {
378 #ifdef INET
379         case AF_INET:
380                 error = in_gif_output(ifp, dst->sa_family, m);
381                 break;
382 #endif
383 #ifdef INET6
384         case AF_INET6:
385                 error = in6_gif_output(ifp, dst->sa_family, m);
386                 break;
387 #endif
388         default:
389                 m_freem(m);             
390                 error = ENETDOWN;
391                 goto end;
392         }
393
394   end:
395         called = 0;             /* reset recursion counter */
396         if (error)
397                 ifp->if_oerrors++;
398         return error;
399 }
400
401 void
402 gif_input(m, af, ifp)
403         struct mbuf *m;
404         int af;
405         struct ifnet *ifp;
406 {
407         int isr;
408
409         if (ifp == NULL) {
410                 /* just in case */
411                 m_freem(m);
412                 return;
413         }
414
415         m->m_pkthdr.rcvif = ifp;
416
417 #ifdef MAC
418         mac_create_mbuf_from_ifnet(ifp, m);
419 #endif
420
421         if (ifp->if_bpf) {
422                 /*
423                  * We need to prepend the address family as
424                  * a four byte field.  Cons up a dummy header
425                  * to pacify bpf.  This is safe because bpf
426                  * will only read from the mbuf (i.e., it won't
427                  * try to free it or keep a pointer a to it).
428                  */
429                 struct mbuf m0;
430                 u_int32_t af1 = af;
431                 
432                 m0.m_next = m;
433                 m0.m_len = 4;
434                 m0.m_data = (char *)&af1;
435                 
436                 BPF_MTAP(ifp, &m0);
437         }
438
439         if (ng_gif_input_p != NULL) {
440                 (*ng_gif_input_p)(ifp, &m, af);
441                 if (m == NULL)
442                         return;
443         }
444
445         /*
446          * Put the packet to the network layer input queue according to the
447          * specified address family.
448          * Note: older versions of gif_input directly called network layer
449          * input functions, e.g. ip6_input, here.  We changed the policy to
450          * prevent too many recursive calls of such input functions, which
451          * might cause kernel panic.  But the change may introduce another
452          * problem; if the input queue is full, packets are discarded.
453          * The kernel stack overflow really happened, and we believed
454          * queue-full rarely occurs, so we changed the policy.
455          */
456         switch (af) {
457 #ifdef INET
458         case AF_INET:
459                 isr = NETISR_IP;
460                 break;
461 #endif
462 #ifdef INET6
463         case AF_INET6:
464                 isr = NETISR_IPV6;
465                 break;
466 #endif
467         default:
468                 if (ng_gif_input_orphan_p != NULL)
469                         (*ng_gif_input_orphan_p)(ifp, m, af);
470                 else
471                         m_freem(m);
472                 return;
473         }
474
475         ifp->if_ipackets++;
476         ifp->if_ibytes += m->m_pkthdr.len;
477         netisr_dispatch(isr, m);
478 }
479
480 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
481 int
482 gif_ioctl(ifp, cmd, data)
483         struct ifnet *ifp;
484         u_long cmd;
485         caddr_t data;
486 {
487         struct gif_softc *sc  = (struct gif_softc*)ifp;
488         struct ifreq     *ifr = (struct ifreq*)data;
489         int error = 0, size;
490         struct sockaddr *dst, *src;
491 #ifdef  SIOCSIFMTU /* xxx */
492         u_long mtu;
493 #endif
494
495         switch (cmd) {
496         case SIOCSIFADDR:
497                 ifp->if_flags |= IFF_UP;
498                 break;
499                 
500         case SIOCSIFDSTADDR:
501                 break;
502
503         case SIOCADDMULTI:
504         case SIOCDELMULTI:
505                 break;
506
507 #ifdef  SIOCSIFMTU /* xxx */
508         case SIOCGIFMTU:
509                 break;
510
511         case SIOCSIFMTU:
512                 mtu = ifr->ifr_mtu;
513                 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
514                         return (EINVAL);
515                 ifp->if_mtu = mtu;
516                 break;
517 #endif /* SIOCSIFMTU */
518
519 #ifdef INET
520         case SIOCSIFPHYADDR:
521 #endif
522 #ifdef INET6
523         case SIOCSIFPHYADDR_IN6:
524 #endif /* INET6 */
525         case SIOCSLIFPHYADDR:
526                 switch (cmd) {
527 #ifdef INET
528                 case SIOCSIFPHYADDR:
529                         src = (struct sockaddr *)
530                                 &(((struct in_aliasreq *)data)->ifra_addr);
531                         dst = (struct sockaddr *)
532                                 &(((struct in_aliasreq *)data)->ifra_dstaddr);
533                         break;
534 #endif
535 #ifdef INET6
536                 case SIOCSIFPHYADDR_IN6:
537                         src = (struct sockaddr *)
538                                 &(((struct in6_aliasreq *)data)->ifra_addr);
539                         dst = (struct sockaddr *)
540                                 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
541                         break;
542 #endif
543                 case SIOCSLIFPHYADDR:
544                         src = (struct sockaddr *)
545                                 &(((struct if_laddrreq *)data)->addr);
546                         dst = (struct sockaddr *)
547                                 &(((struct if_laddrreq *)data)->dstaddr);
548                         break;
549                 default:
550                         return EINVAL;
551                 }
552
553                 /* sa_family must be equal */
554                 if (src->sa_family != dst->sa_family)
555                         return EINVAL;
556
557                 /* validate sa_len */
558                 switch (src->sa_family) {
559 #ifdef INET
560                 case AF_INET:
561                         if (src->sa_len != sizeof(struct sockaddr_in))
562                                 return EINVAL;
563                         break;
564 #endif
565 #ifdef INET6
566                 case AF_INET6:
567                         if (src->sa_len != sizeof(struct sockaddr_in6))
568                                 return EINVAL;
569                         break;
570 #endif
571                 default:
572                         return EAFNOSUPPORT;
573                 }
574                 switch (dst->sa_family) {
575 #ifdef INET
576                 case AF_INET:
577                         if (dst->sa_len != sizeof(struct sockaddr_in))
578                                 return EINVAL;
579                         break;
580 #endif
581 #ifdef INET6
582                 case AF_INET6:
583                         if (dst->sa_len != sizeof(struct sockaddr_in6))
584                                 return EINVAL;
585                         break;
586 #endif
587                 default:
588                         return EAFNOSUPPORT;
589                 }
590
591                 /* check sa_family looks sane for the cmd */
592                 switch (cmd) {
593                 case SIOCSIFPHYADDR:
594                         if (src->sa_family == AF_INET)
595                                 break;
596                         return EAFNOSUPPORT;
597 #ifdef INET6
598                 case SIOCSIFPHYADDR_IN6:
599                         if (src->sa_family == AF_INET6)
600                                 break;
601                         return EAFNOSUPPORT;
602 #endif /* INET6 */
603                 case SIOCSLIFPHYADDR:
604                         /* checks done in the above */
605                         break;
606                 }
607
608                 error = gif_set_tunnel(&sc->gif_if, src, dst);
609                 break;
610
611 #ifdef SIOCDIFPHYADDR
612         case SIOCDIFPHYADDR:
613                 gif_delete_tunnel(&sc->gif_if);
614                 break;
615 #endif
616                         
617         case SIOCGIFPSRCADDR:
618 #ifdef INET6
619         case SIOCGIFPSRCADDR_IN6:
620 #endif /* INET6 */
621                 if (sc->gif_psrc == NULL) {
622                         error = EADDRNOTAVAIL;
623                         goto bad;
624                 }
625                 src = sc->gif_psrc;
626                 switch (cmd) {
627 #ifdef INET
628                 case SIOCGIFPSRCADDR:
629                         dst = &ifr->ifr_addr;
630                         size = sizeof(ifr->ifr_addr);
631                         break;
632 #endif /* INET */
633 #ifdef INET6
634                 case SIOCGIFPSRCADDR_IN6:
635                         dst = (struct sockaddr *)
636                                 &(((struct in6_ifreq *)data)->ifr_addr);
637                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
638                         break;
639 #endif /* INET6 */
640                 default:
641                         error = EADDRNOTAVAIL;
642                         goto bad;
643                 }
644                 if (src->sa_len > size)
645                         return EINVAL;
646                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
647                 break;
648                         
649         case SIOCGIFPDSTADDR:
650 #ifdef INET6
651         case SIOCGIFPDSTADDR_IN6:
652 #endif /* INET6 */
653                 if (sc->gif_pdst == NULL) {
654                         error = EADDRNOTAVAIL;
655                         goto bad;
656                 }
657                 src = sc->gif_pdst;
658                 switch (cmd) {
659 #ifdef INET
660                 case SIOCGIFPDSTADDR:
661                         dst = &ifr->ifr_addr;
662                         size = sizeof(ifr->ifr_addr);
663                         break;
664 #endif /* INET */
665 #ifdef INET6
666                 case SIOCGIFPDSTADDR_IN6:
667                         dst = (struct sockaddr *)
668                                 &(((struct in6_ifreq *)data)->ifr_addr);
669                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
670                         break;
671 #endif /* INET6 */
672                 default:
673                         error = EADDRNOTAVAIL;
674                         goto bad;
675                 }
676                 if (src->sa_len > size)
677                         return EINVAL;
678                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
679                 break;
680
681         case SIOCGLIFPHYADDR:
682                 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
683                         error = EADDRNOTAVAIL;
684                         goto bad;
685                 }
686
687                 /* copy src */
688                 src = sc->gif_psrc;
689                 dst = (struct sockaddr *)
690                         &(((struct if_laddrreq *)data)->addr);
691                 size = sizeof(((struct if_laddrreq *)data)->addr);
692                 if (src->sa_len > size)
693                         return EINVAL;
694                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
695
696                 /* copy dst */
697                 src = sc->gif_pdst;
698                 dst = (struct sockaddr *)
699                         &(((struct if_laddrreq *)data)->dstaddr);
700                 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
701                 if (src->sa_len > size)
702                         return EINVAL;
703                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
704                 break;
705
706         case SIOCSIFFLAGS:
707                 /* if_ioctl() takes care of it */
708                 break;
709
710         default:
711                 error = EINVAL;
712                 break;
713         }
714  bad:
715         return error;
716 }
717
718 int
719 gif_set_tunnel(ifp, src, dst)
720         struct ifnet *ifp;
721         struct sockaddr *src;
722         struct sockaddr *dst;
723 {
724         struct gif_softc *sc = (struct gif_softc *)ifp;
725         struct gif_softc *sc2;
726         struct sockaddr *osrc, *odst, *sa;
727         int s;
728         int error = 0; 
729
730         s = splnet();
731
732         LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
733                 if (sc2 == sc)
734                         continue;
735                 if (!sc2->gif_pdst || !sc2->gif_psrc)
736                         continue;
737                 if (sc2->gif_pdst->sa_family != dst->sa_family ||
738                     sc2->gif_pdst->sa_len != dst->sa_len ||
739                     sc2->gif_psrc->sa_family != src->sa_family ||
740                     sc2->gif_psrc->sa_len != src->sa_len)
741                         continue;
742
743                 /*
744                  * Disallow parallel tunnels unless instructed
745                  * otherwise.
746                  */
747                 if (!parallel_tunnels &&
748                     bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
749                     bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
750                         error = EADDRNOTAVAIL;
751                         goto bad;
752                 }
753
754                 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
755         }
756
757         /* XXX we can detach from both, but be polite just in case */
758         if (sc->gif_psrc)
759                 switch (sc->gif_psrc->sa_family) {
760 #ifdef INET
761                 case AF_INET:
762                         (void)in_gif_detach(sc);
763                         break;
764 #endif
765 #ifdef INET6
766                 case AF_INET6:
767                         (void)in6_gif_detach(sc);
768                         break;
769 #endif
770                 }
771
772         osrc = sc->gif_psrc;
773         sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
774         bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
775         sc->gif_psrc = sa;
776
777         odst = sc->gif_pdst;
778         sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
779         bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
780         sc->gif_pdst = sa;
781
782         switch (sc->gif_psrc->sa_family) {
783 #ifdef INET
784         case AF_INET:
785                 error = in_gif_attach(sc);
786                 break;
787 #endif
788 #ifdef INET6
789         case AF_INET6:
790                 error = in6_gif_attach(sc);
791                 break;
792 #endif
793         }
794         if (error) {
795                 /* rollback */
796                 free((caddr_t)sc->gif_psrc, M_IFADDR);
797                 free((caddr_t)sc->gif_pdst, M_IFADDR);
798                 sc->gif_psrc = osrc;
799                 sc->gif_pdst = odst;
800                 goto bad;
801         }
802
803         if (osrc)
804                 free((caddr_t)osrc, M_IFADDR);
805         if (odst)
806                 free((caddr_t)odst, M_IFADDR);
807
808         if (sc->gif_psrc && sc->gif_pdst)
809                 ifp->if_flags |= IFF_RUNNING;
810         else
811                 ifp->if_flags &= ~IFF_RUNNING;
812         splx(s);
813
814         return 0;
815
816  bad:
817         if (sc->gif_psrc && sc->gif_pdst)
818                 ifp->if_flags |= IFF_RUNNING;
819         else
820                 ifp->if_flags &= ~IFF_RUNNING;
821         splx(s);
822
823         return error;
824 }
825
826 void
827 gif_delete_tunnel(ifp)
828         struct ifnet *ifp;
829 {
830         struct gif_softc *sc = (struct gif_softc *)ifp;
831         int s;
832
833         s = splnet();
834
835         if (sc->gif_psrc) {
836                 free((caddr_t)sc->gif_psrc, M_IFADDR);
837                 sc->gif_psrc = NULL;
838         }
839         if (sc->gif_pdst) {
840                 free((caddr_t)sc->gif_pdst, M_IFADDR);
841                 sc->gif_pdst = NULL;
842         }
843         /* it is safe to detach from both */
844 #ifdef INET
845         (void)in_gif_detach(sc);
846 #endif
847 #ifdef INET6
848         (void)in6_gif_detach(sc);
849 #endif
850
851         if (sc->gif_psrc && sc->gif_pdst)
852                 ifp->if_flags |= IFF_RUNNING;
853         else
854                 ifp->if_flags &= ~IFF_RUNNING;
855         splx(s);
856 }