2 * EAP peer: Method registration
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #ifdef CONFIG_DYNAMIC_EAP_METHODS
12 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
16 #include "eap_methods.h"
19 static struct eap_method *eap_methods = NULL;
21 static void eap_peer_method_free(struct eap_method *method);
25 * eap_peer_get_eap_method - Get EAP method based on type number
26 * @vendor: EAP Vendor-Id (0 = IETF)
27 * @method: EAP type number
28 * Returns: Pointer to EAP method or %NULL if not found
30 const struct eap_method * eap_peer_get_eap_method(int vendor, EapType method)
33 for (m = eap_methods; m; m = m->next) {
34 if (m->vendor == vendor && m->method == method)
42 * eap_peer_get_type - Get EAP type for the given EAP method name
43 * @name: EAP method name, e.g., TLS
44 * @vendor: Buffer for returning EAP Vendor-Id
45 * Returns: EAP method type or %EAP_TYPE_NONE if not found
47 * This function maps EAP type names into EAP type numbers based on the list of
48 * EAP methods included in the build.
50 EapType eap_peer_get_type(const char *name, int *vendor)
53 for (m = eap_methods; m; m = m->next) {
54 if (os_strcmp(m->name, name) == 0) {
59 *vendor = EAP_VENDOR_IETF;
65 * eap_get_name - Get EAP method name for the given EAP type
66 * @vendor: EAP Vendor-Id (0 = IETF)
67 * @type: EAP method type
68 * Returns: EAP method name, e.g., TLS, or %NULL if not found
70 * This function maps EAP type numbers into EAP type names based on the list of
71 * EAP methods included in the build.
73 const char * eap_get_name(int vendor, EapType type)
76 if (vendor == EAP_VENDOR_IETF && type == EAP_TYPE_EXPANDED)
78 for (m = eap_methods; m; m = m->next) {
79 if (m->vendor == vendor && m->method == type)
87 * eap_get_names - Get space separated list of names for supported EAP methods
88 * @buf: Buffer for names
89 * @buflen: Buffer length
90 * Returns: Number of characters written into buf (not including nul
93 size_t eap_get_names(char *buf, size_t buflen)
105 for (m = eap_methods; m; m = m->next) {
106 ret = os_snprintf(pos, end - pos, "%s%s",
107 m == eap_methods ? "" : " ", m->name);
108 if (os_snprintf_error(end - pos, ret))
112 buf[buflen - 1] = '\0';
119 * eap_get_names_as_string_array - Get supported EAP methods as string array
120 * @num: Buffer for returning the number of items in array, not including %NULL
121 * terminator. This parameter can be %NULL if the length is not needed.
122 * Returns: A %NULL-terminated array of strings, or %NULL on error.
124 * This function returns the list of names for all supported EAP methods as an
125 * array of strings. The caller must free the returned array items and the
128 char ** eap_get_names_as_string_array(size_t *num)
130 struct eap_method *m;
131 size_t array_len = 0;
135 for (m = eap_methods; m; m = m->next)
138 array = os_calloc(array_len + 1, sizeof(char *));
142 for (m = eap_methods; m; m = m->next) {
143 array[i++] = os_strdup(m->name);
144 if (array[i - 1] == NULL) {
145 for (j = 0; j < i; j++)
161 * eap_peer_get_methods - Get a list of enabled EAP peer methods
162 * @count: Set to number of available methods
163 * Returns: List of enabled EAP peer methods
165 const struct eap_method * eap_peer_get_methods(size_t *count)
168 struct eap_method *m;
170 for (m = eap_methods; m; m = m->next)
178 #ifdef CONFIG_DYNAMIC_EAP_METHODS
180 * eap_peer_method_load - Load a dynamic EAP method library (shared object)
181 * @so: File path for the shared object file to load
182 * Returns: 0 on success, -1 on failure
184 int eap_peer_method_load(const char *so)
187 int (*dyn_init)(void);
190 handle = dlopen(so, RTLD_LAZY);
191 if (handle == NULL) {
192 wpa_printf(MSG_ERROR, "EAP: Failed to open dynamic EAP method "
193 "'%s': %s", so, dlerror());
197 dyn_init = dlsym(handle, "eap_peer_method_dynamic_init");
198 if (dyn_init == NULL) {
200 wpa_printf(MSG_ERROR, "EAP: Invalid EAP method '%s' - no "
201 "eap_peer_method_dynamic_init()", so);
208 wpa_printf(MSG_ERROR, "EAP: Failed to add EAP method '%s' - "
213 /* Store the handle for this shared object. It will be freed with
214 * dlclose() when the EAP method is unregistered. */
215 eap_methods->dl_handle = handle;
217 wpa_printf(MSG_DEBUG, "EAP: Loaded dynamic EAP method: '%s'", so);
224 * eap_peer_method_unload - Unload a dynamic EAP method library (shared object)
225 * @method: Pointer to the dynamically loaded EAP method
226 * Returns: 0 on success, -1 on failure
228 * This function can be used to unload EAP methods that have been previously
229 * loaded with eap_peer_method_load(). Before unloading the method, all
230 * references to the method must be removed to make sure that no dereferences
231 * of freed memory will occur after unloading.
233 int eap_peer_method_unload(struct eap_method *method)
235 struct eap_method *m, *prev;
247 if (m == NULL || m->dl_handle == NULL)
251 prev->next = m->next;
253 eap_methods = m->next;
255 handle = m->dl_handle;
260 eap_peer_method_free(m);
266 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
270 * eap_peer_method_alloc - Allocate EAP peer method structure
271 * @version: Version of the EAP peer method interface (set to
272 * EAP_PEER_METHOD_INTERFACE_VERSION)
273 * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
274 * @method: EAP type number (EAP_TYPE_*)
275 * @name: Name of the method (e.g., "TLS")
276 * Returns: Allocated EAP method structure or %NULL on failure
278 * The returned structure should be freed with eap_peer_method_free() when it
279 * is not needed anymore.
281 struct eap_method * eap_peer_method_alloc(int version, int vendor,
282 EapType method, const char *name)
284 struct eap_method *eap;
285 eap = os_zalloc(sizeof(*eap));
288 eap->version = version;
289 eap->vendor = vendor;
290 eap->method = method;
297 * eap_peer_method_free - Free EAP peer method structure
298 * @method: Method structure allocated with eap_peer_method_alloc()
300 static void eap_peer_method_free(struct eap_method *method)
307 * eap_peer_method_register - Register an EAP peer method
308 * @method: EAP method to register from eap_peer_method_alloc()
309 * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
310 * has already been registered
312 * Each EAP peer method needs to call this function to register itself as a
313 * supported EAP method. The caller must not free the allocated method data
314 * regardless of the return value.
316 int eap_peer_method_register(struct eap_method *method)
318 struct eap_method *m, *last = NULL;
320 if (method == NULL || method->name == NULL ||
321 method->version != EAP_PEER_METHOD_INTERFACE_VERSION) {
322 eap_peer_method_free(method);
326 for (m = eap_methods; m; m = m->next) {
327 if ((m->vendor == method->vendor &&
328 m->method == method->method) ||
329 os_strcmp(m->name, method->name) == 0) {
330 eap_peer_method_free(method);
339 eap_methods = method;
346 * eap_peer_unregister_methods - Unregister EAP peer methods
348 * This function is called at program termination to unregister all EAP peer
351 void eap_peer_unregister_methods(void)
353 struct eap_method *m;
354 #ifdef CONFIG_DYNAMIC_EAP_METHODS
356 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
358 while (eap_methods) {
360 eap_methods = eap_methods->next;
362 #ifdef CONFIG_DYNAMIC_EAP_METHODS
363 handle = m->dl_handle;
364 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
369 eap_peer_method_free(m);
371 #ifdef CONFIG_DYNAMIC_EAP_METHODS
374 #endif /* CONFIG_DYNAMIC_EAP_METHODS */