esp4.c 12 KB

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