]>
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_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
25 * v01.23 All incoming packets come here first 06/21/90 mbs
26 * Modified for MP, 1996 by Tuyen Nguyen
27 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
30 #include <sys/errno.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <machine/spl.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
48 #include <netat/sysglue.h>
49 #include <netat/appletalk.h>
50 #include <netat/at_pcb.h>
51 #include <netat/ddp.h>
52 #include <netat/at_var.h>
54 #include <netat/adsp.h>
55 #include <netat/adsp_internal.h>
57 extern at_ifaddr_t
*ifID_home
;
62 * We just got a packet for this session, glean its address &
70 static void GleanSession(sp
) /* (CCBPtr sp) */
73 if (sp
->openState
== O_STATE_OPEN
) {
74 /* This is true for both state = sOpen & sClosing */
75 RemoveTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
);
76 InsertTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
,
85 * The same code handles incoming Open Connection Request,
86 * Open Request + Ack, Open Connection Ack, Open Connection Denial
88 * We could be in four different states, LISTEN, OPENWAIT, ESTABLISHED,
94 * Ok, there are 16 combinations. 8 are do-nothings, 2 have to be
95 * special cased (Open Deny and Req+Ack on Open session)
97 * Build a table of actions:
99 * What to match on (local socket, whole address, DestCID, SrcCID)
100 * What to send (Ack or Req+Ack)
101 * Next State (both the ccb state and the open state)
108 u_char match
; /* Characteristics that have to match
109 * (Bit-Mapped, see below) */
110 char action
; /* What to do if CCB matches */
111 char send
; /* What to send in response
112 * (Bit mapped, same as sendCtl field of
114 char openState
; /* Next Open state */
115 char state
; /* Next ccb state. */
116 char pad
; /* Too bad we need this to make structure
120 #define M_LSOC 0x01 /* bit 0 - Match on local socket */
121 #define M_ADDR 0x02 /* bit 1 - Match on whole address */
122 #define M_DCID 0x04 /* bit 2 - Match on DestCID */
123 #define M_SCID 0x08 /* bit 3 - Match SrcCID */
124 #define M_DCIDZERO 0x10 /* bit 4 - Dest CID must be 0 */
125 #define M_SCIDZERO 0x20 /* bit 5 - Src CID must be 0 */
126 #define M_FILTER 0x40 /* bit 6 - Match address filter */
127 #define M_IGNORE 0x80 /* bit 7 - Ignore */
129 #define A_COMPLETE 0x01 /* Complete open parameter block */
130 #define A_SAVEPARMS 0x02 /* Save connection parameters */
131 #define A_OREQACKOPEN 0x04 /* special case for open Req+Ack on
133 #define A_GLEAN 0x08 /* We'll be talking back to this guy */
134 #define A_DENY 0x10 /* We've been denied! */
138 * So here's our table
141 static TBL tbl
[16] = {
144 * For Open Request ($81)
147 * Match on destination socket
148 * Match on address filter
151 * Save Open Connection parameters
153 * Change state to ESTABLISHED
155 { M_LSOC
+ M_DCIDZERO
+ M_FILTER
,
156 A_SAVEPARMS
+ A_GLEAN
,
166 * Match on Remote Address & destination socket
168 * Save Open Connection parameters
170 * Change state to ESTABLISHED
172 { M_LSOC
+ M_ADDR
+ M_DCIDZERO
,
173 A_SAVEPARMS
+ A_GLEAN
,
182 * Match on Remote Address & SrcCID
186 { M_ADDR
+ M_SCID
+ M_DCIDZERO
,
234 * Match on SrcCID & DestCID & Address & Local Socket
235 * Complete Listen or Connect PB
238 { M_ADDR
+ M_DCID
+ M_SCID
+ M_LSOC
,
239 A_COMPLETE
+ A_GLEAN
,
260 * For Open Request + Ack ($83)
275 * Match on DestCID & socket
276 * Do not test remote address -- our open req could have
277 * been passed to another address by a connection server
278 * Save Open Connection parameters
279 * Complete Connect parameter block
284 A_COMPLETE
+ A_SAVEPARMS
+ A_GLEAN
,
305 * Match on Remote Address & SrcCID & DestCID & Local Socket
306 * If we've never gotten any data
307 * Send Ack & Retransmit
309 { M_ADDR
+ M_DCID
+ M_SCID
+ M_LSOC
,
310 A_OREQACKOPEN
+ A_GLEAN
,
320 * For Open Deny ($84)
335 * Match on DestCID & Address
336 * Source CID must be 0
337 * Complete with error
339 { M_SCIDZERO
+ M_DCID
+ M_ADDR
,
357 }, /* %%% No we probably don't want to ignore in this case */
372 extern at_ifaddr_t
*ifID_table
[];
375 * Used to search down queue of sessions for a session waiting for an
384 byte idx
; /* Index into state tables */
385 TBLPtr t
; /* Ptr to entry in table above */
391 * Called by Rx connection to find which stream (if any) should get this open
392 * request/ack/req+ack/deny packet.
397 MatchStream(sp
, m
) /* (CCBPtr sp, MATCHPtr m) */
404 if (sp
->openState
< O_STATE_LISTEN
||
405 sp
->openState
> O_STATE_OPEN
)
409 m
->t
= &tbl
[sp
->openState
- O_STATE_LISTEN
+ m
->idx
];
411 match
= m
->t
->match
; /* Get match criteria */
413 if (match
& M_IGNORE
) /* Ignore this combination */
416 if (match
& M_LSOC
) { /* Match on Local socket */
417 if (sp
->localSocket
!= m
->socket
)
421 if (match
& M_ADDR
) { /* Match on Address */
423 addr
= m
->addr
; /* Make local copy for efficiency */
424 if (sp
->remoteAddress
.a
.node
!= addr
.a
.node
)
426 if (sp
->remoteAddress
.a
.socket
!= addr
.a
.socket
)
428 if (sp
->remoteAddress
.a
.net
&& addr
.a
.net
&&
429 (sp
->remoteAddress
.a
.net
!= addr
.a
.net
))
433 * Handle special case to reject self-sent open request
435 if ((m
->srcCID
== sp
->locCID
) &&
436 (addr
.a
.node
== ifID_home
->ifThisNode
.s_node
) &&
437 /* *** was (addr.a.node == ddpcfg.node_addr.node) && *** */
438 ((addr
.a
.net
== 0) ||
439 (ifID_home
->ifThisNode
.s_net
== 0) ||
440 (ifID_home
->ifThisNode
.s_net
== addr
.a
.net
)) )
442 (NET_VALUE(ddpcfg.node_addr.net) == 0) ||
443 (NET_VALUE(ddpcfg.node_addr.net) == NET_VALUE(addr.a.net))) )
445 /* CID's match, and */
446 /* If nodeID matches, and */
447 /* network matches, */
448 return 0; /* then came from us! */
451 if (match
& M_DCID
) { /* Match on DestCID */
452 if (sp
->locCID
!= m
->dstCID
)
456 if (match
& M_SCID
) { /* Match on SourceCID */
457 if (sp
->remCID
!= m
->srcCID
)
461 if (match
& M_DCIDZERO
) { /* Destination CID must be 0 */
466 if (match
& M_SCIDZERO
) /* Source CID must be 0 */
472 if (match
& M_FILTER
) { /* Check address filter? */
473 if ((opb
= sp
->opb
)) /* There should be a param block... */
476 addr
= m
->addr
; /* Make local copy for efficiency */
477 if ((opb
->u
.openParams
.filterAddress
.net
&&
479 opb
->u
.openParams
.filterAddress
.net
!= addr
.a
.net
) ||
480 (opb
->u
.openParams
.filterAddress
.node
!= 0 &&
481 opb
->u
.openParams
.filterAddress
.node
!= addr
.a
.node
)||
482 (opb
->u
.openParams
.filterAddress
.socket
!= 0 &&
483 opb
->u
.openParams
.filterAddress
.socket
!= addr
.a
.socket
))
494 * Called by rx connection to see which connection listener (if any) should
495 * get this incoming open connection request.
499 static boolean
MatchListener(sp
, m
) /* (CCBPtr sp, MATCHPtr m) */
504 if ((sp
->state
== (word
)sListening
) && /* This CCB is a listener */
505 (sp
->localSocket
== m
->socket
)) /* on the right socket */
514 * We just received one of the 4 Open Connection packets
515 * Interrupts are masked OFF at this point
518 * spPtr Place to put ptr to stream (if we found one -- not
520 * f Pointer to ADSP header for packet, data follows behind it
521 * len # of byte in ADSP header + data
522 * addr Who sent the packet
523 * dsoc Where they sent it to
526 * Returns 1 if packet was ignored
528 static int RXConnection(gref
, spPtr
, f
, len
, addr
, dsoc
)
529 /* (CCBPtr *spPtr, ADSP_FRAMEPtr f, word len, AddrUnion addr, byte dsoc) */
530 gref_t
*gref
; /* READ queue */
538 ADSP_OPEN_DATAPtr op
;
543 ADSP_OPEN_DATAPtr adspop
;
546 op
= (ADSP_OPEN_DATAPtr
)&f
->data
[0]; /* Point to Open-Connection parms */
547 len
-= ADSP_FRAME_LEN
;
549 if (len
< (sizeof(ADSP_OPEN_DATA
))) /* Packet too small */
553 if (UAS_VALUE(op
->version
) != netw(0x0100)) { /* Check version num (on even-byte) */
555 * The open request has been denied. Try to send him a denial.
558 mp
= gbuf_alloc(AT_WR_OFFSET
+ DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+ ADSP_OPEN_FRAME_LEN
,
560 gbuf_rinc(mp
,AT_WR_OFFSET
);
561 gbuf_wset(mp
,DDPL_FRAME_LEN
);
562 adspp
= (ADSP_FRAMEPtr
)gbuf_wptr(mp
);
563 gbuf_winc(mp
,ADSP_FRAME_LEN
);
564 bzero((caddr_t
) gbuf_rptr(mp
),DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+
565 ADSP_OPEN_FRAME_LEN
);
566 adspp
->descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_ODENY
;
567 adspop
= (ADSP_OPEN_DATAPtr
)gbuf_wptr(mp
);
568 gbuf_winc(mp
,ADSP_OPEN_FRAME_LEN
);
569 UAS_UAS(adspop
->dstCID
, f
->CID
);
570 UAS_ASSIGN(adspop
->version
, 0x100);
571 adsp_sendddp(0, mp
, DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+
572 ADSP_OPEN_FRAME_LEN
, &addr
, DDP_ADSP
);
578 m
.descriptor
= f
->descriptor
;
579 m
.srcCID
= UAS_VALUE(f
->CID
);
580 m
.dstCID
= UAS_VALUE(op
->dstCID
); /* On even-byte boundry */
581 m
.idx
= ((f
->descriptor
& ADSP_CONTROL_MASK
) - 1) * 4;
584 * See if we can find a stream that knows what to do with this packet
586 if ((sp
= (CCBPtr
)qfind_m(AT_ADSP_STREAMS
, &m
, (ProcPtr
)MatchStream
)) == 0)
591 * No match, so look for connection listeners if this is an
594 if ((f
->descriptor
& ADSP_CONTROL_MASK
) != (byte
)ADSP_CTL_OREQ
)
597 if ((sp
= (CCBPtr
)qfind_m(AT_ADSP_STREAMS
, &m
,
598 (ProcPtr
)MatchListener
)) == 0)
601 ATDISABLE(s
, sp
->lock
);
602 p
= (struct adspcmd
*)&sp
->opb
;
603 while (n
= (struct adspcmd
*)p
->qLink
) /* Hunt down list of listens */
605 /* Check address filter */
606 if (((n
->u
.openParams
.filterAddress
.net
== 0) ||
608 (n
->u
.openParams
.filterAddress
.net
== addr
.a
.net
)) &&
610 ((n
->u
.openParams
.filterAddress
.node
== 0) ||
611 (n
->u
.openParams
.filterAddress
.node
== addr
.a
.node
)) &&
613 ((n
->u
.openParams
.filterAddress
.socket
== 0) ||
614 (n
->u
.openParams
.filterAddress
.socket
== addr
.a
.socket
))) {
615 p
->qLink
= n
->qLink
; /* Unlink this param block */
616 n
->u
.openParams
.remoteCID
= m
.srcCID
;
617 *((AddrUnionPtr
)&n
->u
.openParams
.remoteAddress
) = addr
;
618 n
->u
.openParams
.sendSeq
= netdw(UAL_VALUE(f
->pktNextRecvSeq
));
619 n
->u
.openParams
.sendWindow
= netw(UAS_VALUE(f
->pktRecvWdw
));
620 n
->u
.openParams
.attnSendSeq
= netdw(UAL_VALUE(op
->pktAttnRecvSeq
));
622 ATENABLE(s
, sp
->lock
);
623 completepb(sp
, n
); /* complete copy of request */
624 /* complete(n, 0); */
626 } /* found CLListen */
628 p
= n
; /* down the list we go... */
632 ATENABLE(s
, sp
->lock
);
636 *spPtr
= sp
; /* Save ptr to stream we just found */
638 ATDISABLE(s
, sp
->lock
);
639 sp
->openState
= m
.t
->openState
; /* Move to next state (may be same) */
640 sp
->state
= m
.t
->state
; /* Move to next state (may be same) */
642 if (m
.t
->action
& A_SAVEPARMS
) { /* Need to Save open-conn parms */
643 sp
->firstRtmtSeq
= sp
->sendSeq
= netdw(UAL_VALUE(f
->pktNextRecvSeq
));
644 sp
->sendWdwSeq
= netdw(UAL_VALUE(f
->pktNextRecvSeq
)) + netw(UAS_VALUE(f
->pktRecvWdw
)) - 1;
645 sp
->attnSendSeq
= netdw(UAL_VALUE(op
->pktAttnRecvSeq
)); /* on even boundry */
648 sp
->remCID
= UAS_VALUE(f
->CID
); /* Save Source CID as RemCID */
649 UAS_UAS(sp
->of
.dstCID
, f
->CID
); /* Save CID in open ctl packet */
651 sp
->remoteAddress
= addr
; /* Save his address */
654 ATENABLE(s
, sp
->lock
);
656 if (m
.t
->action
& A_DENY
) { /* We've been denied ! */
657 DoClose(sp
, errOpenDenied
, -1);
660 if (m
.t
->action
& A_OREQACKOPEN
) {
661 /* Special case for OREQACK */
662 /* on an open session */
663 RemoveTimerElem(&adspGlobal
.fastTimers
, &sp
->RetryTimer
);
664 sp
->sendSeq
= sp
->firstRtmtSeq
;
670 if (m
.t
->send
) { /* Need to send a response */
671 sp
->sendCtl
|= m
.t
->send
;
675 if (m
.t
->action
& A_COMPLETE
) { /* Need to complete open param blk */
676 RemoveTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
);
680 pb
->u
.openParams
.localCID
= sp
->locCID
;
681 pb
->u
.openParams
.remoteCID
= sp
->remCID
;
682 pb
->u
.openParams
.remoteAddress
=
683 *((at_inet_t
*)&sp
->remoteAddress
);
684 pb
->u
.openParams
.sendSeq
= sp
->sendSeq
;
685 pb
->u
.openParams
.sendWindow
= sp
->sendWdwSeq
- sp
->sendSeq
;
686 pb
->u
.openParams
.attnSendSeq
= sp
->attnSendSeq
;
688 completepb(sp
, pb
); /* complete(pb, 0); */
691 /* Start probe timer */
692 InsertTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
,
701 * When a packet is received by the protocol stack with DDP type equal
702 * to ADSP, then execution comes here
704 * DS is set to ATALK's DGROUP
706 * This routine, or one of its children MUST call glean packet
709 * Pointer to DDP header
713 * Note that the incoming message block (mp) is usually discarded, either
714 * by the "ignored" path, or via the "checksend" path. The only case
715 * where the message is NOT freed is via the RxData case in the
716 * non control packet switch. I zero mp after the RxData case succeeds
717 * so that mp will not be freed.
719 int adspPacket(gref
, mp
)
720 /* (bytePtr data, word len, AddrUnion a, byte dsoc) */
729 register DDPX_FRAME
*ddp
; /* DDP frame pointer */
730 register ADSP_FRAMEPtr f
; /* Frame */
733 sp
= 0; /* No stream */
734 bp
= (unsigned char *)gbuf_rptr(mp
);
735 ddp
= (DDPX_FRAME
*)bp
;
736 if (ddp
->ddpx_type
!= DDP_ADSP
)
738 f
= (ADSP_FRAMEPtr
)(bp
+ DDPL_FRAME_LEN
);
740 len
= UAS_VALUE(ddp
->ddpx_length
) & 0x3ff; /* (ten bits of length) */
741 len
-= DDPL_FRAME_LEN
;
742 if (len
< (sizeof(ADSP_FRAME
) - 1)) /* Packet too small */
743 return -1; /* mark the failure */
745 a
.a
.net
= NET_VALUE(ddp
->ddpx_snet
);
746 a
.a
.node
= ddp
->ddpx_snode
;
747 a
.a
.socket
= ddp
->ddpx_source
;
749 dsoc
= ddp
->ddpx_dest
;
751 if (sp
= (CCBPtr
)FindSender(f
, a
))
754 if (f
->descriptor
& ADSP_ATTENTION_BIT
) { /* ATTN packet */
755 if (sp
&& RXAttention(sp
, mp
, f
, len
))
758 mp
= 0; /* attention data is being held */
759 } /* ATTENTION BIT */
761 else if (f
->descriptor
& ADSP_CONTROL_BIT
) { /* Control packet */
762 switch (f
->descriptor
& ADSP_CONTROL_MASK
) {
763 case ADSP_CTL_PROBE
: /* Probe or acknowledgement */
768 case ADSP_CTL_OREQ
: /* Open Connection Request */
769 case ADSP_CTL_OREQACK
: /* Open Request and acknowledgement */
770 case ADSP_CTL_OACK
: /* Open Request acknowledgment */
771 case ADSP_CTL_ODENY
: /* Open Request denial */
772 if (RXConnection(gref
, &sp
, f
, len
, a
, dsoc
))
776 case ADSP_CTL_CLOSE
: /* Close connection advice */
778 /* This pkt may also ack some data we sent */
786 case ADSP_CTL_FRESET
: /* Forward Reset */
787 /* May I rot in hell for the code below... */
788 if (sp
&& (CheckRecvSeq(sp
, f
), RXFReset(sp
, f
)))
792 case ADSP_CTL_FRESET_ACK
: /* Forward Reset Acknowledgement */
793 if (sp
&& (CheckRecvSeq(sp
, f
), RXFResetAck(sp
, f
)))
797 case ADSP_CTL_RETRANSMIT
: /* Retransmit advice */
799 /* This pkt may also ack some data we sent */
801 RemoveTimerElem(&adspGlobal
.fastTimers
, &sp
->RetryTimer
);
802 ATDISABLE(s
, sp
->lock
);
803 sp
->sendSeq
= sp
->firstRtmtSeq
;
807 ATENABLE(s
, sp
->lock
);
815 } /* Control packet */
817 else { /* Data Packet */
818 if ((sp
== 0) || RXData(sp
, mp
, f
, len
))
821 mp
= 0; /* RXData used up the data, DONT free it! */
827 checksend
: /* incoming data was not ignored */
828 if (sp
&& sp
->callSend
) /* If we have a stream & we need to send */