1 #ifndef _INET_ECN_H_
2 #define _INET_ECN_H_
3
4 #include <linux/ip.h>
5 #include <linux/skbuff.h>
6
7 #include <net/inet_sock.h>
8 #include <net/dsfield.h>
9
10 enum {
11 INET_ECN_NOT_ECT = 0,
12 INET_ECN_ECT_1 = 1,
13 INET_ECN_ECT_0 = 2,
14 INET_ECN_CE = 3,
15 INET_ECN_MASK = 3,
16 };
17
INET_ECN_is_ce(__u8 dsfield)18 static inline int INET_ECN_is_ce(__u8 dsfield)
19 {
20 return (dsfield & INET_ECN_MASK) == INET_ECN_CE;
21 }
22
INET_ECN_is_not_ect(__u8 dsfield)23 static inline int INET_ECN_is_not_ect(__u8 dsfield)
24 {
25 return (dsfield & INET_ECN_MASK) == INET_ECN_NOT_ECT;
26 }
27
INET_ECN_is_capable(__u8 dsfield)28 static inline int INET_ECN_is_capable(__u8 dsfield)
29 {
30 return dsfield & INET_ECN_ECT_0;
31 }
32
INET_ECN_encapsulate(__u8 outer,__u8 inner)33 static inline __u8 INET_ECN_encapsulate(__u8 outer, __u8 inner)
34 {
35 outer &= ~INET_ECN_MASK;
36 outer |= !INET_ECN_is_ce(inner) ? (inner & INET_ECN_MASK) :
37 INET_ECN_ECT_0;
38 return outer;
39 }
40
INET_ECN_xmit(struct sock * sk)41 static inline void INET_ECN_xmit(struct sock *sk)
42 {
43 inet_sk(sk)->tos |= INET_ECN_ECT_0;
44 if (inet6_sk(sk) != NULL)
45 inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
46 }
47
INET_ECN_dontxmit(struct sock * sk)48 static inline void INET_ECN_dontxmit(struct sock *sk)
49 {
50 inet_sk(sk)->tos &= ~INET_ECN_MASK;
51 if (inet6_sk(sk) != NULL)
52 inet6_sk(sk)->tclass &= ~INET_ECN_MASK;
53 }
54
55 #define IP6_ECN_flow_init(label) do { \
56 (label) &= ~htonl(INET_ECN_MASK << 20); \
57 } while (0)
58
59 #define IP6_ECN_flow_xmit(sk, label) do { \
60 if (INET_ECN_is_capable(inet6_sk(sk)->tclass)) \
61 (label) |= htonl(INET_ECN_ECT_0 << 20); \
62 } while (0)
63
IP_ECN_set_ce(struct iphdr * iph)64 static inline int IP_ECN_set_ce(struct iphdr *iph)
65 {
66 u32 check = (__force u32)iph->check;
67 u32 ecn = (iph->tos + 1) & INET_ECN_MASK;
68
69 /*
70 * After the last operation we have (in binary):
71 * INET_ECN_NOT_ECT => 01
72 * INET_ECN_ECT_1 => 10
73 * INET_ECN_ECT_0 => 11
74 * INET_ECN_CE => 00
75 */
76 if (!(ecn & 2))
77 return !ecn;
78
79 /*
80 * The following gives us:
81 * INET_ECN_ECT_1 => check += htons(0xFFFD)
82 * INET_ECN_ECT_0 => check += htons(0xFFFE)
83 */
84 check += (__force u16)htons(0xFFFB) + (__force u16)htons(ecn);
85
86 iph->check = (__force __sum16)(check + (check>=0xFFFF));
87 iph->tos |= INET_ECN_CE;
88 return 1;
89 }
90
IP_ECN_clear(struct iphdr * iph)91 static inline void IP_ECN_clear(struct iphdr *iph)
92 {
93 iph->tos &= ~INET_ECN_MASK;
94 }
95
ipv4_copy_dscp(unsigned int dscp,struct iphdr * inner)96 static inline void ipv4_copy_dscp(unsigned int dscp, struct iphdr *inner)
97 {
98 dscp &= ~INET_ECN_MASK;
99 ipv4_change_dsfield(inner, INET_ECN_MASK, dscp);
100 }
101
102 struct ipv6hdr;
103
IP6_ECN_set_ce(struct ipv6hdr * iph)104 static inline int IP6_ECN_set_ce(struct ipv6hdr *iph)
105 {
106 if (INET_ECN_is_not_ect(ipv6_get_dsfield(iph)))
107 return 0;
108 *(__be32*)iph |= htonl(INET_ECN_CE << 20);
109 return 1;
110 }
111
IP6_ECN_clear(struct ipv6hdr * iph)112 static inline void IP6_ECN_clear(struct ipv6hdr *iph)
113 {
114 *(__be32*)iph &= ~htonl(INET_ECN_MASK << 20);
115 }
116
ipv6_copy_dscp(unsigned int dscp,struct ipv6hdr * inner)117 static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)
118 {
119 dscp &= ~INET_ECN_MASK;
120 ipv6_change_dsfield(inner, INET_ECN_MASK, dscp);
121 }
122
INET_ECN_set_ce(struct sk_buff * skb)123 static inline int INET_ECN_set_ce(struct sk_buff *skb)
124 {
125 switch (skb->protocol) {
126 case cpu_to_be16(ETH_P_IP):
127 if (skb->network_header + sizeof(struct iphdr) <= skb->tail)
128 return IP_ECN_set_ce(ip_hdr(skb));
129 break;
130
131 case cpu_to_be16(ETH_P_IPV6):
132 if (skb->network_header + sizeof(struct ipv6hdr) <= skb->tail)
133 return IP6_ECN_set_ce(ipv6_hdr(skb));
134 break;
135 }
136
137 return 0;
138 }
139
140 #endif
141