esp4.c 11 KB

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