int netconn_process_packet(struct netconn *nc, uint8_t * packet, int len, struct sockaddr *from) { char sock_buf[SOCK_ADDR_BUFSIZE]; /*// cw_dbg_pkt_nc(DBG_PKT_IN, nc, packet, len, from);*/ if (len < 8) { /* packet too short */ cw_dbg(DBG_PKT_ERR, "Discarding packet from %s, packet too short, len=%d, at least 8 expected.", sock_addr2str(&nc->addr,sock_buf), len); errno = EAGAIN; return -1; } int preamble = cw_get_hdr_preamble(packet); if ((preamble & 0xf0) != (CAPWAP_VERSION << 4)) { /* wrong version */ cw_dbg(DBG_PKT_ERR, "Discarding packet from %s, wrong version, version=%d, version %d expected.", sock_addr2str(&nc->addr,sock_buf), (preamble & 0xf0) >> 4, CAPWAP_VERSION); errno = EAGAIN; return -1; }
int hdr_print(char *str, uint8_t *packet, int len) { char *s = str; if (len<1){ s+=sprintf(s,"\tNo info available."); return s-str; } int preamble = cw_get_hdr_preamble(packet); //CWTH_GET_PREAMBLE(packet); if (preamble==01){ s+=sprintf(s,"\tEncrypted data."); return s-str; } if (preamble!=00){ s+=sprintf(s,"\tWrong CAPWAP version or encryption type."); return s-str; } if (len < 4){ s+=sprintf(s,"\tNo more data. Packet too short."); return s-str; } int hlen = cw_get_hdr_hlen(packet); //CWTH_GET_HLEN(packet); int rid = cw_get_hdr_rid(packet); int wbid = cw_get_hdr_wbid(packet); s+=sprintf(s,"\tHLEN: %d, RID: %02X, WBID %02X",hlen,rid,wbid); s+=sprintf(s," Flags: (T=%d,F=%d,L=%d,W=%d,M=%d,K=%d)\n", cw_get_hdr_flag_t(packet), cw_get_hdr_flag_f(packet), cw_get_hdr_flag_l(packet), cw_get_hdr_flag_w(packet), cw_get_hdr_flag_m(packet), cw_get_hdr_flag_k(packet) ); if (cw_get_hdr_flag_m(packet)){ uint8_t * rmac = cw_get_hdr_rmac(packet); s+=sprintf(s,"\tRadio MAC: %s\n",sock_hwaddr2str(bstr_data(rmac), bstr_len(rmac))); } if (len < 8){ s+=sprintf(s,"\tNo more data. Packet too short."); return s-str; } int frag_id = cw_get_hdr_fragid(packet); int frag_offs = cw_get_hdr_fragoffset(packet); s+=sprintf(s,"\tFrag Id: %d, Frag Offs:: %d\n",frag_id,frag_offs); int bhlen = 4*hlen; if (cw_get_hdr_flag_f(packet) && frag_offs!=0){ s+=sprintf(s,"\tFragment data ..."); return s-str; } if (len<bhlen+4){ s+=sprintf(s,"\tNo more data. Packet too short."); return s-str; } int msgtype = ntohl(*((uint32_t*)(packet+bhlen))); s+=sprintf(s,"\tMsgType: %d",msgtype); if (len<bhlen+8){ s+=sprintf(s,"\n\tNo more data. Packet too short."); return s-str; } int seqnum = (ntohl(*((uint32_t*)(packet+bhlen+4))))>>24; int msgelemlen = 0xFF & ((ntohl(*((uint32_t*)(packet+bhlen+4))))>>8); s+=sprintf(s,", SeqNum: %d, MsgelemLen:%d",seqnum,msgelemlen); return s-str; }
/** * Format a Packet Header */ int cw_format_pkt_hdr(char *dst,int level,struct conn *conn, uint8_t * packet, int len,struct sockaddr *from) { char *s=dst; switch (level) { case DBG_PKT_IN: if (cw_get_hdr_flag_f(packet)){ s+=sprintf(s,"Fragment from %s",sock_addr2str_p(from)); } else{ s+=sprintf(s,"From %s",sock_addr2str_p(from)); } break; case DBG_PKT_OUT: if (cw_get_hdr_flag_f(packet)){ s+=sprintf(s,"Fragment to %s",sock_addr2str(from)); } else{ s+=sprintf(s,"To %s",sock_addr2str(from)); } break; } s+=sprintf(s," l=%d: ",len); int preamble = cw_get_hdr_preamble(packet); if (preamble==01){ s+=sprintf(s," (encrypted)"); return s-dst; } if (len<4) goto abort; /* if (cw_get_hdr_flag_f(packet)){ s+=sprintf(s," (fragmented)"); } */ int hlen = cw_get_hdr_hlen(packet); int rid = cw_get_hdr_rid(packet); int wbid = cw_get_hdr_wbid(packet); s+=sprintf(s," H:%d R:%02d W:%02d",hlen,rid,wbid); s+=sprintf(s," Flgs:"); s+=format_hdr_flags(s,packet); if (len<8) goto abort; int frag_id = cw_get_hdr_fragid(packet); int frag_offs = cw_get_hdr_fragoffset(packet); s+=sprintf(s," Frag/Offs:%d/%d",frag_id,frag_offs); if (cw_get_hdr_flag_m(packet)) { /* rmac is present, print the rmac */ int rmac_len=cw_get_hdr_rmac_len(packet); int plen=rmac_len; if (rmac_len+8>len) plen=len-8; if (rmac_len>10) plen=10; s+=sprintf(s," R-MAC:"); s+=format_mac(s,cw_get_hdr_rmac_data(packet),plen); if (rmac_len>10){ s+=sprintf(s," ... (len=%d)",rmac_len); } } if (cw_get_hdr_flag_w(packet)){ /* print wireless specific info */ int ws_len = cw_get_hdr_ws_len(packet); int plen = ws_len > 20 ? 20:ws_len; s+=sprintf(s," WS:"); s+=format_hexu(s,cw_get_hdr_ws_data(packet),plen); if (ws_len>20){ s+=sprintf(s," ... (len=%d)",ws_len); } } return s-dst; abort: s+=sprintf(s," Incomplete..."); return s-dst; }