esp6.c 28 KB

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