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