2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.6 2001/07/24 19:10:18 brooks Exp $ */
23 /* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */
26 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the project nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/errno.h>
63 #include <sys/syslog.h>
64 #include <sys/protosw.h>
65 #include <kern/cpu_number.h>
68 #include <net/if_types.h>
69 #include <net/netisr.h>
70 #include <net/route.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
77 #include <netinet/in_var.h>
78 #include <netinet/in_gif.h>
79 #include <netinet/ip_var.h>
83 #include <netinet6/in6_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/in6_gif.h>
87 #include <netinet6/ip6protosw.h>
90 #include <netinet/ip_encap.h>
92 #include <net/if_gif.h>
94 #include <net/net_osdep.h>
97 #define GIFDEV "if_gif"
98 #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
101 static MALLOC_DEFINE(M_GIF
, "gif", "Generic Tunnel Interface");
104 TAILQ_HEAD(gifhead
, gif_softc
) gifs
= TAILQ_HEAD_INITIALIZER(gifs
);
107 void gifattach
__P((void));
108 int gif_pre_output
__P((struct ifnet
*, register struct mbuf
**, struct sockaddr
*,
109 caddr_t
, char *, char *, u_long
));
110 static void gif_create_dev(void);
111 static int gif_encapcheck(const struct mbuf
*, int, int, void*);
114 int ngif
= 0; /* number of interfaces */
118 struct protosw in_gif_protosw
=
119 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
120 in_gif_input
, 0, 0, 0,
128 struct ip6protosw in6_gif_protosw
=
129 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
141 * This macro controls the upper limitation on nesting of gif tunnels.
142 * Since, setting a large value to this macro with a careless configuration
143 * may introduce system crash, we don't allow any nestings by default.
144 * If you need to configure nested gif tunnels, you can define this macro
145 * in your kernel configuration file. However, if you do so, please be
146 * careful to configure the tunnels so that it won't make a loop.
148 #define MAX_GIF_NEST 1
150 static int max_gif_nesting
= MAX_GIF_NEST
;
156 * Theory of operation: initially, one gif interface is created.
157 * Any time a gif interface is configured, if there are no other
158 * unconfigured gif interfaces, a new gif interface is created.
159 * BSD uses the clone mechanism to dynamically create more
162 * We have some extra glue to support DLIL.
165 /* GIF interface module support */
166 int gif_demux(ifp
, m
, frame_header
, proto
)
170 struct if_proto
**proto
;
172 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
174 /* Only one protocol may be attached to a gif interface. */
175 *proto
= gif
->gif_proto
;
181 int gif_add_if(struct ifnet
*ifp
)
183 ifp
->if_demux
= gif_demux
;
189 int gif_del_if(struct ifnet
*ifp
)
195 int gif_add_proto(struct ddesc_head_str
*desc_head
, struct if_proto
*proto
, u_long dl_tag
)
197 /* Only one protocol may be attached at a time */
198 struct gif_softc
* gif
= (struct gif_softc
*)proto
->ifp
;
200 if (gif
->gif_proto
!= NULL
)
201 printf("gif_add_proto: request add_proto for gif%d\n", gif
->gif_if
.if_unit
);
203 gif
->gif_proto
= proto
;
209 int gif_del_proto(struct if_proto
*proto
, u_long dl_tag
)
211 if (((struct gif_softc
*)proto
->ifp
)->gif_proto
== proto
)
212 ((struct gif_softc
*)proto
->ifp
)->gif_proto
= NULL
;
224 void gif_reg_if_mods()
226 struct dlil_ifmod_reg_str gif_ifmod
;
228 bzero(&gif_ifmod
, sizeof(gif_ifmod
));
229 gif_ifmod
.add_if
= gif_add_if
;
230 gif_ifmod
.del_if
= gif_del_if
;
231 gif_ifmod
.add_proto
= gif_add_proto
;
232 gif_ifmod
.del_proto
= gif_del_proto
;
233 gif_ifmod
.ifmod_ioctl
= 0;
234 gif_ifmod
.shutdown
= gif_shutdown
;
236 if (dlil_reg_if_modules(APPLE_IF_FAM_GIF
, &gif_ifmod
))
237 panic("Couldn't register gif modules\n");
241 /* Glue code to attach inet to a gif interface through DLIL */
243 u_long
gif_attach_proto_family(struct ifnet
*ifp
, int af
)
245 struct dlil_proto_reg_str reg
;
246 struct dlil_demux_desc desc
;
251 /* Check if we're already attached */
252 stat
= dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, af
, &dl_tag
);
256 TAILQ_INIT(®
.demux_desc_head
);
257 desc
.type
= DLIL_DESC_RAW
;
258 desc
.variants
.bitmask
.proto_id_length
= 0;
259 desc
.variants
.bitmask
.proto_id
= 0;
260 desc
.variants
.bitmask
.proto_id_mask
= 0;
261 desc
.native_type
= (char *) &native
;
262 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc
, next
);
263 reg
.interface_family
= ifp
->if_family
;
264 reg
.unit_number
= ifp
->if_unit
;
265 reg
.input
= gif_input
;
266 reg
.pre_output
= gif_pre_output
;
270 reg
.default_proto
= 0;
271 reg
.protocol_family
= af
;
273 stat
= dlil_attach_protocol(®
, &dl_tag
);
275 panic("gif_attach_proto_family can't attach interface fam=%d\n", af
);
281 u_long
gif_detach_proto_family(struct ifnet
*ifp
, int af
)
283 u_long ip_dl_tag
= 0;
286 stat
= dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, af
, &ip_dl_tag
);
288 stat
= dlil_detach_protocol(ip_dl_tag
);
290 printf("WARNING: gif_detach can't detach IP fam=%d from interface\n", af
);
298 /* Function to setup the first gif interface */
302 /* Init the list of interfaces */
305 gif_reg_if_mods(); /* DLIL modules */
307 /* Create first device */
311 /* Creates another gif device if there are none free */
315 struct gif_softc
*sc
;
318 /* Can't create more than GIF_MAXUNIT */
319 if (ngif
>= GIF_MAXUNIT
)
322 /* Check for unused gif interface */
323 TAILQ_FOREACH(sc
, &gifs
, gif_link
) {
324 /* If unused, return, no need to create a new interface */
325 if ((sc
->gif_if
.if_flags
& IFF_RUNNING
) == 0)
329 sc
= _MALLOC(sizeof(struct gif_softc
), M_DEVBUF
, M_WAITOK
);
331 log(LOG_ERR
, "gifattach: failed to allocate gif%d\n", ngif
);
335 bzero(sc
, sizeof(struct gif_softc
));
336 sc
->gif_if
.if_softc
= sc
;
337 sc
->gif_if
.if_name
= GIFNAME
;
338 sc
->gif_if
.if_unit
= ngif
;
340 sc
->encap_cookie4
= sc
->encap_cookie6
= NULL
;
342 sc
->encap_cookie4
= encap_attach_func(AF_INET
, -1,
343 gif_encapcheck
, &in_gif_protosw
, sc
);
344 if (sc
->encap_cookie4
== NULL
) {
345 printf("%s: unable to attach encap4\n", if_name(&sc
->gif_if
));
351 sc
->encap_cookie6
= encap_attach_func(AF_INET6
, -1,
352 gif_encapcheck
, (struct protosw
*)&in6_gif_protosw
, sc
);
353 if (sc
->encap_cookie6
== NULL
) {
354 if (sc
->encap_cookie4
) {
355 encap_detach(sc
->encap_cookie4
);
356 sc
->encap_cookie4
= NULL
;
358 printf("%s: unable to attach encap6\n", if_name(&sc
->gif_if
));
364 sc
->gif_if
.if_family
= APPLE_IF_FAM_GIF
;
365 sc
->gif_if
.if_mtu
= GIF_MTU
;
366 sc
->gif_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
368 /* turn off ingress filter */
369 sc
->gif_if
.if_flags
|= IFF_LINK2
;
371 sc
->gif_if
.if_ioctl
= gif_ioctl
;
372 sc
->gif_if
.if_output
= NULL
; /* pre_output returns error or EJUSTRETURN */
373 sc
->gif_if
.if_type
= IFT_GIF
;
374 dlil_if_attach(&sc
->gif_if
);
375 bpfattach(&sc
->gif_if
, DLT_NULL
, sizeof(u_int
));
376 TAILQ_INSERT_TAIL(&gifs
, sc
, gif_link
);
381 gif_encapcheck(m
, off
, proto
, arg
)
382 const struct mbuf
*m
;
388 struct gif_softc
*sc
;
390 sc
= (struct gif_softc
*)arg
;
394 if ((sc
->gif_if
.if_flags
& IFF_UP
) == 0)
397 /* no physical address */
398 if (!sc
->gif_psrc
|| !sc
->gif_pdst
)
414 /* LINTED const cast */
415 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
420 if (sc
->gif_psrc
->sa_family
!= AF_INET
||
421 sc
->gif_pdst
->sa_family
!= AF_INET
)
423 return gif_encapcheck4(m
, off
, proto
, arg
);
427 if (sc
->gif_psrc
->sa_family
!= AF_INET6
||
428 sc
->gif_pdst
->sa_family
!= AF_INET6
)
430 return gif_encapcheck6(m
, off
, proto
, arg
);
438 gif_pre_output(ifp
, m0
, dst
, rt
, frame
, address
, dl_tag
)
441 struct sockaddr
*dst
;
447 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
448 register struct mbuf
* m
= *m0
;
450 static int called
= 0; /* XXX: MUTEX */
453 * gif may cause infinite recursion calls when misconfigured.
454 * We'll prevent this by introducing upper limit.
455 * XXX: this mechanism may introduce another problem about
456 * mutual exclusion of the variable CALLED, especially if we
459 if (++called
> max_gif_nesting
) {
461 "gif_output: recursively called too many times(%d)\n",
463 error
= EIO
; /* is there better errno? */
467 getmicrotime(&ifp
->if_lastchange
);
468 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
469 if (!(ifp
->if_flags
& IFF_UP
) ||
470 sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
477 * We need to prepend the address family as
478 * a four byte field. Cons up a dummy header
479 * to pacify bpf. This is safe because bpf
480 * will only read from the mbuf (i.e., it won't
481 * try to free it or keep a pointer a to it).
484 u_int32_t af
= dst
->sa_family
;
488 m0
.m_data
= (char *)&af
;
493 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
495 /* inner AF-specific encapsulation */
497 /* XXX should we check if our outer source is legal? */
499 /* dispatch to output logic based on outer AF */
500 switch (sc
->gif_psrc
->sa_family
) {
503 error
= in_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
508 error
= in6_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
517 called
= 0; /* reset recursion counter */
521 error
= EJUSTRETURN
; /* if no error, packet got sent already */
526 gif_input(m
, frame_header
, gifp
, dl_tag
, sync_ok
)
534 struct ifqueue
*ifq
= 0;
543 /* Assume packet is of type of protocol attached to this interface */
544 af
= ((struct gif_softc
*)(gifp
->if_softc
))->gif_proto
->protocol_family
;
546 if (m
->m_pkthdr
.rcvif
)
547 m
->m_pkthdr
.rcvif
= gifp
;
551 * We need to prepend the address family as
552 * a four byte field. Cons up a dummy header
553 * to pacify bpf. This is safe because bpf
554 * will only read from the mbuf (i.e., it won't
555 * try to free it or keep a pointer a to it).
562 m0
.m_data
= (char *)&af1
;
568 * Put the packet to the network layer input queue according to the
569 * specified address family.
570 * Note: older versions of gif_input directly called network layer
571 * input functions, e.g. ip6_input, here. We changed the policy to
572 * prevent too many recursive calls of such input functions, which
573 * might cause kernel panic. But the change may introduce another
574 * problem; if the input queue is full, packets are discarded.
575 * We believed it rarely occurs and changed the policy. If we find
576 * it occurs more times than we thought, we may change the policy
594 return (EJUSTRETURN
);
599 IF_DROP(ifq
); /* update statistics */
602 return (EJUSTRETURN
);
605 /* we need schednetisr since the address family may change */
608 gifp
->if_ibytes
+= m
->m_pkthdr
.len
;
614 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
616 gif_ioctl(ifp
, cmd
, data
)
621 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
622 struct ifreq
*ifr
= (struct ifreq
*)data
;
624 struct sockaddr
*dst
, *src
;
628 struct gif_softc
*sc2
;
641 #ifdef SIOCSIFMTU /* xxx */
649 if (mtu
< GIF_MTU_MIN
|| mtu
> GIF_MTU_MAX
) {
655 #endif /* SIOCSIFMTU */
659 case SIOCSIFPHYADDR_IN6
:
661 case SIOCSLIFPHYADDR
:
665 src
= (struct sockaddr
*)
666 &(((struct in_aliasreq
*)data
)->ifra_addr
);
667 dst
= (struct sockaddr
*)
668 &(((struct in_aliasreq
*)data
)->ifra_dstaddr
);
672 case SIOCSIFPHYADDR_IN6
:
673 src
= (struct sockaddr
*)
674 &(((struct in6_aliasreq
*)data
)->ifra_addr
);
675 dst
= (struct sockaddr
*)
676 &(((struct in6_aliasreq
*)data
)->ifra_dstaddr
);
679 case SIOCSLIFPHYADDR
:
680 src
= (struct sockaddr
*)
681 &(((struct if_laddrreq
*)data
)->addr
);
682 dst
= (struct sockaddr
*)
683 &(((struct if_laddrreq
*)data
)->dstaddr
);
686 /* sa_family must be equal */
687 if (src
->sa_family
!= dst
->sa_family
)
690 /* validate sa_len */
691 switch (src
->sa_family
) {
694 if (src
->sa_len
!= sizeof(struct sockaddr_in
))
700 if (src
->sa_len
!= sizeof(struct sockaddr_in6
))
707 switch (dst
->sa_family
) {
710 if (dst
->sa_len
!= sizeof(struct sockaddr_in
))
716 if (dst
->sa_len
!= sizeof(struct sockaddr_in6
))
724 /* check sa_family looks sane for the cmd */
727 if (src
->sa_family
== AF_INET
)
731 case SIOCSIFPHYADDR_IN6
:
732 if (src
->sa_family
== AF_INET6
)
736 case SIOCSLIFPHYADDR
:
737 /* checks done in the above */
741 TAILQ_FOREACH(ifp2
, &ifnet
, if_link
) {
742 if (strcmp(ifp2
->if_name
, GIFNAME
) != 0)
744 sc2
= ifp2
->if_softc
;
747 if (!sc2
->gif_pdst
|| !sc2
->gif_psrc
)
749 if (sc2
->gif_pdst
->sa_family
!= dst
->sa_family
||
750 sc2
->gif_pdst
->sa_len
!= dst
->sa_len
||
751 sc2
->gif_psrc
->sa_family
!= src
->sa_family
||
752 sc2
->gif_psrc
->sa_len
!= src
->sa_len
)
755 /* can't configure same pair of address onto two gifs */
756 if (bcmp(sc2
->gif_pdst
, dst
, dst
->sa_len
) == 0 &&
757 bcmp(sc2
->gif_psrc
, src
, src
->sa_len
) == 0) {
758 error
= EADDRNOTAVAIL
;
763 /* can't configure multiple multi-dest interfaces */
764 #define multidest(x) \
765 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
767 #define multidest6(x) \
768 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
770 if (dst
->sa_family
== AF_INET
&&
771 multidest(dst
) && multidest(sc2
->gif_pdst
)) {
772 error
= EADDRNOTAVAIL
;
776 if (dst
->sa_family
== AF_INET6
&&
777 multidest6(dst
) && multidest6(sc2
->gif_pdst
)) {
778 error
= EADDRNOTAVAIL
;
785 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
786 sa
= (struct sockaddr
*)_MALLOC(src
->sa_len
, M_IFADDR
, M_WAITOK
);
787 bcopy((caddr_t
)src
, (caddr_t
)sa
, src
->sa_len
);
791 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
792 sa
= (struct sockaddr
*)_MALLOC(dst
->sa_len
, M_IFADDR
, M_WAITOK
);
793 bcopy((caddr_t
)dst
, (caddr_t
)sa
, dst
->sa_len
);
796 ifp
->if_flags
|= IFF_RUNNING
;
798 gif_attach_proto_family(ifp
, src
->sa_family
);
801 if_up(ifp
); /* mark interface UP and send up RTM_IFINFO */
803 /* Make sure at least one unused device is still available */
811 #ifdef SIOCDIFPHYADDR
814 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
818 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
821 /* change the IFF_{UP, RUNNING} flag as well? */
825 case SIOCGIFPSRCADDR
:
827 case SIOCGIFPSRCADDR_IN6
:
829 if (sc
->gif_psrc
== NULL
) {
830 error
= EADDRNOTAVAIL
;
836 case SIOCGIFPSRCADDR
:
837 dst
= &ifr
->ifr_addr
;
838 size
= sizeof(ifr
->ifr_addr
);
842 case SIOCGIFPSRCADDR_IN6
:
843 dst
= (struct sockaddr
*)
844 &(((struct in6_ifreq
*)data
)->ifr_addr
);
845 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
849 error
= EADDRNOTAVAIL
;
852 if (src
->sa_len
> size
)
854 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
857 case SIOCGIFPDSTADDR
:
859 case SIOCGIFPDSTADDR_IN6
:
861 if (sc
->gif_pdst
== NULL
) {
862 error
= EADDRNOTAVAIL
;
868 case SIOCGIFPDSTADDR
:
869 dst
= &ifr
->ifr_addr
;
870 size
= sizeof(ifr
->ifr_addr
);
874 case SIOCGIFPDSTADDR_IN6
:
875 dst
= (struct sockaddr
*)
876 &(((struct in6_ifreq
*)data
)->ifr_addr
);
877 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
881 error
= EADDRNOTAVAIL
;
884 if (src
->sa_len
> size
)
886 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
889 case SIOCGLIFPHYADDR
:
890 if (sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
891 error
= EADDRNOTAVAIL
;
897 dst
= (struct sockaddr
*)
898 &(((struct if_laddrreq
*)data
)->addr
);
899 size
= sizeof(((struct if_laddrreq
*)data
)->addr
);
900 if (src
->sa_len
> size
)
902 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
906 dst
= (struct sockaddr
*)
907 &(((struct if_laddrreq
*)data
)->dstaddr
);
908 size
= sizeof(((struct if_laddrreq
*)data
)->dstaddr
);
909 if (src
->sa_len
> size
)
911 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
915 /* if_ioctl() takes care of it */
927 gif_delete_tunnel(sc
)
928 struct gif_softc
*sc
;
930 /* XXX: NetBSD protects this function with splsoftnet() */
933 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
937 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
940 /* change the IFF_UP flag as well? */