Ejemplo n.º 1
0
int setPLC_para(const char *abuf, int addr)
{
	unsigned char ebuf[32];
	int len, ret;

	if (abuf == NULL)
		return 0;

	memset(ebuf, 0, sizeof(ebuf));

	if (addr == OFFSET_PLC_MAC) {
		len = ETHER_ADDR_LEN;
		if (!isValidPara(abuf, len))
			return 0;

		ret = ether_atoe(abuf, ebuf);
	} else if (addr == OFFSET_PLC_NMK) {
		len = PLC_KEY_LEN;
		if (!isValidPara(abuf, len))
			return 0;

		ret = key_atoe(abuf, ebuf);
	} else
		return 0;

	if (ret) {
		FWrite(ebuf, addr, len);
		getPLC_para(addr);
		return 1;
	}
	else
		return 0;
}
Ejemplo n.º 2
0
static int setup_bcom_wds(int skfd, char *ifname)
{
	char buf[8192];
	char wbuf[80];
	char *v;
	int wds_enabled = 0;

	memset(buf, 0, 8192);
	if (v = nvram_get(wl_var("wds"))) {
		struct maclist *wdslist = (struct maclist *) buf;
		struct ether_addr *addr = wdslist->ea;
		char *next;

		foreach(wbuf, v, next) {
			if (ether_atoe(wbuf, addr->ether_addr_octet)) {
				wdslist->count++;
				addr++;
				wds_enabled = 1;
			}
		}
	}
	bcom_ioctl(skfd, ifname, WLC_SET_WDSLIST, buf, sizeof(buf));

	return wds_enabled;
}
Ejemplo n.º 3
0
void load_sysparam(void)
{
	char macstr[32];
#if defined(RTCONFIG_WIRELESSREPEATER) || defined(RTCONFIG_PROXYSTA)
	char tmp[100], prefix[] = "wlXXXXXXXXXXXXXX";
#endif
#ifdef RTCONFIG_WIRELESSREPEATER
	if (nvram_get_int("sw_mode") == SW_MODE_REPEATER)
	{
		snprintf(prefix, sizeof(prefix), "wl%d.1_", nvram_get_int("wlc_band"));
		strncpy(ssid_g, nvram_safe_get(strcat_r(prefix, "ssid", tmp)), sizeof(ssid_g));
	}
	else
#endif
#ifdef RTCONFIG_BCMWL6
#ifdef RTCONFIG_PROXYSTA
	if (is_psta(0) || is_psta(1))
	{
		snprintf(prefix, sizeof(prefix), "wl%d_", 1 - nvram_get_int("wlc_band"));
		strncpy(ssid_g, nvram_safe_get(strcat_r(prefix, "ssid", tmp)), 32);
	}
	else
#endif
#endif
	strncpy(ssid_g, nvram_safe_get("wl0_ssid"), sizeof(ssid_g));
	strncpy(netmask_g, nvram_safe_get("lan_netmask"), sizeof(netmask_g));
	strncpy(productid_g, get_productid(), sizeof(productid_g));

	snprintf(firmver_g, sizeof(firmver_g), "%s.%s", nvram_safe_get("firmver"), nvram_safe_get("buildno"));

	strcpy(macstr, nvram_safe_get("lan_hwaddr"));
//	printf("mac: %d\n", strlen(macstr));
	if (strlen(macstr)!=0) ether_atoe(macstr, mac);
}
Ejemplo n.º 4
0
Archivo: model.c Proyecto: jilir/wl500g
unsigned char *get_hwaddr(char *ifname, void *buf, int size)
{
#ifdef USE_IFNAME
    struct ifreq ifr;
    int fd, res;
    int addrlen = size > ETHER_ADDR_LEN ? ETHER_ADDR_LEN : size;

    if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
        return NULL;

    memset(&ifr, 0, sizeof(ifr));
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
    res = ioctl(fd, SIOCGIFHWADDR, &ifr);
    close(fd);

    if (res < 0 || ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
        return NULL;

    memmove(buf, ifr.ifr_hwaddr.sa_data, addrlen);
    return buf;
#else
    if (!nvram_invmatch("et0macaddr", ""))
        return NULL;

    ether_atoe(nvram_safe_get("et0macaddr"), buf);
    return buf;
#endif
}
Ejemplo n.º 5
0
int main()
{
	int arp_sockfd;
	struct sockaddr_in router_addr;
	char router_ipaddr[17], router_mac[17];
	unsigned char scan_ipaddr[4]; // scan ip
        struct timeval arp_timeout;

	//Get Router's IP/Mac
	strcpy(router_ipaddr, nvram_safe_get("lan_ipaddr"));
#if defined(RTCONFIG_RGMII_BRCM5301X) || defined(RTCONFIG_QCA)
	strcpy(router_mac, nvram_safe_get("et1macaddr"));
#else
	strcpy(router_mac, nvram_safe_get("et0macaddr"));
#endif
#ifdef RTCONFIG_GMAC3
        if(nvram_match("gmac3_enable", "1"))
                strcpy(router_mac, nvram_safe_get("et2macaddr"));
#endif
        inet_aton(router_ipaddr, &router_addr.sin_addr);
        memcpy(my_ipaddr,  &router_addr.sin_addr, 4);

	//Prepare scan 
        memset(scan_ipaddr, 0x00, 4);
        memcpy(scan_ipaddr, &router_addr.sin_addr, 3);

	if (strlen(router_mac)!=0) ether_atoe(router_mac, my_hwaddr);

        // create UDP socket and bind to "br0" to get ARP packet//
	arp_sockfd = create_socket(INTERFACE);

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

        while(1)
        {
		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
			scan_count=0;
			scan_ipaddr[3]=0;
		}
		//sent_arppacket(arp_sockfd, scan_ipaddr);
	} //End of while
	close(arp_sockfd);
	return 0;
}
Ejemplo n.º 6
0
/* end: 169.254.39.254, 0xfe27fea9 */
int gen_rpc_qcsapi_ip(void)
{
	int i;
	unsigned int j;
	unsigned char *hwaddr;
	char hwaddr_5g[18];
	struct ifreq ifr;
	struct in_addr start, addr;
	int hw_len;
	FILE *fp_qcsapi_conf;

	/* BRCM */
	ether_atoe(nvram_safe_get("lan_hwaddr"), (unsigned char *)&ifr.ifr_hwaddr.sa_data);
	for (j = 0, i = 0; i < 6; i++){
		j += ifr.ifr_hwaddr.sa_data[i] + (j << 6) + (j << 16) - j;
	}
	start.s_addr = htonl(ntohl(0x127fea9 /* start */) +
		((j + 0 /* c->addr_epoch */) % (1 + ntohl(0xfe27fea9 /* end */) - ntohl(0x127fea9 /* start */))));
	nvram_set("QTN_RPC_CLIENT", inet_ntoa(start));

	/* QTN */
	strcpy(hwaddr_5g, nvram_safe_get("lan_hwaddr"));
	inc_mac(hwaddr_5g, 4);
	ether_atoe(hwaddr_5g, (unsigned char *)&ifr.ifr_hwaddr.sa_data);
	for (j = 0, i = 0; i < 6; i++){
		j += ifr.ifr_hwaddr.sa_data[i] + (j << 6) + (j << 16) - j;
	}
	start.s_addr = htonl(ntohl(0x127fea9 /* start */) +
		((j + 0 /* c->addr_epoch */) % (1 + ntohl(0xfe27fea9 /* end */) - ntohl(0x127fea9 /* start */))));
	nvram_set("QTN_RPC_SERVER", inet_ntoa(start));
	if ((fp_qcsapi_conf = fopen("/etc/qcsapi_target_ip.conf", "w")) == NULL){
		logmessage("qcsapi", "write qcsapi conf error");
	}else{
		fprintf(fp_qcsapi_conf, "%s", nvram_safe_get("QTN_RPC_SERVER"));
		fclose(fp_qcsapi_conf);
		logmessage("qcsapi", "write qcsapi conf ok");
	}

#if 0
	do_ping_detect(); /* refer wanduck.c */
#endif
}
Ejemplo n.º 7
0
void run_dms(int force_rescan)
{
	int db_rescan_mode;
	unsigned char mac_bin[ETHER_ADDR_LEN] = {0};
	char mac_str[16];
	char *apps_name = "Media Server";
	char *link_path = "/mnt/minidlna";
	char *conf_path = "/etc/minidlna.conf";
	char *dest_dir = ".dms";
	char *minidlna_argv[] = {
		"/usr/bin/minidlnad",
		"-f", conf_path,
		"-s", NULL,
		NULL,	/* -U */
		NULL
	};

	if (!nvram_match("apps_dms", "1"))
		return;

	if (!is_dms_support())
		return;

	if (is_dms_run())
		return;

	unlink(link_path);
	if (!create_mp_link(dest_dir, link_path, 0))
	{
		if (!create_mp_link(dest_dir, link_path, 1))
		{
			logmessage(apps_name, "Cannot start: unable to create DB dir (/%s) on any volumes!", dest_dir);
			return;
		}
	}

	update_minidlna_conf(link_path, conf_path);

	ether_atoe(nvram_safe_get("il0macaddr"), mac_bin);
	minidlna_argv[4] = ether_etoa3(mac_bin, mac_str);

	db_rescan_mode = nvram_get_int("dlna_rescan");
	if (force_rescan || db_rescan_mode == 2)
		minidlna_argv[5] = "-R";
	else if (db_rescan_mode == 1)
		minidlna_argv[5] = "-U";

	_eval(minidlna_argv, NULL, 0, NULL);

	if (is_dms_run())
		logmessage(apps_name, "daemon is started");
}
Ejemplo n.º 8
0
int
get_lan_mac(unsigned char *mac)
{
    unsigned char *lanmac_str = nvram_get("lan_hwaddr");

    if (mac)
        memset(mac, 0, 6);

    if (!lanmac_str || mac == NULL)
        return -1;

    ether_atoe(lanmac_str, mac);

    return 0;
}
Ejemplo n.º 9
0
int start_dhcp6c(char *wan_ifname)
{
	FILE *fp;
	int wan6_dhcp, dns6_auto, lan6_auto, sla_id, sla_len;
	unsigned int ia_id;
	unsigned char ea[ETHER_ADDR_LEN];
	const char *conf_file = "/etc/dhcp6c.conf";
	const char *duid_file = "/tmp/dhcp6c_duid";

	wan6_dhcp = nvram_get_int("ip6_wan_dhcp");
	dns6_auto = nvram_get_int("ip6_dns_auto");
	lan6_auto = nvram_get_int("ip6_lan_auto");

	if (!wan6_dhcp && !dns6_auto && !lan6_auto)
		return 1;

	ia_id = 0;
	sla_id = 1;
	sla_len = 0; /* auto prefix always /64 */

	if (ether_atoe(nvram_safe_get("wan_hwaddr"), ea)) {
		uint16_t duid_len;
		struct {
			uint16_t type;
			uint16_t hwtype;
		} __attribute__ ((__packed__)) duid3;
		
		/* generate IAID from the last 20 bits of WAN MAC */
		ia_id = ((unsigned int)(ea[3] & 0x0f) << 16) |
			((unsigned int)(ea[4]) << 8) |
			((unsigned int)(ea[5]));
		
		/* create DUID from WAN MAC */
		duid_len = sizeof(duid3) + ETHER_ADDR_LEN;
		duid3.type = htons(3);		/* DUID-LL */
		duid3.hwtype = htons(1);	/* Ethernet */
		
		unlink(duid_file);
		if ((fp = fopen(duid_file, "w"))) {
			size_t duid_done = 0;
			duid_done += fwrite(&duid_len, sizeof(duid_len), 1, fp);
			duid_done += fwrite(&duid3, sizeof(duid3), 1, fp);
			duid_done += fwrite(&ea, ETHER_ADDR_LEN, 1, fp);
			fclose(fp);
			if (duid_done != 3)
				unlink(duid_file);
		}
	}
Ejemplo n.º 10
0
// dummy TBD 
int getassoclist_ath9k(char *ifname, unsigned char *list)
{
	unsigned int *count = (unsigned int *)list;
	struct mac80211_info *mac80211_info;
	struct wifi_client_info *wc;
	unsigned char *l = (unsigned char *)list;
	mac80211_info = mac80211_assoclist(ifname);
	l += 4;
	count[0]=0;
	for (wc = mac80211_info->wci; wc; wc = wc->next) {
	
		ether_atoe(wc->mac,l);
		l+=6;
		count[0]++;
	}
	free_wifi_clients(mac80211_info->wci);
	free(mac80211_info);
	return count[0];
}
Ejemplo n.º 11
0
int
set_interface_hwaddr(const char *ifname, const char *mac_str)
{
	struct ifreq ifr;
	int sockfd, ret;

	if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
		return -1;

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
	ether_atoe(mac_str, ifr.ifr_hwaddr.sa_data);

	ret = ioctl(sockfd, SIOCSIFHWADDR, &ifr);

	close(sockfd);

	return ret;
}
Ejemplo n.º 12
0
/* OSL arp function */
int
wps_osl_arp_get(char *client_ip, unsigned char mac[6])
{
	FILE *arpfile;

	char proc_net_arp[] = "/proc/net/arp";
	char titlestr[100], ipaddrstr[20], hwtypestr[10], flagstr[6],
		hwaddrstr[20], maskstr[6], devicestr[20];

	/* Get from arp table for ip to mac */
	if (!(arpfile = fopen(proc_net_arp, "r")) || !fgets(titlestr, 100, arpfile)) {
		if (arpfile)
			fclose(arpfile);

		TUTRACE((TUTRACE_ERR, "can't open /proc/net/arp!!\n"));
		return -1;
	}

	/* parsing /proc/net/arp contents to get desired client mac */
	while (fscanf(arpfile, "%s\t%s\t%s\t%s\t%s\t%s\n",
		ipaddrstr, hwtypestr, flagstr, hwaddrstr, maskstr, devicestr) != EOF) {

		if (!strcasecmp(client_ip, ipaddrstr)) {
			ether_atoe(hwaddrstr, mac);
			TUTRACE((TUTRACE_INFO,
				"SSR client mac = %02x:%02x:%02x:%02x:%02x:%02x!!\n",
				mac[0], mac[1], mac[2],
				mac[3], mac[4], mac[5]));
			break;
		}
	}

	fclose(arpfile);

	return 0;
}
Ejemplo n.º 13
0
int
get_real_mac(char *mac, int maclen)
{
    int idx, unit, subunit;
    char *ptr, ifname[32];
    wlif_name_desc_t *wlif_name;

    if (mac == NULL ||
            maclen < ETHER_ADDR_LEN)
        return -1;

    if (mac[0] != 0 || mac[1] != 0 ||
            mac[2] != 0)
        return 0; /* is a real mac, fast path */

    idx = mac[3];
    idx --; /* map to wlif_name_array index */
    unit = mac[4];
    subunit = mac[5];
    if (idx < 0 || idx >= ARRAYSIZE(wlif_name_array))
        return -1;

    /* get wlx.y mac addr */
    wlif_name = &wlif_name_array[idx];
    if (wlif_name->subunit && !wlif_name->wds)
        snprintf(ifname, sizeof(ifname), "wl%d.%d_hwaddr", unit, subunit);
    else
        snprintf(ifname, sizeof(ifname), "wl%d_hwaddr", unit);

    ptr = nvram_get(ifname);
    if (ptr == NULL)
        return -1;

    ether_atoe(ptr, mac);
    return 0;
}
Ejemplo n.º 14
0
Archivo: rc.c Proyecto: Einheri/wl500g
static int
build_ifnames(char *type, char *names, int *size)
{
	char name[32], *next;
	int len = 0;
	int s;

	/* open a raw scoket for ioctl */
	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
       		return -1;

	/*
	 * go thru all device names (wl<N> il<N> et<N> vlan<N>) and interfaces to 
	 * build an interface name list in which each i/f name coresponds to a device
	 * name in device name list. Interface/device name matching rule is device
	 * type dependant:
	 *
	 *	wl:	by unit # provided by the driver, for example, if eth1 is wireless
	 *		i/f and its unit # is 0, then it will be in the i/f name list if
	 *		wl0 is in the device name list.
	 *	il/et:	by mac address, for example, if et0's mac address is identical to
	 *		that of eth2's, then eth2 will be in the i/f name list if et0 is 
	 *		in the device name list.
	 *	vlan:	by name, for example, vlan0 will be in the i/f name list if vlan0
	 *		is in the device name list.
	 */
	foreach (name, type, next) {
		struct ifreq ifr;
		int i, unit;
		char var[32], *mac, ea[ETHER_ADDR_LEN];
		
		/* vlan: add it to interface name list */
		if (!strncmp(name, "vlan", 4)) {
			/* append interface name to list */
			len += snprintf(&names[len], *size - len, "%s ", name);
			continue;
		}

		/* others: proceed only when rules are met */
		for (i = 1; i <= DEV_NUMIFS; i ++) {
			/* ignore i/f that is not ethernet */
			ifr.ifr_ifindex = i;
			if (ioctl(s, SIOCGIFNAME, &ifr))
				continue;
			if (ioctl(s, SIOCGIFHWADDR, &ifr))
				continue;
			if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
				continue;
			if (!strncmp(ifr.ifr_name, "vlan", 4))
				continue;
			/* wl: use unit # to identify wl */
			if (!strncmp(name, "wl", 2)) {
				if (wl_probe(ifr.ifr_name) ||
				    wl_ioctl(ifr.ifr_name, WLC_GET_INSTANCE, &unit, sizeof(unit)) ||
				    unit != atoi(&name[2]))
					continue;
			}
			/* et/il: use mac addr to identify et/il */
			else if (!strncmp(name, "et", 2) || !strncmp(name, "il", 2)) {
				snprintf(var, sizeof(var), "%smacaddr", name);
				if (!(mac = nvram_get(var)) || !ether_atoe(mac, ea) ||
				    memcmp(ea, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN))
					continue;

				// add by Chen-I to filter out wl interface here
				if (!wl_probe(ifr.ifr_name))
					continue;

			}
			/* mac address: compare value */
			else if (ether_atoe(name, ea) && !memcmp(ea, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN))
				;
			/* others: ignore */
			else
				continue;

			/* append interface name to list */
			len += snprintf(&names[len], *size - len, "%s ", ifr.ifr_name);
		}
	}
	
	close(s);

	*size = len;
	return 0;
}	
Ejemplo n.º 15
0
void start_watchdog(int skfd, char *ifname)
{
	FILE *f;
	unsigned char buf[8192], wdslist[8192], wbuf[80], *v, *p, *next, *tmp;
	int i, j, wds = 0, c = 0, restart_wds = 0, wdstimeout = 0, infra;
	int tsf_attack = 0;
	unsigned int cur_tsf = 0;

	if (fork())
		return;

	f = fopen("/var/run/wifi.pid", "w");
	fprintf(f, "%d\n", getpid());
	fclose(f);

	infra = strtol(nvram_safe_get(wl_var("infra")), NULL, 0);
	
	v = nvram_safe_get(wl_var("wds"));
	memset(wdslist, 0, 8192);
	p = wdslist;
	foreach(wbuf, v, next) {
		if (ether_atoe(wbuf, p)) {
			p += 6;
			wds++;
		}
	}
	
	for (;;) {
		sleep(1);
		
		/* refresh the distance setting - the driver might change it */
		set_distance(skfd, ifname);

		if (restart_wds)
			wdstimeout--;

		if ((c++ < WD_INTERVAL) || ((restart_wds > 0) && (wdstimeout > 0)))
			continue;
		else
			c = 0;

		/*
		 * In adhoc mode it can be desirable to use a specific BSSID to prevent
		 * accidental cell splitting caused by broken cards/drivers.
		 * When wl0_bssid is set, make sure the current BSSID matches the one
		 * set in nvram. If it doesn't change it and try to overpower the hostile
		 * AP by increasing the upper 32 bit of the TSF by one every time.
		 *
		 * In client mode simply use that variable to connect to a specific AP
		 */
		if ((infra < 1) ||
			nvram_match(wl_var("mode"), "sta") ||
			nvram_match(wl_var("mode"), "wet")) {
			rw_reg_t reg;

			if (!(tmp = nvram_get(wl_var("bssid"))))
				continue;

			if (!ether_atoe(tmp, wbuf))
				continue;

			if ((bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf, 6) < 0) ||
				(memcmp(buf, "\0\0\0\0\0\0", 6) == 0) ||
				(memcmp(buf, wbuf, 6) != 0)) {
			
				if (bcom_ioctl(skfd, ifname, (infra < 1 ? WLC_SET_BSSID : WLC_REASSOC), wbuf, 6) < 0)
					continue;

				if (infra < 1) {
					/* upper 32 bit of the TSF */
					memset(&reg, 0, sizeof(reg));
					reg.size = 4;
					reg.byteoff = 0x184;
					
					bcom_ioctl(skfd, ifname, WLC_R_REG, &reg, sizeof(reg));

					if (reg.val > cur_tsf)
						cur_tsf = reg.val;
					
					cur_tsf |= 1;
					cur_tsf <<=1;
					reg.val = (cur_tsf == ~0 ? cur_tsf : cur_tsf + 1);
					bcom_ioctl(skfd, ifname, WLC_W_REG, &reg, sizeof(reg));
					
					/* set the lower 32 bit as well */
					reg.byteoff = 0x180;
					bcom_ioctl(skfd, ifname, WLC_W_REG, &reg, sizeof(reg));
					  
					/* set the bssid again, just in case.. */
					bcom_ioctl(skfd, ifname, WLC_SET_BSSID, wbuf, 6);
					
					/* reached the maximum, next time wrap around to (1 << 16)
					 * instead of 0 */
					if (cur_tsf == ~0)
						cur_tsf = (1 << 16);
				}
			}
		}

		if (infra < 1)
			continue;

		if (nvram_match(wl_var("mode"), "sta") ||
			nvram_match(wl_var("mode"), "wet")) {

			i = 0;
			if (bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf, 6) < 0) 
				i = 1;
			if (memcmp(buf, "\0\0\0\0\0\0", 6) == 0)
				i = 1;
			
			memset(buf, 0, 8192);
			strcpy(buf, "sta_info");
			bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf + strlen(buf) + 1, 6);
			if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
				i = 1;
			} else {
				sta_info_t *sta = (sta_info_t *) (buf + 4);
				if ((sta->flags & 0x18) != 0x18) 
					i = 1;
				if (sta->idle > WD_CLIENT_IDLE)
					i = 1;
			}
			
			if (i) 
				set_wext_ssid(skfd, ifname);
		}
		
		/* wds */
		p = wdslist;
		restart_wds = 0;
		if (wdstimeout == 0)
			wdstimeout = strtol(nvram_safe_get(wl_var("wdstimeout")),NULL,0);
		
		for (i = 0; (i < wds) && !restart_wds; i++, p += 6) {
			memset(buf, 0, 8192);
			strcpy(buf, "sta_info");
			memcpy(buf + strlen(buf) + 1, p, 6);
			if (!(bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0)) {
				sta_info_t *sta = (sta_info_t *) (buf + 4);
				if ((sta->flags & 0x40) == 0x40) /* this is a wds link */ { 
					if (sta->idle > wdstimeout)
						restart_wds = 1;

					/* if not authorized after WD_AUTH_IDLE seconds idletime */
					if (((sta->flags & WL_STA_AUTHO) != WL_STA_AUTHO) && (sta->idle > WD_AUTH_IDLE))
						restart_wds = 1;
				}
			}
		}

		if (restart_wds && (wdstimeout > 0)) {
			setup_bcom_wds(skfd, ifname);
		}
	}
}
Ejemplo n.º 16
0
void set_mac(const char *ifname, const char *nvname, int plus)
{
	int sfd;
	struct ifreq ifr;
	int up;
	int j;
	
	if ((sfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
		_dprintf("%s: %s %d\n", ifname, __FUNCTION__, __LINE__);
		return;
	}

	strcpy(ifr.ifr_name, ifname);
	
	up = 0;
	if (ioctl(sfd, SIOCGIFFLAGS, &ifr) == 0) {
		if ((up = ifr.ifr_flags & IFF_UP) != 0) {
			ifr.ifr_flags &= ~IFF_UP;
			if (ioctl(sfd, SIOCSIFFLAGS, &ifr) != 0) {
				_dprintf("%s: %s %d\n", ifname, __FUNCTION__, __LINE__);
			}
		}
	}
	else {
		_dprintf("%s: %s %d\n", ifname, __FUNCTION__, __LINE__);
	}
	
	if (!ether_atoe(nvram_safe_get(nvname), (unsigned char *)&ifr.ifr_hwaddr.sa_data)) {
		if (!ether_atoe(nvram_safe_get("et0macaddr"), (unsigned char *)&ifr.ifr_hwaddr.sa_data)) {

			// goofy et0macaddr, make something up
			nvram_set("et0macaddr", "00:01:23:45:67:89");
			ifr.ifr_hwaddr.sa_data[0] = 0;
			ifr.ifr_hwaddr.sa_data[1] = 0x01;
			ifr.ifr_hwaddr.sa_data[2] = 0x23;
			ifr.ifr_hwaddr.sa_data[3] = 0x45;
			ifr.ifr_hwaddr.sa_data[4] = 0x67;
			ifr.ifr_hwaddr.sa_data[5] = 0x89;
		}
		
		while (plus-- > 0) {
			for (j = 5; j >= 3; --j) {
				ifr.ifr_hwaddr.sa_data[j]++;
				if (ifr.ifr_hwaddr.sa_data[j] != 0) break;	// continue if rolled over
			}
		}
	}
	
	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
	if (ioctl(sfd, SIOCSIFHWADDR, &ifr) == -1) {
		_dprintf("Error setting %s address\n", ifname);
	}

	if (up) {
		if (ioctl(sfd, SIOCGIFFLAGS, &ifr) == 0) {
			ifr.ifr_flags |= IFF_UP|IFF_RUNNING;
			if (ioctl(sfd, SIOCSIFFLAGS, &ifr) == -1) {
				_dprintf("%s: %s %d\n", ifname, __FUNCTION__, __LINE__);
			}
		}
		else {
			_dprintf("%s: %s %d\n", ifname, __FUNCTION__, __LINE__);
		}
	}
	
	close(sfd);
}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
void getsyspara(void)
{
	unsigned char buffer[32];
	int i;
	char macaddr[]="00:11:22:33:44:55";
	char macaddr2[]="00:11:22:33:44:56";
	char macaddr3[]="001122334457";
	char macaddr4[]="001122334458";
	char ea[ETHER_ADDR_LEN];
	char country_code[3];
	char pin[9];
	char productid[13];
	char fwver[8], fwver_sub[16];
	char blver[20];
	unsigned char txbf_para[33];
	
	/* /dev/mtd/2, RF parameters, starts from 0x40000 */
	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(fwver_sub, 0, sizeof(fwver_sub));
	memset(txbf_para, 0, sizeof(txbf_para));

	if (FRead(buffer, OFFSET_MAC_ADDR, 6)<0)
	{
		dbg("READ MAC address: Out of scope\n");
	}
	else
	{
		if (buffer[0]!=0xff)
		{
			ether_etoa(buffer, macaddr);
			ether_etoa2(buffer, macaddr3);
		}
	}
	
	if (FRead(buffer, OFFSET_MAC_ADDR_2G, 6)<0)
	{
		dbg("READ MAC address 2G: Out of scope\n");
	}
	else
	{
		if (buffer[0]!=0xff)
		{
			ether_etoa(buffer, macaddr2);
			ether_etoa2(buffer, macaddr4);
		}
	}

	nvram_set("il0macaddr", macaddr);
	nvram_set("il1macaddr", macaddr2);
	nvram_set("et0macaddr", macaddr);
	nvram_set("br0hexaddr", macaddr3);
	nvram_set("wanhexaddr", macaddr4);
	
	if (FRead(buffer, OFFSET_MAC_GMAC0, 6)<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(buffer, OFFSET_MAC_GMAC2, 6)<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 (FRead(country_code, OFFSET_COUNTRY_CODE, 2)<0)
	{
		dbg("READ ASUS country code: Out of scope\n");
		strcpy(country_code, "GB");
	}
	else
	{
		country_code[2] = 0;
		if ((unsigned char)country_code[0]==0xff)
			strcpy(country_code, "GB");
	}
	
	if (strlen(nvram_safe_get("rt_country_code")) == 0)
	{
		nvram_set("rt_country_code", country_code);
	}
	
	if (strlen(nvram_safe_get("wl_country_code")) == 0)
	{
		nvram_set("wl_country_code", country_code);
	}
	
	if (!strcasecmp(nvram_safe_get("wl_country_code"), "BR"))
		nvram_set("wl_country_code", "UZ");
	
	/* reserved for Ralink. used as ASUS pin code. */
	if (FRead(pin, OFFSET_PIN_CODE, 8)<0)
	{
		dbg("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");
	}

#if defined(USE_RT3352_MII)
 #define EEPROM_INIC_SIZE (512)
 #define EEPROM_INIT_ADDR 0x48000
	{
		char eeprom[EEPROM_INIC_SIZE];
		if(FRead(eeprom, EEPROM_INIT_ADDR, sizeof(eeprom)) < 0)
		{
			dbg("READ iNIC EEPROM: Out of scope!\n");
		}
		else
		{
			FILE *fp;
			if((fp = fopen("/etc/Wireless/iNIC/iNIC_e2p.bin", "w")))
			{
				fwrite(eeprom, sizeof(eeprom), 1, fp);
				fclose(fp);
			}
		}
	}
#endif

	/* /dev/mtd/3, firmware, starts from 0x50000 */
	if (FRead(buffer, 0x50020, sizeof(buffer))<0)
	{
		dbg("READ firmware header: Out of scope\n");
		nvram_set("productid", "unknown");
		nvram_set("firmver", "unknown");
	}
	else
	{
		strncpy(productid, buffer + 4, 12);
		productid[12] = 0;
		
		if(valid_subver(buffer[27]))
			sprintf(fwver_sub, "%d.%d.%d.%d%c", buffer[0], buffer[1], buffer[2], buffer[3], buffer[27]);
		else
			sprintf(fwver_sub, "%d.%d.%d.%d", buffer[0], buffer[1], buffer[2], buffer[3]);
		
#if defined(FWBLDSTR)
		sprintf(fwver_sub, "%s-%s", fwver_sub, FWBLDSTR);
#endif
		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));
		nvram_set("firmver_sub", trim_r(fwver_sub));
	}

	memset(buffer, 0, sizeof(buffer));
	FRead(buffer, OFFSET_BOOT_VER, 4);
	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));

	int count_0xff = 0;
	if (FRead(txbf_para, OFFSET_TXBF_PARA, 33) < 0)
	{
		dbg("READ TXBF PARA address: Out of scope\n");
	}
	else
	{
		for (i = 0; i < 33; i++)
		{
			if (txbf_para[i] == 0xff)
				count_0xff++;
		}
	}

	if (count_0xff == 33)
		nvram_set("wl_txbf_en", "0");
	else
		nvram_set("wl_txbf_en", "1");
}
Ejemplo n.º 19
0
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);
}
Ejemplo n.º 20
0
void vlan_init(int portmask)
{
	int phyUnit;
	unsigned int phyBase;
	unsigned int phyReg;
	unsigned int phyAddr;
	int i;
	int numports = 5;

	for (i = 0; i < numports - 1; i++)	// last one will be wan port
	{
		ipPhyInfo[i].VLANTableSetting = IP_LAN_PORT_VLAN;
	}
	ipPhyInfo[i++].VLANTableSetting = IP_WAN_PORT_VLAN;
	ipPhyInfo[i].VLANTableSetting = IP_LAN_PORT_VLAN;
	ipPhyInfo[i].isEnetPort = FALSE;
	ipPhyInfo[i].isPhyAlive = TRUE;
	ipPhyInfo[i++].phyAddr = 0x0;

	numports = i;
	fprintf(stderr, "Reset ICPLUS Phy\n");
	for (phyUnit = 0; phyUnit < numports; phyUnit++) {
		if (((1 << phyUnit) & portmask)) {
			phyAddr = IP_PHYADDR(phyUnit);
			setPhy(phyAddr, IP_PHY_CONTROL, IP_CTRL_SOFTWARE_RESET);
		}
	}
	sleep(1);
	fprintf(stderr, "Start Autonegotiation\n");
	for (phyUnit = 0; phyUnit < numports; phyUnit++) {

		if (((1 << phyUnit) & portmask)) {
			phyAddr = IP_PHYADDR(phyUnit);

			setPhy(phyAddr, IP_AUTONEG_ADVERT, IP_ADVERTISE_ALL);
			setPhy(phyAddr, IP_PHY_CONTROL,
			       IP_CTRL_AUTONEGOTIATION_ENABLE |
			       IP_CTRL_START_AUTONEGOTIATION);
		}
	}
	int timeout = 5;

	for (phyUnit = 0; (phyUnit < numports); phyUnit++) {
		if (((1 << phyUnit) & portmask)) {
			for (;;) {
				phyAddr = IP_PHYADDR(phyUnit);

				int phyHwStatus =
				    getPhy(phyAddr, IP_PHY_STATUS);

				if (IP_AUTONEG_DONE(phyHwStatus)) {
					fprintf(stderr,
						"Port %d, Neg Success\n",
						phyUnit);
					break;
				}
				if (timeout == 0) {
					fprintf(stderr,
						"Port %d, Negogiation timeout\n",
						phyUnit);
					break;
				}
				if (--timeout == 0) {
					fprintf(stderr,
						"Port %d, Negogiation timeout\n",
						phyUnit);
					break;
				}
				usleep(150);
			}
		}
	}

	fprintf(stderr, "Setup VLANS\n");
	/*
	 * setPhy(29,24,0); setPhy(29,25,0); setPhy(29,26,0); setPhy(29,27,0);
	 * setPhy(29,28,2); setPhy(29,30,0); setPhy(29,23,0x07c2);
	 * setPhy(30,1,0x002f); setPhy(30,2,0x0030); setPhy(30,9,0x1089);
	 */
	unsigned int phy1Reg = 0;
	unsigned int phy2Reg = 0;
	unsigned int phy23Reg = 0;
	unsigned int phy9Reg = 0;
	for (phyUnit = 0; phyUnit < numports; phyUnit++) {
		if (((1 << phyUnit) & portmask)) {
			setPhy(IP_GLOBAL_PHY29_ADDR,
			       IP_GLOBAL_PHY29_24_REG +
			       ((phyUnit == 5) ? (phyUnit + 1) : phyUnit),
			       IP_VLAN_TABLE_SETTING(phyUnit));
			fprintf(stderr, "write register %d, addr %d with %X\n",
				IP_GLOBAL_PHY29_ADDR,
				IP_GLOBAL_PHY29_24_REG +
				((phyUnit == 5) ? (phyUnit + 1) : phyUnit),
				IP_VLAN_TABLE_SETTING(phyUnit));
			if (IP_IS_ENET_PORT(phyUnit)) {
				if (IP_IS_WAN_PORT(phyUnit)) {
					phy2Reg |=
					    ((1 << phyUnit) <<
					     IP_VLAN2_OUTPUT_PORT_MASK_S);
				} else {
					phy1Reg |=
					    ((1 << phyUnit) <<
					     IP_VLAN0_OUTPUT_PORT_MASK_S);
				}
				phy23Reg =
				    phy23Reg | ((1 << phyUnit) <<
						IP_PORTX_REMOVE_TAG_S);
				phy23Reg =
				    phy23Reg & ~((1 << phyUnit) <<
						 IP_PORTX_ADD_TAG_S);
			} else {
				phy1Reg |=
				    ((1 << phyUnit) <<
				     IP_VLAN0_OUTPUT_PORT_MASK_S);
				phy2Reg |=
				    ((1 << phyUnit) <<
				     IP_VLAN2_OUTPUT_PORT_MASK_S);
				phy23Reg = phy23Reg | (1 << IP_PORT5_ADD_TAG_S);
				phy23Reg =
				    phy23Reg & ~(1 << IP_PORT5_REMOVE_TAG_S);

			}
		}
	}
	phy9Reg = 0;		//getPhy(IP_GLOBAL_PHY30_ADDR,IP_GLOBAL_PHY30_9_REG);
	phy9Reg = phy9Reg | TAG_VLAN_ENABLE;
	phy9Reg = phy9Reg & ~VID_INDX_SEL_M;
	phy9Reg = phy9Reg | 1;	//1 vlan group used for lan
	phy9Reg = phy9Reg | 1 << 3;	//enable smart mac
	phy9Reg = phy9Reg | 1 << 12;	//port 4 is a wan port (required for smart mac)

	fprintf(stderr, "write register %d, addr %d with %X\n",
		IP_GLOBAL_PHY29_ADDR, IP_GLOBAL_PHY29_23_REG, phy23Reg);
	fprintf(stderr, "write register %d, addr %d with %X\n",
		IP_GLOBAL_PHY30_ADDR, IP_GLOBAL_PHY30_1_REG, phy1Reg);
	fprintf(stderr, "write register %d, addr %d with %X\n",
		IP_GLOBAL_PHY30_ADDR, IP_GLOBAL_PHY30_2_REG, phy2Reg);
	fprintf(stderr, "write register %d, addr %d with %X\n",
		IP_GLOBAL_PHY30_ADDR, IP_GLOBAL_PHY30_9_REG, phy9Reg);
	setPhy(IP_GLOBAL_PHY29_ADDR, IP_GLOBAL_PHY29_23_REG, phy23Reg);
	setPhy(IP_GLOBAL_PHY30_ADDR, IP_GLOBAL_PHY30_1_REG, phy1Reg);
	setPhy(IP_GLOBAL_PHY30_ADDR, IP_GLOBAL_PHY30_2_REG, phy2Reg);
	setPhy(IP_GLOBAL_PHY30_ADDR, IP_GLOBAL_PHY30_9_REG, phy9Reg);
//
//              echo "echo \"WRITE 29 23 07c2\" > ".$mii_dev."\n";
//
//              echo "echo \"WRITE 29 24 0\"    > ".$mii_dev."\n";      /* PORT0 Default VLAN ID */
//              echo "echo \"WRITE 29 25 0\"    > ".$mii_dev."\n";      /* PORT1 Default VLAN ID */
//              echo "echo \"WRITE 29 26 0\"    > ".$mii_dev."\n";      /* PORT2 Default VLAN ID */
//              echo "echo \"WRITE 29 27 0\"    > ".$mii_dev."\n";      /* PORT3 Default VLAN ID */
//              echo "echo \"WRITE 29 28 2\"    > ".$mii_dev."\n";      /* PORT4 Default VLAN ID */
//              echo "echo \"WRITE 29 30 0\"    > ".$mii_dev."\n";      /* PORT5 Default VLAN ID (CPU) */
//              echo "echo \"WRITE 29 23 07c2\" > ".$mii_dev."\n";
//              echo "echo \"WRITE 30 1 002f\"  > ".$mii_dev."\n";      /* Port 5,3,2,1,0 = VLAN 0 */
//              echo "echo \"WRITE 30 2 0030\"  > ".$mii_dev."\n";      /* Port 5,4 = VLAN 2 */
//              echo "echo \"WRITE 30 9 1089\"  > ".$mii_dev."\n";
	eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
	eval("vconfig", "add", "eth0", "0");
	eval("vconfig", "add", "eth0", "2");
	struct ifreq ifr;
	int s;

	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
		char eabuf[32];

		strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
		ioctl(s, SIOCGIFHWADDR, &ifr);
		char macaddr[32];

		strcpy(macaddr,
		       ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data,
				  eabuf));
		nvram_set("et0macaddr", macaddr);
		// MAC_ADD (macaddr);
		ether_atoe(macaddr, (unsigned char *)ifr.ifr_hwaddr.sa_data);
		strncpy(ifr.ifr_name, "vlan2", IFNAMSIZ);
		ioctl(s, SIOCSIFHWADDR, &ifr);
		close(s);
	}
	eval("ifconfig", "vlan0", "promisc");
	eval("ifconfig", "vlan2", "promisc");
}
Ejemplo n.º 21
0
int main()
{
	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[512];
	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;

        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
        spinlock_lock(SPINLOCK_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");
            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;
	spinlock_unlock(SPINLOCK_Networkmap);	

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

	//Prepare scan 
        memset(scan_ipaddr, 0x00, 4);
        memcpy(scan_ipaddr, &router_addr.sin_addr, 3);
	networkmap_fullscan = 1;
	nvram_set("networkmap_fullscan", "1");

	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 = 10000;
		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) { 
	                arp_timeout.tv_sec = 0;
        	        arp_timeout.tv_usec = 10000;
                	setsockopt(arp_sockfd, SOL_SOCKET, SO_RCVTIMEO, &arp_timeout, sizeof(arp_timeout));//set receive timeout
			NMP_DEBUG("Starting full scan!\n");
			
                        //reset client tables
			spinlock_lock(SPINLOCK_Networkmap);
        		memset(p_client_detail_info_tab, 0x00, sizeof(CLIENT_DETAIL_INFO_TABLE));
        		p_client_detail_info_tab->detail_info_num = 0;
			spinlock_unlock(SPINLOCK_Networkmap);
		    }
		    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 = 1;
                	arp_timeout.tv_usec = 500000; //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

		arp_getlen=recvfrom(arp_sockfd, buffer, 512, 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, 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],
				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);

			//ARP Response packet to router
			if( 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
			{
			    //NMP_DEBUG("   It's an ARP Response to Router!\n");
                            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) ) 
                                    break;
                            }
			    //i=0, table is empty.
			    //i=num, no the same ip at table.
			    if(i==p_client_detail_info_tab->ip_mac_num){
				spinlock_lock(SPINLOCK_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->ip_mac_num++;
				spinlock_unlock(SPINLOCK_Networkmap);

			    #ifdef DEBUG  //Write client info to file
                		fp_ip=fopen("/var/client_ip_mac.txt", "a");
                		if (fp_ip==NULL) {
                    		printf("File Open Error!\n");
                		}
                		else {
                        	printf("Fill: %d-> %d.%d", 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) ) {
                                              	NMP_DEBUG_M("Find the same IP 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 IP\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

	    //Find All Application of clients
	    if(p_client_detail_info_tab->detail_info_num < p_client_detail_info_tab->ip_mac_num) {
		FindAllApp(my_ipaddr, 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) {
                        printf("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
		p_client_detail_info_tab->detail_info_num++;
	    }

	} //End of main while loop
	close(arp_sockfd);
	return 0;
}
Ejemplo n.º 22
0
void start_sysinit(void)
{
	char buf[PATH_MAX];
	struct stat tmp_stat;
	time_t tm = 0;

	mknod("/dev/mmc", S_IFBLK | 0660, makedev(126, 0));
	mknod("/dev/mmc0", S_IFBLK | 0660, makedev(126, 1));
	mknod("/dev/mmc1", S_IFBLK | 0660, makedev(126, 2));
	mknod("/dev/mmc2", S_IFBLK | 0660, makedev(126, 3));
	mknod("/dev/mmc3", S_IFBLK | 0660, makedev(126, 4));

	eval("/bin/tar", "-xzf", "/dev/mtdblock/3", "-C", "/");
	FILE *in = fopen("/tmp/nvram/nvram.db", "rb");

	if (in != NULL) {
		fclose(in);
		eval("/usr/sbin/convertnvram");
		eval("/sbin/mtd", "erase", "nvram");
		nvram_commit();
	}
	if (!nvram_match("disable_watchdog", "1"))
		eval("watchdog");
	/*
	 * Setup console 
	 */

	cprintf("sysinit() klogctl\n");
	klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
	cprintf("sysinit() get router\n");
#ifdef HAVE_RTG32
	insmod("slhc");
	insmod("ppp_generic");
	insmod("ppp_async");
	insmod("ppp_synctty");
	insmod("ppp_mppe_mppc ");
	insmod("pppox");
	insmod("pppoe");
#endif
	/*
	 * network drivers 
	 */
#ifdef HAVE_HOTPLUG2
	insmod("ar231x");
#else
	insmod("ar2313");
#endif
	detect_wireless_devices();

	eval("ifconfig", "eth0", "up");	// wan
	system("swconfig dev eth0 set reset 1");
	system("swconfig dev eth0 set enable_vlan 1");
#ifdef HAVE_RTG32
	system("swconfig dev eth0 vlan 1 set ports \"0t 1 2 3 4\"");
	system("swconfig dev eth0 vlan 2 set ports \"0t 5\"");
#else
	system("swconfig dev eth0 vlan 1 set ports \"0t 2 3 4 5\"");
	system("swconfig dev eth0 vlan 2 set ports \"0t 1\"");
#endif
	system("swconfig dev eth0 set apply");
	eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
	eval("vconfig", "add", "eth0", "1");
	eval("vconfig", "add", "eth0", "2");

	struct ifreq ifr;
	int s;

	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
		char eabuf[32];

		strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
		ioctl(s, SIOCGIFHWADDR, &ifr);
		char macaddr[32];

		strcpy(macaddr, ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data, eabuf));
		nvram_set("et0macaddr", macaddr);
		MAC_ADD(macaddr);
		ether_atoe(macaddr, (unsigned char *)ifr.ifr_hwaddr.sa_data);
		strncpy(ifr.ifr_name, "vlan2", IFNAMSIZ);
		ioctl(s, SIOCSIFHWADDR, &ifr);
		close(s);
	}
	eval("gpio", "enable", "1");
#ifdef HAVE_RTG32
	writeproc("/proc/sys/dev/wifi0/ledpin", "7");
	writeproc("/proc/sys/dev/wifi0/softled", "1");
#else
	writeproc("/proc/sys/dev/wifi0/ledpin", "0");
	writeproc("/proc/sys/dev/wifi0/softled", "1");
#endif
	/*
	 * Set a sane date 
	 */
	stime(&tm);
	nvram_set("wl0_ifname", "ath0");

	return;
}
Ejemplo n.º 23
0
/*
 * var_pControlClientTable():
 *   Handle this table separately from the scalar value case.
 *   The workings of this are basically the same as for var_parentalControl above.
 */
unsigned char *
var_pControlClientTable(struct variable *vp,
    	    oid     *name,
    	    size_t  *length,
    	    int     exact,
    	    size_t  *var_len,
    	    WriteMethod **write_method)
{
    /* variables we may use later */
    static long index;
    static unsigned char string[SPRINT_MAX_LEN];
    int i, val_count = 0;
    char *nv, *nvp, *b; 

    if(sw_mode == SW_MODE_AP || sw_mode == SW_MODE_REPEATER || !strcmp(nmp_get("ipv6_service"), "disabled"))    //Doesn't support in AP and repeater mode
       return NULL;     

    if(!pc_list_flag) {
        for(i = 0; i < MAX_PCONTROL_ENTRY; i++)
            memset(&pc_list_info[i], 0x0, sizeof(pc_list_info_t));        
        pc_list_count = get_pc_list_info(&pc_list_info);
        pc_list_flag = 1;
    }

    /* 
   * This assumes that the table is a 'simple' table.
   *	See the implementation documentation for the meaning of this.
   *	You will need to provide the correct value for the TABLE_SIZE parameter
   *
   * If this table does not meet the requirements for a simple table,
   *	you will need to provide the replacement code yourself.
   *	Mib2c is not smart enough to write this for you.
   *    Again, see the implementation documentation for what is required.
   */
    if (header_simple_table(vp,name,length,exact,var_len,write_method, MAX_PCONTROL_ENTRY)
                                                == MATCH_FAILED )
    return NULL;

    /* 
   * this is where we do the value assignments for the mib results.
   */
    switch(vp->magic) {
    case PCONTROLCLIENTINDEX:
        return NULL;
    case PCONTROLENTRYENABLE:
        *write_method = write_pControlEntryEnable;
        index = name[*length-1]-1;  
        tmpval = 1;        
        if(index < pc_list_count) 
            tmpval = pc_list_info[index].enable;
        *var_len = sizeof( long );
        return ( u_char * ) &tmpval;
    case PCONTROLCLIENTNAME:
        *write_method = write_pControlClientName;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].device_name);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLCLIENTMAC:
        *write_method = write_pControlClientMac;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)              
            ether_atoe(pc_list_info[index].mac, tmpstr);
        *var_len = 6;
        return ( u_char * )tmpstr;
    case PCONTROLSUNALLOWTIME:
        *write_method = write_pControlSunAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].sunday);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLMONALLOWTIME:
        *write_method = write_pControlMonAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].monday);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLTUEALLOWTIME:
        *write_method = write_pControlTueAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].tuesday);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLWEDALLOWTIME:
        *write_method = write_pControlWedAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].wednesday);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLTHUALLOWTIME:
        *write_method = write_pControlThuAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].thursday);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLFRIALLOWTIME:
        *write_method = write_pControlFriAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].friday);
        *var_len = strlen(tmpstr);
        return ( u_char * )tmpstr;
    case PCONTROLSATALLOWTIME:
        *write_method = write_pControlSatAllowTime;
        index = name[*length-1]-1;
        memset(tmpstr, 0x0, SPRINT_MAX_LEN);   
        if(index < pc_list_count)  
            strcpy(tmpstr,pc_list_info[index].saturday);
        *var_len = strlen(tmpstr);
        pc_list_flag = 0;        
        return ( u_char * )tmpstr;
    case PCONTROLCLIENTDELENTRY:
        *write_method = write_pControlClientDelEntry;
        tmpval = 2;
        *var_len = sizeof( long );
        return ( u_char * ) &tmpval;
    default:
      ERROR_MSG("");
    }
    return NULL;
}
Ejemplo n.º 24
0
void init_syspara(void)
{
	unsigned char buffer[16];
	unsigned int *src;
	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);

	/* /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 (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);
	}

	if (!ralink_mssid_mac_validate(macaddr) || !ralink_mssid_mac_validate(macaddr2))
		nvram_set("wl_mssid", "0");
	else
		nvram_set("wl_mssid", "1");

	//TODO: separate for different chipset solution
	nvram_set("et0macaddr", macaddr);
	nvram_set("et1macaddr", macaddr2);
	

	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. */
	dst = 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
	{
		if ((unsigned char)country_code[0]!=0xff)
		{ 
		   //for specific power
		        if (country_code[0] ==0x5a  && country_code[1] == 0x31)
			{
			   country_code[0]='U';
			   country_code[1]='S';
			}   
			else if (country_code[0] ==0x5a  && country_code[1] == 0x32)
			{
			   country_code[0]='G';
			   country_code[1]='B';
			}   
			else if (country_code[0] ==0x5a  && country_code[1] == 0x33)
			{
			   country_code[0]='T';
			   country_code[1]='W';
			}   
			else if (country_code[0] ==0x5a  && country_code[1] == 0x34)
			{
			   country_code[0]='C';
			   country_code[1]='N';
			}   
			   
			nvram_set("wl_country_code", country_code);
			nvram_set("wl0_country_code", country_code);
			nvram_set("wl1_country_code", country_code);
		}
		else
		{
			nvram_set("wl_country_code", "DB");
			nvram_set("wl0_country_code", "DB");
			nvram_set("wl1_country_code", "DB");
		}

		if (!strcasecmp(nvram_safe_get("wl_country_code"), "BR"))
		{
			nvram_set("wl_country_code", "UZ");
			nvram_set("wl0_country_code", "UZ");
			nvram_set("wl1_country_code", "UZ");
		}

		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");
	}

	src = (unsigned int*) 0x50020;	/* /dev/mtd/3, firmware, starts from 0x50000 */
	dst = buffer;
	bytes = 16;
	if (FRead(dst, (int)src, bytes)<0)
	{
		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 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))
		{
			nvram_set("odmpid", modelname);
		}
		else
