forked from philpraxis/lig
-
Notifications
You must be signed in to change notification settings - Fork 6
/
send_map_request.c
435 lines (386 loc) · 14.6 KB
/
send_map_request.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
/*
* send_map_request.c
*
* By David Meyer <[email protected]>
* Copyright 2009 David Meyer
*
* Functions related to sending a map-request
*
* David Meyer
* Tue Apr 14 14:48:13 2009
*
* IPv6 support added by Lorand Jakab <[email protected]>
* Mon Aug 23 15:26:51 2010 +0200
*
* Instance ID support added by Lorand Jakab <[email protected]>
* Thu Jul 26 00:50:51 PDT 2012
*
* Non-encapsulated Map-Request Functionality added by Alexandru Iuhas <[email protected]>
* Mon May 27 17:00:20 2013, Polytechnic University of Catalonia
*
* 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/send_map_request.c,v 1.1 2010/11/14 20:48:35 dmm Exp $
*
*/
#include "lig.h"
#include "lig-external.h"
#include "send_map_request.h"
/*
* send_map_request --
*
* Sends a IP/UDP encapsulated map-request for eid to map_server
*
*
* Here's the packet we need to build:
*
* IP header (ip.src = <us>, ip.dst = <map-resolver>)
* UDP header (udp.srcport = <kernel>, udp.dstport = 4342)
* lcp -> lisp_control_pkt
* packet,iph -> IP header (ip.src = <this host>, ip.dst = eid)
* udph -> UDP (udp.srcport = ANY, udp.dstport = 4342)
* map_request -> struct map-request
*
* We'll open a UDP socket on dest port 4342, and
* give it a "packet" that that looks like:
*
* lcp -> lisp_control_pkt
* packet,iph -> IP header (SRC = this host, DEST = eid)
* udph -> UDP (DEST PORT = 4342)
* map_request -> struct map-request
*
*
* David Meyer
* Thu Apr 16 14:46:51 2009
*
* $Header: /mnt/disk1/dmm/src/lig/RCS/send_map_request.c,v 1.1 2010/11/14 20:48:35 dmm Exp $
*
*/
size_t addr_size(struct sockaddr *addr)
{
size_t len = sizeof(uint16_t);
if (addr == NULL){
return (len);
}
if (addr->sa_family == AF_INET){
return (sizeof(struct in_addr)+len);
}else{
return (sizeof(struct in6_addr)+len);
}
}
size_t addr_write(struct sockaddr *addr, uint8_t *ptr)
{
uint16_t *afi;
size_t len;
afi = (uint16_t *)ptr;
len = sizeof(uint16_t);
if (addr){
if (addr->sa_family == AF_INET){
*afi = htons(LISP_AFI_IP);
ptr = CO(afi,sizeof(uint16_t));
memcpy(ptr,&(((struct sockaddr_in *)addr)->sin_addr), sizeof(struct in_addr));
len += sizeof(struct in_addr);
}else{
*afi = htons(LISP_AFI_IPV6);
ptr = CO(afi,sizeof(uint16_t));
memcpy(ptr,&(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
len += sizeof(struct in6_addr);
}
}else{
*afi = 0;
}
return (len);
}
int send_map_request(
int send_socket,
unsigned int nonce0,
unsigned int nonce1,
struct timeval *before,
struct sockaddr *eid_addr,
struct sockaddr *map_resolver_addr,
struct sockaddr *my_addr,
struct sockaddr *src_eid,
int encapsulate)
{
unsigned int ip_len = 0;
unsigned int udp_len = 0;
unsigned int packet_len = 0;
unsigned int map_req_len = 0;
int nbytes = 0;
int e = 0;
char buf1[NI_MAXHOST];
char buf2[NI_MAXHOST];
uchar packet[MAX_IP_PACKET];
uchar *ptr;
struct lisp_control_pkt *lcp = NULL;
struct ip *iph = NULL;
struct ip6_hdr *ip6h = NULL;
struct udphdr *udph = NULL;
struct map_request_pkt *map_request;
struct map_request_rec *rec;
size_t len;
/*
* The source address in the inner IP header
*
* Its address family depends on the destination EID, regardless of the
* Map-Resolver. If the host has no usable IPv6 address, the "::" address
* will be used.
*
*/
struct sockaddr_storage inner_src;
if (get_my_ip_addr(eid_addr->sa_family,&inner_src)) {
struct addrinfo hints;
struct addrinfo *res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = eid_addr->sa_family;
hints.ai_socktype = 0;
hints.ai_flags = AI_NUMERICHOST;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
/* Update IPv6 with the v4 mapped address instead of :: */
if ((e = getaddrinfo(
(eid_addr->sa_family == AF_INET) ? "0.0.0.0" : "::",
NULL, &hints, &res)) != 0) {
fprintf(stderr, "getting local socket: getaddrinfo: %s\n", gai_strerror(e));
exit(BAD);
}
memcpy(&inner_src, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
}
if (debug > 2) {
getnameinfo((struct sockaddr *)&inner_src,
SA_LEN(((struct sockaddr *)&inner_src)->sa_family),
buf1,NI_MAXHOST,NULL,0,NI_NUMERICHOST);
getnameinfo(eid_addr,SA_LEN(eid_addr->sa_family),buf2,NI_MAXHOST,NULL,0,NI_NUMERICHOST);
fprintf(stderr, "send_map_request (inner header): <%s:%d,%s:%d>\n",
buf1,
emr_inner_src_port,
buf2,
LISP_CONTROL_PORT);
}
/*
* make sure packet is clean
*/
memset(packet, 0, MAX_IP_PACKET);
/*
* Build the packet.
*
* The packet has the following form:
*
* outer-ip-header built by the kernel
* udp-header (4342) built by the kernel
* lisp lisp control packet struct lisp_control_pkt *lcp
* inner-ip-header struct ip *iphdr
* udp-header (4342) struct udphdr *udphdr
* lisp-header (map-request) struct map_request_pkt *map_request
*/
/*
* CO is a macro that makes sure the pointer
* arithmetic is done correctly. Basically...
*
* #define CO(addr,len) (((char *) addr + len))
*
*/
/*
* compute lengths of interest
*/
map_req_len = sizeof(struct map_request_pkt) + addr_size(src_eid) + addr_size(my_addr) + sizeof (struct map_request_rec) + addr_size(eid_addr);
if (encapsulate){
udp_len = sizeof(struct udphdr) + map_req_len;
}else{
udp_len = map_req_len;
}
if (eid_addr->sa_family == AF_INET){
ip_len = udp_len + sizeof(struct ip);
}else{
ip_len = udp_len + sizeof(struct ip6_hdr);
}
if (encapsulate){
packet_len = ip_len + sizeof(struct lisp_control_pkt);
}else {
packet_len = udp_len;
}
if (encapsulate == 1) {
lcp = (struct lisp_control_pkt *) packet;
if (eid_addr->sa_family == AF_INET) {
iph = (struct ip *) CO(lcp, sizeof(struct lisp_control_pkt));
ip6h = NULL;
udph = (struct udphdr *) CO(iph, sizeof(struct ip));
} else {
iph = NULL;
ip6h = (struct ip6_hdr *) CO(lcp, sizeof(struct lisp_control_pkt));
udph = (struct udphdr *) CO(ip6h, sizeof(struct ip6_hdr));
}
lcp->type = LISP_ENCAP_CONTROL_TYPE;
/*
* Build inner IP header
*
* packet,iph -> IP header (SRC = this host, DEST = EID)
*
*/
if (eid_addr->sa_family == AF_INET) {
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = htons(ip_len); /* ip + udp headers, + map_request */
iph->ip_id = htons(54321); /* the value doesn't matter here */
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = IPPROTO_UDP;
iph->ip_sum = 0; /* compute checksum later */
iph->ip_src.s_addr = ((struct sockaddr_in *)&inner_src)->sin_addr.s_addr;
iph->ip_dst.s_addr = ((struct sockaddr_in *)eid_addr)->sin_addr.s_addr;
} else {
ip6h->ip6_vfc = 0x6E;
ip6h->ip6_plen = htons(udp_len); /* udp header + map_request */
ip6h->ip6_nxt = IPPROTO_UDP;
ip6h->ip6_hlim = 64;
ip6h->ip6_src = ((struct sockaddr_in6 *)&inner_src)->sin6_addr;
ip6h->ip6_dst = ((struct sockaddr_in6 *)eid_addr)->sin6_addr;
}
/*
* Build UDP inner header
*
* DEST Port is 4342 (LISP Control)
*/
#ifdef BSD
udph->uh_sport = htons(emr_inner_src_port);
udph->uh_dport = htons(LISP_CONTROL_PORT);
udph->uh_ulen = htons(udp_len);
udph->uh_sum = 0;
#else
udph->source = htons(emr_inner_src_port);
udph->dest = htons(LISP_CONTROL_PORT);
udph->len = htons(udp_len);
udph->check = 0;
#endif
map_request = (struct map_request_pkt *) CO(udph, sizeof(struct udphdr));
}else{
map_request = (struct map_request_pkt *) packet;
}
/*
* Build the Map-Request
*
* Map-Request Message Format
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Type=1 |A|M|P|S| Reserved | IRC | Record Count |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Nonce . . . |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | . . . Nonce |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source-EID-AFI | Source EID Address ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ITR-RLOC-AFI 1 | ITR-RLOC Address 1 ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ITR-RLOC-AFI n | ITR-RLOC Address n ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* / | Reserved | EID mask-len | EID-prefix-AFI |
* Rec +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* \ | EID-prefix ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Map-Reply Record ... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Mapping Protocol Data |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
map_request->smr_invoked = smri;
map_request->proxy_itr = 0;
map_request->smr_bit = smr;
map_request->rloc_probe = probe;
map_request->map_data_present = 0;
map_request->auth_bit = mrauth;
map_request->lisp_type = LISP_MAP_REQUEST;
map_request->irc = 0;
map_request->record_count = 1;
map_request->lisp_nonce0 = htonl(nonce0);
map_request->lisp_nonce1 = htonl(nonce1);
ptr = CO(map_request, sizeof(struct map_request_pkt));
len = addr_write(src_eid, ptr);
ptr = CO(ptr, len);
len = addr_write(my_addr, ptr);
rec = (struct map_request_rec *)CO(ptr, len);
if (eid_addr->sa_family == AF_INET) {
rec->eid_mask_len = LISP_IP_MASK_LEN;
}else{
rec->eid_mask_len = LISP_IPV6_MASK_LEN;
}
ptr = CO(rec,sizeof(struct map_request_rec));
len = addr_write(eid_addr, ptr);
if (encapsulate == 1){
if (udp_checksum_disabled)
udpsum(udph) = 0;
else {
if (eid_addr->sa_family == AF_INET)
udpsum(udph) = udp_checksum(udph,udp_len,iph->ip_src.s_addr,iph->ip_dst.s_addr);
else
udpsum(udph) = udp6_checksum(ip6h,udph,udp_len);
}
}
/*
* Set up to talk to the map-resolver
*
* Kernel puts on:
*
* IP (SRC = my_addr, DEST = map_resolver)
* UDP (DEST PORT = 4342)
*
* The UDP packet we build (packet) looks like:
*
* IP (SRC = my_addr, DEST = eid)
* UDP (DEST PORT = 4342)
* map-request_pkt
*
*/
if (gettimeofday(before,NULL) == -1) {
perror("gettimeofday");
return(BAD);
}
if ((nbytes = sendto(send_socket,
(const void *) packet,
packet_len,
0,
map_resolver_addr,
SA_LEN(map_resolver_addr->sa_family))) < 0) {
perror("sendto");
exit(BAD);
}
if (nbytes != packet_len) {
fprintf(stderr,
"send_map_request: nbytes (%d) != packet_len(%d)\n",
nbytes, packet_len);
exit(BAD);
}
return(GOOD);
}