ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / decnet / dn_nsp_in.c
1 /*
2  * DECnet       An implementation of the DECnet protocol suite for the LINUX
3  *              operating system.  DECnet is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              DECnet Network Services Protocol (Input)
7  *
8  * Author:      Eduardo Marcelo Serrat <emserrat@geocities.com>
9  *
10  * Changes:
11  *
12  *    Steve Whitehouse:  Split into dn_nsp_in.c and dn_nsp_out.c from
13  *                       original dn_nsp.c.
14  *    Steve Whitehouse:  Updated to work with my new routing architecture.
15  *    Steve Whitehouse:  Add changes from Eduardo Serrat's patches.
16  *    Steve Whitehouse:  Put all ack handling code in a common routine.
17  *    Steve Whitehouse:  Put other common bits into dn_nsp_rx()
18  *    Steve Whitehouse:  More checks on skb->len to catch bogus packets
19  *                       Fixed various race conditions and possible nasties.
20  *    Steve Whitehouse:  Now handles returned conninit frames.
21  *     David S. Miller:  New socket locking
22  *    Steve Whitehouse:  Fixed lockup when socket filtering was enabled.
23  *         Paul Koning:  Fix to push CC sockets into RUN when acks are
24  *                       received.
25  *    Steve Whitehouse:
26  *   Patrick Caulfield:  Checking conninits for correctness & sending of error
27  *                       responses.
28  *    Steve Whitehouse:  Added backlog congestion level return codes.
29  *   Patrick Caulfield:
30  *    Steve Whitehouse:  Added flow control support (outbound)
31  *    Steve Whitehouse:  Prepare for nonlinear skbs
32  */
33
34 /******************************************************************************
35     (c) 1995-1998 E.M. Serrat           emserrat@geocities.com
36     
37     This program is free software; you can redistribute it and/or modify
38     it under the terms of the GNU General Public License as published by
39     the Free Software Foundation; either version 2 of the License, or
40     any later version.
41
42     This program is distributed in the hope that it will be useful,
43     but WITHOUT ANY WARRANTY; without even the implied warranty of
44     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45     GNU General Public License for more details.
46 *******************************************************************************/
47
48 #include <linux/config.h>
49 #include <linux/errno.h>
50 #include <linux/types.h>
51 #include <linux/socket.h>
52 #include <linux/in.h>
53 #include <linux/kernel.h>
54 #include <linux/sched.h>
55 #include <linux/timer.h>
56 #include <linux/string.h>
57 #include <linux/sockios.h>
58 #include <linux/net.h>
59 #include <linux/netdevice.h>
60 #include <linux/inet.h>
61 #include <linux/route.h>
62 #include <net/sock.h>
63 #include <net/tcp.h>
64 #include <asm/system.h>
65 #include <linux/fcntl.h>
66 #include <linux/mm.h>
67 #include <linux/termios.h>      
68 #include <linux/interrupt.h>
69 #include <linux/proc_fs.h>
70 #include <linux/stat.h>
71 #include <linux/init.h>
72 #include <linux/poll.h>
73 #include <linux/netfilter_decnet.h>
74 #include <net/neighbour.h>
75 #include <net/dst.h>
76 #include <net/dn.h>
77 #include <net/dn_nsp.h>
78 #include <net/dn_dev.h>
79 #include <net/dn_route.h>
80
81 extern int decnet_log_martians;
82
83 static void dn_log_martian(struct sk_buff *skb, const char *msg)
84 {
85         if (decnet_log_martians && net_ratelimit()) {
86                 char *devname = skb->dev ? skb->dev->name : "???";
87                 struct dn_skb_cb *cb = DN_SKB_CB(skb);
88                 printk(KERN_INFO "DECnet: Martian packet (%s) dev=%s src=0x%04hx dst=0x%04hx srcport=0x%04hx dstport=0x%04hx\n", msg, devname, cb->src, cb->dst, cb->src_port, cb->dst_port);
89         }
90 }
91
92 /*
93  * For this function we've flipped the cross-subchannel bit
94  * if the message is an otherdata or linkservice message. Thus
95  * we can use it to work out what to update.
96  */
97 static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
98 {
99         struct dn_scp *scp = DN_SK(sk);
100         unsigned short type = ((ack >> 12) & 0x0003);
101         int wakeup = 0;
102
103         switch(type) {
104                 case 0: /* ACK - Data */
105                         if (dn_after(ack, scp->ackrcv_dat)) {
106                                 scp->ackrcv_dat = ack & 0x0fff;
107                                 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->data_xmit_queue, ack);
108                         }
109                         break;
110                 case 1: /* NAK - Data */
111                         break;
112                 case 2: /* ACK - OtherData */
113                         if (dn_after(ack, scp->ackrcv_oth)) {
114                                 scp->ackrcv_oth = ack & 0x0fff;
115                                 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->other_xmit_queue, ack);
116                         }
117                         break;
118                 case 3: /* NAK - OtherData */
119                         break;
120         }
121
122         if (wakeup && !sock_flag(sk, SOCK_DEAD))
123                 sk->sk_state_change(sk);
124 }
125
126 /*
127  * This function is a universal ack processor.
128  */
129 static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
130 {
131         unsigned short *ptr = (unsigned short *)skb->data;
132         int len = 0;
133         unsigned short ack;
134
135         if (skb->len < 2)
136                 return len;
137
138         if ((ack = dn_ntohs(*ptr)) & 0x8000) {
139                 skb_pull(skb, 2);
140                 ptr++;
141                 len += 2;
142                 if ((ack & 0x4000) == 0) {
143                         if (oth) 
144                                 ack ^= 0x2000;
145                         dn_ack(sk, skb, ack);
146                 }
147         }
148
149         if (skb->len < 2)
150                 return len;
151
152         if ((ack = dn_ntohs(*ptr)) & 0x8000) {
153                 skb_pull(skb, 2);
154                 len += 2;
155                 if ((ack & 0x4000) == 0) {
156                         if (oth) 
157                                 ack ^= 0x2000;
158                         dn_ack(sk, skb, ack);
159                 }
160         }
161
162         return len;
163 }
164
165
166 /**
167  * dn_check_idf - Check an image data field format is correct.
168  * @pptr: Pointer to pointer to image data
169  * @len: Pointer to length of image data
170  * @max: The maximum allowed length of the data in the image data field
171  * @follow_on: Check that this many bytes exist beyond the end of the image data
172  *
173  * Returns: 0 if ok, -1 on error
174  */
175 static inline int dn_check_idf(unsigned char **pptr, int *len, unsigned char max, unsigned char follow_on)
176 {
177         unsigned char *ptr = *pptr;
178         unsigned char flen = *ptr++;
179
180         (*len)--;
181         if (flen > max)
182                 return -1;
183         if ((flen + follow_on) > *len)
184                 return -1;
185
186         *len -= flen;
187         *pptr = ptr + flen;
188         return 0;
189 }
190
191 /*
192  * Table of reason codes to pass back to node which sent us a badly
193  * formed message, plus text messages for the log. A zero entry in
194  * the reason field means "don't reply" otherwise a disc init is sent with
195  * the specified reason code.
196  */
197 static struct {
198         unsigned short reason;
199         const char *text;
200 } ci_err_table[] = {
201  { 0,             "CI: Truncated message" },
202  { NSP_REASON_ID, "CI: Destination username error" },
203  { NSP_REASON_ID, "CI: Destination username type" },
204  { NSP_REASON_US, "CI: Source username error" },
205  { 0,             "CI: Truncated at menuver" },
206  { 0,             "CI: Truncated before access or user data" },
207  { NSP_REASON_IO, "CI: Access data format error" },
208  { NSP_REASON_IO, "CI: User data format error" }
209 };
210
211 /*
212  * This function uses a slightly different lookup method
213  * to find its sockets, since it searches on object name/number
214  * rather than port numbers. Various tests are done to ensure that
215  * the incoming data is in the correct format before it is queued to
216  * a socket.
217  */
218 static struct sock *dn_find_listener(struct sk_buff *skb, unsigned short *reason)
219 {
220         struct dn_skb_cb *cb = DN_SKB_CB(skb);
221         struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
222         struct sockaddr_dn dstaddr;
223         struct sockaddr_dn srcaddr;
224         unsigned char type = 0;
225         int dstlen;
226         int srclen;
227         unsigned char *ptr;
228         int len;
229         int err = 0;
230         unsigned char menuver;
231
232         memset(&dstaddr, 0, sizeof(struct sockaddr_dn));
233         memset(&srcaddr, 0, sizeof(struct sockaddr_dn));
234
235         /*
236          * 1. Decode & remove message header
237          */
238         cb->src_port = msg->srcaddr;
239         cb->dst_port = msg->dstaddr;
240         cb->services = msg->services;
241         cb->info     = msg->info;
242         cb->segsize  = dn_ntohs(msg->segsize);
243
244         if (!pskb_may_pull(skb, sizeof(*msg)))
245                 goto err_out;
246
247         skb_pull(skb, sizeof(*msg));
248
249         len = skb->len;
250         ptr = skb->data;
251
252         /*
253          * 2. Check destination end username format
254          */
255         dstlen = dn_username2sockaddr(ptr, len, &dstaddr, &type);
256         err++;
257         if (dstlen < 0)
258                 goto err_out;
259
260         err++;
261         if (type > 1)
262                 goto err_out;
263
264         len -= dstlen;
265         ptr += dstlen;
266
267         /*
268          * 3. Check source end username format
269          */
270         srclen = dn_username2sockaddr(ptr, len, &srcaddr, &type);
271         err++;
272         if (srclen < 0)
273                 goto err_out;
274
275         len -= srclen;
276         ptr += srclen;
277         err++;
278         if (len < 1)
279                 goto err_out;
280
281         menuver = *ptr;
282         ptr++;
283         len--;
284
285         /*
286          * 4. Check that optional data actually exists if menuver says it does
287          */
288         err++;
289         if ((menuver & (DN_MENUVER_ACC | DN_MENUVER_USR)) && (len < 1))
290                 goto err_out;
291
292         /*
293          * 5. Check optional access data format
294          */
295         err++;
296         if (menuver & DN_MENUVER_ACC) {
297                 if (dn_check_idf(&ptr, &len, 39, 1))
298                         goto err_out;
299                 if (dn_check_idf(&ptr, &len, 39, 1))
300                         goto err_out;
301                 if (dn_check_idf(&ptr, &len, 39, (menuver & DN_MENUVER_USR) ? 1 : 0))
302                         goto err_out;
303         }
304
305         /*
306          * 6. Check optional user data format
307          */
308         err++;
309         if (menuver & DN_MENUVER_USR) {
310                 if (dn_check_idf(&ptr, &len, 16, 0))
311                         goto err_out;
312         }
313
314         /*
315          * 7. Look up socket based on destination end username
316          */
317         return dn_sklist_find_listener(&dstaddr);
318 err_out:
319         dn_log_martian(skb, ci_err_table[err].text);
320         *reason = ci_err_table[err].reason;
321         return NULL;
322 }
323
324
325 static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
326 {
327         if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
328                 kfree_skb(skb);
329                 return;
330         }
331
332         sk->sk_ack_backlog++;
333         skb_queue_tail(&sk->sk_receive_queue, skb);
334         sk->sk_state_change(sk);
335 }
336
337 static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
338 {
339         struct dn_skb_cb *cb = DN_SKB_CB(skb);
340         struct dn_scp *scp = DN_SK(sk);
341         unsigned char *ptr;
342
343         if (skb->len < 4)
344                 goto out;
345
346         ptr = skb->data;
347         cb->services = *ptr++;
348         cb->info = *ptr++;
349         cb->segsize = dn_ntohs(*(__u16 *)ptr);
350
351         if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
352                 scp->persist = 0;
353                 scp->addrrem = cb->src_port;
354                 sk->sk_state = TCP_ESTABLISHED;
355                 scp->state = DN_RUN;
356                 scp->services_rem = cb->services;
357                 scp->info_rem = cb->info;
358                 scp->segsize_rem = cb->segsize;
359
360                 if ((scp->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
361                         scp->max_window = decnet_no_fc_max_cwnd;
362
363                 if (skb->len > 0) {
364                         unsigned char dlen = *skb->data;
365                         if ((dlen <= 16) && (dlen <= skb->len)) {
366                                 scp->conndata_in.opt_optl = dlen;
367                                 memcpy(scp->conndata_in.opt_data, skb->data + 1, dlen);
368                         }
369                 }
370                 dn_nsp_send_link(sk, DN_NOCHANGE, 0);
371                 if (!sock_flag(sk, SOCK_DEAD))
372                         sk->sk_state_change(sk);
373         }
374
375 out:
376         kfree_skb(skb);
377 }
378
379 static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
380 {
381         struct dn_scp *scp = DN_SK(sk);
382
383         if (scp->state == DN_CI) {
384                 scp->state = DN_CD;
385                 scp->persist = 0;
386         }
387
388         kfree_skb(skb);
389 }
390
391 static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
392 {
393         struct dn_scp *scp = DN_SK(sk);
394         struct dn_skb_cb *cb = DN_SKB_CB(skb);
395         unsigned short reason;
396
397         if (skb->len < 2)
398                 goto out;
399
400         reason = dn_ntohs(*(__u16 *)skb->data);
401         skb_pull(skb, 2);
402
403         scp->discdata_in.opt_status = reason;
404         scp->discdata_in.opt_optl   = 0;
405         memset(scp->discdata_in.opt_data, 0, 16);
406
407         if (skb->len > 0) {
408                 unsigned char dlen = *skb->data;
409                 if ((dlen <= 16) && (dlen <= skb->len)) {
410                         scp->discdata_in.opt_optl = dlen;
411                         memcpy(scp->discdata_in.opt_data, skb->data + 1, dlen);
412                 }
413         }
414
415         scp->addrrem = cb->src_port;
416         sk->sk_state = TCP_CLOSE;
417
418         switch(scp->state) {
419                 case DN_CI:
420                 case DN_CD:
421                         scp->state = DN_RJ;
422                         break;
423                 case DN_RUN:
424                         sk->sk_shutdown |= SHUTDOWN_MASK;
425                         scp->state = DN_DN;
426                         break;
427                 case DN_DI:
428                         scp->state = DN_DIC;
429                         break;
430         }
431
432         if (!sock_flag(sk, SOCK_DEAD)) {
433                 if (sk->sk_socket->state != SS_UNCONNECTED)
434                         sk->sk_socket->state = SS_DISCONNECTING;
435                 sk->sk_state_change(sk);
436         }
437
438         /* 
439          * It appears that its possible for remote machines to send disc
440          * init messages with no port identifier if we are in the CI and
441          * possibly also the CD state. Obviously we shouldn't reply with
442          * a message if we don't know what the end point is.
443          */
444         if (scp->addrrem) {
445                 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
446         }
447         scp->persist_fxn = dn_destroy_timer;
448         scp->persist = dn_nsp_persist(sk);
449
450 out:
451         kfree_skb(skb);
452 }
453
454 /*
455  * disc_conf messages are also called no_resources or no_link
456  * messages depending upon the "reason" field.
457  */
458 static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
459 {
460         struct dn_scp *scp = DN_SK(sk);
461         unsigned short reason;
462
463         if (skb->len != 2)
464                 goto out;
465
466         reason = dn_ntohs(*(__u16 *)skb->data);
467
468         sk->sk_state = TCP_CLOSE;
469
470         switch(scp->state) {
471                 case DN_CI:
472                         scp->state = DN_NR;
473                         break;
474                 case DN_DR:
475                         if (reason == NSP_REASON_DC)
476                                 scp->state = DN_DRC;
477                         if (reason == NSP_REASON_NL)
478                                 scp->state = DN_CN;
479                         break;
480                 case DN_DI:
481                         scp->state = DN_DIC;
482                         break;
483                 case DN_RUN:
484                         sk->sk_shutdown |= SHUTDOWN_MASK;
485                 case DN_CC:
486                         scp->state = DN_CN;
487         }
488
489         if (!sock_flag(sk, SOCK_DEAD)) {
490                 if (sk->sk_socket->state != SS_UNCONNECTED)
491                         sk->sk_socket->state = SS_DISCONNECTING;
492                 sk->sk_state_change(sk);
493         }
494
495         scp->persist_fxn = dn_destroy_timer;
496         scp->persist = dn_nsp_persist(sk);
497
498 out:
499         kfree_skb(skb);
500 }
501
502 static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
503 {
504         struct dn_scp *scp = DN_SK(sk);
505         unsigned short segnum;
506         unsigned char lsflags;
507         char fcval;
508         int wake_up = 0;
509         char *ptr = skb->data;
510         unsigned char fctype = scp->services_rem & NSP_FC_MASK;
511
512         if (skb->len != 4)
513                 goto out;
514
515         segnum = dn_ntohs(*(__u16 *)ptr);
516         ptr += 2;
517         lsflags = *(unsigned char *)ptr++;
518         fcval = *ptr;
519
520         /*
521          * Here we ignore erronous packets which should really
522          * should cause a connection abort. It is not critical 
523          * for now though.
524          */
525         if (lsflags & 0xf8)
526                 goto out;
527
528         if (seq_next(scp->numoth_rcv, segnum)) {
529                 seq_add(&scp->numoth_rcv, 1);
530                 switch(lsflags & 0x04) { /* FCVAL INT */
531                 case 0x00: /* Normal Request */
532                         switch(lsflags & 0x03) { /* FCVAL MOD */
533                         case 0x00: /* Request count */
534                                 if (fcval < 0) {
535                                         unsigned char p_fcval = -fcval;
536                                         if ((scp->flowrem_dat > p_fcval) &&
537                                             (fctype == NSP_FC_SCMC)) {
538                                                 scp->flowrem_dat -= p_fcval;
539                                         }
540                                 } else if (fcval > 0) {
541                                         scp->flowrem_dat += fcval;
542                                         wake_up = 1;
543                                 }
544                                 break;
545                         case 0x01: /* Stop outgoing data */
546                                 scp->flowrem_sw = DN_DONTSEND;
547                                 break;
548                         case 0x02: /* Ok to start again */
549                                 scp->flowrem_sw = DN_SEND;
550                                 dn_nsp_output(sk);
551                                 wake_up = 1;
552                         }
553                         break;
554                 case 0x04: /* Interrupt Request */
555                         if (fcval > 0) {
556                                 scp->flowrem_oth += fcval;
557                                 wake_up = 1;
558                         }
559                         break;
560                 }
561                 if (wake_up && !sock_flag(sk, SOCK_DEAD))
562                         sk->sk_state_change(sk);
563         }
564
565         dn_nsp_send_oth_ack(sk);
566
567 out:
568         kfree_skb(skb);
569 }
570
571 /*
572  * Copy of sock_queue_rcv_skb (from sock.h) without
573  * bh_lock_sock() (its already held when this is called) which
574  * also allows data and other data to be queued to a socket.
575  */
576 static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
577 {
578         int err;
579         
580         /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
581            number of warnings when compiling with -W --ANK
582          */
583         if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
584             (unsigned)sk->sk_rcvbuf) {
585                 err = -ENOMEM;
586                 goto out;
587         }
588
589         err = sk_filter(sk, skb, 0);
590         if (err)
591                 goto out;
592
593         skb_set_owner_r(skb, sk);
594         skb_queue_tail(queue, skb);
595
596         /* This code only runs from BH or BH protected context.
597          * Therefore the plain read_lock is ok here. -DaveM
598          */
599         read_lock(&sk->sk_callback_lock);
600         if (!sock_flag(sk, SOCK_DEAD)) {
601                 struct socket *sock = sk->sk_socket;
602                 wake_up_interruptible(sk->sk_sleep);
603                 if (sock && sock->fasync_list &&
604                     !test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
605                         __kill_fasync(sock->fasync_list, sig, 
606                                     (sig == SIGURG) ? POLL_PRI : POLL_IN);
607         }
608         read_unlock(&sk->sk_callback_lock);
609 out:
610         return err;
611 }
612
613 static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
614 {
615         struct dn_scp *scp = DN_SK(sk);
616         unsigned short segnum;
617         struct dn_skb_cb *cb = DN_SKB_CB(skb);
618         int queued = 0;
619
620         if (skb->len < 2)
621                 goto out;
622
623         cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
624         skb_pull(skb, 2);
625
626         if (seq_next(scp->numoth_rcv, segnum)) {
627
628                 if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
629                         seq_add(&scp->numoth_rcv, 1);
630                         scp->other_report = 0;
631                         queued = 1;
632                 }
633         }
634
635         dn_nsp_send_oth_ack(sk);
636 out:
637         if (!queued)
638                 kfree_skb(skb);
639 }
640
641 static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
642 {
643         int queued = 0;
644         unsigned short segnum;
645         struct dn_skb_cb *cb = DN_SKB_CB(skb);
646         struct dn_scp *scp = DN_SK(sk);
647
648         if (skb->len < 2)
649                 goto out;
650
651         cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
652         skb_pull(skb, 2);
653
654         if (seq_next(scp->numdat_rcv, segnum)) {
655                 if (dn_queue_skb(sk, skb, SIGIO, &sk->sk_receive_queue) == 0) {
656                         seq_add(&scp->numdat_rcv, 1);
657                         queued = 1;
658                 }
659
660                 if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
661                         scp->flowloc_sw = DN_DONTSEND;
662                         dn_nsp_send_link(sk, DN_DONTSEND, 0);
663                 }
664         }
665
666         dn_nsp_send_data_ack(sk);
667 out:
668         if (!queued)
669                 kfree_skb(skb);
670 }
671
672 /*
673  * If one of our conninit messages is returned, this function
674  * deals with it. It puts the socket into the NO_COMMUNICATION
675  * state.
676  */
677 static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
678 {
679         struct dn_scp *scp = DN_SK(sk);
680
681         if (scp->state == DN_CI) {
682                 scp->state = DN_NC;
683                 sk->sk_state = TCP_CLOSE;
684                 if (!sock_flag(sk, SOCK_DEAD))
685                         sk->sk_state_change(sk);
686         }
687
688         kfree_skb(skb);
689 }
690
691 static int dn_nsp_no_socket(struct sk_buff *skb, unsigned short reason)
692 {
693         struct dn_skb_cb *cb = DN_SKB_CB(skb);
694         int ret = NET_RX_DROP;
695
696         /* Must not reply to returned packets */
697         if (cb->rt_flags & DN_RT_F_RTS)
698                 goto out;
699
700         if ((reason != NSP_REASON_OK) && ((cb->nsp_flags & 0x0c) == 0x08)) {
701                 switch(cb->nsp_flags & 0x70) {
702                         case 0x10:
703                         case 0x60: /* (Retransmitted) Connect Init */
704                                 dn_nsp_return_disc(skb, NSP_DISCINIT, reason);
705                                 ret = NET_RX_SUCCESS;
706                                 break;
707                         case 0x20: /* Connect Confirm */
708                                 dn_nsp_return_disc(skb, NSP_DISCCONF, reason);
709                                 ret = NET_RX_SUCCESS;
710                                 break;
711                 }
712         }
713
714 out:
715         kfree_skb(skb);
716         return ret;
717 }
718
719 static int dn_nsp_rx_packet(struct sk_buff *skb)
720 {
721         struct dn_skb_cb *cb = DN_SKB_CB(skb);
722         struct sock *sk = NULL;
723         unsigned char *ptr = (unsigned char *)skb->data;
724         unsigned short reason = NSP_REASON_NL;
725
726         if (!pskb_may_pull(skb, 2))
727                 goto free_out;
728
729         skb->h.raw    = skb->data;
730         cb->nsp_flags = *ptr++;
731
732         if (decnet_debug_level & 2)
733                 printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
734
735         if (cb->nsp_flags & 0x83) 
736                 goto free_out;
737
738         /*
739          * Filter out conninits and useless packet types
740          */
741         if ((cb->nsp_flags & 0x0c) == 0x08) {
742                 switch(cb->nsp_flags & 0x70) {
743                         case 0x00: /* NOP */
744                         case 0x70: /* Reserved */
745                         case 0x50: /* Reserved, Phase II node init */
746                                 goto free_out;
747                         case 0x10:
748                         case 0x60:
749                                 if (unlikely(cb->rt_flags & DN_RT_F_RTS))
750                                         goto free_out;
751                                 sk = dn_find_listener(skb, &reason);
752                                 goto got_it;
753                 }
754         }
755
756         if (!pskb_may_pull(skb, 3))
757                 goto free_out;
758
759         /*
760          * Grab the destination address.
761          */
762         cb->dst_port = *(unsigned short *)ptr;
763         cb->src_port = 0;
764         ptr += 2;
765
766         /*
767          * If not a connack, grab the source address too.
768          */
769         if (pskb_may_pull(skb, 5)) {
770                 cb->src_port = *(unsigned short *)ptr;
771                 ptr += 2;
772                 skb_pull(skb, 5);
773         }
774
775         /*
776          * Returned packets...
777          * Swap src & dst and look up in the normal way.
778          */
779         if (unlikely(cb->rt_flags & DN_RT_F_RTS)) {
780                 unsigned short tmp = cb->dst_port;
781                 cb->dst_port = cb->src_port;
782                 cb->src_port = tmp;
783                 tmp = cb->dst;
784                 cb->dst = cb->src;
785                 cb->src = tmp;
786         }
787
788         /*
789          * Find the socket to which this skb is destined.
790          */
791         sk = dn_find_by_skb(skb);
792 got_it:
793         if (sk != NULL) {
794                 struct dn_scp *scp = DN_SK(sk);
795                 int ret;
796
797                 /* Reset backoff */
798                 scp->nsp_rxtshift = 0;
799
800                 /*
801                  * We linearize everything except data segments here.
802                  */
803                 if (cb->nsp_flags & ~0x60) {
804                         if (unlikely(skb_is_nonlinear(skb)) &&
805                             skb_linearize(skb, GFP_ATOMIC) != 0)
806                                 goto free_out;
807                 }
808
809                 bh_lock_sock(sk);
810                 ret = NET_RX_SUCCESS;
811                 if (decnet_debug_level & 8)
812                         printk(KERN_DEBUG "NSP: 0x%02x 0x%02x 0x%04x 0x%04x %d\n",
813                                 (int)cb->rt_flags, (int)cb->nsp_flags, 
814                                 (int)cb->src_port, (int)cb->dst_port, 
815                                 !!sock_owned_by_user(sk));
816                 if (!sock_owned_by_user(sk))
817                         ret = dn_nsp_backlog_rcv(sk, skb);
818                 else
819                         sk_add_backlog(sk, skb);
820                 bh_unlock_sock(sk);
821                 sock_put(sk);
822
823                 return ret;
824         }
825
826         return dn_nsp_no_socket(skb, reason);
827
828 free_out:
829         kfree_skb(skb);
830         return NET_RX_DROP;
831 }
832
833 int dn_nsp_rx(struct sk_buff *skb)
834 {
835         return NF_HOOK(PF_DECnet, NF_DN_LOCAL_IN, skb, skb->dev, NULL, dn_nsp_rx_packet);
836 }
837
838 /*
839  * This is the main receive routine for sockets. It is called
840  * from the above when the socket is not busy, and also from
841  * sock_release() when there is a backlog queued up.
842  */
843 int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
844 {
845         struct dn_scp *scp = DN_SK(sk);
846         struct dn_skb_cb *cb = DN_SKB_CB(skb);
847
848         if (cb->rt_flags & DN_RT_F_RTS) {
849                 if (cb->nsp_flags == 0x18 || cb->nsp_flags == 0x68)
850                         dn_returned_conn_init(sk, skb);
851                 else
852                         kfree_skb(skb);
853                 return NET_RX_SUCCESS;
854         }
855
856         /*
857          * Control packet.
858          */
859         if ((cb->nsp_flags & 0x0c) == 0x08) {
860                 switch(cb->nsp_flags & 0x70) {
861                         case 0x10:
862                         case 0x60:
863                                 dn_nsp_conn_init(sk, skb);
864                                 break;
865                         case 0x20:
866                                 dn_nsp_conn_conf(sk, skb);
867                                 break;
868                         case 0x30:
869                                 dn_nsp_disc_init(sk, skb);
870                                 break;
871                         case 0x40:      
872                                 dn_nsp_disc_conf(sk, skb);
873                                 break;
874                 }
875
876         } else if (cb->nsp_flags == 0x24) {
877                 /*
878                  * Special for connacks, 'cos they don't have
879                  * ack data or ack otherdata info.
880                  */
881                 dn_nsp_conn_ack(sk, skb);
882         } else {
883                 int other = 1;
884
885                 /* both data and ack frames can kick a CC socket into RUN */
886                 if ((scp->state == DN_CC) && !sock_flag(sk, SOCK_DEAD)) {
887                         scp->state = DN_RUN;
888                         sk->sk_state = TCP_ESTABLISHED;
889                         sk->sk_state_change(sk);
890                 }
891
892                 if ((cb->nsp_flags & 0x1c) == 0)
893                         other = 0;
894                 if (cb->nsp_flags == 0x04)
895                         other = 0;
896
897                 /*
898                  * Read out ack data here, this applies equally
899                  * to data, other data, link serivce and both
900                  * ack data and ack otherdata.
901                  */
902                 dn_process_ack(sk, skb, other);
903
904                 /*
905                  * If we've some sort of data here then call a
906                  * suitable routine for dealing with it, otherwise
907                  * the packet is an ack and can be discarded.
908                  */
909                 if ((cb->nsp_flags & 0x0c) == 0) {
910
911                         if (scp->state != DN_RUN)
912                                 goto free_out;
913
914                         switch(cb->nsp_flags) {
915                                 case 0x10: /* LS */
916                                         dn_nsp_linkservice(sk, skb);
917                                         break;
918                                 case 0x30: /* OD */
919                                         dn_nsp_otherdata(sk, skb);
920                                         break;
921                                 default:
922                                         dn_nsp_data(sk, skb);
923                         }
924
925                 } else { /* Ack, chuck it out here */
926 free_out:
927                         kfree_skb(skb);
928                 }
929         }
930
931         return NET_RX_SUCCESS;
932 }
933