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