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