blob: 836c686532a7d194b9e9a0e7c02b754b757ed10d [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*****************************************************************************************************
2 * Software License Agreement (BSD License)
3 * Author : Souheil Ben Ayed <souheil@tera.ics.keio.ac.jp>
4 *
5 * Copyright (c) 2009-2010, Souheil Ben Ayed, Teraoka Laboratory of Keio University, and the WIDE Project
6 * All rights reserved.
7 *
8 * Redistribution and use of this software in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Souheil Ben Ayed <souheil@tera.ics.keio.ac.jp>.
21 *
22 * 4. Neither the name of Souheil Ben Ayed, Teraoka Laboratory of Keio University or the WIDE Project nor the
23 * names of its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTfORS BE LIABLE FOR ANY
30 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *****************************************************************************************************/
37
38#include "diameap_common.h"
39
40/********************************************/
41/* Loading and initializing plug-ins */
42/********************************************/
43
44/* Add new plugin */
45int diameap_plugin_add(char * name, eap_type methodtype, u32 vendor,
46 char * filename, char * conffile)
47{
48 TRACE_ENTRY("%p %d %d %p %p", name, methodtype, vendor, filename, conffile);
49
50 /* Check the filename is valid */
51 CHECK_PARAMS(filename);
52
53 /* add new EAP method in the list of plugins*/
54 {
55 struct plugin * plugin;
56 CHECK_MALLOC(plugin = malloc(sizeof(struct plugin)));
57 memset(plugin, 0, sizeof(struct plugin));
58 fd_list_init(&plugin->chain, plugin);
59 plugin->methodname = name;
60 plugin->methodtype = methodtype;
61 plugin->vendor = vendor;
62 plugin->pluginfile = filename;
63 plugin->conffile = conffile;
64 fd_list_insert_before(&plugins_list, &plugin->chain);
65 }
66
67 return 0;
68}
69
70/* Dump plugins list */
71void diameap_plugin_dump(void)
72{
73 struct fd_list * pl;
74
75 for (pl = plugins_list.next; pl != &plugins_list; pl = pl->next)
76 {
77 struct plugin * plugin = (struct plugin *) pl;
78 fd_log_debug("\t\t - %s plugin \t [Type: %i, Vendor: %i] %sloaded", plugin->methodname,
79 plugin->methodtype,plugin->vendor, plugin->handler ? "" : "not ");
80 }
81}
82
83int diameap_plugin_get(u32 vendor, eap_type type, struct plugin ** getplugin)
84{
85 TRACE_ENTRY("%d %d %p",vendor,type,getplugin);
86 struct fd_list * pl;
87
88 for (pl = plugins_list.next; pl != &plugins_list; pl = pl->next)
89 {
90 struct plugin * plugin = (struct plugin *) pl;
91 if (plugin->methodtype == type && plugin->vendor == vendor)
92 {
93 *getplugin = plugin;
94 return 0;
95 }
96 }
97 return 1;
98}
99
100/* Load all method in the plugins list */
101int diameap_plugin_load(void)
102{
103
104 int ret;
105 struct fd_list * pl;
106
107 /* Loop on all plugins */
108
109 for (pl = plugins_list.next; pl != &plugins_list; pl = pl->next)
110 {
111 struct plugin * plugin = (struct plugin *) pl;
112 struct register_plugin * registerplugin;
113 int (*diameap_plugin_register)();
114
115 TRACE_DEBUG(FULL, "%sLoading EAP method plugin: %s",DIAMEAP_EXTENSION, plugin->methodname);
116
117 /* Load the method */
118 plugin->handler = dlopen(plugin->pluginfile, RTLD_LAZY | RTLD_GLOBAL);
119 if (plugin->handler == NULL)
120 {
121 TRACE_DEBUG(INFO, "%sLoading of plugin %s failed: %s",DIAMEAP_EXTENSION,
122 plugin->methodname, dlerror());
123 return EINVAL;
124 }
125
126 /* Defined register methods for this EAP method*/
127 diameap_plugin_register = (int(*)()) dlsym(plugin->handler,
128 "diameap_plugin_register");
129 if (!diameap_plugin_register)
130 {
131 TRACE_DEBUG(INFO,
132 "%s[%s plugin] Unable to register EAP method: %s.",DIAMEAP_EXTENSION,
133 plugin->methodname, dlerror());
134 return EINVAL;
135 }
136
137 if ((*diameap_plugin_register)() != 0)
138 {
139 TRACE_DEBUG(INFO,
140 "%s[%s plugin] Unable to register EAP method plugin",DIAMEAP_EXTENSION,plugin->methodname);
141 }
142
143 int (*diameap_plugin_objects)(struct register_plugin **);
144
145 diameap_plugin_objects = (int(*)(struct register_plugin **)) dlsym(
146 plugin->handler, "diameap_plugin_objects");
147 if (!diameap_plugin_objects)
148 {
149 TRACE_DEBUG(INFO,
150 "%s[%s plugin] Unable to resolve symbol of the plugin: %s",DIAMEAP_EXTENSION,
151 plugin->methodname, dlerror());
152 return EINVAL;
153 }
154
155 if ((*diameap_plugin_objects)(&registerplugin) != 0)
156 {
157
158 TRACE_DEBUG(
159 INFO,
160 "%s[%s plugin] Unable to get objects description from the plug-in: %s",DIAMEAP_EXTENSION,
161 plugin->methodname, dlerror());
162 return EINVAL;
163 }
164
165 /* eap_method_configure method */
166 if (registerplugin->configure)
167 {
168 plugin->eap_method_configure = (int(*)(char *)) dlsym(
169 plugin->handler, registerplugin->configure);
170 if (plugin->eap_method_configure == NULL)
171 {
172 TRACE_DEBUG(
173 INFO,
174 "%s[%s plugin] Unable to resolve symbol for 'eap_method_configure': %s",DIAMEAP_EXTENSION,
175 plugin->methodname, dlerror());
176 }
177 }
178 else
179 {
180 plugin->eap_method_configure = NULL;
181 TRACE_DEBUG(FULL+1,"%s[%s plugin] Unavailable function 'eap_method_configure'",DIAMEAP_EXTENSION, plugin->methodname);
182 }
183
184 /* eap_method_init method */
185 if (registerplugin->init)
186 {
187 plugin->eap_method_init
188 = (int(*)(struct eap_state_machine *)) dlsym(
189 plugin->handler, registerplugin->init);
190 if (plugin->eap_method_init == NULL)
191 {
192 TRACE_DEBUG(
193 INFO,
194 "%s[%s plugin] Unable to resolve symbol for 'eap_method_init': %s",DIAMEAP_EXTENSION,
195 plugin->methodname, dlerror());
196 return EINVAL;
197 }
198 }
199 else
200 {
201 plugin->eap_method_init = NULL;
202 TRACE_DEBUG(INFO,"%s[%s plugin] Unavailable function 'eap_method_init'",DIAMEAP_EXTENSION, plugin->methodname);
203 return EINVAL;
204 }
205
206 /* eap_method_initPickUp method */
207 if (registerplugin->initPickUp)
208 {
209 plugin->eap_method_initPickUp
210 = (int(*)(struct eap_state_machine *)) dlsym(
211 plugin->handler, registerplugin->initPickUp);
212 }
213 else
214 {
215 plugin->eap_method_initPickUp = NULL;
216 TRACE_DEBUG(FULL+1,"%s[%s plugin] Unavailable function 'eap_method_initPickUp'",DIAMEAP_EXTENSION, plugin->methodname);
217 }
218
219 /* eap_method_buildReq method */
220 if (registerplugin->buildReq)
221 {
222 plugin->eap_method_buildReq = (int(*)(struct eap_state_machine *,
223 u8, struct eap_packet *)) dlsym(plugin->handler,
224 registerplugin->buildReq);
225 if (plugin->eap_method_buildReq == NULL)
226 {
227 TRACE_DEBUG(
228 INFO,
229 "%s[%s plugin] Unable to resolve symbol for 'eap_method_buildReq': %s",DIAMEAP_EXTENSION,
230 plugin->methodname, dlerror());
231 return EINVAL;
232 }
233 }
234 else
235 {
236 plugin->eap_method_buildReq = NULL;
237 TRACE_DEBUG(INFO,"%s[%s plugin] Unavailable function 'eap_method_buildReq'",DIAMEAP_EXTENSION, plugin->methodname);
238 return EINVAL;
239 }
240
241 /* eap_method_getTimeout method */
242 if (registerplugin->getTimeout)
243 {
244 plugin->eap_method_getTimeout = (int(*)(struct eap_state_machine *,
245 int *)) dlsym(plugin->handler, registerplugin->getTimeout);
246 if (plugin->eap_method_getTimeout == NULL)
247 {
248 TRACE_DEBUG(
249 INFO,
250 "%s[%s plugin] Unable to resolve symbol for 'eap_method_getTimeout': %s",DIAMEAP_EXTENSION,
251 plugin->methodname, dlerror());
252 return EINVAL;
253 }
254 }
255 else
256 {
257 plugin->eap_method_getTimeout = NULL;
258 TRACE_DEBUG(FULL+1,"%s[%s plugin] Unavailable function 'eap_method_getTimeout'",DIAMEAP_EXTENSION, plugin->methodname);
259 }
260
261 /* eap_method_check method */
262 if (registerplugin->check)
263 {
264 plugin->eap_method_check = (boolean(*)(struct eap_state_machine *,
265 struct eap_packet*)) dlsym(plugin->handler,
266 registerplugin->check);
267 if (plugin->eap_method_check == NULL)
268 {
269 TRACE_DEBUG(
270 INFO,
271 "%s[%s plugin] Unable to resolve symbol for 'eap_method_check': %s",DIAMEAP_EXTENSION,
272 plugin->methodname, dlerror());
273 return EINVAL;
274 }
275 }
276 else
277 {
278 plugin->eap_method_check = NULL;
279 TRACE_DEBUG(INFO,"%s[%s plugin] Unavailable function 'eap_method_check'",DIAMEAP_EXTENSION, plugin->methodname);
280 return EINVAL;
281 }
282
283 /* eap_method_process method */
284 if (registerplugin->process)
285 {
286 plugin->eap_method_process = (int(*)(struct eap_state_machine *,
287 struct eap_packet*)) dlsym(plugin->handler,
288 registerplugin->process);
289 if (plugin->eap_method_process == NULL)
290 {
291 TRACE_DEBUG(
292 INFO,
293 "%s[%s plugin] Unable to resolve symbol for 'eap_method_process': %s",DIAMEAP_EXTENSION,
294 plugin->methodname, dlerror());
295 return EINVAL;
296 }
297 }
298 else
299 {
300 plugin->eap_method_process = NULL;
301 TRACE_DEBUG(INFO,"%s[%s plugin] Unavailable function 'eap_method_process'",DIAMEAP_EXTENSION, plugin->methodname);
302 return EINVAL;
303 }
304
305 /* eap_method_isDone method */
306 if (registerplugin->isDone)
307 {
308 plugin->eap_method_isDone
309 = (boolean(*)(struct eap_state_machine *)) dlsym(
310 plugin->handler, registerplugin->isDone);
311 if (plugin->eap_method_isDone == NULL)
312 {
313 TRACE_DEBUG(
314 INFO,
315 "%s[%s plugin] Unable to resolve symbol for 'eap_method_isDone': %s",DIAMEAP_EXTENSION,
316 plugin->methodname, dlerror());
317 return EINVAL;
318 }
319 }
320 else
321 {
322 plugin->eap_method_isDone = NULL;
323 TRACE_DEBUG(INFO,"%s[%s plugin] Unavailable function 'eap_method_isDone'",DIAMEAP_EXTENSION, plugin->methodname);
324 return EINVAL;
325 }
326
327 /* eap_method_getKey method */
328 if (registerplugin->getKey)
329 {
330 plugin->eap_method_getKey = (int(*)(struct eap_state_machine *,
331 u8**, int*,u8**, int*)) dlsym(plugin->handler, registerplugin->getKey);
332 if (plugin->eap_method_getKey == NULL)
333 {
334 TRACE_DEBUG(
335 INFO,
336 "%s[%s plugin] Unable to resolve symbol for 'eap_method_getKey': %s",DIAMEAP_EXTENSION,
337 plugin->methodname, dlerror());
338 return EINVAL;
339 }
340 }
341 else
342 {
343 plugin->eap_method_getKey = NULL;
344 TRACE_DEBUG(FULL+1,"%s[%s plugin] Unavailable function 'eap_method_getKey'",DIAMEAP_EXTENSION, plugin->methodname);
345 }
346
347 /* eap_method_unregister method */
348 if (registerplugin->unregister)
349 {
350 plugin->eap_method_unregister = (void(*)(void)) dlsym(
351 plugin->handler, registerplugin->unregister);
352 if (plugin->eap_method_unregister == NULL)
353 {
354 TRACE_DEBUG(
355 INFO,
356 "%s[%s plugin] Unable to resolve symbol for 'eap_method_unregister': %s",DIAMEAP_EXTENSION,
357 plugin->methodname, dlerror());
358 return EINVAL;
359 }
360 }
361 else
362 {
363 plugin->eap_method_unregister = NULL;
364 TRACE_DEBUG(FULL+1,"%s[%s plugin] Unavailable function 'eap_method_unregister'",DIAMEAP_EXTENSION, plugin->methodname);
365 }
366
367 /* eap_method_datafree method */
368 if (registerplugin->datafree)
369 {
370 plugin->eap_method_free = (void(*)(void *)) dlsym(plugin->handler,
371 registerplugin->datafree);
372 if (plugin->eap_method_free == NULL)
373 {
374 TRACE_DEBUG(
375 INFO,
376 "%s[%s plugin] Unable to resolve symbol for 'eap_method_datafree': %s",DIAMEAP_EXTENSION,
377 plugin->methodname, dlerror());
378 return EINVAL;
379 }
380 }
381 else
382 {
383 plugin->eap_method_free = NULL;
384 TRACE_DEBUG(FULL+1,"%s[%s plugin] Unavailable function 'eap_method_datafree'",DIAMEAP_EXTENSION, plugin->methodname);
385 }
386
387 if (plugin->eap_method_configure != NULL)
388 {
389 /* call the configuration method */
390 ret = (*plugin->eap_method_configure)(plugin->conffile);
391 if (ret != 0)
392 {
393 TRACE_DEBUG(
394 INFO,
395 "%s[%s plugin] Unable to configure the plugin",DIAMEAP_EXTENSION,
396 plugin->methodname);
397 return ret;
398 }
399 }
400
401 TRACE_DEBUG(FULL, "%s[%s plugin] Loaded successfully.",DIAMEAP_EXTENSION,
402 plugin->methodname);
403
404 /* load next method */
405 }
406
407 return 0;
408}
409
410boolean diameap_plugin_exist(u32 vendor, eap_type type)
411{
412
413 TRACE_ENTRY("%d %d",vendor,type);
414 struct fd_list * pl;
415
416 for (pl = plugins_list.next; pl != &plugins_list; pl = pl->next)
417 {
418 struct plugin * plugin = (struct plugin *) pl;
419 if (plugin->methodtype == type && plugin->vendor == vendor)
420 {
421 return TRUE;
422 }
423 }
424 return FALSE;
425}
426
427int diameap_plugin_unload(void)
428{
429
430 while (!FD_IS_LIST_EMPTY(&plugins_list))
431 {
432 struct fd_list * plugin = plugins_list.next;
433 struct plugin * item = (struct plugin *) plugin;
434
435 fd_list_unlink(plugin);
436
437 if (item->eap_method_unregister != NULL)
438 {
439 (*item->eap_method_unregister)();
440 }
441
442 if (item->handler)
443 {
444 if (dlclose(item->handler) != 0)
445 {
446 TRACE_DEBUG (INFO, "%sFail to unload plugin %s : %s",DIAMEAP_EXTENSION, item->methodname, dlerror());
447 }
448 }
449
450 free(item->conffile);
451 free(item->pluginfile);
452 free(item);
453 }
454 return 0;
455}