2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.6 2001/07/24 19:10:18 brooks Exp $ */
24 /* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */
27 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. Neither the name of the project nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/errno.h>
64 #include <sys/syslog.h>
65 #include <sys/protosw.h>
66 #include <kern/cpu_number.h>
69 #include <net/if_types.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(void);
108 int gif_pre_output(struct ifnet
*ifp
, u_long protocol_family
, struct mbuf
**m0
,
109 const struct sockaddr
*dst
, caddr_t rt
, char *frame
, char *address
);
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,
129 struct ip6protosw in6_gif_protosw
=
130 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
131 in6_gif_input
, 0, 0, 0,
143 * This macro controls the upper limitation on nesting of gif tunnels.
144 * Since, setting a large value to this macro with a careless configuration
145 * may introduce system crash, we don't allow any nestings by default.
146 * If you need to configure nested gif tunnels, you can define this macro
147 * in your kernel configuration file. However, if you do so, please be
148 * careful to configure the tunnels so that it won't make a loop.
150 #define MAX_GIF_NEST 1
152 static int max_gif_nesting
= MAX_GIF_NEST
;
158 * Theory of operation: initially, one gif interface is created.
159 * Any time a gif interface is configured, if there are no other
160 * unconfigured gif interfaces, a new gif interface is created.
161 * BSD uses the clone mechanism to dynamically create more
164 * We have some extra glue to support DLIL.
167 /* GIF interface module support */
172 u_long
*protocol_family
)
174 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
176 /* Only one protocol may be attached to a gif interface. */
177 *protocol_family
= gif
->gif_proto
;
183 int gif_add_proto(struct ifnet
*ifp
, u_long protocol_family
, struct ddesc_head_str
*desc_head
)
185 /* Only one protocol may be attached at a time */
186 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
188 if (gif
->gif_proto
!= 0)
189 printf("gif_add_proto: request add_proto for gif%d\n", gif
->gif_if
.if_unit
);
191 gif
->gif_proto
= protocol_family
;
197 int gif_del_proto(struct ifnet
*ifp
, u_long protocol_family
)
199 if (((struct gif_softc
*)ifp
)->gif_proto
== protocol_family
)
200 ((struct gif_softc
*)ifp
)->gif_proto
= 0;
207 /* Glue code to attach inet to a gif interface through DLIL */
209 gif_attach_proto_family(
211 u_long protocol_family
)
213 struct dlil_proto_reg_str reg
;
216 bzero(®
, sizeof(reg
));
217 TAILQ_INIT(®
.demux_desc_head
);
218 reg
.interface_family
= ifp
->if_family
;
219 reg
.unit_number
= ifp
->if_unit
;
220 reg
.input
= gif_input
;
221 reg
.pre_output
= gif_pre_output
;
222 reg
.protocol_family
= protocol_family
;
224 stat
= dlil_attach_protocol(®
);
225 if (stat
&& stat
!= EEXIST
) {
226 panic("gif_attach_proto_family can't attach interface fam=%d\n", protocol_family
);
234 /* Function to setup the first gif interface */
240 /* Init the list of interfaces */
243 /* Register protocol registration functions */
244 if ( error
= dlil_reg_proto_module(AF_INET
, APPLE_IF_FAM_GIF
, gif_attach_proto_family
, NULL
) != 0)
245 printf("dlil_reg_proto_module failed for AF_INET error=%d\n", error
);
247 if ( error
= dlil_reg_proto_module(AF_INET6
, APPLE_IF_FAM_GIF
, gif_attach_proto_family
, NULL
) != 0)
248 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
250 /* Create first device */
254 /* Creates another gif device if there are none free */
258 struct gif_softc
*sc
;
261 /* Can't create more than GIF_MAXUNIT */
262 if (ngif
>= GIF_MAXUNIT
)
265 /* Check for unused gif interface */
266 TAILQ_FOREACH(sc
, &gifs
, gif_link
) {
267 /* If unused, return, no need to create a new interface */
268 if ((sc
->gif_if
.if_flags
& IFF_RUNNING
) == 0)
272 sc
= _MALLOC(sizeof(struct gif_softc
), M_DEVBUF
, M_WAITOK
);
274 log(LOG_ERR
, "gifattach: failed to allocate gif%d\n", ngif
);
278 bzero(sc
, sizeof(struct gif_softc
));
279 sc
->gif_if
.if_softc
= sc
;
280 sc
->gif_if
.if_name
= GIFNAME
;
281 sc
->gif_if
.if_unit
= ngif
;
283 sc
->encap_cookie4
= sc
->encap_cookie6
= NULL
;
285 sc
->encap_cookie4
= encap_attach_func(AF_INET
, -1,
286 gif_encapcheck
, &in_gif_protosw
, sc
);
287 if (sc
->encap_cookie4
== NULL
) {
288 printf("%s: unable to attach encap4\n", if_name(&sc
->gif_if
));
294 sc
->encap_cookie6
= encap_attach_func(AF_INET6
, -1,
295 gif_encapcheck
, (struct protosw
*)&in6_gif_protosw
, sc
);
296 if (sc
->encap_cookie6
== NULL
) {
297 if (sc
->encap_cookie4
) {
298 encap_detach(sc
->encap_cookie4
);
299 sc
->encap_cookie4
= NULL
;
301 printf("%s: unable to attach encap6\n", if_name(&sc
->gif_if
));
308 sc
->gif_if
.if_family
= APPLE_IF_FAM_GIF
;
309 sc
->gif_if
.if_mtu
= GIF_MTU
;
310 sc
->gif_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
312 /* turn off ingress filter */
313 sc
->gif_if
.if_flags
|= IFF_LINK2
;
315 sc
->gif_if
.if_demux
= gif_demux
;
316 sc
->gif_if
.if_ioctl
= gif_ioctl
;
317 sc
->gif_if
.if_output
= NULL
; /* pre_output returns error or EJUSTRETURN */
318 sc
->gif_if
.if_type
= IFT_GIF
;
319 sc
->gif_if
.if_add_proto
= gif_add_proto
;
320 sc
->gif_if
.if_del_proto
= gif_del_proto
;
321 dlil_if_attach(&sc
->gif_if
);
322 bpfattach(&sc
->gif_if
, DLT_NULL
, sizeof(u_int
));
323 TAILQ_INSERT_TAIL(&gifs
, sc
, gif_link
);
328 gif_encapcheck(m
, off
, proto
, arg
)
329 const struct mbuf
*m
;
335 struct gif_softc
*sc
;
337 sc
= (struct gif_softc
*)arg
;
341 if ((sc
->gif_if
.if_flags
& IFF_UP
) == 0)
344 /* no physical address */
345 if (!sc
->gif_psrc
|| !sc
->gif_pdst
)
361 /* LINTED const cast */
362 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
367 if (sc
->gif_psrc
->sa_family
!= AF_INET
||
368 sc
->gif_pdst
->sa_family
!= AF_INET
)
370 return gif_encapcheck4(m
, off
, proto
, arg
);
374 if (sc
->gif_psrc
->sa_family
!= AF_INET6
||
375 sc
->gif_pdst
->sa_family
!= AF_INET6
)
377 return gif_encapcheck6(m
, off
, proto
, arg
);
387 u_long protocol_family
,
389 const struct sockaddr
*dst
,
394 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
395 register struct mbuf
* m
= *m0
;
399 * gif may cause infinite recursion calls when misconfigured.
400 * We'll prevent this by introducing upper limit.
401 * XXX: this mechanism may introduce another problem about
402 * mutual exclusion of the variable CALLED, especially if we
405 if (++sc
->gif_called
> max_gif_nesting
) {
407 "gif_output: recursively called too many times(%d)\n",
409 m_freem(m
); /* free it here not in dlil_output*/
410 error
= EIO
; /* is there better errno? */
414 ifnet_touch_lastchange(ifp
);
415 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
416 if (!(ifp
->if_flags
& IFF_UP
) ||
417 sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
418 m_freem(m
); /* free it here not in dlil_output */
425 * We need to prepend the address family as
426 * a four byte field. Cons up a dummy header
427 * to pacify bpf. This is safe because bpf
428 * will only read from the mbuf (i.e., it won't
429 * try to free it or keep a pointer a to it).
432 u_int32_t protocol_family
= dst
->sa_family
;
436 m0
.m_data
= (char *)&protocol_family
;
441 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
443 /* inner AF-specific encapsulation */
445 /* XXX should we check if our outer source is legal? */
447 /* dispatch to output logic based on outer AF */
448 switch (sc
->gif_psrc
->sa_family
) {
451 error
= in_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
456 error
= in6_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
465 sc
->gif_called
= 0; /* reset recursion counter */
467 /* the mbuf was freed either by in_gif_output or in here */
468 *m0
= NULL
; /* avoid getting dlil_output freeing it */
472 error
= EJUSTRETURN
; /* if no error, packet got sent already */
481 u_long protocol_family
,
491 if (m
->m_pkthdr
.rcvif
)
492 m
->m_pkthdr
.rcvif
= gifp
;
496 * We need to prepend the address family as
497 * a four byte field. Cons up a dummy header
498 * to pacify bpf. This is safe because bpf
499 * will only read from the mbuf (i.e., it won't
500 * try to free it or keep a pointer a to it).
503 u_int32_t protocol_family1
= protocol_family
;
507 m0
.m_data
= (char *)&protocol_family1
;
513 * Put the packet to the network layer input queue according to the
514 * specified address family.
515 * Note: older versions of gif_input directly called network layer
516 * input functions, e.g. ip6_input, here. We changed the policy to
517 * prevent too many recursive calls of such input functions, which
518 * might cause kernel panic. But the change may introduce another
519 * problem; if the input queue is full, packets are discarded.
520 * We believed it rarely occurs and changed the policy. If we find
521 * it occurs more times than we thought, we may change the policy
524 proto_input(protocol_family
, m
);
526 gifp
->if_ibytes
+= m
->m_pkthdr
.len
;
531 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
533 gif_ioctl(ifp
, cmd
, data
)
538 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
539 struct ifreq
*ifr
= (struct ifreq
*)data
;
541 struct sockaddr
*dst
, *src
;
545 struct gif_softc
*sc2
;
558 #ifdef SIOCSIFMTU /* xxx */
566 if (mtu
< GIF_MTU_MIN
|| mtu
> GIF_MTU_MAX
) {
572 #endif /* SIOCSIFMTU */
576 case SIOCSIFPHYADDR_IN6
:
578 case SIOCSLIFPHYADDR
:
582 src
= (struct sockaddr
*)
583 &(((struct in_aliasreq
*)data
)->ifra_addr
);
584 dst
= (struct sockaddr
*)
585 &(((struct in_aliasreq
*)data
)->ifra_dstaddr
);
589 case SIOCSIFPHYADDR_IN6
:
590 src
= (struct sockaddr
*)
591 &(((struct in6_aliasreq
*)data
)->ifra_addr
);
592 dst
= (struct sockaddr
*)
593 &(((struct in6_aliasreq
*)data
)->ifra_dstaddr
);
596 case SIOCSLIFPHYADDR
:
597 src
= (struct sockaddr
*)
598 &(((struct if_laddrreq
*)data
)->addr
);
599 dst
= (struct sockaddr
*)
600 &(((struct if_laddrreq
*)data
)->dstaddr
);
603 /* sa_family must be equal */
604 if (src
->sa_family
!= dst
->sa_family
)
607 /* validate sa_len */
608 switch (src
->sa_family
) {
611 if (src
->sa_len
!= sizeof(struct sockaddr_in
))
617 if (src
->sa_len
!= sizeof(struct sockaddr_in6
))
624 switch (dst
->sa_family
) {
627 if (dst
->sa_len
!= sizeof(struct sockaddr_in
))
633 if (dst
->sa_len
!= sizeof(struct sockaddr_in6
))
641 /* check sa_family looks sane for the cmd */
644 if (src
->sa_family
== AF_INET
)
648 case SIOCSIFPHYADDR_IN6
:
649 if (src
->sa_family
== AF_INET6
)
653 case SIOCSLIFPHYADDR
:
654 /* checks done in the above */
658 ifnet_head_lock_shared();
659 TAILQ_FOREACH(ifp2
, &ifnet_head
, if_link
) {
660 if (strcmp(ifp2
->if_name
, GIFNAME
) != 0)
662 sc2
= ifp2
->if_softc
;
665 if (!sc2
->gif_pdst
|| !sc2
->gif_psrc
)
667 if (sc2
->gif_pdst
->sa_family
!= dst
->sa_family
||
668 sc2
->gif_pdst
->sa_len
!= dst
->sa_len
||
669 sc2
->gif_psrc
->sa_family
!= src
->sa_family
||
670 sc2
->gif_psrc
->sa_len
!= src
->sa_len
)
673 /* can't configure same pair of address onto two gifs */
674 if (bcmp(sc2
->gif_pdst
, dst
, dst
->sa_len
) == 0 &&
675 bcmp(sc2
->gif_psrc
, src
, src
->sa_len
) == 0) {
676 error
= EADDRNOTAVAIL
;
682 /* can't configure multiple multi-dest interfaces */
683 #define multidest(x) \
684 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
686 #define multidest6(x) \
687 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
689 if (dst
->sa_family
== AF_INET
&&
690 multidest(dst
) && multidest(sc2
->gif_pdst
)) {
691 error
= EADDRNOTAVAIL
;
696 if (dst
->sa_family
== AF_INET6
&&
697 multidest6(dst
) && multidest6(sc2
->gif_pdst
)) {
698 error
= EADDRNOTAVAIL
;
707 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
708 sa
= (struct sockaddr
*)_MALLOC(src
->sa_len
, M_IFADDR
, M_WAITOK
);
709 bcopy((caddr_t
)src
, (caddr_t
)sa
, src
->sa_len
);
713 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
714 sa
= (struct sockaddr
*)_MALLOC(dst
->sa_len
, M_IFADDR
, M_WAITOK
);
715 bcopy((caddr_t
)dst
, (caddr_t
)sa
, dst
->sa_len
);
718 ifp
->if_flags
|= IFF_RUNNING
;
721 if_up(ifp
); /* mark interface UP and send up RTM_IFINFO */
723 /* Make sure at least one unused device is still available */
731 #ifdef SIOCDIFPHYADDR
734 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
738 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
741 /* change the IFF_{UP, RUNNING} flag as well? */
745 case SIOCGIFPSRCADDR
:
747 case SIOCGIFPSRCADDR_IN6
:
749 if (sc
->gif_psrc
== NULL
) {
750 error
= EADDRNOTAVAIL
;
756 case SIOCGIFPSRCADDR
:
757 dst
= &ifr
->ifr_addr
;
758 size
= sizeof(ifr
->ifr_addr
);
762 case SIOCGIFPSRCADDR_IN6
:
763 dst
= (struct sockaddr
*)
764 &(((struct in6_ifreq
*)data
)->ifr_addr
);
765 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
769 error
= EADDRNOTAVAIL
;
772 if (src
->sa_len
> size
)
774 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
777 case SIOCGIFPDSTADDR
:
779 case SIOCGIFPDSTADDR_IN6
:
781 if (sc
->gif_pdst
== NULL
) {
782 error
= EADDRNOTAVAIL
;
788 case SIOCGIFPDSTADDR
:
789 dst
= &ifr
->ifr_addr
;
790 size
= sizeof(ifr
->ifr_addr
);
794 case SIOCGIFPDSTADDR_IN6
:
795 dst
= (struct sockaddr
*)
796 &(((struct in6_ifreq
*)data
)->ifr_addr
);
797 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
801 error
= EADDRNOTAVAIL
;
804 if (src
->sa_len
> size
)
806 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
809 case SIOCGLIFPHYADDR
:
810 if (sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
811 error
= EADDRNOTAVAIL
;
817 dst
= (struct sockaddr
*)
818 &(((struct if_laddrreq
*)data
)->addr
);
819 size
= sizeof(((struct if_laddrreq
*)data
)->addr
);
820 if (src
->sa_len
> size
)
822 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
826 dst
= (struct sockaddr
*)
827 &(((struct if_laddrreq
*)data
)->dstaddr
);
828 size
= sizeof(((struct if_laddrreq
*)data
)->dstaddr
);
829 if (src
->sa_len
> size
)
831 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
835 /* if_ioctl() takes care of it */
847 /* This function is not used in our stack */
849 gif_delete_tunnel(sc
)
850 struct gif_softc
*sc
;
852 /* XXX: NetBSD protects this function with splsoftnet() */
855 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
859 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
862 /* change the IFF_UP flag as well? */