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.2 (Berkeley) 10/09/92 8 */ 9 10 #include "param.h" 11 #include "proc.h" 12 #include "systm.h" 13 #include "malloc.h" 14 #include "mbuf.h" 15 #include "socket.h" 16 #include "socketvar.h" 17 #include "protosw.h" 18 #include "errno.h" 19 20 #include "../net/route.h" 21 #include "../net/if.h" 22 23 #include "in.h" 24 #include "in_systm.h" 25 #include "ip.h" 26 #include "in_pcb.h" 27 #include "ip_var.h" 28 #include "ip_icmp.h" 29 #include "tcp.h" 30 #include "tcp_fsm.h" 31 #include "tcp_seq.h" 32 #include "tcp_timer.h" 33 #include "tcp_var.h" 34 #include "tcpip.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 42 #include "tuba_addr.h" 43 /* 44 * Tuba initialization 45 */ 46 tuba_init() 47 { 48 #define TUBAHDRSIZE (3 /*LLC*/ + 9 /*CLNP Fixed*/ + 42 /*Addresses*/ \ 49 + 6 /*CLNP Segment*/ + 20 /*TCP*/) 50 extern struct isopcb tuba_isopcb; 51 52 tuba_isopcb.isop_next = tuba_isopcb.isop_prev = &tuba_isopcb; 53 if (max_protohdr < TUBAHDRSIZE) 54 max_protohdr = TUBAHDRSIZE; 55 if (max_linkhdr + TUBAHDRSIZE > MHLEN) 56 panic("tuba_init"); 57 tuba_timer_init(); 58 } 59 60 tuba_output(tp, m) 61 struct tcpcb *tp; 62 register struct mbuf *m; 63 { 64 struct isopcb *isop = (struct isopcb *)tp->t_tuba_pcb; 65 register struct tcpiphdr *n = tp->tp_template; 66 67 if (n->ni_sum == 0) { 68 register struct inpcb *inp = tp->tp_inpcb; 69 register struct tuba_cache *tc; 70 u_long cksum_fixup; 71 72 if ((tc = tuba_table[inp->in_faddr.s_addr]) == 0) 73 return (ENOBUFS); 74 cksum_fixup = tc->tc_sum_out; /* includes -index */ 75 if ((tc = tuba_table[inp->in_laddr.s_addr]) == 0) 76 return (ENOBUFS); 77 ICKSUM(cksum_fixup, cksum_fixup + tc->tc_sum_out); 78 n->ti_sum = cksum_fixup; 79 n = mtod(m, struct tcpiphdr *); 80 ICKSUM(n->ti_sum, cksum_fixup + n->ti_sum); 81 } 82 m->m_len -= sizeof (struct ip); 83 m->m_pkthdr.len -= sizeof (struct ip); 84 m->m_data += sizeof (struct ip); 85 return (clnp_output(m, isop, m->m_pkthdr.len, 0)); 86 } 87 88 tuba_refcnt(isop, delta) 89 struct isopcb *isop; 90 { 91 register struct tuba_cache *tc; 92 unsigned index; 93 94 if (delta != 1) 95 delta = -1; 96 if (isop == 0 || isop->isop_faddr == 0 || isop->isop_laddr == 0 || 97 (delta == -1 && isop->isop_tuba_cached == 0) || 98 (delta == 1 && isop->isop_tuba_cached != 0)) 99 return; 100 isop->isop_tuba_cached = (delta == 1); 101 if ((index = tuba_lookup(&isop->isop_faddr.siso_addr)) != 0 && 102 (tc = tuba_cache[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) 103 tc->tc_refcnt += delta; 104 if ((index = tuba_lookup(&isop->isop_laddr.siso_addr)) != 0 && 105 (tc = tuba_cache[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) 106 tc->tc_refcnt += delta; 107 } 108 109 tuba_pcbdetach(isop) 110 struct isopcb *isop; 111 { 112 if (isop == 0) 113 return; 114 tuba_refcnt(isop, -1); 115 isop->isop_socket = 0; 116 iso_pcbdetach(isop); 117 } 118 119 static struct sockaddr_iso null_siso = { sizeof(null_siso), AF_ISO, }; 120 /* 121 * Avoid in_pcbconnect in faked out tcp_input() 122 */ 123 tuba_pcbconnect(inp, nam) 124 register struct inpcb *inp; 125 struct mbuf *nam; 126 { 127 register struct sockaddr_iso *siso = mtod(m, struct sockaddr_iso *); 128 struct sockaddr_in *sin = mtod(m, struct sockaddr_in *); 129 struct tcpcb *tp = intotcpcb(inp); 130 unsigned index = sin->sin_addr.s_addr; 131 struct tuba_cache *tc = tuba_table[index]; 132 struct isopcb *isop = (struct isopcb *)tp->tp_tuba_pcb; 133 int error; 134 135 inp->inp_faddr.s_addr = index; 136 inp->inp_fport = sin->sin_port; 137 *siso = null_siso; 138 siso->siso_addr = tc->tc_addr; 139 siso->siso_tlen = sizeof(inp->inp_fport); 140 bcopy((caddr_t)&inp->inp_fport, TSEL(siso), sizeof(inp->inp_fport)); 141 nam->m_len = sizeof(*siso); 142 if ((error = iso_pcbconnect(isop, nam)) == 0) 143 tuba_refcnt(isop, 1); 144 return (error); 145 } 146 147 /* 148 * CALLED FROM: 149 * clnp's input routine, indirectly through the protosw. 150 * FUNCTION and ARGUMENTS: 151 * Take a packet (m) from clnp, strip off the clnp header and give it to tcp 152 * or udp. 153 * No return value. 154 */ 155 ProtoHook 156 tpclnp_input(m, src, dst, clnp_len, ce_bit) 157 register struct mbuf *m; 158 struct sockaddr_iso *src, *dst; 159 int clnp_len, ce_bit; 160 { 161 int s = splnet(); 162 unsigned long fix_csum, lindex, findex; 163 register struct tcpiphdr *ti; 164 register struct inpcb *inp; 165 struct mbuf *om = 0; 166 int len, tlen, off; 167 register struct tcpcb *tp = 0; 168 int tiflags; 169 struct socket *so; 170 int todrop, acked, ourfinisacked, needoutput = 0; 171 short ostate; 172 struct in_addr laddr; 173 int dropsocket = 0, iss = 0; 174 175 if ((m->m_flags & M_PKTHDR) == 0) 176 panic("tuba_input"); 177 /* 178 * Do some housekeeping looking up CLNP addresses. 179 * If we are out of space might as well drop the packet now. 180 */ 181 tcpstat.tcps_rcvtotal++; 182 if ((lindex = tuba_lookup(&dst->siso_addr) == 0 || 183 (findex = tuba_lookup(&dst->siso_addr) == 0)) 184 goto drop; 185 /* 186 * Get CLNP and TCP header together in first mbuf. 187 * CLNP gave us an mbuf chain WITH the clnp header pulled up, 188 * and the length of the clnp header. 189 */ 190 len = clnp_len + sizeof(struct tcphdr); 191 if (m->m_len < len) { 192 if ((m = m_pullup(m, len)) == 0) { 193 tcpstat.tcps_rcvshort++; 194 return; 195 } 196 } 197 /* 198 * Calculate checksum of extended TCP header and data, 199 * by adjusting the checksum for missing parts of the header. 200 */ 201 m->m_data += clnp_len; 202 m->m_len -= clnp_len; 203 tlen = m->m_pkthdr.len -= clnp_len; 204 ICKSUM(fix_cksum, tuba_table[findex]->tc_sum_in + htons((u_short)tlen) 205 + tuba_table[lindex]->tc_sum_in + in_cksum(m, tlen)); 206 if (fix_cksum != 0xffff) { 207 tcpstat.tcps_rcvbadsum++; 208 goto drop; 209 } 210 m->m_data -= sizeof(struct ip); 211 m->m_len += sizeof(struct ip); 212 m->m_pkthdr.len += sizeof(struct ip); 213 /* 214 * The reassembly code assumes it will be overwriting a useless 215 * part of the packet, which is why we need to have ti point 216 * into the packet itself. 217 * 218 * Check to see if the data is properly alligned 219 * so that we can save copying the tcp header. 220 * This code knows way too much about the structure of mbufs! 221 */ 222 off = ((sizeof (long) - 1) & ((m->m_flags & EXT) ? 223 (m->m_data - m->m_ext.ext_buf) : (m->m_data - m_pktdat))); 224 if (off) { 225 struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_DATA) 226 if (m0 == 0) { 227 goto drop; 228 } 229 bcopy(mtod(m, caddr_t) + sizeof(struct ip), 230 mtod(m0, caddr_t) + sizeof(struct ip), 231 sizeof(struct tcphdr)); 232 m->m_data += sizeof(struct tcpiphdr); 233 m0->m_next = m; 234 m0->m_pkthdr = m->m_pkthdr; 235 m0->m_flags = m->m_pkthdr & M_COPYFLAGS; 236 m0->m_len = sizeof(struct tcpiphdr); 237 m = m0; 238 } 239 ti = mtod(m, struct tcpiphdr *); 240 ti->ti_src.s_addr = findex; 241 ti->ti_dst.s_addr = lindex; 242 ti->ti_len = tlen; 243 /* 244 * Now include the rest of TCP input 245 */ 246 #define TUBA_INPUT 247 #define in_pcbconnect tuba_pcbconnect 248 249 #include "../netinet/tcp_input.c" 250 } 251