2008-12-05 05:01:41 +01:00
|
|
|
/*
|
|
|
|
* Host AP crypto routines
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
|
|
|
|
* Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation. See README and COPYING for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-20 13:10:57 +01:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2008-12-05 05:01:41 +01:00
|
|
|
//#include <linux/config.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/slab.h>
|
2012-11-20 13:10:42 +01:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/errno.h>
|
2008-12-05 05:01:41 +01:00
|
|
|
|
|
|
|
#include "ieee80211.h"
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Jouni Malinen");
|
|
|
|
MODULE_DESCRIPTION("HostAP crypto");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
struct ieee80211_crypto_alg {
|
|
|
|
struct list_head list;
|
|
|
|
struct ieee80211_crypto_ops *ops;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ieee80211_crypto {
|
|
|
|
struct list_head algs;
|
|
|
|
spinlock_t lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ieee80211_crypto *hcrypt;
|
|
|
|
|
|
|
|
void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee,
|
|
|
|
int force)
|
|
|
|
{
|
|
|
|
struct list_head *ptr, *n;
|
|
|
|
struct ieee80211_crypt_data *entry;
|
|
|
|
|
|
|
|
for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
|
|
|
|
ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
|
|
|
|
entry = list_entry(ptr, struct ieee80211_crypt_data, list);
|
|
|
|
|
|
|
|
if (atomic_read(&entry->refcnt) != 0 && !force)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
list_del(ptr);
|
|
|
|
|
2009-10-02 16:03:38 +02:00
|
|
|
if (entry->ops)
|
2008-12-05 05:01:41 +01:00
|
|
|
entry->ops->deinit(entry->priv);
|
|
|
|
kfree(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ieee80211_crypt_deinit_handler(unsigned long data)
|
|
|
|
{
|
|
|
|
struct ieee80211_device *ieee = (struct ieee80211_device *)data;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ieee->lock, flags);
|
|
|
|
ieee80211_crypt_deinit_entries(ieee, 0);
|
|
|
|
if (!list_empty(&ieee->crypt_deinit_list)) {
|
2012-11-20 13:10:57 +01:00
|
|
|
pr_debug("entries remaining in delayed crypt deletion list\n");
|
2008-12-05 05:01:41 +01:00
|
|
|
ieee->crypt_deinit_timer.expires = jiffies + HZ;
|
|
|
|
add_timer(&ieee->crypt_deinit_timer);
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&ieee->lock, flags);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
|
|
|
|
struct ieee80211_crypt_data **crypt)
|
|
|
|
{
|
|
|
|
struct ieee80211_crypt_data *tmp;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (*crypt == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tmp = *crypt;
|
|
|
|
*crypt = NULL;
|
|
|
|
|
|
|
|
/* must not run ops->deinit() while there may be pending encrypt or
|
|
|
|
* decrypt operations. Use a list of delayed deinits to avoid needing
|
|
|
|
* locking. */
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ieee->lock, flags);
|
|
|
|
list_add(&tmp->list, &ieee->crypt_deinit_list);
|
|
|
|
if (!timer_pending(&ieee->crypt_deinit_timer)) {
|
|
|
|
ieee->crypt_deinit_timer.expires = jiffies + HZ;
|
|
|
|
add_timer(&ieee->crypt_deinit_timer);
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&ieee->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct ieee80211_crypto_alg *alg;
|
|
|
|
|
|
|
|
if (hcrypt == NULL)
|
|
|
|
return -1;
|
|
|
|
|
Staging: Use kcalloc or kzalloc
Use kcalloc or kzalloc rather than the combination of kmalloc and memset.
The semantic patch that makes this change is as follows:
(http://coccinelle.lip6.fr/)
// <smpl>
@@
expression x,y,flags;
statement S;
type T;
@@
x =
- kmalloc
+ kcalloc
(
- y * sizeof(T),
+ y, sizeof(T),
flags);
if (x == NULL) S
-memset(x, 0, y * sizeof(T));
@@
expression x,size,flags;
statement S;
@@
-x = kmalloc(size,flags);
+x = kzalloc(size,flags);
if (x == NULL) S
-memset(x, 0, size);
// </smpl>
Signed-off-by: Julia Lawall <julia@diku.dk>
2010-05-13 22:00:05 +02:00
|
|
|
alg = kzalloc(sizeof(*alg), GFP_KERNEL);
|
2008-12-05 05:01:41 +01:00
|
|
|
if (alg == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
alg->ops = ops;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hcrypt->lock, flags);
|
|
|
|
list_add(&alg->list, &hcrypt->algs);
|
|
|
|
spin_unlock_irqrestore(&hcrypt->lock, flags);
|
|
|
|
|
2012-11-20 13:10:57 +01:00
|
|
|
pr_debug("registered algorithm '%s'\n", ops->name);
|
2008-12-05 05:01:41 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct list_head *ptr;
|
|
|
|
struct ieee80211_crypto_alg *del_alg = NULL;
|
|
|
|
|
|
|
|
if (hcrypt == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hcrypt->lock, flags);
|
|
|
|
for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
|
|
|
|
struct ieee80211_crypto_alg *alg =
|
|
|
|
(struct ieee80211_crypto_alg *) ptr;
|
|
|
|
if (alg->ops == ops) {
|
|
|
|
list_del(&alg->list);
|
|
|
|
del_alg = alg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&hcrypt->lock, flags);
|
|
|
|
|
|
|
|
if (del_alg) {
|
2012-11-20 13:10:57 +01:00
|
|
|
pr_debug("unregistered algorithm '%s'\n", ops->name);
|
2008-12-05 05:01:41 +01:00
|
|
|
kfree(del_alg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return del_alg ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 13:11:12 +01:00
|
|
|
struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
|
2008-12-05 05:01:41 +01:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct list_head *ptr;
|
|
|
|
struct ieee80211_crypto_alg *found_alg = NULL;
|
|
|
|
|
|
|
|
if (hcrypt == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&hcrypt->lock, flags);
|
|
|
|
for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
|
|
|
|
struct ieee80211_crypto_alg *alg =
|
|
|
|
(struct ieee80211_crypto_alg *) ptr;
|
|
|
|
if (strcmp(alg->ops->name, name) == 0) {
|
|
|
|
found_alg = alg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&hcrypt->lock, flags);
|
|
|
|
|
|
|
|
if (found_alg)
|
|
|
|
return found_alg->ops;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 13:11:12 +01:00
|
|
|
static void *ieee80211_crypt_null_init(int keyidx) { return (void *) 1; }
|
2008-12-05 05:01:41 +01:00
|
|
|
static void ieee80211_crypt_null_deinit(void *priv) {}
|
|
|
|
|
|
|
|
static struct ieee80211_crypto_ops ieee80211_crypt_null = {
|
|
|
|
.name = "NULL",
|
|
|
|
.init = ieee80211_crypt_null_init,
|
|
|
|
.deinit = ieee80211_crypt_null_deinit,
|
|
|
|
.encrypt_mpdu = NULL,
|
|
|
|
.decrypt_mpdu = NULL,
|
|
|
|
.encrypt_msdu = NULL,
|
|
|
|
.decrypt_msdu = NULL,
|
|
|
|
.set_key = NULL,
|
|
|
|
.get_key = NULL,
|
|
|
|
.extra_prefix_len = 0,
|
|
|
|
.extra_postfix_len = 0,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int ieee80211_crypto_init(void)
|
|
|
|
{
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
Staging: Use kcalloc or kzalloc
Use kcalloc or kzalloc rather than the combination of kmalloc and memset.
The semantic patch that makes this change is as follows:
(http://coccinelle.lip6.fr/)
// <smpl>
@@
expression x,y,flags;
statement S;
type T;
@@
x =
- kmalloc
+ kcalloc
(
- y * sizeof(T),
+ y, sizeof(T),
flags);
if (x == NULL) S
-memset(x, 0, y * sizeof(T));
@@
expression x,size,flags;
statement S;
@@
-x = kmalloc(size,flags);
+x = kzalloc(size,flags);
if (x == NULL) S
-memset(x, 0, size);
// </smpl>
Signed-off-by: Julia Lawall <julia@diku.dk>
2010-05-13 22:00:05 +02:00
|
|
|
hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL);
|
2008-12-05 05:01:41 +01:00
|
|
|
if (!hcrypt)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&hcrypt->algs);
|
|
|
|
spin_lock_init(&hcrypt->lock);
|
|
|
|
|
|
|
|
ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
|
|
|
|
if (ret < 0) {
|
|
|
|
kfree(hcrypt);
|
|
|
|
hcrypt = NULL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ieee80211_crypto_deinit(void)
|
|
|
|
{
|
|
|
|
struct list_head *ptr, *n;
|
2009-02-17 17:16:26 +01:00
|
|
|
struct ieee80211_crypto_alg *alg = NULL;
|
2008-12-05 05:01:41 +01:00
|
|
|
|
|
|
|
if (hcrypt == NULL)
|
|
|
|
return;
|
|
|
|
|
2009-02-17 17:16:26 +01:00
|
|
|
list_for_each_safe(ptr, n, &hcrypt->algs) {
|
|
|
|
alg = list_entry(ptr, struct ieee80211_crypto_alg, list);
|
|
|
|
if (alg) {
|
|
|
|
list_del(ptr);
|
2012-11-20 13:10:57 +01:00
|
|
|
pr_debug("unregistered algorithm '%s' (deinit)\n",
|
|
|
|
alg->ops->name);
|
2009-02-17 17:16:26 +01:00
|
|
|
kfree(alg);
|
|
|
|
}
|
2008-12-05 05:01:41 +01:00
|
|
|
}
|
|
|
|
kfree(hcrypt);
|
|
|
|
}
|