2013-11-15 18:21:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
|
|
|
|
* Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* MAC sequence numbers management
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
* Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "net/mac/mac-sequence.h"
|
|
|
|
#include "net/packetbuf.h"
|
|
|
|
|
|
|
|
struct seqno {
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_t sender;
|
2016-06-23 16:28:18 +00:00
|
|
|
clock_time_t timestamp;
|
2013-11-15 18:21:41 +00:00
|
|
|
uint8_t seqno;
|
|
|
|
};
|
|
|
|
|
2016-06-23 16:28:18 +00:00
|
|
|
#ifdef NETSTACK_CONF_MAC_SEQNO_MAX_AGE
|
|
|
|
#define SEQNO_MAX_AGE NETSTACK_CONF_MAC_SEQNO_MAX_AGE
|
|
|
|
#else /* NETSTACK_CONF_MAC_SEQNO_MAX_AGE */
|
|
|
|
#define SEQNO_MAX_AGE (20 * CLOCK_SECOND)
|
|
|
|
#endif /* NETSTACK_CONF_MAC_SEQNO_MAX_AGE */
|
|
|
|
|
2013-11-15 18:21:41 +00:00
|
|
|
#ifdef NETSTACK_CONF_MAC_SEQNO_HISTORY
|
|
|
|
#define MAX_SEQNOS NETSTACK_CONF_MAC_SEQNO_HISTORY
|
|
|
|
#else /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
|
|
|
|
#define MAX_SEQNOS 16
|
|
|
|
#endif /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
|
|
|
|
static struct seqno received_seqnos[MAX_SEQNOS];
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
mac_sequence_is_duplicate(void)
|
|
|
|
{
|
|
|
|
int i;
|
2016-06-23 16:28:18 +00:00
|
|
|
clock_time_t now = clock_time();
|
2013-11-15 18:21:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for duplicate packet by comparing the sequence number of the incoming
|
|
|
|
* packet with the last few ones we saw.
|
|
|
|
*/
|
|
|
|
for(i = 0; i < MAX_SEQNOS; ++i) {
|
2013-12-12 22:58:52 +00:00
|
|
|
if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
|
2013-11-15 18:21:41 +00:00
|
|
|
&received_seqnos[i].sender)) {
|
2015-05-06 12:23:32 +00:00
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO) == received_seqnos[i].seqno) {
|
2016-06-23 16:28:18 +00:00
|
|
|
#if SEQNO_MAX_AGE > 0
|
|
|
|
if(now - received_seqnos[i].timestamp <= SEQNO_MAX_AGE) {
|
|
|
|
/* Duplicate packet. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else /* SEQNO_MAX_AGE > 0 */
|
2013-11-15 18:31:02 +00:00
|
|
|
return 1;
|
2016-06-23 16:28:18 +00:00
|
|
|
#endif /* SEQNO_MAX_AGE > 0 */
|
2013-11-15 18:31:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-11-15 18:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mac_sequence_register_seqno(void)
|
|
|
|
{
|
2013-11-15 18:31:02 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
/* Locate possible previous sequence number for this address. */
|
|
|
|
for(i = 0; i < MAX_SEQNOS; ++i) {
|
2013-12-12 22:58:52 +00:00
|
|
|
if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
|
2013-11-15 18:31:02 +00:00
|
|
|
&received_seqnos[i].sender)) {
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 18:21:41 +00:00
|
|
|
|
2013-11-15 18:31:02 +00:00
|
|
|
/* Keep the last sequence number for each address as per 802.15.4e. */
|
|
|
|
for(j = i - 1; j > 0; --j) {
|
|
|
|
memcpy(&received_seqnos[j], &received_seqnos[j - 1], sizeof(struct seqno));
|
2013-11-15 18:21:41 +00:00
|
|
|
}
|
2015-05-06 12:23:32 +00:00
|
|
|
received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
|
2016-06-23 16:28:18 +00:00
|
|
|
received_seqnos[0].timestamp = clock_time();
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_copy(&received_seqnos[0].sender,
|
2013-11-15 18:21:41 +00:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|