esp4.c 11 KB

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