xref: /csrg-svn/sys/netiso/tuba_usrreq.c (revision 56690)
1 /*
2  * Copyright (c) 1992 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tuba_usrreq.c	7.5 (Berkeley) 11/05/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/malloc.h>
13 #include <sys/mbuf.h>
14 #include <sys/socket.h>
15 #include <sys/socketvar.h>
16 #include <sys/protosw.h>
17 #include <sys/errno.h>
18 #include <sys/stat.h>
19 
20 #include <net/if.h>
21 #include <net/route.h>
22 
23 #include <netinet/in.h>
24 #include <netinet/in_systm.h>
25 #include <netinet/ip.h>
26 #include <netinet/in_pcb.h>
27 #include <netinet/ip_var.h>
28 #include <netinet/tcp.h>
29 #include <netinet/tcp_fsm.h>
30 #include <netinet/tcp_seq.h>
31 #include <netinet/tcp_timer.h>
32 #include <netinet/tcp_var.h>
33 #include <netinet/tcpip.h>
34 #include <netinet/tcp_debug.h>
35 
36 #include <netiso/argo_debug.h>
37 #include <netiso/iso.h>
38 #include <netiso/clnp.h>
39 #include <netiso/iso_pcb.h>
40 #include <netiso/iso_var.h>
41 #include <netiso/tuba_table.h>
42 /*
43  * TCP protocol interface to socket abstraction.
44  */
45 extern	char *tcpstates[];
46 extern	struct inpcb tuba_inpcb;
47 extern	struct isopcb tuba_isopcb;
48 
49 /*
50  * Process a TCP user request for TCP tb.  If this is a send request
51  * then m is the mbuf chain of send data.  If this is a timer expiration
52  * (called from the software clock routine), then timertype tells which timer.
53  */
54 /*ARGSUSED*/
55 tuba_usrreq(so, req, m, nam, control)
56 	struct socket *so;
57 	int req;
58 	struct mbuf *m, *nam, *control;
59 {
60 	register struct inpcb *inp;
61 	register struct isopcb *isop;
62 	register struct tcpcb *tp;
63 	int s;
64 	int error = 0;
65 	int ostate;
66 	struct sockaddr_iso *siso;
67 
68 	if (req == PRU_CONTROL)
69 		return (iso_control(so, (int)m, (caddr_t)nam,
70 			(struct ifnet *)control));
71 
72 	s = splnet();
73 	inp = sotoinpcb(so);
74 	/*
75 	 * When a TCP is attached to a socket, then there will be
76 	 * a (struct inpcb) pointed at by the socket, and this
77 	 * structure will point at a subsidary (struct tcpcb).
78 	 */
79 	if (inp == 0  && req != PRU_ATTACH) {
80 		splx(s);
81 		return (EINVAL);		/* XXX */
82 	}
83 	if (inp) {
84 		tp = intotcpcb(inp);
85 		if (tp == 0)
86 			panic("tuba_usrreq");
87 		ostate = tp->t_state;
88 		isop = (struct isopcb *)tp->t_tuba_pcb;
89 		if (isop == 0)
90 			panic("tuba_usrreq 2");
91 	} else
92 		ostate = 0;
93 	switch (req) {
94 
95 	/*
96 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
97 	 * and an internet control block.  We also need to
98 	 * allocate an isopcb and separate the control block from
99 	 * tcp/ip ones.
100 	 */
101 	case PRU_ATTACH:
102 		if (error = iso_pcballoc(so, &tuba_isopcb))
103 			break;
104 		isop = (struct isopcb *)so->so_pcb;
105 		so->so_pcb = 0;
106 		if (error = tcp_usrreq(so, req, m, nam, control)) {
107 			isop->isop_socket = 0;
108 			iso_pcbdetach(isop);
109 		} else {
110 			inp = sotoinpcb(so);
111 			remque(inp);
112 			insque(inp, &tuba_inpcb);
113 			inp->inp_head = &tuba_inpcb;
114 			tp = intotcpcb(inp);
115 			if (tp == 0)
116 				panic("tuba_usrreq 3");
117 			tp->t_tuba_pcb = (caddr_t) isop;
118 		}
119 		goto notrace;
120 
121 	/*
122 	 * PRU_DETACH detaches the TCP protocol from the socket.
123 	 * If the protocol state is non-embryonic, then can't
124 	 * do this directly: have to initiate a PRU_DISCONNECT,
125 	 * which may finish later; embryonic TCB's can just
126 	 * be discarded here.
127 	 */
128 	case PRU_DETACH:
129 		if (tp->t_state > TCPS_LISTEN)
130 			tp = tcp_disconnect(tp);
131 		else
132 			tp = tcp_close(tp);
133 		if (tp == 0)
134 			tuba_pcbdetach(isop);
135 		break;
136 
137 	/*
138 	 * Give the socket an address.
139 	 */
140 	case PRU_BIND:
141 		siso = mtod(nam, struct sockaddr_iso *);
142 		if (siso->siso_tlen && siso->siso_tlen != 2) {
143 			error = EINVAL;
144 			break;
145 		}
146 		if ((error = iso_pcbbind(isop, nam)) ||
147 		    (siso = isop->isop_laddr) == 0)
148 			break;
149 		bcopy(TSEL(siso), &inp->inp_lport, 2);
150 		if (siso->siso_nlen &&
151 		    !(inp->inp_laddr.s_addr = tuba_lookup(&siso->siso_addr, M_WAITOK)))
152 			error = ENOBUFS;
153 		break;
154 
155 	/*
156 	 * Prepare to accept connections.
157 	 */
158 	case PRU_CONNECT:
159 	case PRU_LISTEN:
160 		if (inp->inp_lport == 0 &&
161 		    (error = iso_pcbbind(isop, (struct mbuf *)0)))
162 			break;
163 		bcopy(TSEL(isop->isop_laddr), &inp->inp_lport, 2);
164 		if (req == PRU_LISTEN) {
165 			tp->t_state = TCPS_LISTEN;
166 			break;
167 		}
168 	/*FALLTHROUGH*/
169 	/*
170 	 * Initiate connection to peer.
171 	 * Create a template for use in transmissions on this connection.
172 	 * Enter SYN_SENT state, and mark socket as connecting.
173 	 * Start keep-alive timer, and seed output sequence space.
174 	 * Send initial segment on connection.
175 	 */
176 	/* case PRU_CONNECT: */
177 		if (error = iso_pcbconnect(isop, nam))
178 			break;
179 		siso = mtod(nam, struct sockaddr_iso *);
180 		if (!(inp->inp_faddr.s_addr = tuba_lookup(&siso->siso_addr, M_WAITOK))) {
181 		unconnect:
182 			iso_pcbdisconnect(isop);
183 			error = ENOBUFS;
184 			break;
185 		}
186 		bcopy(TSEL(isop->isop_faddr), &inp->inp_fport, 2);
187 		if (inp->inp_laddr.s_addr == 0 &&
188 		     (inp->inp_laddr.s_addr =
189 			    tuba_lookup(&isop->isop_laddr->siso_addr, M_WAITOK)) == 0)
190 			goto unconnect;
191 		if ((tp->t_template = tcp_template(tp)) == 0)
192 			goto unconnect;
193 		soisconnecting(so);
194 		tcpstat.tcps_connattempt++;
195 		tp->t_state = TCPS_SYN_SENT;
196 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
197 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
198 		tcp_sendseqinit(tp);
199 		error = tcp_output(tp);
200 		tuba_refcnt(isop, 1);
201 		break;
202 
203 	/*
204 	 * Initiate disconnect from peer.
205 	 * If connection never passed embryonic stage, just drop;
206 	 * else if don't need to let data drain, then can just drop anyways,
207 	 * else have to begin TCP shutdown process: mark socket disconnecting,
208 	 * drain unread data, state switch to reflect user close, and
209 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
210 	 * when peer sends FIN and acks ours.
211 	 *
212 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
213 	 */
214 	case PRU_DISCONNECT:
215 		if ((tp = tcp_disconnect(tp)) == 0)
216 			tuba_pcbdetach(isop);
217 		break;
218 
219 	/*
220 	 * Accept a connection.  Essentially all the work is
221 	 * done at higher levels; just return the address
222 	 * of the peer, storing through addr.
223 	 */
224 	case PRU_ACCEPT:
225 		bcopy((caddr_t)isop->isop_faddr, mtod(m, caddr_t),
226 			nam->m_len = isop->isop_faddr->siso_len);
227 		break;
228 
229 	/*
230 	 * Mark the connection as being incapable of further output.
231 	 */
232 	case PRU_SHUTDOWN:
233 		socantsendmore(so);
234 		tp = tcp_usrclosed(tp);
235 		if (tp)
236 			error = tcp_output(tp);
237 		else
238 			tuba_pcbdetach(isop);
239 		break;
240 	/*
241 	 * Abort the TCP.
242 	 */
243 	case PRU_ABORT:
244 		if ((tp = tcp_drop(tp, ECONNABORTED)) == 0)
245 			tuba_pcbdetach(isop);
246 		break;
247 
248 
249 	case PRU_SOCKADDR:
250 		if (isop->isop_laddr)
251 			bcopy((caddr_t)isop->isop_laddr, mtod(m, caddr_t),
252 				nam->m_len = isop->isop_laddr->siso_len);
253 		break;
254 
255 	case PRU_PEERADDR:
256 		if (isop->isop_faddr)
257 			bcopy((caddr_t)isop->isop_faddr, mtod(m, caddr_t),
258 				nam->m_len = isop->isop_faddr->siso_len);
259 		break;
260 
261 	default:
262 		error = tcp_usrreq(so, req, m, nam, control);
263 		goto notrace;
264 	}
265 	if (tp && (so->so_options & SO_DEBUG))
266 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
267 notrace:
268 	splx(s);
269 	return(error);
270 }
271 
272 tuba_ctloutput(op, so, level, optname, mp)
273 	int op;
274 	struct socket *so;
275 	int level, optname;
276 	struct mbuf **mp;
277 {
278 	int clnp_ctloutput(), tcp_ctloutput();
279 
280 	return ((level != IPPROTO_TCP ? clnp_ctloutput : tcp_ctloutput)
281 		(clnp_ctloutput(op, so, level, optname, mp)));
282 }
283