2009-10-19 20:30:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* MAC framer for IEEE 802.15.4
|
|
|
|
* \author
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/mac/framer-802154.h"
|
|
|
|
#include "net/mac/frame802154.h"
|
2014-02-07 17:29:06 +01:00
|
|
|
#include "net/llsec/llsec802154.h"
|
2010-06-14 21:19:16 +02:00
|
|
|
#include "net/packetbuf.h"
|
2010-03-01 14:17:01 +01:00
|
|
|
#include "lib/random.h"
|
2009-10-20 09:42:03 +02:00
|
|
|
#include <string.h>
|
2009-10-19 20:30:03 +02:00
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#define PRINTADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7])
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#define PRINTADDR(addr)
|
|
|
|
#endif
|
|
|
|
|
2011-07-11 12:04:52 +02:00
|
|
|
/** \brief The sequence number (0x00 - 0xff) added to the transmitted
|
|
|
|
* data or MAC command frame. The default is a random value within
|
|
|
|
* the range.
|
|
|
|
*/
|
2009-10-19 20:30:03 +02:00
|
|
|
static uint8_t mac_dsn;
|
2011-07-11 12:04:52 +02:00
|
|
|
|
2010-03-01 14:17:01 +01:00
|
|
|
static uint8_t initialized = 0;
|
2011-07-11 12:04:52 +02:00
|
|
|
|
2009-10-19 20:30:03 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2014-04-11 17:03:23 +02:00
|
|
|
create_frame(int type, int do_create)
|
2009-10-19 20:30:03 +02:00
|
|
|
{
|
|
|
|
frame802154_t params;
|
2013-06-19 13:48:48 +02:00
|
|
|
int hdr_len;
|
2009-10-19 20:30:03 +02:00
|
|
|
|
2015-09-21 18:39:30 +02:00
|
|
|
if(frame802154_get_pan_id() == 0xffff) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-19 20:30:03 +02:00
|
|
|
/* init to zeros */
|
|
|
|
memset(¶ms, 0, sizeof(params));
|
|
|
|
|
2010-03-01 14:17:01 +01:00
|
|
|
if(!initialized) {
|
|
|
|
initialized = 1;
|
|
|
|
mac_dsn = random_rand() & 0xff;
|
|
|
|
}
|
|
|
|
|
2009-10-19 20:30:03 +02:00
|
|
|
/* Build the FCF. */
|
2013-05-24 15:54:55 +02:00
|
|
|
params.fcf.frame_type = packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE);
|
2010-04-03 15:28:30 +02:00
|
|
|
params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
|
2014-08-05 13:49:05 +02:00
|
|
|
if(packetbuf_holds_broadcast()) {
|
2010-03-09 21:40:11 +01:00
|
|
|
params.fcf.ack_required = 0;
|
2016-06-23 18:25:52 +02:00
|
|
|
/* Suppress seqno on broadcast if supported (frame v2 or more) */
|
|
|
|
params.fcf.sequence_number_suppression = FRAME802154_VERSION >= FRAME802154_IEEE802154E_2012;
|
2010-03-09 21:40:11 +01:00
|
|
|
} else {
|
2010-04-30 09:25:51 +02:00
|
|
|
params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK);
|
2016-06-23 18:25:52 +02:00
|
|
|
params.fcf.sequence_number_suppression = FRAME802154_SUPPR_SEQNO;
|
2010-03-09 21:40:11 +01:00
|
|
|
}
|
2015-09-21 18:39:30 +02:00
|
|
|
/* We do not compress PAN ID in outgoing frames, i.e. include one PAN ID (dest by default)
|
|
|
|
* There is one exception, seemingly a typo in Table 2a: rows 2 and 3: when there is no
|
|
|
|
* source nor destination address, we have dest PAN ID iff compression is *set*. */
|
2009-10-19 20:30:03 +02:00
|
|
|
params.fcf.panid_compression = 0;
|
|
|
|
|
2015-09-21 18:39:30 +02:00
|
|
|
/* Insert IEEE 802.15.4 version bits. */
|
|
|
|
params.fcf.frame_version = FRAME802154_VERSION;
|
2013-05-24 13:40:01 +02:00
|
|
|
|
2015-12-17 12:13:32 +01:00
|
|
|
#if LLSEC802154_USES_AUX_HEADER
|
2014-02-07 17:29:06 +01:00
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
|
|
|
|
params.fcf.security_enabled = 1;
|
|
|
|
}
|
2013-05-24 13:40:01 +02:00
|
|
|
/* Setting security-related attributes */
|
|
|
|
params.aux_hdr.security_control.security_level = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
|
2015-09-21 18:39:30 +02:00
|
|
|
#if LLSEC802154_USES_FRAME_COUNTER
|
2013-05-24 13:40:01 +02:00
|
|
|
params.aux_hdr.frame_counter.u16[0] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1);
|
|
|
|
params.aux_hdr.frame_counter.u16[1] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3);
|
2015-09-21 18:39:30 +02:00
|
|
|
#else /* LLSEC802154_USES_FRAME_COUNTER */
|
|
|
|
params.aux_hdr.security_control.frame_counter_suppression = 1;
|
|
|
|
params.aux_hdr.security_control.frame_counter_size = 1;
|
|
|
|
#endif /* LLSEC802154_USES_FRAME_COUNTER */
|
2013-06-24 08:49:11 +02:00
|
|
|
#if LLSEC802154_USES_EXPLICIT_KEYS
|
|
|
|
params.aux_hdr.security_control.key_id_mode = packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE);
|
|
|
|
params.aux_hdr.key_index = packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX);
|
|
|
|
params.aux_hdr.key_source.u16[0] = packetbuf_attr(PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1);
|
|
|
|
#endif /* LLSEC802154_USES_EXPLICIT_KEYS */
|
2015-12-17 12:13:32 +01:00
|
|
|
#endif /* LLSEC802154_USES_AUX_HEADER */
|
2009-10-19 20:30:03 +02:00
|
|
|
|
|
|
|
/* Increment and set the data sequence number. */
|
2014-04-11 17:03:23 +02:00
|
|
|
if(!do_create) {
|
|
|
|
/* Only length calculation - no sequence number is needed and
|
|
|
|
should not be consumed. */
|
|
|
|
|
|
|
|
} else if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
|
2010-04-08 11:32:56 +02:00
|
|
|
params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
|
2014-04-11 17:03:23 +02:00
|
|
|
|
2010-04-08 11:32:56 +02:00
|
|
|
} else {
|
2013-05-27 15:30:14 +02:00
|
|
|
/* Ensure that the sequence number 0 is not used as it would bypass the above check. */
|
|
|
|
if(mac_dsn == 0) {
|
|
|
|
mac_dsn++;
|
|
|
|
}
|
2010-04-08 11:32:56 +02:00
|
|
|
params.seq = mac_dsn++;
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq);
|
|
|
|
}
|
2009-10-19 20:30:03 +02:00
|
|
|
|
|
|
|
/* Complete the addressing fields. */
|
|
|
|
/**
|
|
|
|
\todo For phase 1 the addresses are all long. We'll need a mechanism
|
|
|
|
in the rime attributes to tell the mac to use long or short for phase 2.
|
2015-09-21 18:39:30 +02:00
|
|
|
*/
|
2014-04-11 17:16:59 +02:00
|
|
|
if(LINKADDR_SIZE == 2) {
|
2013-12-12 23:58:52 +01:00
|
|
|
/* Use short address mode if linkaddr size is short. */
|
2010-03-12 15:49:21 +01:00
|
|
|
params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
|
|
|
|
} else {
|
|
|
|
params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
|
|
|
|
}
|
2015-09-21 18:39:30 +02:00
|
|
|
params.dest_pid = frame802154_get_pan_id();
|
2009-10-19 20:30:03 +02:00
|
|
|
|
2014-08-05 13:49:05 +02:00
|
|
|
if(packetbuf_holds_broadcast()) {
|
2009-10-19 20:30:03 +02:00
|
|
|
/* Broadcast requires short address mode. */
|
|
|
|
params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
|
2010-02-18 22:00:28 +01:00
|
|
|
params.dest_addr[0] = 0xFF;
|
|
|
|
params.dest_addr[1] = 0xFF;
|
2009-10-19 20:30:03 +02:00
|
|
|
} else {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy((linkaddr_t *)¶ms.dest_addr,
|
2010-02-18 22:00:28 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
|
2013-12-12 23:58:52 +01:00
|
|
|
/* Use short address mode if linkaddr size is small */
|
2014-04-11 17:16:59 +02:00
|
|
|
if(LINKADDR_SIZE == 2) {
|
2010-03-12 15:49:21 +01:00
|
|
|
params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
|
|
|
|
} else {
|
|
|
|
params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
|
|
|
|
}
|
2009-10-19 20:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the source PAN ID to the global variable. */
|
2015-09-21 18:39:30 +02:00
|
|
|
params.src_pid = frame802154_get_pan_id();
|
2011-07-11 12:04:52 +02:00
|
|
|
|
2009-10-19 20:30:03 +02:00
|
|
|
/*
|
|
|
|
* Set up the source address using only the long address mode for
|
|
|
|
* phase 1.
|
|
|
|
*/
|
2016-12-14 14:00:14 +01:00
|
|
|
linkaddr_copy((linkaddr_t *)¶ms.src_addr,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
2009-10-19 20:30:03 +02:00
|
|
|
|
|
|
|
params.payload = packetbuf_dataptr();
|
|
|
|
params.payload_len = packetbuf_datalen();
|
2013-06-19 13:48:48 +02:00
|
|
|
hdr_len = frame802154_hdrlen(¶ms);
|
2014-04-11 17:03:23 +02:00
|
|
|
if(!do_create) {
|
|
|
|
/* Only calculate header length */
|
2013-06-19 13:48:48 +02:00
|
|
|
return hdr_len;
|
|
|
|
} else if(packetbuf_hdralloc(hdr_len)) {
|
2013-06-19 13:58:48 +02:00
|
|
|
frame802154_create(¶ms, packetbuf_hdrptr());
|
2009-10-19 20:30:03 +02:00
|
|
|
|
2010-03-01 14:17:01 +01:00
|
|
|
PRINTF("15.4-OUT: %2X", params.fcf.frame_type);
|
2013-10-15 01:43:26 +02:00
|
|
|
PRINTADDR(params.dest_addr);
|
2013-06-19 13:48:48 +02:00
|
|
|
PRINTF("%d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
|
2009-10-19 20:30:03 +02:00
|
|
|
|
2013-06-19 13:48:48 +02:00
|
|
|
return hdr_len;
|
2009-10-19 20:30:03 +02:00
|
|
|
} else {
|
2013-06-19 13:48:48 +02:00
|
|
|
PRINTF("15.4-OUT: too large header: %u\n", hdr_len);
|
2012-01-01 03:00:21 +01:00
|
|
|
return FRAMER_FAILED;
|
2009-10-19 20:30:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2014-04-11 17:03:23 +02:00
|
|
|
hdr_length(void)
|
|
|
|
{
|
|
|
|
return create_frame(FRAME802154_DATAFRAME, 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
create(void)
|
|
|
|
{
|
|
|
|
return create_frame(FRAME802154_DATAFRAME, 1);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2009-10-19 20:30:03 +02:00
|
|
|
parse(void)
|
|
|
|
{
|
|
|
|
frame802154_t frame;
|
2013-05-24 09:31:14 +02:00
|
|
|
int hdr_len;
|
2015-09-21 18:39:30 +02:00
|
|
|
|
2013-05-24 09:31:14 +02:00
|
|
|
hdr_len = frame802154_parse(packetbuf_dataptr(), packetbuf_datalen(), &frame);
|
2015-09-21 18:39:30 +02:00
|
|
|
|
2013-05-24 09:31:14 +02:00
|
|
|
if(hdr_len && packetbuf_hdrreduce(hdr_len)) {
|
2013-05-24 15:54:55 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, frame.fcf.frame_type);
|
2015-09-21 18:39:30 +02:00
|
|
|
|
2009-10-19 20:30:03 +02:00
|
|
|
if(frame.fcf.dest_addr_mode) {
|
2015-09-21 18:39:30 +02:00
|
|
|
if(frame.dest_pid != frame802154_get_pan_id() &&
|
|
|
|
frame.dest_pid != FRAME802154_BROADCASTPANDID) {
|
2009-10-19 20:30:03 +02:00
|
|
|
/* Packet to another PAN */
|
2010-03-01 14:17:01 +01:00
|
|
|
PRINTF("15.4: for another pan %u\n", frame.dest_pid);
|
2012-01-01 03:00:21 +01:00
|
|
|
return FRAMER_FAILED;
|
2009-10-19 20:30:03 +02:00
|
|
|
}
|
2015-09-21 18:39:30 +02:00
|
|
|
if(!frame802154_is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
|
2013-12-12 23:58:52 +01:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
|
2009-10-19 20:30:03 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 23:58:52 +01:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
|
2010-04-03 15:28:30 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending);
|
2015-09-21 18:39:30 +02:00
|
|
|
if(frame.fcf.sequence_number_suppression == 0) {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, frame.seq);
|
|
|
|
} else {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, 0xffff);
|
|
|
|
}
|
2015-05-06 14:23:32 +02:00
|
|
|
#if NETSTACK_CONF_WITH_RIME
|
2010-04-05 21:28:07 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
|
2015-05-06 14:23:32 +02:00
|
|
|
#endif
|
2015-09-21 18:39:30 +02:00
|
|
|
|
2015-12-17 12:13:32 +01:00
|
|
|
#if LLSEC802154_USES_AUX_HEADER
|
2013-06-24 08:49:11 +02:00
|
|
|
if(frame.fcf.security_enabled) {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, frame.aux_hdr.security_control.security_level);
|
2015-09-21 18:39:30 +02:00
|
|
|
#if LLSEC802154_USES_FRAME_COUNTER
|
2013-06-24 08:49:11 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, frame.aux_hdr.frame_counter.u16[0]);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, frame.aux_hdr.frame_counter.u16[1]);
|
2015-09-21 18:39:30 +02:00
|
|
|
#endif /* LLSEC802154_USES_FRAME_COUNTER */
|
2013-06-24 08:49:11 +02:00
|
|
|
#if LLSEC802154_USES_EXPLICIT_KEYS
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, frame.aux_hdr.security_control.key_id_mode);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, frame.aux_hdr.key_index);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1, frame.aux_hdr.key_source.u16[0]);
|
|
|
|
#endif /* LLSEC802154_USES_EXPLICIT_KEYS */
|
|
|
|
}
|
2015-12-17 12:13:32 +01:00
|
|
|
#endif /* LLSEC802154_USES_AUX_HEADER */
|
2009-10-19 20:30:03 +02:00
|
|
|
|
2010-03-01 14:17:01 +01:00
|
|
|
PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
|
2009-10-19 20:30:03 +02:00
|
|
|
PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
|
2013-05-24 09:31:14 +02:00
|
|
|
PRINTF("%d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
|
2015-09-21 18:39:30 +02:00
|
|
|
|
2013-05-24 09:31:14 +02:00
|
|
|
return hdr_len;
|
2009-10-19 20:30:03 +02:00
|
|
|
}
|
2012-01-01 03:00:21 +01:00
|
|
|
return FRAMER_FAILED;
|
2009-10-19 20:30:03 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct framer framer_802154 = {
|
2014-04-26 11:39:51 +02:00
|
|
|
hdr_length,
|
|
|
|
create,
|
|
|
|
parse
|
2009-10-19 20:30:03 +02:00
|
|
|
};
|
2014-04-26 11:39:51 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|