tunnel.c revision 1.3 1 /* $NetBSD: tunnel.c,v 1.3 2005/03/19 22:57:06 thorpej 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.3 2005/03/19 22:57:06 thorpej 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 <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53
54 #include "extern.h"
55 #include "tunnel.h"
56
57 #ifdef INET6
58 extern void in6_fillscopeid(struct sockaddr_in6 *sin6); /* XXX */
59 #endif
60
61 void
62 settunnel(const char *src, const char *dst)
63 {
64 struct addrinfo hints, *srcres, *dstres;
65 int ecode;
66 struct if_laddrreq req;
67
68 memset(&hints, 0, sizeof(hints));
69 hints.ai_family = afp->af_af;
70 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
71
72 if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
73 errx(EXIT_FAILURE, "error in parsing address string: %s",
74 gai_strerror(ecode));
75
76 if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
77 errx(EXIT_FAILURE, "error in parsing address string: %s",
78 gai_strerror(ecode));
79
80 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
81 errx(EXIT_FAILURE,
82 "source and destination address families do not match");
83
84 if (srcres->ai_addrlen > sizeof(req.addr) ||
85 dstres->ai_addrlen > sizeof(req.dstaddr))
86 errx(EXIT_FAILURE, "invalid sockaddr");
87
88 memset(&req, 0, sizeof(req));
89 strncpy(req.iflr_name, name, sizeof(req.iflr_name));
90 memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
91 memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
92
93 #ifdef INET6
94 if (req.addr.ss_family == AF_INET6) {
95 struct sockaddr_in6 *s6, *d;
96
97 s6 = (struct sockaddr_in6 *)&req.addr;
98 d = (struct sockaddr_in6 *)&req.dstaddr;
99 if (s6->sin6_scope_id != d->sin6_scope_id) {
100 errx(EXIT_FAILURE, "scope mismatch");
101 /* NOTREACHED */
102 }
103 #ifdef __KAME__
104 /* embed scopeid */
105 if (s6->sin6_scope_id &&
106 (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
107 IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
108 *(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
109 htons(s6->sin6_scope_id);
110 }
111 if (d->sin6_scope_id &&
112 (IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
113 IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
114 *(u_int16_t *)&d->sin6_addr.s6_addr[2] =
115 htons(d->sin6_scope_id);
116 }
117 #endif /* __KAME__ */
118 }
119 #endif /* INET6 */
120
121 if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
122 warn("SIOCSLIFPHYADDR");
123
124 freeaddrinfo(srcres);
125 freeaddrinfo(dstres);
126 }
127
128 /*ARGSUSED*/
129 void
130 deletetunnel(const char *ifname, int param)
131 {
132
133 if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
134 err(EXIT_FAILURE, "SIOCDIFPHYADDR");
135 }
136
137 void
138 tunnel_status(void)
139 {
140 char psrcaddr[NI_MAXHOST];
141 char pdstaddr[NI_MAXHOST];
142 const int niflag = NI_NUMERICHOST;
143 struct if_laddrreq req;
144 struct afswtch *lafp;
145
146 psrcaddr[0] = pdstaddr[0] = '\0';
147
148 memset(&req, 0, sizeof(req));
149 strncpy(req.iflr_name, name, IFNAMSIZ);
150 if (ioctl(s, SIOCGLIFPHYADDR, &req) == -1)
151 return;
152 lafp = lookup_af_bynum(req.addr.ss_family);
153 #ifdef INET6
154 if (req.addr.ss_family == AF_INET6)
155 in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
156 #endif /* INET6 */
157 getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
158 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
159
160 #ifdef INET6
161 if (req.dstaddr.ss_family == AF_INET6)
162 in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
163 #endif
164 getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
165 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
166
167 printf("\ttunnel %s %s --> %s\n", lafp ? lafp->af_name : "???",
168 psrcaddr, pdstaddr);
169 }
170