Print this page
7388 Support DHCP Client FQDN. Allow IAID/DUID for all v4.
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/cmd-inet/sbin/dhcpagent/agent.c
+++ new/usr/src/cmd/cmd-inet/sbin/dhcpagent/agent.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
|
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23 + * Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
23 24 */
24 25
25 26 #include <sys/types.h>
26 27 #include <stdlib.h>
27 28 #include <assert.h>
28 29 #include <errno.h>
29 30 #include <locale.h>
30 31 #include <string.h>
31 32 #include <unistd.h>
32 33 #include <signal.h>
33 34 #include <stdio.h>
34 35 #include <stdio_ext.h>
35 36 #include <dhcp_hostconf.h>
36 37 #include <dhcpagent_ipc.h>
37 38 #include <dhcpagent_util.h>
38 39 #include <dhcpmsg.h>
40 +#include <dhcp_inittab.h>
41 +#include <dhcp_symbol.h>
39 42 #include <netinet/dhcp.h>
40 43 #include <net/route.h>
41 44 #include <sys/sockio.h>
42 45 #include <sys/stat.h>
43 46 #include <stropts.h>
44 47 #include <fcntl.h>
45 48 #include <sys/scsi/adapters/iscsi_if.h>
46 49
47 50 #include "async.h"
48 51 #include "agent.h"
49 52 #include "script_handler.h"
50 53 #include "util.h"
51 54 #include "class_id.h"
52 55 #include "states.h"
53 56 #include "packet.h"
54 57 #include "interface.h"
55 58 #include "defaults.h"
56 59
57 60 #ifndef TEXT_DOMAIN
58 61 #define TEXT_DOMAIN "SYS_TEST"
59 62 #endif
60 63
61 64 iu_timer_id_t inactivity_id;
62 65 int class_id_len = 0;
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
63 66 char *class_id;
64 67 iu_eh_t *eh;
65 68 iu_tq_t *tq;
66 69 pid_t grandparent;
67 70 int rtsock_fd;
68 71
69 72 static boolean_t shutdown_started = B_FALSE;
70 73 static boolean_t do_adopt = B_FALSE;
71 74 static unsigned int debug_level = 0;
72 75 static iu_eh_callback_t accept_event, ipc_event, rtsock_event;
76 +static void dhcp_smach_set_msg_reqhost(dhcp_smach_t *dsmp,
77 + ipc_action_t *iap);
78 +static DHCP_OPT * dhcp_get_ack_or_state(const dhcp_smach_t *dsmp,
79 + const PKT_LIST *plp, uint_t codenum, boolean_t *did_alloc);
73 80
74 81 /*
75 82 * The ipc_cmd_allowed[] table indicates which IPC commands are allowed in
76 83 * which states; a non-zero value indicates the command is permitted.
77 84 *
78 85 * START is permitted if the state machine is fresh, or if we are in the
79 86 * process of trying to obtain a lease (as a convenience to save the
80 87 * administrator from having to do an explicit DROP). EXTEND, RELEASE, and
81 88 * GET_TAG require a lease to be obtained in order to make sense. INFORM is
82 89 * permitted if the interface is fresh or has an INFORM in progress or
83 90 * previously done on it -- otherwise a DROP or RELEASE is first required.
84 91 * PING and STATUS always make sense and thus are always permitted, as is DROP
85 92 * in order to permit the administrator to always bail out.
86 93 */
87 94 static int ipc_cmd_allowed[DHCP_NSTATES][DHCP_NIPC] = {
88 95 /* D E P R S S I G */
89 96 /* R X I E T T N E */
90 97 /* O T N L A A F T */
91 98 /* P E G E R T O _ */
92 99 /* . N . A T U R T */
93 100 /* . D . S . S M A */
94 101 /* . . . E . . . G */
95 102 /* INIT */ { 1, 0, 1, 0, 1, 1, 1, 0 },
96 103 /* SELECTING */ { 1, 0, 1, 0, 1, 1, 0, 0 },
97 104 /* REQUESTING */ { 1, 0, 1, 0, 1, 1, 0, 0 },
98 105 /* PRE_BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 },
99 106 /* BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 },
100 107 /* RENEWING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
101 108 /* REBINDING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
102 109 /* INFORMATION */ { 1, 0, 1, 0, 1, 1, 1, 1 },
103 110 /* INIT_REBOOT */ { 1, 0, 1, 1, 1, 1, 0, 0 },
104 111 /* ADOPTING */ { 1, 0, 1, 1, 0, 1, 0, 0 },
105 112 /* INFORM_SENT */ { 1, 0, 1, 0, 1, 1, 1, 0 },
106 113 /* DECLINING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
107 114 /* RELEASING */ { 1, 0, 1, 0, 0, 1, 0, 1 },
108 115 };
109 116
110 117 #define CMD_ISPRIV 0x1 /* Command requires privileges */
111 118 #define CMD_CREATE 0x2 /* Command creates an interface */
112 119 #define CMD_BOOTP 0x4 /* Command is valid with BOOTP */
113 120 #define CMD_IMMED 0x8 /* Reply is immediate (no BUSY state) */
114 121
115 122 static uint_t ipc_cmd_flags[DHCP_NIPC] = {
116 123 /* DHCP_DROP */ CMD_ISPRIV|CMD_BOOTP,
117 124 /* DHCP_EXTEND */ CMD_ISPRIV,
118 125 /* DHCP_PING */ CMD_BOOTP|CMD_IMMED,
119 126 /* DHCP_RELEASE */ CMD_ISPRIV,
120 127 /* DHCP_START */ CMD_CREATE|CMD_ISPRIV|CMD_BOOTP,
121 128 /* DHCP_STATUS */ CMD_BOOTP|CMD_IMMED,
122 129 /* DHCP_INFORM */ CMD_CREATE|CMD_ISPRIV,
123 130 /* DHCP_GET_TAG */ CMD_BOOTP|CMD_IMMED
124 131 };
125 132
126 133 static boolean_t is_iscsi_active(void);
127 134
128 135 int
129 136 main(int argc, char **argv)
130 137 {
131 138 boolean_t is_daemon = B_TRUE;
132 139 boolean_t is_verbose;
133 140 int ipc_fd;
134 141 int c;
135 142 int aware = RTAW_UNDER_IPMP;
136 143 struct rlimit rl;
137 144
138 145 debug_level = df_get_int("", B_FALSE, DF_DEBUG_LEVEL);
139 146 is_verbose = df_get_bool("", B_FALSE, DF_VERBOSE);
140 147
141 148 /*
142 149 * -l is ignored for compatibility with old agent.
143 150 */
144 151
145 152 while ((c = getopt(argc, argv, "vd:l:fa")) != EOF) {
146 153
147 154 switch (c) {
148 155
149 156 case 'a':
150 157 do_adopt = B_TRUE;
151 158 grandparent = getpid();
152 159 break;
153 160
154 161 case 'd':
155 162 debug_level = strtoul(optarg, NULL, 0);
156 163 break;
157 164
158 165 case 'f':
159 166 is_daemon = B_FALSE;
160 167 break;
161 168
162 169 case 'v':
163 170 is_verbose = B_TRUE;
164 171 break;
165 172
166 173 case '?':
167 174 (void) fprintf(stderr, "usage: %s [-a] [-d n] [-f] [-v]"
168 175 "\n", argv[0]);
169 176 return (EXIT_FAILURE);
170 177
171 178 default:
172 179 break;
173 180 }
174 181 }
175 182
176 183 (void) setlocale(LC_ALL, "");
177 184 (void) textdomain(TEXT_DOMAIN);
178 185
179 186 if (geteuid() != 0) {
180 187 dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
181 188 dhcpmsg(MSG_ERROR, "must be super-user");
182 189 dhcpmsg_fini();
183 190 return (EXIT_FAILURE);
184 191 }
185 192
186 193 if (is_daemon && daemonize() == 0) {
187 194 dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
188 195 dhcpmsg(MSG_ERR, "cannot become daemon, exiting");
189 196 dhcpmsg_fini();
190 197 return (EXIT_FAILURE);
191 198 }
192 199
193 200 /*
194 201 * Seed the random number generator, since we're going to need it
195 202 * to set transaction id's and for exponential backoff.
196 203 */
197 204 srand48(gethrtime() ^ gethostid() ^ getpid());
198 205
199 206 dhcpmsg_init(argv[0], is_daemon, is_verbose, debug_level);
200 207 (void) atexit(dhcpmsg_fini);
201 208
202 209 tq = iu_tq_create();
203 210 eh = iu_eh_create();
204 211
205 212 if (eh == NULL || tq == NULL) {
206 213 errno = ENOMEM;
207 214 dhcpmsg(MSG_ERR, "cannot create timer queue or event handler");
208 215 return (EXIT_FAILURE);
209 216 }
210 217
211 218 /*
212 219 * ignore most signals that could be reasonably generated.
213 220 */
214 221
215 222 (void) signal(SIGTERM, graceful_shutdown);
216 223 (void) signal(SIGQUIT, graceful_shutdown);
217 224 (void) signal(SIGPIPE, SIG_IGN);
218 225 (void) signal(SIGUSR1, SIG_IGN);
219 226 (void) signal(SIGUSR2, SIG_IGN);
220 227 (void) signal(SIGINT, SIG_IGN);
221 228 (void) signal(SIGHUP, SIG_IGN);
222 229 (void) signal(SIGCHLD, SIG_IGN);
223 230
224 231 /*
225 232 * upon SIGTHAW we need to refresh any non-infinite leases.
226 233 */
227 234
228 235 (void) iu_eh_register_signal(eh, SIGTHAW, refresh_smachs, NULL);
229 236
230 237 class_id = get_class_id();
231 238 if (class_id != NULL)
232 239 class_id_len = strlen(class_id);
233 240 else
234 241 dhcpmsg(MSG_WARNING, "get_class_id failed, continuing "
235 242 "with no vendor class id");
236 243
237 244 /*
238 245 * the inactivity timer is enabled any time there are no
239 246 * interfaces under DHCP control. if DHCP_INACTIVITY_WAIT
240 247 * seconds transpire without an interface under DHCP control,
241 248 * the agent shuts down.
242 249 */
243 250
244 251 inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
245 252 inactivity_shutdown, NULL);
246 253
247 254 /*
248 255 * max out the number available descriptors, just in case..
249 256 */
250 257
251 258 rl.rlim_cur = RLIM_INFINITY;
252 259 rl.rlim_max = RLIM_INFINITY;
253 260 if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
254 261 dhcpmsg(MSG_ERR, "setrlimit failed");
255 262
256 263 (void) enable_extended_FILE_stdio(-1, -1);
257 264
258 265 /*
259 266 * Create and bind default IP sockets used to control interfaces and to
260 267 * catch stray packets.
261 268 */
262 269
263 270 if (!dhcp_ip_default())
264 271 return (EXIT_FAILURE);
265 272
266 273 /*
267 274 * create the ipc channel that the agent will listen for
268 275 * requests on, and register it with the event handler so that
269 276 * `accept_event' will be called back.
270 277 */
271 278
272 279 switch (dhcp_ipc_init(&ipc_fd)) {
273 280
274 281 case 0:
275 282 break;
276 283
277 284 case DHCP_IPC_E_BIND:
278 285 dhcpmsg(MSG_ERROR, "dhcp_ipc_init: cannot bind to port "
279 286 "%i (agent already running?)", IPPORT_DHCPAGENT);
280 287 return (EXIT_FAILURE);
281 288
282 289 default:
283 290 dhcpmsg(MSG_ERROR, "dhcp_ipc_init failed");
284 291 return (EXIT_FAILURE);
285 292 }
286 293
287 294 if (iu_register_event(eh, ipc_fd, POLLIN, accept_event, 0) == -1) {
288 295 dhcpmsg(MSG_ERR, "cannot register ipc fd for messages");
289 296 return (EXIT_FAILURE);
290 297 }
291 298
292 299 /*
293 300 * Create the global routing socket. This is used for monitoring
294 301 * interface transitions, so that we learn about the kernel's Duplicate
295 302 * Address Detection status, and for inserting and removing default
296 303 * routes as learned from DHCP servers. Both v4 and v6 are handed
297 304 * with this one socket.
298 305 */
299 306 rtsock_fd = socket(PF_ROUTE, SOCK_RAW, 0);
300 307 if (rtsock_fd == -1) {
301 308 dhcpmsg(MSG_ERR, "cannot open routing socket");
302 309 return (EXIT_FAILURE);
303 310 }
304 311
305 312 /*
306 313 * We're IPMP-aware and can manage IPMP test addresses, so issue
307 314 * RT_AWARE to get routing socket messages for interfaces under IPMP.
308 315 */
309 316 if (setsockopt(rtsock_fd, SOL_ROUTE, RT_AWARE, &aware,
310 317 sizeof (aware)) == -1) {
311 318 dhcpmsg(MSG_ERR, "cannot set RT_AWARE on routing socket");
312 319 return (EXIT_FAILURE);
313 320 }
314 321
315 322 if (iu_register_event(eh, rtsock_fd, POLLIN, rtsock_event, 0) == -1) {
316 323 dhcpmsg(MSG_ERR, "cannot register routing socket for messages");
317 324 return (EXIT_FAILURE);
318 325 }
319 326
320 327 /*
321 328 * if the -a (adopt) option was specified, try to adopt the
322 329 * kernel-managed interface before we start.
323 330 */
324 331
325 332 if (do_adopt && !dhcp_adopt())
326 333 return (EXIT_FAILURE);
327 334
328 335 /*
329 336 * For DHCPv6, we own all of the interfaces marked DHCPRUNNING. As
330 337 * we're starting operation here, if there are any of those interfaces
331 338 * lingering around, they're strays, and need to be removed.
332 339 *
333 340 * It might be nice to save these addresses off somewhere -- for both
334 341 * v4 and v6 -- and use them as hints for later negotiation.
335 342 */
336 343 remove_v6_strays();
337 344
338 345 /*
339 346 * enter the main event loop; this is where all the real work
340 347 * takes place (through registering events and scheduling timers).
341 348 * this function only returns when the agent is shutting down.
342 349 */
343 350
344 351 switch (iu_handle_events(eh, tq)) {
345 352
346 353 case -1:
347 354 dhcpmsg(MSG_WARNING, "iu_handle_events exited abnormally");
348 355 break;
349 356
350 357 case DHCP_REASON_INACTIVITY:
351 358 dhcpmsg(MSG_INFO, "no interfaces to manage, shutting down...");
352 359 break;
353 360
354 361 case DHCP_REASON_TERMINATE:
355 362 dhcpmsg(MSG_INFO, "received SIGTERM, shutting down...");
356 363 break;
357 364
358 365 case DHCP_REASON_SIGNAL:
359 366 dhcpmsg(MSG_WARNING, "received unexpected signal, shutting "
360 367 "down...");
361 368 break;
362 369 }
363 370
364 371 (void) iu_eh_unregister_signal(eh, SIGTHAW, NULL);
365 372
366 373 iu_eh_destroy(eh);
367 374 iu_tq_destroy(tq);
368 375
369 376 return (EXIT_SUCCESS);
370 377 }
371 378
372 379 /*
373 380 * drain_script(): event loop callback during shutdown
374 381 *
375 382 * input: eh_t *: unused
376 383 * void *: unused
377 384 * output: boolean_t: B_TRUE if event loop should exit; B_FALSE otherwise
378 385 */
379 386
380 387 /* ARGSUSED */
381 388 boolean_t
382 389 drain_script(iu_eh_t *ehp, void *arg)
383 390 {
384 391 if (shutdown_started == B_FALSE) {
385 392 shutdown_started = B_TRUE;
386 393 /*
387 394 * Check if the system is diskless client and/or
388 395 * there are active iSCSI sessions
389 396 *
390 397 * Do not drop the lease, or the system will be
391 398 * unable to sync(dump) through nfs/iSCSI driver
392 399 */
393 400 if (!do_adopt && !is_iscsi_active()) {
394 401 nuke_smach_list();
395 402 }
396 403 }
397 404 return (script_count == 0);
398 405 }
399 406
400 407 /*
401 408 * accept_event(): accepts a new connection on the ipc socket and registers
402 409 * to receive its messages with the event handler
403 410 *
404 411 * input: iu_eh_t *: unused
405 412 * int: the file descriptor in the iu_eh_t * the connection came in on
406 413 * (other arguments unused)
407 414 * output: void
408 415 */
409 416
410 417 /* ARGSUSED */
411 418 static void
412 419 accept_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
413 420 {
414 421 int client_fd;
415 422 int is_priv;
416 423
417 424 if (dhcp_ipc_accept(fd, &client_fd, &is_priv) != 0) {
418 425 dhcpmsg(MSG_ERR, "accept_event: accept on ipc socket");
419 426 return;
420 427 }
421 428
422 429 if (iu_register_event(eh, client_fd, POLLIN, ipc_event,
423 430 (void *)is_priv) == -1) {
424 431 dhcpmsg(MSG_ERROR, "accept_event: cannot register ipc socket "
425 432 "for callback");
426 433 }
427 434 }
428 435
429 436 /*
430 437 * ipc_event(): processes incoming ipc requests
431 438 *
432 439 * input: iu_eh_t *: unused
433 440 * int: the file descriptor in the iu_eh_t * the request came in on
434 441 * short: unused
435 442 * iu_event_id_t: event ID
436 443 * void *: indicates whether the request is from a privileged client
437 444 * output: void
438 445 */
439 446
440 447 /* ARGSUSED */
441 448 static void
442 449 ipc_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
443 450 {
444 451 ipc_action_t ia, *iap;
445 452 dhcp_smach_t *dsmp;
446 453 int error, is_priv = (int)arg;
447 454 const char *ifname;
448 455 boolean_t isv6;
449 456 boolean_t dsm_created = B_FALSE;
450 457
451 458 ipc_action_init(&ia);
452 459 error = dhcp_ipc_recv_request(fd, &ia.ia_request,
453 460 DHCP_IPC_REQUEST_WAIT);
454 461 if (error != DHCP_IPC_SUCCESS) {
455 462 if (error != DHCP_IPC_E_EOF) {
456 463 dhcpmsg(MSG_ERROR,
457 464 "ipc_event: dhcp_ipc_recv_request failed: %s",
458 465 dhcp_ipc_strerror(error));
459 466 } else {
460 467 dhcpmsg(MSG_DEBUG, "ipc_event: connection closed");
461 468 }
462 469 if ((dsmp = lookup_smach_by_event(id)) != NULL) {
463 470 ipc_action_finish(dsmp, error);
464 471 } else {
465 472 (void) iu_unregister_event(eh, id, NULL);
466 473 (void) dhcp_ipc_close(fd);
467 474 }
468 475 return;
469 476 }
470 477
471 478 /* Fill in temporary ipc_action structure for utility functions */
472 479 ia.ia_cmd = DHCP_IPC_CMD(ia.ia_request->message_type);
473 480 ia.ia_fd = fd;
474 481 ia.ia_eid = id;
475 482
476 483 if (ia.ia_cmd >= DHCP_NIPC) {
477 484 dhcpmsg(MSG_ERROR,
478 485 "ipc_event: invalid command (%s) attempted on %s",
479 486 dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
480 487 send_error_reply(&ia, DHCP_IPC_E_CMD_UNKNOWN);
481 488 return;
482 489 }
483 490
484 491 /* return EPERM for any of the privileged actions */
485 492
486 493 if (!is_priv && (ipc_cmd_flags[ia.ia_cmd] & CMD_ISPRIV)) {
487 494 dhcpmsg(MSG_WARNING,
488 495 "ipc_event: privileged ipc command (%s) attempted on %s",
489 496 dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
490 497 send_error_reply(&ia, DHCP_IPC_E_PERM);
491 498 return;
492 499 }
493 500
494 501 /*
495 502 * Try to locate the state machine associated with this command. If
496 503 * the command is DHCP_START or DHCP_INFORM and there isn't a state
497 504 * machine already, make one (there may already be one from a previous
498 505 * failed attempt to START or INFORM). Otherwise, verify the reference
499 506 * is still valid.
500 507 *
501 508 * The interface name may be blank. In that case, we look up the
502 509 * primary interface, and the requested type (v4 or v6) doesn't matter.
503 510 */
504 511
505 512 isv6 = (ia.ia_request->message_type & DHCP_V6) != 0;
506 513 ifname = ia.ia_request->ifname;
507 514 if (*ifname == '\0')
508 515 dsmp = primary_smach(isv6);
509 516 else
510 517 dsmp = lookup_smach(ifname, isv6);
511 518
512 519 if (dsmp != NULL) {
513 520 /* Note that verify_smach drops a reference */
514 521 hold_smach(dsmp);
515 522 if (!verify_smach(dsmp))
516 523 dsmp = NULL;
517 524 }
518 525
519 526 if (dsmp == NULL) {
520 527 /*
521 528 * If the user asked for the primary DHCP interface by giving
522 529 * an empty string and there is no primary, then check if we're
523 530 * handling dhcpinfo. If so, then simulate primary selection.
524 531 * Otherwise, report failure.
525 532 */
526 533 if (ifname[0] == '\0') {
527 534 if (ia.ia_cmd == DHCP_GET_TAG)
528 535 dsmp = info_primary_smach(isv6);
529 536 if (dsmp == NULL)
530 537 error = DHCP_IPC_E_NOPRIMARY;
531 538
532 539 /*
533 540 * If there's no interface, and we're starting up, then create
534 541 * it now, along with a state machine for it. Note that if
535 542 * insert_smach fails, it discards the LIF reference.
536 543 */
537 544 } else if (ipc_cmd_flags[ia.ia_cmd] & CMD_CREATE) {
538 545 dhcp_lif_t *lif;
539 546
540 547 lif = attach_lif(ifname, isv6, &error);
541 548 if (lif != NULL &&
542 549 (dsmp = insert_smach(lif, &error)) != NULL) {
543 550 /*
544 551 * Get client ID for logical interface. (V4
545 552 * only, because V6 plumbs its own interfaces.)
546 553 */
547 554 error = get_smach_cid(dsmp);
548 555 if (error != DHCP_IPC_SUCCESS) {
549 556 remove_smach(dsmp);
550 557 dsmp = NULL;
551 558 }
552 559 dsm_created = (dsmp != NULL);
553 560 }
554 561
555 562 /*
556 563 * Otherwise, this is an operation on an unknown interface.
557 564 */
558 565 } else {
559 566 error = DHCP_IPC_E_UNKIF;
560 567 }
561 568 if (dsmp == NULL) {
562 569 send_error_reply(&ia, error);
563 570 return;
564 571 }
565 572 }
566 573
567 574 /*
568 575 * If this is a request for DHCP to manage a lease on an address,
569 576 * ensure that IFF_DHCPRUNNING is set (we don't set this when the lif
570 577 * is created because the lif may have been created for INFORM).
571 578 */
572 579 if (ia.ia_cmd == DHCP_START &&
573 580 (error = set_lif_dhcp(dsmp->dsm_lif)) != DHCP_IPC_SUCCESS) {
574 581 if (dsm_created)
575 582 remove_smach(dsmp);
576 583 send_error_reply(&ia, error);
577 584 return;
578 585 }
579 586
580 587 if ((dsmp->dsm_dflags & DHCP_IF_BOOTP) &&
581 588 !(ipc_cmd_flags[ia.ia_cmd] & CMD_BOOTP)) {
582 589 dhcpmsg(MSG_ERROR, "command %s not valid for BOOTP on %s",
583 590 dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
584 591 send_error_reply(&ia, DHCP_IPC_E_BOOTP);
585 592 return;
586 593 }
587 594
588 595 /*
589 596 * verify that the state machine is in a state which will allow the
590 597 * command. we do this up front so that we can return an error
591 598 * *before* needlessly cancelling an in-progress transaction.
592 599 */
593 600
594 601 if (!check_cmd_allowed(dsmp->dsm_state, ia.ia_cmd)) {
595 602 dhcpmsg(MSG_DEBUG,
596 603 "in state %s; not allowing %s command on %s",
597 604 dhcp_state_to_string(dsmp->dsm_state),
598 605 dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
599 606 send_error_reply(&ia,
600 607 ia.ia_cmd == DHCP_START && dsmp->dsm_state != INIT ?
601 608 DHCP_IPC_E_RUNNING : DHCP_IPC_E_OUTSTATE);
602 609 return;
603 610 }
604 611
605 612 dhcpmsg(MSG_DEBUG, "in state %s; allowing %s command on %s",
606 613 dhcp_state_to_string(dsmp->dsm_state),
607 614 dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
608 615
609 616 if ((ia.ia_request->message_type & DHCP_PRIMARY) && is_priv)
610 617 make_primary(dsmp);
611 618
612 619 /*
613 620 * The current design dictates that there can be only one outstanding
614 621 * transaction per state machine -- this simplifies the code
615 622 * considerably and also fits well with RFCs 2131 and 3315. It is
616 623 * worth classifying the different DHCP commands into synchronous
617 624 * (those which we will handle now and reply to immediately) and
618 625 * asynchronous (those which require transactions and will be completed
619 626 * at an indeterminate time in the future):
620 627 *
621 628 * DROP: removes the agent's management of a state machine.
622 629 * asynchronous as the script program may be invoked.
623 630 *
624 631 * PING: checks to see if the agent has a named state machine.
625 632 * synchronous, since no packets need to be sent
626 633 * to the DHCP server.
627 634 *
628 635 * STATUS: returns information about a state machine.
629 636 * synchronous, since no packets need to be sent
630 637 * to the DHCP server.
631 638 *
632 639 * RELEASE: releases the agent's management of a state machine
633 640 * and brings the associated interfaces down. asynchronous
634 641 * as the script program may be invoked.
635 642 *
636 643 * EXTEND: renews a lease. asynchronous, since the agent
637 644 * needs to wait for an ACK, etc.
638 645 *
639 646 * START: starts DHCP on a named state machine. asynchronous since
640 647 * the agent needs to wait for OFFERs, ACKs, etc.
641 648 *
642 649 * INFORM: obtains configuration parameters for the system using
643 650 * externally configured interface. asynchronous, since the
644 651 * agent needs to wait for an ACK.
645 652 *
646 653 * Notice that EXTEND, INFORM, START, DROP and RELEASE are
647 654 * asynchronous. Notice also that asynchronous commands may occur from
648 655 * within the agent -- for instance, the agent will need to do implicit
649 656 * EXTENDs to extend the lease. In order to make the code simpler, the
650 657 * following rules apply for asynchronous commands:
651 658 *
652 659 * There can only be one asynchronous command at a time per state
653 660 * machine. The current asynchronous command is managed by the async_*
654 661 * api: async_start(), async_finish(), and async_cancel().
655 662 * async_start() starts management of a new asynchronous command on an
656 663 * state machine, which should only be done after async_cancel() to
657 664 * terminate a previous command. When the command is completed,
658 665 * async_finish() should be called.
659 666 *
660 667 * Asynchronous commands started by a user command have an associated
661 668 * ipc_action which provides the agent with information for how to get
662 669 * in touch with the user command when the action completes. These
663 670 * ipc_action records also have an associated timeout which may be
664 671 * infinite. ipc_action_start() should be called when starting an
665 672 * asynchronous command requested by a user, which sets up the timer
666 673 * and keeps track of the ipc information (file descriptor, request
667 674 * type). When the asynchronous command completes, ipc_action_finish()
668 675 * should be called to return a command status code to the user and
669 676 * close the ipc connection). If the command does not complete before
670 677 * the timer fires, ipc_action_timeout() is called which closes the ipc
671 678 * connection and returns DHCP_IPC_E_TIMEOUT to the user. Note that
672 679 * independent of ipc_action_timeout(), ipc_action_finish() should be
673 680 * called.
674 681 *
675 682 * on a case-by-case basis, here is what happens (per state machine):
676 683 *
677 684 * o When an asynchronous command is requested, then
678 685 * async_cancel() is called to terminate any non-user
679 686 * action in progress. If there's a user action running,
680 687 * the user command is sent DHCP_IPC_E_PEND.
681 688 *
682 689 * o otherwise, the the transaction is started with
683 690 * async_start(). if the transaction is on behalf
684 691 * of a user, ipc_action_start() is called to keep
685 692 * track of the ipc information and set up the
686 693 * ipc_action timer.
687 694 *
688 695 * o if the command completes normally and before a
689 696 * timeout fires, then async_finish() is called.
690 697 * if there was an associated ipc_action,
691 698 * ipc_action_finish() is called to complete it.
692 699 *
693 700 * o if the command fails before a timeout fires, then
694 701 * async_finish() is called, and the state machine is
695 702 * is returned to a known state based on the command.
696 703 * if there was an associated ipc_action,
697 704 * ipc_action_finish() is called to complete it.
698 705 *
699 706 * o if the ipc_action timer fires before command
700 707 * completion, then DHCP_IPC_E_TIMEOUT is returned to
701 708 * the user. however, the transaction continues to
702 709 * be carried out asynchronously.
703 710 */
704 711
705 712 if (ipc_cmd_flags[ia.ia_cmd] & CMD_IMMED) {
706 713 /*
707 714 * Only immediate commands (ping, status, get_tag) need to
708 715 * worry about freeing ia through one of the reply functions
709 716 * before returning.
710 717 */
711 718 iap = &ia;
712 719 } else {
713 720 /*
714 721 * if shutdown request has been received, send back an error.
715 722 */
716 723 if (shutdown_started) {
717 724 send_error_reply(&ia, DHCP_IPC_E_OUTSTATE);
718 725 return;
719 726 }
720 727
721 728 if (dsmp->dsm_dflags & DHCP_IF_BUSY) {
722 729 send_error_reply(&ia, DHCP_IPC_E_PEND);
723 730 return;
724 731 }
725 732
726 733 if (!ipc_action_start(dsmp, &ia)) {
727 734 dhcpmsg(MSG_WARNING, "ipc_event: ipc_action_start "
728 735 "failed for %s", dsmp->dsm_name);
729 736 send_error_reply(&ia, DHCP_IPC_E_MEMORY);
730 737 return;
731 738 }
732 739
733 740 /* Action structure consumed by above function */
734 741 iap = &dsmp->dsm_ia;
735 742 }
736 743
737 744 switch (iap->ia_cmd) {
738 745
739 746 case DHCP_DROP:
740 747 if (dsmp->dsm_droprelease)
741 748 break;
742 749 dsmp->dsm_droprelease = B_TRUE;
743 750
744 751 /*
745 752 * Ensure that a timer associated with the existing state
746 753 * doesn't pop while we're waiting for the script to complete.
|
↓ open down ↓ |
664 lines elided |
↑ open up ↑ |
747 754 * (If so, chaos can result -- e.g., a timer causes us to end
748 755 * up in dhcp_selecting() would start acquiring a new lease on
749 756 * dsmp while our DHCP_DROP dismantling is ongoing.)
750 757 */
751 758 cancel_smach_timers(dsmp);
752 759 (void) script_start(dsmp, isv6 ? EVENT_DROP6 : EVENT_DROP,
753 760 dhcp_drop, NULL, NULL);
754 761 break; /* not an immediate function */
755 762
756 763 case DHCP_EXTEND:
764 + dhcp_smach_set_msg_reqhost(dsmp, iap);
757 765 (void) dhcp_extending(dsmp);
758 766 break;
759 767
760 768 case DHCP_GET_TAG: {
761 769 dhcp_optnum_t optnum;
762 770 void *opt = NULL;
763 771 uint_t optlen;
764 772 boolean_t did_alloc = B_FALSE;
765 773 PKT_LIST *ack = dsmp->dsm_ack;
766 774 int i;
767 775
768 776 /*
769 777 * verify the request makes sense.
770 778 */
771 779
772 780 if (iap->ia_request->data_type != DHCP_TYPE_OPTNUM ||
773 781 iap->ia_request->data_length != sizeof (dhcp_optnum_t)) {
774 782 send_error_reply(iap, DHCP_IPC_E_PROTO);
775 783 break;
776 784 }
777 785
778 786 (void) memcpy(&optnum, iap->ia_request->buffer,
779 787 sizeof (dhcp_optnum_t));
780 788
781 789 load_option:
782 790 switch (optnum.category) {
783 791
784 792 case DSYM_SITE: /* FALLTHRU */
785 793 case DSYM_STANDARD:
|
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
786 794 for (i = 0; i < dsmp->dsm_pillen; i++) {
787 795 if (dsmp->dsm_pil[i] == optnum.code)
788 796 break;
789 797 }
790 798 if (i < dsmp->dsm_pillen)
791 799 break;
792 800 if (isv6) {
793 801 opt = dhcpv6_pkt_option(ack, NULL, optnum.code,
794 802 NULL);
795 803 } else {
796 - if (optnum.code <= DHCP_LAST_OPT)
797 - opt = ack->opts[optnum.code];
804 + opt = dhcp_get_ack_or_state(dsmp, ack, optnum.code,
805 + &did_alloc);
798 806 }
799 807 break;
800 808
801 809 case DSYM_VENDOR:
802 810 if (isv6) {
803 811 dhcpv6_option_t *d6o;
804 812 uint32_t ent;
805 813
806 814 /*
807 815 * Look through vendor options to find our
808 816 * enterprise number.
809 817 */
810 818 d6o = NULL;
811 819 for (;;) {
812 820 d6o = dhcpv6_pkt_option(ack, d6o,
813 821 DHCPV6_OPT_VENDOR_OPT, &optlen);
814 822 if (d6o == NULL)
815 823 break;
816 824 optlen -= sizeof (*d6o);
817 825 if (optlen < sizeof (ent))
818 826 continue;
819 827 (void) memcpy(&ent, d6o + 1,
820 828 sizeof (ent));
821 829 if (ntohl(ent) != DHCPV6_SUN_ENT)
822 830 continue;
823 831 break;
824 832 }
825 833 if (d6o != NULL) {
826 834 /*
827 835 * Now find the requested vendor option
828 836 * within the vendor options block.
829 837 */
830 838 opt = dhcpv6_find_option(
831 839 (char *)(d6o + 1) + sizeof (ent),
832 840 optlen - sizeof (ent), NULL,
833 841 optnum.code, NULL);
834 842 }
835 843 } else {
836 844 /*
837 845 * the test against VS_OPTION_START is broken
838 846 * up into two tests to avoid compiler warnings
839 847 * under intel.
840 848 */
841 849 if ((optnum.code > VS_OPTION_START ||
842 850 optnum.code == VS_OPTION_START) &&
843 851 optnum.code <= VS_OPTION_END)
844 852 opt = ack->vs[optnum.code];
845 853 }
846 854 break;
847 855
848 856 case DSYM_FIELD:
849 857 if (isv6) {
850 858 dhcpv6_message_t *d6m =
851 859 (dhcpv6_message_t *)ack->pkt;
852 860 dhcpv6_option_t *d6o;
853 861
854 862 /* Validate the packet field the user wants */
855 863 optlen = optnum.code + optnum.size;
856 864 if (d6m->d6m_msg_type ==
857 865 DHCPV6_MSG_RELAY_FORW ||
858 866 d6m->d6m_msg_type ==
859 867 DHCPV6_MSG_RELAY_REPL) {
860 868 if (optlen > sizeof (dhcpv6_relay_t))
861 869 break;
862 870 } else {
863 871 if (optlen > sizeof (*d6m))
864 872 break;
865 873 }
866 874
867 875 opt = malloc(sizeof (*d6o) + optnum.size);
868 876 if (opt != NULL) {
|
↓ open down ↓ |
61 lines elided |
↑ open up ↑ |
869 877 d6o = opt;
870 878 d6o->d6o_code = htons(optnum.code);
871 879 d6o->d6o_len = htons(optnum.size);
872 880 (void) memcpy(d6o + 1, (caddr_t)d6m +
873 881 optnum.code, optnum.size);
874 882 }
875 883 } else {
876 884 if (optnum.code + optnum.size > sizeof (PKT))
877 885 break;
878 886
879 - /*
880 - * + 2 to account for option code and length
881 - * byte
882 - */
883 - opt = malloc(optnum.size + 2);
887 + opt = malloc(optnum.size + DHCP_OPT_META_LEN);
884 888 if (opt != NULL) {
885 889 DHCP_OPT *v4opt = opt;
886 890
887 891 v4opt->len = optnum.size;
888 892 v4opt->code = optnum.code;
889 893 (void) memcpy(v4opt->value,
890 894 (caddr_t)ack->pkt + optnum.code,
891 895 optnum.size);
892 896 }
893 897 }
894 898
895 899 if (opt == NULL) {
896 900 send_error_reply(iap, DHCP_IPC_E_MEMORY);
897 901 return;
|
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
898 902 }
899 903 did_alloc = B_TRUE;
900 904 break;
901 905
902 906 default:
903 907 send_error_reply(iap, DHCP_IPC_E_PROTO);
904 908 return;
905 909 }
906 910
907 911 /*
908 - * return the option payload, if there was one. the "+ 2"
909 - * accounts for the option code number and length byte.
912 + * return the option payload, if there was one.
910 913 */
911 914
912 915 if (opt != NULL) {
913 916 if (isv6) {
914 917 dhcpv6_option_t d6ov;
915 918
916 919 (void) memcpy(&d6ov, opt, sizeof (d6ov));
917 920 optlen = ntohs(d6ov.d6o_len) + sizeof (d6ov);
918 921 } else {
919 - optlen = ((DHCP_OPT *)opt)->len + 2;
922 + optlen = ((DHCP_OPT *)opt)->len + DHCP_OPT_META_LEN;
920 923 }
921 924 send_data_reply(iap, 0, DHCP_TYPE_OPTION, opt, optlen);
922 925
923 926 if (did_alloc)
924 927 free(opt);
925 928 break;
926 929 } else if (ack != dsmp->dsm_orig_ack) {
927 930 /*
928 931 * There wasn't any definition for the option in the
929 932 * current ack, so now retry with the original ack if
930 933 * the original ack is not the current ack.
931 934 */
932 935 ack = dsmp->dsm_orig_ack;
933 936 goto load_option;
934 937 }
935 938
936 939 /*
937 940 * note that an "okay" response is returned either in
938 941 * the case of an unknown option or a known option
939 942 * with no payload. this is okay (for now) since
940 943 * dhcpinfo checks whether an option is valid before
941 944 * ever performing ipc with the agent.
942 945 */
943 946
944 947 send_ok_reply(iap);
945 948 break;
946 949 }
947 950
948 951 case DHCP_INFORM:
949 952 dhcp_inform(dsmp);
950 953 /* next destination: dhcp_acknak() */
951 954 break; /* not an immediate function */
952 955
953 956 case DHCP_PING:
954 957 if (dsmp->dsm_dflags & DHCP_IF_FAILED)
955 958 send_error_reply(iap, DHCP_IPC_E_FAILEDIF);
956 959 else
957 960 send_ok_reply(iap);
958 961 break;
959 962
960 963 case DHCP_RELEASE:
961 964 if (dsmp->dsm_droprelease)
962 965 break;
963 966 dsmp->dsm_droprelease = B_TRUE;
|
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
964 967 cancel_smach_timers(dsmp); /* see comment in DHCP_DROP above */
965 968 (void) script_start(dsmp, isv6 ? EVENT_RELEASE6 :
966 969 EVENT_RELEASE, dhcp_release, "Finished with lease.", NULL);
967 970 break; /* not an immediate function */
968 971
969 972 case DHCP_START: {
970 973 PKT_LIST *ack, *oack;
971 974 PKT_LIST *plp[2];
972 975
973 976 deprecate_leases(dsmp);
977 + dhcp_smach_set_msg_reqhost(dsmp, iap);
974 978
975 979 /*
976 980 * if we have a valid hostconf lying around, then jump
977 981 * into INIT_REBOOT. if it fails, we'll end up going
978 982 * through the whole selecting() procedure again.
979 983 */
980 984
981 985 error = read_hostconf(dsmp->dsm_name, plp, 2, dsmp->dsm_isv6);
982 986 ack = error > 0 ? plp[0] : NULL;
983 987 oack = error > 1 ? plp[1] : NULL;
984 988
985 989 /*
986 990 * If the allocation of the old ack fails, that's fine;
987 991 * continue without it.
988 992 */
989 993 if (oack == NULL)
990 994 oack = ack;
991 995
992 996 /*
993 997 * As long as we've allocated something, start using it.
994 998 */
995 999 if (ack != NULL) {
996 1000 dsmp->dsm_orig_ack = oack;
997 1001 dsmp->dsm_ack = ack;
998 1002 dhcp_init_reboot(dsmp);
999 1003 /* next destination: dhcp_acknak() */
1000 1004 break;
1001 1005 }
1002 1006
1003 1007 /*
1004 1008 * if not debugging, wait for a few seconds before
1005 1009 * going into SELECTING.
1006 1010 */
1007 1011
1008 1012 if (debug_level == 0 && set_start_timer(dsmp)) {
1009 1013 /* next destination: dhcp_start() */
1010 1014 break;
1011 1015 } else {
1012 1016 dhcp_selecting(dsmp);
1013 1017 /* next destination: dhcp_requesting() */
1014 1018 break;
1015 1019 }
1016 1020 }
1017 1021
1018 1022 case DHCP_STATUS: {
1019 1023 dhcp_status_t status;
1020 1024 dhcp_lease_t *dlp;
1021 1025
1022 1026 status.if_began = monosec_to_time(dsmp->dsm_curstart_monosec);
1023 1027
1024 1028 /*
1025 1029 * We return information on just the first lease as being
1026 1030 * representative of the lot. A better status mechanism is
1027 1031 * needed.
1028 1032 */
1029 1033 dlp = dsmp->dsm_leases;
1030 1034
1031 1035 if (dlp == NULL ||
1032 1036 dlp->dl_lifs->lif_expire.dt_start == DHCP_PERM) {
1033 1037 status.if_t1 = DHCP_PERM;
1034 1038 status.if_t2 = DHCP_PERM;
1035 1039 status.if_lease = DHCP_PERM;
1036 1040 } else {
1037 1041 status.if_t1 = status.if_began +
1038 1042 dlp->dl_t1.dt_start;
1039 1043 status.if_t2 = status.if_began +
1040 1044 dlp->dl_t2.dt_start;
1041 1045 status.if_lease = status.if_began +
1042 1046 dlp->dl_lifs->lif_expire.dt_start;
1043 1047 }
1044 1048
1045 1049 status.version = DHCP_STATUS_VER;
1046 1050 status.if_state = dsmp->dsm_state;
1047 1051 status.if_dflags = dsmp->dsm_dflags;
1048 1052 status.if_sent = dsmp->dsm_sent;
1049 1053 status.if_recv = dsmp->dsm_received;
1050 1054 status.if_bad_offers = dsmp->dsm_bad_offers;
|
↓ open down ↓ |
67 lines elided |
↑ open up ↑ |
1051 1055
1052 1056 (void) strlcpy(status.if_name, dsmp->dsm_name, LIFNAMSIZ);
1053 1057
1054 1058 send_data_reply(iap, 0, DHCP_TYPE_STATUS, &status,
1055 1059 sizeof (dhcp_status_t));
1056 1060 break;
1057 1061 }
1058 1062 }
1059 1063 }
1060 1064
1065 +/*
1066 + * dhcp_smach_set_msg_reqhost(): set dsm_msg_reqhost based on the message
1067 + * content of a DHCP IPC message
1068 + *
1069 + * input: dhcp_smach_t *: the state machine instance;
1070 + * ipc_action_t *: the decoded DHCP IPC message;
1071 + * output: void
1072 + */
1073 +
1074 +static void
1075 +dhcp_smach_set_msg_reqhost(dhcp_smach_t *dsmp, ipc_action_t *iap)
1076 +{
1077 + if (dsmp->dsm_msg_reqhost != NULL) {
1078 + dhcpmsg(MSG_DEBUG,
1079 + "dhcp_smach_set_msg_reqhost: nullify former value, %s",
1080 + dsmp->dsm_msg_reqhost);
1081 + free(dsmp->dsm_msg_reqhost);
1082 + dsmp->dsm_msg_reqhost = NULL;
1083 + }
1084 + free(dsmp->dsm_reqfqdn);
1085 + dsmp->dsm_reqfqdn = NULL;
1086 +
1087 + /*
1088 + * if a STANDARD/HOSTNAME was sent in the IPC request, then copy that
1089 + * value into the state machine data if decoding succeeds. Otherwise,
1090 + * log to indicate at what step the decoding stopped.
1091 + */
1092 +
1093 + if (dsmp->dsm_isv6) {
1094 + dhcpmsg(MSG_DEBUG, "dhcp_smach_set_msg_reqhost: ipv6 is not"
1095 + " handled");
1096 + } else if (iap->ia_request->data_type != DHCP_TYPE_OPTION) {
1097 + dhcpmsg(MSG_DEBUG, "dhcp_smach_set_msg_reqhost: request type %d is"
1098 + " not DHCP_TYPE_OPTION", iap->ia_request->data_type);
1099 + } else {
1100 + if (iap->ia_request->buffer == NULL
1101 + || iap->ia_request->data_length <= DHCP_OPT_META_LEN) {
1102 + dhcpmsg(MSG_WARNING, "dhcp_smach_set_msg_reqhost:"
1103 + " DHCP_TYPE_OPTION ia_request buffer is NULL (0) or"
1104 + " short (1): %d",
1105 + iap->ia_request->buffer == NULL ? 0 : 1);
1106 + } else {
1107 + DHCP_OPT *d4o = (DHCP_OPT *)iap->ia_request->buffer;
1108 +
1109 + if (d4o->code != CD_HOSTNAME)
1110 + {
1111 + dhcpmsg(MSG_DEBUG,
1112 + "dhcp_smach_set_msg_reqhost: ignoring DHCPv4"
1113 + " option %u", d4o->code);
1114 + } else if (iap->ia_request->data_length - DHCP_OPT_META_LEN
1115 + != d4o->len) {
1116 + dhcpmsg(MSG_WARNING, "dhcp_smach_set_msg_reqhost:"
1117 + " unexpected DHCP_OPT buffer length %u for CD_HOSTNAME"
1118 + " option length %u", iap->ia_request->data_length,
1119 + d4o->len);
1120 + } else {
1121 +
1122 + dhcp_symbol_t *entry = inittab_getbycode(ITAB_CAT_STANDARD,
1123 + ITAB_CONS_INFO, CD_HOSTNAME);
1124 + if (entry == NULL) {
1125 + dhcpmsg(MSG_WARNING,
1126 + "dhcp_smach_set_msg_reqhost: error getting"
1127 + " ITAB_CAT_STANDARD ITAB_CONS_INFO"
1128 + " CD_HOSTNAME entry");
1129 + } else {
1130 + char *value = inittab_decode(entry, d4o->value,
1131 + d4o->len, /* just_payload */ B_TRUE);
1132 + if (value == NULL) {
1133 + dhcpmsg(MSG_WARNING,
1134 + "dhcp_smach_set_msg_reqhost: error decoding"
1135 + " CD_HOSTNAME value from DHCP_OPT");
1136 + } else {
1137 + dhcpmsg(MSG_DEBUG,
1138 + "dhcp_smach_set_msg_reqhost: host %s", value);
1139 + free(dsmp->dsm_msg_reqhost);
1140 + dsmp->dsm_msg_reqhost = value;
1141 + }
1142 + free(entry);
1143 + entry = NULL;
1144 + }
1145 + }
1146 + }
1147 + }
1148 +}
1149 +
1150 +/*
1151 + * dhcp_get_ack_or_state(): get a v4 option from the ACK or from the state
1152 + * machine state for certain codes that are not ACKed (e.g., CD_CLIENT_ID)
1153 + *
1154 + * input: dhcp_smach_t *: the state machine instance;
1155 + * PKT_LIST *: the decoded DHCP IPC message;
1156 + * uint_t: the DHCP client option code;
1157 + * boolean_t *: a pointer to a value that will be set to B_TRUE if
1158 + * the return value must be freed (or else set to B_FALSE);
1159 + * output: the option if found or else NULL.
1160 + */
1161 +
1162 +/* ARGSUSED */
1163 +static DHCP_OPT *
1164 +dhcp_get_ack_or_state(const dhcp_smach_t *dsmp, const PKT_LIST *plp,
1165 + uint_t codenum, boolean_t *did_alloc)
1166 +{
1167 + DHCP_OPT *opt;
1168 +
1169 + *did_alloc = B_FALSE;
1170 +
1171 + if (codenum > DHCP_LAST_OPT)
1172 + return NULL;
1173 +
1174 + /* check the ACK first for all codes */
1175 + opt = plp->opts[codenum];
1176 + if (opt != NULL)
1177 + return opt;
1178 +
1179 + /* check the machine state also for certain codes */
1180 + switch (codenum) {
1181 + case CD_CLIENT_ID:
1182 + /*
1183 + * CD_CLIENT_ID is not sent in an ACK, but it's possibly available
1184 + * from the state machine data
1185 + */
1186 +
1187 + if (dsmp->dsm_cidlen > 0) {
1188 + if ((opt = malloc(dsmp->dsm_cidlen + DHCP_OPT_META_LEN))
1189 + != NULL) {
1190 + *did_alloc = B_TRUE;
1191 + (void) encode_dhcp_opt(opt, B_FALSE /* is IPv6 */,
1192 + CD_CLIENT_ID, dsmp->dsm_cid, dsmp->dsm_cidlen);
1193 + }
1194 + }
1195 + break;
1196 + default:
1197 + break;
1198 + }
1199 + return (opt);
1200 +}
1201 +
1061 1202 /*
1062 1203 * check_rtm_addr(): determine if routing socket message matches interface
1063 1204 * address
1064 1205 *
1065 1206 * input: const struct if_msghdr *: pointer to routing socket message
1066 1207 * int: routing socket message length
1067 1208 * boolean_t: set to B_TRUE if IPv6
1068 1209 * const in6_addr_t *: pointer to IP address
1069 1210 * output: boolean_t: B_TRUE if address is a match
1070 1211 */
1071 1212
1072 1213 static boolean_t
1073 1214 check_rtm_addr(const struct ifa_msghdr *ifam, int msglen, boolean_t isv6,
1074 1215 const in6_addr_t *addr)
1075 1216 {
1076 1217 const char *cp, *lim;
1077 1218 uint_t flag;
1078 1219 const struct sockaddr *sa;
1079 1220
1080 1221 if (!(ifam->ifam_addrs & RTA_IFA))
1081 1222 return (B_FALSE);
1082 1223
1083 1224 cp = (const char *)(ifam + 1);
1084 1225 lim = (const char *)ifam + msglen;
1085 1226 for (flag = 1; flag < RTA_IFA; flag <<= 1) {
1086 1227 if (ifam->ifam_addrs & flag) {
1087 1228 /* LINTED: alignment */
1088 1229 sa = (const struct sockaddr *)cp;
1089 1230 if ((const char *)(sa + 1) > lim)
1090 1231 return (B_FALSE);
1091 1232 switch (sa->sa_family) {
1092 1233 case AF_INET:
1093 1234 cp += sizeof (struct sockaddr_in);
1094 1235 break;
1095 1236 case AF_LINK:
1096 1237 cp += sizeof (struct sockaddr_dl);
1097 1238 break;
1098 1239 case AF_INET6:
1099 1240 cp += sizeof (struct sockaddr_in6);
1100 1241 break;
1101 1242 default:
1102 1243 cp += sizeof (struct sockaddr);
1103 1244 break;
1104 1245 }
1105 1246 }
1106 1247 }
1107 1248 if (isv6) {
1108 1249 const struct sockaddr_in6 *sin6;
1109 1250
1110 1251 /* LINTED: alignment */
1111 1252 sin6 = (const struct sockaddr_in6 *)cp;
1112 1253 if ((const char *)(sin6 + 1) > lim)
1113 1254 return (B_FALSE);
1114 1255 if (sin6->sin6_family != AF_INET6)
1115 1256 return (B_FALSE);
1116 1257 return (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, addr));
1117 1258 } else {
1118 1259 const struct sockaddr_in *sinp;
1119 1260 ipaddr_t v4addr;
1120 1261
1121 1262 /* LINTED: alignment */
1122 1263 sinp = (const struct sockaddr_in *)cp;
1123 1264 if ((const char *)(sinp + 1) > lim)
1124 1265 return (B_FALSE);
1125 1266 if (sinp->sin_family != AF_INET)
1126 1267 return (B_FALSE);
1127 1268 IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
1128 1269 return (sinp->sin_addr.s_addr == v4addr);
1129 1270 }
1130 1271 }
1131 1272
1132 1273 /*
1133 1274 * is_rtm_v6(): determine if routing socket message is IPv6
1134 1275 *
1135 1276 * input: struct ifa_msghdr *: pointer to routing socket message
1136 1277 * int: message length
1137 1278 * output: boolean_t
1138 1279 */
1139 1280
1140 1281 static boolean_t
1141 1282 is_rtm_v6(const struct ifa_msghdr *ifam, int msglen)
1142 1283 {
1143 1284 const char *cp, *lim;
1144 1285 uint_t flag;
1145 1286 const struct sockaddr *sa;
1146 1287
1147 1288 cp = (const char *)(ifam + 1);
1148 1289 lim = (const char *)ifam + msglen;
1149 1290 for (flag = ifam->ifam_addrs; flag != 0; flag &= flag - 1) {
1150 1291 /* LINTED: alignment */
1151 1292 sa = (const struct sockaddr *)cp;
1152 1293 if ((const char *)(sa + 1) > lim)
1153 1294 return (B_FALSE);
1154 1295 switch (sa->sa_family) {
1155 1296 case AF_INET:
1156 1297 return (B_FALSE);
1157 1298 case AF_LINK:
1158 1299 cp += sizeof (struct sockaddr_dl);
1159 1300 break;
1160 1301 case AF_INET6:
1161 1302 return (B_TRUE);
1162 1303 default:
1163 1304 cp += sizeof (struct sockaddr);
1164 1305 break;
1165 1306 }
1166 1307 }
1167 1308 return (B_FALSE);
1168 1309 }
1169 1310
1170 1311 /*
1171 1312 * check_lif(): check the state of a given logical interface and its DHCP
1172 1313 * lease. We've been told by the routing socket that the
1173 1314 * corresponding ifIndex has changed. This may mean that DAD has
1174 1315 * completed or failed.
1175 1316 *
1176 1317 * input: dhcp_lif_t *: pointer to the LIF
1177 1318 * const struct ifa_msghdr *: routing socket message
1178 1319 * int: size of routing socket message
1179 1320 * output: boolean_t: B_TRUE if DAD has completed on this interface
1180 1321 */
1181 1322
1182 1323 static boolean_t
1183 1324 check_lif(dhcp_lif_t *lif, const struct ifa_msghdr *ifam, int msglen)
1184 1325 {
1185 1326 boolean_t isv6, dad_wait, unplumb;
1186 1327 int fd;
1187 1328 struct lifreq lifr;
1188 1329
1189 1330 isv6 = lif->lif_pif->pif_isv6;
1190 1331 fd = isv6 ? v6_sock_fd : v4_sock_fd;
1191 1332
1192 1333 /*
1193 1334 * Get the real (64 bit) logical interface flags. Note that the
1194 1335 * routing socket message has flags, but these are just the lower 32
1195 1336 * bits.
1196 1337 */
1197 1338 unplumb = B_FALSE;
1198 1339 (void) memset(&lifr, 0, sizeof (lifr));
1199 1340 (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1200 1341 if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1201 1342 /*
1202 1343 * Failing to retrieve flags means that the interface is gone.
1203 1344 * It hasn't failed to verify with DAD, but we still have to
1204 1345 * give up on it.
1205 1346 */
1206 1347 lifr.lifr_flags = 0;
1207 1348 if (errno == ENXIO) {
1208 1349 lif->lif_plumbed = B_FALSE;
1209 1350 dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1210 1351 lif->lif_name);
1211 1352 if (!isv6)
1212 1353 discard_default_routes(lif->lif_smachs);
1213 1354 } else {
1214 1355 dhcpmsg(MSG_ERR,
1215 1356 "unable to retrieve interface flags on %s",
1216 1357 lif->lif_name);
1217 1358 }
1218 1359 unplumb = B_TRUE;
1219 1360 } else if (!check_rtm_addr(ifam, msglen, isv6, &lif->lif_v6addr)) {
1220 1361 /*
1221 1362 * If the message is not about this logical interface,
1222 1363 * then just ignore it.
1223 1364 */
1224 1365 return (B_FALSE);
1225 1366 } else if (lifr.lifr_flags & IFF_DUPLICATE) {
1226 1367 dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1227 1368 lif->lif_name);
1228 1369 lif_mark_decline(lif, "duplicate address");
1229 1370 close_ip_lif(lif);
1230 1371 (void) open_ip_lif(lif, INADDR_ANY, B_TRUE);
1231 1372 }
1232 1373
1233 1374 dad_wait = lif->lif_dad_wait;
1234 1375 if (dad_wait) {
1235 1376 dhcpmsg(MSG_VERBOSE, "check_lif: %s has finished DAD",
1236 1377 lif->lif_name);
1237 1378 lif->lif_dad_wait = B_FALSE;
1238 1379 }
1239 1380
1240 1381 if (unplumb)
1241 1382 unplumb_lif(lif);
1242 1383
1243 1384 return (dad_wait);
1244 1385 }
1245 1386
1246 1387 /*
1247 1388 * check_main_lif(): check the state of a main logical interface for a state
1248 1389 * machine. This is used only for DHCPv6.
1249 1390 *
1250 1391 * input: dhcp_smach_t *: pointer to the state machine
1251 1392 * const struct ifa_msghdr *: routing socket message
1252 1393 * int: size of routing socket message
1253 1394 * output: boolean_t: B_TRUE if LIF is ok.
1254 1395 */
1255 1396
1256 1397 static boolean_t
1257 1398 check_main_lif(dhcp_smach_t *dsmp, const struct ifa_msghdr *ifam, int msglen)
1258 1399 {
1259 1400 dhcp_lif_t *lif = dsmp->dsm_lif;
1260 1401 struct lifreq lifr;
1261 1402
1262 1403 /*
1263 1404 * Get the real (64 bit) logical interface flags. Note that the
1264 1405 * routing socket message has flags, but these are just the lower 32
1265 1406 * bits.
1266 1407 */
1267 1408 (void) memset(&lifr, 0, sizeof (lifr));
1268 1409 (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1269 1410 if (ioctl(v6_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) {
1270 1411 /*
1271 1412 * Failing to retrieve flags means that the interface is gone.
1272 1413 * Our state machine is now trash.
1273 1414 */
1274 1415 if (errno == ENXIO) {
1275 1416 dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1276 1417 lif->lif_name);
1277 1418 } else {
1278 1419 dhcpmsg(MSG_ERR,
1279 1420 "unable to retrieve interface flags on %s",
1280 1421 lif->lif_name);
1281 1422 }
1282 1423 return (B_FALSE);
1283 1424 } else if (!check_rtm_addr(ifam, msglen, B_TRUE, &lif->lif_v6addr)) {
1284 1425 /*
1285 1426 * If the message is not about this logical interface,
1286 1427 * then just ignore it.
1287 1428 */
1288 1429 return (B_TRUE);
1289 1430 } else if (lifr.lifr_flags & IFF_DUPLICATE) {
1290 1431 dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1291 1432 lif->lif_name);
1292 1433 return (B_FALSE);
1293 1434 } else {
1294 1435 return (B_TRUE);
1295 1436 }
1296 1437 }
1297 1438
1298 1439 /*
1299 1440 * process_link_up_down(): check the state of a physical interface for up/down
1300 1441 * transitions; must go through INIT_REBOOT state if
1301 1442 * the link flaps.
1302 1443 *
1303 1444 * input: dhcp_pif_t *: pointer to the physical interface to check
1304 1445 * const struct if_msghdr *: routing socket message
1305 1446 * output: none
1306 1447 */
1307 1448
1308 1449 static void
1309 1450 process_link_up_down(dhcp_pif_t *pif, const struct if_msghdr *ifm)
1310 1451 {
1311 1452 struct lifreq lifr;
1312 1453 boolean_t isv6;
1313 1454 int fd;
1314 1455
1315 1456 /*
1316 1457 * If the message implies no change of flags, then we're done; no need
1317 1458 * to check further. Note that if we have multiple state machines on a
1318 1459 * single physical interface, this test keeps us from issuing an ioctl
1319 1460 * for each one.
1320 1461 */
1321 1462 if ((ifm->ifm_flags & IFF_RUNNING) && pif->pif_running ||
1322 1463 !(ifm->ifm_flags & IFF_RUNNING) && !pif->pif_running)
1323 1464 return;
1324 1465
1325 1466 /*
1326 1467 * We don't know what the real interface flags are, because the
1327 1468 * if_index number is only 16 bits; we must go ask.
1328 1469 */
1329 1470 isv6 = pif->pif_isv6;
1330 1471 fd = isv6 ? v6_sock_fd : v4_sock_fd;
1331 1472 (void) memset(&lifr, 0, sizeof (lifr));
1332 1473 (void) strlcpy(lifr.lifr_name, pif->pif_name, sizeof (lifr.lifr_name));
1333 1474
1334 1475 if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1 ||
1335 1476 !(lifr.lifr_flags & IFF_RUNNING)) {
1336 1477 /*
1337 1478 * If we've lost the interface or it has gone down, then
1338 1479 * nothing special to do; just turn off the running flag.
1339 1480 */
1340 1481 pif_status(pif, B_FALSE);
1341 1482 } else {
1342 1483 /*
1343 1484 * Interface has come back up: go through verification process.
1344 1485 */
1345 1486 pif_status(pif, B_TRUE);
1346 1487 }
1347 1488 }
1348 1489
1349 1490 /*
1350 1491 * rtsock_event(): fetches routing socket messages and updates internal
1351 1492 * interface state based on those messages.
1352 1493 *
1353 1494 * input: iu_eh_t *: unused
1354 1495 * int: the routing socket file descriptor
1355 1496 * (other arguments unused)
1356 1497 * output: void
1357 1498 */
1358 1499
1359 1500 /* ARGSUSED */
1360 1501 static void
1361 1502 rtsock_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
1362 1503 {
1363 1504 dhcp_smach_t *dsmp, *dsmnext;
1364 1505 union {
1365 1506 struct ifa_msghdr ifam;
1366 1507 struct if_msghdr ifm;
1367 1508 char buf[1024];
1368 1509 } msg;
1369 1510 uint16_t ifindex;
1370 1511 int msglen;
1371 1512 boolean_t isv6;
1372 1513
1373 1514 if ((msglen = read(fd, &msg, sizeof (msg))) <= 0)
1374 1515 return;
1375 1516
1376 1517 /* Note that the routing socket interface index is just 16 bits */
1377 1518 if (msg.ifm.ifm_type == RTM_IFINFO) {
1378 1519 ifindex = msg.ifm.ifm_index;
1379 1520 isv6 = (msg.ifm.ifm_flags & IFF_IPV6) ? B_TRUE : B_FALSE;
1380 1521 } else if (msg.ifam.ifam_type == RTM_DELADDR ||
1381 1522 msg.ifam.ifam_type == RTM_NEWADDR) {
1382 1523 ifindex = msg.ifam.ifam_index;
1383 1524 isv6 = is_rtm_v6(&msg.ifam, msglen);
1384 1525 } else {
1385 1526 return;
1386 1527 }
1387 1528
1388 1529 for (dsmp = lookup_smach_by_uindex(ifindex, NULL, isv6);
1389 1530 dsmp != NULL; dsmp = dsmnext) {
1390 1531 DHCPSTATE oldstate;
1391 1532 boolean_t lif_finished;
1392 1533 boolean_t lease_removed;
1393 1534 dhcp_lease_t *dlp, *dlnext;
1394 1535
1395 1536 /*
1396 1537 * Note that script_start can call dhcp_drop directly, and
1397 1538 * that will do release_smach.
1398 1539 */
1399 1540 dsmnext = lookup_smach_by_uindex(ifindex, dsmp, isv6);
1400 1541 oldstate = dsmp->dsm_state;
1401 1542
1402 1543 /*
1403 1544 * Ignore state machines that are currently processing drop or
1404 1545 * release; there is nothing more we can do for them.
1405 1546 */
1406 1547 if (dsmp->dsm_droprelease)
1407 1548 continue;
1408 1549
1409 1550 /*
1410 1551 * Look for link up/down notifications. These occur on a
1411 1552 * physical interface basis.
1412 1553 */
1413 1554 if (msg.ifm.ifm_type == RTM_IFINFO) {
1414 1555 process_link_up_down(dsmp->dsm_lif->lif_pif, &msg.ifm);
1415 1556 continue;
1416 1557 }
1417 1558
1418 1559 /*
1419 1560 * Since we cannot trust the flags reported by the routing
1420 1561 * socket (they're just 32 bits -- and thus never include
1421 1562 * IFF_DUPLICATE), and we can't trust the ifindex (it's only 16
1422 1563 * bits and also doesn't reflect the alias in use), we get
1423 1564 * flags on all matching interfaces, and go by that.
1424 1565 */
1425 1566 lif_finished = B_FALSE;
1426 1567 lease_removed = B_FALSE;
1427 1568 for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlnext) {
1428 1569 dhcp_lif_t *lif, *lifnext;
1429 1570 uint_t nlifs = dlp->dl_nlifs;
1430 1571
1431 1572 dlnext = dlp->dl_next;
1432 1573 for (lif = dlp->dl_lifs; lif != NULL && nlifs > 0;
1433 1574 lif = lifnext, nlifs--) {
1434 1575 lifnext = lif->lif_next;
1435 1576 if (check_lif(lif, &msg.ifam, msglen)) {
1436 1577 dsmp->dsm_lif_wait--;
1437 1578 lif_finished = B_TRUE;
1438 1579 }
1439 1580 }
1440 1581 if (dlp->dl_nlifs == 0) {
1441 1582 remove_lease(dlp);
1442 1583 lease_removed = B_TRUE;
1443 1584 }
1444 1585 }
1445 1586
1446 1587 if ((isv6 && !check_main_lif(dsmp, &msg.ifam, msglen)) ||
1447 1588 (!isv6 && !verify_lif(dsmp->dsm_lif))) {
1448 1589 finished_smach(dsmp, DHCP_IPC_E_INVIF);
1449 1590 continue;
1450 1591 }
1451 1592
1452 1593 /*
1453 1594 * Ignore this state machine if nothing interesting has
1454 1595 * happened.
1455 1596 */
1456 1597 if (!lif_finished && dsmp->dsm_lif_down == 0 &&
1457 1598 (dsmp->dsm_leases != NULL || !lease_removed))
1458 1599 continue;
1459 1600
1460 1601 /*
1461 1602 * If we're still waiting for DAD to complete on some of the
1462 1603 * configured LIFs, then don't send a response.
1463 1604 */
1464 1605 if (dsmp->dsm_lif_wait != 0) {
1465 1606 dhcpmsg(MSG_VERBOSE, "rtsock_event: %s still has %d "
1466 1607 "LIFs waiting on DAD", dsmp->dsm_name,
1467 1608 dsmp->dsm_lif_wait);
1468 1609 continue;
1469 1610 }
1470 1611
1471 1612 /*
1472 1613 * If we have some failed LIFs, then handle them now. We'll
1473 1614 * remove them from the list. Any leases that become empty are
1474 1615 * also removed as part of the decline-generation process.
1475 1616 */
1476 1617 if (dsmp->dsm_lif_down != 0)
1477 1618 send_declines(dsmp);
1478 1619
1479 1620 if (dsmp->dsm_leases == NULL) {
1480 1621 dsmp->dsm_bad_offers++;
1481 1622 /*
1482 1623 * For DHCPv6, we'll process the restart once we're
1483 1624 * done sending Decline messages, because these are
1484 1625 * supposed to be acknowledged. With DHCPv4, there's
1485 1626 * no acknowledgment for a DECLINE, so after sending
1486 1627 * it, we just restart right away.
1487 1628 */
1488 1629 if (!dsmp->dsm_isv6) {
1489 1630 dhcpmsg(MSG_VERBOSE, "rtsock_event: %s has no "
1490 1631 "LIFs left", dsmp->dsm_name);
1491 1632 dhcp_restart(dsmp);
1492 1633 }
1493 1634 } else {
1494 1635 /*
1495 1636 * If we're now up on at least some of the leases and
1496 1637 * we were waiting for that, then kick off the rest of
1497 1638 * configuration. Lease validation and DAD are done.
1498 1639 */
1499 1640 dhcpmsg(MSG_VERBOSE, "rtsock_event: all LIFs verified "
1500 1641 "on %s in %s state", dsmp->dsm_name,
1501 1642 dhcp_state_to_string(oldstate));
1502 1643 if (oldstate == PRE_BOUND ||
1503 1644 oldstate == ADOPTING)
1504 1645 dhcp_bound_complete(dsmp);
1505 1646 if (oldstate == ADOPTING)
1506 1647 dhcp_adopt_complete(dsmp);
1507 1648 }
1508 1649 }
1509 1650 }
1510 1651
1511 1652 /*
1512 1653 * check_cmd_allowed(): check whether the requested command is allowed in the
1513 1654 * state specified.
1514 1655 *
1515 1656 * input: DHCPSTATE: current state
1516 1657 * dhcp_ipc_type_t: requested command
1517 1658 * output: boolean_t: B_TRUE if command is allowed in this state
1518 1659 */
1519 1660
1520 1661 boolean_t
1521 1662 check_cmd_allowed(DHCPSTATE state, dhcp_ipc_type_t cmd)
1522 1663 {
1523 1664 return (ipc_cmd_allowed[state][cmd] != 0);
1524 1665 }
1525 1666
1526 1667 static boolean_t
1527 1668 is_iscsi_active(void)
1528 1669 {
1529 1670 int fd;
1530 1671 int active = 0;
1531 1672
1532 1673 if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) != -1) {
1533 1674 if (ioctl(fd, ISCSI_IS_ACTIVE, &active) != 0)
1534 1675 active = 0;
1535 1676 (void) close(fd);
1536 1677 }
1537 1678
1538 1679 return (active != 0);
1539 1680 }
|
↓ open down ↓ |
469 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX