コード例 #1
0
ファイル: boardapi.c プロジェクト: themiron/asuswrt-merlin
void led_control_lte(int percent)
{
	if(percent >= 0)
	{ //high priority led for LTE
		int LTE_LED[] = { LED_SIG1, LED_SIG2, LED_SIG3, LED_USB, LED_LAN, LED_2G, LED_5G, LED_WAN, LED_LTE, LED_POWER };
		int i;
		nvram_set(LED_CTRL_HIPRIO, "1");
		for(i = 0; i < ARRAY_SIZE(LTE_LED); i++)
		{
			if((percent/9) > i)
				do_led_control(LTE_LED[i], LED_ON);
			else
				do_led_control(LTE_LED[i], LED_OFF);
		}
	}
	else if(nvram_match(LED_CTRL_HIPRIO, "1"))
	{ //restore
		int which, mode;
		char name[16];
		char *p;

		nvram_unset(LED_CTRL_HIPRIO);

		for(which = 0; which < LED_ID_MAX; which++)
		{
			sprintf(name, "led%02d", which);
			if ((p = nvram_get(name)) != NULL)
			{
				mode = atoi(p);
				do_led_control(which, mode);
			}
		}
	}
}
コード例 #2
0
ファイル: qca8337.c プロジェクト: rjknight123/asuswrt-merlin
static void build_wan_lan_mask(int stb)
{
	int i, unit;
	int wanscap_lan = get_wans_dualwan() & WANSCAP_LAN;
	int wans_lanport = nvram_get_int("wans_lanport");
	int sw_mode = nvram_get_int("sw_mode");
	char prefix[8], nvram_ports[20];

	if (sw_mode == SW_MODE_AP || sw_mode == SW_MODE_REPEATER)
		wanscap_lan = 0;

	if (stb == 100 && (sw_mode == SW_MODE_AP || __mediabridge_mode(sw_mode)))
		stb = 7;	/* Don't create WAN port. */

#if 0	/* TODO: no WAN port */
	if ((get_wans_dualwan() & (WANSCAP_LAN | WANSCAP_WAN)) == 0)
		stb = 7; // no WAN?
#endif

	if (wanscap_lan && (wans_lanport < 0 || wans_lanport > 4)) {
		_dprintf("%s: invalid wans_lanport %d!\n", __func__, wans_lanport);
		wanscap_lan = 0;
	}

	lan_mask = wan_mask = wans_lan_mask = 0;
	for (i = 0; i < NR_WANLAN_PORT; ++i) {
		switch (lan_wan_partition[stb][i]) {
		case 0:
			wan_mask |= 1U << lan_id_to_port_nr(i);
			break;
		case 1:
			lan_mask |= 1U << lan_id_to_port_nr(i);
			break;
		default:
			_dprintf("%s: Unknown LAN/WAN port definition. (stb %d i %d val %d)\n",
				__func__, stb, i, lan_wan_partition[stb][i]);
		}
	}

	//DUALWAN
	if (wanscap_lan) {
		wans_lan_mask = 1U << lan_id_to_port_nr(wans_lanport);
		lan_mask &= ~wans_lan_mask;
	}

	for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit) {
		sprintf(prefix, "%d", unit);
		sprintf(nvram_ports, "wan%sports_mask", (unit == WAN_UNIT_FIRST)?"":prefix);

		if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_WAN) {
			nvram_set_int(nvram_ports, wan_mask);
		}
		else if (get_dualwan_by_unit(unit) == WANS_DUALWAN_IF_LAN) {
			nvram_set_int(nvram_ports, wans_lan_mask);
		}
		else
			nvram_unset(nvram_ports);
	}
	nvram_set_int("lanports_mask", lan_mask);
}
コード例 #3
0
ファイル: nvram_linux.c プロジェクト: jhu-chang/r6300v2
static ssize_t
dev_nvram_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
{
	char tmp[512], *name = tmp, *value;
	ssize_t ret;

	if ((count+1) > sizeof(tmp)) {
		if (!(name = kmalloc(count+1, GFP_KERNEL)))
			return -ENOMEM;
	}

	if (copy_from_user(name, buf, count)) {
		ret = -EFAULT;
		goto done;
	}
	name[count] = '\0';
	value = name;
	name = strsep(&value, "=");
	if (value)
		ret = nvram_set(name, value) ;
	else
		ret = nvram_unset(name) ;

	if( 0 == ret )
		ret = count;
done:
	if (name != tmp)
		kfree(name);

	return ret;
}
コード例 #4
0
ファイル: nvparse.c プロジェクト: Einheri/wl500g
bool
del_forward_port(int which)
{
	char name[NV_FORWARD_PORT_MAXLEN];

	snprintf(name, sizeof(name), "forward_port%d", which);
	return (nvram_unset(name) == 0) ? TRUE : FALSE;
}
コード例 #5
0
ファイル: nvparse.c プロジェクト: Einheri/wl500g
bool
del_autofw_port(int which)
{
	char name[] = "autofw_portXXXXXXXXXX";

	snprintf(name, sizeof(name), "autofw_port%d", which);
	return (nvram_unset(name) == 0) ? TRUE : FALSE;
}
コード例 #6
0
ファイル: nvram.c プロジェクト: eckyecky/rt-n56u
/* NVRAM utility */
int
main(int argc, char **argv)
{
	char *name, *value;

	/* Skip program name */
	--argc;
	++argv;

	if (!*argv) 
		usage();

	/* Process the remaining arguments. */
	for (; *argv; argv++) {
		if (!strncmp(*argv, "get", 3)) {
			if (*++argv) {
				if ((value = nvram_get(*argv)))
					puts(value);
			}
		}
		else if (!strncmp(*argv, "set", 3)) {
			if (*++argv) {
				char buf[1024];
				strncpy(value = buf, *argv, sizeof(buf)-1);
				name = strsep(&value, "=");
				nvram_set(name, value);
			}
		}
		else if (!strncmp(*argv, "unset", 5)) {
			if (*++argv)
				nvram_unset(*argv);
		}
		else if (!strncmp(*argv, "commit", 6)) {
			nvram_commit();
		}
		else if (!strncmp(*argv, "clear", 5)) {
			nvram_clear();
		}
		else if (!strncmp(*argv, "save", 4)) 
		{
			if (*++argv) 
				nvram_save_new(*argv);
		}
		else if (!strncmp(*argv, "restore", 7)) 
		{
			if (*++argv) 
				nvram_restore_new(*argv);
		}
		else if (!strncmp(*argv, "show", 4) || !strncmp(*argv, "getall", 6)) {
			nvram_show_new();
		}
		if (!*argv)
			break;
	}

	return 0;
}
コード例 #7
0
ファイル: nvram_linux.c プロジェクト: eckyecky/rt-n56u
/* User mode interface below */
int
user_nvram_set(anvram_ioctl_t __user *nvr)
{
	int ret;
	char param[NVRAM_MAX_PARAM_LEN];
	char tmp[64], *value;

	if (!nvr)
		return -EINVAL;

	if (nvr->size != sizeof(anvram_ioctl_t))
		return -EINVAL;

	if (nvr->len_param > (NVRAM_MAX_PARAM_LEN-1) || nvr->len_param < 0)
		return -EOVERFLOW;

	if (!nvr->param)
		return -EINVAL;

	if (copy_from_user(param, nvr->param, nvr->len_param))
		return -EFAULT;

	param[nvr->len_param] = '\0';
	if (!param[0])
		return -EINVAL;

	if (nvr->len_value > (NVRAM_MAX_VALUE_LEN-1) || nvr->len_value < 0)
		return -EOVERFLOW;

	value = tmp;
	if ((nvr->len_value+1) > sizeof(tmp)) {
		if (!(value = kmalloc(nvr->len_value+1, GFP_KERNEL)))
			return -ENOMEM;
	}

	if (nvr->len_value > 0 && nvr->value) {
		if (copy_from_user(value, nvr->value, nvr->len_value)) {
			ret = -EFAULT;
			goto done;
		}
		value[nvr->len_value] = '\0';
	} else {
		value[0] = '\0';
	}

	if (nvr->value)
		ret = nvram_set(param, value);
	else
		ret = nvram_unset(param);

done:
	if (value != tmp)
		kfree(value);

	return ret;
}
コード例 #8
0
static void nv_fix_wl(const char *oldnv, const char *newnv)
{
	char *p;

	if (nvram_get(wl_nvname(newnv, 0, 0)) == NULL) {
		p = nvram_get(oldnv);
		if (p != NULL) nvram_set(wl_nvname(newnv, -1, 0), p);
		nvram_unset(oldnv);
	}
}
コード例 #9
0
void aoss_save(webs_t wp)
{
	char buf[32];
	nvram_set("aoss_enable", websGetVar(wp, "aoss_enable", "0"));
	nvram_set("aoss_aes", websGetVar(wp, "aoss_aes", "0"));
	nvram_set("aoss_tkip", websGetVar(wp, "aoss_tkip", "0"));
	nvram_set("aoss_wep", websGetVar(wp, "aoss_wep", "0"));
#ifdef HAVE_WPS
	nvram_set("wps_enabled", websGetVar(wp, "wps_enabled", "0"));
	char *pin = websGetVar(wp, "wps_ap_pin", NULL);
	if (pin)
		nvram_set("pincode", pin);
#endif
	// check if at least one value was set
	if (nvram_match("aoss_aes", "0")
	    && nvram_match("aoss_tkip", "0")
	    && nvram_match("aoss_wep", "0")) {
		nvram_set("aoss_aes", "1");
	}
	if (strlen(nvram_safe_get("aoss_vifs"))) {
		nvram_unset("ath0_vifs");
		nvram_unset("aoss_vifs");
		nvram_commit();
	}
	if (strlen(nvram_safe_get("aossa_vifs"))) {
		nvram_unset("ath1_vifs");
		nvram_unset("aossa_vifs");
		nvram_commit();
	}
	char *registrar = websGetVar(wp, "wps_registrar", NULL);
	if (registrar && nvram_invmatch("wps_registrar", registrar)) {
		nvram_set("wps_registrar", registrar);
		addAction("wireless");
		nvram_set("nowebaction", "1");
		service_restart();
	}
	// all other vars
	//validate_cgi(wp);
}
コード例 #10
0
static int ren_main(int argc, char **argv)
{
	char *p;

	if ((p = nvram_get(argv[1])) == NULL) {
		fprintf(stderr, "Unable to find %s\n", argv[1]);
		return 1;
	}
	if (strcmp(argv[1], argv[2]) != 0) {
		nvram_set(argv[2], p);
		nvram_unset(argv[1]);
	}
	return 0;
}
コード例 #11
0
ファイル: main.c プロジェクト: Cribstone/linino
/* NVRAM utility */
int
main(int argc, char **argv)
{
	char *name, *value, buf[NVRAM_SPACE];
	int size;

	/* Skip program name */
	--argc;
	++argv;

	if (!*argv) 
		usage();

	/* Process the remaining arguments. */
	for (; *argv; argv++) {
		if (!strncmp(*argv, "get", 3)) {
			if (*++argv) {
				if ((value = nvram_get(*argv))) {
					puts_trim_cr(value);
				}
			}
		}
		else if (!strncmp(*argv, "set", 3)) {
			if (*++argv) {
				strncpy(value = buf, *argv, sizeof(buf));
				name = strsep(&value, "=");
				nvram_set(name, value);
			}
		}
		else if (!strncmp(*argv, "unset", 5)) {
			if (*++argv)
				nvram_unset(*argv);
		}
		else if (!strncmp(*argv, "commit", 5)) {
			nvram_commit();
		}
		else if (!strncmp(*argv, "show", 4) ||
			   !strncmp(*argv, "getall", 6)) {
			nvram_getall(buf, sizeof(buf));
			for (name = buf; *name; name += strlen(name) + 1)
				puts_trim_cr(name);
			size = sizeof(struct nvram_header) + (int) name - (int) buf;
			fprintf(stderr, "size: %d bytes (%d left)\n", size, NVRAM_SPACE - size);
		}
		if (!*argv)
			break;
	}

	return 0;
}	
コード例 #12
0
// restore nvram from file
int nvram_restore(char *file, char *buf)
{
   	FILE *fp;
   	char header[8], *p, *v;
  	unsigned long count, *filelen;

   	if ((fp=fopen(file, "r+"))==NULL) return -1;
    	   
   	count = fread(header, 1, 8, fp);
   	if (count>=8 && strncmp(header, PROFILE_HEADER, 4)==0)
   	{  
	    filelen = (unsigned long *)(header + 4);
   	    count = fread(buf, 1, *filelen, fp);
   	}   
   	fclose(fp);

   	p = buf;       		   
   
   	while (*p)
   	{       
		//printf("nv:%s\n", p);     	     			     	
       		v = strchr(p, '=');

		if (v!=NULL)
		{	
			*v++ = '\0' /*eric--NULL*/;

			if (issyspara(p))
			{
				nvram_set(p, v);
			}

       			p = v + strlen(v) + 1;			
		}
		else 
		{
			nvram_unset(p);
			p = p + 1;
		}
   	}

	nvram_set("x_Setting", "1");
	return 0;
}
コード例 #13
0
ファイル: tomato.c プロジェクト: twtomato/twtomato
static void wo_logout(char *url)
{
	char s[256];

	// doesn't work with all browsers...

	if (((user_agent) && (strstr(user_agent, "Opera") != NULL))) {
		sprintf(s, "%llx", (unsigned long long)time(NULL) * rand());
		send_authenticate(s);
	}
	else {
		send_authenticate(NULL);
	}

#if 0
	gen_sessnum();
#endif

	//
#if 0
	char *c;
	char *p;

	p = nvram_safe_get("web_out");
	c = inet_ntoa(clientsai.sin_addr);
	if ((c != NULL) && (!find_word(p, c))) {
		while (strlen(p) > 128) {
			p = strchr(p, ',');
			if (!p) break;
			++p;
		}
		if ((p) && (*p)) {
			sprintf(s, "%s,%s", p, c);
			nvram_set("web_out", s);
		}
		else {
			nvram_set("web_out", c);
		}
		nvram_unset("web_outx");
	}
#endif
}
コード例 #14
0
void stop_single_pppoe(int pppoe_num)
{
	int ret;
	char pppoe_pid[15], pppoe_ifname[15];
	char ppp_unlink[2][20] = { "/tmp/ppp/link", "/tmp/ppp/link_1" };
	char ppp_wan_dns[2][20] = { "wan_get_dns", "wan_get_dns_1" };

	sprintf(pppoe_pid, "pppoe_pid%d", pppoe_num);
	sprintf(pppoe_ifname, "pppoe_ifname%d", pppoe_num);
	dprintf("start! stop pppoe %d, pid %s \n", pppoe_num, nvram_safe_get(pppoe_pid));

	ret = eval("kill", nvram_safe_get(pppoe_pid));
	unlink(ppp_unlink[pppoe_num]);
	nvram_unset(pppoe_ifname);

	nvram_set(ppp_wan_dns[pppoe_num], "");
	stop_dns_clear_resolv();

	dprintf("done\n");
	return;
}
コード例 #15
0
void start_l2tp(int status)
{
	int ret;
	FILE *fp;
	char *l2tp_argv[] = { "xl2tpd",
		NULL
	};
	char username[80], passwd[80];

	// stop_dhcpc();
#ifdef HAVE_PPPOE
	stop_pppoe();
#endif
#ifdef HAVE_PPTP
	stop_pptp();
#endif
	stop_l2tp();

	snprintf(username, sizeof(username), "%s",
		 nvram_safe_get("ppp_username"));
	snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));

	if (status != REDIAL) {
		insmod("ipv6");
		insmod("l2tp_core");
		insmod("l2tp_netlink");
		insmod("l2tp_ppp");
		mkdir("/tmp/ppp", 0777);
		mkdir("/var/run/xl2tpd", 0777);
		mkdir("/tmp/xl2tpd", 0777);
		symlink("/sbin/rc", "/tmp/ppp/ip-up");
		symlink("/sbin/rc", "/tmp/ppp/ip-down");
		symlink("/dev/null", "/tmp/ppp/connect-errors");

		/*
		 * Generate L2TP configuration file 
		 */
		if (!(fp = fopen("/tmp/xl2tpd/xl2tpd.conf", "w"))) {
			perror("/tmp/xl2tpd/xl2tpd.conf");
			return;
		}
/*[global]
port = 1701
;auth file = /etc/xl2tpd/xl2tp-secrets

[lac fbnl2tpserver]
lns = 10.64.1.237
require chap = yes
refuse pap = yes
require authentication = yes
; Name should be the same as the username in the PPP authentication!
name = dani
ppp debug = yes
pppoptfile = /etc/xl2tpd/options.l2tp
length bit = yes
*/

		fprintf(fp, "[global]\n");	// Global section
		fprintf(fp, "port = 1701\n");	// Bind address
		fprintf(fp, "[lac %s]\n", nvram_safe_get("l2tp_server_name"));
		fprintf(fp, "lns = %s\n", nvram_safe_get("l2tp_server_name"));
		fprintf(fp, "require chap = %s\n",
			nvram_default_get("l2tp_req_chap", "yes"));
		fprintf(fp, "refuse pap = %s\n",
			nvram_default_get("l2tp_ref_pap", "yes"));
		fprintf(fp, "redial = yes\n");
		fprintf(fp, "redial timeout = 15\n");
		fprintf(fp, "require authentication = %s\n",
			nvram_default_get("l2tp_req_auth", "yes"));
		fprintf(fp, "name = %s\n", username);
		fprintf(fp, "pppoptfile = /tmp/ppp/options\n");
		fprintf(fp, "length bit = yes\n");
		fclose(fp);

		/*
		 * Generate options file 
		 */
		if (!(fp = fopen("/tmp/ppp/options", "w"))) {
			perror("/tmp/ppp/options");
			return;
		}

		if (nvram_match("mtu_enable", "1")) {
			if (atoi(nvram_safe_get("wan_mtu")) > 0) {
				fprintf(fp, "mtu %s\n",
					nvram_safe_get("wan_mtu"));
				fprintf(fp, "mru %s\n",
					nvram_safe_get("wan_mtu"));
			}

		}

		fprintf(fp, "defaultroute\n");	// Add a default route to the 
		// system routing tables,
		// using the peer as the
		// gateway
		fprintf(fp, "usepeerdns\n");	// Ask the peer for up to 2 DNS
		// server addresses
		// fprintf(fp, "pty 'pptp %s
		// --nolaunchpppd'\n",nvram_safe_get("pptp_server_ip")); 
		fprintf(fp, "user '%s'\n", username);
		// fprintf(fp, "persist\n"); // Do not exit after a connection is
		// terminated.

		if (nvram_match("ppp_demand", "1")) {	// demand mode
			fprintf(fp, "idle %d\n",
				nvram_match("ppp_demand",
					    "1") ?
				atoi(nvram_safe_get("ppp_idletime")) * 60 : 0);
			// fprintf(fp, "demand\n"); // Dial on demand
			// fprintf(fp, "persist\n"); // Do not exit after a connection is 
			// terminated.
			// fprintf(fp, "%s:%s\n",PPP_PSEUDO_IP,PPP_PSEUDO_GW); // <local
			// IP>:<remote IP>
			fprintf(fp, "ipcp-accept-remote\n");
			fprintf(fp, "ipcp-accept-local\n");
			fprintf(fp, "connect true\n");
			fprintf(fp, "noipdefault\n");	// Disables the default
			// behaviour when no local IP 
			// address is specified
			fprintf(fp, "ktune\n");	// Set /proc/sys/net/ipv4/ip_dynaddr
			// to 1 in demand mode if the local
			// address changes
		} else {	// keepalive mode
			start_redial();
		}

		fprintf(fp, "default-asyncmap\n");	// Disable asyncmap
		fprintf(fp, "crtscts\n");	// Disable protocol field compression
		// negotiation
		fprintf(fp, "nopcomp\n");	// Disable protocol field compression
		fprintf(fp, "refuse-eap\n");	// Disable protocol field compression
		fprintf(fp, "noaccomp\n");	// Disable Address/Control
		// compression 
		fprintf(fp, "noccp\n");	// Disable CCP (Compression Control
		// Protocol)
		fprintf(fp, "novj\n");	// Disable Van Jacobson style TCP/IP
		// header compression
		fprintf(fp, "nobsdcomp\n");	// Disables BSD-Compress compression
		fprintf(fp, "nodeflate\n");	// Disables Deflate compression
		fprintf(fp, "lcp-echo-interval 0\n");	// Don't send an LCP
		// echo-request frame to the
		// peer
		fprintf(fp, "lock\n");
		fprintf(fp, "noauth\n");
//              fprintf(fp, "debug\n");

		fclose(fp);

		/*
		 * Generate pap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
			perror("/tmp/ppp/pap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/pap-secrets", 0600);

		/*
		 * Generate chap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
			perror("/tmp/ppp/chap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/chap-secrets", 0600);

		/*
		 * Enable Forwarding 
		 */
		if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
			fputc('1', fp);
			fclose(fp);
		} else
			perror("/proc/sys/net/ipv4/ip_forward");
	}

	/*
	 * Bring up WAN interface 
	 */
	// ifconfig(nvram_safe_get("wan_ifname"), IFUP,
	// nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));

	ret = _evalpid(l2tp_argv, NULL, 0, NULL);
	sleep(1);

	if (nvram_match("ppp_demand", "1")) {
		/*
		 * Trigger Connect On Demand if user press Connect button in Status
		 * page 
		 */
		if (nvram_match("action_service", "start_l2tp")) {
			start_force_to_dial();
			nvram_unset("action_service");
		}
		/*
		 * Trigger Connect On Demand if user ping pptp server 
		 */
		else
			eval("listen", nvram_safe_get("lan_ifname"));
	} else {
		sysprintf("echo \"c %s\" >  /var/run/xl2tpd/l2tp-control",
			  nvram_safe_get("l2tp_server_name"));
	}

	cprintf("done\n");
	return;
}
コード例 #16
0
ファイル: nvram.c プロジェクト: eckyecky/rt-n56u
int nvram_restore_new(char *file)
{
	FILE *fp;
	char header[8], *p, *v, *buf;
	unsigned long count, filelen, *filelenptr, i;
	unsigned char rand, *randptr;

	if ((fp = fopen(file, "r+")) == NULL) return -1;

	buf = malloc(NVRAM_SPACE);
	if (!buf) {
		fclose(fp);
		perror ("Out of memory!\n");
		return -1;
	}

	buf[0] = 0;

	count = fread(header, 1, 8, fp);
	if (count>=8 && strncmp(header, PROFILE_HEADER, 4) == 0)
	{
		filelenptr = (unsigned long *)(header + 4);
		fread(buf, 1, *filelenptr, fp);
	}
	else if (count>=8 && strncmp(header, PROFILE_HEADER_NEW, 4) == 0)
	{
		filelenptr = (unsigned long *)(header + 4);
		filelen = *filelenptr & 0xffffff;
		randptr = (unsigned char *)(header + 7);
		rand = *randptr;
		if (filelen > NVRAM_SPACE)
			filelen = NVRAM_SPACE;
		count = fread(buf, 1, filelen, fp);
		for (i = 0; i < count; i++)
		{
			if ((unsigned char) buf[i] > ( 0xfd - 0x1))
				buf[i] = 0x0;
			else
				buf[i] = 0xff + rand - buf[i];
		}
	}
	else
	{
		fclose(fp);
		return 0;
	}
	fclose(fp);

	nvram_clear();

	p = buf;

	while (*p)
	{
		v = strchr(p, '=');
		if (v != NULL)
		{
			*v++ = 0;
			if (is_sys_param(p))
				nvram_set(p, v);
			p = v + strlen(v) + 1;
		}
		else
		{
			nvram_unset(p);
			p = p + 1;
		}
	}

	free(buf);

	nvram_set("x_Setting", "1");
	nvram_set("w_Setting", "1");
	return 0;
}
コード例 #17
0
ファイル: init-ralink.c プロジェクト: nngx/asuswrt-merlin
void init_syspara(void)
{
	unsigned char buffer[16];
	unsigned char *dst;
	unsigned int bytes;
	int i;
	char macaddr[]="00:11:22:33:44:55";
	char macaddr2[]="00:11:22:33:44:58";
	char country_code[3];
	char pin[9];
	char productid[13];
	char fwver[8];
	char blver[20];
	unsigned char txbf_para[33];
	char ea[ETHER_ADDR_LEN];

	nvram_set("buildno", rt_serialno);
	nvram_set("extendno", rt_extendno);
	nvram_set("buildinfo", rt_buildinfo);
	nvram_set("swpjverno", rt_swpjverno);

	/* /dev/mtd/2, RF parameters, starts from 0x40000 */
	dst = buffer;
	bytes = 6;
	memset(buffer, 0, sizeof(buffer));
	memset(country_code, 0, sizeof(country_code));
	memset(pin, 0, sizeof(pin));
	memset(productid, 0, sizeof(productid));
	memset(fwver, 0, sizeof(fwver));
	memset(txbf_para, 0, sizeof(txbf_para));

	if (FRead(dst, OFFSET_MAC_ADDR, bytes)<0)
	{
		_dprintf("READ MAC address: Out of scope\n");
	}
	else
	{
		if (buffer[0]!=0xff)
			ether_etoa(buffer, macaddr);
	}

#if !defined(RTN14U) // single band
	if (FRead(dst, OFFSET_MAC_ADDR_2G, bytes)<0)
	{
		_dprintf("READ MAC address 2G: Out of scope\n");
	}
	else
	{
		if (buffer[0]!=0xff)
			ether_etoa(buffer, macaddr2);
	}
#endif

#if defined(RTN14U) // single band
	if (!mssid_mac_validate(macaddr))
#else
	if (!mssid_mac_validate(macaddr) || !mssid_mac_validate(macaddr2))
#endif
		nvram_set("wl_mssid", "0");
	else
		nvram_set("wl_mssid", "1");

#if defined(RTN14U) // single band
	nvram_set("et0macaddr", macaddr);
	nvram_set("et1macaddr", macaddr);
#else
	//TODO: separate for different chipset solution
	nvram_set("et0macaddr", macaddr);
	nvram_set("et1macaddr", macaddr2);
#endif

	if (FRead(dst, OFFSET_MAC_GMAC0, bytes)<0)
		dbg("READ MAC address GMAC0: Out of scope\n");
	else
	{
		if (buffer[0]==0xff)
		{
			if (ether_atoe(macaddr, ea))
				FWrite(ea, OFFSET_MAC_GMAC0, 6);
		}
	}

	if (FRead(dst, OFFSET_MAC_GMAC2, bytes)<0)
		dbg("READ MAC address GMAC2: Out of scope\n");
	else
	{
		if (buffer[0]==0xff)
		{
			if (ether_atoe(macaddr2, ea))
				FWrite(ea, OFFSET_MAC_GMAC2, 6);
		}
	}

	/* reserved for Ralink. used as ASUS country code. */
#if ! defined(RTCONFIG_NEW_REGULATION_DOMAIN)
	dst = (unsigned char*) country_code;
	bytes = 2;
	if (FRead(dst, OFFSET_COUNTRY_CODE, bytes)<0)
	{
		_dprintf("READ ASUS country code: Out of scope\n");
		nvram_set("wl_country_code", "");
	}
	else
	{
		chk_valid_country_code(country_code);
		nvram_set("wl_country_code", country_code);
		nvram_set("wl0_country_code", country_code);
		nvram_set("wl1_country_code", country_code);
	}
#else	/* ! RTCONFIG_NEW_REGULATION_DOMAIN */
	dst = buffer;

	bytes = MAX_REGSPEC_LEN;
	memset(dst, 0, MAX_REGSPEC_LEN+1);
	if(FRead(dst, REGSPEC_ADDR, bytes) < 0)
		nvram_set("reg_spec", "FCC"); // DEFAULT
	else
	{
		for (i=(MAX_REGSPEC_LEN-1);i>=0;i--) {
			if ((dst[i]==0xff) || (dst[i]=='\0'))
				dst[i]='\0';
		}
		if (dst[0]!=0x00)
			nvram_set("reg_spec", dst);
		else
			nvram_set("reg_spec", "FCC"); // DEFAULT
	}

	if (FRead(dst, REG2G_EEPROM_ADDR, MAX_REGDOMAIN_LEN)<0 || memcmp(dst,"2G_CH", 5) != 0)
	{
		_dprintf("READ ASUS country code: Out of scope\n");
		nvram_set("wl_country_code", "");
		nvram_set("wl0_country_code", "DB");
		nvram_set("wl_reg_2g", "2G_CH14");
	}
	else
	{
		for(i = 0; i < MAX_REGDOMAIN_LEN; i++)
			if(dst[i] == 0xff || dst[i] == 0)
				break;

		dst[i] = 0;
		nvram_set("wl_reg_2g", dst);
		if      (strcmp(dst, "2G_CH11") == 0)
			nvram_set("wl0_country_code", "US");
		else if (strcmp(dst, "2G_CH13") == 0)
			nvram_set("wl0_country_code", "GB");
		else if (strcmp(dst, "2G_CH14") == 0)
			nvram_set("wl0_country_code", "DB");
		else
			nvram_set("wl0_country_code", "DB");
	}

	if (FRead(dst, REG5G_EEPROM_ADDR, MAX_REGDOMAIN_LEN)<0 || memcmp(dst,"5G_", 3) != 0)
	{
		_dprintf("READ ASUS country code: Out of scope\n");
		nvram_set("wl_country_code", "");
		nvram_set("wl1_country_code", "DB");
		nvram_set("wl_reg_5g", "5G_ALL");
	}
	else
	{
		for(i = 0; i < MAX_REGDOMAIN_LEN; i++)
			if(dst[i] == 0xff || dst[i] == 0)
				break;

		dst[i] = 0;
		nvram_set("wl_reg_5g", dst);
		if      (strcmp(dst, "5G_BAND1") == 0)
			nvram_set("wl1_country_code", "GB");
		else if (strcmp(dst, "5G_BAND123") == 0)
			nvram_set("wl1_country_code", "GB");
		else if (strcmp(dst, "5G_BAND14") == 0)
			nvram_set("wl1_country_code", "US");
		else if (strcmp(dst, "5G_BAND24") == 0)
			nvram_set("wl1_country_code", "TW");
		else if (strcmp(dst, "5G_BAND4") == 0)
			nvram_set("wl1_country_code", "CN");
		else
			nvram_set("wl1_country_code", "DB");
	}
#endif	/* ! RTCONFIG_NEW_REGULATION_DOMAIN */
#if defined(RTN56U) || defined(RTCONFIG_DSL)
		if (nvram_match("wl_country_code", "BR"))
		{
			nvram_set("wl_country_code", "UZ");
			nvram_set("wl0_country_code", "UZ");
			nvram_set("wl1_country_code", "UZ");
		}
#endif
		if (nvram_match("wl_country_code", "HK") && nvram_match("preferred_lang", ""))
			nvram_set("preferred_lang", "TW");

	/* reserved for Ralink. used as ASUS pin code. */
	dst = (char*)pin;
	bytes = 8;
	if (FRead(dst, OFFSET_PIN_CODE, bytes)<0)
	{
		_dprintf("READ ASUS pin code: Out of scope\n");
		nvram_set("wl_pin_code", "");
	}
	else
	{
		if ((unsigned char)pin[0]!=0xff)
			nvram_set("secret_code", pin);
		else
			nvram_set("secret_code", "12345670");
	}

	dst = buffer;
	bytes = 16;
	if (linuxRead(dst, 0x20, bytes)<0)	/* The "linux" MTD partition, offset 0x20. */
	{
		fprintf(stderr, "READ firmware header: Out of scope\n");
		nvram_set("productid", "unknown");
		nvram_set("firmver", "unknown");
	}
	else
	{
		strncpy(productid, buffer + 4, 12);
		productid[12] = 0;
		sprintf(fwver, "%d.%d.%d.%d", buffer[0], buffer[1], buffer[2], buffer[3]);
		nvram_set("productid", trim_r(productid));
		nvram_set("firmver", trim_r(fwver));
	}

	memset(buffer, 0, sizeof(buffer));
	FRead(buffer, OFFSET_BOOT_VER, 4);
//	sprintf(blver, "%c.%c.%c.%c", buffer[0], buffer[1], buffer[2], buffer[3]);
	sprintf(blver, "%s-0%c-0%c-0%c-0%c", trim_r(productid), buffer[0], buffer[1], buffer[2], buffer[3]);
	nvram_set("blver", trim_r(blver));

	_dprintf("bootloader version: %s\n", nvram_safe_get("blver"));
	_dprintf("firmware version: %s\n", nvram_safe_get("firmver"));

	dst = txbf_para;
	int count_0xff = 0;
	if (FRead(dst, OFFSET_TXBF_PARA, 33) < 0)
	{
		fprintf(stderr, "READ TXBF PARA address: Out of scope\n");
	}
	else
	{
		for (i = 0; i < 33; i++)
		{
			if (txbf_para[i] == 0xff)
				count_0xff++;
/*
			if ((i % 16) == 0) fprintf(stderr, "\n");
			fprintf(stderr, "%02x ", (unsigned char) txbf_para[i]);
*/
		}
/*
		fprintf(stderr, "\n");

		fprintf(stderr, "TxBF parameter 0xFF count: %d\n", count_0xff);
*/
	}

	if (count_0xff == 33)
		nvram_set("wl1_txbf_en", "0");
	else
		nvram_set("wl1_txbf_en", "1");

#if defined (RTCONFIG_WLMODULE_RT3352_INIC_MII)
#define EEPROM_INIC_SIZE (512)
#define EEPROM_INIT_ADDR 0x48000
#define EEPROM_INIT_FILE "/etc/Wireless/iNIC/iNIC_e2p.bin"
	{
		char eeprom[EEPROM_INIC_SIZE];
		if(FRead(eeprom, EEPROM_INIT_ADDR, sizeof(eeprom)) < 0)
		{
			fprintf(stderr, "FRead(eeprom, 0x%08x, 0x%x) failed\n", EEPROM_INIT_ADDR, sizeof(eeprom));
		}
		else
		{
			FILE *fp;
			char *filepath = EEPROM_INIT_FILE;

			system("mkdir -p /etc/Wireless/iNIC/");
			if((fp = fopen(filepath, "w")) == NULL)
			{
				fprintf(stderr, "fopen(%s) failed!!\n", filepath);
			}
			else
			{
				if(fwrite(eeprom, sizeof(eeprom), 1, fp) < 1)
				{
					perror("fwrite(eeprom)");
				}
				fclose(fp);
			}
		}
	}
#endif

#ifdef RA_SINGLE_SKU
#if defined(RTAC52U)
	{
		char *reg_spec;

		reg_spec = nvram_safe_get("reg_spec");
		create_SingleSKU("/etc/Wireless/RT2860", "", reg_spec);
		create_SingleSKU("/etc/Wireless/iNIC", "_5G", reg_spec);
	}
#endif	/* RTAC52U */
#endif	/* RA_SINGLE_SKU */

	{
#ifdef RTCONFIG_ODMPID
		char modelname[16];
		FRead(modelname, OFFSET_ODMPID, sizeof(modelname));
		modelname[sizeof(modelname)-1] = '\0';
		if(modelname[0] != 0 && (unsigned char)(modelname[0]) != 0xff && is_valid_hostname(modelname) && strcmp(modelname, "ASUS"))
		{
			nvram_set("odmpid", modelname);
		}
		else
#endif
			nvram_unset("odmpid");
	}

	nvram_set("firmver", rt_version);
	nvram_set("productid", rt_buildname);
}
コード例 #18
0
ファイル: qtn_monitor.c プロジェクト: themiron/asuswrt-merlin
int 
qtn_monitor_main(int argc, char *argv[])
{
	FILE *fp;
	sigset_t sigs_to_catch;
	int ret, retval = 0;
	time_t start_time = uptime();

	/* write pid */
	if ((fp = fopen("/var/run/qtn_monitor.pid", "w")) != NULL)
	{
		fprintf(fp, "%d", getpid());
		fclose(fp);
	}

	/* set the signal handler */
	sigemptyset(&sigs_to_catch);
	sigaddset(&sigs_to_catch, SIGTERM);
	sigprocmask(SIG_UNBLOCK, &sigs_to_catch, NULL);

	signal(SIGTERM, qtn_monitor_exit);

QTN_RESET:
	ret = rpc_qcsapi_init(1);
	if (ret < 0) {
		dbG("rpc_qcsapi_init error, return: %d\n", ret);
		retval = -1;
		goto ERROR;
	}
#if 0	/* replaced by STATELESS, send configuration from brcm to qtn */
	else if (nvram_get_int("qtn_restore_defaults"))
	{
		nvram_unset("qtn_restore_defaults");
		rpc_qcsapi_restore_default_config(0);
		dbG("Restaring Qcsapi init...\n");
		sleep(15);
		goto QTN_RESET;
	}
#endif

#if 0
	if(nvram_get_int("sw_mode") == SW_MODE_AP &&
		nvram_get_int("wlc_psta") == 1 &&
		nvram_get_int("wlc_band") == 1){
		dbG("[sw_mode] start QTN PSTA mode\n");
		start_psta_qtn();
	}
#endif

	ret = qcsapi_init();
	if (ret < 0)
	{
		dbG("Qcsapi qcsapi_init error, return: %d\n", ret);
		retval = -1;
		goto ERROR;
	}
	else
		dbG("Qcsapi qcsapi init takes %ld seconds\n", uptime() - start_time);

	dbG("defer lanport_ctrl(1)\n");
	lanport_ctrl(1);
	eval("ifconfig", "br0:1", "down");
#if defined(RTCONFIG_JFFS2ND_BACKUP)
	check_2nd_jffs();
#endif
	nvram_set("qtn_ready", "1");

	if(nvram_get_int("AllLED") == 0) setAllLedOff();

	// dbG("[QTN] update router_command.sh from brcm to qtn\n");
	// qcsapi_wifi_run_script("set_test_mode", "update_router_command");

#if 1	/* STATELESS */
	if(nvram_get_int("sw_mode") == SW_MODE_AP &&
		nvram_get_int("wlc_psta") == 1 &&
		nvram_get_int("wlc_band") == 1){
		dbG("[sw_mode] skip start_psta_qtn, QTN will run scripts automatically\n");
		// start_psta_qtn();
	}else{
		dbG("[***] rpc_parse_nvram_from_httpd\n");
		rpc_parse_nvram_from_httpd(1,-1);	/* wifi0 */
		rpc_parse_nvram_from_httpd(1,1);	/* wifi1 */
		rpc_parse_nvram_from_httpd(1,2);	/* wifi2 */
		rpc_parse_nvram_from_httpd(1,3);	/* wifi3 */
		dbG("[sw_mode] skip start_ap_qtn, QTN will run scripts automatically\n");
		// start_ap_qtn();
		qcsapi_mac_addr wl_mac_addr;
		ret = rpc_qcsapi_interface_get_mac_addr(WIFINAME, &wl_mac_addr);
		if (ret < 0)
			dbG("rpc_qcsapi_interface_get_mac_addr, return: %d\n", ret);
		else
		{
			nvram_set("1:macaddr", wl_ether_etoa((struct ether_addr *) &wl_mac_addr));
			nvram_set("wl1_hwaddr", wl_ether_etoa((struct ether_addr *) &wl_mac_addr));
		}

		rpc_update_wdslist();

		if(nvram_get_int("wps_enable") == 1){
			ret = rpc_qcsapi_wifi_disable_wps(WIFINAME, 0);
			if (ret < 0)
				dbG("rpc_qcsapi_wifi_disable_wps %s error, return: %d\n", WIFINAME, ret);

			ret = qcsapi_wps_set_ap_pin(WIFINAME, nvram_safe_get("wps_device_pin"));
			if (ret < 0)
				dbG("qcsapi_wps_set_ap_pin %s error, return: %d\n", WIFINAME, ret);

			ret = qcsapi_wps_registrar_set_pp_devname(WIFINAME, 0, (const char *) get_productid());
			if (ret < 0)
				dbG("qcsapi_wps_registrar_set_pp_devname %s error, return: %d\n", WIFINAME, ret);
		}else{
			ret = rpc_qcsapi_wifi_disable_wps(WIFINAME, 1);
			if (ret < 0)
				dbG("rpc_qcsapi_wifi_disable_wps %s error, return: %d\n", WIFINAME, ret);
		}

		rpc_set_radio(1, 0, nvram_get_int("wl1_radio"));

	}
#endif

	if(nvram_get_int("wl1_80211h") == 1){
		dbG("[80211h] set_80211h_on\n");
		qcsapi_wifi_run_script("router_command.sh", "80211h_on");
	}else{
		dbG("[80211h] set_80211h_off\n");
		qcsapi_wifi_run_script("router_command.sh", "80211h_off");
	}

	if(nvram_get_int("sw_mode") == SW_MODE_ROUTER ||
		(nvram_get_int("sw_mode") == SW_MODE_AP &&
			nvram_get_int("wlc_psta") == 0)){
		if(nvram_get_int("wl1_chanspec") == 0){
			if (nvram_match("1:ccode", "EU")){
				if(nvram_get_int("acs_dfs") != 1){
					dbG("[dfs] start nodfs scanning and selection\n");
					start_nodfs_scan_qtn();
				}
			}else{
				/* all country except EU */
				dbG("[dfs] start nodfs scanning and selection\n");
				start_nodfs_scan_qtn();
			}
		}
	}
	if(nvram_get_int("sw_mode") == SW_MODE_AP &&
		nvram_get_int("wlc_psta") == 1 &&
		nvram_get_int("wlc_band") == 0){
		ret = qcsapi_wifi_reload_in_mode(WIFINAME, qcsapi_station);
		if (ret < 0)
			dbG("qtn reload_in_mode STA fail\n");
	}
	if(nvram_get_int("QTNTELNETSRV") == 1 && nvram_get_int("sw_mode") == SW_MODE_ROUTER){
		dbG("[QTNT] enable telnet server\n");
		qcsapi_wifi_run_script("router_command.sh", "enable_telnet_srv 1");
	}

	dbG("[dbg] qtn_monitor startup\n");
ERROR:
	remove("/var/run/qtn_monitor.pid");

	if (nvram_get_int("led_disable") == 1) {
		setAllLedOff_qtn();
//		qcsapi_wifi_run_script("router_command.sh", "wifi_led_off");
//		qcsapi_led_set(1, 0);
	}

	return retval;
}
コード例 #19
0
ファイル: main.c プロジェクト: themiron/asuswrt-merlin
/* NVRAM utility */
int
main(int argc, char **argv)
{
	char *name, *value, buf[MAX_NVRAM_SPACE];
	char *tmpbuf;	//Andy Chiu, 2015/06/09
	int size;

	/* Skip program name */
	--argc;
	++argv;

	if (!*argv)
		usage();

	/* Process the arguments */
	for (; *argv; ++argv) {
		if (!strcmp(*argv, "get")) {
			if (*++argv) {
				if ((value = nvram_get(*argv)))
					puts(value);
			}
		} else if (!strcmp(*argv, "set")) {
			if (*++argv) {
				strncpy(value = buf, *argv, sizeof(buf));
				name = strsep(&value, "=");
				nvram_set(name, value);
			}
		} else if (!strcmp(*argv, "unset")) {
			if (*++argv)
				nvram_unset(*argv);
		} else if (!strcmp(*argv, "commit")) {
			nvram_commit();
		} else if (!strcmp(*argv, "save")) {
			if (*++argv)
			{
				nvram_getall(buf, NVRAM_SPACE);
				nvram_save_new(*argv, buf);
			}
		//Andy Chiu, 2015/06/09
		}else if(!strncmp(*argv, "fb_save", 7)) {
			if (*++argv) 
			{
				tmpbuf = malloc(MAX_NVRAM_SPACE);
				if(!tmpbuf)
				{
					fprintf(stderr, "Can NOT alloc memory!!!");
					return 0;
				}				
				nvram_getall(buf, MAX_NVRAM_SPACE);
				memcpy(tmpbuf, buf, MAX_NVRAM_SPACE);
				_secure_conf(tmpbuf);
#if 0
				FILE *fp = fopen("/tmp/var/fb_conf.test", "w");
				if(fp)
				{
					fwrite(tmpbuf, 1, MAX_NVRAM_SPACE, fp);
					fclose(fp);
				}
#endif
				nvram_save_new(*argv, tmpbuf);
				free(tmpbuf);
			}			
		} else if (!strcmp(*argv, "restore")) {
			if (*++argv) 
				nvram_restore_new(*argv, buf);
		} else if (!strcmp(*argv, "erase")) {
			system("nvram_erase");
		} else if (!strcmp(*argv, "show") ||
		           !strcmp(*argv, "dump")) {
			nvram_getall(buf, sizeof(buf));
			for (name = buf; *name; name += strlen(name) + 1)
				puts(name);
			size = sizeof(struct nvram_header) + (int) name - (int) buf;
			if (**argv != 'd')
				fprintf(stderr, "size: %d bytes (%d left)\n",
				        size, MAX_NVRAM_SPACE - size);
		} else
			usage();
	}

	return 0;
}
コード例 #20
0
ファイル: main.c プロジェクト: themiron/asuswrt-merlin
int nvram_restore_new(char *file, char *buf)
{
	FILE *fp;
	char header[8], *p, *v;
	unsigned long count, filelen, *filelenptr, i;
	unsigned char rand, *randptr;

	if ((fp = fopen(file, "r+")) == NULL) return -1;

	count = fread(header, 1, 8, fp);
	if (count>=8 && strncmp(header, PROFILE_HEADER, 4) == 0)
	{
		filelenptr = (unsigned long *)(header + 4);
#ifdef ASUS_DEBUG
		fprintf(stderr, "restoring original text cfg of length %x\n", *filelenptr);
#endif
		fread(buf, 1, *filelenptr, fp);
	}
	else if (count>=8 && strncmp(header, PROFILE_HEADER_NEW, 4) == 0)
	{
		filelenptr = (unsigned long *)(header + 4);
		filelen = *filelenptr & 0xffffff;
#ifdef ASUS_DEBUG
		fprintf(stderr, "restoring non-text cfg of length %x\n", filelen);
#endif
		randptr = (unsigned char *)(header + 7);
		rand = *randptr;
#ifdef ASUS_DEBUG
		fprintf(stderr, "non-text cfg random number %x\n", rand);
#endif
		count = fread(buf, 1, filelen, fp);
#ifdef ASUS_DEBUG
		fprintf(stderr, "non-text cfg count %x\n", count);
#endif
		for (i = 0; i < count; i++)
		{
			if ((unsigned char) buf[i] > ( 0xfd - 0x1)){
				/* e.g.: to skip the case: 0x61 0x62 0x63 0x00 0x00 0x61 0x62 0x63 */
				if(i > 0 && buf[i-1] != 0x0)
					buf[i] = 0x0;
			}
			else
				buf[i] = 0xff + rand - buf[i];
		}
#ifdef ASUS_DEBUG
		for (i = 0; i < count; i++)
		{
			if (i % 16 == 0) fprintf(stderr, "\n");
			fprintf(stderr, "%2x ", (unsigned char) buf[i]);
		}

		for (i = 0; i < count; i++)
		{
			if (i % 16 == 0) fprintf(stderr, "\n");
			fprintf(stderr, "%c", buf[i]);
		}
#endif
	}
	else
	{
		fclose(fp);
		return 0;
	}
	fclose(fp);

	p = buf;

	while (*p)
	{
#if 1
		/* e.g.: to skip the case: 00 2e 30 2e 32 38 00 ff 77 61 6e */
		if(*p == NULL || *p < 32 || *p > 127 ){
			p = p + 1;
			continue;
		}
#endif
		v = strchr(p, '=');

		if (v != NULL)
		{
			*v++ = '\0';

			if (issyspara(p))
				nvram_set(p, v);

			p = v + strlen(v) + 1;
		}
		else
		{
			nvram_unset(p);
			p = p + 1;
		}
	}
	return 0;
}
コード例 #21
0
void start_jffs2(void)
{
	if (!nvram_match("jffs2_on", "1")) {
		notice_set("jffs", "");
		return;
	}

	int format = 0;
	char s[256];
	int size;
	int part;
	const char *p;
	struct statfs sf;
	int model = 0;
	int i = 0;

        while(1) {
		if (wait_action_idle(10)) break;
		else i++;

		if(i>=10) {
			_dprintf("Mount jffs2 failed!");
			return;
		}
	}

	if (!mtd_getinfo(JFFS2_PARTITION, &part, &size)) return;

	model = get_model();
	_dprintf("start jffs2: %d, %d\n", part, size);
	if (nvram_match("jffs2_format", "1")) {
		nvram_set("jffs2_format", "0");
		nvram_commit_x();
		if (!mtd_erase(JFFS_NAME)) {
			error("formatting");
			return;
		}
		format = 1;
	}

	sprintf(s, "%d", size);
	p = nvram_get("jffs2_size");
	if ((p == NULL) || (strcmp(p, s) != 0)) {
		if (format) {
			nvram_set("jffs2_size", s);
			nvram_commit_x();
		}
		else if ((p != NULL) && (*p != 0)) {
			error("verifying known size of");
			return;
		}
	}

	if (statfs("/jffs", &sf) == 0) { 
		switch(model) {
			case MODEL_RTAC56S: 
			case MODEL_RTAC56U: 
			case MODEL_RTAC3200:
			case MODEL_DSLAC68U:
			case MODEL_RPAC68U: 
			case MODEL_RTAC68U: 
			case MODEL_RTAC87U:
			case MODEL_RTN18U: 
			case MODEL_RTN65U:
			case MODEL_RTN14U: // it should be better to use LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,6,36)
			{
				if (sf.f_type != 0x73717368 /* squashfs */) {
					// already mounted
					notice_set("jffs", format ? "Formatted" : "Loaded");
					return;
				}
				break;
			}
			default:
			{
	                        if (sf.f_type != 0x71736873 /* squashfs */) {
        	                        // already mounted
                	                notice_set("jffs", format ? "Formatted" : "Loaded");
                        	        return;
				}
				break;
			}
		}
	}

	if (nvram_get_int("jffs2_clean_fs")) {
		if (!mtd_unlock(JFFS2_PARTITION)) {
			error("unlocking");
			return;
		}
	}
	modprobe(JFFS_NAME);
	sprintf(s, MTD_BLKDEV(%d), part);

	if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
		if (!mtd_erase(JFFS_NAME)) {
                        error("formatting");
                        return;
                }

		format = 1;
		if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
			_dprintf("*** jffs2 2-nd mount error\n");
			//modprobe_r(JFFS_NAME);
			error("mounting");
			return;
		}
	}

#ifdef TEST_INTEGRITY
	int test;

	if (format) {
		if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
			stop_jffs2(0);
			error("setting integrity test for");
			return;
		}
	}
	if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
		stop_jffs2(0);
		error("testing integrity of");
		return;
	}
#endif

	if (nvram_get_int("jffs2_clean_fs")) {
		_dprintf("Clean /jffs/*\n");
		system("rm -fr /jffs/*");
		nvram_unset("jffs2_clean_fs");
		nvram_commit_x();
	}

	notice_set("jffs", format ? "Formatted" : "Loaded");

	if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
		chdir("/jffs");
		system(p);
		chdir("/");
	}
	run_userfile("/jffs", ".asusrouter", "/jffs", 3);

	if (!check_if_dir_exist("/jffs/scripts/")) mkdir("/jffs/scripts/", 0755);
	if (!check_if_dir_exist("/jffs/configs/")) mkdir("/jffs/configs/", 0755);
}
コード例 #22
0
ファイル: shutils.c プロジェクト: NieHao/Tomato-RAF
void cprintf(const char *format, ...)
{
	FILE *f;
	va_list args;

#ifdef DEBUG_NOISY
	{
#else
	if (nvram_match("debug_cprintf", "1")) {
#endif
		if ((f = fopen("/dev/console", "w")) != NULL) {
			va_start(args, format);
			vfprintf(f, format, args);
			va_end(args);
			fclose(f);
		}
	}
#if 1	
	if (nvram_match("debug_cprintf_file", "1")) {
//		char s[32];
//		sprintf(s, "/tmp/cprintf.%d", getpid());
//		if ((f = fopen(s, "a")) != NULL) {
		if ((f = fopen("/tmp/cprintf", "a")) != NULL) {
			va_start(args, format);
			vfprintf(f, format, args);
			va_end(args);
			fclose(f);
		}
	}
#endif
#if 0
	if (nvram_match("debug_cprintf_log", "1")) {
		char s[512];
		
		va_start(args, format);
		vsnprintf(s, sizeof(s), format, args);
		s[sizeof(s) - 1] = 0;
		va_end(args);
		
		if ((s[0] != '\n') || (s[1] != 0)) syslog(LOG_DEBUG, "%s", s);
	}
#endif
}


#ifndef WL_BSS_INFO_VERSION
#error WL_BSS_INFO_VERSION
#endif

#if WL_BSS_INFO_VERSION >= 108
// xref (all): nas, wlconf

#ifndef MAX_NVPARSE
#define MAX_NVPARSE 255
#endif

#if 0
/*
 * Get the ip configuration index if it exists given the
 * eth name.
 *
 * @param	wl_ifname 	pointer to eth interface name
 * @return	index or -1 if not found
 */
int
get_ipconfig_index(char *eth_ifname)
{
	char varname[64];
	char varval[64];
	char *ptr;
	char wl_ifname[NVRAM_MAX_PARAM_LEN];
	int index;

	/* Bail if we get a NULL or empty string */

	if (!eth_ifname) return -1;
	if (!*eth_ifname) return -1;

	/* Look up wl name from the eth name */
	if (osifname_to_nvifname(eth_ifname, wl_ifname, sizeof(wl_ifname)) != 0)
		return -1;

	snprintf(varname, sizeof(varname), "%s_ipconfig_index", wl_ifname);

	ptr = nvram_get(varname);

	if (ptr) {
	/* Check ipconfig_index pointer to see if it is still pointing
	 * the correct lan config block
	 */
		if (*ptr) {
			int index;
			char *ifname;
			char buf[64];
			index = atoi(ptr);

			snprintf(buf, sizeof(buf), "lan%d_ifname", index);

			ifname = nvram_get(buf);

			if (ifname) {
				if  (!(strcmp(ifname, wl_ifname)))
					return index;
			}
			nvram_unset(varname);
		}
	}

	/* The index pointer may not have been configured if the
	 * user enters the variables manually. Do a brute force search
	 *  of the lanXX_ifname variables
	 */
	for (index = 0; index < MAX_NVPARSE; index++) {
		snprintf(varname, sizeof(varname), "lan%d_ifname", index);
		if (nvram_match(varname, wl_ifname)) {
			/* if a match is found set up a corresponding index pointer for wlXX */
			snprintf(varname, sizeof(varname), "%s_ipconfig_index", wl_ifname);
			snprintf(varval, sizeof(varval), "%d", index);
			nvram_set(varname, varval);
			nvram_commit();
			return index;
		};
	}
	return -1;
}
コード例 #23
0
ファイル: restrict.c プロジェクト: AllardJ/Tomato
void ipt_restrictions(void)
{
	char buf[8192];
	char *p, *q;
	int n;
	char *comps, *matches, *http;
	int nrule;
	int blockall;
	char reschain[32];
	char devchain[32];
	char nextchain[32];
	int need_web;
	char *pproto;
	char *dir;
	char *pport;
	int proto;
	char *ipp2p;
	char *layer7;
	char *addr_type, *addr;
	char app[256];
	char ports[256];
	char iptaddr[192];
	int http_file;
	int ex;
	int first;
	int v4v6_ok;

	need_web = 0;
	first = 1;
	nvram_unset("rrules_timewarn");
	nvram_set("rrules_radio", "-1");
	unsched_restrictions();

	for (nrule = 0; nrule < MAX_NRULES; ++nrule) {
		sprintf(buf, "rrule%d", nrule);
		if ((p = nvram_get(buf)) == NULL) continue;
		if (strlen(p) >= sizeof(buf)) continue;
		strcpy(buf, p);

		if ((vstrsep(buf, "|",
			&q,		// 0/1
			&p, &p, &p,	// time (ignored)
			&comps,		//
			&matches,	//
			&http,		//
			&p		// http file match
			) != 8) || (*q != '1')) continue;
		http_file = atoi(p);

		if (comps[0] == '~') {
			// a wireless disable rule, skip
			continue;
		}

		if (first) {
			first = 0;

			ip46t_write(":restrict - [0:0]\n");
#ifdef TCONFIG_IPV6
			if (*wan6face)
				ip6t_write("-A FORWARD -o %s -j restrict\n",
					  wan6face);
#endif
			for (n = 0; n < wanfaces.count; ++n) {
				if (*(wanfaces.iface[n].name)) {
					ipt_write("-A FORWARD -o %s -j restrict\n",
						  wanfaces.iface[n].name);
				}
			}
		// Only mess with DNS requests that are coming in on INPUT
		ip46t_write("-I INPUT 1 ! -i lo -p udp --dport 53 -j restrict\n");
	}

		sprintf(reschain, "rres%02d", nrule);
		ip46t_write(":%s - [0:0]\n", reschain);

		blockall = 1;

		while ((q = strsep(&matches, ">")) != NULL) {
			n = vstrsep(q, "<", &pproto, &dir, &pport, &ipp2p, &layer7, &addr_type, &addr);
			if (n == 5) {
				// fixup for backward compatibility
				addr_type = "0";
			}
			else if (n != 7) continue;

			if ((*dir != 'a') && (*dir != 's') && (*dir != 'd') && (*dir != 'x')) continue;

			// p2p, layer7
			if (!ipt_ipp2p(ipp2p, app)) {
				if (ipt_layer7(layer7, app) == -1) continue;
			}
#ifdef TCONFIG_IPV6
			v4v6_ok = ((*app) ? 0 : IPT_V6) | IPT_V4;
#else
			v4v6_ok = IPT_V4;
#endif

			// dest ip/domain address
			if ((*addr_type == '1') || (*addr_type == '2')) {
				v4v6_ok = ipt_addr(iptaddr, sizeof(iptaddr), addr, (*addr_type == '1') ? "dst" : "src", v4v6_ok, (v4v6_ok == IPT_V4), "restrictions", NULL);
				if (!v4v6_ok)
					continue;
			}
			else {
				iptaddr[0] = 0;
			}

			blockall = 0;

			// proto & ports
			proto = atoi(pproto);
			if (proto <= -2) {
				// shortcut if any proto+any port
				ip46t_flagged_write(v4v6_ok, "-A %s %s %s -j %s\n", reschain, iptaddr, app, chain_out_drop);
				continue;
			}
			else if ((proto == 6) || (proto == 17) || (proto == -1)) {
				if ((*dir != 'a') && (*pport)) {
					if ((*dir == 'x') || (strchr(pport, ','))) {
						// use multiport for multiple ports or src-or-dst type matches
						snprintf(ports, sizeof(ports), "-m multiport --%sports %s", (*dir == 'x') ? "" : dir, pport);
					}
					else {
						// else, use built-in
						snprintf(ports, sizeof(ports), "--%sport %s", dir, pport);
					}
				}
				else {
					ports[0] = 0;
				}
				if (proto != 17)
					ip46t_flagged_write(v4v6_ok, "-A %s -p tcp %s %s %s -j %s\n", reschain, ports, iptaddr, app, chain_out_drop);
				if (proto != 6)
					ip46t_flagged_write(v4v6_ok, "-A %s -p udp %s %s %s -j %s\n", reschain, ports, iptaddr, app, chain_out_drop);
			}
			else {
				ip46t_flagged_write(v4v6_ok, "-A %s -p %d %s %s -j %s\n", reschain, proto, iptaddr, app, chain_out_drop);
			}
		}

		//

		p = http;
		while (*p) {
			if ((*p == '\t') || (*p == '\r') || (*p == '\n') || (*p == '"')) *p = ' ';
			++p;
		}
		while ((n = strlen(http)) > 0) {
			if (n >= 511) {
				p = http + 510;
				while ((p > http) && (*p != ' ')) --p;
				if (p <= http) {
					// too long
					break;
				}
				*p = 0;
			}
			else p = NULL;
			ip46t_write("-A %s -p tcp -m web --hore \"%s\" -j %s\n", reschain, http, chain_out_reject);
			need_web = 1;
			blockall = 0;
			if (p == NULL) break;
			http = p + 1;
		}


		//
		app[0] = 0;
		if (http_file & 1) strcat(app, ".ocx$ .cab$ ");
		if (http_file & 2) strcpy(app, ".swf$ ");
		if (http_file & 4) strcat(app, ".class$ .jar$");
		if (app[0]) {
			ip46t_write("-A %s -p tcp -m multiport --dports %s -m web --path \"%s\" -j %s\n",
				reschain, nvram_safe_get("rrulewp"), app, chain_out_reject);
			need_web = 1;
			blockall = 0;
		}

		if (*comps) {
			if (blockall) {
				ip46t_write("-X %s\n", reschain);	// chain not needed
				sprintf(nextchain, "-j %s", chain_out_drop);
			}
			else {
				sprintf(nextchain, "-g %s", reschain);
			}

			ex = 0;
			sprintf(devchain, "rdev%02d", nrule);
			ip46t_write(":%s - [0:0]\n", devchain);
			while ((q = strsep(&comps, ">")) != NULL) {
				if (*q == 0) continue;
				if (*q == '!') {
					ex = 1;
					continue;
				}
#ifdef TCONFIG_IPV6
				v4v6_ok = IPT_V6 | IPT_V4;
#else
				v4v6_ok = IPT_V4;
#endif
				if (sscanf(q, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
					   iptaddr, iptaddr, iptaddr, iptaddr, iptaddr, iptaddr) == 6) {
					snprintf(iptaddr, sizeof(iptaddr), "-m mac --mac-source %s", q);
				}
				else {
					v4v6_ok = ipt_addr(iptaddr, sizeof(iptaddr), q, "src", v4v6_ok, (v4v6_ok == IPT_V4), "restrictions", "filtering");
					if (!v4v6_ok)
						continue;
				}
				ip46t_flagged_write(v4v6_ok,
					"-A %s %s %s\n", devchain, iptaddr, ex ? "-j RETURN" : nextchain);
			}

			if (ex) {
				ip46t_write("-A %s %s\n", devchain, nextchain);
			}
		}
		else if (blockall) {
			ip46t_write("-A %s -j %s\n", reschain, chain_out_drop);
		}
	}

	nvram_set("rrules_activated", "0");

	if (need_web)
		modprobe("ipt_web");
}
コード例 #24
0
ファイル: networkmap.c プロジェクト: Shonk/asuswrt-merlin
int main(int argc, char *argv[])
{
	int arp_sockfd, arp_getlen, i;
	int send_count=0, file_num=0;
	struct sockaddr_in router_addr, device_addr;
	char router_ipaddr[17], router_mac[17], buffer[ARP_BUFFER_SIZE];
	unsigned char scan_ipaddr[4]; // scan ip
	FILE *fp_ip;
	fd_set rfds;
        ARP_HEADER * arp_ptr;
        struct timeval tv1, tv2, arp_timeout;
	int shm_client_detail_info_id;
	int ip_dup, mac_dup, real_num;
	int lock;

        FILE *fp = fopen("/var/run/networkmap.pid", "w");
        if(fp != NULL){
                fprintf(fp, "%d", getpid());
                fclose(fp);
        }
	#ifdef DEBUG
		eval("rm", "/var/client*");
	#endif

	//Initial client tables
	lock = file_lock("networkmap");
	shm_client_detail_info_id = shmget((key_t)1001, sizeof(CLIENT_DETAIL_INFO_TABLE), 0666|IPC_CREAT);
        if (shm_client_detail_info_id == -1){
    	    fprintf(stderr,"shmget failed\n");
	    file_unlock(lock);
            exit(1);
    	}

	CLIENT_DETAIL_INFO_TABLE *p_client_detail_info_tab = (P_CLIENT_DETAIL_INFO_TABLE)shmat(shm_client_detail_info_id,(void *) 0,0);
	//Reset shared memory
	memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE));
	p_client_detail_info_tab->ip_mac_num = 0;
	p_client_detail_info_tab->detail_info_num = 0;
	file_unlock(lock);

	#ifdef NMP_DB
		nmp_client_list = strdup(nvram_safe_get("nmp_client_list"));
		NMP_DEBUG_M("NMP Client:\n%s\n", nmp_client_list);
		signal(SIGUSR2, reset_db);
	#endif

	//Get Router's IP/Mac
	strcpy(router_ipaddr, nvram_safe_get("lan_ipaddr"));
