Import 2.4.0-test2pre7
[davej-history.git] / net / decnet / dn_nsp_in.c
blob6155ebccfd912cb3aab22ba5d9b8af29571c0b49
2 /*
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Network Services Protocol (Input)
9 * Author: Eduardo Marcelo Serrat <emserrat@geocities.com>
11 * Changes:
13 * Steve Whitehouse: Split into dn_nsp_in.c and dn_nsp_out.c from
14 * original dn_nsp.c.
15 * Steve Whitehouse: Updated to work with my new routing architecture.
16 * Steve Whitehouse: Add changes from Eduardo Serrat's patches.
17 * Steve Whitehouse: Put all ack handling code in a common routine.
18 * Steve Whitehouse: Put other common bits into dn_nsp_rx()
19 * Steve Whitehouse: More checks on skb->len to catch bogus packets
20 * Fixed various race conditions and possible nasties.
21 * Steve Whitehouse: Now handles returned conninit frames.
22 * David S. Miller: New socket locking
23 * Steve Whitehouse: Fixed lockup when socket filtering was enabled.
24 * Paul Koning: Fix to push CC sockets into RUN when acks are
25 * received.
28 /******************************************************************************
29 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
31 This program is free software; you can redistribute it and/or modify
32 it under the terms of the GNU General Public License as published by
33 the Free Software Foundation; either version 2 of the License, or
34 any later version.
36 This program is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 GNU General Public License for more details.
40 *******************************************************************************/
42 #include <linux/config.h>
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/in.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/timer.h>
50 #include <linux/string.h>
51 #include <linux/sockios.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/inet.h>
55 #include <linux/route.h>
56 #include <net/sock.h>
57 #include <asm/segment.h>
58 #include <asm/system.h>
59 #include <linux/fcntl.h>
60 #include <linux/mm.h>
61 #include <linux/termios.h>
62 #include <linux/interrupt.h>
63 #include <linux/proc_fs.h>
64 #include <linux/stat.h>
65 #include <linux/init.h>
66 #include <linux/poll.h>
67 #include <linux/netfilter_decnet.h>
68 #include <net/neighbour.h>
69 #include <net/dst.h>
70 #include <net/dn_nsp.h>
71 #include <net/dn_dev.h>
72 #include <net/dn_route.h>
76 * For this function we've flipped the cross-subchannel bit
77 * if the message is an otherdata or linkservice message. Thus
78 * we can use it to work out what to update.
80 static void dn_ack(struct sock *sk, struct sk_buff *skb, unsigned short ack)
82 struct dn_scp *scp = &sk->protinfo.dn;
83 unsigned short type = ((ack >> 12) & 0x0003);
84 int wakeup = 0;
86 /* printk(KERN_DEBUG "dn_ack: %hd 0x%04hx\n", type, ack); */
88 switch(type) {
89 case 0: /* ACK - Data */
90 if (after(ack, scp->ackrcv_dat)) {
91 scp->ackrcv_dat = ack & 0x0fff;
92 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->data_xmit_queue, ack);
94 break;
95 case 1: /* NAK - Data */
96 break;
97 case 2: /* ACK - OtherData */
98 if (after(ack, scp->ackrcv_oth)) {
99 scp->ackrcv_oth = ack & 0x0fff;
100 wakeup |= dn_nsp_check_xmit_queue(sk, skb, &scp->other_xmit_queue, ack);
102 break;
103 case 3: /* NAK - OtherData */
104 break;
107 if (wakeup && !sk->dead)
108 sk->state_change(sk);
112 * This function is a universal ack processor.
114 static int dn_process_ack(struct sock *sk, struct sk_buff *skb, int oth)
116 unsigned short *ptr = (unsigned short *)skb->data;
117 int len = 0;
118 unsigned short ack;
120 if (skb->len < 2)
121 return len;
123 if ((ack = dn_ntohs(*ptr)) & 0x8000) {
124 skb_pull(skb, 2);
125 ptr++;
126 len += 2;
127 if ((ack & 0x4000) == 0) {
128 if (oth)
129 ack ^= 0x2000;
130 dn_ack(sk, skb, ack);
134 if (skb->len < 2)
135 return len;
137 if ((ack = dn_ntohs(*ptr)) & 0x8000) {
138 skb_pull(skb, 2);
139 len += 2;
140 if ((ack & 0x4000) == 0) {
141 if (oth)
142 ack ^= 0x2000;
143 dn_ack(sk, skb, ack);
147 return len;
152 * This function uses a slightly different lookup method
153 * to find its sockets, since it searches on object name/number
154 * rather than port numbers
156 static struct sock *dn_find_listener(struct sk_buff *skb)
158 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
159 struct nsp_conn_init_msg *msg = (struct nsp_conn_init_msg *)skb->data;
160 struct sockaddr_dn addr;
161 unsigned char type = 0;
163 memset(&addr, 0, sizeof(struct sockaddr_dn));
165 cb->src_port = msg->srcaddr;
166 cb->dst_port = msg->dstaddr;
167 cb->services = msg->services;
168 cb->info = msg->info;
169 cb->segsize = dn_ntohs(msg->segsize);
171 skb_pull(skb, sizeof(*msg));
173 /* printk(KERN_DEBUG "username2sockaddr 1\n"); */
174 if (dn_username2sockaddr(skb->data, skb->len, &addr, &type) < 0)
175 goto err_out;
177 if (type > 1)
178 goto err_out;
180 /* printk(KERN_DEBUG "looking for listener...\n"); */
181 return dn_sklist_find_listener(&addr);
182 err_out:
183 return NULL;
186 static void dn_nsp_conn_init(struct sock *sk, struct sk_buff *skb)
188 /* printk(KERN_DEBUG "checking backlog...\n"); */
189 if (sk->ack_backlog >= sk->max_ack_backlog) {
190 kfree_skb(skb);
191 return;
194 /* printk(KERN_DEBUG "waking up socket...\n"); */
195 sk->ack_backlog++;
196 skb_queue_tail(&sk->receive_queue, skb);
197 sk->state_change(sk);
200 static void dn_nsp_conn_conf(struct sock *sk, struct sk_buff *skb)
202 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
203 struct dn_scp *scp = &sk->protinfo.dn;
205 if (skb->len < 3)
206 goto out;
208 cb->services = *skb->data;
209 cb->info = *(skb->data+1);
210 skb_pull(skb, 2);
211 cb->segsize = dn_ntohs(*(__u16 *)skb->data);
212 skb_pull(skb, 2);
215 * FIXME: Check out services and info fields to check that
216 * we can talk to this kind of node.
219 if ((scp->state == DN_CI) || (scp->state == DN_CD)) {
220 scp->persist = 0;
221 scp->addrrem = cb->src_port;
222 sk->state = TCP_ESTABLISHED;
223 scp->state = DN_RUN;
225 if (scp->mss > cb->segsize)
226 scp->mss = cb->segsize;
227 if (scp->mss < 230)
228 scp->mss = 230;
230 if (skb->len > 0) {
231 unsigned char dlen = *skb->data;
232 if ((dlen <= 16) && (dlen <= skb->len)) {
233 scp->conndata_in.opt_optl = dlen;
234 memcpy(scp->conndata_in.opt_data, skb->data + 1, dlen);
237 dn_nsp_send_lnk(sk, DN_NOCHANGE);
238 if (!sk->dead)
239 sk->state_change(sk);
242 out:
243 kfree_skb(skb);
246 static void dn_nsp_conn_ack(struct sock *sk, struct sk_buff *skb)
248 struct dn_scp *scp = &sk->protinfo.dn;
250 if (scp->state == DN_CI) {
251 scp->state = DN_CD;
252 scp->persist = 0;
255 kfree_skb(skb);
258 static void dn_nsp_disc_init(struct sock *sk, struct sk_buff *skb)
260 struct dn_scp *scp = &sk->protinfo.dn;
261 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
262 unsigned short reason;
264 if (skb->len < 2)
265 goto out;
267 reason = dn_ntohs(*(__u16 *)skb->data);
268 skb_pull(skb, 2);
270 scp->discdata_in.opt_status = reason;
271 scp->discdata_in.opt_optl = 0;
272 memset(scp->discdata_in.opt_data, 0, 16);
274 if (skb->len > 0) {
275 unsigned char dlen = *skb->data;
276 if ((dlen <= 16) && (dlen <= skb->len)) {
277 scp->discdata_in.opt_optl = dlen;
278 memcpy(scp->discdata_in.opt_data, skb->data + 1, dlen);
282 scp->addrrem = cb->src_port;
283 sk->state = TCP_CLOSE;
285 switch(scp->state) {
286 case DN_CI:
287 case DN_CD:
288 scp->state = DN_RJ;
289 break;
290 case DN_RUN:
291 sk->shutdown |= SHUTDOWN_MASK;
292 scp->state = DN_DN;
293 break;
294 case DN_DI:
295 scp->state = DN_DIC;
296 break;
299 if (!sk->dead) {
300 if (sk->socket->state != SS_UNCONNECTED)
301 sk->socket->state = SS_DISCONNECTING;
302 sk->state_change(sk);
305 dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, GFP_ATOMIC);
306 scp->persist_fxn = dn_destroy_timer;
307 scp->persist = dn_nsp_persist(sk);
309 out:
310 kfree_skb(skb);
314 * disc_conf messages are also called no_resources or no_link
315 * messages depending upon the "reason" field.
317 static void dn_nsp_disc_conf(struct sock *sk, struct sk_buff *skb)
319 struct dn_scp *scp = &sk->protinfo.dn;
320 unsigned short reason;
322 if (skb->len != 2)
323 goto out;
325 reason = dn_ntohs(*(__u16 *)skb->data);
327 sk->state = TCP_CLOSE;
329 switch(scp->state) {
330 case DN_CI:
331 scp->state = DN_NR;
332 break;
333 case DN_DR:
334 if (reason == NSP_REASON_DC)
335 scp->state = DN_DRC;
336 if (reason == NSP_REASON_NL)
337 scp->state = DN_CN;
338 break;
339 case DN_DI:
340 scp->state = DN_DIC;
341 break;
342 case DN_RUN:
343 sk->shutdown |= SHUTDOWN_MASK;
344 case DN_CC:
345 scp->state = DN_CN;
348 if (!sk->dead) {
349 if (sk->socket->state != SS_UNCONNECTED)
350 sk->socket->state = SS_DISCONNECTING;
351 sk->state_change(sk);
354 scp->persist_fxn = dn_destroy_timer;
355 scp->persist = dn_nsp_persist(sk);
357 out:
358 kfree_skb(skb);
361 static void dn_nsp_linkservice(struct sock *sk, struct sk_buff *skb)
363 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
364 unsigned short segnum;
365 unsigned char lsflags;
366 char fcval;
368 if (skb->len != 4)
369 goto out;
371 cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
372 skb_pull(skb, 2);
373 lsflags = *(unsigned char *)skb->data;
374 skb_pull(skb, 1);
375 fcval = *(char *)skb->data;
377 if (lsflags & 0xf0)
378 goto out;
380 if (((sk->protinfo.dn.numoth_rcv + 1) & 0x0FFF) == (segnum & 0x0FFF)) {
381 sk->protinfo.dn.numoth_rcv += 1;
382 switch(lsflags & 0x03) {
383 case 0x00:
384 break;
385 case 0x01:
386 sk->protinfo.dn.flowrem_sw = DN_DONTSEND;
387 break;
388 case 0x02:
389 sk->protinfo.dn.flowrem_sw = DN_SEND;
390 dn_nsp_output(sk);
391 if (!sk->dead)
392 sk->state_change(sk);
397 dn_nsp_send_oth_ack(sk);
399 out:
400 kfree_skb(skb);
404 * Copy of sock_queue_rcv_skb (from sock.h) without
405 * bh_lock_sock() (its already held when this is called) which
406 * also allows data and other data to be queued to a socket.
408 static __inline__ int dn_queue_skb(struct sock *sk, struct sk_buff *skb, int sig, struct sk_buff_head *queue)
410 #ifdef CONFIG_FILTER
411 struct sk_filter *filter;
412 #endif
414 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
415 number of warnings when compiling with -W --ANK
417 if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf
419 return -ENOMEM;
421 #ifdef CONFIG_FILTER
422 if (sk->filter) {
423 int err = 0;
424 if ((filter = sk->filter) != NULL && sk_filter(skb, sk->filter))
425 err = -EPERM; /* Toss packet */
426 if (err)
427 return err;
429 #endif /* CONFIG_FILTER */
431 skb_set_owner_r(skb, sk);
432 skb_queue_tail(queue, skb);
434 /* This code only runs from BH or BH protected context.
435 * Therefore the plain read_lock is ok here. -DaveM
437 read_lock(&sk->callback_lock);
438 if (!sk->dead) {
439 struct socket *sock = sk->socket;
440 wake_up_interruptible(sk->sleep);
441 if (sock && sock->fasync_list &&
442 !test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
443 __kill_fasync(sock->fasync_list, sig,
444 (sig == SIGURG) ? POLL_PRI : POLL_IN);
446 read_unlock(&sk->callback_lock);
448 return 0;
451 static void dn_nsp_otherdata(struct sock *sk, struct sk_buff *skb)
453 struct dn_scp *scp = &sk->protinfo.dn;
454 unsigned short segnum;
455 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
456 int queued = 0;
458 if (skb->len < 2)
459 goto out;
461 cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
462 skb_pull(skb, 2);
464 if (((sk->protinfo.dn.numoth_rcv + 1) & 0x0fff) == (segnum & 0x0fff)) {
466 if (dn_queue_skb(sk, skb, SIGURG, &scp->other_receive_queue) == 0) {
467 sk->protinfo.dn.numoth_rcv++;
468 scp->other_report = 0;
469 queued = 1;
473 dn_nsp_send_oth_ack(sk);
474 out:
475 if (!queued)
476 kfree_skb(skb);
479 static void dn_nsp_data(struct sock *sk, struct sk_buff *skb)
481 int queued = 0;
482 unsigned short segnum;
483 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
484 struct dn_scp *scp = &sk->protinfo.dn;
486 if (skb->len < 2)
487 goto out;
489 cb->segnum = segnum = dn_ntohs(*(__u16 *)skb->data);
490 skb_pull(skb, 2);
492 if (((sk->protinfo.dn.numdat_rcv + 1) & 0x0FFF) ==
493 (segnum & 0x0FFF)) {
495 if (dn_queue_skb(sk, skb, SIGIO, &sk->receive_queue) == 0) {
496 sk->protinfo.dn.numdat_rcv++;
497 queued = 1;
500 if ((scp->flowloc_sw == DN_SEND) && dn_congested(sk)) {
501 scp->flowloc_sw = DN_DONTSEND;
502 dn_nsp_send_lnk(sk, DN_DONTSEND);
506 dn_nsp_send_data_ack(sk);
507 out:
508 if (!queued)
509 kfree_skb(skb);
513 * If one of our conninit messages is returned, this function
514 * deals with it. It puts the socket into the NO_COMMUNICATION
515 * state.
517 static void dn_returned_conn_init(struct sock *sk, struct sk_buff *skb)
519 struct dn_scp *scp = &sk->protinfo.dn;
521 if (scp->state == DN_CI) {
522 scp->state = DN_NC;
523 sk->state = TCP_CLOSE;
524 if (!sk->dead)
525 sk->state_change(sk);
528 kfree_skb(skb);
531 static void dn_nsp_no_socket(struct sk_buff *skb)
533 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
535 switch(cb->nsp_flags) {
536 case 0x28: /* Connect Confirm */
537 dn_nsp_return_disc(skb, NSP_DISCCONF, NSP_REASON_NL);
540 kfree_skb(skb);
543 static int dn_nsp_rx_packet(struct sk_buff *skb)
545 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
546 struct sock *sk = NULL;
547 unsigned char *ptr = (unsigned char *)skb->data;
549 skb->h.raw = skb->data;
550 cb->nsp_flags = *ptr++;
552 if (decnet_debug_level & 2)
553 printk(KERN_DEBUG "dn_nsp_rx: Message type 0x%02x\n", (int)cb->nsp_flags);
555 if (skb->len < 2)
556 goto free_out;
558 if (cb->nsp_flags & 0x83)
559 goto free_out;
562 * Returned packets...
563 * Swap src & dst and look up in the normal way.
565 if (cb->rt_flags & DN_RT_F_RTS) {
566 unsigned short tmp = cb->dst_port;
567 cb->dst_port = cb->src_port;
568 cb->src_port = tmp;
569 tmp = cb->dst;
570 cb->dst = cb->src;
571 cb->src = tmp;
572 sk = dn_find_by_skb(skb);
573 goto got_it;
577 * Filter out conninits and useless packet types
579 if ((cb->nsp_flags & 0x0c) == 0x08) {
580 switch(cb->nsp_flags & 0x70) {
581 case 0x00: /* NOP */
582 case 0x70: /* Reserved */
583 case 0x50: /* Reserved, Phase II node init */
584 goto free_out;
585 case 0x10:
586 case 0x60:
587 sk = dn_find_listener(skb);
588 goto got_it;
592 if (skb->len < 3)
593 goto free_out;
596 * Grab the destination address.
598 cb->dst_port = *(unsigned short *)ptr;
599 cb->src_port = 0;
600 ptr += 2;
603 * If not a connack, grab the source address too.
605 if (skb->len >= 5) {
606 cb->src_port = *(unsigned short *)ptr;
607 ptr += 2;
608 skb_pull(skb, 5);
612 * Find the socket to which this skb is destined.
614 sk = dn_find_by_skb(skb);
615 got_it:
616 if (sk != NULL) {
617 struct dn_scp *scp = &sk->protinfo.dn;
618 int ret;
620 /* Reset backoff */
621 scp->nsp_rxtshift = 0;
623 bh_lock_sock(sk);
624 ret = 0;
625 if (sk->lock.users == 0)
626 ret = dn_nsp_backlog_rcv(sk, skb);
627 else
628 sk_add_backlog(sk, skb);
629 bh_unlock_sock(sk);
630 sock_put(sk);
632 return ret;
635 dn_nsp_no_socket(skb);
636 return 1;
638 free_out:
639 kfree_skb(skb);
640 return 0;
643 int dn_nsp_rx(struct sk_buff *skb)
645 return NF_HOOK(PF_DECnet, NF_DN_LOCAL_IN, skb, skb->rx_dev, NULL, dn_nsp_rx_packet);
649 * This is the main receive routine for sockets. It is called
650 * from the above when the socket is not busy, and also from
651 * sock_release() when there is a backlog queued up.
653 int dn_nsp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
655 struct dn_scp *scp = &sk->protinfo.dn;
656 struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb;
658 if (cb->rt_flags & DN_RT_F_RTS) {
659 dn_returned_conn_init(sk, skb);
660 return 0;
664 * Control packet.
666 if ((cb->nsp_flags & 0x0c) == 0x08) {
667 /* printk(KERN_DEBUG "control type\n"); */
668 switch(cb->nsp_flags & 0x70) {
669 case 0x10:
670 case 0x60:
671 dn_nsp_conn_init(sk, skb);
672 break;
673 case 0x20:
674 dn_nsp_conn_conf(sk, skb);
675 break;
676 case 0x30:
677 dn_nsp_disc_init(sk, skb);
678 break;
679 case 0x40:
680 dn_nsp_disc_conf(sk, skb);
681 break;
684 } else if (cb->nsp_flags == 0x24) {
686 * Special for connacks, 'cos they don't have
687 * ack data or ack otherdata info.
689 dn_nsp_conn_ack(sk, skb);
690 } else {
691 int other = 1;
693 /* both data and ack frames can kick a CC socket into RUN */
694 if ((scp->state == DN_CC) && !sk->dead) {
695 scp->state = DN_RUN;
696 sk->state = TCP_ESTABLISHED;
697 sk->state_change(sk);
700 if ((cb->nsp_flags & 0x1c) == 0)
701 other = 0;
702 if (cb->nsp_flags == 0x04)
703 other = 0;
706 * Read out ack data here, this applies equally
707 * to data, other data, link serivce and both
708 * ack data and ack otherdata.
710 dn_process_ack(sk, skb, other);
713 * If we've some sort of data here then call a
714 * suitable routine for dealing with it, otherwise
715 * the packet is an ack and can be discarded.
717 if ((cb->nsp_flags & 0x0c) == 0) {
719 if (scp->state != DN_RUN)
720 goto free_out;
722 switch(cb->nsp_flags) {
723 case 0x10: /* LS */
724 dn_nsp_linkservice(sk, skb);
725 break;
726 case 0x30: /* OD */
727 dn_nsp_otherdata(sk, skb);
728 break;
729 default:
730 dn_nsp_data(sk, skb);
733 } else { /* Ack, chuck it out here */
734 free_out:
735 kfree_skb(skb);
739 return 0;