| 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,char *packet,int len) |
|---|
| 154 | { |
|---|
| 155 | struct sctp_common_hdr *hdr; |
|---|
| 156 | struct sctp_chunk_hdr *chunk; |
|---|
| 157 | int chunk_num = 1; |
|---|
| 158 | |
|---|
| 159 | if(len < (signed)sizeof(struct sctp_common_hdr)) { |
|---|
| 160 | printf(" SCTP: packet too short!\n"); |
|---|
| 161 | return; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | hdr = (struct sctp_common_hdr *)packet; |
|---|
| 165 | |
|---|
| 166 | printf(" SCTP: Header Src port %hu Dst port %hu Tag %u Csum %u\n", |
|---|
| 167 | ntohs(hdr->src_port), ntohs(hdr->dst_port), |
|---|
| 168 | ntohl(hdr->verification_tag), ntohl(hdr->checksum)); |
|---|
| 169 | |
|---|
| 170 | len -= sizeof(struct sctp_common_hdr); |
|---|
| 171 | packet += sizeof(struct sctp_common_hdr); |
|---|
| 172 | |
|---|
| 173 | while(len > 0) { |
|---|
| 174 | chunk = (struct sctp_chunk_hdr *)packet; |
|---|
| 175 | |
|---|
| 176 | chunk->length = ntohs(chunk->length); |
|---|
| 177 | |
|---|
| 178 | printf(" SCTP: Chunk %d Type %s Flags %u Len %u\n", |
|---|
| 179 | chunk_num++, |
|---|
| 180 | sctp_type_to_str(chunk->type), chunk->flags, chunk->length); |
|---|
| 181 | |
|---|
| 182 | if(chunk->length == 0) { |
|---|
| 183 | printf(" SCTP: Invalid chunk length, aborting.\n\n"); |
|---|
| 184 | break; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | switch(chunk->type) { |
|---|
| 188 | case 0: // DATA |
|---|
| 189 | { |
|---|
| 190 | struct sctp_data *data = (struct sctp_data *)(chunk + 1); |
|---|
| 191 | |
|---|
| 192 | printf(" SCTP: TSN %u Stream ID %hu Stream Seqno %hu " |
|---|
| 193 | "Payload ID %u\n", |
|---|
| 194 | ntohl(data->tsn), ntohs(data->stream_id), |
|---|
| 195 | ntohs(data->stream_seqno), |
|---|
| 196 | ntohl(data->payload_proto_id)); |
|---|
| 197 | } |
|---|
| 198 | break; |
|---|
| 199 | case 1: // INIT and |
|---|
| 200 | case 2: // INIT ACK packets have the same structure |
|---|
| 201 | { |
|---|
| 202 | // INIT ACK |
|---|
| 203 | struct sctp_init_ack *ack = (struct sctp_init_ack *) |
|---|
| 204 | (chunk + 1); |
|---|
| 205 | |
|---|
| 206 | printf(" SCTP: Tag %u Credit %u Outbound %hu Inbound %hu " |
|---|
| 207 | "TSN %u\n", |
|---|
| 208 | ntohl(ack->init_tag), |
|---|
| 209 | ntohl(ack->rcv_wnd_credit), |
|---|
| 210 | ntohs(ack->outbound_streams), |
|---|
| 211 | ntohs(ack->inbound_streams), |
|---|
| 212 | ntohl(ack->init_tsn)); |
|---|
| 213 | |
|---|
| 214 | int vlen = chunk->length - (sizeof(struct sctp_init_ack) + |
|---|
| 215 | sizeof(struct sctp_chunk_hdr) + |
|---|
| 216 | sizeof(struct sctp_common_hdr) |
|---|
| 217 | ); |
|---|
| 218 | parse_options((char *)(ack + 1), vlen); |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | break; |
|---|
| 222 | case 3: // SACK |
|---|
| 223 | { |
|---|
| 224 | struct sctp_sack *sack = (struct sctp_sack *)(chunk + 1); |
|---|
| 225 | int i; |
|---|
| 226 | |
|---|
| 227 | printf(" SCTP: Ack %u Wnd %u\n", ntohl(sack->tsn_ack), |
|---|
| 228 | ntohl(sack->a_wnd)); |
|---|
| 229 | |
|---|
| 230 | for(i = 0; i < ntohs(sack->num_gap_blocks); i++) { |
|---|
| 231 | uint16_t *p = (uint16_t *)(sack + 1); |
|---|
| 232 | p += i * 2; |
|---|
| 233 | |
|---|
| 234 | printf(" SCTP: Gap ACK Start %hu End %hu\n", |
|---|
| 235 | ntohs(*p), ntohs(*(p + 1))); |
|---|
| 236 | } |
|---|
| 237 | for(i = 0; i < ntohs(sack->num_dup_tsns); i++) { |
|---|
| 238 | uint32_t *p = (uint32_t *)(sack + 1); |
|---|
| 239 | p += ntohs(sack->num_gap_blocks) + i; |
|---|
| 240 | |
|---|
| 241 | printf(" SCTP: Duplicatate TSN %u\n", ntohl(*p)); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | break; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | packet += chunk->length; |
|---|
| 248 | len -= chunk->length; |
|---|
| 249 | } |
|---|
| 250 | printf("\n"); |
|---|
| 251 | } |
|---|