Beispiel #1
0
void restart_lfp()
{
	char v[32];
	char tmp[32];

	tcapi_get("Lan_Entry0", "IP", tmp);
	// if(nvram_get_int("lfp_disable")==0) {
	if(tcapi_get_int("GUITemp_Entry1", "lfp_disable")==0) {
		// sprintf(v, "%x", inet_addr(nvram_safe_get("lan_ipaddr")));
		sprintf(v, "%x", inet_addr(tmp));
		f_write_string("/proc/net/lfpctrl", v, 0, 0);
	}
	else {
		f_write_string("/proc/net/lfpctrl", "", 0, 0);
	}
}
Beispiel #2
0
int do_led_control(int which, int mode)
#endif
{
	int use_gpio, gpio_nr;
	int v = (mode == LED_OFF)? 0:1;

	// Did the user disable the leds?
	if ((mode == LED_ON) && (nvram_get_int("led_disable") == 1) && (which != LED_TURBO)
#ifdef RTCONFIG_QTN
		&& (which != BTN_QTN_RESET)
#endif
	)
	{
		return 0;
	}

	if (which < 0 || which >= LED_ID_MAX || mode < 0 || mode >= LED_FAN_MODE_MAX)
		return -1;

	get_gpio_values_once(0);
	use_gpio = led_gpio_table[which];
	gpio_nr = use_gpio & 0xFF;

#if defined(RTCONFIG_ETRON_XHCI_USB3_LED)
	if (which == LED_USB3 && nvram_match("led_usb3_gpio", "etron")) {
		char *onoff = "2";	/* LED OFF */

		if (mode == LED_ON || mode == FAN_ON || mode == HAVE_FAN_ON)
			onoff = "3";	/* LED ON */

		f_write_string("/sys/bus/pci/drivers/xhci_hcd/port_led", onoff, 0, 0);
		return 0;
	}
#endif

#if defined(RTAC56U) || defined(RTAC56S)
	if (which == LED_5G && nvram_match("5g_fail", "1"))
		return -1;
#endif

	if (use_gpio < 0 || gpio_nr == 0xFF)
		return -1;

	if (use_gpio & GPIO_ACTIVE_LOW)
		v ^= 1;

	if (mode == LED_OFF) {
		stop_bled(use_gpio);
	}

	set_gpio(gpio_nr, v);

	if (mode == LED_ON) {
		start_bled(use_gpio);
	}

	return 0;
}
Beispiel #3
0
int dualwan_control(int argc, char *argv[])
{
	int sw_mode;
	char dualwan_mode[8];
	char dualwan_wans[16];
	char wan0_proto[10];
	char wan1_proto[10];

#if defined(RTCONFIG_CFEZ) && defined(RTCONFIG_BCMARM)
	if (strcmp(nvram_safe_get("model"), MODEL_PROTECT) != 0){
#else
	if (strcmp(cfe_nvram_safe_get("model"), MODEL_PROTECT) != 0){
#endif
		_dprintf("illegal, cannot enable DualWAN\n");
		return -1;
	}

	memset(dualwan_mode, 0, 8);
	strcpy(dualwan_mode, nvram_safe_get("wans_mode"));
	memset(dualwan_wans, 0, 16);
	strcpy(dualwan_wans, nvram_safe_get("wans_dualwan"));
	memset(wan0_proto, 0, 10);
	strcpy(wan0_proto, nvram_safe_get("wan0_proto"));
	memset(wan1_proto, 0, 10);
	strcpy(wan1_proto, nvram_safe_get("wan1_proto"));

	sw_mode = nvram_get_int("sw_mode");

	if(strcmp(dualwan_mode, "lb") != 0) goto EXIT;
	if(strcmp(dualwan_wans, "wan lan") != 0) goto EXIT;
	if(strcmp(wan0_proto, "pptp") == 0 ||
		strcmp(wan1_proto, "l2tp") == 0)
		goto EXIT;
	if (sw_mode != SW_MODE_ROUTER) goto EXIT;

	while(1){
		f_write_string("/proc/sys/net/ipv4/route/flush", "1", 0, 0);
		f_write_string("/proc/sys/net/ipv4/route/flush", "1", 0, 0);
		f_write_string("/proc/sys/net/ipv4/route/flush", "1", 0, 0);
		sleep(FLUSH_INTERVAL);
	}

EXIT:
	return 0;
}
Beispiel #4
0
static void expires(unsigned int seconds)
{
	struct sysinfo info;
	char s[32];

   	sysinfo(&info);
	sprintf(s, "%u", (unsigned int)info.uptime + seconds);
	f_write_string("/var/lib/misc/dhcpc.expires", s, 0, 0);
}
Beispiel #5
0
uint32_t set_gpio(uint32_t gpio, uint32_t value)
{
	char path[PATH_MAX], val_str[10];

	sprintf(val_str, "%d", !!value);
	sprintf(path, "%s/gpio%d/value", GPIOLIB_DIR, gpio);
	f_write_string(path, val_str, 0, 0);

	return 0;
}
Beispiel #6
0
static void write_ct_timeout(const char *type, const char *name, unsigned int val)
{
	char buf[128];
	char v[16];

	sprintf(buf, "/proc/sys/net/ipv4/netfilter/ip_conntrack_%s_timeout%s%s",
		type, (name && name[0]) ? "_" : "", name ? name : "");
	sprintf(v, "%u", val);

	f_write_string(buf, v, 0, 0);
}
Beispiel #7
0
void asp_upnpinfo(int argc, char **argv)
{
	if (nvram_get_int("upnp_enable")) {
		f_write_string("/etc/upnp/info", "", 0, 0);
		if (killall("miniupnpd", SIGUSR2) == 0) {
			f_wait_notexists("/etc/upnp/info", 5);
		}
	
		web_puts("\nmupnp_data = '");
		web_putfile("/etc/upnp/data.info", WOF_JAVASCRIPT);
		web_puts("';\n");
	}
}
Beispiel #8
0
void start_sshd(void)
{
	int dirty = 0;
	FILE *fp;

	mkdir("/etc/dropbear", 0700);
	mkdir("/root/.ssh", 0700);

	f_write_string("/root/.ssh/authorized_keys", nvram_safe_get("sshd_authkeys"), 0, 0700);

	dirty |= check_host_key("rsa", "sshd_hostkey", "/etc/dropbear/dropbear_rsa_host_key");
	dirty |= check_host_key("dss", "sshd_dsskey",  "/etc/dropbear/dropbear_dss_host_key");
	if (dirty)
		nvram_commit_x();

/*
	xstart("dropbear", "-a", "-p", nvram_safe_get("sshd_port"), nvram_get_int("sshd_pass") ? "" : "-s");
*/

	char *argv[9];
	int argc;
	char *p;

	argv[0] = "dropbear";
	argv[1] = "-p";
	argv[2] = nvram_safe_get("sshd_port");
	argc = 3;

	if (!nvram_get_int("sshd_pass")) argv[argc++] = "-s";

	if (nvram_get_int("sshd_forwarding")) argv[argc++] = "-a";

	if (((p = nvram_get("sshd_rwb")) != NULL) && (*p)) {
		argv[argc++] = "-W";
		argv[argc++] = p;
	}

	argv[argc] = NULL;
	_eval(argv, NULL, 0, NULL);

	if (get_productid())
	{
		if ((fp=fopen("/proc/sys/kernel/hostname", "w+")))
		{
			fputs(get_productid(), fp);
			fclose(fp);
		}
	}
}
Beispiel #9
0
void notice_set(const char *path, const char *format, ...)
{
	char p[256];
	char buf[2048];
	va_list args;

	va_start(args, format);
	vsnprintf(buf, sizeof(buf), format, args);
	va_end(args);

	mkdir("/var/notice", 0755);
	snprintf(p, sizeof(p), "/var/notice/%s", path);
	f_write_string(p, buf, 0, 0);
	if (buf[0]) syslog(LOG_INFO, "notice[%s]: %s", path, buf);
}
Beispiel #10
0
int pppevent_main(int argc, char **argv)
{
	int i;
	
	TRACE_PT("begin\n");

	for (i = 1; i < argc; ++i) {
		TRACE_PT("arg%d=%s\n", i, argv[i]);
		if (strcmp(argv[i], "-t") == 0) {
			if (++i >= argc) return 1;
			if ((strcmp(argv[i], "PAP_AUTH_FAIL") == 0) || (strcmp(argv[i], "CHAP_AUTH_FAIL") == 0)) {
				f_write_string("/tmp/ppp/log", argv[i], 0, 0);
				return 0;
			}
			if ((strcmp(argv[i], "PADO_TIMEOUT") == 0) || (strcmp(argv[i], "PADS_TIMEOUT") == 0)) {
				f_write_string("/tmp/ppp/log", argv[i], 0, 0);
				return 0;
			}
		}
	}

	TRACE_PT("end\n");
	return 1;
}
Beispiel #11
0
void start_sshd(void)
{
	int dirty = 0;
	char buf[2048];

	mkdir("/etc/dropbear", 0700);
	mkdir("/root/.ssh", 0700);

	f_write_string("/root/.ssh/authorized_keys", get_parsed_crt("sshd_authkeys", buf), 0, 0700);

	dirty |= check_host_key("rsa", "sshd_hostkey", "/etc/dropbear/dropbear_rsa_host_key");
	dirty |= check_host_key("dss", "sshd_dsskey",  "/etc/dropbear/dropbear_dss_host_key");
	if (dirty)
		nvram_commit_x();

/*
	xstart("dropbear", "-a", "-p", nvram_safe_get("sshd_port"), nvram_get_int("sshd_pass") ? "" : "-s");
*/

	char *argv[9];
	int argc;
	char *p;

	argv[0] = "dropbear";
	argv[1] = "-p";
	argv[2] = nvram_safe_get("sshd_port");
	argc = 3;

	if (!nvram_get_int("sshd_pass")) argv[argc++] = "-s";

	if (nvram_get_int("sshd_forwarding")) {
		argv[argc++] = "-a";
	} else {
		argv[argc++] = "-j";
		argv[argc++] = "-k";
	}

	if (((p = nvram_get("sshd_rwb")) != NULL) && (*p)) {
		argv[argc++] = "-W";
		argv[argc++] = p;
	}

	argv[argc] = NULL;
	_eval(argv, NULL, 0, NULL);

}
Beispiel #12
0
void wo_upnp(char *url)
{
	char s[256];
	const char *proto;
	const char *eport;

	if (nvram_get_int("upnp_enable")) {
		if (((proto = webcgi_get("remove_proto")) != NULL) && (*proto) &&
			((eport = webcgi_get("remove_eport")) != NULL) && (*eport)) {
			sprintf(s, "%3s %6s\n", proto, eport);
			f_write_string("/etc/upnp/delete", s, 0, 0);
			if (killall("miniupnpd", SIGUSR2) == 0) {
				f_wait_notexists("/etc/upnp/delete", 5);
			}
		}
	}
	common_redirect();
}
Beispiel #13
0
uint32_t gpio_dir(uint32_t gpio, int dir)
{
	char path[PATH_MAX], v[10], *dir_str = "in";

	if (dir == GPIO_DIR_OUT) {
		dir_str = "out";		/* output, low voltage */
		*v = '\0';
		sprintf(path, "%s/gpio%d/value", GPIOLIB_DIR, gpio);
		if (f_read_string(path, v, sizeof(v)) > 0 && atoi(v) == 1)
			dir_str = "high";	/* output, high voltage */
	}

	__export_gpio(gpio);
	sprintf(path, "%s/gpio%d/direction", GPIOLIB_DIR, gpio);
	f_write_string(path, dir_str, 0, 0);

	return 0;
}
Beispiel #14
0
/* Export specified GPIO
 * @return:
 * 	0:	success
 *  otherwise:	fail
 */
static int __export_gpio(uint32_t gpio)
{
	char gpio_path[PATH_MAX], export_path[PATH_MAX], gpio_str[] = "999XXX";

	if (!d_exists(GPIOLIB_DIR)) {
		_dprintf("%s does not exist!\n", __func__);
		return -1;
	}
	sprintf(gpio_path, "%s/gpio%d", GPIOLIB_DIR, gpio);
	if (d_exists(gpio_path))
		return 0;

	sprintf(export_path, "%s/export", GPIOLIB_DIR);
	sprintf(gpio_str, "%d", gpio);
	f_write_string(export_path, gpio_str, 0, 0);

	return 0;
}
Beispiel #15
0
int pppevent_main(int argc, char **argv)
{
	int i;
	
	TRACE_PT("begin\n");

	for (i = 1; i < argc; ++i) {
		TRACE_PT("arg%d=%s\n", i, argv[i]);
		if (strcmp(argv[i], "-t") == 0) {
			if (++i >= argc) return 1;
			if ((strcmp(argv[i], "PAP_AUTH_FAIL") == 0) || (strcmp(argv[i], "CHAP_AUTH_FAIL") == 0)) {
				f_write_string("/tmp/ppp/log", argv[i], 0, 0);
				notice_set("wan", "Authentication failed");	// !!!
				return 0;
			}			
		}
	}

	TRACE_PT("end\n");
	return 1;
}
Beispiel #16
0
int ipup_main(int argc, char **argv)
{
	char *wan_ifname;
	char *value;
	char buf[256];
	const char *p;

	TRACE_PT("begin\n");

	killall("listen", SIGKILL);
	
	if (!wait_action_idle(10)) return -1;

	wan_ifname = safe_getenv("IFNAME");
	if ((!wan_ifname) || (!*wan_ifname)) return -1;
	nvram_set("wan_iface", wan_ifname);	// ppp#

	// ipup receives six arguments:
	//   <interface name>  <tty device>  <speed> <local IP address> <remote IP address> <ipparam>
	//   ppp1 vlan1 0 71.135.98.32 151.164.184.87 0

	f_write_string("/tmp/ppp/link", argv[1], 0, 0);
	
	if ((p = getenv("IPREMOTE"))) {
		nvram_set("wan_gateway_get", p);
		TRACE_PT("IPREMOTE=%s\n", p);
	}

	if ((value = getenv("IPLOCAL"))) {
		_dprintf("IPLOCAL=%s\n", value);

		switch (get_wan_proto()) {
		case WP_PPPOE:
		case WP_PPP3G:
			nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));		// store last ip address
			nvram_set("wan_ipaddr", value);
			nvram_set("wan_netmask", "255.255.255.255");
			break;
		case WP_PPTP:
		case WP_L2TP:
			nvram_set("wan_ipaddr_buf", nvram_safe_get("ppp_get_ip"));
			break;
		}

		if (!nvram_match("ppp_get_ip", value)) {
			ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
			nvram_set("ppp_get_ip", value);
		}

		_ifconfig(wan_ifname, IFUP, value, "255.255.255.255", (p && (*p)) ? p : NULL);
	}

	buf[0] = 0;
	if ((p = getenv("DNS1")) != NULL) strlcpy(buf, p, sizeof(buf));
	if ((p = getenv("DNS2")) != NULL) {
		if (buf[0]) strlcat(buf, " ", sizeof(buf));
		strlcat(buf, p, sizeof(buf));
	}
	nvram_set("wan_get_dns", buf);
	TRACE_PT("DNS=%s\n", buf);

	if ((value = getenv("AC_NAME"))) nvram_set("ppp_get_ac", value);
	if ((value = getenv("SRV_NAME"))) nvram_set("ppp_get_srv", value);
	if ((value = getenv("MTU"))) nvram_set("wan_run_mtu", value);

	start_wan_done(wan_ifname);

	TRACE_PT("end\n");
	return 0;
}
Beispiel #17
0
void create_passwd(void)
{
	char s[512];
	char *p;
	char salt[32];
	FILE *f;
	mode_t m;
#ifdef TCONFIG_SAMBASRV	//!!TB
	char *smbd_user;
#endif

	strcpy(salt, "$1$");
	f_read("/dev/urandom", s, 6);
	base64_encode(s, salt + 3, 6);
	salt[3 + 8] = 0;
	p = salt;
	while (*p) {
		if (*p == '+') *p = '.';
		++p;
	}
	if (((p = nvram_get("http_passwd")) == NULL) || (*p == 0)) p = "admin";

#ifdef TCONFIG_SAMBASRV	//!!TB
	if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
		smbd_user = "******";
#endif

	m = umask(0777);
	if ((f = fopen("/etc/shadow", "w")) != NULL) {
		p = crypt(p, salt);
		fprintf(f, "root:%s:0:0:99999:7:0:0:\n"
				   "nobody:*:0:0:99999:7:0:0:\n", p);
#if TOMATO_SL
		// todo		zzz
		fprintf(f, "admin:*:0:0:99999:7:0:0:\n");
#endif
#ifdef TCONFIG_SAMBASRV	//!!TB
		fprintf(f, "%s:*:0:0:99999:7:0:0:\n", smbd_user);
#endif

		fappend(f, "/etc/shadow.custom");
		fclose(f);
	}
	umask(m);
	chmod("/etc/shadow", 0600);

#ifdef TCONFIG_SAMBASRV	//!!TB
	sprintf(s, 
		"root:x:0:0:root:/root:/bin/sh\n"
		"%s:x:100:100:nas:/dev/null:/dev/null\n"
		"nobody:x:65534:65534:nobody:/dev/null:/dev/null\n",
		smbd_user);
	f_write_string("/etc/passwd", s, 0, 0644);
#else	//!!TB
	f_write_string("/etc/passwd",
		"root:x:0:0:root:/root:/bin/sh\n"
#if TOMATO_SL
		// todo		zzz
		"admin:x:100:100:nas:/dev/null:/dev/null\n"
#endif
		"nobody:x:65534:65534:nobody:/dev/null:/dev/null\n",
		0, 0644);
#endif	//!!TB
	fappend_file("/etc/passwd", "/etc/passwd.custom");

	f_write_string("/etc/gshadow",
		"root:*:0:\n"
//#if TOMATO_SL
#ifdef TCONFIG_SAMBASRV	//!!TB
		"nas:*:100:\n"
#endif
		"nobody:*:65534:\n",
		0, 0600);
	fappend_file("/etc/gshadow", "/etc/gshadow.custom");

	f_write_string("/etc/group",
		"root:x:0:\n"
//#if TOMATO_SL
#ifdef TCONFIG_SAMBASRV	//!!TB
		"nas:x:100:\n"
#endif
		"nobody:x:65534:\n",
		0, 0644);
	fappend_file("/etc/group", "/etc/group.custom");
}
Beispiel #18
0
int do_led(int which, int mode)
{
//				    WLAN  DIAG  WHITE AMBER DMZ   AOSS  BRIDG MYST/USB 5G
//				    ----- ----- ----- ----- ----- ----- ----- -----    -- 
	static int wrt54g[]	= { 255,  1,    2,    3,    7,    255,  255,  255,    255};
	static int wrtsl[]	= { 255,  1,    5,    7,    0,    255,  255,  255,    255};
	static int whrg54[]	= { 2,    7,    255,  255,  255,  6,    1,    3	 ,    255};
	static int wbr2g54[]	= { 255,  -1,   255,  255,  255,  -6,   255,  255,    255};
	static int wzrg54[]	= { 2,    7,    255,  255,  255,  6,    255,  255,    255};
	static int wr850g1[]	= { 7,    3,    255,  255,  255,  255,  255,  255,    255};
	static int wr850g2[]	= { 0,    1,    255,  255,  255,  255,  255,  255,    255};
	static int wtr54gs[]	= { 1,    -1,   255,  255,  255,  255,  255,  255,    255};
	static int dir320[]	= { -99,   1,     4,    3,  255,  255,  255,   -5,    255};
	static int h618b[]	= { 255,  -1,   255,  255,  255,   -5,   -3,   -4,    255};
	static int wl1600gl[]	= { 1,    -5, 	  0,  255,  255,  2,    255,  255,    255};
	static int wrt310nv1[]	= { 255,   1,     9,    3,  255,  255,  255,  255,    255};
	static int wrt160nv1[]	= { 255,   1,     5,    3,  255,  255,  255,  255,    255};
#ifdef CONFIG_BCMWL5
	static int wnr3500[]	= { 255, 255,     2,  255,  255,   -1,  255,  255,    255};
	static int wnr2000v2[]	= { 255, 255,   255,  255,  255,   -7,  255,  255,    255};
	static int f7d[]	= { 255, 255,   255,  255,   12,   13,  255,   14,    255};
	static int wrt160nv3[]	= { 255,   1,     4,    2,  255,  255,  255,  255,    255};
	static int e900[]	= { 255,  -6,     8,  255,  255,  255,  255,  255,    255};
	static int e1000v2[]	= { 255,  -6,     8,    7,  255,  255,  255,  255,    255};
	static int e3200[]	= { 255,  -3,   255,  255,  255,  255,  255,  255,    255};
	static int wrt320n[]	= { 255,   2,     3,    4,  255,  255,  255,  255,    255};
	static int wrt610nv2[]	= { 255,   5,     3,    0,  255,  255,  255,   -7,    255};
	static int e4200[]		= { 255,   5,    -3,  255,  255,  255,  255,  255,    255};
	static int rtn10u[]		= { 255, 255,   255,  255,  255,   -7,  255,   -8,    255};
	static int rtn10p[]		= { 255,  -6,   255,  255,  255,   -7,  255,  255,    255};
	static int rtn12b1[]	= {  -5, 255,   255,  255,  255,  255,  255,  225,    255};
	static int rtn15u[]		= {   1, 255,     3,  255,  255,  255,  255,   -9,    255};
	static int rtn53[]		= {   0, -17,   255,  255,  255,  255,  255,  255,    255};
	static int rtn66u[]		= { 255, -12,   255,  255,  255,  255,  255,   15,     13};
	static int w1800r[] 	= { 255, -13,   255,  255,  255,  255,  255,  -12,     -5};
	static int l600n[]  	= { 255, 255,   255,  255,  255,  -7,   255,  -8,      -5};
	static int dir620c1[]   = { -6,   -8,   255,  255,  255,  -7,   255,  255,     -5};
	static int wndr3700v3[] = { 255, 255,     2,  255,  255,  -1,   255,  255,    255};
	static int d1800h[]     = { -12, -13,     8,  255,  255,  -10,  255,    15, 255,   11};
	static int tdn6[]       = { 255,  -6,     8,  255,  255,  255,  255,   255, 255,  255};

#endif

	char s[16];
	int n;
	int b = 255, c = 255;
	int ret = 255;

	if ((which < 0) || (which >= LED_COUNT)) return ret;

	switch (nvram_match("led_override", "1") ? MODEL_UNKNOWN : get_model()) {
	case MODEL_WRT54G:
		if (check_hw_type() == HW_BCM4702) {
			// G v1.x
			if ((which != LED_DIAG) && (which != LED_DMZ)) return ret;
			b = (which == LED_DMZ) ? 1 : 4;
			if (mode != LED_PROBE) {
				if (f_read_string("/proc/sys/diag", s, sizeof(s)) > 0) {
					n = atoi(s);
					sprintf(s, "%u", mode ? (n | b) : (n & ~b));
					f_write_string("/proc/sys/diag", s, 0, 0);
				}
			}
			return b;
		}
		switch (which) {
		case LED_AMBER:
		case LED_WHITE:
			if (!supports(SUP_WHAM_LED)) return ret;
			break;
		}
		b = wrt54g[which];
		break;
	case MODEL_WTR54GS:
		b = wtr54gs[which];
		break;
	case MODEL_WRTSL54GS:
		b = wrtsl[which];
		break;
	case MODEL_WHRG54S:
	case MODEL_WHRHPG54:
	case MODEL_WHRG125:
		b = whrg54[which];
		break;
	case MODEL_WZRG54:
	case MODEL_WZRHPG54:
	case MODEL_WZRRSG54:
	case MODEL_WZRRSG54HP:
	case MODEL_WVRG54NF:
	case MODEL_WHR2A54G54:
	case MODEL_WHR3AG54:
	case MODEL_WZRG108:
		b = wzrg54[which];
		break;
/*		
	case MODEL_WHR2A54G54:
		if (which != LED_DIAG) return ret;
		b = 7;
		break;
*/
	case MODEL_WBRG54:
		if (which != LED_DIAG) return ret;
		b = 7;
		break;
	case MODEL_WBR2G54:
		b = wbr2g54[which];
		break;
	case MODEL_WR850GV1:
		b = wr850g1[which];
		break;
	case MODEL_WR850GV2:
	case MODEL_WR100:
		b = wr850g2[which];
		break;
	case MODEL_WL500GP:
		if (which != LED_DIAG) return ret;
		b = -1;	// power light
		break;
	case MODEL_WL500W:
		if (which != LED_DIAG) return ret;
		b = -5;	// power light
		break;
	case MODEL_DIR320:
		b = dir320[which];
		break;
	case MODEL_H618B:
		b = h618b[which];
		break;
	case MODEL_WL1600GL:
		b = wl1600gl[which];
		break;
	case MODEL_WL500GPv2:
	case MODEL_WL500GD:
	case MODEL_WL520GU:
	case MODEL_WL330GE:
		if (which != LED_DIAG) return ret;
		b = -99;	// Invert power light as diag indicator
		break;
#ifdef CONFIG_BCMWL5
	case MODEL_RTN12:
		if (which != LED_DIAG) return ret;
		b = -2;	// power light
		break;
	case MODEL_RTN10:
	case MODEL_RTN16:
		if (which != LED_DIAG) return ret;
		b = -1;	// power light
		break;
	case MODEL_RTN15U:
		b = rtn15u[which];
		break;
	case MODEL_RTN53:
	case MODEL_RTN53A1:
		b = rtn53[which];
		break;
	case MODEL_RTN66U:
		b = rtn66u[which];
		break;
	case MODEL_W1800R:
		b = w1800r[which];
		break;
	case MODEL_D1800H:
		if (which == LED_DIAG) {
			// power led gpio: 0x02 - white, 0x13 - red 
			b = (mode) ? 13 : 2;
			c = (mode) ? 2 : 13;
		} else
			b = d1800h[which];
		break;
	case MODEL_WNDR3700v3:
		b = wndr3700v3[which];
		break;
	case MODEL_WNR3500L:
	case MODEL_WNR3500LV2:
		if (which == LED_DIAG) {
			// power led gpio: 0x03 - green, 0x07 - amber
			b = (mode) ? 7 : 3;
			c = (mode) ? 3 : 7;
		} else
			b = wnr3500[which];
		break;
	case MODEL_WNR2000v2:
		if (which == LED_DIAG) {
			// power led gpio: 0x01 - green, 0x02 - amber
			b = (mode) ? 2 : 1;
			c = (mode) ? 1 : 2;
		} else
			b = wnr2000v2[which];
		break;
	case MODEL_F7D3301:
	case MODEL_F7D3302:
	case MODEL_F7D4301:
	case MODEL_F7D4302:
	case MODEL_F5D8235v3:
		if (which == LED_DIAG) {
			// power led gpio: 10 - green, 11 - red
			b = (mode) ? 11 : -10;
			c = (mode) ? -10 : 11;
		} else
			b = f7d[which];
		break;
	case MODEL_E1000v2:
		b = e1000v2[which];
		break;
	case MODEL_E900:
	case MODEL_E1500:
	case MODEL_E1550:
	case MODEL_E2500:
		b = e900[which];
		break;
	case MODEL_E3200:
		b = e3200[which];
		break;
	case MODEL_WRT160Nv3:
		b = wrt160nv3[which];
		break;
	case MODEL_WRT320N:
		b = wrt320n[which];
		break;
	case MODEL_WRT610Nv2:
		b = wrt610nv2[which];
		break;
	case MODEL_E4200:
		b = e4200[which];
		break;
	case MODEL_RTN10U:
		b = rtn10u[which];
		break;
	case MODEL_RTN10P:
		b = rtn10p[which];
		break;
	case MODEL_RTN12B1:
		b = rtn12b1[which];
		break;
	case MODEL_L600N:
		b = l600n[which];
		break;
	case MODEL_DIR620C1:
		b = dir620c1[which];
	case MODEL_TDN60: //bwq518
	case MODEL_TDN6:
		b = tdn6[which];
		break;
#endif
/*
	case MODEL_RT390W:
		break;
*/
	case MODEL_MN700:
		if (which != LED_DIAG) return ret;
		b = 6;
		break;
	case MODEL_WLA2G54L:
		if (which != LED_DIAG) return ret;
		b = 1;
		break;
	case MODEL_WRT300N:
		if (which != LED_DIAG) return ret;
		b = 1;
		break;
	case MODEL_WRT310Nv1:
		b = wrt310nv1[which];
		break;
	case MODEL_WRT160Nv1:
		b = wrt160nv1[which];
		break;
	default:
		sprintf(s, "led_%s", led_names[which]);
		if (nvget_gpio(s, &b, &n)) {
			if ((mode != LED_PROBE) && (n)) mode = !mode;
			ret = (n) ? b : ((b) ? -b : -99);
			goto SET;
		}
		return ret;
	}

	ret = b;
	if (b < 0) {
		if (b == -99) b = 0; // -0 substitute
			else b = -b;
	}
	else if (mode != LED_PROBE) {
		mode = !mode;
	}

SET:
	if (b < 16) {
		if (mode != LED_PROBE) {
			gpio_write(1 << b, mode);

			if (c < 0) {
				if (c == -99) c = 0;
				else c = -c;
			}
			else mode = !mode;
			if (c < 16) gpio_write(1 << c, mode);
		}
	}

	return ret;
}
Beispiel #19
0
void setup_conntrack(void)
{
	unsigned int v[10];
	char p[128] = {0};
	char buf[70];
	int i;
	char *pch;

	// p = nvram_safe_get("ct_tcp_timeout");
	tcapi_get("SysInfo_Entry", "ct_tcp_timeout", p);
	if (sscanf(p, "%u%u%u%u%u%u%u%u%u%u",
		&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8], &v[9]) == 10) {	// lightly verify
		write_tcp_timeout("established", v[1]);
		write_tcp_timeout("syn_sent", v[2]);
		write_tcp_timeout("syn_recv", v[3]);
		write_tcp_timeout("fin_wait", v[4]);
		write_tcp_timeout("time_wait", v[5]);
		write_tcp_timeout("close", v[6]);
		write_tcp_timeout("close_wait", v[7]);
		write_tcp_timeout("last_ack", v[8]);
	}
	else {
		v[1] = read_tcp_timeout("established");
		v[2] = read_tcp_timeout("syn_sent");
		v[3] = read_tcp_timeout("syn_recv");
		v[4] = read_tcp_timeout("fin_wait");
		v[5] = read_tcp_timeout("time_wait");
		v[6] = read_tcp_timeout("close");
		v[7] = read_tcp_timeout("close_wait");
		v[8] = read_tcp_timeout("last_ack");
		sprintf(buf, "0 %u %u %u %u %u %u %u %u 0",
			v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
		// nvram_set("ct_tcp_timeout", buf);
		tcapi_set("SysInfo_Entry", "ct_tcp_timeout", buf);
	}

	setup_udp_timeout(FALSE);

	// p = nvram_safe_get("ct_timeout");
	tcapi_get("SysInfo_Entry", "ct_timeout", p);
	if (sscanf(p, "%u%u", &v[0], &v[1]) == 2) {
//		write_ct_timeout("generic", NULL, v[0]);
		write_ct_timeout("icmp", NULL, v[1]);
	}
	else {
		v[0] = read_ct_timeout("generic", NULL);
		v[1] = read_ct_timeout("icmp", NULL);
		sprintf(buf, "%u %u", v[0], v[1]);
		// nvram_set("ct_timeout", buf);
		tcapi_set("SysInfo_Entry", "ct_timeout", buf);
	}

#ifdef LINUX26
	// p = nvram_safe_get("ct_hashsize");
	tcapi_get("SysInfo_Entry", "ct_hashsize", p);
	i = atoi(p);
	if (i >= 127) {
		f_write_string("/sys/module/nf_conntrack/parameters/hashsize", p, 0, 0);
	}
	else if (f_read_string("/sys/module/nf_conntrack/parameters/hashsize", buf, sizeof(buf)) > 0) {
		if (atoi(buf) > 0) {//nvram_set("ct_hashsize", buf);
			pch = strchr(buf, '\n');
			if(pch != NULL)
				memset(pch, 0, 1);
			tcapi_set("SysInfo_Entry", "ct_hashsize", buf);
		}
	}
#endif

	// p = nvram_safe_get("ct_max");
	tcapi_get("SysInfo_Entry", "ct_max", p);
	i = atoi(p);
	if (i >= 128) {
		f_write_string("/proc/sys/net/ipv4/netfilter/ip_conntrack_max", p, 0, 0);
	}
	else if (f_read_string("/proc/sys/net/ipv4/netfilter/ip_conntrack_max", buf, sizeof(buf)) > 0) {
		if (atoi(buf) > 0) {//nvram_set("ct_max", buf);
			pch = strchr(buf, '\n');
			if(pch != NULL)
				memset(pch, 0, 1);
			tcapi_set("SysInfo_Entry", "ct_max", buf);
		}
	}

	//if (!nvram_match("nf_rtsp", "0")) {
	//	ct_modprobe("rtsp");
	//}
	//else {
	//	ct_modprobe_r("rtsp");
	//}

	// if (!nvram_match("nf_h323", "0")) {
		// ct_modprobe("h323");
	// }
	// else {
		// ct_modprobe_r("h323");
	// }

// #ifdef LINUX26
	// if (!nvram_match("nf_sip", "0")) {
		// ct_modprobe("sip");
	// }
	// else {
		// ct_modprobe_r("sip");
	// }
// #endif

	// !!TB - FTP Server
// #ifdef RTCONFIG_FTP
	// i = nvram_get_int("ftp_port");
	// if (nvram_match("ftp_enable", "1") && (i > 0) && (i != 21))
	// {
		// char ports[32];

		// sprintf(ports, "ports=21,%d", i);
		// ct_modprobe("ftp", ports);
	// }
	// else 
// #endif
	// if (!nvram_match("nf_ftp", "0")
// #ifdef RTCONFIG_FTP
		// || nvram_match("ftp_enable", "1")	// !!TB - FTP Server
// #endif
		// ) {
		// ct_modprobe("ftp");
	// }
	// else {
		// ct_modprobe_r("ftp");
	// }

	// if (!nvram_match("nf_pptp", "0")) {
		// ct_modprobe("proto_gre");
		// ct_modprobe("pptp");
	// }
	// else {
		// ct_modprobe_r("pptp");
		// ct_modprobe_r("proto_gre");
	// }
}
Beispiel #20
0
// used for various testing
static int rctest_main(int argc, char *argv[])
{
	int on;

	if (argc < 2) {
		_dprintf("test what?\n");
	}
	else if (strcmp(argv[1], "rc_service")==0) {
		notify_rc(argv[2]);
	}
	else if(strcmp(argv[1], "get_phy_status")==0) {
		int mask;
		mask = atoi(argv[2]);
		TRACE_PT("debug for phy_status %x\n", get_phy_status(mask));
	}
	else if(strcmp(argv[1], "get_phy_speed")==0) {
		int mask;
		mask = atoi(argv[2]);
		TRACE_PT("debug for phy_speed %x\n", get_phy_speed(mask));
	}
	else if(strcmp(argv[1], "set_phy_ctrl")==0) {
		int mask, ctrl;
		mask = atoi(argv[2]);
		ctrl = atoi(argv[3]);
		TRACE_PT("debug for phy_speed %x\n", set_phy_ctrl(mask, ctrl));
	}
	else if(strcmp(argv[1], "handle_notifications")==0) {
		handle_notifications();
	}
	else if(strcmp(argv[1], "check_action")==0) {
		_dprintf("check: %d\n", check_action());
	}
	else if(strcmp(argv[1], "nvramhex")==0) {
		int i;
		char *nv;

		nv = nvram_safe_get(argv[2]);

		_dprintf("nvram %s(%d): ", nv, strlen(nv));
		for(i=0;i<strlen(nv);i++) {
			_dprintf(" %x", (unsigned char)*(nv+i));
		}
		_dprintf("\n");
	}
	else {
		on = atoi(argv[2]);
		_dprintf("%s %d\n", argv[1], on);

		if (strcmp(argv[1], "vlan") == 0)
		{
			if(on) start_vlan();
			else stop_vlan();
		}
		else if (strcmp(argv[1], "lan") == 0) {
			if(on) start_lan();
			else stop_lan();
		}
		else if (strcmp(argv[1], "wl") == 0) {
			if(on)
			{
				start_wl();
				lanaccess_wl();
			}
		}
		else if (strcmp(argv[1], "wan") == 0) {
			if(on) start_wan();
			else stop_wan();
		}
		else if (strcmp(argv[1], "wan_port") == 0) {
			if(on) start_wan_port();
			else stop_wan_port();
		}
		else if (strcmp(argv[1], "firewall") == 0) {
			//if(on) start_firewall();
			//else stop_firewall();
		}
		else if (strcmp(argv[1], "watchdog") == 0) {
			if(on) start_watchdog();
			else stop_watchdog();
		}
#if ! (defined(RTCONFIG_QCA) || defined(RTCONFIG_RALINK))
		else if (strcmp(argv[1], "watchdog02") == 0) {
			if(on) start_watchdog02();
			else stop_watchdog02();
		}
#endif  /* ! (RTCONFIG_QCA || RTCONFIG_RALINK) */
		else if (strcmp(argv[1], "sw_devled") == 0) {
			if(on) start_sw_devled();
			else stop_sw_devled();
		}
#ifdef RTCONFIG_FANCTRL
		else if (strcmp(argv[1], "phy_tempsense") == 0) {
			if(on) start_phy_tempsense();
			else stop_phy_tempsense();
		}
#endif
#ifdef RTCONFIG_BCMWL6
#ifdef RTCONFIG_PROXYSTA
		else if (strcmp(argv[1], "psta_monitor") == 0) {
			if(on) start_psta_monitor();
			else stop_psta_monitor();
		}
#endif
#endif
#ifdef RTCONFIG_IPERF
		else if (strcmp(argv[1], "monitor") == 0) {
			if(on) start_monitor();
			else stop_monitor();
		}
#endif
		else if (strcmp(argv[1], "qos") == 0) {//qos test
			if(on){
#ifdef RTCONFIG_RALINK
				if (module_loaded("hw_nat"))
				{
					modprobe_r("hw_nat");
					sleep(1);
#if 0
					f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "0", 0, 0);
					f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "0", 0, 0);
#endif
				}
#endif
			add_iQosRules(get_wan_ifname(wan_primary_ifunit()));
#ifdef RTCONFIG_BWDPI
				if(nvram_get_int("qos_type") == 1) {
					start_dpi_engine_service();
					// force to rebuild firewall to avoid some loopback issue
					if (nvram_match("fw_nat_loopback", "2"))
						start_firewall(wan_primary_ifunit(), 0);
				}
				else
#endif
				start_iQos();
			}
			else
			{
#ifdef RTCONFIG_RALINK
				if (nvram_get_int("hwnat") &&
					/* TODO: consider RTCONFIG_DUALWAN case */
//					!nvram_match("wan0_proto", "l2tp") &&
//					!nvram_match("wan0_proto", "pptp") &&
//					!(nvram_get_int("fw_pt_l2tp") || nvram_get_int("fw_pt_ipsec") &&
//					(nvram_match("wl0_radio", "0") || nvram_get_int("wl0_mrate_x")) &&
//					(nvram_match("wl1_radio", "0") || nvram_get_int("wl1_mrate_x")) &&
					!module_loaded("hw_nat"))
				{
#if 0
					f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "2", 0, 0);
					f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "2", 0, 0);
#endif

#if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2)
					if (!(!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")))
#endif
					{
						modprobe("hw_nat");
						sleep(1);
					}
				}
#endif
#ifdef RTCONFIG_BWDPI
				if(nvram_get_int("qos_type") == 1){
					stop_dpi_engine_service(1);
				}
				else
#endif
				stop_iQos();
				del_iQosRules();
			}
		}
