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 /** \brief The 16-bit identifier of the PAN on which the device is
66  * sending to. If this value is 0xffff, the device is not
67  * associated.
68  */
69 static const uint16_t mac_dst_pan_id = IEEE802154_PANID;
70 
71 /** \brief The 16-bit identifier of the PAN on which the device is
72  * operating. If this value is 0xffff, the device is not
73  * associated.
74  */
75 static const uint16_t mac_src_pan_id = IEEE802154_PANID;
76 
77 /*---------------------------------------------------------------------------*/
78 static int
79 is_broadcast_addr(uint8_t mode, uint8_t *addr)
80 {
81  int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
82  while(i-- > 0) {
83  if(addr[i] != 0xff) {
84  return 0;
85  }
86  }
87  return 1;
88 }
89 /*---------------------------------------------------------------------------*/
90 static int
91 create_frame(int type, int do_create)
92 {
93  frame802154_t params;
94  int hdr_len;
95 
96  /* init to zeros */
97  memset(&params, 0, sizeof(params));
98 
99  if(!initialized) {
100  initialized = 1;
101  mac_dsn = random_rand() & 0xff;
102  }
103 
104  /* Build the FCF. */
105  params.fcf.frame_type = packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE);
106  params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
107  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_null)) {
108  params.fcf.ack_required = 0;
109  } else {
110  params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK);
111  }
112  params.fcf.panid_compression = 0;
113 
114  /* Insert IEEE 802.15.4 (2006) version bits. */
115  params.fcf.frame_version = FRAME802154_IEEE802154_2006;
116 
117 #if LLSEC802154_SECURITY_LEVEL
118  if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
119  params.fcf.security_enabled = 1;
120  }
121  /* Setting security-related attributes */
122  params.aux_hdr.security_control.security_level = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
123  params.aux_hdr.frame_counter.u16[0] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1);
124  params.aux_hdr.frame_counter.u16[1] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3);
125 #if LLSEC802154_USES_EXPLICIT_KEYS
126  params.aux_hdr.security_control.key_id_mode = packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE);
127  params.aux_hdr.key_index = packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX);
128  params.aux_hdr.key_source.u16[0] = packetbuf_attr(PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1);
129 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
130 #endif /* LLSEC802154_SECURITY_LEVEL */
131 
132  /* Increment and set the data sequence number. */
133  if(!do_create) {
134  /* Only length calculation - no sequence number is needed and
135  should not be consumed. */
136 
137  } else if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
138  params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
139 
140  } else {
141  /* Ensure that the sequence number 0 is not used as it would bypass the above check. */
142  if(mac_dsn == 0) {
143  mac_dsn++;
144  }
145  params.seq = mac_dsn++;
146  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq);
147  }
148 
149  /* Complete the addressing fields. */
150  /**
151  \todo For phase 1 the addresses are all long. We'll need a mechanism
152  in the rime attributes to tell the mac to use long or short for phase 2.
153  */
154  if(LINKADDR_SIZE == 2) {
155  /* Use short address mode if linkaddr size is short. */
156  params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
157  } else {
158  params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
159  }
160  params.dest_pid = mac_dst_pan_id;
161 
162  /*
163  * If the output address is NULL in the Rime buf, then it is broadcast
164  * on the 802.15.4 network.
165  */
166  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_null)) {
167  /* Broadcast requires short address mode. */
168  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
169  params.dest_addr[0] = 0xFF;
170  params.dest_addr[1] = 0xFF;
171 
172  } else {
173  linkaddr_copy((linkaddr_t *)&params.dest_addr,
174  packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
175  /* Use short address mode if linkaddr size is small */
176  if(LINKADDR_SIZE == 2) {
177  params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
178  } else {
179  params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
180  }
181  }
182 
183  /* Set the source PAN ID to the global variable. */
184  params.src_pid = mac_src_pan_id;
185 
186  /*
187  * Set up the source address using only the long address mode for
188  * phase 1.
189  */
190  linkaddr_copy((linkaddr_t *)&params.src_addr, &linkaddr_node_addr);
191 
192  params.payload = packetbuf_dataptr();
193  params.payload_len = packetbuf_datalen();
194  hdr_len = frame802154_hdrlen(&params);
195  if(!do_create) {
196  /* Only calculate header length */
197  return hdr_len;
198 
199  } else if(packetbuf_hdralloc(hdr_len)) {
201 
202  PRINTF("15.4-OUT: %2X", params.fcf.frame_type);
203  PRINTADDR(params.dest_addr);
204  PRINTF("%d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
205 
206  return hdr_len;
207  } else {
208  PRINTF("15.4-OUT: too large header: %u\n", hdr_len);
209  return FRAMER_FAILED;
210  }
211 }
212 /*---------------------------------------------------------------------------*/
213 static int
214 hdr_length(void)
215 {
216  return create_frame(FRAME802154_DATAFRAME, 0);
217 }
218 /*---------------------------------------------------------------------------*/
219 static int
220 create(void)
221 {
222  return create_frame(FRAME802154_DATAFRAME, 1);
223 }
224 /*---------------------------------------------------------------------------*/
225 static int
226 parse(void)
227 {
228  frame802154_t frame;
229  int hdr_len;
230 
231  hdr_len = frame802154_parse(packetbuf_dataptr(), packetbuf_datalen(), &frame);
232 
233  if(hdr_len && packetbuf_hdrreduce(hdr_len)) {
234  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, frame.fcf.frame_type);
235 
236  if(frame.fcf.dest_addr_mode) {
237  if(frame.dest_pid != mac_src_pan_id &&
238  frame.dest_pid != FRAME802154_BROADCASTPANDID) {
239  /* Packet to another PAN */
240  PRINTF("15.4: for another pan %u\n", frame.dest_pid);
241  return FRAMER_FAILED;
242  }
243  if(!is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
244  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
245  }
246  }
247  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
248  packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending);
249  /* packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, frame.fcf.ack_required);*/
250  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
251 
252 #if LLSEC802154_SECURITY_LEVEL
253  if(frame.fcf.security_enabled) {
254  packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, frame.aux_hdr.security_control.security_level);
255  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, frame.aux_hdr.frame_counter.u16[0]);
256  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, frame.aux_hdr.frame_counter.u16[1]);
257 #if LLSEC802154_USES_EXPLICIT_KEYS
258  packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, frame.aux_hdr.security_control.key_id_mode);
259  packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, frame.aux_hdr.key_index);
260  packetbuf_set_attr(PACKETBUF_ATTR_KEY_SOURCE_BYTES_0_1, frame.aux_hdr.key_source.u16[0]);
261 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
262  }
263 #endif /* LLSEC802154_SECURITY_LEVEL */
264 
265  PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
266  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
267  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
268  PRINTF("%d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
269 
270  return hdr_len;
271  }
272  return FRAMER_FAILED;
273 }
274 /*---------------------------------------------------------------------------*/
275 const struct framer framer_802154 = {
276  hdr_length,
277  create,
278  framer_canonical_create_and_secure,
279  parse
280 };
281 /*---------------------------------------------------------------------------*/
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:172
frame802154_frame_counter_t frame_counter
Frame counter, used for security.
Definition: frame802154.h:166
uint8_t * payload
Pointer to 802.15.4 payload.
Definition: frame802154.h:186
A MAC framer for IEEE 802.15.4
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
802.15.4 frame creation and parsing functions
uint8_t ack_required
1 bit.
Definition: frame802154.h:137
frame802154_key_source_t key_source
Key Source subfield.
Definition: frame802154.h:167
uint8_t frame_version
2 bit.
Definition: frame802154.h:141
const linkaddr_t linkaddr_null
The null Rime address.
uint8_t frame_type
3 bit.
Definition: frame802154.h:134
int frame802154_hdrlen(frame802154_t *p)
Calculates the length of the frame header.
Definition: frame802154.c:166
Header file for the Rime buffer (packetbuf) management
uint8_t security_enabled
1 bit.
Definition: frame802154.h:135
int frame802154_create(frame802154_t *p, uint8_t *buf)
Creates a frame for transmission over the air.
Definition: frame802154.c:186
uint8_t src_addr_mode
2 bit.
Definition: frame802154.h:142
uint8_t panid_compression
1 bit.
Definition: frame802154.h:138
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
uint8_t key_id_mode
2 bit.
Definition: frame802154.h:148
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
Parameters used by the frame802154_create() function.
Definition: frame802154.h:175
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
uint8_t seq
Sequence number.
Definition: frame802154.h:182
int payload_len
Length of payload field.
Definition: frame802154.h:187
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
frame802154_scf_t security_control
Security control bitfield.
Definition: frame802154.h:165
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:179
uint8_t key_index
Key Index subfield.
Definition: frame802154.h:168
uint16_t dest_pid
Destination PAN ID.
Definition: frame802154.h:183
uint16_t src_pid
Source PAN ID.
Definition: frame802154.h:184
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:270
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
Common functionality of 802.15.4-compliant llsec_drivers.
uint8_t frame_pending
1 bit.
Definition: frame802154.h:136
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:181
uint8_t security_level
3 bit.
Definition: frame802154.h:147
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:188
uint8_t src_addr[8]
Source address.
Definition: frame802154.h:180
frame802154_aux_hdr_t aux_hdr
Aux security header.
Definition: frame802154.h:185
uint8_t dest_addr_mode
&lt; 3 bit.
Definition: frame802154.h:140