/* This file is part of libcapwap. libcapwap is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. libcapwap is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Foobar. If not, see . */ #include #include #include "capwap.h" #include "cw_log.h" #include "conn.h" #include "sock.h" static int cwrmsg_init_ctrlhdr(struct cwrmsg * cwrmsg, uint8_t * msg, int len) { if (len<8) return 0; uint32_t val; val = ntohl(*((uint32_t*)(msg+0))); cwrmsg->type = ntohl(*((uint32_t*)(msg))); val = ntohl(*((uint32_t*)(msg+4))); cwrmsg->seqnum = CW_GET_DWORD_BITS(val,0,8); cwrmsg->msgelems_len=CW_GET_DWORD_BITS(val,8,16)-3; // ch->flags=CW_GET_DWORD_BITS(val,24,8); cwrmsg->msgelems=msg+8; if (8+cwrmsg->msgelems_len != len){ return 0; } return 1; } static int process_message(struct conn * conn,struct cwrmsg *cwrmsg,int (*cb)(void*,struct cwrmsg *),void *cbarg) { if (!(cwrmsg->type & 0x1)) { /* It's a response message, no further examination required. */ cb(cbarg,cwrmsg); return 0; } /* It's a request message, check if seqnum is right and if * we have already sent a response message*/ int s1=conn->last_seqnum_received; int s2=cwrmsg->seqnum; int sd=s2-s1; if ((sd>0 && sd<128) || (sd<0 && sd<-128) || s1<0){ /* seqnum is ok, normal message processing */ conn->last_seqnum_received=cwrmsg->seqnum; cb(cbarg,cwrmsg); return 0; } if (sd != 0) { cw_dbg(DBG_CW_MSG_ERR, "Discarding message from %s, old seqnum, seqnum = %d, last seqnum=%d", sock_addr2str(&conn->addr),s2,s1); return 1; } /* the received request message was retransmitte by our peer, * let's retransmit our response message if we have one*/ cw_dbg(DBG_CW_MSG_ERR,"Retransmitted request message from %s etected, seqnum=%d", sock_addr2str(&conn->addr),s2); if (!conn->last_response){ cw_dbg(DBG_CW_MSG_ERR,"No cached response for retransmission, request seqnum=%d",s2); return 0; } cw_dbg(DBG_CW_MSG_ERR,"Retransmitting response message to %s, seqnum=%d", sock_addr2str(&conn->addr),s2); conn_send_cwmsg(conn,conn->last_response); return 1; } void conn_process_packet(struct conn * conn, uint8_t *packet, int len,int (*cb)(void*,struct cwrmsg*),void *cbarg) { cw_dbg(DBG_CW_PKT_DTL,"Processing packet from %s, len=%d",sock_addr2str(&conn->addr),len); if (len<8){ /* packet too short */ cw_dbg(DBG_CW_PKT_ERR,"Discarding packet from %s, packet too short, len=%d",sock_addr2str(&conn->addr),len); return; } uint32_t val = ntohl(*((uint32_t*)packet)); int preamble = val >> 24; if ( (preamble & 0xf0) != CW_VERSION){ /* wrong version */ cw_dbg(DBG_CW_PKT_ERR,"Discarding packet from %s, wrong version, version=%d",sock_addr2str(&conn->addr),preamble&0xf0); return; } if (preamble & 0xf ) { /* decode dtls */ return; } int hlen = 4*((val >> 19) & 0x1f); int payloadlen = len - hlen; if (payloadlen<0){ cw_dbg(DBG_CW_PKT_ERR,"Discarding packet from %s, hlen greater than len, hlen=%d",sock_addr2str(&conn->addr),hlen); /* EINVAL */ return; } struct cwrmsg cwrmsg; cwrmsg.wbid=(val>>9) & 0x1f; cwrmsg.rid=(val>>14) & 0x1f; if (val & CWTH_FLAGS_M){ if (*(packet+8)+8>hlen){ /* wrong rmac size */ cw_dbg(DBG_CW_PKT_ERR,"Discarding packet, wrong rmac size, size=%d",*(packet+8)); return; } memcpy(cwrmsg.rmac, packet+8,8); } else{ cwrmsg.rmac[0]=0; } if (val & CWTH_FLAGS_F){ /* fragmented */ uint8_t * f; f = fragman_add(conn->fragman, packet,hlen,payloadlen); if (f==NULL) return; cwrmsg_init_ctrlhdr(&cwrmsg,f+4,*(uint32_t*)f); process_message(conn,&cwrmsg,cb,cbarg); free (f); return; } cwrmsg_init_ctrlhdr(&cwrmsg,packet+hlen,len-hlen); process_message(conn,&cwrmsg,cb,cbarg); return; }