summaryrefslogtreecommitdiff
path: root/usr/src/lib/varpd/direct/common/libvarpd_direct.c
blob: ed9f79fc7f85edeccece69be4c7158a28c584d80 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2016 Joyent, Inc.
 */

/*
 * Point to point plug-in for varpd.
 *
 * This plugin implements a simple point to point plugin for a packet. It
 * represents the traditional tunnel, just in overlay form. As such, the only
 * properties it needs are those to determine where to send everything. At this
 * time, we don't allow a multicast address; however, there's no reason that the
 * direct plugin shouldn't in theory support multicast, though when implementing
 * it the best path will become clear.
 *
 * In general this module has been designed to make it easy to support a
 * destination of either IP or IP and port; however, we restrict it to the
 * latter as we don't currently have an implementation that would allow us to
 * test that.
 */

#include <libvarpd_provider.h>
#include <umem.h>
#include <errno.h>
#include <thread.h>
#include <synch.h>
#include <strings.h>
#include <assert.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <libnvpair.h>

typedef struct varpd_direct {
	overlay_plugin_dest_t	vad_dest;	/* RO */
	mutex_t			vad_lock;	/* Protects the rest */
	boolean_t		vad_hip;
	boolean_t		vad_hport;
	struct in6_addr		vad_ip;
	uint16_t		vad_port;
} varpd_direct_t;

static const char *varpd_direct_props[] = {
	"direct/dest_ip",
	"direct/dest_port"
};

static boolean_t
varpd_direct_valid_dest(overlay_plugin_dest_t dest)
{
	if (dest & ~(OVERLAY_PLUGIN_D_IP | OVERLAY_PLUGIN_D_PORT))
		return (B_FALSE);

	if (!(dest & (OVERLAY_PLUGIN_D_IP | OVERLAY_PLUGIN_D_PORT)))
		return (B_FALSE);

	return (B_TRUE);
}

/* ARGSUSED */
static int
varpd_direct_create(varpd_provider_handle_t *hdl, void **outp,
    overlay_plugin_dest_t dest)
{
	int ret;
	varpd_direct_t *vdp;

	if (varpd_direct_valid_dest(dest) == B_FALSE)
		return (ENOTSUP);

	vdp = umem_alloc(sizeof (varpd_direct_t), UMEM_DEFAULT);
	if (vdp == NULL)
		return (ENOMEM);

	if ((ret = mutex_init(&vdp->vad_lock, USYNC_THREAD | LOCK_ERRORCHECK,
	    NULL)) != 0) {
		umem_free(vdp, sizeof (varpd_direct_t));
		return (ret);
	}

	vdp->vad_dest = dest;
	vdp->vad_hip = B_FALSE;
	vdp->vad_hport = B_FALSE;
	*outp = vdp;
	return (0);
}

static int
varpd_direct_start(void *arg)
{
	varpd_direct_t *vdp = arg;

	mutex_enter(&vdp->vad_lock);
	if (vdp->vad_hip == B_FALSE ||((vdp->vad_dest & OVERLAY_PLUGIN_D_IP) &&
	    vdp->vad_hport == B_FALSE)) {
		mutex_exit(&vdp->vad_lock);
		return (EAGAIN);
	}
	mutex_exit(&vdp->vad_lock);

	return (0);
}

/* ARGSUSED */
static void
varpd_direct_stop(void *arg)
{
}

static void
varpd_direct_destroy(void *arg)
{
	varpd_direct_t *vdp = arg;

	if (mutex_destroy(&vdp->vad_lock) != 0)
		abort();
	umem_free(vdp, sizeof (varpd_direct_t));
}

static int
varpd_direct_default(void *arg, overlay_target_point_t *otp)
{
	varpd_direct_t *vdp = arg;

	mutex_enter(&vdp->vad_lock);
	bcopy(&vdp->vad_ip, &otp->otp_ip, sizeof (struct in6_addr));
	otp->otp_port = vdp->vad_port;
	mutex_exit(&vdp->vad_lock);

	return (VARPD_LOOKUP_OK);
}

