framer-802154: Conditional compilation of security-related code

This commit is contained in:
kkrentz 2014-02-07 08:29:06 -08:00
parent fb6d2270ab
commit 124dde25f3
3 changed files with 27 additions and 3 deletions

View File

@ -63,6 +63,7 @@
#include "sys/cc.h"
#include "net/mac/frame802154.h"
#include "net/llsec/llsec802154.h"
#include <string.h>
/**
@ -120,6 +121,7 @@ field_len(frame802154_t *p, field_length_t *flen)
flen->dest_addr_len = addr_len(p->fcf.dest_addr_mode & 3);
flen->src_addr_len = addr_len(p->fcf.src_addr_mode & 3);
#if LLSEC802154_SECURITY_LEVEL
/* Aux security header */
if(p->fcf.security_enabled & 1) {
flen->aux_sec_len = 5;
@ -143,6 +145,7 @@ field_len(frame802154_t *p, field_length_t *flen)
}
#endif
}
#endif /* LLSEC802154_SECURITY_LEVEL */
}
/*----------------------------------------------------------------------------*/
/**
@ -220,6 +223,7 @@ frame802154_create(frame802154_t *p, uint8_t *buf)
buf[pos++] = p->src_addr[c - 1];
}
#if LLSEC802154_SECURITY_LEVEL
/* Aux header */
if(flen.aux_sec_len) {
/* TODO Support key identifier mode !=0 */
@ -227,6 +231,7 @@ frame802154_create(frame802154_t *p, uint8_t *buf)
memcpy(buf + pos, p->aux_hdr.frame_counter.u8, 4);
pos += 4;
}
#endif /* LLSEC802154_SECURITY_LEVEL */
return (int)pos;
}
@ -329,6 +334,7 @@ frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
pf->src_pid = 0;
}
#if LLSEC802154_SECURITY_LEVEL
if(fcf.security_enabled) {
pf->aux_hdr.security_control.security_level = p[0] & 7;
pf->aux_hdr.security_control.key_id_mode = (p[0] >> 3) & 3;
@ -342,6 +348,7 @@ frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
return 0;
}
}
#endif /* LLSEC802154_SECURITY_LEVEL */
/* header length */
c = p - data;

View File

@ -38,6 +38,7 @@
#include "net/mac/framer-802154.h"
#include "net/mac/frame802154.h"
#include "net/llsec/llsec802154.h"
#include "net/packetbuf.h"
#include "lib/random.h"
#include <string.h>
@ -102,9 +103,6 @@ create_frame(int type, int do_create)
/* Build the FCF. */
params.fcf.frame_type = packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE);
if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
params.fcf.security_enabled = 1;
}
params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_null)) {
params.fcf.ack_required = 0;
@ -116,10 +114,15 @@ create_frame(int type, int do_create)
/* Insert IEEE 802.15.4 (2006) version bits. */
params.fcf.frame_version = FRAME802154_IEEE802154_2006;
#if LLSEC802154_SECURITY_LEVEL
if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
params.fcf.security_enabled = 1;
}
/* Setting security-related attributes */
params.aux_hdr.security_control.security_level = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
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);
#endif /* LLSEC802154_SECURITY_LEVEL */
/* Increment and set the data sequence number. */
if(!do_create) {
@ -241,10 +244,12 @@ parse(void)
/* packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, frame.fcf.ack_required);*/
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
#if LLSEC802154_SECURITY_LEVEL
/* Setting security-related attributes */
packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, frame.aux_hdr.security_control.security_level);
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]);
#endif /* LLSEC802154_SECURITY_LEVEL */
PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));

View File

@ -54,6 +54,7 @@
#include "contiki-conf.h"
#include "net/linkaddr.h"
#include "net/llsec/llsec802154.h"
/**
* \brief The size of the packetbuf, in bytes
@ -357,9 +358,11 @@ enum {
PACKETBUF_ATTR_NUM_REXMIT,
PACKETBUF_ATTR_PENDING,
PACKETBUF_ATTR_FRAME_TYPE,
#if LLSEC802154_SECURITY_LEVEL
PACKETBUF_ATTR_SECURITY_LEVEL,
PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3,
#endif /* LLSEC802154_SECURITY_LEVEL */
/* Scope 2 attributes: used between end-to-end nodes. */
PACKETBUF_ATTR_HOPS,
@ -377,6 +380,15 @@ enum {
PACKETBUF_ATTR_MAX
};
/* Define surrogates when 802.15.4 security is off */
#if !LLSEC802154_SECURITY_LEVEL
enum {
PACKETBUF_ATTR_SECURITY_LEVEL,
PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1,
PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3
};
#endif /* LLSEC802154_SECURITY_LEVEL */
#define PACKETBUF_NUM_ADDRS 4
#define PACKETBUF_NUM_ATTRS (PACKETBUF_ATTR_MAX - PACKETBUF_NUM_ADDRS)
#define PACKETBUF_ADDR_FIRST PACKETBUF_ADDR_SENDER