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