tunnel.c revision 1.17 1 1.17 dyoung /* $NetBSD: tunnel.c,v 1.17 2009/08/07 18:53:37 dyoung 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.17 dyoung __RCSID("$NetBSD: tunnel.c,v 1.17 2009/08/07 18:53:37 dyoung 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.10 dyoung #include <errno.h>
50 1.1 thorpej #include <netdb.h>
51 1.1 thorpej #include <string.h>
52 1.1 thorpej #include <stdlib.h>
53 1.1 thorpej #include <stdio.h>
54 1.7 christos #include <util.h>
55 1.1 thorpej
56 1.14 dyoung #include "env.h"
57 1.14 dyoung #include "extern.h"
58 1.10 dyoung #include "parse.h"
59 1.10 dyoung #include "util.h"
60 1.1 thorpej
61 1.14 dyoung static status_func_t status;
62 1.15 dyoung static usage_func_t usage;
63 1.14 dyoung static cmdloop_branch_t branch;
64 1.14 dyoung
65 1.14 dyoung static void tunnel_constructor(void) __attribute__((constructor));
66 1.14 dyoung static int settunnel(prop_dictionary_t, prop_dictionary_t);
67 1.14 dyoung static int deletetunnel(prop_dictionary_t, prop_dictionary_t);
68 1.14 dyoung static void tunnel_status(prop_dictionary_t, prop_dictionary_t);
69 1.1 thorpej
70 1.10 dyoung struct paddr tundst = PADDR_INITIALIZER(&tundst, "tundst", settunnel,
71 1.10 dyoung "tundst", NULL, NULL, NULL, &command_root.pb_parser);
72 1.10 dyoung
73 1.10 dyoung struct paddr tunsrc = PADDR_INITIALIZER(&tunsrc, "tunsrc", NULL,
74 1.10 dyoung "tunsrc", NULL, NULL, NULL, &tundst.pa_parser);
75 1.10 dyoung
76 1.12 dyoung static const struct kwinst tunnelkw[] = {
77 1.12 dyoung {.k_word = "deletetunnel", .k_exec = deletetunnel,
78 1.12 dyoung .k_nextparser = &command_root.pb_parser}
79 1.12 dyoung , {.k_word = "tunnel", .k_nextparser = &tunsrc.pa_parser}
80 1.12 dyoung };
81 1.12 dyoung
82 1.12 dyoung struct pkw tunnel = PKW_INITIALIZER(&tunnel, "tunnel", NULL, NULL,
83 1.12 dyoung tunnelkw, __arraycount(tunnelkw), NULL);
84 1.12 dyoung
85 1.14 dyoung static int
86 1.16 dyoung settunnel(prop_dictionary_t env, prop_dictionary_t oenv)
87 1.1 thorpej {
88 1.10 dyoung const struct paddr_prefix *srcpfx, *dstpfx;
89 1.1 thorpej struct if_laddrreq req;
90 1.10 dyoung prop_data_t srcdata, dstdata;
91 1.10 dyoung
92 1.10 dyoung srcdata = (prop_data_t)prop_dictionary_get(env, "tunsrc");
93 1.10 dyoung dstdata = (prop_data_t)prop_dictionary_get(env, "tundst");
94 1.10 dyoung
95 1.10 dyoung if (srcdata == NULL || dstdata == NULL) {
96 1.10 dyoung warnx("%s.%d", __func__, __LINE__);
97 1.10 dyoung errno = ENOENT;
98 1.10 dyoung return -1;
99 1.10 dyoung }
100 1.1 thorpej
101 1.10 dyoung srcpfx = prop_data_data_nocopy(srcdata);
102 1.10 dyoung dstpfx = prop_data_data_nocopy(dstdata);
103 1.1 thorpej
104 1.10 dyoung if (srcpfx->pfx_addr.sa_family != dstpfx->pfx_addr.sa_family)
105 1.1 thorpej errx(EXIT_FAILURE,
106 1.1 thorpej "source and destination address families do not match");
107 1.1 thorpej
108 1.1 thorpej memset(&req, 0, sizeof(req));
109 1.10 dyoung memcpy(&req.addr, &srcpfx->pfx_addr,
110 1.10 dyoung MIN(sizeof(req.addr), srcpfx->pfx_addr.sa_len));
111 1.10 dyoung memcpy(&req.dstaddr, &dstpfx->pfx_addr,
112 1.10 dyoung MIN(sizeof(req.dstaddr), dstpfx->pfx_addr.sa_len));
113 1.1 thorpej
114 1.1 thorpej #ifdef INET6
115 1.1 thorpej if (req.addr.ss_family == AF_INET6) {
116 1.1 thorpej struct sockaddr_in6 *s6, *d;
117 1.1 thorpej
118 1.1 thorpej s6 = (struct sockaddr_in6 *)&req.addr;
119 1.1 thorpej d = (struct sockaddr_in6 *)&req.dstaddr;
120 1.1 thorpej if (s6->sin6_scope_id != d->sin6_scope_id) {
121 1.1 thorpej errx(EXIT_FAILURE, "scope mismatch");
122 1.1 thorpej /* NOTREACHED */
123 1.1 thorpej }
124 1.14 dyoung if (IN6_IS_ADDR_MULTICAST(&d->sin6_addr) ||
125 1.14 dyoung IN6_IS_ADDR_MULTICAST(&s6->sin6_addr))
126 1.14 dyoung errx(EXIT_FAILURE, "tunnel src/dst is multicast");
127 1.1 thorpej /* embed scopeid */
128 1.1 thorpej if (s6->sin6_scope_id &&
129 1.14 dyoung IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
130 1.1 thorpej *(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
131 1.1 thorpej htons(s6->sin6_scope_id);
132 1.1 thorpej }
133 1.14 dyoung if (d->sin6_scope_id && IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr)) {
134 1.1 thorpej *(u_int16_t *)&d->sin6_addr.s6_addr[2] =
135 1.1 thorpej htons(d->sin6_scope_id);
136 1.1 thorpej }
137 1.1 thorpej }
138 1.1 thorpej #endif /* INET6 */
139 1.1 thorpej
140 1.13 dyoung if (direct_ioctl(env, SIOCSLIFPHYADDR, &req) == -1)
141 1.1 thorpej warn("SIOCSLIFPHYADDR");
142 1.10 dyoung return 0;
143 1.1 thorpej }
144 1.1 thorpej
145 1.14 dyoung static int
146 1.16 dyoung deletetunnel(prop_dictionary_t env, prop_dictionary_t oenv)
147 1.1 thorpej {
148 1.13 dyoung if (indirect_ioctl(env, SIOCDIFPHYADDR, NULL) == -1)
149 1.1 thorpej err(EXIT_FAILURE, "SIOCDIFPHYADDR");
150 1.10 dyoung return 0;
151 1.1 thorpej }
152 1.1 thorpej
153 1.14 dyoung static void
154 1.11 dyoung tunnel_status(prop_dictionary_t env, prop_dictionary_t oenv)
155 1.1 thorpej {
156 1.8 dyoung char dstserv[sizeof(",65535")];
157 1.8 dyoung char srcserv[sizeof(",65535")];
158 1.1 thorpej char psrcaddr[NI_MAXHOST];
159 1.1 thorpej char pdstaddr[NI_MAXHOST];
160 1.17 dyoung const int niflag = Nflag ? 0 : (NI_NUMERICHOST|NI_NUMERICSERV);
161 1.1 thorpej struct if_laddrreq req;
162 1.14 dyoung const struct afswtch *afp;
163 1.1 thorpej
164 1.1 thorpej psrcaddr[0] = pdstaddr[0] = '\0';
165 1.1 thorpej
166 1.1 thorpej memset(&req, 0, sizeof(req));
167 1.13 dyoung if (direct_ioctl(env, SIOCGLIFPHYADDR, &req) == -1)
168 1.1 thorpej return;
169 1.14 dyoung afp = lookup_af_bynum(req.addr.ss_family);
170 1.1 thorpej #ifdef INET6
171 1.3 thorpej if (req.addr.ss_family == AF_INET6)
172 1.1 thorpej in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
173 1.1 thorpej #endif /* INET6 */
174 1.1 thorpej getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
175 1.8 dyoung psrcaddr, sizeof(psrcaddr), &srcserv[1], sizeof(srcserv) - 1,
176 1.8 dyoung niflag);
177 1.1 thorpej
178 1.1 thorpej #ifdef INET6
179 1.1 thorpej if (req.dstaddr.ss_family == AF_INET6)
180 1.1 thorpej in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
181 1.1 thorpej #endif
182 1.1 thorpej getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
183 1.8 dyoung pdstaddr, sizeof(pdstaddr), &dstserv[1], sizeof(dstserv) - 1,
184 1.8 dyoung niflag);
185 1.8 dyoung
186 1.8 dyoung srcserv[0] = (strcmp(&srcserv[1], "0") == 0) ? '\0' : ',';
187 1.8 dyoung dstserv[0] = (strcmp(&dstserv[1], "0") == 0) ? '\0' : ',';
188 1.1 thorpej
189 1.14 dyoung printf("\ttunnel %s %s%s --> %s%s\n", afp ? afp->af_name : "???",
190 1.8 dyoung psrcaddr, srcserv, pdstaddr, dstserv);
191 1.1 thorpej }
192 1.14 dyoung
193 1.14 dyoung static void
194 1.15 dyoung tunnel_usage(prop_dictionary_t env)
195 1.15 dyoung {
196 1.15 dyoung fprintf(stderr,
197 1.15 dyoung "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n");
198 1.15 dyoung }
199 1.15 dyoung
200 1.15 dyoung static void
201 1.14 dyoung tunnel_constructor(void)
202 1.14 dyoung {
203 1.14 dyoung cmdloop_branch_init(&branch, &tunnel.pk_parser);
204 1.14 dyoung register_cmdloop_branch(&branch);
205 1.14 dyoung status_func_init(&status, tunnel_status);
206 1.15 dyoung usage_func_init(&usage, tunnel_usage);
207 1.14 dyoung register_status(&status);
208 1.15 dyoung register_usage(&usage);
209 1.14 dyoung }
210