| 1 |
/* |
|---|
| 2 |
* wpa.c |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 |
* |
|---|
| 6 |
* This program is free software; you can redistribute it and/or |
|---|
| 7 |
* modify it under the terms of the GNU General Public License |
|---|
| 8 |
* as published by the Free Software Foundation; either version 2 |
|---|
| 9 |
* of the License. |
|---|
| 10 |
* |
|---|
| 11 |
* This program is distributed in the hope that it will be useful, |
|---|
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 |
* GNU General Public License for more details. |
|---|
| 15 |
* |
|---|
| 16 |
* You should have received a copy of the GNU General Public License |
|---|
| 17 |
* along with this program; if not, write to the Free Software |
|---|
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 |
* |
|---|
| 20 |
* $Id: |
|---|
| 21 |
*/ |
|---|
| 22 |
#ifdef HAVE_NAS |
|---|
| 23 |
|
|---|
| 24 |
#include <unistd.h> |
|---|
| 25 |
#include <string.h> |
|---|
| 26 |
#include <stdio.h> |
|---|
| 27 |
#include <stdlib.h> |
|---|
| 28 |
#include <sys/types.h> |
|---|
| 29 |
#include <bcmnvram.h> |
|---|
| 30 |
#include <shutils.h> |
|---|
| 31 |
#include <nvparse.h> |
|---|
| 32 |
#include "snmp.h" |
|---|
| 33 |
#include <signal.h> |
|---|
| 34 |
#include <utils.h> |
|---|
| 35 |
#include <syslog.h> |
|---|
| 36 |
#include <wlutils.h> |
|---|
| 37 |
#include <bcmutils.h> |
|---|
| 38 |
|
|---|
| 39 |
void start_nas_notify(char *ifname) |
|---|
| 40 |
{ |
|---|
| 41 |
char *argv[] = { "nas4not", "lan", ifname, "up", |
|---|
| 42 |
NULL, /* role */ |
|---|
| 43 |
NULL, /* crypto */ |
|---|
| 44 |
NULL, /* auth */ |
|---|
| 45 |
NULL, /* passphrase */ |
|---|
| 46 |
NULL, /* ssid */ |
|---|
| 47 |
NULL |
|---|
| 48 |
}; |
|---|
| 49 |
char *str = NULL; |
|---|
| 50 |
char tmp[100], prefix[] = "wlXXXXXXXXXX_", pidfile[] = |
|---|
| 51 |
"/tmp/nas.wlXXXXXXXlan.pid"; |
|---|
| 52 |
int unit; |
|---|
| 53 |
char remote[ETHER_ADDR_LEN]; |
|---|
| 54 |
char ssid[48], pass[80], auth[16], crypto[16], role[8]; |
|---|
| 55 |
int i; |
|---|
| 56 |
|
|---|
| 57 |
/* |
|---|
| 58 |
* the wireless interface must be configured to run NAS |
|---|
| 59 |
*/ |
|---|
| 60 |
wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)); |
|---|
| 61 |
snprintf(prefix, sizeof(prefix), "wl%d_", unit); |
|---|
| 62 |
snprintf(pidfile, sizeof(pidfile), "/tmp/nas.wl%dlan.pid", unit); |
|---|
| 63 |
|
|---|
| 64 |
if (!(str = file2str(pidfile))) // no pidfile means no nas was run (required) |
|---|
| 65 |
{ |
|---|
| 66 |
return; |
|---|
| 67 |
} |
|---|
| 68 |
free(str); |
|---|
| 69 |
sleep(3); |
|---|
| 70 |
/* |
|---|
| 71 |
* find WDS link configuration |
|---|
| 72 |
*/ |
|---|
| 73 |
wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN); |
|---|
| 74 |
for (i = 0; i < MAX_NVPARSE; i++) { |
|---|
| 75 |
char mac[ETHER_ADDR_STR_LEN]; |
|---|
| 76 |
uint8 ea[ETHER_ADDR_LEN]; |
|---|
| 77 |
|
|---|
| 78 |
if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass) |
|---|
| 79 |
&& ether_atoe(mac, ea) |
|---|
| 80 |
&& !bcmp(ea, remote, ETHER_ADDR_LEN)) { |
|---|
| 81 |
argv[4] = role; |
|---|
| 82 |
argv[5] = crypto; |
|---|
| 83 |
argv[6] = auth; |
|---|
| 84 |
argv[7] = pass; |
|---|
| 85 |
argv[8] = ssid; |
|---|
| 86 |
break; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
/* |
|---|
| 91 |
* did not find WDS link configuration, use wireless' |
|---|
| 92 |
*/ |
|---|
| 93 |
if (i == MAX_NVPARSE) { |
|---|
| 94 |
/* |
|---|
| 95 |
* role |
|---|
| 96 |
*/ |
|---|
| 97 |
argv[4] = "auto"; |
|---|
| 98 |
/* |
|---|
| 99 |
* crypto |
|---|
| 100 |
*/ |
|---|
| 101 |
argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp)); |
|---|
| 102 |
/* |
|---|
| 103 |
* auth mode |
|---|
| 104 |
*/ |
|---|
| 105 |
argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp)); |
|---|
| 106 |
/* |
|---|
| 107 |
* passphrase |
|---|
| 108 |
*/ |
|---|
| 109 |
argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp)); |
|---|
| 110 |
/* |
|---|
| 111 |
* ssid |
|---|
| 112 |
*/ |
|---|
| 113 |
argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp)); |
|---|
| 114 |
} |
|---|
| 115 |
int pid; |
|---|
| 116 |
|
|---|
| 117 |
_evalpid(argv, ">/dev/console", 0, &pid); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
void start_radius(char *prefix) |
|---|
| 121 |
{ |
|---|
| 122 |
|
|---|
| 123 |
// wrt-radauth $IFNAME $server $port $share $override $mackey $maxun & |
|---|
| 124 |
|
|---|
| 125 |
if (nvram_nmatch("1", "%s_radauth", prefix) |
|---|
| 126 |
&& nvram_nmatch("ap", "%s_mode", prefix)) { |
|---|
| 127 |
char *server = nvram_nget("%s_radius_ipaddr", prefix); |
|---|
| 128 |
char *port = nvram_nget("%s_radius_port", prefix); |
|---|
| 129 |
char *share = nvram_nget("%s_radius_key", prefix); |
|---|
| 130 |
char *ifname = nvram_nget("%s_ifname", prefix); |
|---|
| 131 |
char type[32]; |
|---|
| 132 |
|
|---|
| 133 |
sprintf(type, "%s_radmactype", prefix); |
|---|
| 134 |
char *pragma = ""; |
|---|
| 135 |
|
|---|
| 136 |
if (nvram_default_match(type, "0", "0")) |
|---|
| 137 |
pragma = "-n1 "; |
|---|
| 138 |
if (nvram_match(type, "1")) |
|---|
| 139 |
pragma = "-n2 "; |
|---|
| 140 |
if (nvram_match(type, "2")) |
|---|
| 141 |
pragma = "-n3 "; |
|---|
| 142 |
if (nvram_match(type, "3")) |
|---|
| 143 |
pragma = ""; |
|---|
| 144 |
sleep(1); // some delay is usefull |
|---|
| 145 |
sysprintf("wrt-radauth %s %s %s %s %s %s %s %s &", pragma, |
|---|
| 146 |
ifname, server, port, share, |
|---|
| 147 |
nvram_nget("%s_radius_override", prefix), |
|---|
| 148 |
nvram_nget("%s_radmacpassword", prefix), |
|---|
| 149 |
nvram_nget("%s_max_unauth_users", prefix)); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
void start_nas_single(char *type, char *prefix); |
|---|
| 155 |
|
|---|
| 156 |
// #define HAVE_NASCONF //use this to parse nas parameters from conf file. |
|---|
| 157 |
|
|---|
| 158 |
static void convert_wds(int instance) |
|---|
| 159 |
{ |
|---|
| 160 |
char wds_mac[254]; |
|---|
| 161 |
char buf[254]; |
|---|
| 162 |
|
|---|
| 163 |
if (nvram_nmatch("", "wl%d_wds", instance)) // For Router, accept |
|---|
| 164 |
// all WDS link |
|---|
| 165 |
strcpy(wds_mac, "*"); |
|---|
| 166 |
else // For AP, assign remote WDS MAC |
|---|
| 167 |
strcpy(wds_mac, nvram_nget("wl%d_wds", instance)); |
|---|
| 168 |
|
|---|
| 169 |
/* |
|---|
| 170 |
* For WPA-PSK mode, we want to convert wl_wds_mac to wl0_wds0 ... |
|---|
| 171 |
* wl0_wds255 |
|---|
| 172 |
*/ |
|---|
| 173 |
if (nvram_match("security_mode", "psk") |
|---|
| 174 |
|| nvram_match("security_mode", "psk2")) { |
|---|
| 175 |
int i = 0; |
|---|
| 176 |
int j; |
|---|
| 177 |
char mac[254]; |
|---|
| 178 |
char *next; |
|---|
| 179 |
|
|---|
| 180 |
foreach(mac, wds_mac, next) { |
|---|
| 181 |
snprintf(buf, sizeof(buf), "%s,auto,%s,%s,%s,%s", |
|---|
| 182 |
mac, |
|---|
| 183 |
nvram_safe_get("wl_crypto"), |
|---|
| 184 |
nvram_safe_get("security_mode"), |
|---|
| 185 |
nvram_nget("wl%d_ssid", instance), |
|---|
| 186 |
nvram_safe_get("wl_wpa_psk")); |
|---|
| 187 |
nvram_nset(buf, "wl%d_wds%d", instance, i); |
|---|
| 188 |
i++; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
/* |
|---|
| 192 |
* Del unused entry |
|---|
| 193 |
*/ |
|---|
| 194 |
for (j = i; j < MAX_NVPARSE; j++) |
|---|
| 195 |
del_wds_wsec(instance, j); |
|---|
| 196 |
} |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
void start_guest_nas(void) |
|---|
| 200 |
{ |
|---|
| 201 |
|
|---|
| 202 |
/* |
|---|
| 203 |
* char *unbridged_interfaces; char *next; char name[IFNAMSIZ], |
|---|
| 204 |
* lan[IFNAMSIZ]; int index; |
|---|
| 205 |
* |
|---|
| 206 |
* unbridged_interfaces = nvram_get("unbridged_ifnames"); |
|---|
| 207 |
* |
|---|
| 208 |
* if (unbridged_interfaces) foreach(name,unbridged_interfaces,next){ |
|---|
| 209 |
* index = get_ipconfig_index(name); if (index < 0) continue; |
|---|
| 210 |
* snprintf(lan,sizeof(lan),"lan%d",index); start_nas(lan); } |
|---|
| 211 |
*/ |
|---|
| 212 |
return; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
char *getSecMode(char *prefix) |
|---|
| 216 |
{ |
|---|
| 217 |
char wep[32]; |
|---|
| 218 |
char crypto[32]; |
|---|
| 219 |
|
|---|
| 220 |
sprintf(wep, "%s_wep", prefix); |
|---|
| 221 |
sprintf(crypto, "%s_crypto", prefix); |
|---|
| 222 |
/* |
|---|
| 223 |
* BugBug - should we bail when mode is wep ? |
|---|
| 224 |
*/ |
|---|
| 225 |
if (nvram_match(wep, "wep") || nvram_match(wep, "on") |
|---|
| 226 |
|| nvram_match(wep, "restricted") || nvram_match(wep, "enabled")) |
|---|
| 227 |
return "1"; |
|---|
| 228 |
else if (nvram_match(crypto, "tkip")) |
|---|
| 229 |
return "2"; |
|---|
| 230 |
else if (nvram_match(crypto, "aes")) |
|---|
| 231 |
return "4"; |
|---|
| 232 |
else if (nvram_match(crypto, "tkip+aes")) |
|---|
| 233 |
return "6"; |
|---|
| 234 |
else |
|---|
| 235 |
return "0"; |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
char *getAuthMode(char *prefix) |
|---|
| 239 |
{ |
|---|
| 240 |
char akm[32]; |
|---|
| 241 |
|
|---|
| 242 |
sprintf(akm, "%s_akm", prefix); |
|---|
| 243 |
if (strlen(nvram_safe_get(akm)) == 0 || nvram_match(akm, "disabled") |
|---|
| 244 |
|| nvram_match(akm, "wep")) |
|---|
| 245 |
return NULL; |
|---|
| 246 |
if (nvram_match(akm, "radius")) |
|---|
| 247 |
return "32"; |
|---|
| 248 |
else if (nvram_match(akm, "wpa")) |
|---|
| 249 |
return "2"; |
|---|
| 250 |
else if (nvram_match(akm, "psk")) |
|---|
| 251 |
return "4"; |
|---|
| 252 |
else if (nvram_match(akm, "psk2")) |
|---|
| 253 |
return "128"; |
|---|
| 254 |
else if (nvram_match(akm, "psk psk2")) |
|---|
| 255 |
return "132"; |
|---|
| 256 |
else if (nvram_match(akm, "wpa2")) |
|---|
| 257 |
return "64"; |
|---|
| 258 |
else if (nvram_match(akm, "wpa wpa2")) |
|---|
| 259 |
return "66"; |
|---|
| 260 |
else |
|---|
| 261 |
return "255"; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
char *getKey(char *prefix) |
|---|
| 265 |
{ |
|---|
| 266 |
char akm[32]; |
|---|
| 267 |
|
|---|
| 268 |
sprintf(akm, "%s_akm", prefix); |
|---|
| 269 |
if (nvram_match(akm, "wpa") || nvram_match(akm, "radius") |
|---|
| 270 |
|| nvram_match(akm, "wpa2") || nvram_match(akm, "wpa wpa2")) |
|---|
| 271 |
return nvram_nget("%s_radius_key", prefix); |
|---|
| 272 |
else if (nvram_match(akm, "psk") || nvram_match(akm, "psk2") |
|---|
| 273 |
|| nvram_match(akm, "psk psk2")) |
|---|
| 274 |
return nvram_nget("%s_wpa_psk", prefix); |
|---|
| 275 |
else |
|---|
| 276 |
return ""; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
/* |
|---|
| 280 |
* static void start_nas_ap(char *prefix,char *type) { char sec[32]; |
|---|
| 281 |
* sprintf(sec,"%s_security_mode",prefix); int i; for (i=0;i<strlen(sec);i++) |
|---|
| 282 |
* if (sec[i]=='.')sec[i]='X'; |
|---|
| 283 |
* |
|---|
| 284 |
* char *security_mode = nvram_safe_get (sec); |
|---|
| 285 |
* |
|---|
| 286 |
* if (strstr (security_mode, "psk") || strstr (security_mode, "wpa")) { char |
|---|
| 287 |
* auth[32]; sprintf(auth,"%s_auth",prefix); nvram_set (auth, "0"); } |
|---|
| 288 |
* convert_wds (); |
|---|
| 289 |
* |
|---|
| 290 |
* if (!type || !*type) { if (nvram_match ("wl0_mode", "ap")) type = "lan"; |
|---|
| 291 |
* else type = "wan"; } |
|---|
| 292 |
* |
|---|
| 293 |
* snprintf (cfgfile, sizeof (cfgfile), "/tmp/nas.%s.conf", type); snprintf |
|---|
| 294 |
* (pidfile, sizeof (pidfile), "/tmp/nas.%s.pid", type); |
|---|
| 295 |
* |
|---|
| 296 |
* { char *argv[] = { "/usr/sbin/nas", cfgfile, pidfile, type, NULL }; pid_t |
|---|
| 297 |
* pid; |
|---|
| 298 |
* |
|---|
| 299 |
* _eval (argv, NULL, 0, &pid); cprintf ("done\n"); } } |
|---|
| 300 |
*/ |
|---|
| 301 |
void start_nas_lan(int c) |
|---|
| 302 |
{ |
|---|
| 303 |
char wlname[32]; |
|---|
| 304 |
|
|---|
| 305 |
sprintf(wlname, "wl%d", c); |
|---|
| 306 |
start_radius(wlname); // quick fix, should be vif capable in future |
|---|
| 307 |
start_nas_single("lan", wlname); |
|---|
| 308 |
|
|---|
| 309 |
char *next; |
|---|
| 310 |
char var[80]; |
|---|
| 311 |
char *vifs = nvram_nget("wl%d_vifs", c); |
|---|
| 312 |
|
|---|
| 313 |
if (strlen(vifs)) |
|---|
| 314 |
foreach(var, vifs, next) { |
|---|
| 315 |
start_nas_single("lan", var); |
|---|
| 316 |
} |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
void start_nas_wan(int c) |
|---|
| 320 |
{ |
|---|
| 321 |
char wlname[32]; |
|---|
| 322 |
|
|---|
| 323 |
sprintf(wlname, "wl%d", c); |
|---|
| 324 |
start_nas_single("wan", wlname); |
|---|
| 325 |
|
|---|
| 326 |
char *next; |
|---|
| 327 |
char var[80]; |
|---|
| 328 |
char vif[16]; |
|---|
| 329 |
char *vifs = nvram_nget("wl%d_vifs", c); |
|---|
| 330 |
|
|---|
| 331 |
if (strlen(vifs)) |
|---|
| 332 |
foreach(var, vifs, next) { |
|---|
| 333 |
sprintf(vif, "%s_mode", var); |
|---|
| 334 |
if (nvram_match(vif, "sta") |
|---|
| 335 |
|| nvram_match(vif, "wet") |
|---|
| 336 |
|| nvram_match(vif, "apsta") |
|---|
| 337 |
|| nvram_match(vif, "apstawet")) { |
|---|
| 338 |
start_nas_single("wan", var); |
|---|
| 339 |
} else { |
|---|
| 340 |
start_nas_single("lan", var); |
|---|
| 341 |
} |
|---|
| 342 |
} |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
#ifdef HAVE_WPA_SUPPLICANT |
|---|
| 346 |
extern void setupSupplicant(char *prefix); |
|---|
| 347 |
#endif |
|---|
| 348 |
void start_nas(void) |
|---|
| 349 |
{ |
|---|
| 350 |
unlink("/tmp/.nas"); |
|---|
| 351 |
|
|---|
| 352 |
int cnt = get_wl_instances(); |
|---|
| 353 |
int c; |
|---|
| 354 |
int deadcount; |
|---|
| 355 |
int radiostate = -1; |
|---|
| 356 |
|
|---|
| 357 |
for (c = 0; c < cnt; c++) { |
|---|
| 358 |
if (nvram_nmatch("disabled", "wl%d_net_mode", c)) |
|---|
| 359 |
continue; |
|---|
| 360 |
|
|---|
| 361 |
for (deadcount = 0; deadcount < 5; deadcount++) { |
|---|
| 362 |
wl_ioctl(get_wl_instance_name(c), WLC_GET_RADIO, &radiostate, sizeof(int)); |
|---|
| 363 |
if (radiostate == 0) |
|---|
| 364 |
break; |
|---|
| 365 |
sleep (1); |
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
if (radiostate != 0) |
|---|
| 369 |
continue; |
|---|
| 370 |
char wlname[32]; |
|---|
| 371 |
|
|---|
| 372 |
sprintf(wlname, "wl%d", c); |
|---|
| 373 |
if (nvram_nmatch("sta", "wl%d_mode", c) |
|---|
| 374 |
|| nvram_nmatch("wet", "wl%d_mode", c) |
|---|
| 375 |
|| nvram_nmatch("apsta", "wl%d_mode", c) |
|---|
| 376 |
|| nvram_nmatch("apstawet", "wl%d_mode", c)) { |
|---|
| 377 |
cprintf("start nas wan\n"); |
|---|
| 378 |
#ifdef HAVE_WPA_SUPPLICANT |
|---|
| 379 |
if (nvram_nmatch("8021X", "wl%d_akm", c) |
|---|
| 380 |
&& nvram_nmatch("sta", "wl%d_mode", c)) |
|---|
| 381 |
setupSupplicant(wlname); |
|---|
| 382 |
else |
|---|
| 383 |
#endif |
|---|
| 384 |
start_nas_wan(c); |
|---|
| 385 |
|
|---|
| 386 |
} else { |
|---|
| 387 |
cprintf("start nas lan\n"); |
|---|
| 388 |
start_nas_lan(c); |
|---|
| 389 |
|
|---|
| 390 |
int s; |
|---|
| 391 |
|
|---|
| 392 |
for (s = 1; s <= MAX_WDS_DEVS; s++) { |
|---|
| 393 |
char *dev; |
|---|
| 394 |
|
|---|
| 395 |
if (nvram_nmatch |
|---|
| 396 |
("0", "wl%d_wds%d_enable", c, s)) |
|---|
| 397 |
continue; |
|---|
| 398 |
|
|---|
| 399 |
dev = nvram_nget("wl%d_wds%d_if", c, s); |
|---|
| 400 |
|
|---|
| 401 |
start_nas_notify(dev); |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
} |
|---|
| 405 |
} |
|---|
| 406 |
|
|---|
| 407 |
return; |
|---|
| 408 |
} |
|---|
| 409 |
|
|---|
| 410 |
void start_nas_single(char *type, char *prefix) |
|---|
| 411 |
{ |
|---|
| 412 |
FILE *fnas; |
|---|
| 413 |
#ifdef HAVE_NASCONF |
|---|
| 414 |
char conffile[64]; |
|---|
| 415 |
FILE *conf; |
|---|
| 416 |
#endif |
|---|
| 417 |
char pidfile[64]; |
|---|
| 418 |
char *auth_mode = "255"; /* -m N = WPA authorization mode (N = 0: |
|---|
| 419 |
* none, 1: 802.1x, 2: WPA PSK, 255: |
|---|
| 420 |
* disabled) */ |
|---|
| 421 |
char *sec_mode = { 0 }; /* -w N = security mode bitmask (N = 1: WEP, |
|---|
| 422 |
* 2: TKIP, 4: AES) */ |
|---|
| 423 |
char *key = { 0 }, *iface = { |
|---|
| 424 |
0}, *mode = { |
|---|
| 425 |
0}; |
|---|
| 426 |
|
|---|
| 427 |
if (!strcmp(prefix, "wl0")) |
|---|
| 428 |
led_control(LED_SEC0, LED_OFF); |
|---|
| 429 |
if (!strcmp(prefix, "wl1")) |
|---|
| 430 |
led_control(LED_SEC1, LED_OFF); |
|---|
| 431 |
|
|---|
| 432 |
if (nvram_nmatch("disabled", "%s_net_mode", prefix)) |
|---|
| 433 |
return; |
|---|
| 434 |
if (!strcmp(prefix, "wl0")) |
|---|
| 435 |
convert_wds(0); |
|---|
| 436 |
else |
|---|
| 437 |
convert_wds(1); |
|---|
| 438 |
{ |
|---|
| 439 |
|
|---|
| 440 |
snprintf(pidfile, sizeof(pidfile), "/tmp/nas.%s%s.pid", prefix, |
|---|
| 441 |
type); |
|---|
| 442 |
#ifdef HAVE_NASCONF |
|---|
| 443 |
snprintf(conffile, sizeof(conffile), "/tmp/nas.%s%s.conf", |
|---|
| 444 |
prefix, type); |
|---|
| 445 |
#endif |
|---|
| 446 |
|
|---|
| 447 |
char apmode[32]; |
|---|
| 448 |
|
|---|
| 449 |
sprintf(apmode, "%s_mode", prefix); |
|---|
| 450 |
if (!strcmp(type, "wan") && nvram_match(apmode, "ap")) { |
|---|
| 451 |
return; |
|---|
| 452 |
} |
|---|
| 453 |
// if (!strcmp (type, "lan")) |
|---|
| 454 |
// iface = "br0"; |
|---|
| 455 |
// else |
|---|
| 456 |
|
|---|
| 457 |
if (0 == type || 0 == *type) |
|---|
| 458 |
type = "lan"; |
|---|
| 459 |
if (!strcmp(type, "lan") && nvram_invmatch(apmode, "ap")) |
|---|
| 460 |
iface = "br0"; |
|---|
| 461 |
else { |
|---|
| 462 |
|
|---|
| 463 |
if (!strcmp(prefix, "wl0")) { |
|---|
| 464 |
iface = get_wl_instance_name(0); |
|---|
| 465 |
} else if (!strcmp(prefix, "wl1")) { |
|---|
| 466 |
iface = get_wl_instance_name(1); |
|---|
| 467 |
} else { |
|---|
| 468 |
iface = prefix; |
|---|
| 469 |
} |
|---|
| 470 |
} |
|---|
| 471 |
|
|---|
| 472 |
sec_mode = getSecMode(prefix); |
|---|
| 473 |
auth_mode = getAuthMode(prefix); |
|---|
| 474 |
|
|---|
| 475 |
if (strcmp(sec_mode, "0")) { |
|---|
| 476 |
if (!strcmp(prefix, "wl0")) |
|---|
| 477 |
led_control(LED_SEC0, LED_ON); |
|---|
| 478 |
if (!strcmp(prefix, "wl1")) |
|---|
| 479 |
led_control(LED_SEC1, LED_ON); |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
if (auth_mode == NULL) |
|---|
| 483 |
return; // no nas required |
|---|
| 484 |
if (strcmp(nvram_safe_get(apmode), "sta") |
|---|
| 485 |
&& strcmp(nvram_safe_get(apmode), "wet") |
|---|
| 486 |
&& strcmp(nvram_safe_get(apmode), "apstawet") |
|---|
| 487 |
&& strcmp(nvram_safe_get(apmode), "apsta")) { |
|---|
| 488 |
mode = "-A"; |
|---|
| 489 |
dd_syslog(LOG_INFO, |
|---|
| 490 |
"NAS : NAS lan (%s interface) successfully started\n", |
|---|
| 491 |
prefix); |
|---|
| 492 |
fnas = fopen("/tmp/.nas", "a"); |
|---|
| 493 |
fputc('L', fnas); // L as LAN |
|---|
| 494 |
fclose(fnas); |
|---|
| 495 |
} else { |
|---|
| 496 |
mode = "-S"; |
|---|
| 497 |
dd_syslog(LOG_INFO, |
|---|
| 498 |
"NAS : NAS wan (%s interface) successfully started\n", |
|---|
| 499 |
prefix); |
|---|
| 500 |
fnas = fopen("/tmp/.nas", "a"); |
|---|
| 501 |
fputc('W', fnas); // W as WAN |
|---|
| 502 |
fclose(fnas); |
|---|
| 503 |
} |
|---|
| 504 |
|
|---|
| 505 |
char rekey[32]; |
|---|
| 506 |
char ssid[32]; |
|---|
| 507 |
char radius[32]; |
|---|
| 508 |
char port[32]; |
|---|
| 509 |
char index[32]; |
|---|
| 510 |
|
|---|
| 511 |
sprintf(rekey, "%s_wpa_gtk_rekey", prefix); |
|---|
| 512 |
sprintf(ssid, "%s_ssid", prefix); |
|---|
| 513 |
sprintf(radius, "%s_radius_ipaddr", prefix); |
|---|
| 514 |
sprintf(port, "%s_radius_port", prefix); |
|---|
| 515 |
sprintf(index, "%s_key", prefix); |
|---|
| 516 |
|
|---|
| 517 |
key = getKey(prefix); |
|---|
| 518 |
|
|---|
| 519 |
{ |
|---|
| 520 |
// char *argv[] = {"nas", "-P", pidfile, "-l", |
|---|
| 521 |
// nvram_safe_get("lan_ifname"), "-H", "34954", "-i", iface, |
|---|
| 522 |
// mode, "-m", auth_mode, "-k", key, "-s", |
|---|
| 523 |
// nvram_safe_get("wl0_ssid"), "-w", sec_mode, "-g", |
|---|
| 524 |
// nvram_safe_get("wl0_wpa_gtk_rekey"), "-h", |
|---|
| 525 |
// nvram_safe_get("wl0_radius_ipaddr"), "-p", |
|---|
| 526 |
// nvram_safe_get("wl0_radius_port"), NULL}; |
|---|
| 527 |
pid_t pid; |
|---|
| 528 |
FILE *fp = { 0 }; |
|---|
| 529 |
if (!strcmp(mode, "-S")) { |
|---|
| 530 |
#ifndef HAVE_NASCONF |
|---|
| 531 |
char **argv; |
|---|
| 532 |
|
|---|
| 533 |
if (nvram_nmatch("wet", "%s_mode", prefix) |
|---|
| 534 |
|| nvram_nmatch("apstawet", "%s_mode", |
|---|
| 535 |
prefix)) { |
|---|
| 536 |
argv = (char *[]) { |
|---|
| 537 |
"nas", "-P", pidfile, "-H", |
|---|
| 538 |
"34954", "-l", |
|---|
| 539 |
getBridge(iface), |
|---|
| 540 |
"-i", iface, mode, |
|---|
| 541 |
"-m", auth_mode, |
|---|
| 542 |
"-k", key, "-s", |
|---|
| 543 |
nvram_safe_get |
|---|
| 544 |
(ssid), "-w", |
|---|
| 545 |
sec_mode, "-g", |
|---|
| 546 |
nvram_safe_get |
|---|
| 547 |
(rekey), NULL}; |
|---|
| 548 |
} else { |
|---|
| 549 |
argv = (char *[]) { |
|---|
| 550 |
"nas", "-P", pidfile, "-H", |
|---|
| 551 |
"34954", "-i", |
|---|
| 552 |
iface, mode, "-m", |
|---|
| 553 |
auth_mode, "-k", |
|---|
| 554 |
key, "-s", |
|---|
| 555 |
nvram_safe_get |
|---|
| 556 |
(ssid), "-w", |
|---|
| 557 |
sec_mode, "-g", |
|---|
| 558 |
nvram_safe_get |
|---|
| 559 |
(rekey), NULL}; |
|---|
| 560 |
|
|---|
| 561 |
} |
|---|
| 562 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 563 |
#else |
|---|
| 564 |
conf = fopen(conffile, "w"); |
|---|
| 565 |
fprintf(conf, |
|---|
| 566 |
"-H 34954 -i %s %s -m %s -k %s -s %s -w %s -g %s\n", |
|---|
| 567 |
iface, mode, auth_mode, key, |
|---|
| 568 |
nvram_safe_get(ssid), sec_mode, |
|---|
| 569 |
nvram_safe_get(rekey)); |
|---|
| 570 |
fclose(conf); |
|---|
| 571 |
char *argv[] = |
|---|
| 572 |
{ "nas", conffile, pidfile, "wan", NULL }; |
|---|
| 573 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 574 |
#endif |
|---|
| 575 |
} else { |
|---|
| 576 |
if (!strcmp(auth_mode, "2") |
|---|
| 577 |
|| !strcmp(auth_mode, "64") |
|---|
| 578 |
|| !strcmp(auth_mode, "66")) { |
|---|
| 579 |
#ifndef HAVE_NASCONF |
|---|
| 580 |
if (nvram_nmatch |
|---|
| 581 |
("0", "%s_bridged", iface)) { |
|---|
| 582 |
char *argv[] = |
|---|
| 583 |
{ "nas", "-P", pidfile, |
|---|
| 584 |
"-H", "34954", "-i", |
|---|
| 585 |
iface, mode, |
|---|
| 586 |
"-m", |
|---|
| 587 |
auth_mode, "-r", key, |
|---|
| 588 |
"-s", |
|---|
| 589 |
nvram_safe_get(ssid), |
|---|
| 590 |
"-w", |
|---|
| 591 |
sec_mode, "-g", |
|---|
| 592 |
nvram_safe_get |
|---|
| 593 |
(rekey), "-h", |
|---|
| 594 |
nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 595 |
// //radius |
|---|
| 596 |
// rekey |
|---|
| 597 |
// time |
|---|
| 598 |
NULL |
|---|
| 599 |
}; |
|---|
| 600 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 601 |
} else { |
|---|
| 602 |
char *argv[] = |
|---|
| 603 |
{ "nas", "-P", pidfile, |
|---|
| 604 |
"-H", "34954", "-l", |
|---|
| 605 |
getBridge(iface), "-i", |
|---|
| 606 |
iface, mode, "-m", |
|---|
| 607 |
auth_mode, "-r", key, |
|---|
| 608 |
"-s", |
|---|
| 609 |
nvram_safe_get(ssid), |
|---|
| 610 |
"-w", |
|---|
| 611 |
sec_mode, "-g", |
|---|
| 612 |
nvram_safe_get |
|---|
| 613 |
(rekey), "-h", |
|---|
| 614 |
nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 615 |
// //radius |
|---|
| 616 |
// rekey |
|---|
| 617 |
// time |
|---|
| 618 |
NULL |
|---|
| 619 |
}; |
|---|
| 620 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 621 |
} |
|---|
| 622 |
#else |
|---|
| 623 |
conf = fopen(conffile, "w"); |
|---|
| 624 |
fprintf(conf, |
|---|
| 625 |
"-H 34954 -l %s -i %s %s -m %s -r %s -s %s -w %s -g %s -h %s -p %s\n", |
|---|
| 626 |
getBridge(iface), iface, mode, |
|---|
| 627 |
auth_mode, key, |
|---|
| 628 |
nvram_safe_get(ssid), sec_mode, |
|---|
| 629 |
nvram_safe_get(rekey), |
|---|
| 630 |
nvram_safe_get(radius), |
|---|
| 631 |
nvram_safe_get(port)); |
|---|
| 632 |
fclose(conf); |
|---|
| 633 |
char *argv[] = |
|---|
| 634 |
{ "nas", conffile, pidfile, "lan", |
|---|
| 635 |
NULL |
|---|
| 636 |
}; |
|---|
| 637 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 638 |
#endif |
|---|
| 639 |
} else if (!strcmp(auth_mode, "32")) { |
|---|
| 640 |
int idx = atoi(nvram_safe_get(index)); |
|---|
| 641 |
char wepkey[32]; |
|---|
| 642 |
|
|---|
| 643 |
sprintf(wepkey, "%s_key%d", prefix, |
|---|
| 644 |
idx); |
|---|
| 645 |
#ifndef HAVE_NASCONF |
|---|
| 646 |
if (nvram_nmatch |
|---|
| 647 |
("0", "%s_bridged", iface)) { |
|---|
| 648 |
char *argv[] = |
|---|
| 649 |
{ "nas", "-P", pidfile, |
|---|
| 650 |
"-H", "34954", "-i", |
|---|
| 651 |
iface, mode, |
|---|
| 652 |
"-m", |
|---|
| 653 |
auth_mode, "-r", key, |
|---|
| 654 |
"-s", |
|---|
| 655 |
nvram_safe_get(ssid), |
|---|
| 656 |
"-w", |
|---|
| 657 |
sec_mode, "-I", |
|---|
| 658 |
nvram_safe_get |
|---|
| 659 |
(index), "-k", |
|---|
| 660 |
nvram_safe_get(wepkey), |
|---|
| 661 |
"-h", |
|---|
| 662 |
nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 663 |
// //radius |
|---|
| 664 |
// rekey |
|---|
| 665 |
// time |
|---|
| 666 |
NULL |
|---|
| 667 |
}; |
|---|
| 668 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 669 |
} else { |
|---|
| 670 |
char *argv[] = |
|---|
| 671 |
{ "nas", "-P", pidfile, |
|---|
| 672 |
"-H", "34954", "-l", |
|---|
| 673 |
getBridge(iface), "-i", |
|---|
| 674 |
iface, mode, "-m", |
|---|
| 675 |
auth_mode, "-r", key, |
|---|
| 676 |
"-s", |
|---|
| 677 |
nvram_safe_get(ssid), |
|---|
| 678 |
"-w", |
|---|
| 679 |
sec_mode, "-I", |
|---|
| 680 |
nvram_safe_get |
|---|
| 681 |
(index), "-k", |
|---|
| 682 |
nvram_safe_get(wepkey), |
|---|
| 683 |
"-h", |
|---|
| 684 |
nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 685 |
// //radius |
|---|
| 686 |
// rekey |
|---|
| 687 |
// time |
|---|
| 688 |
NULL |
|---|
| 689 |
}; |
|---|
| 690 |
|
|---|
| 691 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 692 |
|
|---|
| 693 |
} |
|---|
| 694 |
#else |
|---|
| 695 |
conf = fopen(conffile, "w"); |
|---|
| 696 |
fprintf(conf, |
|---|
| 697 |
"-H 34954 -l %s -i %s %s -m %s -r %s -s %s -w %s -I %s -k %s -h %s -p %s\n", |
|---|
| 698 |
getBridge(iface), iface, mode, |
|---|
| 699 |
auth_mode, key, |
|---|
| 700 |
nvram_safe_get(ssid), sec_mode, |
|---|
| 701 |
nvram_safe_get(index), |
|---|
| 702 |
nvram_safe_get(wepkey), |
|---|
| 703 |
nvram_safe_get(radius), |
|---|
| 704 |
nvram_safe_get(port)); |
|---|
| 705 |
fclose(conf); |
|---|
| 706 |
char *argv[] = |
|---|
| 707 |
{ "nas", conffile, pidfile, "lan", |
|---|
| 708 |
NULL |
|---|
| 709 |
}; |
|---|
| 710 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 711 |
#endif |
|---|
| 712 |
} else { |
|---|
| 713 |
#ifndef HAVE_NASCONF |
|---|
| 714 |
if (nvram_nmatch |
|---|
| 715 |
("0", "%s_bridged", iface)) { |
|---|
| 716 |
char *argv[] = |
|---|
| 717 |
{ "nas", "-P", pidfile, |
|---|
| 718 |
"-H", "34954", "-i", |
|---|
| 719 |
iface, mode, |
|---|
| 720 |
"-m", |
|---|
| 721 |
auth_mode, "-k", key, |
|---|
| 722 |
"-s", |
|---|
| 723 |
nvram_safe_get(ssid), |
|---|
| 724 |
"-w", |
|---|
| 725 |
sec_mode, "-g", |
|---|
| 726 |
nvram_safe_get(rekey), |
|---|
| 727 |
NULL |
|---|
| 728 |
}; |
|---|
| 729 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 730 |
} else { |
|---|
| 731 |
char *argv[] = |
|---|
| 732 |
{ "nas", "-P", pidfile, |
|---|
| 733 |
"-H", "34954", "-l", |
|---|
| 734 |
getBridge(iface), "-i", |
|---|
| 735 |
iface, mode, "-m", |
|---|
| 736 |
auth_mode, "-k", key, |
|---|
| 737 |
"-s", |
|---|
| 738 |
nvram_safe_get(ssid), |
|---|
| 739 |
"-w", |
|---|
| 740 |
sec_mode, "-g", |
|---|
| 741 |
nvram_safe_get(rekey), |
|---|
| 742 |
NULL |
|---|
| 743 |
}; |
|---|
| 744 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 745 |
} |
|---|
| 746 |
#else |
|---|
| 747 |
conf = fopen(conffile, "w"); |
|---|
| 748 |
fprintf(conf, |
|---|
| 749 |
"-H 34954 -l %s -i %s %s -m %s -k %s -s %s -w %s -g %s\n", |
|---|
| 750 |
getBridge(iface), iface, mode, |
|---|
| 751 |
auth_mode, key, |
|---|
| 752 |
nvram_safe_get(ssid), sec_mode, |
|---|
| 753 |
nvram_safe_get(rekey)); |
|---|
| 754 |
fclose(conf); |
|---|
| 755 |
char *argv[] = |
|---|
| 756 |
{ "nas", conffile, pidfile, "lan", |
|---|
| 757 |
NULL |
|---|
| 758 |
}; |
|---|
| 759 |
_evalpid(argv, NULL, 0, &pid); |
|---|
| 760 |
#endif |
|---|
| 761 |
} |
|---|
| 762 |
|
|---|
| 763 |
} |
|---|
| 764 |
|
|---|
| 765 |
fp = fopen(pidfile, "w"); |
|---|
| 766 |
if (fp) |
|---|
| 767 |
fprintf(fp, "%d", pid); |
|---|
| 768 |
fclose(fp); |
|---|
| 769 |
|
|---|
| 770 |
cprintf("done\n"); |
|---|
| 771 |
} |
|---|
| 772 |
return; |
|---|
| 773 |
} |
|---|
| 774 |
} |
|---|
| 775 |
|
|---|
| 776 |
void stop_nas(void) |
|---|
| 777 |
{ |
|---|
| 778 |
int ret = 0; |
|---|
| 779 |
|
|---|
| 780 |
unlink("/tmp/.nas"); |
|---|
| 781 |
|
|---|
| 782 |
led_control(LED_SEC0, LED_OFF); |
|---|
| 783 |
led_control(LED_SEC1, LED_OFF); |
|---|
| 784 |
|
|---|
| 785 |
if (pidof("nas") > 0) |
|---|
| 786 |
dd_syslog(LOG_INFO, "NAS : NAS daemon successfully stopped\n"); |
|---|
| 787 |
|
|---|
| 788 |
if (pidof("wrt-radauth") > 0) { |
|---|
| 789 |
dd_syslog(LOG_INFO, |
|---|
| 790 |
"RADAUTH : RADAUTH daemon successfully stopped\n"); |
|---|
| 791 |
killall("wrt-radauth", SIGKILL); |
|---|
| 792 |
} |
|---|
| 793 |
|
|---|
| 794 |
int deadcount = 0; |
|---|
| 795 |
|
|---|
| 796 |
while (pidof("nas") > 0 && (deadcount++) < 8) { |
|---|
| 797 |
/* |
|---|
| 798 |
* NAS sometimes won't exit properly on a normal kill |
|---|
| 799 |
*/ |
|---|
| 800 |
// int ret = killps("nas",NULL); |
|---|
| 801 |
ret = killall("nas", SIGTERM); |
|---|
| 802 |
sleep(2); |
|---|
| 803 |
// killps("nas","-9"); |
|---|
| 804 |
killall("nas", SIGKILL); |
|---|
| 805 |
} |
|---|
| 806 |
#ifdef HAVE_WPA_SUPPLICANT |
|---|
| 807 |
killall("wpa_supplicant", SIGKILL); |
|---|
| 808 |
#endif |
|---|
| 809 |
// clean |
|---|
| 810 |
unlink("/tmp/nas.wl0wan.pid"); |
|---|
| 811 |
unlink("/tmp/nas.wl0lan.pid"); |
|---|
| 812 |
unlink("/tmp/nas.wl1wan.pid"); |
|---|
| 813 |
unlink("/tmp/nas.wl1lan.pid"); |
|---|
| 814 |
#ifdef HAVE_NASCONF |
|---|
| 815 |
unlink("/tmp/nas.wl0wan.conf"); |
|---|
| 816 |
unlink("/tmp/nas.wl0lan.conf"); |
|---|
| 817 |
unlink("/tmp/nas.wl1wan.conf"); |
|---|
| 818 |
unlink("/tmp/nas.wl1lan.conf"); |
|---|
| 819 |
#endif |
|---|
| 820 |
unlink("/tmp/nas.wl0.1lan.pid"); |
|---|
| 821 |
unlink("/tmp/nas.wl0.2lan.pid"); |
|---|
| 822 |
unlink("/tmp/nas.wl0.3lan.pid"); |
|---|
| 823 |
unlink("/tmp/nas.wl1.1lan.pid"); |
|---|
| 824 |
unlink("/tmp/nas.wl1.2lan.pid"); |
|---|
| 825 |
unlink("/tmp/nas.wl1.3lan.pid"); |
|---|
| 826 |
#ifdef HAVE_NASCONF |
|---|
| 827 |
unlink("/tmp/nas.wl0.1lan.conf"); |
|---|
| 828 |
unlink("/tmp/nas.wl0.2lan.conf"); |
|---|
| 829 |
unlink("/tmp/nas.wl0.3lan.conf"); |
|---|
| 830 |
unlink("/tmp/nas.wl1.1lan.conf"); |
|---|
| 831 |
unlink("/tmp/nas.wl1.2lan.conf"); |
|---|
| 832 |
unlink("/tmp/nas.wl1.3lan.conf"); |
|---|
| 833 |
#endif |
|---|
| 834 |
|
|---|
| 835 |
cprintf("done\n"); |
|---|
| 836 |
return; |
|---|
| 837 |
} |
|---|
| 838 |
#endif |
|---|