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; }
/* * Called when link comes up */ int ipup_main(int argc, char **argv) { FILE *fp; char *wan_ifname; char *value; char buf[256]; char tmp[100], prefix[WAN_PREFIX_SZ]; if (ppp_prefix(&wan_ifname, prefix) < 0) return -1; umask(022); /* Touch connection file */ if (!(fp = fopen(strcat_r("/tmp/ppp/link.", wan_ifname, tmp), "a"))) { perror(tmp); return errno; } fclose(fp); if ((value = getenv("IPLOCAL"))) { if (nvram_invmatch(strcat_r(prefix, "ipaddr", tmp), value)) ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL); _ifconfig(wan_ifname, IFUP, value, "255.255.255.255", getenv("IPREMOTE")); nvram_set(strcat_r(prefix, "ipaddr", tmp), value); nvram_set(strcat_r(prefix, "netmask", tmp), "255.255.255.255"); } if ((value = getenv("IPREMOTE"))) nvram_set(strcat_r(prefix, "gateway", tmp), value); strcpy(buf, ""); if (getenv("DNS1")) sprintf(buf, "%s", getenv("DNS1")); if (getenv("DNS2")) sprintf(buf + strlen(buf), "%s%s", strlen(buf) ? " " : "", getenv("DNS2")); nvram_set(strcat_r(prefix, "dns", tmp), buf); wan_up(wan_ifname); logmessage(nvram_safe_get("wan_proto_t"), "connected to ISP"); wanmessage(""); dprintf("done\n"); return 0; }
/* * deconfig: This argument is used when udhcpc starts, and when a * leases is lost. The script should put the interface in an up, but * deconfigured state. */ static int deconfig(void) { char *wan_ifname = safe_getenv("interface"); if (nvram_match("wan0_proto", "l2tp") || nvram_match("wan0_proto", "pptp")) { /* fix hang-up issue */ logmessage("dhcp client", "skipping resetting IP address to 0.0.0.0"); } else ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL); expires(wan_ifname, 0); wan_down(wan_ifname); logmessage("dhcp client", "%s: lease is lost", udhcpstate); wanmessage("lost IP from server"); return 0; }
/* * Called when link goes down */ int ipdown_main(int argc, char **argv) { char *wan_ifname; char tmp[100], prefix[WAN_PREFIX_SZ]; if (ppp_prefix(&wan_ifname, prefix) < 0) return -1; umask(022); wan_down(wan_ifname); unlink(strcat_r("/tmp/ppp/link.", wan_ifname, tmp)); preset_wan_routes(wan_ifname); logmessage(nvram_safe_get("wan_proto_t"), "Disconnected"); wanmessage(pppstatus(tmp)); return 0; }