/* * Copyright (c) 1992 Regents of the University of California. * All rights reserved. * * %sccs.include.redist.c% * * @(#)tuba_subr.c 7.9 (Berkeley) 11/19/92 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static struct sockaddr_iso null_siso = { sizeof(null_siso), AF_ISO, }; extern int tuba_table_size, tcp_keepidle, tcp_keepintvl, tcp_maxidle; extern int tcppcbcachemiss, tcppredack, tcppreddat, tcprexmtthresh; extern struct tcpiphdr tcp_saveti; struct inpcb tuba_inpcb; struct inpcb *tuba_last_inpcb = &tuba_inpcb; struct isopcb tuba_isopcb; /* * Tuba initialization */ tuba_init() { #define TUBAHDRSIZE (3 /*LLC*/ + 9 /*CLNP Fixed*/ + 42 /*Addresses*/ \ + 6 /*CLNP Segment*/ + 20 /*TCP*/) tuba_inpcb.inp_next = tuba_inpcb.inp_prev = &tuba_inpcb; tuba_isopcb.isop_next = tuba_isopcb.isop_prev = &tuba_isopcb; tuba_isopcb.isop_faddr = &tuba_isopcb.isop_sfaddr; tuba_isopcb.isop_laddr = &tuba_isopcb.isop_sladdr; if (max_protohdr < TUBAHDRSIZE) max_protohdr = TUBAHDRSIZE; if (max_linkhdr + TUBAHDRSIZE > MHLEN) panic("tuba_init"); } struct addr_arg { int error; int offset; u_long sum; }; static void tuba_getaddr(arg, siso, index) register struct addr_arg *arg; struct sockaddr_iso **siso; u_long index; { register struct tuba_cache *tc; if (index <= tuba_table_size && (tc = tuba_table[index])) { if (siso) *siso = &tc->tc_siso; arg->sum += (arg->offset & 1 ? tc->tc_ssum_d : tc->tc_sum_d); arg->offset += tc->tc_siso.siso_nlen + 1; } else arg->error = 1; } tuba_output(m, tp) register struct mbuf *m; struct tcpcb *tp; { register struct tcpiphdr *n; struct isopcb *isop; struct addr_arg arg; if (tp == 0 || (n = tp->t_template) == 0 || (isop = (struct isopcb *)tp->t_tuba_pcb) == 0) { isop = &tuba_isopcb; n = mtod(m, struct tcpiphdr *); arg.error = arg.sum = arg.offset = 0; tuba_getaddr(&arg, &tuba_isopcb.isop_faddr, n->ti_dst.s_addr); tuba_getaddr(&arg, &tuba_isopcb.isop_laddr, n->ti_src.s_addr); REDUCE(arg.sum, arg.sum); goto adjust; } if (n->ti_sum == 0) { arg.error = arg.sum = arg.offset = 0; tuba_getaddr(&arg, (struct sockaddr_iso **)0, n->ti_dst.s_addr); tuba_getaddr(&arg, (struct sockaddr_iso **)0, n->ti_src.s_addr); REDUCE(arg.sum, arg.sum); n->ti_sum = arg.sum; n = mtod(m, struct tcpiphdr *); adjust: if (arg.error) { m_freem(m); return (EADDRNOTAVAIL); } REDUCE(n->ti_sum, n->ti_sum + (0xffff ^ arg.sum)); } m->m_len -= sizeof (struct ip); m->m_pkthdr.len -= sizeof (struct ip); m->m_data += sizeof (struct ip); return (clnp_output(m, isop, m->m_pkthdr.len, 0)); } tuba_refcnt(isop, delta) struct isopcb *isop; { register struct tuba_cache *tc; unsigned index, sum; if (delta != 1) delta = -1; if (isop == 0 || isop->isop_faddr == 0 || isop->isop_laddr == 0 || (delta == -1 && isop->isop_tuba_cached == 0) || (delta == 1 && isop->isop_tuba_cached != 0)) return; isop->isop_tuba_cached = (delta == 1); if ((index = tuba_lookup(&isop->isop_sfaddr.siso_addr, M_DONTWAIT)) != 0 && (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) tc->tc_refcnt += delta; if ((index = tuba_lookup(&isop->isop_sladdr.siso_addr, M_DONTWAIT)) != 0 && (tc = tuba_table[index]) != 0 && (delta == 1 || tc->tc_refcnt > 0)) tc->tc_refcnt += delta; } tuba_pcbdetach(isop) struct isopcb *isop; { if (isop == 0) return; tuba_refcnt(isop, -1); isop->isop_socket = 0; iso_pcbdetach(isop); } /* * Avoid in_pcbconnect in faked out tcp_input() */ tuba_pcbconnect(inp, nam) register struct inpcb *inp; struct mbuf *nam; { register struct sockaddr_iso *siso = mtod(nam, struct sockaddr_iso *); struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); struct tcpcb *tp = intotcpcb(inp); unsigned index = sin->sin_addr.s_addr; struct tuba_cache *tc = tuba_table[index]; struct isopcb *isop = (struct isopcb *)tp->t_tuba_pcb; int error; inp->inp_faddr.s_addr = index; inp->inp_fport = sin->sin_port; *siso = tc->tc_siso; siso->siso_tlen = sizeof(inp->inp_fport); bcopy((caddr_t)&inp->inp_fport, TSEL(siso), sizeof(inp->inp_fport)); nam->m_len = sizeof(*siso); if ((error = iso_pcbconnect(isop, nam)) == 0) tuba_refcnt(isop, 1); return (error); } /* * CALLED FROM: * clnp's input routine, indirectly through the protosw. * FUNCTION and ARGUMENTS: * Take a packet (m) from clnp, strip off the clnp header * and do tcp input processing. * No return value. */ tuba_tcpinput(m, src, dst, clnp_len, ce_bit) register struct mbuf *m; struct sockaddr_iso *src, *dst; int clnp_len, ce_bit; { unsigned long sum, lindex, findex; register struct tcpiphdr *ti; register struct inpcb *inp; struct mbuf *om; int len, tlen, off; register struct tcpcb *tp = 0; int tiflags; struct socket *so; int todrop, acked, ourfinisacked, needoutput = 0; short ostate; struct in_addr laddr; int dropsocket = 0, iss = 0; if ((m->m_flags & M_PKTHDR) == 0) { om = m_gethdr(M_DONTWAIT, MT_DATA); if (om == 0) goto drop; om->m_next = m; for (len = 0; m; m = m->m_next) len += m->m_len; m = om; m->m_pkthdr.len = len; } om = 0; /* * Do some housekeeping looking up CLNP addresses. * If we are out of space might as well drop the packet now. */ tcpstat.tcps_rcvtotal++; lindex = tuba_lookup(&dst->siso_addr, M_DONTWAIT); findex = tuba_lookup(&src->siso_addr, M_DONTWAIT); if (lindex == 0 || findex == 0) goto drop; /* * Get CLNP and TCP header together in first mbuf. * CLNP gave us an mbuf chain WITH the clnp header pulled up, * and the length of the clnp header. */ len = clnp_len + sizeof(struct tcphdr); if (m->m_len < len) { if ((m = m_pullup(m, len)) == 0) { tcpstat.tcps_rcvshort++; return; } } /* * Calculate checksum of extended TCP header and data, * by adjusting the checksum for missing parts of the header. */ m->m_data += clnp_len; m->m_len -= clnp_len; tlen = m->m_pkthdr.len -= clnp_len; m->m_data -= sizeof(struct ip); m->m_len += sizeof(struct ip); m->m_pkthdr.len += sizeof(struct ip); /* * The reassembly code assumes it will be overwriting a useless * part of the packet, which is why we need to have it point * into the packet itself. * * Check to see if the data is properly alligned * so that we can save copying the tcp header. * This code knows way too much about the structure of mbufs! */ off = ((sizeof (long) - 1) & ((m->m_flags & M_EXT) ? (m->m_data - m->m_ext.ext_buf) : (m->m_data - m->m_pktdat))); if (off) { struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_DATA); if (m0 == 0) { goto drop; } m0->m_data += max_linkhdr; bcopy(mtod(m, caddr_t) + sizeof(struct ip), mtod(m0, caddr_t) + sizeof(struct ip), sizeof(struct tcphdr)); m->m_data += sizeof(struct tcpiphdr); m->m_len -= sizeof(struct tcpiphdr); m0->m_next = m; m0->m_pkthdr = m->m_pkthdr; m0->m_flags = m->m_flags & M_COPYFLAGS; m0->m_len = sizeof(struct tcpiphdr); m = m0; } ti = mtod(m, struct tcpiphdr *); ti->ti_dst.s_addr = tuba_table[lindex]->tc_sum; if (dst->siso_nlen & 1) ti->ti_src.s_addr = tuba_table[findex]->tc_sum; else ti->ti_src.s_addr = tuba_table[findex]->tc_ssum; ti->ti_prev = ti->ti_next = 0; ti->ti_x1 = 0; ti->ti_pr = ISOPROTO_TCP; ti->ti_len = htons((u_short)tlen); if (ti->ti_sum = in_cksum(m, m->m_pkthdr.len)) { tcpstat.tcps_rcvbadsum++; goto drop; } ti->ti_src.s_addr = findex; ti->ti_dst.s_addr = lindex; /* * Now include the rest of TCP input */ #define TUBA_INCLUDE #define in_pcbconnect tuba_pcbconnect #define tcb tuba_inpcb #define tcp_last_inpcb tuba_last_inpcb #include } #define tcp_slowtimo tuba_slowtimo #define tcp_fasttimo tuba_fasttimo #include