esp4.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #include <crypto/aead.h>
  2. #include <crypto/authenc.h>
  3. #include <linux/err.h>
  4. #include <linux/module.h>
  5. #include <net/ip.h>
  6. #include <net/xfrm.h>
  7. #include <net/esp.h>
  8. #include <linux/scatterlist.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pfkeyv2.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/in6.h>
  15. #include <net/icmp.h>
  16. #include <net/protocol.h>
  17. #include <net/udp.h>
  18. struct esp_skb_cb {
  19. struct xfrm_skb_cb xfrm;
  20. void *tmp;
  21. };
  22. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  23. static u32 esp4_get_mtu(struct xfrm_state *x, int mtu);
  24. /*
  25. * Allocate an AEAD request structure with extra space for SG and IV.
  26. *
  27. * For alignment considerations the IV is placed at the front, followed
  28. * by the request and finally the SG list.
  29. *
  30. * TODO: Use spare space in skb for this where possible.
  31. */
  32. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int seqhilen)
  33. {
  34. unsigned int len;
  35. len = seqhilen;
  36. len += crypto_aead_ivsize(aead);
  37. if (len) {
  38. len += crypto_aead_alignmask(aead) &
  39. ~(crypto_tfm_ctx_alignment() - 1);
  40. len = ALIGN(len, crypto_tfm_ctx_alignment());
  41. }
  42. len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
  43. len = ALIGN(len, __alignof__(struct scatterlist));
  44. len += sizeof(struct scatterlist) * nfrags;
  45. return kmalloc(len, GFP_ATOMIC);
  46. }
  47. static inline __be32 *esp_tmp_seqhi(void *tmp)
  48. {
  49. return PTR_ALIGN((__be32 *)tmp, __alignof__(__be32));
  50. }
  51. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int seqhilen)
  52. {
  53. return crypto_aead_ivsize(aead) ?
  54. PTR_ALIGN((u8 *)tmp + seqhilen,
  55. crypto_aead_alignmask(aead) + 1) : tmp + seqhilen;
  56. }
  57. static inline struct aead_givcrypt_request *esp_tmp_givreq(
  58. struct crypto_aead *aead, u8 *iv)
  59. {
  60. struct aead_givcrypt_request *req;
  61. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  62. crypto_tfm_ctx_alignment());
  63. aead_givcrypt_set_tfm(req, aead);
  64. return req;
  65. }
  66. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  67. {
  68. struct aead_request *req;
  69. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  70. crypto_tfm_ctx_alignment());
  71. aead_request_set_tfm(req, aead);
  72. return req;
  73. }
  74. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  75. struct aead_request *req)
  76. {
  77. return (void *)ALIGN((unsigned long)(req + 1) +
  78. crypto_aead_reqsize(aead),
  79. __alignof__(struct scatterlist));
  80. }
  81. static inline struct scatterlist *esp_givreq_sg(
  82. struct crypto_aead *aead, struct aead_givcrypt_request *req)
  83. {
  84. return (void *)ALIGN((unsigned long)(req + 1) +
  85. crypto_aead_reqsize(aead),
  86. __alignof__(struct scatterlist));
  87. }
  88. static void esp_output_done(struct crypto_async_request *base, int err)
  89. {
  90. struct sk_buff *skb = base->data;
  91. kfree(ESP_SKB_CB(skb)->tmp);
  92. xfrm_output_resume(skb, err);
  93. }
  94. static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
  95. {
  96. int err;
  97. struct ip_esp_hdr *esph;
  98. struct crypto_aead *aead;
  99. struct aead_givcrypt_request *req;
  100. struct scatterlist *sg;
  101. struct scatterlist *asg;
  102. struct esp_data *esp;
  103. struct sk_buff *trailer;
  104. void *tmp;
  105. u8 *iv;
  106. u8 *tail;
  107. int blksize;
  108. int clen;
  109. int alen;
  110. int plen;
  111. int tfclen;
  112. int nfrags;
  113. int assoclen;
  114. int sglists;
  115. int seqhilen;
  116. __be32 *seqhi;
  117. /* skb is pure payload to encrypt */
  118. err = -ENOMEM;
  119. esp = x->data;
  120. aead = esp->aead;
  121. alen = crypto_aead_authsize(aead);
  122. tfclen = 0;
  123. if (x->tfcpad) {
  124. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  125. u32 padto;
  126. padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
  127. if (skb->len < padto)
  128. tfclen = padto - skb->len;
  129. }
  130. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  131. clen = ALIGN(skb->len + 2 + tfclen, blksize);
  132. if (esp->padlen)
  133. clen = ALIGN(clen, esp->padlen);
  134. plen = clen - skb->len - tfclen;
  135. err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
  136. if (err < 0)
  137. goto error;
  138. nfrags = err;
  139. assoclen = sizeof(*esph);
  140. sglists = 1;
  141. seqhilen = 0;
  142. if (x->props.flags & XFRM_STATE_ESN) {
  143. sglists += 2;
  144. seqhilen += sizeof(__be32);
  145. assoclen += seqhilen;
  146. }
  147. tmp = esp_alloc_tmp(aead, nfrags + sglists, seqhilen);
  148. if (!tmp)
  149. goto error;
  150. seqhi = esp_tmp_seqhi(tmp);
  151. iv = esp_tmp_iv(aead, tmp, seqhilen);
  152. req = esp_tmp_givreq(aead, iv);
  153. asg = esp_givreq_sg(aead, req);
  154. sg = asg + sglists;
  155. /* Fill padding... */
  156. tail = skb_tail_pointer(trailer);
  157. if (tfclen) {
  158. memset(tail, 0, tfclen);
  159. tail += tfclen;
  160. }
  161. do {
  162. int i;
  163. for (i = 0; i < plen - 2; i++)
  164. tail[i] = i + 1;
  165. } while (0);
  166. tail[plen - 2] = plen - 2;
  167. tail[plen - 1] = *skb_mac_header(skb);
  168. pskb_put(skb, trailer, clen - skb->len + alen);
  169. skb_push(skb, -skb_network_offset(skb));
  170. esph = ip_esp_hdr(skb);
  171. *skb_mac_header(skb) = IPPROTO_ESP;
  172. /* this is non-NULL only with UDP Encapsulation */
  173. if (x->encap) {
  174. struct xfrm_encap_tmpl *encap = x->encap;
  175. struct udphdr *uh;
  176. __be32 *udpdata32;
  177. __be16 sport, dport;
  178. int encap_type;
  179. spin_lock_bh(&x->lock);
  180. sport = encap->encap_sport;
  181. dport = encap->encap_dport;
  182. encap_type = encap->encap_type;
  183. spin_unlock_bh(&x->lock);
  184. uh = (struct udphdr *)esph;
  185. uh->source = sport;
  186. uh->dest = dport;
  187. uh->len = htons(skb->len - skb_transport_offset(skb));
  188. uh->check = 0;
  189. switch (encap_type) {
  190. default:
  191. case UDP_ENCAP_ESPINUDP:
  192. esph = (struct ip_esp_hdr *)(uh + 1);
  193. break;
  194. case UDP_ENCAP_ESPINUDP_NON_IKE:
  195. udpdata32 = (__be32 *)(uh + 1);
  196. udpdata32[0] = udpdata32[1] = 0;
  197. esph = (struct ip_esp_hdr *)(udpdata32 + 2);
  198. break;
  199. }
  200. *skb_mac_header(skb) = IPPROTO_UDP;
  201. }
  202. esph->spi = x->id.spi;
  203. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  204. sg_init_table(sg, nfrags);
  205. skb_to_sgvec(skb, sg,
  206. esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
  207. clen + alen);
  208. if ((x->props.flags & XFRM_STATE_ESN)) {
  209. sg_init_table(asg, 3);
  210. sg_set_buf(asg, &esph->spi, sizeof(__be32));
  211. *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  212. sg_set_buf(asg + 1, seqhi, seqhilen);
  213. sg_set_buf(asg + 2, &esph->seq_no, sizeof(__be32));
  214. } else
  215. sg_init_one(asg, esph, sizeof(*esph));
  216. aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
  217. aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
  218. aead_givcrypt_set_assoc(req, asg, assoclen);
  219. aead_givcrypt_set_giv(req, esph->enc_data,
  220. XFRM_SKB_CB(skb)->seq.output.low);
  221. ESP_SKB_CB(skb)->tmp = tmp;
  222. err = crypto_aead_givencrypt(req);
  223. if (err == -EINPROGRESS)
  224. goto error;
  225. if (err == -EBUSY)
  226. err = NET_XMIT_DROP;
  227. kfree(tmp);
  228. error:
  229. return err;
  230. }
  231. static int esp_input_done2(struct sk_buff *skb, int err)
  232. {
  233. const struct iphdr *iph;
  234. struct xfrm_state *x = xfrm_input_state(skb);
  235. struct esp_data *esp = x->data;
  236. struct crypto_aead *aead = esp->aead;
  237. int alen = crypto_aead_authsize(aead);
  238. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  239. int elen = skb->len - hlen;
  240. int ihl;
  241. u8 nexthdr[2];
  242. int padlen;
  243. kfree(ESP_SKB_CB(skb)->tmp);
  244. if (unlikely(err))
  245. goto out;
  246. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  247. BUG();
  248. err = -EINVAL;
  249. padlen = nexthdr[0];
  250. if (padlen + 2 + alen >= elen)
  251. goto out;
  252. /* ... check padding bits here. Silly. :-) */
  253. iph = ip_hdr(skb);
  254. ihl = iph->ihl * 4;
  255. if (x->encap) {
  256. struct xfrm_encap_tmpl *encap = x->encap;
  257. struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
  258. /*
  259. * 1) if the NAT-T peer's IP or port changed then
  260. * advertize the change to the keying daemon.
  261. * This is an inbound SA, so just compare
  262. * SRC ports.
  263. */
  264. if (iph->saddr != x->props.saddr.a4 ||
  265. uh->source != encap->encap_sport) {
  266. xfrm_address_t ipaddr;
  267. ipaddr.a4 = iph->saddr;
  268. km_new_mapping(x, &ipaddr, uh->source);
  269. /* XXX: perhaps add an extra
  270. * policy check here, to see
  271. * if we should allow or
  272. * reject a packet from a
  273. * different source
  274. * address/port.
  275. */
  276. }
  277. /*
  278. * 2) ignore UDP/TCP checksums in case
  279. * of NAT-T in Transport Mode, or
  280. * perform other post-processing fixes
  281. * as per draft-ietf-ipsec-udp-encaps-06,
  282. * section 3.1.2
  283. */
  284. if (x->props.mode == XFRM_MODE_TRANSPORT)
  285. skb->ip_summed = CHECKSUM_UNNECESSARY;
  286. }
  287. pskb_trim(skb, skb->len - alen - padlen - 2);
  288. __skb_pull(skb, hlen);
  289. skb_set_transport_header(skb, -ihl);
  290. err = nexthdr[1];
  291. /* RFC4303: Drop dummy packets without any error */
  292. if (err == IPPROTO_NONE)
  293. err = -EINVAL;
  294. out:
  295. return err;
  296. }
  297. static void esp_input_done(struct crypto_async_request *base, int err)
  298. {
  299. struct sk_buff *skb = base->data;
  300. xfrm_input_resume(skb, esp_input_done2(skb, err));
  301. }
  302. /*
  303. * Note: detecting truncated vs. non-truncated authentication data is very
  304. * expensive, so we only support truncated data, which is the recommended
  305. * and common case.
  306. */
  307. static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
  308. {
  309. struct ip_esp_hdr *esph;
  310. struct esp_data *esp = x->data;
  311. struct crypto_aead *aead = esp->aead;
  312. struct aead_request *req;
  313. struct sk_buff *trailer;
  314. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  315. int nfrags;
  316. int assoclen;
  317. int sglists;
  318. int seqhilen;
  319. __be32 *seqhi;
  320. void *tmp;
  321. u8 *iv;
  322. struct scatterlist *sg;
  323. struct scatterlist *asg;
  324. int err = -EINVAL;
  325. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
  326. goto out;
  327. if (elen <= 0)
  328. goto out;
  329. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  330. goto out;
  331. nfrags = err;
  332. assoclen = sizeof(*esph);
  333. sglists = 1;
  334. seqhilen = 0;
  335. if (x->props.flags & XFRM_STATE_ESN) {
  336. sglists += 2;
  337. seqhilen += sizeof(__be32);
  338. assoclen += seqhilen;
  339. }
  340. err = -ENOMEM;
  341. tmp = esp_alloc_tmp(aead, nfrags + sglists, seqhilen);
  342. if (!tmp)
  343. goto out;
  344. ESP_SKB_CB(skb)->tmp = tmp;
  345. seqhi = esp_tmp_seqhi(tmp);
  346. iv = esp_tmp_iv(aead, tmp, seqhilen);
  347. req = esp_tmp_req(aead, iv);
  348. asg = esp_req_sg(aead, req);
  349. sg = asg + sglists;
  350. skb->ip_summed = CHECKSUM_NONE;
  351. esph = (struct ip_esp_hdr *)skb->data;
  352. /* Get ivec. This can be wrong, check against another impls. */
  353. iv = esph->enc_data;
  354. sg_init_table(sg, nfrags);
  355. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  356. if ((x->props.flags & XFRM_STATE_ESN)) {
  357. sg_init_table(asg, 3);
  358. sg_set_buf(asg, &esph->spi, sizeof(__be32));
  359. *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
  360. sg_set_buf(asg + 1, seqhi, seqhilen);
  361. sg_set_buf(asg + 2, &esph->seq_no, sizeof(__be32));
  362. } else
  363. sg_init_one(asg, esph, sizeof(*esph));
  364. aead_request_set_callback(req, 0, esp_input_done, skb);
  365. aead_request_set_crypt(req, sg, sg, elen, iv);
  366. aead_request_set_assoc(req, asg, assoclen);
  367. err = crypto_aead_decrypt(req);
  368. if (err == -EINPROGRESS)
  369. goto out;
  370. err = esp_input_done2(skb, err);
  371. out:
  372. return err;
  373. }
  374. static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
  375. {
  376. struct esp_data *esp = x->data;
  377. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  378. u32 align = max_t(u32, blksize, esp->padlen);
  379. u32 rem;
  380. mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
  381. rem = mtu & (align - 1);
  382. mtu &= ~(align - 1);
  383. switch (x->props.mode) {
  384. case XFRM_MODE_TUNNEL:
  385. break;
  386. default:
  387. case XFRM_MODE_TRANSPORT:
  388. /* The worst case */
  389. mtu -= blksize - 4;
  390. mtu += min_t(u32, blksize - 4, rem);
  391. break;
  392. case XFRM_MODE_BEET:
  393. /* The worst case. */
  394. mtu += min_t(u32, IPV4_BEET_PHMAXLEN, rem);
  395. break;
  396. }
  397. return mtu - 2;
  398. }
  399. static void esp4_err(struct sk_buff *skb, u32 info)
  400. {
  401. struct net *net = dev_net(skb->dev);
  402. const struct iphdr *iph = (const struct iphdr *)skb->data;
  403. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  404. struct xfrm_state *x;
  405. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  406. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  407. return;
  408. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  409. esph->spi, IPPROTO_ESP, AF_INET);
  410. if (!x)
  411. return;
  412. NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
  413. ntohl(esph->spi), ntohl(iph->daddr));
  414. xfrm_state_put(x);
  415. }
  416. static void esp_destroy(struct xfrm_state *x)
  417. {
  418. struct esp_data *esp = x->data;
  419. if (!esp)
  420. return;
  421. crypto_free_aead(esp->aead);
  422. kfree(esp);
  423. }
  424. static int esp_init_aead(struct xfrm_state *x)
  425. {
  426. struct esp_data *esp = x->data;
  427. struct crypto_aead *aead;
  428. int err;
  429. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  430. err = PTR_ERR(aead);
  431. if (IS_ERR(aead))
  432. goto error;
  433. esp->aead = aead;
  434. err = crypto_aead_setkey(aead, x->aead->alg_key,
  435. (x->aead->alg_key_len + 7) / 8);
  436. if (err)
  437. goto error;
  438. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  439. if (err)
  440. goto error;
  441. error:
  442. return err;
  443. }
  444. static int esp_init_authenc(struct xfrm_state *x)
  445. {
  446. struct esp_data *esp = x->data;
  447. struct crypto_aead *aead;
  448. struct crypto_authenc_key_param *param;
  449. struct rtattr *rta;
  450. char *key;
  451. char *p;
  452. char authenc_name[CRYPTO_MAX_ALG_NAME];
  453. unsigned int keylen;
  454. int err;
  455. err = -EINVAL;
  456. if (x->ealg == NULL)
  457. goto error;
  458. err = -ENAMETOOLONG;
  459. if ((x->props.flags & XFRM_STATE_ESN)) {
  460. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  461. "authencesn(%s,%s)",
  462. x->aalg ? x->aalg->alg_name : "digest_null",
  463. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  464. goto error;
  465. } else {
  466. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  467. "authenc(%s,%s)",
  468. x->aalg ? x->aalg->alg_name : "digest_null",
  469. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  470. goto error;
  471. }
  472. aead = crypto_alloc_aead(authenc_name, 0, 0);
  473. err = PTR_ERR(aead);
  474. if (IS_ERR(aead))
  475. goto error;
  476. esp->aead = aead;
  477. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  478. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  479. err = -ENOMEM;
  480. key = kmalloc(keylen, GFP_KERNEL);
  481. if (!key)
  482. goto error;
  483. p = key;
  484. rta = (void *)p;
  485. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  486. rta->rta_len = RTA_LENGTH(sizeof(*param));
  487. param = RTA_DATA(rta);
  488. p += RTA_SPACE(sizeof(*param));
  489. if (x->aalg) {
  490. struct xfrm_algo_desc *aalg_desc;
  491. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  492. p += (x->aalg->alg_key_len + 7) / 8;
  493. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  494. BUG_ON(!aalg_desc);
  495. err = -EINVAL;
  496. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  497. crypto_aead_authsize(aead)) {
  498. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  499. x->aalg->alg_name,
  500. crypto_aead_authsize(aead),
  501. aalg_desc->uinfo.auth.icv_fullbits/8);
  502. goto free_key;
  503. }
  504. err = crypto_aead_setauthsize(
  505. aead, x->aalg->alg_trunc_len / 8);
  506. if (err)
  507. goto free_key;
  508. }
  509. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  510. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  511. err = crypto_aead_setkey(aead, key, keylen);
  512. free_key:
  513. kfree(key);
  514. error:
  515. return err;
  516. }
  517. static int esp_init_state(struct xfrm_state *x)
  518. {
  519. struct esp_data *esp;
  520. struct crypto_aead *aead;
  521. u32 align;
  522. int err;
  523. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  524. if (esp == NULL)
  525. return -ENOMEM;
  526. x->data = esp;
  527. if (x->aead)
  528. err = esp_init_aead(x);
  529. else
  530. err = esp_init_authenc(x);
  531. if (err)
  532. goto error;
  533. aead = esp->aead;
  534. esp->padlen = 0;
  535. x->props.header_len = sizeof(struct ip_esp_hdr) +
  536. crypto_aead_ivsize(aead);
  537. if (x->props.mode == XFRM_MODE_TUNNEL)
  538. x->props.header_len += sizeof(struct iphdr);
  539. else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
  540. x->props.header_len += IPV4_BEET_PHMAXLEN;
  541. if (x->encap) {
  542. struct xfrm_encap_tmpl *encap = x->encap;
  543. switch (encap->encap_type) {
  544. default:
  545. goto error;
  546. case UDP_ENCAP_ESPINUDP:
  547. x->props.header_len += sizeof(struct udphdr);
  548. break;
  549. case UDP_ENCAP_ESPINUDP_NON_IKE:
  550. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  551. break;
  552. }
  553. }
  554. align = ALIGN(crypto_aead_blocksize(aead), 4);
  555. if (esp->padlen)
  556. align = max_t(u32, align, esp->padlen);
  557. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  558. error:
  559. return err;
  560. }
  561. static const struct xfrm_type esp_type =
  562. {
  563. .description = "ESP4",
  564. .owner = THIS_MODULE,
  565. .proto = IPPROTO_ESP,
  566. .flags = XFRM_TYPE_REPLAY_PROT,
  567. .init_state = esp_init_state,
  568. .destructor = esp_destroy,
  569. .get_mtu = esp4_get_mtu,
  570. .input = esp_input,
  571. .output = esp_output
  572. };
  573. static const struct net_protocol esp4_protocol = {
  574. .handler = xfrm4_rcv,
  575. .err_handler = esp4_err,
  576. .no_policy = 1,
  577. .netns_ok = 1,
  578. };
  579. static int __init esp4_init(void)
  580. {
  581. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  582. printk(KERN_INFO "ip esp init: can't add xfrm type\n");
  583. return -EAGAIN;
  584. }
  585. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  586. printk(KERN_INFO "ip esp init: can't add protocol\n");
  587. xfrm_unregister_type(&esp_type, AF_INET);
  588. return -EAGAIN;
  589. }
  590. return 0;
  591. }
  592. static void __exit esp4_fini(void)
  593. {
  594. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  595. printk(KERN_INFO "ip esp close: can't remove protocol\n");
  596. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  597. printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
  598. }
  599. module_init(esp4_init);
  600. module_exit(esp4_fini);
  601. MODULE_LICENSE("GPL");
  602. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);