xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/async.c (revision 2159:1a6fcb0ade72)
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
52157Sdh155122  * Common Development and Distribution License (the "License").
62157Sdh155122  * 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 /*
222157Sdh155122  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <dhcpmsg.h>
310Sstevel@tonic-gate #include <libinetutil.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "async.h"
340Sstevel@tonic-gate #include "util.h"
350Sstevel@tonic-gate #include "agent.h"
360Sstevel@tonic-gate #include "interface.h"
370Sstevel@tonic-gate #include "script_handler.h"
38*2159Sdh155122 #include "states.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static void	async_timeout(iu_tq_t *, void *);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * async_pending(): checks to see if an async command is pending.  if a stale
440Sstevel@tonic-gate  *		    async command is found, cancellation is attempted.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *   input: struct ifslist *: the interface to check for an async command on
470Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if async command is pending, B_FALSE if not
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate boolean_t
510Sstevel@tonic-gate async_pending(struct ifslist *ifsp)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	if (!(ifsp->if_dflags & DHCP_IF_BUSY))
540Sstevel@tonic-gate 		return (B_FALSE);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	/*
570Sstevel@tonic-gate 	 * if the command was not started by the user (i.e., was
580Sstevel@tonic-gate 	 * started internal to the agent), then it will timeout in
590Sstevel@tonic-gate 	 * async_timeout() -- don't shoot it here.
600Sstevel@tonic-gate 	 */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (!ifsp->if_async.as_user)
630Sstevel@tonic-gate 		return (B_TRUE);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (ifsp->if_script_pid != -1)
660Sstevel@tonic-gate 		return (B_TRUE);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * user command -- see if they went away.  if they went away,
700Sstevel@tonic-gate 	 * either a timeout was already sent to them or they
710Sstevel@tonic-gate 	 * control-c'd out.
720Sstevel@tonic-gate 	 */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (ipc_action_pending(ifsp))
750Sstevel@tonic-gate 		return (B_TRUE);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * it appears they went away.  try to cancel their pending
790Sstevel@tonic-gate 	 * command.  if we can't cancel it, we leave their command
800Sstevel@tonic-gate 	 * pending and it's just gonna have to complete its business
810Sstevel@tonic-gate 	 * in any case, cancel the ipc_action timer, since we know
820Sstevel@tonic-gate 	 * they've gone away.
830Sstevel@tonic-gate 	 */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	dhcpmsg(MSG_DEBUG, "async_pending: async command left, attempting "
860Sstevel@tonic-gate 	    "cancellation");
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	ipc_action_cancel_timer(ifsp);
890Sstevel@tonic-gate 	return (async_cancel(ifsp) ? B_FALSE : B_TRUE);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * async_start(): starts an asynchronous command on an interface
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  *   input: struct ifslist *: the interface to start the async command on
960Sstevel@tonic-gate  *	    dhcp_ipc_type_t: the command to start
970Sstevel@tonic-gate  *	    boolean_t: B_TRUE if the command was started by a user
980Sstevel@tonic-gate  *  output: int: 1 on success, 0 on failure
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate int
1020Sstevel@tonic-gate async_start(struct ifslist *ifsp, dhcp_ipc_type_t cmd, boolean_t user)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	iu_timer_id_t tid;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (async_pending(ifsp))
1070Sstevel@tonic-gate 		return (0);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	tid = iu_schedule_timer(tq, DHCP_ASYNC_WAIT, async_timeout, ifsp);
1100Sstevel@tonic-gate 	if (tid == -1)
1110Sstevel@tonic-gate 		return (0);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	hold_ifs(ifsp);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	ifsp->if_async.as_tid	 = tid;
1160Sstevel@tonic-gate 	ifsp->if_async.as_cmd	 = cmd;
1170Sstevel@tonic-gate 	ifsp->if_async.as_user	 = user;
1180Sstevel@tonic-gate 	ifsp->if_dflags		|= DHCP_IF_BUSY;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (1);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * async_finish(): completes an asynchronous command
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  *   input: struct ifslist *: the interface with the pending async command
1280Sstevel@tonic-gate  *  output: void
1290Sstevel@tonic-gate  *    note: should only be used when the command has no residual state to
1300Sstevel@tonic-gate  *	    clean up
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate void
1340Sstevel@tonic-gate async_finish(struct ifslist *ifsp)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * be defensive here. the script may still be running if
1380Sstevel@tonic-gate 	 * the asynchronous action times out before it is killed by the
1390Sstevel@tonic-gate 	 * script helper process.
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (ifsp->if_script_pid != -1)
1430Sstevel@tonic-gate 		script_stop(ifsp);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * in case async_timeout() has already called async_cancel(),
1470Sstevel@tonic-gate 	 * and to be idempotent, check the DHCP_IF_BUSY flag
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (!(ifsp->if_dflags & DHCP_IF_BUSY))
1510Sstevel@tonic-gate 		return;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (ifsp->if_async.as_tid == -1) {
1540Sstevel@tonic-gate 		ifsp->if_dflags &= ~DHCP_IF_BUSY;
1550Sstevel@tonic-gate 		return;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (iu_cancel_timer(tq, ifsp->if_async.as_tid, NULL) == 1) {
1590Sstevel@tonic-gate 		ifsp->if_dflags &= ~DHCP_IF_BUSY;
1600Sstevel@tonic-gate 		ifsp->if_async.as_tid = -1;
1610Sstevel@tonic-gate 		(void) release_ifs(ifsp);
1620Sstevel@tonic-gate 		return;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * if we can't cancel this timer, we'll just leave the
1670Sstevel@tonic-gate 	 * interface busy and when the timeout finally fires, we'll
1680Sstevel@tonic-gate 	 * mark it free, which will just cause a minor nuisance.
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	dhcpmsg(MSG_WARNING, "async_finish: cannot cancel async timer");
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * async_cancel(): cancels a pending asynchronous command
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *   input: struct ifslist *: the interface with the pending async command
1780Sstevel@tonic-gate  *  output: int: 1 if cancellation was successful, 0 on failure
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate int
1820Sstevel@tonic-gate async_cancel(struct ifslist *ifsp)
1830Sstevel@tonic-gate {
1842157Sdh155122 	boolean_t do_restart = B_FALSE;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * we decide how to cancel the command depending on our
1880Sstevel@tonic-gate 	 * current state, since commands such as EXTEND may in fact
1890Sstevel@tonic-gate 	 * cause us to enter back into SELECTING (if a NAK results
1900Sstevel@tonic-gate 	 * from the EXTEND).
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	switch (ifsp->if_state) {
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	case BOUND:
1960Sstevel@tonic-gate 	case INFORMATION:
1970Sstevel@tonic-gate 		break;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	case RENEWING:					/* FALLTHRU */
2000Sstevel@tonic-gate 	case REBINDING:					/* FALLTHRU */
2010Sstevel@tonic-gate 	case INFORM_SENT:
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		/*
2040Sstevel@tonic-gate 		 * these states imply that we've sent a packet and we're
2050Sstevel@tonic-gate 		 * awaiting an ACK or NAK.  just cancel the wait.
2060Sstevel@tonic-gate 		 */
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		if (unregister_acknak(ifsp) == 0)
2090Sstevel@tonic-gate 			return (0);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		break;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	case INIT:					/* FALLTHRU */
2140Sstevel@tonic-gate 	case SELECTING:					/* FALLTHRU */
2150Sstevel@tonic-gate 	case REQUESTING:				/* FALLTHRU */
2160Sstevel@tonic-gate 	case INIT_REBOOT:
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		/*
2190Sstevel@tonic-gate 		 * these states imply we're still trying to get a lease.
2202157Sdh155122 		 * jump to SELECTING and start from there -- but not until
2210Sstevel@tonic-gate 		 * after we've finished the asynchronous command!
2220Sstevel@tonic-gate 		 */
2230Sstevel@tonic-gate 
2242157Sdh155122 		do_restart = B_TRUE;
2250Sstevel@tonic-gate 		break;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	default:
2280Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "async_cancel: cancellation in unexpected "
2290Sstevel@tonic-gate 		    "state %d", ifsp->if_state);
2300Sstevel@tonic-gate 		return (0);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	async_finish(ifsp);
2340Sstevel@tonic-gate 	dhcpmsg(MSG_DEBUG, "async_cancel: asynchronous command (%d) aborted",
2350Sstevel@tonic-gate 	    ifsp->if_async.as_cmd);
2362157Sdh155122 	if (do_restart)
2372157Sdh155122 		dhcp_selecting(ifsp);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	return (1);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * async_timeout(): expires stale asynchronous commands
2440Sstevel@tonic-gate  *
2450Sstevel@tonic-gate  *   input: iu_tq_t *: the timer queue on which the timeout went off
2460Sstevel@tonic-gate  *	    void *: the interface with the pending async command
2470Sstevel@tonic-gate  *  output: void
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static void
2510Sstevel@tonic-gate async_timeout(iu_tq_t *tq, void *arg)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	struct ifslist		*ifsp = (struct ifslist *)arg;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (check_ifs(ifsp) == 0) {
2560Sstevel@tonic-gate 		(void) release_ifs(ifsp);
2570Sstevel@tonic-gate 		return;
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/* we've expired now */
2610Sstevel@tonic-gate 	ifsp->if_async.as_tid = -1;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * if the command was generated internally to the agent, try
2650Sstevel@tonic-gate 	 * to cancel it immediately.  otherwise, if the user has gone
2660Sstevel@tonic-gate 	 * away, we cancel it in async_pending().  otherwise, we let
2670Sstevel@tonic-gate 	 * it live.
2680Sstevel@tonic-gate 	 */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (!ifsp->if_async.as_user) {
2710Sstevel@tonic-gate 		(void) async_cancel(ifsp);
2720Sstevel@tonic-gate 		return;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (async_pending(ifsp)) {
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		ifsp->if_async.as_tid = iu_schedule_timer(tq, DHCP_ASYNC_WAIT,
2780Sstevel@tonic-gate 		    async_timeout, ifsp);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		if (ifsp->if_async.as_tid != -1) {
2810Sstevel@tonic-gate 			hold_ifs(ifsp);
2820Sstevel@tonic-gate 			dhcpmsg(MSG_DEBUG, "async_timeout: asynchronous "
2830Sstevel@tonic-gate 			    "command %d still pending", ifsp->if_async.as_cmd);
2840Sstevel@tonic-gate 			return;
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		/*
2880Sstevel@tonic-gate 		 * what can we do but cancel it?  we can't get called
2890Sstevel@tonic-gate 		 * back again and otherwise we'll end up in the
2900Sstevel@tonic-gate 		 * twilight zone with the interface permanently busy
2910Sstevel@tonic-gate 		 */
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		ipc_action_finish(ifsp, DHCP_IPC_E_INT);
2940Sstevel@tonic-gate 		(void) async_cancel(ifsp);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate }
297