Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * xfrm6_output.c - Common IPsec encapsulation code for IPv6. | |
3 | * Copyright (C) 2002 USAGI/WIDE Project | |
4 | * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
16a6677f | 12 | #include <linux/compiler.h> |
1da177e4 LT |
13 | #include <linux/skbuff.h> |
14 | #include <linux/spinlock.h> | |
15 | #include <linux/icmpv6.h> | |
16a6677f | 16 | #include <linux/netfilter_ipv6.h> |
1da177e4 LT |
17 | #include <net/dsfield.h> |
18 | #include <net/inet_ecn.h> | |
19 | #include <net/ipv6.h> | |
20 | #include <net/xfrm.h> | |
21 | ||
22 | /* Add encapsulation header. | |
23 | * | |
24 | * In transport mode, the IP header and mutable extension headers will be moved | |
25 | * forward to make space for the encapsulation header. | |
26 | * | |
27 | * In tunnel mode, the top IP header will be constructed per RFC 2401. | |
28 | * The following fields in it shall be filled in by x->type->output: | |
29 | * payload_len | |
30 | * | |
31 | * On exit, skb->h will be set to the start of the encapsulation header to be | |
32 | * filled in by x->type->output and skb->nh will be set to the nextheader field | |
33 | * of the extension header directly preceding the encapsulation header, or in | |
34 | * its absence, that of the top IP header. The value of skb->data will always | |
35 | * point to the top IP header. | |
36 | */ | |
37 | static void xfrm6_encap(struct sk_buff *skb) | |
38 | { | |
39 | struct dst_entry *dst = skb->dst; | |
40 | struct xfrm_state *x = dst->xfrm; | |
41 | struct ipv6hdr *iph, *top_iph; | |
42 | int dsfield; | |
43 | ||
44 | skb_push(skb, x->props.header_len); | |
45 | iph = skb->nh.ipv6h; | |
46 | ||
47 | if (!x->props.mode) { | |
48 | u8 *prevhdr; | |
49 | int hdr_len; | |
50 | ||
51 | hdr_len = ip6_find_1stfragopt(skb, &prevhdr); | |
52 | skb->nh.raw = prevhdr - x->props.header_len; | |
53 | skb->h.raw = skb->data + hdr_len; | |
54 | memmove(skb->data, iph, hdr_len); | |
55 | return; | |
56 | } | |
57 | ||
58 | skb->nh.raw = skb->data; | |
59 | top_iph = skb->nh.ipv6h; | |
60 | skb->nh.raw = &top_iph->nexthdr; | |
61 | skb->h.ipv6h = top_iph + 1; | |
62 | ||
63 | top_iph->version = 6; | |
64 | top_iph->priority = iph->priority; | |
65 | top_iph->flow_lbl[0] = iph->flow_lbl[0]; | |
66 | top_iph->flow_lbl[1] = iph->flow_lbl[1]; | |
67 | top_iph->flow_lbl[2] = iph->flow_lbl[2]; | |
68 | dsfield = ipv6_get_dsfield(top_iph); | |
69 | dsfield = INET_ECN_encapsulate(dsfield, dsfield); | |
70 | if (x->props.flags & XFRM_STATE_NOECN) | |
71 | dsfield &= ~INET_ECN_MASK; | |
72 | ipv6_change_dsfield(top_iph, 0, dsfield); | |
73 | top_iph->nexthdr = IPPROTO_IPV6; | |
74 | top_iph->hop_limit = dst_metric(dst->child, RTAX_HOPLIMIT); | |
75 | ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr); | |
76 | ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr); | |
77 | } | |
78 | ||
79 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) | |
80 | { | |
81 | int mtu, ret = 0; | |
82 | struct dst_entry *dst = skb->dst; | |
83 | ||
84 | mtu = dst_mtu(dst); | |
85 | if (mtu < IPV6_MIN_MTU) | |
86 | mtu = IPV6_MIN_MTU; | |
87 | ||
88 | if (skb->len > mtu) { | |
180e4250 | 89 | skb->dev = dst->dev; |
1da177e4 LT |
90 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev); |
91 | ret = -EMSGSIZE; | |
92 | } | |
93 | ||
94 | return ret; | |
95 | } | |
96 | ||
16a6677f | 97 | static int xfrm6_output_one(struct sk_buff *skb) |
1da177e4 LT |
98 | { |
99 | struct dst_entry *dst = skb->dst; | |
100 | struct xfrm_state *x = dst->xfrm; | |
101 | int err; | |
102 | ||
103 | if (skb->ip_summed == CHECKSUM_HW) { | |
104 | err = skb_checksum_help(skb, 0); | |
105 | if (err) | |
106 | goto error_nolock; | |
107 | } | |
108 | ||
109 | if (x->props.mode) { | |
110 | err = xfrm6_tunnel_check_size(skb); | |
111 | if (err) | |
112 | goto error_nolock; | |
113 | } | |
114 | ||
16a6677f PM |
115 | do { |
116 | spin_lock_bh(&x->lock); | |
117 | err = xfrm_state_check(x, skb); | |
118 | if (err) | |
119 | goto error; | |
1da177e4 | 120 | |
16a6677f | 121 | xfrm6_encap(skb); |
1da177e4 | 122 | |
16a6677f PM |
123 | err = x->type->output(x, skb); |
124 | if (err) | |
125 | goto error; | |
1da177e4 | 126 | |
16a6677f PM |
127 | x->curlft.bytes += skb->len; |
128 | x->curlft.packets++; | |
1da177e4 | 129 | |
16a6677f | 130 | spin_unlock_bh(&x->lock); |
1da177e4 | 131 | |
16a6677f PM |
132 | skb->nh.raw = skb->data; |
133 | ||
134 | if (!(skb->dst = dst_pop(dst))) { | |
135 | err = -EHOSTUNREACH; | |
136 | goto error_nolock; | |
137 | } | |
138 | dst = skb->dst; | |
139 | x = dst->xfrm; | |
140 | } while (x && !x->props.mode); | |
141 | ||
142 | err = 0; | |
1da177e4 LT |
143 | |
144 | out_exit: | |
145 | return err; | |
146 | error: | |
147 | spin_unlock_bh(&x->lock); | |
148 | error_nolock: | |
149 | kfree_skb(skb); | |
150 | goto out_exit; | |
151 | } | |
16a6677f PM |
152 | |
153 | static int xfrm6_output_finish(struct sk_buff *skb) | |
154 | { | |
155 | int err; | |
156 | ||
157 | while (likely((err = xfrm6_output_one(skb)) == 0)) { | |
158 | nf_reset(skb); | |
159 | ||
160 | err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, &skb, NULL, | |
161 | skb->dst->dev, dst_output); | |
162 | if (unlikely(err != 1)) | |
163 | break; | |
164 | ||
165 | if (!skb->dst->xfrm) | |
166 | return dst_output(skb); | |
167 | ||
168 | err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, &skb, NULL, | |
169 | skb->dst->dev, xfrm6_output_finish); | |
170 | if (unlikely(err != 1)) | |
171 | break; | |
172 | } | |
173 | ||
174 | return err; | |
175 | } | |
176 | ||
177 | int xfrm6_output(struct sk_buff *skb) | |
178 | { | |
179 | return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev, | |
180 | xfrm6_output_finish); | |
181 | } |