git.infertux.com NAT-project / master nat.c
master

Tree @master (Download .tar.gz)

nat.c @masterraw · history · blame

  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
/*
   Copyright (C) 2010  Infertux <infertux@infertux.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   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 Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */

/**********************************************************************
 * Description: Masquerading NAT application                           *
 **********************************************************************/

#include <pcap.h>
#include <libnet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <semaphore.h>
#include <netdb.h>
#include "nat.h"
#include "table.h"
#include "arp.h"


/**
 * Return a staticly allocated string buffer containing the IP protocol name
 * from its ID
 *
 * @param protocol The IP protocol identifier
 * @return The IP protocol name (or the given ID if unknown)
 */
char *ip_protocol_ntoa(int protocol)
{
    static char str[8];


    switch (protocol) {
        case IPPROTO_ICMP: strcpy(str, "ICMP"); break;
        case IPPROTO_TCP: strcpy(str, "TCP"); break;
        case IPPROTO_UDP: strcpy(str, "UDP"); break;
        default: sprintf(str, "%d", protocol);
    }

    return str;
}

/**
 * Handle signals received by our program and quit properly if SIGINT
 *
 * @param signal The received signal ID
 */
static void signal_handler(int signal)
{
    if (signal == SIGINT) { /* exit gracefully */
        pcap_breakloop(interfaces[INTERFACE_INTERNAL].descriptor);
        pcap_breakloop(interfaces[INTERFACE_EXTERNAL].descriptor);

        /* stop listening */
        /*pcap_close(interfaces[INTERFACE_INTERNAL].descriptor);
          pcap_close(interfaces[INTERFACE_EXTERNAL].descriptor);*/
        /* segfault but why? */

        printf("Bye!\n");

        /* stop threads */
        pthread_kill(interfaces[INTERFACE_INTERNAL].thread, SIGKILL);
        pthread_kill(interfaces[INTERFACE_EXTERNAL].thread, SIGKILL);

        /* exit master */
        exit(EXIT_SUCCESS);
    }

    fprintf(stderr, "Caught unknown signal %d, aborting.\n", signal);
    exit(EXIT_FAILURE);
}

/**
 * Print a IP packet
 *
 * @param packet_header The packet header
 * @param packet        The packet data
 */
void packet_print(const struct pcap_pkthdr *packet_header, const u_char *packet)
{
    const struct ethhdr *eth = (struct ethhdr *)packet;
    const struct iphdr *ip;
    char source[18], dest[18];


    /*** OSI level 2 ***/
    if (packet_header->caplen < ETHER_HDRLEN) {
        fprintf(stderr, "Packet length less than ethernet header length\n");
        return;
    }

    /* string returned by ether_ntoa() is in a statically allocated buffer */
    strcpy(source, ether_ntoa((struct ether_addr *)&(eth->h_source)));
    strcpy(dest, ether_ntoa((struct ether_addr *)&(eth->h_dest)));
    printf("ETH  src=%s dst=%s\n", source, dest);

    /*** OSI level 3 ***/
    /* jump pass the ethernet header */
    ip = (struct iphdr*)(packet + sizeof(struct ether_header));

    /* check to see we have a packet of valid length */
    if (packet_header->len - sizeof(struct ethhdr) < sizeof(struct iphdr)) {
        fprintf(stderr, "Truncated IP");
        return;
    }

    /* check header length */
    if(ip->ihl < 5) {
        printf("Bad header length: %d\n", ip->ihl);
        return;
    }

    /* check IP version */
    if(ip->version != IPVERSION) {
        fprintf(stderr, "Unknown version: IPv%d\n", ip->version);
        return;
    }

    /* string returned by inet_ntoa() is in a statically allocated buffer */
    strcpy(source, inet_ntoa(*(struct in_addr *)&ip->saddr));
    strcpy(dest, inet_ntoa(*(struct in_addr *)&ip->daddr));

    printf("IP   src=%s dst=%s\n", source, dest);
}

/**
 * Compute IP/ICMP checksum
 * Note: Before starting the calculation, the checksum field must be zero
 *
 * @param addr   The pointer on the begginning of the packet
 * @param length Length which be computed
 * @return The 16 bits unsigned integer checksum
 */
u_int16_t ip_icmp_calc_checksum(const u_int16_t *addr, int length)
{
    int left = length;
    const u_int16_t *w = addr;
    u_int16_t answer;
    register int sum = 0;

    while (left > 1)  {
        sum += *w++;
        left -= 2;
    }

    /* mop up an odd byte, if necessary */
    if (left == 1)
        sum += htons(*(u_char *)w << 8);

    /* add back carry outs from top 16 bits to low 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);    /* add hi 16 to low 16 */
    sum += (sum >> 16);    /* add carry */
    answer = ~sum; /* truncate to 16 bits */
    return answer;
}

