File: deconstruct_unix.c

package info (click to toggle)
mtr 0.95-1.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 900 kB
  • sloc: ansic: 10,325; python: 572; makefile: 164; sh: 141
file content (587 lines) | stat: -rw-r--r-- 18,449 bytes parent folder | download | duplicates (4)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
    mtr  --  a network diagnostic tool
    Copyright (C) 2016  Matt Kimball

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "deconstruct_unix.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "protocols.h"
#include "sockaddr.h"

#define MAX_MPLS_LABELS 8

/*
    Given an ICMP id + ICMP sequence, find the match probe we've
    transmitted and if found, respond to the command which sent it
*/
static
void find_and_receive_probe(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    struct timeval *timestamp,
    int icmp_type,
    int protocol,
    int icmp_id,
    int icmp_sequence,
    int mpls_count,
    struct mpls_label_t *mpls)
{
    struct probe_t *probe;

    probe = find_probe(net_state, protocol, icmp_id, icmp_sequence);
    if (probe == NULL) {
        return;
    }

    receive_probe(net_state, probe, icmp_type,
                  remote_addr, timestamp, mpls_count, mpls);
}

/*
    Handle a UDP packet received embedded in an ICMP reply.
    The sequence number identifying the probe might be in
    the source port number, the destination port number, or
    the checksum.  We'll check all three.
*/
static
void handle_inner_udp_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    int icmp_result,
    int af,
    const void *ip,
    const struct UDPHeader *udp,
    int udp_length,
    struct timeval *timestamp,
    int mpls_count,
    struct mpls_label_t *mpls)
{
    struct probe_t *probe;

    probe = find_probe(net_state, IPPROTO_UDP, 0, udp->dstport);
    if (probe == NULL) {
        probe = find_probe(net_state, IPPROTO_UDP, 0, udp->srcport);
    }
    if (probe == NULL) {
        probe = find_probe(net_state, IPPROTO_UDP, 0, udp->checksum);
    }
    if (probe == NULL)
        return;

    if (probe->remote_addr.ss_family != remote_addr->ss_family)
        return;

    if (udp->dstport != *sockaddr_port_offset(&probe->remote_addr) )
        return;

    if (udp->srcport != *sockaddr_port_offset(&probe->local_addr) )
        return;

    void *saddr, *daddr;
    if (af == AF_INET)
    {
        saddr = &((struct IPHeader *)ip)->saddr;
        daddr = &((struct IPHeader *)ip)->daddr;
    } else
    if (af == AF_INET6)
    {
        daddr = &((struct IP6Header *)ip)->daddr;
        saddr = &((struct IP6Header *)ip)->saddr;
    } else
    {
        return;
    }

    if ( memcmp(sockaddr_addr_offset(&probe->remote_addr),
               daddr,
               sockaddr_addr_size(&probe->remote_addr)) != 0 )
            return;

    if ( memcmp(sockaddr_addr_offset(&probe->local_addr),
           saddr,
           sockaddr_addr_size(&probe->local_addr)) != 0)
        return;

    /* probe is not null */
    receive_probe(net_state, probe, icmp_result,
                      remote_addr, timestamp, mpls_count, mpls);
}

void handle_error_queue_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    int icmp_result,
    int proto,
    char *packet,
    int packet_length,
    struct timeval *timestamp)
{
    if (proto == IPPROTO_UDP) {
        handle_inner_udp_packet(net_state, remote_addr, ICMP_TIME_EXCEEDED, 0, NULL,
                (struct UDPHeader *)packet, packet_length, timestamp, 0, NULL);
    } else if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) {
        const struct ICMPHeader *icmp = (struct ICMPHeader *)packet;
        find_and_receive_probe(net_state, remote_addr, timestamp,
                               ICMP_TIME_EXCEEDED, IPPROTO_ICMP, icmp->id,
                               icmp->sequence, 0, NULL);
    }

}

/*
    We've received an ICMP message with an embedded IP packet.
    We will try to determine which of our outgoing probes
    corresponds to the embedded IP packet and record the response.
*/
static
void handle_inner_ip4_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    int icmp_result,
    const struct IPHeader *ip,
    int packet_length,
    struct timeval *timestamp,
    int mpls_count,
    struct mpls_label_t *mpls)
{
    const int ip_icmp_size =
        sizeof(struct IPHeader) + sizeof(struct ICMPHeader);
    const int ip_udp_size =
        sizeof(struct IPHeader) + sizeof(struct UDPHeader);
    const int ip_tcp_size =
        sizeof(struct IPHeader) + sizeof(struct TCPHeader);
    const struct ICMPHeader *icmp;
    const struct UDPHeader *udp;
    const struct TCPHeader *tcp;
    int udp_length;
#ifdef IPPROTO_SCTP
    const int ip_sctp_size =
        sizeof(struct IPHeader) + sizeof(struct SCTPHeader);
    const struct SCTPHeader *sctp;
#endif

