1 | #include <stdio.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <dlfcn.h> |
---|
4 | #include <map> |
---|
5 | #include "libpacketdump.h" |
---|
6 | |
---|
7 | #include <sys/socket.h> |
---|
8 | #include <netinet/in.h> |
---|
9 | #include <arpa/inet.h> |
---|
10 | #include <assert.h> |
---|
11 | |
---|
12 | /* SCTP decoding by Sam Jansen, 31/08/2004 |
---|
13 | * |
---|
14 | * Based on RFC 2960 - Stream Control Transmission Protocol |
---|
15 | */ |
---|
16 | |
---|
17 | struct sctp_common_hdr |
---|
18 | { |
---|
19 | uint16_t src_port, dst_port; |
---|
20 | uint32_t verification_tag; |
---|
21 | uint32_t checksum; |
---|
22 | } __attribute__((__packed__)); |
---|
23 | |
---|
24 | struct sctp_chunk_hdr |
---|
25 | { |
---|
26 | uint8_t type; |
---|
27 | uint8_t flags; |
---|
28 | uint16_t length; |
---|
29 | } __attribute__((__packed__)); |
---|
30 | |
---|
31 | struct sctp_data |
---|
32 | { |
---|
33 | uint32_t tsn; |
---|
34 | uint16_t stream_id; |
---|
35 | uint16_t stream_seqno; |
---|
36 | uint32_t payload_proto_id; |
---|
37 | } __attribute__((__packed__)); |
---|
38 | |
---|
39 | // The following works for INIT and INIT ACK packets |
---|
40 | struct sctp_init_ack |
---|
41 | { |
---|
42 | uint32_t init_tag; |
---|
43 | uint32_t rcv_wnd_credit; |
---|
44 | uint16_t outbound_streams; |
---|
45 | uint16_t inbound_streams; |
---|
46 | uint32_t init_tsn; |
---|
47 | } __attribute__((__packed__)); |
---|
48 | |
---|
49 | struct sctp_sack |
---|
50 | { |
---|
51 | uint32_t tsn_ack; |
---|
52 | uint32_t a_wnd; |
---|
53 | uint16_t num_gap_blocks; |
---|
54 | uint16_t num_dup_tsns; |
---|
55 | } __attribute__((__packed__)); |
---|
56 | |
---|
57 | struct sctp_var_param_hdr |
---|
58 | { |
---|
59 | uint16_t type; |
---|
60 | uint16_t length; |
---|
61 | } __attribute__((__packed__)); |
---|
62 | |
---|
63 | static char *sctp_type_to_str(uint8_t type) |
---|
64 | { |
---|
65 | switch(type) |
---|
66 | { |
---|
67 | case 0: return "DATA"; |
---|
68 | case 1: return "INIT"; |
---|
69 | case 2: return "INIT ACK"; |
---|
70 | case 3: return "SACK"; |
---|
71 | case 4: return "HEARTBEAT"; |
---|
72 | case 5: return "HEARTBEAT ACK"; |
---|
73 | case 6: return "ABORT"; |
---|
74 | case 7: return "SHUTDOWN"; |
---|
75 | case 8: return "SHUTDOWN ACK"; |
---|
76 | case 9: return "ERROR"; |
---|
77 | case 10: return "COOKIE ECHO"; |
---|
78 | case 11: return "COOKIE ACK"; |
---|
79 | case 12: return "Reserved for ECNE"; |
---|
80 | case 13: return "Reserved for CWR"; |
---|
81 | case 14: return "SHUTDOWN COMPLETE"; |
---|
82 | case 63: |
---|
83 | case 127: |
---|
84 | case 191: |
---|
85 | case 255: return "IETF-defined Chunk Extensions"; |
---|
86 | }; |
---|
87 | |
---|
88 | return "reserved by IETF"; |
---|
89 | } |
---|
90 | |
---|
91 | static void parse_options(char *offset, int vlen) |
---|
92 | { |
---|
93 | while(vlen > 0) { |
---|
94 | struct sctp_var_param_hdr *ph = (struct sctp_var_param_hdr *)(offset); |
---|
95 | char *data = (char *)(ph + 1); |
---|
96 | |
---|
97 | switch(ntohs(ph->type)) { |
---|
98 | case 5: |
---|
99 | { |
---|
100 | struct in_addr *ia = (struct in_addr *)data; |
---|
101 | printf(" SCTP: Option IP address %s\n", inet_ntoa(*ia)); |
---|
102 | } |
---|
103 | break; |
---|
104 | case 6: |
---|
105 | { |
---|
106 | printf(" SCTP: Option IPv6 address (TODO)\n"); |
---|
107 | } |
---|
108 | break; |
---|
109 | case 7: |
---|
110 | { |
---|
111 | printf(" SCTP: Option State cookie\n"); |
---|
112 | /* // Prolly don't want to print this out :) |
---|
113 | for(int i = 0; i < ntohs(ph->length) - 8; i++) |
---|
114 | printf("%02x", data[i]); |
---|
115 | printf("'\n");*/ |
---|
116 | } |
---|
117 | break; |
---|
118 | case 9: |
---|
119 | { |
---|
120 | printf(" SCTP: Option Cookie preservative (TODO)\n"); |
---|
121 | } |
---|
122 | break; |
---|
123 | case 11: |
---|
124 | { |
---|
125 | printf(" SCTP: Option Host name %s\n", data); |
---|
126 | } |
---|
127 | break; |
---|
128 | case 12: |
---|
129 | { |
---|
130 | uint16_t *p = (uint16_t *)data; |
---|
131 | int len = ntohs(ph->length) - |
---|
132 | sizeof(struct sctp_var_param_hdr); |
---|
133 | |
---|
134 | printf(" SCTP: Option Supported address types "); |
---|
135 | |
---|
136 | while(len) { |
---|
137 | printf("%hu ", ntohs(*p)); |
---|
138 | p++; |
---|
139 | len -= sizeof(*p); |
---|
140 | } |
---|
141 | printf("\n"); |
---|
142 | } |
---|
143 | break; |
---|
144 | default: |
---|
145 | printf(" SCTP: Option Unknown type=%hu len=%hu\n", |
---|
146 | ntohs(ph->type), ntohs(ph->length)); |
---|
147 | } |
---|
148 | |
---|
149 | vlen -= ntohs(ph->length); |
---|
150 | offset += ntohs(ph->length); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | extern "C" |
---|
155 | void decode(int link_type,char *packet,int len) |
---|
156 | { |
---|
157 | struct sctp_common_hdr *hdr; |
---|
158 | struct sctp_chunk_hdr *chunk; |
---|
159 | int chunk_num = 1; |
---|
160 | |
---|
161 | if(len < (signed)sizeof(struct sctp_common_hdr)) { |
---|
162 | printf(" SCTP: packet too short!\n"); |
---|
163 | return; |
---|
164 | } |
---|
165 | |
---|
166 | hdr = (struct sctp_common_hdr *)packet; |
---|
167 | |
---|
168 | printf(" SCTP: Header Src port %hu Dst port %hu Tag %u Csum %u\n", |
---|
169 | ntohs(hdr->src_port), ntohs(hdr->dst_port), |
---|
170 | ntohl(hdr->verification_tag), ntohl(hdr->checksum)); |
---|
171 | |
---|
172 | len -= sizeof(struct sctp_common_hdr); |
---|
173 | packet += sizeof(struct sctp_common_hdr); |
---|
174 | |
---|
175 | while(len > 0) { |
---|
176 | chunk = (struct sctp_chunk_hdr *)packet; |
---|
177 | |
---|
178 | chunk->length = ntohs(chunk->length); |
---|
179 | |
---|
180 | printf(" SCTP: Chunk %d Type %s Flags %u Len %u\n", |
---|
181 | chunk_num++, |
---|
182 | sctp_type_to_str(chunk->type), chunk->flags, chunk->length); |
---|
183 | |
---|
184 | if(chunk->length == 0) { |
---|
185 | printf(" SCTP: Invalid chunk length, aborting.\n\n"); |
---|
186 | break; |
---|
187 | } |
---|
188 | |
---|
189 | switch(chunk->type) { |
---|
190 | case 0: // DATA |
---|
191 | { |
---|
192 | struct sctp_data *data = (struct sctp_data *)(chunk + 1); |
---|
193 | |
---|
194 | printf(" SCTP: TSN %u Stream ID %hu Stream Seqno %hu " |
---|
195 | "Payload ID %u\n", |
---|
196 | ntohl(data->tsn), ntohs(data->stream_id), |
---|
197 | ntohs(data->stream_seqno), |
---|
198 | ntohl(data->payload_proto_id)); |
---|
199 | } |
---|
200 | break; |
---|
201 | case 1: // INIT and |
---|
202 | case 2: // INIT ACK packets have the same structure |
---|
203 | { |
---|
204 | // INIT ACK |
---|
205 | struct sctp_init_ack *ack = (struct sctp_init_ack *) |
---|
206 | (chunk + 1); |
---|
207 | |
---|
208 | printf(" SCTP: Tag %u Credit %u Outbound %hu Inbound %hu " |
---|
209 | "TSN %u\n", |
---|
210 | ntohl(ack->init_tag), |
---|
211 | ntohl(ack->rcv_wnd_credit), |
---|
212 | ntohs(ack->outbound_streams), |
---|
213 | ntohs(ack->inbound_streams), |
---|
214 | ntohl(ack->init_tsn)); |
---|
215 | |
---|
216 | int vlen = chunk->length - (sizeof(struct sctp_init_ack) + |
---|
217 | sizeof(struct sctp_chunk_hdr) + |
---|
218 | sizeof(struct sctp_common_hdr) |
---|
219 | ); |
---|
220 | parse_options((char *)(ack + 1), vlen); |
---|
221 | |
---|
222 | } |
---|
223 | break; |
---|
224 | case 3: // SACK |
---|
225 | { |
---|
226 | struct sctp_sack *sack = (struct sctp_sack *)(chunk + 1); |
---|
227 | int i; |
---|
228 | |
---|
229 | printf(" SCTP: Ack %u Wnd %u\n", ntohl(sack->tsn_ack), |
---|
230 | ntohl(sack->a_wnd)); |
---|
231 | |
---|
232 | for(i = 0; i < ntohs(sack->num_gap_blocks); i++) { |
---|
233 | uint16_t *p = (uint16_t *)(sack + 1); |
---|
234 | p += i * 2; |
---|
235 | |
---|
236 | printf(" SCTP: Gap ACK Start %hu End %hu\n", |
---|
237 | ntohs(*p), ntohs(*(p + 1))); |
---|
238 | } |
---|
239 | for(i = 0; i < ntohs(sack->num_dup_tsns); i++) { |
---|
240 | uint32_t *p = (uint32_t *)(sack + 1); |
---|
241 | p += ntohs(sack->num_gap_blocks) + i; |
---|
242 | |
---|
243 | printf(" SCTP: Duplicatate TSN %u\n", ntohl(*p)); |
---|
244 | } |
---|
245 | } |
---|
246 | break; |
---|
247 | } |
---|
248 | |
---|
249 | packet += chunk->length; |
---|
250 | len -= chunk->length; |
---|
251 | } |
---|
252 | printf("\n"); |
---|
253 | } |
---|