blob: ac9d91d4bb0563c2e471bfa5483c901ac38bac64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Patrick McHardy16a66772006-01-06 23:01:48 -080011#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/skbuff.h>
13#include <linux/spinlock.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080014#include <linux/netfilter_ipv4.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/ip.h>
16#include <net/xfrm.h>
17#include <net/icmp.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static int xfrm4_tunnel_check_size(struct sk_buff *skb)
20{
21 int mtu, ret = 0;
22 struct dst_entry *dst;
23 struct iphdr *iph = skb->nh.iph;
24
25 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
26 goto out;
27
28 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
29
30 if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
31 goto out;
32
33 dst = skb->dst;
34 mtu = dst_mtu(dst);
35 if (skb->len > mtu) {
36 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
37 ret = -EMSGSIZE;
38 }
39out:
40 return ret;
41}
42
Patrick McHardy16a66772006-01-06 23:01:48 -080043static int xfrm4_output_one(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct dst_entry *dst = skb->dst;
46 struct xfrm_state *x = dst->xfrm;
47 int err;
48
49 if (skb->ip_summed == CHECKSUM_HW) {
50 err = skb_checksum_help(skb, 0);
51 if (err)
52 goto error_nolock;
53 }
54
55 if (x->props.mode) {
56 err = xfrm4_tunnel_check_size(skb);
57 if (err)
58 goto error_nolock;
59 }
60
Patrick McHardy16a66772006-01-06 23:01:48 -080061 do {
62 spin_lock_bh(&x->lock);
63 err = xfrm_state_check(x, skb);
64 if (err)
65 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Herbert Xub59f45d2006-05-27 23:05:54 -070067 err = x->mode->output(skb);
68 if (err)
69 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardy16a66772006-01-06 23:01:48 -080071 err = x->type->output(x, skb);
72 if (err)
73 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Patrick McHardy16a66772006-01-06 23:01:48 -080075 x->curlft.bytes += skb->len;
76 x->curlft.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Patrick McHardy16a66772006-01-06 23:01:48 -080078 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Patrick McHardy16a66772006-01-06 23:01:48 -080080 if (!(skb->dst = dst_pop(dst))) {
81 err = -EHOSTUNREACH;
82 goto error_nolock;
83 }
84 dst = skb->dst;
85 x = dst->xfrm;
86 } while (x && !x->props.mode);
87
Patrick McHardy3e3850e2006-01-06 23:04:54 -080088 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Patrick McHardy16a66772006-01-06 23:01:48 -080089 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91out_exit:
92 return err;
93error:
94 spin_unlock_bh(&x->lock);
95error_nolock:
96 kfree_skb(skb);
97 goto out_exit;
98}
Patrick McHardy16a66772006-01-06 23:01:48 -080099
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800100static int xfrm4_output_finish(struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800101{
102 int err;
103
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800104#ifdef CONFIG_NETFILTER
105 if (!skb->dst->xfrm) {
106 IPCB(skb)->flags |= IPSKB_REROUTED;
107 return dst_output(skb);
108 }
109#endif
Patrick McHardy16a66772006-01-06 23:01:48 -0800110 while (likely((err = xfrm4_output_one(skb)) == 0)) {
111 nf_reset(skb);
112
113 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
114 skb->dst->dev, dst_output);
115 if (unlikely(err != 1))
116 break;
117
118 if (!skb->dst->xfrm)
119 return dst_output(skb);
120
121 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
122 skb->dst->dev, xfrm4_output_finish);
123 if (unlikely(err != 1))
124 break;
125 }
126
127 return err;
128}
129
130int xfrm4_output(struct sk_buff *skb)
131{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800132 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
133 xfrm4_output_finish,
134 !(IPCB(skb)->flags & IPSKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800135}