sixlowpan_print.c
7.28 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include <stdio.h>
#include <inttypes.h>
#include "od.h"
#include "net/ipv6/hdr.h"
#include "net/sixlowpan.h"
void sixlowpan_print(uint8_t *data, size_t size)
{
if (data[0] == SIXLOWPAN_UNCOMP) {
puts("Uncompressed IPv6 packet");
/* might just be the dispatch (or fragmented) so better check */
if (size > sizeof(ipv6_hdr_t)) {
ipv6_hdr_print((ipv6_hdr_t *)(data + 1));
od_hex_dump(data + sizeof(ipv6_hdr_t) + 1,
size - sizeof(ipv6_hdr_t) - 1,
OD_WIDTH_DEFAULT);
}
}
else if (sixlowpan_nalp(data[0])) {
puts("Not a LoWPAN (NALP) frame");
od_hex_dump(data, size, OD_WIDTH_DEFAULT);
}
else if ((data[0] & SIXLOWPAN_FRAG_DISP_MASK) == SIXLOWPAN_FRAG_1_DISP) {
sixlowpan_frag_t *hdr = (sixlowpan_frag_t *)data;
puts("Fragmentation Header (first)");
printf("datagram size: %" PRIu16 "\n",
(uint16_t) (byteorder_ntohs(hdr->disp_size) & SIXLOWPAN_FRAG_SIZE_MASK));
printf("tag: 0x%" PRIu16 "\n", byteorder_ntohs(hdr->tag));
/* Print next dispatch */
sixlowpan_print(data + sizeof(sixlowpan_frag_t),
size - sizeof(sixlowpan_frag_t));
}
else if ((data[0] & SIXLOWPAN_FRAG_DISP_MASK) == SIXLOWPAN_FRAG_N_DISP) {
sixlowpan_frag_n_t *hdr = (sixlowpan_frag_n_t *)data;
puts("Fragmentation Header (subsequent)");
printf("datagram size: %" PRIu16 "\n",
(uint16_t) (byteorder_ntohs(hdr->disp_size) & SIXLOWPAN_FRAG_SIZE_MASK));
printf("tag: 0x%" PRIu16 "\n", byteorder_ntohs(hdr->tag));
printf("offset: 0x%u\n", (unsigned)hdr->offset);
od_hex_dump(data + sizeof(sixlowpan_frag_n_t),
size - sizeof(sixlowpan_frag_n_t),
OD_WIDTH_DEFAULT);
}
else if ((data[0] & SIXLOWPAN_IPHC1_DISP_MASK) == SIXLOWPAN_IPHC1_DISP) {
uint8_t offset = SIXLOWPAN_IPHC_HDR_LEN;
puts("IPHC dispatch");
switch (data[0] & SIXLOWPAN_IPHC1_TF) {
case 0x00:
puts("TF: ECN + DSCP + Flow Label (4 bytes)");
break;
case 0x08:
puts("TF: ECN + Flow Label (3 bytes)");
break;
case 0x10:
puts("TF: ECN + DSCP (1 bytes)");
break;
case 0x18:
puts("TF: traffic class and flow label elided");
break;
}
switch (data[0] & SIXLOWPAN_IPHC1_NH) {
case 0x00:
puts("NH: inline");
break;
case 0x04:
puts("NH: LOWPAN_NHC");
break;
}
switch (data[0] & SIXLOWPAN_IPHC1_HL) {
case 0x00:
puts("HLIM: inline");
break;
case 0x01:
puts("HLIM: 1");
break;
case 0x02:
puts("HLIM: 64");
break;
case 0x03:
puts("HLIM: 255");
break;
}
if (data[1] & SIXLOWPAN_IPHC2_SAC) {
printf("Stateful source address compression: ");
switch (data[1] & SIXLOWPAN_IPHC2_SAM) {
case 0x00:
puts("unspecified address (::)");
break;
case 0x10:
puts("64 bits inline");
break;
case 0x20:
puts("16 bits inline");
break;
case 0x30:
puts("elided (use L2 address)");
break;
}
}
else {
printf("Stateless source address compression: ");
switch (data[1] & SIXLOWPAN_IPHC2_SAM) {
case 0x00:
puts("128 bits inline");
break;
case 0x10:
puts("64 bits inline");
break;
case 0x20:
puts("16 bits inline");
break;
case 0x30:
puts("elided (use L2 address)");
break;
}
}
if (data[1] & SIXLOWPAN_IPHC2_M) {
if (data[1] & SIXLOWPAN_IPHC2_DAC) {
puts("Stateful destinaton multicast address compression:");
switch (data[1] & SIXLOWPAN_IPHC2_DAM) {
case 0x00:
puts(" 48 bits carried inline (Unicast-Prefix-based)");
break;
case 0x01:
case 0x02:
case 0x03:
puts(" reserved");
break;
}
}
else {
puts("Stateless destinaton multicast address compression:");
switch (data[1] & SIXLOWPAN_IPHC2_DAM) {
case 0x00:
puts(" 128 bits carried inline");
break;
case 0x01:
puts(" 48 bits carried inline");
break;
case 0x02:
puts(" 32 bits carried inline");
break;
case 0x03:
puts(" 8 bits carried inline");
break;
}
}
}
else {
if (data[1] & SIXLOWPAN_IPHC2_DAC) {
printf("Stateful destinaton address compression: ");
switch (data[1] & SIXLOWPAN_IPHC2_DAM) {
case 0x00:
puts("reserved");
break;
case 0x10:
puts("64 bits inline");
break;
case 0x20:
puts("16 bits inline");
break;
case 0x30:
puts("elided (use L2 address)");
break;
}
}
else {
printf("Stateless destinaton address compression: ");
switch (data[1] & SIXLOWPAN_IPHC2_DAM) {
case 0x00:
puts("128 bits inline");
break;
case 0x10:
puts("64 bits inline");
break;
case 0x20:
puts("16 bits inline");
break;
case 0x30:
puts("elided (use L2 address)");
break;
}
}
}
if (data[1] & SIXLOWPAN_IPHC2_CID_EXT) {
offset += SIXLOWPAN_IPHC_CID_EXT_LEN;
printf("SCI: 0x%x, DCI: 0x%x\n", (unsigned)(data[2] >> 4),
(unsigned)(data[2] & 0xf));
}
od_hex_dump(data + offset, size - offset, OD_WIDTH_DEFAULT);
}
}
/** @} */