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 53431Scarlsonj * Common Development and Distribution License (the "License"). 63431Scarlsonj * 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 /* 223431Scarlsonj * 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> 373431Scarlsonj #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> 413431Scarlsonj #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 79*4106Scarlsonj /* must be kept in sync with enum in dhcpagent_ipc.h */ 80*4106Scarlsonj static const char *ipc_typestr[] = { 81*4106Scarlsonj "drop", "extend", "ping", "release", "start", "status", 82*4106Scarlsonj "inform", "get_tag" 83*4106Scarlsonj }; 84*4106Scarlsonj 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * dhcp_ipc_alloc_request(): allocates a dhcp_ipc_request_t of the given type 870Sstevel@tonic-gate * and interface, with a timeout of 0. 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * input: dhcp_ipc_type_t: the type of ipc request to allocate 900Sstevel@tonic-gate * const char *: the interface to associate the request with 913431Scarlsonj * const void *: the payload to send with the message (NULL if none) 920Sstevel@tonic-gate * uint32_t: the payload size (0 if none) 930Sstevel@tonic-gate * dhcp_data_type_t: the description of the type of payload 940Sstevel@tonic-gate * output: dhcp_ipc_request_t *: the request on success, NULL on failure 950Sstevel@tonic-gate */ 960Sstevel@tonic-gate 970Sstevel@tonic-gate dhcp_ipc_request_t * 983431Scarlsonj dhcp_ipc_alloc_request(dhcp_ipc_type_t type, const char *ifname, 993431Scarlsonj const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate dhcp_ipc_request_t *request = calloc(1, DHCP_IPC_REQUEST_SIZE + 1020Sstevel@tonic-gate buffer_size); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate if (request == NULL) 1050Sstevel@tonic-gate return (NULL); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate request->message_type = type; 1080Sstevel@tonic-gate request->data_length = buffer_size; 1090Sstevel@tonic-gate request->data_type = data_type; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate if (ifname != NULL) 1120Sstevel@tonic-gate (void) strlcpy(request->ifname, ifname, IFNAMSIZ); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate if (buffer != NULL) 1150Sstevel@tonic-gate (void) memcpy(request->buffer, buffer, buffer_size); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate return (request); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * dhcp_ipc_alloc_reply(): allocates a dhcp_ipc_reply_t 1220Sstevel@tonic-gate * 1230Sstevel@tonic-gate * input: dhcp_ipc_request_t *: the request the reply is for 1240Sstevel@tonic-gate * int: the return code (0 for success, DHCP_IPC_E_* otherwise) 1253431Scarlsonj * const void *: the payload to send with the message (NULL if none) 1260Sstevel@tonic-gate * uint32_t: the payload size (0 if none) 1270Sstevel@tonic-gate * dhcp_data_type_t: the description of the type of payload 1280Sstevel@tonic-gate * output: dhcp_ipc_reply_t *: the reply on success, NULL on failure 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate dhcp_ipc_reply_t * 1323431Scarlsonj dhcp_ipc_alloc_reply(dhcp_ipc_request_t *request, int return_code, 1333431Scarlsonj const void *buffer, uint32_t buffer_size, dhcp_data_type_t data_type) 1340Sstevel@tonic-gate { 1350Sstevel@tonic-gate dhcp_ipc_reply_t *reply = calloc(1, DHCP_IPC_REPLY_SIZE + buffer_size); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate if (reply == NULL) 1380Sstevel@tonic-gate return (NULL); 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate reply->message_type = request->message_type; 1410Sstevel@tonic-gate reply->ipc_id = request->ipc_id; 1420Sstevel@tonic-gate reply->return_code = return_code; 1430Sstevel@tonic-gate reply->data_length = buffer_size; 1440Sstevel@tonic-gate reply->data_type = data_type; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate if (buffer != NULL) 1470Sstevel@tonic-gate (void) memcpy(reply->buffer, buffer, buffer_size); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate return (reply); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * dhcp_ipc_get_data(): gets the data and data type from a dhcp_ipc_reply_t 1540Sstevel@tonic-gate * 1550Sstevel@tonic-gate * input: dhcp_ipc_reply_t *: the reply to get data from 1560Sstevel@tonic-gate * size_t *: the size of the resulting data 1570Sstevel@tonic-gate * dhcp_data_type_t *: the type of the message (returned) 1580Sstevel@tonic-gate * output: void *: a pointer to the data, if there is any. 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate void * 1620Sstevel@tonic-gate dhcp_ipc_get_data(dhcp_ipc_reply_t *reply, size_t *size, dhcp_data_type_t *type) 1630Sstevel@tonic-gate { 1640Sstevel@tonic-gate if (reply == NULL || reply->data_length == 0) { 1650Sstevel@tonic-gate *size = 0; 1660Sstevel@tonic-gate return (NULL); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate if (type != NULL) 1700Sstevel@tonic-gate *type = reply->data_type; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate *size = reply->data_length; 1730Sstevel@tonic-gate return (reply->buffer); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate /* 1770Sstevel@tonic-gate * dhcp_ipc_recv_msg(): gets a message using the agent's ipc protocol 1780Sstevel@tonic-gate * 1790Sstevel@tonic-gate * input: int: the file descriptor to get the message from 1800Sstevel@tonic-gate * void **: the address of a pointer to store the message 1810Sstevel@tonic-gate * (dynamically allocated) 1820Sstevel@tonic-gate * uint32_t: the minimum length of the packet 1830Sstevel@tonic-gate * int: the # of milliseconds to wait for the message (-1 is forever) 1843431Scarlsonj * output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise 1850Sstevel@tonic-gate */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate static int 1880Sstevel@tonic-gate dhcp_ipc_recv_msg(int fd, void **msg, uint32_t base_length, int msec) 1890Sstevel@tonic-gate { 1903431Scarlsonj int retval; 1910Sstevel@tonic-gate dhcp_ipc_reply_t *ipc_msg; 1920Sstevel@tonic-gate uint32_t length; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate retval = dhcp_ipc_timed_read(fd, &length, sizeof (uint32_t), &msec); 1953431Scarlsonj if (retval != DHCP_IPC_SUCCESS) 1963431Scarlsonj return (retval); 1973431Scarlsonj 1983431Scarlsonj if (length == 0) 1993431Scarlsonj return (DHCP_IPC_E_PROTO); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate *msg = malloc(length); 2020Sstevel@tonic-gate if (*msg == NULL) 2030Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate retval = dhcp_ipc_timed_read(fd, *msg, length, &msec); 2063431Scarlsonj if (retval != DHCP_IPC_SUCCESS) { 2070Sstevel@tonic-gate free(*msg); 2083431Scarlsonj return (retval); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate if (length < base_length) { 2120Sstevel@tonic-gate free(*msg); 2133431Scarlsonj return (DHCP_IPC_E_PROTO); 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * the data_length field is in the same place in either ipc message. 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate ipc_msg = (dhcp_ipc_reply_t *)(*msg); 2210Sstevel@tonic-gate if (ipc_msg->data_length + base_length != length) { 2220Sstevel@tonic-gate free(*msg); 2233431Scarlsonj return (DHCP_IPC_E_PROTO); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2263431Scarlsonj return (DHCP_IPC_SUCCESS); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * dhcp_ipc_recv_request(): gets a request using the agent's ipc protocol 2310Sstevel@tonic-gate * 2320Sstevel@tonic-gate * input: int: the file descriptor to get the message from 2330Sstevel@tonic-gate * dhcp_ipc_request_t **: address of a pointer to store the request 2340Sstevel@tonic-gate * (dynamically allocated) 2350Sstevel@tonic-gate * int: the # of milliseconds to wait for the message (-1 is forever) 2360Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate int 2400Sstevel@tonic-gate dhcp_ipc_recv_request(int fd, dhcp_ipc_request_t **request, int msec) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate int retval; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate retval = dhcp_ipc_recv_msg(fd, (void **)request, DHCP_IPC_REQUEST_SIZE, 2450Sstevel@tonic-gate msec); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* guarantee that ifname will be NUL-terminated */ 2480Sstevel@tonic-gate if (retval == 0) 2490Sstevel@tonic-gate (*request)->ifname[IFNAMSIZ - 1] = '\0'; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate return (retval); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * dhcp_ipc_recv_reply(): gets a reply using the agent's ipc protocol 2560Sstevel@tonic-gate * 2570Sstevel@tonic-gate * input: int: the file descriptor to get the message from 2580Sstevel@tonic-gate * dhcp_ipc_reply_t **: address of a pointer to store the reply 2590Sstevel@tonic-gate * (dynamically allocated) 2600Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2610Sstevel@tonic-gate */ 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate static int 2640Sstevel@tonic-gate dhcp_ipc_recv_reply(int fd, dhcp_ipc_reply_t **reply) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate return (dhcp_ipc_recv_msg(fd, (void **)reply, DHCP_IPC_REPLY_SIZE, -1)); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * dhcp_ipc_send_msg(): transmits a message using the agent's ipc protocol 2710Sstevel@tonic-gate * 2720Sstevel@tonic-gate * input: int: the file descriptor to transmit on 2730Sstevel@tonic-gate * void *: the message to send 2740Sstevel@tonic-gate * uint32_t: the message length 2750Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate static int 2790Sstevel@tonic-gate dhcp_ipc_send_msg(int fd, void *msg, uint32_t message_length) 2800Sstevel@tonic-gate { 2810Sstevel@tonic-gate struct iovec iovec[2]; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate iovec[0].iov_base = (caddr_t)&message_length; 2840Sstevel@tonic-gate iovec[0].iov_len = sizeof (uint32_t); 2850Sstevel@tonic-gate iovec[1].iov_base = msg; 2860Sstevel@tonic-gate iovec[1].iov_len = message_length; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (writev(fd, iovec, sizeof (iovec) / sizeof (*iovec)) == -1) 2890Sstevel@tonic-gate return (DHCP_IPC_E_WRITEV); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate return (0); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * dhcp_ipc_send_reply(): transmits a reply using the agent's ipc protocol 2960Sstevel@tonic-gate * 2970Sstevel@tonic-gate * input: int: the file descriptor to transmit on 2980Sstevel@tonic-gate * dhcp_ipc_reply_t *: the reply to send 2990Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3000Sstevel@tonic-gate */ 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate int 3030Sstevel@tonic-gate dhcp_ipc_send_reply(int fd, dhcp_ipc_reply_t *reply) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate return (dhcp_ipc_send_msg(fd, reply, DHCP_IPC_REPLY_SIZE + 3060Sstevel@tonic-gate reply->data_length)); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* 3100Sstevel@tonic-gate * dhcp_ipc_send_request(): transmits a request using the agent's ipc protocol 3110Sstevel@tonic-gate * 3120Sstevel@tonic-gate * input: int: the file descriptor to transmit on 3130Sstevel@tonic-gate * dhcp_ipc_request_t *: the request to send 3140Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate static int 3180Sstevel@tonic-gate dhcp_ipc_send_request(int fd, dhcp_ipc_request_t *request) 3190Sstevel@tonic-gate { 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * for now, ipc_ids aren't really used, but they're intended 3220Sstevel@tonic-gate * to make it easy to send several requests and then collect 3230Sstevel@tonic-gate * all of the replies (and pair them with the requests). 3240Sstevel@tonic-gate */ 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate request->ipc_id = gethrtime(); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate return (dhcp_ipc_send_msg(fd, request, DHCP_IPC_REQUEST_SIZE + 3290Sstevel@tonic-gate request->data_length)); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate /* 3330Sstevel@tonic-gate * dhcp_ipc_make_request(): sends the provided request to the agent and reaps 3340Sstevel@tonic-gate * the reply 3350Sstevel@tonic-gate * 3360Sstevel@tonic-gate * input: dhcp_ipc_request_t *: the request to make 3370Sstevel@tonic-gate * dhcp_ipc_reply_t **: the reply (dynamically allocated) 3380Sstevel@tonic-gate * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 3390Sstevel@tonic-gate * or DHCP_IPC_WAIT_DEFAULT 3400Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate int 3440Sstevel@tonic-gate dhcp_ipc_make_request(dhcp_ipc_request_t *request, dhcp_ipc_reply_t **reply, 3450Sstevel@tonic-gate int32_t timeout) 3460Sstevel@tonic-gate { 3473431Scarlsonj int fd, on, retval; 3483431Scarlsonj struct sockaddr_in sinv; 3490Sstevel@tonic-gate 3503431Scarlsonj fd = socket(AF_INET, SOCK_STREAM, 0); 3513431Scarlsonj if (fd == -1) 3523431Scarlsonj return (DHCP_IPC_E_SOCKET); 3530Sstevel@tonic-gate 3543431Scarlsonj /* 3553431Scarlsonj * Bind a privileged port if we have sufficient privilege to do so. 3563431Scarlsonj * Continue as non-privileged otherwise. 3573431Scarlsonj */ 3583431Scarlsonj on = 1; 3593431Scarlsonj (void) setsockopt(fd, IPPROTO_TCP, TCP_ANONPRIVBIND, &on, sizeof (on)); 3600Sstevel@tonic-gate 3613431Scarlsonj (void) memset(&sinv, 0, sizeof (sinv)); 3623431Scarlsonj sinv.sin_family = AF_INET; 3633431Scarlsonj if (bind(fd, (struct sockaddr *)&sinv, sizeof (sinv)) == -1) { 3643431Scarlsonj (void) dhcp_ipc_close(fd); 3653431Scarlsonj return (DHCP_IPC_E_BIND); 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3683431Scarlsonj sinv.sin_port = htons(IPPORT_DHCPAGENT); 3693431Scarlsonj sinv.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 3703431Scarlsonj retval = connect(fd, (struct sockaddr *)&sinv, sizeof (sinv)); 3710Sstevel@tonic-gate if (retval == -1) { 3720Sstevel@tonic-gate (void) dhcp_ipc_close(fd); 3730Sstevel@tonic-gate return (DHCP_IPC_E_CONNECT); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate request->timeout = timeout; 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate retval = dhcp_ipc_send_request(fd, request); 3790Sstevel@tonic-gate if (retval == 0) 3800Sstevel@tonic-gate retval = dhcp_ipc_recv_reply(fd, reply); 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate (void) dhcp_ipc_close(fd); 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate return (retval); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* 3880Sstevel@tonic-gate * dhcp_ipc_init(): initializes the ipc channel for use by the agent 3890Sstevel@tonic-gate * 3900Sstevel@tonic-gate * input: int *: the file descriptor to accept on (returned) 3910Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate int 3950Sstevel@tonic-gate dhcp_ipc_init(int *listen_fd) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate struct sockaddr_in sin; 3980Sstevel@tonic-gate int on = 1; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate (void) memset(&sin, 0, sizeof (struct sockaddr_in)); 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate sin.sin_family = AF_INET; 4030Sstevel@tonic-gate sin.sin_port = htons(IPPORT_DHCPAGENT); 4040Sstevel@tonic-gate sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate *listen_fd = socket(AF_INET, SOCK_STREAM, 0); 4070Sstevel@tonic-gate if (*listen_fd == -1) 4080Sstevel@tonic-gate return (DHCP_IPC_E_SOCKET); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * we use SO_REUSEADDR here since in the case where there 4120Sstevel@tonic-gate * really is another daemon running that is using the agent's 4130Sstevel@tonic-gate * port, bind(3N) will fail. so we can't lose. 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate (void) setsockopt(*listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, 4170Sstevel@tonic-gate sizeof (on)); 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate if (bind(*listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) { 4200Sstevel@tonic-gate (void) close(*listen_fd); 4210Sstevel@tonic-gate return (DHCP_IPC_E_BIND); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate if (listen(*listen_fd, DHCP_IPC_LISTEN_BACKLOG) == -1) { 4250Sstevel@tonic-gate (void) close(*listen_fd); 4260Sstevel@tonic-gate return (DHCP_IPC_E_LISTEN); 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate return (0); 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * dhcp_ipc_accept(): accepts an incoming connection for the agent 4340Sstevel@tonic-gate * 4350Sstevel@tonic-gate * input: int: the file descriptor to accept on 4360Sstevel@tonic-gate * int *: the accepted file descriptor (returned) 4370Sstevel@tonic-gate * int *: nonzero if the client is privileged (returned) 4380Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 4390Sstevel@tonic-gate * note: sets the socket into nonblocking mode 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate int 4430Sstevel@tonic-gate dhcp_ipc_accept(int listen_fd, int *fd, int *is_priv) 4440Sstevel@tonic-gate { 4450Sstevel@tonic-gate struct sockaddr_in sin_peer; 4460Sstevel@tonic-gate int sin_len = sizeof (sin_peer); 4470Sstevel@tonic-gate int sockflags; 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * if we were extremely concerned with portability, we would 4510Sstevel@tonic-gate * set the socket into nonblocking mode before doing the 4520Sstevel@tonic-gate * accept(3N), since on BSD-based networking stacks, there is 4530Sstevel@tonic-gate * a potential race that can occur if the socket which 4540Sstevel@tonic-gate * connected to us performs a TCP RST before we accept, since 4550Sstevel@tonic-gate * BSD handles this case entirely in the kernel and as a 4560Sstevel@tonic-gate * result even though select said we will not block, we can 4570Sstevel@tonic-gate * end up blocking since there is no longer a connection to 4580Sstevel@tonic-gate * accept. on SVR4-based systems, this should be okay, 4590Sstevel@tonic-gate * and we will get EPROTO back, even though POSIX.1g says 4600Sstevel@tonic-gate * we should get ECONNABORTED. 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate *fd = accept(listen_fd, (struct sockaddr *)&sin_peer, &sin_len); 4640Sstevel@tonic-gate if (*fd == -1) 4650Sstevel@tonic-gate return (DHCP_IPC_E_ACCEPT); 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* get credentials */ 4680Sstevel@tonic-gate *is_priv = ntohs(sin_peer.sin_port) < IPPORT_RESERVED; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * kick the socket into non-blocking mode so that later 4720Sstevel@tonic-gate * operations on the socket don't block and hold up the whole 4730Sstevel@tonic-gate * application. with the event demuxing approach, this may 4740Sstevel@tonic-gate * seem unnecessary, but in order to get partial reads/writes 4750Sstevel@tonic-gate * and to handle our internal protocol for passing data 4760Sstevel@tonic-gate * between the agent and its consumers, this is needed. 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate if ((sockflags = fcntl(*fd, F_GETFL, 0)) == -1) { 4800Sstevel@tonic-gate (void) close(*fd); 4810Sstevel@tonic-gate return (DHCP_IPC_E_FCNTL); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate if (fcntl(*fd, F_SETFL, sockflags | O_NONBLOCK) == -1) { 4850Sstevel@tonic-gate (void) close(*fd); 4860Sstevel@tonic-gate return (DHCP_IPC_E_FCNTL); 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate return (0); 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * dhcp_ipc_close(): closes an ipc descriptor 4940Sstevel@tonic-gate * 4950Sstevel@tonic-gate * input: int: the file descriptor to close 4960Sstevel@tonic-gate * output: int: 0 on success, DHCP_IPC_E_* otherwise 4970Sstevel@tonic-gate */ 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate int 5000Sstevel@tonic-gate dhcp_ipc_close(int fd) 5010Sstevel@tonic-gate { 5020Sstevel@tonic-gate return ((close(fd) == -1) ? DHCP_IPC_E_CLOSE : 0); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * dhcp_ipc_strerror(): maps an ipc error code into a human-readable string 5070Sstevel@tonic-gate * 5080Sstevel@tonic-gate * input: int: the ipc error code to map 5090Sstevel@tonic-gate * output: const char *: the corresponding human-readable string 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate const char * 5130Sstevel@tonic-gate dhcp_ipc_strerror(int error) 5140Sstevel@tonic-gate { 5150Sstevel@tonic-gate /* note: this must be kept in sync with DHCP_IPC_E_* definitions */ 5160Sstevel@tonic-gate const char *syscalls[] = { 5170Sstevel@tonic-gate "<unknown>", "socket", "fcntl", "read", "accept", "close", 5183431Scarlsonj "bind", "listen", "malloc", "connect", "writev", "poll" 5190Sstevel@tonic-gate }; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate const char *error_string; 5220Sstevel@tonic-gate static char buffer[BUFMAX]; 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate switch (error) { 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * none of these errors actually go over the wire. 5280Sstevel@tonic-gate * hence, we assume that errno is still fresh. 5290Sstevel@tonic-gate */ 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate case DHCP_IPC_E_SOCKET: /* FALLTHRU */ 5320Sstevel@tonic-gate case DHCP_IPC_E_FCNTL: /* FALLTHRU */ 5330Sstevel@tonic-gate case DHCP_IPC_E_READ: /* FALLTHRU */ 5340Sstevel@tonic-gate case DHCP_IPC_E_ACCEPT: /* FALLTHRU */ 5350Sstevel@tonic-gate case DHCP_IPC_E_CLOSE: /* FALLTHRU */ 5360Sstevel@tonic-gate case DHCP_IPC_E_BIND: /* FALLTHRU */ 5370Sstevel@tonic-gate case DHCP_IPC_E_LISTEN: /* FALLTHRU */ 5380Sstevel@tonic-gate case DHCP_IPC_E_CONNECT: /* FALLTHRU */ 5393431Scarlsonj case DHCP_IPC_E_WRITEV: /* FALLTHRU */ 5403431Scarlsonj case DHCP_IPC_E_POLL: 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate error_string = strerror(errno); 5430Sstevel@tonic-gate if (error_string == NULL) 5440Sstevel@tonic-gate error_string = "unknown error"; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate (void) snprintf(buffer, sizeof (buffer), "%s: %s", 5470Sstevel@tonic-gate syscalls[error], error_string); 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate error_string = buffer; 5500Sstevel@tonic-gate break; 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate case DHCP_IPC_E_MEMORY: 5530Sstevel@tonic-gate error_string = "out of memory"; 5540Sstevel@tonic-gate break; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate case DHCP_IPC_E_TIMEOUT: 5570Sstevel@tonic-gate error_string = "wait timed out, operation still pending..."; 5580Sstevel@tonic-gate break; 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate case DHCP_IPC_E_INVIF: 5610Sstevel@tonic-gate error_string = "interface does not exist or cannot be managed " 5620Sstevel@tonic-gate "using DHCP"; 5630Sstevel@tonic-gate break; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate case DHCP_IPC_E_INT: 5660Sstevel@tonic-gate error_string = "internal error (might work later)"; 5670Sstevel@tonic-gate break; 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate case DHCP_IPC_E_PERM: 5700Sstevel@tonic-gate error_string = "permission denied"; 5710Sstevel@tonic-gate break; 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate case DHCP_IPC_E_OUTSTATE: 5740Sstevel@tonic-gate error_string = "interface not in appropriate state for command"; 5750Sstevel@tonic-gate break; 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate case DHCP_IPC_E_PEND: 5780Sstevel@tonic-gate error_string = "interface currently has a pending command " 5790Sstevel@tonic-gate "(try later)"; 5800Sstevel@tonic-gate break; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate case DHCP_IPC_E_BOOTP: 5830Sstevel@tonic-gate error_string = "interface is administered with BOOTP, not DHCP"; 5840Sstevel@tonic-gate break; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate case DHCP_IPC_E_CMD_UNKNOWN: 5870Sstevel@tonic-gate error_string = "unknown command"; 5880Sstevel@tonic-gate break; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate case DHCP_IPC_E_UNKIF: 5910Sstevel@tonic-gate error_string = "interface is not under DHCP control"; 5920Sstevel@tonic-gate break; 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate case DHCP_IPC_E_PROTO: 5950Sstevel@tonic-gate error_string = "ipc protocol violation"; 5960Sstevel@tonic-gate break; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate case DHCP_IPC_E_FAILEDIF: 5990Sstevel@tonic-gate error_string = "interface is in a FAILED state and must be " 6000Sstevel@tonic-gate "manually restarted"; 6010Sstevel@tonic-gate break; 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate case DHCP_IPC_E_NOPRIMARY: 6040Sstevel@tonic-gate error_string = "primary interface requested but no primary " 6050Sstevel@tonic-gate "interface is set"; 6060Sstevel@tonic-gate break; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate case DHCP_IPC_E_NOIPIF: 6090Sstevel@tonic-gate error_string = "interface currently has no IP address"; 6100Sstevel@tonic-gate break; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate case DHCP_IPC_E_DOWNIF: 6130Sstevel@tonic-gate error_string = "interface is currently down"; 6140Sstevel@tonic-gate break; 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate case DHCP_IPC_E_NOVALUE: 6170Sstevel@tonic-gate error_string = "no value was found for this option"; 6180Sstevel@tonic-gate break; 6190Sstevel@tonic-gate 6203431Scarlsonj case DHCP_IPC_E_RUNNING: 6213431Scarlsonj error_string = "DHCP is already running"; 6223431Scarlsonj break; 6233431Scarlsonj 6243431Scarlsonj case DHCP_IPC_E_SRVFAILED: 6253431Scarlsonj error_string = "DHCP server refused request"; 6263431Scarlsonj break; 6273431Scarlsonj 6283431Scarlsonj case DHCP_IPC_E_EOF: 6293431Scarlsonj error_string = "ipc connection closed"; 6300Sstevel@tonic-gate break; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate default: 6330Sstevel@tonic-gate error_string = "unknown error"; 6340Sstevel@tonic-gate break; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * TODO: internationalize this error string 6390Sstevel@tonic-gate */ 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate return (error_string); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* 645*4106Scarlsonj * dhcp_string_to_request(): maps a string into a request code 646*4106Scarlsonj * 647*4106Scarlsonj * input: const char *: the string to map 648*4106Scarlsonj * output: dhcp_ipc_type_t: the request code, or -1 if unknown 649*4106Scarlsonj */ 650*4106Scarlsonj 651*4106Scarlsonj dhcp_ipc_type_t 652*4106Scarlsonj dhcp_string_to_request(const char *request) 653*4106Scarlsonj { 654*4106Scarlsonj unsigned int i; 655*4106Scarlsonj 656*4106Scarlsonj for (i = 0; i < DHCP_NIPC; i++) 657*4106Scarlsonj if (strcmp(ipc_typestr[i], request) == 0) 658*4106Scarlsonj return ((dhcp_ipc_type_t)i); 659*4106Scarlsonj 660*4106Scarlsonj return ((dhcp_ipc_type_t)-1); 661*4106Scarlsonj } 662*4106Scarlsonj 663*4106Scarlsonj /* 6643431Scarlsonj * dhcp_ipc_type_to_string(): maps an ipc command code into a human-readable 6653431Scarlsonj * string 6663431Scarlsonj * 6673431Scarlsonj * input: int: the ipc command code to map 6683431Scarlsonj * output: const char *: the corresponding human-readable string 6693431Scarlsonj */ 6703431Scarlsonj 6713431Scarlsonj const char * 6723431Scarlsonj dhcp_ipc_type_to_string(dhcp_ipc_type_t type) 6733431Scarlsonj { 6743431Scarlsonj if (type < 0 || type >= DHCP_NIPC) 6753431Scarlsonj return ("unknown"); 6763431Scarlsonj else 677*4106Scarlsonj return (ipc_typestr[(int)type]); 6783431Scarlsonj } 6793431Scarlsonj 6803431Scarlsonj /* 6810Sstevel@tonic-gate * getinfo_ifnames(): checks the value of a specified option on a list of 6820Sstevel@tonic-gate * interface names. 6830Sstevel@tonic-gate * input: const char *: a list of interface names to query (in order) for 6840Sstevel@tonic-gate * the option; "" queries the primary interface 6850Sstevel@tonic-gate * dhcp_optnum_t *: a description of the desired option 6860Sstevel@tonic-gate * DHCP_OPT **: filled in with the (dynamically allocated) value of 6870Sstevel@tonic-gate * the option upon success. 6880Sstevel@tonic-gate * output: int: DHCP_IPC_E_* on error, 0 on success or if no value was 6890Sstevel@tonic-gate * found but no error occurred either (*result will be NULL) 6900Sstevel@tonic-gate */ 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate static int 6930Sstevel@tonic-gate getinfo_ifnames(const char *ifn, dhcp_optnum_t *optnum, DHCP_OPT **result) 6940Sstevel@tonic-gate { 6950Sstevel@tonic-gate dhcp_ipc_request_t *request; 6960Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 6970Sstevel@tonic-gate char *ifnames, *ifnames_head; 6980Sstevel@tonic-gate DHCP_OPT *opt; 6990Sstevel@tonic-gate size_t opt_size; 7000Sstevel@tonic-gate int retval = 0; 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate *result = NULL; 7030Sstevel@tonic-gate ifnames_head = ifnames = strdup(ifn); 7040Sstevel@tonic-gate if (ifnames == NULL) 7050Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_GET_TAG, "", optnum, 7080Sstevel@tonic-gate sizeof (dhcp_optnum_t), DHCP_TYPE_OPTNUM); 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate if (request == NULL) { 7110Sstevel@tonic-gate free(ifnames_head); 7120Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate ifnames = strtok(ifnames, " "); 7160Sstevel@tonic-gate if (ifnames == NULL) 7170Sstevel@tonic-gate ifnames = ""; 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate for (; ifnames != NULL; ifnames = strtok(NULL, " ")) { 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate (void) strlcpy(request->ifname, ifnames, IFNAMSIZ); 7220Sstevel@tonic-gate retval = dhcp_ipc_make_request(request, &reply, 0); 7230Sstevel@tonic-gate if (retval != 0) 7240Sstevel@tonic-gate break; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate if (reply->return_code == 0) { 7270Sstevel@tonic-gate opt = dhcp_ipc_get_data(reply, &opt_size, NULL); 7280Sstevel@tonic-gate if (opt_size > 2 && (opt->len == opt_size - 2)) { 7290Sstevel@tonic-gate *result = malloc(opt_size); 7300Sstevel@tonic-gate if (*result == NULL) 7310Sstevel@tonic-gate retval = DHCP_IPC_E_MEMORY; 7320Sstevel@tonic-gate else 7330Sstevel@tonic-gate (void) memcpy(*result, opt, opt_size); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate free(reply); 7360Sstevel@tonic-gate break; 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate free(reply); 7410Sstevel@tonic-gate if (ifnames[0] == '\0') 7420Sstevel@tonic-gate break; 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate free(request); 7460Sstevel@tonic-gate free(ifnames_head); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate return (retval); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* 7520Sstevel@tonic-gate * get_ifnames(): returns a space-separated list of interface names that 7530Sstevel@tonic-gate * match the specified flags 7540Sstevel@tonic-gate * 7550Sstevel@tonic-gate * input: int: flags which must be on in each interface returned 7560Sstevel@tonic-gate * int: flags which must be off in each interface returned 7570Sstevel@tonic-gate * output: char *: a dynamically-allocated list of interface names, or 7580Sstevel@tonic-gate * NULL upon failure. 7590Sstevel@tonic-gate */ 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate static char * 7620Sstevel@tonic-gate get_ifnames(int flags_on, int flags_off) 7630Sstevel@tonic-gate { 7640Sstevel@tonic-gate struct ifconf ifc; 7650Sstevel@tonic-gate int n_ifs, i, sock_fd; 7660Sstevel@tonic-gate char *ifnames; 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 7700Sstevel@tonic-gate if (sock_fd == -1) 7710Sstevel@tonic-gate return (NULL); 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate if ((ioctl(sock_fd, SIOCGIFNUM, &n_ifs) == -1) || (n_ifs <= 0)) { 7740Sstevel@tonic-gate (void) close(sock_fd); 7750Sstevel@tonic-gate return (NULL); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate ifnames = calloc(1, n_ifs * (IFNAMSIZ + 1)); 7790Sstevel@tonic-gate ifc.ifc_len = n_ifs * sizeof (struct ifreq); 7800Sstevel@tonic-gate ifc.ifc_req = calloc(n_ifs, sizeof (struct ifreq)); 7810Sstevel@tonic-gate if (ifc.ifc_req != NULL && ifnames != NULL) { 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate if (ioctl(sock_fd, SIOCGIFCONF, &ifc) == -1) { 7840Sstevel@tonic-gate (void) close(sock_fd); 7850Sstevel@tonic-gate free(ifnames); 7860Sstevel@tonic-gate free(ifc.ifc_req); 7870Sstevel@tonic-gate return (NULL); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate for (i = 0; i < n_ifs; i++) { 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate if (ioctl(sock_fd, SIOCGIFFLAGS, &ifc.ifc_req[i]) == 0) 7930Sstevel@tonic-gate if ((ifc.ifc_req[i].ifr_flags & 7940Sstevel@tonic-gate (flags_on | flags_off)) != flags_on) 7950Sstevel@tonic-gate continue; 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate (void) strcat(ifnames, ifc.ifc_req[i].ifr_name); 7980Sstevel@tonic-gate (void) strcat(ifnames, " "); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate if (strlen(ifnames) > 1) 8020Sstevel@tonic-gate ifnames[strlen(ifnames) - 1] = '\0'; 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate (void) close(sock_fd); 8060Sstevel@tonic-gate free(ifc.ifc_req); 8070Sstevel@tonic-gate return (ifnames); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate /* 8110Sstevel@tonic-gate * dhcp_ipc_getinfo(): attempts to retrieve a value for the specified DHCP 8120Sstevel@tonic-gate * option; tries primary interface, then all DHCP-owned 8130Sstevel@tonic-gate * interfaces, then INFORMs on the remaining interfaces 8140Sstevel@tonic-gate * (these interfaces are dropped prior to returning). 8150Sstevel@tonic-gate * input: dhcp_optnum_t *: a description of the desired option 8160Sstevel@tonic-gate * DHCP_OPT **: filled in with the (dynamically allocated) value of 8170Sstevel@tonic-gate * the option upon success. 8180Sstevel@tonic-gate * int32_t: timeout (in seconds), or DHCP_IPC_WAIT_FOREVER, 8190Sstevel@tonic-gate * or DHCP_IPC_WAIT_DEFAULT. 8200Sstevel@tonic-gate * output: int: DHCP_IPC_E_* on error, 0 upon success. 8210Sstevel@tonic-gate */ 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate int 8240Sstevel@tonic-gate dhcp_ipc_getinfo(dhcp_optnum_t *optnum, DHCP_OPT **result, int32_t timeout) 8250Sstevel@tonic-gate { 8260Sstevel@tonic-gate dhcp_ipc_request_t *request; 8270Sstevel@tonic-gate dhcp_ipc_reply_t *reply; 8280Sstevel@tonic-gate char *ifnames, *ifnames_copy, *ifnames_head; 8290Sstevel@tonic-gate int retval; 8300Sstevel@tonic-gate time_t start_time = time(NULL); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate if (timeout == DHCP_IPC_WAIT_DEFAULT) 8330Sstevel@tonic-gate timeout = DHCP_IPC_DEFAULT_WAIT; 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * wait at most 5 seconds for the agent to start. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate if (dhcp_start_agent((timeout > 5 || timeout < 0) ? 5 : timeout) == -1) 8400Sstevel@tonic-gate return (DHCP_IPC_E_INT); 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate /* 8430Sstevel@tonic-gate * check the primary interface for the option value first. 8440Sstevel@tonic-gate */ 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate retval = getinfo_ifnames("", optnum, result); 8470Sstevel@tonic-gate if ((retval != 0) || (retval == 0 && *result != NULL)) 8480Sstevel@tonic-gate return (retval); 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * no luck. get a list of the interfaces under DHCP control 8520Sstevel@tonic-gate * and perform a GET_TAG on each one. 8530Sstevel@tonic-gate */ 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate ifnames = get_ifnames(IFF_DHCPRUNNING, 0); 8560Sstevel@tonic-gate if (ifnames != NULL && strlen(ifnames) != 0) { 8570Sstevel@tonic-gate retval = getinfo_ifnames(ifnames, optnum, result); 8580Sstevel@tonic-gate if ((retval != 0) || (retval == 0 && *result != NULL)) { 8590Sstevel@tonic-gate free(ifnames); 8600Sstevel@tonic-gate return (retval); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate free(ifnames); 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate /* 8660Sstevel@tonic-gate * still no luck. retrieve a list of all interfaces on the 8670Sstevel@tonic-gate * system that could use DHCP but aren't. send INFORMs out on 8680Sstevel@tonic-gate * each one. after that, sit in a loop for the next `timeout' 8690Sstevel@tonic-gate * seconds, trying every second to see if a response for the 8700Sstevel@tonic-gate * option we want has come in on one of the interfaces. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate ifnames = get_ifnames(IFF_UP|IFF_RUNNING, IFF_LOOPBACK|IFF_DHCPRUNNING); 8740Sstevel@tonic-gate if (ifnames == NULL || strlen(ifnames) == 0) { 8750Sstevel@tonic-gate free(ifnames); 8760Sstevel@tonic-gate return (DHCP_IPC_E_NOVALUE); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate ifnames_head = ifnames_copy = strdup(ifnames); 8800Sstevel@tonic-gate if (ifnames_copy == NULL) { 8810Sstevel@tonic-gate free(ifnames); 8820Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate request = dhcp_ipc_alloc_request(DHCP_INFORM, "", NULL, 0, 8860Sstevel@tonic-gate DHCP_TYPE_NONE); 8870Sstevel@tonic-gate if (request == NULL) { 8880Sstevel@tonic-gate free(ifnames); 8890Sstevel@tonic-gate free(ifnames_head); 8900Sstevel@tonic-gate return (DHCP_IPC_E_MEMORY); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate ifnames_copy = strtok(ifnames_copy, " "); 8940Sstevel@tonic-gate for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) { 8950Sstevel@tonic-gate (void) strlcpy(request->ifname, ifnames_copy, IFNAMSIZ); 8960Sstevel@tonic-gate if (dhcp_ipc_make_request(request, &reply, 0) == 0) 8970Sstevel@tonic-gate free(reply); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate for (;;) { 9010Sstevel@tonic-gate if ((timeout != DHCP_IPC_WAIT_FOREVER) && 9020Sstevel@tonic-gate (time(NULL) - start_time > timeout)) { 9030Sstevel@tonic-gate retval = DHCP_IPC_E_TIMEOUT; 9040Sstevel@tonic-gate break; 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate retval = getinfo_ifnames(ifnames, optnum, result); 9080Sstevel@tonic-gate if (retval != 0 || (retval == 0 && *result != NULL)) 9090Sstevel@tonic-gate break; 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate (void) sleep(1); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate /* 9150Sstevel@tonic-gate * drop any interfaces that weren't under DHCP control before 9160Sstevel@tonic-gate * we got here; this keeps this function more of a black box 9170Sstevel@tonic-gate * and the behavior more consistent from call to call. 9180Sstevel@tonic-gate */ 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate request->message_type = DHCP_DROP; 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate ifnames_copy = strcpy(ifnames_head, ifnames); 9230Sstevel@tonic-gate ifnames_copy = strtok(ifnames_copy, " "); 9240Sstevel@tonic-gate for (; ifnames_copy != NULL; ifnames_copy = strtok(NULL, " ")) { 9250Sstevel@tonic-gate (void) strlcpy(request->ifname, ifnames_copy, IFNAMSIZ); 9260Sstevel@tonic-gate if (dhcp_ipc_make_request(request, &reply, 0) == 0) 9270Sstevel@tonic-gate free(reply); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate free(request); 9310Sstevel@tonic-gate free(ifnames_head); 9320Sstevel@tonic-gate free(ifnames); 9330Sstevel@tonic-gate return (retval); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * dhcp_ipc_timed_read(): reads from a descriptor using a maximum timeout 9380Sstevel@tonic-gate * 9390Sstevel@tonic-gate * input: int: the file descriptor to read from 9400Sstevel@tonic-gate * void *: the buffer to read into 9410Sstevel@tonic-gate * unsigned int: the total length of data to read 9420Sstevel@tonic-gate * int *: the number of milliseconds to wait; the number of 9433431Scarlsonj * milliseconds left are returned (-1 is "forever") 9443431Scarlsonj * output: int: DHCP_IPC_SUCCESS on success, DHCP_IPC_E_* otherwise 9450Sstevel@tonic-gate */ 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate static int 9480Sstevel@tonic-gate dhcp_ipc_timed_read(int fd, void *buffer, unsigned int length, int *msec) 9490Sstevel@tonic-gate { 9500Sstevel@tonic-gate unsigned int n_total = 0; 9510Sstevel@tonic-gate ssize_t n_read; 9520Sstevel@tonic-gate struct pollfd pollfd; 9533431Scarlsonj hrtime_t start, end; 9543431Scarlsonj int retv; 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate pollfd.fd = fd; 9570Sstevel@tonic-gate pollfd.events = POLLIN; 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate while (n_total < length) { 9600Sstevel@tonic-gate 9613431Scarlsonj start = gethrtime(); 9620Sstevel@tonic-gate 9633431Scarlsonj retv = poll(&pollfd, 1, *msec); 9643431Scarlsonj if (retv == 0) { 9653431Scarlsonj /* This can happen only if *msec is not -1 */ 9660Sstevel@tonic-gate *msec = 0; 9673431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 9683431Scarlsonj } 9690Sstevel@tonic-gate 9703431Scarlsonj if (*msec != -1) { 9713431Scarlsonj end = gethrtime(); 9723431Scarlsonj *msec -= (end - start) / (NANOSEC / MILLISEC); 9733431Scarlsonj if (*msec < 0) 9743431Scarlsonj *msec = 0; 9753431Scarlsonj } 9760Sstevel@tonic-gate 9773431Scarlsonj if (retv == -1) { 9783431Scarlsonj if (errno != EINTR) 9793431Scarlsonj return (DHCP_IPC_E_POLL); 9803431Scarlsonj else if (*msec == 0) 9813431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 9823431Scarlsonj continue; 9833431Scarlsonj } 9840Sstevel@tonic-gate 9853431Scarlsonj if (!(pollfd.revents & POLLIN)) { 9863431Scarlsonj errno = EINVAL; 9873431Scarlsonj return (DHCP_IPC_E_POLL); 9883431Scarlsonj } 9890Sstevel@tonic-gate 9903431Scarlsonj n_read = read(fd, (caddr_t)buffer + n_total, length - n_total); 9910Sstevel@tonic-gate 9923431Scarlsonj if (n_read == -1) { 9933431Scarlsonj if (errno != EINTR) 9943431Scarlsonj return (DHCP_IPC_E_READ); 9953431Scarlsonj else if (*msec == 0) 9963431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 9973431Scarlsonj continue; 9983431Scarlsonj } 9990Sstevel@tonic-gate 10003431Scarlsonj if (n_read == 0) { 10013431Scarlsonj return (n_total == 0 ? DHCP_IPC_E_EOF : 10023431Scarlsonj DHCP_IPC_E_PROTO); 10030Sstevel@tonic-gate } 10043431Scarlsonj 10053431Scarlsonj n_total += n_read; 10063431Scarlsonj 10073431Scarlsonj if (*msec == 0 && n_total < length) 10083431Scarlsonj return (DHCP_IPC_E_TIMEOUT); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10113431Scarlsonj return (DHCP_IPC_SUCCESS); 10120Sstevel@tonic-gate } 1013