aboutsummaryrefslogtreecommitdiff
path: root/drivers/net/danipc/danipc_ioctl.c
blob: ce90e91120063a5180d8083a97d979a4aaaf5c8c (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
	All files except if stated otherwise in the begining of the file
	are under the ISC license:
	----------------------------------------------------------------------

	Copyright (c) 2010-2012 Design Art Networks Ltd.

	Permission to use, copy, modify, and/or distribute this software for any
	purpose with or without fee is hereby granted, provided that the above
	copyright notice and this permission notice appear in all copies.

	THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
	WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
	MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
	ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
	WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
	ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
	OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/netdevice.h>
#include <linux/ioctl.h>

#include "danipc_k.h"
#include "ipc_api.h"
#include "danipc_lowlevel.h"


struct agent_data {
	struct task_struct	*task;
	pid_t			pid;
};
struct agent_data		agent_data[MAX_LOCAL_AGENT];

static int
is_process_alive(const struct agent_data *agent_data_p)
{
	int			rc = 0;
	struct task_struct	*task = find_task_by_vpid(agent_data_p->pid);

	if (task) {
		/* Paranoid check: task_struct exists, now verify it belongs
		 * to the correct process.
		 */
		if (agent_data_p->task == task &&
				agent_data_p->pid == task->pid)
			rc = 1;
	}

	return rc;
}

static void
ipc_gc(void)
{
	unsigned lid;

	for (lid = 0; lid < MAX_LOCAL_AGENT; lid++) {
		struct agent_data *agent_data_p = &agent_data[lid];

		if (agent_data_p->task && !is_process_alive(agent_data_p)) {
			agent_data_p->task	= NULL;
			agent_data_p->pid	= 0;
		}
	}
}


static int
danipc_strncmp(const char *cs, const char *ct, size_t cs_size, size_t ct_size)
{
	return ((strnlen(cs, cs_size) == strnlen(ct, ct_size)) &&
		(strcmp(cs, ct) == 0));
}

/* Second registration is allowed only for the same process. */
static int
second_registration(struct danipc_reg *danipc_reg_p, const int aid,
			const unsigned lid)
{
	return (danipc_strncmp(agent_table[aid].name, danipc_reg_p->name,
			MAX_AGENT_NAME_LEN, MAX_AGENT_NAME) &&
		agent_data[lid].task == current) ? 1 : 0;
}

static int
register_agent(struct net_device *dev, struct danipc_reg *danipc_reg_p)
{
	struct danipc_reg	danipc_reg;
	unsigned		r_lid;
	unsigned		agent_id = INVALID_ID;
	int			rc = 0;

	ipc_gc();

	if (copy_from_user(&danipc_reg, danipc_reg_p, sizeof(danipc_reg)))
		return -EFAULT;

	r_lid = danipc_reg.requested_lid;

	if (r_lid != INVALID_ID) {
		const unsigned r_aid = __IPC_AGENT_ID(LOCAL_IPC_ID, r_lid);

		/* Requested ID is not used, so assign it */
		if (!*agent_table[r_aid].name ||
		    second_registration(&danipc_reg, r_aid, r_lid) ||
		    (danipc_strncmp(danipc_reg.name,
				agent_table[r_aid].name,
				MAX_AGENT_NAME, MAX_AGENT_NAME_LEN) &&
			 agent_data[r_lid].task == NULL)) {
			if (put_user(r_lid, &danipc_reg_p->assigned_lid))
				return -EFAULT;
			agent_id = r_aid;
		}
	}

	if (agent_id == INVALID_ID) {
		unsigned	lid;
		/* Scan for the ID already assigned */
		for (lid = 0; lid < MAX_LOCAL_AGENT; lid++) {
			const unsigned aid = __IPC_AGENT_ID(LOCAL_IPC_ID, lid);
			if (danipc_strncmp(danipc_reg.name,
				agent_table[aid].name,
					MAX_AGENT_NAME, MAX_AGENT_NAME_LEN)
					&& agent_data[lid].task == NULL) {
				if (put_user(lid, &danipc_reg_p->assigned_lid))
					return -EFAULT;
				agent_id = aid;
				break;
			}
		}
	}

	if (agent_id == INVALID_ID) {
		unsigned	lid;
		/* Scan for the 1st free ID */
		for (lid = 0; lid < MAX_LOCAL_AGENT; lid++) {
			const unsigned aid = __IPC_AGENT_ID(LOCAL_IPC_ID, lid);
			if (!*agent_table[aid].name ||
				second_registration(&danipc_reg, aid, lid)) {
				if (put_user(lid, &danipc_reg_p->assigned_lid))
					return -EFAULT;
				agent_id = aid;
				break;
			}
		}
	}

	if (agent_id != INVALID_ID) {
		const unsigned an_siz = sizeof(agent_table[agent_id].name);
		const uint16_t cookie = cpu_to_be16(AGENTID_TO_COOKIE(agent_id,
							danipc_reg.prio));
		strlcpy(agent_table[agent_id].name, danipc_reg.name,
			an_siz);
		agent_table[agent_id].name[an_siz-1] = 0;
		if (put_user(cookie, &danipc_reg_p->cookie))
			rc = -EFAULT;
		agent_data[ipc_lid(agent_id)].task	= current;
		agent_data[ipc_lid(agent_id)].pid	= current->pid;
		netdev_dbg(dev,
			"%s: agent_id=0x%x assigned_lid=0x%x agent_table[]=\"%s\"\n",
			__func__, agent_id, danipc_reg.assigned_lid,
			agent_table[agent_id].name);
	} else
		rc = -ENOBUFS;

	return rc;
}


static int
get_name_by_addr(struct net_device *dev, struct danipc_name *danipc_name_p)
{
	int			rc = -ENODATA;
	danipc_addr_t		addr;

	if (get_user(addr, &danipc_name_p->addr))
		return -EFAULT;

	if (*agent_table[addr].name) {
		if (copy_to_user(danipc_name_p->name,
				agent_table[danipc_name_p->addr].name,
				sizeof(danipc_name_p->name)))
			rc = -EFAULT;
		else
			rc = 0;
		netdev_dbg(dev, "%s(): addr=0x%x -> name=%s\n", __func__,
			addr, agent_table[danipc_name_p->addr].name);
	}

	return rc;
}


static int
get_addr_by_name(struct net_device *dev, struct danipc_name *danipc_name_p)
{
	char			name[MAX_AGENT_NAME];
	int			rc = -ENODATA;
	unsigned		aid;

	if (copy_from_user(name, danipc_name_p->name, sizeof(name)))
		return -EFAULT;

	for (aid = 0; aid < MAX_AGENTS; aid++) {
		if (danipc_strncmp(name, agent_table[aid].name,
				MAX_AGENT_NAME, MAX_AGENT_NAME_LEN)) {
			const unsigned cpuid = aid / MAX_LOCAL_AGENT;
			const unsigned lid = aid % MAX_LOCAL_AGENT;
			if (put_user(__IPC_AGENT_ID(cpuid, lid),
						&danipc_name_p->addr))
				rc = -EFAULT;
			else
				rc = 0;
			netdev_dbg(dev, "%s: name=%s -> addr=0x%x\n", __func__,
				agent_table[aid].name, aid);
		}
	}

	return rc;
}



int danipc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	int			rc = -EINVAL;

	if (dev && ifr && ifr->ifr_data) {
		struct danipc_priv *priv = netdev_priv(dev);

		mutex_lock(&priv->lock);
		switch (cmd) {
		case DANIPC_IOCS_REGISTER:
			rc = register_agent(dev,
					(struct danipc_reg *)ifr->ifr_data);
			break;
		case DANIPC_IOCG_ADDR2NAME:
			rc = get_name_by_addr(dev,
					(struct danipc_name *)ifr->ifr_data);
			break;
		case DANIPC_IOCG_NAME2ADDR:
			rc = get_addr_by_name(dev,
					(struct danipc_name *)ifr->ifr_data);
			break;
		}
		mutex_unlock(&priv->lock);
	}
	return rc;
}