xref: /csrg-svn/sys/netiso/tuba_subr.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_subr.c	7.7 (Berkeley) 11/05/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;
46 extern	int	tcppcbcachemiss, tcppredack, tcppreddat, tcprexmtthresh;
47 extern	struct	tcpiphdr tcp_saveti;
48 struct	inpcb	*tuba_last_inpcb;
49 struct	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 = null_siso;
80 			siso->siso_addr = tc->tc_addr;
81 		}
82 		REDUCE(*sum, *sum + tc->tc_sum_out);
83 	} else
84 		*error = 1;
85 }
86 
87 tuba_output(m, tp)
88 	register struct mbuf *m;
89 	struct tcpcb *tp;
90 {
91 	struct isopcb *isop;
92 	register struct tcpiphdr *n;
93 	u_long sum, i;
94 
95 	if (tp == 0 || (n = tp->t_template) == 0 ||
96 	    (isop = (struct isopcb *)tp->t_tuba_pcb) == 0) {
97 		isop = &tuba_isopcb;
98 		n = mtod(m, struct tcpiphdr *);
99 		i = sum = 0;
100 		tuba_getaddr(&i, &sum, tuba_isopcb.isop_faddr,
101 				n->ti_dst.s_addr);
102 		tuba_getaddr(&i, &sum, tuba_isopcb.isop_laddr,
103 				n->ti_src.s_addr);
104 		goto adjust;
105 	}
106 	if (n->ti_sum == 0) {
107 		i = sum = 0;
108 		tuba_getaddr(&i, &sum, (struct sockaddr_iso *)0,
109 				n->ti_dst.s_addr);
110 		tuba_getaddr(&i, &sum, (struct sockaddr_iso *)0,
111 				n->ti_src.s_addr);
112 		n->ti_sum = sum;
113 		n = mtod(m, struct tcpiphdr *);
114 	adjust:
115 		if (i) {
116 			m_freem(m);
117 			return (EADDRNOTAVAIL);
118 		}
119 		REDUCE(n->ti_sum, n->ti_sum + (0xffff ^ sum));
120 	}
121 	m->m_len -= sizeof (struct ip);
122 	m->m_pkthdr.len -= sizeof (struct ip);
123 	m->m_data += sizeof (struct ip);
124 	return (clnp_output(m, isop, m->m_pkthdr.len, 0));
125 }
126 
127 
128 tuba_refcnt(isop, delta)
129 	struct isopcb *isop;
130 {
131 	register struct tuba_cache *tc;
132 	unsigned index, sum;
133 
134 	if (delta != 1)
135 		delta = -1;
136 	if (isop == 0 || isop->isop_faddr == 0 || isop->isop_laddr == 0 ||
137 	    (delta == -1 && isop->isop_tuba_cached == 0) ||
138 	    (delta == 1 && isop->isop_tuba_cached != 0))
139 		return;
140 	isop->isop_tuba_cached = (delta == 1);
141 	if ((index = tuba_lookup(&isop->isop_sfaddr.siso_addr, M_DONTWAIT)) != 0 &&
142 	    (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0))
143 		tc->tc_refcnt += delta;
144 	if ((index = tuba_lookup(&isop->isop_sladdr.siso_addr, M_DONTWAIT)) != 0 &&
145 	    (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0))
146 		tc->tc_refcnt += delta;
147 }
148 
149 tuba_pcbdetach(isop)
150 	struct isopcb *isop;
151 {
152 	if (isop == 0)
153 		return;
154 	tuba_refcnt(isop, -1);
155 	isop->isop_socket = 0;
156 	iso_pcbdetach(isop);
157 }
158 
159 /*
160  * Avoid  in_pcbconnect in faked out tcp_input()
161  */
162 tuba_pcbconnect(inp, nam)
163 	register struct inpcb *inp;
164 	struct mbuf *nam;
165 {
166 	register struct sockaddr_iso *siso = mtod(nam, struct sockaddr_iso *);
167 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
168 	struct tcpcb *tp = intotcpcb(inp);
169 	unsigned index = sin->sin_addr.s_addr;
170 	struct tuba_cache *tc = tuba_table[index];
171 	struct isopcb *isop = (struct isopcb *)tp->t_tuba_pcb;
172 	int error;
173 
174 	inp->inp_faddr.s_addr = index;
175 	inp->inp_fport = sin->sin_port;
176 	*siso = null_siso;
177 	siso->siso_addr = tc->tc_addr;
178 	siso->siso_tlen = sizeof(inp->inp_fport);
179 	bcopy((caddr_t)&inp->inp_fport, TSEL(siso), sizeof(inp->inp_fport));
180 	nam->m_len = sizeof(*siso);
181 	if ((error = iso_pcbconnect(isop, nam)) == 0)
182 		tuba_refcnt(isop, 1);
183 	return (error);
184 }
185 
186 /*
187  * CALLED FROM:
188  * 	clnp's input routine, indirectly through the protosw.
189  * FUNCTION and ARGUMENTS:
190  * Take a packet (m) from clnp, strip off the clnp header
191  * and do tcp input processing.
192  * No return value.
193  */
194 tuba_tcpinput(m, src, dst, clnp_len, ce_bit)
195 	register struct mbuf *m;
196 	struct sockaddr_iso *src, *dst;
197 	int clnp_len, ce_bit;
198 {
199 	int s = splnet();
200 	unsigned long sum, lindex, findex;
201 	register struct tcpiphdr *ti;
202 	register struct inpcb *inp;
203 	struct mbuf *om;
204 	int len, tlen, off;
205 	register struct tcpcb *tp = 0;
206 	int tiflags;
207 	struct socket *so;
208 	int todrop, acked, ourfinisacked, needoutput = 0;
209 	short ostate;
210 	struct in_addr laddr;
211 	int dropsocket = 0, iss = 0;
212 
213 	if ((m->m_flags & M_PKTHDR) == 0) {
214 		om = m_gethdr(M_DONTWAIT, MT_DATA);
215 		if (om == 0)
216 			goto drop;
217 		om->m_next = m;
218 		for (len = 0; m; m = m->m_next)
219 			len += m->m_len;
220 		m = om;
221 		m->m_pkthdr.len = len;
222 	}
223 	om = 0;
224 	/*
225 	 * Do some housekeeping looking up CLNP addresses.
226 	 * If we are out of space might as well drop the packet now.
227 	 */
228 	tcpstat.tcps_rcvtotal++;
229 	lindex = tuba_lookup(&dst->siso_addr, M_DONTWAIT);
230 	findex = tuba_lookup(&dst->siso_addr, M_DONTWAIT);
231 	if (lindex == 0 || findex == 0)
232 		goto drop;
233 	/*
234 	 * Get CLNP and TCP header together in first mbuf.
235 	 * CLNP gave us an mbuf chain WITH the clnp header pulled up,
236 	 * and the length of the clnp header.
237 	 */
238 	len = clnp_len + sizeof(struct tcphdr);
239 	if (m->m_len < len) {
240 		if ((m = m_pullup(m, len)) == 0) {
241 			tcpstat.tcps_rcvshort++;
242 			return;
243 		}
244 	}
245 	/*
246 	 * Calculate checksum of extended TCP header and data,
247 	 * by adjusting the checksum for missing parts of the header.
248 	 */
249 	m->m_data += clnp_len;
250 	m->m_len -= clnp_len;
251 	tlen = m->m_pkthdr.len -= clnp_len;
252 	m->m_data -= sizeof(struct ip);
253 	m->m_len += sizeof(struct ip);
254 	m->m_pkthdr.len += sizeof(struct ip);
255 	/*
256 	 * The reassembly code assumes it will be overwriting a useless
257 	 * part of the packet, which is why we need to have it point
258 	 * into the packet itself.
259 	 *
260 	 * Check to see if the data is properly alligned
261 	 * so that we can save copying the tcp header.
262 	 * This code knows way too much about the structure of mbufs!
263 	 */
264 	off = ((sizeof (long) - 1) & ((m->m_flags & M_EXT) ?
265 		(m->m_data - m->m_ext.ext_buf) :  (m->m_data - m->m_pktdat)));
266 	if (off) {
267 		struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_DATA);
268 		if (m0 == 0) {
269 			goto drop;
270 		}
271 		m0->m_data += max_linkhdr;
272 		bcopy(mtod(m, caddr_t) + sizeof(struct ip),
273 		      mtod(m0, caddr_t) + sizeof(struct ip),
274 		      sizeof(struct tcphdr));
275 		m->m_data += sizeof(struct tcpiphdr);
276 		m0->m_next = m;
277 		m0->m_pkthdr = m->m_pkthdr;
278 		m0->m_flags = m->m_flags & M_COPYFLAGS;
279 		m0->m_len = sizeof(struct tcpiphdr);
280 		m = m0;
281 	}
282 	ti = mtod(m, struct tcpiphdr *);
283 	ti->ti_src.s_addr = tuba_table[findex]->tc_sum_in;
284 	ti->ti_dst.s_addr = tuba_table[lindex]->tc_sum_in;
285 	ti->ti_prev = ti->ti_next = 0;
286 	ti->ti_x1 = 0; ti->ti_pr = ISOPROTO_TCP;
287 	ti->ti_len = htons((u_short)tlen);
288 	if (ti->ti_sum = in_cksum(m, m->m_pkthdr.len)) {
289 		tcpstat.tcps_rcvbadsum++;
290 		goto drop;
291 	}
292 	ti->ti_src.s_addr = findex;
293 	ti->ti_dst.s_addr = lindex;
294 	/*
295 	 * Now include the rest of TCP input
296 	 */
297 #define TUBA_INCLUDE
298 #define	in_pcbconnect	tuba_pcbconnect
299 #define	tcb		tuba_inpcb
300 #define tcp_last_inpcb	tuba_last_inpcb
301 
302 #include <netinet/tcp_input.c>
303 }
304