esp4.c 11 KB

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