K2.6 patches and update.
[tomato.git] / release / src-rt / wl / nas / nas_linux.c
blobf62ec6bb731a026cf0d1d1482c062a3b27328b70
1 /*
2 * Network Authentication Service deamon (Linux)
4 * Copyright (C) 2010, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
8 * the contents of this file may not be disclosed to third parties, copied
9 * or duplicated in any form, in whole or in part, without the prior
10 * written permission of Broadcom Corporation.
12 * $Id: nas_linux.c 241388 2011-02-18 03:33:22Z stakita $
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/ioctl.h>
24 #include <sys/time.h>
25 #include <netinet/in.h>
26 #include <net/if.h>
27 #include <net/if_arp.h>
28 #include <arpa/inet.h>
29 #include <signal.h>
30 #include <fcntl.h>
32 #include <linux/if_packet.h>
33 #include <linux/if_ether.h>
34 #include <linux/types.h>
35 #include <linux/filter.h>
37 #include <typedefs.h>
38 #include <bcmutils.h>
39 #include <bcmnvram.h>
40 #include <proto/ethernet.h>
41 #include <proto/eapol.h>
42 #include <bcmcrypto/md5.h>
43 #include <wlutils.h>
45 #include <nas_wksp.h>
46 #include <eapd.h>
48 static nas_wksp_t * nas_nwksp = NULL;
50 void
51 nas_sleep_ms(uint ms)
53 usleep(1000*ms);
56 void
57 nas_rand128(uint8 *rand128)
59 static int dev_random_fd = -1;
60 struct timeval tv;
61 struct timezone tz;
62 MD5_CTX md5;
63 if (dev_random_fd == -1)
64 /* Use /dev/urandom because /dev/random, when it works at all,
65 * won't return anything when its entropy pool runs short and
66 * we can't control that. /dev/urandom look good enough.
68 dev_random_fd = open("/dev/urandom", O_RDONLY|O_NONBLOCK);
69 if (dev_random_fd != -1)
70 read(dev_random_fd, rand128, 16);
71 else {
72 gettimeofday(&tv, &tz);
73 tv.tv_sec ^= rand();
74 MD5Init(&md5);
75 MD5Update(&md5, (unsigned char *) &tv, sizeof(tv));
76 MD5Update(&md5, (unsigned char *) &tz, sizeof(tz));
77 MD5Final(rand128, &md5);
81 static void
82 hup_hdlr(int sig)
84 if (nas_nwksp)
85 nas_nwksp->flags = NAS_WKSP_FLAG_SHUTDOWN;
86 return;
90 * Configuration APIs
92 int
93 nas_safe_get_conf(char *outval, int outval_size, char *name)
95 char *val;
97 if (name == NULL || outval == NULL) {
98 if (outval)
99 memset(outval, 0, outval_size);
100 return -1;
103 val = nvram_safe_get(name);
104 if (!strcmp(val, ""))
105 memset(outval, 0, outval_size);
106 else
107 snprintf(outval, outval_size, "%s", val);
108 return 0;
111 /* service main entry */
113 main(int argc, char *argv[])
115 /* display usage if nothing is specified */
116 if (argc == 2 &&
117 (!strncmp(argv[1], "-h", 2) ||
118 !strncmp(argv[1], "-H", 2))) {
119 nas_wksp_display_usage();
120 return 0;
123 /* alloc nas/wpa work space */
124 if (!(nas_nwksp = nas_wksp_alloc_workspace())) {
125 NASMSG("Unable to allocate work space memory. Quitting...\n");
126 return 0;
129 if (argc > 1 && nas_wksp_parse_cmd(argc, argv, nas_nwksp)) {
130 NASMSG("Command line parsing error. Quitting...\n");
131 nas_wksp_free_workspace(nas_nwksp);
132 return 0;
134 else {
135 #ifdef BCMDBG
136 /* verbose - 0:no | others:yes */
137 /* for workspace */
138 char debug[8];
139 if (nas_safe_get_conf(debug, sizeof(debug), "nas_dbg") == 0)
140 debug_nwksp = (int)atoi(debug);
141 #endif
144 /* establish a handler to handle SIGTERM. */
145 signal(SIGTERM, hup_hdlr);
147 /* run main loop to dispatch messages */
148 nas_wksp_main_loop(nas_nwksp);
150 return 0;
153 void
154 nas_reset_board()
156 kill(1, SIGTERM);
157 return;