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 | { |
| 19 | struct iphdr *outer_iph = skb->nh.iph; |
| 20 | struct iphdr *inner_iph = skb->h.ipiph; |
| 21 | |
| 22 | if (INET_ECN_is_ce(outer_iph->tos)) |
| 23 | IP_ECN_set_ce(inner_iph); |
| 24 | } |
| 25 | |
| 26 | /* Add encapsulation header. |
| 27 | * |
| 28 | * The top IP header will be constructed per RFC 2401. The following fields |
| 29 | * in it shall be filled in by x->type->output: |
| 30 | * tot_len |
| 31 | * check |
| 32 | * |
| 33 | * On exit, skb->h will be set to the start of the payload to be processed |
| 34 | * by x->type->output and skb->nh will be set to the top IP header. |
| 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 | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 39 | struct iphdr *iph, *top_iph; |
| 40 | int flags; |
| 41 | |
| 42 | iph = skb->nh.iph; |
| 43 | skb->h.ipiph = iph; |
| 44 | |
| 45 | skb->nh.raw = skb_push(skb, x->props.header_len); |
| 46 | top_iph = skb->nh.iph; |
| 47 | |
| 48 | top_iph->ihl = 5; |
| 49 | top_iph->version = 4; |
| 50 | |
| 51 | /* DS disclosed */ |
| 52 | top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos); |
| 53 | |
| 54 | flags = x->props.flags; |
| 55 | if (flags & XFRM_STATE_NOECN) |
| 56 | IP_ECN_clear(top_iph); |
| 57 | |
| 58 | top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ? |
| 59 | 0 : (iph->frag_off & htons(IP_DF)); |
| 60 | if (!top_iph->frag_off) |
| 61 | __ip_select_ident(top_iph, dst->child, 0); |
| 62 | |
| 63 | top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT); |
| 64 | |
| 65 | top_iph->saddr = x->props.saddr.a4; |
| 66 | top_iph->daddr = x->id.daddr.a4; |
| 67 | top_iph->protocol = IPPROTO_IPIP; |
| 68 | |
| 69 | memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options)); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) |
| 74 | { |
| 75 | struct iphdr *iph = skb->nh.iph; |
| 76 | int err = -EINVAL; |
| 77 | |
| 78 | if (iph->protocol != IPPROTO_IPIP) |
| 79 | goto out; |
| 80 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 81 | goto out; |
| 82 | |
| 83 | if (skb_cloned(skb) && |
| 84 | (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) |
| 85 | goto out; |
| 86 | |
| 87 | if (x->props.flags & XFRM_STATE_DECAP_DSCP) |
| 88 | ipv4_copy_dscp(iph, skb->h.ipiph); |
| 89 | if (!(x->props.flags & XFRM_STATE_NOECN)) |
| 90 | ipip_ecn_decapsulate(skb); |
| 91 | skb->mac.raw = memmove(skb->data - skb->mac_len, |
| 92 | skb->mac.raw, skb->mac_len); |
| 93 | skb->nh.raw = skb->data; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 94 | err = 0; |
| 95 | |
| 96 | out: |
| 97 | return err; |
| 98 | } |
| 99 | |
| 100 | static struct xfrm_mode xfrm4_tunnel_mode = { |
| 101 | .input = xfrm4_tunnel_input, |
| 102 | .output = xfrm4_tunnel_output, |
| 103 | .owner = THIS_MODULE, |
| 104 | .encap = XFRM_MODE_TUNNEL, |
| 105 | }; |
| 106 | |
| 107 | static int __init xfrm4_tunnel_init(void) |
| 108 | { |
| 109 | return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET); |
| 110 | } |
| 111 | |
| 112 | static void __exit xfrm4_tunnel_exit(void) |
| 113 | { |
| 114 | int err; |
| 115 | |
| 116 | err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET); |
| 117 | BUG_ON(err); |
| 118 | } |
| 119 | |
| 120 | module_init(xfrm4_tunnel_init); |
| 121 | module_exit(xfrm4_tunnel_exit); |
| 122 | MODULE_LICENSE("GPL"); |
| 123 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL); |