1 /*
2  *  $Id: icmp_mask.c,v 1.1.1.1 1999/05/18 15:33:43 dugsong Exp $
3  *
4  *  libnet
5  *  icmp.c - Build an ICMP_HOSTMASK packet at the link layer
6  *
7  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
8  *                           route|daemon9 <route@infonexus.com>
9  *  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #if (HAVE_CONFIG_H)
35 #include "../../include/config.h"
36 #endif
37 #include "../libnet_test.h"
38 
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42     int  c;
43     u_long src_ip, dst_ip;
44     char errbuf[256];
45     char *device = NULL;
46     struct link_int *l;
47 
48     printf("link layer ICMP packet building/writing test\n");
49 
50     src_ip  = 0;
51     dst_ip  = 0;
52     while ((c = getopt(argc, argv, "i:d:s:")) != EOF)
53     {
54         switch (c)
55         {
56             case 'd':
57                 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
58                 {
59                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
60                     exit(1);
61                 }
62                 break;
63             case 'i':
64                 device = optarg;
65                 break;
66             case 's':
67                 if (!(src_ip = libnet_name_resolve(optarg, 1)))
68                 {
69                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
70                     exit(1);
71                 }
72                 break;
73             default:
74                 exit(EXIT_FAILURE);
75         }
76     }
77 
78     if (!src_ip || !dst_ip || !device)
79     {
80         usage(argv[0]);
81         exit(EXIT_FAILURE);
82     }
83 
84     if ((l = libnet_open_link_interface(device, errbuf)) == NULL)
85     {
86         fprintf(stderr, "libnet_open_link_interface: %s\n", errbuf);
87         exit(EXIT_FAILURE);
88     }
89     c = send_icmp(l, device, src_ip, dst_ip);
90 
91     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
92 }
93 
94 
95 int
send_icmp(struct link_int * l,u_char * device,u_long src_ip,u_long dst_ip)96 send_icmp(struct link_int *l, u_char *device, u_long src_ip, u_long dst_ip)
97 {
98     int n;
99     u_char *buf;
100 
101     if (libnet_init_packet(ICMP_MASK_H + IP_H + ETH_H, &buf) == -1)
102     {
103         perror("no packet memory");
104         exit(EXIT_FAILURE);
105     }
106 
107     /*
108      *  Ethernet header
109      */
110     libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_IP, NULL, 0, buf);
111 
112     libnet_build_ip(ICMP_MASK_H,
113         0,                      /* IP tos */
114         242,                    /* IP ID */
115         0,                      /* Frag */
116         64,                     /* TTL */
117         IPPROTO_ICMP,           /* Transport protocol */
118         src_ip,                 /* Source IP */
119         dst_ip,                 /* Destination IP */
120         NULL,                   /* Pointer to payload (none) */
121         0,
122         buf + ETH_H);           /* Packet header memory */
123 
124     libnet_build_icmp_mask(ICMP_MASKREPLY,  /* type */
125 		    0,                      /* code */
126 		    242,                    /* id */
127 		    0,                      /* seq */
128 		    0xffffffff,             /* mask */
129 		    NULL,                   /* payload */
130 		    0,                      /* payload_s */
131 		    buf + ETH_H + IP_H);
132 
133     libnet_do_checksum(buf + ETH_H, IPPROTO_IP, IP_H);
134     libnet_do_checksum(buf + ETH_H, IPPROTO_ICMP, ICMP_H);
135 
136     printf("Packet as it will appear on the wire (give or take):");
137     libnet_hex_dump(buf, ETH_H + IP_H + ICMP_MASK_H, 0, stdout);
138     printf("\n");
139 
140     n = libnet_write_link_layer(l, device, buf, ETH_H + IP_H + ICMP_MASK_H);
141     if (n != ETH_H + IP_H + ICMP_MASK_H)
142     {
143         fprintf(stderr, "Oopz.  Only wrote %d bytes\n", n);
144     }
145     else
146     {
147         printf("Wrote %d byte ICMP packet through linktype %d\n", n, l->linktype);
148     }
149     libnet_destroy_packet(&buf);
150     return (n);
151 }
152 
153 
154 void
usage(u_char * name)155 usage(u_char *name)
156 {
157     fprintf(stderr, "usage: %s -i interface -s source_ip -d destination_ip\n", name);
158 }
159 
160 /* EOF */
161