blob: 89eb3e05116de5a373faccaf14e0e9d7bc8a589b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _NET_TCP_ECN_H_
2#define _NET_TCP_ECN_H_ 1
3
4#include <net/inet_ecn.h>
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07005#include <net/request_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
8
9#define TCP_ECN_OK 1
10#define TCP_ECN_QUEUE_CWR 2
11#define TCP_ECN_DEMAND_CWR 4
12
13static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
14{
15 if (tp->ecn_flags&TCP_ECN_OK)
16 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
17}
18
19
20/* Output functions */
21
22static inline void TCP_ECN_send_synack(struct tcp_sock *tp,
23 struct sk_buff *skb)
24{
25 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_CWR;
26 if (!(tp->ecn_flags&TCP_ECN_OK))
27 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_ECE;
28}
29
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070030static inline void TCP_ECN_send_syn(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070032 struct tcp_sock *tp = tcp_sk(sk);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 tp->ecn_flags = 0;
Michael Chanb0da85372006-06-29 12:30:00 -070035 if (sysctl_tcp_ecn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ECE|TCPCB_FLAG_CWR;
37 tp->ecn_flags = TCP_ECN_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39}
40
41static __inline__ void
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -070042TCP_ECN_make_synack(struct request_sock *req, struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -070044 if (inet_rsk(req)->ecn_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 th->ece = 1;
46}
47
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070048static inline void TCP_ECN_send(struct sock *sk, struct sk_buff *skb,
49 int tcp_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070051 struct tcp_sock *tp = tcp_sk(sk);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (tp->ecn_flags & TCP_ECN_OK) {
54 /* Not-retransmitted data segment: set ECT and inject CWR. */
55 if (skb->len != tcp_header_len &&
56 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {
57 INET_ECN_xmit(sk);
58 if (tp->ecn_flags&TCP_ECN_QUEUE_CWR) {
59 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -070060 tcp_hdr(skb)->cwr = 1;
Herbert Xuf83ef8c2006-06-30 13:37:03 -070061 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63 } else {
64 /* ACK or retransmitted segment: clear ECT|CE */
65 INET_ECN_dontxmit(sk);
66 }
67 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR)
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -070068 tcp_hdr(skb)->ece = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70}
71
72/* Input functions */
73
74static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb)
75{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -070076 if (tcp_hdr(skb)->cwr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
78}
79
80static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
81{
82 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
83}
84
85static inline void TCP_ECN_check_ce(struct tcp_sock *tp, struct sk_buff *skb)
86{
87 if (tp->ecn_flags&TCP_ECN_OK) {
88 if (INET_ECN_is_ce(TCP_SKB_CB(skb)->flags))
89 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
90 /* Funny extension: if ECT is not set on a segment,
91 * it is surely retransmit. It is not in ECN RFC,
92 * but Linux follows this rule. */
93 else if (INET_ECN_is_not_ect((TCP_SKB_CB(skb)->flags)))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070094 tcp_enter_quickack_mode((struct sock *)tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96}
97
98static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th)
99{
100 if ((tp->ecn_flags&TCP_ECN_OK) && (!th->ece || th->cwr))
101 tp->ecn_flags &= ~TCP_ECN_OK;
102}
103
104static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th)
105{
106 if ((tp->ecn_flags&TCP_ECN_OK) && (!th->ece || !th->cwr))
107 tp->ecn_flags &= ~TCP_ECN_OK;
108}
109
110static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th)
111{
112 if (th->ece && !th->syn && (tp->ecn_flags&TCP_ECN_OK))
113 return 1;
114 return 0;
115}
116
117static inline void TCP_ECN_openreq_child(struct tcp_sock *tp,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700118 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700120 tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static __inline__ void
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700124TCP_ECN_create_request(struct request_sock *req, struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 if (sysctl_tcp_ecn && th->ece && th->cwr)
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700127 inet_rsk(req)->ecn_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130#endif