diff --git a/usr.sbin/greconfig/Makefile b/usr.sbin/greconfig/Makefile new file mode 100644 index 000000000000..65322c2c5fbb --- /dev/null +++ b/usr.sbin/greconfig/Makefile @@ -0,0 +1,6 @@ +# $NetBSD: Makefile,v 1.1 1998/09/13 20:50:55 hwr Exp $ +# + +PROG= greconfig +MAN= greconfig.8 +.include diff --git a/usr.sbin/greconfig/README.gre b/usr.sbin/greconfig/README.gre new file mode 100644 index 000000000000..01c4f7106bc4 --- /dev/null +++ b/usr.sbin/greconfig/README.gre @@ -0,0 +1,73 @@ +$Id: README.gre,v 1.1 1998/09/13 20:50:55 hwr Exp $ +--------------------------------------------------------------------------- + A GRE Tunnel for NetBSD + + Heiko W.Rupp + + +This is a very alpha implementation of GRE packet encapsulation. +Generic Route Encapsulation (GRE) is described in RFC 1701 and 1702. +The interface also supports IPIP (protocol 4) encapsulation, +as described in RFC 2003 but default is GRE mode. + +Basically GRE allows to tunnel not only IP over IP, but also provides +for other protocols like Appletalk etc. Cisco routers do their multicast +tunnels over GRE (1). The hope is, that with GRE encapsulation, a method +is found to encapsulate all other protocols with one mechanism and reduce +the need for own tunneling in ip_mroute etc. + +This implementation is (as said) still alpha, but works for me. +Successful testing has been done for GRE encapsulation with Cisco routers +and NetBSD boxes as remote and with Cisco routers and Linux 2.0.35 for +IPIP encapsulation. + +The files: + + net/if_gre.[ch] : output interface, encapsulates packets. + netinet/ip_gre.[ch]: input part, deencapsulates packets. Is IP only + at the moment and only supports GRE without options. + netinet/in.h : Addition of IPPROTO_GRE + netinet/in_proto.c : Addition of protocol switch for GRE , additional + protocol switch for IPIP, when MROUTING is not + defined. + conf/files : mention new files for GRE + greconfig.c : Utility to set/display tunnel endpoints / mode. + greconfig.8 : Manpage for greconfig. + gre.4 : Documentation of driver plus example. + + +Installation: + + On NetBSD: add "pseudo-device gre " + to your kernel config file with being the number of tunnel interfaces + you want. Then recompile and install the new kernel. + + Also compile greconfig.c: cc -o greconfig greconfig.c + Grectl is used to set/display tunnel endpoints. In case when IP is + encapsulated, this is not needed, but e.g. in the AppleTalk over IP + case. + +Todos/Bugs: + Support other protocols for encapsulation + + The compute_route() code in if_gre.c toggles the last bit of the + IP-address to provoke the search for a less specific route than the + one directly over the tunnel to prevent loops. This is possibly not + the best solution. + + GRE RFC not yet fully implemented (no GRE options), no other protocols + yet than IP over IP. + RFC 2003 also not yet fully supportet wrt. options etc. + + Code quality is surely not best and possibly not near anything like KNF + + Traceroute does not work yet over the tunnel :( + + BPF does probably not yet work (it might, but last time I looked, + it bombed, so I #if 0'd it out). And also only in outgoing + direction. + +--------------------------------------------------------------------------- + +(1) tunnels that end on a mrouted have tunnel mode DVMRP set + diff --git a/usr.sbin/greconfig/greconfig.8 b/usr.sbin/greconfig/greconfig.8 new file mode 100644 index 000000000000..12f5c56a650c --- /dev/null +++ b/usr.sbin/greconfig/greconfig.8 @@ -0,0 +1,113 @@ +.\" $NetBSD: greconfig.8,v 1.1 1998/09/13 20:50:55 hwr Exp $ +.\" +.\" Copyright 1998 (c) The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Heiko W.Rupp +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the NetBSD +.\" Foundation, Inc. and its contributors. +.\" 4. Neither the name of the The NetBSD Foundation nor the names of its +.\" contributors may be used to endorse or promote products derived +.\" from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd 14 August 1998 +.Dt GRECTL 8 +.Os NetBSD 4 +.Sh NAME +.Nm greconfig +.Nd set characteristics of the +.Xr gre 4 +pseudo device. +.Sh SYNOPSIS +.Nm +.Fl i Ar unit +.Op Fl v +.Nm "" +.Fl i Ar unit +.Op Fl d Ar dst +.Op Fl s Ar src +.Op Fl p Ar proto +.Op Fl v +.Sh DESCRIPTION +.Nm +is used to manipulate or display the characteristics of the +.Xr gre 4 +tunnel device. The first form shown above displays the current +parameters, while the second one sets parameters to new values. +.Pp +The options have the following meanings: +.Bl -tag -width flag +.It Fl i Ar unit +Denotes the interface that is to be set/displayed. Unit is in the form +``greN'' whrere N is an interger from zero to the maximum numbers of +units. This option is always required. +.It Fl d Ar dst +Sets the IP address of the remote tunnel end. This may be the same as the +tunnel destination set with +.Xr ifconfig +when IP packets are to be encapsulated. +.It Fl s Ar src +Sets the IP address of the local tunnel end. This may be the same as +the tunnel gateway set with +.Xr ifconfig +when IP packets are to be encapsulated. +.It Fl p Ar proto +Sets the way how packets are encapsulated. Currently supported values are: +.Bl -tag -width aaa +.It 4 IPIP encapsulation as described in RFC 2003 +.It 47 GRE encapsulation as described in RFC 1701,1702 +.El +.It Fl v +Give a little more verbose output when running +.Nm . +.El +.Pp +Note that the values for +.Ar src , +.Ar dst , +.Ar proto +must be coordinated with the remote end of the tunnel. +.Pp +For tunnels that run IP as encapsulated protocol, the tunnel endpoints +can be set with the normal +.Xr ifconfig . +In this case, the encapsulation protocol can be choosen by giving +``link0'' for IPIP encapsulation and ``-link0'' GRE encapsulation +as arguments to +.Xr ifconfig . +.Sh SEE ALSO +.Xr ifconfig 8 , +.Xr gre 4 , +.Xr protocols 5 , +.Xr netintro 4 +.Pp +A description of GRE encapsulation can be found in RFC 1701, RFC 1702. +.Pp +A description of IPIP encapsulation can be found in RFC 2003. + +.Sh AUTHOR +Heiko W.Rupp diff --git a/usr.sbin/greconfig/greconfig.c b/usr.sbin/greconfig/greconfig.c new file mode 100644 index 000000000000..1b095496edb3 --- /dev/null +++ b/usr.sbin/greconfig/greconfig.c @@ -0,0 +1,198 @@ +/* + * (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Heiko W.Rupp + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * greconfig - frontend to set/query tunnel endpoints + * + * $NetBSD: greconfig.c,v 1.1 1998/09/13 20:50:55 hwr Exp $ + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +/* from sys/sockio.h */ +#define GRESADDRS _IOW('i', 101, struct ifreq) +#define GRESADDRD _IOW('i', 102, struct ifreq) +#define GREGADDRS _IOWR('i', 103, struct ifreq) +#define GREGADDRD _IOWR('i', 104, struct ifreq) +#define GRESPROTO _IOW('i' , 105, struct ifreq) +#define GREGPROTO _IOWR('i', 106, struct ifreq) + + +void usage(void); +void name2sa(char *name,struct sockaddr **sa); +char* sa2name(struct sockaddr *sa); + +int verbose; + +void +main(int argc, char **argv) +{ + int i,s,err; + char *dst,*src,*inf; + struct ifreq ifr; + struct sockaddr *sa; + int pflag=0; + u_char proto = 47; + + dst=src=inf=NULL; + verbose=0; + + while ((i = getopt(argc, argv, "d:i:p:s:v")) != -1) + switch(i) { + case 'd': dst=optarg; break; + case 'i': inf=optarg; break; + case 'p': proto=atoi(optarg); pflag++; break; + case 's': src=optarg; break; + case 'v': verbose++; break; + default: usage(); exit(1); + } + + if (inf==NULL) { + usage(); + exit(2); + } + + if (strncmp("gre",inf,3)!=0) { + usage(); + exit(3); + } + + if((proto!=4) && (proto!=47)) { + usage(); + exit(4); + } + + s=socket(PF_INET,SOCK_DGRAM,0); + if(s<0) { + perror("Socket() failed: \n"); + exit(5); + } + if (pflag) { /* IPPROTO_GRE is default in kernel */ + strncpy(ifr.ifr_name,inf,sizeof(ifr.ifr_name)); + ifr.ifr_flags = proto; + if (verbose) + printf("Setting tunnel protocol to proto %d\n",proto); + err=ioctl(s,GRESPROTO,(caddr_t)&ifr); + if(err<0) + perror("GRESPROTO"); + } + if (src!=NULL) { + strncpy(ifr.ifr_name,inf,sizeof(ifr.ifr_name)); + name2sa(src,&sa); + ifr.ifr_addr=*sa; + if(verbose) + printf("Setting source address ...\n"); + err=ioctl(s,GRESADDRS,(caddr_t)&ifr); + if(err<0) + perror("GRESADDRS"); + } + if (dst!=NULL) { + strncpy(ifr.ifr_name,inf,sizeof(ifr.ifr_name)); + name2sa(dst,&sa); + ifr.ifr_addr=*sa; + if(verbose) + printf("Setting destination address ...\n"); + err=ioctl(s,GRESADDRD,(caddr_t)&ifr); + if(err<0) + perror("GRESADDRD"); + } + if (src==NULL && dst==NULL && !pflag) { + strncpy(ifr.ifr_name,inf,sizeof(ifr.ifr_name)); + err=ioctl(s,GREGADDRS,(caddr_t)&ifr); + if(err<0) + perror("GREGADDRS"); + else + printf("%s -> ",sa2name(&ifr.ifr_addr)); + strncpy(ifr.ifr_name,inf,sizeof(ifr.ifr_name)); + err=ioctl(s,GREGADDRD,(caddr_t)&ifr); + if(err<0) + perror("GREGADDRD"); + else + printf("%s, ",sa2name(&ifr.ifr_addr)); + err=ioctl(s,GREGPROTO,(caddr_t)&ifr); + if(err<0) + perror("GREGPROTO"); + else + printf("running IP-Proto %d\n",ifr.ifr_flags); + } + close(s); +} + +void +usage(void) +{ + printf("greconfig -i unit [-d dst] [-s src] [-p proto] [-v]\n"); + printf("unit is gre, proto either 4 or 47\n"); +} + +void name2sa(char *name,struct sockaddr **sa) +{ + struct hostent *hp; + struct sockaddr_in *si; + struct sockaddr_in s; + + hp=gethostbyname(name); + + bzero(&s,sizeof(struct sockaddr_in)); + s.sin_family=hp->h_addrtype; + if(hp->h_addrtype != AF_INET) { + printf("Only internet addresses allowed, not %s\n",name); + exit(5); + } + bcopy(hp->h_addr,&s.sin_addr,hp->h_length); + si=&s; + + *sa=(struct sockaddr *)si; +} + +char* sa2name(struct sockaddr *sa) +{ + struct sockaddr_in *si; + + si=((struct sockaddr_in *)(sa)); + return(inet_ntoa(si->sin_addr)); +}