1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | #include <stdio.h> |
---|
27 | #include <inttypes.h> |
---|
28 | #include <dlfcn.h> |
---|
29 | #include "libpacketdump.h" |
---|
30 | |
---|
31 | #include <sys/socket.h> |
---|
32 | #include <netinet/in.h> |
---|
33 | #include <arpa/inet.h> |
---|
34 | #include <assert.h> |
---|
35 | |
---|
36 | /* SCTP decoding by Sam Jansen, 31/08/2004 |
---|
37 | * |
---|
38 | * Based on RFC 2960 - Stream Control Transmission Protocol |
---|
39 | */ |
---|
40 | |
---|
41 | struct sctp_common_hdr |
---|
42 | { |
---|
43 | uint16_t src_port, dst_port; |
---|
44 | uint32_t verification_tag; |
---|
45 | uint32_t checksum; |
---|
46 | } __attribute__((__packed__)); |
---|
47 | |
---|
48 | struct sctp_chunk_hdr |
---|
49 | { |
---|
50 | uint8_t type; |
---|
51 | uint8_t flags; |
---|
52 | uint16_t length; |
---|
53 | } __attribute__((__packed__)); |
---|
54 | |
---|
55 | struct sctp_data |
---|
56 | { |
---|
57 | uint32_t tsn; |
---|
58 | uint16_t stream_id; |
---|
59 | uint16_t stream_seqno; |
---|
60 | uint32_t payload_proto_id; |
---|
61 | } __attribute__((__packed__)); |
---|
62 | |
---|
63 | /* The following works for INIT and INIT ACK packets */ |
---|
64 | struct sctp_init_ack |
---|
65 | { |
---|
66 | uint32_t init_tag; |
---|
67 | uint32_t rcv_wnd_credit; |
---|
68 | uint16_t outbound_streams; |
---|
69 | uint16_t inbound_streams; |
---|
70 | uint32_t init_tsn; |
---|
71 | } __attribute__((__packed__)); |
---|
72 | |
---|
73 | struct sctp_sack |
---|
74 | { |
---|
75 | uint32_t tsn_ack; |
---|
76 | uint32_t a_wnd; |
---|
77 | uint16_t num_gap_blocks; |
---|
78 | uint16_t num_dup_tsns; |
---|
79 | } __attribute__((__packed__)); |
---|
80 | |
---|
81 | struct sctp_var_param_hdr |
---|
82 | { |
---|
83 | uint16_t type; |
---|
84 | uint16_t length; |
---|
85 | } __attribute__((__packed__)); |
---|
86 | |
---|
87 | static char *sctp_type_to_str(uint8_t type) |
---|
88 | { |
---|
89 | switch(type) |
---|
90 | { |
---|
91 | case 0: return "DATA"; |
---|
92 | case 1: return "INIT"; |
---|
93 | case 2: return "INIT ACK"; |
---|
94 | case 3: return "SACK"; |
---|
95 | case 4: return "HEARTBEAT"; |
---|
96 | case 5: return "HEARTBEAT ACK"; |
---|
97 | case 6: return "ABORT"; |
---|
98 | case 7: return "SHUTDOWN"; |
---|
99 | case 8: return "SHUTDOWN ACK"; |
---|
100 | case 9: return "ERROR"; |
---|
101 | case 10: return "COOKIE ECHO"; |
---|
102 | case 11: return "COOKIE ACK"; |
---|
103 | case 12: return "Reserved for ECNE"; |
---|
104 | case 13: return "Reserved for CWR"; |
---|
105 | case 14: return "SHUTDOWN COMPLETE"; |
---|
106 | case 63: |
---|
107 | case 127: |
---|
108 | case 191: |
---|
109 | case 255: return "IETF-defined Chunk Extensions"; |
---|
110 | }; |
---|
111 | |
---|
112 | return "reserved by IETF"; |
---|
113 | } |
---|
114 | |
---|
115 | static void parse_options(char *offset, int vlen) |
---|
116 | { |
---|
117 | while(vlen > 0) { |
---|
118 | struct sctp_var_param_hdr *ph = (struct sctp_var_param_hdr *)(offset); |
---|
119 | char *data = (char *)(ph + 1); |
---|
120 | |
---|
121 | switch(ntohs(ph->type)) { |
---|
122 | case 5: |
---|
123 | { |
---|
124 | struct in_addr *ia = (struct in_addr *)data; |
---|
125 | printf(" SCTP: Option IP address %s\n", inet_ntoa(*ia)); |
---|
126 | } |
---|
127 | break; |
---|
128 | case 6: |
---|
129 | { |
---|
130 | printf(" SCTP: Option IPv6 address (TODO)\n"); |
---|
131 | } |
---|
132 | break; |
---|
133 | case 7: |
---|
134 | { |
---|
135 | printf(" SCTP: Option State cookie\n"); |
---|
136 | /* // Prolly don't want to print this out :) |
---|
137 | for(int i = 0; i < ntohs(ph->length) - 8; i++) |
---|
138 | printf("%02x", data[i]); |
---|
139 | printf("'\n");*/ |
---|
140 | } |
---|
141 | break; |
---|
142 | case 9: |
---|
143 | { |
---|
144 | printf(" SCTP: Option Cookie preservative (TODO)\n"); |
---|
145 | } |
---|
146 | break; |
---|
147 | case 11: |
---|
148 | { |
---|
149 | printf(" SCTP: Option Host name %s\n", data); |
---|
150 | } |
---|
151 | break; |
---|
152 | case 12: |
---|
153 | { |
---|
154 | uint16_t *p = (uint16_t *)data; |
---|
155 | int len = ntohs(ph->length) - |
---|
156 | sizeof(struct sctp_var_param_hdr); |
---|
157 | |
---|
158 | printf(" SCTP: Option Supported address types "); |
---|
159 | |
---|
160 | while(len) { |
---|
161 | printf("%hu ", ntohs(*p)); |
---|
162 | p++; |
---|
163 | len -= sizeof(*p); |
---|
164 | } |
---|
165 | printf("\n"); |
---|
166 | } |
---|
167 | break; |
---|
168 | default: |
---|
169 | printf(" SCTP: Option Unknown type=%hu len=%hu\n", |
---|
170 | ntohs(ph->type), ntohs(ph->length)); |
---|
171 | } |
---|
172 | |
---|
173 | vlen -= ntohs(ph->length); |
---|
174 | offset += ntohs(ph->length); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len) |
---|
179 | { |
---|
180 | struct sctp_common_hdr *hdr; |
---|
181 | struct sctp_chunk_hdr *chunk; |
---|
182 | int chunk_num = 1; |
---|
183 | int vlen = 0; |
---|
184 | uint16_t chunklen = 0; |
---|
185 | |
---|
186 | if(len < (signed)sizeof(struct sctp_common_hdr)) { |
---|
187 | printf(" SCTP: packet too short!\n"); |
---|
188 | return; |
---|
189 | } |
---|
190 | |
---|
191 | hdr = (struct sctp_common_hdr *)packet; |
---|
192 | |
---|
193 | printf(" SCTP: Header Src port %hu Dst port %hu Tag %u Csum %u\n", |
---|
194 | ntohs(hdr->src_port), ntohs(hdr->dst_port), |
---|
195 | ntohl(hdr->verification_tag), ntohl(hdr->checksum)); |
---|
196 | |
---|
197 | len -= sizeof(struct sctp_common_hdr); |
---|
198 | packet += sizeof(struct sctp_common_hdr); |
---|
199 | |
---|
200 | while(len > 0) { |
---|
201 | if (len < sizeof(struct sctp_chunk_hdr)) { |
---|
202 | printf(" SCTP: [Truncated]\n\n"); |
---|
203 | break; |
---|
204 | } |
---|
205 | |
---|
206 | chunk = (struct sctp_chunk_hdr *)packet; |
---|
207 | |
---|
208 | chunklen = ntohs(chunk->length); |
---|
209 | |
---|
210 | printf(" SCTP: Chunk %d Type %s Flags %u Len %u\n", |
---|
211 | chunk_num++, |
---|
212 | sctp_type_to_str(chunk->type), chunk->flags, chunklen); |
---|
213 | |
---|
214 | if(chunklen == 0) { |
---|
215 | printf(" SCTP: Invalid chunk length, aborting.\n\n"); |
---|
216 | break; |
---|
217 | } |
---|
218 | |
---|
219 | /* Stupid SCTP has padding that is not accounted for in either |
---|
220 | * the chunk length or the payload length fields */ |
---|
221 | if ((chunklen % 4) != 0) { |
---|
222 | /* Pad to the next four byte boundary */ |
---|
223 | chunklen += ( 4 - (chunklen % 4) ); |
---|
224 | } |
---|
225 | |
---|
226 | /* Truncate any ridiculous chunk lengths so that they don't |
---|
227 | * exceed the confines of the packet */ |
---|
228 | if (chunklen > len) { |
---|
229 | chunklen = len; |
---|
230 | } |
---|
231 | |
---|
232 | switch(chunk->type) { |
---|
233 | case 0: /* DATA */ |
---|
234 | { |
---|
235 | struct sctp_data *data = (struct sctp_data *)(chunk + 1); |
---|
236 | |
---|
237 | printf(" SCTP: TSN %u Stream ID %hu Stream Seqno %hu " |
---|
238 | "Payload ID %u\n", |
---|
239 | ntohl(data->tsn), ntohs(data->stream_id), |
---|
240 | ntohs(data->stream_seqno), |
---|
241 | ntohl(data->payload_proto_id)); |
---|
242 | } |
---|
243 | break; |
---|
244 | case 1: /* INIT and */ |
---|
245 | case 2: /* INIT ACK packets have the same structure */ |
---|
246 | { |
---|
247 | /* INIT ACK */ |
---|
248 | struct sctp_init_ack *ack = (struct sctp_init_ack *) |
---|
249 | (chunk + 1); |
---|
250 | |
---|
251 | printf(" SCTP: Tag %u Credit %u Outbound %hu Inbound %hu " |
---|
252 | "TSN %u\n", |
---|
253 | ntohl(ack->init_tag), |
---|
254 | ntohl(ack->rcv_wnd_credit), |
---|
255 | ntohs(ack->outbound_streams), |
---|
256 | ntohs(ack->inbound_streams), |
---|
257 | ntohl(ack->init_tsn)); |
---|
258 | |
---|
259 | vlen = chunklen - (sizeof(struct sctp_init_ack) + |
---|
260 | sizeof(struct sctp_chunk_hdr) + |
---|
261 | sizeof(struct sctp_common_hdr) |
---|
262 | ); |
---|
263 | parse_options((char *)(ack + 1), vlen); |
---|
264 | |
---|
265 | } |
---|
266 | break; |
---|
267 | case 3: /* SACK */ |
---|
268 | { |
---|
269 | struct sctp_sack *sack = (struct sctp_sack *)(chunk + 1); |
---|
270 | int i; |
---|
271 | |
---|
272 | printf(" SCTP: Ack %u Wnd %u\n", ntohl(sack->tsn_ack), |
---|
273 | ntohl(sack->a_wnd)); |
---|
274 | |
---|
275 | for(i = 0; i < ntohs(sack->num_gap_blocks); i++) { |
---|
276 | uint16_t *p = (uint16_t *)(sack + 1); |
---|
277 | p += i * 2; |
---|
278 | |
---|
279 | printf(" SCTP: Gap ACK Start %hu End %hu\n", |
---|
280 | ntohs(*p), ntohs(*(p + 1))); |
---|
281 | } |
---|
282 | for(i = 0; i < ntohs(sack->num_dup_tsns); i++) { |
---|
283 | uint32_t *p = (uint32_t *)(sack + 1); |
---|
284 | p += ntohs(sack->num_gap_blocks) + i; |
---|
285 | |
---|
286 | printf(" SCTP: Duplicatate TSN %u\n", ntohl(*p)); |
---|
287 | } |
---|
288 | } |
---|
289 | break; |
---|
290 | } |
---|
291 | |
---|
292 | packet += chunklen; |
---|
293 | len -= chunklen; |
---|
294 | } |
---|
295 | printf("\n"); |
---|
296 | } |
---|