Ejemplo n.º 1
0
static int inline file_2_len(u32* len,int fd){
    if(read(fd,len,4) != 4){
        wave_error_printf("读取长度失败");
        return -1;
    }
    *len = be_to_host32(*len);
    return 0;
}
Ejemplo n.º 2
0
static int inline file_2_element(int fd,void* ele,int size){
    if(read(fd,ele,size) != size){
        wave_error_printf("读取长度失败");
        return -1;
    }
     switch(size){
        case 1:
            *(u8*)ele = *(u8*)ele;
            break;
        case 2:
            *(u16*)ele = be_to_host16( *(u16*)ele );
            break;
        case 4:
            *(u32*)ele = be_to_host32( *(u32*)ele );
            break;
        case 8:
            *(u64*)ele = be_to_host64( *(u64*)ele );
            break;
    }
    return 0;
}
Ejemplo n.º 3
0
static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf,
			size_t len)
{
	struct hostapd_data *hapd = ctx;
	const struct bootp_pkt *b;
	struct sta_info *sta;
	int exten_len;
	const u8 *end, *pos;
	int res, msgtype = 0, prefixlen = 32;
	u32 subnet_mask = 0;
	u16 tot_len;
	int dns_flag = 0;
	u32 dns1 = 0;
	u32 dns2 = 0;
	u8 *tmp;
	
	/*
	printPacketBuffer(buf, len);
	*/

	exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten));
	if (exten_len < 4)
		return;

	b = (const struct bootp_pkt *) &buf[ETH_HLEN];
	tot_len = ntohs(b->iph.tot_len);
	if (tot_len > (unsigned int) (len - ETH_HLEN))
		return;

	if (os_memcmp(b->exten, ic_bootp_cookie, ARRAY_SIZE(ic_bootp_cookie)))
		return;

	printf("dhcp_snoop: Found DHCP IPv4 address %s/%d",
		ipaddr_str(ntohl(b->your_ip)),
		prefixlen);

	/* Parse DHCP options */
	end = (const u8 *) b + tot_len;
	pos = &b->exten[4];
	while (pos < end && *pos != 0xff) {
		const u8 *opt = pos++;

		if (*opt == 0) /* padding */
			continue;

		pos += *pos + 1;
		if (pos >= end)
			break;

		switch (*opt) {
		case 1:  /* subnet mask */
			if (opt[1] == 4)
				subnet_mask = WPA_GET_BE32(&opt[2]);
			if (subnet_mask == 0)
				return;
			while (!(subnet_mask & 0x1)) {
				subnet_mask >>= 1;
				prefixlen--;
			}
			break;
		case 6: /* dns */
			dns_flag = 1;
			if (opt[1] == 4) {
				dns1 = WPA_GET_BE32(&opt[2]);
				tmp = (u8 *)&dns1;
				wpa_printf(MSG_DEBUG, "%d.%d.%d.%d\n", tmp[0], tmp[1], tmp[2], tmp[3]);
			}
			if (opt[1] == 8) {
				dns1 = WPA_GET_BE32(&opt[2]);
				dns2 = WPA_GET_BE32(&opt[6]);
			
				tmp = (u8 *)&dns1;
				wpa_printf(MSG_DEBUG, "dns1 %d.%d.%d.%d\n", tmp[0], tmp[1], tmp[2], tmp[3]);
				tmp = (u8 *)&dns2;
				wpa_printf(MSG_DEBUG, "dns2 %d.%d.%d.%d\n", tmp[0], tmp[1], tmp[2], tmp[3]);
			}
		case 53: /* message type */
			if (opt[1])
				msgtype = opt[2];
			break;
		default:
			break;
		}
	}

	if (msgtype == DHCPACK) {
		if (b->your_ip == 0)
			return;

		/* DHCPACK for DHCPREQUEST */
		sta = ap_get_sta(hapd, b->hw_addr);
		if (!sta)
			return;

		wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR
			   " @ IPv4 address %s/%d",
			   MAC2STR(sta->addr), ipaddr_str(ntohl(b->your_ip)),
			   prefixlen);

		if (sta->ipaddr == b->your_ip)
			return;

		sta->ipaddr = b->your_ip;
		if (dns_flag) {
			sta->dns[0] = dns1;
			sta->dns[1] = dns2;
		}

