Contiki-NG
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 
41 #include "net/mac/llsec802154.h"
42 #include "net/packetbuf.h"
43 #include "lib/random.h"
44 #include <string.h>
45 
46 #include "sys/log.h"
47 #define LOG_MODULE "Frame 15.4"
48 #define LOG_LEVEL LOG_LEVEL_FRAMER
49 
50 /** \brief The sequence number (0x00 - 0xff) added to the transmitted
51  * data or MAC command frame. The default is a random value within
52  * the range.
53  */
54 static uint8_t mac_dsn;
55 
56 static uint8_t initialized = 0;
57 
58 /*---------------------------------------------------------------------------*/
59 static int
60 create_frame(int do_create)
61 {
62  frame802154_t params;
63  int hdr_len;
64 
65  if(frame802154_get_pan_id() == 0xffff) {
66  return -1;
67  }
68 
69  /* init to zeros */
70  memset(&params, 0, sizeof(params));
71 
72  if(!initialized) {
73  initialized = 1;
74  mac_dsn = random_rand() & 0xff;
75  }
76 
77  /*
78  * Before setting up "params", make sure we won't use 0 as the sequence number
79  * of a newly created frame.
80  *
81  * The case when do_create is 0 is ignored since it's for only length
82  * calculation. No sequence number is needed and should not be consumed.
83  */
84  if(do_create != 0 && packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO) == 0) {
85  if(mac_dsn == 0) {
86  mac_dsn++;
87  }
88  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, mac_dsn);
89  mac_dsn++;
90  }
91 
92  framer_802154_setup_params(packetbuf_attr, packetbuf_holds_broadcast(),
93  &params);
94 
96  params.dest_addr[0] = 0xFF;
97  params.dest_addr[1] = 0xFF;
98  } else {
99  linkaddr_copy((linkaddr_t *)&params.dest_addr,
100  packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
101  }
102 
103  linkaddr_copy((linkaddr_t *)&params.src_addr,
104  packetbuf_addr(PACKETBUF_ADDR_SENDER));
105 
106  params.payload = packetbuf_dataptr();
107  params.payload_len = packetbuf_datalen();
108  hdr_len = frame802154_hdrlen(&params);
109  if(!do_create) {
110  /* Only calculate header length */
111  return hdr_len;
112  } else if(packetbuf_hdralloc(hdr_len)) {
114 
115  LOG_INFO("Out: %2X ", params.fcf.frame_type);
116  LOG_INFO_LLADDR((const linkaddr_t *)params.dest_addr);
117  LOG_INFO_(" %d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
118 
119  return hdr_len;
120  } else {
121  LOG_ERR("Out: too large header: %u\n", hdr_len);
122  return FRAMER_FAILED;
123  }
124 }
125 /*---------------------------------------------------------------------------*/
126 void
127 framer_802154_setup_params(packetbuf_attr_t (*get_attr)(uint8_t type),
128  uint8_t dest_is_broadcast, frame802154_t *params)
129 {
130  if(get_attr == NULL || params == NULL) {
131  LOG_INFO("framer-802154: cannot setup params because of invalid argument\n");
132  return;
133  }
134 
135  /*
136  * Don't initialize params with 0 because a caller may have already set
137  * something to it
138  */
139 
140  /* Build the FCF. */
141  params->fcf.frame_type = get_attr(PACKETBUF_ATTR_FRAME_TYPE);
142  params->fcf.frame_pending = 0;
143  if(dest_is_broadcast) {
144  params->fcf.ack_required = 0;
145  /* Suppress seqno on broadcast if supported (frame v2 or more) */
146  params->fcf.sequence_number_suppression = FRAME802154_VERSION >= FRAME802154_IEEE802154_2015;
147  } else {
148  params->fcf.ack_required = get_attr(PACKETBUF_ATTR_MAC_ACK);
149  params->fcf.sequence_number_suppression = FRAME802154_SUPPR_SEQNO;
150  }
151 
152  /* Set IE Present bit */
153  params->fcf.ie_list_present = get_attr(PACKETBUF_ATTR_MAC_METADATA);
154 
155  /* Insert IEEE 802.15.4 version bits. */
156  params->fcf.frame_version = FRAME802154_VERSION;
157 
158 #if LLSEC802154_USES_AUX_HEADER
159  if(get_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
160  params->fcf.security_enabled = 1;
161  }
162  /* Setting security-related attributes */
163  params->aux_hdr.security_control.security_level = get_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
164 #if LLSEC802154_USES_FRAME_COUNTER
165  params->aux_hdr.frame_counter.u16[0] = get_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1);
166  params->aux_hdr.frame_counter.u16[1] = get_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3);
167 #else /* LLSEC802154_USES_FRAME_COUNTER */
170 #endif /* LLSEC802154_USES_FRAME_COUNTER */
171 #if LLSEC802154_USES_EXPLICIT_KEYS
172  params->aux_hdr.security_control.key_id_mode = get_attr(PACKETBUF_ATTR_KEY_ID_MODE);
173  params->aux_hdr.key_index = get_attr(PACKETBUF_ATTR_KEY_INDEX);
174 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
175 #else
176  params->fcf.security_enabled = 0;
177 #endif /* LLSEC802154_USES_AUX_HEADER */
178 
179  params->seq = get_attr(PACKETBUF_ATTR_MAC_SEQNO);
180 
181  /* Set the source PAN ID to the global variable. */
182  params->src_pid = frame802154_get_pan_id();
183 
184  /* Source address itself should be set outside this function. */
185  if(get_attr(PACKETBUF_ATTR_MAC_NO_SRC_ADDR) == 1) {
187  } else {
188  if(LINKADDR_SIZE == 2) {
189  /* Use short address mode if linkaddr size is short. */
190  params->fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
191  } else {
192  params->fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
193  }
194  }
195 
196  params->dest_pid = frame802154_get_pan_id();
197 
198  /* Destination address itself should be set outside this function. */
199  if(get_attr(PACKETBUF_ATTR_MAC_NO_DEST_ADDR) == 1) {
201  } else if(dest_is_broadcast) {
202  /* Broadcast requires short address mode. */
203  params->fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
204  } else {
205  if(LINKADDR_SIZE == 2) {
206  params->fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
207  } else {
208  params->fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
209  }
210  }
211 
212  /* Suppress Source PAN ID and put Destination PAN ID by default */
213  if(params->fcf.src_addr_mode == FRAME802154_SHORTADDRMODE ||
214  params->fcf.dest_addr_mode == FRAME802154_SHORTADDRMODE) {
215  params->fcf.panid_compression = 1;
216  } else {
217  params->fcf.panid_compression = 0;
218  }
219 }
220 /*---------------------------------------------------------------------------*/
221 static int
222 hdr_length(void)
223 {
224  return create_frame(0);
225 }
226 /*---------------------------------------------------------------------------*/
227 static int
228 create(void)
229 {
230  return create_frame(1);
231 }
232 /*---------------------------------------------------------------------------*/
233 static int
234 parse(void)
235 {
236  frame802154_t frame;
237  int hdr_len;
238 
239  hdr_len = frame802154_parse(packetbuf_dataptr(), packetbuf_datalen(), &frame);
240 
241  if(hdr_len && packetbuf_hdrreduce(hdr_len)) {
242  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, frame.fcf.frame_type);
243  packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, frame.fcf.ack_required);
244 
245  if(frame.fcf.dest_addr_mode) {
246  if(frame.dest_pid != frame802154_get_pan_id() &&
247  frame.dest_pid != FRAME802154_BROADCASTPANDID) {
248  /* Packet to another PAN */
249  LOG_WARN("15.4: for another pan %u\n", frame.dest_pid);
250  return FRAMER_FAILED;
251  }
252  if(!frame802154_is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
253  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
254  }
255  }
256  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
257  if(frame.fcf.sequence_number_suppression == 0) {
258  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, frame.seq);
259  } else {
260  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, 0xffff);
261  }
262 #if NETSTACK_CONF_WITH_RIME
263  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
264 #endif
265 
266 #if LLSEC802154_USES_AUX_HEADER
267  if(frame.fcf.security_enabled) {
268  packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, frame.aux_hdr.security_control.security_level);
269 #if LLSEC802154_USES_FRAME_COUNTER
270  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, frame.aux_hdr.frame_counter.u16[0]);
271  packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, frame.aux_hdr.frame_counter.u16[1]);
272 #endif /* LLSEC802154_USES_FRAME_COUNTER */
273 #if LLSEC802154_USES_EXPLICIT_KEYS
274  packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, frame.aux_hdr.security_control.key_id_mode);
275  packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, frame.aux_hdr.key_index);
276 #endif /* LLSEC802154_USES_EXPLICIT_KEYS */
277  }
278 #endif /* LLSEC802154_USES_AUX_HEADER */
279 
280  LOG_INFO("In: %2X ", frame.fcf.frame_type);
281  LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
282  LOG_INFO_(" ");
283  LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
284  LOG_INFO_(" %d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
285 
286  return hdr_len;
287  }
288  return FRAMER_FAILED;
289 }
290 /*---------------------------------------------------------------------------*/
291 const struct framer framer_802154 = {
292  hdr_length,
293  create,
294  parse
295 };
296 /*---------------------------------------------------------------------------*/
frame802154_frame_counter_t frame_counter
Frame counter, used for security.
Definition: frame802154.h:189
uint16_t src_pid
Source PAN ID.
Definition: frame802154.h:207
frame802154_scf_t security_control
Security control bitfield.
Definition: frame802154.h:188
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
uint8_t dest_addr_mode
2 bit.
Definition: frame802154.h:161
uint8_t sequence_number_suppression
< 1 bit.
Definition: frame802154.h:159
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:204
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:107
#define FRAME802154_NOADDR
Only valid for ACK or Beacon frames.
Definition: frame802154.h:109
uint8_t security_level
3 bit.
Definition: frame802154.h:168
uint8_t security_enabled
1 bit.
Definition: frame802154.h:154
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:124
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:500
int payload_len
Length of payload field.
Definition: frame802154.h:210
Common functionality of 802.15.4-compliant llsec_drivers.
uint8_t frame_pending
1 bit.
Definition: frame802154.h:155
uint8_t src_addr[8]
Source address.
Definition: frame802154.h:203
int frame802154_hdrlen(frame802154_t *p)
Calculates the length of the frame header.
Definition: frame802154.c:358
A MAC framer for IEEE 802.15.4
uint8_t key_id_mode
2 bit.
Definition: frame802154.h:169
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:155
uint8_t key_index
Key Index subfield.
Definition: frame802154.h:191
uint8_t frame_counter_suppression
1 bit.
Definition: frame802154.h:170
uint8_t frame_version
2 bit.
Definition: frame802154.h:162
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:167
static uint8_t mac_dsn
The sequence number (0x00 - 0xff) added to the transmitted data or MAC command frame.
Definition: framer-802154.c:54
uint8_t src_addr_mode
2 bit.
Definition: frame802154.h:163
802.15.4 frame creation and parsing functions
int packetbuf_holds_broadcast(void)
Checks whether the current packet is a broadcast.
Definition: packetbuf.c:231
uint8_t frame_type
3 bit.
Definition: frame802154.h:153
uint16_t dest_pid
Destination PAN ID.
Definition: frame802154.h:206
Parameters used by the frame802154_create() function.
Definition: frame802154.h:198
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:149
uint8_t ie_list_present
1 bit.
Definition: frame802154.h:160
uint8_t seq
Sequence number.
Definition: frame802154.h:205
uint8_t panid_compression
1 bit.
Definition: frame802154.h:157
Header file for the Packet buffer (packetbuf) management
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
uint8_t * payload
Pointer to 802.15.4 payload.
Definition: frame802154.h:209
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:202
Header file for the logging system
uint8_t ack_required
1 bit.
Definition: frame802154.h:156
uint8_t frame_counter_size
1 bit.
Definition: frame802154.h:171
frame802154_aux_hdr_t aux_hdr
Aux security header.
Definition: frame802154.h:208
int frame802154_create(frame802154_t *p, uint8_t *buf)
Creates a frame for transmission over the air.
Definition: frame802154.c:392