#endif
			nvram_unset("odmpid");
	}

	nvram_set("firmver", rt_version);
	nvram_set("productid", rt_buildname);
}
Ejemplo n.º 25
0
/*
 * Name        : wpsenr_wksp_mainloop
 * Description : Main loop point for the WPS stack
 * Arguments   : wpsenr_param_t *param - argument set
 * Return type : int
 */
static wpssta_wksp_t *
wpssta_init(char *ifname)
{
	wpssta_wksp_t *sta_wksp = NULL;
	int pbc = WPS_UI_PBC_SW;
	char start_ok = false;
	wps_ap_list_info_t *wpsaplist;
	char scan = false;
	char *val, *next;
	char op[6] = {0};
	int oob = 0;
	int i, imax;
	int wps_action;
	char *env_ssid = NULL;
	char *env_sec = NULL;
	char *env_bssid = NULL;
	char *env_pin = NULL;
#ifdef __CONFIG_WFI__
	char *ui_env_pin = NULL;
#endif /* __CONFIG_WFI__ */
	char tmp[100];
	char *wlnames, name[256];


	TUTRACE((TUTRACE_INFO, "*********************************************\n"));
	TUTRACE((TUTRACE_INFO, "WPS - Enrollee App Broacom Corp.\n"));
	TUTRACE((TUTRACE_INFO, "Version: %s\n", MOD_VERSION_STR));
	TUTRACE((TUTRACE_INFO, "*********************************************\n"));

	/* we need to specify the if name before anything else */
	if (!ifname) {
		TUTRACE((TUTRACE_INFO, "no ifname exist!! return\n"));
		return 0;
	}

	/* WSC 2.0,  support WPS V2 or not */
	if (strcmp(wps_safe_get_conf("wps_version2"), "enabled") == 0)
		b_wps_version2 = true;

	wps_set_ifname(ifname);
	wps_osl_set_ifname(ifname);

	/* reset assoc_state in INIT state */
	assoc_state = WPS_ASSOC_STATE_INIT;

	/* reset enroll_again */
	enroll_again = false;

	/* Check whether scan needed */
	val = wps_ui_get_env("wps_enr_scan");
	if (val)
		scan = atoi(val);

	/* if scan requested : display and exit */
	if (scan) {
		/* do scan and wait the scan results */
		do_wps_scan();
		while (get_wps_scan_results() == NULL)
			WpsSleep(1);

		/* use scan result to create ap list */
		wpsaplist = create_aplist();
		if (wpsaplist) {
			wpssta_display_aplist(wpsaplist);
			wps_get_aplist(wpsaplist, wpsaplist);

			TUTRACE((TUTRACE_INFO, "WPS Enabled AP list :\n"));
			wpssta_display_aplist(wpsaplist);
		}
		goto exit;
	}

	/* init workspace */
	if ((sta_wksp = (wpssta_wksp_t *)alloc_init(sizeof(wpssta_wksp_t))) == NULL) {
		TUTRACE((TUTRACE_INFO, "Can not allocate memory for wps workspace...\n"));
		return NULL;
	}
	memset(sta_wksp, 0, sizeof(wpssta_wksp_t));
	wps_action = atoi(wps_ui_get_env("wps_action"));

	/* Setup STA action */
	if (wps_action == WPS_UI_ACT_STA_CONFIGAP || wps_action == WPS_UI_ACT_STA_GETAPCONFIG) {
		sta_wksp->mode = WPSM_STA_BUILTINREG;
		if (wps_action == WPS_UI_ACT_STA_CONFIGAP)
			sta_wksp->configap = true;
	}
	else
		sta_wksp->mode = WPSM_STA_ENROLL;

	val = wps_ui_get_env("wps_pbc_method");
	if (val)
		pbc = atoi(val);


	/* Save maximum instance number, and probe if any wl interface */
	imax = wps_get_ess_num();
	for (i = 0; i < imax; i++) {
		sprintf(tmp, "ess%d_wlnames", i);
		wlnames = wps_safe_get_conf(tmp);

		foreach(name, wlnames, next) {
			if (!strcmp(name, ifname)) {
				sta_wksp->ess_id = i;
				goto found;
			}
		}
	}
	goto exit;

found:
	/* Retrieve ENV */
	if (pbc ==  WPS_UI_PBC_HW) {
		strcat(op, "pb");
	}
	else {
		/* SW PBC */
		if (atoi(wps_ui_get_env("wps_method")) == WPS_UI_METHOD_PBC) {
			strcat(op, "pb");
		}
		else { /* PIN */
			strcat(op, "pin");
			env_pin = wps_get_conf("wps_device_pin");
			
			env_sec = wps_ui_get_env("wps_enr_wsec");
			if (env_sec[0] != 0) {
				wsec = atoi(env_sec);
			}

			env_ssid = wps_ui_get_env("wps_enr_ssid");
			if (env_ssid[0] == 0) {
				TUTRACE((TUTRACE_ERR, "\n\nPlease specify ssid or use pbc method\n\n"));
				goto exit;
			}
			wps_strncpy(ssid, env_ssid, sizeof(ssid));

			env_bssid = wps_ui_get_env("wps_enr_bssid");
			if (env_bssid[0] == 0) {
				/*
				 * WARNING : this "bssid" is used only to create an 802.1X socket.
				 *
				 * Normally, it should be the bssid of the AP we will associate to.
				 *
				 * Setting this manually means that we might be proceeding to
				 * eapol exchange with a different AP than the one we are associated to,
				 * which might work ... or not.
				 *
				 * When implementing an application, one might want to enforce association
				 * with the AP with that particular BSSID. In case of multiple AP
				 * on the ESS, this might not be stable with roaming enabled.
				 */
				ether_atoe(env_bssid, bssid);
			}
#ifdef __CONFIG_WFI__
			/* For WiFi-Invite session PIN */
			ui_env_pin = wps_ui_get_env("wps_device_pin");
			if (ui_env_pin[0] != '\0')
				env_pin = ui_env_pin;
#endif /* __CONFIG_WFI__ */

			if (sta_wksp->mode == WPSM_STA_BUILTINREG) {
				env_pin = wps_ui_get_env("wps_stareg_ap_pin");
				if (wps_validate_pin(env_pin) == FALSE) {
					TUTRACE((TUTRACE_INFO, "Not a valid PIN [%s]\n",
						env_pin ? (char *)env_pin : ""));
					goto exit;
				}

				sprintf(tmp, "ess%d_wps_oob", sta_wksp->ess_id);
				val = wps_ui_get_env(tmp);
				if (strcmp(val, "enabled") == 0)
					oob = 1;
			}

			/* If we want to get AP config and the AP is unconfigured,
			 * configure the AP directly
			*/
			if (sta_wksp->mode == WPSM_STA_BUILTINREG && sta_wksp->configap == false) {
				val = wps_ui_get_env("wps_scstate");
				if (strcmp(val, "unconfigured") == 0) {
					sta_wksp->configap = true;
					TUTRACE((TUTRACE_INFO, "AP-%s is unconfigure, "
						"using our security settings to configre it.\n"));
				}
			}
		}
	}

	TUTRACE((TUTRACE_INFO,
		"pbc = %s, wpsenr param: ifname = %s, mode= %s, op = %s, sec = %s, "
		"ssid = %s, bssid = %s, pin = %s, oob = %s\n",
		(pbc == 1? "HW_PBC": "SW_PBC"),
		ifname,
		(sta_wksp->mode == WPSM_STA_BUILTINREG) ? "STA_REG" : "STA_ENR",
		op,
		(env_sec? (char *)env_sec : "NULL"),
		(env_ssid? (char *)env_ssid : "NULL"),
		(env_bssid? (char *)env_bssid : "NULL"),
		(env_pin? (char *)env_pin : "NULL"),
		(oob == 1? "Enabled": "Disabled")));

	/*
	 * setup device configuration for WPS
	 * needs to be done before eventual scan for PBC.
	 */
	if (sta_wksp->mode == WPSM_STA_BUILTINREG) {
		if (wpssta_reg_config_init(sta_wksp, ifname, bssid, oob) != WPS_SUCCESS) {
			TUTRACE((TUTRACE_ERR, "wpssta_reg_config_init failed, exit.\n"));
			goto exit;
		}
	}
	else {
		if (wpssta_enr_config_init() != WPS_SUCCESS) {
			TUTRACE((TUTRACE_ERR, "wpssta_enr_config_init failed, exit.\n"));
			goto exit;
		}
	}

	/* if ssid specified, use it */
	if (!strcmp(op, "pin")) {
		pin = env_pin;
		if (!pin) {
			pin = def_pin;
			TUTRACE((TUTRACE_ERR,
				"\n\nStation Pin not specified, use default Pin %s\n\n",
				def_pin));
		}
		start_ok = true;
		/* WSC 2.0,  Test Plan 5.1.1 step 8 must add wps ie to probe request */
		if (b_wps_version2)
			add_wps_ie(NULL, 0, 0, b_wps_version2);
	}
	else {
		pin = NULL;
		wpsenr_osl_proc_states(WPS_FIND_PBC_AP);

		/* add wps ie to probe  */
		add_wps_ie(NULL, 0, TRUE, b_wps_version2);
		do_wps_scan();
		assoc_state_time = get_current_time();
		assoc_state = WPS_ASSOC_STATE_SCANNING;

		start_ok = false;
	}

	/* start WPS two minutes period at Finding a PBC AP or Associating with AP */
	start_time = get_current_time();

	if (start_ok) {
		/* clear current security setting */
		wpsenr_osl_clear_wsec();

		/*
		 * join. If user_bssid is specified, it might not
		 * match the actual associated AP.
		 * An implementation might want to make sure
		 * it associates to the same bssid.
		 * There might be problems with roaming.
		 */
		wpssta_do_join(false);
		return sta_wksp;
	}
	else if (assoc_state == WPS_ASSOC_STATE_SCANNING) {
		return sta_wksp;
	}

exit:
	wpssta_deinit(sta_wksp);
	return NULL;
}
Ejemplo n.º 26
0
Archivo: wpa.c Proyecto: rogerhu/dd-wrt
void start_nas_notify(char *ifname)
{
	char *argv[] = { "nas4not", "lan", ifname, "up",
		NULL,		/* role */
		NULL,		/* crypto */
		NULL,		/* auth */
		NULL,		/* passphrase */
		NULL,		/* ssid */
		NULL
	};
	char *str = NULL;
	char tmp[100], prefix[] = "wlXXXXXXXXXX_", pidfile[] = "/tmp/nas.wlXXXXXXXlan.pid";
	int unit;
	char remote[ETHER_ADDR_LEN];
	char ssid[48], pass[80], auth[16], crypto[16], role[8];
	int i;

	/*
	 * the wireless interface must be configured to run NAS 
	 */
	wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit));
	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
	snprintf(pidfile, sizeof(pidfile), "/tmp/nas.wl%dlan.pid", unit);

	if (!(str = file2str(pidfile)))	// no pidfile means no nas was run (required)
	{
		return;
	}
	free(str);
	sleep(3);
	/*
	 * find WDS link configuration 
	 */
	wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN);
	for (i = 0; i < MAX_NVPARSE; i++) {
		char mac[ETHER_ADDR_STR_LEN];
		uint8 ea[ETHER_ADDR_LEN];

		if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass)
		    && ether_atoe(mac, ea)
		    && !bcmp(ea, remote, ETHER_ADDR_LEN)) {
			argv[4] = role;
			argv[5] = crypto;
			argv[6] = auth;
			argv[7] = pass;
			argv[8] = ssid;
			break;
		}
	}

	/*
	 * did not find WDS link configuration, use wireless' 
	 */
	if (i == MAX_NVPARSE) {
		/*
		 * role 
		 */
		argv[4] = "auto";
		/*
		 * crypto 
		 */
		argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
		/*
		 * auth mode 
		 */
		argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
		/*
		 * passphrase 
		 */
		argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
		/*
		 * ssid 
		 */
		argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp));
	}
	int pid;

	_evalpid(argv, ">/dev/console", 0, &pid);
}
Ejemplo n.º 27
0
void start_sysinit(void)
{
	time_t tm = 0;

	eval("/bin/tar", "-xzf", "/dev/mtdblock/3", "-C", "/");
	FILE *in = fopen("/tmp/nvram/nvram.db", "rb");

	if (in != NULL) {
		fclose(in);
		eval("/usr/sbin/convertnvram");
		eval("/sbin/mtd", "erase", "nvram");
		nvram_commit();
	}
	if (!nvram_match("disable_watchdog", "1"))
		eval("watchdog");
	/*
	 * Setup console 
	 */

	cprintf("sysinit() klogctl\n");
	klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
	cprintf("sysinit() get router\n");

	/*
	 * network drivers 
	 */
#ifdef HAVE_HOTPLUG2
	insmod("ar231x");
#else
	insmod("ar2313");
#endif
	detect_wireless_devices();
	int s;
	struct ifreq ifr;
	if (getRouterBrand() == ROUTER_BOARD_RDAT81) {
		writeproc("/proc/sys/dev/wifi0/ledpin","7");
		writeproc("/proc/sys/dev/wifi0/softled","1");
		writeproc("/proc/sys/dev/wifi1/ledpin","5");
		writeproc("/proc/sys/dev/wifi1/softled","1");
	}
	if (getRouterBrand() == ROUTER_BOARD_RCAA01) {
		insmod("mvswitch");
//		eval("ifconfig", "eth0", "up", "promisc");	// required for vlan config
		eval("/sbin/vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
		eval("/sbin/vconfig", "add", "eth0", "0");
		eval("/sbin/vconfig", "add", "eth0", "1");

		if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
			char eabuf[32];

			strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
			ioctl(s, SIOCGIFHWADDR, &ifr);
			char macaddr[32];

			strcpy(macaddr,
			       ether_etoa((unsigned char *)ifr.ifr_hwaddr.
					  sa_data, eabuf));
			nvram_set("et0macaddr", macaddr);
//          MAC_ADD( macaddr );
			ether_atoe(macaddr,
				   (unsigned char *)ifr.ifr_hwaddr.sa_data);
			strncpy(ifr.ifr_name, "vlan1", IFNAMSIZ);
			ioctl(s, SIOCSIFHWADDR, &ifr);
			close(s);
		}
		writeproc("/proc/sys/dev/wifi0/ledpin","4");
		writeproc("/proc/sys/dev/wifi0/softled","1");
		writeproc("/proc/sys/dev/wifi1/ledpin","5");
		writeproc("/proc/sys/dev/wifi1/softled","1");
	}
	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
		char eabuf[32];

		strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
		ioctl(s, SIOCGIFHWADDR, &ifr);
		char macaddr[32];

		strcpy(macaddr,
		       ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data,
				  eabuf));
		nvram_set("et0macaddr", macaddr);
		nvram_set("et0macaddr_safe", macaddr);
		close(s);
	}

	/*
	 * Set a sane date 
	 */
	stime(&tm);
	nvram_set("wl0_ifname", "ath0");

	return;
	cprintf("done\n");
}
Ejemplo n.º 28
0
/*
 * var_pControlScanTable():
 *   Handle this table separately from the scalar value case.
 *   The workings of this are basically the same as for var_parentalControl above.
 */