#if 0 /* temporarily close  */
		if (sta->ipaddr != 0) {
			wpa_printf(MSG_DEBUG,
				   "dhcp_snoop: Removing IPv4 address %s from the ip neigh table",
				   ipaddr_str(be_to_host32(sta->ipaddr)));
			hostapd_drv_br_delete_ip_neigh(hapd, 4,
						       (u8 *) &sta->ipaddr);
		}

		res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip,
						  prefixlen, sta->addr);
		if (res) {
			wpa_printf(MSG_DEBUG,
				   "dhcp_snoop: Adding ip neigh table failed: %d",
				   res);
			return;
		}
		sta->ipaddr = b->your_ip;
#endif
		send_msg_to_eag(hapd, sta, STA_ADD);
	}

	if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) {
		for (sta = hapd->sta_list; sta; sta = sta->next) {
			if (!(sta->flags & WLAN_STA_AUTHORIZED))
				continue;
			x_snoop_mcast_to_ucast_convert_send(hapd, sta,
							    (u8 *) buf, len);
		}
	}
}
Ejemplo n.º 4
0
static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
					  struct sta_info *sta,
					  int status_type)
{
	struct radius_msg *msg;
	char buf[128];
	u8 *val;
	size_t len;
	int i;
	struct wpabuf *b;

	msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
			     radius_client_get_id(hapd->radius));
	if (msg == NULL) {
		wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
		return NULL;
	}

	if (sta) {
		radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));

		if ((hapd->conf->wpa & 2) &&
		    !hapd->conf->disable_pmksa_caching &&
		    sta->eapol_sm && sta->eapol_sm->acct_multi_session_id_hi) {
			os_snprintf(buf, sizeof(buf), "%08X+%08X",
				    sta->eapol_sm->acct_multi_session_id_hi,
				    sta->eapol_sm->acct_multi_session_id_lo);
			if (!radius_msg_add_attr(
				    msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
				    (u8 *) buf, os_strlen(buf))) {
				wpa_printf(MSG_INFO,
					   "Could not add Acct-Multi-Session-Id");
				goto fail;
			}
		}
	} else {
		radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
	}

	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
				       status_type)) {
		wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
		goto fail;
	}

	if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
					    RADIUS_ATTR_ACCT_AUTHENTIC) &&
	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
				       hapd->conf->ieee802_1x ?
				       RADIUS_ACCT_AUTHENTIC_RADIUS :
				       RADIUS_ACCT_AUTHENTIC_LOCAL)) {
		wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
		goto fail;
	}

	if (sta) {
		/* Use 802.1X identity if available */
		val = ieee802_1x_get_identity(sta->eapol_sm, &len);

		/* Use RADIUS ACL identity if 802.1X provides no identity */
		if (!val && sta->identity) {
			val = (u8 *) sta->identity;
			len = os_strlen(sta->identity);
		}

		/* Use STA MAC if neither 802.1X nor RADIUS ACL provided
		 * identity */
		if (!val) {
			os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
				    MAC2STR(sta->addr));
			val = (u8 *) buf;
			len = os_strlen(buf);
		}

		if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
					 len)) {
			wpa_printf(MSG_INFO, "Could not add User-Name");
			goto fail;
		}
	}

	if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
				   msg) < 0)
		goto fail;

	if (sta) {
		for (i = 0; ; i++) {
			val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
							  i);
			if (val == NULL)
				break;

			if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
						 val, len)) {
				wpa_printf(MSG_INFO, "Could not add Class");
				goto fail;
			}
		}

		b = ieee802_1x_get_radius_cui(sta->eapol_sm);
		if (b &&
		    !radius_msg_add_attr(msg,
					 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
					 wpabuf_head(b), wpabuf_len(b))) {
			wpa_printf(MSG_ERROR, "Could not add CUI");
			goto fail;
		}

		if (!b && sta->radius_cui &&
		    !radius_msg_add_attr(msg,
					 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
					 (u8 *) sta->radius_cui,
					 os_strlen(sta->radius_cui))) {
			wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
			goto fail;
		}

		if (sta->ipaddr &&
		    !radius_msg_add_attr_int32(msg,
					       RADIUS_ATTR_FRAMED_IP_ADDRESS,
					       be_to_host32(sta->ipaddr))) {
			wpa_printf(MSG_ERROR,
				   "Could not add Framed-IP-Address");
			goto fail;
		}
	}

	return msg;

 fail:
	radius_msg_free(msg);
	return NULL;
}