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