int ipdown_main(int argc, char **argv) { int proto; TRACE_PT("begin\n"); if (!wait_action_idle(10)) return -1; stop_ddns(); // avoid to trigger DOD stop_ntpc(); unlink("/tmp/ppp/link"); proto = get_wan_proto(); if (proto == WP_L2TP || proto == WP_PPTP) { /* clear dns from the resolv.conf */ nvram_set("wan_get_dns",""); dns_to_resolv(); if (proto == WP_L2TP) { route_del(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway"), "255.255.255.255"); // fixed routing problem in Israel by kanki } // Restore the default gateway for WAN interface nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway")); // Set default route to gateway if specified route_del(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0"); route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0"); } if (nvram_get_int("ppp_demand")) { killall("listen", SIGKILL); eval("listen", nvram_safe_get("lan_ifname")); } TRACE_PT("end\n"); return 1; }
void handle_notifications(void) { int i, stop_handle = 0; char notify_name[256]; DIR *directory = opendir(DIR_RC_NOTIFY); if (!directory) return; // handle max 10 requests at once (prevent deadlock) for (i=0; i < 10; i++) { struct dirent *entry; FILE *test_fp; entry = readdir(directory); if (!entry) break; if (strcmp(entry->d_name, ".") == 0) continue; if (strcmp(entry->d_name, "..") == 0) continue; /* Remove the marker file. */ snprintf(notify_name, sizeof(notify_name), "%s/%s", DIR_RC_NOTIFY, entry->d_name); remove(notify_name); printf("rc notification: %s\n", entry->d_name); /* Take the appropriate action. */ if (!strcmp(entry->d_name, RCN_RESTART_REBOOT)) { stop_handle = 1; sys_exit(); } else if (!strcmp(entry->d_name, "flash_firmware")) { stop_handle = 1; flash_firmware(); } #if defined (USE_IPV6) else if (!strcmp(entry->d_name, RCN_RESTART_IPV6)) { if (!get_ap_mode()) { full_restart_ipv6(nvram_ipv6_type); nvram_ipv6_type = get_ipv6_type(); } } else if (strcmp(entry->d_name, RCN_RESTART_RADVD) == 0) { restart_dhcpd(); restart_radvd(); } #endif else if (!strcmp(entry->d_name, RCN_RESTART_WAN)) { full_restart_wan(); } else if (!strcmp(entry->d_name, RCN_RESTART_LAN)) { full_restart_lan(); } else if (!strcmp(entry->d_name, "stop_whole_wan")) { stop_wan(); } else if (!strcmp(entry->d_name, RCN_RESTART_IPTV)) { int is_ap_mode = get_ap_mode(); restart_iptv(is_ap_mode); if (!is_ap_mode) restart_firewall(); } else if(!strcmp(entry->d_name, "deferred_wan_connect")) { deferred_wan_connect(); } else if(!strcmp(entry->d_name, "auto_wan_reconnect")) { auto_wan_reconnect(); } else if(!strcmp(entry->d_name, "auto_wan_reconnect_pause")) { auto_wan_reconnect_pause(); } else if(!strcmp(entry->d_name, "manual_wan_reconnect")) { manual_wan_reconnect(); } else if(!strcmp(entry->d_name, "manual_wan_disconnect")) { manual_wan_disconnect(); } else if(!strcmp(entry->d_name, "manual_ddns_hostname_check")) { manual_ddns_hostname_check(); } #if (BOARD_NUM_USB_PORTS > 0) else if (!strcmp(entry->d_name, RCN_RESTART_MODEM)) { int wan_stopped = 0; int modules_reloaded = 0; int need_restart_wan = get_usb_modem_wan(0); int modem_rule = nvram_get_int("modem_rule"); int modem_type = nvram_get_int("modem_type"); if (nvram_modem_rule != modem_rule) { nvram_modem_rule = modem_rule; if (need_restart_wan) { wan_stopped = 1; stop_wan(); } if (modem_rule > 0) { modules_reloaded = 1; reload_modem_modules(modem_type, 1); } else { unload_modem_modules(); } } if (nvram_modem_type != modem_type) { if (nvram_modem_type == 3 || modem_type == 3) { if (modem_rule > 0 && !modules_reloaded) { if (need_restart_wan && !wan_stopped) stop_wan(); reload_modem_modules(modem_type, 1); } } nvram_modem_type = modem_type; } if (need_restart_wan) full_restart_wan(); } else if (strcmp(entry->d_name, RCN_RESTART_SPOOLER) == 0) { restart_usb_printer_spoolers(); } else if (strcmp(entry->d_name, RCN_RESTART_HDDTUNE) == 0) { system("/sbin/hddtune.sh"); set_pagecache_reclaim(); } #if defined(APP_FTPD) else if (strcmp(entry->d_name, RCN_RESTART_FTPD) == 0) { restart_ftpd(); } #endif #if defined(APP_SMBD) else if (strcmp(entry->d_name, RCN_RESTART_SMBD) == 0) { restart_smbd(); } #endif #if defined(APP_NFSD) else if (strcmp(entry->d_name, RCN_RESTART_NFSD) == 0) { restart_nfsd(); } #endif #if defined(APP_MINIDLNA) else if (strcmp(entry->d_name, "restart_dms_rescan") == 0) { restart_dms(1); } else if (strcmp(entry->d_name, RCN_RESTART_DMS) == 0) { restart_dms(0); } #endif #if defined(APP_FIREFLY) else if (strcmp(entry->d_name, RCN_RESTART_ITUNES) == 0) { restart_itunes(); } #endif #if defined(APP_TRMD) else if (strcmp(entry->d_name, RCN_RESTART_TRMD) == 0) { restart_torrent(); } #endif #if defined(APP_ARIA) else if (strcmp(entry->d_name, RCN_RESTART_ARIA) == 0) { restart_aria(); } #endif else if (!strcmp(entry->d_name, "on_hotplug_usb_storage")) { // deferred run usb apps nvram_set_int_temp("usb_hotplug_ms", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_unplug_usb_storage")) { umount_ejected(); } else if (!strcmp(entry->d_name, "on_hotplug_usb_printer")) { // deferred run usb printer daemons nvram_set_int_temp("usb_hotplug_lp", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_unplug_usb_printer")) { // deferred stop usb printer daemons nvram_set_int_temp("usb_unplug_lp", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_hotplug_usb_modem")) { // deferred run usb modem to wan nvram_set_int_temp("usb_hotplug_md", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_unplug_usb_modem")) { // deferred restart wan nvram_set_int_temp("usb_unplug_md", 1); alarm(5); } #endif else if (strcmp(entry->d_name, RCN_RESTART_HTTPD) == 0) { restart_httpd(); } else if (strcmp(entry->d_name, RCN_RESTART_TELNETD) == 0) { stop_telnetd(); start_telnetd(); } #if defined(APP_SSHD) else if (strcmp(entry->d_name, RCN_RESTART_SSHD) == 0) { restart_sshd(); } #endif #if defined(APP_SMBD) || defined(APP_NMBD) else if (strcmp(entry->d_name, RCN_RESTART_NMBD) == 0) { restart_nmbd(); } else if (strcmp(entry->d_name, RCN_RESTART_WINS) == 0) { restart_nmbd(); restart_dhcpd(); reload_vpn_server(); } #endif else if (strcmp(entry->d_name, RCN_RESTART_LLTD) == 0) { restart_lltd(); } else if (strcmp(entry->d_name, RCN_RESTART_ADSC) == 0) { restart_infosvr(); } else if (strcmp(entry->d_name, RCN_RESTART_VPNSVR) == 0) { restart_vpn_server(); } else if (strcmp(entry->d_name, RCN_RESTART_VPNCLI) == 0) { restart_vpn_client(); } else if (strcmp(entry->d_name, "start_vpn_client") == 0) { start_vpn_client(); } else if (strcmp(entry->d_name, "stop_vpn_client") == 0) { stop_vpn_client(); } else if (strcmp(entry->d_name, RCN_RESTART_DDNS) == 0) { stop_ddns(); start_ddns(1); } else if (strcmp(entry->d_name, RCN_RESTART_DI) == 0) { if (get_ap_mode() || has_wan_ip4(0)) notify_run_detect_internet(2); } else if (strcmp(entry->d_name, RCN_RESTART_DHCPD) == 0) { if (get_ap_mode()) update_hosts_ap(); restart_dhcpd(); } else if (strcmp(entry->d_name, RCN_RESTART_UPNP) == 0) { restart_upnp(); } else if (strcmp(entry->d_name, RCN_RESTART_SWITCH_CFG) == 0) { config_bridge(get_ap_mode()); switch_config_base(); switch_config_storm(); switch_config_link(); } else if (strcmp(entry->d_name, RCN_RESTART_SWITCH_VLAN) == 0) { notify_reset_detect_link(); switch_config_vlan(0); } else if (strcmp(entry->d_name, RCN_RESTART_SYSLOG) == 0) { stop_logger(); start_logger(0); } else if (strcmp(entry->d_name, RCN_RESTART_WDG) == 0) { restart_watchdog_cpu(); } else if (strcmp(entry->d_name, RCN_RESTART_TWEAKS) == 0) { notify_leds_detect_link(); } else if (strcmp(entry->d_name, "restart_firewall_wan") == 0) { restart_firewall(); } else if (strcmp(entry->d_name, RCN_RESTART_FIREWALL) == 0) { reload_nat_modules(); restart_firewall(); } else if (strcmp(entry->d_name, RCN_RESTART_NTPC) == 0) { notify_watchdog_time(); } else if (strcmp(entry->d_name, RCN_RESTART_TIME) == 0) { stop_logger(); set_timezone(); notify_watchdog_time(); notify_rstats_time(); start_logger(0); } else if (strcmp(entry->d_name, RCN_RESTART_SYSCTL) == 0) { int nf_nat_type = nvram_get_int("nf_nat_type"); restart_all_sysctl(); /* flush conntrack after NAT model changing */ if (nvram_nf_nat_type != nf_nat_type) { nvram_nf_nat_type = nf_nat_type; flush_conntrack_table(NULL); } } else if (!strcmp(entry->d_name, RCN_RESTART_WIFI5)) { int radio_on = get_enabled_radio_wl(); if (radio_on) radio_on = is_radio_allowed_wl(); restart_wifi_wl(radio_on, 1); } else if (!strcmp(entry->d_name, RCN_RESTART_WIFI2)) { int radio_on = get_enabled_radio_rt(); if (radio_on) radio_on = is_radio_allowed_rt(); restart_wifi_rt(radio_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_guest_wl")) { int guest_on = is_guest_allowed_wl(); control_guest_wl(guest_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_guest_rt")) { int guest_on = is_guest_allowed_rt(); control_guest_rt(guest_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_guest_wl_on")) { control_guest_wl(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_guest_wl_off")) { control_guest_wl(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_guest_rt_on")) { control_guest_rt(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_guest_rt_off")) { control_guest_rt(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_wl")) { int radio_on = get_enabled_radio_wl(); if (radio_on) radio_on = is_radio_allowed_wl(); control_radio_wl(radio_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_radio_rt")) { int radio_on = get_enabled_radio_rt(); if (radio_on) radio_on = is_radio_allowed_rt(); control_radio_rt(radio_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_radio_wl_on")) { control_radio_wl(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_wl_off")) { control_radio_wl(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_rt_on")) { control_radio_rt(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_rt_off")) { control_radio_rt(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_config_wl")) { gen_ralink_config_5g(0); } else if (!strcmp(entry->d_name, "control_wifi_config_rt")) { gen_ralink_config_2g(0); } else { dbg("WARNING: rc notified of unrecognized event `%s'.\n", entry->d_name); } /* * If there hasn't been another request for the same event made since * we started, we can safely remove the ``action incomplete'' marker. * Otherwise, we leave the marker because we'll go through here again * for this even and mark it complete only after we've completed it * without getting another request for the same event while handling * it. */ test_fp = fopen(notify_name, "r"); if (test_fp != NULL) { fclose(test_fp); } else { /* Remove the marker file. */ snprintf(notify_name, sizeof(notify_name), "%s/%s", DIR_RC_INCOMPLETE, entry->d_name); remove(notify_name); } if (stop_handle) break; } closedir(directory); }
/* * Called when link goes down */ int ipdown_main(int argc, char **argv) { if (check_action() != ACT_IDLE) return -1; runStartup("/etc/config", ".ipdown"); #ifdef HAVE_REGISTER if (isregistered_real()) #endif { #ifdef HAVE_RB500 runStartup("/usr/local/etc/config", ".ipdown"); #else runStartup("/jffs/etc/config", ".ipdown"); runStartup("/mmc/etc/config", ".ipdown"); runStartup("/tmp/etc/config", ".ipdown"); #endif } stop_ddns(); stop_ntpc(); unlink("/tmp/ppp/link"); if (nvram_match("wan_proto", "l2tp")) { /* * clear dns from the resolv.conf */ nvram_set("wan_get_dns", ""); dns_to_resolv(); // todo route_del(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255"); /* * Restore the default gateway for WAN interface */ nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf")); /* * Set default route to gateway if specified */ route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0", nvram_safe_get("wan_gateway"), "0.0.0.0"); } if (nvram_match("wan_proto", "pptp")) { eval("route", "del", "default"); nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf")); eval("route", "add", "default", "gw", nvram_safe_get("wan_gateway")); sysprintf("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE\n", nvram_safe_get("pptp_ifname")); } #ifdef HAVE_3G #if defined(HAVE_TMK) || defined(HAVE_BKM) else if (nvram_match("wan_proto", "3g")) { char *gpio3g = nvram_get("gpio3g"); if (gpio3g != NULL) set_gpio(atoi(gpio3g), 0); } #endif #endif nvram_set("pppoe_ifname", ""); nvram_set("pppd_pppifname", ""); // write PPP traffic statistics to nvram if wanted if (nvram_match("ppp_traffic", "1")) { char buffer[64]; long long old_in, old_out; long long in, out; char *pin; char *pout; time_t stamp; old_in = atol(nvram_safe_get("ppp_byte_in")); old_out = atol(nvram_safe_get("ppp_byte_out")); if ((pin = getenv("BYTES_RCVD"))) in = atol(pin); else in = 0; if ((pout = getenv("BYTES_SENT"))) out = atol(pout); else out = 0; in += old_in; out += old_out; snprintf(buffer, 63, "%lld", in); nvram_set("ppp_byte_in", buffer); snprintf(buffer, 63, "%lld", out); nvram_set("ppp_byte_out", buffer); if ((stamp = time(NULL)) < 1087818160) // clock is not set // properly stamp = 0; snprintf(buffer, 63, "%ld", stamp); nvram_set("ppp_byte_stamp", buffer); nvram_commit(); } if (nvram_match("ppp_demand", "1") && (nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp") || nvram_match("wan_proto", "pppoe"))) { stop_process("listen", "activity listener"); eval("listen", nvram_safe_get("lan_ifname")); } return 1; }