#ifdef RTCONFIG_RGMII_BRCM5301X
	strcpy(router_mac, nvram_safe_get("et1macaddr"));
#else
	strcpy(router_mac, nvram_safe_get("et0macaddr"));
#endif
        inet_aton(router_ipaddr, &router_addr.sin_addr);
        memcpy(my_ipaddr,  &router_addr.sin_addr, 4);

	//Prepare scan 
	networkmap_fullscan = 1;
	nvram_set("networkmap_fullscan", "1");

	if (argc > 1) {
		if (strcmp(argv[1], "--bootwait") == 0) {
			sleep(30);
		}
	}
	if (strlen(router_mac)!=0) ether_atoe(router_mac, my_hwaddr);

	signal(SIGUSR1, refresh_sig); //catch UI refresh signal
	
        // create UDP socket and bind to "br0" to get ARP packet//
	arp_sockfd = create_socket(INTERFACE);

        if(arp_sockfd < 0)
                perror("create socket ERR:");
	else {
	        arp_timeout.tv_sec = 0;
        	arp_timeout.tv_usec = 50000;
		setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout
		dst_sockll = src_sockll; //Copy sockaddr info to dst
		memset(dst_sockll.sll_addr, -1, sizeof(dst_sockll.sll_addr)); // set dmac= FF:FF:FF:FF:FF:FF
	}

        while(1)//main while loop
        {
	    while(1) { //full scan and reflush recv buffer
		fullscan:
                if(networkmap_fullscan == 1) { //Scan all IP address in the subnetwork
		    if(scan_count == 0) { 
			asusdiscovery();	//find asus device
			// (re)-start from the begining
			memset(scan_ipaddr, 0x00, 4);
			memcpy(scan_ipaddr, &router_addr.sin_addr, 3);
	                arp_timeout.tv_sec = 0;
        	        arp_timeout.tv_usec = 50000;
                	setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout
			NMP_DEBUG("Starting full scan!\n");

                        if(nvram_match("refresh_networkmap", "1")) {//reset client tables
				lock = file_lock("networkmap");
        			memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE));
        			//p_client_detail_info_tab->detail_info_num = 0;
				//p_client_detail_info_tab->ip_mac_num = 0;
				file_unlock(lock);
				nvram_unset("refresh_networkmap");
			}
			else {
				int x = 0;
				for(; x<255; x++)
					p_client_detail_info_tab->exist[x]=0;
			}
		    }
		    scan_count++;
		    scan_ipaddr[3]++;

		    if( scan_count<255 && memcmp(scan_ipaddr, my_ipaddr, 4) ) {
                        sent_arppacket(arp_sockfd, scan_ipaddr);
		    }
		    else if(scan_count==255) { //Scan completed
                	arp_timeout.tv_sec = 2;
                	arp_timeout.tv_usec = 0; //Reset timeout at monitor state for decase cpu loading
                	setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout
			networkmap_fullscan = 0;
			//scan_count = 0;
			nvram_set("networkmap_fullscan", "0");
			NMP_DEBUG("Finish full scan!\n");
		    }
                }// End of full scan

		memset(buffer, 0, ARP_BUFFER_SIZE);
		arp_getlen=recvfrom(arp_sockfd, buffer, ARP_BUFFER_SIZE, 0, NULL, NULL);

	   	if(arp_getlen == -1) {
			if( scan_count<255)
				goto fullscan;
			else
				break;
		}
		else {
		    arp_ptr = (ARP_HEADER*)(buffer);
                    NMP_DEBUG("*Receive an ARP Packet from: %d.%d.%d.%d to %d.%d.%d.%d:%02X:%02X:%02X:%02X - len:%d\n",
				(int *)arp_ptr->source_ipaddr[0],(int *)arp_ptr->source_ipaddr[1],
				(int *)arp_ptr->source_ipaddr[2],(int *)arp_ptr->source_ipaddr[3],
				(int *)arp_ptr->dest_ipaddr[0],(int *)arp_ptr->dest_ipaddr[1],
				(int *)arp_ptr->dest_ipaddr[2],(int *)arp_ptr->dest_ipaddr[3],
                                arp_ptr->dest_hwaddr[0],arp_ptr->dest_hwaddr[1],
                                arp_ptr->dest_hwaddr[2],arp_ptr->dest_hwaddr[3],
				arp_getlen);

		    //Check ARP packet if source ip and router ip at the same network
                    if( !memcmp(my_ipaddr, arp_ptr->source_ipaddr, 3) ) {

			swapbytes16(arp_ptr->message_type);

			if( //ARP packet to router
			   (arp_ptr->message_type == 0x02 &&   		       	// ARP response
                       	    memcmp(arp_ptr->dest_ipaddr, my_ipaddr, 4) == 0 && 	// dest IP
                       	    memcmp(arp_ptr->dest_hwaddr, my_hwaddr, 6) == 0) 	// dest MAC
			    ||
			   (arp_ptr->message_type == 0x01 &&                    // ARP request
                            memcmp(arp_ptr->dest_ipaddr, my_ipaddr, 4) == 0)    // dest IP
			){
			    //NMP_DEBUG("   It's an ARP Response to Router!\n");
                            NMP_DEBUG("*RCV %d.%d.%d.%d-%02X:%02X:%02X:%02X:%02X:%02X\n",
                            (int *)arp_ptr->source_ipaddr[0],(int *)arp_ptr->source_ipaddr[1],
                            (int *)arp_ptr->source_ipaddr[2],(int *)arp_ptr->source_ipaddr[3],
                            arp_ptr->source_hwaddr[0],arp_ptr->source_hwaddr[1],
                            arp_ptr->source_hwaddr[2],arp_ptr->source_hwaddr[3],
                            arp_ptr->source_hwaddr[4],arp_ptr->source_hwaddr[5]);

                            for(i=0; i<p_client_detail_info_tab->ip_mac_num; i++) {
				ip_dup = memcmp(p_client_detail_info_tab->ip_addr[i], arp_ptr->source_ipaddr, 4);
                                mac_dup = memcmp(p_client_detail_info_tab->mac_addr[i], arp_ptr->source_hwaddr, 6);

				if((ip_dup == 0) && (mac_dup == 0)) {
					lock = file_lock("networkmap");
					p_client_detail_info_tab->exist[i] = 1;
					file_unlock(lock);
					break;
				}
				else if((ip_dup != 0) && (mac_dup != 0)) {
					continue;
				}

				else if( (scan_count>=255) && ((ip_dup != 0) && (mac_dup == 0)) ) { 
					NMP_DEBUG("IP changed, update immediately\n");
					NMP_DEBUG("*CMP %d.%d.%d.%d-%02X:%02X:%02X:%02X:%02X:%02X\n",
                                    	p_client_detail_info_tab->ip_addr[i][0],p_client_detail_info_tab->ip_addr[i][1],
                                    	p_client_detail_info_tab->ip_addr[i][2],p_client_detail_info_tab->ip_addr[i][3],
                                    	p_client_detail_info_tab->mac_addr[i][0],p_client_detail_info_tab->mac_addr[i][1],
                                    	p_client_detail_info_tab->mac_addr[i][2],p_client_detail_info_tab->mac_addr[i][3],
                                    	p_client_detail_info_tab->mac_addr[i][4],p_client_detail_info_tab->mac_addr[i][5]);

					lock = file_lock("networkmap");
	                                memcpy(p_client_detail_info_tab->ip_addr[i],
        	                                arp_ptr->source_ipaddr, 4);
                	                memcpy(p_client_detail_info_tab->mac_addr[i],
                        	                arp_ptr->source_hwaddr, 6);
					p_client_detail_info_tab->exist[i] = 1;
					file_unlock(lock);
					/*
					real_num = p_client_detail_info_tab->detail_info_num;
					p_client_detail_info_tab->detail_info_num = i;
                                        #ifdef NMP_DB
                                                check_nmp_db(p_client_detail_info_tab, i);
                                        #endif
					FindAllApp(my_ipaddr, p_client_detail_info_tab);
					FindHostname(p_client_detail_info_tab);
					p_client_detail_info_tab->detail_info_num = real_num;
					*/
					break;
				}

                            }
			    //NMP_DEBUG("Out check!\n");
			    //i=0, table is empty.
			    //i=num, no the same ip at table.
			    if(i==p_client_detail_info_tab->ip_mac_num){
				lock = file_lock("networkmap");
				memcpy(p_client_detail_info_tab->ip_addr[p_client_detail_info_tab->ip_mac_num], 
					arp_ptr->source_ipaddr, 4);
                                memcpy(p_client_detail_info_tab->mac_addr[p_client_detail_info_tab->ip_mac_num], 
					arp_ptr->source_hwaddr, 6);
				p_client_detail_info_tab->exist[p_client_detail_info_tab->ip_mac_num] = 1;
                                #ifdef NMP_DB
                                        check_nmp_db(p_client_detail_info_tab, i);
                                #endif
                                p_client_detail_info_tab->ip_mac_num++;
				file_unlock(lock);

			    #ifdef DEBUG  //Write client info to file
                		fp_ip=fopen("/var/client_ip_mac.txt", "a");
                		if (fp_ip==NULL) {
                    		NMP_DEBUG("File Open Error!\n");
                		}
                		else {
                        	NMP_DEBUG_M("Fill: %d-> %d.%d\n", i,p_client_detail_info_tab->ip_addr[i][2],p_client_detail_info_tab->ip_addr[i][3]);

                        	fprintf(fp_ip, "%d.%d.%d.%d,%02X:%02X:%02X:%02X:%02X:%02X\n",
                      	 	    p_client_detail_info_tab->ip_addr[i][0],p_client_detail_info_tab->ip_addr[i][1],
                       	 	    p_client_detail_info_tab->ip_addr[i][2],p_client_detail_info_tab->ip_addr[i][3],
                        	    p_client_detail_info_tab->mac_addr[i][0],p_client_detail_info_tab->mac_addr[i][1],
                            	    p_client_detail_info_tab->mac_addr[i][2],p_client_detail_info_tab->mac_addr[i][3],
                       		    p_client_detail_info_tab->mac_addr[i][4],p_client_detail_info_tab->mac_addr[i][5]);
                    		}
                    		fclose(fp_ip);
			    #endif
                	    }
			}
			else { //Nomo ARP Packet or ARP response to other IP
        	                //Compare IP and IP buffer if not exist
                        	for(i=0; i<p_client_detail_info_tab->ip_mac_num; i++) {
                                        if( !memcmp(p_client_detail_info_tab->ip_addr[i], arp_ptr->source_ipaddr, 4) &&
					    !memcmp(p_client_detail_info_tab->mac_addr[i], arp_ptr->source_hwaddr, 6)) {
                                              	NMP_DEBUG_M("Find the same IP/MAC at the table!\n");
                	                        break;
                        	        }
                        	}
                        	if( i==p_client_detail_info_tab->ip_mac_num ) //Find a new IP or table is empty! Send an ARP request.
				{
					NMP_DEBUG("New device or IP/MAC changed!!\n");
					if(memcmp(my_ipaddr, arp_ptr->source_ipaddr, 4))
                                		sent_arppacket(arp_sockfd, arp_ptr->source_ipaddr);
					else
						NMP_DEBUG("New IP is the same as Router IP! Ignore it!\n");
				}
			}//End of Nomo ARP Packet
		    }//Source IP in the same subnetwork
		}//End of arp_getlen != -1
	    } // End of while for flush buffer

