libhydra: Move kernel interface to libcharon
[strongswan.git] / src / libcharon / kernel / kernel_handler.c
1 /*
2 * Copyright (C) 2010 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_handler.h"
17
18 #include <daemon.h>
19 #include <processing/jobs/acquire_job.h>
20 #include <processing/jobs/delete_child_sa_job.h>
21 #include <processing/jobs/migrate_job.h>
22 #include <processing/jobs/rekey_child_sa_job.h>
23 #include <processing/jobs/roam_job.h>
24 #include <processing/jobs/update_sa_job.h>
25
26 typedef struct private_kernel_handler_t private_kernel_handler_t;
27
28 /**
29 * Private data of a kernel_handler_t object.
30 */
31 struct private_kernel_handler_t {
32
33 /**
34 * Public part of kernel_handler_t object.
35 */
36 kernel_handler_t public;
37 };
38
39 /**
40 * convert an IP protocol identifier to the IKEv2 specific protocol identifier.
41 */
42 static inline protocol_id_t proto_ip2ike(u_int8_t protocol)
43 {
44 switch (protocol)
45 {
46 case IPPROTO_ESP:
47 return PROTO_ESP;
48 case IPPROTO_AH:
49 return PROTO_AH;
50 default:
51 return protocol;
52 }
53 }
54
55 METHOD(kernel_listener_t, acquire, bool,
56 private_kernel_handler_t *this, u_int32_t reqid,
57 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
58 {
59 if (src_ts && dst_ts)
60 {
61 DBG1(DBG_KNL, "creating acquire job for policy %R === %R with "
62 "reqid {%u}", src_ts, dst_ts, reqid);
63 }
64 else
65 {
66 DBG1(DBG_KNL, "creating acquire job for policy with reqid {%u}", reqid);
67 }
68 lib->processor->queue_job(lib->processor,
69 (job_t*)acquire_job_create(reqid, src_ts, dst_ts));
70 return TRUE;
71 }
72
73 METHOD(kernel_listener_t, expire, bool,
74 private_kernel_handler_t *this, u_int8_t protocol, u_int32_t spi,
75 host_t *dst, bool hard)
76 {
77 protocol_id_t proto = proto_ip2ike(protocol);
78
79 DBG1(DBG_KNL, "creating %s job for CHILD_SA %N/0x%08x/%H",
80 hard ? "delete" : "rekey", protocol_id_names, proto, ntohl(spi), dst);
81
82 if (hard)
83 {
84 lib->processor->queue_job(lib->processor,
85 (job_t*)delete_child_sa_job_create(proto, spi, dst, hard));
86 }
87 else
88 {
89 lib->processor->queue_job(lib->processor,
90 (job_t*)rekey_child_sa_job_create(proto, spi, dst));
91 }
92 return TRUE;
93 }
94
95 METHOD(kernel_listener_t, mapping, bool,
96 private_kernel_handler_t *this, u_int8_t protocol, u_int32_t spi,
97 host_t *dst, host_t *remote)
98 {
99 protocol_id_t proto = proto_ip2ike(protocol);
100
101 DBG1(DBG_KNL, "NAT mappings of CHILD_SA %N/0x%08x/%H changed to %#H, "
102 "queuing update job", protocol_id_names, proto, ntohl(spi), dst,
103 remote);
104
105 lib->processor->queue_job(lib->processor,
106 (job_t*)update_sa_job_create(proto, spi, dst, remote));
107 return TRUE;
108 }
109
110 METHOD(kernel_listener_t, migrate, bool,
111 private_kernel_handler_t *this, u_int32_t reqid,
112 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
113 policy_dir_t direction, host_t *local, host_t *remote)
114 {
115 DBG1(DBG_KNL, "creating migrate job for policy %R === %R %N with reqid {%u}",
116 src_ts, dst_ts, policy_dir_names, direction, reqid, local);
117
118 lib->processor->queue_job(lib->processor,
119 (job_t*)migrate_job_create(reqid, src_ts, dst_ts,
120 direction, local, remote));
121 return TRUE;
122 }
123
124 METHOD(kernel_listener_t, roam, bool,
125 private_kernel_handler_t *this, bool address)
126 {
127 DBG2(DBG_KNL, "creating roam job %s",
128 address ? "due to address/link change" : "due to route change");
129
130 lib->processor->queue_job(lib->processor, (job_t*)roam_job_create(address));
131 return TRUE;
132 }
133
134 METHOD(kernel_handler_t, destroy, void,
135 private_kernel_handler_t *this)
136 {
137 charon->kernel->remove_listener(charon->kernel, &this->public.listener);
138 free(this);
139 }
140
141 kernel_handler_t *kernel_handler_create()
142 {
143 private_kernel_handler_t *this;
144
145 INIT(this,
146 .public = {
147 .listener = {
148 .acquire = _acquire,
149 .expire = _expire,
150 .mapping = _mapping,
151 .migrate = _migrate,
152 .roam = _roam,
153 },
154 .destroy = _destroy,
155 },
156 );
157
158 charon->kernel->add_listener(charon->kernel, &this->public.listener);
159
160 return &this->public;
161 }