9ac77084ff00b487981494b253bee20e94a05d2e
[strongswan.git] / src / charon / kernel / kernel_interface.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
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 "kernel_interface.h"
17
18 #include <pthread.h>
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <utils/mutex.h>
23
24 typedef struct private_kernel_interface_t private_kernel_interface_t;
25
26 /**
27 * Private data of a kernel_interface_t object.
28 */
29 struct private_kernel_interface_t {
30
31 /**
32 * Public part of kernel_interface_t object.
33 */
34 kernel_interface_t public;
35
36 /**
37 * list of registered ipsec kernel interfaces
38 */
39 linked_list_t *ipsec_interfaces;
40
41 /**
42 * list of registered network kernel interfaces
43 */
44 linked_list_t *net_interfaces;
45
46 /**
47 * ipsec interface
48 */
49 kernel_ipsec_t *ipsec;
50
51 /**
52 * network interface
53 */
54 kernel_net_t *net;
55
56 /**
57 * locking mutex
58 */
59 mutex_t *mutex;
60 };
61
62 /**
63 * Implementation of kernel_interface_t.get_spi
64 */
65 static status_t get_spi(private_kernel_interface_t *this, host_t *src, host_t *dst,
66 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi)
67 {
68 return this->ipsec->get_spi(this->ipsec, src, dst, protocol, reqid, spi);
69 }
70
71 /**
72 * Implementation of kernel_interface_t.get_cpi
73 */
74 static status_t get_cpi(private_kernel_interface_t *this, host_t *src, host_t *dst,
75 u_int32_t reqid, u_int16_t *cpi)
76 {
77 return this->ipsec->get_cpi(this->ipsec, src, dst, reqid, cpi);
78 }
79
80 /**
81 * Implementation of kernel_interface_t.add_sa
82 */
83 static status_t add_sa(private_kernel_interface_t *this, host_t *src, host_t *dst,
84 u_int32_t spi, protocol_id_t protocol, u_int32_t reqid,
85 u_int64_t expire_soft, u_int64_t expire_hard,
86 u_int16_t enc_alg, chunk_t enc_key,
87 u_int16_t int_alg, chunk_t int_key,
88 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi, bool encap,
89 bool inbound)
90 {
91 return this->ipsec->add_sa(this->ipsec, src, dst, spi, protocol, reqid,
92 expire_soft, expire_hard, enc_alg, enc_key, int_alg, int_key,
93 mode, ipcomp, cpi, encap, inbound);
94 }
95
96 /**
97 * Implementation of kernel_interface_t.update_sa
98 */
99 static status_t update_sa(private_kernel_interface_t *this, u_int32_t spi,
100 protocol_id_t protocol, u_int16_t cpi, host_t *src, host_t *dst,
101 host_t *new_src, host_t *new_dst, bool encap, bool new_encap)
102 {
103 return this->ipsec->update_sa(this->ipsec, spi, protocol, cpi, src, dst,
104 new_src, new_dst, encap, new_encap);
105 }
106
107 /**
108 * Implementation of kernel_interface_t.del_sa
109 */
110 static status_t del_sa(private_kernel_interface_t *this, host_t *src, host_t *dst,
111 u_int32_t spi, protocol_id_t protocol, u_int16_t cpi)
112 {
113 return this->ipsec->del_sa(this->ipsec, src, dst, spi, protocol, cpi);
114 }
115
116 /**
117 * Implementation of kernel_interface_t.add_policy
118 */
119 static status_t add_policy(private_kernel_interface_t *this, host_t *src, host_t *dst,
120 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
121 policy_dir_t direction, u_int32_t spi, protocol_id_t protocol,
122 u_int32_t reqid, ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
123 bool routed)
124 {
125 return this->ipsec->add_policy(this->ipsec, src, dst, src_ts, dst_ts,
126 direction, spi, protocol, reqid, mode, ipcomp, cpi, routed);
127 }
128
129 /**
130 * Implementation of kernel_interface_t.query_policy
131 */
132 static status_t query_policy(private_kernel_interface_t *this,
133 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
134 policy_dir_t direction, u_int32_t *use_time)
135 {
136 return this->ipsec->query_policy(this->ipsec, src_ts, dst_ts, direction, use_time);
137 }
138
139 /**
140 * Implementation of kernel_interface_t.del_policy
141 */
142 static status_t del_policy(private_kernel_interface_t *this,
143 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
144 policy_dir_t direction, bool unrouted)
145 {
146 return this->ipsec->del_policy(this->ipsec, src_ts, dst_ts, direction, unrouted);
147 }
148
149 /**
150 * Implementation of kernel_interface_t.get_source_addr
151 */
152 static host_t *get_source_addr(private_kernel_interface_t *this,
153 host_t *dest, host_t *src)
154 {
155 return this->net->get_source_addr(this->net, dest, src);
156 }
157
158 /**
159 * Implementation of kernel_interface_t.get_nexthop
160 */
161 static host_t *get_nexthop(private_kernel_interface_t *this, host_t *dest)
162 {
163 return this->net->get_nexthop(this->net, dest);
164 }
165
166 /**
167 * Implementation of kernel_interface_t.get_interface
168 */
169 static char* get_interface(private_kernel_interface_t *this, host_t *host)
170 {
171 return this->net->get_interface(this->net, host);
172 }
173
174 /**
175 * Implementation of kernel_interface_t.create_address_enumerator
176 */
177 static enumerator_t *create_address_enumerator(private_kernel_interface_t *this,
178 bool include_down_ifaces, bool include_virtual_ips)
179 {
180 return this->net->create_address_enumerator(this->net, include_down_ifaces,
181 include_virtual_ips);
182 }
183
184 /**
185 * Implementation of kernel_interface_t.add_ip
186 */
187 static status_t add_ip(private_kernel_interface_t *this, host_t *virtual_ip,
188 host_t *iface_ip)
189 {
190 return this->net->add_ip(this->net, virtual_ip, iface_ip);
191 }
192
193 /**
194 * Implementation of kernel_interface_t.del_ip
195 */
196 static status_t del_ip(private_kernel_interface_t *this, host_t *virtual_ip)
197 {
198 return this->net->del_ip(this->net, virtual_ip);
199 }
200
201 /**
202 * Implementation of kernel_interface_t.add_route
203 */
204 static status_t add_route(private_kernel_interface_t *this, chunk_t dst_net,
205 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
206 {
207 return this->net->add_route(this->net, dst_net, prefixlen, gateway, src_ip,
208 if_name);
209 }
210
211 /**
212 * Implementation of kernel_interface_t.del_route
213 */
214 static status_t del_route(private_kernel_interface_t *this, chunk_t dst_net,
215 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
216 {
217 return this->net->del_route(this->net, dst_net, prefixlen, gateway, src_ip,
218 if_name);
219 }
220
221
222 /**
223 * Implementation of kernel_interface_t.get_address_by_ts
224 */
225 static status_t get_address_by_ts(private_kernel_interface_t *this,
226 traffic_selector_t *ts, host_t **ip)
227 {
228 enumerator_t *addrs;
229 host_t *host;
230 int family;
231 bool found = FALSE;
232
233 DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
234
235 /* if we have a family which includes localhost, we do not
236 * search for an IP, we use the default */
237 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
238
239 if (family == AF_INET)
240 {
241 host = host_create_from_string("127.0.0.1", 0);
242 }
243 else
244 {
245 host = host_create_from_string("::1", 0);
246 }
247
248 if (ts->includes(ts, host))
249 {
250 *ip = host_create_any(family);
251 host->destroy(host);
252 DBG2(DBG_KNL, "using host %H", *ip);
253 return SUCCESS;
254 }
255 host->destroy(host);
256
257 addrs = create_address_enumerator(this, TRUE, TRUE);
258 while (addrs->enumerate(addrs, (void**)&host))
259 {
260 if (ts->includes(ts, host))
261 {
262 found = TRUE;
263 *ip = host->clone(host);
264 break;
265 }
266 }
267 addrs->destroy(addrs);
268
269 if (!found)
270 {
271 DBG1(DBG_KNL, "no local address found in traffic selector %R", ts);
272 return FAILED;
273 }
274
275 DBG2(DBG_KNL, "using host %H", *ip);
276 return SUCCESS;
277 }
278
279
280 /**
281 * Implementation of kernel_interface_t.add_ipsec_interface.
282 */
283 static void add_ipsec_interface(private_kernel_interface_t *this,
284 kernel_ipsec_constructor_t *create)
285 {
286 this->mutex->lock(this->mutex);
287 this->ipsec_interfaces->insert_last(this->ipsec_interfaces, create);
288 this->mutex->unlock(this->mutex);
289 }
290
291 /**
292 * Implementation of kernel_interface_t.remove_ipsec_interface.
293 */
294 static void remove_ipsec_interface(private_kernel_interface_t *this,
295 kernel_ipsec_constructor_t *create)
296 {
297 this->mutex->lock(this->mutex);
298 this->ipsec_interfaces->remove(this->ipsec_interfaces, create, NULL);
299 this->mutex->unlock(this->mutex);
300 }
301
302 /**
303 * Implementation of kernel_interface_t.add_net_interface.
304 */
305 static void add_net_interface(private_kernel_interface_t *this,
306 kernel_net_constructor_t *create)
307 {
308 this->mutex->lock(this->mutex);
309 this->net_interfaces->insert_last(this->net_interfaces, create);
310 this->mutex->unlock(this->mutex);
311 }
312
313 /**
314 * Implementation of kernel_interface_t.remove_net_interface.
315 */
316 static void remove_net_interface(private_kernel_interface_t *this,
317 kernel_net_constructor_t *create)
318 {
319 this->mutex->lock(this->mutex);
320 this->net_interfaces->remove(this->net_interfaces, create, NULL);
321 this->mutex->unlock(this->mutex);
322 }
323
324 /**
325 * Implementation of kernel_interface_t.create_interfaces.
326 */
327 static void create_interfaces(private_kernel_interface_t *this)
328 {
329 kernel_ipsec_constructor_t create_ipsec;
330 kernel_net_constructor_t create_net;
331
332 this->mutex->lock(this->mutex);
333 if (this->ipsec_interfaces->get_first(this->ipsec_interfaces, (void**)&create_ipsec) != SUCCESS)
334 {
335 this->mutex->unlock(this->mutex);
336 charon->kill(charon, "no ipsec kernel interface loaded");
337 }
338
339 if (this->net_interfaces->get_first(this->net_interfaces, (void**)&create_net) != SUCCESS)
340 {
341 this->mutex->unlock(this->mutex);
342 charon->kill(charon, "no network kernel interface loaded");
343 }
344 this->mutex->unlock(this->mutex);
345
346 this->ipsec = create_ipsec();
347 this->net = create_net();
348 }
349
350 /**
351 * Implementation of kernel_interface_t.destroy.
352 */
353 static void destroy(private_kernel_interface_t *this)
354 {
355 DESTROY_IF(this->ipsec);
356 DESTROY_IF(this->net);
357 this->ipsec_interfaces->destroy(this->ipsec_interfaces);
358 this->net_interfaces->destroy(this->net_interfaces);
359 this->mutex->destroy(this->mutex);
360 free(this);
361 }
362
363 /*
364 * Described in header-file
365 */
366 kernel_interface_t *kernel_interface_create()
367 {
368 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
369
370 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
371 this->public.get_cpi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,u_int32_t,u_int16_t*))get_cpi;
372 this->public.add_sa = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t,u_int64_t,u_int64_t,u_int16_t,chunk_t,u_int16_t,chunk_t,ipsec_mode_t,u_int16_t,u_int16_t,bool,bool))add_sa;
373 this->public.update_sa = (status_t(*)(kernel_interface_t*,u_int32_t,protocol_id_t,u_int16_t,host_t*,host_t*,host_t*,host_t*,bool,bool))update_sa;
374 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,host_t*,u_int32_t,protocol_id_t,u_int16_t))del_sa;
375 this->public.add_policy = (status_t(*)(kernel_interface_t*,host_t*,host_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t,protocol_id_t,u_int32_t,ipsec_mode_t,u_int16_t,u_int16_t,bool))add_policy;
376 this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
377 this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,bool))del_policy;
378
379 this->public.get_source_addr = (host_t*(*)(kernel_interface_t*, host_t *dest, host_t *src))get_source_addr;
380 this->public.get_nexthop = (host_t*(*)(kernel_interface_t*, host_t *dest))get_nexthop;
381 this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface;
382 this->public.create_address_enumerator = (enumerator_t*(*)(kernel_interface_t*,bool,bool))create_address_enumerator;
383 this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
384 this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*)) del_ip;
385 this->public.add_route = (status_t(*)(kernel_interface_t*,chunk_t,u_int8_t,host_t*,host_t*,char*)) add_route;
386 this->public.del_route = (status_t(*)(kernel_interface_t*,chunk_t,u_int8_t,host_t*,host_t*,char*)) del_route;
387
388 this->public.get_address_by_ts = (status_t(*)(kernel_interface_t*,traffic_selector_t*,host_t**))get_address_by_ts;
389
390 this->public.add_ipsec_interface = (void(*)(kernel_interface_t*, kernel_ipsec_constructor_t))add_ipsec_interface;
391 this->public.remove_ipsec_interface = (void(*)(kernel_interface_t*, kernel_ipsec_constructor_t))remove_ipsec_interface;
392 this->public.add_net_interface = (void(*)(kernel_interface_t*, kernel_net_constructor_t))add_net_interface;
393 this->public.remove_net_interface = (void(*)(kernel_interface_t*, kernel_net_constructor_t))remove_net_interface;
394
395 this->public.create_interfaces = (void (*)(kernel_interface_t*))create_interfaces;
396 this->public.destroy = (void (*)(kernel_interface_t*))destroy;
397
398 this->ipsec_interfaces = linked_list_create();
399 this->net_interfaces = linked_list_create();
400 this->mutex = mutex_create(MUTEX_RECURSIVE);
401 this->ipsec = NULL;
402 this->net = NULL;
403
404 return &this->public;
405 }