static int
varpd_direct_nprops(void *arg, uint_t *nprops)
{
	const varpd_direct_t *vdp = arg;

	*nprops = 0;
	if (vdp->vad_dest & OVERLAY_PLUGIN_D_ETHERNET)
		*nprops += 1;

	if (vdp->vad_dest & OVERLAY_PLUGIN_D_IP)
		*nprops += 1;

	if (vdp->vad_dest & OVERLAY_PLUGIN_D_PORT)
		*nprops += 1;

	assert(*nprops == 1 || *nprops == 2);

	return (0);
}

static int
varpd_direct_propinfo(void *arg, uint_t propid, varpd_prop_handle_t *vph)
{
	varpd_direct_t *vdp = arg;

	/*
	 * Because we only support IP + port combos right now, prop 0 should
	 * always be the IP. We don't support a port without an IP.
	 */
	assert(vdp->vad_dest & OVERLAY_PLUGIN_D_IP);
	if (propid == 0) {
		libvarpd_prop_set_name(vph, varpd_direct_props[0]);
		libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
		libvarpd_prop_set_type(vph, OVERLAY_PROP_T_IP);
		libvarpd_prop_set_nodefault(vph);
		return (0);
	}

	if (propid == 1 && vdp->vad_dest & OVERLAY_PLUGIN_D_PORT) {
		libvarpd_prop_set_name(vph, varpd_direct_props[1]);
		libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
		libvarpd_prop_set_type(vph, OVERLAY_PROP_T_UINT);
		libvarpd_prop_set_nodefault(vph);
		libvarpd_prop_set_range_uint32(vph, 1, UINT16_MAX);
		return (0);
	}

	return (EINVAL);
}

static int
varpd_direct_getprop(void *arg, const char *pname, void *buf, uint32_t *sizep)
{
	varpd_direct_t *vdp = arg;

	/* direct/dest_ip */
	if (strcmp(pname, varpd_direct_props[0]) == 0) {
		if (*sizep < sizeof (struct in6_addr))
			return (EOVERFLOW);
		mutex_enter(&vdp->vad_lock);
		if (vdp->vad_hip == B_FALSE) {
			*sizep = 0;
		} else {
			bcopy(&vdp->vad_ip, buf, sizeof (struct in6_addr));
			*sizep = sizeof (struct in6_addr);
		}
		mutex_exit(&vdp->vad_lock);
		return (0);
	}

	/* direct/dest_port */
	if (strcmp(pname, varpd_direct_props[1]) == 0) {
		uint64_t val;

		if (*sizep < sizeof (uint64_t))
			return (EOVERFLOW);
		mutex_enter(&vdp->vad_lock);
		if (vdp->vad_hport == B_FALSE) {
			*sizep = 0;
		} else {
			val = vdp->vad_port;
			bcopy(&val, buf, sizeof (uint64_t));
			*sizep = sizeof (uint64_t);
		}
		mutex_exit(&vdp->vad_lock);
		return (0);
	}

	return (EINVAL);
}

static int
varpd_direct_setprop(void *arg, const char *pname, const void *buf,
    const uint32_t size)
{
	varpd_direct_t *vdp = arg;

	/* direct/dest_ip */
	if (strcmp(pname, varpd_direct_props[0]) == 0) {
		const struct in6_addr *ipv6 = buf;

		if (size < sizeof (struct in6_addr))
			return (EOVERFLOW);

		if (IN6_IS_ADDR_V4COMPAT(ipv6))
			return (EINVAL);

		if (IN6_IS_ADDR_6TO4(ipv6))
			return (EINVAL);

		mutex_enter(&vdp->vad_lock);
		bcopy(buf, &vdp->vad_ip, sizeof (struct in6_addr));
		vdp->vad_hip = B_TRUE;
		mutex_exit(&vdp->vad_lock);
		return (0);
	}

	/* direct/dest_port */
	if (strcmp(pname, varpd_direct_props[1]) == 0) {
		const uint64_t *valp = buf;
		if (size < sizeof (uint64_t))
			return (EOVERFLOW);

		if (*valp == 0 || *valp > UINT16_MAX)
			return (EINVAL);

		mutex_enter(&vdp->vad_lock);
		vdp->vad_port = (uint16_t)*valp;
		vdp->vad_hport = B_TRUE;
		mutex_exit(&vdp->vad_lock);
		return (0);
	}

	return (EINVAL);
}

