This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / ipv4 / xfrm4_output.c
1 /*
2  * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
3  * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  */
10
11 #include <linux/skbuff.h>
12 #include <linux/spinlock.h>
13 #include <net/inet_ecn.h>
14 #include <net/ip.h>
15 #include <net/xfrm.h>
16
17 /* Add encapsulation header.
18  *
19  * In transport mode, the IP header will be moved forward to make space
20  * for the encapsulation header.
21  *
22  * In tunnel mode, the top IP header will be constructed per RFC 2401.
23  * The following fields in it shall be filled in by x->type->output:
24  *      tot_len
25  *      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.
29  */
30 static void xfrm4_encap(struct sk_buff *skb)
31 {
32         struct dst_entry *dst = skb->dst;
33         struct xfrm_state *x = dst->xfrm;
34         struct iphdr *iph, *top_iph;
35
36         iph = skb->nh.iph;
37         skb->h.ipiph = iph;
38
39         skb->nh.raw = skb_push(skb, x->props.header_len);
40         top_iph = skb->nh.iph;
41
42         if (!x->props.mode) {
43                 skb->h.raw += iph->ihl*4;
44                 memmove(top_iph, iph, iph->ihl*4);
45                 return;
46         }
47
48         top_iph->ihl = 5;
49         top_iph->version = 4;
50
51         /* DS disclosed */
52         top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
53         if (x->props.flags & XFRM_STATE_NOECN)
54                 IP_ECN_clear(top_iph);
55
56         top_iph->frag_off = iph->frag_off & htons(IP_DF);
57         if (!top_iph->frag_off)
58                 __ip_select_ident(top_iph, dst, 0);
59
60         /* TTL disclosed */
61         top_iph->ttl = iph->ttl;
62
63         top_iph->saddr = x->props.saddr.a4;
64         top_iph->daddr = x->id.daddr.a4;
65         top_iph->protocol = IPPROTO_IPIP;
66
67         memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
68 }
69
70 int xfrm4_output(struct sk_buff **pskb)
71 {
72         struct sk_buff *skb = *pskb;
73         struct dst_entry *dst = skb->dst;
74         struct xfrm_state *x = dst->xfrm;
75         int err;
76         
77         if (skb->ip_summed == CHECKSUM_HW) {
78                 err = skb_checksum_help(pskb, 0);
79                 skb = *pskb;
80                 if (err)
81                         goto error_nolock;
82         }
83
84         spin_lock_bh(&x->lock);
85         err = xfrm_state_check(x, skb);
86         if (err)
87                 goto error;
88
89         if (x->props.mode) {
90                 err = xfrm4_tunnel_check_size(skb);
91                 if (err)
92                         goto error;
93         }
94
95         xfrm4_encap(skb);
96
97         err = x->type->output(pskb);
98         skb = *pskb;
99         if (err)
100                 goto error;
101
102         x->curlft.bytes += skb->len;
103         x->curlft.packets++;
104
105         spin_unlock_bh(&x->lock);
106         
107         if (!(skb->dst = dst_pop(dst))) {
108                 err = -EHOSTUNREACH;
109                 goto error_nolock;
110         }
111         err = NET_XMIT_BYPASS;
112
113 out_exit:
114         return err;
115 error:
116         spin_unlock_bh(&x->lock);
117 error_nolock:
118         kfree_skb(skb);
119         goto out_exit;
120 }