Blame __dist_sample/sample/icmp_unreach.c

Packit Service b25606
/*
Packit Service b25606
 *  $Id: icmp_unreach.c,v 1.4 2004/03/16 18:40:59 mike Exp $
Packit Service b25606
 *
Packit Service b25606
 *  libnet 1.1
Packit Service b25606
 *  Build an ICMP unreachable packet
Packit Service b25606
 *
Packit Service b25606
 *  Hacked by Frederic Raynal <pappy@security-labs.org> to illustrate
Packit Service b25606
 *  the new API of ICMP error messages fixing Aaron's bugs.
Packit Service b25606
 *
Packit Service b25606
 *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
Packit Service b25606
 *  All rights reserved.
Packit Service b25606
 *
Packit Service b25606
 * Redistribution and use in source and binary forms, with or without
Packit Service b25606
 * modification, are permitted provided that the following conditions
Packit Service b25606
 * are met:
Packit Service b25606
 * 1. Redistributions of source code must retain the above copyright
Packit Service b25606
 *    notice, this list of conditions and the following disclaimer.
Packit Service b25606
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service b25606
 *    notice, this list of conditions and the following disclaimer in the
Packit Service b25606
 *    documentation and/or other materials provided with the distribution.
Packit Service b25606
 *
Packit Service b25606
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Packit Service b25606
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service b25606
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service b25606
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit Service b25606
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service b25606
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service b25606
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service b25606
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service b25606
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service b25606
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service b25606
 * SUCH DAMAGE.
Packit Service b25606
 *
Packit Service b25606
 */
