Print this page
OS-4335 ipadm_door_call should work in a branded zone without chroot
OS-4336 ipmgmtd should work in a branded zone without chroot
Reviewed by: Robert Mustacchi <rm@joyent.com>
OS-2837 lx brand only works with shared IP stacks
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libipadm/common/libipadm.c
+++ new/usr/src/lib/libipadm/common/libipadm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2015 Joyent, Inc.
24 25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 26 */
26 27
27 28 #include <stdio.h>
28 29 #include <stdlib.h>
29 30 #include <string.h>
30 31 #include <errno.h>
31 32 #include <fcntl.h>
32 33 #include <unistd.h>
33 34 #include <stropts.h>
34 35 #include <sys/sockio.h>
35 36 #include <sys/types.h>
36 37 #include <sys/stat.h>
37 38 #include <sys/socket.h>
38 39 #include <net/route.h>
39 40 #include <netinet/in.h>
40 41 #include <inet/ip.h>
41 42 #include <arpa/inet.h>
42 43 #include <libintl.h>
43 44 #include <libdlpi.h>
44 45 #include <libinetutil.h>
45 46 #include <libdladm.h>
46 47 #include <libdllink.h>
47 48 #include <libdliptun.h>
48 49 #include <strings.h>
49 50 #include <zone.h>
50 51 #include <ctype.h>
51 52 #include <limits.h>
52 53 #include <assert.h>
53 54 #include <netdb.h>
54 55 #include <pwd.h>
55 56 #include <auth_attr.h>
56 57 #include <secdb.h>
57 58 #include <nss_dbdefs.h>
58 59 #include "libipadm_impl.h"
59 60
60 61 /* error codes and text description */
61 62 static struct ipadm_error_info {
62 63 ipadm_status_t error_code;
63 64 const char *error_desc;
64 65 } ipadm_errors[] = {
65 66 { IPADM_SUCCESS, "Operation succeeded" },
66 67 { IPADM_FAILURE, "Operation failed" },
67 68 { IPADM_EAUTH, "Insufficient user authorizations" },
68 69 { IPADM_EPERM, "Permission denied" },
69 70 { IPADM_NO_BUFS, "No buffer space available" },
70 71 { IPADM_NO_MEMORY, "Insufficient memory" },
71 72 { IPADM_BAD_ADDR, "Invalid address" },
72 73 { IPADM_BAD_PROTOCOL, "Incorrect protocol family for operation" },
73 74 { IPADM_DAD_FOUND, "Duplicate address detected" },
74 75 { IPADM_EXISTS, "Already exists" },
75 76 { IPADM_IF_EXISTS, "Interface already exists" },
76 77 { IPADM_ADDROBJ_EXISTS, "Address object already exists" },
77 78 { IPADM_ADDRCONF_EXISTS, "Addrconf already in progress" },
78 79 { IPADM_ENXIO, "Interface does not exist" },
79 80 { IPADM_GRP_NOTEMPTY, "IPMP group is not empty" },
80 81 { IPADM_INVALID_ARG, "Invalid argument provided" },
81 82 { IPADM_INVALID_NAME, "Invalid name" },
82 83 { IPADM_DLPI_FAILURE, "Could not open DLPI link" },
83 84 { IPADM_DLADM_FAILURE, "Datalink does not exist" },
84 85 { IPADM_PROP_UNKNOWN, "Unknown property" },
85 86 { IPADM_ERANGE, "Value is outside the allowed range" },
86 87 { IPADM_ESRCH, "Value does not exist" },
87 88 { IPADM_EOVERFLOW, "Number of values exceeds the allowed limit" },
88 89 { IPADM_NOTFOUND, "Object not found" },
89 90 { IPADM_IF_INUSE, "Interface already in use" },
90 91 { IPADM_ADDR_INUSE, "Address already in use" },
91 92 { IPADM_BAD_HOSTNAME, "Hostname maps to multiple IP addresses" },
92 93 { IPADM_ADDR_NOTAVAIL, "Can't assign requested address" },
93 94 { IPADM_ALL_ADDRS_NOT_ENABLED, "All addresses could not be enabled" },
94 95 { IPADM_NDPD_NOT_RUNNING, "IPv6 autoconf daemon in.ndpd not running" },
95 96 { IPADM_DHCP_START_ERROR, "Could not start dhcpagent" },
96 97 { IPADM_DHCP_IPC_ERROR, "Could not communicate with dhcpagent" },
97 98 { IPADM_DHCP_IPC_TIMEOUT, "Communication with dhcpagent timed out" },
98 99 { IPADM_TEMPORARY_OBJ, "Persistent operation on temporary object" },
99 100 { IPADM_IPC_ERROR, "Could not communicate with ipmgmtd" },
100 101 { IPADM_NOTSUP, "Operation not supported" },
101 102 { IPADM_OP_DISABLE_OBJ, "Operation not supported on disabled object" },
102 103 { IPADM_EBADE, "Invalid data exchange with daemon" },
103 104 { IPADM_GZ_PERM, "Operation not permitted on from-gz interface"}
104 105 };
105 106
106 107 #define IPADM_NUM_ERRORS (sizeof (ipadm_errors) / sizeof (*ipadm_errors))
107 108
108 109 ipadm_status_t
109 110 ipadm_errno2status(int error)
110 111 {
111 112 switch (error) {
112 113 case 0:
113 114 return (IPADM_SUCCESS);
114 115 case ENXIO:
115 116 return (IPADM_ENXIO);
116 117 case ENOMEM:
117 118 return (IPADM_NO_MEMORY);
118 119 case ENOBUFS:
119 120 return (IPADM_NO_BUFS);
120 121 case EINVAL:
121 122 return (IPADM_INVALID_ARG);
122 123 case EBUSY:
123 124 return (IPADM_IF_INUSE);
124 125 case EEXIST:
125 126 return (IPADM_EXISTS);
126 127 case EADDRNOTAVAIL:
127 128 return (IPADM_ADDR_NOTAVAIL);
128 129 case EADDRINUSE:
129 130 return (IPADM_ADDR_INUSE);
130 131 case ENOENT:
131 132 return (IPADM_NOTFOUND);
132 133 case ERANGE:
133 134 return (IPADM_ERANGE);
134 135 case EPERM:
135 136 return (IPADM_EPERM);
136 137 case ENOTSUP:
137 138 case EOPNOTSUPP:
138 139 return (IPADM_NOTSUP);
139 140 case EBADF:
140 141 return (IPADM_IPC_ERROR);
141 142 case EBADE:
142 143 return (IPADM_EBADE);
143 144 case ESRCH:
144 145 return (IPADM_ESRCH);
145 146 case EOVERFLOW:
146 147 return (IPADM_EOVERFLOW);
147 148 default:
148 149 return (IPADM_FAILURE);
149 150 }
150 151 }
151 152
152 153 /*
153 154 * Returns a message string for the given libipadm error status.
154 155 */
155 156 const char *
156 157 ipadm_status2str(ipadm_status_t status)
157 158 {
158 159 int i;
159 160
160 161 for (i = 0; i < IPADM_NUM_ERRORS; i++) {
161 162 if (status == ipadm_errors[i].error_code)
162 163 return (dgettext(TEXT_DOMAIN,
163 164 ipadm_errors[i].error_desc));
164 165 }
165 166
166 167 return (dgettext(TEXT_DOMAIN, "<unknown error>"));
167 168 }
168 169
169 170 /*
170 171 * Opens a handle to libipadm.
171 172 * Possible values for flags:
172 173 * IPH_VRRP: Used by VRRP daemon to set the socket option SO_VRRP.
173 174 * IPH_LEGACY: This is used whenever an application needs to provide a
174 175 * logical interface name while creating or deleting
175 176 * interfaces and static addresses.
176 177 * IPH_INIT: Used by ipadm_init_prop(), to initialize protocol properties
177 178 * on reboot.
178 179 */
179 180 ipadm_status_t
180 181 ipadm_open(ipadm_handle_t *handle, uint32_t flags)
181 182 {
182 183 ipadm_handle_t iph;
183 184 ipadm_status_t status = IPADM_SUCCESS;
184 185 zoneid_t zoneid;
185 186 ushort_t zflags;
186 187 int on = B_TRUE;
187 188
188 189 if (handle == NULL)
189 190 return (IPADM_INVALID_ARG);
190 191 *handle = NULL;
191 192
192 193 if (flags & ~(IPH_VRRP|IPH_LEGACY|IPH_INIT|IPH_IPMGMTD))
193 194 return (IPADM_INVALID_ARG);
194 195
195 196 if ((iph = calloc(1, sizeof (struct ipadm_handle))) == NULL)
196 197 return (IPADM_NO_MEMORY);
197 198 iph->iph_sock = -1;
198 199 iph->iph_sock6 = -1;
199 200 iph->iph_door_fd = -1;
200 201 iph->iph_rtsock = -1;
201 202 iph->iph_flags = flags;
202 203 (void) pthread_mutex_init(&iph->iph_lock, NULL);
203 204
204 205 if ((iph->iph_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
205 206 (iph->iph_sock6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
206 207 goto errnofail;
207 208 }
208 209
209 210 /*
210 211 * We open a handle to libdladm here, to facilitate some daemons (like
211 212 * nwamd) which opens handle to libipadm before devfsadmd installs the
212 213 * right device permissions into the kernel and requires "all"
213 214 * privileges to open DLD_CONTROL_DEV.
214 215 *
215 216 * In a non-global shared-ip zone there will be no DLD_CONTROL_DEV node
216 217 * and dladm_open() will fail. So, we avoid this by not calling
217 218 * dladm_open() for such zones.
218 219 */
219 220 zoneid = getzoneid();
220 221 iph->iph_zoneid = zoneid;
221 222 if (zoneid != GLOBAL_ZONEID) {
222 223 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &zflags,
223 224 sizeof (zflags)) < 0) {
224 225 goto errnofail;
225 226 }
226 227 }
227 228 if ((zoneid == GLOBAL_ZONEID) || (zflags & ZF_NET_EXCL)) {
228 229 if (dladm_open(&iph->iph_dlh) != DLADM_STATUS_OK) {
229 230 ipadm_close(iph);
230 231 return (IPADM_DLADM_FAILURE);
231 232 }
232 233 if (zoneid != GLOBAL_ZONEID) {
233 234 iph->iph_rtsock = socket(PF_ROUTE, SOCK_RAW, 0);
234 235 /*
235 236 * Failure to open rtsock is ignored as this is
236 237 * only used in non-global zones to initialize
237 238 * routing socket information.
238 239 */
239 240 }
240 241 } else {
241 242 assert(zoneid != GLOBAL_ZONEID);
242 243 iph->iph_dlh = NULL;
243 244 }
244 245 if (flags & IPH_VRRP) {
245 246 if (setsockopt(iph->iph_sock6, SOL_SOCKET, SO_VRRP, &on,
246 247 sizeof (on)) < 0 || setsockopt(iph->iph_sock, SOL_SOCKET,
247 248 SO_VRRP, &on, sizeof (on)) < 0) {
248 249 goto errnofail;
249 250 }
250 251 }
251 252 *handle = iph;
252 253 return (status);
253 254
254 255 errnofail:
255 256 status = ipadm_errno2status(errno);
256 257 ipadm_close(iph);
257 258 return (status);
258 259 }
259 260
260 261 /*
261 262 * Closes and frees the libipadm handle.
262 263 */
263 264 void
264 265 ipadm_close(ipadm_handle_t iph)
265 266 {
266 267 if (iph == NULL)
267 268 return;
268 269 if (iph->iph_sock != -1)
269 270 (void) close(iph->iph_sock);
270 271 if (iph->iph_sock6 != -1)
271 272 (void) close(iph->iph_sock6);
272 273 if (iph->iph_rtsock != -1)
273 274 (void) close(iph->iph_rtsock);
274 275 if (iph->iph_door_fd != -1)
275 276 (void) close(iph->iph_door_fd);
276 277 dladm_close(iph->iph_dlh);
277 278 (void) pthread_mutex_destroy(&iph->iph_lock);
|
↓ open down ↓ |
244 lines elided |
↑ open up ↑ |
278 279 free(iph);
279 280 }
280 281
281 282 /*
282 283 * Checks if the caller has the authorization to configure network
283 284 * interfaces.
284 285 */
285 286 boolean_t
286 287 ipadm_check_auth(void)
287 288 {
289 + int uid;
288 290 struct passwd pwd;
289 291 char buf[NSS_BUFLEN_PASSWD];
290 292
293 + /*
294 + * Branded zones may have different kinds of auth, but root always
295 + * allowed.
296 + */
297 + if ((uid = getuid()) == 0)
298 + return (B_TRUE);
299 +
291 300 /* get the password entry for the given user ID */
292 - if (getpwuid_r(getuid(), &pwd, buf, sizeof (buf)) == NULL)
301 + if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) == NULL)
293 302 return (B_FALSE);
294 303
295 304 /* check for presence of given authorization */
296 305 return (chkauthattr(NETWORK_INTERFACE_CONFIG_AUTH, pwd.pw_name) != 0);
297 306 }
298 307
299 308 /*
300 309 * Stores the index value of the interface in `ifname' for the address
301 310 * family `af' into the buffer pointed to by `index'.
302 311 */
303 312 static ipadm_status_t
304 313 i_ipadm_get_index(ipadm_handle_t iph, const char *ifname, sa_family_t af,
305 314 int *index)
306 315 {
307 316 struct lifreq lifr;
308 317 int sock;
309 318
310 319 bzero(&lifr, sizeof (lifr));
311 320 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
312 321 if (af == AF_INET)
313 322 sock = iph->iph_sock;
314 323 else
315 324 sock = iph->iph_sock6;
316 325
317 326 if (ioctl(sock, SIOCGLIFINDEX, (caddr_t)&lifr) < 0)
318 327 return (ipadm_errno2status(errno));
319 328 *index = lifr.lifr_index;
320 329
321 330 return (IPADM_SUCCESS);
322 331 }
323 332
324 333 /*
325 334 * Maximum amount of time (in milliseconds) to wait for Duplicate Address
326 335 * Detection to complete in the kernel.
327 336 */
328 337 #define DAD_WAIT_TIME 1000
329 338
330 339 /*
331 340 * Any time that flags are changed on an interface where either the new or the
332 341 * existing flags have IFF_UP set, we'll get a RTM_NEWADDR message to
333 342 * announce the new address added and its flag status.
334 343 * We wait here for that message and look for IFF_UP.
335 344 * If something's amiss with the kernel, though, we don't wait forever.
336 345 * (Note that IFF_DUPLICATE is a high-order bit, and we cannot see
337 346 * it in the routing socket messages.)
338 347 */
339 348 static ipadm_status_t
340 349 i_ipadm_dad_wait(ipadm_handle_t handle, const char *lifname, sa_family_t af,
341 350 int rtsock)
342 351 {
343 352 struct pollfd fds[1];
344 353 union {
345 354 struct if_msghdr ifm;
346 355 char buf[1024];
347 356 } msg;
348 357 int index;
349 358 ipadm_status_t retv;
350 359 uint64_t flags;
351 360 hrtime_t starttime, now;
352 361
353 362 fds[0].fd = rtsock;
354 363 fds[0].events = POLLIN;
355 364 fds[0].revents = 0;
356 365
357 366 retv = i_ipadm_get_index(handle, lifname, af, &index);
358 367 if (retv != IPADM_SUCCESS)
359 368 return (retv);
360 369
361 370 starttime = gethrtime();
362 371 for (;;) {
363 372 now = gethrtime();
364 373 now = (now - starttime) / 1000000;
365 374 if (now >= DAD_WAIT_TIME)
366 375 break;
367 376 if (poll(fds, 1, DAD_WAIT_TIME - (int)now) <= 0)
368 377 break;
369 378 if (read(rtsock, &msg, sizeof (msg)) <= 0)
370 379 break;
371 380 if (msg.ifm.ifm_type != RTM_NEWADDR)
372 381 continue;
373 382 /* Note that ifm_index is just 16 bits */
374 383 if (index == msg.ifm.ifm_index && (msg.ifm.ifm_flags & IFF_UP))
375 384 return (IPADM_SUCCESS);
376 385 }
377 386
378 387 retv = i_ipadm_get_flags(handle, lifname, af, &flags);
379 388 if (retv != IPADM_SUCCESS)
380 389 return (retv);
381 390 if (flags & IFF_DUPLICATE)
382 391 return (IPADM_DAD_FOUND);
383 392
384 393 return (IPADM_SUCCESS);
385 394 }
386 395
387 396 /*
388 397 * Sets the flags `on_flags' and resets the flags `off_flags' for the logical
389 398 * interface in `lifname'.
390 399 *
391 400 * If the new flags value will transition the interface from "down" to "up"
392 401 * then duplicate address detection is performed by the kernel. This routine
393 402 * waits to get the outcome of that test.
394 403 */
395 404 ipadm_status_t
396 405 i_ipadm_set_flags(ipadm_handle_t iph, const char *lifname, sa_family_t af,
397 406 uint64_t on_flags, uint64_t off_flags)
398 407 {
399 408 struct lifreq lifr;
400 409 uint64_t oflags;
401 410 ipadm_status_t ret;
402 411 int rtsock = -1;
403 412 int sock, err;
404 413
405 414 ret = i_ipadm_get_flags(iph, lifname, af, &oflags);
406 415 if (ret != IPADM_SUCCESS)
407 416 return (ret);
408 417
409 418 sock = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
410 419
411 420 /*
412 421 * Any time flags are changed on an interface that has IFF_UP set,
413 422 * we get a routing socket message. We care about the status,
414 423 * though, only when the new flags are marked "up."
415 424 */
416 425 if (!(oflags & IFF_UP) && (on_flags & IFF_UP))
417 426 rtsock = socket(PF_ROUTE, SOCK_RAW, af);
418 427
419 428 oflags |= on_flags;
420 429 oflags &= ~off_flags;
421 430 bzero(&lifr, sizeof (lifr));
422 431 (void) strlcpy(lifr.lifr_name, lifname, sizeof (lifr.lifr_name));
423 432 lifr.lifr_flags = oflags;
424 433 if (ioctl(sock, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
425 434 err = errno;
426 435 if (rtsock != -1)
427 436 (void) close(rtsock);
428 437 return (ipadm_errno2status(err));
429 438 }
430 439 if (rtsock == -1) {
431 440 return (IPADM_SUCCESS);
432 441 } else {
433 442 /* Wait for DAD to complete. */
434 443 ret = i_ipadm_dad_wait(iph, lifname, af, rtsock);
435 444 (void) close(rtsock);
436 445 return (ret);
437 446 }
438 447 }
439 448
440 449 /*
441 450 * Returns the flags value for the logical interface in `lifname'
442 451 * in the buffer pointed to by `flags'.
443 452 */
444 453 ipadm_status_t
445 454 i_ipadm_get_flags(ipadm_handle_t iph, const char *lifname, sa_family_t af,
446 455 uint64_t *flags)
447 456 {
448 457 struct lifreq lifr;
449 458 int sock;
450 459
451 460 bzero(&lifr, sizeof (lifr));
452 461 (void) strlcpy(lifr.lifr_name, lifname, sizeof (lifr.lifr_name));
453 462 if (af == AF_INET)
454 463 sock = iph->iph_sock;
455 464 else
456 465 sock = iph->iph_sock6;
457 466
458 467 if (ioctl(sock, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
459 468 return (ipadm_errno2status(errno));
460 469 }
461 470 *flags = lifr.lifr_flags;
462 471
463 472 return (IPADM_SUCCESS);
464 473 }
465 474
466 475 /*
467 476 * Determines whether or not an interface name represents a loopback
468 477 * interface, before the interface has been plumbed.
469 478 * It is assumed that the interface name in `ifname' is of correct format
470 479 * as verified by ifparse_ifspec().
471 480 *
472 481 * Returns: B_TRUE if loopback, B_FALSE if not.
473 482 */
474 483 boolean_t
475 484 i_ipadm_is_loopback(const char *ifname)
476 485 {
477 486 int len = strlen(LOOPBACK_IF);
478 487
479 488 return (strncmp(ifname, LOOPBACK_IF, len) == 0 &&
480 489 (ifname[len] == '\0' || ifname[len] == IPADM_LOGICAL_SEP));
481 490 }
482 491
483 492 /*
484 493 * Determines whether or not an interface name represents a vni
485 494 * interface, before the interface has been plumbed.
486 495 * It is assumed that the interface name in `ifname' is of correct format
487 496 * as verified by ifparse_ifspec().
488 497 *
489 498 * Returns: B_TRUE if vni, B_FALSE if not.
490 499 */
491 500 boolean_t
492 501 i_ipadm_is_vni(const char *ifname)
493 502 {
494 503 ifspec_t ifsp;
495 504
496 505 return (ifparse_ifspec(ifname, &ifsp) &&
497 506 strcmp(ifsp.ifsp_devnm, "vni") == 0);
498 507 }
499 508
500 509 /*
501 510 * Returns B_TRUE if `ifname' is an IP interface on a 6to4 tunnel.
502 511 */
503 512 boolean_t
504 513 i_ipadm_is_6to4(ipadm_handle_t iph, char *ifname)
505 514 {
506 515 dladm_status_t dlstatus;
507 516 datalink_class_t class;
508 517 iptun_params_t params;
509 518 datalink_id_t linkid;
510 519
511 520 if (iph->iph_dlh == NULL) {
512 521 assert(iph->iph_zoneid != GLOBAL_ZONEID);
513 522 return (B_FALSE);
514 523 }
515 524 dlstatus = dladm_name2info(iph->iph_dlh, ifname, &linkid, NULL,
516 525 &class, NULL);
517 526 if (dlstatus == DLADM_STATUS_OK && class == DATALINK_CLASS_IPTUN) {
518 527 params.iptun_param_linkid = linkid;
519 528 dlstatus = dladm_iptun_getparams(iph->iph_dlh, ¶ms,
520 529 DLADM_OPT_ACTIVE);
521 530 if (dlstatus == DLADM_STATUS_OK &&
522 531 params.iptun_param_type == IPTUN_TYPE_6TO4) {
523 532 return (B_TRUE);
524 533 }
525 534 }
526 535 return (B_FALSE);
527 536 }
528 537
529 538 /*
530 539 * Returns B_TRUE if `ifname' represents an IPMP underlying interface.
531 540 */
532 541 boolean_t
533 542 i_ipadm_is_under_ipmp(ipadm_handle_t iph, const char *ifname)
534 543 {
535 544 struct lifreq lifr;
536 545
537 546 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
538 547 if (ioctl(iph->iph_sock, SIOCGLIFGROUPNAME, (caddr_t)&lifr) < 0) {
539 548 if (ioctl(iph->iph_sock6, SIOCGLIFGROUPNAME,
540 549 (caddr_t)&lifr) < 0) {
541 550 return (B_FALSE);
542 551 }
543 552 }
544 553 return (lifr.lifr_groupname[0] != '\0');
545 554 }
546 555
547 556 /*
548 557 * Returns B_TRUE if `ifname' represents an IPMP meta-interface.
549 558 */
550 559 boolean_t
551 560 i_ipadm_is_ipmp(ipadm_handle_t iph, const char *ifname)
552 561 {
553 562 uint64_t flags;
554 563
555 564 if (i_ipadm_get_flags(iph, ifname, AF_INET, &flags) != IPADM_SUCCESS &&
556 565 i_ipadm_get_flags(iph, ifname, AF_INET6, &flags) != IPADM_SUCCESS)
557 566 return (B_FALSE);
558 567
559 568 return ((flags & IFF_IPMP) != 0);
560 569 }
561 570
562 571 /*
563 572 * For a given interface name, ipadm_if_enabled() checks if v4
564 573 * or v6 or both IP interfaces exist in the active configuration.
565 574 */
566 575 boolean_t
567 576 ipadm_if_enabled(ipadm_handle_t iph, const char *ifname, sa_family_t af)
568 577 {
569 578 struct lifreq lifr;
570 579 int s4 = iph->iph_sock;
571 580 int s6 = iph->iph_sock6;
572 581
573 582 bzero(&lifr, sizeof (lifr));
574 583 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
575 584 switch (af) {
576 585 case AF_INET:
577 586 if (ioctl(s4, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0)
578 587 return (B_TRUE);
579 588 break;
580 589 case AF_INET6:
581 590 if (ioctl(s6, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0)
582 591 return (B_TRUE);
583 592 break;
584 593 case AF_UNSPEC:
585 594 if (ioctl(s4, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0 ||
586 595 ioctl(s6, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0) {
587 596 return (B_TRUE);
588 597 }
589 598 }
590 599 return (B_FALSE);
591 600 }
592 601
593 602 /*
594 603 * Apply the interface property by retrieving information from nvl.
595 604 */
596 605 static ipadm_status_t
597 606 i_ipadm_init_ifprop(ipadm_handle_t iph, nvlist_t *nvl)
598 607 {
599 608 nvpair_t *nvp;
600 609 char *name, *pname = NULL;
601 610 char *protostr = NULL, *ifname = NULL, *pval = NULL;
602 611 uint_t proto;
603 612 int err = 0;
604 613
605 614 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
606 615 nvp = nvlist_next_nvpair(nvl, nvp)) {
607 616 name = nvpair_name(nvp);
608 617 if (strcmp(name, IPADM_NVP_IFNAME) == 0) {
609 618 if ((err = nvpair_value_string(nvp, &ifname)) != 0)
610 619 break;
611 620 } else if (strcmp(name, IPADM_NVP_PROTONAME) == 0) {
612 621 if ((err = nvpair_value_string(nvp, &protostr)) != 0)
613 622 break;
614 623 } else {
615 624 assert(!IPADM_PRIV_NVP(name));
616 625 pname = name;
617 626 if ((err = nvpair_value_string(nvp, &pval)) != 0)
618 627 break;
619 628 }
620 629 }
621 630 if (err != 0)
622 631 return (ipadm_errno2status(err));
623 632 proto = ipadm_str2proto(protostr);
624 633 return (ipadm_set_ifprop(iph, ifname, pname, pval, proto,
625 634 IPADM_OPT_ACTIVE));
626 635 }
627 636
628 637 /*
629 638 * Instantiate the address object or set the address object property by
630 639 * retrieving the configuration from the nvlist `nvl'.
631 640 */
632 641 ipadm_status_t
633 642 i_ipadm_init_addrobj(ipadm_handle_t iph, nvlist_t *nvl)
634 643 {
635 644 nvpair_t *nvp;
636 645 char *name;
637 646 char *aobjname = NULL, *pval = NULL, *ifname = NULL;
638 647 sa_family_t af = AF_UNSPEC;
639 648 ipadm_addr_type_t atype = IPADM_ADDR_NONE;
640 649 int err = 0;
641 650 ipadm_status_t status = IPADM_SUCCESS;
642 651
643 652 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
644 653 nvp = nvlist_next_nvpair(nvl, nvp)) {
645 654 name = nvpair_name(nvp);
646 655 if (strcmp(name, IPADM_NVP_IFNAME) == 0) {
647 656 if ((err = nvpair_value_string(nvp, &ifname)) != 0)
648 657 break;
649 658 } else if (strcmp(name, IPADM_NVP_AOBJNAME) == 0) {
650 659 if ((err = nvpair_value_string(nvp, &aobjname)) != 0)
651 660 break;
652 661 } else if (i_ipadm_name2atype(name, &af, &atype)) {
653 662 break;
654 663 } else {
655 664 assert(!IPADM_PRIV_NVP(name));
656 665 err = nvpair_value_string(nvp, &pval);
657 666 break;
658 667 }
659 668 }
660 669 if (err != 0)
661 670 return (ipadm_errno2status(err));
662 671
663 672 switch (atype) {
664 673 case IPADM_ADDR_STATIC:
665 674 status = i_ipadm_enable_static(iph, ifname, nvl, af);
666 675 break;
667 676 case IPADM_ADDR_DHCP:
668 677 status = i_ipadm_enable_dhcp(iph, ifname, nvl);
669 678 if (status == IPADM_DHCP_IPC_TIMEOUT)
670 679 status = IPADM_SUCCESS;
671 680 break;
672 681 case IPADM_ADDR_IPV6_ADDRCONF:
673 682 status = i_ipadm_enable_addrconf(iph, ifname, nvl);
674 683 break;
675 684 case IPADM_ADDR_NONE:
676 685 status = ipadm_set_addrprop(iph, name, pval, aobjname,
677 686 IPADM_OPT_ACTIVE);
678 687 break;
679 688 }
680 689
681 690 return (status);
682 691 }
683 692
684 693 /*
685 694 * Instantiate the interface object by retrieving the configuration from
686 695 * `ifnvl'. The nvlist `ifnvl' contains all the persistent configuration
687 696 * (interface properties and address objects on that interface) for the
688 697 * given `ifname'.
689 698 */
690 699 ipadm_status_t
691 700 i_ipadm_init_ifobj(ipadm_handle_t iph, const char *ifname, nvlist_t *ifnvl)
692 701 {
693 702 nvlist_t *nvl = NULL;
694 703 nvpair_t *nvp;
695 704 char *afstr;
696 705 ipadm_status_t status;
697 706 ipadm_status_t ret_status = IPADM_SUCCESS;
698 707 char newifname[LIFNAMSIZ];
699 708 char *aobjstr;
700 709 sa_family_t af = AF_UNSPEC;
701 710 boolean_t is_ngz = (iph->iph_zoneid != GLOBAL_ZONEID);
702 711
703 712 (void) strlcpy(newifname, ifname, sizeof (newifname));
704 713 /*
705 714 * First plumb the given interface and then apply all the persistent
706 715 * interface properties and then instantiate any persistent addresses
707 716 * objects on that interface.
708 717 */
709 718 for (nvp = nvlist_next_nvpair(ifnvl, NULL); nvp != NULL;
710 719 nvp = nvlist_next_nvpair(ifnvl, nvp)) {
711 720 if (nvpair_value_nvlist(nvp, &nvl) != 0)
712 721 continue;
713 722
714 723 if (nvlist_lookup_string(nvl, IPADM_NVP_FAMILY, &afstr) == 0) {
715 724 status = i_ipadm_plumb_if(iph, newifname, atoi(afstr),
716 725 IPADM_OPT_ACTIVE);
717 726 /*
718 727 * If the interface is already plumbed, we should
719 728 * ignore this error because there might be address
720 729 * address objects on that interface that needs to
721 730 * be enabled again.
722 731 */
723 732 if (status == IPADM_IF_EXISTS)
724 733 status = IPADM_SUCCESS;
725 734
726 735 if (is_ngz)
727 736 af = atoi(afstr);
728 737 } else if (nvlist_lookup_string(nvl, IPADM_NVP_AOBJNAME,
729 738 &aobjstr) == 0) {
730 739 /*
731 740 * For a static address, we need to search for
732 741 * the prefixlen in the nvlist `ifnvl'.
733 742 */
734 743 if (nvlist_exists(nvl, IPADM_NVP_IPV4ADDR) ||
735 744 nvlist_exists(nvl, IPADM_NVP_IPV6ADDR)) {
736 745 status = i_ipadm_merge_prefixlen_from_nvl(ifnvl,
737 746 nvl, aobjstr);
738 747 if (status != IPADM_SUCCESS)
739 748 continue;
740 749 }
741 750 status = i_ipadm_init_addrobj(iph, nvl);
742 751 /*
743 752 * If this address is in use on some other interface,
744 753 * we want to record an error to be returned as
745 754 * a soft error and continue processing the rest of
746 755 * the addresses.
747 756 */
748 757 if (status == IPADM_ADDR_NOTAVAIL) {
749 758 ret_status = IPADM_ALL_ADDRS_NOT_ENABLED;
750 759 status = IPADM_SUCCESS;
751 760 }
752 761 } else {
753 762 assert(nvlist_exists(nvl, IPADM_NVP_PROTONAME));
754 763 status = i_ipadm_init_ifprop(iph, nvl);
755 764 }
756 765 if (status != IPADM_SUCCESS)
757 766 return (status);
758 767 }
759 768
760 769 if (is_ngz && af != AF_UNSPEC)
761 770 ret_status = ipadm_init_net_from_gz(iph, newifname, NULL);
762 771 return (ret_status);
763 772 }
764 773
765 774 /*
766 775 * Retrieves the persistent configuration for the given interface(s) in `ifs'
767 776 * by contacting the daemon and dumps the information in `allifs'.
768 777 */
769 778 ipadm_status_t
770 779 i_ipadm_init_ifs(ipadm_handle_t iph, const char *ifs, nvlist_t **allifs)
771 780 {
772 781 nvlist_t *nvl = NULL;
773 782 size_t nvlsize, bufsize;
774 783 ipmgmt_initif_arg_t *iargp;
775 784 char *buf = NULL, *nvlbuf = NULL;
776 785 ipmgmt_get_rval_t *rvalp = NULL;
777 786 int err;
778 787 ipadm_status_t status = IPADM_SUCCESS;
779 788
780 789 if ((err = ipadm_str2nvlist(ifs, &nvl, IPADM_NORVAL)) != 0)
781 790 return (ipadm_errno2status(err));
782 791
783 792 err = nvlist_pack(nvl, &nvlbuf, &nvlsize, NV_ENCODE_NATIVE, 0);
784 793 if (err != 0) {
785 794 status = ipadm_errno2status(err);
786 795 goto done;
787 796 }
788 797 bufsize = sizeof (*iargp) + nvlsize;
789 798 if ((buf = malloc(bufsize)) == NULL) {
790 799 status = ipadm_errno2status(errno);
791 800 goto done;
792 801 }
793 802
794 803 /* populate the door_call argument structure */
795 804 iargp = (void *)buf;
796 805 iargp->ia_cmd = IPMGMT_CMD_INITIF;
797 806 iargp->ia_flags = 0;
798 807 iargp->ia_family = AF_UNSPEC;
799 808 iargp->ia_nvlsize = nvlsize;
800 809 (void) bcopy(nvlbuf, buf + sizeof (*iargp), nvlsize);
801 810
802 811 if ((rvalp = malloc(sizeof (ipmgmt_get_rval_t))) == NULL) {
803 812 status = ipadm_errno2status(errno);
804 813 goto done;
805 814 }
806 815 if ((err = ipadm_door_call(iph, iargp, bufsize, (void **)&rvalp,
807 816 sizeof (*rvalp), B_TRUE)) != 0) {
808 817 status = ipadm_errno2status(err);
809 818 goto done;
810 819 }
811 820
812 821 /*
813 822 * Daemon reply pointed to by rvalp contains ipmgmt_get_rval_t structure
814 823 * followed by a list of packed nvlists, each of which represents
815 824 * configuration information for the given interface(s).
816 825 */
817 826 err = nvlist_unpack((char *)rvalp + sizeof (ipmgmt_get_rval_t),
818 827 rvalp->ir_nvlsize, allifs, NV_ENCODE_NATIVE);
819 828 if (err != 0)
820 829 status = ipadm_errno2status(err);
821 830 done:
822 831 nvlist_free(nvl);
823 832 free(buf);
824 833 free(nvlbuf);
825 834 free(rvalp);
826 835 return (status);
827 836 }
828 837
829 838 /*
830 839 * Returns B_FALSE if
831 840 * (1) `ifname' is NULL or has no string or has a string of invalid length
832 841 * (2) ifname is a logical interface and IPH_LEGACY is not set, or
833 842 */
834 843 boolean_t
835 844 i_ipadm_validate_ifname(ipadm_handle_t iph, const char *ifname)
836 845 {
837 846 ifspec_t ifsp;
838 847
839 848 if (ifname == NULL || ifname[0] == '\0' ||
840 849 !ifparse_ifspec(ifname, &ifsp))
841 850 return (B_FALSE);
842 851 if (ifsp.ifsp_lunvalid)
843 852 return (ifsp.ifsp_lun > 0 && (iph->iph_flags & IPH_LEGACY));
844 853 return (B_TRUE);
845 854 }
846 855
847 856 /*
848 857 * Wrapper for sending a non-transparent I_STR ioctl().
849 858 * Returns: Result from ioctl().
850 859 */
851 860 int
852 861 i_ipadm_strioctl(int s, int cmd, char *buf, int buflen)
853 862 {
854 863 struct strioctl ioc;
855 864
856 865 (void) memset(&ioc, 0, sizeof (ioc));
857 866 ioc.ic_cmd = cmd;
858 867 ioc.ic_timout = 0;
859 868 ioc.ic_len = buflen;
860 869 ioc.ic_dp = buf;
861 870
862 871 return (ioctl(s, I_STR, (char *)&ioc));
863 872 }
864 873
865 874 /*
866 875 * Make a door call to the server and checks if the door call succeeded or not.
867 876 * `is_varsize' specifies that the data returned by ipmgmtd daemon is of
868 877 * variable size and door will allocate buffer using mmap(). In such cases
869 878 * we re-allocate the required memory,n assign it to `rbufp', copy the data to
870 879 * `rbufp' and then call munmap() (see below).
871 880 *
872 881 * It also checks to see if the server side procedure ran successfully by
873 882 * checking for ir_err. Therefore, for some callers who just care about the
874 883 * return status can set `rbufp' to NULL and set `rsize' to 0.
875 884 */
876 885 int
877 886 ipadm_door_call(ipadm_handle_t iph, void *arg, size_t asize, void **rbufp,
878 887 size_t rsize, boolean_t is_varsize)
879 888 {
880 889 door_arg_t darg;
881 890 int err;
882 891 ipmgmt_retval_t rval, *rvalp;
883 892 boolean_t reopen = B_FALSE;
884 893
885 894 if (rbufp == NULL) {
886 895 rvalp = &rval;
887 896 rbufp = (void **)&rvalp;
888 897 rsize = sizeof (rval);
889 898 }
|
↓ open down ↓ |
587 lines elided |
↑ open up ↑ |
890 899
891 900 darg.data_ptr = arg;
892 901 darg.data_size = asize;
893 902 darg.desc_ptr = NULL;
894 903 darg.desc_num = 0;
895 904 darg.rbuf = *rbufp;
896 905 darg.rsize = rsize;
897 906
898 907 reopen:
899 908 (void) pthread_mutex_lock(&iph->iph_lock);
900 - /* The door descriptor is opened if it isn't already */
909 + /*
910 + * The door descriptor is opened if it isn't already.
911 + */
901 912 if (iph->iph_door_fd == -1) {
902 - if ((iph->iph_door_fd = open(IPMGMT_DOOR, O_RDONLY)) < 0) {
913 + char door[MAXPATHLEN];
914 + const char *zroot = zone_get_nroot();
915 +
916 + /*
917 + * If this is a branded zone, make sure we use the "/native"
918 + * prefix for the door path:
919 + */
920 + (void) snprintf(door, sizeof (door), "%s%s", zroot != NULL ?
921 + zroot : "", IPMGMT_DOOR);
922 +
923 + if ((iph->iph_door_fd = open(door, O_RDONLY)) < 0) {
903 924 err = errno;
904 925 (void) pthread_mutex_unlock(&iph->iph_lock);
905 926 return (err);
906 927 }
907 928 }
908 929 (void) pthread_mutex_unlock(&iph->iph_lock);
909 930
910 931 if (door_call(iph->iph_door_fd, &darg) == -1) {
911 932 /*
912 933 * Stale door descriptor is possible if ipmgmtd was restarted
913 934 * since last iph_door_fd was opened, so try re-opening door
914 935 * descriptor.
915 936 */
916 937 if (!reopen && errno == EBADF) {
917 938 (void) close(iph->iph_door_fd);
918 939 iph->iph_door_fd = -1;
919 940 reopen = B_TRUE;
920 941 goto reopen;
921 942 }
922 943 return (errno);
923 944 }
924 945 err = ((ipmgmt_retval_t *)(void *)(darg.rbuf))->ir_err;
925 946 if (darg.rbuf != *rbufp) {
926 947 /*
927 948 * if the caller is expecting the result to fit in specified
928 949 * buffer then return failure.
929 950 */
930 951 if (!is_varsize)
931 952 err = EBADE;
932 953 /*
933 954 * The size of the buffer `*rbufp' was not big enough
934 955 * and the door itself allocated buffer, for us. We will
935 956 * hit this, on several occasion as for some cases
936 957 * we cannot predict the size of the return structure.
937 958 * Reallocate the buffer `*rbufp' and memcpy() the contents
938 959 * to new buffer.
939 960 */
940 961 if (err == 0) {
941 962 void *newp;
942 963
943 964 /* allocated memory will be freed by the caller */
944 965 if ((newp = realloc(*rbufp, darg.rsize)) == NULL) {
945 966 err = ENOMEM;
946 967 } else {
947 968 *rbufp = newp;
948 969 (void) memcpy(*rbufp, darg.rbuf, darg.rsize);
949 970 }
950 971 }
951 972 /* munmap() the door buffer */
952 973 (void) munmap(darg.rbuf, darg.rsize);
953 974 } else {
954 975 if (darg.rsize != rsize)
955 976 err = EBADE;
956 977 }
957 978 return (err);
958 979 }
|
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX