]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netiso/tuba_usrreq.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@
23 * Copyright (c) 1992, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)tuba_usrreq.c 8.1 (Berkeley) 6/10/93
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/malloc.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/protosw.h>
64 #include <sys/errno.h>
68 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/tcp.h>
76 #include <netinet/tcp_fsm.h>
77 #include <netinet/tcp_seq.h>
78 #include <netinet/tcp_timer.h>
79 #include <netinet/tcp_var.h>
80 #include <netinet/tcpip.h>
81 #include <netinet/tcp_debug.h>
83 #include <netiso/argo_debug.h>
84 #include <netiso/iso.h>
85 #include <netiso/clnp.h>
86 #include <netiso/iso_pcb.h>
87 #include <netiso/iso_var.h>
88 #include <netiso/tuba_table.h>
90 * TCP protocol interface to socket abstraction.
92 extern char *tcpstates
[];
93 extern struct inpcb tuba_inpcb
;
94 extern struct isopcb tuba_isopcb
;
97 * Process a TCP user request for TCP tb. If this is a send request
98 * then m is the mbuf chain of send data. If this is a timer expiration
99 * (called from the software clock routine), then timertype tells which timer.
102 tuba_usrreq(so
, req
, m
, nam
, control
)
105 struct mbuf
*m
, *nam
, *control
;
107 register struct inpcb
*inp
;
108 register struct isopcb
*isop
;
109 register struct tcpcb
*tp
;
113 struct sockaddr_iso
*siso
;
115 if (req
== PRU_CONTROL
)
116 return (iso_control(so
, (int)m
, (caddr_t
)nam
,
117 (struct ifnet
*)control
));
122 * When a TCP is attached to a socket, then there will be
123 * a (struct inpcb) pointed at by the socket, and this
124 * structure will point at a subsidary (struct tcpcb).
126 if (inp
== 0 && req
!= PRU_ATTACH
) {
128 return (EINVAL
); /* XXX */
133 panic("tuba_usrreq");
134 ostate
= tp
->t_state
;
135 isop
= (struct isopcb
*)tp
->t_tuba_pcb
;
137 panic("tuba_usrreq 2");
143 * TCP attaches to socket via PRU_ATTACH, reserving space,
144 * and an internet control block. We also need to
145 * allocate an isopcb and separate the control block from
149 if (error
= iso_pcballoc(so
, &tuba_isopcb
))
151 isop
= (struct isopcb
*)so
->so_pcb
;
153 if (error
= tcp_usrreq(so
, req
, m
, nam
, control
)) {
154 isop
->isop_socket
= 0;
159 insque(inp
, &tuba_inpcb
);
160 inp
->inp_head
= &tuba_inpcb
;
163 panic("tuba_usrreq 3");
164 tp
->t_tuba_pcb
= (caddr_t
) isop
;
169 * PRU_DETACH detaches the TCP protocol from the socket.
170 * If the protocol state is non-embryonic, then can't
171 * do this directly: have to initiate a PRU_DISCONNECT,
172 * which may finish later; embryonic TCB's can just
176 if (tp
->t_state
> TCPS_LISTEN
)
177 tp
= tcp_disconnect(tp
);
181 tuba_pcbdetach(isop
);
185 * Give the socket an address.
188 siso
= mtod(nam
, struct sockaddr_iso
*);
189 if (siso
->siso_tlen
&& siso
->siso_tlen
!= 2) {
193 if ((error
= iso_pcbbind(isop
, nam
)) ||
194 (siso
= isop
->isop_laddr
) == 0)
196 bcopy(TSEL(siso
), &inp
->inp_lport
, 2);
197 if (siso
->siso_nlen
&&
198 !(inp
->inp_laddr
.s_addr
= tuba_lookup(siso
, M_WAITOK
)))
203 * Prepare to accept connections.
207 if (inp
->inp_lport
== 0 &&
208 (error
= iso_pcbbind(isop
, (struct mbuf
*)0)))
210 bcopy(TSEL(isop
->isop_laddr
), &inp
->inp_lport
, 2);
211 if (req
== PRU_LISTEN
) {
212 tp
->t_state
= TCPS_LISTEN
;
217 * Initiate connection to peer.
218 * Create a template for use in transmissions on this connection.
219 * Enter SYN_SENT state, and mark socket as connecting.
220 * Start keep-alive timer, and seed output sequence space.
221 * Send initial segment on connection.
223 /* case PRU_CONNECT: */
224 if (error
= iso_pcbconnect(isop
, nam
))
226 if ((siso
= isop
->isop_laddr
) && siso
->siso_nlen
> 1)
227 siso
->siso_data
[siso
->siso_nlen
- 1] = ISOPROTO_TCP
;
229 panic("tuba_usrreq: connect");
230 siso
= mtod(nam
, struct sockaddr_iso
*);
231 if (!(inp
->inp_faddr
.s_addr
= tuba_lookup(siso
, M_WAITOK
))) {
233 iso_pcbdisconnect(isop
);
237 bcopy(TSEL(isop
->isop_faddr
), &inp
->inp_fport
, 2);
238 if (inp
->inp_laddr
.s_addr
== 0 &&
239 (inp
->inp_laddr
.s_addr
=
240 tuba_lookup(isop
->isop_laddr
, M_WAITOK
)) == 0)
242 if ((tp
->t_template
= tcp_template(tp
)) == 0)
245 tcpstat
.tcps_connattempt
++;
246 tp
->t_state
= TCPS_SYN_SENT
;
247 tp
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
;
248 tp
->iss
= tcp_iss
; tcp_iss
+= TCP_ISSINCR
/2;
250 error
= tcp_output(tp
);
251 tuba_refcnt(isop
, 1);
255 * Initiate disconnect from peer.
256 * If connection never passed embryonic stage, just drop;
257 * else if don't need to let data drain, then can just drop anyways,
258 * else have to begin TCP shutdown process: mark socket disconnecting,
259 * drain unread data, state switch to reflect user close, and
260 * send segment (e.g. FIN) to peer. Socket will be really disconnected
261 * when peer sends FIN and acks ours.
263 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
266 if ((tp
= tcp_disconnect(tp
)) == 0)
267 tuba_pcbdetach(isop
);
271 * Accept a connection. Essentially all the work is
272 * done at higher levels; just return the address
273 * of the peer, storing through addr.
276 bcopy((caddr_t
)isop
->isop_faddr
, mtod(nam
, caddr_t
),
277 nam
->m_len
= isop
->isop_faddr
->siso_len
);
281 * Mark the connection as being incapable of further output.
285 tp
= tcp_usrclosed(tp
);
287 error
= tcp_output(tp
);
289 tuba_pcbdetach(isop
);
295 if ((tp
= tcp_drop(tp
, ECONNABORTED
)) == 0)
296 tuba_pcbdetach(isop
);
301 if (isop
->isop_laddr
)
302 bcopy((caddr_t
)isop
->isop_laddr
, mtod(nam
, caddr_t
),
303 nam
->m_len
= isop
->isop_laddr
->siso_len
);
307 if (isop
->isop_faddr
)
308 bcopy((caddr_t
)isop
->isop_faddr
, mtod(nam
, caddr_t
),
309 nam
->m_len
= isop
->isop_faddr
->siso_len
);
313 error
= tcp_usrreq(so
, req
, m
, nam
, control
);
316 if (tp
&& (so
->so_options
& SO_DEBUG
))
317 tcp_trace(TA_USER
, ostate
, tp
, (struct tcpiphdr
*)0, req
);
323 tuba_ctloutput(op
, so
, level
, optname
, mp
)
329 int clnp_ctloutput(), tcp_ctloutput();
331 return ((level
!= IPPROTO_TCP
? clnp_ctloutput
: tcp_ctloutput
)
332 (op
, so
, level
, optname
, mp
));