4fae6cde2ff5b7b8068c3eba3b1eddb6e78fb44a
[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 <hydra.h>
19 #include <daemon.h>
20 #include <processing/jobs/acquire_job.h>
21 #include <processing/jobs/delete_child_sa_job.h>
22 #include <processing/jobs/migrate_job.h>
23 #include <processing/jobs/rekey_child_sa_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 METHOD(kernel_listener_t, acquire, bool,
41 private_kernel_handler_t *this, u_int32_t reqid,
42 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
43 {
44 job_t *job;
45 if (src_ts && dst_ts)
46 {
47 DBG1(DBG_KNL, "creating acquire job for policy %R === %R "
48 "with reqid {%u}", src_ts, dst_ts, reqid);
49 }
50 else
51 {
52 DBG1(DBG_KNL, "creating acquire job for policy with reqid {%u}", reqid);
53 }
54 job = (job_t*)acquire_job_create(reqid, src_ts, dst_ts);
55 hydra->processor->queue_job(hydra->processor, job);
56 return TRUE;
57 }
58
59 METHOD(kernel_listener_t, expire, bool,
60 private_kernel_handler_t *this, u_int32_t reqid, protocol_id_t protocol,
61 u_int32_t spi, bool hard)
62 {
63 job_t *job;
64 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA with SPI %.8x "
65 "and reqid {%u}", hard ? "delete" : "rekey",
66 protocol_id_names, protocol, ntohl(spi), reqid);
67 if (hard)
68 {
69 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
70 }
71 else
72 {
73 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
74 }
75 hydra->processor->queue_job(hydra->processor, job);
76 return TRUE;
77 }
78
79 METHOD(kernel_listener_t, mapping, bool,
80 private_kernel_handler_t *this, u_int32_t reqid, u_int32_t spi,
81 host_t *remote)
82 {
83 job_t *job;
84 DBG1(DBG_KNL, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
85 "reqid {%u} changed, queuing update job", ntohl(spi), reqid);
86 job = (job_t*)update_sa_job_create(reqid, remote);
87 hydra->processor->queue_job(hydra->processor, job);
88 return TRUE;
89 }
90
91 METHOD(kernel_listener_t, migrate, bool,
92 private_kernel_handler_t *this, u_int32_t reqid,
93 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
94 policy_dir_t direction, host_t *local, host_t *remote)
95 {
96 job_t *job;
97 DBG1(DBG_KNL, "creating migrate job for policy %R === %R %N with "
98 "reqid {%u}", src_ts, dst_ts, policy_dir_names, direction,
99 reqid, local);
100 job = (job_t*)migrate_job_create(reqid, src_ts, dst_ts, direction, local,
101 remote);
102 hydra->processor->queue_job(hydra->processor, job);
103 return TRUE;
104 }
105
106 METHOD(kernel_handler_t, destroy, void,
107 private_kernel_handler_t *this)
108 {
109 charon->kernel_interface->remove_listener(charon->kernel_interface,
110 &this->public.listener);
111 free(this);
112 }
113
114 kernel_handler_t *kernel_handler_create()
115 {
116 private_kernel_handler_t *this;
117
118 INIT(this,
119 .public = {
120 .listener = {
121 .acquire = _acquire,
122 .expire = _expire,
123 .mapping = _mapping,
124 .migrate = _migrate,
125 },
126 .destroy = _destroy,
127 },
128 );
129
130 charon->kernel_interface->add_listener(charon->kernel_interface,
131 &this->public.listener);
132
133 return &this->public;
134 }
135