summaryrefslogtreecommitdiffstats
path: root/net/decnet/dn_raw.c
blob: 90e9b2bad72fb0c5b1f9b971505a8711a0a3e30e (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * DECnet       An implementation of the DECnet protocol suite for the LINUX
 *              operating system.  DECnet is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              DECnet Raw Sockets Interface
 *
 * Author:      Steve Whitehouse <SteveW@ACM.org>
 *
 *
 * Changes:
 *           Steve Whitehouse - connect() function.
 *           Steve Whitehouse - SMP changes, removed MOP stubs. MOP will
 *                              be userland only.
 */

#include <linux/config.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <net/sock.h>
#include <net/dst.h>
#include <net/dn.h>
#include <net/dn_raw.h>
#include <net/dn_route.h>

static rwlock_t dn_raw_hash_lock = RW_LOCK_UNLOCKED;
static struct sock *dn_raw_nsp_sklist = NULL;
static struct sock *dn_raw_routing_sklist = NULL;

static void dn_raw_hash(struct sock *sk)
{
	struct sock **skp;

	switch(sk->protocol) {
		case DNPROTO_NSP:
			skp = &dn_raw_nsp_sklist;
			break;
		case DNPROTO_ROU:
			skp = &dn_raw_routing_sklist;
			break;
		default:
			printk(KERN_DEBUG "dn_raw_hash: Unknown protocol\n");
			return;
	}

	write_lock_bh(&dn_raw_hash_lock);
	sk->next = *skp;
	sk->pprev = skp;
	*skp = sk;
	write_unlock_bh(&dn_raw_hash_lock);
}

static void dn_raw_unhash(struct sock *sk)
{
	struct sock **skp = sk->pprev;

	if (skp == NULL)
		return;

	write_lock_bh(&dn_raw_hash_lock);
	while(*skp != sk)
		skp = &((*skp)->next);
	*skp = sk->next;
	write_unlock_bh(&dn_raw_hash_lock);

	sk->next = NULL;
	sk->pprev = NULL;
}

static void dn_raw_autobind(struct sock *sk)
{
	dn_raw_hash(sk);
	sk->zapped = 0;
}

static int dn_raw_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	if (sk == NULL)
		return 0;

	if (!sk->dead) sk->state_change(sk);

	sk->dead = 1;
	sk->socket = NULL;
	sock->sk = NULL;

	dn_raw_unhash(sk);
	sock_put(sk);

	return 0;
}

/*
 * Bind does odd things with raw sockets. Its basically used to filter
 * the incoming packets, but this differs with the different layers
 * at which you extract packets.
 *
 * For Routing layer sockets, the object name is a host ordered unsigned
 * short which is a mask for the 16 different types of possible routing
 * packet. I'd like to also select by destination address of the packets
 * but alas, this is rather too difficult to do at the moment.
 */
static int dn_raw_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
	struct sock *sk = sock->sk;
	struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;

	if (addr_len != sizeof(struct sockaddr_dn))
		return -EINVAL;

	if (sk->zapped == 0)
		return -EINVAL;

	switch(sk->protocol) {
		case DNPROTO_ROU:
			if (dn_ntohs(addr->sdn_objnamel) && (dn_ntohs(addr->sdn_objnamel) != 2))
				return -EINVAL;
			/* Fall through here */
		case DNPROTO_NSP:
			if (dn_ntohs(addr->sdn_add.a_len) && (dn_ntohs(addr->sdn_add.a_len) != 2))
				return -EINVAL;
			break;
		default:
			return -EPROTONOSUPPORT;
	}

	if (dn_ntohs(addr->sdn_objnamel) > (DN_MAXOBJL-1))
		return -EINVAL;

	if (dn_ntohs(addr->sdn_add.a_len) > DN_MAXADDL)
		return -EINVAL;

	memcpy(&sk->protinfo.dn.addr, addr, sizeof(struct sockaddr_dn));

	dn_raw_autobind(sk);

	return 0;
}

/*
 * This is to allow send() and write() to work. You set the destination address
 * with this function.
 */
static int dn_raw_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
{
	struct sock *sk = sock->sk;
	struct dn_scp *scp = &sk->protinfo.dn;
	struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
	int err;

	lock_sock(sk);

	err = -EINVAL;
	if (addr_len != sizeof(struct sockaddr_dn))
		goto out;

	if (saddr->sdn_family != AF_DECnet)
		goto out;

	if (dn_ntohs(saddr->sdn_objnamel) > (DN_MAXOBJL-1))
		goto out;

	if (dn_ntohs(saddr->sdn_add.a_len) > DN_MAXADDL)
		goto out;

	if (sk->zapped)
		dn_raw_autobind(sk);

	if ((err = dn_route_output(&sk->dst_cache, dn_saddr2dn(saddr), dn_saddr2dn(&scp->addr), 0)) < 0)
		goto out;

	memcpy(&scp->peer, saddr, sizeof(struct sockaddr_dn));
out:
	release_sock(sk);

	return err;
}

