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.5 (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 struct isopcb tuba_isopcb; 46 extern int tuba_table_size; 47 extern int tcppcbcachemiss, tcppredack, tcppreddat, tcprexmtthresh; 48 extern struct inpcb *tcp_last_inpcb; 49 extern struct tcpiphdr tcp_saveti; 50 /* 51 * Tuba initialization 52 */ 53 tuba_init() 54 { 55 #define TUBAHDRSIZE (3 /*LLC*/ + 9 /*CLNP Fixed*/ + 42 /*Addresses*/ \ 56 + 6 /*CLNP Segment*/ + 20 /*TCP*/) 57 58 tuba_isopcb.isop_next = tuba_isopcb.isop_prev = &tuba_isopcb; 59 tuba_isopcb.isop_faddr = &tuba_isopcb.isop_sfaddr; 60 tuba_isopcb.isop_laddr = &tuba_isopcb.isop_sladdr; 61 if (max_protohdr < TUBAHDRSIZE) 62 max_protohdr = TUBAHDRSIZE; 63 if (max_linkhdr + TUBAHDRSIZE > MHLEN) 64 panic("tuba_init"); 65 } 66 67 static void 68 tuba_getaddr(error, sum, siso, index) 69 int *error; 70 register u_long *sum; 71 struct sockaddr_iso *siso; 72 u_long index; 73 { 74 register struct tuba_cache *tc; 75 if (index <= tuba_table_size && (tc = tuba_table[index])) { 76 if (siso) { 77 *siso = null_siso; 78 siso->siso_addr = tc->tc_addr; 79 } 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 return (clnp_output(m, isop, m->m_pkthdr.len, 0)); 123 } 124 125 tuba_refcnt(isop, delta) 126 struct isopcb *isop; 127 { 128 register struct tuba_cache *tc; 129 unsigned index, sum; 130 131 if (delta != 1) 132 delta = -1; 133 if (isop == 0 || isop->isop_faddr == 0 || isop->isop_laddr == 0 || 134 (delta == -1 && isop->isop_tuba_cached == 0) || 135 (delta == 1 && isop->isop_tuba_cached != 0)) 136 return; 137 isop->isop_tuba_cached = (delta == 1); 138 if ((index = tuba_lookup(&isop->isop_sfaddr.siso_addr, M_DONTWAIT)) != 0 && 139 (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) 140 tc->tc_refcnt += delta; 141 if ((index = tuba_lookup(&isop->isop_sladdr.siso_addr, M_DONTWAIT)) != 0 && 142 (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) 143 tc->tc_refcnt += delta; 144 } 145 146 tuba_pcbdetach(isop) 147 struct isopcb *isop; 148 { 149 if (isop == 0) 150 return; 151 tuba_refcnt(isop, -1); 152 isop->isop_socket = 0; 153 iso_pcbdetach(isop); 154 } 155 156 /* 157 * Avoid in_pcbconnect in faked out tcp_input() 158 */ 159 tuba_pcbconnect(inp, nam) 160 register struct inpcb *inp; 161 struct mbuf *nam; 162 { 163 register struct sockaddr_iso *siso = mtod(nam, struct sockaddr_iso *); 164 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 165 struct tcpcb *tp = intotcpcb(inp); 166 unsigned index = sin->sin_addr.s_addr; 167 struct tuba_cache *tc = tuba_table[index]; 168 struct isopcb *isop = (struct isopcb *)tp->t_tuba_pcb; 169 int error; 170 171 inp->inp_faddr.s_addr = index; 172 inp->inp_fport = sin->sin_port; 173 *siso = null_siso; 174 siso->siso_addr = tc->tc_addr; 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 tuba_badcksum() {} /* XXX - to set breakpoints */ 184 185 /* 186 * CALLED FROM: 187 * clnp's input routine, indirectly through the protosw. 188 * FUNCTION and ARGUMENTS: 189 * Take a packet (m) from clnp, strip off the clnp header 190 * and do tcp input processing. 191 * No return value. 192 */ 193 tuba_tcpinput(m, src, dst, clnp_len, ce_bit) 194 register struct mbuf *m; 195 struct sockaddr_iso *src, *dst; 196 int clnp_len, ce_bit; 197 { 198 int s = splnet(); 199 unsigned long sum, lindex, findex; 200 register struct tcpiphdr *ti; 201 register struct inpcb *inp; 202 struct mbuf *om; 203 int len, tlen, off; 204 register struct tcpcb *tp = 0; 205 int tiflags; 206 struct socket *so; 207 int todrop, acked, ourfinisacked, needoutput = 0; 208 short ostate; 209 struct in_addr laddr; 210 int dropsocket = 0, iss = 0; 211 212 if ((m->m_flags & M_PKTHDR) == 0) { 213 om = m_gethdr(M_DONTWAIT, MT_DATA); 214 if (om == 0) 215 goto drop; 216 om->m_next = m; 217 } else 218 om = m; 219 for (len = 0; m; m = m->m_next) 220 len += m->m_len; 221 m = om; 222 om = 0; 223 m->m_pkthdr.len = len; 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 REDUCE(sum, tuba_table[findex]->tc_sum_in + htons((u_short)tlen) 253 + htons((u_short)ISOPROTO_TCP) 254 + tuba_table[lindex]->tc_sum_in + in_cksum(m, tlen)); 255 if (sum) { 256 tcpstat.tcps_rcvbadsum++; 257 tuba_badcksum(); 258 goto drop; 259 } 260 m->m_data -= sizeof(struct ip); 261 m->m_len += sizeof(struct ip); 262 m->m_pkthdr.len += sizeof(struct ip); 263 /* 264 * The reassembly code assumes it will be overwriting a useless 265 * part of the packet, which is why we need to have it point 266 * into the packet itself. 267 * 268 * Check to see if the data is properly alligned 269 * so that we can save copying the tcp header. 270 * This code knows way too much about the structure of mbufs! 271 */ 272 off = ((sizeof (long) - 1) & ((m->m_flags & M_EXT) ? 273 (m->m_data - m->m_ext.ext_buf) : (m->m_data - m->m_pktdat))); 274 if (off) { 275 struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_DATA); 276 if (m0 == 0) { 277 goto drop; 278 } 279 bcopy(mtod(m, caddr_t) + sizeof(struct ip), 280 mtod(m0, caddr_t) + sizeof(struct ip), 281 sizeof(struct tcphdr)); 282 m->m_data += sizeof(struct tcpiphdr); 283 m0->m_next = m; 284 m0->m_pkthdr = m->m_pkthdr; 285 m0->m_flags = m->m_flags & M_COPYFLAGS; 286 m0->m_len = sizeof(struct tcpiphdr); 287 m = m0; 288 } 289 ti = mtod(m, struct tcpiphdr *); 290 ti->ti_src.s_addr = findex; 291 ti->ti_dst.s_addr = lindex; 292 /* 293 * Now include the rest of TCP input 294 */ 295 #define TUBA_INCLUDE 296 #define in_pcbconnect tuba_pcbconnect 297 298 #include <netinet/tcp_input.c> 299 } 300