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