Blame view

net/ipv6/ipcomp6.c 4.93 KB
1ccea77e2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
  /*
   * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
   *
   * Copyright (C)2003 USAGI/WIDE Project
   *
   * Author	Mitsuru KANDA  <mk@linux-ipv6.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
   */
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
9
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
   * [Memo]
   *
   * Outbound:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
13
14
   *  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
15
16
   *
   * Inbound:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
17
   *  The decompression of IP datagram MUST be done after the reassembly,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
   *  AH/ESP processing.
   */
f32138319   Joe Perches   net: ipv6: Standa...
20
21
  
  #define pr_fmt(fmt) "IPv6: " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
  #include <linux/module.h>
  #include <net/ip.h>
  #include <net/xfrm.h>
  #include <net/ipcomp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  #include <linux/crypto.h>
4999f3621   Herbert Xu   [IPSEC]: Fix cryp...
27
  #include <linux/err.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
  #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>
81aded246   David S. Miller   ipv6: Handle PMTU...
35
  #include <net/ip6_route.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
  #include <net/icmp.h>
  #include <net/ipv6.h>
14c850212   Arnaldo Carvalho de Melo   [INET_SOCK]: Move...
38
  #include <net/protocol.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  #include <linux/ipv6.h>
  #include <linux/icmpv6.h>
4a3e2f711   Arjan van de Ven   [NET] sem2mutex: ...
41
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42

59b84351c   Steffen Klassert   ipcomp6: Use the ...
43
  static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
d5fdd6bab   Brian Haley   ipv6: Use correct...
44
  				u8 type, u8 code, int offset, __be32 info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
  {
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
46
  	struct net *net = dev_net(skb->dev);
a94cfd197   Al Viro   [XFRM]: xfrm_stat...
47
  	__be32 spi;
b71d1d426   Eric Dumazet   inet: constify ip...
48
  	const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
87bdc48d3   Herbert Xu   [IPSEC]: Get rid ...
49
50
  	struct ip_comp_hdr *ipcomph =
  		(struct ip_comp_hdr *)(skb->data + offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  	struct xfrm_state *x;
b3b2b9e19   Steffen Klassert   ipsec: Don't upda...
52
  	if (type != ICMPV6_PKT_TOOBIG &&
ec18d9a26   David S. Miller   ipv6: Add redirec...
53
  	    type != NDISC_REDIRECT)
59b84351c   Steffen Klassert   ipcomp6: Use the ...
54
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55

4195f8145   Alexey Dobriyan   [NET]: Fix "ntohl...
56
  	spi = htonl(ntohs(ipcomph->cpi));
b71d1d426   Eric Dumazet   inet: constify ip...
57
58
  	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
59
  	if (!x)
59b84351c   Steffen Klassert   ipcomp6: Use the ...
60
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

ec18d9a26   David S. Miller   ipv6: Add redirec...
62
  	if (type == NDISC_REDIRECT)
e2d118a1c   Lorenzo Colitti   net: inet: Suppor...
63
64
  		ip6_redirect(skb, net, skb->dev->ifindex, 0,
  			     sock_net_uid(net, NULL));
ec18d9a26   David S. Miller   ipv6: Add redirec...
65
  	else
e2d118a1c   Lorenzo Colitti   net: inet: Suppor...
66
  		ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  	xfrm_state_put(x);
59b84351c   Steffen Klassert   ipcomp6: Use the ...
68
69
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
73
  }
  
  static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  {
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
74
  	struct net *net = xs_net(x);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
  	struct xfrm_state *t = NULL;
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
76
  	t = xfrm_state_alloc(net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
  	if (!t)
  		goto out;
  
  	t->id.proto = IPPROTO_IPV6;
d74340d31   Alexey Dobriyan   netns xfrm: ipcom...
81
  	t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr);
6abaaaae6   Herbert Xu   [IPSEC]: Fix tunn...
82
83
  	if (!t->id.spi)
  		goto error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
  	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...
87
  	t->props.mode = x->props.mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
  	memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
bd55775c8   Jamal Hadi Salim   xfrm: SA lookups ...
89
  	memcpy(&t->mark, &x->mark, sizeof(t->mark));
d5a7a5057   Xin Long   ipcomp: assign if...
90
  	t->if_id = x->if_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91

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

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

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

6fccab671   Herbert Xu   ipsec: ipcomp - M...
148
149
  	err = ipcomp_init_state(x);
  	if (err)
e40b32861   Herbert Xu   [IPSEC]: Forbid B...
150
  		goto out;
7e49e6de3   Masahide NAKAMURA   [XFRM]: Add XFRM_...
151
  	if (x->props.mode == XFRM_MODE_TUNNEL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
  		err = ipcomp6_tunnel_attach(x);
  		if (err)
10e7454ed   Herbert Xu   ipcomp: Avoid dup...
154
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
  	err = 0;
  out:
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
  }
59b84351c   Steffen Klassert   ipcomp6: Use the ...
160
161
162
163
  static int ipcomp6_rcv_cb(struct sk_buff *skb, int err)
  {
  	return 0;
  }
cc24becae   Ian Morris   ipv6: White-space...
164
  static const struct xfrm_type ipcomp6_type = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  };
cc24becae   Ian Morris   ipv6: White-space...
174
  static struct xfrm6_protocol ipcomp6_protocol = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
  	.handler	= xfrm6_rcv,
0146dca70   Sabrina Dubroca   xfrm: add support...
176
  	.input_handler	= xfrm_input,
59b84351c   Steffen Klassert   ipcomp6: Use the ...
177
  	.cb_handler	= ipcomp6_rcv_cb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  	.err_handler	= ipcomp6_err,
59b84351c   Steffen Klassert   ipcomp6: Use the ...
179
  	.priority	= 0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
183
184
  };
  
  static int __init ipcomp6_init(void)
  {
  	if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
f32138319   Joe Perches   net: ipv6: Standa...
185
186
  		pr_info("%s: can't add xfrm type
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
  		return -EAGAIN;
  	}
59b84351c   Steffen Klassert   ipcomp6: Use the ...
189
  	if (xfrm6_protocol_register(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
f32138319   Joe Perches   net: ipv6: Standa...
190
191
  		pr_info("%s: can't add protocol
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
193
194
195
196
197
198
199
  		xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  		return -EAGAIN;
  	}
  	return 0;
  }
  
  static void __exit ipcomp6_fini(void)
  {
59b84351c   Steffen Klassert   ipcomp6: Use the ...
200
  	if (xfrm6_protocol_deregister(&ipcomp6_protocol, IPPROTO_COMP) < 0)
f32138319   Joe Perches   net: ipv6: Standa...
201
202
  		pr_info("%s: can't remove protocol
  ", __func__);
4f518e802   Florian Westphal   xfrm: remove type...
203
  	xfrm_unregister_type(&ipcomp6_type, AF_INET6);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
208
209
210
  }
  
  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...
211
  MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);