K2.6 patches and update.
[tomato.git] / release / src-rt / wl / nas / nas_vx.c
blobf7113de373367158af0ffd1ddec0732deac15ea1
1 /* Network Authentication Service deamon (vxWorks)
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_vx.c 241388 2011-02-18 03:33:22Z stakita $
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include <ctype.h>
20 #include <assert.h>
22 #include <vxWorks.h>
23 #include <ioLib.h>
24 #include <ifLib.h>
25 #include <muxLib.h>
26 #include <muxTkLib.h>
27 #include <tickLib.h>
28 #include <taskLib.h>
29 #include <taskVarLib.h>
30 #include <errnoLib.h>
32 extern int gettimeofday(struct timeval *tv, struct timezone *tz);
33 extern int sysClkRateGet(void);
34 extern void sys_reboot();
36 #include <typedefs.h>
37 #include <bcmutils.h>
38 #include <bcmnvram.h>
39 #include <proto/ethernet.h>
40 #include <proto/eapol.h>
41 #include <bcmcrypto/md5.h>
42 #include <wlutils.h>
44 #include <nas_wksp.h>
45 #include <eapd.h>
47 static nas_wksp_t * nas_nwksp = NULL;
49 void
50 nas_sleep_ms(uint ms)
52 taskDelay(ms*sysClkRateGet()/1000);
55 void
56 nas_rand128(uint8 *rand128)
58 struct timeval tv;
59 struct timezone tz;
60 MD5_CTX md5;
62 gettimeofday(&tv, &tz);
63 tv.tv_sec ^= rand();
64 MD5Init(&md5);
65 MD5Update(&md5, (unsigned char *) &tv, sizeof(tv));
66 MD5Update(&md5, (unsigned char *) &tz, sizeof(tz));
67 MD5Final(rand128, &md5);
70 static void
71 hup_hdlr(int sig)
73 if (nas_nwksp)
74 nas_nwksp->flags = NAS_WKSP_FLAG_SHUTDOWN;
76 return;
80 * Configuration APIs
82 int
83 nas_safe_get_conf(char *outval, int outval_size, char *name)
85 char *val;
87 if (name == NULL || outval == NULL) {
88 if (outval)
89 memset(outval, 0, outval_size);
90 return -1;
93 val = nvram_safe_get(name);
94 if (!strcmp(val, ""))
95 memset(outval, 0, outval_size);
96 else
97 snprintf(outval, outval_size, "%s", val);
98 return 0;
101 /* nas task entry */
103 nas_main()
105 #ifdef BCMDBG
106 char debug[8];
107 #endif
109 /* clear rootnwksp */
110 nas_nwksp = NULL;
112 /* alloc nas/wpa work space */
113 if (!(nas_nwksp = nas_wksp_alloc_workspace())) {
114 NASMSG("Unable to allocate work space memory. Quitting...\n");
115 return 0;
118 #ifdef BCMDBG
119 /* verbose - 0:no | others:yes */
120 /* for workspace */
121 if (nas_safe_get_conf(debug, sizeof(debug), "nas_dbg") == 0)
122 debug_nwksp = (int)atoi(debug);
123 #endif
124 /* establish a handler to handle SIGTERM. */
125 signal(SIGTERM, hup_hdlr);
127 /* run main loop to dispatch message */
128 nas_wksp_main_loop(nas_nwksp);
130 return 0;
133 void
134 nas_reset_board()
136 sys_reboot();
137 return;
140 void
141 nasStart(void)
143 int tid = taskNameToId("NAS");
144 ULONG ticks;
146 if (tid == ERROR) {
147 /* clear nas wksp initialization flag */
148 nas_wksp_clear_inited();
150 taskSpawn("NAS",
151 60, /* priority of new task */
152 0, /* task option word */
153 30000, /* size (bytes) of stack needed plus name */
154 (FUNCPTR)nas_main, /* entry point of new task */
157 0, 0, 0, 0, 0, 0, 0, 0);
158 printf("NAS task started.\n");
160 /* wait until nas initialization finished */
161 ticks = tickGet();
162 do {
163 if (tickGet() - ticks < 3 * sysClkRateGet())
164 taskDelay(sysClkRateGet());
165 else {
166 printf("Unable to wait NAS initialization finished!.\n");
167 return;
169 } while (taskNameToId("NAS") != ERROR && !nas_wksp_is_inited());
171 else
172 printf("NAS task is already running.\n");
175 void
176 nasStop(void)
178 int tid = taskNameToId("NAS");
180 if (tid != ERROR) {
181 ULONG ticks;
183 kill(tid, SIGTERM);
185 /* wait till the task is dead */
186 ticks = tickGet();
187 do {
188 if (tickGet() - ticks < 3 * sysClkRateGet())
189 taskDelay(sysClkRateGet());
190 else {
191 printf("Unable to kill NAS task!.\n");
192 return;
195 while (taskNameToId("NAS") != ERROR);
196 printf("NAS task killed.\n");
198 else
199 printf("NAS task is not running.\n");