summaryrefslogtreecommitdiff
path: root/net/dccp/ccid.c
blob: 06b191a5740b4713ecb4a942dbad87e4df2b5b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 *  net/dccp/ccid.c
 *
 *  An implementation of the DCCP protocol
 *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *
 *  CCID infrastructure
 *
 *	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.
 */

#include "ccid.h"

static struct ccid *ccids[CCID_MAX];
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
static atomic_t ccids_lockct = ATOMIC_INIT(0);
static DEFINE_SPINLOCK(ccids_lock);

/*
 * The strategy is: modifications ccids vector are short, do not sleep and
 * veeery rare, but read access should be free of any exclusive locks.
 */
static void ccids_write_lock(void)
{
	spin_lock(&ccids_lock);
	while (atomic_read(&ccids_lockct) != 0) {
		spin_unlock(&ccids_lock);
		yield();
		spin_lock(&ccids_lock);
	}
}

static inline void ccids_write_unlock(void)
{
	spin_unlock(&ccids_lock);
}

static inline void ccids_read_lock(void)
{
	atomic_inc(&ccids_lockct);
	spin_unlock_wait(&ccids_lock);
}

static inline void ccids_read_unlock(void)
{
	atomic_dec(&ccids_lockct);
}

#else
#define ccids_write_lock() do { } while(0)
#define ccids_write_unlock() do { } while(0)
#define ccids_read_lock() do { } while(0)
#define ccids_read_unlock() do { } while(0)
#endif

int ccid_register(struct ccid *ccid)
{
	int err;

	ccids_write_lock();
	err = -EEXIST;
	if (ccids[ccid->ccid_id] == NULL) {
		ccids[ccid->ccid_id] = ccid;
		err = 0;
	}
	ccids_write_unlock();
	if (err == 0)
		pr_info("CCID: Registered CCID %d (%s)\n",
			ccid->ccid_id, ccid->ccid_name);
	return err;
}

EXPORT_SYMBOL_GPL(ccid_register);

int ccid_unregister(struct ccid *ccid)
{
	ccids_write_lock();
	ccids[ccid->ccid_id] = NULL;
	ccids_write_unlock();
	pr_info("CCID: Unregistered CCID %d (%s)\n",
		ccid->ccid_id, ccid->ccid_name);
	return 0;
}

EXPORT_SYMBOL_GPL(ccid_unregister);

struct ccid *ccid_init(unsigned char id, struct sock *sk)
{
	struct ccid *ccid;

#ifdef CONFIG_KMOD
	if (ccids[id] == NULL)
		request_module("net-dccp-ccid-%d", id);
#endif
	ccids_read_lock();

	ccid = ccids[id];
	if (ccid == NULL)
		goto out;

	if (!try_module_get(ccid->ccid_owner))
		goto out_err;

	if (ccid->ccid_init != NULL && ccid->ccid_init(sk) != 0)
		goto out_module_put;
out:
	ccids_read_unlock();
	return ccid;
out_module_put:
	module_put(ccid->ccid_owner);
out_err:
	ccid = NULL;
	goto out;
}

EXPORT_SYMBOL_GPL(ccid_init);

void ccid_exit(struct ccid *ccid, struct sock *sk)
{
	if (ccid == NULL)
		return;

	ccids_read_lock();

	if (ccids[ccid->ccid_id] != NULL) {
		if (ccid->ccid_exit != NULL)
			ccid->ccid_exit(sk);
		module_put(ccid->ccid_owner);
	}

	ccids_read_unlock();
}

EXPORT_SYMBOL_GPL(ccid_exit);