Home | History | Annotate | Line # | Download | only in ifconfig
tunnel.c revision 1.7
      1  1.7  christos /*	$NetBSD: tunnel.c,v 1.7 2006/08/26 18:14:28 christos Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*
      4  1.1   thorpej  * Copyright (c) 1983, 1993
      5  1.1   thorpej  *      The Regents of the University of California.  All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1   thorpej  * modification, are permitted provided that the following conditions
      9  1.1   thorpej  * are met:
     10  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     16  1.1   thorpej  *    may be used to endorse or promote products derived from this software
     17  1.1   thorpej  *    without specific prior written permission.
     18  1.1   thorpej  *
     19  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1   thorpej  * SUCH DAMAGE.
     30  1.1   thorpej  */
     31  1.1   thorpej 
     32  1.1   thorpej #include <sys/cdefs.h>
     33  1.1   thorpej #ifndef lint
     34  1.7  christos __RCSID("$NetBSD: tunnel.c,v 1.7 2006/08/26 18:14:28 christos Exp $");
     35  1.1   thorpej #endif /* not lint */
     36  1.1   thorpej 
     37  1.1   thorpej #include <sys/param.h>
     38  1.1   thorpej #include <sys/ioctl.h>
     39  1.1   thorpej #include <sys/socket.h>
     40  1.1   thorpej 
     41  1.1   thorpej #include <net/if.h>
     42  1.1   thorpej 
     43  1.1   thorpej #ifdef INET6
     44  1.1   thorpej #include <netinet/in.h>
     45  1.1   thorpej #endif
     46  1.1   thorpej 
     47  1.1   thorpej #include <ctype.h>
     48  1.1   thorpej #include <err.h>
     49  1.1   thorpej #include <netdb.h>
     50  1.1   thorpej #include <string.h>
     51  1.1   thorpej #include <stdlib.h>
     52  1.1   thorpej #include <stdio.h>
     53  1.7  christos #include <util.h>
     54  1.1   thorpej 
     55  1.1   thorpej #include "extern.h"
     56  1.1   thorpej #include "tunnel.h"
     57  1.1   thorpej 
     58  1.2      yamt #ifdef INET6
     59  1.5   thorpej #include "af_inet6.h"
     60  1.2      yamt #endif
     61  1.1   thorpej 
     62  1.1   thorpej void
     63  1.1   thorpej settunnel(const char *src, const char *dst)
     64  1.1   thorpej {
     65  1.1   thorpej 	struct addrinfo hints, *srcres, *dstres;
     66  1.1   thorpej 	int ecode;
     67  1.1   thorpej 	struct if_laddrreq req;
     68  1.1   thorpej 
     69  1.1   thorpej 	memset(&hints, 0, sizeof(hints));
     70  1.1   thorpej 	hints.ai_family = afp->af_af;
     71  1.1   thorpej 	hints.ai_socktype = SOCK_DGRAM; /*dummy*/
     72  1.1   thorpej 
     73  1.1   thorpej 	if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
     74  1.1   thorpej 		errx(EXIT_FAILURE, "error in parsing address string: %s",
     75  1.1   thorpej 		    gai_strerror(ecode));
     76  1.1   thorpej 
     77  1.1   thorpej 	if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
     78  1.1   thorpej 		errx(EXIT_FAILURE, "error in parsing address string: %s",
     79  1.1   thorpej 		    gai_strerror(ecode));
     80  1.1   thorpej 
     81  1.1   thorpej 	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
     82  1.1   thorpej 		errx(EXIT_FAILURE,
     83  1.1   thorpej 		    "source and destination address families do not match");
     84  1.1   thorpej 
     85  1.1   thorpej 	if (srcres->ai_addrlen > sizeof(req.addr) ||
     86  1.1   thorpej 	    dstres->ai_addrlen > sizeof(req.dstaddr))
     87  1.1   thorpej 		errx(EXIT_FAILURE, "invalid sockaddr");
     88  1.1   thorpej 
     89  1.1   thorpej 	memset(&req, 0, sizeof(req));
     90  1.6      elad 	estrlcpy(req.iflr_name, name, sizeof(req.iflr_name));
     91  1.1   thorpej 	memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
     92  1.1   thorpej 	memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
     93  1.1   thorpej 
     94  1.1   thorpej #ifdef INET6
     95  1.1   thorpej 	if (req.addr.ss_family == AF_INET6) {
     96  1.1   thorpej 		struct sockaddr_in6 *s6, *d;
     97  1.1   thorpej 
     98  1.1   thorpej 		s6 = (struct sockaddr_in6 *)&req.addr;
     99  1.1   thorpej 		d = (struct sockaddr_in6 *)&req.dstaddr;
    100  1.1   thorpej 		if (s6->sin6_scope_id != d->sin6_scope_id) {
    101  1.1   thorpej 			errx(EXIT_FAILURE, "scope mismatch");
    102  1.1   thorpej 			/* NOTREACHED */
    103  1.1   thorpej 		}
    104  1.1   thorpej #ifdef __KAME__
    105  1.1   thorpej 		/* embed scopeid */
    106  1.1   thorpej 		if (s6->sin6_scope_id &&
    107  1.1   thorpej 		    (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
    108  1.1   thorpej 		     IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
    109  1.1   thorpej 			*(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
    110  1.1   thorpej 			    htons(s6->sin6_scope_id);
    111  1.1   thorpej 		}
    112  1.1   thorpej 		if (d->sin6_scope_id &&
    113  1.1   thorpej 		    (IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
    114  1.1   thorpej 		     IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
    115  1.1   thorpej 			*(u_int16_t *)&d->sin6_addr.s6_addr[2] =
    116  1.1   thorpej 			    htons(d->sin6_scope_id);
    117  1.1   thorpej 		}
    118  1.1   thorpej #endif /* __KAME__ */
    119  1.1   thorpej 	}
    120  1.1   thorpej #endif /* INET6 */
    121  1.1   thorpej 
    122  1.1   thorpej 	if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
    123  1.1   thorpej 		warn("SIOCSLIFPHYADDR");
    124  1.1   thorpej 
    125  1.1   thorpej 	freeaddrinfo(srcres);
    126  1.1   thorpej 	freeaddrinfo(dstres);
    127  1.1   thorpej }
    128  1.1   thorpej 
    129  1.1   thorpej /*ARGSUSED*/
    130  1.1   thorpej void
    131  1.1   thorpej deletetunnel(const char *ifname, int param)
    132  1.1   thorpej {
    133  1.1   thorpej 
    134  1.1   thorpej 	if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
    135  1.1   thorpej 		err(EXIT_FAILURE, "SIOCDIFPHYADDR");
    136  1.1   thorpej }
    137  1.1   thorpej 
    138  1.1   thorpej void
    139  1.1   thorpej tunnel_status(void)
    140  1.1   thorpej {
    141  1.1   thorpej 	char psrcaddr[NI_MAXHOST];
    142  1.1   thorpej 	char pdstaddr[NI_MAXHOST];
    143  1.1   thorpej 	const int niflag = NI_NUMERICHOST;
    144  1.1   thorpej 	struct if_laddrreq req;
    145  1.4   thorpej 	const struct afswtch *lafp;
    146  1.1   thorpej 
    147  1.1   thorpej 	psrcaddr[0] = pdstaddr[0] = '\0';
    148  1.1   thorpej 
    149  1.1   thorpej 	memset(&req, 0, sizeof(req));
    150  1.6      elad 	estrlcpy(req.iflr_name, name, IFNAMSIZ);
    151  1.1   thorpej 	if (ioctl(s, SIOCGLIFPHYADDR, &req) == -1)
    152  1.1   thorpej 		return;
    153  1.3   thorpej 	lafp = lookup_af_bynum(req.addr.ss_family);
    154  1.1   thorpej #ifdef INET6
    155  1.3   thorpej 	if (req.addr.ss_family == AF_INET6)
    156  1.1   thorpej 		in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
    157  1.1   thorpej #endif /* INET6 */
    158  1.1   thorpej 	getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
    159  1.1   thorpej 	    psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
    160  1.1   thorpej 
    161  1.1   thorpej #ifdef INET6
    162  1.1   thorpej 	if (req.dstaddr.ss_family == AF_INET6)
    163  1.1   thorpej 		in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
    164  1.1   thorpej #endif
    165  1.1   thorpej 	getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
    166  1.1   thorpej 	    pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
    167  1.1   thorpej 
    168  1.3   thorpej 	printf("\ttunnel %s %s --> %s\n", lafp ? lafp->af_name : "???",
    169  1.3   thorpej 	    psrcaddr, pdstaddr);
    170  1.1   thorpej }
    171