esp6.c 29 KB

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