3 * Politecnico di Torino. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the Politecnico
13 * di Torino, and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include "tcp_session.h"
30 #include <net/tme/tme.h>
31 #include <net/tme/tcp_session.h>
34 #include <tme/tcp_session.h>
39 uint32
tcp_session(uint8
*block
, uint32 pkt_size
, TME_DATA
*data
, MEM_TYPE
*mem_ex
, uint8
*mem_data
)
44 uint32 direction
=ULONG_AT(mem_data
,12);
45 uint8 flags
=mem_ex
->buffer
[25];
46 tcp_data
*session
=(tcp_data
*)(block
+data
->key_len
*4);
48 session
->last_timestamp
=session
->timestamp_block
;
49 session
->timestamp_block
.tv_sec
=0x7fffffff;
51 if (direction
==session
->direction
)
53 session
->pkts_cln_to_srv
++;
54 session
->bytes_cln_to_srv
+=pkt_size
;
58 session
->pkts_srv_to_cln
++;
59 session
->bytes_srv_to_cln
+=pkt_size
;
61 /* we use only thes four flags, we don't need PSH or URG */
62 flags
&=(ACK
|FIN
|SYN
|RST
);
64 switch (session
->status
)
67 next_status
=ERROR_TCP
;
73 if (SW_ULONG_AT(mem_ex
->buffer
,20)!=0)
76 next_status
=ERROR_TCP
;
80 session
->syn_timestamp
=session
->last_timestamp
;
82 session
->direction
=direction
;
83 session
->seq_n_0_cln
=SW_ULONG_AT(mem_ex
->buffer
,16);
90 if ((flags
&RST
)&&(direction
!=session
->direction
))
92 next_status
=CLOSED_RST
;
95 if ((flags
==SYN
)&&(direction
==session
->direction
))
98 session
->seq_n_0_cln
=SW_ULONG_AT(mem_ex
->buffer
,16);
102 if ((flags
==(SYN
|ACK
))&&(direction
!=session
->direction
))
104 if (SW_ULONG_AT(mem_ex
->buffer
,20)!=session
->seq_n_0_cln
+1)
106 next_status
=ERROR_TCP
;
109 next_status
=SYN_ACK_RCV
;
111 session
->syn_ack_timestamp
=session
->last_timestamp
;
113 session
->seq_n_0_srv
=SW_ULONG_AT(mem_ex
->buffer
,16);
114 session
->ack_cln
=session
->seq_n_0_cln
+1;
118 next_status
=ERROR_TCP
;
123 if ((flags
&ACK
)&&(flags
&RST
)&&(direction
==session
->direction
))
125 next_status
=CLOSED_RST
;
126 session
->ack_srv
=SW_ULONG_AT(mem_ex
->buffer
,20);
130 if ((flags
==ACK
)&&(!(flags
&(SYN
|FIN
|RST
)))&&(direction
==session
->direction
))
132 if (SW_ULONG_AT(mem_ex
->buffer
,20)!=session
->seq_n_0_srv
+1)
134 next_status
=ERROR_TCP
;
137 next_status
=ESTABLISHED
;
138 session
->ack_srv
=session
->seq_n_0_srv
+1;
141 if ((flags
&ACK
)&&(flags
&SYN
)&&(direction
!=session
->direction
))
143 next_status
=SYN_ACK_RCV
;
147 next_status
=ERROR_TCP
;
154 (direction
!=session
->direction
)&&
155 ((session
->ack_cln
-SW_ULONG_AT(mem_ex
->buffer
,20))<MAX_WINDOW
)
157 { /* SYN_ACK duplicato */
158 next_status
=ESTABLISHED
;
163 (direction
==session
->direction
)&&
164 (SW_ULONG_AT(mem_ex
->buffer
,16)==session
->seq_n_0_cln
)&&
165 (ULONG_AT(mem_ex
->buffer
,20)==0)
167 { /* syn duplicato */
168 next_status
=ESTABLISHED
;
172 next_status
=ERROR_TCP
;
177 if (direction
==session
->direction
)
179 uint32 new_ack
=SW_ULONG_AT(mem_ex
->buffer
,20);
180 if (new_ack
-session
->ack_srv
<MAX_WINDOW
)
181 session
->ack_srv
=new_ack
;
185 uint32 new_ack
=SW_ULONG_AT(mem_ex
->buffer
,20);
186 if (new_ack
-session
->ack_cln
<MAX_WINDOW
)
187 session
->ack_cln
=new_ack
;
192 next_status
=CLOSED_RST
;
197 if (direction
==session
->direction
)
198 { /* an hack to make all things work */
199 session
->ack_cln
=SW_ULONG_AT(mem_ex
->buffer
,16);
200 next_status
=FIN_CLN_RCV
;
205 session
->ack_srv
=SW_ULONG_AT(mem_ex
->buffer
,16);
206 next_status
=FIN_SRV_RCV
;
210 next_status
=ESTABLISHED
;
214 next_status
=CLOSED_RST
;
220 next_status
=ERROR_TCP
;
224 next_status
=FIN_SRV_RCV
;
228 uint32 new_ack
=SW_ULONG_AT(mem_ex
->buffer
,20);
229 if (direction
!=session
->direction
)
230 if ((new_ack
-session
->ack_cln
)<MAX_WINDOW
)
231 session
->ack_cln
=new_ack
;
235 next_status
=CLOSED_RST
;
237 if ((flags
&FIN
)&&(direction
==session
->direction
))
239 session
->ack_cln
=SW_ULONG_AT(mem_ex
->buffer
,16);
240 next_status
=CLOSED_FIN
;
248 next_status
=ERROR_TCP
;
252 next_status
=FIN_CLN_RCV
;
256 uint32 new_ack
=SW_ULONG_AT(mem_ex
->buffer
,20);
257 if (direction
==session
->direction
)
258 if (new_ack
-session
->ack_srv
<MAX_WINDOW
)
259 session
->ack_srv
=new_ack
;
263 next_status
=CLOSED_RST
;
265 if ((flags
&FIN
)&&(direction
!=session
->direction
))
267 session
->ack_srv
=SW_ULONG_AT(mem_ex
->buffer
,16);
268 next_status
=CLOSED_FIN
;
274 next_status
=CLOSED_FIN
;
277 next_status
=ERROR_TCP
;
281 session
->status
=next_status
;
283 if ((next_status
==CLOSED_FIN
)||(next_status
==UNKNOWN
)||(next_status
==CLOSED_RST
)||(next_status
==ERROR_TCP
))
284 session
->timestamp_block
=session
->last_timestamp
;