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