gnrc_rpl.c
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (C) 2015 Cenk Gündoğan <cnkgndgn@gmail.com>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*
* @author Cenk Gündoğan <cnkgndgn@gmail.com>
*/
#include "net/icmpv6.h"
#include "net/ipv6.h"
#include "net/gnrc/ipv6/netif.h"
#include "net/gnrc.h"
#include "mutex.h"
#include "net/gnrc/rpl.h"
#ifdef MODULE_GNRC_RPL_P2P
#include "net/gnrc/rpl/p2p.h"
#include "net/gnrc/rpl/p2p_dodag.h"
#endif
#define ENABLE_DEBUG (0)
#include "debug.h"
static char _stack[GNRC_RPL_STACK_SIZE];
kernel_pid_t gnrc_rpl_pid = KERNEL_PID_UNDEF;
const ipv6_addr_t ipv6_addr_all_rpl_nodes = GNRC_RPL_ALL_NODES_ADDR;
static uint32_t _lt_time = GNRC_RPL_LIFETIME_UPDATE_STEP * US_PER_SEC;
static xtimer_t _lt_timer;
static msg_t _lt_msg = { .type = GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE };
static msg_t _msg_q[GNRC_RPL_MSG_QUEUE_SIZE];
static gnrc_netreg_entry_t _me_reg;
static mutex_t _inst_id_mutex = MUTEX_INIT;
static uint8_t _instance_id;
gnrc_rpl_instance_t gnrc_rpl_instances[GNRC_RPL_INSTANCES_NUMOF];
gnrc_rpl_parent_t gnrc_rpl_parents[GNRC_RPL_PARENTS_NUMOF];
#ifdef MODULE_NETSTATS_RPL
netstats_rpl_t gnrc_rpl_netstats;
#endif
static void _update_lifetime(void);
static void _dao_handle_send(gnrc_rpl_dodag_t *dodag);
static void _receive(gnrc_pktsnip_t *pkt);
static void *_event_loop(void *args);
kernel_pid_t gnrc_rpl_init(kernel_pid_t if_pid)
{
/* check if RPL was initialized before */
if (gnrc_rpl_pid == KERNEL_PID_UNDEF) {
_instance_id = 0;
/* start the event loop */
gnrc_rpl_pid = thread_create(_stack, sizeof(_stack), GNRC_RPL_PRIO,
THREAD_CREATE_STACKTEST,
_event_loop, NULL, "RPL");
if (gnrc_rpl_pid == KERNEL_PID_UNDEF) {
DEBUG("RPL: could not start the event loop\n");
return KERNEL_PID_UNDEF;
}
_me_reg.demux_ctx = ICMPV6_RPL_CTRL;
_me_reg.target.pid = gnrc_rpl_pid;
/* register interest in all ICMPv6 packets */
gnrc_netreg_register(GNRC_NETTYPE_ICMPV6, &_me_reg);
gnrc_rpl_of_manager_init();
xtimer_set_msg(&_lt_timer, _lt_time, &_lt_msg, gnrc_rpl_pid);
#ifdef MODULE_NETSTATS_RPL
memset(&gnrc_rpl_netstats, 0, sizeof(gnrc_rpl_netstats));
#endif
}
/* register all_RPL_nodes multicast address */
gnrc_ipv6_netif_add_addr(if_pid, &ipv6_addr_all_rpl_nodes, IPV6_ADDR_BIT_LEN, 0);
gnrc_rpl_send_DIS(NULL, (ipv6_addr_t *) &ipv6_addr_all_rpl_nodes);
return gnrc_rpl_pid;
}
gnrc_rpl_instance_t *gnrc_rpl_root_init(uint8_t instance_id, ipv6_addr_t *dodag_id,
bool gen_inst_id, bool local_inst_id)
{
if (gen_inst_id) {
instance_id = gnrc_rpl_gen_instance_id(local_inst_id);
}
gnrc_rpl_dodag_t *dodag = NULL;
gnrc_rpl_instance_t *inst = gnrc_rpl_root_instance_init(instance_id, dodag_id,
GNRC_RPL_DEFAULT_MOP);
if (!inst) {
return NULL;
}
dodag = &inst->dodag;
dodag->dtsn = 1;
dodag->prf = 0;
dodag->dio_interval_doubl = GNRC_RPL_DEFAULT_DIO_INTERVAL_DOUBLINGS;
dodag->dio_min = GNRC_RPL_DEFAULT_DIO_INTERVAL_MIN;
dodag->dio_redun = GNRC_RPL_DEFAULT_DIO_REDUNDANCY_CONSTANT;
dodag->default_lifetime = GNRC_RPL_DEFAULT_LIFETIME;
dodag->lifetime_unit = GNRC_RPL_LIFETIME_UNIT;
dodag->version = GNRC_RPL_COUNTER_INIT;
dodag->grounded = GNRC_RPL_GROUNDED;
dodag->node_status = GNRC_RPL_ROOT_NODE;
dodag->my_rank = GNRC_RPL_ROOT_RANK;
dodag->dio_opts |= GNRC_RPL_REQ_DIO_OPT_DODAG_CONF;
#ifndef GNRC_RPL_WITHOUT_PIO
dodag->dio_opts |= GNRC_RPL_REQ_DIO_OPT_PREFIX_INFO;
#endif
trickle_start(gnrc_rpl_pid, &dodag->trickle, GNRC_RPL_MSG_TYPE_TRICKLE_MSG,
(1 << dodag->dio_min), dodag->dio_interval_doubl,
dodag->dio_redun);
return inst;
}
static void _receive(gnrc_pktsnip_t *icmpv6)
{
gnrc_pktsnip_t *ipv6, *netif;
ipv6_hdr_t *ipv6_hdr;
icmpv6_hdr_t *icmpv6_hdr;
kernel_pid_t iface = KERNEL_PID_UNDEF;
assert(icmpv6 != NULL);
ipv6 = gnrc_pktsnip_search_type(icmpv6, GNRC_NETTYPE_IPV6);
netif = gnrc_pktsnip_search_type(icmpv6, GNRC_NETTYPE_NETIF);
assert(ipv6 != NULL);
if (netif) {
iface = ((gnrc_netif_hdr_t *)netif->data)->if_pid;
}
ipv6_hdr = (ipv6_hdr_t *)ipv6->data;
icmpv6_hdr = (icmpv6_hdr_t *)icmpv6->data;
switch (icmpv6_hdr->code) {
case GNRC_RPL_ICMPV6_CODE_DIS:
DEBUG("RPL: DIS received\n");
gnrc_rpl_recv_DIS((gnrc_rpl_dis_t *)(icmpv6_hdr + 1), iface, &ipv6_hdr->src,
&ipv6_hdr->dst, byteorder_ntohs(ipv6_hdr->len));
break;
case GNRC_RPL_ICMPV6_CODE_DIO:
DEBUG("RPL: DIO received\n");
/*******************************************************************/
//ADDED BY PFE15 2018
gnrc_netif_hdr_t* header=(gnrc_netif_hdr_t*)netif->data;
gnrc_rpl_dio_t * dio_msg=(gnrc_rpl_dio_t *)(icmpv6_hdr + 1);
dio_msg->mc.rssi=header->rssi;
dio_msg->mc.lqi=header->lqi;
//\ADDED BY PFE15 2018
/*******************************************************************/
gnrc_rpl_recv_DIO((gnrc_rpl_dio_t *)(icmpv6_hdr + 1), iface, &ipv6_hdr->src,
&ipv6_hdr->dst, byteorder_ntohs(ipv6_hdr->len));
break;
case GNRC_RPL_ICMPV6_CODE_DAO:
DEBUG("RPL: DAO received\n");
gnrc_rpl_recv_DAO((gnrc_rpl_dao_t *)(icmpv6_hdr + 1), iface, &ipv6_hdr->src,
&ipv6_hdr->dst, byteorder_ntohs(ipv6_hdr->len));
break;
case GNRC_RPL_ICMPV6_CODE_DAO_ACK:
DEBUG("RPL: DAO-ACK received\n");
gnrc_rpl_recv_DAO_ACK((gnrc_rpl_dao_ack_t *)(icmpv6_hdr + 1), iface, &ipv6_hdr->src,
&ipv6_hdr->dst, byteorder_ntohs(ipv6_hdr->len));
break;
#ifdef MODULE_GNRC_RPL_P2P
case GNRC_RPL_P2P_ICMPV6_CODE_DRO:
DEBUG("RPL: P2P DRO received\n");
gnrc_pktsnip_t *icmpv6_snip = gnrc_pktbuf_add(NULL, NULL, icmpv6->size,
GNRC_NETTYPE_ICMPV6);
if (icmpv6_snip == NULL) {
DEBUG("RPL-P2P: cannot copy ICMPv6 packet\n");
break;
}
memcpy(icmpv6_snip->data, icmpv6->data, icmpv6->size);
gnrc_rpl_p2p_recv_DRO(icmpv6_snip, &ipv6_hdr->src);
break;
case GNRC_RPL_P2P_ICMPV6_CODE_DRO_ACK:
DEBUG("RPL: P2P DRO-ACK received\n");
break;
#endif
default:
DEBUG("RPL: Unknown ICMPV6 code received\n");
break;
}
gnrc_pktbuf_release(icmpv6);
}
static void *_event_loop(void *args)
{
msg_t msg, reply;
(void)args;
msg_init_queue(_msg_q, GNRC_RPL_MSG_QUEUE_SIZE);
/* preinitialize ACK */
reply.type = GNRC_NETAPI_MSG_TYPE_ACK;
trickle_t *trickle;
/* start event loop */
while (1) {
DEBUG("RPL: waiting for incoming message.\n");
msg_receive(&msg);
switch (msg.type) {
case GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE:
DEBUG("RPL: GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE received\n");
_update_lifetime();
break;
case GNRC_RPL_MSG_TYPE_TRICKLE_MSG:
DEBUG("RPL: GNRC_RPL_MSG_TYPE_TRICKLE_MSG received\n");
trickle = msg.content.ptr;
if (trickle && (trickle->callback.func != NULL)) {
trickle_callback(trickle);
}
break;
case GNRC_NETAPI_MSG_TYPE_RCV:
DEBUG("RPL: GNRC_NETAPI_MSG_TYPE_RCV received\n");
_receive(msg.content.ptr);
break;
case GNRC_NETAPI_MSG_TYPE_SND:
break;
case GNRC_NETAPI_MSG_TYPE_GET:
case GNRC_NETAPI_MSG_TYPE_SET:
DEBUG("RPL: reply to unsupported get/set\n");
reply.content.value = -ENOTSUP;
msg_reply(&msg, &reply);
break;
default:
break;
}
}
return NULL;
}
void _update_lifetime(void)
{
gnrc_rpl_parent_t *parent;
gnrc_rpl_instance_t *inst;
for (uint8_t i = 0; i < GNRC_RPL_PARENTS_NUMOF; ++i) {
parent = &gnrc_rpl_parents[i];
if (parent->state != 0) {
if (parent->lifetime > GNRC_RPL_LIFETIME_UPDATE_STEP) {
if (parent->lifetime <= (2 * GNRC_RPL_LIFETIME_UPDATE_STEP)) {
gnrc_rpl_send_DIS(parent->dodag->instance, &parent->addr);
}
parent->lifetime -= GNRC_RPL_LIFETIME_UPDATE_STEP;
}
else {
gnrc_rpl_dodag_t *dodag = parent->dodag;
gnrc_rpl_parent_remove(parent);
gnrc_rpl_parent_update(dodag, NULL);
}
}
}
for (int i = 0; i < GNRC_RPL_INSTANCES_NUMOF; ++i) {
inst = &gnrc_rpl_instances[i];
if (inst->state != 0) {
/*******************************************************************/
//ADDED BY PFE15 2018
if((inst->dodag.my_rank >= GNRC_RPL_INFINITE_RANK) || (inst-> dodag.my_rank<=0)){
LED3_OFF;
}
else{
LED3_ON;
}
//\ADDED BY PFE15 2018
/*******************************************************************/
if ((inst->cleanup > 0) && (inst->dodag.parents == NULL) &&
(inst->dodag.my_rank == GNRC_RPL_INFINITE_RANK)) {
inst->cleanup -= GNRC_RPL_LIFETIME_UPDATE_STEP;
if (inst->cleanup <= 0) {
/* no parents - delete this instance and DODAG */
gnrc_rpl_instance_remove(inst);
continue;
}
}
if (inst->dodag.dao_time > GNRC_RPL_LIFETIME_UPDATE_STEP) {
inst->dodag.dao_time -= GNRC_RPL_LIFETIME_UPDATE_STEP;
}
else {
_dao_handle_send(&inst->dodag);
}
}
}
#ifdef MODULE_GNRC_RPL_P2P
gnrc_rpl_p2p_update();
#endif
xtimer_set_msg(&_lt_timer, _lt_time, &_lt_msg, gnrc_rpl_pid);
}
void gnrc_rpl_delay_dao(gnrc_rpl_dodag_t *dodag)
{
dodag->dao_time = GNRC_RPL_DEFAULT_DAO_DELAY;
dodag->dao_counter = 0;
dodag->dao_ack_received = false;
}
void gnrc_rpl_long_delay_dao(gnrc_rpl_dodag_t *dodag)
{
dodag->dao_time = GNRC_RPL_REGULAR_DAO_INTERVAL;
dodag->dao_counter = 0;
dodag->dao_ack_received = false;
}
void _dao_handle_send(gnrc_rpl_dodag_t *dodag)
{
#ifdef MODULE_GNRC_RPL_P2P
if (dodag->instance->mop == GNRC_RPL_P2P_MOP) {
return;
}
#endif
if ((dodag->dao_ack_received == false) && (dodag->dao_counter < GNRC_RPL_DAO_SEND_RETRIES)) {
dodag->dao_counter++;
gnrc_rpl_send_DAO(dodag->instance, NULL, dodag->default_lifetime);
dodag->dao_time = GNRC_RPL_DEFAULT_WAIT_FOR_DAO_ACK;
}
else if (dodag->dao_ack_received == false) {
gnrc_rpl_long_delay_dao(dodag);
}
}
uint8_t gnrc_rpl_gen_instance_id(bool local)
{
mutex_lock(&_inst_id_mutex);
uint8_t instance_id = GNRC_RPL_DEFAULT_INSTANCE;
if (local) {
instance_id = ((_instance_id++) | GNRC_RPL_INSTANCE_ID_MSB);
mutex_unlock(&_inst_id_mutex);
return instance_id;
}
instance_id = ((_instance_id++) & GNRC_RPL_GLOBAL_INSTANCE_MASK);
mutex_unlock(&_inst_id_mutex);
return instance_id;
}
/**
* @}
*/