blob: a205eaa87f5257429ec4520a3766d3137645d4f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * DECnet Socket Layer Interface
8 *
9 * Authors: Eduardo Marcelo Serrat <emserrat@geocities.com>
10 * Patrick Caulfield <patrick@pandh.demon.co.uk>
11 *
12 * Changes:
13 * Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's
14 * version of the code. Original copyright preserved
15 * below.
16 * Steve Whitehouse: Some bug fixes, cleaning up some code to make it
17 * compatible with my routing layer.
18 * Steve Whitehouse: Merging changes from Eduardo Serrat and Patrick
19 * Caulfield.
20 * Steve Whitehouse: Further bug fixes, checking module code still works
21 * with new routing layer.
22 * Steve Whitehouse: Additional set/get_sockopt() calls.
23 * Steve Whitehouse: Fixed TIOCINQ ioctl to be same as Eduardo's new
24 * code.
25 * Steve Whitehouse: recvmsg() changed to try and behave in a POSIX like
26 * way. Didn't manage it entirely, but its better.
27 * Steve Whitehouse: ditto for sendmsg().
28 * Steve Whitehouse: A selection of bug fixes to various things.
29 * Steve Whitehouse: Added TIOCOUTQ ioctl.
30 * Steve Whitehouse: Fixes to username2sockaddr & sockaddr2username.
31 * Steve Whitehouse: Fixes to connect() error returns.
32 * Patrick Caulfield: Fixes to delayed acceptance logic.
33 * David S. Miller: New socket locking
34 * Steve Whitehouse: Socket list hashing/locking
35 * Arnaldo C. Melo: use capable, not suser
36 * Steve Whitehouse: Removed unused code. Fix to use sk->allocation
37 * when required.
38 * Patrick Caulfield: /proc/net/decnet now has object name/number
39 * Steve Whitehouse: Fixed local port allocation, hashed sk list
40 * Matthew Wilcox: Fixes for dn_ioctl()
41 * Steve Whitehouse: New connect/accept logic to allow timeouts and
42 * prepare for sendpage etc.
43 */
44
45
46/******************************************************************************
47 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 This program is free software; you can redistribute it and/or modify
50 it under the terms of the GNU General Public License as published by
51 the Free Software Foundation; either version 2 of the License, or
52 any later version.
53
54 This program is distributed in the hope that it will be useful,
55 but WITHOUT ANY WARRANTY; without even the implied warranty of
56 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 GNU General Public License for more details.
58
59HISTORY:
60
61Version Kernel Date Author/Comments
62------- ------ ---- ---------------
63Version 0.0.1 2.0.30 01-dic-97 Eduardo Marcelo Serrat
64 (emserrat@geocities.com)
65
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090066 First Development of DECnet Socket La-
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 yer for Linux. Only supports outgoing
68 connections.
69
70Version 0.0.2 2.1.105 20-jun-98 Patrick J. Caulfield
71 (patrick@pandh.demon.co.uk)
72
73 Port to new kernel development version.
74
75Version 0.0.3 2.1.106 25-jun-98 Eduardo Marcelo Serrat
76 (emserrat@geocities.com)
77 _
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090078 Added support for incoming connections
79 so we can start developing server apps
80 on Linux.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 -
82 Module Support
83Version 0.0.4 2.1.109 21-jul-98 Eduardo Marcelo Serrat
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090084 (emserrat@geocities.com)
85 _
86 Added support for X11R6.4. Now we can
87 use DECnet transport for X on Linux!!!
88 -
Linus Torvalds1da177e2005-04-16 15:20:36 -070089Version 0.0.5 2.1.110 01-aug-98 Eduardo Marcelo Serrat
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090090 (emserrat@geocities.com)
91 Removed bugs on flow control
92 Removed bugs on incoming accessdata
93 order
94 -
Linus Torvalds1da177e2005-04-16 15:20:36 -070095Version 0.0.6 2.1.110 07-aug-98 Eduardo Marcelo Serrat
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090096 dn_recvmsg fixes
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090098 Patrick J. Caulfield
99 dn_bind fixes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100*******************************************************************************/
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/module.h>
103#include <linux/errno.h>
104#include <linux/types.h>
105#include <linux/slab.h>
106#include <linux/socket.h>
107#include <linux/in.h>
108#include <linux/kernel.h>
109#include <linux/sched.h>
110#include <linux/timer.h>
111#include <linux/string.h>
112#include <linux/sockios.h>
113#include <linux/net.h>
114#include <linux/netdevice.h>
115#include <linux/inet.h>
116#include <linux/route.h>
117#include <linux/netfilter.h>
118#include <linux/seq_file.h>
119#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -0700120#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <net/flow.h>
122#include <asm/system.h>
123#include <asm/ioctls.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800124#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#include <linux/mm.h>
126#include <linux/interrupt.h>
127#include <linux/proc_fs.h>
128#include <linux/stat.h>
129#include <linux/init.h>
130#include <linux/poll.h>
131#include <net/neighbour.h>
132#include <net/dst.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700133#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#include <net/dn.h>
135#include <net/dn_nsp.h>
136#include <net/dn_dev.h>
137#include <net/dn_route.h>
138#include <net/dn_fib.h>
139#include <net/dn_neigh.h>
140
141struct dn_sock {
142 struct sock sk;
143 struct dn_scp scp;
144};
145
146static void dn_keepalive(struct sock *sk);
147
148#define DN_SK_HASH_SHIFT 8
149#define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
150#define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
151
152
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800153static const struct proto_ops dn_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static DEFINE_RWLOCK(dn_hash_lock);
155static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
156static struct hlist_head dn_wild_sk;
Steven Whitehouse1f12bcc2005-12-05 13:42:06 -0800157static atomic_t decnet_memory_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen, int flags);
160static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
161
162static struct hlist_head *dn_find_list(struct sock *sk)
163{
164 struct dn_scp *scp = DN_SK(sk);
165
166 if (scp->addr.sdn_flags & SDF_WILD)
167 return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;
168
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800169 return &dn_sk_hash[dn_ntohs(scp->addrloc) & DN_SK_HASH_MASK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Valid ports are those greater than zero and not already in use.
174 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800175static int check_port(__le16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct sock *sk;
178 struct hlist_node *node;
179
180 if (port == 0)
181 return -1;
182
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800183 sk_for_each(sk, node, &dn_sk_hash[dn_ntohs(port) & DN_SK_HASH_MASK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct dn_scp *scp = DN_SK(sk);
185 if (scp->addrloc == port)
186 return -1;
187 }
188 return 0;
189}
190
191static unsigned short port_alloc(struct sock *sk)
192{
193 struct dn_scp *scp = DN_SK(sk);
194static unsigned short port = 0x2000;
195 unsigned short i_port = port;
196
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800197 while(check_port(dn_htons(++port)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (port == i_port)
199 return 0;
200 }
201
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800202 scp->addrloc = dn_htons(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 return 1;
205}
206
207/*
208 * Since this is only ever called from user
209 * level, we don't need a write_lock() version
210 * of this.
211 */
212static int dn_hash_sock(struct sock *sk)
213{
214 struct dn_scp *scp = DN_SK(sk);
215 struct hlist_head *list;
216 int rv = -EUSERS;
217
218 BUG_ON(sk_hashed(sk));
219
220 write_lock_bh(&dn_hash_lock);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!scp->addrloc && !port_alloc(sk))
223 goto out;
224
225 rv = -EADDRINUSE;
226 if ((list = dn_find_list(sk)) == NULL)
227 goto out;
228
229 sk_add_node(sk, list);
230 rv = 0;
231out:
232 write_unlock_bh(&dn_hash_lock);
233 return rv;
234}
235
236static void dn_unhash_sock(struct sock *sk)
237{
238 write_lock(&dn_hash_lock);
239 sk_del_node_init(sk);
240 write_unlock(&dn_hash_lock);
241}
242
243static void dn_unhash_sock_bh(struct sock *sk)
244{
245 write_lock_bh(&dn_hash_lock);
246 sk_del_node_init(sk);
247 write_unlock_bh(&dn_hash_lock);
248}
249
250static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
251{
252 int i;
253 unsigned hash = addr->sdn_objnum;
254
255 if (hash == 0) {
256 hash = addr->sdn_objnamel;
257 for(i = 0; i < dn_ntohs(addr->sdn_objnamel); i++) {
258 hash ^= addr->sdn_objname[i];
259 hash ^= (hash << 3);
260 }
261 }
262
263 return &dn_sk_hash[hash & DN_SK_HASH_MASK];
264}
265
266/*
267 * Called to transform a socket from bound (i.e. with a local address)
268 * into a listening socket (doesn't need a local port number) and rehashes
269 * based upon the object name/number.
270 */
271static void dn_rehash_sock(struct sock *sk)
272{
273 struct hlist_head *list;
274 struct dn_scp *scp = DN_SK(sk);
275
276 if (scp->addr.sdn_flags & SDF_WILD)
277 return;
278
279 write_lock_bh(&dn_hash_lock);
280 sk_del_node_init(sk);
281 DN_SK(sk)->addrloc = 0;
282 list = listen_hash(&DN_SK(sk)->addr);
283 sk_add_node(sk, list);
284 write_unlock_bh(&dn_hash_lock);
285}
286
287int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
288{
289 int len = 2;
290
291 *buf++ = type;
292
293 switch(type) {
294 case 0:
295 *buf++ = sdn->sdn_objnum;
296 break;
297 case 1:
298 *buf++ = 0;
299 *buf++ = dn_ntohs(sdn->sdn_objnamel);
300 memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
301 len = 3 + dn_ntohs(sdn->sdn_objnamel);
302 break;
303 case 2:
304 memset(buf, 0, 5);
305 buf += 5;
306 *buf++ = dn_ntohs(sdn->sdn_objnamel);
307 memcpy(buf, sdn->sdn_objname, dn_ntohs(sdn->sdn_objnamel));
308 len = 7 + dn_ntohs(sdn->sdn_objnamel);
309 break;
310 }
311
312 return len;
313}
314
315/*
316 * On reception of usernames, we handle types 1 and 0 for destination
317 * addresses only. Types 2 and 4 are used for source addresses, but the
318 * UIC, GIC are ignored and they are both treated the same way. Type 3
319 * is never used as I've no idea what its purpose might be or what its
320 * format is.
321 */
322int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
323{
324 unsigned char type;
325 int size = len;
326 int namel = 12;
327
328 sdn->sdn_objnum = 0;
329 sdn->sdn_objnamel = dn_htons(0);
330 memset(sdn->sdn_objname, 0, DN_MAXOBJL);
331
332 if (len < 2)
333 return -1;
334
335 len -= 2;
336 *fmt = *data++;
337 type = *data++;
338
339 switch(*fmt) {
340 case 0:
341 sdn->sdn_objnum = type;
342 return 2;
343 case 1:
344 namel = 16;
345 break;
346 case 2:
347 len -= 4;
348 data += 4;
349 break;
350 case 4:
351 len -= 8;
352 data += 8;
353 break;
354 default:
355 return -1;
356 }
357
358 len -= 1;
359
360 if (len < 0)
361 return -1;
362
363 sdn->sdn_objnamel = dn_htons(*data++);
364 len -= dn_ntohs(sdn->sdn_objnamel);
365
366 if ((len < 0) || (dn_ntohs(sdn->sdn_objnamel) > namel))
367 return -1;
368
369 memcpy(sdn->sdn_objname, data, dn_ntohs(sdn->sdn_objnamel));
370
371 return size - len;
372}
373
374struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
375{
376 struct hlist_head *list = listen_hash(addr);
377 struct hlist_node *node;
378 struct sock *sk;
379
380 read_lock(&dn_hash_lock);
381 sk_for_each(sk, node, list) {
382 struct dn_scp *scp = DN_SK(sk);
383 if (sk->sk_state != TCP_LISTEN)
384 continue;
385 if (scp->addr.sdn_objnum) {
386 if (scp->addr.sdn_objnum != addr->sdn_objnum)
387 continue;
388 } else {
389 if (addr->sdn_objnum)
390 continue;
391 if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
392 continue;
393 if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, dn_ntohs(addr->sdn_objnamel)) != 0)
394 continue;
395 }
396 sock_hold(sk);
397 read_unlock(&dn_hash_lock);
398 return sk;
399 }
400
401 sk = sk_head(&dn_wild_sk);
402 if (sk) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900403 if (sk->sk_state == TCP_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sock_hold(sk);
405 else
406 sk = NULL;
407 }
408
409 read_unlock(&dn_hash_lock);
410 return sk;
411}
412
413struct sock *dn_find_by_skb(struct sk_buff *skb)
414{
415 struct dn_skb_cb *cb = DN_SKB_CB(skb);
416 struct sock *sk;
417 struct hlist_node *node;
418 struct dn_scp *scp;
419
420 read_lock(&dn_hash_lock);
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800421 sk_for_each(sk, node, &dn_sk_hash[dn_ntohs(cb->dst_port) & DN_SK_HASH_MASK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 scp = DN_SK(sk);
423 if (cb->src != dn_saddr2dn(&scp->peer))
424 continue;
425 if (cb->dst_port != scp->addrloc)
426 continue;
427 if (scp->addrrem && (cb->src_port != scp->addrrem))
428 continue;
429 sock_hold(sk);
430 goto found;
431 }
432 sk = NULL;
433found:
434 read_unlock(&dn_hash_lock);
435 return sk;
436}
437
438
439
440static void dn_destruct(struct sock *sk)
441{
442 struct dn_scp *scp = DN_SK(sk);
443
444 skb_queue_purge(&scp->data_xmit_queue);
445 skb_queue_purge(&scp->other_xmit_queue);
446 skb_queue_purge(&scp->other_receive_queue);
447
448 dst_release(xchg(&sk->sk_dst_cache, NULL));
449}
450
Steven Whitehouse1f12bcc2005-12-05 13:42:06 -0800451static int dn_memory_pressure;
452
453static void dn_enter_memory_pressure(void)
454{
455 if (!dn_memory_pressure) {
456 dn_memory_pressure = 1;
457 }
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static struct proto dn_proto = {
Steven Whitehouse1f12bcc2005-12-05 13:42:06 -0800461 .name = "NSP",
462 .owner = THIS_MODULE,
463 .enter_memory_pressure = dn_enter_memory_pressure,
464 .memory_pressure = &dn_memory_pressure,
465 .memory_allocated = &decnet_memory_allocated,
466 .sysctl_mem = sysctl_decnet_mem,
467 .sysctl_wmem = sysctl_decnet_wmem,
468 .sysctl_rmem = sysctl_decnet_rmem,
469 .max_header = DN_MAX_NSP_DATA_HEADER + 64,
470 .obj_size = sizeof(struct dn_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Al Virodd0fc662005-10-07 07:46:04 +0100473static struct sock *dn_alloc_sock(struct socket *sock, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct dn_scp *scp;
476 struct sock *sk = sk_alloc(PF_DECnet, gfp, &dn_proto, 1);
477
478 if (!sk)
479 goto out;
480
481 if (sock)
482 sock->ops = &dn_proto_ops;
483 sock_init_data(sock, sk);
484
485 sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
486 sk->sk_destruct = dn_destruct;
487 sk->sk_no_check = 1;
488 sk->sk_family = PF_DECnet;
489 sk->sk_protocol = 0;
490 sk->sk_allocation = gfp;
Steven Whitehouse1f12bcc2005-12-05 13:42:06 -0800491 sk->sk_sndbuf = sysctl_decnet_wmem[1];
492 sk->sk_rcvbuf = sysctl_decnet_rmem[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Initialization of DECnet Session Control Port */
495 scp = DN_SK(sk);
496 scp->state = DN_O; /* Open */
497 scp->numdat = 1; /* Next data seg to tx */
498 scp->numoth = 1; /* Next oth data to tx */
499 scp->ackxmt_dat = 0; /* Last data seg ack'ed */
500 scp->ackxmt_oth = 0; /* Last oth data ack'ed */
501 scp->ackrcv_dat = 0; /* Highest data ack recv*/
502 scp->ackrcv_oth = 0; /* Last oth data ack rec*/
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900503 scp->flowrem_sw = DN_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 scp->flowloc_sw = DN_SEND;
505 scp->flowrem_dat = 0;
506 scp->flowrem_oth = 1;
507 scp->flowloc_dat = 0;
508 scp->flowloc_oth = 1;
509 scp->services_rem = 0;
510 scp->services_loc = 1 | NSP_FC_NONE;
511 scp->info_rem = 0;
512 scp->info_loc = 0x03; /* NSP version 4.1 */
513 scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
514 scp->nonagle = 0;
515 scp->multi_ireq = 1;
516 scp->accept_mode = ACC_IMMED;
517 scp->addr.sdn_family = AF_DECnet;
518 scp->peer.sdn_family = AF_DECnet;
519 scp->accessdata.acc_accl = 5;
520 memcpy(scp->accessdata.acc_acc, "LINUX", 5);
521
522 scp->max_window = NSP_MAX_WINDOW;
523 scp->snd_window = NSP_MIN_WINDOW;
524 scp->nsp_srtt = NSP_INITIAL_SRTT;
525 scp->nsp_rttvar = NSP_INITIAL_RTTVAR;
526 scp->nsp_rxtshift = 0;
527
528 skb_queue_head_init(&scp->data_xmit_queue);
529 skb_queue_head_init(&scp->other_xmit_queue);
530 skb_queue_head_init(&scp->other_receive_queue);
531
532 scp->persist = 0;
533 scp->persist_fxn = NULL;
534 scp->keepalive = 10 * HZ;
535 scp->keepalive_fxn = dn_keepalive;
536
537 init_timer(&scp->delack_timer);
538 scp->delack_pending = 0;
539 scp->delack_fxn = dn_nsp_delayed_ack;
540
541 dn_start_slow_timer(sk);
542out:
543 return sk;
544}
545
546/*
547 * Keepalive timer.
548 * FIXME: Should respond to SO_KEEPALIVE etc.
549 */
550static void dn_keepalive(struct sock *sk)
551{
552 struct dn_scp *scp = DN_SK(sk);
553
554 /*
555 * By checking the other_data transmit queue is empty
556 * we are double checking that we are not sending too
557 * many of these keepalive frames.
558 */
David S. Millerb03efcf2005-07-08 14:57:23 -0700559 if (skb_queue_empty(&scp->other_xmit_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
561}
562
563
564/*
565 * Timer for shutdown/destroyed sockets.
566 * When socket is dead & no packets have been sent for a
567 * certain amount of time, they are removed by this
568 * routine. Also takes care of sending out DI & DC
569 * frames at correct times.
570 */
571int dn_destroy_timer(struct sock *sk)
572{
573 struct dn_scp *scp = DN_SK(sk);
574
575 scp->persist = dn_nsp_persist(sk);
576
577 switch(scp->state) {
578 case DN_DI:
579 dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
580 if (scp->nsp_rxtshift >= decnet_di_count)
581 scp->state = DN_CN;
582 return 0;
583
584 case DN_DR:
585 dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
586 if (scp->nsp_rxtshift >= decnet_dr_count)
587 scp->state = DN_DRC;
588 return 0;
589
590 case DN_DN:
591 if (scp->nsp_rxtshift < decnet_dn_count) {
592 /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
593 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
594 return 0;
595 }
596 }
597
598 scp->persist = (HZ * decnet_time_wait);
599
600 if (sk->sk_socket)
601 return 0;
602
603 if ((jiffies - scp->stamp) >= (HZ * decnet_time_wait)) {
604 dn_unhash_sock(sk);
605 sock_put(sk);
606 return 1;
607 }
608
609 return 0;
610}
611
612static void dn_destroy_sock(struct sock *sk)
613{
614 struct dn_scp *scp = DN_SK(sk);
615
616 scp->nsp_rxtshift = 0; /* reset back off */
617
618 if (sk->sk_socket) {
619 if (sk->sk_socket->state != SS_UNCONNECTED)
620 sk->sk_socket->state = SS_DISCONNECTING;
621 }
622
623 sk->sk_state = TCP_CLOSE;
624
625 switch(scp->state) {
626 case DN_DN:
627 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
628 sk->sk_allocation);
629 scp->persist_fxn = dn_destroy_timer;
630 scp->persist = dn_nsp_persist(sk);
631 break;
632 case DN_CR:
633 scp->state = DN_DR;
634 goto disc_reject;
635 case DN_RUN:
636 scp->state = DN_DI;
637 case DN_DI:
638 case DN_DR:
639disc_reject:
640 dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
641 case DN_NC:
642 case DN_NR:
643 case DN_RJ:
644 case DN_DIC:
645 case DN_CN:
646 case DN_DRC:
647 case DN_CI:
648 case DN_CD:
649 scp->persist_fxn = dn_destroy_timer;
650 scp->persist = dn_nsp_persist(sk);
651 break;
652 default:
653 printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
654 case DN_O:
655 dn_stop_slow_timer(sk);
656
657 dn_unhash_sock_bh(sk);
658 sock_put(sk);
659
660 break;
661 }
662}
663
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800664char *dn_addr2asc(__u16 addr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 unsigned short node, area;
667
668 node = addr & 0x03ff;
669 area = addr >> 10;
670 sprintf(buf, "%hd.%hd", area, node);
671
672 return buf;
673}
674
675
676
677static int dn_create(struct socket *sock, int protocol)
678{
679 struct sock *sk;
680
681 switch(sock->type) {
682 case SOCK_SEQPACKET:
683 if (protocol != DNPROTO_NSP)
684 return -EPROTONOSUPPORT;
685 break;
686 case SOCK_STREAM:
687 break;
688 default:
689 return -ESOCKTNOSUPPORT;
690 }
691
692
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900693 if ((sk = dn_alloc_sock(sock, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -ENOBUFS;
695
696 sk->sk_protocol = protocol;
697
698 return 0;
699}
700
701
702static int
703dn_release(struct socket *sock)
704{
705 struct sock *sk = sock->sk;
706
707 if (sk) {
708 sock_orphan(sk);
709 sock_hold(sk);
710 lock_sock(sk);
711 dn_destroy_sock(sk);
712 release_sock(sk);
713 sock_put(sk);
714 }
715
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
720{
721 struct sock *sk = sock->sk;
722 struct dn_scp *scp = DN_SK(sk);
723 struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
724 struct net_device *dev;
725 int rv;
726
727 if (addr_len != sizeof(struct sockaddr_dn))
728 return -EINVAL;
729
730 if (saddr->sdn_family != AF_DECnet)
731 return -EINVAL;
732
733 if (dn_ntohs(saddr->sdn_nodeaddrl) && (dn_ntohs(saddr->sdn_nodeaddrl) != 2))
734 return -EINVAL;
735
736 if (dn_ntohs(saddr->sdn_objnamel) > DN_MAXOBJL)
737 return -EINVAL;
738
739 if (saddr->sdn_flags & ~SDF_WILD)
740 return -EINVAL;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
743 (saddr->sdn_flags & SDF_WILD)))
744 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (!(saddr->sdn_flags & SDF_WILD)) {
747 if (dn_ntohs(saddr->sdn_nodeaddrl)) {
748 read_lock(&dev_base_lock);
749 for(dev = dev_base; dev; dev = dev->next) {
750 if (!dev->dn_ptr)
751 continue;
752 if (dn_dev_islocal(dev, dn_saddr2dn(saddr)))
753 break;
754 }
755 read_unlock(&dev_base_lock);
756 if (dev == NULL)
757 return -EADDRNOTAVAIL;
758 }
759 }
760
761 rv = -EINVAL;
762 lock_sock(sk);
763 if (sock_flag(sk, SOCK_ZAPPED)) {
764 memcpy(&scp->addr, saddr, addr_len);
765 sock_reset_flag(sk, SOCK_ZAPPED);
766
767 rv = dn_hash_sock(sk);
768 if (rv)
769 sock_set_flag(sk, SOCK_ZAPPED);
770 }
771 release_sock(sk);
772
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900773 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776
777static int dn_auto_bind(struct socket *sock)
778{
779 struct sock *sk = sock->sk;
780 struct dn_scp *scp = DN_SK(sk);
781 int rv;
782
783 sock_reset_flag(sk, SOCK_ZAPPED);
784
785 scp->addr.sdn_flags = 0;
786 scp->addr.sdn_objnum = 0;
787
788 /*
789 * This stuff is to keep compatibility with Eduardo's
790 * patch. I hope I can dispense with it shortly...
791 */
792 if ((scp->accessdata.acc_accl != 0) &&
793 (scp->accessdata.acc_accl <= 12)) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 scp->addr.sdn_objnamel = dn_htons(scp->accessdata.acc_accl);
796 memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, dn_ntohs(scp->addr.sdn_objnamel));
797
798 scp->accessdata.acc_accl = 0;
799 memset(scp->accessdata.acc_acc, 0, 40);
800 }
801 /* End of compatibility stuff */
802
803 scp->addr.sdn_add.a_len = dn_htons(2);
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800804 rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (rv == 0) {
806 rv = dn_hash_sock(sk);
807 if (rv)
808 sock_set_flag(sk, SOCK_ZAPPED);
809 }
810
811 return rv;
812}
813
Al Virodd0fc662005-10-07 07:46:04 +0100814static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct dn_scp *scp = DN_SK(sk);
817 DEFINE_WAIT(wait);
818 int err;
819
820 if (scp->state != DN_CR)
821 return -EINVAL;
822
823 scp->state = DN_CC;
824 scp->segsize_loc = dst_metric(__sk_dst_get(sk), RTAX_ADVMSS);
825 dn_send_conn_conf(sk, allocation);
826
827 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
828 for(;;) {
829 release_sock(sk);
830 if (scp->state == DN_CC)
831 *timeo = schedule_timeout(*timeo);
832 lock_sock(sk);
833 err = 0;
834 if (scp->state == DN_RUN)
835 break;
836 err = sock_error(sk);
837 if (err)
838 break;
839 err = sock_intr_errno(*timeo);
840 if (signal_pending(current))
841 break;
842 err = -EAGAIN;
843 if (!*timeo)
844 break;
845 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
846 }
847 finish_wait(sk->sk_sleep, &wait);
848 if (err == 0) {
849 sk->sk_socket->state = SS_CONNECTED;
850 } else if (scp->state != DN_CC) {
851 sk->sk_socket->state = SS_UNCONNECTED;
852 }
853 return err;
854}
855
856static int dn_wait_run(struct sock *sk, long *timeo)
857{
858 struct dn_scp *scp = DN_SK(sk);
859 DEFINE_WAIT(wait);
860 int err = 0;
861
862 if (scp->state == DN_RUN)
863 goto out;
864
865 if (!*timeo)
866 return -EALREADY;
867
868 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
869 for(;;) {
870 release_sock(sk);
871 if (scp->state == DN_CI || scp->state == DN_CC)
872 *timeo = schedule_timeout(*timeo);
873 lock_sock(sk);
874 err = 0;
875 if (scp->state == DN_RUN)
876 break;
877 err = sock_error(sk);
878 if (err)
879 break;
880 err = sock_intr_errno(*timeo);
881 if (signal_pending(current))
882 break;
883 err = -ETIMEDOUT;
884 if (!*timeo)
885 break;
886 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
887 }
888 finish_wait(sk->sk_sleep, &wait);
889out:
890 if (err == 0) {
891 sk->sk_socket->state = SS_CONNECTED;
892 } else if (scp->state != DN_CI && scp->state != DN_CC) {
893 sk->sk_socket->state = SS_UNCONNECTED;
894 }
895 return err;
896}
897
898static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
899{
900 struct socket *sock = sk->sk_socket;
901 struct dn_scp *scp = DN_SK(sk);
902 int err = -EISCONN;
903 struct flowi fl;
904
905 if (sock->state == SS_CONNECTED)
906 goto out;
907
908 if (sock->state == SS_CONNECTING) {
909 err = 0;
910 if (scp->state == DN_RUN) {
911 sock->state = SS_CONNECTED;
912 goto out;
913 }
914 err = -ECONNREFUSED;
915 if (scp->state != DN_CI && scp->state != DN_CC) {
916 sock->state = SS_UNCONNECTED;
917 goto out;
918 }
919 return dn_wait_run(sk, timeo);
920 }
921
922 err = -EINVAL;
923 if (scp->state != DN_O)
924 goto out;
925
926 if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
927 goto out;
928 if (addr->sdn_family != AF_DECnet)
929 goto out;
930 if (addr->sdn_flags & SDF_WILD)
931 goto out;
932
933 if (sock_flag(sk, SOCK_ZAPPED)) {
934 err = dn_auto_bind(sk->sk_socket);
935 if (err)
936 goto out;
937 }
938
939 memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
940
941 err = -EHOSTUNREACH;
942 memset(&fl, 0, sizeof(fl));
943 fl.oif = sk->sk_bound_dev_if;
944 fl.fld_dst = dn_saddr2dn(&scp->peer);
945 fl.fld_src = dn_saddr2dn(&scp->addr);
946 dn_sk_ports_copy(&fl, scp);
947 fl.proto = DNPROTO_NSP;
948 if (dn_route_output_sock(&sk->sk_dst_cache, &fl, sk, flags) < 0)
949 goto out;
950 sk->sk_route_caps = sk->sk_dst_cache->dev->features;
951 sock->state = SS_CONNECTING;
952 scp->state = DN_CI;
953 scp->segsize_loc = dst_metric(sk->sk_dst_cache, RTAX_ADVMSS);
954
955 dn_nsp_send_conninit(sk, NSP_CI);
956 err = -EINPROGRESS;
957 if (*timeo) {
958 err = dn_wait_run(sk, timeo);
959 }
960out:
961 return err;
962}
963
964static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
965{
966 struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
967 struct sock *sk = sock->sk;
968 int err;
969 long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
970
971 lock_sock(sk);
972 err = __dn_connect(sk, addr, addrlen, &timeo, 0);
973 release_sock(sk);
974
975 return err;
976}
977
978static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
979{
980 struct dn_scp *scp = DN_SK(sk);
981
982 switch(scp->state) {
983 case DN_RUN:
984 return 0;
985 case DN_CR:
986 return dn_confirm_accept(sk, timeo, sk->sk_allocation);
987 case DN_CI:
988 case DN_CC:
989 return dn_wait_run(sk, timeo);
990 case DN_O:
991 return __dn_connect(sk, addr, addrlen, timeo, flags);
992 }
993
994 return -EINVAL;
995}
996
997
998static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
999{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001000 unsigned char *ptr = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001002 acc->acc_userl = *ptr++;
1003 memcpy(&acc->acc_user, ptr, acc->acc_userl);
1004 ptr += acc->acc_userl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001006 acc->acc_passl = *ptr++;
1007 memcpy(&acc->acc_pass, ptr, acc->acc_passl);
1008 ptr += acc->acc_passl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001010 acc->acc_accl = *ptr++;
1011 memcpy(&acc->acc_acc, ptr, acc->acc_accl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001013 skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015}
1016
1017static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
1018{
Steven Whitehouse375d9d72006-11-07 15:09:17 -08001019 unsigned char *ptr = skb->data;
1020 u16 len = *ptr++; /* yes, it's 8bit on the wire */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Steven Whitehouse375d9d72006-11-07 15:09:17 -08001022 BUG_ON(len > 16); /* we've checked the contents earlier */
1023 opt->opt_optl = dn_htons(len);
1024 opt->opt_status = 0;
1025 memcpy(opt->opt_data, ptr, len);
1026 skb_pull(skb, len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
1030{
1031 DEFINE_WAIT(wait);
1032 struct sk_buff *skb = NULL;
1033 int err = 0;
1034
1035 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1036 for(;;) {
1037 release_sock(sk);
1038 skb = skb_dequeue(&sk->sk_receive_queue);
1039 if (skb == NULL) {
1040 *timeo = schedule_timeout(*timeo);
1041 skb = skb_dequeue(&sk->sk_receive_queue);
1042 }
1043 lock_sock(sk);
1044 if (skb != NULL)
1045 break;
1046 err = -EINVAL;
1047 if (sk->sk_state != TCP_LISTEN)
1048 break;
1049 err = sock_intr_errno(*timeo);
1050 if (signal_pending(current))
1051 break;
1052 err = -EAGAIN;
1053 if (!*timeo)
1054 break;
1055 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1056 }
1057 finish_wait(sk->sk_sleep, &wait);
1058
1059 return skb == NULL ? ERR_PTR(err) : skb;
1060}
1061
1062static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
1063{
1064 struct sock *sk = sock->sk, *newsk;
1065 struct sk_buff *skb = NULL;
1066 struct dn_skb_cb *cb;
1067 unsigned char menuver;
1068 int err = 0;
1069 unsigned char type;
1070 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1071
1072 lock_sock(sk);
1073
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001074 if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 release_sock(sk);
1076 return -EINVAL;
1077 }
1078
1079 skb = skb_dequeue(&sk->sk_receive_queue);
1080 if (skb == NULL) {
1081 skb = dn_wait_for_connect(sk, &timeo);
1082 if (IS_ERR(skb)) {
1083 release_sock(sk);
1084 return PTR_ERR(skb);
1085 }
1086 }
1087
1088 cb = DN_SKB_CB(skb);
1089 sk->sk_ack_backlog--;
1090 newsk = dn_alloc_sock(newsock, sk->sk_allocation);
1091 if (newsk == NULL) {
1092 release_sock(sk);
1093 kfree_skb(skb);
1094 return -ENOBUFS;
1095 }
1096 release_sock(sk);
1097
1098 dst_release(xchg(&newsk->sk_dst_cache, skb->dst));
1099 skb->dst = NULL;
1100
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001101 DN_SK(newsk)->state = DN_CR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 DN_SK(newsk)->addrrem = cb->src_port;
1103 DN_SK(newsk)->services_rem = cb->services;
1104 DN_SK(newsk)->info_rem = cb->info;
1105 DN_SK(newsk)->segsize_rem = cb->segsize;
1106 DN_SK(newsk)->accept_mode = DN_SK(sk)->accept_mode;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (DN_SK(newsk)->segsize_rem < 230)
1109 DN_SK(newsk)->segsize_rem = 230;
1110
1111 if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
1112 DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;
1113
1114 newsk->sk_state = TCP_LISTEN;
1115 memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));
1116
1117 /*
1118 * If we are listening on a wild socket, we don't want
1119 * the newly created socket on the wrong hash queue.
1120 */
1121 DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;
1122
1123 skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
1124 skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001125 *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
1126 *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 menuver = *skb->data;
1129 skb_pull(skb, 1);
1130
1131 if (menuver & DN_MENUVER_ACC)
1132 dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
1133
1134 if (menuver & DN_MENUVER_USR)
1135 dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
1136
1137 if (menuver & DN_MENUVER_PRX)
1138 DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;
1139
1140 if (menuver & DN_MENUVER_UIC)
1141 DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;
1142
1143 kfree_skb(skb);
1144
1145 memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
1146 sizeof(struct optdata_dn));
1147 memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
1148 sizeof(struct optdata_dn));
1149
1150 lock_sock(newsk);
1151 err = dn_hash_sock(newsk);
1152 if (err == 0) {
1153 sock_reset_flag(newsk, SOCK_ZAPPED);
1154 dn_send_conn_ack(newsk);
1155
1156 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001157 * Here we use sk->sk_allocation since although the conn conf is
1158 * for the newsk, the context is the old socket.
1159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (DN_SK(newsk)->accept_mode == ACC_IMMED)
1161 err = dn_confirm_accept(newsk, &timeo,
1162 sk->sk_allocation);
1163 }
1164 release_sock(newsk);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168
1169static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
1170{
1171 struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
1172 struct sock *sk = sock->sk;
1173 struct dn_scp *scp = DN_SK(sk);
1174
1175 *uaddr_len = sizeof(struct sockaddr_dn);
1176
1177 lock_sock(sk);
1178
1179 if (peer) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001180 if ((sock->state != SS_CONNECTED &&
1181 sock->state != SS_CONNECTING) &&
Patrick McHardy52c41a32006-10-12 01:48:20 -07001182 scp->accept_mode == ACC_IMMED) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001183 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -ENOTCONN;
Patrick McHardy52c41a32006-10-12 01:48:20 -07001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
1188 } else {
1189 memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
1190 }
1191
1192 release_sock(sk);
1193
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197
1198static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table *wait)
1199{
1200 struct sock *sk = sock->sk;
1201 struct dn_scp *scp = DN_SK(sk);
1202 int mask = datagram_poll(file, sock, wait);
1203
David S. Millerb03efcf2005-07-08 14:57:23 -07001204 if (!skb_queue_empty(&scp->other_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 mask |= POLLRDBAND;
1206
1207 return mask;
1208}
1209
1210static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1211{
1212 struct sock *sk = sock->sk;
1213 struct dn_scp *scp = DN_SK(sk);
1214 int err = -EOPNOTSUPP;
1215 long amount = 0;
1216 struct sk_buff *skb;
1217 int val;
1218
1219 switch(cmd)
1220 {
1221 case SIOCGIFADDR:
1222 case SIOCSIFADDR:
1223 return dn_dev_ioctl(cmd, (void __user *)arg);
1224
1225 case SIOCATMARK:
1226 lock_sock(sk);
David S. Millerb03efcf2005-07-08 14:57:23 -07001227 val = !skb_queue_empty(&scp->other_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (scp->state != DN_RUN)
1229 val = -ENOTCONN;
1230 release_sock(sk);
1231 return val;
1232
1233 case TIOCOUTQ:
1234 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1235 if (amount < 0)
1236 amount = 0;
1237 err = put_user(amount, (int __user *)arg);
1238 break;
1239
1240 case TIOCINQ:
1241 lock_sock(sk);
1242 if ((skb = skb_peek(&scp->other_receive_queue)) != NULL) {
1243 amount = skb->len;
1244 } else {
1245 struct sk_buff *skb = sk->sk_receive_queue.next;
1246 for(;;) {
1247 if (skb ==
1248 (struct sk_buff *)&sk->sk_receive_queue)
1249 break;
1250 amount += skb->len;
1251 skb = skb->next;
1252 }
1253 }
1254 release_sock(sk);
1255 err = put_user(amount, (int __user *)arg);
1256 break;
1257
1258 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001259 err = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 }
1262
1263 return err;
1264}
1265
1266static int dn_listen(struct socket *sock, int backlog)
1267{
1268 struct sock *sk = sock->sk;
1269 int err = -EINVAL;
1270
1271 lock_sock(sk);
1272
1273 if (sock_flag(sk, SOCK_ZAPPED))
1274 goto out;
1275
1276 if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN))
1277 goto out;
1278
1279 sk->sk_max_ack_backlog = backlog;
1280 sk->sk_ack_backlog = 0;
1281 sk->sk_state = TCP_LISTEN;
1282 err = 0;
1283 dn_rehash_sock(sk);
1284
1285out:
1286 release_sock(sk);
1287
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291
1292static int dn_shutdown(struct socket *sock, int how)
1293{
1294 struct sock *sk = sock->sk;
1295 struct dn_scp *scp = DN_SK(sk);
1296 int err = -ENOTCONN;
1297
1298 lock_sock(sk);
1299
1300 if (sock->state == SS_UNCONNECTED)
1301 goto out;
1302
1303 err = 0;
1304 if (sock->state == SS_DISCONNECTING)
1305 goto out;
1306
1307 err = -EINVAL;
1308 if (scp->state == DN_O)
1309 goto out;
1310
1311 if (how != SHUTDOWN_MASK)
1312 goto out;
1313
1314 sk->sk_shutdown = how;
1315 dn_destroy_sock(sk);
1316 err = 0;
1317
1318out:
1319 release_sock(sk);
1320
1321 return err;
1322}
1323
1324static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1325{
1326 struct sock *sk = sock->sk;
1327 int err;
1328
1329 lock_sock(sk);
1330 err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
1331 release_sock(sk);
1332
1333 return err;
1334}
1335
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001336static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, int optlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct sock *sk = sock->sk;
1339 struct dn_scp *scp = DN_SK(sk);
1340 long timeo;
1341 union {
1342 struct optdata_dn opt;
1343 struct accessdata_dn acc;
1344 int mode;
1345 unsigned long win;
1346 int val;
1347 unsigned char services;
1348 unsigned char info;
1349 } u;
1350 int err;
1351
1352 if (optlen && !optval)
1353 return -EINVAL;
1354
1355 if (optlen > sizeof(u))
1356 return -EINVAL;
1357
1358 if (copy_from_user(&u, optval, optlen))
1359 return -EFAULT;
1360
1361 switch(optname) {
1362 case DSO_CONDATA:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001363 if (sock->state == SS_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EISCONN;
1365 if ((scp->state != DN_O) && (scp->state != DN_CR))
1366 return -EINVAL;
1367
1368 if (optlen != sizeof(struct optdata_dn))
1369 return -EINVAL;
1370
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001371 if (dn_ntohs(u.opt.opt_optl) > 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EINVAL;
1373
1374 memcpy(&scp->conndata_out, &u.opt, optlen);
1375 break;
1376
1377 case DSO_DISDATA:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001378 if (sock->state != SS_CONNECTED && scp->accept_mode == ACC_IMMED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -ENOTCONN;
1380
1381 if (optlen != sizeof(struct optdata_dn))
1382 return -EINVAL;
1383
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001384 if (dn_ntohs(u.opt.opt_optl) > 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EINVAL;
1386
1387 memcpy(&scp->discdata_out, &u.opt, optlen);
1388 break;
1389
1390 case DSO_CONACCESS:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001391 if (sock->state == SS_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EISCONN;
1393 if (scp->state != DN_O)
1394 return -EINVAL;
1395
1396 if (optlen != sizeof(struct accessdata_dn))
1397 return -EINVAL;
1398
1399 if ((u.acc.acc_accl > DN_MAXACCL) ||
1400 (u.acc.acc_passl > DN_MAXACCL) ||
1401 (u.acc.acc_userl > DN_MAXACCL))
1402 return -EINVAL;
1403
1404 memcpy(&scp->accessdata, &u.acc, optlen);
1405 break;
1406
1407 case DSO_ACCEPTMODE:
1408 if (sock->state == SS_CONNECTED)
1409 return -EISCONN;
1410 if (scp->state != DN_O)
1411 return -EINVAL;
1412
1413 if (optlen != sizeof(int))
1414 return -EINVAL;
1415
1416 if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
1417 return -EINVAL;
1418
1419 scp->accept_mode = (unsigned char)u.mode;
1420 break;
1421
1422 case DSO_CONACCEPT:
1423
1424 if (scp->state != DN_CR)
1425 return -EINVAL;
1426 timeo = sock_rcvtimeo(sk, 0);
1427 err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
1428 return err;
1429
1430 case DSO_CONREJECT:
1431
1432 if (scp->state != DN_CR)
1433 return -EINVAL;
1434
1435 scp->state = DN_DR;
1436 sk->sk_shutdown = SHUTDOWN_MASK;
1437 dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
1438 break;
1439
1440 default:
1441#ifdef CONFIG_NETFILTER
1442 return nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
1443#endif
1444 case DSO_LINKINFO:
1445 case DSO_STREAM:
1446 case DSO_SEQPACKET:
1447 return -ENOPROTOOPT;
1448
1449 case DSO_MAXWINDOW:
1450 if (optlen != sizeof(unsigned long))
1451 return -EINVAL;
1452 if (u.win > NSP_MAX_WINDOW)
1453 u.win = NSP_MAX_WINDOW;
1454 if (u.win == 0)
1455 return -EINVAL;
1456 scp->max_window = u.win;
1457 if (scp->snd_window > u.win)
1458 scp->snd_window = u.win;
1459 break;
1460
1461 case DSO_NODELAY:
1462 if (optlen != sizeof(int))
1463 return -EINVAL;
1464 if (scp->nonagle == 2)
1465 return -EINVAL;
1466 scp->nonagle = (u.val == 0) ? 0 : 1;
1467 /* if (scp->nonagle == 1) { Push pending frames } */
1468 break;
1469
1470 case DSO_CORK:
1471 if (optlen != sizeof(int))
1472 return -EINVAL;
1473 if (scp->nonagle == 1)
1474 return -EINVAL;
1475 scp->nonagle = (u.val == 0) ? 0 : 2;
1476 /* if (scp->nonagle == 0) { Push pending frames } */
1477 break;
1478
1479 case DSO_SERVICES:
1480 if (optlen != sizeof(unsigned char))
1481 return -EINVAL;
1482 if ((u.services & ~NSP_FC_MASK) != 0x01)
1483 return -EINVAL;
1484 if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
1485 return -EINVAL;
1486 scp->services_loc = u.services;
1487 break;
1488
1489 case DSO_INFO:
1490 if (optlen != sizeof(unsigned char))
1491 return -EINVAL;
1492 if (u.info & 0xfc)
1493 return -EINVAL;
1494 scp->info_loc = u.info;
1495 break;
1496 }
1497
1498 return 0;
1499}
1500
1501static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
1502{
1503 struct sock *sk = sock->sk;
1504 int err;
1505
1506 lock_sock(sk);
1507 err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
1508 release_sock(sk);
1509
1510 return err;
1511}
1512
1513static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
1514{
1515 struct sock *sk = sock->sk;
1516 struct dn_scp *scp = DN_SK(sk);
1517 struct linkinfo_dn link;
1518 unsigned int r_len;
1519 void *r_data = NULL;
1520 unsigned int val;
1521
1522 if(get_user(r_len , optlen))
1523 return -EFAULT;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 switch(optname) {
1526 case DSO_CONDATA:
1527 if (r_len > sizeof(struct optdata_dn))
1528 r_len = sizeof(struct optdata_dn);
1529 r_data = &scp->conndata_in;
1530 break;
1531
1532 case DSO_DISDATA:
1533 if (r_len > sizeof(struct optdata_dn))
1534 r_len = sizeof(struct optdata_dn);
1535 r_data = &scp->discdata_in;
1536 break;
1537
1538 case DSO_CONACCESS:
1539 if (r_len > sizeof(struct accessdata_dn))
1540 r_len = sizeof(struct accessdata_dn);
1541 r_data = &scp->accessdata;
1542 break;
1543
1544 case DSO_ACCEPTMODE:
1545 if (r_len > sizeof(unsigned char))
1546 r_len = sizeof(unsigned char);
1547 r_data = &scp->accept_mode;
1548 break;
1549
1550 case DSO_LINKINFO:
1551 if (r_len > sizeof(struct linkinfo_dn))
1552 r_len = sizeof(struct linkinfo_dn);
1553
1554 switch(sock->state) {
1555 case SS_CONNECTING:
1556 link.idn_linkstate = LL_CONNECTING;
1557 break;
1558 case SS_DISCONNECTING:
1559 link.idn_linkstate = LL_DISCONNECTING;
1560 break;
1561 case SS_CONNECTED:
1562 link.idn_linkstate = LL_RUNNING;
1563 break;
1564 default:
1565 link.idn_linkstate = LL_INACTIVE;
1566 }
1567
1568 link.idn_segsize = scp->segsize_rem;
1569 r_data = &link;
1570 break;
1571
1572 default:
1573#ifdef CONFIG_NETFILTER
1574 {
1575 int val, len;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if(get_user(len, optlen))
1578 return -EFAULT;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001579
1580 val = nf_getsockopt(sk, PF_DECnet, optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 optval, &len);
1582 if (val >= 0)
1583 val = put_user(len, optlen);
1584 return val;
1585 }
1586#endif
1587 case DSO_STREAM:
1588 case DSO_SEQPACKET:
1589 case DSO_CONACCEPT:
1590 case DSO_CONREJECT:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001591 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 case DSO_MAXWINDOW:
1594 if (r_len > sizeof(unsigned long))
1595 r_len = sizeof(unsigned long);
1596 r_data = &scp->max_window;
1597 break;
1598
1599 case DSO_NODELAY:
1600 if (r_len > sizeof(int))
1601 r_len = sizeof(int);
1602 val = (scp->nonagle == 1);
1603 r_data = &val;
1604 break;
1605
1606 case DSO_CORK:
1607 if (r_len > sizeof(int))
1608 r_len = sizeof(int);
1609 val = (scp->nonagle == 2);
1610 r_data = &val;
1611 break;
1612
1613 case DSO_SERVICES:
1614 if (r_len > sizeof(unsigned char))
1615 r_len = sizeof(unsigned char);
1616 r_data = &scp->services_rem;
1617 break;
1618
1619 case DSO_INFO:
1620 if (r_len > sizeof(unsigned char))
1621 r_len = sizeof(unsigned char);
1622 r_data = &scp->info_rem;
1623 break;
1624 }
1625
1626 if (r_data) {
1627 if (copy_to_user(optval, r_data, r_len))
1628 return -EFAULT;
1629 if (put_user(r_len, optlen))
1630 return -EFAULT;
1631 }
1632
1633 return 0;
1634}
1635
1636
1637static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
1638{
1639 struct sk_buff *skb = q->next;
1640 int len = 0;
1641
1642 if (flags & MSG_OOB)
David S. Millerb03efcf2005-07-08 14:57:23 -07001643 return !skb_queue_empty(q) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 while(skb != (struct sk_buff *)q) {
1646 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1647 len += skb->len;
1648
1649 if (cb->nsp_flags & 0x40) {
1650 /* SOCK_SEQPACKET reads to EOM */
1651 if (sk->sk_type == SOCK_SEQPACKET)
1652 return 1;
1653 /* so does SOCK_STREAM unless WAITALL is specified */
1654 if (!(flags & MSG_WAITALL))
1655 return 1;
1656 }
1657
1658 /* minimum data length for read exceeded */
1659 if (len >= target)
1660 return 1;
1661
1662 skb = skb->next;
1663 }
1664
1665 return 0;
1666}
1667
1668
1669static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
1670 struct msghdr *msg, size_t size, int flags)
1671{
1672 struct sock *sk = sock->sk;
1673 struct dn_scp *scp = DN_SK(sk);
1674 struct sk_buff_head *queue = &sk->sk_receive_queue;
1675 size_t target = size > 1 ? 1 : 0;
1676 size_t copied = 0;
1677 int rv = 0;
1678 struct sk_buff *skb, *nskb;
1679 struct dn_skb_cb *cb = NULL;
1680 unsigned char eor = 0;
1681 long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1682
1683 lock_sock(sk);
1684
1685 if (sock_flag(sk, SOCK_ZAPPED)) {
1686 rv = -EADDRNOTAVAIL;
1687 goto out;
1688 }
1689
Patrick Caulfield9eb5c942005-11-11 12:04:28 -08001690 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1691 rv = 0;
1692 goto out;
1693 }
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 rv = dn_check_state(sk, NULL, 0, &timeo, flags);
1696 if (rv)
1697 goto out;
1698
Patrick Caulfieldc60992d2006-03-20 22:43:05 -08001699 if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 rv = -EOPNOTSUPP;
1701 goto out;
1702 }
1703
1704 if (flags & MSG_OOB)
1705 queue = &scp->other_receive_queue;
1706
1707 if (flags & MSG_WAITALL)
1708 target = size;
1709
1710
1711 /*
1712 * See if there is data ready to read, sleep if there isn't
1713 */
1714 for(;;) {
1715 if (sk->sk_err)
1716 goto out;
1717
David S. Millerb03efcf2005-07-08 14:57:23 -07001718 if (!skb_queue_empty(&scp->other_receive_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (!(flags & MSG_OOB)) {
1720 msg->msg_flags |= MSG_OOB;
1721 if (!scp->other_report) {
1722 scp->other_report = 1;
1723 goto out;
1724 }
1725 }
1726 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (scp->state != DN_RUN)
1729 goto out;
1730
1731 if (signal_pending(current)) {
1732 rv = sock_intr_errno(timeo);
1733 goto out;
1734 }
1735
1736 if (dn_data_ready(sk, queue, flags, target))
1737 break;
1738
1739 if (flags & MSG_DONTWAIT) {
1740 rv = -EWOULDBLOCK;
1741 goto out;
1742 }
1743
1744 set_bit(SOCK_ASYNC_WAITDATA, &sock->flags);
1745 SOCK_SLEEP_PRE(sk)
1746
1747 if (!dn_data_ready(sk, queue, flags, target))
1748 schedule();
1749
1750 SOCK_SLEEP_POST(sk)
1751 clear_bit(SOCK_ASYNC_WAITDATA, &sock->flags);
1752 }
1753
1754 for(skb = queue->next; skb != (struct sk_buff *)queue; skb = nskb) {
1755 unsigned int chunk = skb->len;
1756 cb = DN_SKB_CB(skb);
1757
1758 if ((chunk + copied) > size)
1759 chunk = size - copied;
1760
1761 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1762 rv = -EFAULT;
1763 break;
1764 }
1765 copied += chunk;
1766
1767 if (!(flags & MSG_PEEK))
1768 skb_pull(skb, chunk);
1769
1770 eor = cb->nsp_flags & 0x40;
1771 nskb = skb->next;
1772
1773 if (skb->len == 0) {
David S. Miller8728b832005-08-09 19:25:21 -07001774 skb_unlink(skb, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 kfree_skb(skb);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001776 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 * N.B. Don't refer to skb or cb after this point
1778 * in loop.
1779 */
1780 if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
1781 scp->flowloc_sw = DN_SEND;
1782 dn_nsp_send_link(sk, DN_SEND, 0);
1783 }
1784 }
1785
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001786 if (eor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (sk->sk_type == SOCK_SEQPACKET)
1788 break;
1789 if (!(flags & MSG_WAITALL))
1790 break;
1791 }
1792
1793 if (flags & MSG_OOB)
1794 break;
1795
1796 if (copied >= target)
1797 break;
1798 }
1799
1800 rv = copied;
1801
1802
1803 if (eor && (sk->sk_type == SOCK_SEQPACKET))
1804 msg->msg_flags |= MSG_EOR;
1805
1806out:
1807 if (rv == 0)
1808 rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
1809
1810 if ((rv >= 0) && msg->msg_name) {
1811 memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
1812 msg->msg_namelen = sizeof(struct sockaddr_dn);
1813 }
1814
1815 release_sock(sk);
1816
1817 return rv;
1818}
1819
1820
1821static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
1822{
1823 unsigned char fctype = scp->services_rem & NSP_FC_MASK;
1824 if (skb_queue_len(queue) >= scp->snd_window)
1825 return 1;
1826 if (fctype != NSP_FC_NONE) {
1827 if (flags & MSG_OOB) {
1828 if (scp->flowrem_oth == 0)
1829 return 1;
1830 } else {
1831 if (scp->flowrem_dat == 0)
1832 return 1;
1833 }
1834 }
1835 return 0;
1836}
1837
1838/*
1839 * The DECnet spec requires the the "routing layer" accepts packets which
1840 * are at least 230 bytes in size. This excludes any headers which the NSP
1841 * layer might add, so we always assume that we'll be using the maximal
1842 * length header on data packets. The variation in length is due to the
1843 * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
1844 * make much practical difference.
1845 */
1846unsigned dn_mss_from_pmtu(struct net_device *dev, int mtu)
1847{
1848 unsigned mss = 230 - DN_MAX_NSP_DATA_HEADER;
1849 if (dev) {
1850 struct dn_dev *dn_db = dev->dn_ptr;
1851 mtu -= LL_RESERVED_SPACE(dev);
1852 if (dn_db->use_long)
1853 mtu -= 21;
1854 else
1855 mtu -= 6;
1856 mtu -= DN_MAX_NSP_DATA_HEADER;
1857 } else {
1858 /*
1859 * 21 = long header, 16 = guess at MAC header length
1860 */
1861 mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
1862 }
1863 if (mtu > mss)
1864 mss = mtu;
1865 return mss;
1866}
1867
1868static inline unsigned int dn_current_mss(struct sock *sk, int flags)
1869{
1870 struct dst_entry *dst = __sk_dst_get(sk);
1871 struct dn_scp *scp = DN_SK(sk);
1872 int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
1873
1874 /* Other data messages are limited to 16 bytes per packet */
1875 if (flags & MSG_OOB)
1876 return 16;
1877
1878 /* This works out the maximum size of segment we can send out */
1879 if (dst) {
1880 u32 mtu = dst_mtu(dst);
1881 mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
1882 }
1883
1884 return mss_now;
1885}
1886
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001887/*
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07001888 * N.B. We get the timeout wrong here, but then we always did get it
1889 * wrong before and this is another step along the road to correcting
1890 * it. It ought to get updated each time we pass through the routine,
1891 * but in practise it probably doesn't matter too much for now.
1892 */
1893static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
1894 unsigned long datalen, int noblock,
1895 int *errcode)
1896{
1897 struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
1898 noblock, errcode);
1899 if (skb) {
1900 skb->protocol = __constant_htons(ETH_P_DNA_RT);
1901 skb->pkt_type = PACKET_OUTGOING;
1902 }
1903 return skb;
1904}
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07001907 struct msghdr *msg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct sock *sk = sock->sk;
1910 struct dn_scp *scp = DN_SK(sk);
1911 size_t mss;
1912 struct sk_buff_head *queue = &scp->data_xmit_queue;
1913 int flags = msg->msg_flags;
1914 int err = 0;
1915 size_t sent = 0;
1916 int addr_len = msg->msg_namelen;
1917 struct sockaddr_dn *addr = (struct sockaddr_dn *)msg->msg_name;
1918 struct sk_buff *skb = NULL;
1919 struct dn_skb_cb *cb;
1920 size_t len;
1921 unsigned char fctype;
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07001922 long timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
1925 return -EOPNOTSUPP;
1926
1927 if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
1928 return -EINVAL;
1929
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07001930 lock_sock(sk);
1931 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /*
1933 * The only difference between stream sockets and sequenced packet
1934 * sockets is that the stream sockets always behave as if MSG_EOR
1935 * has been set.
1936 */
1937 if (sock->type == SOCK_STREAM) {
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07001938 if (flags & MSG_EOR) {
1939 err = -EINVAL;
1940 goto out;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 flags |= MSG_EOR;
1943 }
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 err = dn_check_state(sk, addr, addr_len, &timeo, flags);
1947 if (err)
1948 goto out_err;
1949
1950 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1951 err = -EPIPE;
Patrick Caulfield9eb5c942005-11-11 12:04:28 -08001952 if (!(flags & MSG_NOSIGNAL))
1953 send_sig(SIGPIPE, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto out_err;
1955 }
1956
1957 if ((flags & MSG_TRYHARD) && sk->sk_dst_cache)
1958 dst_negative_advice(&sk->sk_dst_cache);
1959
1960 mss = scp->segsize_rem;
1961 fctype = scp->services_rem & NSP_FC_MASK;
1962
1963 mss = dn_current_mss(sk, flags);
1964
1965 if (flags & MSG_OOB) {
1966 queue = &scp->other_xmit_queue;
1967 if (size > mss) {
1968 err = -EMSGSIZE;
1969 goto out;
1970 }
1971 }
1972
1973 scp->persist_fxn = dn_nsp_xmit_timeout;
1974
1975 while(sent < size) {
1976 err = sock_error(sk);
1977 if (err)
1978 goto out;
1979
1980 if (signal_pending(current)) {
1981 err = sock_intr_errno(timeo);
1982 goto out;
1983 }
1984
1985 /*
1986 * Calculate size that we wish to send.
1987 */
1988 len = size - sent;
1989
1990 if (len > mss)
1991 len = mss;
1992
1993 /*
1994 * Wait for queue size to go down below the window
1995 * size.
1996 */
1997 if (dn_queue_too_long(scp, queue, flags)) {
1998 if (flags & MSG_DONTWAIT) {
1999 err = -EWOULDBLOCK;
2000 goto out;
2001 }
2002
2003 SOCK_SLEEP_PRE(sk)
2004
2005 if (dn_queue_too_long(scp, queue, flags))
2006 schedule();
2007
2008 SOCK_SLEEP_POST(sk)
2009
2010 continue;
2011 }
2012
2013 /*
2014 * Get a suitably sized skb.
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07002015 * 64 is a bit of a hack really, but its larger than any
2016 * link-layer headers and has served us well as a good
2017 * guess as to their real length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 */
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07002019 skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
2020 flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 if (err)
2023 break;
2024
2025 if (!skb)
2026 continue;
2027
2028 cb = DN_SKB_CB(skb);
2029
Patrick Caulfield12a49ff2005-09-01 17:43:45 -07002030 skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
2033 err = -EFAULT;
2034 goto out;
2035 }
2036
2037 if (flags & MSG_OOB) {
2038 cb->nsp_flags = 0x30;
2039 if (fctype != NSP_FC_NONE)
2040 scp->flowrem_oth--;
2041 } else {
2042 cb->nsp_flags = 0x00;
2043 if (scp->seg_total == 0)
2044 cb->nsp_flags |= 0x20;
2045
2046 scp->seg_total += len;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (((sent + len) == size) && (flags & MSG_EOR)) {
2049 cb->nsp_flags |= 0x40;
2050 scp->seg_total = 0;
2051 if (fctype == NSP_FC_SCMC)
2052 scp->flowrem_dat--;
2053 }
2054 if (fctype == NSP_FC_SRC)
2055 scp->flowrem_dat--;
2056 }
2057
2058 sent += len;
2059 dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
2060 skb = NULL;
2061
2062 scp->persist = dn_nsp_persist(sk);
2063
2064 }
2065out:
2066
2067 if (skb)
2068 kfree_skb(skb);
2069
2070 release_sock(sk);
2071
2072 return sent ? sent : err;
2073
2074out_err:
Steven Whitehouse001ab022005-08-10 11:32:57 -07002075 err = sk_stream_error(sk, flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 release_sock(sk);
2077 return err;
2078}
2079
2080static int dn_device_event(struct notifier_block *this, unsigned long event,
2081 void *ptr)
2082{
2083 struct net_device *dev = (struct net_device *)ptr;
2084
2085 switch(event) {
2086 case NETDEV_UP:
2087 dn_dev_up(dev);
2088 break;
2089 case NETDEV_DOWN:
2090 dn_dev_down(dev);
2091 break;
2092 default:
2093 break;
2094 }
2095
2096 return NOTIFY_DONE;
2097}
2098
2099static struct notifier_block dn_dev_notifier = {
2100 .notifier_call = dn_device_event,
2101};
2102
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002103extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105static struct packet_type dn_dix_packet_type = {
2106 .type = __constant_htons(ETH_P_DNA_RT),
2107 .dev = NULL, /* All devices */
2108 .func = dn_route_rcv,
2109};
2110
2111#ifdef CONFIG_PROC_FS
2112struct dn_iter_state {
2113 int bucket;
2114};
2115
2116static struct sock *dn_socket_get_first(struct seq_file *seq)
2117{
2118 struct dn_iter_state *state = seq->private;
2119 struct sock *n = NULL;
2120
2121 for(state->bucket = 0;
2122 state->bucket < DN_SK_HASH_SIZE;
2123 ++state->bucket) {
2124 n = sk_head(&dn_sk_hash[state->bucket]);
2125 if (n)
2126 break;
2127 }
2128
2129 return n;
2130}
2131
2132static struct sock *dn_socket_get_next(struct seq_file *seq,
2133 struct sock *n)
2134{
2135 struct dn_iter_state *state = seq->private;
2136
2137 n = sk_next(n);
2138try_again:
2139 if (n)
2140 goto out;
2141 if (++state->bucket >= DN_SK_HASH_SIZE)
2142 goto out;
2143 n = sk_head(&dn_sk_hash[state->bucket]);
2144 goto try_again;
2145out:
2146 return n;
2147}
2148
2149static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos)
2150{
2151 struct sock *sk = dn_socket_get_first(seq);
2152
2153 if (sk) {
2154 while(*pos && (sk = dn_socket_get_next(seq, sk)))
2155 --*pos;
2156 }
2157 return *pos ? NULL : sk;
2158}
2159
2160static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos)
2161{
2162 void *rc;
2163 read_lock_bh(&dn_hash_lock);
2164 rc = socket_get_idx(seq, &pos);
2165 if (!rc) {
2166 read_unlock_bh(&dn_hash_lock);
2167 }
2168 return rc;
2169}
2170
2171static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos)
2172{
2173 return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2174}
2175
2176static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2177{
2178 void *rc;
2179
2180 if (v == SEQ_START_TOKEN) {
2181 rc = dn_socket_get_idx(seq, 0);
2182 goto out;
2183 }
2184
2185 rc = dn_socket_get_next(seq, v);
2186 if (rc)
2187 goto out;
2188 read_unlock_bh(&dn_hash_lock);
2189out:
2190 ++*pos;
2191 return rc;
2192}
2193
2194static void dn_socket_seq_stop(struct seq_file *seq, void *v)
2195{
2196 if (v && v != SEQ_START_TOKEN)
2197 read_unlock_bh(&dn_hash_lock);
2198}
2199
2200#define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
2201
2202static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
2203{
2204 int i;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 switch (dn_ntohs(dn->sdn_objnamel)) {
2207 case 0:
2208 sprintf(buf, "%d", dn->sdn_objnum);
2209 break;
2210 default:
2211 for (i = 0; i < dn_ntohs(dn->sdn_objnamel); i++) {
2212 buf[i] = dn->sdn_objname[i];
2213 if (IS_NOT_PRINTABLE(buf[i]))
2214 buf[i] = '.';
2215 }
2216 buf[i] = 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
2220static char *dn_state2asc(unsigned char state)
2221{
2222 switch(state) {
2223 case DN_O:
2224 return "OPEN";
2225 case DN_CR:
2226 return " CR";
2227 case DN_DR:
2228 return " DR";
2229 case DN_DRC:
2230 return " DRC";
2231 case DN_CC:
2232 return " CC";
2233 case DN_CI:
2234 return " CI";
2235 case DN_NR:
2236 return " NR";
2237 case DN_NC:
2238 return " NC";
2239 case DN_CD:
2240 return " CD";
2241 case DN_RJ:
2242 return " RJ";
2243 case DN_RUN:
2244 return " RUN";
2245 case DN_DI:
2246 return " DI";
2247 case DN_DIC:
2248 return " DIC";
2249 case DN_DN:
2250 return " DN";
2251 case DN_CL:
2252 return " CL";
2253 case DN_CN:
2254 return " CN";
2255 }
2256
2257 return "????";
2258}
2259
2260static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk)
2261{
2262 struct dn_scp *scp = DN_SK(sk);
2263 char buf1[DN_ASCBUF_LEN];
2264 char buf2[DN_ASCBUF_LEN];
2265 char local_object[DN_MAXOBJL+3];
2266 char remote_object[DN_MAXOBJL+3];
2267
2268 dn_printable_object(&scp->addr, local_object);
2269 dn_printable_object(&scp->peer, remote_object);
2270
2271 seq_printf(seq,
2272 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
2273 "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
2274 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp->addr)), buf1),
2275 scp->addrloc,
2276 scp->numdat,
2277 scp->numoth,
2278 scp->ackxmt_dat,
2279 scp->ackxmt_oth,
2280 scp->flowloc_sw,
2281 local_object,
2282 dn_addr2asc(dn_ntohs(dn_saddr2dn(&scp->peer)), buf2),
2283 scp->addrrem,
2284 scp->numdat_rcv,
2285 scp->numoth_rcv,
2286 scp->ackrcv_dat,
2287 scp->ackrcv_oth,
2288 scp->flowrem_sw,
2289 remote_object,
2290 dn_state2asc(scp->state),
2291 ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
2292}
2293
2294static int dn_socket_seq_show(struct seq_file *seq, void *v)
2295{
2296 if (v == SEQ_START_TOKEN) {
2297 seq_puts(seq, "Local Remote\n");
2298 } else {
2299 dn_socket_format_entry(seq, v);
2300 }
2301 return 0;
2302}
2303
2304static struct seq_operations dn_socket_seq_ops = {
2305 .start = dn_socket_seq_start,
2306 .next = dn_socket_seq_next,
2307 .stop = dn_socket_seq_stop,
2308 .show = dn_socket_seq_show,
2309};
2310
2311static int dn_socket_seq_open(struct inode *inode, struct file *file)
2312{
2313 struct seq_file *seq;
2314 int rc = -ENOMEM;
2315 struct dn_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2316
2317 if (!s)
2318 goto out;
2319
2320 rc = seq_open(file, &dn_socket_seq_ops);
2321 if (rc)
2322 goto out_kfree;
2323
2324 seq = file->private_data;
2325 seq->private = s;
2326 memset(s, 0, sizeof(*s));
2327out:
2328 return rc;
2329out_kfree:
2330 kfree(s);
2331 goto out;
2332}
2333
Arjan van de Ven9a321442007-02-12 00:55:35 -08002334static const struct file_operations dn_socket_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 .owner = THIS_MODULE,
2336 .open = dn_socket_seq_open,
2337 .read = seq_read,
2338 .llseek = seq_lseek,
2339 .release = seq_release_private,
2340};
2341#endif
2342
2343static struct net_proto_family dn_family_ops = {
2344 .family = AF_DECnet,
2345 .create = dn_create,
2346 .owner = THIS_MODULE,
2347};
2348
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002349static const struct proto_ops dn_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 .family = AF_DECnet,
2351 .owner = THIS_MODULE,
2352 .release = dn_release,
2353 .bind = dn_bind,
2354 .connect = dn_connect,
2355 .socketpair = sock_no_socketpair,
2356 .accept = dn_accept,
2357 .getname = dn_getname,
2358 .poll = dn_poll,
2359 .ioctl = dn_ioctl,
2360 .listen = dn_listen,
2361 .shutdown = dn_shutdown,
2362 .setsockopt = dn_setsockopt,
2363 .getsockopt = dn_getsockopt,
2364 .sendmsg = dn_sendmsg,
2365 .recvmsg = dn_recvmsg,
2366 .mmap = sock_no_mmap,
2367 .sendpage = sock_no_sendpage,
2368};
2369
2370void dn_register_sysctl(void);
2371void dn_unregister_sysctl(void);
2372
2373MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
2374MODULE_AUTHOR("Linux DECnet Project Team");
2375MODULE_LICENSE("GPL");
2376MODULE_ALIAS_NETPROTO(PF_DECnet);
2377
2378static char banner[] __initdata = KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
2379
2380static int __init decnet_init(void)
2381{
2382 int rc;
2383
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09002384 printk(banner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 rc = proto_register(&dn_proto, 1);
2387 if (rc != 0)
2388 goto out;
2389
2390 dn_neigh_init();
2391 dn_dev_init();
2392 dn_route_init();
2393 dn_fib_init();
2394
2395 sock_register(&dn_family_ops);
2396 dev_add_pack(&dn_dix_packet_type);
2397 register_netdevice_notifier(&dn_dev_notifier);
2398
2399 proc_net_fops_create("decnet", S_IRUGO, &dn_socket_seq_fops);
2400 dn_register_sysctl();
2401out:
2402 return rc;
2403
2404}
2405module_init(decnet_init);
2406
2407/*
2408 * Prevent DECnet module unloading until its fixed properly.
2409 * Requires an audit of the code to check for memory leaks and
2410 * initialisation problems etc.
2411 */
2412#if 0
2413static void __exit decnet_exit(void)
2414{
2415 sock_unregister(AF_DECnet);
Thomas Graffa34ddd2007-03-22 11:57:46 -07002416 rtnl_unregister_all(PF_DECnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 dev_remove_pack(&dn_dix_packet_type);
2418
2419 dn_unregister_sysctl();
2420
2421 unregister_netdevice_notifier(&dn_dev_notifier);
2422
2423 dn_route_cleanup();
2424 dn_dev_cleanup();
2425 dn_neigh_cleanup();
2426 dn_fib_cleanup();
2427
2428 proc_net_remove("decnet");
2429
2430 proto_unregister(&dn_proto);
2431}
2432module_exit(decnet_exit);
2433#endif