]>
Commit | Line | Data |
---|---|---|
09ae31f1 TB |
1 | /* |
2 | * Copyright (C) 2010 Tobias Brunner | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
09ae31f1 TB |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
15 | */ | |
16 | ||
17 | #include "kernel_handler.h" | |
18 | ||
19 | #include <daemon.h> | |
81f6ec27 | 20 | #include <processing/jobs/acquire_job.h> |
a22853b3 | 21 | #include <processing/jobs/delete_child_sa_job.h> |
4e258e63 TB |
22 | #include <processing/jobs/migrate_job.h> |
23 | #include <processing/jobs/rekey_child_sa_job.h> | |
ba26508d | 24 | #include <processing/jobs/roam_job.h> |
01563352 | 25 | #include <processing/jobs/update_sa_job.h> |
09ae31f1 TB |
26 | |
27 | typedef struct private_kernel_handler_t private_kernel_handler_t; | |
28 | ||
29 | /** | |
30 | * Private data of a kernel_handler_t object. | |
31 | */ | |
32 | struct private_kernel_handler_t { | |
33 | ||
34 | /** | |
35 | * Public part of kernel_handler_t object. | |
36 | */ | |
37 | kernel_handler_t public; | |
09ae31f1 TB |
38 | }; |
39 | ||
9f166d9a TB |
40 | /** |
41 | * convert an IP protocol identifier to the IKEv2 specific protocol identifier. | |
42 | */ | |
b12c53ce | 43 | static inline protocol_id_t proto_ip2ike(uint8_t protocol) |
9f166d9a TB |
44 | { |
45 | switch (protocol) | |
46 | { | |
47 | case IPPROTO_ESP: | |
48 | return PROTO_ESP; | |
49 | case IPPROTO_AH: | |
50 | return PROTO_AH; | |
51 | default: | |
52 | return protocol; | |
53 | } | |
54 | } | |
55 | ||
81f6ec27 | 56 | METHOD(kernel_listener_t, acquire, bool, |
3b699c72 | 57 | private_kernel_handler_t *this, uint32_t reqid, kernel_acquire_data_t *data) |
81f6ec27 | 58 | { |
81c36282 TB |
59 | char buf[BUF_LEN] = ""; |
60 | ||
61 | if (data->label) | |
62 | { | |
63 | snprintf(buf, sizeof(buf), ", label {%s}", | |
64 | data->label->get_string(data->label)); | |
65 | } | |
3b699c72 | 66 | if (data->src && data->dst) |
81f6ec27 | 67 | { |
44e6aa4f | 68 | DBG1(DBG_KNL, "creating acquire job for policy %R === %R with " |
81c36282 | 69 | "reqid {%u}%s", data->src, data->dst, reqid, buf); |
81f6ec27 TB |
70 | } |
71 | else | |
72 | { | |
81c36282 TB |
73 | DBG1(DBG_KNL, "creating acquire job for policy with reqid {%u}%s", |
74 | reqid, buf); | |
81f6ec27 | 75 | } |
44e6aa4f | 76 | lib->processor->queue_job(lib->processor, |
3b699c72 | 77 | (job_t*)acquire_job_create(reqid, data)); |
81f6ec27 TB |
78 | return TRUE; |
79 | } | |
80 | ||
a22853b3 | 81 | METHOD(kernel_listener_t, expire, bool, |
b12c53ce | 82 | private_kernel_handler_t *this, uint8_t protocol, uint32_t spi, |
f81a9497 | 83 | host_t *dst, bool hard) |
a22853b3 | 84 | { |
9f166d9a | 85 | protocol_id_t proto = proto_ip2ike(protocol); |
44e6aa4f | 86 | |
f81a9497 MW |
87 | DBG1(DBG_KNL, "creating %s job for CHILD_SA %N/0x%08x/%H", |
88 | hard ? "delete" : "rekey", protocol_id_names, proto, ntohl(spi), dst); | |
44e6aa4f | 89 | |
a22853b3 TB |
90 | if (hard) |
91 | { | |
44e6aa4f | 92 | lib->processor->queue_job(lib->processor, |
f81a9497 | 93 | (job_t*)delete_child_sa_job_create(proto, spi, dst, hard)); |
a22853b3 TB |
94 | } |
95 | else | |
96 | { | |
44e6aa4f | 97 | lib->processor->queue_job(lib->processor, |
f81a9497 | 98 | (job_t*)rekey_child_sa_job_create(proto, spi, dst)); |
a22853b3 | 99 | } |
a22853b3 TB |
100 | return TRUE; |
101 | } | |
102 | ||
01563352 | 103 | METHOD(kernel_listener_t, mapping, bool, |
b12c53ce | 104 | private_kernel_handler_t *this, uint8_t protocol, uint32_t spi, |
b125839a | 105 | host_t *dst, host_t *remote) |
01563352 | 106 | { |
b125839a MW |
107 | protocol_id_t proto = proto_ip2ike(protocol); |
108 | ||
f30be6a9 TB |
109 | DBG1(DBG_KNL, "NAT mappings of CHILD_SA %N/0x%08x/%H changed to %#H, " |
110 | "queuing update job", protocol_id_names, proto, ntohl(spi), dst, | |
111 | remote); | |
44e6aa4f MW |
112 | |
113 | lib->processor->queue_job(lib->processor, | |
b125839a | 114 | (job_t*)update_sa_job_create(proto, spi, dst, remote)); |
01563352 TB |
115 | return TRUE; |
116 | } | |
117 | ||
4e258e63 | 118 | METHOD(kernel_listener_t, migrate, bool, |
b12c53ce | 119 | private_kernel_handler_t *this, uint32_t reqid, |
44e6aa4f MW |
120 | traffic_selector_t *src_ts, traffic_selector_t *dst_ts, |
121 | policy_dir_t direction, host_t *local, host_t *remote) | |
4e258e63 | 122 | { |
44e6aa4f MW |
123 | DBG1(DBG_KNL, "creating migrate job for policy %R === %R %N with reqid {%u}", |
124 | src_ts, dst_ts, policy_dir_names, direction, reqid, local); | |
125 | ||
126 | lib->processor->queue_job(lib->processor, | |
127 | (job_t*)migrate_job_create(reqid, src_ts, dst_ts, | |
128 | direction, local, remote)); | |
4e258e63 TB |
129 | return TRUE; |
130 | } | |
131 | ||
ba26508d | 132 | METHOD(kernel_listener_t, roam, bool, |
44e6aa4f | 133 | private_kernel_handler_t *this, bool address) |
ba26508d | 134 | { |
44e6aa4f MW |
135 | DBG2(DBG_KNL, "creating roam job %s", |
136 | address ? "due to address/link change" : "due to route change"); | |
137 | ||
138 | lib->processor->queue_job(lib->processor, (job_t*)roam_job_create(address)); | |
ba26508d TB |
139 | return TRUE; |
140 | } | |
141 | ||
09ae31f1 | 142 | METHOD(kernel_handler_t, destroy, void, |
44e6aa4f | 143 | private_kernel_handler_t *this) |
09ae31f1 | 144 | { |
8394ea2a | 145 | charon->kernel->remove_listener(charon->kernel, &this->public.listener); |
09ae31f1 TB |
146 | free(this); |
147 | } | |
148 | ||
149 | kernel_handler_t *kernel_handler_create() | |
150 | { | |
151 | private_kernel_handler_t *this; | |
152 | ||
153 | INIT(this, | |
154 | .public = { | |
155 | .listener = { | |
81f6ec27 | 156 | .acquire = _acquire, |
a22853b3 | 157 | .expire = _expire, |
01563352 | 158 | .mapping = _mapping, |
4e258e63 | 159 | .migrate = _migrate, |
ba26508d | 160 | .roam = _roam, |
09ae31f1 TB |
161 | }, |
162 | .destroy = _destroy, | |
163 | }, | |
164 | ); | |
165 | ||
8394ea2a | 166 | charon->kernel->add_listener(charon->kernel, &this->public.listener); |
09ae31f1 TB |
167 | |
168 | return &this->public; | |
169 | } |