static int bound(void) // udhcpc bound here, also call wanup { char *wan_ifname = safe_getenv("interface"); char *value; char tmp[100], prefix[] = "wanXXXXXXXXXX_"; int unit; if ((unit = wan_ifunit(wan_ifname)) < 0) strcpy(prefix, "wanx_"); else snprintf(prefix, sizeof(prefix), "wan%d_", unit); if ((value = getenv("ip"))) nvram_set(strcat_r(prefix, "ipaddr", tmp), trim_r(value)); if ((value = getenv("subnet"))) nvram_set(strcat_r(prefix, "netmask", tmp), trim_r(value)); if ((value = getenv("router"))) nvram_set(strcat_r(prefix, "gateway", tmp), trim_r(value)); if ((value = getenv("dns"))) nvram_set(strcat_r(prefix, "dns", tmp), trim_r(value)); if ((value = getenv("wins"))) nvram_set(strcat_r(prefix, "wins", tmp), trim_r(value)); nvram_set(strcat_r(prefix, "routes", tmp), getenv("routes")); nvram_set(strcat_r(prefix, "msroutes", tmp), getenv("msroutes")); #if 0 if ((value = getenv("hostname"))) sethostname(trim_r(value), strlen(value) + 1); #endif if ((value = getenv("domain"))) nvram_set(strcat_r(prefix, "domain", tmp), trim_r(value)); if ((value = getenv("lease"))) { nvram_set(strcat_r(prefix, "lease", tmp), trim_r(value)); expires(wan_ifname, atoi(value)); } ifconfig(wan_ifname, IFUP, nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), nvram_safe_get(strcat_r(prefix, "netmask", tmp))); spinlock_lock(SPINLOCK_DHCPRenew); nvram_set("dhcp_renew", "0"); // for detectWAN spinlock_unlock(SPINLOCK_DHCPRenew); wan_up(wan_ifname); logmessage("dhcp client", "%s IP: %s from %s (prefix: %s)", udhcpstate, nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), nvram_safe_get(strcat_r(prefix, "gateway", tmp)), prefix); wanmessage(""); dprintf("done\n"); return 0; }
/* * renew: This argument is used when a DHCP lease is renewed. All of * the paramaters are set in enviromental variables. This argument is * used when the interface is already configured, so the IP address, * will not change, however, the other DHCP paramaters, such as the * default gateway, subnet mask, and dns server may change. */ static int renew(void) { char *wan_ifname = safe_getenv("interface"); char *value; char tmp[100], prefix[] = "wanXXXXXXXXXX_"; int unit; if ((unit = wan_ifunit(wan_ifname)) < 0) strcpy(prefix, "wanx_"); else snprintf(prefix, sizeof(prefix), "wan%d_", unit); if (!(value = getenv("subnet")) || !nvram_match(strcat_r(prefix, "netmask", tmp), trim_r(value))) return bound(); if (!(value = getenv("router")) || !nvram_match(strcat_r(prefix, "gateway", tmp), trim_r(value))) return bound(); if ((value = getenv("dns")) && !nvram_match(strcat_r(prefix, "dns", tmp), trim_r(value))) { nvram_set(strcat_r(prefix, "dns", tmp), trim_r(value)); #if 0 update_resolvconf(); #else add_dns(wan_ifname); #endif } if ((value = getenv("wins"))) nvram_set(strcat_r(prefix, "wins", tmp), trim_r(value)); #if 0 if ((value = getenv("hostname"))) sethostname(trim_r(value), strlen(value) + 1); #endif if ((value = getenv("domain"))) nvram_set(strcat_r(prefix, "domain", tmp), trim_r(value)); if ((value = getenv("lease"))) { nvram_set(strcat_r(prefix, "lease", tmp), trim_r(value)); expires(wan_ifname, atoi(value)); } logmessage("dhcp client", "%s IP: %s from %s (prefix: %s)", udhcpstate, nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), nvram_safe_get(strcat_r(prefix, "gateway", tmp)), prefix); if (unit == 0) update_wan_status(1); wanmessage(""); dprintf("done\n"); return 0; }
/* * bound: This argument is used when udhcpc moves from an unbound, to * a bound state. All of the paramaters are set in enviromental * variables, The script should configure the interface, and set any * other relavent parameters (default gateway, dns server, etc). */ static int bound(void) { char *wan_ifname = safe_getenv("interface"); char *value; char tmp[100], prefix[] = "wanXXXXXXXXXX_"; int unit; if ((unit = wan_ifunit(wan_ifname)) < 0) return -1; snprintf(prefix, sizeof(prefix), "wan%d_", unit); if ((value = getenv("ip"))) nvram_set(strcat_r(prefix, "ipaddr", tmp), value); if ((value = getenv("subnet"))) nvram_set(strcat_r(prefix, "netmask", tmp), value); if ((value = getenv("router"))) nvram_set(strcat_r(prefix, "gateway", tmp), value); if ((value = getenv("dns"))) nvram_set(strcat_r(prefix, "dns", tmp), value); if ((value = getenv("wins"))) nvram_set(strcat_r(prefix, "wins", tmp), value); if ((value = getenv("hostname"))) sethostname(value, strlen(value) + 1); if ((value = getenv("domain"))) nvram_set(strcat_r(prefix, "domain", tmp), value); if ((value = getenv("lease"))) { nvram_set(strcat_r(prefix, "lease", tmp), value); expires(wan_ifname, atoi(value)); } ifconfig(wan_ifname, IFUP, nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), nvram_safe_get(strcat_r(prefix, "netmask", tmp))); wan_up(wan_ifname); dprintf("done\n"); return 0; }
static int expires(char *wan_ifname, unsigned int in) { time_t now; FILE *fp; char tmp[100]; int unit; if ((unit = wan_ifunit(wan_ifname)) < 0) return -1; time(&now); snprintf(tmp, sizeof(tmp), "/tmp/udhcpc%d.expires", unit); if (!(fp = fopen(tmp, "w"))) { perror(tmp); return errno; } fprintf(fp, "%d", (unsigned int) now + in); fclose(fp); return 0; }
int wpacli_main(int argc, char **argv) { char tmp[100]; char prefix[] = "wanXXXXXXXXXX_"; int unit; if (argc < 3 || (unit = wan_ifunit(argv[1])) < 0) return EINVAL; snprintf(prefix, sizeof(prefix), "wan%d_", unit); if (!nvram_match(strcat_r(prefix, "auth_x", tmp), "8021x-md5")) return 0; if (strncmp(argv[2], "CONNECTED", sizeof("CONNECTED")) == 0) { /* Called only once, no action */ } if (strncmp(argv[2], "EAP-SUCCESS", sizeof("EAP-SUCCESS")) == 0) { /* Called multiple times on success (re)auth */ logmessage("802.1x client", "authenticated"); /* TODO: refresh dhcp? */ } else if (strncmp(argv[2], "EAP-FAILURE", sizeof("EAP-FAILURE")) == 0) { /* Called every/multiple times on (re)auth fail and/or timeout */ if (nvram_get_int(strcat_r(prefix, "state_t", tmp)) != WAN_STATE_STOPPED) logmessage("802.1x client", "authentication failed"); /* Reuse auth-fail state */ update_wan_state(prefix, WAN_STATE_STOPPED, WAN_STOPPED_REASON_PPP_AUTH_FAIL); } if (strncmp(argv[2], "DISCONNECTED", sizeof("DISCONNECTED")) == 0) { /* Called only once, no action */ } return 0; }