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