1 | /*
|
---|
2 | * Copyright 2004 Henning Brauer <henning@openbsd.org>
|
---|
3 | * Copyright (c) 1995, 1996, 1997, 1998, 1999
|
---|
4 | * The Internet Software Consortium. All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * 3. Neither the name of The Internet Software Consortium nor the names
|
---|
16 | * of its contributors may be used to endorse or promote products derived
|
---|
17 | * from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
|
---|
20 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
---|
21 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
23 | * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
|
---|
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
---|
27 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
28 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * This software has been written for the Internet Software Consortium
|
---|
34 | * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
|
---|
35 | * Enterprises. To learn more about the Internet Software Consortium,
|
---|
36 | * see ``http://www.vix.com/isc''. To learn more about Vixie
|
---|
37 | * Enterprises, see ``http://www.vix.com''.
|
---|
38 | *
|
---|
39 | * This client was substantially modified and enhanced by Elliot Poger
|
---|
40 | * for use on Linux while he was working on the MosquitoNet project at
|
---|
41 | * Stanford.
|
---|
42 | *
|
---|
43 | * The current version owes much to Elliot's Linux enhancements, but
|
---|
44 | * was substantially reorganized and partially rewritten by Ted Lemon
|
---|
45 | * so as to use the same networking framework that the Internet Software
|
---|
46 | * Consortium DHCP server uses. Much system-specific configuration code
|
---|
47 | * was moved into a shell script so that as support for more operating
|
---|
48 | * systems is added, it will not be necessary to port and maintain
|
---|
49 | * system-specific configuration code to these operating systems - instead,
|
---|
50 | * the shell script can invoke the native tools to accomplish the same
|
---|
51 | * purpose.
|
---|
52 | */
|
---|
53 | /*
|
---|
54 | * WIDE Project DHCP Implementation
|
---|
55 | * Copyright (c) 1995-1997 Akihiro Tominaga
|
---|
56 | * Copyright (c) 1995-1997 WIDE Project
|
---|
57 | * All rights reserved.
|
---|
58 | *
|
---|
59 | * Permission to use, copy, modify and distribute this software and its
|
---|
60 | * documentation is hereby granted, provided the following conditions
|
---|
61 | * are satisfied,
|
---|
62 | *
|
---|
63 | * 1. Both the copyright notice and this permission notice appear in
|
---|
64 | * all copies of the software, derivative works or modified versions,
|
---|
65 | * and any portions thereof, and that both notices appear in
|
---|
66 | * supporting documentation.
|
---|
67 | * 2. All advertising materials mentioning features or use of this software
|
---|
68 | * must display the following acknowledgement:
|
---|
69 | * This product includes software developed by WIDE Project and
|
---|
70 | * its contributors.
|
---|
71 | * 3. Neither the name of WIDE Project nor the names of its contributors
|
---|
72 | * may be used to endorse or promote products derived from this software
|
---|
73 | * without specific prior written permission.
|
---|
74 | *
|
---|
75 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND WIDE
|
---|
76 | * PROJECT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
|
---|
77 | * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. ALSO, THERE
|
---|
78 | * IS NO WARRANTY IMPLIED OR OTHERWISE, NOR IS SUPPORT PROVIDED.
|
---|
79 | *
|
---|
80 | * Feedback of the results generated from any improvements or
|
---|
81 | * extensions made to this software would be much appreciated.
|
---|
82 | * Any such feedback should be sent to:
|
---|
83 | *
|
---|
84 | * Akihiro Tominaga
|
---|
85 | * WIDE Project
|
---|
86 | * Keio University, Endo 5322, Kanagawa, Japan
|
---|
87 | * (E-mail: dhcp-dist@wide.ad.jp)
|
---|
88 | *
|
---|
89 | * WIDE project has the rights to redistribute these changes.
|
---|
90 | */
|
---|
91 | /*
|
---|
92 | * TINET (TCP/IP Protocol Stack)
|
---|
93 | *
|
---|
94 | * Copyright (C) 2011 by Dep. of Computer Science and Engineering
|
---|
95 | * Tomakomai National College of Technology, JAPAN
|
---|
96 | *
|
---|
97 | * ä¸è¨èä½æ¨©è
|
---|
98 | ã¯ï¼ä»¥ä¸ã® (1)ï½(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
99 | * ã«ãã£ã¦å
|
---|
100 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
101 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
102 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
103 | å¸ï¼ä»¥ä¸ï¼
|
---|
104 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
105 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
106 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
107 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
108 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
109 | * ç¨ã§ããå½¢ã§åé
|
---|
110 | å¸ããå ´åã«ã¯ï¼åé
|
---|
111 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
112 | * è
|
---|
113 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
114 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
115 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
116 | * ç¨ã§ããªãå½¢ã§åé
|
---|
117 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
118 | * ã¨ï¼
|
---|
119 | * (a) åé
|
---|
120 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
121 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
122 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
123 | * (b) åé
|
---|
124 | å¸ã®å½¢æ
|
---|
125 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
126 | * å ±åãããã¨ï¼
|
---|
127 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
128 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
129 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
130 | 責ãããã¨ï¼
|
---|
131 | *
|
---|
132 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
133 | ã
|
---|
134 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
135 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
136 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
137 | *
|
---|
138 | * @(#) $Id$
|
---|
139 | */
|
---|
140 |
|
---|
141 | #include <string.h>
|
---|
142 |
|
---|
143 | #ifdef TARGET_KERNEL_ASP
|
---|
144 |
|
---|
145 | #include <kernel.h>
|
---|
146 | #include <sil.h>
|
---|
147 | #include <t_syslog.h>
|
---|
148 | #include "kernel_cfg.h"
|
---|
149 |
|
---|
150 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
151 |
|
---|
152 | #ifdef TARGET_KERNEL_JSP
|
---|
153 |
|
---|
154 | #include <s_services.h>
|
---|
155 | #include <t_services.h>
|
---|
156 | #include "kernel_id.h"
|
---|
157 |
|
---|
158 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
159 |
|
---|
160 | #include <tinet_defs.h>
|
---|
161 | #include <tinet_config.h>
|
---|
162 |
|
---|
163 | #include <net/if.h>
|
---|
164 | #include <net/ethernet.h>
|
---|
165 | #include <net/net.h>
|
---|
166 | #include <net/net_endian.h>
|
---|
167 | #include <net/net_var.h>
|
---|
168 | #include <net/net_timer.h>
|
---|
169 |
|
---|
170 | #include <netinet/in.h>
|
---|
171 | #include <netinet/in_var.h>
|
---|
172 | #include <netinet/in_itron.h>
|
---|
173 | #include <netinet/if_ether.h>
|
---|
174 |
|
---|
175 | #include <net/if_var.h>
|
---|
176 |
|
---|
177 | #include <netapp/netapp.h>
|
---|
178 | #include <netapp/netapp_var.h>
|
---|
179 | #include <netapp/dhcp4_cli.h>
|
---|
180 | #include <netapp/dhcp4_cli_var.h>
|
---|
181 | #include <netapp/resolver.h>
|
---|
182 | #include "lcd.h"
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * DHCP ã¯ã©ã¤ã¢ã³ãã¿ã¹ã¯
|
---|
186 | */
|
---|
187 |
|
---|
188 | #ifdef DHCP4_CLI_CFG
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * å¤æ°
|
---|
192 | */
|
---|
193 |
|
---|
194 | static T_DHCP4_CLI_CONTEXT context; /* ã¯ã©ã¤ã¢ã³ãã³ã³ããã¹ã */
|
---|
195 | static uint8_t magicno[DHCP4_MAGIC_LEN] = DHCP4_RFC1048_MAGIC; /* magic çªå·ï¼RFC1048ï¼*/
|
---|
196 |
|
---|
197 | /* å¿
|
---|
198 | é ãªãã·ã§ã³ãªã¹ã */
|
---|
199 | #ifdef DHCP4_CLI_CFG_REQUIRED_OLIST
|
---|
200 | static uint8_t required_plist[] = DHCP4_CLI_CFG_REQUIRED_OLIST;
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | /* è¦æ±ãªãã·ã§ã³ãªã¹ã */
|
---|
204 | #ifdef DHCP4_CLI_CFG_REQUEST_OLIST
|
---|
205 | static uint8_t request_plist[] = DHCP4_CLI_CFG_REQUEST_OLIST;
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * dhcp4_cksum -- ãã§ãã¯ãµã ã®è¨ç®é¢æ°
|
---|
210 | */
|
---|
211 |
|
---|
212 | static uint32_t
|
---|
213 | dhcp4_cksum (void *data, uint_t len /*ãªã¯ãããåä½*/)
|
---|
214 | {
|
---|
215 | uint32_t carry, sum = 0;
|
---|
216 |
|
---|
217 | for ( ; len > 0; len -= 2) {
|
---|
218 | sum += (*(uint8_t*)data << 8) + *((uint8_t*)data + 1);
|
---|
219 | data = (uint8_t*)data + 2;
|
---|
220 | }
|
---|
221 |
|
---|
222 | while (sum >= 0x00010000) {
|
---|
223 | carry = sum >> 16;
|
---|
224 | sum = (sum & 0xffff) + carry;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return sum;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * create_xid -- ãã©ã³ã¶ã¯ã·ã§ã³ ID ãçæããã
|
---|
232 | */
|
---|
233 |
|
---|
234 | static uint32_t
|
---|
235 | create_xid (void)
|
---|
236 | {
|
---|
237 | T_IF_SOFTC *ic;
|
---|
238 | SYSTIM time;
|
---|
239 |
|
---|
240 | ic = IF_ETHER_NIC_GET_SOFTC();
|
---|
241 | get_tim(&time);
|
---|
242 | return (dhcp4_cksum((void*)&ic->ifaddr.lladdr, ETHER_ADDR_LEN) << 16)
|
---|
243 | + dhcp4_cksum((void*)&time, sizeof(time));
|
---|
244 | }
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * find_option_sub -- æå®ãããªãã·ã§ã³ãæ¢ç´¢ããã
|
---|
248 | *
|
---|
249 | * 注æ: å¼æ° size ã 0 ã®æã¯ãªãã·ã§ã³ãµã¤ãºãæ¤è¨¼ããªãã
|
---|
250 | */
|
---|
251 |
|
---|
252 | static uint8_t *
|
---|
253 | find_option_sub (T_DHCP4_CLI_MSG *msg, uint_t off, uint_t len,
|
---|
254 | uint_t tag, uint_t size, bool_t overload)
|
---|
255 | {
|
---|
256 | uint8_t *opt, *tmp;
|
---|
257 |
|
---|
258 | opt = (uint8_t*)&msg->msg + off;
|
---|
259 | while (opt < (uint8_t*)&msg->msg + off + len && *opt != DHCP4_OPT_END) {
|
---|
260 | if (*opt == DHCP4_OPT_PAD)
|
---|
261 | opt ++;
|
---|
262 | else if ((*opt == DHCP4_OPT_OPT_OVERLOAD) && overload) {
|
---|
263 | if (*(opt + 2) & DHCP4_OPTOL_FILE) {
|
---|
264 | tmp = find_option_sub(msg, DHCP4_FILE_OFFSET, DHCP4_FILE_LEN, tag, len, false);
|
---|
265 | if (tmp != NULL)
|
---|
266 | return tmp;
|
---|
267 | }
|
---|
268 | if (*(opt + 2) & DHCP4_OPTOL_SNAME) {
|
---|
269 | tmp = find_option_sub(msg, DHCP4_SNAME_OFFSET, DHCP4_SNAME_LEN, tag, len, false);
|
---|
270 | if (tmp != NULL)
|
---|
271 | return tmp;
|
---|
272 | }
|
---|
273 | opt += *(opt + 1) + 2;
|
---|
274 | }
|
---|
275 | else if (*opt == tag && (size == 0 || *(opt + 1) == size))
|
---|
276 | return opt;
|
---|
277 | else
|
---|
278 | opt += *(opt + 1) + 2;
|
---|
279 | }
|
---|
280 | return NULL;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * find_option -- æå®ãããªãã·ã§ã³ãæ¢ç´¢ããã
|
---|
285 | *
|
---|
286 | * 注æ: å¼æ° size ã 0 ã®æã¯ãªãã·ã§ã³ãµã¤ãºãæ¤è¨¼ããªãã
|
---|
287 | */
|
---|
288 |
|
---|
289 | static uint8_t *
|
---|
290 | find_option (T_DHCP4_CLI_MSG *msg, uint_t tag, uint_t size)
|
---|
291 | {
|
---|
292 | return find_option_sub(msg, DHCP4_OPTION_OFFSET + DHCP4_MAGIC_LEN,
|
---|
293 | msg->len - (DHCP4_OPTION_OFFSET + DHCP4_MAGIC_LEN),
|
---|
294 | tag, size, true);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * is_equal_sid -- ã¡ãã»ã¼ã¸å
|
---|
299 | ã® SERVER ID ãæ¯è¼ããã
|
---|
300 | */
|
---|
301 |
|
---|
302 | static bool_t
|
---|
303 | is_equal_sid (T_DHCP4_CLI_MSG *msg1, T_DHCP4_CLI_MSG *msg2)
|
---|
304 | {
|
---|
305 | uint8_t *opt1, *opt2;
|
---|
306 |
|
---|
307 | if ((opt1 = find_option(msg1, DHCP4_OPT_SERVER_ID, sizeof(T_IN4_ADDR))) == NULL)
|
---|
308 | return false;
|
---|
309 | if ((opt2 = find_option(msg2, DHCP4_OPT_SERVER_ID, sizeof(T_IN4_ADDR))) == NULL)
|
---|
310 | return false;
|
---|
311 |
|
---|
312 | return msg2host32(opt1) == msg2host32(opt2);
|
---|
313 | }
|
---|
314 |
|
---|
315 | /*
|
---|
316 | * timer_value -- ã¡ãã»ã¼ã¸ããæéæ
|
---|
317 | å ±ãåãåºãã
|
---|
318 | */
|
---|
319 |
|
---|
320 | static uint32_t
|
---|
321 | timer_value (T_DHCP4_CLI_MSG *msg, uint_t tag, uint32_t def_value)
|
---|
322 | {
|
---|
323 | uint8_t *opt;
|
---|
324 | uint32_t seconds;
|
---|
325 |
|
---|
326 | if ((opt = find_option(msg, tag, sizeof(uint32_t))) == NULL)
|
---|
327 | return def_value;
|
---|
328 | else {
|
---|
329 |
|
---|
330 | /* ãµã¼ãããã®ãªã¼ã¹æéã¯[s]ã*/
|
---|
331 | seconds = msg2host32(opt + 2);
|
---|
332 |
|
---|
333 | /* ã³ã³ããã¹ãå
|
---|
334 | ã®ãªã¼ã¹æé㯠SYSTIM_HZã*/
|
---|
335 | if (seconds == DHCP4_TIME_INFINITY)
|
---|
336 | return DHCP4_TIME_INFINITY;
|
---|
337 | else if (seconds > SYSTIM2SEC(DHCP4_TIME_INFINITY))
|
---|
338 | return DHCP4_TIME_INFINITY;
|
---|
339 | else
|
---|
340 | return SEC2SYSTIM(seconds);
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * rel_addr -- IPv4 ã¢ãã¬ã¹ããµãããããã¹ã¯ãéççµè·¯è¡¨ã解æ¾ããã
|
---|
346 | */
|
---|
347 |
|
---|
348 | static void
|
---|
349 | rel_addr (void)
|
---|
350 | {
|
---|
351 | ER error;
|
---|
352 | uint_t count = 0;
|
---|
353 |
|
---|
354 | if ((error = in4_add_ifaddr(IPV4_ADDRANY, IPV4_ADDRANY)) != E_OK) {
|
---|
355 | syslog(LOG_NOTICE, "[DHCP4C] error, IPv4 addr: %s", itron_strerror(error));
|
---|
356 | count ++;
|
---|
357 | }
|
---|
358 |
|
---|
359 | if ((error = in4_add_route(0, IPV4_ADDRANY, IPV4_ADDRANY, IPV4_ADDRANY)) != E_OK) {
|
---|
360 | syslog(LOG_NOTICE, "[DHCP4C] error, release routing table: %s", itron_strerror(error));
|
---|
361 | count ++;
|
---|
362 | }
|
---|
363 |
|
---|
364 | if ((error = in4_add_route(1, IPV4_ADDRANY, IPV4_ADDRANY, IPV4_ADDRANY)) != E_OK) {
|
---|
365 | syslog(LOG_NOTICE, "[DHCP4C] error, release routing table: %s", itron_strerror(error));
|
---|
366 | count ++;
|
---|
367 | }
|
---|
368 |
|
---|
369 | if ((error = in4_add_route(2, 0xffffffff, 0xffffffff, IPV4_ADDRANY)) != E_OK) {
|
---|
370 | syslog(LOG_NOTICE, "[DHCP4C] error, release routing table: %s", itron_strerror(error));
|
---|
371 | count ++;
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (count == 0) {
|
---|
375 | syslog(LOG_NOTICE, "[DHCP4C] release IPv4 addr.");
|
---|
376 |
|
---|
377 | #if 0
|
---|
378 | #ifdef USE_LCD
|
---|
379 | dly_tsk(1 * 1000);
|
---|
380 | lcd_puts(LCD_PORTID, "0.0.0.0\n");
|
---|
381 | #endif /* of #ifdef USE_LCD */
|
---|
382 | #endif
|
---|
383 |
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | /*
|
---|
388 | * rel_options -- ãªã¼ã¹ããããªãã·ã§ã³ã解æãã¦å¤ã解æ¾ããã
|
---|
389 | */
|
---|
390 |
|
---|
391 | static void
|
---|
392 | rel_options (T_DHCP4_CLI_CONTEXT *ct, uint_t off, uint_t len, bool_t overload)
|
---|
393 | {
|
---|
394 | #if defined(USE_RESOLVER)
|
---|
395 | T_IN4_ADDR addr;
|
---|
396 | #endif
|
---|
397 |
|
---|
398 | uint8_t *opt;
|
---|
399 |
|
---|
400 | opt = (uint8_t*)&ct->act_msg->msg + off;
|
---|
401 | while (opt < (uint8_t*)&ct->act_msg->msg + off + len && *opt != DHCP4_OPT_END) {
|
---|
402 | if (*opt == DHCP4_OPT_PAD)
|
---|
403 | opt ++;
|
---|
404 | else if ((*opt == DHCP4_OPT_OPT_OVERLOAD) && overload) {
|
---|
405 | if (*(opt + 2) & DHCP4_OPTOL_FILE)
|
---|
406 | rel_options(ct, DHCP4_FILE_OFFSET, DHCP4_FILE_LEN, false);
|
---|
407 | if (*(opt + 2) & DHCP4_OPTOL_SNAME)
|
---|
408 | rel_options(ct, DHCP4_SNAME_OFFSET, DHCP4_SNAME_LEN, false);
|
---|
409 | opt += *(opt + 1) + 2;
|
---|
410 | }
|
---|
411 | else {
|
---|
412 |
|
---|
413 | switch (*opt) {
|
---|
414 |
|
---|
415 | #if defined(USE_RESOLVER)
|
---|
416 |
|
---|
417 | case DHCP4_OPT_DNS_SERVER:
|
---|
418 | addr = IPV4_ADDRANY;
|
---|
419 | dns_in4_set_addr(&addr);
|
---|
420 | break;
|
---|
421 |
|
---|
422 | case DHCP4_OPT_HOST_NAME:
|
---|
423 | break;
|
---|
424 |
|
---|
425 | case DHCP4_OPT_DOMAIN_NAME:
|
---|
426 | dns_in4_set_dname("", 0);
|
---|
427 | break;
|
---|
428 |
|
---|
429 | #else /* of #if defined(USE_RESOLVER) */
|
---|
430 |
|
---|
431 | case DHCP4_OPT_DNS_SERVER:
|
---|
432 | case DHCP4_OPT_HOST_NAME:
|
---|
433 | case DHCP4_OPT_DOMAIN_NAME:
|
---|
434 |
|
---|
435 | #endif /* of #if defined(USE_RESOLVER) */
|
---|
436 |
|
---|
437 | case DHCP4_OPT_SUBNET_MASK:
|
---|
438 | case DHCP4_OPT_ROUTER:
|
---|
439 | case DHCP4_OPT_LEASE_TIME:
|
---|
440 | case DHCP4_OPT_RENEWAL_T1:
|
---|
441 | case DHCP4_OPT_REBINDING_T2:
|
---|
442 | break;
|
---|
443 |
|
---|
444 | default:
|
---|
445 | break;
|
---|
446 | }
|
---|
447 |
|
---|
448 | opt += *(opt + 1) + 2;
|
---|
449 | }
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | /*
|
---|
454 | * rel_lease -- ãªã¼ã¹ãããæ
|
---|
455 | å ±ã解æ¾ããã
|
---|
456 | */
|
---|
457 |
|
---|
458 | static void
|
---|
459 | rel_lease (T_DHCP4_CLI_CONTEXT *ct)
|
---|
460 | {
|
---|
461 | /* æå¹ãªãªã¼ã¹ã»ã¡ãã»ã¼ã¸æ§é ä½ãç¡ããã°ä½ãããªãã*/
|
---|
462 | if (ct->act_msg == NULL)
|
---|
463 | return;
|
---|
464 |
|
---|
465 | /* ãªã¼ã¹ããããªãã·ã§ã³ã解æãã¦å¤ã解æ¾ããã*/
|
---|
466 | rel_options(ct, DHCP4_OPTION_OFFSET + DHCP4_MAGIC_LEN,
|
---|
467 | ct->act_msg->len - (DHCP4_OPTION_OFFSET + DHCP4_MAGIC_LEN), true);
|
---|
468 | rel_addr();
|
---|
469 |
|
---|
470 | /* æå¹ãªãªã¼ã¹ã»ã¡ãã»ã¼ã¸æ§é ä½ã解æ¾ããã*/
|
---|
471 | syscall(rel_mpf(MPF_DHCP4_CLI_MSG, (void*)ct->act_msg));
|
---|
472 | ct->act_msg = NULL;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * set_addr -- IPv4 ã¢ãã¬ã¹ããµãããããã¹ã¯ãéççµè·¯è¡¨ãè¨å®ããã
|
---|
477 | */
|
---|
478 |
|
---|
479 | static void
|
---|
480 | set_addr (T_DHCP4_CLI_CONTEXT *ct, uint8_t *mopt, uint8_t *ropt)
|
---|
481 | {
|
---|
482 | ER error;
|
---|
483 | T_IN4_ADDR addr, mask, router, net_addr;
|
---|
484 |
|
---|
485 | addr = ntohl(ct->act_msg->msg.yiaddr);
|
---|
486 | mask = msg2host32(mopt + 2);
|
---|
487 | net_addr = addr & mask;
|
---|
488 | router = msg2host32(ropt + 2);
|
---|
489 |
|
---|
490 | if ((error = in4_add_ifaddr(addr, mask)) != E_OK)
|
---|
491 | syslog(LOG_NOTICE, "[DHCP4C] error, set IPv4 addr: %s", itron_strerror(error));
|
---|
492 |
|
---|
493 | if ((error = in4_add_route(0, IPV4_ADDRANY, IPV4_ADDRANY, router)) != E_OK)
|
---|
494 | syslog(LOG_NOTICE, "[DHCP4C] error, set routing table: %s", itron_strerror(error));
|
---|
495 |
|
---|
496 | if ((error = in4_add_route(1, net_addr, mask, IPV4_ADDRANY)) != E_OK)
|
---|
497 | syslog(LOG_NOTICE, "[DHCP4C] error, set routing table: %s", itron_strerror(error));
|
---|
498 |
|
---|
499 | if ((error = in4_add_route(2, 0xffffffff, 0xffffffff, IPV4_ADDRANY)) != E_OK)
|
---|
500 | syslog(LOG_NOTICE, "[DHCP4C] error, set routing table: %s", itron_strerror(error));
|
---|
501 |
|
---|
502 | if (error == E_OK) {
|
---|
503 | syslog(LOG_NOTICE, "[DHCP4C] set IPv4 addr: %s.", ip2str(NULL, &addr));
|
---|
504 |
|
---|
505 | #ifdef USE_LCD
|
---|
506 | dly_tsk(1 * 1000);
|
---|
507 | lcd_puts(LCD_PORTID, ip2str(NULL, &addr));
|
---|
508 | lcd_putc(LCD_PORTID, '\n');
|
---|
509 | #endif /* of #ifdef USE_LCD */
|
---|
510 | if (ct->set_addr_callback != NULL)
|
---|
511 | ct->set_addr_callback();
|
---|
512 |
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * set_options -- ãªã¼ã¹ããããªãã·ã§ã³ã解æãã¦å¤ãè¨å®ããã
|
---|
518 | */
|
---|
519 |
|
---|
520 | static void
|
---|
521 | set_options (T_DHCP4_CLI_CONTEXT *ct, uint_t off, uint_t len, bool_t overload)
|
---|
522 | {
|
---|
523 | #if defined(USE_RESOLVER)
|
---|
524 | T_IN4_ADDR addr;
|
---|
525 | #endif
|
---|
526 |
|
---|
527 | uint8_t *opt, *mopt = NULL, *ropt = NULL;
|
---|
528 |
|
---|
529 | opt = (uint8_t*)&ct->act_msg->msg + off;
|
---|
530 | while (opt < (uint8_t*)&ct->act_msg->msg + off + len && *opt != DHCP4_OPT_END) {
|
---|
531 | if (*opt == DHCP4_OPT_PAD)
|
---|
532 | opt ++;
|
---|
533 | else if ((*opt == DHCP4_OPT_OPT_OVERLOAD) && overload) {
|
---|
534 | if (*(opt + 2) & DHCP4_OPTOL_FILE)
|
---|
535 | set_options(ct, DHCP4_FILE_OFFSET, DHCP4_FILE_LEN, false);
|
---|
536 | if (*(opt + 2) & DHCP4_OPTOL_SNAME)
|
---|
537 | set_options(ct, DHCP4_SNAME_OFFSET, DHCP4_SNAME_LEN, false);
|
---|
538 | opt += *(opt + 1) + 2;
|
---|
539 | }
|
---|
540 | else {
|
---|
541 |
|
---|
542 | switch (*opt) {
|
---|
543 | case DHCP4_OPT_SUBNET_MASK:
|
---|
544 | mopt = opt;
|
---|
545 | break;
|
---|
546 |
|
---|
547 | case DHCP4_OPT_ROUTER:
|
---|
548 | ropt = opt;
|
---|
549 | break;
|
---|
550 |
|
---|
551 | #if defined(USE_RESOLVER)
|
---|
552 |
|
---|
553 | case DHCP4_OPT_DNS_SERVER:
|
---|
554 | addr = msg2host32(opt + 2);
|
---|
555 | dns_in4_set_addr(&addr);
|
---|
556 | break;
|
---|
557 |
|
---|
558 | case DHCP4_OPT_HOST_NAME:
|
---|
559 | break;
|
---|
560 |
|
---|
561 | case DHCP4_OPT_DOMAIN_NAME:
|
---|
562 | dns_in4_set_dname(opt + 2, *(opt + 1));
|
---|
563 | break;
|
---|
564 |
|
---|
565 | #else /* of #if defined(USE_RESOLVER) */
|
---|
566 |
|
---|
567 | case DHCP4_OPT_DNS_SERVER:
|
---|
568 | case DHCP4_OPT_HOST_NAME:
|
---|
569 | case DHCP4_OPT_DOMAIN_NAME:
|
---|
570 |
|
---|
571 | #endif /* of #if defined(USE_RESOLVER) */
|
---|
572 |
|
---|
573 | case DHCP4_OPT_LEASE_TIME:
|
---|
574 | case DHCP4_OPT_RENEWAL_T1:
|
---|
575 | case DHCP4_OPT_REBINDING_T2:
|
---|
576 | break;
|
---|
577 |
|
---|
578 | default:
|
---|
579 | break;
|
---|
580 | }
|
---|
581 |
|
---|
582 | opt += *(opt + 1) + 2;
|
---|
583 | }
|
---|
584 | }
|
---|
585 | if (!(mopt == NULL || ropt == NULL))
|
---|
586 | set_addr(ct, mopt, ropt);
|
---|
587 | }
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * set_lease -- ãªã¼ã¹ãããæ
|
---|
591 | å ±ãè¨å®ããã
|
---|
592 | */
|
---|
593 |
|
---|
594 | static void
|
---|
595 | set_lease (T_DHCP4_CLI_CONTEXT *ct)
|
---|
596 | {
|
---|
597 | /* ãªã¼ã¹ããããªãã·ã§ã³ã解æãã¦å¤ãè¨å®ããã*/
|
---|
598 | set_options(ct, DHCP4_OPTION_OFFSET + DHCP4_MAGIC_LEN,
|
---|
599 | ct->act_msg->len - (DHCP4_OPTION_OFFSET + DHCP4_MAGIC_LEN), true);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * validate_duplicate -- æå®ããã IPv4 ã¢ãã¬ã¹ã使ããã¦ããªããæ¤è¨¼ããã
|
---|
604 | *
|
---|
605 | * 使ããã¦ããªãæ㯠true ãè¿ãã
|
---|
606 | */
|
---|
607 |
|
---|
608 | static bool_t
|
---|
609 | validate_duplicate (T_DHCP4_CLI_MSG *msg)
|
---|
610 | {
|
---|
611 | T_IN4_ADDR yiaddr;
|
---|
612 |
|
---|
613 | /* æå®ããã IPv4 ã¢ãã¬ã¹ã ADDR ANY ãªã使ããã¦ããªãã¨å¤æããã*/
|
---|
614 | yiaddr = ntohl(msg->msg.yiaddr);
|
---|
615 | if (yiaddr == IPV4_ADDRANY)
|
---|
616 | return true;
|
---|
617 |
|
---|
618 | /* ARP ãã£ãã·ã¥ã«ããã°ä½¿ããã¦ããã¨å¤æããã*/
|
---|
619 | if (arp_lookup(yiaddr, false) != NULL)
|
---|
620 | return false;
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * ARP è¦æ±ãåºåã㦠TMO_DHCP4C_ARP_WAIT å¾
|
---|
624 | ã¡ã
|
---|
625 | * ARP ãã£ãã·ã¥ã«ãªããã°ä½¿ããã¦ããªãã¨å¤æããã
|
---|
626 | */
|
---|
627 | arp_request(&IF_ETHER_NIC_GET_SOFTC()->ifaddr, yiaddr);
|
---|
628 | dly_tsk(TMO_DHCP4C_ARP_WAIT);
|
---|
629 | return arp_lookup(yiaddr, false) == NULL;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * validate_rcv_msg -- åä¿¡ã¡ãã»ã¼ã¸ãæ¤è¨¼ããã
|
---|
634 | */
|
---|
635 |
|
---|
636 | static ER
|
---|
637 | validate_rcv_msg (T_DHCP4_CLI_CONTEXT *ct, T_DHCP4_CLI_MSG *msg)
|
---|
638 | {
|
---|
639 | uint8_t *opt;
|
---|
640 |
|
---|
641 | /* packet type ã DHCP4_REPLY ã§ãããã¨ã確èªããã*/
|
---|
642 | if (!(msg->msg.op == DHCP4_REPLY))
|
---|
643 | return false;
|
---|
644 |
|
---|
645 | /* ã¡ãã»ã¼ã¸ã¿ã¤ããªãã·ã§ã³ããããã¨ã確èªããã*/
|
---|
646 | if ((opt = find_option(msg, DHCP4_OPT_MSG_TYPE, 1)) == NULL)
|
---|
647 | return E_PAR;
|
---|
648 |
|
---|
649 | /* ã¡ãã»ã¼ã¸ã®ã¿ã¤ããä¸è´ãã¦ãããã¨ãæ¤è¨¼ããã*/
|
---|
650 | msg->type = *(opt + 2);
|
---|
651 | switch (ct->fsm) {
|
---|
652 |
|
---|
653 | case DHCP4_FSM_SELECT:
|
---|
654 | if (!(msg->type == DHCP4_MTYPE_OFFER))
|
---|
655 | return E_PAR;
|
---|
656 | break;
|
---|
657 |
|
---|
658 | case DHCP4_FSM_REQUEST:
|
---|
659 | case DHCP4_FSM_REBOOT:
|
---|
660 | case DHCP4_FSM_RENEW:
|
---|
661 | case DHCP4_FSM_REBIND:
|
---|
662 | case DHCP4_FSM_REL_INFO:
|
---|
663 | if (!((msg->type == DHCP4_MTYPE_ACK) || (msg->type == DHCP4_MTYPE_NAK)))
|
---|
664 | return E_PAR;
|
---|
665 | break;
|
---|
666 |
|
---|
667 | case DHCP4_FSM_INIT:
|
---|
668 | case DHCP4_FSM_BOUND:
|
---|
669 | case DHCP4_FSM_INIT_REBOOT:
|
---|
670 | return E_PAR;
|
---|
671 | break;
|
---|
672 |
|
---|
673 | default:
|
---|
674 | syslog(LOG_NOTICE, "[DHCP4C] error, unknown state: %d.", ct->fsm);
|
---|
675 | return E_PAR;
|
---|
676 | break;
|
---|
677 | }
|
---|
678 |
|
---|
679 | /* ãã¸ãã¯çªå·ãæ¤è¨¼ããã*/
|
---|
680 | if (memcmp((uint8_t*)&msg->msg.options, magicno, DHCP4_MAGIC_LEN))
|
---|
681 | return E_PAR;
|
---|
682 |
|
---|
683 | /* ãã©ã³ã¶ã¯ã·ã§ã³ ID ãç°ãªã£ã¦ããã°ã¨ã©ã¼ */
|
---|
684 | if (ntohl(msg->msg.xid) != ct->xid)
|
---|
685 | return E_PAR;
|
---|
686 |
|
---|
687 | /* ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹é·ã 6ï¼MAC ã¢ãã¬ã¹é·ï¼ã§ãªããã°ã¨ã©ã¼ */
|
---|
688 | if (msg->msg.hlen != ETHER_ADDR_LEN)
|
---|
689 | return E_PAR;
|
---|
690 |
|
---|
691 | /* ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹ãç°ãªã£ã¦ããã°ã¨ã©ã¼ */
|
---|
692 | if (memcmp(&msg->msg.chaddr, &ct->sc->ifaddr.lladdr, sizeof(ct->sc->ifaddr.lladdr)))
|
---|
693 | return E_PAR;
|
---|
694 |
|
---|
695 | /*
|
---|
696 | * ã¡ãã»ã¼ã¸ã¿ã¤ãã OFFER ã®å ´åã¯ã
|
---|
697 | * ARP ã使ç¨ãã¦ãæå®ããã IPv4 ã¢ãã¬ã¹ã使ããã¦ããªããæ¤è¨¼ããã
|
---|
698 | * 使ããã¦ããæã¯ãä»ã® IPv4 ã¢ãã¬ã¹ã®æä¾ãå¾
|
---|
699 | ã¤ã
|
---|
700 | */
|
---|
701 | if (msg->type == DHCP4_MTYPE_OFFER) {
|
---|
702 | if (!validate_duplicate(msg))
|
---|
703 | return E_PAR;
|
---|
704 | }
|
---|
705 |
|
---|
706 | return E_OK;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /*
|
---|
710 | * eval_prefer -- åä¿¡ã¡ãã»ã¼ã¸ã®æ¨å¥¨åº¦ãè©ä¾¡ããã
|
---|
711 | */
|
---|
712 |
|
---|
713 | static void
|
---|
714 | eval_prefer (T_DHCP4_CLI_MSG *msg)
|
---|
715 | {
|
---|
716 | int ix;
|
---|
717 |
|
---|
718 | /* ç¡å¹ã®å ´åã¯çµäºããã*/
|
---|
719 | if (msg == NULL)
|
---|
720 | return;
|
---|
721 |
|
---|
722 | /* æå®ããã IPv4 ã¢ãã¬ã¹ã ADDR ANY ãªãæ¨å¥¨åº¦ã 0ã«è¨å®ããã*/
|
---|
723 | if (ntohl(msg->msg.yiaddr) == IPV4_ADDRANY)
|
---|
724 | msg->prefer = 0;
|
---|
725 | else
|
---|
726 | msg->prefer = DHCP4_CLI_CFG_PREFER_ADDR;
|
---|
727 |
|
---|
728 | #if defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
729 |
|
---|
730 | /* è¦æ±ãªãã·ã§ã³ãæ¤è¨¼ããã*/
|
---|
731 | for (ix = sizeof(request_plist); ix -- > 0; ) {
|
---|
732 | if (find_option(msg, request_plist[ix], 0) != NULL)
|
---|
733 | msg->prefer ++;
|
---|
734 | }
|
---|
735 |
|
---|
736 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUEST_OLIST) */
|
---|
737 |
|
---|
738 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST)
|
---|
739 |
|
---|
740 | /* å¿
|
---|
741 | é ãªãã·ã§ã³ãæ¤è¨¼ããã*/
|
---|
742 | for (ix = sizeof(required_plist); ix -- > 0; ) {
|
---|
743 | if (find_option(msg, required_plist[ix], 0) == NULL)
|
---|
744 | msg->prefer = 0;
|
---|
745 | }
|
---|
746 |
|
---|
747 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) */
|
---|
748 | }
|
---|
749 |
|
---|
750 | /*
|
---|
751 | * eval_rcv_msg -- åä¿¡ã¡ãã»ã¼ã¸ãè©ä¾¡ããã
|
---|
752 | */
|
---|
753 |
|
---|
754 | static ER
|
---|
755 | eval_rcv_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
756 | {
|
---|
757 | T_DHCP4_CLI_MSG *evl, *msg;
|
---|
758 |
|
---|
759 | /* ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ããªããã°ä¸æ¢ããã*/
|
---|
760 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
761 | if (ct->val_lst == NULL) {
|
---|
762 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
763 | return E_OK;
|
---|
764 | }
|
---|
765 |
|
---|
766 | /* æ¤è¨¼ã¡ãã»ã¼ã¸ãªã¹ãããè©ä¾¡ã¡ãã»ã¼ã¸ãåãåºãã*/
|
---|
767 | evl = ct->val_lst;
|
---|
768 | ct->val_lst = ct->val_lst->next;
|
---|
769 | evl->next = NULL;
|
---|
770 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
771 |
|
---|
772 | /* è©ä¾¡ã¡ãã»ã¼ã¸ãæ¤è¨¼ããã*/
|
---|
773 | if (validate_rcv_msg(ct, evl) != E_OK) {
|
---|
774 |
|
---|
775 | /* ã¨ã©ã¼ãçºçããè©ä¾¡ã¡ãã»ã¼ã¸ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã*/
|
---|
776 | goto rcv_ret;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /* åä¿¡ããã¡ãã»ã¼ã¸ã NAK ã®æã®å¦ç */
|
---|
780 | if (evl->type == DHCP4_MTYPE_NAK) {
|
---|
781 |
|
---|
782 | /* ãªã¼ã¹ãããæ
|
---|
783 | å ±ã解æ¾ããã*/
|
---|
784 | rel_lease(ct);
|
---|
785 |
|
---|
786 | /* å
|
---|
787 | é¨ã¨ã©ã¼ãè¨å®ãã¦çµäºããã*/
|
---|
788 | return E_PAR;
|
---|
789 | }
|
---|
790 |
|
---|
791 | /* REL_INFO ç¶æ
|
---|
792 | ã®æã®å¦ç */
|
---|
793 | //cons_printf(CONSOLE_PORTID, "evl_rcv_msg3[s=%d]\n", ct->fsm);
|
---|
794 | if (ct->fsm == DHCP4_FSM_REL_INFO) {
|
---|
795 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
796 | evl->next = ct->prf_lst;
|
---|
797 | ct->prf_lst = evl;
|
---|
798 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
799 | return E_OK;
|
---|
800 | }
|
---|
801 |
|
---|
802 | /* è©ä¾¡ã¡ãã»ã¼ã¸ã®æ¨å¥¨åº¦ãè©ä¾¡ããã*/
|
---|
803 | eval_prefer(evl);
|
---|
804 |
|
---|
805 | /* æ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã空ãªãè©ä¾¡ã¡ãã»ã¼ã¸ã移ãã¦çµäºããã*/
|
---|
806 | if (ct->prf_lst == NULL) {
|
---|
807 | ct->max_prefer = evl->prefer;
|
---|
808 | ct->prf_lst = evl;
|
---|
809 | return E_OK;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /* è©ä¾¡ã¡ãã»ã¼ã¸ã®æ¨å¥¨åº¦ã¨æ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã®æ大æ¨å¥¨åº¦ãæ¯è¼ããã*/
|
---|
813 | if (evl->prefer > ct->max_prefer) {
|
---|
814 |
|
---|
815 | /*
|
---|
816 | * è©ä¾¡ã¡ãã»ã¼ã¸ã®æ¨å¥¨åº¦ãæ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã®æ大æ¨å¥¨åº¦ããé«ããã°ã
|
---|
817 | * æ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ããåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã
|
---|
818 | * è©ä¾¡ã¡ãã»ã¼ã¸ãæ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã«ç§»ãã
|
---|
819 | */
|
---|
820 | ct->max_prefer = evl->prefer;
|
---|
821 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
822 | if (ct->rcv_lst == NULL)
|
---|
823 | ct->rcv_lst = ct->prf_lst;
|
---|
824 | else {
|
---|
825 | for (msg = ct->rcv_lst; msg->next != NULL; msg = msg->next)
|
---|
826 | ;
|
---|
827 | msg->next = ct->prf_lst;
|
---|
828 | }
|
---|
829 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
830 | ct->prf_lst = evl;
|
---|
831 | return E_OK;
|
---|
832 | }
|
---|
833 |
|
---|
834 | else if (evl->prefer == ct->max_prefer) {
|
---|
835 | for (msg = ct->prf_lst; msg != NULL; msg = msg->next) {
|
---|
836 | if (is_equal_sid(evl, msg)) {
|
---|
837 |
|
---|
838 | /*
|
---|
839 | * æ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã«åã DHCP4_OPT_SERVER_ID ã®ã¡ãã»ã¼ã¸ãããã°ã
|
---|
840 | * è©ä¾¡ã¡ãã»ã¼ã¸ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã
|
---|
841 | */
|
---|
842 | goto rcv_ret;
|
---|
843 | }
|
---|
844 | }
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * è©ä¾¡ã¡ãã»ã¼ã¸ãæ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã«ç§»ãã
|
---|
848 | */
|
---|
849 | evl->next = ct->prf_lst;
|
---|
850 | ct->prf_lst = evl;
|
---|
851 | return E_OK;
|
---|
852 | }
|
---|
853 |
|
---|
854 | /*else */
|
---|
855 | /*
|
---|
856 | * è©ä¾¡ã¡ãã»ã¼ã¸ã®æ¨å¥¨åº¦ãæ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã®æ大æ¨å¥¨åº¦ããä½ããã°ã
|
---|
857 | * è©ä¾¡ã¡ãã»ã¼ã¸ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã
|
---|
858 | */
|
---|
859 |
|
---|
860 | rcv_ret:
|
---|
861 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
862 | evl->next = ct->rcv_lst;
|
---|
863 | ct->rcv_lst = evl;
|
---|
864 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
865 | return E_OK;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /*
|
---|
869 | * select_msg -- æ¨å¥¨ã¡ãã»ã¼ã¸ãé¸æããã
|
---|
870 | */
|
---|
871 |
|
---|
872 | static T_DHCP4_CLI_MSG*
|
---|
873 | select_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
874 | {
|
---|
875 | T_DHCP4_CLI_MSG *msg, *select;
|
---|
876 |
|
---|
877 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
878 | if (ct->prf_lst == NULL) {
|
---|
879 | /* ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ããªããã°ä¸æ¢ããã*/
|
---|
880 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
881 | return NULL;
|
---|
882 | }
|
---|
883 |
|
---|
884 | /* æ¨å¥¨ã¡ãã»ã¼ã¸ãªã¹ãã®å
|
---|
885 | é ã®ã¡ãã»ã¼ã¸ãé¸æããã*/
|
---|
886 | select = ct->prf_lst;
|
---|
887 | ct->prf_lst = ct->prf_lst->next;
|
---|
888 | select->next = NULL;
|
---|
889 |
|
---|
890 | /* é¸æãããªãã£ãæ¤è¨¼ã¡ãã»ã¼ã¸ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã*/
|
---|
891 | if (ct->rcv_lst == NULL)
|
---|
892 | ct->rcv_lst = ct->prf_lst;
|
---|
893 | else {
|
---|
894 | for (msg = ct->rcv_lst; msg->next != NULL; msg = msg->next)
|
---|
895 | ;
|
---|
896 | msg->next = ct->prf_lst;
|
---|
897 | }
|
---|
898 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
899 | return select;
|
---|
900 | }
|
---|
901 |
|
---|
902 | /*
|
---|
903 | * select_ack -- ACK ã¡ãã»ã¼ã¸ãæ¤è¨¼ããã
|
---|
904 | *
|
---|
905 | * åãå
|
---|
906 | ¥ãããã¨ãå¯è½ã®æ㯠true ãè¿ãã
|
---|
907 | */
|
---|
908 |
|
---|
909 | static bool_t
|
---|
910 | select_ack (T_DHCP4_CLI_CONTEXT *ct)
|
---|
911 | {
|
---|
912 | T_DHCP4_CLI_MSG *msg;
|
---|
913 |
|
---|
914 | if ((msg = select_msg(ct)) == NULL)
|
---|
915 | return false;
|
---|
916 |
|
---|
917 | /* æå¹ãªãªã¼ã¹ã»ã¡ãã»ã¼ã¸ãæ®ã£ã¦ããã°ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã*/
|
---|
918 | if (ct->act_msg != NULL) {
|
---|
919 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
920 | ct->act_msg->next = ct->rcv_lst;
|
---|
921 | ct->rcv_lst = ct->act_msg;
|
---|
922 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
923 | }
|
---|
924 |
|
---|
925 | /* æå¹ãªãªã¼ã¹ã»ã¡ãã»ã¼ã¸ã«è¨å®ããã*/
|
---|
926 | ct->act_msg = msg;
|
---|
927 | return true;
|
---|
928 | }
|
---|
929 |
|
---|
930 | /*
|
---|
931 | * select_offer -- OFFER ã¡ãã»ã¼ã¸ãæ¤è¨¼ããã
|
---|
932 | *
|
---|
933 | * åãå
|
---|
934 | ¥ãããã¨ãå¯è½ã®æ㯠true ãè¿ãã
|
---|
935 | */
|
---|
936 |
|
---|
937 | static bool_t
|
---|
938 | select_offer (T_DHCP4_CLI_CONTEXT *ct)
|
---|
939 | {
|
---|
940 | return (ct->off_msg = select_msg(ct)) != NULL;
|
---|
941 | }
|
---|
942 |
|
---|
943 | /*
|
---|
944 | * cancel_all_timers -- å
|
---|
945 | ¨ã¦ã® TIMER ããã£ã³ã»ã«ããã
|
---|
946 | */
|
---|
947 |
|
---|
948 | static void
|
---|
949 | cancel_all_timers (T_DHCP4_CLI_CONTEXT *ct)
|
---|
950 | {
|
---|
951 | int ix;
|
---|
952 |
|
---|
953 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
954 | for (ix = NUM_DHCP4C_TIMERS; ix -- > 0; )
|
---|
955 | ct->timers[ix] = 0;
|
---|
956 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
957 | }
|
---|
958 |
|
---|
959 |
|
---|
960 | /*
|
---|
961 | * start_select -- DISCOVER ã¡ãã»ã¼ã¸ãéä¿¡ã㦠SELECT ç¶æ
|
---|
962 | ã«é·ç§»ããã
|
---|
963 | */
|
---|
964 |
|
---|
965 | static void
|
---|
966 | start_select (T_DHCP4_CLI_CONTEXT *ct)
|
---|
967 | {
|
---|
968 | /* éä¿¡ãéå§ããæéãè¨é²ããã*/
|
---|
969 | get_tim(&ct->snd_start);
|
---|
970 |
|
---|
971 | /* OFFER ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ã ã¢ã¦ããè¨å®ããã*/
|
---|
972 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
973 | ct->timers[DHCP4C_TIMER_RCV_OFFER] = SYSTIM2TIMER(TMO_DHCP4C_OFFER);
|
---|
974 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
975 |
|
---|
976 | /* éä¿¡å
|
---|
977 | ã¢ãã¬ã¹ã«ãã¼ã«ã«ãããã¯ã¼ã¯ã¸ã®ããã¼ããã£ã¹ããè¨å®ããã*/
|
---|
978 | ct->dst.ipaddr = IPV4_ADDRBROADCAST;
|
---|
979 |
|
---|
980 | /* éä¿¡ééããªã»ããããã*/
|
---|
981 | ct->interval = 0;
|
---|
982 |
|
---|
983 | /* SELECT ç¶æ
|
---|
984 | ã«é·ç§»ããã*/
|
---|
985 | ct->fsm = DHCP4_FSM_SELECT;
|
---|
986 |
|
---|
987 | /* DISCOVER ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
|
---|
988 | ct->flags |= DHCP4C_FLAG_TMOUT_SND_DISC;
|
---|
989 | syscall(sig_sem(SEM_DHCP4_CLI_READY));
|
---|
990 | }
|
---|
991 |
|
---|
992 | /*
|
---|
993 | * start_bound -- BOUND ç¶æ
|
---|
994 | ã«é·ç§»ããã
|
---|
995 | */
|
---|
996 |
|
---|
997 | static void
|
---|
998 | start_bound (T_DHCP4_CLI_CONTEXT *ct)
|
---|
999 | {
|
---|
1000 | /* ãªã¼ã¹ãããæ
|
---|
1001 | å ±ãè¨å®ããã*/
|
---|
1002 | set_lease(ct);
|
---|
1003 |
|
---|
1004 | /* ãªã¼ã¹ã®æå¹æéãè¨å®ããã*/
|
---|
1005 | ct->expire = timer_value(ct->act_msg, DHCP4_OPT_LEASE_TIME, TMO_DHCP4C_DEF_LEASE);
|
---|
1006 |
|
---|
1007 | /* ãªã¼ã¹æéã TMO_DHCP4C_MIN_LEASE 以ä¸ã®æã¯ãTMO_DHCP4C_MIN_LEASE ã«è¨å®ããã*/
|
---|
1008 | if (ct->expire < TMO_DHCP4C_MIN_LEASE)
|
---|
1009 | ct->expire = TMO_DHCP4C_MIN_LEASE;
|
---|
1010 |
|
---|
1011 | /* RENEW ç¶æ
|
---|
1012 | ã¾ã§ã®æéãè¨å®ããã*/
|
---|
1013 | ct->renew = timer_value(ct->act_msg, DHCP4_OPT_RENEWAL_T1, ct->expire / 2);
|
---|
1014 |
|
---|
1015 | /* REBIND ç¶æ
|
---|
1016 | ã¾ã§ã®æéãè¨å®ããã*/
|
---|
1017 | ct->rebind = timer_value(ct->act_msg, DHCP4_OPT_REBINDING_T2, ct->renew + ct->renew / 2 + ct->renew / 4);
|
---|
1018 |
|
---|
1019 | /* EXPIREãRENEWãREBIND ã¿ã¤ãã¼ãè¨å®ããã*/
|
---|
1020 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1021 | ct->timers[DHCP4C_TIMER_EXPIRE] = SYSTIM2TIMER(ct->expire); /* EXPIRE ã¾ã§ã®æé */
|
---|
1022 | ct->timers[DHCP4C_TIMER_REBIND] = SYSTIM2TIMER(ct->rebind); /* REBINDï¼T2ï¼ã¾ã§ã®æé */
|
---|
1023 | ct->timers[DHCP4C_TIMER_RENEW] = SYSTIM2TIMER(ct->renew); /* RENEW ï¼T1ï¼ã¾ã§ã®æé */
|
---|
1024 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1025 |
|
---|
1026 | /* BOUND ç¶æ
|
---|
1027 | ã«é·ç§»ããã*/
|
---|
1028 | ct->fsm = DHCP4_FSM_BOUND;
|
---|
1029 |
|
---|
1030 | /* BIND ãéå§ããæéãè¨é²ããã*/
|
---|
1031 | get_tim(&ct->bind_start);
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | /*
|
---|
1035 | * start_send_request -- REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ãã¦ãæå®ãããç¶æ
|
---|
1036 | ã«é·ç§»ããã
|
---|
1037 | */
|
---|
1038 |
|
---|
1039 | static void
|
---|
1040 | start_send_request (T_DHCP4_CLI_CONTEXT *ct, uint_t fsm)
|
---|
1041 | {
|
---|
1042 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
1043 |
|
---|
1044 | /* éä¿¡ãéå§ããæéãè¨é²ããã*/
|
---|
1045 | get_tim(&ct->snd_start);
|
---|
1046 |
|
---|
1047 | /* é·ç§»ããç¶æ
|
---|
1048 | ã RENEW 以å¤ã§ã¯ ACK/NAK ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ã ã¢ã¦ããè¨å®ããã*/
|
---|
1049 | if (fsm != DHCP4_FSM_RENEW) {
|
---|
1050 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1051 | ct->timers[DHCP4C_TIMER_RCV_ACK] = SYSTIM2TIMER(TMO_DHCP4C_ACK);
|
---|
1052 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | /*
|
---|
1056 | * é·ç§»ããç¶æ
|
---|
1057 | ã RENEW ã§ã
|
---|
1058 | * SERVER ID ãªãã·ã§ã³ãããã°ãã¼ã«ã«ãããã¯ã¼ã¯ã¸ã®ããã¼ããã£ã¹ããéä¿¡ããã
|
---|
1059 | */
|
---|
1060 | if ((fsm == DHCP4_FSM_RENEW) &&
|
---|
1061 | (find_option(ct->act_msg, DHCP4_OPT_SERVER_ID, sizeof(T_IN4_ADDR)) != NULL))
|
---|
1062 | ct->dst.ipaddr = (ifp->in4_ifaddr.addr & ifp->in4_ifaddr.mask) | ~ifp->in4_ifaddr.mask;
|
---|
1063 | else
|
---|
1064 | ct->dst.ipaddr = IPV4_ADDRBROADCAST;
|
---|
1065 |
|
---|
1066 | /* éä¿¡ééããªã»ããããã*/
|
---|
1067 | ct->interval = 0;
|
---|
1068 |
|
---|
1069 | /* æå®ãããç¶æ
|
---|
1070 | ã«é·ç§»ããã*/
|
---|
1071 | ct->fsm = fsm;
|
---|
1072 |
|
---|
1073 | /* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
|
---|
1074 | ct->flags |= DHCP4C_FLAG_TMOUT_SND_REQ;
|
---|
1075 | syscall(sig_sem(SEM_DHCP4_CLI_READY));
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | /*
|
---|
1079 | * start_rel_info -- ã¢ãã¬ã¹æ
|
---|
1080 | å ±ã解æ¾ããã
|
---|
1081 | *
|
---|
1082 | * 注æ: DHCPv4 ã® RFC2131 ã«ã¯ç¡ã
|
---|
1083 | */
|
---|
1084 |
|
---|
1085 | static void
|
---|
1086 | start_rel_info (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1087 | {
|
---|
1088 | /* å
|
---|
1089 | ¨ã¦ã® TIMER ããã£ã³ã»ã«ããã*/
|
---|
1090 | cancel_all_timers(ct);
|
---|
1091 |
|
---|
1092 | /* æå¹ãªãªã¼ã¹æ
|
---|
1093 | å ±ãç¡ããã° SLEEP ã«é·ç§»ããã*/
|
---|
1094 | if (ct->act_msg == NULL) {
|
---|
1095 | ct->fsm = DHCP4_FSM_SLEEP;
|
---|
1096 | return;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | /* éä¿¡å
|
---|
1100 | ã¢ãã¬ã¹ã«ãã¼ã«ã«ãããã¯ã¼ã¯ã¸ã®ããã¼ããã£ã¹ããè¨å®ããã*/
|
---|
1101 | ct->dst.ipaddr = IPV4_ADDRBROADCAST;
|
---|
1102 |
|
---|
1103 | /* RELEASE ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
|
---|
1104 | ct->flags |= DHCP4C_FLAG_TMOUT_SND_REL;
|
---|
1105 | syscall(sig_sem(SEM_DHCP4_CLI_READY));
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /*
|
---|
1109 | * setup_dhcp_com_msg -- DHCP ã¡ãã»ã¼ã¸ã®å
|
---|
1110 | ±éé¨åãä½æããã
|
---|
1111 | */
|
---|
1112 |
|
---|
1113 | static uint8_t *
|
---|
1114 | setup_dhcp_com_msg (T_DHCP4_CLI_CONTEXT *ct, uint_t op, uint_t type)
|
---|
1115 | {
|
---|
1116 | uint8_t *option;
|
---|
1117 |
|
---|
1118 | memset((void*)&ct->snd_msg->msg, sizeof(ct->snd_msg->msg), 0);
|
---|
1119 | ct->snd_msg->len = sizeof(ct->snd_msg->msg);
|
---|
1120 |
|
---|
1121 | /* ãããé¨ãè¨å®ããã*/
|
---|
1122 | ct->snd_msg->msg.op = op;
|
---|
1123 | ct->snd_msg->msg.htype = DHCP4_HWA_TYPE_ETHER;
|
---|
1124 | ct->snd_msg->msg.hlen = ETHER_ADDR_LEN;
|
---|
1125 | HOST2MSG32(ct->snd_msg->msg.xid, ct->xid);
|
---|
1126 |
|
---|
1127 | /* MAC ã¢ãã¬ã¹ãè¨å®ããã*/
|
---|
1128 | memcpy(&ct->snd_msg->msg.chaddr, &ct->sc->ifaddr.lladdr, ETHER_ADDR_LEN);
|
---|
1129 |
|
---|
1130 | /*
|
---|
1131 | * ãªãã·ã§ã³é¨ãè¨å®ããã
|
---|
1132 | */
|
---|
1133 |
|
---|
1134 | /* RFC1048 ã«å®ç¾©ããã¦ãããã¸ãã¯çªå·ãè¨å®ããã*/
|
---|
1135 | memcpy(&ct->snd_msg->msg.options, magicno, DHCP4_MAGIC_LEN);
|
---|
1136 | option = ct->snd_msg->msg.options + DHCP4_MAGIC_LEN;
|
---|
1137 |
|
---|
1138 | /* DHCP ã¡ãã»ã¼ã¸ã¿ã¤ããè¨å®ããã*/
|
---|
1139 | *(option ++) = DHCP4_OPT_MSG_TYPE;
|
---|
1140 | *(option ++) = 1;
|
---|
1141 | *(option ++) = type;
|
---|
1142 |
|
---|
1143 | return option;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /*
|
---|
1147 | * setup_release_msg -- RELEASE ã¡ãã»ã¼ã¸ãä½æããã
|
---|
1148 | */
|
---|
1149 |
|
---|
1150 | static ER
|
---|
1151 | setup_release_msg (T_DHCP4_CLI_CONTEXT *ct, T_DHCP4_CLI_MSG *ref)
|
---|
1152 | {
|
---|
1153 | uint8_t *opt, *ref_opt;
|
---|
1154 |
|
---|
1155 | /* æå¹ãªãªã¼ã¹ã»ã¡ãã»ã¼ã¸ããªããã°ã¨ã©ã¼ */
|
---|
1156 | if (ref == NULL)
|
---|
1157 | return E_PAR;
|
---|
1158 |
|
---|
1159 | ct->xid = create_xid();
|
---|
1160 | if ((opt = setup_dhcp_com_msg(ct, DHCP4_REQUEST, DHCP4_MTYPE_RELEASE)) == NULL)
|
---|
1161 | return E_PAR;
|
---|
1162 |
|
---|
1163 | /*
|
---|
1164 | * ãµã¼ã IDï¼server-ipï¼ãè¨å®ããã
|
---|
1165 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1166 | §
|
---|
1167 | */
|
---|
1168 | if ((ref_opt = find_option(ref, DHCP4_OPT_SERVER_ID, sizeof(T_IN4_ADDR))) == NULL)
|
---|
1169 | return E_PAR;
|
---|
1170 | *(opt ++) = *(ref_opt ++);
|
---|
1171 | *(opt ++) = *(ref_opt ++);
|
---|
1172 | memcpy(opt, ref_opt, *(ref_opt - 1));
|
---|
1173 | opt += *(ref_opt - 1);
|
---|
1174 |
|
---|
1175 | /*
|
---|
1176 | * ã¯ã©ã¤ã¢ã³ã IDï¼MAC ã¢ãã¬ã¹ï¼ãè¨å®ããã
|
---|
1177 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1178 | §
|
---|
1179 | */
|
---|
1180 | *(opt ++) = DHCP4_OPT_CLIENT_ID;
|
---|
1181 | *(opt ++) = ETHER_ADDR_LEN + 1;
|
---|
1182 | *(opt ++) = DHCP4_HWA_TYPE_ETHER;
|
---|
1183 | memcpy(opt, &ct->sc->ifaddr.lladdr, ETHER_ADDR_LEN);
|
---|
1184 | opt += ETHER_ADDR_LEN;
|
---|
1185 |
|
---|
1186 | /* ãªãã·ã§ã³ã®çµäºãè¨å®ããã*/
|
---|
1187 | *(opt ++) = DHCP4_OPT_END;
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * ã¯ã©ã¤ã¢ã³ã IP ã¢ãã¬ã¹ï¼ciaddrï¼ãè¨å®ããã
|
---|
1191 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1192 | §
|
---|
1193 | */
|
---|
1194 | HOST2MSG32(ct->snd_msg->msg.ciaddr, IF_GET_IFNET()->in4_ifaddr.addr);
|
---|
1195 |
|
---|
1196 | /* ã¡ãã»ã¼ã¸é·ãè¨å®ããã*/
|
---|
1197 | ct->snd_msg->len = sizeof(ct->snd_msg->msg) - DHCP4_DFLT_OPT_LEN
|
---|
1198 | + (opt - (uint8_t*)&ct->snd_msg->msg.options);
|
---|
1199 | return E_OK;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 |
|
---|
1203 | /*
|
---|
1204 | * setup_request_msg -- REQUEST ã¡ãã»ã¼ã¸ãä½æããã
|
---|
1205 | */
|
---|
1206 |
|
---|
1207 | static ER
|
---|
1208 | setup_request_msg (T_DHCP4_CLI_CONTEXT *ct, T_DHCP4_CLI_MSG *ref)
|
---|
1209 | {
|
---|
1210 | uint8_t *opt, *ref_opt;
|
---|
1211 |
|
---|
1212 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) || defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
1213 | uint_t len = 0;
|
---|
1214 | #endif
|
---|
1215 |
|
---|
1216 | /* åç
|
---|
1217 | §ããã¡ãã»ã¼ã¸ããªããã°ã¨ã©ã¼ */
|
---|
1218 | if (ref == NULL)
|
---|
1219 | return E_PAR;
|
---|
1220 |
|
---|
1221 | ct->xid = create_xid();
|
---|
1222 | if ((opt = setup_dhcp_com_msg(ct, DHCP4_REQUEST, DHCP4_MTYPE_REQUEST)) == NULL)
|
---|
1223 | return E_PAR;
|
---|
1224 |
|
---|
1225 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) || defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
1226 |
|
---|
1227 | /* 追å ã®ãªãã·ã§ã³ãè¨å®ããã*/
|
---|
1228 |
|
---|
1229 | *opt = DHCP4_OPT_REQ_PARAM;
|
---|
1230 | opt += 2;
|
---|
1231 |
|
---|
1232 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST)
|
---|
1233 |
|
---|
1234 | /* è¦æ±ãªãã·ã§ã³ãªã¹ããè¨å®ããã*/
|
---|
1235 | if (sizeof(required_plist) > 0) {
|
---|
1236 | memcpy(opt, required_plist, sizeof(required_plist));
|
---|
1237 | opt += sizeof(required_plist);
|
---|
1238 | len += sizeof(required_plist);
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) */
|
---|
1242 |
|
---|
1243 | #if defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
1244 |
|
---|
1245 | /* è¦æ±ãªãã·ã§ã³ãªã¹ããè¨å®ããã*/
|
---|
1246 | if (sizeof(request_plist) > 0) {
|
---|
1247 | memcpy(opt, request_plist, sizeof(request_plist));
|
---|
1248 | opt += sizeof(request_plist);
|
---|
1249 | len += sizeof(request_plist);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUEST_OLIST) */
|
---|
1253 |
|
---|
1254 | *(opt - len - 1) = len;
|
---|
1255 |
|
---|
1256 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) || defined(DHCP4_CLI_CFG_REQUEST_OLIST) */
|
---|
1257 |
|
---|
1258 | /*
|
---|
1259 | * ãµã¼ã IDï¼server-ipï¼ãè¨å®ããã
|
---|
1260 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1261 | §
|
---|
1262 | */
|
---|
1263 | if (ct->fsm == DHCP4_FSM_REQUEST) {
|
---|
1264 | if ((ref_opt = find_option(ref, DHCP4_OPT_SERVER_ID, sizeof(T_IN4_ADDR))) == NULL)
|
---|
1265 | return E_PAR;
|
---|
1266 | *(opt ++) = *(ref_opt ++);
|
---|
1267 | *(opt ++) = *(ref_opt ++);
|
---|
1268 | memcpy(opt, ref_opt, *(ref_opt - 1));
|
---|
1269 | opt += *(ref_opt - 1);
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | /*
|
---|
1273 | * è¦æ± IP ã¢ãã¬ã¹ï¼requested-ipï¼ãè¨å®ããã
|
---|
1274 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1275 | §
|
---|
1276 | * RFC2131 ã§ã¯ãDHCP4_FSM_RENEW ã®æ MUST NOT ã«ãªã£ã¦ãããã
|
---|
1277 | * MUST ã®ããã§ããã
|
---|
1278 | */
|
---|
1279 | *(opt ++) = DHCP4_OPT_REQ_IPV4;
|
---|
1280 | *(opt ++) = sizeof(ref->msg.yiaddr);
|
---|
1281 | if (ct->fsm == DHCP4_FSM_REQUEST || ct->fsm == DHCP4_FSM_INIT_REBOOT ||
|
---|
1282 | ct->fsm == DHCP4_FSM_RENEW || ct->fsm == DHCP4_FSM_REBIND)
|
---|
1283 | /* åç
|
---|
1284 | §ã¡ãã»ã¼ã¸ã¨åã IP ã¢ãã¬ã¹ï¼requested-ipï¼ãè¦æ±ããã*/
|
---|
1285 | memcpy(opt, (uint8_t*)&ref->msg.yiaddr, sizeof(ref->msg.yiaddr));
|
---|
1286 | else
|
---|
1287 | host2msg32(opt, IPV4_ADDRANY);
|
---|
1288 | opt += sizeof(ref->msg.yiaddr);
|
---|
1289 |
|
---|
1290 | /*
|
---|
1291 | * ã¯ã©ã¤ã¢ã³ã IDï¼MAC ã¢ãã¬ã¹ï¼ãè¨å®ããã
|
---|
1292 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1293 | §
|
---|
1294 | */
|
---|
1295 | *(opt ++) = DHCP4_OPT_CLIENT_ID;
|
---|
1296 | *(opt ++) = ETHER_ADDR_LEN + 1;
|
---|
1297 | *(opt ++) = DHCP4_HWA_TYPE_ETHER;
|
---|
1298 | memcpy(opt, &ct->sc->ifaddr.lladdr, ETHER_ADDR_LEN);
|
---|
1299 | opt += ETHER_ADDR_LEN;
|
---|
1300 |
|
---|
1301 | /* ãªãã·ã§ã³ã®çµäºãè¨å®ããã*/
|
---|
1302 | *(opt ++) = DHCP4_OPT_END;
|
---|
1303 |
|
---|
1304 | /*
|
---|
1305 | * ã¯ã©ã¤ã¢ã³ã IP ã¢ãã¬ã¹ï¼ciaddrï¼ãè¨å®ããã
|
---|
1306 | * RFC2131 4.4.1 Initialization and allocation of network address åç
|
---|
1307 | §
|
---|
1308 | */
|
---|
1309 | if (ct->fsm == DHCP4_FSM_RENEW || ct->fsm == DHCP4_FSM_REBIND)
|
---|
1310 | HOST2MSG32(ct->snd_msg->msg.ciaddr, IF_GET_IFNET()->in4_ifaddr.addr);
|
---|
1311 | else
|
---|
1312 | HOST2MSG32(ct->snd_msg->msg.ciaddr, IPV4_ADDRANY);
|
---|
1313 |
|
---|
1314 | /* ã¡ãã»ã¼ã¸é·ãè¨å®ããã*/
|
---|
1315 | ct->snd_msg->len = sizeof(ct->snd_msg->msg) - DHCP4_DFLT_OPT_LEN
|
---|
1316 | + (opt - (uint8_t*)&ct->snd_msg->msg.options);
|
---|
1317 | return E_OK;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | /*
|
---|
1321 | * setup_discover_msg -- DISCOVER ã¡ãã»ã¼ã¸ãä½æããã
|
---|
1322 | */
|
---|
1323 |
|
---|
1324 | static ER
|
---|
1325 | setup_discover_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1326 | {
|
---|
1327 | uint8_t *opt;
|
---|
1328 |
|
---|
1329 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) || defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
1330 | uint_t len = 0;
|
---|
1331 | #endif
|
---|
1332 |
|
---|
1333 | ct->xid = create_xid();
|
---|
1334 | if ((opt = setup_dhcp_com_msg(ct, DHCP4_REQUEST, DHCP4_MTYPE_DISCOVER)) == NULL)
|
---|
1335 | return E_PAR;
|
---|
1336 |
|
---|
1337 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) || defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
1338 |
|
---|
1339 | /* 追å ã®ãªãã·ã§ã³ãè¨å®ããã*/
|
---|
1340 |
|
---|
1341 | *opt = DHCP4_OPT_REQ_PARAM;
|
---|
1342 | opt += 2;
|
---|
1343 |
|
---|
1344 | #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST)
|
---|
1345 |
|
---|
1346 | /* è¦æ±ãªãã·ã§ã³ãªã¹ããè¨å®ããã*/
|
---|
1347 | if (sizeof(required_plist) > 0) {
|
---|
1348 | memcpy(opt, required_plist, sizeof(required_plist));
|
---|
1349 | opt += sizeof(required_plist);
|
---|
1350 | len += sizeof(required_plist);
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) */
|
---|
1354 |
|
---|
1355 | #if defined(DHCP4_CLI_CFG_REQUEST_OLIST)
|
---|
1356 |
|
---|
1357 | /* è¦æ±ãªãã·ã§ã³ãªã¹ããè¨å®ããã*/
|
---|
1358 | if (sizeof(request_plist) > 0) {
|
---|
1359 | memcpy(opt, request_plist, sizeof(request_plist));
|
---|
1360 | opt += sizeof(request_plist);
|
---|
1361 | len += sizeof(request_plist);
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUEST_OLIST) */
|
---|
1365 |
|
---|
1366 | *(opt - len - 1) = len;
|
---|
1367 |
|
---|
1368 | #endif /* of #if defined(DHCP4_CLI_CFG_REQUIRED_OLIST) || defined(DHCP4_CLI_CFG_REQUEST_OLIST) */
|
---|
1369 |
|
---|
1370 | /* æ¢ã«ã¢ãã¬ã¹æ
|
---|
1371 | å ±ããªã¼ã¹ããã¦ããã°ãåã IP ã¢ãã¬ã¹ãè¦æ±ããã*/
|
---|
1372 | if (ct->act_msg != NULL) {
|
---|
1373 | *(opt ++) = DHCP4_OPT_REQ_IPV4;
|
---|
1374 | *(opt ++) = sizeof(ct->act_msg->msg.yiaddr);
|
---|
1375 | memcpy(opt, (uint8_t*)&ct->act_msg->msg.yiaddr, sizeof(ct->act_msg->msg.yiaddr));
|
---|
1376 | opt += sizeof(ct->act_msg->msg.yiaddr);
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | /* ã¯ã©ã¤ã¢ã³ã IDï¼MAC ã¢ãã¬ã¹ï¼ãè¨å®ããã*/
|
---|
1380 | *(opt ++) = DHCP4_OPT_CLIENT_ID;
|
---|
1381 | *(opt ++) = ETHER_ADDR_LEN + 1;
|
---|
1382 | *(opt ++) = DHCP4_HWA_TYPE_ETHER;
|
---|
1383 | memcpy(opt, &ct->sc->ifaddr.lladdr, ETHER_ADDR_LEN);
|
---|
1384 | opt += ETHER_ADDR_LEN;
|
---|
1385 |
|
---|
1386 | /* ãªãã·ã§ã³ã®çµäºãè¨å®ããã*/
|
---|
1387 | *(opt ++) = DHCP4_OPT_END;
|
---|
1388 |
|
---|
1389 | /* éä¿¡å
|
---|
1390 | ã¢ãã¬ã¹ãè¨å®ããã*/
|
---|
1391 | ct->dst.ipaddr = IPV4_ADDRBROADCAST;
|
---|
1392 |
|
---|
1393 | /* ã¡ãã»ã¼ã¸é·ãè¨å®ããã*/
|
---|
1394 | ct->snd_msg->len = sizeof(ct->snd_msg->msg) - DHCP4_DFLT_OPT_LEN
|
---|
1395 | + (opt - (uint8_t*)&ct->snd_msg->msg.options);
|
---|
1396 | return E_OK;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | /*
|
---|
1400 | * ack_msg_timeout -- ACK/NAK ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ã ã¢ã¦ãå¦ç
|
---|
1401 | */
|
---|
1402 |
|
---|
1403 | static void
|
---|
1404 | ack_msg_timeout (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1405 | {
|
---|
1406 | /* ACK ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ããã°ãBOUND ç¶æ
|
---|
1407 | ã«é·ç§»ããã*/
|
---|
1408 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1409 | if (select_ack(ct)) {
|
---|
1410 |
|
---|
1411 | /* REQUEST ã¡ãã»ã¼ã¸éä¿¡ã¿ã¤ãã¼ãåæ¢ããã*/
|
---|
1412 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1413 | ct->timers[DHCP4C_TIMER_SND_REQ] = 0;
|
---|
1414 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1415 |
|
---|
1416 | /*
|
---|
1417 | * ç¶æ
|
---|
1418 | ã REQUEST ã®æã¯ã
|
---|
1419 | * OFFER ã¡ãã»ã¼ã¸ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã
|
---|
1420 | */
|
---|
1421 | if (ct->fsm == DHCP4_FSM_REQUEST && ct->off_msg != NULL) {
|
---|
1422 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
1423 | ct->off_msg->next = ct->rcv_lst;
|
---|
1424 | ct->rcv_lst = ct->off_msg;
|
---|
1425 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
1426 | ct->off_msg = NULL;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | /* BOUND ç¶æ
|
---|
1430 | ã«é·ç§»ããã*/
|
---|
1431 | start_bound(ct);
|
---|
1432 | return;
|
---|
1433 | }
|
---|
1434 | else {
|
---|
1435 |
|
---|
1436 | /* ãªã¼ã¹æ
|
---|
1437 | å ±ã解æ¾ããã*/
|
---|
1438 | rel_lease(ct);
|
---|
1439 |
|
---|
1440 | ct->error = E_TMOUT;
|
---|
1441 | }
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | /*
|
---|
1445 | * send_request_msg -- REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ããã
|
---|
1446 | */
|
---|
1447 |
|
---|
1448 | static void
|
---|
1449 | send_request_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1450 | {
|
---|
1451 | T_DHCP4_CLI_MSG *ref;
|
---|
1452 | ER error;
|
---|
1453 | SYSTIM time;
|
---|
1454 | RELTIM interval;
|
---|
1455 | uint16_t secs;
|
---|
1456 |
|
---|
1457 | /* ã¿ã¤ã ã¢ã¦ããã©ã°ãã¯ãªã¢ã¼ããã*/
|
---|
1458 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1459 |
|
---|
1460 | /* ACK ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ããã°ãBOUND ç¶æ
|
---|
1461 | ã«é·ç§»ããã*/
|
---|
1462 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1463 | if (select_ack(ct)) {
|
---|
1464 |
|
---|
1465 | /* ACK/NAK ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ãã¼ãåæ¢ããã*/
|
---|
1466 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1467 | ct->timers[DHCP4C_TIMER_RCV_ACK] = 0;
|
---|
1468 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1469 |
|
---|
1470 | /*
|
---|
1471 | * ç¶æ
|
---|
1472 | ã REQUEST ã®æã¯ã
|
---|
1473 | * OFFER ã¡ãã»ã¼ã¸ãåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã«æ»ãã
|
---|
1474 | */
|
---|
1475 | if (ct->fsm == DHCP4_FSM_REQUEST && ct->off_msg != NULL) {
|
---|
1476 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
1477 | ct->off_msg->next = ct->rcv_lst;
|
---|
1478 | ct->rcv_lst = ct->off_msg;
|
---|
1479 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
1480 | ct->off_msg = NULL;
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | /* BOUND ç¶æ
|
---|
1484 | ã«é·ç§»ããã*/
|
---|
1485 | start_bound(ct);
|
---|
1486 | return;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | /* REQUEST ã¡ãã»ã¼ã¸ãä½æããã*/
|
---|
1490 | if (ct->fsm == DHCP4_FSM_REQUEST)
|
---|
1491 | ref = ct->off_msg;
|
---|
1492 | else
|
---|
1493 | ref = ct->act_msg;
|
---|
1494 | if ((error = setup_request_msg(ct, ref)) != E_OK) {
|
---|
1495 | syslog(LOG_NOTICE, "[DHCP4C] error, setup request message: %s", itron_strerror(error));
|
---|
1496 | ct->error = error;
|
---|
1497 | return;
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | /* éä¿¡ãéå§ãã¦ããã®æéãå¾ãã*/
|
---|
1501 | get_tim(&time);
|
---|
1502 | interval = time - ct->snd_start;
|
---|
1503 |
|
---|
1504 | /*
|
---|
1505 | * éä¿¡ãéå§ãã¦ããã®æéï¼ç§ï¼ãã¡ãã»ã¼ã¸ã«è¨å®ããã
|
---|
1506 | * æ大å¤ã¯ 65535ã
|
---|
1507 | */
|
---|
1508 | if (ct->fsm == DHCP4_FSM_REQUEST)
|
---|
1509 | secs = ct->secs;
|
---|
1510 | else {
|
---|
1511 | if (SYSTIM2SEC(interval) < 65536)
|
---|
1512 | secs = SYSTIM2SEC(interval);
|
---|
1513 | else
|
---|
1514 | secs = 65535;
|
---|
1515 | }
|
---|
1516 | HOST2MSG16(ct->snd_msg->msg.secs, secs);
|
---|
1517 |
|
---|
1518 | /* ãã³ããããã³ã°ã³ã¼ã«ã§ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
|
---|
1519 | error = udp_snd_dat(ct->cepid, &ct->dst, &ct->snd_msg->msg, ct->snd_msg->len, TMO_NBLK);
|
---|
1520 | if (error != E_WBLK) {
|
---|
1521 | syslog(LOG_NOTICE, "[DHCP4C] error, send request message: %s", itron_strerror(error));
|
---|
1522 | ct->error = error;
|
---|
1523 | return;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | /* éä¿¡ééãè¨å®ããã*/
|
---|
1527 | if (ct->interval == 0)
|
---|
1528 | /* æªè¨å® */
|
---|
1529 | ct->interval = TMO_DHCP4C_INIT_INTVL;
|
---|
1530 | else
|
---|
1531 | /* ããã¯ãªãã¢ã«ã´ãªãºã 㧠2åã«ããï¼ä¹±æ°ä½¿ç¨ï¼éä¿¡ééãå ç®ããã*/
|
---|
1532 | ct->interval += (netapp_rand() >> 2) % (ct->interval << 1);
|
---|
1533 |
|
---|
1534 | /* éä¿¡å¨æã®å¶éå¤ãè¶
|
---|
1535 | ããªãããã«ããã*/
|
---|
1536 | if (ct->interval > TMO_DHCP4C_MAX_BACKOFF)
|
---|
1537 | ct->interval = (TMO_DHCP4C_MAX_BACKOFF >> 1) + ((netapp_rand() >> 2) % TMO_DHCP4C_MAX_BACKOFF);
|
---|
1538 |
|
---|
1539 | /* ãªã¼ã¹æéã®ã¿ã¤ã ã¢ã¦ããå
|
---|
1540 | éãã */
|
---|
1541 | if (ct->fsm != DHCP4_FSM_REQUEST &&
|
---|
1542 | TIMEC_GT(ct->interval + time, ct->expire + ct->bind_start))
|
---|
1543 | ct->interval = ct->expire + ct->bind_start - time + SYSTIM_HZ;
|
---|
1544 |
|
---|
1545 | /* åéä¿¡ã¿ã¤ã ã¢ã¦ããè¨å®ããã*/
|
---|
1546 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1547 | ct->timers[DHCP4C_TIMER_SND_REQ] = SYSTIM2TIMER(ct->interval);
|
---|
1548 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | /*
|
---|
1552 | * send_release_msg -- RELEASE ã¡ãã»ã¼ã¸ãéä¿¡ããã
|
---|
1553 | */
|
---|
1554 |
|
---|
1555 | static void
|
---|
1556 | send_release_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1557 | {
|
---|
1558 | ER error;
|
---|
1559 |
|
---|
1560 | /* ã¿ã¤ã ã¢ã¦ããã©ã°ãã¯ãªã¢ã¼ããã*/
|
---|
1561 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1562 |
|
---|
1563 | /* RELEASE ã¡ãã»ã¼ã¸ãä½æããã*/
|
---|
1564 | if ((error = setup_release_msg(ct, ct->act_msg)) != E_OK) {
|
---|
1565 | syslog(LOG_NOTICE, "[DHCP4C] error, setup release message: %s", itron_strerror(error));
|
---|
1566 | ct->error = error;
|
---|
1567 | return;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | /* ãªã¼ã¹æ
|
---|
1571 | å ±ã解æ¾ããã*/
|
---|
1572 | rel_lease(ct);
|
---|
1573 |
|
---|
1574 | /* ãã³ããããã³ã°ã³ã¼ã«ã§ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
|
---|
1575 | error = udp_snd_dat(ct->cepid, &ct->dst, &ct->snd_msg->msg, ct->snd_msg->len, TMO_NBLK);
|
---|
1576 | if (error != E_WBLK) {
|
---|
1577 | syslog(LOG_NOTICE, "[DHCP4C] error, send release message: %s", itron_strerror(error));
|
---|
1578 | ct->error = error;
|
---|
1579 | return;
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | /* ã¡ãã»ã¼ã¸éä¿¡å¾
|
---|
1583 | ã¡ãè¡ãã*/
|
---|
1584 | syscall(wai_sem(SEM_DHCP4_CLI_READY));
|
---|
1585 |
|
---|
1586 | /* SLEEP ç¶æ
|
---|
1587 | ã«é·ç§»ããã*/
|
---|
1588 | ct->fsm = DHCP4_FSM_SLEEP;
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | /*
|
---|
1592 | * offer_msg_timeout -- OFFER ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ã ã¢ã¦ãå¦ç
|
---|
1593 | */
|
---|
1594 |
|
---|
1595 | static void
|
---|
1596 | offer_msg_timeout (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1597 | {
|
---|
1598 | /* OFFER ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ããã°ãREQUEST ç¶æ
|
---|
1599 | ã«é·ç§»ããã*/
|
---|
1600 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1601 | if (select_offer(ct)) {
|
---|
1602 |
|
---|
1603 | /* DISCOVER ã¡ãã»ã¼ã¸éä¿¡ã¿ã¤ãã¼ãåæ¢ããã*/
|
---|
1604 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1605 | ct->timers[DHCP4C_TIMER_SND_DISC] = 0;
|
---|
1606 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1607 |
|
---|
1608 | /* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ã㦠REQUEST ç¶æ
|
---|
1609 | ã«é·ç§»ããã*/
|
---|
1610 | start_send_request(ct, DHCP4_FSM_REQUEST);
|
---|
1611 | }
|
---|
1612 | else
|
---|
1613 | ct->error = E_TMOUT;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | /*
|
---|
1617 | * send_discover_msg -- DISCOVERã¡ãã»ã¼ã¸ãéä¿¡ããã
|
---|
1618 | */
|
---|
1619 |
|
---|
1620 | static void
|
---|
1621 | send_discover_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1622 | {
|
---|
1623 | ER error;
|
---|
1624 | SYSTIM time;
|
---|
1625 | RELTIM interval;
|
---|
1626 |
|
---|
1627 | /* ã¿ã¤ã ã¢ã¦ããã©ã°ãã¯ãªã¢ã¼ããã*/
|
---|
1628 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1629 |
|
---|
1630 | /* OFFER ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ããã°ãREQUEST ç¶æ
|
---|
1631 | ã«é·ç§»ããã*/
|
---|
1632 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1633 | if (select_offer(ct)) {
|
---|
1634 |
|
---|
1635 | /* OFFER ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ãã¼ãåæ¢ããã*/
|
---|
1636 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1637 | ct->timers[DHCP4C_TIMER_RCV_OFFER] = 0;
|
---|
1638 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1639 |
|
---|
1640 | /* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ã㦠REQUEST ç¶æ
|
---|
1641 | ã«é·ç§»ããã*/
|
---|
1642 | start_send_request(ct, DHCP4_FSM_REQUEST);
|
---|
1643 | return;
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | /* DISCOVER ã¡ãã»ã¼ã¸ãä½æããã*/
|
---|
1647 | if ((error = setup_discover_msg(ct)) != E_OK) {
|
---|
1648 | syslog(LOG_NOTICE, "[DHCP4C] error, setup discover message: %s", itron_strerror(error));
|
---|
1649 | ct->error = error;
|
---|
1650 | return;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | /* éä¿¡ãéå§ãã¦ããã®æéãå¾ãã*/
|
---|
1654 | get_tim(&time);
|
---|
1655 | interval = time - ct->snd_start;
|
---|
1656 |
|
---|
1657 | /*
|
---|
1658 | * éä¿¡ãéå§ãã¦ããã®æéï¼ç§ï¼ãã¡ãã»ã¼ã¸ã«è¨å®ããã
|
---|
1659 | * æ大å¤ã¯ 65535ã
|
---|
1660 | */
|
---|
1661 | if (SYSTIM2SEC(interval) < 65536)
|
---|
1662 | ct->secs = SYSTIM2SEC(interval);
|
---|
1663 | else
|
---|
1664 | ct->secs = 65535;
|
---|
1665 | HOST2MSG16(ct->snd_msg->msg.secs, ct->secs);
|
---|
1666 |
|
---|
1667 | /* ãã³ããããã³ã°ã³ã¼ã«ã§ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
|
---|
1668 | error = udp_snd_dat(ct->cepid, &ct->dst, &ct->snd_msg->msg, ct->snd_msg->len, TMO_NBLK);
|
---|
1669 | if (error != E_WBLK) {
|
---|
1670 | syslog(LOG_NOTICE, "[DHCP4C] error, send discover message: %s", itron_strerror(error));
|
---|
1671 | ct->error = error;
|
---|
1672 | return;
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | /* éä¿¡ééãè¨å®ããã*/
|
---|
1676 | if (ct->interval == 0)
|
---|
1677 | /* æªè¨å® */
|
---|
1678 | ct->interval = TMO_DHCP4C_INIT_INTVL;
|
---|
1679 | else
|
---|
1680 | /* ããã¯ãªãã¢ã«ã´ãªãºã 㧠2åã«ããï¼ä¹±æ°ä½¿ç¨ï¼éä¿¡ééãå ç®ããã*/
|
---|
1681 | ct->interval += (netapp_rand() >> 2) % (ct->interval << 1);
|
---|
1682 |
|
---|
1683 | /* éä¿¡å¨æã®å¶éå¤ãè¶
|
---|
1684 | ããªãããã«ããã*/
|
---|
1685 | if (ct->interval > TMO_DHCP4C_MAX_BACKOFF)
|
---|
1686 | ct->interval = (TMO_DHCP4C_MAX_BACKOFF >> 1) + ((netapp_rand() >> 2) % TMO_DHCP4C_MAX_BACKOFF);
|
---|
1687 |
|
---|
1688 | /* OFFER ã¡ãã»ã¼ã¸ã®åä¿¡ã¿ã¤ã ã¢ã¦ããè¶
|
---|
1689 | ããªãããã«èª¿æ´ããã*/
|
---|
1690 | if (TIMEC_GT(time + ct->interval, ct->snd_start + TMO_DHCP4C_OFFER))
|
---|
1691 | ct->interval = (TMO_DHCP4C_INIT_INTVL + TMO_DHCP4C_OFFER) - time + 1;
|
---|
1692 |
|
---|
1693 | /* åéä¿¡ã¿ã¤ã ã¢ã¦ããè¨å®ããã*/
|
---|
1694 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1695 | ct->timers[DHCP4C_TIMER_SND_DISC] = SYSTIM2TIMER(ct->interval);
|
---|
1696 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1697 | }
|
---|
1698 |
|
---|
1699 | /*
|
---|
1700 | * dhcpc_timer -- TIMER 管ç
|
---|
1701 | */
|
---|
1702 |
|
---|
1703 | static void
|
---|
1704 | dhcpc_timer (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1705 | {
|
---|
1706 | int ix;
|
---|
1707 |
|
---|
1708 | syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1709 | for (ix = NUM_DHCP4C_TIMERS; ix -- > 0; ) {
|
---|
1710 | if (ct->timers[ix] != 0) {
|
---|
1711 | ct->timers[ix] --;
|
---|
1712 | if (ct->timers[ix] == 0) {
|
---|
1713 | ct->flags = (ct->flags & ~DHCP4C_FLAG_TMOUT_MASK) | DHCP4C_FLAG_TMOUT_TIMER | ix;
|
---|
1714 | syscall(sig_sem(SEM_DHCP4_CLI_READY));
|
---|
1715 | }
|
---|
1716 | }
|
---|
1717 | }
|
---|
1718 | syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
|
---|
1719 |
|
---|
1720 | /* 1ç§æ¯ã«ã¿ã¤ã ã¢ã¦ãããã*/
|
---|
1721 | timeout((callout_func)dhcpc_timer, ct, NET_TIMER_HZ);
|
---|
1722 | }
|
---|
1723 |
|
---|
1724 | /*
|
---|
1725 | * expire_timeout -- EXPIRE ã¿ã¤ã ã¢ã¦ãå¦ç
|
---|
1726 | */
|
---|
1727 |
|
---|
1728 | static void
|
---|
1729 | expire_timeout (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1730 | {
|
---|
1731 | //ER error;
|
---|
1732 |
|
---|
1733 | /* ã¿ã¤ã ã¢ã¦ããã©ã°ãã¯ãªã¢ã¼ããã*/
|
---|
1734 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1735 |
|
---|
1736 | /* ç¶æ
|
---|
1737 | ã REQUEST ã®æã¯ä½ãããªãã*/
|
---|
1738 | if (ct->fsm == DHCP4_FSM_REQUEST)
|
---|
1739 | return;
|
---|
1740 |
|
---|
1741 | /* å
|
---|
1742 | ¨ã¦ã® TIMER ããã£ã³ã»ã«ããã*/
|
---|
1743 | cancel_all_timers(ct);
|
---|
1744 |
|
---|
1745 | /* ãªã¼ã¹ãããæ
|
---|
1746 | å ±ã解æ¾ããã*/
|
---|
1747 | rel_lease(ct);
|
---|
1748 |
|
---|
1749 | ct->error = E_TMOUT;
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | /*
|
---|
1753 | * rebind_timeout -- REBIND ã¿ã¤ã ã¢ã¦ãå¦ç
|
---|
1754 | */
|
---|
1755 |
|
---|
1756 | static void
|
---|
1757 | rebind_timeout (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1758 | {
|
---|
1759 | /* ã¿ã¤ã ã¢ã¦ããã©ã°ãã¯ãªã¢ã¼ããã*/
|
---|
1760 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1761 |
|
---|
1762 | /* å
|
---|
1763 | ¨ã¦ã® TIMER ããã£ã³ã»ã«ããã*/
|
---|
1764 | cancel_all_timers(ct);
|
---|
1765 |
|
---|
1766 | /* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ã㦠REBIND ç¶æ
|
---|
1767 | ã«é·ç§»ããã*/
|
---|
1768 | start_send_request(ct, DHCP4_FSM_REBIND);
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | /*
|
---|
1772 | * renew_timeout -- RENEW ã¿ã¤ã ã¢ã¦ãå¦ç
|
---|
1773 | */
|
---|
1774 |
|
---|
1775 | static void
|
---|
1776 | renew_timeout (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1777 | {
|
---|
1778 | /* ã¿ã¤ã ã¢ã¦ããã©ã°ãã¯ãªã¢ã¼ããã*/
|
---|
1779 | ct->flags &= ~DHCP4C_FLAG_TMOUT_MASK;
|
---|
1780 |
|
---|
1781 | /* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ã㦠RENEW ç¶æ
|
---|
1782 | ã«é·ç§»ããã*/
|
---|
1783 | start_send_request(ct, DHCP4_FSM_RENEW);
|
---|
1784 | }
|
---|
1785 |
|
---|
1786 | /*
|
---|
1787 | * dispatch_timeout -- ã¿ã¤ã ã¢ã¦ãããæã®å¦ç
|
---|
1788 | */
|
---|
1789 |
|
---|
1790 | static ER
|
---|
1791 | dispatch_timeout (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1792 | {
|
---|
1793 | switch (ct->flags & DHCP4C_FLAG_TMOUT_TIMER_MASK) {
|
---|
1794 |
|
---|
1795 | case DHCP4C_TIMER_SND_DISC:
|
---|
1796 | send_discover_msg(ct);
|
---|
1797 | break;
|
---|
1798 |
|
---|
1799 | case DHCP4C_TIMER_SND_REQ:
|
---|
1800 | send_request_msg(ct);
|
---|
1801 | break;
|
---|
1802 |
|
---|
1803 | case DHCP4C_TIMER_RCV_OFFER:
|
---|
1804 | offer_msg_timeout(ct);
|
---|
1805 | break;
|
---|
1806 |
|
---|
1807 | case DHCP4C_TIMER_RCV_ACK:
|
---|
1808 | ack_msg_timeout(ct);
|
---|
1809 | break;
|
---|
1810 |
|
---|
1811 | case DHCP4C_TIMER_RENEW:
|
---|
1812 | renew_timeout(ct);
|
---|
1813 | break;
|
---|
1814 |
|
---|
1815 | case DHCP4C_TIMER_REBIND:
|
---|
1816 | rebind_timeout(ct);
|
---|
1817 | break;
|
---|
1818 |
|
---|
1819 | case DHCP4C_TIMER_EXPIRE:
|
---|
1820 | expire_timeout(ct);
|
---|
1821 | break;
|
---|
1822 |
|
---|
1823 | case DHCP4C_TIMER_SND_REL:
|
---|
1824 | send_release_msg(ct);
|
---|
1825 | break;
|
---|
1826 |
|
---|
1827 | default:
|
---|
1828 | break;
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 | return ct->error;
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | /*
|
---|
1835 | * dispatch_event -- ã¤ãã³ãæ¯ã®å¦ç
|
---|
1836 | */
|
---|
1837 |
|
---|
1838 | static ER
|
---|
1839 | dispatch_event (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1840 | {
|
---|
1841 | ER error = E_OK;
|
---|
1842 |
|
---|
1843 | while (ct->fsm != DHCP4_FSM_SLEEP && error == E_OK) {
|
---|
1844 |
|
---|
1845 | /* ã¡ãã»ã¼ã¸ã®åä¿¡ã¨ã¿ã¤ã ã¢ã¦ããå¾
|
---|
1846 | ã¤ã*/
|
---|
1847 | syscall(wai_sem(SEM_DHCP4_CLI_READY));
|
---|
1848 |
|
---|
1849 | if (ct->flags & DHCP4C_FLAG_RCV_MSG) {
|
---|
1850 | while (ct->val_lst != NULL) {
|
---|
1851 | error = eval_rcv_msg(ct);
|
---|
1852 | }
|
---|
1853 | ct->flags &= ~DHCP4C_FLAG_RCV_MSG;
|
---|
1854 | }
|
---|
1855 | if (ct->flags & DHCP4C_FLAG_TMOUT_MASK)
|
---|
1856 | error = dispatch_timeout(ct);
|
---|
1857 |
|
---|
1858 | if (ct->error != E_OK)
|
---|
1859 | error = ct->error;
|
---|
1860 | }
|
---|
1861 |
|
---|
1862 | return error;
|
---|
1863 | }
|
---|
1864 |
|
---|
1865 | /*
|
---|
1866 | * init_context -- DHCP ã¯ã©ã¤ã¢ã³ãã³ã³ããã¹ãæ§é ä½ãåæåããã
|
---|
1867 | */
|
---|
1868 |
|
---|
1869 | static void
|
---|
1870 | init_context (T_DHCP4_CLI_CONTEXT *ct, ID cepid)
|
---|
1871 | {
|
---|
1872 | memset(ct, 0, sizeof(*ct));
|
---|
1873 | ct->cepid = cepid;
|
---|
1874 | ct->sc = IF_ETHER_NIC_GET_SOFTC();
|
---|
1875 | ct->dst.portno = DHCP4_SRV_CFG_PORTNO;
|
---|
1876 | ct->dst.ipaddr = IPV4_ADDRANY;
|
---|
1877 | ct->set_addr_callback = DHCP4_CLI_SET_ADDR_CALLBACK;
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 | /*
|
---|
1881 | * rel_cli_msg -- ã¡ãã»ã¼ã¸æ§é ä½ã解æ¾ããã
|
---|
1882 | */
|
---|
1883 |
|
---|
1884 | static void
|
---|
1885 | rel_cli_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1886 | {
|
---|
1887 | T_DHCP4_CLI_MSG *ptr, *cell;
|
---|
1888 |
|
---|
1889 | for (ptr = ct->rcv_lst; ptr != NULL; ) {
|
---|
1890 | cell = ptr;
|
---|
1891 | ptr = ptr->next;
|
---|
1892 | syscall(rel_mpf(MPF_DHCP4_CLI_MSG, (void*)cell));
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | for (ptr = ct->val_lst; ptr != NULL; ) {
|
---|
1896 | cell = ptr;
|
---|
1897 | ptr = ptr->next;
|
---|
1898 | syscall(rel_mpf(MPF_DHCP4_CLI_MSG, (void*)cell));
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 | for (ptr = ct->prf_lst; ptr != NULL; ) {
|
---|
1902 | cell = ptr;
|
---|
1903 | ptr = ptr->next;
|
---|
1904 | syscall(rel_mpf(MPF_DHCP4_CLI_MSG, (void*)cell));
|
---|
1905 | }
|
---|
1906 |
|
---|
1907 | if (ct->snd_msg != NULL)
|
---|
1908 | syscall(rel_mpf(MPF_DHCP4_CLI_MSG, (void*)ct->snd_msg));
|
---|
1909 | ct->rcv_lst = ct->prf_lst = ct->val_lst = ct->snd_msg = NULL;
|
---|
1910 | }
|
---|
1911 |
|
---|
1912 | /*
|
---|
1913 | * init_cli_msg -- åã¡ãã»ã¼ã¸æ§é ä½ãåæåããã
|
---|
1914 | */
|
---|
1915 |
|
---|
1916 | static ER
|
---|
1917 | init_cli_msg (T_DHCP4_CLI_CONTEXT *ct)
|
---|
1918 | {
|
---|
1919 | T_DHCP4_CLI_MSG *cell;
|
---|
1920 | ER error;
|
---|
1921 | int count;
|
---|
1922 |
|
---|
1923 | /* éä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãç²å¾ããã*/
|
---|
1924 | if ((error = tget_mpf(MPF_DHCP4_CLI_MSG, (void*)&ct->snd_msg, TMO_DHCP4C_MPF_GET)) != E_OK) {
|
---|
1925 | syslog(LOG_NOTICE, "[DHCP4C] error, tget_mpf() for send: %s.", itron_strerror(error));
|
---|
1926 | return error;
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | /*
|
---|
1930 | * æå¹ãªãªã¼ã¹ã»ã¡ãã»ã¼ã¸æ§é ä½ãããå ´åã¯ã
|
---|
1931 | * ç²å¾ããåä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãªã¹ãã®æ§é ä½ã 1æ¸ããã
|
---|
1932 | */
|
---|
1933 | if (ct->act_msg == NULL)
|
---|
1934 | count = NUM_DHCP4_MSG_LIST;
|
---|
1935 | else
|
---|
1936 | count = NUM_DHCP4_MSG_LIST - 1;
|
---|
1937 |
|
---|
1938 | /* åä¿¡ã¡ãã»ã¼ã¸æ§é ä½ãç²å¾ããã*/
|
---|
1939 | while (count --> 0) {
|
---|
1940 | if ((error = tget_mpf(MPF_DHCP4_CLI_MSG, (void*)&cell, TMO_DHCP4C_MPF_GET)) != E_OK) {
|
---|
1941 | syslog(LOG_NOTICE, "[DHCP4C] error, tget_mpf() for receive: %s.", itron_strerror(error));
|
---|
1942 | break;
|
---|
1943 | }
|
---|
1944 | cell->next = ct->rcv_lst;
|
---|
1945 | ct->rcv_lst = cell;
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 | return E_OK;
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 | /*
|
---|
1952 | * dhcp4c_renew_info -- ãããã¯ã¼ã¯æ
|
---|
1953 | å ±ãååå¾ããã
|
---|
1954 | */
|
---|
1955 |
|
---|
1956 | ER
|
---|
1957 | dhcp4c_renew_info (void)
|
---|
1958 | {
|
---|
1959 | if (context.fsm == DHCP4_FSM_BOUND) {
|
---|
1960 |
|
---|
1961 | /* REL_INFO ç¶æ
|
---|
1962 | ã«é·ç§»ããå¾ã« SLEEP ã解é¤ããããã«è¨å®ããã*/
|
---|
1963 | context.flags |= DHCP4C_FLAG_RENEW;
|
---|
1964 | start_rel_info(&context);
|
---|
1965 | return E_OK;
|
---|
1966 | }
|
---|
1967 | else if (context.fsm == DHCP4_FSM_SLEEP) {
|
---|
1968 |
|
---|
1969 | /* SLEEP ã解é¤ããã*/
|
---|
1970 | wup_tsk(DHCP4_CLI_TASK);
|
---|
1971 | return E_OK;
|
---|
1972 | }
|
---|
1973 | else
|
---|
1974 | return E_OBJ;
|
---|
1975 | }
|
---|
1976 |
|
---|
1977 | /*
|
---|
1978 | * dhcp4c_rel_info -- DHCP ã§è¨å®ããããããã¯ã¼ã¯æ
|
---|
1979 | å ±ã解æ¾ããã
|
---|
1980 | */
|
---|
1981 |
|
---|
1982 | ER
|
---|
1983 | dhcp4c_rel_info (void)
|
---|
1984 | {
|
---|
1985 | /* REL_INFO ç¶æ
|
---|
1986 | ã«é·ç§»ããã*/
|
---|
1987 | start_rel_info(&context);
|
---|
1988 | return E_OK;
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | /*
|
---|
1992 | * dhcp4c_get_info -- DHCP ã§è¨å®ããããããã¯ã¼ã¯æ
|
---|
1993 | å ±ãè¿ãã
|
---|
1994 | */
|
---|
1995 |
|
---|
1996 | ER
|
---|
1997 | dhcp4c_get_info (T_IN4_ADDR *addr, uint32_t *expire, uint32_t *renew,
|
---|
1998 | uint32_t *rebind, SYSTIM *bind_start)
|
---|
1999 | {
|
---|
2000 | if (addr == NULL || expire == NULL ||
|
---|
2001 | renew == NULL || rebind == NULL || bind_start == NULL)
|
---|
2002 | return E_PAR;
|
---|
2003 | else if (context.act_msg == NULL)
|
---|
2004 | return E_OBJ;
|
---|
2005 | else {
|
---|
2006 | *addr = context.act_msg->srv.ipaddr;
|
---|
2007 | *expire = context.timers[DHCP4C_TIMER_EXPIRE];
|
---|
2008 | *renew = context.timers[DHCP4C_TIMER_RENEW];
|
---|
2009 | *rebind = context.timers[DHCP4C_TIMER_REBIND];
|
---|
2010 | *bind_start = context.bind_start;
|
---|
2011 | return E_OK;
|
---|
2012 | }
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | /*
|
---|
2016 | * ã³ã¼ã«ããã¯é¢æ°
|
---|
2017 | */
|
---|
2018 |
|
---|
2019 | ER
|
---|
2020 | callback_nblk_dhcp4_cli (ID cepid, FN fncd, void *p_parblk)
|
---|
2021 | {
|
---|
2022 | T_DHCP4_CLI_MSG *msg;
|
---|
2023 | ER_UINT len;
|
---|
2024 |
|
---|
2025 | len = *(ER_UINT*)p_parblk;
|
---|
2026 | if (len < 0 && len != E_RLWAI) {
|
---|
2027 | /* E_RLWAI 以å¤ã§ã0 以ä¸ã®å ´åã¯ãã¨ã©ã¼ãæå³ãã¦ããã*/
|
---|
2028 | syslog(LOG_NOTICE, "[DHCPC(CBR)] error: %s, fncd: %s", itron_strerror(len), in_strtfn(fncd));
|
---|
2029 | }
|
---|
2030 | else {
|
---|
2031 | if (fncd == TEV_UDP_RCV_DAT) {
|
---|
2032 |
|
---|
2033 | /* åä¿¡ã¡ãã»ã¼ã¸ãªã¹ãã空ã®æã¯ãåä¿¡ããªãã§ãã¼ã¿ã°ã©ã ãæ¨ã¦ãã*/
|
---|
2034 | if (context.rcv_lst == NULL)
|
---|
2035 | syslog(LOG_NOTICE, "[DHCP4C(CBR)] receive buffer all busy.");
|
---|
2036 | else {
|
---|
2037 | /* ã¡ãã»ã¼ã¸ãåä¿¡ããã¡ãã»ã¼ã¸æ§é ä½ãæºåããã*/
|
---|
2038 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
2039 | msg = context.rcv_lst;
|
---|
2040 | context.rcv_lst = context.rcv_lst->next;
|
---|
2041 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
2042 |
|
---|
2043 | /* ã¡ãã»ã¼ã¸ãåä¿¡ããã*/
|
---|
2044 | len = udp_rcv_dat(context.cepid, &msg->srv, &msg->msg, sizeof(msg->msg), TMO_POL);
|
---|
2045 |
|
---|
2046 | syscall(wai_sem(SEM_DHCP4_CLI_LOCK));
|
---|
2047 | msg->len = len;
|
---|
2048 | if (len >= 0) {
|
---|
2049 | /* åä¿¡ããã¡ãã»ã¼ã¸ãæ¤è¨¼ã¡ãã»ã¼ã¸ãªã¹ãã«ç§»ãã*/
|
---|
2050 | msg->next = context.val_lst;
|
---|
2051 | context.val_lst = msg;
|
---|
2052 | context.flags |= DHCP4C_FLAG_RCV_MSG;
|
---|
2053 | }
|
---|
2054 | else {
|
---|
2055 | /* ã¡ãã»ã¼ã¸æ§é ä½ãåä¿¡ã¡ãã»ã¼ã¸ãªã¹ãã«æ»ãã*/
|
---|
2056 | context.error = len;
|
---|
2057 | msg->next = context.rcv_lst;
|
---|
2058 | context.rcv_lst = msg;
|
---|
2059 | }
|
---|
2060 | syscall(sig_sem(SEM_DHCP4_CLI_LOCK));
|
---|
2061 | }
|
---|
2062 | }
|
---|
2063 | syscall(sig_sem(SEM_DHCP4_CLI_READY));
|
---|
2064 | }
|
---|
2065 | return E_OK;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | /*
|
---|
2069 | * dhcp4_cli_task -- DHCP ã¯ã©ã¤ã¢ã³ãã¿ã¹ã¯
|
---|
2070 | */
|
---|
2071 |
|
---|
2072 | void
|
---|
2073 | dhcp4_cli_task (intptr_t exinf)
|
---|
2074 | {
|
---|
2075 | T_DHCP4_CLI_CONTEXT *ct;
|
---|
2076 | ID tskid;
|
---|
2077 | ER error;
|
---|
2078 |
|
---|
2079 | dly_tsk(1000);
|
---|
2080 | get_tid(&tskid);
|
---|
2081 | syslog(LOG_NOTICE, "[DHCP4C:%d,%d] started.", tskid, (ID)exinf);
|
---|
2082 | dly_tsk(500);
|
---|
2083 | ct = &context;
|
---|
2084 |
|
---|
2085 | /* DHCP ã¯ã©ã¤ã¢ã³ãã³ã³ããã¹ãæ§é ä½ãåæåããã*/
|
---|
2086 | init_context(ct, (ID)exinf);
|
---|
2087 |
|
---|
2088 | while (true) {
|
---|
2089 |
|
---|
2090 | /* 1ç§æ¯ã«ã¿ã¤ã ã¢ã¦ãããããã«ã¿ã¤ãã¼ãè¨å®ããã*/
|
---|
2091 | timeout((callout_func)dhcpc_timer, ct, NET_TIMER_HZ / DHCP4C_TIMER_HZ);
|
---|
2092 |
|
---|
2093 | /* ã¡ãã»ã¼ã¸æ§é ä½ãåæåããã*/
|
---|
2094 | if ((error = init_cli_msg(ct)) == E_OK) {
|
---|
2095 |
|
---|
2096 | /* SELECT ç¶æ
|
---|
2097 | ã«é·ç§»ããã*/
|
---|
2098 | start_select(ct);
|
---|
2099 |
|
---|
2100 | /* ã¡ã¤ã³ã«ã¼ã */
|
---|
2101 | error = dispatch_event(ct);
|
---|
2102 | }
|
---|
2103 |
|
---|
2104 | /* å
|
---|
2105 | ¨ã¦ã® TIMER ããã£ã³ã»ã«ããã*/
|
---|
2106 | cancel_all_timers(ct);
|
---|
2107 |
|
---|
2108 | /* ã¿ã¤ãã¼ãåæ¢ããã*/
|
---|
2109 | untimeout((callout_func)dhcpc_timer, ct);
|
---|
2110 |
|
---|
2111 | /* ã¡ãã»ã¼ã¸æ§é ä½ã解æ¾ããã*/
|
---|
2112 | rel_cli_msg (ct);
|
---|
2113 |
|
---|
2114 | /* ãªã¼ã¹ã解æ¾å¾ãååå¾ãæå®ããã¦ããªããã°ä¼æ¢ããã*/
|
---|
2115 | if (!(ct->flags & DHCP4C_FLAG_RENEW)) {
|
---|
2116 |
|
---|
2117 | /* ä¼æ¢ããã*/
|
---|
2118 | if (error == E_OK)
|
---|
2119 | syslog(LOG_NOTICE, "[DHCP4C] lease released, go to sleep.");
|
---|
2120 | else {
|
---|
2121 | syslog(LOG_NOTICE, "[DHCP4C] server not available, go to sleep, error: %s.", itron_strerror(error));
|
---|
2122 | ct->fsm = DHCP4_FSM_SLEEP;
|
---|
2123 | }
|
---|
2124 | slp_tsk();
|
---|
2125 | }
|
---|
2126 | ct->flags = 0;
|
---|
2127 | ct->error = E_OK;
|
---|
2128 | }
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | #endif /* of #ifdef DHCP4_CLI_CFG */
|
---|