2 * EAP peer: Method registration
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #ifdef CONFIG_DYNAMIC_EAP_METHODS
18 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
22 #include "eap_methods.h"
25 static struct eap_method
*eap_methods
= NULL
;
29 * eap_peer_get_eap_method - Get EAP method based on type number
30 * @vendor: EAP Vendor-Id (0 = IETF)
31 * @method: EAP type number
32 * Returns: Pointer to EAP method or %NULL if not found
34 const struct eap_method
* eap_peer_get_eap_method(int vendor
, EapType method
)
37 for (m
= eap_methods
; m
; m
= m
->next
) {
38 if (m
->vendor
== vendor
&& m
->method
== method
)
46 * eap_peer_get_type - Get EAP type for the given EAP method name
47 * @name: EAP method name, e.g., TLS
48 * @vendor: Buffer for returning EAP Vendor-Id
49 * Returns: EAP method type or %EAP_TYPE_NONE if not found
51 * This function maps EAP type names into EAP type numbers based on the list of
52 * EAP methods included in the build.
54 EapType
eap_peer_get_type(const char *name
, int *vendor
)
57 for (m
= eap_methods
; m
; m
= m
->next
) {
58 if (os_strcmp(m
->name
, name
) == 0) {
63 *vendor
= EAP_VENDOR_IETF
;
69 * eap_get_name - Get EAP method name for the given EAP type
70 * @vendor: EAP Vendor-Id (0 = IETF)
71 * @type: EAP method type
72 * Returns: EAP method name, e.g., TLS, or %NULL if not found
74 * This function maps EAP type numbers into EAP type names based on the list of
75 * EAP methods included in the build.
77 const char * eap_get_name(int vendor
, EapType type
)
80 for (m
= eap_methods
; m
; m
= m
->next
) {
81 if (m
->vendor
== vendor
&& m
->method
== type
)
89 * eap_get_names - Get space separated list of names for supported EAP methods
90 * @buf: Buffer for names
91 * @buflen: Buffer length
92 * Returns: Number of characters written into buf (not including nul
95 size_t eap_get_names(char *buf
, size_t buflen
)
107 for (m
= eap_methods
; m
; m
= m
->next
) {
108 ret
= os_snprintf(pos
, end
- pos
, "%s%s",
109 m
== eap_methods
? "" : " ", m
->name
);
110 if (ret
< 0 || ret
>= end
- pos
)
114 buf
[buflen
- 1] = '\0';
121 * eap_get_names_as_string_array - Get supported EAP methods as string array
122 * @num: Buffer for returning the number of items in array, not including %NULL
123 * terminator. This parameter can be %NULL if the length is not needed.
124 * Returns: A %NULL-terminated array of strings, or %NULL on error.
126 * This function returns the list of names for all supported EAP methods as an
127 * array of strings. The caller must free the returned array items and the
130 char ** eap_get_names_as_string_array(size_t *num
)
132 struct eap_method
*m
;
133 size_t array_len
= 0;
137 for (m
= eap_methods
; m
; m
= m
->next
)
140 array
= os_zalloc(sizeof(char *) * (array_len
+ 1));
144 for (m
= eap_methods
; m
; m
= m
->next
) {
145 array
[i
++] = os_strdup(m
->name
);
146 if (array
[i
- 1] == NULL
) {
147 for (j
= 0; j
< i
; j
++)
163 * eap_peer_get_methods - Get a list of enabled EAP peer methods
164 * @count: Set to number of available methods
165 * Returns: List of enabled EAP peer methods
167 const struct eap_method
* eap_peer_get_methods(size_t *count
)
170 struct eap_method
*m
;
172 for (m
= eap_methods
; m
; m
= m
->next
)
180 #ifdef CONFIG_DYNAMIC_EAP_METHODS
182 * eap_peer_method_load - Load a dynamic EAP method library (shared object)
183 * @so: File path for the shared object file to load
184 * Returns: 0 on success, -1 on failure
186 int eap_peer_method_load(const char *so
)
189 int (*dyn_init
)(void);
192 handle
= dlopen(so
, RTLD_LAZY
);
193 if (handle
== NULL
) {
194 wpa_printf(MSG_ERROR
, "EAP: Failed to open dynamic EAP method "
195 "'%s': %s", so
, dlerror());
199 dyn_init
= dlsym(handle
, "eap_peer_method_dynamic_init");
200 if (dyn_init
== NULL
) {
202 wpa_printf(MSG_ERROR
, "EAP: Invalid EAP method '%s' - no "
203 "eap_peer_method_dynamic_init()", so
);
210 wpa_printf(MSG_ERROR
, "EAP: Failed to add EAP method '%s' - "
215 /* Store the handle for this shared object. It will be freed with
216 * dlclose() when the EAP method is unregistered. */
217 eap_methods
->dl_handle
= handle
;
219 wpa_printf(MSG_DEBUG
, "EAP: Loaded dynamic EAP method: '%s'", so
);
226 * eap_peer_method_unload - Unload a dynamic EAP method library (shared object)
227 * @method: Pointer to the dynamically loaded EAP method
228 * Returns: 0 on success, -1 on failure
230 * This function can be used to unload EAP methods that have been previously
231 * loaded with eap_peer_method_load(). Before unloading the method, all
232 * references to the method must be removed to make sure that no dereferences
233 * of freed memory will occur after unloading.
235 int eap_peer_method_unload(struct eap_method
*method
)
237 struct eap_method
*m
, *prev
;
249 if (m
== NULL
|| m
->dl_handle
== NULL
)
253 prev
->next
= m
->next
;
255 eap_methods
= m
->next
;
257 handle
= m
->dl_handle
;
262 eap_peer_method_free(m
);
268 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
272 * eap_peer_method_alloc - Allocate EAP peer method structure
273 * @version: Version of the EAP peer method interface (set to
274 * EAP_PEER_METHOD_INTERFACE_VERSION)
275 * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
276 * @method: EAP type number (EAP_TYPE_*)
277 * @name: Name of the method (e.g., "TLS")
278 * Returns: Allocated EAP method structure or %NULL on failure
280 * The returned structure should be freed with eap_peer_method_free() when it
281 * is not needed anymore.
283 struct eap_method
* eap_peer_method_alloc(int version
, int vendor
,
284 EapType method
, const char *name
)
286 struct eap_method
*eap
;
287 eap
= os_zalloc(sizeof(*eap
));
290 eap
->version
= version
;
291 eap
->vendor
= vendor
;
292 eap
->method
= method
;
299 * eap_peer_method_free - Free EAP peer method structure
300 * @method: Method structure allocated with eap_peer_method_alloc()
302 void eap_peer_method_free(struct eap_method
*method
)
309 * eap_peer_method_register - Register an EAP peer method
310 * @method: EAP method to register
311 * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
312 * has already been registered
314 * Each EAP peer method needs to call this function to register itself as a
315 * supported EAP method.
317 int eap_peer_method_register(struct eap_method
*method
)
319 struct eap_method
*m
, *last
= NULL
;
321 if (method
== NULL
|| method
->name
== NULL
||
322 method
->version
!= EAP_PEER_METHOD_INTERFACE_VERSION
)
325 for (m
= eap_methods
; m
; m
= m
->next
) {
326 if ((m
->vendor
== method
->vendor
&&
327 m
->method
== method
->method
) ||
328 os_strcmp(m
->name
, method
->name
) == 0)
336 eap_methods
= method
;
343 * eap_peer_unregister_methods - Unregister EAP peer methods
345 * This function is called at program termination to unregister all EAP peer
348 void eap_peer_unregister_methods(void)
350 struct eap_method
*m
;
351 #ifdef CONFIG_DYNAMIC_EAP_METHODS
353 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
355 while (eap_methods
) {
357 eap_methods
= eap_methods
->next
;
359 #ifdef CONFIG_DYNAMIC_EAP_METHODS
360 handle
= m
->dl_handle
;
361 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
366 eap_peer_method_free(m
);
368 #ifdef CONFIG_DYNAMIC_EAP_METHODS
371 #endif /* CONFIG_DYNAMIC_EAP_METHODS */