implemented periodic IF-MAP RenewSession request
[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 enumerator_t *enumerator;
75 ike_sa_t *ike_sa;
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 {
88 success = FALSE;
89 break;
90 }
91 }
92 enumerator->destroy(enumerator);
93
94 return success;
95 }
96
97 METHOD(listener_t, ike_updown, bool,
98 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, bool up)
99 {
100 if (ike_sa->get_state(ike_sa) != IKE_CONNECTING)
101 {
102 this->ifmap->publish_ike_sa(this->ifmap, ike_sa, up);
103 }
104 return TRUE;
105 }
106
107 METHOD(listener_t, alert, bool,
108 private_tnc_ifmap_listener_t *this, ike_sa_t *ike_sa, alert_t alert,
109 va_list args)
110 {
111 if (alert == ALERT_PEER_AUTH_FAILED)
112 {
113 this->ifmap->publish_enforcement_report(this->ifmap,
114 ike_sa->get_other_host(ike_sa),
115 "block", "authentication failed");
116 }
117 return TRUE;
118 }
119
120 METHOD(tnc_ifmap_listener_t, destroy, void,
121 private_tnc_ifmap_listener_t *this)
122 {
123 DESTROY_IF(this->ifmap);
124 free(this);
125 }
126
127 /**
128 * See header
129 */
130 tnc_ifmap_listener_t *tnc_ifmap_listener_create(bool reload)
131 {
132 private_tnc_ifmap_listener_t *this;
133 job_t *job;
134 u_int32_t reschedule;
135
136 INIT(this,
137 .public = {
138 .listener = {
139 .ike_updown = _ike_updown,
140 .alert = _alert,
141 },
142 .destroy = _destroy,
143 },
144 .ifmap = tnc_ifmap_soap_create(),
145 );
146
147 if (!this->ifmap)
148 {
149 destroy(this);
150 return NULL;
151 }
152 if (!this->ifmap->newSession(this->ifmap))
153 {
154 destroy(this);
155 return NULL;
156 }
157 if (!this->ifmap->purgePublisher(this->ifmap))
158 {
159 destroy(this);
160 return NULL;
161 }
162 if (!publish_device_ip_addresses(this))
163 {
164 destroy(this);
165 return NULL;
166 }
167 if (reload)
168 {
169 if (!reload_metadata(this))
170 {
171 destroy(this);
172 return NULL;
173 }
174 }
175
176 /* schedule periodic transmission of IF-MAP renewSession request */
177 reschedule = lib->settings->get_int(lib->settings,
178 "%s.plugins.tnc-ifmap.renew_session_interval",
179 IFMAP_RENEW_SESSION_INTERVAL, charon->name);
180
181 job = (job_t*)tnc_ifmap_renew_session_job_create(this->ifmap, reschedule);
182 lib->scheduler->schedule_job(lib->scheduler, job, reschedule);
183
184 return &this->public;
185 }
186