Skip to content

Commit 37fedd3

Browse files
herbertxDavid S. Miller
authored andcommitted
[IPSEC]: Use IPv6 calling convention as the convention for x->mode->output
The IPv6 calling convention for x->mode->output is more general and could help an eventual protocol-generic x->type->output implementation. This patch adopts it for IPv4 as well and modifies the IPv4 type output functions accordingly. It also rewrites the IPv6 mac/transport header calculation to be based off the network header where practical. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7b277b1 commit 37fedd3

11 files changed

Lines changed: 44 additions & 67 deletions

‎include/net/xfrm.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ extern void xfrm_put_type(struct xfrm_type *type);
300300

301301
struct xfrm_mode {
302302
int (*input)(struct xfrm_state *x, struct sk_buff *skb);
303+
304+
/*
305+
* Add encapsulation header.
306+
*
307+
* On exit, the transport header will be set to the start of the
308+
* encapsulation header to be filled in by x->type->output and
309+
* the mac header will be set to the nextheader (protocol for
310+
* IPv4) field of the extension header directly preceding the
311+
* encapsulation header, or in its absence, that of the top IP
312+
* header. The value of the network header will always point
313+
* to the top IP header while skb->data will point to the payload.
314+
*/
303315
int (*output)(struct xfrm_state *x,struct sk_buff *skb);
304316

305317
struct module *owner;

‎net/ipv4/ah4.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
8282
goto error;
8383
}
8484

85-
ah = (struct ip_auth_hdr *)((char *)top_iph+top_iph->ihl*4);
86-
ah->nexthdr = top_iph->protocol;
85+
ah = (struct ip_auth_hdr *)skb_transport_header(skb);
86+
ah->nexthdr = *skb_mac_header(skb);
87+
*skb_mac_header(skb) = IPPROTO_AH;
8788

8889
top_iph->tos = 0;
8990
top_iph->tot_len = htons(skb->len);
9091
top_iph->frag_off = 0;
9192
top_iph->ttl = 0;
92-
top_iph->protocol = IPPROTO_AH;
9393
top_iph->check = 0;
9494

9595
ahp = x->data;

‎net/ipv4/esp4.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
6060

6161
skb_push(skb, -skb_network_offset(skb));
6262
top_iph = ip_hdr(skb);
63-
esph = (struct ip_esp_hdr *)(skb_network_header(skb) +
64-
top_iph->ihl * 4);
63+
esph = (struct ip_esp_hdr *)skb_transport_header(skb);
6564
top_iph->tot_len = htons(skb->len + alen);
66-
*(skb_tail_pointer(trailer) - 1) = top_iph->protocol;
65+
*(skb_tail_pointer(trailer) - 1) = *skb_mac_header(skb);
66+
*skb_mac_header(skb) = IPPROTO_ESP;
6767

6868
spin_lock_bh(&x->lock);
6969

@@ -91,9 +91,8 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
9191
break;
9292
}
9393

94-
top_iph->protocol = IPPROTO_UDP;
95-
} else
96-
top_iph->protocol = IPPROTO_ESP;
94+
*skb_mac_header(skb) = IPPROTO_UDP;
95+
}
9796

9897
esph->spi = x->id.spi;
9998
esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);

‎net/ipv4/ipcomp.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ 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 = ip_hdrlen(skb);
101+
const int ihlen = skb_transport_offset(skb);
102102
const int plen = skb->len - ihlen;
103103
int dlen = IPCOMP_SCRATCH_SIZE;
104-
u8 *start = skb->data + ihlen;
104+
u8 *start = skb_transport_header(skb);
105105
const int cpu = get_cpu();
106106
u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
107107
struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
@@ -154,11 +154,11 @@ static int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
154154

155155
/* Install ipcomp header, convert into ipcomp datagram. */
156156
iph->tot_len = htons(skb->len);
157-
ipch = (struct ip_comp_hdr *)((char *)iph + iph->ihl * 4);
158-
ipch->nexthdr = iph->protocol;
157+
ipch = (struct ip_comp_hdr *)skb_transport_header(skb);
158+
ipch->nexthdr = *skb_mac_header(skb);
159159
ipch->flags = 0;
160160
ipch->cpi = htons((u16 )ntohl(x->id.spi));
161-
iph->protocol = IPPROTO_COMP;
161+
*skb_mac_header(skb) = IPPROTO_COMP;
162162
ip_send_check(iph);
163163
return 0;
164164

