esp4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <net/ip.h>
  4. #include <net/xfrm.h>
  5. #include <net/esp.h>
  6. #include <asm/scatterlist.h>
  7. #include <linux/crypto.h>
  8. #include <linux/kernel.h>
  9. #include <linux/pfkeyv2.h>
  10. #include <linux/random.h>
  11. #include <net/icmp.h>
  12. #include <net/protocol.h>
  13. #include <net/udp.h>
  14. static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
  15. {
  16. int err;
  17. struct iphdr *top_iph;
  18. struct ip_esp_hdr *esph;
  19. struct crypto_tfm *tfm;
  20. struct esp_data *esp;
  21. struct sk_buff *trailer;
  22. int blksize;
  23. int clen;
  24. int alen;
  25. int nfrags;
  26. /* Strip IP+ESP header. */
  27. __skb_pull(skb, skb->h.raw - skb->data);
  28. /* Now skb is pure payload to encrypt */
  29. err = -ENOMEM;
  30. /* Round to block size */
  31. clen = skb->len;
  32. esp = x->data;
  33. alen = esp->auth.icv_trunc_len;
  34. tfm = esp->conf.tfm;
  35. blksize = ALIGN(crypto_tfm_alg_blocksize(tfm), 4);
  36. clen = ALIGN(clen + 2, blksize);
  37. if (esp->conf.padlen)
  38. clen = ALIGN(clen, esp->conf.padlen);
  39. if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
  40. goto error;
  41. /* Fill padding... */
  42. do {
  43. int i;
  44. for (i=0; i<clen-skb->len - 2; i++)
  45. *(u8*)(trailer->tail + i) = i+1;
  46. } while (0);
  47. *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
  48. pskb_put(skb, trailer, clen - skb->len);
  49. __skb_push(skb, skb->data - skb->nh.raw);
  50. top_iph = skb->nh.iph;
  51. esph = (struct ip_esp_hdr *)(skb->nh.raw + top_iph->ihl*4);
  52. top_iph->tot_len = htons(skb->len + alen);
  53. *(u8*)(trailer->tail - 1) = top_iph->protocol;
  54. /* this is non-NULL only with UDP Encapsulation */
  55. if (x->encap) {
  56. struct xfrm_encap_tmpl *encap = x->encap;
  57. struct udphdr *uh;
  58. u32 *udpdata32;
  59. uh = (struct udphdr *)esph;
  60. uh->source = encap->encap_sport;
  61. uh->dest = encap->encap_dport;
  62. uh->len = htons(skb->len + alen - top_iph->ihl*4);
  63. uh->check = 0;
  64. switch (encap->encap_type) {
  65. default:
  66. case UDP_ENCAP_ESPINUDP:
  67. esph = (struct ip_esp_hdr *)(uh + 1);
  68. break;
  69. case UDP_ENCAP_ESPINUDP_NON_IKE:
  70. udpdata32 = (u32 *)(uh + 1);
  71. udpdata32[0] = udpdata32[1] = 0;
  72. esph = (struct ip_esp_hdr *)(udpdata32 + 2);
  73. break;
  74. }
  75. top_iph->protocol = IPPROTO_UDP;
  76. } else
  77. top_iph->protocol = IPPROTO_ESP;
  78. esph->spi = x->id.spi;
  79. esph->seq_no = htonl(++x->replay.oseq);
  80. if (esp->conf.ivlen)
  81. crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
  82. do {
  83. struct scatterlist *sg = &esp->sgbuf[0];
  84. if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
  85. sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
  86. if (!sg)
  87. goto error;
  88. }
  89. skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
  90. crypto_cipher_encrypt(tfm, sg, sg, clen);
  91. if (unlikely(sg != &esp->sgbuf[0]))
  92. kfree(sg);
  93. } while (0);
  94. if (esp->conf.ivlen) {
  95. memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
  96. crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
  97. }
  98. if (esp->auth.icv_full_len) {
  99. esp->auth.icv(esp, skb, (u8*)esph-skb->data,
  100. sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
  101. pskb_put(skb, trailer, alen);
  102. }
  103. ip_send_check(top_iph);
  104. err = 0;
  105. error:
  106. return err;
  107. }
  108. /*
  109. * Note: detecting truncated vs. non-truncated authentication data is very
  110. * expensive, so we only support truncated data, which is the recommended
  111. * and common case.
  112. */
  113. static int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
  114. {
  115. struct iphdr *iph;
  116. struct ip_esp_hdr *esph;
  117. struct esp_data *esp = x->data;
  118. struct sk_buff *trailer;
  119. int blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
  120. int alen = esp->auth.icv_trunc_len;
  121. int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
  122. int nfrags;
  123. int encap_len = 0;
  124. u8 nexthdr[2];
  125. struct scatterlist *sg;
  126. u8 workbuf[60];
  127. int padlen;
  128. if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
  129. goto out;
  130. if (elen <= 0 || (elen & (blksize-1)))
  131. goto out;
  132. /* If integrity check is required, do this. */
  133. if (esp->auth.icv_full_len) {
  134. u8 sum[esp->auth.icv_full_len];
  135. u8 sum1[alen];
  136. esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
  137. if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
  138. BUG();
  139. if (unlikely(memcmp(sum, sum1, alen))) {
  140. x->stats.integrity_failed++;
  141. goto out;
  142. }
  143. }
  144. if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
  145. goto out;
  146. skb->ip_summed = CHECKSUM_NONE;
  147. esph = (struct ip_esp_hdr*)skb->data;
  148. iph = skb->nh.iph;
  149. /* Get ivec. This can be wrong, check against another impls. */
  150. if (esp->conf.ivlen)
  151. crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
  152. sg = &esp->sgbuf[0];
  153. if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
  154. sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
  155. if (!sg)
  156. goto out;
  157. }
  158. skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
  159. crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
  160. if (unlikely(sg != &esp->sgbuf[0]))
  161. kfree(sg);
  162. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  163. BUG();
  164. padlen = nexthdr[0];
  165. if (padlen+2 >= elen)
  166. goto out;
  167. /* ... check padding bits here. Silly. :-) */
  168. if (x->encap) {
  169. struct xfrm_encap_tmpl *encap = x->encap;
  170. struct udphdr *uh;
  171. if (encap->encap_type != decap->decap_type)
  172. goto out;
  173. uh = (struct udphdr *)(iph + 1);
  174. encap_len = (void*)esph - (void*)uh;
  175. /*
  176. * 1) if the NAT-T peer's IP or port changed then
  177. * advertize the change to the keying daemon.
  178. * This is an inbound SA, so just compare
  179. * SRC ports.
  180. */
  181. if (iph->saddr != x->props.saddr.a4 ||
  182. uh->source != encap->encap_sport) {
  183. xfrm_address_t ipaddr;
  184. ipaddr.a4 = iph->saddr;
  185. km_new_mapping(x, &ipaddr, uh->source);
  186. /* XXX: perhaps add an extra
  187. * policy check here, to see
  188. * if we should allow or
  189. * reject a packet from a
  190. * different source
  191. * address/port.
  192. */
  193. }
  194. /*
  195. * 2) ignore UDP/TCP checksums in case
  196. * of NAT-T in Transport Mode, or
  197. * perform other post-processing fixes
  198. * as per draft-ietf-ipsec-udp-encaps-06,
  199. * section 3.1.2
  200. */
  201. if (!x->props.mode)
  202. skb->ip_summed = CHECKSUM_UNNECESSARY;
  203. }
  204. iph->protocol = nexthdr[1];
  205. pskb_trim(skb, skb->len - alen - padlen - 2);
  206. memcpy(workbuf, skb->nh.raw, iph->ihl*4);
  207. skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
  208. skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
  209. memcpy(skb->nh.raw, workbuf, iph->ihl*4);
  210. skb->nh.iph->tot_len = htons(skb->len);
  211. return 0;
  212. out:
  213. return -EINVAL;
  214. }
  215. static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
  216. {
  217. struct esp_data *esp = x->data;
  218. u32 blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
  219. if (x->props.mode) {
  220. mtu = ALIGN(mtu + 2, blksize);
  221. } else {
  222. /* The worst case. */
  223. mtu = ALIGN(mtu + 2, 4) + blksize - 4;
  224. }
  225. if (esp->conf.padlen)
  226. mtu = ALIGN(mtu, esp->conf.padlen);
  227. return mtu + x->props.header_len + esp->auth.icv_trunc_len;
  228. }
  229. static void esp4_err(struct sk_buff *skb, u32 info)
  230. {
  231. struct iphdr *iph = (struct iphdr*)skb->data;
  232. struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
  233. struct xfrm_state *x;
  234. if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
  235. skb->h.icmph->code != ICMP_FRAG_NEEDED)
  236. return;
  237. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
  238. if (!x)
  239. return;
  240. NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
  241. ntohl(esph->spi), ntohl(iph->daddr));
  242. xfrm_state_put(x);
  243. }
  244. static void esp_destroy(struct xfrm_state *x)
  245. {
  246. struct esp_data *esp = x->data;
  247. if (!esp)
  248. return;
  249. crypto_free_tfm(esp->conf.tfm);
  250. esp->conf.tfm = NULL;
  251. kfree(esp->conf.ivec);
  252. esp->conf.ivec = NULL;
  253. crypto_free_tfm(esp->auth.tfm);
  254. esp->auth.tfm = NULL;
  255. kfree(esp->auth.work_icv);
  256. esp->auth.work_icv = NULL;
  257. kfree(esp);
  258. }
  259. static int esp_init_state(struct xfrm_state *x)
  260. {
  261. struct esp_data *esp = NULL;
  262. /* null auth and encryption can have zero length keys */
  263. if (x->aalg) {
  264. if (x->aalg->alg_key_len > 512)
  265. goto error;
  266. }
  267. if (x->ealg == NULL)
  268. goto error;
  269. esp = kmalloc(sizeof(*esp), GFP_KERNEL);
  270. if (esp == NULL)
  271. return -ENOMEM;
  272. memset(esp, 0, sizeof(*esp));
  273. if (x->aalg) {
  274. struct xfrm_algo_desc *aalg_desc;
  275. esp->auth.key = x->aalg->alg_key;
  276. esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
  277. esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
  278. if (esp->auth.tfm == NULL)
  279. goto error;
  280. esp->auth.icv = esp_hmac_digest;
  281. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  282. BUG_ON(!aalg_desc);
  283. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  284. crypto_tfm_alg_digestsize(esp->auth.tfm)) {
  285. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  286. x->aalg->alg_name,
  287. crypto_tfm_alg_digestsize(esp->auth.tfm),
  288. aalg_desc->uinfo.auth.icv_fullbits/8);
  289. goto error;
  290. }
  291. esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  292. esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  293. esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
  294. if (!esp->auth.work_icv)
  295. goto error;
  296. }
  297. esp->conf.key = x->ealg->alg_key;
  298. esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
  299. if (x->props.ealgo == SADB_EALG_NULL)
  300. esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
  301. else
  302. esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
  303. if (esp->conf.tfm == NULL)
  304. goto error;
  305. esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
  306. esp->conf.padlen = 0;
  307. if (esp->conf.ivlen) {
  308. esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
  309. if (unlikely(esp->conf.ivec == NULL))
  310. goto error;
  311. get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
  312. }
  313. if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len))
  314. goto error;
  315. x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
  316. if (x->props.mode)
  317. x->props.header_len += sizeof(struct iphdr);
  318. if (x->encap) {
  319. struct xfrm_encap_tmpl *encap = x->encap;
  320. switch (encap->encap_type) {
  321. default:
  322. goto error;
  323. case UDP_ENCAP_ESPINUDP:
  324. x->props.header_len += sizeof(struct udphdr);
  325. break;
  326. case UDP_ENCAP_ESPINUDP_NON_IKE:
  327. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  328. break;
  329. }
  330. }
  331. x->data = esp;
  332. x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
  333. return 0;
  334. error:
  335. x->data = esp;
  336. esp_destroy(x);
  337. x->data = NULL;
  338. return -EINVAL;
  339. }
  340. static struct xfrm_type esp_type =
  341. {
  342. .description = "ESP4",
  343. .owner = THIS_MODULE,
  344. .proto = IPPROTO_ESP,
  345. .init_state = esp_init_state,
  346. .destructor = esp_destroy,
  347. .get_max_size = esp4_get_max_size,
  348. .input = esp_input,
  349. .output = esp_output
  350. };
  351. static struct net_protocol esp4_protocol = {
  352. .handler = xfrm4_rcv,
  353. .err_handler = esp4_err,
  354. .no_policy = 1,
  355. };
  356. static int __init esp4_init(void)
  357. {
  358. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  359. printk(KERN_INFO "ip esp init: can't add xfrm type\n");
  360. return -EAGAIN;
  361. }
  362. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  363. printk(KERN_INFO "ip esp init: can't add protocol\n");
  364. xfrm_unregister_type(&esp_type, AF_INET);
  365. return -EAGAIN;
  366. }
  367. return 0;
  368. }
  369. static void __exit esp4_fini(void)
  370. {
  371. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  372. printk(KERN_INFO "ip esp close: can't remove protocol\n");
  373. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  374. printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
  375. }
  376. module_init(esp4_init);
  377. module_exit(esp4_fini);
  378. MODULE_LICENSE("GPL");