xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 5068)
1*5068Swnj /* tcp_subr.c 4.1 81/11/24 */
2*5068Swnj 
3*5068Swnj #include "../h/param.h"
4*5068Swnj #include "../h/systm.h"
5*5068Swnj #include "../h/mbuf.h"
6*5068Swnj #include "../h/socket.h"
7*5068Swnj #include "../h/socketvar.h"
8*5068Swnj #include "../h/protosw.h"
9*5068Swnj #include "../net/inet.h"
10*5068Swnj #include "../net/inet_pcb.h"
11*5068Swnj #include "../net/inet_systm.h"
12*5068Swnj #include "../net/if.h"
13*5068Swnj #include "../net/imp.h"
14*5068Swnj #include "../net/ip.h"
15*5068Swnj #include "../net/ip_var.h"
16*5068Swnj #include "../net/tcp.h"
17*5068Swnj #define TCPFSTAB
18*5068Swnj #include "../net/tcp_fsm.h"
19*5068Swnj #include "../net/tcp_var.h"
20*5068Swnj #include "/usr/include/errno.h"
21*5068Swnj 
22*5068Swnj /*
23*5068Swnj  * Tcp initialization
24*5068Swnj  */
25*5068Swnj tcp_init()
26*5068Swnj {
27*5068Swnj 
28*5068Swnj 	tcp_iss = 1;		/* wrong */
29*5068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
30*5068Swnj }
31*5068Swnj 
32*5068Swnj /*
33*5068Swnj  * Create template to be used to send tcp packets on a connection.
34*5068Swnj  * Call after host entry created, allocates an mbuf and fills
35*5068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
36*5068Swnj  * necessary when the connection is used.
37*5068Swnj  */
38*5068Swnj struct tcpiphdr *
39*5068Swnj tcp_template(tp)
40*5068Swnj 	struct tcpcb *tp;
41*5068Swnj {
42*5068Swnj 	register struct inpcb *inp = tp->t_inpcb;
43*5068Swnj 	register struct mbuf *m;
44*5068Swnj 	register struct tcpiphdr *n;
45*5068Swnj 
46*5068Swnj COUNT(TCP_TEMPLATE);
47*5068Swnj 	m = m_get(1);
48*5068Swnj 	if (m == 0)
49*5068Swnj 		return (0);
50*5068Swnj 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
51*5068Swnj 	m->m_len = sizeof (struct tcpiphdr);
52*5068Swnj 	n = mtod(m, struct tcpiphdr *);
53*5068Swnj 	n->ti_next = n->ti_prev = 0;
54*5068Swnj 	n->ti_x1 = 0;
55*5068Swnj 	n->ti_pr = IPPROTO_TCP;
56*5068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
57*5068Swnj 	n->ti_src = inp->inp_laddr;
58*5068Swnj 	n->ti_dst = inp->inp_faddr;
59*5068Swnj 	n->ti_sport = inp->inp_lport;
60*5068Swnj 	n->ti_dport = inp->inp_fport;
61*5068Swnj 	n->ti_seq = 0;
62*5068Swnj 	n->ti_ackno = 0;
63*5068Swnj 	n->ti_x2 = 0;
64*5068Swnj 	n->ti_off = 5;
65*5068Swnj 	n->ti_flags = 0;
66*5068Swnj 	n->ti_win = 0;
67*5068Swnj 	n->ti_sum = 0;
68*5068Swnj 	n->ti_urp = 0;
69*5068Swnj 	return (n);
70*5068Swnj }
71*5068Swnj 
72*5068Swnj /*
73*5068Swnj  * Reflect a control message back to sender of tcp segment ti,
74*5068Swnj  * with ack, seq and flags fields as specified by parameters.
75*5068Swnj  */
76*5068Swnj tcp_reflect(ti, ack, seq, flags)
77*5068Swnj 	register struct tcpiphdr *ti;
78*5068Swnj 	tcpseq_t ack, seq;
79*5068Swnj 	int flags;
80*5068Swnj {
81*5068Swnj 
82*5068Swnj 	m_freem(m->m_next);
83*5068Swnj 	m->m_next = 0;
84*5068Swnj 	m->m_len = sizeof(struct tcpiphdr);
85*5068Swnj #define xchg(a,b) j=a; a=b; b=j
86*5068Swnj 	xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr);
87*5068Swnj 	xchg(ti->ti_dport, ti->ti_sport);
88*5068Swnj #undef xchg
89*5068Swnj 	ti->ti_ack = htonl(ack);
90*5068Swnj 	ti->ti_seq = htonl(seq);
91*5068Swnj 	ti->ti_flags = flags;
92*5068Swnj 
93*5068Swnj 	ti->ti_len = htons(sizeof (struct tcphdr));
94*5068Swnj 	ti->ti_off = 5;
95*5068Swnj 	ti->ti_sum = inet_cksum(m, sizeof(struct tcpiphdr));
96*5068Swnj 	((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr);
97*5068Swnj 	((struct ip *)ti)->ip_ttl = MAXTTL;
98*5068Swnj 	ip_output(m);
99*5068Swnj }
100