summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/6to4relay.c
blob: 34bf605432410618e32c8cb2166fbc2bbf9bca78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/socket.h>
#include <sys/stream.h>
#include <sys/param.h>

#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <locale.h>

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stropts.h>
#include <fcntl.h>
#include <libdliptun.h>

static void usage(void);

static dladm_handle_t	handle;
/* booleans corresponding to command line flags */
static boolean_t	eflag = B_FALSE;
static boolean_t	dflag = B_FALSE;
static boolean_t	aflag = B_FALSE;


/*
 * printkstatus()
 *
 * Queries the kernel for the current 6to4 Relay Router value, prints
 * a status message based on the value and exits this command.
 * INADDR_ANY is used to denote that Relay Router communication support is
 * disabled within the kernel.
 */
static void
printkstatus(void)
{
	struct in_addr	rr_addr;
	char		buf[INET6_ADDRSTRLEN];
	char		errstr[DLADM_STRSIZE];
	dladm_status_t	status;

	status = dladm_iptun_get6to4relay(handle, &rr_addr);
	if (status != DLADM_STATUS_OK) {
		(void) fprintf(stderr, gettext("6to4relay: unable to get "
		    "6to4 relay status: %s\n"),
		    dladm_status2str(status, errstr));
		return;
	}
	(void) printf("6to4relay: ");
	if (rr_addr.s_addr == INADDR_ANY) {
		(void) printf(gettext("6to4 Relay Router communication "
		    "support is disabled.\n"));
	} else {
		(void) printf(gettext("6to4 Relay Router communication "
		    "support is enabled.\n"));
		(void) printf(gettext("IPv4 destination address of Relay "
		    "Router = "));
		(void) printf("%s\n",
		    inet_ntop(AF_INET, &rr_addr, buf, sizeof (buf)));
	}
}

/*
 * modifyroute(cmd, in_gw)
 *
 * Modifies a default IPv6 route with DST = ::, GATEWAY = in_gw, NETMASK = ::
 * and flags = <GATEWAY, STATIC>.
 * This route is to be propagated through the 6to4 site so that 6to4 hosts
 * can send packets to native IPv6 hosts behind a remote 6to4 Relay Router.
 */
