696d5ce04357a632a2392971c13aa09c623056ff
[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.2 2003/06/17 04:28:48 dillon 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 <sys/rman.h>
52 #include <machine/cpu.h>
53
54 #include <net/if.h>
55 #include <net/if_types.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/bpf.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #ifdef  INET
64 #include <netinet/in_var.h>
65 #include <netinet/in_gif.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ipprotosw.h>
68 #endif  /* INET */
69
70 #ifdef INET6
71 #ifndef INET
72 #include <netinet/in.h>
73 #endif
74 #include <netinet6/in6_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/in6_gif.h>
78 #include <netinet6/ip6protosw.h>
79 #endif /* INET6 */
80
81 #include <netinet/ip_encap.h>
82 #include <net/if_gif.h>
83
84 #include <net/net_osdep.h>
85
86 #define GIFNAME         "gif"
87 #define GIFDEV          "if_gif"
88 #define GIF_MAXUNIT     0x7fff  /* ifp->if_unit is only 15 bits */
89
90 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
91 static struct rman gifunits[1];
92 LIST_HEAD(, gif_softc) gif_softc_list;
93
94 int     gif_clone_create __P((struct if_clone *, int *));
95 void    gif_clone_destroy __P((struct ifnet *));
96
97 struct if_clone gif_cloner =
98     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
99
100 static int gifmodevent __P((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 resource *r;
139         struct gif_softc *sc;
140
141         if (*unit > GIF_MAXUNIT)
142                 return (ENXIO);
143
144         if (*unit < 0) {
145                 r = rman_reserve_resource(gifunits, 0, GIF_MAXUNIT, 1,
146                     RF_ALLOCATED | RF_ACTIVE, NULL);
147                 if (r == NULL)
148                         return (ENOSPC);
149                 *unit = rman_get_start(r);
150         } else {
151                 r = rman_reserve_resource(gifunits, *unit, *unit, 1,
152                     RF_ALLOCATED | RF_ACTIVE, NULL);
153                 if (r == NULL)
154                         return (EEXIST);
155         }
156         
157         sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
158         bzero(sc, sizeof(struct gif_softc));
159
160         sc->gif_if.if_softc = sc;
161         sc->gif_if.if_name = GIFNAME;
162         sc->gif_if.if_unit = *unit;
163         sc->r_unit = r;
164
165         gifattach0(sc);
166
167         LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
168         return (0);
169 }
170
171 void
172 gifattach0(sc)
173         struct gif_softc *sc;
174 {
175
176         sc->encap_cookie4 = sc->encap_cookie6 = NULL;
177
178         sc->gif_if.if_addrlen = 0;
179         sc->gif_if.if_mtu    = GIF_MTU;
180         sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
181 #if 0
182         /* turn off ingress filter */
183         sc->gif_if.if_flags  |= IFF_LINK2;
184 #endif
185         sc->gif_if.if_ioctl  = gif_ioctl;
186         sc->gif_if.if_output = gif_output;
187         sc->gif_if.if_type   = IFT_GIF;
188         sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
189         if_attach(&sc->gif_if);
190         bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
191 }
192
193 void
194 gif_clone_destroy(ifp)
195         struct ifnet *ifp;
196 {
197         int err;
198         struct gif_softc *sc = ifp->if_softc;
199
200         gif_delete_tunnel(&sc->gif_if);
201         LIST_REMOVE(sc, gif_list);
202 #ifdef INET6
203         if (sc->encap_cookie6 != NULL) {
204                 err = encap_detach(sc->encap_cookie6);
205                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
206         }
207 #endif
208 #ifdef INET
209         if (sc->encap_cookie4 != NULL) {
210                 err = encap_detach(sc->encap_cookie4);
211                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
212         }
213 #endif
214
215         bpfdetach(ifp);
216         if_detach(ifp);
217
218         err = rman_release_resource(sc->r_unit);
219         KASSERT(err == 0, ("Unexpected error freeing resource"));
220
221         free(sc, M_GIF);
222 }
223
224 static int
225 gifmodevent(mod, type, data)
226         module_t mod;
227         int type;
228         void *data;
229 {
230         int err;
231
232         switch (type) {
233         case MOD_LOAD:
234                 gifunits->rm_type = RMAN_ARRAY;
235                 gifunits->rm_descr = "configurable if_gif units";
236                 err = rman_init(gifunits);
237                 if (err != 0)
238                         return (err);
239                 err = rman_manage_region(gifunits, 0, GIF_MAXUNIT);
240                 if (err != 0) {
241                         printf("%s: gifunits: rman_manage_region: Failed %d\n",
242                             GIFNAME, err);
243                         rman_fini(gifunits);
244                         return (err);
245                 }
246                 LIST_INIT(&gif_softc_list);
247                 if_clone_attach(&gif_cloner);
248
249 #ifdef INET6
250                 ip6_gif_hlim = GIF_HLIM;
251 #endif
252
253                 break;
254         case MOD_UNLOAD:
255                 if_clone_detach(&gif_cloner);
256
257                 while (!LIST_EMPTY(&gif_softc_list))
258                         gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
259
260                 err = rman_fini(gifunits);
261                 if (err != 0)
262                         return (err);
263 #ifdef INET6
264                 ip6_gif_hlim = 0;
265 #endif
266                 break;
267         }
268         return 0;
269 }
270
271 static moduledata_t gif_mod = {
272         "if_gif",
273         gifmodevent,
274         0
275 };
276
277 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
278
279 int
280 gif_encapcheck(m, off, proto, arg)
281         const struct mbuf *m;
282         int off;
283         int proto;
284         void *arg;
285 {
286         struct ip ip;
287         struct gif_softc *sc;
288
289         sc = (struct gif_softc *)arg;
290         if (sc == NULL)
291                 return 0;
292
293         if ((sc->gif_if.if_flags & IFF_UP) == 0)
294                 return 0;
295
296         /* no physical address */
297         if (!sc->gif_psrc || !sc->gif_pdst)
298                 return 0;
299
300         switch (proto) {
301 #ifdef INET
302         case IPPROTO_IPV4:
303                 break;
304 #endif
305 #ifdef INET6
306         case IPPROTO_IPV6:
307                 break;
308 #endif
309         default:
310                 return 0;
311         }
312
313         /* Bail on short packets */
314         if (m->m_pkthdr.len < sizeof(ip))
315                 return 0;
316
317         /* LINTED const cast */
318         m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
319
320         switch (ip.ip_v) {
321 #ifdef INET
322         case 4:
323                 if (sc->gif_psrc->sa_family != AF_INET ||
324                     sc->gif_pdst->sa_family != AF_INET)
325                         return 0;
326                 return gif_encapcheck4(m, off, proto, arg);
327 #endif
328 #ifdef INET6
329         case 6:
330                 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
331                         return 0;
332                 if (sc->gif_psrc->sa_family != AF_INET6 ||
333                     sc->gif_pdst->sa_family != AF_INET6)
334                         return 0;
335                 return gif_encapcheck6(m, off, proto, arg);
336 #endif
337         default:
338                 return 0;
339         }
340 }
341
342 int
343 gif_output(ifp, m, dst, rt)
344         struct ifnet *ifp;
345         struct mbuf *m;
346         struct sockaddr *dst;
347         struct rtentry *rt;     /* added in net2 */
348 {
349         struct gif_softc *sc = (struct gif_softc*)ifp;
350         int error = 0;
351         static int called = 0;  /* XXX: MUTEX */
352
353         /*
354          * gif may cause infinite recursion calls when misconfigured.
355          * We'll prevent this by introducing upper limit.
356          * XXX: this mechanism may introduce another problem about
357          *      mutual exclusion of the variable CALLED, especially if we
358          *      use kernel thread.
359          */
360         if (++called > max_gif_nesting) {
361                 log(LOG_NOTICE,
362                     "gif_output: recursively called too many times(%d)\n",
363                     called);
364                 m_freem(m);
365                 error = EIO;    /* is there better errno? */
366                 goto end;
367         }
368
369         m->m_flags &= ~(M_BCAST|M_MCAST);
370         if (!(ifp->if_flags & IFF_UP) ||
371             sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
372                 m_freem(m);
373                 error = ENETDOWN;
374                 goto end;
375         }
376
377         if (ifp->if_bpf) {
378                 /*
379                  * We need to prepend the address family as
380                  * a four byte field.  Cons up a dummy header
381                  * to pacify bpf.  This is safe because bpf
382                  * will only read from the mbuf (i.e., it won't
383                  * try to free it or keep a pointer a to it).
384                  */
385                 struct mbuf m0;
386                 u_int32_t af = dst->sa_family;
387
388                 m0.m_next = m;
389                 m0.m_len = 4;
390                 m0.m_data = (char *)&af;
391                 
392                 bpf_mtap(ifp, &m0);
393         }
394         ifp->if_opackets++;     
395         ifp->if_obytes += m->m_pkthdr.len;
396
397         /* inner AF-specific encapsulation */
398
399         /* XXX should we check if our outer source is legal? */
400
401         /* dispatch to output logic based on outer AF */
402         switch (sc->gif_psrc->sa_family) {
403 #ifdef INET
404         case AF_INET:
405                 error = in_gif_output(ifp, dst->sa_family, m);
406                 break;
407 #endif
408 #ifdef INET6
409         case AF_INET6:
410                 error = in6_gif_output(ifp, dst->sa_family, m);
411                 break;
412 #endif
413         default:
414                 m_freem(m);             
415                 error = ENETDOWN;
416                 goto end;
417         }
418
419   end:
420         called = 0;             /* reset recursion counter */
421         if (error)
422                 ifp->if_oerrors++;
423         return error;
424 }
425
426 void
427 gif_input(m, af, ifp)
428         struct mbuf *m;
429         int af;
430         struct ifnet *ifp;
431 {
432         int s, isr;
433         struct ifqueue *ifq = NULL;
434
435         if (ifp == NULL) {
436                 /* just in case */
437                 m_freem(m);
438                 return;
439         }
440
441         m->m_pkthdr.rcvif = ifp;
442         
443         if (ifp->if_bpf) {
444                 /*
445                  * We need to prepend the address family as
446                  * a four byte field.  Cons up a dummy header
447                  * to pacify bpf.  This is safe because bpf
448                  * will only read from the mbuf (i.e., it won't
449                  * try to free it or keep a pointer a to it).
450                  */
451                 struct mbuf m0;
452                 u_int32_t af1 = af;
453                 
454                 m0.m_next = m;
455                 m0.m_len = 4;
456                 m0.m_data = (char *)&af1;
457                 
458                 bpf_mtap(ifp, &m0);
459         }
460
461         /*
462          * Put the packet to the network layer input queue according to the
463          * specified address family.
464          * Note: older versions of gif_input directly called network layer
465          * input functions, e.g. ip6_input, here.  We changed the policy to
466          * prevent too many recursive calls of such input functions, which
467          * might cause kernel panic.  But the change may introduce another
468          * problem; if the input queue is full, packets are discarded.
469          * The kernel stack overflow really happened, and we believed
470          * queue-full rarely occurs, so we changed the policy.
471          */
472         switch (af) {
473 #ifdef INET
474         case AF_INET:
475                 ifq = &ipintrq;
476                 isr = NETISR_IP;
477                 break;
478 #endif
479 #ifdef INET6
480         case AF_INET6:
481                 ifq = &ip6intrq;
482                 isr = NETISR_IPV6;
483                 break;
484 #endif
485         default:
486                 m_freem(m);
487                 return;
488         }
489
490         s = splimp();
491         if (IF_QFULL(ifq)) {
492                 IF_DROP(ifq);   /* update statistics */
493                 m_freem(m);
494                 splx(s);
495                 return;
496         }
497         ifp->if_ipackets++;
498         ifp->if_ibytes += m->m_pkthdr.len;
499         IF_ENQUEUE(ifq, m);
500         /* we need schednetisr since the address family may change */
501         schednetisr(isr);
502         splx(s);
503
504         return;
505 }
506
507 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
508 int
509 gif_ioctl(ifp, cmd, data)
510         struct ifnet *ifp;
511         u_long cmd;
512         caddr_t data;
513 {
514         struct gif_softc *sc  = (struct gif_softc*)ifp;
515         struct ifreq     *ifr = (struct ifreq*)data;
516         int error = 0, size;
517         struct sockaddr *dst, *src;
518 #ifdef  SIOCSIFMTU /* xxx */
519         u_long mtu;
520 #endif
521
522         switch (cmd) {
523         case SIOCSIFADDR:
524                 ifp->if_flags |= IFF_UP;
525                 break;
526                 
527         case SIOCSIFDSTADDR:
528                 break;
529
530         case SIOCADDMULTI:
531         case SIOCDELMULTI:
532                 break;
533
534 #ifdef  SIOCSIFMTU /* xxx */
535         case SIOCGIFMTU:
536                 break;
537
538         case SIOCSIFMTU:
539                 mtu = ifr->ifr_mtu;
540                 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
541                         return (EINVAL);
542                 ifp->if_mtu = mtu;
543                 break;
544 #endif /* SIOCSIFMTU */
545
546 #ifdef INET
547         case SIOCSIFPHYADDR:
548 #endif
549 #ifdef INET6
550         case SIOCSIFPHYADDR_IN6:
551 #endif /* INET6 */
552         case SIOCSLIFPHYADDR:
553                 switch (cmd) {
554 #ifdef INET
555                 case SIOCSIFPHYADDR:
556                         src = (struct sockaddr *)
557                                 &(((struct in_aliasreq *)data)->ifra_addr);
558                         dst = (struct sockaddr *)
559                                 &(((struct in_aliasreq *)data)->ifra_dstaddr);
560                         break;
561 #endif
562 #ifdef INET6
563                 case SIOCSIFPHYADDR_IN6:
564                         src = (struct sockaddr *)
565                                 &(((struct in6_aliasreq *)data)->ifra_addr);
566                         dst = (struct sockaddr *)
567                                 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
568                         break;
569 #endif
570                 case SIOCSLIFPHYADDR:
571                         src = (struct sockaddr *)
572                                 &(((struct if_laddrreq *)data)->addr);
573                         dst = (struct sockaddr *)
574                                 &(((struct if_laddrreq *)data)->dstaddr);
575                         break;
576                 default:
577                         return EINVAL;
578                 }
579
580                 /* sa_family must be equal */
581                 if (src->sa_family != dst->sa_family)
582                         return EINVAL;
583
584                 /* validate sa_len */
585                 switch (src->sa_family) {
586 #ifdef INET
587                 case AF_INET:
588                         if (src->sa_len != sizeof(struct sockaddr_in))
589                                 return EINVAL;
590                         break;
591 #endif
592 #ifdef INET6
593                 case AF_INET6:
594                         if (src->sa_len != sizeof(struct sockaddr_in6))
595                                 return EINVAL;
596                         break;
597 #endif
598                 default:
599                         return EAFNOSUPPORT;
600                 }
601                 switch (dst->sa_family) {
602 #ifdef INET
603                 case AF_INET:
604                         if (dst->sa_len != sizeof(struct sockaddr_in))
605                                 return EINVAL;
606                         break;
607 #endif
608 #ifdef INET6
609                 case AF_INET6:
610                         if (dst->sa_len != sizeof(struct sockaddr_in6))
611                                 return EINVAL;
612                         break;
613 #endif
614                 default:
615                         return EAFNOSUPPORT;
616                 }
617
618                 /* check sa_family looks sane for the cmd */
619                 switch (cmd) {
620                 case SIOCSIFPHYADDR:
621                         if (src->sa_family == AF_INET)
622                                 break;
623                         return EAFNOSUPPORT;
624 #ifdef INET6
625                 case SIOCSIFPHYADDR_IN6:
626                         if (src->sa_family == AF_INET6)
627                                 break;
628                         return EAFNOSUPPORT;
629 #endif /* INET6 */
630                 case SIOCSLIFPHYADDR:
631                         /* checks done in the above */
632                         break;
633                 }
634
635                 error = gif_set_tunnel(&sc->gif_if, src, dst);
636                 break;
637
638 #ifdef SIOCDIFPHYADDR
639         case SIOCDIFPHYADDR:
640                 gif_delete_tunnel(&sc->gif_if);
641                 break;
642 #endif
643                         
644         case SIOCGIFPSRCADDR:
645 #ifdef INET6
646         case SIOCGIFPSRCADDR_IN6:
647 #endif /* INET6 */
648                 if (sc->gif_psrc == NULL) {
649                         error = EADDRNOTAVAIL;
650                         goto bad;
651                 }
652                 src = sc->gif_psrc;
653                 switch (cmd) {
654 #ifdef INET
655                 case SIOCGIFPSRCADDR:
656                         dst = &ifr->ifr_addr;
657                         size = sizeof(ifr->ifr_addr);
658                         break;
659 #endif /* INET */
660 #ifdef INET6
661                 case SIOCGIFPSRCADDR_IN6:
662                         dst = (struct sockaddr *)
663                                 &(((struct in6_ifreq *)data)->ifr_addr);
664                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
665                         break;
666 #endif /* INET6 */
667                 default:
668                         error = EADDRNOTAVAIL;
669                         goto bad;
670                 }
671                 if (src->sa_len > size)
672                         return EINVAL;
673                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
674                 break;
675                         
676         case SIOCGIFPDSTADDR:
677 #ifdef INET6
678         case SIOCGIFPDSTADDR_IN6:
679 #endif /* INET6 */
680                 if (sc->gif_pdst == NULL) {
681                         error = EADDRNOTAVAIL;
682                         goto bad;
683                 }
684                 src = sc->gif_pdst;
685                 switch (cmd) {
686 #ifdef INET
687                 case SIOCGIFPDSTADDR:
688                         dst = &ifr->ifr_addr;
689                         size = sizeof(ifr->ifr_addr);
690                         break;
691 #endif /* INET */
692 #ifdef INET6
693                 case SIOCGIFPDSTADDR_IN6:
694                         dst = (struct sockaddr *)
695                                 &(((struct in6_ifreq *)data)->ifr_addr);
696                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
697                         break;
698 #endif /* INET6 */
699                 default:
700                         error = EADDRNOTAVAIL;
701                         goto bad;
702                 }
703                 if (src->sa_len > size)
704                         return EINVAL;
705                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
706                 break;
707
708         case SIOCGLIFPHYADDR:
709                 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
710                         error = EADDRNOTAVAIL;
711                         goto bad;
712                 }
713
714                 /* copy src */
715                 src = sc->gif_psrc;
716                 dst = (struct sockaddr *)
717                         &(((struct if_laddrreq *)data)->addr);
718                 size = sizeof(((struct if_laddrreq *)data)->addr);
719                 if (src->sa_len > size)
720                         return EINVAL;
721                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
722
723                 /* copy dst */
724                 src = sc->gif_pdst;
725                 dst = (struct sockaddr *)
726                         &(((struct if_laddrreq *)data)->dstaddr);
727                 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
728                 if (src->sa_len > size)
729                         return EINVAL;
730                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
731                 break;
732
733         case SIOCSIFFLAGS:
734                 /* if_ioctl() takes care of it */
735                 break;
736
737         default:
738                 error = EINVAL;
739                 break;
740         }
741  bad:
742         return error;
743 }
744
745 int
746 gif_set_tunnel(ifp, src, dst)
747         struct ifnet *ifp;
748         struct sockaddr *src;
749         struct sockaddr *dst;
750 {
751         struct gif_softc *sc = (struct gif_softc *)ifp;
752         struct gif_softc *sc2;
753         struct sockaddr *osrc, *odst, *sa;
754         int s;
755         int error = 0; 
756
757         s = splnet();
758
759         LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
760                 if (sc2 == sc)
761                         continue;
762                 if (!sc2->gif_pdst || !sc2->gif_psrc)
763                         continue;
764                 if (sc2->gif_pdst->sa_family != dst->sa_family ||
765                     sc2->gif_pdst->sa_len != dst->sa_len ||
766                     sc2->gif_psrc->sa_family != src->sa_family ||
767                     sc2->gif_psrc->sa_len != src->sa_len)
768                         continue;
769
770                 /*
771                  * Disallow parallel tunnels unless instructed
772                  * otherwise.
773                  */
774                 if (!parallel_tunnels &&
775                     bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
776                     bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
777                         error = EADDRNOTAVAIL;
778                         goto bad;
779                 }
780
781                 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
782         }
783
784         /* XXX we can detach from both, but be polite just in case */
785         if (sc->gif_psrc)
786                 switch (sc->gif_psrc->sa_family) {
787 #ifdef INET
788                 case AF_INET:
789                         (void)in_gif_detach(sc);
790                         break;
791 #endif
792 #ifdef INET6
793                 case AF_INET6:
794                         (void)in6_gif_detach(sc);
795                         break;
796 #endif
797                 }
798
799         osrc = sc->gif_psrc;
800         sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
801         bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
802         sc->gif_psrc = sa;
803
804         odst = sc->gif_pdst;
805         sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
806         bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
807         sc->gif_pdst = sa;
808
809         switch (sc->gif_psrc->sa_family) {
810 #ifdef INET
811         case AF_INET:
812                 error = in_gif_attach(sc);
813                 break;
814 #endif
815 #ifdef INET6
816         case AF_INET6:
817                 error = in6_gif_attach(sc);
818                 break;
819 #endif
820         }
821         if (error) {
822                 /* rollback */
823                 free((caddr_t)sc->gif_psrc, M_IFADDR);
824                 free((caddr_t)sc->gif_pdst, M_IFADDR);
825                 sc->gif_psrc = osrc;
826                 sc->gif_pdst = odst;
827                 goto bad;
828         }
829
830         if (osrc)
831                 free((caddr_t)osrc, M_IFADDR);
832         if (odst)
833                 free((caddr_t)odst, M_IFADDR);
834
835         if (sc->gif_psrc && sc->gif_pdst)
836                 ifp->if_flags |= IFF_RUNNING;
837         else
838                 ifp->if_flags &= ~IFF_RUNNING;
839         splx(s);
840
841         return 0;
842
843  bad:
844         if (sc->gif_psrc && sc->gif_pdst)
845                 ifp->if_flags |= IFF_RUNNING;
846         else
847                 ifp->if_flags &= ~IFF_RUNNING;
848         splx(s);
849
850         return error;
851 }
852
853 void
854 gif_delete_tunnel(ifp)
855         struct ifnet *ifp;
856 {
857         struct gif_softc *sc = (struct gif_softc *)ifp;
858         int s;
859
860         s = splnet();
861
862         if (sc->gif_psrc) {
863                 free((caddr_t)sc->gif_psrc, M_IFADDR);
864                 sc->gif_psrc = NULL;
865         }
866         if (sc->gif_pdst) {
867                 free((caddr_t)sc->gif_pdst, M_IFADDR);
868                 sc->gif_pdst = NULL;
869         }
870         /* it is safe to detach from both */
871 #ifdef INET
872         (void)in_gif_detach(sc);
873 #endif
874 #ifdef INET6
875         (void)in6_gif_detach(sc);
876 #endif
877
878         if (sc->gif_psrc && sc->gif_pdst)
879                 ifp->if_flags |= IFF_RUNNING;
880         else
881                 ifp->if_flags &= ~IFF_RUNNING;
882         splx(s);
883 }