ipcomp6.c 11 KB

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