1998-09-14 00:50:55 +04:00
|
|
|
/*
|
|
|
|
* (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Heiko W.Rupp <hwr@pilhuhn.de>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
1999-06-28 11:37:13 +04:00
|
|
|
* $NetBSD: greconfig.c,v 1.5 1999/06/28 07:37:13 explorer Exp $
|
1998-09-14 00:50:55 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1999-06-28 11:37:13 +04:00
|
|
|
#include <err.h>
|
1998-09-14 00:50:55 +04:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sockio.h>
|
1999-06-28 11:37:13 +04:00
|
|
|
|
1998-09-14 00:50:55 +04:00
|
|
|
#include <netdb.h>
|
1999-06-28 11:37:13 +04:00
|
|
|
|
1998-09-14 00:50:55 +04:00
|
|
|
#include <net/if.h>
|
1999-06-28 11:37:13 +04:00
|
|
|
|
1998-09-14 00:50:55 +04:00
|
|
|
#include <netinet/in.h>
|
1999-06-28 11:37:13 +04:00
|
|
|
|
1998-09-14 00:50:55 +04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
/* 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);
|
1999-06-28 11:37:13 +04:00
|
|
|
char *sa2name(struct sockaddr *sa);
|
1998-09-14 00:50:55 +04:00
|
|
|
|
|
|
|
int verbose;
|
|
|
|
|
1998-09-17 11:41:11 +04:00
|
|
|
int
|
1998-09-14 00:50:55 +04:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
1999-06-28 11:37:13 +04:00
|
|
|
int i, s, err;
|
|
|
|
char *dst, *src, *inf;
|
1998-09-14 00:50:55 +04:00
|
|
|
struct ifreq ifr;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int pflag=0;
|
|
|
|
u_char proto = 47;
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
dst = src = inf = NULL;
|
|
|
|
verbose = 0;
|
1998-09-14 00:50:55 +04:00
|
|
|
|
|
|
|
while ((i = getopt(argc, argv, "d:i:p:s:v")) != -1)
|
|
|
|
switch(i) {
|
1999-06-28 11:37:13 +04:00
|
|
|
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);
|
1998-09-14 00:50:55 +04:00
|
|
|
}
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
if (inf == NULL) {
|
1998-09-14 00:50:55 +04:00
|
|
|
usage();
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
if (strncmp("gre", inf, 3) != 0) {
|
1998-09-14 00:50:55 +04:00
|
|
|
usage();
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
if ((proto != 47) && (proto != 55)) {
|
1998-09-14 00:50:55 +04:00
|
|
|
usage();
|
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("Socket() failed: \n");
|
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
if (pflag) { /* IPPROTO_GRE is default in kernel */
|
1999-06-28 11:37:13 +04:00
|
|
|
strncpy(ifr.ifr_name, inf, sizeof(ifr.ifr_name));
|
1998-09-14 00:50:55 +04:00
|
|
|
ifr.ifr_flags = proto;
|
|
|
|
if (verbose)
|
1999-06-28 11:37:13 +04:00
|
|
|
printf("Setting tunnel protocol to proto %d\n", proto);
|
|
|
|
err = ioctl(s, GRESPROTO, (caddr_t)&ifr);
|
|
|
|
if (err < 0)
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("GRESPROTO");
|
|
|
|
}
|
1999-06-28 11:37:13 +04:00
|
|
|
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 to %s...\n",
|
|
|
|
sa2name(sa));
|
|
|
|
err = ioctl(s, GRESADDRS, (caddr_t)&ifr);
|
|
|
|
if (err < 0)
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("GRESADDRS");
|
|
|
|
}
|
1999-06-28 11:37:13 +04:00
|
|
|
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 to %s...\n",
|
|
|
|
sa2name(sa));
|
|
|
|
err = ioctl(s, GRESADDRD, (caddr_t)&ifr);
|
|
|
|
if (err < 0)
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("GRESADDRD");
|
|
|
|
}
|
1999-06-28 11:37:13 +04:00
|
|
|
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)
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("GREGADDRS");
|
|
|
|
else
|
1999-06-28 11:37:13 +04:00
|
|
|
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)
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("GREGADDRD");
|
|
|
|
else
|
1999-06-28 11:37:13 +04:00
|
|
|
printf("%s, ", sa2name(&ifr.ifr_addr));
|
|
|
|
err = ioctl(s, GREGPROTO, (caddr_t)&ifr);
|
|
|
|
if (err < 0)
|
1998-09-14 00:50:55 +04:00
|
|
|
perror("GREGPROTO");
|
|
|
|
else
|
1999-06-28 11:37:13 +04:00
|
|
|
printf("running IP-Proto %d\n", ifr.ifr_flags);
|
1998-09-14 00:50:55 +04:00
|
|
|
}
|
|
|
|
close(s);
|
1998-09-17 11:41:11 +04:00
|
|
|
|
|
|
|
exit(0);
|
1998-09-14 00:50:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
1999-06-28 11:37:13 +04:00
|
|
|
fprintf(stderr,
|
|
|
|
"greconfig -i unit [-d dst] [-s src] [-p proto] [-v]\n");
|
|
|
|
fprintf(stderr, "unit is gre<n>, proto either 47 or 55\n");
|
1998-09-14 00:50:55 +04:00
|
|
|
}
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
void
|
|
|
|
name2sa(char *name, struct sockaddr **sa)
|
1998-09-14 00:50:55 +04:00
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
struct sockaddr_in *si;
|
1999-06-28 11:37:13 +04:00
|
|
|
static struct sockaddr_in s;
|
1998-09-14 00:50:55 +04:00
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
hp = gethostbyname(name);
|
1998-09-14 00:50:55 +04:00
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
bzero(&s, sizeof(struct sockaddr_in));
|
|
|
|
s.sin_family = hp->h_addrtype;
|
|
|
|
if (hp->h_addrtype != AF_INET)
|
|
|
|
errx(5, "Only internet addresses allowed, not %s\n", name);
|
|
|
|
bcopy(hp->h_addr, &s.sin_addr, hp->h_length);
|
|
|
|
si = &s;
|
1998-09-14 00:50:55 +04:00
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
*sa = (struct sockaddr *)si;
|
1998-09-14 00:50:55 +04:00
|
|
|
}
|
1999-06-28 11:37:13 +04:00
|
|
|
|
|
|
|
char *
|
|
|
|
sa2name(struct sockaddr *sa)
|
1998-09-14 00:50:55 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_in *si;
|
|
|
|
|
1999-06-28 11:37:13 +04:00
|
|
|
si = ((struct sockaddr_in *)(sa));
|
|
|
|
|
|
|
|
return (inet_ntoa(si->sin_addr));
|
1998-09-14 00:50:55 +04:00
|
|
|
}
|