Contiki 3.x
tsch-packet.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, SICS Swedish ICT.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * TSCH packet format management
36  * \author
37  * Simon Duquennoy <simonduq@sics.se>
38  * Beshr Al Nahas <beshr@sics.se>
39  */
40 
41 #include "contiki.h"
42 #include "net/packetbuf.h"
43 #include "net/mac/tsch/tsch.h"
44 #include "net/mac/tsch/tsch-packet.h"
46 #include "net/mac/tsch/tsch-schedule.h"
47 #include "net/mac/tsch/tsch-security.h"
48 #include "net/mac/tsch/tsch-log.h"
49 #include "net/mac/frame802154.h"
50 #include "net/mac/framer-802154.h"
51 #include "net/netstack.h"
52 #include "net/llsec/anti-replay.h"
53 #include "lib/ccm-star.h"
54 #include "lib/aes-128.h"
55 #include <stdio.h>
56 #include <string.h>
57 
58 #if TSCH_LOG_LEVEL >= 1
59 #define DEBUG DEBUG_PRINT
60 #else /* TSCH_LOG_LEVEL */
61 #define DEBUG DEBUG_NONE
62 #endif /* TSCH_LOG_LEVEL */
63 #include "net/net-debug.h"
64 
65 /*---------------------------------------------------------------------------*/
66 /* Construct enhanced ACK packet and return ACK length */
67 int
68 tsch_packet_create_eack(uint8_t *buf, int buf_size,
69  linkaddr_t *dest_addr, uint8_t seqno, int16_t drift, int nack)
70 {
71  int ret;
72  uint8_t curr_len = 0;
73  frame802154_t p;
74  struct ieee802154_ies ies;
75 
76  memset(&p, 0, sizeof(p));
77  p.fcf.frame_type = FRAME802154_ACKFRAME;
78  p.fcf.frame_version = FRAME802154_IEEE802154E_2012;
79  p.fcf.ie_list_present = 1;
80  /* Compression unset. According to IEEE802.15.4e-2012:
81  * - if no address is present: elide PAN ID
82  * - if at least one address is present: include exactly one PAN ID (dest by default) */
83  p.fcf.panid_compression = 0;
84  p.dest_pid = IEEE802154_PANID;
85  p.seq = seqno;
86 #if TSCH_PACKET_EACK_WITH_DEST_ADDR
87  if(dest_addr != NULL) {
88  p.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
89  linkaddr_copy((linkaddr_t *)&p.dest_addr, dest_addr);
90  }
91 #endif
92 #if TSCH_PACKET_EACK_WITH_SRC_ADDR
93  p.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
94  p.src_pid = IEEE802154_PANID;
95  linkaddr_copy((linkaddr_t *)&p.src_addr, &linkaddr_node_addr);
96 #endif
97 #if LLSEC802154_ENABLED
98  if(tsch_is_pan_secured) {
99  p.fcf.security_enabled = 1;
100  p.aux_hdr.security_control.security_level = TSCH_SECURITY_KEY_SEC_LEVEL_ACK;
101  p.aux_hdr.security_control.key_id_mode = FRAME802154_1_BYTE_KEY_ID_MODE;
104  p.aux_hdr.key_index = TSCH_SECURITY_KEY_INDEX_ACK;
105  }
106 #endif /* LLSEC802154_ENABLED */
107 
108  if((curr_len = frame802154_create(&p, buf)) == 0) {
109  return 0;
110  }
111 
112  /* Append IE timesync */
113  memset(&ies, 0, sizeof(ies));
114  ies.ie_time_correction = drift;
115  ies.ie_is_nack = nack;
116 
117  if((ret = frame80215e_create_ie_header_ack_nack_time_correction(buf+curr_len, buf_size-curr_len, &ies)) == -1) {
118  return -1;
119  }
120  curr_len += ret;
121 
122  return curr_len;
123 }
124 /*---------------------------------------------------------------------------*/
125 /* Parse enhanced ACK packet, extract drift and nack */
126 int
127 tsch_packet_parse_eack(const uint8_t *buf, int buf_size,
128  uint8_t seqno, frame802154_t *frame, struct ieee802154_ies *ies, uint8_t *hdr_len)
129 {
130  uint8_t curr_len = 0;
131  int ret;
132  linkaddr_t dest;
133 
134  if(frame == NULL || buf_size < 0) {
135  return 0;
136  }
137  /* Parse 802.15.4-2006 frame, i.e. all fields before Information Elements */
138  if((ret = frame802154_parse((uint8_t *)buf, buf_size, frame)) < 3) {
139  return 0;
140  }
141  if(hdr_len != NULL) {
142  *hdr_len = ret;
143  }
144  curr_len += ret;
145 
146  /* Check seqno */
147  if(seqno != frame->seq) {
148  return 0;
149  }
150 
151  /* Check destination PAN ID */
152  if(frame802154_check_dest_panid(frame) == 0) {
153  return 0;
154  }
155 
156  /* Check destination address (if any) */
157  if(frame802154_extract_linkaddr(frame, NULL, &dest) == 0 ||
159  && !linkaddr_cmp(&dest, &linkaddr_null))) {
160  return 0;
161  }
162 
163  if(ies != NULL) {
164  memset(ies, 0, sizeof(struct ieee802154_ies));
165  }
166 
167  if(frame->fcf.ie_list_present) {
168  int mic_len = 0;
169 #if LLSEC802154_ENABLED
170  /* Check if there is space for the security MIC (if any) */
171  mic_len = tsch_security_mic_len(frame);
172  if(buf_size < curr_len + mic_len) {
173  return 0;
174  }
175 #endif /* LLSEC802154_ENABLED */
176  /* Parse information elements. We need to substract the MIC length, as the exact payload len is needed while parsing */
177  if((ret = frame802154e_parse_information_elements(buf + curr_len, buf_size - curr_len - mic_len, ies)) == -1) {
178  return 0;
179  }
180  curr_len += ret;
181  }
182 
183  if(hdr_len != NULL) {
184  *hdr_len += ies->ie_payload_ie_offset;
185  }
186 
187  return curr_len;
188 }
189 /*---------------------------------------------------------------------------*/
190 /* Create an EB packet */
191 int
192 tsch_packet_create_eb(uint8_t *buf, int buf_size, uint8_t seqno,
193  uint8_t *hdr_len, uint8_t *tsch_sync_ie_offset)
194 {
195  int ret = 0;
196  uint8_t curr_len = 0;
197  uint8_t mlme_ie_offset;
198 
199  frame802154_t p;
200  struct ieee802154_ies ies;
201 
202  if(buf_size < TSCH_PACKET_MAX_LEN) {
203  return 0;
204  }
205 
206  /* Create 802.15.4 header */
207  memset(&p, 0, sizeof(p));
208  p.fcf.frame_type = FRAME802154_BEACONFRAME;
209  p.fcf.ie_list_present = 1;
210  p.fcf.frame_version = FRAME802154_IEEE802154E_2012;
211  p.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
212  p.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
213  p.seq = seqno;
214  p.fcf.sequence_number_suppression = FRAME802154_SUPPR_SEQNO;
215  /* It is important not to compress PAN ID, as this would result in not including either
216  * source nor destination PAN ID, leaving potential joining devices unaware of the PAN ID. */
217  p.fcf.panid_compression = 0;
218 
219  p.src_pid = frame802154_get_pan_id();
220  p.dest_pid = frame802154_get_pan_id();
221  linkaddr_copy((linkaddr_t *)&p.src_addr, &linkaddr_node_addr);
222  p.dest_addr[0] = 0xff;
223  p.dest_addr[1] = 0xff;
224 
225 #if LLSEC802154_ENABLED
226  if(tsch_is_pan_secured) {
227  p.fcf.security_enabled = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0;
228  p.aux_hdr.security_control.security_level = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
229  p.aux_hdr.security_control.key_id_mode = packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE);
232  p.aux_hdr.key_index = packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX);
233  }
234 #endif /* LLSEC802154_ENABLED */
235 
236  if((curr_len = frame802154_create(&p, buf)) == 0) {
237  return 0;
238  }
239 
240  /* Prepare Information Elements for inclusion in the EB */
241  memset(&ies, 0, sizeof(ies));
242 
243  /* Add TSCH timeslot timing IE. */
244 #if TSCH_PACKET_EB_WITH_TIMESLOT_TIMING
245  {
246  int i;
247  ies.ie_tsch_timeslot_id = 1;
248  for(i = 0; i < tsch_ts_elements_count; i++) {
249  ies.ie_tsch_timeslot[i] = RTIMERTICKS_TO_US(tsch_timing[i]);
250  }
251  }
252 #endif /* TSCH_PACKET_EB_WITH_TIMESLOT_TIMING */
253 
254  /* Add TSCH hopping sequence IE */
255 #if TSCH_PACKET_EB_WITH_HOPPING_SEQUENCE
256  if(tsch_hopping_sequence_length.val <= sizeof(ies.ie_hopping_sequence_list)) {
257  ies.ie_channel_hopping_sequence_id = 1;
258  ies.ie_hopping_sequence_len = tsch_hopping_sequence_length.val;
259  memcpy(ies.ie_hopping_sequence_list, tsch_hopping_sequence, ies.ie_hopping_sequence_len);
260  }
261 #endif /* TSCH_PACKET_EB_WITH_HOPPING_SEQUENCE */
262 
263  /* Add Slotframe and Link IE */
264 #if TSCH_PACKET_EB_WITH_SLOTFRAME_AND_LINK
265  {
266  /* Send slotframe 0 with link at timeslot 0 */
267  struct tsch_slotframe *sf0 = tsch_schedule_get_slotframe_by_handle(0);
268  struct tsch_link *link0 = tsch_schedule_get_link_by_timeslot(sf0, 0);
269  if(sf0 && link0) {
270  ies.ie_tsch_slotframe_and_link.num_slotframes = 1;
271  ies.ie_tsch_slotframe_and_link.slotframe_handle = sf0->handle;
272  ies.ie_tsch_slotframe_and_link.slotframe_size = sf0->size.val;
273  ies.ie_tsch_slotframe_and_link.num_links = 1;
274  ies.ie_tsch_slotframe_and_link.links[0].timeslot = link0->timeslot;
275  ies.ie_tsch_slotframe_and_link.links[0].channel_offset = link0->channel_offset;
276  ies.ie_tsch_slotframe_and_link.links[0].link_options = link0->link_options;
277  }
278  }
279 #endif /* TSCH_PACKET_EB_WITH_SLOTFRAME_AND_LINK */
280 
281  /* First add header-IE termination IE to stipulate that next come payload IEs */
282  if((ret = frame80215e_create_ie_header_list_termination_1(buf + curr_len, buf_size - curr_len, &ies)) == -1) {
283  return -1;
284  }
285  curr_len += ret;
286 
287  /* We start payload IEs, save offset */
288  if(hdr_len != NULL) {
289  *hdr_len = curr_len;
290  }
291 
292  /* Save offset of the MLME IE descriptor, we need to know the total length
293  * before writing it */
294  mlme_ie_offset = curr_len;
295  curr_len += 2; /* Space needed for MLME descriptor */
296 
297  /* Save the offset of the TSCH Synchronization IE, needed to update ASN and join priority before sending */
298  if(tsch_sync_ie_offset != NULL) {
299  *tsch_sync_ie_offset = curr_len;
300  }
301  if((ret = frame80215e_create_ie_tsch_synchronization(buf + curr_len, buf_size - curr_len, &ies)) == -1) {
302  return -1;
303  }
304  curr_len += ret;
305 
306  if((ret = frame80215e_create_ie_tsch_timeslot(buf + curr_len, buf_size - curr_len, &ies)) == -1) {
307  return -1;
308  }
309  curr_len += ret;
310 
311  if((ret = frame80215e_create_ie_tsch_channel_hopping_sequence(buf + curr_len, buf_size - curr_len, &ies)) == -1) {
312  return -1;
313  }
314  curr_len += ret;
315 
316  if((ret = frame80215e_create_ie_tsch_slotframe_and_link(buf + curr_len, buf_size - curr_len, &ies)) == -1) {
317  return -1;
318  }
319  curr_len += ret;
320 
321  ies.ie_mlme_len = curr_len - mlme_ie_offset - 2;
322  if((ret = frame80215e_create_ie_mlme(buf + mlme_ie_offset, buf_size - mlme_ie_offset, &ies)) == -1) {
323  return -1;
324  }
325 
326  /* Payload IE list termination: optional */
327  /*
328  if((ret = frame80215e_create_ie_payload_list_termination(buf + curr_len, buf_size - curr_len, &ies)) == -1) {
329  return -1;
330  }
331  curr_len += ret;
332  */
333 
334  return curr_len;
335 }
336 /*---------------------------------------------------------------------------*/
337 /* Update ASN in EB packet */
338 int
339 tsch_packet_update_eb(uint8_t *buf, int buf_size, uint8_t tsch_sync_ie_offset)
340 {
341  struct ieee802154_ies ies;
342  ies.ie_asn = current_asn;
343  ies.ie_join_priority = tsch_join_priority;
344  frame80215e_create_ie_tsch_synchronization(buf+tsch_sync_ie_offset, buf_size-tsch_sync_ie_offset, &ies);
345  return 1;
346 }
347 /*---------------------------------------------------------------------------*/
348 /* Parse a IEEE 802.15.4e TSCH Enhanced Beacon (EB) */
349 int
350 tsch_packet_parse_eb(const uint8_t *buf, int buf_size,
351  frame802154_t *frame, struct ieee802154_ies *ies, uint8_t *hdr_len, int frame_without_mic)
352 {
353  uint8_t curr_len = 0;
354  int ret;
355 
356  if(frame == NULL || buf_size < 0) {
357  return 0;
358  }
359 
360  /* Parse 802.15.4-2006 frame, i.e. all fields before Information Elements */
361  if((ret = frame802154_parse((uint8_t *)buf, buf_size, frame)) == 0) {
362  PRINTF("TSCH:! parse_eb: failed to parse frame\n");
363  return 0;
364  }
365 
366  if(frame->fcf.frame_version < FRAME802154_IEEE802154E_2012
367  || frame->fcf.frame_type != FRAME802154_BEACONFRAME) {
368  PRINTF("TSCH:! parse_eb: frame is not a valid TSCH beacon. Frame version %u, type %u, FCF %02x %02x\n",
369  frame->fcf.frame_version, frame->fcf.frame_type, buf[0], buf[1]);
370  PRINTF("TSCH:! parse_eb: frame was from 0x%x/", frame->src_pid);
371  PRINTLLADDR((const uip_lladdr_t *)&frame->src_addr);
372  PRINTF(" to 0x%x/", frame->dest_pid);
373  PRINTLLADDR((const uip_lladdr_t *)&frame->dest_addr);
374  PRINTF("\n");
375  return 0;
376  }
377 
378  if(hdr_len != NULL) {
379  *hdr_len = ret;
380  }
381  curr_len += ret;
382 
383  if(ies != NULL) {
384  memset(ies, 0, sizeof(struct ieee802154_ies));
385  ies->ie_join_priority = 0xff; /* Use max value in case the Beacon does not include a join priority */
386  }
387  if(frame->fcf.ie_list_present) {
388  /* Calculate space needed for the security MIC, if any, before attempting to parse IEs */
389  int mic_len = 0;
390 #if LLSEC802154_ENABLED
391  if(!frame_without_mic) {
392  mic_len = tsch_security_mic_len(frame);
393  if(buf_size < curr_len + mic_len) {
394  return 0;
395  }
396  }
397 #endif /* LLSEC802154_ENABLED */
398 
399  /* Parse information elements. We need to substract the MIC length, as the exact payload len is needed while parsing */
400  if((ret = frame802154e_parse_information_elements(buf + curr_len, buf_size - curr_len - mic_len, ies)) == -1) {
401  PRINTF("TSCH:! parse_eb: failed to parse IEs\n");
402  return 0;
403  }
404  curr_len += ret;
405  }
406 
407  if(hdr_len != NULL) {
408  *hdr_len += ies->ie_payload_ie_offset;
409  }
410 
411  return curr_len;
412 }
413 /*---------------------------------------------------------------------------*/
uint8_t security_enabled
1 bit.
Definition: frame802154.h:148
uint16_t src_pid
Source PAN ID.
Definition: frame802154.h:201
frame802154_scf_t security_control
Security control bitfield.
Definition: frame802154.h:182
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:196
uint8_t key_id_mode
2 bit.
Definition: frame802154.h:163
uint16_t dest_pid
Destination PAN ID.
Definition: frame802154.h:200
uint8_t key_index
Key Index subfield.
Definition: frame802154.h:185
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:198
uint8_t ie_list_present
1 bit.
Definition: frame802154.h:154
AES-128.
uint8_t src_addr[8]
Source address.
Definition: frame802154.h:197
uint8_t seq
Sequence number.
Definition: frame802154.h:199
Private TSCH definitions (meant for use by TSCH implementation files only) ...
const linkaddr_t linkaddr_null
The null Rime address.
Definition: eth-conf.c:37
Header file for the Rime buffer (packetbuf) management
int frame802154_create(frame802154_t *p, uint8_t *buf)
Creates a frame for transmission over the air.
Definition: frame802154.c:369
Interface to anti-replay mechanisms.
int frame80215e_create_ie_header_ack_nack_time_correction(uint8_t *buf, int len, struct ieee802154_ies *ies)
Insert various Information Elements.
A set of debugging macros for the netstack
CCM* header file.
#define NULL
The null pointer.
802.3 address
Definition: uip.h:129
uint8_t frame_type
3 bit.
Definition: frame802154.h:147
802.15.4 frame creation and parsing functions
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:465
uint8_t security_level
3 bit.
Definition: frame802154.h:162
uint8_t src_addr_mode
2 bit.
Definition: frame802154.h:157
uint8_t frame_counter_size
1 bit.
Definition: frame802154.h:165
frame802154_aux_hdr_t aux_hdr
Aux security header.
Definition: frame802154.h:202
uint8_t dest_addr_mode
2 bit.
Definition: frame802154.h:155
uint8_t panid_compression
1 bit.
Definition: frame802154.h:151
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
uint8_t frame_version
2 bit.
Definition: frame802154.h:156
Parameters used by the frame802154_create() function.
Definition: frame802154.h:192
uint8_t frame_counter_suppression
1 bit.
Definition: frame802154.h:164
Include file for the Contiki low-layer network stack (NETSTACK)
A MAC framer for IEEE 802.15.4
uint8_t sequence_number_suppression
< 1 bit.
Definition: frame802154.h:153