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
5*d04ccbb3Scarlsonj  * Common Development and Distribution License (the "License").
6*d04ccbb3Scarlsonj  * 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 /*
22*d04ccbb3Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <sys/uio.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
37*d04ccbb3Scarlsonj #include <netinet/tcp.h>
387c478bd9Sstevel@tonic-gate #include <net/if.h>
397c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
407c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
41*d04ccbb3Scarlsonj #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>		/* snprintf */
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>		/* ntohl, ntohs, etc */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "dhcpagent_ipc.h"
467c478bd9Sstevel@tonic-gate #include "dhcpagent_util.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * the protocol used here is a simple request/reply scheme: a client
507c478bd9Sstevel@tonic-gate  * sends a dhcp_ipc_request_t message to the agent, and the agent
517c478bd9Sstevel@tonic-gate  * sends a dhcp_ipc_reply_t back to the client.  since the requests
527c478bd9Sstevel@tonic-gate  * and replies can be variable-length, they are prefixed on "the wire"
537c478bd9Sstevel@tonic-gate  * by a 32-bit number that tells the other end how many bytes to
547c478bd9Sstevel@tonic-gate  * expect.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * the format of a request consists of a single dhcp_ipc_request_t;
577c478bd9Sstevel@tonic-gate  * note that the length of this dhcp_ipc_request_t is variable (using
587c478bd9Sstevel@tonic-gate  * the standard c array-of-size-1 trick).  the type of the payload is
597c478bd9Sstevel@tonic-gate  * given by `data_type', which is guaranteed to be `data_length' bytes
607c478bd9Sstevel@tonic-gate  * long starting at `buffer'.  note that `buffer' is guaranteed to be
617c478bd9Sstevel@tonic-gate  * 32-bit aligned but it is poor taste to rely on this.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * the format of a reply is much the same: a single dhcp_ipc_reply_t;
647c478bd9Sstevel@tonic-gate  * note again that the length of the dhcp_ipc_reply_t is variable.
657c478bd9Sstevel@tonic-gate  * the type of the payload is given by `data_type', which is
667c478bd9Sstevel@tonic-gate  * guaranteed to be `data_length' bytes long starting at `buffer'.
677c478bd9Sstevel@tonic-gate  * once again, note that `buffer' is guaranteed to be 32-bit aligned
687c478bd9Sstevel@tonic-gate  * but it is poor taste to rely on this.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * requests and replies can be paired up by comparing `ipc_id' fields.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	BUFMAX	256
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int	dhcp_ipc_timed_read(int, void *, unsigned int, int *);
767c478bd9Sstevel@tonic-gate static int	getinfo_ifnames(const char *, dhcp_optnum_t *, DHCP_OPT **);
777c478bd9Sstevel@tonic-gate static char	*get_ifnames(int, int);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * dhcp_ipc_alloc_request(): allocates a dhcp_ipc_request_t of the given type
817c478bd9Sstevel@tonic-gate  *			     and interface, with a timeout of 0.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  *   input: dhcp_ipc_type_t: the type of ipc request to allocate
847c478bd9Sstevel@tonic-gate  *	    const char *: the interface to associate the request with
85*d04ccbb3Scarlsonj  *	    const void *: the payload to send with the message (NULL if none)
867c478bd9Sstevel@tonic-gate  *	    uint32_t: the payload size (0 if none)
877c478bd9Sstevel@tonic-gate  *	    dhcp_data_type_t: the description of the type of payload
887c478bd9Sstevel@tonic-gate  *  output: dhcp_ipc_request_t *: the request on success, NULL on failure
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *
92*d04ccbb3Scarlsonj dhcp_ipc_alloc_request(dhcp_ipc_type_t type, const char *ifname,
93*d04ccbb3Scarlsonj     const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t *request = calloc(1, DHCP_IPC_REQUEST_SIZE +
967c478bd9Sstevel@tonic-gate 	    buffer_size);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (request == NULL)
997c478bd9Sstevel@tonic-gate 		return (NULL);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	request->message_type   = type;
1027c478bd9Sstevel@tonic-gate 	request->data_length    = buffer_size;
1037c478bd9Sstevel@tonic-gate 	request->data_type	= data_type;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (ifname != NULL)
1067c478bd9Sstevel@tonic-gate 		(void) strlcpy(request->ifname, ifname, IFNAMSIZ);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (buffer != NULL)
1097c478bd9Sstevel@tonic-gate 		(void) memcpy(request->buffer, buffer, buffer_size);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	return (request);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * dhcp_ipc_alloc_reply(): allocates a dhcp_ipc_reply_t
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  *   input: dhcp_ipc_request_t *: the request the reply is for
1187c478bd9Sstevel@tonic-gate  *	    int: the return code (0 for success, DHCP_IPC_E_* otherwise)
119*d04ccbb3Scarlsonj  *	    const void *: the payload to send with the message (NULL if none)
1207c478bd9Sstevel@tonic-gate  *	    uint32_t: the payload size (0 if none)
1217c478bd9Sstevel@tonic-gate  *	    dhcp_data_type_t: the description of the type of payload
1227c478bd9Sstevel@tonic-gate  *  output: dhcp_ipc_reply_t *: the reply on success, NULL on failure
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *
126*d04ccbb3Scarlsonj dhcp_ipc_alloc_reply(dhcp_ipc_request_t *request, int return_code,
127*d04ccbb3Scarlsonj     const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t *reply = calloc(1, DHCP_IPC_REPLY_SIZE + buffer_size);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (reply == NULL)
1327c478bd9Sstevel@tonic-gate 		return (NULL);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	reply->message_type	= request->message_type;
1357c478bd9Sstevel@tonic-gate 	reply->ipc_id		= request->ipc_id;
1367c478bd9Sstevel@tonic-gate 	reply->return_code	= return_code;
1377c478bd9Sstevel@tonic-gate 	reply->data_length	= buffer_size;
1387c478bd9Sstevel@tonic-gate 	reply->data_type	= data_type;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (buffer != NULL)
1417c478bd9Sstevel@tonic-gate 		(void) memcpy(reply->buffer, buffer, buffer_size);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return (reply);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * dhcp_ipc_get_data(): gets the data and data type from a dhcp_ipc_reply_t
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *   input: dhcp_ipc_reply_t *: the reply to get data from
1507c478bd9Sstevel@tonic-gate  *	    size_t *: the size of the resulting data
1517c478bd9Sstevel@tonic-gate  *	    dhcp_data_type_t *: the type of the message (returned)
1527c478bd9Sstevel@tonic-gate  *  output: void *: a pointer to the data, if there is any.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate void *
1567c478bd9Sstevel@tonic-gate dhcp_ipc_get_data(dhcp_ipc_reply_t *reply, size_t *size, dhcp_data_type_t *type)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	if (reply == NULL || reply->data_length == 0) {
1597c478bd9Sstevel@tonic-gate 		*size = 0;
1607c478bd9Sstevel@tonic-gate 		return (NULL);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (type != NULL)
1647c478bd9Sstevel@tonic-gate 		*type = reply->data_type;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	*size = reply->data_length;
1677c478bd9Sstevel@tonic-gate 	return (reply->buffer);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * dhcp_ipc_recv_msg(): gets a message using the agent's ipc protocol
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to get the message from
1747c478bd9Sstevel@tonic-gate  *	    void **: the address of a pointer to store the message
1757c478bd9Sstevel@tonic-gate  *		     (dynamically allocated)
1767c478bd9Sstevel@tonic-gate  *	    uint32_t: the minimum length of the packet
1777c478bd9Sstevel@tonic-gate  *	    int: the # of milliseconds to wait for the message (-1 is forever)
178*d04ccbb3Scarlsonj  *  output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static int
1827c478bd9Sstevel@tonic-gate dhcp_ipc_recv_msg(int fd, void **msg, uint32_t base_length, int msec)
1837c478bd9Sstevel@tonic-gate {
184*d04ccbb3Scarlsonj 	int			retval;
1857c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*ipc_msg;
1867c478bd9Sstevel@tonic-gate 	uint32_t		length;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	retval = dhcp_ipc_timed_read(fd, &length, sizeof (uint32_t), &msec);
189*d04ccbb3Scarlsonj 	if (retval != DHCP_IPC_SUCCESS)
190*d04ccbb3Scarlsonj 		return (retval);
191*d04ccbb3Scarlsonj 
192*d04ccbb3Scarlsonj 	if (length == 0)
193*d04ccbb3Scarlsonj 		return (DHCP_IPC_E_PROTO);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	*msg = malloc(length);
1967c478bd9Sstevel@tonic-gate 	if (*msg == NULL)
1977c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_MEMORY);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	retval = dhcp_ipc_timed_read(fd, *msg, length, &msec);
200*d04ccbb3Scarlsonj 	if (retval != DHCP_IPC_SUCCESS) {
2017c478bd9Sstevel@tonic-gate 		free(*msg);
202*d04ccbb3Scarlsonj 		return (retval);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (length < base_length) {
2067c478bd9Sstevel@tonic-gate 		free(*msg);
207*d04ccbb3Scarlsonj 		return (DHCP_IPC_E_PROTO);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * the data_length field is in the same place in either ipc message.
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	ipc_msg = (dhcp_ipc_reply_t *)(*msg);
2157c478bd9Sstevel@tonic-gate 	if (ipc_msg->data_length + base_length != length) {
2167c478bd9Sstevel@tonic-gate 		free(*msg);
217*d04ccbb3Scarlsonj 		return (DHCP_IPC_E_PROTO);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
220*d04ccbb3Scarlsonj 	return (DHCP_IPC_SUCCESS);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * dhcp_ipc_recv_request(): gets a request using the agent's ipc protocol
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to get the message from
2277c478bd9Sstevel@tonic-gate  *	    dhcp_ipc_request_t **: address of a pointer to store the request
2287c478bd9Sstevel@tonic-gate  *				 (dynamically allocated)
2297c478bd9Sstevel@tonic-gate  *	    int: the # of milliseconds to wait for the message (-1 is forever)
2307c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate int
2347c478bd9Sstevel@tonic-gate dhcp_ipc_recv_request(int fd, dhcp_ipc_request_t **request, int msec)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	int	retval;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	retval = dhcp_ipc_recv_msg(fd, (void **)request, DHCP_IPC_REQUEST_SIZE,
2397c478bd9Sstevel@tonic-gate 	    msec);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* guarantee that ifname will be NUL-terminated */
2427c478bd9Sstevel@tonic-gate 	if (retval == 0)
2437c478bd9Sstevel@tonic-gate 		(*request)->ifname[IFNAMSIZ - 1] = '\0';
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (retval);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * dhcp_ipc_recv_reply(): gets a reply using the agent's ipc protocol
2507c478bd9Sstevel@tonic-gate  *
2517c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to get the message from
2527c478bd9Sstevel@tonic-gate  *	    dhcp_ipc_reply_t **: address of a pointer to store the reply
2537c478bd9Sstevel@tonic-gate  *				 (dynamically allocated)
2547c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate static int
2587c478bd9Sstevel@tonic-gate dhcp_ipc_recv_reply(int fd, dhcp_ipc_reply_t **reply)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	return (dhcp_ipc_recv_msg(fd, (void **)reply, DHCP_IPC_REPLY_SIZE, -1));
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * dhcp_ipc_send_msg(): transmits a message using the agent's ipc protocol
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to transmit on
2677c478bd9Sstevel@tonic-gate  *	    void *: the message to send
2687c478bd9Sstevel@tonic-gate  *	    uint32_t: the message length
2697c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static int
2737c478bd9Sstevel@tonic-gate dhcp_ipc_send_msg(int fd, void *msg, uint32_t message_length)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	struct iovec	iovec[2];
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	iovec[0].iov_base = (caddr_t)&message_length;
2787c478bd9Sstevel@tonic-gate 	iovec[0].iov_len  = sizeof (uint32_t);
2797c478bd9Sstevel@tonic-gate 	iovec[1].iov_base = msg;
2807c478bd9Sstevel@tonic-gate 	iovec[1].iov_len  = message_length;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (writev(fd, iovec, sizeof (iovec) / sizeof (*iovec)) == -1)
2837c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_WRITEV);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	return (0);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * dhcp_ipc_send_reply(): transmits a reply using the agent's ipc protocol
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to transmit on
2927c478bd9Sstevel@tonic-gate  *	    dhcp_ipc_reply_t *: the reply to send
2937c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate int
2977c478bd9Sstevel@tonic-gate dhcp_ipc_send_reply(int fd, dhcp_ipc_reply_t *reply)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	return (dhcp_ipc_send_msg(fd, reply, DHCP_IPC_REPLY_SIZE +
3007c478bd9Sstevel@tonic-gate 	    reply->data_length));
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * dhcp_ipc_send_request(): transmits a request using the agent's ipc protocol
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to transmit on
3077c478bd9Sstevel@tonic-gate  *	    dhcp_ipc_request_t *: the request to send
3087c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate static int
3127c478bd9Sstevel@tonic-gate dhcp_ipc_send_request(int fd, dhcp_ipc_request_t *request)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * for now, ipc_ids aren't really used, but they're intended
3167c478bd9Sstevel@tonic-gate 	 * to make it easy to send several requests and then collect
3177c478bd9Sstevel@tonic-gate 	 * all of the replies (and pair them with the requests).
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	request->ipc_id = gethrtime();
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (dhcp_ipc_send_msg(fd, request, DHCP_IPC_REQUEST_SIZE +
3237c478bd9Sstevel@tonic-gate 	    request->data_length));
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * dhcp_ipc_make_request(): sends the provided request to the agent and reaps
3287c478bd9Sstevel@tonic-gate  *			    the reply
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  *   input: dhcp_ipc_request_t *: the request to make
3317c478bd9Sstevel@tonic-gate  *	    dhcp_ipc_reply_t **: the reply (dynamically allocated)
3327c478bd9Sstevel@tonic-gate  *	    int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER,
3337c478bd9Sstevel@tonic-gate  *		     or DHCP_IPC_WAIT_DEFAULT
3347c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
3387c478bd9Sstevel@tonic-gate dhcp_ipc_make_request(dhcp_ipc_request_t *request, dhcp_ipc_reply_t **reply,
3397c478bd9Sstevel@tonic-gate     int32_t timeout)
3407c478bd9Sstevel@tonic-gate {
341*d04ccbb3Scarlsonj 	int			fd, on, retval;
342*d04ccbb3Scarlsonj 	struct sockaddr_in	sinv;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_STREAM, 0);
3457c478bd9Sstevel@tonic-gate 	if (fd == -1)
3467c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_SOCKET);
347*d04ccbb3Scarlsonj 
348*d04ccbb3Scarlsonj 	/*
349*d04ccbb3Scarlsonj 	 * Bind a privileged port if we have sufficient privilege to do so.
350*d04ccbb3Scarlsonj 	 * Continue as non-privileged otherwise.
351*d04ccbb3Scarlsonj 	 */
352*d04ccbb3Scarlsonj 	on = 1;
353*d04ccbb3Scarlsonj 	(void) setsockopt(fd, IPPROTO_TCP, TCP_ANONPRIVBIND, &on, sizeof (on));
354*d04ccbb3Scarlsonj 
355*d04ccbb3Scarlsonj 	(void) memset(&sinv, 0, sizeof (sinv));
356*d04ccbb3Scarlsonj 	sinv.sin_family	 = AF_INET;
357*d04ccbb3Scarlsonj 	if (bind(fd, (struct sockaddr *)&sinv, sizeof (sinv)) == -1) {
358*d04ccbb3Scarlsonj 		(void) dhcp_ipc_close(fd);
359*d04ccbb3Scarlsonj 		return (DHCP_IPC_E_BIND);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
362*d04ccbb3Scarlsonj 	sinv.sin_port = htons(IPPORT_DHCPAGENT);
363*d04ccbb3Scarlsonj 	sinv.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
364*d04ccbb3Scarlsonj 	retval = connect(fd, (struct sockaddr *)&sinv, sizeof (sinv));
3657c478bd9Sstevel@tonic-gate 	if (retval == -1) {
3667c478bd9Sstevel@tonic-gate 		(void) dhcp_ipc_close(fd);
3677c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_CONNECT);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	request->timeout = timeout;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	retval = dhcp_ipc_send_request(fd, request);
3737c478bd9Sstevel@tonic-gate 	if (retval == 0)
3747c478bd9Sstevel@tonic-gate 		retval = dhcp_ipc_recv_reply(fd, reply);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	(void) dhcp_ipc_close(fd);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	return (retval);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * dhcp_ipc_init(): initializes the ipc channel for use by the agent
3837c478bd9Sstevel@tonic-gate  *
3847c478bd9Sstevel@tonic-gate  *   input: int *: the file descriptor to accept on (returned)
3857c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate int
3897c478bd9Sstevel@tonic-gate dhcp_ipc_init(int *listen_fd)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	struct sockaddr_in	sin;
3927c478bd9Sstevel@tonic-gate 	int			on = 1;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	(void) memset(&sin, 0, sizeof (struct sockaddr_in));
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	sin.sin_family		= AF_INET;
3977c478bd9Sstevel@tonic-gate 	sin.sin_port		= htons(IPPORT_DHCPAGENT);
3987c478bd9Sstevel@tonic-gate 	sin.sin_addr.s_addr	= htonl(INADDR_LOOPBACK);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	*listen_fd = socket(AF_INET, SOCK_STREAM, 0);
4017c478bd9Sstevel@tonic-gate 	if (*listen_fd == -1)
4027c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_SOCKET);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * we use SO_REUSEADDR here since in the case where there
4067c478bd9Sstevel@tonic-gate 	 * really is another daemon running that is using the agent's
4077c478bd9Sstevel@tonic-gate 	 * port, bind(3N) will fail.  so we can't lose.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	(void) setsockopt(*listen_fd, SOL_SOCKET, SO_REUSEADDR, &on,
4117c478bd9Sstevel@tonic-gate 	    sizeof (on));
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (bind(*listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) {
4147c478bd9Sstevel@tonic-gate 		(void) close(*listen_fd);
4157c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_BIND);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (listen(*listen_fd, DHCP_IPC_LISTEN_BACKLOG) == -1) {
4197c478bd9Sstevel@tonic-gate 		(void) close(*listen_fd);
4207c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_LISTEN);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	return (0);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * dhcp_ipc_accept(): accepts an incoming connection for the agent
4287c478bd9Sstevel@tonic-gate  *
4297c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to accept on
4307c478bd9Sstevel@tonic-gate  *	    int *: the accepted file descriptor (returned)
4317c478bd9Sstevel@tonic-gate  *	    int *: nonzero if the client is privileged (returned)
4327c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
4337c478bd9Sstevel@tonic-gate  *    note: sets the socket into nonblocking mode
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate int
4377c478bd9Sstevel@tonic-gate dhcp_ipc_accept(int listen_fd, int *fd, int *is_priv)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	struct sockaddr_in	sin_peer;
4407c478bd9Sstevel@tonic-gate 	int			sin_len = sizeof (sin_peer);
4417c478bd9Sstevel@tonic-gate 	int			sockflags;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * if we were extremely concerned with portability, we would
4457c478bd9Sstevel@tonic-gate 	 * set the socket into nonblocking mode before doing the
4467c478bd9Sstevel@tonic-gate 	 * accept(3N), since on BSD-based networking stacks, there is
4477c478bd9Sstevel@tonic-gate 	 * a potential race that can occur if the socket which
4487c478bd9Sstevel@tonic-gate 	 * connected to us performs a TCP RST before we accept, since
4497c478bd9Sstevel@tonic-gate 	 * BSD handles this case entirely in the kernel and as a
4507c478bd9Sstevel@tonic-gate 	 * result even though select said we will not block, we can
4517c478bd9Sstevel@tonic-gate 	 * end up blocking since there is no longer a connection to
4527c478bd9Sstevel@tonic-gate 	 * accept.  on SVR4-based systems, this should be okay,
4537c478bd9Sstevel@tonic-gate 	 * and we will get EPROTO back, even though POSIX.1g says
4547c478bd9Sstevel@tonic-gate 	 * we should get ECONNABORTED.
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	*fd = accept(listen_fd, (struct sockaddr *)&sin_peer, &sin_len);
4587c478bd9Sstevel@tonic-gate 	if (*fd == -1)
4597c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_ACCEPT);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/* get credentials */
4627c478bd9Sstevel@tonic-gate 	*is_priv = ntohs(sin_peer.sin_port) < IPPORT_RESERVED;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * kick the socket into non-blocking mode so that later
4667c478bd9Sstevel@tonic-gate 	 * operations on the socket don't block and hold up the whole
4677c478bd9Sstevel@tonic-gate 	 * application.  with the event demuxing approach, this may
4687c478bd9Sstevel@tonic-gate 	 * seem unnecessary, but in order to get partial reads/writes
4697c478bd9Sstevel@tonic-gate 	 * and to handle our internal protocol for passing data
4707c478bd9Sstevel@tonic-gate 	 * between the agent and its consumers, this is needed.
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if ((sockflags = fcntl(*fd, F_GETFL, 0)) == -1) {
4747c478bd9Sstevel@tonic-gate 		(void) close(*fd);
4757c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_FCNTL);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (fcntl(*fd, F_SETFL, sockflags | O_NONBLOCK) == -1) {
4797c478bd9Sstevel@tonic-gate 		(void) close(*fd);
4807c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_FCNTL);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (0);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * dhcp_ipc_close(): closes an ipc descriptor
4887c478bd9Sstevel@tonic-gate  *
4897c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to close
4907c478bd9Sstevel@tonic-gate  *  output: int: 0 on success, DHCP_IPC_E_* otherwise
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate int
4947c478bd9Sstevel@tonic-gate dhcp_ipc_close(int fd)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	return ((close(fd) == -1) ? DHCP_IPC_E_CLOSE : 0);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * dhcp_ipc_strerror(): maps an ipc error code into a human-readable string
5017c478bd9Sstevel@tonic-gate  *
5027c478bd9Sstevel@tonic-gate  *   input: int: the ipc error code to map
5037c478bd9Sstevel@tonic-gate  *  output: const char *: the corresponding human-readable string
5047c478bd9Sstevel@tonic-gate  */
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate const char *
5077c478bd9Sstevel@tonic-gate dhcp_ipc_strerror(int error)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	/* note: this must be kept in sync with DHCP_IPC_E_* definitions */
5107c478bd9Sstevel@tonic-gate 	const char *syscalls[] = {
5117c478bd9Sstevel@tonic-gate 		"<unknown>", "socket", "fcntl", "read", "accept", "close",
512*d04ccbb3Scarlsonj 		"bind", "listen", "malloc", "connect", "writev", "poll"
5137c478bd9Sstevel@tonic-gate 	};
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	const char	*error_string;
5167c478bd9Sstevel@tonic-gate 	static char	buffer[BUFMAX];
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	switch (error) {
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * none of these errors actually go over the wire.
5227c478bd9Sstevel@tonic-gate 	 * hence, we assume that errno is still fresh.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_SOCKET:			/* FALLTHRU */
5267c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_FCNTL:			/* FALLTHRU */
5277c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_READ:			/* FALLTHRU */
5287c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_ACCEPT:			/* FALLTHRU */
5297c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_CLOSE:			/* FALLTHRU */
5307c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_BIND:			/* FALLTHRU */
5317c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_LISTEN:			/* FALLTHRU */
5327c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_CONNECT:		/* FALLTHRU */
533*d04ccbb3Scarlsonj 	case DHCP_IPC_E_WRITEV:			/* FALLTHRU */
534*d04ccbb3Scarlsonj 	case DHCP_IPC_E_POLL:
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 		error_string = strerror(errno);
5377c478bd9Sstevel@tonic-gate 		if (error_string == NULL)
5387c478bd9Sstevel@tonic-gate 			error_string = "unknown error";
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer, sizeof (buffer), "%s: %s",
5417c478bd9Sstevel@tonic-gate 		    syscalls[error], error_string);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		error_string = buffer;
5447c478bd9Sstevel@tonic-gate 		break;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_MEMORY:
5477c478bd9Sstevel@tonic-gate 		error_string = "out of memory";
5487c478bd9Sstevel@tonic-gate 		break;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_TIMEOUT:
5517c478bd9Sstevel@tonic-gate 		error_string = "wait timed out, operation still pending...";
5527c478bd9Sstevel@tonic-gate 		break;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_INVIF:
5557c478bd9Sstevel@tonic-gate 		error_string = "interface does not exist or cannot be managed "
5567c478bd9Sstevel@tonic-gate 		    "using DHCP";
5577c478bd9Sstevel@tonic-gate 		break;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_INT:
5607c478bd9Sstevel@tonic-gate 		error_string = "internal error (might work later)";
5617c478bd9Sstevel@tonic-gate 		break;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_PERM:
5647c478bd9Sstevel@tonic-gate 		error_string = "permission denied";
5657c478bd9Sstevel@tonic-gate 		break;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_OUTSTATE:
5687c478bd9Sstevel@tonic-gate 		error_string = "interface not in appropriate state for command";
5697c478bd9Sstevel@tonic-gate 		break;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_PEND:
5727c478bd9Sstevel@tonic-gate 		error_string = "interface currently has a pending command "
5737c478bd9Sstevel@tonic-gate 		    "(try later)";
5747c478bd9Sstevel@tonic-gate 		break;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_BOOTP:
5777c478bd9Sstevel@tonic-gate 		error_string = "interface is administered with BOOTP, not DHCP";
5787c478bd9Sstevel@tonic-gate 		break;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_CMD_UNKNOWN:
5817c478bd9Sstevel@tonic-gate 		error_string = "unknown command";
5827c478bd9Sstevel@tonic-gate 		break;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_UNKIF:
5857c478bd9Sstevel@tonic-gate 		error_string = "interface is not under DHCP control";
5867c478bd9Sstevel@tonic-gate 		break;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_PROTO:
5897c478bd9Sstevel@tonic-gate 		error_string = "ipc protocol violation";
5907c478bd9Sstevel@tonic-gate 		break;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_FAILEDIF:
5937c478bd9Sstevel@tonic-gate 		error_string = "interface is in a FAILED state and must be "
5947c478bd9Sstevel@tonic-gate 		    "manually restarted";
5957c478bd9Sstevel@tonic-gate 		break;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_NOPRIMARY:
5987c478bd9Sstevel@tonic-gate 		error_string = "primary interface requested but no primary "
5997c478bd9Sstevel@tonic-gate 		    "interface is set";
6007c478bd9Sstevel@tonic-gate 		break;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_NOIPIF:
6037c478bd9Sstevel@tonic-gate 		error_string = "interface currently has no IP address";
6047c478bd9Sstevel@tonic-gate 		break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_DOWNIF:
6077c478bd9Sstevel@tonic-gate 		error_string = "interface is currently down";
6087c478bd9Sstevel@tonic-gate 		break;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_NOVALUE:
6117c478bd9Sstevel@tonic-gate 		error_string = "no value was found for this option";
6127c478bd9Sstevel@tonic-gate 		break;
6137c478bd9Sstevel@tonic-gate 
614*d04ccbb3Scarlsonj 	case DHCP_IPC_E_RUNNING:
615*d04ccbb3Scarlsonj 		error_string = "DHCP is already running";
616*d04ccbb3Scarlsonj 		break;
617*d04ccbb3Scarlsonj 
618*d04ccbb3Scarlsonj 	case DHCP_IPC_E_SRVFAILED:
619*d04ccbb3Scarlsonj 		error_string = "DHCP server refused request";
620*d04ccbb3Scarlsonj 		break;
621*d04ccbb3Scarlsonj 
622*d04ccbb3Scarlsonj 	case DHCP_IPC_E_EOF:
623*d04ccbb3Scarlsonj 		error_string = "ipc connection closed";
6247c478bd9Sstevel@tonic-gate 		break;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	default:
6277c478bd9Sstevel@tonic-gate 		error_string = "unknown error";
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * TODO: internationalize this error string
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	return (error_string);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
639*d04ccbb3Scarlsonj  * dhcp_ipc_type_to_string(): maps an ipc command code into a human-readable
640*d04ccbb3Scarlsonj  *			      string
641*d04ccbb3Scarlsonj  *
642*d04ccbb3Scarlsonj  *   input: int: the ipc command code to map
643*d04ccbb3Scarlsonj  *  output: const char *: the corresponding human-readable string
644*d04ccbb3Scarlsonj  */
645*d04ccbb3Scarlsonj 
646*d04ccbb3Scarlsonj const char *
647*d04ccbb3Scarlsonj dhcp_ipc_type_to_string(dhcp_ipc_type_t type)
648*d04ccbb3Scarlsonj {
649*d04ccbb3Scarlsonj 	static const char *typestr[] = {
650*d04ccbb3Scarlsonj 		"drop", "extend", "ping", "release", "start", "status",
651*d04ccbb3Scarlsonj 		"inform", "get_tag"
652*d04ccbb3Scarlsonj 	};
653*d04ccbb3Scarlsonj 
654*d04ccbb3Scarlsonj 	if (type < 0 || type >= DHCP_NIPC)
655*d04ccbb3Scarlsonj 		return ("unknown");
656*d04ccbb3Scarlsonj 	else
657*d04ccbb3Scarlsonj 		return (typestr[(int)type]);
658*d04ccbb3Scarlsonj }
659*d04ccbb3Scarlsonj 
660*d04ccbb3Scarlsonj /*
6617c478bd9Sstevel@tonic-gate  * getinfo_ifnames(): checks the value of a specified option on a list of
6627c478bd9Sstevel@tonic-gate  *		      interface names.
6637c478bd9Sstevel@tonic-gate  *   input: const char *: a list of interface names to query (in order) for
6647c478bd9Sstevel@tonic-gate  *			  the option; "" queries the primary interface
6657c478bd9Sstevel@tonic-gate  *	    dhcp_optnum_t *: a description of the desired option
6667c478bd9Sstevel@tonic-gate  *	    DHCP_OPT **:  filled in with the (dynamically allocated) value of
6677c478bd9Sstevel@tonic-gate  *			  the option upon success.
6687c478bd9Sstevel@tonic-gate  *  output: int: DHCP_IPC_E_* on error, 0 on success or if no value was
6697c478bd9Sstevel@tonic-gate  *	         found but no error occurred either (*result will be NULL)
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate static int
6737c478bd9Sstevel@tonic-gate getinfo_ifnames(const char *ifn, dhcp_optnum_t *optnum, DHCP_OPT **result)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
6767c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
6777c478bd9Sstevel@tonic-gate 	char			*ifnames, *ifnames_head;
6787c478bd9Sstevel@tonic-gate 	DHCP_OPT		*opt;
6797c478bd9Sstevel@tonic-gate 	size_t			opt_size;
6807c478bd9Sstevel@tonic-gate 	int			retval = 0;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	*result = NULL;
6837c478bd9Sstevel@tonic-gate 	ifnames_head = ifnames = strdup(ifn);
6847c478bd9Sstevel@tonic-gate 	if (ifnames == NULL)
6857c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_MEMORY);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(DHCP_GET_TAG, "", optnum,
6887c478bd9Sstevel@tonic-gate 	    sizeof (dhcp_optnum_t), DHCP_TYPE_OPTNUM);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (request == NULL) {
6917c478bd9Sstevel@tonic-gate 		free(ifnames_head);
6927c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_MEMORY);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	ifnames = strtok(ifnames, " ");
6967c478bd9Sstevel@tonic-gate 	if (ifnames == NULL)
6977c478bd9Sstevel@tonic-gate 		ifnames = "";
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	for (; ifnames != NULL; ifnames = strtok(NULL, " ")) {
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		(void) strlcpy(request->ifname, ifnames, IFNAMSIZ);
7027c478bd9Sstevel@tonic-gate 		retval = dhcp_ipc_make_request(request, &reply, 0);
7037c478bd9Sstevel@tonic-gate 		if (retval != 0)
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		if (reply->return_code == 0) {
7077c478bd9Sstevel@tonic-gate 			opt = dhcp_ipc_get_data(reply, &opt_size, NULL);
7087c478bd9Sstevel@tonic-gate 			if (opt_size > 2 && (opt->len == opt_size - 2)) {
7097c478bd9Sstevel@tonic-gate 				*result = malloc(opt_size);
7107c478bd9Sstevel@tonic-gate 				if (*result == NULL)
7117c478bd9Sstevel@tonic-gate 					retval = DHCP_IPC_E_MEMORY;
7127c478bd9Sstevel@tonic-gate 				else
7137c478bd9Sstevel@tonic-gate 					(void) memcpy(*result, opt, opt_size);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 				free(reply);
7167c478bd9Sstevel@tonic-gate 				break;
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		free(reply);
7217c478bd9Sstevel@tonic-gate 		if (ifnames[0] == '\0')
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	free(request);
7267c478bd9Sstevel@tonic-gate 	free(ifnames_head);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	return (retval);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate /*
7327c478bd9Sstevel@tonic-gate  * get_ifnames(): returns a space-separated list of interface names that
7337c478bd9Sstevel@tonic-gate  *		  match the specified flags
7347c478bd9Sstevel@tonic-gate  *
7357c478bd9Sstevel@tonic-gate  *   input: int: flags which must be on in each interface returned
7367c478bd9Sstevel@tonic-gate  *	    int: flags which must be off in each interface returned
7377c478bd9Sstevel@tonic-gate  *  output: char *: a dynamically-allocated list of interface names, or
7387c478bd9Sstevel@tonic-gate  *		    NULL upon failure.
7397c478bd9Sstevel@tonic-gate  */
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate static char *
7427c478bd9Sstevel@tonic-gate get_ifnames(int flags_on, int flags_off)
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	struct ifconf	ifc;
7457c478bd9Sstevel@tonic-gate 	int		n_ifs, i, sock_fd;
7467c478bd9Sstevel@tonic-gate 	char		*ifnames;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
7507c478bd9Sstevel@tonic-gate 	if (sock_fd == -1)
7517c478bd9Sstevel@tonic-gate 		return (NULL);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if ((ioctl(sock_fd, SIOCGIFNUM, &n_ifs) == -1) || (n_ifs <= 0)) {
7547c478bd9Sstevel@tonic-gate 		(void) close(sock_fd);
7557c478bd9Sstevel@tonic-gate 		return (NULL);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	ifnames = calloc(1, n_ifs * (IFNAMSIZ + 1));
7597c478bd9Sstevel@tonic-gate 	ifc.ifc_len = n_ifs * sizeof (struct ifreq);
7607c478bd9Sstevel@tonic-gate 	ifc.ifc_req = calloc(n_ifs, sizeof (struct ifreq));
7617c478bd9Sstevel@tonic-gate 	if (ifc.ifc_req != NULL && ifnames != NULL) {
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		if (ioctl(sock_fd, SIOCGIFCONF, &ifc) == -1) {
7647c478bd9Sstevel@tonic-gate 			(void) close(sock_fd);
7657c478bd9Sstevel@tonic-gate 			free(ifnames);
7667c478bd9Sstevel@tonic-gate 			free(ifc.ifc_req);
7677c478bd9Sstevel@tonic-gate 			return (NULL);
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		for (i = 0; i < n_ifs; i++) {
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 			if (ioctl(sock_fd, SIOCGIFFLAGS, &ifc.ifc_req[i]) == 0)
7737c478bd9Sstevel@tonic-gate 				if ((ifc.ifc_req[i].ifr_flags &
7747c478bd9Sstevel@tonic-gate 				    (flags_on | flags_off)) != flags_on)
7757c478bd9Sstevel@tonic-gate 					continue;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 			(void) strcat(ifnames, ifc.ifc_req[i].ifr_name);
7787c478bd9Sstevel@tonic-gate 			(void) strcat(ifnames, " ");
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		if (strlen(ifnames) > 1)
7827c478bd9Sstevel@tonic-gate 			ifnames[strlen(ifnames) - 1] = '\0';
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	(void) close(sock_fd);
7867c478bd9Sstevel@tonic-gate 	free(ifc.ifc_req);
7877c478bd9Sstevel@tonic-gate 	return (ifnames);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * dhcp_ipc_getinfo(): attempts to retrieve a value for the specified DHCP
7927c478bd9Sstevel@tonic-gate  *		       option; tries primary interface, then all DHCP-owned
7937c478bd9Sstevel@tonic-gate  *		       interfaces, then INFORMs on the remaining interfaces
7947c478bd9Sstevel@tonic-gate  *		       (these interfaces are dropped prior to returning).
7957c478bd9Sstevel@tonic-gate  *   input: dhcp_optnum_t *: a description of the desired option
7967c478bd9Sstevel@tonic-gate  *	    DHCP_OPT **:  filled in with the (dynamically allocated) value of
7977c478bd9Sstevel@tonic-gate  *			  the option upon success.
7987c478bd9Sstevel@tonic-gate  *	    int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER,
7997c478bd9Sstevel@tonic-gate  *		     or DHCP_IPC_WAIT_DEFAULT.
8007c478bd9Sstevel@tonic-gate  *  output: int: DHCP_IPC_E_* on error, 0 upon success.
8017c478bd9Sstevel@tonic-gate  */
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate int
8047c478bd9Sstevel@tonic-gate dhcp_ipc_getinfo(dhcp_optnum_t *optnum, DHCP_OPT **result, int32_t timeout)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
8077c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
8087c478bd9Sstevel@tonic-gate 	char			*ifnames, *ifnames_copy, *ifnames_head;
8097c478bd9Sstevel@tonic-gate 	int			retval;
8107c478bd9Sstevel@tonic-gate 	time_t			start_time = time(NULL);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (timeout == DHCP_IPC_WAIT_DEFAULT)
8137c478bd9Sstevel@tonic-gate 		timeout = DHCP_IPC_DEFAULT_WAIT;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/*
8167c478bd9Sstevel@tonic-gate 	 * wait at most 5 seconds for the agent to start.
8177c478bd9Sstevel@tonic-gate 	 */
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (dhcp_start_agent((timeout > 5 || timeout < 0) ? 5 : timeout) == -1)
8207c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_INT);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/*
8237c478bd9Sstevel@tonic-gate 	 * check the primary interface for the option value first.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	retval = getinfo_ifnames("", optnum, result);
8277c478bd9Sstevel@tonic-gate 	if ((retval != 0) || (retval == 0 && *result != NULL))
8287c478bd9Sstevel@tonic-gate 		return (retval);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/*
8317c478bd9Sstevel@tonic-gate 	 * no luck.  get a list of the interfaces under DHCP control
8327c478bd9Sstevel@tonic-gate 	 * and perform a GET_TAG on each one.
8337c478bd9Sstevel@tonic-gate 	 */
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	ifnames = get_ifnames(IFF_DHCPRUNNING, 0);
8367c478bd9Sstevel@tonic-gate 	if (ifnames != NULL && strlen(ifnames) != 0) {
8377c478bd9Sstevel@tonic-gate 		retval = getinfo_ifnames(ifnames, optnum, result);
8387c478bd9Sstevel@tonic-gate 		if ((retval != 0) || (retval == 0 && *result != NULL)) {
8397c478bd9Sstevel@tonic-gate 			free(ifnames);
8407c478bd9Sstevel@tonic-gate 			return (retval);
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	free(ifnames);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	/*
8467c478bd9Sstevel@tonic-gate 	 * still no luck.  retrieve a list of all interfaces on the
8477c478bd9Sstevel@tonic-gate 	 * system that could use DHCP but aren't.  send INFORMs out on
8487c478bd9Sstevel@tonic-gate 	 * each one. after that, sit in a loop for the next `timeout'
8497c478bd9Sstevel@tonic-gate 	 * seconds, trying every second to see if a response for the
8507c478bd9Sstevel@tonic-gate 	 * option we want has come in on one of the interfaces.
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	ifnames = get_ifnames(IFF_UP|IFF_RUNNING, IFF_LOOPBACK|IFF_DHCPRUNNING);
8547c478bd9Sstevel@tonic-gate 	if (ifnames == NULL || strlen(ifnames) == 0) {
8557c478bd9Sstevel@tonic-gate 		free(ifnames);
8567c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_NOVALUE);
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	ifnames_head = ifnames_copy = strdup(ifnames);
8607c478bd9Sstevel@tonic-gate 	if (ifnames_copy == NULL) {
8617c478bd9Sstevel@tonic-gate 		free(ifnames);
8627c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_MEMORY);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(DHCP_INFORM, "", NULL, 0,
8667c478bd9Sstevel@tonic-gate 	    DHCP_TYPE_NONE);
8677c478bd9Sstevel@tonic-gate 	if (request == NULL) {
8687c478bd9Sstevel@tonic-gate 		free(ifnames);
8697c478bd9Sstevel@tonic-gate 		free(ifnames_head);
8707c478bd9Sstevel@tonic-gate 		return (DHCP_IPC_E_MEMORY);
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	ifnames_copy = strtok(ifnames_copy, " ");
8747c478bd9Sstevel@tonic-gate 	for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) {
8757c478bd9Sstevel@tonic-gate 		(void) strlcpy(request->ifname, ifnames_copy, IFNAMSIZ);
8767c478bd9Sstevel@tonic-gate 		if (dhcp_ipc_make_request(request, &reply, 0) == 0)
8777c478bd9Sstevel@tonic-gate 			free(reply);
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	for (;;) {
8817c478bd9Sstevel@tonic-gate 		if ((timeout != DHCP_IPC_WAIT_FOREVER) &&
8827c478bd9Sstevel@tonic-gate 		    (time(NULL) - start_time > timeout)) {
8837c478bd9Sstevel@tonic-gate 			retval = DHCP_IPC_E_TIMEOUT;
8847c478bd9Sstevel@tonic-gate 			break;
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		retval = getinfo_ifnames(ifnames, optnum, result);
8887c478bd9Sstevel@tonic-gate 		if (retval != 0 || (retval == 0 && *result != NULL))
8897c478bd9Sstevel@tonic-gate 			break;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		(void) sleep(1);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	/*
8957c478bd9Sstevel@tonic-gate 	 * drop any interfaces that weren't under DHCP control before
8967c478bd9Sstevel@tonic-gate 	 * we got here; this keeps this function more of a black box
8977c478bd9Sstevel@tonic-gate 	 * and the behavior more consistent from call to call.
8987c478bd9Sstevel@tonic-gate 	 */
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	request->message_type = DHCP_DROP;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	ifnames_copy = strcpy(ifnames_head, ifnames);
9037c478bd9Sstevel@tonic-gate 	ifnames_copy = strtok(ifnames_copy, " ");
9047c478bd9Sstevel@tonic-gate 	for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) {
9057c478bd9Sstevel@tonic-gate 		(void) strlcpy(request->ifname, ifnames_copy, IFNAMSIZ);
9067c478bd9Sstevel@tonic-gate 		if (dhcp_ipc_make_request(request, &reply, 0) == 0)
9077c478bd9Sstevel@tonic-gate 			free(reply);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	free(request);
9117c478bd9Sstevel@tonic-gate 	free(ifnames_head);
9127c478bd9Sstevel@tonic-gate 	free(ifnames);
9137c478bd9Sstevel@tonic-gate 	return (retval);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate /*
9177c478bd9Sstevel@tonic-gate  * dhcp_ipc_timed_read(): reads from a descriptor using a maximum timeout
9187c478bd9Sstevel@tonic-gate  *
9197c478bd9Sstevel@tonic-gate  *   input: int: the file descriptor to read from
9207c478bd9Sstevel@tonic-gate  *	    void *: the buffer to read into
9217c478bd9Sstevel@tonic-gate  *	    unsigned int: the total length of data to read
9227c478bd9Sstevel@tonic-gate  *	    int *: the number of milliseconds to wait; the number of
923*d04ccbb3Scarlsonj  *		   milliseconds left are returned (-1 is "forever")
924*d04ccbb3Scarlsonj  *  output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise
9257c478bd9Sstevel@tonic-gate  */
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate static int
9287c478bd9Sstevel@tonic-gate dhcp_ipc_timed_read(int fd, void *buffer, unsigned int length, int *msec)
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate 	unsigned int	n_total = 0;
9317c478bd9Sstevel@tonic-gate 	ssize_t		n_read;
9327c478bd9Sstevel@tonic-gate 	struct pollfd	pollfd;
933*d04ccbb3Scarlsonj 	hrtime_t	start, end;
934*d04ccbb3Scarlsonj 	int		retv;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	pollfd.fd	= fd;
9377c478bd9Sstevel@tonic-gate 	pollfd.events	= POLLIN;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	while (n_total < length) {
9407c478bd9Sstevel@tonic-gate 
941*d04ccbb3Scarlsonj 		start = gethrtime();
9427c478bd9Sstevel@tonic-gate 
943*d04ccbb3Scarlsonj 		retv = poll(&pollfd, 1, *msec);
944*d04ccbb3Scarlsonj 		if (retv == 0) {
945*d04ccbb3Scarlsonj 			/* This can happen only if *msec is not -1 */
9467c478bd9Sstevel@tonic-gate 			*msec = 0;
947*d04ccbb3Scarlsonj 			return (DHCP_IPC_E_TIMEOUT);
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 
950*d04ccbb3Scarlsonj 		if (*msec != -1) {
951*d04ccbb3Scarlsonj 			end = gethrtime();
952*d04ccbb3Scarlsonj 			*msec -= (end - start) / (NANOSEC / MILLISEC);
953*d04ccbb3Scarlsonj 			if (*msec < 0)
954*d04ccbb3Scarlsonj 				*msec = 0;
955*d04ccbb3Scarlsonj 		}
9567c478bd9Sstevel@tonic-gate 
957*d04ccbb3Scarlsonj 		if (retv == -1) {
958*d04ccbb3Scarlsonj 			if (errno != EINTR)
959*d04ccbb3Scarlsonj 				return (DHCP_IPC_E_POLL);
960*d04ccbb3Scarlsonj 			else if (*msec == 0)
961*d04ccbb3Scarlsonj 				return (DHCP_IPC_E_TIMEOUT);
962*d04ccbb3Scarlsonj 			continue;
963*d04ccbb3Scarlsonj 		}
964*d04ccbb3Scarlsonj 
965*d04ccbb3Scarlsonj 		if (!(pollfd.revents & POLLIN)) {
966*d04ccbb3Scarlsonj 			errno = EINVAL;
967*d04ccbb3Scarlsonj 			return (DHCP_IPC_E_POLL);
968*d04ccbb3Scarlsonj 		}
969*d04ccbb3Scarlsonj 
970*d04ccbb3Scarlsonj 		n_read = read(fd, (caddr_t)buffer + n_total, length - n_total);
971*d04ccbb3Scarlsonj 
972*d04ccbb3Scarlsonj 		if (n_read == -1) {
973*d04ccbb3Scarlsonj 			if (errno != EINTR)
974*d04ccbb3Scarlsonj 				return (DHCP_IPC_E_READ);
975*d04ccbb3Scarlsonj 			else if (*msec == 0)
976*d04ccbb3Scarlsonj 				return (DHCP_IPC_E_TIMEOUT);
977*d04ccbb3Scarlsonj 			continue;
978*d04ccbb3Scarlsonj 		}
979*d04ccbb3Scarlsonj 
980*d04ccbb3Scarlsonj 		if (n_read == 0) {
981*d04ccbb3Scarlsonj 			return (n_total == 0 ? DHCP_IPC_E_EOF :
982*d04ccbb3Scarlsonj 			    DHCP_IPC_E_PROTO);
983*d04ccbb3Scarlsonj 		}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		n_total += n_read;
986*d04ccbb3Scarlsonj 
987*d04ccbb3Scarlsonj 		if (*msec == 0 && n_total < length)
988*d04ccbb3Scarlsonj 			return (DHCP_IPC_E_TIMEOUT);
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
991*d04ccbb3Scarlsonj 	return (DHCP_IPC_SUCCESS);
9927c478bd9Sstevel@tonic-gate }
993