|
| 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 | + */ |
| 36 | +static int xfrm4_tunnel_output(struct sk_buff *skb) |
| 37 | +{ |
| 38 | + struct dst_entry *dst = skb->dst; |
| 39 | + struct xfrm_state *x = dst->xfrm; |
| 40 | + struct iphdr *iph, *top_iph; |
| 41 | + int flags; |
| 42 | + |
| 43 | + iph = skb->nh.iph; |
| 44 | + skb->h.ipiph = iph; |
| 45 | + |
| 46 | + skb->nh.raw = skb_push(skb, x->props.header_len); |
| 47 | + top_iph = skb->nh.iph; |
| 48 | + |
| 49 | + top_iph->ihl = 5; |
| 50 | + top_iph->version = 4; |
| 51 | + |
| 52 | + /* DS disclosed */ |
| 53 | + top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos); |
| 54 | + |
| 55 | + flags = x->props.flags; |
| 56 | + if (flags & XFRM_STATE_NOECN) |
| 57 | + IP_ECN_clear(top_iph); |
| 58 | + |
| 59 | + top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ? |
| 60 | + 0 : (iph->frag_off & htons(IP_DF)); |
| 61 | + if (!top_iph->frag_off) |
| 62 | + __ip_select_ident(top_iph, dst->child, 0); |
| 63 | + |
| 64 | + top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT); |
| 65 | + |
| 66 | + top_iph->saddr = x->props.saddr.a4; |
| 67 | + top_iph->daddr = x->id.daddr.a4; |
| 68 | + top_iph->protocol = IPPROTO_IPIP; |
| 69 | + |
| 70 | + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options)); |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) |
| 75 | +{ |
| 76 | + struct iphdr *iph = skb->nh.iph; |
| 77 | + int err = -EINVAL; |
| 78 | + |
| 79 | + if (iph->protocol != IPPROTO_IPIP) |
| 80 | + goto out; |
| 81 | + if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 82 | + goto out; |
| 83 | + |
| 84 | + if (skb_cloned(skb) && |
| 85 | + (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) |
| 86 | + goto out; |
| 87 | + |
| 88 | + if (x->props.flags & XFRM_STATE_DECAP_DSCP) |
| 89 | + ipv4_copy_dscp(iph, skb->h.ipiph); |
| 90 | + if (!(x->props.flags & XFRM_STATE_NOECN)) |
| 91 | + ipip_ecn_decapsulate(skb); |
| 92 | + skb->mac.raw = memmove(skb->data - skb->mac_len, |
| 93 | + skb->mac.raw, skb->mac_len); |
| 94 | + skb->nh.raw = skb->data; |
| 95 | + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options)); |
| 96 | + err = 0; |
| 97 | + |
| 98 | +out: |
| 99 | + return err; |
| 100 | +} |
| 101 | + |
| 102 | +static struct xfrm_mode xfrm4_tunnel_mode = { |
| 103 | + .input = xfrm4_tunnel_input, |
| 104 | + .output = xfrm4_tunnel_output, |
| 105 | + .owner = THIS_MODULE, |
| 106 | + .encap = XFRM_MODE_TUNNEL, |
| 107 | +}; |
| 108 | + |
| 109 | +static int __init xfrm4_tunnel_init(void) |
| 110 | +{ |
| 111 | + return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET); |
| 112 | +} |
| 113 | + |
| 114 | +static void __exit xfrm4_tunnel_exit(void) |
| 115 | +{ |
| 116 | + int err; |
| 117 | + |
| 118 | + err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET); |
| 119 | + BUG_ON(err); |
| 120 | +} |
| 121 | + |
| 122 | +module_init(xfrm4_tunnel_init); |
| 123 | +module_exit(xfrm4_tunnel_exit); |
| 124 | +MODULE_LICENSE("GPL"); |
| 125 | +MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL); |
0 commit comments