Annotation of embedaddon/libnet/sample/icmp_unreach.c, revision 1.1.1.3

1.1       misho       1: /*
                      2:  *  $Id: icmp_unreach.c,v 1.4 2004/03/16 18:40:59 mike Exp $
                      3:  *
                      4:  *  libnet 1.1
                      5:  *  Build an ICMP unreachable packet
                      6:  *
                      7:  *  Hacked by Frederic Raynal <pappy@security-labs.org> to illustrate
                      8:  *  the new API of ICMP error messages fixing Aaron's bugs.
                      9:  *
                     10:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                     11:  *  All rights reserved.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  *
                     34:  */
                     35: 
                     36: #if (HAVE_CONFIG_H)
                     37: #include "../include/config.h"
                     38: #endif
                     39: #include "./libnet_test.h"
                     40: 
                     41: int
                     42: main(int argc, char **argv)
                     43: {
                     44:     int c, i;
                     45:     libnet_t *l = NULL;
                     46:     libnet_ptag_t ip_err = 0, icmp = 0, ip = 0, eth = 0;
                     47:     u_long src_ip, dst_ip; 
                     48:     u_char payload[8] = {0x11, 0x11, 0x22, 0x22, 0x00, 0x08, 0xc6, 0xa5};
                     49:     u_long payload_s = 8;
                     50:     int mode = LIBNET_LINK;
                     51:     char errbuf[LIBNET_ERRBUF_SIZE];
                     52: 
                     53:     printf("libnet 1.1 packet shaping: ICMP unreachable[link]\n"); 
                     54: 
                     55:     src_ip = 0;
                     56:     dst_ip = 0;
                     57: 
                     58:     while((c = getopt(argc, argv, "d:s:r")) != EOF)
                     59:     {
                     60:         switch (c)
                     61:         {
                     62:             case 'd':
                     63:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                     64:                 {
                     65:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
                     66:                     exit(1);
                     67:                 }
                     68:                 break;
                     69:             case 's':
                     70:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                     71:                 {
                     72:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
                     73:                     exit(1);
                     74:                 }
                     75:                 break;
                     76:             case 'r':
                     77:                 mode = LIBNET_RAW4;
                     78:                 break;
                     79:         }
                     80:     }
                     81:     if (!src_ip || !dst_ip)
                     82:     {
                     83:         usage(argv[0]);
                     84:         exit(EXIT_FAILURE);
                     85:     }
                     86: 
                     87:     /*
                     88:      *  Initialize the library.  Root priviledges are required.
                     89:      */
                     90:     l = libnet_init(
                     91:             mode,                            /* injection type */
                     92:             NULL,                                   /* network interface */
                     93:             errbuf);                                /* errbuf */
                     94:  
                     95:     if (l == NULL)
                     96:     {
                     97:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
                     98:         exit(EXIT_FAILURE);
                     99:     }
                    100:     
                    101:     for (i=0; i<255; i++)
                    102:     {
                    103:         ip_err = libnet_build_ipv4(
                    104:         LIBNET_IPV4_H + payload_s,              /* o length */
                    105:         IPTOS_LOWDELAY | IPTOS_THROUGHPUT,      /* o IP tos */
1.1.1.2   misho     106:         (uint16_t)i,                           /* o IP ID */
1.1       misho     107:         0,                                      /* o frag */
                    108:         64,                                     /* o TTL */
                    109:         IPPROTO_UDP,                            /* o protocol */
                    110:         0,                                      /* o checksum */
                    111:         dst_ip,                                 /* o source IP */
                    112:         src_ip,                                 /* o destination IP */
                    113:         payload,                                /* payload */
                    114:         payload_s,                              /* payload size */
                    115:         l,
                    116:         ip_err);
                    117:     if (ip_err == -1)
                    118:     {
                    119:         fprintf(stderr, "Can't build error IPv4 header: %s\n", 
                    120:                 libnet_geterror(l));
                    121:         goto bad;
                    122:     }
                    123: 
                    124:     icmp = libnet_build_icmpv4_unreach(
                    125:         ICMP_UNREACH,                               /* type */
                    126:         ICMP_UNREACH_PORT,                          /* code */
                    127:         0,                                          /* checksum */
                    128:         NULL,                                       /* payload */
                    129:         0,                                          /* payload size */
                    130:         l,                                          /* libnet handle */
                    131:         icmp);
                    132:     if (icmp == -1)
                    133:     {
                    134:         fprintf(stderr, "Can't build ICMP header: %s\n", libnet_geterror(l));
                    135:         goto bad;
                    136:     }
                    137: 
                    138:     ip = libnet_build_ipv4(
                    139:         LIBNET_IPV4_H + LIBNET_ICMPV4_UNREACH_H +
                    140:         LIBNET_IPV4_H + payload_s,                  /* length */
                    141:         IPTOS_LOWDELAY | IPTOS_THROUGHPUT,          /* TOS */
1.1.1.2   misho     142:         (uint16_t)i + 1,                           /* IP ID */
1.1       misho     143:         0,                                          /* IP Frag */
                    144:         64,                                         /* TTL */
                    145:         IPPROTO_ICMP,                               /* protocol */
                    146:         0,                                          /* checksum */
                    147:         src_ip,                                     /* source IP */
                    148:         dst_ip,                                     /* destination IP */
                    149:         NULL,                                       /* payload */
                    150:         0,                                          /* payload size */
                    151:         l,                                          /* libnet handle */
                    152:         ip);
                    153:     if (ip == -1)
                    154:     {
                    155:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
                    156:         goto bad;
                    157:     }
                    158: 
                    159:     if (mode == LIBNET_LINK) {
                    160:     eth = libnet_build_ethernet(
                    161:        enet_dst,                                   /* ethernet destination */
                    162:         enet_src,                                   /* ethernet source */
                    163:         ETHERTYPE_IP,                               /* protocol type */
                    164:         NULL,                                       /* payload */
                    165:         0,                                          /* payload size */
                    166:         l,                                          /* libnet handle */
                    167:         eth);                                         /* libnet id */
                    168: 
                    169:     if (eth == -1)
                    170:     {
                    171:         fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
                    172:         goto bad;
                    173:     }
                    174:     }
                    175:     /*
                    176:      *  Write it to the wire.
                    177:      */
                    178:     libnet_diag_dump_pblock(l);
                    179:     c = libnet_write(l);
                    180:     if (c == -1)
                    181:     {
                    182:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    183:         goto bad;
                    184:     }
                    185:     else
                    186:     {
                    187:         fprintf(stderr, "Wrote %d byte ICMP packet; check the wire.\n", c);
                    188:     }
                    189: }
                    190:     libnet_destroy(l);
                    191:     return (EXIT_SUCCESS);
                    192: bad:
                    193:     libnet_destroy(l);
                    194:     return (EXIT_FAILURE);
                    195: }
                    196: 
                    197: 
                    198: void
                    199: usage(char *name)
                    200: {
                    201:     fprintf(stderr, "usage: %s [-r] -s source_ip -d destination_ip\n ", name);
                    202: }
                    203: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>