]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_gif.c
add a sysctl to turn debug msgs on/off when built with IFMEDIA_DEBUG
[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/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/protosw.h>
51 #include <sys/conf.h>
52 #include <machine/cpu.h>
53
54 #include <net/if.h>
55 #include <net/if_clone.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/bpf.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #ifdef  INET
65 #include <netinet/in_var.h>
66 #include <netinet/in_gif.h>
67 #include <netinet/ip_var.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/scope6_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 <net/ethernet.h>
84 #include <net/if_bridgevar.h>
85 #include <net/if_gif.h>
86
87 #include <net/net_osdep.h>
88
89 #define GIFNAME         "gif"
90
91 /*
92  * gif_mtx protects the global gif_softc_list.
93  * XXX: Per-softc locking is still required.
94  */
95 static struct mtx gif_mtx;
96 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
97 static LIST_HEAD(, gif_softc) gif_softc_list;
98
99 void    (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
100 void    (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
101 void    (*ng_gif_attach_p)(struct ifnet *ifp);
102 void    (*ng_gif_detach_p)(struct ifnet *ifp);
103
104 static void     gif_start(struct ifnet *);
105 static int      gif_clone_create(struct if_clone *, int);
106 static void     gif_clone_destroy(struct ifnet *);
107
108 IFC_SIMPLE_DECLARE(gif, 0);
109
110 static int gifmodevent(module_t, int, void *);
111
112 SYSCTL_DECL(_net_link);
113 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
114     "Generic Tunnel Interface");
115 #ifndef MAX_GIF_NEST
116 /*
117  * This macro controls the default upper limitation on nesting of gif tunnels.
118  * Since, setting a large value to this macro with a careless configuration
119  * may introduce system crash, we don't allow any nestings by default.
120  * If you need to configure nested gif tunnels, you can define this macro
121  * in your kernel configuration file.  However, if you do so, please be
122  * careful to configure the tunnels so that it won't make a loop.
123  */
124 #define MAX_GIF_NEST 1
125 #endif
126 static int max_gif_nesting = MAX_GIF_NEST;
127 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
128     &max_gif_nesting, 0, "Max nested tunnels");
129
130 /*
131  * By default, we disallow creation of multiple tunnels between the same
132  * pair of addresses.  Some applications require this functionality so
133  * we allow control over this check here.
134  */
135 #ifdef XBONEHACK
136 static int parallel_tunnels = 1;
137 #else
138 static int parallel_tunnels = 0;
139 #endif
140 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
141     &parallel_tunnels, 0, "Allow parallel tunnels?");
142
143 static int
144 gif_clone_create(ifc, unit)
145         struct if_clone *ifc;
146         int unit;
147 {
148         struct gif_softc *sc;
149
150         sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
151         GIF2IFP(sc) = if_alloc(IFT_GIF);
152         if (GIF2IFP(sc) == NULL) {
153                 free(sc, M_GIF);
154                 return (ENOSPC);
155         }
156
157         GIF2IFP(sc)->if_softc = sc;
158         if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
159
160         gifattach0(sc);
161
162         mtx_lock(&gif_mtx);
163         LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
164         mtx_unlock(&gif_mtx);
165         return (0);
166 }
167
168 void
169 gifattach0(sc)
170         struct gif_softc *sc;
171 {
172
173         sc->encap_cookie4 = sc->encap_cookie6 = NULL;
174
175         GIF2IFP(sc)->if_addrlen = 0;
176         GIF2IFP(sc)->if_mtu    = GIF_MTU;
177         GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
178 #if 0
179         /* turn off ingress filter */
180         GIF2IFP(sc)->if_flags  |= IFF_LINK2;
181 #endif
182         GIF2IFP(sc)->if_ioctl  = gif_ioctl;
183         GIF2IFP(sc)->if_start  = gif_start;
184         GIF2IFP(sc)->if_output = gif_output;
185         GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
186         if_attach(GIF2IFP(sc));
187         bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
188         if (ng_gif_attach_p != NULL)
189                 (*ng_gif_attach_p)(GIF2IFP(sc));
190 }
191
192 static void
193 gif_clone_destroy(ifp)
194         struct ifnet *ifp;
195 {
196         int err;
197         struct gif_softc *sc = ifp->if_softc;
198
199         mtx_lock(&gif_mtx);
200         LIST_REMOVE(sc, gif_list);
201         mtx_unlock(&gif_mtx);
202
203         gif_delete_tunnel(ifp);
204 #ifdef INET6
205         if (sc->encap_cookie6 != NULL) {
206                 err = encap_detach(sc->encap_cookie6);
207                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
208         }
209 #endif
210 #ifdef INET
211         if (sc->encap_cookie4 != NULL) {
212                 err = encap_detach(sc->encap_cookie4);
213                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
214         }
215 #endif
216
217         if (ng_gif_detach_p != NULL)
218                 (*ng_gif_detach_p)(ifp);
219         bpfdetach(ifp);
220         if_detach(ifp);
221         if_free(ifp);
222
223         free(sc, M_GIF);
224 }
225
226 static int
227 gifmodevent(mod, type, data)
228         module_t mod;
229         int type;
230         void *data;
231 {
232
233         switch (type) {
234         case MOD_LOAD:
235                 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
236                 LIST_INIT(&gif_softc_list);
237                 if_clone_attach(&gif_cloner);
238
239 #ifdef INET6
240                 ip6_gif_hlim = GIF_HLIM;
241 #endif
242
243                 break;
244         case MOD_UNLOAD:
245                 if_clone_detach(&gif_cloner);
246                 mtx_destroy(&gif_mtx);
247 #ifdef INET6
248                 ip6_gif_hlim = 0;
249 #endif
250                 break;
251         default:
252                 return EOPNOTSUPP;
253         }
254         return 0;
255 }
256
257 static moduledata_t gif_mod = {
258         "if_gif",
259         gifmodevent,
260         0
261 };
262
263 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
264 MODULE_VERSION(if_gif, 1);
265
266 int
267 gif_encapcheck(m, off, proto, arg)
268         const struct mbuf *m;
269         int off;
270         int proto;
271         void *arg;
272 {
273         struct ip ip;
274         struct gif_softc *sc;
275
276         sc = (struct gif_softc *)arg;
277         if (sc == NULL)
278                 return 0;
279
280         if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
281                 return 0;
282
283         /* no physical address */
284         if (!sc->gif_psrc || !sc->gif_pdst)
285                 return 0;
286
287         switch (proto) {
288 #ifdef INET
289         case IPPROTO_IPV4:
290                 break;
291 #endif
292 #ifdef INET6
293         case IPPROTO_IPV6:
294                 break;
295 #endif
296         case IPPROTO_ETHERIP:
297                 break;
298
299         default:
300                 return 0;
301         }
302
303         /* Bail on short packets */
304         if (m->m_pkthdr.len < sizeof(ip))
305                 return 0;
306
307         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
308
309         switch (ip.ip_v) {
310 #ifdef INET
311         case 4:
312                 if (sc->gif_psrc->sa_family != AF_INET ||
313                     sc->gif_pdst->sa_family != AF_INET)
314                         return 0;
315                 return gif_encapcheck4(m, off, proto, arg);
316 #endif
317 #ifdef INET6
318         case 6:
319                 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
320                         return 0;
321                 if (sc->gif_psrc->sa_family != AF_INET6 ||
322                     sc->gif_pdst->sa_family != AF_INET6)
323                         return 0;
324                 return gif_encapcheck6(m, off, proto, arg);
325 #endif
326         default:
327                 return 0;
328         }
329 }
330
331 static void
332 gif_start(struct ifnet *ifp)
333 {
334         struct gif_softc *sc;
335         struct mbuf *m;
336
337         sc = ifp->if_softc;
338
339         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
340         for (;;) {
341                 IFQ_DEQUEUE(&ifp->if_snd, m);
342                 if (m == 0)
343                         break;
344
345                 gif_output(ifp, m, sc->gif_pdst, NULL);
346
347         }
348         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
349
350         return;
351 }
352
353 int
354 gif_output(ifp, m, dst, rt)
355         struct ifnet *ifp;
356         struct mbuf *m;
357         struct sockaddr *dst;
358         struct rtentry *rt;     /* added in net2 */
359 {
360         struct gif_softc *sc = ifp->if_softc;
361         struct m_tag *mtag;
362         int error = 0;
363         int gif_called;
364         u_int32_t af;
365
366 #ifdef MAC
367         error = mac_check_ifnet_transmit(ifp, m);
368         if (error) {
369                 m_freem(m);
370                 goto end;
371         }
372 #endif
373
374         /*
375          * gif may cause infinite recursion calls when misconfigured.
376          * We'll prevent this by detecting loops.
377          *
378          * High nesting level may cause stack exhaustion.
379          * We'll prevent this by introducing upper limit.
380          */
381         gif_called = 1;
382         mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
383         while (mtag != NULL) {
384                 if (*(struct ifnet **)(mtag + 1) == ifp) {
385                         log(LOG_NOTICE,
386                             "gif_output: loop detected on %s\n",
387                             (*(struct ifnet **)(mtag + 1))->if_xname);
388                         m_freem(m);
389                         error = EIO;    /* is there better errno? */
390                         goto end;
391                 }
392                 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
393                 gif_called++;
394         }
395         if (gif_called > max_gif_nesting) {
396                 log(LOG_NOTICE,
397                     "gif_output: recursively called too many times(%d)\n",
398                     gif_called);
399                 m_freem(m);
400                 error = EIO;    /* is there better errno? */
401                 goto end;
402         }
403         mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
404             M_NOWAIT);
405         if (mtag == NULL) {
406                 m_freem(m);
407                 error = ENOMEM;
408                 goto end;
409         }
410         *(struct ifnet **)(mtag + 1) = ifp;
411         m_tag_prepend(m, mtag);
412
413         m->m_flags &= ~(M_BCAST|M_MCAST);
414         if (!(ifp->if_flags & IFF_UP) ||
415             sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
416                 m_freem(m);
417                 error = ENETDOWN;
418                 goto end;
419         }
420
421         /* BPF writes need to be handled specially. */
422         if (dst->sa_family == AF_UNSPEC) {
423                 bcopy(dst->sa_data, &af, sizeof(af));
424                 dst->sa_family = af;
425         }
426
427         af = dst->sa_family;
428         if (ifp->if_bpf) {
429                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
430         }
431         ifp->if_opackets++;     
432         ifp->if_obytes += m->m_pkthdr.len;
433
434         /* override to IPPROTO_ETHERIP for bridged traffic */
435         if (ifp->if_bridge)
436                 af = AF_LINK;
437
438         /* inner AF-specific encapsulation */
439
440         /* XXX should we check if our outer source is legal? */
441
442         /* dispatch to output logic based on outer AF */
443         switch (sc->gif_psrc->sa_family) {
444 #ifdef INET
445         case AF_INET:
446                 error = in_gif_output(ifp, af, m);
447                 break;
448 #endif
449 #ifdef INET6
450         case AF_INET6:
451                 error = in6_gif_output(ifp, af, m);
452                 break;
453 #endif
454         default:
455                 m_freem(m);             
456                 error = ENETDOWN;
457                 goto end;
458         }
459
460   end:
461         if (error)
462                 ifp->if_oerrors++;
463         return error;
464 }
465
466 void
467 gif_input(m, af, ifp)
468         struct mbuf *m;
469         int af;
470         struct ifnet *ifp;
471 {
472         int isr, n;
473         struct etherip_header *eip;
474
475         if (ifp == NULL) {
476                 /* just in case */
477                 m_freem(m);
478                 return;
479         }
480
481         m->m_pkthdr.rcvif = ifp;
482
483 #ifdef MAC
484         mac_create_mbuf_from_ifnet(ifp, m);
485 #endif
486
487         if (ifp->if_bpf) {
488                 u_int32_t af1 = af;
489                 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
490         }
491
492         if (ng_gif_input_p != NULL) {
493                 (*ng_gif_input_p)(ifp, &m, af);
494                 if (m == NULL)
495                         return;
496         }
497
498         /*
499          * Put the packet to the network layer input queue according to the
500          * specified address family.
501          * Note: older versions of gif_input directly called network layer
502          * input functions, e.g. ip6_input, here.  We changed the policy to
503          * prevent too many recursive calls of such input functions, which
504          * might cause kernel panic.  But the change may introduce another
505          * problem; if the input queue is full, packets are discarded.
506          * The kernel stack overflow really happened, and we believed
507          * queue-full rarely occurs, so we changed the policy.
508          */
509         switch (af) {
510 #ifdef INET
511         case AF_INET:
512                 isr = NETISR_IP;
513                 break;
514 #endif
515 #ifdef INET6
516         case AF_INET6:
517                 isr = NETISR_IPV6;
518                 break;
519 #endif
520         case AF_LINK:
521                 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
522                 if (n > m->m_len) {
523                         m = m_pullup(m, n);
524                         if (m == NULL) {
525                                 ifp->if_ierrors++;
526                                 return;
527                         }
528                 }
529
530                 eip = mtod(m, struct etherip_header *);
531                 if (eip->eip_ver !=
532                     (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
533                         /* discard unknown versions */
534                         m_freem(m);
535                         return;
536                 }
537                 m_adj(m, sizeof(struct etherip_header));
538
539                 m->m_flags &= ~(M_BCAST|M_MCAST);
540                 m->m_pkthdr.rcvif = ifp;
541
542                 if (ifp->if_bridge)
543                         BRIDGE_INPUT(ifp, m);
544                 
545                 if (m != NULL)
546                         m_freem(m);
547                 return;
548
549         default:
550                 if (ng_gif_input_orphan_p != NULL)
551                         (*ng_gif_input_orphan_p)(ifp, m, af);
552                 else
553                         m_freem(m);
554                 return;
555         }
556
557         ifp->if_ipackets++;
558         ifp->if_ibytes += m->m_pkthdr.len;
559         netisr_dispatch(isr, m);
560 }
561
562 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
563 int
564 gif_ioctl(ifp, cmd, data)
565         struct ifnet *ifp;
566         u_long cmd;
567         caddr_t data;
568 {
569         struct gif_softc *sc  = ifp->if_softc;
570         struct ifreq     *ifr = (struct ifreq*)data;
571         int error = 0, size;
572         struct sockaddr *dst, *src;
573 #ifdef  SIOCSIFMTU /* xxx */
574         u_long mtu;
575 #endif
576
577         switch (cmd) {
578         case SIOCSIFADDR:
579                 ifp->if_flags |= IFF_UP;
580                 break;
581                 
582         case SIOCSIFDSTADDR:
583                 break;
584
585         case SIOCADDMULTI:
586         case SIOCDELMULTI:
587                 break;
588
589 #ifdef  SIOCSIFMTU /* xxx */
590         case SIOCGIFMTU:
591                 break;
592
593         case SIOCSIFMTU:
594                 mtu = ifr->ifr_mtu;
595                 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
596                         return (EINVAL);
597                 ifp->if_mtu = mtu;
598                 break;
599 #endif /* SIOCSIFMTU */
600
601 #ifdef INET
602         case SIOCSIFPHYADDR:
603 #endif
604 #ifdef INET6
605         case SIOCSIFPHYADDR_IN6:
606 #endif /* INET6 */
607         case SIOCSLIFPHYADDR:
608                 switch (cmd) {
609 #ifdef INET
610                 case SIOCSIFPHYADDR:
611                         src = (struct sockaddr *)
612                                 &(((struct in_aliasreq *)data)->ifra_addr);
613                         dst = (struct sockaddr *)
614                                 &(((struct in_aliasreq *)data)->ifra_dstaddr);
615                         break;
616 #endif
617 #ifdef INET6
618                 case SIOCSIFPHYADDR_IN6:
619                         src = (struct sockaddr *)
620                                 &(((struct in6_aliasreq *)data)->ifra_addr);
621                         dst = (struct sockaddr *)
622                                 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
623                         break;
624 #endif
625                 case SIOCSLIFPHYADDR:
626                         src = (struct sockaddr *)
627                                 &(((struct if_laddrreq *)data)->addr);
628                         dst = (struct sockaddr *)
629                                 &(((struct if_laddrreq *)data)->dstaddr);
630                         break;
631                 default:
632                         return EINVAL;
633                 }
634
635                 /* sa_family must be equal */
636                 if (src->sa_family != dst->sa_family)
637                         return EINVAL;
638
639                 /* validate sa_len */
640                 switch (src->sa_family) {
641 #ifdef INET
642                 case AF_INET:
643                         if (src->sa_len != sizeof(struct sockaddr_in))
644                                 return EINVAL;
645                         break;
646 #endif
647 #ifdef INET6
648                 case AF_INET6:
649                         if (src->sa_len != sizeof(struct sockaddr_in6))
650                                 return EINVAL;
651                         break;
652 #endif
653                 default:
654                         return EAFNOSUPPORT;
655                 }
656                 switch (dst->sa_family) {
657 #ifdef INET
658                 case AF_INET:
659                         if (dst->sa_len != sizeof(struct sockaddr_in))
660                                 return EINVAL;
661                         break;
662 #endif
663 #ifdef INET6
664                 case AF_INET6:
665                         if (dst->sa_len != sizeof(struct sockaddr_in6))
666                                 return EINVAL;
667                         break;
668 #endif
669                 default:
670                         return EAFNOSUPPORT;
671                 }
672
673                 /* check sa_family looks sane for the cmd */
674                 switch (cmd) {
675                 case SIOCSIFPHYADDR:
676                         if (src->sa_family == AF_INET)
677                                 break;
678                         return EAFNOSUPPORT;
679 #ifdef INET6
680                 case SIOCSIFPHYADDR_IN6:
681                         if (src->sa_family == AF_INET6)
682                                 break;
683                         return EAFNOSUPPORT;
684 #endif /* INET6 */
685                 case SIOCSLIFPHYADDR:
686                         /* checks done in the above */
687                         break;
688                 }
689
690                 error = gif_set_tunnel(GIF2IFP(sc), src, dst);
691                 break;
692
693 #ifdef SIOCDIFPHYADDR
694         case SIOCDIFPHYADDR:
695                 gif_delete_tunnel(GIF2IFP(sc));
696                 break;
697 #endif
698                         
699         case SIOCGIFPSRCADDR:
700 #ifdef INET6
701         case SIOCGIFPSRCADDR_IN6:
702 #endif /* INET6 */
703                 if (sc->gif_psrc == NULL) {
704                         error = EADDRNOTAVAIL;
705                         goto bad;
706                 }
707                 src = sc->gif_psrc;
708                 switch (cmd) {
709 #ifdef INET
710                 case SIOCGIFPSRCADDR:
711                         dst = &ifr->ifr_addr;
712                         size = sizeof(ifr->ifr_addr);
713                         break;
714 #endif /* INET */
715 #ifdef INET6
716                 case SIOCGIFPSRCADDR_IN6:
717                         dst = (struct sockaddr *)
718                                 &(((struct in6_ifreq *)data)->ifr_addr);
719                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
720                         break;
721 #endif /* INET6 */
722                 default:
723                         error = EADDRNOTAVAIL;
724                         goto bad;
725                 }
726                 if (src->sa_len > size)
727                         return EINVAL;
728                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
729 #ifdef INET6
730                 if (dst->sa_family == AF_INET6) {
731                         error = sa6_recoverscope((struct sockaddr_in6 *)dst);
732                         if (error != 0)
733                                 return (error);
734                 }
735 #endif
736                 break;
737                         
738         case SIOCGIFPDSTADDR:
739 #ifdef INET6
740         case SIOCGIFPDSTADDR_IN6:
741 #endif /* INET6 */
742                 if (sc->gif_pdst == NULL) {
743                         error = EADDRNOTAVAIL;
744                         goto bad;
745                 }
746                 src = sc->gif_pdst;
747                 switch (cmd) {
748 #ifdef INET
749                 case SIOCGIFPDSTADDR:
750                         dst = &ifr->ifr_addr;
751                         size = sizeof(ifr->ifr_addr);
752                         break;
753 #endif /* INET */
754 #ifdef INET6
755                 case SIOCGIFPDSTADDR_IN6:
756                         dst = (struct sockaddr *)
757                                 &(((struct in6_ifreq *)data)->ifr_addr);
758                         size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
759                         break;
760 #endif /* INET6 */
761                 default:
762                         error = EADDRNOTAVAIL;
763                         goto bad;
764                 }
765                 if (src->sa_len > size)
766                         return EINVAL;
767                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
768 #ifdef INET6
769                 if (dst->sa_family == AF_INET6) {
770                         error = sa6_recoverscope((struct sockaddr_in6 *)dst);
771                         if (error != 0)
772                                 return (error);
773                 }
774 #endif
775                 break;
776
777         case SIOCGLIFPHYADDR:
778                 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
779                         error = EADDRNOTAVAIL;
780                         goto bad;
781                 }
782
783                 /* copy src */
784                 src = sc->gif_psrc;
785                 dst = (struct sockaddr *)
786                         &(((struct if_laddrreq *)data)->addr);
787                 size = sizeof(((struct if_laddrreq *)data)->addr);
788                 if (src->sa_len > size)
789                         return EINVAL;
790                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
791
792                 /* copy dst */
793                 src = sc->gif_pdst;
794                 dst = (struct sockaddr *)
795                         &(((struct if_laddrreq *)data)->dstaddr);
796                 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
797                 if (src->sa_len > size)
798                         return EINVAL;
799                 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
800                 break;
801
802         case SIOCSIFFLAGS:
803                 /* if_ioctl() takes care of it */
804                 break;
805
806         default:
807                 error = EINVAL;
808                 break;
809         }
810  bad:
811         return error;
812 }
813
814 /*
815  * XXXRW: There's a general event-ordering issue here: the code to check
816  * if a given tunnel is already present happens before we perform a
817  * potentially blocking setup of the tunnel.  This code needs to be
818  * re-ordered so that the check and replacement can be atomic using
819  * a mutex.
820  */
821 int
822 gif_set_tunnel(ifp, src, dst)
823         struct ifnet *ifp;
824         struct sockaddr *src;
825         struct sockaddr *dst;
826 {
827         struct gif_softc *sc = ifp->if_softc;
828         struct gif_softc *sc2;
829         struct sockaddr *osrc, *odst, *sa;
830         int s;
831         int error = 0; 
832
833         s = splnet();
834
835         mtx_lock(&gif_mtx);
836         LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
837                 if (sc2 == sc)
838                         continue;
839                 if (!sc2->gif_pdst || !sc2->gif_psrc)
840                         continue;
841                 if (sc2->gif_pdst->sa_family != dst->sa_family ||
842                     sc2->gif_pdst->sa_len != dst->sa_len ||
843                     sc2->gif_psrc->sa_family != src->sa_family ||
844                     sc2->gif_psrc->sa_len != src->sa_len)
845                         continue;
846
847                 /*
848                  * Disallow parallel tunnels unless instructed
849                  * otherwise.
850                  */
851                 if (!parallel_tunnels &&
852                     bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
853                     bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
854                         error = EADDRNOTAVAIL;
855                         mtx_unlock(&gif_mtx);
856                         goto bad;
857                 }
858
859                 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
860         }
861         mtx_unlock(&gif_mtx);
862
863         /* XXX we can detach from both, but be polite just in case */
864         if (sc->gif_psrc)
865                 switch (sc->gif_psrc->sa_family) {
866 #ifdef INET
867                 case AF_INET:
868                         (void)in_gif_detach(sc);
869                         break;
870 #endif
871 #ifdef INET6
872                 case AF_INET6:
873                         (void)in6_gif_detach(sc);
874                         break;
875 #endif
876                 }
877
878         osrc = sc->gif_psrc;
879         sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
880         bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
881         sc->gif_psrc = sa;
882
883         odst = sc->gif_pdst;
884         sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
885         bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
886         sc->gif_pdst = sa;
887
888         switch (sc->gif_psrc->sa_family) {
889 #ifdef INET
890         case AF_INET:
891                 error = in_gif_attach(sc);
892                 break;
893 #endif
894 #ifdef INET6
895         case AF_INET6:
896                 /*
897                  * Check validity of the scope zone ID of the addresses, and
898                  * convert it into the kernel internal form if necessary.
899                  */
900                 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
901                 if (error != 0)
902                         break;
903                 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
904                 if (error != 0)
905                         break;
906                 error = in6_gif_attach(sc);
907                 break;
908 #endif
909         }
910         if (error) {
911                 /* rollback */
912                 free((caddr_t)sc->gif_psrc, M_IFADDR);
913                 free((caddr_t)sc->gif_pdst, M_IFADDR);
914                 sc->gif_psrc = osrc;
915                 sc->gif_pdst = odst;
916                 goto bad;
917         }
918
919         if (osrc)
920                 free((caddr_t)osrc, M_IFADDR);
921         if (odst)
922                 free((caddr_t)odst, M_IFADDR);
923
924         if (sc->gif_psrc && sc->gif_pdst)
925                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
926         else
927                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
928         splx(s);
929
930         return 0;
931
932  bad:
933         if (sc->gif_psrc && sc->gif_pdst)
934                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
935         else
936                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
937         splx(s);
938
939         return error;
940 }
941
942 void
943 gif_delete_tunnel(ifp)
944         struct ifnet *ifp;
945 {
946         struct gif_softc *sc = ifp->if_softc;
947         int s;
948
949         s = splnet();
950
951         if (sc->gif_psrc) {
952                 free((caddr_t)sc->gif_psrc, M_IFADDR);
953                 sc->gif_psrc = NULL;
954         }
955         if (sc->gif_pdst) {
956                 free((caddr_t)sc->gif_pdst, M_IFADDR);
957                 sc->gif_pdst = NULL;
958         }
959         /* it is safe to detach from both */
960 #ifdef INET
961         (void)in_gif_detach(sc);
962 #endif
963 #ifdef INET6
964         (void)in6_gif_detach(sc);
965 #endif
966
967         if (sc->gif_psrc && sc->gif_pdst)
968                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
969         else
970                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
971         splx(s);
972 }