ipcomp6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/semaphore.h>
  38. #include <linux/crypto.h>
  39. #include <linux/err.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_comp **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 nexthdr;
  65. int err = -ENOMEM;
  66. struct ip_comp_hdr *ipch;
  67. int plen, dlen;
  68. struct ipcomp_data *ipcd = x->data;
  69. u8 *start, *scratch;
  70. struct crypto_comp *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. ipch = (void *)skb->data;
  77. nexthdr = ipch->nexthdr;
  78. skb->transport_header = skb->network_header + 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. goto out_put_cpu;
  90. if (dlen < (plen + sizeof(*ipch))) {
  91. err = -EINVAL;
  92. goto out_put_cpu;
  93. }
  94. err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
  95. if (err) {
  96. goto out_put_cpu;
  97. }
  98. skb->truesize += dlen - plen;
  99. __skb_put(skb, dlen - plen);
  100. skb_copy_to_linear_data(skb, scratch, dlen);
  101. err = nexthdr;
  102. out_put_cpu:
  103. put_cpu();
  104. out:
  105. return err;
  106. }
  107. static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
  108. {
  109. int err;
  110. struct ip_comp_hdr *ipch;
  111. struct ipcomp_data *ipcd = x->data;
  112. int plen, dlen;
  113. u8 *start, *scratch;
  114. struct crypto_comp *tfm;
  115. int cpu;
  116. /* check whether datagram len is larger than threshold */
  117. if (skb->len < ipcd->threshold) {
  118. goto out_ok;
  119. }
  120. if (skb_linearize_cow(skb))
  121. goto out_ok;
  122. /* compression */
  123. plen = skb->len;
  124. dlen = IPCOMP_SCRATCH_SIZE;
  125. start = skb->data;
  126. cpu = get_cpu();
  127. scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
  128. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  129. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  130. if (err || (dlen + sizeof(*ipch)) >= plen) {
  131. put_cpu();
  132. goto out_ok;
  133. }
  134. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  135. put_cpu();
  136. pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
  137. /* insert ipcomp header and replace datagram */
  138. ipch = ip_comp_hdr(skb);
  139. ipch->nexthdr = *skb_mac_header(skb);
  140. ipch->flags = 0;
  141. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  142. *skb_mac_header(skb) = IPPROTO_COMP;
  143. out_ok:
  144. skb_push(skb, -skb_network_offset(skb));
  145. return 0;
  146. }
  147. static void ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  148. int type, int code, int offset, __be32 info)
  149. {
  150. __be32 spi;
  151. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  152. struct ip_comp_hdr *ipcomph =
  153. (struct ip_comp_hdr *)(skb->data + offset);
  154. struct xfrm_state *x;
  155. if (type != ICMPV6_DEST_UNREACH && type != ICMPV6_PKT_TOOBIG)
  156. return;
  157. spi = htonl(ntohs(ipcomph->cpi));
  158. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
  159. if (!x)
  160. return;
  161. printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/" NIP6_FMT "\n",
  162. spi, NIP6(iph->daddr));
  163. xfrm_state_put(x);
  164. }
  165. static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  166. {
  167. struct xfrm_state *t = NULL;
  168. t = xfrm_state_alloc();
  169. if (!t)
  170. goto out;
  171. t->id.proto = IPPROTO_IPV6;
  172. t->id.spi = xfrm6_tunnel_alloc_spi((xfrm_address_t *)&x->props.saddr);
  173. if (!t->id.spi)
  174. goto error;
  175. memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
  176. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  177. t->props.family = AF_INET6;
  178. t->props.mode = x->props.mode;
  179. memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
  180. if (xfrm_init_state(t))
  181. goto error;
  182. atomic_set(&t->tunnel_users, 1);
  183. out:
  184. return t;
  185. error:
  186. t->km.state = XFRM_STATE_DEAD;
  187. xfrm_state_put(t);
  188. t = NULL;
  189. goto out;
  190. }
  191. static int ipcomp6_tunnel_attach(struct xfrm_state *x)
  192. {
  193. int err = 0;
  194. struct xfrm_state *t = NULL;
  195. __be32 spi;
  196. spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&x->props.saddr);
  197. if (spi)
  198. t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr,
  199. spi, IPPROTO_IPV6, AF_INET6);
  200. if (!t) {
  201. t = ipcomp6_tunnel_create(x);
  202. if (!t) {
  203. err = -EINVAL;
  204. goto out;
  205. }
  206. xfrm_state_insert(t);
  207. xfrm_state_hold(t);
  208. }
  209. x->tunnel = t;
  210. atomic_inc(&t->tunnel_users);
  211. out:
  212. return err;
  213. }
  214. static void ipcomp6_free_scratches(void)
  215. {
  216. int i;
  217. void **scratches;
  218. if (--ipcomp6_scratch_users)
  219. return;
  220. scratches = ipcomp6_scratches;
  221. if (!scratches)
  222. return;
  223. for_each_possible_cpu(i) {
  224. void *scratch = *per_cpu_ptr(scratches, i);
  225. vfree(scratch);
  226. }
  227. free_percpu(scratches);
  228. }
  229. static void **ipcomp6_alloc_scratches(void)
  230. {
  231. int i;
  232. void **scratches;
  233. if (ipcomp6_scratch_users++)
  234. return ipcomp6_scratches;
  235. scratches = alloc_percpu(void *);
  236. if (!scratches)
  237. return NULL;
  238. ipcomp6_scratches = scratches;
  239. for_each_possible_cpu(i) {
  240. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  241. if (!scratch)
  242. return NULL;
  243. *per_cpu_ptr(scratches, i) = scratch;
  244. }
  245. return scratches;
  246. }
  247. static void ipcomp6_free_tfms(struct crypto_comp **tfms)
  248. {
  249. struct ipcomp6_tfms *pos;
  250. int cpu;
  251. list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
  252. if (pos->tfms == tfms)
  253. break;
  254. }
  255. BUG_TRAP(pos);
  256. if (--pos->users)
  257. return;
  258. list_del(&pos->list);
  259. kfree(pos);
  260. if (!tfms)
  261. return;
  262. for_each_possible_cpu(cpu) {
  263. struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
  264. crypto_free_comp(tfm);
  265. }
  266. free_percpu(tfms);
  267. }
  268. static struct crypto_comp **ipcomp6_alloc_tfms(const char *alg_name)
  269. {
  270. struct ipcomp6_tfms *pos;
  271. struct crypto_comp **tfms;
  272. int cpu;
  273. /* This can be any valid CPU ID so we don't need locking. */
  274. cpu = raw_smp_processor_id();
  275. list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
  276. struct crypto_comp *tfm;
  277. tfms = pos->tfms;
  278. tfm = *per_cpu_ptr(tfms, cpu);
  279. if (!strcmp(crypto_comp_name(tfm), alg_name)) {
  280. pos->users++;
  281. return tfms;
  282. }
  283. }
  284. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  285. if (!pos)
  286. return NULL;
  287. pos->users = 1;
  288. INIT_LIST_HEAD(&pos->list);
  289. list_add(&pos->list, &ipcomp6_tfms_list);
  290. pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
  291. if (!tfms)
  292. goto error;
  293. for_each_possible_cpu(cpu) {
  294. struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
  295. CRYPTO_ALG_ASYNC);
  296. if (IS_ERR(tfm))
  297. goto error;
  298. *per_cpu_ptr(tfms, cpu) = tfm;
  299. }
  300. return tfms;
  301. error:
  302. ipcomp6_free_tfms(tfms);
  303. return NULL;
  304. }
  305. static void ipcomp6_free_data(struct ipcomp_data *ipcd)
  306. {
  307. if (ipcd->tfms)
  308. ipcomp6_free_tfms(ipcd->tfms);
  309. ipcomp6_free_scratches();
  310. }
  311. static void ipcomp6_destroy(struct xfrm_state *x)
  312. {
  313. struct ipcomp_data *ipcd = x->data;
  314. if (!ipcd)
  315. return;
  316. xfrm_state_delete_tunnel(x);
  317. mutex_lock(&ipcomp6_resource_mutex);
  318. ipcomp6_free_data(ipcd);
  319. mutex_unlock(&ipcomp6_resource_mutex);
  320. kfree(ipcd);
  321. xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
  322. }
  323. static int ipcomp6_init_state(struct xfrm_state *x)
  324. {
  325. int err;
  326. struct ipcomp_data *ipcd;
  327. struct xfrm_algo_desc *calg_desc;
  328. err = -EINVAL;
  329. if (!x->calg)
  330. goto out;
  331. if (x->encap)
  332. goto out;
  333. x->props.header_len = 0;
  334. switch (x->props.mode) {
  335. case XFRM_MODE_TRANSPORT:
  336. break;
  337. case XFRM_MODE_TUNNEL:
  338. x->props.header_len += sizeof(struct ipv6hdr);
  339. break;
  340. default:
  341. goto out;
  342. }
  343. err = -ENOMEM;
  344. ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
  345. if (!ipcd)
  346. goto out;
  347. mutex_lock(&ipcomp6_resource_mutex);
  348. if (!ipcomp6_alloc_scratches())
  349. goto error;
  350. ipcd->tfms = ipcomp6_alloc_tfms(x->calg->alg_name);
  351. if (!ipcd->tfms)
  352. goto error;
  353. mutex_unlock(&ipcomp6_resource_mutex);
  354. if (x->props.mode == XFRM_MODE_TUNNEL) {
  355. err = ipcomp6_tunnel_attach(x);
  356. if (err)
  357. goto error_tunnel;
  358. }
  359. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  360. BUG_ON(!calg_desc);
  361. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  362. x->data = ipcd;
  363. err = 0;
  364. out:
  365. return err;
  366. error_tunnel:
  367. mutex_lock(&ipcomp6_resource_mutex);
  368. error:
  369. ipcomp6_free_data(ipcd);
  370. mutex_unlock(&ipcomp6_resource_mutex);
  371. kfree(ipcd);
  372. goto out;
  373. }
  374. static const struct xfrm_type ipcomp6_type =
  375. {
  376. .description = "IPCOMP6",
  377. .owner = THIS_MODULE,
  378. .proto = IPPROTO_COMP,
  379. .init_state = ipcomp6_init_state,
  380. .destructor = ipcomp6_destroy,
  381. .input = ipcomp6_input,
  382. .output = ipcomp6_output,
  383. .hdr_offset = xfrm6_find_1stfragopt,
  384. };
  385. static struct inet6_protocol ipcomp6_protocol =
  386. {
  387. .handler = xfrm6_rcv,
  388. .err_handler = ipcomp6_err,
  389. .flags = INET6_PROTO_NOPOLICY,
  390. };
  391. static int __init ipcomp6_init(void)
  392. {
  393. if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
  394. printk(KERN_INFO "ipcomp6 init: can't add xfrm type\n");
  395. return -EAGAIN;
  396. }
  397. if (inet6_add_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
  398. printk(KERN_INFO "ipcomp6 init: can't add protocol\n");
  399. xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  400. return -EAGAIN;
  401. }
  402. return 0;
  403. }
  404. static void __exit ipcomp6_fini(void)
  405. {
  406. if (inet6_del_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0)
  407. printk(KERN_INFO "ipv6 ipcomp close: can't remove protocol\n");
  408. if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
  409. printk(KERN_INFO "ipv6 ipcomp close: can't remove xfrm type\n");
  410. }
  411. module_init(ipcomp6_init);
  412. module_exit(ipcomp6_fini);
  413. MODULE_LICENSE("GPL");
  414. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
  415. MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
  416. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);