xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * INIT_REBOOT state of the DHCP client state machine.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <limits.h>
34*0Sstevel@tonic-gate #include <sys/socket.h>
35*0Sstevel@tonic-gate #include <netinet/in.h>
36*0Sstevel@tonic-gate #include <netinet/dhcp.h>
37*0Sstevel@tonic-gate #include <netinet/udp.h>
38*0Sstevel@tonic-gate #include <netinet/ip_var.h>
39*0Sstevel@tonic-gate #include <netinet/udp_var.h>
40*0Sstevel@tonic-gate #include <dhcpmsg.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "agent.h"
44*0Sstevel@tonic-gate #include "packet.h"
45*0Sstevel@tonic-gate #include "states.h"
46*0Sstevel@tonic-gate #include "util.h"
47*0Sstevel@tonic-gate #include "interface.h"
48*0Sstevel@tonic-gate #include "defaults.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static stop_func_t	stop_init_reboot;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * dhcp_init_reboot(): attempts to reuse a cached configuration on an interface
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface to reuse the configuration on
56*0Sstevel@tonic-gate  *  output: void
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate void
60*0Sstevel@tonic-gate dhcp_init_reboot(struct ifslist *ifsp)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	dhcp_pkt_t		*dpkt;
63*0Sstevel@tonic-gate 	const char		*reqhost;
64*0Sstevel@tonic-gate 	char			hostfile[PATH_MAX + 1];
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
67*0Sstevel@tonic-gate 	    "INIT_REBOOT", ifsp->if_name);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	ifsp->if_state = INIT_REBOOT;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (register_acknak(ifsp) == 0) {
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 		ifsp->if_state   = INIT;
74*0Sstevel@tonic-gate 		ifsp->if_dflags |= DHCP_IF_FAILED;
75*0Sstevel@tonic-gate 		ipc_action_finish(ifsp, DHCP_IPC_E_MEMORY);
76*0Sstevel@tonic-gate 		async_finish(ifsp);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
79*0Sstevel@tonic-gate 		    "collect ACK/NAK packets, reverting to INIT on %s",
80*0Sstevel@tonic-gate 		    ifsp->if_name);
81*0Sstevel@tonic-gate 		return;
82*0Sstevel@tonic-gate 	}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/*
85*0Sstevel@tonic-gate 	 * assemble DHCPREQUEST message.  The max dhcp message size
86*0Sstevel@tonic-gate 	 * option is set to the interface max, minus the size of the udp and
87*0Sstevel@tonic-gate 	 * ip headers.
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	dpkt = init_pkt(ifsp, REQUEST);
91*0Sstevel@tonic-gate 	add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
92*0Sstevel@tonic-gate 	    ifsp->if_ack->pkt->yiaddr.s_addr);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
95*0Sstevel@tonic-gate 	add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE, htons(ifsp->if_max -
96*0Sstevel@tonic-gate 			sizeof (struct udpiphdr)));
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
99*0Sstevel@tonic-gate 	add_pkt_opt(dpkt, CD_REQUEST_LIST, ifsp->if_prl, ifsp->if_prllen);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/*
102*0Sstevel@tonic-gate 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
103*0Sstevel@tonic-gate 	 * is found in /etc/hostname.<ifname>
104*0Sstevel@tonic-gate 	 */
105*0Sstevel@tonic-gate 	if (df_get_bool(ifsp->if_name, DF_REQUEST_HOSTNAME)) {
106*0Sstevel@tonic-gate 		dhcpmsg(MSG_DEBUG, "dhcp_selecting: DF_REQUEST_HOSTNAME");
107*0Sstevel@tonic-gate 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
108*0Sstevel@tonic-gate 		    ifsp->if_name);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
111*0Sstevel@tonic-gate 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
112*0Sstevel@tonic-gate 			if ((ifsp->if_reqhost = strdup(reqhost)) != NULL)
113*0Sstevel@tonic-gate 				add_pkt_opt(dpkt, CD_HOSTNAME, ifsp->if_reqhost,
114*0Sstevel@tonic-gate 				    strlen(ifsp->if_reqhost));
115*0Sstevel@tonic-gate 			else
116*0Sstevel@tonic-gate 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
117*0Sstevel@tonic-gate 				    " allocate memory for host name option");
118*0Sstevel@tonic-gate 		}
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	add_pkt_opt(dpkt, CD_END, NULL, 0);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	(void) send_pkt(ifsp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
128*0Sstevel@tonic-gate  *
129*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface REQUESTs are being sent on
130*0Sstevel@tonic-gate  *	    unsigned int: the number of REQUESTs sent so far
131*0Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if retransmissions should stop
132*0Sstevel@tonic-gate  */
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate static boolean_t
135*0Sstevel@tonic-gate stop_init_reboot(struct ifslist *ifsp, unsigned int n_requests)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	if (n_requests >= DHCP_MAX_REQUESTS) {
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 		(void) unregister_acknak(ifsp);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
142*0Sstevel@tonic-gate 		    "using remainder of existing lease on %s", ifsp->if_name);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		/*
145*0Sstevel@tonic-gate 		 * we already stuck our old ack in ifsp->if_ack and
146*0Sstevel@tonic-gate 		 * relativized the packet times, so we can just
147*0Sstevel@tonic-gate 		 * pretend that the server sent it to us and move to
148*0Sstevel@tonic-gate 		 * bound.  if that fails, fall back to selecting.
149*0Sstevel@tonic-gate 		 */
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 		if (dhcp_bound(ifsp, NULL) == 0)
152*0Sstevel@tonic-gate 			dhcp_selecting(ifsp);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 		return (B_TRUE);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	return (B_FALSE);
158*0Sstevel@tonic-gate }
159