K2.6 patches and update.
[tomato.git] / release / src-rt / wl / nas / nas_ecos.c
blobb7605437126e83b526871fe84e2e171b062d7995
1 /* Network Authentication Service deamon (Ecos)
3 * Copyright (C) 2010, Broadcom Corporation
4 * All Rights Reserved.
5 *
6 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
7 * the contents of this file may not be disclosed to third parties, copied
8 * or duplicated in any form, in whole or in part, without the prior
9 * written permission of Broadcom Corporation.
11 * $Id: nas_ecos.c 241388 2011-02-18 03:33:22Z stakita $
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
17 #include <typedefs.h>
18 #include <bcmutils.h>
19 #include <bcmnvram.h>
20 #include <proto/ethernet.h>
21 #include <proto/eapol.h>
22 #include <bcmcrypto/md5.h>
23 #include <wlutils.h>
25 #include <sys/socket.h>
26 #include <net/if.h>
27 #include <net/if_var.h>
29 #include <netinet/in.h>
30 #include <netinet/in_var.h>
31 #include <netinet/ip.h>
32 #include <netinet/udp.h>
33 #include <ecos_oslib.h>
35 #include <netconf.h>
36 #include <nvparse.h>
38 #include <proto/eap.h>
39 #include <nas_wksp.h>
40 #include <nas_radius.h>
42 #include <eapd.h>
44 extern int gettimeofday(struct timeval *tv, struct timezone *tz);
45 extern void sys_reboot(void);
47 static cyg_handle_t nas_main_hdl;
48 static char nas_main_stack[16*1024];
49 static cyg_thread nas_thread;
50 static int _nas_pid = 0, _nas_ready = 0;
52 static nas_wksp_t *nas_nwksp = NULL;
54 void
55 nas_sleep_ms(uint ms)
57 cyg_tick_count_t ostick;
59 ostick = ms / 10;
60 cyg_thread_delay(ostick);
63 void
64 nas_rand128(uint8 *rand128)
66 struct timeval tv;
67 struct timezone tz;
68 MD5_CTX md5;
70 gettimeofday(&tv, &tz);
71 tv.tv_sec ^= rand();
72 MD5Init(&md5);
73 MD5Update(&md5, (unsigned char *) &tv, sizeof(tv));
74 MD5Update(&md5, (unsigned char *) &tz, sizeof(tz));
75 MD5Final(rand128, &md5);
78 static void
79 hup_hdlr(int sig)
81 if (nas_nwksp)
82 nas_nwksp->flags = NAS_WKSP_FLAG_SHUTDOWN;
84 return;
88 * Configuration APIs
90 int
91 nas_safe_get_conf(char *outval, int outval_size, char *name)
93 char *val;
95 if (name == NULL || outval == NULL) {
96 if (outval)
97 memset(outval, 0, outval_size);
98 return -1;
101 val = nvram_safe_get(name);
102 if (!strcmp(val, ""))
103 memset(outval, 0, outval_size);
104 else
105 snprintf(outval, outval_size, "%s", val);
106 return 0;
109 void
110 nas_task_ready(void)
112 _nas_ready = 1;
115 /* nas task entry */
116 int nas_main(void)
118 #ifdef BCMDBG
119 char debug[8];
120 #endif
121 /* clear _nas_ready */
122 _nas_ready = 0;
124 /* fill up NAS task pid */
125 _nas_pid = oslib_pid();
127 /* clear rootnwksp */
128 nas_nwksp = NULL;
130 /* alloc nas/wpa work space */
131 if (!(nas_nwksp = nas_wksp_alloc_workspace())) {
132 NASMSG("Unable to allocate work space memory. Quitting...\n");
133 return -1;
136 #ifdef BCMDBG
137 /* verbose - 0:no | others:yes */
138 /* for workspace */
139 if (nas_safe_get_conf(debug, sizeof(debug), "nas_dbg") == 0)
140 debug_nwksp = (int)atoi(debug);
141 #endif
143 /* run main loop to dispatch message */
144 nas_wksp_main_loop(nas_nwksp);
146 return 0;
149 void
150 nas_reset_board()
152 sys_reboot();
153 return;
156 void
157 nasd_start(void)
159 int wait_time = 1 * 100; /* 1 second */
161 if (!_nas_pid ||
162 !oslib_waitpid(_nas_pid, NULL)) {
163 cyg_thread_create(7,
164 (cyg_thread_entry_t *)nas_main,
165 (cyg_addrword_t)NULL,
166 "NAS",
167 (void *)nas_main_stack,
168 sizeof(nas_main_stack),
169 &nas_main_hdl,
170 &nas_thread);
171 cyg_thread_resume(nas_main_hdl);
173 /* Make sure nas stared and initial completed. Otherwise,
174 * it may lost some wireless driver events.
176 while (_nas_ready == 0 && wait_time > 0) {
177 cyg_thread_delay(10);
178 wait_time -= 10;
181 NASMSG("NAS task started\n");
185 void
186 nasd_stop(void)
188 if (_nas_pid) {
189 hup_hdlr(15 /* SIGTERM */);
191 /* wait until not running */
192 while (oslib_waitpid(_nas_pid, NULL))
193 cyg_thread_delay(10);
194 _nas_pid = 0;
195 NASMSG("NAS task stopped\n");