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/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
76 #include <netinet/in_var.h>
77 #include <netinet/in_gif.h>
78 #include <netinet/ip_var.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet6/in6_gif.h>
86 #include <netinet6/ip6protosw.h>
89 #include <netinet/ip_encap.h>
91 #include <net/if_gif.h>
93 #include <net/net_osdep.h>
96 #define GIFDEV "if_gif"
97 #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
100 static MALLOC_DEFINE(M_GIF
, "gif", "Generic Tunnel Interface");
103 TAILQ_HEAD(gifhead
, gif_softc
) gifs
= TAILQ_HEAD_INITIALIZER(gifs
);
106 void gifattach(void);
107 int gif_pre_output(struct ifnet
*ifp
, u_long protocol_family
, struct mbuf
**m0
,
108 const struct sockaddr
*dst
, caddr_t rt
, char *frame
, char *address
);
109 static void gif_create_dev(void);
110 static int gif_encapcheck(const struct mbuf
*, int, int, void*);
113 int ngif
= 0; /* number of interfaces */
117 struct protosw in_gif_protosw
=
118 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
119 in_gif_input
, 0, 0, 0,
128 struct ip6protosw in6_gif_protosw
=
129 { SOCK_RAW
, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC
|PR_ADDR
,
130 in6_gif_input
, 0, 0, 0,
142 * This macro controls the upper limitation on nesting of gif tunnels.
143 * Since, setting a large value to this macro with a careless configuration
144 * may introduce system crash, we don't allow any nestings by default.
145 * If you need to configure nested gif tunnels, you can define this macro
146 * in your kernel configuration file. However, if you do so, please be
147 * careful to configure the tunnels so that it won't make a loop.
149 #define MAX_GIF_NEST 1
151 static int max_gif_nesting
= MAX_GIF_NEST
;
157 * Theory of operation: initially, one gif interface is created.
158 * Any time a gif interface is configured, if there are no other
159 * unconfigured gif interfaces, a new gif interface is created.
160 * BSD uses the clone mechanism to dynamically create more
163 * We have some extra glue to support DLIL.
166 /* GIF interface module support */
171 u_long
*protocol_family
)
173 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
175 /* Only one protocol may be attached to a gif interface. */
176 *protocol_family
= gif
->gif_proto
;
182 int gif_add_proto(struct ifnet
*ifp
, u_long protocol_family
, struct ddesc_head_str
*desc_head
)
184 /* Only one protocol may be attached at a time */
185 struct gif_softc
* gif
= (struct gif_softc
*)ifp
->if_softc
;
187 if (gif
->gif_proto
!= 0)
188 printf("gif_add_proto: request add_proto for gif%d\n", gif
->gif_if
.if_unit
);
190 gif
->gif_proto
= protocol_family
;
196 int gif_del_proto(struct ifnet
*ifp
, u_long protocol_family
)
198 if (((struct gif_softc
*)ifp
)->gif_proto
== protocol_family
)
199 ((struct gif_softc
*)ifp
)->gif_proto
= 0;
206 /* Glue code to attach inet to a gif interface through DLIL */
208 gif_attach_proto_family(
210 u_long protocol_family
)
212 struct dlil_proto_reg_str reg
;
215 bzero(®
, sizeof(reg
));
216 TAILQ_INIT(®
.demux_desc_head
);
217 reg
.interface_family
= ifp
->if_family
;
218 reg
.unit_number
= ifp
->if_unit
;
219 reg
.input
= gif_input
;
220 reg
.pre_output
= gif_pre_output
;
221 reg
.protocol_family
= protocol_family
;
223 stat
= dlil_attach_protocol(®
);
224 if (stat
&& stat
!= EEXIST
) {
225 panic("gif_attach_proto_family can't attach interface fam=%d\n", protocol_family
);
233 /* Function to setup the first gif interface */
239 /* Init the list of interfaces */
242 /* Register protocol registration functions */
243 if ( error
= dlil_reg_proto_module(AF_INET
, APPLE_IF_FAM_GIF
, gif_attach_proto_family
, NULL
) != 0)
244 printf("dlil_reg_proto_module failed for AF_INET error=%d\n", error
);
246 if ( error
= dlil_reg_proto_module(AF_INET6
, APPLE_IF_FAM_GIF
, gif_attach_proto_family
, NULL
) != 0)
247 printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
249 /* Create first device */
253 /* Creates another gif device if there are none free */
257 struct gif_softc
*sc
;
260 /* Can't create more than GIF_MAXUNIT */
261 if (ngif
>= GIF_MAXUNIT
)
264 /* Check for unused gif interface */
265 TAILQ_FOREACH(sc
, &gifs
, gif_link
) {
266 /* If unused, return, no need to create a new interface */
267 if ((sc
->gif_if
.if_flags
& IFF_RUNNING
) == 0)
271 sc
= _MALLOC(sizeof(struct gif_softc
), M_DEVBUF
, M_WAITOK
);
273 log(LOG_ERR
, "gifattach: failed to allocate gif%d\n", ngif
);
277 bzero(sc
, sizeof(struct gif_softc
));
278 sc
->gif_if
.if_softc
= sc
;
279 sc
->gif_if
.if_name
= GIFNAME
;
280 sc
->gif_if
.if_unit
= ngif
;
282 sc
->encap_cookie4
= sc
->encap_cookie6
= NULL
;
284 sc
->encap_cookie4
= encap_attach_func(AF_INET
, -1,
285 gif_encapcheck
, &in_gif_protosw
, sc
);
286 if (sc
->encap_cookie4
== NULL
) {
287 printf("%s: unable to attach encap4\n", if_name(&sc
->gif_if
));
293 sc
->encap_cookie6
= encap_attach_func(AF_INET6
, -1,
294 gif_encapcheck
, (struct protosw
*)&in6_gif_protosw
, sc
);
295 if (sc
->encap_cookie6
== NULL
) {
296 if (sc
->encap_cookie4
) {
297 encap_detach(sc
->encap_cookie4
);
298 sc
->encap_cookie4
= NULL
;
300 printf("%s: unable to attach encap6\n", if_name(&sc
->gif_if
));
307 sc
->gif_if
.if_family
= APPLE_IF_FAM_GIF
;
308 sc
->gif_if
.if_mtu
= GIF_MTU
;
309 sc
->gif_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
311 /* turn off ingress filter */
312 sc
->gif_if
.if_flags
|= IFF_LINK2
;
314 sc
->gif_if
.if_demux
= gif_demux
;
315 sc
->gif_if
.if_ioctl
= gif_ioctl
;
316 sc
->gif_if
.if_output
= NULL
; /* pre_output returns error or EJUSTRETURN */
317 sc
->gif_if
.if_type
= IFT_GIF
;
318 sc
->gif_if
.if_add_proto
= gif_add_proto
;
319 sc
->gif_if
.if_del_proto
= gif_del_proto
;
320 dlil_if_attach(&sc
->gif_if
);
321 bpfattach(&sc
->gif_if
, DLT_NULL
, sizeof(u_int
));
322 TAILQ_INSERT_TAIL(&gifs
, sc
, gif_link
);
327 gif_encapcheck(m
, off
, proto
, arg
)
328 const struct mbuf
*m
;
334 struct gif_softc
*sc
;
336 sc
= (struct gif_softc
*)arg
;
340 if ((sc
->gif_if
.if_flags
& IFF_UP
) == 0)
343 /* no physical address */
344 if (!sc
->gif_psrc
|| !sc
->gif_pdst
)
360 /* LINTED const cast */
361 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
366 if (sc
->gif_psrc
->sa_family
!= AF_INET
||
367 sc
->gif_pdst
->sa_family
!= AF_INET
)
369 return gif_encapcheck4(m
, off
, proto
, arg
);
373 if (sc
->gif_psrc
->sa_family
!= AF_INET6
||
374 sc
->gif_pdst
->sa_family
!= AF_INET6
)
376 return gif_encapcheck6(m
, off
, proto
, arg
);
386 u_long protocol_family
,
388 const struct sockaddr
*dst
,
393 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
394 register struct mbuf
* m
= *m0
;
398 * gif may cause infinite recursion calls when misconfigured.
399 * We'll prevent this by introducing upper limit.
400 * XXX: this mechanism may introduce another problem about
401 * mutual exclusion of the variable CALLED, especially if we
404 if (++sc
->gif_called
> max_gif_nesting
) {
406 "gif_output: recursively called too many times(%d)\n",
408 m_freem(m
); /* free it here not in dlil_output*/
409 error
= EIO
; /* is there better errno? */
413 ifnet_touch_lastchange(ifp
);
414 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
415 if (!(ifp
->if_flags
& IFF_UP
) ||
416 sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
417 m_freem(m
); /* free it here not in dlil_output */
424 * We need to prepend the address family as
425 * a four byte field. Cons up a dummy header
426 * to pacify bpf. This is safe because bpf
427 * will only read from the mbuf (i.e., it won't
428 * try to free it or keep a pointer a to it).
431 u_int32_t protocol_family
= dst
->sa_family
;
435 m0
.m_data
= (char *)&protocol_family
;
440 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
442 /* inner AF-specific encapsulation */
444 /* XXX should we check if our outer source is legal? */
446 /* dispatch to output logic based on outer AF */
447 switch (sc
->gif_psrc
->sa_family
) {
450 error
= in_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
455 error
= in6_gif_output(ifp
, dst
->sa_family
, m
, (struct rtentry
*)rt
);
464 sc
->gif_called
= 0; /* reset recursion counter */
466 /* the mbuf was freed either by in_gif_output or in here */
467 *m0
= NULL
; /* avoid getting dlil_output freeing it */
471 error
= EJUSTRETURN
; /* if no error, packet got sent already */
480 u_long protocol_family
,
490 if (m
->m_pkthdr
.rcvif
)
491 m
->m_pkthdr
.rcvif
= gifp
;
495 * We need to prepend the address family as
496 * a four byte field. Cons up a dummy header
497 * to pacify bpf. This is safe because bpf
498 * will only read from the mbuf (i.e., it won't
499 * try to free it or keep a pointer a to it).
502 u_int32_t protocol_family1
= protocol_family
;
506 m0
.m_data
= (char *)&protocol_family1
;
512 * Put the packet to the network layer input queue according to the
513 * specified address family.
514 * Note: older versions of gif_input directly called network layer
515 * input functions, e.g. ip6_input, here. We changed the policy to
516 * prevent too many recursive calls of such input functions, which
517 * might cause kernel panic. But the change may introduce another
518 * problem; if the input queue is full, packets are discarded.
519 * We believed it rarely occurs and changed the policy. If we find
520 * it occurs more times than we thought, we may change the policy
523 proto_input(protocol_family
, m
);
525 gifp
->if_ibytes
+= m
->m_pkthdr
.len
;
530 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
532 gif_ioctl(ifp
, cmd
, data
)
537 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
538 struct ifreq
*ifr
= (struct ifreq
*)data
;
540 struct sockaddr
*dst
, *src
;
544 struct gif_softc
*sc2
;
557 #ifdef SIOCSIFMTU /* xxx */
565 if (mtu
< GIF_MTU_MIN
|| mtu
> GIF_MTU_MAX
) {
571 #endif /* SIOCSIFMTU */
575 case SIOCSIFPHYADDR_IN6
:
577 case SIOCSLIFPHYADDR
:
581 src
= (struct sockaddr
*)
582 &(((struct in_aliasreq
*)data
)->ifra_addr
);
583 dst
= (struct sockaddr
*)
584 &(((struct in_aliasreq
*)data
)->ifra_dstaddr
);
588 case SIOCSIFPHYADDR_IN6
:
589 src
= (struct sockaddr
*)
590 &(((struct in6_aliasreq
*)data
)->ifra_addr
);
591 dst
= (struct sockaddr
*)
592 &(((struct in6_aliasreq
*)data
)->ifra_dstaddr
);
595 case SIOCSLIFPHYADDR
:
596 src
= (struct sockaddr
*)
597 &(((struct if_laddrreq
*)data
)->addr
);
598 dst
= (struct sockaddr
*)
599 &(((struct if_laddrreq
*)data
)->dstaddr
);
602 /* sa_family must be equal */
603 if (src
->sa_family
!= dst
->sa_family
)
606 /* validate sa_len */
607 switch (src
->sa_family
) {
610 if (src
->sa_len
!= sizeof(struct sockaddr_in
))
616 if (src
->sa_len
!= sizeof(struct sockaddr_in6
))
623 switch (dst
->sa_family
) {
626 if (dst
->sa_len
!= sizeof(struct sockaddr_in
))
632 if (dst
->sa_len
!= sizeof(struct sockaddr_in6
))
640 /* check sa_family looks sane for the cmd */
643 if (src
->sa_family
== AF_INET
)
647 case SIOCSIFPHYADDR_IN6
:
648 if (src
->sa_family
== AF_INET6
)
652 case SIOCSLIFPHYADDR
:
653 /* checks done in the above */
657 ifnet_head_lock_shared();
658 TAILQ_FOREACH(ifp2
, &ifnet_head
, if_link
) {
659 if (strcmp(ifp2
->if_name
, GIFNAME
) != 0)
661 sc2
= ifp2
->if_softc
;
664 if (!sc2
->gif_pdst
|| !sc2
->gif_psrc
)
666 if (sc2
->gif_pdst
->sa_family
!= dst
->sa_family
||
667 sc2
->gif_pdst
->sa_len
!= dst
->sa_len
||
668 sc2
->gif_psrc
->sa_family
!= src
->sa_family
||
669 sc2
->gif_psrc
->sa_len
!= src
->sa_len
)
672 /* can't configure same pair of address onto two gifs */
673 if (bcmp(sc2
->gif_pdst
, dst
, dst
->sa_len
) == 0 &&
674 bcmp(sc2
->gif_psrc
, src
, src
->sa_len
) == 0) {
675 error
= EADDRNOTAVAIL
;
681 /* can't configure multiple multi-dest interfaces */
682 #define multidest(x) \
683 (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
685 #define multidest6(x) \
686 (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
688 if (dst
->sa_family
== AF_INET
&&
689 multidest(dst
) && multidest(sc2
->gif_pdst
)) {
690 error
= EADDRNOTAVAIL
;
695 if (dst
->sa_family
== AF_INET6
&&
696 multidest6(dst
) && multidest6(sc2
->gif_pdst
)) {
697 error
= EADDRNOTAVAIL
;
706 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
707 sa
= (struct sockaddr
*)_MALLOC(src
->sa_len
, M_IFADDR
, M_WAITOK
);
708 bcopy((caddr_t
)src
, (caddr_t
)sa
, src
->sa_len
);
712 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
713 sa
= (struct sockaddr
*)_MALLOC(dst
->sa_len
, M_IFADDR
, M_WAITOK
);
714 bcopy((caddr_t
)dst
, (caddr_t
)sa
, dst
->sa_len
);
717 ifp
->if_flags
|= IFF_RUNNING
;
720 if_up(ifp
); /* mark interface UP and send up RTM_IFINFO */
722 /* Make sure at least one unused device is still available */
730 #ifdef SIOCDIFPHYADDR
733 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
737 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
740 /* change the IFF_{UP, RUNNING} flag as well? */
744 case SIOCGIFPSRCADDR
:
746 case SIOCGIFPSRCADDR_IN6
:
748 if (sc
->gif_psrc
== NULL
) {
749 error
= EADDRNOTAVAIL
;
755 case SIOCGIFPSRCADDR
:
756 dst
= &ifr
->ifr_addr
;
757 size
= sizeof(ifr
->ifr_addr
);
761 case SIOCGIFPSRCADDR_IN6
:
762 dst
= (struct sockaddr
*)
763 &(((struct in6_ifreq
*)data
)->ifr_addr
);
764 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
768 error
= EADDRNOTAVAIL
;
771 if (src
->sa_len
> size
)
773 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
776 case SIOCGIFPDSTADDR
:
778 case SIOCGIFPDSTADDR_IN6
:
780 if (sc
->gif_pdst
== NULL
) {
781 error
= EADDRNOTAVAIL
;
787 case SIOCGIFPDSTADDR
:
788 dst
= &ifr
->ifr_addr
;
789 size
= sizeof(ifr
->ifr_addr
);
793 case SIOCGIFPDSTADDR_IN6
:
794 dst
= (struct sockaddr
*)
795 &(((struct in6_ifreq
*)data
)->ifr_addr
);
796 size
= sizeof(((struct in6_ifreq
*)data
)->ifr_addr
);
800 error
= EADDRNOTAVAIL
;
803 if (src
->sa_len
> size
)
805 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
808 case SIOCGLIFPHYADDR
:
809 if (sc
->gif_psrc
== NULL
|| sc
->gif_pdst
== NULL
) {
810 error
= EADDRNOTAVAIL
;
816 dst
= (struct sockaddr
*)
817 &(((struct if_laddrreq
*)data
)->addr
);
818 size
= sizeof(((struct if_laddrreq
*)data
)->addr
);
819 if (src
->sa_len
> size
)
821 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
825 dst
= (struct sockaddr
*)
826 &(((struct if_laddrreq
*)data
)->dstaddr
);
827 size
= sizeof(((struct if_laddrreq
*)data
)->dstaddr
);
828 if (src
->sa_len
> size
)
830 bcopy((caddr_t
)src
, (caddr_t
)dst
, src
->sa_len
);
834 /* if_ioctl() takes care of it */
846 /* This function is not used in our stack */
848 gif_delete_tunnel(sc
)
849 struct gif_softc
*sc
;
851 /* XXX: NetBSD protects this function with splsoftnet() */
854 FREE((caddr_t
)sc
->gif_psrc
, M_IFADDR
);
858 FREE((caddr_t
)sc
->gif_pdst
, M_IFADDR
);
861 /* change the IFF_UP flag as well? */