esp4.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. 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. err = -ENOMEM;
  150. goto error;
  151. }
  152. seqhi = esp_tmp_seqhi(tmp);
  153. iv = esp_tmp_iv(aead, tmp, seqhilen);
  154. req = esp_tmp_givreq(aead, iv);
  155. asg = esp_givreq_sg(aead, req);
  156. sg = asg + sglists;
  157. /* Fill padding... */
  158. tail = skb_tail_pointer(trailer);
  159. if (tfclen) {
  160. memset(tail, 0, tfclen);
  161. tail += tfclen;
  162. }
  163. do {
  164. int i;
  165. for (i = 0; i < plen - 2; i++)
  166. tail[i] = i + 1;
  167. } while (0);
  168. tail[plen - 2] = plen - 2;
  169. tail[plen - 1] = *skb_mac_header(skb);
  170. pskb_put(skb, trailer, clen - skb->len + alen);
  171. skb_push(skb, -skb_network_offset(skb));
  172. esph = ip_esp_hdr(skb);
  173. *skb_mac_header(skb) = IPPROTO_ESP;
  174. /* this is non-NULL only with UDP Encapsulation */
  175. if (x->encap) {
  176. struct xfrm_encap_tmpl *encap = x->encap;
  177. struct udphdr *uh;
  178. __be32 *udpdata32;
  179. __be16 sport, dport;
  180. int encap_type;
  181. spin_lock_bh(&x->lock);
  182. sport = encap->encap_sport;
  183. dport = encap->encap_dport;
  184. encap_type = encap->encap_type;
  185. spin_unlock_bh(&x->lock);
  186. uh = (struct udphdr *)esph;
  187. uh->source = sport;
  188. uh->dest = dport;
  189. uh->len = htons(skb->len - skb_transport_offset(skb));
  190. uh->check = 0;
  191. switch (encap_type) {
  192. default:
  193. case UDP_ENCAP_ESPINUDP:
  194. esph = (struct ip_esp_hdr *)(uh + 1);
  195. break;
  196. case UDP_ENCAP_ESPINUDP_NON_IKE:
  197. udpdata32 = (__be32 *)(uh + 1);
  198. udpdata32[0] = udpdata32[1] = 0;
  199. esph = (struct ip_esp_hdr *)(udpdata32 + 2);
  200. break;
  201. }
  202. *skb_mac_header(skb) = IPPROTO_UDP;
  203. }
  204. esph->spi = x->id.spi;
  205. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  206. sg_init_table(sg, nfrags);
  207. skb_to_sgvec(skb, sg,
  208. esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
  209. clen + alen);
  210. if ((x->props.flags & XFRM_STATE_ESN)) {
  211. sg_init_table(asg, 3);
  212. sg_set_buf(asg, &esph->spi, sizeof(__be32));
  213. *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  214. sg_set_buf(asg + 1, seqhi, seqhilen);
  215. sg_set_buf(asg + 2, &esph->seq_no, sizeof(__be32));
  216. } else
  217. sg_init_one(asg, esph, sizeof(*esph));
  218. aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
  219. aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
  220. aead_givcrypt_set_assoc(req, asg, assoclen);
  221. aead_givcrypt_set_giv(req, esph->enc_data,
  222. XFRM_SKB_CB(skb)->seq.output.low);
  223. ESP_SKB_CB(skb)->tmp = tmp;
  224. err = crypto_aead_givencrypt(req);
  225. if (err == -EINPROGRESS)
  226. goto error;
  227. if (err == -EBUSY)
  228. err = NET_XMIT_DROP;
  229. kfree(tmp);
  230. error:
  231. return err;
  232. }
  233. static int esp_input_done2(struct sk_buff *skb, int err)
  234. {
  235. const struct iphdr *iph;
  236. struct xfrm_state *x = xfrm_input_state(skb);
  237. struct esp_data *esp = x->data;
  238. struct crypto_aead *aead = esp->aead;
  239. int alen = crypto_aead_authsize(aead);
  240. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  241. int elen = skb->len - hlen;
  242. int ihl;
  243. u8 nexthdr[2];
  244. int padlen;
  245. kfree(ESP_SKB_CB(skb)->tmp);
  246. if (unlikely(err))
  247. goto out;
  248. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  249. BUG();
  250. err = -EINVAL;
  251. padlen = nexthdr[0];
  252. if (padlen + 2 + alen >= elen)
  253. goto out;
  254. /* ... check padding bits here. Silly. :-) */
  255. iph = ip_hdr(skb);
  256. ihl = iph->ihl * 4;
  257. if (x->encap) {
  258. struct xfrm_encap_tmpl *encap = x->encap;
  259. struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
  260. /*
  261. * 1) if the NAT-T peer's IP or port changed then
  262. * advertize the change to the keying daemon.
  263. * This is an inbound SA, so just compare
  264. * SRC ports.
  265. */
  266. if (iph->saddr != x->props.saddr.a4 ||
  267. uh->source != encap->encap_sport) {
  268. xfrm_address_t ipaddr;
  269. ipaddr.a4 = iph->saddr;
  270. km_new_mapping(x, &ipaddr, uh->source);
  271. /* XXX: perhaps add an extra
  272. * policy check here, to see
  273. * if we should allow or
  274. * reject a packet from a
  275. * different source
  276. * address/port.
  277. */
  278. }
  279. /*
  280. * 2) ignore UDP/TCP checksums in case
  281. * of NAT-T in Transport Mode, or
  282. * perform other post-processing fixes
  283. * as per draft-ietf-ipsec-udp-encaps-06,
  284. * section 3.1.2
  285. */
  286. if (x->props.mode == XFRM_MODE_TRANSPORT)
  287. skb->ip_summed = CHECKSUM_UNNECESSARY;
  288. }
  289. pskb_trim(skb, skb->len - alen - padlen - 2);
  290. __skb_pull(skb, hlen);
  291. skb_set_transport_header(skb, -ihl);
  292. err = nexthdr[1];
  293. /* RFC4303: Drop dummy packets without any error */
  294. if (err == IPPROTO_NONE)
  295. err = -EINVAL;
  296. out:
  297. return err;
  298. }
  299. static void esp_input_done(struct crypto_async_request *base, int err)
  300. {
  301. struct sk_buff *skb = base->data;
  302. xfrm_input_resume(skb, esp_input_done2(skb, err));
  303. }
  304. /*
  305. * Note: detecting truncated vs. non-truncated authentication data is very
  306. * expensive, so we only support truncated data, which is the recommended
  307. * and common case.
  308. */
  309. static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
  310. {
  311. struct ip_esp_hdr *esph;
  312. struct esp_data *esp = x->data;
  313. struct crypto_aead *aead = esp->aead;
  314. struct aead_request *req;
  315. struct sk_buff *trailer;
  316. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  317. int nfrags;
  318. int assoclen;
  319. int sglists;
  320. int seqhilen;
  321. __be32 *seqhi;
  322. void *tmp;
  323. u8 *iv;
  324. struct scatterlist *sg;
  325. struct scatterlist *asg;
  326. int err = -EINVAL;
  327. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
  328. goto out;
  329. if (elen <= 0)
  330. goto out;
  331. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  332. goto out;
  333. nfrags = err;
  334. assoclen = sizeof(*esph);
  335. sglists = 1;
  336. seqhilen = 0;
  337. if (x->props.flags & XFRM_STATE_ESN) {
  338. sglists += 2;
  339. seqhilen += sizeof(__be32);
  340. assoclen += seqhilen;
  341. }
  342. err = -ENOMEM;
  343. tmp = esp_alloc_tmp(aead, nfrags + sglists, seqhilen);
  344. if (!tmp)
  345. goto out;
  346. ESP_SKB_CB(skb)->tmp = tmp;
  347. seqhi = esp_tmp_seqhi(tmp);
  348. iv = esp_tmp_iv(aead, tmp, seqhilen);
  349. req = esp_tmp_req(aead, iv);
  350. asg = esp_req_sg(aead, req);
  351. sg = asg + sglists;
  352. skb->ip_summed = CHECKSUM_NONE;
  353. esph = (struct ip_esp_hdr *)skb->data;
  354. /* Get ivec. This can be wrong, check against another impls. */
  355. iv = esph->enc_data;
  356. sg_init_table(sg, nfrags);
  357. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  358. if ((x->props.flags & XFRM_STATE_ESN)) {
  359. sg_init_table(asg, 3);
  360. sg_set_buf(asg, &esph->spi, sizeof(__be32));
  361. *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
  362. sg_set_buf(asg + 1, seqhi, seqhilen);
  363. sg_set_buf(asg + 2, &esph->seq_no, sizeof(__be32));
  364. } else
  365. sg_init_one(asg, esph, sizeof(*esph));
  366. aead_request_set_callback(req, 0, esp_input_done, skb);
  367. aead_request_set_crypt(req, sg, sg, elen, iv);
  368. aead_request_set_assoc(req, asg, assoclen);
  369. err = crypto_aead_decrypt(req);
  370. if (err == -EINPROGRESS)
  371. goto out;
  372. err = esp_input_done2(skb, err);
  373. out:
  374. return err;
  375. }
  376. static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
  377. {
  378. struct esp_data *esp = x->data;
  379. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  380. u32 align = max_t(u32, blksize, esp->padlen);
  381. unsigned int net_adj;
  382. switch (x->props.mode) {
  383. case XFRM_MODE_TRANSPORT:
  384. case XFRM_MODE_BEET:
  385. net_adj = sizeof(struct iphdr);
  386. break;
  387. case XFRM_MODE_TUNNEL:
  388. net_adj = 0;
  389. break;
  390. default:
  391. BUG();
  392. }
  393. return ((mtu - x->props.header_len - crypto_aead_authsize(esp->aead) -
  394. net_adj) & ~(align - 1)) + (net_adj - 2);
  395. }
  396. static void esp4_err(struct sk_buff *skb, u32 info)
  397. {
  398. struct net *net = dev_net(skb->dev);
  399. const struct iphdr *iph = (const struct iphdr *)skb->data;
  400. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  401. struct xfrm_state *x;
  402. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  403. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  404. return;
  405. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  406. esph->spi, IPPROTO_ESP, AF_INET);
  407. if (!x)
  408. return;
  409. NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
  410. ntohl(esph->spi), ntohl(iph->daddr));
  411. ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ESP, 0);
  412. xfrm_state_put(x);
  413. }
  414. static void esp_destroy(struct xfrm_state *x)
  415. {
  416. struct esp_data *esp = x->data;
  417. if (!esp)
  418. return;
  419. crypto_free_aead(esp->aead);
  420. kfree(esp);
  421. }
  422. static int esp_init_aead(struct xfrm_state *x)
  423. {
  424. struct esp_data *esp = x->data;
  425. struct crypto_aead *aead;
  426. int err;
  427. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  428. err = PTR_ERR(aead);
  429. if (IS_ERR(aead))
  430. goto error;
  431. esp->aead = aead;
  432. err = crypto_aead_setkey(aead, x->aead->alg_key,
  433. (x->aead->alg_key_len + 7) / 8);
  434. if (err)
  435. goto error;
  436. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  437. if (err)
  438. goto error;
  439. error:
  440. return err;
  441. }
  442. static int esp_init_authenc(struct xfrm_state *x)
  443. {
  444. struct esp_data *esp = x->data;
  445. struct crypto_aead *aead;
  446. struct crypto_authenc_key_param *param;
  447. struct rtattr *rta;
  448. char *key;
  449. char *p;
  450. char authenc_name[CRYPTO_MAX_ALG_NAME];
  451. unsigned int keylen;
  452. int err;
  453. err = -EINVAL;
  454. if (x->ealg == NULL)
  455. goto error;
  456. err = -ENAMETOOLONG;
  457. if ((x->props.flags & XFRM_STATE_ESN)) {
  458. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  459. "authencesn(%s,%s)",
  460. x->aalg ? x->aalg->alg_name : "digest_null",
  461. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  462. goto error;
  463. } else {
  464. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  465. "authenc(%s,%s)",
  466. x->aalg ? x->aalg->alg_name : "digest_null",
  467. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  468. goto error;
  469. }
  470. aead = crypto_alloc_aead(authenc_name, 0, 0);
  471. err = PTR_ERR(aead);
  472. if (IS_ERR(aead))
  473. goto error;
  474. esp->aead = aead;
  475. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  476. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  477. err = -ENOMEM;
  478. key = kmalloc(keylen, GFP_KERNEL);
  479. if (!key)
  480. goto error;
  481. p = key;
  482. rta = (void *)p;
  483. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  484. rta->rta_len = RTA_LENGTH(sizeof(*param));
  485. param = RTA_DATA(rta);
  486. p += RTA_SPACE(sizeof(*param));
  487. if (x->aalg) {
  488. struct xfrm_algo_desc *aalg_desc;
  489. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  490. p += (x->aalg->alg_key_len + 7) / 8;
  491. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  492. BUG_ON(!aalg_desc);
  493. err = -EINVAL;
  494. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  495. crypto_aead_authsize(aead)) {
  496. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  497. x->aalg->alg_name,
  498. crypto_aead_authsize(aead),
  499. aalg_desc->uinfo.auth.icv_fullbits/8);
  500. goto free_key;
  501. }
  502. err = crypto_aead_setauthsize(
  503. aead, x->aalg->alg_trunc_len / 8);
  504. if (err)
  505. goto free_key;
  506. }
  507. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  508. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  509. err = crypto_aead_setkey(aead, key, keylen);
  510. free_key:
  511. kfree(key);
  512. error:
  513. return err;
  514. }
  515. static int esp_init_state(struct xfrm_state *x)
  516. {
  517. struct esp_data *esp;
  518. struct crypto_aead *aead;
  519. u32 align;
  520. int err;
  521. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  522. if (esp == NULL)
  523. return -ENOMEM;
  524. x->data = esp;
  525. if (x->aead)
  526. err = esp_init_aead(x);
  527. else
  528. err = esp_init_authenc(x);
  529. if (err)
  530. goto error;
  531. aead = esp->aead;
  532. esp->padlen = 0;
  533. x->props.header_len = sizeof(struct ip_esp_hdr) +
  534. crypto_aead_ivsize(aead);
  535. if (x->props.mode == XFRM_MODE_TUNNEL)
  536. x->props.header_len += sizeof(struct iphdr);
  537. else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
  538. x->props.header_len += IPV4_BEET_PHMAXLEN;
  539. if (x->encap) {
  540. struct xfrm_encap_tmpl *encap = x->encap;
  541. switch (encap->encap_type) {
  542. default:
  543. goto error;
  544. case UDP_ENCAP_ESPINUDP:
  545. x->props.header_len += sizeof(struct udphdr);
  546. break;
  547. case UDP_ENCAP_ESPINUDP_NON_IKE:
  548. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  549. break;
  550. }
  551. }
  552. align = ALIGN(crypto_aead_blocksize(aead), 4);
  553. if (esp->padlen)
  554. align = max_t(u32, align, esp->padlen);
  555. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  556. error:
  557. return err;
  558. }
  559. static const struct xfrm_type esp_type =
  560. {
  561. .description = "ESP4",
  562. .owner = THIS_MODULE,
  563. .proto = IPPROTO_ESP,
  564. .flags = XFRM_TYPE_REPLAY_PROT,
  565. .init_state = esp_init_state,
  566. .destructor = esp_destroy,
  567. .get_mtu = esp4_get_mtu,
  568. .input = esp_input,
  569. .output = esp_output
  570. };
  571. static const struct net_protocol esp4_protocol = {
  572. .handler = xfrm4_rcv,
  573. .err_handler = esp4_err,
  574. .no_policy = 1,
  575. .netns_ok = 1,
  576. };
  577. static int __init esp4_init(void)
  578. {
  579. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  580. pr_info("%s: can't add xfrm type\n", __func__);
  581. return -EAGAIN;
  582. }
  583. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  584. pr_info("%s: can't add protocol\n", __func__);
  585. xfrm_unregister_type(&esp_type, AF_INET);
  586. return -EAGAIN;
  587. }
  588. return 0;
  589. }
  590. static void __exit esp4_fini(void)
  591. {
  592. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  593. pr_info("%s: can't remove protocol\n", __func__);
  594. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  595. pr_info("%s: can't remove xfrm type\n", __func__);
  596. }
  597. module_init(esp4_init);
  598. module_exit(esp4_fini);
  599. MODULE_LICENSE("GPL");
  600. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);