xref: /illumos-gate/usr/src/lib/libdladm/common/libdloverlay.c (revision f24fee035ef9b37d5a6868aed10261da6316a6b2)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015 Joyent, Inc.
14  */
15 
16 #include <libdladm_impl.h>
17 #include <libdllink.h>
18 #include <libdloverlay.h>
19 #include <sys/dld.h>
20 #include <sys/overlay.h>
21 #include <strings.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <limits.h>
28 #include <libvarpd_client.h>
29 
30 #define	VARPD_PROPERTY_NAME	"varpd/id"
31 
32 static const char *dladm_overlay_doorpath = "/var/run/varpd/varpd.door";
33 
34 typedef struct dladm_overlay_propinfo {
35 	boolean_t dop_isvarpd;
36 	union {
37 		overlay_ioc_propinfo_t *dop_overlay;
38 		varpd_client_prop_handle_t *dop_varpd;
39 	} dop_un;
40 } dladm_overlay_propinfo_t;
41 
42 dladm_status_t
43 dladm_overlay_prop_info(dladm_overlay_propinfo_handle_t phdl,
44     const char **namep, uint_t *typep, uint_t *protp, const void **defp,
45     uint32_t *sizep, const mac_propval_range_t **possp)
46 {
47 	dladm_overlay_propinfo_t *infop = (dladm_overlay_propinfo_t *)phdl;
48 	overlay_ioc_propinfo_t *oinfop = infop->dop_un.dop_overlay;
49 
50 	if (infop->dop_isvarpd == B_FALSE) {
51 		if (namep != NULL)
52 			*namep = oinfop->oipi_name;
53 		if (typep != NULL)
54 			*typep = oinfop->oipi_type;
55 		if (protp != NULL)
56 			*protp = oinfop->oipi_prot;
57 		if (defp != NULL)
58 			*defp = oinfop->oipi_default;
59 		if (sizep != NULL)
60 			*sizep = oinfop->oipi_defsize;
61 		if (possp != NULL) {
62 			*possp = (const mac_propval_range_t *)oinfop->oipi_poss;
63 		}
64 
65 	} else {
66 		int ret;
67 		ret = libvarpd_c_prop_info(infop->dop_un.dop_varpd, namep,
68 		    typep, protp, defp, sizep, possp);
69 		if (ret != 0)
70 			return (dladm_errno2status(ret));
71 
72 	}
73 
74 	return (DLADM_STATUS_OK);
75 }
76 
77 static dladm_status_t
78 dladm_overlay_parse_prop(overlay_prop_type_t type, void *buf, uint32_t *sizep,
79     const char *val)
80 {
81 	int ret;
82 	int64_t ival;
83 	uint64_t uval;
84 	char *eptr;
85 	struct in6_addr ipv6;
86 	struct in_addr ip;
87 
88 	switch (type) {
89 	case OVERLAY_PROP_T_INT:
90 		errno = 0;
91 		ival = strtol(val, &eptr, 10);
92 		if ((ival == 0 && errno == EINVAL) ||
93 		    ((ival == LONG_MAX || ival == LONG_MIN) &&
94 		    errno == ERANGE))
95 			return (DLADM_STATUS_BADARG);
96 		bcopy(&ival, buf, sizeof (int64_t));
97 		*sizep = sizeof (int64_t);
98 		break;
99 	case OVERLAY_PROP_T_UINT:
100 		errno = 0;
101 		uval = strtol(val, &eptr, 10);
102 		if ((uval == 0 && errno == EINVAL) ||
103 		    (uval == ULONG_MAX && errno == ERANGE))
104 			return (DLADM_STATUS_BADARG);
105 		bcopy(&uval, buf, sizeof (uint64_t));
106 		*sizep = sizeof (uint64_t);
107 		break;
108 	case OVERLAY_PROP_T_STRING:
109 		ret = strlcpy((char *)buf, val, OVERLAY_PROP_SIZEMAX);
110 		if (ret >= OVERLAY_PROP_SIZEMAX)
111 			return (DLADM_STATUS_BADARG);
112 		*sizep = ret + 1;
113 		break;
114 	case OVERLAY_PROP_T_IP:
115 		/*
116 		 * Always try to parse the IP as an IPv6 address. If that fails,
117 		 * try to interpret it as an IPv4 address and transform it into
118 		 * an IPv6 mapped IPv4 address.
119 		 */
120 		if (inet_pton(AF_INET6, val, &ipv6) != 1) {
121 			if (inet_pton(AF_INET, val, &ip) != 1)
122 				return (DLADM_STATUS_BADARG);
123 
124 			IN6_INADDR_TO_V4MAPPED(&ip, &ipv6);
125 		}
126 		bcopy(&ipv6, buf, sizeof (struct in6_addr));
127 		*sizep = sizeof (struct in6_addr);
128 		break;
129 	default:
130 		abort();
131 	}
132 
133 	return (DLADM_STATUS_OK);
134 }
135 
136 /* ARGSUSED */
137 static dladm_status_t
138 dladm_overlay_varpd_setprop(dladm_handle_t handle, varpd_client_handle_t *chdl,
139     uint64_t inst, const char *name, char *const *valp, uint_t cnt)
140 {
141 	int ret;
142 	uint32_t size;
143 	uint8_t buf[LIBVARPD_PROP_SIZEMAX];
144 	varpd_client_prop_handle_t *phdl;
145 	uint_t type;
146 	dladm_status_t status;
147 
148 	if ((ret = libvarpd_c_prop_handle_alloc(chdl, inst, &phdl)) != 0)
149 		return (dladm_errno2status(ret));
150 
151 	if ((ret = libvarpd_c_prop_info_fill_by_name(phdl, name)) != 0) {
152 		libvarpd_c_prop_handle_free(phdl);
153 		return (dladm_errno2status(ret));
154 	}
155 
156 	if ((ret = libvarpd_c_prop_info(phdl, NULL, &type, NULL, NULL, NULL,
157 	    NULL)) != 0) {
158 		libvarpd_c_prop_handle_free(phdl);
159 		return (dladm_errno2status(ret));
160 	}
161 
162 	if ((status = dladm_overlay_parse_prop(type, buf, &size, valp[0])) !=
163 	    DLADM_STATUS_OK) {
164 		libvarpd_c_prop_handle_free(phdl);
165 		return (status);
166 	}
167 
168 	ret = libvarpd_c_prop_set(phdl, buf, size);
169 	libvarpd_c_prop_handle_free(phdl);
170 
171 	return (dladm_errno2status(ret));
172 }
173 
174 dladm_status_t
175 dladm_overlay_setprop(dladm_handle_t handle, datalink_id_t linkid,
176     const char *name, char *const *valp, uint_t cnt)
177 {
178 	int			ret;
179 	dladm_status_t		status;
180 	overlay_ioc_propinfo_t	info;
181 	overlay_ioc_prop_t	prop;
182 
183 	if (linkid == DATALINK_INVALID_LINKID ||
184 	    name == NULL || valp == NULL || cnt != 1)
185 		return (DLADM_STATUS_BADARG);
186 
187 	bzero(&info, sizeof (overlay_ioc_propinfo_t));
188 	info.oipi_linkid = linkid;
189 	info.oipi_id = -1;
190 	if (strlcpy(info.oipi_name, name, OVERLAY_PROP_NAMELEN) >=
191 	    OVERLAY_PROP_NAMELEN)
192 		return (DLADM_STATUS_BADARG);
193 
194 	status = DLADM_STATUS_OK;
195 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_PROPINFO, &info);
196 	if (ret != 0)
197 		status = dladm_errno2status(errno);
198 
199 	if (status != DLADM_STATUS_OK)
200 		return (status);
201 
202 	prop.oip_linkid = linkid;
203 	prop.oip_id = info.oipi_id;
204 	prop.oip_name[0] = '\0';
205 	if ((ret = dladm_overlay_parse_prop(info.oipi_type, prop.oip_value,
206 	    &prop.oip_size, valp[0])) != DLADM_STATUS_OK)
207 		return (ret);
208 
209 	status = DLADM_STATUS_OK;
210 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_SETPROP, &prop);
211 	if (ret != 0)
212 		status = dladm_errno2status(errno);
213 
214 	return (ret);
215 }
216 
217 /*
218  * Tell the user about any unset required properties.
219  */
220 static int
221 dladm_overlay_activate_cb(dladm_handle_t handle, datalink_id_t linkid,
222     dladm_overlay_propinfo_handle_t phdl, void *arg)
223 {
224 	dladm_status_t status;
225 	uint8_t buf[DLADM_OVERLAY_PROP_SIZEMAX];
226 	uint_t prot;
227 	size_t size = sizeof (buf);
228 	const char *name;
229 	dladm_errlist_t *errs = arg;
230 
231 	if ((status = dladm_overlay_prop_info(phdl, &name, NULL, &prot, NULL,
232 	    NULL, NULL)) != DLADM_STATUS_OK)
233 		return (status);
234 
235 	if ((prot & OVERLAY_PROP_PERM_REQ) == 0)
236 		return (DLADM_WALK_CONTINUE);
237 
238 	if (dladm_overlay_get_prop(handle, linkid, phdl, buf, &size) !=
239 	    DLADM_STATUS_OK)
240 		return (DLADM_WALK_CONTINUE);
241 
242 	if (size == 0)
243 		(void) dladm_errlist_append(errs, "unset required property: %s",
244 		    name);
245 
246 	return (DLADM_WALK_CONTINUE);
247 }
248 
249 /*
250  * We need to clean up the world here. The problem is that we may or may not
251  * actually have everything created. While in the normal case, we'd always have
252  * an overlay device, assigned datalink id, and a varpd instance, we might not
253  * have any of those, except for the datalink instance. Therefore, as long as
254  * the id refers to a valid overlay, we should try to clean up as much of the
255  * state as possible and most importantly, we need to make sure we delete the
256  * datalink id. If we fail to do that, then that name will become lost to time.
257  */
258 dladm_status_t
259 dladm_overlay_delete(dladm_handle_t handle, datalink_id_t linkid)
260 {
261 	datalink_class_t class;
262 	overlay_ioc_delete_t oid;
263 	varpd_client_handle_t *chdl;
264 	int ret;
265 	uint32_t flags;
266 	uint64_t varpdid;
267 
268 	if (dladm_datalink_id2info(handle, linkid, &flags, &class, NULL,
269 	    NULL, 0) != DLADM_STATUS_OK)
270 		return (DLADM_STATUS_BADARG);
271 
272 	if (class != DATALINK_CLASS_OVERLAY)
273 		return (DLADM_STATUS_BADARG);
274 
275 	oid.oid_linkid = linkid;
276 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_DELETE, &oid);
277 	if (ret != 0 && errno != ENOENT) {
278 		return (dladm_errno2status(errno));
279 	}
280 
281 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0) {
282 		return (dladm_errno2status(ret));
283 	}
284 
285 	if ((ret = libvarpd_c_instance_lookup(chdl, linkid, &varpdid)) != 0) {
286 		if (ret == ENOENT) {
287 			goto finish;
288 		}
289 		(void) libvarpd_c_destroy(chdl);
290 		return (dladm_errno2status(ret));
291 	}
292 
293 	ret = libvarpd_c_instance_destroy(chdl, varpdid);
294 finish:
295 	(void) libvarpd_c_destroy(chdl);
296 	(void) dladm_destroy_datalink_id(handle, linkid, flags);
297 
298 	return (dladm_errno2status(ret));
299 }
300 
301 dladm_status_t
302 dladm_overlay_get_prop(dladm_handle_t handle, datalink_id_t linkid,
303     dladm_overlay_propinfo_handle_t infohdl, void *buf, size_t *sizep)
304 {
305 	int ret;
306 	overlay_ioc_prop_t oip;
307 	dladm_overlay_propinfo_t *infop = (dladm_overlay_propinfo_t *)infohdl;
308 
309 	/*
310 	 * It'd be nice if we had a better or more specific error for this. If
311 	 * this kind of error becomes common place, let's get a better dladm
312 	 * error.
313 	 */
314 	if (*sizep < DLADM_OVERLAY_PROP_SIZEMAX)
315 		return (dladm_errno2status(ERANGE));
316 
317 	if (infop->dop_isvarpd == B_FALSE) {
318 		bzero(&oip, sizeof (overlay_ioc_prop_t));
319 		oip.oip_linkid = linkid;
320 		oip.oip_id = infop->dop_un.dop_overlay->oipi_id;
321 		ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_GETPROP, &oip);
322 		if (ret != 0)
323 			return (dladm_errno2status(errno));
324 		bcopy(oip.oip_value, buf, DLADM_OVERLAY_PROP_SIZEMAX);
325 		*sizep = oip.oip_size;
326 	} else {
327 		uint32_t size = *sizep;
328 
329 		ret = libvarpd_c_prop_get(infop->dop_un.dop_varpd, buf, &size);
330 		if (ret != 0)
331 			return (dladm_errno2status(errno));
332 		*sizep = size;
333 	}
334 
335 	return (DLADM_STATUS_OK);
336 }
337 
338 static dladm_status_t
339 dladm_overlay_walk_varpd_prop(dladm_handle_t handle, datalink_id_t linkid,
340     uint64_t varpdid, dladm_overlay_prop_f func, void *arg)
341 {
342 	int ret, i;
343 	varpd_client_handle_t *chdl;
344 	varpd_client_prop_handle_t *phdl;
345 	uint_t nprops;
346 	dladm_status_t status;
347 
348 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0)
349 		return (dladm_errno2status(ret));
350 
351 	if ((ret = libvarpd_c_prop_handle_alloc(chdl, varpdid, &phdl)) != 0) {
352 		(void) libvarpd_c_destroy(chdl);
353 		return (dladm_errno2status(ret));
354 	}
355 
356 	if ((ret = libvarpd_c_prop_nprops(chdl, varpdid, &nprops)) != 0) {
357 		libvarpd_c_prop_handle_free(phdl);
358 		(void) libvarpd_c_destroy(chdl);
359 		return (dladm_errno2status(ret));
360 	}
361 
362 	status = DLADM_STATUS_OK;
363 	for (i = 0; i < nprops; i++) {
364 		dladm_overlay_propinfo_t dop;
365 
366 		bzero(&dop, sizeof (dop));
367 		dop.dop_isvarpd = B_TRUE;
368 		dop.dop_un.dop_varpd = phdl;
369 
370 		if ((ret = libvarpd_c_prop_info_fill(phdl, i)) != 0) {
371 			status = dladm_errno2status(ret);
372 			break;
373 		}
374 
375 		ret = func(handle, linkid,
376 		    (dladm_overlay_propinfo_handle_t)&dop, arg);
377 		if (ret == DLADM_WALK_TERMINATE)
378 			break;
379 	}
380 
381 	libvarpd_c_prop_handle_free(phdl);
382 	libvarpd_c_destroy(chdl);
383 
384 	return (status);
385 }
386 
387 dladm_status_t
388 dladm_overlay_walk_prop(dladm_handle_t handle, datalink_id_t linkid,
389     dladm_overlay_prop_f func, void *arg, dladm_errlist_t *errs)
390 {
391 	int i, ret;
392 	datalink_class_t class;
393 	overlay_ioc_nprops_t oin;
394 	overlay_ioc_propinfo_t oipi;
395 	dladm_overlay_propinfo_t dop;
396 	uint64_t varpdid = UINT64_MAX;
397 
398 	if (dladm_datalink_id2info(handle, linkid, NULL, &class, NULL,
399 	    NULL, 0) != DLADM_STATUS_OK)
400 		return (DLADM_STATUS_BADARG);
401 
402 	if (class != DATALINK_CLASS_OVERLAY)
403 		return (DLADM_STATUS_BADARG);
404 
405 	bzero(&oin, sizeof (overlay_ioc_nprops_t));
406 	oin.oipn_linkid = linkid;
407 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_NPROPS, &oin);
408 	if (ret != 0)
409 		return (dladm_errno2status(errno));
410 
411 	for (i = 0; i < oin.oipn_nprops; i++) {
412 		bzero(&dop, sizeof (dladm_overlay_propinfo_t));
413 		bzero(&oipi, sizeof (overlay_ioc_propinfo_t));
414 		oipi.oipi_linkid = linkid;
415 		oipi.oipi_id = i;
416 		ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_PROPINFO, &oipi);
417 		if (ret != 0) {
418 			(void) dladm_errlist_append(errs, "failed to get "
419 			    "propinfo for property %d: %s", i, strerror(errno));
420 			return (dladm_errno2status(errno));
421 		}
422 
423 		dop.dop_isvarpd = B_FALSE;
424 		dop.dop_un.dop_overlay = &oipi;
425 		ret = func(handle, linkid,
426 		    (dladm_overlay_propinfo_handle_t)&dop, arg);
427 		if (ret == DLADM_WALK_TERMINATE)
428 			break;
429 
430 		if (strcmp(oipi.oipi_name, VARPD_PROPERTY_NAME) == 0) {
431 			uint8_t buf[DLADM_OVERLAY_PROP_SIZEMAX];
432 			size_t bufsize = sizeof (buf);
433 			uint64_t *vp;
434 
435 			if (dladm_overlay_get_prop(handle, linkid,
436 			    (dladm_overlay_propinfo_handle_t)&dop, buf,
437 			    &bufsize) != DLADM_STATUS_OK)
438 				continue;
439 
440 			vp = (uint64_t *)buf;
441 			varpdid = *vp;
442 		}
443 	}
444 
445 	/* Should this really be possible? */
446 	if (varpdid == UINT64_MAX)
447 		return (DLADM_STATUS_OK);
448 
449 	return (dladm_overlay_walk_varpd_prop(handle, linkid, varpdid, func,
450 	    arg));
451 }
452 
453 dladm_status_t
454 dladm_overlay_create(dladm_handle_t handle, const char *name,
455     const char *encap, const char *search, uint64_t vid,
456     dladm_arg_list_t *props, dladm_errlist_t *errs, uint32_t flags)
457 {
458 	int ret, i;
459 	dladm_status_t status;
460 	datalink_id_t linkid;
461 	overlay_ioc_create_t oic;
462 	overlay_ioc_activate_t oia;
463 	size_t slen;
464 	varpd_client_handle_t *vch;
465 	uint64_t id;
466 
467 	status = dladm_create_datalink_id(handle, name, DATALINK_CLASS_OVERLAY,
468 	    DL_ETHER, flags, &linkid);
469 	if (status != DLADM_STATUS_OK)
470 		return (status);
471 
472 	bzero(&oic, sizeof (oic));
473 	oic.oic_linkid = linkid;
474 	oic.oic_vnetid = vid;
475 	(void) strlcpy(oic.oic_encap, encap, MAXLINKNAMELEN);
476 
477 	status = DLADM_STATUS_OK;
478 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_CREATE, &oic);
479 	if (ret != 0) {
480 		/*
481 		 * It'd be nice if we had private errors so we could better
482 		 * distinguish between different classes of errors.
483 		 */
484 		status = dladm_errno2status(errno);
485 	}
486 
487 	if (status != DLADM_STATUS_OK) {
488 		(void) dladm_destroy_datalink_id(handle, linkid, flags);
489 		return (status);
490 	}
491 
492 	slen = strlen(search);
493 	for (i = 0; props != NULL && i < props->al_count; i++) {
494 		dladm_arg_info_t	*aip = &props->al_info[i];
495 
496 		/*
497 		 * If it's a property for the search plugin, eg. it has the
498 		 * prefix '<search>/', then we don't set the property on the
499 		 * overlay device and instead set it on the varpd instance.
500 		 */
501 		if (strncmp(aip->ai_name, search, slen) == 0 &&
502 		    aip->ai_name[slen] == '/')
503 			continue;
504 		status = dladm_overlay_setprop(handle, linkid, aip->ai_name,
505 		    aip->ai_val, aip->ai_count);
506 		if (status != DLADM_STATUS_OK) {
507 			(void) dladm_errlist_append(errs,
508 			    "failed to set property %s",
509 			    aip->ai_name);
510 			(void) dladm_overlay_delete(handle, linkid);
511 			return (status);
512 		}
513 	}
514 
515 	if ((ret = libvarpd_c_create(&vch, dladm_overlay_doorpath)) != 0) {
516 		(void) dladm_errlist_append(errs,
517 		    "failed to create libvarpd handle: %s", strerror(ret));
518 		(void) dladm_overlay_delete(handle, linkid);
519 		return (dladm_errno2status(ret));
520 	}
521 
522 	if ((ret = libvarpd_c_instance_create(vch, linkid, search,
523 	    &id)) != 0) {
524 		(void) dladm_errlist_append(errs,
525 		    "failed to create varpd instance: %s", strerror(ret));
526 		libvarpd_c_destroy(vch);
527 		(void) dladm_overlay_delete(handle, linkid);
528 		return (dladm_errno2status(ret));
529 	}
530 
531 	for (i = 0; props != NULL && i < props->al_count; i++) {
532 		dladm_arg_info_t	*aip = &props->al_info[i];
533 
534 		/*
535 		 * Skip arguments we've processed already.
536 		 */
537 		if (strncmp(aip->ai_name, search, slen) != 0)
538 			continue;
539 
540 		if (aip->ai_name[slen] != '/')
541 			continue;
542 
543 		ret = dladm_overlay_varpd_setprop(handle, vch, id, aip->ai_name,
544 		    aip->ai_val, aip->ai_count);
545 		if (ret != 0) {
546 			(void) dladm_errlist_append(errs,
547 			    "failed to set varpd prop: %s\n",
548 			    aip->ai_name);
549 			(void) libvarpd_c_instance_destroy(vch, id);
550 			libvarpd_c_destroy(vch);
551 			(void) dladm_overlay_delete(handle, linkid);
552 			return (dladm_errno2status(ret));
553 		}
554 	}
555 
556 	if ((ret = libvarpd_c_instance_activate(vch, id)) != 0) {
557 		(void) dladm_errlist_append(errs,
558 		    "failed to activate varpd instance: %s", strerror(ret));
559 		(void) dladm_overlay_walk_varpd_prop(handle, linkid, id,
560 		    dladm_overlay_activate_cb, errs);
561 		(void) libvarpd_c_instance_destroy(vch, id);
562 		libvarpd_c_destroy(vch);
563 		(void) dladm_overlay_delete(handle, linkid);
564 		return (dladm_errno2status(ret));
565 
566 	}
567 
568 	bzero(&oia, sizeof (oia));
569 	oia.oia_linkid = linkid;
570 	status = DLADM_STATUS_OK;
571 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_ACTIVATE, &oia);
572 	if (ret != 0) {
573 		ret = errno;
574 		(void) dladm_errlist_append(errs, "failed to activate "
575 		    "device: %s", strerror(ret));
576 		(void) libvarpd_c_instance_destroy(vch, id);
577 		(void) dladm_overlay_walk_prop(handle, linkid,
578 		    dladm_overlay_activate_cb, errs, errs);
579 		status = dladm_errno2status(ret);
580 		(void) libvarpd_c_instance_destroy(vch, id);
581 	}
582 
583 	libvarpd_c_destroy(vch);
584 	if (status != DLADM_STATUS_OK)
585 		(void) dladm_overlay_delete(handle, linkid);
586 
587 	return (status);
588 }
589 
590 
591 
592 typedef struct overlay_walk_cb {
593 	dladm_handle_t		owc_handle;
594 	datalink_id_t		owc_linkid;
595 	void			*owc_arg;
596 	dladm_overlay_cache_f	owc_func;
597 	uint_t			owc_mode;
598 	uint_t			owc_dest;
599 } overlay_walk_cb_t;
600 
601 /* ARGSUSED */
602 static int
603 dladm_overlay_walk_cache_cb(varpd_client_handle_t *chdl, uint64_t varpdid,
604     const struct ether_addr *key, const varpd_client_cache_entry_t *entry,
605     void *arg)
606 {
607 	overlay_walk_cb_t *owc = arg;
608 	dladm_overlay_point_t point;
609 
610 	bzero(&point, sizeof (dladm_overlay_point_t));
611 	point.dop_dest = owc->owc_dest;
612 	point.dop_mac = entry->vcp_mac;
613 	point.dop_flags = entry->vcp_flags;
614 	point.dop_ip = entry->vcp_ip;
615 	point.dop_port = entry->vcp_port;
616 
617 	if (owc->owc_mode == OVERLAY_TARGET_POINT)
618 		point.dop_flags |= DLADM_OVERLAY_F_DEFAULT;
619 
620 	if (owc->owc_func(owc->owc_handle, owc->owc_linkid, key, &point,
621 	    owc->owc_arg) == DLADM_WALK_TERMINATE)
622 		return (1);
623 	return (0);
624 }
625 
626 dladm_status_t
627 dladm_overlay_walk_cache(dladm_handle_t handle, datalink_id_t linkid,
628     dladm_overlay_cache_f func, void *arg)
629 {
630 	int ret;
631 	uint_t mode, dest;
632 	uint64_t varpdid;
633 	varpd_client_handle_t *chdl;
634 	overlay_walk_cb_t cbarg;
635 
636 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0)
637 		return (dladm_errno2status(ret));
638 
639 	if ((ret = libvarpd_c_instance_lookup(chdl, linkid, &varpdid)) != 0) {
640 		libvarpd_c_destroy(chdl);
641 		return (dladm_errno2status(ret));
642 	}
643 
644 	if ((ret = libvarpd_c_instance_target_mode(chdl, varpdid,
645 	    &dest, &mode)) != 0) {
646 		libvarpd_c_destroy(chdl);
647 		return (dladm_errno2status(ret));
648 	}
649 
650 	cbarg.owc_handle = handle;
651 	cbarg.owc_linkid = linkid;
652 	cbarg.owc_arg = arg;
653 	cbarg.owc_func = func;
654 	cbarg.owc_dest = dest;
655 	cbarg.owc_mode = mode;
656 	ret = libvarpd_c_instance_cache_walk(chdl, varpdid,
657 	    dladm_overlay_walk_cache_cb, &cbarg);
658 	libvarpd_c_destroy(chdl);
659 
660 	return (dladm_errno2status(ret));
661 }
662 
663 /* ARGSUSED */
664 dladm_status_t
665 dladm_overlay_cache_flush(dladm_handle_t handle, datalink_id_t linkid)
666 {
667 	int ret;
668 	uint64_t varpdid;
669 	varpd_client_handle_t *chdl;
670 
671 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0)
672 		return (dladm_errno2status(ret));
673 
674 	if ((ret = libvarpd_c_instance_lookup(chdl, linkid, &varpdid)) != 0) {
675 		libvarpd_c_destroy(chdl);
676 		return (dladm_errno2status(ret));
677 	}
678 
679 	ret = libvarpd_c_instance_cache_flush(chdl, varpdid);
680 	libvarpd_c_destroy(chdl);
681 
682 	return (dladm_errno2status(ret));
683 }
684 
685 /* ARGSUSED */
686 dladm_status_t
687 dladm_overlay_cache_delete(dladm_handle_t handle, datalink_id_t linkid,
688     const struct ether_addr *key)
689 {
690 	int ret;
691 	uint64_t varpdid;
692 	varpd_client_handle_t *chdl;
693 
694 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0)
695 		return (dladm_errno2status(ret));
696 
697 	if ((ret = libvarpd_c_instance_lookup(chdl, linkid, &varpdid)) != 0) {
698 		libvarpd_c_destroy(chdl);
699 		return (dladm_errno2status(ret));
700 	}
701 
702 	ret = libvarpd_c_instance_cache_delete(chdl, varpdid, key);
703 	libvarpd_c_destroy(chdl);
704 
705 	return (dladm_errno2status(ret));
706 }
707 
708 /* ARGSUSED */
709 dladm_status_t
710 dladm_overlay_cache_set(dladm_handle_t handle, datalink_id_t linkid,
711     const struct ether_addr *key, char *val)
712 {
713 	int ret;
714 	uint_t dest;
715 	uint64_t varpdid;
716 	char *ip, *port = NULL;
717 	varpd_client_handle_t *chdl;
718 	varpd_client_cache_entry_t vcp;
719 
720 
721 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0)
722 		return (dladm_errno2status(ret));
723 
724 	if ((ret = libvarpd_c_instance_lookup(chdl, linkid, &varpdid)) != 0) {
725 		libvarpd_c_destroy(chdl);
726 		return (dladm_errno2status(ret));
727 	}
728 
729 	if ((ret = libvarpd_c_instance_target_mode(chdl, varpdid,
730 	    &dest, NULL)) != 0) {
731 		libvarpd_c_destroy(chdl);
732 		return (dladm_errno2status(ret));
733 	}
734 
735 	/*
736 	 * Mode tells us what we should expect in val. It we have more than one
737 	 * thing listed, the canonical format of it right now is mac,ip:port.
738 	 */
739 	bzero(&vcp, sizeof (varpd_client_cache_entry_t));
740 
741 	if (strcasecmp(val, "drop") == 0) {
742 		vcp.vcp_flags = OVERLAY_TARGET_CACHE_DROP;
743 		goto send;
744 	}
745 
746 	if (dest & OVERLAY_PLUGIN_D_ETHERNET) {
747 		if (ether_aton_r(val, &vcp.vcp_mac) == NULL) {
748 			libvarpd_c_destroy(chdl);
749 			return (dladm_errno2status(EINVAL));
750 		}
751 	}
752 
753 	if (dest & OVERLAY_PLUGIN_D_IP) {
754 		if (dest & OVERLAY_PLUGIN_D_ETHERNET) {
755 			if ((ip = strchr(val, ',')) == NULL) {
756 				libvarpd_c_destroy(chdl);
757 				return (dladm_errno2status(ret));
758 			}
759 			ip++;
760 		} else {
761 			ip = val;
762 		}
763 
764 		if (dest & OVERLAY_PLUGIN_D_PORT) {
765 			if ((port = strchr(val, ':')) == NULL) {
766 				libvarpd_c_destroy(chdl);
767 				return (dladm_errno2status(ret));
768 			}
769 			*port = '\0';
770 			port++;
771 		}
772 
773 		/* Try v6, then fall back to v4 */
774 		ret = inet_pton(AF_INET6, ip, &vcp.vcp_ip);
775 		if (ret == -1)
776 			abort();
777 		if (ret == 0) {
778 			struct in_addr v4;
779 
780 			ret = inet_pton(AF_INET, ip, &v4);
781 			if (ret == -1)
782 				abort();
783 			if (ret == 0) {
784 				libvarpd_c_destroy(chdl);
785 				return (dladm_errno2status(ret));
786 			}
787 			IN6_INADDR_TO_V4MAPPED(&v4, &vcp.vcp_ip);
788 		}
789 	}
790 
791 	if (dest & OVERLAY_PLUGIN_D_PORT) {
792 		char *eptr;
793 		unsigned long l;
794 		if (port == NULL && (dest & OVERLAY_PLUGIN_D_ETHERNET)) {
795 			if ((port = strchr(val, ',')) == NULL) {
796 				libvarpd_c_destroy(chdl);
797 				return (dladm_errno2status(EINVAL));
798 			}
799 		} else if (port == NULL)
800 			port = val;
801 
802 		errno = 0;
803 		l = strtoul(port, &eptr, 10);
804 		if (errno != 0 || *eptr != '\0') {
805 			libvarpd_c_destroy(chdl);
806 			return (dladm_errno2status(EINVAL));
807 		}
808 		if (l == 0 || l > UINT16_MAX) {
809 			libvarpd_c_destroy(chdl);
810 			return (dladm_errno2status(EINVAL));
811 		}
812 		vcp.vcp_port = l;
813 	}
814 
815 send:
816 	ret = libvarpd_c_instance_cache_set(chdl, varpdid, key, &vcp);
817 
818 	libvarpd_c_destroy(chdl);
819 	return (dladm_errno2status(ret));
820 }
821 
822 /* ARGSUSED */
823 dladm_status_t
824 dladm_overlay_cache_get(dladm_handle_t handle, datalink_id_t linkid,
825     const struct ether_addr *key, dladm_overlay_point_t *point)
826 {
827 	int ret;
828 	uint_t dest, mode;
829 	uint64_t varpdid;
830 	varpd_client_handle_t *chdl;
831 	varpd_client_cache_entry_t entry;
832 
833 	if ((ret = libvarpd_c_create(&chdl, dladm_overlay_doorpath)) != 0)
834 		return (dladm_errno2status(ret));
835 
836 	if ((ret = libvarpd_c_instance_lookup(chdl, linkid, &varpdid)) != 0) {
837 		libvarpd_c_destroy(chdl);
838 		return (dladm_errno2status(ret));
839 	}
840 
841 	if ((ret = libvarpd_c_instance_target_mode(chdl, varpdid,
842 	    &dest, &mode)) != 0) {
843 		libvarpd_c_destroy(chdl);
844 		return (dladm_errno2status(ret));
845 	}
846 
847 	ret = libvarpd_c_instance_cache_get(chdl, varpdid, key, &entry);
848 	if (ret == 0) {
849 		point->dop_dest = dest;
850 		point->dop_mac = entry.vcp_mac;
851 		point->dop_flags = entry.vcp_flags;
852 		point->dop_ip = entry.vcp_ip;
853 		point->dop_port = entry.vcp_port;
854 		if (mode == OVERLAY_TARGET_POINT)
855 			point->dop_flags |= DLADM_OVERLAY_F_DEFAULT;
856 	}
857 
858 	libvarpd_c_destroy(chdl);
859 	return (dladm_errno2status(ret));
860 }
861 
862 dladm_status_t
863 dladm_overlay_status(dladm_handle_t handle, datalink_id_t linkid,
864     dladm_overlay_status_f func, void *arg)
865 {
866 	int ret;
867 	dladm_status_t status;
868 	overlay_ioc_status_t ois;
869 	dladm_overlay_status_t dos;
870 
871 	ois.ois_linkid = linkid;
872 	status = DLADM_STATUS_OK;
873 	ret = ioctl(dladm_dld_fd(handle), OVERLAY_IOC_STATUS, &ois);
874 	if (ret != 0)
875 		status = dladm_errno2status(errno);
876 	if (status != DLADM_STATUS_OK)
877 		return (status);
878 
879 	dos.dos_degraded = ois.ois_status == OVERLAY_I_DEGRADED ? B_TRUE :
880 	    B_FALSE;
881 	(void) strlcpy(dos.dos_fmamsg, ois.ois_message,
882 	    sizeof (dos.dos_fmamsg));
883 	func(handle, linkid, &dos, arg);
884 	return (DLADM_STATUS_OK);
885 }
886