/*
	int y = 0;
	while(p_client_detail_info_tab->type[y]!=0){
            NMP_DEBUG("%d: %d.%d.%d.%d,%02X:%02X:%02X:%02X:%02X:%02X,%s,%d,%d,%d,%d,%d\n", y ,
                                p_client_detail_info_tab->ip_addr[y][0],p_client_detail_info_tab->ip_addr[y][1],
                                p_client_detail_info_tab->ip_addr[y][2],p_client_detail_info_tab->ip_addr[y][3],
                                p_client_detail_info_tab->mac_addr[y][0],p_client_detail_info_tab->mac_addr[y][1],
                                p_client_detail_info_tab->mac_addr[y][2],p_client_detail_info_tab->mac_addr[y][3],
                                p_client_detail_info_tab->mac_addr[y][4],p_client_detail_info_tab->mac_addr[y][5],
                                p_client_detail_info_tab->device_name[y],
                                p_client_detail_info_tab->type[y],
                                p_client_detail_info_tab->http[y],
                                p_client_detail_info_tab->printer[y],
                                p_client_detail_info_tab->itune[y],
				p_client_detail_info_tab->exist[y]);
		y++;
        }
*/
	    //Find All Application of clients
	    //NMP_DEBUG("\ndetail ? ip : %d ? %d\n\n", p_client_detail_info_tab->detail_info_num, p_client_detail_info_tab->ip_mac_num);
	    if(p_client_detail_info_tab->detail_info_num < p_client_detail_info_tab->ip_mac_num) {
		nvram_set("networkmap_status", "1");
		FindAllApp(my_ipaddr, p_client_detail_info_tab);
		FindHostname(p_client_detail_info_tab);

		#ifdef DEBUG //Fill client detail info table
                fp_ip=fopen("/var/client_detail_info.txt", "a");
                if (fp_ip==NULL) {
                        NMP_DEBUG("File Open Error!\n");
                }
                else {
                        fprintf(fp_ip, "%s,%d,%d,%d,%d\n",
                                p_client_detail_info_tab->device_name[p_client_detail_info_tab->detail_info_num], 
				p_client_detail_info_tab->type[p_client_detail_info_tab->detail_info_num], 
				p_client_detail_info_tab->http[p_client_detail_info_tab->detail_info_num],
                                p_client_detail_info_tab->printer[p_client_detail_info_tab->detail_info_num], 
				p_client_detail_info_tab->itune[p_client_detail_info_tab->detail_info_num]);
                        fclose(fp_ip);
                }
		#endif
		#ifdef NMP_DB
			write_to_nvram(p_client_detail_info_tab);
		#endif
		p_client_detail_info_tab->detail_info_num++;
	    }
	    #ifdef NMP_DB
	    else {
		NMP_DEBUG_M("commit_no, cli_no, updated: %d, %d, %d\n", 
		commit_no, p_client_detail_info_tab->detail_info_num, client_updated);
		if( (commit_no != p_client_detail_info_tab->detail_info_num) || client_updated ) {
			NMP_DEBUG("Commit nmp client list\n");
			nvram_commit();
		    	commit_no = p_client_detail_info_tab->detail_info_num;
			client_updated = 0;
		}
	    }
	    #endif

	    if(p_client_detail_info_tab->detail_info_num == p_client_detail_info_tab->ip_mac_num)
		nvram_set("networkmap_status", "0");    // Done scanning and resolving
	} //End of main while loop
	close(arp_sockfd);
	return 0;
}
コード例 #25
0
ファイル: rc.c プロジェクト: clockzhong/RouterN56U
static void
nvram_convert_old_params(void)
{
	char *test_value;

	/* convert old params */
	test_value = nvram_get("front_leds");
	if (test_value) {
		int front_leds = atoi(test_value);
		if (front_leds > 2)
			nvram_set_int("front_led_all", 0);
		if (front_leds == 4 || front_leds == 2)
			nvram_set_int("front_led_pwr", 0);
		nvram_unset("front_leds");
	}

	test_value = nvram_get("pppoe_dhcp_route");
	if (test_value) {
		if (strlen(test_value) > 0 && strlen(nvram_safe_get("wan_pppoe_man")) == 0)
			nvram_set("wan_pppoe_man", test_value);
		nvram_unset("pppoe_dhcp_route");
	}

	test_value = nvram_get("wan_heartbeat_x");
	if (test_value) {
		if (strlen(test_value) > 0 && strlen(nvram_safe_get("wan_ppp_peer")) == 0)
			nvram_set("wan_ppp_peer", test_value);
		nvram_unset("wan_heartbeat_x");
	}
	nvram_unset("wan0_heartbeat_x");

	test_value = nvram_get("wan_3g_pin");
	if (test_value) {
		if (strlen(test_value) > 0 && strlen(nvram_safe_get("modem_pin")) == 0)
			nvram_set("modem_pin", test_value);
		nvram_unset("wan_3g_pin");
	}
	nvram_unset("wan0_3g_pin");

	/* remove old unused params */
	nvram_unset("lan_route");
	nvram_unset("wan0_route");
	nvram_unset("wan_route");
	nvram_unset("wan_dns_t");
	nvram_unset("wan_proto_t");
	nvram_unset("wan_ipaddr_t");
	nvram_unset("wan_netmask_t");
	nvram_unset("wan_gateway_t");
	nvram_unset("wan_ifname_t");
	nvram_unset("wan_status_t");
	nvram_unset("wan_subnet_t");
	nvram_unset("lan_subnet_t");
	nvram_unset("link_lan");
	nvram_unset("rt_mcastrate");
	nvram_unset("wl_mcastrate");
}
コード例 #26
0
ファイル: qca8337.c プロジェクト: rjknight123/asuswrt-merlin
void reset_qca_switch(void)
{
	eval("swconfig", "dev", MII_IFNAME, "set", "reset"); // clear
	nvram_unset("vlan_idx");
}
コード例 #27
0
ファイル: nvram.c プロジェクト: jing-git/rt-n56u
/* NVRAM utility */
int
main(int argc, char **argv)
{
	char *name, *value;
	int ret = 0;

	/* Skip program name */
	--argc;
	++argv;

	if (!*argv)
		usage();

	/* Process the remaining arguments. */
	for (; *argv; argv++) {
		if (!strcmp(*argv, "get")) {
			if (*++argv) {
				if ((value = nvram_get(*argv)))
					puts(value);
			}
		}
		else if (!strcmp(*argv, "settmp")) {
			if (*++argv) {
				char buf[1024];
				strncpy(value = buf, *argv, sizeof(buf)-1);
				name = strsep(&value, "=");
				nvram_set_temp(name, value);
			}
		}
		else if (!strcmp(*argv, "set")) {
			if (*++argv) {
				char buf[1024];
				strncpy(value = buf, *argv, sizeof(buf)-1);
				name = strsep(&value, "=");
				nvram_set(name, value);
			}
		}
		else if (!strcmp(*argv, "unset")) {
			if (*++argv)
				nvram_unset(*argv);
		}
		else if (!strcmp(*argv, "clear")) {
			nvram_clear();
		}
		else if (!strcmp(*argv, "commit")) {
			nvram_commit();
			break;
		}
		else if (!strcmp(*argv, "save")) {
			if (*++argv)
				ret |= nvram_save(*argv);
			break;
		}
		else if (!strcmp(*argv, "restore")) {
			if (*++argv)
				ret |= nvram_restore(*argv);
			break;
		}
		else if (!strcmp(*argv, "show")) {
			ret |= nvram_show(0);
			break;
		}
		else if (!strcmp(*argv, "showall")) {
			ret |= nvram_show(1);
			break;
		}
		if (!*argv)
			break;
	}

	return ret;
}
コード例 #28
0
void start_chilli(void)
{
	int ret = 0;
	char ssid[128];

if ((nvram_match("usb_enable", "1")
	&& nvram_match("usb_storage", "1")
	&& nvram_match("usb_automnt", "1")
	&& nvram_match("usb_mntpoint", "jffs"))
	|| (nvram_match("enable_jffs2", "1")
	&& nvram_match("jffs_mounted", "1")
	&& nvram_match("sys_enable_jffs2", "1")))
		jffs = 1;

	stop_chilli();		//ensure that its stopped

	if (!strlen(nvram_safe_get("chilli_interface")))
		nvram_set("chilli_interface", get_wdev());
	if (!strlen(nvram_safe_get("hotss_interface")))
		nvram_set("hotss_interface", get_wdev());
	main_config();

#ifdef HAVE_HOTSPOT

	if (nvram_match("chilli_enable", "1")
	    && nvram_match("chilli_def_enable", "0")
	    && !nvram_match("hotss_enable", "1")) {
		nvram_unset("chilli_def_enable");
		nvram_set("chilli_enable", "0");
		return;
	}

	if (!nvram_match("chilli_enable", "1")
	    && !nvram_match("hotss_enable", "1")) {
		nvram_unset("chilli_def_enable");
		return;
	}

	if (nvram_match("hotss_enable", "1")) {
		stop_cron();
		if (!nvram_match("chilli_enable", "1")) {
			nvram_set("chilli_enable", "1");	// to get care of firewall, network, etc.
			nvram_set("chilli_def_enable", "0");
		}
		if (!nvram_match("hotss_preconfig", "1")) {
			nvram_set("hotss_preconfig", "1");
			sprintf(ssid, "HotSpotSystem.com-%s_%s", nvram_get("hotss_operatorid"), nvram_get("hotss_locationid"));
			nvram_set("wl0_ssid", ssid);
			nvram_set("time_zone", "+00");
			nvram_set("daylight_time", "1");
		}
		hotspotsys_config();
		start_cron();
	} else if (nvram_match("chilli_enable", "1")) {
		nvram_unset("chilli_def_enable");
		chilli_config();
	}
#else
	if (!nvram_match("chilli_enable", "1"))
		return;

	chilli_config();

#endif

	ret = killall("chilli", SIGTERM);
	ret = killall("chilli", SIGKILL);
	if (f_exists("/tmp/chilli/hotss.conf")) {
#ifdef HAVE_COOVA_CHILLI
		putenv("CHILLISTATEDIR=/var/run/chilli1");
		mkdir("/var/run/chilli1", 0700);
		ret = eval("chilli", "--statedir=/var/run/chilli1",
			"--pidfile=/var/run/chilli1/chilli.pid",
			"-c", "/tmp/chilli/hotss.conf");
#else
		ret = eval("chilli", "-c", "/tmp/chilli/hotss.conf");
#endif
		dd_syslog(LOG_INFO, "hotspotsystem : chilli daemon successfully started\n");
	} else {
#ifdef HAVE_COOVA_CHILLI
		putenv("CHILLISTATEDIR=/var/run/chilli1");
		mkdir("/var/run/chilli1", 0700);
		ret = eval("chilli", "--statedir=/var/run/chilli1",
			"--pidfile=/var/run/chilli1/chilli.pid",
			"-c", "/tmp/chilli/chilli.conf");
#else
		ret = eval("chilli", "-c", "/tmp/chilli/chilli.conf");
#endif
		dd_syslog(LOG_INFO, "chilli : chilli daemon successfully started\n");
	}
#ifdef HAVE_TIEXTRA1
	start_mchilli();
#endif

	cprintf("done\n");
	return;
}
コード例 #29
0
void start_chilli(void)
{
	int ret = 0;
	char ssid[128];

	stop_chilli();		//ensure that its stopped

	if (!strlen(nvram_safe_get("chilli_interface")))
		nvram_set("chilli_interface", get_wdev());
	if (!strlen(nvram_safe_get("hotss_interface")))
		nvram_set("hotss_interface", get_wdev());
	main_config();
	
#ifdef HAVE_HOTSPOT

	if (nvram_match("chilli_enable", "1")
	    && nvram_match("chilli_def_enable", "0")
	    && !nvram_match("hotss_enable", "1")) {
		nvram_unset("chilli_def_enable");
		nvram_set("chilli_enable", "0");
		return;
	}

	if (!nvram_match("chilli_enable", "1")
	    && !nvram_match("hotss_enable", "1")) {
		nvram_unset("chilli_def_enable");
		return;
	}

	if (nvram_match("hotss_enable", "1")) {
		stop_cron();
		if (!nvram_match("chilli_enable", "1")) {
			nvram_set("chilli_enable", "1");	// to get care of firewall, network, etc.
			nvram_set("chilli_def_enable", "0");
		}
		if (!nvram_match("hotss_preconfig", "1")) {
			nvram_set("hotss_preconfig", "1");
			sprintf(ssid, "HotSpotSystem.com-%s_%s",
				nvram_get("hotss_operatorid"),
				nvram_get("hotss_locationid"));
			nvram_set("wl0_ssid", ssid);
			nvram_set("time_zone", "+00");
			nvram_set("daylight_time", "1");
		}
		hotspotsys_config();
		start_cron();
	} else if (nvram_match("chilli_enable", "1")) {
		nvram_unset("chilli_def_enable");
		chilli_config();
	}
#else
	if (!nvram_match("chilli_enable", "1"))
		return;

	chilli_config();

#endif

	ret = killall("chilli", SIGTERM);
	ret = killall("chilli", SIGKILL);
	if (f_exists("/tmp/chilli/hotss.conf")) {
		ret = eval("chilli", "-c", "/tmp/chilli/hotss.conf");
		dd_syslog(LOG_INFO,
			  "hotspotsystem : chilli daemon successfully started\n");
	} else {
		ret = eval("chilli", "-c", "/tmp/chilli/chilli.conf");
		dd_syslog(LOG_INFO,
			  "chilli : chilli daemon successfully started\n");
	}
#ifdef HAVE_TIEXTRA1
	start_mchilli();
#endif

	cprintf("done\n");
	return;
}
コード例 #30
0
int
httpd_check_v2()
{
#if (!defined(W7_LOGO) && !defined(WIFI_LOGO))
	time_t now = uptime();

//	if (!nvram_match("wan_route_x", "IP_Routed"))
//		return 1;

	if (check_count_down)
	{
		check_count_down--;
		httpd_error_count = 0;
		return 1;
	}

        httpd_timer = (httpd_timer + 1) % 2;
        if (httpd_timer) return 1;

	if (nvram_match("v2_debug", "1"))
		dbg("uptime: %d\n", now);

	if (nvram_get("login_timestamp") && ((unsigned long)(now - strtoul(nvram_safe_get("login_timestamp"), NULL, 10)) < 60))
	{
		if (nvram_match("v2_debug", "1"))
			dbg("user login within 1 minutu: %d\n", (unsigned long)(now - strtoul(nvram_safe_get("login_timestamp"), NULL, 10)));

		httpd_error_count = 0;
		return 1;
	}

	int ret = 0;
	FILE *fp = NULL;
	char line[80], cmd[128], url[80];

	/* httpd will not count 127.0.0.1 */
	//sprintf(url, "http://%s/httpd_check.htm", get_lan_ipaddr());
	sprintf(url, "http://%s/httpd_check.htm", "127.0.0.1");
	remove(DETECT_HTTPD_FILE);

	wget_timestamp = uptime();
	memset(wget_timestampstr, 0, 32);
	sprintf(wget_timestampstr, "%lu", wget_timestamp);
	nvram_set("wget_timestamp", wget_timestampstr);
/*
	if (nvram_get("login_timestamp") && !nvram_match("login_timestamp", ""))
	{
		httpd_error_count = 0;
		return 1;
	}
	else
*/
		eval("wget", "-q", url, "-O", DETECT_HTTPD_FILE, "&");

	if ((fp = fopen(DETECT_HTTPD_FILE, "r")) != NULL)
	{
		if ( fgets(line, sizeof(line), fp) != NULL )
		{
			if (strstr(line, "ASUSTeK"))
			{
				if (nvram_match("v2_debug", "1"))
					dbg("httpd is alive!\n");

				ret = 1;
			}
		}

		fclose(fp);
	}
	else
	{
		if (nvram_match("v2_debug", "1"))
			dbg("fopen %s error!\n", DETECT_HTTPD_FILE);

		if (pids("wget"))
			system("killall wget");
	}

	nvram_unset("wget_timestamp");

	if (!ret)
	{
		if (nvram_match("v2_debug", "1"))
			dbg("httpd no response!\n");

		httpd_error_count++;
	}
	else
		httpd_error_count = 0;

	if (nvram_match("v2_debug", "1"))
		dbg("httpd_error_count: %d\n", httpd_error_count);

	if (httpd_error_count > 2)
	{
		dbg("httpd is so dead!!!\n");
		httpd_error_count = 0;
		return 0;
	}
	else
		return 1;
#else
	return 1;
#endif
}