    if (ip->protocol == IPPROTO_ICMP) {
        if (packet_length < ip_icmp_size) {
            return;
        }

        icmp = (struct ICMPHeader *) (ip + 1);

        find_and_receive_probe(net_state, remote_addr, timestamp,
                               icmp_result, IPPROTO_ICMP, icmp->id,
                               icmp->sequence, mpls_count, mpls);
    } else if (ip->protocol == IPPROTO_UDP) {
        if (packet_length < ip_udp_size) {
            return;
        }

        udp = (struct UDPHeader *) (ip + 1);
        udp_length = packet_length - sizeof(struct IPHeader);

        handle_inner_udp_packet(net_state, remote_addr, icmp_result, AF_INET, ip, udp,
                                udp_length, timestamp, mpls_count, mpls);
    } else if (ip->protocol == IPPROTO_TCP) {
        if (packet_length < ip_tcp_size) {
            return;
        }

        tcp = (struct TCPHeader *) (ip + 1);

        find_and_receive_probe(net_state, remote_addr, timestamp,
                               icmp_result, IPPROTO_TCP, 0, tcp->srcport,
                               mpls_count, mpls);
#ifdef IPPROTO_SCTP
    } else if (ip->protocol == IPPROTO_SCTP) {
        if (packet_length < ip_sctp_size) {
            return;
        }

        sctp = (struct SCTPHeader *) (ip + 1);

        find_and_receive_probe(net_state, remote_addr, timestamp,
                               icmp_result, IPPROTO_SCTP, 0, sctp->srcport,
                               mpls_count, mpls);
#endif
    }
}

/*
    Examine the IPv6 header embedded in a returned ICMPv6 packet
    in order to match it with a probe which we previously sent.
*/
static
void handle_inner_ip6_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    int icmp_result,
    const struct IP6Header *ip,
    int packet_length,
    struct timeval *timestamp,
    int mpls_count,
    struct mpls_label_t *mpls)
{
    const int ip_icmp_size =
        sizeof(struct IP6Header) + sizeof(struct ICMPHeader);
    const int ip_udp_size =
        sizeof(struct IP6Header) + sizeof(struct UDPHeader);
    const int ip_tcp_size =
        sizeof(struct IP6Header) + sizeof(struct TCPHeader);
    const struct ICMPHeader *icmp;
    const struct UDPHeader *udp;
    const struct TCPHeader *tcp;
    int udp_length;
#ifdef IPPROTO_SCTP
    const int ip_sctp_size =
        sizeof(struct IPHeader) + sizeof(struct SCTPHeader);
    const struct SCTPHeader *sctp;
#endif

    if (ip->protocol == IPPROTO_ICMPV6) {
        if (packet_length < ip_icmp_size) {
            return;
        }

        icmp = (struct ICMPHeader *) (ip + 1);

        find_and_receive_probe(net_state, remote_addr, timestamp,
                               icmp_result, IPPROTO_ICMP, icmp->id,
                               icmp->sequence, mpls_count, mpls);
    } else if (ip->protocol == IPPROTO_UDP) {
        if (packet_length < ip_udp_size) {
            return;
        }

        udp = (struct UDPHeader *) (ip + 1);
        udp_length = packet_length - sizeof(struct IP6Header);

        handle_inner_udp_packet(net_state, remote_addr, icmp_result, AF_INET6, ip, udp,
                                udp_length, timestamp, mpls_count, mpls);
    } else if (ip->protocol == IPPROTO_TCP) {
        if (packet_length < ip_tcp_size) {
            return;
        }

        tcp = (struct TCPHeader *) (ip + 1);
        find_and_receive_probe(net_state, remote_addr, timestamp,
                               icmp_result, IPPROTO_TCP, 0, tcp->srcport,
                               mpls_count, mpls);
#ifdef IPPROTO_SCTP
    } else if (ip->protocol == IPPROTO_SCTP) {
        if (packet_length < ip_sctp_size) {
            return;
        }

        sctp = (struct SCTPHeader *) (ip + 1);

        find_and_receive_probe(net_state, remote_addr, timestamp,
                               icmp_result, IPPROTO_SCTP, 0, sctp->srcport,
                               mpls_count, mpls);
#endif
    }
}

