]>
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> | |
1ab1457c | 5 | * |
1da177e4 LT |
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 | ||
406ef77c | 12 | #include <linux/if_ether.h> |
36cf9acf HX |
13 | #include <linux/kernel.h> |
14 | #include <linux/module.h> | |
1da177e4 | 15 | #include <linux/skbuff.h> |
1da177e4 | 16 | #include <linux/icmpv6.h> |
16a6677f | 17 | #include <linux/netfilter_ipv6.h> |
36cf9acf | 18 | #include <net/dst.h> |
1da177e4 LT |
19 | #include <net/ipv6.h> |
20 | #include <net/xfrm.h> | |
21 | ||
aee5adb4 MN |
22 | int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, |
23 | u8 **prevhdr) | |
24 | { | |
25 | return ip6_find_1stfragopt(skb, prevhdr); | |
26 | } | |
27 | ||
7159039a YH |
28 | EXPORT_SYMBOL(xfrm6_find_1stfragopt); |
29 | ||
1da177e4 LT |
30 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) |
31 | { | |
32 | int mtu, ret = 0; | |
33 | struct dst_entry *dst = skb->dst; | |
34 | ||
35 | mtu = dst_mtu(dst); | |
36 | if (mtu < IPV6_MIN_MTU) | |
37 | mtu = IPV6_MIN_MTU; | |
38 | ||
39 | if (skb->len > mtu) { | |
180e4250 | 40 | skb->dev = dst->dev; |
1da177e4 LT |
41 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev); |
42 | ret = -EMSGSIZE; | |
43 | } | |
44 | ||
45 | return ret; | |
46 | } | |
47 | ||
36cf9acf HX |
48 | int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
49 | { | |
50 | int err; | |
51 | ||
52 | err = xfrm6_tunnel_check_size(skb); | |
53 | if (err) | |
54 | return err; | |
55 | ||
56 | return xfrm6_extract_header(skb); | |
57 | } | |
58 | ||
59 | int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb) | |
60 | { | |
61 | int err; | |
62 | ||
63 | err = x->inner_mode->afinfo->extract_output(x, skb); | |
64 | if (err) | |
65 | return err; | |
66 | ||
67 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); | |
68 | ||
69 | skb->protocol = htons(ETH_P_IPV6); | |
70 | ||
71 | return x->outer_mode->output2(x, skb); | |
72 | } | |
73 | EXPORT_SYMBOL(xfrm6_prepare_output); | |
74 | ||
406ef77c | 75 | static inline int xfrm6_output_one(struct sk_buff *skb) |
1da177e4 | 76 | { |
ceb1eec8 | 77 | struct ipv6hdr *iph; |
1da177e4 | 78 | int err; |
1ab1457c | 79 | |
406ef77c HX |
80 | err = xfrm_output(skb); |
81 | if (err) | |
82 | goto error_nolock; | |
16a6677f | 83 | |
ceb1eec8 HX |
84 | iph = ipv6_hdr(skb); |
85 | iph->payload_len = htons(skb->len - sizeof(*iph)); | |
86 | ||
3e3850e9 | 87 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
16a6677f | 88 | err = 0; |
1da177e4 LT |
89 | |
90 | out_exit: | |
91 | return err; | |
1da177e4 LT |
92 | error_nolock: |
93 | kfree_skb(skb); | |
94 | goto out_exit; | |
95 | } | |
16a6677f | 96 | |
09b8f7a9 | 97 | static int xfrm6_output_finish2(struct sk_buff *skb) |
16a6677f PM |
98 | { |
99 | int err; | |
100 | ||
101 | while (likely((err = xfrm6_output_one(skb)) == 0)) { | |
102 | nf_reset(skb); | |
1ab1457c | 103 | |
3db05fea | 104 | err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, |
16a6677f PM |
105 | skb->dst->dev, dst_output); |
106 | if (unlikely(err != 1)) | |
107 | break; | |
108 | ||
109 | if (!skb->dst->xfrm) | |
110 | return dst_output(skb); | |
111 | ||
3db05fea | 112 | err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, |
09b8f7a9 | 113 | skb->dst->dev, xfrm6_output_finish2); |
16a6677f PM |
114 | if (unlikely(err != 1)) |
115 | break; | |
116 | } | |
117 | ||
118 | return err; | |
119 | } | |
120 | ||
09b8f7a9 HX |
121 | static int xfrm6_output_finish(struct sk_buff *skb) |
122 | { | |
123 | struct sk_buff *segs; | |
124 | ||
89114afd | 125 | if (!skb_is_gso(skb)) |
09b8f7a9 HX |
126 | return xfrm6_output_finish2(skb); |
127 | ||
679e898a | 128 | skb->protocol = htons(ETH_P_IPV6); |
09b8f7a9 HX |
129 | segs = skb_gso_segment(skb, 0); |
130 | kfree_skb(skb); | |
131 | if (unlikely(IS_ERR(segs))) | |
132 | return PTR_ERR(segs); | |
133 | ||
134 | do { | |
135 | struct sk_buff *nskb = segs->next; | |
136 | int err; | |
137 | ||
138 | segs->next = NULL; | |
139 | err = xfrm6_output_finish2(segs); | |
140 | ||
141 | if (unlikely(err)) { | |
142 | while ((segs = nskb)) { | |
143 | nskb = segs->next; | |
144 | segs->next = NULL; | |
145 | kfree_skb(segs); | |
146 | } | |
147 | return err; | |
148 | } | |
149 | ||
150 | segs = nskb; | |
151 | } while (segs); | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
16a6677f PM |
156 | int xfrm6_output(struct sk_buff *skb) |
157 | { | |
158 | return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev, | |
159 | xfrm6_output_finish); | |
160 | } |