Contiki 3.x
framer-802154.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Swedish Institute of Computer Science.
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  */
30 
31 /**
32  * \file
33  * MAC framer for IEEE 802.15.4
34  * \author
35  * Niclas Finne <nfi@sics.se>
36  * Joakim Eriksson <joakime@sics.se>
37  */
38 
39 #include "net/mac/framer-802154.h"
40 #include "net/mac/frame802154.h"
41 #include "net/llsec/llsec802154.h"
42 #include "net/packetbuf.h"
43 #include "lib/random.h"
44 #include <string.h>
45 
46 #define DEBUG 0
47 
48 #if DEBUG
49 #include <stdio.h>
50 #define PRINTF(...) printf(__VA_ARGS__)
51 #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])
52 #else
53 #define PRINTF(...)
54 #define PRINTADDR(addr)
55 #endif
56 
57 /** \brief The sequence number (0x00 - 0xff) added to the transmitted
58  * data or MAC command frame. The default is a random value within
59  * the range.
60  */
61 static uint8_t mac_dsn;
62 
63 static uint8_t initialized = 0;
64 
65 /*---------------------------------------------------------------------------*/
66 static int
67 create_frame(int type, int do_create)
68 {
69  frame802154_t params;
70  int hdr_len;
71 
72  if(frame802154_get_pan_id() == 0xffff) {
73  return -1;
74  }
75 
76  /* init to zeros */
77  memset(&params, 0, sizeof(params));
78 
79  if(!initialized) {
80  initialized = 1;
81  mac_dsn = random_rand() & 0xff;
82  }
83 
84  /* Build the FCF. */
85  params.fcf.frame_type = packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE);
86  params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
88  params.fcf.ack_required = 0;
89  } else {
90  params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK);
91  }
92  /* We do not compress PAN ID in outgoing frames, i.e. include one PAN ID (dest by default)
93  * There is one exception, seemingly a typo in Table 2a: rows 2 and 3: when there is no
94  * source nor destination address, we have dest PAN ID iff compression is *set*. */
95  params.fcf.panid_compression = 0;
96  params.fcf.sequence_number_suppression = FRAME802154_SUPPR_SEQNO;
97 
98  /* Insert IEEE 802.15.4 version bits. */
99  params.fcf.frame_version = FRAME802154_VERSION;
100 
101 #if LLSEC802154_USES_AUX_HEADER
102  if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
103  params.fcf.security_enabled = 1;
104  }
105  /* Setting security-related attributes */
106  params.aux_hdr.security_control.security_level = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
107 #if LLSEC802154_USES_FRAME_COUNTER
108  params.aux_hdr.frame_counter.u16[0] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1);
109  params.aux_hdr.frame_counter.u16[1] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3);
110 #else /* LLSEC802154_USES_FRAME_COUNTER */
113 #endif /* LLSEC802154_USES_FRAME_COUNTER */
114 #if LLSEC802154_USES_EXPLICIT_KEYS
115  params.aux_hdr.security_control.key_id_mode = packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE);
116  params.aux_hdr.key_index = packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX);
117  params.aux_hdr.key_source.u16[0] = packetbuf_attr(PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1);
118 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
119 #endif /* LLSEC802154_USES_AUX_HEADER */
120 
121  /* Increment and set the data sequence number. */
122  if(!do_create) {
123  /* Only length calculation - no sequence number is needed and
124  should not be consumed. */
125 
126  } else if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
127  params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
128 
129  } else {
130  /* Ensure that the sequence number 0 is not used as it would bypass the above check. */
131  if(mac_dsn == 0) {
132  mac_dsn++;
133  }
134  params.seq = mac_dsn++;
135  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq);
136  }
137 
138  /* Complete the addressing fields. */
139  /**
140  \todo For phase 1 the addresses are all long. We'll need a mechanism
141  in the rime attributes to tell the mac to use long or short for phase 2.
142  */
143  if(LINKADDR_SIZE == 2) {
144  /* Use short address mode if linkaddr size is short. */
145  params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
146  } else {
147  params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
148  }
149  params.dest_pid = frame802154_get_pan_id();
150 
152  /* Broadcast requires short address mode. */
153  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
154  params.dest_addr[0] = 0xFF;
155  params.dest_addr[1] = 0xFF;
156  } else {
157  linkaddr_copy((linkaddr_t *)&params.dest_addr,
158  packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
159  /* Use short address mode if linkaddr size is small */
160  if(LINKADDR_SIZE == 2) {
161  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
162  } else {
163  params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
164  }
165  }
166 
167  /* Set the source PAN ID to the global variable. */
168  params.src_pid = frame802154_get_pan_id();
169 
170  /*
171  * Set up the source address using only the long address mode for
172  * phase 1.
173  */
174  linkaddr_copy((linkaddr_t *)&params.src_addr, &linkaddr_node_addr);
175 
176  params.payload = packetbuf_dataptr();
177  params.payload_len = packetbuf_datalen();
178  hdr_len = frame802154_hdrlen(&params);
179  if(!do_create) {
180  /* Only calculate header length */
181  return hdr_len;
182  } else if(packetbuf_hdralloc(hdr_len)) {
184 
185  PRINTF("15.4-OUT: %2X", params.fcf.frame_type);
186  PRINTADDR(params.dest_addr);
187  PRINTF("%d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
188 
189  return hdr_len;
190  } else {
191  PRINTF("15.4-OUT: too large header: %u\n", hdr_len);
192  return FRAMER_FAILED;
193  }
194 }
195 /*---------------------------------------------------------------------------*/
196 static int
197 hdr_length(void)
198 {
199  return create_frame(FRAME802154_DATAFRAME, 0);
200 }
201 /*---------------------------------------------------------------------------*/
202 static int
203 create(void)
204 {
205  return create_frame(FRAME802154_DATAFRAME, 1);
206 }
207 /*---------------------------------------------------------------------------*/
208 static int
209 parse(void)
210 {
211  frame802154_t frame;
212  int hdr_len;
213 
214  hdr_len = frame802154_parse(packetbuf_dataptr(), packetbuf_datalen(), &frame);
215 
216  if(hdr_len && packetbuf_hdrreduce(hdr_len)) {
217  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, frame.fcf.frame_type);
218 
219  if(frame.fcf.dest_addr_mode) {
220  if(frame.dest_pid != frame802154_get_pan_id() &&
221  frame.dest_pid != FRAME802154_BROADCASTPANDID) {
222  /* Packet to another PAN */
223  PRINTF("15.4: for another pan %u\n", frame.dest_pid);
224  return FRAMER_FAILED;
225  }
226  if(!frame802154_is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
227  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
228  }
229  }
230  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
231  packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending);
232  if(frame.fcf.sequence_number_suppression == 0) {
233  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, frame.seq);
234  } else {
235  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, 0xffff);
236  }
237 #if NETSTACK_CONF_WITH_RIME
238  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
239 #endif
240 
241 #if LLSEC802154_USES_AUX_HEADER
242  if(frame.fcf.security_enabled) {
243  packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, frame.aux_hdr.security_control.security_level);
244 #if LLSEC802154_USES_FRAME_COUNTER
245  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, frame.aux_hdr.frame_counter.u16[0]);
246  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, frame.aux_hdr.frame_counter.u16[1]);
247 #endif /* LLSEC802154_USES_FRAME_COUNTER */
248 #if LLSEC802154_USES_EXPLICIT_KEYS
249  packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, frame.aux_hdr.security_control.key_id_mode);
250  packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, frame.aux_hdr.key_index);
251  packetbuf_set_attr(PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1, frame.aux_hdr.key_source.u16[0]);
252 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
253  }
254 #endif /* LLSEC802154_USES_AUX_HEADER */
255 
256  PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
257  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
258  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
259  PRINTF("%d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
260 
261  return hdr_len;
262  }
263  return FRAMER_FAILED;
264 }
265 /*---------------------------------------------------------------------------*/
266 const struct framer framer_802154 = {
267  hdr_length,
268  create,
269  parse
270 };
271 /*---------------------------------------------------------------------------*/
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
int frame802154_hdrlen(frame802154_t *p)
Calculates the length of the frame header.
Definition: frame802154.c:349
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
frame802154_frame_counter_t frame_counter
Frame counter, used for security.
Definition: frame802154.h:183
int payload_len
Length of payload field.
Definition: frame802154.h:204
uint8_t frame_pending
1 bit.
Definition: frame802154.h:149
uint8_t src_addr[8]
Source address.
Definition: frame802154.h:197
uint8_t seq
Sequence number.
Definition: frame802154.h:199
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:182
uint8_t * payload
Pointer to 802.15.4 payload.
Definition: frame802154.h:203
Common functionality of 802.15.4-compliant llsec_drivers.
int packetbuf_holds_broadcast(void)
Checks whether the current packet is a broadcast.
Definition: packetbuf.c:242
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:164
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
frame802154_key_source_t key_source
Key Source subfield.
Definition: frame802154.h:184
uint8_t frame_type
3 bit.
Definition: frame802154.h:147
802.15.4 frame creation and parsing functions
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:47
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
static uint8_t mac_dsn
The sequence number (0x00 - 0xff) added to the transmitted data or MAC command frame.
Definition: framer-802154.c:61
uint8_t panid_compression
1 bit.
Definition: frame802154.h:151
uint8_t ack_required
1 bit.
Definition: frame802154.h:150
static int create_frame(int type, int do_create)
Definition: framer-802154.c:67
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:122
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
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:139
Parameters used by the frame802154_create() function.
Definition: frame802154.h:192
uint8_t frame_counter_suppression
1 bit.
Definition: frame802154.h:164
A MAC framer for IEEE 802.15.4
uint8_t sequence_number_suppression
< 1 bit.
Definition: frame802154.h:153