xref: /linux/net/ipv4/ipcomp.c (revision c6fbb759)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * IP Payload Compression Protocol (IPComp) - RFC3173.
4  *
5  * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
6  *
7  * Todo:
8  *   - Tunable compression parameters.
9  *   - Compression stats.
10  *   - Adaptive compression.
11  */
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/rtnetlink.h>
15 #include <net/ip.h>
16 #include <net/xfrm.h>
17 #include <net/icmp.h>
18 #include <net/ipcomp.h>
19 #include <net/protocol.h>
20 #include <net/sock.h>
21 
22 static int ipcomp4_err(struct sk_buff *skb, u32 info)
23 {
24 	struct net *net = dev_net(skb->dev);
25 	__be32 spi;
26 	const struct iphdr *iph = (const struct iphdr *)skb->data;
27 	struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
28 	struct xfrm_state *x;
29 
30 	switch (icmp_hdr(skb)->type) {
31 	case ICMP_DEST_UNREACH:
32 		if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
33 			return 0;
34 		break;
35 	case ICMP_REDIRECT:
36 		break;
37 	default:
38 		return 0;
39 	}
40 
41 	spi = htonl(ntohs(ipch->cpi));
42 	x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
43 			      spi, IPPROTO_COMP, AF_INET);
44 	if (!x)
45 		return 0;
46 
47 	if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
48 		ipv4_update_pmtu(skb, net, info, 0, IPPROTO_COMP);
49 	else
50 		ipv4_redirect(skb, net, 0, IPPROTO_COMP);
51 	xfrm_state_put(x);
52 
53 	return 0;
54 }
55 
56 /* We always hold one tunnel user reference to indicate a tunnel */
57 static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
58 {
59 	struct net *net = xs_net(x);
60 	struct xfrm_state *t;
61 
62 	t = xfrm_state_alloc(net);
63 	if (!t)
64 		goto out;
65 
66 	t->id.proto = IPPROTO_IPIP;
67 	t->id.spi = x->props.saddr.a4;
68 	t->id.daddr.a4 = x->id.daddr.a4;
69 	memcpy(&t->sel, &x->sel, sizeof(t->sel));
70 	t->props.family = AF_INET;
71 	t->props.mode = x->props.mode;
72 	t->props.saddr.a4 = x->props.saddr.a4;
73 	t->props.flags = x->props.flags;
74 	t->props.extra_flags = x->props.extra_flags;
75 	memcpy(&t->mark, &x->mark, sizeof(t->mark));
76 	t->if_id = x->if_id;
77 
78 	if (xfrm_init_state(t))
79 		goto error;
80 
81 	atomic_set(&t->tunnel_users, 1);
82 out:
83 	return t;
84 
85 error:
86 	t->km.state = XFRM_STATE_DEAD;
87 	xfrm_state_put(t);
88 	t = NULL;
89 	goto out;
90 }
91 
92 /*
93  * Must be protected by xfrm_cfg_mutex.  State and tunnel user references are
94  * always incremented on success.
95  */
96 static int ipcomp_tunnel_attach(struct xfrm_state *x)
97 {
98 	struct net *net = xs_net(x);
99 	int err = 0;
100 	struct xfrm_state *t;
101 	u32 mark = x->mark.v & x->mark.m;
102 
103 	t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4,
104 			      x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
105 	if (!t) {
106 		t = ipcomp_tunnel_create(x);
107 		if (!t) {
108 			err = -EINVAL;
109 			goto out;
110 		}
111 		xfrm_state_insert(t);
112 		xfrm_state_hold(t);
113 	}
114 	x->tunnel = t;
115 	atomic_inc(&t->tunnel_users);
116 out:
117 	return err;
118 }
119 
120 static int ipcomp4_init_state(struct xfrm_state *x,
121 			      struct netlink_ext_ack *extack)
122 {
123 	int err = -EINVAL;
124 
125 	x->props.header_len = 0;
126 	switch (x->props.mode) {
127 	case XFRM_MODE_TRANSPORT:
128 		break;
129 	case XFRM_MODE_TUNNEL:
130 		x->props.header_len += sizeof(struct iphdr);
131 		break;
132 	default:
133 		NL_SET_ERR_MSG(extack, "Unsupported XFRM mode for IPcomp");
134 		goto out;
135 	}
136 
137 	err = ipcomp_init_state(x, extack);
138 	if (err)
139 		goto out;
140 
141 	if (x->props.mode == XFRM_MODE_TUNNEL) {
142 		err = ipcomp_tunnel_attach(x);
143 		if (err) {
144 			NL_SET_ERR_MSG(extack, "Kernel error: failed to initialize the associated state");
145 			goto out;
146 		}
147 	}
148 
149 	err = 0;
150 out:
151 	return err;
152 }
153 
154 static int ipcomp4_rcv_cb(struct sk_buff *skb, int err)
155 {
156 	return 0;
157 }
158 
159 static const struct xfrm_type ipcomp_type = {
160 	.owner		= THIS_MODULE,
161 	.proto	     	= IPPROTO_COMP,
162 	.init_state	= ipcomp4_init_state,
163 	.destructor	= ipcomp_destroy,
164 	.input		= ipcomp_input,
165 	.output		= ipcomp_output
166 };
167 
168 static struct xfrm4_protocol ipcomp4_protocol = {
169 	.handler	=	xfrm4_rcv,
170 	.input_handler	=	xfrm_input,
171 	.cb_handler	=	ipcomp4_rcv_cb,
172 	.err_handler	=	ipcomp4_err,
173 	.priority	=	0,
174 };
175 
176 static int __init ipcomp4_init(void)
177 {
178 	if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
179 		pr_info("%s: can't add xfrm type\n", __func__);
180 		return -EAGAIN;
181 	}
182 	if (xfrm4_protocol_register(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
183 		pr_info("%s: can't add protocol\n", __func__);
184 		xfrm_unregister_type(&ipcomp_type, AF_INET);
185 		return -EAGAIN;
186 	}
187 	return 0;
188 }
189 
190 static void __exit ipcomp4_fini(void)
191 {
192 	if (xfrm4_protocol_deregister(&ipcomp4_protocol, IPPROTO_COMP) < 0)
193 		pr_info("%s: can't remove protocol\n", __func__);
194 	xfrm_unregister_type(&ipcomp_type, AF_INET);
195 }
196 
197 module_init(ipcomp4_init);
198 module_exit(ipcomp4_fini);
199 
200 MODULE_LICENSE("GPL");
201 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173");
202 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
203 
204 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);
205