esp4.c 11 KB

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