Packit Service b25606
Packit Service b25606
#if (HAVE_CONFIG_H)
Packit Service b25606
#include "../include/config.h"
Packit Service b25606
#endif
Packit Service b25606
#include "./libnet_test.h"
Packit Service b25606
Packit Service b25606
int
Packit Service b25606
main(int argc, char **argv)
Packit Service b25606
{
Packit Service b25606
    int c, i;
Packit Service b25606
    libnet_t *l = NULL;
Packit Service b25606
    libnet_ptag_t ip_err = 0, icmp = 0, ip = 0, eth = 0;
Packit Service b25606
    u_long src_ip, dst_ip; 
Packit Service b25606
    u_char payload[8] = {0x11, 0x11, 0x22, 0x22, 0x00, 0x08, 0xc6, 0xa5};
Packit Service b25606
    u_long payload_s = 8;
Packit Service b25606
    int mode = LIBNET_LINK;
Packit Service b25606
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
Packit Service b25606
    printf("libnet 1.1 packet shaping: ICMP unreachable[link]\n"); 
Packit Service b25606
Packit Service b25606
    src_ip = 0;
Packit Service b25606
    dst_ip = 0;
Packit Service b25606
Packit Service b25606
    while((c = getopt(argc, argv, "d:s:r")) != EOF)
Packit Service b25606
    {
Packit Service b25606
        switch (c)
Packit Service b25606
        {
Packit Service b25606
            case 'd':
Packit Service b25606
                if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
Packit Service b25606
                {
Packit Service b25606
                    fprintf(stderr, "Bad destination IP address: %s\n", optarg);
Packit Service b25606
                    exit(1);
Packit Service b25606
                }
Packit Service b25606
                break;
Packit Service b25606
            case 's':
Packit Service b25606
                if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
Packit Service b25606
                {
Packit Service b25606
                    fprintf(stderr, "Bad source IP address: %s\n", optarg);
Packit Service b25606
                    exit(1);
Packit Service b25606
                }
Packit Service b25606
                break;
Packit Service b25606
            case 'r':
Packit Service b25606
                mode = LIBNET_RAW4;
Packit Service b25606
                break;
Packit Service b25606
        }
Packit Service b25606
    }
Packit Service b25606
    if (!src_ip || !dst_ip)
Packit Service b25606
    {
Packit Service b25606
        usage(argv[0]);
Packit Service b25606
        exit(EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    /*
Packit Service b25606
     *  Initialize the library.  Root priviledges are required.
Packit Service b25606
     */
Packit Service b25606
    l = libnet_init(
Packit Service b25606
            mode,                            /* injection type */
Packit Service b25606
            NULL,                                   /* network interface */
Packit Service b25606
            errbuf);                                /* errbuf */
Packit Service b25606
 
Packit Service b25606
    if (l == NULL)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "libnet_init() failed: %s", errbuf);
Packit Service b25606
        exit(EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
    
Packit Service b25606
    for (i=0; i<255; i++)
Packit Service b25606
    {
Packit Service b25606
        ip_err = libnet_build_ipv4(
Packit Service b25606
        LIBNET_IPV4_H + payload_s,              /* o length */
Packit Service b25606
        IPTOS_LOWDELAY | IPTOS_THROUGHPUT,      /* o IP tos */
Packit Service b25606
        (uint16_t)i,                           /* o IP ID */
Packit Service b25606
        0,                                      /* o frag */
Packit Service b25606
        64,                                     /* o TTL */
Packit Service b25606
        IPPROTO_UDP,                            /* o protocol */
Packit Service b25606
        0,                                      /* o checksum */
Packit Service b25606
        dst_ip,                                 /* o source IP */
Packit Service b25606
        src_ip,                                 /* o destination IP */
Packit Service b25606
        payload,                                /* payload */
Packit Service b25606
        payload_s,                              /* payload size */
Packit Service b25606
        l,
Packit Service b25606
        ip_err);
Packit Service b25606
    if (ip_err == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build error IPv4 header: %s\n", 
Packit Service b25606
                libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    icmp = libnet_build_icmpv4_unreach(
Packit Service b25606
        ICMP_UNREACH,                               /* type */
Packit Service b25606
        ICMP_UNREACH_PORT,                          /* code */
Packit Service b25606
        0,                                          /* checksum */
Packit Service b25606
        NULL,                                       /* payload */
Packit Service b25606
        0,                                          /* payload size */
Packit Service b25606
        l,                                          /* libnet handle */
Packit Service b25606
        icmp);
Packit Service b25606
    if (icmp == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build ICMP header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    ip = libnet_build_ipv4(
Packit Service b25606
        LIBNET_IPV4_H + LIBNET_ICMPV4_UNREACH_H +
Packit Service b25606
        LIBNET_IPV4_H + payload_s,                  /* length */
Packit Service b25606
        IPTOS_LOWDELAY | IPTOS_THROUGHPUT,          /* TOS */
Packit Service b25606
        (uint16_t)i + 1,                           /* IP ID */
Packit Service b25606
        0,                                          /* IP Frag */
Packit Service b25606
        64,                                         /* TTL */
Packit Service b25606
        IPPROTO_ICMP,                               /* protocol */
Packit Service b25606
        0,                                          /* checksum */
Packit Service b25606
        src_ip,                                     /* source IP */
Packit Service b25606
        dst_ip,                                     /* destination IP */
Packit Service b25606
        NULL,                                       /* payload */
Packit Service b25606
        0,                                          /* payload size */
Packit Service b25606
        l,                                          /* libnet handle */
Packit Service b25606
        ip);
Packit Service b25606
    if (ip == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    if (mode == LIBNET_LINK) {
Packit Service b25606
    eth = libnet_build_ethernet(
Packit Service b25606
       enet_dst,                                   /* ethernet destination */
Packit Service b25606
        enet_src,                                   /* ethernet source */
Packit Service b25606
        ETHERTYPE_IP,                               /* protocol type */
Packit Service b25606
        NULL,                                       /* payload */
Packit Service b25606
        0,                                          /* payload size */
Packit Service b25606
        l,                                          /* libnet handle */
Packit Service b25606
        eth);                                         /* libnet id */
Packit Service b25606
Packit Service b25606
    if (eth == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
    }
Packit Service b25606
    /*
Packit Service b25606
     *  Write it to the wire.
Packit Service b25606
     */
Packit Service b25606
    libnet_diag_dump_pblock(l);
Packit Service b25606
    c = libnet_write(l);
Packit Service b25606
    if (c == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
    else
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Wrote %d byte ICMP packet; check the wire.\n", c);
Packit Service b25606
    }
Packit Service b25606
}
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_SUCCESS);
Packit Service b25606
bad:
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_FAILURE);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
Packit Service b25606
void
Packit Service b25606
usage(char *name)
Packit Service b25606
{
Packit Service b25606
    fprintf(stderr, "usage: %s [-r] -s source_ip -d destination_ip\n ", name);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */