File: ip_132.c

package info (click to toggle)
libtrace3 3.0.21-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 4,356 kB
  • ctags: 3,791
  • sloc: ansic: 24,422; sh: 11,372; cpp: 1,811; makefile: 464; yacc: 96; lex: 50
file content (252 lines) | stat: -rw-r--r-- 7,278 bytes parent folder | download | duplicates (5)
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
251
252
#include <stdio.h>
#include <inttypes.h>
#include <dlfcn.h>
#include "libpacketdump.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>

/* SCTP decoding by Sam Jansen, 31/08/2004
 *
 * Based on RFC 2960 - Stream Control Transmission Protocol
 */

struct sctp_common_hdr
{
    uint16_t src_port, dst_port;
    uint32_t verification_tag;
    uint32_t checksum;
} __attribute__((__packed__));

struct sctp_chunk_hdr
{
    uint8_t type;
    uint8_t flags;
    uint16_t length;
} __attribute__((__packed__));

struct sctp_data
{
    uint32_t tsn;
    uint16_t stream_id;
    uint16_t stream_seqno;
    uint32_t payload_proto_id;
} __attribute__((__packed__));

/* The following works for INIT and INIT ACK packets */
struct sctp_init_ack
{
    uint32_t init_tag;
    uint32_t rcv_wnd_credit;
    uint16_t outbound_streams;
    uint16_t inbound_streams;
    uint32_t init_tsn;
} __attribute__((__packed__));

struct sctp_sack
{
    uint32_t tsn_ack;
    uint32_t a_wnd;
    uint16_t num_gap_blocks;
    uint16_t num_dup_tsns;
} __attribute__((__packed__));

struct sctp_var_param_hdr
{
    uint16_t type;
    uint16_t length;
} __attribute__((__packed__));

static char *sctp_type_to_str(uint8_t type)
{
    switch(type)
    {
        case 0: return "DATA";
        case 1: return "INIT";
        case 2: return "INIT ACK";
        case 3: return "SACK";
        case 4: return "HEARTBEAT";
        case 5: return "HEARTBEAT ACK";
        case 6: return "ABORT";
        case 7: return "SHUTDOWN";
        case 8: return "SHUTDOWN ACK";
        case 9: return "ERROR";
        case 10: return "COOKIE ECHO";
        case 11: return "COOKIE ACK";
        case 12: return "Reserved for ECNE";
        case 13: return "Reserved for CWR";
        case 14: return "SHUTDOWN COMPLETE";
        case 63:
        case 127:
        case 191:
        case 255: return "IETF-defined Chunk Extensions";
    };

   return "reserved by IETF";
}

static void parse_options(char *offset, int vlen)
{
    while(vlen > 0) {
        struct sctp_var_param_hdr *ph = (struct sctp_var_param_hdr *)(offset);
        char *data = (char *)(ph + 1);

        switch(ntohs(ph->type)) {
            case 5:
            {
                struct in_addr *ia = (struct in_addr *)data;
                printf(" SCTP: Option IP address %s\n", inet_ntoa(*ia));
            }
            break;
            case 6:
            {
                printf(" SCTP: Option IPv6 address (TODO)\n");
            }
            break;
            case 7:
            {
                printf(" SCTP: Option State cookie\n");
                /* // Prolly don't want to print this out :)
                for(int i = 0; i < ntohs(ph->length) - 8; i++)
                    printf("%02x", data[i]);
                printf("'\n");*/
            }
            break;
            case 9:
            {
                printf(" SCTP: Option Cookie preservative (TODO)\n");
            }
            break;
            case 11:
            {
                printf(" SCTP: Option Host name %s\n", data);
            }
            break;
            case 12:
            {
                uint16_t *p = (uint16_t *)data;
                int len = ntohs(ph->length) - 
                    sizeof(struct sctp_var_param_hdr);
                
                printf(" SCTP: Option Supported address types ");
                
                while(len) {
                    printf("%hu ", ntohs(*p));
                    p++;
                    len -= sizeof(*p);
                }
                printf("\n");
            }
            break;
            default:
                printf(" SCTP: Option Unknown type=%hu len=%hu\n", 
                        ntohs(ph->type), ntohs(ph->length));
        }

        vlen -= ntohs(ph->length);
        offset += ntohs(ph->length);
    }
}

DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
{
    struct sctp_common_hdr *hdr;
    struct sctp_chunk_hdr *chunk;
    int chunk_num = 1;
    int vlen;

    if(len < (signed)sizeof(struct sctp_common_hdr)) {
        printf(" SCTP: packet too short!\n");
        return;
    }

    hdr = (struct sctp_common_hdr *)packet;

    printf(" SCTP: Header Src port %hu Dst port %hu Tag %u Csum %u\n",
            ntohs(hdr->src_port), ntohs(hdr->dst_port),
            ntohl(hdr->verification_tag), ntohl(hdr->checksum));

    len -= sizeof(struct sctp_common_hdr);
    packet += sizeof(struct sctp_common_hdr);

    while(len > 0) {
        chunk = (struct sctp_chunk_hdr *)packet;

        chunk->length = ntohs(chunk->length);

        printf(" SCTP: Chunk %d Type %s Flags %u Len %u\n",
            chunk_num++,
            sctp_type_to_str(chunk->type), chunk->flags, chunk->length);

        if(chunk->length == 0) {
            printf(" SCTP: Invalid chunk length, aborting.\n\n");
            break;
        }

        switch(chunk->type) {
            case 0: /* DATA */
            {
                struct sctp_data *data = (struct sctp_data *)(chunk + 1);

                printf(" SCTP: TSN %u Stream ID %hu Stream Seqno %hu "
                        "Payload ID %u\n",
                        ntohl(data->tsn), ntohs(data->stream_id),
                        ntohs(data->stream_seqno),
                        ntohl(data->payload_proto_id));
            }
            break;
            case 1: /* INIT and  */
            case 2: /* INIT ACK packets have the same structure */
            {
                /* INIT ACK */
                struct sctp_init_ack *ack = (struct sctp_init_ack *)
                    (chunk + 1);
                
                printf(" SCTP: Tag %u Credit %u Outbound %hu Inbound %hu "
                        "TSN %u\n",
                        ntohl(ack->init_tag),
                        ntohl(ack->rcv_wnd_credit),
                        ntohs(ack->outbound_streams),
                        ntohs(ack->inbound_streams),
                        ntohl(ack->init_tsn));

                vlen = chunk->length - (sizeof(struct sctp_init_ack) +
                        sizeof(struct sctp_chunk_hdr) +
                        sizeof(struct sctp_common_hdr)
                        );
                parse_options((char *)(ack + 1), vlen);

            }
            break;
            case 3: /* SACK */
            {
                struct sctp_sack *sack = (struct sctp_sack *)(chunk + 1);
                int i;

                printf(" SCTP: Ack %u Wnd %u\n", ntohl(sack->tsn_ack),
                        ntohl(sack->a_wnd));

                for(i = 0; i < ntohs(sack->num_gap_blocks); i++) {
                    uint16_t *p = (uint16_t *)(sack + 1);
                    p += i * 2;

                    printf(" SCTP: Gap ACK Start %hu End %hu\n",
                            ntohs(*p), ntohs(*(p + 1)));
                }
                for(i = 0; i < ntohs(sack->num_dup_tsns); i++) {
                    uint32_t *p = (uint32_t *)(sack + 1);
                    p += ntohs(sack->num_gap_blocks) + i;

                    printf(" SCTP: Duplicatate TSN %u\n", ntohl(*p));
                }
            }
            break;
        }
        
        packet += chunk->length;
        len -= chunk->length;
    }
    printf("\n");
}