xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/inform.c (revision d0698e0d179f97729cacdbc2f13446a6b0a3f22a)
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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  *
24  * INFORM_SENT state of the client state machine.
25  */
26 
27 #include <sys/types.h>
28 #include <time.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/udp.h>
32 #include <netinet/ip_var.h>
33 #include <netinet/udp_var.h>
34 #include <dhcpmsg.h>
35 
36 #include "agent.h"
37 #include "states.h"
38 #include "interface.h"
39 #include "packet.h"
40 
41 static boolean_t stop_informing(dhcp_smach_t *, unsigned int);
42 
43 /*
44  * dhcp_inform(): sends an INFORM packet and sets up reception for an ACK
45  *
46  *   input: dhcp_smach_t *: the state machine to use
47  *  output: void
48  *    note: the INFORM cannot be sent successfully if the interface
49  *	    does not have an IP address (this is mostly an issue for IPv4).
50  *	    We switch into INFORM_SENT state before sending the packet so
51  *	    that the packet-sending subsystem uses regular sockets and sets
52  *	    the source address.  (See set_smach_state.)
53  */
54 
55 void
56 dhcp_inform(dhcp_smach_t *dsmp)
57 {
58 	dhcp_pkt_t		*dpkt;
59 
60 	if (!set_smach_state(dsmp, INFORM_SENT))
61 		goto failed;
62 
63 	if (dsmp->dsm_isv6) {
64 		dpkt = init_pkt(dsmp, DHCPV6_MSG_INFO_REQ);
65 
66 		/* Add required Option Request option */
67 		(void) add_pkt_prl(dpkt, dsmp);
68 		dsmp->dsm_server = ipv6_all_dhcp_relay_and_servers;
69 		(void) send_pkt_v6(dsmp, dpkt, dsmp->dsm_server,
70 		    stop_informing, DHCPV6_INF_TIMEOUT, DHCPV6_INF_MAX_RT);
71 	} else {
72 		ipaddr_t server;
73 
74 		/*
75 		 * Assemble a DHCPREQUEST packet, without the Server ID option.
76 		 * Fill in ciaddr, since we know this.  dsm_server will be set
77 		 * to the server's IP address, which will be the broadcast
78 		 * address if we don't know it.  The max DHCP message size
79 		 * option is set to the interface max, minus the size of the
80 		 * UDP and IP headers.
81 		 */
82 
83 		dpkt = init_pkt(dsmp, INFORM);
84 		IN6_V4MAPPED_TO_INADDR(&dsmp->dsm_lif->lif_v6addr,
85 		    &dpkt->pkt->ciaddr);
86 
87 		(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
88 		    htons(dsmp->dsm_lif->lif_pif->pif_max -
89 		    sizeof (struct udpiphdr)));
90 		if (class_id_len != 0) {
91 			(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id,
92 			    class_id_len);
93 		}
94 		(void) add_pkt_prl(dpkt, dsmp);
95 		(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
96 
97 		IN6_V4MAPPED_TO_IPADDR(&dsmp->dsm_server, server);
98 		if (!send_pkt(dsmp, dpkt, server, stop_informing)) {
99 			dhcpmsg(MSG_ERROR, "dhcp_inform: send_pkt failed");
100 			goto failed;
101 		}
102 	}
103 
104 	return;
105 
106 failed:
107 	dsmp->dsm_dflags |= DHCP_IF_FAILED;
108 	ipc_action_finish(dsmp, DHCP_IPC_E_INT);
109 	(void) set_smach_state(dsmp, INIT);
110 }
111 
112 /*
113  * stop_informing(): decides when to stop retransmitting Information-Requests
114  *
115  *   input: dhcp_smach_t *: the state machine Info-Reqs are being sent from
116  *	    unsigned int: the number of requests sent so far
117  *  output: boolean_t: B_TRUE if retransmissions should stop
118  */
119 
120 /* ARGSUSED */
121 static boolean_t
122 stop_informing(dhcp_smach_t *dsmp, unsigned int n_requests)
123 {
124 	return (B_FALSE);
125 }
126