Contiki-NG
Loading...
Searching...
No Matches
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 params->fcf.panid_compression =
186 params->fcf.src_addr_mode == FRAME802154_SHORTADDRMODE ||
187 params->fcf.dest_addr_mode == FRAME802154_SHORTADDRMODE;
188}
189/*---------------------------------------------------------------------------*/
190static int
191hdr_length(void)
192{
193 return create_frame(0);
194}
195/*---------------------------------------------------------------------------*/
196static int
197create(void)
198{
199 return create_frame(1);
200}
201/*---------------------------------------------------------------------------*/
202static int
203parse(void)
204{
205 frame802154_t frame;
206 int hdr_len;
207
209
210 if(hdr_len && packetbuf_hdrreduce(hdr_len)) {
211 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, frame.fcf.frame_type);
212 packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, frame.fcf.ack_required);
213
214 if(frame.fcf.dest_addr_mode) {
215 if(frame.dest_pid != frame802154_get_pan_id() &&
216 frame.dest_pid != FRAME802154_BROADCASTPANDID) {
217 /* Packet to another PAN */
218 LOG_WARN("15.4: for another pan %u\n", frame.dest_pid);
219 return FRAMER_FAILED;
220 }
221 if(!frame802154_is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
222 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (linkaddr_t *)&frame.dest_addr);
223 }
224 }
225 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (linkaddr_t *)&frame.src_addr);
226 if(frame.fcf.sequence_number_suppression == 0) {
227 packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, frame.seq);
228 } else {
229 packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, 0xffff);
230 }
231
232#if LLSEC802154_USES_AUX_HEADER
233 if(frame.fcf.security_enabled) {
234 packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, frame.aux_hdr.security_control.security_level);
235#if LLSEC802154_USES_FRAME_COUNTER
236 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1, frame.aux_hdr.frame_counter.u16[0]);
237 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3, frame.aux_hdr.frame_counter.u16[1]);
238#endif /* LLSEC802154_USES_FRAME_COUNTER */
239#if LLSEC802154_USES_EXPLICIT_KEYS
240 packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, frame.aux_hdr.security_control.key_id_mode);
241 packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, frame.aux_hdr.key_index);
242#endif /* LLSEC802154_USES_EXPLICIT_KEYS */
243 }
244#endif /* LLSEC802154_USES_AUX_HEADER */
245
246 LOG_INFO("In: %2X ", frame.fcf.frame_type);
247 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
248 LOG_INFO_(" ");
249 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
250 LOG_INFO_(" %d %u (%u)\n", hdr_len, packetbuf_datalen(), packetbuf_totlen());
251
252 return hdr_len;
253 }
254 return FRAMER_FAILED;
255}
256/*---------------------------------------------------------------------------*/
257const struct framer framer_802154 = {
258 hdr_length,
259 create,
260 parse
261};
262/*---------------------------------------------------------------------------*/
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.
#define FRAME802154_NOADDR
Only valid for ACK or Beacon frames.
int frame802154_create(frame802154_t *p, uint8_t *buf)
Creates a frame for transmission over the air.
int frame802154_hdrlen(frame802154_t *p)
Calculates the length of the frame header.
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:229
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.
frame802154_frame_counter_t frame_counter
Frame counter, used for security.
uint8_t key_index
Key Index subfield.
uint8_t frame_type
3 bit.
uint8_t frame_version
2 bit.
uint8_t ie_list_present
1 bit.
uint8_t security_enabled
1 bit.
uint8_t sequence_number_suppression
< 1 bit.
uint8_t src_addr_mode
2 bit.
uint8_t panid_compression
1 bit.
uint8_t ack_required
1 bit.
uint8_t dest_addr_mode
2 bit.
uint8_t frame_pending
1 bit.
uint8_t key_id_mode
2 bit.
uint8_t frame_counter_size
1 bit.
uint8_t frame_counter_suppression
1 bit.
uint8_t security_level
3 bit.
Parameters used by the frame802154_create() function.
uint8_t seq
Sequence number.
uint8_t dest_addr[8]
Destination address.
frame802154_aux_hdr_t aux_hdr
Aux security header.
uint8_t * payload
Pointer to 802.15.4 payload.
uint8_t src_addr[8]
Source address.
uint16_t src_pid
Source PAN ID.
frame802154_fcf_t fcf
Frame control field
uint16_t dest_pid
Destination PAN ID.
int payload_len
Length of payload field.