]>
Commit | Line | Data |
---|---|---|
52b7d2ce A |
1 | /* |
2 | * Copyright (C) 2004 SuSE Linux AG, Nuernberg, Germany. | |
3 | * Contributed by: Michal Ludvig <mludvig@suse.cz>, SUSE Labs | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. Neither the name of the project nor the names of its contributors | |
15 | * may be used to endorse or promote products derived from this software | |
16 | * without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 | * SUCH DAMAGE. | |
29 | */ | |
30 | ||
31 | #include "config.h" | |
32 | ||
33 | #include <sys/types.h> | |
34 | #include <sys/param.h> | |
35 | ||
36 | #ifdef __linux__ | |
37 | #include <linux/udp.h> | |
38 | #endif | |
39 | #if defined(__NetBSD__) || defined (__FreeBSD__) | |
40 | #include <netinet/udp.h> | |
41 | #endif | |
42 | ||
43 | #include <stdlib.h> | |
44 | #include <stdio.h> | |
45 | #include <string.h> | |
46 | #include <errno.h> | |
47 | #include <ctype.h> | |
48 | ||
49 | #include "var.h" | |
50 | #include "misc.h" | |
51 | #include "vmbuf.h" | |
52 | #include "plog.h" | |
53 | #include "debug.h" | |
54 | ||
55 | #include "localconf.h" | |
56 | #include "remoteconf.h" | |
57 | #include "sockmisc.h" | |
58 | #include "isakmp_var.h" | |
59 | #include "isakmp.h" | |
60 | #include "oakley.h" | |
61 | #include "ipsec_doi.h" | |
62 | #include "vendorid.h" | |
63 | #include "handler.h" | |
64 | #include "crypto_openssl.h" | |
65 | #include "schedule.h" | |
66 | #include "nattraversal.h" | |
67 | #include "grabmyaddr.h" | |
d1e348cf | 68 | #include "ike_session.h" |
52b7d2ce A |
69 | |
70 | struct natt_ka_addrs { | |
71 | struct sockaddr *src; | |
72 | struct sockaddr *dst; | |
73 | unsigned in_use; | |
74 | ||
75 | TAILQ_ENTRY(natt_ka_addrs) chain; | |
76 | }; | |
77 | ||
78 | static TAILQ_HEAD(_natt_ka_addrs, natt_ka_addrs) ka_tree; | |
79 | ||
80 | /* | |
81 | * check if the given vid is NAT-T. | |
82 | */ | |
83 | int | |
84 | natt_vendorid (int vid) | |
85 | { | |
86 | return ( | |
87 | #ifdef ENABLE_NATT_00 | |
88 | vid == VENDORID_NATT_00 || | |
89 | #endif | |
90 | #ifdef ENABLE_NATT_01 | |
91 | vid == VENDORID_NATT_01 || | |
92 | #endif | |
93 | #ifdef ENABLE_NATT_02 | |
94 | vid == VENDORID_NATT_02 || | |
95 | vid == VENDORID_NATT_02_N || | |
96 | #endif | |
97 | #ifdef ENABLE_NATT_03 | |
98 | vid == VENDORID_NATT_03 || | |
99 | #endif | |
100 | #ifdef ENABLE_NATT_04 | |
101 | vid == VENDORID_NATT_04 || | |
102 | #endif | |
103 | #ifdef ENABLE_NATT_05 | |
104 | vid == VENDORID_NATT_05 || | |
105 | #endif | |
106 | #ifdef ENABLE_NATT_06 | |
107 | vid == VENDORID_NATT_06 || | |
108 | #endif | |
109 | #ifdef ENABLE_NATT_07 | |
110 | vid == VENDORID_NATT_07 || | |
111 | #endif | |
112 | #ifdef ENABLE_NATT_08 | |
113 | vid == VENDORID_NATT_08 || | |
114 | #endif | |
115 | #ifdef ENABLE_NATT_APPLE | |
116 | vid == VENDORID_NATT_APPLE || | |
117 | #endif | |
118 | /* Always enable NATT RFC if ENABLE_NATT | |
119 | */ | |
120 | vid == VENDORID_NATT_RFC); | |
121 | } | |
122 | ||
123 | vchar_t * | |
124 | natt_hash_addr (struct ph1handle *iph1, struct sockaddr *addr) | |
125 | { | |
126 | vchar_t *natd; | |
127 | vchar_t *buf; | |
128 | char *ptr; | |
129 | void *addr_ptr, *addr_port; | |
130 | size_t buf_size, addr_size; | |
131 | ||
132 | plog (LLV_INFO, LOCATION, addr, "Hashing %s with algo #%d %s\n", | |
133 | saddr2str(addr), iph1->approval->hashtype, | |
134 | (iph1->rmconf->nat_traversal == NATT_FORCE)?"(NAT-T forced)":""); | |
135 | ||
136 | if (addr->sa_family == AF_INET) { | |
137 | addr_size = sizeof (struct in_addr); /* IPv4 address */ | |
138 | addr_ptr = &((struct sockaddr_in *)addr)->sin_addr; | |
139 | addr_port = &((struct sockaddr_in *)addr)->sin_port; | |
140 | } | |
141 | else if (addr->sa_family == AF_INET6) { | |
142 | addr_size = sizeof (struct in6_addr); /* IPv6 address */ | |
143 | addr_ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; | |
144 | addr_port = &((struct sockaddr_in6 *)addr)->sin6_port; | |
145 | } | |
146 | else { | |
147 | plog (LLV_ERROR, LOCATION, addr, "Unsupported address family #0x%x\n", addr->sa_family); | |
148 | return NULL; | |
149 | } | |
150 | ||
151 | buf_size = 2 * sizeof (cookie_t); /* CKY-I + CKY+R */ | |
152 | buf_size += addr_size + 2; /* Address + Port */ | |
153 | ||
154 | if ((buf = vmalloc (buf_size)) == NULL) | |
155 | return NULL; | |
156 | ||
157 | ptr = buf->v; | |
158 | ||
159 | /* Copy-in CKY-I */ | |
160 | memcpy (ptr, iph1->index.i_ck, sizeof (cookie_t)); | |
161 | ptr += sizeof (cookie_t); | |
162 | ||
163 | /* Copy-in CKY-I */ | |
164 | memcpy (ptr, iph1->index.r_ck, sizeof (cookie_t)); | |
165 | ptr += sizeof (cookie_t); | |
166 | ||
167 | /* Copy-in Address (or zeroes if NATT_FORCE) */ | |
168 | if (iph1->rmconf->nat_traversal == NATT_FORCE) | |
169 | memset (ptr, 0, addr_size); | |
170 | else | |
171 | memcpy (ptr, addr_ptr, addr_size); | |
172 | ptr += addr_size; | |
173 | ||
174 | /* Copy-in Port number */ | |
175 | memcpy (ptr, addr_port, 2); | |
176 | ||
177 | natd = oakley_hash (buf, iph1); | |
178 | vfree(buf); | |
179 | ||
180 | return natd; | |
181 | } | |
182 | ||
183 | int | |
184 | natt_compare_addr_hash (struct ph1handle *iph1, vchar_t *natd_received, | |
185 | int natd_seq) | |
186 | { | |
187 | vchar_t *natd_computed; | |
188 | u_int32_t flag; | |
189 | int verified = 0; | |
190 | ||
191 | if (iph1->rmconf->nat_traversal == NATT_FORCE) | |
192 | return verified; | |
193 | ||
194 | #ifdef __APPLE__ | |
195 | /* old APPLE version sends natd payload in the wrong order */ | |
196 | if (iph1->natt_options->version == VENDORID_NATT_APPLE) { | |
197 | if (natd_seq == 0) { | |
198 | natd_computed = natt_hash_addr (iph1, iph1->remote); | |
199 | flag = NAT_DETECTED_PEER; | |
200 | } | |
201 | else { | |
202 | natd_computed = natt_hash_addr (iph1, iph1->local); | |
203 | flag = NAT_DETECTED_ME; | |
204 | } | |
205 | } else | |
206 | #endif | |
207 | { | |
208 | if (natd_seq == 0) { | |
209 | natd_computed = natt_hash_addr (iph1, iph1->local); | |
210 | flag = NAT_DETECTED_ME; | |
211 | } | |
212 | else { | |
213 | natd_computed = natt_hash_addr (iph1, iph1->remote); | |
214 | flag = NAT_DETECTED_PEER; | |
215 | } | |
216 | } | |
217 | ||
218 | if (natd_received->l == natd_computed->l && | |
219 | memcmp (natd_received->v, natd_computed->v, natd_received->l) == 0) { | |
220 | iph1->natt_flags &= ~flag; | |
221 | verified = 1; | |
222 | } | |
223 | ||
d1e348cf A |
224 | if (iph1->parent_session) |
225 | iph1->parent_session->natt_flags = iph1->natt_flags; | |
226 | ||
52b7d2ce A |
227 | vfree (natd_computed); |
228 | ||
229 | return verified; | |
230 | } | |
231 | ||
232 | int | |
233 | natt_udp_encap (int encmode) | |
234 | { | |
235 | return (encmode == IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC || | |
236 | encmode == IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC || | |
237 | encmode == IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT || | |
238 | encmode == IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT); | |
239 | } | |
240 | ||
241 | int | |
242 | natt_fill_options (struct ph1natt_options *opts, int version) | |
243 | { | |
244 | if (! opts) | |
245 | return -1; | |
246 | ||
247 | opts->version = version; | |
248 | ||
249 | switch (version) { | |
250 | #ifndef __APPLE__ | |
251 | case VENDORID_NATT_00: | |
252 | case VENDORID_NATT_01: | |
253 | opts->float_port = 0; /* No port floating for those drafts */ | |
254 | opts->payload_nat_d = ISAKMP_NPTYPE_NATD_DRAFT; | |
255 | opts->payload_nat_oa = ISAKMP_NPTYPE_NATOA_DRAFT; | |
256 | opts->mode_udp_tunnel = IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT; | |
257 | opts->mode_udp_transport = IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT; | |
258 | opts->encaps_type = UDP_ENCAP_ESPINUDP_NON_IKE; | |
259 | break; | |
260 | #endif | |
261 | ||
262 | case VENDORID_NATT_02: | |
263 | case VENDORID_NATT_02_N: | |
264 | case VENDORID_NATT_03: | |
265 | opts->float_port = lcconf->port_isakmp_natt; | |
266 | opts->payload_nat_d = ISAKMP_NPTYPE_NATD_DRAFT; | |
267 | opts->payload_nat_oa = ISAKMP_NPTYPE_NATOA_DRAFT; | |
268 | opts->mode_udp_tunnel = IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT; | |
269 | opts->mode_udp_transport = IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT; | |
270 | opts->encaps_type = UDP_ENCAP_ESPINUDP; | |
271 | break; | |
272 | case VENDORID_NATT_04: | |
273 | case VENDORID_NATT_05: | |
274 | case VENDORID_NATT_06: | |
275 | case VENDORID_NATT_07: | |
276 | case VENDORID_NATT_08: | |
277 | #ifdef __APPLE__ | |
278 | case VENDORID_NATT_APPLE: | |
279 | opts->float_port = lcconf->port_isakmp_natt; | |
280 | opts->payload_nat_d = ISAKMP_NPTYPE_NATD_BADDRAFT; | |
281 | opts->payload_nat_oa = ISAKMP_NPTYPE_NONE; | |
282 | opts->mode_udp_tunnel = IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC; | |
283 | opts->mode_udp_transport = IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC; | |
284 | opts->encaps_type = UDP_ENCAP_ESPINUDP; | |
285 | break; | |
286 | #endif | |
287 | case VENDORID_NATT_RFC: | |
288 | opts->float_port = lcconf->port_isakmp_natt; | |
289 | opts->payload_nat_d = ISAKMP_NPTYPE_NATD_RFC; | |
290 | opts->payload_nat_oa = ISAKMP_NPTYPE_NATOA_RFC; | |
291 | opts->mode_udp_tunnel = IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC; | |
292 | opts->mode_udp_transport = IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC; | |
293 | opts->encaps_type = UDP_ENCAP_ESPINUDP; | |
294 | break; | |
295 | default: | |
296 | plog(LLV_ERROR, LOCATION, NULL, | |
297 | "unsupported NAT-T version: %s\n", | |
298 | vid_string_by_id(version)); | |
299 | return -1; | |
300 | } | |
301 | ||
302 | opts->mode_udp_diff = opts->mode_udp_tunnel - IPSECDOI_ATTR_ENC_MODE_TUNNEL; | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
d1e348cf | 307 | int |
52b7d2ce A |
308 | create_natoa_payloads(struct ph2handle *iph2, vchar_t **natoa_i, vchar_t **natoa_r) |
309 | { | |
310 | int natoa_type = 0; | |
52b7d2ce A |
311 | vchar_t *i; |
312 | vchar_t *r; | |
313 | u_int8_t *p; | |
d1e348cf A |
314 | struct sockaddr *i_addr; |
315 | struct sockaddr *r_addr; | |
316 | size_t i_size; | |
317 | size_t r_size; | |
52b7d2ce A |
318 | |
319 | *natoa_i = *natoa_r = NULL; | |
320 | ||
321 | ||
322 | /* create natoa payloads if natt being used */ | |
323 | /* don't send if type == apple */ | |
d1e348cf A |
324 | if (!iph2->ph1->natt_options) |
325 | return 0; | |
326 | ||
327 | natoa_type = iph2->ph1->natt_options->payload_nat_oa; | |
52b7d2ce A |
328 | if (natoa_type == 0) |
329 | return 0; | |
d1e348cf A |
330 | |
331 | if (iph2->side == INITIATOR) { | |
332 | i_addr = iph2->src; | |
333 | r_addr = iph2->dst; | |
334 | } else { | |
335 | i_addr = iph2->dst; | |
336 | r_addr = iph2->src; | |
337 | } | |
338 | ||
339 | switch (i_addr->sa_family) { | |
52b7d2ce | 340 | case AF_INET: |
d1e348cf | 341 | i_size = sizeof(in_addr_t); |
52b7d2ce A |
342 | break; |
343 | #ifdef INET6 | |
344 | case AF_INET6: | |
d1e348cf | 345 | i_size = sizeof(struct in6_addr); |
52b7d2ce A |
346 | break; |
347 | #endif | |
348 | default: | |
349 | plog(LLV_ERROR, LOCATION, NULL, | |
d1e348cf | 350 | "invalid address family: %d\n", i_addr->sa_family); |
52b7d2ce A |
351 | return -1; |
352 | } | |
d1e348cf A |
353 | |
354 | switch (r_addr->sa_family) { | |
52b7d2ce | 355 | case AF_INET: |
d1e348cf | 356 | r_size = sizeof(in_addr_t); |
52b7d2ce A |
357 | break; |
358 | #ifdef INET6 | |
359 | case AF_INET6: | |
d1e348cf | 360 | r_size = sizeof(struct in6_addr); |
52b7d2ce A |
361 | break; |
362 | #endif | |
363 | default: | |
364 | plog(LLV_ERROR, LOCATION, NULL, | |
d1e348cf | 365 | "invalid address family: %d\n", r_addr->sa_family); |
52b7d2ce A |
366 | return -1; |
367 | } | |
368 | ||
d1e348cf A |
369 | i = vmalloc(sizeof(struct isakmp_pl_natoa) + i_size - sizeof(struct isakmp_gen)); |
370 | if (i == NULL) { | |
371 | plog(LLV_ERROR, LOCATION, NULL, | |
372 | "failed to get buffer for natoa payload.\n"); | |
373 | return -1; | |
374 | } | |
375 | r = vmalloc(sizeof(struct isakmp_pl_natoa) + r_size - sizeof(struct isakmp_gen)); | |
376 | if (r == NULL) { | |
377 | vfree(i); | |
52b7d2ce A |
378 | plog(LLV_ERROR, LOCATION, NULL, |
379 | "failed to get buffer for natoa payload.\n"); | |
380 | return -1; | |
381 | } | |
382 | ||
383 | /* copy src address */ | |
d1e348cf | 384 | p = (__typeof__(p))i->v; |
52b7d2ce | 385 | |
d1e348cf | 386 | switch (i_addr->sa_family) { |
52b7d2ce A |
387 | case AF_INET: |
388 | *p = IPSECDOI_ID_IPV4_ADDR; | |
d1e348cf | 389 | bcopy(&(((struct sockaddr_in *)i_addr)->sin_addr.s_addr), p + sizeof(u_int32_t), i_size); |
52b7d2ce A |
390 | break; |
391 | #ifdef INET6 | |
392 | case AF_INET6: | |
393 | *p = IPSECDOI_ID_IPV6_ADDR; | |
d1e348cf | 394 | bcopy(&(((struct sockaddr_in6 *)i_addr)->sin6_addr), p + sizeof(u_int32_t), i_size); |
52b7d2ce A |
395 | break; |
396 | #endif | |
397 | } | |
398 | ||
399 | /* copy dst address */ | |
d1e348cf | 400 | p = (__typeof__(p))r->v; |
52b7d2ce | 401 | |
d1e348cf | 402 | switch (r_addr->sa_family) { |
52b7d2ce A |
403 | case AF_INET: |
404 | *p = IPSECDOI_ID_IPV4_ADDR; | |
d1e348cf | 405 | bcopy(&(((struct sockaddr_in *)r_addr)->sin_addr.s_addr), p + sizeof(u_int32_t), r_size); |
52b7d2ce A |
406 | break; |
407 | #ifdef INET6 | |
408 | case AF_INET6: | |
409 | *p = IPSECDOI_ID_IPV6_ADDR; | |
d1e348cf | 410 | bcopy(&(((struct sockaddr_in6 *)r_addr)->sin6_addr), p + sizeof(u_int32_t), r_size); |
52b7d2ce A |
411 | break; |
412 | #endif | |
413 | } | |
414 | ||
415 | *natoa_i = i; | |
416 | *natoa_r = r; | |
417 | return natoa_type; | |
418 | } | |
d1e348cf A |
419 | |
420 | struct sockaddr * | |
421 | process_natoa_payload(vchar_t *buf) | |
422 | { | |
423 | struct sockaddr *saddr = NULL; | |
424 | struct ipsecdoi_id_b *id_b = (struct ipsecdoi_id_b *)buf->v; | |
425 | ||
426 | switch (id_b->type) { | |
427 | case IPSECDOI_ID_IPV4_ADDR: | |
428 | saddr = racoon_malloc(sizeof(struct sockaddr_in)); | |
429 | if (!saddr) { | |
430 | plog(LLV_ERROR, LOCATION, NULL, | |
431 | "error allocating addr for NAT-OA payload\n"); | |
432 | return NULL; | |
433 | } | |
434 | saddr->sa_len = sizeof(struct sockaddr_in); | |
435 | saddr->sa_family = AF_INET; | |
436 | ((struct sockaddr_in *)saddr)->sin_port = IPSEC_PORT_ANY; | |
437 | memcpy(&((struct sockaddr_in *)saddr)->sin_addr, | |
438 | buf->v + sizeof(*id_b), sizeof(struct in_addr)); | |
439 | break; | |
440 | #ifdef INET6 | |
441 | case IPSECDOI_ID_IPV6_ADDR: | |
442 | saddr = racoon_malloc(sizeof(struct sockaddr_in6)); | |
443 | if (!saddr) { | |
444 | plog(LLV_ERROR, LOCATION, NULL, | |
445 | "error allocating addr for NAT-OA payload\n"); | |
446 | return NULL; | |
447 | } | |
448 | saddr->sa_len = sizeof(struct sockaddr_in6); | |
449 | saddr->sa_family = AF_INET6; | |
450 | ((struct sockaddr_in6 *)saddr)->sin6_port = IPSEC_PORT_ANY; | |
451 | memcpy(&((struct sockaddr_in6 *)saddr)->sin6_addr, | |
452 | buf->v + sizeof(*id_b), sizeof(struct in6_addr)); | |
453 | break; | |
454 | #endif | |
455 | default: | |
456 | plog(LLV_ERROR, LOCATION, NULL, | |
457 | "invalid NAT-OA payload %d\n", id_b->type); | |
458 | return NULL; | |
459 | } | |
460 | return saddr; | |
461 | } | |
52b7d2ce A |
462 | |
463 | void | |
464 | natt_float_ports (struct ph1handle *iph1) | |
465 | { | |
466 | ||
47612122 | 467 | if (! (iph1->natt_flags & NAT_DETECTED) ) |
52b7d2ce A |
468 | return; |
469 | if (! iph1->natt_options->float_port){ | |
470 | /* Drafts 00 / 01, just schedule keepalive */ | |
471 | #ifndef __APPLE__ | |
472 | natt_keepalive_add_ph1 (iph1); | |
473 | #endif | |
474 | return; | |
475 | } | |
476 | ||
477 | /* | |
478 | * Only switch ports if port == isakmp port. | |
479 | * In the case where ports are set from policy or from | |
480 | * remote config we could be talking to a device behind | |
481 | * a nat using the translated port. | |
482 | */ | |
483 | if (*get_port_ptr(iph1->local) == htons(lcconf->port_isakmp)) | |
484 | set_port (iph1->local, iph1->natt_options->float_port); | |
485 | if (*get_port_ptr(iph1->remote) == htons(lcconf->port_isakmp)) | |
486 | set_port (iph1->remote, iph1->natt_options->float_port); | |
487 | iph1->natt_flags |= NAT_PORTS_CHANGED | NAT_ADD_NON_ESP_MARKER; | |
488 | ||
d1e348cf A |
489 | ike_session_ikev1_float_ports(iph1); |
490 | ||
52b7d2ce A |
491 | #ifndef __APPLE__ |
492 | natt_keepalive_add_ph1 (iph1); | |
493 | #endif | |
494 | } | |
495 | ||
496 | void | |
497 | natt_handle_vendorid (struct ph1handle *iph1, int vid_numeric) | |
498 | { | |
80318cb7 A |
499 | int version; |
500 | ||
52b7d2ce A |
501 | if (! iph1->natt_options) |
502 | iph1->natt_options = racoon_calloc (1, sizeof (*iph1->natt_options)); | |
503 | ||
504 | if (! iph1->natt_options) { | |
505 | plog (LLV_ERROR, LOCATION, NULL, | |
506 | "Allocating memory for natt_options failed!\n"); | |
507 | return; | |
508 | } | |
80318cb7 A |
509 | |
510 | // stick to the version we already selected on a previous phase1 | |
511 | version = ike_session_get_natt_version(iph1); | |
512 | if (version) { | |
513 | vid_numeric = version; | |
514 | } | |
515 | ||
52b7d2ce A |
516 | if (iph1->natt_options->version < vid_numeric) |
517 | if (natt_fill_options (iph1->natt_options, vid_numeric) == 0) | |
518 | iph1->natt_flags |= NAT_ANNOUNCED; | |
519 | } | |
520 | ||
521 | #ifndef __APPLE__ | |
522 | /* NAT keepalive functions */ | |
523 | static void | |
524 | natt_keepalive_send (void *param) | |
525 | { | |
526 | struct natt_ka_addrs *ka, *next = NULL; | |
527 | char keepalive_packet[] = { 0xff }; | |
528 | size_t len; | |
529 | int s; | |
530 | ||
531 | for (ka = TAILQ_FIRST(&ka_tree); ka; ka = next) { | |
532 | next = TAILQ_NEXT(ka, chain); | |
533 | ||
534 | s = getsockmyaddr(ka->src); | |
535 | if (s == -1) { | |
536 | TAILQ_REMOVE (&ka_tree, ka, chain); | |
537 | racoon_free (ka); | |
538 | continue; | |
539 | } | |
540 | plog (LLV_DEBUG, LOCATION, NULL, "KA: %s\n", | |
541 | saddr2str_fromto("%s->%s", ka->src, ka->dst)); | |
542 | len = sendfromto(s, keepalive_packet, sizeof (keepalive_packet), | |
543 | ka->src, ka->dst, 1); | |
544 | if (len == -1) | |
545 | plog(LLV_ERROR, LOCATION, NULL, "KA: sendfromto failed: %s\n", | |
546 | strerror (errno)); | |
547 | } | |
548 | ||
549 | sched_new (lcconf->natt_ka_interval, natt_keepalive_send, NULL); | |
550 | } | |
551 | ||
552 | void | |
553 | natt_keepalive_init (void) | |
554 | { | |
555 | TAILQ_INIT(&ka_tree); | |
556 | ||
557 | /* To disable sending KAs set natt_ka_interval=0 */ | |
558 | if (lcconf->natt_ka_interval > 0) | |
559 | sched_new (lcconf->natt_ka_interval, natt_keepalive_send, NULL); | |
560 | } | |
561 | ||
562 | int | |
563 | natt_keepalive_add (struct sockaddr *src, struct sockaddr *dst) | |
564 | { | |
565 | struct natt_ka_addrs *ka = NULL, *new_addr; | |
566 | ||
567 | TAILQ_FOREACH (ka, &ka_tree, chain) { | |
568 | if (cmpsaddrstrict(ka->src, src) == 0 && | |
569 | cmpsaddrstrict(ka->dst, dst) == 0) { | |
570 | ka->in_use++; | |
571 | plog (LLV_INFO, LOCATION, NULL, "KA found: %s (in_use=%u)\n", | |
572 | saddr2str_fromto("%s->%s", src, dst), ka->in_use); | |
573 | return 0; | |
574 | } | |
575 | } | |
576 | ||
577 | plog (LLV_INFO, LOCATION, NULL, "KA list add: %s\n", saddr2str_fromto("%s->%s", src, dst)); | |
578 | ||
579 | new_addr = (struct natt_ka_addrs *)racoon_malloc(sizeof(*new_addr)); | |
580 | if (! new_addr) { | |
581 | plog (LLV_ERROR, LOCATION, NULL, "Can't allocate new KA list item\n"); | |
582 | return -1; | |
583 | } | |
584 | ||
d1e348cf A |
585 | if ((new_addr->src = dupsaddr(src)) == NULL) { |
586 | racoon_free(new_addr); | |
587 | plog (LLV_ERROR, LOCATION, NULL, "Can't allocate new KA list item\n"); | |
588 | return -1; | |
589 | } | |
590 | if ((new_addr->dst = dupsaddr(dst)) == NULL) { | |
591 | racoon_free(new_addr); | |
592 | plog (LLV_ERROR, LOCATION, NULL, "Can't allocate new KA list item\n"); | |
593 | return -1; | |
594 | } | |
52b7d2ce A |
595 | new_addr->in_use = 1; |
596 | TAILQ_INSERT_TAIL(&ka_tree, new_addr, chain); | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | int | |
602 | natt_keepalive_add_ph1 (struct ph1handle *iph1) | |
603 | { | |
604 | int ret = 0; | |
605 | ||
606 | /* Should only the NATed host send keepalives? | |
607 | If yes, add '(iph1->natt_flags & NAT_DETECTED_ME)' | |
608 | to the following condition. */ | |
609 | if (iph1->natt_flags & NAT_DETECTED && | |
610 | ! (iph1->natt_flags & NAT_KA_QUEUED)) { | |
611 | ret = natt_keepalive_add (iph1->local, iph1->remote); | |
612 | if (ret == 0) | |
613 | iph1->natt_flags |= NAT_KA_QUEUED; | |
614 | } | |
615 | ||
616 | return ret; | |
617 | } | |
618 | ||
619 | void | |
620 | natt_keepalive_remove (struct sockaddr *src, struct sockaddr *dst) | |
621 | { | |
622 | struct natt_ka_addrs *ka, *next = NULL; | |
623 | ||
624 | plog (LLV_INFO, LOCATION, NULL, "KA remove: %s\n", saddr2str_fromto("%s->%s", src, dst)); | |
625 | ||
626 | for (ka = TAILQ_FIRST(&ka_tree); ka; ka = next) { | |
627 | next = TAILQ_NEXT(ka, chain); | |
628 | ||
629 | plog (LLV_DEBUG, LOCATION, NULL, "KA tree dump: %s (in_use=%u)\n", | |
630 | saddr2str_fromto("%s->%s", src, dst), ka->in_use); | |
631 | ||
632 | if (cmpsaddrstrict(ka->src, src) == 0 && | |
633 | cmpsaddrstrict(ka->dst, dst) == 0 && | |
634 | -- ka->in_use <= 0) { | |
635 | ||
636 | plog (LLV_DEBUG, LOCATION, NULL, "KA removing this one...\n"); | |
637 | ||
638 | TAILQ_REMOVE (&ka_tree, ka, chain); | |
639 | racoon_free (ka); | |
640 | /* Should we break here? Every pair of addresses should | |
641 | be inserted only once, but who knows :-) Lets traverse | |
642 | the whole list... */ | |
643 | } | |
644 | } | |
645 | } | |
646 | #endif /* __APPLE__ */ | |
647 | ||
648 | static struct remoteconf * | |
649 | natt_enabled_in_rmconf_stub (struct remoteconf *rmconf, void *data) | |
650 | { | |
651 | return (rmconf->nat_traversal ? rmconf : NULL); | |
652 | } | |
653 | ||
654 | int | |
655 | natt_enabled_in_rmconf () | |
656 | { | |
657 | return foreachrmconf (natt_enabled_in_rmconf_stub, NULL) != NULL; | |
658 | } | |
659 | ||
660 | ||
661 | struct payload_list * | |
662 | isakmp_plist_append_natt_vids (struct payload_list *plist, vchar_t *vid_natt[MAX_NATT_VID_COUNT]){ | |
663 | int i, vid_natt_i = 0; | |
664 | ||
665 | if(vid_natt == NULL) | |
666 | return NULL; | |
667 | ||
668 | for (i = 0; i < MAX_NATT_VID_COUNT; i++) | |
669 | vid_natt[i]=NULL; | |
670 | ||
671 | /* Puts the olders VIDs last, as some implementations may choose the first | |
672 | * NATT VID given | |
673 | */ | |
674 | ||
675 | /* Always set RFC VID | |
676 | */ | |
677 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_RFC)) != NULL) | |
678 | vid_natt_i++; | |
679 | #ifdef ENABLE_NATT_APPLE | |
680 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_APPLE)) != NULL) | |
681 | vid_natt_i++; | |
682 | #endif | |
683 | #ifdef ENABLE_NATT_08 | |
684 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_08)) != NULL) | |
685 | vid_natt_i++; | |
686 | #endif | |
687 | #ifdef ENABLE_NATT_07 | |
688 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_07)) != NULL) | |
689 | vid_natt_i++; | |
690 | #endif | |
691 | #ifdef ENABLE_NATT_06 | |
692 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_06)) != NULL) | |
693 | vid_natt_i++; | |
694 | #endif | |
695 | #ifdef ENABLE_NATT_05 | |
696 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_05)) != NULL) | |
697 | vid_natt_i++; | |
698 | #endif | |
699 | #ifdef ENABLE_NATT_04 | |
700 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_04)) != NULL) | |
701 | vid_natt_i++; | |
702 | #endif | |
703 | #ifdef ENABLE_NATT_03 | |
704 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_03)) != NULL) | |
705 | vid_natt_i++; | |
706 | #endif | |
707 | #ifdef ENABLE_NATT_02 | |
708 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_02)) != NULL) | |
709 | vid_natt_i++; | |
710 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_02_N)) != NULL) | |
711 | vid_natt_i++; | |
712 | #endif | |
713 | #ifdef ENABLE_NATT_01 | |
714 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_01)) != NULL) | |
715 | vid_natt_i++; | |
716 | #endif | |
717 | #ifdef ENABLE_NATT_00 | |
718 | if ((vid_natt[vid_natt_i] = set_vendorid(VENDORID_NATT_00)) != NULL) | |
719 | vid_natt_i++; | |
720 | #endif | |
721 | /* set VID payload for NAT-T */ | |
722 | for (i = 0; i < vid_natt_i; i++) | |
723 | plist = isakmp_plist_append(plist, vid_natt[i], ISAKMP_NPTYPE_VID); | |
724 | ||
725 | return plist; | |
726 | } |