Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm4_output.c - Common IPsec encapsulation code for IPv4. |
| 3 | * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au> |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 12 | #include <linux/if_ether.h> |
| 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 15 | #include <linux/netfilter_ipv4.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <net/ip.h> |
| 17 | #include <net/xfrm.h> |
| 18 | #include <net/icmp.h> |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static int xfrm4_tunnel_check_size(struct sk_buff *skb) |
| 21 | { |
| 22 | int mtu, ret = 0; |
| 23 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE) |
| 26 | goto out; |
| 27 | |
| 28 | IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 29 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 30 | if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->local_df) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
| 39 | out: |
| 40 | return ret; |
| 41 | } |
| 42 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 43 | static inline int xfrm4_output_one(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | struct dst_entry *dst = skb->dst; |
| 46 | struct xfrm_state *x = dst->xfrm; |
Herbert Xu | ceb1eec8 | 2007-10-10 15:45:52 -0700 | [diff] [blame] | 47 | struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | int err; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 49 | |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 50 | if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | err = xfrm4_tunnel_check_size(skb); |
| 52 | if (err) |
| 53 | goto error_nolock; |
| 54 | } |
| 55 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 56 | err = xfrm_output(skb); |
| 57 | if (err) |
| 58 | goto error_nolock; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 59 | |
Herbert Xu | ceb1eec8 | 2007-10-10 15:45:52 -0700 | [diff] [blame] | 60 | iph = ip_hdr(skb); |
| 61 | iph->tot_len = htons(skb->len); |
| 62 | ip_send_check(iph); |
| 63 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 64 | IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 65 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | out_exit: |
| 68 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | error_nolock: |
| 70 | kfree_skb(skb); |
| 71 | goto out_exit; |
| 72 | } |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 73 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 74 | static int xfrm4_output_finish2(struct sk_buff *skb) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 75 | { |
| 76 | int err; |
| 77 | |
| 78 | while (likely((err = xfrm4_output_one(skb)) == 0)) { |
| 79 | nf_reset(skb); |
| 80 | |
Herbert Xu | 3db05fea | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 81 | err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 82 | skb->dst->dev, dst_output); |
| 83 | if (unlikely(err != 1)) |
| 84 | break; |
| 85 | |
| 86 | if (!skb->dst->xfrm) |
| 87 | return dst_output(skb); |
| 88 | |
Herbert Xu | 3db05fea | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 89 | err = nf_hook(PF_INET, NF_IP_POST_ROUTING, skb, NULL, |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 90 | skb->dst->dev, xfrm4_output_finish2); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 91 | if (unlikely(err != 1)) |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | return err; |
| 96 | } |
| 97 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 98 | static int xfrm4_output_finish(struct sk_buff *skb) |
| 99 | { |
| 100 | struct sk_buff *segs; |
| 101 | |
| 102 | #ifdef CONFIG_NETFILTER |
| 103 | if (!skb->dst->xfrm) { |
| 104 | IPCB(skb)->flags |= IPSKB_REROUTED; |
| 105 | return dst_output(skb); |
| 106 | } |
| 107 | #endif |
| 108 | |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 109 | if (!skb_is_gso(skb)) |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 110 | return xfrm4_output_finish2(skb); |
| 111 | |
| 112 | skb->protocol = htons(ETH_P_IP); |
| 113 | segs = skb_gso_segment(skb, 0); |
| 114 | kfree_skb(skb); |
| 115 | if (unlikely(IS_ERR(segs))) |
| 116 | return PTR_ERR(segs); |
| 117 | |
| 118 | do { |
| 119 | struct sk_buff *nskb = segs->next; |
| 120 | int err; |
| 121 | |
| 122 | segs->next = NULL; |
| 123 | err = xfrm4_output_finish2(segs); |
| 124 | |
| 125 | if (unlikely(err)) { |
| 126 | while ((segs = nskb)) { |
| 127 | nskb = segs->next; |
| 128 | segs->next = NULL; |
| 129 | kfree_skb(segs); |
| 130 | } |
| 131 | return err; |
| 132 | } |
| 133 | |
| 134 | segs = nskb; |
| 135 | } while (segs); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 140 | int xfrm4_output(struct sk_buff *skb) |
| 141 | { |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 142 | return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev, |
| 143 | xfrm4_output_finish, |
| 144 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 145 | } |