~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/decnet/af_decnet.c

Version: ~ [ linux-6.6-rc1 ] ~ [ linux-6.5.2 ] ~ [ linux-6.4.15 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.52 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.131 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.194 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.256 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.294 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.325 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp