Skip to content

Commit ceb1eec

Browse files
herbertxDavid S. Miller
authored andcommitted
[IPSEC]: Move IP length/checksum setting out of transforms
This patch moves the setting of the IP length and checksum fields out of the transforms and into the xfrmX_output functions. This would help future efforts in merging the transforms themselves. It also adds an optimisation to ipcomp due to the fact that the transport offset is guaranteed to be zero. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 87bdc48 commit ceb1eec

14 files changed

Lines changed: 23 additions & 65 deletions

‎net/ipv4/ah4.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
115115
memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
116116
}
117117

118-
ip_send_check(top_iph);
119-
120118
err = 0;
121119

122120
error:

‎net/ipv4/esp4.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
1717
{
1818
int err;
19-
struct iphdr *top_iph;
2019
struct ip_esp_hdr *esph;
2120
struct crypto_blkcipher *tfm;
2221
struct blkcipher_desc desc;
@@ -59,9 +58,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
5958
pskb_put(skb, trailer, clen - skb->len);
6059

6160
skb_push(skb, -skb_network_offset(skb));
62-
top_iph = ip_hdr(skb);
6361
esph = ip_esp_hdr(skb);
64-
top_iph->tot_len = htons(skb->len + alen);
6562
*(skb_tail_pointer(trailer) - 1) = *skb_mac_header(skb);
6663
*skb_mac_header(skb) = IPPROTO_ESP;
6764

@@ -76,7 +73,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
7673
uh = (struct udphdr *)esph;
7774
uh->source = encap->encap_sport;
7875
uh->dest = encap->encap_dport;
79-
uh->len = htons(skb->len + alen - top_iph->ihl*4);
76+
uh->len = htons(skb->len + alen - skb_transport_offset(skb));
8077
uh->check = 0;
8178

8279
switch (encap->encap_type) {
@@ -136,8 +133,6 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
136133
unlock:
137134
spin_unlock_bh(&x->lock);
138135

139-
ip_send_check(top_iph);
140-
141136
error:
142137
return err;
143138
}

‎net/ipv4/ipcomp.c‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ static int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
9898
static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
9999
{
100100
struct ipcomp_data *ipcd = x->data;
101-
const int ihlen = skb_transport_offset(skb);
102-
const int plen = skb->len - ihlen;
101+
const int plen = skb->len;
103102
int dlen = IPCOMP_SCRATCH_SIZE;
104-
u8 *start = skb_transport_header(skb);
103+
u8 *start = skb->data;
105104
const int cpu = get_cpu();
106105
u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
107106
struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
@@ -118,7 +117,7 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
118117
memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
119118
put_cpu();
120119

121-
pskb_trim(skb, ihlen + dlen + sizeof(struct ip_comp_hdr));
120+
pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
122121
return 0;
123122

124123
out:
@@ -131,13 +130,8 @@ static int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
131130
int err;
132131
struct ip_comp_hdr *ipch;
133132
struct ipcomp_data *ipcd = x->data;
134-
int hdr_len = 0;
135-
struct iphdr *iph = ip_hdr(skb);
136133

137-
skb_push(skb, -skb_network_offset(skb));
138-
iph->tot_len = htons(skb->len);
139-
hdr_len = iph->ihl * 4;
140-
if ((skb->len - hdr_len) < ipcd->threshold) {
134+
if (skb->len < ipcd->threshold) {
141135
/* Don't bother compressing */
142136
goto out_ok;
143137
}
@@ -146,25 +140,19 @@ static int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
146140
goto out_ok;
147141

148142
err = ipcomp_compress(x, skb);
149-
iph = ip_hdr(skb);
150143

151144
if (err) {
152145
goto out_ok;
153146
}
154147

155148
/* Install ipcomp header, convert into ipcomp datagram. */
156-
iph->tot_len = htons(skb->len);
157149
ipch = ip_comp_hdr(skb);
158150
ipch->nexthdr = *skb_mac_header(skb);
159151
ipch->flags = 0;
160152
ipch->cpi = htons((u16 )ntohl(x->id.spi));
161153
*skb_mac_header(skb) = IPPROTO_COMP;
162-
ip_send_check(iph);
163-
return 0;
164-
165154
out_ok:
166-
if (x->props.mode == XFRM_MODE_TUNNEL)
167-
ip_send_check(iph);
155+
skb_push(skb, -skb_network_offset(skb));
168156
return 0;
169157
}
170158

‎net/ipv4/xfrm4_mode_beet.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
/* Add encapsulation header.
2121
*
2222
* The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
23-
* The following fields in it shall be filled in by x->type->output:
24-
* tot_len
25-
* check
2623
*/
2724
static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
2825
{

‎net/ipv4/xfrm4_mode_tunnel.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
3131

3232
/* Add encapsulation header.
3333
*
34-
* The top IP header will be constructed per RFC 2401. The following fields
35-
* in it shall be filled in by x->type->output:
36-
* tot_len
37-
* check
34+
* The top IP header will be constructed per RFC 2401.
3835
*/
3936
static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
4037
{

‎net/ipv4/xfrm4_output.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static inline int xfrm4_output_one(struct sk_buff *skb)
4444
{
4545
struct dst_entry *dst = skb->dst;
4646
struct xfrm_state *x = dst->xfrm;
47+
struct iphdr *iph;
4748
int err;
4849

4950
if (x->props.mode == XFRM_MODE_TUNNEL) {
@@ -56,6 +57,10 @@ static inline int xfrm4_output_one(struct sk_buff *skb)
5657
if (err)
5758
goto error_nolock;
5859

60+
iph = ip_hdr(skb);
61+
iph->tot_len = htons(skb->len);
62+
ip_send_check(iph);
63+
5964
IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
6065
err = 0;
6166

‎net/ipv4/xfrm4_tunnel.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212

1313
static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
1414
{
15-
struct iphdr *iph = ip_hdr(skb);
16-
1715
skb_push(skb, -skb_network_offset(skb));
18-
iph->tot_len = htons(skb->len);
19-
ip_send_check(iph);
20-
2116
return 0;
2217
}
2318

‎net/ipv6/esp6.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
4444
{
4545
int err;
46-
struct ipv6hdr *top_iph;
4746
struct ip_esp_hdr *esph;
4847
struct crypto_blkcipher *tfm;
4948
struct blkcipher_desc desc;
@@ -85,9 +84,7 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
8584
pskb_put(skb, trailer, clen - skb->len);
8685

8786
skb_push(skb, -skb_network_offset(skb));
88-
top_iph = ipv6_hdr(skb);
8987
esph = ip_esp_hdr(skb);
90-
top_iph->payload_len = htons(skb->len + alen - sizeof(*top_iph));
9188
*(skb_tail_pointer(trailer) - 1) = *skb_mac_header(skb);
9289
*skb_mac_header(skb) = IPPROTO_ESP;
9390

‎net/ipv6/ipcomp6.c‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,25 @@ static int ipcomp6_input(struct xfrm_state *x, struct sk_buff *skb)
119119
static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
120120
{
121121
int err;
122-
struct ipv6hdr *top_iph;
123122
struct ip_comp_hdr *ipch;
124123
struct ipcomp_data *ipcd = x->data;
125124
int plen, dlen;
126125
u8 *start, *scratch;
127126
struct crypto_comp *tfm;
128127
int cpu;
129-
int hdr_len;
130-
131-
skb_push(skb, -skb_network_offset(skb));
132-
hdr_len = skb_transport_offset(skb);
133128

134129
/* check whether datagram len is larger than threshold */
135-
if ((skb->len - hdr_len) < ipcd->threshold) {
130+
if (skb->len < ipcd->threshold) {
136131
goto out_ok;
137132
}
138133

139134
if (skb_linearize_cow(skb))
140135
goto out_ok;
141136

142137
/* compression */
143-
plen = skb->len - hdr_len;
138+
plen = skb->len;
144139
dlen = IPCOMP_SCRATCH_SIZE;
145-
start = skb_transport_header(skb);
140+
start = skb->data;
146141

147142
cpu = get_cpu();
148143
scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
@@ -155,20 +150,18 @@ static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
155150
}
156151
memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
157152
put_cpu();
158-
pskb_trim(skb, hdr_len + dlen + sizeof(struct ip_comp_hdr));
153+
pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
159154

160155
/* insert ipcomp header and replace datagram */
161-
top_iph = ipv6_hdr(skb);
162-
163-
top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
164-
165156
ipch = ip_comp_hdr(skb);
166157
ipch->nexthdr = *skb_mac_header(skb);
167158
ipch->flags = 0;
168159
ipch->cpi = htons((u16 )ntohl(x->id.spi));
169160
*skb_mac_header(skb) = IPPROTO_COMP;
170161

171162
out_ok:
163+
skb_push(skb, -skb_network_offset(skb));
164+
172165
return 0;
173166
}
174167

‎net/ipv6/mip6.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
155155

156156
skb_push(skb, -skb_network_offset(skb));
157157
iph = ipv6_hdr(skb);
158-
iph->payload_len = htons(skb->len - sizeof(*iph));
159158

160159
nexthdr = *skb_mac_header(skb);
161160
*skb_mac_header(skb) = IPPROTO_DSTOPTS;
@@ -370,7 +369,6 @@ static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
370369

371370
skb_push(skb, -skb_network_offset(skb));
372371
iph = ipv6_hdr(skb);
373-
iph->payload_len = htons(skb->len - sizeof(*iph));
374372

375373
nexthdr = *skb_mac_header(skb);
376374
*skb_mac_header(skb) = IPPROTO_ROUTING;

0 commit comments

Comments
 (0)