esp4.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) "IPsec: " fmt
  3. #include <crypto/aead.h>
  4. #include <crypto/authenc.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <net/ip.h>
  8. #include <net/xfrm.h>
  9. #include <net/esp.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pfkeyv2.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/in6.h>
  17. #include <net/icmp.h>
  18. #include <net/protocol.h>
  19. #include <net/udp.h>
  20. #include <net/tcp.h>
  21. #include <net/espintcp.h>
  22. #include <linux/skbuff_ref.h>
  23. #include <linux/highmem.h>
  24. struct esp_skb_cb {
  25. struct xfrm_skb_cb xfrm;
  26. void *tmp;
  27. };
  28. struct esp_output_extra {
  29. __be32 seqhi;
  30. u32 esphoff;
  31. };
  32. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  33. /*
  34. * Allocate an AEAD request structure with extra space for SG and IV.
  35. *
  36. * For alignment considerations the IV is placed at the front, followed
  37. * by the request and finally the SG list.
  38. *
  39. * TODO: Use spare space in skb for this where possible.
  40. */
  41. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
  42. {
  43. unsigned int len;
  44. len = extralen;
  45. len += crypto_aead_ivsize(aead);
  46. if (len) {
  47. len += crypto_aead_alignmask(aead) &
  48. ~(crypto_tfm_ctx_alignment() - 1);
  49. len = ALIGN(len, crypto_tfm_ctx_alignment());
  50. }
  51. len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
  52. len = ALIGN(len, __alignof__(struct scatterlist));
  53. len += sizeof(struct scatterlist) * nfrags;
  54. return kmalloc(len, GFP_ATOMIC);
  55. }
  56. static inline void *esp_tmp_extra(void *tmp)
  57. {
  58. return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
  59. }
  60. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
  61. {
  62. return crypto_aead_ivsize(aead) ?
  63. PTR_ALIGN((u8 *)tmp + extralen,
  64. crypto_aead_alignmask(aead) + 1) : tmp + extralen;
  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 void esp_ssg_unref(struct xfrm_state *x, void *tmp, struct sk_buff *skb)
  82. {
  83. struct crypto_aead *aead = x->data;
  84. int extralen = 0;
  85. u8 *iv;
  86. struct aead_request *req;
  87. struct scatterlist *sg;
  88. if (x->props.flags & XFRM_STATE_ESN)
  89. extralen += sizeof(struct esp_output_extra);
  90. iv = esp_tmp_iv(aead, tmp, extralen);
  91. req = esp_tmp_req(aead, iv);
  92. /* Unref skb_frag_pages in the src scatterlist if necessary.
  93. * Skip the first sg which comes from skb->data.
  94. */
  95. if (req->src != req->dst)
  96. for (sg = sg_next(req->src); sg; sg = sg_next(sg))
  97. skb_page_unref(page_to_netmem(sg_page(sg)),
  98. skb->pp_recycle);
  99. }
  100. #ifdef CONFIG_INET_ESPINTCP
  101. static struct sock *esp_find_tcp_sk(struct xfrm_state *x)
  102. {
  103. struct xfrm_encap_tmpl *encap = x->encap;
  104. struct net *net = xs_net(x);
  105. __be16 sport, dport;
  106. struct sock *sk;
  107. spin_lock_bh(&x->lock);
  108. sport = encap->encap_sport;
  109. dport = encap->encap_dport;
  110. spin_unlock_bh(&x->lock);
  111. sk = inet_lookup_established(net, x->id.daddr.a4, dport,
  112. x->props.saddr.a4, sport, 0);
  113. if (!sk)
  114. return ERR_PTR(-ENOENT);
  115. if (!tcp_is_ulp_esp(sk)) {
  116. sock_put(sk);
  117. return ERR_PTR(-EINVAL);
  118. }
  119. return sk;
  120. }
  121. static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
  122. {
  123. struct sock *sk;
  124. int err;
  125. rcu_read_lock();
  126. sk = esp_find_tcp_sk(x);
  127. err = PTR_ERR_OR_ZERO(sk);
  128. if (err) {
  129. kfree_skb(skb);
  130. goto out;
  131. }
  132. bh_lock_sock(sk);
  133. if (sock_owned_by_user(sk))
  134. err = espintcp_queue_out(sk, skb);
  135. else
  136. err = espintcp_push_skb(sk, skb);
  137. bh_unlock_sock(sk);
  138. sock_put(sk);
  139. out:
  140. rcu_read_unlock();
  141. return err;
  142. }
  143. static int esp_output_tcp_encap_cb(struct net *net, struct sock *sk,
  144. struct sk_buff *skb)
  145. {
  146. struct dst_entry *dst = skb_dst(skb);
  147. struct xfrm_state *x = dst->xfrm;
  148. return esp_output_tcp_finish(x, skb);
  149. }
  150. static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
  151. {
  152. int err;
  153. local_bh_disable();
  154. err = xfrm_trans_queue_net(xs_net(x), skb, esp_output_tcp_encap_cb);
  155. local_bh_enable();
  156. /* EINPROGRESS just happens to do the right thing. It
  157. * actually means that the skb has been consumed and
  158. * isn't coming back.
  159. */
  160. return err ?: -EINPROGRESS;
  161. }
  162. #else
  163. static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
  164. {
  165. WARN_ON(1);
  166. return -EOPNOTSUPP;
  167. }
  168. #endif
  169. static void esp_output_done(void *data, int err)
  170. {
  171. struct sk_buff *skb = data;
  172. struct xfrm_offload *xo = xfrm_offload(skb);
  173. void *tmp;
  174. struct xfrm_state *x;
  175. if (xo && (xo->flags & XFRM_DEV_RESUME)) {
  176. struct sec_path *sp = skb_sec_path(skb);
  177. x = sp->xvec[sp->len - 1];
  178. } else {
  179. x = skb_dst(skb)->xfrm;
  180. }
  181. tmp = ESP_SKB_CB(skb)->tmp;
  182. esp_ssg_unref(x, tmp, skb);
  183. kfree(tmp);
  184. if (xo && (xo->flags & XFRM_DEV_RESUME)) {
  185. if (err) {
  186. XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  187. kfree_skb(skb);
  188. return;
  189. }
  190. skb_push(skb, skb->data - skb_mac_header(skb));
  191. secpath_reset(skb);
  192. xfrm_dev_resume(skb);
  193. } else {
  194. if (!err &&
  195. x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP) {
  196. err = esp_output_tail_tcp(x, skb);
  197. if (err != -EINPROGRESS)
  198. kfree_skb(skb);
  199. } else {
  200. xfrm_output_resume(skb_to_full_sk(skb), skb, err);
  201. }
  202. }
  203. }
  204. /* Move ESP header back into place. */
  205. static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
  206. {
  207. struct ip_esp_hdr *esph = (void *)(skb->data + offset);
  208. void *tmp = ESP_SKB_CB(skb)->tmp;
  209. __be32 *seqhi = esp_tmp_extra(tmp);
  210. esph->seq_no = esph->spi;
  211. esph->spi = *seqhi;
  212. }
  213. static void esp_output_restore_header(struct sk_buff *skb)
  214. {
  215. void *tmp = ESP_SKB_CB(skb)->tmp;
  216. struct esp_output_extra *extra = esp_tmp_extra(tmp);
  217. esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
  218. sizeof(__be32));
  219. }
  220. static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
  221. struct xfrm_state *x,
  222. struct ip_esp_hdr *esph,
  223. struct esp_output_extra *extra)
  224. {
  225. /* For ESN we move the header forward by 4 bytes to
  226. * accommodate the high bits. We will move it back after
  227. * encryption.
  228. */
  229. if ((x->props.flags & XFRM_STATE_ESN)) {
  230. __u32 seqhi;
  231. struct xfrm_offload *xo = xfrm_offload(skb);
  232. if (xo)
  233. seqhi = xo->seq.hi;
  234. else
  235. seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
  236. extra->esphoff = (unsigned char *)esph -
  237. skb_transport_header(skb);
  238. esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
  239. extra->seqhi = esph->spi;
  240. esph->seq_no = htonl(seqhi);
  241. }
  242. esph->spi = x->id.spi;
  243. return esph;
  244. }
  245. static void esp_output_done_esn(void *data, int err)
  246. {
  247. struct sk_buff *skb = data;
  248. esp_output_restore_header(skb);
  249. esp_output_done(data, err);
  250. }
  251. static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb,
  252. int encap_type,
  253. struct esp_info *esp,
  254. __be16 sport,
  255. __be16 dport)
  256. {
  257. struct udphdr *uh;
  258. unsigned int len;
  259. struct xfrm_offload *xo = xfrm_offload(skb);
  260. len = skb->len + esp->tailen - skb_transport_offset(skb);
  261. if (len + sizeof(struct iphdr) > IP_MAX_MTU)
  262. return ERR_PTR(-EMSGSIZE);
  263. uh = (struct udphdr *)esp->esph;
  264. uh->source = sport;
  265. uh->dest = dport;
  266. uh->len = htons(len);
  267. uh->check = 0;
  268. /* For IPv4 ESP with UDP encapsulation, if xo is not null, the skb is in the crypto offload
  269. * data path, which means that esp_output_udp_encap is called outside of the XFRM stack.
  270. * In this case, the mac header doesn't point to the IPv4 protocol field, so don't set it.
  271. */
  272. if (!xo || encap_type != UDP_ENCAP_ESPINUDP)
  273. *skb_mac_header(skb) = IPPROTO_UDP;
  274. return (struct ip_esp_hdr *)(uh + 1);
  275. }
  276. #ifdef CONFIG_INET_ESPINTCP
  277. static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x,
  278. struct sk_buff *skb,
  279. struct esp_info *esp)
  280. {
  281. __be16 *lenp = (void *)esp->esph;
  282. struct ip_esp_hdr *esph;
  283. unsigned int len;
  284. struct sock *sk;
  285. len = skb->len + esp->tailen - skb_transport_offset(skb);
  286. if (len > IP_MAX_MTU)
  287. return ERR_PTR(-EMSGSIZE);
  288. rcu_read_lock();
  289. sk = esp_find_tcp_sk(x);
  290. rcu_read_unlock();
  291. if (IS_ERR(sk))
  292. return ERR_CAST(sk);
  293. sock_put(sk);
  294. *lenp = htons(len);
  295. esph = (struct ip_esp_hdr *)(lenp + 1);
  296. return esph;
  297. }
  298. #else
  299. static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x,
  300. struct sk_buff *skb,
  301. struct esp_info *esp)
  302. {
  303. return ERR_PTR(-EOPNOTSUPP);
  304. }
  305. #endif
  306. static int esp_output_encap(struct xfrm_state *x, struct sk_buff *skb,
  307. struct esp_info *esp)
  308. {
  309. struct xfrm_encap_tmpl *encap = x->encap;
  310. struct ip_esp_hdr *esph;
  311. __be16 sport, dport;
  312. int encap_type;
  313. spin_lock_bh(&x->lock);
  314. sport = encap->encap_sport;
  315. dport = encap->encap_dport;
  316. encap_type = encap->encap_type;
  317. spin_unlock_bh(&x->lock);
  318. switch (encap_type) {
  319. default:
  320. case UDP_ENCAP_ESPINUDP:
  321. esph = esp_output_udp_encap(skb, encap_type, esp, sport, dport);
  322. break;
  323. case TCP_ENCAP_ESPINTCP:
  324. esph = esp_output_tcp_encap(x, skb, esp);
  325. break;
  326. }
  327. if (IS_ERR(esph))
  328. return PTR_ERR(esph);
  329. esp->esph = esph;
  330. return 0;
  331. }
  332. int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  333. {
  334. u8 *tail;
  335. int nfrags;
  336. int esph_offset;
  337. struct page *page;
  338. struct sk_buff *trailer;
  339. int tailen = esp->tailen;
  340. /* this is non-NULL only with TCP/UDP Encapsulation */
  341. if (x->encap) {
  342. int err = esp_output_encap(x, skb, esp);
  343. if (err < 0)
  344. return err;
  345. }
  346. if (ALIGN(tailen, L1_CACHE_BYTES) > PAGE_SIZE ||
  347. ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE)
  348. goto cow;
  349. if (!skb_cloned(skb)) {
  350. if (tailen <= skb_tailroom(skb)) {
  351. nfrags = 1;
  352. trailer = skb;
  353. tail = skb_tail_pointer(trailer);
  354. goto skip_cow;
  355. } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
  356. && !skb_has_frag_list(skb)) {
  357. int allocsize;
  358. struct sock *sk = skb->sk;
  359. struct page_frag *pfrag = &x->xfrag;
  360. esp->inplace = false;
  361. allocsize = ALIGN(tailen, L1_CACHE_BYTES);
  362. spin_lock_bh(&x->lock);
  363. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  364. spin_unlock_bh(&x->lock);
  365. goto cow;
  366. }
  367. page = pfrag->page;
  368. get_page(page);
  369. tail = page_address(page) + pfrag->offset;
  370. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  371. nfrags = skb_shinfo(skb)->nr_frags;
  372. __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
  373. tailen);
  374. skb_shinfo(skb)->nr_frags = ++nfrags;
  375. pfrag->offset = pfrag->offset + allocsize;
  376. spin_unlock_bh(&x->lock);
  377. nfrags++;
  378. skb_len_add(skb, tailen);
  379. if (sk && sk_fullsock(sk))
  380. refcount_add(tailen, &sk->sk_wmem_alloc);
  381. goto out;
  382. }
  383. }
  384. cow:
  385. esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
  386. nfrags = skb_cow_data(skb, tailen, &trailer);
  387. if (nfrags < 0)
  388. goto out;
  389. tail = skb_tail_pointer(trailer);
  390. esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
  391. skip_cow:
  392. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  393. pskb_put(skb, trailer, tailen);
  394. out:
  395. return nfrags;
  396. }
  397. EXPORT_SYMBOL_GPL(esp_output_head);
  398. int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  399. {
  400. u8 *iv;
  401. int alen;
  402. void *tmp;
  403. int ivlen;
  404. int assoclen;
  405. int extralen;
  406. struct page *page;
  407. struct ip_esp_hdr *esph;
  408. struct crypto_aead *aead;
  409. struct aead_request *req;
  410. struct scatterlist *sg, *dsg;
  411. struct esp_output_extra *extra;
  412. int err = -ENOMEM;
  413. assoclen = sizeof(struct ip_esp_hdr);
  414. extralen = 0;
  415. if (x->props.flags & XFRM_STATE_ESN) {
  416. extralen += sizeof(*extra);
  417. assoclen += sizeof(__be32);
  418. }
  419. aead = x->data;
  420. alen = crypto_aead_authsize(aead);
  421. ivlen = crypto_aead_ivsize(aead);
  422. tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
  423. if (!tmp)
  424. goto error;
  425. extra = esp_tmp_extra(tmp);
  426. iv = esp_tmp_iv(aead, tmp, extralen);
  427. req = esp_tmp_req(aead, iv);
  428. sg = esp_req_sg(aead, req);
  429. if (esp->inplace)
  430. dsg = sg;
  431. else
  432. dsg = &sg[esp->nfrags];
  433. esph = esp_output_set_extra(skb, x, esp->esph, extra);
  434. esp->esph = esph;
  435. sg_init_table(sg, esp->nfrags);
  436. err = skb_to_sgvec(skb, sg,
  437. (unsigned char *)esph - skb->data,
  438. assoclen + ivlen + esp->clen + alen);
  439. if (unlikely(err < 0))
  440. goto error_free;
  441. if (!esp->inplace) {
  442. int allocsize;
  443. struct page_frag *pfrag = &x->xfrag;
  444. allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
  445. spin_lock_bh(&x->lock);
  446. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  447. spin_unlock_bh(&x->lock);
  448. goto error_free;
  449. }
  450. skb_shinfo(skb)->nr_frags = 1;
  451. page = pfrag->page;
  452. get_page(page);
  453. /* replace page frags in skb with new page */
  454. __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
  455. pfrag->offset = pfrag->offset + allocsize;
  456. spin_unlock_bh(&x->lock);
  457. sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
  458. err = skb_to_sgvec(skb, dsg,
  459. (unsigned char *)esph - skb->data,
  460. assoclen + ivlen + esp->clen + alen);
  461. if (unlikely(err < 0))
  462. goto error_free;
  463. }
  464. if ((x->props.flags & XFRM_STATE_ESN))
  465. aead_request_set_callback(req, 0, esp_output_done_esn, skb);
  466. else
  467. aead_request_set_callback(req, 0, esp_output_done, skb);
  468. aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
  469. aead_request_set_ad(req, assoclen);
  470. memset(iv, 0, ivlen);
  471. memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
  472. min(ivlen, 8));
  473. ESP_SKB_CB(skb)->tmp = tmp;
  474. err = crypto_aead_encrypt(req);
  475. switch (err) {
  476. case -EINPROGRESS:
  477. goto error;
  478. case -ENOSPC:
  479. err = NET_XMIT_DROP;
  480. break;
  481. case 0:
  482. if ((x->props.flags & XFRM_STATE_ESN))
  483. esp_output_restore_header(skb);
  484. }
  485. if (sg != dsg)
  486. esp_ssg_unref(x, tmp, skb);
  487. if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
  488. err = esp_output_tail_tcp(x, skb);
  489. error_free:
  490. kfree(tmp);
  491. error:
  492. return err;
  493. }
  494. EXPORT_SYMBOL_GPL(esp_output_tail);
  495. static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
  496. {
  497. int alen;
  498. int blksize;
  499. struct ip_esp_hdr *esph;
  500. struct crypto_aead *aead;
  501. struct esp_info esp;
  502. esp.inplace = true;
  503. esp.proto = *skb_mac_header(skb);
  504. *skb_mac_header(skb) = IPPROTO_ESP;
  505. /* skb is pure payload to encrypt */
  506. aead = x->data;
  507. alen = crypto_aead_authsize(aead);
  508. esp.tfclen = 0;
  509. if (x->tfcpad) {
  510. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  511. u32 padto;
  512. padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached));
  513. if (skb->len < padto)
  514. esp.tfclen = padto - skb->len;
  515. }
  516. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  517. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  518. esp.plen = esp.clen - skb->len - esp.tfclen;
  519. esp.tailen = esp.tfclen + esp.plen + alen;
  520. esp.esph = ip_esp_hdr(skb);
  521. esp.nfrags = esp_output_head(x, skb, &esp);
  522. if (esp.nfrags < 0)
  523. return esp.nfrags;
  524. esph = esp.esph;
  525. esph->spi = x->id.spi;
  526. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  527. esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  528. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  529. skb_push(skb, -skb_network_offset(skb));
  530. return esp_output_tail(x, skb, &esp);
  531. }
  532. static inline int esp_remove_trailer(struct sk_buff *skb)
  533. {
  534. struct xfrm_state *x = xfrm_input_state(skb);
  535. struct crypto_aead *aead = x->data;
  536. int alen, hlen, elen;
  537. int padlen, trimlen;
  538. __wsum csumdiff;
  539. u8 nexthdr[2];
  540. int ret;
  541. alen = crypto_aead_authsize(aead);
  542. hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  543. elen = skb->len - hlen;
  544. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  545. BUG();
  546. ret = -EINVAL;
  547. padlen = nexthdr[0];
  548. if (padlen + 2 + alen >= elen) {
  549. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  550. padlen + 2, elen - alen);
  551. goto out;
  552. }
  553. trimlen = alen + padlen + 2;
  554. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  555. csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
  556. skb->csum = csum_block_sub(skb->csum, csumdiff,
  557. skb->len - trimlen);
  558. }
  559. ret = pskb_trim(skb, skb->len - trimlen);
  560. if (unlikely(ret))
  561. return ret;
  562. ret = nexthdr[1];
  563. out:
  564. return ret;
  565. }
  566. int esp_input_done2(struct sk_buff *skb, int err)
  567. {
  568. const struct iphdr *iph;
  569. struct xfrm_state *x = xfrm_input_state(skb);
  570. struct xfrm_offload *xo = xfrm_offload(skb);
  571. struct crypto_aead *aead = x->data;
  572. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  573. int ihl;
  574. if (!xo || !(xo->flags & CRYPTO_DONE))
  575. kfree(ESP_SKB_CB(skb)->tmp);
  576. if (unlikely(err))
  577. goto out;
  578. err = esp_remove_trailer(skb);
  579. if (unlikely(err < 0))
  580. goto out;
  581. iph = ip_hdr(skb);
  582. ihl = iph->ihl * 4;
  583. if (x->encap) {
  584. struct xfrm_encap_tmpl *encap = x->encap;
  585. struct tcphdr *th = (void *)(skb_network_header(skb) + ihl);
  586. struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
  587. __be16 source;
  588. switch (x->encap->encap_type) {
  589. case TCP_ENCAP_ESPINTCP:
  590. source = th->source;
  591. break;
  592. case UDP_ENCAP_ESPINUDP:
  593. source = uh->source;
  594. break;
  595. default:
  596. WARN_ON_ONCE(1);
  597. err = -EINVAL;
  598. goto out;
  599. }
  600. /*
  601. * 1) if the NAT-T peer's IP or port changed then
  602. * advertise the change to the keying daemon.
  603. * This is an inbound SA, so just compare
  604. * SRC ports.
  605. */
  606. if (iph->saddr != x->props.saddr.a4 ||
  607. source != encap->encap_sport) {
  608. xfrm_address_t ipaddr;
  609. ipaddr.a4 = iph->saddr;
  610. km_new_mapping(x, &ipaddr, source);
  611. /* XXX: perhaps add an extra
  612. * policy check here, to see
  613. * if we should allow or
  614. * reject a packet from a
  615. * different source
  616. * address/port.
  617. */
  618. }
  619. /*
  620. * 2) ignore UDP/TCP checksums in case
  621. * of NAT-T in Transport Mode, or
  622. * perform other post-processing fixes
  623. * as per draft-ietf-ipsec-udp-encaps-06,
  624. * section 3.1.2
  625. */
  626. if (x->props.mode == XFRM_MODE_TRANSPORT)
  627. skb->ip_summed = CHECKSUM_UNNECESSARY;
  628. }
  629. skb_pull_rcsum(skb, hlen);
  630. if (x->props.mode == XFRM_MODE_TUNNEL ||
  631. x->props.mode == XFRM_MODE_IPTFS)
  632. skb_reset_transport_header(skb);
  633. else
  634. skb_set_transport_header(skb, -ihl);
  635. /* RFC4303: Drop dummy packets without any error */
  636. if (err == IPPROTO_NONE)
  637. err = -EINVAL;
  638. out:
  639. return err;
  640. }
  641. EXPORT_SYMBOL_GPL(esp_input_done2);
  642. static void esp_input_done(void *data, int err)
  643. {
  644. struct sk_buff *skb = data;
  645. xfrm_input_resume(skb, esp_input_done2(skb, err));
  646. }
  647. static void esp_input_restore_header(struct sk_buff *skb)
  648. {
  649. esp_restore_header(skb, 0);
  650. __skb_pull(skb, 4);
  651. }
  652. static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
  653. {
  654. struct xfrm_state *x = xfrm_input_state(skb);
  655. struct ip_esp_hdr *esph;
  656. /* For ESN we move the header forward by 4 bytes to
  657. * accommodate the high bits. We will move it back after
  658. * decryption.
  659. */
  660. if ((x->props.flags & XFRM_STATE_ESN)) {
  661. esph = skb_push(skb, 4);
  662. *seqhi = esph->spi;
  663. esph->spi = esph->seq_no;
  664. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  665. }
  666. }
  667. static void esp_input_done_esn(void *data, int err)
  668. {
  669. struct sk_buff *skb = data;
  670. esp_input_restore_header(skb);
  671. esp_input_done(data, err);
  672. }
  673. /*
  674. * Note: detecting truncated vs. non-truncated authentication data is very
  675. * expensive, so we only support truncated data, which is the recommended
  676. * and common case.
  677. */
  678. static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
  679. {
  680. struct crypto_aead *aead = x->data;
  681. struct aead_request *req;
  682. struct sk_buff *trailer;
  683. int ivlen = crypto_aead_ivsize(aead);
  684. int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
  685. int nfrags;
  686. int assoclen;
  687. int seqhilen;
  688. __be32 *seqhi;
  689. void *tmp;
  690. u8 *iv;
  691. struct scatterlist *sg;
  692. int err = -EINVAL;
  693. if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
  694. goto out;
  695. if (elen <= 0)
  696. goto out;
  697. assoclen = sizeof(struct ip_esp_hdr);
  698. seqhilen = 0;
  699. if (x->props.flags & XFRM_STATE_ESN) {
  700. seqhilen += sizeof(__be32);
  701. assoclen += seqhilen;
  702. }
  703. if (!skb_cloned(skb)) {
  704. if (!skb_is_nonlinear(skb)) {
  705. nfrags = 1;
  706. goto skip_cow;
  707. } else if (!skb_has_frag_list(skb)) {
  708. nfrags = skb_shinfo(skb)->nr_frags;
  709. nfrags++;
  710. goto skip_cow;
  711. }
  712. }
  713. err = skb_cow_data(skb, 0, &trailer);
  714. if (err < 0)
  715. goto out;
  716. nfrags = err;
  717. skip_cow:
  718. err = -ENOMEM;
  719. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  720. if (!tmp)
  721. goto out;
  722. ESP_SKB_CB(skb)->tmp = tmp;
  723. seqhi = esp_tmp_extra(tmp);
  724. iv = esp_tmp_iv(aead, tmp, seqhilen);
  725. req = esp_tmp_req(aead, iv);
  726. sg = esp_req_sg(aead, req);
  727. esp_input_set_header(skb, seqhi);
  728. sg_init_table(sg, nfrags);
  729. err = skb_to_sgvec(skb, sg, 0, skb->len);
  730. if (unlikely(err < 0)) {
  731. kfree(tmp);
  732. goto out;
  733. }
  734. skb->ip_summed = CHECKSUM_NONE;
  735. if ((x->props.flags & XFRM_STATE_ESN))
  736. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  737. else
  738. aead_request_set_callback(req, 0, esp_input_done, skb);
  739. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  740. aead_request_set_ad(req, assoclen);
  741. err = crypto_aead_decrypt(req);
  742. if (err == -EINPROGRESS)
  743. goto out;
  744. if ((x->props.flags & XFRM_STATE_ESN))
  745. esp_input_restore_header(skb);
  746. err = esp_input_done2(skb, err);
  747. out:
  748. return err;
  749. }
  750. static int esp4_err(struct sk_buff *skb, u32 info)
  751. {
  752. struct net *net = dev_net(skb->dev);
  753. const struct iphdr *iph = (const struct iphdr *)skb->data;
  754. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  755. struct xfrm_state *x;
  756. switch (icmp_hdr(skb)->type) {
  757. case ICMP_DEST_UNREACH:
  758. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  759. return 0;
  760. break;
  761. case ICMP_REDIRECT:
  762. break;
  763. default:
  764. return 0;
  765. }
  766. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  767. esph->spi, IPPROTO_ESP, AF_INET);
  768. if (!x)
  769. return 0;
  770. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  771. ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
  772. else
  773. ipv4_redirect(skb, net, 0, IPPROTO_ESP);
  774. xfrm_state_put(x);
  775. return 0;
  776. }
  777. static void esp_destroy(struct xfrm_state *x)
  778. {
  779. struct crypto_aead *aead = x->data;
  780. if (!aead)
  781. return;
  782. crypto_free_aead(aead);
  783. }
  784. static int esp_init_aead(struct xfrm_state *x, struct netlink_ext_ack *extack)
  785. {
  786. char aead_name[CRYPTO_MAX_ALG_NAME];
  787. struct crypto_aead *aead;
  788. int err;
  789. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  790. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) {
  791. NL_SET_ERR_MSG(extack, "Algorithm name is too long");
  792. return -ENAMETOOLONG;
  793. }
  794. aead = crypto_alloc_aead(aead_name, 0, 0);
  795. err = PTR_ERR(aead);
  796. if (IS_ERR(aead))
  797. goto error;
  798. x->data = aead;
  799. err = crypto_aead_setkey(aead, x->aead->alg_key,
  800. (x->aead->alg_key_len + 7) / 8);
  801. if (err)
  802. goto error;
  803. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  804. if (err)
  805. goto error;
  806. return 0;
  807. error:
  808. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  809. return err;
  810. }
  811. static int esp_init_authenc(struct xfrm_state *x,
  812. struct netlink_ext_ack *extack)
  813. {
  814. struct crypto_aead *aead;
  815. struct crypto_authenc_key_param *param;
  816. struct rtattr *rta;
  817. char *key;
  818. char *p;
  819. char authenc_name[CRYPTO_MAX_ALG_NAME];
  820. unsigned int keylen;
  821. int err;
  822. err = -ENAMETOOLONG;
  823. if ((x->props.flags & XFRM_STATE_ESN)) {
  824. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  825. "%s%sauthencesn(%s,%s)%s",
  826. x->geniv ?: "", x->geniv ? "(" : "",
  827. x->aalg ? x->aalg->alg_name : "digest_null",
  828. x->ealg->alg_name,
  829. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
  830. NL_SET_ERR_MSG(extack, "Algorithm name is too long");
  831. goto error;
  832. }
  833. } else {
  834. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  835. "%s%sauthenc(%s,%s)%s",
  836. x->geniv ?: "", x->geniv ? "(" : "",
  837. x->aalg ? x->aalg->alg_name : "digest_null",
  838. x->ealg->alg_name,
  839. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
  840. NL_SET_ERR_MSG(extack, "Algorithm name is too long");
  841. goto error;
  842. }
  843. }
  844. aead = crypto_alloc_aead(authenc_name, 0, 0);
  845. err = PTR_ERR(aead);
  846. if (IS_ERR(aead)) {
  847. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  848. goto error;
  849. }
  850. x->data = aead;
  851. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  852. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  853. err = -ENOMEM;
  854. key = kmalloc(keylen, GFP_KERNEL);
  855. if (!key)
  856. goto error;
  857. p = key;
  858. rta = (void *)p;
  859. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  860. rta->rta_len = RTA_LENGTH(sizeof(*param));
  861. param = RTA_DATA(rta);
  862. p += RTA_SPACE(sizeof(*param));
  863. if (x->aalg) {
  864. struct xfrm_algo_desc *aalg_desc;
  865. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  866. p += (x->aalg->alg_key_len + 7) / 8;
  867. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  868. BUG_ON(!aalg_desc);
  869. err = -EINVAL;
  870. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  871. crypto_aead_authsize(aead)) {
  872. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  873. goto free_key;
  874. }
  875. err = crypto_aead_setauthsize(
  876. aead, x->aalg->alg_trunc_len / 8);
  877. if (err) {
  878. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  879. goto free_key;
  880. }
  881. }
  882. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  883. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  884. err = crypto_aead_setkey(aead, key, keylen);
  885. free_key:
  886. kfree_sensitive(key);
  887. error:
  888. return err;
  889. }
  890. static int esp_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
  891. {
  892. struct crypto_aead *aead;
  893. u32 align;
  894. int err;
  895. x->data = NULL;
  896. if (x->aead) {
  897. err = esp_init_aead(x, extack);
  898. } else if (x->ealg) {
  899. err = esp_init_authenc(x, extack);
  900. } else {
  901. NL_SET_ERR_MSG(extack, "ESP: AEAD or CRYPT must be provided");
  902. err = -EINVAL;
  903. }
  904. if (err)
  905. goto error;
  906. aead = x->data;
  907. x->props.header_len = sizeof(struct ip_esp_hdr) +
  908. crypto_aead_ivsize(aead);
  909. if (x->props.mode == XFRM_MODE_TUNNEL)
  910. x->props.header_len += sizeof(struct iphdr);
  911. else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
  912. x->props.header_len += IPV4_BEET_PHMAXLEN;
  913. if (x->encap) {
  914. struct xfrm_encap_tmpl *encap = x->encap;
  915. switch (encap->encap_type) {
  916. default:
  917. NL_SET_ERR_MSG(extack, "Unsupported encapsulation type for ESP");
  918. err = -EINVAL;
  919. goto error;
  920. case UDP_ENCAP_ESPINUDP:
  921. x->props.header_len += sizeof(struct udphdr);
  922. break;
  923. #ifdef CONFIG_INET_ESPINTCP
  924. case TCP_ENCAP_ESPINTCP:
  925. /* only the length field, TCP encap is done by
  926. * the socket
  927. */
  928. x->props.header_len += 2;
  929. break;
  930. #endif
  931. }
  932. }
  933. align = ALIGN(crypto_aead_blocksize(aead), 4);
  934. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  935. error:
  936. return err;
  937. }
  938. static int esp4_rcv_cb(struct sk_buff *skb, int err)
  939. {
  940. return 0;
  941. }
  942. static const struct xfrm_type esp_type =
  943. {
  944. .owner = THIS_MODULE,
  945. .proto = IPPROTO_ESP,
  946. .flags = XFRM_TYPE_REPLAY_PROT,
  947. .init_state = esp_init_state,
  948. .destructor = esp_destroy,
  949. .input = esp_input,
  950. .output = esp_output,
  951. };
  952. static struct xfrm4_protocol esp4_protocol = {
  953. .handler = xfrm4_rcv,
  954. .input_handler = xfrm_input,
  955. .cb_handler = esp4_rcv_cb,
  956. .err_handler = esp4_err,
  957. .priority = 0,
  958. };
  959. static int __init esp4_init(void)
  960. {
  961. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  962. pr_info("%s: can't add xfrm type\n", __func__);
  963. return -EAGAIN;
  964. }
  965. if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
  966. pr_info("%s: can't add protocol\n", __func__);
  967. xfrm_unregister_type(&esp_type, AF_INET);
  968. return -EAGAIN;
  969. }
  970. return 0;
  971. }
  972. static void __exit esp4_fini(void)
  973. {
  974. if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
  975. pr_info("%s: can't remove protocol\n", __func__);
  976. xfrm_unregister_type(&esp_type, AF_INET);
  977. }
  978. module_init(esp4_init);
  979. module_exit(esp4_fini);
  980. MODULE_DESCRIPTION("IPv4 ESP transformation library");
  981. MODULE_LICENSE("GPL");
  982. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);