xfrm4_mode_tunnel.c 4.7 KB
Newer Older
1 2 3 4 5 6
/*
 * xfrm4_mode_tunnel.c - Tunnel mode encapsulation for IPv4.
 *
 * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
 */

7
#include <linux/gfp.h>
8 9 10 11 12 13 14 15 16 17
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/stringify.h>
#include <net/dst.h>
#include <net/inet_ecn.h>
#include <net/ip.h>
#include <net/xfrm.h>

18
/* Informational hook. The decap is still done here. */
19
static struct xfrm_tunnel_notifier __rcu *rcv_notify_handlers __read_mostly;
20 21
static DEFINE_MUTEX(xfrm4_mode_tunnel_input_mutex);

22
int xfrm4_mode_tunnel_input_register(struct xfrm_tunnel_notifier *handler)
23
{
24 25
	struct xfrm_tunnel_notifier __rcu **pprev;
	struct xfrm_tunnel_notifier *t;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	int ret = -EEXIST;
	int priority = handler->priority;

	mutex_lock(&xfrm4_mode_tunnel_input_mutex);

	for (pprev = &rcv_notify_handlers;
	     (t = rcu_dereference_protected(*pprev,
	     lockdep_is_held(&xfrm4_mode_tunnel_input_mutex))) != NULL;
	     pprev = &t->next) {
		if (t->priority > priority)
			break;
		if (t->priority == priority)
			goto err;

	}

	handler->next = *pprev;
	rcu_assign_pointer(*pprev, handler);

	ret = 0;

err:
	mutex_unlock(&xfrm4_mode_tunnel_input_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(xfrm4_mode_tunnel_input_register);

53
int xfrm4_mode_tunnel_input_deregister(struct xfrm_tunnel_notifier *handler)
54
{
55 56
	struct xfrm_tunnel_notifier __rcu **pprev;
	struct xfrm_tunnel_notifier *t;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	int ret = -ENOENT;

	mutex_lock(&xfrm4_mode_tunnel_input_mutex);
	for (pprev = &rcv_notify_handlers;
	     (t = rcu_dereference_protected(*pprev,
	     lockdep_is_held(&xfrm4_mode_tunnel_input_mutex))) != NULL;
	     pprev = &t->next) {
		if (t == handler) {
			*pprev = handler->next;
			ret = 0;
			break;
		}
	}
	mutex_unlock(&xfrm4_mode_tunnel_input_mutex);
	synchronize_net();

	return ret;
}
EXPORT_SYMBOL_GPL(xfrm4_mode_tunnel_input_deregister);

77 78
static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
{
79
	struct iphdr *inner_iph = ipip_hdr(skb);
80

81
	if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
82 83 84 85 86
		IP_ECN_set_ce(inner_iph);
}

/* Add encapsulation header.
 *
87
 * The top IP header will be constructed per RFC 2401.
88
 */
89
static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
90
{
E
Eric Dumazet 已提交
91
	struct dst_entry *dst = skb_dst(skb);
92
	struct iphdr *top_iph;
93 94
	int flags;

95
	skb_set_network_header(skb, -x->props.header_len);
96 97
	skb->mac_header = skb->network_header +
			  offsetof(struct iphdr, protocol);
98
	skb->transport_header = skb->network_header + sizeof(*top_iph);
99
	top_iph = ip_hdr(skb);
100 101 102 103

	top_iph->ihl = 5;
	top_iph->version = 4;

E
Eric Dumazet 已提交
104
	top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
105

106 107 108 109 110 111
	/* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
	if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
		top_iph->tos = 0;
	else
		top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
	top_iph->tos = INET_ECN_encapsulate(top_iph->tos,
112
					    XFRM_MODE_SKB_CB(skb)->tos);
113

114
	flags = x->props.flags;
115 116 117
	if (flags & XFRM_STATE_NOECN)
		IP_ECN_clear(top_iph);

118
	top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
119
		0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
120
	ip_select_ident(skb, dst->child, NULL);
121

122
	top_iph->ttl = ip4_dst_hoplimit(dst->child);
123 124 125 126 127 128 129

	top_iph->saddr = x->props.saddr.a4;
	top_iph->daddr = x->id.daddr.a4;

	return 0;
}

130 131 132 133 134
#define for_each_input_rcu(head, handler)	\
	for (handler = rcu_dereference(head);	\
	     handler != NULL;			\
	     handler = rcu_dereference(handler->next))

135
static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
136
{
137
	struct xfrm_tunnel_notifier *handler;
138 139
	int err = -EINVAL;

140 141
	if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
		goto out;
142

143 144 145
	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
		goto out;

146 147 148
	for_each_input_rcu(rcv_notify_handlers, handler)
		handler->handler(skb);

149 150
	err = skb_unclone(skb, GFP_ATOMIC);
	if (err)
151 152
		goto out;

153 154 155 156 157
	if (x->props.flags & XFRM_STATE_DECAP_DSCP)
		ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
	if (!(x->props.flags & XFRM_STATE_NOECN))
		ipip_ecn_decapsulate(skb);

158
	skb_reset_network_header(skb);
159 160
	skb_mac_header_rebuild(skb);

161 162 163 164 165 166 167
	err = 0;

out:
	return err;
}

static struct xfrm_mode xfrm4_tunnel_mode = {
168
	.input2 = xfrm4_mode_tunnel_input,
169
	.input = xfrm_prepare_input,
170
	.output2 = xfrm4_mode_tunnel_output,
171
	.output = xfrm4_prepare_output,
172 173
	.owner = THIS_MODULE,
	.encap = XFRM_MODE_TUNNEL,
H
Herbert Xu 已提交
174
	.flags = XFRM_MODE_FLAG_TUNNEL,
175 176
};

177
static int __init xfrm4_mode_tunnel_init(void)
178 179 180 181
{
	return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
}

182
static void __exit xfrm4_mode_tunnel_exit(void)
183 184 185 186 187 188 189
{
	int err;

	err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
	BUG_ON(err);
}

190 191
module_init(xfrm4_mode_tunnel_init);
module_exit(xfrm4_mode_tunnel_exit);
192 193
MODULE_LICENSE("GPL");
MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);