]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netccitt/hd_subr.c
b65f6bf3798bd982408e7098340282d1a232c8bc
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@
23 * Copyright (c) University of British Columbia, 1984
24 * Copyright (c) 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * the Laboratory for Computation Vision and the Computer Science Department
29 * of the University of British Columbia.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)hd_subr.c 8.1 (Berkeley) 6/10/93
62 #include <sys/param.h>
63 #include <sys/systm.h>
65 #include <sys/domain.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/protosw.h>
69 #include <sys/errno.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
76 #include <netccitt/hdlc.h>
77 #include <netccitt/hd_var.h>
78 #include <netccitt/x25.h>
79 #include <netccitt/pk_var.h>
84 hdintrq
.ifq_maxlen
= IFQ_MAXLEN
;
87 hd_ctlinput (prc
, addr
)
89 struct sockaddr
*addr
;
91 register struct x25config
*xcp
= (struct x25config
*)addr
;
92 register struct hdcb
*hdp
;
93 register struct ifaddr
*ifa
;
97 if (addr
->sa_family
!= AF_CCITT
)
98 return (EAFNOSUPPORT
);
99 if (xcp
->xc_lptype
!= HDLCPROTO_LAPB
)
100 return (EPROTONOSUPPORT
);
101 ifa
= ifa_ifwithaddr(addr
);
102 if (ifa
== 0 || ifa
->ifa_addr
->sa_family
!= AF_CCITT
||
103 (ifp
= ifa
->ifa_ifp
) == 0)
104 panic ("hd_ctlinput");
105 for (hdp
= hdcbhead
; hdp
; hdp
= hdp
->hd_next
)
106 if (hdp
->hd_ifp
== ifp
)
109 if (hdp
== 0) { /* new interface */
111 int hd_ifoutput(), hd_output();
113 /* an hdcb is now too big to fit in an mbuf */
114 MALLOC(hdp
, struct hdcb
*, sizeof (*hdp
), M_PCB
, M_NOWAIT
);
117 bzero((caddr_t
)hdp
, sizeof(*hdp
));
119 (caddr_t
) pk_newlink ((struct x25_ifaddr
*) ifa
,
121 ((struct x25_ifaddr
*)ifa
)->ia_pkcb
=
122 (struct pkcb
*) hdp
->hd_pkp
;
123 if (hdp
-> hd_pkp
== 0) {
130 hdp
->hd_state
= INIT
;
131 hdp
->hd_output
= hd_ifoutput
;
132 hdp
->hd_next
= hdcbhead
;
134 } else if (hdp
->hd_pkp
== 0) { /* interface got reconfigured */
136 (caddr_t
) pk_newlink ((struct x25_ifaddr
*) ifa
,
138 ((struct x25_ifaddr
*)ifa
)->ia_pkcb
=
139 (struct pkcb
*) hdp
->hd_pkp
;
140 if (hdp
-> hd_pkp
== 0) {
148 if (xcp
->xc_lwsize
== 0 ||
149 xcp
->xc_lwsize
> MAX_WINDOW_SIZE
)
150 xcp
->xc_lwsize
= MAX_WINDOW_SIZE
;
151 if (hdp
->hd_state
== INIT
)
156 if (hdp
->hd_state
== ABM
)
157 hd_message (hdp
, "Operator shutdown: link closed");
158 (void) pk_ctlinput (PRC_LINKDOWN
, hdp
->hd_pkp
);
160 /* fall thru to ... */
162 case PRC_DISCONNECT_REQUEST
:
163 /* drop reference to pkcb --- it's dead meat */
164 hdp
->hd_pkp
= (caddr_t
) 0;
165 ((struct x25_ifaddr
*)ifa
)->ia_pkcb
= (struct pkcb
*) 0;
167 hd_writeinternal (hdp
, DISC
, POLLON
);
168 hdp
->hd_state
= DISC_SENT
;
175 register struct hdcb
*hdp
;
177 register struct mbuf
*m
;
180 /* Clear Transmit queue. */
181 while ((m
= hd_remove (&hdp
->hd_txq
)) != NULL
)
184 /* Clear Retransmit queue. */
185 i
= hdp
->hd_lastrxnr
;
186 while (i
!= hdp
->hd_retxqi
) {
187 m_freem (hdp
->hd_retxq
[i
]);
188 i
= (i
+ 1) % MODULUS
;
192 hdp
->hd_vs
= hdp
->hd_vr
= 0;
193 hdp
->hd_lasttxnr
= hdp
->hd_lastrxnr
= 0;
197 hdp
->hd_condition
= 0;
200 hd_decode (hdp
, frame
)
201 register struct hdcb
*hdp
;
202 struct Hdlc_frame
*frame
;
204 register int frametype
= ILLEGAL
;
205 register struct Hdlc_iframe
*iframe
= (struct Hdlc_iframe
*) frame
;
206 register struct Hdlc_sframe
*sframe
= (struct Hdlc_sframe
*) frame
;
207 register struct Hdlc_uframe
*uframe
= (struct Hdlc_uframe
*) frame
;
209 if (iframe
-> hdlc_0
== 0) {
211 hdp
->hd_iframes_in
++;
214 else if (sframe
-> hdlc_01
== 1) {
215 /* Supervisory format. */
216 switch (sframe
-> s2
) {
232 else if (uframe
-> hdlc_11
== 3) {
233 /* Unnumbered format. */
234 switch (uframe
-> m3
) {
260 * This routine is called when the HDLC layer internally generates a
261 * command or response for the remote machine ( eg. RR, UA etc. ).
262 * Only supervisory or unnumbered frames are processed.
265 hd_writeinternal (hdp
, frametype
, pf
)
266 register struct hdcb
*hdp
;
267 register int frametype
, pf
;
269 register struct mbuf
*buf
;
270 struct Hdlc_frame
*frame
;
271 register struct Hdlc_sframe
*sframe
;
272 register struct Hdlc_uframe
*uframe
;
274 MGETHDR (buf
, M_DONTWAIT
, MT_HEADER
);
277 frame
= mtod (buf
, struct Hdlc_frame
*);
278 sframe
= mtod (buf
, struct Hdlc_sframe
*);
279 uframe
= mtod (buf
, struct Hdlc_uframe
*);
281 /* Assume a response - address structure for DTE */
282 frame
-> address
= ADDRESS_A
;
284 buf
-> m_act
= buf
-> m_next
= NULL
;
288 frame
-> control
= RR_CONTROL
;
293 frame
-> control
= RNR_CONTROL
;
298 frame
-> control
= REJ_CONTROL
;
303 frame
-> control
= SABM_CONTROL
;
304 frame
-> address
= ADDRESS_B
;
308 if ((hdp
->hd_ifp
->if_flags
& IFF_UP
) == 0) {
309 hdp
->hd_state
= DISCONNECTED
;
310 (void) m_freem (buf
);
311 hd_flush (hdp
->hd_ifp
);
314 frame
-> control
= DISC_CONTROL
;
315 frame
-> address
= ADDRESS_B
;
319 frame
-> control
= DM_CONTROL
;
323 frame
-> control
= UA_CONTROL
;
327 frame
-> control
= FRMR_CONTROL
;
328 bcopy ((caddr_t
)&hd_frmr
, (caddr_t
)frame
-> info
, 3);
334 if (sframe
-> hdlc_01
== 1) {
335 /* Supervisory format - RR, REJ, or RNR. */
336 sframe
-> nr
= hdp
->hd_vr
;
338 hdp
->hd_lasttxnr
= hdp
->hd_vr
;
344 hd_trace (hdp
, TX
, frame
);
345 buf
-> m_pkthdr
.len
= buf
-> m_len
;
346 (*hdp
->hd_output
) (hdp
, buf
);
353 register struct mbuf
*m
;
357 if ((q
-> head
= m
-> m_act
) == NULL
)
365 register struct hdtxq
*q
;
366 register struct mbuf
*m
;
370 if (q
-> tail
== NULL
)
373 q
-> tail
-> m_act
= m
;
380 register struct mbuf
*m
;
385 IF_DEQUEUE (&ifp
->if_snd
, m
);
393 hd_message (hdp
, msg
)
399 if (hdcbhead
-> hd_next
)
400 printf ("HDLC(%s): %s\n", format_ntn (hdp
->hd_xcp
), msg
);
402 printf ("HDLC: %s\n", msg
);
409 printf ("HDLC STATUS:\n V(S)=%d, V(R)=%d, retxqi=%d,\n",
410 hdp
->hd_vs
, hdp
->hd_vr
, hdp
->hd_retxqi
);
412 printf ("Last_rx_nr=%d, Last_tx_nr=%d,\n Condition=%d, Xx=%d\n",
413 hdp
->hd_lastrxnr
, hdp
->hd_lasttxnr
, hdp
->hd_condition
, hdp
->hd_xx
);