/*
 * TBD.
 */
static int dn_raw_sendmsg(struct socket *sock, struct msghdr *hdr, int size,
			struct scm_cookie *scm)
{
	struct sock *sk = sock->sk;

	if (sk->zapped)
		dn_raw_autobind(sk);

	if (sk->protocol != DNPROTO_NSP)
		return -EOPNOTSUPP;

	return 0;
}

/*
 * This works fine, execpt that it doesn't report the originating address
 * or anything at the moment.
 */
static int dn_raw_recvmsg(struct socket *sock, struct msghdr *msg, int size,
			int flags, struct scm_cookie *scm)
{
	struct sock *sk = sock->sk;
	struct sk_buff *skb;
	int err = 0;
	int copied = 0;

	lock_sock(sk);

	if (sk->zapped)
		dn_raw_autobind(sk);

	if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &err)) == NULL)
		goto out;

	copied = skb->len;

	if (copied > size) {
		copied = size;
		msg->msg_flags |= MSG_TRUNC;
	}

	if ((err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied)) != 0) {
		if (flags & MSG_PEEK)
			atomic_dec(&skb->users);
		else
			skb_queue_head(&sk->receive_queue, skb);

		goto out;
	}

	skb_free_datagram(sk, skb);

out:
	release_sock(sk);

	return copied ? copied : err;
}

struct proto_ops dn_raw_proto_ops = {
	AF_DECnet,

	dn_raw_release,
	dn_raw_bind,
	dn_raw_connect,
	sock_no_socketpair,
	sock_no_accept,
	sock_no_getname,
	datagram_poll,
	sock_no_ioctl,
	sock_no_listen,
	sock_no_shutdown,
	sock_no_setsockopt,
	sock_no_getsockopt,
	sock_no_fcntl,
	dn_raw_sendmsg,
	dn_raw_recvmsg,
	sock_no_mmap
};

#ifdef CONFIG_PROC_FS
int dn_raw_get_info(char *buffer, char **start, off_t offset, int length)
{
	int len = 0;
	off_t pos = 0;
	off_t begin = 0;
	struct sock *sk;

	read_lock_bh(&dn_raw_hash_lock);
	for(sk = dn_raw_nsp_sklist; sk; sk = sk->next) {	
		len += sprintf(buffer+len, "NSP\n");

		pos = begin + len;

		if (pos < offset) {
			len = 0;
			begin = pos;
		}

		if (pos > offset + length)
			goto all_done;

	}

	for(sk = dn_raw_routing_sklist; sk; sk = sk->next) {
		len += sprintf(buffer+len, "ROU\n");

		pos = begin + len;

		if (pos < offset) {
			len = 0;
			begin = pos;
		}

		if (pos > offset + length)
			goto all_done;
	}

all_done:
	read_unlock_bh(&dn_raw_hash_lock);

	*start = buffer + (offset - begin);
	len -= (offset - begin);

	if (len > length) len = length;

	return(len);
}
#endif /* CONFIG_PROC_FS */

void dn_raw_rx_nsp(struct sk_buff *skb)
{
	struct sock *sk;
	struct sk_buff *skb2;

	read_lock(&dn_raw_hash_lock);
	for(sk = dn_raw_nsp_sklist; sk != NULL; sk = sk->next) {
		if (skb->len > sock_rspace(sk))
			continue;
		if (sk->dead)
			continue;
		if ((skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
			skb_set_owner_r(skb2, sk);
			skb_queue_tail(&sk->receive_queue, skb2);
			sk->data_ready(sk, skb->len);	
		}
	}
	read_unlock(&dn_raw_hash_lock);
}

void dn_raw_rx_routing(struct sk_buff *skb)
{
	struct sock *sk;
	struct sk_buff *skb2;
	struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
	unsigned short rt_flagmask;
	unsigned short objnamel;
	struct dn_scp *scp;

	read_lock(&dn_raw_hash_lock);
	for(sk = dn_raw_routing_sklist; sk != NULL; sk = sk->next) {
		if (skb->len > sock_rspace(sk))
			continue;
		if (sk->dead)
			continue;
		scp = &sk->protinfo.dn;

		rt_flagmask = dn_ntohs(*(unsigned short *)scp->addr.sdn_objname);
		objnamel = dn_ntohs(scp->addr.sdn_objnamel);

		if ((objnamel == 2) && (!((1 << (cb->rt_flags & 0x0f)) & rt_flagmask)))
			continue;

		if ((skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
			skb_set_owner_r(skb2, sk);
			skb_queue_tail(&sk->receive_queue, skb2);
			sk->data_ready(sk, skb->len);
		}
	}
	read_unlock(&dn_raw_hash_lock);
}