/*  Convert an ICMP MPLS extension object into an mpls_label_t structure  */
static
int decode_mpls_object(
    struct ICMPExtensionObject *icmp_obj,
    int obj_len,
    struct mpls_label_t *mpls,
    int mpls_count)
{
    int label_bytes;
    int labels_present;
    int i;
    struct ICMPExtMPLSLabel *ext_mpls;
    struct ICMPExtMPLSLabel *ext_label;
    struct mpls_label_t *label;

    label_bytes = obj_len - sizeof(struct ICMPExtensionObject);
    labels_present = label_bytes / sizeof(struct ICMPExtMPLSLabel);

    ext_mpls = (struct ICMPExtMPLSLabel *) (icmp_obj + 1);
    for (i = 0; i < mpls_count && i < labels_present; i++) {
        ext_label = &ext_mpls[i];
        label = &mpls[i];

        memset(label, 0, sizeof(struct mpls_label_t));

        label->label =
            ext_label->label[0] << 12 |
            ext_label->label[1] << 4 | ext_label->label[2] >> 4;
        label->traffic_class = (ext_label->label[2] & 0x0E) >> 1;
        label->bottom_of_stack = ext_label->label[2] & 0x01;
        label->ttl = ext_label->ttl;
    }

    return i;
}

/*  Extract MPLS labels from the ICMP extension header, if present  */
static
int decode_mpls_labels(
    const struct ICMPHeader *icmp,
    int packet_length,
    struct mpls_label_t *mpls,
    int mpls_count)
{
    const int icmp_orig_icmp_ext_size =
        sizeof(struct ICMPHeader) + ICMP_ORIGINAL_DATAGRAM_MIN_SIZE +
        sizeof(struct ICMPExtensionHeader);
    char *inner_packet;
    char *icmp_object_bytes;
    struct ICMPExtensionHeader *icmp_ext;
    struct ICMPExtensionObject *icmp_obj;
    int remaining_size;
    int obj_len;

    if (packet_length < icmp_orig_icmp_ext_size) {
        return 0;
    }

    inner_packet = (char *) (icmp + 1);
    icmp_ext = (struct ICMPExtensionHeader *)
        (inner_packet + ICMP_ORIGINAL_DATAGRAM_MIN_SIZE);

    if ((icmp_ext->version & 0xF0) != 0x20) {
        return 0;
    }

    remaining_size = packet_length - icmp_orig_icmp_ext_size;
    icmp_object_bytes = (char *) (icmp_ext + 1);

    /*
       Iterate through the chain of extension objects, looking for
       an MPLS label extension.
     */
    while (remaining_size >= sizeof(struct ICMPExtensionObject)) {
        icmp_obj = (struct ICMPExtensionObject *) icmp_object_bytes;
        obj_len = ntohs(icmp_obj->len);

        if (obj_len > remaining_size) {
            return 0;
        }
        if (obj_len < sizeof(struct ICMPExtensionObject)) {
            return 0;
        }

        if (icmp_obj->classnum == ICMP_EXT_MPLS_CLASSNUM &&
            icmp_obj->ctype == ICMP_EXT_MPLS_CTYPE) {

            return decode_mpls_object(icmp_obj, obj_len, mpls, mpls_count);
        }

        remaining_size -= obj_len;
        icmp_object_bytes += obj_len;
    }

    return 0;
}

/*
    Decode the ICMP header received and try to find a probe which it
    is in response to.
*/
static
void handle_received_icmp4_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    const struct ICMPHeader *icmp,
    int packet_length,
    struct timeval *timestamp)
{
    int icmp_ip_size = 0;
    const struct IPHeader *inner_ip;
    int inner_size = packet_length - sizeof(struct ICMPHeader);
    int mpls_count;
    struct mpls_label_t mpls[MAX_MPLS_LABELS];

    if (net_state->platform.ip4_socket_raw) {
        icmp_ip_size += sizeof(struct IPHeader);
    }
    icmp_ip_size += sizeof(struct ICMPHeader);
    mpls_count =
        decode_mpls_labels(icmp, packet_length, mpls, MAX_MPLS_LABELS);

    /*  If we get an echo reply, our probe reached the destination host  */
    if (icmp->type == ICMP_ECHOREPLY) {
        find_and_receive_probe(net_state, remote_addr, timestamp,
                               ICMP_ECHOREPLY, IPPROTO_ICMP, icmp->id,
                               icmp->sequence, mpls_count, mpls);
    }

    if (packet_length < icmp_ip_size) {
        return;
    }
    inner_ip = (struct IPHeader *) (icmp + 1);

    /*
       If we get a time exceeded, we got a response from an intermediate
       host along the path to our destination.
     */
    if (icmp->type == ICMP_TIME_EXCEEDED) {
        /*
           The IP packet inside the ICMP response contains our original
           IP header.  That's where we can get our original ID and
           sequence number.
         */
        handle_inner_ip4_packet(net_state, remote_addr,
                                ICMP_TIME_EXCEEDED, inner_ip, inner_size,
                                timestamp, mpls_count, mpls);
    }

