reimplemented dbus plugin for NetworkManager 0.7, renamed to nm
[strongswan.git] / src / charon / plugins / nm / nm_service.c
1 /*
2 * Copyright (C) 2008 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 * $Id$
16 */
17
18 #include <nm-setting-vpn.h>
19 #include <nm-setting-vpn-properties.h>
20 #include "nm_service.h"
21
22 #include <daemon.h>
23 #include <utils/host.h>
24 #include <utils/identification.h>
25 #include <config/peer_cfg.h>
26
27 #include <stdio.h>
28
29 #define CONFIG_NAME "NetworkManager"
30
31 G_DEFINE_TYPE(NMStrongswanPlugin, nm_strongswan_plugin, NM_TYPE_VPN_PLUGIN)
32
33 /**
34 * Private data of NMStrongswanPlugin
35 */
36 typedef struct {
37 bus_listener_t listener;
38 ike_sa_t *ike_sa;
39 NMVPNPlugin *plugin;
40 } NMStrongswanPluginPrivate;
41
42 #define NM_STRONGSWAN_PLUGIN_GET_PRIVATE(o) \
43 (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
44 NM_TYPE_STRONGSWAN_PLUGIN, NMStrongswanPluginPrivate))
45
46 /**
47 * convert a traffic selector address range to subnet and its mask.
48 */
49 static u_int ts2subnet(traffic_selector_t* ts, u_int8_t *mask)
50 {
51 /* there is no way to do this cleanly, as the address range may
52 * be anything else but a subnet. We use from_addr as subnet
53 * and try to calculate a usable subnet mask.
54 */
55 int byte, bit, net;
56 bool found = FALSE;
57 chunk_t from, to;
58 size_t size = (ts->get_type(ts) == TS_IPV4_ADDR_RANGE) ? 4 : 16;
59
60 from = ts->get_from_address(ts);
61 to = ts->get_to_address(ts);
62
63 *mask = (size * 8);
64 /* go trough all bits of the addresses, beginning in the front.
65 * as long as they are equal, the subnet gets larger
66 */
67 for (byte = 0; byte < size; byte++)
68 {
69 for (bit = 7; bit >= 0; bit--)
70 {
71 if ((1<<bit & from.ptr[byte]) != (1<<bit & to.ptr[byte]))
72 {
73 *mask = ((7 - bit) + (byte * 8));
74 found = TRUE;
75 break;
76 }
77 }
78 if (found)
79 {
80 break;
81 }
82 }
83 net = *(u_int32_t*)from.ptr;
84 chunk_free(&from);
85 chunk_free(&to);
86 return net;
87 }
88
89 /**
90 * signal IPv4 config to NM, set connection as established
91 */
92 static void signal_ipv4_config(NMVPNPlugin *plugin, child_sa_t *child_sa)
93 {
94 linked_list_t *list;
95 traffic_selector_t *ts = NULL;
96 enumerator_t *enumerator;
97
98 list = child_sa->get_traffic_selectors(child_sa, FALSE);
99 enumerator = list->create_enumerator(list);
100 while (enumerator->enumerate(enumerator, &ts))
101 {
102 GValue *val;
103 GHashTable *config;
104 u_int8_t mask;
105
106 config = g_hash_table_new(g_str_hash, g_str_equal);
107
108 val = g_slice_new0(GValue);
109 g_value_init(val, G_TYPE_UINT);
110 g_value_set_uint(val, ts2subnet(ts, &mask));
111 g_hash_table_insert(config, NM_VPN_PLUGIN_IP4_CONFIG_ADDRESS, val);
112
113 val = g_slice_new0(GValue);
114 g_value_init(val, G_TYPE_UINT);
115 g_value_set_uint(val, mask);
116 g_hash_table_insert(config, NM_VPN_PLUGIN_IP4_CONFIG_PREFIX, val);
117
118 nm_vpn_plugin_set_ip4_config(plugin, config);
119 }
120 enumerator->destroy(enumerator);
121 }
122
123 /**
124 * Bus listen function to wait for SA establishing
125 */
126 bool listen_bus(bus_listener_t *listener, signal_t signal, level_t level,
127 int thread, ike_sa_t *ike_sa, void *data,
128 char* format, va_list args)
129 {
130 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
131
132 if (private->ike_sa == ike_sa)
133 {
134 switch (signal)
135 {
136 case CHD_UP_SUCCESS:
137 if (data)
138 {
139 signal_ipv4_config(private->plugin, (child_sa_t*)data);
140 return FALSE;
141 }
142 /* FALL */
143 case IKE_UP_FAILED:
144 case CHD_UP_FAILED:
145 nm_vpn_plugin_failure(private->plugin,
146 NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED);
147 /* TODO: NM does not react on this failure!? So additionaly
148 * reset state */
149 nm_vpn_plugin_set_state(private->plugin,
150 NM_VPN_SERVICE_STATE_STOPPED);
151 return FALSE;
152 default:
153 break;
154 }
155 }
156 return TRUE;
157 }
158
159 /**
160 * Read a string from a hash table using a given key
161 */
162 static char* get_str(GHashTable *hash, char *key)
163 {
164 GValue *value;
165
166 value = g_hash_table_lookup(hash, key);
167 if (G_VALUE_TYPE (value) == G_TYPE_STRING)
168 {
169 return (char*)g_value_get_string(value);
170 }
171 return NULL;
172 }
173
174 /**
175 * Connect function called from NM via DBUS
176 */
177 static gboolean connect_(NMVPNPlugin *plugin, NMConnection *connection,
178 GError **err)
179 {
180 NMSettingVPNProperties *properties;
181 identification_t *user = NULL;
182 char *address, *str;
183 ike_cfg_t *ike_cfg;
184 peer_cfg_t *peer_cfg;
185 child_cfg_t *child_cfg;
186 traffic_selector_t *ts;
187 ike_sa_t *ike_sa;
188
189 /**
190 * Read parameters
191 */
192 properties = NM_SETTING_VPN_PROPERTIES(
193 nm_connection_get_setting(connection, NM_TYPE_SETTING_VPN_PROPERTIES));
194
195 if (!properties)
196 {
197 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
198 "%s", "Invalid arguments.");
199 return FALSE;
200 }
201
202 DBG2(DBG_CFG, "received NetworkManager connection: %s",
203 nm_setting_to_string(NM_SETTING(properties)));
204
205 str = get_str(properties->data, "user");
206 if (str)
207 {
208 user = identification_create_from_string(str);
209 }
210 if (!user)
211 {
212 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
213 "Username '%s' invalid.", str);
214 return FALSE;
215 }
216 address = get_str(properties->data, "address");
217 if (!address || !*address)
218 {
219 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
220 "Gateway address missing.");
221 return FALSE;
222 }
223
224 /**
225 * Set up configurations
226 */
227 ike_cfg = ike_cfg_create(TRUE, TRUE, "0.0.0.0", address);
228 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
229 peer_cfg = peer_cfg_create(CONFIG_NAME, 2, ike_cfg, user,
230 identification_create_from_encoding(ID_ANY, chunk_empty),
231 CERT_SEND_IF_ASKED, UNIQUE_REPLACE, CONF_AUTH_PUBKEY,
232 0, 0, 1, /* EAP method, vendor, keyingtries */
233 18000, 0, /* rekey 5h, reauth none */
234 600, 600, /* jitter, over 10min */
235 TRUE, 0, /* mobike, DPD */
236 host_create_from_string("0.0.0.0", 0), /* virtual ip */
237 NULL, FALSE, NULL, NULL); /* pool, mediation */
238 child_cfg = child_cfg_create(CONFIG_NAME,
239 3600, 3000, /* lifetime 1h, rekey 50min */
240 300, /* jitter 5min */
241 NULL, TRUE, MODE_TUNNEL, /* updown, hostaccess */
242 ACTION_NONE, ACTION_NONE, FALSE); /* ipcomp */
243 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
244 ts = traffic_selector_create_dynamic(0, 0, 65535);
245 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
246 ts = traffic_selector_create_from_string(0, TS_IPV4_ADDR_RANGE,
247 "0.0.0.0", 0,
248 "255.255.255.255", 65535);
249 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
250 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
251
252 /**
253 * Start to initiate
254 */
255 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
256 peer_cfg);
257 if (!ike_sa->get_peer_cfg(ike_sa))
258 {
259 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
260 }
261 else
262 {
263 peer_cfg->destroy(peer_cfg);
264 }
265 if (ike_sa->initiate(ike_sa, child_cfg) != SUCCESS)
266 {
267 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
268
269 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
270 "Initiating failed.");
271 return FALSE;
272 }
273
274 /**
275 * Register listener
276 */
277 NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin)->ike_sa = ike_sa;
278 charon->bus->add_listener(charon->bus,
279 &NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin)->listener);
280 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
281 return TRUE;
282 }
283
284 /**
285 * NeedSecrets called from NM via DBUS
286 */
287 static gboolean need_secrets(NMVPNPlugin *plugin, NMConnection *connection,
288 char **setting_name, GError **error)
289 {
290 return FALSE;
291 }
292
293 /**
294 * Disconnect called from NM via DBUS
295 */
296 static gboolean disconnect(NMVPNPlugin *plugin, GError **err)
297 {
298 enumerator_t *enumerator;
299 ike_sa_t *ike_sa;
300 u_int id;
301
302 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
303 while (enumerator->enumerate(enumerator, &ike_sa))
304 {
305 if (streq(CONFIG_NAME, ike_sa->get_name(ike_sa)))
306 {
307 id = ike_sa->get_unique_id(ike_sa);
308 enumerator->destroy(enumerator);
309 charon->controller->terminate_ike(charon->controller, id,
310 controller_cb_empty, NULL);
311 return TRUE;
312 }
313 }
314 enumerator->destroy(enumerator);
315 return FALSE;
316 }
317
318 /**
319 * Initializer
320 */
321 static void nm_strongswan_plugin_init(NMStrongswanPlugin *plugin)
322 {
323 NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin)->plugin = NM_VPN_PLUGIN(plugin);
324 NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin)->listener.signal = listen_bus;
325 }
326
327 /**
328 * Class constructor
329 */
330 static void nm_strongswan_plugin_class_init(
331 NMStrongswanPluginClass *strongswan_class)
332 {
333 NMVPNPluginClass *parent_class = NM_VPN_PLUGIN_CLASS(strongswan_class);
334
335 g_type_class_add_private(G_OBJECT_CLASS(strongswan_class),
336 sizeof(NMStrongswanPluginPrivate));
337 parent_class->connect = connect_;
338 parent_class->need_secrets = need_secrets;
339 parent_class->disconnect = disconnect;
340 }
341
342 /**
343 * Object constructor
344 */
345 NMStrongswanPlugin *nm_strongswan_plugin_new(void)
346 {
347 return (NMStrongswanPlugin *)g_object_new (
348 NM_TYPE_STRONGSWAN_PLUGIN, NM_VPN_PLUGIN_DBUS_SERVICE_NAME,
349 NM_DBUS_SERVICE_STRONGSWAN, NULL);
350 }
351