Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm4_mode_tunnel.c - Tunnel mode encapsulation for IPv4. |
| 3 | * |
| 4 | * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <linux/stringify.h> |
| 12 | #include <net/dst.h> |
| 13 | #include <net/inet_ecn.h> |
| 14 | #include <net/ip.h> |
| 15 | #include <net/xfrm.h> |
| 16 | |
| 17 | static inline void ipip_ecn_decapsulate(struct sk_buff *skb) |
| 18 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 19 | struct iphdr *outer_iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | b0061ce | 2007-04-25 18:02:22 -0700 | [diff] [blame] | 20 | struct iphdr *inner_iph = ipip_hdr(skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 21 | |
| 22 | if (INET_ECN_is_ce(outer_iph->tos)) |
| 23 | IP_ECN_set_ce(inner_iph); |
| 24 | } |
| 25 | |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 26 | static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb) |
| 27 | { |
| 28 | if (INET_ECN_is_ce(iph->tos)) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 29 | IP6_ECN_set_ce(ipv6_hdr(skb)); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 32 | /* Add encapsulation header. |
| 33 | * |
Herbert Xu | ceb1eec | 2007-10-10 15:45:52 -0700 | [diff] [blame] | 34 | * The top IP header will be constructed per RFC 2401. |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 35 | */ |
Jamal Hadi Salim | eb878e8 | 2006-08-31 17:42:59 -0700 | [diff] [blame] | 36 | static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb) |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 37 | { |
| 38 | struct dst_entry *dst = skb->dst; |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 39 | struct iphdr *top_iph; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 40 | int flags; |
| 41 | |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 42 | skb_set_network_header(skb, -x->props.header_len); |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame] | 43 | skb->mac_header = skb->network_header + |
| 44 | offsetof(struct iphdr, protocol); |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 45 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 46 | top_iph = ip_hdr(skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 47 | |
| 48 | top_iph->ihl = 5; |
| 49 | top_iph->version = 4; |
| 50 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 51 | top_iph->protocol = x->inner_mode->afinfo->proto; |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 52 | |
| 53 | /* DS disclosed */ |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 54 | top_iph->tos = INET_ECN_encapsulate(XFRM_MODE_SKB_CB(skb)->tos, |
| 55 | XFRM_MODE_SKB_CB(skb)->tos); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 56 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 57 | flags = x->props.flags; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 58 | if (flags & XFRM_STATE_NOECN) |
| 59 | IP_ECN_clear(top_iph); |
| 60 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 61 | top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ? |
| 62 | 0 : XFRM_MODE_SKB_CB(skb)->frag_off; |
| 63 | ip_select_ident(top_iph, dst->child, NULL); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 64 | |
| 65 | top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT); |
| 66 | |
| 67 | top_iph->saddr = x->props.saddr.a4; |
| 68 | top_iph->daddr = x->id.daddr.a4; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 69 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) |
| 74 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 75 | struct iphdr *iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 39f69c6 | 2007-03-10 12:40:27 -0300 | [diff] [blame] | 76 | const unsigned char *old_mac; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 77 | int err = -EINVAL; |
| 78 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 79 | switch (iph->protocol){ |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 80 | case IPPROTO_IPIP: |
Patrick McHardy | e2e01bf | 2007-02-12 20:27:10 -0800 | [diff] [blame] | 81 | break; |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 82 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 83 | case IPPROTO_IPV6: |
| 84 | break; |
| 85 | #endif |
| 86 | default: |
| 87 | goto out; |
| 88 | } |
| 89 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 90 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 91 | goto out; |
| 92 | |
| 93 | if (skb_cloned(skb) && |
| 94 | (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) |
| 95 | goto out; |
| 96 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 97 | iph = ip_hdr(skb); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 98 | if (iph->protocol == IPPROTO_IPIP) { |
| 99 | if (x->props.flags & XFRM_STATE_DECAP_DSCP) |
Herbert Xu | 29bb43b | 2007-11-13 21:40:13 -0800 | [diff] [blame] | 100 | ipv4_copy_dscp(ipv4_get_dsfield(iph), ipip_hdr(skb)); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 101 | if (!(x->props.flags & XFRM_STATE_NOECN)) |
| 102 | ipip_ecn_decapsulate(skb); |
| 103 | } |
| 104 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 105 | else { |
| 106 | if (!(x->props.flags & XFRM_STATE_NOECN)) |
| 107 | ipip6_ecn_decapsulate(iph, skb); |
| 108 | skb->protocol = htons(ETH_P_IPV6); |
| 109 | } |
| 110 | #endif |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 111 | old_mac = skb_mac_header(skb); |
Arnaldo Carvalho de Melo | 39f69c6 | 2007-03-10 12:40:27 -0300 | [diff] [blame] | 112 | skb_set_mac_header(skb, -skb->mac_len); |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 113 | memmove(skb_mac_header(skb), old_mac, skb->mac_len); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 114 | skb_reset_network_header(skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 115 | err = 0; |
| 116 | |
| 117 | out: |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | static struct xfrm_mode xfrm4_tunnel_mode = { |
| 122 | .input = xfrm4_tunnel_input, |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame^] | 123 | .output2 = xfrm4_tunnel_output, |
| 124 | .output = xfrm4_prepare_output, |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 125 | .owner = THIS_MODULE, |
| 126 | .encap = XFRM_MODE_TUNNEL, |
Herbert Xu | 1bfcb10 | 2007-10-17 21:31:50 -0700 | [diff] [blame] | 127 | .flags = XFRM_MODE_FLAG_TUNNEL, |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static int __init xfrm4_tunnel_init(void) |
| 131 | { |
| 132 | return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET); |
| 133 | } |
| 134 | |
| 135 | static void __exit xfrm4_tunnel_exit(void) |
| 136 | { |
| 137 | int err; |
| 138 | |
| 139 | err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET); |
| 140 | BUG_ON(err); |
| 141 | } |
| 142 | |
| 143 | module_init(xfrm4_tunnel_init); |
| 144 | module_exit(xfrm4_tunnel_exit); |
| 145 | MODULE_LICENSE("GPL"); |
| 146 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL); |