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