-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmac-decode.c
138 lines (110 loc) · 3.61 KB
/
mac-decode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* File: mac-decode.c
Copyright (C) 2013 David Hauweele <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#define _BSD_SOURCE
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "sys-endian.h"
#include "mac.h"
#include "common.h"
/* Check if the new pointer is within the range of the orig buffer. */
#define CHECK(orig, new, size) if(new > orig + size) return -1
static int decode_pan(struct mac_addr *addr, const unsigned char *raw_frame,
uint16_t *pan)
{
if(!pan) {
addr->pan = le16toh(U8_TO(uint16_t, raw_frame));
return 2;
} else {
addr->pan = *pan;
return 0;
}
}
static int decode_address(struct mac_addr *addr, enum mac_addr_mode mode,
const unsigned char *raw_frame, uint16_t *pan)
{
const unsigned char *raw = raw_frame;
switch(mode) {
case MAM_FULL:
return 0;
case MAM_SHORT:
raw += decode_pan(addr, raw, pan);
addr->mac = le16toh(U8_TO(uint16_t, raw));
raw += sizeof(uint16_t);
break;
case MAM_LONG:
case MAM_RESERVED:
raw += decode_pan(addr, raw, pan);
addr->mac = le64toh(U8_TO(uint64_t, raw));
raw += sizeof(uint64_t);
break;
default:
assert(0);
}
return raw - raw_frame;
}
int mac_decode(struct mac_frame *frame, const unsigned char *raw_frame,
bool decode_crc, unsigned int size)
{
size_t payload_size;
const unsigned char *raw = raw_frame;
/* We have to initialize the payload to avoid
a buffer overflow with crafted frames. */
frame->payload = NULL;
frame->control = le16toh(U8_TO(uint16_t, raw));
raw += sizeof(uint16_t);
CHECK(raw_frame, raw, size);
frame->seqno = *raw;
raw++;
CHECK(raw_frame, raw, size);
raw += decode_address(&frame->dst, (frame->control & MC_DAM) >> MC_DAM_SHR,
raw, NULL);
CHECK(raw_frame, raw, size);
raw += decode_address(&frame->src,
(frame->control & MC_SAM) >> MC_SAM_SHR, raw,
(frame->control & MC_PANCOMP) ? &frame->dst.pan : NULL);
if(frame->control & MC_SECURITY) {
/* Auxiliary security headers are not implemented. */
return -1;
}
frame->security = NULL;
if(decode_crc)
payload_size = size - sizeof(uint16_t) - (raw - raw_frame);
else
payload_size = size - (raw - raw_frame);
/* We only copy the payload when needed. */
if(payload_size) {
frame->payload = malloc(payload_size);
memcpy((void *)frame->payload, raw, payload_size);
frame->size = payload_size;
}
/* Check for an overflow which may arise with crafted frames.
That is minus one converted to unsigned int which would.
result in a very large payload. */
if(frame->size > 0xff)
frame->payload = NULL;
else
raw += frame->size;
CHECK(raw_frame, raw, size);
if(decode_crc) {
frame->fcs = be16toh(U8_TO(uint16_t, raw));
CHECK(raw_frame, raw+2, size);
}
return 0;
}
void free_mac_frame(struct mac_frame *frame)
{
if(frame->payload)
free((void *)frame->payload);
}