Blame view

net/ipv6/ipcomp6.c 5.38 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
  /*
   * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
   *
   * Copyright (C)2003 USAGI/WIDE Project
   *
   * Author	Mitsuru KANDA  <mk@linux-ipv6.org>
   *
   * 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 Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
12
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
17
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
22
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
   * [Memo]
   *
   * Outbound:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
26
27
   *  The compression of IP datagram MUST be done before AH/ESP processing,
   *  fragmentation, and the addition of Hop-by-Hop/Routing header.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
   *
   * Inbound:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
30
   *  The decompression of IP datagram MUST be done after the reassembly,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
   *  AH/ESP processing.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
  #include <linux/module.h>
  #include <net/ip.h>
  #include <net/xfrm.h>
  #include <net/ipcomp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  #include <linux/crypto.h>
4999f3621   Herbert Xu   [IPSEC]: Fix cryp...
38
  #include <linux/err.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
44
45
46
47
  #include <linux/pfkeyv2.h>
  #include <linux/random.h>
  #include <linux/percpu.h>
  #include <linux/smp.h>
  #include <linux/list.h>
  #include <linux/vmalloc.h>
  #include <linux/rtnetlink.h>
  #include <net/icmp.h>
  #include <net/ipv6.h>
14c850212   Arnaldo Carvalho de Melo   [INET_SOCK]: Move...
48
  #include <net/protocol.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
  #include <linux/ipv6.h>
  #include <linux/icmpv6.h>
4a3e2f711   Arjan van de Ven   [NET] sem2mutex: ...
51
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  static void ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
d5fdd6bab   Brian Haley   ipv6: Use correct...
54
  				u8 type, u8 code, int offset, __be32 info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
  {
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
56
  	struct net *net = dev_net(skb->dev);
a94cfd197   Al Viro   [XFRM]: xfrm_stat...
57
  	__be32 spi;
b71d1d426   Eric Dumazet   inet: constify ip...
58
  	const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
87bdc48d3   Herbert Xu   [IPSEC]: Get rid ...
59
60
  	struct ip_comp_hdr *ipcomph =
  		(struct ip_comp_hdr *)(skb->data + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
64
  	struct xfrm_state *x;
  
  	if (type != ICMPV6_DEST_UNREACH && type != ICMPV6_PKT_TOOBIG)
  		return;
4195f8145   Alexey Dobriyan   [NET]: Fix "ntohl...
65
  	spi = htonl(ntohs(ipcomph->cpi));
b71d1d426   Eric Dumazet   inet: constify ip...
66
67
  	x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  			      spi, IPPROTO_COMP, AF_INET6);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
  	if (!x)
  		return;
5b095d989   Harvey Harrison   net: replace %p6 ...
70
71
  	printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/%pI6
  ",
0c6ce78ab   Harvey Harrison   net: replace uses...
72
  			spi, &iph->daddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
  	xfrm_state_put(x);
  }
  
  static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  {
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
78
  	struct net *net = xs_net(x);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
  	struct xfrm_state *t = NULL;
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
80
  	t = xfrm_state_alloc(net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
  	if (!t)
  		goto out;
  
  	t->id.proto = IPPROTO_IPV6;
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
85
  	t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr);
6abaaaae6   Herbert Xu   [IPSEC]: Fix tunn...
86
87
  	if (!t->id.spi)
  		goto error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
  	memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
  	memcpy(&t->sel, &x->sel, sizeof(t->sel));
  	t->props.family = AF_INET6;
e40b32861   Herbert Xu   [IPSEC]: Forbid B...
91
  	t->props.mode = x->props.mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
bd55775c8   Jamal Hadi Salim   xfrm: SA lookups ...
93
  	memcpy(&t->mark, &x->mark, sizeof(t->mark));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94

72cb6962a   Herbert Xu   [IPSEC]: Add xfrm...
95
  	if (xfrm_init_state(t))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  		goto error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
100
101
102
  	atomic_set(&t->tunnel_users, 1);
  
  out:
  	return t;
  
  error:
6abaaaae6   Herbert Xu   [IPSEC]: Fix tunn...
103
  	t->km.state = XFRM_STATE_DEAD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  	xfrm_state_put(t);
6abaaaae6   Herbert Xu   [IPSEC]: Fix tunn...
105
  	t = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
108
109
110
  	goto out;
  }
  
  static int ipcomp6_tunnel_attach(struct xfrm_state *x)
  {
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
111
  	struct net *net = xs_net(x);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
  	int err = 0;
  	struct xfrm_state *t = NULL;
a94cfd197   Al Viro   [XFRM]: xfrm_stat...
114
  	__be32 spi;
bd55775c8   Jamal Hadi Salim   xfrm: SA lookups ...
115
  	u32 mark = x->mark.m & x->mark.v;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116

d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
117
  	spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  	if (spi)
bd55775c8   Jamal Hadi Salim   xfrm: SA lookups ...
119
  		t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  					      spi, IPPROTO_IPV6, AF_INET6);
  	if (!t) {
  		t = ipcomp6_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;
  }
72cb6962a   Herbert Xu   [IPSEC]: Add xfrm...
136
  static int ipcomp6_init_state(struct xfrm_state *x)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
  {
2c3abab7c   David S. Miller   ipcomp: Fix warni...
138
  	int err = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  	x->props.header_len = 0;
ca68145f1   Herbert Xu   [IPSEC]: Disallow...
141
  	switch (x->props.mode) {
ca68145f1   Herbert Xu   [IPSEC]: Disallow...
142
143
144
  	case XFRM_MODE_TRANSPORT:
  		break;
  	case XFRM_MODE_TUNNEL:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  		x->props.header_len += sizeof(struct ipv6hdr);
e40b32861   Herbert Xu   [IPSEC]: Forbid B...
146
  		break;
ca68145f1   Herbert Xu   [IPSEC]: Disallow...
147
  	default:
e40b32861   Herbert Xu   [IPSEC]: Forbid B...
148
  		goto out;
ca68145f1   Herbert Xu   [IPSEC]: Disallow...
149
  	}
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
150

6fccab671   Herbert Xu   ipsec: ipcomp - M...
151
152
  	err = ipcomp_init_state(x);
  	if (err)
e40b32861   Herbert Xu   [IPSEC]: Forbid B...
153
  		goto out;
7e49e6de3   Masahide NAKAMURA   [XFRM]: Add XFRM_...
154
  	if (x->props.mode == XFRM_MODE_TUNNEL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  		err = ipcomp6_tunnel_attach(x);
  		if (err)
10e7454ed   Herbert Xu   ipcomp: Avoid dup...
157
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
160
161
  	err = 0;
  out:
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  }
533cb5b0a   Eric Dumazet   [XFRM]: constify ...
163
  static const struct xfrm_type ipcomp6_type =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
166
167
168
  {
  	.description	= "IPCOMP6",
  	.owner		= THIS_MODULE,
  	.proto		= IPPROTO_COMP,
  	.init_state	= ipcomp6_init_state,
6fccab671   Herbert Xu   ipsec: ipcomp - M...
169
170
171
  	.destructor	= ipcomp_destroy,
  	.input		= ipcomp_input,
  	.output		= ipcomp_output,
aee5adb43   Masahide NAKAMURA   [XFRM] STATE: Add...
172
  	.hdr_offset	= xfrm6_find_1stfragopt,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  };
41135cc83   Alexey Dobriyan   net: constify str...
174
  static const struct inet6_protocol ipcomp6_protocol =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  {
  	.handler	= xfrm6_rcv,
  	.err_handler	= ipcomp6_err,
  	.flags		= INET6_PROTO_NOPOLICY,
  };
  
  static int __init ipcomp6_init(void)
  {
  	if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
  		printk(KERN_INFO "ipcomp6 init: can't add xfrm type
  ");
  		return -EAGAIN;
  	}
  	if (inet6_add_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
  		printk(KERN_INFO "ipcomp6 init: can't add protocol
  ");
  		xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  		return -EAGAIN;
  	}
  	return 0;
  }
  
  static void __exit ipcomp6_fini(void)
  {
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
199
  	if (inet6_del_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
204
205
206
207
208
209
210
211
  		printk(KERN_INFO "ipv6 ipcomp close: can't remove protocol
  ");
  	if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
  		printk(KERN_INFO "ipv6 ipcomp close: can't remove xfrm type
  ");
  }
  
  module_init(ipcomp6_init);
  module_exit(ipcomp6_fini);
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
  MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
d3d6dd3ad   Masahide NAKAMURA   [XFRM]: Add modul...
212
  MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);