-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathreceive_ra6.c
249 lines (211 loc) · 8.08 KB
/
receive_ra6.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
/* 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/>.
*/
// Receive an IPv6 router advertisement and extract
// various information stored in the ethernet frame.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // close()
#include <string.h> // strcpy, memset()
#include <netinet/icmp6.h> // struct nd_router_advert, which contains icmp6_hdr, ND_ROUTER_ADVERT
#include <netinet/in.h> // IPPROTO_IPV6, IPPROTO_ICMPV6, INET6_ADDRSTRLEN
#include <netinet/ip.h> // IP_MAXPACKET (65535)
#include <arpa/inet.h> // 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 <bits/socket.h> // structs msghdr and cmsghdr
#include <net/if.h> // struct ifreq
#include <errno.h> // errno, perror()
// Taken from <linux/ipv6.h>, also in <netinet/in.h>
struct in6_pktinfo {
struct in6_addr ipi6_addr;
int ipi6_ifindex;
};
// Function prototypes
static void *find_ancillary (struct msghdr *, int);
char *allocate_strmem (int);
uint8_t *allocate_ustrmem (int);
int
main (int argc, char **argv)
{
int i, status, sd, on, ifindex, hoplimit;
struct nd_router_advert *ra;
uint8_t *inpack;
int len;
struct msghdr msghdr;
struct iovec iov[2];
uint8_t *opt, *pkt;
char *interface, *destination;
struct in6_addr dst;
int rcv_ifindex;
struct ifreq ifr;
// Allocate memory for various arrays.
inpack = allocate_ustrmem (IP_MAXPACKET);
interface = allocate_strmem (40);
destination = allocate_strmem (INET6_ADDRSTRLEN);
// Interface to receive packet on.
strcpy (interface, "eth0");
// Prepare msghdr for recvmsg().
memset (&msghdr, 0, sizeof (msghdr));
msghdr.msg_name = NULL;
msghdr.msg_namelen = 0;
memset (&iov, 0, sizeof (iov));
iov[0].iov_base = (uint8_t *) inpack;
iov[0].iov_len = IP_MAXPACKET;
msghdr.msg_iov = iov;
msghdr.msg_iovlen = 1;
msghdr.msg_control = allocate_ustrmem (IP_MAXPACKET);
msghdr.msg_controllen = IP_MAXPACKET * sizeof (uint8_t);
// Request a socket descriptor sd.
if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
perror ("Failed to get socket descriptor ");
exit (EXIT_FAILURE);
}
// Set flag so we receive hop limit from recvmsg.
on = 1;
if ((status = setsockopt (sd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof (on))) < 0) {
perror ("setsockopt to IPV6_RECVHOPLIMIT failed ");
exit (EXIT_FAILURE);
}
// Set flag so we receive destination address from recvmsg.
on = 1;
if ((status = setsockopt (sd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on))) < 0) {
perror ("setsockopt to IPV6_RECVPKTINFO failed ");
exit (EXIT_FAILURE);
}
// Obtain MAC address of this node.
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);
}
// Retrieve interface index of this node.
if ((ifindex = if_nametoindex (interface)) == 0) {
perror ("if_nametoindex() failed to obtain interface index ");
exit (EXIT_FAILURE);
}
printf ("\nOn this node, index for interface %s is %i\n", interface, ifindex);
// Bind socket to interface of this node.
if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, (void *) &ifr, sizeof (ifr)) < 0) {
perror ("SO_BINDTODEVICE failed");
exit (EXIT_FAILURE);
}
// Listen for incoming message from socket sd.
// Keep at it until we get a router advertisement.
ra = (struct nd_router_advert *) inpack;
while (ra->nd_ra_hdr.icmp6_type != ND_ROUTER_ADVERT) {
if ((len = recvmsg (sd, &msghdr, 0)) < 0) {
perror ("recvmsg failed ");
return (EXIT_FAILURE);
}
}
// Ancillary data
printf ("\nIPv6 header data:\n");
opt = find_ancillary (&msghdr, IPV6_HOPLIMIT);
if (opt == NULL) {
fprintf (stderr, "Unknown hop limit\n");
exit (EXIT_FAILURE);
}
hoplimit = *(int *) opt;
printf ("Hop limit: %i\n", hoplimit);
opt = find_ancillary (&msghdr, IPV6_PKTINFO);
if (opt == NULL) {
fprintf (stderr, "Unkown destination address\n");
exit (EXIT_FAILURE);
}
dst = ((struct in6_pktinfo *) opt)->ipi6_addr;
if (inet_ntop (AF_INET6, &dst, destination, INET6_ADDRSTRLEN) == NULL) {
status = errno;
fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status));
exit (EXIT_FAILURE);
}
printf ("Destination address: %s\n", destination);
rcv_ifindex = ((struct in6_pktinfo *) opt)->ipi6_ifindex;
printf ("Destination interface index: %i\n", rcv_ifindex);
// ICMPv6 header and options data
printf ("\nICMPv6 header data:\n");
printf ("Type (134 = router advertisement): %u\n", ra->nd_ra_hdr.icmp6_type);
printf ("Code: %u\n", ra->nd_ra_hdr.icmp6_code);
printf ("Checksum: %x\n", ntohs (ra->nd_ra_hdr.icmp6_cksum));
printf ("Hop limit recommended by this router (0 is no recommendation): %u\n", ra->nd_ra_curhoplimit);
printf ("Managed address configuration flag: %u\n", ra->nd_ra_flags_reserved >> 7);
printf ("Other stateful configuration flag: %u\n", (ra->nd_ra_flags_reserved >> 6) & 1);
printf ("Mobile home agent flag: %u\n", (ra->nd_ra_flags_reserved >> 5) & 1);
printf ("Router lifetime as default router (s): %u\n", ntohs (ra->nd_ra_router_lifetime));
printf ("Reachable time (ms): %u\n", ntohl (ra->nd_ra_reachable));
printf ("Retransmission time (ms): %u\n", ntohl (ra->nd_ra_retransmit));
printf ("\nOptions:\n"); // Contents here are consistent with ra6.c, but others are possible
pkt = (uint8_t *) inpack;
printf ("Type: %u\n", pkt[sizeof (struct nd_router_advert)]);
printf ("Length: %u (units of 8 octets)\n", pkt[sizeof (struct nd_router_advert) + 1]);
printf ("MAC address: ");
for (i=2; i<7; i++) {
printf ("%02x:", pkt[sizeof (struct nd_router_advert) + i]);
}
printf ("%02x\n", pkt[sizeof (struct nd_router_advert) + 7]);
close (sd);
// Free allocated memory.
free (inpack);
free (interface);
free (destination);
free (msghdr.msg_control);
return (EXIT_SUCCESS);
}
static void *
find_ancillary (struct msghdr *msg, int cmsg_type)
{
struct cmsghdr *cmsg = NULL;
for (cmsg = CMSG_FIRSTHDR (msg); cmsg != NULL; cmsg = CMSG_NXTHDR (msg, cmsg)) {
if ((cmsg->cmsg_level == IPPROTO_IPV6) && (cmsg->cmsg_type == cmsg_type)) {
return (CMSG_DATA (cmsg));
}
}
return (NULL);
}
// 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);
}
}