2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.6 2001/07/24 19:10:18 brooks Exp $ */
31 /* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */
34 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the project nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
69 #include <sys/errno.h>
71 #include <sys/syslog.h>
72 #include <sys/protosw.h>
73 #include <kern/cpu_number.h>
76 #include <net/if_types.h>
77 #include <net/route.h>
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/ip.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_gif.h>
86 #include <netinet/ip_var.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/in6_gif.h>
94 #include <netinet6/ip6protosw.h>
97 #include <netinet/ip_encap.h>
99 #include <net/if_gif.h>
101 #include <net/net_osdep.h>
103 #define GIFNAME "gif"
104 #define GIFDEV "if_gif"
105 #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
108 static MALLOC_DEFINE(M_GIF
, "gif", "Generic Tunnel Interface");
111 TAILQ_HEAD(gifhead
, gif_softc
) gifs
= TAILQ_HEAD_INITIALIZER(gifs
);
114 void gifattach(void);
115 int gif_pre_output(struct ifnet
*ifp
, u_long protocol_family
, struct mbuf
**m0
,
116 const struct sockaddr
*dst
, caddr_t rt
, char *frame
, char *address
);
117 static void gif_create_dev(void);
118 static int gif_encapcheck(const struct mbuf
*, int, int, void*);
121 int ngif
= 0; /* number of interfaces */
125 struct protosw in_gif_protosw
=
126 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
127 in_gif_input
, 0, 0, 0,
136 struct ip6protosw in6_gif_protosw
=
137 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
138 in6_gif_input
, 0, 0, 0,
150 * This macro controls the upper limitation on nesting of gif tunnels.
151 * Since, setting a large value to this macro with a careless configuration
152 * may introduce system crash, we don't allow any nestings by default.
153 * If you need to configure nested gif tunnels, you can define this macro
154 * in your kernel configuration file. However, if you do so, please be
155 * careful to configure the tunnels so that it won't make a loop.
157 #define MAX_GIF_NEST 1
159 static int max_gif_nesting
= MAX_GIF_NEST
;
165 * Theory of operation: initially, one gif interface is created.
166 * Any time a gif interface is configured, if there are no other
167 * unconfigured gif interfaces, a new gif interface is created.
168 * BSD uses the clone mechanism to dynamically create more
171 * We have some extra glue to support DLIL.
174 /* GIF interface module support */
179 u_long
*protocol_family
)
181 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
183 /* Only one protocol may be attached to a gif interface. */
184 *protocol_family
= gif
->gif_proto
;
190 int gif_add_proto(struct ifnet
*ifp
, u_long protocol_family
, struct ddesc_head_str
*desc_head
)
192 /* Only one protocol may be attached at a time */
193 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
195 if (gif
->gif_proto
!= 0)
196 printf("gif_add_proto: request add_proto for gif%d\n", gif
->gif_if
.if_unit
);
198 gif
->gif_proto
= protocol_family
;
204 int gif_del_proto(struct ifnet
*ifp
, u_long protocol_family
)
206 if (((struct gif_softc
*)ifp
)->gif_proto
== protocol_family
)
207 ((struct gif_softc
*)ifp
)->gif_proto
= 0;
214 /* Glue code to attach inet to a gif interface through DLIL */
216 gif_attach_proto_family(
218 u_long protocol_family
)
220 struct dlil_proto_reg_str reg
;
223 bzero(®
, sizeof(reg
));
224 TAILQ_INIT(®
.demux_desc_head
);
225 reg
.interface_family
= ifp
->if_family
;
226 reg
.unit_number
= ifp
->if_unit
;
227 reg
.input
= gif_input
;
228 reg
.pre_output
= gif_pre_output
;
229 reg
.protocol_family
= protocol_family
;
231 stat
= dlil_attach_protocol(®
);
232 if (stat
&& stat
!= EEXIST
) {
233 panic("gif_attach_proto_family can't attach interface fam=%d\n", protocol_family
);
241 /* Function to setup the first gif interface */
247 /* Init the list of interfaces */
250 /* Register protocol registration functions */
251 if ( error
= dlil_reg_proto_module(AF_INET
, APPLE_IF_FAM_GIF
, gif_attach_proto_family
, NULL
) != 0)
252 printf("dlil_reg_proto_module failed for AF_INET error=%d\n", error
);
254 if ( error
= dlil_reg_proto_module(AF_INET6
, APPLE_IF_FAM_GIF
, gif_attach_proto_family
, NULL
) != 0)
255 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
257 /* Create first device */
261 /* Creates another gif device if there are none free */
265 struct gif_softc
*sc
;
268 /* Can't create more than GIF_MAXUNIT */
269 if (ngif
>= GIF_MAXUNIT
)
272 /* Check for unused gif interface */
273 TAILQ_FOREACH(sc
, &gifs
, gif_link
) {
274 /* If unused, return, no need to create a new interface */
275 if ((sc
->gif_if
.if_flags
& IFF_RUNNING
) == 0)
279 sc
= _MALLOC(sizeof(struct gif_softc
), M_DEVBUF
, M_WAITOK
);
281 log(LOG_ERR
, "gifattach: failed to allocate gif%d\n", ngif
);
285 bzero(sc
, sizeof(struct gif_softc
));
286 sc
->gif_if
.if_softc
= sc
;
287 sc
->gif_if
.if_name
= GIFNAME
;
288 sc
->gif_if
.if_unit
= ngif
;
290 sc
->encap_cookie4
= sc
->encap_cookie6
= NULL
;
292 sc
->encap_cookie4
= encap_attach_func(AF_INET
, -1,
293 gif_encapcheck
, &in_gif_protosw
, sc
);
294 if (sc
->encap_cookie4
== NULL
) {
295 printf("%s: unable to attach encap4\n", if_name(&sc
->gif_if
));
301 sc
->encap_cookie6
= encap_attach_func(AF_INET6
, -1,
302 gif_encapcheck
, (struct protosw
*)&in6_gif_protosw
, sc
);
303 if (sc
->encap_cookie6
== NULL
) {
304 if (sc
->encap_cookie4
) {
305 encap_detach(sc
->encap_cookie4
);
306 sc
->encap_cookie4
= NULL
;
308 printf("%s: unable to attach encap6\n", if_name(&sc
->gif_if
));
315 sc
->gif_if
.if_family
= APPLE_IF_FAM_GIF
;
316 sc
->gif_if
.if_mtu
= GIF_MTU
;
317 sc
->gif_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
319 /* turn off ingress filter */
320 sc
->gif_if
.if_flags
|= IFF_LINK2
;
322 sc
->gif_if
.if_demux
= gif_demux
;
323 sc
->gif_if
.if_ioctl
= gif_ioctl
;
324 sc
->gif_if
.if_output
= NULL
; /* pre_output returns error or EJUSTRETURN */
325 sc
->gif_if
.if_type
= IFT_GIF
;
326 sc
->gif_if
.if_add_proto
= gif_add_proto
;
327 sc
->gif_if
.if_del_proto
= gif_del_proto
;
328 dlil_if_attach(&sc
->gif_if
);
329 bpfattach(&sc
->gif_if
, DLT_NULL
, sizeof(u_int
));
330 TAILQ_INSERT_TAIL(&gifs
, sc
, gif_link
);
335 gif_encapcheck(m
, off
, proto
, arg
)
336 const struct mbuf
*m
;
342 struct gif_softc
*sc
;
344 sc
= (struct gif_softc
*)arg
;
348 if ((sc
->gif_if
.if_flags
& IFF_UP
) == 0)
351 /* no physical address */
352 if (!sc
->gif_psrc
|| !sc
->gif_pdst
)
368 /* LINTED const cast */
369 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
374 if (sc
->gif_psrc
->sa_family
!= AF_INET
||
375 sc
->gif_pdst
->sa_family
!= AF_INET
)
377 return gif_encapcheck4(m
, off
, proto
, arg
);
381 if (sc
->gif_psrc
->sa_family
!= AF_INET6
||
382 sc
->gif_pdst
->sa_family
!= AF_INET6
)
384 return gif_encapcheck6(m
, off
, proto
, arg
);
394 u_long protocol_family
,
396 const struct sockaddr
*dst
,
401 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
402 register struct mbuf
* m
= *m0
;
406 * gif may cause infinite recursion calls when misconfigured.
407 * We'll prevent this by introducing upper limit.
408 * XXX: this mechanism may introduce another problem about
409 * mutual exclusion of the variable CALLED, especially if we
412 if (++sc
->gif_called
> max_gif_nesting
) {
414 "gif_output: recursively called too many times(%d)\n",
416 m_freem(m
); /* free it here not in dlil_output*/
417 error
= EIO
; /* is there better errno? */
421 ifnet_touch_lastchange(ifp
);
422 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
423 if (!(ifp
->if_flags
& IFF_UP
) ||
424 sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
425 m_freem(m
); /* free it here not in dlil_output */
432 * We need to prepend the address family as
433 * a four byte field. Cons up a dummy header
434 * to pacify bpf. This is safe because bpf
435 * will only read from the mbuf (i.e., it won't
436 * try to free it or keep a pointer a to it).
439 u_int32_t protocol_family
= dst
->sa_family
;
443 m0
.m_data
= (char *)&protocol_family
;
448 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
450 /* inner AF-specific encapsulation */
452 /* XXX should we check if our outer source is legal? */
454 /* dispatch to output logic based on outer AF */
455 switch (sc
->gif_psrc
->sa_family
) {
458 error
= in_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
463 error
= in6_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
472 sc
->gif_called
= 0; /* reset recursion counter */
474 /* the mbuf was freed either by in_gif_output or in here */
475 *m0
= NULL
; /* avoid getting dlil_output freeing it */
479 error
= EJUSTRETURN
; /* if no error, packet got sent already */
488 u_long protocol_family
,
498 if (m
->m_pkthdr
.rcvif
)
499 m
->m_pkthdr
.rcvif
= gifp
;
503 * We need to prepend the address family as
504 * a four byte field. Cons up a dummy header
505 * to pacify bpf. This is safe because bpf
506 * will only read from the mbuf (i.e., it won't
507 * try to free it or keep a pointer a to it).
510 u_int32_t protocol_family1
= protocol_family
;
514 m0
.m_data
= (char *)&protocol_family1
;
520 * Put the packet to the network layer input queue according to the
521 * specified address family.
522 * Note: older versions of gif_input directly called network layer
523 * input functions, e.g. ip6_input, here. We changed the policy to
524 * prevent too many recursive calls of such input functions, which
525 * might cause kernel panic. But the change may introduce another
526 * problem; if the input queue is full, packets are discarded.
527 * We believed it rarely occurs and changed the policy. If we find
528 * it occurs more times than we thought, we may change the policy
531 proto_input(protocol_family
, m
);
533 gifp
->if_ibytes
+= m
->m_pkthdr
.len
;
538 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
540 gif_ioctl(ifp
, cmd
, data
)
545 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
546 struct ifreq
*ifr
= (struct ifreq
*)data
;
548 struct sockaddr
*dst
, *src
;
552 struct gif_softc
*sc2
;
565 #ifdef SIOCSIFMTU /* xxx */
573 if (mtu
< GIF_MTU_MIN
|| mtu
> GIF_MTU_MAX
) {
579 #endif /* SIOCSIFMTU */
583 case SIOCSIFPHYADDR_IN6
:
585 case SIOCSLIFPHYADDR
:
589 src
= (struct sockaddr
*)
590 &(((struct in_aliasreq
*)data
)->ifra_addr
);
591 dst
= (struct sockaddr
*)
592 &(((struct in_aliasreq
*)data
)->ifra_dstaddr
);
596 case SIOCSIFPHYADDR_IN6
:
597 src
= (struct sockaddr
*)
598 &(((struct in6_aliasreq
*)data
)->ifra_addr
);
599 dst
= (struct sockaddr
*)
600 &(((struct in6_aliasreq
*)data
)->ifra_dstaddr
);
603 case SIOCSLIFPHYADDR
:
604 src
= (struct sockaddr
*)
605 &(((struct if_laddrreq
*)data
)->addr
);
606 dst
= (struct sockaddr
*)
607 &(((struct if_laddrreq
*)data
)->dstaddr
);
610 /* sa_family must be equal */
611 if (src
->sa_family
!= dst
->sa_family
)
614 /* validate sa_len */
615 switch (src
->sa_family
) {
618 if (src
->sa_len
!= sizeof(struct sockaddr_in
))
624 if (src
->sa_len
!= sizeof(struct sockaddr_in6
))
631 switch (dst
->sa_family
) {
634 if (dst
->sa_len
!= sizeof(struct sockaddr_in
))
640 if (dst
->sa_len
!= sizeof(struct sockaddr_in6
))
648 /* check sa_family looks sane for the cmd */
651 if (src
->sa_family
== AF_INET
)
655 case SIOCSIFPHYADDR_IN6
:
656 if (src
->sa_family
== AF_INET6
)
660 case SIOCSLIFPHYADDR
:
661 /* checks done in the above */
665 ifnet_head_lock_shared();
666 TAILQ_FOREACH(ifp2
, &ifnet_head
, if_link
) {
667 if (strcmp(ifp2
->if_name
, GIFNAME
) != 0)
669 sc2
= ifp2
->if_softc
;
672 if (!sc2
->gif_pdst
|| !sc2
->gif_psrc
)
674 if (sc2
->gif_pdst
->sa_family
!= dst
->sa_family
||
675 sc2
->gif_pdst
->sa_len
!= dst
->sa_len
||
676 sc2
->gif_psrc
->sa_family
!= src
->sa_family
||
677 sc2
->gif_psrc
->sa_len
!= src
->sa_len
)
680 /* can't configure same pair of address onto two gifs */
681 if (bcmp(sc2
->gif_pdst
, dst
, dst
->sa_len
) == 0 &&
682 bcmp(sc2
->gif_psrc
, src
, src
->sa_len
) == 0) {
683 error
= EADDRNOTAVAIL
;
689 /* can't configure multiple multi-dest interfaces */
690 #define multidest(x) \
691 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
693 #define multidest6(x) \
694 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
696 if (dst
->sa_family
== AF_INET
&&
697 multidest(dst
) && multidest(sc2
->gif_pdst
)) {
698 error
= EADDRNOTAVAIL
;
703 if (dst
->sa_family
== AF_INET6
&&
704 multidest6(dst
) && multidest6(sc2
->gif_pdst
)) {
705 error
= EADDRNOTAVAIL
;
714 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
715 sa
= (struct sockaddr
*)_MALLOC(src
->sa_len
, M_IFADDR
, M_WAITOK
);
716 bcopy((caddr_t
)src
, (caddr_t
)sa
, src
->sa_len
);
720 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
721 sa
= (struct sockaddr
*)_MALLOC(dst
->sa_len
, M_IFADDR
, M_WAITOK
);
722 bcopy((caddr_t
)dst
, (caddr_t
)sa
, dst
->sa_len
);
725 ifp
->if_flags
|= IFF_RUNNING
;
728 if_up(ifp
); /* mark interface UP and send up RTM_IFINFO */
730 /* Make sure at least one unused device is still available */
738 #ifdef SIOCDIFPHYADDR
741 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
745 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
748 /* change the IFF_{UP, RUNNING} flag as well? */
752 case SIOCGIFPSRCADDR
:
754 case SIOCGIFPSRCADDR_IN6
:
756 if (sc
->gif_psrc
== NULL
) {
757 error
= EADDRNOTAVAIL
;
763 case SIOCGIFPSRCADDR
:
764 dst
= &ifr
->ifr_addr
;
765 size
= sizeof(ifr
->ifr_addr
);
769 case SIOCGIFPSRCADDR_IN6
:
770 dst
= (struct sockaddr
*)
771 &(((struct in6_ifreq
*)data
)->ifr_addr
);
772 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
776 error
= EADDRNOTAVAIL
;
779 if (src
->sa_len
> size
)
781 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
784 case SIOCGIFPDSTADDR
:
786 case SIOCGIFPDSTADDR_IN6
:
788 if (sc
->gif_pdst
== NULL
) {
789 error
= EADDRNOTAVAIL
;
795 case SIOCGIFPDSTADDR
:
796 dst
= &ifr
->ifr_addr
;
797 size
= sizeof(ifr
->ifr_addr
);
801 case SIOCGIFPDSTADDR_IN6
:
802 dst
= (struct sockaddr
*)
803 &(((struct in6_ifreq
*)data
)->ifr_addr
);
804 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
808 error
= EADDRNOTAVAIL
;
811 if (src
->sa_len
> size
)
813 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
816 case SIOCGLIFPHYADDR
:
817 if (sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
818 error
= EADDRNOTAVAIL
;
824 dst
= (struct sockaddr
*)
825 &(((struct if_laddrreq
*)data
)->addr
);
826 size
= sizeof(((struct if_laddrreq
*)data
)->addr
);
827 if (src
->sa_len
> size
)
829 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
833 dst
= (struct sockaddr
*)
834 &(((struct if_laddrreq
*)data
)->dstaddr
);
835 size
= sizeof(((struct if_laddrreq
*)data
)->dstaddr
);
836 if (src
->sa_len
> size
)
838 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
842 /* if_ioctl() takes care of it */
854 /* This function is not used in our stack */
856 gif_delete_tunnel(sc
)
857 struct gif_softc
*sc
;
859 /* XXX: NetBSD protects this function with splsoftnet() */
862 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
866 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
869 /* change the IFF_UP flag as well? */