Handle IKEv1 NAT-T vendor ID payload (only RFC 3947 for now).
[strongswan.git] / src / libcharon / sa / tasks / ike_vendor_v1.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "ike_vendor_v1.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/vendor_id_payload.h>
20
21 typedef struct private_ike_vendor_v1_t private_ike_vendor_v1_t;
22
23 /**
24 * Private data of an ike_vendor_v1_t object.
25 */
26 struct private_ike_vendor_v1_t {
27
28 /**
29 * Public ike_vendor_v1_t interface.
30 */
31 ike_vendor_v1_t public;
32
33 /**
34 * Associated IKE_SA
35 */
36 ike_sa_t *ike_sa;
37
38 /**
39 * Are we the inititator of this task
40 */
41 bool initiator;
42 };
43
44 /**
45 * Indicate support for XAuth, MD5("draft-ietf-ipsra-isakmp-xauth-06.txt")
46 * Truncated to the first half.
47 */
48 static chunk_t xauth6_vid = chunk_from_chars(
49 0x09,0x00,0x26,0x89,0xdf,0xd6,0xb7,0x12
50 );
51
52 /**
53 * Indicate support for NAT-Traversal, MD5("RFC 3947")
54 */
55 static chunk_t natt_vid = chunk_from_chars(
56 0x4a,0x13,0x1c,0x81,0x07,0x03,0x58,0x45,
57 0x5c,0x57,0x28,0xf2,0x0e,0x95,0x45,0x2f
58 );
59
60 /**
61 * strongSwan specific vendor ID without version, MD5("strongSwan")
62 */
63 static chunk_t strongswan_vid = chunk_from_chars(
64 0x88,0x2f,0xe5,0x6d,0x6f,0xd2,0x0d,0xbc,
65 0x22,0x51,0x61,0x3b,0x2e,0xbe,0x5b,0xeb
66 );
67
68 /**
69 * Add a vendor ID to message
70 */
71 static void add_vendor_id(private_ike_vendor_v1_t *this, message_t *message,
72 chunk_t vid)
73 {
74 vendor_id_payload_t *vid_payload;
75
76 vid_payload = vendor_id_payload_create_data(VENDOR_ID_V1, chunk_clone(vid));
77 message->add_payload(message, &vid_payload->payload_interface);
78 }
79
80 METHOD(task_t, build, status_t,
81 private_ike_vendor_v1_t *this, message_t *message)
82 {
83
84 if (lib->settings->get_bool(lib->settings,
85 "charon.send_vendor_id", FALSE))
86 {
87 add_vendor_id(this, message, strongswan_vid);
88 }
89
90 add_vendor_id(this, message, xauth6_vid);
91 add_vendor_id(this, message, natt_vid);
92
93 return this->initiator ? NEED_MORE : SUCCESS;
94 }
95
96 METHOD(task_t, process, status_t,
97 private_ike_vendor_v1_t *this, message_t *message)
98 {
99 enumerator_t *enumerator;
100 payload_t *payload;
101
102 enumerator = message->create_payload_enumerator(message);
103 while (enumerator->enumerate(enumerator, &payload))
104 {
105 if (payload->get_type(payload) == VENDOR_ID_V1)
106 {
107 vendor_id_payload_t *vid;
108 chunk_t data;
109
110 vid = (vendor_id_payload_t*)payload;
111 data = vid->get_data(vid);
112
113 if (chunk_equals(data, strongswan_vid))
114 {
115 DBG1(DBG_IKE, "received strongSwan vendor id");
116 this->ike_sa->enable_extension(this->ike_sa, EXT_STRONGSWAN);
117 }
118 else if (chunk_equals(data, xauth6_vid))
119 {
120 DBG1(DBG_IKE, "received XAuth vendor id");
121 this->ike_sa->enable_extension(this->ike_sa, EXT_XAUTH);
122 }
123 else if (chunk_equals(data, natt_vid))
124 {
125 DBG1(DBG_IKE, "received NAT-T vendor id");
126 this->ike_sa->enable_extension(this->ike_sa, EXT_NATT);
127 }
128 else
129 {
130 DBG1(DBG_ENC, "received unknown vendor id: %#B", &data);
131 }
132 }
133 }
134 enumerator->destroy(enumerator);
135
136 return this->initiator ? SUCCESS : NEED_MORE;
137 }
138
139 METHOD(task_t, migrate, void,
140 private_ike_vendor_v1_t *this, ike_sa_t *ike_sa)
141 {
142 this->ike_sa = ike_sa;
143 }
144
145 METHOD(task_t, get_type, task_type_t,
146 private_ike_vendor_v1_t *this)
147 {
148 return TASK_VENDOR_V1;
149 }
150
151 METHOD(task_t, destroy, void,
152 private_ike_vendor_v1_t *this)
153 {
154 free(this);
155 }
156
157 /**
158 * See header
159 */
160 ike_vendor_v1_t *ike_vendor_v1_create(ike_sa_t *ike_sa, bool initiator)
161 {
162 private_ike_vendor_v1_t *this;
163
164 INIT(this,
165 .public = {
166 .task = {
167 .build = _build,
168 .process = _process,
169 .migrate = _migrate,
170 .get_type = _get_type,
171 .destroy = _destroy,
172 },
173 },
174 .initiator = initiator,
175 .ike_sa = ike_sa,
176 );
177
178 return &this->public;
179 }