1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/dhcp.h>
#include <netinet/udp.h>
#include <netinet/ip_var.h>
#include <netinet/udp_var.h>
#include <libinetutil.h>
#include <dhcpmsg.h>
#include <string.h>
#include "packet.h"
#include "agent.h"
#include "script_handler.h"
#include "interface.h"
#include "states.h"
#include "util.h"
/*
* next_extend_time(): returns the next time an EXTEND request should be sent
*
* input: monosec_t: the absolute time when the next state is entered
* output: uint32_t: the number of seconds in the future to send the next
* EXTEND request
*/
static uint32_t
next_extend_time(monosec_t limit_monosec)
{
monosec_t current_monosec = monosec();
if (limit_monosec - current_monosec < DHCP_REBIND_MIN)
return (0);
return ((limit_monosec - current_monosec) / 2);
}
/*
* dhcp_renew(): attempts to renew a DHCP lease
*
* input: iu_tq_t *: unused
* void *: the ifslist to renew the lease on
* output: void
*/
/* ARGSUSED */
void
dhcp_renew(iu_tq_t *tqp, void *arg)
{
struct ifslist *ifsp = (struct ifslist *)arg;
uint32_t next;
ifsp->if_timer[DHCP_T1_TIMER] = -1;
if (check_ifs(ifsp) == 0) {
(void) release_ifs(ifsp);
return;
}
/*
* sanity check: don't send packets if we're past t2.
*/
if (monosec() > (ifsp->if_curstart_monosec + ifsp->if_t2))
return;
next = next_extend_time(ifsp->if_curstart_monosec + ifsp->if_t2);
/*
* if there isn't an async event pending, then try to renew.
*/
if (!async_pending(ifsp))
if (async_start(ifsp, DHCP_EXTEND, B_FALSE) != 0)
/*
* try to send extend. if we don't succeed,
* async_timeout() will clean us up.
*/
(void) dhcp_extending(ifsp);
/*
* if we're within DHCP_REBIND_MIN seconds of REBINDING, don't
* reschedule ourselves.
*/
if (next == 0)
return;
/*
* no big deal if we can't reschedule; we still have the REBIND
* state to save us.
*/
(void) schedule_ifs_timer(ifsp, DHCP_T1_TIMER, next, dhcp_renew);
}
/*
* dhcp_rebind(): attempts to renew a DHCP lease from the REBINDING state
*
* input: iu_tq_t *: unused
* void *: the ifslist to renew the lease on
* output: void
*/
/* ARGSUSED */
void
dhcp_rebind(iu_tq_t *tqp, void *arg)
{
struct ifslist *ifsp = (struct ifslist *)arg;
uint32_t next;
ifsp->if_timer[DHCP_T2_TIMER] = -1;
if (check_ifs(ifsp) == 0) {
(void) release_ifs(ifsp);
return;
}
/*
* sanity check: don't send packets if we've already expired.
*/
if (monosec() > (ifsp->if_curstart_monosec + ifsp->if_lease))
return;
next = next_extend_time(ifsp->if_curstart_monosec + ifsp->if_lease);
/*
* if this is our first venture into the REBINDING state, then
* reset the server address. we know the renew timer has
* already been cancelled (or we wouldn't be here).
*/
if (ifsp->if_state == RENEWING) {
ifsp->if_state = REBINDING;
ifsp->if_server.s_addr = htonl(INADDR_BROADCAST);
}
/*
* if there isn't an async event pending, then try to rebind.
*/
if (!async_pending(ifsp))
if (async_start(ifsp, DHCP_EXTEND, B_FALSE) != 0)
/*
* try to send extend. if we don't succeed,
* async_timeout() will clean us up.
*/
(void) dhcp_extending(ifsp);
/*
* if we're within DHCP_REBIND_MIN seconds of EXPIRE, don't
* reschedule ourselves.
*/
if (next == 0) {
dhcpmsg(MSG_WARNING, "dhcp_rebind: lease on %s expires in less "
"than %i seconds!", ifsp->if_name, DHCP_REBIND_MIN);
return;
}
if (schedule_ifs_timer(ifsp, DHCP_T2_TIMER, next, dhcp_rebind) == 0)
/*
* we'll just end up in dhcp_expire(), but it sure sucks.
*/
dhcpmsg(MSG_CRIT, "dhcp_rebind: cannot reschedule another "
"rebind attempt; lease may expire for %s", ifsp->if_name);
}
/*
* dhcp_restart(): callback function to script_start
*
* input: struct ifslist *: the interface to be restarted
* const char *: unused
* output: int: always 1
*/
/* ARGSUSED */
static int
dhcp_restart(struct ifslist *ifsp, const char *msg)
{
dhcpmsg(MSG_INFO, "lease expired on %s -- restarting DHCP",
ifsp->if_name);
/*
* in the case where the lease is less than DHCP_REBIND_MIN
* seconds, we will never enter dhcp_renew() and thus the packet
* counters will not be reset. in that case, reset them here.
*/
if (ifsp->if_state == BOUND) {
ifsp->if_bad_offers = 0;
ifsp->if_sent = 0;
ifsp->if_received = 0;
}
(void) canonize_ifs(ifsp);
/* reset_ifs() in dhcp_selecting() will clean up any leftover state */
dhcp_selecting(ifsp);
return (1);
}
/*
* dhcp_expire(): expires a lease on a given interface and restarts DHCP
*
* input: iu_tq_t *: unused
* void *: the ifslist to expire the lease on
* output: void
*/
/* ARGSUSED */
void
dhcp_expire(iu_tq_t *tqp, void *arg)
{
struct ifslist *ifsp = (struct ifslist *)arg;
ifsp->if_timer[DHCP_LEASE_TIMER] = -1;
if (check_ifs(ifsp) == 0) {
(void) release_ifs(ifsp);
return;
}
if (async_pending(ifsp))
if (async_cancel(ifsp) == 0) {
dhcpmsg(MSG_WARNING, "dhcp_expire: cannot cancel "
"current asynchronous command against %s",
ifsp->if_name);
/*
* try to schedule ourselves for callback.
* we're really situation critical here
* there's not much hope for us if this fails.
*/
if (iu_schedule_timer(tq, DHCP_EXPIRE_WAIT, dhcp_expire,
ifsp) != -1) {
hold_ifs(ifsp);
return;
}
dhcpmsg(MSG_CRIT, "dhcp_expire: cannot reschedule "
"dhcp_expire to get called back, proceeding...");
}
/*
* just march on if this fails; at worst someone will be able
* to async_start() while we're actually busy with our own
* asynchronous transaction. better than not having a lease.
*/
if (async_start(ifsp, DHCP_START, B_FALSE) == 0)
dhcpmsg(MSG_WARNING, "dhcp_expire: cannot start asynchronous "
"transaction on %s, continuing...", ifsp->if_name);
(void) script_start(ifsp, EVENT_EXPIRE, dhcp_restart, NULL, NULL);
}
/*
* dhcp_extending(): sends a REQUEST to extend a lease on a given interface
* and registers to receive the ACK/NAK server reply
*
* input: struct ifslist *: the interface to send the REQUEST on
* output: int: 1 if the extension request was sent, 0 otherwise
*/
int
dhcp_extending(struct ifslist *ifsp)
{
dhcp_pkt_t *dpkt;
if (ifsp->if_state == BOUND) {
ifsp->if_neg_monosec = monosec();
ifsp->if_state = RENEWING;
ifsp->if_bad_offers = 0;
ifsp->if_sent = 0;
ifsp->if_received = 0;
}
dhcpmsg(MSG_DEBUG, "dhcp_extending: registering dhcp_acknak on %s",
ifsp->if_name);
if (register_acknak(ifsp) == 0) {
ipc_action_finish(ifsp, DHCP_IPC_E_MEMORY);
async_finish(ifsp);
dhcpmsg(MSG_WARNING, "dhcp_extending: cannot register "
"dhcp_acknak for %s, not sending renew request",
ifsp->if_name);
return (0);
}
/*
* assemble DHCPREQUEST message. The max dhcp message size
* option is set to the interface max, minus the size of the udp and
* ip headers.
*/
dpkt = init_pkt(ifsp, REQUEST);
dpkt->pkt->ciaddr.s_addr = ifsp->if_addr.s_addr;
add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE, htons(ifsp->if_max -
sizeof (struct udpiphdr)));
add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
add_pkt_opt(dpkt, CD_REQUEST_LIST, ifsp->if_prl, ifsp->if_prllen);
/*
* if_reqhost was set for this interface in dhcp_selecting()
* if the REQUEST_HOSTNAME option was set and a host name was
* found.
*/
if (ifsp->if_reqhost != NULL) {
add_pkt_opt(dpkt, CD_HOSTNAME, ifsp->if_reqhost,
strlen(ifsp->if_reqhost));
}
add_pkt_opt(dpkt, CD_END, NULL, 0);
/*
* if we can't send the packet, leave the event handler registered
* anyway, since we're not expecting to get any other types of
* packets in other than ACKs/NAKs anyway.
*/
return (send_pkt(ifsp, dpkt, ifsp->if_server.s_addr, NULL));
}
|