Home | History | Annotate | Line # | Download | only in ifconfig
tunnel.c revision 1.12
      1 /*	$NetBSD: tunnel.c,v 1.12 2008/05/07 20:03:27 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *      The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: tunnel.c,v 1.12 2008/05/07 20:03:27 dyoung Exp $");
     35 #endif /* not lint */
     36 
     37 #include <sys/param.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/socket.h>
     40 
     41 #include <net/if.h>
     42 
     43 #ifdef INET6
     44 #include <netinet/in.h>
     45 #endif
     46 
     47 #include <ctype.h>
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <netdb.h>
     51 #include <string.h>
     52 #include <stdlib.h>
     53 #include <stdio.h>
     54 #include <util.h>
     55 
     56 #include "parse.h"
     57 #include "env.h"
     58 #include "util.h"
     59 #include "tunnel.h"
     60 
     61 #ifdef INET6
     62 #include "af_inet6.h"
     63 #endif
     64 
     65 struct paddr tundst = PADDR_INITIALIZER(&tundst, "tundst", settunnel,
     66     "tundst", NULL, NULL, NULL, &command_root.pb_parser);
     67 
     68 struct paddr tunsrc = PADDR_INITIALIZER(&tunsrc, "tunsrc", NULL,
     69     "tunsrc", NULL, NULL, NULL, &tundst.pa_parser);
     70 
     71 static const struct kwinst tunnelkw[] = {
     72 	  {.k_word = "deletetunnel", .k_exec = deletetunnel,
     73 	   .k_nextparser = &command_root.pb_parser}
     74 	, {.k_word = "tunnel", .k_nextparser = &tunsrc.pa_parser}
     75 };
     76 
     77 struct pkw tunnel = PKW_INITIALIZER(&tunnel, "tunnel", NULL, NULL,
     78     tunnelkw, __arraycount(tunnelkw), NULL);
     79 
     80 int
     81 settunnel(prop_dictionary_t env, prop_dictionary_t xenv)
     82 {
     83 	const struct paddr_prefix *srcpfx, *dstpfx;
     84 	struct if_laddrreq req;
     85 	int s;
     86 	prop_data_t srcdata, dstdata;
     87 	const char *ifname;
     88 
     89 	if ((s = getsock(AF_UNSPEC)) == -1)
     90 		err(EXIT_FAILURE, "%s: getsock", __func__);
     91 
     92 	if ((ifname = getifname(env)) == NULL)
     93 		return -1;
     94 
     95 	srcdata = (prop_data_t)prop_dictionary_get(env, "tunsrc");
     96 	dstdata = (prop_data_t)prop_dictionary_get(env, "tundst");
     97 
     98 	if (srcdata == NULL || dstdata == NULL) {
     99 		warnx("%s.%d", __func__, __LINE__);
    100 		errno = ENOENT;
    101 		return -1;
    102 	}
    103 
    104 	srcpfx = prop_data_data_nocopy(srcdata);
    105 	dstpfx = prop_data_data_nocopy(dstdata);
    106 
    107 	if (srcpfx->pfx_addr.sa_family != dstpfx->pfx_addr.sa_family)
    108 		errx(EXIT_FAILURE,
    109 		    "source and destination address families do not match");
    110 
    111 	memset(&req, 0, sizeof(req));
    112 	estrlcpy(req.iflr_name, ifname, sizeof(req.iflr_name));
    113 	memcpy(&req.addr, &srcpfx->pfx_addr,
    114 	    MIN(sizeof(req.addr), srcpfx->pfx_addr.sa_len));
    115 	memcpy(&req.dstaddr, &dstpfx->pfx_addr,
    116 	    MIN(sizeof(req.dstaddr), dstpfx->pfx_addr.sa_len));
    117 
    118 #ifdef INET6
    119 	if (req.addr.ss_family == AF_INET6) {
    120 		struct sockaddr_in6 *s6, *d;
    121 
    122 		s6 = (struct sockaddr_in6 *)&req.addr;
    123 		d = (struct sockaddr_in6 *)&req.dstaddr;
    124 		if (s6->sin6_scope_id != d->sin6_scope_id) {
    125 			errx(EXIT_FAILURE, "scope mismatch");
    126 			/* NOTREACHED */
    127 		}
    128 		/* embed scopeid */
    129 		if (s6->sin6_scope_id &&
    130 		    (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
    131 		     IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
    132 			*(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
    133 			    htons(s6->sin6_scope_id);
    134 		}
    135 		if (d->sin6_scope_id &&
    136 		    (IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
    137 		     IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
    138 			*(u_int16_t *)&d->sin6_addr.s6_addr[2] =
    139 			    htons(d->sin6_scope_id);
    140 		}
    141 	}
    142 #endif /* INET6 */
    143 
    144 	if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
    145 		warn("SIOCSLIFPHYADDR");
    146 	return 0;
    147 }
    148 
    149 int
    150 deletetunnel(prop_dictionary_t env, prop_dictionary_t xenv)
    151 {
    152 	int s;
    153 	struct ifreq ifr;
    154 	const char *ifname;
    155 
    156 	if ((s = getsock(AF_UNSPEC)) == -1)
    157 		err(EXIT_FAILURE, "%s: getsock", __func__);
    158 
    159 	if ((ifname = getifname(env)) == NULL)
    160 		return -1;
    161 
    162 	memset(&ifr, 0, sizeof(ifr));
    163 
    164 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    165 	if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
    166 		err(EXIT_FAILURE, "SIOCDIFPHYADDR");
    167 	return 0;
    168 }
    169 
    170 void
    171 tunnel_status(prop_dictionary_t env, prop_dictionary_t oenv)
    172 {
    173 	char dstserv[sizeof(",65535")];
    174 	char srcserv[sizeof(",65535")];
    175 	char psrcaddr[NI_MAXHOST];
    176 	char pdstaddr[NI_MAXHOST];
    177 	const int niflag = NI_NUMERICHOST|NI_NUMERICSERV;
    178 	struct if_laddrreq req;
    179 	const struct afswtch *lafp;
    180 	int s;
    181 	const char *ifname;
    182 
    183 	if ((s = getsock(AF_UNSPEC)) == -1)
    184 		err(EXIT_FAILURE, "%s: getsock", __func__);
    185 
    186 	psrcaddr[0] = pdstaddr[0] = '\0';
    187 
    188 	if ((ifname = getifname(env)) == NULL)
    189 		err(EXIT_FAILURE, "%s: getifname", __func__);
    190 
    191 	memset(&req, 0, sizeof(req));
    192 	estrlcpy(req.iflr_name, ifname, IFNAMSIZ);
    193 	if (ioctl(s, SIOCGLIFPHYADDR, &req) == -1)
    194 		return;
    195 	lafp = lookup_af_bynum(req.addr.ss_family);
    196 #ifdef INET6
    197 	if (req.addr.ss_family == AF_INET6)
    198 		in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
    199 #endif /* INET6 */
    200 	getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
    201 	    psrcaddr, sizeof(psrcaddr), &srcserv[1], sizeof(srcserv) - 1,
    202 	    niflag);
    203 
    204 #ifdef INET6
    205 	if (req.dstaddr.ss_family == AF_INET6)
    206 		in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
    207 #endif
    208 	getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
    209 	    pdstaddr, sizeof(pdstaddr), &dstserv[1], sizeof(dstserv) - 1,
    210 	    niflag);
    211 
    212 	srcserv[0] = (strcmp(&srcserv[1], "0") == 0) ? '\0' : ',';
    213 	dstserv[0] = (strcmp(&dstserv[1], "0") == 0) ? '\0' : ',';
    214 
    215 	printf("\ttunnel %s %s%s --> %s%s\n", lafp ? lafp->af_name : "???",
    216 	    psrcaddr, srcserv, pdstaddr, dstserv);
    217 }
    218