unsigned char *
var_pControlScanTable(struct variable *vp,
    	    oid     *name,
    	    size_t  *length,
    	    int     exact,
    	    size_t  *var_len,
    	    WriteMethod **write_method)
{
    /* variables we may use later */
    static long index = 0;

    /* 
   * This assumes that the table is a 'simple' table.
   *	See the implementation documentation for the meaning of this.
   *	You will need to provide the correct value for the TABLE_SIZE parameter
   *
   * If this table does not meet the requirements for a simple table,
   *	you will need to provide the replacement code yourself.
   *	Mib2c is not smart enough to write this for you.
   *    Again, see the implementation documentation for what is required.
   */
    if (header_simple_table(vp,name,length,exact,var_len,write_method, MAX_CLIENT_LIST)
                                                == MATCH_FAILED )
    return NULL;

    /* 
   * this is where we do the value assignments for the mib results.
   */
    switch(vp->magic) {
    case PCONTROLSCANINDEX:
        return NULL;
    case PCONTROLSCANIPADDR:
        index = name[*length-1]-1;
        if(index < scanclient_count)
        {
            memset(tmpstr, 0x0, SPRINT_MAX_LEN);
            sprintf(tmpstr, "%d.%d.%d.%d", scanclient_list[index].ip_addr[0],
                        scanclient_list[index].ip_addr[1],
                        scanclient_list[index].ip_addr[2],
                        scanclient_list[index].ip_addr[3]);

            tmpval_u = inet_addr(tmpstr);
            *var_len = sizeof( unsigned long );
            return ( u_char * ) &tmpval_u;          
        }   
        return NULL;        
    case PCONTROLSCANCLIENTNAME:
        index = name[*length-1]-1;
        if(index < scanclient_count)
        {
            memset(tmpstr, 0x0, SPRINT_MAX_LEN);
            sprintf(tmpstr, "%s", scanclient_list[index].device_name);
            *var_len = strlen(tmpstr);
            return ( u_char * )tmpstr;          
        }   
        return NULL;
    case PCONTROLSCANCLIENTMAC:
        index = name[*length-1]-1;
        if(index < scanclient_count)
        {    
            char macstr[18];

            memset(macstr, 0x0, 18);
            memset(tmpstr, 0x0, SPRINT_MAX_LEN);
            sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", scanclient_list[index].mac_addr[0],
                                scanclient_list[index].mac_addr[1],
                                scanclient_list[index].mac_addr[2],
                                scanclient_list[index].mac_addr[3],
                                scanclient_list[index].mac_addr[4],
                                scanclient_list[index].mac_addr[5]);
            ether_atoe(macstr, tmpstr);
            *var_len = 6;
            return ( u_char * )tmpstr;
        }
        return NULL;
    default:
      ERROR_MSG("");
    }
    return NULL;
}
Ejemplo n.º 29
0
void vlan_init(int numports)
{
	int phyUnit;
	UINT16 phyHwStatus;
	UINT16 timeout;
	int liveLinks = 0;
	UINT32 phyBase = 0;
	BOOL foundPhy = FALSE;
	UINT32 phyAddr;
	UINT32 reg = 0;

	/*
	 * Reset PHYs 
	 */
	for (phyUnit = 0; phyUnit < ADM_PHY_MAX; phyUnit++) {
		if (!ADM_IS_ETHUNIT(phyUnit, 0)) {
			continue;
		}

		phyAddr = ADM_PHYADDR(phyUnit);

		setPhy(phyAddr, ADM_PHY_CONTROL, ADM_CTRL_SOFTWARE_RESET);
	}
	/*
	 * After the phy is reset, it takes a little while before
	 * it can respond properly.
	 */
	sleep(1);
	/*
	 * Verify that the switch is what we think it is, and that it's ready 
	 */
	adm_verifyReady(0);

	/*
	 * LAN SETTING: enable Auto-MDIX 
	 */
	phyAddr = ADM_SW_PHY_PORT0_REG / ADM_PHY_BASE_REG_NUM;
	reg = getPhy(phyAddr, ADM_SW_PHY_PORT0_REG);
	reg |= ADM_SW_AUTO_MDIX_EN;
	setPhy(phyAddr, ADM_SW_PHY_PORT0_REG, reg);

	phyAddr = ADM_SW_PHY_PORT1_REG / ADM_PHY_BASE_REG_NUM;
	reg = getPhy(phyAddr, ADM_SW_PHY_PORT1_REG);
	reg |= ADM_SW_AUTO_MDIX_EN;
	setPhy(phyAddr, ADM_SW_PHY_PORT1_REG, reg);

	phyAddr = ADM_SW_PHY_PORT2_REG / ADM_PHY_BASE_REG_NUM;
	getPhy(phyAddr, ADM_SW_PHY_PORT2_REG);
	reg |= ADM_SW_AUTO_MDIX_EN;
	setPhy(phyAddr, ADM_SW_PHY_PORT2_REG, reg);

	phyAddr = ADM_SW_PHY_PORT3_REG / ADM_PHY_BASE_REG_NUM;
	reg = getPhy(phyAddr, ADM_SW_PHY_PORT3_REG);
	reg |= ADM_SW_AUTO_MDIX_EN;
	setPhy(phyAddr, ADM_SW_PHY_PORT3_REG, reg);

	phyAddr = ADM_SW_PHY_PORT4_REG / ADM_PHY_BASE_REG_NUM;
	reg = getPhy(phyAddr, ADM_SW_PHY_PORT4_REG);
	reg |= ADM_SW_AUTO_MDIX_EN;
	setPhy(phyAddr, ADM_SW_PHY_PORT4_REG, reg);

	phyAddr = ADM_SW_PHY_PORT5_REG / ADM_PHY_BASE_REG_NUM;
	reg = getPhy(phyAddr, ADM_SW_PHY_PORT5_REG);
	reg |= ADM_SW_AUTO_MDIX_EN;
	setPhy(phyAddr, ADM_SW_PHY_PORT5_REG, reg);

	/*
	 * See if there's any configuration data for this enet 
	 */
	for (phyUnit = 0; phyUnit < ADM_PHY_MAX; phyUnit++) {
		if (ADM_ETHUNIT(phyUnit) != 0) {
			continue;
		}

		foundPhy = TRUE;
		break;
	}

	if (!foundPhy) {
		return FALSE;	/* No PHY's configured for this ethUnit */
	}

	/*
	 * start auto negogiation on each phy 
	 */
	for (phyUnit = 0; phyUnit < ADM_PHY_MAX; phyUnit++) {
		if (!ADM_IS_ETHUNIT(phyUnit, 0)) {
			continue;
		}
		phyAddr = ADM_PHYADDR(phyUnit);

		setPhy(phyAddr, ADM_AUTONEG_ADVERT, ADM_ADVERTISE_ALL);

		setPhy(phyAddr, ADM_PHY_CONTROL,
		       ADM_CTRL_AUTONEGOTIATION_ENABLE |
		       ADM_CTRL_START_AUTONEGOTIATION);
	}

	/*
	 * Wait up to .75 seconds for ALL associated PHYs to finish
	 * autonegotiation.  The only way we get out of here sooner is
	 * if ALL PHYs are connected AND finish autonegotiation.
	 */
	timeout = 15;
	for (phyUnit = 0; (phyUnit < ADM_PHY_MAX) /* && (timeout > 0) */ ;
	     phyUnit++) {
		if (!ADM_IS_ETHUNIT(phyUnit, 0)) {
			continue;
		}
		for (;;) {
			phyAddr = ADM_PHYADDR(phyUnit);

			phyHwStatus = getPhy(phyAddr, ADM_PHY_STATUS);

			if (ADM_AUTONEG_DONE(phyHwStatus)) {
				fprintf(stderr,
					"Port %d, Negotiation Success\n",
					phyUnit);
				break;
			}
			if (timeout == 0) {
				fprintf(stderr,
					"Port %d, Negotiation timeout\n",
					phyUnit);
				break;
			}
			if (--timeout == 0) {
				fprintf(stderr,
					"Port %d, Negotiation timeout\n",
					phyUnit);
				break;
			}

			usleep(75);
		}
	}

	/*
	 * All PHYs have had adequate time to autonegotiate.
	 * Now initialize software status.
	 *
	 * It's possible that some ports may take a bit longer
	 * to autonegotiate; but we can't wait forever.  They'll
	 * get noticed by mv_phyCheckStatusChange during regular
	 * polling activities.
	 */
	for (phyUnit = 0; phyUnit < ADM_PHY_MAX; phyUnit++) {

		if (adm_phyIsLinkAlive(phyUnit)) {
			liveLinks++;
			ADM_IS_PHY_ALIVE(phyUnit) = TRUE;
		} else {
			ADM_IS_PHY_ALIVE(phyUnit) = FALSE;
		}

		fprintf(stderr, "adm_phySetup: eth%d phy%d: Phy Status=%4.4x\n",
			0, phyUnit, getPhy(ADM_PHYADDR(phyUnit),
					   ADM_PHY_STATUS));
	}

	config_vlan();
	eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
	eval("vconfig", "add", "eth0", "1");
	eval("vconfig", "add", "eth0", "2");
	struct ifreq ifr;
	int s;

	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
		char eabuf[32];

		strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
		ioctl(s, SIOCGIFHWADDR, &ifr);
		char macaddr[32];

		strcpy(macaddr,
		       ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data,
				  eabuf));
		nvram_set("et0macaddr", macaddr);
		MAC_ADD(macaddr);
		ether_atoe(macaddr, (unsigned char *)ifr.ifr_hwaddr.sa_data);
		strncpy(ifr.ifr_name, "vlan2", IFNAMSIZ);
		ioctl(s, SIOCSIFHWADDR, &ifr);
		close(s);
	}

}
Ejemplo n.º 30
0
void setup_bcom(int skfd, char *ifname)
{
	int val = 0;
	char buf[8192];
	char wbuf[80];
	char *v;
	
	if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
		return;
	
	nvram_set(wl_var("ifname"), ifname);
	
	stop_bcom(skfd, ifname);

	/* Set Country */
	strncpy(buf, nvram_safe_get(wl_var("country_code")), 4);
	buf[3] = 0;
	bcom_ioctl(skfd, ifname, WLC_SET_COUNTRY, buf, 4);
	
	/* Set up afterburner */
	val = ABO_AUTO;
	if (nvram_enabled(wl_var("afterburner")))
		val = ABO_ON;
	if (nvram_disabled(wl_var("afterburner")))
		val = ABO_OFF;
	bcom_set_val(skfd, ifname, "afterburner_override", &val, sizeof(val));
	
	/* Set other options */
	val = nvram_enabled(wl_var("lazywds"));
	bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
	
	if (v = nvram_get(wl_var("frag"))) {
		val = atoi(v);
		bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
	}
	if (v = nvram_get(wl_var("dtim"))) {
		val = atoi(v);
		bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
	}
	if (v = nvram_get(wl_var("bcn"))) {
		val = atoi(v);
		bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
	}
	if (v = nvram_get(wl_var("rts"))) {
		val = atoi(v);
		bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
	}
	if (v = nvram_get(wl_var("antdiv"))) {
		val = atoi(v);
		bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
	}
	if (v = nvram_get(wl_var("txant"))) {
		val = atoi(v);
		bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
	}
	
	val = nvram_enabled(wl_var("closed"));
	bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));

	val = nvram_enabled(wl_var("ap_isolate"));
	bcom_set_int(skfd, ifname, "ap_isolate", val);

	val = nvram_enabled(wl_var("frameburst"));
	bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));

	/* Set up MAC list */
	if (nvram_match(wl_var("macmode"), "allow"))
		val = WLC_MACMODE_ALLOW;
	else if (nvram_match(wl_var("macmode"), "deny"))
		val = WLC_MACMODE_DENY;
	else
		val = WLC_MACMODE_DISABLED;

	if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
		struct maclist *mac_list;
		struct ether_addr *addr;
		char *next;
		
		memset(buf, 0, 8192);
		mac_list = (struct maclist *) buf;
		addr = mac_list->ea;
		
		foreach(wbuf, v, next) {
			if (ether_atoe(wbuf, addr->ether_addr_octet)) {
				mac_list->count++;
				addr++;
			}
		}
		bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
	} else {