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.10 (Berkeley) 11/20/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 struct addr_arg { 70 int error; 71 int offset; 72 u_long sum; 73 }; 74 75 /* 76 * Calculate contribution to fudge factor for TCP checksum, 77 * and coincidentally set pointer for convenience of clnp_output 78 * if we are are responding when there is no isopcb around. 79 */ 80 static void 81 tuba_getaddr(arg, siso, index) 82 register struct addr_arg *arg; 83 struct sockaddr_iso **siso; 84 u_long index; 85 { 86 register struct tuba_cache *tc; 87 if (index <= tuba_table_size && (tc = tuba_table[index])) { 88 if (siso) 89 *siso = &tc->tc_siso; 90 arg->sum += (arg->offset & 1 ? tc->tc_ssum : tc->tc_sum) 91 + (0xffff ^ index); 92 arg->offset += tc->tc_siso.siso_nlen + 1; 93 } else 94 arg->error = 1; 95 } 96 97 tuba_output(m, tp) 98 register struct mbuf *m; 99 struct tcpcb *tp; 100 { 101 register struct tcpiphdr *n; 102 struct isopcb *isop; 103 struct addr_arg arg; 104 105 if (tp == 0 || (n = tp->t_template) == 0 || 106 (isop = (struct isopcb *)tp->t_tuba_pcb) == 0) { 107 isop = &tuba_isopcb; 108 n = mtod(m, struct tcpiphdr *); 109 arg.error = arg.sum = arg.offset = 0; 110 tuba_getaddr(&arg, &tuba_isopcb.isop_faddr, n->ti_dst.s_addr); 111 tuba_getaddr(&arg, &tuba_isopcb.isop_laddr, n->ti_src.s_addr); 112 REDUCE(arg.sum, arg.sum); 113 goto adjust; 114 } 115 if (n->ti_sum == 0) { 116 arg.error = arg.sum = arg.offset = 0; 117 tuba_getaddr(&arg, (struct sockaddr_iso **)0, n->ti_dst.s_addr); 118 tuba_getaddr(&arg, (struct sockaddr_iso **)0, n->ti_src.s_addr); 119 REDUCE(arg.sum, arg.sum); 120 n->ti_sum = arg.sum; 121 n = mtod(m, struct tcpiphdr *); 122 adjust: 123 if (arg.error) { 124 m_freem(m); 125 return (EADDRNOTAVAIL); 126 } 127 REDUCE(n->ti_sum, n->ti_sum + (0xffff ^ arg.sum)); 128 } 129 m->m_len -= sizeof (struct ip); 130 m->m_pkthdr.len -= sizeof (struct ip); 131 m->m_data += sizeof (struct ip); 132 return (clnp_output(m, isop, m->m_pkthdr.len, 0)); 133 } 134 135 tuba_refcnt(isop, delta) 136 struct isopcb *isop; 137 { 138 register struct tuba_cache *tc; 139 unsigned index, sum; 140 141 if (delta != 1) 142 delta = -1; 143 if (isop == 0 || isop->isop_faddr == 0 || isop->isop_laddr == 0 || 144 (delta == -1 && isop->isop_tuba_cached == 0) || 145 (delta == 1 && isop->isop_tuba_cached != 0)) 146 return; 147 isop->isop_tuba_cached = (delta == 1); 148 if ((index = tuba_lookup(&isop->isop_sfaddr.siso_addr, M_DONTWAIT)) != 0 && 149 (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) 150 tc->tc_refcnt += delta; 151 if ((index = tuba_lookup(&isop->isop_sladdr.siso_addr, M_DONTWAIT)) != 0 && 152 (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) 153 tc->tc_refcnt += delta; 154 } 155 156 tuba_pcbdetach(isop) 157 struct isopcb *isop; 158 { 159 if (isop == 0) 160 return; 161 tuba_refcnt(isop, -1); 162 isop->isop_socket = 0; 163 iso_pcbdetach(isop); 164 } 165 166 /* 167 * Avoid in_pcbconnect in faked out tcp_input() 168 */ 169 tuba_pcbconnect(inp, nam) 170 register struct inpcb *inp; 171 struct mbuf *nam; 172 { 173 register struct sockaddr_iso *siso = mtod(nam, struct sockaddr_iso *); 174 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 175 struct tcpcb *tp = intotcpcb(inp); 176 unsigned index = sin->sin_addr.s_addr; 177 struct tuba_cache *tc = tuba_table[index]; 178 struct isopcb *isop = (struct isopcb *)tp->t_tuba_pcb; 179 int error; 180 181 inp->inp_faddr.s_addr = index; 182 inp->inp_fport = sin->sin_port; 183 *siso = tc->tc_siso; 184 siso->siso_tlen = sizeof(inp->inp_fport); 185 bcopy((caddr_t)&inp->inp_fport, TSEL(siso), sizeof(inp->inp_fport)); 186 nam->m_len = sizeof(*siso); 187 if ((error = iso_pcbconnect(isop, nam)) == 0) 188 tuba_refcnt(isop, 1); 189 return (error); 190 } 191 192 /* 193 * CALLED FROM: 194 * clnp's input routine, indirectly through the protosw. 195 * FUNCTION and ARGUMENTS: 196 * Take a packet (m) from clnp, strip off the clnp header 197 * and do tcp input processing. 198 * No return value. 199 */ 200 tuba_tcpinput(m, src, dst) 201 register struct mbuf *m; 202 struct sockaddr_iso *src, *dst; 203 { 204 unsigned long sum, lindex, findex; 205 register struct tcpiphdr *ti; 206 register struct inpcb *inp; 207 struct mbuf *om; 208 int len, tlen, off; 209 register struct tcpcb *tp = 0; 210 int tiflags; 211 struct socket *so; 212 int todrop, acked, ourfinisacked, needoutput = 0; 213 short ostate; 214 struct in_addr laddr; 215 int dropsocket = 0, iss = 0; 216 217 if ((m->m_flags & M_PKTHDR) == 0) 218 panic("tuba_tcpinput"); 219 /* 220 * Do some housekeeping looking up CLNP addresses. 221 * If we are out of space might as well drop the packet now. 222 */ 223 tcpstat.tcps_rcvtotal++; 224 lindex = tuba_lookup(&dst->siso_addr, M_DONTWAIT); 225 findex = tuba_lookup(&src->siso_addr, M_DONTWAIT); 226 if (lindex == 0 || findex == 0) 227 goto drop; 228 /* 229 * CLNP gave us an mbuf chain WITH the clnp header pulled up, 230 * but the data pointer pushed past it. 231 */ 232 len = m->m_len; 233 tlen = m->m_pkthdr.len; 234 m->m_data -= sizeof(struct ip); 235 m->m_len += sizeof(struct ip); 236 m->m_pkthdr.len += sizeof(struct ip); 237 /* 238 * The reassembly code assumes it will be overwriting a useless 239 * part of the packet, which is why we need to have it point 240 * into the packet itself. 241 * 242 * Check to see if the data is properly alligned 243 * so that we can save copying the tcp header. 244 * This code knows way too much about the structure of mbufs! 245 */ 246 off = ((sizeof (long) - 1) & ((m->m_flags & M_EXT) ? 247 (m->m_data - m->m_ext.ext_buf) : (m->m_data - m->m_pktdat))); 248 if (off || len < sizeof(struct tcphdr)) { 249 struct mbuf *m0 = m; 250 251 MGETHDR(m, M_DONTWAIT, MT_DATA); 252 if (m == 0) { 253 m = m0; 254 goto drop; 255 } 256 m->m_next = m0; 257 m->m_data += max_linkhdr; 258 m->m_pkthdr = m->m_pkthdr; 259 m->m_flags = m->m_flags & M_COPYFLAGS; 260 if (len < sizeof(struct tcphdr)) { 261 if ((m = m_pullup(m, sizeof(struct tcpiphdr))) == 0) { 262 tcpstat.tcps_rcvshort++; 263 return; 264 } 265 } else { 266 bcopy(mtod(m, caddr_t) + sizeof(struct ip), 267 mtod(m0, caddr_t) + sizeof(struct ip), 268 sizeof(struct tcphdr)); 269 m0->m_len -= sizeof(struct tcpiphdr); 270 m0->m_data += sizeof(struct tcpiphdr); 271 m->m_len = sizeof(struct tcpiphdr); 272 } 273 } 274 /* 275 * Calculate checksum of extended TCP header and data, 276 * replacing what would have been IP addresses by 277 * the IP checksum of the CLNP addresses. 278 */ 279 ti = mtod(m, struct tcpiphdr *); 280 ti->ti_dst.s_addr = tuba_table[lindex]->tc_sum; 281 if (dst->siso_nlen & 1) 282 ti->ti_src.s_addr = tuba_table[findex]->tc_sum; 283 else 284 ti->ti_src.s_addr = tuba_table[findex]->tc_ssum; 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 305 #define tcp_slowtimo tuba_slowtimo 306 #define tcp_fasttimo tuba_fasttimo 307 308 #include <netinet/tcp_timer.c> 309