esp4.c 13 KB

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