ipcomp.c 4.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 * IP Payload Compression Protocol (IPComp) - RFC3173.
 *
 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
8
 * Software Foundation; either version 2 of the License, or (at your option)
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16
 * any later version.
 *
 * Todo:
 *   - Tunable compression parameters.
 *   - Compression stats.
 *   - Adaptive compression.
 */
#include <linux/module.h>
17
#include <linux/err.h>
L
Linus Torvalds 已提交
18 19 20 21 22
#include <linux/rtnetlink.h>
#include <net/ip.h>
#include <net/xfrm.h>
#include <net/icmp.h>
#include <net/ipcomp.h>
23
#include <net/protocol.h>
24
#include <net/sock.h>
L
Linus Torvalds 已提交
25 26 27

static void ipcomp4_err(struct sk_buff *skb, u32 info)
{
A
Alexey Dobriyan 已提交
28
	struct net *net = dev_net(skb->dev);
29
	__be32 spi;
30
	const struct iphdr *iph = (const struct iphdr *)skb->data;
L
Linus Torvalds 已提交
31 32 33
	struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
	struct xfrm_state *x;

34 35 36 37 38 39 40
	switch (icmp_hdr(skb)->type) {
	case ICMP_DEST_UNREACH:
		if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
			return;
	case ICMP_REDIRECT:
		break;
	default:
L
Linus Torvalds 已提交
41
		return;
42
	}
L
Linus Torvalds 已提交
43

A
Alexey Dobriyan 已提交
44
	spi = htonl(ntohs(ipch->cpi));
45
	x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
46
			      spi, IPPROTO_COMP, AF_INET);
L
Linus Torvalds 已提交
47 48
	if (!x)
		return;
49 50 51 52 53

	if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
		ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_COMP, 0);
	else
		ipv4_redirect(skb, net, 0, 0, IPPROTO_COMP, 0);
L
Linus Torvalds 已提交
54 55 56
	xfrm_state_put(x);
}

57
/* We always hold one tunnel user reference to indicate a tunnel */
L
Linus Torvalds 已提交
58 59
static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
{
A
Alexey Dobriyan 已提交
60
	struct net *net = xs_net(x);
L
Linus Torvalds 已提交
61
	struct xfrm_state *t;
62

A
Alexey Dobriyan 已提交
63
	t = xfrm_state_alloc(net);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71
	if (t == NULL)
		goto out;

	t->id.proto = IPPROTO_IPIP;
	t->id.spi = x->props.saddr.a4;
	t->id.daddr.a4 = x->id.daddr.a4;
	memcpy(&t->sel, &x->sel, sizeof(t->sel));
	t->props.family = AF_INET;
72
	t->props.mode = x->props.mode;
L
Linus Torvalds 已提交
73 74
	t->props.saddr.a4 = x->props.saddr.a4;
	t->props.flags = x->props.flags;
75
	memcpy(&t->mark, &x->mark, sizeof(t->mark));
H
Herbert Xu 已提交
76 77

	if (xfrm_init_state(t))
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
		goto error;

	atomic_set(&t->tunnel_users, 1);
out:
	return t;

error:
	t->km.state = XFRM_STATE_DEAD;
	xfrm_state_put(t);
	t = NULL;
	goto out;
}

/*
A
Arjan van de Ven 已提交
92
 * Must be protected by xfrm_cfg_mutex.  State and tunnel user references are
L
Linus Torvalds 已提交
93 94 95 96
 * always incremented on success.
 */
static int ipcomp_tunnel_attach(struct xfrm_state *x)
{
A
Alexey Dobriyan 已提交
97
	struct net *net = xs_net(x);
L
Linus Torvalds 已提交
98 99
	int err = 0;
	struct xfrm_state *t;
100
	u32 mark = x->mark.v & x->mark.m;
L
Linus Torvalds 已提交
101

102
	t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4,
103
			      x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	if (!t) {
		t = ipcomp_tunnel_create(x);
		if (!t) {
			err = -EINVAL;
			goto out;
		}
		xfrm_state_insert(t);
		xfrm_state_hold(t);
	}
	x->tunnel = t;
	atomic_inc(&t->tunnel_users);
out:
	return err;
}

119
static int ipcomp4_init_state(struct xfrm_state *x)
L
Linus Torvalds 已提交
120
{
121
	int err = -EINVAL;
L
Linus Torvalds 已提交
122

123 124 125 126 127 128 129 130 131 132 133
	x->props.header_len = 0;
	switch (x->props.mode) {
	case XFRM_MODE_TRANSPORT:
		break;
	case XFRM_MODE_TUNNEL:
		x->props.header_len += sizeof(struct iphdr);
		break;
	default:
		goto out;
	}

134 135
	err = ipcomp_init_state(x);
	if (err)
L
Linus Torvalds 已提交
136 137
		goto out;

138
	if (x->props.mode == XFRM_MODE_TUNNEL) {
L
Linus Torvalds 已提交
139 140
		err = ipcomp_tunnel_attach(x);
		if (err)
141
			goto out;
L
Linus Torvalds 已提交
142 143 144 145 146 147 148
	}

	err = 0;
out:
	return err;
}

149
static const struct xfrm_type ipcomp_type = {
L
Linus Torvalds 已提交
150 151 152
	.description	= "IPCOMP4",
	.owner		= THIS_MODULE,
	.proto	     	= IPPROTO_COMP,
153
	.init_state	= ipcomp4_init_state,
L
Linus Torvalds 已提交
154 155 156 157 158
	.destructor	= ipcomp_destroy,
	.input		= ipcomp_input,
	.output		= ipcomp_output
};

159
static const struct net_protocol ipcomp4_protocol = {
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167
	.handler	=	xfrm4_rcv,
	.err_handler	=	ipcomp4_err,
	.no_policy	=	1,
};

static int __init ipcomp4_init(void)
{
	if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
J
Joe Perches 已提交
168
		pr_info("%s: can't add xfrm type\n", __func__);
L
Linus Torvalds 已提交
169 170 171
		return -EAGAIN;
	}
	if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
J
Joe Perches 已提交
172
		pr_info("%s: can't add protocol\n", __func__);
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181
		xfrm_unregister_type(&ipcomp_type, AF_INET);
		return -EAGAIN;
	}
	return 0;
}

static void __exit ipcomp4_fini(void)
{
	if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
J
Joe Perches 已提交
182
		pr_info("%s: can't remove protocol\n", __func__);
L
Linus Torvalds 已提交
183
	if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
J
Joe Perches 已提交
184
		pr_info("%s: can't remove xfrm type\n", __func__);
L
Linus Torvalds 已提交
185 186 187 188 189 190
}

module_init(ipcomp4_init);
module_exit(ipcomp4_fini);

MODULE_LICENSE("GPL");
191
MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173");
L
Linus Torvalds 已提交
192 193
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");

194
MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);