Blame __dist_sample/sample/icmp_redirect.c

Packit Service b25606
/*
Packit Service b25606
 *  $Id: icmp_redirect.c,v 1.3 2004/03/16 18:40:58 mike Exp $
Packit Service b25606
 *
Packit Service b25606
 *  libnet 1.1
Packit Service b25606
 *  Build an ICMP redirect packet
Packit Service b25606
 *
Packit Service b25606
 *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
Packit Service b25606
 *  Copyright (c) 2003 Alberto Ornaghi <alor@antifork.org>
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;
Packit Service b25606
    libnet_t *l;
Packit Service b25606
    libnet_ptag_t t;
Packit Service b25606
    u_long src_ip, dst_ip, gw_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
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
Packit Service b25606
    printf("libnet 1.1 packet shaping: ICMP redirect[link]\n"); 
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
            LIBNET_LINK,                            /* 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
    src_ip = 0;
Packit Service b25606
    dst_ip = 0;
Packit Service b25606
    gw_ip = 0;
Packit Service b25606
    while((c = getopt(argc, argv, "d:s:g:")) != 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 'g':
Packit Service b25606
                if ((gw_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
Packit Service b25606
                {
Packit Service b25606
                    fprintf(stderr, "Bad gateway IP address: %s\n", optarg);
Packit Service b25606
                    exit(1);
Packit Service b25606
                }
Packit Service b25606
                break;
Packit Service b25606
        }
Packit Service b25606
    }
Packit Service b25606
    if (!src_ip || !dst_ip || !gw_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
    t = libnet_build_ipv4(
Packit Service b25606
        LIBNET_IPV4_H + payload_s,                  /* length */
Packit Service b25606
        IPTOS_LOWDELAY | IPTOS_THROUGHPUT,          /* TOS */
Packit Service b25606
        0x42,                                       /* 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
        dst_ip,                                     /* source IP */
Packit Service b25606
        src_ip,                                     /* destination IP */
Packit Service b25606
        payload,                                    /* payload */
Packit Service b25606
        payload_s,                                  /* payload size */
Packit Service b25606
        l,                                          /* libnet handle */
Packit Service b25606
        0);
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build error IP header: %s\n", 
Packit Service b25606
                libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
   t = libnet_build_icmpv4_redirect(
Packit Service b25606
           ICMP_REDIRECT,                 /* type */
Packit Service b25606
           ICMP_REDIRECT_HOST,            /* code */
Packit Service b25606
           0,                             /* checksum */
Packit Service b25606
           gw_ip,
Packit Service b25606
           NULL,
Packit Service b25606
           0,
Packit Service b25606
           l,             /* libnet handle */
Packit Service b25606
           0);                            /* libnet id */
Packit Service b25606
    if (t == -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
    t = libnet_build_ipv4(
Packit Service b25606
        LIBNET_IPV4_H + LIBNET_ICMPV4_REDIRECT_H +
Packit Service b25606
        LIBNET_IPV4_H + payload_s,                  /* length */
Packit Service b25606
        IPTOS_LOWDELAY | IPTOS_THROUGHPUT,          /* TOS */
Packit Service b25606
        0xee,                                       /* 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
        0);
Packit Service b25606
    if (t == -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
    t = 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
        0);                                         /* libnet id */
Packit Service b25606
    if (t == -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
    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
    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 -s source_ip -d destination_ip -g gateway_ip\n ", name);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */