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