ipcomp6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
  3. *
  4. * Copyright (C)2003 USAGI/WIDE Project
  5. *
  6. * Author Mitsuru KANDA <mk@linux-ipv6.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*
  23. * [Memo]
  24. *
  25. * Outbound:
  26. * The compression of IP datagram MUST be done before AH/ESP processing,
  27. * fragmentation, and the addition of Hop-by-Hop/Routing header.
  28. *
  29. * Inbound:
  30. * The decompression of IP datagram MUST be done after the reassembly,
  31. * AH/ESP processing.
  32. */
  33. #include <linux/module.h>
  34. #include <net/ip.h>
  35. #include <net/xfrm.h>
  36. #include <net/ipcomp.h>
  37. #include <asm/scatterlist.h>
  38. #include <asm/semaphore.h>
  39. #include <linux/crypto.h>
  40. #include <linux/pfkeyv2.h>
  41. #include <linux/random.h>
  42. #include <linux/percpu.h>
  43. #include <linux/smp.h>
  44. #include <linux/list.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/rtnetlink.h>
  47. #include <net/icmp.h>
  48. #include <net/ipv6.h>
  49. #include <net/protocol.h>
  50. #include <linux/ipv6.h>
  51. #include <linux/icmpv6.h>
  52. #include <linux/mutex.h>
  53. struct ipcomp6_tfms {
  54. struct list_head list;
  55. struct crypto_tfm **tfms;
  56. int users;
  57. };
  58. static DEFINE_MUTEX(ipcomp6_resource_mutex);
  59. static void **ipcomp6_scratches;
  60. static int ipcomp6_scratch_users;
  61. static LIST_HEAD(ipcomp6_tfms_list);
  62. static int ipcomp6_input(struct xfrm_state *x, struct sk_buff *skb)
  63. {
  64. int err = -ENOMEM;
  65. struct ipv6hdr *iph;
  66. struct ipv6_comp_hdr *ipch;
  67. int plen, dlen;
  68. struct ipcomp_data *ipcd = x->data;
  69. u8 *start, *scratch;
  70. struct crypto_tfm *tfm;
  71. int cpu;
  72. if (skb_linearize_cow(skb))
  73. goto out;
  74. skb->ip_summed = CHECKSUM_NONE;
  75. /* Remove ipcomp header and decompress original payload */
  76. iph = skb->nh.ipv6h;
  77. ipch = (void *)skb->data;
  78. skb->h.raw = skb->nh.raw + sizeof(*ipch);
  79. __skb_pull(skb, sizeof(*ipch));
  80. /* decompression */
  81. plen = skb->len;
  82. dlen = IPCOMP_SCRATCH_SIZE;
  83. start = skb->data;
  84. cpu = get_cpu();
  85. scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
  86. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  87. err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
  88. if (err) {
  89. err = -EINVAL;
  90. goto out_put_cpu;
  91. }
  92. if (dlen < (plen + sizeof(struct ipv6_comp_hdr))) {
  93. err = -EINVAL;
  94. goto out_put_cpu;
  95. }
  96. err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
  97. if (err) {
  98. goto out_put_cpu;
  99. }
  100. skb_put(skb, dlen - plen);
  101. memcpy(skb->data, scratch, dlen);
  102. err = ipch->nexthdr;
  103. out_put_cpu:
  104. put_cpu();
  105. out:
  106. return err;
  107. }
  108. static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
  109. {
  110. int err;
  111. struct ipv6hdr *top_iph;
  112. int hdr_len;
  113. struct ipv6_comp_hdr *ipch;
  114. struct ipcomp_data *ipcd = x->data;
  115. int plen, dlen;
  116. u8 *start, *scratch;
  117. struct crypto_tfm *tfm;
  118. int cpu;
  119. hdr_len = skb->h.raw - skb->data;
  120. /* check whether datagram len is larger than threshold */
  121. if ((skb->len - hdr_len) < ipcd->threshold) {
  122. goto out_ok;
  123. }
  124. if (skb_linearize_cow(skb))
  125. goto out_ok;
  126. /* compression */
  127. plen = skb->len - hdr_len;
  128. dlen = IPCOMP_SCRATCH_SIZE;
  129. start = skb->h.raw;
  130. cpu = get_cpu();
  131. scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
  132. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  133. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  134. if (err || (dlen + sizeof(struct ipv6_comp_hdr)) >= plen) {
  135. put_cpu();
  136. goto out_ok;
  137. }
  138. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  139. put_cpu();
  140. pskb_trim(skb, hdr_len + dlen + sizeof(struct ip_comp_hdr));
  141. /* insert ipcomp header and replace datagram */
  142. top_iph = (struct ipv6hdr *)skb->data;
  143. top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  144. ipch = (struct ipv6_comp_hdr *)start;
  145. ipch->nexthdr = *skb->nh.raw;
  146. ipch->flags = 0;
  147. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  148. *skb->nh.raw = IPPROTO_COMP;
  149. out_ok:
  150. return 0;
  151. }
  152. static void ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  153. int type, int code, int offset, __u32 info)
  154. {
  155. u32 spi;
  156. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  157. struct ipv6_comp_hdr *ipcomph = (struct ipv6_comp_hdr*)(skb->data+offset);
  158. struct xfrm_state *x;
  159. if (type != ICMPV6_DEST_UNREACH && type != ICMPV6_PKT_TOOBIG)
  160. return;
  161. spi = htonl(ntohs(ipcomph->cpi));
  162. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
  163. if (!x)
  164. return;
  165. printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/" NIP6_FMT "\n",
  166. spi, NIP6(iph->daddr));
  167. xfrm_state_put(x);
  168. }
  169. static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  170. {
  171. struct xfrm_state *t = NULL;
  172. t = xfrm_state_alloc();
  173. if (!t)
  174. goto out;
  175. t->id.proto = IPPROTO_IPV6;
  176. t->id.spi = xfrm6_tunnel_alloc_spi((xfrm_address_t *)&x->props.saddr);
  177. if (!t->id.spi)
  178. goto error;
  179. memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
  180. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  181. t->props.family = AF_INET6;
  182. t->props.mode = 1;
  183. memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
  184. if (xfrm_init_state(t))
  185. goto error;
  186. atomic_set(&t->tunnel_users, 1);
  187. out:
  188. return t;
  189. error:
  190. t->km.state = XFRM_STATE_DEAD;
  191. xfrm_state_put(t);
  192. t = NULL;
  193. goto out;
  194. }
  195. static int ipcomp6_tunnel_attach(struct xfrm_state *x)
  196. {
  197. int err = 0;
  198. struct xfrm_state *t = NULL;
  199. u32 spi;
  200. spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&x->props.saddr);
  201. if (spi)
  202. t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr,
  203. spi, IPPROTO_IPV6, AF_INET6);
  204. if (!t) {
  205. t = ipcomp6_tunnel_create(x);
  206. if (!t) {
  207. err = -EINVAL;
  208. goto out;
  209. }
  210. xfrm_state_insert(t);
  211. xfrm_state_hold(t);
  212. }
  213. x->tunnel = t;
  214. atomic_inc(&t->tunnel_users);
  215. out:
  216. return err;
  217. }
  218. static void ipcomp6_free_scratches(void)
  219. {
  220. int i;
  221. void **scratches;
  222. if (--ipcomp6_scratch_users)
  223. return;
  224. scratches = ipcomp6_scratches;
  225. if (!scratches)
  226. return;
  227. for_each_possible_cpu(i) {
  228. void *scratch = *per_cpu_ptr(scratches, i);
  229. vfree(scratch);
  230. }
  231. free_percpu(scratches);
  232. }
  233. static void **ipcomp6_alloc_scratches(void)
  234. {
  235. int i;
  236. void **scratches;
  237. if (ipcomp6_scratch_users++)
  238. return ipcomp6_scratches;
  239. scratches = alloc_percpu(void *);
  240. if (!scratches)
  241. return NULL;
  242. ipcomp6_scratches = scratches;
  243. for_each_possible_cpu(i) {
  244. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  245. if (!scratch)
  246. return NULL;
  247. *per_cpu_ptr(scratches, i) = scratch;
  248. }
  249. return scratches;
  250. }
  251. static void ipcomp6_free_tfms(struct crypto_tfm **tfms)
  252. {
  253. struct ipcomp6_tfms *pos;
  254. int cpu;
  255. list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
  256. if (pos->tfms == tfms)
  257. break;
  258. }
  259. BUG_TRAP(pos);
  260. if (--pos->users)
  261. return;
  262. list_del(&pos->list);
  263. kfree(pos);
  264. if (!tfms)
  265. return;
  266. for_each_possible_cpu(cpu) {
  267. struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
  268. crypto_free_tfm(tfm);
  269. }
  270. free_percpu(tfms);
  271. }
  272. static struct crypto_tfm **ipcomp6_alloc_tfms(const char *alg_name)
  273. {
  274. struct ipcomp6_tfms *pos;
  275. struct crypto_tfm **tfms;
  276. int cpu;
  277. /* This can be any valid CPU ID so we don't need locking. */
  278. cpu = raw_smp_processor_id();
  279. list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
  280. struct crypto_tfm *tfm;
  281. tfms = pos->tfms;
  282. tfm = *per_cpu_ptr(tfms, cpu);
  283. if (!strcmp(crypto_tfm_alg_name(tfm), alg_name)) {
  284. pos->users++;
  285. return tfms;
  286. }
  287. }
  288. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  289. if (!pos)
  290. return NULL;
  291. pos->users = 1;
  292. INIT_LIST_HEAD(&pos->list);
  293. list_add(&pos->list, &ipcomp6_tfms_list);
  294. pos->tfms = tfms = alloc_percpu(struct crypto_tfm *);
  295. if (!tfms)
  296. goto error;
  297. for_each_possible_cpu(cpu) {
  298. struct crypto_tfm *tfm = crypto_alloc_tfm(alg_name, 0);
  299. if (!tfm)
  300. goto error;
  301. *per_cpu_ptr(tfms, cpu) = tfm;
  302. }
  303. return tfms;
  304. error:
  305. ipcomp6_free_tfms(tfms);
  306. return NULL;
  307. }
  308. static void ipcomp6_free_data(struct ipcomp_data *ipcd)
  309. {
  310. if (ipcd->tfms)
  311. ipcomp6_free_tfms(ipcd->tfms);
  312. ipcomp6_free_scratches();
  313. }
  314. static void ipcomp6_destroy(struct xfrm_state *x)
  315. {
  316. struct ipcomp_data *ipcd = x->data;
  317. if (!ipcd)
  318. return;
  319. xfrm_state_delete_tunnel(x);
  320. mutex_lock(&ipcomp6_resource_mutex);
  321. ipcomp6_free_data(ipcd);
  322. mutex_unlock(&ipcomp6_resource_mutex);
  323. kfree(ipcd);
  324. xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
  325. }
  326. static int ipcomp6_init_state(struct xfrm_state *x)
  327. {
  328. int err;
  329. struct ipcomp_data *ipcd;
  330. struct xfrm_algo_desc *calg_desc;
  331. err = -EINVAL;
  332. if (!x->calg)
  333. goto out;
  334. if (x->encap)
  335. goto out;
  336. err = -ENOMEM;
  337. ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
  338. if (!ipcd)
  339. goto out;
  340. x->props.header_len = 0;
  341. if (x->props.mode)
  342. x->props.header_len += sizeof(struct ipv6hdr);
  343. mutex_lock(&ipcomp6_resource_mutex);
  344. if (!ipcomp6_alloc_scratches())
  345. goto error;
  346. ipcd->tfms = ipcomp6_alloc_tfms(x->calg->alg_name);
  347. if (!ipcd->tfms)
  348. goto error;
  349. mutex_unlock(&ipcomp6_resource_mutex);
  350. if (x->props.mode) {
  351. err = ipcomp6_tunnel_attach(x);
  352. if (err)
  353. goto error_tunnel;
  354. }
  355. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  356. BUG_ON(!calg_desc);
  357. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  358. x->data = ipcd;
  359. err = 0;
  360. out:
  361. return err;
  362. error_tunnel:
  363. mutex_lock(&ipcomp6_resource_mutex);
  364. error:
  365. ipcomp6_free_data(ipcd);
  366. mutex_unlock(&ipcomp6_resource_mutex);
  367. kfree(ipcd);
  368. goto out;
  369. }
  370. static struct xfrm_type ipcomp6_type =
  371. {
  372. .description = "IPCOMP6",
  373. .owner = THIS_MODULE,
  374. .proto = IPPROTO_COMP,
  375. .init_state = ipcomp6_init_state,
  376. .destructor = ipcomp6_destroy,
  377. .input = ipcomp6_input,
  378. .output = ipcomp6_output,
  379. };
  380. static struct inet6_protocol ipcomp6_protocol =
  381. {
  382. .handler = xfrm6_rcv,
  383. .err_handler = ipcomp6_err,
  384. .flags = INET6_PROTO_NOPOLICY,
  385. };
  386. static int __init ipcomp6_init(void)
  387. {
  388. if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
  389. printk(KERN_INFO "ipcomp6 init: can't add xfrm type\n");
  390. return -EAGAIN;
  391. }
  392. if (inet6_add_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
  393. printk(KERN_INFO "ipcomp6 init: can't add protocol\n");
  394. xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  395. return -EAGAIN;
  396. }
  397. return 0;
  398. }
  399. static void __exit ipcomp6_fini(void)
  400. {
  401. if (inet6_del_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0)
  402. printk(KERN_INFO "ipv6 ipcomp close: can't remove protocol\n");
  403. if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
  404. printk(KERN_INFO "ipv6 ipcomp close: can't remove xfrm type\n");
  405. }
  406. module_init(ipcomp6_init);
  407. module_exit(ipcomp6_fini);
  408. MODULE_LICENSE("GPL");
  409. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
  410. MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");