-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathicmp6_ancillary1.c
292 lines (246 loc) · 9.39 KB
/
icmp6_ancillary1.c
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
/* Copyright (C) 2011-2013 P.D. Buchan (pdbuchan@yahoo.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Send an IPv6 ICMP echo request packet.
// Changes hoplimit using ancillary data method.
// Use setsockopt() to bind socket to interface.
// Includes ICMP data.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // close()
#include <string.h> // strcpy, memset(), and memcpy()
#include <sys/socket.h> // struct msghdr
#include <netinet/in.h> // IPPROTO_IPV6, IPPROTO_ICMPV6
#include <netinet/ip.h> // IP_MAXPACKET (which is 65535)
#include <netinet/icmp6.h> // struct icmp6_hdr, ICMP6_ECHO_REQUEST
#include <netdb.h> // struct addrinfo
#include <sys/ioctl.h> // macro ioctl is defined
#include <bits/ioctls.h> // defines values for argument "request" of ioctl.
#include <net/if.h> // struct ifreq
#include <errno.h> // errno, perror()
// Define some constants.
#define IP6_HDRLEN 40 // IPv6 header length
#define ICMP_HDRLEN 8 // ICMP header length for echo request, excludes data
// Function prototypes
uint16_t checksum (uint16_t *, int);
char *allocate_strmem (int);
uint8_t *allocate_ustrmem (int);
int
main (int argc, char **argv)
{
int status, datalen, sd, cmsglen, hoplimit, psdhdrlen;
char *interface, *target, *source;
struct icmp6_hdr *icmphdr;
uint8_t *data, *outpack, *psdhdr;
struct addrinfo hints, *res;
struct sockaddr_in6 src, dst;
socklen_t srclen;
struct ifreq ifr;
struct msghdr msghdr;
struct cmsghdr *cmsghdr;
struct iovec iov[2];
// Allocate memory for various arrays.
source = allocate_strmem (40);
target = allocate_strmem (40);
interface = allocate_strmem (40);
data = allocate_ustrmem (IP_MAXPACKET);
outpack = allocate_ustrmem (IP_MAXPACKET);
psdhdr = allocate_ustrmem (IP_MAXPACKET);
// Interface to send packet through.
strcpy (interface, "eth0");
// Submit request for a socket descriptor to look up interface.
if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_IPV6)) < 0) {
perror ("socket() failed to get socket descriptor for using ioctl() ");
exit (EXIT_FAILURE);
}
// Use ioctl() to look up interface index which we will use to
// bind socket descriptor sd to specified interface with setsockopt() since
// none of the other arguments of sendto() specify which interface to use.
memset (&ifr, 0, sizeof (ifr));
snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
perror ("ioctl() failed to find interface ");
return (EXIT_FAILURE);
}
close (sd);
printf ("Index for interface %s is %i\n", interface, ifr.ifr_ifindex);
// Source IPv6 address: you need to fill this out
strcpy (source, "2001:db8::214:51ff:fe2f:1556");
// Destination URL or IPv6 address: you need to fill this out
strcpy (target, "ipv6.google.com");
// Fill out hints for getaddrinfo().
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = hints.ai_flags | AI_CANONNAME;
// Resolve source using getaddrinfo().
if ((status = getaddrinfo (source, NULL, &hints, &res)) != 0) {
fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status));
return (EXIT_FAILURE);
}
memcpy (&src, res->ai_addr, res->ai_addrlen);
srclen = res->ai_addrlen;
memcpy (psdhdr, src.sin6_addr.s6_addr, 16 * sizeof (uint8_t)); // Copy to checksum pseudo-header
freeaddrinfo (res);
// Resolve target using getaddrinfo().
if ((status = getaddrinfo (target, NULL, &hints, &res)) != 0) {
fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status));
return (EXIT_FAILURE);
}
memcpy (&dst, res->ai_addr, res->ai_addrlen);
memcpy (psdhdr + 16, dst.sin6_addr.s6_addr, 16 * sizeof (uint8_t)); // Copy to checksum pseudo-header
freeaddrinfo (res);
// Define first part of buffer outpack to be an ICMPV6 struct.
icmphdr = (struct icmp6_hdr *) outpack;
memset (icmphdr, 0, ICMP_HDRLEN * sizeof (uint8_t));
// Populate icmphdr portion of buffer outpack.
icmphdr->icmp6_type = ICMP6_ECHO_REQUEST;
icmphdr->icmp6_code = 0;
icmphdr->icmp6_cksum = 0;
icmphdr->icmp6_id = htons (5);
icmphdr->icmp6_seq = htons (300);
// ICMP data
datalen = 4;
data[0] = 'T';
data[1] = 'e';
data[2] = 's';
data[3] = 't';
// Append ICMP data.
memcpy (outpack + ICMP_HDRLEN, data, datalen * sizeof (uint8_t));
// Need a pseudo-header for checksum calculation. Define length. (RFC 2460)
// Length = source IP (16 bytes) + destination IP (16 bytes)
// + upper layer packet length (4 bytes) + zero (3 bytes)
// + next header (1 byte)
psdhdrlen = 16 + 16 + 4 + 3 + 1 + ICMP_HDRLEN + datalen;
// Compose the msghdr structure.
memset (&msghdr, 0, sizeof (msghdr));
msghdr.msg_name = &dst; // pointer to socket address structure
msghdr.msg_namelen = sizeof (dst); // size of socket address structure
memset (&iov, 0, sizeof (iov));
iov[0].iov_base = (uint8_t *) outpack;
iov[0].iov_len = ICMP_HDRLEN + datalen;
msghdr.msg_iov = iov; // scatter/gather array
msghdr.msg_iovlen = 1; // number of elements in scatter/gather array
// Initialize msghdr and control data to total length of the message to be sent.
// Allocate some memory for our cmsghdr data.
cmsglen = CMSG_SPACE (sizeof (int));
msghdr.msg_control = allocate_ustrmem (cmsglen);
msghdr.msg_controllen = cmsglen;
// Change hop limit to 255.
hoplimit = 255;
cmsghdr = CMSG_FIRSTHDR (&msghdr);
cmsghdr->cmsg_level = IPPROTO_IPV6;
cmsghdr->cmsg_type = IPV6_HOPLIMIT; // We want to change hop limit
cmsghdr->cmsg_len = CMSG_LEN (sizeof (int));
*((int *) CMSG_DATA (cmsghdr)) = hoplimit;
// Compute ICMPv6 checksum (RFC 2460).
// psdhdr[0 to 15] = source IPv6 address, set earlier.
// psdhdr[16 to 31] = destination IPv6 address, set earlier.
psdhdr[32] = 0; // Length should not be greater than 65535 (i.e., 2 bytes)
psdhdr[33] = 0; // Length should not be greater than 65535 (i.e., 2 bytes)
psdhdr[34] = (ICMP_HDRLEN + datalen) / 256; // Upper layer packet length
psdhdr[35] = (ICMP_HDRLEN + datalen) % 256; // Upper layer packet length
psdhdr[36] = 0; // Must be zero
psdhdr[37] = 0; // Must be zero
psdhdr[38] = 0; // Must be zero
psdhdr[39] = IPPROTO_ICMPV6;
memcpy (psdhdr + 40, outpack, (ICMP_HDRLEN + datalen) * sizeof (uint8_t));
icmphdr->icmp6_cksum = checksum ((uint16_t *) psdhdr, psdhdrlen);
printf ("Checksum: %x\n", ntohs (icmphdr->icmp6_cksum));
// Request a socket descriptor sd.
if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
fprintf (stderr, "Failed to get socket descriptor.\n");
exit (EXIT_FAILURE);
}
// Bind the socket descriptor to the source address.
if (bind (sd, (struct sockaddr *) &src, srclen) != 0) {
fprintf (stderr, "Failed to bind the socket descriptor to the source address.\n");
exit (EXIT_FAILURE);
}
// Bind socket to interface index.
if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr)) < 0) {
perror ("setsockopt() failed to bind to interface ");
exit (EXIT_FAILURE);
}
// Send packet.
if (sendmsg (sd, &msghdr, 0) < 0) {
perror ("sendmsg() failed ");
exit (EXIT_FAILURE);
}
close (sd);
// Free allocated memory.
free (source);
free (target);
free (interface);
free (data);
free (outpack);
free (psdhdr);
free (msghdr.msg_control);
return (EXIT_SUCCESS);
}
// IP checksum function
uint16_t
checksum (uint16_t *addr, int len)
{
int nleft = len;
int sum = 0;
uint16_t *w = addr;
uint16_t answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= sizeof (uint16_t);
}
if (nleft == 1) {
*(uint8_t *) (&answer) = *(uint8_t *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
// Allocate memory for an array of chars.
char *
allocate_strmem (int len)
{
void *tmp;
if (len <= 0) {
fprintf (stderr, "ERROR: Cannot allocate memory because len = %i in allocate_strmem().\n", len);
exit (EXIT_FAILURE);
}
tmp = (char *) malloc (len * sizeof (char));
if (tmp != NULL) {
memset (tmp, 0, len * sizeof (char));
return (tmp);
} else {
fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_strmem().\n");
exit (EXIT_FAILURE);
}
}
// Allocate memory for an array of unsigned chars.
uint8_t *
allocate_ustrmem (int len)
{
void *tmp;
if (len <= 0) {
fprintf (stderr, "ERROR: Cannot allocate memory because len = %i in allocate_ustrmem().\n", len);
exit (EXIT_FAILURE);
}
tmp = (uint8_t *) malloc (len * sizeof (uint8_t));
if (tmp != NULL) {
memset (tmp, 0, len * sizeof (uint8_t));
return (tmp);
} else {
fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_ustrmem().\n");
exit (EXIT_FAILURE);
}
}