static void
modifyroute(unsigned int cmd, in6_addr_t *in_gw)
{
	static int rtmseq;
	int rtsock;
	int rlen;

	static struct {
		struct rt_msghdr	rt_hdr;
		struct sockaddr_in6	rt_dst;
		struct sockaddr_in6	rt_gate;
		struct sockaddr_in6	rt_mask;
	} rt_msg;

	/* Open a routing socket for passing route commands */
	if ((rtsock = socket(AF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
		(void) fprintf(stderr, gettext("6to4relay: unable to modify "
		    "default IPv6 route: socket: %s\n"), strerror(errno));
		return;
	}

	(void) memset(&rt_msg, 0, sizeof (rt_msg));
	rt_msg.rt_hdr.rtm_msglen = sizeof (rt_msg);
	rt_msg.rt_hdr.rtm_version = RTM_VERSION;
	rt_msg.rt_hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
	rt_msg.rt_hdr.rtm_pid = getpid();
	rt_msg.rt_hdr.rtm_type = cmd;
	rt_msg.rt_hdr.rtm_seq = ++rtmseq;
	rt_msg.rt_hdr.rtm_flags = RTF_STATIC | RTF_GATEWAY;

	/* DST */
	rt_msg.rt_dst.sin6_family = AF_INET6;
	(void) memset(&rt_msg.rt_dst.sin6_addr.s6_addr, 0,
	    sizeof (in6_addr_t));

	/* GATEWAY */
	rt_msg.rt_gate.sin6_family = AF_INET6;
	bcopy(in_gw->s6_addr, &rt_msg.rt_gate.sin6_addr.s6_addr,
	    sizeof (in6_addr_t));

	/* NETMASK */
	rt_msg.rt_mask.sin6_family = AF_INET6;
	(void) memset(&rt_msg.rt_mask.sin6_addr.s6_addr, 0,
	    sizeof (in6_addr_t));

	/* Send the routing message */
	rlen = write(rtsock, &rt_msg, rt_msg.rt_hdr.rtm_msglen);
	if (rlen < rt_msg.rt_hdr.rtm_msglen) {
		if (rlen < 0) {
			(void) fprintf(stderr,
			    gettext("6to4relay: write to routing socket: %s\n"),
			    strerror(errno));
		} else {
			(void) fprintf(stderr, gettext("6to4relay: write to "
			    "routing socket got only %d for rlen\n"), rlen);
		}
	}
	(void) close(rtsock);
}

static void
usage(void)
{
	(void) fprintf(stderr,
	    gettext("usage:\n"
	    "\t6to4relay\n"
	    "\t6to4relay -e [-a <addr>]\n"
	    "\t6to4relay -d\n"
	    "\t6to4relay -h\n"));
}

int
main(int argc, char **argv)
{
	int		ch;
	char		*relay_arg = NULL;
	dladm_status_t	status;
	char		errstr[DLADM_STRSIZE];

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if ((status = dladm_open(&handle)) != DLADM_STATUS_OK) {
		(void) fprintf(stderr, gettext("6to4relay: error opening "
		    "dladm handle: %s\n"), dladm_status2str(status, errstr));
		return (EXIT_FAILURE);
	}

	/* If no args are specified, print the current status. */
	if (argc < 2) {
		printkstatus();
		return (EXIT_SUCCESS);
	}

	while ((ch = getopt(argc, argv, "ea:dh")) != EOF) {
		switch (ch) {
		case 'e':
			eflag = B_TRUE;
			break;
		case 'd':
			dflag = B_TRUE;
			break;
		case 'a':
			aflag = B_TRUE;
			relay_arg = optarg;
			break;
		case 'h':
			usage();
			return (EXIT_SUCCESS);
		default:
			usage();
			return (EXIT_FAILURE);
		}
	}
	/*
	 * If -a is specified, -e must also be specified.  Also, the
	 * combination of -e and -d is illegal.  Fail on either case.
	 */
	if ((aflag && !eflag) || (eflag && dflag)) {
		usage();
		return (EXIT_FAILURE);
	}

	/*
	 * Enable Relay Router communication support in the kernel.
	 */
	if (eflag) {
		struct in_addr current_addr;
		struct in_addr new_addr;
		in6_addr_t v6_rt;

		/*
		 * if -a was not specified, the well-known anycast will
		 * be used.
		 */
		if (!aflag) {
			new_addr.s_addr = htonl(INADDR_6TO4RRANYCAST);
		} else if (inet_pton(AF_INET, relay_arg, &new_addr) <= 0) {
			(void) fprintf(stderr, gettext("6to4relay: input "
			    "address (%s) is not a valid IPv4 dotted-decimal "
			    "string.\n"), relay_arg);
			return (EXIT_FAILURE);
		}

		status = dladm_iptun_get6to4relay(handle, &current_addr);
		if (status != DLADM_STATUS_OK) {
			(void) fprintf(stderr, gettext("6to4relay: "
			    "unable to obtain current 6to4 relay address: %s"),
			    dladm_status2str(status, errstr));
			return (EXIT_FAILURE);
		}

		if (current_addr.s_addr == new_addr.s_addr)
			return (EXIT_SUCCESS);

		status = dladm_iptun_set6to4relay(handle, &new_addr);
		if (status != DLADM_STATUS_OK) {
			(void) fprintf(stderr, gettext("6to4relay: "
			    "unable to set the 6to4 relay router address: "
			    "%s\n"), dladm_status2str(status, errstr));
			return (EXIT_FAILURE);
		}

		if (current_addr.s_addr != INADDR_ANY) {
			/* remove old default IPv6 route */
			IN6_V4ADDR_TO_6TO4(&current_addr, &v6_rt);
			modifyroute(RTM_DELETE, &v6_rt);
		}

		IN6_V4ADDR_TO_6TO4(&new_addr, &v6_rt);
		modifyroute(RTM_ADD, &v6_rt);
	}

	/*
	 * Disable Relay Router communication support in kernel.
	 */
	if (dflag) {
		struct in_addr rr_addr;
		in6_addr_t v6_rt;

		/*
		 * get Relay Router address from the kernel and delete
		 * default IPv6 route that was added for it.
		 */
		status = dladm_iptun_get6to4relay(handle, &rr_addr);
		if (status != DLADM_STATUS_OK) {
			(void) fprintf(stderr, gettext("6to4relay: "
			    "unable to obtain current 6to4 relay address: %s"),
			    dladm_status2str(status, errstr));
			return (EXIT_FAILURE);
		}
		if (rr_addr.s_addr == INADDR_ANY)
			return (EXIT_SUCCESS);

		IN6_V4ADDR_TO_6TO4(&rr_addr, &v6_rt);
		modifyroute(RTM_DELETE, &v6_rt);

		/*
		 * INADDR_ANY (0.0.0.0) is used by the kernel to disable Relay
		 * Router communication support.
		 */
		rr_addr.s_addr = INADDR_ANY;
		status = dladm_iptun_set6to4relay(handle, &rr_addr);
		if (status != DLADM_STATUS_OK) {
			(void) fprintf(stderr, gettext("6to4relay: "
			    "unable to disable tunneling to 6to4 relay router: "
			    "%s\n"), dladm_status2str(status, errstr));
			return (EXIT_FAILURE);
		}
	}

	return (EXIT_SUCCESS);
}