#ifdef RTCONFIG_WEBDAV
		else if (strcmp(argv[1], "webdav") == 0) {
			if(on)
				start_webdav();
		}
#endif
#ifdef RTCONFIG_TUNNEL
		else if (strcmp(argv[1], "mastiff") == 0) {
			if(on)
				start_mastiff();
		}
#endif
		else if (strcmp(argv[1], "gpiow") == 0) {
			if(argc>=4) set_gpio(atoi(argv[2]), atoi(argv[3]));
		}
		else if (strcmp(argv[1], "gpior") == 0) {
			printf("%d\n", get_gpio(atoi(argv[2])));
		}
		else if (strcmp(argv[1], "gpiod") == 0) {
			if(argc>=4) gpio_dir(atoi(argv[2]), atoi(argv[3]));
		}
		else if (strcmp(argv[1], "init_switch") == 0) {
			init_switch();
		}
		else if (strcmp(argv[1], "set_action") == 0) {
			set_action(on);
		}
		else if (strcmp(argv[1], "pwr_usb") == 0) {
			set_pwr_usb(atoi(argv[2]));
			_dprintf("done.\n");
		}
		else if (strcmp(argv[1], "enc_chk") == 0) {
        		unsigned char enc_buf[ENC_WORDS_LEN];
        		unsigned char dec_buf[DATA_WORDS_LEN + 1];

			_dprintf("get enc str:[%s]\n", enc_str(argv[2], (char *) enc_buf));
			_dprintf("get dec str:[%s]\n", dec_str((char *) enc_buf, (char *) dec_buf));

			_dprintf("done(%d)\n", strcmp(argv[2], (const char *) dec_buf));
		}
