ipcomp6.c 11 KB

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