d2ba2e345b8d5de73b37c236822d1e32021b29b7
[strongswan.git] / src / libcharon / plugins / tnc_ifmap / tnc_ifmap_listener.c
1 /*
2 * Copyright (C) 2011-2013 Andreas Steffen
3 * HSR 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 "tnc_ifmap_listener.h"
17 #include "tnc_ifmap_soap.h"
18 #include "tnc_ifmap_renew_session_job.h"
19
20 #include <daemon.h>
21 #include <hydra.h>
22 #include <utils/debug.h>
23
24 #define IFMAP_RENEW_SESSION_INTERVAL 150
25
26 typedef struct private_tnc_ifmap_listener_t private_tnc_ifmap_listener_t;
27
28 /**
29 * Private data of an tnc_ifmap_listener_t object.
30 */
31 struct private_tnc_ifmap_listener_t {
32
33 /**
34 * Public tnc_ifmap_listener_t interface.
35 */
36 tnc_ifmap_listener_t public;
37
38 /**
39 * TNC IF-MAP 2.0 SOAP interface
40 */
41 tnc_ifmap_soap_t *ifmap;
42
43 };
44
45 /**
46 * Publish PEP device-ip metadata
47 */
48 static bool publish_device_ip_addresses(private_tnc_ifmap_listener_t *this)
49 {
50 enumerator_t *enumerator;
51 host_t *host;
52 bool success = TRUE;
53
54 enumerator = hydra->kernel_interface->create_address_enumerator(
55 hydra->kernel_interface, ADDR_TYPE_REGULAR);
56 while (enumerator->enumerate(enumerator, &host))
57 {
58 if (!this->ifmap->publish_device_ip(this->ifmap, host))
59 {
60 success = FALSE;
61 break;
62 }
63 }
64 enumerator->destroy(enumerator);
65
66 return success;
67 }
68
69 /**
70 * Publish all IKE_SA metadata
71 */
72 static bool reload_metadata(private_tnc_ifmap_listener_t *this)
73 {
74 ike_sa_t *ike_sa;
75 enumerator_t *enumerator;
76 bool success = TRUE;
77
78 enumerator = charon->controller->create_ike_sa_enumerator(
79 charon->controller, FALSE);
80 while (enumerator->enumerate(enumerator, &ike_sa))
81 {
82 if (ike_sa->get_state(ike_sa) != IKE_ESTABLISHED)
83 {
84 continue;
85 }
86 if (!this->ifmap->publish_ike_sa(this->ifmap, ike_sa, TRUE) ||
87 !this->ifmap->publish_virtual_ips(this->ifmap, ike_sa, TRUE))
88 {
89 success = FALSE;
90 break;
91 }
92 }
93 enumerator->destroy(enumerator);
94
95 return success;
96 }
97
98 METHOD(listener_t, ike_updown, bool,
99 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, bool up)
100 {
101 if (ike_sa->get_state(ike_sa) != IKE_CONNECTING)
102 {
103 this->ifmap->publish_ike_sa(this->ifmap, ike_sa, up);
104 }
105 return TRUE;
106 }
107
108 METHOD(listener_t, assign_vips, bool,
109 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, bool assign)
110 {
111 this->ifmap->publish_virtual_ips(this->ifmap, ike_sa, assign);
112 return TRUE;
113 }
114
115 METHOD(listener_t, alert, bool,
116 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, alert_t alert,
117 va_list args)
118 {
119 if (alert == ALERT_PEER_AUTH_FAILED)
120 {
121 this->ifmap->publish_enforcement_report(this->ifmap,
122 ike_sa->get_other_host(ike_sa),
123 "block", "authentication failed");
124 }
125 return TRUE;
126 }
127
128 METHOD(tnc_ifmap_listener_t, destroy, void,
129 private_tnc_ifmap_listener_t *this)
130 {
131 if (this->ifmap)
132 {
133 if (this->ifmap->get_session_id(this->ifmap))
134 {
135 this->ifmap->endSession(this->ifmap);
136 }
137 this->ifmap->destroy(this->ifmap);
138 }
139 free(this);
140 }
141
142 /**
143 * See header
144 */
145 tnc_ifmap_listener_t *tnc_ifmap_listener_create(bool reload)
146 {
147 private_tnc_ifmap_listener_t *this;
148 job_t *job;
149 u_int32_t reschedule;
150
151 INIT(this,
152 .public = {
153 .listener = {
154 .ike_updown = _ike_updown,
155 .assign_vips = _assign_vips,
156 .alert = _alert,
157 },
158 .destroy = _destroy,
159 },
160 .ifmap = tnc_ifmap_soap_create(),
161 );
162
163 if (!this->ifmap)
164 {
165 destroy(this);
166 return NULL;
167 }
168 if (!this->ifmap->newSession(this->ifmap))
169 {
170 destroy(this);
171 return NULL;
172 }
173 if (!this->ifmap->purgePublisher(this->ifmap))
174 {
175 destroy(this);
176 return NULL;
177 }
178 if (!publish_device_ip_addresses(this))
179 {
180 destroy(this);
181 return NULL;
182 }
183 if (reload)
184 {
185 if (!reload_metadata(this))
186 {
187 destroy(this);
188 return NULL;
189 }
190 }
191
192 /* schedule periodic transmission of IF-MAP renewSession request */
193 reschedule = lib->settings->get_int(lib->settings,
194 "%s.plugins.tnc-ifmap.renew_session_interval",
195 IFMAP_RENEW_SESSION_INTERVAL, lib->ns);
196
197 job = (job_t*)tnc_ifmap_renew_session_job_create(
198 this->ifmap->get_ref(this->ifmap), reschedule);
199 lib->scheduler->schedule_job(lib->scheduler, job, reschedule);
200
201 return &this->public;
202 }
203