Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
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 "
lib/random.h
"
49
#include "
net/mac/mac-sequence.h
"
50
#include "
net/packetbuf.h
"
51
52
struct
seqno {
53
linkaddr_t sender;
54
clock_time_t timestamp;
55
uint8_t seqno;
56
};
57
58
#ifdef NETSTACK_CONF_MAC_SEQNO_MAX_AGE
59
#define SEQNO_MAX_AGE NETSTACK_CONF_MAC_SEQNO_MAX_AGE
60
#else
/* NETSTACK_CONF_MAC_SEQNO_MAX_AGE */
61
#define SEQNO_MAX_AGE (20 * CLOCK_SECOND)
62
#endif
/* NETSTACK_CONF_MAC_SEQNO_MAX_AGE */
63
64
#ifdef NETSTACK_CONF_MAC_SEQNO_HISTORY
65
#define MAX_SEQNOS NETSTACK_CONF_MAC_SEQNO_HISTORY
66
#else
/* NETSTACK_CONF_MAC_SEQNO_HISTORY */
67
#define MAX_SEQNOS 16
68
#endif
/* NETSTACK_CONF_MAC_SEQNO_HISTORY */
69
static
struct
seqno received_seqnos[MAX_SEQNOS];
70
71
static
uint8_t mac_dsn;
72
73
/*---------------------------------------------------------------------------*/
74
void
75
mac_sequence_init
(
void
)
76
{
77
mac_dsn = random_rand();
78
}
79
/*---------------------------------------------------------------------------*/
80
void
81
mac_sequence_set_dsn
(
void
)
82
{
83
packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, mac_dsn++);
84
}
85
/*---------------------------------------------------------------------------*/
86
int
87
mac_sequence_is_duplicate
(
void
)
88
{
89
int
i;
90
clock_time_t now =
clock_time
();
91
92
/*
93
* Check for duplicate packet by comparing the sequence number of the incoming
94
* packet with the last few ones we saw.
95
*/
96
for
(i = 0; i < MAX_SEQNOS; ++i) {
97
if
(
linkaddr_cmp
(packetbuf_addr(PACKETBUF_ADDR_SENDER),
98
&received_seqnos[i].sender)) {
99
if
(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO) == received_seqnos[i].seqno) {
100
#if SEQNO_MAX_AGE > 0
101
if
(now - received_seqnos[i].timestamp <= SEQNO_MAX_AGE) {
102
/* Duplicate packet. */
103
return
1;
104
}
105
#else
/* SEQNO_MAX_AGE > 0 */
106
return
1;
107
#endif
/* SEQNO_MAX_AGE > 0 */
108
}
109
break
;
110
}
111
}
112
return
0;
113
}
114
/*---------------------------------------------------------------------------*/
115
void
116
mac_sequence_register_seqno
(
void
)
117
{
118
int
i, j;
119
120
/* Locate possible previous sequence number for this address. */
121
for
(i = 0; i < MAX_SEQNOS; ++i) {
122
if
(
linkaddr_cmp
(packetbuf_addr(PACKETBUF_ADDR_SENDER),
123
&received_seqnos[i].sender)) {
124
i++;
125
break
;
126
}
127
}
128
129
/* Keep the last sequence number for each address as per 802.15.4e. */
130
for
(j = i - 1; j > 0; --j) {
131
memcpy(&received_seqnos[j], &received_seqnos[j - 1],
sizeof
(
struct
seqno));
132
}
133
received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
134
received_seqnos[0].timestamp =
clock_time
();
135
linkaddr_copy
(&received_seqnos[0].sender,
136
packetbuf_addr(PACKETBUF_ADDR_SENDER));
137
}
138
/*---------------------------------------------------------------------------*/
clock_time
clock_time_t clock_time(void)
Get the current clock time.
Definition
clock.c:118
linkaddr_copy
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition
linkaddr.c:63
linkaddr_cmp
bool linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition
linkaddr.c:69
mac_sequence_set_dsn
void mac_sequence_set_dsn(void)
Sets and increments the destination sequence number.
Definition
mac-sequence.c:81
mac_sequence_is_duplicate
int mac_sequence_is_duplicate(void)
Tell whether the packetbuf is a duplicate packet.
Definition
mac-sequence.c:87
mac_sequence_register_seqno
void mac_sequence_register_seqno(void)
Register the sequence number of the packetbuf.
Definition
mac-sequence.c:116
mac_sequence_init
void mac_sequence_init(void)
brief Initializes the destination sequence number to a random value.
Definition
mac-sequence.c:75
mac-sequence.h
Header file for MAC sequence numbers management.
packetbuf.h
Header file for the Packet buffer (packetbuf) management.
random.h
Header file for generating non-cryptographic random numbers.
os
net
mac
mac-sequence.c
Generated on
for Contiki-NG by
1.17.0