esp4.c 12 KB

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