10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*3431Scarlsonj * Common Development and Distribution License (the "License"). 6*3431Scarlsonj * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <string.h> 290Sstevel@tonic-gate #include <unistd.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <sys/uio.h> 320Sstevel@tonic-gate #include <sys/socket.h> 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <fcntl.h> 350Sstevel@tonic-gate #include <errno.h> 360Sstevel@tonic-gate #include <netinet/in.h> 37*3431Scarlsonj #include <netinet/tcp.h> 380Sstevel@tonic-gate #include <net/if.h> 390Sstevel@tonic-gate #include <sys/sockio.h> 400Sstevel@tonic-gate #include <sys/fcntl.h> 41*3431Scarlsonj #include <sys/time.h> 420Sstevel@tonic-gate #include <stdio.h> /* snprintf */ 430Sstevel@tonic-gate #include <arpa/inet.h> /* ntohl, ntohs, etc */ 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "dhcpagent_ipc.h" 460Sstevel@tonic-gate #include "dhcpagent_util.h" 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* 490Sstevel@tonic-gate * the protocol used here is a simple request/reply scheme: a client 500Sstevel@tonic-gate * sends a dhcp_ipc_request_t message to the agent, and the agent 510Sstevel@tonic-gate * sends a dhcp_ipc_reply_t back to the client. since the requests 520Sstevel@tonic-gate * and replies can be variable-length, they are prefixed on "the wire" 530Sstevel@tonic-gate * by a 32-bit number that tells the other end how many bytes to 540Sstevel@tonic-gate * expect. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * the format of a request consists of a single dhcp_ipc_request_t; 570Sstevel@tonic-gate * note that the length of this dhcp_ipc_request_t is variable (using 580Sstevel@tonic-gate * the standard c array-of-size-1 trick). the type of the payload is 590Sstevel@tonic-gate * given by `data_type', which is guaranteed to be `data_length' bytes 600Sstevel@tonic-gate * long starting at `buffer'. note that `buffer' is guaranteed to be 610Sstevel@tonic-gate * 32-bit aligned but it is poor taste to rely on this. 620Sstevel@tonic-gate * 630Sstevel@tonic-gate * the format of a reply is much the same: a single dhcp_ipc_reply_t; 640Sstevel@tonic-gate * note again that the length of the dhcp_ipc_reply_t is variable. 650Sstevel@tonic-gate * the type of the payload is given by `data_type', which is 660Sstevel@tonic-gate * guaranteed to be `data_length' bytes long starting at `buffer'. 670Sstevel@tonic-gate * once again, note that `buffer' is guaranteed to be 32-bit aligned 680Sstevel@tonic-gate * but it is poor taste to rely on this. 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * requests and replies can be paired up by comparing `ipc_id' fields. 710Sstevel@tonic-gate */ 720Sstevel@tonic-gate 730Sstevel@tonic-gate #define BUFMAX 256 740Sstevel@tonic-gate 750Sstevel@tonic-gate static int dhcp_ipc_timed_read(int, void *, unsigned int, int *); 760Sstevel@tonic-gate static int getinfo_ifnames(const char *, dhcp_optnum_t *, DHCP_OPT **); 770Sstevel@tonic-gate static char *get_ifnames(int, int); 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * dhcp_ipc_alloc_request(): allocates a dhcp_ipc_request_t of the given type 810Sstevel@tonic-gate * and interface, with a timeout of 0. 820Sstevel@tonic-gate * 830Sstevel@tonic-gate * input: dhcp_ipc_type_t: the type of ipc request to allocate 840Sstevel@tonic-gate * const char *: the interface to associate the request with 85*3431Scarlsonj * const void *: the payload to send with the message (NULL if none) 860Sstevel@tonic-gate * uint32_t: the payload size (0 if none) 870Sstevel@tonic-gate * dhcp_data_type_t: the description of the type of payload 880Sstevel@tonic-gate * output: dhcp_ipc_request_t *: the request on success, NULL on failure 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate 910Sstevel@tonic-gate dhcp_ipc_request_t * 92*3431Scarlsonj dhcp_ipc_alloc_request(dhcp_ipc_type_t type, const char *ifname, 93*3431Scarlsonj const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type) 940Sstevel@tonic-gate { 950Sstevel@tonic-gate dhcp_ipc_request_t *request = calloc(1, DHCP_IPC_REQUEST_SIZE + 960Sstevel@tonic-gate buffer_size); 970Sstevel@tonic-gate 980Sstevel@tonic-gate if (request == NULL) 990Sstevel@tonic-gate return (NULL); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate request->message_type = type; 1020Sstevel@tonic-gate request->data_length = buffer_size; 1030Sstevel@tonic-gate request->data_type = data_type; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if (ifname != NULL) 1060Sstevel@tonic-gate (void) strlcpy(request->ifname, ifname, IFNAMSIZ); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if (buffer != NULL) 1090Sstevel@tonic-gate (void) memcpy(request->buffer, buffer, buffer_size); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate return (request); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * dhcp_ipc_alloc_reply(): allocates a dhcp_ipc_reply_t 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * input: dhcp_ipc_request_t *: the request the reply is for 1180Sstevel@tonic-gate * int: the return code (0 for success, DHCP_IPC_E_* otherwise) 119*3431Scarlsonj * const void *: the payload to send with the message (NULL if none) 1200Sstevel@tonic-gate * uint32_t: the payload size (0 if none) 1210Sstevel@tonic-gate * dhcp_data_type_t: the description of the type of payload 1220Sstevel@tonic-gate * output: dhcp_ipc_reply_t *: the reply on success, NULL on failure 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate dhcp_ipc_reply_t * 126*3431Scarlsonj dhcp_ipc_alloc_reply(dhcp_ipc_request_t *request, int return_code, 127*3431Scarlsonj const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type) 1280Sstevel@tonic-gate { 1290Sstevel@tonic-gate dhcp_ipc_reply_t *reply = calloc(1, DHCP_IPC_REPLY_SIZE + buffer_size); 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate if (reply == NULL) 1320Sstevel@tonic-gate return (NULL); 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate reply->message_type = request->message_type; 1350Sstevel@tonic-gate reply->ipc_id = request->ipc_id; 1360Sstevel@tonic-gate reply->return_code = return_code; 1370Sstevel@tonic-gate reply->data_length = buffer_size; 1380Sstevel@tonic-gate reply->data_type = data_type; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate if (buffer != NULL) 1410Sstevel@tonic-gate (void) memcpy(reply->buffer, buffer, buffer_size); 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate return (reply); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * dhcp_ipc_get_data(): gets the data and data type from a dhcp_ipc_reply_t 1480Sstevel@tonic-gate * 1490Sstevel@tonic-gate * input: dhcp_ipc_reply_t *: the reply to get data from 1500Sstevel@tonic-gate * size_t *: the size of the resulting data 1510Sstevel@tonic-gate * dhcp_data_type_t *: the type of the message (returned) 1520Sstevel@tonic-gate * output: void *: a pointer to the data, if there is any. 1530Sstevel@tonic-gate */ 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate void * 1560Sstevel@tonic-gate dhcp_ipc_get_data(dhcp_ipc_reply_t *reply, size_t *size, dhcp_data_type_t *type) 1570Sstevel@tonic-gate { 1580Sstevel@tonic-gate if (reply == NULL || reply->data_length == 0) { 1590Sstevel@tonic-gate *size = 0; 1600Sstevel@tonic-gate return (NULL); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (type != NULL) 1640Sstevel@tonic-gate *type = reply->data_type; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate *size = reply->data_length; 1670Sstevel@tonic-gate return (reply->buffer); 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * dhcp_ipc_recv_msg(): gets a message using the agent's ipc protocol 1720Sstevel@tonic-gate * 1730Sstevel@tonic-gate * input: int: the file descriptor to get the message from 1740Sstevel@tonic-gate * void **: the address of a pointer to store the message 1750Sstevel@tonic-gate * (dynamically allocated) 1760Sstevel@tonic-gate * uint32_t: the minimum length of the packet 1770Sstevel@tonic-gate * int: the # of milliseconds to wait for the message (-1 is forever) 178*3431Scarlsonj * output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate static int 1820Sstevel@tonic-gate dhcp_ipc_recv_msg(int fd, void **msg, uint32_t base_length, int msec) 1830Sstevel@tonic-gate { 184*3431Scarlsonj int retval; 1850Sstevel@tonic-gate dhcp_ipc_reply_t *ipc_msg; 1860Sstevel@tonic-gate uint32_t length; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate retval = dhcp_ipc_timed_read(fd, &length, sizeof (uint32_t), &msec); 189*3431Scarlsonj if (retval != DHCP_IPC_SUCCESS) 190*3431Scarlsonj return (retval); 191*3431Scarlsonj 192*3431Scarlsonj if (length == 0) 193*3431Scarlsonj return (DHCP_IPC_E_PROTO); 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate *msg = malloc(length); 1960Sstevel@tonic-gate if (*msg == NULL) 1970Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate retval = dhcp_ipc_timed_read(fd, *msg, length, &msec); 200*3431Scarlsonj if (retval != DHCP_IPC_SUCCESS) { 2010Sstevel@tonic-gate free(*msg); 202*3431Scarlsonj return (retval); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate if (length < base_length) { 2060Sstevel@tonic-gate free(*msg); 207*3431Scarlsonj return (DHCP_IPC_E_PROTO); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * the data_length field is in the same place in either ipc message. 2120Sstevel@tonic-gate */ 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate ipc_msg = (dhcp_ipc_reply_t *)(*msg); 2150Sstevel@tonic-gate if (ipc_msg->data_length + base_length != length) { 2160Sstevel@tonic-gate free(*msg); 217*3431Scarlsonj return (DHCP_IPC_E_PROTO); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 220*3431Scarlsonj return (DHCP_IPC_SUCCESS); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* 2240Sstevel@tonic-gate * dhcp_ipc_recv_request(): gets a request using the agent's ipc protocol 2250Sstevel@tonic-gate * 2260Sstevel@tonic-gate * input: int: the file descriptor to get the message from 2270Sstevel@tonic-gate * dhcp_ipc_request_t **: address of a pointer to store the request 2280Sstevel@tonic-gate * (dynamically allocated) 2290Sstevel@tonic-gate * int: the # of milliseconds to wait for the message (-1 is forever) 2300Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate int 2340Sstevel@tonic-gate dhcp_ipc_recv_request(int fd, dhcp_ipc_request_t **request, int msec) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate int retval; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate retval = dhcp_ipc_recv_msg(fd, (void **)request, DHCP_IPC_REQUEST_SIZE, 2390Sstevel@tonic-gate msec); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* guarantee that ifname will be NUL-terminated */ 2420Sstevel@tonic-gate if (retval == 0) 2430Sstevel@tonic-gate (*request)->ifname[IFNAMSIZ - 1] = '\0'; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate return (retval); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate /* 2490Sstevel@tonic-gate * dhcp_ipc_recv_reply(): gets a reply using the agent's ipc protocol 2500Sstevel@tonic-gate * 2510Sstevel@tonic-gate * input: int: the file descriptor to get the message from 2520Sstevel@tonic-gate * dhcp_ipc_reply_t **: address of a pointer to store the reply 2530Sstevel@tonic-gate * (dynamically allocated) 2540Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate static int 2580Sstevel@tonic-gate dhcp_ipc_recv_reply(int fd, dhcp_ipc_reply_t **reply) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate return (dhcp_ipc_recv_msg(fd, (void **)reply, DHCP_IPC_REPLY_SIZE, -1)); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * dhcp_ipc_send_msg(): transmits a message using the agent's ipc protocol 2650Sstevel@tonic-gate * 2660Sstevel@tonic-gate * input: int: the file descriptor to transmit on 2670Sstevel@tonic-gate * void *: the message to send 2680Sstevel@tonic-gate * uint32_t: the message length 2690Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate static int 2730Sstevel@tonic-gate dhcp_ipc_send_msg(int fd, void *msg, uint32_t message_length) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate struct iovec iovec[2]; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate iovec[0].iov_base = (caddr_t)&message_length; 2780Sstevel@tonic-gate iovec[0].iov_len = sizeof (uint32_t); 2790Sstevel@tonic-gate iovec[1].iov_base = msg; 2800Sstevel@tonic-gate iovec[1].iov_len = message_length; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if (writev(fd, iovec, sizeof (iovec) / sizeof (*iovec)) == -1) 2830Sstevel@tonic-gate return (DHCP_IPC_E_WRITEV); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate return (0); 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * dhcp_ipc_send_reply(): transmits a reply using the agent's ipc protocol 2900Sstevel@tonic-gate * 2910Sstevel@tonic-gate * input: int: the file descriptor to transmit on 2920Sstevel@tonic-gate * dhcp_ipc_reply_t *: the reply to send 2930Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate int 2970Sstevel@tonic-gate dhcp_ipc_send_reply(int fd, dhcp_ipc_reply_t *reply) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate return (dhcp_ipc_send_msg(fd, reply, DHCP_IPC_REPLY_SIZE + 3000Sstevel@tonic-gate reply->data_length)); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * dhcp_ipc_send_request(): transmits a request using the agent's ipc protocol 3050Sstevel@tonic-gate * 3060Sstevel@tonic-gate * input: int: the file descriptor to transmit on 3070Sstevel@tonic-gate * dhcp_ipc_request_t *: the request to send 3080Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate static int 3120Sstevel@tonic-gate dhcp_ipc_send_request(int fd, dhcp_ipc_request_t *request) 3130Sstevel@tonic-gate { 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * for now, ipc_ids aren't really used, but they're intended 3160Sstevel@tonic-gate * to make it easy to send several requests and then collect 3170Sstevel@tonic-gate * all of the replies (and pair them with the requests). 3180Sstevel@tonic-gate */ 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate request->ipc_id = gethrtime(); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate return (dhcp_ipc_send_msg(fd, request, DHCP_IPC_REQUEST_SIZE + 3230Sstevel@tonic-gate request->data_length)); 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate /* 3270Sstevel@tonic-gate * dhcp_ipc_make_request(): sends the provided request to the agent and reaps 3280Sstevel@tonic-gate * the reply 3290Sstevel@tonic-gate * 3300Sstevel@tonic-gate * input: dhcp_ipc_request_t *: the request to make 3310Sstevel@tonic-gate * dhcp_ipc_reply_t **: the reply (dynamically allocated) 3320Sstevel@tonic-gate * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 3330Sstevel@tonic-gate * or DHCP_IPC_WAIT_DEFAULT 3340Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate int 3380Sstevel@tonic-gate dhcp_ipc_make_request(dhcp_ipc_request_t *request, dhcp_ipc_reply_t **reply, 3390Sstevel@tonic-gate int32_t timeout) 3400Sstevel@tonic-gate { 341*3431Scarlsonj int fd, on, retval; 342*3431Scarlsonj struct sockaddr_in sinv; 3430Sstevel@tonic-gate 344*3431Scarlsonj fd = socket(AF_INET, SOCK_STREAM, 0); 345*3431Scarlsonj if (fd == -1) 346*3431Scarlsonj return (DHCP_IPC_E_SOCKET); 3470Sstevel@tonic-gate 348*3431Scarlsonj /* 349*3431Scarlsonj * Bind a privileged port if we have sufficient privilege to do so. 350*3431Scarlsonj * Continue as non-privileged otherwise. 351*3431Scarlsonj */ 352*3431Scarlsonj on = 1; 353*3431Scarlsonj (void) setsockopt(fd, IPPROTO_TCP, TCP_ANONPRIVBIND, &on, sizeof (on)); 3540Sstevel@tonic-gate 355*3431Scarlsonj (void) memset(&sinv, 0, sizeof (sinv)); 356*3431Scarlsonj sinv.sin_family = AF_INET; 357*3431Scarlsonj if (bind(fd, (struct sockaddr *)&sinv, sizeof (sinv)) == -1) { 358*3431Scarlsonj (void) dhcp_ipc_close(fd); 359*3431Scarlsonj return (DHCP_IPC_E_BIND); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 362*3431Scarlsonj sinv.sin_port = htons(IPPORT_DHCPAGENT); 363*3431Scarlsonj sinv.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 364*3431Scarlsonj retval = connect(fd, (struct sockaddr *)&sinv, sizeof (sinv)); 3650Sstevel@tonic-gate if (retval == -1) { 3660Sstevel@tonic-gate (void) dhcp_ipc_close(fd); 3670Sstevel@tonic-gate return (DHCP_IPC_E_CONNECT); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate request->timeout = timeout; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate retval = dhcp_ipc_send_request(fd, request); 3730Sstevel@tonic-gate if (retval == 0) 3740Sstevel@tonic-gate retval = dhcp_ipc_recv_reply(fd, reply); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate (void) dhcp_ipc_close(fd); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate return (retval); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * dhcp_ipc_init(): initializes the ipc channel for use by the agent 3830Sstevel@tonic-gate * 3840Sstevel@tonic-gate * input: int *: the file descriptor to accept on (returned) 3850Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate int 3890Sstevel@tonic-gate dhcp_ipc_init(int *listen_fd) 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate struct sockaddr_in sin; 3920Sstevel@tonic-gate int on = 1; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate (void) memset(&sin, 0, sizeof (struct sockaddr_in)); 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate sin.sin_family = AF_INET; 3970Sstevel@tonic-gate sin.sin_port = htons(IPPORT_DHCPAGENT); 3980Sstevel@tonic-gate sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate *listen_fd = socket(AF_INET, SOCK_STREAM, 0); 4010Sstevel@tonic-gate if (*listen_fd == -1) 4020Sstevel@tonic-gate return (DHCP_IPC_E_SOCKET); 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * we use SO_REUSEADDR here since in the case where there 4060Sstevel@tonic-gate * really is another daemon running that is using the agent's 4070Sstevel@tonic-gate * port, bind(3N) will fail. so we can't lose. 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate (void) setsockopt(*listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, 4110Sstevel@tonic-gate sizeof (on)); 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if (bind(*listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) { 4140Sstevel@tonic-gate (void) close(*listen_fd); 4150Sstevel@tonic-gate return (DHCP_IPC_E_BIND); 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate if (listen(*listen_fd, DHCP_IPC_LISTEN_BACKLOG) == -1) { 4190Sstevel@tonic-gate (void) close(*listen_fd); 4200Sstevel@tonic-gate return (DHCP_IPC_E_LISTEN); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate return (0); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate /* 4270Sstevel@tonic-gate * dhcp_ipc_accept(): accepts an incoming connection for the agent 4280Sstevel@tonic-gate * 4290Sstevel@tonic-gate * input: int: the file descriptor to accept on 4300Sstevel@tonic-gate * int *: the accepted file descriptor (returned) 4310Sstevel@tonic-gate * int *: nonzero if the client is privileged (returned) 4320Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 4330Sstevel@tonic-gate * note: sets the socket into nonblocking mode 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate int 4370Sstevel@tonic-gate dhcp_ipc_accept(int listen_fd, int *fd, int *is_priv) 4380Sstevel@tonic-gate { 4390Sstevel@tonic-gate struct sockaddr_in sin_peer; 4400Sstevel@tonic-gate int sin_len = sizeof (sin_peer); 4410Sstevel@tonic-gate int sockflags; 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * if we were extremely concerned with portability, we would 4450Sstevel@tonic-gate * set the socket into nonblocking mode before doing the 4460Sstevel@tonic-gate * accept(3N), since on BSD-based networking stacks, there is 4470Sstevel@tonic-gate * a potential race that can occur if the socket which 4480Sstevel@tonic-gate * connected to us performs a TCP RST before we accept, since 4490Sstevel@tonic-gate * BSD handles this case entirely in the kernel and as a 4500Sstevel@tonic-gate * result even though select said we will not block, we can 4510Sstevel@tonic-gate * end up blocking since there is no longer a connection to 4520Sstevel@tonic-gate * accept. on SVR4-based systems, this should be okay, 4530Sstevel@tonic-gate * and we will get EPROTO back, even though POSIX.1g says 4540Sstevel@tonic-gate * we should get ECONNABORTED. 4550Sstevel@tonic-gate */ 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate *fd = accept(listen_fd, (struct sockaddr *)&sin_peer, &sin_len); 4580Sstevel@tonic-gate if (*fd == -1) 4590Sstevel@tonic-gate return (DHCP_IPC_E_ACCEPT); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* get credentials */ 4620Sstevel@tonic-gate *is_priv = ntohs(sin_peer.sin_port) < IPPORT_RESERVED; 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* 4650Sstevel@tonic-gate * kick the socket into non-blocking mode so that later 4660Sstevel@tonic-gate * operations on the socket don't block and hold up the whole 4670Sstevel@tonic-gate * application. with the event demuxing approach, this may 4680Sstevel@tonic-gate * seem unnecessary, but in order to get partial reads/writes 4690Sstevel@tonic-gate * and to handle our internal protocol for passing data 4700Sstevel@tonic-gate * between the agent and its consumers, this is needed. 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate if ((sockflags = fcntl(*fd, F_GETFL, 0)) == -1) { 4740Sstevel@tonic-gate (void) close(*fd); 4750Sstevel@tonic-gate return (DHCP_IPC_E_FCNTL); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate if (fcntl(*fd, F_SETFL, sockflags | O_NONBLOCK) == -1) { 4790Sstevel@tonic-gate (void) close(*fd); 4800Sstevel@tonic-gate return (DHCP_IPC_E_FCNTL); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate return (0); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * dhcp_ipc_close(): closes an ipc descriptor 4880Sstevel@tonic-gate * 4890Sstevel@tonic-gate * input: int: the file descriptor to close 4900Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate int 4940Sstevel@tonic-gate dhcp_ipc_close(int fd) 4950Sstevel@tonic-gate { 4960Sstevel@tonic-gate return ((close(fd) == -1) ? DHCP_IPC_E_CLOSE : 0); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* 5000Sstevel@tonic-gate * dhcp_ipc_strerror(): maps an ipc error code into a human-readable string 5010Sstevel@tonic-gate * 5020Sstevel@tonic-gate * input: int: the ipc error code to map 5030Sstevel@tonic-gate * output: const char *: the corresponding human-readable string 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate const char * 5070Sstevel@tonic-gate dhcp_ipc_strerror(int error) 5080Sstevel@tonic-gate { 5090Sstevel@tonic-gate /* note: this must be kept in sync with DHCP_IPC_E_* definitions */ 5100Sstevel@tonic-gate const char *syscalls[] = { 5110Sstevel@tonic-gate "<unknown>", "socket", "fcntl", "read", "accept", "close", 512*3431Scarlsonj "bind", "listen", "malloc", "connect", "writev", "poll" 5130Sstevel@tonic-gate }; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate const char *error_string; 5160Sstevel@tonic-gate static char buffer[BUFMAX]; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate switch (error) { 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* 5210Sstevel@tonic-gate * none of these errors actually go over the wire. 5220Sstevel@tonic-gate * hence, we assume that errno is still fresh. 5230Sstevel@tonic-gate */ 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate case DHCP_IPC_E_SOCKET: /* FALLTHRU */ 5260Sstevel@tonic-gate case DHCP_IPC_E_FCNTL: /* FALLTHRU */ 5270Sstevel@tonic-gate case DHCP_IPC_E_READ: /* FALLTHRU */ 5280Sstevel@tonic-gate case DHCP_IPC_E_ACCEPT: /* FALLTHRU */ 5290Sstevel@tonic-gate case DHCP_IPC_E_CLOSE: /* FALLTHRU */ 5300Sstevel@tonic-gate case DHCP_IPC_E_BIND: /* FALLTHRU */ 5310Sstevel@tonic-gate case DHCP_IPC_E_LISTEN: /* FALLTHRU */ 5320Sstevel@tonic-gate case DHCP_IPC_E_CONNECT: /* FALLTHRU */ 533*3431Scarlsonj case DHCP_IPC_E_WRITEV: /* FALLTHRU */ 534*3431Scarlsonj case DHCP_IPC_E_POLL: 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate error_string = strerror(errno); 5370Sstevel@tonic-gate if (error_string == NULL) 5380Sstevel@tonic-gate error_string = "unknown error"; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate (void) snprintf(buffer, sizeof (buffer), "%s: %s", 5410Sstevel@tonic-gate syscalls[error], error_string); 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate error_string = buffer; 5440Sstevel@tonic-gate break; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate case DHCP_IPC_E_MEMORY: 5470Sstevel@tonic-gate error_string = "out of memory"; 5480Sstevel@tonic-gate break; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate case DHCP_IPC_E_TIMEOUT: 5510Sstevel@tonic-gate error_string = "wait timed out, operation still pending..."; 5520Sstevel@tonic-gate break; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate case DHCP_IPC_E_INVIF: 5550Sstevel@tonic-gate error_string = "interface does not exist or cannot be managed " 5560Sstevel@tonic-gate "using DHCP"; 5570Sstevel@tonic-gate break; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate case DHCP_IPC_E_INT: 5600Sstevel@tonic-gate error_string = "internal error (might work later)"; 5610Sstevel@tonic-gate break; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate case DHCP_IPC_E_PERM: 5640Sstevel@tonic-gate error_string = "permission denied"; 5650Sstevel@tonic-gate break; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate case DHCP_IPC_E_OUTSTATE: 5680Sstevel@tonic-gate error_string = "interface not in appropriate state for command"; 5690Sstevel@tonic-gate break; 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate case DHCP_IPC_E_PEND: 5720Sstevel@tonic-gate error_string = "interface currently has a pending command " 5730Sstevel@tonic-gate "(try later)"; 5740Sstevel@tonic-gate break; 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate case DHCP_IPC_E_BOOTP: 5770Sstevel@tonic-gate error_string = "interface is administered with BOOTP, not DHCP"; 5780Sstevel@tonic-gate break; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate case DHCP_IPC_E_CMD_UNKNOWN: 5810Sstevel@tonic-gate error_string = "unknown command"; 5820Sstevel@tonic-gate break; 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate case DHCP_IPC_E_UNKIF: 5850Sstevel@tonic-gate error_string = "interface is not under DHCP control"; 5860Sstevel@tonic-gate break; 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate case DHCP_IPC_E_PROTO: 5890Sstevel@tonic-gate error_string = "ipc protocol violation"; 5900Sstevel@tonic-gate break; 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate case DHCP_IPC_E_FAILEDIF: 5930Sstevel@tonic-gate error_string = "interface is in a FAILED state and must be " 5940Sstevel@tonic-gate "manually restarted"; 5950Sstevel@tonic-gate break; 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate case DHCP_IPC_E_NOPRIMARY: 5980Sstevel@tonic-gate error_string = "primary interface requested but no primary " 5990Sstevel@tonic-gate "interface is set"; 6000Sstevel@tonic-gate break; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate case DHCP_IPC_E_NOIPIF: 6030Sstevel@tonic-gate error_string = "interface currently has no IP address"; 6040Sstevel@tonic-gate break; 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate case DHCP_IPC_E_DOWNIF: 6070Sstevel@tonic-gate error_string = "interface is currently down"; 6080Sstevel@tonic-gate break; 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate case DHCP_IPC_E_NOVALUE: 6110Sstevel@tonic-gate error_string = "no value was found for this option"; 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate 614*3431Scarlsonj case DHCP_IPC_E_RUNNING: 615*3431Scarlsonj error_string = "DHCP is already running"; 616*3431Scarlsonj break; 617*3431Scarlsonj 618*3431Scarlsonj case DHCP_IPC_E_SRVFAILED: 619*3431Scarlsonj error_string = "DHCP server refused request"; 620*3431Scarlsonj break; 621*3431Scarlsonj 622*3431Scarlsonj case DHCP_IPC_E_EOF: 623*3431Scarlsonj error_string = "ipc connection closed"; 6240Sstevel@tonic-gate break; 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate default: 6270Sstevel@tonic-gate error_string = "unknown error"; 6280Sstevel@tonic-gate break; 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate /* 6320Sstevel@tonic-gate * TODO: internationalize this error string 6330Sstevel@tonic-gate */ 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate return (error_string); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate /* 639*3431Scarlsonj * dhcp_ipc_type_to_string(): maps an ipc command code into a human-readable 640*3431Scarlsonj * string 641*3431Scarlsonj * 642*3431Scarlsonj * input: int: the ipc command code to map 643*3431Scarlsonj * output: const char *: the corresponding human-readable string 644*3431Scarlsonj */ 645*3431Scarlsonj 646*3431Scarlsonj const char * 647*3431Scarlsonj dhcp_ipc_type_to_string(dhcp_ipc_type_t type) 648*3431Scarlsonj { 649*3431Scarlsonj static const char *typestr[] = { 650*3431Scarlsonj "drop", "extend", "ping", "release", "start", "status", 651*3431Scarlsonj "inform", "get_tag" 652*3431Scarlsonj }; 653*3431Scarlsonj 654*3431Scarlsonj if (type < 0 || type >= DHCP_NIPC) 655*3431Scarlsonj return ("unknown"); 656*3431Scarlsonj else 657*3431Scarlsonj return (typestr[(int)type]); 658*3431Scarlsonj } 659*3431Scarlsonj 660*3431Scarlsonj /* 6610Sstevel@tonic-gate * getinfo_ifnames(): checks the value of a specified option on a list of 6620Sstevel@tonic-gate * interface names. 6630Sstevel@tonic-gate * input: const char *: a list of interface names to query (in order) for 6640Sstevel@tonic-gate * the option; "" queries the primary interface 6650Sstevel@tonic-gate * dhcp_optnum_t *: a description of the desired option 6660Sstevel@tonic-gate * DHCP_OPT **: filled in with the (dynamically allocated) value of 6670Sstevel@tonic-gate * the option upon success. 6680Sstevel@tonic-gate * output: int: DHCP_IPC_E_* on error, 0 on success or if no value was 6690Sstevel@tonic-gate * found but no error occurred either (*result will be NULL) 6700Sstevel@tonic-gate */ 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate static int 6730Sstevel@tonic-gate getinfo_ifnames(const char *ifn, dhcp_optnum_t *optnum, DHCP_OPT **result) 6740Sstevel@tonic-gate { 6750Sstevel@tonic-gate dhcp_ipc_request_t *request; 6760Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 6770Sstevel@tonic-gate char *ifnames, *ifnames_head; 6780Sstevel@tonic-gate DHCP_OPT *opt; 6790Sstevel@tonic-gate size_t opt_size; 6800Sstevel@tonic-gate int retval = 0; 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate *result = NULL; 6830Sstevel@tonic-gate ifnames_head = ifnames = strdup(ifn); 6840Sstevel@tonic-gate if (ifnames == NULL) 6850Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_GET_TAG, "", optnum, 6880Sstevel@tonic-gate sizeof (dhcp_optnum_t), DHCP_TYPE_OPTNUM); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (request == NULL) { 6910Sstevel@tonic-gate free(ifnames_head); 6920Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate ifnames = strtok(ifnames, " "); 6960Sstevel@tonic-gate if (ifnames == NULL) 6970Sstevel@tonic-gate ifnames = ""; 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate for (; ifnames != NULL; ifnames = strtok(NULL, " ")) { 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate (void) strlcpy(request->ifname, ifnames, IFNAMSIZ); 7020Sstevel@tonic-gate retval = dhcp_ipc_make_request(request, &reply, 0); 7030Sstevel@tonic-gate if (retval != 0) 7040Sstevel@tonic-gate break; 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate if (reply->return_code == 0) { 7070Sstevel@tonic-gate opt = dhcp_ipc_get_data(reply, &opt_size, NULL); 7080Sstevel@tonic-gate if (opt_size > 2 && (opt->len == opt_size - 2)) { 7090Sstevel@tonic-gate *result = malloc(opt_size); 7100Sstevel@tonic-gate if (*result == NULL) 7110Sstevel@tonic-gate retval = DHCP_IPC_E_MEMORY; 7120Sstevel@tonic-gate else 7130Sstevel@tonic-gate (void) memcpy(*result, opt, opt_size); 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate free(reply); 7160Sstevel@tonic-gate break; 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate free(reply); 7210Sstevel@tonic-gate if (ifnames[0] == '\0') 7220Sstevel@tonic-gate break; 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate free(request); 7260Sstevel@tonic-gate free(ifnames_head); 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate return (retval); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * get_ifnames(): returns a space-separated list of interface names that 7330Sstevel@tonic-gate * match the specified flags 7340Sstevel@tonic-gate * 7350Sstevel@tonic-gate * input: int: flags which must be on in each interface returned 7360Sstevel@tonic-gate * int: flags which must be off in each interface returned 7370Sstevel@tonic-gate * output: char *: a dynamically-allocated list of interface names, or 7380Sstevel@tonic-gate * NULL upon failure. 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate static char * 7420Sstevel@tonic-gate get_ifnames(int flags_on, int flags_off) 7430Sstevel@tonic-gate { 7440Sstevel@tonic-gate struct ifconf ifc; 7450Sstevel@tonic-gate int n_ifs, i, sock_fd; 7460Sstevel@tonic-gate char *ifnames; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 7500Sstevel@tonic-gate if (sock_fd == -1) 7510Sstevel@tonic-gate return (NULL); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate if ((ioctl(sock_fd, SIOCGIFNUM, &n_ifs) == -1) || (n_ifs <= 0)) { 7540Sstevel@tonic-gate (void) close(sock_fd); 7550Sstevel@tonic-gate return (NULL); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate ifnames = calloc(1, n_ifs * (IFNAMSIZ + 1)); 7590Sstevel@tonic-gate ifc.ifc_len = n_ifs * sizeof (struct ifreq); 7600Sstevel@tonic-gate ifc.ifc_req = calloc(n_ifs, sizeof (struct ifreq)); 7610Sstevel@tonic-gate if (ifc.ifc_req != NULL && ifnames != NULL) { 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate if (ioctl(sock_fd, SIOCGIFCONF, &ifc) == -1) { 7640Sstevel@tonic-gate (void) close(sock_fd); 7650Sstevel@tonic-gate free(ifnames); 7660Sstevel@tonic-gate free(ifc.ifc_req); 7670Sstevel@tonic-gate return (NULL); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate for (i = 0; i < n_ifs; i++) { 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate if (ioctl(sock_fd, SIOCGIFFLAGS, &ifc.ifc_req[i]) == 0) 7730Sstevel@tonic-gate if ((ifc.ifc_req[i].ifr_flags & 7740Sstevel@tonic-gate (flags_on | flags_off)) != flags_on) 7750Sstevel@tonic-gate continue; 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate (void) strcat(ifnames, ifc.ifc_req[i].ifr_name); 7780Sstevel@tonic-gate (void) strcat(ifnames, " "); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate if (strlen(ifnames) > 1) 7820Sstevel@tonic-gate ifnames[strlen(ifnames) - 1] = '\0'; 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate (void) close(sock_fd); 7860Sstevel@tonic-gate free(ifc.ifc_req); 7870Sstevel@tonic-gate return (ifnames); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* 7910Sstevel@tonic-gate * dhcp_ipc_getinfo(): attempts to retrieve a value for the specified DHCP 7920Sstevel@tonic-gate * option; tries primary interface, then all DHCP-owned 7930Sstevel@tonic-gate * interfaces, then INFORMs on the remaining interfaces 7940Sstevel@tonic-gate * (these interfaces are dropped prior to returning). 7950Sstevel@tonic-gate * input: dhcp_optnum_t *: a description of the desired option 7960Sstevel@tonic-gate * DHCP_OPT **: filled in with the (dynamically allocated) value of 7970Sstevel@tonic-gate * the option upon success. 7980Sstevel@tonic-gate * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 7990Sstevel@tonic-gate * or DHCP_IPC_WAIT_DEFAULT. 8000Sstevel@tonic-gate * output: int: DHCP_IPC_E_* on error, 0 upon success. 8010Sstevel@tonic-gate */ 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate int 8040Sstevel@tonic-gate dhcp_ipc_getinfo(dhcp_optnum_t *optnum, DHCP_OPT **result, int32_t timeout) 8050Sstevel@tonic-gate { 8060Sstevel@tonic-gate dhcp_ipc_request_t *request; 8070Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 8080Sstevel@tonic-gate char *ifnames, *ifnames_copy, *ifnames_head; 8090Sstevel@tonic-gate int retval; 8100Sstevel@tonic-gate time_t start_time = time(NULL); 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate if (timeout == DHCP_IPC_WAIT_DEFAULT) 8130Sstevel@tonic-gate timeout = DHCP_IPC_DEFAULT_WAIT; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate /* 8160Sstevel@tonic-gate * wait at most 5 seconds for the agent to start. 8170Sstevel@tonic-gate */ 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate if (dhcp_start_agent((timeout > 5 || timeout < 0) ? 5 : timeout) == -1) 8200Sstevel@tonic-gate return (DHCP_IPC_E_INT); 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate /* 8230Sstevel@tonic-gate * check the primary interface for the option value first. 8240Sstevel@tonic-gate */ 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate retval = getinfo_ifnames("", optnum, result); 8270Sstevel@tonic-gate if ((retval != 0) || (retval == 0 && *result != NULL)) 8280Sstevel@tonic-gate return (retval); 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * no luck. get a list of the interfaces under DHCP control 8320Sstevel@tonic-gate * and perform a GET_TAG on each one. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate ifnames = get_ifnames(IFF_DHCPRUNNING, 0); 8360Sstevel@tonic-gate if (ifnames != NULL && strlen(ifnames) != 0) { 8370Sstevel@tonic-gate retval = getinfo_ifnames(ifnames, optnum, result); 8380Sstevel@tonic-gate if ((retval != 0) || (retval == 0 && *result != NULL)) { 8390Sstevel@tonic-gate free(ifnames); 8400Sstevel@tonic-gate return (retval); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate free(ifnames); 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate /* 8460Sstevel@tonic-gate * still no luck. retrieve a list of all interfaces on the 8470Sstevel@tonic-gate * system that could use DHCP but aren't. send INFORMs out on 8480Sstevel@tonic-gate * each one. after that, sit in a loop for the next `timeout' 8490Sstevel@tonic-gate * seconds, trying every second to see if a response for the 8500Sstevel@tonic-gate * option we want has come in on one of the interfaces. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate ifnames = get_ifnames(IFF_UP|IFF_RUNNING, IFF_LOOPBACK|IFF_DHCPRUNNING); 8540Sstevel@tonic-gate if (ifnames == NULL || strlen(ifnames) == 0) { 8550Sstevel@tonic-gate free(ifnames); 8560Sstevel@tonic-gate return (DHCP_IPC_E_NOVALUE); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate ifnames_head = ifnames_copy = strdup(ifnames); 8600Sstevel@tonic-gate if (ifnames_copy == NULL) { 8610Sstevel@tonic-gate free(ifnames); 8620Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_INFORM, "", NULL, 0, 8660Sstevel@tonic-gate DHCP_TYPE_NONE); 8670Sstevel@tonic-gate if (request == NULL) { 8680Sstevel@tonic-gate free(ifnames); 8690Sstevel@tonic-gate free(ifnames_head); 8700Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate ifnames_copy = strtok(ifnames_copy, " "); 8740Sstevel@tonic-gate for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) { 8750Sstevel@tonic-gate (void) strlcpy(request->ifname, ifnames_copy, IFNAMSIZ); 8760Sstevel@tonic-gate if (dhcp_ipc_make_request(request, &reply, 0) == 0) 8770Sstevel@tonic-gate free(reply); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate for (;;) { 8810Sstevel@tonic-gate if ((timeout != DHCP_IPC_WAIT_FOREVER) && 8820Sstevel@tonic-gate (time(NULL) - start_time > timeout)) { 8830Sstevel@tonic-gate retval = DHCP_IPC_E_TIMEOUT; 8840Sstevel@tonic-gate break; 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate retval = getinfo_ifnames(ifnames, optnum, result); 8880Sstevel@tonic-gate if (retval != 0 || (retval == 0 && *result != NULL)) 8890Sstevel@tonic-gate break; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate (void) sleep(1); 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * drop any interfaces that weren't under DHCP control before 8960Sstevel@tonic-gate * we got here; this keeps this function more of a black box 8970Sstevel@tonic-gate * and the behavior more consistent from call to call. 8980Sstevel@tonic-gate */ 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate request->message_type = DHCP_DROP; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate ifnames_copy = strcpy(ifnames_head, ifnames); 9030Sstevel@tonic-gate ifnames_copy = strtok(ifnames_copy, " "); 9040Sstevel@tonic-gate for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) { 9050Sstevel@tonic-gate (void) strlcpy(request->ifname, ifnames_copy, IFNAMSIZ); 9060Sstevel@tonic-gate if (dhcp_ipc_make_request(request, &reply, 0) == 0) 9070Sstevel@tonic-gate free(reply); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate free(request); 9110Sstevel@tonic-gate free(ifnames_head); 9120Sstevel@tonic-gate free(ifnames); 9130Sstevel@tonic-gate return (retval); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate /* 9170Sstevel@tonic-gate * dhcp_ipc_timed_read(): reads from a descriptor using a maximum timeout 9180Sstevel@tonic-gate * 9190Sstevel@tonic-gate * input: int: the file descriptor to read from 9200Sstevel@tonic-gate * void *: the buffer to read into 9210Sstevel@tonic-gate * unsigned int: the total length of data to read 9220Sstevel@tonic-gate * int *: the number of milliseconds to wait; the number of 923*3431Scarlsonj * milliseconds left are returned (-1 is "forever") 924*3431Scarlsonj * output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise 9250Sstevel@tonic-gate */ 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate static int 9280Sstevel@tonic-gate dhcp_ipc_timed_read(int fd, void *buffer, unsigned int length, int *msec) 9290Sstevel@tonic-gate { 9300Sstevel@tonic-gate unsigned int n_total = 0; 9310Sstevel@tonic-gate ssize_t n_read; 9320Sstevel@tonic-gate struct pollfd pollfd; 933*3431Scarlsonj hrtime_t start, end; 934*3431Scarlsonj int retv; 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate pollfd.fd = fd; 9370Sstevel@tonic-gate pollfd.events = POLLIN; 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate while (n_total < length) { 9400Sstevel@tonic-gate 941*3431Scarlsonj start = gethrtime(); 9420Sstevel@tonic-gate 943*3431Scarlsonj retv = poll(&pollfd, 1, *msec); 944*3431Scarlsonj if (retv == 0) { 945*3431Scarlsonj /* This can happen only if *msec is not -1 */ 9460Sstevel@tonic-gate *msec = 0; 947*3431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 948*3431Scarlsonj } 9490Sstevel@tonic-gate 950*3431Scarlsonj if (*msec != -1) { 951*3431Scarlsonj end = gethrtime(); 952*3431Scarlsonj *msec -= (end - start) / (NANOSEC / MILLISEC); 953*3431Scarlsonj if (*msec < 0) 954*3431Scarlsonj *msec = 0; 955*3431Scarlsonj } 9560Sstevel@tonic-gate 957*3431Scarlsonj if (retv == -1) { 958*3431Scarlsonj if (errno != EINTR) 959*3431Scarlsonj return (DHCP_IPC_E_POLL); 960*3431Scarlsonj else if (*msec == 0) 961*3431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 962*3431Scarlsonj continue; 963*3431Scarlsonj } 9640Sstevel@tonic-gate 965*3431Scarlsonj if (!(pollfd.revents & POLLIN)) { 966*3431Scarlsonj errno = EINVAL; 967*3431Scarlsonj return (DHCP_IPC_E_POLL); 968*3431Scarlsonj } 9690Sstevel@tonic-gate 970*3431Scarlsonj n_read = read(fd, (caddr_t)buffer + n_total, length - n_total); 9710Sstevel@tonic-gate 972*3431Scarlsonj if (n_read == -1) { 973*3431Scarlsonj if (errno != EINTR) 974*3431Scarlsonj return (DHCP_IPC_E_READ); 975*3431Scarlsonj else if (*msec == 0) 976*3431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 977*3431Scarlsonj continue; 978*3431Scarlsonj } 9790Sstevel@tonic-gate 980*3431Scarlsonj if (n_read == 0) { 981*3431Scarlsonj return (n_total == 0 ? DHCP_IPC_E_EOF : 982*3431Scarlsonj DHCP_IPC_E_PROTO); 9830Sstevel@tonic-gate } 984*3431Scarlsonj 985*3431Scarlsonj n_total += n_read; 986*3431Scarlsonj 987*3431Scarlsonj if (*msec == 0 && n_total < length) 988*3431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate 991*3431Scarlsonj return (DHCP_IPC_SUCCESS); 9920Sstevel@tonic-gate } 993