C/drv_madwifing.c
/* Monitor, inject, and injmon are all the same method, make a new
 * mwng VAP */
Monitor, inject, and injmon are all the same method, make a new
mwng VAP
Monitor inject and injmon are all the same method make a new mwng VAP
int madwifing_openmon_cb(lorcon_t *context) {
	struct madwifi_vaps *mwvaps;
	char *parent;
	char pcaperr[PCAP_ERRBUF_SIZE];
	short flags;
	struct ifreq if_req;
	struct sockaddr_ll sa_ll;
	int optval;
	socklen_t optlen;
	if ((mwvaps = madwifing_list_vaps(context->ifname, context->errstr)) == NULL) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "drv_madwifing failed to find "
				 "information about %s", context->ifname);
		return -1;
	}
	if (strlen(context->vapname) == 0) {
		snprintf(context->vapname, MAX_IFNAME_LEN, "%smon", context->ifname);
	}
	if ((parent = madwifing_find_parent(mwvaps)) == NULL) {
		free(parent);
		madwifing_free_vaps(mwvaps);
		return -1;
	}
	if (madwifing_build_vap(parent, context->errstr, context->vapname,
							context->vapname, IEEE80211_M_MONITOR, 
							IEEE80211_CLONE_BSSID) < 0) { 
		free(parent);
		madwifing_free_vaps(mwvaps);
		return -1;
	}
	madwifing_free_vaps(mwvaps);
	free(parent);
	parent = NULL;
	if (ifconfig_delta_flags(context->vapname, context->errstr,
							 (IFF_UP | IFF_RUNNING | IFF_PROMISC)) < 0) {
		return -1;
	}
	if (madwifing_setdevtype(context->vapname, ARPHDR_RADIOTAP, 
							 context->errstr) != 0) {
		return -1;
	}
	pcaperr[0] = '\0';
	if ((context->pcap = pcap_open_live(context->vapname, LORCON_MAX_PACKET_LEN, 
										1, context->timeout_ms, pcaperr)) == NULL) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "%s", pcaperr);
		return -1;
	}
	context->capture_fd = pcap_get_selectable_fd(context->pcap);
	context->dlt = pcap_datalink(context->pcap);
	context->inject_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if (context->inject_fd < 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to create injection "
				 "socket: %s", strerror(errno));
		pcap_close(context->pcap);
		return -1;
	}
	memset(&if_req, 0, sizeof(if_req));
	memcpy(if_req.ifr_name, context->vapname, IFNAMSIZ);
	if_req.ifr_name[IFNAMSIZ - 1] = 0;
	if (ioctl(context->inject_fd, SIOCGIFINDEX, &if_req) < 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to get interface idex: %s",
				 strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}
	memset(&sa_ll, 0, sizeof(sa_ll));
	sa_ll.sll_family = AF_PACKET;
	sa_ll.sll_protocol = htons(ETH_P_ALL);
	sa_ll.sll_ifindex = if_req.ifr_ifindex;
	if (bind(context->inject_fd, (struct sockaddr *) &sa_ll, sizeof(sa_ll)) != 0) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to bind injection "
				 "socket: %s", strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}
	optlen = sizeof(optval);
	optval = 20;
	if (setsockopt(context->inject_fd, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
		snprintf(context->errstr, LORCON_STATUS_MAX, "failed to set priority on "
				 "injection socket: %s", strerror(errno));
		close(context->inject_fd);
		pcap_close(context->pcap);
		return -1;
	}
	return 1;
}
int madwifing_openmon_cb ( lorcon_t * context ) { struct madwifi_vaps * mwvaps ; char * parent ; char pcaperr [ PCAP_ERRBUF_SIZE ] ; short flags ; struct ifreq if_req ; struct sockaddr_ll sa_ll ; int optval ; socklen_t optlen ; if ( ( mwvaps = madwifing_list_vaps ( context -> ifname , context -> errstr ) ) == NULL ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " " " , context -> ifname ) ; return -1 ; } if ( strlen ( context -> vapname ) == 0 ) { snprintf ( context -> vapname , MAX_IFNAME_LEN , " " , context -> ifname ) ; } if ( ( parent = madwifing_find_parent ( mwvaps ) ) == NULL ) { free ( parent ) ; madwifing_free_vaps ( mwvaps ) ; return -1 ; } if ( madwifing_build_vap ( parent , context -> errstr , context -> vapname , context -> vapname , IEEE80211_M_MONITOR , IEEE80211_CLONE_BSSID ) < 0 ) { free ( parent ) ; madwifing_free_vaps ( mwvaps ) ; return -1 ; } madwifing_free_vaps ( mwvaps ) ; free ( parent ) ; parent = NULL ; if ( ifconfig_delta_flags ( context -> vapname , context -> errstr , ( IFF_UP | IFF_RUNNING | IFF_PROMISC ) ) < 0 ) { return -1 ; } if ( madwifing_setdevtype ( context -> vapname , ARPHDR_RADIOTAP , context -> errstr ) != 0 ) { return -1 ; } pcaperr [ 0 ] = ' \0 ' ; if ( ( context -> pcap = pcap_open_live ( context -> vapname , LORCON_MAX_PACKET_LEN , 1 , context -> timeout_ms , pcaperr ) ) == NULL ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " , pcaperr ) ; return -1 ; } context -> capture_fd = pcap_get_selectable_fd ( context -> pcap ) ; context -> dlt = pcap_datalink ( context -> pcap ) ; context -> inject_fd = socket ( PF_PACKET , SOCK_RAW , htons ( ETH_P_ALL ) ) ; if ( context -> inject_fd < 0 ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " " " , strerror ( errno ) ) ; pcap_close ( context -> pcap ) ; return -1 ; } memset ( & if_req , 0 , sizeof ( if_req ) ) ; memcpy ( if_req . ifr_name , context -> vapname , IFNAMSIZ ) ; if_req . ifr_name [ IFNAMSIZ - 1 ] = 0 ; if ( ioctl ( context -> inject_fd , SIOCGIFINDEX , & if_req ) < 0 ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " , strerror ( errno ) ) ; close ( context -> inject_fd ) ; pcap_close ( context -> pcap ) ; return -1 ; } memset ( & sa_ll , 0 , sizeof ( sa_ll ) ) ; sa_ll . sll_family = AF_PACKET ; sa_ll . sll_protocol = htons ( ETH_P_ALL ) ; sa_ll . sll_ifindex = if_req . ifr_ifindex ; if ( bind ( context -> inject_fd , ( struct sockaddr * ) & sa_ll , sizeof ( sa_ll ) ) != 0 ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " " " , strerror ( errno ) ) ; close ( context -> inject_fd ) ; pcap_close ( context -> pcap ) ; return -1 ; } optlen = sizeof ( optval ) ; optval = 20 ; if ( setsockopt ( context -> inject_fd , SOL_SOCKET , SO_PRIORITY , & optval , optlen ) ) { snprintf ( context -> errstr , LORCON_STATUS_MAX , " " " " , strerror ( errno ) ) ; close ( context -> inject_fd ) ; pcap_close ( context -> pcap ) ; return -1 ; } return 1 ; }
Monitor, inject, and injmon are all the same method, make a new
 mwng VAP
Monitor inject and injmon are all the same method make a new mwng VAP
// Assign a vapname based on the interface // Find the parent of whatever interface they specified (ought to be the  // parent already) // Make a vap // Set the VAP to radiotap