-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathicmp6_6to4.c
536 lines (433 loc) · 16.5 KB
/
icmp6_6to4.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* 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 through an IPv4 tunnel (6to4)
// via raw socket at the link layer (ethernet frame).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // close()
#include <string.h> // strcpy, memset(), and memcpy()
#include <netinet/ip.h> // struct ip and IP_MAXPACKET (which is 65535)
#include <netinet/in.h> // IPPROTO_RAW, IPPROTO_IP, IPPROTO_IPV6, IPPROTO_ICMPV6, INET_ADDRSTRLEN, INET6_ADDRSTRLEN
#include <netinet/ip6.h> // struct ip6_hdr
#include <netinet/icmp6.h> // struct icmp6_hdr
#include <arpa/inet.h> // inet_pton() and inet_ntop()
#include <netdb.h> // struct addrinfo
#include <sys/ioctl.h> // macro ioctl is defined
#include <bits/ioctls.h> // defines values for argument "request" of ioctl. Here, we need SIOCGIFHWADDR
#include <net/if.h> // struct ifreq
#include <linux/if_ether.h> // ETH_P_IP = 0x0800, ETH_P_IPV6 = 0x86DD
#include <linux/if_packet.h> // struct sockaddr_ll (see man 7 packet)
#include <net/ethernet.h>
#include <errno.h> // errno, perror()
// Define some constants.
#define ETH_HDRLEN 14 // Ethernet header length
#define IP4_HDRLEN 20 // IPv4 header length
#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);
uint16_t icmp6_checksum (struct ip6_hdr, struct icmp6_hdr, uint8_t *, int);
char *allocate_strmem (int);
uint8_t *allocate_ustrmem (int);
int *allocate_intmem (int);
int
main (int argc, char **argv)
{
int i, status, *ip4_flags, datalen, sd, bytes, psdhdrlen, frame_length;
struct ip ip4hdr;
struct ip6_hdr ip6hdr;
struct icmp6_hdr icmphdr;
struct addrinfo hints;
struct addrinfo *res;
struct sockaddr_in6 *ipv6, src, dst;
socklen_t srclen;
struct sockaddr_ll device;
struct ifreq ifr;
uint8_t *ether_frame, *src_mac, *dst_mac, *data, *psdhdr;
char *interface, *target4, *target6, *source4, *source6, *src_ip, *dst_ip;
void *tmp;
// We will add 4 bytes of ICMP data later. Can be anything you choose.
datalen = 4;
// Allocate memory for various arrays.
interface = allocate_strmem (40);
target4 = allocate_strmem (INET_ADDRSTRLEN);
target6 = allocate_strmem (INET6_ADDRSTRLEN);
source4 = allocate_strmem (INET_ADDRSTRLEN);
source6 = allocate_strmem (INET6_ADDRSTRLEN);
src_ip = allocate_strmem (INET6_ADDRSTRLEN);
dst_ip = allocate_strmem (INET6_ADDRSTRLEN);
src_mac = allocate_ustrmem (6);
dst_mac = allocate_ustrmem (6);
ether_frame = allocate_ustrmem (IP_MAXPACKET);
ip4_flags = allocate_intmem (4);
data = 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 (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {
perror ("socket() failed to get socket descriptor for using ioctl() ");
exit (EXIT_FAILURE);
}
// Use ioctl() to look up interface name and get its MAC address.
memset (&ifr, 0, sizeof (ifr));
snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
if (ioctl (sd, SIOCGIFHWADDR, &ifr) < 0) {
perror ("ioctl() failed to get source MAC address ");
return (EXIT_FAILURE);
}
close (sd);
// Copy source MAC address.
memcpy (src_mac, ifr.ifr_hwaddr.sa_data, 6 * sizeof (uint8_t));
// Report source MAC address to stdout.
printf ("MAC address for interface %s is ", interface);
for (i=0; i<5; i++) {
printf ("%02x:", src_mac[i]);
}
printf ("%02x\n", src_mac[5]);
// Fill out sockaddr_ll.
device.sll_family = AF_PACKET;
memcpy (device.sll_addr, src_mac, 6 * sizeof (uint8_t));
device.sll_halen = htons (6);
// Find interface index from interface name and store index in
// struct sockaddr_ll device, which will be used as an argument of sendto().
if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
perror ("if_nametoindex() failed to obtain interface index ");
exit (EXIT_FAILURE);
}
printf ("Index for interface %s is %i\n", interface, device.sll_ifindex);
// Set destination MAC address: you need to fill these out
dst_mac[0] = 0xff;
dst_mac[1] = 0xff;
dst_mac[2] = 0xff;
dst_mac[3] = 0xff;
dst_mac[4] = 0xff;
dst_mac[5] = 0xff;
// Source IPv4 address: you need to fill this out
strcpy (source4, "192.168.1.132");
// Source IPv6 address: you need to fill this out
strcpy (source6, "2001:db8::214:51ff:fe2f:1556");
// IPv4 target as the 6to4 anycast address (do not change)
strcpy (target4, "192.88.99.1");
// Target URL or IPv6 address: you need to fill this out
strcpy (target6, "ipv6.google.com");
// Fill out hints for getaddrinfo().
memset (&hints, 0, sizeof (hints));
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 (source6, 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;
ipv6 = (struct sockaddr_in6 *) res->ai_addr;
tmp = &(ipv6->sin6_addr);
memcpy (psdhdr, tmp, INET_ADDRSTRLEN); // Copy to checksum pseudo-header
if (inet_ntop (AF_INET6, tmp, src_ip, INET6_ADDRSTRLEN) == NULL) {
status = errno;
fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
freeaddrinfo (res);
// Resolve target using getaddrinfo().
if ((status = getaddrinfo (target6, NULL, &hints, &res)) != 0) {
fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status));
return (EXIT_FAILURE);
}
memcpy (&dst, res->ai_addr, res->ai_addrlen);
ipv6 = (struct sockaddr_in6 *) res->ai_addr;
tmp = &(ipv6->sin6_addr);
memcpy (psdhdr + 16, tmp, INET_ADDRSTRLEN); // Copy to checksum pseudo-header
if (inet_ntop (AF_INET6, tmp, dst_ip, INET6_ADDRSTRLEN) == NULL) {
status = errno;
fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
freeaddrinfo (res);
// Copy options data into options buffer.
// datalen was set to 4 above
data[0] = 'T';
data[1] = 'e';
data[2] = 's';
data[3] = '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;
// IPv4 header (Section 3.5 of RFC 4213)
// IPv4 header
// IPv4 header length (4 bits): Number of 32-bit words in header = 5
ip4hdr.ip_hl = sizeof (struct ip) / sizeof (uint32_t);
// Internet Protocol version (4 bits): IPv4
ip4hdr.ip_v = 4;
// Type of service (8 bits)
ip4hdr.ip_tos = 0;
// Total length of datagram (16 bits): IPv4 header + IPv6 header + ICMP header + ICMP data
ip4hdr.ip_len = htons (IP4_HDRLEN + IP6_HDRLEN + ICMP_HDRLEN + datalen);
// ID sequence number (16 bits): unused, since single datagram
ip4hdr.ip_id = htons (0);
// Flags, and Fragmentation offset (3, 13 bits): 0 since single datagram
// Zero (1 bit)
ip4_flags[0] = 0;
// Do not fragment flag (1 bit)
ip4_flags[1] = 1;
// More fragments following flag (1 bit)
ip4_flags[2] = 0;
// Fragmentation offset (13 bits)
ip4_flags[3] = 0;
ip4hdr.ip_off = htons ((ip4_flags[0] << 15)
+ (ip4_flags[1] << 14)
+ (ip4_flags[2] << 13)
+ ip4_flags[3]);
// Time-to-Live (8 bits): use maximum value
ip4hdr.ip_ttl = 255;
// Transport layer protocol (8 bits): 41 for IPv6 (Section 3.5 of RFC 4213)
ip4hdr.ip_p = IPPROTO_IPV6;
// Source IPv4 address (32 bits)
if ((status = inet_pton (AF_INET, source4, &(ip4hdr.ip_src))) != 1) {
fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
// Destination IPv4 address (32 bits)
if ((status = inet_pton (AF_INET, target4, &(ip4hdr.ip_dst))) != 1) {
fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
// IPv4 header checksum (16 bits) - set to 0 when calculating checksum
ip4hdr.ip_sum = 0;
ip4hdr.ip_sum = checksum ((uint16_t *) &ip4hdr, IP4_HDRLEN);
// IPv6 header
// IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
ip6hdr.ip6_flow = htonl ((6 << 28) | (0 << 20) | 0);
// Payload length (16 bits)
ip6hdr.ip6_plen = htons (ICMP_HDRLEN + datalen);
// Next header (8 bits) - 58 for ICMPV6
ip6hdr.ip6_nxt = IPPROTO_ICMPV6;
// Hop limit (8 bits) - use 255 (RFC 4861)
ip6hdr.ip6_hops = 255;
// Source IPv6 address (128 bits)
if ((status = inet_pton (AF_INET6, src_ip, &(ip6hdr.ip6_src))) != 1) {
fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
// Destination IPv6 address (128 bits)
if ((status = inet_pton (AF_INET6, dst_ip, &(ip6hdr.ip6_dst))) != 1) {
fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
// ICMP header
// Message Type (8 bits): echo request
icmphdr.icmp6_type = ICMP6_ECHO_REQUEST;
// Message Code (8 bits): echo request
icmphdr.icmp6_code = 0;
// Identifier (16 bits): usually pid of sending process - pick a number
icmphdr.icmp6_id = htons (1000);
// Sequence Number (16 bits): starts at 0
icmphdr.icmp6_seq = htons (0);
// ICMP header checksum (16 bits): set to 0 when calculating checksum
icmphdr.icmp6_cksum = 0;
icmphdr.icmp6_cksum = icmp6_checksum (ip6hdr, icmphdr, data, datalen);
// Fill out ethernet frame header.
// Ethernet frame length = ethernet header (MAC + MAC + ethernet type) + ethernet data (IP4 header + IP6 header + ICMP header + ICMP data)
frame_length = 6 + 6 + 2 + IP4_HDRLEN + IP6_HDRLEN + ICMP_HDRLEN + datalen;
// Destination and Source MAC addresses
memcpy (ether_frame, dst_mac, 6 * sizeof (uint8_t));
memcpy (ether_frame + 6, src_mac, 6 * sizeof (uint8_t));
// Next is ethernet type code (ETH_P_IP for IPv4).
// http://www.iana.org/assignments/ethernet-numbers
ether_frame[12] = ETH_P_IP / 256;
ether_frame[13] = ETH_P_IP % 256;
// Next is ethernet frame data (IPv4 header + ICMP header + ICMP data).
// IPv4 header
memcpy (ether_frame + ETH_HDRLEN, &ip4hdr, IP4_HDRLEN * sizeof (uint8_t));
// IPv6 header
memcpy (ether_frame + ETH_HDRLEN + IP4_HDRLEN, &ip6hdr, IP6_HDRLEN * sizeof (uint8_t));
// ICMP header
memcpy (ether_frame + ETH_HDRLEN + IP4_HDRLEN + IP6_HDRLEN, &icmphdr, ICMP_HDRLEN * sizeof (uint8_t));
// ICMP data
memcpy (ether_frame + ETH_HDRLEN + IP4_HDRLEN + IP6_HDRLEN + ICMP_HDRLEN, data, datalen * sizeof (uint8_t));
// Submit request for a raw socket descriptor.
if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {
perror ("socket() failed ");
exit (EXIT_FAILURE);
}
// Send ethernet frame to socket.
if ((bytes = sendto (sd, ether_frame, frame_length, 0, (struct sockaddr *) &device, sizeof (device))) <= 0) {
perror ("sendto() failed");
exit (EXIT_FAILURE);
}
// Close socket.
close (sd);
// Free allocated memory.
free (interface);
free (src_mac);
free (dst_mac);
free (ether_frame);
free (target4);
free (target6);
free (source4);
free (source6);
free (src_ip);
free (dst_ip);
free (ip4_flags);
free (data);
free (psdhdr);
return (EXIT_SUCCESS);
}
// 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);
}
// Build IPv6 ICMP pseudo-header and call checksum function (Section 8.1 of RFC 2460).
uint16_t
icmp6_checksum (struct ip6_hdr iphdr, struct icmp6_hdr icmp6hdr, uint8_t *payload, int payloadlen)
{
char buf[IP_MAXPACKET];
char *ptr;
int chksumlen = 0;
int i;
ptr = &buf[0]; // ptr points to beginning of buffer buf
// Copy source IP address into buf (128 bits)
memcpy (ptr, &iphdr.ip6_src.s6_addr, sizeof (iphdr.ip6_src.s6_addr));
ptr += sizeof (iphdr.ip6_src);
chksumlen += sizeof (iphdr.ip6_src);
// Copy destination IP address into buf (128 bits)
memcpy (ptr, &iphdr.ip6_dst.s6_addr, sizeof (iphdr.ip6_dst.s6_addr));
ptr += sizeof (iphdr.ip6_dst.s6_addr);
chksumlen += sizeof (iphdr.ip6_dst.s6_addr);
// Copy Upper Layer Packet length into buf (32 bits).
// Should not be greater than 65535 (i.e., 2 bytes).
*ptr = 0; ptr++;
*ptr = 0; ptr++;
*ptr = (ICMP_HDRLEN + payloadlen) / 256;
ptr++;
*ptr = (ICMP_HDRLEN + payloadlen) % 256;
ptr++;
chksumlen += 4;
// Copy zero field to buf (24 bits)
*ptr = 0; ptr++;
*ptr = 0; ptr++;
*ptr = 0; ptr++;
chksumlen += 3;
// Copy next header field to buf (8 bits)
memcpy (ptr, &iphdr.ip6_nxt, sizeof (iphdr.ip6_nxt));
ptr += sizeof (iphdr.ip6_nxt);
chksumlen += sizeof (iphdr.ip6_nxt);
// Copy ICMPv6 type to buf (8 bits)
memcpy (ptr, &icmp6hdr.icmp6_type, sizeof (icmp6hdr.icmp6_type));
ptr += sizeof (icmp6hdr.icmp6_type);
chksumlen += sizeof (icmp6hdr.icmp6_type);
// Copy ICMPv6 code to buf (8 bits)
memcpy (ptr, &icmp6hdr.icmp6_code, sizeof (icmp6hdr.icmp6_code));
ptr += sizeof (icmp6hdr.icmp6_code);
chksumlen += sizeof (icmp6hdr.icmp6_code);
// Copy ICMPv6 ID to buf (16 bits)
memcpy (ptr, &icmp6hdr.icmp6_id, sizeof (icmp6hdr.icmp6_id));
ptr += sizeof (icmp6hdr.icmp6_id);
chksumlen += sizeof (icmp6hdr.icmp6_id);
// Copy ICMPv6 sequence number to buff (16 bits)
memcpy (ptr, &icmp6hdr.icmp6_seq, sizeof (icmp6hdr.icmp6_seq));
ptr += sizeof (icmp6hdr.icmp6_seq);
chksumlen += sizeof (icmp6hdr.icmp6_seq);
// Copy ICMPv6 checksum to buf (16 bits)
// Zero, since we don't know it yet.
*ptr = 0; ptr++;
*ptr = 0; ptr++;
chksumlen += 2;
// Copy ICMPv6 payload to buf
memcpy (ptr, payload, payloadlen * sizeof (uint8_t));
ptr += payloadlen;
chksumlen += payloadlen;
// Pad to the next 16-bit boundary
for (i=0; i<payloadlen%2; i++, ptr++) {
*ptr = 0;
ptr += 1;
chksumlen += 1;
}
return checksum ((uint16_t *) buf, chksumlen);
}
// 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);
}
}
// Allocate memory for an array of ints.
int *
allocate_intmem (int len)
{
void *tmp;
if (len <= 0) {
fprintf (stderr, "ERROR: Cannot allocate memory because len = %i in allocate_intmem().\n", len);
exit (EXIT_FAILURE);
}
tmp = (int *) malloc (len * sizeof (int));
if (tmp != NULL) {
memset (tmp, 0, len * sizeof (int));
return (tmp);
} else {
fprintf (stderr, "ERROR: Cannot allocate memory for array allocate_intmem().\n");
exit (EXIT_FAILURE);
}
}