1 |
786 |
skrzyp |
#ifndef CYGONCE_NET_TCPIP_DHCP_H
|
2 |
|
|
#define CYGONCE_NET_TCPIP_DHCP_H
|
3 |
|
|
|
4 |
|
|
//==========================================================================
|
5 |
|
|
//
|
6 |
|
|
// include/dhcp.h
|
7 |
|
|
//
|
8 |
|
|
// DHCP protocol support
|
9 |
|
|
//
|
10 |
|
|
//==========================================================================
|
11 |
|
|
// ####BSDALTCOPYRIGHTBEGIN####
|
12 |
|
|
// -------------------------------------------
|
13 |
|
|
// Portions of this software may have been derived from FreeBSD, OpenBSD,
|
14 |
|
|
// or other sources, and if so are covered by the appropriate copyright
|
15 |
|
|
// and license included herein.
|
16 |
|
|
// -------------------------------------------
|
17 |
|
|
// ####BSDALTCOPYRIGHTEND####
|
18 |
|
|
//==========================================================================
|
19 |
|
|
//#####DESCRIPTIONBEGIN####
|
20 |
|
|
//
|
21 |
|
|
// Author(s): hmt
|
22 |
|
|
// Contributors: gthomas
|
23 |
|
|
// Date: 2000-07-01
|
24 |
|
|
// Purpose: Support DHCP initialization in eCos TCPIP stack
|
25 |
|
|
// Description:
|
26 |
|
|
//
|
27 |
|
|
//
|
28 |
|
|
//####DESCRIPTIONEND####
|
29 |
|
|
//
|
30 |
|
|
//==========================================================================
|
31 |
|
|
|
32 |
|
|
//
|
33 |
|
|
// DHCP. RFC2131, RFC1533, RFC1534
|
34 |
|
|
// See also bootp.h
|
35 |
|
|
//
|
36 |
|
|
|
37 |
|
|
#include <pkgconf/system.h>
|
38 |
|
|
#include <pkgconf/net.h>
|
39 |
|
|
|
40 |
|
|
#ifdef CYGPKG_NET_DHCP
|
41 |
|
|
|
42 |
|
|
#include <machine/types.h>
|
43 |
|
|
|
44 |
|
|
#include <cyg/kernel/kapi.h>
|
45 |
|
|
|
46 |
|
|
#include <bootp.h>
|
47 |
|
|
|
48 |
|
|
// DHCP messages; these are sent in the tag TAG_DHCP_MESS_TYPE already
|
49 |
|
|
// defined in bootp.h
|
50 |
|
|
|
51 |
|
|
#define DHCPDISCOVER 1
|
52 |
|
|
#define DHCPOFFER 2
|
53 |
|
|
#define DHCPREQUEST 3
|
54 |
|
|
#define DHCPDECLINE 4
|
55 |
|
|
#define DHCPACK 5
|
56 |
|
|
#define DHCPNAK 6
|
57 |
|
|
#define DHCPRELEASE 7
|
58 |
|
|
|
59 |
|
|
// DHCP interface state machine states; these are published so that app
|
60 |
|
|
// code can know what to do... (see page 35 of RFC2131)
|
61 |
|
|
|
62 |
|
|
// These we will use in the normal course of events
|
63 |
|
|
#define DHCPSTATE_INIT 1
|
64 |
|
|
#define DHCPSTATE_SELECTING 2 // wait for replies to b/c DISCOVER
|
65 |
|
|
#define DHCPSTATE_REQUESTING 3
|
66 |
|
|
#define DHCPSTATE_REQUEST_RECV 4 // wait for replies to b/c REQUEST
|
67 |
|
|
#define DHCPSTATE_BOUND 5
|
68 |
|
|
#define DHCPSTATE_RENEWING 6 // wait for replies to u/c REQUEST
|
69 |
|
|
#define DHCPSTATE_RENEW_RECV 7
|
70 |
|
|
#define DHCPSTATE_REBINDING 8 // wait for replies to b/c REQUEST
|
71 |
|
|
#define DHCPSTATE_REBIND_RECV 9
|
72 |
|
|
#define DHCPSTATE_BOOTP_FALLBACK 10 // fall back to plain bootp
|
73 |
|
|
#define DHCPSTATE_NOTBOUND 11 // To let app tidy up
|
74 |
|
|
#define DHCPSTATE_FAILED 12 // Net is down
|
75 |
|
|
#define DHCPSTATE_DO_RELEASE 13 // Force release of the current lease
|
76 |
|
|
// These we don't use
|
77 |
|
|
//#define DHCPSTATE_INITREBOOT
|
78 |
|
|
//#define DHCPSTATE_REBOOTING
|
79 |
|
|
|
80 |
|
|
// These are to let the app inspect the state of the interfaces when
|
81 |
|
|
// managing them itself, by analogy with eth0_up &c; eth0_bootp_data and so
|
82 |
|
|
// on will still be used with DHCP.
|
83 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
84 |
|
|
extern cyg_uint8 eth0_dhcpstate;
|
85 |
|
|
#endif
|
86 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
87 |
|
|
extern cyg_uint8 eth1_dhcpstate;
|
88 |
|
|
#endif
|
89 |
|
|
|
90 |
|
|
// This is public so the app can wait on it or poll it when managing DHCP
|
91 |
|
|
// itself. It will be zero while the app should wait, and posted when a
|
92 |
|
|
// call to do_dhcp() is needed. If, instead, the app wishes to manage DHCP
|
93 |
|
|
// with a thread per interface somehow, then separate semaphores may be used.
|
94 |
|
|
// See the dhcp_lease structure definition below.
|
95 |
|
|
extern cyg_sem_t dhcp_needs_attention;
|
96 |
|
|
|
97 |
|
|
// This routine is used at startup time, and after relinquishing leases or
|
98 |
|
|
// after a lease timeout: it does DHCP or bootp or static setup according
|
99 |
|
|
// to configuration.
|
100 |
|
|
extern void init_all_network_interfaces(void);
|
101 |
|
|
|
102 |
|
|
// This routine does the work of renewing leases &c.
|
103 |
|
|
// return value: 1 => everything OK, no change.
|
104 |
|
|
// 0 => close your connections, then call do_dhcp_halt() to halt the
|
105 |
|
|
// interface(s) in question (it knows because the state will be NOTBOUND).
|
106 |
|
|
// After that you can return to the start and use
|
107 |
|
|
// init_all_network_interfaces() as usual.
|
108 |
|
|
extern int dhcp_bind( void );
|
109 |
|
|
|
110 |
|
|
// Shutdown any interface which is not already shut down - whether
|
111 |
|
|
// initialized by DHCP or not. Reason: because of the broadcast-while-not-
|
112 |
|
|
// -fully-initialized nature of the DHCP conversation, all other interfaces
|
113 |
|
|
// must be shut down during that. So one down, all down, is required.
|
114 |
|
|
extern int dhcp_halt( void );
|
115 |
|
|
|
116 |
|
|
// Release (and set state to DHCPSTATE_NOTBOUND) all interfaces - we are
|
117 |
|
|
// closing down. (unlikely but useful for testing)
|
118 |
|
|
// Interfaces are left up; use dhcp_halt() to bring them right down, then
|
119 |
|
|
// call init_all_network_interfaces() as usual to restart all.
|
120 |
|
|
extern int dhcp_release( void );
|
121 |
|
|
|
122 |
|
|
// The intent with this API is that a simple DHCP client thread, which
|
123 |
|
|
// maintains the state of the interfaces, can go as follows:
|
124 |
|
|
// (after init_all_networks is called from elsewhere)
|
125 |
|
|
//
|
126 |
|
|
// while ( 1 ) {
|
127 |
|
|
// while ( 1 ) {
|
128 |
|
|
// cyg_semaphore_wait( &dhcp_needs_attention );
|
129 |
|
|
// if ( ! dhcp_bind() ) // a lease expired
|
130 |
|
|
// break; // If we need to re-bind
|
131 |
|
|
// }
|
132 |
|
|
// dhcp_halt(); // tear everything down
|
133 |
|
|
// init_all_network_interfaces(); // re-initialize
|
134 |
|
|
// }
|
135 |
|
|
//
|
136 |
|
|
// and if the application does not want to suffer the overhead of a
|
137 |
|
|
// separate thread and its stack for this, this functionality can be placed
|
138 |
|
|
// in the app's server loop in an obvious fashion. That is the goal of
|
139 |
|
|
// breaking out these internal elements. For example, some server might
|
140 |
|
|
// be arranged to poll DHCP from time to time like this:
|
141 |
|
|
//
|
142 |
|
|
// while ( 1 ) {
|
143 |
|
|
// init_all_network_interfaces();
|
144 |
|
|
// open-my-listen-sockets();
|
145 |
|
|
// while ( 1 ) {
|
146 |
|
|
// serve-one-request();
|
147 |
|
|
// // sleeps if no connections, but not forever; so this loop is
|
148 |
|
|
// // polled a few times a minute...
|
149 |
|
|
// if ( cyg_semaphore_trywait( &dhcp_needs_attention )) {
|
150 |
|
|
// if ( ! dhcp_bind() ) {
|
151 |
|
|
// close-my-listen-sockets();
|
152 |
|
|
// dhcp_halt();
|
153 |
|
|
// break;
|
154 |
|
|
// }
|
155 |
|
|
// }
|
156 |
|
|
// }
|
157 |
|
|
// }
|
158 |
|
|
//
|
159 |
|
|
// ------------------------------------------------------------------------
|
160 |
|
|
|
161 |
|
|
// Set hostname to be used with the DHCP TAG_HOST_NAME option.
|
162 |
|
|
// Call this before calling init_all_network_interfaces() to
|
163 |
|
|
// set the hostname value.
|
164 |
|
|
#ifdef CYGOPT_NET_DHCP_OPTION_HOST_NAME
|
165 |
|
|
extern void dhcp_set_hostname(char *hostname);
|
166 |
|
|
#else
|
167 |
|
|
#define dhcp_set_hostname(hostname) CYG_EMPTY_STATEMENT
|
168 |
|
|
#endif
|
169 |
|
|
|
170 |
|
|
#ifdef CYGOPT_NET_DHCP_DHCP_THREAD
|
171 |
|
|
// Then we provide such a thread...
|
172 |
|
|
|
173 |
|
|
// Provide a separate thread to renew DHCP leases; otherwise the
|
174 |
|
|
// application MUST periodically examine the semaphore dhcp_needs_attention
|
175 |
|
|
// and call dhcp_bind() if it is signalled. If enabled, this thread does
|
176 |
|
|
// all that for you. Independent of this option, initialization of the
|
177 |
|
|
// interfaces still occurs in init_all_network_interfaces() and your
|
178 |
|
|
// startup code must call that. It will start the DHCP management thread
|
179 |
|
|
// if necessary. If a lease fails to be renewed, the management thread
|
180 |
|
|
// will shut down all interfaces and attempt to initialize all the
|
181 |
|
|
// interfaces again from scratch. This may cause chaos in the app, which
|
182 |
|
|
// is why managing the DHCP state in an application aware thread is
|
183 |
|
|
// actually better, just far less convenient for testing.
|
184 |
|
|
|
185 |
|
|
extern cyg_handle_t dhcp_mgt_thread_h; // To allow its external manipulation.
|
186 |
|
|
extern cyg_thread dhcp_mgt_thread; // The object itself
|
187 |
|
|
|
188 |
|
|
extern void dhcp_start_dhcp_mgt_thread( void );
|
189 |
|
|
|
190 |
|
|
#endif
|
191 |
|
|
|
192 |
|
|
// The function is provided unconditionally so that the app can put it in a
|
193 |
|
|
// thread of its own. If the parameter is true, it loops forever; if
|
194 |
|
|
// false, the call returns if a lease expires, and the caller must tidy up
|
195 |
|
|
// or reboot the whole machine.
|
196 |
|
|
extern cyg_thread_entry_t dhcp_mgt_entry;
|
197 |
|
|
extern void dhcp_mgt_entry( cyg_addrword_t loop_on_failure ); // the function
|
198 |
|
|
|
199 |
|
|
// ---------------------------------------------------------------------------
|
200 |
|
|
// These are rather more internal routines, internal to the protocol engine
|
201 |
|
|
// in dhcp_prot.c - those above are in dhcp_support.c
|
202 |
|
|
|
203 |
|
|
#define DHCP_LEASE_T1 1
|
204 |
|
|
#define DHCP_LEASE_T2 2
|
205 |
|
|
#define DHCP_LEASE_EX 4
|
206 |
|
|
|
207 |
|
|
struct dhcp_lease {
|
208 |
|
|
// Client settable: Semaphore to signal when attention is needed:
|
209 |
|
|
cyg_sem_t *needs_attention;
|
210 |
|
|
// Initialize all the rest to zero:
|
211 |
|
|
cyg_tick_count_t t1, t2, expiry;
|
212 |
|
|
volatile cyg_uint8 next;
|
213 |
|
|
volatile cyg_uint8 which;
|
214 |
|
|
cyg_handle_t alarm;
|
215 |
|
|
// except this one, which is just some space:
|
216 |
|
|
cyg_alarm alarm_obj;
|
217 |
|
|
};
|
218 |
|
|
|
219 |
|
|
// These dhcp_lease objects are initialized to use
|
220 |
|
|
// extern cyg_sem_t dhcp_needs_attention;
|
221 |
|
|
// for the semaphore.
|
222 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
223 |
|
|
extern struct dhcp_lease eth0_lease;
|
224 |
|
|
#endif
|
225 |
|
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
226 |
|
|
extern struct dhcp_lease eth1_lease;
|
227 |
|
|
#endif
|
228 |
|
|
|
229 |
|
|
extern int
|
230 |
|
|
do_dhcp(const char *interface, struct bootp *res,
|
231 |
|
|
cyg_uint8 *pstate, struct dhcp_lease *lease);
|
232 |
|
|
// NB *res and *pstate and *lease are all INOUT; *res must point to a valid
|
233 |
|
|
// record from "last time".
|
234 |
|
|
|
235 |
|
|
extern int
|
236 |
|
|
do_dhcp_down_net(const char *intf, struct bootp *res,
|
237 |
|
|
cyg_uint8 *pstate, struct dhcp_lease *lease);
|
238 |
|
|
|
239 |
|
|
extern int
|
240 |
|
|
do_dhcp_release(const char *intf, struct bootp *res,
|
241 |
|
|
cyg_uint8 *pstate, struct dhcp_lease *lease);
|
242 |
|
|
|
243 |
|
|
#endif // CYGPKG_NET_DHCP
|
244 |
|
|
|
245 |
|
|
#endif // CYGONCE_NET_TCPIP_DHCP_H
|
246 |
|
|
// EOF dhcp.h
|