esp4.c 17 KB

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