]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Packet.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 * v01.23 All incoming packets come here first 06/21/90 mbs
34 * Modified for MP, 1996 by Tuyen Nguyen
35 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <machine/spl.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
48 #include <sys/ioctl.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
56 #include <netat/sysglue.h>
57 #include <netat/appletalk.h>
58 #include <netat/at_pcb.h>
59 #include <netat/ddp.h>
60 #include <netat/at_var.h>
62 #include <netat/adsp.h>
63 #include <netat/adsp_internal.h>
65 extern at_ifaddr_t
*ifID_home
;
70 * We just got a packet for this session, glean its address &
78 static void GleanSession(sp
) /* (CCBPtr sp) */
81 if (sp
->openState
== O_STATE_OPEN
) {
82 /* This is true for both state = sOpen & sClosing */
83 RemoveTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
);
84 InsertTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
,
93 * The same code handles incoming Open Connection Request,
94 * Open Request + Ack, Open Connection Ack, Open Connection Denial
96 * We could be in four different states, LISTEN, OPENWAIT, ESTABLISHED,
102 * Ok, there are 16 combinations. 8 are do-nothings, 2 have to be
103 * special cased (Open Deny and Req+Ack on Open session)
105 * Build a table of actions:
107 * What to match on (local socket, whole address, DestCID, SrcCID)
108 * What to send (Ack or Req+Ack)
109 * Next State (both the ccb state and the open state)
116 u_char match
; /* Characteristics that have to match
117 * (Bit-Mapped, see below) */
118 char action
; /* What to do if CCB matches */
119 char send
; /* What to send in response
120 * (Bit mapped, same as sendCtl field of
122 char openState
; /* Next Open state */
123 char state
; /* Next ccb state. */
124 char pad
; /* Too bad we need this to make structure
128 #define M_LSOC 0x01 /* bit 0 - Match on local socket */
129 #define M_ADDR 0x02 /* bit 1 - Match on whole address */
130 #define M_DCID 0x04 /* bit 2 - Match on DestCID */
131 #define M_SCID 0x08 /* bit 3 - Match SrcCID */
132 #define M_DCIDZERO 0x10 /* bit 4 - Dest CID must be 0 */
133 #define M_SCIDZERO 0x20 /* bit 5 - Src CID must be 0 */
134 #define M_FILTER 0x40 /* bit 6 - Match address filter */
135 #define M_IGNORE 0x80 /* bit 7 - Ignore */
137 #define A_COMPLETE 0x01 /* Complete open parameter block */
138 #define A_SAVEPARMS 0x02 /* Save connection parameters */
139 #define A_OREQACKOPEN 0x04 /* special case for open Req+Ack on
141 #define A_GLEAN 0x08 /* We'll be talking back to this guy */
142 #define A_DENY 0x10 /* We've been denied! */
146 * So here's our table
149 static TBL tbl
[16] = {
152 * For Open Request ($81)
155 * Match on destination socket
156 * Match on address filter
159 * Save Open Connection parameters
161 * Change state to ESTABLISHED
163 { M_LSOC
+ M_DCIDZERO
+ M_FILTER
,
164 A_SAVEPARMS
+ A_GLEAN
,
174 * Match on Remote Address & destination socket
176 * Save Open Connection parameters
178 * Change state to ESTABLISHED
180 { M_LSOC
+ M_ADDR
+ M_DCIDZERO
,
181 A_SAVEPARMS
+ A_GLEAN
,
190 * Match on Remote Address & SrcCID
194 { M_ADDR
+ M_SCID
+ M_DCIDZERO
,
242 * Match on SrcCID & DestCID & Address & Local Socket
243 * Complete Listen or Connect PB
246 { M_ADDR
+ M_DCID
+ M_SCID
+ M_LSOC
,
247 A_COMPLETE
+ A_GLEAN
,
268 * For Open Request + Ack ($83)
283 * Match on DestCID & socket
284 * Do not test remote address -- our open req could have
285 * been passed to another address by a connection server
286 * Save Open Connection parameters
287 * Complete Connect parameter block
292 A_COMPLETE
+ A_SAVEPARMS
+ A_GLEAN
,
313 * Match on Remote Address & SrcCID & DestCID & Local Socket
314 * If we've never gotten any data
315 * Send Ack & Retransmit
317 { M_ADDR
+ M_DCID
+ M_SCID
+ M_LSOC
,
318 A_OREQACKOPEN
+ A_GLEAN
,
328 * For Open Deny ($84)
343 * Match on DestCID & Address
344 * Source CID must be 0
345 * Complete with error
347 { M_SCIDZERO
+ M_DCID
+ M_ADDR
,
365 }, /* %%% No we probably don't want to ignore in this case */
380 extern at_ifaddr_t
*ifID_table
[];
383 * Used to search down queue of sessions for a session waiting for an
392 byte idx
; /* Index into state tables */
393 TBLPtr t
; /* Ptr to entry in table above */
399 * Called by Rx connection to find which stream (if any) should get this open
400 * request/ack/req+ack/deny packet.
405 MatchStream(sp
, m
) /* (CCBPtr sp, MATCHPtr m) */
412 if (sp
->openState
< O_STATE_LISTEN
||
413 sp
->openState
> O_STATE_OPEN
)
417 m
->t
= &tbl
[sp
->openState
- O_STATE_LISTEN
+ m
->idx
];
419 match
= m
->t
->match
; /* Get match criteria */
421 if (match
& M_IGNORE
) /* Ignore this combination */
424 if (match
& M_LSOC
) { /* Match on Local socket */
425 if (sp
->localSocket
!= m
->socket
)
429 if (match
& M_ADDR
) { /* Match on Address */
431 addr
= m
->addr
; /* Make local copy for efficiency */
432 if (sp
->remoteAddress
.a
.node
!= addr
.a
.node
)
434 if (sp
->remoteAddress
.a
.socket
!= addr
.a
.socket
)
436 if (sp
->remoteAddress
.a
.net
&& addr
.a
.net
&&
437 (sp
->remoteAddress
.a
.net
!= addr
.a
.net
))
441 * Handle special case to reject self-sent open request
443 if ((m
->srcCID
== sp
->locCID
) &&
444 (addr
.a
.node
== ifID_home
->ifThisNode
.s_node
) &&
445 /* *** was (addr.a.node == ddpcfg.node_addr.node) && *** */
446 ((addr
.a
.net
== 0) ||
447 (ifID_home
->ifThisNode
.s_net
== 0) ||
448 (ifID_home
->ifThisNode
.s_net
== addr
.a
.net
)) )
450 (NET_VALUE(ddpcfg.node_addr.net) == 0) ||
451 (NET_VALUE(ddpcfg.node_addr.net) == NET_VALUE(addr.a.net))) )
453 /* CID's match, and */
454 /* If nodeID matches, and */
455 /* network matches, */
456 return 0; /* then came from us! */
459 if (match
& M_DCID
) { /* Match on DestCID */
460 if (sp
->locCID
!= m
->dstCID
)
464 if (match
& M_SCID
) { /* Match on SourceCID */
465 if (sp
->remCID
!= m
->srcCID
)
469 if (match
& M_DCIDZERO
) { /* Destination CID must be 0 */
474 if (match
& M_SCIDZERO
) /* Source CID must be 0 */
480 if (match
& M_FILTER
) { /* Check address filter? */
481 if ((opb
= sp
->opb
)) /* There should be a param block... */
484 addr
= m
->addr
; /* Make local copy for efficiency */
485 if ((opb
->u
.openParams
.filterAddress
.net
&&
487 opb
->u
.openParams
.filterAddress
.net
!= addr
.a
.net
) ||
488 (opb
->u
.openParams
.filterAddress
.node
!= 0 &&
489 opb
->u
.openParams
.filterAddress
.node
!= addr
.a
.node
)||
490 (opb
->u
.openParams
.filterAddress
.socket
!= 0 &&
491 opb
->u
.openParams
.filterAddress
.socket
!= addr
.a
.socket
))
502 * Called by rx connection to see which connection listener (if any) should
503 * get this incoming open connection request.
507 static boolean
MatchListener(sp
, m
) /* (CCBPtr sp, MATCHPtr m) */
512 if ((sp
->state
== (word
)sListening
) && /* This CCB is a listener */
513 (sp
->localSocket
== m
->socket
)) /* on the right socket */
522 * We just received one of the 4 Open Connection packets
523 * Interrupts are masked OFF at this point
526 * spPtr Place to put ptr to stream (if we found one -- not
528 * f Pointer to ADSP header for packet, data follows behind it
529 * len # of byte in ADSP header + data
530 * addr Who sent the packet
531 * dsoc Where they sent it to
534 * Returns 1 if packet was ignored
536 static int RXConnection(gref
, spPtr
, f
, len
, addr
, dsoc
)
537 /* (CCBPtr *spPtr, ADSP_FRAMEPtr f, word len, AddrUnion addr, byte dsoc) */
538 gref_t
*gref
; /* READ queue */
546 ADSP_OPEN_DATAPtr op
;
551 ADSP_OPEN_DATAPtr adspop
;
553 op
= (ADSP_OPEN_DATAPtr
)&f
->data
[0]; /* Point to Open-Connection parms */
554 len
-= ADSP_FRAME_LEN
;
556 if (len
< (sizeof(ADSP_OPEN_DATA
))) /* Packet too small */
560 if (UAS_VALUE(op
->version
) != netw(0x0100)) { /* Check version num (on even-byte) */
562 * The open request has been denied. Try to send him a denial.
565 mp
= gbuf_alloc(AT_WR_OFFSET
+ DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+ ADSP_OPEN_FRAME_LEN
,
567 gbuf_rinc(mp
,AT_WR_OFFSET
);
568 gbuf_wset(mp
,DDPL_FRAME_LEN
);
569 adspp
= (ADSP_FRAMEPtr
)gbuf_wptr(mp
);
570 gbuf_winc(mp
,ADSP_FRAME_LEN
);
571 bzero((caddr_t
) gbuf_rptr(mp
),DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+
572 ADSP_OPEN_FRAME_LEN
);
573 adspp
->descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_ODENY
;
574 adspop
= (ADSP_OPEN_DATAPtr
)gbuf_wptr(mp
);
575 gbuf_winc(mp
,ADSP_OPEN_FRAME_LEN
);
576 UAS_UAS(adspop
->dstCID
, f
->CID
);
577 UAS_ASSIGN_HTON(adspop
->version
, 0x100);
578 adsp_sendddp(0, mp
, DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+
579 ADSP_OPEN_FRAME_LEN
, &addr
, DDP_ADSP
);
585 m
.descriptor
= f
->descriptor
;
586 m
.srcCID
= UAS_VALUE_NTOH(f
->CID
);
587 m
.dstCID
= UAS_VALUE_NTOH(op
->dstCID
); /* On even-byte boundry */
588 m
.idx
= ((f
->descriptor
& ADSP_CONTROL_MASK
) - 1) * 4;
591 * See if we can find a stream that knows what to do with this packet
593 if ((sp
= (CCBPtr
)qfind_m(AT_ADSP_STREAMS
, &m
, (ProcPtr
)MatchStream
)) == 0)
598 * No match, so look for connection listeners if this is an
601 if ((f
->descriptor
& ADSP_CONTROL_MASK
) != (byte
)ADSP_CTL_OREQ
)
604 if ((sp
= (CCBPtr
)qfind_m(AT_ADSP_STREAMS
, &m
,
605 (ProcPtr
)MatchListener
)) == 0)
608 p
= (struct adspcmd
*)&sp
->opb
;
609 while (n
= (struct adspcmd
*)p
->qLink
) /* Hunt down list of listens */
611 /* Check address filter */
612 if (((n
->u
.openParams
.filterAddress
.net
== 0) ||
614 (n
->u
.openParams
.filterAddress
.net
== addr
.a
.net
)) &&
616 ((n
->u
.openParams
.filterAddress
.node
== 0) ||
617 (n
->u
.openParams
.filterAddress
.node
== addr
.a
.node
)) &&
619 ((n
->u
.openParams
.filterAddress
.socket
== 0) ||
620 (n
->u
.openParams
.filterAddress
.socket
== addr
.a
.socket
))) {
621 p
->qLink
= n
->qLink
; /* Unlink this param block */
622 n
->u
.openParams
.remoteCID
= m
.srcCID
;
623 *((AddrUnionPtr
)&n
->u
.openParams
.remoteAddress
) = addr
;
624 n
->u
.openParams
.sendSeq
= UAL_VALUE_NTOH(f
->pktNextRecvSeq
);
625 n
->u
.openParams
.sendWindow
= UAS_VALUE_NTOH(f
->pktRecvWdw
);
626 n
->u
.openParams
.attnSendSeq
= UAL_VALUE_NTOH(op
->pktAttnRecvSeq
);
628 completepb(sp
, n
); /* complete copy of request */
629 /* complete(n, 0); */
631 } /* found CLListen */
633 p
= n
; /* down the list we go... */
640 *spPtr
= sp
; /* Save ptr to stream we just found */
642 sp
->openState
= m
.t
->openState
; /* Move to next state (may be same) */
643 sp
->state
= m
.t
->state
; /* Move to next state (may be same) */
645 if (m
.t
->action
& A_SAVEPARMS
) { /* Need to Save open-conn parms */
646 sp
->firstRtmtSeq
= sp
->sendSeq
= UAL_VALUE_NTOH(f
->pktNextRecvSeq
);
647 sp
->sendWdwSeq
= UAL_VALUE_NTOH(f
->pktNextRecvSeq
) + UAS_VALUE_NTOH(f
->pktRecvWdw
) - 1;
648 sp
->attnSendSeq
= UAL_VALUE_NTOH(op
->pktAttnRecvSeq
); /* on even boundry */
651 sp
->remCID
= UAS_VALUE_NTOH(f
->CID
); /* Save Source CID as RemCID */
652 UAS_UAS(sp
->of
.dstCID
, f
->CID
); /* Save CID in open ctl packet */
654 sp
->remoteAddress
= addr
; /* Save his address */
658 if (m
.t
->action
& A_DENY
) { /* We've been denied ! */
659 DoClose(sp
, errOpenDenied
, -1);
662 if (m
.t
->action
& A_OREQACKOPEN
) {
663 /* Special case for OREQACK */
664 /* on an open session */
665 RemoveTimerElem(&adspGlobal
.fastTimers
, &sp
->RetryTimer
);
666 sp
->sendSeq
= sp
->firstRtmtSeq
;
672 if (m
.t
->send
) { /* Need to send a response */
673 sp
->sendCtl
|= m
.t
->send
;
677 if (m
.t
->action
& A_COMPLETE
) { /* Need to complete open param blk */
678 RemoveTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
);
682 pb
->u
.openParams
.localCID
= sp
->locCID
;
683 pb
->u
.openParams
.remoteCID
= sp
->remCID
;
684 pb
->u
.openParams
.remoteAddress
=
685 *((at_inet_t
*)&sp
->remoteAddress
);
686 pb
->u
.openParams
.sendSeq
= sp
->sendSeq
;
687 pb
->u
.openParams
.sendWindow
= sp
->sendWdwSeq
- sp
->sendSeq
;
688 pb
->u
.openParams
.attnSendSeq
= sp
->attnSendSeq
;
690 completepb(sp
, pb
); /* complete(pb, 0); */
693 /* Start probe timer */
694 InsertTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
,
703 * When a packet is received by the protocol stack with DDP type equal
704 * to ADSP, then execution comes here
706 * DS is set to ATALK's DGROUP
708 * This routine, or one of its children MUST call glean packet
711 * Pointer to DDP header
715 * Note that the incoming message block (mp) is usually discarded, either
716 * by the "ignored" path, or via the "checksend" path. The only case
717 * where the message is NOT freed is via the RxData case in the
718 * non control packet switch. I zero mp after the RxData case succeeds
719 * so that mp will not be freed.
721 int adspPacket(gref
, mp
)
722 /* (bytePtr data, word len, AddrUnion a, byte dsoc) */
730 register DDPX_FRAME
*ddp
; /* DDP frame pointer */
731 register ADSP_FRAMEPtr f
; /* Frame */
734 sp
= 0; /* No stream */
735 bp
= (unsigned char *)gbuf_rptr(mp
);
736 ddp
= (DDPX_FRAME
*)bp
;
737 if (ddp
->ddpx_type
!= DDP_ADSP
)
739 f
= (ADSP_FRAMEPtr
)(bp
+ DDPL_FRAME_LEN
);
741 len
= UAS_VALUE_NTOH(ddp
->ddpx_length
) & 0x3ff; /* (ten bits of length) */
742 len
-= DDPL_FRAME_LEN
;
743 if (len
< (sizeof(ADSP_FRAME
) - 1)) /* Packet too small */
744 return -1; /* mark the failure */
746 a
.a
.net
= NET_VALUE(ddp
->ddpx_snet
);
747 a
.a
.node
= ddp
->ddpx_snode
;
748 a
.a
.socket
= ddp
->ddpx_source
;
750 dsoc
= ddp
->ddpx_dest
;
752 if (sp
= (CCBPtr
)FindSender(f
, a
))
755 if (f
->descriptor
& ADSP_ATTENTION_BIT
) { /* ATTN packet */
756 if (sp
&& RXAttention(sp
, mp
, f
, len
))
759 mp
= 0; /* attention data is being held */
760 } /* ATTENTION BIT */
762 else if (f
->descriptor
& ADSP_CONTROL_BIT
) { /* Control packet */
763 switch (f
->descriptor
& ADSP_CONTROL_MASK
) {
764 case ADSP_CTL_PROBE
: /* Probe or acknowledgement */
769 case ADSP_CTL_OREQ
: /* Open Connection Request */
770 case ADSP_CTL_OREQACK
: /* Open Request and acknowledgement */
771 case ADSP_CTL_OACK
: /* Open Request acknowledgment */
772 case ADSP_CTL_ODENY
: /* Open Request denial */
773 if (RXConnection(gref
, &sp
, f
, len
, a
, dsoc
))
777 case ADSP_CTL_CLOSE
: /* Close connection advice */
779 /* This pkt may also ack some data we sent */
787 case ADSP_CTL_FRESET
: /* Forward Reset */
788 /* May I rot in hell for the code below... */
789 if (sp
&& (CheckRecvSeq(sp
, f
), RXFReset(sp
, f
)))
793 case ADSP_CTL_FRESET_ACK
: /* Forward Reset Acknowledgement */
794 if (sp
&& (CheckRecvSeq(sp
, f
), RXFResetAck(sp
, f
)))
798 case ADSP_CTL_RETRANSMIT
: /* Retransmit advice */
800 /* This pkt may also ack some data we sent */
802 RemoveTimerElem(&adspGlobal
.fastTimers
, &sp
->RetryTimer
);
803 sp
->sendSeq
= sp
->firstRtmtSeq
;
814 } /* Control packet */
816 else { /* Data Packet */
817 if ((sp
== 0) || RXData(sp
, mp
, f
, len
))
820 mp
= 0; /* RXData used up the data, DONT free it! */
826 checksend
: /* incoming data was not ignored */
827 if (sp
&& sp
->callSend
) /* If we have a stream & we need to send */