/**
 * Compute TCP/UDP checksum
 * Note: Before starting the calculation, the checksum field must be zero
 *
 * @param ip       The pointer on the IP packet
 * @param tcp_udp  The pointer on the TCP/UDP packet
 * @param protocol The protocol (IPPROTO_TCP or IPPROTO_UDP)
 * @return The 16 bits unsigned integer checksum
 */
u_int16_t tcp_udp_calc_checksum(const struct iphdr *ip,
        union tcp_udp_u *tcp_udp, int protocol)
{
    uint16_t ip_tlen = ntohs(ip->tot_len);
    uint16_t tcp_udp_tlen = ip_tlen - ip->ihl * 4;
    uint16_t tcp_udp_hlen = tcp_udp_tlen - sizeof(tcp_udp);
    uint16_t tcp_udp_dlen = tcp_udp_tlen - tcp_udp_hlen;
    uint16_t new_tcp_udp_len = sizeof(pseudo_header) + sizeof(tcp_udp)
        + tcp_udp_dlen;
    unsigned short *new_tcp_udp;

    switch(protocol) {
        case IPPROTO_TCP:
            tcp_udp->tcp.check = 0;
            break;
        case IPPROTO_UDP:
            tcp_udp->udp.check = 0;
            break;
        default:
            return 0;
    }

    pseudo_header.src_addr = ip->saddr;
    pseudo_header.dst_addr = ip->daddr;
    pseudo_header.zero = 0;
    pseudo_header.proto = protocol;
    pseudo_header.length = htons(tcp_udp_tlen);

    if ((new_tcp_udp = (unsigned short *)malloc(
                    new_tcp_udp_len * sizeof(unsigned short))) == NULL) {
        perror("Unable to allocate the pseudo TCP/UDP header");
        exit(1);
    }

    memcpy((u_short *)new_tcp_udp, &pseudo_header, sizeof(pseudo_header));
    memcpy((u_short *)new_tcp_udp + sizeof(pseudo_header),
            (u_char *)tcp_udp, sizeof(tcp_udp));
    memcpy((u_short *)new_tcp_udp + sizeof(pseudo_header) + sizeof(tcp_udp),
            (u_char *)tcp_udp + sizeof(tcp_udp), tcp_udp_dlen);

    return ip_icmp_calc_checksum((const u_short *)new_tcp_udp,
            new_tcp_udp_len);
}

/**
 * Nat a packet
 *
 * @param from_interface_id The originated interface ID
 * @param packet_header     The packet header
 * @param packet            The packet data
 * @return Whether the packet has been NATed or not (1 or 0)
 */
static int packet_nat(u_char from_interface_id,
        struct pcap_pkthdr *packet_header, u_char *packet)
{
    struct ethhdr *eth = (struct ethhdr *)packet;
    struct iphdr *ip = (struct iphdr*)(packet + sizeof(struct ethhdr));
    u_int16_t *src_port, *dst_port;
    struct table_record *record;
    struct tcphdr *tcp;
    struct udphdr *udp;
    struct icmphdr *icmp;
    int natted = 0;


    /* set ports pointers */
    switch (ip->protocol) {
        case IPPROTO_TCP:
            tcp = (struct tcphdr *)(packet + sizeof(struct ethhdr) +
                    sizeof(struct iphdr));
            src_port = &(tcp->source);
            dst_port = &(tcp->dest);
            break;

        case IPPROTO_UDP:
            udp = (struct udphdr *)(packet + sizeof(struct ethhdr) +
                    sizeof(struct iphdr));
            src_port = &(udp->source);
            dst_port = &(udp->dest);
            break;

        case IPPROTO_ICMP:
            icmp = (struct icmphdr *)(packet + sizeof(struct ethhdr));
            /* hack a bit: we use ICMP "identifier" field instead of port ;) */
            src_port = dst_port = &(icmp->un.echo.id);
            break;

        default:
            fprintf(stderr, "Unknown IP protocol, do not NAT the packet\n");
            return 0;
    }


    printf("%-4s src=%u dst=%u\n", ip_protocol_ntoa(ip->protocol),
            htons(*src_port), htons(*dst_port));


