esp4.c 13 KB

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