‎net/ipv4/xfrm4_mode_beet.c‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@
2323
* The following fields in it shall be filled in by x->type->output:
2424
* tot_len
2525
* check
26-
*
27-
* On exit, skb->h will be set to the start of the payload to be processed
28-
* by x->type->output and skb->nh will be set to the top IP header.
2926
*/
3027
static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
3128
{
29+
struct ip_beet_phdr *ph;
3230
struct iphdr *iph, *top_iph;
3331
int hdrlen, optlen;
3432

3533
iph = ip_hdr(skb);
36-
skb->transport_header = skb->network_header;
3734

3835
hdrlen = 0;
3936
optlen = iph->ihl * 4 - sizeof(*iph);
@@ -42,17 +39,17 @@ static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
4239

4340
skb_set_network_header(skb, IPV4_BEET_PHMAXLEN - x->props.header_len -
4441
hdrlen);
45-
top_iph = ip_hdr(skb);
46-
skb->transport_header += sizeof(*iph) - hdrlen;
47-
__skb_pull(skb, sizeof(*iph) - hdrlen);
42+
skb->mac_header = skb->network_header +
43+
offsetof(struct iphdr, protocol);
44+
skb->transport_header = skb->network_header + sizeof(*iph);
45+
46+
ph = (struct ip_beet_phdr *)__skb_pull(skb, sizeof(*iph) - hdrlen);
4847

48+
top_iph = ip_hdr(skb);
4949
memmove(top_iph, iph, sizeof(*iph));
5050
if (unlikely(optlen)) {
51-
struct ip_beet_phdr *ph;
52-
5351
BUG_ON(optlen < 0);
5452

55-
ph = (struct ip_beet_phdr *)skb_transport_header(skb);
5653
ph->padlen = 4 - (optlen & 4);
5754
ph->hdrlen = optlen / 8;
5855
ph->nexthdr = top_iph->protocol;

‎net/ipv4/xfrm4_mode_transport.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
*
1818
* The IP header will be moved forward to make space for the encapsulation
1919
* header.
20-
*
21-
* On exit, skb->h will be set to the start of the payload to be processed
22-
* by x->type->output and skb->nh will be set to the top IP header.
2320
*/
2421
static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
2522
{
2623
struct iphdr *iph = ip_hdr(skb);
2724
int ihl = iph->ihl * 4;
2825

29-
skb->transport_header = skb->network_header + ihl;
3026
skb_set_network_header(skb, -x->props.header_len);
27+
skb->mac_header = skb->network_header +
28+
offsetof(struct iphdr, protocol);
29+
skb->transport_header = skb->network_header + ihl;
3130
__skb_pull(skb, ihl);
3231
memmove(skb_network_header(skb), iph, ihl);
3332
return 0;

‎net/ipv4/xfrm4_mode_tunnel.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
3535
* in it shall be filled in by x->type->output:
3636
* tot_len
3737
* check
38-
*
39-
* On exit, skb->h will be set to the start of the payload to be processed
40-
* by x->type->output and skb->nh will be set to the top IP header.
4138
*/
4239
static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
4340
{
@@ -47,9 +44,11 @@ static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
4744
int flags;
4845

4946
iph = ip_hdr(skb);
50-
skb->transport_header = skb->network_header;
5147

5248
skb_set_network_header(skb, -x->props.header_len);
49+
skb->mac_header = skb->network_header +
50+
offsetof(struct iphdr, protocol);
51+
skb->transport_header = skb->network_header + sizeof(*iph);
5352
top_iph = ip_hdr(skb);
5453

5554
top_iph->ihl = 5;

‎net/ipv6/xfrm6_mode_beet.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
* The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
2525
* The following fields in it shall be filled in by x->type->output:
2626
* payload_len
27-
*
28-
* On exit, skb->h will be set to the start of the encapsulation header to be
29-
* filled in by x->type->output and the mac header will be set to the
30-
* nextheader field of the extension header directly preceding the
31-
* encapsulation header, or in its absence, that of the top IP header.
32-
* The value of the network header will always point to the top IP header
33-
* while skb->data will point to the payload.
3427
*/
3528
static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
3629
{
@@ -44,7 +37,7 @@ static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
4437

4538
skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
4639
skb_set_network_header(skb, -x->props.header_len);
47-
skb_set_transport_header(skb, hdr_len - x->props.header_len);
40+
skb->transport_header = skb->network_header + hdr_len;
4841
__skb_pull(skb, hdr_len);
4942

5043
top_iph = ipv6_hdr(skb);

‎net/ipv6/xfrm6_mode_ro.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@
3737
*
3838
* The IP header and mutable extension headers will be moved forward to make
3939
* space for the route optimization header.
40-
*
41-
* On exit, skb->h will be set to the start of the encapsulation header to be
42-
* filled in by x->type->output and the mac header will be set to the
43-
* nextheader field of the extension header directly preceding the
44-
* encapsulation header, or in its absence, that of the top IP header.
45-
* The value of the network header will always point to the top IP header
46-
* while skb->data will point to the payload.
4740
*/
4841
static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
4942
{
@@ -56,7 +49,7 @@ static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
5649
hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
5750
skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
5851
skb_set_network_header(skb, -x->props.header_len);
59-
skb_set_transport_header(skb, hdr_len - x->props.header_len);
52+
skb->transport_header = skb->network_header + hdr_len;
6053
__skb_pull(skb, hdr_len);
6154
memmove(ipv6_hdr(skb), iph, hdr_len);
6255

‎net/ipv6/xfrm6_mode_transport.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
*
1919
* The IP header and mutable extension headers will be moved forward to make
2020
* space for the encapsulation header.
21-
*
22-
* On exit, skb->h will be set to the start of the encapsulation header to be
23-
* filled in by x->type->output and the mac header will be set to the
24-
* nextheader field of the extension header directly preceding the
25-
* encapsulation header, or in its absence, that of the top IP header.
26-
* The value of the network header will always point to the top IP header
27-
* while skb->data will point to the payload.
2821
*/
2922
static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
3023
{
@@ -37,7 +30,7 @@ static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
3730
hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
3831
skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
3932
skb_set_network_header(skb, -x->props.header_len);
40-
skb_set_transport_header(skb, hdr_len - x->props.header_len);
33+
skb->transport_header = skb->network_header + hdr_len;
4134
__skb_pull(skb, hdr_len);
4235
memmove(ipv6_hdr(skb), iph, hdr_len);
4336
return 0;

0 commit comments

Comments
 (0)