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