esp4.c 11 KB

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