xref: /titanic_44/usr/src/cmd/cmd-inet/sbin/dhcpagent/inform.c (revision 75ce41a57ff334bd8fe2cb9ed51eea835892f944)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * INFORM_SENT state of the client state machine.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <time.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/udp.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/udp_var.h>
37 #include <dhcpmsg.h>
38 
39 #include "agent.h"
40 #include "states.h"
41 #include "interface.h"
42 #include "packet.h"
43 
44 static boolean_t stop_informing(dhcp_smach_t *, unsigned int);
45 
46 /*
47  * dhcp_inform(): sends an INFORM packet and sets up reception for an ACK
48  *
49  *   input: dhcp_smach_t *: the state machine to use
50  *  output: void
51  *    note: the INFORM cannot be sent successfully if the interface
52  *	    does not have an IP address (this is mostly an issue for IPv4).
53  *	    We switch into INFORM_SENT state before sending the packet so
54  *	    that the packet-sending subsystem uses regular sockets and sets
55  *	    the source address.  (See set_smach_state.)
56  */
57 
58 void
59 dhcp_inform(dhcp_smach_t *dsmp)
60 {
61 	dhcp_pkt_t		*dpkt;
62 
63 	if (!set_smach_state(dsmp, INFORM_SENT))
64 		goto failed;
65 
66 	if (dsmp->dsm_isv6) {
67 		dpkt = init_pkt(dsmp, DHCPV6_MSG_INFO_REQ);
68 
69 		/* Add required Option Request option */
70 		(void) add_pkt_prl(dpkt, dsmp);
71 		dsmp->dsm_server = ipv6_all_dhcp_relay_and_servers;
72 		(void) send_pkt_v6(dsmp, dpkt, dsmp->dsm_server,
73 		    stop_informing, DHCPV6_INF_TIMEOUT, DHCPV6_INF_MAX_RT);
74 	} else {
75 		ipaddr_t server;
76 
77 		/*
78 		 * Assemble a DHCPREQUEST packet, without the Server ID option.
79 		 * Fill in ciaddr, since we know this.  dsm_server will be set
80 		 * to the server's IP address, which will be the broadcast
81 		 * address if we don't know it.  The max DHCP message size
82 		 * option is set to the interface max, minus the size of the
83 		 * UDP and IP headers.
84 		 */
85 
86 		dpkt = init_pkt(dsmp, INFORM);
87 		IN6_V4MAPPED_TO_INADDR(&dsmp->dsm_lif->lif_v6addr,
88 		    &dpkt->pkt->ciaddr);
89 
90 		(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
91 		    htons(dsmp->dsm_lif->lif_pif->pif_max -
92 		    sizeof (struct udpiphdr)));
93 		if (class_id_len != 0) {
94 			(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id,
95 			    class_id_len);
96 		}
97 		(void) add_pkt_prl(dpkt, dsmp);
98 		(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
99 
100 		IN6_V4MAPPED_TO_IPADDR(&dsmp->dsm_server, server);
101 		if (!send_pkt(dsmp, dpkt, server, NULL)) {
102 			dhcpmsg(MSG_ERROR, "dhcp_inform: send_pkt failed");
103 			goto failed;
104 		}
105 	}
106 
107 	return;
108 
109 failed:
110 	dsmp->dsm_dflags |= DHCP_IF_FAILED;
111 	ipc_action_finish(dsmp, DHCP_IPC_E_INT);
112 	(void) set_smach_state(dsmp, INIT);
113 }
114 
115 /*
116  * stop_informing(): decides when to stop retransmitting Information-Requests
117  *
118  *   input: dhcp_smach_t *: the state machine Info-Reqs are being sent from
119  *	    unsigned int: the number of requests sent so far
120  *  output: boolean_t: B_TRUE if retransmissions should stop
121  */
122 
123 /* ARGSUSED */
124 static boolean_t
125 stop_informing(dhcp_smach_t *dsmp, unsigned int n_requests)
126 {
127 	return (B_FALSE);
128 }
129