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 | * |
| 34 | * The top IP header will be constructed per RFC 2401. The following fields |
| 35 | * in it shall be filled in by x->type->output: |
| 36 | * tot_len |
| 37 | * check |
| 38 | * |
| 39 | * On exit, skb->h will be set to the start of the payload to be processed |
| 40 | * by x->type->output and skb->nh will be set to the top IP header. |
| 41 | */ |
Jamal Hadi Salim | eb878e8 | 2006-08-31 17:42:59 -0700 | [diff] [blame] | 42 | 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] | 43 | { |
| 44 | struct dst_entry *dst = skb->dst; |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 45 | struct xfrm_dst *xdst = (struct xfrm_dst*)dst; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 46 | struct iphdr *iph, *top_iph; |
| 47 | int flags; |
| 48 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 49 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 50 | skb->transport_header = skb->network_header; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 51 | |
Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 52 | skb_push(skb, x->props.header_len); |
| 53 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 54 | top_iph = ip_hdr(skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 55 | |
| 56 | top_iph->ihl = 5; |
| 57 | top_iph->version = 4; |
| 58 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 59 | flags = x->props.flags; |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 60 | |
| 61 | /* DS disclosed */ |
| 62 | if (xdst->route->ops->family == AF_INET) { |
| 63 | top_iph->protocol = IPPROTO_IPIP; |
| 64 | top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos); |
| 65 | top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ? |
| 66 | 0 : (iph->frag_off & htons(IP_DF)); |
| 67 | } |
| 68 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 69 | else { |
| 70 | struct ipv6hdr *ipv6h = (struct ipv6hdr*)iph; |
| 71 | top_iph->protocol = IPPROTO_IPV6; |
| 72 | top_iph->tos = INET_ECN_encapsulate(iph->tos, ipv6_get_dsfield(ipv6h)); |
| 73 | top_iph->frag_off = 0; |
| 74 | } |
| 75 | #endif |
| 76 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 77 | if (flags & XFRM_STATE_NOECN) |
| 78 | IP_ECN_clear(top_iph); |
| 79 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 80 | if (!top_iph->frag_off) |
| 81 | __ip_select_ident(top_iph, dst->child, 0); |
| 82 | |
| 83 | top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT); |
| 84 | |
| 85 | top_iph->saddr = x->props.saddr.a4; |
| 86 | top_iph->daddr = x->id.daddr.a4; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 87 | |
Kazunori MIYAZAWA | f282d45 | 2007-05-29 13:03:17 -0700 | [diff] [blame] | 88 | skb->protocol = htons(ETH_P_IP); |
| 89 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 90 | memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options)); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) |
| 95 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 96 | struct iphdr *iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 39f69c6 | 2007-03-10 12:40:27 -0300 | [diff] [blame] | 97 | const unsigned char *old_mac; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 98 | int err = -EINVAL; |
| 99 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 100 | switch (iph->protocol){ |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 101 | case IPPROTO_IPIP: |
Patrick McHardy | e2e01bf | 2007-02-12 20:27:10 -0800 | [diff] [blame] | 102 | break; |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 103 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 104 | case IPPROTO_IPV6: |
| 105 | break; |
| 106 | #endif |
| 107 | default: |
| 108 | goto out; |
| 109 | } |
| 110 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 111 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 112 | goto out; |
| 113 | |
| 114 | if (skb_cloned(skb) && |
| 115 | (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) |
| 116 | goto out; |
| 117 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 118 | iph = ip_hdr(skb); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 119 | if (iph->protocol == IPPROTO_IPIP) { |
| 120 | if (x->props.flags & XFRM_STATE_DECAP_DSCP) |
Arnaldo Carvalho de Melo | b0061ce | 2007-04-25 18:02:22 -0700 | [diff] [blame] | 121 | ipv4_copy_dscp(iph, ipip_hdr(skb)); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 122 | if (!(x->props.flags & XFRM_STATE_NOECN)) |
| 123 | ipip_ecn_decapsulate(skb); |
| 124 | } |
| 125 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 126 | else { |
| 127 | if (!(x->props.flags & XFRM_STATE_NOECN)) |
| 128 | ipip6_ecn_decapsulate(iph, skb); |
| 129 | skb->protocol = htons(ETH_P_IPV6); |
| 130 | } |
| 131 | #endif |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 132 | old_mac = skb_mac_header(skb); |
Arnaldo Carvalho de Melo | 39f69c6 | 2007-03-10 12:40:27 -0300 | [diff] [blame] | 133 | skb_set_mac_header(skb, -skb->mac_len); |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 134 | memmove(skb_mac_header(skb), old_mac, skb->mac_len); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 135 | skb_reset_network_header(skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 136 | err = 0; |
| 137 | |
| 138 | out: |
| 139 | return err; |
| 140 | } |
| 141 | |
| 142 | static struct xfrm_mode xfrm4_tunnel_mode = { |
| 143 | .input = xfrm4_tunnel_input, |
| 144 | .output = xfrm4_tunnel_output, |
| 145 | .owner = THIS_MODULE, |
| 146 | .encap = XFRM_MODE_TUNNEL, |
| 147 | }; |
| 148 | |
| 149 | static int __init xfrm4_tunnel_init(void) |
| 150 | { |
| 151 | return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET); |
| 152 | } |
| 153 | |
| 154 | static void __exit xfrm4_tunnel_exit(void) |
| 155 | { |
| 156 | int err; |
| 157 | |
| 158 | err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET); |
| 159 | BUG_ON(err); |
| 160 | } |
| 161 | |
| 162 | module_init(xfrm4_tunnel_init); |
| 163 | module_exit(xfrm4_tunnel_exit); |
| 164 | MODULE_LICENSE("GPL"); |
| 165 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL); |