xref: /titanic_50/usr/src/lib/libdladm/common/libdladm.c (revision d51e90740114c60620c0febffd4d3ce6e280a107)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <unistd.h>
29 #include <stropts.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <strings.h>
33 #include <dirent.h>
34 #include <net/if.h>
35 #include <sys/stat.h>
36 #include <sys/dld.h>
37 #include <libdlpi.h>
38 #include <libdevinfo.h>
39 #include <libdladm_impl.h>
40 #include <libintl.h>
41 
42 typedef struct dladm_dev {
43 	char			dd_name[IFNAMSIZ];
44 	struct dladm_dev	*dd_next;
45 } dladm_dev_t;
46 
47 typedef struct dladm_walk {
48 	dladm_dev_t		*dw_dev_list;
49 } dladm_walk_t;
50 
51 static char		dladm_rootdir[MAXPATHLEN] = "/";
52 
53 /*
54  * Issue an ioctl to the specified file descriptor attached to the
55  * DLD control driver interface.
56  */
57 int
58 i_dladm_ioctl(int fd, int ic_cmd, void *ic_dp, int ic_len)
59 {
60 	struct strioctl	iocb;
61 
62 	iocb.ic_cmd = ic_cmd;
63 	iocb.ic_timout = 0;
64 	iocb.ic_len = ic_len;
65 	iocb.ic_dp = (char *)ic_dp;
66 
67 	return (ioctl(fd, I_STR, &iocb));
68 }
69 
70 /*
71  * Return the attributes of the specified datalink from the DLD driver.
72  */
73 static int
74 i_dladm_info(int fd, const char *name, dladm_attr_t *dap)
75 {
76 	dld_ioc_attr_t	dia;
77 
78 	if (strlen(name) >= IFNAMSIZ) {
79 		errno = EINVAL;
80 		return (-1);
81 	}
82 
83 	(void) strlcpy(dia.dia_name, name, IFNAMSIZ);
84 
85 	if (i_dladm_ioctl(fd, DLDIOCATTR, &dia, sizeof (dia)) < 0)
86 		return (-1);
87 
88 	(void) strlcpy(dap->da_dev, dia.dia_dev, MAXNAMELEN);
89 	dap->da_max_sdu = dia.dia_max_sdu;
90 	dap->da_vid = dia.dia_vid;
91 
92 	return (0);
93 }
94 
95 /*
96  * Adds a datalink to the array corresponding to arg.
97  */
98 static void
99 i_dladm_nt_net_add(void *arg, char *name)
100 {
101 	dladm_walk_t	*dwp = arg;
102 	dladm_dev_t	*ddp = dwp->dw_dev_list;
103 	dladm_dev_t	**lastp = &dwp->dw_dev_list;
104 
105 	while (ddp) {
106 		/*
107 		 * Skip duplicates.
108 		 */
109 		if (strcmp(ddp->dd_name, name) == 0)
110 			return;
111 
112 		lastp = &ddp->dd_next;
113 		ddp = ddp->dd_next;
114 	}
115 
116 	if ((ddp = malloc(sizeof (*ddp))) == NULL)
117 		return;
118 
119 	(void) strlcpy(ddp->dd_name, name, IFNAMSIZ);
120 	ddp->dd_next = NULL;
121 	*lastp = ddp;
122 }
123 
124 /*
125  * Walker callback invoked for each DDI_NT_NET node.
126  */
127 static int
128 i_dladm_nt_net_walk(di_node_t node, di_minor_t minor, void *arg)
129 {
130 	dl_info_ack_t	dlia;
131 	char		name[IFNAMSIZ];
132 	int		fd;
133 	char		*provider;
134 	uint_t		ppa;
135 
136 	provider = di_minor_name(minor);
137 
138 	if ((fd = dlpi_open(provider)) < 0)
139 		return (DI_WALK_CONTINUE);
140 
141 	if (dlpi_info(fd, -1, &dlia, NULL, NULL, NULL, NULL, NULL, NULL) < 0) {
142 		(void) dlpi_close(fd);
143 		return (DI_WALK_CONTINUE);
144 	}
145 
146 	if (dlia.dl_provider_style == DL_STYLE1) {
147 		i_dladm_nt_net_add(arg, provider);
148 		(void) dlpi_close(fd);
149 		return (DI_WALK_CONTINUE);
150 	}
151 
152 	ppa = di_instance(node);
153 
154 	if (dlpi_attach(fd, -1, ppa) < 0) {
155 		(void) dlpi_close(fd);
156 		return (DI_WALK_CONTINUE);
157 	}
158 	(void) snprintf(name, IFNAMSIZ - 1, "%s%d", provider, ppa);
159 	i_dladm_nt_net_add(arg, name);
160 	(void) dlpi_close(fd);
161 	return (DI_WALK_CONTINUE);
162 }
163 
164 /*
165  * Invoke the specified callback function for each active DDI_NT_NET
166  * node.
167  */
168 int
169 dladm_walk(void (*fn)(void *, const char *), void *arg)
170 {
171 	di_node_t	root;
172 	dladm_walk_t	dw;
173 	dladm_dev_t	*ddp, *last_ddp;
174 
175 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
176 		errno = EFAULT;
177 		return (-1);
178 	}
179 	dw.dw_dev_list = NULL;
180 
181 	(void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, &dw,
182 	    i_dladm_nt_net_walk);
183 
184 	di_fini(root);
185 
186 	ddp = dw.dw_dev_list;
187 	while (ddp) {
188 		fn(arg, ddp->dd_name);
189 		(void) dladm_walk_vlan(fn, arg, ddp->dd_name);
190 		last_ddp = ddp;
191 		ddp = ddp->dd_next;
192 		free(last_ddp);
193 	}
194 
195 	return (0);
196 }
197 
198 /*
199  * Invoke the specified callback function for each vlan managed by dld
200  */
201 int
202 dladm_walk_vlan(void (*fn)(void *, const char *), void *arg, const char *name)
203 {
204 	int		fd, bufsize, i;
205 	int		nvlan = 4094;
206 	dld_ioc_vlan_t	*iocp = NULL;
207 	dld_vlan_info_t	*dvip;
208 
209 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
210 		return (-1);
211 
212 	bufsize = sizeof (dld_ioc_vlan_t) + nvlan * sizeof (dld_vlan_info_t);
213 
214 	if ((iocp = (dld_ioc_vlan_t *)calloc(1, bufsize)) == NULL)
215 		return (-1);
216 
217 	(void) strlcpy((char *)iocp->div_name, name, IFNAMSIZ);
218 	if (i_dladm_ioctl(fd, DLDIOCVLAN, iocp, bufsize) == 0) {
219 		dvip = (dld_vlan_info_t *)(iocp + 1);
220 		for (i = 0; i < iocp->div_count; i++)
221 			(*fn)(arg, dvip[i].dvi_name);
222 	}
223 	/*
224 	 * Note: Callers of dladm_walk_vlan() ignore the return
225 	 * value of this routine. So ignoring ioctl failure case
226 	 * and just returning 0.
227 	 */
228 	free(iocp);
229 	(void) close(fd);
230 	return (0);
231 }
232 
233 
234 /*
235  * Returns the current attributes of the specified datalink.
236  */
237 int
238 dladm_info(const char *name, dladm_attr_t *dap)
239 {
240 	int		fd;
241 
242 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
243 		return (-1);
244 
245 	if (i_dladm_info(fd, name, dap) < 0)
246 		goto failed;
247 
248 	(void) close(fd);
249 	return (0);
250 
251 failed:
252 	(void) close(fd);
253 	return (-1);
254 }
255 
256 const char *
257 dladm_status2str(dladm_status_t status, char *buf)
258 {
259 	const char	*s;
260 
261 	switch (status) {
262 	case DLADM_STATUS_OK:
263 		s = "ok";
264 		break;
265 	case DLADM_STATUS_BADARG:
266 		s = "invalid argument";
267 		break;
268 	case DLADM_STATUS_FAILED:
269 		s = "operation failed";
270 		break;
271 	case DLADM_STATUS_TOOSMALL:
272 		s = "buffer size too small";
273 		break;
274 	case DLADM_STATUS_NOTSUP:
275 		s = "operation not supported";
276 		break;
277 	case DLADM_STATUS_NOTFOUND:
278 		s = "object not found";
279 		break;
280 	case DLADM_STATUS_BADVAL:
281 		s = "invalid value";
282 		break;
283 	case DLADM_STATUS_NOMEM:
284 		s = "insufficient memory";
285 		break;
286 	case DLADM_STATUS_EXIST:
287 		s = "object already exists";
288 		break;
289 	case DLADM_STATUS_LINKINVAL:
290 		s = "invalid link";
291 		break;
292 	case DLADM_STATUS_PROPRDONLY:
293 		s = "read-only property";
294 		break;
295 	case DLADM_STATUS_BADVALCNT:
296 		s = "invalid number of values";
297 		break;
298 	case DLADM_STATUS_DBNOTFOUND:
299 		s = "database not found";
300 		break;
301 	case DLADM_STATUS_DENIED:
302 		s = "permission denied";
303 		break;
304 	case DLADM_STATUS_IOERR:
305 		s = "I/O error";
306 		break;
307 	default:
308 		s = "<unknown error>";
309 		break;
310 	}
311 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
312 	return (buf);
313 }
314 
315 /*
316  * Convert a unix errno to a dladm_status_t.
317  * We only convert errnos that are likely to be encountered. All others
318  * are mapped to DLADM_STATUS_FAILED.
319  */
320 dladm_status_t
321 dladm_errno2status(int err)
322 {
323 	switch (err) {
324 	case EINVAL:
325 		return (DLADM_STATUS_BADARG);
326 	case EEXIST:
327 		return (DLADM_STATUS_EXIST);
328 	case ENOENT:
329 		return (DLADM_STATUS_NOTFOUND);
330 	case ENOSPC:
331 		return (DLADM_STATUS_TOOSMALL);
332 	case ENOMEM:
333 		return (DLADM_STATUS_NOMEM);
334 	case ENOTSUP:
335 		return (DLADM_STATUS_NOTSUP);
336 	case EACCES:
337 		return (DLADM_STATUS_DENIED);
338 	case EIO:
339 		return (DLADM_STATUS_IOERR);
340 	default:
341 		return (DLADM_STATUS_FAILED);
342 	}
343 }
344 
345 /*
346  * These are the uid and gid of the user 'dladm'.
347  * The directory /etc/dladm and all files under it are owned by this user.
348  */
349 #define	DLADM_DB_OWNER		15
350 #define	DLADM_DB_GROUP		3
351 #define	LOCK_DB_PERMS		S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
352 
353 static int
354 i_dladm_lock_db(const char *lock_file, short type)
355 {
356 	int	lock_fd;
357 	struct  flock lock;
358 
359 	if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC,
360 	    LOCK_DB_PERMS)) < 0)
361 		return (-1);
362 
363 	lock.l_type = type;
364 	lock.l_whence = SEEK_SET;
365 	lock.l_start = 0;
366 	lock.l_len = 0;
367 
368 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
369 		int err = errno;
370 
371 		(void) close(lock_fd);
372 		(void) unlink(lock_file);
373 		errno = err;
374 		return (-1);
375 	}
376 	return (lock_fd);
377 }
378 
379 static void
380 i_dladm_unlock_db(const char *lock_file, int fd)
381 {
382 	struct flock lock;
383 
384 	if (fd < 0)
385 		return;
386 
387 	lock.l_type = F_UNLCK;
388 	lock.l_whence = SEEK_SET;
389 	lock.l_start = 0;
390 	lock.l_len = 0;
391 
392 	(void) fcntl(fd, F_SETLKW, &lock);
393 	(void) close(fd);
394 	(void) unlink(lock_file);
395 }
396 
397 dladm_status_t
398 i_dladm_rw_db(const char *db_file, mode_t db_perms,
399     dladm_status_t (*process_db)(void *, FILE *, FILE *),
400     void *arg, boolean_t writeop)
401 {
402 	dladm_status_t	status = DLADM_STATUS_OK;
403 	FILE		*fp, *nfp = NULL;
404 	char		lock[MAXPATHLEN];
405 	char		file[MAXPATHLEN];
406 	char		newfile[MAXPATHLEN];
407 	char		*db_basename;
408 	int		nfd, lock_fd;
409 
410 	/*
411 	 * If we are called from a boot script such as net-physical,
412 	 * it's quite likely that the root fs is still not writable.
413 	 * For this case, it's ok for the lock creation to fail since
414 	 * no one else could be accessing our configuration file.
415 	 */
416 	db_basename = strrchr(db_file, '/');
417 	if (db_basename == NULL || db_basename[1] == '\0')
418 		return (dladm_errno2status(EINVAL));
419 	db_basename++;
420 	(void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename);
421 	if ((lock_fd = i_dladm_lock_db
422 	    (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS)
423 		return (dladm_errno2status(errno));
424 
425 	(void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file);
426 	if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) {
427 		int	err = errno;
428 
429 		i_dladm_unlock_db(lock, lock_fd);
430 		if (err == ENOENT)
431 			return (DLADM_STATUS_DBNOTFOUND);
432 
433 		return (dladm_errno2status(err));
434 	}
435 
436 	if (writeop) {
437 		(void) snprintf(newfile, MAXPATHLEN, "%s/%s.new",
438 		    dladm_rootdir, db_file);
439 		if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
440 		    db_perms)) < 0) {
441 			(void) fclose(fp);
442 			i_dladm_unlock_db(lock, lock_fd);
443 			return (dladm_errno2status(errno));
444 		}
445 
446 		if ((nfp = fdopen(nfd, "w")) == NULL) {
447 			(void) close(nfd);
448 			(void) fclose(fp);
449 			(void) unlink(newfile);
450 			i_dladm_unlock_db(lock, lock_fd);
451 			return (dladm_errno2status(errno));
452 		}
453 	}
454 	status = (*process_db)(arg, fp, nfp);
455 	if (!writeop || status != DLADM_STATUS_OK)
456 		goto done;
457 
458 	/*
459 	 * Configuration files need to be owned by the 'dladm' user.
460 	 * If we are invoked by root, the file ownership needs to be fixed.
461 	 */
462 	if (getuid() == 0 || geteuid() == 0) {
463 		if (fchown(nfd, DLADM_DB_OWNER, DLADM_DB_GROUP) < 0) {
464 			status = dladm_errno2status(errno);
465 			goto done;
466 		}
467 	}
468 
469 	if (fflush(nfp) == EOF) {
470 		status = dladm_errno2status(errno);
471 		goto done;
472 	}
473 	(void) fclose(fp);
474 	(void) fclose(nfp);
475 
476 	if (rename(newfile, file) < 0) {
477 		(void) unlink(newfile);
478 		i_dladm_unlock_db(lock, lock_fd);
479 		return (dladm_errno2status(errno));
480 	}
481 
482 	i_dladm_unlock_db(lock, lock_fd);
483 	return (DLADM_STATUS_OK);
484 
485 done:
486 	if (nfp != NULL) {
487 		(void) fclose(nfp);
488 		if (status != DLADM_STATUS_OK)
489 			(void) unlink(newfile);
490 	}
491 	(void) fclose(fp);
492 	i_dladm_unlock_db(lock, lock_fd);
493 	return (status);
494 }
495 
496 dladm_status_t
497 dladm_set_rootdir(const char *rootdir)
498 {
499 	DIR	*dp;
500 
501 	if (rootdir == NULL || *rootdir != '/' ||
502 	    (dp = opendir(rootdir)) == NULL)
503 		return (DLADM_STATUS_BADARG);
504 
505 	(void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN);
506 	(void) closedir(dp);
507 	return (DLADM_STATUS_OK);
508 }
509