static int
varpd_direct_save(void *arg, nvlist_t *nvp)
{
	int ret;
	varpd_direct_t *vdp = arg;

	mutex_enter(&vdp->vad_lock);
	if (vdp->vad_hport == B_TRUE) {
		if ((ret = nvlist_add_uint16(nvp, varpd_direct_props[1],
		    vdp->vad_port)) != 0) {
			mutex_exit(&vdp->vad_lock);
			return (ret);
		}
	}

	if (vdp->vad_hip == B_TRUE) {
		char buf[INET6_ADDRSTRLEN];

		if (inet_ntop(AF_INET6, &vdp->vad_ip, buf, sizeof (buf)) ==
		    NULL)
			abort();
		if ((ret = nvlist_add_string(nvp, varpd_direct_props[0],
		    buf)) != 0) {
			mutex_exit(&vdp->vad_lock);
			return (ret);
		}
	}
	mutex_exit(&vdp->vad_lock);

	return (0);
}

/* ARGSUSED */
static int
varpd_direct_restore(nvlist_t *nvp, varpd_provider_handle_t *hdl,
    overlay_plugin_dest_t dest, void **outp)
{
	int ret;
	char *ipstr;
	varpd_direct_t *vdp;

	if (varpd_direct_valid_dest(dest) == B_FALSE)
		return (ENOTSUP);

	vdp = umem_alloc(sizeof (varpd_direct_t), UMEM_DEFAULT);
	if (vdp == NULL)
		return (ENOMEM);

	if ((ret = mutex_init(&vdp->vad_lock, USYNC_THREAD | LOCK_ERRORCHECK,
	    NULL)) != 0) {
		umem_free(vdp, sizeof (varpd_direct_t));
		return (ret);
	}

	if ((ret = nvlist_lookup_uint16(nvp, varpd_direct_props[1],
	    &vdp->vad_port)) != 0) {
		if (ret != ENOENT) {
			if (mutex_destroy(&vdp->vad_lock) != 0)
				abort();
			umem_free(vdp, sizeof (varpd_direct_t));
			return (ret);
		}
		vdp->vad_hport = B_FALSE;
	} else {
		vdp->vad_hport = B_TRUE;
	}

	if ((ret = nvlist_lookup_string(nvp, varpd_direct_props[0],
	    &ipstr)) != 0) {
		if (ret != ENOENT) {
			if (mutex_destroy(&vdp->vad_lock) != 0)
				abort();
			umem_free(vdp, sizeof (varpd_direct_t));
			return (ret);
		}
		vdp->vad_hip = B_FALSE;
	} else {
		ret = inet_pton(AF_INET6, ipstr, &vdp->vad_ip);
		/*
		 * inet_pton is only defined to return -1 with errno set to
		 * EAFNOSUPPORT, which really, shouldn't happen.
		 */
		if (ret == -1) {
			assert(errno == EAFNOSUPPORT);
			abort();
		}
		if (ret == 0) {
			if (mutex_destroy(&vdp->vad_lock) != 0)
				abort();
			umem_free(vdp, sizeof (varpd_direct_t));
			return (EINVAL);
		}
	}

	*outp = vdp;
	return (0);
}

static const varpd_plugin_ops_t varpd_direct_ops = {
	0,
	varpd_direct_create,
	varpd_direct_start,
	varpd_direct_stop,
	varpd_direct_destroy,
	varpd_direct_default,
	NULL,
	varpd_direct_nprops,
	varpd_direct_propinfo,
	varpd_direct_getprop,
	varpd_direct_setprop,
	varpd_direct_save,
	varpd_direct_restore
};

#pragma init(varpd_direct_init)
static void
varpd_direct_init(void)
{
	int err;
	varpd_plugin_register_t *vpr;

	vpr = libvarpd_plugin_alloc(VARPD_CURRENT_VERSION, &err);
	if (vpr == NULL)
		return;

	vpr->vpr_mode = OVERLAY_TARGET_POINT;
	vpr->vpr_name = "direct";
	vpr->vpr_ops = &varpd_direct_ops;
	(void) libvarpd_plugin_register(vpr);
	libvarpd_plugin_free(vpr);
}