Blackfin arch: Advertise GENERIC_GPIO and remove duplicated GENERIC_CALIBRATE_DELAY
[wrt350n-kernel.git] / net / ipv4 / xfrm4_output.c
blob44ef208a75cb175bfbff21cae090212a6d996b07
1 /*
2 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
3 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
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 */
11 #include <linux/compiler.h>
12 #include <linux/if_ether.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/spinlock.h>
16 #include <linux/netfilter_ipv4.h>
17 #include <net/ip.h>
18 #include <net/xfrm.h>
19 #include <net/icmp.h>
21 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
23 int mtu, ret = 0;
24 struct dst_entry *dst;
26 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
27 goto out;
29 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
31 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->local_df)
32 goto out;
34 dst = skb->dst;
35 mtu = dst_mtu(dst);
36 if (skb->len > mtu) {
37 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
38 ret = -EMSGSIZE;
40 out:
41 return ret;
44 static int xfrm4_output_one(struct sk_buff *skb)
46 struct dst_entry *dst = skb->dst;
47 struct xfrm_state *x = dst->xfrm;
48 int err;
50 if (skb->ip_summed == CHECKSUM_PARTIAL) {
51 err = skb_checksum_help(skb);
52 if (err)
53 goto error_nolock;
56 if (x->props.mode == XFRM_MODE_TUNNEL) {
57 err = xfrm4_tunnel_check_size(skb);
58 if (err)
59 goto error_nolock;
62 do {
63 spin_lock_bh(&x->lock);
64 err = xfrm_state_check(x, skb);
65 if (err)
66 goto error;
68 err = x->mode->output(x, skb);
69 if (err)
70 goto error;
72 err = x->type->output(x, skb);
73 if (err)
74 goto error;
76 x->curlft.bytes += skb->len;
77 x->curlft.packets++;
79 spin_unlock_bh(&x->lock);
81 if (!(skb->dst = dst_pop(dst))) {
82 err = -EHOSTUNREACH;
83 goto error_nolock;
85 dst = skb->dst;
86 x = dst->xfrm;
87 } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
89 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
90 err = 0;
92 out_exit:
93 return err;
94 error:
95 spin_unlock_bh(&x->lock);
96 error_nolock:
97 kfree_skb(skb);
98 goto out_exit;
101 static int xfrm4_output_finish2(struct sk_buff *skb)
103 int err;
105 while (likely((err = xfrm4_output_one(skb)) == 0)) {
106 nf_reset(skb);
108 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
109 skb->dst->dev, dst_output);
110 if (unlikely(err != 1))
111 break;
113 if (!skb->dst->xfrm)
114 return dst_output(skb);
116 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
117 skb->dst->dev, xfrm4_output_finish2);
118 if (unlikely(err != 1))
119 break;
122 return err;
125 static int xfrm4_output_finish(struct sk_buff *skb)
127 struct sk_buff *segs;
129 #ifdef CONFIG_NETFILTER
130 if (!skb->dst->xfrm) {
131 IPCB(skb)->flags |= IPSKB_REROUTED;
132 return dst_output(skb);
134 #endif
136 if (!skb_is_gso(skb))
137 return xfrm4_output_finish2(skb);
139 skb->protocol = htons(ETH_P_IP);
140 segs = skb_gso_segment(skb, 0);
141 kfree_skb(skb);
142 if (unlikely(IS_ERR(segs)))
143 return PTR_ERR(segs);
145 do {
146 struct sk_buff *nskb = segs->next;
147 int err;
149 segs->next = NULL;
150 err = xfrm4_output_finish2(segs);
152 if (unlikely(err)) {
153 while ((segs = nskb)) {
154 nskb = segs->next;
155 segs->next = NULL;
156 kfree_skb(segs);
158 return err;
161 segs = nskb;
162 } while (segs);
164 return 0;
167 int xfrm4_output(struct sk_buff *skb)
169 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
170 xfrm4_output_finish,
171 !(IPCB(skb)->flags & IPSKB_REROUTED));