    if (from_interface_id == INTERFACE_INTERNAL) {

        record = table_outbound(ip->saddr, eth->h_source, htons(*src_port),
                ip->daddr);

        /* change source MAC */
        memcpy(eth->h_source, interfaces[INTERFACE_EXTERNAL].mac, ETH_ALEN);

        /* change dest MAC */
        memcpy(eth->h_dest, gateway.mac, ETH_ALEN);

        /* change source IP */
        ip->saddr = interfaces[INTERFACE_EXTERNAL].ip;

        /* change source port */
        *src_port = ntohs(record->external_port);

        natted = 1;

    } else {

        /* if we known the sender */
        if ((record = table_inbound(ip->saddr, htons(*src_port)))) {

            /* change source MAC */
            memcpy(eth->h_source, interfaces[INTERFACE_INTERNAL].mac, ETH_ALEN);

            /* change dest MAC */
            memcpy(eth->h_dest, record->internal_mac, ETH_ALEN);

            /* change dest IP */
            ip->daddr = record->internal_ip;

            /* change dest port */
            *dst_port = ntohs(record->internal_port);

            natted = 1;

        }

    }

    if (natted) {
        /* compute TCP/UDP checksum */
        switch (ip->protocol) {
            case IPPROTO_TCP:
                tcp->check = tcp_udp_calc_checksum(ip, (union tcp_udp_u *)tcp,
                        IPPROTO_TCP);
                break;

            case IPPROTO_UDP:
                udp->check = tcp_udp_calc_checksum(ip, (union tcp_udp_u *)udp,
                        IPPROTO_UDP);
                break;

            case IPPROTO_ICMP:
                icmp->checksum = 0;
                icmp->checksum = ip_icmp_calc_checksum((u_short *)icmp,
                        ip->tot_len - ip->ihl * 4);
                break;
        }

        /* compute IP checksum */
        /* checksum field must be null before computing new checksum */
        ip->check = 0;
        ip->check = ip_icmp_calc_checksum((u_short *)ip, ip->ihl * 4);

        return 1;
    }

    return 0;
}

/**
 * Inject a packet on an interface
 *
 * @param from_interface_id The interface ID where the packet should be sent
 * @param packet_header     The packet header
 * @param packet            The packet data
 */
static void packet_inject(u_char interface_id,
        const struct pcap_pkthdr *packet_header,
        const u_char *packet)
{
    int n;

    if ((n = pcap_inject(interfaces[interface_id].descriptor, packet,
                    packet_header->len)) == -1) {
        fprintf(stderr, "Injection failure on %s\n",
                interfaces[interface_id].device);
        exit(EXIT_FAILURE);
    }
}

/**
 * Callback function for all captured packets, NAT and print them
 *
 * @param from_interface_id The interface ID from where the packet was captured
 * @param packet_header     The packet header
 * @param packet            The packet data
 */
static void callback(u_char *from_interface_id,
        const struct pcap_pkthdr *packet_header,
        const u_char *packet)
{
    sem_wait(&mutex); /* to avoid interlaced printf() on several packets */

    printf("\nNew IP packet on %s:\n", interfaces[*from_interface_id].device);
    packet_print(packet_header, packet);

    if (packet_nat(*from_interface_id, (struct pcap_pkthdr *)packet_header,
                (u_char *)packet)) { /* the packet have to be natted */
        packet_print(packet_header, packet);
        table_print(PRINT_ALL);
        packet_inject((*from_interface_id == INTERFACE_INTERNAL ?
                    INTERFACE_EXTERNAL : INTERFACE_INTERNAL),
                packet_header, packet); /* forward our natted packet */
    }

    sem_post(&mutex); /* release our semaphore */
}

/**
 * Open an interface in listening mode
 *
 * @param interface_id The interface ID
 */
static void listen_interface(int interface_id)
{
    uint32_t subnet, netmask;
    char subnet_str[18], netmask_str[18];
    struct bpf_program fp; /* hold compiled program */
    char errbuf[PCAP_ERRBUF_SIZE];
    char filter[] = "ip"; /* handle only IP packets */


    printf("Trying to listen on %s...\n", interfaces[interface_id].device);

    /* get MAC & IP addresses */
    get_interface_addresses(interface_id);

    /* ask pcap for the network subnet and mask of the interface */
    if (pcap_lookupnet(interfaces[interface_id].device, &subnet, &netmask,
                errbuf) < 0) {
        fprintf(stderr, "Could not get local IP network info: %s\n", errbuf);
        exit(EXIT_FAILURE);
    }

    strcpy(subnet_str, inet_ntoa(*(struct in_addr *)&subnet));
    strcpy(netmask_str, inet_ntoa(*(struct in_addr *)&netmask));
    printf("%s: hwaddr = %16s, ipaddr = %13s,\n"
            "      subnet = %13s, netmask = %13s\n",
            interfaces[interface_id].device,
            ether_ntoa((struct ether_addr *)&(interfaces[interface_id].mac)),
            inet_ntoa(*(struct in_addr *)&(interfaces[interface_id].ip)),
            subnet_str, netmask_str);

    /* open interface for reading in promiscuous mode */
    if ((interfaces[interface_id].descriptor =
                pcap_open_live(interfaces[interface_id].device, BUFSIZ, 1, -1,
                    errbuf)) == NULL) {
        fprintf(stderr, "pcap_open_live: %s\n", errbuf);
        exit(EXIT_FAILURE);
    }

    /* lets try and compile the filter */
    if (pcap_compile(interfaces[interface_id].descriptor, &fp, filter, 0,
                subnet) == -1) {
        fprintf(stderr, "Error compiling filter\n");
        exit(EXIT_FAILURE);
    }

    /* set the compiled filter */
    if (pcap_setfilter(interfaces[interface_id].descriptor, &fp) == -1) {
        fprintf(stderr, "Error setting filter\n");
        exit(EXIT_FAILURE);
    }

    /* everything is good! */
    printf("Listening on %s...\n", interfaces[interface_id].device);

    /* loop forever */
    pcap_loop(interfaces[interface_id].descriptor, 0, callback,
            (u_char *)(&interface_id));
}

