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