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;
23 * eap_peer_get_eap_method - Get EAP method based on type number
24 * @vendor: EAP Vendor-Id (0 = IETF)
25 * @method: EAP type number
26 * Returns: Pointer to EAP method or %NULL if not found
28 const struct eap_method * eap_peer_get_eap_method(int vendor, EapType method)
31 for (m = eap_methods; m; m = m->next) {
32 if (m->vendor == vendor && m->method == method)
40 * eap_peer_get_type - Get EAP type for the given EAP method name
41 * @name: EAP method name, e.g., TLS
42 * @vendor: Buffer for returning EAP Vendor-Id
43 * Returns: EAP method type or %EAP_TYPE_NONE if not found
45 * This function maps EAP type names into EAP type numbers based on the list of
46 * EAP methods included in the build.
48 EapType eap_peer_get_type(const char *name, int *vendor)
51 for (m = eap_methods; m; m = m->next) {
52 if (os_strcmp(m->name, name) == 0) {
57 *vendor = EAP_VENDOR_IETF;
63 * eap_get_name - Get EAP method name for the given EAP type
64 * @vendor: EAP Vendor-Id (0 = IETF)
65 * @type: EAP method type
66 * Returns: EAP method name, e.g., TLS, or %NULL if not found
68 * This function maps EAP type numbers into EAP type names based on the list of
69 * EAP methods included in the build.
71 const char * eap_get_name(int vendor, EapType type)
74 if (vendor == EAP_VENDOR_IETF && type == EAP_TYPE_EXPANDED)
76 for (m = eap_methods; m; m = m->next) {
77 if (m->vendor == vendor && m->method == type)
85 * eap_get_names - Get space separated list of names for supported EAP methods
86 * @buf: Buffer for names
87 * @buflen: Buffer length
88 * Returns: Number of characters written into buf (not including nul
91 size_t eap_get_names(char *buf, size_t buflen)
103 for (m = eap_methods; m; m = m->next) {
104 ret = os_snprintf(pos, end - pos, "%s%s",
105 m == eap_methods ? "" : " ", m->name);
106 if (ret < 0 || ret >= end - pos)
110 buf[buflen - 1] = '\0';
117 * eap_get_names_as_string_array - Get supported EAP methods as string array
118 * @num: Buffer for returning the number of items in array, not including %NULL
119 * terminator. This parameter can be %NULL if the length is not needed.
120 * Returns: A %NULL-terminated array of strings, or %NULL on error.
122 * This function returns the list of names for all supported EAP methods as an
123 * array of strings. The caller must free the returned array items and the
126 char ** eap_get_names_as_string_array(size_t *num)
128 struct eap_method *m;
129 size_t array_len = 0;
133 for (m = eap_methods; m; m = m->next)
136 array = os_zalloc(sizeof(char *) * (array_len + 1));
140 for (m = eap_methods; m; m = m->next) {
141 array[i++] = os_strdup(m->name);
142 if (array[i - 1] == NULL) {
143 for (j = 0; j < i; j++)
159 * eap_peer_get_methods - Get a list of enabled EAP peer methods
160 * @count: Set to number of available methods
161 * Returns: List of enabled EAP peer methods
163 const struct eap_method * eap_peer_get_methods(size_t *count)
166 struct eap_method *m;
168 for (m = eap_methods; m; m = m->next)
176 #ifdef CONFIG_DYNAMIC_EAP_METHODS
178 * eap_peer_method_load - Load a dynamic EAP method library (shared object)
179 * @so: File path for the shared object file to load
180 * Returns: 0 on success, -1 on failure
182 int eap_peer_method_load(const char *so)
185 int (*dyn_init)(void);
188 handle = dlopen(so, RTLD_LAZY);
189 if (handle == NULL) {
190 wpa_printf(MSG_ERROR, "EAP: Failed to open dynamic EAP method "
191 "'%s': %s", so, dlerror());
195 dyn_init = dlsym(handle, "eap_peer_method_dynamic_init");
196 if (dyn_init == NULL) {
198 wpa_printf(MSG_ERROR, "EAP: Invalid EAP method '%s' - no "
199 "eap_peer_method_dynamic_init()", so);
206 wpa_printf(MSG_ERROR, "EAP: Failed to add EAP method '%s' - "
211 /* Store the handle for this shared object. It will be freed with
212 * dlclose() when the EAP method is unregistered. */
213 eap_methods->dl_handle = handle;
215 wpa_printf(MSG_DEBUG, "EAP: Loaded dynamic EAP method: '%s'", so);
222 * eap_peer_method_unload - Unload a dynamic EAP method library (shared object)
223 * @method: Pointer to the dynamically loaded EAP method
224 * Returns: 0 on success, -1 on failure
226 * This function can be used to unload EAP methods that have been previously
227 * loaded with eap_peer_method_load(). Before unloading the method, all
228 * references to the method must be removed to make sure that no dereferences
229 * of freed memory will occur after unloading.
231 int eap_peer_method_unload(struct eap_method *method)
233 struct eap_method *m, *prev;
245 if (m == NULL || m->dl_handle == NULL)
249 prev->next = m->next;
251 eap_methods = m->next;
253 handle = m->dl_handle;
258 eap_peer_method_free(m);
264 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
268 * eap_peer_method_alloc - Allocate EAP peer method structure
269 * @version: Version of the EAP peer method interface (set to
270 * EAP_PEER_METHOD_INTERFACE_VERSION)
271 * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
272 * @method: EAP type number (EAP_TYPE_*)
273 * @name: Name of the method (e.g., "TLS")
274 * Returns: Allocated EAP method structure or %NULL on failure
276 * The returned structure should be freed with eap_peer_method_free() when it
277 * is not needed anymore.
279 struct eap_method * eap_peer_method_alloc(int version, int vendor,
280 EapType method, const char *name)
282 struct eap_method *eap;
283 eap = os_zalloc(sizeof(*eap));
286 eap->version = version;
287 eap->vendor = vendor;
288 eap->method = method;
295 * eap_peer_method_free - Free EAP peer method structure
296 * @method: Method structure allocated with eap_peer_method_alloc()
298 void eap_peer_method_free(struct eap_method *method)
305 * eap_peer_method_register - Register an EAP peer method
306 * @method: EAP method to register
307 * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
308 * has already been registered
310 * Each EAP peer method needs to call this function to register itself as a
311 * supported EAP method.
313 int eap_peer_method_register(struct eap_method *method)
315 struct eap_method *m, *last = NULL;
317 if (method == NULL || method->name == NULL ||
318 method->version != EAP_PEER_METHOD_INTERFACE_VERSION)
321 for (m = eap_methods; m; m = m->next) {
322 if ((m->vendor == method->vendor &&
323 m->method == method->method) ||
324 os_strcmp(m->name, method->name) == 0)
332 eap_methods = method;
339 * eap_peer_unregister_methods - Unregister EAP peer methods
341 * This function is called at program termination to unregister all EAP peer
344 void eap_peer_unregister_methods(void)
346 struct eap_method *m;
347 #ifdef CONFIG_DYNAMIC_EAP_METHODS
349 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
351 while (eap_methods) {
353 eap_methods = eap_methods->next;
355 #ifdef CONFIG_DYNAMIC_EAP_METHODS
356 handle = m->dl_handle;
357 #endif /* CONFIG_DYNAMIC_EAP_METHODS */
362 eap_peer_method_free(m);
364 #ifdef CONFIG_DYNAMIC_EAP_METHODS
367 #endif /* CONFIG_DYNAMIC_EAP_METHODS */