Blame view
net/ipv6/ipcomp6.c
4.93 KB
1ccea77e2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c
|
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
|
8 |
*/ |
1ab1457c4
|
9 |
/* |
1da177e4c
|
10 11 12 |
* [Memo] * * Outbound: |
1ab1457c4
|
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
|
15 16 |
* * Inbound: |
1ab1457c4
|
17 |
* The decompression of IP datagram MUST be done after the reassembly, |
1da177e4c
|
18 19 |
* AH/ESP processing. */ |
f32138319
|
20 21 |
#define pr_fmt(fmt) "IPv6: " fmt |
1da177e4c
|
22 23 24 25 |
#include <linux/module.h> #include <net/ip.h> #include <net/xfrm.h> #include <net/ipcomp.h> |
1da177e4c
|
26 |
#include <linux/crypto.h> |
4999f3621
|
27 |
#include <linux/err.h> |
1da177e4c
|
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
|
35 |
#include <net/ip6_route.h> |
1da177e4c
|
36 37 |
#include <net/icmp.h> #include <net/ipv6.h> |
14c850212
|
38 |
#include <net/protocol.h> |
1da177e4c
|
39 40 |
#include <linux/ipv6.h> #include <linux/icmpv6.h> |
4a3e2f711
|
41 |
#include <linux/mutex.h> |
1da177e4c
|
42 |
|
59b84351c
|
43 |
static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
d5fdd6bab
|
44 |
u8 type, u8 code, int offset, __be32 info) |
1da177e4c
|
45 |
{ |
d74340d31
|
46 |
struct net *net = dev_net(skb->dev); |
a94cfd197
|
47 |
__be32 spi; |
b71d1d426
|
48 |
const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data; |
87bdc48d3
|
49 50 |
struct ip_comp_hdr *ipcomph = (struct ip_comp_hdr *)(skb->data + offset); |
1da177e4c
|
51 |
struct xfrm_state *x; |
b3b2b9e19
|
52 |
if (type != ICMPV6_PKT_TOOBIG && |
ec18d9a26
|
53 |
type != NDISC_REDIRECT) |
59b84351c
|
54 |
return 0; |
1da177e4c
|
55 |
|
4195f8145
|
56 |
spi = htonl(ntohs(ipcomph->cpi)); |
b71d1d426
|
57 58 |
x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6); |
1da177e4c
|
59 |
if (!x) |
59b84351c
|
60 |
return 0; |
1da177e4c
|
61 |
|
ec18d9a26
|
62 |
if (type == NDISC_REDIRECT) |
e2d118a1c
|
63 64 |
ip6_redirect(skb, net, skb->dev->ifindex, 0, sock_net_uid(net, NULL)); |
ec18d9a26
|
65 |
else |
e2d118a1c
|
66 |
ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL)); |
1da177e4c
|
67 |
xfrm_state_put(x); |
59b84351c
|
68 69 |
return 0; |
1da177e4c
|
70 71 72 73 |
} static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x) { |
d74340d31
|
74 |
struct net *net = xs_net(x); |
1da177e4c
|
75 |
struct xfrm_state *t = NULL; |
d74340d31
|
76 |
t = xfrm_state_alloc(net); |
1da177e4c
|
77 78 79 80 |
if (!t) goto out; t->id.proto = IPPROTO_IPV6; |
d74340d31
|
81 |
t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr); |
6abaaaae6
|
82 83 |
if (!t->id.spi) goto error; |
1da177e4c
|
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
|
87 |
t->props.mode = x->props.mode; |
1da177e4c
|
88 |
memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr)); |
bd55775c8
|
89 |
memcpy(&t->mark, &x->mark, sizeof(t->mark)); |
d5a7a5057
|
90 |
t->if_id = x->if_id; |
1da177e4c
|
91 |
|
72cb6962a
|
92 |
if (xfrm_init_state(t)) |
1da177e4c
|
93 |
goto error; |
1da177e4c
|
94 95 96 97 98 99 |
atomic_set(&t->tunnel_users, 1); out: return t; error: |
6abaaaae6
|
100 |
t->km.state = XFRM_STATE_DEAD; |
1da177e4c
|
101 |
xfrm_state_put(t); |
6abaaaae6
|
102 |
t = NULL; |
1da177e4c
|
103 104 105 106 107 |
goto out; } static int ipcomp6_tunnel_attach(struct xfrm_state *x) { |
d74340d31
|
108 |
struct net *net = xs_net(x); |
1da177e4c
|
109 110 |
int err = 0; struct xfrm_state *t = NULL; |
a94cfd197
|
111 |
__be32 spi; |
bd55775c8
|
112 |
u32 mark = x->mark.m & x->mark.v; |
1da177e4c
|
113 |
|
d74340d31
|
114 |
spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr); |
1da177e4c
|
115 |
if (spi) |
bd55775c8
|
116 |
t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr, |
1da177e4c
|
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
|
133 |
static int ipcomp6_init_state(struct xfrm_state *x) |
1da177e4c
|
134 |
{ |
2c3abab7c
|
135 |
int err = -EINVAL; |
1da177e4c
|
136 |
|
1da177e4c
|
137 |
x->props.header_len = 0; |
ca68145f1
|
138 |
switch (x->props.mode) { |
ca68145f1
|
139 140 141 |
case XFRM_MODE_TRANSPORT: break; case XFRM_MODE_TUNNEL: |
1da177e4c
|
142 |
x->props.header_len += sizeof(struct ipv6hdr); |
e40b32861
|
143 |
break; |
ca68145f1
|
144 |
default: |
e40b32861
|
145 |
goto out; |
ca68145f1
|
146 |
} |
1ab1457c4
|
147 |
|
6fccab671
|
148 149 |
err = ipcomp_init_state(x); if (err) |
e40b32861
|
150 |
goto out; |
7e49e6de3
|
151 |
if (x->props.mode == XFRM_MODE_TUNNEL) { |
1da177e4c
|
152 153 |
err = ipcomp6_tunnel_attach(x); if (err) |
10e7454ed
|
154 |
goto out; |
1da177e4c
|
155 |
} |
1da177e4c
|
156 157 158 |
err = 0; out: return err; |
1da177e4c
|
159 |
} |
59b84351c
|
160 161 162 163 |
static int ipcomp6_rcv_cb(struct sk_buff *skb, int err) { return 0; } |
cc24becae
|
164 |
static const struct xfrm_type ipcomp6_type = { |
1da177e4c
|
165 166 167 168 |
.description = "IPCOMP6", .owner = THIS_MODULE, .proto = IPPROTO_COMP, .init_state = ipcomp6_init_state, |
6fccab671
|
169 170 171 |
.destructor = ipcomp_destroy, .input = ipcomp_input, .output = ipcomp_output, |
aee5adb43
|
172 |
.hdr_offset = xfrm6_find_1stfragopt, |
1da177e4c
|
173 |
}; |
cc24becae
|
174 |
static struct xfrm6_protocol ipcomp6_protocol = { |
1da177e4c
|
175 |
.handler = xfrm6_rcv, |
0146dca70
|
176 |
.input_handler = xfrm_input, |
59b84351c
|
177 |
.cb_handler = ipcomp6_rcv_cb, |
1da177e4c
|
178 |
.err_handler = ipcomp6_err, |
59b84351c
|
179 |
.priority = 0, |
1da177e4c
|
180 181 182 183 184 |
}; static int __init ipcomp6_init(void) { if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) { |
f32138319
|
185 186 |
pr_info("%s: can't add xfrm type ", __func__); |
1da177e4c
|
187 188 |
return -EAGAIN; } |
59b84351c
|
189 |
if (xfrm6_protocol_register(&ipcomp6_protocol, IPPROTO_COMP) < 0) { |
f32138319
|
190 191 |
pr_info("%s: can't add protocol ", __func__); |
1da177e4c
|
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
|
200 |
if (xfrm6_protocol_deregister(&ipcomp6_protocol, IPPROTO_COMP) < 0) |
f32138319
|
201 202 |
pr_info("%s: can't remove protocol ", __func__); |
4f518e802
|
203 |
xfrm_unregister_type(&ipcomp6_type, AF_INET6); |
1da177e4c
|
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
|
211 |
MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP); |