xref: /csrg-svn/sys/netiso/tuba_subr.c (revision 56704)
1 /*
2  * Copyright (c) 1992 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tuba_subr.c	7.8 (Berkeley) 11/08/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/proc.h>
12 #include <sys/systm.h>
13 #include <sys/malloc.h>
14 #include <sys/mbuf.h>
15 #include <sys/socket.h>
16 #include <sys/socketvar.h>
17 #include <sys/protosw.h>
18 #include <sys/errno.h>
19 
20 #include <net/route.h>
21 #include <net/if.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/ip_icmp.h>
29 #include <netinet/tcp.h>
30 #include <netinet/tcp_fsm.h>
31 #include <netinet/tcp_seq.h>
32 #include <netinet/tcp_timer.h>
33 #include <netinet/tcp_var.h>
34 #include <netinet/tcpip.h>
35 #include <netinet/tcp_debug.h>
36 
37 #include <netiso/argo_debug.h>
38 #include <netiso/iso.h>
39 #include <netiso/clnp.h>
40 #include <netiso/iso_pcb.h>
41 #include <netiso/iso_var.h>
42 #include <netiso/tuba_table.h>
43 
44 static	struct	sockaddr_iso null_siso = { sizeof(null_siso), AF_ISO, };
45 extern	int	tuba_table_size, tcp_keepidle, tcp_keepintvl, tcp_maxidle;
46 extern	int	tcppcbcachemiss, tcppredack, tcppreddat, tcprexmtthresh;
47 extern	struct	tcpiphdr tcp_saveti;
48 struct	inpcb	tuba_inpcb;
49 struct	inpcb	*tuba_last_inpcb = &tuba_inpcb;
50 struct	isopcb	tuba_isopcb;
51 /*
52  * Tuba initialization
53  */
54 tuba_init()
55 {
56 #define TUBAHDRSIZE (3 /*LLC*/ + 9 /*CLNP Fixed*/ + 42 /*Addresses*/ \
57 		     + 6 /*CLNP Segment*/ + 20 /*TCP*/)
58 
59 	tuba_inpcb.inp_next = tuba_inpcb.inp_prev = &tuba_inpcb;
60 	tuba_isopcb.isop_next = tuba_isopcb.isop_prev = &tuba_isopcb;
61 	tuba_isopcb.isop_faddr = &tuba_isopcb.isop_sfaddr;
62 	tuba_isopcb.isop_laddr = &tuba_isopcb.isop_sladdr;
63 	if (max_protohdr < TUBAHDRSIZE)
64 		max_protohdr = TUBAHDRSIZE;
65 	if (max_linkhdr + TUBAHDRSIZE > MHLEN)
66 		panic("tuba_init");
67 }
68 
69 static void
70 tuba_getaddr(error, sum, siso, index)
71 	int *error;
72 	register u_long *sum;
73 	struct sockaddr_iso **siso;
74 	u_long index;
75 {
76 	register struct tuba_cache *tc;
77 	if (index <= tuba_table_size && (tc = tuba_table[index])) {
78 		if (siso)
79 			*siso = &tc->tc_siso;
80 		REDUCE(*sum, *sum + tc->tc_sum_out);
81 	} else
82 		*error = 1;
83 }
84 
85 tuba_output(m, tp)
86 	register struct mbuf *m;
87 	struct tcpcb *tp;
88 {
89 	struct isopcb *isop;
90 	register struct tcpiphdr *n;
91 	u_long sum, i;
92 
93 	if (tp == 0 || (n = tp->t_template) == 0 ||
94 	    (isop = (struct isopcb *)tp->t_tuba_pcb) == 0) {
95 		isop = &tuba_isopcb;
96 		n = mtod(m, struct tcpiphdr *);
97 		i = sum = 0;
98 		tuba_getaddr(&i, &sum, &tuba_isopcb.isop_faddr,
99 				n->ti_dst.s_addr);
100 		tuba_getaddr(&i, &sum, &tuba_isopcb.isop_laddr,
101 				n->ti_src.s_addr);
102 		goto adjust;
103 	}
104 	if (n->ti_sum == 0) {
105 		i = sum = 0;
106 		tuba_getaddr(&i, &sum, (struct sockaddr_iso **)0,
107 				n->ti_dst.s_addr);
108 		tuba_getaddr(&i, &sum, (struct sockaddr_iso **)0,
109 				n->ti_src.s_addr);
110 		n->ti_sum = sum;
111 		n = mtod(m, struct tcpiphdr *);
112 	adjust:
113 		if (i) {
114 			m_freem(m);
115 			return (EADDRNOTAVAIL);
116 		}
117 		REDUCE(n->ti_sum, n->ti_sum + (0xffff ^ sum));
118 	}
119 	m->m_len -= sizeof (struct ip);
120 	m->m_pkthdr.len -= sizeof (struct ip);
121 	m->m_data += sizeof (struct ip);
122 	i = clnp_output(m, isop, m->m_pkthdr.len, 0);
123 	return (i);
124 }
125 
126 tuba_refcnt(isop, delta)
127 	struct isopcb *isop;
128 {
129 	register struct tuba_cache *tc;
130 	unsigned index, sum;
131 
132 	if (delta != 1)
133 		delta = -1;
134 	if (isop == 0 || isop->isop_faddr == 0 || isop->isop_laddr == 0 ||
135 	    (delta == -1 && isop->isop_tuba_cached == 0) ||
136 	    (delta == 1 && isop->isop_tuba_cached != 0))
137 		return;
138 	isop->isop_tuba_cached = (delta == 1);
139 	if ((index = tuba_lookup(&isop->isop_sfaddr.siso_addr, M_DONTWAIT)) != 0 &&
140 	    (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0))
141 		tc->tc_refcnt += delta;
142 	if ((index = tuba_lookup(&isop->isop_sladdr.siso_addr, M_DONTWAIT)) != 0 &&
143 	    (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0))
144 		tc->tc_refcnt += delta;
145 }
146 
147 tuba_pcbdetach(isop)
148 	struct isopcb *isop;
149 {
150 	if (isop == 0)
151 		return;
152 	tuba_refcnt(isop, -1);
153 	isop->isop_socket = 0;
154 	iso_pcbdetach(isop);
155 }
156 
157 /*
158  * Avoid  in_pcbconnect in faked out tcp_input()
159  */
160 tuba_pcbconnect(inp, nam)
161 	register struct inpcb *inp;
162 	struct mbuf *nam;
163 {
164 	register struct sockaddr_iso *siso = mtod(nam, struct sockaddr_iso *);
165 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
166 	struct tcpcb *tp = intotcpcb(inp);
167 	unsigned index = sin->sin_addr.s_addr;
168 	struct tuba_cache *tc = tuba_table[index];
169 	struct isopcb *isop = (struct isopcb *)tp->t_tuba_pcb;
170 	int error;
171 
172 	inp->inp_faddr.s_addr = index;
173 	inp->inp_fport = sin->sin_port;
174 	*siso = tc->tc_siso;
175 	siso->siso_tlen = sizeof(inp->inp_fport);
176 	bcopy((caddr_t)&inp->inp_fport, TSEL(siso), sizeof(inp->inp_fport));
177 	nam->m_len = sizeof(*siso);
178 	if ((error = iso_pcbconnect(isop, nam)) == 0)
179 		tuba_refcnt(isop, 1);
180 	return (error);
181 }
182 
183 /*
184  * CALLED FROM:
185  * 	clnp's input routine, indirectly through the protosw.
186  * FUNCTION and ARGUMENTS:
187  * Take a packet (m) from clnp, strip off the clnp header
188  * and do tcp input processing.
189  * No return value.
190  */
191 tuba_tcpinput(m, src, dst, clnp_len, ce_bit)
192 	register struct mbuf *m;
193 	struct sockaddr_iso *src, *dst;
194 	int clnp_len, ce_bit;
195 {
196 	unsigned long sum, lindex, findex;
197 	register struct tcpiphdr *ti;
198 	register struct inpcb *inp;
199 	struct mbuf *om;
200 	int len, tlen, off;
201 	register struct tcpcb *tp = 0;
202 	int tiflags;
203 	struct socket *so;
204 	int todrop, acked, ourfinisacked, needoutput = 0;
205 	short ostate;
206 	struct in_addr laddr;
207 	int dropsocket = 0, iss = 0;
208 
209 	if ((m->m_flags & M_PKTHDR) == 0) {
210 		om = m_gethdr(M_DONTWAIT, MT_DATA);
211 		if (om == 0)
212 			goto drop;
213 		om->m_next = m;
214 		for (len = 0; m; m = m->m_next)
215 			len += m->m_len;
216 		m = om;
217 		m->m_pkthdr.len = len;
218 	}
219 	om = 0;
220 	/*
221 	 * Do some housekeeping looking up CLNP addresses.
222 	 * If we are out of space might as well drop the packet now.
223 	 */
224 	tcpstat.tcps_rcvtotal++;
225 	lindex = tuba_lookup(&dst->siso_addr, M_DONTWAIT);
226 	findex = tuba_lookup(&dst->siso_addr, M_DONTWAIT);
227 	if (lindex == 0 || findex == 0)
228 		goto drop;
229 	/*
230 	 * Get CLNP and TCP header together in first mbuf.
231 	 * CLNP gave us an mbuf chain WITH the clnp header pulled up,
232 	 * and the length of the clnp header.
233 	 */
234 	len = clnp_len + sizeof(struct tcphdr);
235 	if (m->m_len < len) {
236 		if ((m = m_pullup(m, len)) == 0) {
237 			tcpstat.tcps_rcvshort++;
238 			return;
239 		}
240 	}
241 	/*
242 	 * Calculate checksum of extended TCP header and data,
243 	 * by adjusting the checksum for missing parts of the header.
244 	 */
245 	m->m_data += clnp_len;
246 	m->m_len -= clnp_len;
247 	tlen = m->m_pkthdr.len -= clnp_len;
248 	m->m_data -= sizeof(struct ip);
249 	m->m_len += sizeof(struct ip);
250 	m->m_pkthdr.len += sizeof(struct ip);
251 	/*
252 	 * The reassembly code assumes it will be overwriting a useless
253 	 * part of the packet, which is why we need to have it point
254 	 * into the packet itself.
255 	 *
256 	 * Check to see if the data is properly alligned
257 	 * so that we can save copying the tcp header.
258 	 * This code knows way too much about the structure of mbufs!
259 	 */
260 	off = ((sizeof (long) - 1) & ((m->m_flags & M_EXT) ?
261 		(m->m_data - m->m_ext.ext_buf) :  (m->m_data - m->m_pktdat)));
262 	if (off) {
263 		struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_DATA);
264 		if (m0 == 0) {
265 			goto drop;
266 		}
267 		m0->m_data += max_linkhdr;
268 		bcopy(mtod(m, caddr_t) + sizeof(struct ip),
269 		      mtod(m0, caddr_t) + sizeof(struct ip),
270 		      sizeof(struct tcphdr));
271 		m->m_data += sizeof(struct tcpiphdr);
272 		m->m_len -= sizeof(struct tcpiphdr);
273 		m0->m_next = m;
274 		m0->m_pkthdr = m->m_pkthdr;
275 		m0->m_flags = m->m_flags & M_COPYFLAGS;
276 		m0->m_len = sizeof(struct tcpiphdr);
277 		m = m0;
278 	}
279 	ti = mtod(m, struct tcpiphdr *);
280 	ti->ti_src.s_addr = tuba_table[findex]->tc_sum_in;
281 	ti->ti_dst.s_addr = tuba_table[lindex]->tc_sum_in;
282 	ti->ti_prev = ti->ti_next = 0;
283 	ti->ti_x1 = 0; ti->ti_pr = ISOPROTO_TCP;
284 	ti->ti_len = htons((u_short)tlen);
285 	if (ti->ti_sum = in_cksum(m, m->m_pkthdr.len)) {
286 		tcpstat.tcps_rcvbadsum++;
287 		goto drop;
288 	}
289 	ti->ti_src.s_addr = findex;
290 	ti->ti_dst.s_addr = lindex;
291 	/*
292 	 * Now include the rest of TCP input
293 	 */
294 #define TUBA_INCLUDE
295 #define	in_pcbconnect	tuba_pcbconnect
296 #define	tcb		tuba_inpcb
297 #define tcp_last_inpcb	tuba_last_inpcb
298 
299 #include <netinet/tcp_input.c>
300 }
301 
302 #define tcp_slowtimo	tuba_slowtimo
303 #define tcp_fasttimo	tuba_fasttimo
304 #include <netinet/tcp_timer.c>
305