    if (icmp->type == ICMP_DEST_UNREACH) {
        /*
           We'll get a ICMP_PORT_UNREACH when a non-ICMP probe
           reaches its final destination.  (Assuming that port isn't
           open on the destination host.)
         */
        if (icmp->code == ICMP_PORT_UNREACH) {
            handle_inner_ip4_packet(net_state, remote_addr,
                                    ICMP_ECHOREPLY, inner_ip, inner_size,
                                    timestamp, mpls_count, mpls);
        } else {
            /*
                ICMP_DEST_UNREACH subtypes other than port unreachable
                indicate an exceptional condition, and will be reported
                as a "no route to host" probe response.
            */
            handle_inner_ip4_packet(net_state, remote_addr,
                                    ICMP_DEST_UNREACH, inner_ip, inner_size,
                                    timestamp, mpls_count, mpls);
        }
    }
}

/*
    Decode the ICMPv6 header.  The code duplication with ICMPv4 is
    unfortunate, but small details in structure size and ICMP
    constants differ.
*/
static
void handle_received_icmp6_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    const struct ICMPHeader *icmp,
    int packet_length,
    struct timeval *timestamp)
{
    const int icmp_ip_size =
        sizeof(struct ICMPHeader) + sizeof(struct IP6Header);
    const struct IP6Header *inner_ip;
    int inner_size = packet_length - sizeof(struct ICMPHeader);
    int mpls_count;
    struct mpls_label_t mpls[MAX_MPLS_LABELS];

    mpls_count =
        decode_mpls_labels(icmp, packet_length, mpls, MAX_MPLS_LABELS);

    if (icmp->type == ICMP6_ECHOREPLY) {
        find_and_receive_probe(net_state, remote_addr, timestamp,
                               ICMP_ECHOREPLY, IPPROTO_ICMP, icmp->id,
                               icmp->sequence, mpls_count, mpls);
    }

    if (packet_length < icmp_ip_size) {
        return;
    }
    inner_ip = (struct IP6Header *) (icmp + 1);

    if (icmp->type == ICMP6_TIME_EXCEEDED) {
        handle_inner_ip6_packet(net_state, remote_addr,
                                ICMP_TIME_EXCEEDED, inner_ip, inner_size,
                                timestamp, mpls_count, mpls);
    }

    if (icmp->type == ICMP6_DEST_UNREACH) {
        if (icmp->code == ICMP6_PORT_UNREACH) {
            handle_inner_ip6_packet(net_state, remote_addr,
                                    ICMP_ECHOREPLY, inner_ip, inner_size,
                                    timestamp, mpls_count, mpls);
        } else {
            handle_inner_ip6_packet(net_state, remote_addr,
                                    ICMP_DEST_UNREACH, inner_ip, inner_size,
                                    timestamp, mpls_count, mpls);
        }
    }
}

/*
    We've received a new IPv4 ICMP packet.
    We'll check to see that it is a response to one of our probes, and
    if so, report the result of the probe to our command stream.
*/
void handle_received_ip4_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    const void *packet,
    int packet_length,
    struct timeval *timestamp)
{
    int ip_icmp_size = 0;
    const struct IPHeader *ip;
    const struct ICMPHeader *icmp;
    int icmp_length;

    if (net_state->platform.ip4_socket_raw) {
        ip_icmp_size += sizeof(struct IPHeader);
    }
    ip_icmp_size += sizeof(struct ICMPHeader);

    /*  Ensure that we don't access memory beyond the bounds of the packet  */
    if (packet_length < ip_icmp_size) {
        return;
    }

    if (net_state->platform.ip4_socket_raw) {
        ip = (struct IPHeader *) packet;
        if (ip->protocol != IPPROTO_ICMP) {
            return;
        }

        icmp = (struct ICMPHeader *) (ip + 1);
        icmp_length = packet_length - sizeof(struct IPHeader);
    } else {
        icmp = (struct ICMPHeader *) packet;
        icmp_length = packet_length;
    }

    handle_received_icmp4_packet(net_state, remote_addr, icmp, icmp_length,
                                 timestamp);
}

/*
    Unlike ICMPv6 raw sockets, unlike ICMPv4, don't include the IP header
    in received packets, so we can assume the packet we got starts
    with the ICMP packet.
*/
void handle_received_ip6_packet(
    struct net_state_t *net_state,
    const struct sockaddr_storage *remote_addr,
    const void *packet,
    int packet_length,
    struct timeval *timestamp)
{
    const struct ICMPHeader *icmp;

    icmp = (struct ICMPHeader *) packet;

    handle_received_icmp6_packet(net_state, remote_addr, icmp,
                                 packet_length, timestamp);
}