#ifdef RTCONFIG_BCMFA
		else if (strcmp(argv[1], "fa_rev") == 0) {
			_dprintf("(%d) done.\n", get_fa_rev());
		}
		else if (strcmp(argv[1], "fa_dump") == 0) {
			_dprintf("(%d) done.\n", get_fa_dump());
		}
#endif
		else {
			printf("what?\n");
		}
	}
	return 0;
}
Beispiel #21
0
static void save(int quick) {
	int i;
	int n;
	int b;
	char hgz[256];
	char tmp[256];
	char bak[256];
	char bkp[256];
	time_t now;
	struct tm *tms;
	static int lastbak = -1;

	_dprintf("%s: quick=%d\n", __FUNCTION__, quick);

	f_write("/var/lib/misc/cstats-stime", &save_utime, sizeof(save_utime), 0, 0);

	n = save_history_from_tree(history_fn);
	_dprintf("%s: saved %d records from tree on file %s\n", __FUNCTION__, n, history_fn);

	_dprintf("%s: write source=%s\n", __FUNCTION__, save_path);
	f_write_string(source_fn, save_path, 0, 0);

	if (quick) {
		return;
	}

	sprintf(hgz, "%s.gz", history_fn);

	if (save_path[0] != 0) {
		strcpy(tmp, save_path);
		strcat(tmp, ".tmp");

		for (i = 15; i > 0; --i) {
			if (!wait_action_idle(10)) {
				_dprintf("%s: busy, not saving\n", __FUNCTION__);
			}
			else {
				_dprintf("%s: cp %s %s\n", __FUNCTION__, hgz, tmp);
				if (eval("cp", hgz, tmp) == 0) {
					_dprintf("%s: copy ok\n", __FUNCTION__);

					if (!nvram_match("rstats_bak", "0")) {
						now = time(0);
						tms = localtime(&now);
						if (lastbak != tms->tm_yday) {
							strcpy(bak, save_path);
							n = strlen(bak);
							if ((n > 3) && (strcmp(bak + (n - 3), ".gz") == 0)) n -= 3;
//							sprintf(bak + n, "_%d.bak", ((tms->tm_yday / 7) % 3) + 1);
//							if (eval("cp", save_path, bak) == 0) lastbak = tms->tm_yday;
							strcpy(bkp, bak);
							for (b = HI_BACK-1; b > 0; --b) {
								sprintf(bkp + n, "_%d.bak", b + 1);
								sprintf(bak + n, "_%d.bak", b);
								rename(bak, bkp);
							}
							if (eval("cp", "-p", save_path, bak) == 0) lastbak = tms->tm_yday;
						}
					}

					_dprintf("%s: rename %s %s\n", __FUNCTION__, tmp, save_path);
					if (rename(tmp, save_path) == 0) {
						_dprintf("%s: rename ok\n", __FUNCTION__);
						break;
					}
				}
			}

			// might not be ready
			sleep(3);
			if (gotterm) break;
		}
	}
}
Beispiel #22
0
void setup_conntrack(void)
{
	unsigned int v[10];
	const char *p;
	int i;
	
	p = nvram_safe_get("ct_tcp_timeout");
	if (sscanf(p, "%u%u%u%u%u%u%u%u%u%u",
		&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8], &v[9]) == 10) {	// lightly verify
		f_write_string("/proc/sys/net/ipv4/ip_conntrack_tcp_timeouts", p, 0, 0);
	}
	
	p = nvram_safe_get("ct_udp_timeout");
	if (sscanf(p, "%u%u", &v[0], &v[1]) == 2) {
		f_write_string("/proc/sys/net/ipv4/ip_conntrack_udp_timeouts", p, 0, 0);
	}
	
	p = nvram_safe_get("ct_max");
	i = atoi(p);
	if ((i >= 128) && (i <= 65535)) {
		f_write_string("/proc/sys/net/ipv4/ip_conntrack_max", p, 0, 0);
	}

	if (!nvram_match("nf_pptp", "0")) {
		modprobe("ip_conntrack_proto_gre");
		modprobe("ip_nat_proto_gre");
		modprobe("ip_conntrack_pptp");
		modprobe("ip_nat_pptp");
	}
	else {
		modprobe_r("ip_nat_pptp");
		modprobe_r("ip_conntrack_pptp");
		modprobe_r("ip_nat_proto_gre");
		modprobe_r("ip_conntrack_proto_gre");
	}

	if (!nvram_match("nf_h323", "0")) {
		modprobe("ip_conntrack_h323");
		modprobe("ip_nat_h323");
	}
	else {
		modprobe_r("ip_nat_h323");
		modprobe_r("ip_conntrack_h323");
	}
	
	if (!nvram_match("nf_rtsp", "0")) {
		modprobe("ip_conntrack_rtsp");
		modprobe("ip_nat_rtsp");
	}
	else {
		modprobe_r("ip_nat_rtsp");
		modprobe_r("ip_conntrack_rtsp");
	}

	if (!nvram_match("nf_ftp", "0")) {
		modprobe("ip_conntrack_ftp");
		modprobe("ip_nat_ftp");
	}
	else {
		modprobe_r("ip_nat_ftp");
		modprobe_r("ip_conntrack_ftp");
	}

}
Beispiel #23
0
void start_qos(void)
{
	int i;
	char *buf, *g, *p, *qos;
	unsigned int rate;
	unsigned int ceil;
	unsigned int bw;
	unsigned int incomingBandwidthInKilobitsPerSecond;
	unsigned int mtu;
	unsigned int r2q;
	unsigned int qosDefaultClassId;
	unsigned int overhead;
	FILE *f;
	int x;
	int inuse;
	char s[256];
	int first;
	char burst_root[32];
	char burst_leaf[32];

	qosDefaultClassId = (nvram_get_int("qos_default") + 1) * 10;
	incomingBandwidthInKilobitsPerSecond = strtoul(nvram_safe_get("qos_ibw"), NULL, 10);
	
	// move me?
	x = nvram_get_int("ne_vegas");
#ifdef LINUX26
	if (x) {
		char alpha[10], beta[10], gamma[10];
		sprintf(alpha, "alpha=%d", nvram_get_int("ne_valpha"));
		sprintf(beta, "beta=%d", nvram_get_int("ne_vbeta"));
		sprintf(gamma, "gamma=%d", nvram_get_int("ne_vgamma"));
		modprobe("tcp_vegas", alpha, beta, gamma);
		f_write_string("/proc/sys/net/ipv4/tcp_congestion_control", "vegas", 0, 0);
	}
	else {
		modprobe_r("tcp_vegas");
		f_write_string("/proc/sys/net/ipv4/tcp_congestion_control", "cubic", FW_NEWLINE, 0);
	}
#else
	f_write_string("/proc/sys/net/ipv4/tcp_vegas_cong_avoid", x ? "1" : "0", 0, 0);
	if (x) {
		f_write_string("/proc/sys/net/ipv4/tcp_vegas_alpha", nvram_safe_get("ne_valpha"), 0, 0);
		f_write_string("/proc/sys/net/ipv4/tcp_vegas_beta", nvram_safe_get("ne_vbeta"), 0, 0);
		f_write_string("/proc/sys/net/ipv4/tcp_vegas_gamma", nvram_safe_get("ne_vgamma"), 0, 0);
	}
#endif

	if (!nvram_get_int("qos_enable")) return;

	if ((f = fopen(qosfn, "w")) == NULL) return;

	i = nvram_get_int("qos_burst0");
	if (i > 0) sprintf(burst_root, "burst %dk", i);
		else burst_root[0] = 0;
	i = nvram_get_int("qos_burst1");
	if (i > 0) sprintf(burst_leaf, "burst %dk", i);
		else burst_leaf[0] = 0;

	mtu = strtoul(nvram_safe_get("wan_mtu"), NULL, 10);
	bw = strtoul(nvram_safe_get("qos_obw"), NULL, 10);
	overhead = strtoul(nvram_safe_get("atm_overhead"), NULL, 10);
	r2q = 10;

	if ((bw * 1000) / (8 * r2q) < mtu) {
		r2q = (bw * 1000) / (8 * mtu);
		if (r2q < 1) r2q = 1;
	} else if ((bw * 1000) / (8 * r2q) > 60000) {
		r2q = (bw * 1000) / (8 * 60000) + 1;
	}

	x = nvram_get_int("qos_pfifo");
	if (x == 1) {
		qos = "pfifo limit 256";
	} else if (x == 2) {
		qos = "codel";
	} else if (x == 3) {
		qos = "fq_codel";
	} else {
		qos = "sfq perturb 10";
	}
	if (overhead == 0) {
		fprintf(f,
			"#!/bin/sh\n"
			"WAN_DEV=%s\n"
			"IMQ_DEV=%s\n"
			"TQA=\"tc qdisc add dev $WAN_DEV\"\n"
			"TCA=\"tc class add dev $WAN_DEV\"\n"
			"TFA=\"tc filter add dev $WAN_DEV\"\n"
			"TQA_IMQ=\"tc qdisc add dev $IMQ_DEV\"\n"
			"TCA_IMQ=\"tc class add dev $IMQ_DEV\"\n"
			"TFA_IMQ=\"tc filter add dev $IMQ_DEV\"\n"
			"Q=\"%s\"\n"
			"\n"
			"case \"$1\" in\n"
			"start)\n"
			"\ttc qdisc del dev $WAN_DEV root 2>/dev/null\n"
			"\t$TQA root handle 1: htb default %u r2q %u\n"
			"\t$TCA parent 1: classid 1:1 htb rate %ukbit ceil %ukbit %s\n",
				get_wanface(),
				qosImqDeviceString,
				qos,
				qosDefaultClassId, r2q,
				bw, bw, burst_root);
	} else {
		fprintf(f,
			"#!/bin/sh\n"
			"WAN_DEV=%s\n"
			"IMQ_DEV=%s\n"
			"TQA=\"tc qdisc add dev $WAN_DEV\"\n"
			"TCA=\"tc class add dev $WAN_DEV\"\n"
			"TFA=\"tc filter add dev $WAN_DEV\"\n"
			"TQA_IMQ=\"tc qdisc add dev $IMQ_DEV\"\n"
			"TCA_IMQ=\"tc class add dev $IMQ_DEV\"\n"
			"TFA_IMQ=\"tc filter add dev $IMQ_DEV\"\n"
			"Q=\"%s\"\n"
			"\n"
			"case \"$1\" in\n"
			"start)\n"
			"\ttc qdisc del dev $WAN_DEV root 2>/dev/null\n"
			"\t$TQA root handle 1: htb default %u r2q %u\n"
			"\t$TCA parent 1: classid 1:1 htb rate %ukbit ceil %ukbit %s overhead %u linklayer atm\n",
				get_wanface(),
				qosImqDeviceString,
				qos,
				qosDefaultClassId, r2q,
				bw, bw, burst_root, overhead);
		}

	inuse = nvram_get_int("qos_inuse");

	g = buf = strdup(nvram_safe_get("qos_orates"));
	for (i = 0; i < 10; ++i) {
		if ((!g) || ((p = strsep(&g, ",")) == NULL)) break;

		if ((inuse & (1 << i)) == 0) continue;

		// check if we've got a percentage definition in the form of "rate-ceiling"
		if ((sscanf(p, "%u-%u", &rate, &ceil) != 2) || (rate < 1)) continue;	// 0=off

		if (ceil > 0) sprintf(s, "ceil %ukbit ", calc(bw, ceil));
			else s[0] = 0;
		x = (i + 1) * 10;

		if (overhead == 0) {
			fprintf(f,
				"# egress %d: %u-%u%%\n"
				"\t$TCA parent 1:1 classid 1:%d htb rate %ukbit %s %s prio %d quantum %u\n"
				"\t$TQA parent 1:%d handle %d: $Q\n"
				"\t$TFA parent 1: prio %d handle %d fw flowid 1:%d\n",
					i, rate, ceil,
					x, calc(bw, rate), s, burst_leaf, i+1, mtu,
					x, x,
					x, i + 1, x);
		} else {
			fprintf(f,
				"# egress %d: %u-%u%%\n"
				"\t$TCA parent 1:1 classid 1:%d htb rate %ukbit %s %s prio %d quantum %u overhead %u linklayer atm\n"
				"\t$TQA parent 1:%d handle %d: $Q\n"
				"\t$TFA parent 1: prio %d handle %d fw flowid 1:%d\n",
					i, rate, ceil,
					x, calc(bw, rate), s, burst_leaf, i+1, mtu, overhead,
					x, x,
					x, i + 1, x);
		}
	}
	free(buf);

//		"\t$TFA parent 1: prio 10 protocol ip u32 match ip tos 0x10 0xff flowid :10\n"	// TOS EF -> Highest


/*
		if (nvram_match("qos_ack", "1")) {
			fprintf(f,
				"\n"
				"\t$TFA parent 1: prio 15 protocol ip u32 "
				"match ip protocol 6 0xff "		// TCP
				"match u8 0x05 0x0f at 0 "		// IP header length
				"match u16 0x0000 0xffc0 at 2 "	// total length (0-63)
				"match u8 0x10 0xff at 33 "		// ACK only
				"flowid 1:10\n");
		}
		if (nvram_match("qos_icmp", "1")) {
			fputs("\n\t$TFA parent 1: prio 14 protocol ip u32 match ip protocol 1 0xff flowid 1:10\n", f);
		}
*/

/*
	if (nvram_get_int("qos_ack")) {
		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 14 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u16 0x0000 0xff80 at 2 "		// total length (0-127)
			"match u8 0x10 0xff at 33 "			// ACK only
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_syn")) {
		//	10000 = ACK
		//	00010 = SYN

		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 15 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u16 0x0000 0xff80 at 2 "		// total length (0-127)
			"match u8 0x02 0xff at 33 "			// SYN only
			"flowid 1:10\n"
			"\n"
			"\t$TFA parent 1: prio 16 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u16 0x0000 0xff80 at 2 "		// total length (0-127)
			"match u8 0x12 0xff at 33 "			// SYN,ACK
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_fin")) {
		//	10000 = ACK
		//	00001 = FIN

		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 17 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u8 0x11 0xff at 33 "			// ACK,FIN
			"flowid 1:10\n"
			"\n"
			"\t$TFA parent 1: prio 18 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u8 0x01 0xff at 33 "			// FIN
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_rst")) {
		//	10000 = ACK
		//	00100 = RST
		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 19 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u8 0x14 0xff at 33 "			// ACK,RST
			"flowid 1:10\n"
			"\n"
			"\t$TFA parent 1: prio 20 protocol ip u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u8 0x04 0xff at 33 "			// RST
			"flowid 1:10\n");
	}


	if (nvram_get_int("qos_icmp")) {
		fputs("\n\t$TFA parent 1: prio 13 protocol ip u32 match ip protocol 1 0xff flowid 1:10\n", f);
	}
*/

	/*
		10000 = ACK
		00100 = RST
		00010 = SYN
		00001 = FIN
	*/

	if (nvram_get_int("qos_ack")) {
		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 14 u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
//			"match u16 0x0000 0xff80 at 2 "		// total length (0-127)
			"match u16 0x0000 0xffc0 at 2 "		// total length (0-63)
			"match u8 0x10 0xff at 33 "			// ACK only
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_syn")) {
		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 15 u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u16 0x0000 0xffc0 at 2 "		// total length (0-63)
			"match u8 0x02 0x02 at 33 "			// SYN,*
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_fin")) {
		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 17 u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u16 0x0000 0xffc0 at 2 "		// total length (0-63)
			"match u8 0x01 0x01 at 33 "			// FIN,*
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_rst")) {
		fprintf(f,
			"\n"
			"\t$TFA parent 1: prio 19 u32 "
			"match ip protocol 6 0xff "			// TCP
			"match u8 0x05 0x0f at 0 "			// IP header length
			"match u16 0x0000 0xffc0 at 2 "		// total length (0-63)
			"match u8 0x04 0x04 at 33 "			// RST,*
			"flowid 1:10\n");
	}

	if (nvram_get_int("qos_icmp")) {
		fputs("\n\t$TFA parent 1: prio 13 u32 match ip protocol 1 0xff flowid 1:10\n", f);
	}


	////
	//// INCOMING TRAFFIC SHAPING
	////
	
	first = 1;
	overhead = strtoul(nvram_safe_get("atm_overhead"), NULL, 10);

	g = buf = strdup(nvram_safe_get("qos_irates"));
	
	for (i = 0; i < 10; ++i)
	{	
		if ((!g) || ((p = strsep(&g, ",")) == NULL))
		{
			break;
		}
		
		if ((inuse & (1 << i)) == 0)
		{
			continue;
		}

		// check if we've got a percentage definition in the form of "rate-ceiling"
		if ((sscanf(p, "%u-%u", &rate, &ceil) != 2) || (rate < 1))
		{
			continue;	// 0=off
		}
		
		// class ID
		unsigned int classid = ((unsigned int)i + 1) * 10;
		
		// priority
		unsigned int priority = (unsigned int)i + 1;			//prios 1-10 - Toastman
		
		// rate in kb/s
		unsigned int rateInKilobitsPerSecond =
			calc(incomingBandwidthInKilobitsPerSecond, rate);
		
		// ceiling in kb/s
		unsigned int ceilingInKilobitsPerSecond =
			calc(incomingBandwidthInKilobitsPerSecond, ceil);

		// burst rate (2% of the classes' rate) - don't know if we should use this
//Commented out KDB 20130531 - produces compiler warning about being unused!
//		unsigned int burstRateInBitsPerSecond =
//			(rateInKilobitsPerSecond * 1000) / 50;

		r2q = 10;
		if ((incomingBandwidthInKilobitsPerSecond * 1000) / (8 * r2q) < mtu) 
		{
			r2q = (incomingBandwidthInKilobitsPerSecond * 1000) / (8 * mtu);
			if (r2q < 1) r2q = 1;
		} 
		else if ((incomingBandwidthInKilobitsPerSecond * 1000) / (8 * r2q) > 60000) 
		{
			r2q = (incomingBandwidthInKilobitsPerSecond * 1000) / (8 * 60000) + 1;
		}

		if (first)
		{
			first = 0;
			fprintf(f,
				"\n"
				"\ttc qdisc del dev $I ingress 2>/dev/null\n"
				"\t$TQA handle ffff: ingress\n");
			if (overhead == 0) {
				fprintf(f,
				"\n"
				"\tip link set $IMQ_DEV up\n"
				"\ttc qdisc del dev $IMQ_DEV 2>/dev/null\n"
				"\t$TQA_IMQ handle 1: root htb default %u r2q %u\n"
				"\t$TCA_IMQ parent 1: classid 1:1 htb rate %ukbit ceil %ukbit\n",
				qosDefaultClassId, r2q,
				incomingBandwidthInKilobitsPerSecond,
				incomingBandwidthInKilobitsPerSecond);
			} else {
				fprintf(f,
					"\n"
					"\tip link set $IMQ_DEV up\n"
					"\ttc qdisc del dev $IMQ_DEV 2>/dev/null\n"
					"\t$TQA_IMQ handle 1: root htb default %u r2q %u\n"
					"\t$TCA_IMQ parent 1: classid 1:1 htb rate %ukbit ceil %ukbit overhead %u linklayer atm\n",
					qosDefaultClassId, r2q,
					incomingBandwidthInKilobitsPerSecond,
					incomingBandwidthInKilobitsPerSecond, overhead);
				}

			fprintf(f,
			"\n"
			"\t$TFA parent ffff: prio 10 u32 match ip %s action mirred egress redirect dev $IMQ_DEV\n", (nvram_get_int("qos_udp") == 1) ? "protocol 6 0xff" : "dst 0.0.0.0/0");
		}
		
		fprintf(
			f,
			"\n"
			"\t# class id %u: rate %ukbit ceil %ukbit\n",
			classid, rateInKilobitsPerSecond, ceilingInKilobitsPerSecond);

		if (overhead == 0) {
			fprintf(
				f,
				"\t$TCA_IMQ parent 1:1 classid 1:%u htb rate %ukbit ceil %ukbit prio %u quantum %u\n",
				classid, rateInKilobitsPerSecond, ceilingInKilobitsPerSecond, priority, mtu);
		} else {
			fprintf(
				f,
				"\t$TCA_IMQ parent 1:1 classid 1:%u htb rate %ukbit ceil %ukbit prio %u quantum %u overhead %u linklayer atm\n",
				classid, rateInKilobitsPerSecond, ceilingInKilobitsPerSecond, priority, mtu, overhead);
			}

		fprintf(
			f,
			"\t$TQA_IMQ parent 1:%u handle %u: $Q\n",
			classid, classid);

		fprintf(
			f,
			"\t$TFA_IMQ parent 1: prio %u handle %u fw flowid 1:%u \n",           
			classid, priority, classid);
	}

	free(buf);

	//// write commands which adds rule to forward traffic to IMQ device
	fputs(
		"\n"
		"\t# set up the IMQ device (otherwise this won't work) to limit the incoming data\n"
		"\tip link set $IMQ_DEV up\n",
		f);

	fprintf(f,
		"\t;;\n"
		"stop)\n"
		"\tip link set $IMQ_DEV down\n"
		"\ttc qdisc del dev $WAN_DEV root 2>/dev/null\n"
		"\ttc qdisc del dev $IMQ_DEV root 2>/dev/null\n"
		"\ttc filter del dev $WAN_DEV parent ffff: prio 10 u32 match ip %s action mirred egress redirect dev $IMQ_DEV 2>/dev/null\n"
		"\t;;\n"
		"*)\n"
		"\techo \"...\"\n"
		"\techo \"... OUTGOING QDISCS AND CLASSES FOR $WAN_DEV\"\n"
		"\techo \"...\"\n"
		"\ttc -s -d qdisc ls dev $WAN_DEV\n"
		"\techo\n"
		"\ttc -s -d class ls dev $WAN_DEV\n"
		"\techo\n"
		"\techo \"...\"\n"
		"\techo \"... INCOMING QDISCS AND CLASSES FOR $WAN_DEV (routed through $IMQ_DEV)\"\n"
		"\techo \"...\"\n"
		"\ttc -s -d qdisc ls dev $IMQ_DEV\n"
		"\techo\n"
		"\ttc -s -d class ls dev $IMQ_DEV\n"
		"\techo\n"
		"esac\n",
		(nvram_get_int("qos_udp") == 1) ? "protocol 6 0xff" : "dst 0.0.0.0/0");

	fclose(f);
	chmod(qosfn, 0700);
	eval((char *)qosfn, "start");
}
Beispiel #24
0
static void save(int quick)
{
	int i;
	char *bi, *bo;
	int n;
	int b;
	char hgz[256];
	char tmp[256];
	char bak[256];
	char bkp[256];
	time_t now;
	struct tm *tms;
	static int lastbak = -1;

	//_dprintf("%s: quick=%d\n", __FUNCTION__, quick);

	f_write("/var/lib/misc/rstats-stime", &save_utime, sizeof(save_utime), 0, 0);

	comp(speed_fn, speed, sizeof(speed[0]) * speed_count);

/*
	if ((now = time(0)) < Y2K) {
		_dprintf("%s: time not set\n", __FUNCTION__);
		return;
	}
*/

	comp(history_fn, &history, sizeof(history));

	//_dprintf("%s: write source=%s\n", __FUNCTION__, save_path);
	f_write_string(source_fn, save_path, 0, 0);

	if (quick) {
		return;
	}

	sprintf(hgz, "%s.gz", history_fn);

	if (strcmp(save_path, "*nvram") == 0) {
		if (!wait_action_idle(10)) {
			_dprintf("%s: busy, not saving\n", __FUNCTION__);
			return;
		}

		if ((n = f_read_alloc(hgz, &bi, 20 * 1024)) > 0) {
			if ((bo = malloc(base64_encoded_len(n) + 1)) != NULL) {
				n = base64_encode(bi, bo, n);
				bo[n] = 0;
				nvram_set("rstats_data", bo);
				if (!nvram_match("debug_nocommit", "1")) nvram_commit();

				_dprintf("%s: nvram commit\n", __FUNCTION__);

				free(bo);
			}
		}
		free(bi);
	}
	else if (save_path[0] != 0) {
		strcpy(tmp, save_path);
		strcat(tmp, ".tmp");

		for (i = 15; i > 0; --i) {
			if (!wait_action_idle(10)) {
				_dprintf("%s: busy, not saving\n", __FUNCTION__);
			}
			else {
				_dprintf("%s: cp %s %s\n", __FUNCTION__, hgz, tmp);
				if (eval("cp", hgz, tmp) == 0) {
					_dprintf("%s: copy ok\n", __FUNCTION__);

					if (!nvram_match("rstats_bak", "0")) {
						now = time(0);
						tms = localtime(&now);
						if (lastbak != tms->tm_yday) {
							strcpy(bak, save_path);
							n = strlen(bak);
							if ((n > 3) && (strcmp(bak + (n - 3), ".gz") == 0)) n -= 3;
							strcpy(bkp, bak);
							for (b = HI_BACK-1; b > 0; --b) {
								sprintf(bkp + n, "_%d.bak", b + 1);
								sprintf(bak + n, "_%d.bak", b);
								rename(bak, bkp);
							}
							if (eval("cp", "-p", save_path, bak) == 0) lastbak = tms->tm_yday;
						}
					}

					_dprintf("%s: rename %s %s\n", __FUNCTION__, tmp, save_path);
					if (rename(tmp, save_path) == 0) {
						_dprintf("%s: rename ok\n", __FUNCTION__);
						break;
					}
				}
			}

			// might not be ready
			sleep(3);
			if (gotterm) break;
		}
	}
}
Beispiel #25
0
static void safe_leave(int signo){
	csprintf("\n## wanduck.safeexit ##\n");
	signal(SIGTERM, SIG_IGN);
	signal(SIGUSR1, SIG_IGN);
	signal(SIGINT, SIG_IGN);

	FD_ZERO(&allset);
	close(http_sock);
	close(dns_sock);

	int i, ret;
	for(i = 0; i < maxfd; ++i){
		ret = close(i);
		csprintf("## close %d: result=%d.\n", i, ret);
	}

	sleep(1);

#ifdef RTCONFIG_WIRELESSREPEATER
	if(sw_mode == SW_MODE_REPEATER){
		eval("ebtables", "-t", "broute", "-F");
		eval("ebtables", "-t", "filter", "-F");
		f_write_string("/proc/net/dnsmqctrl", "", 0, 0);
	}
#endif

	if(rule_setup == 1){
		csprintf("\n# Disable direct rule(exit wanduck)\n");

		rule_setup = 0;
		conn_changed_state[current_wan_unit] = CONNED; // for cleaning the redirect rules.

#if 0
		// in the function safe_leave(), couldn't set any nvram using nvram_set().
		// So the notify mechanism would be invalid.
#if 0
		handle_wan_line(current_wan_unit, rule_setup);
#else // or
		start_nat_rules();
#endif
#else
		// Couldn't directly use nvram_set().
		// Must use the command: nvram, and set the nat_state.
		char buf[16];
		FILE *fp = fopen("/tmp/nat_rules", "r");

		memset(buf, 0, 16);
		sprintf(buf, "nat_state=%d", NAT_STATE_NORMAL);

		if(fp != NULL){
			fclose(fp);

			eval("nvram", "set", buf);

			_dprintf("%s: apply the nat_rules: %s!\n", __FUNCTION__, buf);
			logmessage("wanduck exit", "apply the nat_rules!");

			setup_ct_timeout(TRUE);
			setup_udp_timeout(TRUE);

			eval("iptables-restore", "/tmp/nat_rules");
		}
#endif
	}

	remove(WANDUCK_PID_FILE);

csprintf("\n# return(exit wanduck)\n");
	exit(0);
}