Contiki-NG
mac-sequence.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
6  * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  *
35  */
36 
37 /**
38  * \file
39  * MAC sequence numbers management
40  * \author
41  * Adam Dunkels <adam@sics.se>
42  * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
43  */
44 
45 #include <string.h>
46 
47 #include "contiki-net.h"
48 #include "net/mac/mac-sequence.h"
49 #include "net/packetbuf.h"
50 
51 struct seqno {
52  linkaddr_t sender;
53  clock_time_t timestamp;
54  uint8_t seqno;
55 };
56 
57 #ifdef NETSTACK_CONF_MAC_SEQNO_MAX_AGE
58 #define SEQNO_MAX_AGE NETSTACK_CONF_MAC_SEQNO_MAX_AGE
59 #else /* NETSTACK_CONF_MAC_SEQNO_MAX_AGE */
60 #define SEQNO_MAX_AGE (20 * CLOCK_SECOND)
61 #endif /* NETSTACK_CONF_MAC_SEQNO_MAX_AGE */
62 
63 #ifdef NETSTACK_CONF_MAC_SEQNO_HISTORY
64 #define MAX_SEQNOS NETSTACK_CONF_MAC_SEQNO_HISTORY
65 #else /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
66 #define MAX_SEQNOS 16
67 #endif /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
68 static struct seqno received_seqnos[MAX_SEQNOS];
69 
70 /*---------------------------------------------------------------------------*/
71 int
73 {
74  int i;
75  clock_time_t now = clock_time();
76 
77  /*
78  * Check for duplicate packet by comparing the sequence number of the incoming
79  * packet with the last few ones we saw.
80  */
81  for(i = 0; i < MAX_SEQNOS; ++i) {
82  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
83  &received_seqnos[i].sender)) {
84  if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO) == received_seqnos[i].seqno) {
85 #if SEQNO_MAX_AGE > 0
86  if(now - received_seqnos[i].timestamp <= SEQNO_MAX_AGE) {
87  /* Duplicate packet. */
88  return 1;
89  }
90 #else /* SEQNO_MAX_AGE > 0 */
91  return 1;
92 #endif /* SEQNO_MAX_AGE > 0 */
93  }
94  break;
95  }
96  }
97  return 0;
98 }
99 /*---------------------------------------------------------------------------*/
100 void
102 {
103  int i, j;
104 
105  /* Locate possible previous sequence number for this address. */
106  for(i = 0; i < MAX_SEQNOS; ++i) {
107  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
108  &received_seqnos[i].sender)) {
109  i++;
110  break;
111  }
112  }
113 
114  /* Keep the last sequence number for each address as per 802.15.4e. */
115  for(j = i - 1; j > 0; --j) {
116  memcpy(&received_seqnos[j], &received_seqnos[j - 1], sizeof(struct seqno));
117  }
118  received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
119  received_seqnos[0].timestamp = clock_time();
120  linkaddr_copy(&received_seqnos[0].sender,
121  packetbuf_addr(PACKETBUF_ADDR_SENDER));
122 }
123 /*---------------------------------------------------------------------------*/
int mac_sequence_is_duplicate(void)
Tell whether the packetbuf is a duplicate packet.
Definition: mac-sequence.c:72
Header file for MAC sequence numbers management
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:118
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
Header file for the Packet buffer (packetbuf) management
void mac_sequence_register_seqno(void)
Register the sequence number of the packetbuf.
Definition: mac-sequence.c:101