2 * Copyright (C) 2004 SuSE Linux AG, Nuernberg, Germany.
3 * Contributed by: Michal Ludvig <mludvig@suse.cz>, SUSE Labs
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <netinet/udp.h>
50 #include "localconf.h"
51 #include "remoteconf.h"
53 #include "isakmp_var.h"
56 #include "ipsec_doi.h"
59 #include "crypto_openssl.h"
61 #include "nattraversal.h"
62 #include "grabmyaddr.h"
63 #include "ike_session.h"
65 struct natt_ka_addrs
{
66 struct sockaddr_storage
*src
;
67 struct sockaddr_storage
*dst
;
70 TAILQ_ENTRY(natt_ka_addrs
) chain
;
74 * check if the given vid is NAT-T.
77 natt_vendorid (int vid
)
81 vid
== VENDORID_NATT_00
||
84 vid
== VENDORID_NATT_01
||
87 vid
== VENDORID_NATT_02
||
88 vid
== VENDORID_NATT_02_N
||
91 vid
== VENDORID_NATT_03
||
94 vid
== VENDORID_NATT_04
||
97 vid
== VENDORID_NATT_05
||
100 vid
== VENDORID_NATT_06
||
102 #ifdef ENABLE_NATT_07
103 vid
== VENDORID_NATT_07
||
105 #ifdef ENABLE_NATT_08
106 vid
== VENDORID_NATT_08
||
108 #ifdef ENABLE_NATT_APPLE
109 vid
== VENDORID_NATT_APPLE
||
111 /* Always enable NATT RFC if ENABLE_NATT
113 vid
== VENDORID_NATT_RFC
);
117 natt_hash_addr (phase1_handle_t
*iph1
, struct sockaddr_storage
*addr
)
122 void *addr_ptr
, *addr_port
;
123 size_t buf_size
, addr_size
;
125 if (iph1
->approval
) {
126 plog(ASL_LEVEL_NOTICE
, "Hashing %s with algo #%d %s\n",
127 saddr2str((struct sockaddr
*)addr
), iph1
->approval
->hashtype
,
128 (iph1
->rmconf
->nat_traversal
== NATT_FORCE
)?"(NAT-T forced)":"");
131 if (addr
->ss_family
== AF_INET
) {
132 addr_size
= sizeof (struct in_addr
); /* IPv4 address */
133 addr_ptr
= &((struct sockaddr_in
*)addr
)->sin_addr
;
134 addr_port
= &((struct sockaddr_in
*)addr
)->sin_port
;
136 else if (addr
->ss_family
== AF_INET6
) {
137 addr_size
= sizeof (struct in6_addr
); /* IPv6 address */
138 addr_ptr
= &((struct sockaddr_in6
*)addr
)->sin6_addr
;
139 addr_port
= &((struct sockaddr_in6
*)addr
)->sin6_port
;
142 plog(ASL_LEVEL_ERR
, "Unsupported address family #0x%x\n", addr
->ss_family
);
146 buf_size
= 2 * sizeof (cookie_t
); /* CKY-I + CKY+R */
147 buf_size
+= addr_size
+ 2; /* Address + Port */
149 if ((buf
= vmalloc (buf_size
)) == NULL
)
155 memcpy (ptr
, iph1
->index
.i_ck
, sizeof (cookie_t
));
156 ptr
+= sizeof (cookie_t
);
159 memcpy (ptr
, iph1
->index
.r_ck
, sizeof (cookie_t
));
160 ptr
+= sizeof (cookie_t
);
162 /* Copy-in Address (or zeroes if NATT_FORCE) */
163 if (iph1
->rmconf
->nat_traversal
== NATT_FORCE
)
164 memset (ptr
, 0, addr_size
);
166 memcpy (ptr
, addr_ptr
, addr_size
);
169 /* Copy-in Port number */
170 memcpy (ptr
, addr_port
, 2);
172 natd
= oakley_hash (buf
, iph1
);
179 natt_compare_addr_hash (phase1_handle_t
*iph1
, vchar_t
*natd_received
,
182 vchar_t
*natd_computed
;
186 if (iph1
->rmconf
->nat_traversal
== NATT_FORCE
)
189 /* old APPLE version sends natd payload in the wrong order */
190 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
192 natd_computed
= natt_hash_addr (iph1
, iph1
->remote
);
193 flag
= NAT_DETECTED_PEER
;
196 natd_computed
= natt_hash_addr (iph1
, iph1
->local
);
197 flag
= NAT_DETECTED_ME
;
202 natd_computed
= natt_hash_addr (iph1
, iph1
->local
);
203 flag
= NAT_DETECTED_ME
;
206 natd_computed
= natt_hash_addr (iph1
, iph1
->remote
);
207 flag
= NAT_DETECTED_PEER
;
211 if (natd_received
->l
== natd_computed
->l
&&
212 memcmp (natd_received
->v
, natd_computed
->v
, natd_received
->l
) == 0) {
213 iph1
->natt_flags
&= ~flag
;
217 if (iph1
->parent_session
)
218 iph1
->parent_session
->natt_flags
= iph1
->natt_flags
;
220 vfree (natd_computed
);
226 natt_udp_encap (int encmode
)
228 return (encmode
== IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
||
229 encmode
== IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
||
230 encmode
== IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
||
231 encmode
== IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
);
235 natt_fill_options (struct ph1natt_options
*opts
, int version
)
240 opts
->version
= version
;
243 case VENDORID_NATT_02
:
244 case VENDORID_NATT_02_N
:
245 case VENDORID_NATT_03
:
246 opts
->float_port
= lcconf
->port_isakmp_natt
;
247 opts
->payload_nat_d
= ISAKMP_NPTYPE_NATD_DRAFT
;
248 opts
->payload_nat_oa
= ISAKMP_NPTYPE_NATOA_DRAFT
;
249 opts
->mode_udp_tunnel
= IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
;
250 opts
->mode_udp_transport
= IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
;
251 opts
->encaps_type
= UDP_ENCAP_ESPINUDP
;
253 case VENDORID_NATT_04
:
254 case VENDORID_NATT_05
:
255 case VENDORID_NATT_06
:
256 case VENDORID_NATT_07
:
257 case VENDORID_NATT_08
:
258 case VENDORID_NATT_APPLE
:
259 opts
->float_port
= lcconf
->port_isakmp_natt
;
260 opts
->payload_nat_d
= ISAKMP_NPTYPE_NATD_BADDRAFT
;
261 opts
->payload_nat_oa
= ISAKMP_NPTYPE_NONE
;
262 opts
->mode_udp_tunnel
= IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
;
263 opts
->mode_udp_transport
= IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
;
264 opts
->encaps_type
= UDP_ENCAP_ESPINUDP
;
266 case VENDORID_NATT_RFC
:
267 opts
->float_port
= lcconf
->port_isakmp_natt
;
268 opts
->payload_nat_d
= ISAKMP_NPTYPE_NATD_RFC
;
269 opts
->payload_nat_oa
= ISAKMP_NPTYPE_NATOA_RFC
;
270 opts
->mode_udp_tunnel
= IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
;
271 opts
->mode_udp_transport
= IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
;
272 opts
->encaps_type
= UDP_ENCAP_ESPINUDP
;
276 "unsupported NAT-T version: %s\n",
277 vid_string_by_id(version
));
281 opts
->mode_udp_diff
= opts
->mode_udp_tunnel
- IPSECDOI_ATTR_ENC_MODE_TUNNEL
;
287 create_natoa_payloads(phase2_handle_t
*iph2
, vchar_t
**natoa_i
, vchar_t
**natoa_r
)
293 struct sockaddr_storage
*i_addr
;
294 struct sockaddr_storage
*r_addr
;
298 *natoa_i
= *natoa_r
= NULL
;
301 /* create natoa payloads if natt being used */
302 /* don't send if type == apple */
303 if (!iph2
->ph1
->natt_options
)
306 natoa_type
= iph2
->ph1
->natt_options
->payload_nat_oa
;
310 if (iph2
->side
== INITIATOR
) {
318 switch (i_addr
->ss_family
) {
320 i_size
= sizeof(in_addr_t
);
324 i_size
= sizeof(struct in6_addr
);
329 "invalid address family: %d\n", i_addr
->ss_family
);
333 switch (r_addr
->ss_family
) {
335 r_size
= sizeof(in_addr_t
);
339 r_size
= sizeof(struct in6_addr
);
344 "invalid address family: %d\n", r_addr
->ss_family
);
348 i
= vmalloc(sizeof(struct isakmp_pl_natoa
) + i_size
- sizeof(struct isakmp_gen
));
351 "failed to get buffer for natoa payload.\n");
354 r
= vmalloc(sizeof(struct isakmp_pl_natoa
) + r_size
- sizeof(struct isakmp_gen
));
358 "failed to get buffer for natoa payload.\n");
362 /* copy src address */
363 p
= (__typeof__(p
))i
->v
;
365 switch (i_addr
->ss_family
) {
367 *p
= IPSECDOI_ID_IPV4_ADDR
;
368 bcopy(&(((struct sockaddr_in
*)i_addr
)->sin_addr
.s_addr
), p
+ sizeof(u_int32_t
), i_size
);
372 *p
= IPSECDOI_ID_IPV6_ADDR
;
373 bcopy(&(((struct sockaddr_in6
*)i_addr
)->sin6_addr
), p
+ sizeof(u_int32_t
), i_size
);
378 /* copy dst address */
379 p
= (__typeof__(p
))r
->v
;
381 switch (r_addr
->ss_family
) {
383 *p
= IPSECDOI_ID_IPV4_ADDR
;
384 bcopy(&(((struct sockaddr_in
*)r_addr
)->sin_addr
.s_addr
), p
+ sizeof(u_int32_t
), r_size
);
388 *p
= IPSECDOI_ID_IPV6_ADDR
;
389 bcopy(&(((struct sockaddr_in6
*)r_addr
)->sin6_addr
), p
+ sizeof(u_int32_t
), r_size
);
399 struct sockaddr_storage
*
400 process_natoa_payload(vchar_t
*buf
)
402 struct sockaddr_storage
*saddr
= NULL
;
403 struct ipsecdoi_id_b
*id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)buf
->v
;
405 switch (id_b
->type
) {
406 case IPSECDOI_ID_IPV4_ADDR
:
407 saddr
= racoon_malloc(sizeof(struct sockaddr_in
));
410 "error allocating addr for NAT-OA payload\n");
413 saddr
->ss_len
= sizeof(struct sockaddr_in
);
414 saddr
->ss_family
= AF_INET
;
415 ((struct sockaddr_in
*)saddr
)->sin_port
= IPSEC_PORT_ANY
;
416 memcpy(&((struct sockaddr_in
*)saddr
)->sin_addr
,
417 buf
->v
+ sizeof(*id_b
), sizeof(struct in_addr
));
420 case IPSECDOI_ID_IPV6_ADDR
:
421 saddr
= racoon_malloc(sizeof(struct sockaddr_in6
));
424 "error allocating addr for NAT-OA payload\n");
427 saddr
->ss_len
= sizeof(struct sockaddr_in6
);
428 saddr
->ss_family
= AF_INET6
;
429 ((struct sockaddr_in6
*)saddr
)->sin6_port
= IPSEC_PORT_ANY
;
430 memcpy(&((struct sockaddr_in6
*)saddr
)->sin6_addr
,
431 buf
->v
+ sizeof(*id_b
), sizeof(struct in6_addr
));
436 "invalid NAT-OA payload %d\n", id_b
->type
);
443 natt_float_ports (phase1_handle_t
*iph1
)
446 if (! (iph1
->natt_flags
& NAT_DETECTED
) )
448 if (! iph1
->natt_options
->float_port
){
449 /* Drafts 00 / 01, just schedule keepalive */
454 * Only switch ports if port == isakmp port.
455 * In the case where ports are set from policy or from
456 * remote config we could be talking to a device behind
457 * a nat using the translated port.
459 if (*get_port_ptr(iph1
->local
) == htons(lcconf
->port_isakmp
))
460 set_port (iph1
->local
, iph1
->natt_options
->float_port
);
461 if (*get_port_ptr(iph1
->remote
) == htons(lcconf
->port_isakmp
))
462 set_port (iph1
->remote
, iph1
->natt_options
->float_port
);
463 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
465 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
466 ike_session_ikev1_float_ports(iph1
);
470 natt_handle_vendorid (phase1_handle_t
*iph1
, int vid_numeric
)
474 if (! iph1
->natt_options
)
475 iph1
->natt_options
= racoon_calloc (1, sizeof (*iph1
->natt_options
));
477 if (! iph1
->natt_options
) {
479 "Allocating memory for natt_options failed!\n");
483 // stick to the version we already selected on a previous phase1
484 version
= ike_session_get_natt_version(iph1
);
486 vid_numeric
= version
;
489 if (iph1
->natt_options
->version
< vid_numeric
)
490 if (natt_fill_options (iph1
->natt_options
, vid_numeric
) == 0)
491 iph1
->natt_flags
|= NAT_ANNOUNCED
;
494 static struct remoteconf
*
495 natt_enabled_in_rmconf_stub (struct remoteconf
*rmconf
, void *data
)
497 return (rmconf
->nat_traversal
? rmconf
: NULL
);
501 natt_enabled_in_rmconf ()
503 return foreachrmconf (natt_enabled_in_rmconf_stub
, NULL
) != NULL
;
507 struct payload_list
*
508 isakmp_plist_append_natt_vids (struct payload_list
*plist
, vchar_t
*vid_natt
[MAX_NATT_VID_COUNT
]){
509 int i
, vid_natt_i
= 0;
514 for (i
= 0; i
< MAX_NATT_VID_COUNT
; i
++)
517 /* Puts the olders VIDs last, as some implementations may choose the first
521 /* Always set RFC VID
523 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_RFC
)) != NULL
)
525 #ifdef ENABLE_NATT_APPLE
526 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_APPLE
)) != NULL
)
529 #ifdef ENABLE_NATT_08
530 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_08
)) != NULL
)
533 #ifdef ENABLE_NATT_07
534 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_07
)) != NULL
)
537 #ifdef ENABLE_NATT_06
538 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_06
)) != NULL
)
541 #ifdef ENABLE_NATT_05
542 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_05
)) != NULL
)
545 #ifdef ENABLE_NATT_04
546 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_04
)) != NULL
)
549 #ifdef ENABLE_NATT_03
550 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_03
)) != NULL
)
553 #ifdef ENABLE_NATT_02
554 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_02
)) != NULL
)
556 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_02_N
)) != NULL
)
559 #ifdef ENABLE_NATT_01
560 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_01
)) != NULL
)
563 #ifdef ENABLE_NATT_00
564 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_00
)) != NULL
)
567 /* set VID payload for NAT-T */
568 for (i
= 0; i
< vid_natt_i
; i
++)
569 plist
= isakmp_plist_append(plist
, vid_natt
[i
], ISAKMP_NPTYPE_VID
);