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>
37 #include <linux/udp.h>
39 #if defined(__NetBSD__) || defined (__FreeBSD__)
40 #include <netinet/udp.h>
55 #include "localconf.h"
56 #include "remoteconf.h"
58 #include "isakmp_var.h"
61 #include "ipsec_doi.h"
64 #include "crypto_openssl.h"
66 #include "nattraversal.h"
67 #include "grabmyaddr.h"
68 #include "ike_session.h"
70 struct natt_ka_addrs
{
75 TAILQ_ENTRY(natt_ka_addrs
) chain
;
78 static TAILQ_HEAD(_natt_ka_addrs
, natt_ka_addrs
) ka_tree
;
81 * check if the given vid is NAT-T.
84 natt_vendorid (int vid
)
88 vid
== VENDORID_NATT_00
||
91 vid
== VENDORID_NATT_01
||
94 vid
== VENDORID_NATT_02
||
95 vid
== VENDORID_NATT_02_N
||
98 vid
== VENDORID_NATT_03
||
100 #ifdef ENABLE_NATT_04
101 vid
== VENDORID_NATT_04
||
103 #ifdef ENABLE_NATT_05
104 vid
== VENDORID_NATT_05
||
106 #ifdef ENABLE_NATT_06
107 vid
== VENDORID_NATT_06
||
109 #ifdef ENABLE_NATT_07
110 vid
== VENDORID_NATT_07
||
112 #ifdef ENABLE_NATT_08
113 vid
== VENDORID_NATT_08
||
115 #ifdef ENABLE_NATT_APPLE
116 vid
== VENDORID_NATT_APPLE
||
118 /* Always enable NATT RFC if ENABLE_NATT
120 vid
== VENDORID_NATT_RFC
);
124 natt_hash_addr (struct ph1handle
*iph1
, struct sockaddr
*addr
)
129 void *addr_ptr
, *addr_port
;
130 size_t buf_size
, addr_size
;
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)":"");
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
;
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
;
147 plog (LLV_ERROR
, LOCATION
, addr
, "Unsupported address family #0x%x\n", addr
->sa_family
);
151 buf_size
= 2 * sizeof (cookie_t
); /* CKY-I + CKY+R */
152 buf_size
+= addr_size
+ 2; /* Address + Port */
154 if ((buf
= vmalloc (buf_size
)) == NULL
)
160 memcpy (ptr
, iph1
->index
.i_ck
, sizeof (cookie_t
));
161 ptr
+= sizeof (cookie_t
);
164 memcpy (ptr
, iph1
->index
.r_ck
, sizeof (cookie_t
));
165 ptr
+= sizeof (cookie_t
);
167 /* Copy-in Address (or zeroes if NATT_FORCE) */
168 if (iph1
->rmconf
->nat_traversal
== NATT_FORCE
)
169 memset (ptr
, 0, addr_size
);
171 memcpy (ptr
, addr_ptr
, addr_size
);
174 /* Copy-in Port number */
175 memcpy (ptr
, addr_port
, 2);
177 natd
= oakley_hash (buf
, iph1
);
184 natt_compare_addr_hash (struct ph1handle
*iph1
, vchar_t
*natd_received
,
187 vchar_t
*natd_computed
;
191 if (iph1
->rmconf
->nat_traversal
== NATT_FORCE
)
195 /* old APPLE version sends natd payload in the wrong order */
196 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
198 natd_computed
= natt_hash_addr (iph1
, iph1
->remote
);
199 flag
= NAT_DETECTED_PEER
;
202 natd_computed
= natt_hash_addr (iph1
, iph1
->local
);
203 flag
= NAT_DETECTED_ME
;
209 natd_computed
= natt_hash_addr (iph1
, iph1
->local
);
210 flag
= NAT_DETECTED_ME
;
213 natd_computed
= natt_hash_addr (iph1
, iph1
->remote
);
214 flag
= NAT_DETECTED_PEER
;
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
;
224 if (iph1
->parent_session
)
225 iph1
->parent_session
->natt_flags
= iph1
->natt_flags
;
227 vfree (natd_computed
);
233 natt_udp_encap (int encmode
)
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
);
242 natt_fill_options (struct ph1natt_options
*opts
, int version
)
247 opts
->version
= version
;
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
;
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
;
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
:
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
;
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
;
296 plog(LLV_ERROR
, LOCATION
, NULL
,
297 "unsupported NAT-T version: %s\n",
298 vid_string_by_id(version
));
302 opts
->mode_udp_diff
= opts
->mode_udp_tunnel
- IPSECDOI_ATTR_ENC_MODE_TUNNEL
;
308 create_natoa_payloads(struct ph2handle
*iph2
, vchar_t
**natoa_i
, vchar_t
**natoa_r
)
314 struct sockaddr
*i_addr
;
315 struct sockaddr
*r_addr
;
319 *natoa_i
= *natoa_r
= NULL
;
322 /* create natoa payloads if natt being used */
323 /* don't send if type == apple */
324 if (!iph2
->ph1
->natt_options
)
327 natoa_type
= iph2
->ph1
->natt_options
->payload_nat_oa
;
331 if (iph2
->side
== INITIATOR
) {
339 switch (i_addr
->sa_family
) {
341 i_size
= sizeof(in_addr_t
);
345 i_size
= sizeof(struct in6_addr
);
349 plog(LLV_ERROR
, LOCATION
, NULL
,
350 "invalid address family: %d\n", i_addr
->sa_family
);
354 switch (r_addr
->sa_family
) {
356 r_size
= sizeof(in_addr_t
);
360 r_size
= sizeof(struct in6_addr
);
364 plog(LLV_ERROR
, LOCATION
, NULL
,
365 "invalid address family: %d\n", r_addr
->sa_family
);
369 i
= vmalloc(sizeof(struct isakmp_pl_natoa
) + i_size
- sizeof(struct isakmp_gen
));
371 plog(LLV_ERROR
, LOCATION
, NULL
,
372 "failed to get buffer for natoa payload.\n");
375 r
= vmalloc(sizeof(struct isakmp_pl_natoa
) + r_size
- sizeof(struct isakmp_gen
));
378 plog(LLV_ERROR
, LOCATION
, NULL
,
379 "failed to get buffer for natoa payload.\n");
383 /* copy src address */
384 p
= (__typeof__(p
))i
->v
;
386 switch (i_addr
->sa_family
) {
388 *p
= IPSECDOI_ID_IPV4_ADDR
;
389 bcopy(&(((struct sockaddr_in
*)i_addr
)->sin_addr
.s_addr
), p
+ sizeof(u_int32_t
), i_size
);
393 *p
= IPSECDOI_ID_IPV6_ADDR
;
394 bcopy(&(((struct sockaddr_in6
*)i_addr
)->sin6_addr
), p
+ sizeof(u_int32_t
), i_size
);
399 /* copy dst address */
400 p
= (__typeof__(p
))r
->v
;
402 switch (r_addr
->sa_family
) {
404 *p
= IPSECDOI_ID_IPV4_ADDR
;
405 bcopy(&(((struct sockaddr_in
*)r_addr
)->sin_addr
.s_addr
), p
+ sizeof(u_int32_t
), r_size
);
409 *p
= IPSECDOI_ID_IPV6_ADDR
;
410 bcopy(&(((struct sockaddr_in6
*)r_addr
)->sin6_addr
), p
+ sizeof(u_int32_t
), r_size
);
421 process_natoa_payload(vchar_t
*buf
)
423 struct sockaddr
*saddr
= NULL
;
424 struct ipsecdoi_id_b
*id_b
= (struct ipsecdoi_id_b
*)buf
->v
;
426 switch (id_b
->type
) {
427 case IPSECDOI_ID_IPV4_ADDR
:
428 saddr
= racoon_malloc(sizeof(struct sockaddr_in
));
430 plog(LLV_ERROR
, LOCATION
, NULL
,
431 "error allocating addr for NAT-OA payload\n");
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
));
441 case IPSECDOI_ID_IPV6_ADDR
:
442 saddr
= racoon_malloc(sizeof(struct sockaddr_in6
));
444 plog(LLV_ERROR
, LOCATION
, NULL
,
445 "error allocating addr for NAT-OA payload\n");
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
));
456 plog(LLV_ERROR
, LOCATION
, NULL
,
457 "invalid NAT-OA payload %d\n", id_b
->type
);
464 natt_float_ports (struct ph1handle
*iph1
)
467 if (! (iph1
->natt_flags
&& NAT_DETECTED
) )
469 if (! iph1
->natt_options
->float_port
){
470 /* Drafts 00 / 01, just schedule keepalive */
472 natt_keepalive_add_ph1 (iph1
);
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.
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
;
489 ike_session_ikev1_float_ports(iph1
);
492 natt_keepalive_add_ph1 (iph1
);
497 natt_handle_vendorid (struct ph1handle
*iph1
, int vid_numeric
)
499 if (! iph1
->natt_options
)
500 iph1
->natt_options
= racoon_calloc (1, sizeof (*iph1
->natt_options
));
502 if (! iph1
->natt_options
) {
503 plog (LLV_ERROR
, LOCATION
, NULL
,
504 "Allocating memory for natt_options failed!\n");
508 if (iph1
->natt_options
->version
< vid_numeric
)
509 if (natt_fill_options (iph1
->natt_options
, vid_numeric
) == 0)
510 iph1
->natt_flags
|= NAT_ANNOUNCED
;
514 /* NAT keepalive functions */
516 natt_keepalive_send (void *param
)
518 struct natt_ka_addrs
*ka
, *next
= NULL
;
519 char keepalive_packet
[] = { 0xff };
523 for (ka
= TAILQ_FIRST(&ka_tree
); ka
; ka
= next
) {
524 next
= TAILQ_NEXT(ka
, chain
);
526 s
= getsockmyaddr(ka
->src
);
528 TAILQ_REMOVE (&ka_tree
, ka
, chain
);
532 plog (LLV_DEBUG
, LOCATION
, NULL
, "KA: %s\n",
533 saddr2str_fromto("%s->%s", ka
->src
, ka
->dst
));
534 len
= sendfromto(s
, keepalive_packet
, sizeof (keepalive_packet
),
535 ka
->src
, ka
->dst
, 1);
537 plog(LLV_ERROR
, LOCATION
, NULL
, "KA: sendfromto failed: %s\n",
541 sched_new (lcconf
->natt_ka_interval
, natt_keepalive_send
, NULL
);
545 natt_keepalive_init (void)
547 TAILQ_INIT(&ka_tree
);
549 /* To disable sending KAs set natt_ka_interval=0 */
550 if (lcconf
->natt_ka_interval
> 0)
551 sched_new (lcconf
->natt_ka_interval
, natt_keepalive_send
, NULL
);
555 natt_keepalive_add (struct sockaddr
*src
, struct sockaddr
*dst
)
557 struct natt_ka_addrs
*ka
= NULL
, *new_addr
;
559 TAILQ_FOREACH (ka
, &ka_tree
, chain
) {
560 if (cmpsaddrstrict(ka
->src
, src
) == 0 &&
561 cmpsaddrstrict(ka
->dst
, dst
) == 0) {
563 plog (LLV_INFO
, LOCATION
, NULL
, "KA found: %s (in_use=%u)\n",
564 saddr2str_fromto("%s->%s", src
, dst
), ka
->in_use
);
569 plog (LLV_INFO
, LOCATION
, NULL
, "KA list add: %s\n", saddr2str_fromto("%s->%s", src
, dst
));
571 new_addr
= (struct natt_ka_addrs
*)racoon_malloc(sizeof(*new_addr
));
573 plog (LLV_ERROR
, LOCATION
, NULL
, "Can't allocate new KA list item\n");
577 if ((new_addr
->src
= dupsaddr(src
)) == NULL
) {
578 racoon_free(new_addr
);
579 plog (LLV_ERROR
, LOCATION
, NULL
, "Can't allocate new KA list item\n");
582 if ((new_addr
->dst
= dupsaddr(dst
)) == NULL
) {
583 racoon_free(new_addr
);
584 plog (LLV_ERROR
, LOCATION
, NULL
, "Can't allocate new KA list item\n");
587 new_addr
->in_use
= 1;
588 TAILQ_INSERT_TAIL(&ka_tree
, new_addr
, chain
);
594 natt_keepalive_add_ph1 (struct ph1handle
*iph1
)
598 /* Should only the NATed host send keepalives?
599 If yes, add '(iph1->natt_flags & NAT_DETECTED_ME)'
600 to the following condition. */
601 if (iph1
->natt_flags
& NAT_DETECTED
&&
602 ! (iph1
->natt_flags
& NAT_KA_QUEUED
)) {
603 ret
= natt_keepalive_add (iph1
->local
, iph1
->remote
);
605 iph1
->natt_flags
|= NAT_KA_QUEUED
;
612 natt_keepalive_remove (struct sockaddr
*src
, struct sockaddr
*dst
)
614 struct natt_ka_addrs
*ka
, *next
= NULL
;
616 plog (LLV_INFO
, LOCATION
, NULL
, "KA remove: %s\n", saddr2str_fromto("%s->%s", src
, dst
));
618 for (ka
= TAILQ_FIRST(&ka_tree
); ka
; ka
= next
) {
619 next
= TAILQ_NEXT(ka
, chain
);
621 plog (LLV_DEBUG
, LOCATION
, NULL
, "KA tree dump: %s (in_use=%u)\n",
622 saddr2str_fromto("%s->%s", src
, dst
), ka
->in_use
);
624 if (cmpsaddrstrict(ka
->src
, src
) == 0 &&
625 cmpsaddrstrict(ka
->dst
, dst
) == 0 &&
626 -- ka
->in_use
<= 0) {
628 plog (LLV_DEBUG
, LOCATION
, NULL
, "KA removing this one...\n");
630 TAILQ_REMOVE (&ka_tree
, ka
, chain
);
632 /* Should we break here? Every pair of addresses should
633 be inserted only once, but who knows :-) Lets traverse
638 #endif /* __APPLE__ */
640 static struct remoteconf
*
641 natt_enabled_in_rmconf_stub (struct remoteconf
*rmconf
, void *data
)
643 return (rmconf
->nat_traversal
? rmconf
: NULL
);
647 natt_enabled_in_rmconf ()
649 return foreachrmconf (natt_enabled_in_rmconf_stub
, NULL
) != NULL
;
653 struct payload_list
*
654 isakmp_plist_append_natt_vids (struct payload_list
*plist
, vchar_t
*vid_natt
[MAX_NATT_VID_COUNT
]){
655 int i
, vid_natt_i
= 0;
660 for (i
= 0; i
< MAX_NATT_VID_COUNT
; i
++)
663 /* Puts the olders VIDs last, as some implementations may choose the first
667 /* Always set RFC VID
669 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_RFC
)) != NULL
)
671 #ifdef ENABLE_NATT_APPLE
672 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_APPLE
)) != NULL
)
675 #ifdef ENABLE_NATT_08
676 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_08
)) != NULL
)
679 #ifdef ENABLE_NATT_07
680 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_07
)) != NULL
)
683 #ifdef ENABLE_NATT_06
684 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_06
)) != NULL
)
687 #ifdef ENABLE_NATT_05
688 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_05
)) != NULL
)
691 #ifdef ENABLE_NATT_04
692 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_04
)) != NULL
)
695 #ifdef ENABLE_NATT_03
696 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_03
)) != NULL
)
699 #ifdef ENABLE_NATT_02
700 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_02
)) != NULL
)
702 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_02_N
)) != NULL
)
705 #ifdef ENABLE_NATT_01
706 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_01
)) != NULL
)
709 #ifdef ENABLE_NATT_00
710 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_00
)) != NULL
)
713 /* set VID payload for NAT-T */
714 for (i
= 0; i
< vid_natt_i
; i
++)
715 plist
= isakmp_plist_append(plist
, vid_natt
[i
], ISAKMP_NPTYPE_VID
);