/**
 * Grab and store MAC & IP addresses for an interface
 *
 * @param interface_id The ID of the NAT interface
 */
void get_interface_addresses(int interface_id)
{
    libnet_t *libnet = NULL;
    char error[LIBNET_ERRBUF_SIZE];
    struct libnet_ether_addr *mac_addr;
    u_int32_t ip_addr;

    /* open libnet */
    libnet = libnet_init(LIBNET_LINK, interfaces[interface_id].device, error);

    /* get MAC address */
    mac_addr = libnet_get_hwaddr(libnet);
    memcpy(interfaces[interface_id].mac, mac_addr, ETH_ALEN);

    /* get IP address */
    ip_addr = libnet_get_ipaddr4(libnet);
    memcpy(&(interfaces[interface_id].ip), &ip_addr, IP_ALEN);

    /* print interface addresses */
#ifdef DEBUG
    printf("%s MAC: %s\n", interfaces[interface_id].device,
            ether_ntoa((struct ether_addr *)&(interfaces[interface_id].mac)));
    printf("%s IP: %s\n", interfaces[interface_id].device,
            inet_ntoa(*(struct in_addr *)&(interfaces[interface_id].ip)));
#endif
}

/**
 * Where it all begins! :D
 */
int main(int argc, char **argv)
{
    int sig;
    struct in_addr gateway_addr;


    /* must be r00t to listen on interfaces */
    if (getuid() && geteuid()) {
        fprintf(stderr, "R007 M3 P13453!\n");
        return EXIT_FAILURE;
    }

    /* check arguments count */
    if(argc != 4) {
        printf("Usage: %s internal_interface external_interface gateway_IP\n",
                argv[0]);
        return EXIT_SUCCESS;
    }

    /* setup our signals handler */
    for (sig = 1; sig < NSIG; sig++)
        signal(SIGINT, signal_handler);

    /* store interface names (e.g. "eth0") */
    interfaces[INTERFACE_INTERNAL].device = argv[1];
    interfaces[INTERFACE_EXTERNAL].device = argv[2];

    if (inet_aton(argv[3], &gateway_addr) == 0) {
        perror("gateway");
        exit(EXIT_FAILURE);
    }

    gateway.ip = gateway_addr.s_addr;
    gateway.interface = INTERFACE_EXTERNAL;

    printf("\nRetrieving gateway addresses...\n");
    arp_request(argv[3], interfaces[gateway.interface].device,
            (u_char *)&gateway.mac);

    printf("Gateway MAC: %s\n",
            ether_ntoa((struct ether_addr *)&(gateway.mac)));
    printf("Gateway IP:  %s\n", inet_ntoa(*(struct in_addr *)&(gateway.ip)));

    printf("\n");

    /* set our semaphore for future usage */
    sem_init(&mutex, 0, 1);

    /* start our two threads for interfaces listenning */
    if (pthread_create(&(interfaces[INTERFACE_INTERNAL].thread), NULL,
                listen_interface, INTERFACE_INTERNAL)) {
        perror("pthread_create");
        exit(EXIT_FAILURE);
    }

    if (pthread_create(&(interfaces[INTERFACE_EXTERNAL].thread), NULL,
                listen_interface, INTERFACE_EXTERNAL)) {
        perror("pthread_create");
        exit(EXIT_FAILURE);
    }

    /* wait for thread ending, it should never happen because they stop from
     * signals handler */
    if (pthread_join(interfaces[INTERFACE_INTERNAL].thread, NULL))
        perror("pthread_join");

    if (pthread_join(interfaces[INTERFACE_EXTERNAL].thread, NULL))
        perror("pthread_join");

    fprintf(stderr, "\nOops!\n");

    return EXIT_FAILURE;
}