1997-10-10 05:54:32 +04:00
|
|
|
/* $NetBSD: tuba_usrreq.c,v 1.12 1997/10/10 01:54:32 explorer Exp $ */
|
1994-06-29 10:39:25 +04:00
|
|
|
|
1994-05-13 10:08:03 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-06-29 10:39:25 +04:00
|
|
|
* @(#)tuba_usrreq.c 8.1 (Berkeley) 6/10/93
|
1994-05-13 10:08:03 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/tcp_fsm.h>
|
|
|
|
#include <netinet/tcp_seq.h>
|
|
|
|
#include <netinet/tcp_timer.h>
|
|
|
|
#include <netinet/tcp_var.h>
|
|
|
|
#include <netinet/tcpip.h>
|
|
|
|
#include <netinet/tcp_debug.h>
|
|
|
|
|
1996-09-08 18:28:08 +04:00
|
|
|
#include <netiso/tp_param.h>
|
1994-05-13 10:08:03 +04:00
|
|
|
#include <netiso/argo_debug.h>
|
|
|
|
#include <netiso/iso.h>
|
|
|
|
#include <netiso/clnp.h>
|
|
|
|
#include <netiso/iso_pcb.h>
|
|
|
|
#include <netiso/iso_var.h>
|
|
|
|
#include <netiso/tuba_table.h>
|
1996-10-11 03:12:43 +04:00
|
|
|
#include <netiso/tp_var.h>
|
1997-10-10 05:54:32 +04:00
|
|
|
|
1994-05-13 10:08:03 +04:00
|
|
|
/*
|
|
|
|
* TCP protocol interface to socket abstraction.
|
|
|
|
*/
|
1996-02-14 01:07:57 +03:00
|
|
|
extern char *tcpstates[];
|
|
|
|
extern struct inpcbtable tuba_inpcb;
|
|
|
|
extern struct isopcb tuba_isopcb;
|
1994-05-13 10:08:03 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a TCP user request for TCP tb. If this is a send request
|
|
|
|
* then m is the mbuf chain of send data. If this is a timer expiration
|
|
|
|
* (called from the software clock routine), then timertype tells which timer.
|
|
|
|
*/
|
1996-02-14 01:07:57 +03:00
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
1996-05-22 17:54:55 +04:00
|
|
|
tuba_usrreq(so, req, m, nam, control, p)
|
|
|
|
struct socket *so;
|
|
|
|
int req;
|
|
|
|
struct mbuf *m, *nam, *control;
|
|
|
|
struct proc *p;
|
1994-05-13 10:08:03 +04:00
|
|
|
{
|
|
|
|
register struct inpcb *inp;
|
1996-02-14 01:07:57 +03:00
|
|
|
register struct isopcb *isop = NULL;
|
|
|
|
register struct tcpcb *tp = NULL;
|
|
|
|
int s;
|
|
|
|
int error = 0;
|
|
|
|
int ostate;
|
1994-05-13 10:08:03 +04:00
|
|
|
struct sockaddr_iso *siso;
|
|
|
|
|
|
|
|
if (req == PRU_CONTROL)
|
1996-05-22 17:54:55 +04:00
|
|
|
return (iso_control(so, (long)m, (caddr_t)nam,
|
|
|
|
(struct ifnet *)control, p));
|
1994-05-13 10:08:03 +04:00
|
|
|
|
1995-08-13 03:59:09 +04:00
|
|
|
s = splsoftnet();
|
1994-05-13 10:08:03 +04:00
|
|
|
inp = sotoinpcb(so);
|
|
|
|
/*
|
|
|
|
* When a TCP is attached to a socket, then there will be
|
|
|
|
* a (struct inpcb) pointed at by the socket, and this
|
|
|
|
* structure will point at a subsidary (struct tcpcb).
|
|
|
|
*/
|
1996-02-14 01:07:57 +03:00
|
|
|
if (inp == 0 && req != PRU_ATTACH) {
|
1996-09-08 18:28:08 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
1994-05-13 10:08:03 +04:00
|
|
|
}
|
|
|
|
if (inp) {
|
|
|
|
tp = intotcpcb(inp);
|
|
|
|
if (tp == 0)
|
|
|
|
panic("tuba_usrreq");
|
|
|
|
ostate = tp->t_state;
|
1996-09-08 18:28:08 +04:00
|
|
|
isop = (struct isopcb *)tp->t_tuba_pcb;
|
1994-05-13 10:08:03 +04:00
|
|
|
if (isop == 0)
|
|
|
|
panic("tuba_usrreq 2");
|
|
|
|
} else
|
|
|
|
ostate = 0;
|
|
|
|
switch (req) {
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* TCP attaches to socket via PRU_ATTACH, reserving space,
|
|
|
|
* and an internet control block. We also need to
|
|
|
|
* allocate an isopcb and separate the control block from
|
|
|
|
* tcp/ip ones.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_ATTACH:
|
1996-02-14 01:07:57 +03:00
|
|
|
if ((error = iso_pcballoc(so, &tuba_isopcb)) != 0)
|
1994-05-13 10:08:03 +04:00
|
|
|
break;
|
1996-02-14 01:07:57 +03:00
|
|
|
isop = (struct isopcb *) so->so_pcb;
|
1994-05-13 10:08:03 +04:00
|
|
|
so->so_pcb = 0;
|
1996-05-22 17:54:55 +04:00
|
|
|
if ((error = tcp_usrreq(so, req, m, nam, control, p)) != 0) {
|
1994-05-13 10:08:03 +04:00
|
|
|
isop->isop_socket = 0;
|
|
|
|
iso_pcbdetach(isop);
|
|
|
|
} else {
|
|
|
|
inp = sotoinpcb(so);
|
1995-07-12 13:17:06 +04:00
|
|
|
CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp,
|
1996-02-14 01:07:57 +03:00
|
|
|
inp_queue);
|
1995-07-12 13:17:06 +04:00
|
|
|
CIRCLEQ_INSERT_HEAD(&tuba_inpcb.inpt_queue, inp,
|
1996-02-14 01:07:57 +03:00
|
|
|
inp_queue);
|
1995-06-13 11:58:10 +04:00
|
|
|
inp->inp_table = &tuba_inpcb;
|
1994-05-13 10:08:03 +04:00
|
|
|
tp = intotcpcb(inp);
|
|
|
|
if (tp == 0)
|
|
|
|
panic("tuba_usrreq 3");
|
|
|
|
tp->t_tuba_pcb = (caddr_t) isop;
|
|
|
|
}
|
|
|
|
goto notrace;
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* PRU_DETACH detaches the TCP protocol from the socket.
|
|
|
|
* If the protocol state is non-embryonic, then can't
|
|
|
|
* do this directly: have to initiate a PRU_DISCONNECT,
|
|
|
|
* which may finish later; embryonic TCB's can just
|
|
|
|
* be discarded here.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_DETACH:
|
|
|
|
if (tp->t_state > TCPS_LISTEN)
|
|
|
|
tp = tcp_disconnect(tp);
|
|
|
|
else
|
|
|
|
tp = tcp_close(tp);
|
|
|
|
if (tp == 0)
|
|
|
|
tuba_pcbdetach(isop);
|
|
|
|
break;
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Give the socket an address.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_BIND:
|
|
|
|
siso = mtod(nam, struct sockaddr_iso *);
|
|
|
|
if (siso->siso_tlen && siso->siso_tlen != 2) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
1996-05-22 17:54:55 +04:00
|
|
|
if ((error = iso_pcbbind(isop, nam, p)) ||
|
1994-05-13 10:08:03 +04:00
|
|
|
(siso = isop->isop_laddr) == 0)
|
|
|
|
break;
|
|
|
|
bcopy(TSEL(siso), &inp->inp_lport, 2);
|
|
|
|
if (siso->siso_nlen &&
|
|
|
|
!(inp->inp_laddr.s_addr = tuba_lookup(siso, M_WAITOK)))
|
|
|
|
error = ENOBUFS;
|
|
|
|
break;
|
|
|
|
|
1996-09-08 18:28:08 +04:00
|
|
|
case PRU_LISTEN:
|
|
|
|
if (inp->inp_lport == 0) {
|
|
|
|
error = iso_pcbbind(isop, (struct mbuf *)0,
|
|
|
|
(struct proc *)0);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bcopy(TSEL(isop->isop_laddr), &inp->inp_lport, 2);
|
|
|
|
tp->t_state = TCPS_LISTEN;
|
|
|
|
break;
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Prepare to accept connections.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_CONNECT:
|
1996-05-22 17:54:55 +04:00
|
|
|
if (inp->inp_lport == 0) {
|
|
|
|
error = iso_pcbbind(isop, (struct mbuf *)0,
|
|
|
|
(struct proc *)0);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
1994-05-13 10:08:03 +04:00
|
|
|
bcopy(TSEL(isop->isop_laddr), &inp->inp_lport, 2);
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Initiate connection to peer.
|
|
|
|
* Create a template for use in transmissions on this connection.
|
|
|
|
* Enter SYN_SENT state, and mark socket as connecting.
|
|
|
|
* Start keep-alive timer, and seed output sequence space.
|
|
|
|
* Send initial segment on connection.
|
|
|
|
*/
|
|
|
|
if ((error = iso_pcbconnect(isop, nam)) != 0)
|
1994-05-13 10:08:03 +04:00
|
|
|
break;
|
|
|
|
if ((siso = isop->isop_laddr) && siso->siso_nlen > 1)
|
|
|
|
siso->siso_data[siso->siso_nlen - 1] = ISOPROTO_TCP;
|
|
|
|
else
|
|
|
|
panic("tuba_usrreq: connect");
|
|
|
|
siso = mtod(nam, struct sockaddr_iso *);
|
|
|
|
if (!(inp->inp_faddr.s_addr = tuba_lookup(siso, M_WAITOK))) {
|
1996-02-14 01:07:57 +03:00
|
|
|
unconnect:
|
1994-05-13 10:08:03 +04:00
|
|
|
iso_pcbdisconnect(isop);
|
|
|
|
error = ENOBUFS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bcopy(TSEL(isop->isop_faddr), &inp->inp_fport, 2);
|
|
|
|
if (inp->inp_laddr.s_addr == 0 &&
|
1996-02-14 01:07:57 +03:00
|
|
|
(inp->inp_laddr.s_addr =
|
|
|
|
tuba_lookup(isop->isop_laddr, M_WAITOK)) == 0)
|
1994-05-13 10:08:03 +04:00
|
|
|
goto unconnect;
|
|
|
|
if ((tp->t_template = tcp_template(tp)) == 0)
|
|
|
|
goto unconnect;
|
|
|
|
soisconnecting(so);
|
|
|
|
tcpstat.tcps_connattempt++;
|
|
|
|
tp->t_state = TCPS_SYN_SENT;
|
|
|
|
tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
|
1997-10-10 05:54:32 +04:00
|
|
|
tp->iss = tcp_new_iss(tp, sizeof(tp), 0);
|
1994-05-13 10:08:03 +04:00
|
|
|
tcp_sendseqinit(tp);
|
|
|
|
error = tcp_output(tp);
|
|
|
|
tuba_refcnt(isop, 1);
|
|
|
|
break;
|
|
|
|
|
1996-09-08 18:28:08 +04:00
|
|
|
case PRU_CONNECT2:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Initiate disconnect from peer.
|
|
|
|
* If connection never passed embryonic stage, just drop;
|
|
|
|
* else if don't need to let data drain, then can just drop anyways,
|
|
|
|
* else have to begin TCP shutdown process: mark socket disconnecting,
|
|
|
|
* drain unread data, state switch to reflect user close, and
|
|
|
|
* send segment (e.g. FIN) to peer. Socket will be really disconnected
|
|
|
|
* when peer sends FIN and acks ours.
|
|
|
|
*
|
|
|
|
* SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_DISCONNECT:
|
|
|
|
if ((tp = tcp_disconnect(tp)) == 0)
|
|
|
|
tuba_pcbdetach(isop);
|
|
|
|
break;
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Accept a connection. Essentially all the work is
|
|
|
|
* done at higher levels; just return the address
|
|
|
|
* of the peer, storing through addr.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_ACCEPT:
|
1996-09-08 18:28:08 +04:00
|
|
|
iso_getnetaddr(isop, nam, TP_FOREIGN);
|
1994-05-13 10:08:03 +04:00
|
|
|
break;
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Mark the connection as being incapable of further output.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_SHUTDOWN:
|
|
|
|
socantsendmore(so);
|
|
|
|
tp = tcp_usrclosed(tp);
|
|
|
|
if (tp)
|
|
|
|
error = tcp_output(tp);
|
|
|
|
else
|
|
|
|
tuba_pcbdetach(isop);
|
|
|
|
break;
|
1996-09-08 18:28:08 +04:00
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
/*
|
|
|
|
* Abort the TCP.
|
|
|
|
*/
|
1994-05-13 10:08:03 +04:00
|
|
|
case PRU_ABORT:
|
|
|
|
if ((tp = tcp_drop(tp, ECONNABORTED)) == 0)
|
|
|
|
tuba_pcbdetach(isop);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case PRU_SOCKADDR:
|
1996-09-08 18:28:08 +04:00
|
|
|
iso_getnetaddr(isop, nam, TP_LOCAL);
|
1994-05-13 10:08:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PRU_PEERADDR:
|
1996-09-08 18:28:08 +04:00
|
|
|
iso_getnetaddr(isop, nam, TP_FOREIGN);
|
1994-05-13 10:08:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1996-05-22 17:54:55 +04:00
|
|
|
error = tcp_usrreq(so, req, m, nam, control, p);
|
1994-05-13 10:08:03 +04:00
|
|
|
goto notrace;
|
|
|
|
}
|
|
|
|
if (tp && (so->so_options & SO_DEBUG))
|
1996-09-08 18:28:08 +04:00
|
|
|
tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
|
1994-05-13 10:08:03 +04:00
|
|
|
notrace:
|
1996-09-08 18:28:08 +04:00
|
|
|
release:
|
1994-05-13 10:08:03 +04:00
|
|
|
splx(s);
|
1996-02-14 01:07:57 +03:00
|
|
|
return (error);
|
1994-05-13 10:08:03 +04:00
|
|
|
}
|
|
|
|
|
1996-02-14 01:07:57 +03:00
|
|
|
int
|
1994-05-13 10:08:03 +04:00
|
|
|
tuba_ctloutput(op, so, level, optname, mp)
|
1996-02-14 01:07:57 +03:00
|
|
|
int op;
|
|
|
|
struct socket *so;
|
|
|
|
int level, optname;
|
|
|
|
struct mbuf **mp;
|
1994-05-13 10:08:03 +04:00
|
|
|
{
|
1996-02-14 01:07:57 +03:00
|
|
|
if (level != IPPROTO_TCP)
|
|
|
|
return rclnp_ctloutput(op, so, level, optname, mp);
|
|
|
|
else
|
|
|
|
return tcp_ctloutput(op, so, level, optname, mp);
|
1994-05-13 10:08:03 +04:00
|
|
|
}
|