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
52088Smeem * Common Development and Distribution License (the "License").
62088Smeem * 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*12989SVasumathi.Sundaram@oracle.COM * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <unistd.h>
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stat.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <netinet/in.h> /* struct in_addr */
300Sstevel@tonic-gate #include <netinet/dhcp.h>
310Sstevel@tonic-gate #include <signal.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate #include <net/route.h>
340Sstevel@tonic-gate #include <net/if_arp.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <dhcpmsg.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <netdb.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <stdio.h>
41*12989SVasumathi.Sundaram@oracle.COM #include <dhcp_hostconf.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate #include "states.h"
440Sstevel@tonic-gate #include "agent.h"
450Sstevel@tonic-gate #include "interface.h"
460Sstevel@tonic-gate #include "util.h"
470Sstevel@tonic-gate #include "packet.h"
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * this file contains utility functions that have no real better home
510Sstevel@tonic-gate * of their own. they can largely be broken into six categories:
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * o conversion functions -- functions to turn integers into strings,
540Sstevel@tonic-gate * or to convert between units of a similar measure.
550Sstevel@tonic-gate *
563431Scarlsonj * o time and timer functions -- functions to handle time measurement
573431Scarlsonj * and events.
583431Scarlsonj *
590Sstevel@tonic-gate * o ipc-related functions -- functions to simplify the generation of
600Sstevel@tonic-gate * ipc messages to the agent's clients.
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * o signal-related functions -- functions to clean up the agent when
630Sstevel@tonic-gate * it receives a signal.
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * o routing table manipulation functions
660Sstevel@tonic-gate *
670Sstevel@tonic-gate * o true miscellany -- anything else
680Sstevel@tonic-gate */
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * pkt_type_to_string(): stringifies a packet type
720Sstevel@tonic-gate *
733431Scarlsonj * input: uchar_t: a DHCP packet type value, RFC 2131 or 3315
743431Scarlsonj * boolean_t: B_TRUE if IPv6
750Sstevel@tonic-gate * output: const char *: the stringified packet type
760Sstevel@tonic-gate */
770Sstevel@tonic-gate
780Sstevel@tonic-gate const char *
pkt_type_to_string(uchar_t type,boolean_t isv6)793431Scarlsonj pkt_type_to_string(uchar_t type, boolean_t isv6)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate /*
823431Scarlsonj * note: the ordering in these arrays allows direct indexing of the
833431Scarlsonj * table based on the RFC packet type value passed in.
840Sstevel@tonic-gate */
850Sstevel@tonic-gate
863431Scarlsonj static const char *v4types[] = {
870Sstevel@tonic-gate "BOOTP", "DISCOVER", "OFFER", "REQUEST", "DECLINE",
880Sstevel@tonic-gate "ACK", "NAK", "RELEASE", "INFORM"
890Sstevel@tonic-gate };
903431Scarlsonj static const char *v6types[] = {
913431Scarlsonj NULL, "SOLICIT", "ADVERTISE", "REQUEST",
923431Scarlsonj "CONFIRM", "RENEW", "REBIND", "REPLY",
933431Scarlsonj "RELEASE", "DECLINE", "RECONFIGURE", "INFORMATION-REQUEST",
943431Scarlsonj "RELAY-FORW", "RELAY-REPL"
953431Scarlsonj };
960Sstevel@tonic-gate
973431Scarlsonj if (isv6) {
983431Scarlsonj if (type >= sizeof (v6types) / sizeof (*v6types) ||
993431Scarlsonj v6types[type] == NULL)
1003431Scarlsonj return ("<unknown>");
1013431Scarlsonj else
1023431Scarlsonj return (v6types[type]);
1033431Scarlsonj } else {
1043431Scarlsonj if (type >= sizeof (v4types) / sizeof (*v4types) ||
1053431Scarlsonj v4types[type] == NULL)
1063431Scarlsonj return ("<unknown>");
1073431Scarlsonj else
1083431Scarlsonj return (v4types[type]);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * monosec_to_string(): converts a monosec_t into a date string
1140Sstevel@tonic-gate *
1150Sstevel@tonic-gate * input: monosec_t: the monosec_t to convert
1160Sstevel@tonic-gate * output: const char *: the corresponding date string
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate const char *
monosec_to_string(monosec_t monosec)1200Sstevel@tonic-gate monosec_to_string(monosec_t monosec)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate time_t time = monosec_to_time(monosec);
1230Sstevel@tonic-gate char *time_string = ctime(&time);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* strip off the newline -- ugh, why, why, why.. */
1260Sstevel@tonic-gate time_string[strlen(time_string) - 1] = '\0';
1270Sstevel@tonic-gate return (time_string);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * monosec(): returns a monotonically increasing time in seconds that
1320Sstevel@tonic-gate * is not affected by stime(2) or adjtime(2).
1330Sstevel@tonic-gate *
1340Sstevel@tonic-gate * input: void
1350Sstevel@tonic-gate * output: monosec_t: the number of seconds since some time in the past
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate monosec_t
monosec(void)1390Sstevel@tonic-gate monosec(void)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate return (gethrtime() / NANOSEC);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * monosec_to_time(): converts a monosec_t into real wall time
1460Sstevel@tonic-gate *
1470Sstevel@tonic-gate * input: monosec_t: the absolute monosec_t to convert
1480Sstevel@tonic-gate * output: time_t: the absolute time that monosec_t represents in wall time
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate time_t
monosec_to_time(monosec_t abs_monosec)1520Sstevel@tonic-gate monosec_to_time(monosec_t abs_monosec)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate return (abs_monosec - monosec()) + time(NULL);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1583431Scarlsonj * hrtime_to_monosec(): converts a hrtime_t to monosec_t
1590Sstevel@tonic-gate *
1603431Scarlsonj * input: hrtime_t: the time to convert
1613431Scarlsonj * output: monosec_t: the time in monosec_t
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate
1643431Scarlsonj monosec_t
hrtime_to_monosec(hrtime_t hrtime)1653431Scarlsonj hrtime_to_monosec(hrtime_t hrtime)
1660Sstevel@tonic-gate {
1673431Scarlsonj return (hrtime / NANOSEC);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * print_server_msg(): prints a message from a DHCP server
1720Sstevel@tonic-gate *
1733431Scarlsonj * input: dhcp_smach_t *: the state machine the message is associated with
1743431Scarlsonj * const char *: the string to display
1753431Scarlsonj * uint_t: length of string
1760Sstevel@tonic-gate * output: void
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate void
print_server_msg(dhcp_smach_t * dsmp,const char * msg,uint_t msglen)1803431Scarlsonj print_server_msg(dhcp_smach_t *dsmp, const char *msg, uint_t msglen)
1810Sstevel@tonic-gate {
1823431Scarlsonj if (msglen > 0) {
1833431Scarlsonj dhcpmsg(MSG_INFO, "%s: message from server: %.*s",
1843431Scarlsonj dsmp->dsm_name, msglen, msg);
1853431Scarlsonj }
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate * alrm_exit(): Signal handler for SIGARLM. terminates grandparent.
1900Sstevel@tonic-gate *
1910Sstevel@tonic-gate * input: int: signal the handler was called with.
1920Sstevel@tonic-gate *
1930Sstevel@tonic-gate * output: void
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate static void
alrm_exit(int sig)1970Sstevel@tonic-gate alrm_exit(int sig)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate int exitval;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (sig == SIGALRM && grandparent != 0)
2020Sstevel@tonic-gate exitval = EXIT_SUCCESS;
2030Sstevel@tonic-gate else
2040Sstevel@tonic-gate exitval = EXIT_FAILURE;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate _exit(exitval);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * daemonize(): daemonizes the process
2110Sstevel@tonic-gate *
2120Sstevel@tonic-gate * input: void
2130Sstevel@tonic-gate * output: int: 1 on success, 0 on failure
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate int
daemonize(void)2170Sstevel@tonic-gate daemonize(void)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate * We've found that adoption takes sufficiently long that
2210Sstevel@tonic-gate * a dhcpinfo run after dhcpagent -a is started may occur
2220Sstevel@tonic-gate * before the agent is ready to process the request.
2230Sstevel@tonic-gate * The result is an error message and an unhappy user.
2240Sstevel@tonic-gate *
2250Sstevel@tonic-gate * The initial process now sleeps for DHCP_ADOPT_SLEEP,
2260Sstevel@tonic-gate * unless interrupted by a SIGALRM, in which case it
2270Sstevel@tonic-gate * exits immediately. This has the effect that the
2280Sstevel@tonic-gate * grandparent doesn't exit until the dhcpagent is ready
2290Sstevel@tonic-gate * to process requests. This defers the the balance of
2300Sstevel@tonic-gate * the system start-up script processing until the
2310Sstevel@tonic-gate * dhcpagent is ready to field requests.
2320Sstevel@tonic-gate *
2330Sstevel@tonic-gate * grandparent is only set for the adopt case; other
2340Sstevel@tonic-gate * cases do not require the wait.
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if (grandparent != 0)
2380Sstevel@tonic-gate (void) signal(SIGALRM, alrm_exit);
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate switch (fork()) {
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate case -1:
2430Sstevel@tonic-gate return (0);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate case 0:
2460Sstevel@tonic-gate if (grandparent != 0)
2470Sstevel@tonic-gate (void) signal(SIGALRM, SIG_DFL);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * setsid() makes us lose our controlling terminal,
2510Sstevel@tonic-gate * and become both a session leader and a process
2520Sstevel@tonic-gate * group leader.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate (void) setsid();
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate * under POSIX, a session leader can accidentally
2590Sstevel@tonic-gate * (through open(2)) acquire a controlling terminal if
2600Sstevel@tonic-gate * it does not have one. just to be safe, fork again
2610Sstevel@tonic-gate * so we are not a session leader.
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate switch (fork()) {
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate case -1:
2670Sstevel@tonic-gate return (0);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate case 0:
2700Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN);
2710Sstevel@tonic-gate (void) chdir("/");
2720Sstevel@tonic-gate (void) umask(022);
2730Sstevel@tonic-gate closefrom(0);
2740Sstevel@tonic-gate break;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate default:
2770Sstevel@tonic-gate _exit(EXIT_SUCCESS);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate break;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate default:
2820Sstevel@tonic-gate if (grandparent != 0) {
2830Sstevel@tonic-gate (void) signal(SIGCHLD, SIG_IGN);
2843431Scarlsonj /*
2853431Scarlsonj * Note that we're not the agent here, so the DHCP
2863431Scarlsonj * logging subsystem hasn't been configured yet.
2873431Scarlsonj */
2883431Scarlsonj syslog(LOG_DEBUG | LOG_DAEMON, "dhcpagent: daemonize: "
2890Sstevel@tonic-gate "waiting for adoption to complete.");
2900Sstevel@tonic-gate if (sleep(DHCP_ADOPT_SLEEP) == 0) {
2913431Scarlsonj syslog(LOG_WARNING | LOG_DAEMON,
2923431Scarlsonj "dhcpagent: daemonize: timed out awaiting "
2933431Scarlsonj "adoption.");
2940Sstevel@tonic-gate }
2953431Scarlsonj syslog(LOG_DEBUG | LOG_DAEMON, "dhcpagent: daemonize: "
2963431Scarlsonj "wait finished");
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate _exit(EXIT_SUCCESS);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate return (1);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate * update_default_route(): update the interface's default route
3060Sstevel@tonic-gate *
3070Sstevel@tonic-gate * input: int: the type of message; either RTM_ADD or RTM_DELETE
3080Sstevel@tonic-gate * struct in_addr: the default gateway to use
3090Sstevel@tonic-gate * const char *: the interface associated with the route
3100Sstevel@tonic-gate * int: any additional flags (besides RTF_STATIC and RTF_GATEWAY)
3113431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE on failure
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate
3143431Scarlsonj static boolean_t
update_default_route(uint32_t ifindex,int type,struct in_addr * gateway_nbo,int flags)3154106Scarlsonj update_default_route(uint32_t ifindex, int type, struct in_addr *gateway_nbo,
3160Sstevel@tonic-gate int flags)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate struct {
3190Sstevel@tonic-gate struct rt_msghdr rm_mh;
3200Sstevel@tonic-gate struct sockaddr_in rm_dst;
3210Sstevel@tonic-gate struct sockaddr_in rm_gw;
3220Sstevel@tonic-gate struct sockaddr_in rm_mask;
3230Sstevel@tonic-gate struct sockaddr_dl rm_ifp;
3240Sstevel@tonic-gate } rtmsg;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate (void) memset(&rtmsg, 0, sizeof (rtmsg));
3270Sstevel@tonic-gate rtmsg.rm_mh.rtm_version = RTM_VERSION;
3280Sstevel@tonic-gate rtmsg.rm_mh.rtm_msglen = sizeof (rtmsg);
3290Sstevel@tonic-gate rtmsg.rm_mh.rtm_type = type;
3300Sstevel@tonic-gate rtmsg.rm_mh.rtm_pid = getpid();
3310Sstevel@tonic-gate rtmsg.rm_mh.rtm_flags = RTF_GATEWAY | RTF_STATIC | flags;
3320Sstevel@tonic-gate rtmsg.rm_mh.rtm_addrs = RTA_GATEWAY | RTA_DST | RTA_NETMASK | RTA_IFP;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate rtmsg.rm_gw.sin_family = AF_INET;
3350Sstevel@tonic-gate rtmsg.rm_gw.sin_addr = *gateway_nbo;
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate rtmsg.rm_dst.sin_family = AF_INET;
3380Sstevel@tonic-gate rtmsg.rm_dst.sin_addr.s_addr = htonl(INADDR_ANY);
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate rtmsg.rm_mask.sin_family = AF_INET;
3410Sstevel@tonic-gate rtmsg.rm_mask.sin_addr.s_addr = htonl(0);
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate rtmsg.rm_ifp.sdl_family = AF_LINK;
3444106Scarlsonj rtmsg.rm_ifp.sdl_index = ifindex;
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate return (write(rtsock_fd, &rtmsg, sizeof (rtmsg)) == sizeof (rtmsg));
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate * add_default_route(): add the default route to the given gateway
3510Sstevel@tonic-gate *
3520Sstevel@tonic-gate * input: const char *: the name of the interface associated with the route
3530Sstevel@tonic-gate * struct in_addr: the default gateway to add
3543431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE otherwise
3550Sstevel@tonic-gate */
3560Sstevel@tonic-gate
3573431Scarlsonj boolean_t
add_default_route(uint32_t ifindex,struct in_addr * gateway_nbo)3584106Scarlsonj add_default_route(uint32_t ifindex, struct in_addr *gateway_nbo)
3590Sstevel@tonic-gate {
3604106Scarlsonj return (update_default_route(ifindex, RTM_ADD, gateway_nbo, RTF_UP));
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate * del_default_route(): deletes the default route to the given gateway
3650Sstevel@tonic-gate *
3660Sstevel@tonic-gate * input: const char *: the name of the interface associated with the route
3670Sstevel@tonic-gate * struct in_addr: if not INADDR_ANY, the default gateway to remove
3683431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE on failure
3690Sstevel@tonic-gate */
3700Sstevel@tonic-gate
3713431Scarlsonj boolean_t
del_default_route(uint32_t ifindex,struct in_addr * gateway_nbo)3724106Scarlsonj del_default_route(uint32_t ifindex, struct in_addr *gateway_nbo)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate if (gateway_nbo->s_addr == htonl(INADDR_ANY)) /* no router */
3753431Scarlsonj return (B_TRUE);
3760Sstevel@tonic-gate
3774106Scarlsonj return (update_default_route(ifindex, RTM_DELETE, gateway_nbo, 0));
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /*
3813431Scarlsonj * inactivity_shutdown(): shuts down agent if there are no state machines left
3823431Scarlsonj * to manage
3830Sstevel@tonic-gate *
3840Sstevel@tonic-gate * input: iu_tq_t *: unused
3850Sstevel@tonic-gate * void *: unused
3860Sstevel@tonic-gate * output: void
3870Sstevel@tonic-gate */
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /* ARGSUSED */
3900Sstevel@tonic-gate void
inactivity_shutdown(iu_tq_t * tqp,void * arg)3910Sstevel@tonic-gate inactivity_shutdown(iu_tq_t *tqp, void *arg)
3920Sstevel@tonic-gate {
3933431Scarlsonj if (smach_count() > 0) /* shouldn't happen, but... */
3940Sstevel@tonic-gate return;
3950Sstevel@tonic-gate
3963431Scarlsonj dhcpmsg(MSG_VERBOSE, "inactivity_shutdown: timed out");
3973431Scarlsonj
3980Sstevel@tonic-gate iu_stop_handling_events(eh, DHCP_REASON_INACTIVITY, NULL, NULL);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate * graceful_shutdown(): shuts down the agent gracefully
4030Sstevel@tonic-gate *
4040Sstevel@tonic-gate * input: int: the signal that caused graceful_shutdown to be called
4050Sstevel@tonic-gate * output: void
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate void
graceful_shutdown(int sig)4090Sstevel@tonic-gate graceful_shutdown(int sig)
4100Sstevel@tonic-gate {
4112088Smeem iu_stop_handling_events(eh, (sig == SIGTERM ? DHCP_REASON_TERMINATE :
4122088Smeem DHCP_REASON_SIGNAL), drain_script, NULL);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate * bind_sock(): binds a socket to a given IP address and port number
4170Sstevel@tonic-gate *
4180Sstevel@tonic-gate * input: int: the socket to bind
4190Sstevel@tonic-gate * in_port_t: the port number to bind to, host byte order
4200Sstevel@tonic-gate * in_addr_t: the address to bind to, host byte order
4213431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE on failure
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate
4243431Scarlsonj boolean_t
bind_sock(int fd,in_port_t port_hbo,in_addr_t addr_hbo)4250Sstevel@tonic-gate bind_sock(int fd, in_port_t port_hbo, in_addr_t addr_hbo)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate struct sockaddr_in sin;
4280Sstevel@tonic-gate int on = 1;
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate (void) memset(&sin, 0, sizeof (struct sockaddr_in));
4310Sstevel@tonic-gate sin.sin_family = AF_INET;
4320Sstevel@tonic-gate sin.sin_port = htons(port_hbo);
4330Sstevel@tonic-gate sin.sin_addr.s_addr = htonl(addr_hbo);
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate (void) setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (int));
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate return (bind(fd, (struct sockaddr *)&sin, sizeof (sin)) == 0);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /*
4413431Scarlsonj * bind_sock_v6(): binds a socket to a given IP address and port number
4423431Scarlsonj *
4433431Scarlsonj * input: int: the socket to bind
4443431Scarlsonj * in_port_t: the port number to bind to, host byte order
4453431Scarlsonj * in6_addr_t: the address to bind to, network byte order
4463431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE on failure
4473431Scarlsonj */
4483431Scarlsonj
4493431Scarlsonj boolean_t
bind_sock_v6(int fd,in_port_t port_hbo,const in6_addr_t * addr_nbo)4503431Scarlsonj bind_sock_v6(int fd, in_port_t port_hbo, const in6_addr_t *addr_nbo)
4513431Scarlsonj {
4523431Scarlsonj struct sockaddr_in6 sin6;
4533431Scarlsonj int on = 1;
4543431Scarlsonj
4553431Scarlsonj (void) memset(&sin6, 0, sizeof (struct sockaddr_in6));
4563431Scarlsonj sin6.sin6_family = AF_INET6;
4573431Scarlsonj sin6.sin6_port = htons(port_hbo);
4583431Scarlsonj if (addr_nbo != NULL) {
4593431Scarlsonj (void) memcpy(&sin6.sin6_addr, addr_nbo,
4603431Scarlsonj sizeof (sin6.sin6_addr));
4613431Scarlsonj }
4623431Scarlsonj
4633431Scarlsonj (void) setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (int));
4643431Scarlsonj
4653431Scarlsonj return (bind(fd, (struct sockaddr *)&sin6, sizeof (sin6)) == 0);
4663431Scarlsonj }
4673431Scarlsonj
4683431Scarlsonj /*
4690Sstevel@tonic-gate * valid_hostname(): check whether a string is a valid hostname
4700Sstevel@tonic-gate *
4710Sstevel@tonic-gate * input: const char *: the string to verify as a hostname
4720Sstevel@tonic-gate * output: boolean_t: B_TRUE if the string is a valid hostname
4730Sstevel@tonic-gate *
4740Sstevel@tonic-gate * Note that we accept both host names beginning with a digit and
4750Sstevel@tonic-gate * those containing hyphens. Neither is strictly legal according
4760Sstevel@tonic-gate * to the RFCs, but both are in common practice, so we endeavour
4770Sstevel@tonic-gate * to not break what customers are using.
4780Sstevel@tonic-gate */
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate static boolean_t
valid_hostname(const char * hostname)4810Sstevel@tonic-gate valid_hostname(const char *hostname)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate unsigned int i;
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate for (i = 0; hostname[i] != '\0'; i++) {
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate if (isalpha(hostname[i]) || isdigit(hostname[i]) ||
4880Sstevel@tonic-gate (((hostname[i] == '-') || (hostname[i] == '.')) && (i > 0)))
4890Sstevel@tonic-gate continue;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate return (B_FALSE);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate return (i > 0);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate * iffile_to_hostname(): return the hostname contained on a line of the form
4990Sstevel@tonic-gate *
5000Sstevel@tonic-gate * [ ^I]*inet[ ^I]+hostname[\n]*\0
5010Sstevel@tonic-gate *
5020Sstevel@tonic-gate * in the file located at the specified path
5030Sstevel@tonic-gate *
5040Sstevel@tonic-gate * input: const char *: the path of the file to look in for the hostname
5050Sstevel@tonic-gate * output: const char *: the hostname at that path, or NULL on failure
5060Sstevel@tonic-gate */
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate #define IFLINE_MAX 1024 /* maximum length of a hostname.<if> line */
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate const char *
iffile_to_hostname(const char * path)5110Sstevel@tonic-gate iffile_to_hostname(const char *path)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate FILE *fp;
5140Sstevel@tonic-gate static char ifline[IFLINE_MAX];
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate fp = fopen(path, "r");
5170Sstevel@tonic-gate if (fp == NULL)
5180Sstevel@tonic-gate return (NULL);
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate * /etc/hostname.<if> may contain multiple ifconfig commands, but each
5220Sstevel@tonic-gate * such command is on a separate line (see the "while read ifcmds" code
5230Sstevel@tonic-gate * in /etc/init.d/inetinit). Thus we will read the file a line at a
5240Sstevel@tonic-gate * time, searching for a line of the form
5250Sstevel@tonic-gate *
5260Sstevel@tonic-gate * [ ^I]*inet[ ^I]+hostname[\n]*\0
5270Sstevel@tonic-gate *
5280Sstevel@tonic-gate * extract the host name from it, and check it for validity.
5290Sstevel@tonic-gate */
5300Sstevel@tonic-gate while (fgets(ifline, sizeof (ifline), fp) != NULL) {
5310Sstevel@tonic-gate char *p;
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate if ((p = strstr(ifline, "inet")) != NULL) {
5340Sstevel@tonic-gate if ((p != ifline) && !isspace(p[-1])) {
5350Sstevel@tonic-gate (void) fclose(fp);
5360Sstevel@tonic-gate return (NULL);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate p += 4; /* skip over "inet" and expect spaces or tabs */
5390Sstevel@tonic-gate if ((*p == '\n') || (*p == '\0')) {
5400Sstevel@tonic-gate (void) fclose(fp);
5410Sstevel@tonic-gate return (NULL);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate if (isspace(*p)) {
5440Sstevel@tonic-gate char *nlptr;
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate /* no need to read more of the file */
5470Sstevel@tonic-gate (void) fclose(fp);
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate while (isspace(*p))
5500Sstevel@tonic-gate p++;
5510Sstevel@tonic-gate if ((nlptr = strrchr(p, '\n')) != NULL)
5520Sstevel@tonic-gate *nlptr = '\0';
5530Sstevel@tonic-gate if (strlen(p) > MAXHOSTNAMELEN) {
5540Sstevel@tonic-gate dhcpmsg(MSG_WARNING,
5550Sstevel@tonic-gate "iffile_to_hostname:"
5560Sstevel@tonic-gate " host name too long");
5570Sstevel@tonic-gate return (NULL);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate if (valid_hostname(p)) {
5600Sstevel@tonic-gate return (p);
5610Sstevel@tonic-gate } else {
5620Sstevel@tonic-gate dhcpmsg(MSG_WARNING,
5630Sstevel@tonic-gate "iffile_to_hostname:"
5640Sstevel@tonic-gate " host name not valid");
5650Sstevel@tonic-gate return (NULL);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate } else {
5680Sstevel@tonic-gate (void) fclose(fp);
5690Sstevel@tonic-gate return (NULL);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate (void) fclose(fp);
5750Sstevel@tonic-gate return (NULL);
5760Sstevel@tonic-gate }
5773431Scarlsonj
5783431Scarlsonj /*
5793431Scarlsonj * init_timer(): set up a DHCP timer
5803431Scarlsonj *
5813431Scarlsonj * input: dhcp_timer_t *: the timer to set up
5823431Scarlsonj * output: void
5833431Scarlsonj */
5843431Scarlsonj
5853431Scarlsonj void
init_timer(dhcp_timer_t * dt,lease_t startval)5863431Scarlsonj init_timer(dhcp_timer_t *dt, lease_t startval)
5873431Scarlsonj {
5883431Scarlsonj dt->dt_id = -1;
5893431Scarlsonj dt->dt_start = startval;
5903431Scarlsonj }
5913431Scarlsonj
5923431Scarlsonj /*
5933431Scarlsonj * cancel_timer(): cancel a DHCP timer
5943431Scarlsonj *
5953431Scarlsonj * input: dhcp_timer_t *: the timer to cancel
5963431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE otherwise
5973431Scarlsonj */
5983431Scarlsonj
5993431Scarlsonj boolean_t
cancel_timer(dhcp_timer_t * dt)6003431Scarlsonj cancel_timer(dhcp_timer_t *dt)
6013431Scarlsonj {
6023431Scarlsonj if (dt->dt_id == -1)
6033431Scarlsonj return (B_TRUE);
6043431Scarlsonj
6053431Scarlsonj if (iu_cancel_timer(tq, dt->dt_id, NULL) == 1) {
6063431Scarlsonj dt->dt_id = -1;
6073431Scarlsonj return (B_TRUE);
6083431Scarlsonj }
6093431Scarlsonj
6103431Scarlsonj return (B_FALSE);
6113431Scarlsonj }
6123431Scarlsonj
6133431Scarlsonj /*
6143431Scarlsonj * schedule_timer(): schedule a DHCP timer. Note that it must not be already
6153431Scarlsonj * running, and that we can't cancel here. If it were, and
6163431Scarlsonj * we did, we'd leak a reference to the callback argument.
6173431Scarlsonj *
6183431Scarlsonj * input: dhcp_timer_t *: the timer to schedule
6193431Scarlsonj * output: boolean_t: B_TRUE on success, B_FALSE otherwise
6203431Scarlsonj */
6213431Scarlsonj
6223431Scarlsonj boolean_t
schedule_timer(dhcp_timer_t * dt,iu_tq_callback_t * cbfunc,void * arg)6233431Scarlsonj schedule_timer(dhcp_timer_t *dt, iu_tq_callback_t *cbfunc, void *arg)
6243431Scarlsonj {
6253431Scarlsonj if (dt->dt_id != -1)
6263431Scarlsonj return (B_FALSE);
6273431Scarlsonj dt->dt_id = iu_schedule_timer(tq, dt->dt_start, cbfunc, arg);
6283431Scarlsonj return (dt->dt_id != -1);
6293431Scarlsonj }
6303431Scarlsonj
6313431Scarlsonj /*
6323431Scarlsonj * dhcpv6_status_code(): report on a DHCPv6 status code found in an option
6333431Scarlsonj * buffer.
6343431Scarlsonj *
6353431Scarlsonj * input: const dhcpv6_option_t *: pointer to option
6363431Scarlsonj * uint_t: option length
6373431Scarlsonj * const char **: error string (nul-terminated)
6383431Scarlsonj * const char **: message from server (unterminated)
6393431Scarlsonj * uint_t *: length of server message
6403431Scarlsonj * output: int: -1 on error, or >= 0 for a DHCPv6 status code
6413431Scarlsonj */
6423431Scarlsonj
6433431Scarlsonj int
dhcpv6_status_code(const dhcpv6_option_t * d6o,uint_t olen,const char ** estr,const char ** msg,uint_t * msglenp)6443431Scarlsonj dhcpv6_status_code(const dhcpv6_option_t *d6o, uint_t olen, const char **estr,
6453431Scarlsonj const char **msg, uint_t *msglenp)
6463431Scarlsonj {
6473431Scarlsonj uint16_t status;
6483431Scarlsonj static const char *v6_status[] = {
6493431Scarlsonj NULL,
6503431Scarlsonj "Unknown reason",
6513431Scarlsonj "Server has no addresses available",
6523431Scarlsonj "Client record unavailable",
6533431Scarlsonj "Prefix inappropriate for link",
6543431Scarlsonj "Client must use multicast",
6553431Scarlsonj "No prefix available"
6563431Scarlsonj };
6573431Scarlsonj static char sbuf[32];
6583431Scarlsonj
6593431Scarlsonj *estr = "";
6603431Scarlsonj *msg = "";
6613431Scarlsonj *msglenp = 0;
6623431Scarlsonj if (d6o == NULL)
6633431Scarlsonj return (0);
6643431Scarlsonj olen -= sizeof (*d6o);
6653431Scarlsonj if (olen < 2) {
6663431Scarlsonj *estr = "garbled status code";
6673431Scarlsonj return (-1);
6683431Scarlsonj }
6693431Scarlsonj
6703431Scarlsonj *msg = (const char *)(d6o + 1) + 2;
6713431Scarlsonj *msglenp = olen - 2;
6723431Scarlsonj
6733431Scarlsonj (void) memcpy(&status, d6o + 1, sizeof (status));
6743431Scarlsonj status = ntohs(status);
6753431Scarlsonj if (status > 0) {
6763431Scarlsonj if (status > DHCPV6_STAT_NOPREFIX) {
6773431Scarlsonj (void) snprintf(sbuf, sizeof (sbuf), "status %u",
6783431Scarlsonj status);
6793431Scarlsonj *estr = sbuf;
6803431Scarlsonj } else {
6813431Scarlsonj *estr = v6_status[status];
6823431Scarlsonj }
6833431Scarlsonj }
6843431Scarlsonj return (status);
6853431Scarlsonj }
686*12989SVasumathi.Sundaram@oracle.COM
687*12989SVasumathi.Sundaram@oracle.COM void
write_lease_to_hostconf(dhcp_smach_t * dsmp)688*12989SVasumathi.Sundaram@oracle.COM write_lease_to_hostconf(dhcp_smach_t *dsmp)
689*12989SVasumathi.Sundaram@oracle.COM {
690*12989SVasumathi.Sundaram@oracle.COM PKT_LIST *plp[2];
691*12989SVasumathi.Sundaram@oracle.COM const char *hcfile;
692*12989SVasumathi.Sundaram@oracle.COM
693*12989SVasumathi.Sundaram@oracle.COM hcfile = ifname_to_hostconf(dsmp->dsm_name, dsmp->dsm_isv6);
694*12989SVasumathi.Sundaram@oracle.COM plp[0] = dsmp->dsm_ack;
695*12989SVasumathi.Sundaram@oracle.COM plp[1] = dsmp->dsm_orig_ack;
696*12989SVasumathi.Sundaram@oracle.COM if (write_hostconf(dsmp->dsm_name, plp, 2,
697*12989SVasumathi.Sundaram@oracle.COM monosec_to_time(dsmp->dsm_curstart_monosec),
698*12989SVasumathi.Sundaram@oracle.COM dsmp->dsm_isv6) != -1) {
699*12989SVasumathi.Sundaram@oracle.COM dhcpmsg(MSG_DEBUG, "wrote lease to %s", hcfile);
700*12989SVasumathi.Sundaram@oracle.COM } else if (errno == EROFS) {
701*12989SVasumathi.Sundaram@oracle.COM dhcpmsg(MSG_DEBUG, "%s is on a read-only file "
702*12989SVasumathi.Sundaram@oracle.COM "system; not saving lease", hcfile);
703*12989SVasumathi.Sundaram@oracle.COM } else {
704*12989SVasumathi.Sundaram@oracle.COM dhcpmsg(MSG_ERR, "cannot write %s (reboot will "
705*12989SVasumathi.Sundaram@oracle.COM "not use cached configuration)", hcfile);
706*12989SVasumathi.Sundaram@oracle.COM }
707*12989SVasumathi.Sundaram@oracle.COM }
708