| 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_nmatch("psk", "wl%d_security_mode", instance) |
|---|
| 174 | || nvram_nmatch("psk2", "wl%d_security_mode", instance)) { |
|---|
| 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_nget("wl%d_crypto", instance), |
|---|
| 184 | nvram_nget("wl%d_security_mode", instance), |
|---|
| 185 | nvram_nget("wl%d_ssid", instance), |
|---|
| 186 | nvram_nget("wl%d_wpa_psk", instance)); |
|---|
| 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 & WL_RADIO_SW_DISABLE == 0) //radio turned on - ready |
|---|
| 364 | break; |
|---|
| 365 | sleep (1); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | if (radiostate & WL_RADIO_SW_DISABLE != 0) // radio turned off |
|---|
| 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 | { |
|---|
| 429 | led_control(LED_SEC0, LED_OFF); |
|---|
| 430 | convert_wds(0); |
|---|
| 431 | } |
|---|
| 432 | if (!strcmp(prefix, "wl1")) |
|---|
| 433 | { |
|---|
| 434 | led_control(LED_SEC1, LED_OFF); |
|---|
| 435 | convert_wds(1); |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | snprintf(pidfile, sizeof(pidfile), "/tmp/nas.%s%s.pid", prefix, |
|---|
| 440 | type); |
|---|
| 441 | #ifdef HAVE_NASCONF |
|---|
| 442 | snprintf(conffile, sizeof(conffile), "/tmp/nas.%s%s.conf", |
|---|
| 443 | prefix, type); |
|---|
| 444 | #endif |
|---|
| 445 | |
|---|
| 446 | char apmode[32]; |
|---|
| 447 | |
|---|
| 448 | sprintf(apmode, "%s_mode", prefix); |
|---|
| 449 | if (!strcmp(type, "wan") && nvram_match(apmode, "ap")) { |
|---|
| 450 | return; |
|---|
| 451 | } |
|---|
| 452 | // if (!strcmp (type, "lan")) |
|---|
| 453 | // iface = "br0"; |
|---|
| 454 | // else |
|---|
| 455 | |
|---|
| 456 | if (0 == type || 0 == *type) |
|---|
| 457 | type = "lan"; |
|---|
| 458 | if (!strcmp(type, "lan") && nvram_invmatch(apmode, "ap")) |
|---|
| 459 | iface = "br0"; |
|---|
| 460 | else { |
|---|
| 461 | |
|---|
| 462 | if (!strcmp(prefix, "wl0")) { |
|---|
| 463 | iface = get_wl_instance_name(0); |
|---|
| 464 | } else if (!strcmp(prefix, "wl1")) { |
|---|
| 465 | iface = get_wl_instance_name(1); |
|---|
| 466 | } else { |
|---|
| 467 | iface = prefix; |
|---|
| 468 | } |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | sec_mode = getSecMode(prefix); |
|---|
| 472 | auth_mode = getAuthMode(prefix); |
|---|
| 473 | |
|---|
| 474 | if (strcmp(sec_mode, "0")) { |
|---|
| 475 | if (!strcmp(prefix, "wl0")) |
|---|
| 476 | led_control(LED_SEC0, LED_ON); |
|---|
| 477 | if (!strcmp(prefix, "wl1")) |
|---|
| 478 | led_control(LED_SEC1, LED_ON); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | if (auth_mode == NULL) |
|---|
| 482 | return; // no nas required |
|---|
| 483 | if (strcmp(nvram_safe_get(apmode), "sta") |
|---|
| 484 | && strcmp(nvram_safe_get(apmode), "wet") |
|---|
| 485 | && strcmp(nvram_safe_get(apmode), "apstawet") |
|---|
| 486 | && strcmp(nvram_safe_get(apmode), "apsta")) { |
|---|
| 487 | mode = "-A"; |
|---|
| 488 | dd_syslog(LOG_INFO, |
|---|
| 489 | "NAS : NAS lan (%s interface) successfully started\n", |
|---|
| 490 | prefix); |
|---|
| 491 | fnas = fopen("/tmp/.nas", "a"); |
|---|
| 492 | fputc('L', fnas); // L as LAN |
|---|
| 493 | fclose(fnas); |
|---|
| 494 | } else { |
|---|
| 495 | mode = "-S"; |
|---|
| 496 | dd_syslog(LOG_INFO, |
|---|
| 497 | "NAS : NAS wan (%s interface) successfully started\n", |
|---|
| 498 | prefix); |
|---|
| 499 | fnas = fopen("/tmp/.nas", "a"); |
|---|
| 500 | fputc('W', fnas); // W as WAN |
|---|
| 501 | fclose(fnas); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | char rekey[32]; |
|---|
| 505 | char ssid[32]; |
|---|
| 506 | char radius[32]; |
|---|
| 507 | char port[32]; |
|---|
| 508 | char index[32]; |
|---|
| 509 | |
|---|
| 510 | sprintf(rekey, "%s_wpa_gtk_rekey", prefix); |
|---|
| 511 | sprintf(ssid, "%s_ssid", prefix); |
|---|
| 512 | sprintf(radius, "%s_radius_ipaddr", prefix); |
|---|
| 513 | sprintf(port, "%s_radius_port", prefix); |
|---|
| 514 | sprintf(index, "%s_key", prefix); |
|---|
| 515 | |
|---|
| 516 | key = getKey(prefix); |
|---|
| 517 | |
|---|
| 518 | { |
|---|
| 519 | // char *argv[] = {"nas", "-P", pidfile, "-l", |
|---|
| 520 | // nvram_safe_get("lan_ifname"), "-H", "34954", "-i", iface, |
|---|
| 521 | // mode, "-m", auth_mode, "-k", key, "-s", |
|---|
| 522 | // nvram_safe_get("wl0_ssid"), "-w", sec_mode, "-g", |
|---|
| 523 | // nvram_safe_get("wl0_wpa_gtk_rekey"), "-h", |
|---|
| 524 | // nvram_safe_get("wl0_radius_ipaddr"), "-p", |
|---|
| 525 | // nvram_safe_get("wl0_radius_port"), NULL}; |
|---|
| 526 | pid_t pid; |
|---|
| 527 | FILE *fp = { 0 }; |
|---|
| 528 | if (!strcmp(mode, "-S")) { |
|---|
| 529 | #ifndef HAVE_NASCONF |
|---|
| 530 | char **argv; |
|---|
| 531 | |
|---|
| 532 | if (nvram_nmatch("wet", "%s_mode", prefix) |
|---|
| 533 | || nvram_nmatch("apstawet", "%s_mode", |
|---|
| 534 | prefix)) { |
|---|
| 535 | argv = (char *[]) { |
|---|
| 536 | "nas", "-P", pidfile, "-H", |
|---|
| 537 | "34954", "-l", |
|---|
| 538 | getBridge(iface), |
|---|
| 539 | "-i", iface, mode, |
|---|
| 540 | "-m", auth_mode, |
|---|
| 541 | "-k", key, "-s", |
|---|
| 542 | nvram_safe_get |
|---|
| 543 | (ssid), "-w", |
|---|
| 544 | sec_mode, "-g", |
|---|
| 545 | nvram_safe_get |
|---|
| 546 | (rekey), NULL}; |
|---|
| 547 | } else { |
|---|
| 548 | argv = (char *[]) { |
|---|
| 549 | "nas", "-P", pidfile, "-H", |
|---|
| 550 | "34954", "-i", |
|---|
| 551 | iface, mode, "-m", |
|---|
| 552 | auth_mode, "-k", |
|---|
| 553 | key, "-s", |
|---|
| 554 | nvram_safe_get |
|---|
| 555 | (ssid), "-w", |
|---|
| 556 | sec_mode, "-g", |
|---|
| 557 | nvram_safe_get |
|---|
| 558 | (rekey), NULL}; |
|---|
| 559 | |
|---|
| 560 | } |
|---|
| 561 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 562 | #else |
|---|
| 563 | conf = fopen(conffile, "w"); |
|---|
| 564 | fprintf(conf, |
|---|
| 565 | "-H 34954 -i %s %s -m %s -k %s -s %s -w %s -g %s\n", |
|---|
| 566 | iface, mode, auth_mode, key, |
|---|
| 567 | nvram_safe_get(ssid), sec_mode, |
|---|
| 568 | nvram_safe_get(rekey)); |
|---|
| 569 | fclose(conf); |
|---|
| 570 | char *argv[] = |
|---|
| 571 | { "nas", conffile, pidfile, "wan", NULL }; |
|---|
| 572 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 573 | #endif |
|---|
| 574 | } else { |
|---|
| 575 | if (!strcmp(auth_mode, "2") |
|---|
| 576 | || !strcmp(auth_mode, "64") |
|---|
| 577 | || !strcmp(auth_mode, "66")) { |
|---|
| 578 | #ifndef HAVE_NASCONF |
|---|
| 579 | if (nvram_nmatch |
|---|
| 580 | ("0", "%s_bridged", iface)) { |
|---|
| 581 | char *argv[] = |
|---|
| 582 | { "nas", "-P", pidfile, |
|---|
| 583 | "-H", "34954", "-i", |
|---|
| 584 | iface, mode, |
|---|
| 585 | "-m", |
|---|
| 586 | auth_mode, "-r", key, |
|---|
| 587 | "-s", |
|---|
| 588 | nvram_safe_get(ssid), |
|---|
| 589 | "-w", |
|---|
| 590 | sec_mode, "-g", |
|---|
| 591 | nvram_safe_get |
|---|
| 592 | (rekey), "-h", |
|---|
| 593 | nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 594 | // //radius |
|---|
| 595 | // rekey |
|---|
| 596 | // time |
|---|
| 597 | NULL |
|---|
| 598 | }; |
|---|
| 599 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 600 | } else { |
|---|
| 601 | char *argv[] = |
|---|
| 602 | { "nas", "-P", pidfile, |
|---|
| 603 | "-H", "34954", "-l", |
|---|
| 604 | getBridge(iface), "-i", |
|---|
| 605 | iface, mode, "-m", |
|---|
| 606 | auth_mode, "-r", key, |
|---|
| 607 | "-s", |
|---|
| 608 | nvram_safe_get(ssid), |
|---|
| 609 | "-w", |
|---|
| 610 | sec_mode, "-g", |
|---|
| 611 | nvram_safe_get |
|---|
| 612 | (rekey), "-h", |
|---|
| 613 | nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 614 | // //radius |
|---|
| 615 | // rekey |
|---|
| 616 | // time |
|---|
| 617 | NULL |
|---|
| 618 | }; |
|---|
| 619 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 620 | } |
|---|
| 621 | #else |
|---|
| 622 | conf = fopen(conffile, "w"); |
|---|
| 623 | fprintf(conf, |
|---|
| 624 | "-H 34954 -l %s -i %s %s -m %s -r %s -s %s -w %s -g %s -h %s -p %s\n", |
|---|
| 625 | getBridge(iface), iface, mode, |
|---|
| 626 | auth_mode, key, |
|---|
| 627 | nvram_safe_get(ssid), sec_mode, |
|---|
| 628 | nvram_safe_get(rekey), |
|---|
| 629 | nvram_safe_get(radius), |
|---|
| 630 | nvram_safe_get(port)); |
|---|
| 631 | fclose(conf); |
|---|
| 632 | char *argv[] = |
|---|
| 633 | { "nas", conffile, pidfile, "lan", |
|---|
| 634 | NULL |
|---|
| 635 | }; |
|---|
| 636 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 637 | #endif |
|---|
| 638 | } else if (!strcmp(auth_mode, "32")) { |
|---|
| 639 | int idx = atoi(nvram_safe_get(index)); |
|---|
| 640 | char wepkey[32]; |
|---|
| 641 | |
|---|
| 642 | sprintf(wepkey, "%s_key%d", prefix, |
|---|
| 643 | idx); |
|---|
| 644 | #ifndef HAVE_NASCONF |
|---|
| 645 | if (nvram_nmatch |
|---|
| 646 | ("0", "%s_bridged", iface)) { |
|---|
| 647 | char *argv[] = |
|---|
| 648 | { "nas", "-P", pidfile, |
|---|
| 649 | "-H", "34954", "-i", |
|---|
| 650 | iface, mode, |
|---|
| 651 | "-m", |
|---|
| 652 | auth_mode, "-r", key, |
|---|
| 653 | "-s", |
|---|
| 654 | nvram_safe_get(ssid), |
|---|
| 655 | "-w", |
|---|
| 656 | sec_mode, "-I", |
|---|
| 657 | nvram_safe_get |
|---|
| 658 | (index), "-k", |
|---|
| 659 | nvram_safe_get(wepkey), |
|---|
| 660 | "-h", |
|---|
| 661 | nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 662 | // //radius |
|---|
| 663 | // rekey |
|---|
| 664 | // time |
|---|
| 665 | NULL |
|---|
| 666 | }; |
|---|
| 667 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 668 | } else { |
|---|
| 669 | char *argv[] = |
|---|
| 670 | { "nas", "-P", pidfile, |
|---|
| 671 | "-H", "34954", "-l", |
|---|
| 672 | getBridge(iface), "-i", |
|---|
| 673 | iface, mode, "-m", |
|---|
| 674 | auth_mode, "-r", key, |
|---|
| 675 | "-s", |
|---|
| 676 | nvram_safe_get(ssid), |
|---|
| 677 | "-w", |
|---|
| 678 | sec_mode, "-I", |
|---|
| 679 | nvram_safe_get |
|---|
| 680 | (index), "-k", |
|---|
| 681 | nvram_safe_get(wepkey), |
|---|
| 682 | "-h", |
|---|
| 683 | nvram_safe_get(radius), "-p", nvram_safe_get(port), // "-t", |
|---|
| 684 | // //radius |
|---|
| 685 | // rekey |
|---|
| 686 | // time |
|---|
| 687 | NULL |
|---|
| 688 | }; |
|---|
| 689 | |
|---|
| 690 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 691 | |
|---|
| 692 | } |
|---|
| 693 | #else |
|---|
| 694 | conf = fopen(conffile, "w"); |
|---|
| 695 | fprintf(conf, |
|---|
| 696 | "-H 34954 -l %s -i %s %s -m %s -r %s -s %s -w %s -I %s -k %s -h %s -p %s\n", |
|---|
| 697 | getBridge(iface), iface, mode, |
|---|
| 698 | auth_mode, key, |
|---|
| 699 | nvram_safe_get(ssid), sec_mode, |
|---|
| 700 | nvram_safe_get(index), |
|---|
| 701 | nvram_safe_get(wepkey), |
|---|
| 702 | nvram_safe_get(radius), |
|---|
| 703 | nvram_safe_get(port)); |
|---|
| 704 | fclose(conf); |
|---|
| 705 | char *argv[] = |
|---|
| 706 | { "nas", conffile, pidfile, "lan", |
|---|
| 707 | NULL |
|---|
| 708 | }; |
|---|
| 709 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 710 | #endif |
|---|
| 711 | } else { |
|---|
| 712 | #ifndef HAVE_NASCONF |
|---|
| 713 | if (nvram_nmatch |
|---|
| 714 | ("0", "%s_bridged", iface)) { |
|---|
| 715 | char *argv[] = |
|---|
| 716 | { "nas", "-P", pidfile, |
|---|
| 717 | "-H", "34954", "-i", |
|---|
| 718 | iface, mode, |
|---|
| 719 | "-m", |
|---|
| 720 | auth_mode, "-k", key, |
|---|
| 721 | "-s", |
|---|
| 722 | nvram_safe_get(ssid), |
|---|
| 723 | "-w", |
|---|
| 724 | sec_mode, "-g", |
|---|
| 725 | nvram_safe_get(rekey), |
|---|
| 726 | NULL |
|---|
| 727 | }; |
|---|
| 728 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 729 | } else { |
|---|
| 730 | char *argv[] = |
|---|
| 731 | { "nas", "-P", pidfile, |
|---|
| 732 | "-H", "34954", "-l", |
|---|
| 733 | getBridge(iface), "-i", |
|---|
| 734 | iface, mode, "-m", |
|---|
| 735 | auth_mode, "-k", key, |
|---|
| 736 | "-s", |
|---|
| 737 | nvram_safe_get(ssid), |
|---|
| 738 | "-w", |
|---|
| 739 | sec_mode, "-g", |
|---|
| 740 | nvram_safe_get(rekey), |
|---|
| 741 | NULL |
|---|
| 742 | }; |
|---|
| 743 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 744 | } |
|---|
| 745 | #else |
|---|
| 746 | conf = fopen(conffile, "w"); |
|---|
| 747 | fprintf(conf, |
|---|
| 748 | "-H 34954 -l %s -i %s %s -m %s -k %s -s %s -w %s -g %s\n", |
|---|
| 749 | getBridge(iface), iface, mode, |
|---|
| 750 | auth_mode, key, |
|---|
| 751 | nvram_safe_get(ssid), sec_mode, |
|---|
| 752 | nvram_safe_get(rekey)); |
|---|
| 753 | fclose(conf); |
|---|
| 754 | char *argv[] = |
|---|
| 755 | { "nas", conffile, pidfile, "lan", |
|---|
| 756 | NULL |
|---|
| 757 | }; |
|---|
| 758 | _evalpid(argv, NULL, 0, &pid); |
|---|
| 759 | #endif |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | fp = fopen(pidfile, "w"); |
|---|
| 765 | if (fp) |
|---|
| 766 | fprintf(fp, "%d", pid); |
|---|
| 767 | fclose(fp); |
|---|
| 768 | |
|---|
| 769 | cprintf("done\n"); |
|---|
| 770 | } |
|---|
| 771 | return; |
|---|
| 772 | |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | void stop_nas(void) |
|---|
| 776 | { |
|---|
| 777 | int ret = 0; |
|---|
| 778 | |
|---|
| 779 | unlink("/tmp/.nas"); |
|---|
| 780 | |
|---|
| 781 | led_control(LED_SEC0, LED_OFF); |
|---|
| 782 | led_control(LED_SEC1, LED_OFF); |
|---|
| 783 | |
|---|
| 784 | if (pidof("nas") > 0) |
|---|
| 785 | dd_syslog(LOG_INFO, "NAS : NAS daemon successfully stopped\n"); |
|---|
| 786 | |
|---|
| 787 | if (pidof("wrt-radauth") > 0) { |
|---|
| 788 | dd_syslog(LOG_INFO, |
|---|
| 789 | "RADAUTH : RADAUTH daemon successfully stopped\n"); |
|---|
| 790 | killall("wrt-radauth", SIGKILL); |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | int deadcount = 0; |
|---|
| 794 | |
|---|
| 795 | while (pidof("nas") > 0 && (deadcount++) < 8) { |
|---|
| 796 | /* |
|---|
| 797 | * NAS sometimes won't exit properly on a normal kill |
|---|
| 798 | */ |
|---|
| 799 | // int ret = killps("nas",NULL); |
|---|
| 800 | ret = killall("nas", SIGTERM); |
|---|
| 801 | sleep(2); |
|---|
| 802 | // killps("nas","-9"); |
|---|
| 803 | killall("nas", SIGKILL); |
|---|
| 804 | } |
|---|
| 805 | #ifdef HAVE_WPA_SUPPLICANT |
|---|
| 806 | killall("wpa_supplicant", SIGKILL); |
|---|
| 807 | #endif |
|---|
| 808 | // clean |
|---|
| 809 | unlink("/tmp/nas.wl0wan.pid"); |
|---|
| 810 | unlink("/tmp/nas.wl0lan.pid"); |
|---|
| 811 | unlink("/tmp/nas.wl1wan.pid"); |
|---|
| 812 | unlink("/tmp/nas.wl1lan.pid"); |
|---|
| 813 | #ifdef HAVE_NASCONF |
|---|
| 814 | unlink("/tmp/nas.wl0wan.conf"); |
|---|
| 815 | unlink("/tmp/nas.wl0lan.conf"); |
|---|
| 816 | unlink("/tmp/nas.wl1wan.conf"); |
|---|
| 817 | unlink("/tmp/nas.wl1lan.conf"); |
|---|
| 818 | #endif |
|---|
| 819 | unlink("/tmp/nas.wl0.1lan.pid"); |
|---|
| 820 | unlink("/tmp/nas.wl0.2lan.pid"); |
|---|
| 821 | unlink("/tmp/nas.wl0.3lan.pid"); |
|---|
| 822 | unlink("/tmp/nas.wl1.1lan.pid"); |
|---|
| 823 | unlink("/tmp/nas.wl1.2lan.pid"); |
|---|
| 824 | unlink("/tmp/nas.wl1.3lan.pid"); |
|---|
| 825 | #ifdef HAVE_NASCONF |
|---|
| 826 | unlink("/tmp/nas.wl0.1lan.conf"); |
|---|
| 827 | unlink("/tmp/nas.wl0.2lan.conf"); |
|---|
| 828 | unlink("/tmp/nas.wl0.3lan.conf"); |
|---|
| 829 | unlink("/tmp/nas.wl1.1lan.conf"); |
|---|
| 830 | unlink("/tmp/nas.wl1.2lan.conf"); |
|---|
| 831 | unlink("/tmp/nas.wl1.3lan.conf"); |
|---|
| 832 | #endif |
|---|
| 833 | |
|---|
| 834 | cprintf("done\n"); |
|---|
| 835 | return; |
|---|
| 836 | } |
|---|
| 837 | #endif |
|---|