summaryrefslogtreecommitdiff
path: root/usr/src/lib/libipadm/common/ipadm_ndpd.c
blob: 29ea8ccf51b07ae59857d9083d6feb6b9206b69e (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * 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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
 */

/*
 * This file contains the functions that are required for communicating
 * with in.ndpd while creating autoconfigured addresses.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/sockio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <inet/ip.h>
#include <arpa/inet.h>
#include <assert.h>
#include <poll.h>
#include <ipadm_ndpd.h>
#include "libipadm_impl.h"

#define	NDPDTIMEOUT		5000
#define	PREFIXLEN_LINKLOCAL	10

static ipadm_status_t	i_ipadm_create_linklocal(ipadm_handle_t,
			    ipadm_addrobj_t);
static void		i_ipadm_make_linklocal(struct sockaddr_in6 *,
			    const struct in6_addr *);
static ipadm_status_t	i_ipadm_send_ndpd_cmd(const char *,
			    const struct ipadm_addrobj_s *, int);

/*
 * Sends message to in.ndpd asking not to do autoconf for the given interface,
 * until IPADM_CREATE_ADDRS or IPADM_ENABLE_AUTOCONF is sent.
 */
ipadm_status_t
i_ipadm_disable_autoconf(const char *ifname)
{
	return (i_ipadm_send_ndpd_cmd(ifname, NULL, IPADM_DISABLE_AUTOCONF));
}

/*
 * Sends message to in.ndpd to enable autoconf for the given interface,
 * until another IPADM_DISABLE_AUTOCONF is sent.
 */
ipadm_status_t
i_ipadm_enable_autoconf(const char *ifname)
{
	return (i_ipadm_send_ndpd_cmd(ifname, NULL, IPADM_ENABLE_AUTOCONF));
}

ipadm_status_t
i_ipadm_create_ipv6addrs(ipadm_handle_t iph, ipadm_addrobj_t addr,
    uint32_t i_flags)
{
	ipadm_status_t status;

	/*
	 * Create the link local based on the given token. If the same intfid
	 * was already used with a different address object, this step will
	 * fail.
	 */
	status = i_ipadm_create_linklocal(iph, addr);
	if (status != IPADM_SUCCESS)
		return (status);

	/*
	 * Request in.ndpd to start the autoconfiguration.
	 * If autoconfiguration was already started by another means (e.g.
	 * "ifconfig" ), in.ndpd will return EEXIST.
	 */
	if (addr->ipadm_stateless || addr->ipadm_stateful) {
		status = i_ipadm_send_ndpd_cmd(addr->ipadm_ifname, addr,
		    IPADM_CREATE_ADDRS);
		if (status != IPADM_SUCCESS &&
		    status != IPADM_NDPD_NOT_RUNNING) {
			(void) i_ipadm_delete_addr(iph, addr);
			return (status);
		}
	}

	/* Persist the intfid. */
	status = i_ipadm_addr_persist(iph, addr, B_FALSE, i_flags, NULL);
	if (status != IPADM_SUCCESS) {
		(void) i_ipadm_delete_addr(iph, addr);
		(void) i_ipadm_send_ndpd_cmd(addr->ipadm_ifname, addr,
		    IPADM_DELETE_ADDRS);
	}

	return (status);
}

ipadm_status_t
i_ipadm_delete_ipv6addrs(ipadm_handle_t iph, ipadm_addrobj_t ipaddr)
{
	ipadm_status_t status;

	/*
	 * Send a msg to in.ndpd to remove the autoconfigured addresses,
	 * and delete the link local that was created.
	 */
	status = i_ipadm_send_ndpd_cmd(ipaddr->ipadm_ifname, ipaddr,
	    IPADM_DELETE_ADDRS);
	if (status == IPADM_NDPD_NOT_RUNNING)
		status = IPADM_SUCCESS;
	if (status == IPADM_SUCCESS)
		status = i_ipadm_delete_addr(iph, ipaddr);

	return (status);
}

static ipadm_status_t
i_ipadm_create_linklocal(ipadm_handle_t iph, ipadm_addrobj_t addr)
{
	boolean_t addif = B_FALSE;
	struct sockaddr_in6 *sin6;
	struct lifreq lifr;
	int err;
	ipadm_status_t status;
	in6_addr_t ll_template = {0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

	/*
	 * Create a logical interface if needed.
	 */
retry:
	status = i_ipadm_do_addif(iph, addr);
	if (status != IPADM_SUCCESS)
		return (status);
	if (!(iph->iph_flags & IPH_INIT)) {
		status = i_ipadm_setlifnum_addrobj(iph, addr);
		if (status == IPADM_ADDROBJ_EXISTS)
			goto retry;
		if (status != IPADM_SUCCESS)
			return (status);
	}

	bzero(&lifr, sizeof (lifr));
	(void) strlcpy(lifr.lifr_name, addr->ipadm_ifname, LIFNAMSIZ);
	i_ipadm_addrobj2lifname(addr, lifr.lifr_name, sizeof (lifr.lifr_name));
	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;

	/* Create the link-local address */
	bzero(&lifr.lifr_addr, sizeof (lifr.lifr_addr));
	(void) plen2mask(PREFIXLEN_LINKLOCAL, AF_INET6,
	    (struct sockaddr *)&lifr.lifr_addr);
	if ((err = ioctl(iph->iph_sock6, SIOCSLIFNETMASK, (caddr_t)&lifr)) < 0)
		goto fail;
	if (addr->ipadm_intfidlen == 0) {
		/*
		 * If we have to use the default interface id,
		 * we just need to set the prefix to the link-local prefix.
		 * SIOCSLIFPREFIX sets the address with the given prefix
		 * and the default interface id.
		 */
		sin6->sin6_addr = ll_template;
		err = ioctl(iph->iph_sock6, SIOCSLIFPREFIX, (caddr_t)&lifr);
		if (err < 0)
			goto fail;
	} else {
		/* Make a linklocal address in sin6 and set it */
		i_ipadm_make_linklocal(sin6, &addr->ipadm_intfid.sin6_addr);
		err = ioctl(iph->iph_sock6, SIOCSLIFADDR, (caddr_t)&lifr);
		if (err < 0)
			goto fail;
	}
	if ((err = ioctl(iph->iph_sock6, SIOCGLIFFLAGS, (char *)&lifr)) < 0)
		goto fail;
	lifr.lifr_flags |= IFF_UP;
	if ((err = ioctl(iph->iph_sock6, SIOCSLIFFLAGS, (char *)&lifr)) < 0)
		goto fail;
	return (IPADM_SUCCESS);

fail:
	if (errno == EEXIST)
		status = IPADM_ADDRCONF_EXISTS;
	else
		status = ipadm_errno2status(errno);
	/* Remove the linklocal that was created. */
	if (addif) {
		(void) ioctl(iph->iph_sock6, SIOCLIFREMOVEIF, (caddr_t)&lifr);
	} else {
		struct sockaddr_in6 *sin6;

		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
		lifr.lifr_flags &= ~IFF_UP;
		(void) ioctl(iph->iph_sock6, SIOCSLIFFLAGS, (caddr_t)&lifr);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_addr = in6addr_any;
		(void) ioctl(iph->iph_sock6, SIOCSLIFADDR, (caddr_t)&lifr);
	}
	return (status);
}

/*
 * Make a linklocal address based on the given intfid and copy it into
 * the output parameter `sin6'.
 */
static void
i_ipadm_make_linklocal(struct sockaddr_in6 *sin6, const struct in6_addr *intfid)
{
	int i;
	in6_addr_t ll_template = {0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

	sin6->sin6_family = AF_INET6;
	sin6->sin6_addr = *intfid;
	for (i = 0; i < 4; i++) {
		sin6->sin6_addr.s6_addr[i] =
		    sin6->sin6_addr.s6_addr[i] | ll_template.s6_addr[i];
	}
}

/*
 * Function that forms an ndpd msg and sends it to the in.ndpd daemon's loopback
 * listener socket.
 */
static ipadm_status_t
i_ipadm_send_ndpd_cmd(const char *ifname, const struct ipadm_addrobj_s *addr,
    int cmd)
{
	int fd;
	struct sockaddr_un servaddr;
	int flags;
	ipadm_ndpd_msg_t msg;
	int retval;

	if (addr == NULL &&
	    (cmd == IPADM_CREATE_ADDRS || cmd == IPADM_DELETE_ADDRS)) {
		return (IPADM_INVALID_ARG);
	}

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1)
		return (IPADM_FAILURE);

	/* Put the socket in non-blocking mode */
	flags = fcntl(fd, F_GETFL, 0);
	if (flags != -1)
		(void) fcntl(fd, F_SETFL, flags | O_NONBLOCK);

	/* Connect to in.ndpd */
	bzero(&servaddr, sizeof (servaddr));
	servaddr.sun_family = AF_UNIX;
	(void) strlcpy(servaddr.sun_path, IPADM_UDS_PATH,
	    sizeof (servaddr.sun_path));
	if (connect(fd, (struct sockaddr *)&servaddr, sizeof (servaddr)) == -1)
		goto fail;

	bzero(&msg, sizeof (msg));
	msg.inm_cmd = cmd;
	(void) strlcpy(msg.inm_ifname, ifname, sizeof (msg.inm_ifname));
	if (addr != NULL) {
		msg.inm_intfid = addr->ipadm_intfid;
		msg.inm_intfidlen = addr->ipadm_intfidlen;
		msg.inm_stateless = addr->ipadm_stateless;
		msg.inm_stateful = addr->ipadm_stateful;
		if (cmd == IPADM_CREATE_ADDRS) {
			(void) strlcpy(msg.inm_aobjname, addr->ipadm_aobjname,
			    sizeof (msg.inm_aobjname));
		}
	}
	if (ipadm_ndpd_write(fd, &msg, sizeof (msg)) < 0)
		goto fail;
	if (ipadm_ndpd_read(fd, &retval, sizeof (retval)) < 0)
		goto fail;
	(void) close(fd);
	if (cmd == IPADM_CREATE_ADDRS && retval == EEXIST)
		return (IPADM_ADDRCONF_EXISTS);
	return (ipadm_errno2status(retval));
fail:
	(void) close(fd);
	return (IPADM_NDPD_NOT_RUNNING);
}

/*
 * Attempt to read `buflen' worth of bytes from `fd' into the buffer pointed
 * to by `buf'.
 */
int
ipadm_ndpd_read(int fd, void *buffer, size_t buflen)
{
	int		retval;
	ssize_t		nbytes = 0;	/* total bytes processed */
	ssize_t		prbytes;	/* per-round bytes processed */
	struct pollfd	pfd;

	while (nbytes < buflen) {

		pfd.fd = fd;
		pfd.events = POLLIN;

		/*
		 * Wait for data to come in or for the timeout to fire.
		 */
		retval = poll(&pfd, 1, NDPDTIMEOUT);
		if (retval <= 0) {
			if (retval == 0)
				errno = ETIME;
			break;
		}

		/*
		 * Descriptor is ready; have at it.
		 */
		prbytes = read(fd, (caddr_t)buffer + nbytes, buflen - nbytes);
		if (prbytes <= 0) {
			if (prbytes == -1 && errno == EINTR)
				continue;
			break;
		}
		nbytes += prbytes;
	}

	return (nbytes == buflen ? 0 : -1);
}

/*
 * Write `buflen' bytes from `buffer' to open file `fd'.  Returns 0
 * if all requested bytes were written, or an error code if not.
 */
int
ipadm_ndpd_write(int fd, const void *buffer, size_t buflen)
{
	size_t		nwritten;
	ssize_t		nbytes;
	const char	*buf = buffer;

	for (nwritten = 0; nwritten < buflen; nwritten += nbytes) {
		nbytes = write(fd, &buf[nwritten], buflen - nwritten);
		if (nbytes == -1)
			return (-1);
		if (nbytes == 0) {
			errno = EIO;
			return (-1);
		}
	}

	assert(nwritten == buflen);
	return (0);
}