esp6.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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, net->ipv4.tcp_death_row.hashinfo, &x->id.daddr.in6,
  128. dport, &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. esp_output_tail_tcp(x, skb);
  229. else
  230. xfrm_output_resume(skb->sk, skb, err);
  231. }
  232. }
  233. /* Move ESP header back into place. */
  234. static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
  235. {
  236. struct ip_esp_hdr *esph = (void *)(skb->data + offset);
  237. void *tmp = ESP_SKB_CB(skb)->tmp;
  238. __be32 *seqhi = esp_tmp_extra(tmp);
  239. esph->seq_no = esph->spi;
  240. esph->spi = *seqhi;
  241. }
  242. static void esp_output_restore_header(struct sk_buff *skb)
  243. {
  244. void *tmp = ESP_SKB_CB(skb)->tmp;
  245. struct esp_output_extra *extra = esp_tmp_extra(tmp);
  246. esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
  247. sizeof(__be32));
  248. }
  249. static struct ip_esp_hdr *esp_output_set_esn(struct sk_buff *skb,
  250. struct xfrm_state *x,
  251. struct ip_esp_hdr *esph,
  252. struct esp_output_extra *extra)
  253. {
  254. /* For ESN we move the header forward by 4 bytes to
  255. * accommodate the high bits. We will move it back after
  256. * encryption.
  257. */
  258. if ((x->props.flags & XFRM_STATE_ESN)) {
  259. __u32 seqhi;
  260. struct xfrm_offload *xo = xfrm_offload(skb);
  261. if (xo)
  262. seqhi = xo->seq.hi;
  263. else
  264. seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
  265. extra->esphoff = (unsigned char *)esph -
  266. skb_transport_header(skb);
  267. esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
  268. extra->seqhi = esph->spi;
  269. esph->seq_no = htonl(seqhi);
  270. }
  271. esph->spi = x->id.spi;
  272. return esph;
  273. }
  274. static void esp_output_done_esn(void *data, int err)
  275. {
  276. struct sk_buff *skb = data;
  277. esp_output_restore_header(skb);
  278. esp_output_done(data, err);
  279. }
  280. static struct ip_esp_hdr *esp6_output_udp_encap(struct sk_buff *skb,
  281. int encap_type,
  282. struct esp_info *esp,
  283. __be16 sport,
  284. __be16 dport)
  285. {
  286. struct udphdr *uh;
  287. unsigned int len;
  288. len = skb->len + esp->tailen - skb_transport_offset(skb);
  289. if (len > U16_MAX)
  290. return ERR_PTR(-EMSGSIZE);
  291. uh = (struct udphdr *)esp->esph;
  292. uh->source = sport;
  293. uh->dest = dport;
  294. uh->len = htons(len);
  295. uh->check = 0;
  296. *skb_mac_header(skb) = IPPROTO_UDP;
  297. return (struct ip_esp_hdr *)(uh + 1);
  298. }
  299. #ifdef CONFIG_INET6_ESPINTCP
  300. static struct ip_esp_hdr *esp6_output_tcp_encap(struct xfrm_state *x,
  301. struct sk_buff *skb,
  302. struct esp_info *esp)
  303. {
  304. __be16 *lenp = (void *)esp->esph;
  305. struct ip_esp_hdr *esph;
  306. unsigned int len;
  307. struct sock *sk;
  308. len = skb->len + esp->tailen - skb_transport_offset(skb);
  309. if (len > IP_MAX_MTU)
  310. return ERR_PTR(-EMSGSIZE);
  311. rcu_read_lock();
  312. sk = esp6_find_tcp_sk(x);
  313. rcu_read_unlock();
  314. if (IS_ERR(sk))
  315. return ERR_CAST(sk);
  316. sock_put(sk);
  317. *lenp = htons(len);
  318. esph = (struct ip_esp_hdr *)(lenp + 1);
  319. return esph;
  320. }
  321. #else
  322. static struct ip_esp_hdr *esp6_output_tcp_encap(struct xfrm_state *x,
  323. struct sk_buff *skb,
  324. struct esp_info *esp)
  325. {
  326. return ERR_PTR(-EOPNOTSUPP);
  327. }
  328. #endif
  329. static int esp6_output_encap(struct xfrm_state *x, struct sk_buff *skb,
  330. struct esp_info *esp)
  331. {
  332. struct xfrm_encap_tmpl *encap = x->encap;
  333. struct ip_esp_hdr *esph;
  334. __be16 sport, dport;
  335. int encap_type;
  336. spin_lock_bh(&x->lock);
  337. sport = encap->encap_sport;
  338. dport = encap->encap_dport;
  339. encap_type = encap->encap_type;
  340. spin_unlock_bh(&x->lock);
  341. switch (encap_type) {
  342. default:
  343. case UDP_ENCAP_ESPINUDP:
  344. esph = esp6_output_udp_encap(skb, encap_type, esp, sport, dport);
  345. break;
  346. case TCP_ENCAP_ESPINTCP:
  347. esph = esp6_output_tcp_encap(x, skb, esp);
  348. break;
  349. }
  350. if (IS_ERR(esph))
  351. return PTR_ERR(esph);
  352. esp->esph = esph;
  353. return 0;
  354. }
  355. int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  356. {
  357. u8 *tail;
  358. int nfrags;
  359. int esph_offset;
  360. struct page *page;
  361. struct sk_buff *trailer;
  362. int tailen = esp->tailen;
  363. if (x->encap) {
  364. int err = esp6_output_encap(x, skb, esp);
  365. if (err < 0)
  366. return err;
  367. }
  368. if (ALIGN(tailen, L1_CACHE_BYTES) > PAGE_SIZE ||
  369. ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE)
  370. goto cow;
  371. if (!skb_cloned(skb)) {
  372. if (tailen <= skb_tailroom(skb)) {
  373. nfrags = 1;
  374. trailer = skb;
  375. tail = skb_tail_pointer(trailer);
  376. goto skip_cow;
  377. } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
  378. && !skb_has_frag_list(skb)) {
  379. int allocsize;
  380. struct sock *sk = skb->sk;
  381. struct page_frag *pfrag = &x->xfrag;
  382. esp->inplace = false;
  383. allocsize = ALIGN(tailen, L1_CACHE_BYTES);
  384. spin_lock_bh(&x->lock);
  385. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  386. spin_unlock_bh(&x->lock);
  387. goto cow;
  388. }
  389. page = pfrag->page;
  390. get_page(page);
  391. tail = page_address(page) + pfrag->offset;
  392. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  393. nfrags = skb_shinfo(skb)->nr_frags;
  394. __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
  395. tailen);
  396. skb_shinfo(skb)->nr_frags = ++nfrags;
  397. pfrag->offset = pfrag->offset + allocsize;
  398. spin_unlock_bh(&x->lock);
  399. nfrags++;
  400. skb->len += tailen;
  401. skb->data_len += tailen;
  402. skb->truesize += tailen;
  403. if (sk && sk_fullsock(sk))
  404. refcount_add(tailen, &sk->sk_wmem_alloc);
  405. goto out;
  406. }
  407. }
  408. cow:
  409. esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
  410. nfrags = skb_cow_data(skb, tailen, &trailer);
  411. if (nfrags < 0)
  412. goto out;
  413. tail = skb_tail_pointer(trailer);
  414. esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
  415. skip_cow:
  416. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  417. pskb_put(skb, trailer, tailen);
  418. out:
  419. return nfrags;
  420. }
  421. EXPORT_SYMBOL_GPL(esp6_output_head);
  422. int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  423. {
  424. u8 *iv;
  425. int alen;
  426. void *tmp;
  427. int ivlen;
  428. int assoclen;
  429. int extralen;
  430. struct page *page;
  431. struct ip_esp_hdr *esph;
  432. struct aead_request *req;
  433. struct crypto_aead *aead;
  434. struct scatterlist *sg, *dsg;
  435. struct esp_output_extra *extra;
  436. int err = -ENOMEM;
  437. assoclen = sizeof(struct ip_esp_hdr);
  438. extralen = 0;
  439. if (x->props.flags & XFRM_STATE_ESN) {
  440. extralen += sizeof(*extra);
  441. assoclen += sizeof(__be32);
  442. }
  443. aead = x->data;
  444. alen = crypto_aead_authsize(aead);
  445. ivlen = crypto_aead_ivsize(aead);
  446. tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
  447. if (!tmp)
  448. goto error;
  449. extra = esp_tmp_extra(tmp);
  450. iv = esp_tmp_iv(aead, tmp, extralen);
  451. req = esp_tmp_req(aead, iv);
  452. sg = esp_req_sg(aead, req);
  453. if (esp->inplace)
  454. dsg = sg;
  455. else
  456. dsg = &sg[esp->nfrags];
  457. esph = esp_output_set_esn(skb, x, esp->esph, extra);
  458. esp->esph = esph;
  459. sg_init_table(sg, esp->nfrags);
  460. err = skb_to_sgvec(skb, sg,
  461. (unsigned char *)esph - skb->data,
  462. assoclen + ivlen + esp->clen + alen);
  463. if (unlikely(err < 0))
  464. goto error_free;
  465. if (!esp->inplace) {
  466. int allocsize;
  467. struct page_frag *pfrag = &x->xfrag;
  468. allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
  469. spin_lock_bh(&x->lock);
  470. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  471. spin_unlock_bh(&x->lock);
  472. goto error_free;
  473. }
  474. skb_shinfo(skb)->nr_frags = 1;
  475. page = pfrag->page;
  476. get_page(page);
  477. /* replace page frags in skb with new page */
  478. __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
  479. pfrag->offset = pfrag->offset + allocsize;
  480. spin_unlock_bh(&x->lock);
  481. sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
  482. err = skb_to_sgvec(skb, dsg,
  483. (unsigned char *)esph - skb->data,
  484. assoclen + ivlen + esp->clen + alen);
  485. if (unlikely(err < 0))
  486. goto error_free;
  487. }
  488. if ((x->props.flags & XFRM_STATE_ESN))
  489. aead_request_set_callback(req, 0, esp_output_done_esn, skb);
  490. else
  491. aead_request_set_callback(req, 0, esp_output_done, skb);
  492. aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
  493. aead_request_set_ad(req, assoclen);
  494. memset(iv, 0, ivlen);
  495. memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
  496. min(ivlen, 8));
  497. ESP_SKB_CB(skb)->tmp = tmp;
  498. err = crypto_aead_encrypt(req);
  499. switch (err) {
  500. case -EINPROGRESS:
  501. goto error;
  502. case -ENOSPC:
  503. err = NET_XMIT_DROP;
  504. break;
  505. case 0:
  506. if ((x->props.flags & XFRM_STATE_ESN))
  507. esp_output_restore_header(skb);
  508. esp_output_encap_csum(skb);
  509. }
  510. if (sg != dsg)
  511. esp_ssg_unref(x, tmp, skb);
  512. if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
  513. err = esp_output_tail_tcp(x, skb);
  514. error_free:
  515. kfree(tmp);
  516. error:
  517. return err;
  518. }
  519. EXPORT_SYMBOL_GPL(esp6_output_tail);
  520. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  521. {
  522. int alen;
  523. int blksize;
  524. struct ip_esp_hdr *esph;
  525. struct crypto_aead *aead;
  526. struct esp_info esp;
  527. esp.inplace = true;
  528. esp.proto = *skb_mac_header(skb);
  529. *skb_mac_header(skb) = IPPROTO_ESP;
  530. /* skb is pure payload to encrypt */
  531. aead = x->data;
  532. alen = crypto_aead_authsize(aead);
  533. esp.tfclen = 0;
  534. if (x->tfcpad) {
  535. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  536. u32 padto;
  537. padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached));
  538. if (skb->len < padto)
  539. esp.tfclen = padto - skb->len;
  540. }
  541. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  542. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  543. esp.plen = esp.clen - skb->len - esp.tfclen;
  544. esp.tailen = esp.tfclen + esp.plen + alen;
  545. esp.esph = ip_esp_hdr(skb);
  546. esp.nfrags = esp6_output_head(x, skb, &esp);
  547. if (esp.nfrags < 0)
  548. return esp.nfrags;
  549. esph = esp.esph;
  550. esph->spi = x->id.spi;
  551. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  552. esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  553. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  554. skb_push(skb, -skb_network_offset(skb));
  555. return esp6_output_tail(x, skb, &esp);
  556. }
  557. static inline int esp_remove_trailer(struct sk_buff *skb)
  558. {
  559. struct xfrm_state *x = xfrm_input_state(skb);
  560. struct crypto_aead *aead = x->data;
  561. int alen, hlen, elen;
  562. int padlen, trimlen;
  563. __wsum csumdiff;
  564. u8 nexthdr[2];
  565. int ret;
  566. alen = crypto_aead_authsize(aead);
  567. hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  568. elen = skb->len - hlen;
  569. ret = skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2);
  570. BUG_ON(ret);
  571. ret = -EINVAL;
  572. padlen = nexthdr[0];
  573. if (padlen + 2 + alen >= elen) {
  574. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  575. padlen + 2, elen - alen);
  576. goto out;
  577. }
  578. trimlen = alen + padlen + 2;
  579. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  580. csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
  581. skb->csum = csum_block_sub(skb->csum, csumdiff,
  582. skb->len - trimlen);
  583. }
  584. ret = pskb_trim(skb, skb->len - trimlen);
  585. if (unlikely(ret))
  586. return ret;
  587. ret = nexthdr[1];
  588. out:
  589. return ret;
  590. }
  591. int esp6_input_done2(struct sk_buff *skb, int err)
  592. {
  593. struct xfrm_state *x = xfrm_input_state(skb);
  594. struct xfrm_offload *xo = xfrm_offload(skb);
  595. struct crypto_aead *aead = x->data;
  596. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  597. int hdr_len = skb_network_header_len(skb);
  598. if (!xo || !(xo->flags & CRYPTO_DONE))
  599. kfree(ESP_SKB_CB(skb)->tmp);
  600. if (unlikely(err))
  601. goto out;
  602. err = esp_remove_trailer(skb);
  603. if (unlikely(err < 0))
  604. goto out;
  605. if (x->encap) {
  606. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  607. int offset = skb_network_offset(skb) + sizeof(*ip6h);
  608. struct xfrm_encap_tmpl *encap = x->encap;
  609. u8 nexthdr = ip6h->nexthdr;
  610. __be16 frag_off, source;
  611. struct udphdr *uh;
  612. struct tcphdr *th;
  613. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  614. if (offset == -1) {
  615. err = -EINVAL;
  616. goto out;
  617. }
  618. uh = (void *)(skb->data + offset);
  619. th = (void *)(skb->data + offset);
  620. hdr_len += offset;
  621. switch (x->encap->encap_type) {
  622. case TCP_ENCAP_ESPINTCP:
  623. source = th->source;
  624. break;
  625. case UDP_ENCAP_ESPINUDP:
  626. source = uh->source;
  627. break;
  628. default:
  629. WARN_ON_ONCE(1);
  630. err = -EINVAL;
  631. goto out;
  632. }
  633. /*
  634. * 1) if the NAT-T peer's IP or port changed then
  635. * advertise the change to the keying daemon.
  636. * This is an inbound SA, so just compare
  637. * SRC ports.
  638. */
  639. if (!ipv6_addr_equal(&ip6h->saddr, &x->props.saddr.in6) ||
  640. source != encap->encap_sport) {
  641. xfrm_address_t ipaddr;
  642. memcpy(&ipaddr.a6, &ip6h->saddr.s6_addr, sizeof(ipaddr.a6));
  643. km_new_mapping(x, &ipaddr, source);
  644. /* XXX: perhaps add an extra
  645. * policy check here, to see
  646. * if we should allow or
  647. * reject a packet from a
  648. * different source
  649. * address/port.
  650. */
  651. }
  652. /*
  653. * 2) ignore UDP/TCP checksums in case
  654. * of NAT-T in Transport Mode, or
  655. * perform other post-processing fixes
  656. * as per draft-ietf-ipsec-udp-encaps-06,
  657. * section 3.1.2
  658. */
  659. if (x->props.mode == XFRM_MODE_TRANSPORT)
  660. skb->ip_summed = CHECKSUM_UNNECESSARY;
  661. }
  662. skb_postpull_rcsum(skb, skb_network_header(skb),
  663. skb_network_header_len(skb));
  664. skb_pull_rcsum(skb, hlen);
  665. if (x->props.mode == XFRM_MODE_TUNNEL)
  666. skb_reset_transport_header(skb);
  667. else
  668. skb_set_transport_header(skb, -hdr_len);
  669. /* RFC4303: Drop dummy packets without any error */
  670. if (err == IPPROTO_NONE)
  671. err = -EINVAL;
  672. out:
  673. return err;
  674. }
  675. EXPORT_SYMBOL_GPL(esp6_input_done2);
  676. static void esp_input_done(void *data, int err)
  677. {
  678. struct sk_buff *skb = data;
  679. xfrm_input_resume(skb, esp6_input_done2(skb, err));
  680. }
  681. static void esp_input_restore_header(struct sk_buff *skb)
  682. {
  683. esp_restore_header(skb, 0);
  684. __skb_pull(skb, 4);
  685. }
  686. static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
  687. {
  688. struct xfrm_state *x = xfrm_input_state(skb);
  689. /* For ESN we move the header forward by 4 bytes to
  690. * accommodate the high bits. We will move it back after
  691. * decryption.
  692. */
  693. if ((x->props.flags & XFRM_STATE_ESN)) {
  694. struct ip_esp_hdr *esph = skb_push(skb, 4);
  695. *seqhi = esph->spi;
  696. esph->spi = esph->seq_no;
  697. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  698. }
  699. }
  700. static void esp_input_done_esn(void *data, int err)
  701. {
  702. struct sk_buff *skb = data;
  703. esp_input_restore_header(skb);
  704. esp_input_done(data, err);
  705. }
  706. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  707. {
  708. struct crypto_aead *aead = x->data;
  709. struct aead_request *req;
  710. struct sk_buff *trailer;
  711. int ivlen = crypto_aead_ivsize(aead);
  712. int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
  713. int nfrags;
  714. int assoclen;
  715. int seqhilen;
  716. int ret = 0;
  717. void *tmp;
  718. __be32 *seqhi;
  719. u8 *iv;
  720. struct scatterlist *sg;
  721. if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen)) {
  722. ret = -EINVAL;
  723. goto out;
  724. }
  725. if (elen <= 0) {
  726. ret = -EINVAL;
  727. goto out;
  728. }
  729. assoclen = sizeof(struct ip_esp_hdr);
  730. seqhilen = 0;
  731. if (x->props.flags & XFRM_STATE_ESN) {
  732. seqhilen += sizeof(__be32);
  733. assoclen += seqhilen;
  734. }
  735. if (!skb_cloned(skb)) {
  736. if (!skb_is_nonlinear(skb)) {
  737. nfrags = 1;
  738. goto skip_cow;
  739. } else if (!skb_has_frag_list(skb)) {
  740. nfrags = skb_shinfo(skb)->nr_frags;
  741. nfrags++;
  742. goto skip_cow;
  743. }
  744. }
  745. nfrags = skb_cow_data(skb, 0, &trailer);
  746. if (nfrags < 0) {
  747. ret = -EINVAL;
  748. goto out;
  749. }
  750. skip_cow:
  751. ret = -ENOMEM;
  752. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  753. if (!tmp)
  754. goto out;
  755. ESP_SKB_CB(skb)->tmp = tmp;
  756. seqhi = esp_tmp_extra(tmp);
  757. iv = esp_tmp_iv(aead, tmp, seqhilen);
  758. req = esp_tmp_req(aead, iv);
  759. sg = esp_req_sg(aead, req);
  760. esp_input_set_header(skb, seqhi);
  761. sg_init_table(sg, nfrags);
  762. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  763. if (unlikely(ret < 0)) {
  764. kfree(tmp);
  765. goto out;
  766. }
  767. skb->ip_summed = CHECKSUM_NONE;
  768. if ((x->props.flags & XFRM_STATE_ESN))
  769. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  770. else
  771. aead_request_set_callback(req, 0, esp_input_done, skb);
  772. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  773. aead_request_set_ad(req, assoclen);
  774. ret = crypto_aead_decrypt(req);
  775. if (ret == -EINPROGRESS)
  776. goto out;
  777. if ((x->props.flags & XFRM_STATE_ESN))
  778. esp_input_restore_header(skb);
  779. ret = esp6_input_done2(skb, ret);
  780. out:
  781. return ret;
  782. }
  783. static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  784. u8 type, u8 code, int offset, __be32 info)
  785. {
  786. struct net *net = dev_net(skb->dev);
  787. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  788. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  789. struct xfrm_state *x;
  790. if (type != ICMPV6_PKT_TOOBIG &&
  791. type != NDISC_REDIRECT)
  792. return 0;
  793. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  794. esph->spi, IPPROTO_ESP, AF_INET6);
  795. if (!x)
  796. return 0;
  797. if (type == NDISC_REDIRECT)
  798. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  799. sock_net_uid(net, NULL));
  800. else
  801. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  802. xfrm_state_put(x);
  803. return 0;
  804. }
  805. static void esp6_destroy(struct xfrm_state *x)
  806. {
  807. struct crypto_aead *aead = x->data;
  808. if (!aead)
  809. return;
  810. crypto_free_aead(aead);
  811. }
  812. static int esp_init_aead(struct xfrm_state *x, struct netlink_ext_ack *extack)
  813. {
  814. char aead_name[CRYPTO_MAX_ALG_NAME];
  815. struct crypto_aead *aead;
  816. int err;
  817. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  818. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) {
  819. NL_SET_ERR_MSG(extack, "Algorithm name is too long");
  820. return -ENAMETOOLONG;
  821. }
  822. aead = crypto_alloc_aead(aead_name, 0, 0);
  823. err = PTR_ERR(aead);
  824. if (IS_ERR(aead))
  825. goto error;
  826. x->data = aead;
  827. err = crypto_aead_setkey(aead, x->aead->alg_key,
  828. (x->aead->alg_key_len + 7) / 8);
  829. if (err)
  830. goto error;
  831. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  832. if (err)
  833. goto error;
  834. return 0;
  835. error:
  836. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  837. return err;
  838. }
  839. static int esp_init_authenc(struct xfrm_state *x,
  840. struct netlink_ext_ack *extack)
  841. {
  842. struct crypto_aead *aead;
  843. struct crypto_authenc_key_param *param;
  844. struct rtattr *rta;
  845. char *key;
  846. char *p;
  847. char authenc_name[CRYPTO_MAX_ALG_NAME];
  848. unsigned int keylen;
  849. int err;
  850. err = -ENAMETOOLONG;
  851. if ((x->props.flags & XFRM_STATE_ESN)) {
  852. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  853. "%s%sauthencesn(%s,%s)%s",
  854. x->geniv ?: "", x->geniv ? "(" : "",
  855. x->aalg ? x->aalg->alg_name : "digest_null",
  856. x->ealg->alg_name,
  857. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
  858. NL_SET_ERR_MSG(extack, "Algorithm name is too long");
  859. goto error;
  860. }
  861. } else {
  862. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  863. "%s%sauthenc(%s,%s)%s",
  864. x->geniv ?: "", x->geniv ? "(" : "",
  865. x->aalg ? x->aalg->alg_name : "digest_null",
  866. x->ealg->alg_name,
  867. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) {
  868. NL_SET_ERR_MSG(extack, "Algorithm name is too long");
  869. goto error;
  870. }
  871. }
  872. aead = crypto_alloc_aead(authenc_name, 0, 0);
  873. err = PTR_ERR(aead);
  874. if (IS_ERR(aead)) {
  875. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  876. goto error;
  877. }
  878. x->data = aead;
  879. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  880. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  881. err = -ENOMEM;
  882. key = kmalloc(keylen, GFP_KERNEL);
  883. if (!key)
  884. goto error;
  885. p = key;
  886. rta = (void *)p;
  887. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  888. rta->rta_len = RTA_LENGTH(sizeof(*param));
  889. param = RTA_DATA(rta);
  890. p += RTA_SPACE(sizeof(*param));
  891. if (x->aalg) {
  892. struct xfrm_algo_desc *aalg_desc;
  893. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  894. p += (x->aalg->alg_key_len + 7) / 8;
  895. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  896. BUG_ON(!aalg_desc);
  897. err = -EINVAL;
  898. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  899. crypto_aead_authsize(aead)) {
  900. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  901. goto free_key;
  902. }
  903. err = crypto_aead_setauthsize(
  904. aead, x->aalg->alg_trunc_len / 8);
  905. if (err) {
  906. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  907. goto free_key;
  908. }
  909. }
  910. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  911. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  912. err = crypto_aead_setkey(aead, key, keylen);
  913. free_key:
  914. kfree(key);
  915. error:
  916. return err;
  917. }
  918. static int esp6_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
  919. {
  920. struct crypto_aead *aead;
  921. u32 align;
  922. int err;
  923. x->data = NULL;
  924. if (x->aead) {
  925. err = esp_init_aead(x, extack);
  926. } else if (x->ealg) {
  927. err = esp_init_authenc(x, extack);
  928. } else {
  929. NL_SET_ERR_MSG(extack, "ESP: AEAD or CRYPT must be provided");
  930. err = -EINVAL;
  931. }
  932. if (err)
  933. goto error;
  934. aead = x->data;
  935. x->props.header_len = sizeof(struct ip_esp_hdr) +
  936. crypto_aead_ivsize(aead);
  937. switch (x->props.mode) {
  938. case XFRM_MODE_BEET:
  939. if (x->sel.family != AF_INET6)
  940. x->props.header_len += IPV4_BEET_PHMAXLEN +
  941. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  942. break;
  943. default:
  944. case XFRM_MODE_TRANSPORT:
  945. break;
  946. case XFRM_MODE_TUNNEL:
  947. x->props.header_len += sizeof(struct ipv6hdr);
  948. break;
  949. }
  950. if (x->encap) {
  951. struct xfrm_encap_tmpl *encap = x->encap;
  952. switch (encap->encap_type) {
  953. default:
  954. NL_SET_ERR_MSG(extack, "Unsupported encapsulation type for ESP");
  955. err = -EINVAL;
  956. goto error;
  957. case UDP_ENCAP_ESPINUDP:
  958. x->props.header_len += sizeof(struct udphdr);
  959. break;
  960. #ifdef CONFIG_INET6_ESPINTCP
  961. case TCP_ENCAP_ESPINTCP:
  962. /* only the length field, TCP encap is done by
  963. * the socket
  964. */
  965. x->props.header_len += 2;
  966. break;
  967. #endif
  968. }
  969. }
  970. align = ALIGN(crypto_aead_blocksize(aead), 4);
  971. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  972. error:
  973. return err;
  974. }
  975. static int esp6_rcv_cb(struct sk_buff *skb, int err)
  976. {
  977. return 0;
  978. }
  979. static const struct xfrm_type esp6_type = {
  980. .owner = THIS_MODULE,
  981. .proto = IPPROTO_ESP,
  982. .flags = XFRM_TYPE_REPLAY_PROT,
  983. .init_state = esp6_init_state,
  984. .destructor = esp6_destroy,
  985. .input = esp6_input,
  986. .output = esp6_output,
  987. };
  988. static struct xfrm6_protocol esp6_protocol = {
  989. .handler = xfrm6_rcv,
  990. .input_handler = xfrm_input,
  991. .cb_handler = esp6_rcv_cb,
  992. .err_handler = esp6_err,
  993. .priority = 0,
  994. };
  995. static int __init esp6_init(void)
  996. {
  997. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  998. pr_info("%s: can't add xfrm type\n", __func__);
  999. return -EAGAIN;
  1000. }
  1001. if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
  1002. pr_info("%s: can't add protocol\n", __func__);
  1003. xfrm_unregister_type(&esp6_type, AF_INET6);
  1004. return -EAGAIN;
  1005. }
  1006. return 0;
  1007. }
  1008. static void __exit esp6_fini(void)
  1009. {
  1010. if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
  1011. pr_info("%s: can't remove protocol\n", __func__);
  1012. xfrm_unregister_type(&esp6_type, AF_INET6);
  1013. }
  1014. module_init(esp6_init);
  1015. module_exit(esp6_fini);
  1016. MODULE_DESCRIPTION("IPv6 ESP transformation helpers");
  1017. MODULE_LICENSE("GPL");
  1018. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);