-
Notifications
You must be signed in to change notification settings - Fork 1
/
lig.c
462 lines (391 loc) · 12 KB
/
lig.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
/*
* lig.c --
*
* lig -- LISP Internet Groper
*
* Copyright (c) 2010, David Meyer <[email protected]>
* All rights reserved.
*
* David Meyer
* Thu Apr 9 09:44:57 2009
*
* IPv6 support added by Lorand Jakab <[email protected]>
* Mon Aug 23 15:26:51 2010 +0200
*
* Machine parsable output added by Job Snijders <[email protected]>
* Wed Dec 15 11:38:42 CET 2010
*
*Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* $Header: /mnt/disk1/dmm/src/lig/RCS/lig.c,v 1.1 2010/11/14 20:46:22 dmm Exp $
*
*/
#include "lig.h"
#include "lig-external.h"
/*
* globals
*/
int s; /* send socket */
int r; /* receive socket */
unsigned int *nonce;
uchar packet[MAX_IP_PACKET];
/*
* use these to construct and parse packets
*/
struct ip *iph;
struct udphdr *udph;
struct map_reply_pkt *map_reply;
/*
* global options
*/
unsigned int udp_checksum_disabled = 0;
unsigned int disallow_eid = 0;
unsigned int debug = 0;
unsigned int machinereadable = 0;
int main(int argc, char *argv[])
{
struct addrinfo hints;
struct addrinfo *res;
struct timeval before;
struct timeval after;
struct protoent *proto;
struct sockaddr_storage from;
struct sockaddr_storage my_addr;
/*
* Remember the requested eid and map resolver properties here
*/
char *eid = NULL;
char *src_ip_addr = NULL;
char *eid_name = NULL;
char *mr_name = NULL;
char *progname = NULL;
char *map_resolver = getenv(LISP_MAP_RESOLVER); /* check for env var */
int eid_addrtype = 0;
int eid_length = 0;
int mr_addrtype = 0;
int mr_length = 0;
struct sockaddr_storage eid_addr;
struct sockaddr_storage map_resolver_addr;
int i = 0; /* generic counter */
unsigned int iseed = 0; /* initial random number generator */
unsigned int nonce0 = 0;
unsigned int nonce1 = 0;
/*
* Get defaults
*/
int count = COUNT;
int timeout = MAP_REPLY_TIMEOUT;
unsigned int port = 0; /* if -p <port> specified, put it in here to find overflow */
emr_inner_src_port = 0;
char emr_inner_src_port_str[NI_MAXSERV];
/*
* Temporary data
*/
int e = 0; /* generic errno holder */
char buf[NI_MAXHOST]; /* buffer for getnameinfo() results */
/*
* parse args
*/
int opt = 0;
char *optstring = "bc:dem:p:t:s:uv";
while ((opt = getopt (argc, argv, optstring)) != -1) {
switch (opt) {
case 'b':
machinereadable += 1;
break;
case 'c':
count = atoi(optarg);
if ((count < MIN_COUNT) || (count > MAX_COUNT)) {
fprintf(stderr,
"%s: Invalid number, specify count in the range (%u:%u)\n",
argv[0], MIN_COUNT,MAX_COUNT);
exit(BAD);
}
break;
case 'd':
debug += 1;
break;
case 'e':
disallow_eid = 1;
break;
case 'p':
if ((port = atoi(optarg)) > MAX_EPHEMERAL_PORT) {
fprintf(stderr, "%s: Invalid port (%d)\n", argv[0], port);
exit(BAD);
}
emr_inner_src_port = (ushort) port;
break;
case 'm':
if ((map_resolver = strdup(optarg)) == NULL) {
perror ("strdup(map_resolver)");
exit(BAD);
}
break;
case 's':
if ((src_ip_addr = strdup(optarg)) == NULL) {
perror ("strdup(src_ip_addr)");
exit(BAD);
}
break;
case 't':
timeout = atoi(optarg);
if ((timeout < MIN_MR_TIMEOUT) || (timeout > MAX_MR_TIMEOUT)) {
fprintf(stderr,
"%s: Invalid number, specify timeout in the range (%u:%u) seconds\n",
argv[0], MIN_MR_TIMEOUT,MAX_MR_TIMEOUT);
exit(BAD);
}
break;
case 'u':
udp_checksum_disabled = 1;
break;
case 'v':
fprintf(stderr, VERSION, argv[0]);
exit (GOOD);
default:
fprintf(stderr, USAGE, argv[0]);
exit (BAD);
}
}
/*
* save the program name somewhere
*/
if ((progname = strdup(argv[0])) == NULL) {
perror ("strdup");
exit(BAD);
}
argc -= optind;
argv += optind;
if (argc != 1) {
fprintf(stderr, USAGE, progname);
exit (BAD);
}
/*
* The requested eid should be in argv[0]
*/
if ((eid = strdup(argv[0])) == NULL) {
perror ("strdup(argv[0])");
exit(BAD);
}
if ((eid_name = strdup(eid)) == NULL) {
perror ("strdup(eid)");
exit(BAD);
}
if (map_resolver == NULL) {
fprintf(stderr,
"%s not set and -m not specified\n",
LISP_MAP_RESOLVER);
fprintf(stderr, USAGE, progname);
exit(BAD);
}
if ((mr_name = strdup(map_resolver)) == NULL) {
perror ("strdup(map_resolver)");
exit(BAD);
}
/*
* We'll use getaddrinfo() for name lookups.
* Explicitly set options by setting up a non-NULL hints
*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_ADDRCONFIG; /* Only return families configured on host */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
if ((e = getaddrinfo(eid_name, NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
exit(BAD);
}
/*
* Save the eid, eid_addr, eid_addrtype
*
*/
if (res != NULL) {
eid_addrtype = res->ai_family;
eid_length = res->ai_addrlen;
if ((e = getnameinfo(res->ai_addr,res->ai_addrlen,
buf,NI_MAXHOST,NULL,0,NI_NUMERICHOST)) != 0) {
fprintf(stderr,"getnameinfo: %s\n",gai_strerror(e));
exit(BAD);
}
eid = strdup(buf);
memcpy(&eid_addr, res->ai_addr, res->ai_addrlen);
}
freeaddrinfo(res);
/*
* likewise for the map resolver
*/
if ((e = getaddrinfo(map_resolver, LISP_CONTROL_PORT_STR, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
exit(BAD);
}
if (res != NULL) {
mr_addrtype = res->ai_family;
mr_length = res->ai_addrlen;
if ((e = getnameinfo(res->ai_addr,res->ai_addrlen,
buf,NI_MAXHOST,NULL,0,NI_NUMERICHOST)) != 0) {
fprintf(stderr,"getnameinfo: %s\n",gai_strerror(e));
exit(BAD);
}
map_resolver = strdup(buf);
memcpy(&map_resolver_addr, res->ai_addr, res->ai_addrlen);
}
freeaddrinfo(res);
/*
* get an array of nonces of size 2*count
* (need 2*count as nonces are 64 bit as of
* draft-ietf-lisp-04.txt)
*/
if ((nonce = (unsigned int *) malloc(2*count*sizeof(unsigned int))) < 0) {
perror ("malloc (nonce)");
exit(BAD);
}
if ((proto = getprotobyname("UDP")) == NULL) {
perror ("getprotobyname");
exit(BAD);
}
if ((s = socket(mr_addrtype,SOCK_DGRAM,proto->p_proto)) < 0) {
perror("SOCK_DGRAM (s)");
exit(1);
}
if ((r = socket(mr_addrtype,SOCK_DGRAM,proto->p_proto)) < 0) {
perror("SOCK_DGRAM (r)");
exit(1);
}
if (src_ip_addr) {
if ((e = getaddrinfo(src_ip_addr, NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
exit(BAD);
}
memcpy(&my_addr, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
} else
if (get_my_ip_addr(mr_addrtype,&my_addr)) {
fprintf(stderr, "No usable %s source address\n",
(mr_addrtype == AF_INET) ? "IPv4" : "IPv6");
exit(BAD);
}
if (debug) {
if ((e = getnameinfo((struct sockaddr *)&my_addr,SA_LEN(my_addr.ss_family),
buf,NI_MAXHOST,NULL,0,NI_NUMERICHOST)) != 0) {
fprintf(stderr,"getnameinfo: %s\n",gai_strerror(e));
exit(BAD);
}
printf("Using source address (ITR-RLOC) %s\n", buf);
}
/*
* Initialize the random number generator for the nonces
*/
iseed = (unsigned int) time (NULL);
srandom(iseed);
/*
* http://tools.ietf.org/html/draft-larsen-tsvwg-port-randomization-02.txt
*/
if (!emr_inner_src_port)
emr_inner_src_port = MIN_EPHEMERAL_PORT +
random() % (MAX_EPHEMERAL_PORT - MIN_EPHEMERAL_PORT);
memset(packet, 0, MAX_IP_PACKET);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = mr_addrtype; /* Bind on AF based on AF of Map-Resolver */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = proto->p_proto;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
sprintf(emr_inner_src_port_str, "%d", emr_inner_src_port);
if ((e = getaddrinfo(NULL, emr_inner_src_port_str, &hints, &res)) != 0) {
fprintf(stderr, "getting local socket: getaddrinfo: %s\n", gai_strerror(e));
exit(BAD);
}
if (bind(r, res->ai_addr, res->ai_addrlen) == -1) {
perror("bind");
exit(BAD);
}
freeaddrinfo(res);
for (i = 0; i < count; i++) {
build_nonce(nonce,i,&nonce0,&nonce1);
if (debug) {
if ((e = getnameinfo((struct sockaddr *)(&map_resolver_addr), mr_length,
buf,NI_MAXHOST,NULL,0,NI_NUMERICHOST)) != 0) {
fprintf(stderr,"getnameinfo: %s\n",gai_strerror(e));
exit(BAD);
}
printf("Send map-request to %s (%s) for %s (%s) ...\n",
mr_name,
buf,
eid_name,
eid);
} else if (machinereadable && i == 0) {
printf("MAPRESOLVER=%s\nEID=%s\n", mr_name, eid_name);
}
else if (!machinereadable)
printf("Send map-request to %s for %s ...\n", mr_name, eid_name);
if (send_map_request(s,
nonce0,
nonce1,
&before,
(struct sockaddr *)&eid_addr,
(struct sockaddr *)&map_resolver_addr,
(struct sockaddr *)&my_addr)) {
fprintf(stderr, "send_map_request: can't send map-request\n");
exit(BAD);
}
if (wait_for_response(r,timeout)) {
if (gettimeofday(&after,NULL) == -1) {
perror("gettimeofday");
return(BAD);
}
if (!get_map_reply(r, packet, mr_addrtype, &from))
continue; /* try again if count left */
map_reply = (struct map_reply_pkt *) packet;
if ((e = getnameinfo((struct sockaddr *)&from,SA_LEN(from.ss_family),
buf,NI_MAXHOST,NULL,0,NI_NUMERICHOST)) != 0) {
fprintf(stderr,"getnameinfo: %s\n",gai_strerror(e));
exit(BAD);
}
if (find_nonce(map_reply,nonce, (i+1))) {
print_map_reply(map_reply,
eid,
map_resolver,
strdup(buf),
tvdiff(&after,&before),
machinereadable);
exit(GOOD);
} else { /* Otherwise assume its spoofed */
printf("Spoofed map-reply: 0x%08x/0x%08x-0x%08x/0x%08x\n",
nonce0,
ntohl(map_reply->lisp_nonce0),
nonce1,
ntohl(map_reply->lisp_nonce1));
continue; /* try again if count left */
}
} /* timed out */
}
printf("*** No map-reply received ***\n");
exit(GOOD);
}