17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d04ccbb3Scarlsonj * Common Development and Distribution License (the "License"). 6d04ccbb3Scarlsonj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2265c8f1c0Smeem * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <string.h> 277c478bd9Sstevel@tonic-gate #include <unistd.h> 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <sys/uio.h> 307c478bd9Sstevel@tonic-gate #include <sys/socket.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <fcntl.h> 337c478bd9Sstevel@tonic-gate #include <errno.h> 34a1196271SJames Carlson #include <limits.h> 357c478bd9Sstevel@tonic-gate #include <netinet/in.h> 36d04ccbb3Scarlsonj #include <netinet/tcp.h> 377c478bd9Sstevel@tonic-gate #include <net/if.h> 387c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 397c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 40d04ccbb3Scarlsonj #include <sys/time.h> 417c478bd9Sstevel@tonic-gate #include <stdio.h> /* snprintf */ 427c478bd9Sstevel@tonic-gate #include <arpa/inet.h> /* ntohl, ntohs, etc */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include "dhcpagent_ipc.h" 457c478bd9Sstevel@tonic-gate #include "dhcpagent_util.h" 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* 487c478bd9Sstevel@tonic-gate * the protocol used here is a simple request/reply scheme: a client 497c478bd9Sstevel@tonic-gate * sends a dhcp_ipc_request_t message to the agent, and the agent 507c478bd9Sstevel@tonic-gate * sends a dhcp_ipc_reply_t back to the client. since the requests 517c478bd9Sstevel@tonic-gate * and replies can be variable-length, they are prefixed on "the wire" 527c478bd9Sstevel@tonic-gate * by a 32-bit number that tells the other end how many bytes to 537c478bd9Sstevel@tonic-gate * expect. 547c478bd9Sstevel@tonic-gate * 557c478bd9Sstevel@tonic-gate * the format of a request consists of a single dhcp_ipc_request_t; 567c478bd9Sstevel@tonic-gate * note that the length of this dhcp_ipc_request_t is variable (using 577c478bd9Sstevel@tonic-gate * the standard c array-of-size-1 trick). the type of the payload is 587c478bd9Sstevel@tonic-gate * given by `data_type', which is guaranteed to be `data_length' bytes 597c478bd9Sstevel@tonic-gate * long starting at `buffer'. note that `buffer' is guaranteed to be 607c478bd9Sstevel@tonic-gate * 32-bit aligned but it is poor taste to rely on this. 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * the format of a reply is much the same: a single dhcp_ipc_reply_t; 637c478bd9Sstevel@tonic-gate * note again that the length of the dhcp_ipc_reply_t is variable. 647c478bd9Sstevel@tonic-gate * the type of the payload is given by `data_type', which is 657c478bd9Sstevel@tonic-gate * guaranteed to be `data_length' bytes long starting at `buffer'. 667c478bd9Sstevel@tonic-gate * once again, note that `buffer' is guaranteed to be 32-bit aligned 677c478bd9Sstevel@tonic-gate * but it is poor taste to rely on this. 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * requests and replies can be paired up by comparing `ipc_id' fields. 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define BUFMAX 256 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate static int dhcp_ipc_timed_read(int, void *, unsigned int, int *); 757c478bd9Sstevel@tonic-gate static int getinfo_ifnames(const char *, dhcp_optnum_t *, DHCP_OPT **); 767c478bd9Sstevel@tonic-gate static char *get_ifnames(int, int); 777c478bd9Sstevel@tonic-gate 78cfb9c9abScarlsonj /* must be kept in sync with enum in dhcpagent_ipc.h */ 79cfb9c9abScarlsonj static const char *ipc_typestr[] = { 80cfb9c9abScarlsonj "drop", "extend", "ping", "release", "start", "status", 81cfb9c9abScarlsonj "inform", "get_tag" 82cfb9c9abScarlsonj }; 83cfb9c9abScarlsonj 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * dhcp_ipc_alloc_request(): allocates a dhcp_ipc_request_t of the given type 867c478bd9Sstevel@tonic-gate * and interface, with a timeout of 0. 877c478bd9Sstevel@tonic-gate * 887c478bd9Sstevel@tonic-gate * input: dhcp_ipc_type_t: the type of ipc request to allocate 897c478bd9Sstevel@tonic-gate * const char *: the interface to associate the request with 90d04ccbb3Scarlsonj * const void *: the payload to send with the message (NULL if none) 917c478bd9Sstevel@tonic-gate * uint32_t: the payload size (0 if none) 927c478bd9Sstevel@tonic-gate * dhcp_data_type_t: the description of the type of payload 937c478bd9Sstevel@tonic-gate * output: dhcp_ipc_request_t *: the request on success, NULL on failure 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate dhcp_ipc_request_t * 97d04ccbb3Scarlsonj dhcp_ipc_alloc_request(dhcp_ipc_type_t type, const char *ifname, 98d04ccbb3Scarlsonj const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *request = calloc(1, DHCP_IPC_REQUEST_SIZE + 1017c478bd9Sstevel@tonic-gate buffer_size); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate if (request == NULL) 1047c478bd9Sstevel@tonic-gate return (NULL); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate request->message_type = type; 1077c478bd9Sstevel@tonic-gate request->data_length = buffer_size; 1087c478bd9Sstevel@tonic-gate request->data_type = data_type; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (ifname != NULL) 11165c8f1c0Smeem (void) strlcpy(request->ifname, ifname, LIFNAMSIZ); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (buffer != NULL) 1147c478bd9Sstevel@tonic-gate (void) memcpy(request->buffer, buffer, buffer_size); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate return (request); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * dhcp_ipc_alloc_reply(): allocates a dhcp_ipc_reply_t 1217c478bd9Sstevel@tonic-gate * 1227c478bd9Sstevel@tonic-gate * input: dhcp_ipc_request_t *: the request the reply is for 1237c478bd9Sstevel@tonic-gate * int: the return code (0 for success, DHCP_IPC_E_* otherwise) 124d04ccbb3Scarlsonj * const void *: the payload to send with the message (NULL if none) 1257c478bd9Sstevel@tonic-gate * uint32_t: the payload size (0 if none) 1267c478bd9Sstevel@tonic-gate * dhcp_data_type_t: the description of the type of payload 1277c478bd9Sstevel@tonic-gate * output: dhcp_ipc_reply_t *: the reply on success, NULL on failure 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t * 131d04ccbb3Scarlsonj dhcp_ipc_alloc_reply(dhcp_ipc_request_t *request, int return_code, 132d04ccbb3Scarlsonj const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *reply = calloc(1, DHCP_IPC_REPLY_SIZE + buffer_size); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate if (reply == NULL) 1377c478bd9Sstevel@tonic-gate return (NULL); 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate reply->message_type = request->message_type; 1407c478bd9Sstevel@tonic-gate reply->ipc_id = request->ipc_id; 1417c478bd9Sstevel@tonic-gate reply->return_code = return_code; 1427c478bd9Sstevel@tonic-gate reply->data_length = buffer_size; 1437c478bd9Sstevel@tonic-gate reply->data_type = data_type; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate if (buffer != NULL) 1467c478bd9Sstevel@tonic-gate (void) memcpy(reply->buffer, buffer, buffer_size); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate return (reply); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * dhcp_ipc_get_data(): gets the data and data type from a dhcp_ipc_reply_t 1537c478bd9Sstevel@tonic-gate * 1547c478bd9Sstevel@tonic-gate * input: dhcp_ipc_reply_t *: the reply to get data from 1557c478bd9Sstevel@tonic-gate * size_t *: the size of the resulting data 1567c478bd9Sstevel@tonic-gate * dhcp_data_type_t *: the type of the message (returned) 1577c478bd9Sstevel@tonic-gate * output: void *: a pointer to the data, if there is any. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate void * 1617c478bd9Sstevel@tonic-gate dhcp_ipc_get_data(dhcp_ipc_reply_t *reply, size_t *size, dhcp_data_type_t *type) 1627c478bd9Sstevel@tonic-gate { 1637c478bd9Sstevel@tonic-gate if (reply == NULL || reply->data_length == 0) { 1647c478bd9Sstevel@tonic-gate *size = 0; 1657c478bd9Sstevel@tonic-gate return (NULL); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (type != NULL) 1697c478bd9Sstevel@tonic-gate *type = reply->data_type; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate *size = reply->data_length; 1727c478bd9Sstevel@tonic-gate return (reply->buffer); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * dhcp_ipc_recv_msg(): gets a message using the agent's ipc protocol 1777c478bd9Sstevel@tonic-gate * 1787c478bd9Sstevel@tonic-gate * input: int: the file descriptor to get the message from 1797c478bd9Sstevel@tonic-gate * void **: the address of a pointer to store the message 1807c478bd9Sstevel@tonic-gate * (dynamically allocated) 1817c478bd9Sstevel@tonic-gate * uint32_t: the minimum length of the packet 1827c478bd9Sstevel@tonic-gate * int: the # of milliseconds to wait for the message (-1 is forever) 183d04ccbb3Scarlsonj * output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate static int 1877c478bd9Sstevel@tonic-gate dhcp_ipc_recv_msg(int fd, void **msg, uint32_t base_length, int msec) 1887c478bd9Sstevel@tonic-gate { 189d04ccbb3Scarlsonj int retval; 1907c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *ipc_msg; 1917c478bd9Sstevel@tonic-gate uint32_t length; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate retval = dhcp_ipc_timed_read(fd, &length, sizeof (uint32_t), &msec); 194d04ccbb3Scarlsonj if (retval != DHCP_IPC_SUCCESS) 195d04ccbb3Scarlsonj return (retval); 196d04ccbb3Scarlsonj 197d04ccbb3Scarlsonj if (length == 0) 198d04ccbb3Scarlsonj return (DHCP_IPC_E_PROTO); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate *msg = malloc(length); 2017c478bd9Sstevel@tonic-gate if (*msg == NULL) 2027c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate retval = dhcp_ipc_timed_read(fd, *msg, length, &msec); 205d04ccbb3Scarlsonj if (retval != DHCP_IPC_SUCCESS) { 2067c478bd9Sstevel@tonic-gate free(*msg); 207d04ccbb3Scarlsonj return (retval); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (length < base_length) { 2117c478bd9Sstevel@tonic-gate free(*msg); 212d04ccbb3Scarlsonj return (DHCP_IPC_E_PROTO); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * the data_length field is in the same place in either ipc message. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate ipc_msg = (dhcp_ipc_reply_t *)(*msg); 2207c478bd9Sstevel@tonic-gate if (ipc_msg->data_length + base_length != length) { 2217c478bd9Sstevel@tonic-gate free(*msg); 222d04ccbb3Scarlsonj return (DHCP_IPC_E_PROTO); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 225d04ccbb3Scarlsonj return (DHCP_IPC_SUCCESS); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * dhcp_ipc_recv_request(): gets a request using the agent's ipc protocol 2307c478bd9Sstevel@tonic-gate * 2317c478bd9Sstevel@tonic-gate * input: int: the file descriptor to get the message from 2327c478bd9Sstevel@tonic-gate * dhcp_ipc_request_t **: address of a pointer to store the request 2337c478bd9Sstevel@tonic-gate * (dynamically allocated) 2347c478bd9Sstevel@tonic-gate * int: the # of milliseconds to wait for the message (-1 is forever) 2357c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2367c478bd9Sstevel@tonic-gate */ 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate int 2397c478bd9Sstevel@tonic-gate dhcp_ipc_recv_request(int fd, dhcp_ipc_request_t **request, int msec) 2407c478bd9Sstevel@tonic-gate { 2417c478bd9Sstevel@tonic-gate int retval; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate retval = dhcp_ipc_recv_msg(fd, (void **)request, DHCP_IPC_REQUEST_SIZE, 2447c478bd9Sstevel@tonic-gate msec); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* guarantee that ifname will be NUL-terminated */ 2477c478bd9Sstevel@tonic-gate if (retval == 0) 24865c8f1c0Smeem (*request)->ifname[LIFNAMSIZ - 1] = '\0'; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate return (retval); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate /* 2547c478bd9Sstevel@tonic-gate * dhcp_ipc_recv_reply(): gets a reply using the agent's ipc protocol 2557c478bd9Sstevel@tonic-gate * 2567c478bd9Sstevel@tonic-gate * input: int: the file descriptor to get the message from 2577c478bd9Sstevel@tonic-gate * dhcp_ipc_reply_t **: address of a pointer to store the reply 2587c478bd9Sstevel@tonic-gate * (dynamically allocated) 259a1196271SJames Carlson * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 260a1196271SJames Carlson * or DHCP_IPC_WAIT_DEFAULT 2617c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate static int 265a1196271SJames Carlson dhcp_ipc_recv_reply(int fd, dhcp_ipc_reply_t **reply, int32_t timeout) 2667c478bd9Sstevel@tonic-gate { 267a1196271SJames Carlson /* 268a1196271SJames Carlson * If the caller doesn't want to wait forever, and the amount of time 269a1196271SJames Carlson * he wants to wait is expressible as an integer number of milliseconds 270a1196271SJames Carlson * (as needed by the msg function), then we wait that amount of time 271a1196271SJames Carlson * plus an extra two seconds for the daemon to do its work. The extra 272a1196271SJames Carlson * two seconds is arbitrary; it should allow plenty of time for the 273a1196271SJames Carlson * daemon to respond within the existing timeout, as specified in the 274a1196271SJames Carlson * original request, so the only time we give up is when the daemon is 275a1196271SJames Carlson * stopped or otherwise malfunctioning. 276a1196271SJames Carlson * 277a1196271SJames Carlson * Note that the wait limit (milliseconds in an 'int') is over 24 days, 278a1196271SJames Carlson * so it's unlikely that any request will actually be that long, and 279a1196271SJames Carlson * it's unlikely that anyone will care if we wait forever on a request 280a1196271SJames Carlson * for a 30 day timer. The point is to protect against daemon 281a1196271SJames Carlson * malfunction in the usual cases, not to provide an absolute command 282a1196271SJames Carlson * timer. 283a1196271SJames Carlson */ 284a1196271SJames Carlson if (timeout == DHCP_IPC_WAIT_DEFAULT) 285a1196271SJames Carlson timeout = DHCP_IPC_DEFAULT_WAIT; 286a1196271SJames Carlson if (timeout != DHCP_IPC_WAIT_FOREVER && timeout < INT_MAX / 1000 - 2) 287a1196271SJames Carlson timeout = (timeout + 2) * 1000; 288a1196271SJames Carlson else 289a1196271SJames Carlson timeout = -1; 290a1196271SJames Carlson return (dhcp_ipc_recv_msg(fd, (void **)reply, DHCP_IPC_REPLY_SIZE, 291a1196271SJames Carlson timeout)); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * dhcp_ipc_send_msg(): transmits a message using the agent's ipc protocol 2967c478bd9Sstevel@tonic-gate * 2977c478bd9Sstevel@tonic-gate * input: int: the file descriptor to transmit on 2987c478bd9Sstevel@tonic-gate * void *: the message to send 2997c478bd9Sstevel@tonic-gate * uint32_t: the message length 3007c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3017c478bd9Sstevel@tonic-gate */ 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate static int 3047c478bd9Sstevel@tonic-gate dhcp_ipc_send_msg(int fd, void *msg, uint32_t message_length) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate struct iovec iovec[2]; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate iovec[0].iov_base = (caddr_t)&message_length; 3097c478bd9Sstevel@tonic-gate iovec[0].iov_len = sizeof (uint32_t); 3107c478bd9Sstevel@tonic-gate iovec[1].iov_base = msg; 3117c478bd9Sstevel@tonic-gate iovec[1].iov_len = message_length; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate if (writev(fd, iovec, sizeof (iovec) / sizeof (*iovec)) == -1) 3147c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_WRITEV); 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate return (0); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* 3207c478bd9Sstevel@tonic-gate * dhcp_ipc_send_reply(): transmits a reply using the agent's ipc protocol 3217c478bd9Sstevel@tonic-gate * 3227c478bd9Sstevel@tonic-gate * input: int: the file descriptor to transmit on 3237c478bd9Sstevel@tonic-gate * dhcp_ipc_reply_t *: the reply to send 3247c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate int 3287c478bd9Sstevel@tonic-gate dhcp_ipc_send_reply(int fd, dhcp_ipc_reply_t *reply) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate return (dhcp_ipc_send_msg(fd, reply, DHCP_IPC_REPLY_SIZE + 3317c478bd9Sstevel@tonic-gate reply->data_length)); 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* 3357c478bd9Sstevel@tonic-gate * dhcp_ipc_send_request(): transmits a request using the agent's ipc protocol 3367c478bd9Sstevel@tonic-gate * 3377c478bd9Sstevel@tonic-gate * input: int: the file descriptor to transmit on 3387c478bd9Sstevel@tonic-gate * dhcp_ipc_request_t *: the request to send 3397c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate static int 3437c478bd9Sstevel@tonic-gate dhcp_ipc_send_request(int fd, dhcp_ipc_request_t *request) 3447c478bd9Sstevel@tonic-gate { 3457c478bd9Sstevel@tonic-gate /* 3467c478bd9Sstevel@tonic-gate * for now, ipc_ids aren't really used, but they're intended 3477c478bd9Sstevel@tonic-gate * to make it easy to send several requests and then collect 3487c478bd9Sstevel@tonic-gate * all of the replies (and pair them with the requests). 3497c478bd9Sstevel@tonic-gate */ 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate request->ipc_id = gethrtime(); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate return (dhcp_ipc_send_msg(fd, request, DHCP_IPC_REQUEST_SIZE + 3547c478bd9Sstevel@tonic-gate request->data_length)); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * dhcp_ipc_make_request(): sends the provided request to the agent and reaps 3597c478bd9Sstevel@tonic-gate * the reply 3607c478bd9Sstevel@tonic-gate * 3617c478bd9Sstevel@tonic-gate * input: dhcp_ipc_request_t *: the request to make 3627c478bd9Sstevel@tonic-gate * dhcp_ipc_reply_t **: the reply (dynamically allocated) 3637c478bd9Sstevel@tonic-gate * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 3647c478bd9Sstevel@tonic-gate * or DHCP_IPC_WAIT_DEFAULT 3657c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate int 3697c478bd9Sstevel@tonic-gate dhcp_ipc_make_request(dhcp_ipc_request_t *request, dhcp_ipc_reply_t **reply, 3707c478bd9Sstevel@tonic-gate int32_t timeout) 3717c478bd9Sstevel@tonic-gate { 372d04ccbb3Scarlsonj int fd, on, retval; 373d04ccbb3Scarlsonj struct sockaddr_in sinv; 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate fd = socket(AF_INET, SOCK_STREAM, 0); 3767c478bd9Sstevel@tonic-gate if (fd == -1) 3777c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_SOCKET); 378d04ccbb3Scarlsonj 379d04ccbb3Scarlsonj /* 380d04ccbb3Scarlsonj * Bind a privileged port if we have sufficient privilege to do so. 381d04ccbb3Scarlsonj * Continue as non-privileged otherwise. 382d04ccbb3Scarlsonj */ 383d04ccbb3Scarlsonj on = 1; 384d04ccbb3Scarlsonj (void) setsockopt(fd, IPPROTO_TCP, TCP_ANONPRIVBIND, &on, sizeof (on)); 385d04ccbb3Scarlsonj 386d04ccbb3Scarlsonj (void) memset(&sinv, 0, sizeof (sinv)); 387d04ccbb3Scarlsonj sinv.sin_family = AF_INET; 388d04ccbb3Scarlsonj if (bind(fd, (struct sockaddr *)&sinv, sizeof (sinv)) == -1) { 389d04ccbb3Scarlsonj (void) dhcp_ipc_close(fd); 390d04ccbb3Scarlsonj return (DHCP_IPC_E_BIND); 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 393d04ccbb3Scarlsonj sinv.sin_port = htons(IPPORT_DHCPAGENT); 394d04ccbb3Scarlsonj sinv.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 395d04ccbb3Scarlsonj retval = connect(fd, (struct sockaddr *)&sinv, sizeof (sinv)); 3967c478bd9Sstevel@tonic-gate if (retval == -1) { 3977c478bd9Sstevel@tonic-gate (void) dhcp_ipc_close(fd); 3987c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_CONNECT); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate request->timeout = timeout; 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate retval = dhcp_ipc_send_request(fd, request); 4047c478bd9Sstevel@tonic-gate if (retval == 0) 405a1196271SJames Carlson retval = dhcp_ipc_recv_reply(fd, reply, timeout); 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate (void) dhcp_ipc_close(fd); 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate return (retval); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * dhcp_ipc_init(): initializes the ipc channel for use by the agent 4147c478bd9Sstevel@tonic-gate * 4157c478bd9Sstevel@tonic-gate * input: int *: the file descriptor to accept on (returned) 4167c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 4177c478bd9Sstevel@tonic-gate */ 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate int 4207c478bd9Sstevel@tonic-gate dhcp_ipc_init(int *listen_fd) 4217c478bd9Sstevel@tonic-gate { 4227c478bd9Sstevel@tonic-gate struct sockaddr_in sin; 4237c478bd9Sstevel@tonic-gate int on = 1; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate (void) memset(&sin, 0, sizeof (struct sockaddr_in)); 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate sin.sin_family = AF_INET; 4287c478bd9Sstevel@tonic-gate sin.sin_port = htons(IPPORT_DHCPAGENT); 4297c478bd9Sstevel@tonic-gate sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate *listen_fd = socket(AF_INET, SOCK_STREAM, 0); 4327c478bd9Sstevel@tonic-gate if (*listen_fd == -1) 4337c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_SOCKET); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate /* 4367c478bd9Sstevel@tonic-gate * we use SO_REUSEADDR here since in the case where there 4377c478bd9Sstevel@tonic-gate * really is another daemon running that is using the agent's 4387c478bd9Sstevel@tonic-gate * port, bind(3N) will fail. so we can't lose. 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate (void) setsockopt(*listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, 4427c478bd9Sstevel@tonic-gate sizeof (on)); 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate if (bind(*listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) { 4457c478bd9Sstevel@tonic-gate (void) close(*listen_fd); 4467c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_BIND); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate if (listen(*listen_fd, DHCP_IPC_LISTEN_BACKLOG) == -1) { 4507c478bd9Sstevel@tonic-gate (void) close(*listen_fd); 4517c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_LISTEN); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate return (0); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate /* 4587c478bd9Sstevel@tonic-gate * dhcp_ipc_accept(): accepts an incoming connection for the agent 4597c478bd9Sstevel@tonic-gate * 4607c478bd9Sstevel@tonic-gate * input: int: the file descriptor to accept on 4617c478bd9Sstevel@tonic-gate * int *: the accepted file descriptor (returned) 4627c478bd9Sstevel@tonic-gate * int *: nonzero if the client is privileged (returned) 4637c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 4647c478bd9Sstevel@tonic-gate * note: sets the socket into nonblocking mode 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate int 4687c478bd9Sstevel@tonic-gate dhcp_ipc_accept(int listen_fd, int *fd, int *is_priv) 4697c478bd9Sstevel@tonic-gate { 4707c478bd9Sstevel@tonic-gate struct sockaddr_in sin_peer; 4717c478bd9Sstevel@tonic-gate int sin_len = sizeof (sin_peer); 4727c478bd9Sstevel@tonic-gate int sockflags; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate /* 4757c478bd9Sstevel@tonic-gate * if we were extremely concerned with portability, we would 4767c478bd9Sstevel@tonic-gate * set the socket into nonblocking mode before doing the 4777c478bd9Sstevel@tonic-gate * accept(3N), since on BSD-based networking stacks, there is 4787c478bd9Sstevel@tonic-gate * a potential race that can occur if the socket which 4797c478bd9Sstevel@tonic-gate * connected to us performs a TCP RST before we accept, since 4807c478bd9Sstevel@tonic-gate * BSD handles this case entirely in the kernel and as a 4817c478bd9Sstevel@tonic-gate * result even though select said we will not block, we can 4827c478bd9Sstevel@tonic-gate * end up blocking since there is no longer a connection to 4837c478bd9Sstevel@tonic-gate * accept. on SVR4-based systems, this should be okay, 4847c478bd9Sstevel@tonic-gate * and we will get EPROTO back, even though POSIX.1g says 4857c478bd9Sstevel@tonic-gate * we should get ECONNABORTED. 4867c478bd9Sstevel@tonic-gate */ 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate *fd = accept(listen_fd, (struct sockaddr *)&sin_peer, &sin_len); 4897c478bd9Sstevel@tonic-gate if (*fd == -1) 4907c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_ACCEPT); 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate /* get credentials */ 4937c478bd9Sstevel@tonic-gate *is_priv = ntohs(sin_peer.sin_port) < IPPORT_RESERVED; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* 4967c478bd9Sstevel@tonic-gate * kick the socket into non-blocking mode so that later 4977c478bd9Sstevel@tonic-gate * operations on the socket don't block and hold up the whole 4987c478bd9Sstevel@tonic-gate * application. with the event demuxing approach, this may 4997c478bd9Sstevel@tonic-gate * seem unnecessary, but in order to get partial reads/writes 5007c478bd9Sstevel@tonic-gate * and to handle our internal protocol for passing data 5017c478bd9Sstevel@tonic-gate * between the agent and its consumers, this is needed. 5027c478bd9Sstevel@tonic-gate */ 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate if ((sockflags = fcntl(*fd, F_GETFL, 0)) == -1) { 5057c478bd9Sstevel@tonic-gate (void) close(*fd); 5067c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_FCNTL); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate if (fcntl(*fd, F_SETFL, sockflags | O_NONBLOCK) == -1) { 5107c478bd9Sstevel@tonic-gate (void) close(*fd); 5117c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_FCNTL); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate return (0); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* 5187c478bd9Sstevel@tonic-gate * dhcp_ipc_close(): closes an ipc descriptor 5197c478bd9Sstevel@tonic-gate * 5207c478bd9Sstevel@tonic-gate * input: int: the file descriptor to close 5217c478bd9Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 5227c478bd9Sstevel@tonic-gate */ 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate int 5257c478bd9Sstevel@tonic-gate dhcp_ipc_close(int fd) 5267c478bd9Sstevel@tonic-gate { 5277c478bd9Sstevel@tonic-gate return ((close(fd) == -1) ? DHCP_IPC_E_CLOSE : 0); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* 5317c478bd9Sstevel@tonic-gate * dhcp_ipc_strerror(): maps an ipc error code into a human-readable string 5327c478bd9Sstevel@tonic-gate * 5337c478bd9Sstevel@tonic-gate * input: int: the ipc error code to map 5347c478bd9Sstevel@tonic-gate * output: const char *: the corresponding human-readable string 5357c478bd9Sstevel@tonic-gate */ 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate const char * 5387c478bd9Sstevel@tonic-gate dhcp_ipc_strerror(int error) 5397c478bd9Sstevel@tonic-gate { 5407c478bd9Sstevel@tonic-gate /* note: this must be kept in sync with DHCP_IPC_E_* definitions */ 5417c478bd9Sstevel@tonic-gate const char *syscalls[] = { 5427c478bd9Sstevel@tonic-gate "<unknown>", "socket", "fcntl", "read", "accept", "close", 543d04ccbb3Scarlsonj "bind", "listen", "malloc", "connect", "writev", "poll" 5447c478bd9Sstevel@tonic-gate }; 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate const char *error_string; 5477c478bd9Sstevel@tonic-gate static char buffer[BUFMAX]; 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate switch (error) { 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * none of these errors actually go over the wire. 5537c478bd9Sstevel@tonic-gate * hence, we assume that errno is still fresh. 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate case DHCP_IPC_E_SOCKET: /* FALLTHRU */ 5577c478bd9Sstevel@tonic-gate case DHCP_IPC_E_FCNTL: /* FALLTHRU */ 5587c478bd9Sstevel@tonic-gate case DHCP_IPC_E_READ: /* FALLTHRU */ 5597c478bd9Sstevel@tonic-gate case DHCP_IPC_E_ACCEPT: /* FALLTHRU */ 5607c478bd9Sstevel@tonic-gate case DHCP_IPC_E_CLOSE: /* FALLTHRU */ 5617c478bd9Sstevel@tonic-gate case DHCP_IPC_E_BIND: /* FALLTHRU */ 5627c478bd9Sstevel@tonic-gate case DHCP_IPC_E_LISTEN: /* FALLTHRU */ 5637c478bd9Sstevel@tonic-gate case DHCP_IPC_E_CONNECT: /* FALLTHRU */ 564d04ccbb3Scarlsonj case DHCP_IPC_E_WRITEV: /* FALLTHRU */ 565d04ccbb3Scarlsonj case DHCP_IPC_E_POLL: 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate error_string = strerror(errno); 5687c478bd9Sstevel@tonic-gate if (error_string == NULL) 5697c478bd9Sstevel@tonic-gate error_string = "unknown error"; 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate (void) snprintf(buffer, sizeof (buffer), "%s: %s", 5727c478bd9Sstevel@tonic-gate syscalls[error], error_string); 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate error_string = buffer; 5757c478bd9Sstevel@tonic-gate break; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate case DHCP_IPC_E_MEMORY: 5787c478bd9Sstevel@tonic-gate error_string = "out of memory"; 5797c478bd9Sstevel@tonic-gate break; 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate case DHCP_IPC_E_TIMEOUT: 5827c478bd9Sstevel@tonic-gate error_string = "wait timed out, operation still pending..."; 5837c478bd9Sstevel@tonic-gate break; 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate case DHCP_IPC_E_INVIF: 5867c478bd9Sstevel@tonic-gate error_string = "interface does not exist or cannot be managed " 5877c478bd9Sstevel@tonic-gate "using DHCP"; 5887c478bd9Sstevel@tonic-gate break; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate case DHCP_IPC_E_INT: 5917c478bd9Sstevel@tonic-gate error_string = "internal error (might work later)"; 5927c478bd9Sstevel@tonic-gate break; 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate case DHCP_IPC_E_PERM: 5957c478bd9Sstevel@tonic-gate error_string = "permission denied"; 5967c478bd9Sstevel@tonic-gate break; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate case DHCP_IPC_E_OUTSTATE: 5997c478bd9Sstevel@tonic-gate error_string = "interface not in appropriate state for command"; 6007c478bd9Sstevel@tonic-gate break; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate case DHCP_IPC_E_PEND: 6037c478bd9Sstevel@tonic-gate error_string = "interface currently has a pending command " 6047c478bd9Sstevel@tonic-gate "(try later)"; 6057c478bd9Sstevel@tonic-gate break; 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate case DHCP_IPC_E_BOOTP: 6087c478bd9Sstevel@tonic-gate error_string = "interface is administered with BOOTP, not DHCP"; 6097c478bd9Sstevel@tonic-gate break; 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate case DHCP_IPC_E_CMD_UNKNOWN: 6127c478bd9Sstevel@tonic-gate error_string = "unknown command"; 6137c478bd9Sstevel@tonic-gate break; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate case DHCP_IPC_E_UNKIF: 6167c478bd9Sstevel@tonic-gate error_string = "interface is not under DHCP control"; 6177c478bd9Sstevel@tonic-gate break; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate case DHCP_IPC_E_PROTO: 6207c478bd9Sstevel@tonic-gate error_string = "ipc protocol violation"; 6217c478bd9Sstevel@tonic-gate break; 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate case DHCP_IPC_E_FAILEDIF: 6247c478bd9Sstevel@tonic-gate error_string = "interface is in a FAILED state and must be " 6257c478bd9Sstevel@tonic-gate "manually restarted"; 6267c478bd9Sstevel@tonic-gate break; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate case DHCP_IPC_E_NOPRIMARY: 6297c478bd9Sstevel@tonic-gate error_string = "primary interface requested but no primary " 6307c478bd9Sstevel@tonic-gate "interface is set"; 6317c478bd9Sstevel@tonic-gate break; 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate case DHCP_IPC_E_NOIPIF: 6347c478bd9Sstevel@tonic-gate error_string = "interface currently has no IP address"; 6357c478bd9Sstevel@tonic-gate break; 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate case DHCP_IPC_E_DOWNIF: 6387c478bd9Sstevel@tonic-gate error_string = "interface is currently down"; 6397c478bd9Sstevel@tonic-gate break; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate case DHCP_IPC_E_NOVALUE: 6427c478bd9Sstevel@tonic-gate error_string = "no value was found for this option"; 6437c478bd9Sstevel@tonic-gate break; 6447c478bd9Sstevel@tonic-gate 645d04ccbb3Scarlsonj case DHCP_IPC_E_RUNNING: 646d04ccbb3Scarlsonj error_string = "DHCP is already running"; 647d04ccbb3Scarlsonj break; 648d04ccbb3Scarlsonj 649d04ccbb3Scarlsonj case DHCP_IPC_E_SRVFAILED: 650d04ccbb3Scarlsonj error_string = "DHCP server refused request"; 651d04ccbb3Scarlsonj break; 652d04ccbb3Scarlsonj 653d04ccbb3Scarlsonj case DHCP_IPC_E_EOF: 654d04ccbb3Scarlsonj error_string = "ipc connection closed"; 6557c478bd9Sstevel@tonic-gate break; 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate default: 6587c478bd9Sstevel@tonic-gate error_string = "unknown error"; 6597c478bd9Sstevel@tonic-gate break; 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate /* 6637c478bd9Sstevel@tonic-gate * TODO: internationalize this error string 6647c478bd9Sstevel@tonic-gate */ 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate return (error_string); 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate /* 670cfb9c9abScarlsonj * dhcp_string_to_request(): maps a string into a request code 671cfb9c9abScarlsonj * 672cfb9c9abScarlsonj * input: const char *: the string to map 673cfb9c9abScarlsonj * output: dhcp_ipc_type_t: the request code, or -1 if unknown 674cfb9c9abScarlsonj */ 675cfb9c9abScarlsonj 676cfb9c9abScarlsonj dhcp_ipc_type_t 677cfb9c9abScarlsonj dhcp_string_to_request(const char *request) 678cfb9c9abScarlsonj { 679cfb9c9abScarlsonj unsigned int i; 680cfb9c9abScarlsonj 681cfb9c9abScarlsonj for (i = 0; i < DHCP_NIPC; i++) 682cfb9c9abScarlsonj if (strcmp(ipc_typestr[i], request) == 0) 683cfb9c9abScarlsonj return ((dhcp_ipc_type_t)i); 684cfb9c9abScarlsonj 685cfb9c9abScarlsonj return ((dhcp_ipc_type_t)-1); 686cfb9c9abScarlsonj } 687cfb9c9abScarlsonj 688cfb9c9abScarlsonj /* 689d04ccbb3Scarlsonj * dhcp_ipc_type_to_string(): maps an ipc command code into a human-readable 690d04ccbb3Scarlsonj * string 691d04ccbb3Scarlsonj * 692d04ccbb3Scarlsonj * input: int: the ipc command code to map 693d04ccbb3Scarlsonj * output: const char *: the corresponding human-readable string 694d04ccbb3Scarlsonj */ 695d04ccbb3Scarlsonj 696d04ccbb3Scarlsonj const char * 697d04ccbb3Scarlsonj dhcp_ipc_type_to_string(dhcp_ipc_type_t type) 698d04ccbb3Scarlsonj { 699d04ccbb3Scarlsonj if (type < 0 || type >= DHCP_NIPC) 700d04ccbb3Scarlsonj return ("unknown"); 701d04ccbb3Scarlsonj else 702cfb9c9abScarlsonj return (ipc_typestr[(int)type]); 703d04ccbb3Scarlsonj } 704d04ccbb3Scarlsonj 705d04ccbb3Scarlsonj /* 7067c478bd9Sstevel@tonic-gate * getinfo_ifnames(): checks the value of a specified option on a list of 7077c478bd9Sstevel@tonic-gate * interface names. 7087c478bd9Sstevel@tonic-gate * input: const char *: a list of interface names to query (in order) for 7097c478bd9Sstevel@tonic-gate * the option; "" queries the primary interface 7107c478bd9Sstevel@tonic-gate * dhcp_optnum_t *: a description of the desired option 7117c478bd9Sstevel@tonic-gate * DHCP_OPT **: filled in with the (dynamically allocated) value of 7127c478bd9Sstevel@tonic-gate * the option upon success. 7137c478bd9Sstevel@tonic-gate * output: int: DHCP_IPC_E_* on error, 0 on success or if no value was 7147c478bd9Sstevel@tonic-gate * found but no error occurred either (*result will be NULL) 7157c478bd9Sstevel@tonic-gate */ 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate static int 7187c478bd9Sstevel@tonic-gate getinfo_ifnames(const char *ifn, dhcp_optnum_t *optnum, DHCP_OPT **result) 7197c478bd9Sstevel@tonic-gate { 7207c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *request; 7217c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 7227c478bd9Sstevel@tonic-gate char *ifnames, *ifnames_head; 7237c478bd9Sstevel@tonic-gate DHCP_OPT *opt; 7247c478bd9Sstevel@tonic-gate size_t opt_size; 7257c478bd9Sstevel@tonic-gate int retval = 0; 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate *result = NULL; 7287c478bd9Sstevel@tonic-gate ifnames_head = ifnames = strdup(ifn); 7297c478bd9Sstevel@tonic-gate if (ifnames == NULL) 7307c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_GET_TAG, "", optnum, 7337c478bd9Sstevel@tonic-gate sizeof (dhcp_optnum_t), DHCP_TYPE_OPTNUM); 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate if (request == NULL) { 7367c478bd9Sstevel@tonic-gate free(ifnames_head); 7377c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate ifnames = strtok(ifnames, " "); 7417c478bd9Sstevel@tonic-gate if (ifnames == NULL) 7427c478bd9Sstevel@tonic-gate ifnames = ""; 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate for (; ifnames != NULL; ifnames = strtok(NULL, " ")) { 7457c478bd9Sstevel@tonic-gate 74665c8f1c0Smeem (void) strlcpy(request->ifname, ifnames, LIFNAMSIZ); 7477c478bd9Sstevel@tonic-gate retval = dhcp_ipc_make_request(request, &reply, 0); 7487c478bd9Sstevel@tonic-gate if (retval != 0) 7497c478bd9Sstevel@tonic-gate break; 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate if (reply->return_code == 0) { 7527c478bd9Sstevel@tonic-gate opt = dhcp_ipc_get_data(reply, &opt_size, NULL); 7537c478bd9Sstevel@tonic-gate if (opt_size > 2 && (opt->len == opt_size - 2)) { 7547c478bd9Sstevel@tonic-gate *result = malloc(opt_size); 7557c478bd9Sstevel@tonic-gate if (*result == NULL) 7567c478bd9Sstevel@tonic-gate retval = DHCP_IPC_E_MEMORY; 7577c478bd9Sstevel@tonic-gate else 7587c478bd9Sstevel@tonic-gate (void) memcpy(*result, opt, opt_size); 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate free(reply); 7617c478bd9Sstevel@tonic-gate break; 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate free(reply); 7667c478bd9Sstevel@tonic-gate if (ifnames[0] == '\0') 7677c478bd9Sstevel@tonic-gate break; 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate free(request); 7717c478bd9Sstevel@tonic-gate free(ifnames_head); 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate return (retval); 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate /* 7777c478bd9Sstevel@tonic-gate * get_ifnames(): returns a space-separated list of interface names that 7787c478bd9Sstevel@tonic-gate * match the specified flags 7797c478bd9Sstevel@tonic-gate * 7807c478bd9Sstevel@tonic-gate * input: int: flags which must be on in each interface returned 7817c478bd9Sstevel@tonic-gate * int: flags which must be off in each interface returned 7827c478bd9Sstevel@tonic-gate * output: char *: a dynamically-allocated list of interface names, or 7837c478bd9Sstevel@tonic-gate * NULL upon failure. 7847c478bd9Sstevel@tonic-gate */ 7857c478bd9Sstevel@tonic-gate 7867c478bd9Sstevel@tonic-gate static char * 7877c478bd9Sstevel@tonic-gate get_ifnames(int flags_on, int flags_off) 7887c478bd9Sstevel@tonic-gate { 7897c478bd9Sstevel@tonic-gate struct ifconf ifc; 7907c478bd9Sstevel@tonic-gate int n_ifs, i, sock_fd; 7917c478bd9Sstevel@tonic-gate char *ifnames; 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 7957c478bd9Sstevel@tonic-gate if (sock_fd == -1) 7967c478bd9Sstevel@tonic-gate return (NULL); 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate if ((ioctl(sock_fd, SIOCGIFNUM, &n_ifs) == -1) || (n_ifs <= 0)) { 7997c478bd9Sstevel@tonic-gate (void) close(sock_fd); 8007c478bd9Sstevel@tonic-gate return (NULL); 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate 80365c8f1c0Smeem ifnames = calloc(1, n_ifs * (LIFNAMSIZ + 1)); 8047c478bd9Sstevel@tonic-gate ifc.ifc_len = n_ifs * sizeof (struct ifreq); 8057c478bd9Sstevel@tonic-gate ifc.ifc_req = calloc(n_ifs, sizeof (struct ifreq)); 8067c478bd9Sstevel@tonic-gate if (ifc.ifc_req != NULL && ifnames != NULL) { 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate if (ioctl(sock_fd, SIOCGIFCONF, &ifc) == -1) { 8097c478bd9Sstevel@tonic-gate (void) close(sock_fd); 8107c478bd9Sstevel@tonic-gate free(ifnames); 8117c478bd9Sstevel@tonic-gate free(ifc.ifc_req); 8127c478bd9Sstevel@tonic-gate return (NULL); 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate for (i = 0; i < n_ifs; i++) { 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate if (ioctl(sock_fd, SIOCGIFFLAGS, &ifc.ifc_req[i]) == 0) 8187c478bd9Sstevel@tonic-gate if ((ifc.ifc_req[i].ifr_flags & 8197c478bd9Sstevel@tonic-gate (flags_on | flags_off)) != flags_on) 8207c478bd9Sstevel@tonic-gate continue; 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate (void) strcat(ifnames, ifc.ifc_req[i].ifr_name); 8237c478bd9Sstevel@tonic-gate (void) strcat(ifnames, " "); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate if (strlen(ifnames) > 1) 8277c478bd9Sstevel@tonic-gate ifnames[strlen(ifnames) - 1] = '\0'; 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate (void) close(sock_fd); 8317c478bd9Sstevel@tonic-gate free(ifc.ifc_req); 8327c478bd9Sstevel@tonic-gate return (ifnames); 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate 8357c478bd9Sstevel@tonic-gate /* 8367c478bd9Sstevel@tonic-gate * dhcp_ipc_getinfo(): attempts to retrieve a value for the specified DHCP 8377c478bd9Sstevel@tonic-gate * option; tries primary interface, then all DHCP-owned 8387c478bd9Sstevel@tonic-gate * interfaces, then INFORMs on the remaining interfaces 8397c478bd9Sstevel@tonic-gate * (these interfaces are dropped prior to returning). 8407c478bd9Sstevel@tonic-gate * input: dhcp_optnum_t *: a description of the desired option 8417c478bd9Sstevel@tonic-gate * DHCP_OPT **: filled in with the (dynamically allocated) value of 8427c478bd9Sstevel@tonic-gate * the option upon success. 8437c478bd9Sstevel@tonic-gate * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 8447c478bd9Sstevel@tonic-gate * or DHCP_IPC_WAIT_DEFAULT. 8457c478bd9Sstevel@tonic-gate * output: int: DHCP_IPC_E_* on error, 0 upon success. 8467c478bd9Sstevel@tonic-gate */ 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate int 8497c478bd9Sstevel@tonic-gate dhcp_ipc_getinfo(dhcp_optnum_t *optnum, DHCP_OPT **result, int32_t timeout) 8507c478bd9Sstevel@tonic-gate { 8517c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *request; 8527c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 8537c478bd9Sstevel@tonic-gate char *ifnames, *ifnames_copy, *ifnames_head; 8547c478bd9Sstevel@tonic-gate int retval; 8557c478bd9Sstevel@tonic-gate time_t start_time = time(NULL); 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate if (timeout == DHCP_IPC_WAIT_DEFAULT) 8587c478bd9Sstevel@tonic-gate timeout = DHCP_IPC_DEFAULT_WAIT; 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate /* 8617c478bd9Sstevel@tonic-gate * wait at most 5 seconds for the agent to start. 8627c478bd9Sstevel@tonic-gate */ 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate if (dhcp_start_agent((timeout > 5 || timeout < 0) ? 5 : timeout) == -1) 8657c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_INT); 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate /* 8687c478bd9Sstevel@tonic-gate * check the primary interface for the option value first. 8697c478bd9Sstevel@tonic-gate */ 8707c478bd9Sstevel@tonic-gate 8717c478bd9Sstevel@tonic-gate retval = getinfo_ifnames("", optnum, result); 8727c478bd9Sstevel@tonic-gate if ((retval != 0) || (retval == 0 && *result != NULL)) 8737c478bd9Sstevel@tonic-gate return (retval); 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * no luck. get a list of the interfaces under DHCP control 8777c478bd9Sstevel@tonic-gate * and perform a GET_TAG on each one. 8787c478bd9Sstevel@tonic-gate */ 8797c478bd9Sstevel@tonic-gate 8807c478bd9Sstevel@tonic-gate ifnames = get_ifnames(IFF_DHCPRUNNING, 0); 8817c478bd9Sstevel@tonic-gate if (ifnames != NULL && strlen(ifnames) != 0) { 8827c478bd9Sstevel@tonic-gate retval = getinfo_ifnames(ifnames, optnum, result); 8837c478bd9Sstevel@tonic-gate if ((retval != 0) || (retval == 0 && *result != NULL)) { 8847c478bd9Sstevel@tonic-gate free(ifnames); 8857c478bd9Sstevel@tonic-gate return (retval); 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate free(ifnames); 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate /* 8917c478bd9Sstevel@tonic-gate * still no luck. retrieve a list of all interfaces on the 8927c478bd9Sstevel@tonic-gate * system that could use DHCP but aren't. send INFORMs out on 8937c478bd9Sstevel@tonic-gate * each one. after that, sit in a loop for the next `timeout' 8947c478bd9Sstevel@tonic-gate * seconds, trying every second to see if a response for the 8957c478bd9Sstevel@tonic-gate * option we want has come in on one of the interfaces. 8967c478bd9Sstevel@tonic-gate */ 8977c478bd9Sstevel@tonic-gate 8987c478bd9Sstevel@tonic-gate ifnames = get_ifnames(IFF_UP|IFF_RUNNING, IFF_LOOPBACK|IFF_DHCPRUNNING); 8997c478bd9Sstevel@tonic-gate if (ifnames == NULL || strlen(ifnames) == 0) { 9007c478bd9Sstevel@tonic-gate free(ifnames); 9017c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_NOVALUE); 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate ifnames_head = ifnames_copy = strdup(ifnames); 9057c478bd9Sstevel@tonic-gate if (ifnames_copy == NULL) { 9067c478bd9Sstevel@tonic-gate free(ifnames); 9077c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_INFORM, "", NULL, 0, 9117c478bd9Sstevel@tonic-gate DHCP_TYPE_NONE); 9127c478bd9Sstevel@tonic-gate if (request == NULL) { 9137c478bd9Sstevel@tonic-gate free(ifnames); 9147c478bd9Sstevel@tonic-gate free(ifnames_head); 9157c478bd9Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate ifnames_copy = strtok(ifnames_copy, " "); 9197c478bd9Sstevel@tonic-gate for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) { 92065c8f1c0Smeem (void) strlcpy(request->ifname, ifnames_copy, LIFNAMSIZ); 9217c478bd9Sstevel@tonic-gate if (dhcp_ipc_make_request(request, &reply, 0) == 0) 9227c478bd9Sstevel@tonic-gate free(reply); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate for (;;) { 9267c478bd9Sstevel@tonic-gate if ((timeout != DHCP_IPC_WAIT_FOREVER) && 9277c478bd9Sstevel@tonic-gate (time(NULL) - start_time > timeout)) { 9287c478bd9Sstevel@tonic-gate retval = DHCP_IPC_E_TIMEOUT; 9297c478bd9Sstevel@tonic-gate break; 9307c478bd9Sstevel@tonic-gate } 9317c478bd9Sstevel@tonic-gate 9327c478bd9Sstevel@tonic-gate retval = getinfo_ifnames(ifnames, optnum, result); 9337c478bd9Sstevel@tonic-gate if (retval != 0 || (retval == 0 && *result != NULL)) 9347c478bd9Sstevel@tonic-gate break; 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate (void) sleep(1); 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate /* 9407c478bd9Sstevel@tonic-gate * drop any interfaces that weren't under DHCP control before 9417c478bd9Sstevel@tonic-gate * we got here; this keeps this function more of a black box 9427c478bd9Sstevel@tonic-gate * and the behavior more consistent from call to call. 9437c478bd9Sstevel@tonic-gate */ 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate request->message_type = DHCP_DROP; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate ifnames_copy = strcpy(ifnames_head, ifnames); 9487c478bd9Sstevel@tonic-gate ifnames_copy = strtok(ifnames_copy, " "); 9497c478bd9Sstevel@tonic-gate for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) { 95065c8f1c0Smeem (void) strlcpy(request->ifname, ifnames_copy, LIFNAMSIZ); 9517c478bd9Sstevel@tonic-gate if (dhcp_ipc_make_request(request, &reply, 0) == 0) 9527c478bd9Sstevel@tonic-gate free(reply); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate free(request); 9567c478bd9Sstevel@tonic-gate free(ifnames_head); 9577c478bd9Sstevel@tonic-gate free(ifnames); 9587c478bd9Sstevel@tonic-gate return (retval); 9597c478bd9Sstevel@tonic-gate } 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate /* 9627c478bd9Sstevel@tonic-gate * dhcp_ipc_timed_read(): reads from a descriptor using a maximum timeout 9637c478bd9Sstevel@tonic-gate * 9647c478bd9Sstevel@tonic-gate * input: int: the file descriptor to read from 9657c478bd9Sstevel@tonic-gate * void *: the buffer to read into 9667c478bd9Sstevel@tonic-gate * unsigned int: the total length of data to read 9677c478bd9Sstevel@tonic-gate * int *: the number of milliseconds to wait; the number of 968d04ccbb3Scarlsonj * milliseconds left are returned (-1 is "forever") 969d04ccbb3Scarlsonj * output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise 9707c478bd9Sstevel@tonic-gate */ 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate static int 9737c478bd9Sstevel@tonic-gate dhcp_ipc_timed_read(int fd, void *buffer, unsigned int length, int *msec) 9747c478bd9Sstevel@tonic-gate { 9757c478bd9Sstevel@tonic-gate unsigned int n_total = 0; 9767c478bd9Sstevel@tonic-gate ssize_t n_read; 9777c478bd9Sstevel@tonic-gate struct pollfd pollfd; 978d04ccbb3Scarlsonj hrtime_t start, end; 979d04ccbb3Scarlsonj int retv; 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate pollfd.fd = fd; 9827c478bd9Sstevel@tonic-gate pollfd.events = POLLIN; 9837c478bd9Sstevel@tonic-gate 9847c478bd9Sstevel@tonic-gate while (n_total < length) { 9857c478bd9Sstevel@tonic-gate 986d04ccbb3Scarlsonj start = gethrtime(); 9877c478bd9Sstevel@tonic-gate 988d04ccbb3Scarlsonj retv = poll(&pollfd, 1, *msec); 989d04ccbb3Scarlsonj if (retv == 0) { 990d04ccbb3Scarlsonj /* This can happen only if *msec is not -1 */ 9917c478bd9Sstevel@tonic-gate *msec = 0; 992d04ccbb3Scarlsonj return (DHCP_IPC_E_TIMEOUT); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 995d04ccbb3Scarlsonj if (*msec != -1) { 996d04ccbb3Scarlsonj end = gethrtime(); 997*19449258SJosef 'Jeff' Sipek *msec -= NSEC2MSEC(end - start); 998d04ccbb3Scarlsonj if (*msec < 0) 999d04ccbb3Scarlsonj *msec = 0; 1000d04ccbb3Scarlsonj } 10017c478bd9Sstevel@tonic-gate 1002d04ccbb3Scarlsonj if (retv == -1) { 1003d04ccbb3Scarlsonj if (errno != EINTR) 1004d04ccbb3Scarlsonj return (DHCP_IPC_E_POLL); 1005d04ccbb3Scarlsonj else if (*msec == 0) 1006d04ccbb3Scarlsonj return (DHCP_IPC_E_TIMEOUT); 1007d04ccbb3Scarlsonj continue; 1008d04ccbb3Scarlsonj } 1009d04ccbb3Scarlsonj 1010d04ccbb3Scarlsonj if (!(pollfd.revents & POLLIN)) { 1011d04ccbb3Scarlsonj errno = EINVAL; 1012d04ccbb3Scarlsonj return (DHCP_IPC_E_POLL); 1013d04ccbb3Scarlsonj } 1014d04ccbb3Scarlsonj 1015d04ccbb3Scarlsonj n_read = read(fd, (caddr_t)buffer + n_total, length - n_total); 1016d04ccbb3Scarlsonj 1017d04ccbb3Scarlsonj if (n_read == -1) { 1018d04ccbb3Scarlsonj if (errno != EINTR) 1019d04ccbb3Scarlsonj return (DHCP_IPC_E_READ); 1020d04ccbb3Scarlsonj else if (*msec == 0) 1021d04ccbb3Scarlsonj return (DHCP_IPC_E_TIMEOUT); 1022d04ccbb3Scarlsonj continue; 1023d04ccbb3Scarlsonj } 1024d04ccbb3Scarlsonj 1025d04ccbb3Scarlsonj if (n_read == 0) { 1026d04ccbb3Scarlsonj return (n_total == 0 ? DHCP_IPC_E_EOF : 1027d04ccbb3Scarlsonj DHCP_IPC_E_PROTO); 1028d04ccbb3Scarlsonj } 10297c478bd9Sstevel@tonic-gate 10307c478bd9Sstevel@tonic-gate n_total += n_read; 1031d04ccbb3Scarlsonj 1032d04ccbb3Scarlsonj if (*msec == 0 && n_total < length) 1033d04ccbb3Scarlsonj return (DHCP_IPC_E_TIMEOUT); 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate 1036d04ccbb3Scarlsonj return (DHCP_IPC_SUCCESS); 10377c478bd9Sstevel@tonic-gate } 1038