File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / vap.c
Revision 1.1.2.6: download - view: text, annotated - select for diffs - revision graph
Thu Oct 28 15:45:27 2010 UTC (13 years, 8 months ago) by misho
Branches: tools1_0
added script support

    1: #include "global.h"
    2: #include "dwds.h"
    3: 
    4: 
    5: static inline int
    6: wifi_getParent(const char *csVAP, char *psParent, size_t plen)
    7: {
    8: 	char szOID[STRSIZ] = { 0 };
    9: 
   10: 	FTRACE(5);
   11: 
   12: 	assert(csVAP);
   13: 	assert(psParent);
   14: 
   15: 	memset(psParent, 0, plen);
   16: 	snprintf(szOID, STRSIZ, "net.wlan.%s.%%parent", csVAP + 4);
   17: 	if (sysctlbyname(szOID, psParent, &plen, NULL, 0) == -1) {
   18: //		syslog(LOG_ERR, "Error:: can`t get parent #%d - %s\n", errno, strerror(errno));
   19: 		return -1;
   20: 	} else
   21: 		psParent[plen] = 0;
   22: 
   23: 	return 0;
   24: }
   25: 
   26: inline int
   27: wifi_chkIface(const char *csVAP, char **ppsIF, int nIF)
   28: {
   29: 	char szParent[IFNAMSIZ];
   30: 	register int i;
   31: 
   32: 	FTRACE(5);
   33: 
   34: 	assert(csVAP);
   35: 
   36: 	if (wifi_getParent(csVAP, szParent, IFNAMSIZ) == -1)
   37: 		return -1;
   38: 
   39: 	for (i = 0; i < nIF; i++)
   40: 		if (!strcasecmp(ppsIF[i], "any") || !strcmp(ppsIF[i], szParent))
   41: 			return 1; /* OK, vap is child */
   42: 
   43: 	syslog(LOG_ERR, "Error:: Interface %s parent %s not being monitored", csVAP, szParent);
   44: 	return 0;
   45: }
   46: 
   47: static inline int
   48: wifi_isWDS(int fd, const char *csVAP)
   49: {
   50: 	struct ifmediareq ifmr;
   51: 
   52: 	FTRACE(5);
   53: 
   54: 	assert(csVAP);
   55: 
   56: 	memset(&ifmr, 0, sizeof ifmr);
   57: 	strlcpy(ifmr.ifm_name, csVAP, sizeof ifmr.ifm_name);
   58: 	if (ioctl(fd, SIOCGIFMEDIA, &ifmr) == -1) {
   59: 		syslog(LOG_ERR, "Error:: can`t get media for %s #%d - %s\n", csVAP, 
   60: 				errno, strerror(errno));
   61: 		return -1;
   62: 	}
   63: 
   64: 	return (ifmr.ifm_current & IFM_IEEE80211_WDS) != 0;
   65: }
   66: 
   67: static inline int
   68: wifi_getBSSID(int fd, const char *csVAP, uint8_t *psBSSID, int len)
   69: {
   70: 	struct ieee80211req ireq;
   71: 
   72: 	FTRACE(5);
   73: 
   74: 	assert(csVAP);
   75: 	assert(psBSSID);
   76: 
   77: 	memset(&ireq, 0, sizeof ireq);
   78: 	strlcpy(ireq.i_name, csVAP, sizeof ireq.i_name);
   79: 	ireq.i_type = IEEE80211_IOC_BSSID;
   80: 	ireq.i_data = psBSSID;
   81: 	ireq.i_len = len;
   82: 	if (ioctl(fd, SIOCG80211, &ireq) == -1) {
   83: 		syslog(LOG_ERR, "Error:: can`t get BSSID for %s #%d - %s\n", csVAP, 
   84: 				errno, strerror(errno));
   85: 		return -1;
   86: 	}
   87: 
   88: 	return 0;
   89: }
   90: 
   91: struct dwds_if *
   92: wifi_buildWDS(int fd, char **ppsIF, int nIF)
   93: {
   94: 	struct dwds_if *p, *wds = NULL;
   95: 	char szVAP[IFNAMSIZ];
   96: 	struct ether_addr bssid;
   97: 	register int i;
   98: 
   99: 	for (i = 0; i < 128; i++) {
  100: 		memset(szVAP, 0, IFNAMSIZ);
  101: 		snprintf(szVAP, IFNAMSIZ, "wlan%d", i);
  102: 		if (wifi_chkIface(szVAP, ppsIF, nIF) > 0 && wifi_isWDS(fd, szVAP) > 0) {
  103: 			p = malloc(sizeof(struct dwds_if));
  104: 			if (!p) {
  105: 				syslog(LOG_ERR, "Error:: can`t allocate memory #%d - %s\n", 
  106: 						errno, strerror(errno));
  107: 				i = -1;
  108: 				break;
  109: 			}
  110: 			strlcpy(p->if_name, szVAP, IFNAMSIZ);
  111: 			if (wifi_getBSSID(fd, szVAP, p->if_bssid, IEEE80211_ADDR_LEN) == -1) {
  112: 				free(p);
  113: 				i = -1;
  114: 				break;
  115: 			}
  116: 			p->if_next = wds;
  117: 			wds = p;
  118: 
  119: 			memcpy(&bssid, p->if_bssid, ETHER_ADDR_LEN);
  120: 			syslog(LOG_INFO, "BSSID:%s discover WDS vap %s\n", ether_ntoa(&bssid), szVAP);
  121: 		}
  122: 	}
  123: 
  124: 	if (i == -1)
  125: 		while ((p = wds)) {
  126: 			wds = wds->if_next;
  127: 			free(p);
  128: 		}
  129: 
  130: 	return wds;
  131: }
  132: 
  133: inline int
  134: wifi_destroyWDS(const char *csIface, struct dwds_if **wds)
  135: {
  136: 	struct dwds_if *p, **pp;
  137: 
  138: 	assert(csIface);
  139: 	assert(wds);
  140: 
  141: 	for (pp = wds; (p = *pp); pp = &p->if_next)
  142: 		if (!strncmp(p->if_name, csIface, IFNAMSIZ))
  143: 			break;
  144: 	if (p) {
  145: 		*pp = p->if_next;
  146: 		free(p);
  147: 		return 0;
  148: 	}
  149: 
  150: 	return 1;
  151: }
  152: 
  153: static int
  154: wifi_vapDestroy(const char *csVAP)
  155: {
  156: 	struct ieee80211req ifr;
  157: 	int s;
  158: 
  159: 	assert(csVAP);
  160: 
  161: 	s = socket(PF_INET, SOCK_DGRAM, 0);
  162: 	if (s == -1) {
  163: 		syslog(LOG_ERR, "Error:: destroy socket(vap) %m\n");
  164: 		return -1;
  165: 	}
  166: 
  167: 	memset(&ifr, 0, sizeof ifr);
  168: 	strlcpy(ifr.i_name, csVAP, IFNAMSIZ);
  169: 	if (ioctl(s, SIOCIFDESTROY, &ifr) == -1) {
  170: 		syslog(LOG_ERR, "Error:: destroy ioctl(vap) %m\n");
  171: 		close(s);
  172: 		return -1;
  173: 	} else
  174: 		close(s);
  175: 
  176: 	return 0;
  177: }
  178: 
  179: inline int
  180: wifi_leaveWDS(struct ether_addr bssid, struct dwds_if **wds)
  181: {
  182: 	struct dwds_if *p, **pp;
  183: 
  184: 	assert(wds);
  185: 
  186: 	for (pp = wds; (p = *pp); pp = &p->if_next)
  187: 		if (!memcmp(p->if_bssid, &bssid, IEEE80211_ADDR_LEN))
  188: 			break;
  189: 	if (p) {
  190: 		*pp = p->if_next;
  191: 		if (wifi_vapDestroy(p->if_name) != -1)
  192: 		       VERB(1) syslog(LOG_INFO, "BSSID:%s WDS VAP %s destroyed\n", 
  193: 				       ether_ntoa(&bssid), p->if_name);	
  194: 		free(p);
  195: 		return 0;
  196: 	}
  197: 
  198: 	return 1;
  199: }
  200: 
  201: static int
  202: wifi_vapCreate(const char *csIface, struct dwds_if *p)
  203: {
  204: 	struct ieee80211_clone_params cp;
  205: 	struct ifreq ifr;
  206: 	struct ether_addr bssid;
  207: 	int s;
  208: 
  209: 	assert(csIface);
  210: 	assert(p);
  211: 
  212: 	s = socket(PF_INET, SOCK_DGRAM, 0);
  213: 	if (s == -1) {
  214: 		syslog(LOG_ERR, "Error:: create socket(vap) %m\n");
  215: 		return -1;
  216: 	}
  217: 
  218: 	memset(&cp, 0, sizeof cp);
  219: 	strlcpy(cp.icp_parent, csIface, IFNAMSIZ);
  220: 	cp.icp_opmode = IEEE80211_M_WDS;
  221: 	memcpy(cp.icp_bssid, p->if_bssid, IEEE80211_ADDR_LEN);
  222: 
  223: 	memset(&ifr, 0, sizeof ifr);
  224: 	strlcpy(ifr.ifr_name, "wlan", IFNAMSIZ);
  225: 	ifr.ifr_data = (void *) &cp;
  226: 
  227: 	if (ioctl(s, SIOCIFCREATE2, &ifr) == -1) {
  228: 		memcpy(&bssid, cp.icp_bssid, IEEE80211_ADDR_LEN);
  229: 		syslog(LOG_ERR, "Error:: create ioctl(vap) mode %u flags 0x%x parent %s bssid %s %m\n", 
  230: 				cp.icp_opmode, cp.icp_flags, cp.icp_parent, ether_ntoa(&bssid));
  231: 		close(s);
  232: 		return -1;
  233: 	} else {
  234: 		strlcpy(p->if_name, ifr.ifr_name, IFNAMSIZ);
  235: 		close(s);
  236: 	}
  237: 
  238: 	return 0;
  239: }
  240: 
  241: inline int
  242: wifi_createWDS(const char *csIface, struct ether_addr bssid, struct dwds_if **wds)
  243: {
  244: 	struct dwds_if *p;
  245: 	char szParent[IFNAMSIZ];
  246: 
  247: 	assert(csIface);
  248: 	assert(wds);
  249: 
  250: 	for (p = *wds; p; p = p->if_next)
  251: 		if (!memcmp(p->if_bssid, &bssid, IEEE80211_ADDR_LEN)) {
  252: 			VERB(1) syslog(LOG_INFO, "BSSID:%s WDS VAP already created (%s)\n",
  253: 			    ether_ntoa(&bssid), csIface);
  254: 			return 1;
  255: 		}
  256: 	if (wifi_getParent(csIface, szParent, IFNAMSIZ) == -1) {
  257: 		syslog(LOG_ERR, "Error:: %s no pointer to parent interface\n", csIface);
  258: 		return -1;
  259: 	}
  260: 
  261: 	p = malloc(sizeof(struct dwds_if));
  262: 	if (!p) {
  263: 		syslog(LOG_ERR, "Error:: malloc failed: %m\n");
  264: 		return -1;
  265: 	} else
  266: 		memcpy(p->if_bssid, &bssid, IEEE80211_ADDR_LEN);
  267: 	if (wifi_vapCreate(csIface, p) == -1) {
  268: 		free(p);
  269: 		return -1;
  270: 	} else {
  271: 		p->if_next = *wds;
  272: 		*wds = p;
  273: 		VERB(1) syslog(LOG_INFO, "BSSID:%s create WDS VAP %s\n", ether_ntoa(&bssid), p->if_name);
  274: 	}
  275: 
  276: 	return 0;
  277: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>